-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcbmarcs.c
2626 lines (2285 loc) · 80 KB
/
cbmarcs.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
/*
* cbmarcs.c
*
* Commodore archive formats directory display routines
*
* Compile this file with "pack structures" compiler flag if not GNU C
*
* fvcbm is copyright 1993-2023 Dan Fandrich, et. al.
* fvcbm is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* fvcbm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with fvcbm; if not, see <https://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "cbmarcs.h"
#ifdef __MSDOS__
#include <io.h>
#else
extern long filelength(int);
#endif
#if defined(MSC) || defined(SCO)
#define stricmp strcmp /* this isn't equivalent, but it's good enough */
#elif defined(__GNUC__)
#define stricmp strcasecmp
#endif
#ifdef __GNUC__
#define PACK __attribute__ ((packed)) /* pack structures on byte boundaries */
#else
#define PACK /* pack using a compiler switch instead */
#endif
#ifdef SUNOS
#include <unistd.h>
#endif
#ifdef DEBUG
/* Enable low-level debug logs */
#define DEBUGLOG printf
#else
/* Disable low-level debug logs */
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
#define DEBUGLOG(...) do { } while (0)
#elif defined(__GNUC__)
#define DEBUGLOG(x...) do { } while (0)
#else
#define DEBUGLOG (void)
#endif
#endif
extern char *ProgName;
/******************************************************************************
* Constants
******************************************************************************/
/* These descriptions must be in the order encountered in ArchiveTypes */
const char * const ArchiveFormats[] = {
/* C64_ARC */ " ARC",
/* C64_10 */ " C64",
/* C64_13 */ " C64",
/* C64_15 */ " C64",
/* C128_15 */ "C128",
/* LHA_SFX */ " LHA",
/* LHA */ " LHA",
/* Lynx */ "Lynx",
/* New Lynx */ "Lynx",
/* Tape image */" T64",
/* Disk image */" D64",
/* Disk image */"1581",
/* Disk image */" X64",
/* PRG file */ " P00",
/* SEQ file */ " S00",
/* USR file */ " U00",
/* REL file */ " R00",
/* DEL file */ " D00",
/* P00-like */ "P00?",
/* N64 file */ " N64",
/* LBR file */ " LBR",
/* TAP file */ " TAP"
};
/* File types as found on disk (bitwise AND code with CBM_TYPE) */
static const char * const CBMFileTypes[] = {
/* 0 */ "DEL",
/* 1 */ "SEQ",
/* 2 */ "PRG",
/* 3 */ "USR",
/* 4 */ "REL",
/* 5 */ "CBM", /* 1581 partition type */
/* 6 */ "DJJ", /* C65 file type */
/* 7 */ "FAB" /* C65 file type */
};
/* File type mask bits */
enum {
CBM_DEL = 0,
CBM_SEQ,
CBM_PRG,
CBM_USR,
CBM_REL,
CBM_CBM,
CBM_DJJ,
CBM_FAB,
CBM_TYPE = 0x07, /* Mask to get preceding 8 file types */
CBM_CLOSED = 0x80, /* Mask to get closed bit */
CBM_LOCKED = 0x40 /* Mask to get locked bit */
};
/* End of 1541 filename character */
enum { CBM_END_NAME = '\xA0' };
/* 1571 double sided flag */
enum { FLAG_DOUBLE_SIDED = 0x80 };
/******************************************************************************
* Types
******************************************************************************/
typedef int bool;
/******************************************************************************
* Functions
******************************************************************************/
/******************************************************************************
* Return smallest of 2 numbers
******************************************************************************/
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
/******************************************************************************
* Return file type string given letter code
******************************************************************************/
static const char *FileTypes(char TypeCode)
{
switch (toupper(TypeCode)) {
case 'P': return "PRG";
case 'S': return "SEQ";
case 'U': return "USR";
case 'R': return "REL";
case 'D': return "DEL";
case ' ': return " ";
default: return "???";
}
}
/******************************************************************************
* Convert CBM file names into ASCII strings
* Converts in place & returns pointer to start of string
******************************************************************************/
static char *ConvertCBMName(char *InString)
{
char *LastNonBlank = InString;
char *Ch;
for (Ch = InString; *Ch; ++Ch)
if (!isspace(*Ch = *Ch & 0x7F)) /* strip high bit */
LastNonBlank = Ch;
*++LastNonBlank = 0; /* truncate string after last character */
return InString;
}
/*---------------------------------------------------------------------------*/
/******************************************************************************
* ARC routines
******************************************************************************/
struct ArchiveHeaderNew {
BYTE Filler1 PACK;
BYTE FirstOffL PACK;
BYTE Filler2[3] PACK;
BYTE FirstOffH PACK;
};
struct ArchiveEntryHeader {
BYTE Magic PACK;
BYTE EntryType PACK;
WORD Checksum PACK;
WORD LengthL PACK;
BYTE LengthH PACK;
BYTE BlockLength PACK;
BYTE Filler PACK;
BYTE FileType PACK;
BYTE FileNameLen PACK;
};
enum {MagicARCEntry = 2};
struct C64_10 {
WORD StartAddress PACK;
BYTE Filler1[2] PACK;
WORD Version PACK;
BYTE Magic1[10] PACK;
BYTE Filler2 PACK;
BYTE FirstOffL PACK;
BYTE Magic2[3] PACK;
BYTE FirstOffH PACK;
};
struct C64_13 {
WORD StartAddress PACK;
BYTE Filler1[2] PACK;
WORD Version PACK;
BYTE Magic1[10] PACK;
BYTE Filler2[11] PACK;
BYTE FirstOffL PACK;
BYTE Magic2[3] PACK;
BYTE FirstOffH PACK;
};
struct C64_15 {
WORD StartAddress PACK;
BYTE Filler1[2] PACK;
WORD Version PACK;
BYTE Magic1[10] PACK;
BYTE Filler2[7] PACK;
BYTE Magic2[4] PACK;
WORD StartPointer PACK;
};
struct C128_15 {
WORD StartAddress PACK;
BYTE Filler1[2] PACK;
WORD Version PACK;
BYTE Magic1[10] PACK;
BYTE Magic2 PACK;
WORD StartPointer PACK;
};
struct C64_ARC {
BYTE Magic PACK;
BYTE EntryType PACK;
};
/* CBM ARC compression types */
static const char * const ARCEntryTypes[] = {
/* 0 */ "Stored",
/* 1 */ "Packed",
/* 2 */ "Squeezed",
/* 3 */ "Crunched",
/* 4 */ "Squashed",
/* 5 */ "?5", /* future use */
/* 6 */ "?6",
/* 7 */ "?7"
};
enum {MaxARCEntry = 7};
static const BYTE MagicHeaderC64[10] = {0x9e,'(','2','0','6','3',')',0x00,0x00,0x00};
static const BYTE MagicHeaderC128[10] = {0x9e,'(','7','1','8','3',')',0x00,0x00,0x00};
/******************************************************************************
* Is archive C64 ARC format?
******************************************************************************/
bool IsC64_10(FILE *InFile, const char *FileName)
{
static const BYTE MagicC64_10[3] = {0x85,0xfd,0xa9};
struct C64_10 Header;
rewind(InFile);
return ((fread(&Header, sizeof(Header), 1, InFile) == 1)
&& (memcmp(Header.Magic1, MagicHeaderC64, sizeof(MagicHeaderC64)) == 0)
&& (memcmp(Header.Magic2, MagicC64_10, sizeof(MagicC64_10)) == 0));
}
bool IsC64_13(FILE *InFile, const char *FileName)
{
static const BYTE MagicC64_13[3] = {0x85,0x2f,0xa9};
struct C64_13 Header;
rewind(InFile);
return ((fread(&Header, sizeof(Header), 1, InFile) == 1)
&& (memcmp(Header.Magic1, MagicHeaderC64, sizeof(MagicHeaderC64)) == 0)
&& (memcmp(Header.Magic2, MagicC64_13, sizeof(MagicC64_13)) == 0));
}
bool IsC64_15(FILE *InFile, const char *FileName)
{
static const BYTE MagicC64_15[4] = {0x8d,0x21,0xd0,0x4c};
struct C64_15 Header;
rewind(InFile);
return ((fread(&Header, sizeof(Header), 1, InFile) == 1)
&& (memcmp(Header.Magic1, MagicHeaderC64, sizeof(MagicHeaderC64)) == 0)
&& (memcmp(Header.Magic2, MagicC64_15, sizeof(MagicC64_15)) == 0));
}
bool IsC128_15(FILE *InFile, const char *FileName)
{
static const BYTE MagicC128_15 = 0x4c;
struct C128_15 Header;
rewind(InFile);
return ((fread(&Header, sizeof(Header), 1, InFile) == 1)
&& (memcmp(Header.Magic1, MagicHeaderC128, sizeof(MagicHeaderC128)) == 0)
&& (Header.Magic2 == MagicC128_15));
}
bool IsC64_ARC(FILE *InFile, const char *FileName)
{
enum {MagicHeaderARC = 2};
struct C64_ARC Header;
rewind(InFile);
return ((fread(&Header, sizeof(Header), 1, InFile) == 1)
&& ((BYTE) Header.Magic == MagicHeaderARC)
&& ((BYTE) Header.EntryType <= MaxARCEntry));
}
/******************************************************************************
* Read directory
******************************************************************************/
int DirARC(FILE *InFile, enum ArchiveTypes ArcType, struct ArcTotals *Totals,
DisplayStartFunc DisplayStart, DisplayEntryFunc DisplayEntry)
{
long CurrentPos;
Totals->ArchiveEntries = 0;
Totals->TotalBlocks = 0;
Totals->TotalBlocksNow = 0;
Totals->TotalLength = 0;
Totals->DearcerBlocks = 0;
Totals->Version = 0;
if (fseek(InFile, 0, SEEK_SET) != 0) {
perror(ProgName);
return 2;
}
/******************************************************************************
* Find the version number and first archive entry offset for each format
******************************************************************************/
switch (ArcType) {
case C64_ARC: /* Not a self dearcer -- just the arc data */
CurrentPos = 0L;
break;
case C64_10: {
struct C64_10 Header;
if (fread(&Header, sizeof(Header), 1, InFile) != 1) {
fprintf(stderr,"%s: Archive format error\n", ProgName);
return 2;
}
CurrentPos = 1016;
/*
CurrentPos = ((Header.FirstOffH << 8) |
Header.FirstOffL) -
CF_LE_W(Header.StartAddress) + 2;
*/
Totals->Version = -CF_LE_W(Header.Version);
Totals->DearcerBlocks = (int) ((CurrentPos-1) / 254 + 1);
}
break;
case C64_13: {
struct C64_13 Header;
if (fread(&Header, sizeof(Header), 1, InFile) != 1) {
fprintf(stderr,"%s: Archive format error\n", ProgName);
return 2;
}
CurrentPos = 1778;
/*
CurrentPos = ((Header.FirstOffH << 8) |
Header.FirstOffL) -
CF_LE_W(Header.StartAddress) + 2;
*/
Totals->Version = -CF_LE_W(Header.Version);
Totals->DearcerBlocks = (int) ((CurrentPos-1) / 254 + 1);
}
break;
case C64_15: {
struct C64_15 Header;
if (fread(&Header, sizeof(Header), 1, InFile) != 1) {
fprintf(stderr,"%s: Archive format error\n", ProgName);
return 2;
}
CurrentPos = 2286;
/*
fseek(InFile, CF_LE_W(Header.StartPointer) -
CF_LE_W(Header.StartAddress) + 2, SEEK_SET);
fread(&FileHeaderNew, sizeof(FileHeaderNew), 1, InFile);
CurrentPos = ((FileHeaderNew.FirstOffH << 8) |
FileHeaderNew.FirstOffL) -
CF_LE_W(Header.StartAddress) + 2;
*/
Totals->Version = -CF_LE_W(Header.Version);
Totals->DearcerBlocks = (int) ((CurrentPos-1) / 254 + 1);
}
break;
case C128_15: {
struct C128_15 Header;
if (fread(&Header, sizeof(Header), 1, InFile) != 1) {
fprintf(stderr,"%s: Archive format error\n", ProgName);
return 2;
}
CurrentPos = 2285;
/*
fseek(InFile, CF_LE_W(Header.StartPointer) -
CF_LE_W(Header.StartAddress) + 2, SEEK_SET);
fread(&FileHeaderNew, sizeof(FileHeaderNew), 1, InFile);
CurrentPos = ((FileHeaderNew.FirstOffH << 8) |
FileHeaderNew.FirstOffL) -
CF_LE_W(Header.StartAddress) + 2;
*/
Totals->Version = -CF_LE_W(Header.Version);
Totals->DearcerBlocks = (int) ((CurrentPos-1) / 254 + 1);
}
break;
default:
return 2; /* wrong archive type */
}
/*printf("DirArc CurrentPos: %ld\n", CurrentPos);*/
/******************************************************************************
* Read the archive directory contents
******************************************************************************/
if (fseek(InFile, CurrentPos, SEEK_SET) != 0) {
perror(ProgName);
return 2;
}
DisplayStart(ArcType, NULL);
while (1) {
char EntryName[17];
long FileLen;
struct ArchiveEntryHeader FileHeader;
/* struct ArchiveHeaderNew FileHeaderNew;*/
if (fread(&FileHeader, sizeof(FileHeader), 1, InFile) != 1)
break;
if (FileHeader.Magic != MagicARCEntry)
break;
if (fread(&EntryName, FileHeader.FileNameLen, 1, InFile) != 1)
break;
EntryName[FileHeader.FileNameLen] = 0;
FileLen = (long) (FileHeader.LengthH << 16L) | CF_LE_W(FileHeader.LengthL);
DisplayEntry(
ConvertCBMName(EntryName),
FileTypes(FileHeader.FileType),
(long) FileLen,
(unsigned) ((FileLen-1) / 254 + 1),
ARCEntryTypes[FileHeader.EntryType],
(int) (100 - (FileHeader.BlockLength * 100L / (FileLen / 254 + 1))),
(unsigned) FileHeader.BlockLength,
(long) CF_LE_W(FileHeader.Checksum)
);
CurrentPos += FileHeader.BlockLength * 254;
if (fseek(InFile, CurrentPos, SEEK_SET) != 0) {
perror(ProgName);
return 2;
}
++Totals->ArchiveEntries;
Totals->TotalLength += FileLen;
Totals->TotalBlocks += (int) ((FileLen-1) / 254 + 1);
Totals->TotalBlocksNow += FileHeader.BlockLength;
};
return 0;
}
/*---------------------------------------------------------------------------*/
/******************************************************************************
* Lynx reading routines
******************************************************************************/
/******************************************************************************
* Convert Roman numeral to decimal
* Only works for numerals up to 399
* Note: input string is cleared
******************************************************************************/
static int RomanToDec(char *Roman)
{
int Last = -1;
int Value = 0;
while (*Roman) {
int Digit;
switch (toupper(*Roman)) {
case 'I': Digit = 1; break;
case 'V': Digit = 5; break;
case 'X': Digit = 10; break;
case 'L': Digit = 50; break;
case 'C': Digit = 100; break;
default: Digit = 0; break;
}
Value = Last < Digit ? Digit - Value: Value + RomanToDec(Roman);
Last = Digit;
*Roman++ = 0;
}
return Value;
}
static const BYTE MagicHeaderLynx[10] = {' ','1',' ',' ',' ','L','Y','N','X',' '};
static const BYTE MagicHeaderLynxNew[25] = {0x97,'5','3','2','8','0',',','0',0x3A,
0x97,'5','3','2','8','1',',','0',0x3A,
0x97,'6','4','6',',',0xC2,0x28};
struct Lynx {
BYTE Magic[sizeof(MagicHeaderLynx)] PACK;
};
struct LynxNew {
WORD StartAddress PACK;
WORD EndHeaderAddr PACK;
WORD Version PACK;
BYTE Magic[sizeof(MagicHeaderLynxNew)] PACK;
};
/******************************************************************************
* Is archive Lynx format?
******************************************************************************/
bool IsLynx(FILE *InFile, const char *FileName)
{
struct Lynx Header;
rewind(InFile);
return ((fread(&Header, sizeof(Header), 1, InFile) == 1)
&& (memcmp(Header.Magic, MagicHeaderLynx, sizeof(MagicHeaderLynx)) == 0));
}
bool IsLynxNew(FILE *InFile, const char *FileName)
{
struct LynxNew Header;
rewind(InFile);
return ((fread(&Header, sizeof(Header), 1, InFile) == 1)
&& (memcmp(Header.Magic, MagicHeaderLynxNew, sizeof(MagicHeaderLynxNew)) == 0));
}
/******************************************************************************
* Read directory
******************************************************************************/
int DirLynx(FILE *InFile, enum ArchiveTypes LynxType, struct ArcTotals *Totals,
DisplayStartFunc DisplayStart, DisplayEntryFunc DisplayEntry)
{
int NumFiles;
char LynxVer[10];
char LynxName[16];
int ExpectLastLength;
Totals->ArchiveEntries = 0;
Totals->TotalBlocks = 0;
Totals->TotalBlocksNow = 0;
Totals->TotalLength = 0;
Totals->DearcerBlocks = 0;
Totals->Version = 0;
/******************************************************************************
* Find the version number and first archive entry offset for each format
******************************************************************************/
switch (LynxType) {
case Lynx:
if (fseek(InFile, 0, SEEK_SET) != 0) {
perror(ProgName);
return 2;
}
if (fscanf(InFile, " %*s LYNX %s %*[^\r]", LynxVer) != 1) {
fprintf(stderr,"%s: Archive format error\n", ProgName);
return 2;
}
getc(InFile); /* Get CR without killing whitespace */
Totals->Version = RomanToDec(LynxVer);
Totals->DearcerBlocks = 0;
ExpectLastLength = Totals->Version >= 10;
break;
case LynxNew:
/* fseek(InFile, CF_LE_W(Header.Type.LynxNew.EndHeaderAddr) -
CF_LE_W(Header.Type.LynxNew.StartAddress) + 5, SEEK_SET); */
if (fseek(InFile, 0x5F, SEEK_SET) != 0) {
perror(ProgName);
return 2;
}
if (fscanf(InFile, " %*s *%15s %s %*[^\r]", LynxName, LynxVer) != 2) {
fprintf(stderr,"%s: Archive format error\n", ProgName);
return 2;
}
getc(InFile); /* Get CR without killing whitespace */
if (isupper(*LynxVer))
Totals->Version = RomanToDec(LynxVer); /* Lynx */
else
Totals->Version = atoi(LynxVer); /* Ultra-Lynx */
/* Only old versions of Lynx need this FALSE */
/* Ultra-Lynx looks like it always has Version > 10 */
ExpectLastLength = Totals->Version >= 10;
Totals->DearcerBlocks = 0;
break;
default:
return 2; /* wrong archive type */
}
if (fscanf(InFile, "%d%*[^\r]\r", &NumFiles) != 1) {
fprintf(stderr,"%s: Archive format error\n", ProgName);
return 2;
}
DisplayStart(LynxType, NULL);
/******************************************************************************
* Read the archive directory contents
******************************************************************************/
for (; NumFiles--;) {
char EntryName[17];
char FileType[2];
int FileBlocks;
long FileLen = 0;
int ReadCount = fscanf(InFile, "%16[^\r]%*[^\r]", EntryName);
(void) getc(InFile); /* eat the CR here because Sun won't in scanf */
ReadCount += fscanf(InFile, "%d%*[^\r]", &FileBlocks);
(void) getc(InFile);
ReadCount += fscanf(InFile, "%1s%*[^\r]", FileType);
(void) getc(InFile); /* eat the CR without killing whitespace so
ftell() will be correct, below */
if (ReadCount != 3) {
fprintf(stderr,"%s: Archive format error\n", ProgName);
return 2;
}
/******************************************************************************
* Find the exact length of the file.
* For the first n-1 entries (for all n entries in newer Lynx versions, like
* XVII), the length in bytes of the last block of the file is specified.
* For the last entry in older Lynx versions, like IX, we must find out how many
* bytes in the file and subtract everything not belonging to the last file.
* This can give an incorrect result if the file has padding after the last
* file (which would happen if the file was transferred using XMODEM), but
* Lynx thinks the padding is part of the file, too.
* Should check for an error return from filelength()
******************************************************************************/
if (NumFiles || ExpectLastLength) {
int LastBlockSize = 0;
fscanf(InFile, "%d%*[^\r]\r", &LastBlockSize);
FileLen = (long) ((FileBlocks-1) * 254L + LastBlockSize - 1);
} else /* last entry -- calculate based on file size */
FileLen = filelength(fileno(InFile)) - Totals->TotalBlocksNow * 254L -
(((ftell(InFile) - 1) / 254) + 1) * 254L;
DisplayEntry(
ConvertCBMName(EntryName),
FileTypes(FileType[0]),
(long) FileLen,
FileBlocks,
"Stored",
0,
FileBlocks,
-1L
);
++Totals->ArchiveEntries;
Totals->TotalLength += FileLen;
/* The following two values should equal */
Totals->TotalBlocks += (int) ((FileLen-1) / 254 + 1);
Totals->TotalBlocksNow += FileBlocks;
};
return 0;
}
/*---------------------------------------------------------------------------*/
/******************************************************************************
* LHA (SFX) reading routines
******************************************************************************/
struct LHAEntryHeader {
BYTE HeadSize PACK;
BYTE HeadChk PACK;
BYTE HeadID[3] PACK;
BYTE EntryType PACK;
BYTE Magic PACK;
LONG PackSize PACK;
LONG OrigSize PACK;
#if 0 /* avoid ANSI warning because WORD != int */
struct { /* DOS format time */
WORD ft_tsec : 5; /* Two second interval */
WORD ft_min : 6; /* Minutes */
WORD ft_hour : 5; /* Hours */
WORD ft_day : 5; /* Days */
WORD ft_month: 4; /* Months */
WORD ft_year : 7; /* Year */
} FileTime PACK;
#else
LONG DosTime PACK;
#endif
WORD Attr PACK;
BYTE FileNameLen PACK;
/*
* File name is variable length and occurs at the end of the header, so it
* can't be read as part of the header because with a short file name there
* might not be enough file left to read. */
/* BYTE FileName[64] PACK; */
};
struct LHAEntryFileName {
BYTE FileName[64] PACK;
/*
* Checksum is stored after the variable-length filename, but can't be part of
* the struct because it's variable length...
* BYTE ChecksumL PACK;
* BYTE ChecksumH PACK;
*/
};
/* Perform a compile-time check on the size of the struct to ensure that struct
* element packing (with the PACK macro) is working correctly. If the compiler
* complains in the following line with an error like: "invalid array size" or
* "size of array ‘PackStructCompileCheck’ is negative" then fix the PACK macro
* to perform structure packing for your compiler.
* Instead of checking all the packed structs in the program, this one example
* was chosen as a sentinel since it is particularly likely to show problems
* due to its odd-byte alignment of LONG and WORD elements. If the compiler
* errors out on this line, struct alignment is not working on this compiler
* and it must be fixed before the code will work.
*/
#ifndef __SCCZ80
typedef char PackStructCompileCheck[sizeof(struct LHAEntryHeader) == 22 ? 1 : -1];
#endif
/* LHA compression types */
static const char * const LHAEntryTypes[] = {
/* 0 */ "Stored",
/* 1 */ "lh1",
/* 2 */ "lh2",
/* 3 */ "lh3",
/* 4 */ "lh4",
/* 5 */ "lh5",
/* 6 */ "lh6",
/* 7 */ "lh7",
/* 8 */ "lh8",
/* 9 */ "lh9",
/* A */ "lhA",
/* B */ "lhB"
};
static const BYTE MagicLHAEntry[3] = {'-','l','h'};
static const BYTE MagicHeaderLHASFX[10] = {0x97,0x32,0x30,0x2C,0x30,0x3A,0x8B,0xC2,0x28,0x32};
static const BYTE MagicHeaderLHA[3] = {'-','l','h'};
struct LHA_SFX {
WORD StartAddress PACK;
BYTE Filler[4] PACK;
BYTE Magic[sizeof(MagicHeaderLHASFX)] PACK;
};
struct LHA {
BYTE Filler[2] PACK;
BYTE Magic[sizeof(MagicHeaderLHA)] PACK;
};
/******************************************************************************
* Is archive LHA format?
******************************************************************************/
bool IsLHA_SFX(FILE *InFile, const char *FileName)
{
struct LHA_SFX Header;
rewind(InFile);
return ((fread(&Header, sizeof(Header), 1, InFile) == 1)
&& (memcmp(Header.Magic, MagicHeaderLHASFX, sizeof(MagicHeaderLHASFX)) == 0));
}
bool IsLHA(FILE *InFile, const char *FileName)
{
struct LHA Header;
rewind(InFile);
return ((fread(&Header, sizeof(Header), 1, InFile) == 1)
&& (memcmp(Header.Magic, MagicHeaderLHA, sizeof(MagicHeaderLHA)) == 0));
}
/******************************************************************************
* Read directory
******************************************************************************/
int DirLHA(FILE *InFile, enum ArchiveTypes LHAType, struct ArcTotals *Totals,
DisplayStartFunc DisplayStart, DisplayEntryFunc DisplayEntry)
{
long CurrentPos;
Totals->ArchiveEntries = 0;
Totals->TotalBlocks = 0;
Totals->TotalBlocksNow = 0;
Totals->TotalLength = 0;
/******************************************************************************
* Find the version number and first archive entry offset for each format
******************************************************************************/
switch (LHAType) {
case LHA_SFX:
CurrentPos = 0xE89; /* Must be a better way than this */
Totals->Version = 0;
Totals->DearcerBlocks = (int) ((CurrentPos-1) / 254 + 1);
break;
case LHA:
CurrentPos = 0;
Totals->Version = 0;
Totals->DearcerBlocks = 0;
break;
default:
return 2; /* wrong archive type */
}
/******************************************************************************
* Read the archive directory contents
******************************************************************************/
if (fseek(InFile, CurrentPos, SEEK_SET) != 0) {
perror(ProgName);
return 2;
}
DisplayStart(LHAType, NULL);
while (1) {
struct LHAEntryHeader FileHeader;
struct LHAEntryFileName EntryFileName;
char FileName[80]; /* must be > sizeof(EntryFileName) */
if (fread(&FileHeader, sizeof(FileHeader), 1, InFile) != 1)
break;
if (memcmp(FileHeader.HeadID, MagicLHAEntry, sizeof(MagicLHAEntry)) != 0)
break;
/* 2-byte checksum is stored as part of the filename but not counted here */
if (FileHeader.FileNameLen > sizeof(EntryFileName.FileName)-2)
break; /* exceeds limit; probably corrupt */
if (fread(&EntryFileName, FileHeader.FileNameLen+2, 1, InFile) != 1)
break;
memcpy(FileName, EntryFileName.FileName, FileHeader.FileNameLen);
FileName[min(sizeof(FileName)-1, FileHeader.FileNameLen)] = 0;
DisplayEntry(
ConvertCBMName(FileName),
FileTypes(EntryFileName.FileName[FileHeader.FileNameLen-2] ? ' ' : EntryFileName.FileName[FileHeader.FileNameLen-1]),
(long) CF_LE_L(FileHeader.OrigSize),
CF_LE_L(FileHeader.OrigSize) ? (unsigned) ((CF_LE_L(FileHeader.OrigSize)-1) / 254 + 1) : 0,
LHAEntryTypes[FileHeader.EntryType - '0'],
CF_LE_L(FileHeader.OrigSize) ? (int) (100 - (CF_LE_L(FileHeader.PackSize) * 100L / CF_LE_L(FileHeader.OrigSize))) : 100,
CF_LE_L(FileHeader.PackSize) ? (unsigned) ((CF_LE_L(FileHeader.PackSize)-1) / 254 + 1) : 0,
(long) (unsigned) (EntryFileName.FileName[FileHeader.FileNameLen+1] << 8) | EntryFileName.FileName[FileHeader.FileNameLen]
);
CurrentPos += FileHeader.HeadSize + CF_LE_L(FileHeader.PackSize) + 2;
fseek(InFile, CurrentPos, SEEK_SET);
++Totals->ArchiveEntries;
Totals->TotalLength += CF_LE_L(FileHeader.OrigSize);
Totals->TotalBlocks += (int) ((CF_LE_L(FileHeader.OrigSize)-1) / 254 + 1);
Totals->TotalBlocksNow += (int) ((CF_LE_L(FileHeader.PackSize)-1) / 254 + 1);
};
return 0;
}
/*---------------------------------------------------------------------------*/
/******************************************************************************
* T64 reading routines
******************************************************************************/
struct T64Header {
BYTE Magic[32] PACK;
BYTE MinorVersion PACK;
BYTE MajorVersion PACK;
WORD Entries PACK; /* Room for this many files in tape directory */
WORD Used PACK; /* Number of files in archive (sometimes just 0) */
WORD unused PACK;
BYTE TapeName[24] PACK;
};
struct T64EntryHeader {
BYTE EntryType PACK;
BYTE FileType PACK;
WORD StartAddr PACK;
WORD EndAddr PACK;
WORD unused1 PACK;
LONG FileOffset PACK;
LONG unused2 PACK;
BYTE FileName[16] PACK;
};
/* File types in T64 tape archives */
/* At least one archive writing program uses the 1541 file types */
static const char * const T64FileTypes[] = {
/* 0 */ "SEQ",
/* 1 */ "PRG",
/* 2 */ "?2?",
/* 3 */ "?3?",
/* 4 */ "?4?",
/* 5 */ "?5?",
/* 6 */ "?6?",
/* 7 */ "?7?"
};
struct T64 {
BYTE Magic[20] PACK; /* room for terminating NUL */
};
/******************************************************************************
* Is archive T64 format?
******************************************************************************/
bool IsT64(FILE *InFile, const char *FileName)
{
struct T64 Header;
rewind(InFile);
if (fread(&Header, sizeof(Header.Magic) - 1, 1, InFile) != 1)
return 0;
/* Zero terminate just in case */
Header.Magic[sizeof(Header.Magic) - 1] = '\0';
/* Common headers are 'C64 tape image file' and 'C64S tape file' but
* lots of variants exist. The FAQ suggests the following check:
*/
return (strstr((char *) Header.Magic, "C64") != NULL &&
strstr((char *) Header.Magic, "tape") != NULL);
}
/******************************************************************************
* Read directory
******************************************************************************/
int DirT64(FILE *InFile, enum ArchiveTypes ArchiveType, struct ArcTotals *Totals,
DisplayStartFunc DisplayStart, DisplayEntryFunc DisplayEntry)
{
char TapeName[25];
int NumFiles;
struct T64Header Header;
Totals->ArchiveEntries = 0;
Totals->TotalBlocks = 0;
Totals->TotalBlocksNow = 0;
Totals->TotalLength = 0;
Totals->DearcerBlocks = 0;
Totals->Version = 0;
if (fseek(InFile, 0, SEEK_SET) != 0) {
perror(ProgName);
return 2;
}
if (fread(&Header, sizeof(Header), 1, InFile) != 1) {
fprintf(stderr,"%s: Archive format error\n", ProgName);
return 2;
}
memcpy(TapeName, Header.TapeName, sizeof(TapeName)-1);
TapeName[sizeof(TapeName)-1] = '\0';
ConvertCBMName(TapeName);
DisplayStart(ArchiveType, TapeName);
Totals->Version = -(Header.MajorVersion * 10 + Header.MinorVersion);
Totals->ArchiveEntries = CF_LE_W(Header.Used);
/******************************************************************************
* Read the archive directory contents
******************************************************************************/
for (NumFiles = CF_LE_W(Header.Used); NumFiles; --NumFiles) {
struct T64EntryHeader FileHeader;
char FileName[17];
unsigned FileLength;