File: hexbin.c

package info (click to toggle)
macutils 2.0b3-17.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,264 kB
  • sloc: ansic: 12,737; makefile: 661
file content (364 lines) | stat: -rwxr-xr-x 7,783 bytes parent folder | download | duplicates (2)
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
#ifdef TYPES_H
#include <sys/types.h>
#endif /* TYPES_H */
#include <sys/stat.h>
#include "globals.h"
#include "crc.h"
#include "readline.h"
#include "../util/masks.h"
#include "../util/util.h"
#include "../util/patchlevel.h"
#include "../fileio/wrfile.h"
#include "../fileio/wrfileopt.h"
#include "../fileio/machdr.h"
#include "../fileio/kind.h"
#include "../util/curtime.h"
#include "hexbin.h"

#define LOCALOPT	"ilvcn:qVH"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef DL
extern void dl();
#endif /* DL */
#ifdef HECX
extern void hecx();
#endif /* HECX */
#ifdef HQX
extern void hqx();
#endif /* HQX */
#ifdef MU
extern void mu();
#endif /* MU */

static void usage();
static void do_files();
static int find_header();

static char options[128];

int main(argc, argv)
int argc;
char **argv;
{
    char *filename;
    char macname[32];
    extern int optind;
    extern char *optarg;
    int errflg;
    int c;

    set_wrfileopt(0);
    (void)strcat(options, get_wrfileopt());
    (void)strcat(options, LOCALOPT);
    errflg = 0;
    filename = "";
    macname[0] = 0;

    while((c = getopt(argc, argv, options)) != EOF) {
	if(!wrfileopt((char)c)) {
	    switch(c) {
	    case 'l':
		listmode++;
		break;
	    case 'v':
		verbose++;
		break;
	    case 'i':
		info_only++;
		break;
	    case 'c':
		uneven_lines++;
		break;
	    case 'n':
		backtrans(macname, optarg);
		break;
	    case '?':
		errflg++;
		break;
	    case 'H':
		give_wrfileopt();
		(void)fprintf(stderr, "Hexbin specific options:\n");
		(void)fprintf(stderr,
			"-i:\tgive information only, do not convert\n");
		(void)fprintf(stderr, "-l:\tgive listing\n");
		(void)fprintf(stderr,
			"-v:\tgive verbose listing, including lines skipped\n");
		(void)fprintf(stderr,
			"-c:\tdo not check for equal line lengths\n");
		(void)fprintf(stderr, "-n nm:\tname to be generated\n");
		(void)fprintf(stderr,
			"-V:\tgive information about this version\n");
		(void)fprintf(stderr, "-H:\tthis message\n");
		(void)fprintf(stderr, "Default is silent conversion\n");
		exit(0);
	    case 'V':
		(void)fprintf(stderr, "Version %s, ", VERSION);
		(void)fprintf(stderr, "patchlevel %d", PATCHLEVEL);
		(void)fprintf(stderr, "%s.\n", get_mina());
		(void)fprintf(stderr, "Hexified files recognized:\n");
#ifdef DL
		(void)fprintf(stderr, "\tDownload (.dl)\n");
#endif /* DL */
#ifdef HECX
		(void)fprintf(stderr, "\tBinHex 2.0 (.hex)\n");
		(void)fprintf(stderr, "\tBinHex 3.0 (.hcx)\n");
#endif /* HECX */
#ifdef HQX
		(void)fprintf(stderr, "\tBinHex 4.0 (.hqx)\n");
#endif /* HQX */
#ifdef MU
		(void)fprintf(stderr, "\tUUTool (.mu)\n");
#endif /* MU */
		exit(0);
	    }
	}
    }
    if(errflg) {
	usage();
	exit(1);
    }
    if(info_only || verbose) {
	listmode++;
    }

    do {
	if(optind == argc) {
	    filename = "-";
	} else {
	    filename = argv[optind];
	    optind++;
#ifdef SCAN
	    do_idf(filename, UNIX_NAME);
#endif /* SCAN */
	}
	do_files(filename, macname);
    } while(optind < argc);
    exit(0);
    /* NOTREACHED */
}

static char *extensions[] = {
#ifdef DL
    ".dl",
#endif /* DL */
#ifdef HECX
    ".hex",
    ".Hex",
    ".hcx",
    ".Hcx",
#endif /* HECX */
#ifdef HQX
    ".hqx",
    ".Hqx",
#endif /* HQX */
#ifdef MU
    ".mu",
#endif /* MU */
    "",
    NULL
};

