/src/libpff/libpff/libpff_offsets_index.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Offsets index functions |
3 | | * |
4 | | * Copyright (C) 2008-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 <memory.h> |
24 | | #include <types.h> |
25 | | |
26 | | #include "libpff_definitions.h" |
27 | | #include "libpff_index.h" |
28 | | #include "libpff_index_value.h" |
29 | | #include "libpff_index_values_list.h" |
30 | | #include "libpff_libbfio.h" |
31 | | #include "libpff_libcdata.h" |
32 | | #include "libpff_libcerror.h" |
33 | | #include "libpff_offsets_index.h" |
34 | | |
35 | | /* Creates an offsets index |
36 | | * Make sure the value offsets_index is referencing, is set to NULL |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | int libpff_offsets_index_initialize( |
40 | | libpff_offsets_index_t **offsets_index, |
41 | | off64_t root_node_offset, |
42 | | uint64_t root_node_back_pointer, |
43 | | libcerror_error_t **error ) |
44 | 8.63k | { |
45 | 8.63k | static char *function = "libpff_offsets_index_initialize"; |
46 | | |
47 | 8.63k | if( offsets_index == NULL ) |
48 | 0 | { |
49 | 0 | libcerror_error_set( |
50 | 0 | error, |
51 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
52 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
53 | 0 | "%s: invalid offsets index.", |
54 | 0 | function ); |
55 | |
|
56 | 0 | return( -1 ); |
57 | 0 | } |
58 | 8.63k | if( *offsets_index != NULL ) |
59 | 0 | { |
60 | 0 | libcerror_error_set( |
61 | 0 | error, |
62 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
63 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
64 | 0 | "%s: invalid offsets index value already set.", |
65 | 0 | function ); |
66 | |
|
67 | 0 | return( -1 ); |
68 | 0 | } |
69 | 8.63k | *offsets_index = memory_allocate_structure( |
70 | 8.63k | libpff_offsets_index_t ); |
71 | | |
72 | 8.63k | if( *offsets_index == NULL ) |
73 | 0 | { |
74 | 0 | libcerror_error_set( |
75 | 0 | error, |
76 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
77 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
78 | 0 | "%s: unable to create offsets index.", |
79 | 0 | function ); |
80 | |
|
81 | 0 | goto on_error; |
82 | 0 | } |
83 | 8.63k | if( memory_set( |
84 | 8.63k | *offsets_index, |
85 | 8.63k | 0, |
86 | 8.63k | sizeof( libpff_offsets_index_t ) ) == NULL ) |
87 | 0 | { |
88 | 0 | libcerror_error_set( |
89 | 0 | error, |
90 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
91 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
92 | 0 | "%s: unable to clear offsets index.", |
93 | 0 | function ); |
94 | |
|
95 | 0 | memory_free( |
96 | 0 | *offsets_index ); |
97 | |
|
98 | 0 | *offsets_index = NULL; |
99 | |
|
100 | 0 | return( -1 ); |
101 | 0 | } |
102 | 8.63k | if( libpff_index_initialize( |
103 | 8.63k | &( ( *offsets_index )->index ), |
104 | 8.63k | LIBPFF_INDEX_TYPE_OFFSET, |
105 | 8.63k | root_node_offset, |
106 | 8.63k | root_node_back_pointer, |
107 | 8.63k | 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 index.", |
114 | 0 | function ); |
115 | |
|
116 | 0 | goto on_error; |
117 | 0 | } |
118 | 8.63k | if( libcdata_btree_initialize( |
119 | 8.63k | &( ( *offsets_index )->recovered_index_values_tree ), |
120 | 8.63k | 257, |
121 | 8.63k | error ) != 1 ) |
122 | 0 | { |
123 | 0 | libcerror_error_set( |
124 | 0 | error, |
125 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
126 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
127 | 0 | "%s: unable to create recovered index values B-tree.", |
128 | 0 | function ); |
129 | |
|
130 | 0 | goto on_error; |
131 | 0 | } |
132 | 8.63k | return( 1 ); |
133 | | |
134 | 0 | on_error: |
135 | 0 | if( *offsets_index != NULL ) |
136 | 0 | { |
137 | 0 | if( ( *offsets_index )->recovered_index_values_tree != NULL ) |
138 | 0 | { |
139 | 0 | libcdata_btree_free( |
140 | 0 | &( ( *offsets_index )->recovered_index_values_tree ), |
141 | 0 | (int (*)(intptr_t **, libcerror_error_t **)) &libpff_index_values_list_free, |
142 | 0 | NULL ); |
143 | 0 | } |
144 | 0 | if( ( *offsets_index )->index != NULL ) |
145 | 0 | { |
146 | 0 | libpff_index_free( |
147 | 0 | &( ( *offsets_index )->index ), |
148 | 0 | NULL ); |
149 | 0 | } |
150 | 0 | memory_free( |
151 | 0 | *offsets_index ); |
152 | |
|
153 | 0 | *offsets_index = NULL; |
154 | 0 | } |
155 | 0 | return( -1 ); |
156 | 8.63k | } |
157 | | |
158 | | /* Frees an offsets index |
159 | | * Returns 1 if successful or -1 on error |
160 | | */ |
161 | | int libpff_offsets_index_free( |
162 | | libpff_offsets_index_t **offsets_index, |
163 | | libcerror_error_t **error ) |
164 | 8.63k | { |
165 | 8.63k | static char *function = "libpff_offsets_index_free"; |
166 | 8.63k | int result = 1; |
167 | | |
168 | 8.63k | if( offsets_index == NULL ) |
169 | 0 | { |
170 | 0 | libcerror_error_set( |
171 | 0 | error, |
172 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
173 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
174 | 0 | "%s: invalid offsets index.", |
175 | 0 | function ); |
176 | |
|
177 | 0 | return( -1 ); |
178 | 0 | } |
179 | 8.63k | if( *offsets_index != NULL ) |
180 | 8.63k | { |
181 | 8.63k | if( libcdata_btree_free( |
182 | 8.63k | &( ( *offsets_index )->recovered_index_values_tree ), |
183 | 8.63k | (int (*)(intptr_t **, libcerror_error_t **)) &libpff_index_values_list_free, |
184 | 8.63k | error ) != 1 ) |
185 | 0 | { |
186 | 0 | libcerror_error_set( |
187 | 0 | error, |
188 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
189 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
190 | 0 | "%s: unable to free recovered index values tree.", |
191 | 0 | function ); |
192 | |
|
193 | 0 | result = -1; |
194 | 0 | } |
195 | 8.63k | if( libpff_index_free( |
196 | 8.63k | &( ( *offsets_index )->index ), |
197 | 8.63k | error ) != 1 ) |
198 | 0 | { |
199 | 0 | libcerror_error_set( |
200 | 0 | error, |
201 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
202 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
203 | 0 | "%s: unable to free index.", |
204 | 0 | function ); |
205 | |
|
206 | 0 | result = -1; |
207 | 0 | } |
208 | 8.63k | memory_free( |
209 | 8.63k | *offsets_index ); |
210 | | |
211 | 8.63k | *offsets_index = NULL; |
212 | 8.63k | } |
213 | 8.63k | return( result ); |
214 | 8.63k | } |
215 | | |
216 | | /* Retrieves an index value for a specific identifier |
217 | | * Returns 1 if successful, 0 if no index value was found or -1 on error |
218 | | */ |
219 | | int libpff_offsets_index_get_index_value_by_identifier( |
220 | | libpff_offsets_index_t *offsets_index, |
221 | | libpff_io_handle_t *io_handle, |
222 | | libbfio_handle_t *file_io_handle, |
223 | | uint64_t data_identifier, |
224 | | uint8_t recovered, |
225 | | int recovered_value_index, |
226 | | libpff_index_value_t **index_value, |
227 | | libcerror_error_t **error ) |
228 | 85.8k | { |
229 | 85.8k | libcdata_tree_node_t *upper_node = NULL; |
230 | 85.8k | libpff_index_value_t *lookup_index_value = NULL; |
231 | 85.8k | libpff_index_value_t *safe_index_value = NULL; |
232 | 85.8k | libpff_index_values_list_t *index_values_list = NULL; |
233 | 85.8k | static char *function = "libpff_offsets_index_get_index_value_by_identifier"; |
234 | 85.8k | uint64_t lookup_data_identifier = 0; |
235 | 85.8k | int result = 0; |
236 | | |
237 | 85.8k | if( offsets_index == 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 offsets index.", |
244 | 0 | function ); |
245 | |
|
246 | 0 | return( -1 ); |
247 | 0 | } |
248 | 85.8k | lookup_data_identifier = data_identifier & (uint64_t) LIBPFF_OFFSET_INDEX_IDENTIFIER_MASK; |
249 | | |
250 | 85.8k | if( recovered == 0 ) |
251 | 85.8k | { |
252 | 85.8k | result = libpff_index_get_value_by_identifier( |
253 | 85.8k | offsets_index->index, |
254 | 85.8k | io_handle, |
255 | 85.8k | file_io_handle, |
256 | 85.8k | lookup_data_identifier, |
257 | 85.8k | index_value, |
258 | 85.8k | error ); |
259 | | |
260 | 85.8k | if( result == -1 ) |
261 | 3.03k | { |
262 | 3.03k | libcerror_error_set( |
263 | 3.03k | error, |
264 | 3.03k | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
265 | 3.03k | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
266 | 3.03k | "%s: unable to retrieve index value: %" PRIu64 " from index.", |
267 | 3.03k | function, |
268 | 3.03k | lookup_data_identifier ); |
269 | | |
270 | 3.03k | goto on_error; |
271 | 3.03k | } |
272 | 85.8k | } |
273 | 0 | else |
274 | 0 | { |
275 | 0 | if( libpff_index_value_initialize( |
276 | 0 | &lookup_index_value, |
277 | 0 | error ) != 1 ) |
278 | 0 | { |
279 | 0 | libcerror_error_set( |
280 | 0 | error, |
281 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
282 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
283 | 0 | "%s: unable to create lookup index value.", |
284 | 0 | function ); |
285 | |
|
286 | 0 | goto on_error; |
287 | 0 | } |
288 | 0 | lookup_index_value->identifier = lookup_data_identifier; |
289 | |
|
290 | 0 | result = libcdata_btree_get_value_by_value( |
291 | 0 | offsets_index->recovered_index_values_tree, |
292 | 0 | (intptr_t *) lookup_index_value, |
293 | 0 | (int (*)(intptr_t *, intptr_t *, libcerror_error_t **)) &libpff_index_values_list_compare, |
294 | 0 | &upper_node, |
295 | 0 | (intptr_t **) &index_values_list, |
296 | 0 | error ); |
297 | |
|
298 | 0 | if( result == -1 ) |
299 | 0 | { |
300 | 0 | libcerror_error_set( |
301 | 0 | error, |
302 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
303 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
304 | 0 | "%s: unable to retrieve recovered index value: %" PRIu64 " list from tree.", |
305 | 0 | function, |
306 | 0 | lookup_data_identifier ); |
307 | |
|
308 | 0 | goto on_error; |
309 | 0 | } |
310 | 0 | else if( result != 0 ) |
311 | 0 | { |
312 | 0 | if( libpff_index_values_list_get_value_by_index( |
313 | 0 | index_values_list, |
314 | 0 | recovered_value_index, |
315 | 0 | &safe_index_value, |
316 | 0 | error ) != 1 ) |
317 | 0 | { |
318 | 0 | libcerror_error_set( |
319 | 0 | error, |
320 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
321 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
322 | 0 | "%s: unable to retrieve retrieve recovered index value: %" PRIu64 " list entry: %d.", |
323 | 0 | function, |
324 | 0 | lookup_data_identifier, |
325 | 0 | recovered_value_index ); |
326 | |
|
327 | 0 | goto on_error; |
328 | 0 | } |
329 | 0 | if( safe_index_value == NULL ) |
330 | 0 | { |
331 | 0 | libcerror_error_set( |
332 | 0 | error, |
333 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
334 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
335 | 0 | "%s: missing recovered index value: %" PRIu64 " list entry: %d.", |
336 | 0 | function, |
337 | 0 | lookup_data_identifier, |
338 | 0 | recovered_value_index ); |
339 | |
|
340 | 0 | goto on_error; |
341 | 0 | } |
342 | 0 | lookup_index_value->file_offset = lookup_index_value->file_offset; |
343 | 0 | lookup_index_value->data_size = lookup_index_value->data_size; |
344 | 0 | lookup_index_value->reference_count = lookup_index_value->reference_count; |
345 | |
|
346 | 0 | *index_value = lookup_index_value; |
347 | 0 | } |
348 | 0 | } |
349 | 82.8k | return( result ); |
350 | | |
351 | 3.03k | on_error: |
352 | 3.03k | if( lookup_index_value != NULL ) |
353 | 0 | { |
354 | 0 | libpff_index_value_free( |
355 | 0 | &lookup_index_value, |
356 | 0 | NULL ); |
357 | 0 | } |
358 | 3.03k | return( -1 ); |
359 | 85.8k | } |
360 | | |
361 | | /* Inserts a recovered index value into the offsets index |
362 | | * Returns 1 if successful or -1 on error |
363 | | */ |
364 | | int libpff_offsets_index_insert_recovered_index_value( |
365 | | libpff_offsets_index_t *offsets_index, |
366 | | libpff_index_value_t *index_value, |
367 | | libcerror_error_t **error ) |
368 | 0 | { |
369 | 0 | libcdata_tree_node_t *upper_node = NULL; |
370 | 0 | libpff_index_values_list_t *existing_index_values_list = NULL; |
371 | 0 | libpff_index_values_list_t *index_values_list = NULL; |
372 | 0 | static char *function = "libpff_offsets_index_insert_recovered_index_value"; |
373 | 0 | int result = 0; |
374 | 0 | int value_index = 0; |
375 | |
|
376 | 0 | if( offsets_index == NULL ) |
377 | 0 | { |
378 | 0 | libcerror_error_set( |
379 | 0 | error, |
380 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
381 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
382 | 0 | "%s: invalid offsets index.", |
383 | 0 | function ); |
384 | |
|
385 | 0 | return( -1 ); |
386 | 0 | } |
387 | 0 | if( index_value == NULL ) |
388 | 0 | { |
389 | 0 | libcerror_error_set( |
390 | 0 | error, |
391 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
392 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
393 | 0 | "%s: invalid index value.", |
394 | 0 | function ); |
395 | |
|
396 | 0 | return( -1 ); |
397 | 0 | } |
398 | 0 | if( libpff_index_values_list_initialize( |
399 | 0 | &index_values_list, |
400 | 0 | error ) != 1 ) |
401 | 0 | { |
402 | 0 | libcerror_error_set( |
403 | 0 | error, |
404 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
405 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
406 | 0 | "%s: unable to create index values list.", |
407 | 0 | function ); |
408 | |
|
409 | 0 | goto on_error; |
410 | 0 | } |
411 | 0 | index_values_list->identifier = index_value->identifier; |
412 | |
|
413 | 0 | result = libcdata_btree_insert_value( |
414 | 0 | offsets_index->recovered_index_values_tree, |
415 | 0 | &value_index, |
416 | 0 | (intptr_t *) index_values_list, |
417 | 0 | (int (*)(intptr_t *, intptr_t *, libcerror_error_t **)) &libpff_index_values_list_compare, |
418 | 0 | &upper_node, |
419 | 0 | (intptr_t **) &existing_index_values_list, |
420 | 0 | error ); |
421 | |
|
422 | 0 | if( result == -1 ) |
423 | 0 | { |
424 | 0 | libcerror_error_set( |
425 | 0 | error, |
426 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
427 | 0 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
428 | 0 | "%s: unable to insert recovered offsets index value: %" PRIu64 " list.", |
429 | 0 | function, |
430 | 0 | index_value->identifier ); |
431 | |
|
432 | 0 | goto on_error; |
433 | 0 | } |
434 | 0 | else if( result != 0 ) |
435 | 0 | { |
436 | 0 | existing_index_values_list = index_values_list; |
437 | 0 | index_values_list = NULL; |
438 | 0 | } |
439 | 0 | else |
440 | 0 | { |
441 | 0 | if( libpff_index_values_list_free( |
442 | 0 | &index_values_list, |
443 | 0 | error ) != 1 ) |
444 | 0 | { |
445 | 0 | libcerror_error_set( |
446 | 0 | error, |
447 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
448 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
449 | 0 | "%s: unable to free index values list.", |
450 | 0 | function ); |
451 | |
|
452 | 0 | goto on_error; |
453 | 0 | } |
454 | 0 | } |
455 | 0 | if( libpff_index_values_list_append_value( |
456 | 0 | existing_index_values_list, |
457 | 0 | index_value, |
458 | 0 | error ) != 1 ) |
459 | 0 | { |
460 | 0 | libcerror_error_set( |
461 | 0 | error, |
462 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
463 | 0 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
464 | 0 | "%s: unable to append index value to list.", |
465 | 0 | function ); |
466 | |
|
467 | 0 | goto on_error; |
468 | 0 | } |
469 | 0 | return( 1 ); |
470 | | |
471 | 0 | on_error: |
472 | 0 | if( index_values_list != NULL ) |
473 | 0 | { |
474 | 0 | libpff_index_values_list_free( |
475 | 0 | &index_values_list, |
476 | 0 | NULL ); |
477 | 0 | } |
478 | 0 | return( -1 ); |
479 | 0 | } |
480 | | |