Coverage Report

Created: 2025-06-13 07:22

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