/src/libwrc/libwrc/libwrc_data_descriptor.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Data descriptor functions |
3 | | * |
4 | | * Copyright (C) 2011-2024, 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 <byte_stream.h> |
24 | | #include <memory.h> |
25 | | #include <types.h> |
26 | | |
27 | | #include "libwrc_data_descriptor.h" |
28 | | #include "libwrc_libbfio.h" |
29 | | #include "libwrc_libcerror.h" |
30 | | #include "libwrc_libcnotify.h" |
31 | | |
32 | | #include "wrc_data_descriptor.h" |
33 | | |
34 | | /* Creates a data descriptor |
35 | | * Make sure the value data_descriptor is referencing, is set to NULL |
36 | | * Returns 1 if successful or -1 on error |
37 | | */ |
38 | | int libwrc_data_descriptor_initialize( |
39 | | libwrc_data_descriptor_t **data_descriptor, |
40 | | libcerror_error_t **error ) |
41 | 2.01k | { |
42 | 2.01k | static char *function = "libwrc_data_descriptor_initialize"; |
43 | | |
44 | 2.01k | if( data_descriptor == NULL ) |
45 | 0 | { |
46 | 0 | libcerror_error_set( |
47 | 0 | error, |
48 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
49 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
50 | 0 | "%s: invalid data descriptor.", |
51 | 0 | function ); |
52 | |
|
53 | 0 | return( -1 ); |
54 | 0 | } |
55 | 2.01k | if( *data_descriptor != NULL ) |
56 | 0 | { |
57 | 0 | libcerror_error_set( |
58 | 0 | error, |
59 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
60 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
61 | 0 | "%s: invalid data descriptor value already set.", |
62 | 0 | function ); |
63 | |
|
64 | 0 | return( -1 ); |
65 | 0 | } |
66 | 2.01k | *data_descriptor = memory_allocate_structure( |
67 | 2.01k | libwrc_data_descriptor_t ); |
68 | | |
69 | 2.01k | if( *data_descriptor == NULL ) |
70 | 0 | { |
71 | 0 | libcerror_error_set( |
72 | 0 | error, |
73 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
74 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
75 | 0 | "%s: unable to create data descriptor.", |
76 | 0 | function ); |
77 | |
|
78 | 0 | goto on_error; |
79 | 0 | } |
80 | 2.01k | if( memory_set( |
81 | 2.01k | *data_descriptor, |
82 | 2.01k | 0, |
83 | 2.01k | sizeof( libwrc_data_descriptor_t ) ) == NULL ) |
84 | 0 | { |
85 | 0 | libcerror_error_set( |
86 | 0 | error, |
87 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
88 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
89 | 0 | "%s: unable to clear data descriptor.", |
90 | 0 | function ); |
91 | |
|
92 | 0 | goto on_error; |
93 | 0 | } |
94 | 2.01k | return( 1 ); |
95 | | |
96 | 0 | on_error: |
97 | 0 | if( *data_descriptor != NULL ) |
98 | 0 | { |
99 | 0 | memory_free( |
100 | 0 | *data_descriptor ); |
101 | |
|
102 | 0 | *data_descriptor = NULL; |
103 | 0 | } |
104 | 0 | return( -1 ); |
105 | 2.01k | } |
106 | | |
107 | | /* Frees a data descriptor |
108 | | * Returns 1 if successful or -1 on error |
109 | | */ |
110 | | int libwrc_data_descriptor_free( |
111 | | libwrc_data_descriptor_t **data_descriptor, |
112 | | libcerror_error_t **error ) |
113 | 7.69k | { |
114 | 7.69k | static char *function = "libwrc_data_descriptor_free"; |
115 | | |
116 | 7.69k | if( data_descriptor == NULL ) |
117 | 0 | { |
118 | 0 | libcerror_error_set( |
119 | 0 | error, |
120 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
121 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
122 | 0 | "%s: invalid data descriptor.", |
123 | 0 | function ); |
124 | |
|
125 | 0 | return( -1 ); |
126 | 0 | } |
127 | 7.69k | if( *data_descriptor != NULL ) |
128 | 2.01k | { |
129 | 2.01k | memory_free( |
130 | 2.01k | *data_descriptor ); |
131 | | |
132 | 2.01k | *data_descriptor = NULL; |
133 | 2.01k | } |
134 | 7.69k | return( 1 ); |
135 | 7.69k | } |
136 | | |
137 | | /* Reads the data descriptor data |
138 | | * Returns 1 if successful or -1 on error |
139 | | */ |
140 | | int libwrc_data_descriptor_read_data( |
141 | | libwrc_data_descriptor_t *data_descriptor, |
142 | | const uint8_t *data, |
143 | | size_t data_size, |
144 | | libcerror_error_t **error ) |
145 | 1.86k | { |
146 | 1.86k | static char *function = "libwrc_data_descriptor_read_data"; |
147 | | |
148 | 1.86k | if( data_descriptor == NULL ) |
149 | 0 | { |
150 | 0 | libcerror_error_set( |
151 | 0 | error, |
152 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
153 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
154 | 0 | "%s: invalid data descriptor.", |
155 | 0 | function ); |
156 | |
|
157 | 0 | return( -1 ); |
158 | 0 | } |
159 | 1.86k | if( data == NULL ) |
160 | 0 | { |
161 | 0 | libcerror_error_set( |
162 | 0 | error, |
163 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
164 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
165 | 0 | "%s: invalid data.", |
166 | 0 | function ); |
167 | |
|
168 | 0 | return( -1 ); |
169 | 0 | } |
170 | 1.86k | if( ( data_size < 8 ) |
171 | 1.86k | || ( data_size > (size_t) SSIZE_MAX ) ) |
172 | 0 | { |
173 | 0 | libcerror_error_set( |
174 | 0 | error, |
175 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
176 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
177 | 0 | "%s: invalid data size value out of bounds.", |
178 | 0 | function ); |
179 | |
|
180 | 0 | return( -1 ); |
181 | 0 | } |
182 | | #if defined( HAVE_DEBUG_OUTPUT ) |
183 | | if( libcnotify_verbose != 0 ) |
184 | | { |
185 | | libcnotify_printf( |
186 | | "%s: data descriptor data:\n", |
187 | | function ); |
188 | | libcnotify_print_data( |
189 | | data, |
190 | | sizeof( wrc_data_descriptor_t ), |
191 | | 0 ); |
192 | | } |
193 | | #endif |
194 | 1.86k | byte_stream_copy_to_uint32_little_endian( |
195 | 1.86k | ( (wrc_data_descriptor_t *) data )->virtual_address, |
196 | 1.86k | data_descriptor->virtual_address ); |
197 | | |
198 | 1.86k | byte_stream_copy_to_uint32_little_endian( |
199 | 1.86k | ( (wrc_data_descriptor_t *) data )->size, |
200 | 1.86k | data_descriptor->size ); |
201 | | |
202 | | #if defined( HAVE_DEBUG_OUTPUT ) |
203 | | if( libcnotify_verbose != 0 ) |
204 | | { |
205 | | libcnotify_printf( |
206 | | "%s: virtual address\t\t\t: 0x%08" PRIx32 "\n", |
207 | | function, |
208 | | data_descriptor->virtual_address ); |
209 | | |
210 | | libcnotify_printf( |
211 | | "%s: size\t\t\t\t\t: %" PRIu32 "\n", |
212 | | function, |
213 | | data_descriptor->size ); |
214 | | |
215 | | libcnotify_printf( |
216 | | "\n" ); |
217 | | } |
218 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
219 | | |
220 | 1.86k | return( 1 ); |
221 | 1.86k | } |
222 | | |
223 | | /* Reads the data descriptor |
224 | | * Returns 1 if successful or -1 on error |
225 | | */ |
226 | | int libwrc_data_descriptor_read_file_io_handle( |
227 | | libwrc_data_descriptor_t *data_descriptor, |
228 | | libbfio_handle_t *file_io_handle, |
229 | | off64_t file_offset, |
230 | | libcerror_error_t **error ) |
231 | 2.01k | { |
232 | 2.01k | uint8_t data_descriptor_data[ sizeof( wrc_data_descriptor_t ) ]; |
233 | | |
234 | 2.01k | static char *function = "libwrc_data_descriptor_read_file_io_handle"; |
235 | 2.01k | ssize_t read_count = 0; |
236 | | |
237 | 2.01k | if( data_descriptor == NULL ) |
238 | 0 | { |
239 | 0 | libcerror_error_set( |
240 | 0 | error, |
241 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
242 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
243 | 0 | "%s: invalid data descriptor.", |
244 | 0 | function ); |
245 | |
|
246 | 0 | return( -1 ); |
247 | 0 | } |
248 | | #if defined( HAVE_DEBUG_OUTPUT ) |
249 | | if( libcnotify_verbose != 0 ) |
250 | | { |
251 | | libcnotify_printf( |
252 | | "%s: reading data descriptor at offset: %" PRIi64 " (0x%08" PRIx64 ")\n", |
253 | | function, |
254 | | file_offset, |
255 | | file_offset ); |
256 | | } |
257 | | #endif |
258 | 2.01k | read_count = libbfio_handle_read_buffer_at_offset( |
259 | 2.01k | file_io_handle, |
260 | 2.01k | data_descriptor_data, |
261 | 2.01k | sizeof( wrc_data_descriptor_t ), |
262 | 2.01k | file_offset, |
263 | 2.01k | error ); |
264 | | |
265 | 2.01k | if( read_count != (ssize_t) sizeof( wrc_data_descriptor_t ) ) |
266 | 158 | { |
267 | 158 | libcerror_error_set( |
268 | 158 | error, |
269 | 158 | LIBCERROR_ERROR_DOMAIN_IO, |
270 | 158 | LIBCERROR_IO_ERROR_READ_FAILED, |
271 | 158 | "%s: unable to read data descriptor data at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
272 | 158 | function, |
273 | 158 | file_offset, |
274 | 158 | file_offset ); |
275 | | |
276 | 158 | return( -1 ); |
277 | 158 | } |
278 | 1.86k | if( libwrc_data_descriptor_read_data( |
279 | 1.86k | data_descriptor, |
280 | 1.86k | data_descriptor_data, |
281 | 1.86k | sizeof( wrc_data_descriptor_t ), |
282 | 1.86k | error ) != 1 ) |
283 | 0 | { |
284 | 0 | libcerror_error_set( |
285 | 0 | error, |
286 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
287 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
288 | 0 | "%s: unable to read data descriptor.", |
289 | 0 | function ); |
290 | |
|
291 | 0 | return( -1 ); |
292 | 0 | } |
293 | 1.86k | return( 1 ); |
294 | 1.86k | } |
295 | | |