Coverage Report

Created: 2025-09-05 06:58

/src/libewf/libewf/libewf_segment_table.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Segment table functions
3
 *
4
 * Copyright (C) 2006-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 "libewf_definitions.h"
30
#include "libewf_libbfio.h"
31
#include "libewf_libcdata.h"
32
#include "libewf_libcerror.h"
33
#include "libewf_libclocale.h"
34
#include "libewf_libfdata.h"
35
#include "libewf_libuna.h"
36
#include "libewf_segment_file.h"
37
#include "libewf_segment_table.h"
38
39
/* Creates a segment table
40
 * Make sure the value segment_table is referencing, is set to NULL
41
 * Returns 1 if successful or -1 on error
42
 */
43
int libewf_segment_table_initialize(
44
     libewf_segment_table_t **segment_table,
45
     libewf_io_handle_t *io_handle,
46
     size64_t maximum_segment_size,
47
     libcerror_error_t **error )
48
3.23k
{
49
3.23k
  static char *function = "libewf_segment_table_initialize";
50
51
3.23k
  if( segment_table == 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 segment table.",
58
0
     function );
59
60
0
    return( -1 );
61
0
  }
62
3.23k
  if( *segment_table != 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 segment table value already set.",
69
0
     function );
70
71
0
    return( -1 );
72
0
  }
73
3.23k
  if( io_handle == NULL )
74
0
  {
75
0
    libcerror_error_set(
76
0
     error,
77
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
78
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
79
0
     "%s: invalid IO handle.",
80
0
     function );
81
82
0
    return( -1 );
83
0
  }
84
3.23k
  *segment_table = memory_allocate_structure(
85
3.23k
                    libewf_segment_table_t );
86
87
3.23k
  if( *segment_table == NULL )
88
0
  {
89
0
    libcerror_error_set(
90
0
     error,
91
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
92
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
93
0
     "%s: unable to create segment table.",
94
0
     function );
95
96
0
    goto on_error;
97
0
  }
98
3.23k
  if( memory_set(
99
3.23k
       *segment_table,
100
3.23k
       0,
101
3.23k
       sizeof( libewf_segment_table_t ) ) == NULL )
102
0
  {
103
0
    libcerror_error_set(
104
0
     error,
105
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
106
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
107
0
     "%s: unable to clear segment table.",
108
0
     function );
109
110
0
    memory_free(
111
0
     *segment_table );
112
113
0
    *segment_table = NULL;
114
115
0
    return( -1 );
116
0
  }
117
3.23k
  if( libfdata_list_initialize(
118
3.23k
       &( ( *segment_table )->segment_files_list ),
119
3.23k
       (intptr_t *) io_handle,
120
3.23k
       (int (*)(intptr_t **, libcerror_error_t **)) &libewf_io_handle_free,
121
3.23k
       (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) &libewf_io_handle_clone,
122
3.23k
       (int (*)(intptr_t *, intptr_t *, libfdata_list_element_t *, libfdata_cache_t *, int, off64_t, size64_t, uint32_t, uint8_t, libcerror_error_t **)) &libewf_segment_file_read_element_data,
123
3.23k
       NULL,
124
3.23k
       LIBFDATA_DATA_HANDLE_FLAG_NON_MANAGED,
125
3.23k
       error ) != 1 )
126
0
  {
127
0
    libcerror_error_set(
128
0
     error,
129
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
130
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
131
0
     "%s: unable to create segment files list.",
132
0
     function );
133
134
0
    goto on_error;
135
0
  }
136
3.23k
  if( libfcache_cache_initialize(
137
3.23k
       &( ( *segment_table )->segment_files_cache ),
138
3.23k
       LIBEWF_MAXIMUM_CACHE_ENTRIES_SEGMENT_FILES,
139
3.23k
       error ) != 1 )
140
0
  {
141
0
    libcerror_error_set(
142
0
     error,
143
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
144
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
145
0
     "%s: unable to create segment files cache.",
146
0
     function );
147
148
0
    goto on_error;
149
0
  }
150
3.23k
  ( *segment_table )->maximum_segment_size = maximum_segment_size;
151
152
3.23k
  return( 1 );
153
154
0
on_error:
155
0
  if( *segment_table != NULL )
156
0
  {
157
0
    if( ( *segment_table )->segment_files_list != NULL )
158
0
    {
159
0
      libfdata_list_free(
160
0
       &( ( *segment_table )->segment_files_list ),
161
0
       NULL );
162
0
    }
163
0
    memory_free(
164
0
     *segment_table );
165
166
0
    *segment_table = NULL;
167
0
  }
168
0
  return( -1 );
169
3.23k
}
170
171
/* Frees a segment table
172
 * Returns 1 if successful or -1 on error
173
 */
174
int libewf_segment_table_free(
175
     libewf_segment_table_t **segment_table,
176
     libcerror_error_t **error )
177
3.23k
{
178
3.23k
  static char *function = "libewf_segment_table_free";
179
3.23k
  int result            = 1;
180
181
3.23k
  if( segment_table == NULL )
182
0
  {
183
0
    libcerror_error_set(
184
0
     error,
185
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
186
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
187
0
     "%s: invalid segment table.",
188
0
     function );
189
190
0
    return( -1 );
191
0
  }
192
3.23k
  if( *segment_table != NULL )
193
3.23k
  {
194
3.23k
    if( libewf_segment_table_clear(
195
3.23k
         *segment_table,
196
3.23k
         error ) != 1 )
197
0
    {
198
0
      libcerror_error_set(
199
0
       error,
200
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
201
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
202
0
       "%s: unable to clear segment table.",
203
0
       function );
204
205
0
      result = -1;
206
0
    }
207
3.23k
    if( libfcache_cache_free(
208
3.23k
         &( ( *segment_table )->segment_files_cache ),
209
3.23k
         error ) != 1 )
210
0
    {
211
0
      libcerror_error_set(
212
0
       error,
213
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
214
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
215
0
       "%s: unable to free segment files cache.",
216
0
       function );
217
218
0
      result = -1;
219
0
    }
220
3.23k
    if( libfdata_list_free(
221
3.23k
         &( ( *segment_table )->segment_files_list ),
222
3.23k
         error ) != 1 )
223
0
    {
224
0
      libcerror_error_set(
225
0
       error,
226
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
227
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
228
0
       "%s: unable to free segment files list.",
229
0
       function );
230
231
0
      result = -1;
232
0
    }
233
3.23k
    memory_free(
234
3.23k
     *segment_table );
235
236
3.23k
    *segment_table = NULL;
237
3.23k
  }
238
3.23k
  return( result );
239
3.23k
}
240
241
/* Clears the segment table
242
 * Returns 1 if successful or -1 on error
243
 */
244
int libewf_segment_table_clear(
245
     libewf_segment_table_t *segment_table,
246
     libcerror_error_t **error )
247
4.42k
{
248
4.42k
  static char *function = "libewf_segment_table_clear";
249
250
4.42k
  if( segment_table == NULL )
251
0
  {
252
0
    libcerror_error_set(
253
0
     error,
254
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
255
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
256
0
     "%s: invalid segment table.",
257
0
     function );
258
259
0
    return( -1 );
260
0
  }
261
4.42k
  if( segment_table->basename != NULL )
262
0
  {
263
0
    memory_free(
264
0
     segment_table->basename );
265
266
0
    segment_table->basename = NULL;
267
0
  }
268
4.42k
  if( libfdata_list_empty(
269
4.42k
       segment_table->segment_files_list,
270
4.42k
       error ) != 1 )
271
0
  {
272
0
    libcerror_error_set(
273
0
     error,
274
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
275
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
276
0
     "%s: unable to empty segment files list.",
277
0
     function );
278
279
0
    return( -1 );
280
0
  }
281
4.42k
  if( libfcache_cache_empty(
282
4.42k
       segment_table->segment_files_cache,
283
4.42k
       error ) != 1 )
284
0
  {
285
0
    libcerror_error_set(
286
0
     error,
287
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
288
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
289
0
     "%s: unable to empty segment files cache.",
290
0
     function );
291
292
0
    return( -1 );
293
0
  }
294
4.42k
  segment_table->maximum_segment_size = 0;
295
4.42k
  segment_table->number_of_segments   = 0;
296
4.42k
  segment_table->current_segment_file = NULL;
297
4.42k
  segment_table->flags                = 0;
298
299
4.42k
  return( 1 );
300
4.42k
}
301
302
/* Clones the segment table
303
 * Returns 1 if successful or -1 on error
304
 */
