split apart the moving of the data, for further flexibility

This commit is contained in:
2025-08-27 14:35:12 +02:00
parent f203cabad9
commit cd277873b5

View File

@@ -1,6 +1,6 @@
#include "mcx.h" #include "mcx.h"
#include <endian.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -9,7 +9,23 @@
#include "../util/compat/endian.h" #include "../util/compat/endian.h"
#include "../util/intdef.h" #include "../util/intdef.h"
/* Deletes chunk `sidx`, by moving chunks up to `eidx` back over `sidx` in `buf`. /* 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`.
* `rmb` is an optional additional offset that can be applied, and signifies bytes already removed. * `rmb` is an optional additional offset that can be applied, and signifies bytes already removed.
* Returns the bytes removed by this function. */ * Returns the bytes removed by this function. */
static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) { static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) {
@@ -24,16 +40,11 @@ static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) {
table[sidx] = 0; 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] = htobe32(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 *dst = buf + bidx - rmb;
u8 *src = buf + bidx + blen; u8 *src = buf + bidx + blen;
rmb = blen; mvchunks(buf, src, dst, sidx, eidx - 1);
blen = 0; return blen;
for (sidx++; sidx < eidx; sidx++) {
blen += be32toh(table[sidx] & 0xFF) * 0x1000;
table[sidx] -= htobe32((rmb / 0x1000) << 8);
}
memmove(dst, src, blen);
return rmb;
} }
/* Just call `delchunk` with the parameters and some defaults. /* Just call `delchunk` with the parameters and some defaults.