diff --git a/src/dat/mcx.c b/src/dat/mcx.c index 44f4f81..7707c8c 100644 --- a/src/dat/mcx.c +++ b/src/dat/mcx.c @@ -3,10 +3,31 @@ #include #include #include +#include +#include #include "../util/compat/endian.h" #include "../util/intdef.h" +void mcx_delchunk(u8 *restrict buf, int chunk) { + // load the table data, and clear it + u32 *table = (u32 *)buf; + size_t bidx = (table[chunk] >> 8) * 0x1000; // compute the byte offset the chunk starts at + size_t blen = (table[chunk] & 0xFF) * 0x1000; // compute the byte length of the chunk + table[chunk] = 0; + table[chunk + 0x400] = time(NULL); // assign the current time to the timestamp, for correctness NOTE: might need to zero-out instead + + // store the head and tail end of the current chunk + u8 *head = buf + bidx; + u8 *tail = buf + bidx + blen; + + // count the amount of bytes that we must move + blen = 0; + for (chunk++; chunk < 0x400; chunk++) + blen += table[chunk] & 0xFF * 0x1000; + memmove(head, tail, blen); +} + /* 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 97dd584..c20a185 100644 --- a/src/dat/mcx.h +++ b/src/dat/mcx.h @@ -11,9 +11,13 @@ 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 + u32 time; // modification time in epoch seconds }; +// TODO: should return some form of feedback about its success +/* Deletes chunk `idx` from `buf`, moving all chunks downwards in the process. */ +void mcx_delchunk(u8 *restrict buf, int idx); + /* 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));