-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathihr.c
406 lines (388 loc) · 9.16 KB
/
ihr.c
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
#include "ihr.h"
#define SUCCESS 0
#define FAILURE -1
/* Convert a hex digit to its integer form, or -1 if the given is not hex.
* TODO: Do we need to accept lowercase hex? */
static int read_nibble(char hex)
{
if ('0' <= hex && hex <= '9') return hex - '0';
hex |= 0x20; /* Normalize to lowercase */
if ('a' <= hex && hex <= 'f') return 10 + hex - 'a';
return FAILURE;
}
/* Convert two hex digits to an unsigned byte, or -1 if a digit was invalid. */
static int read_u8(const char hex[2])
{
return (read_nibble(hex[0]) << 4) | read_nibble(hex[1]);
}
/* Returns 1 if the type is valid for the given file type or 0 otherwise. */
static int ihex_valid_type(int file_type, IHR_U8 type)
{
switch (type) {
case IHRR_I_DATA:
case IHRR_I_END_OF_FILE:
return 1;
case IHRR_I_EXT_SEG_ADDR:
case IHRR_I_START_SEG_ADDR:
return file_type == IHRT_I16;
case IHRR_I_EXT_LIN_ADDR:
case IHRR_I_START_LIN_ADDR:
return file_type == IHRT_I32;
default:
return 0;
}
}
/* Returns 1 if the type is valid for the given file type or 0 otherwise. */
static int srec_valid_type(int file_type, IHR_U8 type)
{
switch (type) {
case IHRR_S0_HEADER:
case IHRR_S5_COUNT_16:
return 1;
case IHRR_S1_DATA_16:
case IHRR_S9_START_16:
return file_type == IHRT_S19;
case IHRR_S2_DATA_24:
case IHRR_S8_START_24:
return file_type == IHRT_S28;
case IHRR_S3_DATA_32:
case IHRR_S7_START_32:
return file_type == IHRT_S37;
case IHRR_S6_COUNT_24:
return file_type != IHRT_S19;
default:
return 0;
}
}
/* Choose an error code to suit a pair of unparseable digits. Returns
* -IHRE_INVALID_SIZE if the pair was a line break (the record was shorter than
* indicated,) or -IHRE_NOT_HEX otherwise. */
static int invalid_hex_error(const char *pair)
{
switch (pair[0]) {
case '\r':
case '\n':
return -IHRE_INVALID_SIZE;
default:
return -IHRE_NOT_HEX;
}
}
static int find_line_end(const char *text,
size_t len,
size_t *idx,
struct ihr_record *rec)
{
if (*idx < len) {
switch (text[*idx]) {
case '\n':
break;
case '\r':
++*idx;
if (*idx >= len || text[*idx] != '\n')
--*idx;
break;
default:
if (rec->size < IHR_MAX_SIZE)
rec->type = -IHRE_INVALID_SIZE;
else
rec->type = -IHRE_EXPECTED_EOL;
return FAILURE;
}
++*idx;
}
return SUCCESS;
}
static int read_data(const char *text, size_t *idx, struct ihr_record *rec)
{
int status = SUCCESS;
const char *hex = text + *idx;
IHR_U8 i;
for (i = 0; i < rec->size; ++i) {
const char *pair = hex + i * 2;
int byte = read_u8(pair);
if (byte < 0) {
rec->type = invalid_hex_error(pair);
status = FAILURE;
break;
}
rec->data.data[i] = byte;
}
*idx += (size_t)i * 2;
return status;
}
static int ihex_read(int file_type,
size_t len,
const char *text,
struct ihr_record *rec)
{
size_t idx = 0;
int read_cksum;
/* Check that the given text can be a valid record: */
if (len < IHR_I_MIN_LENGTH) {
rec->type = -IHRE_SUB_MIN_LENGTH;
goto error;
}
/* Check for record beginning colon: */
if (text[idx] != ':') {
rec->type = -IHRE_MISSING_START;
goto error;
} else {
idx += 1;
}
/* Read in fields at record beginning (size, address, type): */
{
int size, addr, type;
size = read_u8(text + idx);
if (size < 0) goto error_not_hex;
rec->size = size;
idx += 2;
addr = read_u8(text + idx);
if (addr < 0) goto error_not_hex;
rec->addr = addr << 8;
idx += 2;
addr = read_u8(text + idx);
if (addr < 0) goto error_not_hex;
rec->addr |= addr;
idx += 2;
type = read_u8(text + idx);
if (type < 0) goto error_not_hex;
rec->type = type;
if (!ihex_valid_type(file_type, type)) {
rec->type = -IHRE_INVALID_TYPE;
goto error;
}
idx += 2;
}
/* Read data field: */
{
int min_size;
if (len < idx + ((size_t)rec->size + 1) * 2)
goto error_invalid_size;
if (rec->type == IHRR_I_END_OF_FILE) {
if (rec->size != 0) goto error_invalid_size;
} else {
switch (rec->type) {
case IHRR_I_EXT_SEG_ADDR:
case IHRR_I_EXT_LIN_ADDR:
min_size = 2;
break;
case IHRR_I_START_SEG_ADDR:
case IHRR_I_START_LIN_ADDR:
min_size = 4;
break;
default:
min_size = -1;
break;
}
if ((int)rec->size < min_size) {
rec->type = -IHRE_INVALID_SIZE;
goto error_invalid_size;
}
if (read_data(text, &idx, rec)) goto error;
}
}
/* Read in the checksum (verification comes later): */
if ((read_cksum = read_u8(text + idx)) < 0) {
rec->type = invalid_hex_error(text + idx);
goto error;
} else {
idx += 2;
}
if (find_line_end(text, len, &idx, rec)) goto error;
/* Verify checksum: */
{
/* The checksum is the two's complement of the least significant
* byte of the sum of all preceding bytes. */
IHR_U8 right_cksum = 0;
IHR_U8 i = 0;
right_cksum += rec->size;
right_cksum += rec->addr >> 8;
right_cksum += rec->addr & 0xFF;
right_cksum += rec->type;
for (i = 0; i < rec->size; ++i) {
right_cksum += rec->data.data[i];
}
right_cksum = (~right_cksum + 1) & 0xFF;
if ((IHR_U8)read_cksum != right_cksum) {
rec->type = -IHRE_INVALID_CHECKSUM;
goto error;
}
}
/* Transfer data from rec->data.data to record-type-specific fields: */
{
IHR_U8 *data = rec->data.data;
switch (rec->type) {
case IHRR_I_DATA:
case IHRR_I_END_OF_FILE:
break;
case IHRR_I_EXT_SEG_ADDR:
case IHRR_I_EXT_LIN_ADDR:
rec->data.ihex.base_addr = ((IHR_U16)data[0] << 8)
| (IHR_U16)data[1];
break;
case IHRR_I_START_SEG_ADDR:
rec->data.ihex.start.code_seg = ((IHR_U16)data[0] << 8)
| (IHR_U16)data[1];
rec->data.ihex.start.instr_ptr = ((IHR_U16)data[2] << 8)
| (IHR_U16)data[3];
break;
case IHRR_I_START_LIN_ADDR:
rec->data.ihex.ext_instr_ptr = ((IHR_U32)data[0] << 24)
| ((IHR_U32)data[1] << 16)
| ((IHR_U32)data[2] << 8) | (IHR_U32)data[3];
break;
}
}
return idx;
error_invalid_size:
/* The reported size of the record was incorrect. */
rec->type = -IHRE_INVALID_SIZE;
return ~1;
error_not_hex:
/* A character which should have been a hex digit was not. */
rec->type = -IHRE_NOT_HEX;
return ~idx;
error:
/* Any other error. */
return ~idx;
}
IHR_U8 srec_addr_size(IHR_U8 type)
{
switch (type) {
case IHRR_S0_HEADER:
case IHRR_S1_DATA_16:
case IHRR_S5_COUNT_16:
case IHRR_S9_START_16:
return 2;
case IHRR_S2_DATA_24:
case IHRR_S6_COUNT_24:
case IHRR_S8_START_24:
return 3;
case IHRR_S3_DATA_32:
case IHRR_S7_START_32:
return 4;
}
return 2;
}
int srec_read(int file_type,
size_t len,
const char *text,
struct ihr_record *rec)
{
size_t idx = 0;
int addr_size;
int read_cksum;
/* Check that the given text can be a valid record: */
if (len < IHR_S_MIN_LENGTH) {
rec->type = -IHRE_SUB_MIN_LENGTH;
goto error;
}
/* Check for record beginning S: */
if (text[idx] != 'S') {
rec->type = -IHRE_MISSING_START;
goto error;
} else {
idx += 1;
}
/* Read in fields at record beginning (type, size, address): */
{
int i, size, type;
type = read_nibble(text[idx]);
if (type < 0) goto error_not_hex;
rec->type = type;
if (!srec_valid_type(file_type, type)) {
rec->type = -IHRE_INVALID_TYPE;
goto error;
}
addr_size = srec_addr_size(rec->type);
idx += 1;
size = read_u8(text + idx);
if (size < 0) goto error_not_hex;
size -= addr_size + 1;
if (size < 0) goto error_invalid_size;
rec->size = size;
idx += 2;
rec->addr = 0;
for (i = 0; i < addr_size; ++i) {
int addr = read_u8(text + idx);
if (addr < 0) goto error_not_hex;
idx += 2;
rec->addr |= addr;
rec->addr <<= 8;
}
}
/* Read data field: */
{
switch (rec->type) {
case IHRR_S0_HEADER:
case IHRR_S1_DATA_16:
case IHRR_S2_DATA_24:
case IHRR_S3_DATA_32:
if (len < idx + ((size_t)rec->size + 1) * 2)
goto error_invalid_size;
if (read_data(text, &idx, rec)) goto error;
break;
default:
if (rec->size != 0) goto error_invalid_size;
break;
}
}
/* Read in the checksum (verification comes later): */
if ((read_cksum = read_u8(text + idx)) < 0) {
rec->type = invalid_hex_error(text + idx);
goto error;
} else {
idx += 2;
}
if (find_line_end(text, len, &idx, rec)) goto error;
/* Verify checksum: */
{
/* The checksum is the one's complement of the least significant
* byte of the sum of all preceding bytes (not the type.) */
IHR_U8 right_cksum = 0;
IHR_U32 addr = rec->addr;
IHR_U8 i;
right_cksum += rec->size + addr_size + 1;
for (i = 0; i < addr_size; ++i) {
right_cksum += addr & 0xFF;
addr >>= 8;
}
for (i = 0; i < rec->size; ++i) {
right_cksum += rec->data.data[i];
}
right_cksum = ~right_cksum & 0xFF;
if ((IHR_U8)read_cksum != right_cksum) {
rec->type = -IHRE_INVALID_CHECKSUM;
goto error;
}
}
return idx;
error_invalid_size:
/* The reported size of the record was incorrect. */
rec->type = -IHRE_INVALID_SIZE;
return ~1;
error_not_hex:
/* A character which should have been a hex digit was not. */
rec->type = -IHRE_NOT_HEX;
return ~idx;
error:
/* Any other error. */
return ~idx;
}
int ihr_read(int file_type,
size_t len,
const char *text,
struct ihr_record *rec)
{
switch (file_type) {
case IHRT_I8:
case IHRT_I16:
case IHRT_I32:
return ihex_read(file_type, len, text, rec);
case IHRT_S19:
case IHRT_S28:
case IHRT_S37:
return srec_read(file_type, len, text, rec);
}
return FAILURE; /* It is undefined behavior to reach here. */
}