Coverage Report

Created: 2024-02-25 07:20

/src/libevtx/libfvalue/libfvalue_data_handle.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Data handle 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 "libfvalue_data_handle.h"
27
#include "libfvalue_definitions.h"
28
#include "libfvalue_libcdata.h"
29
#include "libfvalue_libcerror.h"
30
#include "libfvalue_types.h"
31
#include "libfvalue_value_entry.h"
32
33
/* Creates a data handle
34
 * Make sure the value data_handle is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libfvalue_data_handle_initialize(
38
     libfvalue_data_handle_t **data_handle,
39
     int (*read_value_entries)(
40
            libfvalue_data_handle_t *data_handle,
41
            const uint8_t *data,
42
            size_t data_size,
43
            int encoding,
44
            uint32_t data_flags,
45
            libcerror_error_t **error ),
46
     libcerror_error_t **error )
47
1.43M
{
48
1.43M
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
49
1.43M
  static char *function                                  = "libfvalue_data_handle_initialize";
50
51
1.43M
  if( data_handle == NULL )
52
0
  {
53
0
    libcerror_error_set(
54
0
     error,
55
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
56
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
57
0
     "%s: invalid data handle.",
58
0
     function );
59
60
0
    return( -1 );
61
0
  }
62
1.43M
  if( *data_handle != NULL )
63
0
  {
64
0
    libcerror_error_set(
65
0
     error,
66
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
67
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
68
0
     "%s: invalid data handle value already set.",
69
0
     function );
70
71
0
    return( -1 );
72
0
  }
73
1.43M
  internal_data_handle = memory_allocate_structure(
74
1.43M
                          libfvalue_internal_data_handle_t );
75
76
1.43M
  if( internal_data_handle == NULL )
77
0
  {
78
0
    libcerror_error_set(
79
0
     error,
80
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
81
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
82
0
     "%s: unable to create data handle.",
83
0
     function );
84
85
0
    goto on_error;
86
0
  }
87
1.43M
  if( memory_set(
88
1.43M
       internal_data_handle,
89
1.43M
       0,
90
1.43M
       sizeof( libfvalue_internal_data_handle_t ) ) == NULL )
91
0
  {
92
0
    libcerror_error_set(
93
0
     error,
94
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
95
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
96
0
     "%s: unable to clear data handle.",
97
0
     function );
98
99
0
    goto on_error;
100
0
  }
101
1.43M
  internal_data_handle->read_value_entries = read_value_entries;
102
103
1.43M
  *data_handle = (libfvalue_data_handle_t *) internal_data_handle;
104
105
1.43M
  return( 1 );
106
107
0
on_error:
108
0
  if( internal_data_handle != NULL )
109
0
  {
110
0
    memory_free(
111
0
     internal_data_handle );
112
0
  }
113
0
  return( -1 );
114
1.43M
}
115
116
/* Frees a data handle
117
 * Returns 1 if successful or -1 on error
118
 */
119
int libfvalue_data_handle_free(
120
     libfvalue_data_handle_t **data_handle,
121
     libcerror_error_t **error )
122
1.43M
{
123
1.43M
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
124
1.43M
  static char *function                                  = "libfvalue_data_handle_free";
125
1.43M
  int result                                             = 1;
126
127
1.43M
  if( data_handle == NULL )
128
0
  {
129
0
    libcerror_error_set(
130
0
     error,
131
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
132
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
133
0
     "%s: invalid data handle.",
134
0
     function );
135
136
0
    return( -1 );
137
0
  }
138
1.43M
  if( *data_handle != NULL )
139
1.43M
  {
140
1.43M
    internal_data_handle = (libfvalue_internal_data_handle_t *) *data_handle;
141
1.43M
    *data_handle         = NULL;
142
143
1.43M
    if( internal_data_handle->value_entries != NULL )
144
2.21k
    {
145
2.21k
      if( libcdata_array_free(
146
2.21k
           &( internal_data_handle->value_entries ),
147
2.21k
           (int (*)(intptr_t **, libcerror_error_t **)) &libfvalue_value_entry_free,
148
2.21k
           error ) != 1 )
149
0
      {
150
0
        libcerror_error_set(
151
0
         error,
152
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
153
0
         LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
154
0
         "%s: unable to free value entries array.",
155
0
         function );
156
157
0
        result = -1;
158
0
      }
159
2.21k
    }
160
1.43M
    if( ( internal_data_handle->flags & LIBFVALUE_VALUE_DATA_FLAG_MANAGED ) != 0 )
161
1.35M
    {
162
1.35M
      if( internal_data_handle->data != NULL )
163
1
      {
164
1
        memory_free(
165
1
         internal_data_handle->data );
166
1
      }
167
1.35M
    }
168
1.43M
    memory_free(
169
1.43M
     internal_data_handle );
170
1.43M
  }
171
1.43M
  return( result );
172
1.43M
}
173
174
/* Clones a data handle
175
 * Returns 1 if successful or -1 on error
176
 */
177
int libfvalue_data_handle_clone(
178
     libfvalue_data_handle_t **destination_data_handle,
179
     libfvalue_data_handle_t *source_data_handle,
180
     libcerror_error_t **error )
