#include #include #include #include #include #include #include #include #include static uint8_t tolowercase(uint8_t ch) { if ((ch >= 'A') && (ch <= 'Z')) return ch + 0x20; // Convert uppercase to lowercase return ch; } static int8_t parseHexDigit(uint8_t digit) { digit = tolowercase(digit); if (isdigit(digit)) return (int8_t)digit - '0'; if ((digit >= 'a') && (digit <= 'f')) return (int8_t)digit + 0xA - 'a'; return -1; // Error case - input wasn't a valid hex digit } static int hexstring_parse(const char *hexstr, uint8_t *buf, size_t *buflen) { size_t hexstrlen = *buflen * 2; size_t i; if (hexstrlen & 0x1) { fprintf(stderr, "hexstring_parse: not even\n"); return 1; } if (*buflen < hexstrlen/2) { fprintf(stderr, "hexstring_parse: buffer too small %zu < %zu\n", *buflen, hexstrlen/2); return 1; } for (i=0;i> 8; sum += addr & 0xFF; for (i=0;i buflen) { fprintf(stderr, "bad hexfile, too big\n"); return 1; } memcpy(buf + addr, data, record_len); } else if (type == 1) { *eof = true; } else { fprintf(stderr, "bad hexfile: unknown record type %02X\n", type); } return 0; } int read_hexfile(uint8_t *buf, size_t buflen, const char *filename) { FILE *fp; char line[1024]; bool eof; if (NULL == (fp = fopen(filename, "ro"))) return 1; while (NULL != fgets(line, sizeof(line), fp)) { char *p = (line + strlen(line)) - 1; while(p > line) { if (isspace((int)(*p))) *p = 0; p--; } if (0 != read_record(buf, buflen, line, &eof)) goto fail; if (eof) break; } fclose(fp); return 0; fail: fclose(fp); return 1; }