From f5352edf3dc23c044a91f1d1537fa0dc0f0babc7 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 9 Jun 2007 05:43:04 +0000 Subject: Alright, looks like the bzip2 filter can decompress just fine. It won't try to compensate for overshooting the end of the compression block yet, which it won't be able to do on streams that don't support seeking...I think I'll make it only try on stop commands, and try to re-use the buffer otherwise...maybe...it's an interesting problem since it *always* overshoots (unless you're really, really lucky...) --- src/bzip2.cpp | 37 ++++++++++++++++++++++++++++++------- src/tests/bzip2.cpp | 10 +++++----- 2 files changed, 35 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/bzip2.cpp b/src/bzip2.cpp index b8c0f74..433fc91 100644 --- a/src/bzip2.cpp +++ b/src/bzip2.cpp @@ -12,14 +12,11 @@ Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) : Bu::BZip2::~BZip2() { - printf("-> Bu::BZip2::~BZip2()\n"); stop(); } void Bu::BZip2::start() { - printf("-> Bu::BZip2::start()\n"); - printf("Hey, it's starting...\n"); bzState.state = NULL; bzState.bzalloc = NULL; bzState.bzfree = NULL; @@ -31,11 +28,11 @@ void Bu::BZip2::start() void Bu::BZip2::stop() { - printf("-> Bu::BZip2::stop()\n"); if( bzState.state ) { if( bReading ) { + BZ2_bzDecompressEnd( &bzState ); } else { @@ -81,13 +78,39 @@ size_t Bu::BZip2::read( void *pData, size_t nBytes ) if( !bzState.state ) { bReading = true; + BZ2_bzDecompressInit( &bzState, 0, 0 ); + bzState.next_in = pBuf; + bzState.avail_in = 0; } if( bReading == false ) throw ExceptionBase("This bzip2 filter is in writing mode, you can't read."); - //bzState.next_in = pData; - //bzState.avail_in = nSizeIn; + + int nRead = 0; + int nReadTotal = bzState.total_out_lo32; + for(;;) + { + bzState.next_out = (char *)pData; + bzState.avail_out = nBytes; + int ret = BZ2_bzDecompress( &bzState ); - //printf("%db at [%08X] (%db)\n", bzState.avail_in, (uint32_t)bzState.next_in, bzState.total_in_lo32 ); + nReadTotal += nRead-bzState.avail_out; + + if( ret == BZ_STREAM_END ) + { + return nBytes-bzState.avail_out; + } + + if( bzState.avail_out ) + { + nRead = rNext.read( pBuf, nBufSize ); + bzState.next_in = pBuf; + bzState.avail_in = nRead; + } + else + { + return nBytes-bzState.avail_out; + } + } return 0; } diff --git a/src/tests/bzip2.cpp b/src/tests/bzip2.cpp index 683d3d7..ef9328f 100644 --- a/src/tests/bzip2.cpp +++ b/src/tests/bzip2.cpp @@ -6,17 +6,17 @@ int main( int argc, char *argv[] ) char buf[1024]; size_t nRead; - Bu::File f( "test.bz2", "wb" ); + Bu::File f( "test.bz2", "rb" ); Bu::BZip2 bz2( f ); - Bu::File fin( argv[1], "rb"); + Bu::File fin( argv[1], "wb"); for(;;) { - nRead = fin.read( buf, 1024 ); + nRead = bz2.read( buf, 1024 ); if( nRead > 0 ) - bz2.write( buf, nRead ); - if( fin.isEOS() ) + fin.write( buf, nRead ); + if( bz2.isEOS() ) break; } } -- cgit v1.2.3