181
0
{
182
0
  libfvalue_internal_data_handle_t *internal_source_data_handle = NULL;
183
0
  static char *function                                         = "libfvalue_data_handle_clone";
184
185
0
  if( destination_data_handle == NULL )
186
0
  {
187
0
    libcerror_error_set(
188
0
     error,
189
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
190
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
191
0
     "%s: invalid destination data handle.",
192
0
     function );
193
194
0
    return( -1 );
195
0
  }
196
0
  if( *destination_data_handle != NULL )
197
0
  {
198
0
    libcerror_error_set(
199
0
     error,
200
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
201
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
202
0
     "%s: destination data handle already set.",
203
0
     function );
204
205
0
    return( -1 );
206
0
  }
207
0
  if( source_data_handle == NULL )
208
0
  {
209
0
    *destination_data_handle = NULL;
210
211
0
    return( 1 );
212
0
  }
213
0
  internal_source_data_handle = (libfvalue_internal_data_handle_t *) source_data_handle;
214
  
215
0
  if( libfvalue_data_handle_initialize(
216
0
       destination_data_handle,
217
0
       internal_source_data_handle->read_value_entries,
218
0
       error ) != 1 )
219
0
  {
220
0
    libcerror_error_set(
221
0
     error,
222
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
223
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
224
0
     "%s: unable to create destination data handle.",
225
0
     function );
226
227
0
    goto on_error;
228
0
  }
229
0
  if( *destination_data_handle == NULL )
230
0
  {
231
0
    libcerror_error_set(
232
0
     error,
233
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
234
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
235
0
     "%s: missing destination data handle.",
236
0
     function );
237
238
0
    goto on_error;
239
0
  }
240
0
  if( internal_source_data_handle->data != NULL )
241
0
  {
242
0
    if( libfvalue_data_handle_set_data(
243
0
         *destination_data_handle,
244
0
         internal_source_data_handle->data,
245
0
         internal_source_data_handle->data_size,
246
0
         internal_source_data_handle->encoding,
247
0
         LIBFVALUE_VALUE_DATA_FLAG_MANAGED,
248
0
         error ) != 1 )
249
0
    {
250
0
      libcerror_error_set(
251
0
       error,
252
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
253
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
254
0
       "%s: unable to set data in destination data handle.",
255
0
       function );
256
257
0
      goto on_error;
258
0
    }
259
0
  }
260
0
  if( internal_source_data_handle->value_entries != NULL )
261
0
  {
262
0
    if( libcdata_array_clone(
263
0
         &( ( (libfvalue_internal_data_handle_t *) *destination_data_handle )->value_entries ),
264
0
         internal_source_data_handle->value_entries,
265
0
         (int (*)(intptr_t **, libcerror_error_t **)) &libfvalue_value_entry_free,
266
0
         (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) &libfvalue_value_entry_clone,
267
0
         error ) != 1 )
268
0
    {
269
0
      libcerror_error_set(
270
0
       error,
271
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
272
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
273
0
       "%s: unable to create destination value entries array.",
274
0
       function );
275
276
0
      goto on_error;
277
0
    }
278
0
  }
279
0
  return( 1 );
280
281
0
on_error:
282
0
  if( *destination_data_handle != NULL )
283
0
  {
284
0
    libfvalue_data_handle_free(
285
0
     destination_data_handle,
286
0
     NULL );
287
0
  }
288
0
  return( -1 );
289
0
}
290
291
/* Clears a data handle
292
 * Returns 1 if successful or -1 on error
293
 */
294
int libfvalue_data_handle_clear(
295
     libfvalue_data_handle_t *data_handle,
296
     libcerror_error_t **error )
297
1.43M
{
298
1.43M
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
299
1.43M
  static char *function                                  = "libfvalue_data_handle_clear";
300
301
1.43M
  if( data_handle == NULL )
302
0
  {
303
0
    libcerror_error_set(
304
0
     error,
305
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
306
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
307
0
     "%s: invalid data handle.",
308
0
     function );
309
310
0
    return( -1 );
311
0
  }
312
1.43M
  internal_data_handle = (libfvalue_internal_data_handle_t *) data_handle;
313
314
1.43M
  if( internal_data_handle->value_entries != NULL )
315
2.21k
  {
316
2.21k
    if( libcdata_array_empty(
317
2.21k
         internal_data_handle->value_entries,
318
2.21k
         (int (*)(intptr_t **, libcerror_error_t **)) &libfvalue_value_entry_free,
319
2.21k
         error ) != 1 )
320
0
    {
321
0
      libcerror_error_set(
322
0
       error,
323
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
324
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
325
0
       "%s: unable to empty value instances array.",
326
0
       function );
327
328
0
      return( -1 );
329
0
    }
330
2.21k
  }
331
1.43M
  if( ( internal_data_handle->flags & LIBFVALUE_VALUE_DATA_FLAG_MANAGED ) != 0 )
332
1.35M
  {
333
1.35M
    if( internal_data_handle->data != NULL )
334
1.35M
    {
335
1.35M
      memory_free(
336
1.35M
       internal_data_handle->data );
337
1.35M
    }
338
1.35M
  }
339
1.43M
  internal_data_handle->data       = NULL;
340
1.43M
  internal_data_handle->data_size  = 0;
341
1.43M
  internal_data_handle->encoding   = 0;
342
1.43M
  internal_data_handle->data_flags = 0;
343
344
1.43M
  return( 1 );
345
1.43M
}
346
347
/* Retrieves the data
348
 * Returns 1 if successful or -1 on error
349
 */
350
int libfvalue_data_handle_get_data(
351
     libfvalue_data_handle_t *data_handle,
352
     uint8_t **data,
353
     size_t *data_size,
354
     int *encoding,
355
     libcerror_error_t **error )
356
91.6k
{
357
91.6k
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
358
91.6k
  static char *function                                  = "libfvalue_data_handle_get_data";
359
360
91.6k
  if( data_handle == NULL )
361
0
  {
362
0
    libcerror_error_set(
363
0
     error,
364
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
365
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
366
0
     "%s: invalid data handle.",
367
0
     function );
368
369
0
    return( -1 );
370
0
  }
371
91.6k
  internal_data_handle = (libfvalue_internal_data_handle_t *) data_handle;
372
  
373
91.6k
  if( data == NULL )
374
0
  {
375
0
    libcerror_error_set(
376
0
     error,
377
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
378
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
379
0
     "%s: invalid data.",
380
0
     function );
381
382
0
    return( -1 );
383
0
  }
384
91.6k
  if( data_size == NULL )
385
0
  {
386
0
    libcerror_error_set(
387
0
     error,
388
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
389
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
390
0
     "%s: invalid data size.",
391
0
     function );
392
393
0
    return( -1 );
394
0
  }
395
91.6k
  if( encoding == NULL )
396
0
  {
397
0
    libcerror_error_set(
398
0
     error,
399
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
400
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
401
0
     "%s: invalid byte order.",
402
0
     function );
403
404
0
    return( -1 );
405
0
  }
406
91.6k
  *data      = internal_data_handle->data;
407
91.6k
  *data_size = internal_data_handle->data_size;
408
91.6k
  *encoding  = internal_data_handle->encoding;
409
410
91.6k
  return( 1 );
411
91.6k
}
412
413
/* Sets the data
414
 * Returns 1 if successful or -1 on error
415
 */
416
int libfvalue_data_handle_set_data(
417
     libfvalue_data_handle_t *data_handle,
418
     const uint8_t *data,
419
     size_t data_size,
420
     int encoding,
421
     uint8_t flags,
422
     libcerror_error_t **error )
