Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpff/libpff/libpff_local_descriptors.c
Line
Count
Source
1
/*
2
 * Local descriptors 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_definitions.h"
28
#include "libpff_io_handle.h"
29
#include "libpff_libbfio.h"
30
#include "libpff_libcerror.h"
31
#include "libpff_libcnotify.h"
32
#include "libpff_libfcache.h"
33
#include "libpff_libfdata.h"
34
#include "libpff_local_descriptors_node.h"
35
#include "libpff_local_descriptor_value.h"
36
#include "libpff_local_descriptors.h"
37
#include "libpff_offsets_index.h"
38
#include "libpff_unused.h"
39
40
#include "pff_local_descriptors_node.h"
41
42
/* Creates local descriptors
43
 * Make sure the value local_descriptors is referencing, is set to NULL
44
 * Returns 1 if successful or -1 on error
45
 */
46
int libpff_local_descriptors_initialize(
47
     libpff_local_descriptors_t **local_descriptors,
48
     libpff_io_handle_t *io_handle,
49
     libpff_offsets_index_t *offsets_index,
50
     uint32_t descriptor_identifier,
51
     uint64_t root_node_data_identifier,
52
     uint8_t recovered,
53
     libcerror_error_t **error )
54
17.6k
{
55
17.6k
  static char *function = "libpff_local_descriptors_initialize";
56
57
17.6k
  if( local_descriptors == NULL )
58
0
  {
59
0
    libcerror_error_set(
60
0
     error,
61
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
62
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
63
0
     "%s: invalid local descriptors.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
17.6k
  if( *local_descriptors != NULL )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
73
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
74
0
     "%s: invalid local descriptors value already set.",
75
0
     function );
76
77
0
    return( -1 );
78
0
  }
79
17.6k
  if( io_handle == NULL )
80
0
  {
81
0
    libcerror_error_set(
82
0
     error,
83
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
84
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
85
0
     "%s: invalid IO handle.",
86
0
     function );
87
88
0
    return( -1 );
89
0
  }
90
17.6k
  if( offsets_index == NULL )
91
0
  {
92
0
    libcerror_error_set(
93
0
     error,
94
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
95
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
96
0
     "%s: invalid offsets index.",
97
0
     function );
98
99
0
    return( -1 );
100
0
  }
101
17.6k
  *local_descriptors = memory_allocate_structure(
102
17.6k
                        libpff_local_descriptors_t );
103
104
17.6k
  if( *local_descriptors == NULL )
105
0
  {
106
0
    libcerror_error_set(
107
0
     error,
108
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
109
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
110
0
     "%s: unable to create local descriptors.",
111
0
     function );
112
113
0
    goto on_error;
114
0
  }
115
17.6k
  if( memory_set(
116
17.6k
       *local_descriptors,
117
17.6k
       0,
118
17.6k
       sizeof( libpff_local_descriptors_t ) ) == NULL )
119
0
  {
120
0
    libcerror_error_set(
121
0
     error,
122
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
123
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
124
0
     "%s: unable to clear local descriptors.",
125
0
     function );
126
127
0
    memory_free(
128
0
     *local_descriptors );
129
130
0
    *local_descriptors = NULL;
131
132
0
    return( -1 );
133
0
  }
134
17.6k
  if( libfcache_cache_initialize(
135
17.6k
       &( ( *local_descriptors )->local_descriptors_nodes_cache ),
136
17.6k
       LIBPFF_MAXIMUM_CACHE_ENTRIES_LOCAL_DESCRIPTORS_NODES,
137
17.6k
       error ) != 1 )
138
0
  {
139
0
    libcerror_error_set(
140
0
     error,
141
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
142
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
143
0
     "%s: unable to create local descriptors nodes cache.",
144
0
     function );
145
146
0
    goto on_error;
147
0
  }
148
17.6k
  ( *local_descriptors )->io_handle                 = io_handle;
149
17.6k
  ( *local_descriptors )->offsets_index             = offsets_index;
150
17.6k
  ( *local_descriptors )->descriptor_identifier     = descriptor_identifier;
151
17.6k
  ( *local_descriptors )->root_node_data_identifier = root_node_data_identifier;
152
17.6k
  ( *local_descriptors )->recovered                 = recovered;
153
154
17.6k
  return( 1 );
155
156
0
on_error:
157
0
  if( *local_descriptors != NULL )
158
0
  {
159
0
    memory_free(
160
0
     *local_descriptors );
161
162
0
    *local_descriptors = NULL;
163
0
  }
164
0
  return( -1 );
165
17.6k
}
166
167
/* Frees local descriptors
168
 * Returns 1 if successful or -1 on error
169
 */
