61018be9dbaaeb65f00b98fe413e57f75e512c51
1 /* Convert a hex b file into a ccs data file */
3 #include <stdio.h>
4 #include <malloc.h>
7 int asciiByte (unsigned char c)
8 {
9 if ((c >= '0') && (c <= '9'))
10 return (1);
12 if ((c >= 'A') && (c <= 'F'))
13 return (1);
15 return (0);
16 }
18 int toNum (unsigned char c)
19 {
20 if ((c >= '0') && (c <= '9'))
21 return (c - '0');
23 return (c - 'A' + 10);
25 }
28 void stripLine (FILE *s)
29 {
30 char iline[132];
32 fgets (iline, 131, s);
34 }
36 /* Read a .b file. */
37 unsigned long readBFile (FILE *s, unsigned char *data, unsigned long maxSize)
38 {
39 unsigned char x, y;
40 unsigned long byteCount = 0;
42 /* Strip the 1st two lines */
43 stripLine (s);
44 stripLine (s);
46 for (;;) {
48 /* read the 1st ascii char */
49 do {
50 x = fgetc (s);
51 if (x == (unsigned char)EOF)
52 return (byteCount);
54 } while (!asciiByte(x));
56 /* Read the next ascii char */
57 y = fgetc (s);
58 if (y == (unsigned char)EOF)
59 return (byteCount);
60 if (asciiByte(y))
61 data[byteCount++] = (toNum(x) << 4) | toNum (y);
63 if (byteCount >= maxSize) {
64 fprintf (stderr, "Max input array size exceeded\n");
65 return (-1);
66 }
67 }
68 }
71 unsigned dwordConvert (unsigned char *data, unsigned long idx, unsigned long iMax)
72 {
73 unsigned value;
74 unsigned char c[4];
75 int i;
77 c[0] = c[1] = c[2] = c[3] = 0;
79 for (i = 0; i < 4; i++) {
80 if (idx >= iMax)
81 break;
82 c[i] = data[idx++];
83 }
85 value = c[3] | (c[2] << 8) | (c[1] << 16) | (c[0] << 24);
87 return (value);
89 }
93 #define SIZE (1024 * 1024 * 2) /* max array size 2MB */
95 int main (int argc, char *argv[])
96 {
97 FILE *strin;
98 FILE *strout;
100 unsigned char *dataSet1;
102 unsigned char block[128];
103 unsigned blockSize;
105 unsigned long pIn;
106 unsigned long pOut;
108 unsigned long inSize;
109 unsigned long i;
111 /* Arg check */
112 if (argc != 3) {
113 fprintf (stderr, "usage: %s infile outfile\n", argv[0]);
114 return (-1);
115 }
117 /* Open the input file */
118 strin = fopen (argv[1], "r");
119 if (strin == NULL) {
120 fprintf (stderr, "%s: Could not open file %s for reading\n", argv[0], argv[1]);
121 return (-1);
122 }
124 /* Allocate the two data set memories */
125 dataSet1 = malloc (SIZE * sizeof (unsigned char));
126 if (dataSet1 == NULL) {
127 fprintf (stderr, "%s: Malloc failure\n", argv[0]);
128 return (-1);
129 }
131 /* Read the data into the byte stream */
132 if ((inSize = readBFile (strin, dataSet1, SIZE)) < 0)
133 return (inSize);
134 fclose (strin);
136 strout = fopen (argv[2], "w");
137 if (strout == NULL) {
138 fprintf (stderr, "%s error: Could not open output file %s\n", argv[0], argv[2]);
139 free (dataSet1);
140 return (-1);
141 }
143 /* Write the CCS header */
144 fprintf (strout, "1651 1 10000 1 %x\n", (inSize + 3) / 4);
146 /* Write out each 32 bit line. */
147 for (i = 0; i < inSize; i += 4)
148 fprintf (strout, "0x%08x\n", dwordConvert (dataSet1, i, inSize));
150 free (dataSet1);
151 fclose (strout);
154 return (0);
156 }