Coverage Report

Created: 2026-07-10 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvhdi/libvhdi/libvhdi_metadata_table_entry.c
Line
Count
Source
1
/*
2
 * Metadata table entry 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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libvhdi_debug.h"
28
#include "libvhdi_libcerror.h"
29
#include "libvhdi_libcnotify.h"
30
#include "libvhdi_libfguid.h"
31
#include "libvhdi_metadata_item_identifier.h"
32
#include "libvhdi_metadata_table_entry.h"
33
34
#include "vhdi_metadata_table.h"
35
36
/* Creates metadata table entry
37
 * Make sure the value metadata_table_entry is referencing, is set to NULL
38
 * Returns 1 if successful or -1 on error
39
 */
40
int libvhdi_metadata_table_entry_initialize(
41
     libvhdi_metadata_table_entry_t **metadata_table_entry,
42
     libcerror_error_t **error )
43
10.8k
{
44
10.8k
  static char *function = "libvhdi_metadata_table_entry_initialize";
45
46
10.8k
  if( metadata_table_entry == 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 entry.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
10.8k
  if( *metadata_table_entry != 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 entry value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
10.8k
  *metadata_table_entry = memory_allocate_structure(
69
10.8k
                           libvhdi_metadata_table_entry_t );
70
71
10.8k
  if( *metadata_table_entry == 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 entry.",
78
0
     function );
79
80
0
    goto on_error;
81
0
  }
82
10.8k
  if( memory_set(
83
10.8k
       *metadata_table_entry,
84
10.8k
       0,
85
10.8k
       sizeof( libvhdi_metadata_table_entry_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 entry.",
92
0
     function );
93
94
0
    goto on_error;
95
0
  }
96
10.8k
  return( 1 );
97
98
0
on_error:
99
0
  if( *metadata_table_entry != NULL )
100
0
  {
101
0
    memory_free(
102
0
     *metadata_table_entry );
103
104
0
    *metadata_table_entry = NULL;
105
0
  }
106
0
  return( -1 );
107
10.8k
}
108
109
/* Frees metadata table entry
110
 * Returns 1 if successful or -1 on error
111
 */
112
int libvhdi_metadata_table_entry_free(
113
     libvhdi_metadata_table_entry_t **metadata_table_entry,
114
     libcerror_error_t **error )
115
10.8k
{
116
10.8k
  static char *function = "libvhdi_metadata_table_entry_free";
117
118
10.8k
  if( metadata_table_entry == NULL )
119
0
  {
120
0
    libcerror_error_set(
121
0
     error,
122
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
123
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
124
0
     "%s: invalid metadata table entry.",
125
0
     function );
126
127
0
    return( -1 );
128
0
  }
129
10.8k
  if( *metadata_table_entry != NULL )
130
10.8k
  {
131
10.8k
    memory_free(
132
10.8k
     *metadata_table_entry );
133
134
10.8k
    *metadata_table_entry = NULL;
135
10.8k
  }
136
10.8k
  return( 1 );
137
10.8k
}
138
139
/* Reads the metadata table entry data
140
 * Returns 1 if successful or -1 on error
141
 */
142
int libvhdi_metadata_table_entry_read_data(
143
     libvhdi_metadata_table_entry_t *metadata_table_entry,
144
     const uint8_t *data,
145
     size_t data_size,
146
     libcerror_error_t **error )
147
10.8k
{
148
10.8k
  static char *function = "libvhdi_metadata_table_entry_read_data";
149
150
#if defined( HAVE_DEBUG_OUTPUT )
151
  uint64_t value_64bit  = 0;
152
#endif
153
154
10.8k
  if( metadata_table_entry == NULL )
155
0
  {
156
0
    libcerror_error_set(
157
0
     error,
158
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
159
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
160
0
     "%s: invalid metadata table entry.",
161
0
     function );
162
163
0
    return( -1 );
164
0
  }
165
10.8k
  if( data == NULL )
166
0
  {
167
0
    libcerror_error_set(
168
0
     error,
169
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
170
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
171
0
     "%s: invalid data.",
172
0
     function );
173
174
0
    return( -1 );
175
0
  }
176
10.8k
  if( ( data_size < sizeof( vhdi_metadata_table_entry_t ) )
177
10.8k
   || ( data_size > (size_t) SSIZE_MAX ) )
178
0
  {
179
0
    libcerror_error_set(
180
0
     error,
181
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
182
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
183
0
     "%s: invalid data size value out of bounds.",
184
0
     function );
185
186
0
    return( -1 );
187
0
  }
188
#if defined( HAVE_DEBUG_OUTPUT )
189
  if( libcnotify_verbose != 0 )
190
  {
191
    libcnotify_printf(
192
     "%s: metadata table entry data:\n",
193
     function );
194
    libcnotify_print_data(
195
     data,
196
     sizeof( vhdi_metadata_table_entry_t ),
197
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
198
  }
199
#endif
200
10.8k
  if( memory_copy(
201
10.8k
       metadata_table_entry->item_identifier,
202
10.8k
       ( (vhdi_metadata_table_entry_t *) data )->item_identifier,
203
10.8k
       16 ) == NULL )
204
0
  {
205
0
    libcerror_error_set(
206
0
     error,
207
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
208
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
209
0
     "%s: unable to copy item identifier.",
210
0
     function );
211
212
0
    return( -1 );
213
0
  }
214
10.8k
  byte_stream_copy_to_uint32_little_endian(
215
10.8k
   ( (vhdi_metadata_table_entry_t *) data )->item_offset,
216
10.8k
   metadata_table_entry->item_offset );
217
218
10.8k
  byte_stream_copy_to_uint32_little_endian(
219
10.8k
   ( (vhdi_metadata_table_entry_t *) data )->item_size,
220
10.8k
   metadata_table_entry->item_size );
221
222
#if defined( HAVE_DEBUG_OUTPUT )
223
  if( libcnotify_verbose != 0 )
224
  {
225
    if( libvhdi_debug_print_guid_value(
226
         function,
227
         "item identifier\t\t\t",
228
         ( (vhdi_metadata_table_entry_t *) data )->item_identifier,
229
         16,
230
         LIBFGUID_ENDIAN_LITTLE,
231
         LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
232
         error ) != 1 )
233
    {
234
      libcerror_error_set(
235
       error,
236
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
237
       LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
238
       "%s: unable to print GUID value.",
239
       function );
240
241
      return( -1 );
242
    }
243
    libcnotify_printf(
244
     "%s: item description\t\t: %s\n",
245
     function,
246
     libvhdi_metadata_item_identifier_get_description(
247
      ( (vhdi_metadata_table_entry_t *) data )->item_identifier ) );
248
249
    libcnotify_printf(
250
     "%s: item offset\t\t\t: 0x%08" PRIx32 "\n",
251
     function,
252
     metadata_table_entry->item_offset );
253
254
    libcnotify_printf(
255
     "%s: item size\t\t\t: %" PRIu32 "\n",
256
     function,
257
     metadata_table_entry->item_size );
258
259
    byte_stream_copy_to_uint64_little_endian(
260
     ( (vhdi_metadata_table_entry_t *) data )->unknown1,
261
     value_64bit );
262
    libcnotify_printf(
263
     "%s: unknown1\t\t\t: 0x%08" PRIx64 "\n",
264
     function,
265
     value_64bit );
266
267
    if( ( value_64bit & 0x00000001UL ) != 0 )
268
    {
269
      libcnotify_printf(
270
       "\t(IsUser)\n" );
271
    }
272
    if( ( value_64bit & 0x00000002UL ) != 0 )
273
    {
274
      libcnotify_printf(
275
       "\t(IsVirtualDisk)\n" );
276
    }
277
    if( ( value_64bit & 0x00000004UL ) != 0 )
278
    {
279
      libcnotify_printf(
280
       "\t(IsRequired)\n" );
281
    }
282
    libcnotify_printf(
283
     "\n" );
284
  }
285
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
286
287
10.8k
  if( metadata_table_entry->item_offset < ( 64 * 1024 ) )
288
19
  {
289
19
    libcerror_error_set(
290
19
     error,
291
19
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
292
19
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
293
19
     "%s: invalid item offset value out of bounds.",
294
19
     function );
295
296
19
    return( -1 );
297
19
  }
298
10.8k
  return( 1 );
299
10.8k
}
300