423
1.37M
{
424
1.37M
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
425
1.37M
  static char *function                                  = "libfvalue_data_handle_set_data";
426
427
1.37M
  if( data_handle == NULL )
428
0
  {
429
0
    libcerror_error_set(
430
0
     error,
431
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
432
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
433
0
     "%s: invalid data handle.",
434
0
     function );
435
436
0
    return( -1 );
437
0
  }
438
1.37M
  internal_data_handle = (libfvalue_internal_data_handle_t *) data_handle;
439
440
1.37M
  if( data == NULL )
441
0
  {
442
0
    if( data_size != 0 )
443
0
    {
444
0
      libcerror_error_set(
445
0
       error,
446
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
447
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
448
0
       "%s: invalid data size value out of bounds.",
449
0
       function );
450
451
0
      return( -1 );
452
0
    }
453
0
  }
454
1.37M
  else
455
1.37M
  {
456
1.37M
    if( data_size > (size_t) SSIZE_MAX )
457
0
    {
458
0
      libcerror_error_set(
459
0
       error,
460
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
461
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
462
0
       "%s: invalid data size value exceeds maximum.",
463
0
       function );
464
465
0
      return( -1 );
466
0
    }
467
1.37M
  }
468
1.37M
  if( ( flags & ~( LIBFVALUE_VALUE_DATA_FLAG_MANAGED | LIBFVALUE_VALUE_DATA_FLAG_CLONE_BY_REFERENCE ) ) != 0 )
469
0
  {
470
0
    libcerror_error_set(
471
0
     error,
472
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
473
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
474
0
     "%s: unsupported flags: 0x%02" PRIx8 ".",
475
0
     function,
476
0
     flags );
477
478
0
    return( -1 );
479
0
  }
480
1.37M
  if( ( internal_data_handle->flags & LIBFVALUE_VALUE_DATA_FLAG_MANAGED ) != 0 )
481
9.11k
  {
482
9.11k
    if( internal_data_handle->data != NULL )
483
9.11k
    {
484
9.11k
      memory_free(
485
9.11k
       internal_data_handle->data );
486
487
9.11k
      internal_data_handle->data      = NULL;
488
9.11k
      internal_data_handle->data_size = 0;
489
9.11k
    }
490
9.11k
    internal_data_handle->flags &= ~( LIBFVALUE_VALUE_DATA_FLAG_MANAGED );
491
9.11k
  }
492
  /* Make sure empty values have data that refers to NULL
493
   */
494
1.37M
  if( ( data == NULL )
495
1.37M
   || ( data_size == 0 ) )
496
11.5k
  {
497
11.5k
    internal_data_handle->data = NULL;
498
11.5k
  }
499
1.36M
  else if( ( flags & LIBFVALUE_VALUE_DATA_FLAG_CLONE_BY_REFERENCE ) != 0 )
500
0
  {
501
0
    internal_data_handle->data = (uint8_t *) data;
502
503
0
    if( ( flags & LIBFVALUE_VALUE_DATA_FLAG_MANAGED ) != 0 )
504
0
    {
505
0
      internal_data_handle->flags |= LIBFVALUE_VALUE_DATA_FLAG_MANAGED;
506
0
    }
507
0
  }
508
1.36M
  else
509
1.36M
  {
510
1.36M
    internal_data_handle->data = (uint8_t *) memory_allocate(
511
1.36M
                                              sizeof( uint8_t ) * data_size );
512
513
1.36M
    if( internal_data_handle->data == NULL )
514
0
    {
515
0
      libcerror_error_set(
516
0
       error,
517
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
518
0
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
519
0
       "%s: unable to create data.",
520
0
       function );
521
522
0
      goto on_error;
523
0
    }
524
1.36M
    if( memory_copy(
525
1.36M
         internal_data_handle->data,
526
1.36M
         data,
527
1.36M
         data_size ) == NULL )
528
0
    {
529
0
      libcerror_error_set(
530
0
       error,
531
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
532
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
533
0
       "%s: unable to copy data.",
534
0
       function );
535
536
0
      goto on_error;
537
0
    }
538
1.36M
    internal_data_handle->flags |= LIBFVALUE_VALUE_DATA_FLAG_MANAGED;
539
1.36M
  }
540
1.37M
  internal_data_handle->data_size = data_size;
541
1.37M
  internal_data_handle->encoding  = encoding;
542
543
1.37M
  return( 1 );
544
545
0
on_error:
546
0
  if( internal_data_handle->data != NULL )
547
0
  {
548
0
    memory_free(
549
0
     internal_data_handle->data );
550
551
0
    internal_data_handle->data = NULL;
552
0
  }
553
0
  return( -1 );
554
1.37M
}
555
556
/* Retrieves the data flags
557
 * Returns 1 if successful or -1 on error
558
 */
559
int libfvalue_data_handle_get_data_flags(
560
     libfvalue_data_handle_t *data_handle,
561
     uint32_t *data_flags,
562
     libcerror_error_t **error )
563
0
{
564
0
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
565
0
  static char *function                                  = "libfvalue_data_handle_get_data_flags";
566
567
0
  if( data_handle == NULL )
568
0
  {
569
0
    libcerror_error_set(
570
0
     error,
571
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
572
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
573
0
     "%s: invalid data handle.",
574
0
     function );
575
576
0
    return( -1 );
577
0
  }
578
0
  internal_data_handle = (libfvalue_internal_data_handle_t *) data_handle;
579
  
580
0
  if( data_flags == NULL )
581
0
  {
582
0
    libcerror_error_set(
583
0
     error,
584
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
585
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
586
0
     "%s: invalid data flags.",
587
0
     function );
588
589
0
    return( -1 );
590
0
  }
591
0
  *data_flags = internal_data_handle->data_flags;
592
593
0
  return( 1 );
594
0
}
595
596
/* Sets the data flags
597
 * Returns 1 if successful or -1 on error
598
 */
599
int libfvalue_data_handle_set_data_flags(
600
     libfvalue_data_handle_t *data_handle,
601
     uint32_t data_flags,
602
     libcerror_error_t **error )
603
77
{
604
77
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
605
77
  static char *function                                  = "libfvalue_data_handle_set_data_flags";
606
607
77
  if( data_handle == NULL )
608
0
  {
609
0
    libcerror_error_set(
610
0
     error,
611
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
612
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
613
0
     "%s: invalid data handle.",
614
0
     function );
615
616
0
    return( -1 );
617
0
  }
618
77
  internal_data_handle = (libfvalue_internal_data_handle_t *) data_handle;
619
620
77
  internal_data_handle->data_flags = data_flags;
621
622
77
  return( 1 );
623
77
}
624
625
/* Retrieves the number of value entries
626
 * Returns if successful or -1 on error
627
 */