170
int libpff_local_descriptors_free(
171
     libpff_local_descriptors_t **local_descriptors,
172
     libcerror_error_t **error )
173
17.6k
{
174
17.6k
  static char *function = "libpff_local_descriptors_free";
175
17.6k
  int result            = 1;
176
177
17.6k
  if( local_descriptors == NULL )
178
0
  {
179
0
    libcerror_error_set(
180
0
     error,
181
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
182
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
183
0
     "%s: invalid local descriptors.",
184
0
     function );
185
186
0
    return( -1 );
187
0
  }
188
17.6k
  if( *local_descriptors != NULL )
189
17.6k
  {
190
17.6k
    if( libfcache_cache_free(
191
17.6k
         &( ( *local_descriptors )->local_descriptors_nodes_cache ),
192
17.6k
         error ) != 1 )
193
0
    {
194
0
      libcerror_error_set(
195
0
       error,
196
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
197
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
198
0
       "%s: unable to free local descriptors nodes cache.",
199
0
       function );
200
201
0
      result = -1;
202
0
    }
203
17.6k
    memory_free(
204
17.6k
     *local_descriptors );
205
206
17.6k
    *local_descriptors = NULL;
207
17.6k
  }
208
17.6k
  return( result );
209
17.6k
}
210
211
/* Clones the local descriptors
212
 * Returns 1 if successful or -1 on error
213
 */
214
int libpff_local_descriptors_clone(
215
     libpff_local_descriptors_t **destination_local_descriptors,
216
     libpff_local_descriptors_t *source_local_descriptors,
217
     libcerror_error_t **error )
218
0
{
219
0
  static char *function = "libpff_local_descriptors_clone";
220
221
0
  if( destination_local_descriptors == NULL )
222
0
  {
223
0
    libcerror_error_set(
224
0
     error,
225
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
226
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
227
0
     "%s: invalid destination local descriptors.",
228
0
     function );
229
230
0
    return( -1 );
231
0
  }
232
0
  if( *destination_local_descriptors != NULL )
233
0
  {
234
0
    libcerror_error_set(
235
0
     error,
236
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
237
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
238
0
     "%s: invalid destination local descriptors already set.",
239
0
     function );
240
241
0
    return( -1 );
242
0
  }
243
0
  if( source_local_descriptors == NULL )
244
0
  {
245
0
    *destination_local_descriptors = NULL;
246
247
0
    return( 1 );
248
0
  }
249
0
  if( libpff_local_descriptors_initialize(
250
0
       destination_local_descriptors,
251
0
       source_local_descriptors->io_handle,
252
0
       source_local_descriptors->offsets_index,
253
0
       source_local_descriptors->descriptor_identifier,
254
0
       source_local_descriptors->root_node_data_identifier,
255
0
       source_local_descriptors->recovered,
256
0
       error ) != 1 )
257
0
  {
258
0
    libcerror_error_set(
259
0
     error,
260
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
261
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
262
0
     "%s: unable to create destination local descriptors.",
263
0
     function );
264
265
0
    return( -1 );
266
0
  }
267
0
  return( 1 );
268
0
}
269
270
/* Retrieves the leaf node from an local descriptors node for the specific identifier
271
 * Returns 1 if successful, 0 if no leaf node was found or -1 on error
272
 */
273
int libpff_local_descriptors_get_leaf_node_from_node_by_identifier(
274
     libpff_local_descriptors_t *local_descriptors,
275
     libpff_io_handle_t *io_handle,
276
     libbfio_handle_t *file_io_handle,
277
     uint64_t identifier,
278
     uint64_t data_identifier,
279
     libpff_local_descriptors_node_t **leaf_node,
280
     uint16_t *leaf_node_entry_index,
281
     int recursion_depth,
282
     libcerror_error_t **error )
