///////////////////////////////////////////////////////////////////////////// // Creation 23/12/2003 // // // BZ2IFACETEST.C // -------------- // // // Sylvain MARECHAL - sylvain.marechal1@libertysurf.fr ///////////////////////////////////////////////////////////////////////////// // // Show bz2iface usage // ///////////////////////////////////////////////////////////////////////////// #include #include "bz2iface.h" ///////////////////////////////////////////////////////////////////////////// // // BFZ2IFWRITECALLBACK ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --bfz2IfWriteCallback-- // // The callback used for compression and decompression // // ARGUMENTS // Argument1: void * userData // Argument2: char * pData // Argument3: int cbData // RETOUR/RESULTAT // int // REMARQUE // Rev 23/12/2003 ////////////////////////////////////////////////////////////////////////////// int bfz2IfWriteCallback( void * userData, char * pData, int cbData) { FILE * pFile = (FILE *)userData; if( pData == NULL && cbData == 0 ) { // Happens only for decompression to indicate the end of the file return 0; } return fwrite( pData, 1, cbData, pFile ); } ///////////////////////////////////////////////////////////////////////////// // // BZ2IFTEST ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --bz2ifTest-- // ARGUMENTS // Argument1: const char * pszFileSource // Argument2: const char * pszFileDest // Argument3: int nCompression // RETOUR/RESULTAT // int // REMARQUE // Rev 23/12/2003 ////////////////////////////////////////////////////////////////////////////// int bz2ifTest( const char * pszFileSource, const char * pszFileDest, int nCompression ) { Bz2Iface * pbz2if; FILE * pfSource, * pfDest; char Buffer[5000]; int cbRead; // Open source and dest pfSource = fopen ( pszFileSource, "rb" ); if( ! pfSource ) return -1; pfDest = fopen ( pszFileDest, "wb" ); if( ! pfDest ) { fclose( pfSource ); return -1; } // Create bz2 object interface // nCompression = 0 => decompression of data // nCompression > 0 && < 10 => compression of data pbz2if = Bz2IfCreate( nCompression, bfz2IfWriteCallback, (void * )pfDest ); if( ! pbz2if ) { fclose( pfSource ); fclose( pfDest ); return -1; } // Loop until end of file do { // Read the file cbRead = fread( Buffer, 1, sizeof(Buffer), pfSource ); if( cbRead ) { // Compress date if( Bz2IfWrite( pbz2if, Buffer, cbRead ) < 0 ) { printf( "Bz2IfWrite() Error\n" ); fclose( pfSource ); fclose( pfDest ); Bz2IfDestroy( pbz2if ); return -1; } } } while( cbRead > 0 ); // Indicates end of file for compressio if( nCompression ) { if( Bz2IfWrite( pbz2if, NULL, 0 ) < 0 ) { printf( "Bz2IfWrite() end Error\n" ); fclose( pfSource ); fclose( pfDest ); Bz2IfDestroy( pbz2if ); return -1; } } // Close files fclose( pfSource ); fclose( pfDest ); // Cleanup Bz2IfDestroy( pbz2if ); return 0; } ///////////////////////////////////////////////////////////////////////////// // // MAIN ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --main-- // // Usage: bz2ifacetest // (c for compression) // // ARGUMENTS // Argument1: int argc // Argument2: char * argv[] // RETOUR/RESULTAT // int // REMARQUE // Rev 23/12/2003 ////////////////////////////////////////////////////////////////////////////// int main( int argc, char * argv[] ) { int fCompress; if( argc != 4 ) { printf( "Usage: %s \n", argv[0] ); return 1; } fCompress = argv[1][0] == 'c'; if( fCompress ) { bz2ifTest( argv[2], argv[3], 1 ); } else { bz2ifTest( argv[2], argv[3], 0 ); } return 0; }