Compare commits

...

13 Commits

16 changed files with 144 additions and 107 deletions

View File

@@ -87,7 +87,7 @@ BinPackArguments: true
DerivePointerAlignment: false
PointerAlignment: Right
ReferenceAlignment: Pointer
QualifierAlignment: Right
QualifierAlignment: Left
# ---------------------------
# include settings and sorting

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

@@ -1,103 +1,99 @@
#include "nbt.h"
#include <assert.h>
#include <stddef.h>
#include <string.h>
#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(u8 const *restrict buf) {
return be16toh(*(u16 *)(buf));
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;
}
/* returns the length of an array from a specific location in the buffer */
static inline i32 nbt_arrlen(u8 const *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(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);
}
/* acquires the array's byte size (excluding name + size spec)
* returns `0` upon failure */
static size_t nbt_arrbsize(u8 const *restrict buf) {
i32 mems = 0;
// 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, u16 slen) {
const u8 *ptr = buf + 3 + slen;
u8 dat[8];
size_t arrlen = 0;
switch (*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); // mems+1 multiplied by the array length
case NBT_STR: return nbt_strlen(buf);
case NBT_LIST:
mems = nbt_primsize(*buf);
if (mems > 0) return mems * nbt_arrlen(buf + 1);
return 0;
default: return 0;
}
}
/* returns the (expected) pointer of the tag following this one.
* `NBT_COMPOUND` and `NBT_END` tags are not valid for this function and should be handled separately.
* `NULL` is returned if anything went wrong. */
static u8 const *nbt_nexttag(u8 const *restrict buf) {
u8 const *nxt = buf + 1;
nxt += nbt_strlen(nxt) + 2;
i32 tmp;
switch (*buf) {
case NBT_I8: __attribute__((fallthrough));
case NBT_I16: __attribute__((fallthrough));
// integral types
case NBT_I8: *dat = *ptr; return ptr;
case NBT_I16: *(u16 *)dat = be16toh(*(u16 *)ptr); return ptr + 2;
case NBT_I32: __attribute__((fallthrough));
case NBT_F32: *(u32 *)dat = be16toh(*(u32 *)ptr); return ptr + 4;
case NBT_I64: __attribute__((fallthrough));
case NBT_F32: __attribute__((fallthrough));
case NBT_F64:
tmp = nbt_primsize(*buf);
return (tmp >= 0) ? (nxt + tmp) : 0;
case NBT_F64: *(u64 *)dat = be16toh(*(u64 *)ptr); return ptr + 8;
case NBT_LIST: nxt += 5; break;
case NBT_STR: nxt += 2; break;
// arrays, handled differently
case NBT_LIST: __attribute__((fallthrough));
case NBT_ARR_I8: __attribute__((fallthrough));
case NBT_STR: __attribute__((fallthrough));
case NBT_ARR_I32: __attribute__((fallthrough));
case NBT_ARR_I64: nxt += 4; break;
case NBT_ARR_I64:
// arrlen = nbt_arrbsize(ptr);
break;
default: return NULL; // failure on compound/end tags; these require more nuanced logic
default: return NULL;
}
tmp = nbt_arrbsize(buf);
return (tmp >= 0) ? (nxt + tmp) : NULL;
if (!arrlen) return NULL;
return ptr + nbt_primsize(*buf);
}
default: return NULL; // failure on compound/end tags; these require more nuanced logic
/* 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;
}
int nbt_proc(void **restrict datout, u8 const *restrict buf, size_t len) {
// 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;
// first byte should be a compound tag
if (*buf != NBT_COMPOUND) return 1;
uint ncomp = 1;
i16 dpt = 0;
i16 mdpt = 0;
// ignore the first tag + its name, so we start with the juicy data
uint tmp = nbt_strlen(buf + 1) + 3;
buf += tmp;
len -= tmp;
// acquire the maximum depth that we'll need to go (exclusive)
for (uint i = 0; i < npats; i++) {
int tmp = pats[i].len - mdpt;
mdpt += -(tmp > 0) & tmp;
}
assert(mdpt > 0);
// storing the segments of the current path
const char *cpat[mdpt - 1];
memset((void *)cpat, 0, 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 0;
return !dpt;
}
int nbt_primsize(u8 tag) {
@@ -112,6 +108,31 @@ int nbt_primsize(u8 tag) {
}
}
size_t nbt_tagdatlen(const u8 *restrict buf) {
i32 mems = 0;
switch (*buf) {
case NBT_I8: __attribute__((fallthrough));
case NBT_I16: __attribute__((fallthrough));
case NBT_I32: __attribute__((fallthrough));
case NBT_F32: __attribute__((fallthrough));
case NBT_I64: __attribute__((fallthrough));
case NBT_F64: __attribute__((fallthrough));
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 * (i32)be32toh(*(u32 *)(buf)) + 4;
case NBT_STR: return be16toh(*(u16 *)buf) + 2;
case NBT_LIST:
mems = nbt_primsize(*buf);
if (mems > 0) return mems * (i32)be32toh(*(u32 *)(buf + 1)) + 5;
return 0;
default: return 0;
}
}
int nbt_isprim(u8 tag) {
switch (tag) {
case NBT_I8: __attribute__((fallthrough));

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.
@@ -34,12 +36,30 @@ enum nbt_tagid {
NBT_ARR_I64 = 0x0C, // starts with a i32, denoting size, followed by the u32 data
};
int nbt_proc(void **restrict datout, u8 const *restrict buf, size_t len);
struct nbt_path {
const char **restrict pat; // specifies the NBT path components as separate elements
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);
/* gets the byte size of an NBT tag's data (excluding id and name), returns `0` upon error. */
atrb_pure size_t nbt_tagdatlen(const u8 *buf);
/* 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);

View File

@@ -17,5 +17,5 @@
#define fatal(s, ...) \
do { \
printf("\033[101m" __FILE__ ":" MACRO_STR2(__LINE__) ": [FAT]: " s "\033[0m\n", ##__VA_ARGS__); \
exit(1); \
exit(EXIT_FAILURE); \
} while (0)

View File

@@ -4,7 +4,7 @@
#include <GLFW/glfw3.h>
void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods) {
void key_callback(GLFWwindow *win, int key, int scancode, int action, int mods) {
(void)win, (void)key, (void)scancode, (void)action, (void)mods; // make the compiler shut up as this is fine
#ifndef NDEBUG
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)

View File

@@ -4,4 +4,4 @@
#include <GLFW/glfw3.h>
void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods);
void key_callback(GLFWwindow *win, int key, int scancode, int action, int mods);

View File

@@ -7,4 +7,4 @@
#include <GLFW/glfw3.h>
int render_init(void);
void render_update(GLFWwindow* win);
void render_update(GLFWwindow *win);

View File

@@ -12,7 +12,7 @@
#define NAM_E(name) _binary_res_##name##_end // name of an end variable
// macro for generating the variable declarations
#define DEF_GLSL(name) \
#define DEF_GLSL(name) \
extern char const NAM_S(name)[]; \
extern char const NAM_E(name)[]
@@ -24,7 +24,7 @@ DEF_GLSL(sh_geom_glsl);
// NOLINTEND
/* compile a shader */
static GLuint shader_compile(GLenum type, char const* src, size_t len) {
static GLuint shader_compile(GLenum type, const char *src, size_t len) {
int ilen = len;
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, &ilen);

View File

@@ -16,7 +16,7 @@
#define WIN_DEFAULT_WIDTH 640
#define WIN_DEFAULT_HEIGHT 480
static GLFWwindow* win = NULL;
static GLFWwindow *win = NULL;
int window_init(void) {
#ifndef NDEBUG

View File

@@ -17,7 +17,7 @@
#define WIN_DEFAULT_HEIGHT 480
// callback for GLFW errors
static void error_callback(int err, char const *const msg) {
static void error_callback(int err, const char *const msg) {
fprintf(stderr, "\033[91mE: glfw returned (%i); \"%s\"\033[0m\n", err, msg);
}

View File

@@ -14,11 +14,11 @@
#define atrb_format(...) __attribute__((format(__VA_ARGS__)))
#define atrb_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
#elif defined(_MSC_VER)
#define atrb __declspec
#define atrb(...) __declspec(__VA_ARGS__)
#define atrb_deprecated __declspec(deprecated)
#define atrb_noreturn __declspec(noreturn)
#else
#define atrb
#define atrb()
#define atrb_deprecated
#define atrb_unused
#define atrb_pure

View File

@@ -13,7 +13,7 @@
/* tests a files access with F_OK, X_OK, R_OK, W_OK OR'd together
returns 0 upon success. -1 when errno is set and anything else when one or more of the permissions isn't set */
static inline int faccess(char const *restrict fname, int perms);
static inline int faccess(const char *restrict fname, int perms);
// define the constants if they haven't been
#ifndef F_OK

View File

@@ -41,7 +41,7 @@ struct conf_fstr {
/* defines the structure of a config file entry */
struct conf_entry {
char const *key; // the key of this entry
const char *key; // the key of this entry
void *out; // the pointer to which the data is written value is read if the given option is incorrect or missing
uint8_t type; // the primitive type which we are querying for
};
@@ -51,15 +51,15 @@ struct conf_entry {
* `kout` and `vout` will contain a null-terminated string if the function returned successfully.
* returns `0` on success, `<0` when no data was found. `>0` when data was invalid but something went wrong.
* see `CONF_E*` or `enum conf_err` */
int conf_procbuf(char const *restrict buf, char *restrict kout, char *restrict vout, size_t len);
int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, size_t len);
/* matches the key with one of the options and returns the pointer. Returns NULL if none could be found. */
struct conf_entry const *conf_matchopt(struct conf_entry const *opts, size_t optc, char const *restrict key);
struct conf_entry const *conf_matchopt(struct conf_entry const *opts, size_t optc, const char *restrict key);
/* processes the value belonging to the key and outputs the result to opts.
* - `val` points to a null-terminated string which contains the key and value.
* returns `0` upon success, non-zero upon failure. For information about specific error codes, see `enum conf_err` */
int conf_procval(struct conf_entry const *opts, char const *restrict val);
int conf_procval(struct conf_entry const *opts, const char *restrict val);
/* acquires the config file path, appending str to the end (you need to handle path separators yourself)
* expecting str to be null-terminated
@@ -67,4 +67,4 @@ int conf_procval(struct conf_entry const *opts, char const *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 !! */
atrb_malloc atrb_nonnull(1) char *conf_getpat(char const *);
atrb_malloc atrb_nonnull(1) char *conf_getpat(const char *);

View File

@@ -19,7 +19,7 @@ struct test_getpat_envdat {
/* save the current environment variables */
static void env_save(struct test_getpat_envdat *s) {
char const *tmp;
const char *tmp;
tmp = getenv("XDG_CONFIG_HOME");
s->xdg_config_home = tmp ? strdup(tmp) : NULL;
@@ -64,9 +64,9 @@ static void env_restore(struct test_getpat_envdat *s) {
/* check procbuf's functionality */
struct test_procbuf {
char const *in; // data in
char const *xkey; // expected key
char const *xval; // expected value
const char *in; // data in
const char *xkey; // expected key
const char *xval; // expected value
int xret; // expected return type
};
int test_procbuf(void *arg) {
@@ -81,7 +81,7 @@ int test_procbuf(void *arg) {
/* check matchopt functionality */
struct test_matchopt {
char const *key; // key to search for (key1, key2, key3)
const char *key; // key to search for (key1, key2, key3)
int xidx; // expect index (<0 is NULL, may not be more than 2)
};
int test_matchopt(void *arg) {
@@ -96,7 +96,7 @@ int test_matchopt(void *arg) {
}
struct test_procval_int {
char const *val;
const char *val;
u64 xres;
u8 type;
};

View File

@@ -3,12 +3,12 @@
#pragma once
#include <stdio.h>
char const *test_ctest;
const char *test_ctest;
size_t test_runs = 0;
// evaluates the test
// returns 1 upon error
static inline int assert_helper(int cond, char const *restrict fname, unsigned ln, char const *restrict fnname, char const *restrict expr) {
static inline int assert_helper(int cond, const char *restrict fname, unsigned ln, const char *restrict fnname, const char *restrict expr) {
test_runs++;
if (cond)
printf("[\033[32;1m OK \033[0m] %s %s -> %s:%u (%s)\n", test_ctest, fnname, fname, ln, expr);
@@ -22,7 +22,7 @@ static inline int assert_helper(int cond, char const *restrict fname, unsigned l
// contains the data for executing a single test
struct testdat {
char const *name; // test name
const char *name; // test name
int (*test)(void *); // test, returns 0 upon success, non-zero upon failure
void *args; // arguments to the test
};