Coverage Report

Created: 2025-10-14 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvhdi/libvhdi/libvhdi_parent_locator.c
Line
Count
Source
1
/*
2
 * Parent locator functions
3
 *
4
 * Copyright (C) 2012-2024, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <memory.h>
24
#include <types.h>
25
26
#include "libvhdi_libcdata.h"
27
#include "libvhdi_libcerror.h"
28
#include "libvhdi_libcnotify.h"
29
#include "libvhdi_libuna.h"
30
#include "libvhdi_parent_locator.h"
31
#include "libvhdi_parent_locator_entry.h"
32
#include "libvhdi_parent_locator_header.h"
33
34
#include "vhdi_parent_locator.h"
35
36
/* Creates parent locator
37
 * Make sure the value parent_locator is referencing, is set to NULL
38
 * Returns 1 if successful or -1 on error
39
 */
40
int libvhdi_parent_locator_initialize(
41
     libvhdi_parent_locator_t **parent_locator,
42
     libcerror_error_t **error )
43
360
{
44
360
  static char *function = "libvhdi_parent_locator_initialize";
45
46
360
  if( parent_locator == NULL )
47
0
  {
48
0
    libcerror_error_set(
49
0
     error,
50
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
51
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
52
0
     "%s: invalid parent locator.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
360
  if( *parent_locator != NULL )
58
0
  {
59
0
    libcerror_error_set(
60
0
     error,
61
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
62
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
63
0
     "%s: invalid parent locator value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
360
  *parent_locator = memory_allocate_structure(
69
360
                     libvhdi_parent_locator_t );
70
71
360
  if( *parent_locator == NULL )
72
0
  {
73
0
    libcerror_error_set(
74
0
     error,
75
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
76
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
77
0
     "%s: unable to create parent locator.",
78
0
     function );
79
80
0
    goto on_error;
81
0
  }
82
360
  if( memory_set(
83
360
       *parent_locator,
84
360
       0,
85
360
       sizeof( libvhdi_parent_locator_t ) ) == NULL )
86
0
  {
87
0
    libcerror_error_set(
88
0
     error,
89
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
90
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
91
0
     "%s: unable to clear parent locator.",
92
0
     function );
93
94
0
    memory_free(
95
0
     *parent_locator );
96
97
0
    *parent_locator = NULL;
98
99
0
    return( -1 );
100
0
  }
101
360
  if( libcdata_array_initialize(
102
360
       &( ( *parent_locator )->entries_array ),
103
360
       0,
104
360
       error ) != 1 )
105
0
  {
106
0
    libcerror_error_set(
107
0
     error,
108
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
109
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
110
0
     "%s: unable to create entries array.",
111
0
     function );
112
113
0
    goto on_error;
114
0
  }
115
360
  return( 1 );
116
117
0
on_error:
118
0
  if( *parent_locator != NULL )
119
0
  {
120
0
    memory_free(
121
0
     *parent_locator );
122
123
0
    *parent_locator = NULL;
124
0
  }
125
0
  return( -1 );
126
360
}
127
128
/* Frees parent locator
129
 * Returns 1 if successful or -1 on error
130
 */
131
int libvhdi_parent_locator_free(
132
     libvhdi_parent_locator_t **parent_locator,
133
     libcerror_error_t **error )
134
360
{
135
360
  static char *function = "libvhdi_parent_locator_free";
136
360
  int result            = 1;
137
138
360
  if( parent_locator == NULL )
139
0
  {
140
0
    libcerror_error_set(
141
0
     error,
142
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
143
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
144
0
     "%s: invalid parent locator.",
145
0
     function );
146
147
0
    return( -1 );
148
0
  }
149
360
  if( *parent_locator != NULL )
150
360
  {
151
360
    if( ( *parent_locator )->header != NULL )
152
239
    {
153
239
      if( libvhdi_parent_locator_header_free(
154
239
           &( ( *parent_locator )->header ),
155
239
           error ) != 1 )
156
0
      {
157
0
        libcerror_error_set(
158
0
         error,
159
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
160
0
         LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
161
0
         "%s: unable to free header.",
162
0
         function );
163
164
0
        result = -1;
165
0
      }
166
239
    }
167
360
    if( libcdata_array_free(
168
360
         &( ( *parent_locator )->entries_array ),
169
360
         (int (*)(intptr_t **, libcerror_error_t **)) &libvhdi_parent_locator_entry_free,
170
360
         error ) != 1 )
171
0
    {
172
0
      libcerror_error_set(
173
0
       error,
174
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
175
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
176
0
       "%s: unable to free entries array.",
177
0
       function );
178
179
0
      result = -1;
180
0
    }
181
360
    memory_free(
182
360
     *parent_locator );
183
184
360
    *parent_locator = NULL;
185
360
  }
186
360
  return( result );
187
360
}
188
189
/* Reads the parent locator
190
 * Returns 1 if successful or -1 on error
191
 */
192
int libvhdi_parent_locator_read_data(
193
     libvhdi_parent_locator_t *parent_locator,
194
     const uint8_t *data,
195
     size_t data_size,
196
     libcerror_error_t **error )
197
360
{
198
360
  libvhdi_parent_locator_entry_t *parent_locator_entry = NULL;
199
360
  static char *function                                = "libvhdi_parent_locator_read_data";
200
360
  size_t data_offset                                   = 0;
201
360
  uint32_t parent_locator_entry_index                  = 0;
202
360
  int entry_index                                      = 0;
203
204
360
  if( parent_locator == NULL )
205
0
  {
206
0
    libcerror_error_set(
207
0
     error,
208
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
209
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
210
0
     "%s: invalid parent locator.",
211
0
     function );
212
213
0
    return( -1 );
214
0
  }
215
360
  if( parent_locator->header != NULL )
216
0
  {
217
0
    libcerror_error_set(
218
0
     error,
219
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
220
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
221
0
     "%s: invalid parent locator - header already set.",
222
0
     function );
223
224
0
    return( -1 );
225
0
  }
226
360
  if( data == NULL )
227
0
  {
228
0
    libcerror_error_set(
229
0
     error,
230
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
231
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
232
0
     "%s: invalid data.",
233
0
     function );
234
235
0
    return( -1 );
236
0
  }
237
360
  if( ( data_size < sizeof( vhdi_parent_locator_header_t ) )
238
357
   || ( data_size > (size_t) SSIZE_MAX ) )
239
3
  {
240
3
    libcerror_error_set(
241
3
     error,
242
3
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
243
3
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
244
3
     "%s: invalid data size value out of bounds.",
245
3
     function );
246
247
3
    return( -1 );
248
3
  }
249
357
  if( libvhdi_parent_locator_header_initialize(
250
357
       &( parent_locator->header ),
251
357
       error ) != 1 )
252
0
  {
253
0
    libcerror_error_set(
254
0
     error,
255
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
256
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
257
0
     "%s: unable to create header.",
258
0
     function );
259
260
0
    goto on_error;
261
0
  }
262
357
  if( libvhdi_parent_locator_header_read_data(
263
357
       parent_locator->header,
264
357
       data,
265
357
       sizeof( vhdi_parent_locator_header_t ),
266
357
       error ) != 1 )
267
20
  {
268
20
    libcerror_error_set(
269
20
     error,
270
20
     LIBCERROR_ERROR_DOMAIN_IO,
271
20
     LIBCERROR_IO_ERROR_READ_FAILED,
272
20
     "%s: unable to read parent locator header.",
273
20
     function );
274
275
20
    goto on_error;
276
20
  }
277
337
  data_offset = sizeof( vhdi_parent_locator_header_t );
278
279
337
  for( parent_locator_entry_index = 0;
280
780
       parent_locator_entry_index < parent_locator->header->number_of_entries;
281
443
       parent_locator_entry_index++ )
282
541
  {
283
541
    if( data_offset > ( data_size - sizeof( vhdi_parent_locator_entry_t ) ) )
284
4
    {
285
4
      libcerror_error_set(
286
4
       error,
287
4
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
288
4
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
289
4
       "%s: invalid data size value out of bounds.",
290
4
       function );
291
292
4
      goto on_error;
293
4
    }
294
537
    if( libvhdi_parent_locator_entry_initialize(
295
537
         &parent_locator_entry,
296
537
         error ) != 1 )
297
0
    {
298
0
      libcerror_error_set(
299
0
       error,
300
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
301
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
302
0
       "%s: unable to create parent locator entry: %d.",
303
0
       function,
304
0
       parent_locator_entry_index );
305
306
0
      goto on_error;
307
0
    }
308
537
    if( libvhdi_parent_locator_entry_read_data(
309
537
         parent_locator_entry,
310
537
         &( data[ data_offset ] ),
311
537
         sizeof( vhdi_parent_locator_entry_t ),
312
537
         error ) != 1 )
313
0
    {
314
0
      libcerror_error_set(
315
0
       error,
316
0
       LIBCERROR_ERROR_DOMAIN_IO,
317
0
       LIBCERROR_IO_ERROR_READ_FAILED,
318
0
       "%s: unable to read parent locator entry: %d.",
319
0
       function,
320
0
       parent_locator_entry_index );
321
322
0
      goto on_error;
323
0
    }
324
537
    data_offset += sizeof( vhdi_parent_locator_entry_t );
325
326
537
    if( libvhdi_parent_locator_entry_read_key_value_data(
327
537
         parent_locator_entry,
328
537
         data,
329
537
         data_size,
330
537
         error ) != 1 )
331
94
    {
332
94
      libcerror_error_set(
333
94
       error,
334
94
       LIBCERROR_ERROR_DOMAIN_IO,
335
94
       LIBCERROR_IO_ERROR_READ_FAILED,
336
94
       "%s: unable to read parent locator entry: %d key and value.",
337
94
       function,
338
94
       parent_locator_entry_index );
339
340
94
      goto on_error;
341
94
    }
342
443
    if( libcdata_array_append_entry(
343
443
         parent_locator->entries_array,
344
443
         &entry_index,
345
443
         (intptr_t *) parent_locator_entry,
346
443
         error ) != 1 )
347
0
    {
348
0
      libcerror_error_set(
349
0
       error,
350
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
351
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
352
0
       "%s: unable to append parent locator entry to entries array.",
353
0
       function );
354
355
0
      goto on_error;
356
0
    }
357
443
    parent_locator_entry = NULL;
358
443
  }
359
239
  return( 1 );
360
361
118
on_error:
362
118
  if( parent_locator_entry != NULL )
363
94
  {
364
94
    libvhdi_parent_locator_entry_free(
365
94
     &parent_locator_entry,
366
94
     NULL );
367
94
  }
368
118
  if( parent_locator->header != NULL )
369
118
  {
370
118
    libvhdi_parent_locator_header_free(
371
118
     &( parent_locator->header ),
372
118
     NULL );
373
118
  }
374
118
  return( -1 );
375
337
}
376
377
/* Retrieves the entry of a specific UTF-8 formatted key
378
 * Returns 1 if successful, 0 if not available or -1 on error
379
 */
380
int libvhdi_parent_locator_get_entry_by_utf8_key(
381
     libvhdi_parent_locator_t *parent_locator,
382
     const uint8_t *utf8_string,
383
     size_t utf8_string_length,
384
     libvhdi_parent_locator_entry_t **entry,
385
     libcerror_error_t **error )
386
849
{
387
849
  libvhdi_parent_locator_entry_t *safe_entry = NULL;
388
849
  static char *function                      = "libvhdi_parent_locator_get_entry_by_utf8_key";
389
849
  int entry_index                            = 0;
390
849
  int number_of_entries                      = 0;
391
849
  int result                                 = 0;
392
393
849
  if( parent_locator == NULL )
394
0
  {
395
0
    libcerror_error_set(
396
0
     error,
397
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
398
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
399
0
     "%s: invalid parent locator.",
400
0
     function );
401
402
0
    return( -1 );
403
0
  }
404
849
  if( utf8_string == NULL )
405
0
  {
406
0
    libcerror_error_set(
407
0
     error,
408
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
409
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
410
0
     "%s: invalid UTF-8 string.",
411
0
     function );
412
413
0
    return( -1 );
414
0
  }
415
849
  if( ( utf8_string_length == 0 )
416
849
   || ( utf8_string_length > (size_t) ( SSIZE_MAX - 1 ) ) )
417
0
  {
418
0
    libcerror_error_set(
419
0
     error,
420
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
421
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
422
0
     "%s: invalid UTF-8 string length value out of bounds.",
423
0
     function );
424
425
0
    return( -1 );
426
0
  }
427
849
  if( entry == NULL )
428
0
  {
429
0
    libcerror_error_set(
430
0
     error,
431
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
432
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
433
0
     "%s: invalid entry.",
434
0
     function );
435
436
0
    return( -1 );
437
0
  }
438
849
  *entry = NULL;
439
440
849
  if( libcdata_array_get_number_of_entries(
441
849
       parent_locator->entries_array,
442
849
       &number_of_entries,
443
849
       error ) != 1 )
444
0
  {
445
0
    libcerror_error_set(
446
0
     error,
447
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
448
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
449
0
     "%s: unable to retrieve number of entries from array.",
450
0
     function );
451
452
0
    return( -1 );
453
0
  }
454
849
  for( entry_index = 0;
455
2.15k
       entry_index < number_of_entries;
456
1.30k
       entry_index++ )
457
1.35k
  {
458
1.35k
    if( libcdata_array_get_entry_by_index(
459
1.35k
         parent_locator->entries_array,
460
1.35k
         entry_index,
461
1.35k
         (intptr_t **) &safe_entry,
462
1.35k
         error ) != 1 )
463
0
    {
464
0
      libcerror_error_set(
465
0
       error,
466
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
467
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
468
0
       "%s: unable to retrieve entry: %d from array.",
469
0
       function,
470
0
       entry_index );
471
472
0
      return( -1 );
473
0
    }
474
1.35k
    result = libvhdi_parent_locator_compare_key_with_utf8_string(
475
1.35k
              safe_entry,
476
1.35k
              utf8_string,
477
1.35k
              utf8_string_length,
478
1.35k
              error );
479
480
1.35k
    if( result == -1 )
481
41
    {
482
41
      libcerror_error_set(
483
41
       error,
484
41
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
485
41
       LIBCERROR_RUNTIME_ERROR_GENERIC,
486
41
       "%s: unable to compare UTF-8 string with key.",
487
41
       function );
488
489
41
      return( -1 );
490
41
    }
491
1.30k
    else if( result == LIBUNA_COMPARE_EQUAL )
492
0
    {
493
0
      *entry = safe_entry;
494
495
0
      return( 1 );
496
0
    }
497
1.35k
  }
498
808
  return( 0 );
499
849
}
500