From e02f4091b868e1adff6155196fc063876a4f3789 Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 25 Aug 2025 14:29:45 +0200 Subject: [PATCH] start writing code for `*.mcX` file parsing, starting with indexing logic. --- src/dat/mcx.c | 24 ++++++++++++++++++++++++ src/dat/mcx.h | 15 +++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/dat/mcx.c diff --git a/src/dat/mcx.c b/src/dat/mcx.c new file mode 100644 index 0000000..44f4f81 --- /dev/null +++ b/src/dat/mcx.c @@ -0,0 +1,24 @@ +#include "mcx.h" + +#include +#include +#include + +#include "../util/compat/endian.h" +#include "../util/intdef.h" + +/* 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. + * From the start, and bytes `0x000000FF` represent the length in `0x1000` sectors. */ +void mcx_index(const u8 *restrict buf, struct mcx_chunk *restrict chunks) { + const u32 *ptr = (u32 *)buf; + for (uint i = 0; i < 0x400; i++) { + u32 dat = be32toh(ptr[i]); + chunks[i] = (struct mcx_chunk){ + .idx = (dat >> 8) * 0x1000, + .len = (dat & 0xFF) * 0x1000, + .time = be32toh(ptr[i + 0x400]), + }; + } +} diff --git a/src/dat/mcx.h b/src/dat/mcx.h index 2a97db4..97dd584 100644 --- a/src/dat/mcx.h +++ b/src/dat/mcx.h @@ -2,6 +2,21 @@ // Licensed under the MIT Licence. See LICENSE for details #pragma once +#include + +#include "../util/atrb.h" +#include "../util/intdef.h" + +/* contains chunk metadata */ +struct mcx_chunk { + size_t idx; // byte offset for start of chunk data + u32 len; // byte length of chunk (+ padding) + u32 time; // modification time in epoch seconds +}; + +/* 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)); + /* the MCR (Minecraft region) and MCA (Minecraft anvil) files are similar * MCA is the newer variant, where it includes: * - a world height of 256, rather than 128.