From 39c7f05ea96bea83f97d3b80056046314b7f27c8 Mon Sep 17 00:00:00 2001 From: Vikram Rajkumar Date: Wed, 2 Jul 2014 23:28:08 -0400 Subject: [PATCH] Implement LZMA file decompression --- .gitignore | 5 +- CMakeLists.txt | 4 +- include/fc/compress/lzma.hpp | 7 ++- src/compress/lzma.cpp | 56 +++++++++---------- .../{lzma_compress_file.cpp => lzma_test.cpp} | 9 +-- 5 files changed, 44 insertions(+), 37 deletions(-) rename tests/{lzma_compress_file.cpp => lzma_test.cpp} (59%) diff --git a/.gitignore b/.gitignore index 0a8ee06..e956fca 100644 --- a/.gitignore +++ b/.gitignore @@ -33,9 +33,11 @@ ZERO_CHECK Debug/ Release/ +CMakeCache.txt CMakeFiles Makefile -cmake_install.cmake +*.cmake + libfc.a libfc_debug.a @@ -45,6 +47,7 @@ fc_automoc.cpp git_revision.cpp GitSHA3.cpp +lzma_test ntp_test task_cancel_test udt_client diff --git a/CMakeLists.txt b/CMakeLists.txt index 6775d43..78812b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -237,8 +237,8 @@ target_link_libraries( udt_server fc udt ) add_executable( udt_client tests/udtc.cpp ) target_link_libraries( udt_client fc udt ) -add_executable( lzma_compress_file tests/lzma_compress_file.cpp ) -target_link_libraries( lzma_compress_file fc ) +add_executable( lzma_test tests/lzma_test.cpp ) +target_link_libraries( lzma_test fc ) #add_executable( test_compress tests/compress.cpp ) #target_link_libraries( test_compress fc ) diff --git a/include/fc/compress/lzma.hpp b/include/fc/compress/lzma.hpp index e1b411c..44d17b7 100644 --- a/include/fc/compress/lzma.hpp +++ b/include/fc/compress/lzma.hpp @@ -8,9 +8,12 @@ namespace fc { std::vector lzma_compress( const std::vector& in ); std::vector lzma_decompress( const std::vector& compressed ); -void lzma_compress_file( path src_path, - path dst_path, +void lzma_compress_file( const path& src_path, + const path& dst_path, unsigned char level = 5, unsigned int dict_size = (1 << 20) ); +void lzma_decompress_file( const path& src_path, + const path& dst_path ); + } // namespace fc diff --git a/src/compress/lzma.cpp b/src/compress/lzma.cpp index 91d5475..21ad80c 100644 --- a/src/compress/lzma.cpp +++ b/src/compress/lzma.cpp @@ -1,15 +1,8 @@ +#include #include #include - -#include - -#include - #include - - - -#include +#include namespace fc { @@ -120,8 +113,8 @@ static size_t output_callback( void* output_ctx, const void* output_buf, size_t return output_len; } -void lzma_compress_file( path src_path, - path dst_path, +void lzma_compress_file( const path& src_path, + const path& dst_path, unsigned char level, unsigned int dict_size ) { @@ -170,29 +163,36 @@ void lzma_compress_file( path src_path, elzma_compress_free( &handle ); FC_ASSERT( rc == ELZMA_E_OK ); +} +void lzma_decompress_file( const path& src_path, + const path& dst_path ) +{ + FC_ASSERT( exists( src_path ) ); + FC_ASSERT( !exists( dst_path ) ); - /* TEST */ - FC_ASSERT( exists( dst_path ) ); + boost::iostreams::mapped_file_source src_file; + src_file.open( src_path.string() ); + FC_ASSERT( src_file.is_open() ); - boost::iostreams::mapped_file_source dst_file; - dst_file.open( dst_path.string() ); - FC_ASSERT( dst_file.is_open() ); + elzma_decompress_handle handle = NULL; + handle = elzma_decompress_alloc(); + FC_ASSERT( handle != NULL ); - std::vector result( dst_file.data(), dst_file.data() + dst_file.size() ); - dst_file.close(); + struct lzma_file_ctx ctx; + ctx.src_buf = ( const unsigned char* )src_file.data(); + ctx.src_len = src_file.size(); + ctx.dst_path = dst_path; - for( const auto& c : result ) - { - std::cout << c; - } - std::cout << "\n"; + auto rc = elzma_decompress_run( handle, + input_callback, + ( void * )&ctx, + output_callback, + ( void * )&ctx, + elzma_file_format::ELZMA_lzma ); - result = lzma_decompress( result ); - for( const auto& c : result ) - { - std::cout << c; - } + elzma_decompress_free( &handle ); + FC_ASSERT( rc == ELZMA_E_OK ); } } // namespace fc diff --git a/tests/lzma_compress_file.cpp b/tests/lzma_test.cpp similarity index 59% rename from tests/lzma_compress_file.cpp rename to tests/lzma_test.cpp index 3f76733..3457597 100644 --- a/tests/lzma_compress_file.cpp +++ b/tests/lzma_test.cpp @@ -8,16 +8,17 @@ using namespace fc; int main( int argc, char** argv ) { - if( argc != 2 && argc != 3 ) + if( argc != 2 ) { - std::cout << "usage: " << argv[0] << " [dst_path = src_path.lzma]\n"; + std::cout << "usage: " << argv[0] << " \n"; exit( -1 ); } auto src = std::string( argv[1] ); - auto dst = (argc == 3) ? std::string( argv[2] ) : src + ".lzma"; - + auto dst = src + ".compressed"; lzma_compress_file( src, dst ); + lzma_decompress_file( dst, src + ".decompressed" ); + return 0; }