628
int libfvalue_data_handle_get_number_of_value_entries(
629
     libfvalue_data_handle_t *data_handle,
630
     int *number_of_value_entries,
631
     libcerror_error_t **error )
632
243k
{
633
243k
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
634
243k
  static char *function                                  = "libfvalue_data_handle_get_number_of_value_entries";
635
636
243k
  if( data_handle == NULL )
637
0
  {
638
0
    libcerror_error_set(
639
0
     error,
640
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
641
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
642
0
     "%s: invalid data handle.",
643
0
     function );
644
645
0
    return( -1 );
646
0
  }
647
243k
  internal_data_handle = (libfvalue_internal_data_handle_t *) data_handle;
648
  
649
243k
  if( number_of_value_entries == 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 number of value entries.",
656
0
     function );
657
658
0
    return( -1 );
659
0
  }
660
243k
  if( internal_data_handle->data == NULL )
661
38.9k
  {
662
38.9k
    *number_of_value_entries = 0;
663
38.9k
  }
664
204k
  else if( internal_data_handle->value_entries == NULL )
665
2.21k
  {
666
2.21k
    *number_of_value_entries = 1;
667
2.21k
  }
668
202k
  else
669
202k
  {
670
202k
    if( libcdata_array_get_number_of_entries(
671
202k
         internal_data_handle->value_entries,
672
202k
         number_of_value_entries,
673
202k
         error ) != 1 )
674
0
    {
675
0
      libcerror_error_set(
676
0
       error,
677
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
678
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
679
0
       "%s: unable to retrieve number of entries from value entries array.",
680
0
       function );
681
682
0
      return( -1 );
683
0
    }
684
202k
  }
685
243k
  return( 1 );
686
243k
}
687
688
/* Retrieves a specific value entry
689
 * Returns if successful or -1 on error
690
 */
691
int libfvalue_data_handle_get_value_entry(
692
     libfvalue_data_handle_t *data_handle,
693
     int value_entry_index,
694
     size_t *value_entry_offset,
695
     size_t *value_entry_size,
696
     libcerror_error_t **error )
697
91.6k
{
698
91.6k
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
699
91.6k
  libfvalue_value_entry_t *value_entry                   = NULL;
700
91.6k
  static char *function                                  = "libfvalue_data_handle_get_value_entry";
701
702
91.6k
  if( data_handle == NULL )
703
0
  {
704
0
    libcerror_error_set(
705
0
     error,
706
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
707
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
708
0
     "%s: invalid data handle.",
709
0
     function );
710
711
0
    return( -1 );
712
0
  }
713
91.6k
  internal_data_handle = (libfvalue_internal_data_handle_t *) data_handle;
714
715
91.6k
  if( internal_data_handle->data == NULL )
716
0
  {
717
0
    libcerror_error_set(
718
0
     error,
719
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
720
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
721
0
     "%s: invalid data handle - missing data.",
722
0
     function );
723
724
0
    return( -1 );
725
0
  }
726
91.6k
  if( value_entry_offset == NULL )
727
0
  {
728
0
    libcerror_error_set(
729
0
     error,
730
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
731
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
732
0
     "%s: invalid value entry offset.",
733
0
     function );
734
735
0
    return( -1 );
736
0
  }
737
91.6k
  if( value_entry_size == NULL )
738
0
  {
739
0
    libcerror_error_set(
740
0
     error,
741
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
742
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
743
0
     "%s: invalid value entry size.",
744
0
     function );
745
746
0
    return( -1 );
747
0
  }
748
91.6k
  if( internal_data_handle->value_entries == NULL )
749
91.6k
  {
750
91.6k
    *value_entry_offset = 0;
751
91.6k
    *value_entry_size   = internal_data_handle->data_size;
752
91.6k
  }
753
0
  else
754
0
  {
755
0
    if( libcdata_array_get_entry_by_index(
756
0
         internal_data_handle->value_entries,
757
0
         value_entry_index,
758
0
         (intptr_t **) &value_entry,
759
0
         error ) != 1 )
760
0
    {
761
0
      libcerror_error_set(
762
0
       error,
763
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
764
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
765
0
       "%s: unable to retrieve entry: %d from values entries array.",
766
0
       function,
767
0
       value_entry_index );
768
769
0
      return( -1 );
770
0
    }
771
0
    if( value_entry == NULL )
772
0
    {
773
0
      libcerror_error_set(
774
0
       error,
775
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
776
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
777
0
       "%s: missing value entry: %d.",
778
0
       function,
779
0
       value_entry_index );
780
781
0
      return( -1 );
782
0
    }
783
0
    *value_entry_offset = value_entry->offset;
784
0
    *value_entry_size   = value_entry->size;
785
0
  }
786
91.6k
  return( 1 );
787
91.6k
}
788
789
/* Sets a specific value entry
790
 * Returns if successful or -1 on error
791
 */
792
int libfvalue_data_handle_set_value_entry(
793
     libfvalue_data_handle_t *data_handle,
794
     int value_entry_index,
795
     size_t value_entry_offset,
796
     size_t value_entry_size,
797
     libcerror_error_t **error )
