summaryrefslogtreecommitdiff
path: root/support/packihx/packihx.c
blob: 1201786ed250935b0e1902d836ff852aadc43a75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*-----------------------------------------------------------------------
 * packihx.c:
 *
 * utility to pack an Intel HEX format file by removing redundant 
 * extended offset records and accumulating data records up to
 * OUTPUT_CHUNK (currently 16) bytes.
 *
 * Released to the public domain 10/16/2000 Kevin Vigor.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

#if defined(_MSC_VER) || defined(__BORLANDC__)

typedef unsigned char Uint8 ;
typedef unsigned Uint16 ;

#else

#include "config.h"
#endif

/* A cooked line of input. */
typedef struct _Line
{
    Uint8 		len;		/* length of data portion of record. */
    Uint16      offset;
    Uint8		type;
    Uint8		*data;		
    Uint8		checksum;
} Line;

/* Largest expected line of raw input. */
#define MAX_INPUT_RAW	128
/* Largest expected cooked data portion of an input line. */
#define MAX_INPUT_COOKED (MAX_INPUT_RAW / 2)

/* Globals: current input & output line numbers. */
int lineno = 0;
int outlineno = 0;

/* Convert hex digit to numeric value 0 - 15; assumes input is a 
 * valid digit (i.e. passes isxdigit()).
 */
static Uint8 hexDigit(const unsigned char c)
{
    if (isdigit(c))
    {
        return (Uint8)(c - '0');
    }
    else
    {
    	return (Uint8)((islower(c) ? toupper(c) : c)  - 'A' + 10);
    }
}

/* Convert two hex digits from cp to a byte value. */
static int getHexByte(const char *cp, Uint8 *byte)
{
    if (cp && cp[0] && isxdigit(cp[0]) && cp[1] && isxdigit(cp[1]))
    {
 	*byte = (hexDigit(cp[0]) << 4) + hexDigit(cp[1]);
    }
    else
    {
    	return -1;
    }
    return 0;
}

/* Convert four hex digits from cp to a 2 byte value. */
static int getHexWord(const char *cp, Uint16 *word)
{
    Uint8 byte1, byte2;
    
    if (getHexByte(cp, &byte1) || getHexByte(cp + 2, &byte2))
    {
    	return -1;
    }
    *word = (byte1 << 8) + byte2;
    return 0;
}

/* Return a single cooked line of input from the passed file handle. */
Line *readLine(FILE *inFile)
{
    static char buffer[MAX_INPUT_RAW];
    const char  *bp;
    Line 	*line;
    unsigned 	i;

    line = (Line *)malloc(sizeof(Line));
    if (!line)
    {
        fprintf(stderr, "packihx: no memory!\n");
        return NULL;
    }

    do
    {
    	if (!fgets(buffer, MAX_INPUT_RAW, inFile))
    	{
    	    return NULL;
    	}
        ++lineno;
    
    	if (!buffer[0] || buffer[0] == '\r' || buffer[0] == '\n')
    	{
    	    /* Empty input line. */
            return NULL;
        }
    } while (buffer[0] != ':');

    bp = buffer;
    bp++;	/* Skip leading : */
     
    if (getHexByte(bp, &line->len))
    {
        fprintf(stderr, "packihx: can't read line length @ line %d\n", 
        	lineno);
    	free(line);
    	return NULL;
    }
    bp += 2;	/* Two digits consumed. */

    if (line->len > MAX_INPUT_COOKED)
    {
        fprintf(stderr, "packihx: line length %X too long @ line %d\n",
                (int)line->len, lineno);
        free(line);
        return NULL;
    }
    
    if (getHexWord(bp, &line->offset))
    {
        fprintf(stderr, "packihx: can't read line offset @ line %d\n",
                lineno);
        free(line);
        return NULL;        
    }
    bp += 4; /* Four digits consumed. */
    
    if (getHexByte(bp, &line->type))
    {
        fprintf(stderr, "packihx: can't read record type @ line %d\n", 
        	lineno);
    	free(line);
    	return NULL;
    }
    bp += 2;	/* Two digits consumed. */    
   
    /* Hack - always allocate something, even if len is zero.
     * Avoids special case for len == 0. */
    line->data = (Uint8 *)malloc(line->len ? line->len : 1);
    if (!line->data)
    {
    	free(line);
        fprintf(stderr, "packihx: no memory!\n");
        return NULL;
    }
    
    for (i = 0; i < (unsigned)line->len; i++)
    {
        if (getHexByte(bp, &(line->data[i])))
        {
            fprintf(stderr, 
            	    "packihx: can't read data byte %u of %u @ line %d\n", 
            	    i, (unsigned) line->len, lineno);
            free(line->data);
            free(line);
            return NULL; 
        }
        bp += 2; /* Two digits consumed. */
    }
    
    if (getHexByte(bp, &line->checksum))
    {
        fprintf(stderr, "packihx: can't read checksum @ line %d\n", 
        	lineno);
        free(line->data);
    	free(line);
    	return NULL;
    }
    /* bp += 2; */    /* Two digits consumed. */
        
    return line;
}

