-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlzss-tool.c
583 lines (549 loc) · 14 KB
/
lzss-tool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/* LZSS compression and decompression tool
Written by Valley Bell, 2024-02
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "lzss-lib.h"
#ifdef _MSC_VER
#define strdup _strdup
#endif
typedef struct archive_header_value
{
char type;
union
{
struct
{
size_t len;
const uint8_t* ptr;
} str;
uint8_t dataVal;
struct
{
uint8_t bytes; // value size (number of bytes)
uint8_t endianess; // see ENDIAN_* constants
uint8_t set_val; // set to 1 for the item that sets values
} size;
} d;
} ARC_HDR_VAL;
#define ENDIAN_LITTLE 0
#define ENDIAN_BIG 1
typedef struct archive_header_specification
{
size_t len;
size_t count;
ARC_HDR_VAL vals[0x10];
} ARC_HDR_SPEC;
typedef struct file_data
{
size_t len;
uint8_t* data;
} FILE_DATA;
#define MODE_NONE 0x00
#define MODE_ENCODE 0x01
#define MODE_DECODE 0x02
static void PrintHelp(const char* appName);
static void ParseHeaderSpec(const char* spec, ARC_HDR_SPEC* header);
static size_t WriteArchiveHeader(FILE_DATA* hdrData, const ARC_HDR_SPEC* hdrSpec, size_t decSize, size_t cmpSize);
static uint8_t ReadArchiveHeader(const FILE_DATA* hdrData, const ARC_HDR_SPEC* hdrSpec, size_t* decSize, size_t* cmpSize);
int main(int argc, char *argv[])
{
FILE_DATA inFile;
FILE_DATA outFile;
ARC_HDR_SPEC arcHdrSpec;
uint8_t ret;
FILE* fp;
LZSS_CFG cfg;
LZSS_COMPR* lzss;
int argbase;
uint8_t mode;
lzssGetDefaultConfig(&cfg);
argbase = 1;
mode = MODE_NONE;
arcHdrSpec.len = 0x00;
arcHdrSpec.count = 0;
while(argbase < argc)
{
char* endptr;
long val;
if (!strcmp(argv[argbase], "-h")) // help
{
PrintHelp(argv[0]);
return 0;
}
else if (!strcmp(argv[argbase], "-e")) // encode
{
mode = MODE_ENCODE;
}
else if (!strcmp(argv[argbase], "-d")) // decode
{
mode = MODE_DECODE;
}
else if (!strcmp(argv[argbase], "-a")) // archive header
{
argbase ++;
if (argbase >= argc)
{
fprintf(stderr, "Insufficient arguments.\n");
return 1;
}
ParseHeaderSpec(argv[argbase], &arcHdrSpec);
}
else if (!strcmp(argv[argbase], "-n")) // name table initialization value
{
argbase ++;
if (argbase >= argc)
{
fprintf(stderr, "Insufficient arguments.\n");
return 1;
}
val = strtol(argv[argbase], &endptr, 0);
if (argv[argbase][0] == 'n')
{
cfg.nameTblType = LZSS_NTINIT_NONE;
}
else if (argv[argbase][0] == 'p')
{
cfg.nameTblType = LZSS_NTINIT_FUNC;
cfg.nameTblFunc = lzssNameTbl_CommonPatterns;
cfg.ntFuncParam = NULL;
}
else if (endptr != argv[argbase])
{
cfg.nameTblType = LZSS_NTINIT_VALUE;
cfg.nameTblValue = (uint8_t)val;
}
else
{
fprintf(stderr, "Unknown name table initialization parameter: %s\n", argv[argbase]);
return 1;
}
}
else if (!strcmp(argv[argbase], "-C")) // control word bit order
{
argbase ++;
if (argbase >= argc)
{
fprintf(stderr, "Insufficient arguments.\n");
return 1;
}
val = strtol(argv[argbase], &endptr, 0);
if (endptr != argv[argbase])
{
cfg.flags &= ~LZSS_FLAGS_CTRLMASK;
cfg.flags |= val ? LZSS_FLAGS_CTRL_M : LZSS_FLAGS_CTRL_L;
}
}
else if (!strcmp(argv[argbase], "-R")) // nametable reference value
{
argbase ++;
if (argbase >= argc)
{
fprintf(stderr, "Insufficient arguments.\n");
return 1;
}
val = strtol(argv[argbase], &endptr, 0);
if (endptr != argv[argbase])
{
cfg.flags &= ~(LZSS_FLAGS_MTCH_EMASK | LZSS_FLAGS_MTCH_LMASK);
cfg.flags |= (val << 4);
}
}
else
{
break;
}
argbase ++;
}
if (argc < argbase + 2)
{
PrintHelp(argv[0]);
return 1;
}
if (mode == MODE_NONE)
{
fprintf(stderr, "No mode specified!\n");
return 1;
}
fp = fopen(argv[argbase + 0], "rb");
if (fp == NULL)
{
fprintf(stderr, "Error opening input file: %s\n", argv[argbase + 0]);
return 2;
}
fseek(fp, 0, SEEK_END);
inFile.len = ftell(fp);
rewind(fp);
inFile.data = (uint8_t*)malloc(inFile.len);
fread(inFile.data, 1, inFile.len, fp);
fclose(fp);
lzss = lzssCreate(&cfg);
if (mode == MODE_ENCODE)
{
size_t encDataSize;
size_t dataOfs = arcHdrSpec.len;
outFile.len = dataOfs + inFile.len + inFile.len / 8 + 1;
outFile.data = (uint8_t*)malloc(outFile.len);
ret = lzssEncode(lzss, outFile.len - dataOfs, &outFile.data[dataOfs], &encDataSize, inFile.len, inFile.data);
fprintf(stderr, "In : %u bytes\n", inFile.len);
fprintf(stderr, "Out: %u bytes\n", encDataSize);
fprintf(stderr, "Ratio: %.2f %%\n", (double)encDataSize / inFile.len * 100.0);
WriteArchiveHeader(&outFile, &arcHdrSpec, inFile.len, encDataSize);
outFile.len = dataOfs + encDataSize;
}
else //if (mode == MODE_DECODE)
{
size_t cmpSize = inFile.len;
size_t dataOfs = arcHdrSpec.len;
outFile.len = inFile.len * 8; // assume compression up to 12.5%
ret = ReadArchiveHeader(&inFile, &arcHdrSpec, &outFile.len, &cmpSize);
if (ret)
{
fprintf(stderr, "Header parsing error!\n");
return 4;
}
if (inFile.len > cmpSize)
inFile.len = cmpSize;
outFile.data = (uint8_t*)malloc(outFile.len);
ret = lzssDecode(lzss, outFile.len, outFile.data, &outFile.len, inFile.len - dataOfs, &inFile.data[dataOfs]);
}
if (ret != LZSS_ERR_OK)
fprintf(stderr, "LZSS error code %u after writing %zu bytes.\n", ret, outFile.len);
fp = fopen(argv[argbase + 1], "wb");
if (fp == NULL)
{
fprintf(stderr, "Error opening output file: %s\n", argv[argbase + 1]);
return 3;
}
fwrite(outFile.data, 1, outFile.len, fp);
fclose(fp);
return 0;
}
static void PrintHelp(const char* appName)
{
fprintf(stderr, "Usage: %s [mode/options] input.bin output.bin\n", appName);
fprintf(stderr, "\n");
fprintf(stderr, "Mode: (required)\n");
fprintf(stderr, " -h show this help screen\n");
fprintf(stderr, " -e encode / compress\n");
fprintf(stderr, " -d decode / decompress\n");
fprintf(stderr, "\n");
fprintf(stderr, "File format options:\n");
fprintf(stderr, " -a .. add/read archive header (list of comma-separated values)\n");
fprintf(stderr, " n - none, don't add any header [default]\n");
fprintf(stderr, " oNE - original size\n");
fprintf(stderr, " cNE - compressed size\n");
fprintf(stderr, " N = size of the value in bytes (2/4)\n");
fprintf(stderr, " E = endianess (L = little [default], B = big) [optional]\n");
fprintf(stderr, " sABC - string \"ABC\"\n");
fprintf(stderr, " bXX - byte XX (hexadecimal value)\n");
fprintf(stderr, " iXX - ignored (XX is optional and used for writing)\n");
fprintf(stderr, " Example: -a sLZS,b1A,c2B,o4\n");
fprintf(stderr, "\n");
fprintf(stderr, "Compression options:\n");
fprintf(stderr, " -n n name table initialization value (0x00..0xFF, default: 0x20/space)\n");
fprintf(stderr, " special values:\n");
fprintf(stderr, " n - none (prevent lookup to data before beginning of the file)\n");
fprintf(stderr, " p - various patterns (commonly used by Japanese developers)\n");
fprintf(stderr, " -C n control word bit order (0 = low->high [default], 1 = high->low)\n");
fprintf(stderr, " -R n reference word format (bit mask, default: 0x01)\n");
fprintf(stderr, " mask 0x03: nibble position (0 = highest .. 3 = lowest)\n");
fprintf(stderr, " mask 0x04: byte endianess (0 = Little Endian, 4 = Big Endian)\n");
return;
}
static void ChooseHeaderSetVal(ARC_HDR_SPEC* header, char item_type)
{
size_t lastSetItem = (size_t)-1;
size_t lastSize = 0;
size_t hdrItem;
for (hdrItem = 0; hdrItem < header->count; hdrItem++)
{
ARC_HDR_VAL* ahv = &header->vals[header->count];
if (ahv->type != item_type)
continue;
if (ahv->type != 'o' || ahv->type != 'c')
continue;
ahv->d.size.set_val = 0;
if (ahv->d.size.bytes >= lastSize)
{
lastSetItem = hdrItem;
lastSize = ahv->d.size.bytes;
}
}
if (lastSetItem != (size_t)-1)
header->vals[lastSetItem].d.size.set_val = 1;
return;
}
static void ParseHeaderSpec(const char* spec, ARC_HDR_SPEC* header)
{
char* spec_str;
char* spec_ptr;
char* endptr;
header->len = 0x00;
header->count = 0;
if (*spec == '\0')
return;
spec_str = strdup(spec);
spec_ptr = spec_str;
while (spec_ptr != NULL && *spec_ptr != '\0' && header->count < 0x10)
{
ARC_HDR_VAL* ahv = &header->vals[header->count];
char mode = *spec_ptr;
char* sep_ptr = strchr(spec_ptr, ',');
if (sep_ptr != NULL)
*sep_ptr = '\0';
spec_ptr += 1;
if (mode == 'n')
{
header->count = 0;
break;
}
else if (mode == 'o' || mode == 'c')
{
size_t param_len = strlen(spec_ptr);
uint8_t is_good = 1;
ahv->type = mode;
ahv->d.size.bytes = 4;
ahv->d.size.endianess = ENDIAN_LITTLE;
ahv->d.size.set_val = 0;
// parse value size digit
if (param_len >= 1)
{
if (spec_ptr[0] >= '0' && spec_ptr[0] <= '9')
{
ahv->d.size.bytes = spec_ptr[0] - '0';
if (!(ahv->d.size.bytes == 2 || ahv->d.size.bytes == 4))
{
fprintf(stderr, "Archive header mode %c: Value size is %u, but can only be 2 or 4.\n",
mode, ahv->d.size.bytes);
is_good = 0;
}
}
else
{
fprintf(stderr, "Archive header mode %c: Invalid value size: %c\n", mode, spec_ptr[0]);
is_good = 0;
}
}
// parse endianess
if (param_len >= 2)
{
if (spec_ptr[1] == 'L' || spec_ptr[1] == 'l')
ahv->d.size.endianess = ENDIAN_LITTLE;
else if (spec_ptr[1] == 'B' || spec_ptr[1] == 'b')
ahv->d.size.endianess = ENDIAN_BIG;
else
{
fprintf(stderr, "Archive header mode %c: Invalid endianess: %c\n", mode, spec_ptr[1]);
is_good = 0;
}
}
if (is_good)
{
header->count ++;
header->len += ahv->d.size.bytes;
}
}
else if (mode == 's')
{
ahv->type = mode;
ahv->d.str.ptr = spec + (spec_ptr - spec_str);
ahv->d.str.len = strlen(spec_ptr);
header->count ++;
header->len += ahv->d.str.len;
}
else if (mode == 'b' || mode == 'i')
{
ahv->type = mode;
ahv->d.dataVal = (uint8_t)strtoul(spec_ptr, &endptr, 0x10);
if (mode == 'i' && *spec_ptr == '\0')
{
ahv->d.dataVal = 0x00;
header->count ++;
header->len += 0x01;
}
else if (*endptr == '\0' && *spec_ptr != '\0')
{
header->count ++;
header->len += 0x01;
}
else
{
fprintf(stderr, "Archive header specification: Invalid byte value: %s\n", spec_ptr);
}
}
else
{
fprintf(stderr, "Archive header specification: Invalid header value type: %c\n", mode);
}
if (sep_ptr == NULL)
break;
spec_ptr = sep_ptr + 1;
}
free(spec_str);
ChooseHeaderSetVal(header, 'o');
ChooseHeaderSetVal(header, 'c');
return;
}
static size_t ReadLE(const uint8_t* buffer, uint8_t valSize)
{
size_t pos;
size_t val = 0x00;
for (pos = valSize; pos > 0; pos--)
{
val <<= 8;
val |= buffer[pos - 1];
}
return val;
}
static void WriteLE(uint8_t* buffer, uint8_t valSize, size_t val)
{
size_t pos;
for (pos = 0; pos < valSize; pos++)
{
buffer[pos] = val & 0xFF;
val >>= 8;
}
return;
}
static size_t ReadBE(const uint8_t* buffer, uint8_t valSize)
{
size_t pos;
size_t val = 0x00;
for (pos = 0; pos < valSize; pos++)
{
val <<= 8;
val |= buffer[pos];
}
return val;
}
static void WriteBE(uint8_t* buffer, uint8_t valSize, size_t val)
{
size_t pos;
for (pos = valSize; pos > 0; pos--)
{
buffer[pos - 1] = val & 0xFF;
val >>= 8;
}
return;
}
static size_t WriteArchiveHeader(FILE_DATA* hdrData, const ARC_HDR_SPEC* hdrSpec, size_t decSize, size_t cmpSize)
{
size_t hdrItem;
size_t pos;
if (hdrSpec->len == 0)
return 0x00;
pos = 0x00;
for (hdrItem = 0; hdrItem < hdrSpec->count; hdrItem++)
{
const ARC_HDR_VAL* ahv = &hdrSpec->vals[hdrItem];
if (pos >= hdrData->len)
break; // should never happen
switch (ahv->type)
{
case 'n':
hdrData->len = 0x00;
return 0x00;
case 'o':
case 'c':
{
size_t val = 0;
if (pos + ahv->d.size.bytes > hdrData->len)
break;
if (ahv->type == 'o')
val = decSize;
else //if (ahv->type == 'c')
val = cmpSize;
if (ahv->d.size.endianess == ENDIAN_LITTLE)
WriteLE(&hdrData->data[pos], ahv->d.size.bytes, val);
else //if (ahv->d.size.endianess == ENDIAN_BIG)
WriteBE(&hdrData->data[pos], ahv->d.size.bytes, val);
pos += ahv->d.size.bytes;
break;
}
case 's':
{
size_t copyLen = ahv->d.str.len;
if (pos + copyLen > hdrData->len)
copyLen = hdrSpec->len - pos;
memcpy(&hdrData->data[pos], ahv->d.str.ptr, copyLen);
pos += ahv->d.str.len;
break;
}
case 'b':
case 'i':
hdrData->data[pos] = ahv->d.dataVal;
pos += 0x01;
break;
}
}
return pos;
}
static uint8_t ReadArchiveHeader(const FILE_DATA* hdrData, const ARC_HDR_SPEC* hdrSpec, size_t* decSize, size_t* cmpSize)
{
size_t hdrItem;
size_t pos;
if (hdrSpec->len == 0)
return 0;
pos = 0x00;
for (hdrItem = 0; hdrItem < hdrSpec->count; hdrItem++)
{
const ARC_HDR_VAL* ahv = &hdrSpec->vals[hdrItem];
if (pos >= hdrData->len)
break; // should never happen
switch (ahv->type)
{
case 'n':
return 0;
case 'o':
case 'c':
{
size_t val;
if (pos + ahv->d.size.bytes > hdrData->len)
break;
if (ahv->d.size.endianess == ENDIAN_LITTLE)
val = ReadLE(&hdrData->data[pos], ahv->d.size.bytes);
else //if (ahv->d.size.endianess == ENDIAN_BIG)
val = ReadBE(&hdrData->data[pos], ahv->d.size.bytes);
if (ahv->d.size.set_val)
{
if (ahv->type == 'o')
*decSize = val;
else //if (ahv->type == 'c')
*cmpSize = val;
}
pos += ahv->d.size.bytes;
break;
}
case 's':
{
size_t checkLen = ahv->d.str.len;
if (pos + checkLen > hdrData->len)
checkLen = hdrSpec->len - pos;
if (memcmp(&hdrData->data[pos], ahv->d.str.ptr, checkLen))
{
fprintf(stderr, "Header mismatch at offset 0x%02X: \"%.*s\" != \"%.*s\"\n",
pos, checkLen, (const char*)&hdrData->data[pos], ahv->d.str.len, ahv->d.str.ptr);
return 1;
}
pos += ahv->d.str.len;
break;
}
case 'b':
if (hdrData->data[pos] != ahv->d.dataVal)
{
fprintf(stderr, "Header mismatch at offset 0x%02X: byte 0x%02X != 0x%02X\n",
pos, hdrData->data[pos], ahv->d.dataVal);
return 1;
}
pos += 0x01;
break;
case 'i':
pos += 0x01;
break;
}
}
return 0;
}