1 /*
2  * Debug functions
3  *
4  * Copyright (C) 2012-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 "libmodi_debug.h"
26 #include "libmodi_definitions.h"
27 #include "libmodi_libbfio.h"
28 #include "libmodi_libcerror.h"
29 #include "libmodi_libcnotify.h"
30 
31 #if defined( HAVE_DEBUG_OUTPUT )
32 
33 /* Retrieves a string containing the UDIF block table entry type
34  */
libmodi_debug_get_udif_block_table_entry_type(uint32_t udif_block_table_entry)35 const char *libmodi_debug_get_udif_block_table_entry_type(
36              uint32_t udif_block_table_entry )
37 {
38 	switch( udif_block_table_entry )
39 	{
40 		case 0x00000000UL:
41 			return( "sparse" );
42 
43 		case 0x00000001UL:
44 			return( "raw" );
45 
46 		case 0x00000002UL:
47 			return( "sparse" );
48 
49 		case 0x7ffffffeUL:
50 			return( "comment" );
51 
52 		case LIBMODI_UDIF_BLOCK_TABLE_ENTRY_TYPE_ADC_COMPRESSED:
53 			return( "ADC compressed" );
54 
55 		case LIBMODI_UDIF_BLOCK_TABLE_ENTRY_TYPE_ZLIB_COMPRESSED:
56 			return( "zlib compressed" );
57 
58 		case LIBMODI_UDIF_BLOCK_TABLE_ENTRY_TYPE_BZIP2_COMPRESSED:
59 			return( "bzip2 compressed" );
60 
61 		case LIBMODI_UDIF_BLOCK_TABLE_ENTRY_TYPE_LZFSE_COMPRESSED:
62 			return( "LZFSE compressed" );
63 
64 		case LIBMODI_UDIF_BLOCK_TABLE_ENTRY_TYPE_LZMA_COMPRESSED:
65 			return( "LZMA compressed" );
66 
67 		case LIBMODI_UDIF_BLOCK_TABLE_ENTRY_TYPE_TERMINATOR:
68 			return( "terminator" );
69 	}
70 	return( "UNKNOWN" );
71 }
72 
73 /* Prints the read offsets
74  * Returns 1 if successful or -1 on error
75  */
libmodi_debug_print_read_offsets(libbfio_handle_t * file_io_handle,libcerror_error_t ** error)76 int libmodi_debug_print_read_offsets(
77      libbfio_handle_t *file_io_handle,
78      libcerror_error_t **error )
79 {
80 	static char *function = "libmodi_debug_print_read_offsets";
81 	off64_t offset        = 0;
82 	size64_t size         = 0;
83 	int number_of_offsets = 0;
84 	int offset_iterator   = 0;
85 
86 	if( file_io_handle == NULL )
87 	{
88 		libcerror_error_set(
89 		 error,
90 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
91 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
92 		 "%s: invalid file IO handle.",
93 		 function );
94 
95 		return( -1 );
96 	}
97 	if( libbfio_handle_get_number_of_offsets_read(
98 	     file_io_handle,
99 	     &number_of_offsets,
100 	     error ) != 1 )
101 	{
102 		libcerror_error_set(
103 		 error,
104 		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
105 		 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
106 		 "%s: unable to retrieve number of offsets read.",
107 		 function );
108 
109 		return( -1 );
110 	}
111 	libcnotify_printf(
112 	 "Offsets read:\n" );
113 
114 	for( offset_iterator = 0;
115 	     offset_iterator < number_of_offsets;
116 	     offset_iterator++ )
117 	{
118 		if( libbfio_handle_get_offset_read(
119 		     file_io_handle,
120 		     offset_iterator,
121 		     &offset,
122 		     &size,
123 		     error ) != 1 )
124 		{
125 			libcerror_error_set(
126 			 error,
127 			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
128 			 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
129 			 "%s: unable to retrieve offset: %d.",
130 			 function,
131 			 offset_iterator );
132 
133 			return( -1 );
134 		}
135 		libcnotify_printf(
136 		 "%08" PRIi64 " ( 0x%08" PRIx64 " ) - %08" PRIi64 " ( 0x%08" PRIx64 " ) size: %" PRIi64 "\n",
137 		 offset,
138 		 offset,
139 		 offset + (off64_t) size,
140 		 offset + (off64_t) size,
141 		 size );
142 	}
143 	libcnotify_printf(
144 	 "\n" );
145 
146 	return( 1 );
147 }
148 
149 #endif /* defined( HAVE_DEBUG_OUTPUT ) */
150 
151