Debian Codegraph @

libbfio/libbfio_file_io_handle.c

raw symbols SCIP
1 /*
2 * File IO 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 <narrow_string.h>
25 #include <system_string.h>
26 #include <types.h>
27 #include <wide_string.h>
28
29 #include "libbfio_definitions.h"
30 #include "libbfio_file_io_handle.h"
31 #include "libbfio_libcerror.h"
32 #include "libbfio_libcfile.h"
33 #include "libbfio_system_string.h"
34
35 /* Creates a file IO handle
36 * Make sure the value file_io_handle is referencing, is set to NULL
37 * Returns 1 if successful or -1 on error
38 */
39 int libbfio_file_io_handle_initialize(
40 libbfio_file_io_handle_t **file_io_handle,
41 libcerror_error_t **error )
42 {
43 static char *function = "libbfio_file_io_handle_initialize";
44
45 if( file_io_handle == NULL )
46 {
47 libcerror_error_set(
48 error,
49 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
50 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
51 "%s: invalid file IO handle.",
52 function );
53
54 return( -1 );
55 }
56 if( *file_io_handle != NULL )
57 {
58 libcerror_error_set(
59 error,
60 LIBCERROR_ERROR_DOMAIN_RUNTIME,
61 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
62 "%s: invalid file IO handle value already set.",
63 function );
64
65 return( -1 );
66 }
67 *file_io_handle = memory_allocate_structure(
68 libbfio_file_io_handle_t );
69
70 if( *file_io_handle == NULL )
71 {
72 libcerror_error_set(
73 error,
74 LIBCERROR_ERROR_DOMAIN_MEMORY,
75 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
76 "%s: unable to create file IO handle.",
77 function );
78
79 goto on_error;
80 }
81 if( memory_set(
82 *file_io_handle,
83 0,
84 sizeof( libbfio_file_io_handle_t ) ) == NULL )
85 {
86 libcerror_error_set(
87 error,
88 LIBCERROR_ERROR_DOMAIN_MEMORY,
89 LIBCERROR_MEMORY_ERROR_SET_FAILED,
90 "%s: unable to clear file IO handle.",
91 function );
92
93 goto on_error;
94 }
95 if( libcfile_file_initialize(
96 &( ( *file_io_handle )->file ),
97 error ) != 1 )
98 {
99 libcerror_error_set(
100 error,
101 LIBCERROR_ERROR_DOMAIN_RUNTIME,
102 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
103 "%s: unable to create file.",
104 function );
105
106 goto on_error;
107 }
108 return( 1 );
109
110 on_error:
111 if( *file_io_handle != NULL )
112 {
113 memory_free(
114 *file_io_handle );
115
116 *file_io_handle = NULL;
117 }
118 return( -1 );
119 }
120
121 /* Frees a file IO handle
122 * Returns 1 if succesful or -1 on error
123 */
124 int libbfio_file_io_handle_free(
125 libbfio_file_io_handle_t **file_io_handle,
126 libcerror_error_t **error )
127 {
128 static char *function = "libbfio_file_io_handle_free";
129 int result = 1;
130
131 if( file_io_handle == NULL )
132 {
133 libcerror_error_set(
134 error,
135 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
136 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
137 "%s: invalid file IO handle.",
138 function );
139
140 return( -1 );
141 }
142 if( *file_io_handle != NULL )
143 {
144 if( ( *file_io_handle )->name != NULL )
145 {
146 memory_free(
147 ( *file_io_handle )->name );
148 }
149 if( libcfile_file_free(
150 &( ( *file_io_handle )->file ),
151 error ) != 1 )
152 {
153 libcerror_error_set(
154 error,
155 LIBCERROR_ERROR_DOMAIN_RUNTIME,
156 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
157 "%s: unable to free file.",
158 function );
159
160 result = -1;
161 }
162 memory_free(
163 *file_io_handle );
164
165 *file_io_handle = NULL;
166 }
167 return( result );
168 }
169
170 /* Clones (duplicates) the file IO handle and its attributes
171 * Returns 1 if succesful or -1 on error
172 */
173 int libbfio_file_io_handle_clone(
174 libbfio_file_io_handle_t **destination_file_io_handle,
175 libbfio_file_io_handle_t *source_file_io_handle,
176 libcerror_error_t **error )
177 {
178 static char *function = "libbfio_file_io_handle_clone";
179
180 if( destination_file_io_handle == NULL )
181 {
182 libcerror_error_set(
183 error,
184 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
185 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
186 "%s: invalid destination file IO handle.",
187 function );
188
189 return( -1 );
190 }
191 if( *destination_file_io_handle != NULL )
192 {
193 libcerror_error_set(
194 error,
195 LIBCERROR_ERROR_DOMAIN_RUNTIME,
196 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
197 "%s: destination file IO handle already set.",
198 function );
199
200 return( -1 );
201 }
202 if( source_file_io_handle == NULL )
203 {
204 *destination_file_io_handle = NULL;
205
206 return( 1 );
207 }
208 if( libbfio_file_io_handle_initialize(
209 destination_file_io_handle,
210 error ) != 1 )
211 {
212 libcerror_error_set(
213 error,
214 LIBCERROR_ERROR_DOMAIN_RUNTIME,
215 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
216 "%s: unable to create file IO handle.",
217 function );
218
219 goto on_error;
220 }
221 if( *destination_file_io_handle == NULL )
222 {
223 libcerror_error_set(
224 error,
225 LIBCERROR_ERROR_DOMAIN_RUNTIME,
226 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
227 "%s: missing destination file IO handle.",
228 function );
229
230 goto on_error;
231 }
232 if( source_file_io_handle->name_size > 0 )
233 {
234 if( source_file_io_handle->name == NULL )
235 {
236 libcerror_error_set(
237 error,
238 LIBCERROR_ERROR_DOMAIN_RUNTIME,
239 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
240 "%s: invalid source file IO handle - missing name.",
241 function );
242
243 goto on_error;
244 }
245 if( source_file_io_handle->name_size > ( (size_t) SSIZE_MAX / sizeof( system_character_t ) ) )
246 {
247 libcerror_error_set(
248 error,
249 LIBCERROR_ERROR_DOMAIN_RUNTIME,
250 LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
251 "%s: invalid source file IO handle - name size value exceeds maximum.",
252 function );
253
254 goto on_error;
255 }
256 ( *destination_file_io_handle )->name = system_string_allocate(
257 source_file_io_handle->name_size );
258
259 if( ( *destination_file_io_handle )->name == NULL )
260 {
261 libcerror_error_set(
262 error,
263 LIBCERROR_ERROR_DOMAIN_MEMORY,
264 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
265 "%s: unable to create name.",
266 function );
267
268 goto on_error;
269 }
270 if( source_file_io_handle->name_size > 1 )
271 {
272 if( system_string_copy(
273 ( *destination_file_io_handle )->name,
274 source_file_io_handle->name,
275 source_file_io_handle->name_size ) == NULL )
276 {
277 libcerror_error_set(
278 error,
279 LIBCERROR_ERROR_DOMAIN_MEMORY,
280 LIBCERROR_MEMORY_ERROR_COPY_FAILED,
281 "%s: unable to copy name.",
282 function );
283
284 goto on_error;
285 }
286 }
287 ( *destination_file_io_handle )->name[ source_file_io_handle->name_size - 1 ] = 0;
288
289 ( *destination_file_io_handle )->name_size = source_file_io_handle->name_size;
290 }
291 return( 1 );
292
293 on_error:
294 if( *destination_file_io_handle != NULL )
295 {
296 libbfio_file_io_handle_free(
297 destination_file_io_handle,
298 NULL );
299 }
300 return( -1 );
301 }
302
303 /* Retrieves the name size of the file IO handle
304 * The name size includes the end of string character
305 * Returns 1 if succesful or -1 on error
306 */
307 int libbfio_file_io_handle_get_name_size(
308 libbfio_file_io_handle_t *file_io_handle,
309 size_t *name_size,
310 libcerror_error_t **error )
311 {
312 static char *function = "libbfio_file_io_handle_get_name_size";
313
314 if( file_io_handle == NULL )
315 {
316 libcerror_error_set(
317 error,
318 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
319 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
320 "%s: invalid file IO handle.",
321 function );
322
323 return( -1 );
324 }
325 if( libbfio_system_string_size_to_narrow_string(
326 file_io_handle->name,
327 file_io_handle->name_size,
328 name_size,
329 error ) != 1 )
330 {
331 libcerror_error_set(
332 error,
333 LIBCERROR_ERROR_DOMAIN_CONVERSION,
334 LIBCERROR_CONVERSION_ERROR_GENERIC,
335 "%s: unable to determine name size.",
336 function );
337
338 return( -1 );
339 }
340 return( 1 );
341 }
342
343 /* Retrieves the name of the file IO handle
344 * The name size should include the end of string character
345 * Returns 1 if succesful or -1 on error
346 */
347 int libbfio_file_io_handle_get_name(
348 libbfio_file_io_handle_t *file_io_handle,
349 char *name,
350 size_t name_size,
351 libcerror_error_t **error )
352 {
353 static char *function = "libbfio_file_io_handle_get_name";
354
355 if( file_io_handle == NULL )
356 {
357 libcerror_error_set(
358 error,
359 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
360 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
361 "%s: invalid file IO handle.",
362 function );
363
364 return( -1 );
365 }
366 if( libbfio_system_string_copy_to_narrow_string(
367 file_io_handle->name,
368 file_io_handle->name_size,
369 name,
370 name_size,
371 error ) != 1 )
372 {
373 libcerror_error_set(
374 error,
375 LIBCERROR_ERROR_DOMAIN_CONVERSION,
376 LIBCERROR_CONVERSION_ERROR_GENERIC,
377 "%s: unable to set name.",
378 function );
379
380 return( -1 );
381 }
382 return( 1 );
383 }
384
385 /* Sets the name for the file IO handle
386 * Returns 1 if succesful or -1 on error
387 */
388 int libbfio_file_io_handle_set_name(
389 libbfio_file_io_handle_t *file_io_handle,
390 const char *name,
391 size_t name_length,
392 libcerror_error_t **error )
393 {
394 static char *function = "libbfio_file_io_handle_set_name";
395 int result = 0;
396
397 if( file_io_handle == NULL )
398 {
399 libcerror_error_set(
400 error,
401 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
402 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
403 "%s: invalid file IO handle.",
404 function );
405
406 return( -1 );
407 }
408 if( name == NULL )
409 {
410 libcerror_error_set(
411 error,
412 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
413 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
414 "%s: invalid name.",
415 function );
416
417 return( -1 );
418 }
419 if( ( name_length == 0 )
420 || ( name_length > ( (size_t) SSIZE_MAX - 1 ) ) )
421 {
422 libcerror_error_set(
423 error,
424 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
425 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
426 "%s: invalid name length value out of bounds.",
427 function );
428
429 return( -1 );
430 }
431 if( file_io_handle->name != NULL )
432 {
433 result = libcfile_file_is_open(
434 file_io_handle->file,
435 error );
436
437 if( result == -1 )
438 {
439 libcerror_error_set(
440 error,
441 LIBCERROR_ERROR_DOMAIN_RUNTIME,
442 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
443 "%s: unable to determine if file is open.",
444 function );
445
446 return( -1 );
447 }
448 if( result != 0 )
449 {
450 libcerror_error_set(
451 error,
452 LIBCERROR_ERROR_DOMAIN_RUNTIME,
453 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
454 "%s: unable to set name when file is open.",
455 function );
456
457 return( -1 );
458 }
459 memory_free(
460 file_io_handle->name );
461
462 file_io_handle->name = NULL;
463 file_io_handle->name_size = 0;
464 }
465 if( libbfio_system_string_size_from_narrow_string(
466 name,
467 name_length + 1,
468 &( file_io_handle->name_size ),
469 error ) != 1 )
470 {
471 libcerror_error_set(
472 error,
473 LIBCERROR_ERROR_DOMAIN_CONVERSION,
474 LIBCERROR_CONVERSION_ERROR_GENERIC,
475 "%s: unable to determine name size.",
476 function );
477
478 goto on_error;
479 }
480 if( ( file_io_handle->name_size == 0 )
481 || ( file_io_handle->name_size > ( (size_t) SSIZE_MAX / sizeof( system_character_t ) ) ) )
482 {
483 libcerror_error_set(
484 error,
485 LIBCERROR_ERROR_DOMAIN_RUNTIME,
486 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
487 "%s: invalid file IO handle - name size value out of bounds.",
488 function );
489
490 goto on_error;
491 }
492 file_io_handle->name = system_string_allocate(
493 file_io_handle->name_size );
494
495 if( file_io_handle->name == NULL )
496 {
497 libcerror_error_set(
498 error,
499 LIBCERROR_ERROR_DOMAIN_MEMORY,
500 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
501 "%s: unable to create name.",
502 function );
503
504 goto on_error;
505 }
506 if( libbfio_system_string_copy_from_narrow_string(
507 file_io_handle->name,
508 file_io_handle->name_size,
509 name,
510 name_length + 1,
511 error ) != 1 )
512 {
513 libcerror_error_set(
514 error,
515 LIBCERROR_ERROR_DOMAIN_CONVERSION,
516 LIBCERROR_CONVERSION_ERROR_GENERIC,
517 "%s: unable to set name.",
518 function );
519
520 goto on_error;
521 }
522 return( 1 );
523
524 on_error:
525 if( file_io_handle->name != NULL )
526 {
527 memory_free(
528 file_io_handle->name );
529
530 file_io_handle->name = NULL;
531 }
532 file_io_handle->name_size = 0;
533
534 return( -1 );
535 }
536
537 #if defined( HAVE_WIDE_CHARACTER_TYPE )
538
539 /* Retrieves the name size of the file IO handle
540 * The name size includes the end of string character
541 * Returns 1 if succesful or -1 on error
542 */
543 int libbfio_file_io_handle_get_name_size_wide(
544 libbfio_file_io_handle_t *file_io_handle,
545 size_t *name_size,
546 libcerror_error_t **error )
547 {
548 static char *function = "libbfio_file_io_handle_get_name_size_wide";
549
550 if( file_io_handle == NULL )
551 {
552 libcerror_error_set(
553 error,
554 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
555 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
556 "%s: invalid file IO handle.",
557 function );
558
559 return( -1 );
560 }
561 if( libbfio_system_string_size_to_wide_string(
562 file_io_handle->name,
563 file_io_handle->name_size,
564 name_size,
565 error ) != 1 )
566 {
567 libcerror_error_set(
568 error,
569 LIBCERROR_ERROR_DOMAIN_CONVERSION,
570 LIBCERROR_CONVERSION_ERROR_GENERIC,
571 "%s: unable to determine name size.",
572 function );
573
574 return( -1 );
575 }
576 return( 1 );
577 }
578
579 /* Retrieves the name of the file IO handle
580 * The name size should include the end of string character
581 * Returns 1 if succesful or -1 on error
582 */
583 int libbfio_file_io_handle_get_name_wide(
584 libbfio_file_io_handle_t *file_io_handle,
585 wchar_t *name,
586 size_t name_size,
587 libcerror_error_t **error )
588 {
589 static char *function = "libbfio_file_io_handle_get_name_wide";
590
591 if( file_io_handle == NULL )
592 {
593 libcerror_error_set(
594 error,
595 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
596 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
597 "%s: invalid file IO handle.",
598 function );
599
600 return( -1 );
601 }
602 if( libbfio_system_string_copy_to_wide_string(
603 file_io_handle->name,
604 file_io_handle->name_size,
605 name,
606 name_size,
607 error ) != 1 )
608 {
609 libcerror_error_set(
610 error,
611 LIBCERROR_ERROR_DOMAIN_CONVERSION,
612 LIBCERROR_CONVERSION_ERROR_GENERIC,
613 "%s: unable to set name.",
614 function );
615
616 return( -1 );
617 }
618 return( 1 );
619 }
620
621 /* Sets the name for the file IO handle
622 * Returns 1 if succesful or -1 on error
623 */
624 int libbfio_file_io_handle_set_name_wide(
625 libbfio_file_io_handle_t *file_io_handle,
626 const wchar_t *name,
627 size_t name_length,
628 libcerror_error_t **error )
629 {
630 static char *function = "libbfio_file_io_handle_set_name_wide";
631 int result = 0;
632
633 if( file_io_handle == NULL )
634 {
635 libcerror_error_set(
636 error,
637 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
638 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
639 "%s: invalid file IO handle.",
640 function );
641
642 return( -1 );
643 }
644 if( name == NULL )
645 {
646 libcerror_error_set(
647 error,
648 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
649 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
650 "%s: invalid name.",
651 function );
652
653 return( -1 );
654 }
655 if( ( name_length == 0 )
656 || ( name_length > ( (size_t) SSIZE_MAX - 1 ) ) )
657 {
658 libcerror_error_set(
659 error,
660 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
661 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
662 "%s: invalid name length value out of bounds.",
663 function );
664
665 return( -1 );
666 }
667 if( file_io_handle->name != NULL )
668 {
669 result = libcfile_file_is_open(
670 file_io_handle->file,
671 error );
672
673 if( result == -1 )
674 {
675 libcerror_error_set(
676 error,
677 LIBCERROR_ERROR_DOMAIN_RUNTIME,
678 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
679 "%s: unable to determine if file is open.",
680 function );
681
682 return( -1 );
683 }
684 if( result != 0 )
685 {
686 libcerror_error_set(
687 error,
688 LIBCERROR_ERROR_DOMAIN_RUNTIME,
689 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
690 "%s: unable to set name when file is open.",
691 function );
692
693 return( -1 );
694 }
695 memory_free(
696 file_io_handle->name );
697
698 file_io_handle->name = NULL;
699 file_io_handle->name_size = 0;
700 }
701 if( libbfio_system_string_size_from_wide_string(
702 name,
703 name_length + 1,
704 &( file_io_handle->name_size ),
705 error ) != 1 )
706 {
707 libcerror_error_set(
708 error,
709 LIBCERROR_ERROR_DOMAIN_CONVERSION,
710 LIBCERROR_CONVERSION_ERROR_GENERIC,
711 "%s: unable to determine name size.",
712 function );
713
714 goto on_error;
715 }
716 if( ( file_io_handle->name_size == 0 )
717 || ( file_io_handle->name_size > ( (size_t) SSIZE_MAX / sizeof( system_character_t ) ) ) )
718 {
719 libcerror_error_set(
720 error,
721 LIBCERROR_ERROR_DOMAIN_RUNTIME,
722 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
723 "%s: invalid file IO handle - name size value out of bounds.",
724 function );
725
726 goto on_error;
727 }
728 file_io_handle->name = system_string_allocate(
729 file_io_handle->name_size );
730
731 if( file_io_handle->name == NULL )
732 {
733 libcerror_error_set(
734 error,
735 LIBCERROR_ERROR_DOMAIN_MEMORY,
736 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
737 "%s: unable to create name.",
738 function );
739
740 goto on_error;
741 }
742 if( libbfio_system_string_copy_from_wide_string(
743 file_io_handle->name,
744 file_io_handle->name_size,
745 name,
746 name_length + 1,
747 error ) != 1 )
748 {
749 libcerror_error_set(
750 error,
751 LIBCERROR_ERROR_DOMAIN_CONVERSION,
752 LIBCERROR_CONVERSION_ERROR_GENERIC,
753 "%s: unable to set name.",
754 function );
755
756 goto on_error;
757 }
758 return( 1 );
759
760 on_error:
761 if( file_io_handle->name != NULL )
762 {
763 memory_free(
764 file_io_handle->name );
765
766 file_io_handle->name = NULL;
767 }
768 file_io_handle->name_size = 0;
769
770 return( -1 );
771 }
772
773 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
774
775 /* Opens the file handle
776 * Returns 1 if successful or -1 on error
777 */
778 int libbfio_file_io_handle_open(
779 libbfio_file_io_handle_t *file_io_handle,
780 int access_flags,
781 libcerror_error_t **error )
782 {
783 static char *function = "libbfio_file_io_handle_open";
784 int result = 0;
785
786 if( file_io_handle == NULL )
787 {
788 libcerror_error_set(
789 error,
790 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
791 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
792 "%s: invalid file IO handle.",
793 function );
794
795 return( -1 );
796 }
797 if( file_io_handle->name == NULL )
798 {
799 libcerror_error_set(
800 error,
801 LIBCERROR_ERROR_DOMAIN_RUNTIME,
802 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
803 "%s: invalid file IO handle - missing name.",
804 function );
805
806 return( -1 );
807 }
808 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
809 result = libcfile_file_open_wide(
810 file_io_handle->file,
811 file_io_handle->name,
812 access_flags,
813 error );
814 #else
815 result = libcfile_file_open(
816 file_io_handle->file,
817 file_io_handle->name,
818 access_flags,
819 error );
820 #endif
821 if( result != 1 )
822 {
823 libcerror_error_set(
824 error,
825 LIBCERROR_ERROR_DOMAIN_IO,
826 LIBCERROR_IO_ERROR_OPEN_FAILED,
827 "%s: unable to open file: %" PRIs_SYSTEM ".",
828 function,
829 file_io_handle->name );
830
831 return( -1 );
832 }
833 file_io_handle->access_flags = access_flags;
834
835 return( 1 );
836 }
837
838 /* Closes the file handle
839 * Returns 0 if successful or -1 on error
840 */
841 int libbfio_file_io_handle_close(
842 libbfio_file_io_handle_t *file_io_handle,
843 libcerror_error_t **error )
844 {
845 static char *function = "libbfio_file_close";
846
847 if( file_io_handle == NULL )
848 {
849 libcerror_error_set(
850 error,
851 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
852 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
853 "%s: invalid file IO handle.",
854 function );
855
856 return( -1 );
857 }
858 if( file_io_handle->name == NULL )
859 {
860 libcerror_error_set(
861 error,
862 LIBCERROR_ERROR_DOMAIN_RUNTIME,
863 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
864 "%s: invalid file IO handle - missing name.",
865 function );
866
867 return( -1 );
868 }
869 if( libcfile_file_close(
870 file_io_handle->file,
871 error ) != 0 )
872 {
873 libcerror_error_set(
874 error,
875 LIBCERROR_ERROR_DOMAIN_IO,
876 LIBCERROR_IO_ERROR_OPEN_FAILED,
877 "%s: unable to close file: %" PRIs_SYSTEM ".",
878 function,
879 file_io_handle->name );
880
881 return( -1 );
882 }
883 file_io_handle->access_flags = 0;
884
885 return( 0 );
886 }
887
888 /* Reads a buffer from the file handle
889 * Returns the number of bytes read if successful, or -1 on error
890 */
891 ssize_t libbfio_file_io_handle_read_buffer(
892 libbfio_file_io_handle_t *file_io_handle,
893 uint8_t *buffer,
894 size_t size,
895 libcerror_error_t **error )
896 {
897 static char *function = "libbfio_file_io_handle_read_buffer";
898 ssize_t read_count = 0;
899
900 if( file_io_handle == NULL )
901 {
902 libcerror_error_set(
903 error,
904 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
905 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
906 "%s: invalid file IO handle.",
907 function );
908
909 return( -1 );
910 }
911 if( file_io_handle->name == NULL )
912 {
913 libcerror_error_set(
914 error,
915 LIBCERROR_ERROR_DOMAIN_RUNTIME,
916 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
917 "%s: invalid file IO handle - missing name.",
918 function );
919
920 return( -1 );
921 }
922 read_count = libcfile_file_read_buffer(
923 file_io_handle->file,
924 buffer,
925 size,
926 error );
927
928 if( read_count < 0 )
929 {
930 libcerror_error_set(
931 error,
932 LIBCERROR_ERROR_DOMAIN_IO,
933 LIBCERROR_IO_ERROR_READ_FAILED,
934 "%s: unable to read from file: %" PRIs_SYSTEM ".",
935 function,
936 file_io_handle->name );
937
938 return( -1 );
939 }
940 return( read_count );
941 }
942
943 /* Writes a buffer to the file handle
944 * Returns the number of bytes written if successful, or -1 on error
945 */
946 ssize_t libbfio_file_io_handle_write_buffer(
947 libbfio_file_io_handle_t *file_io_handle,
948 const uint8_t *buffer,
949 size_t size,
950 libcerror_error_t **error )
951 {
952 static char *function = "libbfio_file_write_buffer";
953 ssize_t write_count = 0;
954
955 if( file_io_handle == NULL )
956 {
957 libcerror_error_set(
958 error,
959 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
960 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
961 "%s: invalid file IO handle.",
962 function );
963
964 return( -1 );
965 }
966 if( file_io_handle->name == NULL )
967 {
968 libcerror_error_set(
969 error,
970 LIBCERROR_ERROR_DOMAIN_RUNTIME,
971 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
972 "%s: invalid file IO handle - missing name.",
973 function );
974
975 return( -1 );
976 }
977 write_count = libcfile_file_write_buffer(
978 file_io_handle->file,
979 buffer,
980 size,
981 error );
982
983 if( write_count < 0 )
984 {
985 libcerror_error_set(
986 error,
987 LIBCERROR_ERROR_DOMAIN_IO,
988 LIBCERROR_IO_ERROR_WRITE_FAILED,
989 "%s: unable to write to file: %" PRIs_SYSTEM ".",
990 function,
991 file_io_handle->name );
992
993 return( -1 );
994 }
995 return( write_count );
996 }
997
998 /* Seeks a certain offset within the file handle
999 * Returns the offset if the seek is successful or -1 on error
1000 */
1001 off64_t libbfio_file_io_handle_seek_offset(
1002 libbfio_file_io_handle_t *file_io_handle,
1003 off64_t offset,
1004 int whence,
1005 libcerror_error_t **error )
1006 {
1007 static char *function = "libbfio_file_seek_offset";
1008 off64_t seek_offset = 0;
1009
1010 if( file_io_handle == NULL )
1011 {
1012 libcerror_error_set(
1013 error,
1014 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1015 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1016 "%s: invalid file IO handle.",
1017 function );
1018
1019 return( -1 );
1020 }
1021 if( file_io_handle->name == NULL )
1022 {
1023 libcerror_error_set(
1024 error,
1025 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1026 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1027 "%s: invalid file IO handle - missing name.",
1028 function );
1029
1030 return( -1 );
1031 }
1032 seek_offset = libcfile_file_seek_offset(
1033 file_io_handle->file,
1034 offset,
1035 whence,
1036 error );
1037
1038 if( seek_offset == -1 )
1039 {
1040 libcerror_error_set(
1041 error,
1042 LIBCERROR_ERROR_DOMAIN_IO,
1043 LIBCERROR_IO_ERROR_SEEK_FAILED,
1044 "%s: unable to seek offset: %" PRIi64 " in file: %" PRIs_SYSTEM ".",
1045 function,
1046 offset,
1047 file_io_handle->name );
1048
1049 return( -1 );
1050 }
1051 return( seek_offset );
1052 }
1053
1054 /* Function to determine if a file exists
1055 * Returns 1 if file exists, 0 if not or -1 on error
1056 */
1057 int libbfio_file_io_handle_exists(
1058 libbfio_file_io_handle_t *file_io_handle,
1059 libcerror_error_t **error )
1060 {
1061 static char *function = "libbfio_file_exists";
1062 int result = 0;
1063
1064 if( file_io_handle == NULL )
1065 {
1066 libcerror_error_set(
1067 error,
1068 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1069 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1070 "%s: invalid file IO handle.",
1071 function );
1072
1073 return( -1 );
1074 }
1075 if( file_io_handle->name == NULL )
1076 {
1077 libcerror_error_set(
1078 error,
1079 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1080 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1081 "%s: invalid file IO handle - missing name.",
1082 function );
1083
1084 return( -1 );
1085 }
1086 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1087 result = libcfile_file_exists_wide(
1088 file_io_handle->name,
1089 error );
1090 #else
1091 result = libcfile_file_exists(
1092 file_io_handle->name,
1093 error );
1094 #endif
1095 if( result == -1 )
1096 {
1097 libcerror_error_set(
1098 error,
1099 LIBCERROR_ERROR_DOMAIN_IO,
1100 LIBCERROR_IO_ERROR_GENERIC,
1101 "%s: unable to determine if file: %" PRIs_SYSTEM " exists.",
1102 function,
1103 file_io_handle->name );
1104
1105 return( -1 );
1106 }
1107 return( result );
1108 }
1109
1110 /* Check if the file is open
1111 * Returns 1 if open, 0 if not or -1 on error
1112 */
1113 int libbfio_file_io_handle_is_open(
1114 libbfio_file_io_handle_t *file_io_handle,
1115 libcerror_error_t **error )
1116 {
1117 static char *function = "libbfio_file_is_open";
1118 int result = 0;
1119
1120 if( file_io_handle == NULL )
1121 {
1122 libcerror_error_set(
1123 error,
1124 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1125 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1126 "%s: invalid file IO handle.",
1127 function );
1128
1129 return( -1 );
1130 }
1131 result = libcfile_file_is_open(
1132 file_io_handle->file,
1133 error );
1134
1135 if( result == -1 )
1136 {
1137 libcerror_error_set(
1138 error,
1139 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1140 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1141 "%s: unable to determine if file is open.",
1142 function );
1143
1144 return( -1 );
1145 }
1146 return( result );
1147 }
1148
1149 /* Retrieves the file size
1150 * Returns 1 if successful or -1 on error
1151 */
1152 int libbfio_file_io_handle_get_size(
1153 libbfio_file_io_handle_t *file_io_handle,
1154 size64_t *size,
1155 libcerror_error_t **error )
1156 {
1157 static char *function = "libbfio_file_get_size";
1158
1159 if( file_io_handle == NULL )
1160 {
1161 libcerror_error_set(
1162 error,
1163 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1164 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1165 "%s: invalid file IO handle.",
1166 function );
1167
1168 return( -1 );
1169 }
1170 if( file_io_handle->name == NULL )
1171 {
1172 libcerror_error_set(
1173 error,
1174 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1175 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1176 "%s: invalid file IO handle - missing name.",
1177 function );
1178
1179 return( -1 );
1180 }
1181 if( libcfile_file_get_size(
1182 file_io_handle->file,
1183 size,
1184 error ) != 1 )
1185 {
1186 libcerror_error_set(
1187 error,
1188 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1189 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1190 "%s: unable to retrieve size of file: %" PRIs_SYSTEM ".",
1191 function,
1192 file_io_handle->name );
1193
1194 return( -1 );
1195 }
1196 return( 1 );
1197 }
1198