/src/libagdb/libfcache/libfcache_cache_value.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Cache value functions |
3 | | * |
4 | | * Copyright (C) 2010-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 "libfcache_cache_value.h" |
27 | | #include "libfcache_definitions.h" |
28 | | #include "libfcache_libcerror.h" |
29 | | #include "libfcache_types.h" |
30 | | |
31 | | /* Creates a cache value |
32 | | * Make sure the value cache_value is referencing, is set to NULL |
33 | | * Returns 1 if successful or -1 on error |
34 | | */ |
35 | | int libfcache_cache_value_initialize( |
36 | | libfcache_cache_value_t **cache_value, |
37 | | libcerror_error_t **error ) |
38 | 125k | { |
39 | 125k | libfcache_internal_cache_value_t *internal_cache_value = NULL; |
40 | 125k | static char *function = "libfcache_cache_value_initialize"; |
41 | | |
42 | 125k | if( cache_value == NULL ) |
43 | 0 | { |
44 | 0 | libcerror_error_set( |
45 | 0 | error, |
46 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
47 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
48 | 0 | "%s: invalid cache value.", |
49 | 0 | function ); |
50 | |
|
51 | 0 | return( -1 ); |
52 | 0 | } |
53 | 125k | if( *cache_value != NULL ) |
54 | 0 | { |
55 | 0 | libcerror_error_set( |
56 | 0 | error, |
57 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
58 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
59 | 0 | "%s: invalid cache value value already set.", |
60 | 0 | function ); |
61 | |
|
62 | 0 | return( -1 ); |
63 | 0 | } |
64 | 125k | internal_cache_value = memory_allocate_structure( |
65 | 125k | libfcache_internal_cache_value_t ); |
66 | | |
67 | 125k | if( internal_cache_value == NULL ) |
68 | 0 | { |
69 | 0 | libcerror_error_set( |
70 | 0 | error, |
71 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
72 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
73 | 0 | "%s: unable to create cache value.", |
74 | 0 | function ); |
75 | |
|
76 | 0 | goto on_error; |
77 | 0 | } |
78 | 125k | if( memory_set( |
79 | 125k | internal_cache_value, |
80 | 125k | 0, |
81 | 125k | sizeof( libfcache_internal_cache_value_t ) ) == NULL ) |
82 | 0 | { |
83 | 0 | libcerror_error_set( |
84 | 0 | error, |
85 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
86 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
87 | 0 | "%s: unable to clear cache value.", |
88 | 0 | function ); |
89 | |
|
90 | 0 | goto on_error; |
91 | 0 | } |
92 | 125k | internal_cache_value->file_index = -1; |
93 | 125k | internal_cache_value->offset = (off64_t) -1; |
94 | | |
95 | 125k | *cache_value = (libfcache_cache_value_t *) internal_cache_value; |
96 | | |
97 | 125k | return( 1 ); |
98 | | |
99 | 0 | on_error: |
100 | 0 | if( internal_cache_value != NULL ) |
101 | 0 | { |
102 | 0 | memory_free( |
103 | 0 | internal_cache_value ); |
104 | 0 | } |
105 | 0 | return( -1 ); |
106 | 125k | } |
107 | | |
108 | | /* Frees a cache value |
109 | | * Returns 1 if successful or -1 on error |
110 | | */ |
111 | | int libfcache_cache_value_free( |
112 | | libfcache_cache_value_t **cache_value, |
113 | | libcerror_error_t **error ) |
114 | 125k | { |
115 | 125k | libfcache_internal_cache_value_t *internal_cache_value = NULL; |
116 | 125k | static char *function = "libfcache_cache_value_free"; |
117 | 125k | int result = 1; |
118 | | |
119 | 125k | if( cache_value == NULL ) |
120 | 0 | { |
121 | 0 | libcerror_error_set( |
122 | 0 | error, |
123 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
124 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
125 | 0 | "%s: invalid cache value.", |
126 | 0 | function ); |
127 | |
|
128 | 0 | return( -1 ); |
129 | 0 | } |
130 | 125k | if( *cache_value != NULL ) |
131 | 125k | { |
132 | 125k | internal_cache_value = (libfcache_internal_cache_value_t *) *cache_value; |
133 | 125k | *cache_value = NULL; |
134 | | |
135 | 125k | if( internal_cache_value->value != NULL ) |
136 | 123k | { |
137 | 123k | if( ( internal_cache_value->flags & LIBFCACHE_CACHE_VALUE_FLAG_MANAGED ) != 0 ) |
138 | 117k | { |
139 | 117k | if( internal_cache_value->value_free_function == NULL ) |
140 | 0 | { |
141 | 0 | libcerror_error_set( |
142 | 0 | error, |
143 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
144 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
145 | 0 | "%s: invalid cache value - missing value free function.", |
146 | 0 | function ); |
147 | |
|
148 | 0 | result = -1; |
149 | 0 | } |
150 | 117k | else if( internal_cache_value->value_free_function( |
151 | 117k | &( internal_cache_value->value ), |
152 | 117k | error ) != 1 ) |
153 | 0 | { |
154 | 0 | libcerror_error_set( |
155 | 0 | error, |
156 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
157 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
158 | 0 | "%s: unable to free value.", |
159 | 0 | function ); |
160 | |
|
161 | 0 | result = -1; |
162 | 0 | } |
163 | 117k | } |
164 | 123k | } |
165 | 125k | memory_free( |
166 | 125k | internal_cache_value ); |
167 | 125k | } |
168 | 125k | return( result ); |
169 | 125k | } |
170 | | |
171 | | /* Clears the cache value |
172 | | * This function does not free the value |
173 | | * Returns 1 if successful or -1 on error |
174 | | */ |
175 | | int libfcache_cache_value_clear( |
176 | | libfcache_cache_value_t *cache_value, |
177 | | libcerror_error_t **error ) |
178 | 2.12k | { |
179 | 2.12k | libfcache_internal_cache_value_t *internal_cache_value = NULL; |
180 | 2.12k | static char *function = "libfcache_cache_value_free"; |
181 | | |
182 | 2.12k | if( cache_value == NULL ) |
183 | 0 | { |
184 | 0 | libcerror_error_set( |
185 | 0 | error, |
186 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
187 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
188 | 0 | "%s: invalid cache value.", |
189 | 0 | function ); |
190 | |
|
191 | 0 | return( -1 ); |
192 | 0 | } |
193 | 2.12k | internal_cache_value = (libfcache_internal_cache_value_t *) cache_value; |
194 | | |
195 | 2.12k | if( memory_set( |
196 | 2.12k | internal_cache_value, |
197 | 2.12k | 0, |
198 | 2.12k | sizeof( libfcache_internal_cache_value_t ) ) == NULL ) |
199 | 0 | { |
200 | 0 | libcerror_error_set( |
201 | 0 | error, |
202 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
203 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
204 | 0 | "%s: unable to clear cache value.", |
205 | 0 | function ); |
206 | |
|
207 | 0 | return( -1 ); |
208 | 0 | } |
209 | 2.12k | internal_cache_value->file_index = -1; |
210 | 2.12k | internal_cache_value->offset = (off64_t) -1; |
211 | | |
212 | 2.12k | return( 1 ); |
213 | 2.12k | } |
214 | | |
215 | | /* Retrieves the cache value identifier |
216 | | * Returns 1 if successful or -1 on error |
217 | | */ |
218 | | int libfcache_cache_value_get_identifier( |
219 | | libfcache_cache_value_t *cache_value, |
220 | | int *file_index, |
221 | | off64_t *offset, |
222 | | int64_t *timestamp, |
223 | | libcerror_error_t **error ) |
224 | 2.75M | { |
225 | 2.75M | libfcache_internal_cache_value_t *internal_cache_value = NULL; |
226 | 2.75M | static char *function = "libfcache_cache_value_get_identifier"; |
227 | | |
228 | 2.75M | if( cache_value == NULL ) |
229 | 0 | { |
230 | 0 | libcerror_error_set( |
231 | 0 | error, |
232 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
233 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
234 | 0 | "%s: invalid cache value.", |
235 | 0 | function ); |
236 | |
|
237 | 0 | return( -1 ); |
238 | 0 | } |
239 | 2.75M | internal_cache_value = (libfcache_internal_cache_value_t *) cache_value; |
240 | | |
241 | 2.75M | if( file_index == NULL ) |
242 | 0 | { |
243 | 0 | libcerror_error_set( |
244 | 0 | error, |
245 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
246 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
247 | 0 | "%s: invalid file index.", |
248 | 0 | function ); |
249 | |
|
250 | 0 | return( -1 ); |
251 | 0 | } |
252 | 2.75M | if( offset == NULL ) |
253 | 0 | { |
254 | 0 | libcerror_error_set( |
255 | 0 | error, |
256 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
257 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
258 | 0 | "%s: invalid offset.", |
259 | 0 | function ); |
260 | |
|
261 | 0 | return( -1 ); |
262 | 0 | } |
263 | 2.75M | if( timestamp == NULL ) |
264 | 0 | { |
265 | 0 | libcerror_error_set( |
266 | 0 | error, |
267 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
268 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
269 | 0 | "%s: invalid timestamp.", |
270 | 0 | function ); |
271 | |
|
272 | 0 | return( -1 ); |
273 | 0 | } |
274 | 2.75M | *file_index = internal_cache_value->file_index; |
275 | 2.75M | *offset = internal_cache_value->offset; |
276 | 2.75M | *timestamp = internal_cache_value->timestamp; |
277 | | |
278 | 2.75M | return( 1 ); |
279 | 2.75M | } |
280 | | |
281 | | /* Retrieves the cache value cache index |
282 | | * Returns 1 if successful or -1 on error |
283 | | */ |
284 | | int libfcache_cache_value_get_cache_index( |
285 | | libfcache_cache_value_t *cache_value, |
286 | | int *cache_index, |
287 | | libcerror_error_t **error ) |
288 | 86.9k | { |
289 | 86.9k | libfcache_internal_cache_value_t *internal_cache_value = NULL; |
290 | 86.9k | static char *function = "libfcache_cache_value_get_cache_index"; |
291 | | |
292 | 86.9k | if( cache_value == NULL ) |
293 | 0 | { |
294 | 0 | libcerror_error_set( |
295 | 0 | error, |
296 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
297 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
298 | 0 | "%s: invalid cache value.", |
299 | 0 | function ); |
300 | |
|
301 | 0 | return( -1 ); |
302 | 0 | } |
303 | 86.9k | internal_cache_value = (libfcache_internal_cache_value_t *) cache_value; |
304 | | |
305 | 86.9k | if( cache_index == NULL ) |
306 | 0 | { |
307 | 0 | libcerror_error_set( |
308 | 0 | error, |
309 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
310 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
311 | 0 | "%s: invalid cache index.", |
312 | 0 | function ); |
313 | |
|
314 | 0 | return( -1 ); |
315 | 0 | } |
316 | 86.9k | *cache_index = internal_cache_value->cache_index; |
317 | | |
318 | 86.9k | return( 1 ); |
319 | 86.9k | } |
320 | | |
321 | | /* Sets the cache value identifier |
322 | | * Returns 1 if successful or -1 on error |
323 | | */ |
324 | | int libfcache_cache_value_set_identifier( |
325 | | libfcache_cache_value_t *cache_value, |
326 | | int file_index, |
327 | | off64_t offset, |
328 | | int64_t timestamp, |
329 | | libcerror_error_t **error ) |
330 | 212k | { |
331 | 212k | libfcache_internal_cache_value_t *internal_cache_value = NULL; |
332 | 212k | static char *function = "libfcache_cache_value_set_identifier"; |
333 | | |
334 | 212k | if( cache_value == NULL ) |
335 | 0 | { |
336 | 0 | libcerror_error_set( |
337 | 0 | error, |
338 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
339 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
340 | 0 | "%s: invalid cache value.", |
341 | 0 | function ); |
342 | |
|
343 | 0 | return( -1 ); |
344 | 0 | } |
345 | 212k | internal_cache_value = (libfcache_internal_cache_value_t *) cache_value; |
346 | | |
347 | 212k | internal_cache_value->file_index = file_index; |
348 | 212k | internal_cache_value->offset = offset; |
349 | 212k | internal_cache_value->timestamp = timestamp; |
350 | | |
351 | 212k | return( 1 ); |
352 | 212k | } |
353 | | |
354 | | /* Sets the cache value cache index |
355 | | * Returns 1 if successful or -1 on error |
356 | | */ |
357 | | int libfcache_cache_value_set_cache_index( |
358 | | libfcache_cache_value_t *cache_value, |
359 | | int cache_index, |
360 | | libcerror_error_t **error ) |
361 | 125k | { |
362 | 125k | libfcache_internal_cache_value_t *internal_cache_value = NULL; |
363 | 125k | static char *function = "libfcache_cache_value_set_cache_index"; |
364 | | |
365 | 125k | if( cache_value == NULL ) |
366 | 0 | { |
367 | 0 | libcerror_error_set( |
368 | 0 | error, |
369 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
370 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
371 | 0 | "%s: invalid cache value.", |
372 | 0 | function ); |
373 | |
|
374 | 0 | return( -1 ); |
375 | 0 | } |
376 | 125k | internal_cache_value = (libfcache_internal_cache_value_t *) cache_value; |
377 | | |
378 | 125k | internal_cache_value->cache_index = cache_index; |
379 | | |
380 | 125k | return( 1 ); |
381 | 125k | } |
382 | | |
383 | | /* Retrieves the cache value |
384 | | * Returns 1 if successful or -1 on error |
385 | | */ |
386 | | int libfcache_cache_value_get_value( |
387 | | libfcache_cache_value_t *cache_value, |
388 | | intptr_t **value, |
389 | | libcerror_error_t **error ) |
390 | 1.13M | { |
391 | 1.13M | libfcache_internal_cache_value_t *internal_cache_value = NULL; |
392 | 1.13M | static char *function = "libfcache_cache_value_get_value"; |
393 | | |
394 | 1.13M | if( cache_value == NULL ) |
395 | 0 | { |
396 | 0 | libcerror_error_set( |
397 | 0 | error, |
398 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
399 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
400 | 0 | "%s: invalid cache value.", |
401 | 0 | function ); |
402 | |
|
403 | 0 | return( -1 ); |
404 | 0 | } |
405 | 1.13M | internal_cache_value = (libfcache_internal_cache_value_t *) cache_value; |
406 | | |
407 | 1.13M | if( value == NULL ) |
408 | 0 | { |
409 | 0 | libcerror_error_set( |
410 | 0 | error, |
411 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
412 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
413 | 0 | "%s: invalid value.", |
414 | 0 | function ); |
415 | |
|
416 | 0 | return( -1 ); |
417 | 0 | } |
418 | 1.13M | *value = internal_cache_value->value; |
419 | | |
420 | 1.13M | return( 1 ); |
421 | 1.13M | } |
422 | | |
423 | | /* Sets the cache value |
424 | | * Returns 1 if successful or -1 on error |
425 | | */ |
426 | | int libfcache_cache_value_set_value( |
427 | | libfcache_cache_value_t *cache_value, |
428 | | intptr_t *value, |
429 | | int (*value_free_function)( |
430 | | intptr_t **value, |
431 | | libcerror_error_t **error ), |
432 | | uint8_t flags, |
433 | | libcerror_error_t **error ) |
434 | 212k | { |
435 | 212k | libfcache_internal_cache_value_t *internal_cache_value = NULL; |
436 | 212k | static char *function = "libfcache_cache_value_set_value"; |
437 | | |
438 | 212k | if( cache_value == NULL ) |
439 | 0 | { |
440 | 0 | libcerror_error_set( |
441 | 0 | error, |
442 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
443 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
444 | 0 | "%s: invalid cache value.", |
445 | 0 | function ); |
446 | |
|
447 | 0 | return( -1 ); |
448 | 0 | } |
449 | 212k | internal_cache_value = (libfcache_internal_cache_value_t *) cache_value; |
450 | | |
451 | 212k | if( ( flags & LIBFCACHE_CACHE_VALUE_FLAG_MANAGED ) != 0 ) |
452 | 206k | { |
453 | 206k | if( value_free_function == NULL ) |
454 | 0 | { |
455 | 0 | libcerror_error_set( |
456 | 0 | error, |
457 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
458 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
459 | 0 | "%s: invalid value free function.", |
460 | 0 | function ); |
461 | |
|
462 | 0 | return( -1 ); |
463 | 0 | } |
464 | 206k | } |
465 | 212k | if( ( internal_cache_value->flags & LIBFCACHE_CACHE_VALUE_FLAG_MANAGED ) != 0 ) |
466 | 87.1k | { |
467 | 87.1k | if( internal_cache_value->value != NULL ) |
468 | 87.1k | { |
469 | 87.1k | if( internal_cache_value->value_free_function == NULL ) |
470 | 0 | { |
471 | 0 | libcerror_error_set( |
472 | 0 | error, |
473 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
474 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
475 | 0 | "%s: invalid cache value - missing free value function.", |
476 | 0 | function ); |
477 | |
|
478 | 0 | return( -1 ); |
479 | 0 | } |
480 | 87.1k | if( internal_cache_value->value_free_function( |
481 | 87.1k | &( internal_cache_value->value ), |
482 | 87.1k | error ) != 1 ) |
483 | 0 | { |
484 | 0 | libcerror_error_set( |
485 | 0 | error, |
486 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
487 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
488 | 0 | "%s: unable to free value.", |
489 | 0 | function ); |
490 | |
|
491 | 0 | return( -1 ); |
492 | 0 | } |
493 | 87.1k | } |
494 | 87.1k | internal_cache_value->flags &= ~( LIBFCACHE_CACHE_VALUE_FLAG_MANAGED ); |
495 | 87.1k | } |
496 | 212k | internal_cache_value->value = value; |
497 | 212k | internal_cache_value->value_free_function = value_free_function; |
498 | 212k | internal_cache_value->flags |= flags; |
499 | | |
500 | 212k | return( 1 ); |
501 | 212k | } |
502 | | |