Debian Codegraph @

vhditools/info_handle.c

raw symbols SCIP
1 /*
2 * Info handle
3 *
4 * Copyright (C) 2012-2025, Joachim Metz <[email protected]>
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 <narrow_string.h>
26 #include <system_string.h>
27 #include <types.h>
28 #include <wide_string.h>
29
30 #include "byte_size_string.h"
31 #include "info_handle.h"
32 #include "vhditools_libcerror.h"
33 #include "vhditools_libcnotify.h"
34 #include "vhditools_libfguid.h"
35 #include "vhditools_libvhdi.h"
36
37 #define INFO_HANDLE_NOTIFY_STREAM stdout
38
39 /* Creates an info handle
40 * Make sure the value info_handle is referencing, is set to NULL
41 * Returns 1 if successful or -1 on error
42 */
43 int info_handle_initialize(
44 info_handle_t **info_handle,
45 libcerror_error_t **error )
46 {
47 static char *function = "info_handle_initialize";
48
49 if( info_handle == NULL )
50 {
51 libcerror_error_set(
52 error,
53 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
54 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
55 "%s: invalid info handle.",
56 function );
57
58 return( -1 );
59 }
60 if( *info_handle != NULL )
61 {
62 libcerror_error_set(
63 error,
64 LIBCERROR_ERROR_DOMAIN_RUNTIME,
65 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
66 "%s: invalid info handle value already set.",
67 function );
68
69 return( -1 );
70 }
71 *info_handle = memory_allocate_structure(
72 info_handle_t );
73
74 if( *info_handle == NULL )
75 {
76 libcerror_error_set(
77 error,
78 LIBCERROR_ERROR_DOMAIN_MEMORY,
79 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
80 "%s: unable to create info handle.",
81 function );
82
83 goto on_error;
84 }
85 if( memory_set(
86 *info_handle,
87 0,
88 sizeof( info_handle_t ) ) == NULL )
89 {
90 libcerror_error_set(
91 error,
92 LIBCERROR_ERROR_DOMAIN_MEMORY,
93 LIBCERROR_MEMORY_ERROR_SET_FAILED,
94 "%s: unable to clear info handle.",
95 function );
96
97 memory_free(
98 *info_handle );
99
100 *info_handle = NULL;
101
102 return( -1 );
103 }
104 if( libvhdi_file_initialize(
105 &( ( *info_handle )->input ),
106 error ) != 1 )
107 {
108 libcerror_error_set(
109 error,
110 LIBCERROR_ERROR_DOMAIN_RUNTIME,
111 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
112 "%s: unable to initialize input.",
113 function );
114
115 goto on_error;
116 }
117 ( *info_handle )->notify_stream = INFO_HANDLE_NOTIFY_STREAM;
118
119 return( 1 );
120
121 on_error:
122 if( *info_handle != NULL )
123 {
124 memory_free(
125 *info_handle );
126
127 *info_handle = NULL;
128 }
129 return( -1 );
130 }
131
132 /* Frees an info handle
133 * Returns 1 if successful or -1 on error
134 */
135 int info_handle_free(
136 info_handle_t **info_handle,
137 libcerror_error_t **error )
138 {
139 static char *function = "info_handle_free";
140 int result = 1;
141
142 if( info_handle == NULL )
143 {
144 libcerror_error_set(
145 error,
146 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
147 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
148 "%s: invalid info handle.",
149 function );
150
151 return( -1 );
152 }
153 if( *info_handle != NULL )
154 {
155 if( ( *info_handle )->input != NULL )
156 {
157 if( libvhdi_file_free(
158 &( ( *info_handle )->input ),
159 error ) != 1 )
160 {
161 libcerror_error_set(
162 error,
163 LIBCERROR_ERROR_DOMAIN_RUNTIME,
164 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
165 "%s: unable to free input.",
166 function );
167
168 result = -1;
169 }
170 }
171 memory_free(
172 *info_handle );
173
174 *info_handle = NULL;
175 }
176 return( result );
177 }
178
179 /* Signals the info handle to abort
180 * Returns 1 if successful or -1 on error
181 */
182 int info_handle_signal_abort(
183 info_handle_t *info_handle,
184 libcerror_error_t **error )
185 {
186 static char *function = "info_handle_signal_abort";
187
188 if( info_handle == NULL )
189 {
190 libcerror_error_set(
191 error,
192 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
193 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
194 "%s: invalid info handle.",
195 function );
196
197 return( -1 );
198 }
199 if( info_handle->input != NULL )
200 {
201 if( libvhdi_file_signal_abort(
202 info_handle->input,
203 error ) != 1 )
204 {
205 libcerror_error_set(
206 error,
207 LIBCERROR_ERROR_DOMAIN_RUNTIME,
208 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
209 "%s: unable to signal input to abort.",
210 function );
211
212 return( -1 );
213 }
214 }
215 return( 1 );
216 }
217
218 /* Opens the info handle
219 * Returns 1 if successful or -1 on error
220 */
221 int info_handle_open_input(
222 info_handle_t *info_handle,
223 const system_character_t *filename,
224 libcerror_error_t **error )
225 {
226 static char *function = "info_handle_open_input";
227
228 if( info_handle == NULL )
229 {
230 libcerror_error_set(
231 error,
232 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
233 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
234 "%s: invalid info handle.",
235 function );
236
237 return( -1 );
238 }
239 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
240 if( libvhdi_file_open_wide(
241 info_handle->input,
242 filename,
243 LIBVHDI_OPEN_READ,
244 error ) != 1 )
245 #else
246 if( libvhdi_file_open(
247 info_handle->input,
248 filename,
249 LIBVHDI_OPEN_READ,
250 error ) != 1 )
251 #endif
252 {
253 libcerror_error_set(
254 error,
255 LIBCERROR_ERROR_DOMAIN_IO,
256 LIBCERROR_IO_ERROR_OPEN_FAILED,
257 "%s: unable to open input.",
258 function );
259
260 return( -1 );
261 }
262 return( 1 );
263 }
264
265 /* Closes the info handle
266 * Returns the 0 if succesful or -1 on error
267 */
268 int info_handle_close(
269 info_handle_t *info_handle,
270 libcerror_error_t **error )
271 {
272 static char *function = "info_handle_close";
273
274 if( info_handle == NULL )
275 {
276 libcerror_error_set(
277 error,
278 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
279 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
280 "%s: invalid info handle.",
281 function );
282
283 return( -1 );
284 }
285 if( info_handle->input == NULL )
286 {
287 libcerror_error_set(
288 error,
289 LIBCERROR_ERROR_DOMAIN_RUNTIME,
290 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
291 "%s: invalid info handle - missing input.",
292 function );
293
294 return( -1 );
295 }
296 if( libvhdi_file_close(
297 info_handle->input,
298 error ) != 0 )
299 {
300 libcerror_error_set(
301 error,
302 LIBCERROR_ERROR_DOMAIN_IO,
303 LIBCERROR_IO_ERROR_CLOSE_FAILED,
304 "%s: unable to close input.",
305 function );
306
307 return( -1 );
308 }
309 return( 0 );
310 }
311
312 /* Prints the input information to a stream
313 * Returns 1 if successful or -1 on error
314 */
315 int info_handle_input_fprint(
316 info_handle_t *info_handle,
317 libcerror_error_t **error )
318 {
319 system_character_t byte_size_string[ 16 ];
320 system_character_t guid_string[ 48 ];
321 uint8_t guid_data[ 16 ];
322
323 libfguid_identifier_t *guid = NULL;
324 system_character_t *disk_type_string = NULL;
325 system_character_t *value_string = NULL;
326 static char *function = "info_handle_input_fprint";
327 size64_t media_size = 0;
328 size_t value_string_size = 0;
329 uint32_t value_32bit = 0;
330 uint16_t major_version = 0;
331 uint16_t minor_version = 0;
332 int file_type = 0;
333 int result = 0;
334
335 if( info_handle == NULL )
336 {
337 libcerror_error_set(
338 error,
339 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
340 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
341 "%s: invalid info handle.",
342 function );
343
344 return( -1 );
345 }
346 if( libvhdi_file_get_file_type(
347 info_handle->input,
348 &file_type,
349 error ) != 1 )
350 {
351 libcerror_error_set(
352 error,
353 LIBCERROR_ERROR_DOMAIN_RUNTIME,
354 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
355 "%s: unable to retrieve file type.",
356 function );
357
358 goto on_error;
359 }
360 fprintf(
361 info_handle->notify_stream,
362 "Virtual Hard Disk image information:\n" );
363
364 fprintf(
365 info_handle->notify_stream,
366 "\tFormat\t\t\t:" );
367
368 switch( file_type )
369 {
370 case LIBVHDI_FILE_TYPE_VHD:
371 fprintf(
372 info_handle->notify_stream,
373 " VHD (version 1)" );
374 break;
375
376 case LIBVHDI_FILE_TYPE_VHDX:
377 fprintf(
378 info_handle->notify_stream,
379 " VHDX (version 2)" );
380 break;
381
382 default:
383 fprintf(
384 info_handle->notify_stream,
385 " Unknown" );
386 break;
387 }
388 fprintf(
389 info_handle->notify_stream,
390 "\n" );
391
392 if( libvhdi_file_get_format_version(
393 info_handle->input,
394 &major_version,
395 &minor_version,
396 error ) != 1 )
397 {
398 libcerror_error_set(
399 error,
400 LIBCERROR_ERROR_DOMAIN_RUNTIME,
401 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
402 "%s: unable to retrieve format version.",
403 function );
404
405 goto on_error;
406 }
407 fprintf(
408 info_handle->notify_stream,
409 "\tFormat version\t\t:" );
410
411 if( file_type == LIBVHDI_FILE_TYPE_VHD )
412 {
413 fprintf(
414 info_handle->notify_stream,
415 " %" PRIu16 ".%" PRIu16 "",
416 major_version,
417 minor_version );
418 }
419 else if( file_type == LIBVHDI_FILE_TYPE_VHDX )
420 {
421 fprintf(
422 info_handle->notify_stream,
423 " %" PRIu16 "",
424 major_version );
425 }
426 fprintf(
427 info_handle->notify_stream,
428 "\n" );
429
430 if( libvhdi_file_get_disk_type(
431 info_handle->input,
432 &value_32bit,
433 error ) != 1 )
434 {
435 libcerror_error_set(
436 error,
437 LIBCERROR_ERROR_DOMAIN_RUNTIME,
438 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
439 "%s: unable to retrieve disk type.",
440 function );
441
442 goto on_error;
443 }
444 switch( value_32bit )
445 {
446 case LIBVHDI_DISK_TYPE_FIXED:
447 disk_type_string = "Fixed";
448 break;
449
450 case LIBVHDI_DISK_TYPE_DYNAMIC:
451 disk_type_string = "Dynamic";
452 break;
453
454 case LIBVHDI_DISK_TYPE_DIFFERENTIAL:
455 disk_type_string = "Differential";
456 break;
457
458 default:
459 disk_type_string = "Unknown";
460 break;
461 }
462 fprintf(
463 info_handle->notify_stream,
464 "\tDisk type\t\t: %" PRIs_SYSTEM "\n",
465 disk_type_string );
466
467 if( libvhdi_file_get_media_size(
468 info_handle->input,
469 &media_size,
470 error ) != 1 )
471 {
472 libcerror_error_set(
473 error,
474 LIBCERROR_ERROR_DOMAIN_RUNTIME,
475 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
476 "%s: unable to retrieve media size.",
477 function );
478
479 goto on_error;
480 }
481 fprintf(
482 info_handle->notify_stream,
483 "\tMedia size\t\t:" );
484
485 result = byte_size_string_create(
486 byte_size_string,
487 16,
488 media_size,
489 BYTE_SIZE_STRING_UNIT_MEBIBYTE,
490 NULL );
491
492 if( result == 1 )
493 {
494 fprintf(
495 info_handle->notify_stream,
496 " %" PRIs_SYSTEM " (%" PRIu64 " bytes)",
497 byte_size_string,
498 media_size );
499 }
500 else
501 {
502 fprintf(
503 info_handle->notify_stream,
504 " %" PRIu64 " bytes",
505 media_size );
506 }
507 fprintf(
508 info_handle->notify_stream,
509 "\n" );
510
511 if( libvhdi_file_get_bytes_per_sector(
512 info_handle->input,
513 &value_32bit,
514 error ) != 1 )
515 {
516 libcerror_error_set(
517 error,
518 LIBCERROR_ERROR_DOMAIN_RUNTIME,
519 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
520 "%s: unable to retrieve bytes per sector.",
521 function );
522
523 goto on_error;
524 }
525 fprintf(
526 info_handle->notify_stream,
527 "\tBytes per sector\t: %" PRIu32 " bytes\n",
528 value_32bit );
529
530 if( libfguid_identifier_initialize(
531 &guid,
532 error ) != 1 )
533 {
534 libcerror_error_set(
535 error,
536 LIBCERROR_ERROR_DOMAIN_RUNTIME,
537 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
538 "%s: unable to create GUID.",
539 function );
540
541 goto on_error;
542 }
543 if( libvhdi_file_get_identifier(
544 info_handle->input,
545 guid_data,
546 16,
547 error ) != 1 )
548 {
549 libcerror_error_set(
550 error,
551 LIBCERROR_ERROR_DOMAIN_RUNTIME,
552 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
553 "%s: unable to retrieve identifier.",
554 function );
555
556 goto on_error;
557 }
558 if( libfguid_identifier_copy_from_byte_stream(
559 guid,
560 guid_data,
561 16,
562 LIBFGUID_ENDIAN_BIG,
563 error ) != 1 )
564 {
565 libcerror_error_set(
566 error,
567 LIBCERROR_ERROR_DOMAIN_RUNTIME,
568 LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
569 "%s: unable to copy byte stream to GUID.",
570 function );
571
572 goto on_error;
573 }
574 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
575 result = libfguid_identifier_copy_to_utf16_string(
576 guid,
577 (uint16_t *) guid_string,
578 48,
579 LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
580 error );
581 #else
582 result = libfguid_identifier_copy_to_utf8_string(
583 guid,
584 (uint8_t *) guid_string,
585 48,
586 LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
587 error );
588 #endif
589 if( result != 1 )
590 {
591 libcerror_error_set(
592 error,
593 LIBCERROR_ERROR_DOMAIN_RUNTIME,
594 LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
595 "%s: unable to copy GUID to string.",
596 function );
597
598 goto on_error;
599 }
600 fprintf(
601 info_handle->notify_stream,
602 "\tIdentifier\t\t: %" PRIs_SYSTEM "\n",
603 guid_string );
604
605 result = libvhdi_file_get_parent_identifier(
606 info_handle->input,
607 guid_data,
608 16,
609 error );
610
611 if( result == -1 )
612 {
613 libcerror_error_set(
614 error,
615 LIBCERROR_ERROR_DOMAIN_RUNTIME,
616 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
617 "%s: unable to retrieve parent identifier.",
618 function );
619
620 goto on_error;
621 }
622 else if( result != 0 )
623 {
624 if( libfguid_identifier_copy_from_byte_stream(
625 guid,
626 guid_data,
627 16,
628 LIBFGUID_ENDIAN_BIG,
629 error ) != 1 )
630 {
631 libcerror_error_set(
632 error,
633 LIBCERROR_ERROR_DOMAIN_RUNTIME,
634 LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
635 "%s: unable to copy byte stream to GUID.",
636 function );
637
638 goto on_error;
639 }
640 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
641 result = libfguid_identifier_copy_to_utf16_string(
642 guid,
643 (uint16_t *) guid_string,
644 48,
645 LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
646 error );
647 #else
648 result = libfguid_identifier_copy_to_utf8_string(
649 guid,
650 (uint8_t *) guid_string,
651 48,
652 LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
653 error );
654 #endif
655 if( result != 1 )
656 {
657 libcerror_error_set(
658 error,
659 LIBCERROR_ERROR_DOMAIN_RUNTIME,
660 LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
661 "%s: unable to copy GUID to string.",
662 function );
663
664 goto on_error;
665 }
666 fprintf(
667 info_handle->notify_stream,
668 "\tParent identifier\t: %" PRIs_SYSTEM "\n",
669 guid_string );
670 }
671 if( libfguid_identifier_free(
672 &guid,
673 error ) != 1 )
674 {
675 libcerror_error_set(
676 error,
677 LIBCERROR_ERROR_DOMAIN_RUNTIME,
678 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
679 "%s: unable to free GUID.",
680 function );
681
682 goto on_error;
683 }
684 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
685 result = libvhdi_file_get_utf16_parent_filename_size(
686 info_handle->input,
687 &value_string_size,
688 error );
689 #else
690 result = libvhdi_file_get_utf8_parent_filename_size(
691 info_handle->input,
692 &value_string_size,
693 error );
694 #endif
695 if( result == -1 )
696 {
697 libcerror_error_set(
698 error,
699 LIBCERROR_ERROR_DOMAIN_RUNTIME,
700 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
701 "%s: unable to retrieve parent filename string size.",
702 function );
703
704 goto on_error;
705 }
706 else if( result != 0 )
707 {
708 if( value_string_size > (size_t) ( SSIZE_MAX / sizeof( system_character_t ) ) )
709 {
710 libcerror_error_set(
711 error,
712 LIBCERROR_ERROR_DOMAIN_RUNTIME,
713 LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
714 "%s: invalid parent filename size value exceeds maximum.",
715 function );
716
717 goto on_error;
718 }
719 value_string = system_string_allocate(
720 value_string_size );
721
722 if( value_string == NULL )
723 {
724 libcerror_error_set(
725 error,
726 LIBCERROR_ERROR_DOMAIN_MEMORY,
727 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
728 "%s: unable to create parent filename string.",
729 function );
730
731 goto on_error;
732 }
733 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
734 result = libvhdi_file_get_utf16_parent_filename(
735 info_handle->input,
736 (uint16_t *) value_string,
737 value_string_size,
738 error );
739 #else
740 result = libvhdi_file_get_utf8_parent_filename(
741 info_handle->input,
742 (uint8_t *) value_string,
743 value_string_size,
744 error );
745 #endif
746 if( result != 1 )
747 {
748 libcerror_error_set(
749 error,
750 LIBCERROR_ERROR_DOMAIN_RUNTIME,
751 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
752 "%s: unable to retrieve parent filename.",
753 function );
754
755 goto on_error;
756 }
757 fprintf(
758 info_handle->notify_stream,
759 "\tParent filename\t\t: %s\n",
760 value_string );
761
762 memory_free(
763 value_string );
764
765 value_string = NULL;
766 }
767 /* TODO add more info */
768
769 fprintf(
770 info_handle->notify_stream,
771 "\n" );
772
773 return( 1 );
774
775 on_error:
776 if( value_string != NULL )
777 {
778 memory_free(
779 value_string );
780 }
781 if( guid != NULL )
782 {
783 libfguid_identifier_free(
784 &guid,
785 NULL );
786 }
787 return( -1 );
788 }
789