305
int libewf_segment_table_clone(
306
     libewf_segment_table_t **destination_segment_table,
307
     libewf_segment_table_t *source_segment_table,
308
     libcerror_error_t **error )
309
0
{
310
0
  static char *function = "libewf_segment_table_clone";
311
312
0
  if( destination_segment_table == 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 destination segment table.",
319
0
     function );
320
321
0
    return( -1 );
322
0
  }
323
0
  if( *destination_segment_table != NULL )
324
0
  {
325
0
    libcerror_error_set(
326
0
     error,
327
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
328
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
329
0
     "%s: invalid destination segment table value already set.",
330
0
     function );
331
332
0
    return( -1 );
333
0
  }
334
0
  if( source_segment_table == NULL )
335
0
  {
336
0
    *destination_segment_table = NULL;
337
338
0
    return( 1 );
339
0
  }
340
0
  *destination_segment_table = memory_allocate_structure(
341
0
                                libewf_segment_table_t );
342
343
0
  if( *destination_segment_table == NULL )
344
0
  {
345
0
    libcerror_error_set(
346
0
     error,
347
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
348
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
349
0
     "%s: unable to create destination segment table.",
350
0
     function );
351
352
0
    goto on_error;
353
0
  }
354
0
  if( memory_set(
355
0
       *destination_segment_table,
356
0
       0,
357
0
       sizeof( libewf_segment_table_t ) ) == NULL )
358
0
  {
359
0
    libcerror_error_set(
360
0
     error,
361
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
362
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
363
0
     "%s: unable to clear source to destination segment table.",
364
0
     function );
365
366
0
    memory_free(
367
0
     *destination_segment_table );
368
369
0
    *destination_segment_table = NULL;
370
371
0
    return( -1 );
372
0
  }
373
0
  if( source_segment_table->basename != NULL )
374
0
  {
375
0
    ( *destination_segment_table )->basename = system_string_allocate(
376
0
                              source_segment_table->basename_size );
377
378
0
    if( *destination_segment_table == NULL )
379
0
    {
380
0
      libcerror_error_set(
381
0
       error,
382
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
383
0
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
384
0
       "%s: unable to create destination segment table.",
385
0
       function );
386
387
0
      goto on_error;
388
0
    }
389
0
    if( memory_copy(
390
0
         ( *destination_segment_table )->basename,
391
0
         source_segment_table->basename,
392
0
         sizeof( system_character_t ) * source_segment_table->basename_size ) == NULL )
393
0
    {
394
0
      libcerror_error_set(
395
0
       error,
396
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
397
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
398
0
       "%s: unable to copy source to destination basename.",
399
0
       function );
400
401
0
      goto on_error;
402
0
    }
403
0
    ( *destination_segment_table )->basename_size = source_segment_table->basename_size;
404
0
  }
405
0
  if( libfdata_list_clone(
406
0
       &( ( *destination_segment_table )->segment_files_list ),
407
0
       source_segment_table->segment_files_list,
408
0
       error ) != 1 )
409
0
  {
410
0
    libcerror_error_set(
411
0
     error,
412
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
413
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
414
0
     "%s: unable to create destination segment files list.",
415
0
     function );
416
417
0
    goto on_error;
418
0
  }
419
0
  if( libfcache_cache_clone(
420
0
       &( ( *destination_segment_table )->segment_files_cache ),
421
0
       source_segment_table->segment_files_cache,
422
0
       error ) != 1 )
423
0
  {
424
0
    libcerror_error_set(
425
0
     error,
426
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
427
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
428
0
     "%s: unable to create destination segment files cache.",
429
0
     function );
430
431
0
    goto on_error;
432
0
  }
433
0
  ( *destination_segment_table )->maximum_segment_size = source_segment_table->maximum_segment_size;
434
435
0
  return( 1 );
436
437
0
on_error:
438
0
  if( *destination_segment_table != NULL )
439
0
  {
440
0
    if( ( *destination_segment_table )->segment_files_list != NULL )
441
0
    {
442
0
      libfdata_list_free(
443
0
       &( ( *destination_segment_table )->segment_files_list ),
444
0
       NULL );
445
0
    }
446
0
    if( ( *destination_segment_table )->basename != NULL )
447
0
    {
448
0
      memory_free(
449
0
       ( *destination_segment_table )->basename );
450
0
    }
451
0
    memory_free(
452
0
     *destination_segment_table );
453
454
0
    *destination_segment_table = NULL;
455
0
  }
456
0
  return( -1 );
457
0
}
458
459
/* Retrieves the size of the basename
460
 * Returns 1 if successful, 0 if not set or -1 on error
461
 */
462
int libewf_segment_table_get_basename_size(
463
     libewf_segment_table_t *segment_table,
464
     size_t *basename_size,
465
     libcerror_error_t **error )
466
0
{
467
0
  static char *function = "libewf_segment_table_get_basename_size";
468
469
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
470
  int result            = 0;
471
#endif
472
473
0
  if( segment_table == NULL )
474
0
  {
475
0
    libcerror_error_set(
476
0
     error,
477
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
478
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
479
0
     "%s: invalid segment table.",
480
0
     function );
481
482
0
    return( -1 );
483
0
  }
484
0
  if( basename_size == NULL )
485
0
  {
486
0
    libcerror_error_set(
487
0
     error,
488
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
489
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
490
0
     "%s: invalid basename size.",
491
0
     function );
492
493
0
    return( -1 );
494
0
  }
495
0
  if( segment_table->basename == NULL )
496
0
  {
497
0
    return( 0 );
498
0
  }
499
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
500
  if( libclocale_codepage == 0 )
501
  {
502
#if SIZEOF_WCHAR_T == 4
503
    result = libuna_utf8_string_size_from_utf32(
504
              (libuna_utf32_character_t *) segment_table->basename,
505
              segment_table->basename_size,
506
              basename_size,
507
              error );
508
#elif SIZEOF_WCHAR_T == 2
509
    result = libuna_utf8_string_size_from_utf16(
510
              (libuna_utf16_character_t *) segment_table->basename,
511
              segment_table->basename_size,
512
              basename_size,
513
              error );
514
#else
515
#error Unsupported size of wchar_t
516
#endif /* SIZEOF_WCHAR_T */
517
  }
518
  else
519
  {
520
#if SIZEOF_WCHAR_T == 4
521
    result = libuna_byte_stream_size_from_utf32(
522
              (libuna_utf32_character_t *) segment_table->basename,
523
              segment_table->basename_size,
524
              libclocale_codepage,
525
              basename_size,
526
              error );
527
#elif SIZEOF_WCHAR_T == 2
528
    result = libuna_byte_stream_size_from_utf16(
529
              (libuna_utf16_character_t *) segment_table->basename,
530
              segment_table->basename_size,
531
              libclocale_codepage,
532
              basename_size,
533
              error );
534
#else
535
#error Unsupported size of wchar_t
536
#endif /* SIZEOF_WCHAR_T */
537
  }
538
  if( result != 1 )
539
  {
540
    libcerror_error_set(
541
     error,
542
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
543
     LIBCERROR_CONVERSION_ERROR_GENERIC,
544
     "%s: unable to determine basename size.",
545
     function );
546
547
    return( -1 );
548
  }
549
#else
550
0
  *basename_size = segment_table->basename_size;
551
0
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
552
553
0
  return( 1 );
554
0
}
555
556
/* Retrieves the basename
557
 * Returns 1 if successful, 0 if not set or -1 on error
558
 */
559
int libewf_segment_table_get_basename(
560
     libewf_segment_table_t *segment_table,
561
     char *basename,
562
     size_t basename_size,
563
     libcerror_error_t **error )
