Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libagdb/libagdb/libagdb_file.c
Line
Count
Source
1
/*
2
 * File functions
3
 *
4
 * Copyright (C) 2014-2026, 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 <types.h>
26
#include <wide_string.h>
27
28
#include "libagdb_compressed_block.h"
29
#include "libagdb_compressed_blocks_stream.h"
30
#include "libagdb_compressed_file_header.h"
31
#include "libagdb_debug.h"
32
#include "libagdb_definitions.h"
33
#include "libagdb_io_handle.h"
34
#include "libagdb_file.h"
35
#include "libagdb_file_header.h"
36
#include "libagdb_libbfio.h"
37
#include "libagdb_libcdata.h"
38
#include "libagdb_libcerror.h"
39
#include "libagdb_libcnotify.h"
40
#include "libagdb_libfcache.h"
41
#include "libagdb_libfdata.h"
42
#include "libagdb_source_information.h"
43
#include "libagdb_volume_information.h"
44
45
/* Creates a file
46
 * Make sure the value file is referencing, is set to NULL
47
 * Returns 1 if successful or -1 on error
48
 */
49
int libagdb_file_initialize(
50
     libagdb_file_t **file,
51
     libcerror_error_t **error )
52
2.73k
{
53
2.73k
  libagdb_internal_file_t *internal_file = NULL;
54
2.73k
  static char *function                  = "libagdb_file_initialize";
55
56
2.73k
  if( file == NULL )
57
0
  {
58
0
    libcerror_error_set(
59
0
     error,
60
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
61
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
62
0
     "%s: invalid file.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
2.73k
  if( *file != NULL )
68
0
  {
69
0
    libcerror_error_set(
70
0
     error,
71
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
72
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
73
0
     "%s: invalid file value already set.",
74
0
     function );
75
76
0
    return( -1 );
77
0
  }
78
2.73k
  internal_file = memory_allocate_structure(
79
2.73k
                   libagdb_internal_file_t );
80
81
2.73k
  if( internal_file == NULL )
82
0
  {
83
0
    libcerror_error_set(
84
0
     error,
85
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
86
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
87
0
     "%s: unable to create file.",
88
0
     function );
89
90
0
    goto on_error;
91
0
  }
92
2.73k
  if( memory_set(
93
2.73k
       internal_file,
94
2.73k
       0,
95
2.73k
       sizeof( libagdb_internal_file_t ) ) == NULL )
96
0
  {
97
0
    libcerror_error_set(
98
0
     error,
99
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
100
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
101
0
     "%s: unable to clear file.",
102
0
     function );
103
104
0
    memory_free(
105
0
     internal_file );
106
107
0
    return( -1 );
108
0
  }
109
2.73k
  if( libcdata_array_initialize(
110
2.73k
       &( internal_file->volumes_array ),
111
2.73k
       0,
112
2.73k
       error ) != 1 )
113
0
  {
114
0
    libcerror_error_set(
115
0
     error,
116
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
117
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
118
0
     "%s: unable to create volumes array.",
119
0
     function );
120
121
0
    goto on_error;
122
0
  }
123
2.73k
  if( libcdata_array_initialize(
124
2.73k
       &( internal_file->sources_array ),
125
2.73k
       0,
126
2.73k
       error ) != 1 )
127
0
  {
128
0
    libcerror_error_set(
129
0
     error,
130
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
131
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
132
0
     "%s: unable to create sources array.",
133
0
     function );
134
135
0
    goto on_error;
136
0
  }
137
2.73k
  if( libagdb_io_handle_initialize(
138
2.73k
       &( internal_file->io_handle ),
139
2.73k
       error ) != 1 )
140
0
  {
141
0
    libcerror_error_set(
142
0
     error,
143
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
144
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
145
0
     "%s: unable to create IO handle.",
146
0
     function );
147
148
0
    goto on_error;
149
0
  }
150
2.73k
  *file = (libagdb_file_t *) internal_file;
151
152
2.73k
  return( 1 );
153
154
0
on_error:
155
0
  if( internal_file != NULL )
156
0
  {
157
0
    if( internal_file->sources_array != NULL )
158
0
    {
159
0
      libcdata_array_free(
160
0
       &( internal_file->sources_array ),
161
0
       NULL,
162
0
       NULL );
163
0
    }
164
0
    if( internal_file->volumes_array != NULL )
165
0
    {
166
0
      libcdata_array_free(
167
0
       &( internal_file->volumes_array ),
168
0
       NULL,
169
0
       NULL );
170
0
    }
171
0
    memory_free(
172
0
     internal_file );
173
0
  }
174
0
  return( -1 );
175
2.73k
}
176
177
/* Frees a file
178
 * Returns 1 if successful or -1 on error
179
 */
180
int libagdb_file_free(
181
     libagdb_file_t **file,
182
     libcerror_error_t **error )
183
2.73k
{
184
2.73k
  libagdb_internal_file_t *internal_file = NULL;
185
2.73k
  static char *function                  = "libagdb_file_free";
186
2.73k
  int result                             = 1;
187
188
2.73k
  if( file == NULL )
189
0
  {
190
0
    libcerror_error_set(
191
0
     error,
192
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
193
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
194
0
     "%s: invalid file.",
195
0
     function );
196
197
0
    return( -1 );
198
0
  }
199
2.73k
  if( *file != NULL )
200
2.73k
  {
201
2.73k
    internal_file = (libagdb_internal_file_t *) *file;
202
203
2.73k
    if( internal_file->file_io_handle != NULL )
204
0
    {
205
0
      if( libagdb_file_close(
206
0
           *file,
207
0
           error ) != 0 )
208
0
      {
209
0
        libcerror_error_set(
210
0
         error,
211
0
         LIBCERROR_ERROR_DOMAIN_IO,
212
0
         LIBCERROR_IO_ERROR_CLOSE_FAILED,
213
0
         "%s: unable to close file.",
214
0
         function );
215
216
0
        result = -1;
217
0
      }
218
0
    }
219
2.73k
    *file = NULL;
220
221
2.73k
    if( libcdata_array_free(
222
2.73k
         &( internal_file->volumes_array ),
223
2.73k
         (int (*)(intptr_t **, libcerror_error_t **)) &libagdb_internal_volume_information_free,
224
2.73k
         error ) != 1 )
225
0
    {
226
0
      libcerror_error_set(
227
0
       error,
228
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
229
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
230
0
       "%s: unable to free volumes array.",
231
0
       function );
232
233
0
      result = -1;
234
0
    }
235
2.73k
    if( libcdata_array_free(
236
2.73k
         &( internal_file->sources_array ),
237
2.73k
         (int (*)(intptr_t **, libcerror_error_t **)) &libagdb_internal_source_information_free,
238
2.73k
         error ) != 1 )
239
0
    {
240
0
      libcerror_error_set(
241
0
       error,
242
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
243
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
244
0
       "%s: unable to free sources array.",
245
0
       function );
246
247
0
      result = -1;
248
0
    }
249
2.73k
    if( libagdb_io_handle_free(
250
2.73k
         &( internal_file->io_handle ),
251
2.73k
         error ) != 1 )
252
0
    {
253
0
      libcerror_error_set(
254
0
       error,
255
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
256
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
257
0
       "%s: unable to free IO handle.",
258
0
       function );
259
260
0
      result = -1;
261
0
    }
262
2.73k
    memory_free(
263
2.73k
     internal_file );
264
2.73k
  }
265
2.73k
  return( result );
266
2.73k
}
267
268
/* Signals the file to abort its current activity
269
 * Returns 1 if successful or -1 on error
270
 */
271
int libagdb_file_signal_abort(
272
     libagdb_file_t *file,
273
     libcerror_error_t **error )
274
0
{
275
0
  libagdb_internal_file_t *internal_file = NULL;
276
0
  static char *function                  = "libagdb_file_signal_abort";
277
278
0
  if( file == NULL )
279
0
  {
280
0
    libcerror_error_set(
281
0
     error,
282
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
283
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
284
0
     "%s: invalid file.",
285
0
     function );
286
287
0
    return( -1 );
288
0
  }
289
0
  internal_file = (libagdb_internal_file_t *) file;
290
291
0
  if( internal_file->io_handle == NULL )
292
0
  {
293
0
    libcerror_error_set(
294
0
     error,
295
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
296
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
297
0
     "%s: invalid file - missing IO handle.",
298
0
     function );
299
300
0
    return( -1 );
301
0
  }
302
0
  internal_file->io_handle->abort = 1;
303
304
0
  return( 1 );
305
0
}
306
307
/* Opens a file
308
 * Returns 1 if successful or -1 on error
309
 */
310
int libagdb_file_open(
311
     libagdb_file_t *file,
312
     const char *filename,
313
     int access_flags,
314
     libcerror_error_t **error )
315
0
{
316
0
  libbfio_handle_t *file_io_handle       = NULL;
317
0
  libagdb_internal_file_t *internal_file = NULL;
318
0
  static char *function                  = "libagdb_file_open";
319
0
  size_t filename_length                 = 0;
320
321
0
  if( file == NULL )
322
0
  {
323
0
    libcerror_error_set(
324
0
     error,
325
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
326
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
327
0
     "%s: invalid file.",
328
0
     function );
329
330
0
    return( -1 );
331
0
  }
332
0
  internal_file = (libagdb_internal_file_t *) file;
333
334
0
  if( filename == NULL )
335
0
  {
336
0
    libcerror_error_set(
337
0
     error,
338
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
339
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
340
0
     "%s: invalid filename.",
341
0
     function );
342
343
0
    return( -1 );
344
0
  }
345
0
  if( ( ( access_flags & LIBAGDB_ACCESS_FLAG_READ ) == 0 )
346
0
   && ( ( access_flags & LIBAGDB_ACCESS_FLAG_WRITE ) == 0 ) )
347
0
  {
348
0
    libcerror_error_set(
349
0
     error,
350
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
351
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
352
0
     "%s: unsupported access flags.",
353
0
     function );
354
355
0
    return( -1 );
356
0
  }
357
0
  if( ( access_flags & LIBAGDB_ACCESS_FLAG_WRITE ) != 0 )
358
0
  {
359
0
    libcerror_error_set(
360
0
     error,
361
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
362
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
363
0
     "%s: write access currently not supported.",
364
0
     function );
365
366
0
    return( -1 );
367
0
  }
368
0
  if( libbfio_file_initialize(
369
0
       &file_io_handle,
370
0
       error ) != 1 )
371
0
  {
372
0
    libcerror_error_set(
373
0
     error,
374
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
375
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
376
0
     "%s: unable to create file IO handle.",
377
0
     function );
378
379
0
    goto on_error;
380
0
  }
381
#if defined( HAVE_DEBUG_OUTPUT )
382
  if( libbfio_handle_set_track_offsets_read(
383
       file_io_handle,
384
       1,
385
       error ) != 1 )
386
  {
387
                libcerror_error_set(
388
                 error,
389
                 LIBCERROR_ERROR_DOMAIN_RUNTIME,
390
                 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
391
                 "%s: unable to set track offsets read in file IO handle.",
392
                 function );
393
394
    goto on_error;
395
  }
396
#endif
397
0
  filename_length = narrow_string_length(
398
0
                     filename );
399
400
0
  if( libbfio_file_set_name(
401
0
       file_io_handle,
402
0
       filename,
403
0
       filename_length + 1,
404
0
       error ) != 1 )
405
0
  {
406
0
                libcerror_error_set(
407
0
                 error,
408
0
                 LIBCERROR_ERROR_DOMAIN_RUNTIME,
409
0
                 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
410
0
                 "%s: unable to set filename in file IO handle.",
411
0
                 function );
412
413
0
    goto on_error;
414
0
  }
415
0
  if( libagdb_file_open_file_io_handle(
416
0
       file,
417
0
       file_io_handle,
418
0
       access_flags,
419
0
       error ) != 1 )
420
0
  {
421
0
    libcerror_error_set(
422
0
     error,
423
0
     LIBCERROR_ERROR_DOMAIN_IO,
424
0
     LIBCERROR_IO_ERROR_OPEN_FAILED,
425
0
     "%s: unable to open file: %s.",
426
0
     function,
427
0
     filename );
428
429
0
    goto on_error;
430
0
  }
431
0
  internal_file->file_io_handle_created_in_library = 1;
432
433
0
  return( 1 );
434
435
0
on_error:
436
0
  if( file_io_handle != NULL )
437
0
  {
438
0
    libbfio_handle_free(
439
0
     &file_io_handle,
440
0
     NULL );
441
0
  }
442
0
  return( -1 );
443
0
}
444
445
#if defined( HAVE_WIDE_CHARACTER_TYPE )
446
447
/* Opens a file
448
 * Returns 1 if successful or -1 on error
449
 */
450
int libagdb_file_open_wide(
451
     libagdb_file_t *file,
452
     const wchar_t *filename,
453
     int access_flags,
454
     libcerror_error_t **error )
455
{
456
  libbfio_handle_t *file_io_handle       = NULL;
457
  libagdb_internal_file_t *internal_file = NULL;
458
  static char *function                  = "libagdb_file_open_wide";
459
  size_t filename_length                 = 0;
460
461
  if( file == NULL )
462
  {
463
    libcerror_error_set(
464
     error,
465
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
466
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
467
     "%s: invalid file.",
468
     function );
469
470
    return( -1 );
471
  }
472
  internal_file = (libagdb_internal_file_t *) file;
473
474
  if( filename == NULL )
475
  {
476
    libcerror_error_set(
477
     error,
478
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
479
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
480
     "%s: invalid filename.",
481
     function );
482
483
    return( -1 );
484
  }
485
  if( ( ( access_flags & LIBAGDB_ACCESS_FLAG_READ ) == 0 )
486
   && ( ( access_flags & LIBAGDB_ACCESS_FLAG_WRITE ) == 0 ) )
487
  {
488
    libcerror_error_set(
489
     error,
490
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
491
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
492
     "%s: unsupported access flags.",
493
     function );
494
495
    return( -1 );
496
  }
497
  if( ( access_flags & LIBAGDB_ACCESS_FLAG_WRITE ) != 0 )
498
  {
499
    libcerror_error_set(
500
     error,
501
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
502
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
503
     "%s: write access currently not supported.",
504
     function );
505
506
    return( -1 );
507
  }
508
  if( libbfio_file_initialize(
509
       &file_io_handle,
510
       error ) != 1 )
511
  {
512
    libcerror_error_set(
513
     error,
514
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
515
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
516
     "%s: unable to create file IO handle.",
517
     function );
518
519
    goto on_error;
520
  }
521
#if defined( HAVE_DEBUG_OUTPUT )
522
  if( libbfio_handle_set_track_offsets_read(
523
       file_io_handle,
524
       1,
525
       error ) != 1 )
526
  {
527
                libcerror_error_set(
528
                 error,
529
                 LIBCERROR_ERROR_DOMAIN_RUNTIME,
530
                 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
531
                 "%s: unable to set track offsets read in file IO handle.",
532
                 function );
533
534
    goto on_error;
535
  }
536
#endif
537
  filename_length = wide_string_length(
538
                     filename );
539
540
  if( libbfio_file_set_name_wide(
541
       file_io_handle,
542
       filename,
543
       filename_length + 1,
544
       error ) != 1 )
545
  {
546
                libcerror_error_set(
547
                 error,
548
                 LIBCERROR_ERROR_DOMAIN_RUNTIME,
549
                 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
550
                 "%s: unable to set filename in file IO handle.",
551
                 function );
552
553
    goto on_error;
554
  }
555
  if( libagdb_file_open_file_io_handle(
556
       file,
557
       file_io_handle,
558
       access_flags,
559
       error ) != 1 )
560
  {
561
    libcerror_error_set(
562
     error,
563
     LIBCERROR_ERROR_DOMAIN_IO,
564
     LIBCERROR_IO_ERROR_OPEN_FAILED,
565
     "%s: unable to open file: %ls.",
566
     function,
567
     filename );
568
569
    goto on_error;
570
  }
571
  internal_file->file_io_handle_created_in_library = 1;
572
573
  return( 1 );
574
575
on_error:
576
  if( file_io_handle != NULL )
577
  {
578
    libbfio_handle_free(
579
     &file_io_handle,
580
     NULL );
581
  }
582
  return( -1 );
583
}
584
585
#endif
586
587
/* Opens a file using a Basic File IO (bfio) handle
588
 * Returns 1 if successful or -1 on error
589
 */
590
int libagdb_file_open_file_io_handle(
591
     libagdb_file_t *file,
592
     libbfio_handle_t *file_io_handle,
593
     int access_flags,
594
     libcerror_error_t **error )
595
2.73k
{
596
2.73k
  libagdb_internal_file_t *internal_file = NULL;
597
2.73k
  static char *function                  = "libagdb_file_open_file_io_handle";
598
2.73k
  int bfio_access_flags                  = 0;
599
2.73k
  int file_io_handle_is_open             = 0;
600
601
2.73k
  if( file == NULL )
602
0
  {
603
0
    libcerror_error_set(
604
0
     error,
605
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
606
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
607
0
     "%s: invalid file.",
608
0
     function );
609
610
0
    return( -1 );
611
0
  }
612
2.73k
  internal_file = (libagdb_internal_file_t *) file;
613
614
2.73k
  if( internal_file->file_io_handle != NULL )
615
0
  {
616
0
    libcerror_error_set(
617
0
     error,
618
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
619
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
620
0
     "%s: invalid file - file IO handle already set.",
621
0
     function );
622
623
0
    return( -1 );
624
0
  }
625
2.73k
  if( file_io_handle == NULL )
626
0
  {
627
0
    libcerror_error_set(
628
0
     error,
629
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
630
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
631
0
     "%s: invalid file IO handle.",
632
0
     function );
633
634
0
    return( -1 );
635
0
  }
636
2.73k
  if( ( ( access_flags & LIBAGDB_ACCESS_FLAG_READ ) == 0 )
637
0
   && ( ( access_flags & LIBAGDB_ACCESS_FLAG_WRITE ) == 0 ) )
638
0
  {
639
0
    libcerror_error_set(
640
0
     error,
641
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
642
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
643
0
     "%s: unsupported access flags.",
644
0
     function );
645
646
0
    return( -1 );
647
0
  }
648
2.73k
  if( ( access_flags & LIBAGDB_ACCESS_FLAG_WRITE ) != 0 )
649
0
  {
650
0
    libcerror_error_set(
651
0
     error,
652
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
653
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
654
0
     "%s: write access currently not supported.",
655
0
     function );
656
657
0
    return( -1 );
658
0
  }
659
2.73k
  if( ( access_flags & LIBAGDB_ACCESS_FLAG_READ ) != 0 )
660
2.73k
  {
661
2.73k
    bfio_access_flags = LIBBFIO_ACCESS_FLAG_READ;
662
2.73k
  }
663
2.73k
  internal_file->file_io_handle = file_io_handle;
664
665
2.73k
  file_io_handle_is_open = libbfio_handle_is_open(
666
2.73k
                            internal_file->file_io_handle,
667
2.73k
                            error );
668
669
2.73k
  if( file_io_handle_is_open == -1 )
670
0
  {
671
0
    libcerror_error_set(
672
0
     error,
673
0
     LIBCERROR_ERROR_DOMAIN_IO,
674
0
     LIBCERROR_IO_ERROR_OPEN_FAILED,
675
0
     "%s: unable to determine if file IO handle is open.",
676
0
     function );
677
678
0
    goto on_error;
679
0
  }
680
2.73k
  else if( file_io_handle_is_open == 0 )
681
2.73k
  {
682
2.73k
    if( libbfio_handle_open(
683
2.73k
         internal_file->file_io_handle,
684
2.73k
         bfio_access_flags,
685
2.73k
         error ) != 1 )
686
0
    {
687
0
      libcerror_error_set(
688
0
       error,
689
0
       LIBCERROR_ERROR_DOMAIN_IO,
690
0
       LIBCERROR_IO_ERROR_OPEN_FAILED,
691
0
       "%s: unable to open file IO handle.",
692
0
       function );
693
694
0
      goto on_error;
695
0
    }
696
2.73k
    internal_file->file_io_handle_opened_in_library = 1;
697
2.73k
  }
698
2.73k
  if( libagdb_file_open_read(
699
2.73k
       internal_file,
700
2.73k
       file_io_handle,
701
2.73k
       error ) != 1 )
702
2.34k
  {
703
2.34k
    libcerror_error_set(
704
2.34k
     error,
705
2.34k
     LIBCERROR_ERROR_DOMAIN_IO,
706
2.34k
     LIBCERROR_IO_ERROR_READ_FAILED,
707
2.34k
     "%s: unable to read from file handle.",
708
2.34k
     function );
709
710
2.34k
    goto on_error;
711
2.34k
  }
712
389
  internal_file->file_io_handle = file_io_handle;
713
714
389
  return( 1 );
715
716
2.34k
on_error:
717
2.34k
  if( ( file_io_handle_is_open == 0 )
718
2.34k
   && ( internal_file->file_io_handle_opened_in_library != 0 ) )
719
2.34k
  {
720
2.34k
    libbfio_handle_close(
721
2.34k
     file_io_handle,
722
2.34k
     error );
723
724
2.34k
    internal_file->file_io_handle_opened_in_library = 0;
725
2.34k
  }
726
2.34k
  internal_file->file_io_handle = NULL;
727
728
2.34k
  return( -1 );
729
2.73k
}
730
731
/* Closes a file
732
 * Returns 0 if successful or -1 on error
733
 */
734
int libagdb_file_close(
735
     libagdb_file_t *file,
736
     libcerror_error_t **error )
737
389
{
738
389
  libagdb_internal_file_t *internal_file = NULL;
739
389
  static char *function                  = "libagdb_file_close";
740
389
  int result                             = 0;
741
742
389
  if( file == NULL )
743
0
  {
744
0
    libcerror_error_set(
745
0
     error,
746
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
747
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
748
0
     "%s: invalid file.",
749
0
     function );
750
751
0
    return( -1 );
752
0
  }
753
389
  internal_file = (libagdb_internal_file_t *) file;
754
755
389
  if( internal_file->file_io_handle == NULL )
756
0
  {
757
0
    libcerror_error_set(
758
0
     error,
759
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
760
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
761
0
     "%s: invalid file - missing file IO handle.",
762
0
     function );
763
764
0
    return( -1 );
765
0
  }
766
#if defined( HAVE_DEBUG_OUTPUT )
767
  if( libcnotify_verbose != 0 )
768
  {
769
    if( internal_file->file_io_handle_created_in_library != 0 )
770
    {
771
      if( libagdb_debug_print_read_offsets(
772
           internal_file->file_io_handle,
773
           error ) != 1 )
774
      {
775
        libcerror_error_set(
776
         error,
777
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
778
         LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
779
         "%s: unable to print the read offsets.",
780
         function );
781
782
        result = -1;
783
      }
784
    }
785
  }
786
#endif
787
389
  if( internal_file->file_io_handle_opened_in_library != 0 )
788
389
  {
789
389
    if( libbfio_handle_close(
790
389
         internal_file->file_io_handle,
791
389
         error ) != 0 )
792
0
    {
793
0
      libcerror_error_set(
794
0
       error,
795
0
       LIBCERROR_ERROR_DOMAIN_IO,
796
0
       LIBCERROR_IO_ERROR_CLOSE_FAILED,
797
0
       "%s: unable to close file IO handle.",
798
0
       function );
799
800
0
      result = -1;
801
0
    }
802
389
    internal_file->file_io_handle_opened_in_library = 0;
803
389
  }
804
389
  if( internal_file->file_io_handle_created_in_library != 0 )
805
0
  {
806
0
    if( libbfio_handle_free(
807
0
         &( internal_file->file_io_handle ),
808
0
         error ) != 1 )
809
0
    {
810
0
      libcerror_error_set(
811
0
       error,
812
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
813
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
814
0
       "%s: unable to free file IO handle.",
815
0
       function );
816
817
0
      result = -1;
818
0
    }
819
0
    internal_file->file_io_handle_created_in_library = 0;
820
0
  }
821
389
  internal_file->file_io_handle = NULL;
822
823
389
  if( libagdb_io_handle_clear(
824
389
       internal_file->io_handle,
825
389
       error ) != 1 )
826
0
  {
827
0
    libcerror_error_set(
828
0
     error,
829
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
830
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
831
0
     "%s: unable to clear IO handle.",
832
0
     function );
833
834
0
    result = -1;
835
0
  }
836
389
  if( internal_file->compressed_blocks_list != NULL )
837
16
  {
838
16
    if( libfdata_list_free(
839
16
         &( internal_file->compressed_blocks_list ),
840
16
         error ) != 1 )
841
0
    {
842
0
      libcerror_error_set(
843
0
       error,
844
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
845
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
846
0
       "%s: unable to free compressed blocks list.",
847
0
       function );
848
849
0
      result = -1;
850
0
    }
851
16
  }
852
389
  if( internal_file->compressed_blocks_cache != NULL )
853
16
  {
854
16
    if( libfcache_cache_free(
855
16
         &( internal_file->compressed_blocks_cache ),
856
16
         error ) != 1 )
857
0
    {
858
0
      libcerror_error_set(
859
0
       error,
860
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
861
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
862
0
       "%s: unable to free compressed blocks cache.",
863
0
       function );
864
865
0
      result = -1;
866
0
    }
867
16
  }
868
389
  if( internal_file->uncompressed_data_stream != NULL )
869
389
  {
870
389
    if( libfdata_stream_free(
871
389
         &( internal_file->uncompressed_data_stream ),
872
389
         error ) != 1 )
873
0
    {
874
0
      libcerror_error_set(
875
0
       error,
876
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
877
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
878
0
       "%s: unable to free uncompressed data strea,.",
879
0
       function );
880
881
0
      result = -1;
882
0
    }
883
389
  }
884
389
  if( libcdata_array_resize(
885
389
       internal_file->volumes_array,
886
389
       0,
887
389
       (int (*)(intptr_t **, libcerror_error_t **)) &libagdb_internal_volume_information_free,
888
389
       error ) != 1 )
889
0
  {
890
0
    libcerror_error_set(
891
0
     error,
892
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
893
0
     LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
894
0
     "%s: unable to resize volumes array.",
895
0
     function );
896
897
0
    result = -1;
898
0
  }
899
389
  if( libcdata_array_resize(
900
389
       internal_file->sources_array,
901
389
       0,
902
389
       (int (*)(intptr_t **, libcerror_error_t **)) &libagdb_internal_source_information_free,
903
389
       error ) != 1 )
904
0
  {
905
0
    libcerror_error_set(
906
0
     error,
907
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
908
0
     LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
909
0
     "%s: unable to resize sources array.",
910
0
     function );
911
912
0
    result = -1;
913
0
  }
914
389
  return( result );
915
389
}
916
917
/* Opens a file for reading
918
 * Returns 1 if successful or -1 on error
919
 */
920
int libagdb_file_open_read(
921
     libagdb_internal_file_t *internal_file,
922
     libbfio_handle_t *file_io_handle,
923
     libcerror_error_t **error )
924
2.73k
{
925
2.73k
  uint8_t alignment_padding_data[ 8 ];
926
927
2.73k
  libagdb_compressed_file_header_t *compressed_file_header = NULL;
928
2.73k
  libagdb_file_header_t *file_header                       = NULL;
929
2.73k
  libagdb_source_information_t *source_information         = NULL;
930
2.73k
  libagdb_volume_information_t *volume_information         = NULL;
931
2.73k
  static char *function                                    = "libagdb_file_open_read";
932
2.73k
  ssize64_t read_count                                     = 0;
933
2.73k
  size_t alignment_padding_size                            = 0;
934
2.73k
  off64_t file_offset                                      = 0;
935
2.73k
  uint32_t source_index                                    = 0;
936
2.73k
  uint32_t volume_index                                    = 0;
937
2.73k
  int entry_index                                          = 0;
938
2.73k
  int segment_index                                        = 0;
939
940
2.73k
  if( internal_file == NULL )
941
0
  {
942
0
    libcerror_error_set(
943
0
     error,
944
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
945
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
946
0
     "%s: invalid file.",
947
0
     function );
948
949
0
    return( -1 );
950
0
  }
951
2.73k
  if( internal_file->io_handle == NULL )
952
0
  {
953
0
    libcerror_error_set(
954
0
     error,
955
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
956
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
957
0
     "%s: invalid file - missing IO handle.",
958
0
     function );
959
960
0
    return( -1 );
961
0
  }
962
2.73k
  if( internal_file->compressed_blocks_list != NULL )
963
0
  {
964
0
    libcerror_error_set(
965
0
     error,
966
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
967
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
968
0
     "%s: invalid file - compressed blocks list value already set.",
969
0
     function );
970
971
0
    return( -1 );
972
0
  }
973
2.73k
  if( internal_file->compressed_blocks_cache != NULL )
974
0
  {
975
0
    libcerror_error_set(
976
0
     error,
977
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
978
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
979
0
     "%s: invalid file - compressed blocks cache value already set.",
980
0
     function );
981
982
0
    return( -1 );
983
0
  }
984
2.73k
  if( internal_file->io_handle->abort != 0 )
985
0
  {
986
0
    internal_file->io_handle->abort = 0;
987
0
  }
988
#if defined( HAVE_DEBUG_OUTPUT )
989
  if( libcnotify_verbose != 0 )
990
  {
991
    libcnotify_printf(
992
     "Reading file header:\n" );
993
  }
994
#endif
995
2.73k
  if( libagdb_compressed_file_header_initialize(
996
2.73k
       &compressed_file_header,
997
2.73k
       error ) != 1 )
998
0
  {
999
0
    libcerror_error_set(
1000
0
     error,
1001
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1002
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1003
0
     "%s: unable to create compressed file header.",
1004
0
     function );
1005
1006
0
    goto on_error;
1007
0
  }
1008
2.73k
  if( libagdb_compressed_file_header_read_file_io_handle(
1009
2.73k
       compressed_file_header,
1010
2.73k
       file_io_handle,
1011
2.73k
       error ) != 1 )
1012
62
  {
1013
62
    libcerror_error_set(
1014
62
     error,
1015
62
     LIBCERROR_ERROR_DOMAIN_IO,
1016
62
     LIBCERROR_IO_ERROR_READ_FAILED,
1017
62
     "%s: unable to read compressed file header.",
1018
62
     function );
1019
1020
62
    goto on_error;
1021
62
  }
1022
2.67k
  internal_file->io_handle->file_type               = compressed_file_header->file_type;
1023
2.67k
  internal_file->io_handle->file_size               = compressed_file_header->file_size;
1024
2.67k
  internal_file->io_handle->uncompressed_block_size = compressed_file_header->uncompressed_block_size;
1025
2.67k
  internal_file->io_handle->uncompressed_data_size  = compressed_file_header->uncompressed_data_size;
1026
1027
2.67k
  if( libagdb_compressed_file_header_free(
1028
2.67k
       &compressed_file_header,
1029
2.67k
       error ) != 1 )
1030
0
  {
1031
0
    libcerror_error_set(
1032
0
     error,
1033
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1034
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1035
0
     "%s: unable to free compressed file header.",
1036
0
     function );
1037
1038
0
    goto on_error;
1039
0
  }
1040
2.67k
  if( internal_file->io_handle->file_type != LIBAGDB_FILE_TYPE_UNCOMPRESSED )
1041
1.19k
  {
1042
1.19k
    if( libfdata_list_initialize(
1043
1.19k
         &( internal_file->compressed_blocks_list ),
1044
1.19k
         (intptr_t *) internal_file->io_handle,
1045
1.19k
         NULL,
1046
1.19k
         NULL,
1047
1.19k
         (int (*)(intptr_t *, intptr_t *, libfdata_list_element_t *, libfdata_cache_t *, int, off64_t, size64_t, uint32_t, uint8_t, libcerror_error_t **)) &libagdb_compressed_block_read_element_data,
1048
1.19k
         NULL,
1049
1.19k
         LIBFDATA_DATA_HANDLE_FLAG_NON_MANAGED,
1050
1.19k
         error ) != 1 )
1051
0
    {
1052
0
      libcerror_error_set(
1053
0
       error,
1054
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1055
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1056
0
       "%s: unable to create compressed blocks list.",
1057
0
       function );
1058
1059
0
      goto on_error;
1060
0
    }
1061
#if defined( HAVE_DEBUG_OUTPUT )
1062
    if( libcnotify_verbose != 0 )
1063
    {
1064
      libcnotify_printf(
1065
       "Reading compressed blocks:\n" );
1066
    }
1067
#endif
1068
1.19k
    if( libagdb_io_handle_read_compressed_blocks(
1069
1.19k
         internal_file->io_handle,
1070
1.19k
         file_io_handle,
1071
1.19k
         internal_file->compressed_blocks_list,
1072
1.19k
         error ) != 1 )
1073
49
    {
1074
49
      libcerror_error_set(
1075
49
       error,
1076
49
       LIBCERROR_ERROR_DOMAIN_IO,
1077
49
       LIBCERROR_IO_ERROR_READ_FAILED,
1078
49
       "%s: unable to read compressed blocks.",
1079
49
       function );
1080
1081
49
      goto on_error;
1082
49
    }
1083
1.14k
    if( libfcache_cache_initialize(
1084
1.14k
         &( internal_file->compressed_blocks_cache ),
1085
1.14k
         LIBAGDB_MAXIMUM_CACHE_ENTRIES_COMPRESSED_BLOCKS,
1086
1.14k
         error ) != 1 )
1087
0
    {
1088
0
      libcerror_error_set(
1089
0
       error,
1090
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1091
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1092
0
       "%s: unable to create compressed blocks cache.",
1093
0
       function );
1094
1095
0
      goto on_error;
1096
0
    }
1097
1.14k
    if( libagdb_compressed_blocks_stream_initialize(
1098
1.14k
         &( internal_file->uncompressed_data_stream ),
1099
1.14k
         internal_file->compressed_blocks_list,
1100
1.14k
         internal_file->compressed_blocks_cache,
1101
1.14k
         error ) != 1 )
1102
0
    {
1103
0
      libcerror_error_set(
1104
0
       error,
1105
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1106
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1107
0
       "%s: unable to create compressed blocks stream.",
1108
0
       function );
1109
1110
0
      goto on_error;
1111
0
    }
1112
1.14k
  }
1113
1.47k
  else
1114
1.47k
  {
1115
1.47k
    if( libfdata_stream_initialize(
1116
1.47k
         &( internal_file->uncompressed_data_stream ),
1117
1.47k
         NULL,
1118
1.47k
         NULL,
1119
1.47k
         NULL,
1120
1.47k
         NULL,
1121
1.47k
         (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libagdb_io_handle_read_segment_data,
1122
1.47k
         NULL,
1123
1.47k
         (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libagdb_io_handle_seek_segment_offset,
1124
1.47k
         0,
1125
1.47k
         error ) != 1 )
1126
0
    {
1127
0
      libcerror_error_set(
1128
0
       error,
1129
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1130
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1131
0
       "%s: unable to create uncompressed data stream.",
1132
0
       function );
1133
1134
0
      goto on_error;
1135
0
    }
1136
1.47k
    if( libfdata_stream_append_segment(
1137
1.47k
         internal_file->uncompressed_data_stream,
1138
1.47k
         &segment_index,
1139
1.47k
         0,
1140
1.47k
         0,
1141
1.47k
         (size64_t) internal_file->io_handle->uncompressed_data_size,
1142
1.47k
         0,
1143
1.47k
         error ) != 1 )
1144
0
    {
1145
0
      libcerror_error_set(
1146
0
       error,
1147
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1148
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1149
0
       "%s: unable to append uncompressed data stream segment: 0.",
1150
0
       function );
1151
1152
0
      goto on_error;
1153
0
    }
1154
1.47k
  }
1155
2.62k
  if( libagdb_file_header_initialize(
1156
2.62k
       &file_header,
1157
2.62k
       error ) != 1 )
1158
0
  {
1159
0
    libcerror_error_set(
1160
0
     error,
1161
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1162
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1163
0
     "%s: unable to create file header.",
1164
0
     function );
1165
1166
0
    goto on_error;
1167
0
  }
1168
2.62k
  if( libagdb_file_header_read_stream(
1169
2.62k
       file_header,
1170
2.62k
       internal_file->io_handle,
1171
2.62k
       internal_file->uncompressed_data_stream,
1172
2.62k
       file_io_handle,
1173
2.62k
       error ) != 1 )
1174
1.04k
  {
1175
1.04k
    libcerror_error_set(
1176
1.04k
     error,
1177
1.04k
     LIBCERROR_ERROR_DOMAIN_IO,
1178
1.04k
     LIBCERROR_IO_ERROR_READ_FAILED,
1179
1.04k
     "%s: unable to read file header.",
1180
1.04k
     function );
1181
1182
1.04k
    goto on_error;
1183
1.04k
  }
1184
1.57k
  internal_file->io_handle->file_header_signature = file_header->signature;
1185
1186
1.57k
  file_offset = (off64_t) file_header->size;
1187
1188
1.57k
  for( volume_index = 0;
1189
14.3k
       volume_index < file_header->number_of_volumes;
1190
12.7k
       volume_index++ )
1191
13.6k
  {
1192
13.6k
    alignment_padding_size = (size_t) ( file_offset % 8 );
1193
1194
13.6k
    if( alignment_padding_size != 0 )
1195
939
    {
1196
939
      alignment_padding_size = 8 - alignment_padding_size;
1197
#if defined( HAVE_DEBUG_OUTPUT )
1198
      if( libcnotify_verbose != 0 )
1199
      {
1200
        libcnotify_printf(
1201
         "%s: alignment padding size\t\t\t\t: %" PRIzd "\n",
1202
         function,
1203
         alignment_padding_size );
1204
      }
1205
#endif
1206
939
      read_count = libfdata_stream_read_buffer(
1207
939
                    internal_file->uncompressed_data_stream,
1208
939
                    (intptr_t *) internal_file->file_io_handle,
1209
939
                    alignment_padding_data,
1210
939
                    alignment_padding_size,
1211
939
                    0,
1212
939
                    error );
1213
1214
939
      if( read_count != (ssize_t) alignment_padding_size )
1215
17
      {
1216
17
        libcerror_error_set(
1217
17
         error,
1218
17
         LIBCERROR_ERROR_DOMAIN_IO,
1219
17
         LIBCERROR_IO_ERROR_READ_FAILED,
1220
17
         "%s: unable to read alignment padding data.",
1221
17
         function );
1222
1223
17
        goto on_error;
1224
17
      }
1225
922
      file_offset += read_count;
1226
1227
#if defined( HAVE_DEBUG_OUTPUT )
1228
      if( libcnotify_verbose != 0 )
1229
      {
1230
        libcnotify_printf(
1231
         "%s: alignment padding data:\n",
1232
         function );
1233
        libcnotify_print_data(
1234
         alignment_padding_data,
1235
         alignment_padding_size,
1236
         0 );
1237
      }
1238
#endif
1239
922
    }
1240
13.6k
    if( libagdb_volume_information_initialize(
1241
13.6k
         &volume_information,
1242
13.6k
         error ) != 1 )
1243
0
    {
1244
0
      libcerror_error_set(
1245
0
       error,
1246
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1247
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1248
0
       "%s: unable to create volume: %" PRIu32 " information.",
1249
0
       function,
1250
0
       volume_index );
1251
1252
0
      goto on_error;
1253
0
    }
1254
13.6k
    read_count = libagdb_internal_volume_information_read_file_io_handle(
1255
13.6k
                  (libagdb_internal_volume_information_t *) volume_information,
1256
13.6k
                  internal_file->io_handle,
1257
13.6k
                  internal_file->uncompressed_data_stream,
1258
13.6k
                  internal_file->file_io_handle,
1259
13.6k
                  file_offset,
1260
13.6k
                  volume_index,
1261
13.6k
                  error );
1262
1263
13.6k
    if( read_count == -1 )
1264
839
    {
1265
839
      libcerror_error_set(
1266
839
       error,
1267
839
       LIBCERROR_ERROR_DOMAIN_IO,
1268
839
       LIBCERROR_IO_ERROR_READ_FAILED,
1269
839
       "%s: unable to read volume: %" PRIu32 " information.",
1270
839
       function,
1271
839
       volume_index );
1272
1273
839
      goto on_error;
1274
839
    }
1275
12.7k
    file_offset += read_count;
1276
1277
12.7k
    if( libcdata_array_append_entry(
1278
12.7k
         internal_file->volumes_array,
1279
12.7k
         &entry_index,
1280
12.7k
         (intptr_t *) volume_information,
1281
12.7k
         error ) != 1 )
1282
0
    {
1283
0
      libcerror_error_set(
1284
0
       error,
1285
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1286
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1287
0
       "%s: unable to append volume: %" PRIu32 " information to array.",
1288
0
       function,
1289
0
       volume_index );
1290
1291
0
      goto on_error;
1292
0
    }
1293
12.7k
    volume_information = NULL;
1294
12.7k
  }
1295
723
  for( source_index = 0;
1296
43.6k
       source_index < file_header->number_of_sources;
1297
42.9k
       source_index++ )
1298
43.2k
  {
1299
43.2k
    if( libagdb_source_information_initialize(
1300
43.2k
         &source_information,
1301
43.2k
         error ) != 1 )
1302
0
    {
1303
0
      libcerror_error_set(
1304
0
       error,
1305
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1306
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1307
0
       "%s: unable to create source: %" PRIu32 " information.",
1308
0
       function,
1309
0
       source_index );
1310
1311
0
      goto on_error;
1312
0
    }
1313
43.2k
    read_count = libagdb_source_information_read(
1314
43.2k
                  (libagdb_internal_source_information_t *) source_information,
1315
43.2k
                  internal_file->io_handle,
1316
43.2k
                  internal_file->uncompressed_data_stream,
1317
43.2k
                  internal_file->file_io_handle,
1318
43.2k
                  source_index,
1319
43.2k
                  file_offset,
1320
43.2k
                  error );
1321
1322
43.2k
    if( read_count == -1 )
1323
334
    {
1324
334
      libcerror_error_set(
1325
334
       error,
1326
334
       LIBCERROR_ERROR_DOMAIN_IO,
1327
334
       LIBCERROR_IO_ERROR_READ_FAILED,
1328
334
       "%s: unable to read source: %" PRIu32 " information.",
1329
334
       function,
1330
334
       source_index );
1331
1332
334
      goto on_error;
1333
334
    }
1334
42.9k
    file_offset += read_count;
1335
1336
42.9k
    if( libcdata_array_append_entry(
1337
42.9k
         internal_file->sources_array,
1338
42.9k
         &entry_index,
1339
42.9k
         (intptr_t *) source_information,
1340
42.9k
         error ) != 1 )
1341
0
    {
1342
0
      libcerror_error_set(
1343
0
       error,
1344
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1345
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1346
0
       "%s: unable to append source: %" PRIu32 " information to array.",
1347
0
       function,
1348
0
       source_index );
1349
1350
0
      goto on_error;
1351
0
    }
1352
42.9k
    source_information = NULL;
1353
42.9k
  }
1354
389
  if( libagdb_file_header_free(
1355
389
       &file_header,
1356
389
       error ) != 1 )
1357
0
  {
1358
0
    libcerror_error_set(
1359
0
     error,
1360
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1361
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1362
0
     "%s: unable to free file header.",
1363
0
     function );
1364
1365
0
    goto on_error;
1366
0
  }
1367
/* TODO remove use offset instead */
1368
#if defined( HAVE_DEBUG_OUTPUT )
1369
  if( libcnotify_verbose != 0 )
1370
  {
1371
    uint8_t trailing_data[ 128 ];
1372
1373
    read_count = libfdata_stream_read_buffer(
1374
            internal_file->uncompressed_data_stream,
1375
            (intptr_t *) internal_file->file_io_handle,
1376
            trailing_data,
1377
            128,
1378
            0,
1379
            error );
1380
1381
    if( read_count == -1 )
1382
    {
1383
      libcerror_error_set(
1384
       error,
1385
       LIBCERROR_ERROR_DOMAIN_IO,
1386
       LIBCERROR_IO_ERROR_READ_FAILED,
1387
       "%s: unable to read trailing data.",
1388
       function );
1389
1390
      goto on_error;
1391
    }
1392
    if( read_count > 0 )
1393
    {
1394
      libcnotify_printf(
1395
       "%s: trailing data:\n",
1396
       function );
1397
      libcnotify_print_data(
1398
       trailing_data,
1399
       read_count,
1400
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
1401
    }
1402
  }
1403
#endif
1404
389
  return( 1 );
1405
1406
2.34k
on_error:
1407
2.34k
  if( source_information != NULL )
1408
334
  {
1409
334
    libagdb_internal_source_information_free(
1410
334
     (libagdb_internal_source_information_t **) &source_information,
1411
334
     NULL );
1412
334
  }
1413
2.34k
  if( volume_information != NULL )
1414
839
  {
1415
839
    libagdb_internal_volume_information_free(
1416
839
     (libagdb_internal_volume_information_t **) &volume_information,
1417
839
     NULL );
1418
839
  }
1419
2.34k
  if( internal_file->uncompressed_data_stream != NULL )
1420
2.23k
  {
1421
2.23k
    libfdata_stream_free(
1422
2.23k
     &( internal_file->uncompressed_data_stream ),
1423
2.23k
     NULL );
1424
2.23k
  }
1425
2.34k
  if( internal_file->compressed_blocks_cache != NULL )
1426
1.13k
  {
1427
1.13k
    libfcache_cache_free(
1428
1.13k
     &( internal_file->compressed_blocks_cache ),
1429
1.13k
     NULL );
1430
1.13k
  }
1431
2.34k
  if( internal_file->compressed_blocks_list != NULL )
1432
1.18k
  {
1433
1.18k
    libfdata_list_free(
1434
1.18k
     &( internal_file->compressed_blocks_list ),
1435
1.18k
     NULL );
1436
1.18k
  }
1437
2.34k
  if( file_header != NULL )
1438
2.23k
  {
1439
2.23k
    libagdb_file_header_free(
1440
2.23k
     &file_header,
1441
2.23k
     NULL );
1442
2.23k
  }
1443
2.34k
  if( compressed_file_header != NULL )
1444
62
  {
1445
62
    libagdb_compressed_file_header_free(
1446
62
     &compressed_file_header,
1447
62
     NULL );
1448
62
  }
1449
2.34k
  return( -1 );
1450
389
}
1451
1452
/* Retrieves the number of volumes
1453
 * Returns 1 if successful or -1 on error
1454
 */
1455
int libagdb_file_get_number_of_volumes(
1456
     libagdb_file_t *file,
1457
     int *number_of_volumes,
1458
     libcerror_error_t **error )
1459
389
{
1460
389
  libagdb_internal_file_t *internal_file = NULL;
1461
389
  static char *function                  = "libagdb_file_get_number_of_volumes";
1462
1463
389
  if( file == NULL )
1464
0
  {
1465
0
    libcerror_error_set(
1466
0
     error,
1467
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1468
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1469
0
     "%s: invalid file.",
1470
0
     function );
1471
1472
0
    return( -1 );
1473
0
  }
1474
389
  internal_file = (libagdb_internal_file_t *) file;
1475
1476
389
  if( libcdata_array_get_number_of_entries(
1477
389
       internal_file->volumes_array,
1478
389
       number_of_volumes,
1479
389
       error ) != 1 )
1480
0
  {
1481
0
    libcerror_error_set(
1482
0
     error,
1483
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1484
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1485
0
     "%s: unable to retrieve number of volumes.",
1486
0
     function );
1487
1488
0
    return( -1 );
1489
0
  }
1490
389
  return( 1 );
1491
389
}
1492
1493
/* Retrieves a specific volume information
1494
 * Returns 1 if successful or -1 on error
1495
 */
1496
int libagdb_file_get_volume_information(
1497
     libagdb_file_t *file,
1498
     int volume_index,
1499
     libagdb_volume_information_t **volume_information,
1500
     libcerror_error_t **error )
1501
558
{
1502
558
  libagdb_internal_file_t *internal_file = NULL;
1503
558
  static char *function                  = "libagdb_file_get_volume_information";
1504
1505
558
  if( file == NULL )
1506
0
  {
1507
0
    libcerror_error_set(
1508
0
     error,
1509
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1510
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1511
0
     "%s: invalid file.",
1512
0
     function );
1513
1514
0
    return( -1 );
1515
0
  }
1516
558
  internal_file = (libagdb_internal_file_t *) file;
1517
1518
558
  if( volume_information == NULL )
1519
0
  {
1520
0
    libcerror_error_set(
1521
0
     error,
1522
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1523
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1524
0
     "%s: invalid volume information.",
1525
0
     function );
1526
1527
0
    return( -1 );
1528
0
  }
1529
558
  if( *volume_information != NULL )
1530
0
  {
1531
0
    libcerror_error_set(
1532
0
     error,
1533
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1534
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
1535
0
     "%s: invalid volume information value already set.",
1536
0
     function );
1537
1538
0
    return( -1 );
1539
0
  }
1540
558
  if( libcdata_array_get_entry_by_index(
1541
558
       internal_file->volumes_array,
1542
558
       volume_index,
1543
558
       (intptr_t **) volume_information,
1544
558
       error ) != 1 )
1545
0
  {
1546
0
    libcerror_error_set(
1547
0
     error,
1548
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1549
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1550
0
     "%s: unable to retrieve volume: %d information.",
1551
0
     function,
1552
0
     volume_index );
1553
1554
0
    return( -1 );
1555
0
  }
1556
558
  return( 1 );
1557
558
}
1558
1559
/* Retrieves the number of sources
1560
 * Returns 1 if successful or -1 on error
1561
 */
1562
int libagdb_file_get_number_of_sources(
1563
     libagdb_file_t *file,
1564
     int *number_of_sources,
1565
     libcerror_error_t **error )
1566
389
{
1567
389
  libagdb_internal_file_t *internal_file = NULL;
1568
389
  static char *function                  = "libagdb_file_get_number_of_sources";
1569
1570
389
  if( file == NULL )
1571
0
  {
1572
0
    libcerror_error_set(
1573
0
     error,
1574
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1575
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1576
0
     "%s: invalid file.",
1577
0
     function );
1578
1579
0
    return( -1 );
1580
0
  }
1581
389
  internal_file = (libagdb_internal_file_t *) file;
1582
1583
389
  if( libcdata_array_get_number_of_entries(
1584
389
       internal_file->sources_array,
1585
389
       number_of_sources,
1586
389
       error ) != 1 )
1587
0
  {
1588
0
    libcerror_error_set(
1589
0
     error,
1590
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1591
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1592
0
     "%s: unable to retrieve number of sources.",
1593
0
     function );
1594
1595
0
    return( -1 );
1596
0
  }
1597
389
  return( 1 );
1598
389
}
1599
1600
/* Retrieves a specific source information
1601
 * Returns 1 if successful or -1 on error
1602
 */
1603
int libagdb_file_get_source_information(
1604
     libagdb_file_t *file,
1605
     int source_index,
1606
     libagdb_source_information_t **source_information,
1607
     libcerror_error_t **error )
1608
116
{
1609
116
  libagdb_internal_file_t *internal_file = NULL;
1610
116
  static char *function                  = "libagdb_file_get_source_information";
1611
1612
116
  if( file == NULL )
1613
0
  {
1614
0
    libcerror_error_set(
1615
0
     error,
1616
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1617
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1618
0
     "%s: invalid file.",
1619
0
     function );
1620
1621
0
    return( -1 );
1622
0
  }
1623
116
  internal_file = (libagdb_internal_file_t *) file;
1624
1625
116
  if( source_information == NULL )
1626
0
  {
1627
0
    libcerror_error_set(
1628
0
     error,
1629
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1630
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1631
0
     "%s: invalid source information.",
1632
0
     function );
1633
1634
0
    return( -1 );
1635
0
  }
1636
116
  if( *source_information != NULL )
1637
0
  {
1638
0
    libcerror_error_set(
1639
0
     error,
1640
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1641
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
1642
0
     "%s: invalid source information value already set.",
1643
0
     function );
1644
1645
0
    return( -1 );
1646
0
  }
1647
116
  if( libcdata_array_get_entry_by_index(
1648
116
       internal_file->sources_array,
1649
116
       source_index,
1650
116
       (intptr_t **) source_information,
1651
116
       error ) != 1 )
1652
0
  {
1653
0
    libcerror_error_set(
1654
0
     error,
1655
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1656
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1657
0
     "%s: unable to retrieve source: %d information.",
1658
0
     function,
1659
0
     source_index );
1660
1661
0
    return( -1 );
1662
0
  }
1663
116
  return( 1 );
1664
116
}
1665