Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libesedb/libesedb/libesedb_file.c
Line
Count
Source
1
/*
2
 * File functions
3
 *
4
 * Copyright (C) 2009-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 "libesedb_catalog.h"
29
#include "libesedb_database.h"
30
#include "libesedb_debug.h"
31
#include "libesedb_definitions.h"
32
#include "libesedb_i18n.h"
33
#include "libesedb_io_handle.h"
34
#include "libesedb_file.h"
35
#include "libesedb_file_header.h"
36
#include "libesedb_libbfio.h"
37
#include "libesedb_libcerror.h"
38
#include "libesedb_libcnotify.h"
39
#include "libesedb_libfcache.h"
40
#include "libesedb_libfdata.h"
41
#include "libesedb_page.h"
42
#include "libesedb_table.h"
43
#include "libesedb_table_definition.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 libesedb_file_initialize(
50
     libesedb_file_t **file,
51
     libcerror_error_t **error )
52
4.90k
{
53
4.90k
  libesedb_internal_file_t *internal_file = NULL;
54
4.90k
  static char *function                   = "libesedb_file_initialize";
55
56
4.90k
  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
4.90k
  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
4.90k
  internal_file = memory_allocate_structure(
79
4.90k
                   libesedb_internal_file_t );
80
81
4.90k
  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
4.90k
  if( memory_set(
93
4.90k
       internal_file,
94
4.90k
       0,
95
4.90k
       sizeof( libesedb_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
4.90k
  if( libesedb_io_handle_initialize(
110
4.90k
       &( internal_file->io_handle ),
111
4.90k
       error ) != 1 )
112
0
  {
113
0
    libcerror_error_set(
114
0
     error,
115
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
116
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
117
0
     "%s: unable to create IO handle.",
118
0
     function );
119
120
0
    goto on_error;
121
0
  }
122
4.90k
  if( libesedb_i18n_initialize(
123
4.90k
       error ) != 1 )
124
0
  {
125
0
    libcerror_error_set(
126
0
     error,
127
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
128
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
129
0
     "%s: unable to initalize internationalization (i18n).",
130
0
     function );
131
132
0
    goto on_error;
133
0
  }
134
4.90k
  *file = (libesedb_file_t *) internal_file;
135
136
4.90k
  return( 1 );
137
138
0
on_error:
139
0
  if( internal_file != NULL )
140
0
  {
141
0
    if( internal_file->io_handle != NULL )
142
0
    {
143
0
      libesedb_io_handle_free(
144
0
       &( internal_file->io_handle ),
145
0
       NULL );
146
0
    }
147
0
    memory_free(
148
0
     internal_file );
149
0
  }
150
0
  return( -1 );
151
4.90k
}
152
153
/* Frees a file
154
 * Returns 1 if successful or -1 on error
155
 */
156
int libesedb_file_free(
157
     libesedb_file_t **file,
158
     libcerror_error_t **error )
159
4.90k
{
160
4.90k
  libesedb_internal_file_t *internal_file = NULL;
161
4.90k
  static char *function                   = "libesedb_file_free";
162
4.90k
  int result                              = 1;
163
164
4.90k
  if( file == NULL )
165
0
  {
166
0
    libcerror_error_set(
167
0
     error,
168
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
169
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
170
0
     "%s: invalid file.",
171
0
     function );
172
173
0
    return( -1 );
174
0
  }
175
4.90k
  if( *file != NULL )
176
4.90k
  {
177
4.90k
    internal_file = (libesedb_internal_file_t *) *file;
178
179
4.90k
    if( internal_file->file_io_handle != NULL )
180
333
    {
181
333
      if( libesedb_file_close(
182
333
           *file,
183
333
           error ) != 0 )
184
0
      {
185
0
        libcerror_error_set(
186
0
         error,
187
0
         LIBCERROR_ERROR_DOMAIN_IO,
188
0
         LIBCERROR_IO_ERROR_CLOSE_FAILED,
189
0
         "%s: unable to close file.",
190
0
         function );
191
192
0
        result = -1;
193
0
      }
194
333
    }
195
4.90k
    *file = NULL;
196
197
4.90k
    if( libesedb_io_handle_free(
198
4.90k
         &( internal_file->io_handle ),
199
4.90k
         error ) != 1 )
200
0
    {
201
0
      libcerror_error_set(
202
0
       error,
203
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
204
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
205
0
       "%s: unable to free IO handle.",
206
0
       function );
207
208
0
      result = -1;
209
0
    }
210
4.90k
    memory_free(
211
4.90k
     internal_file );
212
4.90k
  }
213
4.90k
  return( result );
214
4.90k
}
215
216
/* Signals a file to abort its current activity
217
 * Returns 1 if successful or -1 on error
218
 */
219
int libesedb_file_signal_abort(
220
     libesedb_file_t *file,
221
     libcerror_error_t **error )
222
0
{
223
0
  libesedb_internal_file_t *internal_file = NULL;
224
0
  static char *function                   = "libesedb_file_signal_abort";
225
226
0
  if( file == NULL )
227
0
  {
228
0
    libcerror_error_set(
229
0
     error,
230
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
231
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
232
0
     "%s: invalid file.",
233
0
     function );
234
235
0
    return( -1 );
236
0
  }
237
0
  internal_file = (libesedb_internal_file_t *) file;
238
239
0
  if( internal_file->io_handle == NULL )
240
0
  {
241
0
    libcerror_error_set(
242
0
     error,
243
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
244
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
245
0
     "%s: invalid file - missing IO handle.",
246
0
     function );
247
248
0
    return( -1 );
249
0
  }
250
0
  internal_file->io_handle->abort = 1;
251
252
0
  return( 1 );
253
0
}
254
255
/* Opens a file
256
 * Returns 1 if successful or -1 on error
257
 */
258
int libesedb_file_open(
259
     libesedb_file_t *file,
260
     const char *filename,
261
     int access_flags,
262
     libcerror_error_t **error )
263
0
{
264
0
  libbfio_handle_t *file_io_handle        = NULL;
265
0
  libesedb_internal_file_t *internal_file = NULL;
266
0
  static char *function                   = "libesedb_file_open";
267
0
  size_t filename_length                  = 0;
268
269
0
  if( file == NULL )
270
0
  {
271
0
    libcerror_error_set(
272
0
     error,
273
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
274
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
275
0
     "%s: invalid file.",
276
0
     function );
277
278
0
    return( -1 );
279
0
  }
280
0
  internal_file = (libesedb_internal_file_t *) file;
281
282
0
  if( filename == NULL )
283
0
  {
284
0
    libcerror_error_set(
285
0
     error,
286
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
287
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
288
0
     "%s: invalid filename.",
289
0
     function );
290
291
0
    return( -1 );
292
0
  }
293
0
  if( ( ( access_flags & LIBESEDB_ACCESS_FLAG_READ ) == 0 )
294
0
   && ( ( access_flags & LIBESEDB_ACCESS_FLAG_WRITE ) == 0 ) )
295
0
  {
296
0
    libcerror_error_set(
297
0
     error,
298
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
299
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
300
0
     "%s: unsupported access flags.",
301
0
     function );
302
303
0
    return( -1 );
304
0
  }
305
0
  if( ( access_flags & LIBESEDB_ACCESS_FLAG_WRITE ) != 0 )
306
0
  {
307
0
    libcerror_error_set(
308
0
     error,
309
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
310
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
311
0
     "%s: write access currently not supported.",
312
0
     function );
313
314
0
    return( -1 );
315
0
  }
316
0
  if( libbfio_file_initialize(
317
0
       &file_io_handle,
318
0
       error ) != 1 )
319
0
  {
320
0
    libcerror_error_set(
321
0
     error,
322
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
323
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
324
0
     "%s: unable to create file IO handle.",
325
0
     function );
326
327
0
    goto on_error;
328
0
  }
329
#if defined( HAVE_DEBUG_OUTPUT )
330
  if( libbfio_handle_set_track_offsets_read(
331
       file_io_handle,
332
       1,
333
       error ) != 1 )
334
  {
335
                libcerror_error_set(
336
                 error,
337
                 LIBCERROR_ERROR_DOMAIN_RUNTIME,
338
                 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
339
                 "%s: unable to set track offsets read in file IO handle.",
340
                 function );
341
342
    goto on_error;
343
  }
344
#endif
345
0
  filename_length = narrow_string_length(
346
0
                     filename );
347
348
0
  if( libbfio_file_set_name(
349
0
       file_io_handle,
350
0
       filename,
351
0
       filename_length + 1,
352
0
       error ) != 1 )
353
0
  {
354
0
                libcerror_error_set(
355
0
                 error,
356
0
                 LIBCERROR_ERROR_DOMAIN_RUNTIME,
357
0
                 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
358
0
                 "%s: unable to set filename in file IO handle.",
359
0
                 function );
360
361
0
    goto on_error;
362
0
  }
363
0
  if( libesedb_file_open_file_io_handle(
364
0
       file,
365
0
       file_io_handle,
366
0
       access_flags,
367
0
       error ) != 1 )
368
0
  {
369
0
    libcerror_error_set(
370
0
     error,
371
0
     LIBCERROR_ERROR_DOMAIN_IO,
372
0
     LIBCERROR_IO_ERROR_OPEN_FAILED,
373
0
     "%s: unable to open file: %s.",
374
0
     function,
375
0
     filename );
376
377
0
    goto on_error;
378
0
  }
379
0
  internal_file->file_io_handle_created_in_library = 1;
380
381
0
  return( 1 );
382
383
0
on_error:
384
0
  if( file_io_handle != NULL )
385
0
  {
386
0
    libbfio_handle_free(
387
0
     &file_io_handle,
388
0
     NULL );
389
0
  }
390
0
  return( -1 );
391
0
}
392
393
#if defined( HAVE_WIDE_CHARACTER_TYPE )
394
395
/* Opens a file
396
 * Returns 1 if successful or -1 on error
397
 */
