Coverage Report

Created: 2024-02-25 07:20

/src/libwrc/libuna/libuna_utf32_string.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * UTF-32 string functions
3
 *
4
 * Copyright (C) 2008-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 <types.h>
24
25
#include "libuna_definitions.h"
26
#include "libuna_libcerror.h"
27
#include "libuna_scsu.h"
28
#include "libuna_types.h"
29
#include "libuna_unicode_character.h"
30
#include "libuna_utf32_string.h"
31
32
/* Determines the size of an UTF-32 string from a byte stream
33
 * Returns 1 if successful or -1 on error
34
 */
35
int libuna_utf32_string_size_from_byte_stream(
36
     const uint8_t *byte_stream,
37
     size_t byte_stream_size,
38
     int codepage,
39
     size_t *utf32_string_size,
40
     libcerror_error_t **error )
41
0
{
42
0
  static char *function                        = "libuna_utf32_string_size_from_byte_stream";
43
0
  size_t byte_stream_index                     = 0;
44
0
  libuna_unicode_character_t unicode_character = 0;
45
46
0
  if( byte_stream == NULL )
47
0
  {
48
0
    libcerror_error_set(
49
0
     error,
50
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
51
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
52
0
     "%s: invalid byte stream.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
58
0
  {
59
0
    libcerror_error_set(
60
0
     error,
61
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
62
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
63
0
     "%s: invalid byte stream size value exceeds maximum.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
0
  if( utf32_string_size == NULL )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
73
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
74
0
     "%s: invalid UTF-32 string size.",
75
0
     function );
76
77
0
    return( -1 );
78
0
  }
79
0
  *utf32_string_size = 0;
80
81
0
  if( byte_stream_size == 0 )
82
0
  {
83
0
    return( 1 );
84
0
  }
85
0
  while( byte_stream_index < byte_stream_size )
86
0
  {
87
    /* Convert the byte stream bytes into an Unicode character
88
     */
89
0
    if( libuna_unicode_character_copy_from_byte_stream(
90
0
         &unicode_character,
91
0
         byte_stream,
92
0
         byte_stream_size,
93
0
         &byte_stream_index,
94
0
         codepage,
95
0
         error ) != 1 )
96
0
    {
97
0
      libcerror_error_set(
98
0
       error,
99
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
100
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
101
0
       "%s: unable to copy Unicode character from byte stream.",
102
0
       function );
103
104
0
      return( -1 );
105
0
    }
106
    /* Determine how many UTF-32 character byte double words are required
107
     */
108
0
    if( libuna_unicode_character_size_to_utf32(
109
0
        unicode_character,
110
0
        utf32_string_size,
111
0
         error ) != 1 )
112
0
    {
113
0
      libcerror_error_set(
114
0
       error,
115
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
116
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
117
0
       "%s: unable to unable to determine size of Unicode character in UTF-32.",
118
0
       function );
119
120
0
      return( -1 );
121
0
    }
122
0
    if( unicode_character == 0 )
123
0
    {
124
0
      break;
125
0
    }
126
0
  }
127
  /* Check if the string is terminated with an end-of-string character
128
   */
129
0
  if( unicode_character != 0 )
130
0
  {
131
0
    *utf32_string_size += 1;
132
0
  }
133
0
  return( 1 );
134
0
}
135
136
/* Copies an UTF-32 string from a byte stream
137
 * Returns 1 if successful or -1 on error
138
 */
139
int libuna_utf32_string_copy_from_byte_stream(
140
     libuna_utf32_character_t *utf32_string,
141
     size_t utf32_string_size,
142
     const uint8_t *byte_stream,
143
     size_t byte_stream_size,
144
     int codepage,
145
     libcerror_error_t **error )
146
0
{
147
0
  static char *function     = "libuna_utf32_string_copy_from_byte_stream";
148
0
  size_t utf32_string_index = 0;
149
150
0
  if( libuna_utf32_string_with_index_copy_from_byte_stream(
151
0
       utf32_string,
152
0
       utf32_string_size,
153
0
       &utf32_string_index,
154
0
       byte_stream,
155
0
       byte_stream_size,
156
0
       codepage,
157
0
       error ) != 1 )
158
0
  {
159
0
    libcerror_error_set(
160
0
     error,
161
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
162
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
163
0
     "%s: unable to copy byte stream to UTF-32 string.",
164
0
     function );
165
166
0
    return( -1 );
167
0
  }
168
0
  return( 1 );
169
0
}
170
171
/* Copies an UTF-32 string from a byte stream
172
 * Returns 1 if successful or -1 on error
173
 */
174
int libuna_utf32_string_with_index_copy_from_byte_stream(
175
     libuna_utf32_character_t *utf32_string,
176
     size_t utf32_string_size,
177
     size_t *utf32_string_index,
178
     const uint8_t *byte_stream,
179
     size_t byte_stream_size,
180
     int codepage,
181
     libcerror_error_t **error )
182
0
{
183
0
  static char *function                        = "libuna_utf32_string_with_index_copy_from_byte_stream";
184
0
  size_t byte_stream_index                     = 0;
185
0
  libuna_unicode_character_t unicode_character = 0;
186
187
0
  if( utf32_string == NULL )
188
0
  {
189
0
    libcerror_error_set(
190
0
     error,
191
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
192
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
193
0
     "%s: invalid UTF-32 string.",
194
0
     function );
195
196
0
    return( -1 );
197
0
  }
198
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
199
0
  {
200
0
    libcerror_error_set(
201
0
     error,
202
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
203
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
204
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
205
0
     function );
206
207
0
    return( -1 );
208
0
  }
209
0
  if( utf32_string_index == NULL )
210
0
  {
211
0
    libcerror_error_set(
212
0
     error,
213
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
214
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
215
0
     "%s: invalid UTF-32 string index.",
216
0
     function );
217
218
0
    return( -1 );
219
0
  }
220
0
  if( byte_stream == NULL )
221
0
  {
222
0
    libcerror_error_set(
223
0
     error,
224
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
225
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
226
0
     "%s: invalid byte stream.",
227
0
     function );
228
229
0
    return( -1 );
230
0
  }
231
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
232
0
  {
233
0
    libcerror_error_set(
234
0
     error,
235
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
236
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
237
0
     "%s: invalid byte stream size value exceeds maximum.",
238
0
     function );
239
240
0
    return( -1 );
241
0
  }
242
0
  if( byte_stream_size == 0 )
243
0
  {
244
0
    libcerror_error_set(
245
0
     error,
246
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
247
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
248
0
     "%s: missing byte stream value.",
249
0
     function );
250
251
0
    return( -1 );
252
0
  }
253
0
  while( byte_stream_index < byte_stream_size )
254
0
  {
255
    /* Convert the byte stream bytes into an Unicode character
256
     */
257
0
    if( libuna_unicode_character_copy_from_byte_stream(
258
0
         &unicode_character,
259
0
         byte_stream,
260
0
         byte_stream_size,
261
0
         &byte_stream_index,
262
0
         codepage,
263
0
         error ) != 1 )
264
0
    {
265
0
      libcerror_error_set(
266
0
       error,
267
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
268
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
269
0
       "%s: unable to copy Unicode character from byte stream.",
270
0
       function );
271
272
0
      return( -1 );
273
0
    }
274
    /* Convert the Unicode character into UTF-32 character byte double words
275
     */
276
0
    if( libuna_unicode_character_copy_to_utf32(
277
0
         unicode_character,
278
0
         utf32_string,
279
0
         utf32_string_size,
280
0
         utf32_string_index,
281
0
         error ) != 1 )
282
0
    {
283
0
      libcerror_error_set(
284
0
       error,
285
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
286
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
287
0
       "%s: unable to copy Unicode character to UTF-32.",
288
0
       function );
289
290
0
      return( -1 );
291
0
    }
292
0
    if( unicode_character == 0 )
293
0
    {
294
0
      break;
295
0
    }
296
0
  }
297
  /* Check if the string is terminated with an end-of-string character
298
   */
299
0
  if( unicode_character != 0 )
300
0
  {
301
0
    if( *utf32_string_index >= utf32_string_size )
302
0
    {
303
0
      libcerror_error_set(
304
0
       error,
305
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
306
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
307
0
       "%s: UTF-32 string too small.",
308
0
       function );
309
310
0
      return( -1 );
311
0
    }
312
0
    utf32_string[ *utf32_string_index ] = 0;
313
314
0
    *utf32_string_index += 1;
315
0
  }
316
0
  return( 1 );
317
0
}
318
319
/* Compares an UTF-32 string with a byte stream
320
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
321
 */
322
int libuna_utf32_string_compare_with_byte_stream(
323
     const libuna_utf32_character_t *utf32_string,
324
     size_t utf32_string_size,
325
     const uint8_t *byte_stream,
326
     size_t byte_stream_size,
327
     int codepage,
328
     libcerror_error_t **error )
329
0
{
330
0
  static char *function                                    = "libuna_utf32_string_compare_with_byte_stream";
331
0
  size_t byte_stream_index                                 = 0;
332
0
  size_t utf32_string_index                                = 0;
333
0
  libuna_unicode_character_t utf32_unicode_character       = 0;
334
0
  libuna_unicode_character_t byte_stream_unicode_character = 0;
335
336
0
  if( utf32_string == NULL )
337
0
  {
338
0
    libcerror_error_set(
339
0
     error,
340
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
341
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
342
0
     "%s: invalid UTF-32 string.",
343
0
     function );
344
345
0
    return( -1 );
346
0
  }
347
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
348
0
  {
349
0
    libcerror_error_set(
350
0
     error,
351
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
352
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
353
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
354
0
     function );
355
356
0
    return( -1 );
357
0
  }
358
0
  if( byte_stream == NULL )
359
0
  {
360
0
    libcerror_error_set(
361
0
     error,
362
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
363
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
364
0
     "%s: invalid byte stream.",
365
0
     function );
366
367
0
    return( -1 );
368
0
  }
369
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
370
0
  {
371
0
    libcerror_error_set(
372
0
     error,
373
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
374
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
375
0
     "%s: invalid byte stream size value exceeds maximum.",
376
0
     function );
377
378
0
    return( -1 );
379
0
  }
380
0
  if( byte_stream_size == 0 )
381
0
  {
382
0
    libcerror_error_set(
383
0
     error,
384
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
385
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
386
0
     "%s: missing byte stream value.",
387
0
     function );
388
389
0
    return( -1 );
390
0
  }
391
0
  if( ( utf32_string_size >= 1 )
392
0
   && ( utf32_string[ utf32_string_size - 1 ] == 0 ) )
393
0
  {
394
0
    utf32_string_size -= 1;
395
0
  }
396
  /* Check if the byte stream is terminated with zero bytes
397
   */
398
0
  if( ( byte_stream_size >= 1 )
399
0
   && ( byte_stream[ byte_stream_size - 1 ] == 0 ) )
400
0
  {
401
0
    byte_stream_size -= 1;
402
0
  }
403
0
  while( ( utf32_string_index < utf32_string_size )
404
0
      && ( byte_stream_index < byte_stream_size ) )
405
0
  {
406
    /* Convert the UTF-32 character bytes into an Unicode character
407
     */
408
0
    if( libuna_unicode_character_copy_from_utf32(
409
0
         &utf32_unicode_character,
410
0
         utf32_string,
411
0
         utf32_string_size,
412
0
         &utf32_string_index,
413
0
         error ) != 1 )
414
0
    {
415
0
      libcerror_error_set(
416
0
       error,
417
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
418
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
419
0
       "%s: unable to copy Unicode character from UTF-32.",
420
0
       function );
421
422
0
      return( -1 );
423
0
    }
424
    /* Convert the byte stream bytes into an Unicode character
425
     */
426
0
    if( libuna_unicode_character_copy_from_byte_stream(
427
0
         &byte_stream_unicode_character,
428
0
         byte_stream,
429
0
         byte_stream_size,
430
0
         &byte_stream_index,
431
0
         codepage,
432
0
         error ) != 1 )
433
0
    {
434
0
      libcerror_error_set(
435
0
       error,
436
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
437
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
438
0
       "%s: unable to copy Unicode character from byte stream.",
439
0
       function );
440
441
0
      return( -1 );
442
0
    }
443
0
    if( utf32_unicode_character < byte_stream_unicode_character )
444
0
    {
445
0
      return( LIBUNA_COMPARE_LESS );
446
0
    }
447
0
    else if( utf32_unicode_character > byte_stream_unicode_character )
448
0
    {
449
0
      return( LIBUNA_COMPARE_GREATER );
450
0
    }
451
0
  }
452
  /* Check if both strings were entirely processed
453
   */
454
0
  if( utf32_string_index < utf32_string_size )
455
0
  {
456
0
    return( LIBUNA_COMPARE_GREATER );
457
0
  }
458
0
  else if( byte_stream_index < byte_stream_size )
459
0
  {
460
0
    return( LIBUNA_COMPARE_LESS );
461
0
  }
462
0
  return( LIBUNA_COMPARE_EQUAL );
463
0
}
464
465
/* Determines the size of an UTF-32 string from an UTF-7 stream
466
 * Returns 1 if successful or -1 on error
467
 */
468
int libuna_utf32_string_size_from_utf7_stream(
469
     const uint8_t *utf7_stream,
470
     size_t utf7_stream_size,
471
     size_t *utf32_string_size,
472
     libcerror_error_t **error )
473
0
{
474
0
  static char *function                        = "libuna_utf32_string_size_from_utf7_stream";
475
0
  size_t utf7_stream_index                     = 0;
476
0
  libuna_unicode_character_t unicode_character = 0;
477
0
  uint32_t utf7_stream_base64_data             = 0;
478
479
0
  if( utf7_stream == NULL )
480
0
  {
481
0
    libcerror_error_set(
482
0
     error,
483
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
484
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
485
0
     "%s: invalid UTF-7 stream.",
486
0
     function );
487
488
0
    return( -1 );
489
0
  }
490
0
  if( utf7_stream_size > (size_t) SSIZE_MAX )
491
0
  {
492
0
    libcerror_error_set(
493
0
     error,
494
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
495
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
496
0
     "%s: invalid UTF-7 stream size value exceeds maximum.",
497
0
     function );
498
499
0
    return( -1 );
500
0
  }
501
0
  if( utf32_string_size == NULL )
502
0
  {
503
0
    libcerror_error_set(
504
0
     error,
505
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
506
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
507
0
     "%s: invalid UTF-32 string size.",
508
0
     function );
509
510
0
    return( -1 );
511
0
  }
512
0
  *utf32_string_size = 0;
513
514
0
  if( utf7_stream_size == 0 )
515
0
  {
516
0
    return( 1 );
517
0
  }
518
0
  while( utf7_stream_index < utf7_stream_size )
519
0
  {
520
    /* Convert the UTF-7 stream bytes into an Unicode character
521
     */
522
0
    if( libuna_unicode_character_copy_from_utf7_stream(
523
0
         &unicode_character,
524
0
         utf7_stream,
525
0
         utf7_stream_size,
526
0
         &utf7_stream_index,
527
0
         &utf7_stream_base64_data,
528
0
         error ) != 1 )
529
0
    {
530
0
      libcerror_error_set(
531
0
       error,
532
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
533
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
534
0
       "%s: unable to copy Unicode character from UTF-7 stream.",
535
0
       function );
536
537
0
      return( -1 );
538
0
    }
539
    /* Determine how many UTF-32 character bytes are required
540
     */
541
0
    if( libuna_unicode_character_size_to_utf32(
542
0
         unicode_character,
543
0
         utf32_string_size,
544
0
         error ) != 1 )
545
0
    {
546
0
      libcerror_error_set(
547
0
       error,
548
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
549
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
550
0
       "%s: unable to unable to determine size of Unicode character in UTF-32.",
551
0
       function );
552
553
0
      return( -1 );
554
0
    }
555
0
    if( unicode_character == 0 )
556
0
    {
557
0
      break;
558
0
    }
559
0
  }
560
  /* Check if the string is terminated with an end-of-string character
561
   */
562
0
  if( unicode_character != 0 )
563
0
  {
564
0
    *utf32_string_size += 1;
565
0
  }
566
0
  return( 1 );
567
0
}
568
569
/* Copies an UTF-32 string from an UTF-7 stream
570
 * Returns 1 if successful or -1 on error
571
 */
572
int libuna_utf32_string_copy_from_utf7_stream(
573
     libuna_utf32_character_t *utf32_string,
574
     size_t utf32_string_size,
575
     const uint8_t *utf7_stream,
576
     size_t utf7_stream_size,
577
     libcerror_error_t **error )
578
0
{
579
0
  static char *function     = "libuna_utf32_string_copy_from_utf7_stream";
580
0
  size_t utf32_string_index = 0;
581
582
0
  if( libuna_utf32_string_with_index_copy_from_utf7_stream(
583
0
       utf32_string,
584
0
       utf32_string_size,
585
0
       &utf32_string_index,
586
0
       utf7_stream,
587
0
       utf7_stream_size,
588
0
       error ) != 1 )
589
0
  {
590
0
    libcerror_error_set(
591
0
     error,
592
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
593
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
594
0
     "%s: unable to UTF-7 stream to UTF-32 string.",
595
0
     function );
596
597
0
    return( -1 );
598
0
  }
599
0
  return( 1 );
600
0
}
601
602
/* Copies an UTF-32 string from an UTF-7 stream
603
 * Returns 1 if successful or -1 on error
604
 */
605
int libuna_utf32_string_with_index_copy_from_utf7_stream(
606
     libuna_utf32_character_t *utf32_string,
607
     size_t utf32_string_size,
608
     size_t *utf32_string_index,
609
     const uint8_t *utf7_stream,
610
     size_t utf7_stream_size,
611
     libcerror_error_t **error )
612
0
{
613
0
  static char *function                        = "libuna_utf32_string_with_index_copy_from_utf7_stream";
614
0
  size_t utf7_stream_index                     = 0;
615
0
  libuna_unicode_character_t unicode_character = 0;
616
0
  uint32_t utf7_stream_base64_data             = 0;
617
618
0
  if( utf32_string == NULL )
619
0
  {
620
0
    libcerror_error_set(
621
0
     error,
622
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
623
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
624
0
     "%s: invalid UTF-32 string.",
625
0
     function );
626
627
0
    return( -1 );
628
0
  }
629
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
630
0
  {
631
0
    libcerror_error_set(
632
0
     error,
633
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
634
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
635
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
636
0
     function );
637
638
0
    return( -1 );
639
0
  }
640
0
  if( utf32_string_index == NULL )
641
0
  {
642
0
    libcerror_error_set(
643
0
     error,
644
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
645
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
646
0
     "%s: invalid UTF-32 string index.",
647
0
     function );
648
649
0
    return( -1 );
650
0
  }
651
0
  if( utf7_stream == NULL )
652
0
  {
653
0
    libcerror_error_set(
654
0
     error,
655
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
656
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
657
0
     "%s: invalid UTF-7 stream.",
658
0
     function );
659
660
0
    return( -1 );
661
0
  }
662
0
  if( utf7_stream_size > (size_t) SSIZE_MAX )
663
0
  {
664
0
    libcerror_error_set(
665
0
     error,
666
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
667
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
668
0
     "%s: invalid UTF-7 stream size value exceeds maximum.",
669
0
     function );
670
671
0
    return( -1 );
672
0
  }
673
0
  if( utf7_stream_size == 0 )
674
0
  {
675
0
    libcerror_error_set(
676
0
     error,
677
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
678
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
679
0
     "%s: missing UTF-7 stream value.",
680
0
     function );
681
682
0
    return( -1 );
683
0
  }
684
0
  while( utf7_stream_index < utf7_stream_size )
685
0
  {
686
    /* Convert the UTF-7 stream bytes into an Unicode character
687
     */
688
0
    if( libuna_unicode_character_copy_from_utf7_stream(
689
0
         &unicode_character,
690
0
         utf7_stream,
691
0
         utf7_stream_size,
692
0
         &utf7_stream_index,
693
0
         &utf7_stream_base64_data,
694
0
         error ) != 1 )
695
0
    {
696
0
      libcerror_error_set(
697
0
       error,
698
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
699
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
700
0
       "%s: unable to copy Unicode character from UTF-7 stream.",
701
0
       function );
702
703
0
      return( -1 );
704
0
    }
705
    /* Convert the Unicode character into UTF-32 character bytes
706
     */
707
0
    if( libuna_unicode_character_copy_to_utf32(
708
0
         unicode_character,
709
0
         utf32_string,
710
0
         utf32_string_size,
711
0
         utf32_string_index,
712
0
         error ) != 1 )
713
0
    {
714
0
      libcerror_error_set(
715
0
       error,
716
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
717
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
718
0
       "%s: unable to copy Unicode character to UTF-32.",
719
0
       function );
720
721
0
      return( -1 );
722
0
    }
723
0
    if( unicode_character == 0 )
724
0
    {
725
0
      break;
726
0
    }
727
0
  }
728
  /* Check if the string is terminated with an end-of-string character
729
   */
730
0
  if( unicode_character != 0 )
731
0
  {
732
0
    if( *utf32_string_index >= utf32_string_size )
733
0
    {
734
0
      libcerror_error_set(
735
0
       error,
736
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
737
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
738
0
       "%s: UTF-32 string too small.",
739
0
       function );
740
741
0
      return( -1 );
742
0
    }
743
0
    utf32_string[ *utf32_string_index ] = 0;
744
745
0
    *utf32_string_index += 1;
746
0
  }
747
0
  return( 1 );
748
0
}
749
750
/* Compares an UTF-32 string with an UTF-7 stream
751
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
752
 */
753
int libuna_utf32_string_compare_with_utf7_stream(
754
     const libuna_utf32_character_t *utf32_string,
755
     size_t utf32_string_size,
756
     const uint8_t *utf7_stream,
757
     size_t utf7_stream_size,
758
     libcerror_error_t **error )
759
0
{
760
0
  static char *function                                    = "libuna_utf32_string_compare_with_utf7_stream";
761
0
  size_t utf32_string_index                                = 0;
762
0
  size_t utf7_stream_index                                 = 0;
763
0
  libuna_unicode_character_t utf32_unicode_character       = 0;
764
0
  libuna_unicode_character_t utf7_stream_unicode_character = 0;
765
0
  uint32_t utf7_stream_base64_data                         = 0;
766
767
0
  if( utf32_string == NULL )
768
0
  {
769
0
    libcerror_error_set(
770
0
     error,
771
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
772
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
773
0
     "%s: invalid UTF-32 string.",
774
0
     function );
775
776
0
    return( -1 );
777
0
  }
778
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
779
0
  {
780
0
    libcerror_error_set(
781
0
     error,
782
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
783
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
784
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
785
0
     function );
786
787
0
    return( -1 );
788
0
  }
789
0
  if( utf7_stream == NULL )
790
0
  {
791
0
    libcerror_error_set(
792
0
     error,
793
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
794
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
795
0
     "%s: invalid UTF-7 stream.",
796
0
     function );
797
798
0
    return( -1 );
799
0
  }
800
0
  if( utf7_stream_size > (size_t) SSIZE_MAX )
801
0
  {
802
0
    libcerror_error_set(
803
0
     error,
804
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
805
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
806
0
     "%s: invalid UTF-7 stream size value exceeds maximum.",
807
0
     function );
808
809
0
    return( -1 );
810
0
  }
811
0
  if( utf7_stream_size == 0 )
812
0
  {
813
0
    libcerror_error_set(
814
0
     error,
815
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
816
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
817
0
     "%s: missing UTF-7 stream value.",
818
0
     function );
819
820
0
    return( -1 );
821
0
  }
822
0
  if( ( utf32_string_size >= 1 )
823
0
   && ( utf32_string[ utf32_string_size - 1 ] == 0 ) )
824
0
  {
825
0
    utf32_string_size -= 1;
826
0
  }
827
  /* Check if the UTF-7 stream is terminated with zero bytes
828
   */
829
0
  if( ( utf7_stream_size >= 1 )
830
0
   && ( utf7_stream[ utf7_stream_size - 1 ] == 0 ) )
831
0
  {
832
0
    utf7_stream_size -= 1;
833
0
  }
834
0
  while( ( utf32_string_index < utf32_string_size )
835
0
      && ( utf7_stream_index < utf7_stream_size ) )
836
0
  {
837
    /* Convert the UTF-32 character bytes into an Unicode character
838
     */
839
0
    if( libuna_unicode_character_copy_from_utf32(
840
0
         &utf32_unicode_character,
841
0
         utf32_string,
842
0
         utf32_string_size,
843
0
         &utf32_string_index,
844
0
         error ) != 1 )
845
0
    {
846
0
      libcerror_error_set(
847
0
       error,
848
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
849
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
850
0
       "%s: unable to copy Unicode character from UTF-32.",
851
0
       function );
852
853
0
      return( -1 );
854
0
    }
855
    /* Convert the UTF-7 character bytes into an Unicode character
856
     */
857
0
    if( libuna_unicode_character_copy_from_utf7_stream(
858
0
         &utf7_stream_unicode_character,
859
0
         utf7_stream,
860
0
         utf7_stream_size,
861
0
         &utf7_stream_index,
862
0
         &utf7_stream_base64_data,
863
0
                     error ) != 1 )
864
0
    {
865
0
      libcerror_error_set(
866
0
       error,
867
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
868
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
869
0
       "%s: unable to copy Unicode character from UTF-7 stream.",
870
0
       function );
871
872
0
      return( -1 );
873
0
    }
874
0
    if( utf32_unicode_character < utf7_stream_unicode_character )
875
0
    {
876
0
      return( LIBUNA_COMPARE_LESS );
877
0
    }
878
0
    else if( utf32_unicode_character > utf7_stream_unicode_character )
879
0
    {
880
0
      return( LIBUNA_COMPARE_GREATER );
881
0
    }
882
0
  }
883
  /* Check if both strings were entirely processed
884
   */
885
0
  if( utf32_string_index < utf32_string_size )
886
0
  {
887
0
    return( LIBUNA_COMPARE_GREATER );
888
0
  }
889
0
  else if( utf7_stream_index < utf7_stream_size )
890
0
  {
891
0
    return( LIBUNA_COMPARE_LESS );
892
0
  }
893
0
  return( LIBUNA_COMPARE_EQUAL );
894
0
}
895
896
/* Determines the size of an UTF-32 string from an UTF-8 string
897
 * Returns 1 if successful or -1 on error
898
 */
899
int libuna_utf32_string_size_from_utf8(
900
     const libuna_utf8_character_t *utf8_string,
901
     size_t utf8_string_size,
902
     size_t *utf32_string_size,
903
     libcerror_error_t **error )
904
0
{
905
0
  static char *function                        = "libuna_utf32_string_size_from_utf8";
906
0
  size_t utf8_string_index                     = 0;
907
0
  libuna_unicode_character_t unicode_character = 0;
908
909
0
  if( utf8_string == NULL )
910
0
  {
911
0
    libcerror_error_set(
912
0
     error,
913
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
914
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
915
0
     "%s: invalid UTF-8 string.",
916
0
     function );
917
918
0
    return( -1 );
919
0
  }
920
0
  if( utf8_string_size > (size_t) SSIZE_MAX )
921
0
  {
922
0
    libcerror_error_set(
923
0
     error,
924
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
925
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
926
0
     "%s: invalid UTF-8 string size value exceeds maximum.",
927
0
     function );
928
929
0
    return( -1 );
930
0
  }
931
0
  if( utf32_string_size == NULL )
932
0
  {
933
0
    libcerror_error_set(
934
0
     error,
935
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
936
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
937
0
     "%s: invalid UTF-32 string size.",
938
0
     function );
939
940
0
    return( -1 );
941
0
  }
942
0
  *utf32_string_size = 0;
943
944
0
  if( utf8_string_size == 0 )
945
0
  {
946
0
    return( 1 );
947
0
  }
948
0
  while( utf8_string_index < utf8_string_size )
949
0
  {
950
    /* Convert the UTF-8 character bytes into an Unicode character
951
     */
952
0
    if( libuna_unicode_character_copy_from_utf8(
953
0
         &unicode_character,
954
0
         utf8_string,
955
0
         utf8_string_size,
956
0
         &utf8_string_index,
957
0
         error ) != 1 )
958
0
    {
959
0
      libcerror_error_set(
960
0
       error,
961
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
962
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
963
0
       "%s: unable to copy Unicode character from UTF-8.",
964
0
       function );
965
966
0
      return( -1 );
967
0
    }
968
    /* Determine how many UTF-32 character bytes are required
969
     */
970
0
    if( libuna_unicode_character_size_to_utf32(
971
0
         unicode_character,
972
0
         utf32_string_size,
973
0
         error ) != 1 )
974
0
    {
975
0
      libcerror_error_set(
976
0
       error,
977
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
978
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
979
0
       "%s: unable to unable to determine size of Unicode character in UTF-32.",
980
0
       function );
981
982
0
      return( -1 );
983
0
    }
984
0
    if( unicode_character == 0 )
985
0
    {
986
0
      break;
987
0
    }
988
0
  }
989
  /* Check if the string is terminated with an end-of-string character
990
   */
991
0
  if( unicode_character != 0 )
992
0
  {
993
0
    *utf32_string_size += 1;
994
0
  }
995
0
  return( 1 );
996
0
}
997
998
/* Copies an UTF-32 string from an UTF-8 string
999
 * Returns 1 if successful or -1 on error
1000
 */
1001
int libuna_utf32_string_copy_from_utf8(
1002
     libuna_utf32_character_t *utf32_string,
1003
     size_t utf32_string_size,
1004
     const libuna_utf8_character_t *utf8_string,
1005
     size_t utf8_string_size,
1006
     libcerror_error_t **error )
1007
0
{
1008
0
  static char *function     = "libuna_utf32_string_copy_from_utf8";
1009
0
  size_t utf32_string_index = 0;
1010
1011
0
  if( libuna_utf32_string_with_index_copy_from_utf8(
1012
0
       utf32_string,
1013
0
       utf32_string_size,
1014
0
       &utf32_string_index,
1015
0
       utf8_string,
1016
0
       utf8_string_size,
1017
0
       error ) != 1 )
1018
0
  {
1019
0
    libcerror_error_set(
1020
0
     error,
1021
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1022
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
1023
0
     "%s: unable to copy UTF-8 string to UTF-32 string.",
1024
0
     function );
1025
1026
0
    return( -1 );
1027
0
  }
1028
0
  return( 1 );
1029
0
}
1030
1031
/* Copies an UTF-32 string from an UTF-8 string
1032
 * Returns 1 if successful or -1 on error
1033
 */
1034
int libuna_utf32_string_with_index_copy_from_utf8(
1035
     libuna_utf32_character_t *utf32_string,
1036
     size_t utf32_string_size,
1037
     size_t *utf32_string_index,
1038
     const libuna_utf8_character_t *utf8_string,
1039
     size_t utf8_string_size,
1040
     libcerror_error_t **error )
1041
0
{
1042
0
  static char *function                        = "libuna_utf32_string_with_index_copy_from_utf8";
1043
0
  size_t utf8_string_index                     = 0;
1044
0
  libuna_unicode_character_t unicode_character = 0;
1045
1046
0
  if( utf32_string == NULL )
1047
0
  {
1048
0
    libcerror_error_set(
1049
0
     error,
1050
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1051
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1052
0
     "%s: invalid UTF-32 string.",
1053
0
     function );
1054
1055
0
    return( -1 );
1056
0
  }
1057
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
1058
0
  {
1059
0
    libcerror_error_set(
1060
0
     error,
1061
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1062
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1063
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
1064
0
     function );
1065
1066
0
    return( -1 );
1067
0
  }
1068
0
  if( utf32_string_index == NULL )
1069
0
  {
1070
0
    libcerror_error_set(
1071
0
     error,
1072
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1073
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1074
0
     "%s: invalid UTF-32 string index.",
1075
0
     function );
1076
1077
0
    return( -1 );
1078
0
  }
1079
0
  if( utf8_string == NULL )
1080
0
  {
1081
0
    libcerror_error_set(
1082
0
     error,
1083
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1084
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1085
0
     "%s: invalid UTF-8 string.",
1086
0
     function );
1087
1088
0
    return( -1 );
1089
0
  }
1090
0
  if( utf8_string_size > (size_t) SSIZE_MAX )
1091
0
  {
1092
0
    libcerror_error_set(
1093
0
     error,
1094
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1095
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1096
0
     "%s: invalid UTF-8 string size value exceeds maximum.",
1097
0
     function );
1098
1099
0
    return( -1 );
1100
0
  }
1101
0
  if( utf8_string_size == 0 )
1102
0
  {
1103
0
    libcerror_error_set(
1104
0
     error,
1105
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1106
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
1107
0
     "%s: missing UTF-8 string value.",
1108
0
     function );
1109
1110
0
    return( -1 );
1111
0
  }
1112
0
  while( utf8_string_index < utf8_string_size )
1113
0
  {
1114
    /* Convert the UTF-8 character bytes into an Unicode character
1115
     */
1116
0
    if( libuna_unicode_character_copy_from_utf8(
1117
0
         &unicode_character,
1118
0
         utf8_string,
1119
0
         utf8_string_size,
1120
0
         &utf8_string_index,
1121
0
         error ) != 1 )
1122
0
    {
1123
0
      libcerror_error_set(
1124
0
       error,
1125
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
1126
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
1127
0
       "%s: unable to copy Unicode character from UTF-8.",
1128
0
       function );
1129
1130
0
      return( -1 );
1131
0
    }
1132
    /* Convert the Unicode character into UTF-32 character bytes
1133
     */
1134
0
    if( libuna_unicode_character_copy_to_utf32(
1135
0
         unicode_character,
1136
0
         utf32_string,
1137
0
         utf32_string_size,
1138
0
         utf32_string_index,
1139
0
         error ) != 1 )
1140
0
    {
1141
0
      libcerror_error_set(
1142
0
       error,
1143
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
1144
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
1145
0
       "%s: unable to copy Unicode character to UTF-32.",
1146
0
       function );
1147
1148
0
      return( -1 );
1149
0
    }
1150
0
    if( unicode_character == 0 )
1151
0
    {
1152
0
      break;
1153
0
    }
1154
0
  }
1155
  /* Check if the string is terminated with an end-of-string character
1156
   */
1157
0
  if( unicode_character != 0 )
1158
0
  {
1159
0
    if( *utf32_string_index >= utf32_string_size )
1160
0
    {
1161
0
      libcerror_error_set(
1162
0
       error,
1163
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1164
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1165
0
       "%s: UTF-32 string too small.",
1166
0
       function );
1167
1168
0
      return( -1 );
1169
0
    }
1170
0
    utf32_string[ *utf32_string_index ] = 0;
1171
1172
0
    *utf32_string_index += 1;
1173
0
  }
1174
0
  return( 1 );
1175
0
}
1176
1177
/* Determines the size of an UTF-32 string from an UTF-8 stream
1178
 * Returns 1 if successful or -1 on error
1179
 */
1180
int libuna_utf32_string_size_from_utf8_stream(
1181
     const uint8_t *utf8_stream,
1182
     size_t utf8_stream_size,
1183
     size_t *utf32_string_size,
1184
     libcerror_error_t **error )
1185
0
{
1186
0
  static char *function                        = "libuna_utf32_string_size_from_utf8_stream";
1187
0
  size_t utf8_stream_index                     = 0;
1188
0
  libuna_unicode_character_t unicode_character = 0;
1189
1190
0
  if( utf8_stream == NULL )
1191
0
  {
1192
0
    libcerror_error_set(
1193
0
     error,
1194
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1195
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1196
0
     "%s: invalid UTF-8 stream.",
1197
0
     function );
1198
1199
0
    return( -1 );
1200
0
  }
1201
0
  if( utf8_stream_size > (size_t) SSIZE_MAX )
1202
0
  {
1203
0
    libcerror_error_set(
1204
0
     error,
1205
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1206
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1207
0
     "%s: invalid UTF-8 stream size value exceeds maximum.",
1208
0
     function );
1209
1210
0
    return( -1 );
1211
0
  }
1212
0
  if( utf32_string_size == NULL )
1213
0
  {
1214
0
    libcerror_error_set(
1215
0
     error,
1216
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1217
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1218
0
     "%s: invalid UTF-32 string size.",
1219
0
     function );
1220
1221
0
    return( -1 );
1222
0
  }
1223
0
  *utf32_string_size = 0;
1224
1225
0
  if( utf8_stream_size == 0 )
1226
0
  {
1227
0
    return( 1 );
1228
0
  }
1229
  /* Check if UTF-8 stream starts with a byte order mark (BOM)
1230
   */
1231
0
  if( utf8_stream_size >= 3 )
1232
0
  {
1233
0
    if( ( utf8_stream[ 0 ] == 0x0ef )
1234
0
     && ( utf8_stream[ 1 ] == 0x0bb )
1235
0
     && ( utf8_stream[ 2 ] == 0x0bf ) )
1236
0
    {
1237
0
      utf8_stream_index += 3;
1238
0
    }
1239
0
  }
1240
0
  while( utf8_stream_index < utf8_stream_size )
1241
0
  {
1242
    /* Convert the UTF-8 stream bytes into an Unicode character
1243
     */
1244
0
    if( libuna_unicode_character_copy_from_utf8(
1245
0
         &unicode_character,
1246
0
         utf8_stream,
1247
0
         utf8_stream_size,
1248
0
         &utf8_stream_index,
1249
0
         error ) != 1 )
1250
0
    {
1251
0
      libcerror_error_set(
1252
0
       error,
1253
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
1254
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
1255
0
       "%s: unable to copy Unicode character from UTF-8 stream.",
1256
0
       function );
1257
1258
0
      return( -1 );
1259
0
    }
1260
    /* Determine how many UTF-32 character bytes are required
1261
     */
1262
0
    if( libuna_unicode_character_size_to_utf32(
1263
0
        unicode_character,
1264
0
        utf32_string_size,
1265
0
         error ) != 1 )
1266
0
    {
1267
0
      libcerror_error_set(
1268
0
       error,
1269
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
1270
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
1271
0
       "%s: unable to unable to determine size of Unicode character in UTF-32.",
1272
0
       function );
1273
1274
0
      return( -1 );
1275
0
    }
1276
0
    if( unicode_character == 0 )
1277
0
    {
1278
0
      break;
1279
0
    }
1280
0
  }
1281
  /* Check if the string is terminated with an end-of-string character
1282
   */
1283
0
  if( unicode_character != 0 )
1284
0
  {
1285
0
    *utf32_string_size += 1;
1286
0
  }
1287
0
  return( 1 );
1288
0
}
1289
1290
/* Copies an UTF-32 string from an UTF-8 stream
1291
 * Returns 1 if successful or -1 on error
1292
 */
1293
int libuna_utf32_string_copy_from_utf8_stream(
1294
     libuna_utf32_character_t *utf32_string,
1295
     size_t utf32_string_size,
1296
     const uint8_t *utf8_stream,
1297
     size_t utf8_stream_size,
1298
     libcerror_error_t **error )
1299
0
{
1300
0
  static char *function     = "libuna_utf32_string_copy_from_utf8_stream";
1301
0
  size_t utf32_string_index = 0;
1302
1303
0
  if( libuna_utf32_string_with_index_copy_from_utf8_stream(
1304
0
       utf32_string,
1305
0
       utf32_string_size,
1306
0
       &utf32_string_index,
1307
0
       utf8_stream,
1308
0
       utf8_stream_size,
1309
0
       error ) != 1 )
1310
0
  {
1311
0
    libcerror_error_set(
1312
0
     error,
1313
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1314
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
1315
0
     "%s: unable to UTF-8 stream to UTF-32 string.",
1316
0
     function );
1317
1318
0
    return( -1 );
1319
0
  }
1320
0
  return( 1 );
1321
0
}
1322
1323
/* Copies an UTF-32 string from an UTF-8 stream
1324
 * Returns 1 if successful or -1 on error
1325
 */
1326
int libuna_utf32_string_with_index_copy_from_utf8_stream(
1327
     libuna_utf32_character_t *utf32_string,
1328
     size_t utf32_string_size,
1329
     size_t *utf32_string_index,
1330
     const uint8_t *utf8_stream,
1331
     size_t utf8_stream_size,
1332
     libcerror_error_t **error )
1333
0
{
1334
0
  static char *function                        = "libuna_utf32_string_with_index_copy_from_utf8_stream";
1335
0
  size_t utf8_stream_index                     = 0;
1336
0
  libuna_unicode_character_t unicode_character = 0;
1337
1338
0
  if( utf32_string == NULL )
1339
0
  {
1340
0
    libcerror_error_set(
1341
0
     error,
1342
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1343
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1344
0
     "%s: invalid UTF-32 string.",
1345
0
     function );
1346
1347
0
    return( -1 );
1348
0
  }
1349
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
1350
0
  {
1351
0
    libcerror_error_set(
1352
0
     error,
1353
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1354
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1355
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
1356
0
     function );
1357
1358
0
    return( -1 );
1359
0
  }
1360
0
  if( utf32_string_index == NULL )
1361
0
  {
1362
0
    libcerror_error_set(
1363
0
     error,
1364
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1365
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1366
0
     "%s: invalid UTF-32 string index.",
1367
0
     function );
1368
1369
0
    return( -1 );
1370
0
  }
1371
0
  if( utf8_stream == NULL )
1372
0
  {
1373
0
    libcerror_error_set(
1374
0
     error,
1375
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1376
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1377
0
     "%s: invalid UTF-8 stream.",
1378
0
     function );
1379
1380
0
    return( -1 );
1381
0
  }
1382
0
  if( utf8_stream_size > (size_t) SSIZE_MAX )
1383
0
  {
1384
0
    libcerror_error_set(
1385
0
     error,
1386
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1387
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1388
0
     "%s: invalid UTF-8 stream size value exceeds maximum.",
1389
0
     function );
1390
1391
0
    return( -1 );
1392
0
  }
1393
0
  if( utf8_stream_size == 0 )
1394
0
  {
1395
0
    libcerror_error_set(
1396
0
     error,
1397
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1398
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
1399
0
     "%s: missing UTF-8 stream value.",
1400
0
     function );
1401
1402
0
    return( -1 );
1403
0
  }
1404
  /* Check if UTF-8 stream starts with a byte order mark (BOM)
1405
   */
1406
0
  if( utf8_stream_size >= 3 )
1407
0
  {
1408
0
    if( ( utf8_stream[ 0 ] == 0x0ef )
1409
0
     && ( utf8_stream[ 1 ] == 0x0bb )
1410
0
     && ( utf8_stream[ 2 ] == 0x0bf ) )
1411
0
    {
1412
0
      utf8_stream_index += 3;
1413
0
    }
1414
0
  }
1415
0
  while( utf8_stream_index < utf8_stream_size )
1416
0
  {
1417
    /* Convert the UTF-8 stream bytes into an Unicode character
1418
     */
1419
0
    if( libuna_unicode_character_copy_from_utf8(
1420
0
         &unicode_character,
1421
0
         utf8_stream,
1422
0
         utf8_stream_size,
1423
0
         &utf8_stream_index,
1424
0
         error ) != 1 )
1425
0
    {
1426
0
      libcerror_error_set(
1427
0
       error,
1428
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
1429
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
1430
0
       "%s: unable to copy Unicode character from UTF-8 stream.",
1431
0
       function );
1432
1433
0
      return( -1 );
1434
0
    }
1435
    /* Convert the Unicode character into UTF-32 character bytes
1436
     */
1437
0
    if( libuna_unicode_character_copy_to_utf32(
1438
0
         unicode_character,
1439
0
         utf32_string,
1440
0
         utf32_string_size,
1441
0
         utf32_string_index,
1442
0
         error ) != 1 )
1443
0
    {
1444
0
      libcerror_error_set(
1445
0
       error,
1446
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
1447
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
1448
0
       "%s: unable to copy Unicode character to UTF-32.",
1449
0
       function );
1450
1451
0
      return( -1 );
1452
0
    }
1453
0
    if( unicode_character == 0 )
1454
0
    {
1455
0
      break;
1456
0
    }
1457
0
  }
1458
  /* Check if the string is terminated with an end-of-string character
1459
   */
1460
0
  if( unicode_character != 0 )
1461
0
  {
1462
0
    if( *utf32_string_index >= utf32_string_size )
1463
0
    {
1464
0
      libcerror_error_set(
1465
0
       error,
1466
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1467
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1468
0
       "%s: UTF-32 string too small.",
1469
0
       function );
1470
1471
0
      return( -1 );
1472
0
    }
1473
0
    utf32_string[ *utf32_string_index ] = 0;
1474
1475
0
    *utf32_string_index += 1;
1476
0
  }
1477
0
  return( 1 );
1478
0
}
1479
1480
/* Compares an UTF-32 string with an UTF-8 stream
1481
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
1482
 */
1483
int libuna_utf32_string_compare_with_utf8_stream(
1484
     const libuna_utf32_character_t *utf32_string,
1485
     size_t utf32_string_size,
1486
     const uint8_t *utf8_stream,
1487
     size_t utf8_stream_size,
1488
     libcerror_error_t **error )
1489
0
{
1490
0
  static char *function                                    = "libuna_utf32_string_compare_with_utf8_stream";
1491
0
  size_t utf32_string_index                                = 0;
1492
0
  size_t utf8_stream_index                                 = 0;
1493
0
  libuna_unicode_character_t utf32_unicode_character       = 0;
1494
0
  libuna_unicode_character_t utf8_stream_unicode_character = 0;
1495
1496
0
  if( utf32_string == NULL )
1497
0
  {
1498
0
    libcerror_error_set(
1499
0
     error,
1500
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1501
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1502
0
     "%s: invalid UTF-32 string.",
1503
0
     function );
1504
1505
0
    return( -1 );
1506
0
  }
1507
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
1508
0
  {
1509
0
    libcerror_error_set(
1510
0
     error,
1511
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1512
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1513
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
1514
0
     function );
1515
1516
0
    return( -1 );
1517
0
  }
1518
0
  if( utf8_stream == NULL )
1519
0
  {
1520
0
    libcerror_error_set(
1521
0
     error,
1522
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1523
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1524
0
     "%s: invalid UTF-8 stream.",
1525
0
     function );
1526
1527
0
    return( -1 );
1528
0
  }
1529
0
  if( utf8_stream_size > (size_t) SSIZE_MAX )
1530
0
  {
1531
0
    libcerror_error_set(
1532
0
     error,
1533
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1534
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1535
0
     "%s: invalid UTF-8 stream size value exceeds maximum.",
1536
0
     function );
1537
1538
0
    return( -1 );
1539
0
  }
1540
0
  if( utf8_stream_size == 0 )
1541
0
  {
1542
0
    libcerror_error_set(
1543
0
     error,
1544
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1545
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
1546
0
     "%s: missing UTF-8 stream value.",
1547
0
     function );
1548
1549
0
    return( -1 );
1550
0
  }
1551
  /* Check if UTF-8 stream starts with a byte order mark (BOM)
1552
   */
1553
0
  if( utf8_stream_size >= 3 )
1554
0
  {
1555
0
    if( ( utf8_stream[ 0 ] == 0x0ef )
1556
0
     && ( utf8_stream[ 1 ] == 0x0bb )
1557
0
     && ( utf8_stream[ 2 ] == 0x0bf ) )
1558
0
    {
1559
0
      utf8_stream_index += 3;
1560
0
    }
1561
0
  }
1562
0
  if( ( utf32_string_size >= 1 )
1563
0
   && ( utf32_string[ utf32_string_size - 1 ] == 0 ) )
1564
0
  {
1565
0
    utf32_string_size -= 1;
1566
0
  }
1567
  /* Check if the UTF-8 stream is terminated with zero bytes
1568
   */
1569
0
  if( ( utf8_stream_size >= 1 )
1570
0
   && ( utf8_stream[ utf8_stream_size - 1 ] == 0 ) )
1571
0
  {
1572
0
    utf8_stream_size -= 1;
1573
0
  }
1574
0
  while( ( utf32_string_index < utf32_string_size )
1575
0
      && ( utf8_stream_index < utf8_stream_size ) )
1576
0
  {
1577
    /* Convert the UTF-32 character bytes into an Unicode character
1578
     */
1579
0
    if( libuna_unicode_character_copy_from_utf32(
1580
0
         &utf32_unicode_character,
1581
0
         utf32_string,
1582
0
         utf32_string_size,
1583
0
         &utf32_string_index,
1584
0
         error ) != 1 )
1585
0
    {
1586
0
      libcerror_error_set(
1587
0
       error,
1588
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
1589
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
1590
0
       "%s: unable to copy Unicode character from UTF-32.",
1591
0
       function );
1592
1593
0
      return( -1 );
1594
0
    }
1595
    /* Convert the UTF-8 character bytes into an Unicode character
1596
     */
1597
0
    if( libuna_unicode_character_copy_from_utf8(
1598
0
         &utf8_stream_unicode_character,
1599
0
         utf8_stream,
1600
0
         utf8_stream_size,
1601
0
         &utf8_stream_index,
1602
0
                     error ) != 1 )
1603
0
    {
1604
0
      libcerror_error_set(
1605
0
       error,
1606
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
1607
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
1608
0
       "%s: unable to copy Unicode character from UTF-8 stream.",
1609
0
       function );
1610
1611
0
      return( -1 );
1612
0
    }
1613
0
    if( utf32_unicode_character < utf8_stream_unicode_character )
1614
0
    {
1615
0
      return( LIBUNA_COMPARE_LESS );
1616
0
    }
1617
0
    else if( utf32_unicode_character > utf8_stream_unicode_character )
1618
0
    {
1619
0
      return( LIBUNA_COMPARE_GREATER );
1620
0
    }
1621
0
  }
1622
  /* Check if both strings were entirely processed
1623
   */
1624
0
  if( utf32_string_index < utf32_string_size )
1625
0
  {
1626
0
    return( LIBUNA_COMPARE_GREATER );
1627
0
  }
1628
0
  else if( utf8_stream_index < utf8_stream_size )
1629
0
  {
1630
0
    return( LIBUNA_COMPARE_LESS );
1631
0
  }
1632
0
  return( LIBUNA_COMPARE_EQUAL );
1633
0
}
1634
1635
/* Determines the size of an UTF-32 string from an UTF-16 string
1636
 * Returns 1 if successful or -1 on error
1637
 */
1638
int libuna_utf32_string_size_from_utf16(
1639
     const libuna_utf16_character_t *utf16_string,
1640
     size_t utf16_string_size,
1641
     size_t *utf32_string_size,
1642
     libcerror_error_t **error )
1643
0
{
1644
0
  static char *function                        = "libuna_utf32_string_size_from_utf16";
1645
0
  size_t utf16_string_index                    = 0;
1646
0
  libuna_unicode_character_t unicode_character = 0;
1647
1648
0
  if( utf16_string == NULL )
1649
0
  {
1650
0
    libcerror_error_set(
1651
0
     error,
1652
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1653
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1654
0
     "%s: invalid UTF-16 string.",
1655
0
     function );
1656
1657
0
    return( -1 );
1658
0
  }
1659
0
  if( utf16_string_size > (size_t) SSIZE_MAX )
1660
0
  {
1661
0
    libcerror_error_set(
1662
0
     error,
1663
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1664
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1665
0
     "%s: invalid UTF-16 string size value exceeds maximum.",
1666
0
     function );
1667
1668
0
    return( -1 );
1669
0
  }
1670
0
  if( utf32_string_size == NULL )
1671
0
  {
1672
0
    libcerror_error_set(
1673
0
     error,
1674
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1675
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1676
0
     "%s: invalid UTF-32 string size.",
1677
0
     function );
1678
1679
0
    return( -1 );
1680
0
  }
1681
0
  *utf32_string_size = 0;
1682
1683
0
  if( utf16_string_size == 0 )
1684
0
  {
1685
0
    return( 1 );
1686
0
  }
1687
0
  while( utf16_string_index < utf16_string_size )
1688
0
  {
1689
    /* Convert the UTF-16 character bytes into an Unicode character
1690
     */
1691
0
    if( libuna_unicode_character_copy_from_utf16(
1692
0
         &unicode_character,
1693
0
         utf16_string,
1694
0
         utf16_string_size,
1695
0
         &utf16_string_index,
1696
0
         error ) != 1 )
1697
0
    {
1698
0
      libcerror_error_set(
1699
0
       error,
1700
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
1701
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
1702
0
       "%s: unable to copy Unicode character from UTF-16.",
1703
0
       function );
1704
1705
0
      return( -1 );
1706
0
    }
1707
    /* Determine how many UTF-32 character bytes are required
1708
     */
1709
0
    if( libuna_unicode_character_size_to_utf32(
1710
0
         unicode_character,
1711
0
         utf32_string_size,
1712
0
         error ) != 1 )
1713
0
    {
1714
0
      libcerror_error_set(
1715
0
       error,
1716
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
1717
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
1718
0
       "%s: unable to unable to determine size of Unicode character in UTF-32.",
1719
0
       function );
1720
1721
0
      return( -1 );
1722
0
    }
1723
0
    if( unicode_character == 0 )
1724
0
    {
1725
0
      break;
1726
0
    }
1727
0
  }
1728
  /* Check if the string is terminated with an end-of-string character
1729
   */
1730
0
  if( unicode_character != 0 )
1731
0
  {
1732
0
    *utf32_string_size += 1;
1733
0
  }
1734
0
  return( 1 );
1735
0
}
1736
1737
/* Copies an UTF-32 string from an UTF-16 string
1738
 * Returns 1 if successful or -1 on error
1739
 */
1740
int libuna_utf32_string_copy_from_utf16(
1741
     libuna_utf32_character_t *utf32_string,
1742
     size_t utf32_string_size,
1743
     const uint16_t *utf16_string,
1744
     size_t utf16_string_size,
1745
     libcerror_error_t **error )
1746
0
{
1747
0
  static char *function     = "libuna_utf32_string_copy_from_utf16";
1748
0
  size_t utf32_string_index = 0;
1749
1750
0
  if( libuna_utf32_string_with_index_copy_from_utf16(
1751
0
       utf32_string,
1752
0
       utf32_string_size,
1753
0
       &utf32_string_index,
1754
0
       utf16_string,
1755
0
       utf16_string_size,
1756
0
       error ) != 1 )
1757
0
  {
1758
0
    libcerror_error_set(
1759
0
     error,
1760
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1761
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
1762
0
     "%s: unable to copy UTF-16 string to UTF-32 string.",
1763
0
     function );
1764
1765
0
    return( -1 );
1766
0
  }
1767
0
  return( 1 );
1768
0
}
1769
1770
/* Copies an UTF-32 string from an UTF-16 string
1771
 * Returns 1 if successful or -1 on error
1772
 */
1773
int libuna_utf32_string_with_index_copy_from_utf16(
1774
     libuna_utf32_character_t *utf32_string,
1775
     size_t utf32_string_size,
1776
     size_t *utf32_string_index,
1777
     const uint16_t *utf16_string,
1778
     size_t utf16_string_size,
1779
     libcerror_error_t **error )
1780
0
{
1781
0
  static char *function                        = "libuna_utf32_string_with_index_copy_from_utf16";
1782
0
  size_t utf16_string_index                    = 0;
1783
0
  libuna_unicode_character_t unicode_character = 0;
1784
1785
0
  if( utf32_string == NULL )
1786
0
  {
1787
0
    libcerror_error_set(
1788
0
     error,
1789
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1790
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1791
0
     "%s: invalid UTF-32 string.",
1792
0
     function );
1793
1794
0
    return( -1 );
1795
0
  }
1796
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
1797
0
  {
1798
0
    libcerror_error_set(
1799
0
     error,
1800
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1801
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1802
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
1803
0
     function );
1804
1805
0
    return( -1 );
1806
0
  }
1807
0
  if( utf32_string_index == NULL )
1808
0
  {
1809
0
    libcerror_error_set(
1810
0
     error,
1811
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1812
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1813
0
     "%s: invalid UTF-32 string index.",
1814
0
     function );
1815
1816
0
    return( -1 );
1817
0
  }
1818
0
  if( utf16_string == NULL )
1819
0
  {
1820
0
    libcerror_error_set(
1821
0
     error,
1822
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1823
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1824
0
     "%s: invalid UTF-16 string.",
1825
0
     function );
1826
1827
0
    return( -1 );
1828
0
  }
1829
0
  if( utf16_string_size > (size_t) SSIZE_MAX )
1830
0
  {
1831
0
    libcerror_error_set(
1832
0
     error,
1833
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1834
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1835
0
     "%s: invalid UTF-16 string size value exceeds maximum.",
1836
0
     function );
1837
1838
0
    return( -1 );
1839
0
  }
1840
0
  if( utf16_string_size == 0 )
1841
0
  {
1842
0
    libcerror_error_set(
1843
0
     error,
1844
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1845
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
1846
0
     "%s: missing UTF-16 string value.",
1847
0
     function );
1848
1849
0
    return( -1 );
1850
0
  }
1851
0
  while( utf16_string_index < utf16_string_size )
1852
0
  {
1853
    /* Convert the UTF-16 character bytes into an Unicode character
1854
     */
1855
0
    if( libuna_unicode_character_copy_from_utf16(
1856
0
         &unicode_character,
1857
0
         utf16_string,
1858
0
         utf16_string_size,
1859
0
         &utf16_string_index,
1860
0
         error ) != 1 )
1861
0
    {
1862
0
      libcerror_error_set(
1863
0
       error,
1864
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
1865
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
1866
0
       "%s: unable to copy Unicode character from UTF-16.",
1867
0
       function );
1868
1869
0
      return( -1 );
1870
0
    }
1871
    /* Convert the Unicode character into UTF-32 character bytes
1872
     */
1873
0
    if( libuna_unicode_character_copy_to_utf32(
1874
0
         unicode_character,
1875
0
         utf32_string,
1876
0
         utf32_string_size,
1877
0
         utf32_string_index,
1878
0
         error ) != 1 )
1879
0
    {
1880
0
      libcerror_error_set(
1881
0
       error,
1882
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
1883
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
1884
0
       "%s: unable to copy Unicode character to UTF-32.",
1885
0
       function );
1886
1887
0
      return( -1 );
1888
0
    }
1889
0
    if( unicode_character == 0 )
1890
0
    {
1891
0
      break;
1892
0
    }
1893
0
  }
1894
  /* Check if the string is terminated with an end-of-string character
1895
   */
1896
0
  if( unicode_character != 0 )
1897
0
  {
1898
0
    if( *utf32_string_index >= utf32_string_size )
1899
0
    {
1900
0
      libcerror_error_set(
1901
0
       error,
1902
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1903
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1904
0
       "%s: UTF-32 string too small.",
1905
0
       function );
1906
1907
0
      return( -1 );
1908
0
    }
1909
0
    utf32_string[ *utf32_string_index ] = 0;
1910
1911
0
    *utf32_string_index += 1;
1912
0
  }
1913
0
  return( 1 );
1914
0
}
1915
1916
/* Determines the size of an UTF-32 string from an UTF-16 stream
1917
 * Returns 1 if successful or -1 on error
1918
 */
1919
int libuna_utf32_string_size_from_utf16_stream(
1920
     const uint8_t *utf16_stream,
1921
     size_t utf16_stream_size,
1922
     int byte_order,
1923
     size_t *utf32_string_size,
1924
     libcerror_error_t **error )
1925
0
{
1926
0
  static char *function                        = "libuna_utf32_string_size_from_utf16_stream";
1927
0
  libuna_unicode_character_t unicode_character = 0;
1928
0
  size_t utf16_stream_index                    = 0;
1929
0
  int read_byte_order                          = 0;
1930
0
  int result                                   = 0;
1931
1932
0
  if( utf16_stream == NULL )
1933
0
  {
1934
0
    libcerror_error_set(
1935
0
     error,
1936
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1937
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1938
0
     "%s: invalid UTF-16 stream.",
1939
0
     function );
1940
1941
0
    return( -1 );
1942
0
  }
1943
0
  if( utf16_stream_size > (size_t) SSIZE_MAX )
1944
0
  {
1945
0
    libcerror_error_set(
1946
0
     error,
1947
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1948
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1949
0
     "%s: invalid UTF-16 stream size value exceeds maximum.",
1950
0
     function );
1951
1952
0
    return( -1 );
1953
0
  }
1954
0
  if( ( utf16_stream_size % 2 ) != 0 )
1955
0
  {
1956
0
    libcerror_error_set(
1957
0
     error,
1958
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1959
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1960
0
     "%s: missing UTF-16 stream value.",
1961
0
     function );
1962
1963
0
    return( -1 );
1964
0
  }
1965
0
  if( utf32_string_size == NULL )
1966
0
  {
1967
0
    libcerror_error_set(
1968
0
     error,
1969
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1970
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1971
0
     "%s: invalid UTF-32 string size.",
1972
0
     function );
1973
1974
0
    return( -1 );
1975
0
  }
1976
0
  *utf32_string_size = 0;
1977
1978
0
  if( utf16_stream_size == 0 )
1979
0
  {
1980
0
    return( 1 );
1981
0
  }
1982
  /* Check if UTF-16 stream is in big or little endian
1983
   */
1984
0
  if( utf16_stream_size >= 2 )
1985
0
  {
1986
0
    if( ( utf16_stream[ 0 ] == 0x0ff )
1987
0
     && ( utf16_stream[ 1 ] == 0x0fe ) )
1988
0
    {
1989
0
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
1990
0
      utf16_stream_index = 2;
1991
0
    }
1992
0
    else if( ( utf16_stream[ 0 ] == 0x0fe )
1993
0
          && ( utf16_stream[ 1 ] == 0x0ff ) )
1994
0
    {
1995
0
      read_byte_order    = LIBUNA_ENDIAN_BIG;
1996
0
      utf16_stream_index = 2;
1997
0
    }
1998
0
    if( byte_order == 0 )
1999
0
    {
2000
0
      byte_order = read_byte_order;
2001
0
    }
2002
0
  }
2003
0
  while( ( utf16_stream_index + 1 ) < utf16_stream_size )
2004
0
  {
2005
    /* Convert the UTF-16 stream bytes into an Unicode character
2006
     */
2007
0
    if( libuna_unicode_character_copy_from_utf16_stream(
2008
0
         &unicode_character,
2009
0
         utf16_stream,
2010
0
         utf16_stream_size,
2011
0
         &utf16_stream_index,
2012
0
         byte_order,
2013
0
         error ) != 1 )
2014
0
    {
2015
0
      libcerror_error_set(
2016
0
       error,
2017
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2018
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2019
0
       "%s: unable to copy Unicode character from UTF-16 stream.",
2020
0
       function );
2021
2022
0
      return( -1 );
2023
0
    }
2024
    /* Determine how many UTF-8 character bytes are required
2025
     */
2026
0
    if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
2027
0
    {
2028
0
      result = libuna_unicode_character_size_to_utf32(
2029
0
                unicode_character,
2030
0
                utf32_string_size,
2031
0
                error );
2032
0
    }
2033
0
    else
2034
0
    {
2035
0
      result = libuna_unicode_character_size_to_ucs4(
2036
0
                unicode_character,
2037
0
                utf32_string_size,
2038
0
                error );
2039
0
    }
2040
0
    if( result != 1 )
2041
0
    {
2042
0
      libcerror_error_set(
2043
0
       error,
2044
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2045
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2046
0
       "%s: unable to unable to determine size of Unicode character in UTF-32.",
2047
0
       function );
2048
2049
0
      return( -1 );
2050
0
    }
2051
0
    if( unicode_character == 0 )
2052
0
    {
2053
0
      break;
2054
0
    }
2055
0
  }
2056
  /* Check if the string is terminated with an end-of-string character
2057
   */
2058
0
  if( unicode_character != 0 )
2059
0
  {
2060
0
    *utf32_string_size += 1;
2061
0
  }
2062
0
  return( 1 );
2063
0
}
2064
2065
/* Copies an UTF-32 string from an UTF-16 stream
2066
 * Returns 1 if successful or -1 on error
2067
 */
2068
int libuna_utf32_string_copy_from_utf16_stream(
2069
     libuna_utf32_character_t *utf32_string,
2070
     size_t utf32_string_size,
2071
     const uint8_t *utf16_stream,
2072
     size_t utf16_stream_size,
2073
     int byte_order,
2074
     libcerror_error_t **error )
2075
0
{
2076
0
  static char *function     = "libuna_utf32_string_copy_from_utf16_stream";
2077
0
  size_t utf32_string_index = 0;
2078
2079
0
  if( libuna_utf32_string_with_index_copy_from_utf16_stream(
2080
0
       utf32_string,
2081
0
       utf32_string_size,
2082
0
       &utf32_string_index,
2083
0
       utf16_stream,
2084
0
       utf16_stream_size,
2085
0
       byte_order,
2086
0
       error ) != 1 )
2087
0
  {
2088
0
    libcerror_error_set(
2089
0
     error,
2090
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2091
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
2092
0
     "%s: unable to copy UTF-16 stream to UTF-32 string.",
2093
0
     function );
2094
2095
0
    return( -1 );
2096
0
  }
2097
0
  return( 1 );
2098
0
}
2099
2100
/* Copies an UTF-32 string from an UTF-16 stream
2101
 * Returns 1 if successful or -1 on error
2102
 */
2103
int libuna_utf32_string_with_index_copy_from_utf16_stream(
2104
     libuna_utf32_character_t *utf32_string,
2105
     size_t utf32_string_size,
2106
     size_t *utf32_string_index,
2107
     const uint8_t *utf16_stream,
2108
     size_t utf16_stream_size,
2109
     int byte_order,
2110
     libcerror_error_t **error )
2111
0
{
2112
0
  static char *function                        = "libuna_utf32_string_with_index_copy_from_utf16_stream";
2113
0
  libuna_unicode_character_t unicode_character = 0;
2114
0
  size_t utf16_stream_index                    = 0;
2115
0
  int read_byte_order                          = 0;
2116
0
  int result                                   = 0;
2117
2118
0
  if( utf32_string == NULL )
2119
0
  {
2120
0
    libcerror_error_set(
2121
0
     error,
2122
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2123
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2124
0
     "%s: invalid UTF-32 string.",
2125
0
     function );
2126
2127
0
    return( -1 );
2128
0
  }
2129
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
2130
0
  {
2131
0
    libcerror_error_set(
2132
0
     error,
2133
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2134
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2135
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
2136
0
     function );
2137
2138
0
    return( -1 );
2139
0
  }
2140
0
  if( utf32_string_index == NULL )
2141
0
  {
2142
0
    libcerror_error_set(
2143
0
     error,
2144
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2145
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2146
0
     "%s: invalid UTF-32 string index.",
2147
0
     function );
2148
2149
0
    return( -1 );
2150
0
  }
2151
0
  if( utf16_stream == NULL )
2152
0
  {
2153
0
    libcerror_error_set(
2154
0
     error,
2155
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2156
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2157
0
     "%s: invalid UTF-16 stream.",
2158
0
     function );
2159
2160
0
    return( -1 );
2161
0
  }
2162
0
  if( utf16_stream_size > (size_t) SSIZE_MAX )
2163
0
  {
2164
0
    libcerror_error_set(
2165
0
     error,
2166
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2167
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2168
0
     "%s: invalid UTF-16 stream size value exceeds maximum.",
2169
0
     function );
2170
2171
0
    return( -1 );
2172
0
  }
2173
0
  if( ( utf16_stream_size == 0 )
2174
0
   || ( ( utf16_stream_size % 2 ) != 0 ) )
2175
0
  {
2176
0
    libcerror_error_set(
2177
0
     error,
2178
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2179
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2180
0
     "%s: missing UTF-16 stream value.",
2181
0
     function );
2182
2183
0
    return( -1 );
2184
0
  }
2185
  /* Check if UTF-16 stream is in big or little endian
2186
   */
2187
0
  if( utf16_stream_size >= 2 )
2188
0
  {
2189
0
    if( ( utf16_stream[ 0 ] == 0x0ff )
2190
0
     && ( utf16_stream[ 1 ] == 0x0fe ) )
2191
0
    {
2192
0
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
2193
0
      utf16_stream_index = 2;
2194
0
    }
2195
0
    else if( ( utf16_stream[ 0 ] == 0x0fe )
2196
0
          && ( utf16_stream[ 1 ] == 0x0ff ) )
2197
0
    {
2198
0
      read_byte_order    = LIBUNA_ENDIAN_BIG;
2199
0
      utf16_stream_index = 2;
2200
0
    }
2201
0
    if( byte_order == 0 )
2202
0
    {
2203
0
      byte_order = read_byte_order;
2204
0
    }
2205
0
  }
2206
0
  while( ( utf16_stream_index + 1 ) < utf16_stream_size )
2207
0
  {
2208
    /* Convert the UTF-16 stream bytes into an Unicode character
2209
     */
2210
0
    if( libuna_unicode_character_copy_from_utf16_stream(
2211
0
         &unicode_character,
2212
0
         utf16_stream,
2213
0
         utf16_stream_size,
2214
0
         &utf16_stream_index,
2215
0
         byte_order,
2216
0
         error ) != 1 )
2217
0
    {
2218
0
      libcerror_error_set(
2219
0
       error,
2220
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2221
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2222
0
       "%s: unable to copy Unicode character from UTF-16 stream.",
2223
0
       function );
2224
2225
0
      return( -1 );
2226
0
    }
2227
    /* Convert the Unicode character into UTF-32 character bytes
2228
     */
2229
0
    if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
2230
0
    {
2231
0
      result = libuna_unicode_character_copy_to_utf32(
2232
0
                unicode_character,
2233
0
                utf32_string,
2234
0
                utf32_string_size,
2235
0
                utf32_string_index,
2236
0
                error );
2237
0
    }
2238
0
    else
2239
0
    {
2240
0
      result = libuna_unicode_character_copy_to_ucs4(
2241
0
                unicode_character,
2242
0
                utf32_string,
2243
0
                utf32_string_size,
2244
0
                utf32_string_index,
2245
0
                error );
2246
0
    }
2247
0
    if( result != 1 )
2248
0
    {
2249
0
      libcerror_error_set(
2250
0
       error,
2251
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2252
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
2253
0
       "%s: unable to copy Unicode character to UTF-32.",
2254
0
       function );
2255
2256
0
      return( -1 );
2257
0
    }
2258
0
    if( unicode_character == 0 )
2259
0
    {
2260
0
      break;
2261
0
    }
2262
0
  }
2263
  /* Check if the string is terminated with an end-of-string character
2264
   */
2265
0
  if( unicode_character != 0 )
2266
0
  {
2267
0
    if( *utf32_string_index >= utf32_string_size )
2268
0
    {
2269
0
      libcerror_error_set(
2270
0
       error,
2271
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2272
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2273
0
       "%s: UTF-32 string too small.",
2274
0
       function );
2275
2276
0
      return( -1 );
2277
0
    }
2278
0
    utf32_string[ *utf32_string_index ] = 0;
2279
2280
0
    *utf32_string_index += 1;
2281
0
  }
2282
0
  return( 1 );
2283
0
}
2284
2285
/* Compares an UTF-32 string with an UTF-16 stream
2286
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
2287
 */
2288
int libuna_utf32_string_compare_with_utf16_stream(
2289
     const libuna_utf32_character_t *utf32_string,
2290
     size_t utf32_string_size,
2291
     const uint8_t *utf16_stream,
2292
     size_t utf16_stream_size,
2293
     int byte_order,
2294
     libcerror_error_t **error )
2295
0
{
2296
0
  static char *function                                     = "libuna_utf32_string_compare_with_utf16_stream";
2297
0
  libuna_unicode_character_t utf16_stream_unicode_character = 0;
2298
0
  libuna_unicode_character_t utf32_unicode_character        = 0;
2299
0
  size_t utf16_stream_index                                 = 0;
2300
0
  size_t utf32_string_index                                 = 0;
2301
0
  int read_byte_order                                       = 0;
2302
0
  int result                                                = 0;
2303
2304
0
  if( utf32_string == NULL )
2305
0
  {
2306
0
    libcerror_error_set(
2307
0
     error,
2308
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2309
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2310
0
     "%s: invalid UTF-32 string.",
2311
0
     function );
2312
2313
0
    return( -1 );
2314
0
  }
2315
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
2316
0
  {
2317
0
    libcerror_error_set(
2318
0
     error,
2319
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2320
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2321
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
2322
0
     function );
2323
2324
0
    return( -1 );
2325
0
  }
2326
0
  if( utf16_stream == NULL )
2327
0
  {
2328
0
    libcerror_error_set(
2329
0
     error,
2330
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2331
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2332
0
     "%s: invalid UTF-16 stream.",
2333
0
     function );
2334
2335
0
    return( -1 );
2336
0
  }
2337
0
  if( utf16_stream_size > (size_t) SSIZE_MAX )
2338
0
  {
2339
0
    libcerror_error_set(
2340
0
     error,
2341
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2342
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2343
0
     "%s: invalid UTF-16 stream size value exceeds maximum.",
2344
0
     function );
2345
2346
0
    return( -1 );
2347
0
  }
2348
0
  if( ( utf16_stream_size == 0 )
2349
0
   || ( ( utf16_stream_size % 2 ) != 0 ) )
2350
0
  {
2351
0
    libcerror_error_set(
2352
0
     error,
2353
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2354
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2355
0
     "%s: missing UTF-16 stream value.",
2356
0
     function );
2357
2358
0
    return( -1 );
2359
0
  }
2360
  /* Check if UTF-16 stream is in big or little endian
2361
   */
2362
0
  if( utf16_stream_size >= 2 )
2363
0
  {
2364
0
    if( ( utf16_stream[ 0 ] == 0xfe )
2365
0
     && ( utf16_stream[ 1 ] == 0xff ) )
2366
0
    {
2367
0
      read_byte_order    = LIBUNA_ENDIAN_BIG;
2368
0
      utf16_stream_index = 2;
2369
0
    }
2370
0
    else if( ( utf16_stream[ 0 ] == 0xff )
2371
0
          && ( utf16_stream[ 1 ] == 0xfe ) )
2372
0
    {
2373
0
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
2374
0
      utf16_stream_index = 2;
2375
0
    }
2376
0
    if( byte_order == 0 )
2377
0
    {
2378
0
      byte_order = read_byte_order;
2379
0
    }
2380
0
  }
2381
0
  if( ( utf32_string_size >= 1 )
2382
0
   && ( utf32_string[ utf32_string_size - 1 ] == 0 ) )
2383
0
  {
2384
0
    utf32_string_size -= 1;
2385
0
  }
2386
  /* Check if the UTF-16 stream is terminated with zero bytes
2387
   */
2388
0
  if( ( utf16_stream_size >= 2 )
2389
0
   && ( utf16_stream[ utf16_stream_size - 2 ] == 0 )
2390
0
   && ( utf16_stream[ utf16_stream_size - 1 ] == 0 ) )
2391
0
  {
2392
0
    utf16_stream_size -= 2;
2393
0
  }
2394
0
  while( ( utf32_string_index < utf32_string_size )
2395
0
      && ( utf16_stream_index < utf16_stream_size ) )
2396
0
  {
2397
    /* Convert the UTF-32 character bytes into an Unicode character
2398
     */
2399
0
    if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
2400
0
    {
2401
0
      result = libuna_unicode_character_copy_from_utf32(
2402
0
                &utf32_unicode_character,
2403
0
                utf32_string,
2404
0
                utf32_string_size,
2405
0
                &utf32_string_index,
2406
0
                error );
2407
0
    }
2408
0
    else
2409
0
    {
2410
0
      result = libuna_unicode_character_copy_from_ucs4(
2411
0
                &utf32_unicode_character,
2412
0
                utf32_string,
2413
0
                utf32_string_size,
2414
0
                &utf32_string_index,
2415
0
                error );
2416
0
    }
2417
0
    if( result != 1 )
2418
0
    {
2419
0
      libcerror_error_set(
2420
0
       error,
2421
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2422
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
2423
0
       "%s: unable to copy Unicode character from UTF-32.",
2424
0
       function );
2425
2426
0
      return( -1 );
2427
0
    }
2428
    /* Convert the UTF-16 stream bytes into an Unicode character
2429
     */
2430
0
    if( libuna_unicode_character_copy_from_utf16_stream(
2431
0
         &utf16_stream_unicode_character,
2432
0
         utf16_stream,
2433
0
         utf16_stream_size,
2434
0
         &utf16_stream_index,
2435
0
         byte_order,
2436
0
                     error ) != 1 )
2437
0
    {
2438
0
      libcerror_error_set(
2439
0
       error,
2440
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2441
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2442
0
       "%s: unable to copy Unicode character from UTF-16 stream.",
2443
0
       function );
2444
2445
0
      return( -1 );
2446
0
    }
2447
0
    if( utf32_unicode_character < utf16_stream_unicode_character )
2448
0
    {
2449
0
      return( LIBUNA_COMPARE_LESS );
2450
0
    }
2451
0
    else if( utf32_unicode_character > utf16_stream_unicode_character )
2452
0
    {
2453
0
      return( LIBUNA_COMPARE_GREATER );
2454
0
    }
2455
0
  }
2456
  /* Check if both strings were entirely processed
2457
   */
2458
0
  if( utf32_string_index < utf32_string_size )
2459
0
  {
2460
0
    return( LIBUNA_COMPARE_GREATER );
2461
0
  }
2462
0
  else if( utf16_stream_index < utf16_stream_size )
2463
0
  {
2464
0
    return( LIBUNA_COMPARE_LESS );
2465
0
  }
2466
0
  return( LIBUNA_COMPARE_EQUAL );
2467
0
}
2468
2469
/* Determines the size of an UTF-32 string from an UTF-32 stream
2470
 * Returns 1 if successful or -1 on error
2471
 */
2472
int libuna_utf32_string_size_from_utf32_stream(
2473
     const uint8_t *utf32_stream,
2474
     size_t utf32_stream_size,
2475
     int byte_order,
2476
     size_t *utf32_string_size,
2477
     libcerror_error_t **error )
2478
0
{
2479
0
  static char *function                        = "libuna_utf32_string_size_from_utf32_stream";
2480
0
  size_t utf32_stream_index                    = 0;
2481
0
  libuna_unicode_character_t unicode_character = 0;
2482
0
  int read_byte_order                          = 0;
2483
2484
0
  if( utf32_stream == NULL )
2485
0
  {
2486
0
    libcerror_error_set(
2487
0
     error,
2488
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2489
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2490
0
     "%s: invalid UTF-32 stream.",
2491
0
     function );
2492
2493
0
    return( -1 );
2494
0
  }
2495
0
  if( utf32_stream_size > (size_t) SSIZE_MAX )
2496
0
  {
2497
0
    libcerror_error_set(
2498
0
     error,
2499
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2500
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2501
0
     "%s: invalid UTF-32 stream size value exceeds maximum.",
2502
0
     function );
2503
2504
0
    return( -1 );
2505
0
  }
2506
0
  if( ( utf32_stream_size % 4 ) != 0 )
2507
0
  {
2508
0
    libcerror_error_set(
2509
0
     error,
2510
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2511
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2512
0
     "%s: missing UTF-32 stream value.",
2513
0
     function );
2514
2515
0
    return( -1 );
2516
0
  }
2517
0
  if( utf32_string_size == NULL )
2518
0
  {
2519
0
    libcerror_error_set(
2520
0
     error,
2521
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2522
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2523
0
     "%s: invalid UTF-32 string size.",
2524
0
     function );
2525
2526
0
    return( -1 );
2527
0
  }
2528
0
  *utf32_string_size = 0;
2529
2530
0
  if( utf32_stream_size == 0 )
2531
0
  {
2532
0
    return( 1 );
2533
0
  }
2534
  /* Check if UTF-32 stream is in big or little endian
2535
   */
2536
0
  if( utf32_stream_size >= 4 )
2537
0
  {
2538
0
    if( ( utf32_stream[ 0 ] == 0x00 )
2539
0
     && ( utf32_stream[ 1 ] == 0x00 )
2540
0
     && ( utf32_stream[ 2 ] == 0xfe )
2541
0
     && ( utf32_stream[ 3 ] == 0xff ) )
2542
0
    {
2543
0
      read_byte_order    = LIBUNA_ENDIAN_BIG;
2544
0
      utf32_stream_index = 4;
2545
0
    }
2546
0
    else if( ( utf32_stream[ 0 ] == 0xff )
2547
0
          && ( utf32_stream[ 1 ] == 0xfe )
2548
0
          && ( utf32_stream[ 2 ] == 0x00 )
2549
0
          && ( utf32_stream[ 3 ] == 0x00 ) )
2550
0
    {
2551
0
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
2552
0
      utf32_stream_index = 4;
2553
0
    }
2554
0
    if( byte_order == 0 )
2555
0
    {
2556
0
      byte_order = read_byte_order;
2557
0
    }
2558
0
  }
2559
0
  while( ( utf32_stream_index + 1 ) < utf32_stream_size )
2560
0
  {
2561
    /* Convert the UTF-32 stream bytes into an Unicode character
2562
     */
2563
0
    if( libuna_unicode_character_copy_from_utf32_stream(
2564
0
         &unicode_character,
2565
0
         utf32_stream,
2566
0
         utf32_stream_size,
2567
0
         &utf32_stream_index,
2568
0
         byte_order,
2569
0
         error ) != 1 )
2570
0
    {
2571
0
      libcerror_error_set(
2572
0
       error,
2573
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2574
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2575
0
       "%s: unable to copy Unicode character from UTF-32 stream.",
2576
0
       function );
2577
2578
0
      return( -1 );
2579
0
    }
2580
    /* Determine how many UTF-32 character bytes are required
2581
     */
2582
0
    if( libuna_unicode_character_size_to_utf32(
2583
0
         unicode_character,
2584
0
         utf32_string_size,
2585
0
         error ) != 1 )
2586
0
    {
2587
0
      libcerror_error_set(
2588
0
       error,
2589
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2590
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2591
0
       "%s: unable to unable to determine size of Unicode character in UTF-32.",
2592
0
       function );
2593
2594
0
      return( -1 );
2595
0
    }
2596
0
    if( unicode_character == 0 )
2597
0
    {
2598
0
      break;
2599
0
    }
2600
0
  }
2601
  /* Check if the string is terminated with an end-of-string character
2602
   */
2603
0
  if( unicode_character != 0 )
2604
0
  {
2605
0
    *utf32_string_size += 1;
2606
0
  }
2607
0
  return( 1 );
2608
0
}
2609
2610
/* Copies an UTF-32 string from an UTF-32 stream
2611
 * Returns 1 if successful or -1 on error
2612
 */
2613
int libuna_utf32_string_copy_from_utf32_stream(
2614
     libuna_utf32_character_t *utf32_string,
2615
     size_t utf32_string_size,
2616
     const uint8_t *utf32_stream,
2617
     size_t utf32_stream_size,
2618
     int byte_order,
2619
     libcerror_error_t **error )
2620
0
{
2621
0
  static char *function     = "libuna_utf32_string_copy_from_utf32_stream";
2622
0
  size_t utf32_string_index = 0;
2623
2624
0
  if( libuna_utf32_string_with_index_copy_from_utf32_stream(
2625
0
       utf32_string,
2626
0
       utf32_string_size,
2627
0
       &utf32_string_index,
2628
0
       utf32_stream,
2629
0
       utf32_stream_size,
2630
0
       byte_order,
2631
0
       error ) != 1 )
2632
0
  {
2633
0
    libcerror_error_set(
2634
0
     error,
2635
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2636
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
2637
0
     "%s: unable to copy UTF-32 stream to UTF-32 string.",
2638
0
     function );
2639
2640
0
    return( -1 );
2641
0
  }
2642
0
  return( 1 );
2643
0
}
2644
2645
/* Copies an UTF-32 string from an UTF-32 stream
2646
 * Returns 1 if successful or -1 on error
2647
 */
2648
int libuna_utf32_string_with_index_copy_from_utf32_stream(
2649
     libuna_utf32_character_t *utf32_string,
2650
     size_t utf32_string_size,
2651
     size_t *utf32_string_index,
2652
     const uint8_t *utf32_stream,
2653
     size_t utf32_stream_size,
2654
     int byte_order,
2655
     libcerror_error_t **error )
2656
0
{
2657
0
  static char *function                        = "libuna_utf32_string_with_index_copy_from_utf32_stream";
2658
0
  size_t utf32_stream_index                    = 0;
2659
0
  libuna_unicode_character_t unicode_character = 0;
2660
0
  int read_byte_order                          = 0;
2661
2662
0
  if( utf32_string == NULL )
2663
0
  {
2664
0
    libcerror_error_set(
2665
0
     error,
2666
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2667
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2668
0
     "%s: invalid UTF-32 string.",
2669
0
     function );
2670
2671
0
    return( -1 );
2672
0
  }
2673
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
2674
0
  {
2675
0
    libcerror_error_set(
2676
0
     error,
2677
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2678
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2679
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
2680
0
     function );
2681
2682
0
    return( -1 );
2683
0
  }
2684
0
  if( utf32_string_index == NULL )
2685
0
  {
2686
0
    libcerror_error_set(
2687
0
     error,
2688
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2689
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2690
0
     "%s: invalid UTF-32 string index.",
2691
0
     function );
2692
2693
0
    return( -1 );
2694
0
  }
2695
0
  if( utf32_stream == NULL )
2696
0
  {
2697
0
    libcerror_error_set(
2698
0
     error,
2699
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2700
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2701
0
     "%s: invalid UTF-32 stream.",
2702
0
     function );
2703
2704
0
    return( -1 );
2705
0
  }
2706
0
  if( utf32_stream_size > (size_t) SSIZE_MAX )
2707
0
  {
2708
0
    libcerror_error_set(
2709
0
     error,
2710
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2711
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2712
0
     "%s: invalid UTF-32 stream size value exceeds maximum.",
2713
0
     function );
2714
2715
0
    return( -1 );
2716
0
  }
2717
0
  if( ( utf32_stream_size == 0 )
2718
0
   || ( ( utf32_stream_size % 4 ) != 0 ) )
2719
0
  {
2720
0
    libcerror_error_set(
2721
0
     error,
2722
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2723
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2724
0
     "%s: missing UTF-32 stream value.",
2725
0
     function );
2726
2727
0
    return( -1 );
2728
0
  }
2729
  /* Check if UTF-32 stream is in big or little endian
2730
   */
2731
0
  if( utf32_stream_size >= 4 )
2732
0
  {
2733
0
    if( ( utf32_stream[ 0 ] == 0x00 )
2734
0
     && ( utf32_stream[ 1 ] == 0x00 )
2735
0
     && ( utf32_stream[ 2 ] == 0xfe )
2736
0
     && ( utf32_stream[ 3 ] == 0xff ) )
2737
0
    {
2738
0
      read_byte_order    = LIBUNA_ENDIAN_BIG;
2739
0
      utf32_stream_index = 4;
2740
0
    }
2741
0
    else if( ( utf32_stream[ 0 ] == 0xff )
2742
0
          && ( utf32_stream[ 1 ] == 0xfe )
2743
0
          && ( utf32_stream[ 2 ] == 0x00 )
2744
0
          && ( utf32_stream[ 3 ] == 0x00 ) )
2745
0
    {
2746
0
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
2747
0
      utf32_stream_index = 4;
2748
0
    }
2749
0
    if( byte_order == 0 )
2750
0
    {
2751
0
      byte_order = read_byte_order;
2752
0
    }
2753
0
  }
2754
0
  while( ( utf32_stream_index + 1 ) < utf32_stream_size )
2755
0
  {
2756
    /* Convert the UTF-32 stream bytes into an Unicode character
2757
     */
2758
0
    if( libuna_unicode_character_copy_from_utf32_stream(
2759
0
         &unicode_character,
2760
0
         utf32_stream,
2761
0
         utf32_stream_size,
2762
0
         &utf32_stream_index,
2763
0
         byte_order,
2764
0
         error ) != 1 )
2765
0
    {
2766
0
      libcerror_error_set(
2767
0
       error,
2768
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2769
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2770
0
       "%s: unable to copy Unicode character from UTF-32 stream.",
2771
0
       function );
2772
2773
0
      return( -1 );
2774
0
    }
2775
    /* Convert the Unicode character into UTF-32 character bytes
2776
     */
2777
0
    if( libuna_unicode_character_copy_to_utf32(
2778
0
         unicode_character,
2779
0
         utf32_string,
2780
0
         utf32_string_size,
2781
0
         utf32_string_index,
2782
0
         error ) != 1 )
2783
0
    {
2784
0
      libcerror_error_set(
2785
0
       error,
2786
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2787
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
2788
0
       "%s: unable to copy Unicode character to UTF-32.",
2789
0
       function );
2790
2791
0
      return( -1 );
2792
0
    }
2793
0
    if( unicode_character == 0 )
2794
0
    {
2795
0
      break;
2796
0
    }
2797
0
  }
2798
  /* Check if the string is terminated with an end-of-string character
2799
   */
2800
0
  if( unicode_character != 0 )
2801
0
  {
2802
0
    if( *utf32_string_index >= utf32_string_size )
2803
0
    {
2804
0
      libcerror_error_set(
2805
0
       error,
2806
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2807
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2808
0
       "%s: UTF-32 string too small.",
2809
0
       function );
2810
2811
0
      return( -1 );
2812
0
    }
2813
0
    utf32_string[ *utf32_string_index ] = 0;
2814
2815
0
    *utf32_string_index += 1;
2816
0
  }
2817
0
  return( 1 );
2818
0
}
2819
2820
/* Compares an UTF-32 string with an UTF-32 stream
2821
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
2822
 */
2823
int libuna_utf32_string_compare_with_utf32_stream(
2824
     const libuna_utf32_character_t *utf32_string,
2825
     size_t utf32_string_size,
2826
     const uint8_t *utf32_stream,
2827
     size_t utf32_stream_size,
2828
     int byte_order,
2829
     libcerror_error_t **error )
2830
0
{
2831
0
  static char *function                                     = "libuna_utf32_string_compare_with_utf32_stream";
2832
0
  size_t utf32_stream_index                                 = 0;
2833
0
  size_t utf32_string_index                                 = 0;
2834
0
  libuna_unicode_character_t utf32_unicode_character        = 0;
2835
0
  libuna_unicode_character_t utf32_stream_unicode_character = 0;
2836
0
  int read_byte_order                                       = 0;
2837
2838
0
  if( utf32_string == NULL )
2839
0
  {
2840
0
    libcerror_error_set(
2841
0
     error,
2842
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2843
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2844
0
     "%s: invalid UTF-32 string.",
2845
0
     function );
2846
2847
0
    return( -1 );
2848
0
  }
2849
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
2850
0
  {
2851
0
    libcerror_error_set(
2852
0
     error,
2853
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2854
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2855
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
2856
0
     function );
2857
2858
0
    return( -1 );
2859
0
  }
2860
0
  if( utf32_stream == NULL )
2861
0
  {
2862
0
    libcerror_error_set(
2863
0
     error,
2864
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2865
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2866
0
     "%s: invalid UTF-32 stream.",
2867
0
     function );
2868
2869
0
    return( -1 );
2870
0
  }
2871
0
  if( utf32_stream_size > (size_t) SSIZE_MAX )
2872
0
  {
2873
0
    libcerror_error_set(
2874
0
     error,
2875
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2876
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2877
0
     "%s: invalid UTF-32 stream size value exceeds maximum.",
2878
0
     function );
2879
2880
0
    return( -1 );
2881
0
  }
2882
0
  if( ( utf32_stream_size == 0 )
2883
0
   || ( ( utf32_stream_size % 4 ) != 0 ) )
2884
0
  {
2885
0
    libcerror_error_set(
2886
0
     error,
2887
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2888
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2889
0
     "%s: missing UTF-32 stream value.",
2890
0
     function );
2891
2892
0
    return( -1 );
2893
0
  }
2894
  /* Check if UTF-32 stream is in big or little endian
2895
   */
2896
0
  if( utf32_stream_size >= 4 )
2897
0
  {
2898
0
    if( ( utf32_stream[ 0 ] == 0x00 )
2899
0
     && ( utf32_stream[ 1 ] == 0x00 )
2900
0
     && ( utf32_stream[ 2 ] == 0xfe )
2901
0
     && ( utf32_stream[ 3 ] == 0xff ) )
2902
0
    {
2903
0
      read_byte_order    = LIBUNA_ENDIAN_BIG;
2904
0
      utf32_stream_index = 4;
2905
0
    }
2906
0
    else if( ( utf32_stream[ 0 ] == 0xff )
2907
0
          && ( utf32_stream[ 1 ] == 0xfe )
2908
0
          && ( utf32_stream[ 2 ] == 0x00 )
2909
0
          && ( utf32_stream[ 3 ] == 0x00 ) )
2910
0
    {
2911
0
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
2912
0
      utf32_stream_index = 4;
2913
0
    }
2914
0
    if( byte_order == 0 )
2915
0
    {
2916
0
      byte_order = read_byte_order;
2917
0
    }
2918
0
  }
2919
0
  if( ( utf32_string_size >= 1 )
2920
0
   && ( utf32_string[ utf32_string_size - 1 ] == 0 ) )
2921
0
  {
2922
0
    utf32_string_size -= 1;
2923
0
  }
2924
  /* Check if the UTF-32 stream is terminated with zero bytes
2925
   */
2926
0
  if( ( utf32_stream_size >= 4 )
2927
0
   && ( utf32_stream[ utf32_stream_size - 4 ] == 0 )
2928
0
   && ( utf32_stream[ utf32_stream_size - 3 ] == 0 )
2929
0
   && ( utf32_stream[ utf32_stream_size - 2 ] == 0 )
2930
0
   && ( utf32_stream[ utf32_stream_size - 1 ] == 0 ) )
2931
0
  {
2932
0
    utf32_stream_size -= 1;
2933
0
  }
2934
0
  while( ( utf32_string_index < utf32_string_size )
2935
0
      && ( utf32_stream_index < utf32_stream_size ) )
2936
0
  {
2937
    /* Convert the UTF-32 character bytes into an Unicode character
2938
     */
2939
0
    if( libuna_unicode_character_copy_from_utf32(
2940
0
         &utf32_unicode_character,
2941
0
         utf32_string,
2942
0
         utf32_string_size,
2943
0
         &utf32_string_index,
2944
0
         error ) != 1 )
2945
0
    {
2946
0
      libcerror_error_set(
2947
0
       error,
2948
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2949
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
2950
0
       "%s: unable to copy Unicode character from UTF-32.",
2951
0
       function );
2952
2953
0
      return( -1 );
2954
0
    }
2955
    /* Convert the UTF-32 stream bytes into an Unicode character
2956
     */
2957
0
    if( libuna_unicode_character_copy_from_utf32_stream(
2958
0
         &utf32_stream_unicode_character,
2959
0
         utf32_stream,
2960
0
         utf32_stream_size,
2961
0
         &utf32_stream_index,
2962
0
         byte_order,
2963
0
                     error ) != 1 )
2964
0
    {
2965
0
      libcerror_error_set(
2966
0
       error,
2967
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2968
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2969
0
       "%s: unable to copy Unicode character from UTF-32 stream.",
2970
0
       function );
2971
2972
0
      return( -1 );
2973
0
    }
2974
0
    if( utf32_unicode_character < utf32_stream_unicode_character )
2975
0
    {
2976
0
      return( LIBUNA_COMPARE_LESS );
2977
0
    }
2978
0
    else if( utf32_unicode_character > utf32_stream_unicode_character )
2979
0
    {
2980
0
      return( LIBUNA_COMPARE_GREATER );
2981
0
    }
2982
0
  }
2983
  /* Check if both strings were entirely processed
2984
   */
2985
0
  if( utf32_string_index < utf32_string_size )
2986
0
  {
2987
0
    return( LIBUNA_COMPARE_GREATER );
2988
0
  }
2989
0
  else if( utf32_stream_index < utf32_stream_size )
2990
0
  {
2991
0
    return( LIBUNA_COMPARE_LESS );
2992
0
  }
2993
0
  return( LIBUNA_COMPARE_EQUAL );
2994
0
}
2995
2996
/* Determines the size of an UTF-32 string from a Standard Compression Scheme for Unicode (SCSU) stream
2997
 * Returns 1 if successful or -1 on error
2998
 */
2999
int libuna_utf32_string_size_from_scsu_stream(
3000
     const uint8_t *scsu_stream,
3001
     size_t scsu_stream_size,
3002
     size_t *utf32_string_size,
3003
     libcerror_error_t **error )
3004
0
{
3005
0
  uint32_t scsu_dynamic_window_positions[ 8 ] = {
3006
0
    0x0080, 0x00c0, 0x0400, 0x0600, 0x0900, 0x3040, 0x30a0, 0xff00 };
3007
3008
0
  static char *function                        = "libuna_utf8_string_size_from_scsu_stream";
3009
0
  libuna_unicode_character_t unicode_character = 0;
3010
0
  size_t scsu_stream_index                     = 0;
3011
0
  uint32_t scsu_window_position                = 0;
3012
0
  uint8_t byte_value1                          = 0;
3013
0
  uint8_t byte_value2                          = 0;
3014
0
  uint8_t byte_value3                          = 0;
3015
0
  uint8_t dynamic_window_position_index        = 0;
3016
0
  uint8_t in_unicode_mode                      = 0;
3017
0
  uint8_t unicode_character_set                = 0;
3018
3019
0
  if( scsu_stream == NULL )
3020
0
  {
3021
0
    libcerror_error_set(
3022
0
     error,
3023
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3024
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3025
0
     "%s: invalid SCSU stream.",
3026
0
     function );
3027
3028
0
    return( -1 );
3029
0
  }
3030
0
  if( scsu_stream_size > (size_t) SSIZE_MAX )
3031
0
  {
3032
0
    libcerror_error_set(
3033
0
     error,
3034
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3035
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3036
0
     "%s: invalid SCSU stream size value exceeds maximum.",
3037
0
     function );
3038
3039
0
    return( -1 );
3040
0
  }
3041
0
  if( utf32_string_size == NULL )
3042
0
  {
3043
0
    libcerror_error_set(
3044
0
     error,
3045
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3046
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3047
0
     "%s: invalid UTF-32 string size.",
3048
0
     function );
3049
3050
0
    return( -1 );
3051
0
  }
3052
0
  *utf32_string_size = 0;
3053
3054
0
  if( scsu_stream_size == 0 )
3055
0
  {
3056
0
    return( 1 );
3057
0
  }
3058
0
  while( scsu_stream_index < scsu_stream_size )
3059
0
  {
3060
0
    unicode_character_set = 0;
3061
3062
0
    if( scsu_stream_index >= scsu_stream_size )
3063
0
    {
3064
0
      libcerror_error_set(
3065
0
       error,
3066
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3067
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3068
0
       "%s: SCSU stream too small.",
3069
0
       function );
3070
3071
0
      return( -1 );
3072
0
    }
3073
0
    byte_value1 = scsu_stream[ scsu_stream_index++ ];
3074
3075
0
    if( in_unicode_mode != 0 )
3076
0
    {
3077
0
      if( ( byte_value1 <= 0xdf )
3078
0
       || ( byte_value1 >= 0xf3 ) )
3079
0
      {
3080
0
        if( scsu_stream_index >= scsu_stream_size )
3081
0
        {
3082
0
          libcerror_error_set(
3083
0
           error,
3084
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3085
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3086
0
           "%s: SCSU stream too small.",
3087
0
           function );
3088
3089
0
          return( -1 );
3090
0
        }
3091
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3092
3093
0
        unicode_character   = byte_value1;
3094
0
        unicode_character <<= 8;
3095
0
        unicode_character  |= byte_value2;
3096
3097
0
        unicode_character_set = 1;
3098
0
      }
3099
      /* UCn tags
3100
       */
3101
0
      else if( ( byte_value1 >= 0xe0 )
3102
0
            && ( byte_value1 <= 0xe7 ) )
3103
0
      {
3104
0
        dynamic_window_position_index = byte_value1 - 0xe0;
3105
3106
0
        in_unicode_mode = 0;
3107
0
      }
3108
      /* UDn tags
3109
       */
3110
0
      else if( ( byte_value1 >= 0xe8 )
3111
0
            && ( byte_value1 <= 0xef ) )
3112
0
      {
3113
0
        if( scsu_stream_index >= scsu_stream_size )
3114
0
        {
3115
0
          libcerror_error_set(
3116
0
           error,
3117
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3118
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3119
0
           "%s: SCSU stream too small.",
3120
0
           function );
3121
3122
0
          return( -1 );
3123
0
        }
3124
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3125
3126
0
        dynamic_window_position_index = byte_value1 - 0xe8;
3127
0
        scsu_window_position          = libuna_scsu_window_offset_table[ byte_value2 ];
3128
3129
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3130
3131
0
        in_unicode_mode = 0;
3132
0
      }
3133
      /* UQU tag
3134
       */
3135
0
      else if( byte_value1 == 0xf0 )
3136
0
      {
3137
0
        if( ( scsu_stream_size < 2 )
3138
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3139
0
        {
3140
0
          libcerror_error_set(
3141
0
           error,
3142
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3143
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3144
0
           "%s: SCSU stream too small.",
3145
0
           function );
3146
3147
0
          return( -1 );
3148
0
        }
3149
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3150
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3151
3152
0
        unicode_character   = byte_value2;
3153
0
        unicode_character <<= 8;
3154
0
        unicode_character  |= byte_value3;
3155
3156
0
        unicode_character_set = 1;
3157
0
      }
3158
      /* UDX tag
3159
       */
3160
0
      else if( byte_value1 == 0xf1 )
3161
0
      {
3162
0
        if( ( scsu_stream_size < 2 )
3163
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3164
0
        {
3165
0
          libcerror_error_set(
3166
0
           error,
3167
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3168
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3169
0
           "%s: SCSU stream too small.",
3170
0
           function );
3171
3172
0
          return( -1 );
3173
0
        }
3174
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3175
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3176
3177
0
        dynamic_window_position_index = byte_value2 >> 5;
3178
0
        scsu_window_position          = byte_value2 & 0x1f;
3179
0
        scsu_window_position        <<= 8;
3180
0
        scsu_window_position         |= byte_value3;
3181
0
        scsu_window_position        <<= 7;
3182
0
        scsu_window_position         += 0x00010000UL;
3183
3184
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3185
3186
0
        in_unicode_mode = 0;
3187
0
      }
3188
0
    }
3189
0
    else
3190
0
    {
3191
0
      if( ( byte_value1 == 0x00 )
3192
0
       || ( byte_value1 == 0x09 )
3193
0
       || ( byte_value1 == 0x0a )
3194
0
       || ( byte_value1 == 0x0c )
3195
0
       || ( byte_value1 == 0x0d )
3196
0
       || ( ( byte_value1 >= 0x20 )
3197
0
        &&  ( byte_value1 <= 0x7f ) ) )
3198
0
      {
3199
0
        unicode_character = byte_value1;
3200
3201
0
        unicode_character_set = 1;
3202
0
      }
3203
      /* SQn tags
3204
       */
3205
0
      else if( ( byte_value1 >= 0x01 )
3206
0
            && ( byte_value1 <= 0x08 ) )
3207
0
      {
3208
0
        if( scsu_stream_index >= scsu_stream_size )
3209
0
        {
3210
0
          libcerror_error_set(
3211
0
           error,
3212
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3213
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3214
0
           "%s: SCSU stream too small.",
3215
0
           function );
3216
3217
0
          return( -1 );
3218
0
        }
3219
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3220
3221
0
        unicode_character = byte_value2;
3222
3223
0
        if( byte_value2 < 0x80 )
3224
0
        {
3225
0
          unicode_character += libuna_scsu_static_window_positions[ byte_value1 - 0x01 ];
3226
0
        }
3227
0
        else
3228
0
        {
3229
0
          unicode_character -= 0x80;
3230
0
          unicode_character += scsu_dynamic_window_positions[ byte_value1 - 0x01 ];
3231
0
        }
3232
0
        unicode_character_set = 1;
3233
0
      }
3234
      /* SDX tag
3235
       */
3236
0
      else if( byte_value1 == 0x0b )
3237
0
      {
3238
0
        if( ( scsu_stream_size < 2 )
3239
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3240
0
        {
3241
0
          libcerror_error_set(
3242
0
           error,
3243
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3244
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3245
0
           "%s: SCSU stream too small.",
3246
0
           function );
3247
3248
0
          return( -1 );
3249
0
        }
3250
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3251
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3252
3253
0
        dynamic_window_position_index = byte_value2 >> 5;
3254
0
        scsu_window_position          = byte_value2 & 0x1f;
3255
0
        scsu_window_position        <<= 8;
3256
0
        scsu_window_position         |= byte_value3;
3257
0
        scsu_window_position        <<= 7;
3258
0
        scsu_window_position         += 0x00010000UL;
3259
3260
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3261
0
      }
3262
      /* SQU tag
3263
       */
3264
0
      else if( byte_value1 == 0x0e )
3265
0
      {
3266
0
        if( ( scsu_stream_size < 2 )
3267
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3268
0
        {
3269
0
          libcerror_error_set(
3270
0
           error,
3271
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3272
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3273
0
           "%s: SCSU stream too small.",
3274
0
           function );
3275
3276
0
          return( -1 );
3277
0
        }
3278
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3279
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3280
3281
0
        unicode_character   = byte_value2;
3282
0
        unicode_character <<= 8;
3283
0
        unicode_character  |= byte_value3;
3284
3285
0
        unicode_character_set = 1;
3286
0
      }
3287
      /* SCU tag
3288
       */
3289
0
      else if( byte_value1 == 0x0f )
3290
0
      {
3291
0
        in_unicode_mode = 1;
3292
0
      }
3293
      /* SCn tags
3294
       */
3295
0
      else if( ( byte_value1 >= 0x10 )
3296
0
            && ( byte_value1 <= 0x17 ) )
3297
0
      {
3298
0
        dynamic_window_position_index = byte_value1 - 0x10;
3299
0
      }
3300
      /* SDn tags
3301
       */
3302
0
      else if( ( byte_value1 >= 0x18 )
3303
0
            && ( byte_value1 <= 0x1f ) )
3304
0
      {
3305
0
        if( scsu_stream_index >= scsu_stream_size )
3306
0
        {
3307
0
          libcerror_error_set(
3308
0
           error,
3309
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3310
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3311
0
           "%s: SCSU stream too small.",
3312
0
           function );
3313
3314
0
          return( -1 );
3315
0
        }
3316
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3317
3318
0
        dynamic_window_position_index = byte_value1 - 0x18;
3319
0
        scsu_window_position          = libuna_scsu_window_offset_table[ byte_value2 ];
3320
3321
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3322
0
      }
3323
0
      else if( byte_value1 >= 0x80 )
3324
0
      {
3325
0
        unicode_character  = byte_value1 - 0x80;
3326
0
        unicode_character += scsu_dynamic_window_positions[ dynamic_window_position_index ];
3327
3328
0
        unicode_character_set = 1;
3329
0
      }
3330
0
    }
3331
0
    if( unicode_character_set != 0 )
3332
0
    {
3333
      /* Determine how many UTF-32 character bytes are required
3334
       */
3335
0
      if( libuna_unicode_character_size_to_utf32(
3336
0
           unicode_character,
3337
0
           utf32_string_size,
3338
0
           error ) != 1 )
3339
0
      {
3340
0
        libcerror_error_set(
3341
0
         error,
3342
0
         LIBCERROR_ERROR_DOMAIN_CONVERSION,
3343
0
         LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
3344
0
         "%s: unable to unable to determine size of Unicode character in UTF-32.",
3345
0
         function );
3346
3347
0
        return( -1 );
3348
0
      }
3349
0
      if( unicode_character == 0 )
3350
0
      {
3351
0
        break;
3352
0
      }
3353
0
    }
3354
0
  }
3355
  /* Check if the string is terminated with an end-of-string character
3356
   */
3357
0
  if( unicode_character != 0 )
3358
0
  {
3359
0
    *utf32_string_size += 1;
3360
0
  }
3361
0
  return( 1 );
3362
0
}
3363
3364
/* Copies an UTF-32 string from a Standard Compression Scheme for Unicode (SCSU) stream
3365
 * Returns 1 if successful or -1 on error
3366
 */
3367
int libuna_utf32_string_copy_from_scsu_stream(
3368
     libuna_utf32_character_t *utf32_string,
3369
     size_t utf32_string_size,
3370
     const uint8_t *scsu_stream,
3371
     size_t scsu_stream_size,
3372
     libcerror_error_t **error )
3373
0
{
3374
0
  static char *function     = "libuna_utf32_string_copy_from_scsu_stream";
3375
0
  size_t utf32_string_index = 0;
3376
3377
0
  if( libuna_utf32_string_with_index_copy_from_scsu_stream(
3378
0
       utf32_string,
3379
0
       utf32_string_size,
3380
0
       &utf32_string_index,
3381
0
       scsu_stream,
3382
0
       scsu_stream_size,
3383
0
       error ) != 1 )
3384
0
  {
3385
0
    libcerror_error_set(
3386
0
     error,
3387
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3388
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
3389
0
     "%s: unable to SCSU stream to UTF-32 string.",
3390
0
     function );
3391
3392
0
    return( -1 );
3393
0
  }
3394
0
  return( 1 );
3395
0
}
3396
3397
/* Copies an UTF-32 string from a Standard Compression Scheme for Unicode (SCSU) stream
3398
 * Returns 1 if successful or -1 on error
3399
 */
3400
int libuna_utf32_string_with_index_copy_from_scsu_stream(
3401
     libuna_utf32_character_t *utf32_string,
3402
     size_t utf32_string_size,
3403
     size_t *utf32_string_index,
3404
     const uint8_t *scsu_stream,
3405
     size_t scsu_stream_size,
3406
     libcerror_error_t **error )
3407
0
{
3408
0
  uint32_t scsu_dynamic_window_positions[ 8 ] = {
3409
0
    0x0080, 0x00c0, 0x0400, 0x0600, 0x0900, 0x3040, 0x30a0, 0xff00 };
3410
3411
0
  static char *function                        = "libuna_utf32_string_with_index_copy_from_scsu_stream";
3412
0
  libuna_unicode_character_t unicode_character = 0;
3413
0
  size_t scsu_stream_index                     = 0;
3414
0
  uint32_t scsu_window_position                = 0;
3415
0
  uint8_t byte_value1                          = 0;
3416
0
  uint8_t byte_value2                          = 0;
3417
0
  uint8_t byte_value3                          = 0;
3418
0
  uint8_t dynamic_window_position_index        = 0;
3419
0
  uint8_t in_unicode_mode                      = 0;
3420
0
  uint8_t unicode_character_set                = 0;
3421
3422
0
  if( utf32_string == NULL )
3423
0
  {
3424
0
    libcerror_error_set(
3425
0
     error,
3426
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3427
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3428
0
     "%s: invalid UTF-32 string.",
3429
0
     function );
3430
3431
0
    return( -1 );
3432
0
  }
3433
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
3434
0
  {
3435
0
    libcerror_error_set(
3436
0
     error,
3437
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3438
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3439
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
3440
0
     function );
3441
3442
0
    return( -1 );
3443
0
  }
3444
0
  if( utf32_string_index == NULL )
3445
0
  {
3446
0
    libcerror_error_set(
3447
0
     error,
3448
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3449
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3450
0
     "%s: invalid UTF-32 string index.",
3451
0
     function );
3452
3453
0
    return( -1 );
3454
0
  }
3455
0
  if( scsu_stream == NULL )
3456
0
  {
3457
0
    libcerror_error_set(
3458
0
     error,
3459
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3460
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3461
0
     "%s: invalid SCSU stream.",
3462
0
     function );
3463
3464
0
    return( -1 );
3465
0
  }
3466
0
  if( scsu_stream_size > (size_t) SSIZE_MAX )
3467
0
  {
3468
0
    libcerror_error_set(
3469
0
     error,
3470
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3471
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3472
0
     "%s: invalid SCSU stream size value exceeds maximum.",
3473
0
     function );
3474
3475
0
    return( -1 );
3476
0
  }
3477
0
  if( scsu_stream_size == 0 )
3478
0
  {
3479
0
    libcerror_error_set(
3480
0
     error,
3481
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3482
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
3483
0
     "%s: missing SCSU stream value.",
3484
0
     function );
3485
3486
0
    return( -1 );
3487
0
  }
3488
0
  while( scsu_stream_index < scsu_stream_size )
3489
0
  {
3490
0
    unicode_character_set = 0;
3491
3492
0
    if( scsu_stream_index >= scsu_stream_size )
3493
0
    {
3494
0
      libcerror_error_set(
3495
0
       error,
3496
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3497
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3498
0
       "%s: SCSU stream too small.",
3499
0
       function );
3500
3501
0
      return( -1 );
3502
0
    }
3503
0
    byte_value1 = scsu_stream[ scsu_stream_index++ ];
3504
3505
0
    if( in_unicode_mode != 0 )
3506
0
    {
3507
0
      if( ( byte_value1 <= 0xdf )
3508
0
       || ( byte_value1 >= 0xf3 ) )
3509
0
      {
3510
0
        if( scsu_stream_index >= scsu_stream_size )
3511
0
        {
3512
0
          libcerror_error_set(
3513
0
           error,
3514
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3515
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3516
0
           "%s: SCSU stream too small.",
3517
0
           function );
3518
3519
0
          return( -1 );
3520
0
        }
3521
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3522
3523
0
        unicode_character   = byte_value1;
3524
0
        unicode_character <<= 8;
3525
0
        unicode_character  |= byte_value2;
3526
3527
0
        unicode_character_set = 1;
3528
0
      }
3529
      /* UCn tags
3530
       */
3531
0
      else if( ( byte_value1 >= 0xe0 )
3532
0
            && ( byte_value1 <= 0xe7 ) )
3533
0
      {
3534
0
        dynamic_window_position_index = byte_value1 - 0xe0;
3535
3536
0
        in_unicode_mode = 0;
3537
0
      }
3538
      /* UDn tags
3539
       */
3540
0
      else if( ( byte_value1 >= 0xe8 )
3541
0
            && ( byte_value1 <= 0xef ) )
3542
0
      {
3543
0
        if( scsu_stream_index >= scsu_stream_size )
3544
0
        {
3545
0
          libcerror_error_set(
3546
0
           error,
3547
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3548
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3549
0
           "%s: SCSU stream too small.",
3550
0
           function );
3551
3552
0
          return( -1 );
3553
0
        }
3554
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3555
3556
0
        dynamic_window_position_index = byte_value1 - 0xe8;
3557
0
        scsu_window_position          = libuna_scsu_window_offset_table[ byte_value2 ];
3558
3559
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3560
3561
0
        in_unicode_mode = 0;
3562
0
      }
3563
      /* UQU tag
3564
       */
3565
0
      else if( byte_value1 == 0xf0 )
3566
0
      {
3567
0
        if( ( scsu_stream_size < 2 )
3568
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3569
0
        {
3570
0
          libcerror_error_set(
3571
0
           error,
3572
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3573
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3574
0
           "%s: SCSU stream too small.",
3575
0
           function );
3576
3577
0
          return( -1 );
3578
0
        }
3579
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3580
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3581
3582
0
        unicode_character   = byte_value2;
3583
0
        unicode_character <<= 8;
3584
0
        unicode_character  |= byte_value3;
3585
3586
0
        unicode_character_set = 1;
3587
0
      }
3588
      /* UDX tag
3589
       */
3590
0
      else if( byte_value1 == 0xf1 )
3591
0
      {
3592
0
        if( ( scsu_stream_size < 2 )
3593
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3594
0
        {
3595
0
          libcerror_error_set(
3596
0
           error,
3597
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3598
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3599
0
           "%s: SCSU stream too small.",
3600
0
           function );
3601
3602
0
          return( -1 );
3603
0
        }
3604
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3605
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3606
3607
0
        dynamic_window_position_index = byte_value2 >> 5;
3608
0
        scsu_window_position          = byte_value2 & 0x1f;
3609
0
        scsu_window_position        <<= 8;
3610
0
        scsu_window_position         |= byte_value3;
3611
0
        scsu_window_position        <<= 7;
3612
0
        scsu_window_position         += 0x00010000UL;
3613
3614
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3615
3616
0
        in_unicode_mode = 0;
3617
0
      }
3618
0
    }
3619
0
    else
3620
0
    {
3621
0
      if( ( byte_value1 == 0x00 )
3622
0
       || ( byte_value1 == 0x09 )
3623
0
       || ( byte_value1 == 0x0a )
3624
0
       || ( byte_value1 == 0x0c )
3625
0
       || ( byte_value1 == 0x0d )
3626
0
       || ( ( byte_value1 >= 0x20 )
3627
0
        &&  ( byte_value1 <= 0x7f ) ) )
3628
0
      {
3629
0
        unicode_character = byte_value1;
3630
3631
0
        unicode_character_set = 1;
3632
0
      }
3633
      /* SQn tags
3634
       */
3635
0
      else if( ( byte_value1 >= 0x01 )
3636
0
            && ( byte_value1 <= 0x08 ) )
3637
0
      {
3638
0
        if( scsu_stream_index >= scsu_stream_size )
3639
0
        {
3640
0
          libcerror_error_set(
3641
0
           error,
3642
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3643
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3644
0
           "%s: SCSU stream too small.",
3645
0
           function );
3646
3647
0
          return( -1 );
3648
0
        }
3649
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3650
3651
0
        unicode_character = byte_value2;
3652
3653
0
        if( byte_value2 < 0x80 )
3654
0
        {
3655
0
          unicode_character += libuna_scsu_static_window_positions[ byte_value1 - 0x01 ];
3656
0
        }
3657
0
        else
3658
0
        {
3659
0
          unicode_character -= 0x80;
3660
0
          unicode_character += scsu_dynamic_window_positions[ byte_value1 - 0x01 ];
3661
0
        }
3662
0
        unicode_character_set = 1;
3663
0
      }
3664
      /* SDX tag
3665
       */
3666
0
      else if( byte_value1 == 0x0b )
3667
0
      {
3668
0
        if( ( scsu_stream_size < 2 )
3669
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3670
0
        {
3671
0
          libcerror_error_set(
3672
0
           error,
3673
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3674
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3675
0
           "%s: SCSU stream too small.",
3676
0
           function );
3677
3678
0
          return( -1 );
3679
0
        }
3680
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3681
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3682
3683
0
        dynamic_window_position_index = byte_value2 >> 5;
3684
0
        scsu_window_position          = byte_value2 & 0x1f;
3685
0
        scsu_window_position        <<= 8;
3686
0
        scsu_window_position         |= byte_value3;
3687
0
        scsu_window_position        <<= 7;
3688
0
        scsu_window_position         += 0x00010000UL;
3689
3690
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3691
0
      }
3692
      /* SQU tag
3693
       */
3694
0
      else if( byte_value1 == 0x0e )
3695
0
      {
3696
0
        if( ( scsu_stream_size < 2 )
3697
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3698
0
        {
3699
0
          libcerror_error_set(
3700
0
           error,
3701
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3702
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3703
0
           "%s: SCSU stream too small.",
3704
0
           function );
3705
3706
0
          return( -1 );
3707
0
        }
3708
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3709
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3710
3711
0
        unicode_character   = byte_value2;
3712
0
        unicode_character <<= 8;
3713
0
        unicode_character  |= byte_value3;
3714
3715
0
        unicode_character_set = 1;
3716
0
      }
3717
      /* SCU tag
3718
       */
3719
0
      else if( byte_value1 == 0x0f )
3720
0
      {
3721
0
        in_unicode_mode = 1;
3722
0
      }
3723
      /* SCn tags
3724
       */
3725
0
      else if( ( byte_value1 >= 0x10 )
3726
0
            && ( byte_value1 <= 0x17 ) )
3727
0
      {
3728
0
        dynamic_window_position_index = byte_value1 - 0x10;
3729
0
      }
3730
      /* SDn tags
3731
       */
3732
0
      else if( ( byte_value1 >= 0x18 )
3733
0
            && ( byte_value1 <= 0x1f ) )
3734
0
      {
3735
0
        if( scsu_stream_index >= scsu_stream_size )
3736
0
        {
3737
0
          libcerror_error_set(
3738
0
           error,
3739
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3740
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3741
0
           "%s: SCSU stream too small.",
3742
0
           function );
3743
3744
0
          return( -1 );
3745
0
        }
3746
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3747
3748
0
        dynamic_window_position_index = byte_value1 - 0x18;
3749
0
        scsu_window_position          = libuna_scsu_window_offset_table[ byte_value2 ];
3750
3751
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3752
0
      }
3753
0
      else if( byte_value1 >= 0x80 )
3754
0
      {
3755
0
        unicode_character  = byte_value1 - 0x80;
3756
0
        unicode_character += scsu_dynamic_window_positions[ dynamic_window_position_index ];
3757
3758
0
        unicode_character_set = 1;
3759
0
      }
3760
0
    }
3761
0
    if( unicode_character_set != 0 )
3762
0
    {
3763
      /* Convert the Unicode character into UTF-32 character bytes
3764
       */
3765
0
      if( libuna_unicode_character_copy_to_utf32(
3766
0
           unicode_character,
3767
0
           utf32_string,
3768
0
           utf32_string_size,
3769
0
           utf32_string_index,
3770
0
           error ) != 1 )
3771
0
      {
3772
0
        libcerror_error_set(
3773
0
         error,
3774
0
         LIBCERROR_ERROR_DOMAIN_CONVERSION,
3775
0
         LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
3776
0
         "%s: unable to copy Unicode character to UTF-32.",
3777
0
         function );
3778
3779
0
        return( -1 );
3780
0
      }
3781
0
      if( unicode_character == 0 )
3782
0
      {
3783
0
        break;
3784
0
      }
3785
0
    }
3786
0
  }
3787
  /* Check if the string is terminated with an end-of-string character
3788
   */
3789
0
  if( unicode_character != 0 )
3790
0
  {
3791
0
    if( *utf32_string_index >= utf32_string_size )
3792
0
    {
3793
0
      libcerror_error_set(
3794
0
       error,
3795
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3796
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3797
0
       "%s: UTF-32 string too small.",
3798
0
       function );
3799
3800
0
      return( -1 );
3801
0
    }
3802
0
    utf32_string[ *utf32_string_index ] = 0;
3803
3804
0
    *utf32_string_index += 1;
3805
0
  }
3806
0
  return( 1 );
3807
0
}
3808