home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / enterprs / c128 / util / sfxsrc.lzh / SFX2LZH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-12  |  3.9 KB  |  145 lines

  1. /*  sfx2lzh.c
  2.     ==========================================================================
  3.     Convert Self dissolving LHARChive to a LHARChive            22Jan90 - CS
  4.     ==========================================================================
  5.  
  6.     Usage:  sfx2lzh  infile[.sfx] [outfile[.lzh]]
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #ifdef MPU6502
  13. #include <file.hpp>
  14. #endif
  15.  
  16. #define BUFFER_SIZE 0x4000
  17.  
  18. char buf[BUFFER_SIZE];
  19.  
  20.  
  21. int main(int argc, char ** argv)
  22. {
  23.     FILE * fin, * fout = 0;
  24.     char   infile[64], outfile[64];
  25.     int    size, c, i, ck;
  26.     char * cp;
  27.  
  28.     if(argc < 2) {
  29. #ifdef MPU6502
  30.         conout << "Usage:    " << argv[0] << " infile[.sfx] [outfile[.lzh]]\n\n";
  31.         conout << "Purpose:  Converts self dissolving LHARChive to an LHARChive\n";
  32. #else
  33.         printf("Usage:      %s infile[.sfx] [outfile[.lzh]]\n\n", argv[0]);
  34.         printf("Purpose:    Converts self dissolving LHARChive to an LHARChive\n");
  35. #endif
  36.         exit(1);
  37.     }
  38.  
  39.     strcpy(infile,argv[1]);
  40.     strcpy(outfile,argv[argc-1]);
  41.  
  42.     /*  Open the input file in binary mode, default extension of .SFX */
  43.  
  44.     if (strchr(infile,'.') == 0)
  45.         strcat(infile,".sfx");
  46.  
  47.     if ( (fin = fopen(infile,"rb")) == 0) {
  48. #ifdef MPU6502
  49.         conout << "Can't open " << infile << "\n";
  50. #else
  51.         printf("Can't open %s\n",infile);
  52. #endif
  53.         exit(1);
  54.     }
  55.  
  56.     if (strchr(outfile,'.') == 0)
  57.         strcat(outfile,".lzh");
  58.  
  59.     /*  First buffer full must contain at least one valid LHARC entry header */
  60.  
  61.     size = fread(buf,1,BUFFER_SIZE,fin);
  62.  
  63.     if(size) {
  64.  
  65.         cp = buf;
  66.  
  67.         while(1) {
  68.             cp = memchr(cp,'-',size);
  69.  
  70.             if(cp != 0 && cp < &buf[size]) {
  71.                 if(strncmp(cp,"-LH0-",5) || strncmp(cp,"-LH1-",5) ) {
  72.  
  73.                     /* verify that this is a valid header */
  74.                     /* by calculating its checksum        */
  75.  
  76.                     ck = 0;
  77.  
  78.                     for(i=0; i<cp[-2]; i++) {
  79.                         ck += (cp[i] & 0xff);
  80.                         ck &= 0xff;
  81.                     }
  82.  
  83.                     if(ck != (unsigned char) cp[-1]) {
  84.                         cp++;       /* Bad checksum, it can't be a header */
  85.                     }
  86.                     else {
  87.                         /* if buffer starts with a header its .LZH */
  88.                         if(cp == &buf[2]) {
  89. #ifdef MPU6502
  90.                             conout << "This file is already an LHARChive\nNo conversion is nessessary\n";
  91. #else
  92.                             printf("This file is already an LHARChive\n");
  93.                             printf("No conversion is nessessary\n");
  94. #endif
  95.                             fclose(fin);
  96.                             exit(0);
  97.                         }
  98.                         cp -= 2;
  99.                         i = cp - buf;
  100.                         size -= i;
  101.                         memcpy(buf,cp,size);
  102.                         break;          /* end while */
  103.                     }
  104.                 }
  105.             }
  106.             else {
  107. #ifdef MPU6502
  108.                 conout << infile << " isn't an LHARChive\n";
  109. #else
  110.                 printf("%s isn't an LHARChive\n",infile);
  111. #endif
  112.                 fclose(fin);
  113.                 exit(1);
  114.             }
  115.         }
  116.  
  117.         if ( (fout = fopen(outfile,"wb")) == 0) {
  118. #ifdef MPU6502
  119.             conout << "Can't open " << outfile << "\n";
  120. #else
  121.             printf("Can't open %s\n",outfile);
  122. #endif
  123.             exit(1);
  124.         }
  125.         else {
  126. #ifdef MPU6502
  127.             conout << infile << " --> " << outfile << "\n";
  128. #else
  129.             printf("%s --> %s\n",infile,outfile);
  130. #endif
  131.  
  132.         }
  133.     }
  134.  
  135.     while(size) {
  136.         fwrite(buf,1,size,fout);
  137.         size = fread(buf,1,BUFFER_SIZE,fin);
  138.     }
  139.     fclose(fin);
  140.     if(fout)
  141.         fclose(fout);
  142.     exit(0);
  143. }
  144.  
  145.