Coverage Report

Created: 2025-06-22 07:35

/src/libphdi/libphdi/libphdi_image_values.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Image values functions
3
 *
4
 * Copyright (C) 2015-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 <narrow_string.h>
25
#include <types.h>
26
27
#include "libphdi_image_values.h"
28
#include "libphdi_libcerror.h"
29
#include "libphdi_libuna.h"
30
#include "libphdi_uuid_string.h"
31
32
/* Creates image values
33
 * Make sure the value image_values is referencing, is set to NULL
34
 * Returns 1 if successful or -1 on error
35
 */
36
int libphdi_image_values_initialize(
37
     libphdi_image_values_t **image_values,
38
     libcerror_error_t **error )
39
0
{
40
0
  static char *function = "libphdi_image_values_initialize";
41
42
0
  if( image_values == 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 image values.",
49
0
     function );
50
51
0
    return( -1 );
52
0
  }
53
0
  if( *image_values != 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 image values value already set.",
60
0
     function );
61
62
0
    return( -1 );
63
0
  }
64
0
  *image_values = memory_allocate_structure(
65
0
                   libphdi_image_values_t );
66
67
0
  if( *image_values == 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 image values.",
74
0
     function );
75
76
0
    goto on_error;
77
0
  }
78
0
  if( memory_set(
79
0
       *image_values,
80
0
       0,
81
0
       sizeof( libphdi_image_values_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 image values.",
88
0
     function );
89
90
0
    memory_free(
91
0
     *image_values );
92
93
0
    *image_values = NULL;
94
95
0
    return( -1 );
96
0
  }
97
0
  return( 1 );
98
99
0
on_error:
100
0
  if( *image_values != NULL )
101
0
  {
102
0
    memory_free(
103
0
     *image_values );
104
105
0
    *image_values = NULL;
106
0
  }
107
0
  return( -1 );
108
0
}
109
110
/* Frees image values
111
 * Returns 1 if successful or -1 on error
112
 */
113
int libphdi_image_values_free(
114
     libphdi_image_values_t **image_values,
115
     libcerror_error_t **error )
116
0
{
117
0
  static char *function = "libphdi_image_values_free";
118
119
0
  if( image_values == 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 image values.",
126
0
     function );
127
128
0
    return( -1 );
129
0
  }
130
0
  if( *image_values != NULL )
131
0
  {
132
0
    if( ( *image_values )->filename != NULL )
133
0
    {
134
0
      memory_free(
135
0
       ( *image_values )->filename );
136
0
    }
137
0
    memory_free(
138
0
     *image_values );
139
140
0
    *image_values = NULL;
141
0
  }
142
0
  return( 1 );
143
0
}
144
145
/* Sets the identifier
146
 * Returns 1 if successful or -1 on error
147
 */
148
int libphdi_image_values_set_identifier(
149
     libphdi_image_values_t *image_values,
150
     const uint8_t *utf8_string,
151
     size_t utf8_string_length,
152
     libcerror_error_t **error )
153
0
{
154
0
  static char *function = "libphdi_image_values_set_identifier";
155
156
0
  if( image_values == NULL )
157
0
  {
158
0
    libcerror_error_set(
159
0
     error,
160
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
161
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
162
0
     "%s: invalid image values.",
163
0
     function );
164
165
0
    return( -1 );
166
0
  }
167
0
  if( libphdi_uuid_string_copy_to_byte_stream(
168
0
       utf8_string,
169
0
       utf8_string_length,
170
0
       image_values->identifier,
171
0
       16,
172
0
       error ) != 1 )
173
0
  {
174
0
    libcerror_error_set(
175
0
     error,
176
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
177
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
178
0
     "%s: unable to copy UUID string to identifier.",
179
0
     function );
180
181
0
    return( -1 );
182
0
  }
183
0
  return( 1 );
184
0
}
185
186
/* Sets the filename
187
 * Returns 1 if successful or -1 on error
188
 */
189
int libphdi_image_values_set_filename(
190
     libphdi_image_values_t *image_values,
191
     const uint8_t *utf8_string,
192
     size_t utf8_string_length,
193
     libcerror_error_t **error )
