Coverage Report

Created: 2023-06-07 06:53

/src/libodraw/libodraw/libodraw_data_file_descriptor.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Data file descriptor functions
3
 *
4
 * Copyright (C) 2010-2023, 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 "libodraw_data_file_descriptor.h"
30
#include "libodraw_libcerror.h"
31
#include "libodraw_libclocale.h"
32
#include "libodraw_libuna.h"
33
34
/* Creates a data file descriptor
35
 * Make sure the value data_file_descriptor is referencing, is set to NULL
36
 * Returns 1 if successful or -1 on error
37
 */
38
int libodraw_data_file_descriptor_initialize(
39
     libodraw_data_file_descriptor_t **data_file_descriptor,
40
     libcerror_error_t **error )
41
619k
{
42
619k
  static char *function = "libodraw_data_file_descriptor_initialize";
43
44
619k
  if( data_file_descriptor == NULL )
45
0
  {
46
0
    libcerror_error_set(
47
0
     error,
48
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
49
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
50
0
     "%s: invalid data file descriptor.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
619k
  if( *data_file_descriptor != NULL )
56
0
  {
57
0
    libcerror_error_set(
58
0
     error,
59
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
60
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
61
0
     "%s: invalid data file descriptor value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
619k
  *data_file_descriptor = memory_allocate_structure(
67
619k
                           libodraw_data_file_descriptor_t );
68
69
619k
  if( *data_file_descriptor == NULL )
70
0
  {
71
0
    libcerror_error_set(
72
0
     error,
73
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
74
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
75
0
     "%s: unable to create data file descriptor.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
619k
  if( memory_set(
81
619k
       *data_file_descriptor,
82
619k
       0,
83
619k
       sizeof( libodraw_data_file_descriptor_t ) ) == NULL )
84
0
  {
85
0
    libcerror_error_set(
86
0
     error,
87
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
88
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
89
0
     "%s: unable to clear data file descriptor.",
90
0
     function );
91
92
0
    goto on_error;
93
0
  }
94
619k
  return( 1 );
95
96
0
on_error:
97
0
  if( *data_file_descriptor != NULL )
98
0
  {
99
0
    memory_free(
100
0
     *data_file_descriptor );
101
102
0
    *data_file_descriptor = NULL;
103
0
  }
104
0
  return( -1 );
105
619k
}
106
107
/* Frees a data file descriptor
108
 * Returns 1 if successful or -1 on error
109
 */
110
int libodraw_data_file_descriptor_free(
111
     libodraw_data_file_descriptor_t **data_file_descriptor,
112
     libcerror_error_t **error )
113
619k
{
114
619k
  static char *function = "libodraw_data_file_descriptor_free";
115
116
619k
  if( data_file_descriptor == NULL )
117
0
  {
118
0
    libcerror_error_set(
119
0
     error,
120
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
121
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
122
0
     "%s: invalid data file descriptor.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
619k
  if( *data_file_descriptor != NULL )
128
619k
  {
129
619k
    if( ( *data_file_descriptor )->name != NULL )
130
619k
    {
131
619k
      memory_free(
132
619k
       ( *data_file_descriptor )->name );
133
619k
    }
134
619k
    memory_free(
135
619k
     *data_file_descriptor );
136
137
619k
    *data_file_descriptor = NULL;
138
619k
  }
139
619k
  return( 1 );
140
619k
}
141
142
/* Retrieves the size of the name
143
 * Returns 1 if successful or -1 on error
144
 */
145
int libodraw_data_file_descriptor_get_name_size(
146
     libodraw_data_file_descriptor_t *data_file_descriptor,
147
     size_t *name_size,
148
     libcerror_error_t **error )
149
0
{
150
0
  static char *function = "libodraw_data_file_descriptor_get_name_size";
151
152
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
153
  int result            = 0;
154
#endif
155
156
0
  if( data_file_descriptor == NULL )
157
0
  {
158
0
    libcerror_error_set(
159
0
     error,
160
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
161
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
162
0
     "%s: invalid data file descriptor.",
163
0
     function );
164
165
0
    return( -1 );
166
0
  }
167
0
  if( data_file_descriptor->name == NULL )
168
0
  {
169
0
    libcerror_error_set(
170
0
     error,
171
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
172
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
173
0
     "%s: invalid data file descriptor - missing name.",
174
0
     function );
175
176
0
    return( -1 );
177
0
  }
178
0
  if( name_size == NULL )
179
0
  {
180
0
    libcerror_error_set(
181
0
     error,
182
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
183
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
184
0
     "%s: invalid name size.",
185
0
     function );
186
187
0
    return( -1 );
188
0
  }
189
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
190
  if( libclocale_codepage == 0 )
191
  {
192
#if SIZEOF_WCHAR_T == 4
193
    result = libuna_utf8_string_size_from_utf32(
194
              (libuna_utf32_character_t *) data_file_descriptor->name,
195
              data_file_descriptor->name_size,
196
              name_size,
197
              error );
198
#elif SIZEOF_WCHAR_T == 2
199
    result = libuna_utf8_string_size_from_utf16(
200
              (libuna_utf16_character_t *) data_file_descriptor->name,
201
              data_file_descriptor->name_size,
202
              name_size,
203
              error );
204
#else
205
#error Unsupported size of wchar_t
206
#endif /* SIZEOF_WCHAR_T */
207
  }
208
  else
209
  {
210
#if SIZEOF_WCHAR_T == 4
211
    result = libuna_byte_stream_size_from_utf32(
212
              (libuna_utf32_character_t *) data_file_descriptor->name,
213
              data_file_descriptor->name_size,
214
              libclocale_codepage,
215
              name_size,
216
              error );
217
#elif SIZEOF_WCHAR_T == 2
218
    result = libuna_byte_stream_size_from_utf16(
219
              (libuna_utf16_character_t *) data_file_descriptor->name,
220
              data_file_descriptor->name_size,
221
              libclocale_codepage,
222
              name_size,
223
              error );
224
#else
225
#error Unsupported size of wchar_t
226
#endif /* SIZEOF_WCHAR_T */
227
  }
228
  if( result != 1 )
229
  {
230
    libcerror_error_set(
231
     error,
232
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
233
     LIBCERROR_CONVERSION_ERROR_GENERIC,
234
     "%s: unable to determine name size.",
235
     function );
236
237
    return( -1 );
238
  }
239
#else
240
0
  *name_size = data_file_descriptor->name_size;
241
0
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
242
243
0
  return( 1 );
244
0
}
245
246
/* Retrieves the name
247
 * Returns 1 if successful or -1 on error
248
 */
249
int libodraw_data_file_descriptor_get_name(
250
     libodraw_data_file_descriptor_t *data_file_descriptor,
251
     char *name,
252
     size_t name_size,
253
     libcerror_error_t **error )
254
0
{
255
0
  static char *function   = "libodraw_data_file_descriptor_get_name";
256
0
  size_t narrow_name_size = 0;
257
258
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
259
  int result              = 0;
260
#endif
261
262
0
  if( data_file_descriptor == NULL )
263
0
  {
264
0
    libcerror_error_set(
265
0
     error,
266
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
267
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
268
0
     "%s: invalid data file descriptor.",
269
0
     function );
270
271
0
    return( -1 );
272
0
  }
273
0
  if( data_file_descriptor->name == NULL )
274
0
  {
275
0
    libcerror_error_set(
276
0
     error,
277
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
278
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
279
0
     "%s: invalid data file descriptor - missing name.",
280
0
     function );
281
282
0
    return( -1 );
283
0
  }
284
0
  if( name == NULL )
285
0
  {
286
0
    libcerror_error_set(
287
0
     error,
288
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
289
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
290
0
     "%s: invalid name.",
291
0
     function );
292
293
0
    return( -1 );
294
0
  }
295
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
296
  if( libclocale_codepage == 0 )
297
  {
298
#if SIZEOF_WCHAR_T == 4
299
    result = libuna_utf8_string_size_from_utf32(
300
              (libuna_utf32_character_t *) data_file_descriptor->name,
301
              data_file_descriptor->name_size,
302
              &narrow_name_size,
303
              error );
304
#elif SIZEOF_WCHAR_T == 2
305
    result = libuna_utf8_string_size_from_utf16(
306
              (libuna_utf16_character_t *) data_file_descriptor->name,
307
              data_file_descriptor->name_size,
308
              &narrow_name_size,
309
              error );
310
#else
311
#error Unsupported size of wchar_t
312
#endif /* SIZEOF_WCHAR_T */
313
  }
314
  else
315
  {
316
#if SIZEOF_WCHAR_T == 4
317
    result = libuna_byte_stream_size_from_utf32(
318
              (libuna_utf32_character_t *) data_file_descriptor->name,
319
              data_file_descriptor->name_size,
320
              libclocale_codepage,
321
              &narrow_name_size,
322
              error );
323
#elif SIZEOF_WCHAR_T == 2
324
    result = libuna_byte_stream_size_from_utf16(
325
              (libuna_utf16_character_t *) data_file_descriptor->name,
326
              data_file_descriptor->name_size,
327
              libclocale_codepage,
328
              &narrow_name_size,
329
              error );
330
#else
331
#error Unsupported size of wchar_t
332
#endif /* SIZEOF_WCHAR_T */
333
  }
334
  if( result != 1 )
335
  {
336
    libcerror_error_set(
337
     error,
338
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
339
     LIBCERROR_CONVERSION_ERROR_GENERIC,
340
     "%s: unable to determine narrow name size.",
341
     function );
342
343
    return( -1 );
344
  }
345
#else
346
0
  narrow_name_size = data_file_descriptor->name_size;
347
0
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
348
349
0
  if( name_size < narrow_name_size )
350
0
  {
351
0
    libcerror_error_set(
352
0
     error,
353
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
354
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
355
0
     "%s: name too small.",
356
0
     function );
357
358
0
    return( -1 );
359
0
  }
360
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
361
  if( libclocale_codepage == 0 )
362
  {
363
#if SIZEOF_WCHAR_T == 4
364
    result = libuna_utf8_string_copy_from_utf32(
365
              (libuna_utf8_character_t *) name,
366
              name_size,
367
              (libuna_utf32_character_t *) data_file_descriptor->name,
368
              data_file_descriptor->name_size,
369
              error );
370
#elif SIZEOF_WCHAR_T == 2
371
    result = libuna_utf8_string_copy_from_utf16(
372
              (libuna_utf8_character_t *) name,
373
              name_size,
374
              (libuna_utf16_character_t *) data_file_descriptor->name,
375
              data_file_descriptor->name_size,
376
              error );
377
#else
378
#error Unsupported size of wchar_t
379
#endif /* SIZEOF_WCHAR_T */
380
  }
381
  else
382
  {
383
#if SIZEOF_WCHAR_T == 4
384
    result = libuna_byte_stream_copy_from_utf32(
385
              (uint8_t *) name,
386
              name_size,
387
              libclocale_codepage,
388
              (libuna_utf32_character_t *) data_file_descriptor->name,
389
              data_file_descriptor->name_size,
390
              error );
391
#elif SIZEOF_WCHAR_T == 2
392
    result = libuna_byte_stream_copy_from_utf16(
393
              (uint8_t *) name,
394
              name_size,
395
              libclocale_codepage,
396
              (libuna_utf16_character_t *) data_file_descriptor->name,
397
              data_file_descriptor->name_size,
398
              error );
399
#else
400
#error Unsupported size of wchar_t
401
#endif /* SIZEOF_WCHAR_T */
402
  }
403
  if( result != 1 )
404
  {
405
    libcerror_error_set(
406
     error,
407
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
408
     LIBCERROR_CONVERSION_ERROR_GENERIC,
409
     "%s: unable to set name.",
410
     function );
411
412
    return( -1 );
413
  }
414
#else
415
0
  if( system_string_copy(
416
0
       name,
417
0
       data_file_descriptor->name,
418
0
       data_file_descriptor->name_size ) == NULL )
419
0
  {
420
0
    libcerror_error_set(
421
0
     error,
422
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
423
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
424
0
     "%s: unable to set name.",
425
0
     function );
426
427
0
    return( -1 );
428
0
  }
429
0
  name[ data_file_descriptor->name_size - 1 ] = 0;
430
0
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
431
432
0
  return( 1 );
433
0
}
434
435
/* Sets the name
436
 * Returns 1 if successful or -1 on error
437
 */
438
int libodraw_data_file_descriptor_set_name(
439
     libodraw_data_file_descriptor_t *data_file_descriptor,
440
     const char *name,
441
     size_t name_length,
442
     libcerror_error_t **error )
443
619k
{
444
619k
  static char *function = "libodraw_data_file_descriptor_set_name";
445
446
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
447
  int result            = 0;
448
#endif
449
450
619k
  if( data_file_descriptor == NULL )
451
0
  {
452
0
    libcerror_error_set(
453
0
     error,
454
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
455
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
456
0
     "%s: invalid data file descriptor.",
457
0
     function );
458
459
0
    return( -1 );
460
0
  }
461
619k
  if( name == NULL )
462
0
  {
463
0
    libcerror_error_set(
464
0
     error,
465
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
466
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
467
0
     "%s: invalid name.",
468
0
     function );
469
470
0
    return( -1 );
471
0
  }
472
619k
  if( data_file_descriptor->name != NULL )
473
0
  {
474
0
    memory_free(
475
0
     data_file_descriptor->name );
476
477
0
    data_file_descriptor->name      = NULL;
478
0
    data_file_descriptor->name_size = 0;
479
0
  }
480
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
481
  if( libclocale_codepage == 0 )
482
  {
483
#if SIZEOF_WCHAR_T == 4
484
    result = libuna_utf32_string_size_from_utf8(
485
              (libuna_utf8_character_t *) name,
486
              name_length + 1,
487
              &( data_file_descriptor->name_size ),
488
              error );
489
#elif SIZEOF_WCHAR_T == 2
490
    result = libuna_utf16_string_size_from_utf8(
491
              (libuna_utf8_character_t *) name,
492
              name_length + 1,
493
              &( data_file_descriptor->name_size ),
494
              error );
495
#else
496
#error Unsupported size of wchar_t
497
#endif /* SIZEOF_WCHAR_T */
498
  }
499
  else
500
  {
501
#if SIZEOF_WCHAR_T == 4
502
    result = libuna_utf32_string_size_from_byte_stream(
503
              (uint8_t *) name,
504
              name_length + 1,
505
              libclocale_codepage,
506
              &( data_file_descriptor->name_size ),
507
              error );
508
#elif SIZEOF_WCHAR_T == 2
509
    result = libuna_utf16_string_size_from_byte_stream(
510
              (uint8_t *) name,
511
              name_length + 1,
512
              libclocale_codepage,
513
              &( data_file_descriptor->name_size ),
514
              error );
515
#else
516
#error Unsupported size of wchar_t
517
#endif /* SIZEOF_WCHAR_T */
518
  }
519
  if( result != 1 )
520
  {
521
    libcerror_error_set(
522
     error,
523
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
524
     LIBCERROR_CONVERSION_ERROR_GENERIC,
525
     "%s: unable to determine name size.",
526
     function );
527
528
    return( -1 );
529
  }
530
#else
531
619k
  data_file_descriptor->name_size = name_length + 1;
532
619k
#endif
533
619k
  data_file_descriptor->name = system_string_allocate(
534
619k
                                data_file_descriptor->name_size );
535
536
619k
  if( data_file_descriptor->name == NULL )
537
0
  {
538
0
    libcerror_error_set(
539
0
     error,
540
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
541
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
542
0
     "%s: unable to create name.",
543
0
     function );
544
545
0
    data_file_descriptor->name_size = 0;
546
547
0
    return( -1 );
548
0
  }
549
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
550
  if( libclocale_codepage == 0 )
551
  {
552
#if SIZEOF_WCHAR_T == 4
553
    result = libuna_utf32_string_copy_from_utf8(
554
              (libuna_utf32_character_t *) data_file_descriptor->name,
555
              data_file_descriptor->name_size,
556
              (libuna_utf8_character_t *) name,
557
              name_length + 1,
558
              error );
559
#elif SIZEOF_WCHAR_T == 2
560
    result = libuna_utf16_string_copy_from_utf8(
561
              (libuna_utf16_character_t *) data_file_descriptor->name,
562
              data_file_descriptor->name_size,
563
              (libuna_utf8_character_t *) name,
564
              name_length + 1,
565
              error );
566
#else
567
#error Unsupported size of wchar_t
568
#endif /* SIZEOF_WCHAR_T */
569
  }
570
  else
571
  {
572
#if SIZEOF_WCHAR_T == 4
573
    result = libuna_utf32_string_copy_from_byte_stream(
574
              (libuna_utf32_character_t *) data_file_descriptor->name,
575
              data_file_descriptor->name_size,
576
              (uint8_t *) name,
577
              name_length + 1,
578
              libclocale_codepage,
579
              error );
580
#elif SIZEOF_WCHAR_T == 2
581
    result = libuna_utf16_string_copy_from_byte_stream(
582
              (libuna_utf16_character_t *) data_file_descriptor->name,
583
              data_file_descriptor->name_size,
584
              (uint8_t *) name,
585
              name_length + 1,
586
              libclocale_codepage,
587
              error );
588
#else
589
#error Unsupported size of wchar_t
590
#endif /* SIZEOF_WCHAR_T */
591
  }
592
  if( result != 1 )
593
  {
594
    libcerror_error_set(
595
     error,
596
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
597
     LIBCERROR_CONVERSION_ERROR_GENERIC,
598
     "%s: unable to set name.",
599
     function );
600
601
    memory_free(
602
     data_file_descriptor->name );
603
604
    data_file_descriptor->name      = NULL;
605
    data_file_descriptor->name_size = 0;
606
607
    return( -1 );
608
  }
609
#else
610
619k
  if( system_string_copy(
611
619k
       data_file_descriptor->name,
612
619k
       name,
613
619k
       name_length ) == NULL )
614
0
  {
615
0
    libcerror_error_set(
616
0
     error,
617
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
618
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
619
0
     "%s: unable to set name.",
620
0
     function );
621
622
0
    memory_free(
623
0
     data_file_descriptor->name );
624
625
0
    data_file_descriptor->name      = NULL;
626
0
    data_file_descriptor->name_size = 0;
627
628
0
    return( -1 );
629
0
  }
630
619k
  data_file_descriptor->name[ name_length ] = 0;
631
619k
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
632
633
619k
  return( 1 );
634
619k
}
635
636
#if defined( HAVE_WIDE_CHARACTER_TYPE )
637
638
/* Retrieves the size of the name
639
 * Returns 1 if successful or -1 on error
640
 */
641
int libodraw_data_file_descriptor_get_name_size_wide(
642
     libodraw_data_file_descriptor_t *data_file_descriptor,
643
     size_t *name_size,
644
     libcerror_error_t **error )
645
{
646
  static char *function = "libodraw_data_file_descriptor_get_name_size_wide";
647
648
#if !defined( HAVE_WIDE_SYSTEM_CHARACTER )
649
  int result            = 0;
650
#endif
651
652
  if( data_file_descriptor == NULL )
653
  {
654
    libcerror_error_set(
655
     error,
656
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
657
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
658
     "%s: invalid data file descriptor.",
659
     function );
660
661
    return( -1 );
662
  }
663
  if( data_file_descriptor->name == NULL )
664
  {
665
    libcerror_error_set(
666
     error,
667
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
668
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
669
     "%s: invalid data file descriptor - missing name.",
670
     function );
671
672
    return( -1 );
673
  }
674
  if( name_size == NULL )
675
  {
676
    libcerror_error_set(
677
     error,
678
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
679
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
680
     "%s: invalid name size.",
681
     function );
682
683
    return( -1 );
684
  }
685
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
686
  *name_size = data_file_descriptor->name_size;
687
#else
688
  if( libclocale_codepage == 0 )
689
  {
690
#if SIZEOF_WCHAR_T == 4
691
    result = libuna_utf32_string_size_from_utf8(
692
              (libuna_utf8_character_t *) data_file_descriptor->name,
693
              data_file_descriptor->name_size,
694
              name_size,
695
              error );
696
#elif SIZEOF_WCHAR_T == 2
697
    result = libuna_utf16_string_size_from_utf8(
698
              (libuna_utf8_character_t *) data_file_descriptor->name,
699
              data_file_descriptor->name_size,
700
              name_size,
701
              error );
702
#else
703
#error Unsupported size of wchar_t
704
#endif /* SIZEOF_WCHAR_T */
705
  }
706
  else
707
  {
708
#if SIZEOF_WCHAR_T == 4
709
    result = libuna_utf32_string_size_from_byte_stream(
710
              (uint8_t *) data_file_descriptor->name,
711
              data_file_descriptor->name_size,
712
              libclocale_codepage,
713
              name_size,
714
              error );
715
#elif SIZEOF_WCHAR_T == 2
716
    result = libuna_utf16_string_size_from_byte_stream(
717
              (uint8_t *) data_file_descriptor->name,
718
              data_file_descriptor->name_size,
719
              libclocale_codepage,
720
              name_size,
721
              error );
722
#else
723
#error Unsupported size of wchar_t
724
#endif /* SIZEOF_WCHAR_T */
725
  }
726
  if( result != 1 )
727
  {
728
    libcerror_error_set(
729
     error,
730
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
731
     LIBCERROR_CONVERSION_ERROR_GENERIC,
732
     "%s: unable to determine name size.",
733
     function );
734
735
    return( -1 );
736
  }
737
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
738
  return( 1 );
739
}
740
741
/* Retrieves the name
742
 * Returns 1 if successful or -1 on error
743
 */
744
int libodraw_data_file_descriptor_get_name_wide(
745
     libodraw_data_file_descriptor_t *data_file_descriptor,
746
     wchar_t *name,
747
     size_t name_size,
748
     libcerror_error_t **error )
749
{
750
  static char *function = "libodraw_data_file_descriptor_get_name_wide";
751
  size_t wide_name_size = 0;
752
753
#if !defined( HAVE_WIDE_SYSTEM_CHARACTER )
754
  int result            = 0;
755
#endif
756
757
  if( data_file_descriptor == NULL )
758
  {
759
    libcerror_error_set(
760
     error,
761
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
762
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
763
     "%s: invalid data file descriptor.",
764
     function );
765
766
    return( -1 );
767
  }
768
  if( data_file_descriptor->name == NULL )
769
  {
770
    libcerror_error_set(
771
     error,
772
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
773
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
774
     "%s: invalid data file descriptor - missing name.",
775
     function );
776
777
    return( -1 );
778
  }
779
  if( name == NULL )
780
  {
781
    libcerror_error_set(
782
     error,
783
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
784
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
785
     "%s: invalid name.",
786
     function );
787
788
    return( -1 );
789
  }
790
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
791
  wide_name_size = data_file_descriptor->name_size;
792
#else
793
  if( libclocale_codepage == 0 )
794
  {
795
#if SIZEOF_WCHAR_T == 4
796
    result = libuna_utf32_string_size_from_utf8(
797
              (libuna_utf8_character_t *) data_file_descriptor->name,
798
              data_file_descriptor->name_size,
799
              &wide_name_size,
800
              error );
801
#elif SIZEOF_WCHAR_T == 2
802
    result = libuna_utf16_string_size_from_utf8(
803
              (libuna_utf8_character_t *) data_file_descriptor->name,
804
              data_file_descriptor->name_size,
805
              &wide_name_size,
806
              error );
807
#else
808
#error Unsupported size of wchar_t
809
#endif /* SIZEOF_WCHAR_T */
810
  }
811
  else
812
  {
813
#if SIZEOF_WCHAR_T == 4
814
    result = libuna_utf32_string_size_from_byte_stream(
815
              (uint8_t *) data_file_descriptor->name,
816
              data_file_descriptor->name_size,
817
              libclocale_codepage,
818
              &wide_name_size,
819
              error );
820
#elif SIZEOF_WCHAR_T == 2
821
    result = libuna_utf16_string_size_from_byte_stream(
822
              (uint8_t *) data_file_descriptor->name,
823
              data_file_descriptor->name_size,
824
              libclocale_codepage,
825
              &wide_name_size,
826
              error );
827
#else
828
#error Unsupported size of wchar_t
829
#endif /* SIZEOF_WCHAR_T */
830
  }
831
  if( result != 1 )
832
  {
833
    libcerror_error_set(
834
     error,
835
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
836
     LIBCERROR_CONVERSION_ERROR_GENERIC,
837
     "%s: unable to determine name size.",
838
     function );
839
840
    return( -1 );
841
  }
842
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
843
  if( name_size < wide_name_size )
844
  {
845
    libcerror_error_set(
846
     error,
847
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
848
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
849
     "%s: name too small.",
850
     function );
851
852
    return( -1 );
853
  }
854
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
855
  if( system_string_copy(
856
       name,
857
       data_file_descriptor->name,
858
       data_file_descriptor->name_size ) == NULL )
859
  {
860
    libcerror_error_set(
861
     error,
862
     LIBCERROR_ERROR_DOMAIN_MEMORY,
863
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
864
     "%s: unable to set name.",
865
     function );
866
867
    return( -1 );
868
  }
869
  name[ data_file_descriptor->name_size - 1 ] = 0;
870
#else
871
  if( libclocale_codepage == 0 )
872
  {
873
#if SIZEOF_WCHAR_T == 4
874
    result = libuna_utf32_string_copy_from_utf8(
875
              (libuna_utf32_character_t *) name,
876
              name_size,
877
              (libuna_utf8_character_t *) data_file_descriptor->name,
878
              data_file_descriptor->name_size,
879
              error );
880
#elif SIZEOF_WCHAR_T == 2
881
    result = libuna_utf16_string_copy_from_utf8(
882
              (libuna_utf16_character_t *) name,
883
              name_size,
884
              (libuna_utf8_character_t *) data_file_descriptor->name,
885
              data_file_descriptor->name_size,
886
              error );
887
#else
888
#error Unsupported size of wchar_t
889
#endif /* SIZEOF_WCHAR_T */
890
  }
891
  else
892
  {
893
#if SIZEOF_WCHAR_T == 4
894
    result = libuna_utf32_string_copy_from_byte_stream(
895
              (libuna_utf32_character_t *) name,
896
              name_size,
897
              (uint8_t *) data_file_descriptor->name,
898
              data_file_descriptor->name_size,
899
              libclocale_codepage,
900
              error );
901
#elif SIZEOF_WCHAR_T == 2
902
    result = libuna_utf16_string_copy_from_byte_stream(
903
              (libuna_utf16_character_t *) name,
904
              name_size,
905
              (uint8_t *) data_file_descriptor->name,
906
              data_file_descriptor->name_size,
907
              libclocale_codepage,
908
              error );
909
#else
910
#error Unsupported size of wchar_t
911
#endif /* SIZEOF_WCHAR_T */
912
  }
913
  if( result != 1 )
914
  {
915
    libcerror_error_set(
916
     error,
917
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
918
     LIBCERROR_CONVERSION_ERROR_GENERIC,
919
     "%s: unable to set name.",
920
     function );
921
922
    return( -1 );
923
  }
924
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
925
  return( 1 );
926
}
927
928
/* Sets the name
929
 * Returns 1 if successful or -1 on error
930
 */
931
int libodraw_data_file_descriptor_set_name_wide(
932
     libodraw_data_file_descriptor_t *data_file_descriptor,
933
     const wchar_t *name,
934
     size_t name_length,
935
     libcerror_error_t **error )
936
{
937
  static char *function = "libodraw_data_file_descriptor_set_name_wide";
938
939
#if !defined( HAVE_WIDE_SYSTEM_CHARACTER )
940
  int result            = 0;
941
#endif
942
943
  if( data_file_descriptor == NULL )
944
  {
945
    libcerror_error_set(
946
     error,
947
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
948
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
949
     "%s: invalid data file descriptor.",
950
     function );
951
952
    return( -1 );
953
  }
954
  if( name == NULL )
955
  {
956
    libcerror_error_set(
957
     error,
958
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
959
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
960
     "%s: invalid name.",
961
     function );
962
963
    return( -1 );
964
  }
965
  if( data_file_descriptor->name != NULL )
966
  {
967
    memory_free(
968
     data_file_descriptor->name );
969
970
    data_file_descriptor->name      = NULL;
971
    data_file_descriptor->name_size = 0;
972
  }
973
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
974
  data_file_descriptor->name_size = name_length + 1;
975
#else
976
  if( libclocale_codepage == 0 )
977
  {
978
#if SIZEOF_WCHAR_T == 4
979
    result = libuna_utf8_string_size_from_utf32(
980
              (libuna_utf32_character_t *) name,
981
              name_length + 1,
982
              &( data_file_descriptor->name_size ),
983
              error );
984
#elif SIZEOF_WCHAR_T == 2
985
    result = libuna_utf8_string_size_from_utf16(
986
              (libuna_utf16_character_t *) name,
987
              name_length + 1,
988
              &( data_file_descriptor->name_size ),
989
              error );
990
#else
991
#error Unsupported size of wchar_t
992
#endif /* SIZEOF_WCHAR_T */
993
  }
994
  else
995
  {
996
#if SIZEOF_WCHAR_T == 4
997
    result = libuna_byte_stream_size_from_utf32(
998
              (libuna_utf32_character_t *) name,
999
              name_length + 1,
1000
              libclocale_codepage,
1001
              &( data_file_descriptor->name_size ),
1002
              error );
1003
#elif SIZEOF_WCHAR_T == 2
1004
    result = libuna_byte_stream_size_from_utf16(
1005
              (libuna_utf16_character_t *) name,
1006
              name_length + 1,
1007
              libclocale_codepage,
1008
              &( data_file_descriptor->name_size ),
1009
              error );
1010
#else
1011
#error Unsupported size of wchar_t
1012
#endif /* SIZEOF_WCHAR_T */
1013
  }
1014
  if( result != 1 )
1015
  {
1016
    libcerror_error_set(
1017
     error,
1018
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
1019
     LIBCERROR_CONVERSION_ERROR_GENERIC,
1020
     "%s: unable to determine name size.",
1021
     function );
1022
1023
    return( -1 );
1024
  }
1025
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
1026
  data_file_descriptor->name = system_string_allocate(
1027
                                data_file_descriptor->name_size );
1028
1029
  if( data_file_descriptor->name == NULL )
1030
  {
1031
    libcerror_error_set(
1032
     error,
1033
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1034
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1035
     "%s: unable to create name.",
1036
     function );
1037
1038
    return( -1 );
1039
  }
1040
#if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1041
  if( system_string_copy(
1042
       data_file_descriptor->name,
1043
       name,
1044
       name_length ) == NULL )
1045
  {
1046
    libcerror_error_set(
1047
     error,
1048
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1049
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1050
     "%s: unable to set name.",
1051
     function );
1052
1053
    memory_free(
1054
     data_file_descriptor->name );
1055
1056
    data_file_descriptor->name      = NULL;
1057
    data_file_descriptor->name_size = 0;
1058
1059
    return( -1 );
1060
  }
1061
  data_file_descriptor->name[ name_length ] = 0;
1062
#else
1063
  if( libclocale_codepage == 0 )
1064
  {
1065
#if SIZEOF_WCHAR_T == 4
1066
    result = libuna_utf8_string_copy_from_utf32(
1067
              (libuna_utf8_character_t *) data_file_descriptor->name,
1068
              data_file_descriptor->name_size,
1069
              (libuna_utf32_character_t *) name,
1070
              name_length + 1,
1071
              error );
1072
#elif SIZEOF_WCHAR_T == 2
1073
    result = libuna_utf8_string_copy_from_utf16(
1074
              (libuna_utf8_character_t *) data_file_descriptor->name,
1075
              data_file_descriptor->name_size,
1076
              (libuna_utf16_character_t *) name,
1077
              name_length + 1,
1078
              error );
1079
#else
1080
#error Unsupported size of wchar_t
1081
#endif /* SIZEOF_WCHAR_T */
1082
  }
1083
  else
1084
  {
1085
#if SIZEOF_WCHAR_T == 4
1086
    result = libuna_byte_stream_copy_from_utf32(
1087
              (uint8_t *) data_file_descriptor->name,
1088
              data_file_descriptor->name_size,
1089
              libclocale_codepage,
1090
              (libuna_utf32_character_t *) name,
1091
              name_length + 1,
1092
              error );
1093
#elif SIZEOF_WCHAR_T == 2
1094
    result = libuna_byte_stream_copy_from_utf16(
1095
              (uint8_t *) data_file_descriptor->name,
1096
              data_file_descriptor->name_size,
1097
              libclocale_codepage,
1098
              (libuna_utf16_character_t *) name,
1099
              name_length + 1,
1100
              error );
1101
#else
1102
#error Unsupported size of wchar_t
1103
#endif /* SIZEOF_WCHAR_T */
1104
  }
1105
  if( result != 1 )
1106
  {
1107
    libcerror_error_set(
1108
     error,
1109
     LIBCERROR_ERROR_DOMAIN_CONVERSION,
1110
     LIBCERROR_CONVERSION_ERROR_GENERIC,
1111
     "%s: unable to set name.",
1112
     function );
1113
1114
    memory_free(
1115
     data_file_descriptor->name );
1116
1117
    data_file_descriptor->name      = NULL;
1118
    data_file_descriptor->name_size = 0;
1119
1120
    return( -1 );
1121
  }
1122
#endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
1123
  return( 1 );
1124
}
1125
1126
#endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
1127