-
Notifications
You must be signed in to change notification settings - Fork 2
/
unarc.c
744 lines (622 loc) · 16.3 KB
/
unarc.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/**
* @file unarc.c
* ARC/SDA (C64/C128) archive extractor
* @author Chris Smeets
* @author Marko Mäkelä (marko.makela at iki.fi)
*/
/*
** Derived from Chris Smeets' MS-DOS utility a2l that converts Commodore
** ARK, ARC and SDA files to LZH using LHARC 1.12. Optimized for speed,
** converted to standard C and adapted to the cbmconvert package by
** Marko Mäkelä.
**
** Original version: a2l.c March 1st, 1990 Chris Smeets
** Unix port: unarc.c August 28th, 1993 Marko Mäkelä
** Restructured for cbmconvert 2.0 and 2.1 by Marko Mäkelä
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <setjmp.h>
#include "input.h"
/** I/O status. 0=ok, or EOF */
static int Status;
/** Current offset from ARC or SDA file's beginning */
static long FilePos;
/** Current archive */
static FILE* fp;
/** Bit Buffer */
static unsigned int BitBuf;
/** checksum */
static unsigned int crc;
/** used in checksum calculation */
static unsigned char crc2;
/** Huffman codes */
static unsigned long hc[256];
/** Lengths of huffman codes */
static unsigned char hl[256];
/** Character associated with Huffman code */
static unsigned char hv[256];
/** Number of Huffman codes */
static int hcount;
/** Run-Length control character */
static unsigned int ctrl;
/** C64 Archive entry header */
struct entry
{
/** Version number, must be 1 or 2 */
unsigned char version;
/** 0=store, 1=pack, 2=squeeze, 3=crunch,
4=squeeze+pack, 5=crunch in one pass */
unsigned char mode;
/** Checksum */
unsigned int check;
/** Original size. Only three bytes are stored */
size_t size;
/** Compressed size in CBM disk blocks */
unsigned int blocks;
/** File type. P,S,U or R */
unsigned char type;
/** Filename length */
unsigned char fnlen;
/** Filename. Only fnlen bytes are stored */
unsigned char name[17];
/** Record length if relative file */
unsigned char rl;
/** Date archive was created. Same format as in MS-DOS directories */
unsigned int date;
};
/** Current C64 Archive entry header */
struct entry entry;
/** Lempel Zev compression string table entry */
struct lz
{
unsigned int prefix; /**< Prefix code */
byte_t ext; /**< Extension character */
};
/** Lempel Zev compression string table */
struct lz lztab[4096];
/** Lempel Zev stack handling error codes */
enum LZStackErrorType
{
PushError = 1, /**< Stack overflow */
PopError /**< Stack underflow (out of data) */
};
/** Lempel Zev exception handling structure */
static jmp_buf LZStackError;
/** Lempel Zev stack */
static byte_t stack[512];
/** Set to 0 to reset un-crunch */
static int State = 0;
/** Lempel Zev stack pointer */
static unsigned lzstack = 0;
/** Current code size */
static int cdlen;
/** Last received code */
static unsigned code;
/** Bump cdlen when code reaches this value */
static int wtcl;
/** Copy of wtcl */
static int wttcl;
/** Shell Sort algorithm
* from "C Programmer's Library" by Purdum, Leslie and Stegemoller
*/
static void
ssort (void)
{
size_t m;
size_t h,i,j,k;
m = sizeof hl;
while (m >>= 1) {
k = (sizeof hl) - m;
j = 1;
do {
i = j;
do {
h = i + m;
if (hl[h - 1] > hl[i - 1]) {
unsigned long t;
unsigned char u;
t = hc[i - 1], hc[i - 1] = hc[h - 1], hc[h - 1] = t;
u = hv[i - 1], hv[i - 1] = hv[h - 1], hv[h - 1] = u;
u = hl[i - 1], hl[i - 1] = hl[h - 1], hl[h - 1] = u;
i -= m;
}
else
break;
} while (i >= 1);
j += 1;
} while(j <= k);
}
}
/** Receive a byte (eight bits) from the input
* @return the received byte
*/
static byte_t
GetByte (void)
{
if (Status == EOF)
return 0;
if (feof (fp) || ferror (fp)) {
Status = EOF;
return 0;
}
else
Status = 0;
return (unsigned char) (fgetc (fp) & 0xff);
}
/** Receive a word (sixteen bits) from the input
* @return the received word
*/
static word_t
GetWord (void)
{
word_t u = 0;
if (Status == EOF)
return 0;
if (feof (fp) || ferror (fp)) {
Status = EOF;
return 0;
}
else {
Status = 0;
}
u = (word_t) fgetc (fp) & 0xff;
u |= ((word_t) fgetc (fp) & 0xff) << 8;
return u;
}
/** Receive a three-byte integer (twenty-four bits) from the input
* @return the received integer
*/
static tbyte_t
GetThree (void)
{
tbyte_t u = 0;
if (Status == EOF || feof (fp) || ferror (fp)) {
Status = EOF;
return 0;
}
else
Status = 0;
u = (tbyte_t) (fgetc (fp) & 0xff);
u |= ((tbyte_t) fgetc (fp) & 0xff) << 8;
u |= ((tbyte_t) fgetc (fp) & 0xff) << 16;
return u;
}
/** Receive a bit from the input
* @return the received bit
*/
static unsigned
GetBit (void)
{
unsigned result = BitBuf >>= 1;
if (result == 1)
return 1 & (BitBuf = GetByte() | 0x0100U);
else
return 1 & result;
}
/** Fetch a Huffman code and convert it to what it represents
* @return the converted code
*/
static byte_t
Huffin (void)
{
unsigned long hcode = 0;
unsigned long mask = 1;
int size = 1;
int now;
now = hcount; /* First non-zero Huffman code */
do {
if (GetBit ())
hcode |= mask;
while( hl[now] == size) {
if (hc[now] == hcode)
return hv[now];
if (--now < 0) { /* Error in decode table */
Status = EOF;
return 0;
}
}
size++;
mask = mask << 1;
} while (size < 24);
Status = EOF; /* Error. Huffman code too big */
return 0;
}
/** Fetch ARC64 header.
* @return true if header is ok.
*/
static bool
GetHeader (void)
{
unsigned int w, i;
const char LegalTypes[] = "SPUR";
unsigned long mask;
if (feof(fp) || ferror(fp))
return false;
else
Status = 0;
BitBuf = 2; /* Clear Bit buffer */
crc = 0; /* checksum */
crc2 = 0; /* Used in checksum calculation */
State = 0; /* LZW state */
ctrl = 254; /* Run-Length control character */
entry.version = GetByte();
entry.mode = GetByte();
entry.check = GetWord();
entry.size = GetThree();
entry.blocks = GetWord();
entry.type = GetByte();
entry.fnlen = GetByte();
/* Check for invalid header, If invalid, then we've input past the end */
/* Possibly due to XMODEM padding or whatever */
if (entry.fnlen > 16)
return 0;
for (w=0; w < entry.fnlen; w++)
entry.name[w] = GetByte();
entry.name[entry.fnlen] = 0;
if (entry.version > 1) {
entry.rl = GetByte();
entry.date= GetWord();
}
if (Status == EOF)
return false;
if (entry.version == 0 || entry.version > 2)
return false;
if (entry.version == 1) { /* If ARC64 version 1.xx */
if (entry.mode > 2) /* Only store, pack, squeeze */
return false;
}
if (entry.mode == 1) /* If packed get control char */
ctrl = GetByte(); /* V2 always uses 0xfe V1 varies */
if (entry.mode > 5)
return false;
if ((entry.mode == 2) || (entry.mode == 4)) { /* if squeezed or squashed */
hcount = 255; /* Will be first code */
for (w=0; w<256; w++) { /* Fetch Huffman codes */
hv[w] = (unsigned char) w;
hl[w]=0;
mask = 1;
for (i=1; i<6; i++) {
if (GetBit())
hl[w] |= (unsigned char) mask;
mask <<= 1;
}
if (hl[w] > 24)
return false; /* Code too big */
hc[w] = 0;
if (hl[w]) {
i = 0;
mask = 1;
while (i<hl[w]) {
if (GetBit())
hc[w] |= mask;
i++;
mask <<= 1;
}
}
else
hcount--;
}
ssort ();
}
return !!strchr (LegalTypes, entry.type);
}
/** Get start of data. Ignores SDA header.
* @return the starting position of useful data within the file
* (normally 0), or -1 if not an archive
*/
static long
GetStartPos (void)
{
int c; /* Temp */
int cpu; /* C64 or C128 if SDA */
word_t linenum; /* Sys line number */
word_t skip; /* Size of SDA header in bytes */
fseek(fp, 0, SEEK_SET); /* Goto start of file */
Status = 0;
if ( (c=GetByte()) == 2) /* Probably type 2 archive */
return 0; /* Data starts at offset 0 */
if (c != 1) /* IBM archive, or not an archive at all */
return -1;
/* Check if its an SDA */
GetByte(); /* Skip to line number (which is # of header blocks) */
GetWord();
linenum = GetWord();
c = GetByte();
if (c != 0x9e) /* Must be BASIC SYS token */
return 0; /* Else probably type 1 archive */
c = GetByte(); /* Get SYS address */
cpu = GetByte(); /* '2' for C64, '7' for C128 */
skip = (linenum-6)*254; /* True except for SDA232.128 */
if ( (linenum==15) && (cpu=='7') ) /* handle the special case */
skip -= 1;
return skip;
}
/*
* Un-Crunch a byte
*
* This is pretty straight forward if you have Terry Welch's article
* "A Technique for High Performance Data Compression" from IEEE Computer
* June 1984
*
* This implemention reserves code 256 to indicate the end of a crunched
* file, and code 257 was reserved for future considerations. Codes grow
* up to 12 bits and then stay there. There is no reset of the string
* table.
*/
/** Push a byte to the Lempel Zev stack
* @param c the byte to be pushed
*/
static void
push (byte_t c)
{
if (lzstack >= sizeof stack)
longjmp (LZStackError, PushError);
else
stack[lzstack++] = c;
}
/** Pop a byte from the Lempel Zev stack
* @return the popped byte
*/
static byte_t
pop (void)
{
if (!lzstack)
longjmp (LZStackError, PopError);
else
return stack[--lzstack];
}
/** Fetch LZ code
* @return the fetched code
*/
static unsigned int getcode (void)
{
register int i;
long blocks;
code = 0;
i = cdlen;
while(i--)
code = (code << 1) | GetBit ();
/* Special case of 1 pass crunch. Checksum and size are at the end */
if ((code == 256) && (entry.mode == 5)) {
i = 16;
entry.check = 0;
while (i--)
entry.check = (entry.check << 1) | GetBit ();
i = 24;
entry.size = 0;
while (i--)
entry.size = (entry.size << 1) | GetBit ();
i = 16;
while (i--) /* This was never implemented */
GetBit ();
blocks = ftell(fp)-FilePos;
entry.blocks = (unsigned) (blocks / 254);
if (blocks % 254)
entry.blocks++;
}
/* Get ready for next time */
if ((cdlen < 12)) {
if (!(--wttcl)) {
wtcl = wtcl << 1;
cdlen++;
wttcl = wtcl;
}
}
return code;
}
/** Un-crunch a byte
* @return the uncrunched byte
*/
static byte_t
unc (void)
{
static unsigned oldcode, incode;
static byte_t kay;
static unsigned omega;
static unsigned char finchar;
static unsigned ncodes; /* Current # of codes in table */
switch (State) {
case 0: /* First time. Reset. */
lzstack = 0;
ncodes = 258; /* 2 reserved codes */
wtcl = 256; /* 256 Bump code size when we get here */
wttcl = 254; /* 1st time only 254 due to resvd codes */
cdlen = 9; /* Start with 9 bit codes */
oldcode = getcode();
if (oldcode == 256) { /* Code 256 is EOF for this entry */
Status = EOF; /* (ie. a zero length file) */
return 0;
}
kay = (byte_t) oldcode;
finchar = kay;
State = 1;
return kay;
case 1:
incode = getcode();
if (incode == 256) {
State = 0;
Status = EOF;
return 0;
}
if (incode >= ncodes) { /* Undefined code, special case */
kay = finchar;
push(kay);
code = oldcode;
omega = oldcode;
incode = ncodes;
}
while ( code > 255 ) { /* Decompose string */
push(lztab[code].ext);
code = lztab[code].prefix;
}
finchar = kay = (byte_t) code;
State = 2;
return kay;
case 2:
if (!lzstack) { /* Empty stack */
omega = oldcode;
if (ncodes < sizeof lztab / sizeof *lztab) {
lztab[ncodes].prefix = omega;
lztab[ncodes].ext = kay;
ncodes++;
}
oldcode = incode;
State = 1;
return unc();
}
else
return pop();
}
Status = EOF;
return 0;
}
/** Update the checksum
* @param c the data to be added to the checksum
*/
static void
UpdateChecksum (byte_t c)
{
c &= 0xff;
if (entry.version == 1) /* Simple checksum for version 1 */
crc += c;
else
crc += (c ^ (++crc2)); /* A slightly better checksum for version 2 */
}
/** Unpack a byte
* @return the unpacked byte
*/
static byte_t
UnPack (void)
{
switch (entry.mode) {
case 0: /* Stored */
case 1: /* Packed (Run-Length) */
return GetByte();
case 2: /* Squeezed (Huffman only) */
case 4: /* Squashed (Huffman + Run-Length) */
return Huffin();
case 3: /* Crunched */
case 5: /* Crunched in one pass */
return unc();
default: /* Otherwise ERROR */
Status = EOF;
return 0;
}
}
/** Read and convert an ARC/SDA archive
* @param file the file input stream
* @param filename host system name of the file
* @param writeCallback function for writing the contained files
* @param log Call-back function for diagnostic output
* @return status of the operation
*/
enum RdStatus
ReadARC (FILE* file,
const char* filename,
write_file_t writeCallback,
log_t log)
{
(void) filename; /* unused */
fp = file;
switch (setjmp (LZStackError)) {
case PopError:
(*log) (Errors, 0, "Lempel Zev stack underflow");
return RdFail;
case PushError:
(*log) (Errors, 0, "Lempel Zev stack overflow");
return RdFail;
}
{
long temp;
if ((temp = GetStartPos()) < 0) {
(*log) (Errors, 0, "Not a Commodore ARC or SDA.");
return RdFail;
}
else if (fseek (fp, temp, SEEK_SET)) {
(*log) (Errors, 0, "fseek: %s", strerror(errno));
return RdFail;
}
}
FilePos = ftell (fp);
while (GetHeader()) {
byte_t* buffer;
byte_t* buf;
struct Filename name;
enum WrStatus wrStatus;
size_t length = entry.size;
if (entry.mode == 5) /* If 1 pass crunch size is unknown */
length = 65536; /* 64kB should be enough for everyone */
if (!(buf = buffer = malloc (length))) {
(*log) (Errors, 0, "Out of memory.");
return RdFail;
}
while (buf < &buffer[length]) {
byte_t c = UnPack ();
if (Status == EOF)
break;
/* If Run Length is needed */
if (entry.mode != 0 && entry.mode != 2 && c == ctrl) {
int count = UnPack ();
c = UnPack ();
if (Status == EOF)
break;
if (count == 0)
count = entry.version == 1 ? 255 : 256;
while (--count)
UpdateChecksum (*buf++ = c);
}
UpdateChecksum (*buf++ = c);
}
/* Set up the file name information */
{
unsigned i;
for (i = 0; i < entry.fnlen && i < sizeof name.name; i++)
name.name[i] = entry.name[i];
/* pad the file name with shifted spaces */
while (i < sizeof name.name)
name.name[i++] = 0xA0;
switch (entry.type) {
case 'S':
name.type = SEQ;
break;
case 'P':
name.type = PRG;
break;
case 'U':
name.type = USR;
break;
case 'R':
name.type = REL;
name.recordLength = entry.rl;
break;
default:
name.type = 0;
(*log) (Errors, &name, "Unknown type, defaulting to DEL");
name.type = DEL;
break;
}
}
if ((crc ^ entry.check) & 0xffff)
(*log) (Errors, &name, "Checksum error!");
wrStatus = (*writeCallback) (&name, buffer, (size_t) (buf - buffer));
free (buffer);
switch (wrStatus) {
case WrOK:
break;
case WrNoSpace:
return RdNoSpace;
case WrFail:
case WrFileExists:
return RdFail;
}
FilePos += (long)entry.blocks * 254;
if (fseek (fp, FilePos, SEEK_SET)) {
(*log) (Errors, 0, "fseek: %s", strerror (errno));
return RdFail;
}
}
return RdOK;
}