Coverage Report

Created: 2024-02-25 07:19

/src/libregf/libfcache/libfcache_cache.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The cache 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.h"
27
#include "libfcache_cache_value.h"
28
#include "libfcache_definitions.h"
29
#include "libfcache_libcdata.h"
30
#include "libfcache_libcerror.h"
31
#include "libfcache_types.h"
32
33
/* Creates a cache
34
 * Make sure the value cache is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libfcache_cache_initialize(
38
     libfcache_cache_t **cache,
39
     int maximum_cache_entries,
40
     libcerror_error_t **error )
41
2.74k
{
42
2.74k
  libfcache_internal_cache_t *internal_cache = NULL;
43
2.74k
  static char *function                      = "libfcache_cache_initialize";
44
45
2.74k
  if( cache == 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 cache.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
2.74k
  if( *cache != 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 cache value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
2.74k
  if( maximum_cache_entries <= 0 )
68
0
  {
69
0
    libcerror_error_set(
70
0
     error,
71
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
72
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
73
0
     "%s: invalid maximum cache entries value zero or less.",
74
0
     function );
75
76
0
    return( -1 );
77
0
  }
78
2.74k
  internal_cache = memory_allocate_structure(
79
2.74k
                    libfcache_internal_cache_t );
80
81
2.74k
  if( internal_cache == NULL )
82
0
  {
83
0
    libcerror_error_set(
84
0
     error,
85
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
86
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
87
0
     "%s: unable to create cache.",
88
0
     function );
89
90
0
    goto on_error;
91
0
  }
92
2.74k
  if( memory_set(
93
2.74k
       internal_cache,
94
2.74k
       0,
95
2.74k
       sizeof( libfcache_internal_cache_t ) ) == NULL )
96
0
  {
97
0
    libcerror_error_set(
98
0
     error,
99
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
100
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
101
0
     "%s: unable to clear cache.",
102
0
     function );
103
104
0
    memory_free(
105
0
     internal_cache );
106
107
0
    return( -1 );
108
0
  }
109
2.74k
  if( libcdata_array_initialize(
110
2.74k
       &( internal_cache->entries_array ),
111
2.74k
       maximum_cache_entries,
112
2.74k
       error ) != 1 )
113
0
  {
114
0
    libcerror_error_set(
115
0
     error,
116
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
117
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
118
0
     "%s: unable to create entries array.",
119
0
     function );
120
121
0
    goto on_error;
122
0
  }
123
2.74k
  if( libcdata_list_initialize(
124
2.74k
       &( internal_cache->entries_list ),
125
2.74k
       error ) != 1 )
126
0
  {
127
0
    libcerror_error_set(
128
0
     error,
129
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
130
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
131
0
     "%s: unable to create entries list.",
132
0
     function );
133
134
0
    goto on_error;
135
0
  }
136
2.74k
  *cache = (libfcache_cache_t *) internal_cache;
137
138
2.74k
  return( 1 );
139
140
0
on_error:
141
0
  if( internal_cache != NULL )
142
0
  {
143
0
    if( internal_cache->entries_array != NULL )
144
0
    {
145
0
      libcdata_array_free(
146
0
       &( internal_cache->entries_array ),
147
0
       NULL,
148
0
       NULL );
149
0
    }
150
0
    memory_free(
151
0
     internal_cache );
152
0
  }
153
0
  return( -1 );
154
2.74k
}
155
156
/* Frees a cache
157
 * Returns 1 if successful or -1 on error
158
 */
159
int libfcache_cache_free(
160
     libfcache_cache_t **cache,
161
     libcerror_error_t **error )
