2 * This file is a part of configure.c
4 * hex2hex reads an input file in Intel HEX format and produces
5 * an (unsigned char) array which contains the bytes and writes it to the
6 * output file using C syntax
9 #define MAX_SIZE (256*1024)
10 #define ABANDON(why) { \
11 fprintf(stderr, "%s: " why "\n", source); \
12 fclose(inf);fclose(outf);return 0; \
15 int hex2hex(char *source, char *target, char *varline)
20 unsigned char buf[MAX_SIZE];
22 if ((inf=fopen(source, "r"))==NULL)
28 if ((outf=fopen(target, "w"))==NULL)
37 while ((c=getc(inf))!=EOF)
39 if (c == ':') /* Sync with beginning of line */
46 if (fscanf(inf, "%02x", &n) != 1)
47 ABANDON("File format error");
50 if (fscanf(inf, "%04x", &addr) != 1)
51 ABANDON("File format error");
55 if (fscanf(inf, "%02x", &linetype) != 1)
56 ABANDON("File format error");
64 if (fscanf(inf, "%02x", &c) != 1)
65 ABANDON("File format error");
67 ABANDON("File too large");
74 if (fscanf(inf, "%02x", &check) != 1)
75 ABANDON("File format error");
79 ABANDON("Line checksum error");
83 fprintf(outf, "/*\n *\t Computer generated file. Do not edit.\n */\n");
84 fprintf(outf, "static unsigned char %s[] = {\n", varline);
88 if (i) fprintf(outf, ",");
89 if (i && !(i % 16)) fprintf(outf, "\n");
90 fprintf(outf, "0x%02x", buf[i]);
93 fprintf(outf, "\n};\n\n");