home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / cpm68k / utils.lbr / FROMHEX.CQ / FROMHEX.C
Encoding:
Text File  |  1986-05-22  |  1.8 KB  |  75 lines

  1. /* -*-c,save-*- */
  2. #include <stdio.h>
  3. #define FAST register
  4. #define LOCAL static
  5. #define GLOBAL extern
  6. #define BLOCK_SIZE 32
  7.  
  8.  
  9. main(argc,argv)
  10. int argc;
  11. char *argv[];
  12. {FAST FILE *infile,*outfile;
  13.  FILE *fopenb(),*fopen();
  14.  char block_buff[BLOCK_SIZE];
  15.  FAST int mrec,bytes;
  16.  
  17.      if (argc != 3) {
  18.         fprintf(stderr,"Usage: fromhex infile outfile\n");
  19.         abort(1);
  20.         }
  21.     infile = fopen(argv[1],"r");
  22.     if (infile == NULL) {
  23.         perror("fromhex: fopen error (input file)");
  24.         abort(2);
  25.         }
  26.     outfile = fopenb(argv[2],"w");
  27.     if (outfile == NULL) {
  28.         perror("fromhex: fopen error (output file)");
  29.         abort(2);
  30.         }
  31.     mrec = 0;
  32.     while ((bytes = get_hex(&block_buff[0],mrec,BLOCK_SIZE,infile)) > 0) {
  33.         fwrite(&block_buff[0],1,bytes,outfile);
  34.         mrec++;
  35.         }
  36.     fclose(infile);
  37.     fclose(outfile);
  38.     printf("Blocks read: %04x\n",mrec);
  39.     }
  40.  
  41. get_hex(buff,recnum,buffsiz,infile)
  42. FAST char *buff;
  43. FAST int buffsiz;
  44. int recnum;
  45. FAST FILE *infile;
  46. {
  47.     FAST int n,chk2,b;
  48.     int checksum,rec_num,block_len,byte;
  49.     
  50.     while (getc(infile) != ';') ;
  51.     fscanf(infile,"%02x%04x",&block_len,&rec_num);
  52.     chk2 = block_len + (rec_num & 0x00ff) + ((rec_num >> 8) & 0x00ff);
  53.     b = 0;
  54.     while ((block_len--)>0) {
  55.     fscanf(infile,"%02x",&byte);
  56.     byte &= 0x00ff;
  57.     chk2 += byte;
  58.     *buff++ = byte;
  59.     b++;
  60.     if ((buffsiz--)<1) {fprintf(stderr,"Buffer overflow\n");abort(3);}
  61.     }
  62.     fscanf(infile,"%04x",&checksum);
  63.  
  64.     if (checksum != chk2) {
  65.     fprintf(stderr,"Checksum error, recnum=%04x\n",rec_num);
  66.     abort(4);
  67.     }
  68.     if (recnum != rec_num) {
  69.     fprintf(stderr,"Phase error, expect record %04x, got record  %04x\n",
  70.         recnum,rec_num);
  71.     abort(5);
  72.     }
  73.     return(b);
  74.     }
  75.