162
2.74k
{
163
2.74k
  libfcache_internal_cache_t *internal_cache = NULL;
164
2.74k
  static char *function                      = "libfcache_cache_free";
165
2.74k
  int result                                 = 1;
166
167
2.74k
  if( cache == NULL )
168
0
  {
169
0
    libcerror_error_set(
170
0
     error,
171
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
172
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
173
0
     "%s: invalid cache.",
174
0
     function );
175
176
0
    return( -1 );
177
0
  }
178
2.74k
  if( *cache != NULL )
179
2.74k
  {
180
2.74k
    internal_cache = (libfcache_internal_cache_t *) *cache;
181
2.74k
    *cache         = NULL;
182
183
2.74k
    if( libcdata_list_free(
184
2.74k
         &( internal_cache->entries_list ),
185
2.74k
         NULL,
186
2.74k
         error ) != 1 )
187
0
    {
188
0
      libcerror_error_set(
189
0
       error,
190
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
191
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
192
0
       "%s: unable to free the entries list.",
193
0
       function );
194
195
0
      result = -1;
196
0
    }
197
2.74k
    if( libcdata_array_free(
198
2.74k
         &( internal_cache->entries_array ),
199
2.74k
         (int (*)(intptr_t **, libcerror_error_t **)) &libfcache_cache_value_free,
200
2.74k
         error ) != 1 )
201
0
    {
202
0
      libcerror_error_set(
203
0
       error,
204
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
205
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
206
0
       "%s: unable to free the entries array.",
207
0
       function );
208
209
0
      result = -1;
210
0
    }
211
2.74k
    memory_free(
212
2.74k
     internal_cache );
213
2.74k
  }
214
2.74k
  return( result );
215
2.74k
}
216
217
/* Empties the cache
218
 * Returns 1 if successful or -1 on error
219
 */
220
int libfcache_cache_empty(
221
     libfcache_cache_t *cache,
222
     libcerror_error_t **error )
223
0
{
224
0
  libfcache_internal_cache_t *internal_cache = NULL;
225
0
  static char *function                      = "libfcache_cache_empty";
226
227
0
  if( cache == NULL )
228
0
  {
229
0
    libcerror_error_set(
230
0
     error,
231
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
232
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
233
0
     "%s: invalid cache.",
234
0
     function );
235
236
0
    return( -1 );
237
0
  }
238
0
  internal_cache = (libfcache_internal_cache_t *) cache;
239
240
0
  if( libcdata_list_empty(
241
0
       internal_cache->entries_list,
242
0
       NULL,
243
0
       error ) != 1 )
244
0
  {
245
0
    libcerror_error_set(
246
0
     error,
247
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
248
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
249
0
     "%s: unable to empty entries list.",
250
0
     function );
251
252
0
    return( -1 );
253
0
  }
254
0
  if( libcdata_array_clear(
255
0
       internal_cache->entries_array,
256
0
       (int (*)(intptr_t **, libcerror_error_t **)) &libfcache_cache_value_free,
257
0
       error ) != 1 )
258
0
  {
259
0
    libcerror_error_set(
260
0
     error,
261
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
262
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
263
0
     "%s: unable to clear entries array.",
264
0
     function );
265
266
0
    return( -1 );
267
0
  }
268
0
  internal_cache->number_of_cache_values = 0;
269
270
0
  return( 1 );
271
0
}
272
273
/* Clones (duplicates) the cache, not the cache values
274
 * Returns 1 if successful or -1 on error
275
 */
276
int libfcache_cache_clone(
277
     libfcache_cache_t **destination_cache,
278
     libfcache_cache_t *source_cache,
279
     libcerror_error_t **error )
280
0
{
281
0
  libfcache_internal_cache_t *internal_source_cache = NULL;
282
0
  static char *function                             = "libfcache_cache_clone";
283
0
  int number_of_cache_entries                       = 0;
284
285
0
  if( destination_cache == NULL )
286
0
  {
287
0
    libcerror_error_set(
288
0
     error,
289
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
290
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
291
0
     "%s: invalid destination cache.",
292
0
     function );
293
294
0
    return( -1 );
295
0
  }
296
0
  if( *destination_cache != NULL )
297
0
  {
298
0
    libcerror_error_set(
299
0
     error,
300
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
301
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
302
0
     "%s: destination cache already set.",
303
0
     function );
304
305
0
    return( -1 );
306
0
  }
307
0
  if( source_cache == NULL )
308
0
  {
309
0
    *destination_cache = NULL;
310
311
0
    return( 1 );
312
0
  }
313
0
  internal_source_cache = (libfcache_internal_cache_t *) source_cache;
314
315
0
  if( libcdata_array_get_number_of_entries(
316
0
       internal_source_cache->entries_array,
317
0
       &number_of_cache_entries,
318
0
       error ) != 1 )
319
0
  {
320
0
    libcerror_error_set(
321
0
     error,
322
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
323
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
324
0
     "%s: unable to retrieve number of cache entries from source entries array.",
325
0
     function );
326
327
0
    return( -1 );
328
0
  }
329
0
  if( libfcache_cache_initialize(
330
0
       destination_cache,
331
0
       number_of_cache_entries,
332
0
       error ) != 1 )
333
0
  {
334
0
    libcerror_error_set(
335
0
     error,
336
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
337
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
338
0
     "%s: unable to create destination cache.",
339
0
     function );
340
341
0
    return( -1 );
342
0
  }
343
0
  return( 1 );
344
0
}
345
346
/* Resizes the cache
347
 * Returns 1 if successful or -1 on error
348
 */
