This source file includes following definitions.
- loadhex
- hex2hex
1
2
3
4
5
6
7
8
9 #define MAX_SIZE (256*1024)
10 #define ABANDON(why) { \
11 fprintf(stderr, "%s: " why "\n", source); \
12 return 0; \
13 }
14
15 int loadhex(FILE *inf, unsigned char *buf, char *source)
16 {
17 int l=0, c, i;
18
19 while ((c=getc(inf))!=EOF)
20 {
21 if (c == ':')
22 {
23 int n, check;
24 unsigned char sum;
25 int addr;
26 int linetype;
27
28 if (fscanf(inf, "%02x", &n) != 1)
29 ABANDON("File format error");
30 sum = n;
31
32 if (fscanf(inf, "%04x", &addr) != 1)
33 ABANDON("File format error");
34 sum += addr/256;
35 sum += addr%256;
36
37 if (fscanf(inf, "%02x", &linetype) != 1)
38 ABANDON("File format error");
39 sum += linetype;
40
41 if (linetype != 0)
42 continue;
43
44 for (i=0;i<n;i++)
45 {
46 if (fscanf(inf, "%02x", &c) != 1)
47 ABANDON("File format error");
48 if (addr >= MAX_SIZE)
49 ABANDON("File too large");
50 buf[addr++] = c;
51 if (addr > l)
52 l = addr;
53 sum += c;
54 }
55
56 if (fscanf(inf, "%02x", &check) != 1)
57 ABANDON("File format error");
58
59 sum = ~sum + 1;
60 if (check != sum)
61 ABANDON("Line checksum error");
62 }
63 }
64
65 return l;
66 }
67
68 int hex2hex(char *source, char *target, char *varline)
69 {
70 FILE *inf, *outf;
71
72 int i,l;
73 unsigned char buf[MAX_SIZE];
74
75 if ((inf=fopen(source, "r"))==NULL)
76 {
77 perror(source);
78 return 0;
79 }
80
81 if ((outf=fopen(target, "w"))==NULL)
82 {
83 perror(target);
84 fclose(inf);
85 return 0;
86 }
87
88 l=loadhex(inf, buf, source);
89 if (l<=0)
90 {
91 fclose(inf);
92 fclose(outf);
93 return l;
94 }
95
96
97 fprintf(outf, "/*\n *\t Computer generated file. Do not edit.\n */\n");
98 fprintf(outf, "static int %s_len = %d;\n", varline, l);
99 fprintf(outf, "static unsigned char %s[] = {\n", varline);
100
101 for (i=0;i<l;i++)
102 {
103 if (i) fprintf(outf, ",");
104 if (i && !(i % 16)) fprintf(outf, "\n");
105 fprintf(outf, "0x%02x", buf[i]);
106 }
107
108 fprintf(outf, "\n};\n\n");
109 fclose(inf);
110 fclose(outf);
111 return 1;
112 }