Compare commits

..

1 Commits

Author SHA1 Message Date
78db656a92 write function for bulk removal of chunks 2025-08-27 12:30:38 +02:00
2 changed files with 22 additions and 72 deletions

View File

@@ -1,6 +1,6 @@
#include "mcx.h"
#include <assert.h>
#include <endian.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@@ -9,70 +9,35 @@
#include "../util/compat/endian.h"
#include "../util/intdef.h"
/* 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);
// 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);
}
memmove(dst, src, blen);
}
/* Deletes chunk `sidx` by moving chunks up to `eidx` back over `sidx` in `buf`.
/* Deletes chunk `sidx`, by moving chunks up to `eidx` back over `sidx` in `buf`.
* `rmb` is an optional additional offset that can be applied, and signifies bytes already removed.
* Returns the bytes removed by this function. */
static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) {
// load the table data
// load the table data, and clear it
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
// reset the table data
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
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 + 0x400] = 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;
u8 *src = buf + bidx + blen;
mvchunks(buf, src, dst, sidx, eidx - 1);
return blen;
rmb = blen;
blen = 0;
for (sidx++; sidx < eidx; sidx++) {
blen += be32toh(table[sidx] & 0xFF) * 0x1000;
table[sidx] -= htobe32(slen << 8);
}
memmove(dst, src, blen);
return rmb;
}
/* Just call `delchunk` with the parameters and some defaults.
* 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);
}
size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) {
assert(start < end && end < 0x400);
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;
// 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;
}
// move the remaining chunks down
mvchunks(buf, src, dst, start, end);
return src - dst;
}
/* comparer function for to be inputted into `qsort` to compare two */
static int cmp_chunkids(const void *restrict x, const void *restrict y) {
u16 x2 = *(u16 *)x;
@@ -80,19 +45,16 @@ static int cmp_chunkids(const void *restrict x, const void *restrict y) {
return (x2 > y2) - (x2 < y2);
}
/* Sorts the chunks marked for deletion from smallest to greatest index.
* Then performs the deletion in this order. Making sure to only update the chunks up to the next. */
size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) {
void mcx_delchunks(u8 *restrict buf, const u16 *restrict chunks, int chunkc) {
// ensure the chunks ids we're working on are sorted from least to greatest
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] = 0; // set the spare chunk to zero, to prevent out-of-bounds access
size_t rmb = 0;
for (int i = 0; i < chunkc; i++)
rmb += delchunk(buf, rmb, chunkids[i], chunkids[i + 1]);
return rmb;
}
/* Sum together the 4th byte in each location integer to compute the sector size of all chunks.

View File

@@ -14,24 +14,12 @@ struct mcx_chunk {
u32 time; // modification time in epoch seconds
};
/* Deletes a single chunk (`chunk`) out of `buf`.
* The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed. */
size_t mcx_delchunk(u8 *restrict buf, int chunk) NONNULL((1));
/* Deletes chunk `idx` from `buf`, moving all chunks downwards in the process. */
size_t mcx_delchunk(u8 *restrict buf, int idx);
/* Deletes the range defined by `start`—`end` (inclusive) of chunks out of `buf`.
* The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed */
size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) NONNULL((1));
/* Deletes a `chunkc` chunks from `chunks` out of `buf`.
* If the `chunks` indices are known to be sequential, i.e. have a constant difference of `1`, `mcx_delchunk_range` should be preferred.
* The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed */
size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) NONNULL((1, 2));
/* Deletes `chunkc` chunks specified in `chunks` from the `*.mcX` file.
* This is done in a way to perform minimal memmove operations. */
void mcx_delchunks(u8 *restrict buf, const u16 *restrict chunks, int chunkc);
/* Computes the byte size of the `*.mcX` file in `buf` and returns it. */
size_t mcx_calcsize(const u8 *restrict buf) NONNULL((1)) PURE;