6 /* return 0 for end of file. -1 for error */
7 int parse_ihex16(const char *line, uint8_t *bytes, int *addr, int *code)
9 unsigned int sum, len, cksum;
10 unsigned int laddr, lcode;
14 if (line[0] != ':') return -1;
15 if (strlen(line) < 11) return -1;
17 if (!sscanf(p, "%02x", &len)) return -1;
19 if (strlen(line) < (11 + (len * 2))) return -1;
20 if (!sscanf(p, "%04x", &laddr)) return -1;
22 *addr = laddr; // little endian address record
23 if (!sscanf(p, "%02x", &lcode)) return -1;
27 /* end of file record */
28 if (*code == 1) return 0;
30 sum = (len & 0xFF) + ((*addr >> 8) & 0xFF) + (*addr & 0xFF) + (*code & 0xFF);
35 if (!sscanf(p, "%02x", &byte)) return -1;
36 bytes[i++] = byte & 0xFF;
40 if (!sscanf(p, "%02x", &cksum)) return -1;
41 if ( ((sum & 0xFF) + (cksum & 0xFF)) & 0xFF ) return -1;
45 int main(int argc, char **argv)
49 if (argc < 2) return 1;
51 if ((in=fopen(argv[1], "r"))==NULL) {
52 fprintf(stderr, "Failed opening %s: %s\n", argv[1], strerror(errno));
58 while (!feof(in) && fgets(buff, sizeof(buff), in)!=NULL) {
59 if (buff[0] == '#') continue;
61 unsigned char bytes[80];
64 if ((len=parse_ihex16(buff, bytes, &addr, &code)) <= 0) {
65 if (len < 0) fprintf(stderr, "Bad line: %s\n", buff);
69 printf("Addr: %04X +%2d code=%d :", addr, len, code);
70 for (int i=0; i<len; i++) printf(" %02X", bytes[i]);