1 | |
---|
2 | /***** P A C K H E X . C ************************************************ |
---|
3 | * |
---|
4 | * Packhex is a hex-file compaction utility. It attempts to concatenate |
---|
5 | * hex records to produce more size-efficient packaging. |
---|
6 | * |
---|
7 | * Limitations: Input files must be correctly formatted. This utility |
---|
8 | * is not robust enough to detect hex-record formatting |
---|
9 | * errors. |
---|
10 | * |
---|
11 | * Published: 5/93 Embedded Systems magazine |
---|
12 | * |
---|
13 | * Compiler: Microsoft C 6.0 |
---|
14 | * cl /F 1000 packhex.c |
---|
15 | * |
---|
16 | * |
---|
17 | * $Id$ |
---|
18 | * |
---|
19 | **************************************************************************/ |
---|
20 | |
---|
21 | |
---|
22 | /* #define SMALLER_RECORDS */ |
---|
23 | #ifdef SMALLER_RECORDS |
---|
24 | #define MAX_LEN_S1_RECS 128 |
---|
25 | #define MAX_LEN_S2_RECS 128 |
---|
26 | #define MAX_LEN_S3_RECS 128 |
---|
27 | #else |
---|
28 | #define MAX_LEN_S1_RECS 252 |
---|
29 | #define MAX_LEN_S2_RECS 251 |
---|
30 | #define MAX_LEN_S3_RECS 250 |
---|
31 | #endif |
---|
32 | |
---|
33 | |
---|
34 | /*--------------------------------- includes ---------------------------------*/ |
---|
35 | |
---|
36 | #include <stdio.h> |
---|
37 | #include <stdlib.h> |
---|
38 | #include <string.h> |
---|
39 | |
---|
40 | #if defined(__unix__) && !defined(EXIT_FAILURE) |
---|
41 | #define EXIT_FAILURE -1 |
---|
42 | #define EXIT_SUCCESS 0 |
---|
43 | #endif |
---|
44 | |
---|
45 | /*--------------------------------- defines ----------------------------------*/ |
---|
46 | |
---|
47 | #define YES 1 |
---|
48 | #define MAX_LINE_SIZE 600 |
---|
49 | #define EOS '\0' |
---|
50 | |
---|
51 | |
---|
52 | /*---------------------------------- macros ----------------------------------*/ |
---|
53 | |
---|
54 | /* Convert ASCII hexadecimal digit to value. */ |
---|
55 | |
---|
56 | #define HEX_DIGIT( C ) ( ( ( ( C ) > '9' ) ? ( C ) + 25 : ( C ) ) & 0xF ) |
---|
57 | |
---|
58 | |
---|
59 | /*--------------------------------- typedefs ---------------------------------*/ |
---|
60 | |
---|
61 | typedef unsigned char Boolean; |
---|
62 | typedef unsigned char Uchar; |
---|
63 | typedef unsigned int Uint; |
---|
64 | typedef unsigned long Ulong; |
---|
65 | |
---|
66 | typedef struct /* Functions and constant returning Hex-record vital stats. */ |
---|
67 | { |
---|
68 | Boolean ( *is_data_record )( char * ); |
---|
69 | Ulong ( *get_address )( char * ); |
---|
70 | Uint ( *get_data_count )( char * ); |
---|
71 | const Uint max_data_count; |
---|
72 | char *( *get_data_start )( char * ); |
---|
73 | void ( *put_data_record )( Uint, Ulong, char * ); |
---|
74 | } Rec_vitals; |
---|
75 | |
---|
76 | |
---|
77 | /*--------------------------- function prototypes ----------------------------*/ |
---|
78 | |
---|
79 | Rec_vitals * identify_first_data_record( char * ); |
---|
80 | Ulong get_ndigit_hex( char *, int ); |
---|
81 | |
---|
82 | |
---|
83 | /*----------------------------- Intel Hex format -----------------------------*/ |
---|
84 | |
---|
85 | /* |
---|
86 | * Intel Hex data-record layout |
---|
87 | * |
---|
88 | * :aabbbbccd...dee |
---|
89 | * |
---|
90 | * : - header character |
---|
91 | * aa - record data byte count, a 2-digit hex value |
---|
92 | * bbbb - record address, a 4-digit hex value |
---|
93 | * cc - record type, a 2-digit hex value: |
---|
94 | * "00" is a data record |
---|
95 | * "01" is an end-of-data record |
---|
96 | * "02" is an extended-address record |
---|
97 | * "03" is a start record |
---|
98 | * d...d - data (always an even number of chars) |
---|
99 | * ee - record checksum, a 2-digit hex value |
---|
100 | * checksum = 2's complement |
---|
101 | * [ (sum of bytes: aabbbbccd...d) modulo 256 ] |
---|
102 | */ |
---|
103 | |
---|
104 | |
---|
105 | Boolean is_intel_data_rec( char * rec_str ) |
---|
106 | { |
---|
107 | return( ( rec_str[ 0 ] == ':' ) && ( rec_str[ 8 ] == '0' ) ); |
---|
108 | } |
---|
109 | |
---|
110 | Uint get_intel_rec_data_count( char * rec_str ) |
---|
111 | { |
---|
112 | return( ( Uint ) get_ndigit_hex( rec_str + 1, 2 ) ); |
---|
113 | } |
---|
114 | |
---|
115 | Ulong get_intel_rec_address( char * rec_str ) |
---|
116 | { |
---|
117 | return( get_ndigit_hex( rec_str + 3, 4 ) ); |
---|
118 | } |
---|
119 | |
---|
120 | char * get_intel_rec_data_start( char * rec_str ) |
---|
121 | { |
---|
122 | return( rec_str + 9 ); |
---|
123 | } |
---|
124 | |
---|
125 | void put_intel_data_rec( Uint count, Ulong address, char * data_str ) |
---|
126 | { |
---|
127 | char *ptr; |
---|
128 | Uint sum = count + ( address >> 8 & 0xff ) + ( address & 0xff ); |
---|
129 | |
---|
130 | for ( ptr = data_str ; *ptr != EOS ; ptr += 2 ) |
---|
131 | sum += ( Uint ) get_ndigit_hex( ptr, 2 ); |
---|
132 | |
---|
133 | printf( |
---|
134 | ":%02X%04lX00%s%02X\n", count, address, data_str, (~sum + 1) & 0xff |
---|
135 | ); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | Rec_vitals intel_hex = |
---|
140 | { |
---|
141 | is_intel_data_rec, |
---|
142 | get_intel_rec_address, |
---|
143 | get_intel_rec_data_count, |
---|
144 | 255, /* Maximum data bytes in a record. */ |
---|
145 | get_intel_rec_data_start, |
---|
146 | put_intel_data_rec |
---|
147 | }; |
---|
148 | |
---|
149 | |
---|
150 | /*------------------------- Motorola S1-record format ------------------------*/ |
---|
151 | |
---|
152 | /* |
---|
153 | * Motorola S-record data-record layout |
---|
154 | * |
---|
155 | * Sabbc...cd...dee |
---|
156 | * |
---|
157 | * S - header character |
---|
158 | * a - record type, a 1-digit value: |
---|
159 | * "0" is a header record |
---|
160 | * "1" is a 2-byte-address data record |
---|
161 | * "2" is a 3-byte-address data record |
---|
162 | * "3" is a 4-byte-address data record |
---|
163 | * "7" is a 4-byte-address end-of-data record |
---|
164 | * "8" is a 3-byte-address end-of-data record |
---|
165 | * "9" is a 2-byte-address end-of-data record |
---|
166 | * bb - record length in bytes, a 2-digit hex value |
---|
167 | * (record length doesn't count the header/type |
---|
168 | * chars and checksum byte) |
---|
169 | * c...c - record address, a 4-, 6-, or 8-digit value, |
---|
170 | * depending on record type |
---|
171 | * d...d - data (always an even number of chars) |
---|
172 | * ee - record checksum, a 2-digit hex value |
---|
173 | * checksum = 1's complement |
---|
174 | * [ (sum of all bytes: bbc..cd...d) modulo 256 ] |
---|
175 | */ |
---|
176 | |
---|
177 | #define S1_COUNT_OFFSET 3 |
---|
178 | |
---|
179 | |
---|
180 | Boolean is_moto_s1_data_rec( char * rec_str ) |
---|
181 | { |
---|
182 | return ( ( rec_str[ 0 ] == 'S' ) && ( rec_str[ 1 ] == '1' ) ); |
---|
183 | } |
---|
184 | |
---|
185 | Uint get_moto_s1_rec_data_count( char * rec_str ) |
---|
186 | { |
---|
187 | return( ( Uint ) get_ndigit_hex( rec_str + 2, 2 ) - S1_COUNT_OFFSET ); |
---|
188 | } |
---|
189 | |
---|
190 | Ulong get_moto_s1_rec_address( char * rec_str ) |
---|
191 | { |
---|
192 | return( get_ndigit_hex( rec_str + 4, 4 ) ); |
---|
193 | } |
---|
194 | |
---|
195 | char * get_moto_s1_rec_data_start( char * rec_str ) |
---|
196 | { |
---|
197 | return( rec_str + 8 ); |
---|
198 | } |
---|
199 | |
---|
200 | void put_moto_s1_data_rec( Uint count, Ulong address, char * data_str ) |
---|
201 | { |
---|
202 | char *ptr; |
---|
203 | Uint sum = S1_COUNT_OFFSET + count + |
---|
204 | ( address >> 8 & 0xff ) + ( address & 0xff ); |
---|
205 | |
---|
206 | for ( ptr = data_str ; *ptr != EOS ; ptr += 2 ) |
---|
207 | sum += ( Uint ) get_ndigit_hex( ptr, 2 ); |
---|
208 | |
---|
209 | printf( |
---|
210 | "S1%02X%04lX%s%02X\n", |
---|
211 | count + S1_COUNT_OFFSET, address, data_str, ~sum & 0xff |
---|
212 | ); |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | Rec_vitals motorola_s1_rec = |
---|
217 | { |
---|
218 | is_moto_s1_data_rec, |
---|
219 | get_moto_s1_rec_address, |
---|
220 | get_moto_s1_rec_data_count, |
---|
221 | MAX_LEN_S1_RECS, /* Maximum data bytes in a record. */ |
---|
222 | get_moto_s1_rec_data_start, |
---|
223 | put_moto_s1_data_rec |
---|
224 | }; |
---|
225 | |
---|
226 | |
---|
227 | /*------------------------- Motorola S2-record format ------------------------*/ |
---|
228 | |
---|
229 | #define S2_COUNT_OFFSET 4 |
---|
230 | |
---|
231 | Boolean is_moto_s2_data_rec( char * rec_str ) |
---|
232 | { |
---|
233 | return ( ( rec_str[ 0 ] == 'S' ) && ( rec_str[ 1 ] == '2' ) ); |
---|
234 | } |
---|
235 | |
---|
236 | Uint get_moto_s2_rec_data_count( char * rec_str ) |
---|
237 | { |
---|
238 | return( ( Uint ) get_ndigit_hex( rec_str + 2, 2 ) - S2_COUNT_OFFSET ); |
---|
239 | } |
---|
240 | |
---|
241 | Ulong get_moto_s2_rec_address( char * rec_str ) |
---|
242 | { |
---|
243 | return( get_ndigit_hex( rec_str + 4, 6 ) ); |
---|
244 | } |
---|
245 | |
---|
246 | char * get_moto_s2_rec_data_start( char * rec_str ) |
---|
247 | { |
---|
248 | return( rec_str + 10 ); |
---|
249 | } |
---|
250 | |
---|
251 | void put_moto_s2_data_rec( Uint count, Ulong address, char * data_str ) |
---|
252 | { |
---|
253 | char *ptr; |
---|
254 | Uint sum = S2_COUNT_OFFSET + count + ( address >> 16 & 0xff ) + |
---|
255 | ( address >> 8 & 0xff ) + |
---|
256 | ( address & 0xff ); |
---|
257 | |
---|
258 | for ( ptr = data_str ; *ptr != EOS ; ptr += 2 ) |
---|
259 | sum += ( Uint ) get_ndigit_hex( ptr, 2 ); |
---|
260 | |
---|
261 | printf( |
---|
262 | "S2%02X%06lX%s%02X\n", |
---|
263 | count + S2_COUNT_OFFSET, address, data_str, ~sum & 0xff |
---|
264 | ); |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | Rec_vitals motorola_s2_rec = |
---|
269 | { |
---|
270 | is_moto_s2_data_rec, |
---|
271 | get_moto_s2_rec_address, |
---|
272 | get_moto_s2_rec_data_count, |
---|
273 | MAX_LEN_S2_RECS, /* Maximum data bytes in a record. */ |
---|
274 | get_moto_s2_rec_data_start, |
---|
275 | put_moto_s2_data_rec |
---|
276 | }; |
---|
277 | |
---|
278 | |
---|
279 | /*------------------------- Motorola S3-record format ------------------------*/ |
---|
280 | |
---|
281 | #define S3_COUNT_OFFSET 5 |
---|
282 | |
---|
283 | Boolean is_moto_s3_data_rec( char * rec_str ) |
---|
284 | { |
---|
285 | return ( ( rec_str[ 0 ] == 'S' ) && ( rec_str[ 1 ] == '3' ) ); |
---|
286 | } |
---|
287 | |
---|
288 | Uint get_moto_s3_rec_data_count( char * rec_str ) |
---|
289 | { |
---|
290 | return( ( Uint ) get_ndigit_hex( rec_str + 2, 2 ) - S3_COUNT_OFFSET ); |
---|
291 | } |
---|
292 | |
---|
293 | Ulong get_moto_s3_rec_address( char * rec_str ) |
---|
294 | { |
---|
295 | return( get_ndigit_hex( rec_str + 4, 8 ) ); |
---|
296 | } |
---|
297 | |
---|
298 | char * get_moto_s3_rec_data_start( char * rec_str ) |
---|
299 | { |
---|
300 | return( rec_str + 12 ); |
---|
301 | } |
---|
302 | |
---|
303 | void put_moto_s3_data_rec( Uint count, Ulong address, char * data_str ) |
---|
304 | { |
---|
305 | char *ptr; |
---|
306 | Uint sum = S3_COUNT_OFFSET + count + ( address >> 24 & 0xff ) + |
---|
307 | ( address >> 16 & 0xff ) + |
---|
308 | ( address >> 8 & 0xff ) + |
---|
309 | ( address & 0xff ); |
---|
310 | |
---|
311 | for ( ptr = data_str ; *ptr != EOS ; ptr += 2 ) |
---|
312 | sum += ( Uint ) get_ndigit_hex( ptr, 2 ); |
---|
313 | |
---|
314 | printf( |
---|
315 | "S3%02X%08lX%s%02X\n", |
---|
316 | count + S3_COUNT_OFFSET, address, data_str, ~sum & 0xff |
---|
317 | ); |
---|
318 | } |
---|
319 | |
---|
320 | |
---|
321 | Rec_vitals motorola_s3_rec = |
---|
322 | { |
---|
323 | is_moto_s3_data_rec, |
---|
324 | get_moto_s3_rec_address, |
---|
325 | get_moto_s3_rec_data_count, |
---|
326 | MAX_LEN_S3_RECS, /* Maximum data bytes in a record. */ |
---|
327 | get_moto_s3_rec_data_start, |
---|
328 | put_moto_s3_data_rec |
---|
329 | }; |
---|
330 | |
---|
331 | |
---|
332 | /*-------------------- Put your favorite hex format here ---------------------*/ |
---|
333 | |
---|
334 | /* |
---|
335 | * * * * The following is a template for an additional hex format: * * * |
---|
336 | * |
---|
337 | * |
---|
338 | * Boolean is_X_data_rec( char * rec_str ) {} |
---|
339 | * |
---|
340 | * Uint get_X_rec_data_count( char * rec_str ) {} |
---|
341 | * |
---|
342 | * Ulong get_X_rec_address( char * rec_str ) {} |
---|
343 | * |
---|
344 | * char * get_X_rec_data_start( char * rec_str ) {} |
---|
345 | * |
---|
346 | * void put_X_data_rec( Uint count, Ulong address, char * data_str ) {} |
---|
347 | * |
---|
348 | * Rec_vitals X_rec = |
---|
349 | * { |
---|
350 | * is_X_data_rec, |
---|
351 | * get_X_rec_address, |
---|
352 | * get_X_rec_data_count, |
---|
353 | * MAXIMUM DATA BYTES IN A RECORD, |
---|
354 | * get_X_rec_data_start, |
---|
355 | * put_X_data_rec |
---|
356 | * }; |
---|
357 | * |
---|
358 | */ |
---|
359 | |
---|
360 | /*----------------------------------------------------------------------------*/ |
---|
361 | |
---|
362 | |
---|
363 | /* |
---|
364 | * Put address of additional Rec_vitals structures |
---|
365 | * in this array, before the NULL entry. |
---|
366 | */ |
---|
367 | |
---|
368 | Rec_vitals *formats[] = |
---|
369 | { |
---|
370 | &intel_hex, |
---|
371 | &motorola_s1_rec, |
---|
372 | &motorola_s2_rec, |
---|
373 | &motorola_s3_rec, |
---|
374 | ( Rec_vitals * ) NULL |
---|
375 | }; |
---|
376 | |
---|
377 | |
---|
378 | /**** main ***************************************************************** |
---|
379 | * |
---|
380 | * |
---|
381 | * Expects: Nothing (no command-line parameters). |
---|
382 | * |
---|
383 | * Returns: Exit status (EXIT_SUCCESS or EXIT_FAILURE). |
---|
384 | * |
---|
385 | * Reads hex records on the standard input and attempts to |
---|
386 | * splice adjacent data fields together. Results appear on |
---|
387 | * the standard output. |
---|
388 | * |
---|
389 | *******************************************************************************/ |
---|
390 | |
---|
391 | int main( |
---|
392 | int argc, |
---|
393 | char **argv |
---|
394 | ) |
---|
395 | { |
---|
396 | |
---|
397 | char inbuff[ MAX_LINE_SIZE ], outbuff[ MAX_LINE_SIZE ]; |
---|
398 | char *in_dptr, *out_dptr; |
---|
399 | int d_total, d_count, d_excess, n; |
---|
400 | Ulong in_rec_addr, out_rec_addr = 0; |
---|
401 | Rec_vitals *rptr; |
---|
402 | |
---|
403 | |
---|
404 | /* Sift through file until first hex record is identified. */ |
---|
405 | if ( ( rptr = identify_first_data_record( inbuff ) ) == NULL ) |
---|
406 | { |
---|
407 | fputs( "No hex records found.\n", stderr ); |
---|
408 | exit( EXIT_FAILURE ); |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | /* Attempt data-record splicing until end-of-file is reached. */ |
---|
413 | d_total = 0; |
---|
414 | do |
---|
415 | { |
---|
416 | if ( rptr->is_data_record( inbuff ) == YES ) |
---|
417 | { /* Input record is a data record. */ |
---|
418 | d_count = rptr->get_data_count( inbuff ); |
---|
419 | in_rec_addr = rptr->get_address( inbuff ); |
---|
420 | in_dptr = rptr->get_data_start( inbuff ); |
---|
421 | |
---|
422 | if ( d_total == 0 || in_rec_addr != out_rec_addr + d_total ) |
---|
423 | { /* Begin a new output record. */ |
---|
424 | if ( d_total != 0 ) |
---|
425 | rptr->put_data_record( d_total, out_rec_addr, outbuff ); |
---|
426 | out_dptr = outbuff; |
---|
427 | n = d_total = d_count; |
---|
428 | out_rec_addr = in_rec_addr; |
---|
429 | } |
---|
430 | else if |
---|
431 | ( ( d_excess = d_total + d_count - rptr->max_data_count ) > 0 ) |
---|
432 | { /* Output a maximum-length record, then start a new record. */ |
---|
433 | strncat( outbuff, in_dptr, 2 * ( d_count - d_excess ) ); |
---|
434 | rptr->put_data_record( |
---|
435 | rptr->max_data_count, out_rec_addr, outbuff |
---|
436 | ); |
---|
437 | in_dptr += 2 * ( d_count - d_excess ); |
---|
438 | out_dptr = outbuff; |
---|
439 | n = d_total = d_excess; |
---|
440 | out_rec_addr += rptr->max_data_count; |
---|
441 | } |
---|
442 | else |
---|
443 | { /* Append input record's data field with accumulated data. */ |
---|
444 | out_dptr = outbuff + ( 2 * d_total ); |
---|
445 | d_total += n = d_count; |
---|
446 | } |
---|
447 | strncpy( out_dptr, in_dptr, 2 * n ); |
---|
448 | out_dptr[ 2 * n ] = EOS; |
---|
449 | } |
---|
450 | else |
---|
451 | { /* Not a data record; |
---|
452 | * flush accumulated data then echo non-data record. |
---|
453 | */ |
---|
454 | if ( d_total != 0 ) |
---|
455 | { |
---|
456 | rptr->put_data_record( d_total, out_rec_addr, outbuff ); |
---|
457 | d_total = 0; |
---|
458 | } |
---|
459 | puts( inbuff ); |
---|
460 | } |
---|
461 | } while ( gets( inbuff ) != NULL ); |
---|
462 | |
---|
463 | |
---|
464 | exit( EXIT_SUCCESS ); |
---|
465 | |
---|
466 | } |
---|
467 | |
---|
468 | |
---|
469 | /**** identify_first_data_record ******************************************* |
---|
470 | * |
---|
471 | * Expects: Pointer to hex-record line buffer. |
---|
472 | * |
---|
473 | * Returns: Pointer to hex-record structure (NULL if no match found). |
---|
474 | * |
---|
475 | * Reads the standard input, line by line, searching for a valid |
---|
476 | * record header character. If a valid header is found, a pointer |
---|
477 | * to the hex-record's type structure is returned, otherwise NULL. |
---|
478 | * |
---|
479 | * The input-stream pointer is left pointing to the first valid hex record. |
---|
480 | * |
---|
481 | *******************************************************************************/ |
---|
482 | |
---|
483 | Rec_vitals * identify_first_data_record( char * buff_ptr ) |
---|
484 | { |
---|
485 | Rec_vitals ** ptr; |
---|
486 | |
---|
487 | while ( gets( buff_ptr ) != NULL ) |
---|
488 | { |
---|
489 | for ( ptr = formats ; *ptr != ( Rec_vitals * ) NULL ; ptr++ ) |
---|
490 | if ( ( *ptr )->is_data_record( buff_ptr ) == YES ) |
---|
491 | return( *ptr ); /* Successful return. */ |
---|
492 | |
---|
493 | puts( buff_ptr ); /* Echo non-hex-record line. */ |
---|
494 | } |
---|
495 | |
---|
496 | return( ( Rec_vitals * ) NULL ); /* Unsuccessful return. */ |
---|
497 | } |
---|
498 | |
---|
499 | |
---|
500 | /**** get_ndigit_hex ******************************************************* |
---|
501 | * |
---|
502 | * Expects: Pointer to first ASCII hexadecimal digit, number of digits. |
---|
503 | * |
---|
504 | * Returns: Value of hexadecimal string as an unsigned long. |
---|
505 | * |
---|
506 | *******************************************************************************/ |
---|
507 | |
---|
508 | Ulong get_ndigit_hex( char * cptr, int digits ) |
---|
509 | { |
---|
510 | Ulong value; |
---|
511 | |
---|
512 | for ( value = 0 ; --digits >= 0 ; cptr++ ) |
---|
513 | value = ( value * 16L ) + HEX_DIGIT( *cptr ); |
---|
514 | |
---|
515 | return( value ); |
---|
516 | } |
---|