mirror of
https://github.com/thepigeongenerator/mcaselector-lite
synced 2026-02-07 23:28:09 +01:00
Rewrite endian.h to utilise the new endian-specific integer types, for better type safety.
This commit is contained in:
@@ -6,38 +6,36 @@
|
|||||||
#ifndef MCASELECTOR_LITE_ENDIAN_H
|
#ifndef MCASELECTOR_LITE_ENDIAN_H
|
||||||
#define MCASELECTOR_LITE_ENDIAN_H
|
#define MCASELECTOR_LITE_ENDIAN_H
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#include <mcaselector-lite/types.h>
|
||||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
||||||
#define le16toh(x) __uint16_identity(x)
|
|
||||||
#define le32toh(x) __uint32_identity(x)
|
|
||||||
#define le64toh(x) __uint64_identity(x)
|
|
||||||
#define htole16(x) __uint16_identity(x)
|
|
||||||
#define htole32(x) __uint32_identity(x)
|
|
||||||
#define htole64(x) __uint64_identity(x)
|
|
||||||
#define be16toh(x) __builtin_bswap16(x)
|
|
||||||
#define be32toh(x) __builtin_bswap32(x)
|
|
||||||
#define be64toh(x) __builtin_bswap64(x)
|
|
||||||
#define htobe16(x) __builtin_bswap16(x)
|
|
||||||
#define htobe32(x) __builtin_bswap32(x)
|
|
||||||
#define htobe64(x) __builtin_bswap64(x)
|
|
||||||
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
||||||
#define le16toh(x) __builtin_bswap16(x)
|
|
||||||
#define le32toh(x) __builtin_bswap32(x)
|
|
||||||
#define le64toh(x) __builtin_bswap64(x)
|
|
||||||
#define htole16(x) __builtin_bswap16(x)
|
|
||||||
#define htole32(x) __builtin_bswap32(x)
|
|
||||||
#define htole64(x) __builtin_bswap64(x)
|
|
||||||
#define be16toh(x) __uint16_identity(x)
|
|
||||||
#define be32toh(x) __uint32_identity(x)
|
|
||||||
#define be64toh(x) __uint64_identity(x)
|
|
||||||
#define htobe16(x) __uint16_identity(x)
|
|
||||||
#define htobe32(x) __uint32_identity(x)
|
|
||||||
#define htobe64(x) __uint64_identity(x)
|
|
||||||
#else
|
|
||||||
#error machine architecture unsupported! Expected either big-endian or little-endian, make sure to use a compiler which defines __BYTE_ORDER__ (like clang or gcc)
|
|
||||||
#endif /* byte order */
|
|
||||||
|
|
||||||
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||||
|
static inline u16 cvt_le16toh(le16 le) { return (u16)le; }
|
||||||
|
static inline u32 cvt_le32toh(le32 le) { return (u32)le; }
|
||||||
|
static inline u64 cvt_le64toh(le64 le) { return (u64)le; }
|
||||||
|
static inline le16 cvt_htole16(u16 u) { return (le16)u; }
|
||||||
|
static inline le32 cvt_htole32(u32 u) { return (le32)u; }
|
||||||
|
static inline le64 cvt_htole64(u64 u) { return (le64)u; }
|
||||||
|
static inline u16 cvt_be16toh(be16 be) { return __builtin_bswap16((u16)be); }
|
||||||
|
static inline u32 cvt_be32toh(be32 be) { return __builtin_bswap32((u32)be); }
|
||||||
|
static inline u64 cvt_be64toh(be64 be) { return __builtin_bswap64((u64)be); }
|
||||||
|
static inline be16 cvt_htobe16(u16 u) { return (be16)__builtin_bswap16(u); }
|
||||||
|
static inline be32 cvt_htobe32(u32 u) { return (be32)__builtin_bswap32(u); }
|
||||||
|
static inline be64 cvt_htobe64(u64 u) { return (be64)__builtin_bswap64(u); }
|
||||||
|
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||||
|
static inline u16 cvt_le16toh(le16 le) { return __builtin_bswap16((u16)le); }
|
||||||
|
static inline u32 cvt_le32toh(le32 le) { return __builtin_bswap32((u32)le); }
|
||||||
|
static inline u64 cvt_le64toh(le64 le) { return __builtin_bswap64((u64)le); }
|
||||||
|
static inline le16 cvt_htole16(u16 u) { return (le16)__builtin_bswap16(u); }
|
||||||
|
static inline le32 cvt_htole32(u32 u) { return (le32)__builtin_bswap32(u); }
|
||||||
|
static inline le64 cvt_htole64(u64 u) { return (le64)__builtin_bswap64(u); }
|
||||||
|
static inline u16 cvt_be16toh(be16 be) { return (u16)be; }
|
||||||
|
static inline u32 cvt_be32toh(be32 be) { return (u32)be; }
|
||||||
|
static inline u64 cvt_be64toh(be64 be) { return (u64)be; }
|
||||||
|
static inline be16 cvt_htobe16(u16 u) { return (be16)u; }
|
||||||
|
static inline be32 cvt_htobe32(u32 u) { return (be32)u; }
|
||||||
|
static inline be64 cvt_htobe64(u64 u) { return (be64)u; }
|
||||||
#else
|
#else
|
||||||
#error GNU C is unavailable
|
#error "Machine architecture unsupported! Expected either big-endian or little-endian."
|
||||||
#endif /* __GNUC__ */
|
#endif
|
||||||
|
|
||||||
#endif /* MCASELECTOR_LITE_ENDIAN_H */
|
#endif /* MCASELECTOR_LITE_ENDIAN_H */
|
||||||
|
|||||||
@@ -31,11 +31,11 @@ enum mcx_compression {
|
|||||||
/* first 4 bytes is an s32 indicating remaining bytes, the following byte defines the compression scheme */
|
/* 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)
|
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;
|
s32 len;
|
||||||
memcpy(&len, chunk, 4);
|
memcpy(&len, chunk, 4);
|
||||||
len = be32toh(len);
|
len = cvt_be32toh(len);
|
||||||
chunk += 4;
|
chunk += 4;
|
||||||
|
|
||||||
struct archive *archive = archive_read_new();
|
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
|
// count how many bytes we need to move, whilst updating location data
|
||||||
usize blen = 0;
|
usize blen = 0;
|
||||||
for (src_s++; src_s <= src_e; src_s++) {
|
for (src_s++; src_s <= src_e; src_s++) {
|
||||||
blen += (be32toh(table[src_s]) & 0xFF) * SECTOR;
|
blen += (cvt_be32toh(table[src_s]) & 0xFF) * SECTOR;
|
||||||
table[src_s] -= htobe32((len / SECTOR) << 8);
|
table[src_s] -= cvt_htobe32((len / SECTOR) << 8);
|
||||||
}
|
}
|
||||||
memmove(dst, src, blen);
|
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
|
// load the table data
|
||||||
usize slen, bidx, blen;
|
usize slen, bidx, blen;
|
||||||
slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk
|
slen = cvt_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
|
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
|
blen = slen * SECTOR; // compute the byte length of the chunk
|
||||||
|
|
||||||
// reset the table data
|
// reset the table data
|
||||||
table[sidx] = 0;
|
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
|
// move the succeeding chunks over the deleted chunk
|
||||||
u8 *dst = buf + bidx - rmb;
|
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);
|
assert(start < end && end < CHUNKS);
|
||||||
u32 table[TABLE];
|
u32 table[TABLE];
|
||||||
memcpy(table, buf, sizeof(table));
|
memcpy(table, buf, sizeof(table));
|
||||||
u8 *dst = buf + (be32toh(table[start]) >> 8) * SECTOR;
|
u8 *dst = buf + (cvt_be32toh(table[start]) >> 8) * SECTOR;
|
||||||
u8 *src = buf + (be32toh(table[end]) >> 8) * SECTOR;
|
u8 *src = buf + (cvt_be32toh(table[end]) >> 8) * SECTOR;
|
||||||
src += (be32toh(table[end]) & 0xFF) * SECTOR;
|
src += (cvt_be32toh(table[end]) & 0xFF) * SECTOR;
|
||||||
|
|
||||||
// zeroes-out the chunk data within this range. (and set the timestamp)
|
// 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++) {
|
for (int i = start; i <= end; i++) {
|
||||||
table[i] = 0;
|
table[i] = 0;
|
||||||
table[i + CHUNKS] = ts;
|
table[i + CHUNKS] = ts;
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ static inline u16 buftoh16(const void *restrict buf)
|
|||||||
{
|
{
|
||||||
u16 i;
|
u16 i;
|
||||||
memcpy(&i, buf, sizeof(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. */
|
/* 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;
|
u32 i;
|
||||||
memcpy(&i, buf, sizeof(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. */
|
/* 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;
|
u64 i;
|
||||||
memcpy(&i, buf, sizeof(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`.
|
/* 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;
|
s32 i = 0;
|
||||||
while (i < nmemb) {
|
while (i < nmemb) {
|
||||||
switch (size) {
|
switch (size) {
|
||||||
case 2: ((u16 *)out->dat)[i] = be16toh(((u16 *)out->dat)[i]); break;
|
case 2: ((u16 *)out->dat)[i] = cvt_be16toh(((u16 *)out->dat)[i]); break;
|
||||||
case 4: ((u32 *)out->dat)[i] = be16toh(((u32 *)out->dat)[i]); break;
|
case 4: ((u32 *)out->dat)[i] = cvt_be16toh(((u32 *)out->dat)[i]); break;
|
||||||
case 8: ((u64 *)out->dat)[i] = be16toh(((u64 *)out->dat)[i]); break;
|
case 8: ((u64 *)out->dat)[i] = cvt_be16toh(((u64 *)out->dat)[i]); break;
|
||||||
default: __builtin_unreachable(); // this should be impossible
|
default: __builtin_unreachable(); // this should be impossible
|
||||||
}
|
}
|
||||||
i += size;
|
i += size;
|
||||||
@@ -91,7 +91,7 @@ static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out
|
|||||||
buf++;
|
buf++;
|
||||||
s32 len;
|
s32 len;
|
||||||
memcpy(&len, buf, 4);
|
memcpy(&len, buf, 4);
|
||||||
len = be32toh(len);
|
len = cvt_be32toh(len);
|
||||||
buf += 4;
|
buf += 4;
|
||||||
return procarr(buf, len, size, out);
|
return procarr(buf, len, size, out);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user