Debian Codegraph @

libcdata/libcdata_list.c

raw symbols SCIP
1 /*
2 * List functions
3 *
4 * Copyright (C) 2006-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 "libcdata_definitions.h"
27 #include "libcdata_libcerror.h"
28 #include "libcdata_libcthreads.h"
29 #include "libcdata_list.h"
30 #include "libcdata_list_element.h"
31 #include "libcdata_types.h"
32
33 /* Creates a list
34 * Make sure the value list is referencing, is set to NULL
35 * Returns 1 if successful or -1 on error
36 */
37 int libcdata_list_initialize(
38 libcdata_list_t **list,
39 libcerror_error_t **error )
40 {
41 libcdata_internal_list_t *internal_list = NULL;
42 static char *function = "libcdata_list_initialize";
43
44 if( list == NULL )
45 {
46 libcerror_error_set(
47 error,
48 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
49 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
50 "%s: invalid list.",
51 function );
52
53 return( -1 );
54 }
55 if( *list != NULL )
56 {
57 libcerror_error_set(
58 error,
59 LIBCERROR_ERROR_DOMAIN_RUNTIME,
60 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
61 "%s: invalid list value already set.",
62 function );
63
64 return( -1 );
65 }
66 internal_list = memory_allocate_structure(
67 libcdata_internal_list_t );
68
69 if( internal_list == NULL )
70 {
71 libcerror_error_set(
72 error,
73 LIBCERROR_ERROR_DOMAIN_MEMORY,
74 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
75 "%s: unable to create list.",
76 function );
77
78 goto on_error;
79 }
80 if( memory_set(
81 internal_list,
82 0,
83 sizeof( libcdata_internal_list_t ) ) == NULL )
84 {
85 libcerror_error_set(
86 error,
87 LIBCERROR_ERROR_DOMAIN_MEMORY,
88 LIBCERROR_MEMORY_ERROR_SET_FAILED,
89 "%s: unable to clear list.",
90 function );
91
92 memory_free(
93 internal_list );
94
95 return( -1 );
96 }
97 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
98 if( libcthreads_read_write_lock_initialize(
99 &( internal_list->read_write_lock ),
100 error ) != 1 )
101 {
102 libcerror_error_set(
103 error,
104 LIBCERROR_ERROR_DOMAIN_RUNTIME,
105 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
106 "%s: unable to initialize read/write lock.",
107 function );
108
109 goto on_error;
110 }
111 #endif
112 *list = (libcdata_list_t *) internal_list;
113
114 return( 1 );
115
116 on_error:
117 if( internal_list != NULL )
118 {
119 memory_free(
120 internal_list );
121 }
122 return( -1 );
123 }
124
125 /* Frees a list including the elements
126 * Uses the value_free_function to free the element value
127 * Returns 1 if successful or -1 on error
128 */
129 int libcdata_list_free(
130 libcdata_list_t **list,
131 int (*value_free_function)(
132 intptr_t **value,
133 libcerror_error_t **error ),
134 libcerror_error_t **error )
135 {
136 libcdata_internal_list_t *internal_list = NULL;
137 static char *function = "libcdata_list_free";
138 int result = 1;
139
140 if( list == NULL )
141 {
142 libcerror_error_set(
143 error,
144 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
145 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
146 "%s: invalid list.",
147 function );
148
149 return( -1 );
150 }
151 if( *list != NULL )
152 {
153 internal_list = (libcdata_internal_list_t *) *list;
154 *list = NULL;
155
156 if( libcdata_list_empty(
157 (libcdata_list_t*) internal_list,
158 value_free_function,
159 error ) != 1 )
160 {
161 libcerror_error_set(
162 error,
163 LIBCERROR_ERROR_DOMAIN_RUNTIME,
164 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
165 "%s: unable to empty list.",
166 function );
167
168 result = -1;
169 }
170 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
171 if( libcthreads_read_write_lock_free(
172 &( internal_list->read_write_lock ),
173 error ) != 1 )
174 {
175 libcerror_error_set(
176 error,
177 LIBCERROR_ERROR_DOMAIN_RUNTIME,
178 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
179 "%s: unable to free read/write lock.",
180 function );
181
182 result = -1;
183 }
184 #endif
185 memory_free(
186 internal_list );
187 }
188 return( result );
189 }
190
191 /* Empties a list and frees the elements
192 * Uses the value_free_function to free the element value
193 * Returns 1 if successful or -1 on error
194 */
195 int libcdata_list_empty(
196 libcdata_list_t *list,
197 int (*value_free_function)(
198 intptr_t **value,
199 libcerror_error_t **error ),
200 libcerror_error_t **error )
201 {
202 libcdata_internal_list_t *internal_list = NULL;
203 libcdata_list_element_t *list_element = NULL;
204 libcdata_list_element_t *next_element = NULL;
205 static char *function = "libcdata_list_empty";
206 int element_index = 0;
207 int number_of_elements = 0;
208 int result = 1;
209
210 if( list == NULL )
211 {
212 libcerror_error_set(
213 error,
214 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
215 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
216 "%s: invalid list.",
217 function );
218
219 return( -1 );
220 }
221 internal_list = (libcdata_internal_list_t *) list;
222
223 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
224 if( libcthreads_read_write_lock_grab_for_write(
225 internal_list->read_write_lock,
226 error ) != 1 )
227 {
228 libcerror_error_set(
229 error,
230 LIBCERROR_ERROR_DOMAIN_RUNTIME,
231 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
232 "%s: unable to grab read/write lock for writing.",
233 function );
234
235 return( -1 );
236 }
237 #endif
238 if( internal_list->number_of_elements > 0 )
239 {
240 number_of_elements = internal_list->number_of_elements;
241
242 list_element = internal_list->first_element;
243
244 for( element_index = 0;
245 element_index < number_of_elements;
246 element_index++ )
247 {
248 if( libcdata_list_element_get_next_element(
249 list_element,
250 &next_element,
251 error ) != 1 )
252 {
253 libcerror_error_set(
254 error,
255 LIBCERROR_ERROR_DOMAIN_RUNTIME,
256 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
257 "%s: unable to retrieve next element from list element: %d.",
258 function,
259 element_index );
260
261 result = -1;
262
263 break;
264 }
265 internal_list->first_element = next_element;
266
267 if( internal_list->last_element == list_element )
268 {
269 internal_list->last_element = next_element;
270 }
271 internal_list->number_of_elements -= 1;
272
273 if( next_element != NULL )
274 {
275 if( libcdata_list_element_set_previous_element(
276 next_element,
277 NULL,
278 error ) != 1 )
279 {
280 libcerror_error_set(
281 error,
282 LIBCERROR_ERROR_DOMAIN_RUNTIME,
283 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
284 "%s: unable to set previous element of list element: %d.",
285 function,
286 element_index + 1 );
287
288 result = -1;
289 }
290 }
291 if( libcdata_list_element_set_next_element(
292 list_element,
293 NULL,
294 error ) != 1 )
295 {
296 libcerror_error_set(
297 error,
298 LIBCERROR_ERROR_DOMAIN_RUNTIME,
299 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
300 "%s: unable to set next element of list element: %d.",
301 function,
302 element_index );
303
304 result = -1;
305 }
306 if( libcdata_list_element_free(
307 &list_element,
308 value_free_function,
309 error ) != 1 )
310 {
311 libcerror_error_set(
312 error,
313 LIBCERROR_ERROR_DOMAIN_RUNTIME,
314 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
315 "%s: unable to free list element: %d.",
316 function,
317 element_index );
318
319 result = -1;
320 }
321 list_element = next_element;
322 }
323 }
324 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
325 if( libcthreads_read_write_lock_release_for_write(
326 internal_list->read_write_lock,
327 error ) != 1 )
328 {
329 libcerror_error_set(
330 error,
331 LIBCERROR_ERROR_DOMAIN_RUNTIME,
332 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
333 "%s: unable to release read/write lock for writing.",
334 function );
335
336 result = -1;
337 }
338 #endif
339 return( result );
340 }
341
342 /* Clones the list and its elements
343 *
344 * The values are cloned using the value_clone_function
345 * On error the values are freed using the value_free_function
346 *
347 * Returns 1 if successful or -1 on error
348 */
349 int libcdata_list_clone(
350 libcdata_list_t **destination_list,
351 libcdata_list_t *source_list,
352 int (*value_free_function)(
353 intptr_t **value,
354 libcerror_error_t **error ),
355 int (*value_clone_function)(
356 intptr_t **destination_value,
357 intptr_t *source_value,
358 libcerror_error_t **error ),
359 libcerror_error_t **error )
360 {
361 libcdata_internal_list_t *internal_destination_list = NULL;
362 libcdata_internal_list_t *internal_source_list = NULL;
363 libcdata_internal_list_element_t *internal_source_list_element = NULL;
364 intptr_t *destination_value = NULL;
365 static char *function = "libcdata_list_clone";
366 int element_index = 0;
367
368 if( destination_list == NULL )
369 {
370 libcerror_error_set(
371 error,
372 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
373 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
374 "%s: invalid destination list.",
375 function );
376
377 return( -1 );
378 }
379 if( *destination_list != NULL )
380 {
381 libcerror_error_set(
382 error,
383 LIBCERROR_ERROR_DOMAIN_RUNTIME,
384 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
385 "%s: invalid destination list already set.",
386 function );
387
388 return( -1 );
389 }
390 if( value_free_function == NULL )
391 {
392 libcerror_error_set(
393 error,
394 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
395 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
396 "%s: invalid value free function.",
397 function );
398
399 return( -1 );
400 }
401 if( value_clone_function == NULL )
402 {
403 libcerror_error_set(
404 error,
405 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
406 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
407 "%s: invalid value clone function.",
408 function );
409
410 return( -1 );
411 }
412 if( source_list == NULL )
413 {
414 *destination_list = NULL;
415
416 return( 1 );
417 }
418 internal_source_list = (libcdata_internal_list_t *) source_list;
419
420 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
421 if( libcthreads_read_write_lock_grab_for_read(
422 internal_source_list->read_write_lock,
423 error ) != 1 )
424 {
425 libcerror_error_set(
426 error,
427 LIBCERROR_ERROR_DOMAIN_RUNTIME,
428 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
429 "%s: unable to grab read/write lock for reading.",
430 function );
431
432 return( -1 );
433 }
434 #endif
435 if( libcdata_list_initialize(
436 (libcdata_list_t **) &internal_destination_list,
437 error ) != 1 )
438 {
439 libcerror_error_set(
440 error,
441 LIBCERROR_ERROR_DOMAIN_RUNTIME,
442 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
443 "%s: unable to create destination list.",
444 function );
445
446 goto on_error;
447 }
448 if( internal_destination_list == NULL )
449 {
450 libcerror_error_set(
451 error,
452 LIBCERROR_ERROR_DOMAIN_RUNTIME,
453 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
454 "%s: missing destination list.",
455 function );
456
457 goto on_error;
458 }
459 if( internal_source_list->first_element != NULL )
460 {
461 internal_source_list_element = (libcdata_internal_list_element_t *) internal_source_list->first_element;
462
463 for( element_index = 0;
464 element_index < internal_source_list->number_of_elements;
465 element_index++ )
466 {
467 if( internal_source_list_element == NULL )
468 {
469 libcerror_error_set(
470 error,
471 LIBCERROR_ERROR_DOMAIN_RUNTIME,
472 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
473 "%s: missing source list element: %d.",
474 function,
475 element_index );
476
477 goto on_error;
478 }
479 if( value_clone_function(
480 &destination_value,
481 internal_source_list_element->value,
482 error ) != 1 )
483 {
484 libcerror_error_set(
485 error,
486 LIBCERROR_ERROR_DOMAIN_RUNTIME,
487 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
488 "%s: unable to create destination value: %d.",
489 function,
490 element_index );
491
492 goto on_error;
493 }
494 if( libcdata_list_append_value(
495 (libcdata_list_t *) internal_destination_list,
496 destination_value,
497 error ) != 1 )
498 {
499 libcerror_error_set(
500 error,
501 LIBCERROR_ERROR_DOMAIN_RUNTIME,
502 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
503 "%s: unable to append value: %d to destination list.",
504 function,
505 element_index );
506
507 goto on_error;
508 }
509 destination_value = NULL;
510
511 internal_source_list_element = (libcdata_internal_list_element_t *) internal_source_list_element->next_element;
512 }
513 }
514 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
515 if( libcthreads_read_write_lock_release_for_read(
516 internal_source_list->read_write_lock,
517 error ) != 1 )
518 {
519 libcerror_error_set(
520 error,
521 LIBCERROR_ERROR_DOMAIN_RUNTIME,
522 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
523 "%s: unable to release read/write lock for reading.",
524 function );
525
526 libcdata_list_free(
527 (libcdata_list_t **) &internal_destination_list,
528 value_free_function,
529 error );
530
531 return( -1 );
532 }
533 #endif
534 *destination_list = (libcdata_list_t *) internal_destination_list;
535
536 return( 1 );
537
538 on_error:
539 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
540 libcthreads_read_write_lock_release_for_read(
541 internal_source_list->read_write_lock,
542 NULL );
543 #endif
544 if( destination_value != NULL )
545 {
546 value_free_function(
547 &destination_value,
548 NULL );
549 }
550 if( internal_destination_list != NULL )
551 {
552 libcdata_list_free(
553 (libcdata_list_t **) &internal_destination_list,
554 value_free_function,
555 error );
556 }
557 return( -1 );
558 }
559
560 /* Retrieves the number of elements in the list
561 * Returns 1 if successful or -1 on error
562 */
563 int libcdata_list_get_number_of_elements(
564 libcdata_list_t *list,
565 int *number_of_elements,
566 libcerror_error_t **error )
567 {
568 libcdata_internal_list_t *internal_list = NULL;
569 static char *function = "libcdata_list_get_number_of_elements";
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 = (libcdata_internal_list_t *) list;
583
584 if( number_of_elements == NULL )
585 {
586 libcerror_error_set(
587 error,
588 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
589 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
590 "%s: invalid number of elements.",
591 function );
592
593 return( -1 );
594 }
595 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
596 if( libcthreads_read_write_lock_grab_for_read(
597 internal_list->read_write_lock,
598 error ) != 1 )
599 {
600 libcerror_error_set(
601 error,
602 LIBCERROR_ERROR_DOMAIN_RUNTIME,
603 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
604 "%s: unable to grab read/write lock for reading.",
605 function );
606
607 return( -1 );
608 }
609 #endif
610 *number_of_elements = internal_list->number_of_elements;
611
612 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
613 if( libcthreads_read_write_lock_release_for_read(
614 internal_list->read_write_lock,
615 error ) != 1 )
616 {
617 libcerror_error_set(
618 error,
619 LIBCERROR_ERROR_DOMAIN_RUNTIME,
620 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
621 "%s: unable to release read/write lock for reading.",
622 function );
623
624 return( -1 );
625 }
626 #endif
627 return( 1 );
628 }
629
630 /* Retrieves the first elements in the list
631 * Returns 1 if successful or -1 on error
632 */
633 int libcdata_list_get_first_element(
634 libcdata_list_t *list,
635 libcdata_list_element_t **element,
636 libcerror_error_t **error )
637 {
638 libcdata_internal_list_t *internal_list = NULL;
639 static char *function = "libcdata_list_get_first_element";
640
641 if( list == NULL )
642 {
643 libcerror_error_set(
644 error,
645 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
646 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
647 "%s: invalid list.",
648 function );
649
650 return( -1 );
651 }
652 internal_list = (libcdata_internal_list_t *) list;
653
654 if( element == NULL )
655 {
656 libcerror_error_set(
657 error,
658 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
659 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
660 "%s: invalid element.",
661 function );
662
663 return( -1 );
664 }
665 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
666 if( libcthreads_read_write_lock_grab_for_read(
667 internal_list->read_write_lock,
668 error ) != 1 )
669 {
670 libcerror_error_set(
671 error,
672 LIBCERROR_ERROR_DOMAIN_RUNTIME,
673 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
674 "%s: unable to grab read/write lock for reading.",
675 function );
676
677 return( -1 );
678 }
679 #endif
680 *element = internal_list->first_element;
681
682 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
683 if( libcthreads_read_write_lock_release_for_read(
684 internal_list->read_write_lock,
685 error ) != 1 )
686 {
687 libcerror_error_set(
688 error,
689 LIBCERROR_ERROR_DOMAIN_RUNTIME,
690 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
691 "%s: unable to release read/write lock for reading.",
692 function );
693
694 return( -1 );
695 }
696 #endif
697 return( 1 );
698 }
699
700 /* Sets the first element in the list
701 * Returns 1 if successful or -1 on error
702 */
703 int libcdata_internal_list_set_first_element(
704 libcdata_internal_list_t *internal_list,
705 libcdata_list_element_t *element,
706 libcerror_error_t **error )
707 {
708 libcdata_list_element_t *backup_first_element = NULL;
709 static char *function = "libcdata_internal_list_set_first_element";
710
711 if( internal_list == NULL )
712 {
713 libcerror_error_set(
714 error,
715 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
716 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
717 "%s: invalid list.",
718 function );
719
720 return( -1 );
721 }
722 backup_first_element = internal_list->first_element;
723
724 if( element != NULL )
725 {
726 if( libcdata_list_element_set_next_element(
727 element,
728 internal_list->first_element,
729 error ) != 1 )
730 {
731 libcerror_error_set(
732 error,
733 LIBCERROR_ERROR_DOMAIN_RUNTIME,
734 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
735 "%s: unable to set next element of element.",
736 function );
737
738 goto on_error;
739 }
740 }
741 if( internal_list->first_element != NULL )
742 {
743 if( libcdata_list_element_set_previous_element(
744 internal_list->first_element,
745 element,
746 error ) != 1 )
747 {
748 libcerror_error_set(
749 error,
750 LIBCERROR_ERROR_DOMAIN_RUNTIME,
751 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
752 "%s: unable to set previous element of first element.",
753 function );
754
755 goto on_error;
756 }
757 }
758 internal_list->first_element = element;
759
760 return( 1 );
761
762 on_error:
763 if( element != NULL )
764 {
765 libcdata_list_element_set_next_element(
766 element,
767 NULL,
768 NULL );
769 }
770 if( backup_first_element != NULL )
771 {
772 libcdata_list_element_set_next_element(
773 backup_first_element,
774 NULL,
775 NULL );
776 }
777 internal_list->first_element = backup_first_element;
778
779 return( -1 );
780 }
781
782 /* Retrieves the last elements in the list
783 * Returns 1 if successful or -1 on error
784 */
785 int libcdata_list_get_last_element(
786 libcdata_list_t *list,
787 libcdata_list_element_t **element,
788 libcerror_error_t **error )
789 {
790 libcdata_internal_list_t *internal_list = NULL;
791 static char *function = "libcdata_list_get_last_element";
792
793 if( list == NULL )
794 {
795 libcerror_error_set(
796 error,
797 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
798 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
799 "%s: invalid list.",
800 function );
801
802 return( -1 );
803 }
804 internal_list = (libcdata_internal_list_t *) list;
805
806 if( element == NULL )
807 {
808 libcerror_error_set(
809 error,
810 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
811 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
812 "%s: invalid element.",
813 function );
814
815 return( -1 );
816 }
817 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
818 if( libcthreads_read_write_lock_grab_for_read(
819 internal_list->read_write_lock,
820 error ) != 1 )
821 {
822 libcerror_error_set(
823 error,
824 LIBCERROR_ERROR_DOMAIN_RUNTIME,
825 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
826 "%s: unable to grab read/write lock for reading.",
827 function );
828
829 return( -1 );
830 }
831 #endif
832 *element = internal_list->last_element;
833
834 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
835 if( libcthreads_read_write_lock_release_for_read(
836 internal_list->read_write_lock,
837 error ) != 1 )
838 {
839 libcerror_error_set(
840 error,
841 LIBCERROR_ERROR_DOMAIN_RUNTIME,
842 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
843 "%s: unable to release read/write lock for reading.",
844 function );
845
846 return( -1 );
847 }
848 #endif
849 return( 1 );
850 }
851
852 /* Sets the last element in the list
853 * Returns 1 if successful or -1 on error
854 */
855 int libcdata_internal_list_set_last_element(
856 libcdata_internal_list_t *internal_list,
857 libcdata_list_element_t *element,
858 libcerror_error_t **error )
859 {
860 libcdata_list_element_t *backup_last_element = NULL;
861 static char *function = "libcdata_internal_list_set_last_element";
862
863 if( internal_list == NULL )
864 {
865 libcerror_error_set(
866 error,
867 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
868 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
869 "%s: invalid list.",
870 function );
871
872 return( -1 );
873 }
874 backup_last_element = internal_list->last_element;
875
876 if( element != NULL )
877 {
878 if( libcdata_list_element_set_previous_element(
879 element,
880 internal_list->last_element,
881 error ) != 1 )
882 {
883 libcerror_error_set(
884 error,
885 LIBCERROR_ERROR_DOMAIN_RUNTIME,
886 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
887 "%s: unable to set previous element of element.",
888 function );
889
890 goto on_error;
891 }
892 }
893 if( internal_list->last_element != NULL )
894 {
895 if( libcdata_list_element_set_next_element(
896 internal_list->last_element,
897 element,
898 error ) != 1 )
899 {
900 libcerror_error_set(
901 error,
902 LIBCERROR_ERROR_DOMAIN_RUNTIME,
903 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
904 "%s: unable to set next element of last element.",
905 function );
906
907 goto on_error;
908 }
909 }
910 internal_list->last_element = element;
911
912 return( 1 );
913
914 on_error:
915 if( element != NULL )
916 {
917 libcdata_list_element_set_previous_element(
918 element,
919 NULL,
920 NULL );
921 }
922 if( backup_last_element != NULL )
923 {
924 libcdata_list_element_set_next_element(
925 backup_last_element,
926 NULL,
927 NULL );
928 }
929 internal_list->last_element = backup_last_element;
930
931 return( -1 );
932 }
933
934 /* Retrieves a specific element from the list
935 * Returns 1 if successful or -1 on error
936 */
937 int libcdata_list_get_element_by_index(
938 libcdata_list_t *list,
939 int element_index,
940 libcdata_list_element_t **element,
941 libcerror_error_t **error )
942 {
943 libcdata_internal_list_t *internal_list = NULL;
944 libcdata_list_element_t *list_element = NULL;
945 static char *function = "libcdata_list_get_element_by_index";
946 int element_iterator = 0;
947
948 if( list == NULL )
949 {
950 libcerror_error_set(
951 error,
952 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
953 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
954 "%s: invalid list.",
955 function );
956
957 return( -1 );
958 }
959 internal_list = (libcdata_internal_list_t *) list;
960
961 if( ( element_index < 0 )
962 || ( element_index >= internal_list->number_of_elements ) )
963 {
964 libcerror_error_set(
965 error,
966 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
967 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
968 "%s: invalid element index value out of bounds.",
969 function );
970
971 return( -1 );
972 }
973 if( element == NULL )
974 {
975 libcerror_error_set(
976 error,
977 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
978 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
979 "%s: invalid list element.",
980 function );
981
982 return( -1 );
983 }
984 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
985 if( libcthreads_read_write_lock_grab_for_read(
986 internal_list->read_write_lock,
987 error ) != 1 )
988 {
989 libcerror_error_set(
990 error,
991 LIBCERROR_ERROR_DOMAIN_RUNTIME,
992 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
993 "%s: unable to grab read/write lock for reading.",
994 function );
995
996 return( -1 );
997 }
998 #endif
999 if( element_index < ( internal_list->number_of_elements / 2 ) )
1000 {
1001 list_element = internal_list->first_element;
1002
1003 for( element_iterator = 0;
1004 element_iterator < element_index;
1005 element_iterator++ )
1006 {
1007 if( list_element == NULL )
1008 {
1009 libcerror_error_set(
1010 error,
1011 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1012 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1013 "%s: corruption detected in element: %d.",
1014 function,
1015 element_iterator );
1016
1017 goto on_error;
1018 }
1019 if( libcdata_list_element_get_next_element(
1020 list_element,
1021 &list_element,
1022 error ) != 1 )
1023 {
1024 libcerror_error_set(
1025 error,
1026 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1027 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1028 "%s: unable to retrieve next element from list element: %d.",
1029 function,
1030 element_index );
1031
1032 goto on_error;
1033 }
1034 }
1035 }
1036 else
1037 {
1038 list_element = internal_list->last_element;
1039
1040 for( element_iterator = ( internal_list->number_of_elements - 1 );
1041 element_iterator > element_index;
1042 element_iterator-- )
1043 {
1044 if( list_element == NULL )
1045 {
1046 libcerror_error_set(
1047 error,
1048 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1049 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1050 "%s: corruption detected in element: %d.",
1051 function,
1052 element_iterator );
1053
1054 goto on_error;
1055 }
1056 if( libcdata_list_element_get_previous_element(
1057 list_element,
1058 &list_element,
1059 error ) != 1 )
1060 {
1061 libcerror_error_set(
1062 error,
1063 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1064 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1065 "%s: unable to retrieve previous element from list element: %d.",
1066 function,
1067 element_index );
1068
1069 goto on_error;
1070 }
1071 }
1072 }
1073 if( list_element == NULL )
1074 {
1075 libcerror_error_set(
1076 error,
1077 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1078 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1079 "%s: corruption detected - missing list element.",
1080 function );
1081
1082 goto on_error;
1083 }
1084 *element = list_element;
1085
1086 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1087 if( libcthreads_read_write_lock_release_for_read(
1088 internal_list->read_write_lock,
1089 error ) != 1 )
1090 {
1091 libcerror_error_set(
1092 error,
1093 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1094 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1095 "%s: unable to release read/write lock for reading.",
1096 function );
1097
1098 return( -1 );
1099 }
1100 #endif
1101 return( 1 );
1102
1103 on_error:
1104 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1105 libcthreads_read_write_lock_release_for_read(
1106 internal_list->read_write_lock,
1107 NULL );
1108 #endif
1109 return( -1 );
1110 }
1111
1112 /* Retrieves a specific value from the list
1113 * Returns 1 if successful or -1 on error
1114 */
1115 int libcdata_list_get_value_by_index(
1116 libcdata_list_t *list,
1117 int element_index,
1118 intptr_t **value,
1119 libcerror_error_t **error )
1120 {
1121 libcdata_list_element_t *list_element = NULL;
1122 static char *function = "libcdata_list_get_value_by_index";
1123
1124 if( libcdata_list_get_element_by_index(
1125 list,
1126 element_index,
1127 &list_element,
1128 error ) != 1 )
1129 {
1130 libcerror_error_set(
1131 error,
1132 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1133 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1134 "%s: unable to retrieve list element: %d from list.",
1135 function,
1136 element_index );
1137
1138 return( -1 );
1139 }
1140 if( libcdata_list_element_get_value(
1141 list_element,
1142 value,
1143 error ) != 1 )
1144 {
1145 libcerror_error_set(
1146 error,
1147 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1148 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1149 "%s: unable to retrieve value from list element: %d.",
1150 function,
1151 element_index );
1152
1153 return( -1 );
1154 }
1155 return( 1 );
1156 }
1157
1158 /* Prepends a list element to the list
1159 * Returns 1 if successful or -1 on error
1160 */
1161 int libcdata_list_prepend_element(
1162 libcdata_list_t *list,
1163 libcdata_list_element_t *element,
1164 libcerror_error_t **error )
1165 {
1166 libcdata_internal_list_t *internal_list = NULL;
1167 libcdata_list_element_t *next_element = NULL;
1168 libcdata_list_element_t *previous_element = NULL;
1169 static char *function = "libcdata_list_prepend_element";
1170 int result = 1;
1171
1172 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1173 libcdata_list_element_t *backup_first_element = NULL;
1174 libcdata_list_element_t *backup_last_element = NULL;
1175 #endif
1176
1177 if( list == NULL )
1178 {
1179 libcerror_error_set(
1180 error,
1181 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1182 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1183 "%s: invalid list.",
1184 function );
1185
1186 return( -1 );
1187 }
1188 internal_list = (libcdata_internal_list_t *) list;
1189
1190 if( internal_list->number_of_elements == 0 )
1191 {
1192 if( internal_list->first_element != NULL )
1193 {
1194 libcerror_error_set(
1195 error,
1196 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1197 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1198 "%s: corruption detected - first element already set.",
1199 function );
1200
1201 return( -1 );
1202 }
1203 if( internal_list->last_element != NULL )
1204 {
1205 libcerror_error_set(
1206 error,
1207 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1208 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1209 "%s: corruption detected - last element already set.",
1210 function );
1211
1212 return( -1 );
1213 }
1214 }
1215 else
1216 {
1217 if( internal_list->first_element == NULL )
1218 {
1219 libcerror_error_set(
1220 error,
1221 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1222 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1223 "%s: corruption detected - missing first element.",
1224 function );
1225
1226 return( -1 );
1227 }
1228 if( internal_list->last_element == NULL )
1229 {
1230 libcerror_error_set(
1231 error,
1232 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1233 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1234 "%s: corruption detected - missing last element.",
1235 function );
1236
1237 return( -1 );
1238 }
1239 }
1240 if( element == NULL )
1241 {
1242 libcerror_error_set(
1243 error,
1244 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1245 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1246 "%s: invalid list element.",
1247 function );
1248
1249 return( -1 );
1250 }
1251 if( libcdata_list_element_get_elements(
1252 element,
1253 &previous_element,
1254 &next_element,
1255 error ) != 1 )
1256 {
1257 libcerror_error_set(
1258 error,
1259 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1260 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1261 "%s: unable to retrieve previous and next element from list element.",
1262 function );
1263
1264 return( -1 );
1265 }
1266 if( ( previous_element != NULL )
1267 || ( next_element != NULL ) )
1268 {
1269 libcerror_error_set(
1270 error,
1271 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1272 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
1273 "%s: list element already part of a list.",
1274 function );
1275
1276 return( -1 );
1277 }
1278 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1279 if( libcthreads_read_write_lock_grab_for_write(
1280 internal_list->read_write_lock,
1281 error ) != 1 )
1282 {
1283 libcerror_error_set(
1284 error,
1285 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1286 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1287 "%s: unable to grab read/write lock for writing.",
1288 function );
1289
1290 return( -1 );
1291 }
1292 backup_first_element = internal_list->first_element;
1293 backup_last_element = internal_list->last_element;
1294
1295 #endif
1296 result = libcdata_list_element_set_next_element(
1297 element,
1298 internal_list->first_element,
1299 error );
1300
1301 if( result != 1 )
1302 {
1303 libcerror_error_set(
1304 error,
1305 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1306 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1307 "%s: unable to set next element of list element.",
1308 function );
1309
1310 result = -1;
1311 }
1312 if( result == 1 )
1313 {
1314 if( internal_list->first_element != NULL )
1315 {
1316 result = libcdata_list_element_set_previous_element(
1317 internal_list->first_element,
1318 element,
1319 error );
1320
1321 if( result != 1 )
1322 {
1323 libcerror_error_set(
1324 error,
1325 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1326 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1327 "%s: unable to set previous element of first element.",
1328 function );
1329
1330 libcdata_list_element_set_next_element(
1331 element,
1332 NULL,
1333 NULL );
1334
1335 result = -1;
1336 }
1337 }
1338 }
1339 if( result == 1 )
1340 {
1341 internal_list->first_element = element;
1342
1343 if( internal_list->last_element == NULL )
1344 {
1345 internal_list->last_element = element;
1346 }
1347 internal_list->number_of_elements += 1;
1348 }
1349 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1350 if( libcthreads_read_write_lock_release_for_write(
1351 internal_list->read_write_lock,
1352 error ) != 1 )
1353 {
1354 libcerror_error_set(
1355 error,
1356 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1357 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1358 "%s: unable to release read/write lock for writing.",
1359 function );
1360
1361 goto on_error;
1362 }
1363 #endif
1364 return( result );
1365
1366 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1367 on_error:
1368 if( result == 1 )
1369 {
1370 libcdata_list_element_set_next_element(
1371 element,
1372 NULL,
1373 NULL );
1374
1375 libcdata_list_element_set_previous_element(
1376 backup_first_element,
1377 NULL,
1378 NULL );
1379
1380 internal_list->first_element = backup_first_element;
1381 internal_list->last_element = backup_last_element;
1382
1383 internal_list->number_of_elements -= 1;
1384 }
1385 return( -1 );
1386 #endif
1387 }
1388
1389 /* Prepends a value to the list
1390 * Creates a new list element
1391 * Returns 1 if successful or -1 on error
1392 */
1393 int libcdata_list_prepend_value(
1394 libcdata_list_t *list,
1395 intptr_t *value,
1396 libcerror_error_t **error )
1397 {
1398 libcdata_list_element_t *list_element = NULL;
1399 static char *function = "libcdata_list_prepend_value";
1400
1401 if( libcdata_list_element_initialize(
1402 &list_element,
1403 error ) != 1 )
1404 {
1405 libcerror_error_set(
1406 error,
1407 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1408 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1409 "%s: unable to create list element.",
1410 function );
1411
1412 goto on_error;
1413 }
1414 if( libcdata_list_prepend_element(
1415 list,
1416 list_element,
1417 error ) != 1 )
1418 {
1419 libcerror_error_set(
1420 error,
1421 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1422 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1423 "%s: unable to prepend element to list.",
1424 function );
1425
1426 goto on_error;
1427 }
1428 if( libcdata_list_element_set_value(
1429 list_element,
1430 value,
1431 error ) != 1 )
1432 {
1433 libcerror_error_set(
1434 error,
1435 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1436 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1437 "%s: unable to set value of list element.",
1438 function );
1439
1440 goto on_error;
1441 }
1442 return( 1 );
1443
1444 on_error:
1445 if( list_element != NULL )
1446 {
1447 libcdata_list_element_free(
1448 &list_element,
1449 NULL,
1450 NULL );
1451 }
1452 return( -1 );
1453 }
1454
1455 /* Appends a list element to the list
1456 * Returns 1 if successful or -1 on error
1457 */
1458 int libcdata_list_append_element(
1459 libcdata_list_t *list,
1460 libcdata_list_element_t *element,
1461 libcerror_error_t **error )
1462 {
1463 libcdata_internal_list_t *internal_list = NULL;
1464 libcdata_list_element_t *next_element = NULL;
1465 libcdata_list_element_t *previous_element = NULL;
1466 static char *function = "libcdata_list_append_element";
1467 int result = 1;
1468
1469 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1470 libcdata_list_element_t *backup_first_element = NULL;
1471 libcdata_list_element_t *backup_last_element = NULL;
1472 #endif
1473
1474 if( list == NULL )
1475 {
1476 libcerror_error_set(
1477 error,
1478 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1479 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1480 "%s: invalid list.",
1481 function );
1482
1483 return( -1 );
1484 }
1485 internal_list = (libcdata_internal_list_t *) list;
1486
1487 if( internal_list->number_of_elements == 0 )
1488 {
1489 if( internal_list->first_element != NULL )
1490 {
1491 libcerror_error_set(
1492 error,
1493 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1494 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1495 "%s: corruption detected - first element already set.",
1496 function );
1497
1498 return( -1 );
1499 }
1500 if( internal_list->last_element != NULL )
1501 {
1502 libcerror_error_set(
1503 error,
1504 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1505 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1506 "%s: corruption detected - last element already set.",
1507 function );
1508
1509 return( -1 );
1510 }
1511 }
1512 else
1513 {
1514 if( internal_list->first_element == NULL )
1515 {
1516 libcerror_error_set(
1517 error,
1518 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1519 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1520 "%s: corruption detected - missing first element.",
1521 function );
1522
1523 return( -1 );
1524 }
1525 if( internal_list->last_element == NULL )
1526 {
1527 libcerror_error_set(
1528 error,
1529 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1530 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1531 "%s: corruption detected - missing last element.",
1532 function );
1533
1534 return( -1 );
1535 }
1536 }
1537 if( libcdata_list_element_get_elements(
1538 element,
1539 &previous_element,
1540 &next_element,
1541 error ) != 1 )
1542 {
1543 libcerror_error_set(
1544 error,
1545 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1546 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1547 "%s: unable to retrieve previous and next element from list element.",
1548 function );
1549
1550 return( -1 );
1551 }
1552 if( ( previous_element != NULL )
1553 || ( next_element != NULL ) )
1554 {
1555 libcerror_error_set(
1556 error,
1557 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1558 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
1559 "%s: list element already part of a list.",
1560 function );
1561
1562 return( -1 );
1563 }
1564 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1565 if( libcthreads_read_write_lock_grab_for_write(
1566 internal_list->read_write_lock,
1567 error ) != 1 )
1568 {
1569 libcerror_error_set(
1570 error,
1571 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1572 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1573 "%s: unable to grab read/write lock for writing.",
1574 function );
1575
1576 return( -1 );
1577 }
1578 backup_first_element = internal_list->first_element;
1579 backup_last_element = internal_list->last_element;
1580
1581 #endif
1582 result = libcdata_list_element_set_previous_element(
1583 element,
1584 internal_list->last_element,
1585 error );
1586
1587 if( result != 1 )
1588 {
1589 libcerror_error_set(
1590 error,
1591 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1592 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1593 "%s: unable to set previous element of list element.",
1594 function );
1595
1596 result = -1;
1597 }
1598 if( result == 1 )
1599 {
1600 if( internal_list->last_element != NULL )
1601 {
1602 result = libcdata_list_element_set_next_element(
1603 internal_list->last_element,
1604 element,
1605 error );
1606
1607 if( result != 1 )
1608 {
1609 libcerror_error_set(
1610 error,
1611 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1612 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1613 "%s: unable to set next element of last element.",
1614 function );
1615
1616 libcdata_list_element_set_previous_element(
1617 element,
1618 NULL,
1619 NULL );
1620
1621 result = -1;
1622 }
1623 }
1624 }
1625 if( result == 1 )
1626 {
1627 if( internal_list->first_element == NULL )
1628 {
1629 internal_list->first_element = element;
1630 }
1631 internal_list->last_element = element;
1632
1633 internal_list->number_of_elements += 1;
1634 }
1635 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1636 if( libcthreads_read_write_lock_release_for_write(
1637 internal_list->read_write_lock,
1638 error ) != 1 )
1639 {
1640 libcerror_error_set(
1641 error,
1642 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1643 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1644 "%s: unable to release read/write lock for writing.",
1645 function );
1646
1647 goto on_error;
1648 }
1649 #endif
1650 return( result );
1651
1652 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1653 on_error:
1654 if( result == 1 )
1655 {
1656 libcdata_list_element_set_previous_element(
1657 element,
1658 NULL,
1659 NULL );
1660
1661 libcdata_list_element_set_next_element(
1662 backup_last_element,
1663 NULL,
1664 NULL );
1665
1666 internal_list->first_element = backup_first_element;
1667 internal_list->last_element = backup_last_element;
1668
1669 internal_list->number_of_elements -= 1;
1670 }
1671 return( -1 );
1672 #endif
1673 }
1674
1675 /* Appends a value to the list
1676 * Creates a new list element
1677 * Returns 1 if successful or -1 on error
1678 */
1679 int libcdata_list_append_value(
1680 libcdata_list_t *list,
1681 intptr_t *value,
1682 libcerror_error_t **error )
1683 {
1684 libcdata_list_element_t *element = NULL;
1685 static char *function = "libcdata_list_append_value";
1686
1687 if( libcdata_list_element_initialize(
1688 &element,
1689 error ) != 1 )
1690 {
1691 libcerror_error_set(
1692 error,
1693 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1694 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1695 "%s: unable to create list element.",
1696 function );
1697
1698 goto on_error;
1699 }
1700 if( libcdata_list_element_set_value(
1701 element,
1702 value,
1703 error ) != 1 )
1704 {
1705 libcerror_error_set(
1706 error,
1707 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1708 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1709 "%s: unable to set value of list element.",
1710 function );
1711
1712 goto on_error;
1713 }
1714 if( libcdata_list_append_element(
1715 list,
1716 element,
1717 error ) != 1 )
1718 {
1719 libcerror_error_set(
1720 error,
1721 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1722 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1723 "%s: unable to append element to list.",
1724 function );
1725
1726 goto on_error;
1727 }
1728 return( 1 );
1729
1730 on_error:
1731 if( element != NULL )
1732 {
1733 libcdata_list_element_free(
1734 &element,
1735 NULL,
1736 NULL );
1737 }
1738 return( -1 );
1739 }
1740
1741 /* Retrieves the element which the element should be inserted before
1742 *
1743 * Uses the value_compare_function to determine the order of the entries
1744 * The value_compare_function should return LIBCDATA_COMPARE_LESS,
1745 * LIBCDATA_COMPARE_EQUAL, LIBCDATA_COMPARE_GREATER if successful or -1 on error
1746 *
1747 * Duplicate entries are allowed by default and inserted after the last duplicate value.
1748 * Only allowing unique entries can be enforced by setting the flag LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES
1749 *
1750 * On return element will be set to NULL if the element should be inserted at the end of the list.
1751 *
1752 * Returns 1 if successful, 0 if a list element containing the value already exists or -1 on error
1753 */
1754 int libcdata_internal_list_insert_element_find_element(
1755 libcdata_internal_list_t *internal_list,
1756 intptr_t *value_to_insert,
1757 int (*value_compare_function)(
1758 intptr_t *first_value,
1759 intptr_t *second_value,
1760 libcerror_error_t **error ),
1761 uint8_t insert_flags,
1762 int *element_index,
1763 libcdata_list_element_t **element,
1764 libcerror_error_t **error )
1765 {
1766 libcdata_list_element_t *list_element = NULL;
1767 intptr_t *list_element_value = NULL;
1768 static char *function = "libcdata_internal_list_insert_element_find_element";
1769 int compare_result = 0;
1770 int result = 1;
1771 int safe_element_index = 0;
1772
1773 if( internal_list == NULL )
1774 {
1775 libcerror_error_set(
1776 error,
1777 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1778 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1779 "%s: invalid list.",
1780 function );
1781
1782 return( -1 );
1783 }
1784 if( value_compare_function == NULL )
1785 {
1786 libcerror_error_set(
1787 error,
1788 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1789 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1790 "%s: invalid value compare function.",
1791 function );
1792
1793 return( -1 );
1794 }
1795 if( ( insert_flags & ~( LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES ) ) != 0 )
1796 {
1797 libcerror_error_set(
1798 error,
1799 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1800 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1801 "%s: unsupported insert flags: 0x%02" PRIx8 ".",
1802 function,
1803 insert_flags );
1804
1805 return( -1 );
1806 }
1807 if( element_index == NULL )
1808 {
1809 libcerror_error_set(
1810 error,
1811 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1812 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1813 "%s: invalid element index.",
1814 function );
1815
1816 return( -1 );
1817 }
1818 if( element == NULL )
1819 {
1820 libcerror_error_set(
1821 error,
1822 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1823 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1824 "%s: invalid element.",
1825 function );
1826
1827 return( -1 );
1828 }
1829 list_element = internal_list->first_element;
1830 compare_result = LIBCDATA_COMPARE_GREATER;
1831
1832 for( safe_element_index = 0;
1833 safe_element_index < internal_list->number_of_elements;
1834 safe_element_index++ )
1835 {
1836 if( libcdata_list_element_get_value(
1837 list_element,
1838 &list_element_value,
1839 error ) != 1 )
1840 {
1841 libcerror_error_set(
1842 error,
1843 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1844 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1845 "%s: unable to retrieve value from list element: %d.",
1846 function,
1847 safe_element_index );
1848
1849 return( -1 );
1850 }
1851 compare_result = value_compare_function(
1852 value_to_insert,
1853 list_element_value,
1854 error );
1855
1856 if( compare_result == -1 )
1857 {
1858 libcerror_error_set(
1859 error,
1860 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1861 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1862 "%s: unable to compare list element: %d.",
1863 function,
1864 safe_element_index );
1865
1866 return( -1 );
1867 }
1868 else if( compare_result == LIBCDATA_COMPARE_EQUAL )
1869 {
1870 if( ( insert_flags & LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES ) != 0 )
1871 {
1872 result = 0;
1873
1874 break;
1875 }
1876 }
1877 else if( compare_result == LIBCDATA_COMPARE_LESS )
1878 {
1879 break;
1880 }
1881 else if( compare_result != LIBCDATA_COMPARE_GREATER )
1882 {
1883 libcerror_error_set(
1884 error,
1885 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1886 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1887 "%s: unsupported value compare function return value: %d.",
1888 function,
1889 compare_result );
1890
1891 return( -1 );
1892 }
1893 if( libcdata_list_element_get_next_element(
1894 list_element,
1895 &list_element,
1896 error ) != 1 )
1897 {
1898 libcerror_error_set(
1899 error,
1900 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1901 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1902 "%s: unable to retrieve next element from list element: %d.",
1903 function,
1904 safe_element_index );
1905
1906 return( -1 );
1907 }
1908 }
1909 if( ( compare_result == LIBCDATA_COMPARE_EQUAL )
1910 || ( compare_result == LIBCDATA_COMPARE_LESS ) )
1911 {
1912 *element_index = safe_element_index;
1913 *element = list_element;
1914 }
1915 else
1916 {
1917 *element_index = internal_list->number_of_elements;
1918 *element = NULL;
1919 }
1920 return( result );
1921 }
1922
1923 /* Inserts the element before the list element
1924 * If list_element is NULL the element is inserted before or as the first element in the list
1925 * Returns 1 if successful, or -1 on error
1926 */
1927 int libcdata_internal_list_insert_element_before_element(
1928 libcdata_internal_list_t *internal_list,
1929 libcdata_list_element_t *list_element,
1930 libcdata_list_element_t *element_to_insert,
1931 libcerror_error_t **error )
1932 {
1933 libcdata_list_element_t *backup_first_element = NULL;
1934 libcdata_list_element_t *backup_last_element = NULL;
1935 libcdata_list_element_t *previous_element = NULL;
1936 static char *function = "libcdata_internal_list_insert_element_before_element";
1937
1938 if( internal_list == NULL )
1939 {
1940 libcerror_error_set(
1941 error,
1942 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1943 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1944 "%s: invalid list.",
1945 function );
1946
1947 return( -1 );
1948 }
1949 if( element_to_insert == NULL )
1950 {
1951 libcerror_error_set(
1952 error,
1953 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1954 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1955 "%s: invalid element to insert.",
1956 function );
1957
1958 return( -1 );
1959 }
1960 backup_first_element = internal_list->first_element;
1961 backup_last_element = internal_list->last_element;
1962
1963 if( list_element != NULL )
1964 {
1965 if( libcdata_list_element_get_previous_element(
1966 list_element,
1967 &previous_element,
1968 error ) != 1 )
1969 {
1970 libcerror_error_set(
1971 error,
1972 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1973 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1974 "%s: unable to retrieve previous element from list element.",
1975 function );
1976
1977 return( -1 );
1978 }
1979 }
1980 if( internal_list->number_of_elements == 0 )
1981 {
1982 internal_list->first_element = element_to_insert;
1983 internal_list->last_element = element_to_insert;
1984 }
1985 else if( list_element == NULL )
1986 {
1987 if( libcdata_internal_list_set_last_element(
1988 internal_list,
1989 element_to_insert,
1990 error ) != 1 )
1991 {
1992 libcerror_error_set(
1993 error,
1994 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1995 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1996 "%s: unable to set last list element.",
1997 function );
1998
1999 goto on_error;
2000 }
2001 }
2002 else
2003 {
2004 if( libcdata_list_element_set_elements(
2005 element_to_insert,
2006 previous_element,
2007 list_element,
2008 error ) != 1 )
2009 {
2010 libcerror_error_set(
2011 error,
2012 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2013 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2014 "%s: unable to set previous and next element of element to insert.",
2015 function );
2016
2017 goto on_error;
2018 }
2019 if( internal_list->first_element == list_element )
2020 {
2021 internal_list->first_element = element_to_insert;
2022 }
2023 else
2024 {
2025 if( libcdata_list_element_set_next_element(
2026 previous_element,
2027 element_to_insert,
2028 error ) != 1 )
2029 {
2030 libcerror_error_set(
2031 error,
2032 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2033 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2034 "%s: unable to set next element of previous list element.",
2035 function );
2036
2037 goto on_error;
2038 }
2039 }
2040 if( libcdata_list_element_set_previous_element(
2041 list_element,
2042 element_to_insert,
2043 error ) != 1 )
2044 {
2045 libcerror_error_set(
2046 error,
2047 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2048 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2049 "%s: unable to set previous element of list element.",
2050 function );
2051
2052 goto on_error;
2053 }
2054 }
2055 internal_list->number_of_elements += 1;
2056
2057 return( 1 );
2058
2059 on_error:
2060 if( element_to_insert != NULL )
2061 {
2062 libcdata_list_element_set_elements(
2063 element_to_insert,
2064 NULL,
2065 NULL,
2066 NULL );
2067 }
2068 if( previous_element != NULL )
2069 {
2070 libcdata_list_element_set_next_element(
2071 previous_element,
2072 list_element,
2073 NULL );
2074 }
2075 if( list_element != NULL )
2076 {
2077 libcdata_list_element_set_previous_element(
2078 list_element,
2079 previous_element,
2080 NULL );
2081 }
2082 internal_list->first_element = backup_first_element;
2083 internal_list->last_element = backup_last_element;
2084
2085 return( -1 );
2086 }
2087
2088 /* Inserts a list element into the list
2089 *
2090 * Uses the value_compare_function to determine the order of the entries
2091 * The value_compare_function should return LIBCDATA_COMPARE_LESS,
2092 * LIBCDATA_COMPARE_EQUAL, LIBCDATA_COMPARE_GREATER if successful or -1 on error
2093 *
2094 * Duplicate entries are allowed by default and inserted after the last duplicate value.
2095 * Only allowing unique entries can be enforced by setting the flag LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES
2096 *
2097 * Returns 1 if successful, 0 if the list element already exists or -1 on error
2098 */
2099 int libcdata_list_insert_element(
2100 libcdata_list_t *list,
2101 libcdata_list_element_t *element_to_insert,
2102 int (*value_compare_function)(
2103 intptr_t *first_value,
2104 intptr_t *second_value,
2105 libcerror_error_t **error ),
2106 uint8_t insert_flags,
2107 libcerror_error_t **error )
2108 {
2109 libcdata_list_element_t *existing_element = NULL;
2110 static char *function = "libcdata_list_insert_element";
2111 int result = 0;
2112
2113 result = libcdata_list_insert_element_with_existing(
2114 list,
2115 element_to_insert,
2116 value_compare_function,
2117 insert_flags,
2118 &existing_element,
2119 error );
2120
2121 if( result == -1 )
2122 {
2123 libcerror_error_set(
2124 error,
2125 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2126 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2127 "%s: unable to insert element into list.",
2128 function );
2129
2130 return( -1 );
2131 }
2132 return( result );
2133 }
2134
2135 /* Inserts a list element into the list
2136 *
2137 * Uses the value_compare_function to determine the order of the entries
2138 * The value_compare_function should return LIBCDATA_COMPARE_LESS,
2139 * LIBCDATA_COMPARE_EQUAL, LIBCDATA_COMPARE_GREATER if successful or -1 on error
2140 *
2141 * Duplicate entries are allowed by default and inserted after the last duplicate value.
2142 * Only allowing unique entries can be enforced by setting the flag LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES
2143 *
2144 * Returns 1 if successful, 0 if the list element already exists or -1 on error
2145 */
2146 int libcdata_list_insert_element_with_existing(
2147 libcdata_list_t *list,
2148 libcdata_list_element_t *element_to_insert,
2149 int (*value_compare_function)(
2150 intptr_t *first_value,
2151 intptr_t *second_value,
2152 libcerror_error_t **error ),
2153 uint8_t insert_flags,
2154 libcdata_list_element_t **existing_element,
2155 libcerror_error_t **error )
2156 {
2157 libcdata_internal_list_t *internal_list = NULL;
2158 libcdata_list_element_t *next_element = NULL;
2159 libcdata_list_element_t *previous_element = NULL;
2160 libcdata_list_element_t *safe_existing_element = NULL;
2161 intptr_t *value_to_insert = NULL;
2162 static char *function = "libcdata_list_insert_element_with_existing";
2163 int element_index = 0;
2164 int result = 1;
2165
2166 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2167 libcdata_list_element_t *backup_first_element = NULL;
2168 libcdata_list_element_t *backup_last_element = NULL;
2169 #endif
2170
2171 if( list == NULL )
2172 {
2173 libcerror_error_set(
2174 error,
2175 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2176 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2177 "%s: invalid list.",
2178 function );
2179
2180 return( -1 );
2181 }
2182 internal_list = (libcdata_internal_list_t *) list;
2183
2184 if( internal_list->number_of_elements == 0 )
2185 {
2186 if( internal_list->first_element != NULL )
2187 {
2188 libcerror_error_set(
2189 error,
2190 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2191 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2192 "%s: corruption detected - first element already set.",
2193 function );
2194
2195 return( -1 );
2196 }
2197 if( internal_list->last_element != NULL )
2198 {
2199 libcerror_error_set(
2200 error,
2201 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2202 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2203 "%s: corruption detected - last element already set.",
2204 function );
2205
2206 return( -1 );
2207 }
2208 }
2209 else
2210 {
2211 if( internal_list->first_element == NULL )
2212 {
2213 libcerror_error_set(
2214 error,
2215 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2216 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2217 "%s: corruption detected - missing first element.",
2218 function );
2219
2220 return( -1 );
2221 }
2222 if( internal_list->last_element == NULL )
2223 {
2224 libcerror_error_set(
2225 error,
2226 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2227 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2228 "%s: corruption detected - missing last element.",
2229 function );
2230
2231 return( -1 );
2232 }
2233 }
2234 if( element_to_insert == NULL )
2235 {
2236 libcerror_error_set(
2237 error,
2238 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2239 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2240 "%s: invalid element to insert.",
2241 function );
2242
2243 return( -1 );
2244 }
2245 if( value_compare_function == NULL )
2246 {
2247 libcerror_error_set(
2248 error,
2249 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2250 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2251 "%s: invalid value compare function.",
2252 function );
2253
2254 return( -1 );
2255 }
2256 if( ( insert_flags & ~( LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES ) ) != 0 )
2257 {
2258 libcerror_error_set(
2259 error,
2260 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2261 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2262 "%s: unsupported insert flags: 0x%02" PRIx8 ".",
2263 function,
2264 insert_flags );
2265
2266 return( -1 );
2267 }
2268 if( existing_element == NULL )
2269 {
2270 libcerror_error_set(
2271 error,
2272 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2273 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2274 "%s: invalid existing element.",
2275 function );
2276
2277 return( -1 );
2278 }
2279 *existing_element = NULL;
2280
2281 if( libcdata_list_element_get_elements(
2282 element_to_insert,
2283 &previous_element,
2284 &next_element,
2285 error ) != 1 )
2286 {
2287 libcerror_error_set(
2288 error,
2289 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2290 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2291 "%s: unable to retrieve previous and next element from element to insert.",
2292 function );
2293
2294 return( -1 );
2295 }
2296 if( ( previous_element != NULL )
2297 || ( next_element != NULL ) )
2298 {
2299 libcerror_error_set(
2300 error,
2301 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2302 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
2303 "%s: invalid element to insert - already part of a list.",
2304 function );
2305
2306 return( -1 );
2307 }
2308 if( libcdata_list_element_get_value(
2309 element_to_insert,
2310 &value_to_insert,
2311 error ) != 1 )
2312 {
2313 libcerror_error_set(
2314 error,
2315 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2316 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2317 "%s: unable to retrieve value from element to insert.",
2318 function );
2319
2320 return( -1 );
2321 }
2322 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2323 if( libcthreads_read_write_lock_grab_for_write(
2324 internal_list->read_write_lock,
2325 error ) != 1 )
2326 {
2327 libcerror_error_set(
2328 error,
2329 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2330 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2331 "%s: unable to grab read/write lock for writing.",
2332 function );
2333
2334 return( -1 );
2335 }
2336 backup_first_element = internal_list->first_element;
2337 backup_last_element = internal_list->last_element;
2338
2339 #endif /* defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA ) */
2340
2341 result = libcdata_internal_list_insert_element_find_element(
2342 internal_list,
2343 value_to_insert,
2344 value_compare_function,
2345 insert_flags,
2346 &element_index,
2347 &safe_existing_element,
2348 error );
2349
2350 if( result == -1 )
2351 {
2352 libcerror_error_set(
2353 error,
2354 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2355 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2356 "%s: unable to find element in list.",
2357 function );
2358
2359 result = -1;
2360 }
2361 else if( result == 0 )
2362 {
2363 *existing_element = safe_existing_element;
2364 }
2365 else
2366 {
2367 if( safe_existing_element != NULL )
2368 {
2369 if( libcdata_list_element_get_elements(
2370 safe_existing_element,
2371 &previous_element,
2372 &next_element,
2373 error ) != 1 )
2374 {
2375 libcerror_error_set(
2376 error,
2377 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2378 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2379 "%s: unable to retrieve previous and next element from list element: %d.",
2380 function,
2381 element_index );
2382
2383 result = -1;
2384 }
2385 }
2386 if( result == 1 )
2387 {
2388 if( libcdata_internal_list_insert_element_before_element(
2389 internal_list,
2390 safe_existing_element,
2391 element_to_insert,
2392 error ) != 1 )
2393 {
2394 libcerror_error_set(
2395 error,
2396 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2397 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2398 "%s: unable to insert element before list element.",
2399 function );
2400
2401 result = -1;
2402 }
2403 }
2404 }
2405 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2406 if( libcthreads_read_write_lock_release_for_write(
2407 internal_list->read_write_lock,
2408 error ) != 1 )
2409 {
2410 libcerror_error_set(
2411 error,
2412 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2413 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2414 "%s: unable to release read/write lock for writing.",
2415 function );
2416
2417 goto on_error;
2418 }
2419 #endif
2420 return( result );
2421
2422 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2423
2424 on_error:
2425 if( result == 1 )
2426 {
2427 libcdata_list_element_set_elements(
2428 element_to_insert,
2429 NULL,
2430 NULL,
2431 NULL );
2432
2433 if( safe_existing_element != NULL )
2434 {
2435 libcdata_list_element_set_elements(
2436 safe_existing_element,
2437 previous_element,
2438 next_element,
2439 NULL );
2440 }
2441 else if( backup_last_element != NULL )
2442 {
2443 libcdata_list_element_set_next_element(
2444 backup_last_element,
2445 NULL,
2446 NULL );
2447 }
2448 internal_list->first_element = backup_first_element;
2449 internal_list->last_element = backup_last_element;
2450
2451 internal_list->number_of_elements -= 1;
2452 }
2453 return( -1 );
2454
2455 #endif /* defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA ) */
2456 }
2457
2458 /* Inserts a value to the list
2459 *
2460 * Creates a new list element
2461 *
2462 * Uses the value_compare_function to determine the order of the entries
2463 * The value_compare_function should return LIBCDATA_COMPARE_LESS,
2464 * LIBCDATA_COMPARE_EQUAL, LIBCDATA_COMPARE_GREATER if successful or -1 on error
2465 *
2466 * Duplicate entries are allowed by default and inserted after the last duplicate value.
2467 * Only allowing unique entries can be enforced by setting the flag LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES
2468 *
2469 * Returns 1 if successful, 0 if the list element already exists or -1 on error
2470 */
2471 int libcdata_list_insert_value(
2472 libcdata_list_t *list,
2473 intptr_t *value,
2474 int (*value_compare_function)(
2475 intptr_t *first_value,
2476 intptr_t *second_value,
2477 libcerror_error_t **error ),
2478 uint8_t insert_flags,
2479 libcerror_error_t **error )
2480 {
2481 intptr_t *existing_value = NULL;
2482 static char *function = "libcdata_list_insert_value";
2483 int result = 0;
2484
2485 result = libcdata_list_insert_value_with_existing(
2486 list,
2487 value,
2488 value_compare_function,
2489 insert_flags,
2490 &existing_value,
2491 error );
2492
2493 if( result == -1 )
2494 {
2495 libcerror_error_set(
2496 error,
2497 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2498 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2499 "%s: unable to insert value into list.",
2500 function );
2501
2502 return( -1 );
2503 }
2504 return( result );
2505 }
2506
2507 /* Inserts a value to the list
2508 *
2509 * Creates a new list element
2510 *
2511 * Uses the value_compare_function to determine the order of the entries
2512 * The value_compare_function should return LIBCDATA_COMPARE_LESS,
2513 * LIBCDATA_COMPARE_EQUAL, LIBCDATA_COMPARE_GREATER if successful or -1 on error
2514 *
2515 * Duplicate entries are allowed by default and inserted after the last duplicate value.
2516 * Only allowing unique entries can be enforced by setting the flag LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES
2517 *
2518 * Returns 1 if successful, 0 if the list element already exists or -1 on error
2519 */
2520 int libcdata_list_insert_value_with_existing(
2521 libcdata_list_t *list,
2522 intptr_t *value,
2523 int (*value_compare_function)(
2524 intptr_t *first_value,
2525 intptr_t *second_value,
2526 libcerror_error_t **error ),
2527 uint8_t insert_flags,
2528 intptr_t **existing_value,
2529 libcerror_error_t **error )
2530 {
2531 libcdata_list_element_t *element = NULL;
2532 libcdata_list_element_t *existing_element = NULL;
2533 static char *function = "libcdata_list_insert_value_with_existing";
2534 int result = 1;
2535
2536 if( existing_value == NULL )
2537 {
2538 libcerror_error_set(
2539 error,
2540 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2541 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2542 "%s: invalid existing value.",
2543 function );
2544
2545 return( -1 );
2546 }
2547 *existing_value = NULL;
2548
2549 if( libcdata_list_element_initialize(
2550 &element,
2551 error ) != 1 )
2552 {
2553 libcerror_error_set(
2554 error,
2555 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2556 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2557 "%s: unable to create list element.",
2558 function );
2559
2560 return( -1 );
2561 }
2562 if( libcdata_list_element_set_value(
2563 element,
2564 value,
2565 error ) != 1 )
2566 {
2567 libcerror_error_set(
2568 error,
2569 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2570 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2571 "%s: unable to set value of list element.",
2572 function );
2573
2574 goto on_error;
2575 }
2576 result = libcdata_list_insert_element_with_existing(
2577 list,
2578 element,
2579 value_compare_function,
2580 insert_flags,
2581 &existing_element,
2582 error );
2583
2584 if( result == -1 )
2585 {
2586 libcerror_error_set(
2587 error,
2588 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2589 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2590 "%s: unable to insert element to list.",
2591 function );
2592
2593 goto on_error;
2594 }
2595 else if( result == 0 )
2596 {
2597 if( libcdata_list_element_free(
2598 &element,
2599 NULL,
2600 error ) != 1 )
2601 {
2602 libcerror_error_set(
2603 error,
2604 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2605 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
2606 "%s: unable to free list element.",
2607 function );
2608
2609 goto on_error;
2610 }
2611 if( libcdata_list_element_get_value(
2612 existing_element,
2613 existing_value,
2614 error ) != 1 )
2615 {
2616 libcerror_error_set(
2617 error,
2618 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2619 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2620 "%s: unable to retrieve value from existing list element.",
2621 function );
2622
2623 goto on_error;
2624 }
2625 }
2626 return( result );
2627
2628 on_error:
2629 if( element != NULL )
2630 {
2631 libcdata_list_element_free(
2632 &element,
2633 NULL,
2634 NULL );
2635 }
2636 return( -1 );
2637 }
2638
2639 /* Removes a list element from the list
2640 * Returns 1 if successful or -1 on error
2641 */
2642 int libcdata_list_remove_element(
2643 libcdata_list_t *list,
2644 libcdata_list_element_t *element_to_remove,
2645 libcerror_error_t **error )
2646 {
2647 libcdata_internal_list_t *internal_list = NULL;
2648 libcdata_list_element_t *next_element = NULL;
2649 libcdata_list_element_t *previous_element = NULL;
2650 static char *function = "libcdata_list_remove_element";
2651 int result = 1;
2652
2653 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2654 libcdata_list_element_t *backup_first_element = NULL;
2655 libcdata_list_element_t *backup_last_element = NULL;
2656 #endif
2657
2658 if( list == NULL )
2659 {
2660 libcerror_error_set(
2661 error,
2662 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2663 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2664 "%s: invalid list.",
2665 function );
2666
2667 return( -1 );
2668 }
2669 internal_list = (libcdata_internal_list_t *) list;
2670
2671 if( internal_list->number_of_elements == 0 )
2672 {
2673 if( internal_list->first_element != NULL )
2674 {
2675 libcerror_error_set(
2676 error,
2677 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2678 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2679 "%s: corruption detected - first element already set.",
2680 function );
2681
2682 return( -1 );
2683 }
2684 if( internal_list->last_element != NULL )
2685 {
2686 libcerror_error_set(
2687 error,
2688 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2689 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2690 "%s: corruption detected - last element already set.",
2691 function );
2692
2693 return( -1 );
2694 }
2695 }
2696 else
2697 {
2698 if( internal_list->first_element == NULL )
2699 {
2700 libcerror_error_set(
2701 error,
2702 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2703 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2704 "%s: corruption detected - missing first element.",
2705 function );
2706
2707 return( -1 );
2708 }
2709 if( internal_list->last_element == NULL )
2710 {
2711 libcerror_error_set(
2712 error,
2713 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2714 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2715 "%s: corruption detected - missing last element.",
2716 function );
2717
2718 return( -1 );
2719 }
2720 }
2721 if( element_to_remove == NULL )
2722 {
2723 libcerror_error_set(
2724 error,
2725 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2726 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2727 "%s: invalid element to remove.",
2728 function );
2729
2730 return( -1 );
2731 }
2732 if( libcdata_list_element_get_elements(
2733 element_to_remove,
2734 &previous_element,
2735 &next_element,
2736 error ) != 1 )
2737 {
2738 libcerror_error_set(
2739 error,
2740 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2741 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2742 "%s: unable to retrieve previous and next element from element to remove.",
2743 function );
2744
2745 return( -1 );
2746 }
2747 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2748 if( libcthreads_read_write_lock_grab_for_write(
2749 internal_list->read_write_lock,
2750 error ) != 1 )
2751 {
2752 libcerror_error_set(
2753 error,
2754 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2755 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2756 "%s: unable to grab read/write lock for writing.",
2757 function );
2758
2759 return( -1 );
2760 }
2761 backup_first_element = internal_list->first_element;
2762 backup_last_element = internal_list->last_element;
2763
2764 #endif
2765 result = libcdata_list_element_set_elements(
2766 element_to_remove,
2767 NULL,
2768 NULL,
2769 error );
2770
2771 if( result != 1 )
2772 {
2773 libcerror_error_set(
2774 error,
2775 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2776 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2777 "%s: unable to set previous and next element of element to remove.",
2778 function );
2779
2780 result = -1;
2781 }
2782 if( result == 1 )
2783 {
2784 if( next_element != NULL )
2785 {
2786 result = libcdata_list_element_set_previous_element(
2787 next_element,
2788 previous_element,
2789 error );
2790
2791 if( result != 1 )
2792 {
2793 libcerror_error_set(
2794 error,
2795 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2796 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2797 "%s: unable to set previous element of next element.",
2798 function );
2799
2800 libcdata_list_element_set_elements(
2801 element_to_remove,
2802 previous_element,
2803 next_element,
2804 NULL );
2805
2806 result = -1;
2807 }
2808 }
2809 }
2810 if( result == 1 )
2811 {
2812 if( previous_element != NULL )
2813 {
2814 result = libcdata_list_element_set_next_element(
2815 previous_element,
2816 next_element,
2817 error );
2818
2819 if( result != 1 )
2820 {
2821 libcerror_error_set(
2822 error,
2823 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2824 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2825 "%s: unable to set next element of previous element.",
2826 function );
2827
2828 if( next_element != NULL )
2829 {
2830 libcdata_list_element_set_previous_element(
2831 next_element,
2832 element_to_remove,
2833 NULL );
2834 }
2835 libcdata_list_element_set_elements(
2836 element_to_remove,
2837 previous_element,
2838 next_element,
2839 NULL );
2840
2841 result = -1;
2842 }
2843 }
2844 }
2845 if( result == 1 )
2846 {
2847 if( element_to_remove == internal_list->first_element )
2848 {
2849 internal_list->first_element = next_element;
2850 }
2851 if( element_to_remove == internal_list->last_element )
2852 {
2853 internal_list->last_element = previous_element;
2854 }
2855 internal_list->number_of_elements -= 1;
2856 }
2857 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2858 if( libcthreads_read_write_lock_release_for_write(
2859 internal_list->read_write_lock,
2860 error ) != 1 )
2861 {
2862 libcerror_error_set(
2863 error,
2864 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2865 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2866 "%s: unable to release read/write lock for writing.",
2867 function );
2868
2869 goto on_error;
2870 }
2871 #endif
2872 return( result );
2873
2874 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2875 on_error:
2876 if( result == 1 )
2877 {
2878 libcdata_list_element_set_elements(
2879 element_to_remove,
2880 previous_element,
2881 next_element,
2882 NULL );
2883
2884 if( next_element != NULL )
2885 {
2886 libcdata_list_element_set_previous_element(
2887 next_element,
2888 element_to_remove,
2889 NULL );
2890 }
2891 if( previous_element != NULL )
2892 {
2893 libcdata_list_element_set_next_element(
2894 previous_element,
2895 element_to_remove,
2896 NULL );
2897 }
2898 internal_list->first_element = backup_first_element;
2899 internal_list->last_element = backup_last_element;
2900
2901 internal_list->number_of_elements += 1;
2902 }
2903 return( -1 );
2904 #endif
2905 }
2906