/* Compute the checksum of a line. */
Uint16 lineChecksum(unsigned len, unsigned offset, unsigned type, 
		    const Uint8 *data)
{
    Uint16  checksum;
    unsigned 	i;
     
    checksum = len + type + (offset >> 8) + 
     	       (offset & 0xff);
     	        
    for (i = 0; i < len; i++)
    {
    	checksum += data[i];	   
    }
    
    checksum &= 0xff;
    if (checksum)
    {
       checksum = 0x100 - checksum;
    }
    return checksum;
}

/* Ensure that the checksum of a line matches the expected value. */
int validateChecksum(Line *line)
{
    Uint16 checksum;
    
    checksum = lineChecksum(line->len, line->offset, line->type, line->data);
    
    if (checksum != line->checksum)
    {
        fprintf(stderr, "packihx: invalid checksum %X (want %X) @ line %d\n", 
        	(unsigned)(line->checksum), (unsigned)checksum, 
        	lineno);
        return -1;	
    }
    
    return 0;
}

/* Write a single record line. */
int writeRecord(unsigned len, unsigned offset, unsigned type,
		const Uint8 *data)
{
    unsigned i;
    
    if (printf(":%02X%04X%02X", len, offset, type) == EOF)
    {
        return -1;
    }
    
    for (i = 0; i < len; i++)
    {
        if (printf("%02X", data[i]) == EOF)
        {
            return -1;
        }
    }
    
    if (printf("%02X\n", lineChecksum(len, offset, type, data)) == EOF)
    {
    	return -1;
    }
    outlineno++;
    return 0;
}

#define OUTPUT_CHUNK 16		
static unsigned pendingLen = 0;
static unsigned pendingOffset = 0;
static Uint8	pending[MAX_INPUT_COOKED + OUTPUT_CHUNK];

/* Buffer up a data record. */
int bufferOutput(Line *line)
{
    unsigned offset = 0;
    int	    rc = 0;
   
    /* Stick the data onto any pending data. */
    assert(pendingLen < OUTPUT_CHUNK);
    memcpy(&pending[pendingLen], line->data, line->len);
    pendingLen += line->len;
   
    /* Write it out untill we have less than an OUTPUT_CHUNK left. */
    while (!rc && pendingLen >= OUTPUT_CHUNK)
    {
        rc = writeRecord(OUTPUT_CHUNK, pendingOffset, 0, &pending[offset]);
        offset += OUTPUT_CHUNK;
        pendingOffset += OUTPUT_CHUNK;
        pendingLen -= OUTPUT_CHUNK;
    }
   
    /* Copy any remaining bits back to the beginning of the buffer. */
    if (pendingLen)
    {
   	    memmove(pending, &pending[offset], pendingLen);
    }
    return rc;
}

/* Write out any pending data. */
int flushPendingData(void)
{
    int rc = 0;
    
    assert(pendingLen < OUTPUT_CHUNK);
    
    if (pendingLen)
    {
        rc =  writeRecord(pendingLen, pendingOffset, 0, pending);
        pendingLen = pendingOffset = 0;
    }
    return rc;
}

/* Write an arbitrary line of output (buffering if possible) */
int writeLine(Line *line)
{
    static Uint16 lastExtendedOffset = 0;
    int       rc;
    
    if (line->type)
    {
        /* Not a data record. */
        if (line->type == 4)
        {
            Uint16 offset;
            
            /* Extended offset record. */
            if (line->len != 2)
            {
            	fprintf(stderr, 
                 	"packihx: invalid extended offset record @ line %d\n",
                 	lineno);
                return -1;
            }
            
            offset = (line->data[0] << 8) + line->data[1];
            
            if (offset == lastExtendedOffset)
            {
                /* We can simply skip this line. */
                return 0;
            }
            else
            {
            	lastExtendedOffset = offset;
            }
        }
        
     	if (flushPendingData())
     	{
     	    return -1;
     	}
     	
     	/* Write the line as is. */
     	rc = writeRecord(line->len, line->offset, line->type, line->data);
    }
    else
    {
        if (pendingOffset + pendingLen != (unsigned)line->offset)
        {
            /* This line is not contigous with the last one. Dump pending. */
            if (flushPendingData())
            {
            	return -1;
            }
            pendingOffset = line->offset;
        }
        rc = bufferOutput(line);
    }
    return rc;
}

int main(int argc, char *argv[])
{
    FILE *inFile;
    Line *line;
    int  closeFile;
    int  rc = 0;
    
    if (argc > 1)
    {
        inFile = fopen(argv[1], "rt");
        if (!inFile)
        {
            fprintf(stderr, "packihx: cannot open %s\n", 
            	     argv[1]);
            return 1;
        }
        closeFile = 1;
    }
    else
    {
    	inFile = stdin;
    	closeFile = 0;
    }
    
    while (!rc && ((line = readLine(inFile)) != NULL))
    {
        rc = validateChecksum(line);
        
        if (!rc)
        {
            rc = writeLine(line);
        }
        
        free(line->data);
        free(line);
    }
    
    if (!rc && !feof(inFile))
    {
        /* readLine must have failed for some reason. */
        fprintf(stderr, "packihx: aborting after %d lines.\n", lineno);
        rc = 1;
    }
    
    if (!rc)
    {
    	/* Just in case there's something still pending. */
        rc = flushPendingData();
    }
    
    if (!rc)
    {
        fprintf(stderr, "packihx: read %d lines, wrote %d: OK.\n", lineno, outlineno);
    }

    if (closeFile)
    {
        fclose(inFile);
    }    
    return rc;
}