/src/libvhdi/libvhdi/libvhdi_region_table.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Region table 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_checksum.h" |
28 | | #include "libvhdi_libbfio.h" |
29 | | #include "libvhdi_libcdata.h" |
30 | | #include "libvhdi_libcerror.h" |
31 | | #include "libvhdi_libcnotify.h" |
32 | | #include "libvhdi_region_table.h" |
33 | | #include "libvhdi_region_table_entry.h" |
34 | | #include "libvhdi_region_table_header.h" |
35 | | |
36 | | #include "vhdi_region_table.h" |
37 | | |
38 | | /* Creates region table |
39 | | * Make sure the value region_table is referencing, is set to NULL |
40 | | * Returns 1 if successful or -1 on error |
41 | | */ |
42 | | int libvhdi_region_table_initialize( |
43 | | libvhdi_region_table_t **region_table, |
44 | | libcerror_error_t **error ) |
45 | 1.35k | { |
46 | 1.35k | static char *function = "libvhdi_region_table_initialize"; |
47 | | |
48 | 1.35k | if( region_table == NULL ) |
49 | 0 | { |
50 | 0 | libcerror_error_set( |
51 | 0 | error, |
52 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
53 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
54 | 0 | "%s: invalid region table.", |
55 | 0 | function ); |
56 | |
|
57 | 0 | return( -1 ); |
58 | 0 | } |
59 | 1.35k | if( *region_table != NULL ) |
60 | 0 | { |
61 | 0 | libcerror_error_set( |
62 | 0 | error, |
63 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
64 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
65 | 0 | "%s: invalid region table value already set.", |
66 | 0 | function ); |
67 | |
|
68 | 0 | return( -1 ); |
69 | 0 | } |
70 | 1.35k | *region_table = memory_allocate_structure( |
71 | 1.35k | libvhdi_region_table_t ); |
72 | | |
73 | 1.35k | if( *region_table == NULL ) |
74 | 0 | { |
75 | 0 | libcerror_error_set( |
76 | 0 | error, |
77 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
78 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
79 | 0 | "%s: unable to create region table.", |
80 | 0 | function ); |
81 | |
|
82 | 0 | goto on_error; |
83 | 0 | } |
84 | 1.35k | if( memory_set( |
85 | 1.35k | *region_table, |
86 | 1.35k | 0, |
87 | 1.35k | sizeof( libvhdi_region_table_t ) ) == NULL ) |
88 | 0 | { |
89 | 0 | libcerror_error_set( |
90 | 0 | error, |
91 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
92 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
93 | 0 | "%s: unable to clear region table.", |
94 | 0 | function ); |
95 | |
|
96 | 0 | memory_free( |
97 | 0 | *region_table ); |
98 | |
|
99 | 0 | *region_table = NULL; |
100 | |
|
101 | 0 | return( -1 ); |
102 | 0 | } |
103 | 1.35k | if( libcdata_array_initialize( |
104 | 1.35k | &( ( *region_table )->entries_array ), |
105 | 1.35k | 0, |
106 | 1.35k | error ) != 1 ) |
107 | 0 | { |
108 | 0 | libcerror_error_set( |
109 | 0 | error, |
110 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
111 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
112 | 0 | "%s: unable to create entries array.", |
113 | 0 | function ); |
114 | |
|
115 | 0 | goto on_error; |
116 | 0 | } |
117 | 1.35k | return( 1 ); |
118 | | |
119 | 0 | on_error: |
120 | 0 | if( *region_table != NULL ) |
121 | 0 | { |
122 | 0 | memory_free( |
123 | 0 | *region_table ); |
124 | |
|
125 | 0 | *region_table = NULL; |
126 | 0 | } |
127 | 0 | return( -1 ); |
128 | 1.35k | } |
129 | | |
130 | | /* Frees region table |
131 | | * Returns 1 if successful or -1 on error |
132 | | */ |
133 | | int libvhdi_region_table_free( |
134 | | libvhdi_region_table_t **region_table, |
135 | | libcerror_error_t **error ) |
136 | 1.35k | { |
137 | 1.35k | static char *function = "libvhdi_region_table_free"; |
138 | 1.35k | int result = 1; |
139 | | |
140 | 1.35k | if( region_table == NULL ) |
141 | 0 | { |
142 | 0 | libcerror_error_set( |
143 | 0 | error, |
144 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
145 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
146 | 0 | "%s: invalid region table.", |
147 | 0 | function ); |
148 | |
|
149 | 0 | return( -1 ); |
150 | 0 | } |
151 | 1.35k | if( *region_table != NULL ) |
152 | 1.35k | { |
153 | 1.35k | if( ( *region_table )->header != NULL ) |
154 | 1.14k | { |
155 | 1.14k | if( libvhdi_region_table_header_free( |
156 | 1.14k | &( ( *region_table )->header ), |
157 | 1.14k | error ) != 1 ) |
158 | 0 | { |
159 | 0 | libcerror_error_set( |
160 | 0 | error, |
161 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
162 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
163 | 0 | "%s: unable to free header.", |
164 | 0 | function ); |
165 | |
|
166 | 0 | result = -1; |
167 | 0 | } |
168 | 1.14k | } |
169 | 1.35k | if( libcdata_array_free( |
170 | 1.35k | &( ( *region_table )->entries_array ), |
171 | 1.35k | (int (*)(intptr_t **, libcerror_error_t **)) &libvhdi_region_table_entry_free, |
172 | 1.35k | error ) != 1 ) |
173 | 0 | { |
174 | 0 | libcerror_error_set( |
175 | 0 | error, |
176 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
177 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
178 | 0 | "%s: unable to free entries array.", |
179 | 0 | function ); |
180 | |
|
181 | 0 | result = -1; |
182 | 0 | } |
183 | 1.35k | memory_free( |
184 | 1.35k | *region_table ); |
185 | | |
186 | 1.35k | *region_table = NULL; |
187 | 1.35k | } |
188 | 1.35k | return( result ); |
189 | 1.35k | } |
190 | | |
191 | | /* Reads the region table |
192 | | * Returns 1 if successful or -1 on error |
193 | | */ |
194 | | int libvhdi_region_table_read_file_io_handle( |
195 | | libvhdi_region_table_t *region_table, |
196 | | libbfio_handle_t *file_io_handle, |
197 | | off64_t file_offset, |
198 | | libcerror_error_t **error ) |
199 | 1.35k | { |
200 | 1.35k | libvhdi_region_table_entry_t *region_table_entry = NULL; |
201 | 1.35k | uint8_t *data = NULL; |
202 | 1.35k | static char *function = "libvhdi_region_table_read_file_io_handle"; |
203 | 1.35k | size_t data_offset = 0; |
204 | 1.35k | size_t data_size = 64 * 1024; |
205 | 1.35k | ssize_t read_count = 0; |
206 | 1.35k | uint32_t calculated_checksum = 0; |
207 | 1.35k | uint32_t region_table_entry_index = 0; |
208 | 1.35k | int entry_index = 0; |
209 | | |
210 | 1.35k | if( region_table == NULL ) |
211 | 0 | { |
212 | 0 | libcerror_error_set( |
213 | 0 | error, |
214 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
215 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
216 | 0 | "%s: invalid region table.", |
217 | 0 | function ); |
218 | |
|
219 | 0 | return( -1 ); |
220 | 0 | } |
221 | 1.35k | if( region_table->header != NULL ) |
222 | 0 | { |
223 | 0 | libcerror_error_set( |
224 | 0 | error, |
225 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
226 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
227 | 0 | "%s: invalid region table - header already set.", |
228 | 0 | function ); |
229 | |
|
230 | 0 | return( -1 ); |
231 | 0 | } |
232 | | #if defined( HAVE_DEBUG_OUTPUT ) |
233 | | if( libcnotify_verbose != 0 ) |
234 | | { |
235 | | libcnotify_printf( |
236 | | "%s: reading region table at offset: %" PRIi64 " (0x%08" PRIx64 ").\n", |
237 | | function, |
238 | | file_offset, |
239 | | file_offset ); |
240 | | } |
241 | | #endif |
242 | 1.35k | data = (uint8_t *) memory_allocate( |
243 | 1.35k | sizeof( uint8_t ) * data_size ); |
244 | | |
245 | 1.35k | if( data == NULL ) |
246 | 0 | { |
247 | 0 | libcerror_error_set( |
248 | 0 | error, |
249 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
250 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
251 | 0 | "%s: unable to create region table data.", |
252 | 0 | function ); |
253 | |
|
254 | 0 | goto on_error; |
255 | 0 | } |
256 | 1.35k | read_count = libbfio_handle_read_buffer_at_offset( |
257 | 1.35k | file_io_handle, |
258 | 1.35k | data, |
259 | 1.35k | data_size, |
260 | 1.35k | file_offset, |
261 | 1.35k | error ); |
262 | | |
263 | 1.35k | if( read_count != (ssize_t) data_size ) |
264 | 139 | { |
265 | 139 | libcerror_error_set( |
266 | 139 | error, |
267 | 139 | LIBCERROR_ERROR_DOMAIN_IO, |
268 | 139 | LIBCERROR_IO_ERROR_READ_FAILED, |
269 | 139 | "%s: unable to read region table data at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
270 | 139 | function, |
271 | 139 | file_offset, |
272 | 139 | file_offset ); |
273 | | |
274 | 139 | goto on_error; |
275 | 139 | } |
276 | 1.21k | if( libvhdi_region_table_header_initialize( |
277 | 1.21k | &( region_table->header ), |
278 | 1.21k | error ) != 1 ) |
279 | 0 | { |
280 | 0 | libcerror_error_set( |
281 | 0 | error, |
282 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
283 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
284 | 0 | "%s: unable to create header.", |
285 | 0 | function ); |
286 | |
|
287 | 0 | goto on_error; |
288 | 0 | } |
289 | 1.21k | if( libvhdi_region_table_header_read_data( |
290 | 1.21k | region_table->header, |
291 | 1.21k | data, |
292 | 1.21k | sizeof( vhdi_region_table_header_t ), |
293 | 1.21k | error ) != 1 ) |
294 | 28 | { |
295 | 28 | libcerror_error_set( |
296 | 28 | error, |
297 | 28 | LIBCERROR_ERROR_DOMAIN_IO, |
298 | 28 | LIBCERROR_IO_ERROR_READ_FAILED, |
299 | 28 | "%s: unable to read region table header.", |
300 | 28 | function ); |
301 | | |
302 | 28 | goto on_error; |
303 | 28 | } |
304 | 1.18k | data_offset = sizeof( vhdi_region_table_header_t ); |
305 | | |
306 | 1.18k | byte_stream_copy_from_uint32_little_endian( |
307 | 1.18k | &( data[ 4 ] ), |
308 | 1.18k | 0 ); |
309 | | |
310 | 1.18k | if( libvhdi_checksum_calculate_crc32( |
311 | 1.18k | &calculated_checksum, |
312 | 1.18k | data, |
313 | 1.18k | data_size, |
314 | 1.18k | 0, |
315 | 1.18k | error ) != 1 ) |
316 | 0 | { |
317 | 0 | libcerror_error_set( |
318 | 0 | error, |
319 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
320 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
321 | 0 | "%s: unable to calculate CRC-32.", |
322 | 0 | function ); |
323 | |
|
324 | 0 | goto on_error; |
325 | 0 | } |
326 | 1.18k | if( region_table->header->checksum != calculated_checksum ) |
327 | 43 | { |
328 | 43 | libcerror_error_set( |
329 | 43 | error, |
330 | 43 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
331 | 43 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
332 | 43 | "%s: mismatch in checksum ( 0x%08" PRIx32 " != 0x%08" PRIx32 " ).", |
333 | 43 | function, |
334 | 43 | region_table->header->checksum, |
335 | 43 | calculated_checksum ); |
336 | | |
337 | 43 | goto on_error; |
338 | 43 | } |
339 | 1.14k | for( region_table_entry_index = 0; |
340 | 2.12M | region_table_entry_index < region_table->header->number_of_entries; |
341 | 2.12M | region_table_entry_index++ ) |
342 | 2.12M | { |
343 | 2.12M | if( libvhdi_region_table_entry_initialize( |
344 | 2.12M | ®ion_table_entry, |
345 | 2.12M | error ) != 1 ) |
346 | 0 | { |
347 | 0 | libcerror_error_set( |
348 | 0 | error, |
349 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
350 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
351 | 0 | "%s: unable to create entry.", |
352 | 0 | function ); |
353 | |
|
354 | 0 | goto on_error; |
355 | 0 | } |
356 | 2.12M | if( libvhdi_region_table_entry_read_data( |
357 | 2.12M | region_table_entry, |
358 | 2.12M | &( data[ data_offset ] ), |
359 | 2.12M | sizeof( vhdi_region_table_entry_t ), |
360 | 2.12M | error ) != 1 ) |
361 | 0 | { |
362 | 0 | libcerror_error_set( |
363 | 0 | error, |
364 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
365 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
366 | 0 | "%s: unable to read region table entry.", |
367 | 0 | function ); |
368 | |
|
369 | 0 | goto on_error; |
370 | 0 | } |
371 | 2.12M | data_offset += sizeof( vhdi_region_table_entry_t ); |
372 | | |
373 | 2.12M | if( libcdata_array_append_entry( |
374 | 2.12M | region_table->entries_array, |
375 | 2.12M | &entry_index, |
376 | 2.12M | (intptr_t *) region_table_entry, |
377 | 2.12M | error ) != 1 ) |
378 | 0 | { |
379 | 0 | libcerror_error_set( |
380 | 0 | error, |
381 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
382 | 0 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
383 | 0 | "%s: unable to append region table entry to entries array.", |
384 | 0 | function ); |
385 | |
|
386 | 0 | goto on_error; |
387 | 0 | } |
388 | 2.12M | region_table_entry = NULL; |
389 | 2.12M | } |
390 | 1.14k | memory_free( |
391 | 1.14k | data ); |
392 | | |
393 | 1.14k | return( 1 ); |
394 | | |
395 | 210 | on_error: |
396 | 210 | if( region_table->header != NULL ) |
397 | 71 | { |
398 | 71 | libvhdi_region_table_header_free( |
399 | 71 | &( region_table->header ), |
400 | 71 | NULL ); |
401 | 71 | } |
402 | 210 | if( data != NULL ) |
403 | 210 | { |
404 | 210 | memory_free( |
405 | 210 | data ); |
406 | 210 | } |
407 | 210 | return( -1 ); |
408 | 1.14k | } |
409 | | |
410 | | /* Retrieves the entry of a specific type identifier |
411 | | * Returns 1 if successful, 0 if not available or -1 on error |
412 | | */ |
413 | | int libvhdi_region_table_get_entry_by_type_identifier( |
414 | | libvhdi_region_table_t *region_table, |
415 | | const uint8_t *region_type_identifier, |
416 | | libvhdi_region_table_entry_t **entry, |
417 | | libcerror_error_t **error ) |
418 | 565 | { |
419 | 565 | libvhdi_region_table_entry_t *safe_entry = NULL; |
420 | 565 | static char *function = "libvhdi_region_table_get_entry_by_type_identifier"; |
421 | 565 | int entry_index = 0; |
422 | 565 | int number_of_entries = 0; |
423 | | |
424 | 565 | if( region_table == NULL ) |
425 | 0 | { |
426 | 0 | libcerror_error_set( |
427 | 0 | error, |
428 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
429 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
430 | 0 | "%s: invalid region table.", |
431 | 0 | function ); |
432 | |
|
433 | 0 | return( -1 ); |
434 | 0 | } |
435 | 565 | if( region_type_identifier == NULL ) |
436 | 0 | { |
437 | 0 | libcerror_error_set( |
438 | 0 | error, |
439 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
440 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
441 | 0 | "%s: invalid region type identifier.", |
442 | 0 | function ); |
443 | |
|
444 | 0 | return( -1 ); |
445 | 0 | } |
446 | 565 | if( entry == NULL ) |
447 | 0 | { |
448 | 0 | libcerror_error_set( |
449 | 0 | error, |
450 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
451 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
452 | 0 | "%s: invalid entry.", |
453 | 0 | function ); |
454 | |
|
455 | 0 | return( -1 ); |
456 | 0 | } |
457 | 565 | *entry = NULL; |
458 | | |
459 | 565 | if( libcdata_array_get_number_of_entries( |
460 | 565 | region_table->entries_array, |
461 | 565 | &number_of_entries, |
462 | 565 | error ) != 1 ) |
463 | 0 | { |
464 | 0 | libcerror_error_set( |
465 | 0 | error, |
466 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
467 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
468 | 0 | "%s: unable to retrieve number of entries from array.", |
469 | 0 | function ); |
470 | |
|
471 | 0 | return( -1 ); |
472 | 0 | } |
473 | 565 | for( entry_index = 0; |
474 | 124k | entry_index < number_of_entries; |
475 | 123k | entry_index++ ) |
476 | 124k | { |
477 | 124k | if( libcdata_array_get_entry_by_index( |
478 | 124k | region_table->entries_array, |
479 | 124k | entry_index, |
480 | 124k | (intptr_t **) &safe_entry, |
481 | 124k | error ) != 1 ) |
482 | 0 | { |
483 | 0 | libcerror_error_set( |
484 | 0 | error, |
485 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
486 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
487 | 0 | "%s: unable to retrieve entry: %d from array.", |
488 | 0 | function, |
489 | 0 | entry_index ); |
490 | |
|
491 | 0 | return( -1 ); |
492 | 0 | } |
493 | | /* TODO move compare into function */ |
494 | 124k | if( safe_entry == NULL ) |
495 | 0 | { |
496 | 0 | libcerror_error_set( |
497 | 0 | error, |
498 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
499 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
500 | 0 | "%s: missing entry: %d.", |
501 | 0 | function, |
502 | 0 | entry_index ); |
503 | |
|
504 | 0 | return( -1 ); |
505 | 0 | } |
506 | 124k | if( memory_compare( |
507 | 124k | safe_entry->type_identifier, |
508 | 124k | region_type_identifier, |
509 | 124k | 16 ) == 0 ) |
510 | 551 | { |
511 | 551 | *entry = safe_entry; |
512 | | |
513 | 551 | return( 1 ); |
514 | 551 | } |
515 | 124k | } |
516 | 14 | return( 0 ); |
517 | 565 | } |
518 | | |