194
0
{
195
0
  static char *function = "libphdi_image_values_set_filename";
196
197
0
  if( image_values == NULL )
198
0
  {
199
0
    libcerror_error_set(
200
0
     error,
201
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
202
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
203
0
     "%s: invalid image values.",
204
0
     function );
205
206
0
    return( -1 );
207
0
  }
208
0
  if( image_values->filename != NULL )
209
0
  {
210
0
    libcerror_error_set(
211
0
     error,
212
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
213
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
214
0
     "%s: invalid image values - filename value already set.",
215
0
     function );
216
217
0
    return( -1 );
218
0
  }
219
0
  if( utf8_string == NULL )
220
0
  {
221
0
    libcerror_error_set(
222
0
     error,
223
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
224
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
225
0
     "%s: invalid UTF-8 string.",
226
0
     function );
227
228
0
    return( -1 );
229
0
  }
230
0
  if( ( utf8_string_length == 0 )
231
0
   || ( utf8_string_length > ( (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE - 1 ) ) )
232
0
  {
233
0
    libcerror_error_set(
234
0
     error,
235
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
236
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
237
0
     "%s: invalid UTF-8 string length value out of bounds.",
238
0
     function );
239
240
0
    return( -1 );
241
0
  }
242
0
  image_values->filename_size = utf8_string_length + 1;
243
244
0
  image_values->filename = (uint8_t *) memory_allocate(
245
0
                                        sizeof( uint8_t ) * image_values->filename_size );
246
247
0
  if( image_values->filename == NULL )
248
0
  {
249
0
    libcerror_error_set(
250
0
     error,
251
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
252
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
253
0
     "%s: unable to create filename.",
254
0
     function );
255
256
0
    goto on_error;
257
0
  }
258
0
  if( memory_copy(
259
0
       image_values->filename,
260
0
       utf8_string,
261
0
       utf8_string_length ) == NULL )
262
0
  {
263
0
    libcerror_error_set(
264
0
     error,
265
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
266
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
267
0
     "%s: unable to copy filename.",
268
0
     function );
269
270
0
    goto on_error;
271
0
  }
272
0
  image_values->filename[ utf8_string_length ] = 0;
273
274
0
  return( 1 );
275
276
0
on_error:
277
0
  if( image_values->filename != NULL )
278
0
  {
279
0
    memory_free(
280
0
     image_values->filename );
281
282
0
    image_values->filename = NULL;
283
0
  }
284
0
  image_values->filename_size = 0;
285
286
0
  return( -1 );
287
0
}
288
289
/* Retrieves the identifier
290
 * The identifier is a big-endian GUID and is 16 bytes of size
291
 * Returns 1 if successful or -1 on error
292
 */
293
int libphdi_image_values_get_identifier(
294
     libphdi_image_values_t *image_values,
295
     uint8_t *guid_data,
296
     size_t guid_data_size,
297
     libcerror_error_t **error )
298
0
{
299
0
  static char *function = "libphdi_image_values_get_identifier";
300
301
0
  if( image_values == 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 image values.",
308
0
     function );
309
310
0
    return( -1 );
311
0
  }
312
0
  if( guid_data == NULL )
313
0
  {
314
0
    libcerror_error_set(
315
0
     error,
316
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
317
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
318
0
     "%s: invalid GUID data.",
319
0
     function );
320
321
0
    return( -1 );
322
0
  }
323
0
  if( ( guid_data_size < 16 )
324
0
   || ( guid_data_size > SSIZE_MAX ) )
325
0
  {
326
0
    libcerror_error_set(
327
0
     error,
328
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
329
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
330
0
     "%s: invalid GUID data size value out of bounds.",
331
0
     function );
332
333
0
    return( -1 );
334
0
  }
335
0
  if( memory_copy(
336
0
       guid_data,
337
0
       image_values->identifier,
338
0
       16 ) == NULL )
339
0
  {
340
0
    libcerror_error_set(
341
0
     error,
342
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
343
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
344
0
     "%s: unable to copy identifier.",
345
0
     function );
346
347
0
    return( -1 );
348
0
  }
349
0
  return( 1 );
350
0
}
351
352
/* Retrieves the image type
353
 * Returns 1 if successful or -1 on error
354
 */
355
int libphdi_image_values_get_type(
356
     libphdi_image_values_t *image_values,
357
     int *type,
358
     libcerror_error_t **error )
359
0
{
360
0
  static char *function = "libphdi_image_values_get_type";
361
362
0
  if( image_values == NULL )
363
0
  {
364
0
    libcerror_error_set(
365
0
     error,
366
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
367
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
368
0
     "%s: invalid image values.",
369
0
     function );
370
371
0
    return( -1 );
372
0
  }
373
0
  if( type == 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 type.",
380
0
     function );
381
382
0
    return( -1 );
383
0
  }
384
0
  *type = image_values->type;
385
386
0
  return( 1 );
387
0
}
388
389
/* Retrieves the size of the UTF-8 encoded filename
390
 * The returned size includes the end of string character
391
 * Returns 1 if successful, 0 if not available or -1 on error
392
 */
393
int libphdi_image_values_get_utf8_filename_size(
394
     libphdi_image_values_t *image_values,
395
     size_t *utf8_string_size,
396
     libcerror_error_t **error )
397
0
{
398
0
  static char *function = "libphdi_image_values_get_utf8_filename_size";
399
400
0
  if( image_values == NULL )
401
0
  {
402
0
    libcerror_error_set(
403
0
     error,
404
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
405
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
406
0
     "%s: invalid image values.",
407
0
     function );
408
409
0
    return( -1 );
410
0
  }
411
0
  if( image_values->filename == NULL )
412
0
  {
413
0
    return( 0 );
414
0
  }
415
0
  if( utf8_string_size == NULL )
416
0
  {
417
0
    libcerror_error_set(
418
0
     error,
419
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
420
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
421
0
     "%s: invalid UTF-8 string size.",
422
0
     function );
423
424
0
    return( -1 );
425
0
  }
426
0
  *utf8_string_size = image_values->filename_size;
427
428
0
  return( 1 );
429
0
}
430
431
/* Retrieves the UTF-8 encoded filename
432
 * The size should include the end of string character
433
 * Returns 1 if successful, 0 if not available or -1 on error
434
 */
