Debian Codegraph @

libbfio/libbfio_handle.c

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