home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / enterprs / c128 / util / sfxsrc.arc / SFX2LZH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1980-01-01  |  3.0 KB  |  112 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.  
  13. #define BUFFER_SIZE 0x4000
  14.  
  15. char buf[BUFFER_SIZE];
  16.  
  17.  
  18. int main(int argc, char ** argv)
  19. {
  20.     FILE * fin, * fout = 0;
  21.     char   infile[64], outfile[64];
  22.     int    size, c, i, ck;
  23.     char * cp;
  24.  
  25.     if(argc < 2) {
  26.         printf("Usage:      %s infile[.sfx] [outfile[.lzh]]\n\n", argv[0]);
  27.         printf("Purpose:    Converts self dissolving LHARChive to an LHARChive\n");
  28.         exit(1);
  29.     }
  30.  
  31.     strcpy(infile,argv[1]);
  32.     strcpy(outfile,argv[argc-1]);
  33.  
  34.     /*  Open the input file in binary mode, default extension of .SFX */
  35.  
  36.     if (strchr(infile,'.') == 0)
  37.         strcat(infile,".sfx");
  38.  
  39.     if ( (fin = fopen(infile,"rb")) == 0) {
  40.         printf("Can't open %s\n",infile);
  41.         exit(1);
  42.     }
  43.  
  44.     if (strchr(outfile,'.') == 0)
  45.         strcat(outfile,".lzh");
  46.  
  47.     /*  First buffer must contain at least one valid LHARC entry header */
  48.  
  49.     size = fread(buf,1,BUFFER_SIZE,fin);
  50.  
  51.     if(size) {
  52.  
  53.         cp = buf;
  54.  
  55.         while(1) {
  56.             cp = memchr(cp,'-',size);
  57.  
  58.             if(cp != 0 && cp < &buf[size]) {
  59.                 if(strncmp(cp,"-LH0-",5) || strncmp(cp,"-LH1-",5) ) {
  60.  
  61.                     /* verify that this is a valid header */
  62.                     /* by calculating its checksum        */
  63.  
  64.                     ck = 0;
  65.  
  66.                     for(i=0; i<cp[-2]; i++) {
  67.                         ck += (cp[i] & 0xff);
  68.                         ck &= 0xff;
  69.                     }
  70.  
  71.                     if(ck != (unsigned char) cp[-1]) {
  72.                         cp++;       /* Bad checksum, try again */
  73.                     }
  74.                     else {
  75.                         if(cp == &buf[2]) {
  76.                             printf("This file is already an LHARChive\n");
  77.                             printf("No conversion is nessessary\n");
  78.                             fclose(fin);
  79.                             exit(0);
  80.                         }
  81.                         cp -= 2;
  82.                         i = cp - buf;
  83.                         size -= i;
  84.                         memcpy(buf,cp,size);
  85.                         break;          /* end while */
  86.                     }
  87.                 }
  88.             }
  89.             else {
  90.                 printf("%s isn't an LHARChive\n",infile);
  91.                 fclose(fin);
  92.                 exit(1);
  93.             }
  94.         }
  95.  
  96.         if ( (fout = fopen(outfile,"wb")) == 0) {
  97.             printf("Can't open %s\n",outfile);
  98.             exit(1);
  99.         }
  100.     }
  101.  
  102.     while(size) {
  103.         fwrite(buf,1,size,fout);
  104.         size = fread(buf,1,BUFFER_SIZE,fin);
  105.     }
  106.     fclose(fin);
  107.     if(fout)
  108.         fclose(fout);
  109.     exit(0);
  110. }
  111.  
  112.