-
Notifications
You must be signed in to change notification settings - Fork 11
/
extract-adf.c
2740 lines (2517 loc) · 100 KB
/
extract-adf.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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* extract-adf.c
*
* (C)2008 Michael Steil, http://www.pagetable.com/
* Do whatever you want with it, but please give credit.
*
* (C)2011-2019 Sigurbjorn B. Larusson, http://www.dot1q.org/
*
*/
// Define if you have ZLIB support, don't forget to compile with -lz
// Comment out if zlib support is not available (support for adz will not work)
#define _HAVE_ZLIB
#include <libc.h>
#include <sys/_endian.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <inttypes.h>
#include <ctype.h>
#ifdef _HAVE_ZLIB
#include <zlib.h>
#endif
#include <assert.h>
#include <utime.h>
// These are defaults
#define SECTORS 1760
#define FIRST_SECTOR 0
// These are hard coded, no way to set them
#define T_HEADER 2
#define T_DATA 8
#define T_LIST 16
// DMS statics
#define DMS_NOZERO 1
#define DMS_ENCRYPT 2
#define DMS_APPENDS 4
#define DMS_BANNER 8
#define DMS_HIGHDENSITY 16
#define DMS_PC 32
#define DMS_DEVICEFIX 64
#define DMS_FILEIDBIZ 256
// Size of zlib chunks
#define CHUNK 0x4000
// Maximum number of sectors
#define MAX_SECTORS 3520
// Set this to 1 (or anything higher) if you want debugging information printed out, this can now be set at the command line
#define DEBUG 0
// Maximum AmigaDOS filename length
#define MAX_AMIGADOS_FILENAME_LENGTH 32
// Maximum filename length generated by the program and on the input, this is a reasonably large assumption
#define MAX_FILENAME_LENGTH 256
// Maximum path depth, I'm not sure there is an explicit limit, but the max path length is 255 characters, so it can
// never go over that (or even reach it), we'll use 256 just to be safe
#define MAX_PATH_DEPTH 256
typedef unsigned int uint32_t;
// Stucture for the raw sector
struct sector_raw {
uint8_t byte[512];
};
// Tthe blockheader
struct blkhdr {
uint32_t type;
uint32_t header_key;
uint32_t seq_num;
uint32_t data_size;
uint32_t next_data;
uint32_t chksum;
};
// The fileheader
struct fileheader {
struct blkhdr hdr;
uint8_t misc[288];
uint32_t unused1;
uint16_t uid;
uint16_t gid;
uint32_t protect;
uint32_t byte_size;
uint8_t comm_len;
uint8_t comment[79];
uint8_t unused2[12];
int32_t days;
int32_t mins;
int32_t ticks;
uint8_t name_len;
char filename[30];
uint8_t unused3;
uint32_t unused4;
uint32_t real_entry;
uint32_t next_link;
uint32_t unused5[5];
uint32_t hash_chain;
uint32_t parent;
uint32_t extension;
uint32_t sec_type;
};
// The dataheader
struct dataheader {
struct blkhdr hdr;
uint8_t data[488];
};
// The sector
union sector {
struct blkhdr hdr;
struct fileheader fh;
struct dataheader dh;
};
// Added sibbi 2019, DMS packing variables and tables along with DMS unpacking functions
// Copy paste from code written by David Tritscher, with slight formatting changes
#define BUFFERSIZE 48000
unsigned char pack_buffer[BUFFERSIZE];
unsigned char unpack_buffer[BUFFERSIZE];
unsigned char info_header[4];
unsigned char archive_header[52];
unsigned char track_header[20];
unsigned char quick_buffer[256];
unsigned char medium_buffer[16384];
unsigned char deep_buffer[16384];
unsigned short deep_weights[628];
unsigned short deep_symbols[628];
unsigned short deep_hash[942];
unsigned char heavy_buffer[8192];
unsigned short heavy_literal_table[5120];
unsigned short heavy_offset_table[320];
unsigned char heavy_literal_len[512];
unsigned char heavy_offset_len[32];
unsigned int quick_local;
unsigned int medium_local;
unsigned int deep_local;
unsigned int heavy_local;
unsigned int heavy_last_offset;
static const unsigned short CRCTable[256]=
{
0x0000,0xC0C1,0xC181,0x0140,0xC301,0x03C0,0x0280,0xC241,
0xC601,0x06C0,0x0780,0xC741,0x0500,0xC5C1,0xC481,0x0440,
0xCC01,0x0CC0,0x0D80,0xCD41,0x0F00,0xCFC1,0xCE81,0x0E40,
0x0A00,0xCAC1,0xCB81,0x0B40,0xC901,0x09C0,0x0880,0xC841,
0xD801,0x18C0,0x1980,0xD941,0x1B00,0xDBC1,0xDA81,0x1A40,
0x1E00,0xDEC1,0xDF81,0x1F40,0xDD01,0x1DC0,0x1C80,0xDC41,
0x1400,0xD4C1,0xD581,0x1540,0xD701,0x17C0,0x1680,0xD641,
0xD201,0x12C0,0x1380,0xD341,0x1100,0xD1C1,0xD081,0x1040,
0xF001,0x30C0,0x3180,0xF141,0x3300,0xF3C1,0xF281,0x3240,
0x3600,0xF6C1,0xF781,0x3740,0xF501,0x35C0,0x3480,0xF441,
0x3C00,0xFCC1,0xFD81,0x3D40,0xFF01,0x3FC0,0x3E80,0xFE41,
0xFA01,0x3AC0,0x3B80,0xFB41,0x3900,0xF9C1,0xF881,0x3840,
0x2800,0xE8C1,0xE981,0x2940,0xEB01,0x2BC0,0x2A80,0xEA41,
0xEE01,0x2EC0,0x2F80,0xEF41,0x2D00,0xEDC1,0xEC81,0x2C40,
0xE401,0x24C0,0x2580,0xE541,0x2700,0xE7C1,0xE681,0x2640,
0x2200,0xE2C1,0xE381,0x2340,0xE101,0x21C0,0x2080,0xE041,
0xA001,0x60C0,0x6180,0xA141,0x6300,0xA3C1,0xA281,0x6240,
0x6600,0xA6C1,0xA781,0x6740,0xA501,0x65C0,0x6480,0xA441,
0x6C00,0xACC1,0xAD81,0x6D40,0xAF01,0x6FC0,0x6E80,0xAE41,
0xAA01,0x6AC0,0x6B80,0xAB41,0x6900,0xA9C1,0xA881,0x6840,
0x7800,0xB8C1,0xB981,0x7940,0xBB01,0x7BC0,0x7A80,0xBA41,
0xBE01,0x7EC0,0x7F80,0xBF41,0x7D00,0xBDC1,0xBC81,0x7C40,
0xB401,0x74C0,0x7580,0xB541,0x7700,0xB7C1,0xB681,0x7640,
0x7200,0xB2C1,0xB381,0x7340,0xB101,0x71C0,0x7080,0xB041,
0x5000,0x90C1,0x9181,0x5140,0x9301,0x53C0,0x5280,0x9241,
0x9601,0x56C0,0x5780,0x9741,0x5500,0x95C1,0x9481,0x5440,
0x9C01,0x5CC0,0x5D80,0x9D41,0x5F00,0x9FC1,0x9E81,0x5E40,
0x5A00,0x9AC1,0x9B81,0x5B40,0x9901,0x59C0,0x5880,0x9841,
0x8801,0x48C0,0x4980,0x8941,0x4B00,0x8BC1,0x8A81,0x4A40,
0x4E00,0x8EC1,0x8F81,0x4F40,0x8D01,0x4DC0,0x4C80,0x8C41,
0x4400,0x84C1,0x8581,0x4540,0x8701,0x47C0,0x4680,0x8641,
0x8201,0x42C0,0x4380,0x8341,0x4100,0x81C1,0x8081,0x4040
};
/* --------------------------------------------------------------------- */
static const unsigned char table_one[256]=
{
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,
10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,
12,12,12,12,13,13,13,13,14,14,14,14,15,15,15,15,
16,16,16,16,17,17,17,17,18,18,18,18,19,19,19,19,
20,20,20,20,21,21,21,21,22,22,22,22,23,23,23,23,
24,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,
32,32,33,33,34,34,35,35,36,36,37,37,38,38,39,39,
40,40,41,41,42,42,43,43,44,44,45,45,46,46,47,47,
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63
};
static const unsigned char table_two[256]=
{
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
};
// Helper function to convert Amiga timestamp to unix timestamp
struct tm *amigatoepoch(unsigned int amigatime,struct tm *tm) {
// Time variable for epoch at 1978-01-01
time_t origstamp = 252460800;
// Add to the origstampto get epoch time
origstamp += amigatime;
// Make tm struct from timestamp
tm = localtime(&origstamp);
// Return the timestruct
return tm;
}
// Helper function to convert Amiga days, minutes, ticks to timestamp
struct utimbuf *amigadaystoutimbuf(uint32_t days, uint32_t minutes, uint32_t ticks,struct utimbuf *utim) {
// Time variable for epoch at 1978-01-01
time_t origstamp = 252460800;
// Div by zero check and fix for ticks
if(!ticks)
ticks=1;
// Add to the origstampto get epoch time
// Multiply days with 86400 seconds per day, minutes with 60 seconds, and divide the ticks by 50 (ticks per second)
origstamp += (time_t)(days*86400)+(minutes*60)+(ticks/50);
// Set struct mod and access times as the current timestamp
utim->actime = (time_t)origstamp;
utim->modtime = (time_t)origstamp;
// Return the utimstruct
return utim;
}
// DMS helper functions to calculate CRC, (C) 1998 David Tritscher
unsigned int mycrc(unsigned char *memory, unsigned int length)
{
register unsigned int temp = 0;
while(length--)
temp = CRCTable[(temp ^ *memory++) & 255] ^ ((temp >> 8) & 255);
return (temp & 65535);
}
// DMS helper functions to calculate CRC, (C) 1998 David Tritscher
unsigned int mysimplecrc(unsigned char *memory, uint32_t length)
{
register uint32_t temp = 0;
while(length--) temp += *memory++;
return (temp & 65535);
}
// DMS helper functions to depack sectors
// Store (unpacked) function, (C) 1998 David Tritscher
int crunch_store(unsigned char *source, unsigned char *source_end,
unsigned char *destination, unsigned char *destination_end,
unsigned int debug,FILE *debugfile)
{
while((destination < destination_end) && (source < source_end))
*destination++ = *source++;
// Print debug output if wanted
if(debug)
fprintf(debugfile,"\tstore: %s",((source != source_end) || (destination != destination_end)) ? "bad" : "good");
return((source != source_end) || (destination != destination_end));
}
// RLE crunch function, (C) 1998 David Tritscher
// Commented and modified by Sigurbjorn B. Larusson, >255 count didn't work
int crunch_rle(unsigned char *source, unsigned char *source_end,
unsigned char *destination, unsigned char *destination_end,
unsigned int debug, FILE *debugfile)
{
register unsigned char temp = 0;
register unsigned char rlechar = 0;
register unsigned int count = 0;
int tempcount = 0;
int rlebytes = 0;
int rlesaved = 0;
int totalbytes = 0;
int unpackedsize = 0;
int rletotalbytes = 0;
// Until we've completed reading all the destination bytes...
while((destination < destination_end) && (source < source_end)) {
// Read current pointed to of source into temp and then increment source
temp = *source++;
totalbytes++;
rletotalbytes++;
// Not RLE encoded, just copy bytes
if(temp != 144) {
*destination++ = temp;
} else {
// We've wasted a character here on rlebytes
rlebytes++;
// Count is in next seat
count = *source++;
// Another character here on rlebytes
rlebytes++;
totalbytes++;
rletotalbytes++;
// Count uses more than one byte?
if(count==255) {
// Next byte is rlechar
rlechar = *source++;
totalbytes++;
rletotalbytes++;
// Next two bytes are the counter
temp = *source++;
totalbytes++;
rletotalbytes++;
tempcount = (temp <<8);
count = tempcount;
temp = *source++;
totalbytes++;
rletotalbytes++;
count += temp;
rlebytes+=3;
} else if(count != 0) {
// Next byte is rlechar
rlechar = *source++;
totalbytes++;
rletotalbytes++;
}
// Count is 0 than this is a literal É character
if(count == 0) {
*destination++ = temp;
// And we've saved one rle byte
rlebytes--;
// Counter is not 0, procceed with unRLE
} else {
// While we have repeats, fill the destination array with the repeated byte
while(count > 0) {
*destination++ = rlechar;
count--;
rlesaved++;
rletotalbytes++;
}
rlesaved--;
}
}
} // End while
unpackedsize = totalbytes - rlebytes + rlesaved;
if(debug) {
fprintf(debugfile,"\tTotal bytes used on RLE: %u, total bytes saved by RLE: %u, Totalbytes read: %u Totalbytes processed: %u Unpacked size: %u\n",rlebytes,rlesaved,totalbytes,rletotalbytes,unpackedsize);
fprintf(debugfile,"\trunlength: %s\n",((source != source_end) || (destination != destination_end)) ? "bad" : "good");
}
return((source != source_end) || (destination != destination_end));
}
// Quick crunch function, (C) 1998 David Tritscher
int crunch_quick(unsigned char *source, unsigned char *source_end,
unsigned char *destination, unsigned char *destination_end,
unsigned int debug, FILE *debugfile,unsigned int no_clear_flag)
{
register unsigned int control = 0;
register int shift = 0;
int count, offset;
quick_local += 5; /* i have no idea why it adds 5 */
if(!no_clear_flag)
for(quick_local = count = 0; count < 256; count++)
quick_buffer[count] = 0;
while((destination < destination_end) && (source < source_end)) {
control <<= 9; /* all codes are at least 9 bits long */
if((shift += 9) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
if(control & 16777216) {
*destination++ = quick_buffer[quick_local++ & 255] = control >> 16;
} else {
control <<= 2; /* 2 extra bits for length */
if((shift += 2) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
count = ((control >> 24) & 3) + 2;
offset = quick_local - ((control >> 16) & 255) - 1;
while((destination < destination_end) && (count--))
*destination++ = quick_buffer[quick_local++ & 255] =
quick_buffer[offset++ & 255];
}
} /* while */
if(debug)
fprintf(debugfile,"\tquick: %s\n",((source > source_end) || (destination != destination_end)) ? "bad" : "good");
return((source > source_end) || (destination != destination_end));
}
// Medium crunch function, (C) 1998 David Tritscher
int crunch_medium(unsigned char *source, unsigned char *source_end,
unsigned char *destination, unsigned char *destination_end,
unsigned int debug, FILE *debugfile, unsigned int no_clear_flag)
{
register unsigned int control = 0;
register int shift = 0;
int count, offset, temp;
medium_local += 66; /* i have no idea why it adds 66 */
if(!no_clear_flag)
for(medium_local = count = 0; count < 16384; count++)
medium_buffer[count] = 0;
while((destination < destination_end) && (source < source_end)) {
control <<= 9; /* all codes are 9 bits long */
if((shift += 9) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
if((temp = (control >> 16) & 511) >= 256) {
*destination++ = medium_buffer[medium_local++ & 16383] = temp;
} else {
count = table_one[temp] + 3;
temp = table_two[temp];
control <<= temp;
if((shift += temp) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
temp = (control >> 16) & 255;
offset = table_one[temp] << 8;
temp = table_two[temp];
control <<= temp;
if((shift += temp) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
offset += (control >> 16) & 255;
offset = medium_local - offset - 1;
while((destination < destination_end) && (count--))
*destination++ = medium_buffer[medium_local++ & 16383] = medium_buffer[offset++ & 16383];
}
} /* while */
if(debug)
fprintf(debugfile,"\tmedium: %s\n",((source > source_end) || (destination != destination_end)) ? "bad" : "good");
return((source > source_end) || (destination != destination_end));
}
// Deep clear helper function (C) 1998 David Tritscher
void deep_clear(unsigned int debug, FILE *debugfile) {
unsigned short count, temp;
temp = 627;
for(count = 0; count < 314; count++) {
deep_weights[count] = 1;
deep_symbols[count] = temp;
deep_hash[temp] = count;
temp++;
}
temp = 0;
for(count = 314; count < 627; count++) {
deep_weights[count] = deep_weights[temp] + deep_weights[temp + 1];
deep_symbols[count] = temp;
deep_hash[temp] = deep_hash[temp + 1] = count;
temp += 2;
}
deep_weights[count] = 65535;
deep_hash[temp] = 0;
if(debug)
fprintf(debugfile," ...clear");
}
// Deep scale helper function (C) 1998 David Tritscher
void deep_scale(unsigned int debug, FILE *debugfile)
{
int symbol, swap, temp, weight;
temp = 0;
for(symbol = 0; symbol < 627; symbol++) {
if(deep_symbols[symbol] >= 627)
{
deep_weights[temp] = (deep_weights[symbol] + 1) >> 1;
deep_symbols[temp] = deep_symbols[symbol];
temp++;
}
}
temp = 0;
for(symbol = 314; symbol < 627; symbol++) {
weight = deep_weights[temp] + deep_weights[temp + 1];
for(swap = symbol; deep_weights[swap - 1] > weight; swap--)
{
deep_weights[swap] = deep_weights[swap - 1];
deep_symbols[swap] = deep_symbols[swap - 1];
}
deep_weights[swap] = weight;
deep_symbols[swap] = temp;
temp += 2;
}
for(symbol = 0; symbol < 627; symbol++) {
temp = deep_symbols[symbol];
deep_hash[temp] = symbol; if(temp < 627) deep_hash[temp + 1] = symbol;
}
if(debug)
fprintf(debugfile," ...scale");
}
// Deep crunch function, (C) 1998 David Tritscher
int crunch_deep(unsigned char *source, unsigned char *source_end,
unsigned char *destination, unsigned char *destination_end,
unsigned int debug, FILE *debugfile, unsigned int no_clear_flag)
{
register unsigned int control = 0;
register int shift = 0;
int count, offset, temp, symbol, swap, temp1, temp2;
deep_local += 60; /* i have no idea why it adds 60 */
if(!no_clear_flag) {
deep_clear(debug,debugfile);
for(deep_local = count = 0; count < 16384; count++)
deep_buffer[count] = 0;
}
while((destination < destination_end) && (source < source_end)) {
count = deep_symbols[626]; /* start from the root of the trie */
do {
if(!shift++) {
control += *source++ << 8;
control += *source++;
shift = -15;
}
control <<= 1;
count += (control >> 16) & 1;
} while((count = deep_symbols[count]) < 627);
if(deep_weights[626] == 32768) /* scale the trie if the weight gets too large */
deep_scale(debug,debugfile);
symbol = deep_hash[count];
do {
deep_weights[symbol]++; /* increase the weight of this node */
if(deep_weights[symbol + 1] < deep_weights[symbol]) {
temp1 = deep_weights[(swap = symbol)];
do {
swap++;
} while(deep_weights[swap + 1] < temp1);
deep_weights[symbol] = deep_weights[swap];
deep_weights[swap] = temp1;
temp1 = deep_symbols[symbol];
temp2 = deep_symbols[swap];
deep_symbols[swap] = temp1;
deep_symbols[symbol] = temp2;
deep_hash[temp1] = swap;
if(temp1 < 627)
deep_hash[temp1 + 1] = swap;
deep_hash[temp2] = symbol;
if(temp2 < 627)
deep_hash[temp2 + 1] = symbol;
symbol = swap;
}
} while((symbol = deep_hash[symbol])); /* repeat until we reach root */
if((count -= 627) < 256) {
*destination++ = deep_buffer[deep_local++ & 16383] = count;
} else {
count -= 253; /* length is always at least 3 characters */
control <<= 8;
if((shift += 8) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
temp = (control >> 16) & 255;
offset = table_one[temp] << 8;
temp = table_two[temp];
control <<= temp;
if((shift += temp) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
offset += (control >> 16) & 255;
offset = deep_local - offset - 1;
while((destination < destination_end) && (count--))
*destination++ = deep_buffer[deep_local++ & 16383] = deep_buffer[offset++ & 16383];
}
} /* while */
if(debug)
fprintf(debugfile,"\tdeep: %s\n",((source > source_end) || (destination != destination_end)) ? "bad" : "good");
return((source > source_end) || (destination != destination_end));
}
// Helper function for heavy crunch, (C) 1998 David Tritscher
int make_decode_table(int number_symbols, int table_size,
unsigned char *length, unsigned short *table,
unsigned int debug, FILE *debugfile)
{
register unsigned char bit_num = 0;
register int symbol;
unsigned int table_mask, bit_mask, pos, fill, next_symbol, leaf;
int abort = 0;
pos = 0;
fill = 0;
bit_mask = table_mask = 1 << table_size;
while((!abort) && (bit_num <= table_size)) {
for(symbol = 0; symbol < number_symbols; symbol++) {
if(length[symbol] == bit_num) {
if((pos += bit_mask) > table_mask) {
abort = 1;
break; /* we will overrun the table! abort! */
}
while(fill < pos)
table[fill++] = symbol;
}
}
bit_mask >>= 1;
bit_num++;
}
if((!abort) && (pos != table_mask)) {
for(; fill < table_mask; fill++)
table[fill] = 0; /* clear the rest of the table */
next_symbol = table_mask >> 1;
pos <<= 16;
table_mask <<= 16;
bit_mask = 32768;
while((!abort) && (bit_num <= 18)) {
for(symbol = 0; symbol < number_symbols; symbol++) {
if(length[symbol] == bit_num) {
leaf = pos >> 16;
for(fill = 0; fill < bit_num - table_size; fill++) {
if(table[leaf] == 0) {
table[(next_symbol << 1)] = 0;
table[(next_symbol << 1) + 1] = 0;
table[leaf] = next_symbol++;
}
leaf = table[leaf] << 1;
leaf += (pos >> (15 - fill)) & 1;
}
table[leaf] = symbol;
if((pos += bit_mask) > table_mask) {
abort = 1;
break; /* we will overrun the table! abort! */
}
}
}
bit_mask >>= 1;
bit_num++;
}
}
if(debug)
fprintf(debugfile,"\tcreate_table: %s\n",((pos != table_mask) || abort) ? "bad" : "good");
return((pos != table_mask) || abort);
}
// Heavy crunch function, (C) 1998 David Tritscher
int crunch_heavy(unsigned char *source, unsigned char *source_end,
unsigned char *destination, unsigned char *destination_end,
int flag, int special,
int debug, FILE *debugfile, int no_clear_flag)
{
register unsigned int control = 0;
register int shift = 0;
int count, offset, temp;
if(!no_clear_flag)
heavy_local = 0;
if(flag) {
flag = 0;
/* read the literal table */
if(!flag) {
for(count = 0; count < 512; count++)
heavy_literal_len[count] = 255;
control <<= 9; /* get number of literals */
if((shift += 9) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
if((offset = (control >> 16) & 511))
for(count = 0; count < offset; count++) {
control <<= 5; /* get the length of this literal */
if((shift += 5) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
temp = (control >> 16) & 31;
heavy_literal_len[count] = (temp ? temp : 255);
}
else {
control <<= 9; /* get the defined literal */
if((shift += 9) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
heavy_literal_len[(control >> 16) & 511] = 0;
}
flag = make_decode_table(512, 12, heavy_literal_len, heavy_literal_table,debug,debugfile);
}
/* read the offset table */
if(!flag) {
for(count = 0; count < 32; count++)
heavy_offset_len[count] = 255;
control <<= 5; /* get number of offsets */
if((shift += 5) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
if((offset = (control >> 16) & 31))
for(count = 0; count < offset; count++) {
control <<= 4; /* get the length of this offset */
if((shift += 4) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
temp = (control >> 16) & 15;
heavy_offset_len[count] = (temp ? temp : 255);
}
else {
control <<= 5; /* get the defined offset */
if((shift += 5) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
heavy_offset_len[(control >> 16) & 31] = 0;
}
temp = heavy_offset_len[special];
heavy_offset_len[special] = heavy_offset_len[31];
heavy_offset_len[31] = temp;
flag = make_decode_table(32, 8, heavy_offset_len, heavy_offset_table,debug,debugfile);
}
} /* if(flag) */
if(!flag) {
/* prefetch 12 bits for fast huffman decoding */
control <<= 12;
if((shift += 12) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
while((destination < destination_end) && (source < source_end)) {
/* get a literal */
if((count = heavy_literal_table[(control >> 16) & 4095]) >= 512) {
do /* literal is longer than 12 bits */ {
if(!shift++) {
control += *source++ << 8;
control += *source++;
shift = -15;
}
control <<= 1;
count = heavy_literal_table[((control >> 16) & 1) + (count << 1)];
} while(count >= 512);
temp = 12; /* skip the original 12 bits */
} else {
temp = heavy_literal_len[count];
}
control <<= temp;
if((shift += temp) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
/* less than 256 = literal, otherwise = length of string */
if(count < 256) {
*destination++ = heavy_buffer[heavy_local++ & 8191] = count;
} else { /* must have been a string */
count -= 253; /* length is always at least 3 characters */
if((offset = heavy_offset_table[(control >> 20) & 255]) >= 32) {
do { /* offset is longer than 8 bits */
if(!shift++) {
control += *source++ << 8;
control += *source++;
shift = -15;
}
control <<= 1;
offset = heavy_offset_table[((control >> 20) & 1) + (offset << 1)];
} while(offset >= 32);
temp = 8; /* skip the original 8 bits */
} else {
temp = heavy_offset_len[offset];
}
control <<= temp;
if((shift += temp) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
if(offset == 31) {
offset = heavy_last_offset;
} else {
if(offset) {
temp = offset - 1;
offset = ((control & 268369920) | 268435456) >> (28 - temp);
control <<= temp;
if((shift += temp) > 0) {
control += *source++ << (8 + shift);
control += *source++ << shift;
shift -= 16;
}
}
heavy_last_offset = offset;
}
offset = heavy_local - offset - 1;
while((destination < destination_end) && (count--))
*destination++ = heavy_buffer[heavy_local++ & 8191] = heavy_buffer[offset++ & 8191];
} /* if(string) */
}
} /* if(!flag) */
if(debug)
fprintf(debugfile,"\theavy: %s\n",((source > source_end) || (destination != destination_end) || flag) ? "bad" : "good");
return((source > source_end) || (destination != destination_end) || flag);
}
#define DATABYTES (sizeof(union sector)-sizeof(struct blkhdr))
// Print usage information, Added Sibbi 2011, added 2017, 2019
void usage(char *programname) {
fprintf(stderr,"Extract-ADF 4.0 Originally (C)2008 Michael Steil with many further additions by Sigurbjorn B. Larusson\n");
fprintf(stderr,"DMS extraction code (C) 1998 David Tritscher\n");
fprintf(stderr,"\nUsage: %s [-D] [-a] [-z] [-d] [-s <startsector>] [-e <endsector>] [-o <outputfilename>] <adf/adz/dmsfilename>\n",programname);
fprintf(stderr,"\n\t-a will force ADF extraction (if the filename ends in adf ADF will be assumed");
fprintf(stderr,"\n\t-z will force ADZ extraction (if the filename ends in adz or adf.gz ADZ will be assumed");
fprintf(stderr,"\n\t-d will force DMS extraction (if the filename ends in dms DMS format will be assumed");
fprintf(stderr,"\n\t-D will activate debugging output which will print very detailed information about everything that is going on");
fprintf(stderr,"\n\t-s along with an integer argument from 0 to 1760 (DD) or 3520 (HD), will set the starting sector of the extraction process");
fprintf(stderr,"\n\t-e along with an integer argument from 0 to 1760 (DD) or 3520 (HD), will set the end sector of the extraction process");
fprintf(stderr,"\n\t-o along with an outputfilename will redirect output (including debugging output) to a file instead of to the screen");
fprintf(stderr,"\n\tFinally the last argument is the ADF/ADZ or DMS filename to process");
fprintf(stderr,"\n\nThe defaults for start and end sector are 0 and 1760 respectively, this tool was originally"),
fprintf(stderr,"\ncreated to salvage lost data from kickstart disks (which contain the kickstart on sectors 0..512)");
fprintf(stderr,"\nin order to skip the sectors on kickstart disks which might contain non OFS data, set the start sector to 513\n");
fprintf(stderr,"\nTo use this tool on a HD floppy, the end sector needs to be 3520\n");
fprintf(stderr,"\nIf you get a Bus error it means that you specificed a non-existing end sector\n");
fprintf(stderr,"\nThis program does not support FFS floppies(!), it only supports OFS style Amiga Floppies\n");
fprintf(stderr,"\nHappy hunting!\n");
}
// Added Sibbi for version 4
// Uncompress file to temporary location and return a filepointer to the temporary uncompressed file
#ifdef _HAVE_ZLIB
FILE *uncompressfile(char *inputfile,unsigned int debug, FILE *debugfile) {
// File pointers
FILE *infile;
FILE *outfile;
// Temporary filename
char tmptemplate[] = "/tmp/extractadf.XXXXX";
// File descriptor for temp file
int fd = 0;
// To store whether this is a gzip or zip file, both of which are (annoyingly) common
int iszip = 0;
// To store the filename length for a zip file header
int zipfilenamelength = 0;
// To store the extra header length for a zip file header
int zipextraheader = 0;
if(debug)
fprintf(debugfile,"Input filename is %s\n",inputfile);
// Define ZLib stream, and input and output chunks (CHUNK is defined as 0x4000 earlier in thsi program)
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
// For reading in the file header
unsigned char header[5];
// How many bytes we have
unsigned int have = 0;
// Store return code of inflate
int ret = 0;
// Open input and output files
if(inputfile != NULL) {
infile = fopen(inputfile,"r");
if(infile == NULL) {
fprintf(stderr,"Can't open input file\n");
// Can't open file
return NULL;
}
// Is this a zip file? A lot of people assume gzip/zip are the same, they are obviously not, zip is an archive file format
// However, even if this is a zip file, we might still be able to decompress it
// Read in the first four bytes
if(fread(header,1,4,infile) == 4) {
if(header[0] == 0x50 && header[1] == 0x4B && header[2] == 0x03 && header[3] == 0x04) {
fprintf(stderr,"Input file appears to be in zip format\n");
// It's a zip file
iszip=1;
// Check the minimum version fields
if(fread(header,1,2,infile) == 2) {
if(header[1] != 0) {
// Seek to byte 5, after the pkzip + min version
fseek(infile,5,SEEK_SET);
// Otherwise this is a regular zip file
} else {
// Skip to byte 26 in the header
fseek(infile,26,SEEK_SET);
// Read the next 2 bytes which represent the length of the filename
if(fread(header,1,2,infile) == 2) {
// Get length of filename (which is in LSB format)
zipfilenamelength = (header[1]<<8)+header[0];
if(debug)
fprintf(debugfile,"Filename length %u %u\n",header[0],header[1]);
// Then the length of the extra header
if(fread(header,1,2,infile) == 2) {
zipextraheader = (header[1]<<8)+header[0];
// Skip 30+zipfilenamelength + zipextraheader bytes into the file
if(debug)
fprintf(debugfile,"Extra header length %u %u\n",header[0],header[1]);
fseek(infile,30+zipextraheader+zipfilenamelength,SEEK_SET);
} else {
fprintf(stderr,"ZIP header damaged\n");
return NULL;
}
} else {
fprintf(stderr,"ZIP header damaged\n");
return NULL;
}
}
} else {
fprintf(stderr,"ZIP header damaged\n");
return NULL;
}
} else {
// Rewind to start of file
rewind(infile);
}
} else {
// Can't read 4 bytes from file
fprintf(stderr,"Can't read from input file\n");