Coverage Report

Created: 2025-06-24 07:14

/src/libphdi/libphdi/libphdi_extent_table.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Extent table 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 <system_string.h>
26
#include <types.h>
27
#include <wide_string.h>
28
29
#include "libphdi_definitions.h"
30
#include "libphdi_extent_table.h"
31
#include "libphdi_image_values.h"
32
#include "libphdi_io_handle.h"
33
#include "libphdi_libbfio.h"
34
#include "libphdi_libcdata.h"
35
#include "libphdi_libcerror.h"
36
#include "libphdi_libclocale.h"
37
#include "libphdi_libcpath.h"
38
#include "libphdi_libfcache.h"
39
#include "libphdi_libfdata.h"
40
#include "libphdi_libuna.h"
41
#include "libphdi_storage_image.h"
42
#include "libphdi_system_string.h"
43
44
/* Creates an extent table
45
 * Make sure the value extent_table is referencing, is set to NULL
46
 * Returns 1 if successful or -1 on error
47
 */
48
int libphdi_extent_table_initialize(
49
     libphdi_extent_table_t **extent_table,
50
     libcerror_error_t **error )
51
0
{
52
0
  static char *function = "libphdi_extent_table_initialize";
53
54
0
  if( extent_table == NULL )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
59
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
60
0
     "%s: invalid extent table.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
0
  if( *extent_table != NULL )
66
0
  {
67
0
    libcerror_error_set(
68
0
     error,
69
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
70
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
71
0
     "%s: invalid extent table value already set.",
72
0
     function );
73
74
0
    return( -1 );
75
0
  }
76
0
  *extent_table = memory_allocate_structure(
77
0
                   libphdi_extent_table_t );
78
79
0
  if( *extent_table == NULL )
80
0
  {
81
0
    libcerror_error_set(
82
0
     error,
83
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
84
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
85
0
     "%s: unable to create extent table.",
86
0
     function );
87
88
0
    goto on_error;
89
0
  }
90
0
  if( memory_set(
91
0
       *extent_table,
92
0
       0,
93
0
       sizeof( libphdi_extent_table_t ) ) == NULL )
94
0
  {
95
0
    libcerror_error_set(
96
0
     error,
97
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
98
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
99
0
     "%s: unable to clear extent table.",
100
0
     function );
101
102
0
    goto on_error;
103
0
  }
104
0
  return( 1 );
105
106
0
on_error:
107
0
  if( *extent_table != NULL )
108
0
  {
109
0
    memory_free(
110
0
     *extent_table );
111
112
0
    *extent_table = NULL;
113
0
  }
114
0
  return( -1 );
115
0
}
116
117
/* Frees an extent table
118
 * Returns 1 if successful or -1 on error
119
 */
120
int libphdi_extent_table_free(
121
     libphdi_extent_table_t **extent_table,
122
     libcerror_error_t **error )
123
0
{
124
0
  static char *function = "libphdi_extent_table_free";
125
0
  int result            = 1;
126
127
0
  if( extent_table == 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 extent table.",
134
0
     function );
135
136
0
    return( -1 );
137
0
  }
138
0
  if( *extent_table != NULL )
139
0
  {
140
0
    if( libphdi_extent_table_clear(
141
0
         *extent_table,
142
0
         error ) != 1 )
143
0
    {
144
0
      libcerror_error_set(
145
0
       error,
146
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
147
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
148
0
       "%s: unable to clear extent table.",
149
0
       function );
150
151
0
      result = -1;
152
0
    }
153
0
    memory_free(
154
0
     *extent_table );
155
156
0
    *extent_table = NULL;
157
0
  }
158
0
  return( result );
159
0
}
160
161
/* Clears the extent table
162
 * Returns 1 if successful or -1 on error
163
 */
164
int libphdi_extent_table_clear(
165
     libphdi_extent_table_t *extent_table,
166
     libcerror_error_t **error )