798
0
{
799
0
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
800
0
  libfvalue_value_entry_t *value_entry                   = NULL;
801
0
  static char *function                                  = "libfvalue_data_handle_set_value_entry";
802
803
0
  if( data_handle == NULL )
804
0
  {
805
0
    libcerror_error_set(
806
0
     error,
807
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
808
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
809
0
     "%s: invalid data handle.",
810
0
     function );
811
812
0
    return( -1 );
813
0
  }
814
0
  internal_data_handle = (libfvalue_internal_data_handle_t *) data_handle;
815
816
0
  if( internal_data_handle->data == NULL )
817
0
  {
818
0
    libcerror_error_set(
819
0
     error,
820
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
821
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
822
0
     "%s: invalid data handle - missing data.",
823
0
     function );
824
825
0
    return( -1 );
826
0
  }
827
0
  if( value_entry_index != 0 )  
828
0
  {
829
0
    libcerror_error_set(
830
0
     error,
831
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
832
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
833
0
     "%s: invalid value entry index value out of bounds.",
834
0
     function );
835
836
0
    return( -1 );
837
0
  }
838
0
  if( value_entry_offset > (size_t) SSIZE_MAX )
839
0
  {
840
0
    libcerror_error_set(
841
0
     error,
842
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
843
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
844
0
     "%s: invalid value entry offset value exceeds maximum.",
845
0
     function );
846
847
0
    return( -1 );
848
0
  }
849
0
  if( value_entry_size > (size_t) SSIZE_MAX )
850
0
  {
851
0
    libcerror_error_set(
852
0
     error,
853
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
854
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
855
0
     "%s: invalid value entry size value exceeds maximum.",
856
0
     function );
857
858
0
    return( -1 );
859
0
  }
860
0
  if( value_entry_offset > internal_data_handle->data_size )
861
0
  {
862
0
    libcerror_error_set(
863
0
     error,
864
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
865
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
866
0
     "%s: invalid value entry offset value out of bounds.",
867
0
     function );
868
869
0
    return( -1 );
870
0
  }
871
0
  if( ( value_entry_size > internal_data_handle->data_size )
872
0
   || ( value_entry_offset > ( internal_data_handle->data_size - value_entry_size ) ) )
873
0
  {
874
0
    libcerror_error_set(
875
0
     error,
876
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
877
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
878
0
     "%s: invalid value entry size value out of bounds.",
879
0
     function );
880
881
0
    return( -1 );
882
0
  }
883
0
  if( internal_data_handle->value_entries == NULL )
884
0
  {
885
0
    if( value_entry_index != 0 )
886
0
    {
887
0
      libcerror_error_set(
888
0
       error,
889
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
890
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
891
0
       "%s: invalid value index value out of bounds.",
892
0
       function );
893
894
0
      return( -1 );
895
0
    }
896
0
    if( ( value_entry_offset != 0 )
897
0
     || ( value_entry_size != internal_data_handle->data_size ) )
898
0
    {
899
0
      if( libfvalue_value_entry_initialize(
900
0
           &value_entry,
901
0
           error ) != 1 )
902
0
      {
903
0
        libcerror_error_set(
904
0
         error,
905
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
906
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
907
0
         "%s: unable to create value entry.",
908
0
         function );
909
910
0
        return( -1 );
911
0
      }
912
0
      value_entry->offset = value_entry_offset;
913
0
      value_entry->size   = value_entry_size;
914
915
0
      if( libcdata_array_initialize(
916
0
           &( internal_data_handle->value_entries ),
917
0
           1,
918
0
           error ) != 1 )
919
0
      {
920
0
        libcerror_error_set(
921
0
         error,
922
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
923
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
924
0
         "%s: unable to create value entries array.",
925
0
         function );
926
927
0
        libfvalue_value_entry_free(
928
0
         &value_entry,
929
0
         NULL );
930
931
0
        return( -1 );
932
0
      }
933
0
      if( libcdata_array_set_entry_by_index(
934
0
           internal_data_handle->value_entries,
935
0
           0,
936
0
           (intptr_t *) value_entry,
937
0
           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 entry: 0 in values entries array.",
944
0
         function );
945
946
0
        libfvalue_value_entry_free(
947
0
         &value_entry,
948
0
         NULL );
949
950
0
        libcdata_array_free(
951
0
         &( internal_data_handle->value_entries ),
952
0
         NULL,
953
0
         NULL );
954
955
0
        return( -1 );
956
0
      }
957
0
      value_entry = NULL;
958
0
    }
959
0
  }
960
0
  else
961
0
  {
962
0
    if( libcdata_array_get_entry_by_index(
963
0
         internal_data_handle->value_entries,
964
0
         value_entry_index,
965
0
         (intptr_t **) &value_entry,
966
0
         error ) != 1 )
967
0
    {
968
0
      libcerror_error_set(
969
0
       error,
970
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
971
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
972
0
       "%s: unable to retrieve entry: %d from values entries array.",
973
0
       function,
974
0
       value_entry_index );
975
976
0
      return( -1 );
977
0
    }
978
0
    if( value_entry == NULL )
979
0
    {
980
0
      libcerror_error_set(
981
0
       error,
982
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
983
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
984
0
       "%s: missing value entry.",
985
0
       function );
986
987
0
      return( -1 );
988
0
    }
989
0
    value_entry->offset = value_entry_offset;
990
0
    value_entry->size   = value_entry_size;
991
0
  }
992
0
  return( 1 );
993
0
}
994
995
/* Appends a value entry
996
 * Returns if successful or -1 on error
997
 */
998
int libfvalue_data_handle_append_value_entry(
999
     libfvalue_data_handle_t *data_handle,
1000
     int *value_entry_index,
1001
     size_t value_entry_offset,
1002
     size_t value_entry_size,
1003
     libcerror_error_t **error )
