mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 09:45:45 +01:00
Compare commits
2 Commits
3eb92541db
...
9996e84ef5
| Author | SHA1 | Date | |
|---|---|---|---|
| 9996e84ef5 | |||
| 949cac5bfe |
103
src/dat/nbt.c
103
src/dat/nbt.c
@@ -11,26 +11,45 @@
|
||||
|
||||
#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;
|
||||
/* handles incrementing to the next tag in the case of `NBT_LIST`. This function shan't return `NULL`.
|
||||
* `ptr` is assumed to be the start of the `NBT_LIST` data, e.i. The list's ID, followed by the list's length.
|
||||
* If `ID` is `NBT_I8`, `NBT_I16`, `NBT_I32`, `NBT_I64`, `NBT_F32`, or `NBT_F64`, the entire list length is computed and returned.
|
||||
* For other types this won't be possible, and thus will add `1` to `dpt`, and write the list data to `lens` and `tags` at this new `dpt`. */
|
||||
static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) {
|
||||
const u8 *tag = ptr;
|
||||
ptr++;
|
||||
switch (*tag) {
|
||||
case NBT_END: break;
|
||||
case NBT_I8: ptr += (i32)be32toh(*(u32 *)ptr) * 1; break;
|
||||
case NBT_I16: ptr += (i32)be32toh(*(u32 *)ptr) * 2; break;
|
||||
case NBT_I32: // fall through
|
||||
case NBT_F32: ptr += (i32)be32toh(*(u32 *)ptr) * 4; break;
|
||||
case NBT_I64: // fall through
|
||||
case NBT_F64: ptr += (i32)be32toh(*(u32 *)ptr) * 8; 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;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
tag = buf;
|
||||
do {
|
||||
ptr = tag;
|
||||
if (lens[dpt]) {
|
||||
type = tags[dpt];
|
||||
lens[dpt]--;
|
||||
dpt -= !lens[dpt];
|
||||
/* increments to the next tag and returns it (or `NULL`)
|
||||
* - `tag` represents the start of the tag, e.i. The tag ID, or in the case of `NBT_LIST` data, the start of this data.
|
||||
* - `dpt` shall point to the "depth" we're at, this is used as index for `lens` and `tags`
|
||||
* - `lens` shall contain `MAX_DEPTH` of items representing the list length, if the current item is non-zero we shall assume we're in a list.
|
||||
* Where the value is decremented until we reach `0`.
|
||||
* - `tags` shall contain `MAX_DEPTH` of items representing the list's stored type. */
|
||||
static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) {
|
||||
u8 type;
|
||||
const u8 *ptr = tag;
|
||||
if (lens[*dpt]) {
|
||||
type = tags[*dpt];
|
||||
lens[*dpt]--;
|
||||
*dpt -= !lens[*dpt];
|
||||
} else {
|
||||
type = *tag;
|
||||
ptr += be16toh(*(u16 *)(tag + 1)) + 3;
|
||||
@@ -49,36 +68,32 @@ const u8 *nbt_nexttag(const u8 *restrict buf) {
|
||||
case NBT_ARR_I64: ptr += 4 + (i32)be32toh(*(u32 *)ptr) * 8; break;
|
||||
case NBT_STR: ptr += 2 + (u16)be16toh(*(u16 *)ptr) * 1; break;
|
||||
|
||||
case NBT_END: dpt--; break;
|
||||
case NBT_COMPOUND: dpt++; break;
|
||||
case NBT_END: (*dpt)--; break;
|
||||
case NBT_COMPOUND: (*dpt)++; break;
|
||||
|
||||
// TODO: move this into it's own function for readability.
|
||||
case NBT_LIST: {
|
||||
tag = ptr;
|
||||
ptr++;
|
||||
switch (*tag) {
|
||||
case NBT_END: break;
|
||||
case NBT_I8: ptr += (i32)be32toh(*(u32 *)ptr) * 1; break;
|
||||
case NBT_I16: ptr += (i32)be32toh(*(u32 *)ptr) * 2; break;
|
||||
case NBT_I32: // fall through
|
||||
case NBT_F32: ptr += (i32)be32toh(*(u32 *)ptr) * 4; break;
|
||||
case NBT_I64: // fall through
|
||||
case NBT_F64: ptr += (i32)be32toh(*(u32 *)ptr) * 8; 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;
|
||||
}
|
||||
case NBT_LIST: ptr = nexttag_list(ptr, dpt, lens, tags); break;
|
||||
|
||||
default: return NULL; // unexpected value; buffer is likely corrupt
|
||||
}
|
||||
|
||||
tag = ptr;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
u8 tags[MAX_DEPTH] = {0};
|
||||
i32 lens[MAX_DEPTH] = {0};
|
||||
uint dpt = 0;
|
||||
|
||||
tag = buf;
|
||||
do {
|
||||
tag = nexttag(tag, &dpt, lens, tags);
|
||||
} while (dpt > 0);
|
||||
return tag;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user