mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 20:35:45 +01:00
Compare commits
5 Commits
29c8a2b6ea
...
6dae1d1600
| Author | SHA1 | Date | |
|---|---|---|---|
| 6dae1d1600 | |||
| a6442a851e | |||
| ec04177f45 | |||
| 40b8c0ef30 | |||
| 7feb193c51 |
@@ -98,10 +98,13 @@ 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`
|
||||||
|
* 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) {
|
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;
|
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.
|
/* 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. */
|
||||||
@@ -110,31 +113,35 @@ MALLOC static void *nbt_procarr(const u8 *restrict buf, i32 nmem, uint size) {
|
|||||||
ssize_t i = 0;
|
ssize_t i = 0;
|
||||||
while (i < nmem) {
|
while (i < nmem) {
|
||||||
switch (size) {
|
switch (size) {
|
||||||
case 2:
|
case 2: *(u16 *)(ptr->dat + i) = be16toh(*(u16 *)(ptr->dat + i)); break;
|
||||||
*(u16 *)(ptr + i) = be16toh(*(u16 *)(ptr + i));
|
case 4: *(u32 *)(ptr->dat + i) = be32toh(*(u32 *)(ptr->dat + i)); break;
|
||||||
i += 2;
|
case 8: *(u64 *)(ptr->dat + i) = be64toh(*(u64 *)(ptr->dat + i)); break;
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
*(u32 *)(ptr + i) = be32toh(*(u32 *)(ptr + i));
|
|
||||||
i += 4;
|
|
||||||
break;
|
|
||||||
case 8:
|
|
||||||
*(u64 *)(ptr + i) = be64toh(*(u64 *)(ptr + i));
|
|
||||||
i += 8;
|
|
||||||
break;
|
|
||||||
default: __builtin_unreachable(); // this should be impossible
|
default: __builtin_unreachable(); // this should be impossible
|
||||||
}
|
}
|
||||||
|
i += size;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 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) {
|
||||||
|
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_I32: // fall through
|
||||||
|
case NBT_F32: return nbt_procarr(buf + 5, (i32)be32toh(*(u32 *)buf + 1), 4);
|
||||||
|
case NBT_I64: // fall though
|
||||||
|
case NBT_F64: return nbt_procarr(buf + 5, (i32)be32toh(*(u32 *)buf + 1), 8);
|
||||||
|
default: return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* readies the output data for export, returns the new buffer position, or `NULL` upon an error (may be out of bounds) */
|
/* 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 = buf + 3 + slen;
|
||||||
|
|
||||||
switch (*buf) {
|
switch (*buf) {
|
||||||
// integral types
|
|
||||||
case NBT_I8: *(u8 *)out = *ptr; return ptr + 1;
|
case NBT_I8: *(u8 *)out = *ptr; return ptr + 1;
|
||||||
case NBT_I16: *(u16 *)out = be16toh(*(u16 *)ptr); return ptr + 2;
|
case NBT_I16: *(u16 *)out = be16toh(*(u16 *)ptr); return ptr + 2;
|
||||||
case NBT_I32: // fall through
|
case NBT_I32: // fall through
|
||||||
@@ -142,13 +149,12 @@ 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;
|
||||||
|
|
||||||
// arrays, handled differently
|
|
||||||
case NBT_STR: *(void **)out = nbt_procarr(ptr += 2, be16toh(*(u16 *)buf), 1); break;
|
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_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_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_ARR_I64: *(void **)out = nbt_procarr(ptr += 8, be32toh(*(u64 *)buf), 8); break;
|
||||||
|
case NBT_LIST: *(void **)out = nbt_proclist(ptr);
|
||||||
|
|
||||||
case NBT_LIST: // TODO: handle simple lists
|
|
||||||
default:
|
default:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,16 +50,23 @@ struct nbt_procdat {
|
|||||||
i16 dpt, mdpt;
|
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.
|
/* 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. */
|
* `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.
|
/* 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 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 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 `NBT_LIST`, if it is of the type `NBT_Ixx` and `NBT_Fxx`, then it's handled. Otherwise the funcion shall fail.
|
* - In the case of lists, the above is valid, as long as the list contains any of the following types:
|
||||||
* Upon failure, `NULL` is returned. */
|
* `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));
|
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 */
|
||||||
|
|||||||
Reference in New Issue
Block a user