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
|
#include "unalf.h"
#include "addrs.h"
int bad_checksum, bad_checksum_count = 0;
int new_file = 0;
unsigned int bytes_written = 0;
char *out_filename;
void dpoke(int addr, u16 value) {
mem[addr] = value & 0xff;
mem[addr + 1] = value >> 8;
}
u16 dpeek(int addr) {
return mem[addr] | (mem[addr + 1] << 8);
}
static void bad_atari_filename(const char *msg) {
char fn[50] = "";
char xbuf[5];
int i;
u8 c;
for(i = 0; (c = (u8)out_filename[i]) && i < 12; i++) {
if(c < ' ' || c > '|') {
/* not printable, insert a hex escape */
sprintf(xbuf, "$%02X", c);
strcat(fn, xbuf);
} else {
strncat(fn, (char *)&c, 1);
}
}
fprintf(stderr, "%s: bad Atari filename \"%s\": %s\n", self, fn, msg);
}
/* note to self: it's tempting to use isalpha(), isprint(), etc
from ctype.h... but those are locale-aware. we want ASCII-only
versions. */
static void sanity_check_filename(void) {
u8 c;
unsigned int i, bad = 0, dots = 0, uscore = 0;
c = out_filename[0];
if(!c) {
bad_atari_filename("empty! corrupt ALF file?");
return;
} else if(c < 'A' || c > 'Z') {
bad_atari_filename("does not begin with A-Z");
}
for(i = 0; (c = out_filename[i]) && i < 12; i++) {
if(c >= 'A' && c <= 'Z')
continue;
if(c >= '0' && c <= '9')
continue;
if(c == '_') {
uscore++;
continue;
}
if(c == '.') {
dots++;
continue;
}
bad++;
}
if(!dots) {
bad_atari_filename("no \".\"");
} else if(dots > 1) {
bad_atari_filename("more than one \".\"");
}
if(bad)
bad_atari_filename("invalid characters. corrupt ALF file?");
else if(uscore)
fprintf(stderr, "%s: filename has underscore, OK on Sparta/MyDOS, not Atari DOS 2.x\n", self);
}
void fix_filename(void) {
char *p;
if(!out_filename) return;
if(strlen(out_filename) > 12) {
fprintf(stderr, "%s: filename in ALF header not null-terminated, fixing.\n", self);
out_filename[12] = '\0';
}
sanity_check_filename();
if(opts.lowercase) {
for(p = out_filename; *p; p++)
*p = tolower(*p);
}
if(!opts.keepdot) {
for(p = out_filename; *p; p++)
if(p[0] == '.' && !p[1])
*p = '\0';
}
}
void make_backup(void) {
/* up to 12-char FILENAME.EXT, plus a ~, plus null terminator = 14 */
char backup[14];
strncpy(backup, out_filename, 13);
strncat(backup, "~", 13);
/* silently ignore errors! */
rename(out_filename, backup);
}
void extract_alf(void) {
/* get ready to call fake 6502 stuff. set up memory like the Atari. */
dpoke(MEMTOP, 0xbc1f);
while(read_alf_header()) {
out_filename = (char *)(mem + alf_hdr_filename);
fix_filename();
if(!file_wanted(out_filename)) {
if(fseek(in_file, getquad(alf_hdr_compsize0), SEEK_CUR) != 0) {
fputs(self, stderr);
perror(": fseek");
exit(1);
}
out_filename = 0;
continue;
}
if(!opts.quiet) {
printf("%s %s\n", opts.testonly ? "Testing" : "Uncrunching", out_filename);
}
if(opts.extract_to_stdout) {
out_file = stdout;
} else {
char *realname = out_filename;
if(opts.testonly) {
out_filename = "/dev/null";
} else if(!opts.overwrite) {
make_backup();
}
if(!(out_file = fopen(out_filename, "wb"))) {
perror(out_filename);
exit(1);
}
out_filename = realname;
}
bad_checksum = bytes_written = 0;
new_file = 1;
uncrunch_file();
if(bad_checksum) bad_checksum_count++;
if(bytes_written != getquad(alf_hdr_origsize0))
fprintf(stderr, "%s: %s should be %u bytes, but extracted to %u.\n",
self, out_filename, getquad(alf_hdr_origsize0), bytes_written);
if(!opts.extract_to_stdout) fclose(out_file);
out_filename = 0;
}
if(opts.testonly && !opts.quiet) {
if(bad_checksum_count)
printf("%d file%s with bad checksum!\n",
bad_checksum_count,
bad_checksum_count == 1 ? "" : "s");
else
printf("All files OK.\n");
}
}
void chksum_err(void) {
bad_checksum = 1;
fprintf(stderr, "%s: checksum error on %s\n", self, out_filename);
}
void unknown_err(void){
fprintf(stderr, "%s: unknown error (L7712)\n", self);
exit(1);
}
void stack_underrun(void){
fprintf(stderr, "%s: stack underrun\n", self);
exit(1);
}
void stack_overrun(void){
fprintf(stderr, "%s: stack overrun\n", self);
exit(1);
}
|