mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 16:15:45 +01:00
Compare commits
8 Commits
78db656a92
...
bc8803525d
| Author | SHA1 | Date | |
|---|---|---|---|
| bc8803525d | |||
| 7531d786be | |||
| 2c5b9def28 | |||
| cd277873b5 | |||
| f203cabad9 | |||
| 28dd8af353 | |||
| 9c690eb327 | |||
| af8211c5ce |
@@ -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,35 +9,70 @@
|
|||||||
#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) {
|
||||||
// load the table data, and clear it
|
// load the table data
|
||||||
u32 *table = (u32 *)buf;
|
u32 *table = (u32 *)buf;
|
||||||
size_t slen, bidx, blen;
|
size_t slen, bidx, blen;
|
||||||
slen = be32toh(table[sidx] & 0xFF); // acquire the sector length of the chunk
|
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
|
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
|
blen = slen * 0x1000; // compute the byte length of the chunk
|
||||||
table[sidx] = 0;
|
|
||||||
table[sidx + 0x400] = time(NULL); // assign the current time to the timestamp, for correctness NOTE: might need to zero-out instead
|
|
||||||
|
|
||||||
|
// reset the table data
|
||||||
|
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
|
||||||
|
|
||||||
|
// 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(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) {
|
size_t mcx_delchunk(u8 *restrict buf, int chunk) {
|
||||||
return delchunk(buf, 0, chunk, 0x400);
|
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 */
|
/* comparer function for to be inputted into `qsort` to compare two */
|
||||||
static int cmp_chunkids(const void *restrict x, const void *restrict y) {
|
static int cmp_chunkids(const void *restrict x, const void *restrict y) {
|
||||||
u16 x2 = *(u16 *)x;
|
u16 x2 = *(u16 *)x;
|
||||||
@@ -45,16 +80,19 @@ static int cmp_chunkids(const void *restrict x, const void *restrict y) {
|
|||||||
return (x2 > y2) - (x2 < y2);
|
return (x2 > y2) - (x2 < y2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mcx_delchunks(u8 *restrict buf, const u16 *restrict chunks, int chunkc) {
|
/* 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) {
|
||||||
// ensure the chunks ids we're working on are sorted from least to greatest
|
// ensure the chunks ids we're working on are sorted from least to greatest
|
||||||
u16 chunkids[chunkc + 1];
|
u16 chunkids[chunkc + 1];
|
||||||
memcpy(chunkids, chunks, chunkc);
|
memcpy(chunkids, chunks, chunkc);
|
||||||
qsort(chunkids, chunkc, sizeof(int), cmp_chunkids);
|
qsort(chunkids, chunkc, sizeof(int), cmp_chunkids);
|
||||||
chunkids[chunkc] = 0; // set the spare chunk to zero, to prevent out-of-bounds access
|
chunkids[chunkc] = 0x400; // set the spare chunk to the max chunks, so the rest of the chunks are moved
|
||||||
|
|
||||||
size_t rmb = 0;
|
size_t rmb = 0;
|
||||||
for (int i = 0; i < chunkc; i++)
|
for (int i = 0; i < chunkc; i++)
|
||||||
rmb += delchunk(buf, rmb, chunkids[i], chunkids[i + 1]);
|
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.
|
/* Sum together the 4th byte in each location integer to compute the sector size of all chunks.
|
||||||
|
|||||||
@@ -14,12 +14,24 @@ struct mcx_chunk {
|
|||||||
u32 time; // modification time in epoch seconds
|
u32 time; // modification time in epoch seconds
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Deletes chunk `idx` from `buf`, moving all chunks downwards in the process. */
|
/* Deletes a single chunk (`chunk`) out of `buf`.
|
||||||
size_t mcx_delchunk(u8 *restrict buf, int idx);
|
* 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 `chunkc` chunks specified in `chunks` from the `*.mcX` file.
|
/* Deletes the range defined by `start`—`end` (inclusive) of chunks out of `buf`.
|
||||||
* This is done in a way to perform minimal memmove operations. */
|
* The chunk's location data shall become `0`, and timestamp data the current time.
|
||||||
void mcx_delchunks(u8 *restrict buf, const u16 *restrict chunks, int chunkc);
|
* 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));
|
||||||
|
|
||||||
/* Computes the byte size of the `*.mcX` file in `buf` and returns it. */
|
/* Computes the byte size of the `*.mcX` file in `buf` and returns it. */
|
||||||
size_t mcx_calcsize(const u8 *restrict buf) NONNULL((1)) PURE;
|
size_t mcx_calcsize(const u8 *restrict buf) NONNULL((1)) PURE;
|
||||||
|
|||||||
Reference in New Issue
Block a user