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
|
#include "unalf.h"
#include "addrs.h"
#include "sanity.h"
#include <time.h>
#include <utime.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);
}
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(out_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 set_datetime() {
u16 t, d;
struct tm tm;
time_t time;
struct utimbuf utb;
t = dpeek(alf_hdr_time0);
/* don't set the date/time at all, if it's zero */
if(!t) return;
d = dpeek(alf_hdr_date0);
tm.tm_sec = (t & 0x1f) << 1;
tm.tm_hour = t >> 11;
if(tm.tm_hour == 24) tm.tm_hour = 0;
tm.tm_min = (t >> 5) & 0x3f;
tm.tm_year = (d >> 9) + 1980 - 1900;
tm.tm_mon = ((d >> 5) & 0x0f) - 1;
tm.tm_mday = (d & 0x1f);
time = mktime(&tm);
if(time != (time_t) -1) {
utb.actime = utb.modtime = time;
utime(out_filename, &utb); /* ignore errors */
}
}
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) {
fprintf(stderr, "%s: fatal: seek failed on input!\n", self);
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"))) {
fprintf(stderr, "%s: fatal: ", self);
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);
if(!opts.ignore_datetime)
set_datetime();
}
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 truncated_err(void){
fprintf(stderr, "%s: fatal: compressed data is truncated, EOF before end marker\n", self);
exit(1);
}
void stack_underrun(void){
fprintf(stderr, "%s: fatal: stack underrun\n", self);
exit(1);
}
void stack_overrun(void){
fprintf(stderr, "%s: fatal: stack overrun\n", self);
exit(1);
}
|