Debian Codegraph @

libfdata/libfdata_list.c

raw symbols SCIP
1 /*
2 * The list 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_definitions.h"
27 #include "libfdata_libcdata.h"
28 #include "libfdata_libcerror.h"
29 #include "libfdata_libcnotify.h"
30 #include "libfdata_libfcache.h"
31 #include "libfdata_list.h"
32 #include "libfdata_list_element.h"
33 #include "libfdata_mapped_range.h"
34 #include "libfdata_range.h"
35 #include "libfdata_types.h"
36 #include "libfdata_unused.h"
37
38 /* Creates a list
39 * Make sure the value list is referencing, is set to NULL
40 *
41 * If the flag LIBFDATA_DATA_HANDLE_FLAG_MANAGED is set the list
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_list_initialize(
48 libfdata_list_t **list,
49 intptr_t *data_handle,
50 int (*free_data_handle)(
51 intptr_t **data_handle,
52 libcerror_error_t **error ),
53 int (*clone_data_handle)(
54 intptr_t **destination_data_handle,
55 intptr_t *source_data_handle,
56 libcerror_error_t **error ),
57 int (*read_element_data)(
58 intptr_t *data_handle,
59 intptr_t *file_io_handle,
60 libfdata_list_element_t *list_element,
61 libfdata_cache_t *cache,
62 int element_data_file_index,
63 off64_t element_data_offset,
64 size64_t element_data_size,
65 uint32_t element_data_flags,
66 uint8_t read_flags,
67 libcerror_error_t **error ),
68 int (*write_element_data)(
69 intptr_t *data_handle,
70 intptr_t *file_io_handle,
71 libfdata_list_element_t *list_element,
72 libfdata_cache_t *cache,
73 int element_data_file_index,
74 off64_t element_data_offset,
75 size64_t element_data_size,
76 uint32_t element_data_flags,
77 uint8_t write_flags,
78 libcerror_error_t **error ),
79 uint8_t flags,
80 libcerror_error_t **error )
81 {
82 libfdata_internal_list_t *internal_list = NULL;
83 static char *function = "libfdata_list_initialize";
84
85 if( list == NULL )
86 {
87 libcerror_error_set(
88 error,
89 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
90 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
91 "%s: invalid list.",
92 function );
93
94 return( -1 );
95 }
96 if( *list != NULL )
97 {
98 libcerror_error_set(
99 error,
100 LIBCERROR_ERROR_DOMAIN_RUNTIME,
101 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
102 "%s: invalid list value already set.",
103 function );
104
105 return( -1 );
106 }
107 if( ( flags & 0xfe ) != 0 )
108 {
109 libcerror_error_set(
110 error,
111 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
112 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
113 "%s: unsupported flags: 0x%02" PRIx8 ".",
114 function );
115
116 return( -1 );
117 }
118 internal_list = memory_allocate_structure(
119 libfdata_internal_list_t );
120
121 if( internal_list == NULL )
122 {
123 libcerror_error_set(
124 error,
125 LIBCERROR_ERROR_DOMAIN_MEMORY,
126 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
127 "%s: unable to create list.",
128 function );
129
130 goto on_error;
131 }
132 if( memory_set(
133 internal_list,
134 0,
135 sizeof( libfdata_internal_list_t ) ) == NULL )
136 {
137 libcerror_error_set(
138 error,
139 LIBCERROR_ERROR_DOMAIN_MEMORY,
140 LIBCERROR_MEMORY_ERROR_SET_FAILED,
141 "%s: unable to clear list.",
142 function );
143
144 memory_free(
145 internal_list );
146
147 return( -1 );
148 }
149 if( libcdata_array_initialize(
150 &( internal_list->elements_array ),
151 0,
152 error ) != 1 )
153 {
154 libcerror_error_set(
155 error,
156 LIBCERROR_ERROR_DOMAIN_RUNTIME,
157 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
158 "%s: unable to create elements array.",
159 function );
160
161 goto on_error;
162 }
163 if( libcdata_array_initialize(
164 &( internal_list->mapped_ranges_array ),
165 0,
166 error ) != 1 )
167 {
168 libcerror_error_set(
169 error,
170 LIBCERROR_ERROR_DOMAIN_RUNTIME,
171 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
172 "%s: unable to create mapped ranges array.",
173 function );
174
175 goto on_error;
176 }
177 internal_list->flags |= flags;
178 internal_list->data_handle = data_handle;
179 internal_list->free_data_handle = free_data_handle;
180 internal_list->clone_data_handle = clone_data_handle;
181 internal_list->read_element_data = read_element_data;
182 internal_list->write_element_data = write_element_data;
183
184 *list = (libfdata_list_t *) internal_list;
185
186 return( 1 );
187
188 on_error:
189 if( internal_list != NULL )
190 {
191 if( internal_list->elements_array != NULL )
192 {
193 libcdata_array_free(
194 &( internal_list->elements_array ),
195 NULL,
196 NULL );
197 }
198 memory_free(
199 internal_list );
200 }
201 return( -1 );
202 }
203
204 /* Frees a list
205 * Returns 1 if successful or -1 on error
206 */
207 int libfdata_list_free(
208 libfdata_list_t **list,
209 libcerror_error_t **error )
210 {
211 libfdata_internal_list_t *internal_list = NULL;
212 static char *function = "libfdata_list_free";
213 int result = 1;
214
215 if( list == NULL )
216 {
217 libcerror_error_set(
218 error,
219 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
220 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
221 "%s: invalid list.",
222 function );
223
224 return( -1 );
225 }
226 if( *list != NULL )
227 {
228 internal_list = (libfdata_internal_list_t *) *list;
229 *list = NULL;
230
231 if( libcdata_array_free(
232 &( internal_list->elements_array ),
233 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_list_element_free,
234 error ) != 1 )
235 {
236 libcerror_error_set(
237 error,
238 LIBCERROR_ERROR_DOMAIN_RUNTIME,
239 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
240 "%s: unable to free the elements array.",
241 function );
242
243 result = -1;
244 }
245 if( libcdata_array_free(
246 &( internal_list->mapped_ranges_array ),
247 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
248 error ) != 1 )
249 {
250 libcerror_error_set(
251 error,
252 LIBCERROR_ERROR_DOMAIN_RUNTIME,
253 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
254 "%s: unable to free the mapped ranges array.",
255 function );
256
257 result = -1;
258 }
259 if( ( internal_list->flags & LIBFDATA_DATA_HANDLE_FLAG_MANAGED ) != 0 )
260 {
261 if( internal_list->data_handle != NULL )
262 {
263 if( internal_list->free_data_handle == NULL )
264 {
265 libcerror_error_set(
266 error,
267 LIBCERROR_ERROR_DOMAIN_RUNTIME,
268 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
269 "%s: invalid list - missing free data handle function.",
270 function );
271
272 result = -1;
273 }
274 else if( internal_list->free_data_handle(
275 &( internal_list->data_handle ),
276 error ) != 1 )
277 {
278 libcerror_error_set(
279 error,
280 LIBCERROR_ERROR_DOMAIN_RUNTIME,
281 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
282 "%s: unable to free data handle.",
283 function );
284
285 result = -1;
286 }
287 }
288 }
289 memory_free(
290 internal_list );
291 }
292 return( result );
293 }
294
295 /* Clones (duplicates) the list
296 * Returns 1 if successful or -1 on error
297 */
298 int libfdata_list_clone(
299 libfdata_list_t **destination_list,
300 libfdata_list_t *source_list,
301 libcerror_error_t **error )
302 {
303 libfdata_internal_list_t *internal_destination_list = NULL;
304 libfdata_internal_list_t *internal_source_list = NULL;
305 static char *function = "libfdata_list_clone";
306
307 if( destination_list == NULL )
308 {
309 libcerror_error_set(
310 error,
311 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
312 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
313 "%s: invalid destination list.",
314 function );
315
316 return( -1 );
317 }
318 if( *destination_list != NULL )
319 {
320 libcerror_error_set(
321 error,
322 LIBCERROR_ERROR_DOMAIN_RUNTIME,
323 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
324 "%s: invalid destination list value already set.",
325 function );
326
327 return( -1 );
328 }
329 if( source_list == NULL )
330 {
331 *destination_list = NULL;
332
333 return( 1 );
334 }
335 internal_source_list = (libfdata_internal_list_t *) source_list;
336
337 internal_destination_list = memory_allocate_structure(
338 libfdata_internal_list_t );
339
340 if( internal_destination_list == NULL )
341 {
342 libcerror_error_set(
343 error,
344 LIBCERROR_ERROR_DOMAIN_MEMORY,
345 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
346 "%s: unable to create destination list.",
347 function );
348
349 goto on_error;
350 }
351 if( memory_set(
352 internal_destination_list,
353 0,
354 sizeof( libfdata_internal_list_t ) ) == NULL )
355 {
356 libcerror_error_set(
357 error,
358 LIBCERROR_ERROR_DOMAIN_MEMORY,
359 LIBCERROR_MEMORY_ERROR_SET_FAILED,
360 "%s: unable to clear destination list.",
361 function );
362
363 memory_free(
364 internal_destination_list );
365
366 return( -1 );
367 }
368 if( internal_source_list->data_handle != NULL )
369 {
370 if( internal_source_list->free_data_handle == NULL )
371 {
372 libcerror_error_set(
373 error,
374 LIBCERROR_ERROR_DOMAIN_RUNTIME,
375 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
376 "%s: invalid source list - missing free data handle function.",
377 function );
378
379 goto on_error;
380 }
381 if( internal_source_list->clone_data_handle == NULL )
382 {
383 libcerror_error_set(
384 error,
385 LIBCERROR_ERROR_DOMAIN_RUNTIME,
386 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
387 "%s: invalid source list - missing clone data handle function.",
388 function );
389
390 goto on_error;
391 }
392 if( internal_source_list->clone_data_handle(
393 &( internal_destination_list->data_handle ),
394 internal_source_list->data_handle,
395 error ) != 1 )
396 {
397 libcerror_error_set(
398 error,
399 LIBCERROR_ERROR_DOMAIN_RUNTIME,
400 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
401 "%s: unable to clone data handle.",
402 function );
403
404 goto on_error;
405 }
406 }
407 /* TODO set destination list in destination elements */
408 if( libcdata_array_clone(
409 &( internal_destination_list->elements_array ),
410 internal_source_list->elements_array,
411 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_list_element_free,
412 (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) &libfdata_list_element_clone,
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 elements array.",
420 function );
421
422 goto on_error;
423 }
424 if( libcdata_array_clone(
425 &( internal_destination_list->mapped_ranges_array ),
426 internal_source_list->mapped_ranges_array,
427 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
428 (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) &libfdata_mapped_range_clone,
429 error ) != 1 )
430 {
431 libcerror_error_set(
432 error,
433 LIBCERROR_ERROR_DOMAIN_RUNTIME,
434 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
435 "%s: unable to create destination mapped ranges array.",
436 function );
437
438 goto on_error;
439 }
440 internal_destination_list->flags = internal_source_list->flags | LIBFDATA_DATA_HANDLE_FLAG_MANAGED;
441 internal_destination_list->free_data_handle = internal_source_list->free_data_handle;
442 internal_destination_list->clone_data_handle = internal_source_list->clone_data_handle;
443 internal_destination_list->read_element_data = internal_source_list->read_element_data;
444 internal_destination_list->write_element_data = internal_source_list->write_element_data;
445
446 *destination_list = (libfdata_list_t *) internal_destination_list;
447
448 return( 1 );
449
450 on_error:
451 if( internal_destination_list != NULL )
452 {
453 if( internal_destination_list->elements_array != NULL )
454 {
455 libcdata_array_free(
456 &( internal_destination_list->elements_array ),
457 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_list_element_free,
458 NULL );
459 }
460 if( ( internal_destination_list->data_handle != NULL )
461 && ( internal_source_list->free_data_handle != NULL ) )
462 {
463 internal_source_list->free_data_handle(
464 &( internal_destination_list->data_handle ),
465 NULL );
466 }
467 memory_free(
468 internal_destination_list );
469 }
470 return( -1 );
471 }
472
473 /* Sets the calculate mapped ranges flag
474 * Returns 1 if successful or -1 on error
475 */
476 int libfdata_list_set_calculate_mapped_ranges_flag(
477 libfdata_list_t *list,
478 libcerror_error_t **error )
479 {
480 libfdata_internal_list_t *internal_list = NULL;
481 static char *function = "libfdata_list_set_calculate_mapped_ranges_flag";
482
483 if( list == NULL )
484 {
485 libcerror_error_set(
486 error,
487 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
488 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
489 "%s: invalid list.",
490 function );
491
492 return( -1 );
493 }
494 internal_list = (libfdata_internal_list_t *) list;
495
496 internal_list->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
497
498 return( 1 );
499 }
500
501 /* List elements functions
502 */
503
504 /* Empties the list
505 * Returns 1 if successful or -1 on error
506 */
507 int libfdata_list_empty(
508 libfdata_list_t *list,
509 libcerror_error_t **error )
510 {
511 libfdata_internal_list_t *internal_list = NULL;
512 static char *function = "libfdata_list_empty";
513
514 if( list == NULL )
515 {
516 libcerror_error_set(
517 error,
518 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
519 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
520 "%s: invalid list.",
521 function );
522
523 return( -1 );
524 }
525 internal_list = (libfdata_internal_list_t *) list;
526
527 if( libcdata_array_empty(
528 internal_list->elements_array,
529 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_list_element_free,
530 error ) != 1 )
531 {
532 libcerror_error_set(
533 error,
534 LIBCERROR_ERROR_DOMAIN_RUNTIME,
535 LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
536 "%s: unable to empty elements array.",
537 function );
538
539 return( -1 );
540 }
541 if( libcdata_array_empty(
542 internal_list->mapped_ranges_array,
543 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
544 error ) != 1 )
545 {
546 libcerror_error_set(
547 error,
548 LIBCERROR_ERROR_DOMAIN_RUNTIME,
549 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
550 "%s: unable to empty mapped ranges array.",
551 function );
552
553 return( -1 );
554 }
555 internal_list->size = 0;
556
557 return( 1 );
558 }
559
560 /* Resizes the list
561 * Returns 1 if successful or -1 on error
562 */
563 int libfdata_list_resize(
564 libfdata_list_t *list,
565 int number_of_elements,
566 libcerror_error_t **error )
567 {
568 libfdata_internal_list_t *internal_list = NULL;
569 static char *function = "libfdata_list_resize";
570
571 if( list == NULL )
572 {
573 libcerror_error_set(
574 error,
575 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
576 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
577 "%s: invalid list.",
578 function );
579
580 return( -1 );
581 }
582 internal_list = (libfdata_internal_list_t *) list;
583
584 if( libcdata_array_resize(
585 internal_list->elements_array,
586 number_of_elements,
587 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_list_element_free,
588 error ) != 1 )
589 {
590 libcerror_error_set(
591 error,
592 LIBCERROR_ERROR_DOMAIN_RUNTIME,
593 LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
594 "%s: unable to resize elements array.",
595 function );
596
597 return( -1 );
598 }
599 if( libcdata_array_resize(
600 internal_list->mapped_ranges_array,
601 number_of_elements,
602 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
603 error ) != 1 )
604 {
605 libcerror_error_set(
606 error,
607 LIBCERROR_ERROR_DOMAIN_RUNTIME,
608 LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
609 "%s: unable to resize mapped ranges array.",
610 function );
611
612 return( -1 );
613 }
614 internal_list->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
615
616 return( 1 );
617 }
618
619 /* Reverses the order of the elements
620 * Returns 1 if successful or -1 on error
621 */
622 int libfdata_list_reverse(
623 libfdata_list_t *list,
624 libcerror_error_t **error )
625 {
626 libfdata_internal_list_t *internal_list = NULL;
627 libfdata_list_element_t *list_element = NULL;
628 static char *function = "libfdata_list_reverse";
629 int element_index = 0;
630 int number_of_elements = 0;
631
632 if( list == NULL )
633 {
634 libcerror_error_set(
635 error,
636 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
637 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
638 "%s: invalid list.",
639 function );
640
641 return( -1 );
642 }
643 internal_list = (libfdata_internal_list_t *) list;
644
645 if( libcdata_array_reverse(
646 internal_list->elements_array,
647 error ) != 1 )
648 {
649 libcerror_error_set(
650 error,
651 LIBCERROR_ERROR_DOMAIN_RUNTIME,
652 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
653 "%s: unable to reverse elements array.",
654 function );
655
656 return( -1 );
657 }
658 if( libcdata_array_get_number_of_entries(
659 internal_list->elements_array,
660 &number_of_elements,
661 error ) != 1 )
662 {
663 libcerror_error_set(
664 error,
665 LIBCERROR_ERROR_DOMAIN_RUNTIME,
666 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
667 "%s: unable to retrieve number of elements from elements array.",
668 function );
669
670 return( -1 );
671 }
672 for( element_index = 0;
673 element_index < number_of_elements;
674 element_index++ )
675 {
676 if( libcdata_array_get_entry_by_index(
677 internal_list->elements_array,
678 element_index,
679 (intptr_t **) &list_element,
680 error ) != 1 )
681 {
682 libcerror_error_set(
683 error,
684 LIBCERROR_ERROR_DOMAIN_RUNTIME,
685 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
686 "%s: unable to retrieve entry: %d from elements array.",
687 function,
688 element_index );
689
690 return( -1 );
691 }
692 if( libfdata_list_element_set_element_index(
693 list_element,
694 element_index,
695 error ) != 1 )
696 {
697 libcerror_error_set(
698 error,
699 LIBCERROR_ERROR_DOMAIN_RUNTIME,
700 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
701 "%s: unable to set list element: %d index.",
702 function,
703 element_index );
704
705 return( -1 );
706 }
707 }
708 internal_list->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
709
710 return( 1 );
711 }
712
713 /* Retrieves the number of elements of the list
714 * Returns 1 if successful or -1 on error
715 */
716 int libfdata_list_get_number_of_elements(
717 libfdata_list_t *list,
718 int *number_of_elements,
719 libcerror_error_t **error )
720 {
721 libfdata_internal_list_t *internal_list = NULL;
722 static char *function = "libfdata_list_get_number_of_elements";
723
724 if( list == NULL )
725 {
726 libcerror_error_set(
727 error,
728 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
729 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
730 "%s: invalid list.",
731 function );
732
733 return( -1 );
734 }
735 internal_list = (libfdata_internal_list_t *) list;
736
737 if( libcdata_array_get_number_of_entries(
738 internal_list->elements_array,
739 number_of_elements,
740 error ) != 1 )
741 {
742 libcerror_error_set(
743 error,
744 LIBCERROR_ERROR_DOMAIN_RUNTIME,
745 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
746 "%s: unable to retrieve number of elements from elements array.",
747 function );
748
749 return( -1 );
750 }
751 return( 1 );
752 }
753
754 /* Retrieves a specific list element
755 * Returns 1 if successful or -1 on error
756 */
757 int libfdata_list_get_list_element_by_index(
758 libfdata_list_t *list,
759 int element_index,
760 libfdata_list_element_t **element,
761 libcerror_error_t **error )
762 {
763 libfdata_internal_list_t *internal_list = NULL;
764 static char *function = "libfdata_list_get_list_element_by_index";
765
766 if( list == NULL )
767 {
768 libcerror_error_set(
769 error,
770 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
771 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
772 "%s: invalid list.",
773 function );
774
775 return( -1 );
776 }
777 internal_list = (libfdata_internal_list_t *) list;
778
779 if( libcdata_array_get_entry_by_index(
780 internal_list->elements_array,
781 element_index,
782 (intptr_t **) element,
783 error ) != 1 )
784 {
785 libcerror_error_set(
786 error,
787 LIBCERROR_ERROR_DOMAIN_RUNTIME,
788 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
789 "%s: unable to retrieve entry: %d from elements array.",
790 function,
791 element_index );
792
793 return( -1 );
794 }
795 internal_list->current_element_index = element_index;
796
797 return( 1 );
798 }
799
800 /* Retrieves the data range of a specific element
801 * Returns 1 if successful or -1 on error
802 */
803 int libfdata_list_get_element_by_index(
804 libfdata_list_t *list,
805 int element_index,
806 int *element_file_index,
807 off64_t *element_offset,
808 size64_t *element_size,
809 uint32_t *element_flags,
810 libcerror_error_t **error )
811 {
812 libfdata_internal_list_t *internal_list = NULL;
813 libfdata_list_element_t *list_element = NULL;
814 static char *function = "libfdata_list_get_element_by_index";
815
816 if( list == NULL )
817 {
818 libcerror_error_set(
819 error,
820 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
821 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
822 "%s: invalid list.",
823 function );
824
825 return( -1 );
826 }
827 internal_list = (libfdata_internal_list_t *) list;
828
829 if( libcdata_array_get_entry_by_index(
830 internal_list->elements_array,
831 element_index,
832 (intptr_t **) &list_element,
833 error ) != 1 )
834 {
835 libcerror_error_set(
836 error,
837 LIBCERROR_ERROR_DOMAIN_RUNTIME,
838 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
839 "%s: unable to retrieve entry: %d from elements array.",
840 function,
841 element_index );
842
843 return( -1 );
844 }
845 if( libfdata_list_element_get_data_range(
846 list_element,
847 element_file_index,
848 element_offset,
849 element_size,
850 element_flags,
851 error ) != 1 )
852 {
853 libcerror_error_set(
854 error,
855 LIBCERROR_ERROR_DOMAIN_RUNTIME,
856 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
857 "%s: unable to retrieve data range from list element: %d.",
858 function,
859 element_index );
860
861 return( -1 );
862 }
863 internal_list->current_element_index = element_index;
864
865 return( 1 );
866 }
867
868 /* Sets the data range of a specific element
869 * Returns 1 if successful or -1 on error
870 */
871 int libfdata_list_set_element_by_index(
872 libfdata_list_t *list,
873 int element_index,
874 int element_file_index,
875 off64_t element_offset,
876 size64_t element_size,
877 uint32_t element_flags,
878 libcerror_error_t **error )
879 {
880 libfdata_internal_list_t *internal_list = NULL;
881 libfdata_list_element_t *list_element = NULL;
882 libfdata_mapped_range_t *mapped_range = NULL;
883 static char *function = "libfdata_list_set_element_by_index";
884 off64_t previous_element_offset = 0;
885 size64_t mapped_size = 0;
886 size64_t previous_element_size = 0;
887 uint32_t previous_element_flags = 0;
888 int previous_element_file_index = 0;
889 int result = 0;
890
891 #if defined( HAVE_DEBUG_OUTPUT )
892 off64_t mapped_range_offset = 0;
893 size64_t mapped_range_size = 0;
894 #endif
895
896 if( list == NULL )
897 {
898 libcerror_error_set(
899 error,
900 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
901 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
902 "%s: invalid list.",
903 function );
904
905 return( -1 );
906 }
907 internal_list = (libfdata_internal_list_t *) list;
908
909 if( libcdata_array_get_entry_by_index(
910 internal_list->elements_array,
911 element_index,
912 (intptr_t **) &list_element,
913 error ) != 1 )
914 {
915 libcerror_error_set(
916 error,
917 LIBCERROR_ERROR_DOMAIN_RUNTIME,
918 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
919 "%s: unable to retrieve entry: %d from elements array.",
920 function,
921 element_index );
922
923 return( -1 );
924 }
925 if( list_element == NULL )
926 {
927 if( libfdata_list_element_initialize(
928 &list_element,
929 list,
930 element_index,
931 error ) != 1 )
932 {
933 libcerror_error_set(
934 error,
935 LIBCERROR_ERROR_DOMAIN_RUNTIME,
936 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
937 "%s: unable to create list element.",
938 function );
939
940 return( -1 );
941 }
942 if( libcdata_array_set_entry_by_index(
943 internal_list->elements_array,
944 element_index,
945 (intptr_t *) list_element,
946 error ) != 1 )
947 {
948 libcerror_error_set(
949 error,
950 LIBCERROR_ERROR_DOMAIN_RUNTIME,
951 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
952 "%s: unable to set entry: %d in elements array.",
953 function,
954 element_index );
955
956 libfdata_list_element_free(
957 &list_element,
958 NULL );
959
960 return( -1 );
961 }
962 }
963 else
964 {
965 result = libfdata_list_element_get_mapped_size(
966 list_element,
967 &mapped_size,
968 error );
969
970 if( result == -1 )
971 {
972 libcerror_error_set(
973 error,
974 LIBCERROR_ERROR_DOMAIN_RUNTIME,
975 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
976 "%s: unable to retrieve mapped size of list element: %d.",
977 function,
978 element_index );
979
980 return( -1 );
981 }
982 else if( result == 0 )
983 {
984 if( libfdata_list_element_get_data_range(
985 list_element,
986 &previous_element_file_index,
987 &previous_element_offset,
988 &previous_element_size,
989 &previous_element_flags,
990 error ) != 1 )
991 {
992 libcerror_error_set(
993 error,
994 LIBCERROR_ERROR_DOMAIN_RUNTIME,
995 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
996 "%s: unable to retrieve data range of list element: %d.",
997 function,
998 element_index );
999
1000 return( -1 );
1001 }
1002 }
1003 }
1004 if( libfdata_list_element_set_data_range(
1005 list_element,
1006 element_file_index,
1007 element_offset,
1008 element_size,
1009 element_flags,
1010 error ) != 1 )
1011 {
1012 libcerror_error_set(
1013 error,
1014 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1015 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1016 "%s: unable to set data range of list element: %d.",
1017 function,
1018 element_index );
1019
1020 return( -1 );
1021 }
1022 /* Make sure the list has a mapped range entry for every element
1023 */
1024 if( libcdata_array_get_entry_by_index(
1025 internal_list->mapped_ranges_array,
1026 element_index,
1027 (intptr_t **) &mapped_range,
1028 error ) != 1 )
1029 {
1030 libcerror_error_set(
1031 error,
1032 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1033 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1034 "%s: unable to retrieve entry: %d from mapped ranges array.",
1035 function,
1036 element_index );
1037
1038 return( -1 );
1039 }
1040 if( mapped_range == NULL )
1041 {
1042 if( libfdata_mapped_range_initialize(
1043 &mapped_range,
1044 error ) != 1 )
1045 {
1046 libcerror_error_set(
1047 error,
1048 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1049 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1050 "%s: unable to create mapped range.",
1051 function );
1052
1053 return( -1 );
1054 }
1055 if( libcdata_array_set_entry_by_index(
1056 internal_list->mapped_ranges_array,
1057 element_index,
1058 (intptr_t *) mapped_range,
1059 error ) != 1 )
1060 {
1061 libcerror_error_set(
1062 error,
1063 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1064 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1065 "%s: unable to set entry: %d in mapped ranges array.",
1066 function,
1067 element_index );
1068
1069 libfdata_mapped_range_free(
1070 &mapped_range,
1071 NULL );
1072
1073 return( -1 );
1074 }
1075 internal_list->size += element_size;
1076 internal_list->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
1077 }
1078 /* If the size of the element is mapped or if the element size did not change
1079 * there is no need to recalculate the mapped range
1080 */
1081 else if( ( mapped_size == 0 )
1082 && ( previous_element_size != element_size ) )
1083 {
1084 internal_list->size -= previous_element_size;
1085 internal_list->size += element_size;
1086 internal_list->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
1087 }
1088 #if defined( HAVE_DEBUG_OUTPUT )
1089 if( libcnotify_verbose != 0 )
1090 {
1091 libcnotify_printf(
1092 "%s: element: %03d\tfile index: %03d offset: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
1093 function,
1094 element_index,
1095 element_file_index,
1096 element_offset,
1097 element_offset + element_size,
1098 element_offset,
1099 element_offset + element_size,
1100 element_size );
1101
1102 if( ( internal_list->flags & LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES ) == 0 )
1103 {
1104 if( libfdata_mapped_range_get(
1105 mapped_range,
1106 &mapped_range_offset,
1107 &mapped_range_size,
1108 error ) != 1 )
1109 {
1110 libcerror_error_set(
1111 error,
1112 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1113 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1114 "%s: unable to retrieve values from mapped range: %d.",
1115 function,
1116 element_index );
1117
1118 return( -1 );
1119 }
1120 libcnotify_printf(
1121 "%s: element: %03d\tmapped range: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
1122 function,
1123 element_index,
1124 mapped_range_offset,
1125 mapped_range_offset + mapped_range_size,
1126 mapped_range_offset,
1127 mapped_range_offset + mapped_range_size,
1128 mapped_range_size );
1129 }
1130 libcnotify_printf(
1131 "\n" );
1132 }
1133 #endif /* defined( HAVE_DEBUG_OUTPUT ) */
1134
1135 internal_list->current_element_index = element_index;
1136
1137 return( 1 );
1138 }
1139
1140 /* Prepends an element data range
1141 * Returns 1 if successful or -1 on error
1142 */
1143 int libfdata_list_prepend_element(
1144 libfdata_list_t *list,
1145 int element_file_index,
1146 off64_t element_offset,
1147 size64_t element_size,
1148 uint32_t element_flags,
1149 libcerror_error_t **error )
1150 {
1151 libfdata_internal_list_t *internal_list = NULL;
1152 libfdata_list_element_t *list_element = NULL;
1153 libfdata_mapped_range_t *mapped_range = NULL;
1154 static char *function = "libfdata_list_prepend_element";
1155 off64_t mapped_offset = 0;
1156 int element_index = 0;
1157 int mapped_range_index = -1;
1158 int number_of_elements = 0;
1159
1160 if( list == NULL )
1161 {
1162 libcerror_error_set(
1163 error,
1164 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1165 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1166 "%s: invalid list.",
1167 function );
1168
1169 return( -1 );
1170 }
1171 internal_list = (libfdata_internal_list_t *) list;
1172
1173 if( libfdata_mapped_range_initialize(
1174 &mapped_range,
1175 error ) != 1 )
1176 {
1177 libcerror_error_set(
1178 error,
1179 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1180 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1181 "%s: unable to create mapped range.",
1182 function );
1183
1184 goto on_error;
1185 }
1186 mapped_offset = internal_list->mapped_offset + (off64_t) internal_list->size;
1187
1188 if( libfdata_mapped_range_set(
1189 mapped_range,
1190 mapped_offset,
1191 element_size,
1192 error ) != 1 )
1193 {
1194 libcerror_error_set(
1195 error,
1196 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1197 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1198 "%s: unable to set mapped range values.",
1199 function );
1200
1201 goto on_error;
1202 }
1203 if( libcdata_array_append_entry(
1204 internal_list->mapped_ranges_array,
1205 &mapped_range_index,
1206 (intptr_t *) mapped_range,
1207 error ) != 1 )
1208 {
1209 libcerror_error_set(
1210 error,
1211 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1212 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1213 "%s: unable to append mapped range to array.",
1214 function );
1215
1216 goto on_error;
1217 }
1218 if( libfdata_list_element_initialize(
1219 &list_element,
1220 list,
1221 0,
1222 error ) != 1 )
1223 {
1224 libcerror_error_set(
1225 error,
1226 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1227 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1228 "%s: unable to create list element.",
1229 function );
1230
1231 goto on_error;
1232 }
1233 if( libfdata_list_element_set_data_range(
1234 list_element,
1235 element_file_index,
1236 element_offset,
1237 element_size,
1238 element_flags,
1239 error ) != 1 )
1240 {
1241 libcerror_error_set(
1242 error,
1243 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1244 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1245 "%s: unable to set data range of list element.",
1246 function );
1247
1248 goto on_error;
1249 }
1250 if( libcdata_array_prepend_entry(
1251 internal_list->elements_array,
1252 (intptr_t *) list_element,
1253 error ) != 1 )
1254 {
1255 libcerror_error_set(
1256 error,
1257 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1258 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1259 "%s: unable to prepend list element to elements array.",
1260 function );
1261
1262 goto on_error;
1263 }
1264 mapped_range_index = -1;
1265 mapped_range = NULL;
1266
1267 if( libcdata_array_get_number_of_entries(
1268 internal_list->elements_array,
1269 &number_of_elements,
1270 error ) != 1 )
1271 {
1272 libcerror_error_set(
1273 error,
1274 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1275 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1276 "%s: unable to retrieve number of entries from elements array.",
1277 function );
1278
1279 goto on_error;
1280 }
1281 for( element_index = 0;
1282 element_index < number_of_elements;
1283 element_index++ )
1284 {
1285 if( libcdata_array_get_entry_by_index(
1286 internal_list->elements_array,
1287 element_index,
1288 (intptr_t **) &list_element,
1289 error ) != 1 )
1290 {
1291 libcerror_error_set(
1292 error,
1293 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1294 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1295 "%s: unable to retrieve entry: %d from elements array.",
1296 function,
1297 element_index );
1298
1299 list_element = NULL;
1300
1301 goto on_error;
1302 }
1303 if( libfdata_list_element_set_element_index(
1304 list_element,
1305 element_index,
1306 error ) != 1 )
1307 {
1308 libcerror_error_set(
1309 error,
1310 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1311 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1312 "%s: unable to set list element: %d index.",
1313 function,
1314 element_index );
1315
1316 list_element = NULL;
1317
1318 goto on_error;
1319 }
1320 }
1321 internal_list->current_element_index = 0;
1322 internal_list->size += element_size;
1323 internal_list->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
1324
1325 return( 1 );
1326
1327 on_error:
1328 if( list_element != NULL )
1329 {
1330 libfdata_list_element_free(
1331 &list_element,
1332 NULL );
1333 }
1334 if( mapped_range_index != -1 )
1335 {
1336 libcdata_array_set_entry_by_index(
1337 internal_list->mapped_ranges_array,
1338 mapped_range_index,
1339 NULL,
1340 NULL );
1341 }
1342 if( mapped_range != NULL )
1343 {
1344 libfdata_mapped_range_free(
1345 &mapped_range,
1346 NULL );
1347 }
1348 return( -1 );
1349 }
1350
1351 /* Appends an element data range
1352 * Returns 1 if successful or -1 on error
1353 */
1354 int libfdata_list_append_element(
1355 libfdata_list_t *list,
1356 int *element_index,
1357 int element_file_index,
1358 off64_t element_offset,
1359 size64_t element_size,
1360 uint32_t element_flags,
1361 libcerror_error_t **error )
1362 {
1363 libfdata_internal_list_t *internal_list = NULL;
1364 libfdata_list_element_t *list_element = NULL;
1365 libfdata_mapped_range_t *mapped_range = NULL;
1366 static char *function = "libfdata_list_append_element";
1367 off64_t mapped_offset = 0;
1368 int mapped_range_index = -1;
1369
1370 if( list == NULL )
1371 {
1372 libcerror_error_set(
1373 error,
1374 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1375 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1376 "%s: invalid list.",
1377 function );
1378
1379 return( -1 );
1380 }
1381 internal_list = (libfdata_internal_list_t *) list;
1382
1383 if( element_index == NULL )
1384 {
1385 libcerror_error_set(
1386 error,
1387 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1388 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1389 "%s: invalid element index.",
1390 function );
1391
1392 return( -1 );
1393 }
1394 if( libfdata_mapped_range_initialize(
1395 &mapped_range,
1396 error ) != 1 )
1397 {
1398 libcerror_error_set(
1399 error,
1400 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1401 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1402 "%s: unable to create mapped range.",
1403 function );
1404
1405 goto on_error;
1406 }
1407 mapped_offset = internal_list->mapped_offset + (off64_t) internal_list->size;
1408
1409 if( libfdata_mapped_range_set(
1410 mapped_range,
1411 mapped_offset,
1412 element_size,
1413 error ) != 1 )
1414 {
1415 libcerror_error_set(
1416 error,
1417 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1418 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1419 "%s: unable to set mapped range values.",
1420 function );
1421
1422 goto on_error;
1423 }
1424 if( libcdata_array_append_entry(
1425 internal_list->mapped_ranges_array,
1426 &mapped_range_index,
1427 (intptr_t *) mapped_range,
1428 error ) != 1 )
1429 {
1430 libcerror_error_set(
1431 error,
1432 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1433 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1434 "%s: unable to append mapped range to array.",
1435 function );
1436
1437 goto on_error;
1438 }
1439 if( libfdata_list_element_initialize(
1440 &list_element,
1441 list,
1442 0,
1443 error ) != 1 )
1444 {
1445 libcerror_error_set(
1446 error,
1447 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1448 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1449 "%s: unable to create list element.",
1450 function );
1451
1452 goto on_error;
1453 }
1454 if( libfdata_list_element_set_data_range(
1455 list_element,
1456 element_file_index,
1457 element_offset,
1458 element_size,
1459 element_flags,
1460 error ) != 1 )
1461 {
1462 libcerror_error_set(
1463 error,
1464 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1465 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1466 "%s: unable to set data range of list element.",
1467 function );
1468
1469 goto on_error;
1470 }
1471 if( libcdata_array_append_entry(
1472 internal_list->elements_array,
1473 element_index,
1474 (intptr_t *) list_element,
1475 error ) != 1 )
1476 {
1477 libcerror_error_set(
1478 error,
1479 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1480 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1481 "%s: unable to append list element to elements array.",
1482 function );
1483
1484 goto on_error;
1485 }
1486 mapped_range_index = -1;
1487 mapped_range = NULL;
1488
1489 if( libfdata_list_element_set_element_index(
1490 list_element,
1491 *element_index,
1492 error ) != 1 )
1493 {
1494 libcerror_error_set(
1495 error,
1496 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1497 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1498 "%s: unable to set list element index.",
1499 function );
1500
1501 list_element = NULL;
1502
1503 goto on_error;
1504 }
1505 #if defined( HAVE_DEBUG_OUTPUT )
1506 if( libcnotify_verbose != 0 )
1507 {
1508 libcnotify_printf(
1509 "%s: element: %03d\tfile index: %03d offset: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
1510 function,
1511 *element_index,
1512 element_file_index,
1513 element_offset,
1514 element_offset + element_size,
1515 element_offset,
1516 element_offset + element_size,
1517 element_size );
1518
1519 libcnotify_printf(
1520 "%s: element: %03d\tmapped range: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
1521 function,
1522 *element_index,
1523 mapped_offset,
1524 mapped_offset + element_size,
1525 mapped_offset,
1526 mapped_offset + element_size,
1527 element_size );
1528
1529 libcnotify_printf(
1530 "\n" );
1531 }
1532 #endif /* defined( HAVE_DEBUG_OUTPUT ) */
1533
1534 internal_list->current_element_index = *element_index;
1535 internal_list->size += element_size;
1536
1537 return( 1 );
1538
1539 on_error:
1540 if( list_element != NULL )
1541 {
1542 libfdata_list_element_free(
1543 &list_element,
1544 NULL );
1545 }
1546 if( mapped_range_index != -1 )
1547 {
1548 libcdata_array_set_entry_by_index(
1549 internal_list->mapped_ranges_array,
1550 mapped_range_index,
1551 NULL,
1552 NULL );
1553 }
1554 if( mapped_range != NULL )
1555 {
1556 libfdata_mapped_range_free(
1557 &mapped_range,
1558 NULL );
1559 }
1560 return( -1 );
1561 }
1562
1563 /* Appends the element of the source list to the list
1564 * The source list is emptied if successful
1565 * Returns 1 if successful or -1 on error
1566 */
1567 int libfdata_list_append_list(
1568 libfdata_list_t *list,
1569 libfdata_list_t *source_list,
1570 libcerror_error_t **error )
1571 {
1572 libfdata_internal_list_t *internal_list = NULL;
1573 libfdata_internal_list_t *internal_source_list = NULL;
1574 libfdata_list_element_t *list_element = NULL;
1575 libfdata_mapped_range_t *mapped_range = NULL;
1576 static char *function = "libfdata_list_append_list";
1577 off64_t mapped_range_offset = 0;
1578 size64_t mapped_range_size = 0;
1579 int element_index = 0;
1580 int new_number_of_elements = 0;
1581 int number_of_elements = 0;
1582 int result = 1;
1583 int source_element_index = 0;
1584 int source_number_of_elements = 0;
1585
1586 if( list == NULL )
1587 {
1588 libcerror_error_set(
1589 error,
1590 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1591 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1592 "%s: invalid list.",
1593 function );
1594
1595 return( -1 );
1596 }
1597 internal_list = (libfdata_internal_list_t *) list;
1598
1599 if( source_list == NULL )
1600 {
1601 libcerror_error_set(
1602 error,
1603 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1604 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1605 "%s: invalid source list.",
1606 function );
1607
1608 return( -1 );
1609 }
1610 internal_source_list = (libfdata_internal_list_t *) source_list;
1611
1612 if( ( internal_list->flags & LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES ) != 0 )
1613 {
1614 if( libfdata_list_calculate_mapped_ranges(
1615 internal_list,
1616 error ) != 1 )
1617 {
1618 libcerror_error_set(
1619 error,
1620 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1621 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1622 "%s: unable to calculate mapped ranges.",
1623 function );
1624
1625 goto on_error;
1626 }
1627 }
1628 if( ( internal_source_list->flags & LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES ) != 0 )
1629 {
1630 if( libfdata_list_calculate_mapped_ranges(
1631 internal_source_list,
1632 error ) != 1 )
1633 {
1634 libcerror_error_set(
1635 error,
1636 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1637 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1638 "%s: unable to calculate mapped ranges of source list.",
1639 function );
1640
1641 goto on_error;
1642 }
1643 }
1644 if( ( internal_list->mapped_offset != 0 )
1645 || ( internal_source_list->mapped_offset != 0 ) )
1646 {
1647 if( ( (size64_t) internal_list->mapped_offset + internal_list->size ) != (size64_t) internal_source_list->mapped_offset )
1648 {
1649 libcerror_error_set(
1650 error,
1651 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1652 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1653 "%s: invalid source mapped offset value out of bounds.",
1654 function );
1655
1656 goto on_error;
1657 }
1658 }
1659 #if defined( HAVE_DEBUG_OUTPUT )
1660 if( libcnotify_verbose != 0 )
1661 {
1662 libcnotify_printf(
1663 "%s: appending source list with mapped offset: %" PRIi64 " (0x%08" PRIx64 ") and size: %" PRIu64 " to list with mapped offset: %" PRIi64 " (0x%08" PRIx64 ") and size: %" PRIu64 "\n",
1664 function,
1665 internal_source_list->mapped_offset,
1666 internal_source_list->mapped_offset,
1667 internal_source_list->size,
1668 internal_list->mapped_offset,
1669 internal_list->mapped_offset,
1670 internal_list->size );
1671 }
1672 #endif
1673 if( libcdata_array_get_number_of_entries(
1674 internal_list->elements_array,
1675 &number_of_elements,
1676 error ) != 1 )
1677 {
1678 libcerror_error_set(
1679 error,
1680 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1681 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1682 "%s: unable to retrieve number of elements from elements array.",
1683 function );
1684
1685 goto on_error;
1686 }
1687 if( libcdata_array_get_number_of_entries(
1688 internal_source_list->elements_array,
1689 &source_number_of_elements,
1690 error ) != 1 )
1691 {
1692 libcerror_error_set(
1693 error,
1694 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1695 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1696 "%s: unable to retrieve number of elements from source list elements array.",
1697 function );
1698
1699 goto on_error;
1700 }
1701 element_index = number_of_elements;
1702 new_number_of_elements = number_of_elements + source_number_of_elements;
1703
1704 if( libcdata_array_resize(
1705 internal_list->elements_array,
1706 new_number_of_elements,
1707 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_list_element_free,
1708 error ) != 1 )
1709 {
1710 libcerror_error_set(
1711 error,
1712 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1713 LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
1714 "%s: unable to resize elements array.",
1715 function );
1716
1717 goto on_error;
1718 }
1719 if( libcdata_array_resize(
1720 internal_list->mapped_ranges_array,
1721 new_number_of_elements,
1722 (int (*)(intptr_t **, libcerror_error_t **)) &libfdata_mapped_range_free,
1723 error ) != 1 )
1724 {
1725 libcerror_error_set(
1726 error,
1727 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1728 LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
1729 "%s: unable to resize mapped ranges array.",
1730 function );
1731
1732 goto on_error;
1733 }
1734 for( source_element_index = 0;
1735 source_element_index < source_number_of_elements;
1736 source_element_index++ )
1737 {
1738 if( libcdata_array_get_entry_by_index(
1739 internal_source_list->elements_array,
1740 source_element_index,
1741 (intptr_t **) &list_element,
1742 error ) != 1 )
1743 {
1744 libcerror_error_set(
1745 error,
1746 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1747 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1748 "%s: unable to retrieve entry: %d from source list elements array.",
1749 function,
1750 source_element_index );
1751
1752 goto on_error;
1753 }
1754 if( libcdata_array_set_entry_by_index(
1755 internal_list->elements_array,
1756 element_index,
1757 (intptr_t *) list_element,
1758 error ) != 1 )
1759 {
1760 libcerror_error_set(
1761 error,
1762 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1763 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1764 "%s: unable to set entry: %d in elements array.",
1765 function,
1766 element_index );
1767
1768 goto on_error;
1769 }
1770 if( libcdata_array_get_entry_by_index(
1771 internal_source_list->mapped_ranges_array,
1772 source_element_index,
1773 (intptr_t **) &mapped_range,
1774 error ) != 1 )
1775 {
1776 libcerror_error_set(
1777 error,
1778 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1779 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1780 "%s: unable to retrieve entry: %d from source list mapped ranges array.",
1781 function,
1782 source_element_index );
1783
1784 goto on_error;
1785 }
1786 if( libcdata_array_set_entry_by_index(
1787 internal_list->mapped_ranges_array,
1788 element_index,
1789 (intptr_t *) mapped_range,
1790 error ) != 1 )
1791 {
1792 libcerror_error_set(
1793 error,
1794 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1795 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1796 "%s: unable to set entry: %d in mapped ranges array.",
1797 function,
1798 element_index );
1799
1800 goto on_error;
1801 }
1802 element_index++;
1803 }
1804 element_index = number_of_elements;
1805 number_of_elements = new_number_of_elements;
1806 new_number_of_elements = 0;
1807
1808 if( libcdata_array_empty(
1809 internal_source_list->elements_array,
1810 NULL,
1811 error ) != 1 )
1812 {
1813 libcerror_error_set(
1814 error,
1815 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1816 LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
1817 "%s: unable to empty source list elements array.",
1818 function );
1819
1820 result = -1;
1821 }
1822 if( libcdata_array_empty(
1823 internal_source_list->mapped_ranges_array,
1824 NULL,
1825 error ) != 1 )
1826 {
1827 libcerror_error_set(
1828 error,
1829 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1830 LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
1831 "%s: unable to empty source list mapped ranges array.",
1832 function );
1833
1834 result = -1;
1835 }
1836 internal_source_list->size = 0;
1837
1838 while( element_index < number_of_elements )
1839 {
1840 if( libcdata_array_get_entry_by_index(
1841 internal_list->elements_array,
1842 element_index,
1843 (intptr_t **) &list_element,
1844 error ) != 1 )
1845 {
1846 libcerror_error_set(
1847 error,
1848 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1849 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1850 "%s: unable to retrieve entry: %d from elements array.",
1851 function,
1852 element_index );
1853
1854 goto on_error;
1855 }
1856 ( (libfdata_internal_list_element_t *) list_element )->list = list;
1857 ( (libfdata_internal_list_element_t *) list_element )->element_index = element_index;
1858
1859 if( libcdata_array_get_entry_by_index(
1860 internal_list->mapped_ranges_array,
1861 element_index,
1862 (intptr_t **) &mapped_range,
1863 error ) != 1 )
1864 {
1865 libcerror_error_set(
1866 error,
1867 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1868 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1869 "%s: unable to retrieve entry: %d from mapped ranges array.",
1870 function,
1871 element_index );
1872
1873 goto on_error;
1874 }
1875 if( libfdata_mapped_range_get(
1876 mapped_range,
1877 &mapped_range_offset,
1878 &mapped_range_size,
1879 error ) != 1 )
1880 {
1881 libcerror_error_set(
1882 error,
1883 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1884 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1885 "%s: unable to retrieve values from mapped range: %d.",
1886 function,
1887 element_index );
1888
1889 goto on_error;
1890 }
1891 if( libfdata_mapped_range_set(
1892 mapped_range,
1893 internal_list->size,
1894 mapped_range_size,
1895 error ) != 1 )
1896 {
1897 libcerror_error_set(
1898 error,
1899 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1900 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1901 "%s: unable to set values of mapped range: %d.",
1902 function,
1903 element_index );
1904
1905 goto on_error;
1906 }
1907 internal_list->size += mapped_range_size;
1908
1909 element_index++;
1910 }
1911 #if defined( HAVE_DEBUG_OUTPUT )
1912 if( libcnotify_verbose != 0 )
1913 {
1914 libcnotify_printf(
1915 "\n" );
1916 }
1917 #endif
1918 return( result );
1919
1920 on_error:
1921 if( new_number_of_elements != 0 )
1922 {
1923 libcdata_array_resize(
1924 internal_list->mapped_ranges_array,
1925 number_of_elements,
1926 NULL,
1927 NULL );
1928
1929 libcdata_array_resize(
1930 internal_list->elements_array,
1931 number_of_elements,
1932 NULL,
1933 NULL );
1934 }
1935 return( -1 );
1936 }
1937
1938 /* Determines if a specific element is set
1939 * Returns 1 if element is set, 0 if not or -1 on error
1940 */
1941 int libfdata_list_is_element_set(
1942 libfdata_list_t *list,
1943 int element_index,
1944 libcerror_error_t **error )
1945 {
1946 libfdata_internal_list_t *internal_list = NULL;
1947 libfdata_list_element_t *list_element = NULL;
1948 static char *function = "libfdata_list_is_element_set";
1949
1950 if( list == NULL )
1951 {
1952 libcerror_error_set(
1953 error,
1954 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1955 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1956 "%s: invalid list.",
1957 function );
1958
1959 return( -1 );
1960 }
1961 internal_list = (libfdata_internal_list_t *) list;
1962
1963 if( libcdata_array_get_entry_by_index(
1964 internal_list->elements_array,
1965 element_index,
1966 (intptr_t **) &list_element,
1967 error ) != 1 )
1968 {
1969 libcerror_error_set(
1970 error,
1971 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1972 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1973 "%s: unable to retrieve entry: %d from elements array.",
1974 function,
1975 element_index );
1976
1977 return( -1 );
1978 }
1979 if( list_element == NULL )
1980 {
1981 return( 0 );
1982 }
1983 internal_list->current_element_index = element_index;
1984
1985 return( 1 );
1986 }
1987
1988 /* Mapped range functions
1989 */
1990
1991 /* Retrieves the mapped range of a specific element
1992 * Returns 1 if successful or -1 on error
1993 */
1994 int libfdata_list_get_element_mapped_range(
1995 libfdata_list_t *list,
1996 int element_index,
1997 off64_t *mapped_range_offset,
1998 size64_t *mapped_range_size,
1999 libcerror_error_t **error )
2000 {
2001 libfdata_internal_list_t *internal_list = NULL;
2002 libfdata_mapped_range_t *mapped_range = NULL;
2003 static char *function = "libfdata_list_get_element_mapped_range";
2004
2005 if( list == NULL )
2006 {
2007 libcerror_error_set(
2008 error,
2009 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2010 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2011 "%s: invalid list.",
2012 function );
2013
2014 return( -1 );
2015 }
2016 internal_list = (libfdata_internal_list_t *) list;
2017
2018 if( ( internal_list->flags & LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES ) != 0 )
2019 {
2020 if( libfdata_list_calculate_mapped_ranges(
2021 internal_list,
2022 error ) != 1 )
2023 {
2024 libcerror_error_set(
2025 error,
2026 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2027 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2028 "%s: unable to calculate mapped ranges.",
2029 function );
2030
2031 return( -1 );
2032 }
2033 }
2034 if( libcdata_array_get_entry_by_index(
2035 internal_list->mapped_ranges_array,
2036 element_index,
2037 (intptr_t **) &mapped_range,
2038 error ) != 1 )
2039 {
2040 libcerror_error_set(
2041 error,
2042 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2043 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2044 "%s: unable to retrieve entry: %d from mapped ranges array.",
2045 function,
2046 element_index );
2047
2048 return( -1 );
2049 }
2050 if( libfdata_mapped_range_get(
2051 mapped_range,
2052 mapped_range_offset,
2053 mapped_range_size,
2054 error ) != 1 )
2055 {
2056 libcerror_error_set(
2057 error,
2058 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2059 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2060 "%s: unable to retrieve values from mapped range: %d.",
2061 function,
2062 element_index );
2063
2064 return( -1 );
2065 }
2066 return( 1 );
2067 }
2068
2069 /* Retrieves the mapped offset
2070 * Returns 1 if successful, 0 if not set or -1 on error
2071 */
2072 int libfdata_list_get_mapped_offset(
2073 libfdata_list_t *list,
2074 off64_t *mapped_offset,
2075 libcerror_error_t **error )
2076 {
2077 libfdata_internal_list_t *internal_list = NULL;
2078 static char *function = "libfdata_list_get_mapped_offset";
2079
2080 if( list == NULL )
2081 {
2082 libcerror_error_set(
2083 error,
2084 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2085 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2086 "%s: invalid list.",
2087 function );
2088
2089 return( -1 );
2090 }
2091 internal_list = (libfdata_internal_list_t *) list;
2092
2093 if( mapped_offset == NULL )
2094 {
2095 libcerror_error_set(
2096 error,
2097 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2098 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2099 "%s: invalid mapped offset.",
2100 function );
2101
2102 return( -1 );
2103 }
2104 if( ( internal_list->flags & LIBFDATA_LIST_FLAG_HAS_MAPPED_OFFSET ) == 0 )
2105 {
2106 return( 0 );
2107 }
2108 *mapped_offset = internal_list->mapped_offset;
2109
2110 return( 1 );
2111 }
2112
2113 /* Sets the mapped offset
2114 * Returns 1 if successful or -1 on error
2115 */
2116 int libfdata_list_set_mapped_offset(
2117 libfdata_list_t *list,
2118 off64_t mapped_offset,
2119 libcerror_error_t **error )
2120 {
2121 libfdata_internal_list_t *internal_list = NULL;
2122 static char *function = "libfdata_list_set_mapped_offset";
2123
2124 if( list == NULL )
2125 {
2126 libcerror_error_set(
2127 error,
2128 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2129 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2130 "%s: invalid list.",
2131 function );
2132
2133 return( -1 );
2134 }
2135 internal_list = (libfdata_internal_list_t *) list;
2136
2137 if( mapped_offset < 0 )
2138 {
2139 libcerror_error_set(
2140 error,
2141 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2142 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2143 "%s: invalid offset value out of bounds.",
2144 function );
2145
2146 return( -1 );
2147 }
2148 #if defined( HAVE_DEBUG_OUTPUT )
2149 if( libcnotify_verbose != 0 )
2150 {
2151 libcnotify_printf(
2152 "%s: mapped offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
2153 function,
2154 mapped_offset,
2155 mapped_offset );
2156
2157 libcnotify_printf(
2158 "\n" );
2159 }
2160 #endif
2161 internal_list->mapped_offset = mapped_offset;
2162 internal_list->flags |= LIBFDATA_LIST_FLAG_HAS_MAPPED_OFFSET | LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
2163
2164 return( 1 );
2165 }
2166
2167 /* Retrieves the mapped size of a specific element
2168 * Returns 1 if successful, 0 if not set or -1 on error
2169 */
2170 int libfdata_list_get_mapped_size_by_index(
2171 libfdata_list_t *list,
2172 int element_index,
2173 size64_t *mapped_size,
2174 libcerror_error_t **error )
2175 {
2176 libfdata_internal_list_t *internal_list = NULL;
2177 libfdata_list_element_t *list_element = NULL;
2178 static char *function = "libfdata_list_get_mapped_size_by_index";
2179 int result = 0;
2180
2181 if( list == NULL )
2182 {
2183 libcerror_error_set(
2184 error,
2185 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2186 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2187 "%s: invalid list.",
2188 function );
2189
2190 return( -1 );
2191 }
2192 internal_list = (libfdata_internal_list_t *) list;
2193
2194 if( libcdata_array_get_entry_by_index(
2195 internal_list->elements_array,
2196 element_index,
2197 (intptr_t **) &list_element,
2198 error ) != 1 )
2199 {
2200 libcerror_error_set(
2201 error,
2202 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2203 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2204 "%s: unable to retrieve entry: %d from elements array.",
2205 function,
2206 element_index );
2207
2208 return( -1 );
2209 }
2210 result = libfdata_list_element_get_mapped_size(
2211 list_element,
2212 mapped_size,
2213 error );
2214
2215 if( result == -1 )
2216 {
2217 libcerror_error_set(
2218 error,
2219 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2220 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2221 "%s: unable to retrieve mapped size of element: %d.",
2222 function,
2223 element_index );
2224
2225 return( -1 );
2226 }
2227 internal_list->current_element_index = element_index;
2228
2229 return( result );
2230 }
2231
2232 /* Sets the mapped size of a specific element
2233 * Returns 1 if successful or -1 on error
2234 */
2235 int libfdata_list_set_mapped_size_by_index(
2236 libfdata_list_t *list,
2237 int element_index,
2238 size64_t mapped_size,
2239 libcerror_error_t **error )
2240 {
2241 libfdata_internal_list_t *internal_list = NULL;
2242 libfdata_list_element_t *list_element = NULL;
2243 static char *function = "libfdata_list_set_mapped_size_by_index";
2244
2245 if( list == NULL )
2246 {
2247 libcerror_error_set(
2248 error,
2249 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2250 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2251 "%s: invalid list.",
2252 function );
2253
2254 return( -1 );
2255 }
2256 internal_list = (libfdata_internal_list_t *) list;
2257
2258 if( libcdata_array_get_entry_by_index(
2259 internal_list->elements_array,
2260 element_index,
2261 (intptr_t **) &list_element,
2262 error ) != 1 )
2263 {
2264 libcerror_error_set(
2265 error,
2266 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2267 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2268 "%s: unable to retrieve entry: %d from elements array.",
2269 function,
2270 element_index );
2271
2272 return( -1 );
2273 }
2274 if( libfdata_list_element_set_mapped_size(
2275 list_element,
2276 mapped_size,
2277 error ) != 1 )
2278 {
2279 libcerror_error_set(
2280 error,
2281 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2282 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2283 "%s: unable to set mapped size of element: %d.",
2284 function,
2285 element_index );
2286
2287 return( -1 );
2288 }
2289 internal_list->current_element_index = element_index;
2290
2291 return( 1 );
2292 }
2293
2294 /* Retrieves the data range with its mapped size of a specific element
2295 * Returns 1 if successful, 0 if the element has no mapped size or -1 on error
2296 */
2297 int libfdata_list_get_element_by_index_with_mapped_size(
2298 libfdata_list_t *list,
2299 int element_index,
2300 int *element_file_index,
2301 off64_t *element_offset,
2302 size64_t *element_size,
2303 uint32_t *element_flags,
2304 size64_t *mapped_size,
2305 libcerror_error_t **error )
2306 {
2307 libfdata_internal_list_t *internal_list = NULL;
2308 libfdata_list_element_t *list_element = NULL;
2309 static char *function = "libfdata_list_get_element_by_index_with_mapped_size";
2310 int result = 0;
2311
2312 if( list == NULL )
2313 {
2314 libcerror_error_set(
2315 error,
2316 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2317 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2318 "%s: invalid list.",
2319 function );
2320
2321 return( -1 );
2322 }
2323 internal_list = (libfdata_internal_list_t *) list;
2324
2325 if( libcdata_array_get_entry_by_index(
2326 internal_list->elements_array,
2327 element_index,
2328 (intptr_t **) &list_element,
2329 error ) != 1 )
2330 {
2331 libcerror_error_set(
2332 error,
2333 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2334 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2335 "%s: unable to retrieve entry: %d from elements array.",
2336 function,
2337 element_index );
2338
2339 return( -1 );
2340 }
2341 if( libfdata_list_element_get_data_range(
2342 list_element,
2343 element_file_index,
2344 element_offset,
2345 element_size,
2346 element_flags,
2347 error ) != 1 )
2348 {
2349 libcerror_error_set(
2350 error,
2351 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2352 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2353 "%s: unable to retrieve data range from list element: %d.",
2354 function,
2355 element_index );
2356
2357 return( -1 );
2358 }
2359 result = libfdata_list_element_get_mapped_size(
2360 list_element,
2361 mapped_size,
2362 error );
2363
2364 if( result == -1 )
2365 {
2366 libcerror_error_set(
2367 error,
2368 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2369 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2370 "%s: unable to retrieve mapped size of list element: %d.",
2371 function,
2372 element_index );
2373
2374 return( -1 );
2375 }
2376 internal_list->current_element_index = element_index;
2377
2378 return( result );
2379 }
2380
2381 /* Sets the data range of a specific element with its mapped size
2382 * Returns 1 if successful or -1 on error
2383 */
2384 int libfdata_list_set_element_by_index_with_mapped_size(
2385 libfdata_list_t *list,
2386 int element_index,
2387 int element_file_index,
2388 off64_t element_offset,
2389 size64_t element_size,
2390 uint32_t element_flags,
2391 size64_t mapped_size,
2392 libcerror_error_t **error )
2393 {
2394 libfdata_internal_list_t *internal_list = NULL;
2395 libfdata_list_element_t *list_element = NULL;
2396 libfdata_mapped_range_t *mapped_range = NULL;
2397 static char *function = "libfdata_list_set_element_by_index_with_mapped_size";
2398 off64_t previous_element_offset = 0;
2399 size64_t previous_element_size = 0;
2400 size64_t previous_mapped_size = 0;
2401 uint32_t previous_element_flags = 0;
2402 int previous_element_file_index = 0;
2403 int result = 0;
2404
2405 #if defined( HAVE_DEBUG_OUTPUT )
2406 off64_t mapped_range_offset = 0;
2407 size64_t mapped_range_size = 0;
2408 #endif
2409
2410 if( list == NULL )
2411 {
2412 libcerror_error_set(
2413 error,
2414 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2415 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2416 "%s: invalid list.",
2417 function );
2418
2419 return( -1 );
2420 }
2421 internal_list = (libfdata_internal_list_t *) list;
2422
2423 if( libcdata_array_get_entry_by_index(
2424 internal_list->elements_array,
2425 element_index,
2426 (intptr_t **) &list_element,
2427 error ) != 1 )
2428 {
2429 libcerror_error_set(
2430 error,
2431 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2432 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2433 "%s: unable to retrieve entry: %d from elements array.",
2434 function,
2435 element_index );
2436
2437 return( -1 );
2438 }
2439 if( list_element == NULL )
2440 {
2441 if( libfdata_list_element_initialize(
2442 &list_element,
2443 list,
2444 element_index,
2445 error ) != 1 )
2446 {
2447 libcerror_error_set(
2448 error,
2449 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2450 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2451 "%s: unable to create list element.",
2452 function );
2453
2454 return( -1 );
2455 }
2456 if( libcdata_array_set_entry_by_index(
2457 internal_list->elements_array,
2458 element_index,
2459 (intptr_t *) list_element,
2460 error ) != 1 )
2461 {
2462 libcerror_error_set(
2463 error,
2464 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2465 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2466 "%s: unable to set entry: %d in elements array.",
2467 function,
2468 element_index );
2469
2470 libfdata_list_element_free(
2471 &list_element,
2472 NULL );
2473
2474 return( -1 );
2475 }
2476 }
2477 else
2478 {
2479 result = libfdata_list_element_get_mapped_size(
2480 list_element,
2481 &previous_mapped_size,
2482 error );
2483
2484 if( result == -1 )
2485 {
2486 libcerror_error_set(
2487 error,
2488 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2489 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2490 "%s: unable to retrieve mapped size of list element: %d.",
2491 function,
2492 element_index );
2493
2494 return( -1 );
2495 }
2496 else if( result == 0 )
2497 {
2498 if( libfdata_list_element_get_data_range(
2499 list_element,
2500 &previous_element_file_index,
2501 &previous_element_offset,
2502 &previous_element_size,
2503 &previous_element_flags,
2504 error ) != 1 )
2505 {
2506 libcerror_error_set(
2507 error,
2508 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2509 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2510 "%s: unable to retrieve data range of list element: %d.",
2511 function,
2512 element_index );
2513
2514 return( -1 );
2515 }
2516 }
2517 }
2518 if( libfdata_list_element_set_data_range(
2519 list_element,
2520 element_file_index,
2521 element_offset,
2522 element_size,
2523 element_flags,
2524 error ) != 1 )
2525 {
2526 libcerror_error_set(
2527 error,
2528 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2529 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2530 "%s: unable to set data range of list element: %d.",
2531 function,
2532 element_index );
2533
2534 return( -1 );
2535 }
2536 if( libfdata_list_element_set_mapped_size(
2537 list_element,
2538 mapped_size,
2539 error ) != 1 )
2540 {
2541 libcerror_error_set(
2542 error,
2543 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2544 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2545 "%s: unable to set mapped size of list element.",
2546 function );
2547
2548 return( -1 );
2549 }
2550 /* Make sure the list has a mapped range entry for every element
2551 */
2552 if( libcdata_array_get_entry_by_index(
2553 internal_list->mapped_ranges_array,
2554 element_index,
2555 (intptr_t **) &mapped_range,
2556 error ) != 1 )
2557 {
2558 libcerror_error_set(
2559 error,
2560 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2561 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2562 "%s: unable to retrieve entry: %d from mapped ranges array.",
2563 function,
2564 element_index );
2565
2566 return( -1 );
2567 }
2568 if( mapped_range == NULL )
2569 {
2570 if( libfdata_mapped_range_initialize(
2571 &mapped_range,
2572 error ) != 1 )
2573 {
2574 libcerror_error_set(
2575 error,
2576 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2577 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2578 "%s: unable to create mapped range.",
2579 function );
2580
2581 return( -1 );
2582 }
2583 if( libcdata_array_set_entry_by_index(
2584 internal_list->mapped_ranges_array,
2585 element_index,
2586 (intptr_t *) mapped_range,
2587 error ) != 1 )
2588 {
2589 libcerror_error_set(
2590 error,
2591 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2592 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2593 "%s: unable to set entry: %d in mapped ranges array.",
2594 function,
2595 element_index );
2596
2597 libfdata_mapped_range_free(
2598 &mapped_range,
2599 NULL );
2600
2601 return( -1 );
2602 }
2603 internal_list->size += mapped_size;
2604 internal_list->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
2605 }
2606 else if( previous_mapped_size != mapped_size )
2607 {
2608 if( previous_mapped_size != 0 )
2609 {
2610 internal_list->size -= previous_mapped_size;
2611 }
2612 else
2613 {
2614 internal_list->size -= previous_element_size;
2615 }
2616 if( mapped_size != 0 )
2617 {
2618 internal_list->size += mapped_size;
2619 }
2620 else
2621 {
2622 internal_list->size += element_size;
2623 }
2624 internal_list->flags |= LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES;
2625 }
2626 #if defined( HAVE_DEBUG_OUTPUT )
2627 if( libcnotify_verbose != 0 )
2628 {
2629 libcnotify_printf(
2630 "%s: element: %03d\tfile index: %03d offset: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
2631 function,
2632 element_index,
2633 element_file_index,
2634 element_offset,
2635 element_offset + element_size,
2636 element_offset,
2637 element_offset + element_size,
2638 element_size );
2639
2640 if( ( internal_list->flags & LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES ) == 0 )
2641 {
2642 if( libfdata_mapped_range_get(
2643 mapped_range,
2644 &mapped_range_offset,
2645 &mapped_range_size,
2646 error ) != 1 )
2647 {
2648 libcerror_error_set(
2649 error,
2650 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2651 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2652 "%s: unable to retrieve values from mapped range: %d.",
2653 function,
2654 element_index );
2655
2656 return( -1 );
2657 }
2658 libcnotify_printf(
2659 "%s: element: %03d\tmapped range: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
2660 function,
2661 element_index,
2662 mapped_range_offset,
2663 mapped_range_offset + mapped_range_size,
2664 mapped_range_offset,
2665 mapped_range_offset + mapped_range_size,
2666 mapped_range_size );
2667 }
2668 libcnotify_printf(
2669 "\n" );
2670 }
2671 #endif /* defined( HAVE_DEBUG_OUTPUT ) */
2672
2673 internal_list->current_element_index = element_index;
2674
2675 return( 1 );
2676 }
2677
2678 /* Appends an element data range with its mapped size
2679 * Returns 1 if successful or -1 on error
2680 */
2681 int libfdata_list_append_element_with_mapped_size(
2682 libfdata_list_t *list,
2683 int *element_index,
2684 int element_file_index,
2685 off64_t element_offset,
2686 size64_t element_size,
2687 uint32_t element_flags,
2688 size64_t mapped_size,
2689 libcerror_error_t **error )
2690 {
2691 libfdata_internal_list_t *internal_list = NULL;
2692 libfdata_list_element_t *list_element = NULL;
2693 libfdata_mapped_range_t *mapped_range = NULL;
2694 static char *function = "libfdata_list_append_element_with_mapped_size";
2695 off64_t mapped_offset = 0;
2696 int mapped_range_index = -1;
2697 uint8_t list_flags = 0;
2698
2699 if( list == NULL )
2700 {
2701 libcerror_error_set(
2702 error,
2703 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2704 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2705 "%s: invalid list.",
2706 function );
2707
2708 return( -1 );
2709 }
2710 internal_list = (libfdata_internal_list_t *) list;
2711
2712 if( element_index == NULL )
2713 {
2714 libcerror_error_set(
2715 error,
2716 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2717 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2718 "%s: invalid element index.",
2719 function );
2720
2721 return( -1 );
2722 }
2723 if( libfdata_mapped_range_initialize(
2724 &mapped_range,
2725 error ) != 1 )
2726 {
2727 libcerror_error_set(
2728 error,
2729 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2730 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2731 "%s: unable to create mapped range.",
2732 function );
2733
2734 goto on_error;
2735 }
2736 mapped_offset = internal_list->mapped_offset + (off64_t) internal_list->size;
2737
2738 if( libfdata_mapped_range_set(
2739 mapped_range,
2740 mapped_offset,
2741 mapped_size,
2742 error ) != 1 )
2743 {
2744 libcerror_error_set(
2745 error,
2746 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2747 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2748 "%s: unable to set mapped range values.",
2749 function );
2750
2751 goto on_error;
2752 }
2753 if( libcdata_array_append_entry(
2754 internal_list->mapped_ranges_array,
2755 &mapped_range_index,
2756 (intptr_t *) mapped_range,
2757 error ) != 1 )
2758 {
2759 libcerror_error_set(
2760 error,
2761 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2762 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2763 "%s: unable to append mapped range to array.",
2764 function );
2765
2766 goto on_error;
2767 }
2768 if( libfdata_list_element_initialize(
2769 &list_element,
2770 list,
2771 0,
2772 error ) != 1 )
2773 {
2774 libcerror_error_set(
2775 error,
2776 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2777 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2778 "%s: unable to create list element.",
2779 function );
2780
2781 goto on_error;
2782 }
2783 if( libfdata_list_element_set_data_range(
2784 list_element,
2785 element_file_index,
2786 element_offset,
2787 element_size,
2788 element_flags,
2789 error ) != 1 )
2790 {
2791 libcerror_error_set(
2792 error,
2793 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2794 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2795 "%s: unable to set data range of list element.",
2796 function );
2797
2798 goto on_error;
2799 }
2800 list_flags = internal_list->flags;
2801
2802 if( libfdata_list_element_set_mapped_size(
2803 list_element,
2804 mapped_size,
2805 error ) != 1 )
2806 {
2807 libcerror_error_set(
2808 error,
2809 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2810 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2811 "%s: unable to set mapped size of list element.",
2812 function );
2813
2814 goto on_error;
2815 }
2816 /* Setting the mapped size in the list element will set the calculate mapped ranges flag in the list
2817 * Reset the flag if it was not set before setting the mapped size in the list element
2818 */
2819 if( ( list_flags & LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES ) == 0 )
2820 {
2821 internal_list->flags &= ~( LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES );
2822 }
2823 if( libcdata_array_append_entry(
2824 internal_list->elements_array,
2825 element_index,
2826 (intptr_t *) list_element,
2827 error ) != 1 )
2828 {
2829 libcerror_error_set(
2830 error,
2831 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2832 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2833 "%s: unable to append list element to elements array.",
2834 function );
2835
2836 goto on_error;
2837 }
2838 mapped_range_index = -1;
2839 mapped_range = NULL;
2840
2841 if( libfdata_list_element_set_element_index(
2842 list_element,
2843 *element_index,
2844 error ) != 1 )
2845 {
2846 libcerror_error_set(
2847 error,
2848 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2849 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2850 "%s: unable to set list element index.",
2851 function );
2852
2853 list_element = NULL;
2854
2855 goto on_error;
2856 }
2857 #if defined( HAVE_DEBUG_OUTPUT )
2858 if( libcnotify_verbose != 0 )
2859 {
2860 libcnotify_printf(
2861 "%s: element: %03d\tfile index: %03d offset: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
2862 function,
2863 *element_index,
2864 element_file_index,
2865 element_offset,
2866 element_offset + element_size,
2867 element_offset,
2868 element_offset + element_size,
2869 element_size );
2870
2871 libcnotify_printf(
2872 "%s: element: %03d\tmapped range: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
2873 function,
2874 *element_index,
2875 mapped_offset,
2876 mapped_offset + mapped_size,
2877 mapped_offset,
2878 mapped_offset + mapped_size,
2879 mapped_size );
2880
2881 libcnotify_printf(
2882 "\n" );
2883 }
2884 #endif /* defined( HAVE_DEBUG_OUTPUT ) */
2885
2886 internal_list->current_element_index = *element_index;
2887 internal_list->size += mapped_size;
2888
2889 return( 1 );
2890
2891 on_error:
2892 if( list_element != NULL )
2893 {
2894 libfdata_list_element_free(
2895 &list_element,
2896 NULL );
2897 }
2898 if( mapped_range_index != -1 )
2899 {
2900 libcdata_array_set_entry_by_index(
2901 internal_list->mapped_ranges_array,
2902 mapped_range_index,
2903 NULL,
2904 NULL );
2905 }
2906 if( mapped_range != NULL )
2907 {
2908 libfdata_mapped_range_free(
2909 &mapped_range,
2910 NULL );
2911 }
2912 return( -1 );
2913 }
2914
2915 /* Calculates the mapped ranges
2916 * Returns 1 if successful or -1 on error
2917 */
2918 int libfdata_list_calculate_mapped_ranges(
2919 libfdata_internal_list_t *internal_list,
2920 libcerror_error_t **error )
2921 {
2922 libfdata_list_element_t *list_element = NULL;
2923 libfdata_mapped_range_t *mapped_range = NULL;
2924 static char *function = "libfdata_list_calculate_mapped_ranges";
2925 off64_t mapped_offset = 0;
2926 off64_t element_offset = 0;
2927 size64_t element_size = 0;
2928 size64_t mapped_size = 0;
2929 uint32_t element_flags = 0;
2930 int element_file_index = -1;
2931 int element_index = -1;
2932 int number_of_elements = 0;
2933 int result = 0;
2934
2935 if( internal_list == NULL )
2936 {
2937 libcerror_error_set(
2938 error,
2939 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2940 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2941 "%s: invalid list.",
2942 function );
2943
2944 return( -1 );
2945 }
2946 if( libcdata_array_get_number_of_entries(
2947 internal_list->elements_array,
2948 &number_of_elements,
2949 error ) != 1 )
2950 {
2951 libcerror_error_set(
2952 error,
2953 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2954 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2955 "%s: unable to retrieve number of elements from elements array.",
2956 function );
2957
2958 return( -1 );
2959 }
2960 mapped_offset = internal_list->mapped_offset;
2961
2962 for( element_index = 0;
2963 element_index < number_of_elements;
2964 element_index++ )
2965 {
2966 if( libcdata_array_get_entry_by_index(
2967 internal_list->elements_array,
2968 element_index,
2969 (intptr_t **) &list_element,
2970 error ) != 1 )
2971 {
2972 libcerror_error_set(
2973 error,
2974 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2975 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2976 "%s: unable to retrieve entry: %d from elements array.",
2977 function,
2978 element_index );
2979
2980 return( -1 );
2981 }
2982 if( libcdata_array_get_entry_by_index(
2983 internal_list->mapped_ranges_array,
2984 element_index,
2985 (intptr_t **) &mapped_range,
2986 error ) != 1 )
2987 {
2988 libcerror_error_set(
2989 error,
2990 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2991 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2992 "%s: unable to retrieve entry: %d from mapped ranges array.",
2993 function,
2994 element_index );
2995
2996 return( -1 );
2997 }
2998 if( libfdata_list_element_get_data_range(
2999 list_element,
3000 &element_file_index,
3001 &element_offset,
3002 &element_size,
3003 &element_flags,
3004 error ) != 1 )
3005 {
3006 libcerror_error_set(
3007 error,
3008 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3009 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3010 "%s: unable to retrieve data range of list element: %d.",
3011 function,
3012 element_index );
3013
3014 return( -1 );
3015 }
3016 result = libfdata_list_element_get_mapped_size(
3017 list_element,
3018 &mapped_size,
3019 error );
3020
3021 if( result == -1 )
3022 {
3023 libcerror_error_set(
3024 error,
3025 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3026 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3027 "%s: unable to retrieve mapped size of list element: %d.",
3028 function,
3029 element_index );
3030
3031 return( -1 );
3032 }
3033 else if( result == 0 )
3034 {
3035 mapped_size = element_size;
3036 }
3037 #if defined( HAVE_DEBUG_OUTPUT )
3038 if( libcnotify_verbose != 0 )
3039 {
3040 libcnotify_printf(
3041 "%s: element: %03d\tfile index: %03d offset: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
3042 function,
3043 element_index,
3044 element_file_index,
3045 element_offset,
3046 element_offset + element_size,
3047 element_offset,
3048 element_offset + element_size,
3049 element_size );
3050
3051 libcnotify_printf(
3052 "%s: element: %03d\tmapped range: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
3053 function,
3054 element_index,
3055 mapped_offset,
3056 mapped_offset + mapped_size,
3057 mapped_offset,
3058 mapped_offset + mapped_size,
3059 mapped_size );
3060 }
3061 #endif /* defined( HAVE_DEBUG_OUTPUT ) */
3062
3063 if( libfdata_mapped_range_set(
3064 mapped_range,
3065 mapped_offset,
3066 mapped_size,
3067 error ) != 1 )
3068 {
3069 libcerror_error_set(
3070 error,
3071 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3072 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3073 "%s: unable to set mapped range: %d values.",
3074 function,
3075 element_index );
3076
3077 return( -1 );
3078 }
3079 mapped_offset += (off64_t) mapped_size;
3080 }
3081 internal_list->size = (size64_t) mapped_offset - internal_list->mapped_offset;
3082 internal_list->flags &= ~( LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES );
3083
3084 #if defined( HAVE_DEBUG_OUTPUT )
3085 if( libcnotify_verbose != 0 )
3086 {
3087 libcnotify_printf(
3088 "\n" );
3089 }
3090 #endif
3091 return( 1 );
3092 }
3093
3094 /* Retrieves the element index for a specific offset
3095 * The element_data_offset value is set to the offset relative to the start of the element
3096 * Returns 1 if successful, 0 if not or -1 on error
3097 */
3098 int libfdata_list_get_element_index_at_offset(
3099 libfdata_list_t *list,
3100 off64_t offset,
3101 int *element_index,
3102 off64_t *element_data_offset,
3103 libcerror_error_t **error )
3104 {
3105 libfdata_internal_list_t *internal_list = NULL;
3106 libfdata_mapped_range_t *mapped_range = NULL;
3107 static char *function = "libfdata_list_get_element_index_at_offset";
3108 off64_t list_offset = 0;
3109 off64_t mapped_range_end_offset = 0;
3110 off64_t mapped_range_start_offset = 0;
3111 size64_t mapped_range_size = 0;
3112 int initial_element_index = 0;
3113 int number_of_elements = 0;
3114 int result = 0;
3115 int search_element_index = 0;
3116
3117 #if defined( HAVE_DEBUG_OUTPUT )
3118 libfdata_list_element_t *element = NULL;
3119 off64_t element_offset = 0;
3120 size64_t element_size = 0;
3121 uint32_t element_flags = 0;
3122 int element_file_index = -1;
3123 #endif
3124
3125 if( list == NULL )
3126 {
3127 libcerror_error_set(
3128 error,
3129 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3130 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3131 "%s: invalid list.",
3132 function );
3133
3134 return( -1 );
3135 }
3136 internal_list = (libfdata_internal_list_t *) list;
3137
3138 if( offset < 0 )
3139 {
3140 libcerror_error_set(
3141 error,
3142 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3143 LIBCERROR_ARGUMENT_ERROR_VALUE_LESS_THAN_ZERO,
3144 "%s: invalid offset value less than zero.",
3145 function );
3146
3147 return( -1 );
3148 }
3149 if( element_index == NULL )
3150 {
3151 libcerror_error_set(
3152 error,
3153 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3154 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3155 "%s: invalid element index.",
3156 function );
3157
3158 return( -1 );
3159 }
3160 if( element_data_offset == NULL )
3161 {
3162 libcerror_error_set(
3163 error,
3164 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3165 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3166 "%s: invalid element data offset.",
3167 function );
3168
3169 return( -1 );
3170 }
3171 if( ( internal_list->flags & LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES ) != 0 )
3172 {
3173 if( libfdata_list_calculate_mapped_ranges(
3174 internal_list,
3175 error ) != 1 )
3176 {
3177 libcerror_error_set(
3178 error,
3179 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3180 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3181 "%s: unable to calculate mapped ranges.",
3182 function );
3183
3184 return( -1 );
3185 }
3186 }
3187 #if defined( HAVE_DEBUG_OUTPUT )
3188 if( libcnotify_verbose != 0 )
3189 {
3190 libcnotify_printf(
3191 "%s: requested offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
3192 function,
3193 offset,
3194 offset );
3195 }
3196 #endif
3197 if( internal_list->size == 0 )
3198 {
3199 return( 0 );
3200 }
3201 if( offset < internal_list->mapped_offset )
3202 {
3203 return( 0 );
3204 }
3205 list_offset = offset - internal_list->mapped_offset;
3206
3207 if( (size64_t) list_offset >= internal_list->size )
3208 {
3209 return( 0 );
3210 }
3211 if( libcdata_array_get_number_of_entries(
3212 internal_list->mapped_ranges_array,
3213 &number_of_elements,
3214 error ) != 1 )
3215 {
3216 libcerror_error_set(
3217 error,
3218 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3219 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3220 "%s: unable to retrieve number of entries from mapped ranges array.",
3221 function );
3222
3223 return( -1 );
3224 }
3225 #if defined( HAVE_DEBUG_OUTPUT )
3226 if( libcnotify_verbose != 0 )
3227 {
3228 libcnotify_printf(
3229 "%s: number of elements: %d\n",
3230 function,
3231 number_of_elements );
3232 }
3233 #endif
3234 /* This assumes a fairly even distribution of the sizes of the elements
3235 */
3236 initial_element_index = (int) ( ( number_of_elements * list_offset ) / internal_list->size );
3237
3238 /* Look for the corresponding element upwards in the array
3239 */
3240 for( search_element_index = initial_element_index;
3241 search_element_index < number_of_elements;
3242 search_element_index++ )
3243 {
3244 if( libcdata_array_get_entry_by_index(
3245 internal_list->mapped_ranges_array,
3246 search_element_index,
3247 (intptr_t **) &mapped_range,
3248 error ) != 1 )
3249 {
3250 libcerror_error_set(
3251 error,
3252 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3253 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3254 "%s: unable to retrieve entry: %d from mapped ranges array.",
3255 function,
3256 search_element_index );
3257
3258 return( -1 );
3259 }
3260 if( libfdata_mapped_range_get(
3261 mapped_range,
3262 &mapped_range_start_offset,
3263 &mapped_range_size,
3264 error ) != 1 )
3265 {
3266 libcerror_error_set(
3267 error,
3268 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3269 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3270 "%s: unable to retrieve values from mapped range: %d.",
3271 function,
3272 search_element_index );
3273
3274 return( -1 );
3275 }
3276 mapped_range_end_offset = mapped_range_start_offset + (off64_t) mapped_range_size;
3277
3278 if( mapped_range_end_offset < mapped_range_start_offset )
3279 {
3280 libcerror_error_set(
3281 error,
3282 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3283 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3284 "%s: invalid element: %d - mapped range value out of bounds.",
3285 function,
3286 search_element_index );
3287
3288 return( -1 );
3289 }
3290 #if defined( HAVE_DEBUG_OUTPUT )
3291 if( libcnotify_verbose != 0 )
3292 {
3293 libcnotify_printf(
3294 "%s: element: %03d\tmapped range: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
3295 function,
3296 search_element_index,
3297 mapped_range_start_offset,
3298 mapped_range_end_offset,
3299 mapped_range_start_offset,
3300 mapped_range_end_offset,
3301 mapped_range_size );
3302 }
3303 #endif
3304 /* Check if the offset is in the mapped range
3305 */
3306 if( ( offset >= mapped_range_start_offset )
3307 && ( offset < mapped_range_end_offset ) )
3308 {
3309 offset -= mapped_range_start_offset;
3310
3311 break;
3312 }
3313 /* Check if the offset is out of bounds
3314 */
3315 if( offset < mapped_range_start_offset )
3316 {
3317 search_element_index = number_of_elements;
3318
3319 break;
3320 }
3321 }
3322 if( search_element_index >= number_of_elements )
3323 {
3324 /* Look for the corresponding element downwards in the array
3325 */
3326 for( search_element_index = initial_element_index;
3327 search_element_index >= 0;
3328 search_element_index-- )
3329 {
3330 if( libcdata_array_get_entry_by_index(
3331 internal_list->mapped_ranges_array,
3332 search_element_index,
3333 (intptr_t **) &mapped_range,
3334 error ) != 1 )
3335 {
3336 libcerror_error_set(
3337 error,
3338 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3339 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3340 "%s: unable to retrieve entry: %d from mapped ranges array.",
3341 function,
3342 search_element_index );
3343
3344 return( -1 );
3345 }
3346 if( libfdata_mapped_range_get(
3347 mapped_range,
3348 &mapped_range_start_offset,
3349 &mapped_range_size,
3350 error ) != 1 )
3351 {
3352 libcerror_error_set(
3353 error,
3354 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3355 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3356 "%s: unable to retrieve values from mapped range: %d.",
3357 function,
3358 search_element_index );
3359
3360 return( -1 );
3361 }
3362 mapped_range_end_offset = mapped_range_start_offset + (off64_t) mapped_range_size;
3363
3364 if( mapped_range_end_offset < mapped_range_start_offset )
3365 {
3366 libcerror_error_set(
3367 error,
3368 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3369 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3370 "%s: invalid element: %d - mapped range value out of bounds.",
3371 function,
3372 search_element_index );
3373
3374 return( -1 );
3375 }
3376 #if defined( HAVE_DEBUG_OUTPUT )
3377 if( libcnotify_verbose != 0 )
3378 {
3379 libcnotify_printf(
3380 "%s: element: %03d\tmapped range: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
3381 function,
3382 search_element_index,
3383 mapped_range_start_offset,
3384 mapped_range_end_offset,
3385 mapped_range_start_offset,
3386 mapped_range_end_offset,
3387 mapped_range_size );
3388 }
3389 #endif
3390 /* Check if the offset is in the mapped range
3391 */
3392 if( ( offset >= mapped_range_start_offset )
3393 && ( offset < mapped_range_end_offset ) )
3394 {
3395 offset -= mapped_range_start_offset;
3396
3397 break;
3398 }
3399 /* Check if the offset is out of bounds
3400 */
3401 if( offset > mapped_range_start_offset )
3402 {
3403 search_element_index--;
3404
3405 break;
3406 }
3407 }
3408 }
3409 if( ( search_element_index >= 0 )
3410 && ( search_element_index < number_of_elements ) )
3411 {
3412 if( offset < 0 )
3413 {
3414 libcerror_error_set(
3415 error,
3416 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3417 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3418 "%s: invalid offset value out of bounds.",
3419 function );
3420
3421 return( -1 );
3422 }
3423 *element_data_offset = offset;
3424
3425 #if defined( HAVE_DEBUG_OUTPUT )
3426 if( libcnotify_verbose != 0 )
3427 {
3428 if( libcdata_array_get_entry_by_index(
3429 internal_list->elements_array,
3430 search_element_index,
3431 (intptr_t **) &element,
3432 error ) != 1 )
3433 {
3434 libcerror_error_set(
3435 error,
3436 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3437 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3438 "%s: unable to retrieve entry: %d from elements array.",
3439 function,
3440 search_element_index );
3441
3442 return( -1 );
3443 }
3444 if( libfdata_list_element_get_data_range(
3445 element,
3446 &element_file_index,
3447 &element_offset,
3448 &element_size,
3449 &element_flags,
3450 error ) != 1 )
3451 {
3452 libcerror_error_set(
3453 error,
3454 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3455 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3456 "%s: unable to retrieve data range of list element: %d.",
3457 function,
3458 search_element_index );
3459
3460 return( -1 );
3461 }
3462 libcnotify_printf(
3463 "%s: element: %03d\tfile index: %03d offset: %" PRIi64 " - %" PRIi64 " (0x%08" PRIx64 " - 0x%08" PRIx64 ") (size: %" PRIu64 ")\n",
3464 function,
3465 search_element_index,
3466 element_file_index,
3467 element_offset,
3468 element_offset + element_size,
3469 element_offset,
3470 element_offset + element_size,
3471 element_size );
3472 }
3473 #endif /* defined( HAVE_DEBUG_OUTPUT ) */
3474
3475 internal_list->current_element_index = search_element_index;
3476
3477 result = 1;
3478 }
3479 if( result == 1 )
3480 {
3481 *element_index = search_element_index;
3482 }
3483 #if defined( HAVE_DEBUG_OUTPUT )
3484 if( libcnotify_verbose != 0 )
3485 {
3486 libcnotify_printf(
3487 "\n" );
3488 }
3489 #endif
3490 return( result );
3491 }
3492
3493 /* Retrieves the list element for a specific offset
3494 * The element_data_offset value is set to the offset relative to the start of the element
3495 * Returns 1 if successful, 0 if not or -1 on error
3496 */
3497 int libfdata_list_get_list_element_at_offset(
3498 libfdata_list_t *list,
3499 off64_t offset,
3500 int *element_index,
3501 off64_t *element_data_offset,
3502 libfdata_list_element_t **element,
3503 libcerror_error_t **error )
3504 {
3505 libfdata_internal_list_t *internal_list = NULL;
3506 static char *function = "libfdata_list_get_list_element_at_offset";
3507 int result = 0;
3508
3509 if( list == NULL )
3510 {
3511 libcerror_error_set(
3512 error,
3513 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3514 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3515 "%s: invalid list.",
3516 function );
3517
3518 return( -1 );
3519 }
3520 internal_list = (libfdata_internal_list_t *) list;
3521
3522 if( offset < 0 )
3523 {
3524 libcerror_error_set(
3525 error,
3526 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3527 LIBCERROR_ARGUMENT_ERROR_VALUE_LESS_THAN_ZERO,
3528 "%s: invalid offset value less than zero.",
3529 function );
3530
3531 return( -1 );
3532 }
3533 if( element_index == NULL )
3534 {
3535 libcerror_error_set(
3536 error,
3537 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3538 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3539 "%s: invalid element index.",
3540 function );
3541
3542 return( -1 );
3543 }
3544 if( element == NULL )
3545 {
3546 libcerror_error_set(
3547 error,
3548 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3549 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3550 "%s: invalid element.",
3551 function );
3552
3553 return( -1 );
3554 }
3555 result = libfdata_list_get_element_index_at_offset(
3556 list,
3557 offset,
3558 element_index,
3559 element_data_offset,
3560 error );
3561
3562 if( result == -1 )
3563 {
3564 libcerror_error_set(
3565 error,
3566 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3567 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3568 "%s: unable to retrieve element index at offset: %" PRIi64 " (0x%08" PRIx64 ").",
3569 function,
3570 offset,
3571 offset );
3572
3573 return( -1 );
3574 }
3575 else if( result != 0 )
3576 {
3577 if( libcdata_array_get_entry_by_index(
3578 internal_list->elements_array,
3579 *element_index,
3580 (intptr_t **) element,
3581 error ) != 1 )
3582 {
3583 libcerror_error_set(
3584 error,
3585 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3586 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3587 "%s: unable to retrieve entry: %d from elements array.",
3588 function,
3589 *element_index );
3590
3591 return( -1 );
3592 }
3593 }
3594 return( result );
3595 }
3596
3597 /* Retrieves the data range of an element at a specific offset
3598 * Returns 1 if successful, 0 if not or -1 on error
3599 */
3600 int libfdata_list_get_element_at_offset(
3601 libfdata_list_t *list,
3602 off64_t offset,
3603 int *element_index,
3604 off64_t *element_data_offset,
3605 int *element_file_index,
3606 off64_t *element_offset,
3607 size64_t *element_size,
3608 uint32_t *element_flags,
3609 libcerror_error_t **error )
3610 {
3611 libfdata_internal_list_t *internal_list = NULL;
3612 libfdata_list_element_t *list_element = NULL;
3613 static char *function = "libfdata_list_get_element_at_offset";
3614 int result = 0;
3615
3616 if( list == NULL )
3617 {
3618 libcerror_error_set(
3619 error,
3620 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3621 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3622 "%s: invalid list.",
3623 function );
3624
3625 return( -1 );
3626 }
3627 internal_list = (libfdata_internal_list_t *) list;
3628
3629 if( element_index == NULL )
3630 {
3631 libcerror_error_set(
3632 error,
3633 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3634 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3635 "%s: invalid element index.",
3636 function );
3637
3638 return( -1 );
3639 }
3640 result = libfdata_list_get_element_index_at_offset(
3641 list,
3642 offset,
3643 element_index,
3644 element_data_offset,
3645 error );
3646
3647 if( result == -1 )
3648 {
3649 libcerror_error_set(
3650 error,
3651 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3652 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3653 "%s: unable to retrieve element index at offset: %" PRIi64 " (0x%08" PRIx64 ").",
3654 function,
3655 offset,
3656 offset );
3657
3658 return( -1 );
3659 }
3660 else if( result != 0 )
3661 {
3662 if( libcdata_array_get_entry_by_index(
3663 internal_list->elements_array,
3664 *element_index,
3665 (intptr_t **) &list_element,
3666 error ) != 1 )
3667 {
3668 libcerror_error_set(
3669 error,
3670 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3671 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3672 "%s: unable to retrieve entry: %d from elements array.",
3673 function,
3674 *element_index );
3675
3676 return( -1 );
3677 }
3678 if( libfdata_list_element_get_data_range(
3679 list_element,
3680 element_file_index,
3681 element_offset,
3682 element_size,
3683 element_flags,
3684 error ) != 1 )
3685 {
3686 libcerror_error_set(
3687 error,
3688 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3689 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3690 "%s: unable to retrieve data range of list element: %d.",
3691 function,
3692 *element_index );
3693
3694 return( -1 );
3695 }
3696 }
3697 return( result );
3698 }
3699
3700 /* List element value functions
3701 */
3702
3703 /* Caches the element value
3704 * Returns 1 if successful or -1 on error
3705 */
3706 int libfdata_list_cache_element_value(
3707 libfdata_list_t *list,
3708 libfdata_cache_t *cache,
3709 int element_index LIBFDATA_ATTRIBUTE_UNUSED,
3710 int element_file_index,
3711 off64_t element_data_offset,
3712 size64_t element_data_size LIBFDATA_ATTRIBUTE_UNUSED,
3713 uint32_t element_data_flags LIBFDATA_ATTRIBUTE_UNUSED,
3714 int64_t element_timestamp,
3715 intptr_t *element_value,
3716 int (*free_element_value)(
3717 intptr_t **element_value,
3718 libcerror_error_t **error ),
3719 uint8_t write_flags,
3720 libcerror_error_t **error )
3721 {
3722 static char *function = "libfdata_list_cache_element_value";
3723
3724 LIBFDATA_UNREFERENCED_PARAMETER( element_index )
3725 LIBFDATA_UNREFERENCED_PARAMETER( element_data_size )
3726 LIBFDATA_UNREFERENCED_PARAMETER( element_data_flags )
3727
3728 if( list == NULL )
3729 {
3730 libcerror_error_set(
3731 error,
3732 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3733 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3734 "%s: invalid list.",
3735 function );
3736
3737 return( -1 );
3738 }
3739 if( libfcache_cache_set_value_by_identifier(
3740 (libfcache_cache_t *) cache,
3741 element_file_index,
3742 element_data_offset,
3743 element_timestamp,
3744 element_value,
3745 free_element_value,
3746 write_flags,
3747 error ) != 1 )
3748 {
3749 libcerror_error_set(
3750 error,
3751 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3752 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3753 "%s: unable to set value in cache.",
3754 function );
3755
3756 return( -1 );
3757 }
3758 return( 1 );
3759 }
3760
3761 /* Retrieves the element value
3762 * Returns 1 if successful or -1 on error
3763 */
3764 int libfdata_list_get_element_value(
3765 libfdata_list_t *list,
3766 intptr_t *file_io_handle,
3767 libfdata_cache_t *cache,
3768 libfdata_list_element_t *element,
3769 intptr_t **element_value,
3770 uint8_t read_flags,
3771 libcerror_error_t **error )
3772 {
3773 libfcache_cache_value_t *cache_value = NULL;
3774 libfdata_internal_list_t *internal_list = NULL;
3775 static char *function = "libfdata_list_get_element_value";
3776 size64_t element_data_size = 0;
3777 off64_t cache_value_offset = (off64_t) -1;
3778 off64_t element_data_offset = 0;
3779 int64_t cache_value_timestamp = 0;
3780 int64_t element_timestamp = 0;
3781 uint32_t element_data_flags = 0;
3782 int cache_value_file_index = -1;
3783 int element_file_index = -1;
3784 int result = 0;
3785
3786 #if defined( HAVE_DEBUG_OUTPUT )
3787 const char *hit_or_miss = NULL;
3788 #endif
3789
3790 if( list == NULL )
3791 {
3792 libcerror_error_set(
3793 error,
3794 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3795 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3796 "%s: invalid list.",
3797 function );
3798
3799 return( -1 );
3800 }
3801 internal_list = (libfdata_internal_list_t *) list;
3802
3803 if( internal_list->read_element_data == NULL )
3804 {
3805 libcerror_error_set(
3806 error,
3807 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3808 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3809 "%s: invalid list - missing read element data function.",
3810 function );
3811
3812 return( -1 );
3813 }
3814 if( libfdata_list_element_get_data_range(
3815 element,
3816 &element_file_index,
3817 &element_data_offset,
3818 &element_data_size,
3819 &element_data_flags,
3820 error ) != 1 )
3821 {
3822 libcerror_error_set(
3823 error,
3824 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3825 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3826 "%s: unable to retrieve data range from list element.",
3827 function );
3828
3829 return( -1 );
3830 }
3831 if( libfdata_list_element_get_timestamp(
3832 element,
3833 &element_timestamp,
3834 error ) != 1 )
3835 {
3836 libcerror_error_set(
3837 error,
3838 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3839 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3840 "%s: unable to retrieve time stamp from list element.",
3841 function );
3842
3843 return( -1 );
3844 }
3845 if( ( read_flags & LIBFDATA_READ_FLAG_IGNORE_CACHE ) == 0 )
3846 {
3847 result = libfcache_cache_get_value_by_identifier(
3848 (libfcache_cache_t *) cache,
3849 element_file_index,
3850 element_data_offset,
3851 element_timestamp,
3852 &cache_value,
3853 error );
3854
3855 if( result == -1 )
3856 {
3857 libcerror_error_set(
3858 error,
3859 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3860 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3861 "%s: unable to retrieve value from cache.",
3862 function );
3863
3864 return( -1 );
3865 }
3866 #if defined( HAVE_DEBUG_OUTPUT )
3867 if( libcnotify_verbose != 0 )
3868 {
3869 if( result == 0 )
3870 {
3871 hit_or_miss = "miss";
3872 }
3873 else
3874 {
3875 hit_or_miss = "hit";
3876 }
3877 libcnotify_printf(
3878 "%s: cache: 0x%08" PRIjx " %s\n",
3879 function,
3880 (intptr_t) cache,
3881 hit_or_miss );
3882 }
3883 #endif /* defined( HAVE_DEBUG_OUTPUT ) */
3884 }
3885 if( result == 0 )
3886 {
3887 #if defined( HAVE_DEBUG_OUTPUT )
3888 if( libcnotify_verbose != 0 )
3889 {
3890 libcnotify_printf(
3891 "%s: reading element data at offset: %" PRIi64 " (0x%08" PRIx64 ") of size: %" PRIu64 "\n",
3892 function,
3893 element_data_offset,
3894 element_data_offset,
3895 element_data_size );
3896 }
3897 #endif
3898 if( internal_list->read_element_data(
3899 internal_list->data_handle,
3900 file_io_handle,
3901 element,
3902 cache,
3903 element_file_index,
3904 element_data_offset,
3905 element_data_size,
3906 element_data_flags,
3907 read_flags,
3908 error ) != 1 )
3909 {
3910 libcerror_error_set(
3911 error,
3912 LIBCERROR_ERROR_DOMAIN_IO,
3913 LIBCERROR_IO_ERROR_READ_FAILED,
3914 "%s: unable to read element data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
3915 function,
3916 element_data_offset,
3917 element_data_offset );
3918
3919 return( -1 );
3920 }
3921 if( libfcache_cache_get_value_by_identifier(
3922 (libfcache_cache_t *) cache,
3923 element_file_index,
3924 element_data_offset,
3925 element_timestamp,
3926 &cache_value,
3927 error ) != 1 )
3928 {
3929 libcerror_error_set(
3930 error,
3931 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3932 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3933 "%s: unable to retrieve value from cache.",
3934 function );
3935
3936 return( -1 );
3937 }
3938 if( libfcache_cache_value_get_identifier(
3939 cache_value,
3940 &cache_value_file_index,
3941 &cache_value_offset,
3942 &cache_value_timestamp,
3943 error ) != 1 )
3944 {
3945 libcerror_error_set(
3946 error,
3947 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3948 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3949 "%s: unable to retrieve cache value identifier.",
3950 function );
3951
3952 return( -1 );
3953 }
3954 if( ( element_file_index != cache_value_file_index )
3955 || ( element_data_offset != cache_value_offset )
3956 || ( element_timestamp != cache_value_timestamp ) )
3957 {
3958 libcerror_error_set(
3959 error,
3960 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3961 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3962 "%s: missing cache value.",
3963 function );
3964
3965 return( -1 );
3966 }
3967 }
3968 if( libfcache_cache_value_get_value(
3969 cache_value,
3970 element_value,
3971 error ) != 1 )
3972 {
3973 libcerror_error_set(
3974 error,
3975 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3976 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3977 "%s: unable to retrieve element value.",
3978 function );
3979
3980 return( -1 );
3981 }
3982 return( 1 );
3983 }
3984
3985 /* Retrieves the value of a specific element
3986 * Returns 1 if successful or -1 on error
3987 */
3988 int libfdata_list_get_element_value_by_index(
3989 libfdata_list_t *list,
3990 intptr_t *file_io_handle,
3991 libfdata_cache_t *cache,
3992 int element_index,
3993 intptr_t **element_value,
3994 uint8_t read_flags,
3995 libcerror_error_t **error )
3996 {
3997 libfdata_internal_list_t *internal_list = NULL;
3998 libfdata_list_element_t *list_element = NULL;
3999 static char *function = "libfdata_list_get_element_value_by_index";
4000
4001 if( list == NULL )
4002 {
4003 libcerror_error_set(
4004 error,
4005 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4006 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4007 "%s: invalid list.",
4008 function );
4009
4010 return( -1 );
4011 }
4012 internal_list = (libfdata_internal_list_t *) list;
4013
4014 if( libcdata_array_get_entry_by_index(
4015 internal_list->elements_array,
4016 element_index,
4017 (intptr_t **) &list_element,
4018 error ) != 1 )
4019 {
4020 libcerror_error_set(
4021 error,
4022 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4023 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
4024 "%s: unable to retrieve entry: %d from elements array.",
4025 function,
4026 element_index );
4027
4028 return( -1 );
4029 }
4030 if( libfdata_list_get_element_value(
4031 list,
4032 file_io_handle,
4033 cache,
4034 list_element,
4035 element_value,
4036 read_flags,
4037 error ) != 1 )
4038 {
4039 libcerror_error_set(
4040 error,
4041 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4042 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
4043 "%s: unable to retrieve element value.",
4044 function );
4045
4046 return( -1 );
4047 }
4048 internal_list->current_element_index = element_index;
4049
4050 return( 1 );
4051 }
4052
4053 /* Retrieves the value an element at a specific offset
4054 * Returns 1 if successful, 0 if not or -1 on error
4055 */
4056 int libfdata_list_get_element_value_at_offset(
4057 libfdata_list_t *list,
4058 intptr_t *file_io_handle,
4059 libfdata_cache_t *cache,
4060 off64_t offset,
4061 int *element_index,
4062 off64_t *element_data_offset,
4063 intptr_t **element_value,
4064 uint8_t read_flags,
4065 libcerror_error_t **error )
4066 {
4067 libfdata_list_element_t *list_element = NULL;
4068 static char *function = "libfdata_list_get_element_value_at_offset";
4069 int result = 0;
4070
4071 if( element_index == NULL )
4072 {
4073 libcerror_error_set(
4074 error,
4075 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4076 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4077 "%s: invalid element index.",
4078 function );
4079
4080 return( -1 );
4081 }
4082 result = libfdata_list_get_list_element_at_offset(
4083 list,
4084 offset,
4085 element_index,
4086 element_data_offset,
4087 &list_element,
4088 error );
4089
4090 if( result == -1 )
4091 {
4092 libcerror_error_set(
4093 error,
4094 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4095 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
4096 "%s: unable to retrieve element at offset: %" PRIi64 " (0x%08" PRIx64 ").",
4097 function,
4098 offset,
4099 offset );
4100
4101 return( -1 );
4102 }
4103 else if( result != 0 )
4104 {
4105 if( libfdata_list_get_element_value(
4106 list,
4107 file_io_handle,
4108 cache,
4109 list_element,
4110 element_value,
4111 read_flags,
4112 error ) != 1 )
4113 {
4114 libcerror_error_set(
4115 error,
4116 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4117 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
4118 "%s: unable to retrieve element: %d value.",
4119 function,
4120 *element_index );
4121
4122 return( -1 );
4123 }
4124 }
4125 return( result );
4126 }
4127
4128 /* Sets the value of a specific element
4129 *
4130 * If the flag LIBFDATA_LIST_ELEMENT_VALUE_FLAG_MANAGED is set the list
4131 * takes over management of the value and the value is freed when
4132 * no longer needed.
4133 *
4134 * Returns 1 if successful or -1 on error
4135 */
4136 int libfdata_list_set_element_value(
4137 libfdata_list_t *list,
4138 intptr_t *file_io_handle LIBFDATA_ATTRIBUTE_UNUSED,
4139 libfdata_cache_t *cache,
4140 libfdata_list_element_t *element,
4141 intptr_t *element_value,
4142 int (*free_element_value)(
4143 intptr_t **element_value,
4144 libcerror_error_t **error ),
4145 uint8_t write_flags,
4146 libcerror_error_t **error )
4147 {
4148 static char *function = "libfdata_list_set_element_value";
4149 size64_t element_data_size = 0;
4150 off64_t element_data_offset = 0;
4151 int64_t element_timestamp = 0;
4152 uint32_t element_data_flags = 0;
4153 int element_file_index = -1;
4154
4155 LIBFDATA_UNREFERENCED_PARAMETER( file_io_handle )
4156
4157 if( list == NULL )
4158 {
4159 libcerror_error_set(
4160 error,
4161 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4162 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4163 "%s: invalid list.",
4164 function );
4165
4166 return( -1 );
4167 }
4168 if( libfdata_list_element_get_data_range(
4169 element,
4170 &element_file_index,
4171 &element_data_offset,
4172 &element_data_size,
4173 &element_data_flags,
4174 error ) != 1 )
4175 {
4176 libcerror_error_set(
4177 error,
4178 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4179 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
4180 "%s: unable to retrieve data range from list element.",
4181 function );
4182
4183 return( -1 );
4184 }
4185 if( libfdata_list_element_get_timestamp(
4186 element,
4187 &element_timestamp,
4188 error ) != 1 )
4189 {
4190 libcerror_error_set(
4191 error,
4192 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4193 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
4194 "%s: unable to retrieve time stamp from list element.",
4195 function );
4196
4197 return( -1 );
4198 }
4199 if( libfcache_cache_set_value_by_identifier(
4200 (libfcache_cache_t *) cache,
4201 element_file_index,
4202 element_data_offset,
4203 element_timestamp,
4204 element_value,
4205 free_element_value,
4206 write_flags,
4207 error ) != 1 )
4208 {
4209 libcerror_error_set(
4210 error,
4211 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4212 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
4213 "%s: unable to set value in cache.",
4214 function );
4215
4216 return( -1 );
4217 }
4218 return( 1 );
4219 }
4220
4221 /* Sets the value of a specific element
4222 *
4223 * If the flag LIBFDATA_LIST_ELEMENT_VALUE_FLAG_MANAGED is set the list
4224 * takes over management of the value and the value is freed when
4225 * no longer needed.
4226 *
4227 * Returns 1 if successful or -1 on error
4228 */
4229 int libfdata_list_set_element_value_by_index(
4230 libfdata_list_t *list,
4231 intptr_t *file_io_handle,
4232 libfdata_cache_t *cache,
4233 int element_index,
4234 intptr_t *element_value,
4235 int (*free_element_value)(
4236 intptr_t **element_value,
4237 libcerror_error_t **error ),
4238 uint8_t write_flags,
4239 libcerror_error_t **error )
4240 {
4241 libfdata_internal_list_t *internal_list = NULL;
4242 libfdata_list_element_t *list_element = NULL;
4243 static char *function = "libfdata_list_set_element_value_by_index";
4244
4245 if( list == NULL )
4246 {
4247 libcerror_error_set(
4248 error,
4249 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4250 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4251 "%s: invalid list.",
4252 function );
4253
4254 return( -1 );
4255 }
4256 internal_list = (libfdata_internal_list_t *) list;
4257
4258 if( libcdata_array_get_entry_by_index(
4259 internal_list->elements_array,
4260 element_index,
4261 (intptr_t **) &list_element,
4262 error ) != 1 )
4263 {
4264 libcerror_error_set(
4265 error,
4266 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4267 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
4268 "%s: unable to retrieve entry: %d from elements array.",
4269 function,
4270 element_index );
4271
4272 return( -1 );
4273 }
4274 if( libfdata_list_set_element_value(
4275 list,
4276 file_io_handle,
4277 cache,
4278 list_element,
4279 element_value,
4280 free_element_value,
4281 write_flags,
4282 error ) != 1 )
4283 {
4284 libcerror_error_set(
4285 error,
4286 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4287 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
4288 "%s: unable to set element value.",
4289 function );
4290
4291 return( -1 );
4292 }
4293 internal_list->current_element_index = element_index;
4294
4295 return( 1 );
4296 }
4297
4298 /* Sets the value of an element at a specific offset
4299 *
4300 * If the flag LIBFDATA_LIST_ELEMENT_VALUE_FLAG_MANAGED is set the list
4301 * takes over management of the value and the value is freed when
4302 * no longer needed.
4303 *
4304 * Returns 1 if successful, 0 if not or -1 on error
4305 */
4306 int libfdata_list_set_element_value_at_offset(
4307 libfdata_list_t *list,
4308 intptr_t *file_io_handle,
4309 libfdata_cache_t *cache,
4310 off64_t offset,
4311 intptr_t *element_value,
4312 int (*free_element_value)(
4313 intptr_t **element_value,
4314 libcerror_error_t **error ),
4315 uint8_t write_flags,
4316 libcerror_error_t **error )
4317 {
4318 libfdata_list_element_t *list_element = NULL;
4319 static char *function = "libfdata_list_set_element_value_at_offset";
4320 off64_t element_data_offset = 0;
4321 int element_index = 0;
4322 int result = 0;
4323
4324 result = libfdata_list_get_list_element_at_offset(
4325 list,
4326 offset,
4327 &element_index,
4328 &element_data_offset,
4329 &list_element,
4330 error );
4331
4332 if( result == -1 )
4333 {
4334 libcerror_error_set(
4335 error,
4336 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4337 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
4338 "%s: unable to retrieve element at offset: %" PRIi64 " (0x%08" PRIx64 ").",
4339 function,
4340 offset,
4341 offset );
4342
4343 return( -1 );
4344 }
4345 else if( result != 0 )
4346 {
4347 if( libfdata_list_set_element_value(
4348 list,
4349 file_io_handle,
4350 cache,
4351 list_element,
4352 element_value,
4353 free_element_value,
4354 write_flags,
4355 error ) != 1 )
4356 {
4357 libcerror_error_set(
4358 error,
4359 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4360 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
4361 "%s: unable to set value of element: %d.",
4362 function,
4363 element_index );
4364
4365 return( -1 );
4366 }
4367 }
4368 return( result );
4369 }
4370
4371 /* Retrieves the size of the list
4372 * Returns 1 if successful or -1 on error
4373 */
4374 int libfdata_list_get_size(
4375 libfdata_list_t *list,
4376 size64_t *size,
4377 libcerror_error_t **error )
4378 {
4379 libfdata_internal_list_t *internal_list = NULL;
4380 static char *function = "libfdata_list_get_size";
4381
4382 if( list == NULL )
4383 {
4384 libcerror_error_set(
4385 error,
4386 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4387 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4388 "%s: invalid list.",
4389 function );
4390
4391 return( -1 );
4392 }
4393 internal_list = (libfdata_internal_list_t *) list;
4394
4395 if( size == NULL )
4396 {
4397 libcerror_error_set(
4398 error,
4399 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4400 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4401 "%s: invalid size.",
4402 function );
4403
4404 return( -1 );
4405 }
4406 if( ( internal_list->flags & LIBFDATA_FLAG_CALCULATE_MAPPED_RANGES ) != 0 )
4407 {
4408 if( libfdata_list_calculate_mapped_ranges(
4409 internal_list,
4410 error ) != 1 )
4411 {
4412 libcerror_error_set(
4413 error,
4414 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4415 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
4416 "%s: unable to calculate mapped ranges.",
4417 function );
4418
4419 return( -1 );
4420 }
4421 }
4422 *size = internal_list->size;
4423
4424 return( 1 );
4425 }
4426