mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 20:35:45 +01:00
Compare commits
8 Commits
425b703b8d
...
0e00cb2d54
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e00cb2d54 | |||
| c3fc688c35 | |||
| 4871a19695 | |||
| 2d33255ce8 | |||
| 066e1c0049 | |||
| 1052dcaac9 | |||
| f952268152 | |||
| 4b2404e903 |
@@ -110,22 +110,7 @@ static const u8 *nbt_proctag(const u8 *restrict buf, u16 slen) {
|
||||
return ptr + nbt_primsize(*buf);
|
||||
}
|
||||
|
||||
/* finds which of `pats` is equivalent to `cmp`, assumes `cmp` is `≥len` bytes long */
|
||||
static const char *getpat(struct nbt_path const *restrict pats, uint npats, i16 dpt, const char *restrict cmp, u16 len) {
|
||||
for (uint i = 0; i < npats; i++) {
|
||||
if (strncmp(pats[i].pat[dpt], cmp, len) == 0)
|
||||
return pats[i].pat[dpt];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// TODO: make the user do the looping
|
||||
int nbt_proc(struct nbt_path const *restrict pats, uint npats, const u8 *restrict buf, size_t len) {
|
||||
// ensure first and last tag(s) are valid
|
||||
if (buf[0] != NBT_COMPOUND || buf[len - 1] != NBT_END)
|
||||
return 1;
|
||||
|
||||
i16 dpt = 0;
|
||||
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)
|
||||
@@ -136,28 +121,14 @@ int nbt_proc(struct nbt_path const *restrict pats, uint npats, const u8 *restric
|
||||
assert(mdpt > 0);
|
||||
|
||||
// storing the segments of the current path
|
||||
const char *cpat[mdpt - 1];
|
||||
memset((void *)cpat, 0, mdpt - 1);
|
||||
const char **cpat = (const char **)calloc(sizeof(void *), mdpt - 1);
|
||||
|
||||
// looping through the different tags
|
||||
const u8 *ptr = buf + nbt_namelen(buf) + 3;
|
||||
while (ptr < (buf + len) && dpt >= 0) {
|
||||
u16 naml = nbt_namelen(ptr);
|
||||
const char *mat = getpat(pats, npats, dpt, (char *)(ptr + 3), naml);
|
||||
cpat[dpt] = mat;
|
||||
|
||||
if (mat) {
|
||||
switch (*ptr) {
|
||||
case NBT_END: dpt--; break;
|
||||
case NBT_COMPOUND: dpt++; break;
|
||||
default: ptr = nbt_proctag(ptr, naml); break;
|
||||
}
|
||||
} else {
|
||||
ptr = nbt_nexttag(ptr, naml);
|
||||
if (!ptr) return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: finish function
|
||||
return !dpt;
|
||||
// return the initialised structure.
|
||||
return (struct nbt_procdat){
|
||||
pats,
|
||||
cpat,
|
||||
npats,
|
||||
0,
|
||||
mdpt,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -36,11 +36,20 @@ 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;
|
||||
};
|
||||
|
||||
/* searches for the end of a compound tag without processing data, the final pointer is returned.
|
||||
* `NULL` is returned upon failure, the otherwise returned pointer is not guaranteed to be valid.
|
||||
* `cdat` is assumed to be the start of the **compound tag's data**. */
|
||||
@@ -55,18 +64,5 @@ PURE NONNULL((1)) const u8 *nbt_nextlist(const u8 *restrict ldat);
|
||||
* `NULL` is returned upon failure, the otherwise returned pointer is not guaranteed to be valid. */
|
||||
PURE NONNULL((1)) const u8 *nbt_nexttag(const u8 *restrict buf, u16 naml);
|
||||
|
||||
/* returns the name length of a specific tag. `buf` is the pointer to start of the tag */
|
||||
atrb_pure atrb_nonnull(1) static inline u16 nbt_namelen(const u8 *restrict buf) {
|
||||
assert(*buf != NBT_END);
|
||||
return be16toh(*(u16 *)(buf + 1));
|
||||
}
|
||||
|
||||
/* returns the (expected) pointer of the tag following this one.
|
||||
* `NULL` is returned if anything went wrong. */
|
||||
atrb_pure atrb_nonnull(1) const u8 *nbt_nexttag(const u8 *restrict buf, u16 naml);
|
||||
|
||||
/* gets the tag size of primitive types, returns `>0` on success, `<0` on failure */
|
||||
atrb_const int nbt_primsize(u8 tag);
|
||||
|
||||
/* processes the uncompressed `NBT` data in `buf`, with a size of `len`. */
|
||||
atrb_nonnull(1, 3) int nbt_proc(struct nbt_path const *restrict paths, uint npaths, const u8 *restrict buf, size_t len);
|
||||
/* initialises a data structure used whilst processing the tags */
|
||||
PURE NONNULL((1)) struct nbt_procdat nbt_initproc(struct nbt_path const *restrict pats, uint npats);
|
||||
|
||||
Reference in New Issue
Block a user