398
int libesedb_file_open_wide(
399
     libesedb_file_t *file,
400
     const wchar_t *filename,
401
     int access_flags,
402
     libcerror_error_t **error )
403
{
404
  libbfio_handle_t *file_io_handle        = NULL;
405
  libesedb_internal_file_t *internal_file = NULL;
406
  static char *function                   = "libesedb_file_open_wide";
407
  size_t filename_length                  = 0;
408
409
  if( file == NULL )
410
  {
411
    libcerror_error_set(
412
     error,
413
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
414
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
415
     "%s: invalid file.",
416
     function );
417
418
    return( -1 );
419
  }
420
  internal_file = (libesedb_internal_file_t *) file;
421
422
  if( filename == NULL )
423
  {
424
    libcerror_error_set(
425
     error,
426
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
427
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
428
     "%s: invalid filename.",
429
     function );
430
431
    return( -1 );
432
  }
433
  if( ( ( access_flags & LIBESEDB_ACCESS_FLAG_READ ) == 0 )
434
   && ( ( access_flags & LIBESEDB_ACCESS_FLAG_WRITE ) == 0 ) )
435
  {
436
    libcerror_error_set(
437
     error,
438
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
439
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
440
     "%s: unsupported access flags.",
441
     function );
442
443
    return( -1 );
444
  }
445
  if( ( access_flags & LIBESEDB_ACCESS_FLAG_WRITE ) != 0 )
446
  {
447
    libcerror_error_set(
448
     error,
449
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
450
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
451
     "%s: write access currently not supported.",
452
     function );
453
454
    return( -1 );
455
  }
456
  if( libbfio_file_initialize(
457
       &file_io_handle,
458
       error ) != 1 )
459
  {
460
    libcerror_error_set(
461
     error,
462
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
463
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
464
     "%s: unable to create file IO handle.",
465
     function );
466
467
    goto on_error;
468
  }
469
#if defined( HAVE_DEBUG_OUTPUT )
470
  if( libbfio_handle_set_track_offsets_read(
471
       file_io_handle,
472
       1,
473
       error ) != 1 )
474
  {
475
                libcerror_error_set(
476
                 error,
477
                 LIBCERROR_ERROR_DOMAIN_RUNTIME,
478
                 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
479
                 "%s: unable to set track offsets read in file IO handle.",
480
                 function );
481
482
    goto on_error;
483
  }
484
#endif
485
  filename_length = wide_string_length(
486
                     filename );
487
488
  if( libbfio_file_set_name_wide(
489
       file_io_handle,
490
       filename,
491
       filename_length + 1,
492
       error ) != 1 )
493
  {
494
                libcerror_error_set(
495
                 error,
496
                 LIBCERROR_ERROR_DOMAIN_RUNTIME,
497
                 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
498
                 "%s: unable to set filename in file IO handle.",
499
                 function );
500
501
    goto on_error;
502
  }
503
  if( libesedb_file_open_file_io_handle(
504
       file,
505
       file_io_handle,
506
       access_flags,
507
       error ) != 1 )
508
  {
509
    libcerror_error_set(
510
     error,
511
     LIBCERROR_ERROR_DOMAIN_IO,
512
     LIBCERROR_IO_ERROR_OPEN_FAILED,
513
     "%s: unable to open file: %ls.",
514
     function,
515
     filename );
516
517
    goto on_error;
518
  }
519
  internal_file->file_io_handle_created_in_library = 1;
520
521
  return( 1 );
522
523
on_error:
524
  if( file_io_handle != NULL )
525
  {
526
    libbfio_handle_free(
527
     &file_io_handle,
528
     NULL );
529
  }
530
  return( -1 );
531
}
532
533
#endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
534
535
/* Opens a file using a Basic File IO (bfio) handle
536
 * Returns 1 if successful or -1 on error
537
 */
538
int libesedb_file_open_file_io_handle(
539
     libesedb_file_t *file,
540
     libbfio_handle_t *file_io_handle,
541
     int access_flags,
542
     libcerror_error_t **error )
543
4.90k
{
544
4.90k
  libesedb_internal_file_t *internal_file = NULL;
545
4.90k
  static char *function                   = "libesedb_file_open_file_io_handle";
546
4.90k
  int bfio_access_flags                   = 0;
547
4.90k
  int file_io_handle_is_open              = 0;
548
549
4.90k
  if( file == NULL )
550
0
  {
551
0
    libcerror_error_set(
552
0
     error,
553
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
554
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
555
0
     "%s: invalid file.",
556
0
     function );
557
558
0
    return( -1 );
559
0
  }
560
4.90k
  if( file_io_handle == NULL )
561
0
  {
562
0
    libcerror_error_set(
563
0
     error,
564
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
565
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
566
0
     "%s: invalid file IO handle.",
567
0
     function );
568
569
0
    return( -1 );
570
0
  }
571
4.90k
  if( ( ( access_flags & LIBESEDB_ACCESS_FLAG_READ ) == 0 )
572
0
   && ( ( access_flags & LIBESEDB_ACCESS_FLAG_WRITE ) == 0 ) )
573
0
  {
574
0
    libcerror_error_set(
575
0
     error,
576
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
577
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
578
0
     "%s: unsupported access flags.",
579
0
     function );
580
581
0
    return( -1 );
582
0
  }
583
4.90k
  if( ( access_flags & LIBESEDB_ACCESS_FLAG_WRITE ) != 0 )
584
0
  {
585
0
    libcerror_error_set(
586
0
     error,
587
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
588
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
589
0
     "%s: write access currently not supported.",
590
0
     function );
591
592
0
    return( -1 );
593
0
  }
594
4.90k
  internal_file = (libesedb_internal_file_t *) file;
595
596
4.90k
  if( ( access_flags & LIBESEDB_ACCESS_FLAG_READ ) != 0 )
597
4.90k
  {
598
4.90k
    bfio_access_flags = LIBBFIO_ACCESS_FLAG_READ;
599
4.90k
  }
600
4.90k
  file_io_handle_is_open = libbfio_handle_is_open(
601
4.90k
                            file_io_handle,
602
4.90k
                            error );
603
604
4.90k
  if( file_io_handle_is_open == -1 )
605
0
  {
606
0
    libcerror_error_set(
607
0
     error,
608
0
     LIBCERROR_ERROR_DOMAIN_IO,
609
0
     LIBCERROR_IO_ERROR_OPEN_FAILED,
610
0
     "%s: unable to open file.",
611
0
     function );
612
613
0
    goto on_error;
614
0
  }
615
4.90k
  else if( file_io_handle_is_open == 0 )
616
4.90k
  {
617
4.90k
    if( libbfio_handle_open(
618
4.90k
         file_io_handle,
619
4.90k
         bfio_access_flags,
620
4.90k
         error ) != 1 )
621
0
    {
622
0
      libcerror_error_set(
623
0
       error,
624
0
       LIBCERROR_ERROR_DOMAIN_IO,
625
0
       LIBCERROR_IO_ERROR_OPEN_FAILED,
626
0
       "%s: unable to open file IO handle.",
627
0
       function );
628
629
0
      goto on_error;
630
0
    }
631
4.90k
    internal_file->file_io_handle_opened_in_library = 1;
632
4.90k
  }
633
4.90k
  if( libesedb_file_open_read(
634
4.90k
       internal_file,
635
4.90k
       file_io_handle,
636
4.90k
       error ) != 1 )
637
4.17k
  {
638
4.17k
    libcerror_error_set(
639
4.17k
     error,
640
4.17k
     LIBCERROR_ERROR_DOMAIN_IO,
641
4.17k
     LIBCERROR_IO_ERROR_READ_FAILED,
642
4.17k
     "%s: unable to read from file handle.",
643
4.17k
     function );
644
645
4.17k
    goto on_error;
646
4.17k
  }
647
728
  internal_file->file_io_handle = file_io_handle;
648
649
728
  return( 1 );
650
651
4.17k
on_error:
652
4.17k
  if( ( file_io_handle_is_open == 0 )
653
4.17k
   && ( internal_file->file_io_handle_opened_in_library != 0 ) )
654
4.17k
  {
655
4.17k
    libbfio_handle_close(
656
4.17k
     file_io_handle,
657
4.17k
     error );
658
659
4.17k
    internal_file->file_io_handle_opened_in_library = 0;
660
4.17k
  }
661
4.17k
  return( -1 );
662
4.90k
}
663
664
/* Closes a file
665
 * Returns 0 if successful or -1 on error
666
 */
