/src/libvhdi/libvhdi/libvhdi_metadata_table.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Metadata table functions |
3 | | * |
4 | | * Copyright (C) 2012-2023, 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 | 585 | { |
44 | 585 | static char *function = "libvhdi_metadata_table_initialize"; |
45 | | |
46 | 585 | 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 | 585 | 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 | 585 | *metadata_table = memory_allocate_structure( |
69 | 585 | libvhdi_metadata_table_t ); |
70 | | |
71 | 585 | 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 | 585 | if( memory_set( |
83 | 585 | *metadata_table, |
84 | 585 | 0, |
85 | 585 | 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 | 585 | if( libcdata_array_initialize( |
102 | 585 | &( ( *metadata_table )->entries_array ), |
103 | 585 | 0, |
104 | 585 | 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 | 585 | 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 | 585 | } |
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 | 585 | { |
135 | 585 | static char *function = "libvhdi_metadata_table_free"; |
136 | 585 | int result = 1; |
137 | | |
138 | 585 | 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 | 585 | if( *metadata_table != NULL ) |
150 | 585 | { |
151 | 585 | if( ( *metadata_table )->header != NULL ) |
152 | 538 | { |
153 | 538 | if( libvhdi_metadata_table_header_free( |
154 | 538 | &( ( *metadata_table )->header ), |
155 | 538 | 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 | 538 | } |
167 | 585 | if( libcdata_array_free( |
168 | 585 | &( ( *metadata_table )->entries_array ), |
169 | 585 | (int (*)(intptr_t **, libcerror_error_t **)) &libvhdi_metadata_table_entry_free, |
170 | 585 | 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 | 585 | memory_free( |
182 | 585 | *metadata_table ); |
183 | | |
184 | 585 | *metadata_table = NULL; |
185 | 585 | } |
186 | 585 | return( result ); |
187 | 585 | } |
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 | 585 | { |
198 | 585 | libvhdi_metadata_table_entry_t *metadata_table_entry = NULL; |
199 | 585 | uint8_t *data = NULL; |
200 | 585 | static char *function = "libvhdi_metadata_table_read_file_io_handle"; |
201 | 585 | size_t data_offset = 0; |
202 | 585 | size_t data_size = 64 * 1024; |
203 | 585 | ssize_t read_count = 0; |
204 | 585 | uint32_t metadata_table_entry_index = 0; |
205 | 585 | int entry_index = 0; |
206 | | |
207 | 585 | 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 | 585 | 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 | 585 | data = (uint8_t *) memory_allocate( |
240 | 585 | sizeof( uint8_t ) * data_size ); |
241 | | |
242 | 585 | 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 | 585 | read_count = libbfio_handle_read_buffer_at_offset( |
254 | 585 | file_io_handle, |
255 | 585 | data, |
256 | 585 | data_size, |
257 | 585 | file_offset, |
258 | 585 | error ); |
259 | | |
260 | 585 | if( read_count != (ssize_t) data_size ) |
261 | 4 | { |
262 | 4 | libcerror_error_set( |
263 | 4 | error, |
264 | 4 | LIBCERROR_ERROR_DOMAIN_IO, |
265 | 4 | LIBCERROR_IO_ERROR_READ_FAILED, |
266 | 4 | "%s: unable to read metadata table data at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
267 | 4 | function, |
268 | 4 | file_offset, |
269 | 4 | file_offset ); |
270 | | |
271 | 4 | goto on_error; |
272 | 4 | } |
273 | 581 | if( libvhdi_metadata_table_header_initialize( |
274 | 581 | &( metadata_table->header ), |
275 | 581 | 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 | 581 | if( libvhdi_metadata_table_header_read_data( |
287 | 581 | metadata_table->header, |
288 | 581 | data, |
289 | 581 | sizeof( vhdi_metadata_table_header_t ), |
290 | 581 | error ) != 1 ) |
291 | 18 | { |
292 | 18 | libcerror_error_set( |
293 | 18 | error, |
294 | 18 | LIBCERROR_ERROR_DOMAIN_IO, |
295 | 18 | LIBCERROR_IO_ERROR_READ_FAILED, |
296 | 18 | "%s: unable to read metadata table header.", |
297 | 18 | function ); |
298 | | |
299 | 18 | goto on_error; |
300 | 18 | } |
301 | 563 | data_offset = sizeof( vhdi_metadata_table_header_t ); |
302 | | |
303 | 563 | for( metadata_table_entry_index = 0; |
304 | 10.6k | metadata_table_entry_index < metadata_table->header->number_of_entries; |
305 | 10.1k | metadata_table_entry_index++ ) |
306 | 10.1k | { |
307 | 10.1k | if( libvhdi_metadata_table_entry_initialize( |
308 | 10.1k | &metadata_table_entry, |
309 | 10.1k | 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 | 10.1k | if( libvhdi_metadata_table_entry_read_data( |
321 | 10.1k | metadata_table_entry, |
322 | 10.1k | &( data[ data_offset ] ), |
323 | 10.1k | sizeof( vhdi_metadata_table_entry_t ), |
324 | 10.1k | error ) != 1 ) |
325 | 25 | { |
326 | 25 | libcerror_error_set( |
327 | 25 | error, |
328 | 25 | LIBCERROR_ERROR_DOMAIN_IO, |
329 | 25 | LIBCERROR_IO_ERROR_READ_FAILED, |
330 | 25 | "%s: unable to read metadata table entry.", |
331 | 25 | function ); |
332 | | |
333 | 25 | goto on_error; |
334 | 25 | } |
335 | 10.1k | data_offset += sizeof( vhdi_metadata_table_entry_t ); |
336 | | |
337 | 10.1k | if( libcdata_array_append_entry( |
338 | 10.1k | metadata_table->entries_array, |
339 | 10.1k | &entry_index, |
340 | 10.1k | (intptr_t *) metadata_table_entry, |
341 | 10.1k | 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 | 10.1k | metadata_table_entry = NULL; |
353 | 10.1k | } |
354 | 538 | memory_free( |
355 | 538 | data ); |
356 | | |
357 | 538 | return( 1 ); |
358 | | |
359 | 47 | on_error: |
360 | 47 | if( metadata_table_entry != NULL ) |
361 | 25 | { |
362 | 25 | libvhdi_metadata_table_entry_free( |
363 | 25 | &metadata_table_entry, |
364 | 25 | NULL ); |
365 | 25 | } |
366 | 47 | if( metadata_table->header != NULL ) |
367 | 43 | { |
368 | 43 | libvhdi_metadata_table_header_free( |
369 | 43 | &( metadata_table->header ), |
370 | 43 | NULL ); |
371 | 43 | } |
372 | 47 | if( data != NULL ) |
373 | 47 | { |
374 | 47 | memory_free( |
375 | 47 | data ); |
376 | 47 | } |
377 | 47 | libcdata_array_empty( |
378 | 47 | metadata_table->entries_array, |
379 | 47 | (int (*)(intptr_t **, libcerror_error_t **)) &libvhdi_metadata_table_entry_free, |
380 | 47 | NULL ); |
381 | | |
382 | 47 | return( -1 ); |
383 | 563 | } |
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 | 538 | { |
393 | 538 | static char *function = "libvhdi_metadata_table_get_number_of_entries"; |
394 | | |
395 | 538 | 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 | 538 | if( libcdata_array_get_number_of_entries( |
407 | 538 | metadata_table->entries_array, |
408 | 538 | number_of_entries, |
409 | 538 | 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 | 538 | return( 1 ); |
421 | 538 | } |
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.32k | { |
432 | 1.32k | static char *function = "libvhdi_metadata_table_get_entry_by_index"; |
433 | | |
434 | 1.32k | 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.32k | if( libcdata_array_get_entry_by_index( |
446 | 1.32k | metadata_table->entries_array, |
447 | 1.32k | entry_index, |
448 | 1.32k | (intptr_t **) entry, |
449 | 1.32k | 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.32k | return( 1 ); |
462 | 1.32k | } |
463 | | |