Compare commits

..

6 Commits

Author SHA1 Message Date
29c8a2b6ea improve documentation for the nbt_proctag function 2025-08-23 23:19:16 +02:00
8cd29225d1 remove outdated TODO comments 2025-08-23 23:13:27 +02:00
20ec45f230 fix: not actually returning NULL. 2025-08-23 23:13:27 +02:00
3cdee8b40c add nbt_proctag to nbt.h
also changed the `out` parameter from `u8` to `void`, to be more
acurrate that it may point to whatever.
2025-08-23 23:13:27 +02:00
89ceb5263c put attributes after function declarations
super confusing, but the formatter is being annoying otherwise
2025-08-23 23:09:09 +02:00
4fa0a84c94 fix attributes with parameters to be less messy due to vardiac arguments 2025-08-23 23:01:01 +02:00
5 changed files with 20 additions and 13 deletions

View File

@@ -100,7 +100,7 @@ const u8 *nbt_nexttag(const u8 *restrict buf) {
MALLOC static void *nbt_procarr(const u8 *restrict buf, i32 nmem, uint size) {
u8 *ptr = malloc(nmem * size);
if (!ptr) NULL;
if (!ptr) return NULL;
memcpy(ptr, buf, nmem * size);
/* Only include this code for little-endian systems. Since only they require this logic.
@@ -129,14 +129,13 @@ MALLOC static void *nbt_procarr(const u8 *restrict buf, i32 nmem, uint size) {
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, u8 *restrict out, u16 slen) {
const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
const u8 *ptr = buf + 3 + slen;
switch (*buf) {
// integral types
case NBT_I8: *out = *ptr; return ptr + 1;
case NBT_I8: *(u8 *)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 *)out = be16toh(*(u32 *)ptr); return ptr + 4;
@@ -154,7 +153,7 @@ static const u8 *nbt_proctag(const u8 *restrict buf, u8 *restrict out, u16 slen)
return NULL;
}
return ptr; // TODO: return end of array
return ptr;
}
struct nbt_procdat nbt_initproc(struct nbt_path const *restrict pats, uint npats) {

View File

@@ -52,7 +52,15 @@ struct nbt_procdat {
/* searches for the end of a named tag without processing data, the final pointer is returned.
* `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);
const u8 *nbt_nexttag(const u8 *restrict buf) NONNULL((1)) PURE;
/* Processes the tag entered in `buf`, `buf` is assumed to be the start of a named tag. Where `slen` shall be the string length.
* The data in `buf` is processed and outputted to `out`. A pointer to the next tag is returned.
* - In the case for all basic types, `out` will require to be the width of said type.
* - In the case of arrays, a pointer shall be returned pointing to the data. TODO: it'd be nice to know how large this array is.
* - In the case of `NBT_LIST`, if it is of the type `NBT_Ixx` and `NBT_Fxx`, then it's handled. Otherwise the funcion shall fail.
* Upon failure, `NULL` is returned. */
const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) NONNULL((1, 3));
/* 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);
struct nbt_procdat nbt_initproc(struct nbt_path const *restrict pats, uint npats) NONNULL((1)) PURE;

View File

@@ -46,14 +46,14 @@
#endif
#if __has_attribute(__format__)
#define FORMAT(...) __attribute__((format(__VA_ARGS__)))
#define FORMAT(args) __attribute__((format args))
#else
#define FORMAT(...)
#define FORMAT(args)
#endif
#if __has_attribute(__nonnull__)
#define NONNULL(...) __attribute__((nonnull(__VA_ARGS__)))
#define NONNULL(args) __attribute__((__nonnull__ args))
#else
#define NONNULL(...)
#define NONNULL(args)
#endif
#endif

View File

@@ -127,7 +127,7 @@ int conf_procval(struct conf_entry const *opt, const char *restrict val) {
}
/* utility function for conf_getpat to concatenate 3 strings, where we already know the size */
NONNULL(1, 3)
NONNULL((1, 3))
static char *conf_getpat_concat(const char *restrict s1, const char *restrict s2, const char *restrict s3, size_t s1len, size_t s2len, size_t s3len) {
assert(s2 || (!s2 && !s2len)); // ensuring the programmer passes both s2 and s2len as 0, if they intend to
char *buf, *ptr;

View File

@@ -67,4 +67,4 @@ int conf_procval(struct conf_entry const *opts, const char *restrict val);
* - windows: reads %APPDATA%, if empty %USERPROFILE%\AppData\Roaming is used, if both are empty NULL is returned.
* - osx: reads $HOME, uses $HOME/Library/Application Support, if $HOME is empty NULL is returned.
* !! A malloc'd null-terminated string is returned !! */
MALLOC NONNULL((1)) char *conf_getpat(const char *);
char *conf_getpat(const char *) MALLOC NONNULL((1));