mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 09:45:45 +01:00
Compare commits
3 Commits
545eecca37
...
17357d4672
| Author | SHA1 | Date | |
|---|---|---|---|
| 17357d4672 | |||
| 5644d377de | |||
| 44d2271972 |
101
src/dat/nbt.c
101
src/dat/nbt.c
@@ -98,48 +98,64 @@ const u8 *nbt_nexttag(const u8 *restrict buf) {
|
|||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* processes an array at `buf`, of `nmem` items with a size of `size`
|
/* Processes the incoming array data in `buf`. Which contains `nmem` items of `size`.
|
||||||
* returns a malloc'd pointer (which may be `NULL`) to the data.
|
* The data shall be converted to little-endian on little-endian systems
|
||||||
* the data is converted from big endian to little endian on little endian systems. */
|
* Outputs the allocated data to `out`, returns where the next pointer would be. */
|
||||||
MALLOC static void *nbt_procarr(const u8 *restrict buf, i32 nmem, uint size) {
|
static const u8 *procarr(const u8 *restrict buf, i32 nmem, uint size, struct nbt_array *restrict *restrict out) {
|
||||||
struct nbt_array *ptr = malloc(sizeof(struct nbt_array) + nmem * size);
|
size_t len = nmem * size;
|
||||||
if (!ptr) return NULL;
|
*out = malloc(sizeof(struct nbt_array) + len);
|
||||||
memcpy(ptr->dat, buf, nmem * size);
|
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.
|
/* Only include this code for little-endian systems. Since only they require this logic.
|
||||||
* Producing optimised code for other platforms. */
|
* Producing optimised code for other platforms. */
|
||||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||||
if (size == 1) return ptr;
|
if (size == 1) return buf;
|
||||||
ssize_t i = 0;
|
size_t i = 0;
|
||||||
while (i < nmem) {
|
while (i < len) {
|
||||||
switch (size) {
|
switch (size) {
|
||||||
case 2: *(u16 *)(ptr->dat + i) = be16toh(*(u16 *)(ptr->dat + i)); break;
|
case 2: *(u16 *)((*out)->dat + i) = be16toh(*(u16 *)((*out)->dat + i)); break;
|
||||||
case 4: *(u32 *)(ptr->dat + i) = be32toh(*(u32 *)(ptr->dat + i)); break;
|
case 4: *(u32 *)((*out)->dat + i) = be32toh(*(u32 *)((*out)->dat + i)); break;
|
||||||
case 8: *(u64 *)(ptr->dat + i) = be64toh(*(u64 *)(ptr->dat + i)); break;
|
case 8: *(u64 *)((*out)->dat + i) = be64toh(*(u64 *)((*out)->dat + i)); break;
|
||||||
default: __builtin_unreachable(); // this should be impossible
|
default: __builtin_unreachable(); // this should be impossible
|
||||||
}
|
}
|
||||||
i += size;
|
i += size;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return ptr;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* processes a `NBT_LIST` tag, and returns a pointer to malloc'd data, or `NULL`, depending on its success */
|
/* calls `procarr` for the simple types available. */
|
||||||
MALLOC static void *nbt_proclist(const u8 *restrict buf) {
|
static const u8 *proclist(const u8 *restrict buf, struct nbt_array *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;
|
||||||
|
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 *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,38 +165,17 @@ 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;
|
return proclist(ptr, (struct nbt_array **)out);
|
||||||
|
return tmp;
|
||||||
|
|
||||||
|
default: return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ptr;
|
return procarr(ptr, nmem, size, (struct nbt_array **)out);
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,20 +36,6 @@ enum nbt_tagid {
|
|||||||
NBT_ARR_I64 = 0x0C, // starts with a i32, denoting size, followed by the u32 data
|
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 {
|
struct nbt_array {
|
||||||
i32 len;
|
i32 len;
|
||||||
u8 dat[];
|
u8 dat[];
|
||||||
@@ -59,15 +45,11 @@ 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 */
|
|
||||||
struct nbt_procdat nbt_initproc(struct nbt_path const *restrict pats, uint npats) NONNULL((1)) PURE;
|
|
||||||
|
|||||||
Reference in New Issue
Block a user