home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / tnet_x1h.zip / INTEL.C < prev    next >
Text File  |  1992-10-18  |  6KB  |  191 lines

  1. /* ************************************************************************
  2.  *
  3.  * Binary to hex converter for intellec format hex
  4.  *
  5.  * (C) By Dave Roberts G8KBB.
  6.  * This program is provided 'as is', with no warranty. It has been
  7.  * produced for use in self-tuition in amateur radio and is not
  8.  * to be used for commercial applications. It may be freely copied and
  9.  * used under these conditions.
  10.  *
  11.  * Invocation :
  12.  *
  13.  *    INTEL infile outfile [ hex_start_address ]
  14.  *
  15.  * Return codes : AS detailed in the source. 0 for success, error
  16.  *                codes 1 - 5 errors as detailed below.
  17.  *
  18.  *************************************************************************/
  19.  
  20. #include <stdio.h>
  21.  
  22. unsigned Address;        /* effective address of data */
  23. unsigned char Buffer[32786];    /* buffer for i/p data from file */
  24. unsigned char *Ptr;        /* pointer into buffer for reading */
  25. unsigned Count;            /* number of bytes left in buffer */
  26. FILE *Fpin, *Fpout;        /* input & output file handles */
  27.  
  28. /* ***********************************************************************
  29.  * this is the main program.
  30.  * It exits with an error code of 0 if all went correctly.
  31.  * With 1 for a syntax error
  32.  *      2 for an input file open error
  33.  *      3 for an output file open error
  34.  *      4 for an input file read error
  35.  *      5 for an output file write error
  36.  * ***********************************************************************/
  37.  
  38. main( argc, argv )
  39. char *argv[];
  40. {
  41.     int ecode;        /* error code on exit from main loop */
  42.     char *tmpptr;        /* scratch pointer for strtol() */
  43.     int cont;        /* flag to say stop looping */
  44.  
  45.     Address = 0;        /* address is zero unless user inputs one */
  46.     Count = 0;        /* mark buffer as empty */
  47.     ecode = 0;        /* error code will be zero if all goes well */
  48.     Ptr = Buffer;        /* point to start of buffer */
  49.     cont = 1;        /* flag used to loop in processing data */
  50.  
  51.     /* we need an input file & an output file.
  52.      * A start address is optional
  53.      * so need 2 or 3 parameters !
  54.      */
  55.     if( argc < 3 || argc > 4 )
  56.     {
  57.         printf( "Error - usage is :\n    intel infile outfile [ start-address ]\n");
  58.         exit( 1 );
  59.     }
  60.  
  61.     /* OK, we seem to have the parameters - so let's open the input
  62.      * file in BINARY !!!! mode.
  63.      * Error & exit if cannot.
  64.      */
  65.     if( ( Fpin = fopen( argv[1], "rb"  ) ) == NULL )
  66.     {
  67.         perror("Input file error ");
  68.         exit( 2 );
  69.     }
  70.  
  71.     /* Next, lets open the output file for write, in default mode
  72.      * Error & exit if cannot
  73.      */
  74.     if( ( Fpout = fopen( argv[2], "w" ) ) == NULL )
  75.     {
  76.         perror("Output file error ");
  77.         exit( 3 );
  78.     }
  79.  
  80.     /* Now the optional start address.
  81.      * WARNING - being a lazy sod, just take the last 16 bits
  82.      */
  83.     if( argc == 4 )
  84.         Address = strtol( argv[3], &tmpptr, 16 );
  85.  
  86.     /* MAIN LOOP - keep doing this until there is no more data
  87.      * or until an error occurs.
  88.      *
  89.      * Read a buffer full if there is less than 16 bytes left in buffer
  90.      * Crash out if there is a read error.
  91.      * Dump either 16 bytes, or whatever is left to dump
  92.      * Crash out if write error
  93.      * Exit anyway if there was less than 16 bytes ( as we did not 
  94.      * read any more )
  95.      */
  96.     while( cont )
  97.     {
  98.         if( Count < 16 )
  99.         {
  100.             if( !getdata() )
  101.             {
  102.                 perror("Input file ");
  103.                 ecode = 4;
  104.                 break;
  105.             }
  106.         }
  107.         if( Count < 16 )
  108.             cont = 0;
  109.         if( !dump( Count >= 16 ? 16 : Count ) )
  110.         {
  111.             perror("Output file ");
  112.             ecode = 5;
  113.             break;
  114.         }
  115.     }
  116.  
  117.     /* dump trailing line
  118.      * close files
  119.      * and exit.
  120.      */
  121.     fprintf( Fpout, ":00000001ff\n" );
  122.     fclose( Fpin );
  123.     fclose( Fpout );
  124.     return( ecode );
  125. }
  126.  
  127. /* ************************************************************************
  128.  * This function reads data from the input file.
  129.  * Firstly, if there is still a bit of data left in the buffer, move
  130.  * it down to the bottom of the buffer.
  131.  * Then point to the start of the buffer so we handle that data first.
  132.  * Now try to fill the buffer up.
  133.  * If we read less than we expected, just check to see that the reason
  134.  * was end of file & if not die horribly.
  135.  * Now check for file read errors for additional ways to die.
  136.  * Finally, update the byte counter to reflect the new buffer size & return
  137.  * a return of 0 means error, 1 means all went well.
  138.  * ************************************************************************/
  139. getdata()
  140. {
  141.     unsigned int i;
  142.  
  143.     if( Count != 0 )
  144.         memmove( Buffer, Ptr, Count );
  145.     Ptr = Buffer;
  146.     i = fread( Buffer+Count, sizeof( char ), sizeof(Buffer)-Count, Fpin );
  147.     if( i != sizeof(Buffer)-Count && !feof( Fpin ) )
  148.         return( 0 );
  149.     if( ferror( Fpin ) )
  150.         return( 0 );
  151.     Count += i;
  152.     return( 1 );
  153. }
  154.  
  155. /* *************************************************************************
  156.  * Dump function for a line of data.
  157.  * This outputs a single line of (num) bytes of data.
  158.  * It checks each write for an error.
  159.  * Firstly, it sends the line preamble, count, address and type.
  160.  * Then it sends each byte in turn.
  161.  * Finally it computes the checksum & sends that too.
  162.  * A return of 0 means error, a return of 1 means all went well.
  163.  * ************************************************************************/
  164. dump( num )
  165. unsigned num;
  166. {
  167.     int i;
  168.     unsigned char csum;
  169.  
  170.     if( num == 0 )
  171.         return( 1 );
  172.     fprintf( Fpout, ":%02x%04x%02x", num, Address, 0 );
  173.     if( ferror( Fpout ) )
  174.         return( 0 );
  175.     csum = num + ( (Address >> 8) & 0xff) + (Address & 0xff);
  176.     for( i=0; i<num; i++ )
  177.     {
  178.         fprintf( Fpout, "%02x", *Ptr );
  179.         if( ferror( Fpout ) )
  180.             return( 0 );
  181.         csum += *Ptr++;
  182.         Count--;
  183.         if( ! ++Address )
  184.             printf("WARNING. The address exceeded 0xffff.\n");
  185.     }
  186.     fprintf( Fpout, "%02x\n", (0-csum) & 0xff );
  187.     if( ferror( Fpout ) )
  188.         return( 0 );
  189.     return( 1 );
  190. }
  191.