349
int libfcache_cache_resize(
350
     libfcache_cache_t *cache,
351
     int maximum_cache_entries,
352
     libcerror_error_t **error )
353
0
{
354
0
  libfcache_internal_cache_t *internal_cache = NULL;
355
0
  static char *function                      = "libfcache_cache_resize";
356
357
0
  if( cache == NULL )
358
0
  {
359
0
    libcerror_error_set(
360
0
     error,
361
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
362
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
363
0
     "%s: invalid cache.",
364
0
     function );
365
366
0
    return( -1 );
367
0
  }
368
0
  internal_cache = (libfcache_internal_cache_t *) cache;
369
370
0
  if( libcdata_array_resize(
371
0
       internal_cache->entries_array,
372
0
       maximum_cache_entries,
373
0
       (int (*)(intptr_t **, libcerror_error_t **)) &libfcache_cache_value_free,
374
0
       error ) != 1 )
375
0
  {
376
0
    libcerror_error_set(
377
0
     error,
378
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
379
0
     LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
380
0
     "%s: unable to resize entries array.",
381
0
     function );
382
383
0
    return( -1 );
384
0
  }
385
0
  return( 1 );
386
0
}
387
388
/* Retrieves the number of entries of the cache
389
 * Returns 1 if successful or -1 on error
390
 */
391
int libfcache_cache_get_number_of_entries(
392
     libfcache_cache_t *cache,
393
     int *number_of_entries,
394
     libcerror_error_t **error )
395
0
{
396
0
  libfcache_internal_cache_t *internal_cache = NULL;
397
0
  static char *function                      = "libfcache_cache_get_number_of_entries";
398
399
0
  if( cache == NULL )
400
0
  {
401
0
    libcerror_error_set(
402
0
     error,
403
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
404
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
405
0
     "%s: invalid cache.",
406
0
     function );
407
408
0
    return( -1 );
409
0
  }
410
0
  internal_cache = (libfcache_internal_cache_t *) cache;
411
412
0
  if( libcdata_array_get_number_of_entries(
413
0
       internal_cache->entries_array,
414
0
       number_of_entries,
415
0
       error ) != 1 )
416
0
  {
417
0
    libcerror_error_set(
418
0
     error,
419
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
420
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
421
0
     "%s: unable to retrieve number of entries from entries array.",
422
0
     function );
423
424
0
    return( -1 );
425
0
  }
426
0
  return( 1 );
427
0
}
428
429
/* Retrieves the number of cache values
430
 * Returns 1 if successful or -1 on error
431
 */
432
int libfcache_cache_get_number_of_cache_values(
433
     libfcache_cache_t *cache,
434
     int *number_of_cache_values,
435
     libcerror_error_t **error )
436
0
{
437
0
  libfcache_internal_cache_t *internal_cache = NULL;
438
0
  static char *function                      = "libfcache_cache_get_number_of_cache_values";
439
440
0
  if( cache == NULL )
441
0
  {
442
0
    libcerror_error_set(
443
0
     error,
444
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
445
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
446
0
     "%s: invalid cache.",
447
0
     function );
448
449
0
    return( -1 );
450
0
  }
451
0
  internal_cache = (libfcache_internal_cache_t *) cache;
452
453
0
  if( libcdata_list_get_number_of_elements(
454
0
       internal_cache->entries_list,
455
0
       number_of_cache_values,
456
0
       error ) != 1 )
457
0
  {
458
0
    libcerror_error_set(
459
0
     error,
460
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
461
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
462
0
     "%s: unable to retrieve number of elements from entries list.",
463
0
     function );
464
465
0
    return( -1 );
466
0
  }
467
0
  return( 1 );
468
0
}
469
470
/* Clears the cache value for the specific index
471
 * Returns 1 if successful or -1 on error
472
 */
