Coverage Report

Created: 2026-01-20 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvhdi/libvhdi/libvhdi_metadata_table.c
Line
Count
Source
1
/*
2
 * Metadata table functions
3
 *
4
 * Copyright (C) 2012-2025, 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_libbfio.h"
27
#include "libvhdi_libcdata.h"
28
#include "libvhdi_libcerror.h"
29
#include "libvhdi_libcnotify.h"
30
#include "libvhdi_metadata_table.h"
31
#include "libvhdi_metadata_table_entry.h"
32
#include "libvhdi_metadata_table_header.h"
33
34
#include "vhdi_metadata_table.h"
35
36
/* Creates metadata table
37
 * Make sure the value metadata_table is referencing, is set to NULL
38
 * Returns 1 if successful or -1 on error
39
 */
40
int libvhdi_metadata_table_initialize(
41
     libvhdi_metadata_table_t **metadata_table,
42
     libcerror_error_t **error )
43
524
{
44
524
  static char *function = "libvhdi_metadata_table_initialize";
45
46
524
  if( metadata_table == 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 metadata table.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
524
  if( *metadata_table != 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 metadata table value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
524
  *metadata_table = memory_allocate_structure(
69
524
                     libvhdi_metadata_table_t );
70
71
524
  if( *metadata_table == 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 metadata table.",
78
0
     function );
79
80
0
    goto on_error;
81
0
  }
82
524
  if( memory_set(
83
524
       *metadata_table,
84
524
       0,
85
524
       sizeof( libvhdi_metadata_table_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 metadata table.",
92
0
     function );
93
94
0
    memory_free(
95
0
     *metadata_table );
96
97
0
    *metadata_table = NULL;
98
99
0
    return( -1 );
100
0
  }
101
524
  if( libcdata_array_initialize(
102
524
       &( ( *metadata_table )->entries_array ),
103
524
       0,
104
524
       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
524
  return( 1 );
116
117
0
on_error:
118
0
  if( *metadata_table != NULL )
119
0
  {
120
0
    memory_free(
121
0
     *metadata_table );
122
123
0
    *metadata_table = NULL;
124
0
  }
125
0
  return( -1 );
126
524
}
127
128
/* Frees metadata table
129
 * Returns 1 if successful or -1 on error
130
 */
131
int libvhdi_metadata_table_free(
132
     libvhdi_metadata_table_t **metadata_table,
133
     libcerror_error_t **error )
134
524
{
135
524
  static char *function = "libvhdi_metadata_table_free";
136
524
  int result            = 1;
137
138
524
  if( metadata_table == 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 metadata table.",
145
0
     function );
146
147
0
    return( -1 );
148
0
  }
149
524
  if( *metadata_table != NULL )
150
524
  {
151
524
    if( ( *metadata_table )->header != NULL )
152
486
    {
153
486
      if( libvhdi_metadata_table_header_free(
154
486
           &( ( *metadata_table )->header ),
155
486
           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
486
    }
167
524
    if( libcdata_array_free(
168
524
         &( ( *metadata_table )->entries_array ),
169
524
         (int (*)(intptr_t **, libcerror_error_t **)) &libvhdi_metadata_table_entry_free,
170
524
         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
524
    memory_free(
182
524
     *metadata_table );
183
184
524
    *metadata_table = NULL;
185
524
  }
186
524
  return( result );
187
524
}
188
189
/* Reads the metadata table
190
 * Returns 1 if successful or -1 on error
191
 */
192
int libvhdi_metadata_table_read_file_io_handle(
193
     libvhdi_metadata_table_t *metadata_table,
194
     libbfio_handle_t *file_io_handle,
195
     off64_t file_offset,
196
     libcerror_error_t **error )
197
524
{
198
524
  libvhdi_metadata_table_entry_t *metadata_table_entry = NULL;
199
524
  uint8_t *data                                        = NULL;
200
524
  static char *function                                = "libvhdi_metadata_table_read_file_io_handle";
201
524
  size_t data_offset                                   = 0;
202
524
  size_t data_size                                     = 64 * 1024;
203
524
  ssize_t read_count                                   = 0;
204
524
  uint32_t metadata_table_entry_index                  = 0;
205
524
  int entry_index                                      = 0;
206
207
524
  if( metadata_table == NULL )
208
0
  {
209
0
    libcerror_error_set(
210
0
     error,
211
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
212
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
213
0
     "%s: invalid metadata table.",
214
0
     function );
215
216
0
    return( -1 );
217
0
  }
218
524
  if( metadata_table->header != NULL )
219
0
  {
220
0
    libcerror_error_set(
221
0
     error,
222
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
223
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
224
0
     "%s: invalid metadata table - header already set.",
225
0
     function );
226
227
0
    return( -1 );
228
0
  }
229
#if defined( HAVE_DEBUG_OUTPUT )
230
  if( libcnotify_verbose != 0 )
231
  {
232
    libcnotify_printf(
233
     "%s: reading metadata table at offset: %" PRIi64 " (0x%08" PRIx64 ").\n",
234
     function,
235
     file_offset,
236
     file_offset );
237
  }
238
#endif
239
524
  data = (uint8_t *) memory_allocate(
240
524
                      sizeof( uint8_t ) * data_size );
241
242
524
  if( data == NULL )
243
0
  {
244
0
    libcerror_error_set(
245
0
     error,
246
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
247
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
248
0
     "%s: unable to create metadata table data.",
249
0
     function );
250
251
0
    goto on_error;
252
0
  }
253
524
  read_count = libbfio_handle_read_buffer_at_offset(
254
524
                file_io_handle,
255
524
                data,
256
524
                data_size,
257
524
                file_offset,
258
524
                error );
259
260
524
  if( read_count != (ssize_t) data_size )
261
6
  {
262
6
    libcerror_error_set(
263
6
     error,
264
6
     LIBCERROR_ERROR_DOMAIN_IO,
265
6
     LIBCERROR_IO_ERROR_READ_FAILED,
266
6
     "%s: unable to read metadata table data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
267
6
     function,
268
6
     file_offset,
269
6
     file_offset );
270
271
6
    goto on_error;
272
6
  }
273
518
  if( libvhdi_metadata_table_header_initialize(
274
518
       &( metadata_table->header ),
275
518
       error ) != 1 )
276
0
  {
277
0
    libcerror_error_set(
278
0
     error,
279
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
280
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
281
0
     "%s: unable to create header.",
282
0
     function );
283
284
0
    goto on_error;
285
0
  }
286
518
  if( libvhdi_metadata_table_header_read_data(
287
518
       metadata_table->header,
288
518
       data,
289
518
       sizeof( vhdi_metadata_table_header_t ),
290
518
       error ) != 1 )
291
19
  {
292
19
    libcerror_error_set(
293
19
     error,
294
19
     LIBCERROR_ERROR_DOMAIN_IO,
295
19
     LIBCERROR_IO_ERROR_READ_FAILED,
296
19
     "%s: unable to read metadata table header.",
297
19
     function );
298
299
19
    goto on_error;
300
19
  }
301
499
  data_offset = sizeof( vhdi_metadata_table_header_t );
302
303
499
  for( metadata_table_entry_index = 0;
304
10.3k
       metadata_table_entry_index < metadata_table->header->number_of_entries;
305
9.84k
       metadata_table_entry_index++ )
306
9.85k
  {
307
9.85k
    if( libvhdi_metadata_table_entry_initialize(
308
9.85k
         &metadata_table_entry,
309
9.85k
         error ) != 1 )
310
0
    {
311
0
      libcerror_error_set(
312
0
       error,
313
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
314
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
315
0
       "%s: unable to create entry.",
316
0
       function );
317
318
0
      goto on_error;
319
0
    }
320
9.85k
    if( libvhdi_metadata_table_entry_read_data(
321
9.85k
         metadata_table_entry,
322
9.85k
         &( data[ data_offset ] ),
323
9.85k
         sizeof( vhdi_metadata_table_entry_t ),
324
9.85k
         error ) != 1 )
325
13
    {
326
13
      libcerror_error_set(
327
13
       error,
328
13
       LIBCERROR_ERROR_DOMAIN_IO,
329
13
       LIBCERROR_IO_ERROR_READ_FAILED,
330
13
       "%s: unable to read metadata table entry.",
331
13
       function );
332
333
13
      goto on_error;
334
13
    }
335
9.84k
    data_offset += sizeof( vhdi_metadata_table_entry_t );
336
337
9.84k
    if( libcdata_array_append_entry(
338
9.84k
         metadata_table->entries_array,
339
9.84k
         &entry_index,
340
9.84k
         (intptr_t *) metadata_table_entry,
341
9.84k
         error ) != 1 )
342
0
    {
343
0
      libcerror_error_set(
344
0
       error,
345
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
346
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
347
0
       "%s: unable to append metadata table entry to entries array.",
348
0
       function );
349
350
0
      goto on_error;
351
0
    }
352
9.84k
    metadata_table_entry = NULL;
353
9.84k
  }
354
486
  memory_free(
355
486
   data );
356
357
486
  return( 1 );
358
359
38
on_error:
360
38
  if( metadata_table_entry != NULL )
361
13
  {
362
13
    libvhdi_metadata_table_entry_free(
363
13
     &metadata_table_entry,
364
13
     NULL );
365
13
  }
366
38
  if( metadata_table->header != NULL )
367
32
  {
368
32
    libvhdi_metadata_table_header_free(
369
32
     &( metadata_table->header ),
370
32
     NULL );
371
32
  }
372
38
  if( data != NULL )
373
38
  {
374
38
    memory_free(
375
38
     data );
376
38
  }
377
38
  libcdata_array_empty(
378
38
   metadata_table->entries_array,
379
38
   (int (*)(intptr_t **, libcerror_error_t **)) &libvhdi_metadata_table_entry_free,
380
38
   NULL );
381
382
38
  return( -1 );
383
499
}
384
385
/* Retrieves the number of entries
386
 * Returns 1 if successful or -1 on error
387
 */
388
int libvhdi_metadata_table_get_number_of_entries(
389
     libvhdi_metadata_table_t *metadata_table,
390
     int *number_of_entries,
391
     libcerror_error_t **error )
392
486
{
393
486
  static char *function = "libvhdi_metadata_table_get_number_of_entries";
394
395
486
  if( metadata_table == NULL )
396
0
  {
397
0
    libcerror_error_set(
398
0
     error,
399
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
400
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
401
0
     "%s: invalid metadata table.",
402
0
     function );
403
404
0
    return( -1 );
405
0
  }
406
486
  if( libcdata_array_get_number_of_entries(
407
486
       metadata_table->entries_array,
408
486
       number_of_entries,
409
486
       error ) != 1 )
410
0
  {
411
0
    libcerror_error_set(
412
0
     error,
413
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
414
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
415
0
     "%s: unable to retrieve number of entries from array.",
416
0
     function );
417
418
0
    return( -1 );
419
0
  }
420
486
  return( 1 );
421
486
}
422
423
/* Retrieves a specific entry
424
 * Returns 1 if successful or -1 on error
425
 */
426
int libvhdi_metadata_table_get_entry_by_index(
427
     libvhdi_metadata_table_t *metadata_table,
428
     int entry_index,
429
     libvhdi_metadata_table_entry_t **entry,
430
     libcerror_error_t **error )
431
1.22k
{
432
1.22k
  static char *function = "libvhdi_metadata_table_get_entry_by_index";
433
434
1.22k
  if( metadata_table == NULL )
435
0
  {
436
0
    libcerror_error_set(
437
0
     error,
438
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
439
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
440
0
     "%s: invalid metadata table.",
441
0
     function );
442
443
0
    return( -1 );
444
0
  }
445
1.22k
  if( libcdata_array_get_entry_by_index(
446
1.22k
       metadata_table->entries_array,
447
1.22k
       entry_index,
448
1.22k
       (intptr_t **) entry,
449
1.22k
       error ) != 1 )
450
0
  {
451
0
    libcerror_error_set(
452
0
     error,
453
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
454
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
455
0
     "%s: unable to retrieve entry: %d from array.",
456
0
     function,
457
0
     entry_index );
458
459
0
    return( -1 );
460
0
  }
461
1.22k
  return( 1 );
462
1.22k
}
463