add function for comparing an NBT string with a C string

This commit is contained in:
2025-07-15 13:01:34 +02:00
parent 8345ac1164
commit db76d6992b

View File

@@ -2,6 +2,7 @@
#include <endian.h>
#include <stddef.h>
#include <string.h>
#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