mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 20:35:45 +01:00
Compare commits
3 Commits
e018e1ff1b
...
e67d8c7fd4
| Author | SHA1 | Date | |
|---|---|---|---|
| e67d8c7fd4 | |||
| 23b64199d7 | |||
| 6a9d6f75a4 |
@@ -9,17 +9,34 @@
|
|||||||
#include "../util/compat/endian.h"
|
#include "../util/compat/endian.h"
|
||||||
#include "../util/intdef.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 *nbt_nexttag(const u8 *restrict buf) {
|
||||||
const u8 *tag, *ptr;
|
const u8 *tag, *ptr;
|
||||||
|
u8 tags[MAX_DEPTH] = {0};
|
||||||
|
i32 lens[MAX_DEPTH] = {0};
|
||||||
uint dpt = 0;
|
uint dpt = 0;
|
||||||
|
u8 type;
|
||||||
|
|
||||||
tag = buf;
|
tag = buf;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
ptr = tag + be16toh(*(u16 *)(tag + 1)) + 3; // set `ptr` to start of data
|
ptr = tag;
|
||||||
mems = 0;
|
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_I8: ptr += 1; break;
|
||||||
case NBT_I16: ptr += 2; break;
|
case NBT_I16: ptr += 2; break;
|
||||||
case NBT_I32: // fall through
|
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_STR: ptr += 2 + (u16)be16toh(*(u16 *)ptr) * 1; break;
|
||||||
|
|
||||||
case NBT_END: dpt--; 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
|
default: return NULL; // unexpected value; buffer is likely corrupt
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,8 @@
|
|||||||
#define warn(s, ...) fprintf(stderr, "\033[93m" __FILE__ ":" MACRO_STR2(__LINE__) ": [WAR]: " s "\033[0m\n", ##__VA_ARGS__)
|
#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 error(s, ...) fprintf(stderr, "\033[91m" __FILE__ ":" MACRO_STR2(__LINE__) ": [ERR]: " s "\033[0m\n", ##__VA_ARGS__)
|
||||||
|
|
||||||
#define fatal(s, ...) \
|
#define fatal(s, ...) \
|
||||||
do { \
|
do { \
|
||||||
printf("\033[101m" __FILE__ ":" MACRO_STR2(__LINE__) ": [FAT]: " s "\033[0m\n", ##__VA_ARGS__); \
|
fprintf(stderr, "\033[101m" __FILE__ ":" MACRO_STR2(__LINE__) ": [FAT]: " s "\033[0m\n", ##__VA_ARGS__); \
|
||||||
exit(EXIT_FAILURE); \
|
exit(EXIT_FAILURE); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|||||||
Reference in New Issue
Block a user