Debian Codegraph @

libfdata/libfdata_area.c

raw symbols SCIP
1 /*
2 * The area functions
3 *
4 * Copyright (C) 2010-2024, Joachim Metz <[email protected]>
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 "libfdata_area.h"
27 #include "libfdata_definitions.h"
28 #include "libfdata_libcdata.h"
29 #include "libfdata_libcerror.h"
30 #include "libfdata_libcnotify.h"
31 #include "libfdata_libfcache.h"
32 #include "libfdata_mapped_range.h"
33 #include "libfdata_range.h"
34 #include "libfdata_segments_array.h"
35 #include "libfdata_types.h"
36 #include "libfdata_unused.h"
37
38 /* Creates an area
39 * Make sure the value area is referencing, is set to NULL
40 *
41 * If the flag LIBFDATA_DATA_HANDLE_FLAG_MANAGED is set the area
42 * takes over management of the data handle and the data handle is freed when
43 * no longer needed
44 *
45 * Returns 1 if successful or -1 on error
46 */
47 int libfdata_area_initialize(
48 libfdata_area_t **area,
49 size64_t element_data_size,
50 intptr_t *data_handle,
51 int (*free_data_handle)(
52 intptr_t **data_handle,
53 libcerror_error_t **error ),
54 int (*clone_data_handle)(
55 intptr_t **destination_data_handle,
56 intptr_t *source_data_handle,
57 libcerror_error_t **error ),
58 int (*read_element_data)(
59 intptr_t *data_handle,
60 intptr_t *file_io_handle,
61 libfdata_area_t *area,
62 libfdata_cache_t *cache,
63 off64_t element_value_offset,
64 int element_data_file_index,
65 off64_t element_data_offset,
66 size64_t element_data_size,
67 uint32_t element_data_flags,
68 uint8_t read_flags,
69 libcerror_error_t **error ),
70 int (*write_element_data)(
71 intptr_t *data_handle,
72 intptr_t *file_io_handle,
73 libfdata_area_t *area,
74 libfdata_cache_t *cache,
75 off64_t element_value_offset,
76 int element_data_file_index,
77 off64_t element_data_offset,
78 size64_t element_data_size,
79 uint32_t element_data_flags,
80 uint8_t write_flags,
81 libcerror_error_t **error ),
82 uint8_t flags,
83 libcerror_error_t **error )
84 {
85 libfdata_internal_area_t *internal_area = NULL;
86 static char *function = "libfdata_area_initialize";
87
88 if( area == NULL )
89 {
90 libcerror_error_set(
91 error,
92 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
93 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
94 "%s: invalid area.",
95 function );
96
97 return( -1 );
98 }
99 if( *area != NULL )
100 {
101 libcerror_error_set(
102 error,
103 LIBCERROR_ERROR_DOMAIN_RUNTIME,
104 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
105 "%s: invalid area value already set.",
106 function );
107
108 return( -1 );
109 }
110 if( element_data_size == 0 )
111 {
112 libcerror_error_set(
113 error,
114 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
115 LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
116 "%s: invalid element data size value zero or less.",
117 function );
118
119 return( -1 );
120 }
121 internal_area = memory_allocate_structure(
122 libfdata_internal_area_t );
123
124 if( internal_area == NULL )
125 {
126 libcerror_error_set(
127 error,
128 LIBCERROR_ERROR_DOMAIN_MEMORY,
129 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
130 "%s: unable to create area.",
131 function );
132
133 goto on_error;
134 }
135 if( memory_set(
136 internal_area,
137 0,
138 sizeof( libfdata_internal_area_t ) ) == NULL )
139 {
140 libcerror_error_set(
141 error,
142 LIBCERROR_ERROR_DOMAIN_MEMORY,
143 LIBCERROR_MEMORY_ERROR_SET_FAILED,
144 "%s: unable to clear area.",
145 function );
146
147 memory_free(
148 internal_area );
149
150 return( -1 );
151 }
152 if( libcdata_array_initialize(
153 &( internal_area->segments_array ),
154 0,
155 error ) != 1 )
156 {
157 libcerror_error_set(
158 error,
159 LIBCERROR_ERROR_DOMAIN_RUNTIME,
160 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
161 "%s: unable to create segments array.",
162 function );
163
164 goto on_error;
165 }
166 if( libcdata_array_initialize(
167 &( internal_area->mapped_ranges_array ),
168 0,
169 error ) != 1 )
170 {
171 libcerror_error_set(
172 error,
173 LIBCERROR_ERROR_DOMAIN_RUNTIME,
174 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
175 "%s: unable to create mapped ranges array.",
176 function );
177
178 goto on_error;
179 }
180 if( libfcache_date_time_get_timestamp(
181 &( internal_area->timestamp ),
182 error ) != 1 )
183 {
184 libcerror_error_set(
185 error,
186 LIBCERROR_ERROR_DOMAIN_RUNTIME,
187 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
188 "%s: unable to retrieve cache timestamp.",
189 function );
190
191 goto on_error;
192 }
193 internal_area->element_data_size = element_data_size;
194 internal_area->flags |= flags;
195 internal_area->data_handle = data_handle;
196 internal_area->free_data_handle = free_data_handle;
197 internal_area->clone_data_handle = clone_data_handle;
198 internal_area->read_element_data = read_element_data;
199 internal_area->write_element_data = write_element_data;
200
201 *area = (libfdata_area_t *) internal_area;
202
203 return( 1 );
204
205 on_error:
206 if( internal_area != NULL )
207 {
208 if( internal_area->segments_array != NULL )
209 {
210 libcdata_array_free(
211 &( internal_area->segments_array ),
212 NULL,
213 NULL );
214 }
215 memory_free(
216 internal_area );
217 }
218 return( -1 );
219 }
220
221 /* Frees an area
222 * Returns 1 if successful or -1 on error
223 */
224 int libfdata_area_free(
225 libfdata_area_t **area,
226 libcerror_error_t **error )
227 {
228 libfdata_internal_area_t *internal_area = NULL;
229 static char *function = "libfdata_area_free";
230 int result = 1;
231
232 if( area == NULL )
233 {
234 libcerror_error_set(
235 error,
236 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
237 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
238 "%s: invalid area.",
239 function );
240
241 return( -1 );
242 }
243 if( *area != NULL )
244 {
245 internal_area = (libfdata_internal_area_t *) *area;
246 *area = NULL;
247
248 if( libcdata_array_free(
249 &( internal_area->segments_array ),
250 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_range_free,
251 error ) != 1 )
252 {
253 libcerror_error_set(
254 error,
255 LIBCERROR_ERROR_DOMAIN_RUNTIME,
256 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
257 "%s: unable to free segments array.",
258 function );
259
260 result = -1;
261 }
262 if( libcdata_array_free(
263 &( internal_area->mapped_ranges_array ),
264 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
265 error ) != 1 )
266 {
267 libcerror_error_set(
268 error,
269 LIBCERROR_ERROR_DOMAIN_RUNTIME,
270 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
271 "%s: unable to free mapped ranges array.",
272 function );
273
274 result = -1;
275 }
276 if( ( internal_area->flags & LIBFDATA_DATA_HANDLE_FLAG_MANAGED ) != 0 )
277 {
278 if( internal_area->data_handle != NULL )
279 {
280 if( internal_area->free_data_handle == NULL )
281 {
282 libcerror_error_set(
283 error,
284 LIBCERROR_ERROR_DOMAIN_RUNTIME,
285 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
286 "%s: invalid area - missing free data handle function.",
287 function );
288
289 result = -1;
290 }
291 else if( internal_area->free_data_handle(
292 &( internal_area->data_handle ),
293 error ) != 1 )
294 {
295 libcerror_error_set(
296 error,
297 LIBCERROR_ERROR_DOMAIN_RUNTIME,
298 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
299 "%s: unable to free data handle.",
300 function );
301
302 result = -1;
303 }
304 }
305 }
306 memory_free(
307 internal_area );
308 }
309 return( result );
310 }
311
312 /* Clones (duplicates) the area
313 * Returns 1 if successful or -1 on error
314 */
315 int libfdata_area_clone(
316 libfdata_area_t **destination_area,
317 libfdata_area_t *source_area,
318 libcerror_error_t **error )
319 {
320 libfdata_internal_area_t *internal_destination_area = NULL;
321 libfdata_internal_area_t *internal_source_area = NULL;
322 static char *function = "libfdata_area_clone";
323
324 if( destination_area == NULL )
325 {
326 libcerror_error_set(
327 error,
328 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
329 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
330 "%s: invalid destination area.",
331 function );
332
333 return( -1 );
334 }
335 if( *destination_area != NULL )
336 {
337 libcerror_error_set(
338 error,
339 LIBCERROR_ERROR_DOMAIN_RUNTIME,
340 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
341 "%s: invalid destination area value already set.",
342 function );
343
344 return( -1 );
345 }
346 if( source_area == NULL )
347 {
348 *destination_area = source_area;
349
350 return( 1 );
351 }
352 internal_source_area = (libfdata_internal_area_t *) source_area;
353
354 /* TODO refactor to use libfdata_area_initialize this requires libcdata_array_copy_elements function */
355 internal_destination_area = memory_allocate_structure(
356 libfdata_internal_area_t );
357
358 if( internal_destination_area == NULL )
359 {
360 libcerror_error_set(
361 error,
362 LIBCERROR_ERROR_DOMAIN_MEMORY,
363 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
364 "%s: unable to create destination area.",
365 function );
366
367 goto on_error;
368 }
369 if( memory_set(
370 internal_destination_area,
371 0,
372 sizeof( libfdata_internal_area_t ) ) == NULL )
373 {
374 libcerror_error_set(
375 error,
376 LIBCERROR_ERROR_DOMAIN_MEMORY,
377 LIBCERROR_MEMORY_ERROR_SET_FAILED,
378 "%s: unable to clear destination area.",
379 function );
380
381 memory_free(
382 internal_destination_area );
383
384 return( -1 );
385 }
386 if( internal_source_area->data_handle != NULL )
387 {
388 if( internal_source_area->free_data_handle == NULL )
389 {
390 libcerror_error_set(
391 error,
392 LIBCERROR_ERROR_DOMAIN_RUNTIME,
393 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
394 "%s: invalid source area - missing free data handle function.",
395 function );
396
397 goto on_error;
398 }
399 if( internal_source_area->clone_data_handle == NULL )
400 {
401 libcerror_error_set(
402 error,
403 LIBCERROR_ERROR_DOMAIN_RUNTIME,
404 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
405 "%s: invalid source area - missing clone data handle function.",
406 function );
407
408 goto on_error;
409 }
410 if( internal_source_area->clone_data_handle(
411 &( internal_destination_area->data_handle ),
412 internal_source_area->data_handle,
413 error ) != 1 )
414 {
415 libcerror_error_set(
416 error,
417 LIBCERROR_ERROR_DOMAIN_RUNTIME,
418 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
419 "%s: unable to create destination data handle.",
420 function );
421
422 goto on_error;
423 }
424 }
425 if( libcdata_array_clone(
426 &( internal_destination_area->segments_array ),
427 internal_source_area->segments_array,
428 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_range_free,
429 (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) &libfdata_range_clone,
430 error ) != 1 )
431 {
432 libcerror_error_set(
433 error,
434 LIBCERROR_ERROR_DOMAIN_RUNTIME,
435 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
436 "%s: unable to create destination segments array.",
437 function );
438
439 goto on_error;
440 }
441 if( libcdata_array_clone(
442 &( internal_destination_area->mapped_ranges_array ),
443 internal_source_area->mapped_ranges_array,
444 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
445 (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) &libfdata_mapped_range_clone,
446 error ) != 1 )
447 {
448 libcerror_error_set(
449 error,
450 LIBCERROR_ERROR_DOMAIN_RUNTIME,
451 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
452 "%s: unable to create destination mapped ranges array.",
453 function );
454
455 goto on_error;
456 }
457 internal_destination_area->element_data_size = internal_source_area->element_data_size;
458 internal_destination_area->timestamp = internal_source_area->timestamp;
459 internal_destination_area->flags = internal_source_area->flags | LIBFDATA_DATA_HANDLE_FLAG_MANAGED;
460 internal_destination_area->data_handle = internal_source_area->data_handle;
461 internal_destination_area->free_data_handle = internal_source_area->free_data_handle;
462 internal_destination_area->clone_data_handle = internal_source_area->clone_data_handle;
463 internal_destination_area->read_element_data = internal_source_area->read_element_data;
464 internal_destination_area->write_element_data = internal_source_area->write_element_data;
465
466 *destination_area = (libfdata_area_t *) internal_destination_area;
467
468 return( 1 );
469
470 on_error:
471 if( internal_destination_area != NULL )
472 {
473 if( internal_destination_area->segments_array != NULL )
474 {
475 libcdata_array_free(
476 &( internal_destination_area->segments_array ),
477 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_range_free,
478 NULL );
479 }
480 if( ( internal_destination_area->data_handle != NULL )
481 && ( internal_source_area->free_data_handle != NULL ) )
482 {
483 internal_source_area->free_data_handle(
484 &( internal_destination_area->data_handle ),
485 NULL );
486 }
487 memory_free(
488 internal_destination_area );
489 }
490 return( -1 );
491 }
492
493 /* Segment functions
494 */
495
496 /* Empties the area
497 * Returns 1 if successful or -1 on error
498 */
499 int libfdata_area_empty(
500 libfdata_area_t *area,
501 libcerror_error_t **error )
502 {
503 libfdata_internal_area_t *internal_area = NULL;
504 static char *function = "libfdata_area_empty";
505
506 if( area == NULL )
507 {
508 libcerror_error_set(
509 error,
510 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
511 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
512 "%s: invalid area.",
513 function );
514
515 return( -1 );
516 }
517 internal_area = (libfdata_internal_area_t *) area;
518
519 if( libcdata_array_empty(
520 internal_area->segments_array,
521 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_range_free,
522 error ) != 1 )
523 {
524 libcerror_error_set(
525 error,
526 LIBCERROR_ERROR_DOMAIN_RUNTIME,
527 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
528 "%s: unable to empty segments array.",
529 function );
530
531 return( -1 );
532 }
533 if( libcdata_array_empty(
534 internal_area->mapped_ranges_array,
535 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
536 error ) != 1 )
537 {
538 libcerror_error_set(
539 error,
540 LIBCERROR_ERROR_DOMAIN_RUNTIME,
541 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
542 "%s: unable to empty mapped ranges array.",
543 function );
544
545 return( -1 );
546 }
547 internal_area->size = 0;
548
549 return( 1 );
550 }
551
552 /* Resizes the area
553 * Returns 1 if successful or -1 on error
554 */
555 int libfdata_area_resize(
556 libfdata_area_t *area,
557 int number_of_segments,
558 libcerror_error_t **error )
559 {
560 libfdata_internal_area_t *internal_area = NULL;
561 static char *function = "libfdata_area_resize";
562
563 if( area == NULL )
564 {
565 libcerror_error_set(
566 error,
567 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
568 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
569 "%s: invalid area.",
570 function );
571
572 return( -1 );
573 }
574 internal_area = (libfdata_internal_area_t *) area;
575
576 if( libcdata_array_resize(
577 internal_area->segments_array,
578 number_of_segments,
579 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_range_free,
580 error ) != 1 )
581 {
582 libcerror_error_set(
583 error,
584 LIBCERROR_ERROR_DOMAIN_RUNTIME,
585 LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
586 "%s: unable to resize segments array.",
587 function );
588
589 return( -1 );
590 }
591 if( libcdata_array_resize(
592 internal_area->mapped_ranges_array,
593 number_of_segments,
594 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
595 error ) != 1 )
596 {
597 libcerror_error_set(
598 error,
599 LIBCERROR_ERROR_DOMAIN_RUNTIME,
600 LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
601 "%s: unable to resize mapped ranges array.",
602 function );
603
604 return( -1 );
605 }
606 internal_area->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
607
608 return( 1 );
609 }
610
611 /* Retrieves the number of segments of the area
612 * Returns 1 if successful or -1 on error
613 */
614 int libfdata_area_get_number_of_segments(
615 libfdata_area_t *area,
616 int *number_of_segments,
617 libcerror_error_t **error )
618 {
619 libfdata_internal_area_t *internal_area = NULL;
620 static char *function = "libfdata_area_get_number_of_segments";
621
622 if( area == NULL )
623 {
624 libcerror_error_set(
625 error,
626 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
627 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
628 "%s: invalid area.",
629 function );
630
631 return( -1 );
632 }
633 internal_area = (libfdata_internal_area_t *) area;
634
635 if( libcdata_array_get_number_of_entries(
636 internal_area->segments_array,
637 number_of_segments,
638 error ) != 1 )
639 {
640 libcerror_error_set(
641 error,
642 LIBCERROR_ERROR_DOMAIN_RUNTIME,
643 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
644 "%s: unable to retrieve number of entries from segments array.",
645 function );
646
647 return( -1 );
648 }
649 return( 1 );
650 }
651
652 /* Retrieves the offset and size of a specific segment
653 * Returns 1 if successful or -1 on error
654 */
655 int libfdata_area_get_segment_by_index(
656 libfdata_area_t *area,
657 int segment_index,
658 int *segment_file_index,
659 off64_t *segment_offset,
660 size64_t *segment_size,
661 uint32_t *segment_flags,
662 libcerror_error_t **error )
663 {
664 libfdata_internal_area_t *internal_area = NULL;
665 static char *function = "libfdata_area_get_segment_by_index";
666
667 if( area == NULL )
668 {
669 libcerror_error_set(
670 error,
671 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
672 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
673 "%s: invalid area.",
674 function );
675
676 return( -1 );
677 }
678 internal_area = (libfdata_internal_area_t *) area;
679
680 if( libfdata_segments_array_get_segment_by_index(
681 internal_area->segments_array,
682 segment_index,
683 segment_file_index,
684 segment_offset,
685 segment_size,
686 segment_flags,
687 error ) != 1 )
688 {
689 libcerror_error_set(
690 error,
691 LIBCERROR_ERROR_DOMAIN_RUNTIME,
692 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
693 "%s: unable to retrieve segment: %d.",
694 function,
695 segment_index );
696
697 return( -1 );
698 }
699 return( 1 );
700 }
701
702 /* Sets the offset and size of a specific segment
703 * Returns 1 if successful or -1 on error
704 */
705 int libfdata_area_set_segment_by_index(
706 libfdata_area_t *area,
707 int segment_index,
708 int segment_file_index,
709 off64_t segment_offset,
710 size64_t segment_size,
711 uint32_t segment_flags,
712 libcerror_error_t **error )
713 {
714 libfdata_internal_area_t *internal_area = NULL;
715 static char *function = "libfdata_area_set_segment_by_index";
716
717 if( area == NULL )
718 {
719 libcerror_error_set(
720 error,
721 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
722 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
723 "%s: invalid area.",
724 function );
725
726 return( -1 );
727 }
728 internal_area = (libfdata_internal_area_t *) area;
729
730 if( libfdata_segments_array_set_segment_by_index(
731 internal_area->segments_array,
732 internal_area->mapped_ranges_array,
733 &( internal_area->size ),
734 segment_index,
735 segment_file_index,
736 segment_offset,
737 segment_size,
738 segment_flags,
739 error ) != 1 )
740 {
741 libcerror_error_set(
742 error,
743 LIBCERROR_ERROR_DOMAIN_RUNTIME,
744 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
745 "%s: unable to set segment: %d.",
746 function,
747 segment_index );
748
749 return( -1 );
750 }
751 internal_area->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
752
753 return( 1 );
754 }
755
756 /* Prepends a segment
757 * Returns 1 if successful or -1 on error
758 */
759 int libfdata_area_prepend_segment(
760 libfdata_area_t *area,
761 int segment_file_index,
762 off64_t segment_offset,
763 size64_t segment_size,
764 uint32_t segment_flags,
765 libcerror_error_t **error )
766 {
767 libfdata_internal_area_t *internal_area = NULL;
768 static char *function = "libfdata_area_prepend_segment";
769
770 if( area == NULL )
771 {
772 libcerror_error_set(
773 error,
774 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
775 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
776 "%s: invalid area.",
777 function );
778
779 return( -1 );
780 }
781 internal_area = (libfdata_internal_area_t *) area;
782
783 if( libfdata_segments_array_prepend_segment(
784 internal_area->segments_array,
785 internal_area->mapped_ranges_array,
786 &( internal_area->size ),
787 segment_file_index,
788 segment_offset,
789 segment_size,
790 segment_flags,
791 error ) != 1 )
792 {
793 libcerror_error_set(
794 error,
795 LIBCERROR_ERROR_DOMAIN_RUNTIME,
796 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
797 "%s: unable to prepend segment.",
798 function );
799
800 return( -1 );
801 }
802 internal_area->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
803
804 return( 1 );
805 }
806
807 /* Appends a segment
808 * Returns 1 if successful or -1 on error
809 */
810 int libfdata_area_append_segment(
811 libfdata_area_t *area,
812 int *segment_index,
813 int segment_file_index,
814 off64_t segment_offset,
815 size64_t segment_size,
816 uint32_t segment_flags,
817 libcerror_error_t **error )
818 {
819 libfdata_internal_area_t *internal_area = NULL;
820 static char *function = "libfdata_area_append_segment";
821
822 if( area == NULL )
823 {
824 libcerror_error_set(
825 error,
826 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
827 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
828 "%s: invalid area.",
829 function );
830
831 return( -1 );
832 }
833 internal_area = (libfdata_internal_area_t *) area;
834
835 if( libfdata_segments_array_append_segment(
836 internal_area->segments_array,
837 internal_area->mapped_ranges_array,
838 &( internal_area->size ),
839 segment_index,
840 segment_file_index,
841 segment_offset,
842 segment_size,
843 segment_flags,
844 error ) != 1 )
845 {
846 libcerror_error_set(
847 error,
848 LIBCERROR_ERROR_DOMAIN_RUNTIME,
849 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
850 "%s: unable to append segment.",
851 function );
852
853 return( -1 );
854 }
855 internal_area->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
856
857 return( 1 );
858 }
859
860 /* Area element functions
861 */
862
863 /* Retrieves the element data size of the area
864 * Returns 1 if successful or -1 on error
865 */
866 int libfdata_area_get_element_data_size(
867 libfdata_area_t *area,
868 size64_t *element_data_size,
869 libcerror_error_t **error )
870 {
871 libfdata_internal_area_t *internal_area = NULL;
872 static char *function = "libfdata_area_get_element_data_size";
873
874 if( area == NULL )
875 {
876 libcerror_error_set(
877 error,
878 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
879 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
880 "%s: invalid area.",
881 function );
882
883 return( -1 );
884 }
885 internal_area = (libfdata_internal_area_t *) area;
886
887 if( element_data_size == NULL )
888 {
889 libcerror_error_set(
890 error,
891 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
892 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
893 "%s: invalid element data size.",
894 function );
895
896 return( -1 );
897 }
898 *element_data_size = internal_area->element_data_size;
899
900 return( 1 );
901 }
902
903 /* Area element value functions
904 */
905
906 /* Retrieves the value an element at a specific offset
907 * Returns 1 if successful or -1 on error
908 */
909 int libfdata_area_get_element_value_at_offset(
910 libfdata_area_t *area,
911 intptr_t *file_io_handle,
912 libfdata_cache_t *cache,
913 off64_t element_value_offset,
914 intptr_t **element_value,
915 uint8_t read_flags,
916 libcerror_error_t **error )
917 {
918 libfcache_cache_value_t *cache_value = NULL;
919 libfdata_internal_area_t *internal_area = NULL;
920 libfdata_range_t *segment_data_range = NULL;
921 static char *function = "libfdata_area_get_element_value_at_offset";
922 off64_t cache_value_offset = (off64_t) -1;
923 off64_t element_data_offset = (off64_t) -1;
924 int64_t cache_value_timestamp = 0;
925 uint32_t element_data_flags = 0;
926 int cache_entry_index = -1;
927 int cache_value_file_index = -1;
928 int element_data_file_index = -1;
929 int element_index = -1;
930 int number_of_cache_entries = 0;
931 int result = 0;
932
933 if( area == NULL )
934 {
935 libcerror_error_set(
936 error,
937 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
938 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
939 "%s: invalid area.",
940 function );
941
942 return( -1 );
943 }
944 internal_area = (libfdata_internal_area_t *) area;
945
946 if( internal_area->read_element_data == NULL )
947 {
948 libcerror_error_set(
949 error,
950 LIBCERROR_ERROR_DOMAIN_RUNTIME,
951 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
952 "%s: invalid area - missing read element data function.",
953 function );
954
955 return( -1 );
956 }
957 if( internal_area->element_data_size == 0 )
958 {
959 libcerror_error_set(
960 error,
961 LIBCERROR_ERROR_DOMAIN_RUNTIME,
962 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
963 "%s: invalid area - element data size value out of bounds.",
964 function );
965
966 return( -1 );
967 }
968 if( ( internal_area->size == 0 )
969 || ( internal_area->size > (off64_t) INT64_MAX ) )
970 {
971 libcerror_error_set(
972 error,
973 LIBCERROR_ERROR_DOMAIN_RUNTIME,
974 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
975 "%s: invalid area - size value out of bounds.",
976 function );
977
978 return( -1 );
979 }
980 if( ( (size64_t) element_value_offset > internal_area->size )
981 || ( (size64_t) element_value_offset > ( internal_area->size - internal_area->element_data_size ) ) )
982 {
983 libcerror_error_set(
984 error,
985 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
986 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
987 "%s: invalid element value offset value out of bounds.",
988 function );
989
990 return( -1 );
991 }
992 if( libfdata_segments_array_get_data_range_at_offset(
993 internal_area->segments_array,
994 element_value_offset,
995 &element_data_offset,
996 &segment_data_range,
997 error ) != 1 )
998 {
999 libcerror_error_set(
1000 error,
1001 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1002 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1003 "%s: unable to retrieve segment data range for offset: 0x%08" PRIx64 ".",
1004 function,
1005 element_value_offset );
1006
1007 return( -1 );
1008 }
1009 if( segment_data_range == NULL )
1010 {
1011 libcerror_error_set(
1012 error,
1013 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1014 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1015 "%s: missing segment data range.",
1016 function );
1017
1018 return( -1 );
1019 }
1020 element_data_file_index = segment_data_range->file_index;
1021 element_data_offset += segment_data_range->offset;
1022 element_data_flags = segment_data_range->flags;
1023
1024 if( libfcache_cache_get_number_of_entries(
1025 (libfcache_cache_t *) cache,
1026 &number_of_cache_entries,
1027 error ) != 1 )
1028 {
1029 libcerror_error_set(
1030 error,
1031 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1032 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1033 "%s: unable to retrieve number of cache entries.",
1034 function );
1035
1036 return( -1 );
1037 }
1038 if( number_of_cache_entries <= 0 )
1039 {
1040 libcerror_error_set(
1041 error,
1042 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1043 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1044 "%s: invalid number of cache entries value out of bounds.",
1045 function );
1046
1047 return( -1 );
1048 }
1049 element_index = (int) ( element_value_offset / internal_area->element_data_size );
1050
1051 if( ( read_flags & LIBFDATA_READ_FLAG_IGNORE_CACHE ) == 0 )
1052 {
1053 if( internal_area->calculate_cache_entry_index == NULL )
1054 {
1055 cache_entry_index = element_index % number_of_cache_entries;
1056 }
1057 else
1058 {
1059 cache_entry_index = internal_area->calculate_cache_entry_index(
1060 element_index,
1061 element_data_file_index,
1062 element_data_offset,
1063 internal_area->element_data_size,
1064 element_data_flags,
1065 number_of_cache_entries );
1066 }
1067 if( libfcache_cache_get_value_by_index(
1068 (libfcache_cache_t *) cache,
1069 cache_entry_index,
1070 &cache_value,
1071 error ) != 1 )
1072 {
1073 libcerror_error_set(
1074 error,
1075 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1076 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1077 "%s: unable to retrieve cache entry: %d from cache.",
1078 function,
1079 cache_entry_index );
1080
1081 return( -1 );
1082 }
1083 if( cache_value != NULL )
1084 {
1085 if( libfcache_cache_value_get_identifier(
1086 cache_value,
1087 &cache_value_file_index,
1088 &cache_value_offset,
1089 &cache_value_timestamp,
1090 error ) != 1 )
1091 {
1092 libcerror_error_set(
1093 error,
1094 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1095 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1096 "%s: unable to retrieve cache value identifier.",
1097 function );
1098
1099 return( -1 );
1100 }
1101 }
1102 if( ( element_data_file_index == cache_value_file_index )
1103 && ( element_data_offset == cache_value_offset )
1104 && ( internal_area->timestamp == cache_value_timestamp ) )
1105 {
1106 result = 1;
1107 }
1108 #if defined( HAVE_DEBUG_OUTPUT )
1109 if( libcnotify_verbose != 0 )
1110 {
1111 if( result == 0 )
1112 {
1113 libcnotify_printf(
1114 "%s: cache: 0x%08" PRIjx " miss (%d out of %d)\n",
1115 function,
1116 (intptr_t) cache,
1117 cache_entry_index,
1118 number_of_cache_entries );
1119 }
1120 else
1121 {
1122 libcnotify_printf(
1123 "%s: cache: 0x%08" PRIjx " hit (%d out of %d)\n",
1124 function,
1125 (intptr_t) cache,
1126 cache_entry_index,
1127 number_of_cache_entries );
1128 }
1129 }
1130 #endif
1131 }
1132 if( result == 0 )
1133 {
1134 #if defined( HAVE_DEBUG_OUTPUT )
1135 if( libcnotify_verbose != 0 )
1136 {
1137 libcnotify_printf(
1138 "%s: reading element data at offset: %" PRIi64 " (0x%08" PRIx64 ") of size: %" PRIu64 "\n",
1139 function,
1140 element_data_offset,
1141 element_data_offset,
1142 internal_area->element_data_size );
1143 }
1144 #endif
1145 if( internal_area->read_element_data(
1146 internal_area->data_handle,
1147 file_io_handle,
1148 area,
1149 cache,
1150 element_value_offset,
1151 element_data_file_index,
1152 element_data_offset,
1153 internal_area->element_data_size,
1154 element_data_flags,
1155 read_flags,
1156 error ) != 1 )
1157 {
1158 libcerror_error_set(
1159 error,
1160 LIBCERROR_ERROR_DOMAIN_IO,
1161 LIBCERROR_IO_ERROR_READ_FAILED,
1162 "%s: unable to read element data at offset: 0x%08" PRIx64 ".",
1163 function,
1164 element_data_offset );
1165
1166 return( -1 );
1167 }
1168 if( internal_area->calculate_cache_entry_index == NULL )
1169 {
1170 cache_entry_index = element_index % number_of_cache_entries;
1171 }
1172 else
1173 {
1174 cache_entry_index = internal_area->calculate_cache_entry_index(
1175 element_index,
1176 element_data_file_index,
1177 element_data_offset,
1178 internal_area->element_data_size,
1179 element_data_flags,
1180 number_of_cache_entries );
1181 }
1182 if( libfcache_cache_get_value_by_index(
1183 (libfcache_cache_t *) cache,
1184 cache_entry_index,
1185 &cache_value,
1186 error ) != 1 )
1187 {
1188 libcerror_error_set(
1189 error,
1190 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1191 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1192 "%s: unable to retrieve cache entry: %d from cache.",
1193 function,
1194 cache_entry_index );
1195
1196 return( -1 );
1197 }
1198 if( cache_value != NULL )
1199 {
1200 if( libfcache_cache_value_get_identifier(
1201 cache_value,
1202 &cache_value_file_index,
1203 &cache_value_offset,
1204 &cache_value_timestamp,
1205 error ) != 1 )
1206 {
1207 libcerror_error_set(
1208 error,
1209 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1210 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1211 "%s: unable to retrieve cache value identifier.",
1212 function );
1213
1214 return( -1 );
1215 }
1216 }
1217 if( ( element_data_file_index != cache_value_file_index )
1218 || ( element_data_offset != cache_value_offset )
1219 || ( internal_area->timestamp != cache_value_timestamp ) )
1220 {
1221 libcerror_error_set(
1222 error,
1223 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1224 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1225 "%s: missing cache value.",
1226 function );
1227
1228 return( -1 );
1229 }
1230 }
1231 if( libfcache_cache_value_get_value(
1232 cache_value,
1233 element_value,
1234 error ) != 1 )
1235 {
1236 libcerror_error_set(
1237 error,
1238 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1239 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1240 "%s: unable to retrieve element value.",
1241 function );
1242
1243 return( -1 );
1244 }
1245 return( 1 );
1246 }
1247
1248 /* Sets the value of a specific element
1249 *
1250 * If the flag LIBFDATA_AREA_ELEMENT_VALUE_FLAG_MANAGED is set the area
1251 * takes over management of the value and the value is freed when
1252 * no longer needed.
1253 *
1254 * Returns 1 if successful or -1 on error
1255 */
1256 int libfdata_area_set_element_value_at_offset(
1257 libfdata_area_t *area,
1258 intptr_t *file_io_handle LIBFDATA_ATTRIBUTE_UNUSED,
1259 libfdata_cache_t *cache,
1260 off64_t element_value_offset,
1261 intptr_t *element_value,
1262 int (*free_element_value)(
1263 intptr_t **element_value,
1264 libcerror_error_t **error ),
1265 uint8_t write_flags,
1266 libcerror_error_t **error )
1267 {
1268 libfdata_internal_area_t *internal_area = NULL;
1269 libfdata_range_t *segment_data_range = NULL;
1270 static char *function = "libfdata_area_set_element_value_at_offset";
1271 off64_t element_data_offset = (off64_t) -1;
1272 uint32_t element_data_flags = 0;
1273 int cache_entry_index = -1;
1274 int element_data_file_index = -1;
1275 int element_index = -1;
1276 int number_of_cache_entries = 0;
1277
1278 LIBFDATA_UNREFERENCED_PARAMETER( file_io_handle )
1279
1280 if( area == NULL )
1281 {
1282 libcerror_error_set(
1283 error,
1284 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1285 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1286 "%s: invalid area.",
1287 function );
1288
1289 return( -1 );
1290 }
1291 internal_area = (libfdata_internal_area_t *) area;
1292
1293 if( internal_area->element_data_size == 0 )
1294 {
1295 libcerror_error_set(
1296 error,
1297 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1298 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1299 "%s: invalid area - element data size value out of bounds.",
1300 function );
1301
1302 return( -1 );
1303 }
1304 if( ( internal_area->size == 0 )
1305 || ( internal_area->size > (off64_t) INT64_MAX ) )
1306 {
1307 libcerror_error_set(
1308 error,
1309 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1310 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1311 "%s: invalid area - size value out of bounds.",
1312 function );
1313
1314 return( -1 );
1315 }
1316 if( ( (size64_t) element_value_offset > internal_area->size )
1317 || ( (size64_t) element_value_offset > ( internal_area->size - internal_area->element_data_size ) ) )
1318 {
1319 libcerror_error_set(
1320 error,
1321 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1322 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1323 "%s: invalid element value offset value out of bounds.",
1324 function );
1325
1326 return( -1 );
1327 }
1328 if( libfdata_segments_array_get_data_range_at_offset(
1329 internal_area->segments_array,
1330 element_value_offset,
1331 &element_data_offset,
1332 &segment_data_range,
1333 error ) != 1 )
1334 {
1335 libcerror_error_set(
1336 error,
1337 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1338 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1339 "%s: unable to retrieve segment data range for offset: 0x%08" PRIx64 ".",
1340 function,
1341 element_value_offset );
1342
1343 return( -1 );
1344 }
1345 if( segment_data_range == NULL )
1346 {
1347 libcerror_error_set(
1348 error,
1349 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1350 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1351 "%s: missing segment data range.",
1352 function );
1353
1354 return( -1 );
1355 }
1356 element_data_file_index = segment_data_range->file_index;
1357 element_data_offset += segment_data_range->offset;
1358 element_data_flags = segment_data_range->flags;
1359
1360 if( libfcache_cache_get_number_of_entries(
1361 (libfcache_cache_t *) cache,
1362 &number_of_cache_entries,
1363 error ) != 1 )
1364 {
1365 libcerror_error_set(
1366 error,
1367 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1368 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1369 "%s: unable to retrieve number of cache entries.",
1370 function );
1371
1372 return( -1 );
1373 }
1374 if( number_of_cache_entries <= 0 )
1375 {
1376 libcerror_error_set(
1377 error,
1378 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1379 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1380 "%s: invalid number of cache entries value out of bounds.",
1381 function );
1382
1383 return( -1 );
1384 }
1385 element_index = (int) ( element_value_offset / internal_area->element_data_size );
1386
1387 if( internal_area->calculate_cache_entry_index == NULL )
1388 {
1389 cache_entry_index = element_index % number_of_cache_entries;
1390 }
1391 else
1392 {
1393 cache_entry_index = internal_area->calculate_cache_entry_index(
1394 element_index,
1395 element_data_file_index,
1396 element_data_offset,
1397 internal_area->element_data_size,
1398 element_data_flags,
1399 number_of_cache_entries );
1400 }
1401 if( libfcache_cache_set_value_by_index(
1402 (libfcache_cache_t *) cache,
1403 cache_entry_index,
1404 element_data_file_index,
1405 element_data_offset,
1406 internal_area->timestamp,
1407 element_value,
1408 free_element_value,
1409 write_flags,
1410 error ) != 1 )
1411 {
1412 libcerror_error_set(
1413 error,
1414 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1415 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1416 "%s: unable to set value in cache entry: %d.",
1417 function,
1418 cache_entry_index );
1419
1420 return( -1 );
1421 }
1422 return( 1 );
1423 }
1424
1425 /* Retrieves the size
1426 * Returns 1 if successful or -1 on error
1427 */
1428 int libfdata_area_get_size(
1429 libfdata_area_t *area,
1430 size64_t *size,
1431 libcerror_error_t **error )
1432 {
1433 libfdata_internal_area_t *internal_area = NULL;
1434 static char *function = "libfdata_area_get_size";
1435
1436 if( area == NULL )
1437 {
1438 libcerror_error_set(
1439 error,
1440 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1441 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1442 "%s: invalid area.",
1443 function );
1444
1445 return( -1 );
1446 }
1447 internal_area = (libfdata_internal_area_t *) area;
1448
1449 if( size == NULL )
1450 {
1451 libcerror_error_set(
1452 error,
1453 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1454 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1455 "%s: invalid size.",
1456 function );
1457
1458 return( -1 );
1459 }
1460 if( ( internal_area->flags & LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES ) != 0 )
1461 {
1462 if( libfdata_segments_array_calculate_mapped_ranges(
1463 internal_area->segments_array,
1464 internal_area->mapped_ranges_array,
1465 error ) != 1 )
1466 {
1467 libcerror_error_set(
1468 error,
1469 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1470 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1471 "%s: unable to calculate mapped ranges.",
1472 function );
1473
1474 return( -1 );
1475 }
1476 internal_area->flags &= ~( LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES );
1477 }
1478 *size = internal_area->size;
1479
1480 return( 1 );
1481 }
1482