From 4728846985160dc7901834185287972d2aeabacf Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 15 Jul 2025 13:02:08 +0200 Subject: [PATCH] write a function for skipping to the next NBT tag --- src/dat/nbt.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/dat/nbt.c b/src/dat/nbt.c index 6da6055..7035cef 100644 --- a/src/dat/nbt.c +++ b/src/dat/nbt.c @@ -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