aboutsummaryrefslogtreecommitdiff
path: root/src/extract.c
blob: 6f24c0dd44d329e9c374226caf26e9b8f480ff38 (plain)
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
#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) {
		fprintf(stderr, "%s: warning: date/time for %s is null, using current date/time\n", self, out_filename);
		return;
	}

	d = dpeek(alf_hdr_date0);

	tm.tm_isdst = -1;
	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(const char *fname) {
	/* up to 12-char FILENAME.EXT, plus a ~, plus null terminator = 14 */
	/* has to handle up to 16 chars for -s option: LONGFILE.NAME.ALF */
	char backup[20];

	strncpy(backup, fname, 19);
	strncat(backup, "~", 19);

	/* silently ignore errors! */
	rename(fname, backup);
}

void open_out_file(const char *fname) {
	if(opts.testonly) {
		fname = "/dev/null";
	} else if(!opts.overwrite) {
		make_backup(fname);
	}

	if(!(out_file = fopen(fname, "wb"))) {
		fprintf(stderr, "%s: fatal:  ", self);
		perror(fname);
		exit(1);
	}
}

void truncated_err(void);

void write_split_file(void) {
	int i, c;
	unsigned int len;
	char splitname[20];

	strncpy(splitname, out_filename, 13);
	strncat(splitname, ".ALF", 6);

	if(!opts.quiet)
		printf("Writing %s\n", splitname);

	len = getquad(alf_hdr_compsize0);

	open_out_file(splitname);
	fwrite(&mem[alf_header], 1, 29, out_file);
	for(i = 0; i < len; i++) {
		c = fgetc(in_file);
		if(c == EOF) {
			truncated_err();
		}
		fputc(c, out_file);
	}
	fclose(out_file);
}

void extract_alf(void) {
	int files_extracted = 0, skip, file_number = 0;

	/* get ready to call fake 6502 stuff. set up memory like the Atari. */
	dpoke(MEMTOP, 0xbc1f);

	while(read_alf_header()) {
		file_number++;
		skip = 0;

		out_filename = (char *)(mem + alf_hdr_filename);

		fix_filename();

		if(opts.extract_num)
			skip = (opts.extract_num != file_number);
		else
			skip = !file_wanted(out_filename);

		if(skip) {
			if(!opts.quiet)
				printf("Skipping %s\n", 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;
		}

		files_extracted++;

		if(opts.split) {
			write_split_file();
			continue;
		}

		if(!opts.quiet) {
			printf("%s %s\n", opts.testonly ? "Testing" : "Uncrunching", out_filename);
		}

		if(opts.extract_to_stdout) {
			out_file = stdout;
		} else {
			open_out_file(out_filename);
		}

		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");
	}

	if(!opts.quiet && !files_extracted) {
		printf("No files %s!\n", opts.testonly ? "tested" : "extracted");
	}
}

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);
}