564
0
{
565
0
  static char *function       = "libewf_segment_table_get_basename";
566
0
  size_t narrow_basename_size = 0;
567
568
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
569
  int result                  = 0;
570
#endif
571
572
0
  if( segment_table == NULL )
573
0
  {
574
0
    libcerror_error_set(
575
0
     error,
576
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
577
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
578
0
     "%s: invalid segment table.",
579
0
     function );
580
581
0
    return( -1 );
582
0
  }
583
0
  if( basename == NULL )
584
0
  {
585
0
    libcerror_error_set(
586
0
     error,
587
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
588
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
589
0
     "%s: invalid basename.",
590
0
     function );
591
592
0
    return( -1 );
593
0
  }
594
0
  if( segment_table->basename == NULL )
595
0
  {
596
0
    return( 0 );
597
0
  }
598
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
599
  if( libclocale_codepage == 0 )
600
  {
601
#if SIZEOF_WCHAR_T == 4
602
    result = libuna_utf8_string_size_from_utf32(
603
              (libuna_utf32_character_t *) segment_table->basename,
604
              segment_table->basename_size,
605
              &narrow_basename_size,
606
              error );
607
#elif SIZEOF_WCHAR_T == 2
608
    result = libuna_utf8_string_size_from_utf16(
609
              (libuna_utf16_character_t *) segment_table->basename,
610
              segment_table->basename_size,
611
              &narrow_basename_size,
612
              error );
613
#else
614
#error Unsupported size of wchar_t
615
#endif /* SIZEOF_WCHAR_T */
616
  }
617
  else
618
  {
619
#if SIZEOF_WCHAR_T == 4
620
    result = libuna_byte_stream_size_from_utf32(
621
              (libuna_utf32_character_t *) segment_table->basename,
622
              segment_table->basename_size,
623
              libclocale_codepage,
624
              &narrow_basename_size,
625
              error );
626
#elif SIZEOF_WCHAR_T == 2
627
    result = libuna_byte_stream_size_from_utf16(
628
              (libuna_utf16_character_t *) segment_table->basename,
629
              segment_table->basename_size,
630
              libclocale_codepage,
631
              &narrow_basename_size,
632
              error );
633
#else
634
#error Unsupported size of wchar_t
635
#endif /* SIZEOF_WCHAR_T */
636
  }
637
  if( result != 1 )
638
  {
639
    libcerror_error_set(
640
     error,
641
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
642
     LIBCERROR_CONVERSION_ERROR_GENERIC,
643
     "%s: unable to determine narrow basename size.",
644
     function );
645
646
    return( -1 );
647
  }
648
#else
649
0
  narrow_basename_size = segment_table->basename_size;
650
0
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
651
652
0
  if( basename_size < narrow_basename_size )
653
0
  {
654
0
    libcerror_error_set(
655
0
     error,
656
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
657
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
658
0
     "%s: basename too small.",
659
0
     function );
660
661
0
    return( -1 );
662
0
  }
663
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
664
  if( libclocale_codepage == 0 )
665
  {
666
#if SIZEOF_WCHAR_T == 4
667
    result = libuna_utf8_string_copy_from_utf32(
668
              (libuna_utf8_character_t *) basename,
669
              basename_size,
670
              (libuna_utf32_character_t *) segment_table->basename,
671
              segment_table->basename_size,
672
              error );
673
#elif SIZEOF_WCHAR_T == 2
674
    result = libuna_utf8_string_copy_from_utf16(
675
              (libuna_utf8_character_t *) basename,
676
              basename_size,
677
              (libuna_utf16_character_t *) segment_table->basename,
678
              segment_table->basename_size,
679
              error );
680
#else
681
#error Unsupported size of wchar_t
682
#endif /* SIZEOF_WCHAR_T */
683
  }
684
  else
685
  {
686
#if SIZEOF_WCHAR_T == 4
687
    result = libuna_byte_stream_copy_from_utf32(
688
              (uint8_t *) basename,
689
              basename_size,
690
              libclocale_codepage,
691
              (libuna_utf32_character_t *) segment_table->basename,
692
              segment_table->basename_size,
693
              error );
694
#elif SIZEOF_WCHAR_T == 2
695
    result = libuna_byte_stream_copy_from_utf16(
696
              (uint8_t *) basename,
697
              basename_size,
698
              libclocale_codepage,
699
              (libuna_utf16_character_t *) segment_table->basename,
700
              segment_table->basename_size,
701
              error );
702
#else
703
#error Unsupported size of wchar_t
704
#endif /* SIZEOF_WCHAR_T */
705
  }
706
  if( result != 1 )
707
  {
708
    libcerror_error_set(
709
     error,
710
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
711
     LIBCERROR_CONVERSION_ERROR_GENERIC,
712
     "%s: unable to set basename.",
713
     function );
714
715
    return( -1 );
716
  }
717
#else
718
0
  if( system_string_copy(
719
0
       basename,
720
0
       segment_table->basename,
721
0
       segment_table->basename_size ) == NULL )
722
0
  {
723
0
    libcerror_error_set(
724
0
     error,
725
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
726
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
727
0
     "%s: unable to set basename.",
728
0
     function );
729
730
0
    return( -1 );
731
0
  }
732
0
  basename[ segment_table->basename_size - 1 ] = 0;
733
0
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
734
735
0
  return( 1 );
736
0
}
737
738
/* Sets the basename
739
 * Returns 1 if successful or -1 on error
740
 */
741
int libewf_segment_table_set_basename(
742
     libewf_segment_table_t *segment_table,
743
     const char *basename,
744
     size_t basename_length,
745
     libcerror_error_t **error )
