2 * hex2hex reads stdin in Intel HEX format and produces an
3 * (unsigned char) array which contains the bytes and writes it
4 * to stdout using C syntax
11 #define ABANDON(why) { fprintf(stderr, "%s\n", why); exit(1); }
12 #define MAX_SIZE (256*1024)
13 unsigned char buf
[MAX_SIZE
];
15 int loadhex(FILE *inf
, unsigned char *buf
)
19 while ((c
=getc(inf
))!=EOF
)
21 if (c
== ':') /* Sync with beginning of line */
28 if (fscanf(inf
, "%02x", &n
) != 1)
29 ABANDON("File format error");
32 if (fscanf(inf
, "%04x", &addr
) != 1)
33 ABANDON("File format error");
37 if (fscanf(inf
, "%02x", &linetype
) != 1)
38 ABANDON("File format error");
46 if (fscanf(inf
, "%02x", &c
) != 1)
47 ABANDON("File format error");
49 ABANDON("File too large");
56 if (fscanf(inf
, "%02x", &check
) != 1)
57 ABANDON("File format error");
61 ABANDON("Line checksum error");
68 int main( int argc
, const char * argv
[] )
74 if(argv
[1] && strcmp(argv
[1], "-i")==0)
82 fprintf(stderr
,"hex2hex: [-i] filename\n");
86 l
= loadhex(stdin
, buf
);
88 printf("/*\n *\t Computer generated file. Do not edit.\n */\n");
89 printf("static int %s_len = %d;\n", varline
, l
);
90 printf("static unsigned char %s[] %s = {\n", varline
, id
?"__initdata":"");
95 if (i
&& !(i
% 16)) printf("\n");
96 printf("0x%02x", buf
[i
]);