diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bzip2.cpp | 37 | ||||
-rw-r--r-- | src/tests/bzip2.cpp | 10 |
2 files changed, 35 insertions, 12 deletions
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 ) : | |||
12 | 12 | ||
13 | Bu::BZip2::~BZip2() | 13 | Bu::BZip2::~BZip2() |
14 | { | 14 | { |
15 | printf("-> Bu::BZip2::~BZip2()\n"); | ||
16 | stop(); | 15 | stop(); |
17 | } | 16 | } |
18 | 17 | ||
19 | void Bu::BZip2::start() | 18 | void Bu::BZip2::start() |
20 | { | 19 | { |
21 | printf("-> Bu::BZip2::start()\n"); | ||
22 | printf("Hey, it's starting...\n"); | ||
23 | bzState.state = NULL; | 20 | bzState.state = NULL; |
24 | bzState.bzalloc = NULL; | 21 | bzState.bzalloc = NULL; |
25 | bzState.bzfree = NULL; | 22 | bzState.bzfree = NULL; |
@@ -31,11 +28,11 @@ void Bu::BZip2::start() | |||
31 | 28 | ||
32 | void Bu::BZip2::stop() | 29 | void Bu::BZip2::stop() |
33 | { | 30 | { |
34 | printf("-> Bu::BZip2::stop()\n"); | ||
35 | if( bzState.state ) | 31 | if( bzState.state ) |
36 | { | 32 | { |
37 | if( bReading ) | 33 | if( bReading ) |
38 | { | 34 | { |
35 | BZ2_bzDecompressEnd( &bzState ); | ||
39 | } | 36 | } |
40 | else | 37 | else |
41 | { | 38 | { |
@@ -81,13 +78,39 @@ size_t Bu::BZip2::read( void *pData, size_t nBytes ) | |||
81 | if( !bzState.state ) | 78 | if( !bzState.state ) |
82 | { | 79 | { |
83 | bReading = true; | 80 | bReading = true; |
81 | BZ2_bzDecompressInit( &bzState, 0, 0 ); | ||
82 | bzState.next_in = pBuf; | ||
83 | bzState.avail_in = 0; | ||
84 | } | 84 | } |
85 | if( bReading == false ) | 85 | if( bReading == false ) |
86 | throw ExceptionBase("This bzip2 filter is in writing mode, you can't read."); | 86 | throw ExceptionBase("This bzip2 filter is in writing mode, you can't read."); |
87 | //bzState.next_in = pData; | 87 | |
88 | //bzState.avail_in = nSizeIn; | 88 | int nRead = 0; |
89 | int nReadTotal = bzState.total_out_lo32; | ||
90 | for(;;) | ||
91 | { | ||
92 | bzState.next_out = (char *)pData; | ||
93 | bzState.avail_out = nBytes; | ||
94 | int ret = BZ2_bzDecompress( &bzState ); | ||
89 | 95 | ||
90 | //printf("%db at [%08X] (%db)\n", bzState.avail_in, (uint32_t)bzState.next_in, bzState.total_in_lo32 ); | 96 | nReadTotal += nRead-bzState.avail_out; |
97 | |||
98 | if( ret == BZ_STREAM_END ) | ||
99 | { | ||
100 | return nBytes-bzState.avail_out; | ||
101 | } | ||
102 | |||
103 | if( bzState.avail_out ) | ||
104 | { | ||
105 | nRead = rNext.read( pBuf, nBufSize ); | ||
106 | bzState.next_in = pBuf; | ||
107 | bzState.avail_in = nRead; | ||
108 | } | ||
109 | else | ||
110 | { | ||
111 | return nBytes-bzState.avail_out; | ||
112 | } | ||
113 | } | ||
91 | return 0; | 114 | return 0; |
92 | } | 115 | } |
93 | 116 | ||
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[] ) | |||
6 | char buf[1024]; | 6 | char buf[1024]; |
7 | size_t nRead; | 7 | size_t nRead; |
8 | 8 | ||
9 | Bu::File f( "test.bz2", "wb" ); | 9 | Bu::File f( "test.bz2", "rb" ); |
10 | Bu::BZip2 bz2( f ); | 10 | Bu::BZip2 bz2( f ); |
11 | 11 | ||
12 | Bu::File fin( argv[1], "rb"); | 12 | Bu::File fin( argv[1], "wb"); |
13 | 13 | ||
14 | for(;;) | 14 | for(;;) |
15 | { | 15 | { |
16 | nRead = fin.read( buf, 1024 ); | 16 | nRead = bz2.read( buf, 1024 ); |
17 | if( nRead > 0 ) | 17 | if( nRead > 0 ) |
18 | bz2.write( buf, nRead ); | 18 | fin.write( buf, nRead ); |
19 | if( fin.isEOS() ) | 19 | if( bz2.isEOS() ) |
20 | break; | 20 | break; |
21 | } | 21 | } |
22 | } | 22 | } |