746
0
{
747
0
  static char *function = "libewf_segment_table_set_basename";
748
749
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
750
  int result            = 0;
751
#endif
752
753
0
  if( segment_table == NULL )
754
0
  {
755
0
    libcerror_error_set(
756
0
     error,
757
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
758
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
759
0
     "%s: invalid segment table.",
760
0
     function );
761
762
0
    return( -1 );
763
0
  }
764
0
  if( basename == NULL )
765
0
  {
766
0
    libcerror_error_set(
767
0
     error,
768
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
769
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
770
0
     "%s: invalid basename.",
771
0
     function );
772
773
0
    return( -1 );
774
0
  }
775
0
  if( segment_table->basename != NULL )
776
0
  {
777
0
    memory_free(
778
0
     segment_table->basename );
779
780
0
    segment_table->basename      = NULL;
781
0
    segment_table->basename_size = 0;
782
0
  }
783
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
784
  if( libclocale_codepage == 0 )
785
  {
786
#if SIZEOF_WCHAR_T == 4
787
    result = libuna_utf32_string_size_from_utf8(
788
              (libuna_utf8_character_t *) basename,
789
              basename_length + 1,
790
              &( segment_table->basename_size ),
791
              error );
792
#elif SIZEOF_WCHAR_T == 2
793
    result = libuna_utf16_string_size_from_utf8(
794
              (libuna_utf8_character_t *) basename,
795
              basename_length + 1,
796
              &( segment_table->basename_size ),
797
              error );
798
#else
799
#error Unsupported size of wchar_t
800
#endif /* SIZEOF_WCHAR_T */
801
  }
802
  else
803
  {
804
#if SIZEOF_WCHAR_T == 4
805
    result = libuna_utf32_string_size_from_byte_stream(
806
              (uint8_t *) basename,
807
              basename_length + 1,
808
              libclocale_codepage,
809
              &( segment_table->basename_size ),
810
              error );
811
#elif SIZEOF_WCHAR_T == 2
812
    result = libuna_utf16_string_size_from_byte_stream(
813
              (uint8_t *) basename,
814
              basename_length + 1,
815
              libclocale_codepage,
816
              &( segment_table->basename_size ),
817
              error );
818
#else
819
#error Unsupported size of wchar_t
820
#endif /* SIZEOF_WCHAR_T */
821
  }
822
  if( result != 1 )
823
  {
824
    libcerror_error_set(
825
     error,
826
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
827
     LIBCERROR_CONVERSION_ERROR_GENERIC,
828
     "%s: unable to determine basename size.",
829
     function );
830
831
    return( -1 );
832
  }
833
#else
834
0
  segment_table->basename_size = basename_length + 1;
835
0
#endif
836
0
  segment_table->basename = system_string_allocate(
837
0
                             segment_table->basename_size );
838
839
0
  if( segment_table->basename == NULL )
840
0
  {
841
0
    libcerror_error_set(
842
0
     error,
843
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
844
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
845
0
     "%s: unable to create basename.",
846
0
     function );
847
848
0
    segment_table->basename_size = 0;
849
850
0
    return( -1 );
851
0
  }
852
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
853
  if( libclocale_codepage == 0 )
854
  {
855
#if SIZEOF_WCHAR_T == 4
856
    result = libuna_utf32_string_copy_from_utf8(
857
              (libuna_utf32_character_t *) segment_table->basename,
858
              segment_table->basename_size,
859
              (libuna_utf8_character_t *) basename,
860
              basename_length + 1,
861
              error );
862
#elif SIZEOF_WCHAR_T == 2
863
    result = libuna_utf16_string_copy_from_utf8(
864
              (libuna_utf16_character_t *) segment_table->basename,
865
              segment_table->basename_size,
866
              (libuna_utf8_character_t *) basename,
867
              basename_length + 1,
868
              error );
869
#else
870
#error Unsupported size of wchar_t
871
#endif /* SIZEOF_WCHAR_T */
872
  }
873
  else
874
  {
875
#if SIZEOF_WCHAR_T == 4
876
    result = libuna_utf32_string_copy_from_byte_stream(
877
              (libuna_utf32_character_t *) segment_table->basename,
878
              segment_table->basename_size,
879
              (uint8_t *) basename,
880
              basename_length + 1,
881
              libclocale_codepage,
882
              error );
883
#elif SIZEOF_WCHAR_T == 2
884
    result = libuna_utf16_string_copy_from_byte_stream(
885
              (libuna_utf16_character_t *) segment_table->basename,
886
              segment_table->basename_size,
887
              (uint8_t *) basename,
888
              basename_length + 1,
889
              libclocale_codepage,
890
              error );
891
#else
892
#error Unsupported size of wchar_t
893
#endif /* SIZEOF_WCHAR_T */
894
  }
895
  if( result != 1 )
896
  {
897
    libcerror_error_set(
898
     error,
899
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
900
     LIBCERROR_CONVERSION_ERROR_GENERIC,
901
     "%s: unable to set basename.",
902
     function );
903
904
    memory_free(
905
     segment_table->basename );
906
907
    segment_table->basename      = NULL;
908
    segment_table->basename_size = 0;
909
910
    return( -1 );
911
  }
912
#else
913
0
  if( system_string_copy(
914
0
       segment_table->basename,
915
0
       basename,
916
0
       basename_length ) == NULL )
917
0
  {
918
0
    libcerror_error_set(
919
0
     error,
920
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
921
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
922
0
     "%s: unable to set basename.",
923
0
     function );
924
925
0
    memory_free(
926
0
     segment_table->basename );
927
928
0
    segment_table->basename      = NULL;
929
0
    segment_table->basename_size = 0;
930
931
0
    return( -1 );
932
0
  }
933
0
  segment_table->basename[ basename_length ] = 0;
934
0
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
935
936
0
  return( 1 );
937
0
}
938
939
#if defined( HAVE_WIDE_CHARACTER_TYPE )
940
941
/* Retrieves the size of the basename
942
 * Returns 1 if successful, 0 if not set or -1 on error
943
 */
944
int libewf_segment_table_get_basename_size_wide(
945
     libewf_segment_table_t *segment_table,
946
     size_t *basename_size,
947
     libcerror_error_t **error )
948
{
949
  static char *function = "libewf_segment_table_get_basename_size_wide";
950
951
#if !defined( HAVE_WIDE_SYSTEM_CHARACTER )
952
  int result            = 0;
953
#endif
954
955
  if( segment_table == NULL )
956
  {
957
    libcerror_error_set(
958
     error,
959
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
960
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
961
     "%s: invalid segment table.",
962
     function );
963
964
    return( -1 );
965
  }
966
  if( basename_size == NULL )
967
  {
968
    libcerror_error_set(
969
     error,
970
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
971
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
972
     "%s: invalid basename size.",
973
     function );
974
975
    return( -1 );
976
  }
977
  if( segment_table->basename == NULL )
978
  {
979
    return( 0 );
980
  }
981
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
982
  *basename_size = segment_table->basename_size;
983
#else
984
  if( libclocale_codepage == 0 )
985
  {
986
#if SIZEOF_WCHAR_T == 4
987
    result = libuna_utf32_string_size_from_utf8(
988
              (libuna_utf8_character_t *) segment_table->basename,
989
              segment_table->basename_size,
990
              basename_size,
991
              error );
992
#elif SIZEOF_WCHAR_T == 2
993
    result = libuna_utf16_string_size_from_utf8(
994
              (libuna_utf8_character_t *) segment_table->basename,
995
              segment_table->basename_size,
996
              basename_size,
997
              error );
998
#else
999
#error Unsupported size of wchar_t
1000
#endif /* SIZEOF_WCHAR_T */
1001
  }
1002
  else
1003
  {
1004
#if SIZEOF_WCHAR_T == 4
1005
    result = libuna_utf32_string_size_from_byte_stream(
1006
              (uint8_t *) segment_table->basename,
1007
              segment_table->basename_size,
1008
              libclocale_codepage,
1009
              basename_size,
1010
              error );
1011
#elif SIZEOF_WCHAR_T == 2
1012
    result = libuna_utf16_string_size_from_byte_stream(
1013
              (uint8_t *) segment_table->basename,
1014
              segment_table->basename_size,
1015
              libclocale_codepage,
1016
              basename_size,
1017
              error );
1018
#else
1019
#error Unsupported size of wchar_t
1020
#endif /* SIZEOF_WCHAR_T */
1021
  }
1022
  if( result != 1 )
1023
  {
1024
    libcerror_error_set(
1025
     error,
1026
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
1027
     LIBCERROR_CONVERSION_ERROR_GENERIC,
1028
     "%s: unable to determine basename size.",
1029
     function );
1030
1031
    return( -1 );
1032
  }
1033
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
1034
  return( 1 );
1035
}
1036
1037
/* Retrieves the basename
1038
 * Returns 1 if successful, 0 if not set or -1 on error
1039
 */
1040
int libewf_segment_table_get_basename_wide(
1041
     libewf_segment_table_t *segment_table,
1042
     wchar_t *basename,
1043
     size_t basename_size,
1044
     libcerror_error_t **error )