167
0
{
168
0
  static char *function = "libphdi_extent_table_clear";
169
0
  int result            = 1;
170
171
0
  if( extent_table == NULL )
172
0
  {
173
0
    libcerror_error_set(
174
0
     error,
175
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
176
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
177
0
     "%s: invalid extent table.",
178
0
     function );
179
180
0
    return( -1 );
181
0
  }
182
0
  if( extent_table->extent_files_stream != NULL )
183
0
  {
184
0
    if( libfdata_stream_free(
185
0
         &( extent_table->extent_files_stream ),
186
0
         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 extent files stream.",
193
0
       function );
194
195
0
      result = -1;
196
0
    }
197
0
  }
198
0
  if( extent_table->extent_files_list != NULL )
199
0
  {
200
0
    if( libfdata_list_free(
201
0
         &( extent_table->extent_files_list ),
202
0
         error ) != 1 )
203
0
    {
204
0
      libcerror_error_set(
205
0
       error,
206
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
207
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
208
0
       "%s: unable to free extent files list.",
209
0
       function );
210
211
0
      result = -1;
212
0
    }
213
0
  }
214
0
  if( extent_table->extent_files_cache != NULL )
215
0
  {
216
0
    if( libfcache_cache_free(
217
0
         &( extent_table->extent_files_cache ),
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_FINALIZE_FAILED,
224
0
       "%s: unable to free extent files cache.",
225
0
       function );
226
227
0
      result = -1;
228
0
    }
229
0
  }
230
0
  if( memory_set(
231
0
       extent_table,
232
0
       0,
233
0
       sizeof( libphdi_extent_table_t ) ) == NULL )
234
0
  {
235
0
    libcerror_error_set(
236
0
     error,
237
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
238
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
239
0
     "%s: unable to extent table.",
240
0
     function );
241
242
0
    result = -1;
243
0
  }
244
0
  return( result );
245
0
}
246
247
/* Initializes the extents
248
 * Returns 1 if successful or -1 on error
249
 */
250
int libphdi_extent_table_initialize_extents(
251
     libphdi_extent_table_t *extent_table,
252
     libphdi_io_handle_t *io_handle,
253
     int disk_type,
254
     libcerror_error_t **error )
