/src/libmodi/libmodi/libmodi_udif_resource_file.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Universal Disk Image Format (UDIF) resource file functions |
3 | | * |
4 | | * Copyright (C) 2012-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 "libmodi_io_handle.h" |
28 | | #include "libmodi_libbfio.h" |
29 | | #include "libmodi_libcerror.h" |
30 | | #include "libmodi_libcnotify.h" |
31 | | #include "libmodi_udif_resource_file.h" |
32 | | |
33 | | #include "modi_udif_resource_file.h" |
34 | | |
35 | | /* Creates a resource file |
36 | | * Make sure the value resource_file is referencing, is set to NULL |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | int libmodi_udif_resource_file_initialize( |
40 | | libmodi_udif_resource_file_t **resource_file, |
41 | | libcerror_error_t **error ) |
42 | 1.68k | { |
43 | 1.68k | static char *function = "libmodi_udif_resource_file_initialize"; |
44 | | |
45 | 1.68k | if( resource_file == NULL ) |
46 | 0 | { |
47 | 0 | libcerror_error_set( |
48 | 0 | error, |
49 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
50 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
51 | 0 | "%s: invalid resource file.", |
52 | 0 | function ); |
53 | |
|
54 | 0 | return( -1 ); |
55 | 0 | } |
56 | 1.68k | if( *resource_file != NULL ) |
57 | 0 | { |
58 | 0 | libcerror_error_set( |
59 | 0 | error, |
60 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
61 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
62 | 0 | "%s: invalid resource file value already set.", |
63 | 0 | function ); |
64 | |
|
65 | 0 | return( -1 ); |
66 | 0 | } |
67 | 1.68k | *resource_file = memory_allocate_structure( |
68 | 1.68k | libmodi_udif_resource_file_t ); |
69 | | |
70 | 1.68k | if( *resource_file == NULL ) |
71 | 0 | { |
72 | 0 | libcerror_error_set( |
73 | 0 | error, |
74 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
75 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
76 | 0 | "%s: unable to create resource file.", |
77 | 0 | function ); |
78 | |
|
79 | 0 | goto on_error; |
80 | 0 | } |
81 | 1.68k | if( memory_set( |
82 | 1.68k | *resource_file, |
83 | 1.68k | 0, |
84 | 1.68k | sizeof( libmodi_udif_resource_file_t ) ) == NULL ) |
85 | 0 | { |
86 | 0 | libcerror_error_set( |
87 | 0 | error, |
88 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
89 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
90 | 0 | "%s: unable to clear resource file.", |
91 | 0 | function ); |
92 | |
|
93 | 0 | goto on_error; |
94 | 0 | } |
95 | 1.68k | return( 1 ); |
96 | | |
97 | 0 | on_error: |
98 | 0 | if( *resource_file != NULL ) |
99 | 0 | { |
100 | 0 | memory_free( |
101 | 0 | *resource_file ); |
102 | |
|
103 | 0 | *resource_file = NULL; |
104 | 0 | } |
105 | 0 | return( -1 ); |
106 | 1.68k | } |
107 | | |
108 | | /* Frees a resource file |
109 | | * Returns 1 if successful or -1 on error |
110 | | */ |
111 | | int libmodi_udif_resource_file_free( |
112 | | libmodi_udif_resource_file_t **resource_file, |
113 | | libcerror_error_t **error ) |
114 | 1.68k | { |
115 | 1.68k | static char *function = "libmodi_udif_resource_file_free"; |
116 | | |
117 | 1.68k | if( resource_file == NULL ) |
118 | 0 | { |
119 | 0 | libcerror_error_set( |
120 | 0 | error, |
121 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
122 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
123 | 0 | "%s: invalid resource file.", |
124 | 0 | function ); |
125 | |
|
126 | 0 | return( -1 ); |
127 | 0 | } |
128 | 1.68k | if( *resource_file != NULL ) |
129 | 1.68k | { |
130 | 1.68k | memory_free( |
131 | 1.68k | *resource_file ); |
132 | | |
133 | 1.68k | *resource_file = NULL; |
134 | 1.68k | } |
135 | 1.68k | return( 1 ); |
136 | 1.68k | } |
137 | | |
138 | | /* Reads a resource file |
139 | | * Returns 1 if successful, 0 if the signature does not match or -1 on error |
140 | | */ |
141 | | int libmodi_udif_resource_file_read_data( |
142 | | libmodi_udif_resource_file_t *resource_file, |
143 | | const uint8_t *data, |
144 | | size_t data_size, |
145 | | libcerror_error_t **error ) |
146 | 1.68k | { |
147 | 1.68k | static char *function = "libmodi_udif_resource_file_read_data"; |
148 | 1.68k | uint32_t number_of_segments = 0; |
149 | | |
150 | | #if defined( HAVE_DEBUG_OUTPUT ) |
151 | | uint64_t value_64bit = 0; |
152 | | uint32_t value_32bit = 0; |
153 | | #endif |
154 | | |
155 | 1.68k | if( resource_file == NULL ) |
156 | 0 | { |
157 | 0 | libcerror_error_set( |
158 | 0 | error, |
159 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
160 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
161 | 0 | "%s: invalid resource file.", |
162 | 0 | function ); |
163 | |
|
164 | 0 | return( -1 ); |
165 | 0 | } |
166 | 1.68k | if( data == NULL ) |
167 | 0 | { |
168 | 0 | libcerror_error_set( |
169 | 0 | error, |
170 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
171 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
172 | 0 | "%s: invalid data.", |
173 | 0 | function ); |
174 | |
|
175 | 0 | return( -1 ); |
176 | 0 | } |
177 | 1.68k | if( ( data_size < sizeof( modi_udif_resource_file_t ) ) |
178 | 1.68k | || ( data_size > (size_t) SSIZE_MAX ) ) |
179 | 0 | { |
180 | 0 | libcerror_error_set( |
181 | 0 | error, |
182 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
183 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
184 | 0 | "%s: invalid data size value out of bounds.", |
185 | 0 | function ); |
186 | |
|
187 | 0 | return( -1 ); |
188 | 0 | } |
189 | | #if defined( HAVE_DEBUG_OUTPUT ) |
190 | | if( libcnotify_verbose != 0 ) |
191 | | { |
192 | | libcnotify_printf( |
193 | | "%s: resource file data:\n", |
194 | | function ); |
195 | | libcnotify_print_data( |
196 | | data, |
197 | | sizeof( modi_udif_resource_file_t ), |
198 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
199 | | } |
200 | | #endif |
201 | 1.68k | if( memory_compare( |
202 | 1.68k | ( (modi_udif_resource_file_t *) data )->signature, |
203 | 1.68k | modi_udif_resource_file_signature, |
204 | 1.68k | 4 ) != 0 ) |
205 | 478 | { |
206 | 478 | return( 0 ); |
207 | 478 | } |
208 | 1.20k | byte_stream_copy_to_uint64_big_endian( |
209 | 1.20k | ( (modi_udif_resource_file_t *) data )->data_fork_offset, |
210 | 1.20k | resource_file->data_fork_offset ); |
211 | | |
212 | 1.20k | byte_stream_copy_to_uint64_big_endian( |
213 | 1.20k | ( (modi_udif_resource_file_t *) data )->data_fork_size, |
214 | 1.20k | resource_file->data_fork_size ); |
215 | | |
216 | 1.20k | byte_stream_copy_to_uint64_big_endian( |
217 | 1.20k | ( (modi_udif_resource_file_t *) data )->resource_fork_offset, |
218 | 1.20k | resource_file->resource_fork_offset ); |
219 | | |
220 | 1.20k | byte_stream_copy_to_uint64_big_endian( |
221 | 1.20k | ( (modi_udif_resource_file_t *) data )->resource_fork_size, |
222 | 1.20k | resource_file->resource_fork_size ); |
223 | | |
224 | 1.20k | byte_stream_copy_to_uint32_big_endian( |
225 | 1.20k | ( (modi_udif_resource_file_t *) data )->number_of_segments, |
226 | 1.20k | number_of_segments ); |
227 | | |
228 | 1.20k | byte_stream_copy_to_uint64_big_endian( |
229 | 1.20k | ( (modi_udif_resource_file_t *) data )->xml_plist_offset, |
230 | 1.20k | resource_file->xml_plist_offset ); |
231 | | |
232 | 1.20k | byte_stream_copy_to_uint64_big_endian( |
233 | 1.20k | ( (modi_udif_resource_file_t *) data )->xml_plist_size, |
234 | 1.20k | resource_file->xml_plist_size ); |
235 | | |
236 | 1.20k | byte_stream_copy_to_uint32_big_endian( |
237 | 1.20k | ( (modi_udif_resource_file_t *) data )->number_of_sectors, |
238 | 1.20k | resource_file->number_of_sectors ); |
239 | | |
240 | | #if defined( HAVE_DEBUG_OUTPUT ) |
241 | | if( libcnotify_verbose != 0 ) |
242 | | { |
243 | | libcnotify_printf( |
244 | | "%s: signature\t\t\t\t: %c%c%c%c\n", |
245 | | function, |
246 | | ( (modi_udif_resource_file_t *) data )->signature[ 0 ], |
247 | | ( (modi_udif_resource_file_t *) data )->signature[ 1 ], |
248 | | ( (modi_udif_resource_file_t *) data )->signature[ 2 ], |
249 | | ( (modi_udif_resource_file_t *) data )->signature[ 3 ] ); |
250 | | |
251 | | byte_stream_copy_to_uint32_big_endian( |
252 | | ( (modi_udif_resource_file_t *) data )->format_version, |
253 | | value_32bit ); |
254 | | libcnotify_printf( |
255 | | "%s: format version\t\t\t: %" PRIu32 "\n", |
256 | | function, |
257 | | value_32bit ); |
258 | | |
259 | | byte_stream_copy_to_uint32_big_endian( |
260 | | ( (modi_udif_resource_file_t *) data )->size, |
261 | | value_32bit ); |
262 | | libcnotify_printf( |
263 | | "%s: size\t\t\t\t: %" PRIu32 "\n", |
264 | | function, |
265 | | value_32bit ); |
266 | | |
267 | | byte_stream_copy_to_uint32_big_endian( |
268 | | ( (modi_udif_resource_file_t *) data )->flags, |
269 | | value_32bit ); |
270 | | libcnotify_printf( |
271 | | "%s: flags\t\t\t\t: 0x%08" PRIx32 "\n", |
272 | | function, |
273 | | value_32bit ); |
274 | | |
275 | | byte_stream_copy_to_uint64_big_endian( |
276 | | ( (modi_udif_resource_file_t *) data )->unknown1, |
277 | | value_64bit ); |
278 | | libcnotify_printf( |
279 | | "%s: unknown1\t\t\t\t: 0x%08" PRIx64 "\n", |
280 | | function, |
281 | | value_64bit ); |
282 | | |
283 | | libcnotify_printf( |
284 | | "%s: data fork offset\t\t\t: 0x%08" PRIx64 "\n", |
285 | | function, |
286 | | resource_file->data_fork_offset ); |
287 | | |
288 | | libcnotify_printf( |
289 | | "%s: data fork size\t\t\t: %" PRIu64 "\n", |
290 | | function, |
291 | | resource_file->data_fork_size ); |
292 | | |
293 | | libcnotify_printf( |
294 | | "%s: resource fork offset\t\t: 0x%08" PRIx64 "\n", |
295 | | function, |
296 | | resource_file->resource_fork_offset ); |
297 | | |
298 | | libcnotify_printf( |
299 | | "%s: resource fork size\t\t: %" PRIu64 "\n", |
300 | | function, |
301 | | resource_file->resource_fork_size ); |
302 | | |
303 | | byte_stream_copy_to_uint32_big_endian( |
304 | | ( (modi_udif_resource_file_t *) data )->unknown2, |
305 | | value_32bit ); |
306 | | libcnotify_printf( |
307 | | "%s: unknown2\t\t\t\t: 0x%08" PRIx32 "\n", |
308 | | function, |
309 | | value_32bit ); |
310 | | |
311 | | libcnotify_printf( |
312 | | "%s: number of segments\t\t: %" PRIu32 "\n", |
313 | | function, |
314 | | number_of_segments ); |
315 | | |
316 | | libcnotify_printf( |
317 | | "%s: segment identifier:\n", |
318 | | function ); |
319 | | libcnotify_print_data( |
320 | | ( (modi_udif_resource_file_t *) data )->segment_identifier, |
321 | | 16, |
322 | | 0 ); |
323 | | |
324 | | byte_stream_copy_to_uint32_big_endian( |
325 | | ( (modi_udif_resource_file_t *) data )->data_checksum_type, |
326 | | value_32bit ); |
327 | | libcnotify_printf( |
328 | | "%s: data checksum type\t\t: %" PRIu32 "\n", |
329 | | function, |
330 | | value_32bit ); |
331 | | |
332 | | byte_stream_copy_to_uint32_big_endian( |
333 | | ( (modi_udif_resource_file_t *) data )->data_checksum_size, |
334 | | value_32bit ); |
335 | | libcnotify_printf( |
336 | | "%s: data checksum size\t\t: %" PRIu32 "\n", |
337 | | function, |
338 | | value_32bit ); |
339 | | |
340 | | libcnotify_printf( |
341 | | "%s: data checksum:\n", |
342 | | function ); |
343 | | libcnotify_print_data( |
344 | | ( (modi_udif_resource_file_t *) data )->data_checksum, |
345 | | 128, |
346 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
347 | | |
348 | | libcnotify_printf( |
349 | | "%s: XML plist offset\t\t\t: 0x%08" PRIx64 "\n", |
350 | | function, |
351 | | resource_file->xml_plist_offset ); |
352 | | |
353 | | libcnotify_printf( |
354 | | "%s: XML plist size\t\t\t: %" PRIu64 "\n", |
355 | | function, |
356 | | resource_file->xml_plist_size ); |
357 | | |
358 | | libcnotify_printf( |
359 | | "%s: unknown3:\n", |
360 | | function ); |
361 | | libcnotify_print_data( |
362 | | ( (modi_udif_resource_file_t *) data )->unknown3, |
363 | | 120, |
364 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
365 | | |
366 | | byte_stream_copy_to_uint32_big_endian( |
367 | | ( (modi_udif_resource_file_t *) data )->master_checksum_type, |
368 | | value_32bit ); |
369 | | libcnotify_printf( |
370 | | "%s: master checksum type\t\t: %" PRIu32 "\n", |
371 | | function, |
372 | | value_32bit ); |
373 | | |
374 | | byte_stream_copy_to_uint32_big_endian( |
375 | | ( (modi_udif_resource_file_t *) data )->master_checksum_size, |
376 | | value_32bit ); |
377 | | libcnotify_printf( |
378 | | "%s: master checksum size\t\t: %" PRIu32 "\n", |
379 | | function, |
380 | | value_32bit ); |
381 | | |
382 | | libcnotify_printf( |
383 | | "%s: master checksum data:\n", |
384 | | function ); |
385 | | libcnotify_print_data( |
386 | | ( (modi_udif_resource_file_t *) data )->master_checksum, |
387 | | 128, |
388 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
389 | | |
390 | | byte_stream_copy_to_uint32_big_endian( |
391 | | ( (modi_udif_resource_file_t *) data )->image_type, |
392 | | value_32bit ); |
393 | | libcnotify_printf( |
394 | | "%s: image type\t\t\t: %" PRIu32 "\n", |
395 | | function, |
396 | | value_32bit ); |
397 | | |
398 | | libcnotify_printf( |
399 | | "%s: number of sectors\t\t\t: %" PRIu32 "\n", |
400 | | function, |
401 | | resource_file->number_of_sectors ); |
402 | | |
403 | | byte_stream_copy_to_uint32_big_endian( |
404 | | ( (modi_udif_resource_file_t *) data )->unknown5, |
405 | | value_32bit ); |
406 | | libcnotify_printf( |
407 | | "%s: unknown5\t\t\t\t: 0x%08" PRIx32 "\n", |
408 | | function, |
409 | | value_32bit ); |
410 | | |
411 | | byte_stream_copy_to_uint32_big_endian( |
412 | | ( (modi_udif_resource_file_t *) data )->unknown6, |
413 | | value_32bit ); |
414 | | libcnotify_printf( |
415 | | "%s: unknown6\t\t\t\t: 0x%08" PRIx32 "\n", |
416 | | function, |
417 | | value_32bit ); |
418 | | |
419 | | byte_stream_copy_to_uint32_big_endian( |
420 | | ( (modi_udif_resource_file_t *) data )->unknown7, |
421 | | value_32bit ); |
422 | | libcnotify_printf( |
423 | | "%s: unknown7\t\t\t\t: 0x%08" PRIx32 "\n", |
424 | | function, |
425 | | value_32bit ); |
426 | | |
427 | | libcnotify_printf( |
428 | | "\n" ); |
429 | | } |
430 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
431 | | |
432 | 1.20k | if( number_of_segments > 1 ) |
433 | 41 | { |
434 | 41 | libcerror_error_set( |
435 | 41 | error, |
436 | 41 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
437 | 41 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
438 | 41 | "%s: unsupported number of segments.", |
439 | 41 | function ); |
440 | | |
441 | 41 | return( -1 ); |
442 | 41 | } |
443 | 1.16k | return( 1 ); |
444 | 1.20k | } |
445 | | |
446 | | /* Reads a resource file |
447 | | * Returns 1 if successful, 0 if the signature does not match or -1 on error |
448 | | */ |
449 | | int libmodi_udif_resource_file_read_file_io_handle( |
450 | | libmodi_udif_resource_file_t *resource_file, |
451 | | libbfio_handle_t *file_io_handle, |
452 | | off64_t offset, |
453 | | libcerror_error_t **error ) |
454 | 1.68k | { |
455 | 1.68k | uint8_t *resource_file_data = NULL; |
456 | 1.68k | static char *function = "libmodi_udif_resource_file_read_file_io_handle"; |
457 | 1.68k | size_t read_size = 512; |
458 | 1.68k | ssize_t read_count = 0; |
459 | 1.68k | int result = 0; |
460 | | |
461 | 1.68k | if( resource_file == NULL ) |
462 | 0 | { |
463 | 0 | libcerror_error_set( |
464 | 0 | error, |
465 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
466 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
467 | 0 | "%s: invalid resource file.", |
468 | 0 | function ); |
469 | |
|
470 | 0 | return( -1 ); |
471 | 0 | } |
472 | 1.68k | resource_file_data = (uint8_t *) memory_allocate( |
473 | 1.68k | read_size ); |
474 | | |
475 | 1.68k | if( resource_file_data == NULL ) |
476 | 0 | { |
477 | 0 | libcerror_error_set( |
478 | 0 | error, |
479 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
480 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
481 | 0 | "%s: unable to create resource file data.", |
482 | 0 | function ); |
483 | |
|
484 | 0 | goto on_error; |
485 | 0 | } |
486 | | #if defined( HAVE_DEBUG_OUTPUT ) |
487 | | if( libcnotify_verbose != 0 ) |
488 | | { |
489 | | libcnotify_printf( |
490 | | "%s: reading resource file at offset: %" PRIi64 " (0x%08" PRIx64 ").\n", |
491 | | function, |
492 | | offset, |
493 | | offset ); |
494 | | } |
495 | | #endif |
496 | 1.68k | read_count = libbfio_handle_read_buffer_at_offset( |
497 | 1.68k | file_io_handle, |
498 | 1.68k | resource_file_data, |
499 | 1.68k | read_size, |
500 | 1.68k | offset, |
501 | 1.68k | error ); |
502 | | |
503 | 1.68k | if( read_count != (ssize_t) read_size ) |
504 | 0 | { |
505 | 0 | libcerror_error_set( |
506 | 0 | error, |
507 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
508 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
509 | 0 | "%s: unable to read resource file data at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
510 | 0 | function, |
511 | 0 | offset, |
512 | 0 | offset ); |
513 | |
|
514 | 0 | goto on_error; |
515 | 0 | } |
516 | 1.68k | result = libmodi_udif_resource_file_read_data( |
517 | 1.68k | resource_file, |
518 | 1.68k | resource_file_data, |
519 | 1.68k | read_size, |
520 | 1.68k | error ); |
521 | | |
522 | 1.68k | if( result == -1 ) |
523 | 41 | { |
524 | 41 | libcerror_error_set( |
525 | 41 | error, |
526 | 41 | LIBCERROR_ERROR_DOMAIN_IO, |
527 | 41 | LIBCERROR_IO_ERROR_READ_FAILED, |
528 | 41 | "%s: unable to read resource file.", |
529 | 41 | function ); |
530 | | |
531 | 41 | goto on_error; |
532 | 41 | } |
533 | 1.64k | memory_free( |
534 | 1.64k | resource_file_data ); |
535 | | |
536 | 1.64k | resource_file_data = NULL; |
537 | | |
538 | 1.64k | return( result ); |
539 | | |
540 | 41 | on_error: |
541 | 41 | if( resource_file_data != NULL ) |
542 | 41 | { |
543 | 41 | memory_free( |
544 | 41 | resource_file_data ); |
545 | 41 | } |
546 | 41 | return( -1 ); |
547 | 1.68k | } |
548 | | |