write a function for deleting a specific chunk.

This commit is contained in:
2025-08-26 12:12:26 +02:00
parent 2f68574aea
commit 1c863099a9
2 changed files with 26 additions and 1 deletions

View File

@@ -3,10 +3,31 @@
#include <endian.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#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.

View File

@@ -14,6 +14,10 @@ struct mcx_chunk {
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));