1045
{
1046
  static char *function     = "libewf_segment_table_get_basename_wide";
1047
  size_t wide_basename_size = 0;
1048
1049
#if !defined( HAVE_WIDE_SYSTEM_CHARACTER )
1050
  int result                = 0;
1051
#endif
1052
1053
  if( segment_table == NULL )
1054
  {
1055
    libcerror_error_set(
1056
     error,
1057
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1058
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1059
     "%s: invalid segment table.",
1060
     function );
1061
1062
    return( -1 );
1063
  }
1064
  if( basename == NULL )
1065
  {
1066
    libcerror_error_set(
1067
     error,
1068
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1069
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1070
     "%s: invalid basename.",
1071
     function );
1072
1073
    return( -1 );
1074
  }
1075
  if( segment_table->basename == NULL )
1076
  {
1077
    return( 0 );
1078
  }
1079
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1080
  wide_basename_size = segment_table->basename_size;
1081
#else
1082
  if( libclocale_codepage == 0 )
1083
  {
1084
#if SIZEOF_WCHAR_T == 4
1085
    result = libuna_utf32_string_size_from_utf8(
1086
              (libuna_utf8_character_t *) segment_table->basename,
1087
              segment_table->basename_size,
1088
              &wide_basename_size,
1089
              error );
1090
#elif SIZEOF_WCHAR_T == 2
1091
    result = libuna_utf16_string_size_from_utf8(
1092
              (libuna_utf8_character_t *) segment_table->basename,
1093
              segment_table->basename_size,
1094
              &wide_basename_size,
1095
              error );
1096
#else
1097
#error Unsupported size of wchar_t
1098
#endif /* SIZEOF_WCHAR_T */
1099
  }
1100
  else
1101
  {
1102
#if SIZEOF_WCHAR_T == 4
1103
    result = libuna_utf32_string_size_from_byte_stream(
1104
              (uint8_t *) segment_table->basename,
1105
              segment_table->basename_size,
1106
              libclocale_codepage,
1107
              &wide_basename_size,
1108
              error );
1109
#elif SIZEOF_WCHAR_T == 2
1110
    result = libuna_utf16_string_size_from_byte_stream(
1111
              (uint8_t *) segment_table->basename,
1112
              segment_table->basename_size,
1113
              libclocale_codepage,
1114
              &wide_basename_size,
1115
              error );
1116
#else
1117
#error Unsupported size of wchar_t
1118
#endif /* SIZEOF_WCHAR_T */
1119
  }
1120
  if( result != 1 )
1121
  {
1122
    libcerror_error_set(
1123
     error,
1124
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
1125
     LIBCERROR_CONVERSION_ERROR_GENERIC,
1126
     "%s: unable to determine basename size.",
1127
     function );
1128
1129
    return( -1 );
1130
  }
1131
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
1132
  if( basename_size < wide_basename_size )
1133
  {
1134
    libcerror_error_set(
1135
     error,
1136
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1137
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1138
     "%s: basename too small.",
1139
     function );
1140
1141
    return( -1 );
1142
  }
1143
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1144
  if( system_string_copy(
1145
       basename,
1146
       segment_table->basename,
1147
       segment_table->basename_size ) == NULL )
1148
  {
1149
    libcerror_error_set(
1150
     error,
1151
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1152
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1153
     "%s: unable to set basename.",
1154
     function );
1155
1156
    return( -1 );
1157
  }
1158
  basename[ segment_table->basename_size - 1 ] = 0;
1159
#else
1160
  if( libclocale_codepage == 0 )
1161
  {
1162
#if SIZEOF_WCHAR_T == 4
1163
    result = libuna_utf32_string_copy_from_utf8(
1164
              (libuna_utf32_character_t *) basename,
1165
              basename_size,
1166
              (libuna_utf8_character_t *) segment_table->basename,
1167
              segment_table->basename_size,
1168
              error );
1169
#elif SIZEOF_WCHAR_T == 2
1170
    result = libuna_utf16_string_copy_from_utf8(
1171
              (libuna_utf16_character_t *) basename,
1172
              basename_size,
1173
              (libuna_utf8_character_t *) segment_table->basename,
1174
              segment_table->basename_size,
1175
              error );
1176
#else
1177
#error Unsupported size of wchar_t
1178
#endif /* SIZEOF_WCHAR_T */
1179
  }
1180
  else
1181
  {
1182
#if SIZEOF_WCHAR_T == 4
1183
    result = libuna_utf32_string_copy_from_byte_stream(
1184
              (libuna_utf32_character_t *) basename,
1185
              basename_size,
1186
              (uint8_t *) segment_table->basename,
1187
              segment_table->basename_size,
1188
              libclocale_codepage,
1189
              error );
1190
#elif SIZEOF_WCHAR_T == 2
1191
    result = libuna_utf16_string_copy_from_byte_stream(
1192
              (libuna_utf16_character_t *) basename,
1193
              basename_size,
1194
              (uint8_t *) segment_table->basename,
1195
              segment_table->basename_size,
1196
              libclocale_codepage,
1197
              error );
1198
#else
1199
#error Unsupported size of wchar_t
1200
#endif /* SIZEOF_WCHAR_T */
1201
  }
1202
  if( result != 1 )
1203
  {
1204
    libcerror_error_set(
1205
     error,
1206
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
1207
     LIBCERROR_CONVERSION_ERROR_GENERIC,
1208
     "%s: unable to set basename.",
1209
     function );
1210
1211
    return( -1 );
1212
  }
1213
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
1214
  return( 1 );
1215
}
1216
1217
/* Sets the basename
1218
 * Returns 1 if successful or -1 on error
1219
 */
1220
int libewf_segment_table_set_basename_wide(
1221
     libewf_segment_table_t *segment_table,
1222
     const wchar_t *basename,
1223
     size_t basename_length,
1224
     libcerror_error_t **error )
1225
{
1226
  static char *function = "libewf_segment_table_set_basename_wide";
1227
1228
#if !defined( HAVE_WIDE_SYSTEM_CHARACTER )
1229
  int result            = 0;
1230
#endif
1231
1232
  if( segment_table == NULL )
1233
  {
1234
    libcerror_error_set(
1235
     error,
1236
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1237
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1238
     "%s: invalid segment table.",
1239
     function );
1240
1241
    return( -1 );
1242
  }
1243
  if( basename == NULL )
1244
  {
1245
    libcerror_error_set(
1246
     error,
1247
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1248
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1249
     "%s: invalid basename.",
1250
     function );
1251
1252
    return( -1 );
1253
  }
1254
  if( segment_table->basename != NULL )
1255
  {
1256
    memory_free(
1257
     segment_table->basename );
1258
1259
    segment_table->basename      = NULL;
1260
    segment_table->basename_size = 0;
1261
  }
1262
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1263
  segment_table->basename_size = basename_length + 1;
1264
#else
1265
  if( libclocale_codepage == 0 )
1266
  {
1267
#if SIZEOF_WCHAR_T == 4
1268
    result = libuna_utf8_string_size_from_utf32(
1269
              (libuna_utf32_character_t *) basename,
1270
              basename_length + 1,
1271
              &( segment_table->basename_size ),
1272
              error );
1273
#elif SIZEOF_WCHAR_T == 2
1274
    result = libuna_utf8_string_size_from_utf16(
1275
              (libuna_utf16_character_t *) basename,
1276
              basename_length + 1,
1277
              &( segment_table->basename_size ),
1278
              error );
1279
#else
1280
#error Unsupported size of wchar_t
1281
#endif /* SIZEOF_WCHAR_T */
1282
  }
1283
  else
1284
  {
1285
#if SIZEOF_WCHAR_T == 4
1286
    result = libuna_byte_stream_size_from_utf32(
1287
              (libuna_utf32_character_t *) basename,
1288
              basename_length + 1,
1289
              libclocale_codepage,
1290
              &( segment_table->basename_size ),
1291
              error );
1292
#elif SIZEOF_WCHAR_T == 2
1293
    result = libuna_byte_stream_size_from_utf16(
1294
              (libuna_utf16_character_t *) basename,
1295
              basename_length + 1,
1296
              libclocale_codepage,
1297
              &( segment_table->basename_size ),
1298
              error );
1299
#else
1300
#error Unsupported size of wchar_t
1301
#endif /* SIZEOF_WCHAR_T */
1302
  }
1303
  if( result != 1 )
1304
  {
1305
    libcerror_error_set(
1306
     error,
1307
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
1308
     LIBCERROR_CONVERSION_ERROR_GENERIC,
1309
     "%s: unable to determine basename size.",
1310
     function );
1311
1312
    return( -1 );
1313
  }
1314
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
1315
  segment_table->basename = system_string_allocate(
1316
                             segment_table->basename_size );
1317
1318
  if( segment_table->basename == NULL )
1319
  {
1320
    libcerror_error_set(
1321
     error,
1322
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1323
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1324
     "%s: unable to create basename.",
1325
     function );
1326
1327
    return( -1 );
1328
  }
1329
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1330
  if( system_string_copy(
1331
       segment_table->basename,
1332
       basename,
1333
       basename_length ) == NULL )
1334
  {
1335
    libcerror_error_set(
1336
     error,
1337
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1338
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1339
     "%s: unable to set basename.",
1340
     function );
1341
1342
    memory_free(
1343
     segment_table->basename );
1344
1345
    segment_table->basename      = NULL;
1346
    segment_table->basename_size = 0;
1347
1348
    return( -1 );
1349
  }
1350
  segment_table->basename[ basename_length ] = 0;
1351
#else
1352
  if( libclocale_codepage == 0 )
1353
  {
1354
#if SIZEOF_WCHAR_T == 4
1355
    result = libuna_utf8_string_copy_from_utf32(
1356
              (libuna_utf8_character_t *) segment_table->basename,
1357
              segment_table->basename_size,
1358
              (libuna_utf32_character_t *) basename,
1359
              basename_length + 1,
1360
              error );
1361
#elif SIZEOF_WCHAR_T == 2
1362
    result = libuna_utf8_string_copy_from_utf16(
1363
              (libuna_utf8_character_t *) segment_table->basename,
1364
              segment_table->basename_size,
1365
              (libuna_utf16_character_t *) basename,
1366
              basename_length + 1,
1367
              error );
1368
#else
1369
#error Unsupported size of wchar_t
1370
#endif /* SIZEOF_WCHAR_T */
1371
  }
1372
  else
1373
  {
1374
#if SIZEOF_WCHAR_T == 4
1375
    result = libuna_byte_stream_copy_from_utf32(
1376
              (uint8_t *) segment_table->basename,
1377
              segment_table->basename_size,
1378
              libclocale_codepage,
1379
              (libuna_utf32_character_t *) basename,
1380
              basename_length + 1,
1381
              error );
1382
#elif SIZEOF_WCHAR_T == 2
1383
    result = libuna_byte_stream_copy_from_utf16(
1384
              (uint8_t *) segment_table->basename,
1385
              segment_table->basename_size,
1386
              libclocale_codepage,
1387
              (libuna_utf16_character_t *) basename,
1388
              basename_length + 1,
1389
              error );
1390
#else
1391
#error Unsupported size of wchar_t
1392
#endif /* SIZEOF_WCHAR_T */
1393
  }
1394
  if( result != 1 )
1395
  {
1396
    libcerror_error_set(
1397
     error,
1398
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
1399
     LIBCERROR_CONVERSION_ERROR_GENERIC,
1400
     "%s: unable to set basename.",
1401
     function );
1402
1403
    memory_free(
1404
     segment_table->basename );
1405
1406
    segment_table->basename      = NULL;
1407
    segment_table->basename_size = 0;
1408
1409
    return( -1 );
1410
  }
1411
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
1412
  return( 1 );
1413
}
1414
1415
#endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
1416
1417
/* Sets the maximum segment size
1418
 * Returns 1 if successful or -1 on error
1419
 */
