/src/libcreg/libcreg/libcreg_data_block.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Data block functions |
3 | | * |
4 | | * Copyright (C) 2013-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 "libcreg_data_block.h" |
28 | | #include "libcreg_key_name_entry.h" |
29 | | #include "libcreg_libbfio.h" |
30 | | #include "libcreg_libcdata.h" |
31 | | #include "libcreg_libcerror.h" |
32 | | #include "libcreg_libcnotify.h" |
33 | | #include "libcreg_unused.h" |
34 | | |
35 | | #include "creg_data_block.h" |
36 | | |
37 | | const char *creg_data_block_signature = "RGDB"; |
38 | | |
39 | | /* Creates a data block |
40 | | * Make sure the value data_block is referencing, is set to NULL |
41 | | * Returns 1 if successful or -1 on error |
42 | | */ |
43 | | int libcreg_data_block_initialize( |
44 | | libcreg_data_block_t **data_block, |
45 | | libcerror_error_t **error ) |
46 | 2.96M | { |
47 | 2.96M | static char *function = "libcreg_data_block_initialize"; |
48 | | |
49 | 2.96M | if( data_block == NULL ) |
50 | 0 | { |
51 | 0 | libcerror_error_set( |
52 | 0 | error, |
53 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
54 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
55 | 0 | "%s: invalid data block.", |
56 | 0 | function ); |
57 | |
|
58 | 0 | return( -1 ); |
59 | 0 | } |
60 | 2.96M | if( *data_block != NULL ) |
61 | 0 | { |
62 | 0 | libcerror_error_set( |
63 | 0 | error, |
64 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
65 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
66 | 0 | "%s: invalid data block value already set.", |
67 | 0 | function ); |
68 | |
|
69 | 0 | return( -1 ); |
70 | 0 | } |
71 | 2.96M | *data_block = memory_allocate_structure( |
72 | 2.96M | libcreg_data_block_t ); |
73 | | |
74 | 2.96M | if( *data_block == NULL ) |
75 | 0 | { |
76 | 0 | libcerror_error_set( |
77 | 0 | error, |
78 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
79 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
80 | 0 | "%s: unable to create data block.", |
81 | 0 | function ); |
82 | |
|
83 | 0 | goto on_error; |
84 | 0 | } |
85 | 2.96M | if( memory_set( |
86 | 2.96M | *data_block, |
87 | 2.96M | 0, |
88 | 2.96M | sizeof( libcreg_data_block_t ) ) == NULL ) |
89 | 0 | { |
90 | 0 | libcerror_error_set( |
91 | 0 | error, |
92 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
93 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
94 | 0 | "%s: unable to clear data block.", |
95 | 0 | function ); |
96 | |
|
97 | 0 | memory_free( |
98 | 0 | *data_block ); |
99 | |
|
100 | 0 | *data_block = NULL; |
101 | |
|
102 | 0 | return( -1 ); |
103 | 0 | } |
104 | 2.96M | if( libcdata_array_initialize( |
105 | 2.96M | &( ( *data_block )->entries_array ), |
106 | 2.96M | 0, |
107 | 2.96M | error ) != 1 ) |
108 | 0 | { |
109 | 0 | libcerror_error_set( |
110 | 0 | error, |
111 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
112 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
113 | 0 | "%s: unable to create entries array.", |
114 | 0 | function ); |
115 | |
|
116 | 0 | goto on_error; |
117 | 0 | } |
118 | 2.96M | return( 1 ); |
119 | | |
120 | 0 | on_error: |
121 | 0 | if( *data_block != NULL ) |
122 | 0 | { |
123 | 0 | memory_free( |
124 | 0 | *data_block ); |
125 | |
|
126 | 0 | *data_block = NULL; |
127 | 0 | } |
128 | 0 | return( -1 ); |
129 | 2.96M | } |
130 | | |
131 | | /* Frees a data block |
132 | | * Returns 1 if successful or -1 on error |
133 | | */ |
134 | | int libcreg_data_block_free( |
135 | | libcreg_data_block_t **data_block, |
136 | | libcerror_error_t **error ) |
137 | 2.96M | { |
138 | 2.96M | static char *function = "libcreg_data_block_free"; |
139 | 2.96M | int result = 1; |
140 | | |
141 | 2.96M | if( data_block == 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 data block.", |
148 | 0 | function ); |
149 | |
|
150 | 0 | return( -1 ); |
151 | 0 | } |
152 | 2.96M | if( *data_block != NULL ) |
153 | 2.96M | { |
154 | 2.96M | if( libcdata_array_free( |
155 | 2.96M | &( ( *data_block )->entries_array ), |
156 | 2.96M | (int (*)(intptr_t **, libcerror_error_t **)) &libcreg_key_name_entry_free, |
157 | 2.96M | error ) != 1 ) |
158 | 0 | { |
159 | 0 | libcerror_error_set( |
160 | 0 | error, |
161 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
162 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
163 | 0 | "%s: unable to free the entries array.", |
164 | 0 | function ); |
165 | |
|
166 | 0 | result = -1; |
167 | 0 | } |
168 | 2.96M | if( ( *data_block )->data != NULL ) |
169 | 113 | { |
170 | 113 | memory_free( |
171 | 113 | ( *data_block )->data ); |
172 | 113 | } |
173 | 2.96M | memory_free( |
174 | 2.96M | *data_block ); |
175 | | |
176 | 2.96M | *data_block = NULL; |
177 | 2.96M | } |
178 | 2.96M | return( result ); |
179 | 2.96M | } |
180 | | |
181 | | /* Reads a data block header |
182 | | * Returns 1 if successful, 0 if no data block signature was found or -1 on error |
183 | | */ |
184 | | int libcreg_data_block_read_header( |
185 | | libcreg_data_block_t *data_block, |
186 | | libbfio_handle_t *file_io_handle, |
187 | | off64_t file_offset, |
188 | | libcerror_error_t **error ) |
189 | 2.96M | { |
190 | 2.96M | creg_data_block_header_t data_block_header; |
191 | | |
192 | 2.96M | static char *function = "libcreg_data_block_read_header"; |
193 | 2.96M | ssize_t read_count = 0; |
194 | | |
195 | | #if defined( HAVE_DEBUG_OUTPUT ) |
196 | | uint16_t value_16bit = 0; |
197 | | #endif |
198 | | |
199 | 2.96M | if( data_block == NULL ) |
200 | 0 | { |
201 | 0 | libcerror_error_set( |
202 | 0 | error, |
203 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
204 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
205 | 0 | "%s: invalid data block.", |
206 | 0 | function ); |
207 | |
|
208 | 0 | return( -1 ); |
209 | 0 | } |
210 | 2.96M | data_block->offset = file_offset; |
211 | | |
212 | | #if defined( HAVE_DEBUG_OUTPUT ) |
213 | | if( libcnotify_verbose != 0 ) |
214 | | { |
215 | | libcnotify_printf( |
216 | | "%s: reading data block header at offset: %" PRIi64 " (0x%08" PRIx64 ")\n", |
217 | | function, |
218 | | file_offset, |
219 | | file_offset ); |
220 | | } |
221 | | #endif |
222 | 2.96M | read_count = libbfio_handle_read_buffer_at_offset( |
223 | 2.96M | file_io_handle, |
224 | 2.96M | (uint8_t *) &data_block_header, |
225 | 2.96M | sizeof( creg_data_block_header_t ), |
226 | 2.96M | file_offset, |
227 | 2.96M | error ); |
228 | | |
229 | 2.96M | if( read_count != (ssize_t) sizeof( creg_data_block_header_t ) ) |
230 | 244 | { |
231 | 244 | libcerror_error_set( |
232 | 244 | error, |
233 | 244 | LIBCERROR_ERROR_DOMAIN_IO, |
234 | 244 | LIBCERROR_IO_ERROR_READ_FAILED, |
235 | 244 | "%s: unable to read data block header data at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
236 | 244 | function, |
237 | 244 | file_offset, |
238 | 244 | file_offset ); |
239 | | |
240 | 244 | return( -1 ); |
241 | 244 | } |
242 | | #if defined( HAVE_DEBUG_OUTPUT ) |
243 | | if( libcnotify_verbose != 0 ) |
244 | | { |
245 | | libcnotify_printf( |
246 | | "%s: data block header:\n", |
247 | | function ); |
248 | | libcnotify_print_data( |
249 | | (uint8_t *) &data_block_header, |
250 | | sizeof( creg_data_block_header_t ), |
251 | | 0 ); |
252 | | } |
253 | | #endif |
254 | 2.96M | if( memory_compare( |
255 | 2.96M | data_block_header.signature, |
256 | 2.96M | creg_data_block_signature, |
257 | 2.96M | 4 ) != 0 ) |
258 | 223 | { |
259 | 223 | return( 0 ); |
260 | 223 | } |
261 | 2.96M | byte_stream_copy_to_uint32_little_endian( |
262 | 2.96M | data_block_header.size, |
263 | 2.96M | data_block->size ); |
264 | | |
265 | 2.96M | byte_stream_copy_to_uint32_little_endian( |
266 | 2.96M | data_block_header.unused_size, |
267 | 2.96M | data_block->unused_size ); |
268 | | |
269 | 2.96M | byte_stream_copy_to_uint32_little_endian( |
270 | 2.96M | data_block_header.used_size, |
271 | 2.96M | data_block->used_size ); |
272 | | |
273 | | #if defined( HAVE_DEBUG_OUTPUT ) |
274 | | if( libcnotify_verbose != 0 ) |
275 | | { |
276 | | libcnotify_printf( |
277 | | "%s: signature\t\t\t\t: %c%c%c%c\n", |
278 | | function, |
279 | | data_block_header.signature[ 0 ], |
280 | | data_block_header.signature[ 1 ], |
281 | | data_block_header.signature[ 2 ], |
282 | | data_block_header.signature[ 3 ] ); |
283 | | |
284 | | libcnotify_printf( |
285 | | "%s: size\t\t\t\t\t: %" PRIu32 "\n", |
286 | | function, |
287 | | data_block->size ); |
288 | | |
289 | | libcnotify_printf( |
290 | | "%s: unused size\t\t\t\t: %" PRIu32 "\n", |
291 | | function, |
292 | | data_block->unused_size ); |
293 | | |
294 | | byte_stream_copy_to_uint16_little_endian( |
295 | | data_block_header.unknown1, |
296 | | value_16bit ); |
297 | | libcnotify_printf( |
298 | | "%s: unknown1\t\t\t\t: 0x%04" PRIx16 "\n", |
299 | | function, |
300 | | value_16bit ); |
301 | | |
302 | | byte_stream_copy_to_uint16_little_endian( |
303 | | data_block_header.index, |
304 | | value_16bit ); |
305 | | libcnotify_printf( |
306 | | "%s: index\t\t\t\t\t: %" PRIu16 "\n", |
307 | | function, |
308 | | value_16bit ); |
309 | | |
310 | | libcnotify_printf( |
311 | | "%s: used size\t\t\t\t: %" PRIi32 "\n", |
312 | | function, |
313 | | (int32_t) data_block->used_size ); |
314 | | |
315 | | byte_stream_copy_to_uint16_little_endian( |
316 | | data_block_header.unknown2, |
317 | | value_16bit ); |
318 | | libcnotify_printf( |
319 | | "%s: unknown2\t\t\t\t: %" PRIu16 "\n", |
320 | | function, |
321 | | value_16bit ); |
322 | | |
323 | | byte_stream_copy_to_uint16_little_endian( |
324 | | data_block_header.unknown3, |
325 | | value_16bit ); |
326 | | libcnotify_printf( |
327 | | "%s: unknown3\t\t\t\t: %" PRIu16 "\n", |
328 | | function, |
329 | | value_16bit ); |
330 | | |
331 | | libcnotify_printf( |
332 | | "%s: unknown3:\n", |
333 | | function ); |
334 | | libcnotify_print_data( |
335 | | data_block_header.unknown4, |
336 | | 8, |
337 | | 0 ); |
338 | | } |
339 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
340 | | |
341 | | /* TODO check if unused_size + used_size == size */ |
342 | 2.96M | return( 1 ); |
343 | 2.96M | } |
344 | | |
345 | | /* Reads a data block and determines its entries |
346 | | * Returns 1 if successful or -1 on error |
347 | | */ |
348 | | int libcreg_data_block_read_entries( |
349 | | libcreg_data_block_t *data_block, |
350 | | libbfio_handle_t *file_io_handle, |
351 | | int ascii_codepage, |
352 | | libcerror_error_t **error ) |
353 | 985 | { |
354 | 985 | libcreg_key_name_entry_t *key_name_entry = NULL; |
355 | 985 | static char *function = "libcreg_data_block_read_entries"; |
356 | 985 | size_t data_offset = 0; |
357 | 985 | size_t key_name_entry_size = 0; |
358 | 985 | ssize_t read_count = 0; |
359 | 985 | int entry_index = 0; |
360 | 985 | int result = 0; |
361 | | |
362 | 985 | if( data_block == NULL ) |
363 | 0 | { |
364 | 0 | libcerror_error_set( |
365 | 0 | error, |
366 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
367 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
368 | 0 | "%s: invalid data block.", |
369 | 0 | function ); |
370 | |
|
371 | 0 | return( -1 ); |
372 | 0 | } |
373 | 985 | if( data_block->data != NULL ) |
374 | 0 | { |
375 | 0 | libcerror_error_set( |
376 | 0 | error, |
377 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
378 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
379 | 0 | "%s: invalid data block - data already set.", |
380 | 0 | function ); |
381 | |
|
382 | 0 | return( -1 ); |
383 | 0 | } |
384 | 985 | if( ( data_block->size == 0 ) |
385 | 985 | || ( data_block->size > (uint32_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) ) |
386 | 70 | { |
387 | 70 | libcerror_error_set( |
388 | 70 | error, |
389 | 70 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
390 | 70 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
391 | 70 | "%s: invalid data block - size value out of bounds.", |
392 | 70 | function ); |
393 | | |
394 | 70 | return( -1 ); |
395 | 70 | } |
396 | 915 | data_block->data_size = (size_t) data_block->size - sizeof( creg_data_block_header_t ); |
397 | | |
398 | 915 | data_block->data = (uint8_t *) memory_allocate( |
399 | 915 | sizeof( uint8_t ) * data_block->data_size ); |
400 | | |
401 | 915 | if( data_block->data == NULL ) |
402 | 6 | { |
403 | 6 | libcerror_error_set( |
404 | 6 | error, |
405 | 6 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
406 | 6 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
407 | 6 | "%s: unable to create data block data.", |
408 | 6 | function ); |
409 | | |
410 | 6 | goto on_error; |
411 | 6 | } |
412 | 909 | read_count = libbfio_handle_read_buffer( |
413 | 909 | file_io_handle, |
414 | 909 | data_block->data, |
415 | 909 | data_block->data_size, |
416 | 909 | error ); |
417 | | |
418 | 909 | if( read_count != (ssize_t) data_block->data_size ) |
419 | 144 | { |
420 | 144 | libcerror_error_set( |
421 | 144 | error, |
422 | 144 | LIBCERROR_ERROR_DOMAIN_IO, |
423 | 144 | LIBCERROR_IO_ERROR_READ_FAILED, |
424 | 144 | "%s: unable to read data block data.", |
425 | 144 | function ); |
426 | | |
427 | 144 | goto on_error; |
428 | 144 | } |
429 | 4.77k | while( data_offset < data_block->data_size ) |
430 | 4.65k | { |
431 | 4.65k | if( libcreg_key_name_entry_initialize( |
432 | 4.65k | &key_name_entry, |
433 | 4.65k | error ) != 1 ) |
434 | 0 | { |
435 | 0 | libcerror_error_set( |
436 | 0 | error, |
437 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
438 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
439 | 0 | "%s: unable to create key name entry.", |
440 | 0 | function ); |
441 | |
|
442 | 0 | goto on_error; |
443 | 0 | } |
444 | 4.65k | key_name_entry->offset = data_block->offset + sizeof( creg_data_block_header_t ) + data_offset; |
445 | | |
446 | | #if defined( HAVE_DEBUG_OUTPUT ) |
447 | | if( libcnotify_verbose != 0 ) |
448 | | { |
449 | | libcnotify_printf( |
450 | | "%s: reading key name entry at offset: %" PRIu32 " (0x%08" PRIx32 ")\n", |
451 | | function, |
452 | | key_name_entry->offset, |
453 | | key_name_entry->offset ); |
454 | | } |
455 | | #endif |
456 | 4.65k | result = libcreg_key_name_entry_read_data( |
457 | 4.65k | key_name_entry, |
458 | 4.65k | &( ( data_block->data )[ data_offset ] ), |
459 | 4.65k | data_block->data_size - data_offset, |
460 | 4.65k | ascii_codepage, |
461 | 4.65k | error ); |
462 | | |
463 | 4.65k | if( result == -1 ) |
464 | 652 | { |
465 | 652 | libcerror_error_set( |
466 | 652 | error, |
467 | 652 | LIBCERROR_ERROR_DOMAIN_IO, |
468 | 652 | LIBCERROR_IO_ERROR_READ_FAILED, |
469 | 652 | "%s: unable to read key name entry at offset: %" PRIu32 " (0x%08" PRIx32 ").", |
470 | 652 | function, |
471 | 652 | key_name_entry->offset, |
472 | 652 | key_name_entry->offset ); |
473 | | |
474 | 652 | goto on_error; |
475 | 652 | } |
476 | 4.00k | key_name_entry_size = key_name_entry->size; |
477 | | |
478 | 4.00k | if( result == 0 ) |
479 | 621 | { |
480 | 621 | if( key_name_entry_size <= ( data_block->data_size - data_offset ) ) |
481 | 621 | { |
482 | 621 | result = 1; |
483 | 621 | } |
484 | 621 | if( libcreg_key_name_entry_free( |
485 | 621 | &key_name_entry, |
486 | 621 | error ) != 1 ) |
487 | 0 | { |
488 | 0 | libcerror_error_set( |
489 | 0 | error, |
490 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
491 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
492 | 0 | "%s: unable to free key name entry.", |
493 | 0 | function ); |
494 | |
|
495 | 0 | goto on_error; |
496 | 0 | } |
497 | 621 | } |
498 | 4.00k | if( result == 0 ) |
499 | 0 | { |
500 | 0 | break; |
501 | 0 | } |
502 | 4.00k | data_offset += key_name_entry_size; |
503 | | |
504 | 4.00k | if( key_name_entry != NULL ) |
505 | 3.38k | { |
506 | 3.38k | if( libcdata_array_append_entry( |
507 | 3.38k | data_block->entries_array, |
508 | 3.38k | &entry_index, |
509 | 3.38k | (intptr_t *) key_name_entry, |
510 | 3.38k | error ) != 1 ) |
511 | 0 | { |
512 | 0 | libcerror_error_set( |
513 | 0 | error, |
514 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
515 | 0 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
516 | 0 | "%s: unable to append key name entry: %d.", |
517 | 0 | function, |
518 | 0 | key_name_entry ); |
519 | |
|
520 | 0 | goto on_error; |
521 | 0 | } |
522 | 3.38k | key_name_entry = NULL; |
523 | 3.38k | } |
524 | 4.00k | } |
525 | | #if defined( HAVE_DEBUG_OUTPUT ) |
526 | | if( libcnotify_verbose != 0 ) |
527 | | { |
528 | | if( data_offset < data_block->data_size ) |
529 | | { |
530 | | libcnotify_printf( |
531 | | "%s: trailing data:\n", |
532 | | function ); |
533 | | libcnotify_print_data( |
534 | | &( ( data_block->data )[ data_offset ] ), |
535 | | data_block->data_size - data_offset, |
536 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
537 | | } |
538 | | else |
539 | | { |
540 | | libcnotify_printf( |
541 | | "\n" ); |
542 | | } |
543 | | } |
544 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
545 | | |
546 | 113 | return( 1 ); |
547 | | |
548 | 802 | on_error: |
549 | 802 | if( key_name_entry != NULL ) |
550 | 652 | { |
551 | 652 | libcreg_key_name_entry_free( |
552 | 652 | &key_name_entry, |
553 | 652 | NULL ); |
554 | 652 | } |
555 | 802 | if( data_block->entries_array != NULL ) |
556 | 802 | { |
557 | 802 | libcdata_array_clear( |
558 | 802 | data_block->entries_array, |
559 | 802 | (int (*)(intptr_t **, libcerror_error_t **)) &libcreg_key_name_entry_free, |
560 | 802 | NULL ); |
561 | 802 | } |
562 | 802 | if( data_block->data != NULL ) |
563 | 796 | { |
564 | 796 | memory_free( |
565 | 796 | data_block->data ); |
566 | | |
567 | 796 | data_block->data = NULL; |
568 | 796 | } |
569 | 802 | data_block->data_size = 0; |
570 | | |
571 | 802 | return( -1 ); |
572 | 765 | } |
573 | | |
574 | | /* Retrieves the number of key name entries |
575 | | * Returns 1 if successful or -1 on error |
576 | | */ |
577 | | int libcreg_data_block_get_number_of_entries( |
578 | | libcreg_data_block_t *data_block, |
579 | | int *number_of_entries, |
580 | | libcerror_error_t **error ) |
581 | 0 | { |
582 | 0 | static char *function = "libcreg_data_block_get_number_of_entries"; |
583 | |
|
584 | 0 | if( data_block == NULL ) |
585 | 0 | { |
586 | 0 | libcerror_error_set( |
587 | 0 | error, |
588 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
589 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
590 | 0 | "%s: invalid data block.", |
591 | 0 | function ); |
592 | |
|
593 | 0 | return( -1 ); |
594 | 0 | } |
595 | 0 | if( libcdata_array_get_number_of_entries( |
596 | 0 | data_block->entries_array, |
597 | 0 | number_of_entries, |
598 | 0 | error ) != 1 ) |
599 | 0 | { |
600 | 0 | libcerror_error_set( |
601 | 0 | error, |
602 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
603 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
604 | 0 | "%s: unable to retrieve number of entries.", |
605 | 0 | function ); |
606 | |
|
607 | 0 | return( -1 ); |
608 | 0 | } |
609 | 0 | return( 1 ); |
610 | 0 | } |
611 | | |
612 | | /* Retrieves a specific key name entry |
613 | | * Returns 1 if successful, 0 if no such entry or -1 on error |
614 | | */ |
615 | | int libcreg_data_block_get_entry_by_identifier( |
616 | | libcreg_data_block_t *data_block, |
617 | | uint16_t identifier, |
618 | | libcreg_key_name_entry_t **key_name_entry, |
619 | | int ascii_codepage, |
620 | | libcerror_error_t **error ) |
621 | 131 | { |
622 | 131 | libcreg_key_name_entry_t *safe_key_name_entry = NULL; |
623 | 131 | static char *function = "libcreg_data_block_get_entry_by_identifier"; |
624 | 131 | int entry_index = 0; |
625 | 131 | int number_of_entries = 0; |
626 | | |
627 | 131 | if( data_block == NULL ) |
628 | 0 | { |
629 | 0 | libcerror_error_set( |
630 | 0 | error, |
631 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
632 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
633 | 0 | "%s: invalid data block.", |
634 | 0 | function ); |
635 | |
|
636 | 0 | return( -1 ); |
637 | 0 | } |
638 | 131 | if( data_block->data == NULL ) |
639 | 0 | { |
640 | 0 | libcerror_error_set( |
641 | 0 | error, |
642 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
643 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
644 | 0 | "%s: invalid data block - missing data.", |
645 | 0 | function ); |
646 | |
|
647 | 0 | return( -1 ); |
648 | 0 | } |
649 | 131 | if( key_name_entry == NULL ) |
650 | 0 | { |
651 | 0 | libcerror_error_set( |
652 | 0 | error, |
653 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
654 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
655 | 0 | "%s: invalid key name entry.", |
656 | 0 | function ); |
657 | |
|
658 | 0 | return( -1 ); |
659 | 0 | } |
660 | 131 | if( libcdata_array_get_number_of_entries( |
661 | 131 | data_block->entries_array, |
662 | 131 | &number_of_entries, |
663 | 131 | error ) != 1 ) |
664 | 0 | { |
665 | 0 | libcerror_error_set( |
666 | 0 | error, |
667 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
668 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
669 | 0 | "%s: unable to retrieve number of entries.", |
670 | 0 | function ); |
671 | |
|
672 | 0 | return( -1 ); |
673 | 0 | } |
674 | 131 | for( entry_index = 0; |
675 | 1.47k | entry_index < number_of_entries; |
676 | 1.34k | entry_index++ ) |
677 | 1.39k | { |
678 | 1.39k | if( libcdata_array_get_entry_by_index( |
679 | 1.39k | data_block->entries_array, |
680 | 1.39k | entry_index, |
681 | 1.39k | (intptr_t **) &safe_key_name_entry, |
682 | 1.39k | error ) != 1 ) |
683 | 0 | { |
684 | 0 | libcerror_error_set( |
685 | 0 | error, |
686 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
687 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
688 | 0 | "%s: unable to retrieve key name entry: %d.", |
689 | 0 | function, |
690 | 0 | entry_index ); |
691 | |
|
692 | 0 | return( -1 ); |
693 | 0 | } |
694 | 1.39k | if( safe_key_name_entry == NULL ) |
695 | 0 | { |
696 | 0 | libcerror_error_set( |
697 | 0 | error, |
698 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
699 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
700 | 0 | "%s: invalid data block - missing key name entry: %d.", |
701 | 0 | function, |
702 | 0 | entry_index ); |
703 | |
|
704 | 0 | return( -1 ); |
705 | 0 | } |
706 | 1.39k | if( safe_key_name_entry->index == identifier ) |
707 | 44 | { |
708 | 44 | *key_name_entry = safe_key_name_entry; |
709 | | |
710 | 44 | return( 1 ); |
711 | 44 | } |
712 | 1.39k | } |
713 | 87 | return( 0 ); |
714 | 131 | } |
715 | | |