667
int libesedb_file_close(
668
     libesedb_file_t *file,
669
     libcerror_error_t **error )
670
728
{
671
728
  libesedb_internal_file_t *internal_file = NULL;
672
728
  static char *function                   = "libesedb_file_close";
673
728
  int result                              = 0;
674
675
728
  if( file == NULL )
676
0
  {
677
0
    libcerror_error_set(
678
0
     error,
679
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
680
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
681
0
     "%s: invalid file.",
682
0
     function );
683
684
0
    return( -1 );
685
0
  }
686
728
  internal_file = (libesedb_internal_file_t *) file;
687
688
728
  if( internal_file->file_io_handle == NULL )
689
0
  {
690
0
    libcerror_error_set(
691
0
     error,
692
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
693
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
694
0
     "%s: invalid file - missing file IO handle.",
695
0
     function );
696
697
0
    return( -1 );
698
0
  }
699
#if defined( HAVE_DEBUG_OUTPUT )
700
  if( libcnotify_verbose != 0 )
701
  {
702
    if( internal_file->file_io_handle_created_in_library != 0 )
703
    {
704
      if( libesedb_debug_print_read_offsets(
705
           internal_file->file_io_handle,
706
           error ) != 1 )
707
      {
708
        libcerror_error_set(
709
         error,
710
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
711
         LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
712
         "%s: unable to print the read offsets.",
713
         function );
714
715
        result = -1;
716
      }
717
    }
718
  }
719
#endif
720
728
  if( internal_file->file_io_handle_opened_in_library != 0 )
721
728
  {
722
728
    if( libbfio_handle_close(
723
728
         internal_file->file_io_handle,
724
728
         error ) != 0 )
725
0
    {
726
0
      libcerror_error_set(
727
0
       error,
728
0
       LIBCERROR_ERROR_DOMAIN_IO,
729
0
       LIBCERROR_IO_ERROR_CLOSE_FAILED,
730
0
       "%s: unable to close file IO handle.",
731
0
       function );
732
733
0
      result = -1;
734
0
    }
735
728
    internal_file->file_io_handle_opened_in_library = 0;
736
728
  }
737
728
  if( internal_file->file_io_handle_created_in_library != 0 )
738
0
  {
739
0
    if( libbfio_handle_free(
740
0
         &( internal_file->file_io_handle ),
741
0
         error ) != 1 )
742
0
    {
743
0
      libcerror_error_set(
744
0
       error,
745
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
746
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
747
0
       "%s: unable to free file IO handle.",
748
0
       function );
749
750
0
      result = -1;
751
0
    }
752
0
    internal_file->file_io_handle_created_in_library = 0;
753
0
  }
754
728
  internal_file->file_io_handle = NULL;
755
756
728
  if( libesedb_io_handle_clear(
757
728
       internal_file->io_handle,
758
728
       error ) != 1 )
759
0
  {
760
0
    libcerror_error_set(
761
0
     error,
762
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
763
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
764
0
     "%s: unable to clear IO handle.",
765
0
     function );
766
767
0
    result = -1;
768
0
  }
769
728
  if( libfdata_vector_free(
770
728
       &( internal_file->pages_vector ),
771
728
       error ) != 1 )
772
0
  {
773
0
    libcerror_error_set(
774
0
     error,
775
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
776
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
777
0
     "%s: unable to free pages vector.",
778
0
     function );
779
780
0
    result = -1;
781
0
  }
782
728
  if( libfcache_cache_free(
783
728
       &( internal_file->pages_cache ),
784
728
       error ) != 1 )
785
0
  {
786
0
    libcerror_error_set(
787
0
     error,
788
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
789
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
790
0
     "%s: unable to free pages cache.",
791
0
     function );
792
793
0
    result = -1;
794
0
  }
795
728
  if( libesedb_database_free(
796
728
       &( internal_file->database ),
797
728
       error ) != 1 )
798
0
  {
799
0
    libcerror_error_set(
800
0
     error,
801
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
802
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
803
0
     "%s: unable to free database.",
804
0
     function );
805
806
0
    result = -1;
807
0
  }
808
728
  if( libesedb_catalog_free(
809
728
       &( internal_file->catalog ),
810
728
       error ) != 1 )
811
0
  {
812
0
    libcerror_error_set(
813
0
     error,
814
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
815
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
816
0
     "%s: unable to free catalog.",
817
0
     function );
818
819
0
    result = -1;
820
0
  }
821
728
  if( libesedb_catalog_free(
822
728
       &( internal_file->backup_catalog ),
823
728
       error ) != 1 )
824
0
  {
825
0
    libcerror_error_set(
826
0
     error,
827
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
828
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
829
0
     "%s: unable to free backup catalog.",
830
0
     function );
831
832
0
    result = -1;
833
0
  }
834
728
  return( result );
835
728
}
836
837
/* Opens a file for reading
838
 * Returns 1 if successful or -1 on error
839
 */
840
int libesedb_file_open_read(
841
     libesedb_internal_file_t *internal_file,
842
     libbfio_handle_t *file_io_handle,
843
     libcerror_error_t **error )