1004
0
{
1005
0
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
1006
0
  libfvalue_value_entry_t *value_entry                   = NULL;
1007
0
  static char *function                                  = "libfvalue_data_handle_append_value_entry";
1008
1009
0
  if( data_handle == NULL )
1010
0
  {
1011
0
    libcerror_error_set(
1012
0
     error,
1013
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1014
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1015
0
     "%s: invalid data handle.",
1016
0
     function );
1017
1018
0
    return( -1 );
1019
0
  }
1020
0
  internal_data_handle = (libfvalue_internal_data_handle_t *) data_handle;
1021
1022
0
  if( value_entry_index == NULL )
1023
0
  {
1024
0
    libcerror_error_set(
1025
0
     error,
1026
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1027
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1028
0
     "%s: invalid value entry index.",
1029
0
     function );
1030
1031
0
    return( -1 );
1032
0
  }
1033
0
  if( value_entry_offset > (size_t) SSIZE_MAX )
1034
0
  {
1035
0
    libcerror_error_set(
1036
0
     error,
1037
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1038
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1039
0
     "%s: invalid value entry offset value exceeds maximum.",
1040
0
     function );
1041
1042
0
    return( -1 );
1043
0
  }
1044
0
  if( value_entry_size > (size_t) SSIZE_MAX )
1045
0
  {
1046
0
    libcerror_error_set(
1047
0
     error,
1048
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1049
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1050
0
     "%s: invalid value entry size value exceeds maximum.",
1051
0
     function );
1052
1053
0
    return( -1 );
1054
0
  }
1055
0
  if( value_entry_offset > internal_data_handle->data_size )
1056
0
  {
1057
0
    libcerror_error_set(
1058
0
     error,
1059
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1060
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1061
0
     "%s: invalid value entry offset value out of bounds.",
1062
0
     function );
1063
1064
0
    return( -1 );
1065
0
  }
1066
0
  if( ( value_entry_size > internal_data_handle->data_size )
1067
0
   || ( value_entry_offset > ( internal_data_handle->data_size - value_entry_size ) ) )
1068
0
  {
1069
0
    libcerror_error_set(
1070
0
     error,
1071
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1072
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1073
0
     "%s: invalid value entry size value out of bounds.",
1074
0
     function );
1075
1076
0
    return( -1 );
1077
0
  }
1078
0
  if( internal_data_handle->value_entries == NULL )
1079
0
  {
1080
0
    if( libcdata_array_initialize(
1081
0
         &( internal_data_handle->value_entries ),
1082
0
         0,
1083
0
         error ) != 1 )
1084
0
    {
1085
0
      libcerror_error_set(
1086
0
       error,
1087
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1088
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1089
0
       "%s: unable to create value entries array.",
1090
0
       function );
1091
1092
0
      goto on_error;
1093
0
    }
1094
0
  }
1095
0
  if( libfvalue_value_entry_initialize(
1096
0
       &value_entry,
1097
0
       error ) != 1 )
1098
0
  {
1099
0
    libcerror_error_set(
1100
0
     error,
1101
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1102
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1103
0
     "%s: unable to create value entry.",
1104
0
     function );
1105
1106
0
    goto on_error;
1107
0
  }
1108
0
  value_entry->offset = value_entry_offset;
1109
0
  value_entry->size   = value_entry_size;
1110
1111
0
  if( libcdata_array_append_entry(
1112
0
       internal_data_handle->value_entries,
1113
0
       value_entry_index,
1114
0
       (intptr_t *) value_entry,
1115
0
       error ) != 1 )
1116
0
  {
1117
0
    libcerror_error_set(
1118
0
     error,
1119
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1120
0
     LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1121
0
     "%s: unable to append entry to values entries array.",
1122
0
     function );
1123
1124
0
    goto on_error;
1125
0
  }
1126
0
  value_entry = NULL;
1127
1128
0
  return( 1 );
1129
1130
0
on_error:
1131
0
  if( value_entry != NULL )
1132
0
  {
1133
0
    libfvalue_value_entry_free(
1134
0
     &value_entry,
1135
0
     NULL );
1136
0
  }
1137
0
  return( -1 );
1138
0
}
1139
1140
/* Retrieves the data of a specific value entry
1141
 * Returns if successful or -1 on error
1142
 */
1143
int libfvalue_data_handle_get_value_entry_data(
1144
     libfvalue_data_handle_t *data_handle,
1145
     int value_entry_index,
1146
     uint8_t **value_entry_data,
1147
     size_t *value_entry_data_size,
1148
     int *encoding,
1149
     libcerror_error_t **error )
1150
91.6k
{
1151
91.6k
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
1152
91.6k
  static char *function                                  = "libfvalue_data_handle_get_value_entry_data";
1153
91.6k
  size_t value_entry_offset                              = 0;
1154
91.6k
  size_t value_entry_size                                = 0;
1155
1156
91.6k
  if( data_handle == NULL )
1157
0
  {
1158
0
    libcerror_error_set(
1159
0
     error,
1160
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1161
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1162
0
     "%s: invalid data handle.",
1163
0
     function );
1164
1165
0
    return( -1 );
1166
0
  }
1167
91.6k
  internal_data_handle = (libfvalue_internal_data_handle_t *) data_handle;
1168
1169
91.6k
  if( internal_data_handle->data == NULL )
1170
0
  {
1171
0
    libcerror_error_set(
1172
0
     error,
1173
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1174
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1175
0
     "%s: invalid data handle - missing data.",
1176
0
     function );
1177
1178
0
    return( -1 );
1179
0
  }
1180
91.6k
  if( value_entry_data == NULL )
1181
0
  {
1182
0
    libcerror_error_set(
1183
0
     error,
1184
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1185
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1186
0
     "%s: invalid value entry data.",
1187
0
     function );
1188
1189
0
    return( -1 );
1190
0
  }
1191
91.6k
  if( value_entry_data_size == NULL )
1192
0
  {
1193
0
    libcerror_error_set(
1194
0
     error,
1195
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1196
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1197
0
     "%s: invalid value entry data size.",
1198
0
     function );
1199
1200
0
    return( -1 );
1201
0
  }
1202
91.6k
  if( encoding == NULL )
1203
0
  {
1204
0
    libcerror_error_set(
1205
0
     error,
1206
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1207
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1208
0
     "%s: invalid encoding.",
1209
0
     function );
1210
1211
0
    return( -1 );
1212
0
  }
1213
91.6k
  if( libfvalue_data_handle_get_value_entry(
1214
91.6k
       data_handle,
1215
91.6k
       value_entry_index,
1216
91.6k
       &value_entry_offset,
1217
91.6k
       &value_entry_size,
1218
91.6k
       error ) != 1 )
1219
0
  {
1220
0
    libcerror_error_set(
1221
0
     error,
1222
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1223
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1224
0
     "%s: unable to retrieve value entry: %d.",
1225
0
     function,
1226
0
     value_entry_index );
1227
1228
0
    return( -1 );
1229
0
  }
1230
91.6k
  if( value_entry_offset > internal_data_handle->data_size )
1231
0
  {
1232
0
    libcerror_error_set(
1233
0
     error,
1234
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1235
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1236
0
     "%s: value entry: %d offset out of bounds.",
1237
0
     function,
1238
0
     value_entry_index );
1239
1240
0
    return( -1 );
1241
0
  }
1242
91.6k
  if( ( value_entry_size > internal_data_handle->data_size )
1243
91.6k
   || ( value_entry_offset > ( internal_data_handle->data_size - value_entry_size ) ) )
1244
0
  {
1245
0
    libcerror_error_set(
1246
0
     error,
1247
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1248
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1249
0
     "%s: value entry: %d size out of bounds.",
1250
0
     function,
1251
0
     value_entry_index );
1252
1253
0
    return( -1 );
1254
0
  }
1255
91.6k
  if( value_entry_size == 0 )
1256
0
  {
1257
0
    *value_entry_data = NULL;
1258
0
  }
1259
91.6k
  else
1260
91.6k
  {
1261
91.6k
    *value_entry_data = &( ( internal_data_handle->data )[ value_entry_offset ] );
1262
91.6k
  }
1263
91.6k
  *value_entry_data_size = value_entry_size;
1264
91.6k
  *encoding              = internal_data_handle->encoding;
1265
1266
91.6k
  return( 1 );
1267
91.6k
}
1268
1269
/* Sets the data of a specific value entry
1270
 * Returns if successful or -1 on error
1271
 */
