write a function for skipping to the next NBT tag

This commit is contained in:
2025-07-15 13:02:08 +02:00
parent db76d6992b
commit 4728846985

View File

@@ -24,6 +24,31 @@ static int nbt_cmpstr(char const *restrict matstr, u8 const *restrict buf) {
return strncmp(str, matstr, len);
}
/* returns the (expected) pointer of the tag following this one.
* `NBT_COMPOUND` and `NBT_END` tags are not valid for this function and should be handled separately.
* `NULL` is returned if anything went wrong. */
static u8 const *nbt_nexttag(u8 *buf) {
u8 const *nxt = NULL;
switch (*buf) {
case NBT_I8: nxt = buf + 1 + 1; break; // add 1 for the tag size here, since the constant can be precomputed
case NBT_I16: nxt = buf + 1 + 2; break;
case NBT_I32: nxt = buf + 1 + 4; break;
case NBT_I64: nxt = buf + 1 + 8; break;
case NBT_F32: nxt = buf + 1 + 4; break;
case NBT_F64: nxt = buf + 1 + 8; break;
case NBT_ARR_I8:
case NBT_STR: break;
case NBT_LIST: break;
case NBT_ARR_I32: break;
case NBT_ARR_I64: break;
default: return NULL; // failure on compound/end tags; these require more nuanced logic
}
return nxt + nbt_strlen(buf + 1);
}
int nbt_proc(void **restrict datout, u8 const *restrict buf, size_t len) {
// first byte should be a compound tag