-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
453 lines (421 loc) · 15.1 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include "types.h"
#include "aqualead_types.h"
size_t ALLZ_Decode(u8** ptr_dst, u8* src, size_t srcSize);
s32 ALLZ_Setup_EncFlags(s32* encFlags, u8 alFlag, u8** ptr_encoded_data);
s32 ALLZ_AnalyzeBlock(s32* encFlags, u8 alFlag, u8** ptr_encoded_data);
int main(int argc, char* argv[])
{
int al_fileno;
FILE* al_file = NULL;
FILE* outfile = NULL;
u8* input_buf = NULL;
u8* output_buf = NULL;
u32 magic = 0;
size_t read_ret;
size_t decret;
const char* outfile_name = NULL;
printf("\n"
"===========================\n"
"|| Aqualead LZSS Decoder ||\n"
"===========================\n"
" Written by Brolijah.\n");
if(argc < 2) // No input and/or output path specified
{
printf("ERROR: Incorrect usage!!\n"
" Aqualead_LZSS.exe [INPUT_FILE] [OUTPUT_FILE]\n");
return EXIT_FAILURE;
} else if(argc == 2) // output file not specified
{
#ifdef WIN32
char fname[_MAX_FNAME];
char extension[_MAX_EXT];
_splitpath(argv[1], NULL, NULL, fname, extension);
outfile_name = malloc(5 + strlen(fname) + strlen(extension));
sprintf(outfile_name, "out_%s%s", fname, extension);
#else
// Linux users should consider using basename()
// https://linux.die.net/man/3/basename
#endif
} else { // output file supplied
outfile_name = argv[2];
}
al_file = fopen(argv[1], "rb");
if(!al_file)
{
printf("ERROR: Failed to open input file!!\n");
return EXIT_FAILURE;
}
read_ret = fread(&magic, 4, 1, al_file); // assuming little endian
rewind(al_file);
if(read_ret)
{
//printf("File Magic = 0x%08X\n", magic);
if(magic == AL_TYPE_ALLZ) // proceed to decrypt the file
{
// fstat on the aqualead file and malloc the input buffer
al_fileno = fileno(al_file);
{
struct stat filestats;
if(fstat(al_fileno, &filestats))
{
if(errno == EBADF)
printf("ERROR: fstat() : bad file descriptor!!\n");
else if(errno == EINVAL)
printf("ERROR: fstat() : invalid argument!!\n");
goto cleanup;
}
input_buf = malloc(filestats.st_size);
// read whole file into input buffer
read_ret = fread(input_buf, sizeof(*input_buf), filestats.st_size, al_file);
if(read_ret < (size_t)filestats.st_size) // ???
{
printf("ERROR: fread() : Unexpectedly reached end of file!!\n");
goto cleanup;
}
}
// close the al_file
fclose(al_file);
al_file = NULL;
// send to decode
decret = ALLZ_Decode(&output_buf, input_buf, read_ret);
if(!decret)
{
printf("ERROR: ALLZ_Decode() : Failed to decompress the file!!\n");
goto cleanup;
}
printf("Successfully decompressed the file! Saving file...\n");
// save decoded buffer
outfile = fopen(outfile_name, "wb");
if(!outfile)
{
printf("ERROR: Failed to open output file!!\n");
goto cleanup;
}
if(!fwrite(output_buf, decret, 1, outfile))
{
printf("ERROR: fwrite() : Failed to write data to output file!!\n");
}
// cleanup after here
} else {
printf("ERROR: File specified was not an Aqualead ALLZ file!!\n");
}
} else {
if(feof(al_file))
printf("ERROR: Unexpectedly reached end of file!!\n");
else if(ferror(al_file))
printf("ERROR: Failed to read file??\n");
}
cleanup:
if(outfile != NULL) {
fclose(outfile);
outfile = NULL;
}
if(al_file != NULL) {
fclose(al_file);
al_file = NULL;
}
if(input_buf != NULL)
free(input_buf);
if(output_buf != NULL)
free(output_buf);
printf("Exiting...\n");
return EXIT_SUCCESS;
}
/**
* @brief ALLZ_Decode
* Decompresses the packed file data of an Aqualead LZSS file and writes
* to the supplied destination.
* @param ptr_dst
* Pointer-to-pointer to your destination buffer. This is a double pointer
* so that the caller does not need to allocate it before calling.
* @param src
* Pointer to the Aqualead compressed file data.
* @param srcSize
* Size of the Aqualead compressed file in bytes.
* @return
* On SUCCESS, length of decompressed data.
* On ERROR, returns zero. An error indicates either
* (1) Analysis of the compressed file passed EOF
* (2) Did not finish decoding the entire file.
*/
size_t ALLZ_Decode(u8** ptr_dst, u8* src, size_t srcSize)
{
// aqualead file metadata
u8 alarFlag1;
u8 alarFlag2;
u8 allzFlag1;
u8 allzFlag2;
u32 fullSize;
// local variables
u8* dst; /// pointer to destination buffer
u8* encoded_src; /// used in memory to recall data position
u8* encoded_eof; /// used to break out of function
u8* decoded_eof; /// used to break out of function
s32 lzssFlags[2]; /// Some 8-byte variable (probably supposed to be a struct of two integers)
s32 disp_offset; /// offset of displaced bytes needed duplicate
s32 disp_length; /// length of displacement to duplicate
u8* duplicates = NULL; /// pointer to displaced bytes
// These three are lacking proper names
s32 local_var1 = 0; /// Used during ALLZ FLAG 2 decode. Used to copy unencoded bytes.
s32 local_var3 = 0; /// Used during ALLZ FLAG 1 decode. Gets assigned to disp_offset.
s32 local_var5 = 0; /// Used during ALAR FLAG 2 decode. Gets assigned to disp_length.
u8 tempAlFlag; /// Temporary Aqualead Flag for decoding.
s32 tempEncFlag; /// Temporary copy of lzssFlags[0] used to escape loops.
u8* src_temp; /// Temporary copy of encoded_src used to copy duplicate unencoded bytes.
// initialize default values and read ALLZ header
alarFlag1 = *( u8*)(src + 0x04);
alarFlag2 = *( u8*)(src + 0x05);
allzFlag1 = *( u8*)(src + 0x06);
allzFlag2 = *( u8*)(src + 0x07);
fullSize = *(u32*)(src + 0x08);
encoded_src = ( u8*)(src + 0x0C); // src + 12
lzssFlags[0] = 0;
lzssFlags[1] = 0;
if(!(*ptr_dst)) // stupid-proofing in case the caller didn't malloc a buffer
{
*ptr_dst = malloc(fullSize);
memset(*ptr_dst, 0, fullSize);
}
dst = *ptr_dst;
decoded_eof = dst + fullSize;
encoded_eof = src + srcSize;
/* printf("Aqualead LZSS File Metadata:\n"
" ALAR Flag1 = %d\n"
" ALAR Flag2 = %d\n"
" ALLZ Flag1 = %d\n"
" ALLZ Flag2 = %d\n"
" Full size = %u\n\n",
alarFlag1, alarFlag2, allzFlag1, allzFlag2, fullSize); */
if( !alarFlag1 )
{
ALLZ_AnalyzeBlock((s32*)lzssFlags, 1, &encoded_src);
}
local_var1 = 1 + ALLZ_Setup_EncFlags((s32*)lzssFlags, allzFlag2, &encoded_src);
do {
*dst++ = *encoded_src++;
} while(--local_var1 != 0);
local_var3 = ALLZ_Setup_EncFlags((s32*)lzssFlags, allzFlag1, &encoded_src);
disp_offset = local_var3 + 1;
local_var5 = ALLZ_Setup_EncFlags((s32*)lzssFlags, alarFlag2, &encoded_src);
disp_length = local_var5 + 3;
if(encoded_src <= encoded_eof)
{
while((dst + disp_length) < decoded_eof)
{
while(lzssFlags[1] == 0)
{
lzssFlags[0] |= *encoded_src++ << lzssFlags[1];
lzssFlags[1] += 8;
}
tempAlFlag = 0;
tempEncFlag = lzssFlags[0];
lzssFlags[0] >>= 1;
lzssFlags[1] -= 1;
if( !(tempEncFlag & 0x01) )
{
// DECODE WITH ALLZ FLAG 2
tempAlFlag = allzFlag2;
while(1)
{
while(lzssFlags[1] == 0)
{
lzssFlags[0] |= *encoded_src++ << lzssFlags[1];
lzssFlags[1] += 8;
}
tempEncFlag = lzssFlags[0];
lzssFlags[0] >>= 1;
lzssFlags[1] -= 1;
if ( !(tempEncFlag & 0x01) ) break;
++tempAlFlag;
}
local_var1 = ALLZ_AnalyzeBlock((s32*)lzssFlags, tempAlFlag, &encoded_src);
if(tempAlFlag > allzFlag2)
{
local_var1 += ((1 << (tempAlFlag - allzFlag2)) - 1) << allzFlag2;
}
++local_var1;
if((dst + disp_length + local_var1) >= decoded_eof) // finish copying bytes and return
{
if(disp_length)
{
duplicates = dst - disp_offset;
do {
*dst++ = *duplicates++;
} while(--disp_length != 0);
}
if(local_var1)
{
do {
*dst++ = *encoded_src++;
} while(--local_var1 != 0);
}
break; // while((dst + disp_length) < decoded_eof)
}
// DECODE WITH ALLZ FLAG 1
src_temp = encoded_src;
tempAlFlag = allzFlag1;
encoded_src += local_var1;
while(1)
{
while(lzssFlags[1] == 0)
{
lzssFlags[0] |= *encoded_src++ << lzssFlags[1];
lzssFlags[1] += 8;
}
tempEncFlag = lzssFlags[0];
lzssFlags[0] >>= 1;
lzssFlags[1] -= 1;
if ( !(tempEncFlag & 0x01) ) break;
++tempAlFlag;
}
local_var3 = ALLZ_AnalyzeBlock((s32*)lzssFlags, tempAlFlag, &encoded_src);
if(tempAlFlag > allzFlag1)
{
local_var3 += (((1 << (tempAlFlag - allzFlag1)) - 1) << allzFlag1);
}
// DECODE WITH ALAR FLAG 2
tempAlFlag = alarFlag2;
while(1)
{
while(lzssFlags[1] == 0)
{
lzssFlags[0] |= *encoded_src++ << lzssFlags[1];
lzssFlags[1] += 8;
}
tempEncFlag = lzssFlags[0];
lzssFlags[0] >>= 1;
lzssFlags[1] -= 1;
if ( !(tempEncFlag & 0x01) ) break;
++tempAlFlag;
}
local_var5 = ALLZ_AnalyzeBlock((s32*)lzssFlags, tempAlFlag, &encoded_src);
if(tempAlFlag > alarFlag2)
{
local_var5 += (((1 << (tempAlFlag - alarFlag2)) - 1) << alarFlag2);
}
// COPY OVER DECODED BYTES
if(disp_length)
{
duplicates = dst - disp_offset;
do {
*dst++ = *duplicates++;
} while(--disp_length != 0);
}
if(local_var1)
{
do {
*dst++ = *src_temp++;
} while(--local_var1 != 0);
}
} else {
// DECODE WITH ALLZ FLAG 1
tempAlFlag = allzFlag1;
while(1)
{
while(lzssFlags[1] == 0)
{
lzssFlags[0] |= *encoded_src++ << lzssFlags[1];
lzssFlags[1] += 8;
}
tempEncFlag = lzssFlags[0];
lzssFlags[0] >>= 1;
lzssFlags[1] -= 1;
if ( !(tempEncFlag & 0x01) ) break;
++tempAlFlag;
}
local_var3 = ALLZ_AnalyzeBlock((s32*)lzssFlags, tempAlFlag, &encoded_src);
if(tempAlFlag > allzFlag1)
{
local_var3 += (((1 << (tempAlFlag - allzFlag1)) - 1) << allzFlag1);
}
// DECODE WITH ALAR FLAG 2
tempAlFlag = alarFlag2;
while(1)
{
while(lzssFlags[1] == 0)
{
lzssFlags[0] |= *encoded_src++ << lzssFlags[1];
lzssFlags[1] += 8;
}
tempEncFlag = lzssFlags[0];
lzssFlags[0] >>= 1;
lzssFlags[1] -= 1;
if( !(tempEncFlag & 0x01) ) break;
++tempAlFlag;
}
local_var5 = ALLZ_AnalyzeBlock((s32*)lzssFlags, tempAlFlag, &encoded_src);
if(tempAlFlag > alarFlag2)
{
local_var5 += (((1 << (tempAlFlag - alarFlag2)) - 1) << alarFlag2);
}
// COPY OVER DECODED BYTES
if(disp_length)
{
duplicates = dst - disp_offset;
do {
*dst++ = *duplicates++;
} while(--disp_length != 0);
}
}
disp_offset = local_var3 + 1;
disp_length = local_var5 + 3;
if(encoded_src > encoded_eof)
{
//printf("ERROR: decode():%d : encoded_src passed encoded_eof\n", __LINE__);
return 0;
}
}
if(disp_length)
{
duplicates = dst - disp_offset;
do {
*dst++ = *duplicates++;
} while (--disp_length != 0);
}
}
/* printf("DEBUG:\n"
" decoded_dst = 0x%08X\n"
" decoded_eof = 0x%08X\n\n", dst, decoded_eof); */
return (dst == decoded_eof) ? fullSize : 0;
}
s32 ALLZ_AnalyzeBlock(s32* encFlags, u8 alFlag, u8** ptr_encoded_data)
{
u32 tempFlag;
while(encFlags[1] < alFlag)
{
encFlags[0] |= *(*ptr_encoded_data)++ << encFlags[1];
encFlags[1] += 8;
}
tempFlag = encFlags[0];
encFlags[0] >>= alFlag;
encFlags[1] -= alFlag;
return (tempFlag & ((1 << alFlag) - 1));
}
s32 ALLZ_Setup_EncFlags(s32* encFlags, u8 alFlag, u8** ptr_encoded_data)
{
u32 tempAlFlag = alFlag;
u32 tempEncFlag;
s32 result;
while(1)
{
while(encFlags[1] == 0)
{
encFlags[0] |= *(*ptr_encoded_data)++ << encFlags[1];
encFlags[1] += 8;
}
tempEncFlag = encFlags[0];
encFlags[0] >>= 1;
encFlags[1] -= 1;
if( !(tempEncFlag & 0x01) ) break;
++tempAlFlag;
}
result = ALLZ_AnalyzeBlock(encFlags, tempAlFlag, ptr_encoded_data);
if( tempAlFlag > alFlag )
result += ((1 << (tempAlFlag - alFlag)) - 1) << alFlag;
return result;
}