844
4.90k
{
845
4.90k
  libesedb_file_header_t *file_header = NULL;
846
4.90k
  static char *function               = "libesedb_file_open_read";
847
4.90k
  size64_t file_size                  = 0;
848
4.90k
  off64_t file_offset                 = 0;
849
4.90k
  int result                          = 0;
850
4.90k
  int segment_index                   = 0;
851
852
4.90k
  if( internal_file == NULL )
853
0
  {
854
0
    libcerror_error_set(
855
0
     error,
856
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
857
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
858
0
     "%s: invalid file.",
859
0
     function );
860
861
0
    return( -1 );
862
0
  }
863
4.90k
  if( internal_file->io_handle == NULL )
864
0
  {
865
0
    libcerror_error_set(
866
0
     error,
867
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
868
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
869
0
     "%s: invalid file - missing IO handle.",
870
0
     function );
871
872
0
    return( -1 );
873
0
  }
874
4.90k
  if( internal_file->pages_vector != NULL )
875
0
  {
876
0
    libcerror_error_set(
877
0
     error,
878
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
879
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
880
0
     "%s: invalid file - pages vector already set.",
881
0
     function );
882
883
0
    return( -1 );
884
0
  }
885
4.90k
  if( internal_file->pages_cache != NULL )
886
0
  {
887
0
    libcerror_error_set(
888
0
     error,
889
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
890
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
891
0
     "%s: invalid file - pages cache already set.",
892
0
     function );
893
894
0
    return( -1 );
895
0
  }
896
4.90k
  if( internal_file->database != NULL )
897
0
  {
898
0
    libcerror_error_set(
899
0
     error,
900
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
901
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
902
0
     "%s: invalid file - database already set.",
903
0
     function );
904
905
0
    return( -1 );
906
0
  }
907
4.90k
  if( internal_file->catalog != NULL )
908
0
  {
909
0
    libcerror_error_set(
910
0
     error,
911
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
912
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
913
0
     "%s: invalid file - catalog already set.",
914
0
     function );
915
916
0
    return( -1 );
917
0
  }
918
4.90k
  if( internal_file->backup_catalog != NULL )
919
0
  {
920
0
    libcerror_error_set(
921
0
     error,
922
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
923
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
924
0
     "%s: invalid file - backup catalog already set.",
925
0
     function );
926
927
0
    return( -1 );
928
0
  }
929
4.90k
  internal_file->io_handle->abort = 0;
930
931
4.90k
  if( libbfio_handle_get_size(
932
4.90k
       file_io_handle,
933
4.90k
       &file_size,
934
4.90k
       error ) != 1 )
935
0
  {
936
0
    libcerror_error_set(
937
0
     error,
938
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
939
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
940
0
     "%s: unable to retrieve file size.",
941
0
     function );
942
943
0
    goto on_error;
944
0
  }
945
#if defined( HAVE_DEBUG_OUTPUT )
946
  if( libcnotify_verbose != 0 )
947
  {
948
    libcnotify_printf(
949
     "Reading file header:\n" );
950
  }
951
#endif
952
4.90k
  if( libesedb_file_header_initialize(
953
4.90k
       &file_header,
954
4.90k
       error ) != 1 )
955
0
  {
956
0
    libcerror_error_set(
957
0
     error,
958
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
959
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
960
0
     "%s: unable to create file header.",
961
0
     function );
962
963
0
    goto on_error;
964
0
  }
965
4.90k
  if( libesedb_file_header_read_file_io_handle(
966
4.90k
       file_header,
967
4.90k
       file_io_handle,
968
4.90k
       file_offset,
969
4.90k
       error ) != 1 )
970
391
  {
971
391
    libcerror_error_set(
972
391
     error,
973
391
     LIBCERROR_ERROR_DOMAIN_IO,
974
391
     LIBCERROR_IO_ERROR_READ_FAILED,
975
391
     "%s: unable to read file header.",
976
391
     function );
977
978
391
    goto on_error;
979
391
  }
980
4.51k
  internal_file->io_handle->file_type                = file_header->file_type;
981
4.51k
  internal_file->io_handle->format_version           = file_header->format_version;
982
4.51k
  internal_file->io_handle->format_revision          = file_header->format_revision;
983
4.51k
  internal_file->io_handle->page_size                = file_header->page_size;
984
4.51k
  internal_file->io_handle->creation_format_version  = file_header->creation_format_version;
985
4.51k
  internal_file->io_handle->creation_format_revision = file_header->creation_format_revision;
986
4.51k
  internal_file->io_handle->database_state           = file_header->database_state;
987
988
4.51k
  if( libesedb_file_header_free(
989
4.51k
       &file_header,
990
4.51k
       error ) != 1 )
991
0
  {
992
0
    libcerror_error_set(
993
0
     error,
994
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
995
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
996
0
     "%s: unable to free file header.",
997
0
     function );
998
999
0
    goto on_error;
1000
0
  }
1001
#if defined( HAVE_DEBUG_OUTPUT )
1002
  if( libcnotify_verbose != 0 )
1003
  {
1004
    libcnotify_printf(
1005
     "Reading backup file header:\n" );
1006
  }
1007
#endif
1008
4.51k
  if( internal_file->io_handle->page_size != 0 )
1009
4.39k
  {
1010
4.39k
    if( libesedb_file_header_initialize(
1011
4.39k
         &file_header,
1012
4.39k
         error ) != 1 )
1013
0
    {
1014
0
      libcerror_error_set(
1015
0
       error,
1016
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1017
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1018
0
       "%s: unable to create backup file header.",
1019
0
       function );
1020
1021
0
      goto on_error;
1022
0
    }
1023
4.39k
    result = libesedb_file_header_read_file_io_handle(
1024
4.39k
              file_header,
1025
4.39k
              file_io_handle,
1026
4.39k
              (off64_t) internal_file->io_handle->page_size,
1027
4.39k
              error );
1028
1029
4.39k
    if( result != 1 )
1030
258
    {
1031
258
      file_offset = 0x0800;
1032
1033
1.54k
      while( file_offset <= 0x8000 )
1034
1.29k
      {
1035
#if defined( HAVE_DEBUG_OUTPUT )
1036
        if( ( libcnotify_verbose != 0 )
1037
         && ( error != NULL )
1038
         && ( *error != NULL ) )
1039
        {
1040
          libcnotify_print_error_backtrace(
1041
           *error );
1042
        }
1043
#endif
1044
1.29k
        libcerror_error_free(
1045
1.29k
         error );
1046
1047
1.29k
        result = libesedb_file_header_read_file_io_handle(
1048
1.29k
                  file_header,
1049
1.29k
                  file_io_handle,
1050
1.29k
                  (off64_t) internal_file->io_handle->page_size,
1051
1.29k
                  error );
1052
1053
1.29k
        if( result == 1 )
1054
0
        {
1055
0
          break;
1056
0
        }
1057
1.29k
        file_offset <<= 1;
1058
1.29k
      }
1059
258
    }
1060
4.39k
    if( result != 1 )
1061
258
    {
1062
258
      libcerror_error_set(
1063
258
       error,
1064
258
       LIBCERROR_ERROR_DOMAIN_IO,
1065
258
       LIBCERROR_IO_ERROR_READ_FAILED,
1066
258
       "%s: unable to read backup file header.",
1067
258
       function );
1068
1069
258
      goto on_error;
1070
258
    }
1071
4.13k
    if( internal_file->io_handle->format_version == 0 )
1072
47
    {
1073
47
      internal_file->io_handle->format_version = file_header->format_version;
1074
47
    }
1075
4.08k
    else if( internal_file->io_handle->format_version != file_header->format_version )
1076
2.57k
    {
1077
#if defined( HAVE_VERBOSE_OUTPUT )
1078
      if( libcnotify_verbose != 0 )
1079
      {
1080
        libcnotify_printf(
1081
         "%s: mismatch in format version: 0x%" PRIx32 " and backup: 0x%" PRIx32 "\n",
1082
         function,
1083
         internal_file->io_handle->format_version,
1084
         file_header->format_version );
1085
      }
1086
#endif
1087
2.57k
    }
1088
4.13k
    if( internal_file->io_handle->format_revision == 0 )
1089
1.33k
    {
1090
1.33k
      internal_file->io_handle->format_revision = file_header->format_revision;
1091
1.33k
    }
1092
2.80k
    else if( internal_file->io_handle->format_revision != file_header->format_revision )
1093
2.24k
    {
1094
#if defined( HAVE_VERBOSE_OUTPUT )
1095
      if( libcnotify_verbose != 0 )
1096
      {
1097
        libcnotify_printf(
1098
         "%s: mismatch in format revision: 0x%" PRIx32 " and backup: 0x%" PRIx32 "\n",
1099
         function,
1100
         internal_file->io_handle->format_revision,
1101
         file_header->format_revision );
1102
      }
1103
#endif
1104
2.24k
    }
1105
4.13k
    if( internal_file->io_handle->page_size == 0 )
1106
0
    {
1107
0
      internal_file->io_handle->page_size = file_header->page_size;
1108
0
    }
1109
4.13k
    else if( internal_file->io_handle->page_size != file_header->page_size )
1110
300
    {
1111
#if defined( HAVE_VERBOSE_OUTPUT )
1112
      if( libcnotify_verbose != 0 )
1113
      {
1114
        libcnotify_printf(
1115
         "%s: mismatch in page size: 0x%04" PRIx32 " and backup: 0x%04" PRIx32 "\n",
1116
         function,
1117
         internal_file->io_handle->page_size,
1118
         file_header->page_size );
1119
      }
1120
#endif
1121
      /* The offset of the backup (database) file header
1122
       * is a good indication of the actual page size
1123
       */
1124
300
      internal_file->io_handle->page_size = (uint32_t) file_offset;
1125
300
    }
1126
4.13k
    if( libesedb_file_header_free(
1127
4.13k
         &file_header,
1128
4.13k
         error ) != 1 )
1129
0
    {
1130
0
      libcerror_error_set(
1131
0
       error,
1132
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1133
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1134
0
       "%s: unable to free backup file header.",
1135
0
       function );
1136
1137
0
      goto on_error;
1138
0
    }
1139
4.13k
  }
1140
4.25k
  if( internal_file->io_handle->format_version != 0x620 )
1141
437
  {
1142
437
    libcerror_error_set(
1143
437
     error,
1144
437
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1145
437
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1146
437
     "%s: unsupported format version: 0x%04" PRIx32 ".",
1147
437
     function,
1148
437
     internal_file->io_handle->format_version );
1149
1150
437
    goto on_error;
1151
437
  }
1152
3.82k
  if( internal_file->io_handle->page_size == 0 )
1153
13
  {
1154
13
    libcerror_error_set(
1155
13
     error,
1156
13
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1157
13
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1158
13
     "%s: invalid page size.",
1159
13
     function );
1160
1161
13
    goto on_error;
1162
13
  }
1163
3.80k
  if( internal_file->io_handle->format_version == 0x620 )
1164
3.80k
  {
1165
3.80k
    if( internal_file->io_handle->format_revision < 0x11 )
1166
208
    {
1167
208
      if( ( internal_file->io_handle->page_size != 0x1000 )
1168
117
       && ( internal_file->io_handle->page_size != 0x2000 ) )
1169
99
      {
1170
99
        libcerror_error_set(
1171
99
         error,
1172
99
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1173
99
         LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1174
99
         "%s: unsupported page size: %" PRIu32 " (0x%04" PRIx32 ") for format version: 0x%" PRIx32 " revision: 0x%" PRIx32 ".",
1175
99
         function,
1176
99
         internal_file->io_handle->page_size,
1177
99
         internal_file->io_handle->page_size,
1178
99
         internal_file->io_handle->format_version,
1179
99
         internal_file->io_handle->format_revision );
1180
1181
99
        goto on_error;
1182
99
      }
1183
208
    }
1184
3.60k
    else
1185
3.60k
    {
1186
3.60k
      if( ( internal_file->io_handle->page_size != 0x0800 )
1187
577
       && ( internal_file->io_handle->page_size != 0x1000 )
1188
564
       && ( internal_file->io_handle->page_size != 0x2000 )
1189
550
       && ( internal_file->io_handle->page_size != 0x4000 )
1190
391
       && ( internal_file->io_handle->page_size != 0x8000 ) )
1191
220
      {
1192
220
        libcerror_error_set(
1193
220
         error,
1194
220
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1195
220
         LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1196
220
         "%s: unsupported page size: %" PRIu32 " (0x%04" PRIx32 ") for format version: 0x%" PRIx32 " revision: 0x%" PRIx32 ".",
1197
220
         function,
1198
220
         internal_file->io_handle->page_size,
1199
220
         internal_file->io_handle->page_size,
1200
220
         internal_file->io_handle->format_version,
1201
220
         internal_file->io_handle->format_revision );
1202
1203
220
        goto on_error;
1204
220
      }
1205
3.60k
    }
1206
3.80k
  }
1207
3.48k
  if( libesedb_io_handle_set_pages_data_range(
1208
3.48k
       internal_file->io_handle,
1209
3.48k
       file_size,
1210
3.48k
       error ) != 1 )
1211
0
  {
1212
0
                libcerror_error_set(
1213
0
                 error,
1214
0
                 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1215
0
                 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1216
0
                 "%s: unable to set pages data range in IO handle.",
1217
0
                 function );
1218
1219
0
    goto on_error;
1220
0
  }
1221
/* TODO add clone function ? */
1222
3.48k
  if( libfdata_vector_initialize(
1223
3.48k
       &( internal_file->pages_vector ),
1224
3.48k
       (size64_t) internal_file->io_handle->page_size,
1225
3.48k
       (intptr_t *) internal_file->io_handle,
1226
3.48k
       NULL,
1227
3.48k
       NULL,
1228
3.48k
       (int (*)(intptr_t *, intptr_t *, libfdata_vector_t *, libfdata_cache_t *, int, int, off64_t, size64_t, uint32_t, uint8_t, libcerror_error_t **)) &libesedb_io_handle_read_page,
1229
3.48k
       NULL,
1230
3.48k
       LIBFDATA_DATA_HANDLE_FLAG_NON_MANAGED,
1231
3.48k
       error ) != 1 )
1232
0
  {
1233
0
    libcerror_error_set(
1234
0
     error,
1235
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1236
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1237
0
     "%s: unable to create pages vector.",
1238
0
     function );
1239
1240
0
    goto on_error;
1241
0
  }
1242
3.48k
  if( libfdata_vector_append_segment(
1243
3.48k
       internal_file->pages_vector,
1244
3.48k
       &segment_index,
1245
3.48k
       0,
1246
3.48k
       internal_file->io_handle->pages_data_offset,
1247
3.48k
       internal_file->io_handle->pages_data_size,
1248
3.48k
       0,
1249
3.48k
       error ) != 1 )
1250
88
  {
1251
88
    libcerror_error_set(
1252
88
     error,
1253
88
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1254
88
     LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1255
88
     "%s: unable to append segment to pages vector.",
1256
88
     function );
1257
1258
88
    goto on_error;
1259
88
  }
1260
3.40k
  if( libfcache_cache_initialize(
1261
3.40k
       &( internal_file->pages_cache ),
1262
3.40k
       LIBESEDB_MAXIMUM_CACHE_ENTRIES_PAGES,
1263
3.40k
       error ) != 1 )
1264
0
  {
1265
0
    libcerror_error_set(
1266
0
     error,
1267
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1268
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1269
0
     "%s: unable to create pages cache.",
1270
0
     function );
1271
1272
0
    goto on_error;
1273
0
  }
1274
3.40k
  if( internal_file->io_handle->file_type == LIBESEDB_FILE_TYPE_DATABASE )
1275
3.28k
  {
1276
#if defined( HAVE_DEBUG_OUTPUT )
1277
    if( libcnotify_verbose != 0 )
1278
    {
1279
      libcnotify_printf(
1280
       "Reading the database:\n" );
1281
    }
1282
#endif
1283
3.28k
    if( libesedb_database_initialize(
1284
3.28k
         &( internal_file->database ),
1285
3.28k
         internal_file->io_handle,
1286
3.28k
         internal_file->pages_vector,
1287
3.28k
         internal_file->pages_cache,
1288
3.28k
         error ) != 1 )
1289
0
    {
1290
0
      libcerror_error_set(
1291
0
       error,
1292
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1293
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1294
0
       "%s: unable to create database.",
1295
0
       function );
1296
1297
0
      goto on_error;
1298
0
    }
1299
3.28k
    if( libesedb_database_read_file_io_handle(
1300
3.28k
         internal_file->database,
1301
3.28k
         file_io_handle,
1302
3.28k
         error ) != 1 )
1303
1.73k
    {
1304
1.73k
      libcerror_error_set(
1305
1.73k
       error,
1306
1.73k
       LIBCERROR_ERROR_DOMAIN_IO,
1307
1.73k
       LIBCERROR_IO_ERROR_READ_FAILED,
1308
1.73k
       "%s: unable to read database.",
1309
1.73k
       function );
1310
1311
1.73k
      goto on_error;
1312
1.73k
    }
1313
#if defined( HAVE_DEBUG_OUTPUT )
1314
    if( libcnotify_verbose != 0 )
1315
    {
1316
      libcnotify_printf(
1317
       "Reading the catalog:\n" );
1318
    }
1319
#endif
1320
1.55k
    if( libesedb_catalog_initialize(
1321
1.55k
         &( internal_file->catalog ),
1322
1.55k
         internal_file->io_handle,
1323
1.55k
         LIBESEDB_PAGE_NUMBER_CATALOG,
1324
1.55k
         internal_file->pages_vector,
1325
1.55k
         internal_file->pages_cache,
1326
1.55k
         error ) != 1 )
1327
0
    {
1328
0
      libcerror_error_set(
1329
0
       error,
1330
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1331
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1332
0
       "%s: unable to create catalog.",
1333
0
       function );
1334
1335
0
      goto on_error;
1336
0
    }
1337
1.55k
    if( libesedb_catalog_read_file_io_handle(
1338
1.55k
         internal_file->catalog,
1339
1.55k
         file_io_handle,
1340
1.55k
         error ) != 1 )
1341
753
    {
1342
753
      libcerror_error_set(
1343
753
       error,
1344
753
       LIBCERROR_ERROR_DOMAIN_IO,
1345
753
       LIBCERROR_IO_ERROR_READ_FAILED,
1346
753
       "%s: unable to read catalog.",
1347
753
       function );
1348
1349
753
      goto on_error;
1350
753
    }
1351
#if defined( HAVE_DEBUG_OUTPUT )
1352
    if( libcnotify_verbose != 0 )
1353
    {
1354
      libcnotify_printf(
1355
       "Reading the backup catalog:\n" );
1356
    }
1357
#endif
1358
802
    if( libesedb_catalog_initialize(
1359
802
         &( internal_file->backup_catalog ),
1360
802
         internal_file->io_handle,
1361
802
         LIBESEDB_PAGE_NUMBER_CATALOG_BACKUP,
1362
802
         internal_file->pages_vector,
1363
802
         internal_file->pages_cache,
1364
802
         error ) != 1 )
1365
0
    {
1366
0
      libcerror_error_set(
1367
0
       error,
1368
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1369
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1370
0
       "%s: unable to create backup catalog.",
1371
0
       function );
1372
1373
0
      goto on_error;
1374
0
    }
1375
802
    if( libesedb_catalog_read_file_io_handle(
1376
802
         internal_file->backup_catalog,
1377
802
         file_io_handle,
1378
802
         error ) != 1 )
1379
190
    {
1380
190
      libcerror_error_set(
1381
190
       error,
1382
190
       LIBCERROR_ERROR_DOMAIN_IO,
1383
190
       LIBCERROR_IO_ERROR_READ_FAILED,
1384
190
       "%s: unable to read backup catalog.",
1385
190
       function );
1386
1387
190
      goto on_error;
1388
190
    }
1389
/* TODO compare contents of catalogs ? */
1390
802
  }
1391
728
  internal_file->io_handle->abort = 0;
1392
1393
728
  return( 1 );
1394
1395
4.17k
on_error:
1396
4.17k
  if( internal_file->backup_catalog != NULL )
1397
190
  {
1398
190
    libesedb_catalog_free(
1399
190
     &( internal_file->backup_catalog ),
1400
190
     NULL );
1401
190
  }
1402
4.17k
  if( internal_file->catalog != NULL )
1403
943
  {
1404
943
    libesedb_catalog_free(
1405
943
     &( internal_file->catalog ),
1406
943
     NULL );
1407
943
  }
1408
4.17k
  if( internal_file->database != NULL )
1409
2.67k
  {
1410
2.67k
    libesedb_database_free(
1411
2.67k
     &( internal_file->database ),
1412
2.67k
     NULL );
1413
2.67k
  }
1414
4.17k
  if( internal_file->pages_cache != NULL )
1415
2.67k
  {
1416
2.67k
    libfcache_cache_free(
1417
2.67k
     &( internal_file->pages_cache ),
1418
2.67k
     NULL );
1419
2.67k
  }
1420
4.17k
  if( internal_file->pages_vector != NULL )
1421
2.76k
  {
1422
2.76k
    libfdata_vector_free(
1423
2.76k
     &( internal_file->pages_vector ),
1424
2.76k
     NULL );
1425
2.76k
  }
1426
4.17k
  if( file_header != NULL )
1427
649
  {
1428
649
    libesedb_file_header_free(
1429
649
     &file_header,
1430
649
     NULL );
1431
649
  }
1432
4.17k
  internal_file->io_handle->abort = 0;
1433
1434
4.17k
  return( -1 );
1435
3.40k
}
1436
1437
/* Retrieves the file type
1438
 * Returns 1 if successful or -1 on error
1439
 */
