fix: return values and list processing was too funky with nbt_proctag.

- Updated documentation
- Fixed logic so `nbt_proctag` actually returns the next pointer.
This commit is contained in:
2025-08-25 10:57:15 +02:00
parent 545eecca37
commit 44d2271972
2 changed files with 38 additions and 23 deletions

View File

@@ -125,21 +125,34 @@ MALLOC static void *nbt_procarr(const u8 *restrict buf, i32 nmem, uint size) {
} }
/* processes a `NBT_LIST` tag, and returns a pointer to malloc'd data, or `NULL`, depending on its success */ /* processes a `NBT_LIST` tag, and returns a pointer to malloc'd data, or `NULL`, depending on its success */
MALLOC static void *nbt_proclist(const u8 *restrict buf) { MALLOC static void *nbt_proclist(const u8 *restrict buf, const u8 *restrict *restrict out) {
uint size;
*out = NULL;
switch (*buf) { switch (*buf) {
case NBT_I8: return nbt_procarr(buf + 5, (i32)be32toh(*(u32 *)buf + 1), 1); case NBT_I8: size = 1; break;
case NBT_I16: return nbt_procarr(buf + 5, (i32)be32toh(*(u32 *)buf + 1), 2); case NBT_I16: size = 2; break;
case NBT_I32: // fall through case NBT_I32: // fall through
case NBT_F32: return nbt_procarr(buf + 5, (i32)be32toh(*(u32 *)buf + 1), 4); case NBT_F32: size = 4; break;
case NBT_I64: // fall though case NBT_I64: // fall though
case NBT_F64: return nbt_procarr(buf + 5, (i32)be32toh(*(u32 *)buf + 1), 8); case NBT_F64: size = 8; break;
default: return NULL; default: return NULL;
} }
buf++;
i32 len = (i32)be32toh(*(u32 *)buf);
buf += 4;
*out = buf + (len * size);
return nbt_procarr(buf, len, size);
} }
/* readies the output data for export, returns the new buffer position, or `NULL` upon an error (may be out of bounds) */
const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) { const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
const u8 *ptr = buf + 3 + slen; const u8 *ptr, *tmp;
ptr = buf + 3 + slen;
i32 nmem;
uint size;
switch (*buf) { switch (*buf) {
case NBT_I8: *(u8 *)out = *ptr; return ptr + 1; case NBT_I8: *(u8 *)out = *ptr; return ptr + 1;
@@ -149,17 +162,20 @@ const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
case NBT_I64: // fall through case NBT_I64: // fall through
case NBT_F64: *(u64 *)out = be16toh(*(u64 *)ptr); return ptr + 8; case NBT_F64: *(u64 *)out = be16toh(*(u64 *)ptr); return ptr + 8;
case NBT_STR: *(void **)out = nbt_procarr(ptr += 2, be16toh(*(u16 *)buf), 1); break; case NBT_STR: nmem = be16toh(*(u16 *)ptr), size = 1, ptr += 2; break;
case NBT_ARR_I8: *(void **)out = nbt_procarr(ptr += 4, be32toh(*(u32 *)buf), 1); break; case NBT_ARR_I8: nmem = be32toh(*(u32 *)ptr), size = 1, ptr += 4; break;
case NBT_ARR_I32: *(void **)out = nbt_procarr(ptr += 4, be32toh(*(u32 *)buf), 4); break; case NBT_ARR_I32: nmem = be32toh(*(u32 *)ptr), size = 4, ptr += 4; break;
case NBT_ARR_I64: *(void **)out = nbt_procarr(ptr += 8, be32toh(*(u64 *)buf), 8); break; case NBT_ARR_I64: nmem = be32toh(*(u32 *)ptr), size = 8, ptr += 4; break;
case NBT_LIST: *(void **)out = nbt_proclist(ptr);
default: case NBT_LIST:
return NULL; *(void **)out = nbt_proclist(ptr, &tmp);
return tmp;
default: return NULL;
} }
return ptr; *(void **)out = nbt_procarr(ptr, nmem, size);
return ptr + (size * nmem);
} }
struct nbt_procdat nbt_initproc(struct nbt_path const *restrict pats, uint npats) { struct nbt_procdat nbt_initproc(struct nbt_path const *restrict pats, uint npats) {

View File

@@ -59,14 +59,13 @@ struct nbt_array {
* `NULL` is returned upon failure, the otherwise returned pointer is not guaranteed to be valid. */ * `NULL` is returned upon failure, the otherwise returned pointer is not guaranteed to be valid. */
const u8 *nbt_nexttag(const u8 *restrict buf) NONNULL((1)) PURE; const u8 *nbt_nexttag(const u8 *restrict buf) NONNULL((1)) PURE;
/* Processes the tag entered in `buf`, `buf` is assumed to be the start of a named tag. Where `slen` shall be the string length. /* Acquires the data contained by the named tag.
* The data in `buf` is processed and outputted to `out`. A pointer to the next tag, or `NULL` is returned. * - `buf` points to the start of the tag.
* On little-endian systems, the data is processed from big-endian to little-endian. So it can be used like normal. * - `slen` contains the string length of the name.
* - In the case for all basic types, `out` will require to be the width of said type. * - `out` points to where the data should be written.
* - In the case of arrays (and lists), a malloc'd pointer shall be written to `out`, pointing to `struct nbt_array`. This might be `NULL` * if `buf` points to `NBT_I8`, `NBT_I16`, `NBT_I32`, `NBT_I64`, `NBT_F32`, or `NBT_F64`, `*out` is assumed
* - In the case of lists, the above is valid, as long as the list contains any of the following types: * to have the available byte width for one of these types. In the case of `NBT_ARR*` and `NBT_LIST`
* `NBT_I8`, `NBT_I16`, `NBT_I32`, `NBT_I64`, `NBT_F32` or `NBT_F64`. Anything else will result in a `NULL` pointer. * it must point to a `struct nbt_array*`. Where in the case of `NBT_LIST`, it must be one of the previous static-width types. */
* Upon failure, like the tag not being able to be processed, `NULL` is returned. */
const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) NONNULL((1, 3)); const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) NONNULL((1, 3));
/* initialises a data structure used whilst processing the tags */ /* initialises a data structure used whilst processing the tags */