00001 #include <j2k/Fred/Basic.hpp>
00002 #include <time.h>
00003 #include <memory.h>
00004
00005 #include <j2k/Fred/LZH/lzhl.h>
00006
00007 #define BLOCKSIZE 4000000
00008
00009 void usage() {
00010 printf( "Usage: LZHL {+|-} <input_file> <output_file>\n" );
00011 }
00012
00013 int main( int argc, char** argv ) {
00014 if ( argc < 4 ) {
00015 usage();
00016 return 1;
00017 }
00018
00019 if ( *argv[ 1 ] == '+' ) {
00020 FILE* f;
00021 int i, rawLen, compLen;
00022 unsigned char* rawBlock;
00023 unsigned char* compBlock;
00024 clock_t start, finish;
00025 LZHL_CHANDLE comp;
00026
00027 f = fopen( argv[ 2 ], "rb" );
00028 if ( !f )
00029 abort();
00030
00031 fseek( f, 0, SEEK_END );
00032 rawLen = ftell( f );
00033 fseek( f, 0, SEEK_SET );
00034 rawBlock = (unsigned char*)malloc( rawLen );
00035 compBlock = (unsigned char*)malloc( LZHLCompressorCalcMaxBuf( rawLen ) );
00036 fread( rawBlock, 1, rawLen, f );
00037 fclose( f );
00038
00039 start = clock();
00040 comp = LZHLCreateCompressor();
00041 compLen = 0;
00042
00043 for( i=0; i < rawLen ; i += BLOCKSIZE ) {
00044 int l = min( BLOCKSIZE, rawLen - i );
00045 int lComp = LZHLCompress( comp, compBlock + compLen, rawBlock + i, l );
00046 compLen += lComp;
00047 }
00048
00049 LZHLDestroyCompressor( comp );
00050 finish = clock();
00051 printf( "%d ticks\n", finish - start );
00052
00053 f = fopen( argv[ 3 ], "wb" );
00054 if ( !f )
00055 abort();
00056
00057
00058
00059
00060 for ( i=0; i < 4 ; ++i )
00061 fputc( (unsigned char)(rawLen >> i*8), f );
00062
00063 fwrite( compBlock, 1, compLen, f );
00064 fclose( f );
00065
00066
00067 } else if( *argv[ 1 ] == '-' ) {
00068
00069 FILE* f;
00070 int i, fileLen, rawLen, compLen;
00071 size_t srcSz, dstSz;
00072 unsigned char* rawBlock;
00073 unsigned char* compBlock;
00074 clock_t start, finish;
00075 LZHL_DHANDLE decomp;
00076
00077 f = fopen( argv[ 2 ], "rb" );
00078 if ( !f )
00079 abort();
00080
00081 fseek( f, 0, SEEK_END );
00082 fileLen = ftell( f );
00083 fseek( f, 0, SEEK_SET );
00084
00085 if ( fileLen < 4 )
00086 abort();
00087
00088 rawLen = 0;
00089 for ( i=0; i < 4 ; ++i )
00090 rawLen |= (fgetc( f ) << i*8);
00091
00092 compLen = fileLen - 4;
00093 rawBlock = (unsigned char*)malloc( rawLen );
00094 compBlock = (unsigned char*)malloc( fileLen );
00095
00096 if ( !rawBlock || !compBlock )
00097 abort();
00098
00099 fread( compBlock, 1, fileLen, f );
00100 fclose( f );
00101
00102 start = clock();
00103 srcSz = compLen;
00104 dstSz = rawLen;
00105 decomp = LZHLCreateDecompressor();
00106
00107 for(;;) {
00108 int Ok = LZHLDecompress( decomp, rawBlock + rawLen - dstSz, &dstSz, compBlock + compLen - srcSz, &srcSz );
00109 if ( !Ok )
00110 abort();
00111
00112 if ( srcSz == 0 )
00113 break;
00114
00115 }
00116
00117 LZHLDestroyDecompressor( decomp );
00118 finish = clock();
00119 printf( "%d ticks\n", finish - start );
00120
00121 f = fopen( argv[ 3 ], "wb" );
00122 if ( !f )
00123 abort();
00124
00125 fwrite( rawBlock, 1, rawLen, f );
00126 fclose( f );
00127
00128 } else {
00129 usage();
00130 return 1;
00131 }
00132
00133 return 0;
00134 }