summaryrefslogtreecommitdiff
path: root/xpp/hexfile.c
blob: 1227b264560231fb6a4313ed0bb91580ee37238f (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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
 * Written by Oron Peled <oron@actcom.co.il>
 * Copyright (C) 2006, 2007, 2008, Xorcom
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include "hexfile.h"

static const char rcsid[] = "$Id$";

static parse_hexfile_report_func_t	report_func = NULL;

parse_hexfile_report_func_t parse_hexfile_set_reporting(parse_hexfile_report_func_t rf)
{
	parse_hexfile_report_func_t	old_rf = report_func;
	report_func = rf;
	return old_rf;
}

static void chomp(char buf[])
{
	size_t	last = strlen(buf) - 1;
	while(last >= 0 && isspace(buf[last]))
		buf[last--] = '\0';
}

static int hexline_checksum(struct hexline *hexline)
{
	unsigned int	i;
	unsigned int	chksm = 0;
	int		ll = hexline->d.content.header.ll;

	for(i = 0; i <= sizeof(hexline->d.content.header) + ll; i++) {
		chksm += hexline->d.raw[i];
	}
	return chksm & 0xFF;
}

int dump_hexline(int recordno, struct hexline *line, FILE *fp)
{
	uint8_t		ll;
	uint16_t	offset;
	uint8_t		tt;
	uint8_t		old_chksum;
	uint8_t		new_chksum;
	uint8_t		*data;
	unsigned int	i;

	ll = line->d.content.header.ll;
	offset = line->d.content.header.offset;
	tt = line->d.content.header.tt;
	fprintf(fp, ":%02X%04X%02X", ll, offset, tt);
	data = line->d.content.tt_data.data;
	for(i = 0; i < ll; i++) {
		fprintf(fp, "%02X", data[i]);
	}
	old_chksum = data[ll];
	data[ll] = 0;
	new_chksum = 0xFF - hexline_checksum(line) + 1;
	data[ll] = old_chksum;
	fprintf(fp, "%02X\n", new_chksum);
	if(new_chksum != old_chksum) {
		if(report_func)
			report_func(LOG_ERR, "record #%d: new_chksum(%02X) != old_chksum(%02X)\n",
					recordno, new_chksum, old_chksum);
		return 0;
	}
	return 1;
}

struct hexline	*new_hexline(uint8_t datalen, uint16_t offset, uint8_t tt)
{
	struct hexline	*hexline;
	size_t		allocsize;

	allocsize = sizeof(struct hexline) + datalen + 1; /* checksum byte */
	if((hexline = malloc(allocsize)) == NULL) {
		if(report_func)
			report_func(LOG_ERR, "No more memory\n");
		return NULL;
	}
	memset(hexline, 0, allocsize);
	hexline->d.content.header.ll = datalen;
	hexline->d.content.header.offset = offset;
	hexline->d.content.header.tt = tt;
	return hexline;
}

static int append_hexline(struct hexdata *hexdata, char *buf)
{
	int		ret;
	unsigned int	ll, offset, tt;
	char		*p;
	struct hexline	*hexline;
	unsigned int	i;

	if(hexdata->got_eof) {
		if(report_func)
			report_func(LOG_ERR, "Extranous data after EOF record\n");
		return -EINVAL;
	}
	if(hexdata->last_line >= hexdata->maxlines) {
		if(report_func)
			report_func(LOG_ERR, "Hexfile too large (maxline %d)\n", hexdata->maxlines);
		return -ENOMEM;
	}
	ret = sscanf(buf, "%02X%04X%02X", &ll, &offset, &tt);
	if(ret != 3) {
		if(report_func)
			report_func(LOG_ERR, "Bad line header (only %d items out of 3 parsed)\n", ret);
		return -EINVAL;
	}
	switch(tt) {
		case TT_DATA:
			break;
		case TT_EOF:
			if(ll != 0) {
				if(report_func)
					report_func(LOG_ERR,
						"%d: Record %d(EOF): Bad len = %d\n",
						hexdata->last_line, tt, ll);
				return -EINVAL;
			}
			if(offset != 0) {
				if(report_func)
					report_func(LOG_ERR,
						"%d: Record %d(EOF): Bad offset = %d\n",
						hexdata->last_line, tt, offset);
				return -EINVAL;
			}
			hexdata->got_eof = 1;
			break;
		case TT_EXT_SEG:
			if(ll != 2) {
				if(report_func)
					report_func(LOG_ERR,
						"%d: Record %d(EXT_SEG): Bad len = %d\n",
						hexdata->last_line, tt, ll);
				return -EINVAL;
			}
			if(offset != 0) {
				if(report_func)
					report_func(LOG_ERR,
						"%d: Record %d(EXT_SEG): Bad offset = %d\n",
						hexdata->last_line, tt, offset);
				return -EINVAL;
			}
			break;
		case TT_START_SEG:
			if(ll != 4) {
				if(report_func)
					report_func(LOG_ERR,
						"%d: Record %d(START_SEG): Bad len = %d\n",
						hexdata->last_line, tt, ll);
				return -EINVAL;
			}
			if(offset != 0) {
				if(report_func)
					report_func(LOG_ERR,
						"%d: Record %d(START_SEG): Bad offset = %d\n",
						hexdata->last_line, tt, offset);
				return -EINVAL;
			}
			break;
		case TT_EXT_LIN:
			if(ll != 2) {
				if(report_func)
					report_func(LOG_ERR,
						"%d: Record %d(EXT_LIN): Bad len = %d\n",
						hexdata->last_line, tt, ll);
				return -EINVAL;
			}
			if(offset != 0) {
				if(report_func)
					report_func(LOG_ERR,
						"%d: Record %d(EXT_LIN): Bad offset = %d\n",
						hexdata->last_line, tt, ll);
				return -EINVAL;
			}
			break;
		case TT_START_LIN:	/* Unimplemented */
			if(ll != 4) {
				if(report_func)
					report_func(LOG_ERR,
						"%d: Record %d(EXT_LIN): Bad len = %d\n",
						hexdata->last_line, tt, ll);
				return -EINVAL;
			}
			if(offset != 0) {
				if(report_func)
					report_func(LOG_ERR,
						"%d: Record %d(EXT_LIN): Bad offset = %d\n",
						hexdata->last_line, tt, ll);
				return -EINVAL;
			}
			break;
		default:
			if(report_func)
				report_func(LOG_ERR, "%d: Unimplemented record type %d: %s\n",
					hexdata->last_line, tt, buf);
			return -EINVAL;
	}
	buf += 8;	/* Skip header */
	if((hexline = new_hexline(ll, offset, tt)) == NULL) {
		if(report_func)
			report_func(LOG_ERR, "No more memory for hexfile lines\n");
		return -EINVAL;
	}
	p = buf;
	for(i = 0; i < ll + 1; i++) {	/* include checksum */
		unsigned int	val;

		if((*p == '\0') || (*(p+1) == '\0')) {
			if(report_func)
				report_func(LOG_ERR, "Short data string '%s'\n", buf);
			return -EINVAL;
		}
		ret = sscanf(p, "%02X", &val);
		if(ret != 1) {
			if(report_func)
				report_func(LOG_ERR, "Bad data byte #%d\n", i);
			return -EINVAL;
		}
		hexline->d.content.tt_data.data[i] = val;
		p += 2;
	}
	if(hexline_checksum(hexline) != 0) {
		if(report_func) {
			report_func(LOG_ERR, "Bad checksum (%d instead of 0)\n",
				hexline_checksum(hexline));
			dump_hexline(hexdata->last_line, hexline, stderr);
		}
		return -EINVAL;
	}
	hexdata->lines[hexdata->last_line] = hexline;
	if(hexdata->got_eof)
		return 0;
	hexdata->last_line++;
	return 1;
}

void free_hexdata(struct hexdata *hexdata)
{
	if(hexdata) {
		unsigned int	i;

		for(i = 0; i < hexdata->maxlines; i++)
			if(hexdata->lines[i] != NULL)
				free(hexdata->lines[i]);
		free(hexdata);
	}
}

int dump_hexfile(struct hexdata *hexdata, const char *outfile)
{
	FILE		*fp;
	unsigned int	i;

	if(report_func)
		report_func(LOG_INFO, "Dumping hex data into '%s'\n", outfile);
	if(!outfile || strcmp(outfile, "-") == 0)
		fp = stdout;
	else if((fp = fopen(outfile, "w")) == NULL) {
		perror(outfile);
		exit(1);
	}
	for(i = 0; i <= hexdata->last_line; i++) {
		struct hexline	*line = hexdata->lines[i];
		if(!line) {
			if(report_func)
				report_func(LOG_ERR, "Missing line at #%d\n", i);
			return -EINVAL;
		}
		if(!dump_hexline(i, line, fp))
			return -EINVAL;
	}
	return 0;
}

int dump_hexfile2(struct hexdata *hexdata, const char *outfile, uint8_t maxwidth)
{
	FILE		*fp;
	uint8_t		tt;
	unsigned int	i;
	struct hexline	*line;

	if(report_func)
		report_func(LOG_INFO,
			"Dumping hex data into '%s' (maxwidth=%d)\n",
			outfile, maxwidth);
	if(!outfile || strcmp(outfile, "-") == 0)
		fp = stdout;
	else if((fp = fopen(outfile, "w")) == NULL) {
		perror(outfile);
		exit(1);
	}
	if(maxwidth == 0)
		maxwidth = UINT8_MAX;
	for(i = 0; i <= hexdata->last_line; i++) {
		int		bytesleft = 0;
		int		extra_offset = 0;
		int		base_offset;
		uint8_t		*base_data;
		
		line = hexdata->lines[i];
		if(!line) {
			if(report_func)
				report_func(LOG_ERR, "Missing line at #%d\n", i);
			return -EINVAL;
		}
		bytesleft = line->d.content.header.ll;
		/* split the line into several lines */
		tt = line->d.content.header.tt;
		base_offset = line->d.content.header.offset;
		base_data = line->d.content.tt_data.data;
		while (bytesleft > 0) {
			struct hexline	*extraline;
			uint8_t		new_chksum;
			unsigned int	curr_bytes = (bytesleft >= maxwidth) ? maxwidth : bytesleft;

			/* generate the new line */
			if((extraline = new_hexline(curr_bytes, base_offset + extra_offset, tt)) == NULL) {
				if(report_func)
					report_func(LOG_ERR, "No more memory for hexfile lines\n");
				return -EINVAL;
			}
			memcpy(extraline->d.content.tt_data.data, base_data + extra_offset, curr_bytes);
			new_chksum = 0xFF - hexline_checksum(extraline) + 1;
			extraline->d.content.tt_data.data[curr_bytes] = new_chksum;
			/* print it */
			dump_hexline(i, extraline, fp);
			/* cleanups */
			free(extraline);
			extra_offset += curr_bytes;
			bytesleft -= curr_bytes;
		}
	}
	if(tt != TT_EOF) {
		if(report_func)
			report_func(LOG_ERR, "Missing EOF record\n");
		return -EINVAL;
	}
	dump_hexline(i, line, fp);
	return 0;
}

void process_comment(struct hexdata *hexdata, char buf[])
{
	char		*dollar_start;
	char		*dollar_end;
	const char	id_prefix[] = "Id: ";
	char		tmp[BUFSIZ];
	char		*p;
	int		len;

	if(report_func)
		report_func(LOG_INFO, "Comment: %s\n", buf + 1);
	/* Search for RCS keywords */
	if((dollar_start = strchr(buf, '$')) == NULL)
		return;
	if((dollar_end = strchr(dollar_start + 1, '$')) == NULL)
		return;
	/* Crop the '$' signs */
	len = dollar_end - dollar_start;
	len -= 2;
	memcpy(tmp, dollar_start + 1, len);
	tmp[len] = '\0';
	p = tmp;
	if(strstr(tmp, id_prefix) == NULL)
		return;
	p += strlen(id_prefix);
	if((p = strchr(p, ' ')) == NULL)
		return;
	p++;
	snprintf(hexdata->version_info, BUFSIZ, "%s", p);
	if((p = strchr(hexdata->version_info, ' ')) != NULL)
		*p = '\0';
}

struct hexdata *parse_hexfile(const char *fname, unsigned int maxlines)
{
	FILE		*fp;
	struct hexdata	*hexdata = NULL;
	int		datasize;
	char		buf[BUFSIZ];
	int		line;
	int		dos_eof = 0;
	int		ret;

	assert(fname != NULL);
	if(report_func)
		report_func(LOG_INFO, "Parsing %s\n", fname);
	datasize = sizeof(struct hexdata) + maxlines * sizeof(char *);
	hexdata = (struct hexdata *)malloc(datasize);
	if(!hexdata) {
		if(report_func)
			report_func(LOG_ERR, "Failed to allocate %d bytes for hexfile contents\n", datasize);
		goto err;
	}
	memset(hexdata, 0, datasize);
	hexdata->maxlines = maxlines;
	if((fp = fopen(fname, "r")) == NULL) {
		if(report_func)
			report_func(LOG_ERR, "Failed to open hexfile '%s'\n", fname);
		goto err;
	}
	snprintf(hexdata->fname, PATH_MAX, "%s", fname);
	for(line = 1; fgets(buf, BUFSIZ, fp); line++) {
		if(dos_eof) {
			if(report_func)
				report_func(LOG_ERR, "%s:%d - Got DOS EOF character before true EOF\n", fname, line);
			goto err;
		}
		if(buf[0] == 0x1A && buf[1] == '\0') { /* DOS EOF char */
			dos_eof = 1;
			continue;
		}
		chomp(buf);
		if(buf[0] == '\0') {
				if(report_func)
					report_func(LOG_ERR, "%s:%d - Short line\n", fname, line);
				goto err;
		}
		if(buf[0] == '#') {
			process_comment(hexdata, buf);
			continue;
		}
		if(buf[0] != ':') {
			if(report_func)
				report_func(LOG_ERR, "%s:%d - Line begins with 0x%X\n", fname, line, buf[0]);
			goto err;
		}
		if((ret = append_hexline(hexdata, buf + 1)) < 0) {
			if(report_func)
				report_func(LOG_ERR, "%s:%d - Failed parsing.\n", fname, line);
			goto err;
		}
	}
	fclose(fp);
	if(report_func)
		report_func(LOG_INFO, "%s parsed OK\n", fname);
	return hexdata;
err:
	free_hexdata(hexdata);
	return NULL;
}

void dump_binary(struct hexdata *hexdata, const char *outfile)
{
	FILE		*fp;
	unsigned int	i;
	size_t		len;

	if(report_func)
		report_func(LOG_INFO, "Dumping binary data into '%s'\n", outfile);
	if((fp = fopen(outfile, "w")) == NULL) {
		perror(outfile);
		exit(1);
	}
	for(i = 0; i < hexdata->maxlines; i++) {
		struct hexline	*hexline = hexdata->lines[i];

		if(!hexline)
			break;
		switch(hexline->d.content.header.tt) {
		case TT_EOF:
			if(report_func)
				report_func(LOG_INFO, "\ndump: good EOF record");
			break;
		case TT_DATA:
			if(report_func)
				report_func(LOG_INFO, "dump: %6d\r", i);
			len = hexline->d.content.header.ll;
			if(fwrite(hexline->d.content.tt_data.data, 1, len, fp) != len) {
				perror("write");
				exit(1);
			}
			break;
		case TT_EXT_SEG:
		case TT_START_SEG:
		case TT_EXT_LIN:
		case TT_START_LIN:
			if(report_func)
				report_func(LOG_INFO,
					"\ndump(%d): ignored record type %d",
					i, hexline->d.content.header.tt);
			break;
		default:
			if(report_func)
				report_func(LOG_ERR, "dump: Unknown record type %d\n",
					hexline->d.content.header.tt);
			exit(1);
		}
	}
	if(report_func)
		report_func(LOG_INFO, "\nDump finished\n");
	fclose(fp);
}

void gen_hexline(const uint8_t *data, uint16_t addr, size_t len, FILE *output)
{
	struct hexline	*hexline;

	if(!data) {
		fprintf(output, ":%02X%04X%02XFF\n", 0, 0, TT_EOF);
		return;
	}
	if((hexline = new_hexline(len, addr, (!data) ? TT_EOF : TT_DATA)) == NULL) {
		if(report_func)
			report_func(LOG_ERR, "No more memory\n");
		return;
	}
	if(data)
		memcpy(&hexline->d.content.tt_data, data, len);
	dump_hexline(0, hexline, output);
	free(hexline);
}

/*
 * Algorithm lifted of sum(1) implementation from coreutils.
 * We chose the default algorithm (BSD style).
 */
int bsd_checksum(struct hexdata *hexdata)
{
	unsigned int	i;
	size_t		len;
	int		ck = 0;

	for(i = 0; i < hexdata->maxlines; i++) {
		struct hexline	*hexline = hexdata->lines[i];
		unsigned char	*p;

		if(!hexline)
			break;
		if(hexline->d.content.header.tt == TT_EOF)
			continue;
		len = hexline->d.content.header.ll;
		p = hexline->d.content.tt_data.data;
		for(; len; p++, len--) {
			ck = (ck >> 1) + ((ck & 1) << 15);
			ck += *p;
			ck &= 0xffff;	/* Keep it within bounds. */
		}
	}
	return ck;
}