use a flexible array member for including length with array data

This commit is contained in:
2025-08-24 13:21:05 +02:00
parent a6442a851e
commit 6dae1d1600
2 changed files with 17 additions and 10 deletions

View File

@@ -102,9 +102,9 @@ const u8 *nbt_nexttag(const u8 *restrict buf) {
* returns a malloc'd pointer (which may be `NULL`) to the data.
* the data is converted from big endian to little endian on little endian systems. */
MALLOC static void *nbt_procarr(const u8 *restrict buf, i32 nmem, uint size) {
u8 *ptr = malloc(nmem * size);
struct nbt_array *ptr = malloc(sizeof(struct nbt_array) + nmem * size);
if (!ptr) return NULL;
memcpy(ptr, buf, nmem * size);
memcpy(ptr->dat, buf, nmem * size);
/* Only include this code for little-endian systems. Since only they require this logic.
* Producing optimised code for other platforms. */
@@ -113,9 +113,9 @@ MALLOC static void *nbt_procarr(const u8 *restrict buf, i32 nmem, uint size) {
ssize_t i = 0;
while (i < nmem) {
switch (size) {
case 2: *(u16 *)(ptr + i) = be16toh(*(u16 *)(ptr + i)); break;
case 4: *(u32 *)(ptr + i) = be32toh(*(u32 *)(ptr + i)); break;
case 8: *(u64 *)(ptr + i) = be64toh(*(u64 *)(ptr + i)); break;
case 2: *(u16 *)(ptr->dat + i) = be16toh(*(u16 *)(ptr->dat + i)); break;
case 4: *(u32 *)(ptr->dat + i) = be32toh(*(u32 *)(ptr->dat + i)); break;
case 8: *(u64 *)(ptr->dat + i) = be64toh(*(u64 *)(ptr->dat + i)); break;
default: __builtin_unreachable(); // this should be impossible
}
i += size;
@@ -153,8 +153,8 @@ const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
case NBT_ARR_I8: *(void **)out = nbt_procarr(ptr += 4, be32toh(*(u32 *)buf), 1); break;
case NBT_ARR_I32: *(void **)out = nbt_procarr(ptr += 4, be32toh(*(u32 *)buf), 4); break;
case NBT_ARR_I64: *(void **)out = nbt_procarr(ptr += 8, be32toh(*(u64 *)buf), 8); break;
case NBT_LIST: *(void **)out = nbt_proclist(ptr);
case NBT_LIST: return nbt_proclist(ptr);
default:
return NULL;
}

View File

@@ -50,16 +50,23 @@ struct nbt_procdat {
i16 dpt, mdpt;
};
struct nbt_array {
i32 len;
u8 dat[];
};
/* searches for the end of a named tag without processing data, the final pointer is returned.
* `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;
/* Processes the tag entered in `buf`, `buf` is assumed to be the start of a named tag. Where `slen` shall be the string length.
* The data in `buf` is processed and outputted to `out`. A pointer to the next tag is returned.
* The data in `buf` is processed and outputted to `out`. A pointer to the next tag, or `NULL` is returned.
* On little-endian systems, the data is processed from big-endian to little-endian. So it can be used like normal.
* - In the case for all basic types, `out` will require to be the width of said type.
* - In the case of arrays, a pointer shall be returned pointing to the data. TODO: it'd be nice to know how large this array is.
* - In the case of `NBT_LIST`, if it is of the type `NBT_Ixx` and `NBT_Fxx`, then it's handled. Otherwise the funcion shall fail.
* Upon failure, `NULL` is returned. */
* - 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`
* - In the case of lists, the above is valid, as long as the list contains any of the following types:
* `NBT_I8`, `NBT_I16`, `NBT_I32`, `NBT_I64`, `NBT_F32` or `NBT_F64`. Anything else will result in a `NULL` pointer.
* 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));
/* initialises a data structure used whilst processing the tags */