473
int libfcache_cache_clear_value_by_index(
474
     libfcache_cache_t *cache,
475
     int cache_entry_index,
476
     libcerror_error_t **error )
477
0
{
478
0
  libfcache_cache_value_t *cache_value       = NULL;
479
0
  libfcache_internal_cache_t *internal_cache = NULL;
480
0
  static char *function                      = "libfcache_cache_clear_value_by_index";
481
482
0
  if( cache == NULL )
483
0
  {
484
0
    libcerror_error_set(
485
0
     error,
486
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
487
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
488
0
     "%s: invalid cache.",
489
0
     function );
490
491
0
    return( -1 );
492
0
  }
493
0
  internal_cache = (libfcache_internal_cache_t *) cache;
494
495
0
  if( libcdata_array_get_entry_by_index(
496
0
       internal_cache->entries_array,
497
0
       cache_entry_index,
498
0
       (intptr_t **) &cache_value,
499
0
       error ) != 1 )
500
0
  {
501
0
    libcerror_error_set(
502
0
     error,
503
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
504
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
505
0
     "%s: unable to retrieve cache value: %d from entries array.",
506
0
     function,
507
0
     cache_entry_index );
508
509
0
    return( -1 );
510
0
  }
511
0
  if( libfcache_cache_value_clear(
512
0
       cache_value,
513
0
       error ) != 1 )
514
0
  {
515
0
    libcerror_error_set(
516
0
     error,
517
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
518
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
519
0
     "%s: unable to clear cache value.",
520
0
     function );
521
522
0
    return( -1 );
523
0
  }
524
0
  return( 1 );
525
0
}
526
527
/* Retrieves the cache value that matches the file index, offset and timestamp
528
 * Returns 1 if successful, 0 if no such value or -1 on error
529
 */
530
int libfcache_cache_get_value_by_identifier(
531
     libfcache_cache_t *cache,
532
     int file_index,
533
     off64_t offset,
534
     int64_t timestamp,
535
     libfcache_cache_value_t **cache_value,
536
     libcerror_error_t **error )
