Coverage Report

Created: 2025-06-13 07:22

/src/libphdi/libphdi/libphdi_image_descriptor.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Image descriptor 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 <types.h>
25
26
#include "libphdi_image_descriptor.h"
27
#include "libphdi_image_values.h"
28
#include "libphdi_libcerror.h"
29
#include "libphdi_libcthreads.h"
30
#include "libphdi_types.h"
31
32
/* Creates an image descriptor
33
 * Make sure the value image_descriptor is referencing, is set to NULL
34
 * Returns 1 if successful or -1 on error
35
 */
36
int libphdi_image_descriptor_initialize(
37
     libphdi_image_descriptor_t **image_descriptor,
38
     libphdi_image_values_t *image_values,
39
     libcerror_error_t **error )
40
0
{
41
0
  libphdi_internal_image_descriptor_t *internal_image_descriptor = NULL;
42
0
  static char *function                                          = "libphdi_image_descriptor_initialize";
43
44
0
  if( image_descriptor == NULL )
45
0
  {
46
0
    libcerror_error_set(
47
0
     error,
48
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
49
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
50
0
     "%s: invalid image descriptor.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
0
  if( *image_descriptor != NULL )
56
0
  {
57
0
    libcerror_error_set(
58
0
     error,
59
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
60
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
61
0
     "%s: invalid image descriptor value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
0
  if( image_values == NULL )
67
0
  {
68
0
    libcerror_error_set(
69
0
     error,
70
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
71
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
72
0
     "%s: invalid image values.",
73
0
     function );
74
75
0
    return( -1 );
76
0
  }
77
0
  internal_image_descriptor = memory_allocate_structure(
78
0
                               libphdi_internal_image_descriptor_t );
79
80
0
  if( internal_image_descriptor == NULL )
81
0
  {
82
0
    libcerror_error_set(
83
0
     error,
84
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
85
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
86
0
     "%s: unable to create image descriptor.",
87
0
     function );
88
89
0
    goto on_error;
90
0
  }
91
0
  if( memory_set(
92
0
       internal_image_descriptor,
93
0
       0,
94
0
       sizeof( libphdi_internal_image_descriptor_t ) ) == NULL )
95
0
  {
96
0
    libcerror_error_set(
97
0
     error,
98
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
99
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
100
0
     "%s: unable to clear image descriptor.",
101
0
     function );
102
103
0
    memory_free(
104
0
     internal_image_descriptor );
105
106
0
    return( -1 );
107
0
  }
108
0
#if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT )
109
0
  if( libcthreads_read_write_lock_initialize(
110
0
       &( internal_image_descriptor->read_write_lock ),
111
0
       error ) != 1 )
112
0
  {
113
0
    libcerror_error_set(
114
0
     error,
115
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
116
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
117
0
     "%s: unable to initialize read/write lock.",
118
0
     function );
119
120
0
    goto on_error;
121
0
  }
122
0
#endif
123
0
  internal_image_descriptor->image_values = image_values;
124
125
0
  *image_descriptor = (libphdi_image_descriptor_t *) internal_image_descriptor;
126
127
0
  return( 1 );
128
129
0
on_error:
130
0
  if( internal_image_descriptor != NULL )
131
0
  {
132
0
    memory_free(
133
0
     internal_image_descriptor );
134
0
  }
135
0
  return( -1 );
136
0
}
137
138
/* Frees an image descriptor
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libphdi_image_descriptor_free(
142
     libphdi_image_descriptor_t **image_descriptor,
143
     libcerror_error_t **error )
144
0
{
145
0
  libphdi_internal_image_descriptor_t *internal_image_descriptor = NULL;
146
0
  static char *function                                          = "libphdi_image_descriptor_free";
147
0
  int result                                                     = 1;
148
149
0
  if( image_descriptor == NULL )
150
0
  {
151
0
    libcerror_error_set(
152
0
     error,
153
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
154
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
155
0
     "%s: invalid image descriptor.",
156
0
     function );
157
158
0
    return( -1 );
159
0
  }
160
0
  if( *image_descriptor != NULL )
161
0
  {
162
0
    internal_image_descriptor = (libphdi_internal_image_descriptor_t *) *image_descriptor;
163
0
    *image_descriptor         = NULL;
164
165
0
#if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT )
166
0
    if( libcthreads_read_write_lock_free(
167
0
         &( internal_image_descriptor->read_write_lock ),
168
0
         error ) != 1 )
169
0
    {
170
0
      libcerror_error_set(
171
0
       error,
172
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
173
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
174
0
       "%s: unable to free read/write lock.",
175
0
       function );
176
177
0
      result = -1;
178
0
    }
179
0
#endif
180
    /* The image values reference is freed elsewhere
181
     */
182
0
    memory_free(
183
0
     internal_image_descriptor );
184
0
  }
185
0
  return( result );
186
0
}
187
188
/* Retrieves the image type
189
 * Returns 1 if successful or -1 on error
190
 */
191
int libphdi_image_descriptor_get_type(
192
     libphdi_image_descriptor_t *image_descriptor,
193
     int *type,
194
     libcerror_error_t **error )
195
0
{
196
0
  libphdi_internal_image_descriptor_t *internal_image_descriptor = NULL;
197
0
  static char *function                                          = "libphdi_image_descriptor_get_type";
198
0
  int result                                                     = 1;
199
200
0
  if( image_descriptor == NULL )
201
0
  {
202
0
    libcerror_error_set(
203
0
     error,
204
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
205
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
206
0
     "%s: invalid image descriptor.",
207
0
     function );
208
209
0
    return( -1 );
210
0
  }
211
0
  internal_image_descriptor = (libphdi_internal_image_descriptor_t *) image_descriptor;
212
213
0
#if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT )
214
0
  if( libcthreads_read_write_lock_grab_for_read(
215
0
       internal_image_descriptor->read_write_lock,
216
0
       error ) != 1 )
217
0
  {
218
0
    libcerror_error_set(
219
0
     error,
220
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
221
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
222
0
     "%s: unable to grab read/write lock for reading.",
223
0
     function );
224
225
0
    return( -1 );
226
0
  }
227
0
#endif
228
0
  if( libphdi_image_values_get_type(
229
0
       internal_image_descriptor->image_values,
230
0
       type,
231
0
       error ) != 1 )
232
0
  {
233
0
    libcerror_error_set(
234
0
     error,
235
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
236
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
237
0
     "%s: unable to retrieve type.",
238
0
     function );
239
240
0
    result = -1;
241
0
  }
242
0
#if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT )
243
0
  if( libcthreads_read_write_lock_release_for_read(
244
0
       internal_image_descriptor->read_write_lock,
245
0
       error ) != 1 )
246
0
  {
247
0
    libcerror_error_set(
248
0
     error,
249
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
250
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
251
0
     "%s: unable to release read/write lock for reading.",
252
0
     function );
253
254
0
    return( -1 );
255
0
  }
256
0
#endif
257
0
  return( result );
258
0
}
259
260
/* Retrieves the size of the UTF-8 encoded filename
261
 * The returned size includes the end of string character
262
 * Returns 1 if successful, 0 if not available or -1 on error
263
 */
264
int libphdi_image_descriptor_get_utf8_filename_size(
265
     libphdi_image_descriptor_t *image_descriptor,
266
     size_t *utf8_string_size,
267
     libcerror_error_t **error )
268
0
{
269
0
  libphdi_internal_image_descriptor_t *internal_image_descriptor = NULL;
270
0
  static char *function                                          = "libphdi_image_descriptor_get_utf8_filename_size";
271
0
  int result                                                     = 1;
272
273
0
  if( image_descriptor == NULL )
274
0
  {
275
0
    libcerror_error_set(
276
0
     error,
277
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
278
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
279
0
     "%s: invalid image descriptor.",
280
0
     function );
281
282
0
    return( -1 );
283
0
  }
284
0
  internal_image_descriptor = (libphdi_internal_image_descriptor_t *) image_descriptor;
285
286
0
#if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT )
287
0
  if( libcthreads_read_write_lock_grab_for_read(
288
0
       internal_image_descriptor->read_write_lock,
289
0
       error ) != 1 )
290
0
  {
291
0
    libcerror_error_set(
292
0
     error,
293
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
294
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
295
0
     "%s: unable to grab read/write lock for reading.",
296
0
     function );
297
298
0
    return( -1 );
299
0
  }
300
0
#endif
301
0
  if( libphdi_image_values_get_utf8_filename_size(
302
0
       internal_image_descriptor->image_values,
303
0
       utf8_string_size,
304
0
       error ) != 1 )
305
0
  {
306
0
    libcerror_error_set(
307
0
     error,
308
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
309
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
310
0
     "%s: unable to retrieve UTF-8 string size.",
311
0
     function );
312
313
0
    result = -1;
314
0
  }
315
0
#if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT )
316
0
  if( libcthreads_read_write_lock_release_for_read(
317
0
       internal_image_descriptor->read_write_lock,
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_SET_FAILED,
324
0
     "%s: unable to release read/write lock for reading.",
325
0
     function );
326
327
0
    return( -1 );
328
0
  }
329
0
#endif
330
0
  return( result );
331
0
}
332
333
/* Retrieves the UTF-8 encoded filename
334
 * The size should include the end of string character
335
 * Returns 1 if successful, 0 if not available or -1 on error
336
 */
337
int libphdi_image_descriptor_get_utf8_filename(
338
     libphdi_image_descriptor_t *image_descriptor,
339
     uint8_t *utf8_string,
340
     size_t utf8_string_size,
341
     libcerror_error_t **error )
342
0
{
343
0
  libphdi_internal_image_descriptor_t *internal_image_descriptor = NULL;
344
0
  static char *function                                          = "libphdi_image_descriptor_get_utf8_filename";
345
0
  int result                                                     = 1;
346
347
0
  if( image_descriptor == NULL )
348
0
  {
349
0
    libcerror_error_set(
350
0
     error,
351
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
352
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
353
0
     "%s: invalid image descriptor.",
354
0
     function );
355
356
0
    return( -1 );
357
0
  }
358
0
  internal_image_descriptor = (libphdi_internal_image_descriptor_t *) image_descriptor;
359
360
0
#if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT )
361
0
  if( libcthreads_read_write_lock_grab_for_read(
362
0
       internal_image_descriptor->read_write_lock,
363
0
       error ) != 1 )
364
0
  {
365
0
    libcerror_error_set(
366
0
     error,
367
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
368
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
369
0
     "%s: unable to grab read/write lock for reading.",
370
0
     function );
371
372
0
    return( -1 );
373
0
  }
374
0
#endif
375
0
  if( libphdi_image_values_get_utf8_filename(
376
0
       internal_image_descriptor->image_values,
377
0
       utf8_string,
378
0
       utf8_string_size,
379
0
       error ) != 1 )
380
0
  {
381
0
    libcerror_error_set(
382
0
     error,
383
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
384
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
385
0
     "%s: unable to retrieve UTF-8 string.",
386
0
     function );
387
388
0
    result = -1;
389
0
  }
390
0
#if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT )
391
0
  if( libcthreads_read_write_lock_release_for_read(
392
0
       internal_image_descriptor->read_write_lock,
393
0
       error ) != 1 )
394
0
  {
395
0
    libcerror_error_set(
396
0
     error,
397
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
398
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
399
0
     "%s: unable to release read/write lock for reading.",
400
0
     function );
401
402
0
    return( -1 );
403
0
  }
404
0
#endif
405
0
  return( result );
406
0
}
407
408
/* Retrieves the size of the UTF-16 encoded filename
409
 * The returned size includes the end of string character
410
 * Returns 1 if successful, 0 if not available or -1 on error
411
 */
412
int libphdi_image_descriptor_get_utf16_filename_size(
413
     libphdi_image_descriptor_t *image_descriptor,
414
     size_t *utf16_string_size,
415
     libcerror_error_t **error )
416
0
{
417
0
  libphdi_internal_image_descriptor_t *internal_image_descriptor = NULL;
418
0
  static char *function                                          = "libphdi_image_descriptor_get_utf16_filename_size";
419
0
  int result                                                     = 1;
420
421
0
  if( image_descriptor == NULL )
422
0
  {
423
0
    libcerror_error_set(
424
0
     error,
425
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
426
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
427
0
     "%s: invalid image descriptor.",
428
0
     function );
429
430
0
    return( -1 );
431
0
  }
432
0
  internal_image_descriptor = (libphdi_internal_image_descriptor_t *) image_descriptor;
433
434
0
#if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT )
435
0
  if( libcthreads_read_write_lock_grab_for_read(
436
0
       internal_image_descriptor->read_write_lock,
437
0
       error ) != 1 )
438
0
  {
439
0
    libcerror_error_set(
440
0
     error,
441
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
442
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
443
0
     "%s: unable to grab read/write lock for reading.",
444
0
     function );
445
446
0
    return( -1 );
447
0
  }
448
0
#endif
449
0
  if( libphdi_image_values_get_utf16_filename_size(
450
0
       internal_image_descriptor->image_values,
451
0
       utf16_string_size,
452
0
       error ) != 1 )
453
0
  {
454
0
    libcerror_error_set(
455
0
     error,
456
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
457
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
458
0
     "%s: unable to retrieve UTF-16 string size.",
459
0
     function );
460
461
0
    result = -1;
462
0
  }
463
0
#if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT )
464
0
  if( libcthreads_read_write_lock_release_for_read(
465
0
       internal_image_descriptor->read_write_lock,
466
0
       error ) != 1 )
467
0
  {
468
0
    libcerror_error_set(
469
0
     error,
470
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
471
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
472
0
     "%s: unable to release read/write lock for reading.",
473
0
     function );
474
475
0
    return( -1 );
476
0
  }
477
0
#endif
478
0
  return( result );
479
0
}
480
481
/* Retrieves the UTF-16 encoded filename
482
 * The size should include the end of string character
483
 * Returns 1 if successful, 0 if not available or -1 on error
484
 */
485
int libphdi_image_descriptor_get_utf16_filename(
486
     libphdi_image_descriptor_t *image_descriptor,
487
     uint16_t *utf16_string,
488
     size_t utf16_string_size,
489
     libcerror_error_t **error )
490
0
{
491
0
  libphdi_internal_image_descriptor_t *internal_image_descriptor = NULL;
492
0
  static char *function                                          = "libphdi_image_descriptor_get_utf16_filename";
493
0
  int result                                                     = 1;
494
495
0
  if( image_descriptor == NULL )
496
0
  {
497
0
    libcerror_error_set(
498
0
     error,
499
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
500
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
501
0
     "%s: invalid image descriptor.",
502
0
     function );
503
504
0
    return( -1 );
505
0
  }
506
0
  internal_image_descriptor = (libphdi_internal_image_descriptor_t *) image_descriptor;
507
508
0
#if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT )
509
0
  if( libcthreads_read_write_lock_grab_for_read(
510
0
       internal_image_descriptor->read_write_lock,
511
0
       error ) != 1 )
512
0
  {
513
0
    libcerror_error_set(
514
0
     error,
515
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
516
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
517
0
     "%s: unable to grab read/write lock for reading.",
518
0
     function );
519
520
0
    return( -1 );
521
0
  }
522
0
#endif
523
0
  if( libphdi_image_values_get_utf16_filename(
524
0
       internal_image_descriptor->image_values,
525
0
       utf16_string,
526
0
       utf16_string_size,
527
0
       error ) != 1 )
528
0
  {
529
0
    libcerror_error_set(
530
0
     error,
531
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
532
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
533
0
     "%s: unable to retrieve UTF-16 string.",
534
0
     function );
535
536
0
    result = -1;
537
0
  }
538
0
#if defined( HAVE_LIBPHDI_MULTI_THREAD_SUPPORT )
539
0
  if( libcthreads_read_write_lock_release_for_read(
540
0
       internal_image_descriptor->read_write_lock,
541
0
       error ) != 1 )
542
0
  {
543
0
    libcerror_error_set(
544
0
     error,
545
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
546
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
547
0
     "%s: unable to release read/write lock for reading.",
548
0
     function );
549
550
0
    return( -1 );
551
0
  }
552
0
#endif
553
0
  return( result );
554
0
}
555