Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpff/libpff/libpff_recover.c
Line
Count
Source
1
/*
2
 * Recover functions
3
 *
4
 * Copyright (C) 2008-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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libpff_checksum.h"
28
#include "libpff_data_block.h"
29
#include "libpff_definitions.h"
30
#include "libpff_descriptors_index.h"
31
#include "libpff_index.h"
32
#include "libpff_index_node.h"
33
#include "libpff_index_value.h"
34
#include "libpff_index_values_list.h"
35
#include "libpff_item_descriptor.h"
36
#include "libpff_item_tree.h"
37
#include "libpff_io_handle.h"
38
#include "libpff_libbfio.h"
39
#include "libpff_libcdata.h"
40
#include "libpff_libcerror.h"
41
#include "libpff_libcnotify.h"
42
#include "libpff_local_descriptors_node.h"
43
#include "libpff_offsets_index.h"
44
#include "libpff_recover.h"
45
46
#include "pff_block.h"
47
#include "pff_index_node.h"
48
49
/* Scans for recoverable items
50
 * By default only the unallocated space is checked for recoverable items
51
 * Returns 1 if successful or -1 on error
52
 */
53
int libpff_recover_items(
54
     libpff_io_handle_t *io_handle,
55
     libbfio_handle_t *file_io_handle,
56
     libpff_descriptors_index_t *descriptors_index,
57
     libpff_offsets_index_t *offsets_index,
58
     libcdata_range_list_t *unallocated_data_block_list,
59
     libcdata_range_list_t *unallocated_page_block_list,
60
     libcdata_list_t *recovered_item_list,
61
     uint8_t recovery_flags,
62
     libcerror_error_t **error )
