refactor code with new formatting rules

Functions now break before their brace, mirroring the Linux kernel.
The reason for this is that breaking the parameter list otherwise makes
code unreadable.
This commit is contained in:
2025-10-09 18:43:09 +02:00
parent eb45650178
commit 00719b1933
14 changed files with 137 additions and 73 deletions

View File

@@ -26,7 +26,8 @@ enum mcx_compression {
};
/* first 4 bytes is an i32 indicating remaining bytes, the following byte defines the compression scheme */
static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int idx) {
static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int idx)
{
const u8 *chunk = buf + (be32toh(table[idx]) >> 8) * SECTOR;
i32 len;
@@ -64,7 +65,8 @@ static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int
error("failed to decompress %i bytes of compression type %i", len, *chunk);
return 1;
}
if (size == 0) break;
if (size == 0)
break;
// TODO: handle data
}
@@ -72,7 +74,8 @@ static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int
}
/* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */
static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e) {
static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e)
{
assert(src > dst);
uintptr len = src - dst; // acquire the amount of bytes that we shall move
assert(!(len % SECTOR));
@@ -89,7 +92,8 @@ static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e
/* Deletes chunk `sidx` by moving chunks up to `eidx` back over `sidx` in `buf`.
* `rmb` is an optional additional offset that can be applied, and signifies bytes already removed.
* Returns the bytes removed by this function. */
static usize delchunk(u8 *restrict buf, u32 *restrict table, usize rmb, int sidx, int eidx) {
static usize delchunk(u8 *restrict buf, u32 *restrict table, usize rmb, int sidx, int eidx)
{
// load the table data
usize slen, bidx, blen;
slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk
@@ -110,7 +114,8 @@ static usize delchunk(u8 *restrict buf, u32 *restrict table, usize rmb, int sidx
/* Call `delchunk` with the parameters and some defaults. Ensuring the table is copied correctly as well.
* This is done instead of `delchunk` being globally linked, because
* `delchunk` requests more specific parameters, which is confusing outside this module. */
usize mcx_delchunk(u8 *restrict buf, int chunk) {
usize mcx_delchunk(u8 *restrict buf, int chunk)
{
u32 table[TABLE];
memcpy(table, buf, sizeof(table));
usize res = delchunk(buf, table, 0, chunk, CHUNKS);
@@ -118,7 +123,8 @@ usize mcx_delchunk(u8 *restrict buf, int chunk) {
return res;
}
usize mcx_delchunk_range(u8 *restrict buf, int start, int end) {
usize mcx_delchunk_range(u8 *restrict buf, int start, int end)
{
assert(start < end && end < CHUNKS);
u32 table[TABLE];
memcpy(table, buf, sizeof(table));
@@ -141,7 +147,8 @@ usize mcx_delchunk_range(u8 *restrict buf, int start, int end) {
}
/* comparer function for to be inputted into `qsort` to compare two */
static int cmp_chunkids(const void *restrict x, const void *restrict y) {
static int cmp_chunkids(const void *restrict x, const void *restrict y)
{
u16 x2 = *(u16 *)x;
u16 y2 = *(u16 *)y;
return (x2 > y2) - (x2 < y2);
@@ -149,7 +156,8 @@ static int cmp_chunkids(const void *restrict x, const void *restrict y) {
/* Sorts the chunks marked for deletion from smallest to greatest index.
* Then performs the deletion in this order. Making sure to only update the chunks up to the next. */
usize mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) {
usize mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc)
{
// ensure the chunks ids we're working on are sorted from least to greatest
u16 chunkids[chunkc + 1];
memcpy(chunkids, chunks, chunkc);
@@ -169,7 +177,8 @@ usize mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc
/* Sum together the 4th byte in each location integer to compute the sector size of all chunks.
* Multiplying by `SECTOR`, and adding the size of the table itself. */
usize mcx_calcsize(const u8 *restrict buf) {
usize mcx_calcsize(const u8 *restrict buf)
{
usize size = 0;
for (uint i = 0; i < CHUNKS; i++)
size += *(buf + (i * 4) + 3);

View File

@@ -13,21 +13,24 @@
#define MAX_DEPTH 512
/* Extracts a big endian 16 bit integer from address `buf`, converts it to host byte size if needed and returns. */
static inline u16 buftoh16(const void *restrict buf) {
static inline u16 buftoh16(const void *restrict buf)
{
u16 i;
memcpy(&i, buf, sizeof(i));
return be16toh(i);
}
/* Extracts a big endian 32 bit integer from address `buf`, converts it to host byte size if needed and returns. */
static inline u32 buftoh32(const void *restrict buf) {
static inline u32 buftoh32(const void *restrict buf)
{
u32 i;
memcpy(&i, buf, sizeof(i));
return be32toh(i);
}
/* Extracts a big endian 64 bit integer from address `buf`, converts it to host byte size if needed and returns. */
static inline u64 buftoh64(const void *restrict buf) {
static inline u64 buftoh64(const void *restrict buf)
{
u64 i;
memcpy(&i, buf, sizeof(i));
return be64toh(i);
@@ -36,7 +39,8 @@ static inline u64 buftoh64(const void *restrict buf) {
/* Processes the incoming array data in `buf`. Which contains `nmem` items of `size`.
* The data shall be converted to little-endian on little-endian systems
* Outputs the allocated data to `out`, returns where the next pointer would be. */
static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nbt_array *restrict out) {
static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nbt_array *restrict out)
{
usize len = nmemb * size;
*out = (struct nbt_array){
out->nmemb = nmemb,
@@ -51,7 +55,8 @@ static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nb
/* Only include this code for little-endian systems. Since only they require this logic.
* Producing optimised code for other platforms. */
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
if (size == 1) return buf;
if (size == 1)
return buf;
i32 i = 0;
while (i < nmemb) {
switch (size) {
@@ -67,7 +72,8 @@ static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nb
}
/* calls `procarr` for the simple types available. */
static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out) {
static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out)
{
uint size;
switch (*(u8 *)buf) {
@@ -88,7 +94,8 @@ static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out
return procarr(buf, len, size, out);
}
const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out)
{
const u8 *ptr, *tmp;
ptr = buf + 3 + slen;
@@ -123,7 +130,8 @@ const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
* `ptr` is assumed to be the start of the `NBT_LIST` data, e.i. The list's ID, followed by the list's length.
* If `ID` is `NBT_I8`, `NBT_I16`, `NBT_I32`, `NBT_I64`, `NBT_F32`, or `NBT_F64`, the entire list length is computed and returned.
* For other types this won't be possible, and thus will add `1` to `dpt`, and write the list data to `lens` and `tags` at this new `dpt`. */
static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) {
static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags)
{
const u8 *tag = ptr;
ptr++;
switch (*tag) {
@@ -151,7 +159,8 @@ static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt,
* - `lens` shall contain `MAX_DEPTH` of items representing the list length, if the current item is non-zero we shall assume we're in a list.
* Where the value is decremented until we reach `0`.
* - `tags` shall contain `MAX_DEPTH` of items representing the list's stored type. */
static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) {
static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags)
{
u8 type;
const u8 *ptr = tag;
if (lens[*dpt]) {
@@ -193,7 +202,8 @@ static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *
* - compound:list:int32
* - string
*/
const u8 *nbt_nexttag(const u8 *restrict buf) {
const u8 *nbt_nexttag(const u8 *restrict buf)
{
const u8 *tag;
u8 tags[MAX_DEPTH] = {0};
i32 lens[MAX_DEPTH] = {0};