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
|
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "xex.h"
int xex_errno;
static int xex_sys_errno;
int xex_verbose = 0;
static char *errors[] = {
"OK", /* XERR_NONE 0 */
"Failed system call", /* XERR_SYSCALL 1 */
"End address < start address", /* XERR_REVERSED 2 */
"Truncated segment", /* XERR_TRUNCATED 3 */
"No data", /* XERR_NULLDATA 4 */
"Unknown/Invalid XEX error code", /* XERR_MAXERR 5 */
};
int xex_new_seg(xex_segment *seg, unsigned char *data) {
int offset = 0;
if(data[0] == 0xff && data[1] == 0xff) {
offset = 2;
seg->has_ff_header = 1;
} else {
seg->has_ff_header = 0;
}
seg->start_addr = data[offset] + (data[offset + 1] << 8);
offset += 2;
seg->end_addr = data[offset] + (data[offset + 1] << 8);
offset += 2;
seg->object = &data[offset];
seg->len = seg->end_addr - seg->start_addr + 1;
return xex_check_seg(seg);
}
int xex_init_seg(xex_segment *seg, unsigned char *object, unsigned short addr) {
object[0] = XEX_LSB(addr);
object[1] = XEX_MSB(addr);
seg->start_addr = XEX_INITAD;
seg->end_addr = XEX_INITAD + 1;
seg->object = object;
seg->len = 2;
seg->has_ff_header = 0;
if(xex_verbose) {
fprintf(stderr, "xex_init_seg(): created init segment @ %04X:\n", addr);
xex_print_seg_info(seg);
}
return 1;
}
int xex_run_seg(xex_segment *seg, unsigned char *object, unsigned short addr) {
object[0] = XEX_LSB(addr);
object[1] = XEX_MSB(addr);
seg->start_addr = XEX_RUNAD;
seg->end_addr = XEX_RUNAD + 1;
seg->object = object;
seg->len = 2;
seg->has_ff_header = 0;
if(xex_verbose) {
fprintf(stderr, "xex_run(): created run segment @ %04X:\n", addr);
xex_print_seg_info(seg);
}
return 1;
}
static int read_char(FILE *file) {
int c;
c = getc(file);
if(c == EOF) {
if(ferror(file)) {
xex_sys_errno = errno;
xex_errno = XERR_SYSCALL;
} else {
xex_errno = XERR_TRUNCATED;
}
}
return c;
}
int xex_fread_seg_header(xex_segment *seg, FILE *file) {
int c, d;
unsigned short addr;
xex_errno = XERR_NONE;
seg->has_ff_header = 0;
seg->object = NULL;
c = read_char(file);
if(c == EOF) {
if(feof(file))
xex_errno = XERR_NONE;
return 0;
}
d = read_char(file);
if(d == EOF) return 0;
addr = XEX_ADDR(c, d);
if(addr == 0x0984) {
fprintf(stderr, "xex_fread_seg_header(): warning: this might be a DOS 1.0 file (not supported)\n");
}
if(addr == 0xffff) {
seg->has_ff_header = 1;
c = read_char(file);
if(c == EOF) return 0;
d = read_char(file);
if(d == EOF) return 0;
addr = XEX_ADDR(c, d);
}
if(addr == 0xffff) {
xex_errno = XERR_REVERSED;
return 0;
}
seg->start_addr = addr;
c = read_char(file);
if(c == EOF) return 0;
d = read_char(file);
if(d == EOF) return 0;
addr = XEX_ADDR(c, d);
seg->end_addr = addr;
seg->len = seg->end_addr - seg->start_addr + 1;
if(seg->end_addr < seg->start_addr) {
xex_errno = XERR_REVERSED;
return 0;
}
if(xex_verbose) {
fprintf(stderr, "xex_fread_seg_header(): read header:\n");
xex_print_seg_info(seg);
}
return 1;
}
int xex_fread_seg_data(xex_segment *seg, FILE *file) {
int res;
xex_errno = XERR_NONE;
res = fread(seg->object, 1, seg->len, file);
xex_sys_errno = errno;
if(xex_verbose) {
fprintf(stderr, "xex_fread_seg_data(): read data, len==%d, res==%d:\n", seg->len, res);
xex_print_seg_info(seg);
}
if(res == seg->len) {
return 1;
}
if(res < 0 || ferror(file)) {
xex_errno = XERR_SYSCALL;
} else {
/* EOF or short read */
xex_errno = XERR_TRUNCATED;
seg->len = res;
}
return xex_check_seg(seg);
}
int xex_fread_seg(xex_segment *seg, FILE *file) {
unsigned char *tmp = seg->object;
int res;
xex_errno = XERR_NONE;
if(tmp == NULL) {
if(xex_verbose)
fprintf(stderr, "xex_fread_seg(): seg->object == NULL\n");
xex_errno = XERR_NULLDATA;
return 0;
}
res = xex_fread_seg_header(seg, file);
seg->object = tmp;
if(!res)
return 0;
if(!xex_fread_seg_data(seg, file))
return 0;
return 1;
}
int xex_fwrite_seg(xex_segment *seg, FILE *file) {
int res;
xex_errno = XERR_NONE;
if(xex_verbose) {
fprintf(stderr, "xex_fwrite_seg(): about to write:\n");
xex_print_seg_info(seg);
}
if(!xex_check_seg(seg))
return 0;
if(seg->has_ff_header) {
putc(0xff, file);
putc(0xff, file);
}
putc(XEX_LSB(seg->start_addr), file);
putc(XEX_MSB(seg->start_addr), file);
putc(XEX_LSB(seg->end_addr), file);
putc(XEX_MSB(seg->end_addr), file);
res = fwrite(seg->object, 1, seg->len, file);
if(res == seg->len)
return 1;
if(ferror(file)) {
xex_errno = XERR_SYSCALL;
xex_sys_errno = errno;
} else {
xex_errno = XERR_TRUNCATED;
}
return 0;
}
int xex_get_object(xex_segment *seg, unsigned char *data) {
int offset = 0;
if(!xex_check_seg(seg))
return 0;
if(seg->has_ff_header) {
data[0] = data[1] = 0xff;
offset = 2;
}
data[offset] = XEX_LSB(seg->start_addr);
data[offset + 1] = XEX_MSB(seg->start_addr);
offset += 2;
data[offset] = XEX_LSB(seg->end_addr);
data[offset + 1] = XEX_MSB(seg->end_addr);
offset += 2;
memcpy(&data[offset], seg->object, seg->len);
return 1;
}
int xex_check_seg(xex_segment *seg) {
int ret = 1;
xex_errno = XERR_NONE;
if(seg->end_addr < seg->start_addr) {
xex_errno = XERR_REVERSED;
ret = 0;
}
if(seg->len != (seg->end_addr - seg->start_addr) + 1) {
xex_errno = XERR_TRUNCATED;
ret = 0;
}
if(seg->object == NULL) {
xex_errno = XERR_NULLDATA;
ret = 0;
}
if(xex_verbose && !ret && xex_errno != XERR_NULLDATA)
fprintf(stderr, "xex_check_seg() FAILED: %s\n", xex_strerror(xex_errno));
return ret;
}
static int xex_get_addr_from(xex_segment *seg, int location) {
int i;
if(seg->start_addr > location) return -1;
if(seg->end_addr <= location) return -1;
i = location - seg->start_addr;
return XEX_ADDR(seg->object[i], seg->object[i + 1]);
}
int xex_get_run_addr(xex_segment *seg) {
return xex_get_addr_from(seg, XEX_RUNAD);
}
int xex_get_init_addr(xex_segment *seg) {
return xex_get_addr_from(seg, XEX_INITAD);
}
char *xex_strerror(int err) {
if(err < 0 || err >= XERR_MAXERR)
err = XERR_MAXERR;
if(err == XERR_SYSCALL)
return strerror(xex_sys_errno);
else
return errors[err];
}
void xex_perror(char *msg) {
fprintf(stderr, "%s: %s\n", msg, xex_strerror(xex_errno));
}
void xex_print_seg_info(xex_segment *seg) {
xex_check_seg(seg);
fprintf(stderr,
"has_ff_header==%d start==%04X end==%04X "
"len==%04X status==%s\n",
seg->has_ff_header, seg->start_addr, seg->end_addr,
seg->len, xex_strerror(xex_errno));
}
|