File: hecx.c

package info (click to toggle)
macutils 2.0b3-17
  • links: PTS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,256 kB
  • sloc: ansic: 12,737; makefile: 661
file content (256 lines) | stat: -rwxr-xr-x 5,468 bytes parent folder | download | duplicates (7)
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
#include "hexbin.h"
#ifdef HECX
#include "globals.h"
#include "crc.h"
#include "readline.h"
#include "../util/masks.h"
#include "../util/util.h"
#include "../fileio/machdr.h"
#include "../fileio/wrfile.h"
#include "buffer.h"
#include "printhdr.h"

extern void exit();

static void do_o_forks();
static long make_file();
static void comp_c_crc();
static void comp_e_crc();
static int comp_to_bin();
static int hex_to_bin();
static int hexit();

static int compressed;

/* old format -- process .hex and .hcx files */
void hecx(macname, filename)
char *macname, *filename;
{
    int n;

    for(n = 0; n < INFOBYTES; n++) {
	info[n] = 0;
    }
    compressed = 0;
    /* set up name for output files */
    if(macname[0] == '\0') {
	/* strip directories */
	macname = search_last(filename, '/');
	if(macname == NULL) {
	    macname = filename;
	} else {
	    macname++;
	}

	/* strip extension */
	n = strlen(macname);
	if(n > 4) {
	    n -= 4;
	    if(!strncmp(macname + n, ".hex", 4) ||
	       !strncmp(macname + n, ".hcx", 4)) {
		macname[n] = '\0';
	    }
	}
    }
    n = strlen(macname);
    if(n > F_NAMELEN) {
	n = F_NAMELEN;
    }
    (void)strncpy(mh.m_name, macname, n);
    mh.m_name[n] = '\0';

    /* "#TYPEAUTH$flag"  line already read */
    n = strlen(line);
    if(n >= 6 && line[0] == '#' && line[n-5] == '$') {
	if(n >= 10) {
	    (void)strncpy(mh.m_type, &line[1], 4);
	}
	if(n >= 14) {
	    (void)strncpy(mh.m_author, &line[5], 4);
	}
	(void)sscanf(&line[n-4], "%4hx", &mh.m_flags);
    }
    transname(mh.m_name, trname, n);
    define_name(trname);
    do_o_forks();
    if(listmode) {
	if(!compressed) {
	    (void)fprintf(stderr, "This file is in \"hex\" format.\n");
	} else {
	    (void)fprintf(stderr, "This file is in \"hcx\" format.\n");
	}
    }
    print_header0(0);
    print_header1(0, 0);
    info[I_NAMEOFF] = n;
    (void)strncpy(info + I_NAMEOFF + 1, mh.m_name, n);
    (void)strncpy(info + I_TYPEOFF, mh.m_type, 4);
    (void)strncpy(info + I_AUTHOFF, mh.m_author, 4);
    put2(info + I_FLAGOFF, (unsigned long)mh.m_flags);
    put4(info + I_DLENOFF, (unsigned long)mh.m_datalen);
    put4(info + I_RLENOFF, (unsigned long)mh.m_rsrclen);
    put4(info + I_CTIMOFF, (unsigned long)mh.m_createtime);
    put4(info + I_MTIMOFF, (unsigned long)mh.m_modifytime);
    print_header2(0);
    end_put();
}

static void do_o_forks()
{
    int forks = 0, found_crc = 0;
    unsigned long calc_crc, file_crc;

    crc = 0;    /* calculate a crc for both forks */

    set_put(0);
    set_put(1);
    while(!found_crc && readline()) {
	if(line[0] == 0) {
	    continue;
	}
	if(forks == 0 && strncmp(line, "***COMPRESSED", 13) == 0) {
	    compressed++;
	    continue;
	}
	if(strncmp(line, "***DATA", 7) == 0) {
	    set_put(1);
	    mh.m_datalen = make_file(compressed);
	    forks++;
	    continue;
	}
	if(strncmp(line, "***RESOURCE", 11) == 0) {
	    set_put(0);
	    mh.m_rsrclen = make_file(compressed);
	    forks++;
	    continue;
	}
	if(compressed && strncmp(line, "***CRC:", 7) == 0) {
	    found_crc++;
	    calc_crc = crc;
	    (void)sscanf(&line[7], "%lx", &file_crc);
	    break;
	}
	if(!compressed && strncmp(line, "***CHECKSUM:", 12) == 0) {
	    found_crc++;
	    calc_crc = crc & BYTEMASK;
	    (void)sscanf(&line[12], "%lx", &file_crc);
	    file_crc &= BYTEMASK;
	    break;
	}
    }

    if(found_crc) {
	verify_crc(calc_crc, file_crc);
    } else {
	(void)fprintf(stderr, "missing CRC\n");
#ifdef SCAN
	do_error("hexbin: missing CRC");
#endif /* SCAN */
	exit(1);
    }
}

static long make_file(compressed)
int compressed;
{
    register long nbytes = 0L;

    while(readline()) {
	if(line[0] == 0) {
	    continue;
	}
	if(strncmp(line, "***END", 6) == 0) {
	    break;
	}
	if(compressed) {
	    nbytes += comp_to_bin();
	} else {
	    nbytes += hex_to_bin();
	}
    }
    return nbytes;
}

static void comp_c_crc(c)
unsigned char c;
{
    crc = (crc + c) & WORDMASK;
    crc = ((crc << 3) & WORDMASK) | (crc >> 13);
}

static void comp_e_crc(c)
unsigned char c;
{
    crc += c;
}

#define SIXB(c) (((c)-0x20) & 0x3f)

static int comp_to_bin()
{
    char obuf[BUFSIZ];
    register char *ip = line;
    register char *op = obuf;
    register int n, outcount;
    int numread, incount;

    numread = strlen(line);
    outcount = (SIXB(ip[0]) << 2) | (SIXB(ip[1]) >> 4);
    incount = ((outcount / 3) + 1) * 4;
    for(n = numread; n < incount; n++) {  /* restore lost spaces */
	line[n] = ' ';
    }

    n = 0;
    while(n <= outcount) {
	*op++ = SIXB(ip[0]) << 2 | SIXB(ip[1]) >> 4;
	*op++ = SIXB(ip[1]) << 4 | SIXB(ip[2]) >> 2;
	*op++ = SIXB(ip[2]) << 6 | SIXB(ip[3]);
	ip += 4;
	n += 3;
    }

    for(n = 1; n <= outcount; n++) {
	comp_c_crc((unsigned)obuf[n]);
	put_byte(obuf[n]);
    }
    return outcount;
}

static int hex_to_bin()
{
    register char *ip = line;
    register int n, outcount;
    int c;

    n = strlen(line);
    outcount = n / 2;
    for(n = 0; n < outcount; n++) {
	c = hexit((int)*ip++);
	comp_e_crc((unsigned)(c = (c << 4) | hexit((int)*ip++)));
	put_byte((char)c);
    }
    return outcount;
}

static int hexit(c)
int c;
{
    if('0' <= c && c <= '9') {
	return c - '0';
    }
    if('A' <= c && c <= 'F') {
	return c - 'A' + 10;
    }

    (void)fprintf(stderr, "illegal hex digit: %c", c);
#ifdef SCAN
    do_error("hexbin: illegal hex digit");
#endif /* SCAN */
    exit(1);
    /* NOTREACHED */
}
#else /* HECX */
int hecx; /* keep lint and some compilers happy */
#endif /* HECX */