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