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