/src/libwrc/libwrc/libwrc_resource_node_header.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Resource node header 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_libbfio.h" |
28 | | #include "libwrc_libcerror.h" |
29 | | #include "libwrc_libcnotify.h" |
30 | | #include "libwrc_resource_node_header.h" |
31 | | |
32 | | #include "wrc_resource_node.h" |
33 | | |
34 | | /* Creates a table entry |
35 | | * Make sure the value resource_node_header is referencing, is set to NULL |
36 | | * Returns 1 if successful or -1 on error |
37 | | */ |
38 | | int libwrc_resource_node_header_initialize( |
39 | | libwrc_resource_node_header_t **resource_node_header, |
40 | | libcerror_error_t **error ) |
41 | 4.26k | { |
42 | 4.26k | static char *function = "libwrc_resource_node_header_initialize"; |
43 | | |
44 | 4.26k | if( resource_node_header == 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 table entry.", |
51 | 0 | function ); |
52 | |
|
53 | 0 | return( -1 ); |
54 | 0 | } |
55 | 4.26k | if( *resource_node_header != 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 table entry value already set.", |
62 | 0 | function ); |
63 | |
|
64 | 0 | return( -1 ); |
65 | 0 | } |
66 | 4.26k | *resource_node_header = memory_allocate_structure( |
67 | 4.26k | libwrc_resource_node_header_t ); |
68 | | |
69 | 4.26k | if( *resource_node_header == 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 table entry.", |
76 | 0 | function ); |
77 | |
|
78 | 0 | goto on_error; |
79 | 0 | } |
80 | 4.26k | if( memory_set( |
81 | 4.26k | *resource_node_header, |
82 | 4.26k | 0, |
83 | 4.26k | sizeof( libwrc_resource_node_header_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 table entry.", |
90 | 0 | function ); |
91 | |
|
92 | 0 | goto on_error; |
93 | 0 | } |
94 | 4.26k | return( 1 ); |
95 | | |
96 | 0 | on_error: |
97 | 0 | if( *resource_node_header != NULL ) |
98 | 0 | { |
99 | 0 | memory_free( |
100 | 0 | *resource_node_header ); |
101 | |
|
102 | 0 | *resource_node_header = NULL; |
103 | 0 | } |
104 | 0 | return( -1 ); |
105 | 4.26k | } |
106 | | |
107 | | /* Frees a table entry |
108 | | * Returns 1 if successful or -1 on error |
109 | | */ |
110 | | int libwrc_resource_node_header_free( |
111 | | libwrc_resource_node_header_t **resource_node_header, |
112 | | libcerror_error_t **error ) |
113 | 4.26k | { |
114 | 4.26k | static char *function = "libwrc_resource_node_header_free"; |
115 | | |
116 | 4.26k | if( resource_node_header == 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 table entry.", |
123 | 0 | function ); |
124 | |
|
125 | 0 | return( -1 ); |
126 | 0 | } |
127 | 4.26k | if( *resource_node_header != NULL ) |
128 | 4.26k | { |
129 | 4.26k | memory_free( |
130 | 4.26k | *resource_node_header ); |
131 | | |
132 | 4.26k | *resource_node_header = NULL; |
133 | 4.26k | } |
134 | 4.26k | return( 1 ); |
135 | 4.26k | } |
136 | | |
137 | | /* Reads the resource node header data |
138 | | * Returns 1 if successful or -1 on error |
139 | | */ |
140 | | int libwrc_resource_node_header_read_data( |
141 | | libwrc_resource_node_header_t *resource_node_header, |
142 | | const uint8_t *data, |
143 | | size_t data_size, |
144 | | libcerror_error_t **error ) |
145 | 4.20k | { |
146 | 4.20k | static char *function = "libwrc_resource_node_header_read_data"; |
147 | | |
148 | | #if defined( HAVE_DEBUG_OUTPUT ) |
149 | | uint32_t value_32bit = 0; |
150 | | uint16_t value_16bit = 0; |
151 | | #endif |
152 | | |
153 | 4.20k | if( resource_node_header == NULL ) |
154 | 0 | { |
155 | 0 | libcerror_error_set( |
156 | 0 | error, |
157 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
158 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
159 | 0 | "%s: invalid resource node header.", |
160 | 0 | function ); |
161 | |
|
162 | 0 | return( -1 ); |
163 | 0 | } |
164 | 4.20k | if( data == NULL ) |
165 | 0 | { |
166 | 0 | libcerror_error_set( |
167 | 0 | error, |
168 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
169 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
170 | 0 | "%s: invalid data.", |
171 | 0 | function ); |
172 | |
|
173 | 0 | return( -1 ); |
174 | 0 | } |
175 | 4.20k | if( ( data_size < sizeof( wrc_resource_node_header_t ) ) |
176 | 4.20k | || ( data_size > (size_t) SSIZE_MAX ) ) |
177 | 0 | { |
178 | 0 | libcerror_error_set( |
179 | 0 | error, |
180 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
181 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
182 | 0 | "%s: invalid data size value out of bounds.", |
183 | 0 | function ); |
184 | |
|
185 | 0 | return( -1 ); |
186 | 0 | } |
187 | | #if defined( HAVE_DEBUG_OUTPUT ) |
188 | | if( libcnotify_verbose != 0 ) |
189 | | { |
190 | | libcnotify_printf( |
191 | | "%s: resource node header data:\n", |
192 | | function ); |
193 | | libcnotify_print_data( |
194 | | data, |
195 | | sizeof( wrc_resource_node_header_t ), |
196 | | 0 ); |
197 | | } |
198 | | #endif |
199 | 4.20k | byte_stream_copy_to_uint32_little_endian( |
200 | 4.20k | ( (wrc_resource_node_header_t *) data )->flags, |
201 | 4.20k | resource_node_header->flags ); |
202 | | |
203 | 4.20k | byte_stream_copy_to_uint16_little_endian( |
204 | 4.20k | ( (wrc_resource_node_header_t *) data )->number_of_named_entries, |
205 | 4.20k | resource_node_header->number_of_named_entries ); |
206 | | |
207 | 4.20k | byte_stream_copy_to_uint16_little_endian( |
208 | 4.20k | ( (wrc_resource_node_header_t *) data )->number_of_unnamed_entries, |
209 | 4.20k | resource_node_header->number_of_unnamed_entries ); |
210 | | |
211 | | #if defined( HAVE_DEBUG_OUTPUT ) |
212 | | if( libcnotify_verbose != 0 ) |
213 | | { |
214 | | libcnotify_printf( |
215 | | "%s: flags\t\t\t\t: 0x%08" PRIx32 "\n", |
216 | | function, |
217 | | resource_node_header->flags ); |
218 | | |
219 | | /* TODO print meaning of flags */ |
220 | | |
221 | | byte_stream_copy_to_uint32_little_endian( |
222 | | ( (wrc_resource_node_header_t *) data )->flags, |
223 | | value_32bit ); |
224 | | libcnotify_printf( |
225 | | "%s: creation time\t\t\t: 0x%08" PRIx32 "\n", |
226 | | function, |
227 | | value_32bit ); |
228 | | |
229 | | byte_stream_copy_to_uint16_little_endian( |
230 | | ( (wrc_resource_node_header_t *) data )->major_version, |
231 | | value_16bit ); |
232 | | libcnotify_printf( |
233 | | "%s: major version\t\t\t: %" PRIu16 "\n", |
234 | | function, |
235 | | value_16bit ); |
236 | | |
237 | | byte_stream_copy_to_uint16_little_endian( |
238 | | ( (wrc_resource_node_header_t *) data )->minor_version, |
239 | | value_16bit ); |
240 | | libcnotify_printf( |
241 | | "%s: minor version\t\t\t: %" PRIu16 "\n", |
242 | | function, |
243 | | value_16bit ); |
244 | | |
245 | | libcnotify_printf( |
246 | | "%s: number of named entries\t\t: %" PRIu16 "\n", |
247 | | function, |
248 | | resource_node_header->number_of_named_entries ); |
249 | | |
250 | | libcnotify_printf( |
251 | | "%s: number of unnamed entries\t: %" PRIu16 "\n", |
252 | | function, |
253 | | resource_node_header->number_of_unnamed_entries ); |
254 | | |
255 | | libcnotify_printf( |
256 | | "\n" ); |
257 | | } |
258 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
259 | | |
260 | 4.20k | if( resource_node_header->flags != 0 ) |
261 | 52 | { |
262 | 52 | libcerror_error_set( |
263 | 52 | error, |
264 | 52 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
265 | 52 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
266 | 52 | "%s: unsupported flags: 0x%08" PRIx32 ".", |
267 | 52 | function, |
268 | 52 | resource_node_header->flags ); |
269 | | |
270 | 52 | return( -1 ); |
271 | 52 | } |
272 | 4.15k | return( 1 ); |
273 | 4.20k | } |
274 | | |
275 | | /* Reads the resource node header |
276 | | * Returns 1 if successful or -1 on error |
277 | | */ |
278 | | int libwrc_resource_node_header_read_file_io_handle( |
279 | | libwrc_resource_node_header_t *resource_node_header, |
280 | | libbfio_handle_t *file_io_handle, |
281 | | off64_t file_offset, |
282 | | libcerror_error_t **error ) |
283 | 4.26k | { |
284 | 4.26k | uint8_t resource_node_header_data[ sizeof( wrc_resource_node_header_t ) ]; |
285 | | |
286 | 4.26k | static char *function = "libwrc_resource_node_header_read_file_io_handle"; |
287 | 4.26k | ssize_t read_count = 0; |
288 | | |
289 | 4.26k | if( resource_node_header == NULL ) |
290 | 0 | { |
291 | 0 | libcerror_error_set( |
292 | 0 | error, |
293 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
294 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
295 | 0 | "%s: invalid resource node header.", |
296 | 0 | function ); |
297 | |
|
298 | 0 | return( -1 ); |
299 | 0 | } |
300 | | #if defined( HAVE_DEBUG_OUTPUT ) |
301 | | if( libcnotify_verbose != 0 ) |
302 | | { |
303 | | libcnotify_printf( |
304 | | "%s: reading resource node header at offset: %" PRIi64 " (0x%08" PRIx64 ")\n", |
305 | | function, |
306 | | file_offset, |
307 | | file_offset ); |
308 | | } |
309 | | #endif |
310 | 4.26k | read_count = libbfio_handle_read_buffer_at_offset( |
311 | 4.26k | file_io_handle, |
312 | 4.26k | resource_node_header_data, |
313 | 4.26k | sizeof( wrc_resource_node_header_t ), |
314 | 4.26k | file_offset, |
315 | 4.26k | error ); |
316 | | |
317 | 4.26k | if( read_count != (ssize_t) sizeof( wrc_resource_node_header_t ) ) |
318 | 56 | { |
319 | 56 | libcerror_error_set( |
320 | 56 | error, |
321 | 56 | LIBCERROR_ERROR_DOMAIN_IO, |
322 | 56 | LIBCERROR_IO_ERROR_READ_FAILED, |
323 | 56 | "%s: unable to read resource node header data at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
324 | 56 | function, |
325 | 56 | file_offset, |
326 | 56 | file_offset ); |
327 | | |
328 | 56 | return( -1 ); |
329 | 56 | } |
330 | 4.20k | if( libwrc_resource_node_header_read_data( |
331 | 4.20k | resource_node_header, |
332 | 4.20k | resource_node_header_data, |
333 | 4.20k | sizeof( wrc_resource_node_header_t ), |
334 | 4.20k | error ) != 1 ) |
335 | 52 | { |
336 | 52 | libcerror_error_set( |
337 | 52 | error, |
338 | 52 | LIBCERROR_ERROR_DOMAIN_IO, |
339 | 52 | LIBCERROR_IO_ERROR_READ_FAILED, |
340 | 52 | "%s: unable to read resource node header.", |
341 | 52 | function ); |
342 | | |
343 | 52 | return( -1 ); |
344 | 52 | } |
345 | 4.15k | return( 1 ); |
346 | 4.20k | } |
347 | | |