/src/libexe/libexe/libexe_section.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Section functions |
3 | | * |
4 | | * Copyright (C) 2011-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 <memory.h> |
24 | | #include <types.h> |
25 | | |
26 | | #include "libexe_definitions.h" |
27 | | #include "libexe_io_handle.h" |
28 | | #include "libexe_libbfio.h" |
29 | | #include "libexe_libcerror.h" |
30 | | #include "libexe_libfcache.h" |
31 | | #include "libexe_libfdata.h" |
32 | | #include "libexe_libuna.h" |
33 | | #include "libexe_section.h" |
34 | | #include "libexe_section_io_handle.h" |
35 | | |
36 | | /* Creates a section |
37 | | * Make sure the value section is referencing, is set to NULL |
38 | | * Returns 1 if successful or -1 on error |
39 | | */ |
40 | | int libexe_section_initialize( |
41 | | libexe_section_t **section, |
42 | | libexe_io_handle_t *io_handle, |
43 | | libbfio_handle_t *file_io_handle, |
44 | | libexe_section_descriptor_t *section_descriptor, |
45 | | libcerror_error_t **error ) |
46 | 0 | { |
47 | 0 | libexe_internal_section_t *internal_section = NULL; |
48 | 0 | static char *function = "libexe_section_initialize"; |
49 | |
|
50 | 0 | if( section == NULL ) |
51 | 0 | { |
52 | 0 | libcerror_error_set( |
53 | 0 | error, |
54 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
55 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
56 | 0 | "%s: invalid section.", |
57 | 0 | function ); |
58 | |
|
59 | 0 | return( -1 ); |
60 | 0 | } |
61 | 0 | if( *section != NULL ) |
62 | 0 | { |
63 | 0 | libcerror_error_set( |
64 | 0 | error, |
65 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
66 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
67 | 0 | "%s: invalid section value already set.", |
68 | 0 | function ); |
69 | |
|
70 | 0 | return( -1 ); |
71 | 0 | } |
72 | 0 | if( section_descriptor == NULL ) |
73 | 0 | { |
74 | 0 | libcerror_error_set( |
75 | 0 | error, |
76 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
77 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
78 | 0 | "%s: invalid section descriptor.", |
79 | 0 | function ); |
80 | |
|
81 | 0 | return( -1 ); |
82 | 0 | } |
83 | 0 | internal_section = memory_allocate_structure( |
84 | 0 | libexe_internal_section_t ); |
85 | |
|
86 | 0 | if( internal_section == NULL ) |
87 | 0 | { |
88 | 0 | libcerror_error_set( |
89 | 0 | error, |
90 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
91 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
92 | 0 | "%s: unable to create internal section.", |
93 | 0 | function ); |
94 | |
|
95 | 0 | goto on_error; |
96 | 0 | } |
97 | 0 | if( memory_set( |
98 | 0 | internal_section, |
99 | 0 | 0, |
100 | 0 | sizeof( libexe_internal_section_t ) ) == NULL ) |
101 | 0 | { |
102 | 0 | libcerror_error_set( |
103 | 0 | error, |
104 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
105 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
106 | 0 | "%s: unable to clear internal section.", |
107 | 0 | function ); |
108 | |
|
109 | 0 | memory_free( |
110 | 0 | internal_section ); |
111 | |
|
112 | 0 | return( -1 ); |
113 | 0 | } |
114 | 0 | internal_section->file_io_handle = file_io_handle; |
115 | 0 | internal_section->io_handle = io_handle; |
116 | 0 | internal_section->section_descriptor = section_descriptor; |
117 | |
|
118 | 0 | *section = (libexe_section_t *) internal_section; |
119 | |
|
120 | 0 | return( 1 ); |
121 | | |
122 | 0 | on_error: |
123 | 0 | if( internal_section != NULL ) |
124 | 0 | { |
125 | 0 | memory_free( |
126 | 0 | internal_section ); |
127 | 0 | } |
128 | 0 | return( -1 ); |
129 | 0 | } |
130 | | |
131 | | /* Frees a section |
132 | | * Returns 1 if successful or -1 on error |
133 | | */ |
134 | | int libexe_section_free( |
135 | | libexe_section_t **section, |
136 | | libcerror_error_t **error ) |
137 | 0 | { |
138 | 0 | libexe_internal_section_t *internal_section = NULL; |
139 | 0 | static char *function = "libexe_section_free"; |
140 | |
|
141 | 0 | if( section == NULL ) |
142 | 0 | { |
143 | 0 | libcerror_error_set( |
144 | 0 | error, |
145 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
146 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
147 | 0 | "%s: invalid section.", |
148 | 0 | function ); |
149 | |
|
150 | 0 | return( -1 ); |
151 | 0 | } |
152 | 0 | if( *section != NULL ) |
153 | 0 | { |
154 | 0 | internal_section = (libexe_internal_section_t *) *section; |
155 | 0 | *section = NULL; |
156 | | |
157 | | /* The file_io_handle, io_handle and section_descriptor references are freed elsewhere |
158 | | */ |
159 | 0 | memory_free( |
160 | 0 | internal_section ); |
161 | 0 | } |
162 | 0 | return( 1 ); |
163 | 0 | } |
164 | | |
165 | | /* Retrieves the size of the ASCII formatted name |
166 | | * The returned size includes the end of string character |
167 | | * Returns 1 if successful or -1 on error |
168 | | */ |
169 | | int libexe_section_get_name_size( |
170 | | libexe_section_t *section, |
171 | | size_t *string_size, |
172 | | libcerror_error_t **error ) |
173 | 0 | { |
174 | 0 | libexe_internal_section_t *internal_section = NULL; |
175 | 0 | static char *function = "libexe_section_get_name_size"; |
176 | |
|
177 | 0 | if( section == NULL ) |
178 | 0 | { |
179 | 0 | libcerror_error_set( |
180 | 0 | error, |
181 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
182 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
183 | 0 | "%s: invalid section.", |
184 | 0 | function ); |
185 | |
|
186 | 0 | return( -1 ); |
187 | 0 | } |
188 | 0 | internal_section = (libexe_internal_section_t *) section; |
189 | |
|
190 | 0 | if( internal_section->section_descriptor == NULL ) |
191 | 0 | { |
192 | 0 | libcerror_error_set( |
193 | 0 | error, |
194 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
195 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
196 | 0 | "%s: invalid section - missing section descriptor.", |
197 | 0 | function ); |
198 | |
|
199 | 0 | return( -1 ); |
200 | 0 | } |
201 | 0 | if( string_size == NULL ) |
202 | 0 | { |
203 | 0 | libcerror_error_set( |
204 | 0 | error, |
205 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
206 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
207 | 0 | "%s: invalid string size.", |
208 | 0 | function ); |
209 | |
|
210 | 0 | return( -1 ); |
211 | 0 | } |
212 | 0 | *string_size = internal_section->section_descriptor->name_size; |
213 | |
|
214 | 0 | return( 1 ); |
215 | 0 | } |
216 | | |
217 | | /* Retrieves the ASCII formatted name |
218 | | * The size should include the end of string character |
219 | | * Returns 1 if successful or -1 on error |
220 | | */ |
221 | | int libexe_section_get_name( |
222 | | libexe_section_t *section, |
223 | | char *string, |
224 | | size_t string_size, |
225 | | libcerror_error_t **error ) |
226 | 0 | { |
227 | 0 | libexe_internal_section_t *internal_section = NULL; |
228 | 0 | static char *function = "libexe_section_get_name"; |
229 | |
|
230 | 0 | if( section == NULL ) |
231 | 0 | { |
232 | 0 | libcerror_error_set( |
233 | 0 | error, |
234 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
235 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
236 | 0 | "%s: invalid section.", |
237 | 0 | function ); |
238 | |
|
239 | 0 | return( -1 ); |
240 | 0 | } |
241 | 0 | internal_section = (libexe_internal_section_t *) section; |
242 | |
|
243 | 0 | if( internal_section->section_descriptor == NULL ) |
244 | 0 | { |
245 | 0 | libcerror_error_set( |
246 | 0 | error, |
247 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
248 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
249 | 0 | "%s: invalid section - missing section descriptor.", |
250 | 0 | function ); |
251 | |
|
252 | 0 | return( -1 ); |
253 | 0 | } |
254 | 0 | if( string_size > (size_t) SSIZE_MAX ) |
255 | 0 | { |
256 | 0 | libcerror_error_set( |
257 | 0 | error, |
258 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
259 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM, |
260 | 0 | "%s: invalid string size value exceeds maximum.", |
261 | 0 | function ); |
262 | |
|
263 | 0 | return( -1 ); |
264 | 0 | } |
265 | 0 | if( ( string_size < 1 ) |
266 | 0 | || ( string_size < internal_section->section_descriptor->name_size ) ) |
267 | 0 | { |
268 | 0 | libcerror_error_set( |
269 | 0 | error, |
270 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
271 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
272 | 0 | "%s: invalid string value too small.", |
273 | 0 | function ); |
274 | |
|
275 | 0 | return( -1 ); |
276 | 0 | } |
277 | 0 | if( internal_section->section_descriptor->name_size == 0 ) |
278 | 0 | { |
279 | 0 | string[ 0 ] = 0; |
280 | 0 | } |
281 | 0 | else |
282 | 0 | { |
283 | 0 | if( memory_copy( |
284 | 0 | string, |
285 | 0 | internal_section->section_descriptor->name, |
286 | 0 | internal_section->section_descriptor->name_size ) == NULL ) |
287 | 0 | { |
288 | 0 | libcerror_error_set( |
289 | 0 | error, |
290 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
291 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
292 | 0 | "%s: unable to copy name.", |
293 | 0 | function ); |
294 | |
|
295 | 0 | return( -1 ); |
296 | 0 | } |
297 | 0 | } |
298 | 0 | return( 1 ); |
299 | 0 | } |
300 | | |
301 | | /* Retrieves the size of the UTF-8 formatted name |
302 | | * The returned size includes the end of string character |
303 | | * Returns 1 if successful or -1 on error |
304 | | */ |
305 | | int libexe_section_get_utf8_name_size( |
306 | | libexe_section_t *section, |
307 | | size_t *utf8_string_size, |
308 | | libcerror_error_t **error ) |
309 | 0 | { |
310 | 0 | libexe_internal_section_t *internal_section = NULL; |
311 | 0 | static char *function = "libexe_section_get_utf8_name_size"; |
312 | |
|
313 | 0 | if( section == NULL ) |
314 | 0 | { |
315 | 0 | libcerror_error_set( |
316 | 0 | error, |
317 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
318 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
319 | 0 | "%s: invalid section.", |
320 | 0 | function ); |
321 | |
|
322 | 0 | return( -1 ); |
323 | 0 | } |
324 | 0 | internal_section = (libexe_internal_section_t *) section; |
325 | |
|
326 | 0 | if( internal_section->io_handle == NULL ) |
327 | 0 | { |
328 | 0 | libcerror_error_set( |
329 | 0 | error, |
330 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
331 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
332 | 0 | "%s: invalid section - missing IO handle.", |
333 | 0 | function ); |
334 | |
|
335 | 0 | return( -1 ); |
336 | 0 | } |
337 | 0 | if( internal_section->section_descriptor == NULL ) |
338 | 0 | { |
339 | 0 | libcerror_error_set( |
340 | 0 | error, |
341 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
342 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
343 | 0 | "%s: invalid section - missing section descriptor.", |
344 | 0 | function ); |
345 | |
|
346 | 0 | return( -1 ); |
347 | 0 | } |
348 | 0 | if( libuna_utf8_string_size_from_byte_stream( |
349 | 0 | (uint8_t *) internal_section->section_descriptor->name, |
350 | 0 | internal_section->section_descriptor->name_size, |
351 | 0 | internal_section->io_handle->ascii_codepage, |
352 | 0 | utf8_string_size, |
353 | 0 | error ) != 1 ) |
354 | 0 | { |
355 | 0 | libcerror_error_set( |
356 | 0 | error, |
357 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
358 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
359 | 0 | "%s: unable to retrieve UTF-8 string size.", |
360 | 0 | function ); |
361 | |
|
362 | 0 | return( -1 ); |
363 | 0 | } |
364 | 0 | return( 1 ); |
365 | 0 | } |
366 | | |
367 | | /* Retrieves the UTF-8 formatted name |
368 | | * The size should include the end of string character |
369 | | * Returns 1 if successful or -1 on error |
370 | | */ |
371 | | int libexe_section_get_utf8_name( |
372 | | libexe_section_t *section, |
373 | | uint8_t *utf8_string, |
374 | | size_t utf8_string_size, |
375 | | libcerror_error_t **error ) |
376 | 0 | { |
377 | 0 | libexe_internal_section_t *internal_section = NULL; |
378 | 0 | static char *function = "libexe_section_get_utf8_name"; |
379 | |
|
380 | 0 | if( section == NULL ) |
381 | 0 | { |
382 | 0 | libcerror_error_set( |
383 | 0 | error, |
384 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
385 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
386 | 0 | "%s: invalid section.", |
387 | 0 | function ); |
388 | |
|
389 | 0 | return( -1 ); |
390 | 0 | } |
391 | 0 | internal_section = (libexe_internal_section_t *) section; |
392 | |
|
393 | 0 | if( internal_section->io_handle == NULL ) |
394 | 0 | { |
395 | 0 | libcerror_error_set( |
396 | 0 | error, |
397 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
398 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
399 | 0 | "%s: invalid section - missing IO handle.", |
400 | 0 | function ); |
401 | |
|
402 | 0 | return( -1 ); |
403 | 0 | } |
404 | 0 | if( internal_section->section_descriptor == NULL ) |
405 | 0 | { |
406 | 0 | libcerror_error_set( |
407 | 0 | error, |
408 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
409 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
410 | 0 | "%s: invalid section - missing section descriptor.", |
411 | 0 | function ); |
412 | |
|
413 | 0 | return( -1 ); |
414 | 0 | } |
415 | 0 | if( libuna_utf8_string_copy_from_byte_stream( |
416 | 0 | utf8_string, |
417 | 0 | utf8_string_size, |
418 | 0 | (uint8_t *) internal_section->section_descriptor->name, |
419 | 0 | internal_section->section_descriptor->name_size, |
420 | 0 | internal_section->io_handle->ascii_codepage, |
421 | 0 | error ) != 1 ) |
422 | 0 | { |
423 | 0 | libcerror_error_set( |
424 | 0 | error, |
425 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
426 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
427 | 0 | "%s: unable to copy name to UTF-8 string.", |
428 | 0 | function ); |
429 | |
|
430 | 0 | return( -1 ); |
431 | 0 | } |
432 | 0 | return( 1 ); |
433 | 0 | } |
434 | | |
435 | | /* Retrieves the size of the UTF-16 formatted name |
436 | | * The returned size includes the end of string character |
437 | | * Returns 1 if successful or -1 on error |
438 | | */ |
439 | | int libexe_section_get_utf16_name_size( |
440 | | libexe_section_t *section, |
441 | | size_t *utf16_string_size, |
442 | | libcerror_error_t **error ) |
443 | 0 | { |
444 | 0 | libexe_internal_section_t *internal_section = NULL; |
445 | 0 | static char *function = "libexe_section_get_utf16_name_size"; |
446 | |
|
447 | 0 | if( section == NULL ) |
448 | 0 | { |
449 | 0 | libcerror_error_set( |
450 | 0 | error, |
451 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
452 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
453 | 0 | "%s: invalid section.", |
454 | 0 | function ); |
455 | |
|
456 | 0 | return( -1 ); |
457 | 0 | } |
458 | 0 | internal_section = (libexe_internal_section_t *) section; |
459 | |
|
460 | 0 | if( internal_section->io_handle == NULL ) |
461 | 0 | { |
462 | 0 | libcerror_error_set( |
463 | 0 | error, |
464 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
465 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
466 | 0 | "%s: invalid section - missing IO handle.", |
467 | 0 | function ); |
468 | |
|
469 | 0 | return( -1 ); |
470 | 0 | } |
471 | 0 | if( internal_section->section_descriptor == NULL ) |
472 | 0 | { |
473 | 0 | libcerror_error_set( |
474 | 0 | error, |
475 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
476 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
477 | 0 | "%s: invalid section - missing section descriptor.", |
478 | 0 | function ); |
479 | |
|
480 | 0 | return( -1 ); |
481 | 0 | } |
482 | 0 | if( libuna_utf16_string_size_from_byte_stream( |
483 | 0 | (uint8_t *) internal_section->section_descriptor->name, |
484 | 0 | internal_section->section_descriptor->name_size, |
485 | 0 | internal_section->io_handle->ascii_codepage, |
486 | 0 | utf16_string_size, |
487 | 0 | error ) != 1 ) |
488 | 0 | { |
489 | 0 | libcerror_error_set( |
490 | 0 | error, |
491 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
492 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
493 | 0 | "%s: unable to retrieve UTF-16 string size.", |
494 | 0 | function ); |
495 | |
|
496 | 0 | return( -1 ); |
497 | 0 | } |
498 | 0 | return( -1 ); |
499 | 0 | } |
500 | | |
501 | | /* Retrieves the UTF-16 formatted name |
502 | | * The size should include the end of string character |
503 | | * Returns 1 if successful or -1 on error |
504 | | */ |
505 | | int libexe_section_get_utf16_name( |
506 | | libexe_section_t *section, |
507 | | uint16_t *utf16_string, |
508 | | size_t utf16_string_size, |
509 | | libcerror_error_t **error ) |
510 | 0 | { |
511 | 0 | libexe_internal_section_t *internal_section = NULL; |
512 | 0 | static char *function = "libexe_section_get_utf16_name"; |
513 | |
|
514 | 0 | if( section == NULL ) |
515 | 0 | { |
516 | 0 | libcerror_error_set( |
517 | 0 | error, |
518 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
519 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
520 | 0 | "%s: invalid section.", |
521 | 0 | function ); |
522 | |
|
523 | 0 | return( -1 ); |
524 | 0 | } |
525 | 0 | internal_section = (libexe_internal_section_t *) section; |
526 | |
|
527 | 0 | if( internal_section->io_handle == NULL ) |
528 | 0 | { |
529 | 0 | libcerror_error_set( |
530 | 0 | error, |
531 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
532 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
533 | 0 | "%s: invalid section - missing IO handle.", |
534 | 0 | function ); |
535 | |
|
536 | 0 | return( -1 ); |
537 | 0 | } |
538 | 0 | if( internal_section->section_descriptor == NULL ) |
539 | 0 | { |
540 | 0 | libcerror_error_set( |
541 | 0 | error, |
542 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
543 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
544 | 0 | "%s: invalid section - missing section descriptor.", |
545 | 0 | function ); |
546 | |
|
547 | 0 | return( -1 ); |
548 | 0 | } |
549 | 0 | if( libuna_utf16_string_copy_from_byte_stream( |
550 | 0 | utf16_string, |
551 | 0 | utf16_string_size, |
552 | 0 | (uint8_t *) internal_section->section_descriptor->name, |
553 | 0 | internal_section->section_descriptor->name_size, |
554 | 0 | internal_section->io_handle->ascii_codepage, |
555 | 0 | error ) != 1 ) |
556 | 0 | { |
557 | 0 | libcerror_error_set( |
558 | 0 | error, |
559 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
560 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
561 | 0 | "%s: unable to copy name to UTF-16 string.", |
562 | 0 | function ); |
563 | |
|
564 | 0 | return( -1 ); |
565 | 0 | } |
566 | 0 | return( 1 ); |
567 | 0 | } |
568 | | |
569 | | /* Reads data at the current offset into a buffer |
570 | | * Returns the number of bytes read or -1 on error |
571 | | */ |
572 | | ssize_t libexe_section_read_buffer( |
573 | | libexe_section_t *section, |
574 | | void *buffer, |
575 | | size_t buffer_size, |
576 | | libcerror_error_t **error ) |
577 | 0 | { |
578 | 0 | libexe_internal_section_t *internal_section = NULL; |
579 | 0 | static char *function = "libexe_section_read_buffer"; |
580 | 0 | ssize_t read_count = 0; |
581 | |
|
582 | 0 | if( section == NULL ) |
583 | 0 | { |
584 | 0 | libcerror_error_set( |
585 | 0 | error, |
586 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
587 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
588 | 0 | "%s: invalid section.", |
589 | 0 | function ); |
590 | |
|
591 | 0 | return( -1 ); |
592 | 0 | } |
593 | 0 | internal_section = (libexe_internal_section_t *) section; |
594 | |
|
595 | 0 | if( internal_section->section_descriptor == NULL ) |
596 | 0 | { |
597 | 0 | libcerror_error_set( |
598 | 0 | error, |
599 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
600 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
601 | 0 | "%s: invalid section - missing section descriptor.", |
602 | 0 | function ); |
603 | |
|
604 | 0 | return( -1 ); |
605 | 0 | } |
606 | 0 | read_count = libfdata_stream_read_buffer( |
607 | 0 | internal_section->section_descriptor->data_stream, |
608 | 0 | (intptr_t *) internal_section->file_io_handle, |
609 | 0 | buffer, |
610 | 0 | buffer_size, |
611 | 0 | 0, |
612 | 0 | error ); |
613 | |
|
614 | 0 | if( read_count == -1 ) |
615 | 0 | { |
616 | 0 | libcerror_error_set( |
617 | 0 | error, |
618 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
619 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
620 | 0 | "%s: unable to read buffer from section data stream.", |
621 | 0 | function ); |
622 | |
|
623 | 0 | return( -1 ); |
624 | 0 | } |
625 | 0 | return( read_count ); |
626 | 0 | } |
627 | | |
628 | | /* Reads data at a specific offset into a buffer |
629 | | * Returns the number of bytes read or -1 on error |
630 | | */ |
631 | | ssize_t libexe_section_read_buffer_at_offset( |
632 | | libexe_section_t *section, |
633 | | void *buffer, |
634 | | size_t buffer_size, |
635 | | off64_t offset, |
636 | | libcerror_error_t **error ) |
637 | 0 | { |
638 | 0 | static char *function = "libexe_section_read_buffer_at_offset"; |
639 | 0 | ssize_t read_count = 0; |
640 | |
|
641 | 0 | if( libexe_section_seek_offset( |
642 | 0 | section, |
643 | 0 | offset, |
644 | 0 | SEEK_SET, |
645 | 0 | error ) == -1 ) |
646 | 0 | { |
647 | 0 | libcerror_error_set( |
648 | 0 | error, |
649 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
650 | 0 | LIBCERROR_IO_ERROR_SEEK_FAILED, |
651 | 0 | "%s: unable to seek offset.", |
652 | 0 | function ); |
653 | |
|
654 | 0 | return( -1 ); |
655 | 0 | } |
656 | 0 | read_count = libexe_section_read_buffer( |
657 | 0 | section, |
658 | 0 | buffer, |
659 | 0 | buffer_size, |
660 | 0 | error ); |
661 | |
|
662 | 0 | if( read_count < 0 ) |
663 | 0 | { |
664 | 0 | libcerror_error_set( |
665 | 0 | error, |
666 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
667 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
668 | 0 | "%s: unable to read buffer.", |
669 | 0 | function ); |
670 | |
|
671 | 0 | return( -1 ); |
672 | 0 | } |
673 | 0 | return( read_count ); |
674 | 0 | } |
675 | | |
676 | | /* Seeks a certain offset of the data |
677 | | * Returns the offset if seek is successful or -1 on error |
678 | | */ |
679 | | off64_t libexe_section_seek_offset( |
680 | | libexe_section_t *section, |
681 | | off64_t offset, |
682 | | int whence, |
683 | | libcerror_error_t **error ) |
684 | 0 | { |
685 | 0 | libexe_internal_section_t *internal_section = NULL; |
686 | 0 | static char *function = "libexe_section_seek_offset"; |
687 | |
|
688 | 0 | if( section == NULL ) |
689 | 0 | { |
690 | 0 | libcerror_error_set( |
691 | 0 | error, |
692 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
693 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
694 | 0 | "%s: invalid section.", |
695 | 0 | function ); |
696 | |
|
697 | 0 | return( -1 ); |
698 | 0 | } |
699 | 0 | internal_section = (libexe_internal_section_t *) section; |
700 | |
|
701 | 0 | if( internal_section->section_descriptor == NULL ) |
702 | 0 | { |
703 | 0 | libcerror_error_set( |
704 | 0 | error, |
705 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
706 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
707 | 0 | "%s: invalid section - missing section descriptor.", |
708 | 0 | function ); |
709 | |
|
710 | 0 | return( -1 ); |
711 | 0 | } |
712 | 0 | offset = libfdata_stream_seek_offset( |
713 | 0 | internal_section->section_descriptor->data_stream, |
714 | 0 | offset, |
715 | 0 | whence, |
716 | 0 | error ); |
717 | |
|
718 | 0 | if( offset == -1 ) |
719 | 0 | { |
720 | 0 | libcerror_error_set( |
721 | 0 | error, |
722 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
723 | 0 | LIBCERROR_IO_ERROR_SEEK_FAILED, |
724 | 0 | "%s: unable to seek in section data stream.", |
725 | 0 | function ); |
726 | |
|
727 | 0 | return( -1 ); |
728 | 0 | } |
729 | 0 | return( offset ); |
730 | 0 | } |
731 | | |
732 | | /* Retrieves the (current) offset |
733 | | * Returns 1 if successful or -1 on error |
734 | | */ |
735 | | int libexe_section_get_offset( |
736 | | libexe_section_t *section, |
737 | | off64_t *offset, |
738 | | libcerror_error_t **error ) |
739 | 0 | { |
740 | 0 | libexe_internal_section_t *internal_section = NULL; |
741 | 0 | static char *function = "libexe_section_get_offset"; |
742 | |
|
743 | 0 | if( section == NULL ) |
744 | 0 | { |
745 | 0 | libcerror_error_set( |
746 | 0 | error, |
747 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
748 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
749 | 0 | "%s: invalid section.", |
750 | 0 | function ); |
751 | |
|
752 | 0 | return( -1 ); |
753 | 0 | } |
754 | 0 | internal_section = (libexe_internal_section_t *) section; |
755 | |
|
756 | 0 | if( internal_section->section_descriptor == NULL ) |
757 | 0 | { |
758 | 0 | libcerror_error_set( |
759 | 0 | error, |
760 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
761 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
762 | 0 | "%s: invalid section - missing section descriptor.", |
763 | 0 | function ); |
764 | |
|
765 | 0 | return( -1 ); |
766 | 0 | } |
767 | 0 | if( libfdata_stream_get_offset( |
768 | 0 | internal_section->section_descriptor->data_stream, |
769 | 0 | offset, |
770 | 0 | error ) != 1 ) |
771 | 0 | { |
772 | 0 | libcerror_error_set( |
773 | 0 | error, |
774 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
775 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
776 | 0 | "%s: unable to retrieve section data stream offset.", |
777 | 0 | function ); |
778 | |
|
779 | 0 | return( -1 ); |
780 | 0 | } |
781 | 0 | return( 1 ); |
782 | 0 | } |
783 | | |
784 | | /* Retrieves the size |
785 | | * Returns 1 if successful or -1 on error |
786 | | */ |
787 | | int libexe_section_get_size( |
788 | | libexe_section_t *section, |
789 | | size64_t *size, |
790 | | libcerror_error_t **error ) |
791 | 0 | { |
792 | 0 | libexe_internal_section_t *internal_section = NULL; |
793 | 0 | static char *function = "libexe_section_get_size"; |
794 | |
|
795 | 0 | if( section == NULL ) |
796 | 0 | { |
797 | 0 | libcerror_error_set( |
798 | 0 | error, |
799 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
800 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
801 | 0 | "%s: invalid section.", |
802 | 0 | function ); |
803 | |
|
804 | 0 | return( -1 ); |
805 | 0 | } |
806 | 0 | internal_section = (libexe_internal_section_t *) section; |
807 | |
|
808 | 0 | if( libexe_section_descriptor_get_data_size( |
809 | 0 | internal_section->section_descriptor, |
810 | 0 | size, |
811 | 0 | error ) != 1 ) |
812 | 0 | { |
813 | 0 | libcerror_error_set( |
814 | 0 | error, |
815 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
816 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
817 | 0 | "%s: unable to retrieve section descriptor data size.", |
818 | 0 | function ); |
819 | |
|
820 | 0 | return( -1 ); |
821 | 0 | } |
822 | 0 | return( 1 ); |
823 | 0 | } |
824 | | |
825 | | /* Retrieves the start offset |
826 | | * Returns 1 if successful or -1 on error |
827 | | */ |
828 | | int libexe_section_get_start_offset( |
829 | | libexe_section_t *section, |
830 | | off64_t *start_offset, |
831 | | libcerror_error_t **error ) |
832 | 0 | { |
833 | 0 | libexe_internal_section_t *internal_section = NULL; |
834 | 0 | static char *function = "libexe_section_get_start_offset"; |
835 | 0 | size64_t segment_size = 0; |
836 | 0 | uint32_t segment_flags = 0; |
837 | 0 | int segment_file_index = 0; |
838 | |
|
839 | 0 | if( section == NULL ) |
840 | 0 | { |
841 | 0 | libcerror_error_set( |
842 | 0 | error, |
843 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
844 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
845 | 0 | "%s: invalid section.", |
846 | 0 | function ); |
847 | |
|
848 | 0 | return( -1 ); |
849 | 0 | } |
850 | 0 | internal_section = (libexe_internal_section_t *) section; |
851 | |
|
852 | 0 | if( internal_section->section_descriptor == NULL ) |
853 | 0 | { |
854 | 0 | libcerror_error_set( |
855 | 0 | error, |
856 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
857 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
858 | 0 | "%s: invalid section - missing section descriptor.", |
859 | 0 | function ); |
860 | |
|
861 | 0 | return( -1 ); |
862 | 0 | } |
863 | 0 | if( libfdata_stream_get_segment_by_index( |
864 | 0 | internal_section->section_descriptor->data_stream, |
865 | 0 | 0, |
866 | 0 | &segment_file_index, |
867 | 0 | start_offset, |
868 | 0 | &segment_size, |
869 | 0 | &segment_flags, |
870 | 0 | error ) != 1 ) |
871 | 0 | { |
872 | 0 | libcerror_error_set( |
873 | 0 | error, |
874 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
875 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
876 | 0 | "%s: unable to retrieve section data stream segment: 0.", |
877 | 0 | function ); |
878 | |
|
879 | 0 | return( -1 ); |
880 | 0 | } |
881 | 0 | return( 1 ); |
882 | 0 | } |
883 | | |
884 | | /* Retrieves the virtual address |
885 | | * Returns 1 if successful or -1 on error |
886 | | */ |
887 | | int libexe_section_get_virtual_address( |
888 | | libexe_section_t *section, |
889 | | uint32_t *virtual_address, |
890 | | libcerror_error_t **error ) |
891 | 0 | { |
892 | 0 | libexe_internal_section_t *internal_section = NULL; |
893 | 0 | static char *function = "libexe_section_get_virtual_address"; |
894 | |
|
895 | 0 | if( section == NULL ) |
896 | 0 | { |
897 | 0 | libcerror_error_set( |
898 | 0 | error, |
899 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
900 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
901 | 0 | "%s: invalid section.", |
902 | 0 | function ); |
903 | |
|
904 | 0 | return( -1 ); |
905 | 0 | } |
906 | 0 | internal_section = (libexe_internal_section_t *) section; |
907 | |
|
908 | 0 | if( internal_section->section_descriptor == NULL ) |
909 | 0 | { |
910 | 0 | libcerror_error_set( |
911 | 0 | error, |
912 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
913 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
914 | 0 | "%s: invalid section - missing section descriptor.", |
915 | 0 | function ); |
916 | |
|
917 | 0 | return( -1 ); |
918 | 0 | } |
919 | 0 | if( virtual_address == NULL ) |
920 | 0 | { |
921 | 0 | libcerror_error_set( |
922 | 0 | error, |
923 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
924 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
925 | 0 | "%s: invalid virtual address.", |
926 | 0 | function ); |
927 | |
|
928 | 0 | return( -1 ); |
929 | 0 | } |
930 | 0 | *virtual_address = internal_section->section_descriptor->virtual_address; |
931 | |
|
932 | 0 | return( 1 ); |
933 | 0 | } |
934 | | |
935 | | /* Retrieves the section data file IO handle |
936 | | * Returns 1 if successful -1 on error |
937 | | */ |
938 | | int libexe_section_get_data_file_io_handle( |
939 | | libexe_section_t *section, |
940 | | libbfio_handle_t **file_io_handle, |
941 | | libcerror_error_t **error ) |
942 | 0 | { |
943 | 0 | libexe_section_io_handle_t *io_handle = NULL; |
944 | 0 | static char *function = "libexe_section_get_data_file_io_handle"; |
945 | |
|
946 | 0 | if( libexe_section_io_handle_initialize( |
947 | 0 | &io_handle, |
948 | 0 | section, |
949 | 0 | error ) != 1 ) |
950 | 0 | { |
951 | 0 | libcerror_error_set( |
952 | 0 | error, |
953 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
954 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
955 | 0 | "%s: unable to create section file IO handle.", |
956 | 0 | function ); |
957 | |
|
958 | 0 | goto on_error; |
959 | 0 | } |
960 | 0 | if( libbfio_handle_initialize( |
961 | 0 | file_io_handle, |
962 | 0 | (intptr_t *) io_handle, |
963 | 0 | (int (*)(intptr_t **, libcerror_error_t **)) libexe_section_io_handle_free, |
964 | 0 | (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) libexe_section_io_handle_clone, |
965 | 0 | (int (*)(intptr_t *, int flags, libcerror_error_t **)) libexe_section_io_handle_open, |
966 | 0 | (int (*)(intptr_t *, libcerror_error_t **)) libexe_section_io_handle_close, |
967 | 0 | (ssize_t (*)(intptr_t *, uint8_t *, size_t, libcerror_error_t **)) libexe_section_io_handle_read, |
968 | 0 | (ssize_t (*)(intptr_t *, const uint8_t *, size_t, libcerror_error_t **)) libexe_section_io_handle_write, |
969 | 0 | (off64_t (*)(intptr_t *, off64_t, int, libcerror_error_t **)) libexe_section_io_handle_seek_offset, |
970 | 0 | (int (*)(intptr_t *, libcerror_error_t **)) libexe_section_io_handle_exists, |
971 | 0 | (int (*)(intptr_t *, libcerror_error_t **)) libexe_section_io_handle_is_open, |
972 | 0 | (int (*)(intptr_t *, size64_t *, libcerror_error_t **)) libexe_section_io_handle_get_size, |
973 | 0 | LIBBFIO_FLAG_IO_HANDLE_MANAGED | LIBBFIO_FLAG_IO_HANDLE_CLONE_BY_FUNCTION, |
974 | 0 | error ) != 1 ) |
975 | 0 | { |
976 | 0 | libcerror_error_set( |
977 | 0 | error, |
978 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
979 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
980 | 0 | "%s: unable to create file IO handle.", |
981 | 0 | function ); |
982 | |
|
983 | 0 | goto on_error; |
984 | 0 | } |
985 | 0 | return( 1 ); |
986 | | |
987 | 0 | on_error: |
988 | 0 | if( io_handle != NULL ) |
989 | 0 | { |
990 | 0 | libexe_section_io_handle_free( |
991 | 0 | &io_handle, |
992 | 0 | NULL ); |
993 | 0 | } |
994 | 0 | return( -1 ); |
995 | 0 | } |
996 | | |