Compare commits

..

3 Commits

Author SHA1 Message Date
17357d4672 remove unused/unmaintained code.
this will likely re-appear in some form or another later on, but
currently I don't see its use.
2025-08-25 12:42:45 +02:00
5644d377de make helper functions for nbt_proctag have similar IO as itself.
fix: `procarr` was counting `nmem`, whilst we should be counting the
bytes.
fix: `procarr` wasn't actually writing to `struct nbt_array`'s `len`
member.
2025-08-25 12:41:12 +02:00
44d2271972 fix: return values and list processing was too funky with nbt_proctag.
- Updated documentation
- Fixed logic so `nbt_proctag` actually returns the next pointer.
2025-08-25 10:57:15 +02:00
2 changed files with 55 additions and 78 deletions

View File

@@ -98,48 +98,64 @@ const u8 *nbt_nexttag(const u8 *restrict buf) {
return tag;
}
/* processes an array at `buf`, of `nmem` items with a size of `size`
* 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) {
struct nbt_array *ptr = malloc(sizeof(struct nbt_array) + nmem * size);
if (!ptr) return NULL;
memcpy(ptr->dat, buf, nmem * size);
/* Processes the incoming array data in `buf`. Which contains `nmem` items of `size`.
* The data shall be converted to little-endian on little-endian systems
* Outputs the allocated data to `out`, returns where the next pointer would be. */
static const u8 *procarr(const u8 *restrict buf, i32 nmem, uint size, struct nbt_array *restrict *restrict out) {
size_t len = nmem * size;
*out = malloc(sizeof(struct nbt_array) + len);
if (!*out) return buf + len;
memcpy((*out)->dat, buf, len);
(*out)->len = nmem;
buf += len;
/* Only include this code for little-endian systems. Since only they require this logic.
* Producing optimised code for other platforms. */
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
if (size == 1) return ptr;
ssize_t i = 0;
while (i < nmem) {
if (size == 1) return buf;
size_t i = 0;
while (i < len) {
switch (size) {
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;
case 2: *(u16 *)((*out)->dat + i) = be16toh(*(u16 *)((*out)->dat + i)); break;
case 4: *(u32 *)((*out)->dat + i) = be32toh(*(u32 *)((*out)->dat + i)); break;
case 8: *(u64 *)((*out)->dat + i) = be64toh(*(u64 *)((*out)->dat + i)); break;
default: __builtin_unreachable(); // this should be impossible
}
i += size;
}
#endif
return ptr;
return buf;
}
/* 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) {
/* calls `procarr` for the simple types available. */
static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict *restrict out) {
uint size;
*out = NULL;
switch (*buf) {
case NBT_I8: return nbt_procarr(buf + 5, (i32)be32toh(*(u32 *)buf + 1), 1);
case NBT_I16: return nbt_procarr(buf + 5, (i32)be32toh(*(u32 *)buf + 1), 2);
case NBT_I8: size = 1; break;
case NBT_I16: size = 2; break;
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_F64: return nbt_procarr(buf + 5, (i32)be32toh(*(u32 *)buf + 1), 8);
case NBT_F64: size = 8; break;
default: return NULL;
}
buf++;
i32 len = (i32)be32toh(*(u32 *)buf);
buf += 4;
return procarr(buf, len, size, out);
}
/* 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 *ptr = buf + 3 + slen;
const u8 *ptr, *tmp;
ptr = buf + 3 + slen;
i32 nmem;
uint size;
switch (*buf) {
case NBT_I8: *(u8 *)out = *ptr; return ptr + 1;
@@ -149,38 +165,17 @@ const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
case NBT_I64: // fall through
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_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_STR: nmem = be16toh(*(u16 *)ptr), size = 1, ptr += 2; break;
case NBT_ARR_I8: nmem = be32toh(*(u32 *)ptr), size = 1, ptr += 4; break;
case NBT_ARR_I32: nmem = be32toh(*(u32 *)ptr), size = 4, ptr += 4; break;
case NBT_ARR_I64: nmem = be32toh(*(u32 *)ptr), size = 8, ptr += 4; break;
default:
return NULL;
case NBT_LIST:
return proclist(ptr, (struct nbt_array **)out);
return tmp;
default: return NULL;
}
return ptr;
}
struct nbt_procdat nbt_initproc(struct nbt_path const *restrict pats, uint npats) {
i16 mdpt = 0;
// acquire the maximum depth that we'll need to go (exclusive)
for (uint i = 0; i < npats; i++) {
int tmp = pats[i].len - mdpt;
mdpt += -(tmp > 0) & tmp;
}
assert(mdpt > 0);
// storing the segments of the current path
const char **cpat = (const char **)calloc(mdpt - 1, sizeof(void *));
// return the initialised structure.
return (struct nbt_procdat){
pats,
cpat,
npats,
0,
mdpt,
};
return procarr(ptr, nmem, size, (struct nbt_array **)out);
}

View File

@@ -36,20 +36,6 @@ enum nbt_tagid {
NBT_ARR_I64 = 0x0C, // starts with a i32, denoting size, followed by the u32 data
};
/* TODO: write doc */
struct nbt_path {
const char **restrict pat; // specifies the NBT path components as separate elements
i16 len; // specifies the length of the NBT elements
};
/* TODO: write doc */
struct nbt_procdat {
const struct nbt_path *pats;
const char *restrict *cpat;
u32 npats;
i16 dpt, mdpt;
};
struct nbt_array {
i32 len;
u8 dat[];
@@ -59,15 +45,11 @@ struct nbt_array {
* `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, 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 (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. */
/* Acquires the data contained by the named tag.
* - `buf` points to the start of the tag.
* - `slen` contains the string length of the name.
* - `out` points to where the data should be written.
* if `buf` points to `NBT_I8`, `NBT_I16`, `NBT_I32`, `NBT_I64`, `NBT_F32`, or `NBT_F64`, `*out` is assumed
* to have the available byte width for one of these types. In the case of `NBT_ARR*` and `NBT_LIST`
* 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. */
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 */
struct nbt_procdat nbt_initproc(struct nbt_path const *restrict pats, uint npats) NONNULL((1)) PURE;