63
1.05k
{
64
1.05k
  libcdata_tree_node_t *item_tree_node          = NULL;
65
1.05k
  libpff_index_value_t *descriptors_index_value = NULL;
66
1.05k
  libpff_index_values_list_t *index_values_list = NULL;
67
1.05k
  libpff_item_descriptor_t *item_descriptor     = NULL;
68
1.05k
  static char *function                         = "libpff_recover_items";
69
1.05k
  int data_identifier_value_index               = 0;
70
1.05k
  int index_value_iterator                      = 0;
71
1.05k
  int index_values_list_iterator                = 0;
72
1.05k
  int local_descriptors_identifier_value_index  = 0;
73
1.05k
  int number_of_index_values                    = 0;
74
1.05k
  int number_of_index_values_lists              = 0;
75
1.05k
  int result                                    = 0;
76
77
#ifdef TODO
78
        uint32_t maximum_data_block_data_size         = 0;
79
#endif
80
81
1.05k
  if( io_handle == NULL )
82
0
  {
83
0
    libcerror_error_set(
84
0
     error,
85
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
86
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
87
0
     "%s: invalid IO handle.",
88
0
     function );
89
90
0
    return( -1 );
91
0
  }
92
1.05k
  if( ( io_handle->file_type != LIBPFF_FILE_TYPE_32BIT )
93
559
   && ( io_handle->file_type != LIBPFF_FILE_TYPE_64BIT )
94
185
   && ( io_handle->file_type != LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
95
0
  {
96
0
    libcerror_error_set(
97
0
     error,
98
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
99
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
100
0
     "%s: unsupported file type.",
101
0
     function );
102
103
0
    return( -1 );
104
0
  }
105
1.05k
  if( descriptors_index == NULL )
106
0
  {
107
0
    libcerror_error_set(
108
0
     error,
109
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
110
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
111
0
     "%s: invalid descriptors index.",
112
0
     function );
113
114
0
    return( -1 );
115
0
  }
116
1.05k
  if( descriptors_index->index == NULL )
117
0
  {
118
0
    libcerror_error_set(
119
0
     error,
120
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
121
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
122
0
     "%s: invalid descriptors index - missing index.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
1.05k
  if( offsets_index == NULL )
128
0
  {
129
0
    libcerror_error_set(
130
0
     error,
131
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
132
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
133
0
     "%s: invalid offsets index.",
134
0
     function );
135
136
0
    return( -1 );
137
0
  }
138
1.05k
  if( offsets_index->index == NULL )
139
0
  {
140
0
    libcerror_error_set(
141
0
     error,
142
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
143
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
144
0
     "%s: invalid offsets index - missing index.",
145
0
     function );
146
147
0
    return( -1 );
148
0
  }
149
1.05k
  if( recovered_item_list == NULL )
150
0
  {
151
0
    libcerror_error_set(
152
0
     error,
153
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
154
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
155
0
     "%s: invalid recovered item list.",
156
0
     function );
157
158
0
    return( -1 );
159
0
  }
160
#ifdef TODO
161
  if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
162
  {
163
    maximum_data_block_data_size = 8192 - 12;
164
  }
165
  else if( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
166
  {
167
    maximum_data_block_data_size = 8192 - 16;
168
  }
169
  else
170
  {
171
/* TODO: this value is currently assumed based on the 512 x 8 = 4k page */
172
    maximum_data_block_data_size = 65536 - 24;
173
  }
174
#endif /* TODO */
175
#if defined( HAVE_DEBUG_OUTPUT )
176
  if( libbfio_handle_set_track_offsets_read(
177
       file_io_handle,
178
       0,
179
       error ) != 1 )
180
  {
181
                libcerror_error_set(
182
                 error,
183
                 LIBCERROR_ERROR_DOMAIN_RUNTIME,
184
                 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
185
                 "%s: unable to set track offsets read in file IO handle.",
186
                 function );
187
188
    goto on_error;
189
  }
190
#endif
191
#ifdef TODO
192
  /* Scan the offsets index nodes for deleted values
193
   */
194
  if( libpff_recover_analyze_offsets_index_node(
195
       offsets_index,
196
       io_handle,
197
       file_io_handle,
198
       offsets_index->index->root_node_offset,
199
       offsets_index->index->root_node_back_pointer,
200
       maximum_data_block_data_size,
201
       0,
202
       error ) != 1 )
203
  {
204
    libcerror_error_set(
205
     error,
206
     LIBCERROR_ERROR_DOMAIN_IO,
207
     LIBCERROR_IO_ERROR_READ_FAILED,
208
     "%s: unable to recover offsets index nodes.",
209
     function );
210
211
    return( -1 );
212
  }
213
#endif /* TODO */
214
215
1.05k
  if( libpff_recover_data_blocks(
216
1.05k
       io_handle,
217
1.05k
       file_io_handle,
218
1.05k
       descriptors_index,
219
1.05k
       offsets_index,
220
1.05k
       unallocated_data_block_list,
221
1.05k
       unallocated_page_block_list,
222
1.05k
       recovery_flags,
223
1.05k
       error ) != 1 )
224
129
  {
225
129
    libcerror_error_set(
226
129
     error,
227
129
     LIBCERROR_ERROR_DOMAIN_IO,
228
129
     LIBCERROR_IO_ERROR_READ_FAILED,
229
129
     "%s: unable to recover data blocks.",
230
129
     function );
231
232
129
    goto on_error;
233
129
  }
234
  /* Scan the descriptors index nodes for deleted values
235
   */
236
927
  if( libpff_recover_descriptors_index_values(
237
927
       descriptors_index,
238
927
       io_handle,
239
927
       file_io_handle,
240
927
       descriptors_index->index->root_node_offset,
241
927
       descriptors_index->index->root_node_back_pointer,
242
927
       0,
243
927
       error ) != 1 )
244
195
  {
245
195
    libcerror_error_set(
246
195
     error,
247
195
     LIBCERROR_ERROR_DOMAIN_IO,
248
195
     LIBCERROR_IO_ERROR_READ_FAILED,
249
195
     "%s: unable to recover descriptors index values.",
250
195
     function );
251
252
195
    return( -1 );
253
195
  }
254
/* TODO combine the offsets index value check part with libpff_recover_descriptors_index_values so that the size of descriptors_index->recovered_index_values_tree remains small */
255
256
  /* For each recovered descriptors index value check if the corresponding local descriptor and data offsets index values exist
257
   */
258
732
  if( libcdata_btree_get_number_of_values(
259
732
       descriptors_index->recovered_index_values_tree,
260
732
       &number_of_index_values_lists,
261
732
       error ) != 1 )
262
0
  {
263
0
    libcerror_error_set(
264
0
     error,
265
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
266
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
267
0
     "%s: unable to retrieve number of recovered descriptors index values.",
268
0
     function );
269
270
0
    goto on_error;
271
0
  }
272
732
  for( index_values_list_iterator = 0;
273
22.9k
       index_values_list_iterator < number_of_index_values_lists;
274
22.2k
       index_values_list_iterator++ )
275
22.2k
  {
276
22.2k
    if( io_handle->abort != 0 )
277
0
    {
278
/* TODO break instead of error ? */
279
0
      goto on_error;
280
0
    }
281
22.2k
    if( libcdata_btree_get_value_by_index(
282
22.2k
         descriptors_index->recovered_index_values_tree,
283
22.2k
         index_values_list_iterator,
284
22.2k
         (intptr_t **) &index_values_list,
285
22.2k
         error ) != 1 )
286
0
    {
287
0
      libcerror_error_set(
288
0
       error,
289
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
290
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
291
0
       "%s: unable to retrieve recovered descriptors index values list: %d.",
292
0
       function,
293
0
       index_values_list_iterator );
294
295
0
      goto on_error;
296
0
    }
297
22.2k
    if( libpff_index_values_list_number_of_values(
298
22.2k
         index_values_list,
299
22.2k
         &number_of_index_values,
300
22.2k
         error ) != 1 )
301
0
    {
302
0
      libcerror_error_set(
303
0
       error,
304
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
305
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
306
0
       "%s: unable to retrieve number of recovered descriptors index values list: %d elements.",
307
0
       function,
308
0
       index_values_list_iterator );
309
310
0
      goto on_error;
311
0
    }
312
22.2k
    for( index_value_iterator = 0;
313
52.1k
         index_value_iterator < number_of_index_values;
314
29.8k
         index_value_iterator++ )
315
29.8k
    {
316
29.8k
      if( io_handle->abort != 0 )
317
0
      {
318
/* TODO break instead of error ? */
319
0
        goto on_error;
320
0
      }
321
29.8k
      if( libpff_index_values_list_get_value_by_index(
322
29.8k
           index_values_list,
323
29.8k
           index_value_iterator,
324
29.8k
           &descriptors_index_value,
325
29.8k
           error ) != 1 )
326
0
      {
327
0
        libcerror_error_set(
328
0
         error,
329
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
330
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
331
0
         "%s: unable to retrieve recovered descriptors index value: %" PRIu64 " list element: %d.",
332
0
         function,
333
0
         index_values_list->identifier,
334
0
         index_value_iterator );
335
336
0
        goto on_error;
337
0
      }
338
29.8k
      if( descriptors_index_value == NULL )
339
0
      {
340
0
        libcerror_error_set(
341
0
         error,
342
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
343
0
         LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
344
0
         "%s: missing recovered descriptors index value: %" PRIu64 " list element: %d.",
345
0
         function,
346
0
         index_values_list->identifier,
347
0
         index_value_iterator );
348
349
0
        goto on_error;
350
0
      }
351
#if defined( HAVE_DEBUG_OUTPUT )
352
      if( libcnotify_verbose != 0 )
353
      {
354
        libcnotify_printf(
355
         "%s: analyzing identifier: %" PRIu64 ", data: %" PRIu64 ", local descriptors: %" PRIu64 ", parent: %" PRIu32 "\n",
356
         function,
357
         descriptors_index_value->identifier,
358
         descriptors_index_value->data_identifier,
359
         descriptors_index_value->local_descriptors_identifier,
360
         descriptors_index_value->parent_identifier );
361
      }
362
#endif
363
29.8k
      result = libpff_recover_analyze_descriptor_data_identifier(
364
29.8k
          io_handle,
365
29.8k
          file_io_handle,
366
29.8k
          offsets_index,
367
29.8k
          descriptors_index_value,
368
29.8k
          &data_identifier_value_index,
369
29.8k
          error );
370
371
29.8k
      if( result == -1 )
372
0
      {
373
0
        libcerror_error_set(
374
0
         error,
375
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
376
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
377
0
         "%s: unable to recover descriptors index value: %" PRIu64 " data identifier: %" PRIu64 ".",
378
0
         function,
379
0
         descriptors_index_value->identifier,
380
0
         descriptors_index_value->data_identifier & (uint64_t) LIBPFF_OFFSET_INDEX_IDENTIFIER_MASK );
381
382
0
        goto on_error;
383
0
      }
384
29.8k
      else if( result == 0 )
385
27.7k
      {
386
27.7k
        continue;
387
27.7k
      }
388
2.19k
      result = libpff_recover_analyze_descriptor_local_descriptors_identifier(
389
2.19k
          io_handle,
390
2.19k
          file_io_handle,
391
2.19k
          offsets_index,
392
2.19k
          descriptors_index_value,
393
2.19k
          &local_descriptors_identifier_value_index,
394
2.19k
          error );
395
396
2.19k
      if( result == -1 )
397
0
      {
398
0
        libcerror_error_set(
399
0
         error,
400
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
401
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
402
0
         "%s: unable to recover descriptors index value: %" PRIu64 " local descriptors identifier: %" PRIu64 ".",
403
0
         function,
404
0
         descriptors_index_value->identifier,
405
0
         descriptors_index_value->local_descriptors_identifier & (uint64_t) LIBPFF_OFFSET_INDEX_IDENTIFIER_MASK );
406
407
0
        goto on_error;
408
0
      }
409
2.19k
      else if( result == 0 )
410
953
      {
411
953
        continue;
412
953
      }
413
#if defined( HAVE_DEBUG_OUTPUT )
414
      if( libcnotify_verbose != 0 )
415
      {
416
        libcnotify_printf(
417
         "%s: item descriptor: %" PRIu64 " is recoverable.\n",
418
         function,
419
         descriptors_index_value->identifier );
420
      }
421
#endif
422
      /* Create a new item descriptor
423
       */
424
1.23k
      if( libpff_item_descriptor_initialize(
425
1.23k
           &item_descriptor,
426
1.23k
           (uint32_t) descriptors_index_value->identifier,
427
1.23k
           descriptors_index_value->data_identifier,
428
1.23k
           descriptors_index_value->local_descriptors_identifier,
429
1.23k
           1,
430
1.23k
           error ) != 1 )
431
0
      {
432
0
        libcerror_error_set(
433
0
         error,
434
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
435
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
436
0
         "%s: unable to create item descriptor: %" PRIu64 ".",
437
0
         function,
438
0
         descriptors_index_value->identifier );
439
440
0
        goto on_error;
441
0
      }
442
1.23k
      if( item_descriptor == NULL )
443
0
      {
444
0
        libcerror_error_set(
445
0
         error,
446
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
447
0
         LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
448
0
         "%s: missing item descriptor: %" PRIu64 ".",
449
0
         function,
450
0
         descriptors_index_value->identifier );
451
452
0
        goto on_error;
453
0
      }
454
/* TODO add to initialize function or create a separate function for setting these value in the item descriptor */
455
1.23k
      item_descriptor->recovered_data_identifier_value_index              = data_identifier_value_index;
456
1.23k
      item_descriptor->recovered_local_descriptors_identifier_value_index = local_descriptors_identifier_value_index;
457
458
      /* Create a new item tree node
459
       */
460
1.23k
      if( libcdata_tree_node_initialize(
461
1.23k
           &item_tree_node,
462
1.23k
           error ) != 1 )
463
0
      {
464
0
        libcerror_error_set(
465
0
         error,
466
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
467
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
468
0
         "%s: unable to create item tree node.",
469
0
         function );
470
471
0
        goto on_error;
472
0
      }
473
1.23k
      if( libcdata_tree_node_set_value(
474
1.23k
           item_tree_node,
475
1.23k
           (intptr_t *) item_descriptor,
476
1.23k
           error ) != 1 )
477
0
      {
478
0
        libcerror_error_set(
479
0
         error,
480
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
481
0
         LIBCERROR_RUNTIME_ERROR_SET_FAILED,
482
0
         "%s: unable to set item descriptor in item tree node.",
483
0
         function );
484
485
0
        goto on_error;
486
0
      }
487
1.23k
      item_descriptor = NULL;
488
489
1.23k
      if( libcdata_list_append_value(
490
1.23k
           recovered_item_list,
491
1.23k
           (intptr_t *) item_tree_node,
492
1.23k
           error ) != 1 )
493
0
      {
494
0
        libcerror_error_set(
495
0
         error,
496
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
497
0
         LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
498
0
         "%s: unable to append tree node to recovered item list.",
499
0
         function );
500
501
0
        goto on_error;
502
0
      }
503
1.23k
      item_tree_node = NULL;
504
1.23k
    }
505
22.2k
  }
506
#if defined( HAVE_DEBUG_OUTPUT )
507
  if( libbfio_handle_set_track_offsets_read(
508
       file_io_handle,
509
       0,
510
       error ) != 1 )
511
  {
512
                libcerror_error_set(
513
                 error,
514
                 LIBCERROR_ERROR_DOMAIN_RUNTIME,
515
                 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
516
                 "%s: unable to set track offsets read in file IO handle.",
517
                 function );
518
519
                goto on_error;
520
  }
521
#endif
522
  /* TODO
523
   * link recovered descriptors to parent? and add to item hierarchy?
524
   * handle scanning without index data
525
   * what about 'encryption' ?
526
   */
527
732
  return( 1 );
528
529
129
on_error:
530
129
  if( item_tree_node != NULL )
531
0
  {
532
0
    libcdata_tree_node_free(
533
0
     &item_tree_node,
534
0
     (int (*)(intptr_t **, libcerror_error_t **)) &libpff_item_descriptor_free,
535
0
     NULL );
536
0
  }
537
129
  if( item_descriptor != NULL )
538
0
  {
539
0
    libpff_item_descriptor_free(
540
0
     &item_descriptor,
541
0
     NULL );
542
0
  }
543
129
  libcdata_list_empty(
544
129
   recovered_item_list,
545
129
   (int (*)(intptr_t **, libcerror_error_t **)) &libpff_item_tree_node_free_recovered,
546
129
   NULL );
547
548
129
  return( -1 );
549
732
}
550
551
/* Analyze if a specific descriptor data identifier is recoverable
552
 * Returns 1 if recoverable, 0 if not or -1 on error
553
 */
554
int libpff_recover_analyze_descriptor_data_identifier(
555
     libpff_io_handle_t *io_handle,
556
     libbfio_handle_t *file_io_handle,
557
     libpff_offsets_index_t *offsets_index,
558
     libpff_index_value_t *descriptors_index_value,
559
     int *data_identifier_value_index,
560
     libcerror_error_t **error )
561
29.8k
{
562
29.8k
  libcdata_tree_node_t *upper_node              = NULL;
563
29.8k
  libpff_index_value_t *index_value             = NULL;
564
29.8k
  libpff_index_value_t *lookup_index_value      = NULL;
565
29.8k
  libpff_index_values_list_t *index_values_list = NULL;
566
29.8k
  static char *function                         = "libpff_recover_analyze_descriptor_data_identifier";
567
29.8k
  uint64_t lookup_identifier                    = 0;
568
29.8k
  int index_value_iterator                      = 0;
569
29.8k
  int number_of_index_values                    = 0;
570
29.8k
  int result                                    = 0;
571
572
29.8k
  if( offsets_index == NULL )
573
0
  {
574
0
    libcerror_error_set(
575
0
     error,
576
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
577
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
578
0
     "%s: invalid offsets index.",
579
0
     function );
580
581
0
    return( -1 );
582
0
  }
583
29.8k
  if( descriptors_index_value == NULL )
584
0
  {
585
0
    libcerror_error_set(
586
0
     error,
587
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
588
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
589
0
     "%s: invalid descriptors index value.",
590
0
     function );
591
592
0
    return( -1 );
593
0
  }
594
29.8k
  if( data_identifier_value_index == NULL )
595
0
  {
596
0
    libcerror_error_set(
597
0
     error,
598
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
599
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
600
0
     "%s: invalid data identifier value index.",
601
0
     function );
602
603
0
    return( -1 );
604
0
  }
605
29.8k
  lookup_identifier = descriptors_index_value->data_identifier & (uint64_t) LIBPFF_OFFSET_INDEX_IDENTIFIER_MASK;
606
607
29.8k
  if( libpff_index_value_initialize(
608
29.8k
       &lookup_index_value,
609
29.8k
       error ) != 1 )
610
0
  {
611
0
    libcerror_error_set(
612
0
     error,
613
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
614
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
615
0
     "%s: unable to create lookup index value.",
616
0
     function );
617
618
0
    goto on_error;
619
0
  }
620
29.8k
  lookup_index_value->identifier = lookup_identifier;
621
622
/* TODO have a compare function that directly uses the lookup_identifier not the lookup_index_value */
623
29.8k
  result = libcdata_btree_get_value_by_value(
624
29.8k
            offsets_index->recovered_index_values_tree,
625
29.8k
            (intptr_t *) lookup_index_value,
626
29.8k
            (int (*)(intptr_t *, intptr_t *, libcerror_error_t **)) &libpff_index_values_list_compare,
627
29.8k
            &upper_node,
628
29.8k
            (intptr_t **) &index_values_list,
629
29.8k
            error );
630
631
29.8k
  if( result == -1 )
632
0
  {
633
0
    libcerror_error_set(
634
0
     error,
635
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
636
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
637
0
     "%s: unable to retrieve recovered offsets index value: %" PRIu64 " list.",
638
0
     function,
639
0
     lookup_identifier );
640
641
0
    goto on_error;
642
0
  }
643
29.8k
  if( libpff_index_value_free(
644
29.8k
       &lookup_index_value,
645
29.8k
       error ) != 1 )
646
0
  {
647
0
    libcerror_error_set(
648
0
     error,
649
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
650
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
651
0
     "%s: unable to free lookup index value.",
652
0
     function,
653
0
     lookup_identifier );
654
655
0
    goto on_error;
656
0
  }
657
29.8k
  if( result == 0 )
658
27.7k
  {
659
27.7k
    return( 0 );
660
27.7k
  }
661
2.19k
  if( libpff_index_values_list_number_of_values(
662
2.19k
       index_values_list,
663
2.19k
       &number_of_index_values,
664
2.19k
       error ) != 1 )
665
0
  {
666
0
    libcerror_error_set(
667
0
     error,
668
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
669
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
670
0
     "%s: unable to retrieve number of recovered offsets index value: %" PRIu64 " list.",
671
0
     function,
672
0
     lookup_identifier );
673
674
0
    goto on_error;
675
0
  }
676
/* TODO handle if more than 1 offsets index value is recoverable */
677
678
2.19k
  result = 0;
679
680
2.19k
  for( index_value_iterator = 0;
681
2.19k
       index_value_iterator < number_of_index_values;
682
2.19k
       index_value_iterator++ )
683
2.19k
  {
684
2.19k
    if( libpff_index_values_list_get_value_by_index(
685
2.19k
         index_values_list,
686
2.19k
         index_value_iterator,
687
2.19k
         &index_value,
688
2.19k
         error ) != 1 )
689
0
    {
690
0
      libcerror_error_set(
691
0
       error,
692
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
693
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
694
0
       "%s: unable to retrieve recovered offsets index value: %d for data identifier: %" PRIu64 ".",
695
0
       function,
696
0
       index_value_iterator,
697
0
       lookup_identifier );
698
699
0
      goto on_error;
700
0
    }
701
    /* Check if a data block is recoverable
702
     */
703
2.19k
    result = libpff_recover_analyze_data_block(
704
2.19k
        io_handle,
705
2.19k
        file_io_handle,
706
2.19k
        (uint32_t) descriptors_index_value->identifier,
707
2.19k
        index_value,
708
2.19k
        error );
709
710
2.19k
    if( result == -1 )
711
0
    {
712
0
      libcerror_error_set(
713
0
       error,
714
0
       LIBCERROR_ERROR_DOMAIN_IO,
715
0
       LIBCERROR_IO_ERROR_READ_FAILED,
716
0
       "%s: unable to recover data block: %" PRIu64 ".",
717
0
       function,
718
0
       descriptors_index_value->data_identifier );
719
720
#if defined( HAVE_DEBUG_OUTPUT )
721
      if( ( libcnotify_verbose != 0 )
722
       && ( error != NULL )
723
       && ( *error != NULL ) )
724
      {
725
        libcnotify_print_error_backtrace(
726
         *error );
727
      }
728
#endif
729
0
      libcerror_error_free(
730
0
       error );
731
732
/* TODO remove unreadable offset identifier from offsets_index->recovered_index_values_tree ? */
733
0
    }
734
2.19k
    else if( result != 0 )
735
2.19k
    {
736
2.19k
      break;
737
2.19k
    }
738
2.19k
  }
739
2.19k
  if( result != 0 )
740
2.19k
  {
741
2.19k
    *data_identifier_value_index = index_value_iterator;
742
2.19k
  }
743
#if defined( HAVE_DEBUG_OUTPUT )
744
  else if( libcnotify_verbose != 0 )
745
  {
746
    libcnotify_printf(
747
     "%s: recovered offsets index value for data identifier: %" PRIu64 " not available.\n",
748
     function,
749
     lookup_identifier );
750
  }
751
#endif
752
2.19k
  return( result );
753
754
0
on_error:
755
0
  if( lookup_index_value != NULL )
756
0
  {
757
0
    libpff_index_value_free(
758
0
     &lookup_index_value,
759
0
     NULL );
760
0
  }
761
0
  return( -1 );
762
2.19k
}
763
764
/* Analyze if a specific descriptor local descriptors identifier is recoverable
765
 * Returns 1 if recoverable, 0 if not or -1 on error
766
 */
767
int libpff_recover_analyze_descriptor_local_descriptors_identifier(
768
     libpff_io_handle_t *io_handle,
769
     libbfio_handle_t *file_io_handle,
770
     libpff_offsets_index_t *offsets_index,
771
     libpff_index_value_t *descriptors_index_value,
772
     int *local_descriptors_identifier_value_index,
773
     libcerror_error_t **error )
774
2.19k
{
775
2.19k
  libcdata_tree_node_t *upper_node              = NULL;
776
2.19k
  libpff_index_value_t *index_value             = NULL;
777
2.19k
  libpff_index_value_t *lookup_index_value      = NULL;
778
2.19k
  libpff_index_values_list_t *index_values_list = NULL;
779
2.19k
  static char *function                         = "libpff_recover_analyze_descriptor_local_descriptors_identifier";
780
2.19k
  uint64_t lookup_identifier                    = 0;
781
2.19k
  int index_value_iterator                      = 0;
782
2.19k
  int number_of_index_values                    = 0;
783
2.19k
  int result                                    = 0;
784
785
2.19k
  if( offsets_index == NULL )
786
0
  {
787
0
    libcerror_error_set(
788
0
     error,
789
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
790
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
791
0
     "%s: invalid offsets index.",
792
0
     function );
793
794
0
    return( -1 );
795
0
  }
796
2.19k
  if( descriptors_index_value == NULL )
797
0
  {
798
0
    libcerror_error_set(
799
0
     error,
800
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
801
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
802
0
     "%s: invalid descriptors index value.",
803
0
     function );
804
805
0
    return( -1 );
806
0
  }
807
2.19k
  if( local_descriptors_identifier_value_index == NULL )
808
0
  {
809
0
    libcerror_error_set(
810
0
     error,
811
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
812
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
813
0
     "%s: invalid local descriptors identifier value index.",
814
0
     function );
815
816
0
    return( -1 );
817
0
  }
818
  /* The local descriptors identifier is 0 if not set
819
   */
820
2.19k
  if( descriptors_index_value->local_descriptors_identifier == 0 )
821
1.23k
  {
822
1.23k
    return( 1 );
823
1.23k
  }
824
953
  lookup_identifier = descriptors_index_value->local_descriptors_identifier & (uint64_t) LIBPFF_OFFSET_INDEX_IDENTIFIER_MASK;
825
826
953
  if( libpff_index_value_initialize(
827
953
       &lookup_index_value,
828
953
       error ) != 1 )
829
0
  {
830
0
    libcerror_error_set(
831
0
     error,
832
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
833
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
834
0
     "%s: unable to create lookup index value.",
835
0
     function );
836
837
0
    goto on_error;
838
0
  }
839
953
  lookup_index_value->identifier = lookup_identifier;
840
841
/* TODO have a compare function that directly uses the lookup_identifier not the lookup_index_value */
842
953
  result = libcdata_btree_get_value_by_value(
843
953
            offsets_index->recovered_index_values_tree,
844
953
            (intptr_t *) lookup_index_value,
845
953
            (int (*)(intptr_t *, intptr_t *, libcerror_error_t **)) &libpff_index_values_list_compare,
846
953
            &upper_node,
847
953
            (intptr_t **) &index_values_list,
848
953
            error );
849
850
953
  if( result == -1 )
851
0
  {
852
0
    libcerror_error_set(
853
0
     error,
854
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
855
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
856
0
     "%s: unable to retrieve recovered offsets index value: %" PRIu64 " list.",
857
0
     function,
858
0
     lookup_identifier );
859
860
0
    goto on_error;
861
0
  }
862
953
  if( libpff_index_value_free(
863
953
       &lookup_index_value,
864
953
       error ) != 1 )
865
0
  {
866
0
    libcerror_error_set(
867
0
     error,
868
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
869
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
870
0
     "%s: unable to free lookup index value.",
871
0
     function,
872
0
     lookup_identifier );
873
874
0
    goto on_error;
875
0
  }
876
953
  if( result == 0 )
877
701
  {
878
701
    return( 0 );
879
701
  }
880
252
  if( libpff_index_values_list_number_of_values(
881
252
       index_values_list,
882
252
       &number_of_index_values,
883
252
       error ) != 1 )
884
0
  {
885
0
    libcerror_error_set(
886
0
     error,
887
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
888
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
889
0
     "%s: unable to retrieve number of recovered offsets index value: %" PRIu64 " list.",
890
0
     function,
891
0
     lookup_identifier );
892
893
0
    goto on_error;
894
0
  }
895
/* TODO handle if more than 1 offsets index value is recoverable */
896
897
252
  result = 0;
898
899
252
  for( index_value_iterator = 0;
900
562
       index_value_iterator < number_of_index_values;
901
310
       index_value_iterator++ )
902
310
  {
903
310
    if( libpff_index_values_list_get_value_by_index(
904
310
         index_values_list,
905
310
         index_value_iterator,
906
310
         &index_value,
907
310
         error ) != 1 )
908
0
    {
909
0
      libcerror_error_set(
910
0
       error,
911
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
912
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
913
0
       "%s: unable to retrieve recovered offsets index value: %d for data identifier: %" PRIu64 ".",
914
0
       function,
915
0
       index_value_iterator,
916
0
       lookup_identifier );
917
918
0
      goto on_error;
919
0
    }
920
    /* Check if local descriptors are recoverable
921
     */
922
310
    result = libpff_recover_analyze_local_descriptors(
923
310
              io_handle,
924
310
              file_io_handle,
925
310
              offsets_index,
926
310
              descriptors_index_value->local_descriptors_identifier,
927
310
              0,
928
310
              error );
929
930
310
    if( result == -1 )
931
0
    {
932
0
      libcerror_error_set(
933
0
       error,
934
0
       LIBCERROR_ERROR_DOMAIN_IO,
935
0
       LIBCERROR_IO_ERROR_READ_FAILED,
936
0
       "%s: unable to recover local descriptors: %" PRIu64 ".",
937
0
       function,
938
0
       descriptors_index_value->local_descriptors_identifier );
939
940
0
      goto on_error;
941
0
    }
942
310
    else if( result != 0 )
943
0
    {
944
0
      break;
945
0
    }
946
310
  }
947
252
  if( result != 0 )
948
0
  {
949
0
    *local_descriptors_identifier_value_index = index_value_iterator;
950
0
  }
951
#if defined( HAVE_DEBUG_OUTPUT )
952
  else if( libcnotify_verbose != 0 )
953
  {
954
    libcnotify_printf(
955
     "%s: recovered offsets index value for local descriptors identifier: %" PRIu64 " not available.\n",
956
     function,
957
     lookup_identifier );
958
  }
959
#endif
960
252
  return( result );
961
962
0
on_error:
963
0
  if( lookup_index_value != NULL )
964
0
  {
965
0
    libpff_index_value_free(
966
0
     &lookup_index_value,
967
0
     NULL );
968
0
  }
969
0
  return( -1 );
970
252
}
971
972
/* Analyze if a specific data block back pointer is recoverable
973
 * Returns 1 if recoverable, 0 if not or -1 on error
974
 */
975
int libpff_recover_analyze_data_block_back_pointer(
976
     libpff_offsets_index_t *offsets_index,
977
     uint64_t data_block_back_pointer,
978
     off64_t data_block_data_offset,
979
     size32_t data_block_data_size,
980
     libcerror_error_t **error )
981
0
{
982
0
  libcdata_tree_node_t *upper_node              = NULL;
983
0
  libpff_index_value_t *index_value             = NULL;
984
0
  libpff_index_value_t *lookup_index_value      = NULL;
985
0
  libpff_index_values_list_t *index_values_list = NULL;
986
0
  static char *function                         = "libpff_recover_analyze_data_block_back_pointer";
987
0
  uint64_t lookup_identifier                    = 0;
988
0
  int index_value_iterator                      = 0;
989
0
  int number_of_index_values                    = 0;
990
0
  int result                                    = 0;
991
992
0
  if( offsets_index == NULL )
993
0
  {
994
0
    libcerror_error_set(
995
0
     error,
996
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
997
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
998
0
     "%s: invalid offsets index.",
999
0
     function );
1000
1001
0
    return( -1 );
1002
0
  }
1003
0
  lookup_identifier = data_block_back_pointer;
1004
1005
0
  if( libpff_index_value_initialize(
1006
0
       &lookup_index_value,
1007
0
       error ) != 1 )
1008
0
  {
1009
0
    libcerror_error_set(
1010
0
     error,
1011
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1012
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1013
0
     "%s: unable to create lookup index value.",
1014
0
     function );
1015
1016
0
    goto on_error;
1017
0
  }
1018
0
  lookup_index_value->identifier = lookup_identifier;
1019
1020
/* TODO have a compare function that directly uses the lookup_identifier not the lookup_index_value */
1021
0
  result = libcdata_btree_get_value_by_value(
1022
0
            offsets_index->recovered_index_values_tree,
1023
0
            (intptr_t *) lookup_index_value,
1024
0
            (int (*)(intptr_t *, intptr_t *, libcerror_error_t **)) &libpff_index_values_list_compare,
1025
0
            &upper_node,
1026
0
            (intptr_t **) &index_values_list,
1027
0
            error );
1028
1029
0
  if( result == -1 )
1030
0
  {
1031
0
    libcerror_error_set(
1032
0
     error,
1033
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1034
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1035
0
     "%s: unable to retrieve recovered offsets index value: %" PRIu64 " list.",
1036
0
     function,
1037
0
     lookup_identifier );
1038
1039
0
    goto on_error;
1040
0
  }
1041
0
  if( libpff_index_value_free(
1042
0
       &lookup_index_value,
1043
0
       error ) != 1 )
1044
0
  {
1045
0
    libcerror_error_set(
1046
0
     error,
1047
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1048
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1049
0
     "%s: unable to free lookup index value.",
1050
0
     function,
1051
0
     lookup_identifier );
1052
1053
0
    goto on_error;
1054
0
  }
1055
0
  if( result == 0 )
1056
0
  {
1057
0
    return( 0 );
1058
0
  }
1059
0
  if( libpff_index_values_list_number_of_values(
1060
0
       index_values_list,
1061
0
       &number_of_index_values,
1062
0
       error ) != 1 )
1063
0
  {
1064
0
    libcerror_error_set(
1065
0
     error,
1066
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1067
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1068
0
     "%s: unable to retrieve number of recovered offsets index value: %" PRIu64 " list.",
1069
0
     function,
1070
0
     lookup_identifier );
1071
1072
0
    goto on_error;
1073
0
  }
1074
/* TODO handle if more than 1 offsets index value is recoverable */
1075
1076
0
  result = 0;
1077
1078
0
  for( index_value_iterator = 0;
1079
0
       index_value_iterator < number_of_index_values;
1080
0
       index_value_iterator++ )
1081
0
  {
1082
0
    if( libpff_index_values_list_get_value_by_index(
1083
0
         index_values_list,
1084
0
         index_value_iterator,
1085
0
         &index_value,
1086
0
         error ) != 1 )
1087
0
    {
1088
0
      libcerror_error_set(
1089
0
       error,
1090
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1091
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1092
0
       "%s: unable to retrieve recovered offsets index value: %d for data identifier: %" PRIu64 ".",
1093
0
       function,
1094
0
       index_value_iterator,
1095
0
       lookup_identifier );
1096
1097
0
      goto on_error;
1098
0
    }
1099
0
    if( ( data_block_data_offset == index_value->file_offset )
1100
0
     && ( data_block_data_size == index_value->data_size ) )
1101
0
    {
1102
0
      result = 1;
1103
0
    }
1104
0
    if( result != 0 )
1105
0
    {
1106
0
      break;
1107
0
    }
1108
0
  }
1109
0
  if( result != 0 )
1110
0
  {
1111
#if defined( HAVE_DEBUG_OUTPUT )
1112
    if( libcnotify_verbose != 0 )
1113
    {
1114
      libcnotify_printf(
1115
       "%s: recovered data block with identifier: %" PRIu64 " matches existing recovered item value.\n",
1116
       function,
1117
       lookup_identifier );
1118
    }
1119
#endif
1120
0
  }
1121
0
  return( result );
1122
1123
0
on_error:
1124
0
  if( lookup_index_value != NULL )
1125
0
  {
1126
0
    libpff_index_value_free(
1127
0
     &lookup_index_value,
1128
0
     NULL );
1129
0
  }
1130
0
  return( -1 );
1131
0
}
1132
1133
/* Analyze if a specific descriptors index value is recoverable
1134
 * Returns 1 if recoverable, 0 if not or -1 on error
1135
 */
1136
int libpff_recover_analyze_descriptors_index_value(
1137
     libpff_descriptors_index_t *descriptors_index,
1138
     libpff_io_handle_t *io_handle,
1139
     libbfio_handle_t *file_io_handle,
1140
     libpff_index_value_t *descriptors_index_value,
1141
     libcerror_error_t **error )
1142
66.0k
{
1143
66.0k
  libpff_index_value_t *existing_index_value = NULL;
1144
66.0k
  static char *function                      = "libpff_recover_analyze_descriptors_index_value";
1145
66.0k
  int result                                 = 0;
1146
1147
66.0k
  if( descriptors_index == NULL )
1148
0
  {
1149
0
    libcerror_error_set(
1150
0
     error,
1151
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1152
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1153
0
     "%s: invalid descriptors index.",
1154
0
     function );
1155
1156
0
    return( -1 );
1157
0
  }
1158
66.0k
  if( descriptors_index_value == NULL )
1159
0
  {
1160
0
    libcerror_error_set(
1161
0
     error,
1162
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1163
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1164
0
     "%s: invalid descriptors index value.",
1165
0
     function );
1166
1167
0
    return( -1 );
1168
0
  }
1169
#if defined( HAVE_DEBUG_OUTPUT )
1170
  if( libcnotify_verbose != 0 )
1171
  {
1172
    libcnotify_printf(
1173
     "%s: analyzing identifier: %" PRIu64 ", data: %" PRIu64 ", local descriptors: %" PRIu64 ", parent: %" PRIu32 "\n",
1174
     function,
1175
     descriptors_index_value->identifier,
1176
     descriptors_index_value->data_identifier,
1177
     descriptors_index_value->local_descriptors_identifier,
1178
     descriptors_index_value->parent_identifier );
1179
  }
1180
#endif
1181
  /* Check if the descriptors index value matches an existing index value
1182
   */
1183
66.0k
  result = libpff_index_get_value_by_identifier(
1184
66.0k
      descriptors_index->index,
1185
66.0k
      io_handle,
1186
66.0k
      file_io_handle,
1187
66.0k
      descriptors_index_value->identifier,
1188
66.0k
      &existing_index_value,
1189
66.0k
      error );
1190
1191
66.0k
  if( result == -1 )
1192
41
  {
1193
41
    libcerror_error_set(
1194
41
     error,
1195
41
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1196
41
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1197
41
     "%s: unable to retrieve descriptors index value: %" PRIu64 " from index.",
1198
41
     function,
1199
41
     descriptors_index_value->identifier );
1200
1201
41
    goto on_error;
1202
41
  }
1203
65.9k
  else if( result != 0 )
1204
6.40k
  {
1205
6.40k
    if( existing_index_value == NULL )
1206
0
    {
1207
0
      libcerror_error_set(
1208
0
       error,
1209
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1210
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1211
0
       "%s: missing descriptors index value for identifier: %" PRIu64 ".",
1212
0
       function,
1213
0
       descriptors_index_value->identifier );
1214
1215
0
      goto on_error;
1216
0
    }
1217
6.40k
    result = 0;
1218
1219
/* TODO what about parent changes ? */
1220
6.40k
    if( ( descriptors_index_value->data_identifier == existing_index_value->data_identifier )
1221
3.94k
     && ( descriptors_index_value->local_descriptors_identifier == existing_index_value->local_descriptors_identifier ) )
1222
2.78k
    {
1223
#if defined( HAVE_DEBUG_OUTPUT )
1224
      if( libcnotify_verbose != 0 )
1225
      {
1226
        libcnotify_printf(
1227
         "%s: deleted descriptors index value: %" PRIu64 " matches existing item value.\n",
1228
         function,
1229
         descriptors_index_value->identifier );
1230
      }
1231
#endif
1232
2.78k
      result = 1;
1233
2.78k
    }
1234
6.40k
    if( libpff_index_value_free(
1235
6.40k
         &existing_index_value,
1236
6.40k
         error ) != 1 )
1237
0
    {
1238
0
      libcerror_error_set(
1239
0
       error,
1240
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1241
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1242
0
       "%s: unable to free index value: %d.",
1243
0
       function,
1244
0
       descriptors_index_value->identifier );
1245
1246
0
      goto on_error;
1247
0
    }
1248
6.40k
    if( result != 0 )
1249
2.78k
    {
1250
2.78k
      return( 0 );
1251
2.78k
    }
1252
6.40k
  }
1253
63.1k
  result = libpff_recover_check_descriptors_index_for_recovered_value(
1254
63.1k
      descriptors_index,
1255
63.1k
      descriptors_index_value,
1256
63.1k
      error );
1257
1258
63.1k
  if( result == -1 )
1259
0
  {
1260
0
    libcerror_error_set(
1261
0
     error,
1262
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1263
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1264
0
     "%s: unable to determine if deleted descriptors index value: %" PRIu64 " was previously recovered.",
1265
0
     function,
1266
0
     descriptors_index_value->identifier );
1267
1268
0
    goto on_error;
1269
0
  }
1270
63.1k
  else if( result != 0 )
1271
32.1k
  {
1272
#if defined( HAVE_DEBUG_OUTPUT )
1273
    if( libcnotify_verbose != 0 )
1274
    {
1275
      libcnotify_printf(
1276
       "%s: deleted descriptors index value: %" PRIu64 " matches previous recovered index value.\n",
1277
       function,
1278
       descriptors_index_value->identifier );
1279
    }
1280
#endif
1281
32.1k
    return( 0 );
1282
32.1k
  }
1283
31.0k
  return( 1 );
1284
1285
41
on_error:
1286
41
  if( existing_index_value != NULL )
1287
0
  {
1288
0
    libpff_index_value_free(
1289
0
     &existing_index_value,
1290
0
     NULL );
1291
0
  }
1292
41
  return( -1 );
1293
63.1k
}
1294
1295
/* Checks if the specific descriptors index contains a specific recovered index value
1296
 * Returns 1 if exists, 0 if not or -1 on error
1297
 */
1298
int libpff_recover_check_descriptors_index_for_recovered_value(
1299
     libpff_descriptors_index_t *descriptors_index,
1300
     libpff_index_value_t *descriptors_index_value,
1301
     libcerror_error_t **error )
1302
63.1k
{
1303
63.1k
  libcdata_tree_node_t *upper_node              = NULL;
1304
63.1k
  libpff_index_value_t *index_value             = NULL;
1305
63.1k
  libpff_index_values_list_t *index_values_list = NULL;
1306
63.1k
  static char *function                         = "libpff_recover_check_descriptors_index_for_recovered_value";
1307
63.1k
  int index_value_iterator                      = 0;
1308
63.1k
  int number_of_index_values                    = 0;
1309
63.1k
  int result                                    = 0;
1310
1311
63.1k
  if( descriptors_index == NULL )
1312
0
  {
1313
0
    libcerror_error_set(
1314
0
     error,
1315
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1316
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1317
0
     "%s: invalid descriptors index.",
1318
0
     function );
1319
1320
0
    return( -1 );
1321
0
  }
1322
63.1k
  if( descriptors_index_value == NULL )
1323
0
  {
1324
0
    libcerror_error_set(
1325
0
     error,
1326
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1327
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1328
0
     "%s: invalid descriptors index value.",
1329
0
     function );
1330
1331
0
    return( -1 );
1332
0
  }
1333
63.1k
  result = libcdata_btree_get_value_by_value(
1334
63.1k
      descriptors_index->recovered_index_values_tree,
1335
63.1k
      (intptr_t *) descriptors_index_value,
1336
63.1k
      (int (*)(intptr_t *, intptr_t *, libcerror_error_t **)) &libpff_index_values_list_compare,
1337
63.1k
      &upper_node,
1338
63.1k
      (intptr_t **) &index_values_list,
1339
63.1k
      error );
1340
1341
63.1k
  if( result == -1 )
1342
0
  {
1343
0
    libcerror_error_set(
1344
0
     error,
1345
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1346
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1347
0
     "%s: unable to retrieve recovered descriptors index value: %" PRIu64 " list.",
1348
0
     function,
1349
0
     descriptors_index_value->identifier );
1350
1351
0
    return( -1 );
1352
0
  }
1353
63.1k
  else if( result != 0 )
1354
40.0k
  {
1355
40.0k
    if( libpff_index_values_list_number_of_values(
1356
40.0k
         index_values_list,
1357
40.0k
         &number_of_index_values,
1358
40.0k
         error ) != 1 )
1359
0
    {
1360
0
      libcerror_error_set(
1361
0
       error,
1362
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1363
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1364
0
       "%s: unable to retrieve number of recovered descriptors index values for identifier: %" PRIu64 ".",
1365
0
       function,
1366
0
       descriptors_index_value->identifier );
1367
1368
0
      return( -1 );
1369
0
    }
1370
40.0k
    result = 0;
1371
1372
40.0k
    for( index_value_iterator = 0;
1373
148k
         index_value_iterator < number_of_index_values;
1374
108k
         index_value_iterator++ )
1375
140k
    {
1376
140k
      if( libpff_index_values_list_get_value_by_index(
1377
140k
           index_values_list,
1378
140k
           index_value_iterator,
1379
140k
           &index_value,
1380
140k
           error ) != 1 )
1381
0
      {
1382
0
        libcerror_error_set(
1383
0
         error,
1384
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1385
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1386
0
         "%s: unable to retrieve recovered descriptors index value: %" PRIu64 " list element: %d.",
1387
0
         function,
1388
0
         descriptors_index_value->identifier,
1389
0
         index_value_iterator );
1390
1391
0
        return( -1 );
1392
0
      }
1393
140k
      if( index_value == NULL )
1394
0
      {
1395
0
        libcerror_error_set(
1396
0
         error,
1397
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1398
0
         LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1399
0
         "%s: missing recovered descriptors index value: %" PRIu64 " list element: %d.",
1400
0
         function,
1401
0
         descriptors_index_value->identifier,
1402
0
         index_value_iterator );
1403
1404
0
        return( -1 );
1405
0
      }
1406
/* TODO what about parent changes ? */
1407
140k
      if( ( descriptors_index_value->data_identifier == index_value->data_identifier )
1408
41.0k
       && ( descriptors_index_value->local_descriptors_identifier == index_value->local_descriptors_identifier ) )
1409
32.1k
      {
1410
32.1k
        result = 1;
1411
1412
32.1k
        break;
1413
32.1k
      }
1414
140k
    }
1415
40.0k
  }
1416
63.1k
  return( result );
1417
63.1k
}
1418
1419
/* Recovers descriptors index values
1420
 * Returns 1 if successful or -1 on error
1421
 */
1422
int libpff_recover_descriptors_index_values(
1423
     libpff_descriptors_index_t *descriptors_index,
1424
     libpff_io_handle_t *io_handle,
1425
     libbfio_handle_t *file_io_handle,
1426
     off64_t node_offset,
1427
     uint64_t node_back_pointer,
1428
     int recursion_depth,
1429
     libcerror_error_t **error )
1430
2.92k
{
1431
2.92k
  libpff_index_node_t *index_node   = NULL;
1432
2.92k
  libpff_index_value_t *index_value = NULL;
1433
2.92k
  uint8_t *node_entry_data          = NULL;
1434
2.92k
  static char *function             = "libpff_recover_descriptors_index_values";
1435
2.92k
  uint64_t sub_node_back_pointer    = 0;
1436
2.92k
  uint64_t sub_node_offset          = 0;
1437
2.92k
  uint16_t entry_index              = 0;
1438
2.92k
  int result                        = 0;
1439
1440
2.92k
  if( descriptors_index == NULL )
1441
0
  {
1442
0
    libcerror_error_set(
1443
0
     error,
1444
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1445
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1446
0
     "%s: invalid descriptors index.",
1447
0
     function );
1448
1449
0
    return( -1 );
1450
0
  }
1451
2.92k
  if( descriptors_index->index == NULL )
1452
0
  {
1453
0
    libcerror_error_set(
1454
0
     error,
1455
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1456
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1457
0
     "%s: invalid descriptors index - missing index.",
1458
0
     function );
1459
1460
0
    return( -1 );
1461
0
  }
1462
2.92k
  if( io_handle == NULL )
1463
0
  {
1464
0
    libcerror_error_set(
1465
0
     error,
1466
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1467
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1468
0
     "%s: invalid IO handle.",
1469
0
     function );
1470
1471
0
    return( -1 );
1472
0
  }
1473
2.92k
  if( ( recursion_depth < 0 )
1474
2.92k
   || ( recursion_depth > LIBPFF_MAXIMUM_ITEM_TREE_RECURSION_DEPTH ) )
1475
0
  {
1476
0
    libcerror_error_set(
1477
0
     error,
1478
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1479
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1480
0
     "%s: invalid recursion depth value out of bounds.",
1481
0
     function );
1482
1483
0
    return( -1 );
1484
0
  }
1485
2.92k
  if( libpff_index_node_initialize(
1486
2.92k
       &index_node,
1487
2.92k
       error ) != 1 )
1488
0
  {
1489
0
    libcerror_error_set(
1490
0
     error,
1491
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1492
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1493
0
     "%s: unable to create descriptors index node.",
1494
0
     function );
1495
1496
0
    goto on_error;
1497
0
  }
1498
2.92k
  if( libpff_index_node_read_file_io_handle(
1499
2.92k
       index_node,
1500
2.92k
       file_io_handle,
1501
2.92k
       node_offset,
1502
2.92k
       io_handle->file_type,
1503
2.92k
       error ) != 1 )
1504
0
  {
1505
0
    libcerror_error_set(
1506
0
     error,
1507
0
     LIBCERROR_ERROR_DOMAIN_IO,
1508
0
     LIBCERROR_IO_ERROR_READ_FAILED,
1509
0
     "%s: unable to read descriptors index node at offset: %" PRIi64 " (0x%08" PRIx64 ").",
1510
0
     function,
1511
0
     node_offset,
1512
0
     node_offset );
1513
1514
0
    goto on_error;
1515
0
  }
1516
2.92k
  if( index_node->level == LIBPFF_INDEX_NODE_LEVEL_LEAF )
1517
2.46k
  {
1518
2.46k
    for( entry_index = index_node->number_of_entries;
1519
42.5k
         entry_index < index_node->maximum_number_of_entries;
1520
40.0k
         entry_index++ )
1521
40.0k
    {
1522
/* TODO add if( io_handle->abort != 0 ) */
1523
40.0k
      if( libpff_index_node_get_entry_data(
1524
40.0k
           index_node,
1525
40.0k
           entry_index,
1526
40.0k
           &node_entry_data,
1527
40.0k
           error ) != 1 )
1528
0
      {
1529
0
        libcerror_error_set(
1530
0
         error,
1531
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1532
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1533
0
         "%s: unable to retrieve deleted node entry: %" PRIu16 " data.",
1534
0
         function,
1535
0
         entry_index );
1536
1537
0
        goto on_error;
1538
0
      }
1539
40.0k
      if( node_entry_data == NULL )
1540
0
      {
1541
0
        libcerror_error_set(
1542
0
         error,
1543
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1544
0
         LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1545
0
         "%s: missing deleted node entry: %" PRIu16 " data.",
1546
0
         function,
1547
0
         entry_index );
1548
1549
0
        goto on_error;
1550
0
      }
1551
40.0k
      if( libpff_index_value_initialize(
1552
40.0k
           &index_value,
1553
40.0k
           error ) != 1 )
1554
0
      {
1555
0
        libcerror_error_set(
1556
0
         error,
1557
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1558
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1559
0
         "%s: unable to create index value.",
1560
0
         function );
1561
1562
0
        goto on_error;
1563
0
      }
1564
40.0k
      if( libpff_index_value_read_data(
1565
40.0k
           index_value,
1566
40.0k
           io_handle,
1567
40.0k
           LIBPFF_INDEX_TYPE_DESCRIPTOR,
1568
40.0k
           node_entry_data,
1569
40.0k
           (size_t) index_node->entry_size,
1570
40.0k
           error ) != 1 )
1571
10
      {
1572
10
        libcerror_error_set(
1573
10
         error,
1574
10
         LIBCERROR_ERROR_DOMAIN_IO,
1575
10
         LIBCERROR_IO_ERROR_READ_FAILED,
1576
10
         "%s: unable to read index value.",
1577
10
         function );
1578
1579
10
        goto on_error;
1580
10
      }
1581
40.0k
      result = libpff_recover_analyze_descriptors_index_value(
1582
40.0k
                descriptors_index,
1583
40.0k
                io_handle,
1584
40.0k
                file_io_handle,
1585
40.0k
                index_value,
1586
40.0k
                error );
1587
1588
40.0k
      if( result == -1 )
1589
1
      {
1590
1
        libcerror_error_set(
1591
1
         error,
1592
1
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1593
1
         LIBCERROR_RUNTIME_ERROR_GENERIC,
1594
1
         "%s: unable to analyze deleted descriptors index value: %" PRIu16 ".",
1595
1
         function,
1596
1
         entry_index );
1597
1598
1
        goto on_error;
1599
1
      }
1600
40.0k
      else if( result != 0 )
1601
11.6k
      {
1602
        /* Add the recovered descriptors index values to the index tree
1603
         */
1604
#if defined( HAVE_DEBUG_OUTPUT )
1605
        if( libcnotify_verbose != 0 )
1606
        {
1607
          libcnotify_printf(
1608
           "%s: descriptor index value: %" PRIu16 " identifier: %" PRIu64 " is recoverable.\n",
1609
           function,
1610
           entry_index,
1611
           index_value->identifier );
1612
        }
1613
#endif
1614
11.6k
        if( libpff_descriptors_index_insert_recovered_index_value(
1615
11.6k
             descriptors_index,
1616
11.6k
             index_value,
1617
11.6k
             error ) != 1 )
1618
0
        {
1619
0
          libcerror_error_set(
1620
0
           error,
1621
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1622
0
           LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1623
0
           "%s: unable to insert recovered descriptors index value: %" PRIu64 " list.",
1624
0
           function,
1625
0
           index_value->identifier );
1626
1627
0
          goto on_error;
1628
0
        }
1629
11.6k
        index_value = NULL;
1630
11.6k
      }
1631
28.3k
      else
1632
28.3k
      {
1633
28.3k
        if( libpff_index_value_free(
1634
28.3k
             &index_value,
1635
28.3k
             error ) != 1 )
1636
0
        {
1637
0
          libcerror_error_set(
1638
0
           error,
1639
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1640
0
           LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1641
0
           "%s: unable to free index value.",
1642
0
           function );
1643
1644
0
          goto on_error;
1645
0
        }
1646
28.3k
      }
1647
40.0k
    }
1648
2.46k
  }
1649
453
  else
1650
453
  {
1651
453
    if( node_back_pointer != index_node->back_pointer )
1652
184
    {
1653
184
      libcerror_error_set(
1654
184
       error,
1655
184
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1656
184
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1657
184
       "%s: back pointer mismatch (entry: %" PRIu64 ", node: %" PRIu64 ").",
1658
184
       function,
1659
184
       node_back_pointer,
1660
184
       index_node->back_pointer );
1661
1662
184
      goto on_error;
1663
184
    }
1664
269
    for( entry_index = 0;
1665
2.25k
         entry_index < index_node->number_of_entries;
1666
1.98k
         entry_index++ )
1667
1.99k
    {
1668
/* TODO add if( io_handle->abort != 0 ) */
1669
1.99k
      if( libpff_index_node_get_entry_data(
1670
1.99k
           index_node,
1671
1.99k
           entry_index,
1672
1.99k
           &node_entry_data,
1673
1.99k
           error ) != 1 )
1674
0
      {
1675
0
        libcerror_error_set(
1676
0
         error,
1677
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1678
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1679
0
         "%s: unable to retrieve node entry: %" PRIu16 " data.",
1680
0
         function,
1681
0
         entry_index );
1682
1683
0
        goto on_error;
1684
0
      }
1685
1.99k
      if( node_entry_data == NULL )
1686
0
      {
1687
0
        libcerror_error_set(
1688
0
         error,
1689
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1690
0
         LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1691
0
         "%s: missing node entry: %" PRIu16 " data.",
1692
0
         function,
1693
0
         entry_index );
1694
1695
0
        goto on_error;
1696
0
      }
1697
1.99k
      if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
1698
296
      {
1699
296
        byte_stream_copy_to_uint32_little_endian(
1700
296
         ( (pff_index_node_branch_entry_32bit_t *) node_entry_data )->file_offset,
1701
296
         sub_node_offset );
1702
1703
296
        byte_stream_copy_to_uint32_little_endian(
1704
296
         ( (pff_index_node_branch_entry_32bit_t *) node_entry_data )->back_pointer,
1705
296
         sub_node_back_pointer );
1706
296
      }
1707
1.69k
      else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
1708
170
            || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
1709
1.69k
      {
1710
1.69k
        byte_stream_copy_to_uint64_little_endian(
1711
1.69k
         ( (pff_index_node_branch_entry_64bit_t *) node_entry_data )->file_offset,
1712
1.69k
         sub_node_offset );
1713
1714
1.69k
        byte_stream_copy_to_uint64_little_endian(
1715
1.69k
         ( (pff_index_node_branch_entry_64bit_t *) node_entry_data )->back_pointer,
1716
1.69k
         sub_node_back_pointer );
1717
1.69k
      }
1718
#if defined( HAVE_DEBUG_OUTPUT )
1719
      if( libcnotify_verbose != 0 )
1720
      {
1721
        libcnotify_printf(
1722
         "%s: node entry: %" PRIu16 " sub node offset\t: %" PRIi64 " (0x%08" PRIx64 ")\n",
1723
         function,
1724
         entry_index,
1725
         sub_node_offset,
1726
         sub_node_offset );
1727
      }
1728
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
1729
1730
1.99k
      if( libpff_recover_descriptors_index_values(
1731
1.99k
           descriptors_index,
1732
1.99k
           io_handle,
1733
1.99k
           file_io_handle,
1734
1.99k
           sub_node_offset,
1735
1.99k
           sub_node_back_pointer,
1736
1.99k
           recursion_depth + 1,
1737
1.99k
           error ) != 1 )
1738
7
      {
1739
7
        libcerror_error_set(
1740
7
         error,
1741
7
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1742
7
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1743
7
         "%s: unable to analyze descriptors index node at offset: %" PRIi64 " (0x%08" PRIx64 ").",
1744
7
         function,
1745
7
         sub_node_offset,
1746
7
         sub_node_offset );
1747
1748
7
        goto on_error;
1749
7
      }
1750
1.99k
    }
1751
269
  }
1752
2.71k
  if( libpff_index_node_free(
1753
2.71k
       &index_node,
1754
2.71k
       error ) != 1 )
1755
0
  {
1756
0
    libcerror_error_set(
1757
0
     error,
1758
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1759
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1760
0
     "%s: unable to free index node.",
1761
0
     function );
1762
1763
0
    goto on_error;
1764
0
  }
1765
2.71k
  return( 1 );
1766
1767
202
on_error:
1768
202
  if( index_value != NULL )
1769
11
  {
1770
11
    libpff_index_value_free(
1771
11
     &index_value,
1772
11
     NULL );
1773
11
  }
1774
202
  if( index_node != NULL )
1775
202
  {
1776
202
    libpff_index_node_free(
1777
202
     &index_node,
1778
202
     NULL );
1779
202
  }
1780
202
  return( -1 );
1781
2.71k
}
1782
1783
/* Analyze if a specific offsets index value is recoverable
1784
 * Returns 1 if recoverable, 0 if not or -1 on error
1785
 */
1786
int libpff_recover_analyze_offsets_index_value(
1787
     libpff_offsets_index_t *offsets_index,
1788
     libpff_io_handle_t *io_handle,
1789
     libbfio_handle_t *file_io_handle,
1790
     libpff_index_value_t *offsets_index_value,
1791
     uint32_t maximum_data_block_data_size,
1792
     libcerror_error_t **error )
1793
38.0k
{
1794
38.0k
  libpff_index_value_t *existing_index_value = NULL;
1795
38.0k
  static char *function                      = "libpff_recover_analyze_offsets_index_value";
1796
38.0k
  int result                                 = 0;
1797
1798
38.0k
  if( offsets_index == NULL )
1799
0
  {
1800
0
    libcerror_error_set(
1801
0
     error,
1802
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1803
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1804
0
     "%s: invalid offsets index.",
1805
0
     function );
1806
1807
0
    return( -1 );
1808
0
  }
1809
38.0k
  if( offsets_index_value == NULL )
1810
0
  {
1811
0
    libcerror_error_set(
1812
0
     error,
1813
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1814
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1815
0
     "%s: invalid offsets index value.",
1816
0
     function );
1817
1818
0
    return( -1 );
1819
0
  }
1820
#if defined( HAVE_DEBUG_OUTPUT )
1821
  if( libcnotify_verbose != 0 )
1822
  {
1823
    libcnotify_printf(
1824
     "%s: analyzing identifier: %" PRIu64 " (%s) at offset: %" PRIi64 " of size: %" PRIu32 "\n",
1825
     function,
1826
     offsets_index_value->identifier,
1827
     ( ( offsets_index_value->identifier & LIBPFF_OFFSET_INDEX_IDENTIFIER_FLAG_INTERNAL ) ? "internal" : "external" ),
1828
     offsets_index_value->file_offset,
1829
     offsets_index_value->data_size );
1830
  }
1831
#endif
1832
  /* Ignore index values without a valid file offset
1833
   */
1834
38.0k
  if( offsets_index_value->file_offset <= 0 )
1835
1.59k
  {
1836
#if defined( HAVE_DEBUG_OUTPUT )
1837
    if( libcnotify_verbose != 0 )
1838
    {
1839
      libcnotify_printf(
1840
       "%s: deleted offsets index value: %" PRIu64 " has an invalid file offset: %" PRIi64 ".\n",
1841
       function,
1842
       offsets_index_value->identifier,
1843
       offsets_index_value->file_offset );
1844
    }
1845
#endif
1846
1.59k
    return( 0 );
1847
1.59k
  }
1848
  /* Ignore index values without a valid data size
1849
   */
1850
36.4k
  if( ( offsets_index_value->data_size == 0 )
1851
36.4k
   || ( (uint32_t) offsets_index_value->data_size > maximum_data_block_data_size ) )
1852
0
  {
1853
#if defined( HAVE_DEBUG_OUTPUT )
1854
    if( libcnotify_verbose != 0 )
1855
    {
1856
      libcnotify_printf(
1857
       "%s: deleted offsets index value: %" PRIu64 " has an invalid data size: %" PRIu32 ".\n",
1858
       function,
1859
       offsets_index_value->identifier,
1860
       offsets_index_value->data_size );
1861
    }
1862
#endif
1863
0
    return( 0 );
1864
0
  }
1865
  /* Check if the offsets index value matches an existing index value
1866
   */
1867
36.4k
  result = libpff_index_get_value_by_identifier(
1868
36.4k
      offsets_index->index,
1869
36.4k
      io_handle,
1870
36.4k
      file_io_handle,
1871
36.4k
      offsets_index_value->identifier,
1872
36.4k
      &existing_index_value,
1873
36.4k
      error );
1874
1875
36.4k
  if( result == -1 )
1876
89
  {
1877
89
    libcerror_error_set(
1878
89
     error,
1879
89
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1880
89
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1881
89
     "%s: unable to retrieve offsets index value for identifier: %" PRIu64 ".",
1882
89
     function,
1883
89
     offsets_index_value->identifier );
1884
1885
89
    goto on_error;
1886
89
  }
1887
36.3k
  else if( result != 0 )
1888
13.6k
  {
1889
13.6k
    if( existing_index_value == NULL )
1890
0
    {
1891
0
      libcerror_error_set(
1892
0
       error,
1893
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1894
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1895
0
       "%s: missing offsets index value for identifier: %" PRIu64 ".",
1896
0
       function,
1897
0
       offsets_index_value->identifier );
1898
1899
0
      goto on_error;
1900
0
    }
1901
13.6k
    result = 0;
1902
1903
13.6k
    if( ( offsets_index_value->file_offset == existing_index_value->file_offset )
1904
11.9k
     && ( offsets_index_value->data_size == existing_index_value->data_size ) )
1905
11.0k
    {
1906
#if defined( HAVE_DEBUG_OUTPUT )
1907
      if( libcnotify_verbose != 0 )
1908
      {
1909
        libcnotify_printf(
1910
         "%s: deleted offsets index value: %" PRIu64 " matches existing item value.\n",
1911
         function,
1912
         offsets_index_value->identifier );
1913
      }
1914
#endif
1915
11.0k
      result = 1;
1916
11.0k
    }
1917
13.6k
    if( libpff_index_value_free(
1918
13.6k
         &existing_index_value,
1919
13.6k
         error ) != 1 )
1920
0
    {
1921
0
      libcerror_error_set(
1922
0
       error,
1923
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1924
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1925
0
       "%s: unable to free index value: %d.",
1926
0
       function,
1927
0
       offsets_index_value->identifier );
1928
1929
0
      goto on_error;
1930
0
    }
1931
13.6k
    if( result != 0 )
1932
11.0k
    {
1933
11.0k
      return( 0 );
1934
11.0k
    }
1935
13.6k
  }
1936
  /* Check if the offsets index value matches a previously recovered index value
1937
   */
1938
25.3k
  result = libpff_recover_check_offsets_index_for_recovered_value(
1939
25.3k
      offsets_index,
1940
25.3k
      offsets_index_value,
1941
25.3k
      error );
1942
1943
25.3k
  if( result == -1 )
1944
0
  {
1945
0
    libcerror_error_set(
1946
0
     error,
1947
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1948
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1949
0
     "%s: unable to determine if deleted offsets index value: %" PRIu64 " was previously recovered.",
1950
0
     function,
1951
0
     offsets_index_value->identifier );
1952
1953
0
    goto on_error;
1954
0
  }
1955
25.3k
  else if( result != 0 )
1956
9.52k
  {
1957
#if defined( HAVE_DEBUG_OUTPUT )
1958
    if( libcnotify_verbose != 0 )
1959
    {
1960
      libcnotify_printf(
1961
       "%s: deleted offsets index value: %" PRIu64 " matches previous recovered index value.\n",
1962
       function,
1963
       offsets_index_value->identifier );
1964
    }
1965
#endif
1966
9.52k
    return( 0 );
1967
9.52k
  }
1968
15.7k
  return( 1 );
1969
1970
89
on_error:
1971
89
  if( existing_index_value != NULL )
1972
0
  {
1973
0
    libpff_index_value_free(
1974
0
     &existing_index_value,
1975
0
     NULL );
1976
0
  }
1977
89
  return( -1 );
1978
25.3k
}
1979
1980
/* Checks if the specific offsets index contains a specific recovered index value
1981
 * Returns 1 if exists, 0 if not or -1 on error
1982
 */
1983
int libpff_recover_check_offsets_index_for_recovered_value(
1984
     libpff_offsets_index_t *offsets_index,
1985
     libpff_index_value_t *offsets_index_value,
1986
     libcerror_error_t **error )
1987
25.3k
{
1988
25.3k
  libcdata_tree_node_t *upper_node              = NULL;
1989
25.3k
  libpff_index_value_t *index_value             = NULL;
1990
25.3k
  libpff_index_values_list_t *index_values_list = NULL;
1991
25.3k
  static char *function                         = "libpff_recover_check_offsets_index_for_recovered_value";
1992
25.3k
  int index_value_iterator                      = 0;
1993
25.3k
  int number_of_index_values                    = 0;
1994
25.3k
  int result                                    = 0;
1995
1996
25.3k
  if( offsets_index == NULL )
1997
0
  {
1998
0
    libcerror_error_set(
1999
0
     error,
2000
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2001
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2002
0
     "%s: invalid offsets index.",
2003
0
     function );
2004
2005
0
    return( -1 );
2006
0
  }
2007
25.3k
  if( offsets_index_value == NULL )
2008
0
  {
2009
0
    libcerror_error_set(
2010
0
     error,
2011
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2012
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2013
0
     "%s: invalid offsets index value.",
2014
0
     function );
2015
2016
0
    return( -1 );
2017
0
  }
2018
25.3k
  result = libcdata_btree_get_value_by_value(
2019
25.3k
      offsets_index->recovered_index_values_tree,
2020
25.3k
      (intptr_t *) offsets_index_value,
2021
25.3k
      (int (*)(intptr_t *, intptr_t *, libcerror_error_t **)) &libpff_index_values_list_compare,
2022
25.3k
      &upper_node,
2023
25.3k
      (intptr_t **) &index_values_list,
2024
25.3k
      error );
2025
2026
25.3k
  if( result == -1 )
2027
0
  {
2028
0
    libcerror_error_set(
2029
0
     error,
2030
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2031
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2032
0
     "%s: unable to retrieve recovered offsets index value: %" PRIu64 " list.",
2033
0
     function,
2034
0
     offsets_index_value->identifier );
2035
2036
0
    return( -1 );
2037
0
  }
2038
25.3k
  else if( result != 0 )
2039
10.5k
  {
2040
10.5k
    if( libpff_index_values_list_number_of_values(
2041
10.5k
         index_values_list,
2042
10.5k
         &number_of_index_values,
2043
10.5k
         error ) != 1 )
2044
0
    {
2045
0
      libcerror_error_set(
2046
0
       error,
2047
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2048
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2049
0
       "%s: unable to retrieve number of recovered offsets index values for identifier: %" PRIu64 ".",
2050
0
       function,
2051
0
       offsets_index_value->identifier );
2052
2053
0
      return( -1 );
2054
0
    }
2055
10.5k
    result = 0;
2056
2057
10.5k
    for( index_value_iterator = 0;
2058
15.8k
         index_value_iterator < number_of_index_values;
2059
10.5k
         index_value_iterator++ )
2060
14.8k
    {
2061
14.8k
      if( libpff_index_values_list_get_value_by_index(
2062
14.8k
           index_values_list,
2063
14.8k
           index_value_iterator,
2064
14.8k
           &index_value,
2065
14.8k
           error ) != 1 )
2066
0
      {
2067
0
        libcerror_error_set(
2068
0
         error,
2069
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2070
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2071
0
         "%s: unable to retrieve recovered offsets index value: %" PRIu64 " list element: %d.",
2072
0
         function,
2073
0
         offsets_index_value->identifier,
2074
0
         index_value_iterator );
2075
2076
0
        return( -1 );
2077
0
      }
2078
14.8k
      if( index_value == NULL )
2079
0
      {
2080
0
        libcerror_error_set(
2081
0
         error,
2082
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2083
0
         LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2084
0
         "%s: missing recovered offsets index value: %" PRIu64 " list element: %d.",
2085
0
         function,
2086
0
         offsets_index_value->identifier,
2087
0
         index_value_iterator );
2088
2089
0
        return( -1 );
2090
0
      }
2091
14.8k
      if( ( offsets_index_value->file_offset == index_value->file_offset )
2092
10.4k
       && ( offsets_index_value->data_size == index_value->data_size ) )
2093
9.52k
      {
2094
9.52k
        result = 1;
2095
2096
9.52k
        break;
2097
9.52k
      }
2098
14.8k
    }
2099
10.5k
  }
2100
25.3k
  return( result );
2101
25.3k
}
2102
2103
/* Scans an offsets index node for recoverable index nodes
2104
 * Returns 1 if successful or -1 on error
2105
 */
2106
int libpff_recover_analyze_offsets_index_node(
2107
     libpff_offsets_index_t *offsets_index,
2108
     libpff_io_handle_t *io_handle,
2109
     libbfio_handle_t *file_io_handle,
2110
     off64_t node_offset,
2111
     uint64_t node_back_pointer,
2112
     uint32_t maximum_data_block_data_size,
2113
     int recursion_depth,
2114
     libcerror_error_t **error )
2115
0
{
2116
0
  libpff_index_node_t *index_node   = NULL;
2117
0
  libpff_index_value_t *index_value = NULL;
2118
0
  uint8_t *node_entry_data          = NULL;
2119
0
  static char *function             = "libpff_recover_analyze_offsets_index_node";
2120
0
  uint64_t sub_node_back_pointer    = 0;
2121
0
  uint64_t sub_node_offset          = 0;
2122
0
  uint16_t entry_index              = 0;
2123
0
  int result                        = 0;
2124
2125
0
  if( offsets_index == NULL )
2126
0
  {
2127
0
    libcerror_error_set(
2128
0
     error,
2129
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2130
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2131
0
     "%s: invalid offsets index.",
2132
0
     function );
2133
2134
0
    return( -1 );
2135
0
  }
2136
0
  if( offsets_index->index == NULL )
2137
0
  {
2138
0
    libcerror_error_set(
2139
0
     error,
2140
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2141
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2142
0
     "%s: invalid offsets index - missing index.",
2143
0
     function );
2144
2145
0
    return( -1 );
2146
0
  }
2147
0
  if( io_handle == NULL )
2148
0
  {
2149
0
    libcerror_error_set(
2150
0
     error,
2151
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2152
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2153
0
     "%s: invalid IO handle.",
2154
0
     function );
2155
2156
0
    return( -1 );
2157
0
  }
2158
0
  if( ( recursion_depth < 0 )
2159
0
   || ( recursion_depth > LIBPFF_MAXIMUM_ITEM_TREE_RECURSION_DEPTH ) )
2160
0
  {
2161
0
    libcerror_error_set(
2162
0
     error,
2163
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2164
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2165
0
     "%s: invalid recursion depth value out of bounds.",
2166
0
     function );
2167
2168
0
    return( -1 );
2169
0
  }
2170
0
  if( libpff_index_node_initialize(
2171
0
       &index_node,
2172
0
       error ) != 1 )
2173
0
  {
2174
0
    libcerror_error_set(
2175
0
     error,
2176
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2177
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2178
0
     "%s: unable to create offsets index node.",
2179
0
     function );
2180
2181
0
    goto on_error;
2182
0
  }
2183
0
  if( libpff_index_node_read_file_io_handle(
2184
0
       index_node,
2185
0
       file_io_handle,
2186
0
       node_offset,
2187
0
       io_handle->file_type,
2188
0
       error ) != 1 )
2189
0
  {
2190
0
    libcerror_error_set(
2191
0
     error,
2192
0
     LIBCERROR_ERROR_DOMAIN_IO,
2193
0
     LIBCERROR_IO_ERROR_READ_FAILED,
2194
0
     "%s: unable to read offsets index node at offset: %" PRIi64 " (0x%08" PRIx64 ").",
2195
0
     function,
2196
0
     node_offset,
2197
0
     node_offset );
2198
2199
0
    goto on_error;
2200
0
  }
2201
0
  if( index_node->level == LIBPFF_INDEX_NODE_LEVEL_LEAF )
2202
0
  {
2203
0
    for( entry_index = index_node->number_of_entries;
2204
0
         entry_index < index_node->maximum_number_of_entries;
2205
0
         entry_index++ )
2206
0
    {
2207
/* TODO add if( io_handle->abort != 0 ) */
2208
0
      if( libpff_index_node_get_entry_data(
2209
0
           index_node,
2210
0
           entry_index,
2211
0
           &node_entry_data,
2212
0
           error ) != 1 )
2213
0
      {
2214
0
        libcerror_error_set(
2215
0
         error,
2216
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2217
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2218
0
         "%s: unable to retrieve deleted node entry: %" PRIu16 " data.",
2219
0
         function,
2220
0
         entry_index );
2221
2222
0
        goto on_error;
2223
0
      }
2224
0
      if( node_entry_data == NULL )
2225
0
      {
2226
0
        libcerror_error_set(
2227
0
         error,
2228
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2229
0
         LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2230
0
         "%s: missing deleted node entry: %" PRIu16 " data.",
2231
0
         function,
2232
0
         entry_index );
2233
2234
0
        goto on_error;
2235
0
      }
2236
0
      if( libpff_index_value_initialize(
2237
0
           &index_value,
2238
0
           error ) != 1 )
2239
0
      {
2240
0
        libcerror_error_set(
2241
0
         error,
2242
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2243
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2244
0
         "%s: unable to create index value.",
2245
0
         function );
2246
2247
0
        goto on_error;
2248
0
      }
2249
0
      if( libpff_index_value_read_data(
2250
0
           index_value,
2251
0
           io_handle,
2252
0
           LIBPFF_INDEX_TYPE_OFFSET,
2253
0
           node_entry_data,
2254
0
           (size_t) index_node->entry_size,
2255
0
           error ) != 1 )
2256
0
      {
2257
0
        libcerror_error_set(
2258
0
         error,
2259
0
         LIBCERROR_ERROR_DOMAIN_IO,
2260
0
         LIBCERROR_IO_ERROR_READ_FAILED,
2261
0
         "%s: unable to read index value.",
2262
0
         function );
2263
2264
0
        goto on_error;
2265
0
      }
2266
0
      result = libpff_recover_analyze_offsets_index_value(
2267
0
                offsets_index,
2268
0
                io_handle,
2269
0
                file_io_handle,
2270
0
                index_value,
2271
0
                maximum_data_block_data_size,
2272
0
                error );
2273
2274
0
      if( result == -1 )
2275
0
      {
2276
0
        libcerror_error_set(
2277
0
         error,
2278
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2279
0
         LIBCERROR_RUNTIME_ERROR_GENERIC,
2280
0
         "%s: unable to analyze deleted offsets index value: %" PRIu16 ".",
2281
0
         function,
2282
0
         entry_index );
2283
2284
0
        goto on_error;
2285
0
      }
2286
0
      else if( result != 0 )
2287
0
      {
2288
        /* Add the recovered offsets index values to the index tree
2289
         */
2290
#if defined( HAVE_DEBUG_OUTPUT )
2291
        if( libcnotify_verbose != 0 )
2292
        {
2293
          libcnotify_printf(
2294
           "%s: offsets index value: %" PRIu16 " identifier: %" PRIu64 " is recoverable.\n",
2295
           function,
2296
           entry_index,
2297
           index_value->identifier );
2298
        }
2299
#endif
2300
0
        if( libpff_offsets_index_insert_recovered_index_value(
2301
0
             offsets_index,
2302
0
             index_value,
2303
0
             error ) != 1 )
2304
0
        {
2305
0
          libcerror_error_set(
2306
0
           error,
2307
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
2308
0
           LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2309
0
           "%s: unable to insert recovered offsets index value: %" PRIu64 " list.",
2310
0
           function,
2311
0
           index_value->identifier );
2312
2313
0
          goto on_error;
2314
0
        }
2315
0
        index_value = NULL;
2316
0
      }
2317
0
      else
2318
0
      {
2319
0
        if( libpff_index_value_free(
2320
0
             &index_value,
2321
0
             error ) != 1 )
2322
0
        {
2323
0
          libcerror_error_set(
2324
0
           error,
2325
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
2326
0
           LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
2327
0
           "%s: unable to free index value.",
2328
0
           function );
2329
2330
0
          goto on_error;
2331
0
        }
2332
0
      }
2333
0
    }
2334
0
  }
2335
0
  else
2336
0
  {
2337
0
    if( node_back_pointer != index_node->back_pointer )
2338
0
    {
2339
0
      libcerror_error_set(
2340
0
       error,
2341
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2342
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2343
0
       "%s: back pointer mismatch (entry: %" PRIu64 ", node: %" PRIu64 ").",
2344
0
       function,
2345
0
       node_back_pointer,
2346
0
       index_node->back_pointer );
2347
2348
0
      goto on_error;
2349
0
    }
2350
0
    for( entry_index = 0;
2351
0
         entry_index < index_node->number_of_entries;
2352
0
         entry_index++ )
2353
0
    {
2354
/* TODO add if( io_handle->abort != 0 ) */
2355
0
      if( libpff_index_node_get_entry_data(
2356
0
           index_node,
2357
0
           entry_index,
2358
0
           &node_entry_data,
2359
0
           error ) != 1 )
2360
0
      {
2361
0
        libcerror_error_set(
2362
0
         error,
2363
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2364
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2365
0
         "%s: unable to retrieve node entry: %" PRIu16 " data.",
2366
0
         function,
2367
0
         entry_index );
2368
2369
0
        goto on_error;
2370
0
      }
2371
0
      if( node_entry_data == NULL )
2372
0
      {
2373
0
        libcerror_error_set(
2374
0
         error,
2375
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2376
0
         LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2377
0
         "%s: missing node entry: %" PRIu16 " data.",
2378
0
         function,
2379
0
         entry_index );
2380
2381
0
        goto on_error;
2382
0
      }
2383
0
      if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
2384
0
      {
2385
0
        byte_stream_copy_to_uint32_little_endian(
2386
0
         ( (pff_index_node_branch_entry_32bit_t *) node_entry_data )->file_offset,
2387
0
         sub_node_offset );
2388
2389
0
        byte_stream_copy_to_uint32_little_endian(
2390
0
         ( (pff_index_node_branch_entry_32bit_t *) node_entry_data )->back_pointer,
2391
0
         sub_node_back_pointer );
2392
0
      }
2393
0
      else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
2394
0
            || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
2395
0
      {
2396
0
        byte_stream_copy_to_uint64_little_endian(
2397
0
         ( (pff_index_node_branch_entry_64bit_t *) node_entry_data )->file_offset,
2398
0
         sub_node_offset );
2399
2400
0
        byte_stream_copy_to_uint64_little_endian(
2401
0
         ( (pff_index_node_branch_entry_64bit_t *) node_entry_data )->back_pointer,
2402
0
         sub_node_back_pointer );
2403
0
      }
2404
#if defined( HAVE_DEBUG_OUTPUT )
2405
      if( libcnotify_verbose != 0 )
2406
      {
2407
        libcnotify_printf(
2408
         "%s: node entry: %" PRIu16 " sub node offset\t: %" PRIi64 " (0x%08" PRIx64 ")\n",
2409
         function,
2410
         entry_index,
2411
         sub_node_offset,
2412
         sub_node_offset );
2413
      }
2414
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
2415
2416
0
      if( libpff_recover_analyze_offsets_index_node(
2417
0
           offsets_index,
2418
0
           io_handle,
2419
0
           file_io_handle,
2420
0
           sub_node_offset,
2421
0
           sub_node_back_pointer,
2422
0
           maximum_data_block_data_size,
2423
0
           recursion_depth + 1,
2424
0
           error ) != 1 )
2425
0
      {
2426
0
        libcerror_error_set(
2427
0
         error,
2428
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2429
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2430
0
         "%s: unable to analyze offsets index node at offset: %" PRIi64 " (0x%08" PRIx64 ").",
2431
0
         function,
2432
0
         sub_node_offset,
2433
0
         sub_node_offset );
2434
2435
0
        goto on_error;
2436
0
      }
2437
0
    }
2438
0
  }
2439
0
  if( libpff_index_node_free(
2440
0
       &index_node,
2441
0
       error ) != 1 )
2442
0
  {
2443
0
    libcerror_error_set(
2444
0
     error,
2445
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2446
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
2447
0
     "%s: unable to free index node.",
2448
0
     function );
2449
2450
0
    goto on_error;
2451
0
  }
2452
0
  return( 1 );
2453
2454
0
on_error:
2455
0
  if( index_value != NULL )
2456
0
  {
2457
0
    libpff_index_value_free(
2458
0
     &index_value,
2459
0
     NULL );
2460
0
  }
2461
0
  if( index_node != NULL )
2462
0
  {
2463
0
    libpff_index_node_free(
2464
0
     &index_node,
2465
0
     NULL );
2466
0
  }
2467
0
  return( -1 );
2468
0
}
2469
2470
/* Scans for recoverable data blocks
2471
 * Returns 1 if successful or -1 on error
2472
 */
2473
int libpff_recover_data_blocks(
2474
     libpff_io_handle_t *io_handle,
2475
     libbfio_handle_t *file_io_handle,
2476
     libpff_descriptors_index_t *descriptors_index,
2477
     libpff_offsets_index_t *offsets_index,
2478
     libcdata_range_list_t *unallocated_data_block_list,
2479
     libcdata_range_list_t *unallocated_page_block_list,
2480
     uint8_t recovery_flags,
2481
     libcerror_error_t **error )
2482
1.05k
{
2483
1.05k
  uint8_t *block_buffer                   = NULL;
2484
1.05k
  uint8_t *data_block_footer              = NULL;
2485
1.05k
  intptr_t *value                         = NULL;
2486
1.05k
  static char *function                   = "libpff_recover_data_blocks";
2487
1.05k
  off64_t block_buffer_data_offset        = 0;
2488
1.05k
  off64_t block_offset                    = 0;
2489
1.05k
  off64_t data_block_offset               = 0;
2490
1.05k
  off64_t page_block_offset               = 0;
2491
1.05k
  size64_t block_size                     = 0;
2492
1.05k
  size64_t data_block_size                = 0;
2493
1.05k
  size64_t page_block_size                = 0;
2494
1.05k
  size_t block_buffer_offset              = 0;
2495
1.05k
  size_t block_buffer_size_available      = 0;
2496
1.05k
  size_t data_block_data_offset           = 0;
2497
1.05k
  size_t read_size                        = 0;
2498
1.05k
  ssize_t read_count                      = 0;
2499
1.05k
  uint64_t data_block_back_pointer        = 0;
2500
1.05k
  uint32_t data_block_calculated_checksum = 0;
2501
1.05k
  uint32_t data_block_stored_checksum     = 0;
2502
1.05k
  uint32_t maximum_data_block_size        = 0;
2503
1.05k
  uint16_t data_block_data_size           = 0;
2504
1.05k
  uint16_t format_data_block_size         = 0;
2505
1.05k
  uint16_t format_page_block_size         = 0;
2506
1.05k
  uint16_t scan_block_size                = 0;
2507
1.05k
  uint8_t supported_recovery_flags        = 0;
2508
1.05k
  int number_of_unallocated_data_blocks   = 0;
2509
1.05k
  int number_of_unallocated_page_blocks   = 0;
2510
1.05k
  int result                              = 0;
2511
1.05k
  int unallocated_data_block_index        = 0;
2512
1.05k
  int unallocated_page_block_index        = 0;
2513
2514
1.05k
  if( io_handle == NULL )
2515
0
  {
2516
0
    libcerror_error_set(
2517
0
     error,
2518
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2519
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2520
0
     "%s: invalid IO handle.",
2521
0
     function );
2522
2523
0
    return( -1 );
2524
0
  }
2525
1.05k
  if( ( io_handle->file_type != LIBPFF_FILE_TYPE_32BIT )
2526
559
   && ( io_handle->file_type != LIBPFF_FILE_TYPE_64BIT )
2527
185
   && ( io_handle->file_type != LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
2528
0
  {
2529
0
    libcerror_error_set(
2530
0
     error,
2531
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2532
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2533
0
     "%s: unsupported file type.",
2534
0
     function );
2535
2536
0
    return( -1 );
2537
0
  }
2538
1.05k
  if( ( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
2539
559
   || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT ) )
2540
871
  {
2541
871
    format_data_block_size  = 64;
2542
871
    format_page_block_size  = 512;
2543
871
  }
2544
185
  else
2545
185
  {
2546
185
    format_data_block_size  = 512;
2547
185
    format_page_block_size  = 4096;
2548
185
  }
2549
1.05k
  if( ( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
2550
559
   || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT ) )
2551
871
  {
2552
871
    maximum_data_block_size = 8192;
2553
871
  }
2554
185
  else
2555
185
  {
2556
/* TODO: this value is currently assumed based on the 512 x 8 = 4k page */
2557
185
    maximum_data_block_size = 65536;
2558
185
  }
2559
1.05k
  supported_recovery_flags = LIBPFF_RECOVERY_FLAG_IGNORE_ALLOCATION_DATA
2560
1.05k
                           | LIBPFF_RECOVERY_FLAG_SCAN_FOR_FRAGMENTS;
2561
2562
1.05k
  if( ( recovery_flags & ~( supported_recovery_flags ) ) != 0 )
2563
0
  {
2564
0
    libcerror_error_set(
2565
0
     error,
2566
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2567
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2568
0
     "%s: unsupported recovery flags.",
2569
0
     function );
2570
2571
0
    return( -1 );
2572
0
  }
2573
  /* Scan the unallocated page block list or all blocks for index nodes
2574
   */
2575
1.05k
  if( ( recovery_flags & LIBPFF_RECOVERY_FLAG_IGNORE_ALLOCATION_DATA ) == 0 )
2576
0
  {
2577
0
    if( libcdata_range_list_get_number_of_elements(
2578
0
         unallocated_data_block_list,
2579
0
         &number_of_unallocated_data_blocks,
2580
0
         error ) != 1 )
2581
0
    {
2582
0
      libcerror_error_set(
2583
0
       error,
2584
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2585
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2586
0
       "%s: unable to retrieve number of unallocated data blocks.",
2587
0
       function );
2588
2589
0
      goto on_error;
2590
0
    }
2591
0
    if( ( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
2592
0
     || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT ) )
2593
0
    {
2594
0
      if( libcdata_range_list_get_number_of_elements(
2595
0
           unallocated_page_block_list,
2596
0
           &number_of_unallocated_page_blocks,
2597
0
           error ) != 1 )
2598
0
      {
2599
0
        libcerror_error_set(
2600
0
         error,
2601
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
2602
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2603
0
         "%s: unable to retrieve number of unallocated page blocks.",
2604
0
         function );
2605
2606
0
        goto on_error;
2607
0
      }
2608
0
    }
2609
0
    else
2610
0
    {
2611
0
      number_of_unallocated_page_blocks = 0;
2612
0
    }
2613
0
  }
2614
1.05k
  if( ( recovery_flags & LIBPFF_RECOVERY_FLAG_SCAN_FOR_FRAGMENTS ) == 0 )
2615
1.05k
  {
2616
1.05k
    scan_block_size = format_page_block_size;
2617
1.05k
  }
2618
0
  else
2619
0
  {
2620
0
    scan_block_size = format_data_block_size;
2621
0
  }
2622
1.05k
  block_buffer = (uint8_t *) memory_allocate(
2623
1.05k
                              sizeof( uint8_t ) * ( maximum_data_block_size * 2 ) );
2624
2625
1.05k
  if( block_buffer == NULL )
2626
0
  {
2627
0
    libcerror_error_set(
2628
0
     error,
2629
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2630
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
2631
0
     "%s: unable to create block buffer.",
2632
0
     function );
2633
2634
0
    goto on_error;
2635
0
  }
2636
1.05k
  if( ( number_of_unallocated_data_blocks > 0 )
2637
1.05k
   || ( number_of_unallocated_page_blocks > 0 )
2638
1.05k
   || ( ( recovery_flags & LIBPFF_RECOVERY_FLAG_IGNORE_ALLOCATION_DATA ) != 0 ) )
2639
1.05k
  {
2640
1.05k
    block_offset      = 0;
2641
1.05k
    data_block_offset = -1;
2642
1.05k
    page_block_offset = -1;
2643
2644
49.8M
    while( block_offset < (off64_t) io_handle->file_size )
2645
49.8M
    {
2646
49.8M
      if( io_handle->abort != 0 )
2647
0
      {
2648
0
        goto on_error;
2649
0
      }
2650
49.8M
      if( ( recovery_flags & LIBPFF_RECOVERY_FLAG_IGNORE_ALLOCATION_DATA ) == 0 )
2651
0
      {
2652
0
        if( data_block_offset < block_offset )
2653
0
        {
2654
0
          if( unallocated_data_block_index < number_of_unallocated_data_blocks )
2655
0
          {
2656
0
            if( libcdata_range_list_get_range_by_index(
2657
0
                 unallocated_data_block_list,
2658
0
                 unallocated_data_block_index,
2659
0
                 (uint64_t *) &data_block_offset,
2660
0
                 (uint64_t *) &data_block_size,
2661
0
                 &value,
2662
0
                 error ) != 1 )
2663
0
            {
2664
0
              libcerror_error_set(
2665
0
               error,
2666
0
               LIBCERROR_ERROR_DOMAIN_RUNTIME,
2667
0
               LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2668
0
               "%s: unable to retrieve unallocated data block: %d.",
2669
0
               function,
2670
0
               unallocated_data_block_index );
2671
2672
0
              goto on_error;
2673
0
            }
2674
0
            unallocated_data_block_index++;
2675
0
          }
2676
0
          else
2677
0
          {
2678
0
            data_block_offset = (off64_t) io_handle->file_size;
2679
0
            data_block_size   = 0;
2680
0
          }
2681
0
        }
2682
0
        if( page_block_offset < block_offset )
2683
0
        {
2684
0
          if( unallocated_page_block_index < number_of_unallocated_page_blocks )
2685
0
          {
2686
0
            if( libcdata_range_list_get_range_by_index(
2687
0
                 unallocated_page_block_list,
2688
0
                 unallocated_page_block_index,
2689
0
                 (uint64_t *) &page_block_offset,
2690
0
                 (uint64_t *) &page_block_size,
2691
0
                 &value,
2692
0
                 error ) != 1 )
2693
0
            {
2694
0
              libcerror_error_set(
2695
0
               error,
2696
0
               LIBCERROR_ERROR_DOMAIN_RUNTIME,
2697
0
               LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2698
0
               "%s: unable to retrieve unallocated page block: %d.",
2699
0
               function,
2700
0
               unallocated_page_block_index );
2701
2702
0
              goto on_error;
2703
0
            }
2704
0
            unallocated_page_block_index++;
2705
0
          }
2706
0
          else
2707
0
          {
2708
0
            page_block_offset = (off64_t) io_handle->file_size;
2709
0
            page_block_size   = 0;
2710
0
          }
2711
0
        }
2712
0
      }
2713
49.8M
      if( ( recovery_flags & LIBPFF_RECOVERY_FLAG_IGNORE_ALLOCATION_DATA ) == 0 )
2714
0
      {
2715
0
        if( ( data_block_offset >= (off64_t) io_handle->file_size )
2716
0
         && ( page_block_offset >= (off64_t) io_handle->file_size ) )
2717
0
        {
2718
0
          break;
2719
0
        }
2720
        /* Process the smallest offset
2721
         */
2722
0
        else if( ( data_block_offset < page_block_offset )
2723
0
              && ( data_block_size > scan_block_size ) )
2724
0
        {
2725
0
          block_offset = data_block_offset;
2726
0
          block_size   = data_block_size;
2727
0
        }
2728
0
        else if( ( page_block_offset < data_block_offset )
2729
0
              && ( page_block_size > scan_block_size ) )
2730
0
        {
2731
0
          block_offset = page_block_offset;
2732
0
          block_size   = page_block_size;
2733
0
        }
2734
        /* Process the largest range
2735
         */
2736
0
        else if( data_block_size > page_block_size )
2737
0
        {
2738
0
          block_offset = data_block_offset;
2739
0
          block_size   = data_block_size;
2740
0
        }
2741
0
        else
2742
0
        {
2743
0
          block_offset = page_block_offset;
2744
0
          block_size   = page_block_size;
2745
0
        }
2746
0
      }
2747
49.8M
      else
2748
49.8M
      {
2749
49.8M
        block_size = scan_block_size;
2750
49.8M
      }
2751
49.8M
      if( ( block_offset % scan_block_size ) != 0 )
2752
0
      {
2753
0
        block_offset  = ( ( block_offset / scan_block_size ) + 1 ) * scan_block_size;
2754
0
        block_size   -= block_size % scan_block_size;
2755
0
      }
2756
49.8M
      if( block_size < scan_block_size )
2757
0
      {
2758
0
        block_offset += block_size;
2759
2760
0
        continue;
2761
0
      }
2762
99.7M
      while( block_size >= scan_block_size )
2763
49.8M
      {
2764
        /* The index nodes have a fixed block size and stored block size aligned
2765
         */
2766
49.8M
        if( ( block_size >= format_page_block_size )
2767
49.8M
         && ( ( block_offset % format_page_block_size ) == 0 ) )
2768
49.8M
        {
2769
          /* Scan for index values in the index node
2770
           */
2771
49.8M
          result = libpff_recover_index_values(
2772
49.8M
              io_handle,
2773
49.8M
              file_io_handle,
2774
49.8M
              descriptors_index,
2775
49.8M
              offsets_index,
2776
49.8M
              unallocated_data_block_list,
2777
49.8M
              block_offset,
2778
49.8M
              recovery_flags,
2779
49.8M
              error );
2780
2781
49.8M
          if( result == -1 )
2782
129
          {
2783
129
            libcerror_error_set(
2784
129
             error,
2785
129
             LIBCERROR_ERROR_DOMAIN_RUNTIME,
2786
129
             LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2787
129
             "%s: unable to recover index node at offset: %" PRIi64 ".\n",
2788
129
             function,
2789
129
             block_offset );
2790
2791
129
            goto on_error;
2792
129
          }
2793
49.8M
          else if( result == 1 )
2794
7.86k
          {
2795
7.86k
            block_offset += format_page_block_size;
2796
7.86k
            block_size   -= format_page_block_size;
2797
2798
7.86k
            continue;
2799
7.86k
          }
2800
49.8M
        }
2801
49.8M
        if( ( recovery_flags & LIBPFF_RECOVERY_FLAG_SCAN_FOR_FRAGMENTS ) == 0 )
2802
49.8M
        {
2803
49.8M
          block_offset += scan_block_size;
2804
49.8M
          block_size   -= scan_block_size;
2805
2806
49.8M
          continue;
2807
49.8M
        }
2808
/* TODO optimize by minimizing amount of reads */
2809
0
        if( block_buffer_size_available == 0 )
2810
0
        {
2811
0
          block_buffer_data_offset = block_offset;
2812
2813
0
          if( block_buffer_offset > 0 )
2814
0
          {
2815
/* TODO optimize by copying the needed data to the front of the buffer */
2816
0
            block_buffer_data_offset -= format_data_block_size;
2817
0
            block_buffer_offset       = maximum_data_block_size - format_data_block_size;
2818
0
          }
2819
0
          read_size = (size_t) block_size;
2820
2821
0
          if( read_size > (size_t) maximum_data_block_size )
2822
0
          {
2823
0
            read_size = (size_t) maximum_data_block_size;
2824
0
          }
2825
#if defined( HAVE_DEBUG_OUTPUT )
2826
          if( libcnotify_verbose != 0 )
2827
          {
2828
            libcnotify_printf(
2829
             "%s: reading data block at offset: %" PRIi64 " (0x%08" PRIx64 ") of size: %" PRIzd "\n",
2830
             function,
2831
             block_buffer_data_offset,
2832
             block_buffer_data_offset,
2833
             read_size );
2834
          }
2835
#endif
2836
0
          read_count = libbfio_handle_read_buffer_at_offset(
2837
0
                  file_io_handle,
2838
0
                  &( block_buffer[ block_buffer_offset ] ),
2839
0
                  read_size,
2840
0
                  block_buffer_data_offset,
2841
0
                  error );
2842
2843
0
          if( read_count != (ssize_t) read_size )
2844
0
          {
2845
0
            libcerror_error_set(
2846
0
             error,
2847
0
             LIBCERROR_ERROR_DOMAIN_IO,
2848
0
             LIBCERROR_IO_ERROR_READ_FAILED,
2849
0
             "%s: unable to read data block at offset: %" PRIi64 " (0x%08" PRIx64 ").",
2850
0
             function,
2851
0
             block_buffer_data_offset,
2852
0
             block_buffer_data_offset );
2853
2854
0
            goto on_error;
2855
0
          }
2856
0
          block_buffer_size_available = read_size;
2857
0
        }
2858
0
        if( block_buffer_size_available >= format_data_block_size )
2859
0
        {
2860
          /* Scan the block for a data block footer
2861
           */
2862
0
          data_block_footer = &( block_buffer[ block_buffer_offset ] );
2863
2864
0
          if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
2865
0
          {
2866
0
            data_block_footer += format_data_block_size - sizeof( pff_block_footer_32bit_t );
2867
2868
0
            byte_stream_copy_to_uint16_little_endian(
2869
0
             ( (pff_block_footer_32bit_t *) data_block_footer )->data_size,
2870
0
             data_block_data_size );
2871
0
            byte_stream_copy_to_uint32_little_endian(
2872
0
             ( (pff_block_footer_32bit_t *) data_block_footer )->back_pointer,
2873
0
             data_block_back_pointer );
2874
0
            byte_stream_copy_to_uint32_little_endian(
2875
0
             ( (pff_block_footer_32bit_t *) data_block_footer )->checksum,
2876
0
             data_block_stored_checksum );
2877
0
          }
2878
0
          else if( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
2879
0
          {
2880
0
            data_block_footer += format_data_block_size - sizeof( pff_block_footer_64bit_t );
2881
2882
0
            byte_stream_copy_to_uint16_little_endian(
2883
0
             ( (pff_block_footer_64bit_t *) data_block_footer )->data_size,
2884
0
             data_block_data_size );
2885
0
            byte_stream_copy_to_uint32_little_endian(
2886
0
             ( (pff_block_footer_64bit_t *) data_block_footer )->checksum,
2887
0
             data_block_stored_checksum );
2888
0
            byte_stream_copy_to_uint64_little_endian(
2889
0
             ( (pff_block_footer_64bit_t *) data_block_footer )->back_pointer,
2890
0
             data_block_back_pointer );
2891
0
          }
2892
0
          else if( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE )
2893
0
          {
2894
0
            data_block_footer += format_data_block_size - sizeof( pff_block_footer_64bit_4k_page_t );
2895
2896
0
            byte_stream_copy_to_uint16_little_endian(
2897
0
             ( (pff_block_footer_64bit_4k_page_t *) data_block_footer )->data_size,
2898
0
             data_block_data_size );
2899
0
            byte_stream_copy_to_uint32_little_endian(
2900
0
             ( (pff_block_footer_64bit_4k_page_t *) data_block_footer )->checksum,
2901
0
             data_block_stored_checksum );
2902
0
            byte_stream_copy_to_uint64_little_endian(
2903
0
             ( (pff_block_footer_64bit_4k_page_t *) data_block_footer )->back_pointer,
2904
0
             data_block_back_pointer );
2905
0
          }
2906
          /* Check if back pointer itself is not empty but the upper 32-bit are
2907
           */
2908
0
          if( ( data_block_back_pointer != 0 )
2909
0
           && ( ( data_block_back_pointer >> 32 ) == 0 ) )
2910
0
          {
2911
0
            data_block_data_offset = block_buffer_offset - ( ( data_block_data_size / format_data_block_size ) * format_data_block_size );
2912
2913
0
            if( (size_t) data_block_data_size < read_size )
2914
0
            {
2915
0
              result = libpff_recover_analyze_data_block_back_pointer(
2916
0
                        offsets_index,
2917
0
                        data_block_back_pointer,
2918
0
                        (off64_t) block_buffer_data_offset + data_block_data_offset,
2919
0
                        (size32_t) data_block_data_size,
2920
0
                        error );
2921
2922
0
              if( result == -1 )
2923
0
              {
2924
0
                libcerror_error_set(
2925
0
                 error,
2926
0
                 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2927
0
                 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2928
0
                 "%s: unable to recovere data block: %" PRIu64 ".",
2929
0
                 function,
2930
0
                 data_block_back_pointer );
2931
2932
0
                goto on_error;
2933
0
              }
2934
0
              else if( result != 0 )
2935
0
              {
2936
0
                block_offset += format_data_block_size;
2937
0
                block_size   -= format_data_block_size;
2938
2939
/* TODO reset block_buffer_offset and block_buffer_size_available ? */
2940
2941
0
                continue;
2942
0
              }
2943
0
              if( data_block_stored_checksum != 0 )
2944
0
              {
2945
0
                if( libpff_checksum_calculate_weak_crc32(
2946
0
                     &data_block_calculated_checksum,
2947
0
                     &( block_buffer[ data_block_data_offset ] ),
2948
0
                     data_block_data_size,
2949
0
                     0,
2950
0
                     error ) != 1 )
2951
0
                {
2952
0
                  libcerror_error_set(
2953
0
                   error,
2954
0
                   LIBCERROR_ERROR_DOMAIN_RUNTIME,
2955
0
                   LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2956
0
                   "%s: unable to calculate weak CRC-32.",
2957
0
                   function );
2958
2959
0
                  goto on_error;
2960
0
                }
2961
0
                if( data_block_stored_checksum != data_block_calculated_checksum )
2962
0
                {
2963
#if defined( HAVE_DEBUG_OUTPUT )
2964
                  if( libcnotify_verbose != 0 )
2965
                  {
2966
                    libcnotify_printf(
2967
                     "%s: mismatch in data block: %" PRIu64 " checksum ( %" PRIu32 " != %" PRIu32 " ).\n",
2968
                     function,
2969
                     data_block_back_pointer,
2970
                     data_block_stored_checksum,
2971
                     data_block_calculated_checksum );
2972
                  }
2973
#endif
2974
0
                  block_offset += format_data_block_size;
2975
0
                  block_size   -= format_data_block_size;
2976
2977
0
                  continue;
2978
0
                }
2979
0
              }
2980
/* TODO consider data block as fragment */
2981
2982
0
              data_block_data_size = ( ( data_block_data_size / format_data_block_size ) + 1 ) * format_data_block_size;
2983
2984
#if defined( HAVE_DEBUG_OUTPUT )
2985
              if( libcnotify_verbose != 0 )
2986
              {
2987
                libcnotify_printf(
2988
                 "%s: data block back pointer: 0x%08" PRIx64 "\n",
2989
                 function,
2990
                 data_block_back_pointer );
2991
2992
                libcnotify_printf(
2993
                 "%s: data block data at offset: %" PRIi64 " (0x%08" PRIx64 ") of size: %" PRIzd "\n",
2994
                 function,
2995
                 block_buffer_data_offset + data_block_data_offset,
2996
                 block_buffer_data_offset + data_block_data_offset,
2997
                 data_block_data_size );
2998
                libcnotify_print_data(
2999
                 &( block_buffer[ data_block_data_offset ] ),
3000
                 data_block_data_size,
3001
                 LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
3002
              }
3003
#endif
3004
0
            }
3005
0
          }
3006
0
          block_buffer_offset         += format_data_block_size;
3007
0
          block_buffer_size_available -= format_data_block_size;
3008
0
        }
3009
0
        else
3010
0
        {
3011
0
          block_buffer_offset         = 0;
3012
0
          block_buffer_size_available = 0;
3013
0
        }
3014
0
        block_offset += scan_block_size;
3015
0
        block_size   -= scan_block_size;
3016
0
      }
3017
49.8M
    }
3018
1.05k
  }
3019
927
  memory_free(
3020
927
   block_buffer );
3021
3022
927
  return( 1 );
3023
3024
129
on_error:
3025
129
  if( block_buffer != NULL )
3026
129
  {
3027
129
    memory_free(
3028
129
     block_buffer );
3029
129
  }
3030
129
  return( -1 );
3031
1.05k
}
3032
3033
/* Scans for recoverable index values in an index node
3034
 * Returns 1 if successful, returns 0 if no valid index node could be found or -1 on error
3035
 */
3036
int libpff_recover_index_values(
3037
     libpff_io_handle_t *io_handle,
3038
     libbfio_handle_t *file_io_handle,
3039
     libpff_descriptors_index_t *descriptors_index,
3040
     libpff_offsets_index_t *offsets_index,
3041
     libcdata_range_list_t *unallocated_data_block_list,
3042
     size64_t node_offset,
3043
     uint8_t recovery_flags,
3044
     libcerror_error_t **error )
3045
49.8M
{
3046
49.8M
  libpff_index_node_t *index_node       = NULL;
3047
49.8M
  libpff_index_value_t *index_value     = NULL;
3048
49.8M
  uint8_t *node_entry_data              = NULL;
3049
49.8M
  static char *function                 = "libpff_recover_index_values";
3050
49.8M
  const char *index_string              = NULL;
3051
49.8M
  off64_t index_value_file_offset       = 0;
3052
49.8M
  uint64_t index_value_identifier       = 0;
3053
49.8M
        uint32_t maximum_data_block_data_size = 0;
3054
49.8M
  uint16_t index_value_data_size        = 0;
3055
49.8M
  uint8_t entry_index                   = 0;
3056
49.8M
  int recoverable                       = 0;
3057
49.8M
  int result                            = 0;
3058
3059
49.8M
  if( io_handle == NULL )
3060
0
  {
3061
0
    libcerror_error_set(
3062
0
     error,
3063
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3064
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3065
0
     "%s: invalid IO handle.",
3066
0
     function );
3067
3068
0
    return( -1 );
3069
0
  }
3070
49.8M
  if( ( io_handle->file_type != LIBPFF_FILE_TYPE_32BIT )
3071
15.8M
   && ( io_handle->file_type != LIBPFF_FILE_TYPE_64BIT )
3072
15.6M
   && ( io_handle->file_type != LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
3073
0
  {
3074
0
    libcerror_error_set(
3075
0
     error,
3076
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3077
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
3078
0
     "%s: unsupported file type.",
3079
0
     function );
3080
3081
0
    return( -1 );
3082
0
  }
3083
49.8M
  if( descriptors_index == NULL )
3084
0
  {
3085
0
    libcerror_error_set(
3086
0
     error,
3087
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3088
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3089
0
     "%s: invalid descriptors index.",
3090
0
     function );
3091
3092
0
    return( -1 );
3093
0
  }
3094
49.8M
  if( offsets_index == NULL )
3095
0
  {
3096
0
    libcerror_error_set(
3097
0
     error,
3098
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3099
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3100
0
     "%s: invalid offsets index.",
3101
0
     function );
3102
3103
0
    return( -1 );
3104
0
  }
3105
49.8M
  if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
3106
34.0M
  {
3107
34.0M
    maximum_data_block_data_size = 8192 - 12;
3108
34.0M
  }
3109
15.8M
  else if( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
3110
171k
  {
3111
171k
    maximum_data_block_data_size = 8192 - 16;
3112
171k
  }
3113
15.6M
  else
3114
15.6M
  {
3115
/* TODO: this value is currently assumed based on the 512 x 8 = 4k page */
3116
15.6M
    maximum_data_block_data_size = 65536 - 24;
3117
15.6M
  }
3118
49.8M
  if( libpff_index_node_initialize(
3119
49.8M
       &index_node,
3120
49.8M
       error ) != 1 )
3121
0
  {
3122
0
    libcerror_error_set(
3123
0
     error,
3124
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3125
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
3126
0
     "%s: unable to create index node.",
3127
0
     function );
3128
3129
0
    goto on_error;
3130
0
  }
3131
49.8M
  if( libpff_index_node_read_file_io_handle(
3132
49.8M
       index_node,
3133
49.8M
       file_io_handle,
3134
49.8M
       node_offset,
3135
49.8M
       io_handle->file_type,
3136
49.8M
       error ) != 1 )
3137
49.8M
  {
3138
#if defined( HAVE_DEBUG_OUTPUT )
3139
    if( ( libcnotify_verbose != 0 )
3140
     && ( error != NULL )
3141
     && ( *error != NULL ) )
3142
    {
3143
      libcnotify_print_error_backtrace(
3144
       *error );
3145
    }
3146
#endif
3147
49.8M
    libcerror_error_free(
3148
49.8M
     error );
3149
3150
49.8M
    libpff_index_node_free(
3151
49.8M
     &index_node,
3152
49.8M
     NULL );
3153
3154
49.8M
    return( 0 );
3155
49.8M
  }
3156
7.99k
  if( index_node->type == LIBPFF_INDEX_TYPE_DESCRIPTOR )
3157
3.50k
  {
3158
3.50k
    index_string = "descriptors";
3159
3.50k
  }
3160
4.49k
  else if( index_node->type == LIBPFF_INDEX_TYPE_OFFSET )
3161
4.49k
  {
3162
4.49k
    index_string = "offsets";
3163
4.49k
  }
3164
7.99k
  if( ( index_node->type != LIBPFF_INDEX_TYPE_DESCRIPTOR )
3165
4.49k
   && ( index_node->type != LIBPFF_INDEX_TYPE_OFFSET ) )
3166
0
  {
3167
#if defined( HAVE_DEBUG_OUTPUT )
3168
    if( libcnotify_verbose != 0 )
3169
    {
3170
      libcnotify_printf(
3171
       "%s: unsupported index type: 0x%02" PRIx8 ".\n",
3172
       function,
3173
       index_node->type );
3174
    }
3175
#endif
3176
0
  }
3177
7.99k
  else if( index_node->level != LIBPFF_INDEX_NODE_LEVEL_LEAF )
3178
3.37k
  {
3179
#if defined( HAVE_DEBUG_OUTPUT )
3180
    if( libcnotify_verbose != 0 )
3181
    {
3182
      libcnotify_printf(
3183
       "%s: skipping %s index branch node at level: %" PRIu8 ".\n",
3184
       function,
3185
       index_string,
3186
       index_node->level );
3187
    }
3188
#endif
3189
3.37k
  }
3190
4.62k
  else
3191
4.62k
  {
3192
    /* Check if the index leaf entries are recoverable
3193
     */
3194
4.62k
    for( entry_index = 0;
3195
110k
         entry_index < index_node->maximum_number_of_entries;
3196
106k
         entry_index++ )
3197
106k
    {
3198
106k
      if( libpff_index_node_get_entry_data(
3199
106k
           index_node,
3200
106k
           entry_index,
3201
106k
           &node_entry_data,
3202
106k
           error ) != 1 )
3203
0
      {
3204
0
        libcerror_error_set(
3205
0
         error,
3206
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
3207
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3208
0
         "%s: unable to retrieve node entry: %" PRIu8 " data.",
3209
0
         function,
3210
0
         entry_index );
3211
3212
0
        goto on_error;
3213
0
      }
3214
106k
      if( node_entry_data == NULL )
3215
0
      {
3216
0
        libcerror_error_set(
3217
0
         error,
3218
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
3219
0
         LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3220
0
         "%s: missing node entry: %" PRIu8 " data.",
3221
0
         function,
3222
0
         entry_index );
3223
3224
0
        goto on_error;
3225
0
      }
3226
#if defined( HAVE_DEBUG_OUTPUT )
3227
      if( libcnotify_verbose != 0 )
3228
      {
3229
        libcnotify_printf(
3230
         "%s: analyzing %s index entry: %" PRIu8 ".\n",
3231
         function,
3232
         index_string,
3233
         entry_index );
3234
      }
3235
#endif
3236
106k
      if( libpff_index_value_initialize(
3237
106k
           &index_value,
3238
106k
           error ) != 1 )
3239
0
      {
3240
0
        libcerror_error_set(
3241
0
         error,
3242
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
3243
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
3244
0
         "%s: unable to create %s index value.",
3245
0
         function,
3246
0
         index_string );
3247
3248
0
        goto on_error;
3249
0
      }
3250
106k
      result = libpff_recover_index_value_read_data(
3251
106k
                index_value,
3252
106k
                io_handle,
3253
106k
                index_node->type,
3254
106k
                node_entry_data,
3255
106k
                (size_t) index_node->entry_size,
3256
106k
                maximum_data_block_data_size,
3257
106k
                error );
3258
3259
106k
      if( result == -1 )
3260
0
      {
3261
0
        libcerror_error_set(
3262
0
         error,
3263
0
         LIBCERROR_ERROR_DOMAIN_IO,
3264
0
         LIBCERROR_IO_ERROR_READ_FAILED,
3265
0
         "%s: unable to read %s index entry: %" PRIu8 " value.",
3266
0
         function,
3267
0
         index_string,
3268
0
         entry_index );
3269
3270
0
        goto on_error;
3271
0
      }
3272
106k
      recoverable = result;
3273
3274
106k
      if( recoverable != 0 )
3275
64.0k
      {
3276
64.0k
        if( index_node->type == LIBPFF_INDEX_TYPE_DESCRIPTOR )
3277
25.9k
        {
3278
25.9k
          result = libpff_recover_analyze_descriptors_index_value(
3279
25.9k
              descriptors_index,
3280
25.9k
              io_handle,
3281
25.9k
              file_io_handle,
3282
25.9k
              index_value,
3283
25.9k
              error );
3284
25.9k
        }
3285
38.0k
        else if( index_node->type == LIBPFF_INDEX_TYPE_OFFSET )
3286
38.0k
        {
3287
38.0k
          result = libpff_recover_analyze_offsets_index_value(
3288
38.0k
              offsets_index,
3289
38.0k
              io_handle,
3290
38.0k
              file_io_handle,
3291
38.0k
              index_value,
3292
38.0k
              maximum_data_block_data_size,
3293
38.0k
              error );
3294
38.0k
        }
3295
64.0k
        if( result == -1 )
3296
129
        {
3297
129
          libcerror_error_set(
3298
129
           error,
3299
129
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
3300
129
           LIBCERROR_RUNTIME_ERROR_GENERIC,
3301
129
           "%s: unable to analyze deleted %d index value: %" PRIu64 ".",
3302
129
           function,
3303
129
           index_string,
3304
129
           index_value->identifier );
3305
3306
129
          goto on_error;
3307
129
        }
3308
63.8k
        recoverable = result;
3309
63.8k
      }
3310
106k
      if( recoverable != 0 )
3311
35.1k
      {
3312
        /* Check if the offsets index value is unallocated according to the
3313
         * unallocated data block list
3314
         */
3315
35.1k
        if( ( index_node->type == LIBPFF_INDEX_TYPE_OFFSET )
3316
15.7k
         && ( ( recovery_flags & LIBPFF_RECOVERY_FLAG_IGNORE_ALLOCATION_DATA ) == 0 ) )
3317
0
        {
3318
0
          result = libcdata_range_list_range_is_present(
3319
0
              unallocated_data_block_list,
3320
0
              (uint64_t) index_value_file_offset,
3321
0
              (uint64_t) index_value_data_size,
3322
0
              error );
3323
3324
0
          if( result == -1 )
3325
0
          {
3326
0
            libcerror_error_set(
3327
0
             error,
3328
0
             LIBCERROR_ERROR_DOMAIN_RUNTIME,
3329
0
             LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3330
0
             "%s: error determining if offset range is unallocated.",
3331
0
             function );
3332
3333
0
            goto on_error;
3334
0
          }
3335
0
          else if( result == 0 )
3336
0
          {
3337
#if defined( HAVE_DEBUG_OUTPUT )
3338
            if( libcnotify_verbose != 0 )
3339
            {
3340
              libcnotify_printf(
3341
               "%s: %s index entry: %" PRIu8 " identifier: %" PRIu64 " refers to allocated range: 0x%08" PRIx64 " - 0x%08" PRIx64 " (%" PRIu64 ").\n",
3342
               function,
3343
               index_string,
3344
               entry_index,
3345
               index_value_identifier,
3346
               index_value_file_offset,
3347
               index_value_file_offset + index_value_data_size,
3348
               index_value_data_size );
3349
            }
3350
#endif
3351
0
            recoverable = 0;
3352
0
          }
3353
#if defined( HAVE_DEBUG_OUTPUT )
3354
          else if( libcnotify_verbose != 0 )
3355
          {
3356
            libcnotify_printf(
3357
             "%s: %s index entry: %" PRIu8 " identifier: %" PRIu64 " refers to unallocated range: 0x%08" PRIx64 " - 0x%08" PRIx64 " (%" PRIu64 ").\n",
3358
             function,
3359
             index_string,
3360
             entry_index,
3361
             index_value_identifier,
3362
             index_value_file_offset,
3363
             index_value_file_offset + index_value_data_size,
3364
             index_value_data_size );
3365
          }
3366
#endif
3367
0
        }
3368
35.1k
      }
3369
106k
      if( recoverable != 0 )
3370
35.1k
      {
3371
        /* Move the recovered node to the index
3372
         */
3373
#if defined( HAVE_DEBUG_OUTPUT )
3374
        if( libcnotify_verbose != 0 )
3375
        {
3376
          libcnotify_printf(
3377
           "%s: %s index entry: %" PRIu8 " identifier: %" PRIu64 " is recoverable.\n",
3378
           function,
3379
           index_string,
3380
           entry_index,
3381
           index_value_identifier );
3382
        }
3383
#endif
3384
35.1k
        if( index_node->type == LIBPFF_INDEX_TYPE_DESCRIPTOR )
3385
19.3k
        {
3386
19.3k
          result = libpff_descriptors_index_insert_recovered_index_value(
3387
19.3k
                    descriptors_index,
3388
19.3k
                    index_value,
3389
19.3k
                    error );
3390
19.3k
        }
3391
15.7k
        else if( index_node->type == LIBPFF_INDEX_TYPE_OFFSET )
3392
15.7k
        {
3393
15.7k
          result = libpff_offsets_index_insert_recovered_index_value(
3394
15.7k
                    offsets_index,
3395
15.7k
                    index_value,
3396
15.7k
                    error );
3397
15.7k
        }
3398
35.1k
        if( result != 1 )
3399
0
        {
3400
0
          libcerror_error_set(
3401
0
           error,
3402
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
3403
0
           LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
3404
0
           "%s: unable to insert recovered %s index value: %" PRIu64 " list.",
3405
0
           function,
3406
0
           index_string,
3407
0
           index_value_identifier );
3408
3409
0
          goto on_error;
3410
0
        }
3411
35.1k
        index_value = NULL;
3412
35.1k
      }
3413
71.0k
      else
3414
71.0k
      {
3415
71.0k
        if( libpff_index_value_free(
3416
71.0k
             &index_value,
3417
71.0k
             error ) != 1 )
3418
0
        {
3419
0
          libcerror_error_set(
3420
0
           error,
3421
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
3422
0
           LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
3423
0
           "%s: unable to free index value.",
3424
0
           function );
3425
3426
0
          goto on_error;
3427
0
        }
3428
71.0k
      }
3429
106k
      node_offset += index_node->entry_size;
3430
106k
    }
3431
4.62k
  }
3432
7.86k
  if( libpff_index_node_free(
3433
7.86k
       &index_node,
3434
7.86k
       error ) != 1 )
3435
0
  {
3436
0
    libcerror_error_set(
3437
0
     error,
3438
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3439
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
3440
0
     "%s: unable to free index node.",
3441
0
     function );
3442
3443
0
    goto on_error;
3444
0
  }
3445
#if defined( HAVE_DEBUG_OUTPUT )
3446
  if( libcnotify_verbose != 0 )
3447
  {
3448
    libcnotify_printf(
3449
     "\n" );
3450
  }
3451
#endif
3452
7.86k
  return( 1 );
3453
3454
129
on_error:
3455
129
  if( index_value != NULL )
3456
129
  {
3457
129
    libpff_index_value_free(
3458
129
     &index_value,
3459
129
     NULL );
3460
129
  }
3461
129
  if( index_node != NULL )
3462
129
  {
3463
129
    libpff_index_node_free(
3464
129
     &index_node,
3465
129
     NULL );
3466
129
  }
3467
129
  return( -1 );
3468
7.86k
}
3469
3470
/* Reads the index value data
3471
 * Returns 1 if successful, returns 0 if not or -1 on error
3472
 */
3473
int libpff_recover_index_value_read_data(
3474
     libpff_index_value_t *index_value,
3475
     libpff_io_handle_t *io_handle,
3476
     uint8_t index_node_type,
3477
     const uint8_t *data,
3478
     size_t data_size,
3479
     uint32_t maximum_data_block_data_size,
3480
     libcerror_error_t **error )
3481
106k
{
3482
106k
  static char *function    = "libpff_recover_index_value_read_data";
3483
106k
  size_t value_size        = 0;
3484
106k
  uint64_t file_offset     = 0;
3485
3486
#if defined( HAVE_DEBUG_OUTPUT )
3487
  const char *index_string = NULL;
3488
#endif
3489
3490
106k
  if( index_value == NULL )
3491
0
  {
3492
0
    libcerror_error_set(
3493
0
     error,
3494
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3495
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3496
0
     "%s: invalid index value.",
3497
0
     function );
3498
3499
0
    return( -1 );
3500
0
  }
3501
106k
  if( io_handle == NULL )
3502
0
  {
3503
0
    libcerror_error_set(
3504
0
     error,
3505
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3506
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3507
0
     "%s: invalid IO handle.",
3508
0
     function );
3509
3510
0
    return( -1 );
3511
0
  }
3512
106k
  if( index_node_type == LIBPFF_INDEX_TYPE_DESCRIPTOR )
3513
40.0k
  {
3514
40.0k
    if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
3515
10.3k
    {
3516
10.3k
      value_size = sizeof( pff_index_node_descriptor_entry_32bit_t );
3517
10.3k
    }
3518
29.6k
    else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
3519
11.2k
          || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
3520
29.6k
    {
3521
29.6k
      value_size = sizeof( pff_index_node_descriptor_entry_64bit_t );
3522
29.6k
    }
3523
#if defined( HAVE_DEBUG_OUTPUT )
3524
    index_string = "descriptors";
3525
#endif
3526
40.0k
  }
3527
66.3k
  else if( index_node_type == LIBPFF_INDEX_TYPE_OFFSET )
3528
66.3k
  {
3529
66.3k
    if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
3530
20.7k
    {
3531
20.7k
      value_size = sizeof( pff_index_node_offset_entry_32bit_t );
3532
20.7k
    }
3533
45.6k
    else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
3534
7.48k
          || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
3535
45.6k
    {
3536
45.6k
      value_size = sizeof( pff_index_node_offset_entry_64bit_t );
3537
45.6k
    }
3538
#if defined( HAVE_DEBUG_OUTPUT )
3539
    index_string = "offsets";
3540
#endif
3541
66.3k
  }
3542
106k
  if( data == NULL )
3543
0
  {
3544
0
    libcerror_error_set(
3545
0
     error,
3546
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3547
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3548
0
     "%s: invalid data.",
3549
0
     function );
3550
3551
0
    return( -1 );
3552
0
  }
3553
106k
  if( data_size > SSIZE_MAX )
3554
0
  {
3555
0
    libcerror_error_set(
3556
0
     error,
3557
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3558
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3559
0
     "%s: unsupported data size value out of bounds.",
3560
0
     function );
3561
3562
0
    return( -1 );
3563
0
  }
3564
106k
  if( data_size < value_size )
3565
8.92k
  {
3566
#if defined( HAVE_DEBUG_OUTPUT )
3567
    if( libcnotify_verbose != 0 )
3568
    {
3569
      libcnotify_printf(
3570
       "%s: %s index value data too small.\n",
3571
       function,
3572
       index_string );
3573
    }
3574
#endif
3575
8.92k
    return( 0 );
3576
8.92k
  }
3577
#if defined( HAVE_DEBUG_OUTPUT )
3578
  if( libcnotify_verbose != 0 )
3579
  {
3580
    libcnotify_printf(
3581
     "%s: %s index value data:\n",
3582
     function,
3583
     index_string );
3584
    libcnotify_print_data(
3585
     data,
3586
     value_size,
3587
     0 );
3588
  }
3589
#endif
3590
97.4k
  if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
3591
29.1k
  {
3592
29.1k
    byte_stream_copy_to_uint32_little_endian(
3593
29.1k
     data,
3594
29.1k
     index_value->identifier );
3595
29.1k
  }
3596
68.2k
  else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
3597
16.7k
        || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
3598
68.2k
  {
3599
68.2k
    byte_stream_copy_to_uint64_little_endian(
3600
68.2k
     data,
3601
68.2k
     index_value->identifier );
3602
68.2k
  }
3603
  /* Ignore the upper 32-bit of descriptor identifiers
3604
   */
3605
97.4k
  if( index_node_type == LIBPFF_INDEX_TYPE_DESCRIPTOR )
3606
35.4k
  {
3607
35.4k
    index_value->identifier &= 0xffffffffUL;
3608
35.4k
  }
3609
97.4k
  if( index_node_type == LIBPFF_INDEX_TYPE_DESCRIPTOR )
3610
35.4k
  {
3611
35.4k
    if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
3612
9.86k
    {
3613
9.86k
      byte_stream_copy_to_uint32_little_endian(
3614
9.86k
       ( (pff_index_node_descriptor_entry_32bit_t *) data )->data_identifier,
3615
9.86k
       index_value->data_identifier );
3616
3617
9.86k
      byte_stream_copy_to_uint32_little_endian(
3618
9.86k
       ( (pff_index_node_descriptor_entry_32bit_t *) data )->local_descriptors_identifier,
3619
9.86k
       index_value->local_descriptors_identifier );
3620
3621
9.86k
      byte_stream_copy_to_uint32_little_endian(
3622
9.86k
       ( (pff_index_node_descriptor_entry_32bit_t *) data )->parent_identifier,
3623
9.86k
       index_value->parent_identifier );
3624
9.86k
    }
3625
25.5k
    else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
3626
9.75k
          || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
3627
25.5k
    {
3628
25.5k
      byte_stream_copy_to_uint64_little_endian(
3629
25.5k
       ( (pff_index_node_descriptor_entry_64bit_t *) data )->data_identifier,
3630
25.5k
       index_value->data_identifier );
3631
3632
25.5k
      byte_stream_copy_to_uint64_little_endian(
3633
25.5k
       ( (pff_index_node_descriptor_entry_64bit_t *) data )->local_descriptors_identifier,
3634
25.5k
       index_value->local_descriptors_identifier );
3635
3636
25.5k
      byte_stream_copy_to_uint32_little_endian(
3637
25.5k
       ( (pff_index_node_descriptor_entry_64bit_t *) data )->parent_identifier,
3638
25.5k
       index_value->parent_identifier );
3639
25.5k
    }
3640
35.4k
  }
3641
62.0k
  else if( index_node_type == LIBPFF_INDEX_TYPE_OFFSET )
3642
62.0k
  {
3643
62.0k
    if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
3644
19.3k
    {
3645
19.3k
      byte_stream_copy_to_uint32_little_endian(
3646
19.3k
       ( (pff_index_node_offset_entry_32bit_t *) data )->file_offset,
3647
19.3k
       file_offset );
3648
3649
19.3k
      byte_stream_copy_to_uint16_little_endian(
3650
19.3k
       ( (pff_index_node_offset_entry_32bit_t *) data )->data_size,
3651
19.3k
       index_value->data_size );
3652
3653
19.3k
      byte_stream_copy_to_uint16_little_endian(
3654
19.3k
       ( (pff_index_node_offset_entry_32bit_t *) data )->reference_count,
3655
19.3k
       index_value->reference_count );
3656
19.3k
    }
3657
42.6k
    else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
3658
6.97k
          || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
3659
42.6k
    {
3660
42.6k
      byte_stream_copy_to_uint64_little_endian(
3661
42.6k
       ( (pff_index_node_offset_entry_64bit_t *) data )->file_offset,
3662
42.6k
       file_offset );
3663
3664
42.6k
      byte_stream_copy_to_uint16_little_endian(
3665
42.6k
       ( (pff_index_node_offset_entry_64bit_t *) data )->data_size,
3666
42.6k
       index_value->data_size );
3667
3668
42.6k
      byte_stream_copy_to_uint16_little_endian(
3669
42.6k
       ( (pff_index_node_offset_entry_64bit_t *) data )->reference_count,
3670
42.6k
       index_value->reference_count );
3671
42.6k
    }
3672
62.0k
  }
3673
#if defined( HAVE_DEBUG_OUTPUT )
3674
  if( libcnotify_verbose != 0 )
3675
  {
3676
    libcnotify_printf(
3677
     "%s: identifier\t\t\t\t: %" PRIu64 " (0x%08" PRIx64 ")\n",
3678
     function,
3679
     index_value->identifier,
3680
     index_value->identifier );
3681
3682
    if( index_node_type == LIBPFF_INDEX_TYPE_DESCRIPTOR )
3683
    {
3684
      libcnotify_printf(
3685
       "%s: data identifier\t\t\t\t: %" PRIu64 " (0x%08" PRIx64 ")\n",
3686
       function,
3687
       index_value->data_identifier,
3688
       index_value->data_identifier );
3689
3690
      libcnotify_printf(
3691
       "%s: local descriptors identifier\t\t: %" PRIu64 " (0x%08" PRIx64 ")\n",
3692
       function,
3693
       index_value->local_descriptors_identifier,
3694
       index_value->local_descriptors_identifier );
3695
3696
      libcnotify_printf(
3697
       "%s: parent identifier\t\t\t\t: %" PRIu32 " (0x%08" PRIx32 ")\n",
3698
       function,
3699
       index_value->parent_identifier,
3700
       index_value->parent_identifier );
3701
    }
3702
    else if( index_node_type == LIBPFF_INDEX_TYPE_OFFSET )
3703
    {
3704
      libcnotify_printf(
3705
       "%s: file offset\t\t\t\t: %" PRIu64 " (0x%08" PRIx64 ")\n",
3706
       function,
3707
       file_offset,
3708
       file_offset );
3709
3710
      libcnotify_printf(
3711
       "%s: data size\t\t\t\t\t: %" PRIu64 "\n",
3712
       function,
3713
       index_value->data_size );
3714
3715
      libcnotify_printf(
3716
       "%s: reference count\t\t\t\t: %" PRIu64 "\n",
3717
       function,
3718
       index_value->reference_count );
3719
    }
3720
    libcnotify_printf(
3721
     "\n" );
3722
  }
3723
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
3724
3725
  /* Ignore index values without an identifier
3726
   */
3727
97.4k
  if( index_value->identifier == 0 )
3728
17.7k
  {
3729
#if defined( HAVE_DEBUG_OUTPUT )
3730
    if( libcnotify_verbose != 0 )
3731
    {
3732
      libcnotify_printf(
3733
       "%s: %s index value has an empty identifier.\n",
3734
       function,
3735
       index_string );
3736
    }
3737
#endif
3738
17.7k
    return( 0 );
3739
17.7k
  }
3740
79.6k
  if( index_node_type == LIBPFF_INDEX_TYPE_DESCRIPTOR )
3741
28.5k
  {
3742
    /* Ignore descriptors index values without a data identifier
3743
     */
3744
28.5k
    if( index_value->data_identifier == 0 )
3745
2.58k
    {
3746
#if defined( HAVE_DEBUG_OUTPUT )
3747
      if( libcnotify_verbose != 0 )
3748
      {
3749
        libcnotify_printf(
3750
         "%s: %s index value identifier: %" PRIu64 " has an empty data identifier.\n",
3751
         function,
3752
         index_string,
3753
         index_value->identifier );
3754
      }
3755
#endif
3756
2.58k
      return( 0 );
3757
2.58k
    }
3758
28.5k
  }
3759
51.1k
  else if( index_node_type == LIBPFF_INDEX_TYPE_OFFSET )
3760
51.1k
  {
3761
    /* Ignore index values without a valid file offset
3762
     */
3763
51.1k
    if( file_offset > (uint64_t) INT64_MAX )
3764
3.73k
    {
3765
#if defined( HAVE_DEBUG_OUTPUT )
3766
      if( libcnotify_verbose != 0 )
3767
      {
3768
        libcnotify_printf(
3769
         "%s: %s index identifier: %" PRIu64 " has an invalid file offset: %" PRIi64 " (0x%" PRIx64 ").\n",
3770
         function,
3771
         index_string,
3772
         index_value->identifier,
3773
         (int64_t) file_offset,
3774
         file_offset );
3775
      }
3776
#endif
3777
3.73k
      return( 0 );
3778
3.73k
    }
3779
    /* Ignore index values without a valid data size
3780
     */
3781
47.3k
    if( ( index_value->data_size == 0 )
3782
41.9k
     || ( (uint32_t) index_value->data_size > maximum_data_block_data_size ) )
3783
9.34k
    {
3784
#if defined( HAVE_DEBUG_OUTPUT )
3785
      if( libcnotify_verbose != 0 )
3786
      {
3787
        libcnotify_printf(
3788
         "%s: %s index identifier: %" PRIu64 " has an invalid data size: %" PRIu16 ".\n",
3789
         function,
3790
         index_string,
3791
         index_value->identifier,
3792
         index_value->data_size );
3793
      }
3794
#endif
3795
9.34k
      return( 0 );
3796
9.34k
    }
3797
38.0k
    index_value->file_offset = file_offset;
3798
38.0k
  }
3799
64.0k
  return( 1 );
3800
79.6k
}
3801
3802
/* Analyze if specific data block is recoverable
3803
 * Returns 1 if recoverable, 0 if not or -1 on error
3804
 */
3805
int libpff_recover_analyze_data_block(
3806
     libpff_io_handle_t *io_handle,
3807
     libbfio_handle_t *file_io_handle,
3808
     uint32_t descriptor_identifier,
3809
     libpff_index_value_t *offsets_index_value,
3810
     libcerror_error_t **error )
3811
2.19k
{
3812
2.19k
  libpff_data_block_t *data_block = NULL;
3813
2.19k
  static char *function           = "libpff_recover_analyze_data_block";
3814
2.19k
  int result                      = 0;
3815
3816
2.19k
  if( offsets_index_value == NULL )
3817
0
  {
3818
0
    libcerror_error_set(
3819
0
     error,
3820
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3821
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3822
0
     "%s: invalid offsets index value.",
3823
0
     function );
3824
3825
0
    return( -1 );
3826
0
  }
3827
  /* Check if the data block is readable
3828
   */
3829
2.19k
  if( libpff_data_block_initialize(
3830
2.19k
       &data_block,
3831
2.19k
       io_handle,
3832
2.19k
       descriptor_identifier,
3833
2.19k
       offsets_index_value->identifier,
3834
2.19k
       error ) != 1 )
3835
0
  {
3836
0
    libcerror_error_set(
3837
0
     error,
3838
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3839
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
3840
0
     "%s: unable to create data block.",
3841
0
     function );
3842
3843
0
    goto on_error;
3844
0
  }
3845
#if defined( HAVE_DEBUG_OUTPUT )
3846
  if( libcnotify_verbose != 0 )
3847
  {
3848
    libcnotify_printf(
3849
     "%s: attempting to read data block at offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
3850
     function,
3851
     offsets_index_value->file_offset,
3852
     offsets_index_value->file_offset );
3853
  }
3854
#endif
3855
2.19k
  result = libpff_data_block_read_file_io_handle(
3856
2.19k
      data_block,
3857
2.19k
      file_io_handle,
3858
2.19k
      offsets_index_value->file_offset,
3859
2.19k
      offsets_index_value->data_size,
3860
2.19k
      io_handle->file_type,
3861
2.19k
      error );
3862
3863
2.19k
  if( result != 1 )
3864
1.37k
  {
3865
1.37k
    libcerror_error_set(
3866
1.37k
     error,
3867
1.37k
     LIBCERROR_ERROR_DOMAIN_IO,
3868
1.37k
     LIBCERROR_IO_ERROR_READ_FAILED,
3869
1.37k
     "%s: unable to read data block.",
3870
1.37k
     function );
3871
#if defined( HAVE_DEBUG_OUTPUT )
3872
    if( ( libcnotify_verbose != 0 )
3873
     && ( error != NULL )
3874
     && ( *error != NULL ) )
3875
    {
3876
      libcnotify_print_error_backtrace(
3877
       *error );
3878
    }
3879
#endif
3880
1.37k
    libcerror_error_free(
3881
1.37k
     error );
3882
3883
/* TODO delete unreadable offset identifier in offsets_index->recovered_index_tree */
3884
1.37k
  }
3885
2.19k
  if( libpff_data_block_free(
3886
2.19k
       &data_block,
3887
2.19k
       error ) != 1 )
3888
0
  {
3889
0
    libcerror_error_set(
3890
0
     error,
3891
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3892
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
3893
0
     "%s: unable to free recovered data block.",
3894
0
     function );
3895
3896
0
    goto on_error;
3897
0
  }
3898
/* TODO validate the block data ? */
3899
3900
2.19k
  return( 1 );
3901
3902
0
on_error:
3903
0
  if( data_block != NULL )
3904
0
  {
3905
0
    libpff_data_block_free(
3906
0
     &data_block,
3907
0
     NULL );
3908
0
  }
3909
0
  return( -1 );
3910
2.19k
}
3911
3912
/* Analyze if specific local descriptors are recoverable
3913
 * Returns 1 if recoverable, 0 if not or -1 on error
3914
 */
3915
int libpff_recover_analyze_local_descriptors(
3916
     libpff_io_handle_t *io_handle,
3917
     libbfio_handle_t *file_io_handle,
3918
     libpff_offsets_index_t *offsets_index,
3919
     uint64_t local_descriptors_identifier,
3920
     int recursion_depth,
3921
     libcerror_error_t **error )
3922
310
{
3923
310
  libpff_index_value_t *offsets_index_value                    = NULL;
3924
310
  libpff_local_descriptors_node_t *local_descriptors_node      = NULL;
3925
310
  uint8_t *node_entry_data                                     = NULL;
3926
310
  static char *function                                        = "libpff_recover_analyze_local_descriptors";
3927
310
  uint64_t local_descriptor_value_data_identifier              = 0;
3928
310
  uint64_t local_descriptor_value_identifier                   = 0;
3929
310
  uint64_t local_descriptor_value_sub_node_identifier          = 0;
3930
310
  uint16_t entry_index                                         = 0;
3931
310
  int result                                                   = 1;
3932
3933
#if defined( HAVE_DEBUG_OUTPUT )
3934
  uint64_t local_descriptor_value_local_descriptors_identifier = 0;
3935
#endif
3936
3937
310
  if( io_handle == NULL )
3938
0
  {
3939
0
    libcerror_error_set(
3940
0
     error,
3941
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3942
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3943
0
     "%s: invalid IO handle.",
3944
0
     function );
3945
3946
0
    return( -1 );
3947
0
  }
3948
310
  if( ( recursion_depth < 0 )
3949
310
   || ( recursion_depth > LIBPFF_MAXIMUM_LOCAL_DESCRIPTORS_RECURSION_DEPTH ) )
3950
0
  {
3951
0
    libcerror_error_set(
3952
0
     error,
3953
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3954
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
3955
0
     "%s: invalid recursion depth value out of bounds.",
3956
0
     function );
3957
3958
0
    return( -1 );
3959
0
  }
3960
310
  if( libpff_offsets_index_get_index_value_by_identifier(
3961
310
       offsets_index,
3962
310
       io_handle,
3963
310
       file_io_handle,
3964
310
       local_descriptors_identifier,
3965
310
       1,
3966
310
       0,
3967
310
       &offsets_index_value,
3968
310
       error ) != 1 )
3969
0
  {
3970
#if defined( HAVE_DEBUG_OUTPUT )
3971
    if( ( libcnotify_verbose != 0 )
3972
     && ( error != NULL )
3973
     && ( *error != NULL ) )
3974
    {
3975
      libcnotify_print_error_backtrace(
3976
       *error );
3977
    }
3978
#endif
3979
0
    libcerror_error_free(
3980
0
     error );
3981
3982
0
    return( 0 );
3983
0
  }
3984
310
  if( offsets_index_value == NULL )
3985
0
  {
3986
0
    libcerror_error_set(
3987
0
     error,
3988
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3989
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3990
0
     "%s: missing offsets index value.",
3991
0
     function );
3992
3993
0
    return( -1 );
3994
0
  }
3995
#if defined( HAVE_DEBUG_OUTPUT )
3996
  if( libcnotify_verbose != 0 )
3997
  {
3998
    libcnotify_printf(
3999
     "%s: local descriptors node identifier: %" PRIu64 " (%s) at offset: %" PRIi64 " of size: %" PRIu32 "\n",
4000
     function,
4001
     offsets_index_value->identifier,
4002
     ( ( offsets_index_value->identifier & LIBPFF_OFFSET_INDEX_IDENTIFIER_FLAG_INTERNAL ) ? "internal" : "external" ),
4003
     offsets_index_value->file_offset,
4004
     offsets_index_value->data_size );
4005
  }
4006
#endif
4007
310
  if( libpff_local_descriptors_node_initialize(
4008
310
       &local_descriptors_node,
4009
310
       error ) != 1 )
4010
0
  {
4011
0
    libcerror_error_set(
4012
0
     error,
4013
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4014
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
4015
0
     "%s: unable to create local descriptors node.",
4016
0
     function );
4017
4018
0
    libpff_index_value_free(
4019
0
     &offsets_index_value,
4020
0
     NULL );
4021
4022
0
    return( -1 );
4023
0
  }
4024
310
  if( libpff_local_descriptors_node_read_file_io_handle(
4025
310
       local_descriptors_node,
4026
310
       io_handle,
4027
310
       file_io_handle,
4028
310
       0 /* TODO descriptor identifier */,
4029
310
       offsets_index_value->identifier,
4030
310
       offsets_index_value->file_offset,
4031
310
       offsets_index_value->data_size,
4032
310
       error ) != 1 )
4033
310
  {
4034
#if defined( HAVE_DEBUG_OUTPUT )
4035
    if( ( libcnotify_verbose != 0 )
4036
     && ( error != NULL )
4037
     && ( *error != NULL ) )
4038
    {
4039
      libcnotify_print_error_backtrace(
4040
       *error );
4041
    }
4042
#endif
4043
310
    libcerror_error_free(
4044
310
     error );
4045
4046
310
    libpff_index_value_free(
4047
310
     &offsets_index_value,
4048
310
     NULL );
4049
4050
310
    libpff_local_descriptors_node_free(
4051
310
     &local_descriptors_node,
4052
310
     NULL );
4053
4054
310
    return( 0 );
4055
310
  }
4056
0
  for( entry_index = 0;
4057
0
       entry_index < local_descriptors_node->number_of_entries;
4058
0
       entry_index++ )
4059
0
  {
4060
0
    if( libpff_local_descriptors_node_get_entry_data(
4061
0
         local_descriptors_node,
4062
0
         entry_index,
4063
0
         &node_entry_data,
4064
0
         error ) != 1 )
4065
0
    {
4066
0
      libcerror_error_set(
4067
0
       error,
4068
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
4069
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
4070
0
       "%s: unable to retrieve node entry: %" PRIu16 " data.",
4071
0
       function,
4072
0
       entry_index );
4073
4074
0
      libpff_local_descriptors_node_free(
4075
0
       &local_descriptors_node,
4076
0
       NULL );
4077
4078
0
      libpff_index_value_free(
4079
0
       &offsets_index_value,
4080
0
       NULL );
4081
4082
0
      return( -1 );
4083
0
    }
4084
0
    if( node_entry_data == NULL )
4085
0
    {
4086
0
      libcerror_error_set(
4087
0
       error,
4088
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
4089
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
4090
0
       "%s: missing node entry: %" PRIu16 " data.",
4091
0
       function,
4092
0
       entry_index );
4093
4094
0
      libpff_local_descriptors_node_free(
4095
0
       &local_descriptors_node,
4096
0
       NULL );
4097
4098
0
      libpff_index_value_free(
4099
0
       &offsets_index_value,
4100
0
       NULL );
4101
4102
0
      return( -1 );
4103
0
    }
4104
0
    if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
4105
0
    {
4106
0
      byte_stream_copy_to_uint32_little_endian(
4107
0
       node_entry_data,
4108
0
       local_descriptor_value_identifier );
4109
4110
0
      node_entry_data += 4;
4111
0
    }
4112
0
    else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
4113
0
          || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
4114
0
    {
4115
0
      byte_stream_copy_to_uint64_little_endian(
4116
0
       node_entry_data,
4117
0
       local_descriptor_value_identifier );
4118
4119
0
      node_entry_data += 8;
4120
0
    }
4121
    /* Ignore the upper 32-bit of local descriptor identifiers
4122
     */
4123
0
    local_descriptor_value_identifier &= 0xffffffffUL;
4124
4125
    /* Ignore local descriptor values without a data identifier
4126
     */
4127
0
    if( local_descriptor_value_identifier == 0 )
4128
0
    {
4129
#if defined( HAVE_DEBUG_OUTPUT )
4130
      if( libcnotify_verbose != 0 )
4131
      {
4132
        libcnotify_printf(
4133
         "%s: local descriptor entry: %" PRIu8 " identifier: %" PRIu64 " has an empty identifier.\n",
4134
         function,
4135
         entry_index,
4136
         local_descriptor_value_identifier );
4137
      }
4138
#endif
4139
0
      result = 0;
4140
4141
0
      break;
4142
0
    }
4143
0
    if( local_descriptors_node->level == LIBPFF_LOCAL_DESCRIPTOR_NODE_LEVEL_LEAF )
4144
0
    {
4145
0
      if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
4146
0
      {
4147
0
        byte_stream_copy_to_uint32_little_endian(
4148
0
         node_entry_data,
4149
0
         local_descriptor_value_data_identifier );
4150
4151
0
        node_entry_data += 4;
4152
#if defined( HAVE_DEBUG_OUTPUT )
4153
        byte_stream_copy_to_uint32_little_endian(
4154
         node_entry_data,
4155
         local_descriptor_value_local_descriptors_identifier );
4156
#endif
4157
0
        node_entry_data += 4;
4158
0
      }
4159
0
      else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
4160
0
            || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
4161
0
      {
4162
0
        byte_stream_copy_to_uint64_little_endian(
4163
0
         node_entry_data,
4164
0
         local_descriptor_value_data_identifier );
4165
4166
0
        node_entry_data += 8;
4167
#if defined( HAVE_DEBUG_OUTPUT )
4168
        byte_stream_copy_to_uint64_little_endian(
4169
         node_entry_data,
4170
         local_descriptor_value_local_descriptors_identifier );
4171
#endif
4172
0
        node_entry_data += 8;
4173
0
      }
4174
      /* Ignore local descriptor values without a data identifier
4175
       */
4176
0
      if( local_descriptor_value_data_identifier == 0 )
4177
0
      {
4178
#if defined( HAVE_DEBUG_OUTPUT )
4179
        if( libcnotify_verbose != 0 )
4180
        {
4181
          libcnotify_printf(
4182
           "%s: local descriptor entry: %" PRIu8 " identifier: %" PRIu64 " has an empty data identifier.\n",
4183
           function,
4184
           entry_index,
4185
           local_descriptor_value_identifier );
4186
        }
4187
#endif
4188
0
        result = 0;
4189
4190
0
        break;
4191
0
      }
4192
0
    }
4193
0
    else
4194
0
    {
4195
0
      if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
4196
0
      {
4197
0
        byte_stream_copy_to_uint32_little_endian(
4198
0
         node_entry_data,
4199
0
         local_descriptor_value_sub_node_identifier );
4200
4201
0
        node_entry_data += 4;
4202
0
      }
4203
0
      else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
4204
0
            || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
4205
0
      {
4206
0
        byte_stream_copy_to_uint64_little_endian(
4207
0
         node_entry_data,
4208
0
         local_descriptor_value_sub_node_identifier );
4209
4210
0
        node_entry_data += 8;
4211
0
      }
4212
      /* Ignore local descriptor values without a sub node identifier
4213
       */
4214
0
      if( local_descriptor_value_sub_node_identifier == 0 )
4215
0
      {
4216
#if defined( HAVE_DEBUG_OUTPUT )
4217
        if( libcnotify_verbose != 0 )
4218
        {
4219
          libcnotify_printf(
4220
           "%s: local descriptor entry: %" PRIu8 " identifier: %" PRIu64 " has an empty sub node identifier.\n",
4221
           function,
4222
           entry_index,
4223
           local_descriptor_value_identifier );
4224
        }
4225
#endif
4226
0
        result = 0;
4227
4228
0
        break;
4229
0
      }
4230
0
      result = libpff_recover_analyze_local_descriptors(
4231
0
          io_handle,
4232
0
          file_io_handle,
4233
0
          offsets_index,
4234
0
          local_descriptor_value_sub_node_identifier,
4235
0
          recursion_depth + 1,
4236
0
          error );
4237
4238
0
      if( result == -1 )
4239
0
      {
4240
0
        libcerror_error_set(
4241
0
         error,
4242
0
         LIBCERROR_ERROR_DOMAIN_IO,
4243
0
         LIBCERROR_IO_ERROR_READ_FAILED,
4244
0
         "%s: unable to recover local descriptors: %" PRIu64 ".",
4245
0
         function,
4246
0
         local_descriptor_value_sub_node_identifier );
4247
4248
0
        libpff_index_value_free(
4249
0
         &offsets_index_value,
4250
0
         NULL );
4251
4252
0
        libpff_local_descriptors_node_free(
4253
0
         &local_descriptors_node,
4254
0
         NULL );
4255
4256
0
        return( -1 );
4257
0
      }
4258
0
      else if( result == 0 )
4259
0
      {
4260
0
        break;
4261
0
      }
4262
0
    }
4263
0
  }
4264
0
  if( libpff_local_descriptors_node_free(
4265
0
       &local_descriptors_node,
4266
0
       error ) != 1 )
4267
0
  {
4268
0
    libcerror_error_set(
4269
0
     error,
4270
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4271
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
4272
0
     "%s: unable to free local descriptors node.",
4273
0
     function );
4274
4275
0
    libpff_index_value_free(
4276
0
     &offsets_index_value,
4277
0
     NULL );
4278
4279
0
    return( -1 );
4280
0
  }
4281
#if defined( HAVE_DEBUG_OUTPUT )
4282
  if( libcnotify_verbose != 0 )
4283
  {
4284
    libcnotify_printf(
4285
     "\n" );
4286
  }
4287
#endif
4288
0
  libpff_index_value_free(
4289
0
   &offsets_index_value,
4290
   NULL );
4291
0
  return( result );
4292
0
}
4293