Coverage Report

Created: 2025-10-14 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvhdi/libvhdi/libvhdi_metadata_table_header.c
Line
Count
Source
1
/*
2
 * Metadata table header 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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libvhdi_libcerror.h"
28
#include "libvhdi_libcnotify.h"
29
#include "libvhdi_metadata_table_header.h"
30
31
#include "vhdi_metadata_table.h"
32
33
/* Creates metadata table header
34
 * Make sure the value metadata_table_header is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libvhdi_metadata_table_header_initialize(
38
     libvhdi_metadata_table_header_t **metadata_table_header,
39
     libcerror_error_t **error )
40
517
{
41
517
  static char *function = "libvhdi_metadata_table_header_initialize";
42
43
517
  if( metadata_table_header == NULL )
44
0
  {
45
0
    libcerror_error_set(
46
0
     error,
47
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
48
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
49
0
     "%s: invalid metadata table header.",
50
0
     function );
51
52
0
    return( -1 );
53
0
  }
54
517
  if( *metadata_table_header != NULL )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
59
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
60
0
     "%s: invalid metadata table header value already set.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
517
  *metadata_table_header = memory_allocate_structure(
66
517
                            libvhdi_metadata_table_header_t );
67
68
517
  if( *metadata_table_header == NULL )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
73
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
74
0
     "%s: unable to create metadata table header.",
75
0
     function );
76
77
0
    goto on_error;
78
0
  }
79
517
  if( memory_set(
80
517
       *metadata_table_header,
81
517
       0,
82
517
       sizeof( libvhdi_metadata_table_header_t ) ) == NULL )
83
0
  {
84
0
    libcerror_error_set(
85
0
     error,
86
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
87
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
88
0
     "%s: unable to clear metadata table header.",
89
0
     function );
90
91
0
    goto on_error;
92
0
  }
93
517
  return( 1 );
94
95
0
on_error:
96
0
  if( *metadata_table_header != NULL )
97
0
  {
98
0
    memory_free(
99
0
     *metadata_table_header );
100
101
0
    *metadata_table_header = NULL;
102
0
  }
103
0
  return( -1 );
104
517
}
105
106
/* Frees metadata table header
107
 * Returns 1 if successful or -1 on error
108
 */
109
int libvhdi_metadata_table_header_free(
110
     libvhdi_metadata_table_header_t **metadata_table_header,
111
     libcerror_error_t **error )
112
517
{
113
517
  static char *function = "libvhdi_metadata_table_header_free";
114
115
517
  if( metadata_table_header == NULL )
116
0
  {
117
0
    libcerror_error_set(
118
0
     error,
119
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
120
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
121
0
     "%s: invalid metadata table header.",
122
0
     function );
123
124
0
    return( -1 );
125
0
  }
126
517
  if( *metadata_table_header != NULL )
127
517
  {
128
517
    memory_free(
129
517
     *metadata_table_header );
130
131
517
    *metadata_table_header = NULL;
132
517
  }
133
517
  return( 1 );
134
517
}
135
136
/* Reads the metadata table header data
137
 * Returns 1 if successful or -1 on error
138
 */
139
int libvhdi_metadata_table_header_read_data(
140
     libvhdi_metadata_table_header_t *metadata_table_header,
141
     const uint8_t *data,
142
     size_t data_size,
143
     libcerror_error_t **error )
144
517
{
145
517
  static char *function = "libvhdi_metadata_table_header_read_data";
146
147
#if defined( HAVE_DEBUG_OUTPUT )
148
  uint16_t value_16bit  = 0;
149
#endif
150
151
517
  if( metadata_table_header == NULL )
152
0
  {
153
0
    libcerror_error_set(
154
0
     error,
155
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
156
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
157
0
     "%s: invalid metadata table header.",
158
0
     function );
159
160
0
    return( -1 );
161
0
  }
162
517
  if( data == NULL )
163
0
  {
164
0
    libcerror_error_set(
165
0
     error,
166
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
167
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
168
0
     "%s: invalid data.",
169
0
     function );
170
171
0
    return( -1 );
172
0
  }
173
517
  if( ( data_size < sizeof( vhdi_metadata_table_header_t ) )
174
517
   || ( data_size > (size_t) SSIZE_MAX ) )
175
0
  {
176
0
    libcerror_error_set(
177
0
     error,
178
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
179
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
180
0
     "%s: invalid data size value out of bounds.",
181
0
     function );
182
183
0
    return( -1 );
184
0
  }
185
#if defined( HAVE_DEBUG_OUTPUT )
186
  if( libcnotify_verbose != 0 )
187
  {
188
    libcnotify_printf(
189
     "%s: metadata table header data:\n",
190
     function );
191
    libcnotify_print_data(
192
     data,
193
     sizeof( vhdi_metadata_table_header_t ),
194
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
195
  }
196
#endif
197
517
  if( memory_compare(
198
517
       ( (vhdi_metadata_table_header_t *) data )->signature,
199
517
       "metadata",
200
517
       8 ) != 0 )
201
14
  {
202
14
    libcerror_error_set(
203
14
     error,
204
14
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
205
14
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
206
14
     "%s: unsupported signature.",
207
14
     function );
208
209
14
    return( -1 );
210
14
  }
211
503
  byte_stream_copy_to_uint16_little_endian(
212
503
   ( (vhdi_metadata_table_header_t *) data )->number_of_entries,
213
503
   metadata_table_header->number_of_entries );
214
215
#if defined( HAVE_DEBUG_OUTPUT )
216
  if( libcnotify_verbose != 0 )
217
  {
218
    libcnotify_printf(
219
     "%s: signature\t\t\t: %c%c%c%c%c%c%c%c\n",
220
     function,
221
     ( (vhdi_metadata_table_header_t *) data )->signature[ 0 ],
222
     ( (vhdi_metadata_table_header_t *) data )->signature[ 1 ],
223
     ( (vhdi_metadata_table_header_t *) data )->signature[ 2 ],
224
     ( (vhdi_metadata_table_header_t *) data )->signature[ 3 ],
225
     ( (vhdi_metadata_table_header_t *) data )->signature[ 4 ],
226
     ( (vhdi_metadata_table_header_t *) data )->signature[ 5 ],
227
     ( (vhdi_metadata_table_header_t *) data )->signature[ 6 ],
228
     ( (vhdi_metadata_table_header_t *) data )->signature[ 7 ] );
229
230
    byte_stream_copy_to_uint16_little_endian(
231
     ( (vhdi_metadata_table_header_t *) data )->unknown1,
232
     value_16bit );
233
    libcnotify_printf(
234
     "%s: unknown1\t\t\t: 0x%04" PRIx16 "\n",
235
     function,
236
     value_16bit );
237
238
    libcnotify_printf(
239
     "%s: number of entries\t\t: %" PRIu32 "\n",
240
     function,
241
     metadata_table_header->number_of_entries );
242
243
    libcnotify_printf(
244
     "%s: unknown2:\n",
245
     function );
246
    libcnotify_print_data(
247
     ( (vhdi_metadata_table_header_t *) data )->unknown2,
248
     20,
249
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
250
  }
251
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
252
253
503
  if( metadata_table_header->number_of_entries > 2047 )
254
5
  {
255
5
    libcerror_error_set(
256
5
     error,
257
5
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
258
5
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
259
5
     "%s: unsupported number of entries: %" PRIu16 ".",
260
5
     function,
261
5
     metadata_table_header->number_of_entries );
262
263
5
    return( -1 );
264
5
  }
265
498
  return( 1 );
266
503
}
267