mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-17 03:45:46 +01:00
continue working on nbt_proctag focussing on array processing.
This commit is contained in:
@@ -98,34 +98,62 @@ const u8 *nbt_nexttag(const u8 *restrict buf) {
|
||||
return tag;
|
||||
}
|
||||
|
||||
MALLOC static void *nbt_procarr(const u8 *restrict buf, i32 nmem, uint size) {
|
||||
u8 *ptr = malloc(nmem * size);
|
||||
if (!ptr) NULL;
|
||||
memcpy(ptr, buf, nmem * size);
|
||||
|
||||
/* 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) {
|
||||
switch (size) {
|
||||
case 2:
|
||||
*(u16 *)(ptr + i) = be16toh(*(u16 *)(ptr + i));
|
||||
i += 2;
|
||||
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
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return ptr;
|
||||
}
|
||||
|
||||
// TODO: not actually doing anything
|
||||
/* readies the output data for export, returns the new buffer position, or `NULL` upon an error (may be out of bounds) */
|
||||
static const u8 *nbt_proctag(const u8 *restrict buf, u16 slen) {
|
||||
static const u8 *nbt_proctag(const u8 *restrict buf, u8 *restrict out, u16 slen) {
|
||||
const u8 *ptr = buf + 3 + slen;
|
||||
u8 dat[8];
|
||||
size_t arrlen = 0;
|
||||
|
||||
switch (*buf) {
|
||||
// integral types
|
||||
case NBT_I8: *dat = *ptr; return ptr + 1;
|
||||
case NBT_I16: *(u16 *)dat = be16toh(*(u16 *)ptr); return ptr + 2;
|
||||
case NBT_I8: *out = *ptr; return ptr + 1;
|
||||
case NBT_I16: *(u16 *)out = be16toh(*(u16 *)ptr); return ptr + 2;
|
||||
case NBT_I32: // fall through
|
||||
case NBT_F32: *(u32 *)dat = be16toh(*(u32 *)ptr); return ptr + 4;
|
||||
case NBT_F32: *(u32 *)out = be16toh(*(u32 *)ptr); return ptr + 4;
|
||||
case NBT_I64: // fall through
|
||||
case NBT_F64: *(u64 *)dat = be16toh(*(u64 *)ptr); return ptr + 8;
|
||||
case NBT_F64: *(u64 *)out = be16toh(*(u64 *)ptr); return ptr + 8;
|
||||
|
||||
// arrays, handled differently
|
||||
case NBT_LIST:
|
||||
case NBT_ARR_I8:
|
||||
case NBT_STR:
|
||||
case NBT_ARR_I32:
|
||||
case NBT_ARR_I64:
|
||||
// TODO: arrlen = nbt_arrbsize(ptr);
|
||||
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_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;
|
||||
|
||||
default: return NULL;
|
||||
case NBT_LIST: // TODO: handle simple lists
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
if (!arrlen) return NULL;
|
||||
|
||||
return ptr; // TODO: return end of array
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user