Coverage Report

Created: 2024-02-25 07:19

/src/libvsgpt/libbfio/libbfio_handle.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The 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 <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
1.03k
{
84
1.03k
  libbfio_internal_handle_t *internal_handle = NULL;
85
1.03k
  static char *function                      = "libbfio_handle_initialize";
86
87
1.03k
  if( handle == NULL )
88
0
  {
89
0
    libcerror_error_set(
90
0
     error,
91
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
92
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
93
0
     "%s: invalid handle.",
94
0
     function );
95
96
0
    return( -1 );
97
0
  }
98
1.03k
  if( *handle != NULL )
99
0
  {
100
0
    libcerror_error_set(
101
0
     error,
102
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
103
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
104
0
     "%s: invalid handle value already set.",
105
0
     function );
106
107
0
    return( -1 );
108
0
  }
109
1.03k
  internal_handle = memory_allocate_structure(
110
1.03k
                     libbfio_internal_handle_t );
111
112
1.03k
  if( internal_handle == NULL )
113
0
  {
114
0
    libcerror_error_set(
115
0
     error,
116
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
117
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
118
0
     "%s: unable to create handle.",
119
0
     function );
120
121
0
    goto on_error;
122
0
  }
123
1.03k
  if( memory_set(
124
1.03k
       internal_handle,
125
1.03k
       0,
126
1.03k
       sizeof( libbfio_internal_handle_t ) ) == NULL )
127
0
  {
128
0
    libcerror_error_set(
129
0
     error,
130
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
131
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
132
0
     "%s: unable to clear handle.",
133
0
     function );
134
135
0
    memory_free(
136
0
     internal_handle );
137
138
0
    return( -1 );
139
0
  }
140
1.03k
  if( libcdata_range_list_initialize(
141
1.03k
       &( internal_handle->offsets_read ),
142
1.03k
       error ) != 1 )
143
0
  {
144
0
    libcerror_error_set(
145
0
     error,
146
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
147
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
148
0
     "%s: unable to create read offsets list.",
149
0
     function );
150
151
0
    goto on_error;
152
0
  }
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
1.03k
  internal_handle->io_handle       = io_handle;
169
1.03k
  internal_handle->flags           = flags;
170
1.03k
  internal_handle->free_io_handle  = free_io_handle;
171
1.03k
  internal_handle->clone_io_handle = clone_io_handle;
172
1.03k
  internal_handle->open            = open;
173
1.03k
  internal_handle->close           = close;
174
1.03k
  internal_handle->read            = read;
175
1.03k
  internal_handle->write           = write;
176
1.03k
  internal_handle->seek_offset     = seek_offset;
177
1.03k
  internal_handle->exists          = exists;
178
1.03k
  internal_handle->is_open         = is_open;
179
1.03k
  internal_handle->get_size        = get_size;
180
181
1.03k
  *handle = (libbfio_handle_t *) internal_handle;
182
183
1.03k
  return( 1 );
184
185
0
on_error:
186
0
  if( internal_handle != NULL )
187
0
  {
188
0
    if( internal_handle->offsets_read != NULL )
189
0
    {
190
0
      libcdata_range_list_free(
191
0
       &( internal_handle->offsets_read ),
192
0
       NULL,
193
0
       NULL );
194
0
    }
195
0
    memory_free(
196
0
     internal_handle );
197
0
  }
198
0
  return( -1 );
199
1.03k
}
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
1.03k
{
208
1.03k
  libbfio_internal_handle_t *internal_handle = NULL;
209
1.03k
  static char *function                      = "libbfio_handle_free";
210
1.03k
  int is_open                                = 0;
211
1.03k
  int result                                 = 1;
212
213
1.03k
  if( handle == NULL )
214
0
  {
215
0
    libcerror_error_set(
216
0
     error,
217
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
218
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
219
0
     "%s: invalid handle.",
220
0
     function );
221
222
0
    return( -1 );
223
0
  }
224
1.03k
  if( *handle != NULL )
225
1.03k
  {
226
1.03k
    internal_handle = (libbfio_internal_handle_t *) *handle;
227
1.03k
    *handle         = NULL;
228
229
1.03k
    if( internal_handle->is_open != NULL )
230
1.03k
    {
231
1.03k
      is_open = internal_handle->is_open(
232
1.03k
           internal_handle->io_handle,
233
1.03k
                 error );
234
235
1.03k
      if( is_open == -1 )
236
0
      {
237
0
        libcerror_error_set(
238
0
         error,
239
0
         LIBCERROR_ERROR_DOMAIN_IO,
240
0
         LIBCERROR_IO_ERROR_OPEN_FAILED,
241
0
         "%s: unable to determine if handle is open.",
242
0
         function );
243
244
0
        result = -1;
245
0
      }
246
1.03k
    }
247
1.03k
    if( is_open != 0 )
248
0
    {
249
0
      if( internal_handle->close != NULL )
250
0
      {
251
0
        if( internal_handle->close(
252
0
             internal_handle->io_handle,
253
0
             error ) != 0 )
254
0
        {
255
0
          libcerror_error_set(
256
0
           error,
257
0
           LIBCERROR_ERROR_DOMAIN_IO,
258
0
           LIBCERROR_IO_ERROR_CLOSE_FAILED,
259
0
           "%s: unable to close handle.",
260
0
           function );
261
262
0
          result = -1;
263
0
        }
264
0
      }
265
0
    }
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
1.03k
    if( ( internal_handle->flags & LIBBFIO_FLAG_IO_HANDLE_MANAGED ) != 0 )
282
1.03k
    {
283
1.03k
      if( internal_handle->io_handle != NULL )
284
1.03k
      {
285
1.03k
        if( internal_handle->free_io_handle == NULL )
286
0
        {
287
0
          memory_free(
288
0
           internal_handle->io_handle );
289
0
        }
290
1.03k
        else if( internal_handle->free_io_handle(
291
1.03k
            &( internal_handle->io_handle ),
292
1.03k
            error ) != 1 )
293
0
        {
294
0
          libcerror_error_set(
295
0
           error,
296
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
297
0
           LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
298
0
           "%s: unable to free IO handle.",
299
0
           function );
300
301
0
          result = -1;
302
0
        }
303
1.03k
      }
304
1.03k
    }
305
1.03k
    if( internal_handle->offsets_read != NULL )
306
1.03k
    {
307
1.03k
      if( libcdata_range_list_free(
308
1.03k
           &( internal_handle->offsets_read ),
309
1.03k
           NULL,
310
1.03k
           error ) != 1 )
311
0
      {
312
0
        libcerror_error_set(
313
0
         error,
314
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
315
0
         LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
316
0
         "%s: unable to free read offsets list.",
317
0
         function );
318
319
0
        result = -1;
320
0
      }
321
1.03k
    }
322
1.03k
    memory_free(
323
1.03k
     internal_handle );
324
1.03k
  }
325
1.03k
  return( result );
326
1.03k
}
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
0
{
337
0
  libbfio_internal_handle_t *internal_source_handle = NULL;
338
0
  intptr_t *destination_io_handle                   = NULL;
339
0
  static char *function                             = "libbfio_handle_clone";
340
0
  uint8_t destination_flags                         = 0;
341
342
0
  if( destination_handle == NULL )
343
0
  {
344
0
    libcerror_error_set(
345
0
     error,
346
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
347
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
348
0
     "%s: invalid destination handle.",
349
0
     function );
350
351
0
    return( -1 );
352
0
  }
353
0
  if( *destination_handle != NULL )
354
0
  {
355
0
    libcerror_error_set(
356
0
     error,
357
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
358
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
359
0
     "%s: destination handle already set.",
360
0
     function );
361
362
0
    return( -1 );
363
0
  }
364
0
  if( source_handle == NULL )
365
0
  {
366
0
    *destination_handle = NULL;
367
368
0
    return( 1 );
369
0
  }
370
0
  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
0
  if( internal_source_handle->io_handle != NULL )
388
0
  {
389
0
    if( ( internal_source_handle->flags & LIBBFIO_FLAG_IO_HANDLE_CLONE_BY_REFERENCE ) != 0 )
390
0
    {
391
0
      destination_io_handle = internal_source_handle->io_handle;
392
393
0
      destination_flags = LIBBFIO_FLAG_IO_HANDLE_CLONE_BY_REFERENCE;
394
0
    }
395
0
    else
396
0
    {
397
0
      if( internal_source_handle->clone_io_handle == NULL )
398
0
      {
399
0
        libcerror_error_set(
400
0
         error,
401
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
402
0
         LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
403
0
         "%s: invalid handle - missing clone IO handle function.",
404
0
         function );
405
406
0
        goto on_error;
407
0
      }
408
0
      if( internal_source_handle->clone_io_handle(
409
0
           &destination_io_handle,
410
0
           internal_source_handle->io_handle,
411
0
           error ) != 1 )
412
0
      {
413
0
        libcerror_error_set(
414
0
         error,
415
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
416
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
417
0
         "%s: unable to clone IO handle.",
418
0
         function );
419
420
0
        goto on_error;
421
0
      }
422
0
      destination_flags = LIBBFIO_FLAG_IO_HANDLE_MANAGED;
423
0
    }
424
0
  }
425
0
  if( libbfio_handle_initialize(
426
0
       destination_handle,
427
0
       destination_io_handle,
428
0
       internal_source_handle->free_io_handle,
429
0
       internal_source_handle->clone_io_handle,
430
0
       internal_source_handle->open,
431
0
       internal_source_handle->close,
432
0
       internal_source_handle->read,
433
0
       internal_source_handle->write,
434
0
       internal_source_handle->seek_offset,
435
0
       internal_source_handle->exists,
436
0
       internal_source_handle->is_open,
437
0
       internal_source_handle->get_size,
438
0
       destination_flags,
439
0
       error ) != 1 )
440
0
  {
441
0
    libcerror_error_set(
442
0
     error,
443
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
444
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
445
0
     "%s: unable to create destination handle.",
446
0
     function );
447
448
0
    goto on_error;
449
0
  }
450
0
  destination_io_handle = NULL;
451
452
0
  if( internal_source_handle->access_flags != 0 )
453
0
  {
454
0
    if( libbfio_handle_open(
455
0
         *destination_handle,
456
0
         internal_source_handle->access_flags,
457
0
         error ) == -1 )
458
0
    {
459
0
      libcerror_error_set(
460
0
       error,
461
0
       LIBCERROR_ERROR_DOMAIN_IO,
462
0
       LIBCERROR_IO_ERROR_OPEN_FAILED,
463
0
       "%s: unable to open destination handle.",
464
0
       function );
465
466
0
      goto on_error;
467
0
    }
468
0
    if( libbfio_handle_seek_offset(
469
0
         *destination_handle,
470
0
         internal_source_handle->current_offset,
471
0
         SEEK_SET,
472
0
         error ) == -1 )
473
0
    {
474
0
      libcerror_error_set(
475
0
       error,
476
0
       LIBCERROR_ERROR_DOMAIN_IO,
477
0
       LIBCERROR_IO_ERROR_SEEK_FAILED,
478
0
       "%s: unable to seek offset in destination handle.",
479
0
       function );
480
481
0
      goto on_error;
482
0
    }
483
0
  }
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
0
  return( 1 );
504
505
0
on_error:
506
0
  if( ( destination_io_handle != NULL )
507
0
   && ( ( destination_flags & LIBBFIO_FLAG_IO_HANDLE_MANAGED ) != 0 ) )
508
0
  {
509
0
    if( internal_source_handle->free_io_handle == NULL )
510
0
    {
511
0
      memory_free(
512
0
       destination_io_handle );
513
0
    }
514
0
    else
515
0
    {
516
0
      internal_source_handle->free_io_handle(
517
0
       &destination_io_handle,
518
0
       NULL );
519
0
    }
520
0
  }
521
0
  if( destination_handle != NULL )
522
0
  {
523
0
    libbfio_handle_free(
524
0
     destination_handle,
525
0
     NULL );
526
0
  }
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
0
  return( -1 );
533
0
}
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
1.03k
{
543
1.03k
  libbfio_internal_handle_t *internal_handle = NULL;
544
1.03k
  static char *function                      = "libbfio_handle_open";
545
546
1.03k
  if( handle == NULL )
547
0
  {
548
0
    libcerror_error_set(
549
0
     error,
550
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
551
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
552
0
     "%s: invalid handle.",
553
0
     function );
554
555
0
    return( -1 );
556
0
  }
557
1.03k
  internal_handle = (libbfio_internal_handle_t *) handle;
558
559
1.03k
  if( internal_handle->io_handle == NULL )
560
0
  {
561
0
    libcerror_error_set(
562
0
     error,
563
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
564
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
565
0
     "%s: invalid handle - missing IO handle.",
566
0
     function );
567
568
0
    return( -1 );
569
0
  }
570
1.03k
  if( internal_handle->open == NULL )
571
0
  {
572
0
    libcerror_error_set(
573
0
     error,
574
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
575
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
576
0
     "%s: invalid handle - missing open function.",
577
0
     function );
578
579
0
    return( -1 );
580
0
  }
581
1.03k
  if( ( ( access_flags & LIBBFIO_ACCESS_FLAG_READ ) == 0 )
582
1.03k
   && ( ( access_flags & LIBBFIO_ACCESS_FLAG_WRITE ) == 0 ) )
583
0
  {
584
0
    libcerror_error_set(
585
0
     error,
586
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
587
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
588
0
     "%s: unsupported access flags: 0x%02x.",
589
0
     function,
590
0
     access_flags );
591
592
0
    return( -1 );
593
0
  }
594
1.03k
  if( ( internal_handle->open_on_demand != 0 )
595
1.03k
   && ( ( access_flags & LIBBFIO_ACCESS_FLAG_WRITE ) != 0 ) )
596
0
  {
597
0
    libcerror_error_set(
598
0
     error,
599
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
600
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
601
0
     "%s: open on demand cannot be used in combination with write access.",
602
0
     function );
603
604
0
    return( -1 );
605
0
  }
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
1.03k
  if( internal_handle->open_on_demand == 0 )
622
1.03k
  {
623
1.03k
    if( internal_handle->open(
624
1.03k
         internal_handle->io_handle,
625
1.03k
         access_flags,
626
1.03k
         error ) != 1 )
627
0
    {
628
0
      libcerror_error_set(
629
0
       error,
630
0
       LIBCERROR_ERROR_DOMAIN_IO,
631
0
       LIBCERROR_IO_ERROR_OPEN_FAILED,
632
0
       "%s: unable to open handle.",
633
0
       function );
634
635
0
      goto on_error;
636
0
    }
637
1.03k
  }
638
1.03k
  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
1.03k
  return( 1 );
656
657
0
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
0
  return( -1 );
664
1.03k
}
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
0
{
674
0
  libbfio_internal_handle_t *internal_handle = NULL;
675
0
  static char *function                      = "libbfio_handle_reopen";
676
677
0
  if( handle == NULL )
678
0
  {
679
0
    libcerror_error_set(
680
0
     error,
681
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
682
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
683
0
     "%s: invalid handle.",
684
0
     function );
685
686
0
    return( -1 );
687
0
  }
688
0
  internal_handle = (libbfio_internal_handle_t *) handle;
689
690
0
  if( internal_handle->io_handle == NULL )
691
0
  {
692
0
    libcerror_error_set(
693
0
     error,
694
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
695
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
696
0
     "%s: invalid handle - missing IO handle.",
697
0
     function );
698
699
0
    return( -1 );
700
0
  }
701
0
  if( internal_handle->close == NULL )
702
0
  {
703
0
    libcerror_error_set(
704
0
     error,
705
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
706
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
707
0
     "%s: invalid handle - missing close function.",
708
0
     function );
709
710
0
    return( -1 );
711
0
  }
712
0
  if( internal_handle->open == NULL )
713
0
  {
714
0
    libcerror_error_set(
715
0
     error,
716
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
717
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
718
0
     "%s: invalid handle - missing open function.",
719
0
     function );
720
721
0
    return( -1 );
722
0
  }
723
0
  if( internal_handle->seek_offset == NULL )
724
0
  {
725
0
    libcerror_error_set(
726
0
     error,
727
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
728
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
729
0
     "%s: invalid handle - missing seek offset function.",
730
0
     function );
731
732
0
    return( -1 );
733
0
  }
734
0
  if( ( ( access_flags & LIBBFIO_ACCESS_FLAG_READ ) == 0 )
735
0
   && ( ( access_flags & LIBBFIO_ACCESS_FLAG_WRITE ) == 0 ) )
736
0
  {
737
0
    libcerror_error_set(
738
0
     error,
739
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
740
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
741
0
     "%s: unsupported access flags.",
742
0
     function );
743
744
0
    return( -1 );
745
0
  }
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
0
  if( internal_handle->access_flags != access_flags )
764
0
  {
765
0
    if( internal_handle->close(
766
0
         internal_handle->io_handle,
767
0
         error ) != 0 )
768
0
    {
769
0
      libcerror_error_set(
770
0
       error,
771
0
       LIBCERROR_ERROR_DOMAIN_IO,
772
0
       LIBCERROR_IO_ERROR_CLOSE_FAILED,
773
0
       "%s: unable to close handle.",
774
0
       function );
775
776
0
      goto on_error;
777
0
    }
778
0
    if( internal_handle->open_on_demand == 0 )
779
0
    {
780
0
      if( internal_handle->open(
781
0
           internal_handle->io_handle,
782
0
           access_flags,
783
0
           error ) != 1 )
784
0
      {
785
0
        libcerror_error_set(
786
0
         error,
787
0
         LIBCERROR_ERROR_DOMAIN_IO,
788
0
         LIBCERROR_IO_ERROR_OPEN_FAILED,
789
0
         "%s: unable to open handle.",
790
0
         function );
791
792
0
        goto on_error;
793
0
      }
794
0
    }
795
0
    internal_handle->access_flags = access_flags;
796
797
0
    if( internal_handle->open_on_demand == 0 )
798
0
    {
799
      /* Seek the previous file offset only when at least reading the file
800
       */
801
0
      if( ( internal_handle->access_flags & LIBBFIO_ACCESS_FLAG_READ ) != 0 )
802
0
      {
803
0
        if( internal_handle->seek_offset(
804
0
             internal_handle->io_handle,
805
0
             internal_handle->current_offset,
806
0
             SEEK_CUR,
807
0
             error ) == -1 )
808
0
        {
809
0
          libcerror_error_set(
810
0
           error,
811
0
           LIBCERROR_ERROR_DOMAIN_IO,
812
0
           LIBCERROR_IO_ERROR_SEEK_FAILED,
813
0
           "%s: unable to seek offset in handle.",
814
0
           function );
815
816
0
          goto on_error;
817
0
        }
818
0
      }
819
0
    }
820
0
  }
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
0
  return( 1 );
837
838
0
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
0
  return( -1 );
845
0
}
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
1.03k
{
854
1.03k
  libbfio_internal_handle_t *internal_handle = NULL;
855
1.03k
  static char *function                      = "libbfio_handle_close";
856
1.03k
  int is_open                                = 1;
857
858
1.03k
  if( handle == NULL )
859
0
  {
860
0
    libcerror_error_set(
861
0
     error,
862
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
863
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
864
0
     "%s: invalid handle.",
865
0
     function );
866
867
0
    return( -1 );
868
0
  }
869
1.03k
  internal_handle = (libbfio_internal_handle_t *) handle;
870
871
1.03k
  if( internal_handle->io_handle == NULL )
872
0
  {
873
0
    libcerror_error_set(
874
0
     error,
875
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
876
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
877
0
     "%s: invalid handle - missing IO handle.",
878
0
     function );
879
880
0
    return( -1 );
881
0
  }
882
1.03k
  if( internal_handle->close == NULL )
883
0
  {
884
0
    libcerror_error_set(
885
0
     error,
886
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
887
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
888
0
     "%s: invalid handle - missing close function.",
889
0
     function );
890
891
0
    return( -1 );
892
0
  }
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
1.03k
  if( internal_handle->open_on_demand != 0 )
909
0
  {
910
0
    if( internal_handle->is_open == NULL )
911
0
    {
912
0
      libcerror_error_set(
913
0
       error,
914
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
915
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
916
0
       "%s: invalid handle - missing is open function.",
917
0
       function );
918
919
0
      goto on_error;
920
0
    }
921
0
    is_open = internal_handle->is_open(
922
0
         internal_handle->io_handle,
923
0
               error );
924
925
0
    if( is_open == -1 )
926
0
    {
927
0
      libcerror_error_set(
928
0
       error,
929
0
       LIBCERROR_ERROR_DOMAIN_IO,
930
0
       LIBCERROR_IO_ERROR_OPEN_FAILED,
931
0
       "%s: unable to determine if handle is open.",
932
0
       function );
933
934
0
      goto on_error;
935
0
    }
936
0
  }
937
1.03k
  if( is_open != 0 )
938
1.03k
  {
939
1.03k
    if( internal_handle->close(
940
1.03k
         internal_handle->io_handle,
941
1.03k
         error ) != 0 )
942
0
    {
943
0
      libcerror_error_set(
944
0
       error,
945
0
       LIBCERROR_ERROR_DOMAIN_IO,
946
0
       LIBCERROR_IO_ERROR_CLOSE_FAILED,
947
0
       "%s: unable to close handle.",
948
0
       function );
949
950
0
      goto on_error;
951
0
    }
952
1.03k
  }
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
1.03k
  return( 0 );
969
970
0
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
0
  return( -1 );
977
1.03k
}
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
4.38k
{
989
4.38k
  static char *function = "libbfio_internal_handle_read_buffer";
990
4.38k
  ssize_t read_count    = 0;
991
4.38k
  int is_open           = 0;
992
4.38k
  int result            = 0;
993
994
4.38k
  if( internal_handle == NULL )
995
0
  {
996
0
    libcerror_error_set(
997
0
     error,
998
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
999
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1000
0
     "%s: invalid handle.",
1001
0
     function );
1002
1003
0
    return( -1 );
1004
0
  }
1005
4.38k
  if( internal_handle->read == NULL )
1006
0
  {
1007
0
    libcerror_error_set(
1008
0
     error,
1009
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1010
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1011
0
     "%s: invalid handle - missing read function.",
1012
0
     function );
1013
1014
0
    return( -1 );
1015
0
  }
1016
4.38k
  if( buffer == NULL )
1017
0
  {
1018
0
    libcerror_error_set(
1019
0
     error,
1020
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1021
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1022
0
     "%s: invalid buffer.",
1023
0
     function );
1024
1025
0
    return( -1 );
1026
0
  }
1027
4.38k
  if( size > (size_t) SSIZE_MAX )
1028
0
  {
1029
0
    libcerror_error_set(
1030
0
     error,
1031
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1032
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1033
0
     "%s: invalid size value exceeds maximum.",
1034
0
     function );
1035
1036
0
    return( -1 );
1037
0
  }
1038
4.38k
  if( internal_handle->open_on_demand != 0 )
1039
0
  {
1040
0
    if( internal_handle->is_open == NULL )
1041
0
    {
1042
0
      libcerror_error_set(
1043
0
       error,
1044
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1045
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1046
0
       "%s: invalid handle - missing is open function.",
1047
0
       function );
1048
1049
0
      return( -1 );
1050
0
    }
1051
0
    if( internal_handle->open == NULL )
1052
0
    {
1053
0
      libcerror_error_set(
1054
0
       error,
1055
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1056
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1057
0
       "%s: invalid handle - missing open function.",
1058
0
       function );
1059
1060
0
      return( -1 );
1061
0
    }
1062
0
    if( internal_handle->seek_offset == NULL )
1063
0
    {
1064
0
      libcerror_error_set(
1065
0
       error,
1066
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1067
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1068
0
       "%s: invalid handle - missing seek offset function.",
1069
0
       function );
1070
1071
0
      return( -1 );
1072
0
    }
1073
0
    is_open = internal_handle->is_open(
1074
0
         internal_handle->io_handle,
1075
0
               error );
1076
1077
0
    if( is_open == -1 )
1078
0
    {
1079
0
      libcerror_error_set(
1080
0
       error,
1081
0
       LIBCERROR_ERROR_DOMAIN_IO,
1082
0
       LIBCERROR_IO_ERROR_OPEN_FAILED,
1083
0
       "%s: unable to determine if handle is open.",
1084
0
       function );
1085
1086
0
      return( -1 );
1087
0
    }
1088
0
    else if( is_open == 0 )
1089
0
    {
1090
0
      if( internal_handle->open(
1091
0
           internal_handle->io_handle,
1092
0
           internal_handle->access_flags,
1093
0
           error ) != 1 )
1094
0
      {
1095
0
        libcerror_error_set(
1096
0
         error,
1097
0
         LIBCERROR_ERROR_DOMAIN_IO,
1098
0
         LIBCERROR_IO_ERROR_OPEN_FAILED,
1099
0
         "%s: unable to open handle on demand.",
1100
0
         function );
1101
1102
0
        return( -1 );
1103
0
      }
1104
0
      if( internal_handle->seek_offset(
1105
0
           internal_handle->io_handle,
1106
0
           internal_handle->current_offset,
1107
0
           SEEK_SET,
1108
0
           error ) == -1 )
1109
0
      {
1110
0
        libcerror_error_set(
1111
0
         error,
1112
0
         LIBCERROR_ERROR_DOMAIN_IO,
1113
0
         LIBCERROR_IO_ERROR_SEEK_FAILED,
1114
0
         "%s: unable to seek current offset: %" PRIi64 " in handle.",
1115
0
         function,
1116
0
         internal_handle->current_offset );
1117
1118
0
        return( -1 );
1119
0
      }
1120
0
    }
1121
0
  }
1122
4.38k
  read_count = internal_handle->read(
1123
4.38k
                internal_handle->io_handle,
1124
4.38k
                buffer,
1125
4.38k
                size,
1126
4.38k
                error );
1127
1128
4.38k
  if( read_count < 0 )
1129
283
  {
1130
283
    libcerror_error_set(
1131
283
     error,
1132
283
     LIBCERROR_ERROR_DOMAIN_IO,
1133
283
     LIBCERROR_IO_ERROR_READ_FAILED,
1134
283
     "%s: unable to read from handle.",
1135
283
     function );
1136
1137
283
    return( -1 );
1138
283
  }
1139
4.10k
  if( internal_handle->track_offsets_read != 0 )
1140
0
  {
1141
0
    result = libcdata_range_list_insert_range(
1142
0
              internal_handle->offsets_read,
1143
0
              (uint64_t) internal_handle->current_offset,
1144
0
              (uint64_t) read_count,
1145
0
              NULL,
1146
0
              NULL,
1147
0
              NULL,
1148
0
              error );
1149
1150
0
    if( result == -1 )
1151
0
    {
1152
0
      libcerror_error_set(
1153
0
       error,
1154
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1155
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1156
0
       "%s: unable to insert offset range to offsets read table.",
1157
0
       function );
1158
1159
0
      return( -1 );
1160
0
    }
1161
0
  }
1162
4.10k
  internal_handle->current_offset += (off64_t) read_count;
1163
1164
4.10k
  if( internal_handle->open_on_demand != 0 )
1165
0
  {
1166
0
    if( internal_handle->close == NULL )
1167
0
    {
1168
0
      libcerror_error_set(
1169
0
       error,
1170
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1171
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1172
0
       "%s: invalid handle - missing close function.",
1173
0
       function );
1174
1175
0
      return( -1 );
1176
0
    }
1177
0
    if( internal_handle->close(
1178
0
         internal_handle->io_handle,
1179
0
         error ) != 0 )
1180
0
    {
1181
0
      libcerror_error_set(
1182
0
       error,
1183
0
       LIBCERROR_ERROR_DOMAIN_IO,
1184
0
       LIBCERROR_IO_ERROR_OPEN_FAILED,
1185
0
       "%s: unable to close handle on demand.",
1186
0
       function );
1187
1188
0
      return( -1 );
1189
0
    }
1190
0
  }
1191
4.10k
  return( read_count );
1192
4.10k
}
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
0
{
1203
0
  libbfio_internal_handle_t *internal_handle = NULL;
1204
0
  static char *function                      = "libbfio_handle_read_buffer";
1205
0
  ssize_t read_count                         = 0;
1206
1207
0
  if( handle == NULL )
1208
0
  {
1209
0
    libcerror_error_set(
1210
0
     error,
1211
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1212
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1213
0
     "%s: invalid handle.",
1214
0
     function );
1215
1216
0
    return( -1 );
1217
0
  }
1218
0
  internal_handle = (libbfio_internal_handle_t *) handle;
1219
1220
0
  if( internal_handle->io_handle == NULL )
1221
0
  {
1222
0
    libcerror_error_set(
1223
0
     error,
1224
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1225
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1226
0
     "%s: invalid handle - missing IO handle.",
1227
0
     function );
1228
1229
0
    return( -1 );
1230
0
  }
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
0
  read_count = libbfio_internal_handle_read_buffer(
1247
0
                internal_handle,
1248
0
                buffer,
1249
0
                size,
1250
0
                error );
1251
1252
0
  if( read_count < 0 )
1253
0
  {
1254
0
    libcerror_error_set(
1255
0
     error,
1256
0
     LIBCERROR_ERROR_DOMAIN_IO,
1257
0
     LIBCERROR_IO_ERROR_READ_FAILED,
1258
0
     "%s: unable to read buffer.",
1259
0
     function );
1260
1261
0
    read_count = -1;
1262
0
  }
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
0
  return( read_count );
1279
0
}
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
4.44k
{
1291
4.44k
  libbfio_internal_handle_t *internal_handle = NULL;
1292
4.44k
  static char *function                      = "libbfio_handle_read_buffer_at_offset";
1293
4.44k
  ssize_t read_count                         = 0;
1294
1295
4.44k
  if( handle == NULL )
1296
0
  {
1297
0
    libcerror_error_set(
1298
0
     error,
1299
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1300
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1301
0
     "%s: invalid handle.",
1302
0
     function );
1303
1304
0
    return( -1 );
1305
0
  }
1306
4.44k
  internal_handle = (libbfio_internal_handle_t *) handle;
1307
1308
4.44k
  if( internal_handle->io_handle == NULL )
1309
0
  {
1310
0
    libcerror_error_set(
1311
0
     error,
1312
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1313
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1314
0
     "%s: invalid handle - missing IO handle.",
1315
0
     function );
1316
1317
0
    return( -1 );
1318
0
  }
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
4.44k
  if( libbfio_internal_handle_seek_offset(
1335
4.44k
       internal_handle,
1336
4.44k
       offset,
1337
4.44k
       SEEK_SET,
1338
4.44k
       error ) == -1 )
1339
60
  {
1340
60
    libcerror_error_set(
1341
60
     error,
1342
60
     LIBCERROR_ERROR_DOMAIN_IO,
1343
60
     LIBCERROR_IO_ERROR_SEEK_FAILED,
1344
60
     "%s: unable to seek offset: %" PRIi64 " (0x%08" PRIx64 ") in handle.",
1345
60
     function,
1346
60
     offset,
1347
60
     offset );
1348
1349
60
    read_count = -1;
1350
60
  }
1351
4.38k
  else
1352
4.38k
  {
1353
4.38k
    read_count = libbfio_internal_handle_read_buffer(
1354
4.38k
                  internal_handle,
1355
4.38k
                  buffer,
1356
4.38k
                  size,
1357
4.38k
                  error );
1358
1359
4.38k
    if( read_count < 0 )
1360
283
    {
1361
283
      libcerror_error_set(
1362
283
       error,
1363
283
       LIBCERROR_ERROR_DOMAIN_IO,
1364
283
       LIBCERROR_IO_ERROR_READ_FAILED,
1365
283
       "%s: unable to read buffer.",
1366
283
       function );
1367
1368
283
      read_count = -1;
1369
283
    }
1370
4.38k
  }
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
4.44k
  return( read_count );
1387
4.44k
}
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
0
{
1399
0
  static char *function = "libbfio_internal_handle_write_buffer";
1400
0
  ssize_t write_count   = 0;
1401
1402
0
  if( internal_handle == NULL )
1403
0
  {
1404
0
    libcerror_error_set(
1405
0
     error,
1406
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1407
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1408
0
     "%s: invalid handle.",
1409
0
     function );
1410
1411
0
    return( -1 );
1412
0
  }
1413
0
  if( internal_handle->write == NULL )
1414
0
  {
1415
0
    libcerror_error_set(
1416
0
     error,
1417
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1418
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1419
0
     "%s: invalid handle - missing write function.",
1420
0
     function );
1421
1422
0
    return( -1 );
1423
0
  }
1424
0
  if( buffer == NULL )
1425
0
  {
1426
0
    libcerror_error_set(
1427
0
     error,
1428
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1429
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1430
0
     "%s: invalid buffer.",
1431
0
     function );
1432
1433
0
    return( -1 );
1434
0
  }
1435
0
  if( size > (size_t) SSIZE_MAX )
1436
0
  {
1437
0
    libcerror_error_set(
1438
0
     error,
1439
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1440
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1441
0
     "%s: invalid size value exceeds maximum.",
1442
0
     function );
1443
1444
0
    return( -1 );
1445
0
  }
1446
0
  write_count = internal_handle->write(
1447
0
                 internal_handle->io_handle,
1448
0
                 buffer,
1449
0
                 size,
1450
0
                 error );
1451
1452
0
  if( write_count < 0 )
1453
0
  {
1454
0
    libcerror_error_set(
1455
0
     error,
1456
0
     LIBCERROR_ERROR_DOMAIN_IO,
1457
0
     LIBCERROR_IO_ERROR_WRITE_FAILED,
1458
0
     "%s: unable to write to handle.",
1459
0
     function );
1460
1461
0
    return( -1 );
1462
0
  }
1463
0
  internal_handle->current_offset += (off64_t) write_count;
1464
1465
0
  if( (size64_t) internal_handle->current_offset > internal_handle->size )
1466
0
  {
1467
0
    internal_handle->size = (size64_t) internal_handle->current_offset;
1468
0
  }
1469
0
  return( write_count );
1470
0
}
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
0
{
1481
0
  libbfio_internal_handle_t *internal_handle = NULL;
1482
0
  static char *function                      = "libbfio_handle_write_buffer";
1483
0
  ssize_t write_count                        = 0;
1484
1485
0
  if( handle == NULL )
1486
0
  {
1487
0
    libcerror_error_set(
1488
0
     error,
1489
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1490
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1491
0
     "%s: invalid handle.",
1492
0
     function );
1493
1494
0
    return( -1 );
1495
0
  }
1496
0
  internal_handle = (libbfio_internal_handle_t *) handle;
1497
1498
0
  if( internal_handle->io_handle == NULL )
1499
0
  {
1500
0
    libcerror_error_set(
1501
0
     error,
1502
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1503
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1504
0
     "%s: invalid handle - missing IO handle.",
1505
0
     function );
1506
1507
0
    return( -1 );
1508
0
  }
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
0
  write_count = libbfio_internal_handle_write_buffer(
1525
0
                 internal_handle,
1526
0
                 buffer,
1527
0
                 size,
1528
0
                 error );
1529
1530
0
  if( write_count < 0 )
1531
0
  {
1532
0
    libcerror_error_set(
1533
0
     error,
1534
0
     LIBCERROR_ERROR_DOMAIN_IO,
1535
0
     LIBCERROR_IO_ERROR_WRITE_FAILED,
1536
0
     "%s: unable to write to handle.",
1537
0
     function );
1538
1539
0
    write_count = -1;
1540
0
  }
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
0
  return( write_count );
1557
0
}
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
0
{
1569
0
  libbfio_internal_handle_t *internal_handle = NULL;
1570
0
  static char *function                      = "libbfio_handle_write_buffer_at_offset";
1571
0
  ssize_t write_count                        = 0;
1572
1573
0
  if( handle == NULL )
1574
0
  {
1575
0
    libcerror_error_set(
1576
0
     error,
1577
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1578
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1579
0
     "%s: invalid handle.",
1580
0
     function );
1581
1582
0
    return( -1 );
1583
0
  }
1584
0
  internal_handle = (libbfio_internal_handle_t *) handle;
1585
1586
0
  if( internal_handle->io_handle == NULL )
1587
0
  {
1588
0
    libcerror_error_set(
1589
0
     error,
1590
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1591
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1592
0
     "%s: invalid handle - missing IO handle.",
1593
0
     function );
1594
1595
0
    return( -1 );
1596
0
  }
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
0
  if( libbfio_internal_handle_seek_offset(
1613
0
       internal_handle,
1614
0
       offset,
1615
0
       SEEK_SET,
1616
0
       error ) == -1 )
1617
0
  {
1618
0
    libcerror_error_set(
1619
0
     error,
1620
0
     LIBCERROR_ERROR_DOMAIN_IO,
1621
0
     LIBCERROR_IO_ERROR_SEEK_FAILED,
1622
0
     "%s: unable to seek offset: %" PRIi64 " (0x%08" PRIx64 ") in handle.",
1623
0
     function,
1624
0
     offset,
1625
0
     offset );
1626
1627
0
    write_count = -1;
1628
0
  }
1629
0
  else
1630
0
  {
1631
0
    write_count = libbfio_internal_handle_write_buffer(
1632
0
                   internal_handle,
1633
0
                   buffer,
1634
0
                   size,
1635
0
                   error );
1636
1637
0
    if( write_count < 0 )
1638
0
    {
1639
0
      libcerror_error_set(
1640
0
       error,
1641
0
       LIBCERROR_ERROR_DOMAIN_IO,
1642
0
       LIBCERROR_IO_ERROR_WRITE_FAILED,
1643
0
       "%s: unable to write to handle.",
1644
0
       function );
1645
1646
0
      write_count = -1;
1647
0
    }
1648
0
  }
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
0
  return( write_count );
1665
0
}
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
4.44k
{
1677
4.44k
  static char *function = "libbfio_internal_handle_seek_offset";
1678
1679
4.44k
  if( internal_handle == NULL )
1680
0
  {
1681
0
    libcerror_error_set(
1682
0
     error,
1683
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1684
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1685
0
     "%s: invalid handle.",
1686
0
     function );
1687
1688
0
    return( -1 );
1689
0
  }
1690
4.44k
  if( internal_handle->seek_offset == NULL )
1691
0
  {
1692
0
    libcerror_error_set(
1693
0
     error,
1694
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1695
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1696
0
     "%s: invalid handle - missing seek offset function.",
1697
0
     function );
1698
1699
0
    return( -1 );
1700
0
  }
1701
4.44k
  if( ( whence != SEEK_CUR )
1702
4.44k
   && ( whence != SEEK_END )
1703
4.44k
   && ( whence != SEEK_SET ) )
1704
0
  {
1705
0
    libcerror_error_set(
1706
0
     error,
1707
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1708
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1709
0
     "%s: unsupported whence.",
1710
0
     function );
1711
1712
0
    return( -1 );
1713
0
  }
1714
4.44k
  offset = internal_handle->seek_offset(
1715
4.44k
            internal_handle->io_handle,
1716
4.44k
            offset,
1717
4.44k
            whence,
1718
4.44k
            error );
1719
1720
4.44k
  if( offset == -1 )
1721
60
  {
1722
60
    libcerror_error_set(
1723
60
     error,
1724
60
     LIBCERROR_ERROR_DOMAIN_IO,
1725
60
     LIBCERROR_IO_ERROR_SEEK_FAILED,
1726
60
     "%s: unable to seek offset in handle.",
1727
60
     function );
1728
1729
60
    return( -1 );
1730
60
  }
1731
4.38k
  internal_handle->current_offset = offset;
1732
1733
4.38k
  return( offset );
1734
4.44k
}
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
0
{
1745
0
  libbfio_internal_handle_t *internal_handle = NULL;
1746
0
  static char *function                      = "libbfio_handle_seek_offset";
1747
1748
0
  if( handle == NULL )
1749
0
  {
1750
0
    libcerror_error_set(
1751
0
     error,
1752
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1753
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1754
0
     "%s: invalid handle.",
1755
0
     function );
1756
1757
0
    return( -1 );
1758
0
  }
1759
0
  internal_handle = (libbfio_internal_handle_t *) handle;
1760
1761
0
  if( internal_handle->io_handle == NULL )
1762
0
  {
1763
0
    libcerror_error_set(
1764
0
     error,
1765
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1766
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1767
0
     "%s: invalid handle - missing IO handle.",
1768
0
     function );
1769
1770
0
    return( -1 );
1771
0
  }
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
0
  offset = libbfio_internal_handle_seek_offset(
1788
0
            internal_handle,
1789
0
            offset,
1790
0
            whence,
1791
0
            error );
1792
1793
0
  if( offset == -1 )
1794
0
  {
1795
0
    libcerror_error_set(
1796
0
     error,
1797
0
     LIBCERROR_ERROR_DOMAIN_IO,
1798
0
     LIBCERROR_IO_ERROR_SEEK_FAILED,
1799
0
     "%s: unable to seek offset in handle.",
1800
0
     function );
1801
1802
0
    offset = -1;
1803
0
  }
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
0
  return( offset );
1820
0
}
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
0
{
1829
0
  libbfio_internal_handle_t *internal_handle = NULL;
1830
0
  static char *function                      = "libbfio_handle_exists";
1831
0
  int result                                 = 0;
1832
1833
0
  if( handle == NULL )
1834
0
  {
1835
0
    libcerror_error_set(
1836
0
     error,
1837
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1838
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1839
0
     "%s: invalid handle.",
1840
0
     function );
1841
1842
0
    return( -1 );
1843
0
  }
1844
0
  internal_handle = (libbfio_internal_handle_t *) handle;
1845
1846
0
  if( internal_handle->io_handle == NULL )
1847
0
  {
1848
0
    libcerror_error_set(
1849
0
     error,
1850
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1851
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1852
0
     "%s: invalid handle - missing IO handle.",
1853
0
     function );
1854
1855
0
    return( -1 );
1856
0
  }
1857
0
  if( internal_handle->exists == NULL )
1858
0
  {
1859
0
    libcerror_error_set(
1860
0
     error,
1861
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1862
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1863
0
     "%s: invalid handle - missing exists function.",
1864
0
     function );
1865
1866
0
    return( -1 );
1867
0
  }
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
0
  result = internal_handle->exists(
1884
0
            internal_handle->io_handle,
1885
0
            error );
1886
1887
0
  if( result == -1 )
1888
0
  {
1889
0
    libcerror_error_set(
1890
0
     error,
1891
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1892
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1893
0
     "%s: unable to determine if handle exists.",
1894
0
     function );
1895
1896
0
    goto on_error;
1897
0
  }
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
0
  return( result );
1914
1915
0
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
0
  return( -1 );
1922
0
}
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
1.03k
{
1931
1.03k
  libbfio_internal_handle_t *internal_handle = NULL;
1932
1.03k
  static char *function                      = "libbfio_handle_is_open";
1933
1.03k
  int result                                 = 0;
1934
1935
1.03k
  if( handle == NULL )
1936
0
  {
1937
0
    libcerror_error_set(
1938
0
     error,
1939
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1940
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1941
0
     "%s: invalid handle.",
1942
0
     function );
1943
1944
0
    return( -1 );
1945
0
  }
1946
1.03k
  internal_handle = (libbfio_internal_handle_t *) handle;
1947
1948
1.03k
  if( internal_handle->io_handle == NULL )
1949
0
  {
1950
0
    libcerror_error_set(
1951
0
     error,
1952
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1953
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1954
0
     "%s: invalid handle - missing IO handle.",
1955
0
     function );
1956
1957
0
    return( -1 );
1958
0
  }
1959
1.03k
  if( internal_handle->is_open == NULL )
1960
0
  {
1961
0
    libcerror_error_set(
1962
0
     error,
1963
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1964
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1965
0
     "%s: invalid handle - missing is open function.",
1966
0
     function );
1967
1968
0
    return( -1 );
1969
0
  }
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
1.03k
  result = internal_handle->is_open(
1986
1.03k
            internal_handle->io_handle,
1987
1.03k
            error );
1988
1989
1.03k
  if( result == -1 )
1990
0
  {
1991
0
    libcerror_error_set(
1992
0
     error,
1993
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1994
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1995
0
     "%s: unable to determine if handle is open.",
1996
0
     function );
1997
1998
0
    goto on_error;
1999
0
  }
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
1.03k
  return( result );
2016
2017
0
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
0
  return( -1 );
2024
1.03k
}
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
0
{
2034
0
  libbfio_internal_handle_t *internal_handle = NULL;
2035
0
  static char *function                      = "libbfio_handle_get_io_handle";
2036
2037
0
  if( handle == NULL )
2038
0
  {
2039
0
    libcerror_error_set(
2040
0
     error,
2041
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2042
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2043
0
     "%s: invalid handle.",
2044
0
     function );
2045
2046
0
    return( -1 );
2047
0
  }
2048
0
  internal_handle = (libbfio_internal_handle_t *) handle;
2049
2050
0
  if( io_handle == NULL )
2051
0
  {
2052
0
    libcerror_error_set(
2053
0
     error,
2054
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2055
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2056
0
     "%s: invalid IO handle.",
2057
0
     function );
2058
2059
0
    return( -1 );
2060
0
  }
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
0
  *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
0
  return( 1 );
2094
0
}
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
0
{
2104
0
  libbfio_internal_handle_t *internal_handle = NULL;
2105
0
  static char *function                      = "libbfio_handle_get_access_flags";
2106
2107
0
  if( handle == NULL )
2108
0
  {
2109
0
    libcerror_error_set(
2110
0
     error,
2111
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2112
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2113
0
     "%s: invalid handle.",
2114
0
     function );
2115
2116
0
    return( -1 );
2117
0
  }
2118
0
  internal_handle = (libbfio_internal_handle_t *) handle;
2119
2120
0
  if( access_flags == NULL )
2121
0
  {
2122
0
    libcerror_error_set(
2123
0
     error,
2124
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2125
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2126
0
     "%s: invalid access flags.",
2127
0
     function );
2128
2129
0
    return( -1 );
2130
0
  }
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
0
  *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
0
  return( 1 );
2164
0
}
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
0
{
2174
0
  libbfio_internal_handle_t *internal_handle = NULL;
2175
0
  static char *function                      = "libbfio_handle_set_access_flags";
2176
2177
0
  if( handle == NULL )
2178
0
  {
2179
0
    libcerror_error_set(
2180
0
     error,
2181
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2182
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2183
0
     "%s: invalid handle.",
2184
0
     function );
2185
2186
0
    return( -1 );
2187
0
  }
2188
0
  internal_handle = (libbfio_internal_handle_t *) handle;
2189
2190
0
  if( ( ( access_flags & LIBBFIO_ACCESS_FLAG_READ ) == 0 )
2191
0
   && ( ( access_flags & LIBBFIO_ACCESS_FLAG_WRITE ) == 0 ) )
2192
0
  {
2193
0
    libcerror_error_set(
2194
0
     error,
2195
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2196
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2197
0
     "%s: unsupported access flags.",
2198
0
     function );
2199
2200
0
    return( -1 );
2201
0
  }
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
0
  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
0
  return( 1 );
2235
0
}
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
0
{
2245
0
  libbfio_internal_handle_t *internal_handle = NULL;
2246
0
  static char *function                      = "libbfio_handle_get_offset";
2247
2248
0
  if( handle == NULL )
2249
0
  {
2250
0
    libcerror_error_set(
2251
0
     error,
2252
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2253
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2254
0
     "%s: invalid handle.",
2255
0
     function );
2256
2257
0
    return( -1 );
2258
0
  }
2259
0
  internal_handle = (libbfio_internal_handle_t *) handle;
2260
2261
0
  if( offset == NULL )
2262
0
  {
2263
0
    libcerror_error_set(
2264
0
     error,
2265
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2266
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2267
0
     "%s: invalid offset.",
2268
0
     function );
2269
2270
0
    return( -1 );
2271
0
  }
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
0
  *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
0
  return( 1 );
2305
0
}
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
1.03k
{
2315
1.03k
  libbfio_internal_handle_t *internal_handle = NULL;
2316
1.03k
  static char *function                      = "libbfio_handle_get_size";
2317
2318
1.03k
  if( handle == NULL )
2319
0
  {
2320
0
    libcerror_error_set(
2321
0
     error,
2322
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2323
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2324
0
     "%s: invalid handle.",
2325
0
     function );
2326
2327
0
    return( -1 );
2328
0
  }
2329
1.03k
  internal_handle = (libbfio_internal_handle_t *) handle;
2330
2331
1.03k
  if( internal_handle->io_handle == NULL )
2332
0
  {
2333
0
    libcerror_error_set(
2334
0
     error,
2335
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2336
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2337
0
     "%s: invalid handle - missing IO handle.",
2338
0
     function );
2339
2340
0
    return( -1 );
2341
0
  }
2342
1.03k
  if( size == NULL )
2343
0
  {
2344
0
    libcerror_error_set(
2345
0
     error,
2346
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2347
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2348
0
     "%s: invalid size",
2349
0
     function );
2350
2351
0
    return( -1 );
2352
0
  }
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
1.03k
  if( internal_handle->size_set == 0 )
2369
1.03k
  {
2370
1.03k
    if( internal_handle->get_size == NULL )
2371
0
    {
2372
0
      libcerror_error_set(
2373
0
       error,
2374
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2375
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2376
0
       "%s: invalid handle - missing get size function.",
2377
0
       function );
2378
2379
0
      goto on_error;
2380
0
    }
2381
1.03k
    if( internal_handle->get_size(
2382
1.03k
         internal_handle->io_handle,
2383
1.03k
         &( internal_handle->size ),
2384
1.03k
         error ) != 1 )
2385
0
    {
2386
0
      libcerror_error_set(
2387
0
       error,
2388
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2389
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2390
0
       "%s: unable to retrieve size.",
2391
0
       function );
2392
2393
0
      goto on_error;
2394
0
    }
2395
1.03k
    internal_handle->size_set = 1;
2396
1.03k
  }
2397
1.03k
  *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
1.03k
  return( 1 );
2415
2416
0
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
0
  return( -1 );
2423
1.03k
}
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
0
{
2435
0
  libbfio_internal_handle_t *internal_handle = NULL;
2436
0
  static char *function                      = "libbfio_handle_set_open_on_demand";
2437
2438
0
  if( handle == NULL )
2439
0
  {
2440
0
    libcerror_error_set(
2441
0
     error,
2442
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2443
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2444
0
     "%s: invalid handle.",
2445
0
     function );
2446
2447
0
    return( -1 );
2448
0
  }
2449
0
  internal_handle = (libbfio_internal_handle_t *) handle;
2450
2451
0
  if( ( ( internal_handle->access_flags & LIBBFIO_ACCESS_FLAG_WRITE ) != 0 )
2452
0
   && ( open_on_demand != 0 ) )
2453
0
  {
2454
0
    libcerror_error_set(
2455
0
     error,
2456
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2457
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2458
0
     "%s: open on demand cannot be used in combination with write access.",
2459
0
     function );
2460
2461
0
    return( -1 );
2462
0
  }
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
0
  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
0
  return( 1 );
2496
0
}
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
0
{
2507
0
  libbfio_internal_handle_t *internal_handle = NULL;
2508
0
  static char *function                      = "libbfio_handle_set_track_offsets_read";
2509
2510
0
  if( handle == NULL )
2511
0
  {
2512
0
    libcerror_error_set(
2513
0
     error,
2514
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2515
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2516
0
     "%s: invalid handle.",
2517
0
     function );
2518
2519
0
    return( -1 );
2520
0
  }
2521
0
  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
0
  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
0
  return( 1 );
2556
0
}
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
0
{
2566
0
  libbfio_internal_handle_t *internal_handle = NULL;
2567
0
  static char *function                      = "libbfio_handle_get_number_of_offsets_read";
2568
0
  int result                                 = 1;
2569
2570
0
  if( handle == NULL )
2571
0
  {
2572
0
    libcerror_error_set(
2573
0
     error,
2574
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2575
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2576
0
     "%s: invalid handle.",
2577
0
     function );
2578
2579
0
    return( -1 );
2580
0
  }
2581
0
  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
0
  if( libcdata_range_list_get_number_of_elements(
2599
0
       internal_handle->offsets_read,
2600
0
       number_of_read_offsets,
2601
0
       error ) != 1 )
2602
0
  {
2603
0
    libcerror_error_set(
2604
0
     error,
2605
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2606
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2607
0
     "%s: unable to retrieve number of read offsets.",
2608
0
     function );
2609
2610
0
    result = -1;
2611
0
  }
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
0
  return( result );
2628
0
}
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
0
{
2640
0
  libbfio_internal_handle_t *internal_handle = NULL;
2641
0
  intptr_t *value                            = NULL;
2642
0
  static char *function                      = "libbfio_handle_get_offset_read";
2643
0
  int result                                 = 1;
2644
2645
0
  if( handle == NULL )
2646
0
  {
2647
0
    libcerror_error_set(
2648
0
     error,
2649
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2650
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2651
0
     "%s: invalid handle.",
2652
0
     function );
2653
2654
0
    return( -1 );
2655
0
  }
2656
0
  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
0
  if( libcdata_range_list_get_range_by_index(
2674
0
       internal_handle->offsets_read,
2675
0
       index,
2676
0
       (uint64_t *) offset,
2677
0
       (uint64_t *) size,
2678
0
       &value,
2679
0
       error ) != 1 )
2680
0
  {
2681
0
    libcerror_error_set(
2682
0
     error,
2683
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2684
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2685
0
     "%s: unable to retrieve read offset range: %d.",
2686
0
     function,
2687
0
     index );
2688
2689
0
    result = -1;
2690
0
  }
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
0
  return( result );
2707
0
}
2708