Listing 2: A program to test LZH-Light configurations

#define BLOCKSIZE 4000000

void usage()
    {
    printf( "Usage: LZHL {+|-} <input_file> <output_file>\n" );
    }

int main( int argc, char** argv )
    {
    if( argc < 4 )
        {
        usage();
        return 1;
        }
    if( *argv[ 1 ] == '+' )
        {
        FILE* f;
        int i, rawLen, compLen;
        unsigned char* rawBlock;
        unsigned char* compBlock;
        LZHL_CHANDLE comp;
        
        f = fopen( argv[ 2 ], "rb" );
        if( !f )
            abort();
        fseek( f, 0, SEEK_END );
        rawLen = ftell( f );
        fseek( f, 0, SEEK_SET );
        rawBlock = (unsigned char*)malloc( rawLen );
        compBlock = (unsigned char*)
            malloc( LZHLCompressorCalcMaxBuf( rawLen ) );
        fread( rawBlock, 1, rawLen, f );
        fclose( f );

        comp = LZHLCreateCompressor();
        compLen = 0;

        for( i=0; i < rawLen ; i += BLOCKSIZE )
            {
            int l = min( BLOCKSIZE, rawLen - i );
            int lComp = LZHLCompress( comp, compBlock + compLen,
                                      rawBlock + i, l );
            compLen += lComp;
            }
        LZHLDestroyCompressor( comp );

        f = fopen( argv[ 3 ], "wb" );
        if( !f )
            abort();

        // rawLen is stored as a byte sequence to avoid problems 
        // with little_endian/big_endian
        for( i=0; i < 4 ; ++i )
            fputc( (unsigned char)(rawLen >> i*8), f );
        fwrite( compBlock, 1, compLen, f );
        fclose( f );
        }
    else if( *argv[ 1 ] == '-' )
        {
        FILE* f;
        int i, fileLen, rawLen, compLen;
        size_t srcSz, dstSz;
        unsigned char* rawBlock;
        unsigned char* compBlock;
        LZHL_DHANDLE decomp;
        
        f = fopen( argv[ 2 ], "rb" );
        if( !f )
            abort();

        fseek( f, 0, SEEK_END );
        fileLen = ftell( f );
        fseek( f, 0, SEEK_SET );
        if( fileLen < 4 )
            abort();

        rawLen = 0;
        for( i=0; i < 4 ; ++i )
            rawLen |= (fgetc( f ) << i*8);
        compLen = fileLen - 4;

        rawBlock = (unsigned char*)malloc( rawLen );
        compBlock = (unsigned char*)malloc( fileLen );
        if( !rawBlock || !compBlock )
            abort();
        fread( compBlock, 1, fileLen, f );
        fclose( f );

        srcSz = compLen;
        dstSz = rawLen;
        decomp = LZHLCreateDecompressor();
        for(;;)
            {
            int Ok = LZHLDecompress( decomp,
                                     rawBlock + rawLen - dstSz,
                                     &dstSz,
                                     compBlock + compLen - srcSz,
                                     &srcSz );
            if( !Ok )
                abort();
            if( srcSz == 0 )
                break;
            }
        LZHLDestroyDecompressor( decomp );

        f = fopen( argv[ 3 ], "wb" );
        if( !f )
            abort();
        fwrite( rawBlock, 1, rawLen, f );
        fclose( f );
        }
    else
        {
        usage();
        return 1;
        }


    return 0;
    }
//End of File