/src/libqcow/libqcow/libqcow_file_header.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * File header functions |
3 | | * |
4 | | * Copyright (C) 2010-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 <byte_stream.h> |
24 | | #include <memory.h> |
25 | | #include <types.h> |
26 | | |
27 | | #include "libqcow_debug.h" |
28 | | #include "libqcow_file_header.h" |
29 | | #include "libqcow_io_handle.h" |
30 | | #include "libqcow_libcerror.h" |
31 | | #include "libqcow_libcnotify.h" |
32 | | |
33 | | #include "qcow_file_header.h" |
34 | | |
35 | | /* Creates file header |
36 | | * Make sure the value file_header is referencing, is set to NULL |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | int libqcow_file_header_initialize( |
40 | | libqcow_file_header_t **file_header, |
41 | | libcerror_error_t **error ) |
42 | 885 | { |
43 | 885 | static char *function = "libqcow_file_header_initialize"; |
44 | | |
45 | 885 | if( file_header == NULL ) |
46 | 0 | { |
47 | 0 | libcerror_error_set( |
48 | 0 | error, |
49 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
50 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
51 | 0 | "%s: invalid file header.", |
52 | 0 | function ); |
53 | |
|
54 | 0 | return( -1 ); |
55 | 0 | } |
56 | 885 | if( *file_header != NULL ) |
57 | 0 | { |
58 | 0 | libcerror_error_set( |
59 | 0 | error, |
60 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
61 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
62 | 0 | "%s: invalid file header value already set.", |
63 | 0 | function ); |
64 | |
|
65 | 0 | return( -1 ); |
66 | 0 | } |
67 | 885 | *file_header = memory_allocate_structure( |
68 | 885 | libqcow_file_header_t ); |
69 | | |
70 | 885 | if( *file_header == NULL ) |
71 | 0 | { |
72 | 0 | libcerror_error_set( |
73 | 0 | error, |
74 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
75 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
76 | 0 | "%s: unable to create file header.", |
77 | 0 | function ); |
78 | |
|
79 | 0 | goto on_error; |
80 | 0 | } |
81 | 885 | if( memory_set( |
82 | 885 | *file_header, |
83 | 885 | 0, |
84 | 885 | sizeof( libqcow_file_header_t ) ) == NULL ) |
85 | 0 | { |
86 | 0 | libcerror_error_set( |
87 | 0 | error, |
88 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
89 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
90 | 0 | "%s: unable to clear file header.", |
91 | 0 | function ); |
92 | |
|
93 | 0 | goto on_error; |
94 | 0 | } |
95 | 885 | return( 1 ); |
96 | | |
97 | 0 | on_error: |
98 | 0 | if( *file_header != NULL ) |
99 | 0 | { |
100 | 0 | memory_free( |
101 | 0 | *file_header ); |
102 | |
|
103 | 0 | *file_header = NULL; |
104 | 0 | } |
105 | 0 | return( -1 ); |
106 | 885 | } |
107 | | |
108 | | /* Frees file header |
109 | | * Returns 1 if successful or -1 on error |
110 | | */ |
111 | | int libqcow_file_header_free( |
112 | | libqcow_file_header_t **file_header, |
113 | | libcerror_error_t **error ) |
114 | 885 | { |
115 | 885 | static char *function = "libqcow_file_header_free"; |
116 | | |
117 | 885 | if( file_header == NULL ) |
118 | 0 | { |
119 | 0 | libcerror_error_set( |
120 | 0 | error, |
121 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
122 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
123 | 0 | "%s: invalid file header.", |
124 | 0 | function ); |
125 | |
|
126 | 0 | return( -1 ); |
127 | 0 | } |
128 | 885 | if( *file_header != NULL ) |
129 | 885 | { |
130 | 885 | memory_free( |
131 | 885 | *file_header ); |
132 | | |
133 | 885 | *file_header = NULL; |
134 | 885 | } |
135 | 885 | return( 1 ); |
136 | 885 | } |
137 | | |
138 | | /* Reads the file header data |
139 | | * Returns 1 if successful or -1 on error |
140 | | */ |
141 | | int libqcow_file_header_read_data( |
142 | | libqcow_file_header_t *file_header, |
143 | | const uint8_t *data, |
144 | | size_t data_size, |
145 | | libcerror_error_t **error ) |
146 | 858 | { |
147 | 858 | static char *function = "libqcow_file_header_read_data"; |
148 | 858 | size_t file_header_data_size = 0; |
149 | 858 | uint64_t compatible_feature_flags = 0; |
150 | 858 | uint64_t incompatible_feature_flags = 0; |
151 | 858 | uint64_t safe_level1_table_offset = 0; |
152 | 858 | uint64_t safe_snapshots_offset = 0; |
153 | 858 | uint64_t supported_feature_flags = 0; |
154 | 858 | uint32_t file_header_size = 0; |
155 | | |
156 | | #if defined( HAVE_DEBUG_OUTPUT ) |
157 | | uint64_t value_64bit = 0; |
158 | | uint32_t value_32bit = 0; |
159 | | uint16_t value_16bit = 0; |
160 | | #endif |
161 | | |
162 | 858 | if( file_header == 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 file header.", |
169 | 0 | function ); |
170 | |
|
171 | 0 | return( -1 ); |
172 | 0 | } |
173 | 858 | if( data == NULL ) |
174 | 0 | { |
175 | 0 | libcerror_error_set( |
176 | 0 | error, |
177 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
178 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
179 | 0 | "%s: invalid data.", |
180 | 0 | function ); |
181 | |
|
182 | 0 | return( -1 ); |
183 | 0 | } |
184 | 858 | if( ( data_size < 8 ) |
185 | 858 | || ( data_size > (size_t) SSIZE_MAX ) ) |
186 | 0 | { |
187 | 0 | libcerror_error_set( |
188 | 0 | error, |
189 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
190 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
191 | 0 | "%s: invalid data size value out of bounds.", |
192 | 0 | function ); |
193 | |
|
194 | 0 | return( -1 ); |
195 | 0 | } |
196 | 858 | if( memory_compare( |
197 | 858 | ( (qcow_file_header_v1_t *) data )->signature, |
198 | 858 | qcow_file_signature, |
199 | 858 | 4 ) != 0 ) |
200 | 37 | { |
201 | 37 | libcerror_error_set( |
202 | 37 | error, |
203 | 37 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
204 | 37 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
205 | 37 | "%s: unsupported file signature.", |
206 | 37 | function ); |
207 | | |
208 | 37 | return( -1 ); |
209 | 37 | } |
210 | 821 | byte_stream_copy_to_uint32_big_endian( |
211 | 821 | ( (qcow_file_header_v1_t *) data )->format_version, |
212 | 821 | file_header->format_version ); |
213 | | |
214 | 821 | if( file_header->format_version == 1 ) |
215 | 505 | { |
216 | 505 | file_header_data_size = sizeof( qcow_file_header_v1_t ); |
217 | 505 | } |
218 | 316 | else if( file_header->format_version == 2 ) |
219 | 73 | { |
220 | 73 | file_header_data_size = sizeof( qcow_file_header_v2_t ); |
221 | 73 | } |
222 | 243 | else if( file_header->format_version == 3 ) |
223 | 186 | { |
224 | 186 | file_header_data_size = sizeof( qcow_file_header_v3_t ); |
225 | 186 | } |
226 | 57 | else |
227 | 57 | { |
228 | 57 | libcerror_error_set( |
229 | 57 | error, |
230 | 57 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
231 | 57 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
232 | 57 | "%s: unsupported format version: %" PRIu32 ".", |
233 | 57 | function, |
234 | 57 | file_header->format_version ); |
235 | | |
236 | 57 | return( -1 ); |
237 | 57 | } |
238 | 764 | if( data_size < file_header_data_size ) |
239 | 0 | { |
240 | 0 | libcerror_error_set( |
241 | 0 | error, |
242 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
243 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
244 | 0 | "%s: invalid data size value out of bounds.", |
245 | 0 | function ); |
246 | |
|
247 | 0 | return( -1 ); |
248 | 0 | } |
249 | | #if defined( HAVE_DEBUG_OUTPUT ) |
250 | | if( libcnotify_verbose != 0 ) |
251 | | { |
252 | | libcnotify_printf( |
253 | | "%s: file header data:\n", |
254 | | function ); |
255 | | libcnotify_print_data( |
256 | | data, |
257 | | file_header_data_size, |
258 | | 0 ); |
259 | | } |
260 | | #endif |
261 | 764 | byte_stream_copy_to_uint64_big_endian( |
262 | 764 | ( (qcow_file_header_v1_t *) data )->backing_filename_offset, |
263 | 764 | file_header->backing_filename_offset ); |
264 | | |
265 | 764 | byte_stream_copy_to_uint32_big_endian( |
266 | 764 | ( (qcow_file_header_v1_t *) data )->backing_filename_size, |
267 | 764 | file_header->backing_filename_size ); |
268 | | |
269 | | #if defined( HAVE_DEBUG_OUTPUT ) |
270 | | if( libcnotify_verbose != 0 ) |
271 | | { |
272 | | byte_stream_copy_to_uint32_big_endian( |
273 | | ( (qcow_file_header_v1_t *) data )->signature, |
274 | | value_32bit ); |
275 | | libcnotify_printf( |
276 | | "%s: signature\t\t\t\t: 0x%08" PRIx32 "\n", |
277 | | function, |
278 | | value_32bit ); |
279 | | |
280 | | libcnotify_printf( |
281 | | "%s: format version\t\t\t\t: %" PRIu32 "\n", |
282 | | function, |
283 | | file_header->format_version ); |
284 | | |
285 | | libcnotify_printf( |
286 | | "%s: backing filename offset\t\t\t: %" PRIu32 "\n", |
287 | | function, |
288 | | file_header->backing_filename_offset ); |
289 | | |
290 | | libcnotify_printf( |
291 | | "%s: backing filename size\t\t\t: %" PRIu32 "\n", |
292 | | function, |
293 | | file_header->backing_filename_size ); |
294 | | } |
295 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
296 | | |
297 | 764 | if( file_header->format_version == 3 ) |
298 | 186 | { |
299 | 186 | if( data_size < sizeof( qcow_file_header_v3_t ) ) |
300 | 0 | { |
301 | 0 | libcerror_error_set( |
302 | 0 | error, |
303 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
304 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
305 | 0 | "%s: invalid data size value out of bounds.", |
306 | 0 | function ); |
307 | |
|
308 | 0 | return( -1 ); |
309 | 0 | } |
310 | 186 | } |
311 | 764 | if( file_header->format_version == 1 ) |
312 | 505 | { |
313 | 505 | byte_stream_copy_to_uint64_big_endian( |
314 | 505 | ( (qcow_file_header_v1_t *) data )->media_size, |
315 | 505 | file_header->media_size ); |
316 | | |
317 | 505 | file_header->number_of_cluster_block_bits = (uint32_t) ( (qcow_file_header_v1_t *) data )->number_of_cluster_block_bits; |
318 | | |
319 | 505 | file_header->number_of_level2_table_bits = (uint32_t) ( (qcow_file_header_v1_t *) data )->number_of_level2_table_bits; |
320 | | |
321 | 505 | byte_stream_copy_to_uint32_big_endian( |
322 | 505 | ( (qcow_file_header_v1_t *) data )->encryption_method, |
323 | 505 | file_header->encryption_method ); |
324 | | |
325 | 505 | byte_stream_copy_to_uint64_big_endian( |
326 | 505 | ( (qcow_file_header_v2_t *) data )->level1_table_offset, |
327 | 505 | safe_level1_table_offset ); |
328 | | |
329 | | #if defined( HAVE_DEBUG_OUTPUT ) |
330 | | if( libcnotify_verbose != 0 ) |
331 | | { |
332 | | byte_stream_copy_to_uint32_big_endian( |
333 | | ( (qcow_file_header_v1_t *) data )->modification_time, |
334 | | value_32bit ); |
335 | | libcnotify_printf( |
336 | | "%s: modification time\t\t\t: %" PRIu32 "\n", |
337 | | function, |
338 | | value_32bit ); |
339 | | |
340 | | libcnotify_printf( |
341 | | "%s: media size\t\t\t\t: %" PRIu64 "\n", |
342 | | function, |
343 | | file_header->media_size ); |
344 | | |
345 | | libcnotify_printf( |
346 | | "%s: number of cluster block bits\t\t: %" PRIu32 "\n", |
347 | | function, |
348 | | file_header->number_of_cluster_block_bits ); |
349 | | |
350 | | libcnotify_printf( |
351 | | "%s: number of level 2 table bits\t\t: %" PRIu32 "\n", |
352 | | function, |
353 | | file_header->number_of_level2_table_bits ); |
354 | | |
355 | | byte_stream_copy_to_uint16_big_endian( |
356 | | ( (qcow_file_header_v1_t *) data )->unknown1, |
357 | | value_16bit ); |
358 | | libcnotify_printf( |
359 | | "%s: unknown1\t\t\t\t\t: 0x%04" PRIx16 "\n", |
360 | | function, |
361 | | value_16bit ); |
362 | | |
363 | | libcnotify_printf( |
364 | | "%s: encryption method\t\t\t: %" PRIu32 "\n", |
365 | | function, |
366 | | file_header->encryption_method ); |
367 | | |
368 | | libcnotify_printf( |
369 | | "%s: level 1 table offset\t\t\t: 0x%08" PRIx64 "\n", |
370 | | function, |
371 | | safe_level1_table_offset ); |
372 | | } |
373 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
374 | 505 | } |
375 | 259 | else if( ( file_header->format_version == 2 ) |
376 | 259 | || ( file_header->format_version == 3 ) ) |
377 | 259 | { |
378 | 259 | byte_stream_copy_to_uint32_big_endian( |
379 | 259 | ( (qcow_file_header_v2_t *) data )->number_of_cluster_block_bits, |
380 | 259 | file_header->number_of_cluster_block_bits ); |
381 | | |
382 | 259 | byte_stream_copy_to_uint32_big_endian( |
383 | 259 | ( (qcow_file_header_v2_t *) data )->number_of_level1_table_references, |
384 | 259 | file_header->number_of_level1_table_references ); |
385 | | |
386 | 259 | byte_stream_copy_to_uint64_big_endian( |
387 | 259 | ( (qcow_file_header_v2_t *) data )->level1_table_offset, |
388 | 259 | safe_level1_table_offset ); |
389 | | |
390 | 259 | byte_stream_copy_to_uint64_big_endian( |
391 | 259 | ( (qcow_file_header_v2_t *) data )->media_size, |
392 | 259 | file_header->media_size ); |
393 | | |
394 | 259 | byte_stream_copy_to_uint32_big_endian( |
395 | 259 | ( (qcow_file_header_v2_t *) data )->encryption_method, |
396 | 259 | file_header->encryption_method ); |
397 | | |
398 | 259 | byte_stream_copy_to_uint32_big_endian( |
399 | 259 | ( (qcow_file_header_v2_t *) data )->number_of_snapshots, |
400 | 259 | file_header->number_of_snapshots ); |
401 | | |
402 | 259 | byte_stream_copy_to_uint64_big_endian( |
403 | 259 | ( (qcow_file_header_v2_t *) data )->snapshots_offset, |
404 | 259 | safe_snapshots_offset ); |
405 | | |
406 | | #if defined( HAVE_DEBUG_OUTPUT ) |
407 | | if( libcnotify_verbose != 0 ) |
408 | | { |
409 | | libcnotify_printf( |
410 | | "%s: number of cluster block bits\t\t: %" PRIu32 "\n", |
411 | | function, |
412 | | file_header->number_of_cluster_block_bits ); |
413 | | |
414 | | libcnotify_printf( |
415 | | "%s: media size\t\t\t\t: %" PRIu64 "\n", |
416 | | function, |
417 | | file_header->media_size ); |
418 | | |
419 | | libcnotify_printf( |
420 | | "%s: encryption method\t\t\t: %" PRIu32 "\n", |
421 | | function, |
422 | | file_header->encryption_method ); |
423 | | |
424 | | libcnotify_printf( |
425 | | "%s: number of level 1 table references\t: %" PRIu32 "\n", |
426 | | function, |
427 | | file_header->number_of_level1_table_references ); |
428 | | |
429 | | libcnotify_printf( |
430 | | "%s: level 1 table offset\t\t\t: 0x%08" PRIx64 "\n", |
431 | | function, |
432 | | safe_level1_table_offset ); |
433 | | |
434 | | byte_stream_copy_to_uint64_big_endian( |
435 | | ( (qcow_file_header_v2_t *) data )->reference_count_table_offset, |
436 | | value_64bit ); |
437 | | libcnotify_printf( |
438 | | "%s: reference count table offset\t\t: 0x%08" PRIx64 "\n", |
439 | | function, |
440 | | value_64bit ); |
441 | | |
442 | | byte_stream_copy_to_uint32_big_endian( |
443 | | ( (qcow_file_header_v2_t *) data )->reference_count_table_clusters, |
444 | | value_32bit ); |
445 | | libcnotify_printf( |
446 | | "%s: reference count table clusters\t\t: %" PRIu32 "\n", |
447 | | function, |
448 | | value_32bit ); |
449 | | |
450 | | libcnotify_printf( |
451 | | "%s: number of snapshots\t\t\t: %" PRIu32 "\n", |
452 | | function, |
453 | | file_header->number_of_snapshots ); |
454 | | |
455 | | libcnotify_printf( |
456 | | "%s: snapshots offset\t\t\t\t: 0x%08" PRIx64 "\n", |
457 | | function, |
458 | | safe_snapshots_offset ); |
459 | | } |
460 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
461 | 259 | } |
462 | 764 | if( safe_level1_table_offset > (uint64_t) INT64_MAX ) |
463 | 64 | { |
464 | 64 | libcerror_error_set( |
465 | 64 | error, |
466 | 64 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
467 | 64 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
468 | 64 | "%s: invalid level1 table offset value out of bounds.", |
469 | 64 | function ); |
470 | | |
471 | 64 | return( -1 ); |
472 | 64 | } |
473 | 700 | if( file_header->format_version == 3 ) |
474 | 166 | { |
475 | 166 | byte_stream_copy_to_uint64_big_endian( |
476 | 166 | ( (qcow_file_header_v3_t *) data )->incompatible_feature_flags, |
477 | 166 | incompatible_feature_flags ); |
478 | | |
479 | 166 | byte_stream_copy_to_uint64_big_endian( |
480 | 166 | ( (qcow_file_header_v3_t *) data )->compatible_feature_flags, |
481 | 166 | compatible_feature_flags ); |
482 | | |
483 | 166 | byte_stream_copy_to_uint32_big_endian( |
484 | 166 | ( (qcow_file_header_v3_t *) data )->header_size, |
485 | 166 | file_header_size ); |
486 | | |
487 | | #if defined( HAVE_DEBUG_OUTPUT ) |
488 | | if( libcnotify_verbose != 0 ) |
489 | | { |
490 | | libcnotify_printf( |
491 | | "%s: incompatible feature flags\t\t: 0x%08" PRIx64 "\n", |
492 | | function, |
493 | | incompatible_feature_flags ); |
494 | | |
495 | | libcnotify_printf( |
496 | | "%s: compatible feature flags\t\t\t: 0x%08" PRIx64 "\n", |
497 | | function, |
498 | | compatible_feature_flags ); |
499 | | |
500 | | byte_stream_copy_to_uint64_big_endian( |
501 | | ( (qcow_file_header_v3_t *) data )->auto_clear_feature_flags, |
502 | | value_64bit ); |
503 | | libcnotify_printf( |
504 | | "%s: auto-clear feature flags\t\t\t: 0x%08" PRIx64 "\n", |
505 | | function, |
506 | | value_64bit ); |
507 | | |
508 | | byte_stream_copy_to_uint32_big_endian( |
509 | | ( (qcow_file_header_v3_t *) data )->reference_count_order, |
510 | | value_32bit ); |
511 | | libcnotify_printf( |
512 | | "%s: reference count order\t\t\t: %" PRIu32 "\n", |
513 | | function, |
514 | | value_32bit ); |
515 | | |
516 | | libcnotify_printf( |
517 | | "%s: file header size\t\t\t\t: %" PRIu32 "\n", |
518 | | function, |
519 | | file_header_size ); |
520 | | |
521 | | if( file_header_size >= 104 ) |
522 | | { |
523 | | libcnotify_printf( |
524 | | "%s: compression type\t\t\t\t: %" PRIu8 "\n", |
525 | | function, |
526 | | ( (qcow_file_header_v3_t *) data )->compression_type ); |
527 | | |
528 | | libcnotify_printf( |
529 | | "%s: unknown1:\n", |
530 | | function ); |
531 | | libcnotify_print_data( |
532 | | ( (qcow_file_header_v3_t *) data )->unknown1, |
533 | | 7, |
534 | | 0 ); |
535 | | } |
536 | | } |
537 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
538 | | |
539 | 166 | supported_feature_flags = 0x00000001UL; |
540 | | |
541 | 166 | if( ( incompatible_feature_flags & ~( supported_feature_flags ) ) != 0 ) |
542 | 95 | { |
543 | 95 | libcerror_error_set( |
544 | 95 | error, |
545 | 95 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
546 | 95 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
547 | 95 | "%s: unsupported incompatible features flags: 0x%08" PRIx64 ".", |
548 | 95 | function, |
549 | 95 | incompatible_feature_flags ); |
550 | | |
551 | 95 | return( -1 ); |
552 | 95 | } |
553 | 71 | supported_feature_flags = 0x00000001UL; |
554 | | |
555 | 71 | if( ( compatible_feature_flags & ~( supported_feature_flags ) ) != 0 ) |
556 | 14 | { |
557 | 14 | libcerror_error_set( |
558 | 14 | error, |
559 | 14 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
560 | 14 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
561 | 14 | "%s: unsupported compatible features flags: 0x%08" PRIx64 ".", |
562 | 14 | function, |
563 | 14 | compatible_feature_flags ); |
564 | | |
565 | 14 | return( -1 ); |
566 | 14 | } |
567 | 57 | if( ( file_header_size != 104 ) |
568 | 57 | && ( file_header_size != 112 ) ) |
569 | 46 | { |
570 | 46 | libcerror_error_set( |
571 | 46 | error, |
572 | 46 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
573 | 46 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
574 | 46 | "%s: unsupported file header size: %d.", |
575 | 46 | function, |
576 | 46 | file_header_size ); |
577 | | |
578 | 46 | return( -1 ); |
579 | 46 | } |
580 | 57 | } |
581 | | #if defined( HAVE_DEBUG_OUTPUT ) |
582 | | if( libcnotify_verbose != 0 ) |
583 | | { |
584 | | if( ( file_header->format_version != 3 ) |
585 | | || ( file_header_size <= 104 ) ) |
586 | | { |
587 | | libcnotify_printf( |
588 | | "\n" ); |
589 | | } |
590 | | } |
591 | | #endif |
592 | 545 | file_header->level1_table_offset = (off64_t) safe_level1_table_offset; |
593 | 545 | file_header->snapshots_offset = (off64_t) safe_snapshots_offset; |
594 | | |
595 | 545 | return( 1 ); |
596 | 700 | } |
597 | | |
598 | | /* Reads the file header |
599 | | * Returns 1 if successful or -1 on error |
600 | | */ |
601 | | int libqcow_file_header_read_file_io_handle( |
602 | | libqcow_file_header_t *file_header, |
603 | | libbfio_handle_t *file_io_handle, |
604 | | libcerror_error_t **error ) |
605 | 885 | { |
606 | 885 | uint8_t file_header_data[ 512 ]; |
607 | | |
608 | 885 | static char *function = "libqcow_file_header_read_file_io_handle"; |
609 | 885 | ssize_t read_count = 0; |
610 | | |
611 | 885 | if( file_header == NULL ) |
612 | 0 | { |
613 | 0 | libcerror_error_set( |
614 | 0 | error, |
615 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
616 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
617 | 0 | "%s: invalid file header.", |
618 | 0 | function ); |
619 | |
|
620 | 0 | return( -1 ); |
621 | 0 | } |
622 | | #if defined( HAVE_DEBUG_OUTPUT ) |
623 | | if( libcnotify_verbose != 0 ) |
624 | | { |
625 | | libcnotify_printf( |
626 | | "%s: reading file header at offset: 0 (0x00000000)\n", |
627 | | function ); |
628 | | } |
629 | | #endif |
630 | 885 | read_count = libbfio_handle_read_buffer_at_offset( |
631 | 885 | file_io_handle, |
632 | 885 | file_header_data, |
633 | 885 | 512, |
634 | 885 | 0, |
635 | 885 | error ); |
636 | | |
637 | 885 | if( read_count != (ssize_t) 512 ) |
638 | 27 | { |
639 | 27 | libcerror_error_set( |
640 | 27 | error, |
641 | 27 | LIBCERROR_ERROR_DOMAIN_IO, |
642 | 27 | LIBCERROR_IO_ERROR_READ_FAILED, |
643 | 27 | "%s: unable to read file header data at offset: 0 (0x00000000).", |
644 | 27 | function ); |
645 | | |
646 | 27 | return( -1 ); |
647 | 27 | } |
648 | 858 | if( libqcow_file_header_read_data( |
649 | 858 | file_header, |
650 | 858 | file_header_data, |
651 | 858 | 512, |
652 | 858 | error ) != 1 ) |
653 | 313 | { |
654 | 313 | libcerror_error_set( |
655 | 313 | error, |
656 | 313 | LIBCERROR_ERROR_DOMAIN_IO, |
657 | 313 | LIBCERROR_IO_ERROR_READ_FAILED, |
658 | 313 | "%s: unable to read file header.", |
659 | 313 | function ); |
660 | | |
661 | 313 | return( -1 ); |
662 | 313 | } |
663 | 545 | return( 1 ); |
664 | 858 | } |
665 | | |