static void do_files(filename, macname)
char *filename;	/* input file name -- extension optional */
char *macname;	/* name to use on the mac side of things */
{
    char namebuf[256];
    char **ep;
    struct stat stbuf;
    long curtime;
    int qformat;
    int again;

    if(filename[0] == '-') {
	ifp = stdin;
	filename = "stdin";
    } else {
	/* find input file and open it */
	for(ep = extensions; *ep != NULL; ep++) {
	    (void)sprintf(namebuf, "%s%s", filename, *ep);
	    if(stat(namebuf, &stbuf) == 0) {
		break;
	    }
	}
	if(*ep == NULL) {
	    perror(namebuf);
	    exit(1);
	}
	ifp = fopen(namebuf, "r");
	if(ifp == NULL) {
	    perror(namebuf);
	    exit(1);
	}
    }
    again = 0;
nexttry:
    if(ifp == stdin) {
	curtime = (long)time((time_t *)0) + TIMEDIFF;
	mh.m_createtime = curtime;
	mh.m_modifytime = curtime;
    } else {
	mh.m_createtime = stbuf.st_mtime + TIMEDIFF;
	mh.m_modifytime = stbuf.st_mtime + TIMEDIFF;
    }

    qformat = find_header(again); /* eat mailer header &cetera, intuit format */

    switch (qformat) {
#ifdef DL
    case form_dl:
	dl(macname, filename);
	break;
#endif /* DL */
#ifdef HECX
    case form_hecx:
	hecx(macname, filename);
	break;
#endif /* HECX */
#ifdef HQX
    case form_hqx:
	hqx(macname);
	again = 1;
	goto nexttry;
#endif /* HQX */
#ifdef MU
    case form_mu:
	mu(macname);
	again = 1;
	goto nexttry;
#endif /* MU */
    default:
	break;
    }
    (void)fclose(ifp);
}

/* eat characters until header detected, return which format */
static int find_header(again)
int again;
{
    int c, dl_start, llen;
    char *cp;
    char header[INFOBYTES];
    int ds, rs;

    if(again && was_macbin) {
	while(to_read-- > 0) {
	    c = fgetc(ifp);
	}
    }
    was_macbin = 0;
    c = fgetc(ifp);
    (void)ungetc(c, ifp);
    if(c == 0) {
	was_macbin = 1;
	if(fread(header, 1, INFOBYTES, ifp) != INFOBYTES) {
	    (void)fprintf(stderr, "Premature EOF\n");
#ifdef SCAN
	    do_error("hexbin: Premature EOF");
#endif /* SCAN */
	    exit(1);
	}
	ds = get4(header + I_DLENOFF);
	rs = get4(header + I_RLENOFF);
	ds = (((ds + 127) >> 7) << 7);
	rs = (((rs + 127) >> 7) << 7);
	to_read = ds + rs;
	if(strncmp(header + I_TYPEOFF, "TEXT", 4)) {
	    (void)fprintf(stderr, "This file is not a proper BinHexed file.\n");
#ifdef SCAN
	    do_error("hexbin: not a proper BinHexed file");
#endif /* SCAN */
	    exit(1);
	}
	if(listmode) {
	    (void)fprintf(stderr, "This file is probably packed by ");
	    if(!strncmp(header + I_AUTHOFF, "BNHQ", 4)) {
		(void)fprintf(stderr, "\"BinHex\"");
	    } else if(!strncmp(header + I_AUTHOFF, "BthX", 4)) {
		(void)fprintf(stderr, "\"BinHqx\"");
	    } else if(!strncmp(header + I_AUTHOFF, "BnHq", 4)) {
		(void)fprintf(stderr, "\"StuffIt\"");
	    } else if(!strncmp(header + I_AUTHOFF, "ttxt", 4)) {
		(void)fprintf(stderr, "\"Compactor\"");
	    } else if(!strncmp(header + I_AUTHOFF, "BSWU", 4)) {
		(void)fprintf(stderr, "\"UUTool\"");
	    } else {
		(void)fprintf(stderr, "an unknown program");
	    }
	    (void)fprintf(stderr, ".\n");
	}
    }
    /*	look for "(This file ...)" or "(Convert with...)" line. */
    /*	or for "begin " */
    /*	dl format starts with a line containing only the symbols '@' to 'O',
	or '|'. */
    while(readline()) {
	llen = strlen(line);
#ifdef HQX
	if((strncmp(line, "(This file", 10) == 0) ||
	    (strncmp(line, "(Convert with", 13) == 0)) {
	    if(verbose) {
		(void)fprintf(stderr, "Skip:%s\n", line);
	    }
	    break;
	}
#endif /* HQX */
#ifdef MU
	if(strncmp(line, "begin ", 6) == 0) {
	    return form_mu;
	}
#endif /* MU */
#ifdef DL
	/* Do not allow bogus false starts */
	if(llen > 40 && (llen & 1) == 0) {
	    dl_start = 1;
	    for(cp = &line[0]; *cp != 0; cp++) {
		if((*cp < '@' || *cp > 'O') && *cp != '|') {
		    dl_start = 0;
		    break;
		}
	    }
	    if(dl_start && cp > &line[1]) {
		return form_dl;
	    }
	}
#endif /* DL */
	if(llen != 0 && verbose) {
	    (void)fprintf(stderr, "Skip:%s\n", line);
	}
    }
    while(readline()) {
	switch (line[0]) {
#ifdef HQX
	case ':':
	    return form_hqx;
#endif /* HQX */
#ifdef HECX
	case '#':
	    return form_hecx;
#endif /* HECX */
	default:
	    break;
	}
    }

    if(!again) {
	(void)fprintf(stderr, "unexpected EOF\n");
#ifdef SCAN
	do_error("hexbin: unexpected EOF");
#endif /* SCAN */
	exit(1);
    }
    return form_none;
}

static void usage()
{
    (void)fprintf(stderr, "Usage: hexbin [-%s] [filenames]\n", options);
    (void)fprintf(stderr, "Use \"hexbin -H\" for help.\n");
}