diff options
Diffstat (limited to 'src/stable/bzip2.cpp')
-rw-r--r-- | src/stable/bzip2.cpp | 344 |
1 files changed, 172 insertions, 172 deletions
diff --git a/src/stable/bzip2.cpp b/src/stable/bzip2.cpp index 11b8873..5e660d0 100644 --- a/src/stable/bzip2.cpp +++ b/src/stable/bzip2.cpp | |||
@@ -15,222 +15,222 @@ | |||
15 | using namespace Bu; | 15 | using namespace Bu; |
16 | 16 | ||
17 | Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) : | 17 | Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) : |
18 | Bu::Filter( rNext ), | 18 | Bu::Filter( rNext ), |
19 | prState( NULL ), | 19 | prState( NULL ), |
20 | nCompression( nCompression ), | 20 | nCompression( nCompression ), |
21 | sTotalOut( 0 ) | 21 | sTotalOut( 0 ) |
22 | { | 22 | { |
23 | TRACE( nCompression ); | 23 | TRACE( nCompression ); |
24 | start(); | 24 | start(); |
25 | } | 25 | } |
26 | 26 | ||
27 | Bu::BZip2::~BZip2() | 27 | Bu::BZip2::~BZip2() |
28 | { | 28 | { |
29 | TRACE(); | 29 | TRACE(); |
30 | stop(); | 30 | stop(); |
31 | } | 31 | } |
32 | 32 | ||
33 | void Bu::BZip2::start() | 33 | void Bu::BZip2::start() |
34 | { | 34 | { |
35 | TRACE(); | 35 | TRACE(); |
36 | 36 | ||
37 | prState = new bz_stream; | 37 | prState = new bz_stream; |
38 | pState->state = NULL; | 38 | pState->state = NULL; |
39 | pState->bzalloc = NULL; | 39 | pState->bzalloc = NULL; |
40 | pState->bzfree = NULL; | 40 | pState->bzfree = NULL; |
41 | pState->opaque = NULL; | 41 | pState->opaque = NULL; |
42 | 42 | ||
43 | nBufSize = 64*1024; | 43 | nBufSize = 64*1024; |
44 | pBuf = new char[nBufSize]; | 44 | pBuf = new char[nBufSize]; |
45 | } | 45 | } |
46 | 46 | ||
47 | Bu::size Bu::BZip2::stop() | 47 | Bu::size Bu::BZip2::stop() |
48 | { | 48 | { |
49 | TRACE(); | 49 | TRACE(); |
50 | if( !pState ) | 50 | if( !pState ) |
51 | return 0; | 51 | return 0; |
52 | 52 | ||
53 | if( pState->state ) | 53 | if( pState->state ) |
54 | { | 54 | { |
55 | if( bReading ) | 55 | if( bReading ) |
56 | { | 56 | { |
57 | BZ2_bzDecompressEnd( pState ); | 57 | BZ2_bzDecompressEnd( pState ); |
58 | delete[] pBuf; | 58 | delete[] pBuf; |
59 | pBuf = NULL; | 59 | pBuf = NULL; |
60 | delete pState; | 60 | delete pState; |
61 | prState = NULL; | 61 | prState = NULL; |
62 | return 0; | 62 | return 0; |
63 | } | 63 | } |
64 | else | 64 | else |
65 | { | 65 | { |
66 | // Bu::size sTotal = 0; | 66 | // Bu::size sTotal = 0; |
67 | for(;;) | 67 | for(;;) |
68 | { | 68 | { |
69 | pState->next_in = NULL; | 69 | pState->next_in = NULL; |
70 | pState->avail_in = 0; | 70 | pState->avail_in = 0; |
71 | pState->avail_out = nBufSize; | 71 | pState->avail_out = nBufSize; |
72 | pState->next_out = pBuf; | 72 | pState->next_out = pBuf; |
73 | int res = BZ2_bzCompress( pState, BZ_FINISH ); | 73 | int res = BZ2_bzCompress( pState, BZ_FINISH ); |
74 | if( pState->avail_out < nBufSize ) | 74 | if( pState->avail_out < nBufSize ) |
75 | { | 75 | { |
76 | sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); | 76 | sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); |
77 | } | 77 | } |
78 | if( res == BZ_STREAM_END ) | 78 | if( res == BZ_STREAM_END ) |
79 | break; | 79 | break; |
80 | } | 80 | } |
81 | BZ2_bzCompressEnd( pState ); | 81 | BZ2_bzCompressEnd( pState ); |
82 | delete[] pBuf; | 82 | delete[] pBuf; |
83 | pBuf = NULL; | 83 | pBuf = NULL; |
84 | delete pState; | 84 | delete pState; |
85 | prState = NULL; | 85 | prState = NULL; |
86 | return sTotalOut; | 86 | return sTotalOut; |
87 | } | 87 | } |
88 | } | 88 | } |
89 | return 0; | 89 | return 0; |
90 | } | 90 | } |
91 | 91 | ||
92 | void Bu::BZip2::bzError( int code ) | 92 | void Bu::BZip2::bzError( int code ) |
93 | { | 93 | { |
94 | TRACE( code ); | 94 | TRACE( code ); |
95 | switch( code ) | 95 | switch( code ) |
96 | { | 96 | { |
97 | case BZ_OK: | 97 | case BZ_OK: |
98 | case BZ_RUN_OK: | 98 | case BZ_RUN_OK: |
99 | case BZ_FLUSH_OK: | 99 | case BZ_FLUSH_OK: |
100 | case BZ_FINISH_OK: | 100 | case BZ_FINISH_OK: |
101 | return; | 101 | return; |
102 | 102 | ||
103 | case BZ_CONFIG_ERROR: | 103 | case BZ_CONFIG_ERROR: |
104 | throw ExceptionBase("BZip2: Library configured improperly, reinstall."); | 104 | throw ExceptionBase("BZip2: Library configured improperly, reinstall."); |
105 | 105 | ||
106 | case BZ_SEQUENCE_ERROR: | 106 | case BZ_SEQUENCE_ERROR: |
107 | throw ExceptionBase("BZip2: Functions were called in an invalid sequence."); | 107 | throw ExceptionBase("BZip2: Functions were called in an invalid sequence."); |
108 | 108 | ||
109 | case BZ_PARAM_ERROR: | 109 | case BZ_PARAM_ERROR: |
110 | throw ExceptionBase("BZip2: Invalid parameter was passed into a function."); | 110 | throw ExceptionBase("BZip2: Invalid parameter was passed into a function."); |
111 | 111 | ||
112 | case BZ_MEM_ERROR: | 112 | case BZ_MEM_ERROR: |
113 | throw ExceptionBase("BZip2: Couldn't allocate sufficient memory."); | 113 | throw ExceptionBase("BZip2: Couldn't allocate sufficient memory."); |
114 | 114 | ||
115 | case BZ_DATA_ERROR: | 115 | case BZ_DATA_ERROR: |
116 | throw ExceptionBase("BZip2: Data was corrupted before decompression."); | 116 | throw ExceptionBase("BZip2: Data was corrupted before decompression."); |
117 | 117 | ||
118 | case BZ_DATA_ERROR_MAGIC: | 118 | case BZ_DATA_ERROR_MAGIC: |
119 | throw ExceptionBase("BZip2: Stream does not appear to be bzip2 data."); | 119 | throw ExceptionBase("BZip2: Stream does not appear to be bzip2 data."); |
120 | 120 | ||
121 | case BZ_IO_ERROR: | 121 | case BZ_IO_ERROR: |
122 | throw ExceptionBase("BZip2: File couldn't be read from / written to."); | 122 | throw ExceptionBase("BZip2: File couldn't be read from / written to."); |
123 | 123 | ||
124 | case BZ_UNEXPECTED_EOF: | 124 | case BZ_UNEXPECTED_EOF: |
125 | throw ExceptionBase("BZip2: End of file encountered before end of stream."); | 125 | throw ExceptionBase("BZip2: End of file encountered before end of stream."); |
126 | 126 | ||
127 | case BZ_OUTBUFF_FULL: | 127 | case BZ_OUTBUFF_FULL: |
128 | throw ExceptionBase("BZip2: Buffer not large enough to accomidate data."); | 128 | throw ExceptionBase("BZip2: Buffer not large enough to accomidate data."); |
129 | 129 | ||
130 | default: | 130 | default: |
131 | throw ExceptionBase("BZip2: Unknown error encountered."); | 131 | throw ExceptionBase("BZip2: Unknown error encountered."); |
132 | 132 | ||
133 | } | 133 | } |
134 | } | 134 | } |
135 | 135 | ||
136 | Bu::size Bu::BZip2::read( void *pData, Bu::size nBytes ) | 136 | Bu::size Bu::BZip2::read( void *pData, Bu::size nBytes ) |
137 | { | 137 | { |
138 | TRACE( pData, nBytes ); | 138 | TRACE( pData, nBytes ); |
139 | if( !pState->state ) | 139 | if( !pState->state ) |
140 | { | 140 | { |
141 | bReading = true; | 141 | bReading = true; |
142 | BZ2_bzDecompressInit( pState, 0, 0 ); | 142 | BZ2_bzDecompressInit( pState, 0, 0 ); |
143 | pState->next_in = pBuf; | 143 | pState->next_in = pBuf; |
144 | pState->avail_in = 0; | 144 | pState->avail_in = 0; |
145 | } | 145 | } |
146 | if( bReading == false ) | 146 | if( bReading == false ) |
147 | throw ExceptionBase("This bzip2 filter is in writing mode, you can't read."); | 147 | throw ExceptionBase("This bzip2 filter is in writing mode, you can't read."); |
148 | 148 | ||
149 | int nRead = 0; | 149 | int nRead = 0; |
150 | int nReadTotal = pState->total_out_lo32; | 150 | int nReadTotal = pState->total_out_lo32; |
151 | pState->next_out = (char *)pData; | 151 | pState->next_out = (char *)pData; |
152 | pState->avail_out = nBytes; | 152 | pState->avail_out = nBytes; |
153 | for(;;) | 153 | for(;;) |
154 | { | 154 | { |
155 | int ret = BZ2_bzDecompress( pState ); | 155 | int ret = BZ2_bzDecompress( pState ); |
156 | 156 | ||
157 | nReadTotal += nRead-pState->avail_out; | 157 | nReadTotal += nRead-pState->avail_out; |
158 | 158 | ||
159 | if( ret == BZ_STREAM_END ) | 159 | if( ret == BZ_STREAM_END ) |
160 | { | 160 | { |
161 | if( pState->avail_in > 0 ) | 161 | if( pState->avail_in > 0 ) |
162 | { | 162 | { |
163 | if( rNext.isSeekable() ) | 163 | if( rNext.isSeekable() ) |
164 | { | 164 | { |
165 | rNext.seek( -(Bu::size)pState->avail_in ); | 165 | rNext.seek( -(Bu::size)pState->avail_in ); |
166 | } | 166 | } |
167 | } | 167 | } |
168 | return nBytes-pState->avail_out; | 168 | return nBytes-pState->avail_out; |
169 | } | 169 | } |
170 | bzError( ret ); | 170 | bzError( ret ); |
171 | 171 | ||
172 | if( pState->avail_out ) | 172 | if( pState->avail_out ) |
173 | { | 173 | { |
174 | if( pState->avail_in == 0 ) | 174 | if( pState->avail_in == 0 ) |
175 | { | 175 | { |
176 | nRead = rNext.read( pBuf, nBufSize ); | 176 | nRead = rNext.read( pBuf, nBufSize ); |
177 | if( nRead == 0 && rNext.isEos() ) | 177 | if( nRead == 0 && rNext.isEos() ) |
178 | { | 178 | { |
179 | throw Bu::ExceptionBase("Premature end of underlying " | 179 | throw Bu::ExceptionBase("Premature end of underlying " |
180 | "stream found reading bzip2 stream."); | 180 | "stream found reading bzip2 stream."); |
181 | } | 181 | } |
182 | pState->next_in = pBuf; | 182 | pState->next_in = pBuf; |
183 | pState->avail_in = nRead; | 183 | pState->avail_in = nRead; |
184 | } | 184 | } |
185 | } | 185 | } |
186 | else | 186 | else |
187 | { | 187 | { |
188 | return nBytes-pState->avail_out; | 188 | return nBytes-pState->avail_out; |
189 | } | 189 | } |
190 | } | 190 | } |
191 | return 0; | 191 | return 0; |
192 | } | 192 | } |
193 | 193 | ||
194 | Bu::size Bu::BZip2::write( const void *pData, Bu::size nBytes ) | 194 | Bu::size Bu::BZip2::write( const void *pData, Bu::size nBytes ) |
195 | { | 195 | { |
196 | TRACE( pData, nBytes ); | 196 | TRACE( pData, nBytes ); |
197 | if( !pState->state ) | 197 | if( !pState->state ) |
198 | { | 198 | { |
199 | bReading = false; | 199 | bReading = false; |
200 | BZ2_bzCompressInit( pState, nCompression, 0, 30 ); | 200 | BZ2_bzCompressInit( pState, nCompression, 0, 30 ); |
201 | } | 201 | } |
202 | if( bReading == true ) | 202 | if( bReading == true ) |
203 | throw ExceptionBase("This bzip2 filter is in reading mode, you can't write."); | 203 | throw ExceptionBase("This bzip2 filter is in reading mode, you can't write."); |
204 | 204 | ||
205 | // Bu::size sTotalOut = 0; | 205 | // Bu::size sTotalOut = 0; |
206 | pState->next_in = (char *)pData; | 206 | pState->next_in = (char *)pData; |
207 | pState->avail_in = nBytes; | 207 | pState->avail_in = nBytes; |
208 | for(;;) | 208 | for(;;) |
209 | { | 209 | { |
210 | pState->avail_out = nBufSize; | 210 | pState->avail_out = nBufSize; |
211 | pState->next_out = pBuf; | 211 | pState->next_out = pBuf; |
212 | 212 | ||
213 | bzError( BZ2_bzCompress( pState, BZ_RUN ) ); | 213 | bzError( BZ2_bzCompress( pState, BZ_RUN ) ); |
214 | 214 | ||
215 | if( pState->avail_out < nBufSize ) | 215 | if( pState->avail_out < nBufSize ) |
216 | { | 216 | { |
217 | sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); | 217 | sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); |
218 | } | 218 | } |
219 | if( pState->avail_in == 0 ) | 219 | if( pState->avail_in == 0 ) |
220 | break; | 220 | break; |
221 | } | 221 | } |
222 | 222 | ||
223 | return nBytes; | 223 | return nBytes; |
224 | } | 224 | } |
225 | 225 | ||
226 | bool Bu::BZip2::isOpen() | 226 | bool Bu::BZip2::isOpen() |
227 | { | 227 | { |
228 | TRACE(); | 228 | TRACE(); |
229 | return (pState->state != NULL); | 229 | return (pState->state != NULL); |
230 | } | 230 | } |
231 | 231 | ||
232 | Bu::size Bu::BZip2::getCompressedSize() | 232 | Bu::size Bu::BZip2::getCompressedSize() |
233 | { | 233 | { |
234 | return sTotalOut; | 234 | return sTotalOut; |
235 | } | 235 | } |
236 | 236 | ||