435
int libphdi_image_values_get_utf8_filename(
436
     libphdi_image_values_t *image_values,
437
     uint8_t *utf8_string,
438
     size_t utf8_string_size,
439
     libcerror_error_t **error )
440
0
{
441
0
  static char *function = "libphdi_image_values_get_utf8_filename";
442
443
0
  if( image_values == NULL )
444
0
  {
445
0
    libcerror_error_set(
446
0
     error,
447
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
448
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
449
0
     "%s: invalid image values.",
450
0
     function );
451
452
0
    return( -1 );
453
0
  }
454
0
  if( image_values->filename == NULL )
455
0
  {
456
0
    return( 0 );
457
0
  }
458
0
  if( utf8_string == NULL )
459
0
  {
460
0
    libcerror_error_set(
461
0
     error,
462
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
463
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
464
0
     "%s: invalid UTF-8 string.",
465
0
     function );
466
467
0
    return( -1 );
468
0
  }
469
0
  if( utf8_string_size > (size_t) SSIZE_MAX )
470
0
  {
471
0
    libcerror_error_set(
472
0
     error,
473
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
474
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
475
0
     "%s: invalid UTF-8 string size value exceeds maximum.",
476
0
     function );
477
478
0
    return( -1 );
479
0
  }
480
0
  if( utf8_string_size < image_values->filename_size )
481
0
  {
482
0
    libcerror_error_set(
483
0
     error,
484
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
485
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
486
0
     "%s: UTF-8 string is too small.",
487
0
     function );
488
489
0
    return( -1 );
490
0
  }
491
0
  if( narrow_string_copy(
492
0
       utf8_string,
493
0
       image_values->filename,
494
0
       image_values->filename_size ) == NULL )
495
0
  {
496
0
    libcerror_error_set(
497
0
     error,
498
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
499
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
500
0
     "%s: unable to copy UTF-8 string.",
501
0
     function );
502
503
0
    return( -1 );
504
0
  }
505
0
  return( 1 );
506
0
}
507
508
/* Retrieves the size of the UTF-16 encoded filename
509
 * The returned size includes the end of string character
510
 * Returns 1 if successful, 0 if not available or -1 on error
511
 */
512
int libphdi_image_values_get_utf16_filename_size(
513
     libphdi_image_values_t *image_values,
514
     size_t *utf16_string_size,
515
     libcerror_error_t **error )
516
0
{
517
0
  static char *function = "libphdi_image_values_get_utf16_filename_size";
518
519
0
  if( image_values == NULL )
520
0
  {
521
0
    libcerror_error_set(
522
0
     error,
523
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
524
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
525
0
     "%s: invalid image values.",
526
0
     function );
527
528
0
    return( -1 );
529
0
  }
530
0
  if( image_values->filename == NULL )
531
0
  {
532
0
    return( 0 );
533
0
  }
534
0
  if( libuna_utf16_string_size_from_utf8(
535
0
       (uint8_t *) image_values->filename,
536
0
       image_values->filename_size,
537
0
       utf16_string_size,
538
0
       error ) != 1 )
539
0
  {
540
0
    libcerror_error_set(
541
0
     error,
542
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
543
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
544
0
     "%s: unable to retrieve UTF-16 size size.",
545
0
     function );
546
547
0
    return( -1 );
548
0
  }
549
0
  return( 1 );
550
0
}
551
552
/* Retrieves the UTF-16 encoded filename
553
 * The size should include the end of string character
554
 * Returns 1 if successful, 0 if not available or -1 on error
555
 */
556
int libphdi_image_values_get_utf16_filename(
557
     libphdi_image_values_t *image_values,
558
     uint16_t *utf16_string,
559
     size_t utf16_string_size,
560
     libcerror_error_t **error )
561
0
{
562
0
  static char *function = "libphdi_image_values_get_utf16_filename";
563
564
0
  if( image_values == NULL )
565
0
  {
566
0
    libcerror_error_set(
567
0
     error,
568
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
569
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
570
0
     "%s: invalid image values.",
571
0
     function );
572
573
0
    return( -1 );
574
0
  }
575
0
  if( image_values->filename == NULL )
576
0
  {
577
0
    return( 0 );
578
0
  }
579
0
  if( libuna_utf16_string_copy_from_utf8(
580
0
       utf16_string,
581
0
       utf16_string_size,
582
0
       (uint8_t *) image_values->filename,
583
0
       image_values->filename_size,
584
0
       error ) != 1 )
585
0
  {
586
0
    libcerror_error_set(
587
0
     error,
588
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
589
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
590
0
     "%s: unable to copy UTF-16 string.",
591
0
     function );
592
593
0
    return( -1 );
594
0
  }
595
0
  return( 1 );
596
0
}
597