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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#include <time.h>
#include "u816.h"
#include "sanity.h"
#include "self.h"
#include "crunch.h"
int opt_append = 0;
int opt_overwrite = 0;
int opt_zerotime = 0;
int opt_alftime = 0;
int opt_gmtime = 0;
int opt_txtconv = 0;
int opt_verbose = 0;
struct stat in_file_stat;
long hdr_compsize_pos;
FILE *out_file, *in_file;
const char *out_filename, *in_filename;
void store_quad(int pos, unsigned long data) {
int i;
for(i = 0; i < 4; i++) {
output_buf[pos++] = data & 0xff;
data >>= 8;
}
}
void store_cksum(void) {
int i;
u16 cksum = 0;
for(i = 0; i < input_len; i++)
cksum += input_buf[i];
output_buf[23] = cksum & 0xff;
output_buf[24] = cksum >> 8;
}
/* examples:
foo => FOO.
toolongfile => TOOLONGF. (really should be TOOLONGF.ILE)
regular.txt => REGULAR.TXT
too.many.dots => TOO.MAN
*/
void atarify_filename(char *result) {
int i;
char name[9] = { 0 }, ext[4] = { 0 }, *p;
p = strrchr(in_filename, '/');
if(p)
p++;
else
p = (char *)in_filename;
strncpy(name, p, 8);
for(i = 0; i < 8; i++) {
if(!name[i]) break;
if(name[i] == '.') {
name[i] = '\0';
break;
}
}
strcpy(result, name);
strcat(result, ".");
p = strchr(p, '.');
if(p) {
p++;
strncpy(ext, p, 3);
for(p = ext; *p; p++)
if(*p == '.') *p = 0;
strcat(result, ext);
}
for(p = result; *p; p++)
*p = toupper(*p);
}
/* see Arcinfo for the gory details. */
unsigned long get_msdos_date_time(void) {
time_t t = in_file_stat.st_mtime;
struct tm *tm;
int msdos_year;
u16 ms_date, ms_time;
if(opt_gmtime)
tm = gmtime(&t);
else
tm = localtime(&t);
msdos_year = tm->tm_year + 1900 - 1980;
/* tm_mon + 1 because MS uses 1-12 for months, and struct tm uses 0-11 */
ms_date = (tm->tm_mday) | (((tm->tm_mon + 1) << 5)) | (msdos_year << 9);
ms_time = (tm->tm_sec >> 1) | (tm->tm_min << 5) | (tm->tm_hour << 11);
return ms_date | (ms_time << 16);
}
void create_header(void) {
char hdr_filename[13];
unsigned long time;
atarify_filename(hdr_filename);
printf("Crunching %s\n", hdr_filename);
sanity_check_filename(hdr_filename);
if(opt_alftime)
time = 0x03130588;
else if(opt_zerotime)
time = 0;
else
time = get_msdos_date_time();
output_buf[0] = 0x1a;
output_buf[1] = 0x0f;
memset(&output_buf[3], 0x20, 13); /* LZ.COM space-fills this field */
strncat((char *)&output_buf[2], hdr_filename, 13);
store_quad(15, 0); /* compressed size, fill in later */
store_quad(19, time);
store_cksum();
store_quad(25, input_len);
output_len = 29;
}
void update_header(void) {
store_quad(15, output_len - 29);
}
void open_input(const char *filename) {
in_filename = filename;
if(!(in_file = fopen(in_filename, "rb"))) {
perror(in_filename);
exit(1);
}
}
void make_backup(void) {
char bak[PATH_MAX + 2];
strncpy(bak, out_filename, PATH_MAX);
strcat(bak, "~");
rename(out_filename, bak);
}
void convert_eols(void) {
int i;
for(i = 0; i < input_len; i++) {
if(input_buf[i] == '\n')
input_buf[i] = 0x9b;
else if(input_buf[i] == '\t')
input_buf[i] = 0x7f;
}
}
void crunch_file(const char *filename) {
open_input(filename);
/* read in entire input, couldn't do it this way on the Atari */
input_len = fread(input_buf, 1, MAX_INPUT_SIZE - 1, in_file);
if(!feof(in_file)) {
fprintf(stderr, "%s: %s: this file is too large; only compressing the first 16MB.\n", self, filename);
}
if(opt_txtconv)
convert_eols();
output_len = 0;
fstat(fileno(in_file), &in_file_stat); /* for timestamp */
fclose(in_file);
memset(output_buf, 0, sizeof(output_buf));
create_header();
/* crunches the entire input to memory! */
crunch();
update_header();
/* don't open the output file until crunch() has succeeded once.
this avoids leaving 0-byte turds */
if(!out_file) {
if(!opt_overwrite) make_backup();
out_file = fopen(out_filename, opt_append ? "ab" : "wb");
if(!out_file) {
fprintf(stderr, "%s: fatal: ", self);
perror(out_filename);
exit(1);
}
}
fwrite(output_buf, 1, output_len, out_file);
}
void usage(void) {
extern char *usage_msg[];
char **line;
puts("alf (ALF compressor) v" VERSION " by B. Watson, WTFPL.");
printf("Usage: %s [-a|-o] archive.alf file [file ...]\n", self);
puts("Options:");
for(line = usage_msg; *line; line++)
puts(*line);
exit(0);
}
int main(int argc, char **argv) {
int opt;
set_self(argv[0]);
if(argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
usage();
}
if(!strcmp(argv[1], "--version")) {
puts(VERSION);
exit(0);
}
/* don't let getopt() print error message for us. */
opterr = 0;
while((opt = getopt(argc, argv, "aAt:oV")) != -1) {
switch(opt) {
case 'A': opt_txtconv = 1; break;
case 'a': opt_append = 1; opt_overwrite = 1; break;
case 'o': opt_overwrite = 1; opt_append = 0; break;
case 't': opt_zerotime = opt_alftime = opt_gmtime = 0;
switch(*optarg) {
case 'z': opt_zerotime = 1; break;
case 'd': opt_alftime = 1; break;
case 'u': opt_gmtime = 1; break;
default:
fprintf(stderr, "%s: fatal: invalid -t suboption '-%c' (try -h or --help)\n", self, *optarg);
exit(1);
}
break;
case 'V': puts(VERSION); exit(0); break;
default:
fprintf(stderr, "%s: fatal: invalid option '-%c' (try -h or --help)\n", self, optopt);
exit(1);
}
}
if(optind >= argc) {
fprintf(stderr, "%s: fatal: missing alf file argument (try -h or --help)\n", self);
exit(1);
}
out_filename = argv[optind];
optind++;
if(optind >= argc) {
fprintf(stderr, "%s: fatal: no filenames (nothing to compress) (try -h or --help)\n", self);
exit(1);
}
while(optind < argc) {
crunch_file(argv[optind++]);
}
if(out_file) fclose(out_file);
exit(0);
}
|