Rewrite endian.h to utilise the new endian-specific integer types, for better type safety.

This commit is contained in:
2026-01-22 12:21:49 +01:00
parent 44b3af2b46
commit 51dc8f85a6
3 changed files with 48 additions and 50 deletions

View File

@@ -31,11 +31,11 @@ enum mcx_compression {
/* first 4 bytes is an s32 indicating remaining bytes, the following byte defines the compression scheme */
static int mcx_loadchunk(const u8 *restrict buf, const s32 *restrict table, int idx)
{
const u8 *chunk = buf + (be32toh(table[idx]) >> 8) * SECTOR;
const u8 *chunk = buf + (cvt_be32toh(table[idx]) >> 8) * SECTOR;
s32 len;
memcpy(&len, chunk, 4);
len = be32toh(len);
len = cvt_be32toh(len);
chunk += 4;
struct archive *archive = archive_read_new();
@@ -86,8 +86,8 @@ static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e
// count how many bytes we need to move, whilst updating location data
usize blen = 0;
for (src_s++; src_s <= src_e; src_s++) {
blen += (be32toh(table[src_s]) & 0xFF) * SECTOR;
table[src_s] -= htobe32((len / SECTOR) << 8);
blen += (cvt_be32toh(table[src_s]) & 0xFF) * SECTOR;
table[src_s] -= cvt_htobe32((len / SECTOR) << 8);
}
memmove(dst, src, blen);
}
@@ -99,13 +99,13 @@ static usize delchunk(u8 *restrict buf, u32 *restrict table, usize rmb, int sidx
{
// load the table data
usize slen, bidx, blen;
slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk
bidx = (be32toh(table[sidx]) >> 8) * SECTOR; // acquire and compute the byte offset the chunk starts at
slen = cvt_be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk
bidx = (cvt_be32toh(table[sidx]) >> 8) * SECTOR; // acquire and compute the byte offset the chunk starts at
blen = slen * SECTOR; // compute the byte length of the chunk
// reset the table data
table[sidx] = 0;
table[sidx + CHUNKS] = htobe32(time(NULL)); // assign the current time to the timestamp, for correctness NOTE: might need to zero-out instead
table[sidx + CHUNKS] = cvt_htobe32(time(NULL)); // assign the current time to the timestamp, for correctness NOTE: might need to zero-out instead
// move the succeeding chunks over the deleted chunk
u8 *dst = buf + bidx - rmb;
@@ -131,12 +131,12 @@ usize mcx_delchunk_range(u8 *restrict buf, int start, int end)
assert(start < end && end < CHUNKS);
u32 table[TABLE];
memcpy(table, buf, sizeof(table));
u8 *dst = buf + (be32toh(table[start]) >> 8) * SECTOR;
u8 *src = buf + (be32toh(table[end]) >> 8) * SECTOR;
src += (be32toh(table[end]) & 0xFF) * SECTOR;
u8 *dst = buf + (cvt_be32toh(table[start]) >> 8) * SECTOR;
u8 *src = buf + (cvt_be32toh(table[end]) >> 8) * SECTOR;
src += (cvt_be32toh(table[end]) & 0xFF) * SECTOR;
// zeroes-out the chunk data within this range. (and set the timestamp)
u32 ts = htobe32(time(NULL));
u32 ts = cvt_htobe32(time(NULL));
for (int i = start; i <= end; i++) {
table[i] = 0;
table[i + CHUNKS] = ts;

View File

@@ -19,7 +19,7 @@ static inline u16 buftoh16(const void *restrict buf)
{
u16 i;
memcpy(&i, buf, sizeof(i));
return be16toh(i);
return cvt_be16toh(i);
}
/* Extracts a big endian 32 bit integer from address `buf`, converts it to host byte size if needed and returns. */
@@ -27,7 +27,7 @@ static inline u32 buftoh32(const void *restrict buf)
{
u32 i;
memcpy(&i, buf, sizeof(i));
return be32toh(i);
return cvt_be32toh(i);
}
/* Extracts a big endian 64 bit integer from address `buf`, converts it to host byte size if needed and returns. */
@@ -35,7 +35,7 @@ static inline u64 buftoh64(const void *restrict buf)
{
u64 i;
memcpy(&i, buf, sizeof(i));
return be64toh(i);
return cvt_be64toh(i);
}
/* Processes the incoming array data in `buf`. Which contains `nmem` items of `size`.
@@ -62,9 +62,9 @@ static const u8 *procarr(const u8 *restrict buf, s32 nmemb, uint size, struct nb
s32 i = 0;
while (i < nmemb) {
switch (size) {
case 2: ((u16 *)out->dat)[i] = be16toh(((u16 *)out->dat)[i]); break;
case 4: ((u32 *)out->dat)[i] = be16toh(((u32 *)out->dat)[i]); break;
case 8: ((u64 *)out->dat)[i] = be16toh(((u64 *)out->dat)[i]); break;
case 2: ((u16 *)out->dat)[i] = cvt_be16toh(((u16 *)out->dat)[i]); break;
case 4: ((u32 *)out->dat)[i] = cvt_be16toh(((u32 *)out->dat)[i]); break;
case 8: ((u64 *)out->dat)[i] = cvt_be16toh(((u64 *)out->dat)[i]); break;
default: __builtin_unreachable(); // this should be impossible
}
i += size;
@@ -91,7 +91,7 @@ static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out
buf++;
s32 len;
memcpy(&len, buf, 4);
len = be32toh(len);
len = cvt_be32toh(len);
buf += 4;
return procarr(buf, len, size, out);
}