From 9e8a4944e50fab432012878c66e1bdac20649f76 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 7 Jun 2007 19:56:20 +0000 Subject: The Stream Filter archetecture is finished, it's actually much cooler than I had anticipated, and much cleaner. I'll have to add some documentation to it, because it's not really obvious how any of it fits together from the outset, although I have to say that the bzip2 test program is the easiest general bzip2 compression program I've ever made...it just goes :) Decompression in Bu::BZip2 isn't finished yet, but that's ok, it's coming soon. --- src/tests/bzip2.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/tests/bzip2.cpp (limited to 'src/tests/bzip2.cpp') diff --git a/src/tests/bzip2.cpp b/src/tests/bzip2.cpp new file mode 100644 index 0000000..683d3d7 --- /dev/null +++ b/src/tests/bzip2.cpp @@ -0,0 +1,23 @@ +#include "bu/bzip2.h" +#include "bu/file.h" + +int main( int argc, char *argv[] ) +{ + char buf[1024]; + size_t nRead; + + Bu::File f( "test.bz2", "wb" ); + Bu::BZip2 bz2( f ); + + Bu::File fin( argv[1], "rb"); + + for(;;) + { + nRead = fin.read( buf, 1024 ); + if( nRead > 0 ) + bz2.write( buf, nRead ); + if( fin.isEOS() ) + break; + } +} + -- cgit v1.2.3 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/tests/bzip2.cpp') 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 From 408aca47fd423e7c4c38665b892a13c1c9fb1e9a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 11 Jun 2007 07:31:52 +0000 Subject: Few minor tweaks to bzip2, it reports errors now...and there's a bug in odpm that could be in this, but it's going to be hard to tell... --- src/bzip2.cpp | 31 +++++++++++++++++++++++++++---- src/tests/bzip2.cpp | 10 +++++----- 2 files changed, 32 insertions(+), 9 deletions(-) (limited to 'src/tests/bzip2.cpp') diff --git a/src/bzip2.cpp b/src/bzip2.cpp index 5423a10..6bb1429 100644 --- a/src/bzip2.cpp +++ b/src/bzip2.cpp @@ -64,6 +64,13 @@ void Bu::BZip2::bzError( int code ) switch( code ) { case BZ_OK: + printf("\n"); return; + case BZ_RUN_OK: + printf("\n"); return; + case BZ_FLUSH_OK: + printf("\n"); return; + case BZ_FINISH_OK: + printf("\n"); return; return; case BZ_CONFIG_ERROR: @@ -117,20 +124,36 @@ size_t Bu::BZip2::read( void *pData, size_t nBytes ) { bzState.next_out = (char *)pData; bzState.avail_out = nBytes; + printf(" (pre) in: %db, out: %db\n", bzState.avail_in, bzState.avail_out ); int ret = BZ2_bzDecompress( &bzState ); + printf("(post) in: %db, out: %db\n", bzState.avail_in, bzState.avail_out ); nReadTotal += nRead-bzState.avail_out; if( ret == BZ_STREAM_END ) { + printf("\n"); + if( bzState.avail_in > 0 ) + { + if( rNext.canSeek() ) + { + rNext.seek( -bzState.avail_in ); + } + } return nBytes-bzState.avail_out; } + bzError( ret ); if( bzState.avail_out ) { - nRead = rNext.read( pBuf, nBufSize ); - bzState.next_in = pBuf; - bzState.avail_in = nRead; + printf("Still more to fill, in: %db, out: %db\n", bzState.avail_in, bzState.avail_out ); + + if( bzState.avail_in == 0 ) + { + nRead = rNext.read( pBuf, nBufSize ); + bzState.next_in = pBuf; + bzState.avail_in = nRead; + } } else { @@ -158,7 +181,7 @@ size_t Bu::BZip2::write( const void *pData, size_t nBytes ) bzState.avail_out = nBufSize; bzState.next_out = pBuf; - BZ2_bzCompress( &bzState, BZ_RUN ); + bzError( BZ2_bzCompress( &bzState, BZ_RUN ) ); if( bzState.avail_out < nBufSize ) { diff --git a/src/tests/bzip2.cpp b/src/tests/bzip2.cpp index ef9328f..683d3d7 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", "rb" ); + Bu::File f( "test.bz2", "wb" ); Bu::BZip2 bz2( f ); - Bu::File fin( argv[1], "wb"); + Bu::File fin( argv[1], "rb"); for(;;) { - nRead = bz2.read( buf, 1024 ); + nRead = fin.read( buf, 1024 ); if( nRead > 0 ) - fin.write( buf, nRead ); - if( bz2.isEOS() ) + bz2.write( buf, nRead ); + if( fin.isEOS() ) break; } } -- cgit v1.2.3