Coverage Report

Created: 2024-02-25 07:20

/src/libvsbsdl/libbfio/libbfio_file_io_handle.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * File IO handle functions
3
 *
4
 * Copyright (C) 2009-2022, Joachim Metz <joachim.metz@gmail.com>
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
0
{
43
0
  static char *function = "libbfio_file_io_handle_initialize";
44
45
0
  if( file_io_handle == NULL )
46
0
  {
47
0
    libcerror_error_set(
48
0
     error,
49
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
50
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
51
0
     "%s: invalid file IO handle.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
0
  if( *file_io_handle != NULL )
57
0
  {
58
0
    libcerror_error_set(
59
0
     error,
60
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
61
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
62
0
     "%s: invalid file IO handle value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
0
  *file_io_handle = memory_allocate_structure(
68
0
                     libbfio_file_io_handle_t );
69
70
0
  if( *file_io_handle == NULL )
71
0
  {
72
0
    libcerror_error_set(
73
0
     error,
74
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
75
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
76
0
     "%s: unable to create file IO handle.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
0
  if( memory_set(
82
0
       *file_io_handle,
83
0
       0,
84
0
       sizeof( libbfio_file_io_handle_t ) ) == NULL )
85
0
  {
86
0
    libcerror_error_set(
87
0
     error,
88
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
89
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
90
0
     "%s: unable to clear file IO handle.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
0
  if( libcfile_file_initialize(
96
0
       &( ( *file_io_handle )->file ),
97
0
       error ) != 1 )
98
0
  {
99
0
    libcerror_error_set(
100
0
     error,
101
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
102
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
103
0
     "%s: unable to create file.",
104
0
     function );
105
106
0
    goto on_error;
107
0
  }
108
0
  return( 1 );
109
110
0
on_error:
111
0
  if( *file_io_handle != NULL )
112
0
  {
113
0
    memory_free(
114
0
     *file_io_handle );
115
116
0
    *file_io_handle = NULL;
117
0
  }
118
0
  return( -1 );
119
0
}
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
0
{
128
0
  static char *function = "libbfio_file_io_handle_free";
129
0
  int result            = 1;
130
131
0
  if( file_io_handle == NULL )
132
0
  {
133
0
    libcerror_error_set(
134
0
     error,
135
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
136
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
137
0
     "%s: invalid file IO handle.",
138
0
     function );
139
140
0
    return( -1 );
141
0
  }
142
0
  if( *file_io_handle != NULL )
143
0
  {
144
0
    if( ( *file_io_handle )->name != NULL )
145
0
    {
146
0
      memory_free(
147
0
       ( *file_io_handle )->name );
148
0
    }
149
0
    if( libcfile_file_free(
150
0
         &( ( *file_io_handle )->file ),
151
0
         error ) != 1 )
152
0
    {
153
0
      libcerror_error_set(
154
0
       error,
155
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
156
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
157
0
       "%s: unable to free file.",
158
0
       function );
159
160
0
      result = -1;
161
0
    }
162
0
    memory_free(
163
0
     *file_io_handle );
164
165
0
    *file_io_handle = NULL;
166
0
  }
167
0
  return( result );
168
0
}
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
0
{
178
0
  static char *function = "libbfio_file_io_handle_clone";
179
180
0
  if( destination_file_io_handle == NULL )
181
0
  {
182
0
    libcerror_error_set(
183
0
     error,
184
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
185
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
186
0
     "%s: invalid destination file IO handle.",
187
0
     function );
188
189
0
    return( -1 );
190
0
  }
191
0
  if( *destination_file_io_handle != NULL )
192
0
  {
193
0
    libcerror_error_set(
194
0
     error,
195
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
196
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
197
0
     "%s: destination file IO handle already set.",
198
0
     function );
199
200
0
    return( -1 );
201
0
  }
202
0
  if( source_file_io_handle == NULL )
203
0
  {
204
0
    *destination_file_io_handle = NULL;
205
206
0
    return( 1 );
207
0
  }
208
0
  if( libbfio_file_io_handle_initialize(
209
0
       destination_file_io_handle,
210
0
       error ) != 1 )
211
0
  {
212
0
    libcerror_error_set(
213
0
     error,
214
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
215
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
216
0
     "%s: unable to create file IO handle.",
217
0
     function );
218
219
0
    goto on_error;
220
0
  }
221
0
  if( *destination_file_io_handle == NULL )
222
0
  {
223
0
    libcerror_error_set(
224
0
     error,
225
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
226
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
227
0
     "%s: missing destination file IO handle.",
228
0
     function );
229
230
0
    goto on_error;
231
0
  }
232
0
  if( source_file_io_handle->name_size > 0 )
233
0
  {
234
0
    if( source_file_io_handle->name == NULL )
235
0
    {
236
0
      libcerror_error_set(
237
0
       error,
238
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
239
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
240
0
       "%s: invalid source file IO handle - missing name.",
241
0
       function );
242
243
0
      goto on_error;
244
0
    }
245
0
    if( source_file_io_handle->name_size > ( (size_t) SSIZE_MAX / sizeof( system_character_t ) ) )
246
0
    {
247
0
      libcerror_error_set(
248
0
       error,
249
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
250
0
       LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
251
0
       "%s: invalid source file IO handle - name size value exceeds maximum.",
252
0
       function );
253
254
0
      goto on_error;
255
0
    }
256
0
    ( *destination_file_io_handle )->name = system_string_allocate(
257
0
                                             source_file_io_handle->name_size );
258
259
0
    if( ( *destination_file_io_handle )->name == NULL )
260
0
    {
261
0
      libcerror_error_set(
262
0
       error,
263
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
264
0
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
265
0
       "%s: unable to create name.",
266
0
       function );
267
268
0
      goto on_error;
269
0
    }
270
0
    if( source_file_io_handle->name_size > 1 )
271
0
    {
272
0
      if( system_string_copy(
273
0
           ( *destination_file_io_handle )->name,
274
0
           source_file_io_handle->name,
275
0
           source_file_io_handle->name_size ) == NULL )
276
0
      {
277
0
        libcerror_error_set(
278
0
         error,
279
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
280
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
281
0
         "%s: unable to copy name.",
282
0
         function );
283
284
0
        goto on_error;
285
0
      }
286
0
    }
287
0
    ( *destination_file_io_handle )->name[ source_file_io_handle->name_size - 1 ] = 0;
288
289
0
    ( *destination_file_io_handle )->name_size = source_file_io_handle->name_size;
290
0
  }
291
0
  return( 1 );
292
293
0
on_error:
294
0
  if( *destination_file_io_handle != NULL )
295
0
  {
296
0
    libbfio_file_io_handle_free(
297
0
     destination_file_io_handle,
298
0
     NULL );
299
0
  }
300
0
  return( -1 );
301
0
}
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
0
{
312
0
  static char *function = "libbfio_file_io_handle_get_name_size";
313
314
0
  if( file_io_handle == NULL )
315
0
  {
316
0
    libcerror_error_set(
317
0
     error,
318
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
319
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
320
0
     "%s: invalid file IO handle.",
321
0
     function );
322
323
0
    return( -1 );
324
0
  }
325
0
  if( libbfio_system_string_size_to_narrow_string(
326
0
       file_io_handle->name,
327
0
       file_io_handle->name_size,
328
0
       name_size,
329
0
       error ) != 1 )
330
0
  {
331
0
    libcerror_error_set(
332
0
     error,
333
0
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
334
0
     LIBCERROR_CONVERSION_ERROR_GENERIC,
335
0
     "%s: unable to determine name size.",
336
0
     function );
337
338
0
    return( -1 );
339
0
  }
340
0
  return( 1 );
341
0
}
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
0
{
353
0
  static char *function = "libbfio_file_io_handle_get_name";
354
355
0
  if( file_io_handle == NULL )
356
0
  {
357
0
    libcerror_error_set(
358
0
     error,
359
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
360
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
361
0
     "%s: invalid file IO handle.",
362
0
     function );
363
364
0
    return( -1 );
365
0
  }
366
0
  if( libbfio_system_string_copy_to_narrow_string(
367
0
       file_io_handle->name,
368
0
       file_io_handle->name_size,
369
0
       name,
370
0
       name_size,
371
0
       error ) != 1 )
372
0
  {
373
0
    libcerror_error_set(
374
0
     error,
375
0
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
376
0
     LIBCERROR_CONVERSION_ERROR_GENERIC,
377
0
     "%s: unable to set name.",
378
0
     function );
379
380
0
    return( -1 );
381
0
  }
382
0
  return( 1 );
383
0
}
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
0
{
394
0
  static char *function = "libbfio_file_io_handle_set_name";
395
0
  int result            = 0;
396
397
0
  if( file_io_handle == NULL )
398
0
  {
399
0
    libcerror_error_set(
400
0
     error,
401
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
402
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
403
0
     "%s: invalid file IO handle.",
404
0
     function );
405
406
0
    return( -1 );
407
0
  }
408
0
  if( name == NULL )
409
0
  {
410
0
    libcerror_error_set(
411
0
     error,
412
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
413
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
414
0
     "%s: invalid name.",
415
0
     function );
416
417
0
    return( -1 );
418
0
  }
419
0
  if( ( name_length == 0 )
420
0
   || ( name_length > ( (size_t) SSIZE_MAX - 1 ) ) )
421
0
  {
422
0
    libcerror_error_set(
423
0
     error,
424
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
425
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
426
0
     "%s: invalid name length value out of bounds.",
427
0
     function );
428
429
0
    return( -1 );
430
0
  }
431
0
  if( file_io_handle->name != NULL )
432
0
  {
433
0
    result = libcfile_file_is_open(
434
0
              file_io_handle->file,
435
0
              error );
436
437
0
    if( result == -1 )
438
0
    {
439
0
      libcerror_error_set(
440
0
       error,
441
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
442
0
       LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
443
0
       "%s: unable to determine if file is open.",
444
0
       function );
445
446
0
      return( -1 );
447
0
    }
448
0
    if( result != 0 )
449
0
    {
450
0
      libcerror_error_set(
451
0
       error,
452
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
453
0
       LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
454
0
       "%s: unable to set name when file is open.",
455
0
       function );
456
457
0
      return( -1 );
458
0
    }
459
0
    memory_free(
460
0
      file_io_handle->name );
461
462
0
    file_io_handle->name      = NULL;
463
0
    file_io_handle->name_size = 0;
464
0
  }
465
0
  if( libbfio_system_string_size_from_narrow_string(
466
0
       name,
467
0
       name_length + 1,
468
0
       &( file_io_handle->name_size ),
469
0
       error ) != 1 )
470
0
  {
471
0
    libcerror_error_set(
472
0
     error,
473
0
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
474
0
     LIBCERROR_CONVERSION_ERROR_GENERIC,
475
0
     "%s: unable to determine name size.",
476
0
     function );
477
478
0
    goto on_error;
479
0
  }
480
0
  if( ( file_io_handle->name_size == 0 )
481
0
   || ( file_io_handle->name_size > ( (size_t) SSIZE_MAX / sizeof( system_character_t ) ) ) )
482
0
  {
483
0
    libcerror_error_set(
484
0
     error,
485
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
486
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
487
0
     "%s: invalid file IO handle - name size value out of bounds.",
488
0
     function );
489
490
0
    goto on_error;
491
0
  }
492
0
  file_io_handle->name = system_string_allocate(
493
0
                          file_io_handle->name_size );
494
495
0
  if( file_io_handle->name == NULL )
496
0
  {
497
0
    libcerror_error_set(
498
0
     error,
499
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
500
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
501
0
     "%s: unable to create name.",
502
0
     function );
503
504
0
    goto on_error;
505
0
  }
506
0
  if( libbfio_system_string_copy_from_narrow_string(
507
0
       file_io_handle->name,
508
0
       file_io_handle->name_size,
509
0
       name,
510
0
       name_length + 1,
511
0
       error ) != 1 )
512
0
  {
513
0
    libcerror_error_set(
514
0
     error,
515
0
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
516
0
     LIBCERROR_CONVERSION_ERROR_GENERIC,
517
0
     "%s: unable to set name.",
518
0
     function );
519
520
0
    goto on_error;
521
0
  }
522
0
  return( 1 );
523
524
0
on_error:
525
0
  if( file_io_handle->name != NULL )
526
0
  {
527
0
    memory_free(
528
0
     file_io_handle->name );
529
530
0
    file_io_handle->name = NULL;
531
0
  }
532
0
  file_io_handle->name_size = 0;
533
534
0
  return( -1 );
535
0
}
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
0
{
783
0
  static char *function = "libbfio_file_io_handle_open";
784
0
  int result            = 0;
785
786
0
  if( file_io_handle == NULL )
787
0
  {
788
0
    libcerror_error_set(
789
0
     error,
790
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
791
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
792
0
     "%s: invalid file IO handle.",
793
0
     function );
794
795
0
    return( -1 );
796
0
  }
797
0
  if( file_io_handle->name == NULL )
798
0
  {
799
0
    libcerror_error_set(
800
0
     error,
801
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
802
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
803
0
     "%s: invalid file IO handle - missing name.",
804
0
     function );
805
806
0
    return( -1 );
807
0
  }
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
0
  result = libcfile_file_open(
816
0
            file_io_handle->file,
817
0
            file_io_handle->name,
818
0
            access_flags,
819
0
            error );
820
0
#endif
821
0
  if( result != 1 )
822
0
  {
823
0
    libcerror_error_set(
824
0
     error,
825
0
     LIBCERROR_ERROR_DOMAIN_IO,
826
0
     LIBCERROR_IO_ERROR_OPEN_FAILED,
827
0
     "%s: unable to open file: %" PRIs_SYSTEM ".",
828
0
     function,
829
0
     file_io_handle->name );
830
831
0
    return( -1 );
832
0
  }
833
0
  file_io_handle->access_flags = access_flags;
834
835
0
  return( 1 );
836
0
}
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
0
{
845
0
  static char *function = "libbfio_file_close";
846
847
0
  if( file_io_handle == NULL )
848
0
  {
849
0
    libcerror_error_set(
850
0
     error,
851
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
852
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
853
0
     "%s: invalid file IO handle.",
854
0
     function );
855
856
0
    return( -1 );
857
0
  }
858
0
  if( file_io_handle->name == NULL )
859
0
  {
860
0
    libcerror_error_set(
861
0
     error,
862
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
863
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
864
0
     "%s: invalid file IO handle - missing name.",
865
0
     function );
866
867
0
    return( -1 );
868
0
  }
869
0
  if( libcfile_file_close(
870
0
       file_io_handle->file,
871
0
       error ) != 0 )
872
0
  {
873
0
    libcerror_error_set(
874
0
     error,
875
0
     LIBCERROR_ERROR_DOMAIN_IO,
876
0
     LIBCERROR_IO_ERROR_OPEN_FAILED,
877
0
     "%s: unable to close file: %" PRIs_SYSTEM ".",
878
0
     function,
879
0
     file_io_handle->name );
880
881
0
    return( -1 );
882
0
  }
883
0
  file_io_handle->access_flags = 0;
884
885
0
  return( 0 );
886
0
}
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
0
{
897
0
  static char *function = "libbfio_file_io_handle_read_buffer";
898
0
  ssize_t read_count    = 0;
899
900
0
  if( file_io_handle == NULL )
901
0
  {
902
0
    libcerror_error_set(
903
0
     error,
904
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
905
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
906
0
     "%s: invalid file IO handle.",
907
0
     function );
908
909
0
    return( -1 );
910
0
  }
911
0
  if( file_io_handle->name == NULL )
912
0
  {
913
0
    libcerror_error_set(
914
0
     error,
915
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
916
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
917
0
     "%s: invalid file IO handle - missing name.",
918
0
     function );
919
920
0
    return( -1 );
921
0
  }
922
0
  read_count = libcfile_file_read_buffer(
923
0
                file_io_handle->file,
924
0
                buffer,
925
0
                size,
926
0
                error );
927
928
0
  if( read_count < 0 )
929
0
  {
930
0
    libcerror_error_set(
931
0
     error,
932
0
     LIBCERROR_ERROR_DOMAIN_IO,
933
0
     LIBCERROR_IO_ERROR_READ_FAILED,
934
0
     "%s: unable to read from file: %" PRIs_SYSTEM ".",
935
0
     function,
936
0
     file_io_handle->name );
937
938
0
    return( -1 );
939
0
  }
940
0
  return( read_count );
941
0
}
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
0
{
952
0
  static char *function = "libbfio_file_write_buffer";
953
0
  ssize_t write_count   = 0;
954
955
0
  if( file_io_handle == NULL )
956
0
  {
957
0
    libcerror_error_set(
958
0
     error,
959
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
960
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
961
0
     "%s: invalid file IO handle.",
962
0
     function );
963
964
0
    return( -1 );
965
0
  }
966
0
  if( file_io_handle->name == NULL )
967
0
  {
968
0
    libcerror_error_set(
969
0
     error,
970
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
971
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
972
0
     "%s: invalid file IO handle - missing name.",
973
0
     function );
974
975
0
    return( -1 );
976
0
  }
977
0
  write_count = libcfile_file_write_buffer(
978
0
                 file_io_handle->file,
979
0
                 buffer,
980
0
                 size,
981
0
                 error );
982
983
0
  if( write_count < 0 )
984
0
  {
985
0
    libcerror_error_set(
986
0
     error,
987
0
     LIBCERROR_ERROR_DOMAIN_IO,
988
0
     LIBCERROR_IO_ERROR_WRITE_FAILED,
989
0
     "%s: unable to write to file: %" PRIs_SYSTEM ".",
990
0
     function,
991
0
     file_io_handle->name );
992
993
0
    return( -1 );
994
0
  }
995
0
  return( write_count );
996
0
}
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
0
{
1007
0
  static char *function = "libbfio_file_seek_offset";
1008
0
  off64_t seek_offset   = 0;
1009
1010
0
  if( file_io_handle == NULL )
1011
0
  {
1012
0
    libcerror_error_set(
1013
0
     error,
1014
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1015
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1016
0
     "%s: invalid file IO handle.",
1017
0
     function );
1018
1019
0
    return( -1 );
1020
0
  }
1021
0
  if( file_io_handle->name == NULL )
1022
0
  {
1023
0
    libcerror_error_set(
1024
0
     error,
1025
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1026
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1027
0
     "%s: invalid file IO handle - missing name.",
1028
0
     function );
1029
1030
0
    return( -1 );
1031
0
  }
1032
0
  seek_offset = libcfile_file_seek_offset(
1033
0
                 file_io_handle->file,
1034
0
                 offset,
1035
0
                 whence,
1036
0
                 error );
1037
1038
0
  if( seek_offset == -1 )
1039
0
  {
1040
0
    libcerror_error_set(
1041
0
     error,
1042
0
     LIBCERROR_ERROR_DOMAIN_IO,
1043
0
     LIBCERROR_IO_ERROR_SEEK_FAILED,
1044
0
     "%s: unable to seek offset: %" PRIi64 " in file: %" PRIs_SYSTEM ".",
1045
0
     function,
1046
0
     offset,
1047
0
     file_io_handle->name );
1048
1049
0
    return( -1 );
1050
0
  }
1051
0
  return( seek_offset );
1052
0
}
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
0
{
1061
0
  static char *function = "libbfio_file_exists";
1062
0
  int result            = 0;
1063
1064
0
  if( file_io_handle == NULL )
1065
0
  {
1066
0
    libcerror_error_set(
1067
0
     error,
1068
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1069
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1070
0
     "%s: invalid file IO handle.",
1071
0
     function );
1072
1073
0
    return( -1 );
1074
0
  }
1075
0
  if( file_io_handle->name == NULL )
1076
0
  {
1077
0
    libcerror_error_set(
1078
0
     error,
1079
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1080
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1081
0
     "%s: invalid file IO handle - missing name.",
1082
0
     function );
1083
1084
0
    return( -1 );
1085
0
  }
1086
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1087
  result = libcfile_file_exists_wide(
1088
            file_io_handle->name,
1089
            error );
1090
#else
1091
0
  result = libcfile_file_exists(
1092
0
            file_io_handle->name,
1093
0
            error );
1094
0
#endif
1095
0
  if( result == -1 )
1096
0
  {
1097
0
    libcerror_error_set(
1098
0
     error,
1099
0
     LIBCERROR_ERROR_DOMAIN_IO,
1100
0
     LIBCERROR_IO_ERROR_GENERIC,
1101
0
     "%s: unable to determine if file: %" PRIs_SYSTEM " exists.",
1102
0
     function,
1103
0
     file_io_handle->name );
1104
1105
0
    return( -1 );
1106
0
  }
1107
0
  return( result );
1108
0
}
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
0
{
1117
0
  static char *function = "libbfio_file_is_open";
1118
0
  int result            = 0;
1119
1120
0
  if( file_io_handle == NULL )
1121
0
  {
1122
0
    libcerror_error_set(
1123
0
     error,
1124
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1125
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1126
0
     "%s: invalid file IO handle.",
1127
0
     function );
1128
1129
0
    return( -1 );
1130
0
  }
1131
0
  result = libcfile_file_is_open(
1132
0
            file_io_handle->file,
1133
0
            error );
1134
1135
0
  if( result == -1 )
1136
0
  {
1137
0
    libcerror_error_set(
1138
0
     error,
1139
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1140
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1141
0
     "%s: unable to determine if file is open.",
1142
0
     function );
1143
1144
0
    return( -1 );
1145
0
  }
1146
0
  return( result );
1147
0
}
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
0
{
1157
0
  static char *function = "libbfio_file_get_size";
1158
1159
0
  if( file_io_handle == NULL )
1160
0
  {
1161
0
    libcerror_error_set(
1162
0
     error,
1163
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1164
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1165
0
     "%s: invalid file IO handle.",
1166
0
     function );
1167
1168
0
    return( -1 );
1169
0
  }
1170
0
  if( file_io_handle->name == NULL )
1171
0
  {
1172
0
    libcerror_error_set(
1173
0
     error,
1174
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1175
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1176
0
     "%s: invalid file IO handle - missing name.",
1177
0
     function );
1178
1179
0
    return( -1 );
1180
0
  }
1181
0
  if( libcfile_file_get_size(
1182
0
       file_io_handle->file,
1183
0
       size,
1184
0
       error ) != 1 )
1185
0
  {
1186
0
    libcerror_error_set(
1187
0
     error,
1188
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1189
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1190
0
     "%s: unable to retrieve size of file: %" PRIs_SYSTEM ".",
1191
0
     function,
1192
0
     file_io_handle->name );
1193
1194
0
    return( -1 );
1195
0
  }
1196
0
  return( 1 );
1197
0
}
1198