283
31.1k
{
284
31.1k
  libpff_index_value_t *offsets_index_value               = NULL;
285
31.1k
  libpff_local_descriptors_node_t *local_descriptors_node = NULL;
286
31.1k
  uint8_t *node_entry_data                                = NULL;
287
31.1k
  static char *function                                   = "libpff_local_descriptors_get_leaf_node_from_node_by_identifier";
288
31.1k
  uint64_t entry_identifier                               = 0;
289
31.1k
  uint64_t sub_node_identifier                            = 0;
290
31.1k
  uint16_t entry_index                                    = 0;
291
31.1k
  int result                                              = 0;
292
293
31.1k
  if( local_descriptors == NULL )
294
0
  {
295
0
    libcerror_error_set(
296
0
     error,
297
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
298
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
299
0
     "%s: invalid local descriptors.",
300
0
     function );
301
302
0
    return( -1 );
303
0
  }
304
31.1k
  if( io_handle == NULL )
305
0
  {
306
0
    libcerror_error_set(
307
0
     error,
308
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
309
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
310
0
     "%s: invalid IO handle.",
311
0
     function );
312
313
0
    return( -1 );
314
0
  }
315
31.1k
  if( leaf_node == NULL )
316
0
  {
317
0
    libcerror_error_set(
318
0
     error,
319
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
320
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
321
0
     "%s: invalid leaf node.",
322
0
     function );
323
324
0
    return( -1 );
325
0
  }
326
31.1k
  if( leaf_node_entry_index == NULL )
327
0
  {
328
0
    libcerror_error_set(
329
0
     error,
330
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
331
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
332
0
     "%s: invalid leaf node entry index.",
333
0
     function );
334
335
0
    return( -1 );
336
0
  }
337
31.1k
  if( ( recursion_depth < 0 )
338
31.1k
   || ( recursion_depth > LIBPFF_MAXIMUM_LOCAL_DESCRIPTORS_RECURSION_DEPTH ) )
339
4
  {
340
4
    libcerror_error_set(
341
4
     error,
342
4
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
343
4
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
344
4
     "%s: invalid recursion depth value out of bounds.",
345
4
     function );
346
347
4
    return( -1 );
348
4
  }
349
#if defined( HAVE_DEBUG_OUTPUT )
350
  if( libcnotify_verbose != 0 )
351
  {
352
    libcnotify_printf(
353
     "%s: requested identifier\t: 0x%08" PRIx64 " (%" PRIu64 ").\n",
354
     function,
355
     data_identifier,
356
     data_identifier );
357
  }
358
#endif
359
/* TODO handle multiple recovered offsets index values */
360
31.1k
  result = libpff_offsets_index_get_index_value_by_identifier(
361
31.1k
            local_descriptors->offsets_index,
362
31.1k
            io_handle,
363
31.1k
            file_io_handle,
364
31.1k
            data_identifier,
365
31.1k
            local_descriptors->recovered,
366
31.1k
            0,
367
31.1k
            &offsets_index_value,
368
31.1k
            error );
369
370
31.1k
  if( result == -1 )
371
50
  {
372
50
    libcerror_error_set(
373
50
     error,
374
50
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
375
50
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
376
50
     "%s: unable to find offsets index value: 0x%08" PRIx64 " (%" PRIu64 ").",
377
50
     function,
378
50
     data_identifier,
379
50
     data_identifier );
380
381
50
    goto on_error;
382
50
  }
383
31.0k
  else if( result == 0 )
384
6.04k
  {
385
6.04k
    return( 0 );
386
6.04k
  }
387
25.0k
  result = 0;
388
389
25.0k
  if( offsets_index_value == NULL )
390
0
  {
391
0
    libcerror_error_set(
392
0
     error,
393
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
394
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
395
0
     "%s: missing offsets index value: 0x%08" PRIx64 " (%" PRIu64 ").",
396
0
     function,
397
0
     data_identifier,
398
0
     data_identifier );
399
400
0
    goto on_error;
401
0
  }
402
#if defined( HAVE_DEBUG_OUTPUT )
403
  if( libcnotify_verbose != 0 )
404
  {
405
    libcnotify_printf(
406
     "%s: local descriptors node: identifier: %" PRIu64 " (%s) at offset: 0x%08" PRIx64 " of size: %" PRIu32 "\n",
407
     function,
408
     offsets_index_value->identifier,
409
     ( ( offsets_index_value->identifier & LIBPFF_OFFSET_INDEX_IDENTIFIER_FLAG_INTERNAL ) ? "internal" : "external" ),
410
     offsets_index_value->file_offset,
411
     offsets_index_value->data_size );
412
  }
413
#endif
414
25.0k
  if( libpff_local_descriptors_node_initialize(
415
25.0k
       &local_descriptors_node,
416
25.0k
       error ) != 1 )
417
0
  {
418
0
    libcerror_error_set(
419
0
     error,
420
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
421
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
422
0
     "%s: unable to create local descriptors node.",
423
0
     function );
424
425
0
    goto on_error;
426
0
  }
427
25.0k
  if( libpff_local_descriptors_node_read_file_io_handle(
428
25.0k
       local_descriptors_node,
429
25.0k
       io_handle,
430
25.0k
       file_io_handle,
431
25.0k
       local_descriptors->descriptor_identifier,
432
25.0k
       data_identifier,
433
25.0k
       offsets_index_value->file_offset,
434
25.0k
       offsets_index_value->data_size,
435
25.0k
       error ) != 1 )
436
730
  {
437
730
    libcerror_error_set(
438
730
     error,
439
730
     LIBCERROR_ERROR_DOMAIN_IO,
440
730
     LIBCERROR_IO_ERROR_READ_FAILED,
441
730
     "%s: unable to read local descriptors node at offset: %" PRIi64 " (0x%08" PRIx64 ").",
442
730
     function,
443
730
     offsets_index_value->file_offset,
444
730
     offsets_index_value->file_offset );
445
446
730
    goto on_error;
447
730
  }
448
24.2k
  for( entry_index = 0;
449
68.9k
       entry_index < local_descriptors_node->number_of_entries;
450
44.6k
       entry_index++ )
451
66.3k
  {
452
66.3k
    if( libpff_local_descriptors_node_get_entry_data(
453
66.3k
         local_descriptors_node,
454
66.3k
         entry_index,
455
66.3k
         &node_entry_data,
456
66.3k
         error ) != 1 )
457
0
    {
458
0
      libcerror_error_set(
459
0
       error,
460
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
461
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
462
0
       "%s: unable to retrieve node entry: %" PRIu16 " data.",
463
0
       function,
464
0
       entry_index );
465
466
0
      goto on_error;
467
0
    }
468
66.3k
    if( node_entry_data == NULL )
469
0
    {
470
0
      libcerror_error_set(
471
0
       error,
472
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
473
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
474
0
       "%s: missing node entry: %" PRIu16 " data.",
475
0
       function,
476
0
       entry_index );
477
478
0
      goto on_error;
479
0
    }
480
66.3k
    if( local_descriptors->io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
481
2.35k
    {
482
2.35k
      byte_stream_copy_to_uint32_little_endian(
483
2.35k
       ( (pff_local_descriptor_branch_node_entry_type_32bit_t *) node_entry_data )->identifier,
484
2.35k
       entry_identifier );
485
2.35k
    }
486
64.0k
    else if( ( local_descriptors->io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
487
0
          || ( local_descriptors->io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
488
64.0k
    {
489
64.0k
      byte_stream_copy_to_uint64_little_endian(
490
64.0k
       ( (pff_local_descriptor_branch_node_entry_type_64bit_t *) node_entry_data )->identifier,
491
64.0k
       entry_identifier );
492
64.0k
    }
493
    /* Ignore the upper 32-bit of local descriptor identifiers
494
     */
495
66.3k
    entry_identifier &= 0xffffffffUL;
496
497
66.3k
    if( local_descriptors_node->level != LIBPFF_LOCAL_DESCRIPTOR_NODE_LEVEL_LEAF )
498
2.35k
    {
499
2.35k
      if( ( entry_index == 0 )
500
1.22k
       || ( identifier >= entry_identifier ) )
501
1.78k
      {
502
1.78k
        if( io_handle->file_type == LIBPFF_FILE_TYPE_32BIT )
503
1.78k
        {
504
1.78k
          byte_stream_copy_to_uint32_little_endian(
505
1.78k
           ( (pff_local_descriptor_branch_node_entry_type_32bit_t *) node_entry_data )->sub_node_identifier,
506
1.78k
           sub_node_identifier );
507
1.78k
        }
508
0
        else if( ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT )
509
0
              || ( io_handle->file_type == LIBPFF_FILE_TYPE_64BIT_4K_PAGE ) )
510
0
        {
511
0
          byte_stream_copy_to_uint64_little_endian(
512
0
           ( (pff_local_descriptor_branch_node_entry_type_64bit_t *) node_entry_data )->sub_node_identifier,
513
0
           sub_node_identifier );
514
0
        }
515
1.78k
      }
516
2.35k
    }
517
64.0k
    else if( identifier == entry_identifier )
518
17.9k
    {
519
17.9k
      *leaf_node             = local_descriptors_node;
520
17.9k
      *leaf_node_entry_index = entry_index;
521
522
17.9k
      result = 1;
523
17.9k
    }
524
    /* A branch node contains the identifier of its first sub node
525
     */
526
66.3k
    if( identifier <= entry_identifier )
527
21.7k
    {
528
21.7k
      break;
529
21.7k
    }
530
66.3k
  }
531
24.2k
  if( local_descriptors_node->level != LIBPFF_INDEX_NODE_LEVEL_LEAF )
532
1.12k
  {
533
1.12k
    result = libpff_local_descriptors_get_leaf_node_from_node_by_identifier(
534
1.12k
        local_descriptors,
535
1.12k
              io_handle,
536
1.12k
        file_io_handle,
537
1.12k
        identifier,
538
1.12k
        sub_node_identifier,
539
1.12k
        leaf_node,
540
1.12k
        leaf_node_entry_index,
541
1.12k
        recursion_depth + 1,
542
1.12k
        error );
543
544
1.12k
    if( result == -1 )
545
1.04k
    {
546
1.04k
      libcerror_error_set(
547
1.04k
       error,
548
1.04k
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
549
1.04k
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
550
1.04k
       "%s: unable to retrieve leaf node by identifier: 0x%08" PRIx64 " (%" PRIu64 ") from node at offset: %" PRIi64 " (0x%08" PRIx64 ").",
551
1.04k
       function,
552
1.04k
       identifier,
553
1.04k
       identifier,
554
1.04k
       offsets_index_value->file_offset,
555
1.04k
       offsets_index_value->file_offset );
556
557
1.04k
      goto on_error;
558
1.04k
    }
559
1.12k
  }
560
23.2k
  if( ( local_descriptors_node != NULL )
561
23.2k
   && ( local_descriptors_node != *leaf_node ) )
562
5.31k
  {
563
5.31k
    if( libpff_local_descriptors_node_free(
564
5.31k
         &local_descriptors_node,
565
5.31k
         error ) != 1 )
566
0
    {
567
0
      libcerror_error_set(
568
0
       error,
569
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
570
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
571
0
       "%s: unable to free local descriptors node.",
572
0
       function );
573
574
0
      goto on_error;
575
0
    }
576
5.31k
  }
577
23.2k
  if( libpff_index_value_free(
578
23.2k
       &offsets_index_value,
579
23.2k
       error ) != 1 )
580
0
  {
581
0
    libcerror_error_set(
582
0
     error,
583
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
584
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
585
0
     "%s: unable to free offsets index value.",
586
0
     function );
587
588
0
    goto on_error;
589
0
  }
590
23.2k
  return( result );
591
592
1.82k
on_error:
593
1.82k
  if( local_descriptors_node != NULL )
594
1.77k
  {
595
1.77k
    libpff_local_descriptors_node_free(
596
1.77k
     &local_descriptors_node,
597
1.77k
     NULL );
598
1.77k
  }
599
1.82k
  if( offsets_index_value != NULL )
600
1.77k
  {
601
1.77k
    libpff_index_value_free(
602
1.77k
     &offsets_index_value,
603
1.77k
     NULL );
604
1.77k
  }
605
1.82k
  return( -1 );
606
23.2k
}
607
608
/* Retrieves the value for the specific identifier
609
 * Returns 1 if successful, 0 if no value was found or -1 on error
610
 */
611
int libpff_local_descriptors_get_value_by_identifier(
612
     libpff_local_descriptors_t *local_descriptors,
613
     libpff_io_handle_t *io_handle,
614
     libbfio_handle_t *file_io_handle,
615
     uint64_t identifier,
616
     libpff_local_descriptor_value_t **local_descriptor_value,
617
     libcerror_error_t **error )
618
29.9k
{
619
29.9k
  libpff_local_descriptor_value_t *safe_local_descriptor_value = NULL;
620
29.9k
  libpff_local_descriptors_node_t *leaf_node                   = NULL;
621
29.9k
  uint8_t *node_entry_data                                     = NULL;
622
29.9k
  static char *function                                        = "libpff_local_descriptors_get_value_by_identifier";
623
29.9k
  uint16_t leaf_node_entry_index                               = 0;
624
29.9k
  int result                                                   = 0;
625
626
29.9k
  if( local_descriptors == NULL )
627
0
  {
628
0
    libcerror_error_set(
629
0
     error,
630
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
631
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
632
0
     "%s: invalid local descriptors.",
633
0
     function );
634
635
0
    return( -1 );
636
0
  }
637
29.9k
  if( local_descriptor_value == NULL )
638
0
  {
639
0
    libcerror_error_set(
640
0
     error,
641
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
642
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
643
0
     "%s: invalid local descriptor value.",
644
0
     function );
645
646
0
    return( -1 );
647
0
  }
648
#if defined( HAVE_DEBUG_OUTPUT )
649
  if( libcnotify_verbose != 0 )
650
  {
651
    libcnotify_printf(
652
     "%s: requested identifier\t\t\t: 0x%08" PRIx64 " (%" PRIu64 ").\n",
653
     function,
654
     identifier,
655
     identifier );
656
  }
657
#endif
658
29.9k
  result = libpff_local_descriptors_get_leaf_node_from_node_by_identifier(
659
29.9k
      local_descriptors,
660
29.9k
      io_handle,
661
29.9k
      file_io_handle,
662
29.9k
      identifier,
663
29.9k
      local_descriptors->root_node_data_identifier,
664
29.9k
      &leaf_node,
665
29.9k
      &leaf_node_entry_index,
666
29.9k
      0,
667
29.9k
      error );
668
669
29.9k
  if( result == -1 )
670
784
  {
671
784
    libcerror_error_set(
672
784
     error,
673
784
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
674
784
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
675
784
     "%s: unable to retrieve leaf node by identifier: 0x%08" PRIx64 " (%" PRIu64 ") from root node.",
676
784
     function,
677
784
     identifier,
678
784
     identifier );
679
680
784
    goto on_error;
681
784
  }
682
29.2k
  else if( result != 0 )
683
17.9k
  {
684
17.9k
    if( libpff_local_descriptors_node_get_entry_data(
685
17.9k
         leaf_node,
686
17.9k
         leaf_node_entry_index,
687
17.9k
         &node_entry_data,
688
17.9k
         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 node entry: %" PRIu16 " data.",
695
0
       function,
696
0
       leaf_node_entry_index );
697
698
0
      goto on_error;
699
0
    }
700
#if defined( __clang_analyzer__ )
701
    __builtin_assume( leaf_node != NULL );
702
#endif
703
17.9k
    if( node_entry_data == NULL )
704
0
    {
705
0
      libcerror_error_set(
706
0
       error,
707
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
708
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
709
0
       "%s: missing node entry: %" PRIu16 " data.",
710
0
       function,
711
0
       leaf_node_entry_index );
712
713
0
      goto on_error;
714
0
    }
715
17.9k
    if( libpff_local_descriptor_value_initialize(
716
17.9k
         &safe_local_descriptor_value,
717
17.9k
         error ) != 1 )
718
0
    {
719
0
      libcerror_error_set(
720
0
       error,
721
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
722
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
723
0
       "%s: unable to create local descriptor value.",
724
0
       function );
725
726
0
      goto on_error;
727
0
    }
728
17.9k
    if( libpff_local_descriptor_value_read_data(
729
17.9k
         safe_local_descriptor_value,
730
17.9k
         io_handle,
731
17.9k
         node_entry_data,
732
17.9k
         (size_t) leaf_node->entry_size,
733
17.9k
         error ) != 1 )
734
0
    {
735
0
      libcerror_error_set(
736
0
       error,
737
0
       LIBCERROR_ERROR_DOMAIN_IO,
738
0
       LIBCERROR_IO_ERROR_READ_FAILED,
739
0
       "%s: unable to read local descriptor value.",
740
0
       function );
741
742
0
      goto on_error;
743
0
    }
744
17.9k
    if( libpff_local_descriptors_node_free(
745
17.9k
         &leaf_node,
746
17.9k
         error ) != 1 )
747
0
    {
748
0
      libcerror_error_set(
749
0
       error,
750
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
751
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
752
0
       "%s: unable to free leaf node.",
753
0
       function );
754
755
0
      goto on_error;
756
0
    }
757
17.9k
  }
758
29.2k
  *local_descriptor_value = safe_local_descriptor_value;
759
760
29.2k
  return( result );
761
762
784
on_error:
763
784
  if( safe_local_descriptor_value != NULL )
764
0
  {
765
0
    libpff_local_descriptor_value_free(
766
0
     &safe_local_descriptor_value,
767
0
     NULL );
768
0
  }
769
784
  if( leaf_node != NULL )
770
0
  {
771
0
    libpff_local_descriptors_node_free(
772
0
     &leaf_node,
773
     NULL );
774
0
  }
775
784
  return( -1 );
776
29.9k
}
777