/src/libfshfs/libfshfs/libfshfs_allocation_block_stream.c
Line | Count | Source |
1 | | /* |
2 | | * Allocation block stream functions |
3 | | * |
4 | | * Copyright (C) 2009-2025, Joachim Metz <joachim.metz@gmail.com> |
5 | | * |
6 | | * Refer to AUTHORS for acknowledgements. |
7 | | * |
8 | | * This program is free software: you can redistribute it and/or modify |
9 | | * it under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation, either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <common.h> |
23 | | #include <types.h> |
24 | | |
25 | | #include "libfshfs_allocation_block_stream.h" |
26 | | #include "libfshfs_block_data_handle.h" |
27 | | #include "libfshfs_buffer_data_handle.h" |
28 | | #include "libfshfs_compressed_data_handle.h" |
29 | | #include "libfshfs_compressed_data_header.h" |
30 | | #include "libfshfs_extent.h" |
31 | | #include "libfshfs_io_handle.h" |
32 | | #include "libfshfs_libcdata.h" |
33 | | #include "libfshfs_libfdata.h" |
34 | | |
35 | | /* Creates an allocation block stream from a buffer of data |
36 | | * Make sure the value allocation_block_stream is referencing, is set to NULL |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | int libfshfs_allocation_block_stream_initialize_from_data( |
40 | | libfdata_stream_t **allocation_block_stream, |
41 | | const uint8_t *data, |
42 | | size_t data_size, |
43 | | libcerror_error_t **error ) |
44 | 0 | { |
45 | 0 | libfdata_stream_t *safe_allocation_block_stream = NULL; |
46 | 0 | libfshfs_buffer_data_handle_t *data_handle = NULL; |
47 | 0 | static char *function = "libfshfs_allocation_block_stream_initialize_from_data"; |
48 | 0 | int segment_index = 0; |
49 | |
|
50 | 0 | if( allocation_block_stream == 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 allocation block stream.", |
57 | 0 | function ); |
58 | |
|
59 | 0 | return( -1 ); |
60 | 0 | } |
61 | 0 | if( libfshfs_buffer_data_handle_initialize( |
62 | 0 | &data_handle, |
63 | 0 | data, |
64 | 0 | data_size, |
65 | 0 | error ) != 1 ) |
66 | 0 | { |
67 | 0 | libcerror_error_set( |
68 | 0 | error, |
69 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
70 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
71 | 0 | "%s: unable to create buffer data handle.", |
72 | 0 | function ); |
73 | |
|
74 | 0 | goto on_error; |
75 | 0 | } |
76 | 0 | if( libfdata_stream_initialize( |
77 | 0 | &safe_allocation_block_stream, |
78 | 0 | (intptr_t *) data_handle, |
79 | 0 | (int (*)(intptr_t **, libcerror_error_t **)) &libfshfs_buffer_data_handle_free, |
80 | 0 | NULL, |
81 | 0 | NULL, |
82 | 0 | (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libfshfs_buffer_data_handle_read_segment_data, |
83 | 0 | NULL, |
84 | 0 | (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libfshfs_buffer_data_handle_seek_segment_offset, |
85 | 0 | LIBFDATA_DATA_HANDLE_FLAG_MANAGED, |
86 | 0 | error ) != 1 ) |
87 | 0 | { |
88 | 0 | libcerror_error_set( |
89 | 0 | error, |
90 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
91 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
92 | 0 | "%s: unable to create allocation block stream.", |
93 | 0 | function ); |
94 | |
|
95 | 0 | goto on_error; |
96 | 0 | } |
97 | 0 | data_handle = NULL; |
98 | |
|
99 | 0 | if( libfdata_stream_append_segment( |
100 | 0 | safe_allocation_block_stream, |
101 | 0 | &segment_index, |
102 | 0 | 0, |
103 | 0 | 0, |
104 | 0 | (size64_t) data_size, |
105 | 0 | 0, |
106 | 0 | error ) != 1 ) |
107 | 0 | { |
108 | 0 | libcerror_error_set( |
109 | 0 | error, |
110 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
111 | 0 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
112 | 0 | "%s: unable to append allocation block stream segment.", |
113 | 0 | function ); |
114 | |
|
115 | 0 | goto on_error; |
116 | 0 | } |
117 | 0 | *allocation_block_stream = safe_allocation_block_stream; |
118 | |
|
119 | 0 | return( 1 ); |
120 | | |
121 | 0 | on_error: |
122 | 0 | if( safe_allocation_block_stream != NULL ) |
123 | 0 | { |
124 | 0 | libfdata_stream_free( |
125 | 0 | &safe_allocation_block_stream, |
126 | 0 | NULL ); |
127 | 0 | } |
128 | 0 | if( data_handle != NULL ) |
129 | 0 | { |
130 | 0 | libfshfs_buffer_data_handle_free( |
131 | 0 | &data_handle, |
132 | 0 | NULL ); |
133 | 0 | } |
134 | 0 | return( -1 ); |
135 | 0 | } |
136 | | |
137 | | /* Creates an allocation block stream from a fork descriptor |
138 | | * Make sure the value allocation_block is referencing, is set to NULL |
139 | | * Returns 1 if successful or -1 on error |
140 | | */ |
141 | | int libfshfs_allocation_block_stream_initialize_from_fork_descriptor( |
142 | | libfdata_stream_t **allocation_block_stream, |
143 | | libfshfs_io_handle_t *io_handle, |
144 | | libfshfs_fork_descriptor_t *fork_descriptor, |
145 | | libcerror_error_t **error ) |
146 | 390 | { |
147 | 390 | libfdata_stream_t *safe_allocation_block_stream = NULL; |
148 | 390 | static char *function = "libfshfs_allocation_block_stream_initialize_from_fork_descriptor"; |
149 | 390 | size64_t segment_size = 0; |
150 | 390 | off64_t segment_offset = 0; |
151 | 390 | int extent_index = 0; |
152 | 390 | int result = 0; |
153 | 390 | int segment_index = 0; |
154 | | |
155 | 390 | if( allocation_block_stream == NULL ) |
156 | 0 | { |
157 | 0 | libcerror_error_set( |
158 | 0 | error, |
159 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
160 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
161 | 0 | "%s: invalid allocation block stream.", |
162 | 0 | function ); |
163 | |
|
164 | 0 | return( -1 ); |
165 | 0 | } |
166 | 390 | if( io_handle == NULL ) |
167 | 0 | { |
168 | 0 | libcerror_error_set( |
169 | 0 | error, |
170 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
171 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
172 | 0 | "%s: invalid IO handle.", |
173 | 0 | function ); |
174 | |
|
175 | 0 | return( -1 ); |
176 | 0 | } |
177 | 390 | if( io_handle->block_size == 0 ) |
178 | 0 | { |
179 | 0 | libcerror_error_set( |
180 | 0 | error, |
181 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
182 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
183 | 0 | "%s: invalid IO handle - block size value out of bounds.", |
184 | 0 | function ); |
185 | |
|
186 | 0 | return( -1 ); |
187 | 0 | } |
188 | 390 | if( fork_descriptor == NULL ) |
189 | 0 | { |
190 | 0 | libcerror_error_set( |
191 | 0 | error, |
192 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
193 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
194 | 0 | "%s: invalid fork descriptor.", |
195 | 0 | function ); |
196 | |
|
197 | 0 | return( -1 ); |
198 | 0 | } |
199 | 390 | result = libfshfs_fork_descriptor_has_extents_overflow( |
200 | 390 | fork_descriptor, |
201 | 390 | error ); |
202 | | |
203 | 390 | if( result == -1 ) |
204 | 0 | { |
205 | 0 | libcerror_error_set( |
206 | 0 | error, |
207 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
208 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
209 | 0 | "%s: unable to determine if fork descriptor has extents overflow.", |
210 | 0 | function ); |
211 | |
|
212 | 0 | goto on_error; |
213 | 0 | } |
214 | 390 | else if( result != 0 ) |
215 | 8 | { |
216 | 8 | libcerror_error_set( |
217 | 8 | error, |
218 | 8 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
219 | 8 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
220 | 8 | "%s: unsupported fork descriptor has extents overflow.", |
221 | 8 | function ); |
222 | | |
223 | 8 | goto on_error; |
224 | 8 | } |
225 | 382 | if( libfdata_stream_initialize( |
226 | 382 | &safe_allocation_block_stream, |
227 | 382 | NULL, |
228 | 382 | NULL, |
229 | 382 | NULL, |
230 | 382 | NULL, |
231 | 382 | (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libfshfs_block_data_handle_read_segment_data, |
232 | 382 | NULL, |
233 | 382 | (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libfshfs_block_data_handle_seek_segment_offset, |
234 | 382 | 0, |
235 | 382 | error ) != 1 ) |
236 | 0 | { |
237 | 0 | libcerror_error_set( |
238 | 0 | error, |
239 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
240 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
241 | 0 | "%s: unable to create allocation block stream.", |
242 | 0 | function ); |
243 | |
|
244 | 0 | goto on_error; |
245 | 0 | } |
246 | 382 | for( extent_index = 0; |
247 | 2.13k | extent_index < 8; |
248 | 1.75k | extent_index++ ) |
249 | 1.99k | { |
250 | 1.99k | segment_offset = fork_descriptor->extents[ extent_index ][ 0 ]; |
251 | 1.99k | segment_size = fork_descriptor->extents[ extent_index ][ 1 ]; |
252 | | |
253 | 1.99k | if( ( segment_offset == 0 ) |
254 | 1.89k | || ( segment_size == 0 ) ) |
255 | 191 | { |
256 | 191 | break; |
257 | 191 | } |
258 | 1.80k | if( segment_offset > ( (off64_t) INT64_MAX / io_handle->block_size ) ) |
259 | 10 | { |
260 | 10 | libcerror_error_set( |
261 | 10 | error, |
262 | 10 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
263 | 10 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
264 | 10 | "%s: invalid segment offset value out of bounds.", |
265 | 10 | function ); |
266 | | |
267 | 10 | goto on_error; |
268 | 10 | } |
269 | 1.79k | if( segment_size > ( (size64_t) UINT64_MAX / io_handle->block_size ) ) |
270 | 0 | { |
271 | 0 | libcerror_error_set( |
272 | 0 | error, |
273 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
274 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
275 | 0 | "%s: invalid segment size value out of bounds.", |
276 | 0 | function ); |
277 | |
|
278 | 0 | goto on_error; |
279 | 0 | } |
280 | 1.79k | segment_offset *= io_handle->block_size; |
281 | 1.79k | segment_size *= io_handle->block_size; |
282 | | |
283 | 1.79k | if( libfdata_stream_append_segment( |
284 | 1.79k | safe_allocation_block_stream, |
285 | 1.79k | &segment_index, |
286 | 1.79k | 0, |
287 | 1.79k | segment_offset, |
288 | 1.79k | segment_size, |
289 | 1.79k | 0, |
290 | 1.79k | error ) != 1 ) |
291 | 38 | { |
292 | 38 | libcerror_error_set( |
293 | 38 | error, |
294 | 38 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
295 | 38 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
296 | 38 | "%s: unable to append extent: %d allocation block stream segment.", |
297 | 38 | function, |
298 | 38 | extent_index ); |
299 | | |
300 | 38 | goto on_error; |
301 | 38 | } |
302 | 1.79k | } |
303 | 334 | if( libfdata_stream_set_mapped_size( |
304 | 334 | safe_allocation_block_stream, |
305 | 334 | (size64_t) fork_descriptor->size, |
306 | 334 | error ) != 1 ) |
307 | 54 | { |
308 | 54 | libcerror_error_set( |
309 | 54 | error, |
310 | 54 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
311 | 54 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
312 | 54 | "%s: unable to set mapped size of allocation block stream.", |
313 | 54 | function ); |
314 | | |
315 | 54 | goto on_error; |
316 | 54 | } |
317 | 280 | *allocation_block_stream = safe_allocation_block_stream; |
318 | | |
319 | 280 | return( 1 ); |
320 | | |
321 | 110 | on_error: |
322 | 110 | if( safe_allocation_block_stream != NULL ) |
323 | 102 | { |
324 | 102 | libfdata_stream_free( |
325 | 102 | &safe_allocation_block_stream, |
326 | 102 | NULL ); |
327 | 102 | } |
328 | 110 | return( -1 ); |
329 | 334 | } |
330 | | |
331 | | /* Creates an allocation block stream from extents |
332 | | * Make sure the value allocation_block_stream is referencing, is set to NULL |
333 | | * Returns 1 if successful or -1 on error |
334 | | */ |
335 | | int libfshfs_allocation_block_stream_initialize_from_extents( |
336 | | libfdata_stream_t **allocation_block_stream, |
337 | | libfshfs_io_handle_t *io_handle, |
338 | | libcdata_array_t *extents, |
339 | | size64_t data_size, |
340 | | libcerror_error_t **error ) |
341 | 375 | { |
342 | 375 | libfdata_stream_t *safe_allocation_block_stream = NULL; |
343 | 375 | libfshfs_extent_t *extent = NULL; |
344 | 375 | static char *function = "libfshfs_allocation_block_stream_initialize_from_extents"; |
345 | 375 | size64_t segment_size = 0; |
346 | 375 | off64_t segment_offset = 0; |
347 | 375 | int extent_index = 0; |
348 | 375 | int number_of_extents = 0; |
349 | 375 | int segment_index = 0; |
350 | | |
351 | 375 | if( allocation_block_stream == NULL ) |
352 | 0 | { |
353 | 0 | libcerror_error_set( |
354 | 0 | error, |
355 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
356 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
357 | 0 | "%s: invalid allocation block stream.", |
358 | 0 | function ); |
359 | |
|
360 | 0 | return( -1 ); |
361 | 0 | } |
362 | 375 | if( io_handle == NULL ) |
363 | 0 | { |
364 | 0 | libcerror_error_set( |
365 | 0 | error, |
366 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
367 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
368 | 0 | "%s: invalid IO handle.", |
369 | 0 | function ); |
370 | |
|
371 | 0 | return( -1 ); |
372 | 0 | } |
373 | 375 | if( io_handle->block_size == 0 ) |
374 | 0 | { |
375 | 0 | libcerror_error_set( |
376 | 0 | error, |
377 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
378 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
379 | 0 | "%s: invalid IO handle - block size value out of bounds.", |
380 | 0 | function ); |
381 | |
|
382 | 0 | return( -1 ); |
383 | 0 | } |
384 | 375 | if( libcdata_array_get_number_of_entries( |
385 | 375 | extents, |
386 | 375 | &number_of_extents, |
387 | 375 | error ) != 1 ) |
388 | 0 | { |
389 | 0 | libcerror_error_set( |
390 | 0 | error, |
391 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
392 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
393 | 0 | "%s: unable to retrieve number of extents.", |
394 | 0 | function ); |
395 | |
|
396 | 0 | goto on_error; |
397 | 0 | } |
398 | 375 | if( libfdata_stream_initialize( |
399 | 375 | &safe_allocation_block_stream, |
400 | 375 | NULL, |
401 | 375 | NULL, |
402 | 375 | NULL, |
403 | 375 | NULL, |
404 | 375 | (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libfshfs_block_data_handle_read_segment_data, |
405 | 375 | NULL, |
406 | 375 | (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libfshfs_block_data_handle_seek_segment_offset, |
407 | 375 | 0, |
408 | 375 | error ) != 1 ) |
409 | 0 | { |
410 | 0 | libcerror_error_set( |
411 | 0 | error, |
412 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
413 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
414 | 0 | "%s: unable to create allocation block stream.", |
415 | 0 | function ); |
416 | |
|
417 | 0 | goto on_error; |
418 | 0 | } |
419 | 375 | for( extent_index = 0; |
420 | 1.47k | extent_index < number_of_extents; |
421 | 1.10k | extent_index++ ) |
422 | 1.16k | { |
423 | 1.16k | if( libcdata_array_get_entry_by_index( |
424 | 1.16k | extents, |
425 | 1.16k | extent_index, |
426 | 1.16k | (intptr_t **) &extent, |
427 | 1.16k | error ) != 1 ) |
428 | 0 | { |
429 | 0 | libcerror_error_set( |
430 | 0 | error, |
431 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
432 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
433 | 0 | "%s: unable to retrieve extent: %d.", |
434 | 0 | function, |
435 | 0 | extent_index ); |
436 | |
|
437 | 0 | goto on_error; |
438 | 0 | } |
439 | 1.16k | if( extent == NULL ) |
440 | 0 | { |
441 | 0 | libcerror_error_set( |
442 | 0 | error, |
443 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
444 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
445 | 0 | "%s: missing extent: %d.", |
446 | 0 | function, |
447 | 0 | extent_index ); |
448 | |
|
449 | 0 | goto on_error; |
450 | 0 | } |
451 | 1.16k | if( ( extent->block_number == 0 ) |
452 | 1.16k | || ( extent->block_number > ( (uint64_t) INT64_MAX / io_handle->block_size ) ) ) |
453 | 12 | { |
454 | 12 | libcerror_error_set( |
455 | 12 | error, |
456 | 12 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
457 | 12 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
458 | 12 | "%s: invalid extent - invalid block number value out of bounds.", |
459 | 12 | function ); |
460 | | |
461 | 12 | goto on_error; |
462 | 12 | } |
463 | 1.15k | if( ( extent->number_of_blocks == 0 ) |
464 | 1.15k | || ( extent->number_of_blocks > ( (uint64_t) UINT64_MAX / io_handle->block_size ) ) ) |
465 | 0 | { |
466 | 0 | libcerror_error_set( |
467 | 0 | error, |
468 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
469 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
470 | 0 | "%s: invalid extent - invalid number of blocks value out of bounds.", |
471 | 0 | function ); |
472 | |
|
473 | 0 | goto on_error; |
474 | 0 | } |
475 | 1.15k | segment_offset = (off64_t) extent->block_number * io_handle->block_size; |
476 | 1.15k | segment_size = (size64_t) extent->number_of_blocks * io_handle->block_size; |
477 | | |
478 | 1.15k | if( libfdata_stream_append_segment( |
479 | 1.15k | safe_allocation_block_stream, |
480 | 1.15k | &segment_index, |
481 | 1.15k | 0, |
482 | 1.15k | segment_offset, |
483 | 1.15k | segment_size, |
484 | 1.15k | 0, |
485 | 1.15k | error ) != 1 ) |
486 | 51 | { |
487 | 51 | libcerror_error_set( |
488 | 51 | error, |
489 | 51 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
490 | 51 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
491 | 51 | "%s: unable to append extent: %d allocation block stream segment.", |
492 | 51 | function, |
493 | 51 | extent_index ); |
494 | | |
495 | 51 | goto on_error; |
496 | 51 | } |
497 | 1.15k | } |
498 | 312 | if( libfdata_stream_set_mapped_size( |
499 | 312 | safe_allocation_block_stream, |
500 | 312 | data_size, |
501 | 312 | error ) != 1 ) |
502 | 238 | { |
503 | 238 | libcerror_error_set( |
504 | 238 | error, |
505 | 238 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
506 | 238 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
507 | 238 | "%s: unable to set mapped size of allocation block stream.", |
508 | 238 | function ); |
509 | | |
510 | 238 | goto on_error; |
511 | 238 | } |
512 | 74 | *allocation_block_stream = safe_allocation_block_stream; |
513 | | |
514 | 74 | return( 1 ); |
515 | | |
516 | 301 | on_error: |
517 | 301 | if( safe_allocation_block_stream != NULL ) |
518 | 301 | { |
519 | 301 | libfdata_stream_free( |
520 | 301 | &safe_allocation_block_stream, |
521 | 301 | NULL ); |
522 | 301 | } |
523 | 301 | return( -1 ); |
524 | 312 | } |
525 | | |
526 | | /* Creates an allocation block stream from a compressed stream |
527 | | * Make sure the value allocation_block_stream is referencing, is set to NULL |
528 | | * Returns 1 if successful or -1 on error |
529 | | */ |
530 | | int libfshfs_allocation_block_stream_initialize_from_compressed_stream( |
531 | | libfdata_stream_t **allocation_block_stream, |
532 | | libfdata_stream_t *compressed_allocation_block_stream, |
533 | | size64_t uncompressed_data_size, |
534 | | int compression_method, |
535 | | libcerror_error_t **error ) |
536 | 0 | { |
537 | 0 | libfdata_stream_t *safe_allocation_block_stream = NULL; |
538 | 0 | libfshfs_compressed_data_handle_t *data_handle = NULL; |
539 | 0 | static char *function = "libfshfs_allocation_block_stream_initialize_from_compressed_stream"; |
540 | 0 | int segment_index = 0; |
541 | |
|
542 | 0 | if( allocation_block_stream == NULL ) |
543 | 0 | { |
544 | 0 | libcerror_error_set( |
545 | 0 | error, |
546 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
547 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
548 | 0 | "%s: invalid allocation block stream.", |
549 | 0 | function ); |
550 | |
|
551 | 0 | return( -1 ); |
552 | 0 | } |
553 | 0 | if( libfshfs_compressed_data_handle_initialize( |
554 | 0 | &data_handle, |
555 | 0 | compressed_allocation_block_stream, |
556 | 0 | uncompressed_data_size, |
557 | 0 | compression_method, |
558 | 0 | error ) != 1 ) |
559 | 0 | { |
560 | 0 | libcerror_error_set( |
561 | 0 | error, |
562 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
563 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
564 | 0 | "%s: unable to create compressed data handle.", |
565 | 0 | function ); |
566 | |
|
567 | 0 | goto on_error; |
568 | 0 | } |
569 | 0 | if( libfdata_stream_initialize( |
570 | 0 | &safe_allocation_block_stream, |
571 | 0 | (intptr_t *) data_handle, |
572 | 0 | (int (*)(intptr_t **, libcerror_error_t **)) &libfshfs_compressed_data_handle_free, |
573 | 0 | NULL, |
574 | 0 | NULL, |
575 | 0 | (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libfshfs_compressed_data_handle_read_segment_data, |
576 | 0 | NULL, |
577 | 0 | (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libfshfs_compressed_data_handle_seek_segment_offset, |
578 | 0 | LIBFDATA_DATA_HANDLE_FLAG_MANAGED, |
579 | 0 | error ) != 1 ) |
580 | 0 | { |
581 | 0 | libcerror_error_set( |
582 | 0 | error, |
583 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
584 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
585 | 0 | "%s: unable to create allocation block stream.", |
586 | 0 | function ); |
587 | |
|
588 | 0 | goto on_error; |
589 | 0 | } |
590 | 0 | data_handle = NULL; |
591 | |
|
592 | 0 | if( libfdata_stream_append_segment( |
593 | 0 | safe_allocation_block_stream, |
594 | 0 | &segment_index, |
595 | 0 | 0, |
596 | 0 | 0, |
597 | 0 | uncompressed_data_size, |
598 | 0 | LIBFDATA_RANGE_FLAG_IS_COMPRESSED, |
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_APPEND_FAILED, |
605 | 0 | "%s: unable to append data as allocation block stream segment.", |
606 | 0 | function ); |
607 | |
|
608 | 0 | goto on_error; |
609 | 0 | } |
610 | 0 | *allocation_block_stream = safe_allocation_block_stream; |
611 | |
|
612 | 0 | return( 1 ); |
613 | | |
614 | 0 | on_error: |
615 | 0 | if( safe_allocation_block_stream != NULL ) |
616 | 0 | { |
617 | 0 | libfdata_stream_free( |
618 | 0 | &safe_allocation_block_stream, |
619 | 0 | NULL ); |
620 | 0 | } |
621 | 0 | if( data_handle != NULL ) |
622 | 0 | { |
623 | 0 | libfshfs_compressed_data_handle_free( |
624 | 0 | &data_handle, |
625 | | NULL ); |
626 | 0 | } |
627 | 0 | return( -1 ); |
628 | 0 | } |
629 | | |