1440
int libesedb_file_get_type(
1441
     libesedb_file_t *file,
1442
     uint32_t *type,
1443
     libcerror_error_t **error )
1444
0
{
1445
0
  libesedb_internal_file_t *internal_file = NULL;
1446
0
  static char *function                   = "libesedb_file_get_type";
1447
1448
0
  if( file == NULL )
1449
0
  {
1450
0
    libcerror_error_set(
1451
0
     error,
1452
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1453
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1454
0
     "%s: invalid file.",
1455
0
     function );
1456
1457
0
    return( -1 );
1458
0
  }
1459
0
  internal_file = (libesedb_internal_file_t *) file;
1460
1461
0
  if( internal_file->io_handle == NULL )
1462
0
  {
1463
0
    libcerror_error_set(
1464
0
     error,
1465
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1466
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1467
0
     "%s: invalid file - missing IO handle.",
1468
0
     function );
1469
1470
0
    return( -1 );
1471
0
  }
1472
0
  if( type == NULL )
1473
0
  {
1474
0
    libcerror_error_set(
1475
0
     error,
1476
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1477
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1478
0
     "%s: invalid type.",
1479
0
     function );
1480
1481
0
    return( -1 );
1482
0
  }
1483
0
  *type = internal_file->io_handle->file_type;
1484
1485
0
  return( 1 );
1486
0
}
1487
1488
/* Retrieves the (current) formate version
1489
 * Returns 1 if successful or -1 on error
1490
 */