1420
int libewf_segment_table_set_maximum_segment_size(
1421
     libewf_segment_table_t *segment_table,
1422
     size64_t maximum_segment_size,
1423
     libcerror_error_t **error )
1424
0
{
1425
0
  static char *function = "libewf_segment_table_set_maximum_segment_size";
1426
1427
0
  if( segment_table == NULL )
1428
0
  {
1429
0
    libcerror_error_set(
1430
0
     error,
1431
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1432
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1433
0
     "%s: invalid segment table.",
1434
0
     function );
1435
1436
0
    return( -1 );
1437
0
  }
1438
0
  segment_table->maximum_segment_size = maximum_segment_size;
1439
1440
0
  return( 1 );
1441
0
}
1442
1443
/* Retrieves the number of segments
1444
 * Returns 1 if successful or -1 on error
1445
 */
1446
int libewf_segment_table_get_number_of_segments(
1447
     libewf_segment_table_t *segment_table,
1448
     uint32_t *number_of_segments,
1449
     libcerror_error_t **error )
1450
3.06k
{
1451
3.06k
  static char *function = "libewf_segment_table_get_number_of_segments";
1452
1453
3.06k
  if( segment_table == NULL )
1454
0
  {
1455
0
    libcerror_error_set(
1456
0
     error,
1457
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1458
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1459
0
     "%s: invalid segment table.",
1460
0
     function );
1461
1462
0
    return( -1 );
1463
0
  }
1464
3.06k
  if( number_of_segments == NULL )
1465
0
  {
1466
0
    libcerror_error_set(
1467
0
     error,
1468
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1469
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1470
0
     "%s: invalid number of segments.",
1471
0
     function );
1472
1473
0
    return( -1 );
1474
0
  }
1475
3.06k
  *number_of_segments = segment_table->number_of_segments;
1476
1477
3.06k
  return( 1 );
1478
3.06k
}
1479
1480
/* Retrieves a specific segment from the segment table
1481
 * Returns 1 if successful or -1 on error
1482
 */
1483
int libewf_segment_table_get_segment_by_index(
1484
     libewf_segment_table_t *segment_table,
1485
     uint32_t segment_number,
1486
     int *file_io_pool_entry,
1487
     size64_t *segment_file_size,
1488
     libcerror_error_t **error )
1489
2.81k
{
1490
2.81k
  static char *function  = "libewf_segment_table_get_segment_by_index";
1491
2.81k
  off64_t element_offset = 0;
1492
2.81k
  uint32_t element_flags = 0;
1493
1494
2.81k
  if( segment_table == NULL )
1495
0
  {
1496
0
    libcerror_error_set(
1497
0
     error,
1498
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1499
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1500
0
     "%s: invalid segment table.",
1501
0
     function );
1502
1503
0
    return( -1 );
1504
0
  }
1505
2.81k
#if SIZEOF_INT <= 4
1506
2.81k
  if( segment_number > (uint32_t) INT_MAX )
1507
0
  {
1508
0
    libcerror_error_set(
1509
0
     error,
1510
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1511
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1512
0
     "%s: invalid segment number value out of bounds.",
1513
0
     function );
1514
1515
0
    return( -1 );
1516
0
  }
1517
2.81k
#endif
1518
2.81k
  if( libfdata_list_get_element_by_index(
1519
2.81k
       segment_table->segment_files_list,
1520
2.81k
       (int) segment_number,
1521
2.81k
       file_io_pool_entry,
1522
2.81k
       &element_offset,
1523
2.81k
       segment_file_size,
1524
2.81k
       &element_flags,
1525
2.81k
       error ) != 1 )
1526
0
  {
1527
0
    libcerror_error_set(
1528
0
     error,
1529
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1530
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1531
0
     "%s: unable to retrieve element: %" PRIu32 " from segment files list.",
1532
0
     function,
1533
0
     segment_number );
1534
1535
0
    return( -1 );
1536
0
  }
1537
2.81k
  return( 1 );
1538
2.81k
}
1539
1540
/* Retrieves a segment at a specific offset from the segment table
1541
 * Returns 1 if successful, 0 if not or -1 on error
1542
 */
1543
int libewf_segment_table_get_segment_at_offset(
1544
     libewf_segment_table_t *segment_table,
1545
     off64_t offset,
1546
     int *file_io_pool_entry,
1547
     size64_t *segment_file_size,
1548
     libcerror_error_t **error )
1549
0
{
1550
0
  static char *function            = "libewf_segment_table_get_segment_at_offset";
1551
0
  off64_t element_offset           = 0;
1552
0
  off64_t segment_file_data_offset = 0;
1553
0
  uint32_t element_flags           = 0;
1554
0
  int result                       = 0;
1555
0
  int segment_number               = 0;
1556
1557
0
  if( segment_table == NULL )
1558
0
  {
1559
0
    libcerror_error_set(
1560
0
     error,
1561
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1562
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1563
0
     "%s: invalid segment table.",
1564
0
     function );
1565
1566
0
    return( -1 );
1567
0
  }
1568
0
  result = libfdata_list_get_element_at_offset(
1569
0
            segment_table->segment_files_list,
1570
0
            offset,
1571
0
            &segment_number,
1572
0
            &segment_file_data_offset,
1573
0
            file_io_pool_entry,
1574
0
            &element_offset,
1575
0
            segment_file_size,
1576
0
            &element_flags,
1577
0
            error );
1578
1579
0
  if( result == -1 )
1580
0
  {
1581
0
    libcerror_error_set(
1582
0
     error,
1583
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1584
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1585
0
     "%s: unable to retrieve element at offset: %" PRIi64 " (0x%08" PRIx64 ") from segment files list.",
1586
0
     function,
1587
0
     offset,
1588
0
     offset );
1589
1590
0
    return( -1 );
1591
0
  }
1592
0
  return( result );
1593
0
}
1594
1595
/* Retrieves the segment storage media size for a specific segment in the segment table
1596
 * Returns 1 if successful, 0 if not set or -1 on error
1597
 */
