1 /*
2 * Debug data functions
3 *
4 * Copyright (C) 2011-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 <memory.h>
24 #include <types.h>
25
26 #include "libexe_debug_data.h"
27 #include "libexe_libcerror.h"
28 #include "libexe_libcnotify.h"
29 #include "libexe_libfcache.h"
30 #include "libexe_libfdata.h"
31
32 /* Creates a debug data
33 * Make sure the value debug_data is referencing, is set to NULL
34 * Returns 1 if successful or -1 on error
35 */
libexe_debug_data_initialize(libexe_debug_data_t ** debug_data,libcerror_error_t ** error)36 int libexe_debug_data_initialize(
37 libexe_debug_data_t **debug_data,
38 libcerror_error_t **error )
39 {
40 static char *function = "libexe_debug_data_initialize";
41
42 if( debug_data == NULL )
43 {
44 libcerror_error_set(
45 error,
46 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
47 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
48 "%s: invalid debug data.",
49 function );
50
51 return( -1 );
52 }
53 if( *debug_data != NULL )
54 {
55 libcerror_error_set(
56 error,
57 LIBCERROR_ERROR_DOMAIN_RUNTIME,
58 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
59 "%s: invalid debug data value already set.",
60 function );
61
62 return( -1 );
63 }
64 *debug_data = memory_allocate_structure(
65 libexe_debug_data_t );
66
67 if( *debug_data == NULL )
68 {
69 libcerror_error_set(
70 error,
71 LIBCERROR_ERROR_DOMAIN_MEMORY,
72 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
73 "%s: unable to create debug data.",
74 function );
75
76 goto on_error;
77 }
78 if( memory_set(
79 *debug_data,
80 0,
81 sizeof( libexe_debug_data_t ) ) == NULL )
82 {
83 libcerror_error_set(
84 error,
85 LIBCERROR_ERROR_DOMAIN_MEMORY,
86 LIBCERROR_MEMORY_ERROR_SET_FAILED,
87 "%s: unable to clear debug data.",
88 function );
89
90 memory_free(
91 *debug_data );
92
93 *debug_data = NULL;
94
95 return( -1 );
96 }
97 return( 1 );
98
99 on_error:
100 if( *debug_data != NULL )
101 {
102 memory_free(
103 *debug_data );
104
105 *debug_data = NULL;
106 }
107 return( -1 );
108 }
109
110 /* Frees a debug data
111 * Returns 1 if successful or -1 on error
112 */
libexe_debug_data_free(libexe_debug_data_t ** debug_data,libcerror_error_t ** error)113 int libexe_debug_data_free(
114 libexe_debug_data_t **debug_data,
115 libcerror_error_t **error )
116 {
117 static char *function = "libexe_debug_data_free";
118
119 if( debug_data == NULL )
120 {
121 libcerror_error_set(
122 error,
123 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
124 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
125 "%s: invalid debug data.",
126 function );
127
128 return( -1 );
129 }
130 if( *debug_data != NULL )
131 {
132 memory_free(
133 *debug_data );
134
135 *debug_data = NULL;
136 }
137 return( 1 );
138 }
139
140 /* Reads the debug data
141 * Returns 1 if successful or -1 on error
142 */
libexe_debug_data_read(libexe_debug_data_t * debug_data,libbfio_handle_t * file_io_handle,uint32_t file_offset,uint32_t size,libcerror_error_t ** error)143 int libexe_debug_data_read(
144 libexe_debug_data_t *debug_data,
145 libbfio_handle_t *file_io_handle,
146 uint32_t file_offset,
147 uint32_t size,
148 libcerror_error_t **error )
149 {
150 uint8_t *data = NULL;
151 static char *function = "libexe_debug_data_read";
152 size_t read_count = 0;
153
154 if( debug_data == NULL )
155 {
156 libcerror_error_set(
157 error,
158 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
159 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
160 "%s: invalid debug data.",
161 function );
162
163 return( -1 );
164 }
165 if( ( size == 0 )
166 || ( size > MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
167 {
168 libcerror_error_set(
169 error,
170 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
171 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
172 "%s: invalid size value out of bounds.",
173 function );
174
175 return( -1 );
176 }
177 data = (uint8_t *) memory_allocate(
178 sizeof( uint8_t ) * size );
179
180 if( data == NULL )
181 {
182 libcerror_error_set(
183 error,
184 LIBCERROR_ERROR_DOMAIN_MEMORY,
185 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
186 "%s: unable to create debug data.",
187 function );
188
189 goto on_error;
190 }
191 #if defined( HAVE_DEBUG_OUTPUT )
192 if( libcnotify_verbose != 0 )
193 {
194 libcnotify_printf(
195 "%s: reading debug data at offset: %" PRIu32 " (0x%08" PRIx32 ")\n",
196 function,
197 file_offset,
198 file_offset );
199 }
200 #endif
201 read_count = libbfio_handle_read_buffer_at_offset(
202 file_io_handle,
203 data,
204 size,
205 (off64_t) file_offset,
206 error );
207
208 if( read_count != (ssize_t) size )
209 {
210 libcerror_error_set(
211 error,
212 LIBCERROR_ERROR_DOMAIN_IO,
213 LIBCERROR_IO_ERROR_READ_FAILED,
214 "%s: unable to read debug data.",
215 function );
216
217 goto on_error;
218 }
219 #if defined( HAVE_DEBUG_OUTPUT )
220 if( libcnotify_verbose != 0 )
221 {
222 libcnotify_printf(
223 "%s: debug data:\n",
224 function );
225 libcnotify_print_data(
226 data,
227 (size_t) size,
228 0 );
229 }
230 #endif
231 /* TODO extract values */
232
233 memory_free(
234 data );
235
236 return( 1 );
237
238 on_error:
239 if( data != NULL )
240 {
241 memory_free(
242 data );
243 }
244 return( -1 );
245 }
246
247