1491
int libesedb_file_get_format_version(
1492
     libesedb_file_t *file,
1493
     uint32_t *format_version,
1494
     uint32_t *format_revision,
1495
     libcerror_error_t **error )
1496
0
{
1497
0
  libesedb_internal_file_t *internal_file = NULL;
1498
0
  static char *function                   = "libesedb_file_get_format_version";
1499
1500
0
  if( file == NULL )
1501
0
  {
1502
0
    libcerror_error_set(
1503
0
     error,
1504
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1505
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1506
0
     "%s: invalid file.",
1507
0
     function );
1508
1509
0
    return( -1 );
1510
0
  }
1511
0
  internal_file = (libesedb_internal_file_t *) file;
1512
1513
0
  if( internal_file->io_handle == NULL )
1514
0
  {
1515
0
    libcerror_error_set(
1516
0
     error,
1517
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1518
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1519
0
     "%s: invalid file - missing IO handle.",
1520
0
     function );
1521
1522
0
    return( -1 );
1523
0
  }
1524
0
  if( format_version == NULL )
1525
0
  {
1526
0
    libcerror_error_set(
1527
0
     error,
1528
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1529
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1530
0
     "%s: invalid format version.",
1531
0
     function );
1532
1533
0
    return( -1 );
1534
0
  }
1535
0
  if( format_revision == NULL )
1536
0
  {
1537
0
    libcerror_error_set(
1538
0
     error,
1539
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1540
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1541
0
     "%s: invalid format revision.",
1542
0
     function );
1543
1544
0
    return( -1 );
1545
0
  }
1546
0
  *format_version  = internal_file->io_handle->format_version;
1547
0
  *format_revision = internal_file->io_handle->format_revision;
1548
1549
0
  return( 1 );
1550
0
}
1551
1552
/* Retrieves the creation format version
1553
 * Returns 1 if successful or -1 on error
1554
 */
1555
int libesedb_file_get_creation_format_version(
1556
     libesedb_file_t *file,
1557
     uint32_t *format_version,
1558
     uint32_t *format_revision,
1559
     libcerror_error_t **error )
1560
0
{
1561
0
  libesedb_internal_file_t *internal_file = NULL;
1562
0
  static char *function                   = "libesedb_file_get_creation_format_version";
1563
1564
0
  if( file == NULL )
1565
0
  {
1566
0
    libcerror_error_set(
1567
0
     error,
1568
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1569
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1570
0
     "%s: invalid file.",
1571
0
     function );
1572
1573
0
    return( -1 );
1574
0
  }
1575
0
  internal_file = (libesedb_internal_file_t *) file;
1576
1577
0
  if( internal_file->io_handle == NULL )
1578
0
  {
1579
0
    libcerror_error_set(
1580
0
     error,
1581
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1582
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1583
0
     "%s: invalid file - missing IO handle.",
1584
0
     function );
1585
1586
0
    return( -1 );
1587
0
  }
1588
0
  if( format_version == NULL )
1589
0
  {
1590
0
    libcerror_error_set(
1591
0
     error,
1592
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1593
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1594
0
     "%s: invalid format version.",
1595
0
     function );
1596
1597
0
    return( -1 );
1598
0
  }
1599
0
  if( format_revision == NULL )
1600
0
  {
1601
0
    libcerror_error_set(
1602
0
     error,
1603
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1604
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1605
0
     "%s: invalid format revision.",
1606
0
     function );
1607
1608
0
    return( -1 );
1609
0
  }
1610
0
  *format_version  = internal_file->io_handle->creation_format_version;
1611
0
  *format_revision = internal_file->io_handle->creation_format_revision;
1612
1613
0
  return( 1 );
1614
0
}
1615
1616
/* Retrieves the database state
1617
 * Returns 1 if successful or -1 on error
1618
 */
