sch@linus.UUCP (Stephen C. Hemminger) (07/27/83)
The following two programs convert a binary file into/out of Intel Hex format. They only handle 8080 Hex format files. The program unload converts a file from binary to hex records, load does the inverse operation. The one liner: unload | load is effectively a slow nop. The following script extracts unload and load ------------------------------------------------------------- echo x - load.c cat >load.c <<'!E!O!F!' /* * load - convert a hex file to a com file */ #include <stdio.h> unsigned char checksum; unsigned char getbyte () { register int c; unsigned char x; c = getchar (); if ('0' <= c && c <= '9') x = c - '0'; else if ('A' <= c && c <= 'F') x = c - 'A' + 10; else goto funny; x <<= 4; c = getchar (); if ('0' <= c && c <= '9') x |= c - '0'; else if ('A' <= c && c <= 'F') x |= c - 'A' + 10; else { funny: fprintf (stderr, "Funny hex letter %c\n", c); exit (2); } checksum += x; return x; } main () { register unsigned i, n; char c, buf[64]; unsigned type; do { do { c = getchar (); if (c == EOF) { fprintf (stderr, "Premature EOF colon missing\n"); exit (1); } } while (c != ':'); checksum = 0; n = getbyte (); (void) getbyte (); (void) getbyte (); switch (type = getbyte ()) { case 1: break; case 0: for (i = 0; i < n; i++) buf[i] = getbyte (); fwrite (buf, 1, n, stdout); break; default: fprintf (stderr, "Funny record type %d\n"); exit (1); } (void) getbyte (); if (checksum != 0) { fprintf (stderr, "Checksum error"); exit (2); } } while (type != 1); } !E!O!F! echo x - unload.c cat >unload.c <<'!E!O!F!' /* * unload [files ...] */ #include <stdio.h> #include <ctype.h> #define SIZE 16 main(argc,argv) char *argv[]; { FILE *f; if(argc > 1) while(--argc) { if( (f = fopen(argv[argc], "r")) == NULL) { perror(argv[argc]); exit(1); } printf("%s;\n",argv[argc]); unload(f); fclose(f); } else unload(stdin); } unsigned char check; unload(f) FILE *f; { char buf[SIZE]; register int n,i; int addr = 0x100; while((n = fread(buf, 1, SIZE, f)) > 0) { check = 0; putchar(':'); putbyte(n); putbyte(addr>>8); putbyte(addr&0xff); putbyte(0); addr += n; for(i = 0; i<n; i++) putbyte(buf[i]); putbyte( (-check) & 0xff); putchar('\n'); } printf(":00010001FE\n"); } static char hexcode[] = "0123456789ABCDEF"; putbyte(b) register int b; { putchar( hexcode[(b>>4) & 0xf] ); putchar( hexcode[b & 0xf] ); check += (unsigned char) (b & 0xff); } !E!O!F! -- Stephen Hemminger, Mitre Corp. Bedford MA {allegra,genrad,ihnp4, utzoo}!linus!sch (UUCP) linus!sch@mitre-bedford (ARPA)