home *** CD-ROM | disk | FTP | other *** search
- /* sfx2lzh.c
- ==========================================================================
- Convert Self dissolving LHARChive to a LHARChive 22Jan90 - CS
- ==========================================================================
-
- Usage: sfx2lzh infile[.sfx] [outfile[.lzh]]
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #ifdef MPU6502
- #include <file.hpp>
- #endif
-
- #define BUFFER_SIZE 0x4000
-
- char buf[BUFFER_SIZE];
-
-
- int main(int argc, char ** argv)
- {
- FILE * fin, * fout = 0;
- char infile[64], outfile[64];
- int size, c, i, ck;
- char * cp;
-
- if(argc < 2) {
- #ifdef MPU6502
- conout << "Usage: " << argv[0] << " infile[.sfx] [outfile[.lzh]]\n\n";
- conout << "Purpose: Converts self dissolving LHARChive to an LHARChive\n";
- #else
- printf("Usage: %s infile[.sfx] [outfile[.lzh]]\n\n", argv[0]);
- printf("Purpose: Converts self dissolving LHARChive to an LHARChive\n");
- #endif
- exit(1);
- }
-
- strcpy(infile,argv[1]);
- strcpy(outfile,argv[argc-1]);
-
- /* Open the input file in binary mode, default extension of .SFX */
-
- if (strchr(infile,'.') == 0)
- strcat(infile,".sfx");
-
- if ( (fin = fopen(infile,"rb")) == 0) {
- #ifdef MPU6502
- conout << "Can't open " << infile << "\n";
- #else
- printf("Can't open %s\n",infile);
- #endif
- exit(1);
- }
-
- if (strchr(outfile,'.') == 0)
- strcat(outfile,".lzh");
-
- /* First buffer full must contain at least one valid LHARC entry header */
-
- size = fread(buf,1,BUFFER_SIZE,fin);
-
- if(size) {
-
- cp = buf;
-
- while(1) {
- cp = memchr(cp,'-',size);
-
- if(cp != 0 && cp < &buf[size]) {
- if(strncmp(cp,"-LH0-",5) || strncmp(cp,"-LH1-",5) ) {
-
- /* verify that this is a valid header */
- /* by calculating its checksum */
-
- ck = 0;
-
- for(i=0; i<cp[-2]; i++) {
- ck += (cp[i] & 0xff);
- ck &= 0xff;
- }
-
- if(ck != (unsigned char) cp[-1]) {
- cp++; /* Bad checksum, it can't be a header */
- }
- else {
- /* if buffer starts with a header its .LZH */
- if(cp == &buf[2]) {
- #ifdef MPU6502
- conout << "This file is already an LHARChive\nNo conversion is nessessary\n";
- #else
- printf("This file is already an LHARChive\n");
- printf("No conversion is nessessary\n");
- #endif
- fclose(fin);
- exit(0);
- }
- cp -= 2;
- i = cp - buf;
- size -= i;
- memcpy(buf,cp,size);
- break; /* end while */
- }
- }
- }
- else {
- #ifdef MPU6502
- conout << infile << " isn't an LHARChive\n";
- #else
- printf("%s isn't an LHARChive\n",infile);
- #endif
- fclose(fin);
- exit(1);
- }
- }
-
- if ( (fout = fopen(outfile,"wb")) == 0) {
- #ifdef MPU6502
- conout << "Can't open " << outfile << "\n";
- #else
- printf("Can't open %s\n",outfile);
- #endif
- exit(1);
- }
- else {
- #ifdef MPU6502
- conout << infile << " --> " << outfile << "\n";
- #else
- printf("%s --> %s\n",infile,outfile);
- #endif
-
- }
- }
-
- while(size) {
- fwrite(buf,1,size,fout);
- size = fread(buf,1,BUFFER_SIZE,fin);
- }
- fclose(fin);
- if(fout)
- fclose(fout);
- exit(0);
- }
-
-