Compare commits

...

3 Commits

Author SHA1 Message Date
e67d8c7fd4 fix: lens[dpt] is not a type, should be tags[dpt] 2025-08-20 16:45:53 +02:00
23b64199d7 implement handling compound lists 2025-08-20 16:43:01 +02:00
6a9d6f75a4 fix: fatal macro not printing to stderr 2025-08-20 11:46:31 +02:00
2 changed files with 46 additions and 10 deletions

View File

@@ -9,17 +9,34 @@
#include "../util/compat/endian.h"
#include "../util/intdef.h"
#define MAX_DEPTH 512
/* TODO: write test cases for this function:
* - list:compound...
* - non-existent type
* - compound:list:int32
* - string
*/
const u8 *nbt_nexttag(const u8 *restrict buf) {
const u8 *tag, *ptr;
u8 tags[MAX_DEPTH] = {0};
i32 lens[MAX_DEPTH] = {0};
uint dpt = 0;
u8 type;
tag = buf;
do {
ptr = tag + be16toh(*(u16 *)(tag + 1)) + 3; // set `ptr` to start of data
mems = 0;
ptr = tag;
if (lens[dpt]) {
type = tags[dpt];
lens[dpt]--;
dpt -= !lens[dpt];
} else {
type = *tag;
ptr += be16toh(*(u16 *)(tag + 1)) + 3;
}
switch (*tag) {
switch (type) {
case NBT_I8: ptr += 1; break;
case NBT_I16: ptr += 2; break;
case NBT_I32: // fall through
@@ -33,9 +50,28 @@ const u8 *nbt_nexttag(const u8 *restrict buf) {
case NBT_STR: ptr += 2 + (u16)be16toh(*(u16 *)ptr) * 1; break;
case NBT_END: dpt--; break;
case NBT_COMPOUND: dpt++; break;
case NBT_COMPOUND: dpt += (tags[dpt] && *tag) ? 1 : -1; break;
// TODO: handle (compound) lists somehow
case NBT_LIST: {
tag = ptr; // temporarily store the tag to cache later
switch (*(ptr++)) {
case NBT_I8: ptr += 1 * (i32)be32toh(*(u32 *)ptr); break;
case NBT_I16: ptr += 2 * (i32)be32toh(*(u32 *)ptr); break;
case NBT_I32: // fall through
case NBT_F32: ptr += 4 * (i32)be32toh(*(u32 *)ptr); break;
case NBT_I64: // fall through
case NBT_F64: ptr += 8 * (i32)be32toh(*(u32 *)ptr); break;
default:
// TODO: handle out of bounds... Might not be required if we use flexible array member
dpt++;
tags[dpt] = *tag;
lens[dpt] = (i32)be32toh(*(u32 *)ptr);
break;
}
ptr += 4;
break;
}
default: return NULL; // unexpected value; buffer is likely corrupt
}

View File

@@ -14,8 +14,8 @@
#define warn(s, ...) fprintf(stderr, "\033[93m" __FILE__ ":" MACRO_STR2(__LINE__) ": [WAR]: " s "\033[0m\n", ##__VA_ARGS__)
#define error(s, ...) fprintf(stderr, "\033[91m" __FILE__ ":" MACRO_STR2(__LINE__) ": [ERR]: " s "\033[0m\n", ##__VA_ARGS__)
#define fatal(s, ...) \
do { \
printf("\033[101m" __FILE__ ":" MACRO_STR2(__LINE__) ": [FAT]: " s "\033[0m\n", ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
#define fatal(s, ...) \
do { \
fprintf(stderr, "\033[101m" __FILE__ ":" MACRO_STR2(__LINE__) ": [FAT]: " s "\033[0m\n", ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)