537
8.96k
{
538
8.96k
  libcdata_list_element_t *list_element      = NULL;
539
8.96k
  libfcache_cache_value_t *safe_cache_value  = NULL;
540
8.96k
  libfcache_internal_cache_t *internal_cache = NULL;
541
8.96k
  static char *function                      = "libfcache_cache_get_value_by_identifier";
542
8.96k
  off64_t cache_value_offset                 = 0;
543
8.96k
  int64_t cache_value_timestamp              = 0;
544
8.96k
  int cache_value_file_index                 = 0;
545
8.96k
  int number_of_cache_values                 = 0;
546
8.96k
  int result                                 = 0;
547
548
8.96k
  if( cache == NULL )
549
0
  {
550
0
    libcerror_error_set(
551
0
     error,
552
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
553
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
554
0
     "%s: invalid cache.",
555
0
     function );
556
557
0
    return( -1 );
558
0
  }
559
8.96k
  internal_cache = (libfcache_internal_cache_t *) cache;
560
561
8.96k
  if( cache_value == NULL )
562
0
  {
563
0
    libcerror_error_set(
564
0
     error,
565
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
566
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
567
0
     "%s: invalid cache value.",
568
0
     function );
569
570
0
    return( -1 );
571
0
  }
572
8.96k
  *cache_value = NULL;
573
574
8.96k
  if( libcdata_list_get_number_of_elements(
575
8.96k
       internal_cache->entries_list,
576
8.96k
       &number_of_cache_values,
577
8.96k
       error ) != 1 )
578
0
  {
579
0
    libcerror_error_set(
580
0
     error,
581
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
582
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
583
0
     "%s: unable to retrieve number of elements from entries list.",
584
0
     function );
585
586
0
    return( -1 );
587
0
  }
588
8.96k
  if( libcdata_list_get_first_element(
589
8.96k
       internal_cache->entries_list,
590
8.96k
       &list_element,
591
8.96k
       error ) != 1 )
592
0
  {
593
0
    libcerror_error_set(
594
0
     error,
595
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
596
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
597
0
     "%s: unable to retrieve first list element.",
598
0
     function );
599
600
0
    return( -1 );
601
0
  }
602
12.0k
  while( list_element != NULL )
603
9.13k
  {
604
9.13k
    if( libcdata_list_element_get_value(
605
9.13k
         list_element,
606
9.13k
         (intptr_t **) &safe_cache_value,
607
9.13k
         error ) != 1 )
608
0
    {
609
0
      libcerror_error_set(
610
0
       error,
611
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
612
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
613
0
       "%s: unable to retrieve cache value from list element.",
614
0
       function );
615
616
0
      return( -1 );
617
0
    }
618
9.13k
    if( libfcache_cache_value_get_identifier(
619
9.13k
         safe_cache_value,
620
9.13k
         &cache_value_file_index,
621
9.13k
         &cache_value_offset,
622
9.13k
         &cache_value_timestamp,
623
9.13k
         error ) != 1 )
624
0
    {
625
0
      libcerror_error_set(
626
0
       error,
627
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
628
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
629
0
       "%s: unable to retrieve cache value identifier.",
630
0
       function );
631
632
0
      return( -1 );
633
0
    }
634
9.13k
    if( ( cache_value_file_index == file_index )
635
9.13k
     && ( cache_value_offset == offset )
636
9.13k
     && ( cache_value_timestamp == timestamp ) )
637
6.09k
    {
638
6.09k
      result = 1;
639
640
6.09k
      break;
641
6.09k
    }
642
3.04k
    if( libcdata_list_element_get_next_element(
643
3.04k
         list_element,
644
3.04k
         &list_element,
645
3.04k
         error ) != 1 )
646
0
    {
647
0
      libcerror_error_set(
648
0
       error,
649
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
650
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
651
0
       "%s: unable to retrieve next list element.",
652
0
       function );
653
654
0
      return( -1 );
655
0
    }
656
3.04k
  }
657
8.96k
  if( ( result != 0 )
658
8.96k
   && ( number_of_cache_values > 0 ) )
659
6.09k
  {
660
6.09k
    if( libcdata_list_remove_element(
661
6.09k
         internal_cache->entries_list,
662
6.09k
         list_element,
663
6.09k
         error ) != 1 )
664
0
    {
665
0
      libcerror_error_set(
666
0
       error,
667
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
668
0
       LIBCERROR_RUNTIME_ERROR_REMOVE_FAILED,
669
0
       "%s: unable to remove list element.",
670
0
       function );
671
672
0
      return( -1 );
673
0
    }
674
6.09k
    if( libcdata_list_prepend_element(
675
6.09k
         internal_cache->entries_list,
676
6.09k
         list_element,
677
6.09k
         error ) != 1 )
678
0
    {
679
0
      libcerror_error_set(
680
0
       error,
681
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
682
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
683
0
       "%s: unable to prepend list element.",
684
0
       function );
685
686
0
      return( -1 );
687
0
    }
688
6.09k
    *cache_value = safe_cache_value;
689
6.09k
  }
690
8.96k
  return( result );
691
8.96k
}
692
693
/* Retrieves the cache value for the specific index
694
 * Returns 1 if successful or -1 on error
695
 */
696
int libfcache_cache_get_value_by_index(
697
     libfcache_cache_t *cache,
698
     int cache_entry_index,
699
     libfcache_cache_value_t **cache_value,
700
     libcerror_error_t **error )
