+++ /dev/null
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#include <stdint.h>
-
-/* return 0 for end of file. -1 for error */
-int parse_ihex16(const char *line, uint8_t *bytes, int *addr, int *code)
-{
- unsigned int sum, len, cksum;
- unsigned int laddr, lcode;
- int i;
- const char *p;
-
- if (line[0] != ':') return -1;
- if (strlen(line) < 11) return -1;
- p = &line[1];
- if (!sscanf(p, "%02x", &len)) return -1;
- p+=2;
- if (strlen(line) < (11 + (len * 2))) return -1;
- if (!sscanf(p, "%04x", &laddr)) return -1;
- p += 4;
- *addr = laddr; // little endian address record
- if (!sscanf(p, "%02x", &lcode)) return -1;
- p+=2;
- *code = lcode & 0xFF;
-
- /* end of file record */
- if (*code == 1) return 0;
-
- sum = (len & 0xFF) + ((*addr >> 8) & 0xFF) + (*addr & 0xFF) + (*code & 0xFF);
-
- i = 0;
- while (i < len) {
- unsigned int byte;
- if (!sscanf(p, "%02x", &byte)) return -1;
- bytes[i++] = byte & 0xFF;
- sum += byte & 0xFF;
- p += 2;
- }
- if (!sscanf(p, "%02x", &cksum)) return -1;
- if ( ((sum & 0xFF) + (cksum & 0xFF)) & 0xFF ) return -1;
- return len;
-}
-
-int main(int argc, char **argv)
-{
- FILE *in;
-
- if (argc < 2) return 1;
-
- if ((in=fopen(argv[1], "r"))==NULL) {
- fprintf(stderr, "Failed opening %s: %s\n", argv[1], strerror(errno));
- return 1;
- }
-
- char buff[1024];
-
- while (!feof(in) && fgets(buff, sizeof(buff), in)!=NULL) {
- if (buff[0] == '#') continue;
-
- unsigned char bytes[80];
- int len, addr, code;
-
- if ((len=parse_ihex16(buff, bytes, &addr, &code)) <= 0) {
- if (len < 0) fprintf(stderr, "Bad line: %s\n", buff);
- continue;
- }
-
- printf("Addr: %04X +%2d code=%d :", addr, len, code);
- for (int i=0; i<len; i++) printf(" %02X", bytes[i]);
- printf("\n");
- }
-
- fclose(in);
-}