From 9a45294e568b86622a09cd0cea64e2ed695250c3 Mon Sep 17 00:00:00 2001 From: Quinn Date: Fri, 29 Aug 2025 09:35:02 +0200 Subject: [PATCH] write some constants for `mcx.c`, so things are less error-prone. --- src/dat/mcx.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/dat/mcx.c b/src/dat/mcx.c index 039e40f..6a60fec 100644 --- a/src/dat/mcx.c +++ b/src/dat/mcx.c @@ -9,18 +9,22 @@ #include "../util/compat/endian.h" #include "../util/intdef.h" +#define TABLE 0x2000 // table byte size +#define SECTOR 0x1000 // sector size +#define CHUNKS 0x400 // amount of chunks in a file + /* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */ static void mvchunks(u8 *restrict buf, u8 *src, u8 *dst, int src_s, int src_e) { assert(src > dst); u32 *table = (u32 *)buf; size_t len = src - dst; // acquire the amount of bytes that we shall move - assert(!(len % 0x1000)); + assert(!(len % SECTOR)); // count how many bytes we need to move, whilst updating location data size_t blen = 0; for (src_s++; src_s <= src_e; src_s++) { - blen += (be32toh(table[src_s]) & 0xFF) * 0x1000; - table[src_s] -= htobe32((len / 0x1000) << 8); + blen += (be32toh(table[src_s]) & 0xFF) * SECTOR; + table[src_s] -= htobe32((len / SECTOR) << 8); } memmove(dst, src, blen); } @@ -33,12 +37,12 @@ static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) { u32 *table = (u32 *)buf; size_t slen, bidx, blen; slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk - bidx = (be32toh(table[sidx]) >> 8) * 0x1000; // acquire and compute the byte offset the chunk starts at - blen = slen * 0x1000; // compute the byte length of the chunk + bidx = (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 + 0x400] = htobe32(time(NULL)); // assign the current time to the timestamp, for correctness NOTE: might need to zero-out instead + table[sidx + CHUNKS] = 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; @@ -51,26 +55,26 @@ static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) { * This is done instead of `delchunk` being globally linked, because * `delchunk` requests more specific parameters, which is confusing outside this module. */ size_t mcx_delchunk(u8 *restrict buf, int chunk) { - return delchunk(buf, 0, chunk, 0x400); + return delchunk(buf, 0, chunk, CHUNKS); } size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) { - assert(start < end && end < 0x400); + assert(start < end && end < CHUNKS); u32 *table = (u32 *)buf; - u8 *dst = buf + (be32toh(table[start]) >> 8) * 0x1000; - u8 *src = buf + (be32toh(table[end]) >> 8) * 0x1000; - src += (be32toh(table[end]) & 0xFF) * 0x1000; + u8 *dst = buf + (be32toh(table[start]) >> 8) * SECTOR; + u8 *src = buf + (be32toh(table[end]) >> 8) * SECTOR; + src += (be32toh(table[end]) & 0xFF) * SECTOR; // zeroes-out the chunk data within this range. (and set the timestamp) u32 ts = htobe32(time(NULL)); for (int i = start; i <= end; i++) { table[i] = 0; - table[i + 0x400] = ts; + table[i + CHUNKS] = ts; } // move the remaining chunks down - if (end < 0x3FF) - mvchunks(buf, src, dst, end, 0x3FF); + if (end < (CHUNKS - 1)) + mvchunks(buf, src, dst, end, (CHUNKS - 1)); return src - dst; } @@ -88,7 +92,7 @@ size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunk u16 chunkids[chunkc + 1]; memcpy(chunkids, chunks, chunkc); qsort(chunkids, chunkc, sizeof(int), cmp_chunkids); - chunkids[chunkc] = 0x400; // set the spare chunk to the max chunks, so the rest of the chunks are moved + chunkids[chunkc] = CHUNKS; // set the spare chunk to the max chunks, so the rest of the chunks are moved size_t rmb = 0; for (int i = 0; i < chunkc; i++) @@ -97,10 +101,10 @@ size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunk } /* Sum together the 4th byte in each location integer to compute the sector size of all chunks. - * Multiplying by `0x1000`, and adding the size of the table itself. */ + * Multiplying by `SECTOR`, and adding the size of the table itself. */ size_t mcx_calcsize(const u8 *restrict buf) { size_t size = 0; - for (uint i = 0; i < 0x400; i++) + for (uint i = 0; i < CHUNKS; i++) size += *(buf + (i * 4) + 3); - return (size * 0x1000) + 0x2000; + return (size * CHUNKS) + TABLE; }