1619
int libesedb_file_get_database_state(
1620
     libesedb_file_t *file,
1621
     uint32_t *database_state,
1622
     libcerror_error_t **error )
1623
0
{
1624
0
  libesedb_internal_file_t *internal_file = NULL;
1625
0
  static char *function                   = "libesedb_file_get_database_state";
1626
1627
0
  if( file == NULL )
1628
0
  {
1629
0
    libcerror_error_set(
1630
0
     error,
1631
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1632
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1633
0
     "%s: invalid file.",
1634
0
     function );
1635
1636
0
    return( -1 );
1637
0
  }
1638
0
  internal_file = (libesedb_internal_file_t *) file;
1639
1640
0
  if( internal_file->io_handle == NULL )
1641
0
  {
1642
0
    libcerror_error_set(
1643
0
     error,
1644
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1645
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1646
0
     "%s: invalid file - missing IO handle.",
1647
0
     function );
1648
1649
0
    return( -1 );
1650
0
  }
1651
0
  if( database_state == NULL )
1652
0
  {
1653
0
    libcerror_error_set(
1654
0
     error,
1655
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1656
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1657
0
     "%s: invalid database state.",
1658
0
     function );
1659
1660
0
    return( -1 );
1661
0
  }
1662
0
  *database_state = internal_file->io_handle->database_state;
1663
1664
0
  return( 1 );
1665
0
}
1666
1667
/* Retrieves the page size
1668
 * Returns 1 if successful or -1 on error
1669
 */
1670
int libesedb_file_get_page_size(
1671
     libesedb_file_t *file,
1672
     uint32_t *page_size,
1673
     libcerror_error_t **error )
1674
0
{
1675
0
  libesedb_internal_file_t *internal_file = NULL;
1676
0
  static char *function                   = "libesedb_file_get_page_size";
1677
1678
0
  if( file == NULL )
1679
0
  {
1680
0
    libcerror_error_set(
1681
0
     error,
1682
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1683
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1684
0
     "%s: invalid file.",
1685
0
     function );
1686
1687
0
    return( -1 );
1688
0
  }
1689
0
  internal_file = (libesedb_internal_file_t *) file;
1690
1691
0
  if( internal_file->io_handle == NULL )
1692
0
  {
1693
0
    libcerror_error_set(
1694
0
     error,
1695
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1696
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1697
0
     "%s: invalid file - missing IO handle.",
1698
0
     function );
1699
1700
0
    return( -1 );
1701
0
  }
1702
0
  if( page_size == NULL )
1703
0
  {
1704
0
    libcerror_error_set(
1705
0
     error,
1706
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1707
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1708
0
     "%s: invalid page size.",
1709
0
     function );
1710
1711
0
    return( -1 );
1712
0
  }
1713
0
  *page_size = internal_file->io_handle->page_size;
1714
1715
0
  return( 1 );
1716
0
}
1717
1718
/* Retrieves the number of tables
1719
 * Returns 1 if successful or -1 on error
1720
 */
1721
int libesedb_file_get_number_of_tables(
1722
     libesedb_file_t *file,
1723
     int *number_of_tables,
1724
     libcerror_error_t **error )
1725
659
{
1726
659
  libesedb_internal_file_t *internal_file = NULL;
1727
659
  static char *function                   = "libesedb_file_get_number_of_tables";
1728
1729
659
  if( file == NULL )
1730
0
  {
1731
0
    libcerror_error_set(
1732
0
     error,
1733
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1734
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1735
0
     "%s: invalid file.",
1736
0
     function );
1737
1738
0
    return( -1 );
1739
0
  }
1740
659
  internal_file = (libesedb_internal_file_t *) file;
1741
1742
659
  if( internal_file->catalog == NULL )
1743
76
  {
1744
76
    if( number_of_tables == NULL )
1745
0
    {
1746
0
      libcerror_error_set(
1747
0
       error,
1748
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1749
0
       LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1750
0
       "%s: invalid number of tables.",
1751
0
       function );
1752
1753
0
      return( -1 );
1754
0
    }
1755
76
    *number_of_tables = 0;
1756
76
  }
1757
583
  else
1758
583
  {
1759
583
    if( libesedb_catalog_get_number_of_table_definitions(
1760
583
         internal_file->catalog,
1761
583
         number_of_tables,
1762
583
         error ) != 1 )
1763
0
    {
1764
0
      libcerror_error_set(
1765
0
       error,
1766
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1767
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1768
0
       "%s: unable to retrieve number of tables.",
1769
0
       function );
1770
1771
0
      return( -1 );
1772
0
    }
1773
583
  }
1774
659
  return( 1 );
1775
659
}
1776
1777
/* Retrieves a specific table
1778
 * Returns 1 if successful or -1 on error
1779
 */
1780
int libesedb_file_get_table(
1781
     libesedb_file_t *file,
1782
     int table_entry,
1783
     libesedb_table_t **table,
1784
     libcerror_error_t **error )
1785
535
{
1786
535
  libesedb_internal_file_t *internal_file                = NULL;
1787
535
  libesedb_table_definition_t *table_definition          = NULL;
1788
535
  libesedb_table_definition_t *template_table_definition = NULL;
1789
535
  static char *function                                  = "libesedb_file_get_table";
1790
1791
535
  if( file == NULL )
1792
0
  {
1793
0
    libcerror_error_set(
1794
0
     error,
1795
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1796
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1797
0
     "%s: invalid file.",
1798
0
     function );
1799
1800
0
    return( -1 );
1801
0
  }
1802
535
  internal_file = (libesedb_internal_file_t *) file;
1803
1804
535
  if( table == NULL )
1805
0
  {
1806
0
    libcerror_error_set(
1807
0
     error,
1808
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1809
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1810
0
     "%s: invalid table.",
1811
0
     function );
1812
1813
0
    return( -1 );
1814
0
  }
1815
535
  if( *table != NULL )
1816
0
  {
1817
0
    libcerror_error_set(
1818
0
     error,
1819
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1820
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
1821
0
     "%s: invalid table value already set.",
1822
0
     function );
1823
1824
0
    return( -1 );
1825
0
  }
1826
535
  if( libesedb_catalog_get_table_definition_by_index(
1827
535
       internal_file->catalog,
1828
535
       table_entry,
1829
535
       &table_definition,
1830
535
       error ) != 1 )
1831
0
  {
1832
0
    libcerror_error_set(
1833
0
     error,
1834
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1835
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1836
0
     "%s: unable to retrieve table definition: %d.",
1837
0
     function,
1838
0
     table_entry );
1839
1840
0
    return( -1 );
1841
0
  }
1842
535
  if( table_definition == NULL )
1843
0
  {
1844
0
    libcerror_error_set(
1845
0
     error,
1846
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1847
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1848
0
     "%s: missing table definition.",
1849
0
     function );
1850
1851
0
    return( -1 );
1852
0
  }
1853
535
  if( table_definition->table_catalog_definition == NULL )
1854
0
  {
1855
0
    libcerror_error_set(
1856
0
     error,
1857
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1858
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1859
0
     "%s: invalid table definition - missing table catalog definition.",
1860
0
     function );
1861
1862
0
    return( -1 );
1863
0
  }
1864
535
  if( table_definition->table_catalog_definition->template_name != NULL )
1865
272
  {
1866
272
    if( libesedb_catalog_get_table_definition_by_utf8_name(
1867
272
         internal_file->catalog,
1868
272
         table_definition->table_catalog_definition->template_name,
1869
272
         table_definition->table_catalog_definition->template_name_size,
1870
272
         &template_table_definition,
1871
272
         error ) != 1 )
1872
269
    {
1873
269
      libcerror_error_set(
1874
269
       error,
1875
269
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1876
269
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1877
269
       "%s: unable to retrieve template table definition.",
1878
269
       function );
1879
1880
269
      return( -1 );
1881
269
    }
1882
272
  }
1883
266
  if( libesedb_table_initialize(
1884
266
       table,
1885
266
       internal_file->file_io_handle,
1886
266
       internal_file->io_handle,
1887
266
       table_definition,
1888
266
       template_table_definition,
1889
266
       error ) != 1 )
1890
64
  {
1891
64
    libcerror_error_set(
1892
64
     error,
1893
64
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1894
64
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1895
64
     "%s: unable to create table.",
1896
64
     function );
1897
1898
64
    return( -1 );
1899
64
  }
1900
202
  return( 1 );
1901
266
}
1902
1903
/* Retrieves the table for the UTF-8 encoded name
1904
 * Returns 1 if successful, 0 if no table could be found or -1 on error
1905
 */
