1 /*-----------------------------------------------------------------------
4 * utility to pack an Inter HEX format file by removing redundant
5 * extended offset records and accumulating data records up to
6 * OUTPUT_CHUNK (currently 16) bytes.
8 * Released to the public domain 10/16/2000 Kevin Vigor.
19 /* A cooked line of input. */
22 Uint8 len; /* length of data portion of record. */
29 /* Largest expected line of raw input. */
30 #define MAX_INPUT_RAW 128
31 /* Largest expected cooked data portion of an input line. */
32 #define MAX_INPUT_COOKED (MAX_INPUT_RAW / 2)
34 /* Globals: current input & output line numbers. */
38 /* Convert hex digit to numeric value 0 - 15; assumes input is a
39 * valid digit (i.e. passes isxdigit()).
41 static Uint8 hexDigit(const char c)
45 return (Uint8)(c - '0');
49 return (Uint8)((islower(c) ? toupper(c) : c) - 'A' + 10);
53 /* Convert two hex digits from cp to a byte value. */
54 static int getHexByte(const char *cp, Uint8 *byte)
56 if (cp && cp[0] && isxdigit(cp[0]) && cp[1] && isxdigit(cp[1]))
58 *byte = (hexDigit(cp[0]) << 4) + hexDigit(cp[1]);
67 /* Convert four hex digits from cp to a 2 byte value. */
68 static int getHexWord(const char *cp, Uint16 *word)
72 if (getHexByte(cp, &byte1) || getHexByte(cp + 2, &byte2))
76 *word = (byte1 << 8) + byte2;
80 /* Return a single cooked line of input from the passed file handle. */
81 Line *readLine(FILE *inFile)
83 static char buffer[MAX_INPUT_RAW];
88 line = (Line *)malloc(sizeof(Line));
91 fprintf(stderr, "packihx: no memory!\n");
97 if (!fgets(buffer, MAX_INPUT_RAW, inFile))
103 if (!buffer[0] || buffer[0] == '\r' || buffer[0] == '\n')
105 /* Empty input line. */
108 } while (buffer[0] != ':');
111 bp++; /* Skip leading : */
113 if (getHexByte(bp, &line->len))
115 fprintf(stderr, "packihx: can't read line length @ line %d\n",
120 bp += 2; /* Two digits consumed. */
122 if (line->len > MAX_INPUT_COOKED)
124 fprintf(stderr, "packihx: line length %X too long @ line %d\n",
125 (int)line->len, lineno);
130 if (getHexWord(bp, &line->offset))
132 fprintf(stderr, "packihx: can't read line offset @ line %d\n",
137 bp += 4; /* Four digits consumed. */
139 if (getHexByte(bp, &line->type))
141 fprintf(stderr, "packihx: can't read record type @ line %d\n",
146 bp += 2; /* Two digits consumed. */
148 line->data = (Uint8 *)malloc(line->len);
152 fprintf(stderr, "packihx: no memory!\n");
156 for (i = 0; i < (unsigned)line->len; i++)
158 if (getHexByte(bp, &(line->data[i])))
161 "packihx: can't read data byte %u of %u @ line %d\n",
162 i, (unsigned) line->len, lineno);
167 bp += 2; /* Two digits consumed. */
170 if (getHexByte(bp, &line->checksum))
172 fprintf(stderr, "packihx: can't read checksum @ line %d\n",
178 /* bp += 2; */ /* Two digits consumed. */
183 /* Compute the checksum of a line. */
184 Uint16 lineChecksum(unsigned len, unsigned offset, unsigned type,
190 checksum = len + type + (offset >> 8) +
193 for (i = 0; i < len; i++)
201 checksum = 0x100 - checksum;
206 /* Ensure that the checksum of a line matches the expected value. */
207 int validateChecksum(Line *line)
211 checksum = lineChecksum(line->len, line->offset, line->type, line->data);
213 if (checksum != line->checksum)
215 fprintf(stderr, "packihx: invalid checksum %X (want %X) @ line %d\n",
216 (unsigned)checksum, (unsigned)(line->checksum),
224 /* Write a single record line. */
225 int writeRecord(unsigned len, unsigned offset, unsigned type,
230 if (printf(":%02X%04X%02X", len, offset, type) == EOF)
235 for (i = 0; i < len; i++)
237 if (printf("%02X", data[i]) == EOF)
243 if (printf("%02X\n", lineChecksum(len, offset, type, data)) == EOF)
251 #define OUTPUT_CHUNK 16
252 static unsigned pendingLen = 0;
253 static unsigned pendingOffset = 0;
254 static char pending[MAX_INPUT_COOKED + OUTPUT_CHUNK];
256 /* Buffer up a data record. */
257 int bufferOutput(Line *line)
262 /* Stick the data onto any pending data. */
263 assert(pendingLen < OUTPUT_CHUNK);
264 memcpy(&pending[pendingLen], line->data, line->len);
265 pendingLen += line->len;
267 /* Write it out untill we have less than an OUTPUT_CHUNK left. */
268 while (!rc && pendingLen >= OUTPUT_CHUNK)
270 rc = writeRecord(OUTPUT_CHUNK, pendingOffset, 0, &pending[offset]);
271 offset += OUTPUT_CHUNK;
272 pendingOffset += OUTPUT_CHUNK;
273 pendingLen -= OUTPUT_CHUNK;
276 /* Copy any remaining bits back to the beginning of the buffer. */
279 memmove(pending, &pending[offset], pendingLen);
284 /* Write out any pending data. */
285 int flushPendingData(void)
289 assert(pendingLen < OUTPUT_CHUNK);
293 rc = writeRecord(pendingLen, pendingOffset, 0, pending);
294 pendingLen = pendingOffset = 0;
299 /* Write an arbitrary line of output (buffering if possible) */
300 int writeLine(Line *line)
302 static Uint16 lastExtendedOffset = 0;
307 /* Not a data record. */
312 /* Extended offset record. */
316 "packihx: invalid extended offset record @ line %d\n",
321 offset = (line->data[0] << 8) + line->data[1];
323 if (offset == lastExtendedOffset)
325 /* We can simply skip this line. */
330 lastExtendedOffset = offset;
334 if (flushPendingData())
339 /* Write the line as is. */
340 rc = writeRecord(line->len, line->offset, line->type, line->data);
344 if (pendingOffset + pendingLen != (unsigned)line->offset)
346 /* This line is not contigous with the last one. Dump pending. */
347 if (flushPendingData())
351 pendingOffset = line->offset;
353 rc = bufferOutput(line);
358 int main(int argc, char *argv[])
367 inFile = fopen(argv[1], "rt");
370 fprintf(stderr, "packihx: cannot open %s\n",
382 while (!rc && ((line = readLine(inFile)) != NULL))
384 rc = validateChecksum(line);
388 rc = writeLine(line);
395 if (!rc && !feof(inFile))
397 /* readLine must have failed for some reason. */
398 fprintf(stderr, "packihx: aborting after %d lines.\n", lineno);
404 /* Just in case there's something still pending. */
405 rc = flushPendingData();
410 fprintf(stderr, "packihx: read %d lines, wrote %d: OK.\n", lineno, outlineno);