dload.c: Explicit declare functions returning void
[m6809-computer-tools:dload-server.git] / makebin.c
1 /*      makebin--convert .s19 file to Color Computer binary format                                      */
2 /*      1997 Bryan Clingman                                                                                                                     */
3 /*      bac@realtimeweb.com                                                                                                                     */
4 /*      This code is in the public domain.                                                                                      */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 unsigned char   dehex(char *p)
10 {
11         unsigned char a;
12         a = (*p < 'A' ? *p - '0' : *p - 'A' + 10) << 4;
13         a |= *(p+1) < 'A' ? *(p+1) - '0' : *(p+1) - 'A' + 10;
14         return a;
15 }
16
17 struct  stChunk
18         {
19                 unsigned char   *pData;
20                 unsigned short  sLen;
21                 unsigned short  sOffset;
22                 struct stChunk  *pNext;
23         };
24
25 void insert(struct stChunk *pNew, struct stChunk **pList)
26 {
27         struct stChunk  *pCur;
28         struct stChunk  *pLast = 0;
29         pCur = *pList;
30
31         while(pCur)
32         {
33                 if (pCur->sOffset > pNew->sOffset)
34                 {
35                         if (pLast)
36                         {
37                                 pLast->pNext = pNew;
38                                 pNew->pNext = pCur;
39                         }
40                         else
41                         {
42                                 *pList = pNew;
43                                 pNew->pNext = pCur;
44                         }
45                         return;
46                 }
47                 pLast = pCur;
48                 pCur = pCur->pNext;
49         }
50         if (*pList)
51                 pLast->pNext = pNew;
52         else
53                 *pList = pNew;
54
55         pNew->pNext = 0;
56 }
57
58 int main(int    argc, char **argv)
59 {
60         FILE    *fpi, *fpo;
61         unsigned char   caLine[100];
62         short   sLineCount,
63                         sII;
64         int             iLen;
65         struct  stChunk *pList = 0;
66         struct  stChunk *pCur;
67
68         if (argc < 2)
69         {
70                 fprintf (stderr, "No input file\n");
71                 exit(1);
72         }
73                 
74         fpi = fopen(argv[1], "r");
75         if (!fpi)
76         {
77                 fprintf (stderr, "No input file %s\n", argv[1]);
78                 exit(1);
79         }
80
81         if (argc > 2)
82         {
83                 fpo = fopen(argv[2], "w");
84                 if (!fpo)
85                 {
86                         fprintf (stderr, "No unable to create output file %s\n", argv[2]);
87                         exit(1);
88                 }
89         }
90         else
91                 fpo = stdout;
92         sLineCount = 0;
93         while(fgets(caLine, 100, fpi))
94         {
95                 sLineCount++;
96                 if (caLine[0] != 'S')
97                         goto error;
98                 switch (caLine[1])
99                 {
100                         case '1':
101                                 pCur = (struct stChunk *)malloc(sizeof (struct stChunk));
102                                 pCur->sLen = dehex(&caLine[2]) - 3;
103                                 pCur->sOffset = dehex(&caLine[4]) << 8 | dehex(&caLine[6]);
104                                 pCur->pData = (unsigned char *)malloc(pCur->sLen);
105                                 for (sII = 0; sII < pCur->sLen; sII++)
106                                         pCur->pData[sII] = dehex(&caLine[(sII << 1) + 8]);
107                                 pCur->pNext = 0;
108                                 insert(pCur, &pList);
109                                 break;
110                         case '9':
111                                 break;
112                         default:
113                                 goto error;
114                 }
115         }
116         pCur = pList;
117         while (pCur->pNext)
118         {
119                 if (pCur->sOffset + pCur->sLen != pCur->pNext->sOffset)
120                         fprintf(stderr, "discontinuous at %x\n", pCur->sOffset);
121                 pCur = pCur->pNext;
122         }
123
124         iLen = pCur->sOffset + pCur->sLen - pList->sOffset;
125         fprintf(stderr, "Total Length: %d\n", iLen);
126         // the output:
127         caLine[0] = 0;
128         caLine[1] = (unsigned char)((iLen & 0xff00) >> 8);
129         caLine[2] = (unsigned char)(iLen & 0xff);
130         caLine[3] = (unsigned char)((pList->sOffset & 0xff00) >> 8);
131         caLine[4] = (unsigned char)(pList->sOffset & 0xff);
132         fwrite(caLine , 1, 5, fpo);
133         pCur = pList;
134         while (pCur->pNext)
135         {
136                 fwrite(pCur->pData, 1, pCur->sLen, fpo);
137                 for (sII = pCur->sOffset + pCur->sLen; sII < pCur->pNext->sOffset; sII++)
138                         fwrite(caLine, 1, 1, fpo);
139                 pCur = pCur->pNext;
140         }
141         fwrite(pCur->pData, 1, pCur->sLen, fpo);
142
143         caLine[0] = 0xff;
144         caLine[1] = 0;
145         caLine[2] = 0;
146         caLine[3] = (unsigned char)((pList->sOffset & 0xff00) >> 8);
147         caLine[4] = (unsigned char)(pList->sOffset & 0xff);
148         fwrite(caLine , 1, 5, fpo);
149         fclose(fpo);
150
151         return 0;
152         error:
153                 printf("Error in line %d\n", sLineCount);
154                 fclose(fpi);
155                 fclose(fpo);
156                 return 1;
157 }