home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / xtrctbin.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  6KB  |  245 lines

  1. /* ---------------------------------------------------------------------- */
  2. /*                   Copyright (C) 1992 by Natürlich!                     */
  3. /*                      This file is copyrighted!                         */
  4. /*                Refer to the documentation for details.                 */
  5. /* ---------------------------------------------------------------------- */
  6. #include "defines.h"
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #define __BIG_GENERATOR__
  11. #include OSBIND
  12. #if OS == MSDOS
  13. # undef peek
  14. # undef poke
  15. #endif
  16. #include "code.h"
  17.  
  18. /* ---------------------------------------------------------- */
  19. /* rev. history   1.1 --                                      */
  20. /*      Fill up holes option included & C64/AppleII Support   */
  21. /*                1.2 -- no real changes                      */
  22. /* Ref: Bill and Teds bogus Journey Soundtrack.               */
  23. /* ---------------------------------------------------------- */
  24. #if __NSTDC__
  25. void           nferror( char huge *),
  26.                fixheader( void);
  27. #endif
  28.  
  29. struct ffffhead
  30. {
  31.    word  ffff,
  32.          start,
  33.          end;
  34. } header, oldhead;
  35.  
  36.  
  37.  
  38. word   __x;
  39.  
  40. void fixheader()
  41. {
  42.    header.start = dpeek( &header.start);
  43.    header.end   = dpeek( &header.end);
  44. }
  45.  
  46. #if OS == TOS
  47. int     tossable;
  48. #endif
  49. int   fillholes,
  50.    applify,
  51.    cbmify;
  52.  
  53.  
  54. void nferror( s)
  55. char huge *s;
  56. {
  57.    fprintf( stderr, "xtrctbin: Error \"%s\"\n", s);
  58. #if OS == TOS
  59.    keypress();
  60. #endif
  61.    exit(1);
  62. }
  63.  
  64. static char    infile[128], outfile[128];
  65. lword          pos;
  66.  
  67. main( argc, argv)
  68. int   argc;
  69. char  **argv;
  70. {
  71.    int            i = 0,
  72.    fp, fq,
  73.    excellent = 0;
  74.    char huge      *room;
  75.    unsigned long  space,
  76.                   mspace = 0;
  77.    long           bread;
  78.                   
  79.    while( ++i < argc)
  80.    {
  81.       if( *argv[i] == '-')
  82.          switch( argv[i][1])
  83.          {
  84. #if OS == TOS
  85.             case 'T' :
  86.                case 't' :
  87.                   tossable = 1;
  88.                   break;
  89. #endif
  90. #ifdef __DATE__
  91.                   case ':' :
  92.                      fputs( __DATE__, stderr);
  93. # ifdef __TIME__
  94.                   fprintf( stderr, " %s", __TIME__);
  95. # endif                              
  96.                   putc( '\n', stderr);
  97.                   break;
  98. #endif               
  99.                   case 'a' :   
  100.                      case 'A' :
  101.                         if( applify = ! applify)
  102.                            fillholes = 1;
  103.                   break;
  104.                   
  105.                   case 'c' :   
  106.                      case 'C' :
  107.                         if( cbmify = ! cbmify)
  108.                            fillholes = 1;
  109.                   break;
  110.  
  111.                   case 'F' :
  112.                      case 'f' :
  113.                         fillholes = ! fillholes;
  114.                   break;
  115.                   
  116.                   case 'V' :
  117.                      case 'v' :
  118.                         fprintf( stderr, "xtrctbin v1.2 1991 by Natuerlich! -- extracts pure binary data from com files\n");
  119.                   break;
  120.                }
  121.       else
  122.          if( ! infile[0])
  123.             strcpy( infile, argv[i]);
  124.          else
  125.             if( ! outfile[0])
  126.                strcpy( outfile, argv[i]);
  127.             else
  128.                goto usage;
  129.    }
  130.    if( ! (infile[0] && outfile[0]))
  131.       goto usage;
  132.    
  133.    if( (fp = (int) Fopen( infile, 0)) < 0)
  134.       nferror("File open failed");
  135.    if( (fq = (int) Fkreate( outfile, 0x664)) < 0)
  136.       nferror("File creation failed");
  137.    
  138.    if( (bread = Fread( fp, 6L, &header)) < 6)
  139.       nferror("File trunctated");
  140.    if( header.ffff != 0xFFFF)
  141.       nferror("Not a $FF $FF binary file");
  142.    if( applify || cbmify)
  143.    {   
  144.       Fwrite( fq, 2L, &header.start);
  145.       if( applify)
  146.       {
  147.          pos = 2;
  148.          Fwrite( fq, 2L, &header.start);
  149.       }
  150.    }
  151.    
  152.    
  153.    for(;;)
  154.    {
  155.       fixheader();
  156.       space = (unsigned long) (1 + header.end - header.start);
  157.       if( space > mspace)
  158.       {
  159.          if( mspace)
  160.             Mfree( room);
  161.          if( ! (room = (char *) Malloc( (lword) (mspace = space))))
  162.             nferror("out of memory");
  163.       }
  164.       if( excellent && fillholes)
  165.       {
  166.          lword  amount = (lword) header.start - oldhead.end - 1;
  167.          word   i;
  168.  
  169.          memset( room, 0, space);
  170.  
  171.          for( i = amount / space; i--;)
  172.             Fwrite( fq, space, room);
  173.          if( amount %= space)
  174.             Fwrite( fq, amount, room);
  175.       }
  176.  
  177.       if( Fread( fp, space, room) != space)
  178.          nferror("read failed (file truncated ?)");
  179.       if( Fwrite( fq, space, room) != space)
  180.          nferror("write failed (disk full ?)");
  181.       memcpy( &oldhead, &header, (long) sizeof( struct ffffhead));  /*Bogus*/
  182.       excellent = 1;
  183.       if( (bread = Fread( fp, 4L, (char huge *) &header + 2)) <= 0)
  184.          break;
  185.       if( bread < 4)
  186.          nferror("Garbage at end of segment");
  187.       if( header.start == 0xFFFF)
  188.       {
  189.          header.ffff  = header.start;
  190.          header.start = header.end;
  191.          if( (bread = Fread( fp, 2L, (char huge *) &header + 4)) <= 0 ||
  192.              bread < 2)
  193.             nferror("This is a trashed binary");
  194.       }
  195.    }
  196.  
  197.    if( applify)
  198.    {
  199.       lword curr;
  200.       word  bar;
  201.  
  202.       curr = Fseek( 0L, fq, SEEK_CUR);
  203.       Fseek( pos, fq, SEEK_SET);
  204.       pos = curr - pos - 2;
  205.       dpoke( &bar, (word) pos);
  206.       if( Fwrite( fq, 2L, &bar) != 2)
  207.          nferror("out of precious diskspace");
  208.    }
  209.  
  210.  
  211. #if OS == TOS
  212.    keypress();
  213. #endif
  214.    return(0);
  215.  
  216. usage:
  217. #if OS == TOS
  218.    fputs( "usage: xtrctbin [-fact] infile outfile\n", stderr);
  219. #else
  220.    fputs( "usage: xtrctbin [-fac] infile outfile\n", stderr);
  221. #endif
  222.    fputs( "\
  223. \t-a\t: 'Applify' binary, prefaces a ADR,LEN header (& -f option)\n\
  224. \t-c\t: 'C64ify'  binary, prefaces a ADR header (& -f option)\n\
  225. \t-f\t: fill up holes in extracted binary\n\
  226. \t-v\t: version\n", stderr);
  227. #if OS == TOS
  228.    fputs( "\t-t : wait for keypress before exiting\n", stderr);
  229.    keypress();
  230. #endif
  231.    return( 1);
  232. }
  233.  
  234. keypress()
  235. {
  236. #if OS == TOS
  237.    if( tossable)
  238.    {
  239.       while( Bconstat( 2))
  240.          Bconin( 2);
  241.       Bconin( 2);
  242.    }
  243. #endif
  244. }
  245.