1272
int libfvalue_data_handle_set_value_entry_data(
1273
     libfvalue_data_handle_t *data_handle,
1274
     int value_entry_index,
1275
     const uint8_t *value_entry_data,
1276
     size_t value_entry_data_size,
1277
     int encoding,
1278
     libcerror_error_t **error )
1279
0
{
1280
0
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
1281
0
  static char *function                                  = "libfvalue_data_handle_set_value_entry_data";
1282
0
  size_t value_entry_offset                              = 0;
1283
0
  size_t value_entry_size                                = 0;
1284
1285
0
  if( data_handle == NULL )
1286
0
  {
1287
0
    libcerror_error_set(
1288
0
     error,
1289
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1290
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1291
0
     "%s: invalid data handle.",
1292
0
     function );
1293
1294
0
    return( -1 );
1295
0
  }
1296
0
  internal_data_handle = (libfvalue_internal_data_handle_t *) data_handle;
1297
1298
0
  if( internal_data_handle->data == NULL )
1299
0
  {
1300
0
    libcerror_error_set(
1301
0
     error,
1302
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1303
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1304
0
     "%s: invalid data handle - missing data.",
1305
0
     function );
1306
1307
0
    return( -1 );
1308
0
  }
1309
/* TODO remove limitation */
1310
0
  if( value_entry_index != 0 )  
1311
0
  {
1312
0
    libcerror_error_set(
1313
0
     error,
1314
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1315
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1316
0
     "%s: invalid value entry index value out of bounds.",
1317
0
     function );
1318
1319
0
    return( -1 );
1320
0
  }
1321
0
  if( value_entry_data == NULL )
1322
0
  {
1323
0
    libcerror_error_set(
1324
0
     error,
1325
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1326
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1327
0
     "%s: invalid value entry data.",
1328
0
     function );
1329
1330
0
    return( -1 );
1331
0
  }
1332
0
  if( value_entry_data_size > (size_t) SSIZE_MAX )
1333
0
  {
1334
0
    libcerror_error_set(
1335
0
     error,
1336
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1337
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1338
0
     "%s: invalid value entry data size value exceeds maximum.",
1339
0
     function );
1340
1341
0
    return( -1 );
1342
0
  }
1343
0
  if( encoding != internal_data_handle->encoding )
1344
0
  {
1345
0
    libcerror_error_set(
1346
0
     error,
1347
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1348
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1349
0
     "%s: invalid encoding value out of bounds.",
1350
0
     function );
1351
1352
0
    return( -1 );
1353
0
  }
1354
0
  if( libfvalue_data_handle_get_value_entry(
1355
0
       data_handle,
1356
0
       value_entry_index,
1357
0
       &value_entry_offset,
1358
0
       &value_entry_size,
1359
0
       error ) != 1 )
1360
0
  {
1361
0
    libcerror_error_set(
1362
0
     error,
1363
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1364
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1365
0
     "%s: unable to retrieve value entry: %d.",
1366
0
     function,
1367
0
     value_entry_index );
1368
1369
0
    return( -1 );
1370
0
  }
1371
0
  if( value_entry_offset > internal_data_handle->data_size )
1372
0
  {
1373
0
    libcerror_error_set(
1374
0
     error,
1375
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1376
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1377
0
     "%s: value entry: %d offset out of bounds.",
1378
0
     function,
1379
0
     value_entry_index );
1380
1381
0
    return( -1 );
1382
0
  }
1383
0
  if( ( value_entry_size > internal_data_handle->data_size )
1384
0
   || ( value_entry_offset > ( internal_data_handle->data_size - value_entry_size ) ) )
1385
0
  {
1386
0
    libcerror_error_set(
1387
0
     error,
1388
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1389
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1390
0
     "%s: value entry: %d size out of bounds.",
1391
0
     function,
1392
0
     value_entry_index );
1393
1394
0
    return( -1 );
1395
0
  }
1396
0
  if( value_entry_data_size != value_entry_size )
1397
0
  {
1398
0
    libcerror_error_set(
1399
0
     error,
1400
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1401
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1402
0
     "%s: invalid value entry data size value out of bounds.",
1403
0
     function );
1404
1405
0
    return( -1 );
1406
0
  }
1407
0
  if( value_entry_size > 0 )
1408
0
  {
1409
0
    if( memory_copy(
1410
0
         &( ( internal_data_handle->data )[ value_entry_offset ] ),
1411
0
         value_entry_data,
1412
0
         value_entry_size ) == NULL )
1413
0
    {
1414
0
      libcerror_error_set(
1415
0
       error,
1416
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1417
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1418
0
       "%s: unable to copy value entry data.",
1419
0
       function );
1420
1421
0
      return( -1 );
1422
0
    }
1423
0
  }
1424
0
  return( 1 );
1425
0
}
1426
1427
/* Appends the data of a value entry
1428
 * Returns if successful or -1 on error
1429
 */
1430
int libfvalue_data_handle_append_value_entry_data(
1431
     libfvalue_data_handle_t *data_handle,
1432
     int *value_entry_index,
1433
     const uint8_t *value_entry_data,
1434
     size_t value_entry_data_size,
1435
     int encoding,
1436
     libcerror_error_t **error )
