From b7859d56d9cfb5964b6fd50d014898e7621e29e3 Mon Sep 17 00:00:00 2001 From: Quinn Date: Wed, 27 Aug 2025 10:08:18 +0200 Subject: [PATCH] add function for computing the bytesize of the `*.mcX` file --- src/dat/mcx.c | 9 +++++++++ src/dat/mcx.h | 3 +++ 2 files changed, 12 insertions(+) diff --git a/src/dat/mcx.c b/src/dat/mcx.c index 7707c8c..d518cdc 100644 --- a/src/dat/mcx.c +++ b/src/dat/mcx.c @@ -28,6 +28,15 @@ void mcx_delchunk(u8 *restrict buf, int chunk) { memmove(head, tail, blen); } +/* 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. */ +size_t mcx_calcsize(const u8 *restrict buf) { + size_t size = 0; + for (uint i = 0; i < 0x400; i++) + size += *(buf + (i * 4) + 3); + return (size * 0x1000) + 0x2000; +} + /* an `*.mcX` contains a `0x2000` byte long table, the first `0x1000` containing * `0x400` entries of chunk data. * This chunk data is big-endian, where bytes `0xFFFFFF00` represent the `0x1000` sector offset. diff --git a/src/dat/mcx.h b/src/dat/mcx.h index c20a185..04f5ce2 100644 --- a/src/dat/mcx.h +++ b/src/dat/mcx.h @@ -18,6 +18,9 @@ struct mcx_chunk { /* Deletes chunk `idx` from `buf`, moving all chunks downwards in the process. */ void mcx_delchunk(u8 *restrict buf, int idx); +/* Computes the byte size of the `*.mcX` file in `buf` and returns it. */ +size_t mcx_calcsize(const u8 *restrict buf) NONNULL((1)) PURE; + /* indexes the chunks in an `*.mcX` file, writing `0x400` of entries to `chunks` */ void mcx_index(const u8 *restrict buf, struct mcx_chunk *restrict chunks) NONNULL((1, 2));