/src/libewf/libewf/libewf_compression.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Compression handling functions |
3 | | * |
4 | | * Copyright (C) 2006-2024, Joachim Metz <joachim.metz@gmail.com> |
5 | | * |
6 | | * Refer to AUTHORS for acknowledgements. |
7 | | * |
8 | | * This program is free software: you can redistribute it and/or modify |
9 | | * it under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation, either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <common.h> |
23 | | #include <types.h> |
24 | | |
25 | | #if defined( HAVE_STDLIB_H ) || defined( WINAPI ) |
26 | | #include <stdlib.h> |
27 | | #endif |
28 | | |
29 | | #if defined( HAVE_BZLIB ) || defined( BZ_DLL ) |
30 | | #include <bzlib.h> |
31 | | #endif |
32 | | |
33 | | #if defined( HAVE_ZLIB ) || defined( ZLIB_DLL ) |
34 | | #include <zlib.h> |
35 | | #endif |
36 | | |
37 | | #include "libewf_compression.h" |
38 | | #include "libewf_definitions.h" |
39 | | #include "libewf_deflate.h" |
40 | | #include "libewf_libcerror.h" |
41 | | #include "libewf_libcnotify.h" |
42 | | |
43 | | /* Compresses data using the compression method |
44 | | * Returns 1 on success, 0 if buffer is too small or -1 on error |
45 | | */ |
46 | | int libewf_compress_data( |
47 | | uint8_t *compressed_data, |
48 | | size_t *compressed_data_size, |
49 | | uint16_t compression_method, |
50 | | int8_t compression_level, |
51 | | const uint8_t *uncompressed_data, |
52 | | size_t uncompressed_data_size, |
53 | | libcerror_error_t **error ) |
54 | 0 | { |
55 | 0 | static char *function = "libewf_compress_data"; |
56 | 0 | int result = 0; |
57 | |
|
58 | | #if defined( HAVE_BZLIB ) || defined( BZ_DLL ) |
59 | | unsigned int bzip2_compressed_data_size = 0; |
60 | | int bzip2_compression_level = 0; |
61 | | #endif |
62 | | #if ( defined( HAVE_ZLIB ) && defined( HAVE_ZLIB_COMPRESS2 ) ) || defined( ZLIB_DLL ) |
63 | | uLongf zlib_compressed_data_size = 0; |
64 | | int zlib_compression_level = 0; |
65 | | #endif |
66 | |
|
67 | 0 | if( compressed_data == NULL ) |
68 | 0 | { |
69 | 0 | libcerror_error_set( |
70 | 0 | error, |
71 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
72 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
73 | 0 | "%s: invalid compressed data buffer.", |
74 | 0 | function ); |
75 | |
|
76 | 0 | return( -1 ); |
77 | 0 | } |
78 | 0 | if( compressed_data_size == NULL ) |
79 | 0 | { |
80 | 0 | libcerror_error_set( |
81 | 0 | error, |
82 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
83 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
84 | 0 | "%s: invalid compressed data size.", |
85 | 0 | function ); |
86 | |
|
87 | 0 | return( -1 ); |
88 | 0 | } |
89 | 0 | if( uncompressed_data == NULL ) |
90 | 0 | { |
91 | 0 | libcerror_error_set( |
92 | 0 | error, |
93 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
94 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
95 | 0 | "%s: invalid uncompressed data buffer.", |
96 | 0 | function ); |
97 | |
|
98 | 0 | return( -1 ); |
99 | 0 | } |
100 | 0 | if( compressed_data == uncompressed_data ) |
101 | 0 | { |
102 | 0 | libcerror_error_set( |
103 | 0 | error, |
104 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
105 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
106 | 0 | "%s: invalid uncompressed data buffer equals compressed data buffer.", |
107 | 0 | function ); |
108 | |
|
109 | 0 | return( -1 ); |
110 | 0 | } |
111 | 0 | if( compression_method == LIBEWF_COMPRESSION_METHOD_DEFLATE ) |
112 | 0 | { |
113 | | #if ( defined( HAVE_ZLIB ) && defined( HAVE_ZLIB_COMPRESS2 ) ) || defined( ZLIB_DLL ) |
114 | | if( compression_level == LIBEWF_COMPRESSION_LEVEL_DEFAULT ) |
115 | | { |
116 | | zlib_compression_level = Z_DEFAULT_COMPRESSION; |
117 | | } |
118 | | else if( compression_level == LIBEWF_COMPRESSION_LEVEL_FAST ) |
119 | | { |
120 | | zlib_compression_level = Z_BEST_SPEED; |
121 | | } |
122 | | else if( compression_level == LIBEWF_COMPRESSION_LEVEL_BEST ) |
123 | | { |
124 | | zlib_compression_level = Z_BEST_COMPRESSION; |
125 | | } |
126 | | else if( compression_level == LIBEWF_COMPRESSION_LEVEL_NONE ) |
127 | | { |
128 | | zlib_compression_level = Z_NO_COMPRESSION; |
129 | | } |
130 | | else |
131 | | { |
132 | | libcerror_error_set( |
133 | | error, |
134 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
135 | | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
136 | | "%s: unsupported compression level.", |
137 | | function ); |
138 | | |
139 | | return( -1 ); |
140 | | } |
141 | | #if ULONG_MAX < SSIZE_MAX |
142 | | if( *compressed_data_size > (size_t) ULONG_MAX ) |
143 | | #else |
144 | | if( *compressed_data_size > (size_t) SSIZE_MAX ) |
145 | | #endif |
146 | | { |
147 | | libcerror_error_set( |
148 | | error, |
149 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
150 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
151 | | "%s: invalid compressed data size value exceeds maximum.", |
152 | | function ); |
153 | | |
154 | | return( -1 ); |
155 | | } |
156 | | #if ULONG_MAX < SSIZE_MAX |
157 | | if( uncompressed_data_size > (size_t) ULONG_MAX ) |
158 | | #else |
159 | | if( uncompressed_data_size > (size_t) SSIZE_MAX ) |
160 | | #endif |
161 | | { |
162 | | libcerror_error_set( |
163 | | error, |
164 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
165 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
166 | | "%s: invalid uncompressed data size value exceeds maximum.", |
167 | | function ); |
168 | | |
169 | | return( -1 ); |
170 | | } |
171 | | zlib_compressed_data_size = (uLongf) *compressed_data_size; |
172 | | |
173 | | result = compress2( |
174 | | (Bytef *) compressed_data, |
175 | | &zlib_compressed_data_size, |
176 | | (Bytef *) uncompressed_data, |
177 | | (uLong) uncompressed_data_size, |
178 | | zlib_compression_level ); |
179 | | |
180 | | if( result == Z_OK ) |
181 | | { |
182 | | *compressed_data_size = (size_t) zlib_compressed_data_size; |
183 | | |
184 | | result = 1; |
185 | | } |
186 | | else if( result == Z_BUF_ERROR ) |
187 | | { |
188 | | #if defined( HAVE_DEBUG_OUTPUT ) |
189 | | if( libcnotify_verbose != 0 ) |
190 | | { |
191 | | libcnotify_printf( |
192 | | "%s: unable to write compressed data: target buffer too small.\n", |
193 | | function ); |
194 | | } |
195 | | #endif |
196 | | #if defined( HAVE_COMPRESS_BOUND ) || defined( WINAPI ) |
197 | | /* Use compressBound to determine the size of the uncompressed buffer |
198 | | */ |
199 | | zlib_compressed_data_size = compressBound( (uLong) uncompressed_data_size ); |
200 | | *compressed_data_size = (size_t) zlib_compressed_data_size; |
201 | | #else |
202 | | /* Estimate that a factor 2 enlargement should suffice |
203 | | */ |
204 | | *compressed_data_size *= 2; |
205 | | #endif |
206 | | result = 0; |
207 | | } |
208 | | else if( result == Z_MEM_ERROR ) |
209 | | { |
210 | | libcerror_error_set( |
211 | | error, |
212 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
213 | | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
214 | | "%s: unable to write compressed data: insufficient memory.", |
215 | | function ); |
216 | | |
217 | | *compressed_data_size = 0; |
218 | | |
219 | | result = -1; |
220 | | } |
221 | | else |
222 | | { |
223 | | libcerror_error_set( |
224 | | error, |
225 | | LIBCERROR_ERROR_DOMAIN_COMPRESSION, |
226 | | LIBCERROR_COMPRESSION_ERROR_COMPRESS_FAILED, |
227 | | "%s: zlib returned undefined error: %d.", |
228 | | function, |
229 | | result ); |
230 | | |
231 | | *compressed_data_size = 0; |
232 | | |
233 | | result = -1; |
234 | | } |
235 | | #else |
236 | 0 | libcerror_error_set( |
237 | 0 | error, |
238 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
239 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
240 | 0 | "%s: missing support for deflate compression.", |
241 | 0 | function ); |
242 | |
|
243 | 0 | return( -1 ); |
244 | 0 | #endif /* ( defined( HAVE_ZLIB ) && defined( HAVE_ZLIB_COMPRESS2 ) ) || defined( ZLIB_DLL ) */ |
245 | 0 | } |
246 | 0 | else if( compression_method == LIBEWF_COMPRESSION_METHOD_BZIP2 ) |
247 | 0 | { |
248 | | #if defined( HAVE_BZLIB ) || defined( BZ_DLL ) |
249 | | if( ( compression_level == LIBEWF_COMPRESSION_LEVEL_DEFAULT ) |
250 | | || ( compression_level == LIBEWF_COMPRESSION_LEVEL_FAST ) ) |
251 | | { |
252 | | bzip2_compression_level = 1; |
253 | | } |
254 | | else if( compression_level == LIBEWF_COMPRESSION_LEVEL_BEST ) |
255 | | { |
256 | | bzip2_compression_level = 9; |
257 | | } |
258 | | else |
259 | | { |
260 | | libcerror_error_set( |
261 | | error, |
262 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
263 | | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
264 | | "%s: unsupported compression level.", |
265 | | function ); |
266 | | |
267 | | return( -1 ); |
268 | | } |
269 | | if( *compressed_data_size > (size_t) UINT_MAX ) |
270 | | { |
271 | | libcerror_error_set( |
272 | | error, |
273 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
274 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
275 | | "%s: invalid compressed data size value exceeds maximum.", |
276 | | function ); |
277 | | |
278 | | return( -1 ); |
279 | | } |
280 | | if( uncompressed_data_size > (size_t) UINT_MAX ) |
281 | | { |
282 | | libcerror_error_set( |
283 | | error, |
284 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
285 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
286 | | "%s: invalid uncompressed data size value exceeds maximum.", |
287 | | function ); |
288 | | |
289 | | return( -1 ); |
290 | | } |
291 | | bzip2_compressed_data_size = (unsigned int) *compressed_data_size; |
292 | | |
293 | | result = BZ2_bzBuffToBuffCompress( |
294 | | (char *) compressed_data, |
295 | | &bzip2_compressed_data_size, |
296 | | (char *) uncompressed_data, |
297 | | (unsigned int) uncompressed_data_size, |
298 | | bzip2_compression_level, |
299 | | 0, |
300 | | 30 ); |
301 | | |
302 | | if( result == BZ_OK ) |
303 | | { |
304 | | *compressed_data_size = (size_t) bzip2_compressed_data_size; |
305 | | |
306 | | result = 1; |
307 | | } |
308 | | else if( result == BZ_OUTBUFF_FULL ) |
309 | | { |
310 | | #if defined( HAVE_DEBUG_OUTPUT ) |
311 | | if( libcnotify_verbose != 0 ) |
312 | | { |
313 | | libcnotify_printf( |
314 | | "%s: unable to write compressed data: target buffer too small.\n", |
315 | | function ); |
316 | | } |
317 | | #endif |
318 | | /* Estimate that a factor 2 enlargement should suffice |
319 | | */ |
320 | | *compressed_data_size *= 2; |
321 | | |
322 | | result = 0; |
323 | | } |
324 | | else if( result == BZ_MEM_ERROR ) |
325 | | { |
326 | | libcerror_error_set( |
327 | | error, |
328 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
329 | | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
330 | | "%s: unable to write compressed data: insufficient memory.", |
331 | | function ); |
332 | | |
333 | | *compressed_data_size = 0; |
334 | | |
335 | | result = -1; |
336 | | } |
337 | | else |
338 | | { |
339 | | libcerror_error_set( |
340 | | error, |
341 | | LIBCERROR_ERROR_DOMAIN_COMPRESSION, |
342 | | LIBCERROR_COMPRESSION_ERROR_COMPRESS_FAILED, |
343 | | "%s: libbz2 returned undefined error: %d.", |
344 | | function, |
345 | | result ); |
346 | | |
347 | | *compressed_data_size = 0; |
348 | | |
349 | | result = -1; |
350 | | } |
351 | | #else |
352 | 0 | libcerror_error_set( |
353 | 0 | error, |
354 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
355 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
356 | 0 | "%s: missing support for bzip2 compression.", |
357 | 0 | function ); |
358 | |
|
359 | 0 | return( -1 ); |
360 | 0 | #endif /* defined( HAVE_BZLIB ) || defined( BZ_DLL ) */ |
361 | 0 | } |
362 | 0 | else |
363 | 0 | { |
364 | 0 | libcerror_error_set( |
365 | 0 | error, |
366 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
367 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
368 | 0 | "%s: unsupported compression method.", |
369 | 0 | function ); |
370 | |
|
371 | 0 | return( -1 ); |
372 | 0 | } |
373 | 0 | return( result ); |
374 | 0 | } |
375 | | |
376 | | /* Decompresses data using the compression method |
377 | | * Returns 1 on success, 0 on failure or -1 on error |
378 | | */ |
379 | | int libewf_decompress_data( |
380 | | const uint8_t *compressed_data, |
381 | | size_t compressed_data_size, |
382 | | uint16_t compression_method, |
383 | | uint8_t *uncompressed_data, |
384 | | size_t *uncompressed_data_size, |
385 | | libcerror_error_t **error ) |
386 | 3.77k | { |
387 | 3.77k | static char *function = "libewf_decompress_data"; |
388 | 3.77k | int result = 0; |
389 | | |
390 | | #if defined( HAVE_BZLIB ) || defined( BZ_DLL ) |
391 | | unsigned int bzip2_uncompressed_data_size = 0; |
392 | | #endif |
393 | | #if ( defined( HAVE_ZLIB ) && defined( HAVE_ZLIB_UNCOMPRESS ) ) || defined( ZLIB_DLL ) |
394 | | uLongf zlib_uncompressed_data_size = 0; |
395 | | #endif |
396 | | |
397 | 3.77k | if( compressed_data == NULL ) |
398 | 0 | { |
399 | 0 | libcerror_error_set( |
400 | 0 | error, |
401 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
402 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
403 | 0 | "%s: invalid compressed data buffer.", |
404 | 0 | function ); |
405 | |
|
406 | 0 | return( -1 ); |
407 | 0 | } |
408 | 3.77k | if( uncompressed_data == NULL ) |
409 | 0 | { |
410 | 0 | libcerror_error_set( |
411 | 0 | error, |
412 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
413 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
414 | 0 | "%s: invalid uncompressed data buffer.", |
415 | 0 | function ); |
416 | |
|
417 | 0 | return( -1 ); |
418 | 0 | } |
419 | 3.77k | if( uncompressed_data_size == NULL ) |
420 | 0 | { |
421 | 0 | libcerror_error_set( |
422 | 0 | error, |
423 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
424 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
425 | 0 | "%s: invalid uncompressed data size.", |
426 | 0 | function ); |
427 | |
|
428 | 0 | return( -1 ); |
429 | 0 | } |
430 | 3.77k | if( uncompressed_data == compressed_data ) |
431 | 0 | { |
432 | 0 | libcerror_error_set( |
433 | 0 | error, |
434 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
435 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
436 | 0 | "%s: invalid compressed data buffer equals uncompressed data buffer.", |
437 | 0 | function ); |
438 | |
|
439 | 0 | return( -1 ); |
440 | 0 | } |
441 | 3.77k | if( compression_method == LIBEWF_COMPRESSION_METHOD_DEFLATE ) |
442 | 3.76k | { |
443 | | #if ( defined( HAVE_ZLIB ) && defined( HAVE_ZLIB_UNCOMPRESS ) ) || defined( ZLIB_DLL ) |
444 | | #if ULONG_MAX < SSIZE_MAX |
445 | | if( compressed_data_size > (size_t) ULONG_MAX ) |
446 | | #else |
447 | | if( compressed_data_size > (size_t) SSIZE_MAX ) |
448 | | #endif |
449 | | { |
450 | | libcerror_error_set( |
451 | | error, |
452 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
453 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
454 | | "%s: invalid compressed data size value exceeds maximum.", |
455 | | function ); |
456 | | |
457 | | return( -1 ); |
458 | | } |
459 | | #if ULONG_MAX < SSIZE_MAX |
460 | | if( *uncompressed_data_size > (size_t) ULONG_MAX ) |
461 | | #else |
462 | | if( *uncompressed_data_size > (size_t) SSIZE_MAX ) |
463 | | #endif |
464 | | { |
465 | | libcerror_error_set( |
466 | | error, |
467 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
468 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
469 | | "%s: invalid uncompressed data size value exceeds maximum.", |
470 | | function ); |
471 | | |
472 | | return( -1 ); |
473 | | } |
474 | | zlib_uncompressed_data_size = (uLongf) *uncompressed_data_size; |
475 | | |
476 | | result = uncompress( |
477 | | (Bytef *) uncompressed_data, |
478 | | &zlib_uncompressed_data_size, |
479 | | (Bytef *) compressed_data, |
480 | | (uLong) compressed_data_size ); |
481 | | |
482 | | if( result == Z_OK ) |
483 | | { |
484 | | *uncompressed_data_size = (size_t) zlib_uncompressed_data_size; |
485 | | |
486 | | result = 1; |
487 | | } |
488 | | else if( result == Z_DATA_ERROR ) |
489 | | { |
490 | | #if defined( HAVE_DEBUG_OUTPUT ) |
491 | | if( libcnotify_verbose != 0 ) |
492 | | { |
493 | | libcnotify_printf( |
494 | | "%s: unable to read compressed data: data error.\n", |
495 | | function ); |
496 | | } |
497 | | #endif |
498 | | *uncompressed_data_size = 0; |
499 | | |
500 | | result = -1; |
501 | | } |
502 | | else if( result == Z_BUF_ERROR ) |
503 | | { |
504 | | #if defined( HAVE_DEBUG_OUTPUT ) |
505 | | if( libcnotify_verbose != 0 ) |
506 | | { |
507 | | libcnotify_printf( |
508 | | "%s: unable to read compressed data: target buffer too small.\n", |
509 | | function ); |
510 | | } |
511 | | #endif |
512 | | /* Estimate that a factor 2 enlargement should suffice |
513 | | */ |
514 | | *uncompressed_data_size *= 2; |
515 | | |
516 | | result = 0; |
517 | | } |
518 | | else if( result == Z_MEM_ERROR ) |
519 | | { |
520 | | libcerror_error_set( |
521 | | error, |
522 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
523 | | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
524 | | "%s: unable to read compressed data: insufficient memory.", |
525 | | function ); |
526 | | |
527 | | *uncompressed_data_size = 0; |
528 | | |
529 | | result = -1; |
530 | | } |
531 | | else |
532 | | { |
533 | | libcerror_error_set( |
534 | | error, |
535 | | LIBCERROR_ERROR_DOMAIN_COMPRESSION, |
536 | | LIBCERROR_COMPRESSION_ERROR_DECOMPRESS_FAILED, |
537 | | "%s: zlib returned undefined error: %d.", |
538 | | function, |
539 | | result ); |
540 | | |
541 | | *uncompressed_data_size = 0; |
542 | | |
543 | | result = -1; |
544 | | } |
545 | | #else |
546 | 3.76k | result = libewf_deflate_decompress_zlib( |
547 | 3.76k | compressed_data, |
548 | 3.76k | compressed_data_size, |
549 | 3.76k | uncompressed_data, |
550 | 3.76k | uncompressed_data_size, |
551 | 3.76k | error ); |
552 | | |
553 | 3.76k | if( result != 1 ) |
554 | 432 | { |
555 | 432 | libcerror_error_set( |
556 | 432 | error, |
557 | 432 | LIBCERROR_ERROR_DOMAIN_ENCRYPTION, |
558 | 432 | LIBCERROR_ENCRYPTION_ERROR_GENERIC, |
559 | 432 | "%s: unable to decompress deflate compressed data.", |
560 | 432 | function ); |
561 | | |
562 | 432 | return( -1 ); |
563 | 432 | } |
564 | 3.76k | #endif /* ( defined( HAVE_ZLIB ) && defined( HAVE_ZLIB_UNCOMPRESS ) ) || defined( ZLIB_DLL ) */ |
565 | 3.76k | } |
566 | 17 | else if( compression_method == LIBEWF_COMPRESSION_METHOD_BZIP2 ) |
567 | 1 | { |
568 | | #if defined( HAVE_BZLIB ) || defined( BZ_DLL ) |
569 | | if( compressed_data_size > (size_t) UINT_MAX ) |
570 | | { |
571 | | libcerror_error_set( |
572 | | error, |
573 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
574 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
575 | | "%s: invalid compressed data size value exceeds maximum.", |
576 | | function ); |
577 | | |
578 | | return( -1 ); |
579 | | } |
580 | | if( *uncompressed_data_size > (size_t) UINT_MAX ) |
581 | | { |
582 | | libcerror_error_set( |
583 | | error, |
584 | | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
585 | | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
586 | | "%s: invalid uncompressed data size value exceeds maximum.", |
587 | | function ); |
588 | | |
589 | | return( -1 ); |
590 | | } |
591 | | bzip2_uncompressed_data_size = (unsigned int) *uncompressed_data_size; |
592 | | |
593 | | result = BZ2_bzBuffToBuffDecompress( |
594 | | (char *) uncompressed_data, |
595 | | &bzip2_uncompressed_data_size, |
596 | | (char *) compressed_data, |
597 | | (unsigned int) compressed_data_size, |
598 | | 0, |
599 | | 0 ); |
600 | | |
601 | | if( result == BZ_OK ) |
602 | | { |
603 | | *uncompressed_data_size = (size_t) bzip2_uncompressed_data_size; |
604 | | |
605 | | result = 1; |
606 | | } |
607 | | else if( ( result == BZ_DATA_ERROR ) |
608 | | || ( result == BZ_DATA_ERROR_MAGIC ) ) |
609 | | { |
610 | | #if defined( HAVE_DEBUG_OUTPUT ) |
611 | | if( libcnotify_verbose != 0 ) |
612 | | { |
613 | | libcnotify_printf( |
614 | | "%s: unable to read compressed data: data error.\n", |
615 | | function ); |
616 | | } |
617 | | #endif |
618 | | *uncompressed_data_size = 0; |
619 | | |
620 | | result = -1; |
621 | | } |
622 | | else if( result == BZ_OUTBUFF_FULL ) |
623 | | { |
624 | | #if defined( HAVE_DEBUG_OUTPUT ) |
625 | | if( libcnotify_verbose != 0 ) |
626 | | { |
627 | | libcnotify_printf( |
628 | | "%s: unable to read compressed data: target buffer too small.\n", |
629 | | function ); |
630 | | } |
631 | | #endif |
632 | | /* Estimate that a factor 2 enlargement should suffice |
633 | | */ |
634 | | *uncompressed_data_size *= 2; |
635 | | |
636 | | result = 0; |
637 | | } |
638 | | else if( result == BZ_MEM_ERROR ) |
639 | | { |
640 | | libcerror_error_set( |
641 | | error, |
642 | | LIBCERROR_ERROR_DOMAIN_MEMORY, |
643 | | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
644 | | "%s: unable to read compressed data: insufficient memory.", |
645 | | function ); |
646 | | |
647 | | *uncompressed_data_size = 0; |
648 | | |
649 | | result = -1; |
650 | | } |
651 | | else |
652 | | { |
653 | | libcerror_error_set( |
654 | | error, |
655 | | LIBCERROR_ERROR_DOMAIN_COMPRESSION, |
656 | | LIBCERROR_COMPRESSION_ERROR_DECOMPRESS_FAILED, |
657 | | "%s: libbz2 returned undefined error: %d.", |
658 | | function, |
659 | | result ); |
660 | | |
661 | | *uncompressed_data_size = 0; |
662 | | |
663 | | result = -1; |
664 | | } |
665 | | #else |
666 | 1 | libcerror_error_set( |
667 | 1 | error, |
668 | 1 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
669 | 1 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
670 | 1 | "%s: missing support for bzip2 compression.", |
671 | 1 | function ); |
672 | | |
673 | 1 | return( -1 ); |
674 | 1 | #endif /* defined( HAVE_BZLIB ) || defined( BZ_DLL ) */ |
675 | 1 | } |
676 | 16 | else |
677 | 16 | { |
678 | 16 | libcerror_error_set( |
679 | 16 | error, |
680 | 16 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
681 | 16 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
682 | 16 | "%s: unsupported compression method.", |
683 | 16 | function ); |
684 | | |
685 | 16 | return( -1 ); |
686 | 16 | } |
687 | 3.33k | return( result ); |
688 | 3.77k | } |
689 | | |