1906
int libesedb_file_get_table_by_utf8_name(
1907
     libesedb_file_t *file,
1908
     const uint8_t *utf8_string,
1909
     size_t utf8_string_length,
1910
     libesedb_table_t **table,
1911
     libcerror_error_t **error )
1912
0
{
1913
0
  libesedb_internal_file_t *internal_file                = NULL;
1914
0
  libesedb_table_definition_t *table_definition          = NULL;
1915
0
  libesedb_table_definition_t *template_table_definition = NULL;
1916
0
  static char *function                                  = "libesedb_file_get_table_by_utf8_name";
1917
0
  int result                                             = 0;
1918
1919
0
  if( file == NULL )
1920
0
  {
1921
0
    libcerror_error_set(
1922
0
     error,
1923
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1924
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1925
0
     "%s: invalid file.",
1926
0
     function );
1927
1928
0
    return( -1 );
1929
0
  }
1930
0
  internal_file = (libesedb_internal_file_t *) file;
1931
1932
0
  if( table == NULL )
1933
0
  {
1934
0
    libcerror_error_set(
1935
0
     error,
1936
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1937
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1938
0
     "%s: invalid table.",
1939
0
     function );
1940
1941
0
    return( -1 );
1942
0
  }
1943
0
  if( *table != NULL )
1944
0
  {
1945
0
    libcerror_error_set(
1946
0
     error,
1947
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1948
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
1949
0
     "%s: invalid table value already set.",
1950
0
     function );
1951
1952
0
    return( -1 );
1953
0
  }
1954
0
  result = libesedb_catalog_get_table_definition_by_utf8_name(
1955
0
            internal_file->catalog,
1956
0
            utf8_string,
1957
0
            utf8_string_length,
1958
0
            &table_definition,
1959
0
            error );
1960
1961
0
  if( result == -1 )
1962
0
  {
1963
0
    libcerror_error_set(
1964
0
     error,
1965
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1966
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1967
0
     "%s: unable to retrieve table definition.",
1968
0
     function );
1969
1970
0
    return( -1 );
1971
0
  }
1972
0
  else if( result != 0 )
1973
0
  {
1974
0
    if( table_definition == NULL )
1975
0
    {
1976
0
      libcerror_error_set(
1977
0
       error,
1978
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1979
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1980
0
       "%s: missing table definition.",
1981
0
       function );
1982
1983
0
      return( -1 );
1984
0
    }
1985
0
    if( table_definition->table_catalog_definition == NULL )
1986
0
    {
1987
0
      libcerror_error_set(
1988
0
       error,
1989
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1990
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1991
0
       "%s: invalid table definition - missing table catalog definition.",
1992
0
       function );
1993
1994
0
      return( -1 );
1995
0
    }
1996
0
    if( table_definition->table_catalog_definition->template_name != NULL )
1997
0
    {
1998
0
      if( libesedb_catalog_get_table_definition_by_name(
1999
0
           internal_file->catalog,
2000
0
           table_definition->table_catalog_definition->template_name,
2001
0
           table_definition->table_catalog_definition->template_name_size,
2002
0
           &template_table_definition,
2003
0
           error ) != 1 )
2004
0
      {
2005
0
        libcerror_error_set(
2006
0
         error,
2007
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2008
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2009
0
         "%s: unable to retrieve template table definition.",
2010
0
         function );
2011
2012
0
        return( -1 );
2013
0
      }
2014
0
    }
2015
0
    if( libesedb_table_initialize(
2016
0
         table,
2017
0
         internal_file->file_io_handle,
2018
0
         internal_file->io_handle,
2019
0
         table_definition,
2020
0
         template_table_definition,
2021
0
         error ) != 1 )
2022
0
    {
2023
0
      libcerror_error_set(
2024
0
       error,
2025
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2026
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2027
0
       "%s: unable to create table.",
2028
0
       function );
2029
2030
0
      return( -1 );
2031
0
    }
2032
0
  }
2033
0
  return( result );
2034
0
}
2035
2036
/* Retrieves the table for the UTF-16 encoded name
2037
 * Returns 1 if successful, 0 if no table could be found or -1 on error
2038
 */
2039
int libesedb_file_get_table_by_utf16_name(
2040
     libesedb_file_t *file,
2041
     const uint16_t *utf16_string,
2042
     size_t utf16_string_length,
2043
     libesedb_table_t **table,
2044
     libcerror_error_t **error )
2045
0
{
2046
0
  libesedb_internal_file_t *internal_file                = NULL;
2047
0
  libesedb_table_definition_t *table_definition          = NULL;
2048
0
  libesedb_table_definition_t *template_table_definition = NULL;
2049
0
  static char *function                                  = "libesedb_file_get_table_by_utf16_name";
2050
0
  int result                                             = 0;
2051
2052
0
  if( file == NULL )
2053
0
  {
2054
0
    libcerror_error_set(
2055
0
     error,
2056
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2057
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2058
0
     "%s: invalid file.",
2059
0
     function );
2060
2061
0
    return( -1 );
2062
0
  }
2063
0
  internal_file = (libesedb_internal_file_t *) file;
2064
2065
0
  if( table == NULL )
2066
0
  {
2067
0
    libcerror_error_set(
2068
0
     error,
2069
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2070
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2071
0
     "%s: invalid table.",
2072
0
     function );
2073
2074
0
    return( -1 );
2075
0
  }
2076
0
  if( *table != NULL )
2077
0
  {
2078
0
    libcerror_error_set(
2079
0
     error,
2080
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2081
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
2082
0
     "%s: invalid table value already set.",
2083
0
     function );
2084
2085
0
    return( -1 );
2086
0
  }
2087
0
  result = libesedb_catalog_get_table_definition_by_utf16_name(
2088
0
            internal_file->catalog,
2089
0
            utf16_string,
2090
0
            utf16_string_length,
2091
0
            &table_definition,
2092
0
            error );
2093
2094
0
  if( result == -1 )
2095
0
  {
2096
0
    libcerror_error_set(
2097
0
     error,
2098
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2099
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2100
0
     "%s: unable to retrieve table definition.",
2101
0
     function );
2102
2103
0
    return( -1 );
2104
0
  }
2105
0
  else if( result != 0 )
2106
0
  {
2107
0
    if( table_definition == NULL )
2108
0
    {
2109
0
      libcerror_error_set(
2110
0
       error,
2111
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2112
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2113
0
       "%s: missing table definition.",
2114
0
       function );
2115
2116
0
      return( -1 );
2117
0
    }
2118
0
    if( table_definition->table_catalog_definition == NULL )
2119
0
    {
2120
0
      libcerror_error_set(
2121
0
       error,
2122
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2123
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2124
0
       "%s: invalid table definition - missing table catalog definition.",
2125
0
       function );
2126
2127
0
      return( -1 );
2128
0
    }
2129
0
    if( table_definition->table_catalog_definition->template_name != NULL )
2130
0
    {
2131
0
      if( libesedb_catalog_get_table_definition_by_name(
2132
0
           internal_file->catalog,
2133
0
           table_definition->table_catalog_definition->template_name,
2134
0
           table_definition->table_catalog_definition->template_name_size,
2135
0
           &template_table_definition,
2136
0
           error ) != 1 )
2137
0
      {
2138
0
        libcerror_error_set(
2139
0
         error,
2140
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2141
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2142
0
         "%s: unable to retrieve template table definition.",
2143
0
         function );
2144
2145
0
        return( -1 );
2146
0
      }
2147
0
    }
2148
0
    if( libesedb_table_initialize(
2149
0
         table,
2150
0
         internal_file->file_io_handle,
2151
0
         internal_file->io_handle,
2152
0
         table_definition,
2153
0
         template_table_definition,
2154
0
         error ) != 1 )
2155
0
    {
2156
0
      libcerror_error_set(
2157
0
       error,
2158
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2159
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2160
0
       "%s: unable to create table.",
2161
0
       function );
2162
2163
0
      return( -1 );
2164
0
    }
2165
0
  }
2166
0
  return( result );
2167
0
}
2168