1 /*
2  * Debug functions
3  *
4  * Copyright (C) 2009-2021, 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 <types.h>
24 
25 #include "libvmdk_debug.h"
26 #include "libvmdk_definitions.h"
27 #include "libvmdk_libbfio.h"
28 #include "libvmdk_libcerror.h"
29 #include "libvmdk_libcnotify.h"
30 
31 #if defined( HAVE_DEBUG_OUTPUT )
32 
33 /* Prints the flags
34  * Returns 1 if successful or -1 on error
35  */
libvmdk_debug_print_vmdk_flags(uint32_t flags)36 void libvmdk_debug_print_vmdk_flags(
37       uint32_t flags )
38 {
39 	if( ( flags & LIBVMDK_FLAG_NEW_LINE_DETECION_VALID ) != 0 )
40 	{
41 		libcnotify_printf(
42 		 "\tNew line detection valid\n" );
43 	}
44 	if( ( flags & LIBVMDK_FLAG_USE_SECONDARY_GRAIN_DIRECTORY ) != 0 )
45 	{
46 		libcnotify_printf(
47 		 "\tUse secondary grain directory\n" );
48 	}
49 
50 	if( ( flags & LIBVMDK_FLAG_HAS_GRAIN_COMPRESSION ) != 0 )
51 	{
52 		libcnotify_printf(
53 		 "\tHas grain compression\n" );
54 	}
55 	if( ( flags & LIBVMDK_FLAG_HAS_DATA_MARKERS ) != 0 )
56 	{
57 		libcnotify_printf(
58 		 "\tHas data markers\n" );
59 	}
60 	libcnotify_printf(
61 	 "\n" );
62 }
63 
64 /* Returns a string with the compression method description
65  */
libvmdk_debug_get_compression_method_description(uint32_t compression_method)66 const char *libvmdk_debug_get_compression_method_description(
67              uint32_t compression_method )
68 {
69 	switch( compression_method )
70 	{
71 		case LIBVMDK_COMPRESSION_METHOD_NONE:
72 			return( "None" );
73 
74 		case LIBVMDK_COMPRESSION_METHOD_DEFLATE:
75 			return( "Deflate" );
76 
77 		default:
78 			break;
79 	}
80 	return( "UNKNOWN" );
81 }
82 
83 /* Returns a string with the maker description
84  */
libvmdk_debug_get_marker_description(uint32_t marker)85 const char *libvmdk_debug_get_marker_description(
86              uint32_t marker )
87 {
88 	switch( marker )
89 	{
90 		case LIBVMDK_MARKER_END_OF_STREAM:
91 			return( "End of stream" );
92 
93 		case LIBVMDK_MARKER_GRAIN_TABLE:
94 			return( "Grain table" );
95 
96 		case LIBVMDK_MARKER_GRAIN_DIRECTORY:
97 			return( "Grain directory" );
98 
99 		case LIBVMDK_MARKER_FOOTER:
100 			return( "Footer" );
101 
102 		default:
103 			break;
104 	}
105 	return( "UNKNOWN" );
106 }
107 
108 /* Prints the read offsets
109  * Returns 1 if successful or -1 on error
110  */
libvmdk_debug_print_read_offsets(libbfio_handle_t * file_io_handle,libcerror_error_t ** error)111 int libvmdk_debug_print_read_offsets(
112      libbfio_handle_t *file_io_handle,
113      libcerror_error_t **error )
114 {
115 	static char *function = "libvmdk_debug_print_read_offsets";
116 	off64_t offset        = 0;
117 	size64_t size         = 0;
118 	int number_of_offsets = 0;
119 	int offset_iterator   = 0;
120 
121 	if( file_io_handle == NULL )
122 	{
123 		libcerror_error_set(
124 		 error,
125 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
126 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
127 		 "%s: invalid file IO handle.",
128 		 function );
129 
130 		return( -1 );
131 	}
132 	if( libbfio_handle_get_number_of_offsets_read(
133 	     file_io_handle,
134 	     &number_of_offsets,
135 	     error ) != 1 )
136 	{
137 		libcerror_error_set(
138 		 error,
139 		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
140 		 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
141 		 "%s: unable to retrieve number of offsets read.",
142 		 function );
143 
144 		return( -1 );
145 	}
146 	libcnotify_printf(
147 	 "Offsets read:\n" );
148 
149 	for( offset_iterator = 0;
150 	     offset_iterator < number_of_offsets;
151 	     offset_iterator++ )
152 	{
153 		if( libbfio_handle_get_offset_read(
154 		     file_io_handle,
155 		     offset_iterator,
156 		     &offset,
157 		     &size,
158 		     error ) != 1 )
159 		{
160 			libcerror_error_set(
161 			 error,
162 			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
163 			 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
164 			 "%s: unable to retrieve offset: %d.",
165 			 function,
166 			 offset_iterator );
167 
168 			return( -1 );
169 		}
170 		libcnotify_printf(
171 		 "%08" PRIu64 " ( 0x%08" PRIx64 " ) - %08" PRIu64 " ( 0x%08" PRIx64 " ) size: %" PRIu64 "\n",
172 		 offset,
173 		 offset,
174 		 offset + size,
175 		 offset + size,
176 		 size );
177 	}
178 	libcnotify_printf(
179 	 "\n" );
180 
181 	return( 1 );
182 }
183 
184 #endif
185 
186