1598
int libewf_segment_table_get_segment_storage_media_size_by_index(
1599
     libewf_segment_table_t *segment_table,
1600
     uint32_t segment_number,
1601
     size64_t *storage_media_size,
1602
     libcerror_error_t **error )
1603
0
{
1604
0
  static char *function = "libewf_segment_table_get_segment_storage_media_size_by_index";
1605
0
  int result            = 0;
1606
1607
0
  if( segment_table == NULL )
1608
0
  {
1609
0
    libcerror_error_set(
1610
0
     error,
1611
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1612
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1613
0
     "%s: invalid segment table.",
1614
0
     function );
1615
1616
0
    return( -1 );
1617
0
  }
1618
0
#if SIZEOF_INT <= 4
1619
0
  if( segment_number > (uint32_t) INT_MAX )
1620
0
  {
1621
0
    libcerror_error_set(
1622
0
     error,
1623
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1624
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1625
0
     "%s: invalid segment number value out of bounds.",
1626
0
     function );
1627
1628
0
    return( -1 );
1629
0
  }
1630
0
#endif
1631
0
  result = libfdata_list_get_mapped_size_by_index(
1632
0
            segment_table->segment_files_list,
1633
0
            (int) segment_number,
1634
0
            storage_media_size,
1635
0
            error );
1636
1637
0
  if( result == -1 )
1638
0
  {
1639
0
    libcerror_error_set(
1640
0
     error,
1641
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1642
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1643
0
     "%s: unable to get mapped size of element: %" PRIu32 " in segment files list.",
1644
0
     function,
1645
0
     segment_number );
1646
1647
0
    return( -1 );
1648
0
  }
1649
0
  return( result );
1650
0
}
1651
1652
/* Sets the segment storage media size for a specific segment in the segment table
1653
 * Returns 1 if successful or -1 on error
1654
 */
1655
int libewf_segment_table_set_segment_storage_media_size_by_index(
1656
     libewf_segment_table_t *segment_table,
1657
     uint32_t segment_number,
1658
     size64_t storage_media_size,
1659
     libcerror_error_t **error )
1660
1.18k
{
1661
1.18k
  static char *function = "libewf_segment_table_set_segment_storage_media_size_by_index";
1662
1663
1.18k
  if( segment_table == NULL )
1664
0
  {
1665
0
    libcerror_error_set(
1666
0
     error,
1667
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1668
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1669
0
     "%s: invalid segment table.",
1670
0
     function );
1671
1672
0
    return( -1 );
1673
0
  }
1674
1.18k
#if SIZEOF_INT <= 4
1675
1.18k
  if( segment_number > (uint32_t) INT_MAX )
1676
0
  {
1677
0
    libcerror_error_set(
1678
0
     error,
1679
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1680
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1681
0
     "%s: invalid segment number value out of bounds.",
1682
0
     function );
1683
1684
0
    return( -1 );
1685
0
  }
1686
1.18k
#endif
1687
1.18k
  if( libfdata_list_set_mapped_size_by_index(
1688
1.18k
       segment_table->segment_files_list,
1689
1.18k
       (int) segment_number,
1690
1.18k
       storage_media_size,
1691
1.18k
       error ) != 1 )
1692
0
  {
1693
0
    libcerror_error_set(
1694
0
     error,
1695
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1696
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1697
0
     "%s: unable to set mapped size of element: %" PRIu32 " in segment files list.",
1698
0
     function,
1699
0
     segment_number );
1700
1701
0
    return( -1 );
1702
0
  }
1703
1.18k
  return( 1 );
1704
1.18k
}
1705
1706
/* Retrieves a specific segment file from the segment table
1707
 * Returns 1 if successful or -1 on error
1708
 */
1709
int libewf_segment_table_get_segment_file_by_index(
1710
     libewf_segment_table_t *segment_table,
1711
     uint32_t segment_number,
1712
     libbfio_pool_t *file_io_pool,
1713
     libewf_segment_file_t **segment_file,
1714
     libcerror_error_t **error )
1715
6.05k
{
1716
6.05k
  static char *function = "libewf_segment_table_get_segment_file_by_index";
1717
1718
6.05k
  if( segment_table == NULL )
1719
0
  {
1720
0
    libcerror_error_set(
1721
0
     error,
1722
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1723
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1724
0
     "%s: invalid segment table.",
1725
0
     function );
1726
1727
0
    return( -1 );
1728
0
  }
1729
6.05k
#if SIZEOF_INT <= 4
1730
6.05k
  if( segment_number > (uint32_t) INT_MAX )
1731
0
  {
1732
0
    libcerror_error_set(
1733
0
     error,
1734
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1735
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1736
0
     "%s: invalid segment number value out of bounds.",
1737
0
     function );
1738
1739
0
    return( -1 );
1740
0
  }
1741
6.05k
#endif
1742
6.05k
  if( segment_file == NULL )
1743
0
  {
1744
0
    libcerror_error_set(
1745
0
     error,
1746
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1747
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1748
0
     "%s: invalid segment file.",
1749
0
     function );
1750
1751
0
    return( -1 );
1752
0
  }
1753
6.05k
  if( libfdata_list_get_element_value_by_index(
1754
6.05k
       segment_table->segment_files_list,
1755
6.05k
       (intptr_t *) file_io_pool,
1756
6.05k
       (libfdata_cache_t *) segment_table->segment_files_cache,
1757
6.05k
       (int) segment_number,
1758
6.05k
       (intptr_t **) &( segment_table->current_segment_file ),
1759
6.05k
       0,
1760
6.05k
       error ) != 1 )
1761
86
  {
1762
86
    libcerror_error_set(
1763
86
     error,
1764
86
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1765
86
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1766
86
     "%s: unable to retrieve element value: %" PRIu32 " from segment files list.",
1767
86
     function,
1768
86
     segment_number );
1769
1770
86
    return( -1 );
1771
86
  }
1772
5.96k
  if( segment_table->current_segment_file == NULL )
1773
0
  {
1774
0
    libcerror_error_set(
1775
0
     error,
1776
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1777
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1778
0
     "%s: missing segment file.",
1779
0
     function );
1780
1781
0
    return( -1 );
1782
0
  }
1783
5.96k
  if( ( (int64_t) segment_number + 1 ) != (int64_t) segment_table->current_segment_file->segment_number )
1784
0
  {
1785
0
    libcerror_error_set(
1786
0
     error,
1787
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1788
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1789
0
     "%s: invalid segment number value out of bounds.",
1790
0
     function );
1791
1792
0
    return( -1 );
1793
0
  }
1794
5.96k
  if( libfdata_list_get_element_mapped_range(
1795
5.96k
       segment_table->segment_files_list,
1796
5.96k
       segment_number,
1797
5.96k
       &( segment_table->current_segment_file->range_start_offset ),
1798
5.96k
       (size64_t *) &( segment_table->current_segment_file->range_end_offset ),
1799
5.96k
       error ) != 1 )
1800
0
  {
1801
0
    libcerror_error_set(
1802
0
     error,
1803
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1804
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1805
0
     "%s: unable to retrieve segment files list element: %" PRIu32 " mapped range.",
1806
0
     function,
1807
0
     segment_number );
1808
1809
0
    return( -1 );
1810
0
  }
1811
5.96k
  segment_table->current_segment_file->range_end_offset += segment_table->current_segment_file->range_start_offset;
1812
1813
5.96k
  *segment_file = segment_table->current_segment_file;
1814
1815
5.96k
  return( 1 );
1816
5.96k
}
1817
1818
/* Retrieves a segment file at a specific offset from the segment table
1819
 * Returns 1 if successful, 0 if not or -1 on error
1820
 */