701
0
{
702
0
  libfcache_internal_cache_t *internal_cache = NULL;
703
0
  static char *function                      = "libfcache_cache_get_value_by_index";
704
705
0
  if( cache == NULL )
706
0
  {
707
0
    libcerror_error_set(
708
0
     error,
709
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
710
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
711
0
     "%s: invalid cache.",
712
0
     function );
713
714
0
    return( -1 );
715
0
  }
716
0
  internal_cache = (libfcache_internal_cache_t *) cache;
717
718
0
  if( libcdata_array_get_entry_by_index(
719
0
       internal_cache->entries_array,
720
0
       cache_entry_index,
721
0
       (intptr_t **) cache_value,
722
0
       error ) != 1 )
723
0
  {
724
0
    libcerror_error_set(
725
0
     error,
726
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
727
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
728
0
     "%s: unable to retrieve cache value: %d from entries array.",
729
0
     function,
730
0
     cache_entry_index );
731
732
0
    return( -1 );
733
0
  }
734
0
  return( 1 );
735
0
}
736
737
/* Sets the cache value for the file index, offset and timestamp
738
 * Returns 1 if successful or -1 on error
739
 */
740
int libfcache_cache_set_value_by_identifier(
741
     libfcache_cache_t *cache,
742
     int file_index,
743
     off64_t offset,
744
     int64_t timestamp,
745
     intptr_t *value,
746
     int (*value_free_function)(
747
            intptr_t **value,
748
            libcerror_error_t **error ),
749
     uint8_t flags,
750
     libcerror_error_t **error )