255
0
{
256
0
  static char *function = "libphdi_extent_table_initialize_extents";
257
0
  int result            = 0;
258
259
0
  if( extent_table == NULL )
260
0
  {
261
0
    libcerror_error_set(
262
0
     error,
263
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
264
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
265
0
     "%s: invalid extent table.",
266
0
     function );
267
268
0
    return( -1 );
269
0
  }
270
0
  if( ( disk_type != LIBPHDI_DISK_TYPE_EXPANDING )
271
0
   && ( disk_type != LIBPHDI_DISK_TYPE_FIXED ) )
272
0
  {
273
0
    libcerror_error_set(
274
0
     error,
275
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
276
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
277
0
     "%s: unsupported disk type.",
278
0
     function );
279
280
0
    return( -1 );
281
0
  }
282
0
  if( disk_type == LIBPHDI_DISK_TYPE_FIXED )
283
0
  {
284
0
    result = libfdata_stream_initialize(
285
0
              &( extent_table->extent_files_stream ),
286
0
              (intptr_t *) io_handle,
287
0
              NULL,
288
0
              NULL,
289
0
              NULL,
290
0
              (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libphdi_storage_image_read_segment_data,
291
0
              NULL,
292
0
              (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libphdi_storage_image_seek_segment_offset,
293
0
              LIBFDATA_DATA_HANDLE_FLAG_NON_MANAGED,
294
0
              error );
295
296
0
    if( result != 1 )
297
0
    {
298
0
      libcerror_error_set(
299
0
       error,
300
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
301
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
302
0
       "%s: unable to create extent files stream.",
303
0
       function );
304
305
0
      goto on_error;
306
0
    }
307
0
  }
308
0
  else if( disk_type == LIBPHDI_DISK_TYPE_EXPANDING )
309
0
  {
310
0
    result = libfdata_list_initialize(
311
0
              &( extent_table->extent_files_list ),
312
0
              (intptr_t *) io_handle,
313
0
              NULL,
314
0
              NULL,
315
0
              (int (*)(intptr_t *, intptr_t *, libfdata_list_element_t *, libfdata_cache_t *, int, off64_t, size64_t, uint32_t, uint8_t, libcerror_error_t **)) &libphdi_storage_image_read_element_data,
316
0
              NULL,
317
0
              LIBFDATA_DATA_HANDLE_FLAG_NON_MANAGED,
318
0
              error );
319
320
0
    if( result != 1 )
321
0
    {
322
0
      libcerror_error_set(
323
0
       error,
324
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
325
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
326
0
       "%s: unable to create extent files list.",
327
0
       function );
328
329
0
      goto on_error;
330
0
    }
331
0
    result = libfcache_cache_initialize(
332
0
        &( extent_table->extent_files_cache ),
333
0
        LIBPHDI_MAXIMUM_CACHE_ENTRIES_EXTENT_FILES,
334
0
        error );
335
336
0
    if( result != 1 )
337
0
    {
338
0
      libcerror_error_set(
339
0
       error,
340
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
341
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
342
0
       "%s: unable to create extent files cache.",
343
0
       function );
344
345
0
      goto on_error;
346
0
    }
347
0
  }
348
0
  extent_table->disk_type = disk_type;
349
350
0
  return( 1 );
351
352
0
on_error:
353
0
  if( extent_table->extent_files_list != NULL )
354
0
  {
355
0
    libfdata_list_free(
356
0
     &( extent_table->extent_files_list ),
357
0
     NULL );
358
0
  }
359
0
  if( extent_table->extent_files_stream != NULL )
360
0
  {
361
0
    libfdata_stream_free(
362
0
     &( extent_table->extent_files_stream ),
363
0
     NULL );
364
0
  }
365
0
  return( -1 );
366
0
}
367
368
/* Retrieves an extent file at a specific offset from the extent table
369
 * Returns 1 if successful or -1 on error
370
 */
371
int libphdi_extent_table_get_extent_file_at_offset(
372
     libphdi_extent_table_t *extent_table,
373
     off64_t offset,
374
     libbfio_pool_t *file_io_pool,
375
     int *extent_index,
376
     off64_t *extent_file_data_offset,
377
     libphdi_storage_image_t **extent_file,
378
     libcerror_error_t **error )
379
0
{
380
0
  static char *function = "libphdi_extent_table_get_extent_file_at_offset";
381
382
0
  if( extent_table == NULL )
383
0
  {
384
0
    libcerror_error_set(
385
0
     error,
386
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
387
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
388
0
     "%s: invalid extent table.",
389
0
     function );
390
391
0
    return( -1 );
392
0
  }
393
0
  if( libfdata_list_get_element_value_at_offset(
394
0
       extent_table->extent_files_list,
395
0
       (intptr_t *) file_io_pool,
396
0
       (libfdata_cache_t *) extent_table->extent_files_cache,
397
0
       offset,
398
0
       extent_index,
399
0
       extent_file_data_offset,
400
0
       (intptr_t **) extent_file,
401
0
       0,
402
0
       error ) != 1 )
403
0
  {
404
0
    libcerror_error_set(
405
0
     error,
406
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
407
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
408
0
     "%s: unable to retrieve element at offset: %" PRIi64 " (0x%08" PRIx64 ") from extent files list.",
409
0
     function,
410
0
     offset,
411
0
     offset );
412
413
0
    return( -1 );
414
0
  }
415
0
  return( 1 );
416
0
}
417
418
/* Appends an extent to the extent table based on the image values
419
 * Returns 1 if successful or -1 on error
420
 */
421
int libphdi_extent_table_append_extent(
422
     libphdi_extent_table_t *extent_table,
423
     int image_type,
424
     int file_io_pool_entry,
425
     size64_t extent_file_size,
426
     off64_t extent_offset,
427
     size64_t extent_size,
428
     libcerror_error_t **error )
429
0
{
430
0
  static char *function = "libphdi_extent_table_append_extent";
431
0
  int element_index     = 0;
432
0
  int segment_index     = 0;
433
434
0
  if( extent_table == NULL )
435
0
  {
436
0
    libcerror_error_set(
437
0
     error,
438
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
439
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
440
0
     "%s: invalid extent table.",
441
0
     function );
442
443
0
    return( -1 );
444
0
  }
445
0
  if( extent_table->image_type == 0 )
446
0
  {
447
0
    if( image_type == LIBPHDI_IMAGE_TYPE_COMPRESSED )
448
0
    {
449
0
      if( extent_table->disk_type != LIBPHDI_DISK_TYPE_EXPANDING )
450
0
      {
451
0
        libcerror_error_set(
452
0
         error,
453
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
454
0
         LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
455
0
         "%s: image type not supported for disk type.",
456
0
         function );
457
458
0
        return( -1 );
459
0
      }
460
0
    }
461
0
    else if( image_type == LIBPHDI_IMAGE_TYPE_PLAIN )
462
0
    {
463
0
      if( extent_table->disk_type != LIBPHDI_DISK_TYPE_FIXED )
464
0
      {
465
0
        libcerror_error_set(
466
0
         error,
467
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
468
0
         LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
469
0
         "%s: image type not supported for disk type.",
470
0
         function );
471
472
0
        return( -1 );
473
0
      }
474
0
    }
475
0
    else
476
0
    {
477
0
      libcerror_error_set(
478
0
       error,
479
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
480
0
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
481
0
       "%s: unsupported image type.",
482
0
       function );
483
484
0
      return( -1 );
485
0
    }
486
0
    extent_table->image_type = image_type;
487
0
  }
488
0
  else if( extent_table->image_type != image_type )
489
0
  {
490
0
    libcerror_error_set(
491
0
     error,
492
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
493
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
494
0
     "%s: mixed image types not supported.",
495
0
     function );
496
497
0
    return( -1 );
498
0
  }
499
0
  if( image_type == LIBPHDI_IMAGE_TYPE_PLAIN )
500
0
  {
501
0
    if( ( extent_offset < 0 )
502
0
     || ( (size64_t) extent_offset >= extent_file_size ) )
503
0
    {
504
0
      libcerror_error_set(
505
0
       error,
506
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
507
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
508
0
       "%s: invalid extent offset value out of bounds.",
509
0
       function );
510
511
0
      return( -1 );
512
0
    }
513
0
    if( extent_size > ( extent_file_size - (size64_t) extent_offset ) )
514
0
    {
515
0
      libcerror_error_set(
516
0
       error,
517
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
518
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
519
0
       "%s: invalid extent size value out of bounds.",
520
0
       function );
521
522
0
      return( -1 );
523
0
    }
524
0
    if( libfdata_stream_append_segment(
525
0
         extent_table->extent_files_stream,
526
0
         &segment_index,
527
0
         file_io_pool_entry,
528
0
         extent_offset,
529
0
         extent_size,
530
0
         0,
531
0
         error ) != 1 )
532
0
    {
533
0
      libcerror_error_set(
534
0
       error,
535
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
536
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
537
0
       "%s: unable to append segment to extent files stream.",
538
0
       function );
539
540
0
      return( -1 );
541
0
    }
542
0
  }
543
0
  else if( image_type == LIBPHDI_IMAGE_TYPE_COMPRESSED )
544
0
  {
545
0
    if( extent_offset != 0 )
546
0
    {
547
0
      libcerror_error_set(
548
0
       error,
549
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
550
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
551
0
       "%s: invalid extent offset value out of bounds.",
552
0
       function );
553
554
0
      return( -1 );
555
0
    }
556
0
    if( libfdata_list_append_element_with_mapped_size(
557
0
         extent_table->extent_files_list,
558
0
         &element_index,
559
0
         file_io_pool_entry,
560
0
         0,
561
0
         extent_file_size,
562
0
         0,
563
0
         extent_size,
564
0
         error ) != 1 )
565
0
    {
566
0
      libcerror_error_set(
567
0
       error,
568
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
569
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
570
0
       "%s: unable to append element to extent files list.",
571
0
       function );
572
573
0
      return( -1 );
574
0
    }
575
0
  }
576
0
  return( 1 );
577
0
}
578