/src/libmodi/libmodi/libmodi_bzip.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * BZip (un)compression functions |
3 | | * |
4 | | * Copyright (C) 2012-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 <memory.h> |
24 | | #include <types.h> |
25 | | |
26 | | #include "libmodi_bit_stream.h" |
27 | | #include "libmodi_bzip.h" |
28 | | #include "libmodi_huffman_tree.h" |
29 | | #include "libmodi_libcerror.h" |
30 | | #include "libmodi_libcnotify.h" |
31 | | |
32 | | /* Table of the CRC-32 of all 8-bit messages. |
33 | | */ |
34 | | uint32_t libmodi_bzip_crc32_table[ 256 ]; |
35 | | |
36 | | /* Value to indicate the CRC-32 table been computed |
37 | | */ |
38 | | int libmodi_bzip_crc32_table_computed = 0; |
39 | | |
40 | | /* Initializes the internal CRC-32 table |
41 | | * The table speeds up the CRC-32 calculation |
42 | | * The table is calcuted in reverse bit-order |
43 | | */ |
44 | | void libmodi_bzip_initialize_crc32_table( |
45 | | void ) |
46 | 0 | { |
47 | 0 | uint32_t crc32 = 0; |
48 | 0 | uint16_t crc32_table_index = 0; |
49 | 0 | uint8_t bit_iterator = 0; |
50 | |
|
51 | 0 | for( crc32_table_index = 0; |
52 | 0 | crc32_table_index < 256; |
53 | 0 | crc32_table_index++ ) |
54 | 0 | { |
55 | 0 | crc32 = (uint32_t) crc32_table_index << 24; |
56 | |
|
57 | 0 | for( bit_iterator = 0; |
58 | 0 | bit_iterator < 8; |
59 | 0 | bit_iterator++ ) |
60 | 0 | { |
61 | 0 | if( crc32 & 0x80000000UL ) |
62 | 0 | { |
63 | 0 | crc32 = 0x04c11db7UL ^ ( crc32 << 1 ); |
64 | 0 | } |
65 | 0 | else |
66 | 0 | { |
67 | 0 | crc32 = crc32 << 1; |
68 | 0 | } |
69 | 0 | } |
70 | 0 | libmodi_bzip_crc32_table[ crc32_table_index ] = crc32; |
71 | 0 | } |
72 | 0 | libmodi_bzip_crc32_table_computed = 1; |
73 | 0 | } |
74 | | |
75 | | /* Calculates the CRC-32 of a buffer |
76 | | * Use a previous key of 0 to calculate a new CRC-32 |
77 | | * Returns 1 if successful or -1 on error |
78 | | */ |
79 | | int libmodi_bzip_calculate_crc32( |
80 | | uint32_t *crc32, |
81 | | const uint8_t *data, |
82 | | size_t data_size, |
83 | | uint32_t initial_value, |
84 | | libcerror_error_t **error ) |
85 | 0 | { |
86 | 0 | static char *function = "libmodi_bzip_calculate_crc32"; |
87 | 0 | size_t data_offset = 0; |
88 | 0 | uint32_t crc32_table_index = 0; |
89 | 0 | uint32_t safe_crc32 = 0; |
90 | |
|
91 | 0 | if( crc32 == NULL ) |
92 | 0 | { |
93 | 0 | libcerror_error_set( |
94 | 0 | error, |
95 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
96 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
97 | 0 | "%s: invalid CRC-32.", |
98 | 0 | function ); |
99 | |
|
100 | 0 | return( -1 ); |
101 | 0 | } |
102 | 0 | if( data == NULL ) |
103 | 0 | { |
104 | 0 | libcerror_error_set( |
105 | 0 | error, |
106 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
107 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
108 | 0 | "%s: invalid data.", |
109 | 0 | function ); |
110 | |
|
111 | 0 | return( -1 ); |
112 | 0 | } |
113 | 0 | if( data_size > (size_t) SSIZE_MAX ) |
114 | 0 | { |
115 | 0 | libcerror_error_set( |
116 | 0 | error, |
117 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
118 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
119 | 0 | "%s: invalid data size value exceeds maximum.", |
120 | 0 | function ); |
121 | |
|
122 | 0 | return( -1 ); |
123 | 0 | } |
124 | 0 | if( libmodi_bzip_crc32_table_computed == 0 ) |
125 | 0 | { |
126 | 0 | libmodi_bzip_initialize_crc32_table(); |
127 | 0 | } |
128 | 0 | safe_crc32 = initial_value ^ (uint32_t) 0xffffffffUL; |
129 | |
|
130 | 0 | for( data_offset = 0; |
131 | 0 | data_offset < data_size; |
132 | 0 | data_offset++ ) |
133 | 0 | { |
134 | | /* Use the upper 8-bits of the pre-calculated CRC-32 values due to BZip bit ordering |
135 | | */ |
136 | 0 | crc32_table_index = ( ( safe_crc32 >> 24 ) ^ data[ data_offset ] ) & 0x000000ffUL; |
137 | |
|
138 | 0 | safe_crc32 = libmodi_bzip_crc32_table[ crc32_table_index ] ^ ( safe_crc32 << 8 ); |
139 | 0 | } |
140 | 0 | *crc32 = safe_crc32 ^ (uint32_t) 0xffffffffUL; |
141 | |
|
142 | 0 | return( 1 ); |
143 | 0 | } |
144 | | |
145 | | /* Reverses a Burrows-Wheeler transform and run-length encoded strings |
146 | | * Returns 1 on success or -1 on error |
147 | | */ |
148 | | int libmodi_bzip_reverse_burrows_wheeler_transform( |
149 | | const uint8_t *input_data, |
150 | | size_t input_data_size, |
151 | | size_t *permutations, |
152 | | uint32_t origin_pointer, |
153 | | uint8_t *uncompressed_data, |
154 | | size_t uncompressed_data_size, |
155 | | size_t *uncompressed_data_offset, |
156 | | libcerror_error_t **error ) |
157 | 0 | { |
158 | 0 | size_t distributions[ 256 ]; |
159 | |
|
160 | 0 | static char *function = "libmodi_bzip_reverse_burrows_wheeler_transform"; |
161 | 0 | size_t input_data_offset = 0; |
162 | 0 | size_t distribution_value = 0; |
163 | 0 | size_t number_of_values = 0; |
164 | 0 | size_t permutation_value = 0; |
165 | 0 | size_t safe_uncompressed_data_offset = 0; |
166 | 0 | uint16_t byte_value = 0; |
167 | 0 | uint16_t last_byte_value = 0; |
168 | 0 | uint8_t number_of_last_byte_values = 0; |
169 | |
|
170 | 0 | if( input_data == NULL ) |
171 | 0 | { |
172 | 0 | libcerror_error_set( |
173 | 0 | error, |
174 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
175 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
176 | 0 | "%s: invalid input data.", |
177 | 0 | function ); |
178 | |
|
179 | 0 | return( -1 ); |
180 | 0 | } |
181 | 0 | if( input_data_size > (size_t) SSIZE_MAX ) |
182 | 0 | { |
183 | 0 | libcerror_error_set( |
184 | 0 | error, |
185 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
186 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
187 | 0 | "%s: invalid input data size value exceeds maximum.", |
188 | 0 | function ); |
189 | |
|
190 | 0 | return( -1 ); |
191 | 0 | } |
192 | 0 | if( permutations == NULL ) |
193 | 0 | { |
194 | 0 | libcerror_error_set( |
195 | 0 | error, |
196 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
197 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
198 | 0 | "%s: invalid permutations.", |
199 | 0 | function ); |
200 | |
|
201 | 0 | return( -1 ); |
202 | 0 | } |
203 | 0 | if( uncompressed_data == NULL ) |
204 | 0 | { |
205 | 0 | libcerror_error_set( |
206 | 0 | error, |
207 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
208 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
209 | 0 | "%s: invalid uncompressed data.", |
210 | 0 | function ); |
211 | |
|
212 | 0 | return( -1 ); |
213 | 0 | } |
214 | 0 | if( uncompressed_data_size > (size_t) SSIZE_MAX ) |
215 | 0 | { |
216 | 0 | libcerror_error_set( |
217 | 0 | error, |
218 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
219 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
220 | 0 | "%s: invalid uncompressed data size value exceeds maximum.", |
221 | 0 | function ); |
222 | |
|
223 | 0 | return( -1 ); |
224 | 0 | } |
225 | 0 | if( uncompressed_data_offset == NULL ) |
226 | 0 | { |
227 | 0 | libcerror_error_set( |
228 | 0 | error, |
229 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
230 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
231 | 0 | "%s: invalid uncompressed data offset.", |
232 | 0 | function ); |
233 | |
|
234 | 0 | return( -1 ); |
235 | 0 | } |
236 | 0 | safe_uncompressed_data_offset = *uncompressed_data_offset; |
237 | |
|
238 | 0 | if( safe_uncompressed_data_offset > (size_t) SSIZE_MAX ) |
239 | 0 | { |
240 | 0 | libcerror_error_set( |
241 | 0 | error, |
242 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
243 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
244 | 0 | "%s: invalid uncompressed data offset value exceeds maximum.", |
245 | 0 | function ); |
246 | |
|
247 | 0 | return( -1 ); |
248 | 0 | } |
249 | 0 | if( memory_set( |
250 | 0 | distributions, |
251 | 0 | 0, |
252 | 0 | sizeof( size_t ) * 256 ) == NULL ) |
253 | 0 | { |
254 | 0 | libcerror_error_set( |
255 | 0 | error, |
256 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
257 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
258 | 0 | "%s: unable to clear distributions.", |
259 | 0 | function ); |
260 | |
|
261 | 0 | return( -1 ); |
262 | 0 | } |
263 | 0 | for( input_data_offset = 0; |
264 | 0 | input_data_offset < input_data_size; |
265 | 0 | input_data_offset++ ) |
266 | 0 | { |
267 | 0 | byte_value = input_data[ input_data_offset ]; |
268 | |
|
269 | 0 | distributions[ byte_value ] += 1; |
270 | 0 | } |
271 | 0 | for( byte_value = 0; |
272 | 0 | byte_value < 256; |
273 | 0 | byte_value++ ) |
274 | 0 | { |
275 | 0 | number_of_values = distributions[ byte_value ]; |
276 | |
|
277 | 0 | distributions[ byte_value ] = distribution_value; |
278 | |
|
279 | 0 | distribution_value += number_of_values; |
280 | 0 | } |
281 | 0 | for( input_data_offset = 0; |
282 | 0 | input_data_offset < input_data_size; |
283 | 0 | input_data_offset++ ) |
284 | 0 | { |
285 | 0 | byte_value = input_data[ input_data_offset ]; |
286 | |
|
287 | 0 | distribution_value = distributions[ byte_value ]; |
288 | |
|
289 | 0 | permutations[ distribution_value ] = input_data_offset; |
290 | |
|
291 | 0 | distributions[ byte_value ] += 1; |
292 | 0 | } |
293 | 0 | permutation_value = permutations[ origin_pointer ]; |
294 | |
|
295 | 0 | for( input_data_offset = 0; |
296 | 0 | input_data_offset < input_data_size; |
297 | 0 | input_data_offset++ ) |
298 | 0 | { |
299 | 0 | byte_value = input_data[ permutation_value ]; |
300 | |
|
301 | 0 | if( number_of_last_byte_values == 4 ) |
302 | 0 | { |
303 | 0 | if( ( byte_value > uncompressed_data_size ) |
304 | 0 | || ( safe_uncompressed_data_offset > ( uncompressed_data_size - byte_value ) ) ) |
305 | 0 | { |
306 | 0 | libcerror_error_set( |
307 | 0 | error, |
308 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
309 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
310 | 0 | "%s: invalid uncompressed data value too small.", |
311 | 0 | function ); |
312 | |
|
313 | 0 | return( -1 ); |
314 | 0 | } |
315 | 0 | while( byte_value > 0 ) |
316 | 0 | { |
317 | 0 | uncompressed_data[ safe_uncompressed_data_offset++ ] = (uint8_t) last_byte_value; |
318 | |
|
319 | 0 | byte_value--; |
320 | 0 | } |
321 | 0 | last_byte_value = 0; |
322 | 0 | number_of_last_byte_values = 0; |
323 | 0 | } |
324 | 0 | else |
325 | 0 | { |
326 | 0 | if( byte_value != last_byte_value ) |
327 | 0 | { |
328 | 0 | number_of_last_byte_values = 0; |
329 | 0 | } |
330 | 0 | last_byte_value = byte_value; |
331 | 0 | number_of_last_byte_values += 1; |
332 | |
|
333 | 0 | if( safe_uncompressed_data_offset >= uncompressed_data_size ) |
334 | 0 | { |
335 | 0 | libcerror_error_set( |
336 | 0 | error, |
337 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
338 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
339 | 0 | "%s: invalid uncompressed data value too small.", |
340 | 0 | function ); |
341 | |
|
342 | 0 | return( -1 ); |
343 | 0 | } |
344 | 0 | uncompressed_data[ safe_uncompressed_data_offset++ ] = (uint8_t) byte_value; |
345 | 0 | } |
346 | 0 | permutation_value = permutations[ permutation_value ]; |
347 | 0 | } |
348 | 0 | *uncompressed_data_offset = safe_uncompressed_data_offset; |
349 | |
|
350 | 0 | return( 1 ); |
351 | 0 | } |
352 | | |
353 | | /* Reads the stream header |
354 | | * Returns 1 on success or -1 on error |
355 | | */ |
356 | | int libmodi_bzip_read_stream_header( |
357 | | const uint8_t *compressed_data, |
358 | | size_t compressed_data_size, |
359 | | size_t *compressed_data_offset, |
360 | | uint8_t *compression_level, |
361 | | libcerror_error_t **error ) |
362 | 0 | { |
363 | 0 | static char *function = "libmodi_bzip_read_stream_header"; |
364 | 0 | size_t safe_compressed_data_offset = 0; |
365 | 0 | uint8_t safe_compression_level = 0; |
366 | |
|
367 | 0 | if( compressed_data == NULL ) |
368 | 0 | { |
369 | 0 | libcerror_error_set( |
370 | 0 | error, |
371 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
372 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
373 | 0 | "%s: invalid compressed data.", |
374 | 0 | function ); |
375 | |
|
376 | 0 | return( -1 ); |
377 | 0 | } |
378 | 0 | if( ( compressed_data_size < 4 ) |
379 | 0 | || ( compressed_data_size > (size_t) SSIZE_MAX ) ) |
380 | 0 | { |
381 | 0 | libcerror_error_set( |
382 | 0 | error, |
383 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
384 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
385 | 0 | "%s: invalid compressed data size value out of bounds.", |
386 | 0 | function ); |
387 | |
|
388 | 0 | return( -1 ); |
389 | 0 | } |
390 | 0 | if( compressed_data_offset == NULL ) |
391 | 0 | { |
392 | 0 | libcerror_error_set( |
393 | 0 | error, |
394 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
395 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
396 | 0 | "%s: invalid compressed data offset.", |
397 | 0 | function ); |
398 | |
|
399 | 0 | return( -1 ); |
400 | 0 | } |
401 | 0 | safe_compressed_data_offset = *compressed_data_offset; |
402 | |
|
403 | 0 | if( safe_compressed_data_offset > ( compressed_data_size - 4 ) ) |
404 | 0 | { |
405 | 0 | libcerror_error_set( |
406 | 0 | error, |
407 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
408 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
409 | 0 | "%s: invalid compressed data value too small.", |
410 | 0 | function ); |
411 | |
|
412 | 0 | return( -1 ); |
413 | 0 | } |
414 | 0 | if( compression_level == NULL ) |
415 | 0 | { |
416 | 0 | libcerror_error_set( |
417 | 0 | error, |
418 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
419 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
420 | 0 | "%s: invalid compression level.", |
421 | 0 | function ); |
422 | |
|
423 | 0 | return( -1 ); |
424 | 0 | } |
425 | | #if defined( HAVE_DEBUG_OUTPUT ) |
426 | | if( libcnotify_verbose != 0 ) |
427 | | { |
428 | | libcnotify_printf( |
429 | | "%s: stream header data:\n", |
430 | | function ); |
431 | | libcnotify_print_data( |
432 | | &( compressed_data[ safe_compressed_data_offset ] ), |
433 | | 4, |
434 | | 0 ); |
435 | | } |
436 | | #endif |
437 | 0 | if( ( compressed_data[ safe_compressed_data_offset + 0 ] != 'B' ) |
438 | 0 | || ( compressed_data[ safe_compressed_data_offset + 1 ] != 'Z' ) ) |
439 | 0 | { |
440 | 0 | libcerror_error_set( |
441 | 0 | error, |
442 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
443 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
444 | 0 | "%s: unsupported signature.", |
445 | 0 | function ); |
446 | |
|
447 | 0 | return( -1 ); |
448 | 0 | } |
449 | 0 | safe_compression_level = compressed_data[ safe_compressed_data_offset + 3 ]; |
450 | |
|
451 | 0 | if( ( safe_compression_level < '1' ) |
452 | 0 | || ( safe_compression_level > '9' ) ) |
453 | 0 | { |
454 | 0 | libcerror_error_set( |
455 | 0 | error, |
456 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
457 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
458 | 0 | "%s: unsupported compression level.", |
459 | 0 | function ); |
460 | |
|
461 | 0 | return( -1 ); |
462 | 0 | } |
463 | 0 | safe_compression_level -= '0'; |
464 | |
|
465 | | #if defined( HAVE_DEBUG_OUTPUT ) |
466 | | if( libcnotify_verbose != 0 ) |
467 | | { |
468 | | libcnotify_printf( |
469 | | "%s: signature\t\t\t\t: %c%c\n", |
470 | | function, |
471 | | compressed_data[ safe_compressed_data_offset + 0 ], |
472 | | compressed_data[ safe_compressed_data_offset + 1 ] ); |
473 | | |
474 | | libcnotify_printf( |
475 | | "%s: format version\t\t\t: 0x%02" PRIx8 "\n", |
476 | | function, |
477 | | compressed_data[ safe_compressed_data_offset + 2 ] ); |
478 | | |
479 | | libcnotify_printf( |
480 | | "%s: compression level\t\t\t: %" PRIu8 "\n", |
481 | | function, |
482 | | safe_compression_level ); |
483 | | |
484 | | libcnotify_printf( |
485 | | "\n" ); |
486 | | } |
487 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
488 | |
|
489 | 0 | if( compressed_data[ safe_compressed_data_offset + 2 ] != 0x68 ) |
490 | 0 | { |
491 | 0 | libcerror_error_set( |
492 | 0 | error, |
493 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
494 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
495 | 0 | "%s: unsupported format version.", |
496 | 0 | function ); |
497 | |
|
498 | 0 | return( -1 ); |
499 | 0 | } |
500 | 0 | *compressed_data_offset = safe_compressed_data_offset + 4; |
501 | 0 | *compression_level = safe_compression_level; |
502 | |
|
503 | 0 | return( 1 ); |
504 | 0 | } |
505 | | |
506 | | /* Reads a (stream) block header or stream footer signature |
507 | | * Returns 1 on success or -1 on error |
508 | | */ |
509 | | int libmodi_bzip_read_signature( |
510 | | libmodi_bit_stream_t *bit_stream, |
511 | | uint64_t *signature, |
512 | | libcerror_error_t **error ) |
513 | 0 | { |
514 | 0 | static char *function = "libmodi_bzip_read_signature"; |
515 | 0 | uint32_t value_32bit = 0; |
516 | 0 | uint64_t safe_signature = 0; |
517 | |
|
518 | 0 | if( signature == NULL ) |
519 | 0 | { |
520 | 0 | libcerror_error_set( |
521 | 0 | error, |
522 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
523 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
524 | 0 | "%s: invalid signature.", |
525 | 0 | function ); |
526 | |
|
527 | 0 | return( -1 ); |
528 | 0 | } |
529 | 0 | if( libmodi_bit_stream_get_value( |
530 | 0 | bit_stream, |
531 | 0 | 24, |
532 | 0 | &value_32bit, |
533 | 0 | error ) != 1 ) |
534 | 0 | { |
535 | 0 | libcerror_error_set( |
536 | 0 | error, |
537 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
538 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
539 | 0 | "%s: unable to retrieve value from bit stream.", |
540 | 0 | function ); |
541 | |
|
542 | 0 | return( -1 ); |
543 | 0 | } |
544 | 0 | safe_signature = value_32bit; |
545 | |
|
546 | 0 | if( libmodi_bit_stream_get_value( |
547 | 0 | bit_stream, |
548 | 0 | 24, |
549 | 0 | &value_32bit, |
550 | 0 | error ) != 1 ) |
551 | 0 | { |
552 | 0 | libcerror_error_set( |
553 | 0 | error, |
554 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
555 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
556 | 0 | "%s: unable to retrieve value from bit stream.", |
557 | 0 | function ); |
558 | |
|
559 | 0 | return( -1 ); |
560 | 0 | } |
561 | 0 | safe_signature <<= 24; |
562 | 0 | safe_signature |= value_32bit; |
563 | |
|
564 | 0 | *signature = safe_signature; |
565 | |
|
566 | 0 | return( 1 ); |
567 | 0 | } |
568 | | |
569 | | /* Reads a (stream) block header |
570 | | * Returns 1 on success or -1 on error |
571 | | */ |
572 | | int libmodi_bzip_read_block_header( |
573 | | libmodi_bit_stream_t *bit_stream, |
574 | | uint64_t signature, |
575 | | uint32_t *origin_pointer, |
576 | | libcerror_error_t **error ) |
577 | 0 | { |
578 | 0 | static char *function = "libmodi_bzip_read_block_header"; |
579 | 0 | uint32_t checksum = 0; |
580 | 0 | uint32_t safe_origin_pointer = 0; |
581 | 0 | uint32_t value_32bit = 0; |
582 | 0 | uint8_t is_randomized = 0; |
583 | |
|
584 | 0 | if( origin_pointer == NULL ) |
585 | 0 | { |
586 | 0 | libcerror_error_set( |
587 | 0 | error, |
588 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
589 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
590 | 0 | "%s: invalid origin pointer.", |
591 | 0 | function ); |
592 | |
|
593 | 0 | return( -1 ); |
594 | 0 | } |
595 | 0 | if( libmodi_bit_stream_get_value( |
596 | 0 | bit_stream, |
597 | 0 | 32, |
598 | 0 | &checksum, |
599 | 0 | error ) != 1 ) |
600 | 0 | { |
601 | 0 | libcerror_error_set( |
602 | 0 | error, |
603 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
604 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
605 | 0 | "%s: unable to retrieve value from bit stream.", |
606 | 0 | function ); |
607 | |
|
608 | 0 | return( -1 ); |
609 | 0 | } |
610 | 0 | if( libmodi_bit_stream_get_value( |
611 | 0 | bit_stream, |
612 | 0 | 1, |
613 | 0 | &value_32bit, |
614 | 0 | error ) != 1 ) |
615 | 0 | { |
616 | 0 | libcerror_error_set( |
617 | 0 | error, |
618 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
619 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
620 | 0 | "%s: unable to retrieve value from bit stream.", |
621 | 0 | function ); |
622 | |
|
623 | 0 | return( -1 ); |
624 | 0 | } |
625 | 0 | is_randomized = (uint8_t) ( value_32bit & 0x00000001UL ); |
626 | |
|
627 | 0 | if( libmodi_bit_stream_get_value( |
628 | 0 | bit_stream, |
629 | 0 | 24, |
630 | 0 | &safe_origin_pointer, |
631 | 0 | error ) != 1 ) |
632 | 0 | { |
633 | 0 | libcerror_error_set( |
634 | 0 | error, |
635 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
636 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
637 | 0 | "%s: unable to retrieve value from bit stream.", |
638 | 0 | function ); |
639 | |
|
640 | 0 | return( -1 ); |
641 | 0 | } |
642 | | #if defined( HAVE_DEBUG_OUTPUT ) |
643 | | if( libcnotify_verbose != 0 ) |
644 | | { |
645 | | libcnotify_printf( |
646 | | "%s: signature\t\t\t\t: 0x%08" PRIx64 "\n", |
647 | | function, |
648 | | signature ); |
649 | | |
650 | | libcnotify_printf( |
651 | | "%s: checksum\t\t\t\t: 0x%08" PRIx32 "\n", |
652 | | function, |
653 | | checksum ); |
654 | | |
655 | | libcnotify_printf( |
656 | | "%s: is randomized\t\t\t\t: %" PRIu8 "\n", |
657 | | function, |
658 | | is_randomized ); |
659 | | |
660 | | libcnotify_printf( |
661 | | "%s: origin pointer\t\t\t\t: 0x%06" PRIx32 "\n", |
662 | | function, |
663 | | safe_origin_pointer ); |
664 | | |
665 | | libcnotify_printf( |
666 | | "\n" ); |
667 | | } |
668 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
669 | | |
670 | 0 | if( signature != 0x314159265359UL ) |
671 | 0 | { |
672 | 0 | libcerror_error_set( |
673 | 0 | error, |
674 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
675 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
676 | 0 | "%s: unsupported signature.", |
677 | 0 | function ); |
678 | |
|
679 | 0 | return( -1 ); |
680 | 0 | } |
681 | 0 | if( is_randomized != 0 ) |
682 | 0 | { |
683 | 0 | libcerror_error_set( |
684 | 0 | error, |
685 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
686 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
687 | 0 | "%s: unsupported is randomized flag.", |
688 | 0 | function ); |
689 | |
|
690 | 0 | return( -1 ); |
691 | 0 | } |
692 | 0 | *origin_pointer = safe_origin_pointer; |
693 | |
|
694 | 0 | return( 1 ); |
695 | 0 | } |
696 | | |
697 | | /* Reads a (stream block) symbol stack |
698 | | * Returns 1 on success or -1 on error |
699 | | */ |
700 | | int libmodi_bzip_read_symbol_stack( |
701 | | libmodi_bit_stream_t *bit_stream, |
702 | | uint8_t *symbol_stack, |
703 | | uint16_t *number_of_symbols, |
704 | | libcerror_error_t **error ) |
705 | 0 | { |
706 | 0 | static char *function = "libmodi_bzip_read_symbol_stack"; |
707 | 0 | uint32_t level1_bitmask = 0; |
708 | 0 | uint32_t level1_value = 0; |
709 | 0 | uint32_t level2_bitmask = 0; |
710 | 0 | uint32_t level2_value = 0; |
711 | 0 | uint16_t symbol_index = 0; |
712 | 0 | uint8_t level1_bit_index = 0; |
713 | 0 | uint8_t level2_bit_index = 0; |
714 | 0 | uint8_t symbol_value = 0; |
715 | |
|
716 | 0 | if( symbol_stack == NULL ) |
717 | 0 | { |
718 | 0 | libcerror_error_set( |
719 | 0 | error, |
720 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
721 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
722 | 0 | "%s: invalid symbol stack.", |
723 | 0 | function ); |
724 | |
|
725 | 0 | return( -1 ); |
726 | 0 | } |
727 | 0 | if( number_of_symbols == NULL ) |
728 | 0 | { |
729 | 0 | libcerror_error_set( |
730 | 0 | error, |
731 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
732 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
733 | 0 | "%s: invalid number of symbols.", |
734 | 0 | function ); |
735 | |
|
736 | 0 | return( -1 ); |
737 | 0 | } |
738 | 0 | if( libmodi_bit_stream_get_value( |
739 | 0 | bit_stream, |
740 | 0 | 16, |
741 | 0 | &level1_value, |
742 | 0 | error ) != 1 ) |
743 | 0 | { |
744 | 0 | libcerror_error_set( |
745 | 0 | error, |
746 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
747 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
748 | 0 | "%s: unable to retrieve value from bit stream.", |
749 | 0 | function ); |
750 | |
|
751 | 0 | return( -1 ); |
752 | 0 | } |
753 | | #if defined( HAVE_DEBUG_OUTPUT ) |
754 | | if( libcnotify_verbose != 0 ) |
755 | | { |
756 | | libcnotify_printf( |
757 | | "%s: level 1 value\t\t\t\t: 0x%04" PRIx32 "\n", |
758 | | function, |
759 | | level1_value ); |
760 | | } |
761 | | #endif |
762 | 0 | level1_bitmask = 0x00008000UL; |
763 | |
|
764 | 0 | for( level1_bit_index = 0; |
765 | 0 | level1_bit_index < 16; |
766 | 0 | level1_bit_index++ ) |
767 | 0 | { |
768 | 0 | if( ( level1_value & level1_bitmask ) != 0 ) |
769 | 0 | { |
770 | 0 | if( libmodi_bit_stream_get_value( |
771 | 0 | bit_stream, |
772 | 0 | 16, |
773 | 0 | &level2_value, |
774 | 0 | error ) != 1 ) |
775 | 0 | { |
776 | 0 | libcerror_error_set( |
777 | 0 | error, |
778 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
779 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
780 | 0 | "%s: unable to retrieve value from bit stream.", |
781 | 0 | function ); |
782 | |
|
783 | 0 | return( -1 ); |
784 | 0 | } |
785 | | #if defined( HAVE_DEBUG_OUTPUT ) |
786 | | if( libcnotify_verbose != 0 ) |
787 | | { |
788 | | libcnotify_printf( |
789 | | "%s: level 2 value: % 2" PRIu8 "\t\t\t: 0x%04" PRIx32 "\n", |
790 | | function, |
791 | | level1_bit_index, |
792 | | level2_value ); |
793 | | } |
794 | | #endif |
795 | 0 | level2_bitmask = 0x00008000UL; |
796 | |
|
797 | 0 | for( level2_bit_index = 0; |
798 | 0 | level2_bit_index < 16; |
799 | 0 | level2_bit_index++ ) |
800 | 0 | { |
801 | 0 | if( ( level2_value & level2_bitmask ) != 0 ) |
802 | 0 | { |
803 | 0 | symbol_value = ( 16 * level1_bit_index ) + level2_bit_index; |
804 | |
|
805 | | #if defined( HAVE_DEBUG_OUTPUT ) |
806 | | if( libcnotify_verbose != 0 ) |
807 | | { |
808 | | libcnotify_printf( |
809 | | "%s: symbol value: % 2" PRIu16 "\t\t\t: 0x%02" PRIx8 "\n", |
810 | | function, |
811 | | symbol_index, |
812 | | symbol_value ); |
813 | | } |
814 | | #endif |
815 | 0 | if( symbol_index > 256 ) |
816 | 0 | { |
817 | 0 | libcerror_error_set( |
818 | 0 | error, |
819 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
820 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
821 | 0 | "%s: invalid symbol index value out of bounds.", |
822 | 0 | function ); |
823 | |
|
824 | 0 | return( -1 ); |
825 | 0 | } |
826 | 0 | symbol_stack[ symbol_index++ ] = symbol_value; |
827 | 0 | } |
828 | 0 | level2_bitmask >>= 1; |
829 | 0 | } |
830 | 0 | } |
831 | 0 | level1_bitmask >>= 1; |
832 | 0 | } |
833 | | #if defined( HAVE_DEBUG_OUTPUT ) |
834 | | if( libcnotify_verbose != 0 ) |
835 | | { |
836 | | libcnotify_printf( |
837 | | "\n" ); |
838 | | } |
839 | | #endif |
840 | 0 | *number_of_symbols = symbol_index + 2; |
841 | |
|
842 | 0 | return( 1 ); |
843 | 0 | } |
844 | | |
845 | | /* Reads selectors |
846 | | * Returns 1 on success or -1 on error |
847 | | */ |
848 | | int libmodi_bzip_read_selectors( |
849 | | libmodi_bit_stream_t *bit_stream, |
850 | | uint8_t *selectors, |
851 | | uint8_t number_of_trees, |
852 | | uint16_t number_of_selectors, |
853 | | libcerror_error_t **error ) |
854 | 0 | { |
855 | 0 | uint8_t stack[ 7 ] = { 0, 1, 2, 3, 4, 5, 6 }; |
856 | 0 | static char *function = "libmodi_bzip_read_selectors"; |
857 | 0 | uint32_t value_32bit = 0; |
858 | 0 | uint16_t selector_index = 0; |
859 | 0 | uint8_t selector_value = 0; |
860 | 0 | uint8_t stack_index = 0; |
861 | 0 | uint8_t tree_index = 0; |
862 | |
|
863 | 0 | if( selectors == NULL ) |
864 | 0 | { |
865 | 0 | libcerror_error_set( |
866 | 0 | error, |
867 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
868 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
869 | 0 | "%s: invalid selectors.", |
870 | 0 | function ); |
871 | |
|
872 | 0 | return( -1 ); |
873 | 0 | } |
874 | 0 | for( selector_index = 0; |
875 | 0 | selector_index < number_of_selectors; |
876 | 0 | selector_index++ ) |
877 | 0 | { |
878 | 0 | tree_index = 0; |
879 | |
|
880 | 0 | while( tree_index < number_of_trees ) |
881 | 0 | { |
882 | 0 | if( libmodi_bit_stream_get_value( |
883 | 0 | bit_stream, |
884 | 0 | 1, |
885 | 0 | &value_32bit, |
886 | 0 | error ) != 1 ) |
887 | 0 | { |
888 | 0 | libcerror_error_set( |
889 | 0 | error, |
890 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
891 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
892 | 0 | "%s: unable to retrieve value from bit stream.", |
893 | 0 | function ); |
894 | |
|
895 | 0 | return( -1 ); |
896 | 0 | } |
897 | 0 | if( value_32bit == 0 ) |
898 | 0 | { |
899 | 0 | break; |
900 | 0 | } |
901 | 0 | tree_index += 1; |
902 | 0 | } |
903 | 0 | if( tree_index >= number_of_trees ) |
904 | 0 | { |
905 | 0 | libcerror_error_set( |
906 | 0 | error, |
907 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
908 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
909 | 0 | "%s: invalid tree index value out of bounds.", |
910 | 0 | function ); |
911 | |
|
912 | 0 | return( -1 ); |
913 | 0 | } |
914 | | #if defined( HAVE_DEBUG_OUTPUT ) |
915 | | if( libcnotify_verbose != 0 ) |
916 | | { |
917 | | libcnotify_printf( |
918 | | "%s: tree index: %" PRIu16 "\t\t\t\t: %" PRIu8 "\n", |
919 | | function, |
920 | | selector_index, |
921 | | tree_index ); |
922 | | } |
923 | | #endif |
924 | | /* Inverse move-to-front transform |
925 | | */ |
926 | 0 | selector_value = stack[ tree_index ]; |
927 | |
|
928 | 0 | selectors[ selector_index ] = selector_value; |
929 | |
|
930 | 0 | for( stack_index = tree_index; |
931 | 0 | stack_index > 0; |
932 | 0 | stack_index-- ) |
933 | 0 | { |
934 | 0 | stack[ stack_index ] = stack[ stack_index - 1 ]; |
935 | 0 | } |
936 | 0 | stack[ 0 ] = selector_value; |
937 | 0 | } |
938 | | #if defined( HAVE_DEBUG_OUTPUT ) |
939 | | if( libcnotify_verbose != 0 ) |
940 | | { |
941 | | libcnotify_printf( |
942 | | "\n" ); |
943 | | } |
944 | | #endif |
945 | 0 | return( 1 ); |
946 | 0 | } |
947 | | |
948 | | /* Reads a Huffman tree |
949 | | * Returns 1 on success or -1 on error |
950 | | */ |
951 | | int libmodi_bzip_read_huffman_tree( |
952 | | libmodi_bit_stream_t *bit_stream, |
953 | | libmodi_huffman_tree_t *huffman_tree, |
954 | | uint16_t number_of_symbols, |
955 | | libcerror_error_t **error ) |
956 | 0 | { |
957 | 0 | uint8_t code_size_array[ 258 ]; |
958 | |
|
959 | 0 | static char *function = "libmodi_bzip_read_huffman_tree"; |
960 | 0 | uint32_t check_value = 0; |
961 | 0 | uint32_t value_32bit = 0; |
962 | 0 | uint16_t symbol_index = 0; |
963 | 0 | uint8_t code_size = 0; |
964 | 0 | uint8_t largest_code_size = 0; |
965 | |
|
966 | 0 | if( libmodi_bit_stream_get_value( |
967 | 0 | bit_stream, |
968 | 0 | 5, |
969 | 0 | &value_32bit, |
970 | 0 | error ) != 1 ) |
971 | 0 | { |
972 | 0 | libcerror_error_set( |
973 | 0 | error, |
974 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
975 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
976 | 0 | "%s: unable to retrieve value from bit stream.", |
977 | 0 | function ); |
978 | |
|
979 | 0 | return( -1 ); |
980 | 0 | } |
981 | 0 | code_size = (uint8_t) ( value_32bit & 0x0000001fUL ); |
982 | |
|
983 | 0 | for( symbol_index = 0; |
984 | 0 | symbol_index < number_of_symbols; |
985 | 0 | symbol_index++ ) |
986 | 0 | { |
987 | 0 | while( code_size < 20 ) |
988 | 0 | { |
989 | 0 | if( libmodi_bit_stream_get_value( |
990 | 0 | bit_stream, |
991 | 0 | 1, |
992 | 0 | &value_32bit, |
993 | 0 | error ) != 1 ) |
994 | 0 | { |
995 | 0 | libcerror_error_set( |
996 | 0 | error, |
997 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
998 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
999 | 0 | "%s: unable to retrieve value from bit stream.", |
1000 | 0 | function ); |
1001 | |
|
1002 | 0 | return( -1 ); |
1003 | 0 | } |
1004 | 0 | if( value_32bit == 0 ) |
1005 | 0 | { |
1006 | 0 | break; |
1007 | 0 | } |
1008 | 0 | if( libmodi_bit_stream_get_value( |
1009 | 0 | bit_stream, |
1010 | 0 | 1, |
1011 | 0 | &value_32bit, |
1012 | 0 | error ) != 1 ) |
1013 | 0 | { |
1014 | 0 | libcerror_error_set( |
1015 | 0 | error, |
1016 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1017 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
1018 | 0 | "%s: unable to retrieve value from bit stream.", |
1019 | 0 | function ); |
1020 | |
|
1021 | 0 | return( -1 ); |
1022 | 0 | } |
1023 | 0 | if( value_32bit == 0 ) |
1024 | 0 | { |
1025 | 0 | code_size += 1; |
1026 | 0 | } |
1027 | 0 | else |
1028 | 0 | { |
1029 | 0 | code_size -= 1; |
1030 | 0 | } |
1031 | 0 | } |
1032 | 0 | if( code_size >= 20 ) |
1033 | 0 | { |
1034 | 0 | libcerror_error_set( |
1035 | 0 | error, |
1036 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1037 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1038 | 0 | "%s: invalid code size value out of bounds.", |
1039 | 0 | function ); |
1040 | |
|
1041 | 0 | return( -1 ); |
1042 | 0 | } |
1043 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1044 | | if( libcnotify_verbose != 0 ) |
1045 | | { |
1046 | | libcnotify_printf( |
1047 | | "%s: symbol: % 3" PRIu16 " code size\t\t\t: %" PRIu8 "\n", |
1048 | | function, |
1049 | | symbol_index, |
1050 | | code_size ); |
1051 | | } |
1052 | | #endif |
1053 | 0 | code_size_array[ symbol_index ] = code_size; |
1054 | |
|
1055 | 0 | if( code_size > largest_code_size ) |
1056 | 0 | { |
1057 | 0 | largest_code_size = code_size; |
1058 | 0 | } |
1059 | 0 | } |
1060 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1061 | | if( libcnotify_verbose != 0 ) |
1062 | | { |
1063 | | libcnotify_printf( |
1064 | | "\n" ); |
1065 | | } |
1066 | | #endif |
1067 | 0 | if( largest_code_size > 32 ) |
1068 | 0 | { |
1069 | 0 | libcerror_error_set( |
1070 | 0 | error, |
1071 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1072 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1073 | 0 | "%s: invalid largest code size value out of bounds.", |
1074 | 0 | function ); |
1075 | |
|
1076 | 0 | return( -1 ); |
1077 | 0 | } |
1078 | 0 | check_value = 1 << largest_code_size; |
1079 | |
|
1080 | 0 | for( symbol_index = 0; |
1081 | 0 | symbol_index < number_of_symbols; |
1082 | 0 | symbol_index++ ) |
1083 | 0 | { |
1084 | 0 | code_size = code_size_array[ symbol_index ]; |
1085 | 0 | check_value -= 1 << ( largest_code_size - code_size ); |
1086 | 0 | } |
1087 | 0 | if( check_value != 0 ) |
1088 | 0 | { |
1089 | 0 | libcerror_error_set( |
1090 | 0 | error, |
1091 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1092 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1093 | 0 | "%s: invalid check value out of bounds.", |
1094 | 0 | function ); |
1095 | |
|
1096 | 0 | return( -1 ); |
1097 | 0 | } |
1098 | | /* TODO build tree inside fill array loop ? */ |
1099 | 0 | if( libmodi_huffman_tree_build( |
1100 | 0 | huffman_tree, |
1101 | 0 | code_size_array, |
1102 | 0 | number_of_symbols, |
1103 | 0 | error ) != 1 ) |
1104 | 0 | { |
1105 | 0 | libcerror_error_set( |
1106 | 0 | error, |
1107 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1108 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
1109 | 0 | "%s: unable to build Huffman tree.", |
1110 | 0 | function ); |
1111 | |
|
1112 | 0 | return( -1 ); |
1113 | 0 | } |
1114 | 0 | return( 1 ); |
1115 | 0 | } |
1116 | | |
1117 | | /* Reads the Huffman trees |
1118 | | * Returns 1 on success or -1 on error |
1119 | | */ |
1120 | | int libmodi_bzip_read_huffman_trees( |
1121 | | libmodi_bit_stream_t *bit_stream, |
1122 | | libmodi_huffman_tree_t **huffman_trees, |
1123 | | uint8_t number_of_trees, |
1124 | | uint16_t number_of_symbols, |
1125 | | libcerror_error_t **error ) |
1126 | 0 | { |
1127 | 0 | libmodi_huffman_tree_t *huffman_tree = NULL; |
1128 | 0 | static char *function = "libmodi_bzip_read_huffman_trees"; |
1129 | 0 | uint8_t tree_index = 0; |
1130 | |
|
1131 | 0 | if( huffman_trees == NULL ) |
1132 | 0 | { |
1133 | 0 | libcerror_error_set( |
1134 | 0 | error, |
1135 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1136 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1137 | 0 | "%s: invalid Huffman trees.", |
1138 | 0 | function ); |
1139 | |
|
1140 | 0 | return( -1 ); |
1141 | 0 | } |
1142 | 0 | for( tree_index = 0; |
1143 | 0 | tree_index < number_of_trees; |
1144 | 0 | tree_index++ ) |
1145 | 0 | { |
1146 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1147 | | if( libcnotify_verbose != 0 ) |
1148 | | { |
1149 | | libcnotify_printf( |
1150 | | "%s: reading Huffman tree: %" PRIu8 "\n", |
1151 | | function, |
1152 | | tree_index ); |
1153 | | } |
1154 | | #endif |
1155 | 0 | if( libmodi_huffman_tree_initialize( |
1156 | 0 | &huffman_tree, |
1157 | 0 | number_of_symbols, |
1158 | 0 | 20, |
1159 | 0 | error ) != 1 ) |
1160 | 0 | { |
1161 | 0 | libcerror_error_set( |
1162 | 0 | error, |
1163 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1164 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
1165 | 0 | "%s: unable to create Huffman tree: %" PRIu8 ".", |
1166 | 0 | function, |
1167 | 0 | tree_index ); |
1168 | |
|
1169 | 0 | goto on_error; |
1170 | 0 | } |
1171 | 0 | if( libmodi_bzip_read_huffman_tree( |
1172 | 0 | bit_stream, |
1173 | 0 | huffman_tree, |
1174 | 0 | number_of_symbols, |
1175 | 0 | error ) != 1 ) |
1176 | 0 | { |
1177 | 0 | libcerror_error_set( |
1178 | 0 | error, |
1179 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
1180 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
1181 | 0 | "%s: unable to read Huffman tree: %" PRIu8 ".", |
1182 | 0 | function, |
1183 | 0 | tree_index ); |
1184 | |
|
1185 | 0 | goto on_error; |
1186 | 0 | } |
1187 | 0 | huffman_trees[ tree_index ] = huffman_tree; |
1188 | 0 | huffman_tree = NULL; |
1189 | 0 | } |
1190 | 0 | return( 1 ); |
1191 | | |
1192 | 0 | on_error: |
1193 | 0 | if( huffman_tree != NULL ) |
1194 | 0 | { |
1195 | 0 | libmodi_huffman_tree_free( |
1196 | 0 | &huffman_tree, |
1197 | 0 | NULL ); |
1198 | 0 | } |
1199 | 0 | for( tree_index = 0; |
1200 | 0 | tree_index < number_of_trees; |
1201 | 0 | tree_index++ ) |
1202 | 0 | { |
1203 | 0 | libmodi_huffman_tree_free( |
1204 | 0 | &( huffman_trees[ tree_index ] ), |
1205 | 0 | NULL ); |
1206 | 0 | } |
1207 | 0 | return( -1 ); |
1208 | 0 | } |
1209 | | |
1210 | | /* Reads block data |
1211 | | * Returns 1 on success or -1 on error |
1212 | | */ |
1213 | | int libmodi_bzip_read_block_data( |
1214 | | libmodi_bit_stream_t *bit_stream, |
1215 | | libmodi_huffman_tree_t **huffman_trees, |
1216 | | uint8_t number_of_trees, |
1217 | | uint8_t *selectors, |
1218 | | uint16_t number_of_selectors, |
1219 | | uint8_t *symbol_stack, |
1220 | | uint16_t number_of_symbols, |
1221 | | uint8_t *block_data, |
1222 | | size_t *block_data_size, |
1223 | | libcerror_error_t **error ) |
1224 | 0 | { |
1225 | 0 | static char *function = "libmodi_bzip_read_block_data"; |
1226 | 0 | size_t block_data_offset = 0; |
1227 | 0 | size_t safe_block_data_size = 0; |
1228 | 0 | size_t selector_index = 0; |
1229 | 0 | size_t symbol_index = 0; |
1230 | 0 | uint64_t run_length = 0; |
1231 | 0 | uint64_t run_length_value = 0; |
1232 | 0 | uint16_t end_of_block_symbol = 0; |
1233 | 0 | uint16_t symbol = 0; |
1234 | 0 | uint8_t number_of_run_length_symbols = 0; |
1235 | 0 | uint8_t stack_index = 0; |
1236 | 0 | uint8_t stack_value = 0; |
1237 | 0 | uint8_t stack_value_index = 0; |
1238 | 0 | uint8_t tree_index = 0; |
1239 | |
|
1240 | 0 | if( bit_stream == NULL ) |
1241 | 0 | { |
1242 | 0 | libcerror_error_set( |
1243 | 0 | error, |
1244 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1245 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1246 | 0 | "%s: invalid bit stream.", |
1247 | 0 | function ); |
1248 | |
|
1249 | 0 | return( -1 ); |
1250 | 0 | } |
1251 | 0 | if( huffman_trees == NULL ) |
1252 | 0 | { |
1253 | 0 | libcerror_error_set( |
1254 | 0 | error, |
1255 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1256 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1257 | 0 | "%s: invalid Huffman trees.", |
1258 | 0 | function ); |
1259 | |
|
1260 | 0 | return( -1 ); |
1261 | 0 | } |
1262 | 0 | if( symbol_stack == NULL ) |
1263 | 0 | { |
1264 | 0 | libcerror_error_set( |
1265 | 0 | error, |
1266 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1267 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1268 | 0 | "%s: invalid symbol stack.", |
1269 | 0 | function ); |
1270 | |
|
1271 | 0 | return( -1 ); |
1272 | 0 | } |
1273 | 0 | if( block_data == NULL ) |
1274 | 0 | { |
1275 | 0 | libcerror_error_set( |
1276 | 0 | error, |
1277 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1278 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1279 | 0 | "%s: invalid block data.", |
1280 | 0 | function ); |
1281 | |
|
1282 | 0 | return( -1 ); |
1283 | 0 | } |
1284 | 0 | if( block_data_size == NULL ) |
1285 | 0 | { |
1286 | 0 | libcerror_error_set( |
1287 | 0 | error, |
1288 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1289 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1290 | 0 | "%s: invalid block data size.", |
1291 | 0 | function ); |
1292 | |
|
1293 | 0 | return( -1 ); |
1294 | 0 | } |
1295 | 0 | if( *block_data_size > (size_t) SSIZE_MAX ) |
1296 | 0 | { |
1297 | 0 | libcerror_error_set( |
1298 | 0 | error, |
1299 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1300 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1301 | 0 | "%s: invalid block data size value exceeds maximum.", |
1302 | 0 | function ); |
1303 | |
|
1304 | 0 | return( -1 ); |
1305 | 0 | } |
1306 | 0 | safe_block_data_size = *block_data_size; |
1307 | |
|
1308 | 0 | tree_index = selectors[ 0 ]; |
1309 | |
|
1310 | 0 | if( tree_index > number_of_trees ) |
1311 | 0 | { |
1312 | 0 | libcerror_error_set( |
1313 | 0 | error, |
1314 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1315 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1316 | 0 | "%s: invalid tree index value out of bounds.", |
1317 | 0 | function ); |
1318 | |
|
1319 | 0 | return( -1 ); |
1320 | 0 | } |
1321 | 0 | end_of_block_symbol = number_of_symbols - 1; |
1322 | |
|
1323 | 0 | do |
1324 | 0 | { |
1325 | 0 | if( libmodi_huffman_tree_get_symbol_from_bit_stream( |
1326 | 0 | huffman_trees[ tree_index ], |
1327 | 0 | bit_stream, |
1328 | 0 | &symbol, |
1329 | 0 | error ) != 1 ) |
1330 | 0 | { |
1331 | 0 | libcerror_error_set( |
1332 | 0 | error, |
1333 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1334 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
1335 | 0 | "%s: unable to retrieve symbol from Huffman tree: %" PRIu8 ".", |
1336 | 0 | function, |
1337 | 0 | tree_index ); |
1338 | |
|
1339 | 0 | return( -1 ); |
1340 | 0 | } |
1341 | 0 | if( ( number_of_run_length_symbols != 0 ) |
1342 | 0 | && ( symbol > 1 ) ) |
1343 | 0 | { |
1344 | 0 | run_length = ( ( (uint64_t) 1 << number_of_run_length_symbols ) | run_length_value ) - 1; |
1345 | |
|
1346 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1347 | | if( libcnotify_verbose != 0 ) |
1348 | | { |
1349 | | libcnotify_printf( |
1350 | | "%s: 0-byte run-length\t\t\t: %" PRIu64 "\n", |
1351 | | function, |
1352 | | run_length ); |
1353 | | } |
1354 | | #endif |
1355 | 0 | if( ( run_length > safe_block_data_size ) |
1356 | 0 | || ( block_data_offset > ( safe_block_data_size - run_length ) ) ) |
1357 | 0 | { |
1358 | 0 | libcerror_error_set( |
1359 | 0 | error, |
1360 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1361 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1362 | 0 | "%s: invalid run length value out of bounds.", |
1363 | 0 | function ); |
1364 | |
|
1365 | 0 | return( -1 ); |
1366 | 0 | } |
1367 | 0 | run_length_value = 0; |
1368 | 0 | number_of_run_length_symbols = 0; |
1369 | |
|
1370 | 0 | while( run_length > 0 ) |
1371 | 0 | { |
1372 | | /* Inverse move-to-front transform |
1373 | | * Note that 0 is already at the front of the stack hence the stack does not need to be reordered. |
1374 | | */ |
1375 | 0 | block_data[ block_data_offset++ ] = symbol_stack[ 0 ]; |
1376 | |
|
1377 | 0 | run_length--; |
1378 | 0 | } |
1379 | 0 | } |
1380 | 0 | if( symbol > end_of_block_symbol ) |
1381 | 0 | { |
1382 | 0 | libcerror_error_set( |
1383 | 0 | error, |
1384 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1385 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1386 | 0 | "%s: invalid symbol value out of bounds.", |
1387 | 0 | function ); |
1388 | |
|
1389 | 0 | return( -1 ); |
1390 | 0 | } |
1391 | 0 | if( ( symbol == 0 ) |
1392 | 0 | || ( symbol == 1 ) ) |
1393 | 0 | { |
1394 | 0 | run_length_value |= (uint64_t) symbol << number_of_run_length_symbols; |
1395 | 0 | number_of_run_length_symbols += 1; |
1396 | |
|
1397 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1398 | | if( libcnotify_verbose != 0 ) |
1399 | | { |
1400 | | libcnotify_printf( |
1401 | | "%s: symbol\t\t\t\t\t: %" PRIu16 " (run-length)\n", |
1402 | | function, |
1403 | | symbol ); |
1404 | | } |
1405 | | #endif |
1406 | 0 | } |
1407 | 0 | else if( symbol < end_of_block_symbol ) |
1408 | 0 | { |
1409 | | /* Inverse move-to-front transform |
1410 | | */ |
1411 | 0 | stack_value_index = symbol - 1; |
1412 | 0 | stack_value = symbol_stack[ stack_value_index ]; |
1413 | |
|
1414 | 0 | for( stack_index = stack_value_index; |
1415 | 0 | stack_index > 0; |
1416 | 0 | stack_index-- ) |
1417 | 0 | { |
1418 | 0 | symbol_stack[ stack_index ] = symbol_stack[ stack_index - 1 ]; |
1419 | 0 | } |
1420 | 0 | symbol_stack[ 0 ] = stack_value; |
1421 | |
|
1422 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1423 | | if( libcnotify_verbose != 0 ) |
1424 | | { |
1425 | | libcnotify_printf( |
1426 | | "%s: symbol\t\t\t\t\t: %" PRIu16 " (MTF: %" PRIu8 ")\n", |
1427 | | function, |
1428 | | symbol, |
1429 | | stack_value ); |
1430 | | } |
1431 | | #endif |
1432 | 0 | if( block_data_offset > ( safe_block_data_size - 1 ) ) |
1433 | 0 | { |
1434 | 0 | libcerror_error_set( |
1435 | 0 | error, |
1436 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1437 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1438 | 0 | "%s: invalid block data index value out of bounds.", |
1439 | 0 | function ); |
1440 | |
|
1441 | 0 | return( -1 ); |
1442 | 0 | } |
1443 | 0 | block_data[ block_data_offset++ ] = stack_value; |
1444 | 0 | } |
1445 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1446 | | else if( libcnotify_verbose != 0 ) |
1447 | | { |
1448 | | libcnotify_printf( |
1449 | | "%s: symbol\t\t\t\t\t: %" PRIu16 "\n", |
1450 | | function, |
1451 | | symbol ); |
1452 | | } |
1453 | | #endif |
1454 | 0 | symbol_index++; |
1455 | |
|
1456 | 0 | if( ( symbol_index % 50 ) == 0 ) |
1457 | 0 | { |
1458 | 0 | selector_index = symbol_index / 50; |
1459 | |
|
1460 | 0 | if( selector_index > number_of_selectors ) |
1461 | 0 | { |
1462 | 0 | libcerror_error_set( |
1463 | 0 | error, |
1464 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1465 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1466 | 0 | "%s: invalid selector index value out of bounds.", |
1467 | 0 | function ); |
1468 | |
|
1469 | 0 | return( -1 ); |
1470 | 0 | } |
1471 | 0 | tree_index = selectors[ selector_index ]; |
1472 | |
|
1473 | 0 | if( tree_index > number_of_trees ) |
1474 | 0 | { |
1475 | 0 | libcerror_error_set( |
1476 | 0 | error, |
1477 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1478 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1479 | 0 | "%s: invalid tree index value out of bounds.", |
1480 | 0 | function ); |
1481 | |
|
1482 | 0 | return( -1 ); |
1483 | 0 | } |
1484 | 0 | } |
1485 | 0 | } |
1486 | 0 | while( symbol != end_of_block_symbol ); |
1487 | | |
1488 | 0 | *block_data_size = block_data_offset; |
1489 | |
|
1490 | 0 | return( 1 ); |
1491 | 0 | } |
1492 | | |
1493 | | /* Reads a stream foorter |
1494 | | * Returns 1 on success or -1 on error |
1495 | | */ |
1496 | | int libmodi_bzip_read_stream_footer( |
1497 | | libmodi_bit_stream_t *bit_stream, |
1498 | | uint64_t signature, |
1499 | | uint32_t *checksum, |
1500 | | libcerror_error_t **error ) |
1501 | 0 | { |
1502 | 0 | static char *function = "libmodi_bzip_read_stream_footer"; |
1503 | 0 | uint32_t safe_checksum = 0; |
1504 | |
|
1505 | 0 | if( checksum == NULL ) |
1506 | 0 | { |
1507 | 0 | libcerror_error_set( |
1508 | 0 | error, |
1509 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1510 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1511 | 0 | "%s: invalid checksum.", |
1512 | 0 | function ); |
1513 | |
|
1514 | 0 | return( -1 ); |
1515 | 0 | } |
1516 | 0 | if( libmodi_bit_stream_get_value( |
1517 | 0 | bit_stream, |
1518 | 0 | 32, |
1519 | 0 | &safe_checksum, |
1520 | 0 | error ) != 1 ) |
1521 | 0 | { |
1522 | 0 | libcerror_error_set( |
1523 | 0 | error, |
1524 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1525 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
1526 | 0 | "%s: unable to retrieve value from bit stream.", |
1527 | 0 | function ); |
1528 | |
|
1529 | 0 | return( -1 ); |
1530 | 0 | } |
1531 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1532 | | if( libcnotify_verbose != 0 ) |
1533 | | { |
1534 | | libcnotify_printf( |
1535 | | "%s: signature\t\t\t\t\t: 0x%08" PRIx64 "\n", |
1536 | | function, |
1537 | | signature ); |
1538 | | |
1539 | | libcnotify_printf( |
1540 | | "%s: checksum\t\t\t\t\t: 0x%08" PRIx32 "\n", |
1541 | | function, |
1542 | | safe_checksum ); |
1543 | | |
1544 | | libcnotify_printf( |
1545 | | "\n" ); |
1546 | | } |
1547 | | #endif |
1548 | 0 | if( signature != 0x177245385090UL ) |
1549 | 0 | { |
1550 | 0 | libcerror_error_set( |
1551 | 0 | error, |
1552 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1553 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
1554 | 0 | "%s: unsupported signature.", |
1555 | 0 | function ); |
1556 | |
|
1557 | 0 | return( -1 ); |
1558 | 0 | } |
1559 | 0 | *checksum = safe_checksum; |
1560 | |
|
1561 | 0 | return( 1 ); |
1562 | 0 | } |
1563 | | |
1564 | | /* Decompresses data using BZIP2 compression |
1565 | | * Returns 1 on success or -1 on error |
1566 | | */ |
1567 | | int libmodi_bzip_decompress( |
1568 | | const uint8_t *compressed_data, |
1569 | | size_t compressed_data_size, |
1570 | | uint8_t *uncompressed_data, |
1571 | | size_t *uncompressed_data_size, |
1572 | | libcerror_error_t **error ) |
1573 | 0 | { |
1574 | 0 | uint8_t symbol_stack[ 256 ]; |
1575 | 0 | uint8_t selectors[ ( 1 << 15 ) + 1 ]; |
1576 | |
|
1577 | 0 | libmodi_huffman_tree_t *huffman_trees[ 7 ] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL }; |
1578 | |
|
1579 | 0 | libmodi_bit_stream_t *bit_stream = NULL; |
1580 | 0 | size_t *permutations = NULL; |
1581 | 0 | uint8_t *block_data = NULL; |
1582 | 0 | static char *function = "libmodi_bzip_decompress"; |
1583 | 0 | size_t block_data_size = 0; |
1584 | 0 | size_t compressed_data_offset = 0; |
1585 | 0 | size_t safe_block_data_size = 0; |
1586 | 0 | size_t safe_uncompressed_data_size = 0; |
1587 | 0 | size_t uncompressed_data_offset = 0; |
1588 | 0 | uint64_t signature = 0; |
1589 | 0 | uint32_t calculated_checksum = 0; |
1590 | 0 | uint32_t origin_pointer = 0; |
1591 | 0 | uint32_t stored_checksum = 0; |
1592 | 0 | uint32_t value_32bit = 0; |
1593 | 0 | uint16_t number_of_selectors = 0; |
1594 | 0 | uint16_t number_of_symbols = 0; |
1595 | 0 | uint8_t compression_level = 0; |
1596 | 0 | uint8_t number_of_trees = 0; |
1597 | 0 | uint8_t tree_index = 0; |
1598 | |
|
1599 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1600 | | size_t block_data_offset = 0; |
1601 | | #endif |
1602 | |
|
1603 | 0 | if( compressed_data == NULL ) |
1604 | 0 | { |
1605 | 0 | libcerror_error_set( |
1606 | 0 | error, |
1607 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1608 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1609 | 0 | "%s: invalid compressed data.", |
1610 | 0 | function ); |
1611 | |
|
1612 | 0 | return( -1 ); |
1613 | 0 | } |
1614 | 0 | if( ( compressed_data_size < 14 ) |
1615 | 0 | || ( compressed_data_size > (size_t) SSIZE_MAX ) ) |
1616 | 0 | { |
1617 | 0 | libcerror_error_set( |
1618 | 0 | error, |
1619 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1620 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
1621 | 0 | "%s: invalid compressed data size value out of bounds.", |
1622 | 0 | function ); |
1623 | |
|
1624 | 0 | return( -1 ); |
1625 | 0 | } |
1626 | 0 | if( uncompressed_data_size == NULL ) |
1627 | 0 | { |
1628 | 0 | libcerror_error_set( |
1629 | 0 | error, |
1630 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1631 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1632 | 0 | "%s: invalid uncompressed data size.", |
1633 | 0 | function ); |
1634 | |
|
1635 | 0 | return( -1 ); |
1636 | 0 | } |
1637 | 0 | safe_uncompressed_data_size = *uncompressed_data_size; |
1638 | |
|
1639 | 0 | if( libmodi_bzip_read_stream_header( |
1640 | 0 | compressed_data, |
1641 | 0 | compressed_data_size, |
1642 | 0 | &compressed_data_offset, |
1643 | 0 | &compression_level, |
1644 | 0 | error ) != 1 ) |
1645 | 0 | { |
1646 | 0 | libcerror_error_set( |
1647 | 0 | error, |
1648 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
1649 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
1650 | 0 | "%s: unable to read stream header.", |
1651 | 0 | function ); |
1652 | |
|
1653 | 0 | goto on_error; |
1654 | 0 | } |
1655 | 0 | if( compressed_data_offset > ( compressed_data_size - 10 ) ) |
1656 | 0 | { |
1657 | 0 | libcerror_error_set( |
1658 | 0 | error, |
1659 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1660 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
1661 | 0 | "%s: invalid compressed data value too small.", |
1662 | 0 | function ); |
1663 | |
|
1664 | 0 | goto on_error; |
1665 | 0 | } |
1666 | 0 | block_data_size = 100000; |
1667 | |
|
1668 | 0 | block_data = (uint8_t *) memory_allocate( |
1669 | 0 | sizeof( uint8_t ) * block_data_size ); |
1670 | |
|
1671 | 0 | if( block_data == NULL ) |
1672 | 0 | { |
1673 | 0 | libcerror_error_set( |
1674 | 0 | error, |
1675 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1676 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
1677 | 0 | "%s: unable to create block data.", |
1678 | 0 | function ); |
1679 | |
|
1680 | 0 | goto on_error; |
1681 | 0 | } |
1682 | 0 | permutations = (size_t *) memory_allocate( |
1683 | 0 | sizeof( size_t ) * block_data_size ); |
1684 | |
|
1685 | 0 | if( permutations == NULL ) |
1686 | 0 | { |
1687 | 0 | libcerror_error_set( |
1688 | 0 | error, |
1689 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1690 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
1691 | 0 | "%s: unable to create permutations.", |
1692 | 0 | function ); |
1693 | |
|
1694 | 0 | goto on_error; |
1695 | 0 | } |
1696 | 0 | if( libmodi_bit_stream_initialize( |
1697 | 0 | &bit_stream, |
1698 | 0 | compressed_data, |
1699 | 0 | compressed_data_size, |
1700 | 0 | compressed_data_offset, |
1701 | 0 | LIBMODI_BIT_STREAM_STORAGE_TYPE_BYTE_FRONT_TO_BACK, |
1702 | 0 | error ) != 1 ) |
1703 | 0 | { |
1704 | 0 | libcerror_error_set( |
1705 | 0 | error, |
1706 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1707 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
1708 | 0 | "%s: unable to create bit stream.", |
1709 | 0 | function ); |
1710 | |
|
1711 | 0 | goto on_error; |
1712 | 0 | } |
1713 | 0 | while( bit_stream->byte_stream_offset < bit_stream->byte_stream_size ) |
1714 | 0 | { |
1715 | 0 | if( libmodi_bzip_read_signature( |
1716 | 0 | bit_stream, |
1717 | 0 | &signature, |
1718 | 0 | error ) != 1 ) |
1719 | 0 | { |
1720 | 0 | libcerror_error_set( |
1721 | 0 | error, |
1722 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
1723 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
1724 | 0 | "%s: unable to read signature.", |
1725 | 0 | function ); |
1726 | |
|
1727 | 0 | goto on_error; |
1728 | 0 | } |
1729 | 0 | if( signature == 0x177245385090UL ) |
1730 | 0 | { |
1731 | 0 | break; |
1732 | 0 | } |
1733 | 0 | if( signature != 0x314159265359UL ) |
1734 | 0 | { |
1735 | 0 | libcerror_error_set( |
1736 | 0 | error, |
1737 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1738 | 0 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
1739 | 0 | "%s: unsupported signature: 0x%" PRIx64 ".", |
1740 | 0 | function, |
1741 | 0 | signature ); |
1742 | |
|
1743 | 0 | goto on_error; |
1744 | 0 | } |
1745 | 0 | if( libmodi_bzip_read_block_header( |
1746 | 0 | bit_stream, |
1747 | 0 | signature, |
1748 | 0 | &origin_pointer, |
1749 | 0 | error ) != 1 ) |
1750 | 0 | { |
1751 | 0 | libcerror_error_set( |
1752 | 0 | error, |
1753 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
1754 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
1755 | 0 | "%s: unable to read block header.", |
1756 | 0 | function ); |
1757 | |
|
1758 | 0 | goto on_error; |
1759 | 0 | } |
1760 | 0 | if( origin_pointer > block_data_size ) |
1761 | 0 | { |
1762 | 0 | libcerror_error_set( |
1763 | 0 | error, |
1764 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1765 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1766 | 0 | "%s: invalid origin pointer value out of bounds.", |
1767 | 0 | function ); |
1768 | |
|
1769 | 0 | goto on_error; |
1770 | 0 | } |
1771 | 0 | if( memory_set( |
1772 | 0 | symbol_stack, |
1773 | 0 | 0, |
1774 | 0 | 256 ) == NULL ) |
1775 | 0 | { |
1776 | 0 | libcerror_error_set( |
1777 | 0 | error, |
1778 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1779 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
1780 | 0 | "%s: unable to clear symbol stack.", |
1781 | 0 | function ); |
1782 | |
|
1783 | 0 | goto on_error; |
1784 | 0 | } |
1785 | 0 | if( libmodi_bzip_read_symbol_stack( |
1786 | 0 | bit_stream, |
1787 | 0 | symbol_stack, |
1788 | 0 | &number_of_symbols, |
1789 | 0 | error ) != 1 ) |
1790 | 0 | { |
1791 | 0 | libcerror_error_set( |
1792 | 0 | error, |
1793 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
1794 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
1795 | 0 | "%s: unable to read symbol stack.", |
1796 | 0 | function ); |
1797 | |
|
1798 | 0 | goto on_error; |
1799 | 0 | } |
1800 | 0 | if( libmodi_bit_stream_get_value( |
1801 | 0 | bit_stream, |
1802 | 0 | 3, |
1803 | 0 | &value_32bit, |
1804 | 0 | error ) != 1 ) |
1805 | 0 | { |
1806 | 0 | libcerror_error_set( |
1807 | 0 | error, |
1808 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1809 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
1810 | 0 | "%s: unable to retrieve value from bit stream.", |
1811 | 0 | function ); |
1812 | |
|
1813 | 0 | goto on_error; |
1814 | 0 | } |
1815 | 0 | number_of_trees = (uint8_t) ( value_32bit & 0x00000007UL ); |
1816 | |
|
1817 | 0 | if( libmodi_bit_stream_get_value( |
1818 | 0 | bit_stream, |
1819 | 0 | 15, |
1820 | 0 | &value_32bit, |
1821 | 0 | error ) != 1 ) |
1822 | 0 | { |
1823 | 0 | libcerror_error_set( |
1824 | 0 | error, |
1825 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1826 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
1827 | 0 | "%s: unable to retrieve value from bit stream.", |
1828 | 0 | function ); |
1829 | |
|
1830 | 0 | goto on_error; |
1831 | 0 | } |
1832 | 0 | number_of_selectors = (uint16_t) ( value_32bit & 0x00007fffUL ); |
1833 | |
|
1834 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1835 | | if( libcnotify_verbose != 0 ) |
1836 | | { |
1837 | | libcnotify_printf( |
1838 | | "%s: number of trees\t\t\t\t: %" PRIu8 "\n", |
1839 | | function, |
1840 | | number_of_trees ); |
1841 | | |
1842 | | libcnotify_printf( |
1843 | | "%s: number of selectors\t\t\t\t: %" PRIu16 "\n", |
1844 | | function, |
1845 | | number_of_selectors ); |
1846 | | |
1847 | | libcnotify_printf( |
1848 | | "\n" ); |
1849 | | } |
1850 | | #endif |
1851 | 0 | if( libmodi_bzip_read_selectors( |
1852 | 0 | bit_stream, |
1853 | 0 | selectors, |
1854 | 0 | number_of_trees, |
1855 | 0 | number_of_selectors, |
1856 | 0 | error ) != 1 ) |
1857 | 0 | { |
1858 | 0 | libcerror_error_set( |
1859 | 0 | error, |
1860 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
1861 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
1862 | 0 | "%s: unable to read selectors.", |
1863 | 0 | function ); |
1864 | |
|
1865 | 0 | goto on_error; |
1866 | 0 | } |
1867 | 0 | if( libmodi_bzip_read_huffman_trees( |
1868 | 0 | bit_stream, |
1869 | 0 | huffman_trees, |
1870 | 0 | number_of_trees, |
1871 | 0 | number_of_symbols, |
1872 | 0 | error ) != 1 ) |
1873 | 0 | { |
1874 | 0 | libcerror_error_set( |
1875 | 0 | error, |
1876 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
1877 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
1878 | 0 | "%s: unable to read Huffman trees.", |
1879 | 0 | function ); |
1880 | |
|
1881 | 0 | goto on_error; |
1882 | 0 | } |
1883 | 0 | safe_block_data_size = block_data_size; |
1884 | |
|
1885 | 0 | if( libmodi_bzip_read_block_data( |
1886 | 0 | bit_stream, |
1887 | 0 | huffman_trees, |
1888 | 0 | number_of_trees, |
1889 | 0 | selectors, |
1890 | 0 | number_of_selectors, |
1891 | 0 | symbol_stack, |
1892 | 0 | number_of_symbols, |
1893 | 0 | block_data, |
1894 | 0 | &safe_block_data_size, |
1895 | 0 | error ) != 1 ) |
1896 | 0 | { |
1897 | 0 | libcerror_error_set( |
1898 | 0 | error, |
1899 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
1900 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
1901 | 0 | "%s: unable to read block data.", |
1902 | 0 | function ); |
1903 | |
|
1904 | 0 | goto on_error; |
1905 | 0 | } |
1906 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1907 | | block_data_offset = uncompressed_data_offset; |
1908 | | #endif |
1909 | 0 | if( memory_set( |
1910 | 0 | permutations, |
1911 | 0 | 0, |
1912 | 0 | sizeof( size_t ) * block_data_size ) == NULL ) |
1913 | 0 | { |
1914 | 0 | libcerror_error_set( |
1915 | 0 | error, |
1916 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1917 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
1918 | 0 | "%s: unable to clear permutations.", |
1919 | 0 | function ); |
1920 | |
|
1921 | 0 | goto on_error; |
1922 | 0 | } |
1923 | | /* Perform Burrows-Wheeler transform |
1924 | | */ |
1925 | 0 | if( libmodi_bzip_reverse_burrows_wheeler_transform( |
1926 | 0 | block_data, |
1927 | 0 | safe_block_data_size, |
1928 | 0 | permutations, |
1929 | 0 | origin_pointer, |
1930 | 0 | uncompressed_data, |
1931 | 0 | safe_uncompressed_data_size, |
1932 | 0 | &uncompressed_data_offset, |
1933 | 0 | error ) != 1 ) |
1934 | 0 | { |
1935 | 0 | libcerror_error_set( |
1936 | 0 | error, |
1937 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1938 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
1939 | 0 | "%s: unable to reverse Burrows-Wheeler transform.", |
1940 | 0 | function ); |
1941 | |
|
1942 | 0 | goto on_error; |
1943 | 0 | } |
1944 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1945 | | if( libcnotify_verbose != 0 ) |
1946 | | { |
1947 | | libcnotify_printf( |
1948 | | "%s: block data:\n", |
1949 | | function ); |
1950 | | libcnotify_print_data( |
1951 | | &( uncompressed_data[ block_data_offset ] ), |
1952 | | uncompressed_data_offset - block_data_offset, |
1953 | | 0 ); |
1954 | | } |
1955 | | #endif |
1956 | 0 | for( tree_index = 0; |
1957 | 0 | tree_index < number_of_trees; |
1958 | 0 | tree_index++ ) |
1959 | 0 | { |
1960 | 0 | if( libmodi_huffman_tree_free( |
1961 | 0 | &( huffman_trees[ tree_index ] ), |
1962 | 0 | error ) != 1 ) |
1963 | 0 | { |
1964 | 0 | libcerror_error_set( |
1965 | 0 | error, |
1966 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1967 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
1968 | 0 | "%s: unable to free Huffman tree: %" PRIu8 ".", |
1969 | 0 | function, |
1970 | 0 | tree_index ); |
1971 | |
|
1972 | 0 | goto on_error; |
1973 | 0 | } |
1974 | 0 | } |
1975 | 0 | } |
1976 | 0 | if( libmodi_bzip_read_stream_footer( |
1977 | 0 | bit_stream, |
1978 | 0 | signature, |
1979 | 0 | &stored_checksum, |
1980 | 0 | error ) != 1 ) |
1981 | 0 | { |
1982 | 0 | libcerror_error_set( |
1983 | 0 | error, |
1984 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
1985 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
1986 | 0 | "%s: unable to read stream footer.", |
1987 | 0 | function ); |
1988 | |
|
1989 | 0 | goto on_error; |
1990 | 0 | } |
1991 | 0 | if( libmodi_bit_stream_free( |
1992 | 0 | &bit_stream, |
1993 | 0 | error ) != 1 ) |
1994 | 0 | { |
1995 | 0 | libcerror_error_set( |
1996 | 0 | error, |
1997 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1998 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
1999 | 0 | "%s: unable to free bit stream.", |
2000 | 0 | function ); |
2001 | |
|
2002 | 0 | goto on_error; |
2003 | 0 | } |
2004 | 0 | memory_free( |
2005 | 0 | permutations ); |
2006 | |
|
2007 | 0 | permutations = NULL; |
2008 | |
|
2009 | 0 | memory_free( |
2010 | 0 | block_data ); |
2011 | |
|
2012 | 0 | block_data = NULL; |
2013 | |
|
2014 | 0 | if( libmodi_bzip_calculate_crc32( |
2015 | 0 | &calculated_checksum, |
2016 | 0 | uncompressed_data, |
2017 | 0 | uncompressed_data_offset, |
2018 | 0 | 0, |
2019 | 0 | error ) != 1 ) |
2020 | 0 | { |
2021 | 0 | libcerror_error_set( |
2022 | 0 | error, |
2023 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
2024 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
2025 | 0 | "%s: unable to calculate checksum.", |
2026 | 0 | function ); |
2027 | |
|
2028 | 0 | goto on_error; |
2029 | 0 | } |
2030 | | #if defined( HAVE_DEBUG_OUTPUT ) |
2031 | | if( libcnotify_verbose != 0 ) |
2032 | | { |
2033 | | libcnotify_printf( |
2034 | | "%s: stored checksum\t\t\t\t\t: 0x%08" PRIx32 "\n", |
2035 | | function, |
2036 | | stored_checksum ); |
2037 | | |
2038 | | libcnotify_printf( |
2039 | | "%s: calculated checksum\t\t\t\t\t: 0x%08" PRIx32 "\n", |
2040 | | function, |
2041 | | calculated_checksum ); |
2042 | | } |
2043 | | #endif |
2044 | 0 | if( stored_checksum != calculated_checksum ) |
2045 | 0 | { |
2046 | 0 | libcerror_error_set( |
2047 | 0 | error, |
2048 | 0 | LIBCERROR_ERROR_DOMAIN_INPUT, |
2049 | 0 | LIBCERROR_INPUT_ERROR_CHECKSUM_MISMATCH, |
2050 | 0 | "%s: checksum does not match (stored: 0x%08" PRIx32 ", calculated: 0x%08" PRIx32 ").", |
2051 | 0 | function, |
2052 | 0 | stored_checksum, |
2053 | 0 | calculated_checksum ); |
2054 | |
|
2055 | 0 | goto on_error; |
2056 | 0 | } |
2057 | 0 | *uncompressed_data_size = uncompressed_data_offset; |
2058 | |
|
2059 | 0 | return( 1 ); |
2060 | | |
2061 | 0 | on_error: |
2062 | 0 | for( tree_index = 0; |
2063 | 0 | tree_index < number_of_trees; |
2064 | 0 | tree_index++ ) |
2065 | 0 | { |
2066 | 0 | libmodi_huffman_tree_free( |
2067 | 0 | &( huffman_trees[ tree_index ] ), |
2068 | 0 | NULL ); |
2069 | 0 | } |
2070 | 0 | if( bit_stream != NULL ) |
2071 | 0 | { |
2072 | 0 | libmodi_bit_stream_free( |
2073 | 0 | &bit_stream, |
2074 | 0 | NULL ); |
2075 | 0 | } |
2076 | 0 | if( permutations != NULL ) |
2077 | 0 | { |
2078 | 0 | memory_free( |
2079 | 0 | permutations ); |
2080 | 0 | } |
2081 | 0 | if( block_data != NULL ) |
2082 | 0 | { |
2083 | 0 | memory_free( |
2084 | 0 | block_data ); |
2085 | 0 | } |
2086 | 0 | return( -1 ); |
2087 | 0 | } |
2088 | | |