1821
int libewf_segment_table_get_segment_file_at_offset(
1822
     libewf_segment_table_t *segment_table,
1823
     off64_t offset,
1824
     libbfio_pool_t *file_io_pool,
1825
     uint32_t *segment_number,
1826
     off64_t *segment_file_data_offset,
1827
     libewf_segment_file_t **segment_file,
1828
     libcerror_error_t **error )
1829
0
{
1830
0
  static char *function                 = "libewf_segment_table_get_segment_file_at_offset";
1831
0
  off64_t safe_segment_file_data_offset = 0;
1832
0
  int result                            = 0;
1833
0
  int segment_files_list_index          = 0;
1834
1835
0
  if( segment_table == NULL )
1836
0
  {
1837
0
    libcerror_error_set(
1838
0
     error,
1839
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1840
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1841
0
     "%s: invalid segment table.",
1842
0
     function );
1843
1844
0
    return( -1 );
1845
0
  }
1846
0
  if( segment_number == NULL )
1847
0
  {
1848
0
    libcerror_error_set(
1849
0
     error,
1850
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1851
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1852
0
     "%s: invalid segment number.",
1853
0
     function );
1854
1855
0
    return( -1 );
1856
0
  }
1857
0
  if( segment_file_data_offset == NULL )
1858
0
  {
1859
0
    libcerror_error_set(
1860
0
     error,
1861
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1862
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1863
0
     "%s: invalid segment file data offset.",
1864
0
     function );
1865
1866
0
    return( -1 );
1867
0
  }
1868
0
  if( segment_file == NULL )
1869
0
  {
1870
0
    libcerror_error_set(
1871
0
     error,
1872
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1873
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1874
0
     "%s: invalid segment file.",
1875
0
     function );
1876
1877
0
    return( -1 );
1878
0
  }
1879
0
  if( ( segment_table->current_segment_file != NULL )
1880
0
   && ( segment_table->current_segment_file->range_end_offset > 0 ) )
1881
0
  {
1882
0
    if( ( offset >= segment_table->current_segment_file->range_start_offset )
1883
0
     && ( offset < segment_table->current_segment_file->range_end_offset ) )
1884
0
    {
1885
0
      safe_segment_file_data_offset = offset - segment_table->current_segment_file->range_start_offset;
1886
1887
0
      result = 1;
1888
0
    }
1889
0
  }
1890
0
  if( result == 0 )
1891
0
  {
1892
0
    result = libfdata_list_get_element_value_at_offset(
1893
0
              segment_table->segment_files_list,
1894
0
              (intptr_t *) file_io_pool,
1895
0
              (libfdata_cache_t *) segment_table->segment_files_cache,
1896
0
              offset,
1897
0
              &segment_files_list_index,
1898
0
              &safe_segment_file_data_offset,
1899
0
              (intptr_t **) &( segment_table->current_segment_file ),
1900
0
              0,
1901
0
              error );
1902
1903
0
    if( result == -1 )
1904
0
    {
1905
0
      libcerror_error_set(
1906
0
       error,
1907
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1908
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1909
0
       "%s: unable to retrieve segment file for offset: %" PRIi64 " (0x%08" PRIx64 ").",
1910
0
       function,
1911
0
       offset,
1912
0
       offset );
1913
1914
0
      return( -1 );
1915
0
    }
1916
0
    else if( result != 0 )
1917
0
    {
1918
0
      if( segment_table->current_segment_file == NULL )
1919
0
      {
1920
0
        libcerror_error_set(
1921
0
         error,
1922
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1923
0
         LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1924
0
         "%s: missing segment file.",
1925
0
         function );
1926
1927
0
        return( -1 );
1928
0
      }
1929
0
      if( ( (int64_t) segment_files_list_index + 1 ) != (int64_t) segment_table->current_segment_file->segment_number )
1930
0
      {
1931
0
        libcerror_error_set(
1932
0
         error,
1933
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1934
0
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1935
0
         "%s: invalid segment number value out of bounds.",
1936
0
         function );
1937
1938
0
        return( -1 );
1939
0
      }
1940
0
      if( libfdata_list_get_element_mapped_range(
1941
0
           segment_table->segment_files_list,
1942
0
           segment_files_list_index,
1943
0
           &( segment_table->current_segment_file->range_start_offset ),
1944
0
           (size64_t *) &( segment_table->current_segment_file->range_end_offset ),
1945
0
           error ) != 1 )
1946
0
      {
1947
0
        libcerror_error_set(
1948
0
         error,
1949
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1950
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1951
0
         "%s: unable to retrieve segment files list element: %d mapped range.",
1952
0
         function,
1953
0
         segment_files_list_index );
1954
1955
0
        return( -1 );
1956
0
      }
1957
0
      segment_table->current_segment_file->range_end_offset += segment_table->current_segment_file->range_start_offset;
1958
0
    }
1959
0
  }
1960
0
  if( result != 0 )
1961
0
  {
1962
0
    *segment_number           = segment_table->current_segment_file->segment_number - 1;
1963
0
    *segment_file_data_offset = safe_segment_file_data_offset;
1964
0
    *segment_file             = segment_table->current_segment_file;
1965
0
  }
1966
0
  return( result );
1967
0
}
1968
1969
/* Appends a segment to the segment table based on the segment file
1970
 * Returns 1 if successful or -1 on error
1971
 */
1972
int libewf_segment_table_append_segment_by_segment_file(
1973
     libewf_segment_table_t *segment_table,
1974
     libewf_segment_file_t *segment_file,
1975
     int file_io_pool_entry,
1976
     size64_t segment_file_size,
1977
     libcerror_error_t **error )
1978
3.14k
{
1979
3.14k
  static char *function = "libewf_segment_table_append_segment_by_segment_file";
1980
1981
3.14k
  if( segment_table == NULL )
1982
0
  {
1983
0
    libcerror_error_set(
1984
0
     error,
1985
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1986
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1987
0
     "%s: invalid segment table.",
1988
0
     function );
1989
1990
0
    return( -1 );
1991
0
  }
1992
3.14k
  if( segment_file == NULL )
1993
0
  {
1994
0
    libcerror_error_set(
1995
0
     error,
1996
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1997
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1998
0
     "%s: invalid segment file.",
1999
0
     function );
2000
2001
0
    return( -1 );
2002
0
  }
2003
3.14k
#if SIZEOF_INT <= 4
2004
3.14k
  if( segment_file->segment_number > (uint32_t) INT_MAX )
2005
31
  {
2006
31
    libcerror_error_set(
2007
31
     error,
2008
31
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2009
31
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2010
31
     "%s: invalid segment file - segment number value out of bounds.",
2011
31
     function );
2012
2013
31
    return( -1 );
2014
31
  }
2015
3.10k
#endif
2016
3.10k
  if( segment_file->segment_number > segment_table->number_of_segments )
2017
3.10k
  {
2018
3.10k
    if( libfdata_list_resize(
2019
3.10k
         segment_table->segment_files_list,
2020
3.10k
         (int) segment_file->segment_number,
2021
3.10k
         error ) != 1 )
2022
40
    {
2023
40
      libcerror_error_set(
2024
40
       error,
2025
40
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2026
40
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2027
40
       "%s: unable to resize segment files list.",
2028
40
       function );
2029
2030
40
      return( -1 );
2031
40
    }
2032
3.06k
    segment_table->number_of_segments = segment_file->segment_number;
2033
3.06k
  }
2034
3.06k
  if( libfdata_list_set_element_by_index(
2035
3.06k
       segment_table->segment_files_list,
2036
3.06k
       (int) ( segment_file->segment_number - 1 ),
2037
3.06k
       file_io_pool_entry,
2038
3.06k
       0,
2039
3.06k
       segment_file_size,
2040
3.06k
       0,
2041
3.06k
       error ) != 1 )
2042
0
  {
2043
0
    libcerror_error_set(
2044
0
     error,
2045
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2046
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2047
0
     "%s: unable to set element: %" PRIu32 " in segment files list.",
2048
0
     function,
2049
0
     segment_file->segment_number );
2050
2051
0
    return( -1 );
2052
0
  }
2053
3.06k
  return( 1 );
2054
3.06k
}
2055