751
2.32k
{
752
2.32k
  libcdata_list_element_t *list_element      = NULL;
753
2.32k
  libfcache_cache_value_t *cache_value       = NULL;
754
2.32k
  libfcache_internal_cache_t *internal_cache = NULL;
755
2.32k
  static char *function                      = "libfcache_cache_set_value_by_identifier";
756
2.32k
  int cache_entry_index                      = 0;
757
2.32k
  int number_of_cache_entries                = 0;
758
2.32k
  int number_of_cache_values                 = 0;
759
760
2.32k
  if( cache == NULL )
761
0
  {
762
0
    libcerror_error_set(
763
0
     error,
764
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
765
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
766
0
     "%s: invalid cache.",
767
0
     function );
768
769
0
    return( -1 );
770
0
  }
771
2.32k
  internal_cache = (libfcache_internal_cache_t *) cache;
772
773
2.32k
  if( libcdata_array_get_number_of_entries(
774
2.32k
       internal_cache->entries_array,
775
2.32k
       &number_of_cache_entries,
776
2.32k
       error ) != 1 )
777
0
  {
778
0
    libcerror_error_set(
779
0
     error,
780
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
781
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
782
0
     "%s: unable to retrieve number of cache entries from entries array.",
783
0
     function );
784
785
0
    return( -1 );
786
0
  }
787
2.32k
  if( libcdata_list_get_number_of_elements(
788
2.32k
       internal_cache->entries_list,
789
2.32k
       &number_of_cache_values,
790
2.32k
       error ) != 1 )
791
0
  {
792
0
    libcerror_error_set(
793
0
     error,
794
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
795
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
796
0
     "%s: unable to retrieve number of elements from entries list.",
797
0
     function );
798
799
0
    return( -1 );
800
0
  }
801
2.32k
  if( number_of_cache_values < number_of_cache_entries )
802
2.32k
  {
803
2.32k
    cache_entry_index = number_of_cache_values;
804
805
2.32k
    if( libfcache_cache_value_initialize(
806
2.32k
         &cache_value,
807
2.32k
         error ) != 1 )
808
0
    {
809
0
      libcerror_error_set(
810
0
       error,
811
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
812
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
813
0
       "%s: unable to create cache value.",
814
0
       function );
815
816
0
      return( -1 );
817
0
    }
818
2.32k
    if( libfcache_cache_value_set_cache_index(
819
2.32k
         cache_value,
820
2.32k
         cache_entry_index,
821
2.32k
         error ) != 1 )
822
0
    {
823
0
      libcerror_error_set(
824
0
       error,
825
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
826
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
827
0
       "%s: unable to set cache index in cache value.",
828
0
       function );
829
830
0
      libfcache_cache_value_free(
831
0
       &cache_value,
832
0
       NULL );
833
834
0
      return( -1 );
835
0
    }
836
2.32k
    if( libcdata_array_set_entry_by_index(
837
2.32k
         internal_cache->entries_array,
838
2.32k
         cache_entry_index,
839
2.32k
         (intptr_t *) cache_value,
840
2.32k
         error ) != 1 )
841
0
    {
842
0
      libcerror_error_set(
843
0
       error,
844
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
845
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
846
0
       "%s: unable to set cache value: %d in entries array.",
847
0
       function,
848
0
       cache_entry_index );
849
850
0
      libfcache_cache_value_free(
851
0
       &cache_value,
852
0
       NULL );
853
854
0
      return( -1 );
855
0
    }
856
2.32k
    if( libcdata_list_prepend_value(
857
2.32k
         internal_cache->entries_list,
858
2.32k
         (intptr_t *) cache_value,
859
2.32k
         error ) != 1 )
860
0
    {
861
0
      libcerror_error_set(
862
0
       error,
863
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
864
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
865
0
       "%s: unable to prepend cache value: %d to entries list.",
866
0
       function );
867
868
0
      return( -1 );
869
0
    }
870
2.32k
  }
871
0
  else
872
0
  {
873
0
    if( libcdata_list_get_last_element(
874
0
         internal_cache->entries_list,
875
0
         &list_element,
876
0
         error ) != 1 )
877
0
    {
878
0
      libcerror_error_set(
879
0
       error,
880
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
881
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
882
0
       "%s: unable to retrieve last list element.",
883
0
       function );
884
885
0
      return( -1 );
886
0
    }
887
0
    if( libcdata_list_element_get_value(
888
0
         list_element,
889
0
         (intptr_t **) &cache_value,
890
0
         error ) != 1 )
891
0
    {
892
0
      libcerror_error_set(
893
0
       error,
894
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
895
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
896
0
       "%s: unable to retrieve cache value from list element.",
897
0
       function );
898
899
0
      return( -1 );
900
0
    }
901
0
    if( libfcache_cache_value_get_cache_index(
902
0
         cache_value,
903
0
         &cache_entry_index,
904
0
         error ) != 1 )
905
0
    {
906
0
      libcerror_error_set(
907
0
       error,
908
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
909
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
910
0
       "%s: unable to retrieve cache index from cache value.",
911
0
       function );
912
913
0
      return( -1 );
914
0
    }
915
0
    if( libcdata_array_get_entry_by_index(
916
0
         internal_cache->entries_array,
917
0
         cache_entry_index,
918
0
         (intptr_t **) &cache_value,
919
0
         error ) != 1 )
920
0
    {
921
0
      libcerror_error_set(
922
0
       error,
923
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
924
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
925
0
       "%s: unable to retrieve cache value: %d from entries array.",
926
0
       function,
927
0
       cache_entry_index );
928
929
0
      return( -1 );
930
0
    }
931
0
  }
932
2.32k
  if( libfcache_cache_value_set_identifier(
933
2.32k
       cache_value,
934
2.32k
       file_index,
935
2.32k
       offset,
936
2.32k
       timestamp,
937
2.32k
       error ) != 1 )
938
0
  {
939
0
    libcerror_error_set(
940
0
     error,
941
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
942
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
943
0
     "%s: unable to set identifier in cache value.",
944
0
     function );
945
946
0
    return( -1 );
947
0
  }
948
2.32k
  if( libfcache_cache_value_set_value(
949
2.32k
       cache_value,
950
2.32k
       value,
951
2.32k
       value_free_function,
952
2.32k
       flags,
953
2.32k
       error ) != 1 )
954
0
  {
955
0
    libcerror_error_set(
956
0
     error,
957
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
958
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
959
0
     "%s: unable to set value in cache value.",
960
0
     function );
961
962
0
    return( -1 );
963
0
  }
964
2.32k
  return( 1 );
965
2.32k
}
966
967
/* Sets the cache value for the specific index
968
 * Returns 1 if successful or -1 on error
969
 */
970
int libfcache_cache_set_value_by_index(
971
     libfcache_cache_t *cache,
972
     int cache_entry_index,
973
     int file_index,
974
     off64_t offset,
975
     int64_t timestamp,
976
     intptr_t *value,
977
     int (*value_free_function)(
978
            intptr_t **value,
979
            libcerror_error_t **error ),
980
     uint8_t flags,
981
     libcerror_error_t **error )
