Coverage Report

Created: 2024-02-25 07:20

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