/*  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>

#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) {
        printf("Usage:      %s infile[.sfx] [outfile[.lzh]]\n\n", argv[0]);
        printf("Purpose:    Converts self dissolving LHARChive to an LHARChive\n");
        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) {
        printf("Can't open %s\n",infile);
        exit(1);
    }

    if (strchr(outfile,'.') == 0)
        strcat(outfile,".lzh");

    /*  First buffer 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, try again */
                    }
                    else {
                        if(cp == &buf[2]) {
                            printf("This file is already an LHARChive\n");
                            printf("No conversion is nessessary\n");
                            fclose(fin);
                            exit(0);
                        }
                        cp -= 2;
                        i = cp - buf;
                        size -= i;
                        memcpy(buf,cp,size);
                        break;          /* end while */
                    }
                }
            }
            else {
                printf("%s isn't an LHARChive\n",infile);
                fclose(fin);
                exit(1);
            }
        }

        if ( (fout = fopen(outfile,"wb")) == 0) {
            printf("Can't open %s\n",outfile);
            exit(1);
        }
    }

    while(size) {
        fwrite(buf,1,size,fout);
        size = fread(buf,1,BUFFER_SIZE,fin);
    }
    fclose(fin);
    if(fout)
        fclose(fout);
    exit(0);
}

