-
Notifications
You must be signed in to change notification settings - Fork 0
/
6unpack.c
478 lines (418 loc) · 12.6 KB
/
6unpack.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
/*
6PACK - file compressor using FastLZ (lightning-fast compression library)
Copyright (C) 2007 Ariya Hidayat ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIXPACK_VERSION_MAJOR 0
#define SIXPACK_VERSION_MINOR 1
#define SIXPACK_VERSION_REVISION 0
#define SIXPACK_VERSION_STRING "0.1.0"
#include "fastlz.h"
#if defined(WIN32) || defined(__NT__) || defined(_WIN32) || defined(__WIN32__)
#if defined(__BORLANDC__) || defined(_MSC_VER)
#define inline __inline
#endif
#endif
/* magic identifier for 6pack file */
static unsigned char sixpack_magic[8] = {137, '6', 'P', 'K', 13, 10, 26, 10};
#define BLOCK_SIZE 65536
/* prototypes */
static inline unsigned long update_adler32(unsigned long checksum, const void *buf, int len);
void usage(void);
int detect_magic(FILE *f);
static inline unsigned long readU16(const unsigned char* ptr);
static inline unsigned long readU32(const unsigned char* ptr);
void read_chunk_header(FILE* f, int* id, int* options, unsigned long* size,
unsigned long* checksum, unsigned long* extra);
int unpack_file(const char* archive_file);
/* for Adler-32 checksum algorithm, see RFC 1950 Section 8.2 */
#define ADLER32_BASE 65521
static inline unsigned long update_adler32(unsigned long checksum, const void *buf, int len)
{
const unsigned char* ptr = (const unsigned char*)buf;
unsigned long s1 = checksum & 0xffff;
unsigned long s2 = (checksum >> 16) & 0xffff;
while(len>0)
{
unsigned k = len < 5552 ? len : 5552;
len -= k;
while(k >= 8)
{
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
s1 += *ptr++; s2 += s1;
k -= 8;
}
while(k-- > 0)
{
s1 += *ptr++; s2 += s1;
}
s1 = s1 % ADLER32_BASE;
s2 = s2 % ADLER32_BASE;
}
return (s2 << 16) + s1;
}
void usage(void)
{
printf("6unpack: uncompress 6pack archive\n");
printf("Copyright (C) 2007 Ariya Hidayat ([email protected])\n");
printf("\n");
printf("Usage: 6unpack archive-file\n");
printf("\n");
}
/* return non-zero if magic sequence is detected */
/* warning: reset the read pointer to the beginning of the file */
int detect_magic(FILE *f)
{
unsigned char buffer[8];
size_t bytes_read;
int c;
fseek(f, SEEK_SET, 0);
bytes_read = fread(buffer, 1, 8, f);
fseek(f, SEEK_SET, 0);
if(bytes_read < 8)
return 0;
for(c = 0; c < 8; c++)
if(buffer[c] != sixpack_magic[c])
return 0;
return -1;
}
static inline unsigned long readU16( const unsigned char* ptr )
{
return ptr[0]+(ptr[1]<<8);
}
static inline unsigned long readU32( const unsigned char* ptr )
{
return ptr[0]+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24);
}
void read_chunk_header(FILE* f, int* id, int* options, unsigned long* size,
unsigned long* checksum, unsigned long* extra)
{
unsigned char buffer[16];
fread(buffer, 1, 16, f);
*id = readU16(buffer) & 0xffff;
*options = readU16(buffer+2) & 0xffff;
*size = readU32(buffer+4) & 0xffffffff;
*checksum = readU32(buffer+8) & 0xffffffff;
*extra = readU32(buffer+12) & 0xffffffff;
}
int unpack_file(const char* input_file)
{
FILE* in;
unsigned long fsize;
int c;
unsigned long percent;
unsigned char progress[20];
int chunk_id;
int chunk_options;
unsigned long chunk_size;
unsigned long chunk_checksum;
unsigned long chunk_extra;
unsigned char buffer[BLOCK_SIZE];
unsigned long checksum;
unsigned long decompressed_size;
unsigned long total_extracted;
int name_length;
char* output_file;
FILE* f;
unsigned char* compressed_buffer;
unsigned char* decompressed_buffer;
unsigned long compressed_bufsize;
unsigned long decompressed_bufsize;
/* sanity check */
in = fopen(input_file, "rb");
if(!in)
{
printf("Error: could not open %s\n", input_file);
return -1;
}
/* find size of the file */
fseek(in, 0, SEEK_END);
fsize = ftell(in);
fseek(in, 0, SEEK_SET);
/* not a 6pack archive? */
if(!detect_magic(in))
{
fclose(in);
printf("Error: file %s is not a 6pack archive!\n", input_file);
return -1;
}
printf("Archive: %s", input_file);
/* position of first chunk */
fseek(in, 8, SEEK_SET);
/* initialize */
output_file = 0;
f = 0;
total_extracted = 0;
decompressed_size = 0;
percent = 0;
compressed_buffer = 0;
decompressed_buffer = 0;
compressed_bufsize = 0;
decompressed_bufsize = 0;
/* main loop */
for(;;)
{
/* end of file? */
size_t pos = ftell(in);
if(pos >= fsize)
break;
read_chunk_header(in, &chunk_id, &chunk_options,
&chunk_size, &chunk_checksum, &chunk_extra);
if((chunk_id == 1) && (chunk_size > 10) && (chunk_size < BLOCK_SIZE))
{
/* close current file, if any */
printf("\n");
free(output_file);
output_file = 0;
if(f)
fclose(f);
/* file entry */
fread(buffer, 1, chunk_size, in);
checksum = update_adler32(1L, buffer, chunk_size);
if(checksum != chunk_checksum)
{
free(output_file);
output_file = 0;
fclose(in);
printf("\nError: checksum mismatch!\n");
printf("Got %08lX Expecting %08lX\n", checksum, chunk_checksum);
return -1;
}
decompressed_size = readU32(buffer);
total_extracted = 0;
percent = 0;
/* get file to extract */
name_length = (int)readU16(buffer+8);
if(name_length > (int)chunk_size - 10)
name_length = chunk_size - 10;
output_file = (char*)malloc(name_length+1);
memset(output_file, 0, name_length+1);
for(c = 0; c < name_length; c++)
output_file[c] = buffer[10+c];
/* check if already exists */
f = fopen(output_file, "rb");
if(f)
{
fclose(f);
printf("File %s already exists. Skipped.\n", output_file);
free(output_file);
output_file = 0;
f = 0;
}
else
{
/* create the file */
f = fopen(output_file, "wb");
if(!f)
{
printf("Can't create file %s. Skipped.\n", output_file);
free(output_file);
output_file = 0;
f = 0;
}
else
{
/* for progress status */
printf("\n");
memset(progress, ' ', 20);
if(strlen(output_file) < 16)
for(c = 0; c < (int)strlen(output_file); c++)
progress[c] = output_file[c];
else
{
for(c = 0; c < 13; c++)
progress[c] = output_file[c];
progress[13] = '.';
progress[14] = '.';
progress[15] = ' ';
}
progress[16] = '[';
progress[17] = 0;
printf("%s", progress);
for(c = 0; c < 50; c++)
printf(".");
printf("]\r");
printf("%s", progress);
}
}
}
if((chunk_id == 17) && f && output_file && decompressed_size)
{
unsigned long remaining;
/* uncompressed */
switch(chunk_options)
{
/* stored, simply copy to output */
case 0:
/* read one block at at time, write and update checksum */
total_extracted += chunk_size;
remaining = chunk_size;
checksum = 1L;
for(;;)
{
unsigned long r = (BLOCK_SIZE < remaining) ? BLOCK_SIZE: remaining;
size_t bytes_read = fread(buffer, 1, r, in);
if(bytes_read == 0)
break;
fwrite(buffer, 1, bytes_read, f);
checksum = update_adler32(checksum, buffer, bytes_read);
remaining -= bytes_read;
}
/* verify everything is written correctly */
if(checksum != chunk_checksum)
{
fclose(f);
f = 0;
free(output_file);
output_file = 0;
printf("\nError: checksum mismatch. Aborted.\n");
printf("Got %08lX Expecting %08lX\n", checksum, chunk_checksum);
}
break;
/* compressed using FastLZ */
case 1:
/* enlarge input buffer if necessary */
if(chunk_size > compressed_bufsize)
{
compressed_bufsize = chunk_size;
free(compressed_buffer);
compressed_buffer = (unsigned char*)malloc(compressed_bufsize);
}
/* enlarge output buffer if necessary */
if(chunk_extra > decompressed_bufsize)
{
decompressed_bufsize = chunk_extra;
free(decompressed_buffer);
decompressed_buffer = (unsigned char*)malloc(decompressed_bufsize);
}
/* read and check checksum */
fread(compressed_buffer, 1, chunk_size, in);
checksum = update_adler32(1L, compressed_buffer, chunk_size);
total_extracted += chunk_extra;
/* verify that the chunk data is correct */
if(checksum != chunk_checksum)
{
fclose(f);
f = 0;
free(output_file);
output_file = 0;
printf("\nError: checksum mismatch. Skipped.\n");
printf("Got %08lX Expecting %08lX\n", checksum, chunk_checksum);
}
else
{
/* decompress and verify */
remaining = fastlz_decompress(compressed_buffer, chunk_size, decompressed_buffer, chunk_extra);
if(remaining != chunk_extra)
{
fclose(f);
f = 0;
free(output_file);
output_file = 0;
printf("\nError: decompression failed. Skipped.\n");
}
else
fwrite(decompressed_buffer, 1, chunk_extra, f);
}
break;
default:
printf("\nError: unknown compression method (%d)\n", chunk_options);
fclose(f);
f = 0;
free(output_file);
output_file = 0;
break;
}
/* for progress, if everything is fine */
if(f)
{
int last_percent = (int)percent;
if(decompressed_size < (1<<24))
percent = total_extracted * 100 / decompressed_size;
else
percent = total_extracted / 256 * 100 / (decompressed_size >>8);
percent >>= 1;
while(last_percent < (int)percent)
{
printf("#");
last_percent++;
}
}
}
/* position of next chunk */
fseek(in, pos + 16 + chunk_size, SEEK_SET);
}
printf("\n\n");
/* free allocated stuff */
free(compressed_buffer);
free(decompressed_buffer);
free(output_file);
/* close working files */
if(f)
fclose(f);
fclose(in);
/* so far so good */
return 0;
}
int main(int argc, char** argv)
{
int i;
const char* archive_file;
/* show help with no argument at all*/
if(argc == 1)
{
usage();
return 0;
}
/* check for help on usage */
for(i = 1; i <= argc; i++)
if(argv[i])
if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
{
usage();
return 0;
}
/* check for version information */
for(i = 1; i <= argc; i++)
if(argv[i])
if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version"))
{
printf("6unpack: high-speed file compression tool\n");
printf("Version %s (using FastLZ %s)\n",
SIXPACK_VERSION_STRING, FASTLZ_VERSION_STRING);
printf("Copyright (C) 2007 Ariya Hidayat ([email protected])\n");
printf("\n");
return 0;
}
/* needs at least two arguments */
if(argc <= 1)
{
usage();
return 0;
}
archive_file = argv[1];
return unpack_file(archive_file);
}