982
0
{
983
0
  libfcache_cache_value_t *cache_value       = NULL;
984
0
  libfcache_internal_cache_t *internal_cache = NULL;
985
0
  static char *function                      = "libfcache_cache_set_value_by_index";
986
987
0
  if( cache == NULL )
988
0
  {
989
0
    libcerror_error_set(
990
0
     error,
991
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
992
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
993
0
     "%s: invalid cache.",
994
0
     function );
995
996
0
    return( -1 );
997
0
  }
998
0
  internal_cache = (libfcache_internal_cache_t *) cache;
999
1000
0
  if( libcdata_array_get_entry_by_index(
1001
0
       internal_cache->entries_array,
1002
0
       cache_entry_index,
1003
0
       (intptr_t **) &cache_value,
1004
0
       error ) != 1 )
1005
0
  {
1006
0
    libcerror_error_set(
1007
0
     error,
1008
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1009
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1010
0
     "%s: unable to retrieve cache value: %d from entries array.",
1011
0
     function,
1012
0
     cache_entry_index );
1013
1014
0
    return( -1 );
1015
0
  }
1016
0
  if( cache_value == NULL )
1017
0
  {
1018
0
    if( libfcache_cache_value_initialize(
1019
0
         &cache_value,
1020
0
         error ) != 1 )
1021
0
    {
1022
0
      libcerror_error_set(
1023
0
       error,
1024
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1025
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1026
0
       "%s: unable to create cache value.",
1027
0
       function );
1028
1029
0
      return( -1 );
1030
0
    }
1031
0
    if( libfcache_cache_value_set_cache_index(
1032
0
         cache_value,
1033
0
         cache_entry_index,
1034
0
         error ) != 1 )
1035
0
    {
1036
0
      libcerror_error_set(
1037
0
       error,
1038
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1039
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1040
0
       "%s: unable to set cache index in cache value.",
1041
0
       function );
1042
1043
0
      libfcache_cache_value_free(
1044
0
       &cache_value,
1045
0
       NULL );
1046
1047
0
      return( -1 );
1048
0
    }
1049
0
    if( libcdata_array_set_entry_by_index(
1050
0
         internal_cache->entries_array,
1051
0
         cache_entry_index,
1052
0
         (intptr_t *) cache_value,
1053
0
         error ) != 1 )
1054
0
    {
1055
0
      libcerror_error_set(
1056
0
       error,
1057
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1058
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1059
0
       "%s: unable to set cache value: %d in entries array.",
1060
0
       function,
1061
0
       cache_entry_index );
1062
1063
0
      libfcache_cache_value_free(
1064
0
       &cache_value,
1065
0
       NULL );
1066
1067
0
      return( -1 );
1068
0
    }
1069
0
    if( libcdata_list_prepend_value(
1070
0
         internal_cache->entries_list,
1071
0
         (intptr_t *) cache_value,
1072
0
         error ) != 1 )
1073
0
    {
1074
0
      libcerror_error_set(
1075
0
       error,
1076
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1077
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1078
0
       "%s: unable to prepend cache value: %d to entries list.",
1079
0
       function );
1080
1081
0
      return( -1 );
1082
0
    }
1083
0
  }
1084
0
  if( libfcache_cache_value_set_value(
1085
0
       cache_value,
1086
0
       value,
1087
0
       value_free_function,
1088
0
       flags,
1089
0
       error ) != 1 )
1090
0
  {
1091
0
    libcerror_error_set(
1092
0
     error,
1093
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1094
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1095
0
     "%s: unable to set value in cache value.",
1096
0
     function );
1097
1098
0
    return( -1 );
1099
0
  }
1100
0
  if( libfcache_cache_value_set_identifier(
1101
0
       cache_value,
1102
0
       file_index,
1103
0
       offset,
1104
0
       timestamp,
1105
0
       error ) != 1 )
1106
0
  {
1107
0
    libcerror_error_set(
1108
0
     error,
1109
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1110
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1111
0
     "%s: unable to set identifier in cache value.",
1112
0
     function );
1113
1114
0
    return( -1 );
1115
0
  }
1116
0
  return( 1 );
1117
0
}
1118