Coverage Report

Created: 2026-08-31 07:43

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-2026, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <memory.h>
24
#include <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
497
{
44
497
  static char *function = "libvhdi_metadata_table_initialize";
45
46
497
  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
497
  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
497
  *metadata_table = memory_allocate_structure(
69
497
                     libvhdi_metadata_table_t );
70
71
497
  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
497
  if( memory_set(
83
497
       *metadata_table,
84
497
       0,
85
497
       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
497
  if( libcdata_array_initialize(
102
497
       &( ( *metadata_table )->entries_array ),
103
497
       0,
104
497
       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
497
  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
497
}
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
497
{
135
497
  static char *function = "libvhdi_metadata_table_free";
136
497
  int result            = 1;
137
138
497
  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
497
  if( *metadata_table != NULL )
150
497
  {
151
497
    if( ( *metadata_table )->header != NULL )
152
456
    {
153
456
      if( libvhdi_metadata_table_header_free(
154
456
           &( ( *metadata_table )->header ),
155
456
           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
456
    }
167
497
    if( libcdata_array_free(
168
497
         &( ( *metadata_table )->entries_array ),
169
497
         (int (*)(intptr_t **, libcerror_error_t **)) &libvhdi_metadata_table_entry_free,
170
497
         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
497
    memory_free(
182
497
     *metadata_table );
183
184
497
    *metadata_table = NULL;
185
497
  }
186
497
  return( result );
187
497
}
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
497
{
198
497
  libvhdi_metadata_table_entry_t *metadata_table_entry = NULL;
199
497
  uint8_t *data                                        = NULL;
200
497
  static char *function                                = "libvhdi_metadata_table_read_file_io_handle";
201
497
  size_t data_offset                                   = 0;
202
497
  size_t data_size                                     = 64 * 1024;
203
497
  ssize_t read_count                                   = 0;
204
497
  uint32_t metadata_table_entry_index                  = 0;
205
497
  int entry_index                                      = 0;
206
207
497
  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
497
  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
497
  data = (uint8_t *) memory_allocate(
240
497
                      sizeof( uint8_t ) * data_size );
241
242
497
  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
497
  read_count = libbfio_handle_read_buffer_at_offset(
254
497
                file_io_handle,
255
497
                data,
256
497
                data_size,
257
497
                file_offset,
258
497
                error );
259
260
497
  if( read_count != (ssize_t) data_size )
261
10
  {
262
10
    libcerror_error_set(
263
10
     error,
264
10
     LIBCERROR_ERROR_DOMAIN_IO,
265
10
     LIBCERROR_IO_ERROR_READ_FAILED,
266
10
     "%s: unable to read metadata table data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
267
10
     function,
268
10
     file_offset,
269
10
     file_offset );
270
271
10
    goto on_error;
272
10
  }
273
487
  if( libvhdi_metadata_table_header_initialize(
274
487
       &( metadata_table->header ),
275
487
       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
487
  if( libvhdi_metadata_table_header_read_data(
287
487
       metadata_table->header,
288
487
       data,
289
487
       sizeof( vhdi_metadata_table_header_t ),
290
487
       error ) != 1 )
291
20
  {
292
20
    libcerror_error_set(
293
20
     error,
294
20
     LIBCERROR_ERROR_DOMAIN_IO,
295
20
     LIBCERROR_IO_ERROR_READ_FAILED,
296
20
     "%s: unable to read metadata table header.",
297
20
     function );
298
299
20
    goto on_error;
300
20
  }
301
467
  data_offset = sizeof( vhdi_metadata_table_header_t );
302
303
467
  for( metadata_table_entry_index = 0;
304
7.39k
       metadata_table_entry_index < metadata_table->header->number_of_entries;
305
6.92k
       metadata_table_entry_index++ )
306
6.93k
  {
307
6.93k
    if( libvhdi_metadata_table_entry_initialize(
308
6.93k
         &metadata_table_entry,
309
6.93k
         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
6.93k
    if( libvhdi_metadata_table_entry_read_data(
321
6.93k
         metadata_table_entry,
322
6.93k
         &( data[ data_offset ] ),
323
6.93k
         sizeof( vhdi_metadata_table_entry_t ),
324
6.93k
         error ) != 1 )
325
11
    {
326
11
      libcerror_error_set(
327
11
       error,
328
11
       LIBCERROR_ERROR_DOMAIN_IO,
329
11
       LIBCERROR_IO_ERROR_READ_FAILED,
330
11
       "%s: unable to read metadata table entry.",
331
11
       function );
332
333
11
      goto on_error;
334
11
    }
335
6.92k
    data_offset += sizeof( vhdi_metadata_table_entry_t );
336
337
6.92k
    if( libcdata_array_append_entry(
338
6.92k
         metadata_table->entries_array,
339
6.92k
         &entry_index,
340
6.92k
         (intptr_t *) metadata_table_entry,
341
6.92k
         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
6.92k
    metadata_table_entry = NULL;
353
6.92k
  }
354
456
  memory_free(
355
456
   data );
356
357
456
  return( 1 );
358
359
41
on_error:
360
41
  if( metadata_table_entry != NULL )
361
11
  {
362
11
    libvhdi_metadata_table_entry_free(
363
11
     &metadata_table_entry,
364
11
     NULL );
365
11
  }
366
41
  if( metadata_table->header != NULL )
367
31
  {
368
31
    libvhdi_metadata_table_header_free(
369
31
     &( metadata_table->header ),
370
31
     NULL );
371
31
  }
372
41
  if( data != NULL )
373
41
  {
374
41
    memory_free(
375
41
     data );
376
41
  }
377
41
  libcdata_array_empty(
378
41
   metadata_table->entries_array,
379
41
   (int (*)(intptr_t **, libcerror_error_t **)) &libvhdi_metadata_table_entry_free,
380
41
   NULL );
381
382
41
  return( -1 );
383
467
}
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
456
{
393
456
  static char *function = "libvhdi_metadata_table_get_number_of_entries";
394
395
456
  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
456
  if( libcdata_array_get_number_of_entries(
407
456
       metadata_table->entries_array,
408
456
       number_of_entries,
409
456
       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
456
  return( 1 );
421
456
}
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.21k
{
432
1.21k
  static char *function = "libvhdi_metadata_table_get_entry_by_index";
433
434
1.21k
  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.21k
  if( libcdata_array_get_entry_by_index(
446
1.21k
       metadata_table->entries_array,
447
1.21k
       entry_index,
448
1.21k
       (intptr_t **) entry,
449
1.21k
       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.21k
  return( 1 );
462
1.21k
}
463