From db76d6992bc9ff8957b61b5b9aa2b6c75b626798 Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 15 Jul 2025 13:01:34 +0200 Subject: [PATCH] add function for comparing an NBT string with a C string --- src/dat/nbt.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/dat/nbt.c b/src/dat/nbt.c index 4021b37..6da6055 100644 --- a/src/dat/nbt.c +++ b/src/dat/nbt.c @@ -2,6 +2,7 @@ #include #include +#include #include "../util/types.h" @@ -10,6 +11,19 @@ static inline u16 nbt_strlen(u8 const *restrict buf) { return be16toh(*(u16 *)(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(char const *restrict matstr, u8 const *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); +} + int nbt_proc(void **restrict datout, u8 const *restrict buf, size_t len) { // first byte should be a compound tag