1437
243k
{
1438
243k
  libfvalue_internal_data_handle_t *internal_data_handle = NULL;
1439
243k
  libfvalue_value_entry_t *value_entry                   = NULL;
1440
243k
  static char *function                                  = "libfvalue_data_handle_append_value_entry_data";
1441
243k
  void *reallocation                                     = NULL;
1442
243k
  size_t reallocation_data_size                          = 0;
1443
1444
243k
  if( data_handle == NULL )
1445
0
  {
1446
0
    libcerror_error_set(
1447
0
     error,
1448
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1449
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1450
0
     "%s: invalid data handle.",
1451
0
     function );
1452
1453
0
    return( -1 );
1454
0
  }
1455
243k
  internal_data_handle = (libfvalue_internal_data_handle_t *) data_handle;
1456
1457
243k
  if( value_entry_index == NULL )
1458
0
  {
1459
0
    libcerror_error_set(
1460
0
     error,
1461
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1462
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1463
0
     "%s: invalid value entry index.",
1464
0
     function );
1465
1466
0
    return( -1 );
1467
0
  }
1468
243k
  if( value_entry_data == NULL )
1469
0
  {
1470
0
    libcerror_error_set(
1471
0
     error,
1472
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1473
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1474
0
     "%s: invalid value entry data.",
1475
0
     function );
1476
1477
0
    return( -1 );
1478
0
  }
1479
243k
  if( value_entry_data_size > (size_t) SSIZE_MAX )
1480
0
  {
1481
0
    libcerror_error_set(
1482
0
     error,
1483
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1484
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1485
0
     "%s: invalid value entry data size value exceeds maximum.",
1486
0
     function );
1487
1488
0
    return( -1 );
1489
0
  }
1490
243k
  if( internal_data_handle->data == NULL )
1491
38.9k
  {
1492
38.9k
    if( libfvalue_data_handle_set_data(
1493
38.9k
         data_handle,
1494
38.9k
         value_entry_data,
1495
38.9k
         value_entry_data_size,
1496
38.9k
         encoding,
1497
38.9k
         LIBFVALUE_VALUE_DATA_FLAG_MANAGED,
1498
38.9k
         error ) != 1 )
1499
0
    {
1500
0
      libcerror_error_set(
1501
0
       error,
1502
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1503
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1504
0
       "%s: unable to set data in data handle.",
1505
0
       function );
1506
1507
0
      goto on_error;
1508
0
    }
1509
38.9k
    internal_data_handle->encoding = encoding;
1510
38.9k
  }
1511
204k
  else
1512
204k
  {
1513
204k
    if( encoding != internal_data_handle->encoding )
1514
0
    {
1515
0
      libcerror_error_set(
1516
0
       error,
1517
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1518
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1519
0
       "%s: invalid encoding value out of bounds.",
1520
0
       function );
1521
1522
0
      return( -1 );
1523
0
    }
1524
204k
    if( internal_data_handle->data_size > ( (size_t) SSIZE_MAX - value_entry_data_size ) )
1525
0
    {
1526
0
      libcerror_error_set(
1527
0
       error,
1528
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1529
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1530
0
       "%s: invalid value entry data size value exceeds maximum.",
1531
0
       function );
1532
1533
0
      return( -1 );
1534
0
    }
1535
204k
    if( internal_data_handle->value_entries == NULL )
1536
2.21k
    {
1537
2.21k
      if( libcdata_array_initialize(
1538
2.21k
           &( internal_data_handle->value_entries ),
1539
2.21k
           1,
1540
2.21k
           error ) != 1 )
1541
0
      {
1542
0
        libcerror_error_set(
1543
0
         error,
1544
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1545
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1546
0
         "%s: unable to create value entries array.",
1547
0
         function );
1548
1549
0
        goto on_error;
1550
0
      }
1551
2.21k
      if( libfvalue_value_entry_initialize(
1552
2.21k
           &value_entry,
1553
2.21k
           error ) != 1 )
1554
0
      {
1555
0
        libcerror_error_set(
1556
0
         error,
1557
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1558
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1559
0
         "%s: unable to create value entry.",
1560
0
         function );
1561
1562
0
        goto on_error;
1563
0
      }
1564
2.21k
      value_entry->offset = 0;
1565
2.21k
      value_entry->size   = internal_data_handle->data_size;
1566
1567
2.21k
      if( libcdata_array_set_entry_by_index(
1568
2.21k
           internal_data_handle->value_entries,
1569
2.21k
           0,
1570
2.21k
           (intptr_t *) value_entry,
1571
2.21k
           error ) != 1 )
1572
0
      {
1573
0
        libcerror_error_set(
1574
0
         error,
1575
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1576
0
         LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1577
0
         "%s: unable to set entry: 0 in values entries array.",
1578
0
         function );
1579
1580
0
        goto on_error;
1581
0
      }
1582
2.21k
      value_entry = NULL;
1583
2.21k
    }
1584
204k
    if( libfvalue_value_entry_initialize(
1585
204k
         &value_entry,
1586
204k
         error ) != 1 )
1587
0
    {
1588
0
      libcerror_error_set(
1589
0
       error,
1590
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1591
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1592
0
       "%s: unable to create value entry.",
1593
0
       function );
1594
1595
0
      goto on_error;
1596
0
    }
1597
204k
    value_entry->offset    = internal_data_handle->data_size;
1598
204k
    value_entry->size      = value_entry_data_size;
1599
204k
    reallocation_data_size = internal_data_handle->data_size + value_entry_data_size;
1600
1601
204k
    reallocation = memory_reallocate(
1602
204k
                    internal_data_handle->data,
1603
204k
                    reallocation_data_size );
1604
1605
204k
    if( reallocation == NULL )
1606
0
    {
1607
0
      libcerror_error_set(
1608
0
       error,
1609
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1610
0
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1611
0
       "%s: unable to resize array entries.",
1612
0
       function );
1613
1614
0
      goto on_error;
1615
0
    }
1616
204k
    internal_data_handle->data      = (uint8_t *) reallocation;
1617
204k
    internal_data_handle->data_size = reallocation_data_size;
1618
1619
204k
    if( memory_copy(
1620
204k
         &( ( internal_data_handle->data )[ value_entry->offset ] ),
1621
204k
         value_entry_data,
1622
204k
         value_entry->size ) == NULL )
1623
0
    {
1624
0
      libcerror_error_set(
1625
0
       error,
1626
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
1627
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1628
0
       "%s: unable to copy value entry data.",
1629
0
       function );
1630
1631
0
      goto on_error;
1632
0
    }
1633
204k
    if( libcdata_array_append_entry(
1634
204k
         internal_data_handle->value_entries,
1635
204k
         value_entry_index,
1636
204k
         (intptr_t *) value_entry,
1637
204k
         error ) != 1 )
1638
0
    {
1639
0
      libcerror_error_set(
1640
0
       error,
1641
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1642
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1643
0
       "%s: unable to append entry to values entries array.",
1644
0
       function );
1645
1646
0
      goto on_error;
1647
0
    }
1648
204k
    value_entry = NULL;
1649
204k
  }
1650
243k
  return( 1 );
1651
1652
0
on_error:
1653
0
  if( value_entry != NULL )
1654
0
  {
1655
0
    libfvalue_value_entry_free(
1656
0
     &value_entry,
1657
0
     NULL );
1658
0
  }
1659
0
  return( -1 );
1660
243k
}
1661