Compare commits

...

5 Commits

Author SHA1 Message Date
2cafddc2b3 fix: there is no need to let the compilation continue if it fails.
it actually makes it less easy to locate errors.
2025-07-24 16:06:56 +02:00
96817cd061 fix: include my endian.h in nbt.h 2025-07-24 16:05:18 +02:00
d561588490 remove unused function nbt_cmpstr, since we're performing this check directly now. 2025-07-24 16:05:18 +02:00
3078a9fb5c remove nbt_arrlen function 2025-07-24 16:05:18 +02:00
16f74a91c1 move nbt_strlen to nbt.h, and rename to nbt_namelen 2025-07-24 16:05:18 +02:00
3 changed files with 22 additions and 39 deletions

View File

@@ -43,11 +43,7 @@ jobs:
git clone https://github.com/microsoft/vcpkg.git "$VCPKG_ROOT"
"$VCPKG_ROOT/bootstrap-vcpkg.sh"
"$VCPKG_ROOT/vcpkg" install $DEPS_VCPKG
# compilation (using bulk compilation)
- run: make all CALL=compile -j || echo "JOB_FAILED=1" >>"$GITHUB_ENV"
# executing unit tests (using bulk flags)
- run: make all CALL=run DEBUG=test -j || echo "JOB_FAILED=1" >>"$GITHUB_ENV"
# exit if any errors occurred
- name: exit on errors
run: |
[ "$JOB_FAILED" != "1" ]
- name: compilation (using bulk compilation)
run: make all CALL=compile -j
- name: unit tests (using bulk flags)
run: make all CALL=run DEBUG=test -j

View File

@@ -7,32 +7,7 @@
#include "../util/compat/endian.h"
#include "../util/types.h"
/* returns the string length from a specific location in the buffer */
static inline u16 nbt_strlen(const u8 *restrict buf) {
return be16toh(*(u16 *)(buf));
}
/* returns the length of an array from a specific location in the buffer */
static inline i32 nbt_arrlen(const u8 *restrict buf) {
return be32toh(*(i32 *)(buf));
}
/* compares the string in `buf` to `matstr`.
* returns `=0` if equal, `>0` if buf is greater, `<0` if matstr is greater. */
static int nbt_cmpstr(const char *restrict matstr, const u8 *restrict buf) {
u16 len = nbt_strlen(buf);
// allocate and copy bytes
char str[len + 1];
memcpy(str, buf + 2, len);
str[len] = '\0';
return strncmp(str, matstr, len);
}
/* returns the (expected) pointer of the tag following this one.
* `NULL` is returned if anything went wrong. */
static const u8 *nbt_nexttag(const u8 *restrict buf, u16 naml) {
const u8 *nbt_nexttag(const u8 *restrict buf, u16 naml) {
size_t len = nbt_tagdatlen(buf);
if (!len) return NULL; // TODO: compound tags should be handled here
return buf + naml + len + 3;
@@ -99,9 +74,9 @@ int nbt_proc(struct nbt_path const *restrict pats, uint npats, const u8 *restric
memset((void *)cpat, 0, mdpt - 1);
// looping through the different tags
const u8 *ptr = buf + nbt_strlen(buf + 1) + 3;
const u8 *ptr = buf + nbt_namelen(buf) + 3;
while (ptr < (buf + len) && dpt >= 0) {
u16 naml = nbt_strlen(ptr + 1);
u16 naml = nbt_namelen(ptr);
const char *mat = getpat(pats, npats, dpt, (char *)(ptr + 3), naml);
cpat[dpt] = mat;
@@ -146,13 +121,13 @@ size_t nbt_tagdatlen(const u8 *restrict buf) {
case NBT_ARR_I64: mems += sizeof(i64) - sizeof(i32); __attribute__((fallthrough));
case NBT_ARR_I32: mems += sizeof(i32) - sizeof(i8); __attribute__((fallthrough));
case NBT_ARR_I8: return +mems * nbt_arrlen(buf) + 4;
case NBT_ARR_I8: return +mems * (i32)be32toh(*(u32 *)(buf)) + 4;
case NBT_STR: return nbt_strlen(buf) + 2;
case NBT_STR: return be16toh(*(u16 *)buf) + 2;
case NBT_LIST:
mems = nbt_primsize(*buf);
if (mems > 0) return mems * nbt_arrlen(buf + 1) + 5;
if (mems > 0) return mems * (i32)be32toh(*(u32 *)(buf + 1)) + 5;
return 0;
default: return 0;
}

View File

@@ -2,10 +2,12 @@
// Licensed under the MIT Licence. See LICENSE for details
#pragma once
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include "../util/atrb.h"
#include "../util/compat/endian.h"
#include "../util/types.h"
/* NBT (named binary tag) is a tree data structure. Tags have a numeric type ID, name and a payload.
@@ -39,6 +41,16 @@ struct nbt_path {
i16 len; // specifies the length of the NBT elements
};
/* 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);
/* checks whether the tag is a primitive data tag. (not recommended for filtering tags, use a `switch`)
* returns a boolean value. */
atrb_const int nbt_isprim(u8 tag);