mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-17 07:55:45 +01:00
refactor of mcx_delchunk to make things a bit more clear and flexible
This commit is contained in:
@@ -9,26 +9,33 @@
|
|||||||
#include "../util/compat/endian.h"
|
#include "../util/compat/endian.h"
|
||||||
#include "../util/intdef.h"
|
#include "../util/intdef.h"
|
||||||
|
|
||||||
void mcx_delchunk(u8 *restrict buf, int chunk) {
|
/* 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, and clear it
|
// load the table data, and clear it
|
||||||
u32 *table = (u32 *)buf;
|
u32 *table = (u32 *)buf;
|
||||||
size_t bidx = be32toh(table[chunk] >> 8) * 0x1000; // compute the byte offset the chunk starts at
|
size_t slen, bidx, blen;
|
||||||
size_t blen = be32toh(table[chunk] & 0xFF) * 0x1000; // compute the byte length of the chunk
|
slen = be32toh(table[sidx] & 0xFF); // acquire the sector length of the chunk
|
||||||
size_t slen = be32toh(table[chunk] & 0xFF); // acquire the sector length of the chunk
|
bidx = be32toh(table[sidx] >> 8) * 0x1000; // acquire and compute the byte offset the chunk starts at
|
||||||
table[chunk] = 0;
|
blen = slen * 0x1000; // compute the byte length of the chunk
|
||||||
table[chunk + 0x400] = time(NULL); // assign the current time to the timestamp, for correctness NOTE: might need to zero-out instead
|
table[sidx] = 0;
|
||||||
|
table[sidx + 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 *dst = buf + bidx - rmb;
|
||||||
u8 *head = buf + bidx;
|
u8 *src = buf + bidx + blen;
|
||||||
u8 *tail = buf + bidx + blen;
|
rmb = blen;
|
||||||
|
|
||||||
// count the amount of bytes that we must move
|
|
||||||
blen = 0;
|
blen = 0;
|
||||||
for (chunk++; chunk < 0x400; chunk++) {
|
for (sidx++; sidx < eidx; sidx++) {
|
||||||
blen += table[chunk] & 0xFF * 0x1000;
|
blen += be32toh(table[sidx] & 0xFF) * 0x1000;
|
||||||
table[chunk] -= htobe32(slen << 8);
|
table[sidx] -= htobe32(slen << 8);
|
||||||
}
|
}
|
||||||
memmove(head, tail, blen);
|
memmove(dst, src, blen);
|
||||||
|
return rmb;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t mcx_delchunk(u8 *restrict buf, int chunk) {
|
||||||
|
return delchunk(buf, 0, chunk, 0x400);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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,9 +14,8 @@ struct mcx_chunk {
|
|||||||
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. */
|
/* Deletes chunk `idx` from `buf`, moving all chunks downwards in the process. */
|
||||||
void mcx_delchunk(u8 *restrict buf, int idx);
|
size_t mcx_delchunk(u8 *restrict buf, int idx);
|
||||||
|
|
||||||
/* 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