aboutsummaryrefslogtreecommitdiff
path: root/src/bzip2.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-10-24 16:30:39 +0000
committerMike Buland <eichlan@xagasoft.com>2011-10-24 16:30:39 +0000
commit9cb2695ad318dcda83a353b03c21b4fd71d0f9d6 (patch)
tree0dff744dc9d7348ee922e4f2630ca8acfc438b6f /src/bzip2.cpp
parente2fbc414b932ae9fd305c8e9fc315a306a876a09 (diff)
downloadlibbu++-9cb2695ad318dcda83a353b03c21b4fd71d0f9d6.tar.gz
libbu++-9cb2695ad318dcda83a353b03c21b4fd71d0f9d6.tar.bz2
libbu++-9cb2695ad318dcda83a353b03c21b4fd71d0f9d6.tar.xz
libbu++-9cb2695ad318dcda83a353b03c21b4fd71d0f9d6.zip
Made the encoder state opaque to the caller in Deflate and BZip2 to match Lzma.
That means that when you use Bu::Deflate, Bu::Bzip2, or Bu::Lzma you don't get any of the respective libraries' header files.
Diffstat (limited to '')
-rw-r--r--src/bzip2.cpp95
1 files changed, 53 insertions, 42 deletions
diff --git a/src/bzip2.cpp b/src/bzip2.cpp
index 0ff5444..ca007b0 100644
--- a/src/bzip2.cpp
+++ b/src/bzip2.cpp
@@ -8,10 +8,15 @@
8#include "bu/bzip2.h" 8#include "bu/bzip2.h"
9#include "bu/trace.h" 9#include "bu/trace.h"
10 10
11#include <bzlib.h>
12
13#define pState ((bz_stream *)prState)
14
11using namespace Bu; 15using namespace Bu;
12 16
13Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) : 17Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) :
14 Bu::Filter( rNext ), 18 Bu::Filter( rNext ),
19 prState( NULL ),
15 nCompression( nCompression ), 20 nCompression( nCompression ),
16 sTotalOut( 0 ) 21 sTotalOut( 0 )
17{ 22{
@@ -28,10 +33,12 @@ Bu::BZip2::~BZip2()
28void Bu::BZip2::start() 33void Bu::BZip2::start()
29{ 34{
30 TRACE(); 35 TRACE();
31 bzState.state = NULL; 36
32 bzState.bzalloc = NULL; 37 prState = new bz_stream;
33 bzState.bzfree = NULL; 38 pState->state = NULL;
34 bzState.opaque = NULL; 39 pState->bzalloc = NULL;
40 pState->bzfree = NULL;
41 pState->opaque = NULL;
35 42
36 nBufSize = 64*1024; 43 nBufSize = 64*1024;
37 pBuf = new char[nBufSize]; 44 pBuf = new char[nBufSize];
@@ -40,13 +47,15 @@ void Bu::BZip2::start()
40Bu::size Bu::BZip2::stop() 47Bu::size Bu::BZip2::stop()
41{ 48{
42 TRACE(); 49 TRACE();
43 if( bzState.state ) 50 if( pState->state )
44 { 51 {
45 if( bReading ) 52 if( bReading )
46 { 53 {
47 BZ2_bzDecompressEnd( &bzState ); 54 BZ2_bzDecompressEnd( pState );
48 delete[] pBuf; 55 delete[] pBuf;
49 pBuf = NULL; 56 pBuf = NULL;
57 delete pState;
58 prState = NULL;
50 return 0; 59 return 0;
51 } 60 }
52 else 61 else
@@ -54,21 +63,23 @@ Bu::size Bu::BZip2::stop()
54// Bu::size sTotal = 0; 63// Bu::size sTotal = 0;
55 for(;;) 64 for(;;)
56 { 65 {
57 bzState.next_in = NULL; 66 pState->next_in = NULL;
58 bzState.avail_in = 0; 67 pState->avail_in = 0;
59 bzState.avail_out = nBufSize; 68 pState->avail_out = nBufSize;
60 bzState.next_out = pBuf; 69 pState->next_out = pBuf;
61 int res = BZ2_bzCompress( &bzState, BZ_FINISH ); 70 int res = BZ2_bzCompress( pState, BZ_FINISH );
62 if( bzState.avail_out < nBufSize ) 71 if( pState->avail_out < nBufSize )
63 { 72 {
64 sTotalOut += rNext.write( pBuf, nBufSize-bzState.avail_out ); 73 sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out );
65 } 74 }
66 if( res == BZ_STREAM_END ) 75 if( res == BZ_STREAM_END )
67 break; 76 break;
68 } 77 }
69 BZ2_bzCompressEnd( &bzState ); 78 BZ2_bzCompressEnd( pState );
70 delete[] pBuf; 79 delete[] pBuf;
71 pBuf = NULL; 80 pBuf = NULL;
81 delete pState;
82 prState = NULL;
72 return sTotalOut; 83 return sTotalOut;
73 } 84 }
74 } 85 }
@@ -122,42 +133,42 @@ void Bu::BZip2::bzError( int code )
122Bu::size Bu::BZip2::read( void *pData, Bu::size nBytes ) 133Bu::size Bu::BZip2::read( void *pData, Bu::size nBytes )
123{ 134{
124 TRACE( pData, nBytes ); 135 TRACE( pData, nBytes );
125 if( !bzState.state ) 136 if( !pState->state )
126 { 137 {
127 bReading = true; 138 bReading = true;
128 BZ2_bzDecompressInit( &bzState, 0, 0 ); 139 BZ2_bzDecompressInit( pState, 0, 0 );
129 bzState.next_in = pBuf; 140 pState->next_in = pBuf;
130 bzState.avail_in = 0; 141 pState->avail_in = 0;
131 } 142 }
132 if( bReading == false ) 143 if( bReading == false )
133 throw ExceptionBase("This bzip2 filter is in writing mode, you can't read."); 144 throw ExceptionBase("This bzip2 filter is in writing mode, you can't read.");
134 145
135 int nRead = 0; 146 int nRead = 0;
136 int nReadTotal = bzState.total_out_lo32; 147 int nReadTotal = pState->total_out_lo32;
137 bzState.next_out = (char *)pData; 148 pState->next_out = (char *)pData;
138 bzState.avail_out = nBytes; 149 pState->avail_out = nBytes;
139 for(;;) 150 for(;;)
140 { 151 {
141 int ret = BZ2_bzDecompress( &bzState ); 152 int ret = BZ2_bzDecompress( pState );
142 153
143 nReadTotal += nRead-bzState.avail_out; 154 nReadTotal += nRead-pState->avail_out;
144 155
145 if( ret == BZ_STREAM_END ) 156 if( ret == BZ_STREAM_END )
146 { 157 {
147 if( bzState.avail_in > 0 ) 158 if( pState->avail_in > 0 )
148 { 159 {
149 if( rNext.isSeekable() ) 160 if( rNext.isSeekable() )
150 { 161 {
151 rNext.seek( -bzState.avail_in ); 162 rNext.seek( -pState->avail_in );
152 } 163 }
153 } 164 }
154 return nBytes-bzState.avail_out; 165 return nBytes-pState->avail_out;
155 } 166 }
156 bzError( ret ); 167 bzError( ret );
157 168
158 if( bzState.avail_out ) 169 if( pState->avail_out )
159 { 170 {
160 if( bzState.avail_in == 0 ) 171 if( pState->avail_in == 0 )
161 { 172 {
162 nRead = rNext.read( pBuf, nBufSize ); 173 nRead = rNext.read( pBuf, nBufSize );
163 if( nRead == 0 && rNext.isEos() ) 174 if( nRead == 0 && rNext.isEos() )
@@ -165,13 +176,13 @@ Bu::size Bu::BZip2::read( void *pData, Bu::size nBytes )
165 throw Bu::ExceptionBase("Premature end of underlying " 176 throw Bu::ExceptionBase("Premature end of underlying "
166 "stream found reading bzip2 stream."); 177 "stream found reading bzip2 stream.");
167 } 178 }
168 bzState.next_in = pBuf; 179 pState->next_in = pBuf;
169 bzState.avail_in = nRead; 180 pState->avail_in = nRead;
170 } 181 }
171 } 182 }
172 else 183 else
173 { 184 {
174 return nBytes-bzState.avail_out; 185 return nBytes-pState->avail_out;
175 } 186 }
176 } 187 }
177 return 0; 188 return 0;
@@ -180,29 +191,29 @@ Bu::size Bu::BZip2::read( void *pData, Bu::size nBytes )
180Bu::size Bu::BZip2::write( const void *pData, Bu::size nBytes ) 191Bu::size Bu::BZip2::write( const void *pData, Bu::size nBytes )
181{ 192{
182 TRACE( pData, nBytes ); 193 TRACE( pData, nBytes );
183 if( !bzState.state ) 194 if( !pState->state )
184 { 195 {
185 bReading = false; 196 bReading = false;
186 BZ2_bzCompressInit( &bzState, nCompression, 0, 30 ); 197 BZ2_bzCompressInit( pState, nCompression, 0, 30 );
187 } 198 }
188 if( bReading == true ) 199 if( bReading == true )
189 throw ExceptionBase("This bzip2 filter is in reading mode, you can't write."); 200 throw ExceptionBase("This bzip2 filter is in reading mode, you can't write.");
190 201
191// Bu::size sTotalOut = 0; 202// Bu::size sTotalOut = 0;
192 bzState.next_in = (char *)pData; 203 pState->next_in = (char *)pData;
193 bzState.avail_in = nBytes; 204 pState->avail_in = nBytes;
194 for(;;) 205 for(;;)
195 { 206 {
196 bzState.avail_out = nBufSize; 207 pState->avail_out = nBufSize;
197 bzState.next_out = pBuf; 208 pState->next_out = pBuf;
198 209
199 bzError( BZ2_bzCompress( &bzState, BZ_RUN ) ); 210 bzError( BZ2_bzCompress( pState, BZ_RUN ) );
200 211
201 if( bzState.avail_out < nBufSize ) 212 if( pState->avail_out < nBufSize )
202 { 213 {
203 sTotalOut += rNext.write( pBuf, nBufSize-bzState.avail_out ); 214 sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out );
204 } 215 }
205 if( bzState.avail_in == 0 ) 216 if( pState->avail_in == 0 )
206 break; 217 break;
207 } 218 }
208 219
@@ -212,7 +223,7 @@ Bu::size Bu::BZip2::write( const void *pData, Bu::size nBytes )
212bool Bu::BZip2::isOpen() 223bool Bu::BZip2::isOpen()
213{ 224{
214 TRACE(); 225 TRACE();
215 return (bzState.state != NULL); 226 return (pState->state != NULL);
216} 227}
217 228
218Bu::size Bu::BZip2::getCompressedSize() 229Bu::size Bu::BZip2::getCompressedSize()