Lines 110-116
Gzip_Compression::Gzip_Compression(u32bi
Link Here
|
110 |
if(deflateInit2(&(zlib->stream), level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) |
110 |
if(deflateInit2(&(zlib->stream), level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) |
111 |
{ |
111 |
{ |
112 |
delete zlib; zlib = 0; |
112 |
delete zlib; zlib = 0; |
113 |
throw Exception("Gzip_Compression: Memory allocation error"); |
113 |
throw Memory_Exhaustion(); |
114 |
} |
114 |
} |
115 |
} |
115 |
} |
116 |
|
116 |
|
Lines 137-143
void Gzip_Compression::start_msg()
Link Here
|
137 |
/************************************************* |
137 |
/************************************************* |
138 |
* Compress Input with Gzip * |
138 |
* Compress Input with Gzip * |
139 |
*************************************************/ |
139 |
*************************************************/ |
140 |
void Gzip_Compression::write(const byte input[], u32bit length) |
140 |
void Gzip_Compression::write(const byte input[], filter_length_t length) |
141 |
{ |
141 |
{ |
142 |
|
142 |
|
143 |
count += length; |
143 |
count += length; |
Lines 152-158
void Gzip_Compression::write(const byte
Link Here
|
152 |
zlib->stream.avail_out = buffer.size(); |
152 |
zlib->stream.avail_out = buffer.size(); |
153 |
int rc = deflate(&(zlib->stream), Z_NO_FLUSH); |
153 |
int rc = deflate(&(zlib->stream), Z_NO_FLUSH); |
154 |
if (rc != Z_OK && rc != Z_STREAM_END) |
154 |
if (rc != Z_OK && rc != Z_STREAM_END) |
155 |
throw Exception("Internal error in Gzip_Compression deflate."); |
155 |
throw Invalid_State("Internal error in Gzip_Compression deflate."); |
156 |
send(buffer.begin(), buffer.size() - zlib->stream.avail_out); |
156 |
send(buffer.begin(), buffer.size() - zlib->stream.avail_out); |
157 |
} |
157 |
} |
158 |
} |
158 |
} |
Lines 172-178
void Gzip_Compression::end_msg()
Link Here
|
172 |
zlib->stream.avail_out = buffer.size(); |
172 |
zlib->stream.avail_out = buffer.size(); |
173 |
rc = deflate(&(zlib->stream), Z_FINISH); |
173 |
rc = deflate(&(zlib->stream), Z_FINISH); |
174 |
if (rc != Z_OK && rc != Z_STREAM_END) |
174 |
if (rc != Z_OK && rc != Z_STREAM_END) |
175 |
throw Exception("Internal error in Gzip_Compression finishing deflate."); |
175 |
throw Invalid_State("Internal error in Gzip_Compression finishing deflate."); |
176 |
send(buffer.begin(), buffer.size() - zlib->stream.avail_out); |
176 |
send(buffer.begin(), buffer.size() - zlib->stream.avail_out); |
177 |
} |
177 |
} |
178 |
|
178 |
|
Lines 228-234
Gzip_Decompression::Gzip_Decompression()
Link Here
|
228 |
no_writes(true), pipe(new Hash_Filter("CRC32")), footer(0) |
228 |
no_writes(true), pipe(new Hash_Filter("CRC32")), footer(0) |
229 |
{ |
229 |
{ |
230 |
if (DEFAULT_BUFFERSIZE < sizeof(GZIP::GZIP_HEADER)) |
230 |
if (DEFAULT_BUFFERSIZE < sizeof(GZIP::GZIP_HEADER)) |
231 |
throw Exception("DEFAULT_BUFFERSIZE is too small"); |
231 |
throw Decoding_Error("DEFAULT_BUFFERSIZE is too small"); |
232 |
|
232 |
|
233 |
zlib = new Zlib_Stream; |
233 |
zlib = new Zlib_Stream; |
234 |
|
234 |
|
Lines 237-243
Gzip_Decompression::Gzip_Decompression()
Link Here
|
237 |
if(inflateInit2(&(zlib->stream), -15) != Z_OK) |
237 |
if(inflateInit2(&(zlib->stream), -15) != Z_OK) |
238 |
{ |
238 |
{ |
239 |
delete zlib; zlib = 0; |
239 |
delete zlib; zlib = 0; |
240 |
throw Exception("Gzip_Decompression: Memory allocation error"); |
240 |
throw Memory_Exhaustion(); |
241 |
} |
241 |
} |
242 |
} |
242 |
} |
243 |
|
243 |
|
Lines 256-262
void Gzip_Decompression::start_msg()
Link Here
|
256 |
void Gzip_Decompression::start_msg() |
256 |
void Gzip_Decompression::start_msg() |
257 |
{ |
257 |
{ |
258 |
if (!no_writes) |
258 |
if (!no_writes) |
259 |
throw Exception("Gzip_Decompression: start_msg after already writing"); |
259 |
throw Decoding_Error("Gzip_Decompression: start_msg after already writing"); |
260 |
|
260 |
|
261 |
pipe.start_msg(); |
261 |
pipe.start_msg(); |
262 |
datacount = 0; |
262 |
datacount = 0; |
Lines 267-273
void Gzip_Decompression::start_msg()
Link Here
|
267 |
/************************************************* |
267 |
/************************************************* |
268 |
* Decompress Input with Gzip * |
268 |
* Decompress Input with Gzip * |
269 |
*************************************************/ |
269 |
*************************************************/ |
270 |
void Gzip_Decompression::write(const byte input[], u32bit length) |
270 |
void Gzip_Decompression::write(const byte input[], filter_length_t length) |
271 |
{ |
271 |
{ |
272 |
if(length) no_writes = false; |
272 |
if(length) no_writes = false; |
273 |
|
273 |
|
Lines 277-291
void Gzip_Decompression::write(const byt
Link Here
|
277 |
u32bit eat_len = eat_footer(input, length); |
277 |
u32bit eat_len = eat_footer(input, length); |
278 |
input += eat_len; |
278 |
input += eat_len; |
279 |
length -= eat_len; |
279 |
length -= eat_len; |
280 |
if (length == 0) |
|
|
281 |
return; |
282 |
} |
280 |
} |
283 |
|
281 |
|
|
|
282 |
if (length == 0) |
283 |
return; |
284 |
|
284 |
// Check the gzip header |
285 |
// Check the gzip header |
285 |
if (pos < sizeof(GZIP::GZIP_HEADER)) |
286 |
if (pos < sizeof(GZIP::GZIP_HEADER)) |
286 |
{ |
287 |
{ |
287 |
u32bit len = std::min((u32bit)sizeof(GZIP::GZIP_HEADER)-pos, length); |
288 |
filter_length_t len = std::min((filter_length_t)sizeof(GZIP::GZIP_HEADER)-pos, length); |
288 |
u32bit cmplen = len; |
289 |
filter_length_t cmplen = len; |
289 |
// The last byte is the OS flag - we don't care about that |
290 |
// The last byte is the OS flag - we don't care about that |
290 |
if (pos + len - 1 >= GZIP::HEADER_POS_OS) |
291 |
if (pos + len - 1 >= GZIP::HEADER_POS_OS) |
291 |
cmplen--; |
292 |
cmplen--; |
Lines 317-324
void Gzip_Decompression::write(const byt
Link Here
|
317 |
if(rc == Z_NEED_DICT) |
318 |
if(rc == Z_NEED_DICT) |
318 |
throw Decoding_Error("Gzip_Decompression: Need preset dictionary"); |
319 |
throw Decoding_Error("Gzip_Decompression: Need preset dictionary"); |
319 |
if(rc == Z_MEM_ERROR) |
320 |
if(rc == Z_MEM_ERROR) |
320 |
throw Exception("Gzip_Decompression: Memory allocation error"); |
321 |
throw Memory_Exhaustion(); |
321 |
throw Exception("Gzip_Decompression: Unknown decompress error"); |
322 |
throw Decoding_Error("Gzip_Decompression: Unknown decompress error"); |
322 |
} |
323 |
} |
323 |
send(buffer.begin(), buffer.size() - zlib->stream.avail_out); |
324 |
send(buffer.begin(), buffer.size() - zlib->stream.avail_out); |
324 |
pipe.write(buffer.begin(), buffer.size() - zlib->stream.avail_out); |
325 |
pipe.write(buffer.begin(), buffer.size() - zlib->stream.avail_out); |
Lines 346-353
u32bit Gzip_Decompression::eat_footer(co
Link Here
|
346 |
if (footer.size() >= GZIP::FOOTER_LENGTH) |
347 |
if (footer.size() >= GZIP::FOOTER_LENGTH) |
347 |
throw Decoding_Error("Gzip_Decompression: Data integrity error in footer"); |
348 |
throw Decoding_Error("Gzip_Decompression: Data integrity error in footer"); |
348 |
|
349 |
|
|
|
350 |
#if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,9,11) |
351 |
size_t eat_len = std::min(GZIP::FOOTER_LENGTH-footer.size(), |
352 |
static_cast<size_t>(length)); |
353 |
footer += std::make_pair(input, eat_len); |
354 |
#else |
349 |
u32bit eat_len = std::min(GZIP::FOOTER_LENGTH-footer.size(), length); |
355 |
u32bit eat_len = std::min(GZIP::FOOTER_LENGTH-footer.size(), length); |
350 |
footer.append(input, eat_len); |
356 |
footer.append(input, eat_len); |
|
|
357 |
#endif |
351 |
|
358 |
|
352 |
if (footer.size() == GZIP::FOOTER_LENGTH) |
359 |
if (footer.size() == GZIP::FOOTER_LENGTH) |
353 |
{ |
360 |
{ |
Lines 364-370
void Gzip_Decompression::check_footer()
Link Here
|
364 |
void Gzip_Decompression::check_footer() |
371 |
void Gzip_Decompression::check_footer() |
365 |
{ |
372 |
{ |
366 |
if (footer.size() != GZIP::FOOTER_LENGTH) |
373 |
if (footer.size() != GZIP::FOOTER_LENGTH) |
367 |
throw Exception("Gzip_Decompression: Error finalizing decompression"); |
374 |
throw Decoding_Error("Gzip_Decompression: Error finalizing decompression"); |
368 |
|
375 |
|
369 |
pipe.end_msg(); |
376 |
pipe.end_msg(); |
370 |
|
377 |
|
Lines 377-383
void Gzip_Decompression::check_footer()
Link Here
|
377 |
for (int i = 0; i < 4; i++) |
384 |
for (int i = 0; i < 4; i++) |
378 |
buf[3-i] = tmpbuf[i]; |
385 |
buf[3-i] = tmpbuf[i]; |
379 |
|
386 |
|
|
|
387 |
#if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,9,11) |
388 |
tmpbuf.resize(4); |
389 |
tmpbuf.copy(footer.begin(), 4); |
390 |
#else |
380 |
tmpbuf.set(footer.begin(), 4); |
391 |
tmpbuf.set(footer.begin(), 4); |
|
|
392 |
#endif |
381 |
if (buf != tmpbuf) |
393 |
if (buf != tmpbuf) |
382 |
throw Decoding_Error("Gzip_Decompression: Data integrity error - CRC32 error"); |
394 |
throw Decoding_Error("Gzip_Decompression: Data integrity error - CRC32 error"); |
383 |
|
395 |
|
Lines 400-406
void Gzip_Decompression::end_msg()
Link Here
|
400 |
// read, clear() will reset no_writes |
412 |
// read, clear() will reset no_writes |
401 |
if(no_writes) return; |
413 |
if(no_writes) return; |
402 |
|
414 |
|
403 |
throw Exception("Gzip_Decompression: didn't find footer"); |
415 |
throw Decoding_Error("Gzip_Decompression: didn't find footer"); |
404 |
|
416 |
|
405 |
} |
417 |
} |
406 |
|
418 |
|
Lines 412-418
void Gzip_Decompression::clear()
Link Here
|
412 |
no_writes = true; |
424 |
no_writes = true; |
413 |
inflateReset(&(zlib->stream)); |
425 |
inflateReset(&(zlib->stream)); |
414 |
|
426 |
|
|
|
427 |
#if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(1,9,11) |
428 |
footer.clear(); |
429 |
#else |
415 |
footer.destroy(); |
430 |
footer.destroy(); |
|
|
431 |
#endif |
416 |
pos = 0; |
432 |
pos = 0; |
417 |
datacount = 0; |
433 |
datacount = 0; |
418 |
} |
434 |
} |