/src/libfshfs/libfshfs/libfshfs_compressed_data_handle.c
Line | Count | Source |
1 | | /* |
2 | | * The compressed data handle functions |
3 | | * |
4 | | * Copyright (C) 2009-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 <byte_stream.h> |
24 | | #include <memory.h> |
25 | | #include <types.h> |
26 | | |
27 | | #include "libfshfs_compressed_data_handle.h" |
28 | | #include "libfshfs_compression.h" |
29 | | #include "libfshfs_definitions.h" |
30 | | #include "libfshfs_libbfio.h" |
31 | | #include "libfshfs_libcerror.h" |
32 | | #include "libfshfs_libcnotify.h" |
33 | | #include "libfshfs_unused.h" |
34 | | |
35 | 0 | #define LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE 65536 |
36 | | |
37 | | /* Creates compressed data handle |
38 | | * Make sure the value data_handle is referencing, is set to NULL |
39 | | * Returns 1 if successful or -1 on error |
40 | | */ |
41 | | int libfshfs_compressed_data_handle_initialize( |
42 | | libfshfs_compressed_data_handle_t **data_handle, |
43 | | libfdata_stream_t *compressed_data_stream, |
44 | | size64_t uncompressed_data_size, |
45 | | int compression_method, |
46 | | libcerror_error_t **error ) |
47 | 0 | { |
48 | 0 | static char *function = "libfshfs_compressed_data_handle_initialize"; |
49 | |
|
50 | 0 | if( data_handle == NULL ) |
51 | 0 | { |
52 | 0 | libcerror_error_set( |
53 | 0 | error, |
54 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
55 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
56 | 0 | "%s: invalid data handle.", |
57 | 0 | function ); |
58 | |
|
59 | 0 | return( -1 ); |
60 | 0 | } |
61 | 0 | if( *data_handle != NULL ) |
62 | 0 | { |
63 | 0 | libcerror_error_set( |
64 | 0 | error, |
65 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
66 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
67 | 0 | "%s: invalid data handle value already set.", |
68 | 0 | function ); |
69 | |
|
70 | 0 | return( -1 ); |
71 | 0 | } |
72 | 0 | if( compressed_data_stream == NULL ) |
73 | 0 | { |
74 | 0 | libcerror_error_set( |
75 | 0 | error, |
76 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
77 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
78 | 0 | "%s: invalid compressed data stream.", |
79 | 0 | function ); |
80 | |
|
81 | 0 | return( -1 ); |
82 | 0 | } |
83 | 0 | if( uncompressed_data_size > (size64_t) INT64_MAX ) |
84 | 0 | { |
85 | 0 | libcerror_error_set( |
86 | 0 | error, |
87 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
88 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
89 | 0 | "%s: invalid uncompressed data size value exceeds maximum.", |
90 | 0 | function ); |
91 | |
|
92 | 0 | return( -1 ); |
93 | 0 | } |
94 | 0 | if( ( compression_method != LIBFSHFS_COMPRESSION_METHOD_DEFLATE ) |
95 | 0 | && ( compression_method != LIBFSHFS_COMPRESSION_METHOD_LZFSE ) |
96 | 0 | && ( compression_method != LIBFSHFS_COMPRESSION_METHOD_LZVN ) |
97 | 0 | && ( compression_method != LIBFSHFS_COMPRESSION_METHOD_RAW ) |
98 | 0 | && ( compression_method != LIBFSHFS_COMPRESSION_METHOD_UNKNOWN5 ) ) |
99 | 0 | { |
100 | 0 | libcerror_error_set( |
101 | 0 | error, |
102 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
103 | 0 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
104 | 0 | "%s: unsupported compression method.", |
105 | 0 | function ); |
106 | |
|
107 | 0 | return( -1 ); |
108 | 0 | } |
109 | 0 | *data_handle = memory_allocate_structure( |
110 | 0 | libfshfs_compressed_data_handle_t ); |
111 | |
|
112 | 0 | if( *data_handle == NULL ) |
113 | 0 | { |
114 | 0 | libcerror_error_set( |
115 | 0 | error, |
116 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
117 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
118 | 0 | "%s: unable to create data handle.", |
119 | 0 | function ); |
120 | |
|
121 | 0 | goto on_error; |
122 | 0 | } |
123 | 0 | if( memory_set( |
124 | 0 | *data_handle, |
125 | 0 | 0, |
126 | 0 | sizeof( libfshfs_compressed_data_handle_t ) ) == NULL ) |
127 | 0 | { |
128 | 0 | libcerror_error_set( |
129 | 0 | error, |
130 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
131 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
132 | 0 | "%s: unable to clear data handle.", |
133 | 0 | function ); |
134 | |
|
135 | 0 | memory_free( |
136 | 0 | *data_handle ); |
137 | |
|
138 | 0 | *data_handle = NULL; |
139 | |
|
140 | 0 | return( -1 ); |
141 | 0 | } |
142 | 0 | ( *data_handle )->compressed_segment_data = (uint8_t *) memory_allocate( |
143 | 0 | sizeof( uint8_t ) * ( LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE + 1 ) ); |
144 | |
|
145 | 0 | if( ( *data_handle )->compressed_segment_data == NULL ) |
146 | 0 | { |
147 | 0 | libcerror_error_set( |
148 | 0 | error, |
149 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
150 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
151 | 0 | "%s: unable to create compressed segment data.", |
152 | 0 | function ); |
153 | |
|
154 | 0 | goto on_error; |
155 | 0 | } |
156 | 0 | if( compression_method != LIBFSHFS_COMPRESSION_METHOD_UNKNOWN5 ) |
157 | 0 | { |
158 | 0 | ( *data_handle )->segment_data = (uint8_t *) memory_allocate( |
159 | 0 | sizeof( uint8_t ) * LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE ); |
160 | |
|
161 | 0 | if( ( *data_handle )->segment_data == NULL ) |
162 | 0 | { |
163 | 0 | libcerror_error_set( |
164 | 0 | error, |
165 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
166 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
167 | 0 | "%s: unable to create segment data.", |
168 | 0 | function ); |
169 | |
|
170 | 0 | goto on_error; |
171 | 0 | } |
172 | 0 | } |
173 | 0 | ( *data_handle )->compressed_data_stream = compressed_data_stream; |
174 | 0 | ( *data_handle )->current_compressed_block_index = (uint32_t) -1; |
175 | 0 | ( *data_handle )->uncompressed_data_size = uncompressed_data_size; |
176 | 0 | ( *data_handle )->compression_method = compression_method; |
177 | |
|
178 | 0 | return( 1 ); |
179 | | |
180 | 0 | on_error: |
181 | 0 | if( *data_handle != NULL ) |
182 | 0 | { |
183 | 0 | if( ( *data_handle )->segment_data != NULL ) |
184 | 0 | { |
185 | 0 | memory_free( |
186 | 0 | ( *data_handle )->segment_data ); |
187 | 0 | } |
188 | 0 | if( ( *data_handle )->compressed_segment_data != NULL ) |
189 | 0 | { |
190 | 0 | memory_free( |
191 | 0 | ( *data_handle )->compressed_segment_data ); |
192 | 0 | } |
193 | 0 | memory_free( |
194 | 0 | *data_handle ); |
195 | |
|
196 | 0 | *data_handle = NULL; |
197 | 0 | } |
198 | 0 | return( -1 ); |
199 | 0 | } |
200 | | |
201 | | /* Frees data handle |
202 | | * Returns 1 if successful or -1 on error |
203 | | */ |
204 | | int libfshfs_compressed_data_handle_free( |
205 | | libfshfs_compressed_data_handle_t **data_handle, |
206 | | libcerror_error_t **error ) |
207 | 0 | { |
208 | 0 | static char *function = "libfshfs_compressed_data_handle_free"; |
209 | 0 | int result = 1; |
210 | |
|
211 | 0 | if( data_handle == NULL ) |
212 | 0 | { |
213 | 0 | libcerror_error_set( |
214 | 0 | error, |
215 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
216 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
217 | 0 | "%s: invalid data handle.", |
218 | 0 | function ); |
219 | |
|
220 | 0 | return( -1 ); |
221 | 0 | } |
222 | 0 | if( *data_handle != NULL ) |
223 | 0 | { |
224 | 0 | if( ( *data_handle )->segment_data != NULL ) |
225 | 0 | { |
226 | 0 | memory_free( |
227 | 0 | ( *data_handle )->segment_data ); |
228 | 0 | } |
229 | 0 | if( ( *data_handle )->compressed_segment_data != NULL ) |
230 | 0 | { |
231 | 0 | memory_free( |
232 | 0 | ( *data_handle )->compressed_segment_data ); |
233 | 0 | } |
234 | 0 | if( ( *data_handle )->compressed_block_offsets != NULL ) |
235 | 0 | { |
236 | 0 | memory_free( |
237 | 0 | ( *data_handle )->compressed_block_offsets ); |
238 | 0 | } |
239 | 0 | if( libfdata_stream_free( |
240 | 0 | &( ( *data_handle )->compressed_data_stream ), |
241 | 0 | error ) != 1 ) |
242 | 0 | { |
243 | 0 | libcerror_error_set( |
244 | 0 | error, |
245 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
246 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
247 | 0 | "%s: unable to free compressed data stream.", |
248 | 0 | function ); |
249 | |
|
250 | 0 | result = -1; |
251 | 0 | } |
252 | 0 | memory_free( |
253 | 0 | *data_handle ); |
254 | |
|
255 | 0 | *data_handle = NULL; |
256 | 0 | } |
257 | 0 | return( result ); |
258 | 0 | } |
259 | | |
260 | | /* Determines the compressed block offsets |
261 | | * Returns 1 if successful or -1 on error |
262 | | */ |
263 | | int libfshfs_compressed_data_handle_get_compressed_block_offsets( |
264 | | libfshfs_compressed_data_handle_t *data_handle, |
265 | | libbfio_handle_t *file_io_handle, |
266 | | libcerror_error_t **error ) |
267 | 0 | { |
268 | 0 | static char *function = "libfshfs_compressed_data_handle_get_compressed_block_offsets"; |
269 | 0 | size64_t compressed_data_size = 0; |
270 | 0 | size_t compressed_block_descriptor_size = 0; |
271 | 0 | size_t read_size = 0; |
272 | 0 | size_t segment_data_offset = 0; |
273 | 0 | ssize_t read_count = 0; |
274 | 0 | uint32_t compressed_block_index = 0; |
275 | 0 | uint32_t compressed_block_offset = 0; |
276 | 0 | uint32_t compressed_descriptors_offset = 0; |
277 | 0 | uint32_t compressed_footer_offset = 0; |
278 | 0 | uint32_t compressed_footer_size = 0; |
279 | 0 | uint32_t maximum_number_of_compressed_blocks = 0; |
280 | 0 | uint32_t previous_compressed_block_offset = 0; |
281 | 0 | int compare_result = 0; |
282 | |
|
283 | | #if defined( HAVE_DEBUG_OUTPUT ) |
284 | | uint32_t value_32bit = 0; |
285 | | #endif |
286 | |
|
287 | 0 | if( data_handle == NULL ) |
288 | 0 | { |
289 | 0 | libcerror_error_set( |
290 | 0 | error, |
291 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
292 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
293 | 0 | "%s: invalid data handle.", |
294 | 0 | function ); |
295 | |
|
296 | 0 | return( -1 ); |
297 | 0 | } |
298 | 0 | if( data_handle->compressed_block_offsets != NULL ) |
299 | 0 | { |
300 | 0 | libcerror_error_set( |
301 | 0 | error, |
302 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
303 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
304 | 0 | "%s: invalid data handle - compressed block offsets value already set.", |
305 | 0 | function ); |
306 | |
|
307 | 0 | return( -1 ); |
308 | 0 | } |
309 | 0 | if( libfdata_stream_get_size( |
310 | 0 | data_handle->compressed_data_stream, |
311 | 0 | &compressed_data_size, |
312 | 0 | error ) != 1 ) |
313 | 0 | { |
314 | 0 | libcerror_error_set( |
315 | 0 | error, |
316 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
317 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
318 | 0 | "%s: unable to retrieve compressed data size.", |
319 | 0 | function ); |
320 | |
|
321 | 0 | goto on_error; |
322 | 0 | } |
323 | 0 | read_count = libfdata_stream_read_buffer_at_offset( |
324 | 0 | data_handle->compressed_data_stream, |
325 | 0 | (intptr_t *) file_io_handle, |
326 | 0 | data_handle->compressed_segment_data, |
327 | 0 | 4, |
328 | 0 | 0, |
329 | 0 | 0, |
330 | 0 | error ); |
331 | |
|
332 | 0 | if( read_count != 4 ) |
333 | 0 | { |
334 | 0 | libcerror_error_set( |
335 | 0 | error, |
336 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
337 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
338 | 0 | "%s: unable to read buffer at offset: 0 (0x00000000) from data stream.", |
339 | 0 | function ); |
340 | |
|
341 | 0 | goto on_error; |
342 | 0 | } |
343 | 0 | compare_result = memory_compare( |
344 | 0 | data_handle->compressed_segment_data, |
345 | 0 | "fpmc", |
346 | 0 | 4 ); |
347 | |
|
348 | 0 | if( compare_result == 0 ) |
349 | 0 | { |
350 | 0 | if( compressed_data_size > (size64_t) ( LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE + 1 ) ) |
351 | 0 | { |
352 | 0 | libcerror_error_set( |
353 | 0 | error, |
354 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
355 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
356 | 0 | "%s: invalid segment data size value out of bounds.", |
357 | 0 | function ); |
358 | |
|
359 | 0 | goto on_error; |
360 | 0 | } |
361 | 0 | data_handle->number_of_compressed_blocks = 1; |
362 | 0 | } |
363 | 0 | else if( data_handle->compression_method == LIBFSHFS_COMPRESSION_METHOD_DEFLATE ) |
364 | 0 | { |
365 | 0 | byte_stream_copy_to_uint32_big_endian( |
366 | 0 | data_handle->compressed_segment_data, |
367 | 0 | compressed_descriptors_offset ); |
368 | |
|
369 | 0 | if( compressed_descriptors_offset != 0x00000100UL ) |
370 | 0 | { |
371 | 0 | libcerror_error_set( |
372 | 0 | error, |
373 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
374 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
375 | 0 | "%s: invalid compressed descriptors offset value out of bounds.", |
376 | 0 | function ); |
377 | |
|
378 | 0 | goto on_error; |
379 | 0 | } |
380 | 0 | read_size = (size_t) compressed_descriptors_offset + 16 - 4; |
381 | |
|
382 | 0 | read_count = libfdata_stream_read_buffer_at_offset( |
383 | 0 | data_handle->compressed_data_stream, |
384 | 0 | (intptr_t *) file_io_handle, |
385 | 0 | &( data_handle->compressed_segment_data[ 4 ] ), |
386 | 0 | read_size, |
387 | 0 | 4, |
388 | 0 | 0, |
389 | 0 | error ); |
390 | |
|
391 | 0 | if( read_count != (ssize_t) read_size ) |
392 | 0 | { |
393 | 0 | libcerror_error_set( |
394 | 0 | error, |
395 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
396 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
397 | 0 | "%s: unable to read compressed header data at offset: 4 (0x00000004) from data stream.", |
398 | 0 | function ); |
399 | |
|
400 | 0 | goto on_error; |
401 | 0 | } |
402 | | #if defined( HAVE_DEBUG_OUTPUT ) |
403 | | if( libcnotify_verbose != 0 ) |
404 | | { |
405 | | libcnotify_printf( |
406 | | "%s: compressed header data:\n", |
407 | | function ); |
408 | | libcnotify_print_data( |
409 | | data_handle->compressed_segment_data, |
410 | | read_size - 4, |
411 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
412 | | } |
413 | | #endif |
414 | | #if defined( HAVE_DEBUG_OUTPUT ) |
415 | | if( libcnotify_verbose != 0 ) |
416 | | { |
417 | | libcnotify_printf( |
418 | | "%s: compressed descriptors offset\t: 0x%08" PRIx32 "\n", |
419 | | function, |
420 | | compressed_descriptors_offset ); |
421 | | |
422 | | byte_stream_copy_to_uint32_big_endian( |
423 | | &( data_handle->compressed_segment_data[ 4 ] ), |
424 | | compressed_footer_offset ); |
425 | | libcnotify_printf( |
426 | | "%s: compressed footer offset\t\t: 0x%08" PRIx32 "\n", |
427 | | function, |
428 | | compressed_footer_offset ); |
429 | | |
430 | | byte_stream_copy_to_uint32_big_endian( |
431 | | &( data_handle->compressed_segment_data[ 8 ] ), |
432 | | value_32bit ); |
433 | | libcnotify_printf( |
434 | | "%s: compressed data size\t\t: %" PRIu32 "\n", |
435 | | function, |
436 | | value_32bit ); |
437 | | |
438 | | byte_stream_copy_to_uint32_big_endian( |
439 | | &( data_handle->compressed_segment_data[ 12 ] ), |
440 | | compressed_footer_size ); |
441 | | libcnotify_printf( |
442 | | "%s: compressed footer size\t\t: %" PRIu32 "\n", |
443 | | function, |
444 | | compressed_footer_size ); |
445 | | |
446 | | libcnotify_printf( |
447 | | "%s: unknown1:\n", |
448 | | function ); |
449 | | libcnotify_print_data( |
450 | | &( data_handle->compressed_segment_data[ 16 ] ), |
451 | | 240, |
452 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
453 | | |
454 | | byte_stream_copy_to_uint32_big_endian( |
455 | | &( data_handle->compressed_segment_data[ 256 ] ), |
456 | | value_32bit ); |
457 | | libcnotify_printf( |
458 | | "%s: compressed data size\t\t: %" PRIu32 "\n", |
459 | | function, |
460 | | value_32bit ); |
461 | | } |
462 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
463 | | |
464 | 0 | byte_stream_copy_to_uint32_little_endian( |
465 | 0 | &( data_handle->compressed_segment_data[ 260 ] ), |
466 | 0 | data_handle->number_of_compressed_blocks ); |
467 | |
|
468 | 0 | if( ( data_handle->number_of_compressed_blocks == 0 ) |
469 | 0 | || ( data_handle->number_of_compressed_blocks > ( (uint32_t) UINT32_MAX / 8 ) ) ) |
470 | 0 | { |
471 | 0 | libcerror_error_set( |
472 | 0 | error, |
473 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
474 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
475 | 0 | "%s: invalid number of compressed blocks value out of bounds.", |
476 | 0 | function ); |
477 | |
|
478 | 0 | goto on_error; |
479 | 0 | } |
480 | 0 | segment_data_offset = 264; |
481 | |
|
482 | 0 | byte_stream_copy_to_uint32_little_endian( |
483 | 0 | &( data_handle->compressed_segment_data[ segment_data_offset ] ), |
484 | 0 | compressed_block_offset ); |
485 | |
|
486 | 0 | segment_data_offset += 4; |
487 | |
|
488 | 0 | compressed_descriptors_offset += 4; |
489 | 0 | compressed_block_descriptor_size = 8; |
490 | 0 | } |
491 | 0 | else if( ( data_handle->compression_method == LIBFSHFS_COMPRESSION_METHOD_LZFSE ) |
492 | 0 | || ( data_handle->compression_method == LIBFSHFS_COMPRESSION_METHOD_LZVN ) |
493 | 0 | || ( data_handle->compression_method == LIBFSHFS_COMPRESSION_METHOD_RAW ) ) |
494 | 0 | { |
495 | 0 | segment_data_offset = 0; |
496 | |
|
497 | 0 | byte_stream_copy_to_uint32_little_endian( |
498 | 0 | &( data_handle->compressed_segment_data[ segment_data_offset ] ), |
499 | 0 | compressed_block_offset ); |
500 | |
|
501 | 0 | segment_data_offset += 4; |
502 | |
|
503 | 0 | compressed_block_descriptor_size = 4; |
504 | |
|
505 | 0 | if( ( compressed_block_offset <= 0x00000004UL ) |
506 | 0 | || ( compressed_block_offset >= ( LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE + 1 ) ) ) |
507 | 0 | { |
508 | 0 | libcerror_error_set( |
509 | 0 | error, |
510 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
511 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
512 | 0 | "%s: invalid number of compressed blocks value out of bounds.", |
513 | 0 | function ); |
514 | |
|
515 | 0 | goto on_error; |
516 | 0 | } |
517 | 0 | data_handle->number_of_compressed_blocks = compressed_block_offset / 4; |
518 | 0 | } |
519 | | #if defined( HAVE_DEBUG_OUTPUT ) |
520 | | if( libcnotify_verbose != 0 ) |
521 | | { |
522 | | libcnotify_printf( |
523 | | "%s: number of compressed blocks\t: %" PRIu32 "\n", |
524 | | function, |
525 | | data_handle->number_of_compressed_blocks ); |
526 | | } |
527 | | #endif |
528 | 0 | maximum_number_of_compressed_blocks = MEMORY_MAXIMUM_ALLOCATION_SIZE; |
529 | |
|
530 | 0 | if( compressed_block_descriptor_size != 0 ) |
531 | 0 | { |
532 | 0 | maximum_number_of_compressed_blocks /= compressed_block_descriptor_size; |
533 | 0 | } |
534 | 0 | if( data_handle->number_of_compressed_blocks > ( maximum_number_of_compressed_blocks - 1 ) ) |
535 | 0 | { |
536 | 0 | libcerror_error_set( |
537 | 0 | error, |
538 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
539 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
540 | 0 | "%s: invalid number of compressed blocks value exceeds maximum allocation size.", |
541 | 0 | function ); |
542 | |
|
543 | 0 | goto on_error; |
544 | 0 | } |
545 | 0 | data_handle->compressed_block_offsets = (uint32_t *) memory_allocate( |
546 | 0 | sizeof( uint32_t ) * ( data_handle->number_of_compressed_blocks + 1 ) ); |
547 | |
|
548 | 0 | if( data_handle->compressed_block_offsets == NULL ) |
549 | 0 | { |
550 | 0 | libcerror_error_set( |
551 | 0 | error, |
552 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
553 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
554 | 0 | "%s: unable to create compressed block offsets.", |
555 | 0 | function ); |
556 | |
|
557 | 0 | goto on_error; |
558 | 0 | } |
559 | 0 | if( compare_result == 0 ) |
560 | 0 | { |
561 | 0 | data_handle->compressed_block_offsets[ compressed_block_index++ ] = 16; |
562 | 0 | previous_compressed_block_offset = 16; |
563 | 0 | } |
564 | 0 | else |
565 | 0 | { |
566 | 0 | read_size = ( (size_t) data_handle->number_of_compressed_blocks - 1 ) * compressed_block_descriptor_size; |
567 | |
|
568 | 0 | if( read_size > ( LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE + 1 ) - segment_data_offset ) |
569 | 0 | { |
570 | 0 | libcerror_error_set( |
571 | 0 | error, |
572 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
573 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
574 | 0 | "%s: invalid compressed block descriptors data size value out of bounds.", |
575 | 0 | function ); |
576 | |
|
577 | 0 | goto on_error; |
578 | 0 | } |
579 | 0 | read_count = libfdata_stream_read_buffer_at_offset( |
580 | 0 | data_handle->compressed_data_stream, |
581 | 0 | (intptr_t *) file_io_handle, |
582 | 0 | &( data_handle->compressed_segment_data[ segment_data_offset ] ), |
583 | 0 | read_size, |
584 | 0 | segment_data_offset, |
585 | 0 | 0, |
586 | 0 | error ); |
587 | |
|
588 | 0 | if( read_count != (ssize_t) read_size ) |
589 | 0 | { |
590 | 0 | libcerror_error_set( |
591 | 0 | error, |
592 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
593 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
594 | 0 | "%s: unable to read compressed block descriptors data at offset: %" PRIzd " (0x%08" PRIzx ") from data stream.", |
595 | 0 | function, |
596 | 0 | segment_data_offset, |
597 | 0 | segment_data_offset ); |
598 | |
|
599 | 0 | goto on_error; |
600 | 0 | } |
601 | | #if defined( HAVE_DEBUG_OUTPUT ) |
602 | | if( libcnotify_verbose != 0 ) |
603 | | { |
604 | | libcnotify_printf( |
605 | | "%s: compressed block descriptors data:\n", |
606 | | function ); |
607 | | libcnotify_print_data( |
608 | | &( data_handle->compressed_segment_data[ segment_data_offset - compressed_block_descriptor_size ] ), |
609 | | read_size + compressed_block_descriptor_size, |
610 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
611 | | } |
612 | | #endif |
613 | | #if defined( HAVE_DEBUG_OUTPUT ) |
614 | | if( libcnotify_verbose != 0 ) |
615 | | { |
616 | | libcnotify_printf( |
617 | | "%s: compressed block: % 2" PRIu32 " offset\t: 0x%08" PRIx32 " (0x%08" PRIx32 ")\n", |
618 | | function, |
619 | | compressed_block_index, |
620 | | compressed_block_offset, |
621 | | compressed_block_offset + compressed_descriptors_offset ); |
622 | | } |
623 | | #endif |
624 | 0 | if( ( compressed_block_offset <= compressed_block_descriptor_size ) |
625 | 0 | || ( compressed_block_offset >= ( LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE + 1 ) ) ) |
626 | 0 | { |
627 | 0 | libcerror_error_set( |
628 | 0 | error, |
629 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
630 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
631 | 0 | "%s: invalid compressed block offset: %" PRIu32 " (0x%08" PRIx32 ") value out of bounds.", |
632 | 0 | function, |
633 | 0 | compressed_block_offset, |
634 | 0 | compressed_block_offset ); |
635 | |
|
636 | 0 | goto on_error; |
637 | 0 | } |
638 | 0 | compressed_block_offset += compressed_descriptors_offset; |
639 | |
|
640 | 0 | data_handle->compressed_block_offsets[ 0 ] = compressed_block_offset; |
641 | 0 | previous_compressed_block_offset = compressed_block_offset; |
642 | |
|
643 | 0 | if( data_handle->compression_method == LIBFSHFS_COMPRESSION_METHOD_DEFLATE ) |
644 | 0 | { |
645 | | #if defined( HAVE_DEBUG_OUTPUT ) |
646 | | if( libcnotify_verbose != 0 ) |
647 | | { |
648 | | byte_stream_copy_to_uint32_little_endian( |
649 | | &( data_handle->compressed_segment_data[ segment_data_offset ] ), |
650 | | value_32bit ); |
651 | | libcnotify_printf( |
652 | | "%s: compressed block: % 2" PRIu32 " size\t: %" PRIu32 "\n", |
653 | | function, |
654 | | compressed_block_index, |
655 | | value_32bit ); |
656 | | } |
657 | | #endif |
658 | 0 | segment_data_offset += 4; |
659 | 0 | } |
660 | 0 | for( compressed_block_index = 1; |
661 | 0 | compressed_block_index < data_handle->number_of_compressed_blocks; |
662 | 0 | compressed_block_index++ ) |
663 | 0 | { |
664 | 0 | byte_stream_copy_to_uint32_little_endian( |
665 | 0 | &( data_handle->compressed_segment_data[ segment_data_offset ] ), |
666 | 0 | compressed_block_offset ); |
667 | |
|
668 | | #if defined( HAVE_DEBUG_OUTPUT ) |
669 | | if( libcnotify_verbose != 0 ) |
670 | | { |
671 | | libcnotify_printf( |
672 | | "%s: compressed block: % 2" PRIu32 " offset\t: 0x%08" PRIx32 " (0x%08" PRIx32 ")\n", |
673 | | function, |
674 | | compressed_block_index, |
675 | | compressed_block_offset, |
676 | | compressed_block_offset + compressed_descriptors_offset ); |
677 | | } |
678 | | #endif |
679 | 0 | segment_data_offset += 4; |
680 | |
|
681 | 0 | compressed_block_offset += compressed_descriptors_offset; |
682 | |
|
683 | 0 | if( ( previous_compressed_block_offset > compressed_block_offset ) |
684 | 0 | || ( ( compressed_block_offset - previous_compressed_block_offset ) > (uint32_t) ( LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE + 1 ) ) ) |
685 | 0 | { |
686 | 0 | libcerror_error_set( |
687 | 0 | error, |
688 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
689 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
690 | 0 | "%s: invalid compressed block offset: %" PRIu32 " (0x%08" PRIx32 ") value out of bounds.", |
691 | 0 | function, |
692 | 0 | compressed_block_offset, |
693 | 0 | compressed_block_offset ); |
694 | |
|
695 | 0 | goto on_error; |
696 | 0 | } |
697 | 0 | data_handle->compressed_block_offsets[ compressed_block_index ] = compressed_block_offset; |
698 | 0 | previous_compressed_block_offset = compressed_block_offset; |
699 | |
|
700 | 0 | if( data_handle->compression_method == LIBFSHFS_COMPRESSION_METHOD_DEFLATE ) |
701 | 0 | { |
702 | | #if defined( HAVE_DEBUG_OUTPUT ) |
703 | | if( libcnotify_verbose != 0 ) |
704 | | { |
705 | | byte_stream_copy_to_uint32_little_endian( |
706 | | &( data_handle->compressed_segment_data[ segment_data_offset ] ), |
707 | | value_32bit ); |
708 | | libcnotify_printf( |
709 | | "%s: compressed block: % 2" PRIu32 " size\t: %" PRIu32 "\n", |
710 | | function, |
711 | | compressed_block_index, |
712 | | value_32bit ); |
713 | | } |
714 | | #endif |
715 | 0 | segment_data_offset += 4; |
716 | 0 | } |
717 | 0 | } |
718 | 0 | } |
719 | 0 | if( ( previous_compressed_block_offset > compressed_data_size ) |
720 | 0 | || ( ( compressed_data_size - previous_compressed_block_offset ) > (uint32_t) ( LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE + 1 ) ) ) |
721 | 0 | { |
722 | 0 | libcerror_error_set( |
723 | 0 | error, |
724 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
725 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
726 | 0 | "%s: invalid compressed block offset: %" PRIu32 " (0x%08" PRIx32 ") value out of bounds.", |
727 | 0 | function, |
728 | 0 | previous_compressed_block_offset, |
729 | 0 | previous_compressed_block_offset ); |
730 | |
|
731 | 0 | goto on_error; |
732 | 0 | } |
733 | 0 | data_handle->compressed_block_offsets[ compressed_block_index ] = (uint32_t) compressed_data_size; |
734 | |
|
735 | | #if defined( HAVE_DEBUG_OUTPUT ) |
736 | | if( libcnotify_verbose != 0 ) |
737 | | { |
738 | | libcnotify_printf( |
739 | | "\n" ); |
740 | | } |
741 | | #endif |
742 | 0 | if( data_handle->compression_method == LIBFSHFS_COMPRESSION_METHOD_DEFLATE ) |
743 | 0 | { |
744 | 0 | if( compressed_footer_size > (uint32_t) ( LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE + 1 ) ) |
745 | 0 | { |
746 | 0 | libcerror_error_set( |
747 | 0 | error, |
748 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
749 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
750 | 0 | "%s: invalid compressed footer size value out of bounds.", |
751 | 0 | function ); |
752 | |
|
753 | 0 | goto on_error; |
754 | 0 | } |
755 | 0 | read_count = libfdata_stream_read_buffer_at_offset( |
756 | 0 | data_handle->compressed_data_stream, |
757 | 0 | (intptr_t *) file_io_handle, |
758 | 0 | data_handle->compressed_segment_data, |
759 | 0 | (size_t) compressed_footer_size, |
760 | 0 | (off64_t) compressed_footer_offset, |
761 | 0 | 0, |
762 | 0 | error ); |
763 | |
|
764 | 0 | if( read_count != (ssize_t) compressed_footer_size ) |
765 | 0 | { |
766 | 0 | libcerror_error_set( |
767 | 0 | error, |
768 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
769 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
770 | 0 | "%s: unable to read compressed footer data at offset: %" PRIu32 " (0x08%" PRIx32 ") from data stream.", |
771 | 0 | function, |
772 | 0 | compressed_footer_offset, |
773 | 0 | compressed_footer_offset ); |
774 | |
|
775 | 0 | goto on_error; |
776 | 0 | } |
777 | | #if defined( HAVE_DEBUG_OUTPUT ) |
778 | | if( libcnotify_verbose != 0 ) |
779 | | { |
780 | | libcnotify_printf( |
781 | | "%s: compressed footer data:\n", |
782 | | function ); |
783 | | libcnotify_print_data( |
784 | | data_handle->compressed_segment_data, |
785 | | (size_t) compressed_footer_size, |
786 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
787 | | } |
788 | | #endif |
789 | 0 | } |
790 | 0 | else if( data_handle->compression_method == LIBFSHFS_COMPRESSION_METHOD_UNKNOWN5 ) |
791 | 0 | { |
792 | 0 | if( compressed_data_size > (size64_t) SSIZE_MAX ) |
793 | 0 | { |
794 | 0 | libcerror_error_set( |
795 | 0 | error, |
796 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
797 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
798 | 0 | "%s: invalid compressed data size value out of bounds.", |
799 | 0 | function ); |
800 | |
|
801 | 0 | goto on_error; |
802 | 0 | } |
803 | 0 | read_size = (size_t) compressed_data_size - 16; |
804 | |
|
805 | 0 | read_count = libfdata_stream_read_buffer_at_offset( |
806 | 0 | data_handle->compressed_data_stream, |
807 | 0 | (intptr_t *) file_io_handle, |
808 | 0 | data_handle->compressed_segment_data, |
809 | 0 | read_size, |
810 | 0 | 16, |
811 | 0 | 0, |
812 | 0 | error ); |
813 | |
|
814 | 0 | if( read_count != (ssize_t) read_size ) |
815 | 0 | { |
816 | 0 | libcerror_error_set( |
817 | 0 | error, |
818 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
819 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
820 | 0 | "%s: unable to read unknown data at offset: 16 (0x00000010) from data stream.", |
821 | 0 | function ); |
822 | |
|
823 | 0 | goto on_error; |
824 | 0 | } |
825 | | #if defined( HAVE_DEBUG_OUTPUT ) |
826 | | if( libcnotify_verbose != 0 ) |
827 | | { |
828 | | libcnotify_printf( |
829 | | "%s: unknown data:\n", |
830 | | function ); |
831 | | libcnotify_print_data( |
832 | | data_handle->compressed_segment_data, |
833 | | read_size, |
834 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
835 | | } |
836 | | #endif |
837 | 0 | } |
838 | 0 | return( 1 ); |
839 | | |
840 | 0 | on_error: |
841 | 0 | if( data_handle->compressed_block_offsets != NULL ) |
842 | 0 | { |
843 | 0 | memory_free( |
844 | 0 | data_handle->compressed_block_offsets ); |
845 | |
|
846 | 0 | data_handle->compressed_block_offsets = NULL; |
847 | 0 | } |
848 | 0 | return( -1 ); |
849 | 0 | } |
850 | | |
851 | | /* Reads data from the current offset into a compressed |
852 | | * Callback for the data stream |
853 | | * Returns the number of bytes read or -1 on error |
854 | | */ |
855 | | ssize_t libfshfs_compressed_data_handle_read_segment_data( |
856 | | libfshfs_compressed_data_handle_t *data_handle, |
857 | | intptr_t *file_io_handle LIBFSHFS_ATTRIBUTE_UNUSED, |
858 | | int segment_index, |
859 | | int segment_file_index LIBFSHFS_ATTRIBUTE_UNUSED, |
860 | | uint8_t *segment_data, |
861 | | size_t segment_data_size, |
862 | | uint32_t segment_flags LIBFSHFS_ATTRIBUTE_UNUSED, |
863 | | uint8_t read_flags LIBFSHFS_ATTRIBUTE_UNUSED, |
864 | | libcerror_error_t **error ) |
865 | 0 | { |
866 | 0 | static char *function = "libfshfs_compressed_data_handle_read_segment_data"; |
867 | 0 | size_t data_offset = 0; |
868 | 0 | size_t read_size = 0; |
869 | 0 | size_t segment_data_offset = 0; |
870 | 0 | ssize_t read_count = 0; |
871 | 0 | off64_t data_stream_offset = 0; |
872 | 0 | off64_t uncompressed_block_offset = 0; |
873 | 0 | uint32_t compressed_block_index = 0; |
874 | |
|
875 | 0 | LIBFSHFS_UNREFERENCED_PARAMETER( file_io_handle ) |
876 | 0 | LIBFSHFS_UNREFERENCED_PARAMETER( segment_file_index ) |
877 | 0 | LIBFSHFS_UNREFERENCED_PARAMETER( segment_flags ) |
878 | 0 | LIBFSHFS_UNREFERENCED_PARAMETER( read_flags ) |
879 | |
|
880 | 0 | if( data_handle == NULL ) |
881 | 0 | { |
882 | 0 | libcerror_error_set( |
883 | 0 | error, |
884 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
885 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
886 | 0 | "%s: invalid data handle.", |
887 | 0 | function ); |
888 | |
|
889 | 0 | return( -1 ); |
890 | 0 | } |
891 | 0 | if( segment_index != 0 ) |
892 | 0 | { |
893 | 0 | libcerror_error_set( |
894 | 0 | error, |
895 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
896 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
897 | 0 | "%s: invalid segment index value out of bounds.", |
898 | 0 | function ); |
899 | |
|
900 | 0 | return( -1 ); |
901 | 0 | } |
902 | 0 | if( segment_data == NULL ) |
903 | 0 | { |
904 | 0 | libcerror_error_set( |
905 | 0 | error, |
906 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
907 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
908 | 0 | "%s: invalid segment data.", |
909 | 0 | function ); |
910 | |
|
911 | 0 | return( -1 ); |
912 | 0 | } |
913 | 0 | if( segment_data_size > (size_t) SSIZE_MAX ) |
914 | 0 | { |
915 | 0 | libcerror_error_set( |
916 | 0 | error, |
917 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
918 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
919 | 0 | "%s: invalid segment data size value exceeds maximum.", |
920 | 0 | function ); |
921 | |
|
922 | 0 | return( -1 ); |
923 | 0 | } |
924 | 0 | if( data_handle->compressed_block_offsets == NULL ) |
925 | 0 | { |
926 | 0 | if( libfshfs_compressed_data_handle_get_compressed_block_offsets( |
927 | 0 | data_handle, |
928 | 0 | (libbfio_handle_t *) file_io_handle, |
929 | 0 | error ) != 1 ) |
930 | 0 | { |
931 | 0 | libcerror_error_set( |
932 | 0 | error, |
933 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
934 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
935 | 0 | "%s: unable to determine compressed block offsets.", |
936 | 0 | function ); |
937 | |
|
938 | 0 | return( -1 ); |
939 | 0 | } |
940 | 0 | } |
941 | 0 | if( (size64_t) data_handle->current_segment_offset >= data_handle->uncompressed_data_size ) |
942 | 0 | { |
943 | 0 | return( 0 ); |
944 | 0 | } |
945 | 0 | if( data_handle->compression_method == LIBFSHFS_COMPRESSION_METHOD_UNKNOWN5 ) |
946 | 0 | { |
947 | 0 | if( (size64_t) segment_data_size > ( data_handle->uncompressed_data_size - data_handle->current_segment_offset ) ) |
948 | 0 | { |
949 | 0 | read_size = (size_t) ( data_handle->uncompressed_data_size - data_handle->current_segment_offset ); |
950 | 0 | } |
951 | 0 | else |
952 | 0 | { |
953 | 0 | read_size = segment_data_size; |
954 | 0 | } |
955 | 0 | if( memory_set( |
956 | 0 | segment_data, |
957 | 0 | 0, |
958 | 0 | read_size ) == NULL ) |
959 | 0 | { |
960 | 0 | libcerror_error_set( |
961 | 0 | error, |
962 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
963 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
964 | 0 | "%s: unable to clear segment data.", |
965 | 0 | function ); |
966 | |
|
967 | 0 | return( -1 ); |
968 | 0 | } |
969 | 0 | data_handle->current_segment_offset += read_size; |
970 | |
|
971 | 0 | return( (ssize_t) read_size ); |
972 | 0 | } |
973 | 0 | compressed_block_index = data_handle->current_segment_offset / LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE; |
974 | 0 | segment_data_offset = 0; |
975 | 0 | data_offset = (size_t) ( data_handle->current_segment_offset % LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE ); |
976 | |
|
977 | 0 | while( segment_data_size > 0 ) |
978 | 0 | { |
979 | 0 | if( compressed_block_index >= data_handle->number_of_compressed_blocks ) |
980 | 0 | { |
981 | 0 | libcerror_error_set( |
982 | 0 | error, |
983 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
984 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
985 | 0 | "%s: invalid compressed block index value out of bounds.", |
986 | 0 | function ); |
987 | |
|
988 | 0 | return( -1 ); |
989 | 0 | } |
990 | 0 | if( data_handle->current_compressed_block_index != compressed_block_index ) |
991 | 0 | { |
992 | 0 | data_stream_offset = data_handle->compressed_block_offsets[ compressed_block_index ]; |
993 | 0 | read_size = (size_t) ( data_handle->compressed_block_offsets[ compressed_block_index + 1 ] - data_stream_offset ); |
994 | |
|
995 | 0 | read_count = libfdata_stream_read_buffer_at_offset( |
996 | 0 | data_handle->compressed_data_stream, |
997 | 0 | (intptr_t *) file_io_handle, |
998 | 0 | (uint8_t *) data_handle->compressed_segment_data, |
999 | 0 | read_size, |
1000 | 0 | data_stream_offset, |
1001 | 0 | 0, |
1002 | 0 | error ); |
1003 | |
|
1004 | 0 | if( read_count != (ssize_t) read_size ) |
1005 | 0 | { |
1006 | 0 | libcerror_error_set( |
1007 | 0 | error, |
1008 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
1009 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
1010 | 0 | "%s: unable to read buffer at offset: %" PRIi64 " (0x08%" PRIx64 ") from data stream.", |
1011 | 0 | function, |
1012 | 0 | data_stream_offset, |
1013 | 0 | data_stream_offset ); |
1014 | |
|
1015 | 0 | return( -1 ); |
1016 | 0 | } |
1017 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1018 | | if( libcnotify_verbose != 0 ) |
1019 | | { |
1020 | | libcnotify_printf( |
1021 | | "%s: compressed block data:\n", |
1022 | | function ); |
1023 | | libcnotify_print_data( |
1024 | | data_handle->compressed_segment_data, |
1025 | | read_size, |
1026 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
1027 | | } |
1028 | | #endif |
1029 | 0 | data_handle->segment_data_size = LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE; |
1030 | |
|
1031 | 0 | if( libfshfs_decompress_data( |
1032 | 0 | data_handle->compressed_segment_data, |
1033 | 0 | (size_t) read_count, |
1034 | 0 | data_handle->compression_method, |
1035 | 0 | data_handle->segment_data, |
1036 | 0 | &( data_handle->segment_data_size ), |
1037 | 0 | error ) != 1 ) |
1038 | 0 | { |
1039 | 0 | libcerror_error_set( |
1040 | 0 | error, |
1041 | 0 | LIBCERROR_ERROR_DOMAIN_ENCRYPTION, |
1042 | 0 | LIBCERROR_ENCRYPTION_ERROR_GENERIC, |
1043 | 0 | "%s: unable to decompress data.", |
1044 | 0 | function ); |
1045 | |
|
1046 | 0 | return( -1 ); |
1047 | 0 | } |
1048 | | #if defined( HAVE_DEBUG_OUTPUT ) |
1049 | | if( libcnotify_verbose != 0 ) |
1050 | | { |
1051 | | libcnotify_printf( |
1052 | | "%s: uncompressed block data:\n", |
1053 | | function ); |
1054 | | libcnotify_print_data( |
1055 | | data_handle->segment_data, |
1056 | | data_handle->segment_data_size, |
1057 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
1058 | | } |
1059 | | #endif |
1060 | 0 | uncompressed_block_offset = ( compressed_block_index + 1 ) * LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE; |
1061 | |
|
1062 | 0 | if( ( (size64_t) uncompressed_block_offset < data_handle->uncompressed_data_size ) |
1063 | 0 | && ( data_handle->segment_data_size != LIBFSHFS_COMPRESSED_DATA_HANDLE_BLOCK_SIZE ) ) |
1064 | 0 | { |
1065 | 0 | libcerror_error_set( |
1066 | 0 | error, |
1067 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1068 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1069 | 0 | "%s: invalid uncompressed segment data size value out of bounds.", |
1070 | 0 | function ); |
1071 | |
|
1072 | 0 | return( -1 ); |
1073 | 0 | } |
1074 | 0 | data_handle->current_compressed_block_index = compressed_block_index; |
1075 | 0 | } |
1076 | 0 | if( data_offset >= data_handle->segment_data_size ) |
1077 | 0 | { |
1078 | 0 | libcerror_error_set( |
1079 | 0 | error, |
1080 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1081 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
1082 | 0 | "%s: invalid data offset value out of bounds.", |
1083 | 0 | function ); |
1084 | |
|
1085 | 0 | return( -1 ); |
1086 | 0 | } |
1087 | 0 | read_size = data_handle->segment_data_size - data_offset; |
1088 | |
|
1089 | 0 | if( read_size > segment_data_size ) |
1090 | 0 | { |
1091 | 0 | read_size = segment_data_size; |
1092 | 0 | } |
1093 | 0 | if( memory_copy( |
1094 | 0 | &( segment_data[ segment_data_offset ] ), |
1095 | 0 | &( data_handle->segment_data[ data_offset ] ), |
1096 | 0 | read_size ) == NULL ) |
1097 | 0 | { |
1098 | 0 | libcerror_error_set( |
1099 | 0 | error, |
1100 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
1101 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
1102 | 0 | "%s: unable to copy data.", |
1103 | 0 | function ); |
1104 | |
|
1105 | 0 | return( -1 ); |
1106 | 0 | } |
1107 | 0 | data_offset = 0; |
1108 | 0 | segment_data_size -= read_size; |
1109 | 0 | segment_data_offset += read_size; |
1110 | |
|
1111 | 0 | compressed_block_index++; |
1112 | 0 | } |
1113 | 0 | data_handle->current_segment_offset += segment_data_offset; |
1114 | |
|
1115 | 0 | return( (ssize_t) segment_data_offset ); |
1116 | 0 | } |
1117 | | |
1118 | | /* Seeks a certain offset of the data |
1119 | | * Callback for the data stream |
1120 | | * Returns the offset if seek is successful or -1 on error |
1121 | | */ |
1122 | | off64_t libfshfs_compressed_data_handle_seek_segment_offset( |
1123 | | libfshfs_compressed_data_handle_t *data_handle, |
1124 | | intptr_t *file_io_handle LIBFSHFS_ATTRIBUTE_UNUSED, |
1125 | | int segment_index, |
1126 | | int segment_file_index LIBFSHFS_ATTRIBUTE_UNUSED, |
1127 | | off64_t segment_offset, |
1128 | | libcerror_error_t **error ) |
1129 | 0 | { |
1130 | 0 | static char *function = "libfshfs_compressed_data_handle_seek_segment_offset"; |
1131 | |
|
1132 | 0 | LIBFSHFS_UNREFERENCED_PARAMETER( file_io_handle ) |
1133 | 0 | LIBFSHFS_UNREFERENCED_PARAMETER( segment_file_index ) |
1134 | |
|
1135 | 0 | if( data_handle == NULL ) |
1136 | 0 | { |
1137 | 0 | libcerror_error_set( |
1138 | 0 | error, |
1139 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1140 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1141 | 0 | "%s: invalid data handle.", |
1142 | 0 | function ); |
1143 | |
|
1144 | 0 | return( -1 ); |
1145 | 0 | } |
1146 | 0 | if( segment_index != 0 ) |
1147 | 0 | { |
1148 | 0 | libcerror_error_set( |
1149 | 0 | error, |
1150 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1151 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
1152 | 0 | "%s: invalid segment index value out of bounds.", |
1153 | 0 | function ); |
1154 | |
|
1155 | 0 | return( -1 ); |
1156 | 0 | } |
1157 | 0 | if( segment_offset < 0 ) |
1158 | 0 | { |
1159 | 0 | libcerror_error_set( |
1160 | 0 | error, |
1161 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1162 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
1163 | 0 | "%s: invalid segment offset value out of bounds.", |
1164 | 0 | function ); |
1165 | |
|
1166 | 0 | return( -1 ); |
1167 | 0 | } |
1168 | 0 | data_handle->current_segment_offset = segment_offset; |
1169 | |
|
1170 | 0 | return( segment_offset ); |
1171 | 0 | } |
1172 | | |