[Maintenance] [th01] Packfiles: Update to current coding standards

Part of P0231, funded by [Anonymous].
This commit is contained in:
nmlgc 2023-02-07 07:07:46 +01:00
parent 6b5102d2e7
commit 7191f62dde
1 changed files with 31 additions and 35 deletions

View File

@ -12,11 +12,13 @@
#undef arc_file_get #undef arc_file_get
#define FILE_COUNT 64 static const int FILE_COUNT = 64;
#define CACHE_SIZE 0x100 static const size_t CACHE_SIZE = 0x100;
#define PF_TYPE_COMPRESSED "\x95\x95" // "封" in Shift-JIS
typedef struct { typedef struct {
uint8_t type[2]; // •• if RLE-compressed uint8_t type[2]; // PF_TYPE_COMPRESSED if RLE-compressed
int8_t aux; // Always 3, unused int8_t aux; // Always 3, unused
char fn[PF_FN_LEN]; char fn[PF_FN_LEN];
int32_t packsize; int32_t packsize;
@ -40,24 +42,25 @@ size_t cache_bytes_read;
void pascal arc_load(const char fn[PF_FN_LEN]) void pascal arc_load(const char fn[PF_FN_LEN])
{ {
int i, c; int i;
int c;
arc_pfs = new pf_header_t[FILE_COUNT]; arc_pfs = new pf_header_t[FILE_COUNT];
file_ropen(fn); file_ropen(fn);
for(i = 0; i < PF_FN_LEN; i++) { for(i = 0; i < PF_FN_LEN; i++) {
arc_fn[i] = fn[i]; arc_fn[i] = fn[i];
if(fn[i] == 0) { if(fn[i] == '\0') {
break; break;
} }
} }
file_read(arc_pfs, sizeof(pf_header_t) * FILE_COUNT); file_read(arc_pfs, (sizeof(pf_header_t) * FILE_COUNT));
file_close(); file_close();
for(i = 0; i < FILE_COUNT; i++) { for(i = 0; i < FILE_COUNT; i++) {
if(arc_pfs[i].type[0] == 0) { if(arc_pfs[i].type[0] == 0) {
break; break;
} }
for(c = 0; c < PF_FN_LEN; c++) { for(c = 0; c < PF_FN_LEN; c++) {
if(arc_pfs[i].fn[c] == 0) { if(arc_pfs[i].fn[c] == '\0') {
break; break;
} }
arc_pfs[i].fn[c] = ~arc_pfs[i].fn[c]; arc_pfs[i].fn[c] = ~arc_pfs[i].fn[c];
@ -73,12 +76,11 @@ void arc_free(void)
bool16 pascal near at_pos_of(const char fn[PF_FN_LEN]) bool16 pascal near at_pos_of(const char fn[PF_FN_LEN])
{ {
int i; for(int i = 0; i < PF_FN_LEN; i++) {
for(i = 0; i < PF_FN_LEN; i++) {
if(arc_pfs[cur_file_id].fn[i] != toupper(fn[i])) { if(arc_pfs[cur_file_id].fn[i] != toupper(fn[i])) {
return false; return false;
} }
if(fn[i] == 0) { if(fn[i] == '\0') {
return true; return true;
} }
} }
@ -88,13 +90,12 @@ bool16 pascal near at_pos_of(const char fn[PF_FN_LEN])
// Get it? En*crap*tion? // Get it? En*crap*tion?
void pascal near crapt(uint8_t *buf, size_t size) void pascal near crapt(uint8_t *buf, size_t size)
{ {
size_t i; for(size_t i = 0; i < size; i++) {
for(i = 0; i < size; i++) {
buf[i] ^= arc_key; buf[i] ^= arc_key;
} }
} }
uint8_t pascal near cache_next(void) uint8_t pascal near cache_next_raw(void)
{ {
uint8_t b; uint8_t b;
if(cache_bytes_read == 0) { if(cache_bytes_read == 0) {
@ -109,6 +110,11 @@ uint8_t pascal near cache_next(void)
return b; return b;
} }
inline void near cache_next(uint8_t& ret, long& bytes_read) {
ret = cache_next_raw();
bytes_read++;
}
void pascal near unrle(size_t input_size) void pascal near unrle(size_t input_size)
{ {
uint8_t var_1; uint8_t var_1;
@ -116,43 +122,34 @@ void pascal near unrle(size_t input_size)
uint8_t var_3; uint8_t var_3;
long bytes_read = 0; long bytes_read = 0;
long bytes_written = 0; long bytes_written = 0;
#define NEXT() \ cache_next(var_3, bytes_read);
cache_next(); \
bytes_read++;
var_3 = NEXT();
while(input_size > bytes_read) { while(input_size > bytes_read) {
do { do {
file_data[bytes_written] = var_3; var_1 = file_data[bytes_written++] = var_3;
var_1 = var_3; cache_next(var_3, bytes_read);
bytes_written++;
var_3 = NEXT();
} while(var_1 != var_3); } while(var_1 != var_3);
file_data[bytes_written] = var_3; file_data[bytes_written++] = var_3;
while(1) { while(1) {
bytes_written++; cache_next(runs, bytes_read);
runs = NEXT();
while(runs > 0) { while(runs > 0) {
file_data[bytes_written] = var_1; file_data[bytes_written++] = var_1;
bytes_written++;
runs--; runs--;
} }
var_3 = NEXT(); cache_next(var_3, bytes_read);
if(var_3 != var_1) { if(var_3 != var_1) {
break; break;
} }
file_data[bytes_written] = var_1; file_data[bytes_written++] = var_1;
} }
} }
#undef NEXT()
} }
void pascal arc_file_load(const char fn[PF_FN_LEN]) void pascal arc_file_load(const char fn[PF_FN_LEN])
{ {
const uint8_t rle_type[] = {"••"}; const uint8_t rle_type[] = PF_TYPE_COMPRESSED;
int i;
cur_file_id = 0; cur_file_id = 0;
for(i = 0; i < arc_pf_count; i++) { for(int i = 0; i < arc_pf_count; i++) {
if(at_pos_of(fn)) { if(at_pos_of(fn)) {
break; break;
} }
@ -161,7 +158,7 @@ void pascal arc_file_load(const char fn[PF_FN_LEN])
file_pf = &arc_pfs[cur_file_id]; file_pf = &arc_pfs[cur_file_id];
file_ropen(arc_fn); file_ropen(arc_fn);
file_seek(file_pf->offset, SEEK_SET); file_seek(file_pf->offset, SEEK_SET);
if(file_pf->type[0] == rle_type[0] && file_pf->type[1] == rle_type[1]) { if((file_pf->type[0] == rle_type[0]) && (file_pf->type[1] == rle_type[1])) {
file_compressed = true; file_compressed = true;
} else { } else {
file_compressed = false; file_compressed = false;
@ -183,8 +180,7 @@ void pascal arc_file_load(const char fn[PF_FN_LEN])
void pascal arc_file_get(uint8_t *buf, size_t size) void pascal arc_file_get(uint8_t *buf, size_t size)
{ {
uint8_t *p = buf; uint8_t *p = buf;
size_t i; for(size_t i = 0; i < size; i++) {
for(i = 0; i < size; i++) {
if(file_pos >= file_pf->orgsize) { if(file_pos >= file_pf->orgsize) {
break; break;
} }