home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / fasbin21 / conv.c < prev    next >
C/C++ Source or Header  |  1992-09-16  |  2KB  |  90 lines

  1. /*
  2.    convert FAS Hex-output to binary
  3.    (C) 1992 Michael Schwingen
  4.    Michael Schwingen @ AC3 (Mausnet)
  5.    michaels@pool.informatik.rwth-aachen.de (Internet)
  6.  
  7.    this may be distributed freely.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <tos.h>
  13. #include <ext.h>
  14.  
  15. #define MAX_DATA 65536L
  16. unsigned char data[MAX_DATA];
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.   FILE *fp;
  21.   unsigned i,j,num;
  22.   unsigned adr, last_adr = 0, dat;
  23.   
  24.   for (i=0;i<65535u; i++)
  25.     data[i] = 0xFF;
  26.   if (argc != 3)
  27.   {
  28.     fprintf(stderr,"use: conv infile outfile\n");
  29.     return -1;
  30.   }
  31.   fp = fopen(argv[1],"r");
  32.   if (fp == 0)
  33.   {
  34.     fprintf(stderr,"error: cannot open file %s\n",argv[1]);
  35.     return -1;
  36.   }
  37.   while (1)
  38.   {
  39.     while (getc(fp) != ':')
  40.       ;
  41.     if (fscanf(fp,"%02x",&num) != 1)
  42.     {
  43.       fprintf(stderr,"file %s format error.\n",argv[1]);
  44.       fclose(fp);
  45.       return -1;
  46.     }
  47.     if (num == 0)
  48.     {
  49.       fclose(fp);
  50.       break;
  51.     }
  52.     if (fscanf(fp,"%04x",&adr) != 1)
  53.     {
  54.       fprintf(stderr,"file %s format error.\n",argv[1]);
  55.       fclose(fp);
  56.       return -1;
  57.     }
  58.     if ((long) adr + (long) num > MAX_DATA)
  59.     {
  60.       fprintf(stderr,"file %s: too high address. Buffer space is %ld bytes.\n",argv[1],(long) MAX_DATA);
  61.       fclose(fp);
  62.       return -1;
  63.     }
  64.     for (i=0;i<2;i++)
  65.       getc(fp);
  66.     for (i=0;i<num; i++)
  67.     {
  68.       fscanf(fp,"%02x",&dat);
  69.       data[adr++] = dat;
  70.     }
  71.     fscanf(fp,"%02x",&dat);
  72.     if (adr > last_adr)
  73.       last_adr = adr;
  74.   }
  75.   fclose(fp);
  76.   
  77.   fp = fopen(argv[2],"wb");
  78.   if (fp == 0)
  79.   {
  80.     fprintf(stderr,"error: cannot write file %s\n",argv[2]);
  81.     return -1;
  82.   }
  83.  
  84.   for (j=0;j<8;j++) 
  85.       for (i=0;i<8192;i++)
  86.           fputc(data[i],fp);
  87.   fclose(fp);
  88.   return 0;
  89. }
  90.