Coverage Report

Created: 2023-06-07 06:53

/src/libscca/libuna/libuna_utf32_string.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * UTF-32 string functions
3
 *
4
 * Copyright (C) 2008-2022, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <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
  size_t utf16_stream_index                    = 0;
1928
0
  libuna_unicode_character_t unicode_character = 0;
1929
0
  int read_byte_order                          = 0;
1930
1931
0
  if( utf16_stream == NULL )
1932
0
  {
1933
0
    libcerror_error_set(
1934
0
     error,
1935
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1936
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1937
0
     "%s: invalid UTF-16 stream.",
1938
0
     function );
1939
1940
0
    return( -1 );
1941
0
  }
1942
0
  if( utf16_stream_size > (size_t) SSIZE_MAX )
1943
0
  {
1944
0
    libcerror_error_set(
1945
0
     error,
1946
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1947
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1948
0
     "%s: invalid UTF-16 stream size value exceeds maximum.",
1949
0
     function );
1950
1951
0
    return( -1 );
1952
0
  }
1953
0
  if( ( utf16_stream_size % 2 ) != 0 )
1954
0
  {
1955
0
    libcerror_error_set(
1956
0
     error,
1957
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1958
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1959
0
     "%s: missing UTF-16 stream value.",
1960
0
     function );
1961
1962
0
    return( -1 );
1963
0
  }
1964
0
  if( utf32_string_size == NULL )
1965
0
  {
1966
0
    libcerror_error_set(
1967
0
     error,
1968
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1969
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1970
0
     "%s: invalid UTF-32 string size.",
1971
0
     function );
1972
1973
0
    return( -1 );
1974
0
  }
1975
0
  *utf32_string_size = 0;
1976
1977
0
  if( utf16_stream_size == 0 )
1978
0
  {
1979
0
    return( 1 );
1980
0
  }
1981
  /* Check if UTF-16 stream is in big or little endian
1982
   */
1983
0
  if( utf16_stream_size >= 2 )
1984
0
  {
1985
0
    if( ( utf16_stream[ 0 ] == 0x0ff )
1986
0
     && ( utf16_stream[ 1 ] == 0x0fe ) )
1987
0
    {
1988
0
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
1989
0
      utf16_stream_index = 2;
1990
0
    }
1991
0
    else if( ( utf16_stream[ 0 ] == 0x0fe )
1992
0
          && ( utf16_stream[ 1 ] == 0x0ff ) )
1993
0
    {
1994
0
      read_byte_order    = LIBUNA_ENDIAN_BIG;
1995
0
      utf16_stream_index = 2;
1996
0
    }
1997
0
    if( byte_order == 0 )
1998
0
    {
1999
0
      byte_order = read_byte_order;
2000
0
    }
2001
0
  }
2002
0
  if( ( byte_order != LIBUNA_ENDIAN_BIG )
2003
0
   && ( byte_order != LIBUNA_ENDIAN_LITTLE ) )
2004
0
  {
2005
0
    libcerror_error_set(
2006
0
     error,
2007
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2008
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2009
0
     "%s: unsupported byte order.",
2010
0
     function );
2011
2012
0
    return( -1 );
2013
0
  }
2014
0
  while( ( utf16_stream_index + 1 ) < utf16_stream_size )
2015
0
  {
2016
    /* Convert the UTF-16 stream bytes into an Unicode character
2017
     */
2018
0
    if( libuna_unicode_character_copy_from_utf16_stream(
2019
0
         &unicode_character,
2020
0
         utf16_stream,
2021
0
         utf16_stream_size,
2022
0
         &utf16_stream_index,
2023
0
         byte_order,
2024
0
         error ) != 1 )
2025
0
    {
2026
0
      libcerror_error_set(
2027
0
       error,
2028
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2029
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2030
0
       "%s: unable to copy Unicode character from UTF-16 stream.",
2031
0
       function );
2032
2033
0
      return( -1 );
2034
0
    }
2035
    /* Determine how many UTF-8 character bytes are required
2036
     */
2037
0
    if( libuna_unicode_character_size_to_utf32(
2038
0
         unicode_character,
2039
0
         utf32_string_size,
2040
0
         error ) != 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
  size_t utf16_stream_index                    = 0;
2114
0
  libuna_unicode_character_t unicode_character = 0;
2115
0
  int read_byte_order                          = 0;
2116
2117
0
  if( utf32_string == NULL )
2118
0
  {
2119
0
    libcerror_error_set(
2120
0
     error,
2121
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2122
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2123
0
     "%s: invalid UTF-32 string.",
2124
0
     function );
2125
2126
0
    return( -1 );
2127
0
  }
2128
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
2129
0
  {
2130
0
    libcerror_error_set(
2131
0
     error,
2132
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2133
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2134
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
2135
0
     function );
2136
2137
0
    return( -1 );
2138
0
  }
2139
0
  if( utf32_string_index == NULL )
2140
0
  {
2141
0
    libcerror_error_set(
2142
0
     error,
2143
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2144
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2145
0
     "%s: invalid UTF-32 string index.",
2146
0
     function );
2147
2148
0
    return( -1 );
2149
0
  }
2150
0
  if( utf16_stream == NULL )
2151
0
  {
2152
0
    libcerror_error_set(
2153
0
     error,
2154
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2155
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2156
0
     "%s: invalid UTF-16 stream.",
2157
0
     function );
2158
2159
0
    return( -1 );
2160
0
  }
2161
0
  if( utf16_stream_size > (size_t) SSIZE_MAX )
2162
0
  {
2163
0
    libcerror_error_set(
2164
0
     error,
2165
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2166
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2167
0
     "%s: invalid UTF-16 stream size value exceeds maximum.",
2168
0
     function );
2169
2170
0
    return( -1 );
2171
0
  }
2172
0
  if( ( utf16_stream_size == 0 )
2173
0
   || ( ( utf16_stream_size % 2 ) != 0 ) )
2174
0
  {
2175
0
    libcerror_error_set(
2176
0
     error,
2177
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2178
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2179
0
     "%s: missing UTF-16 stream value.",
2180
0
     function );
2181
2182
0
    return( -1 );
2183
0
  }
2184
  /* Check if UTF-16 stream is in big or little endian
2185
   */
2186
0
  if( utf16_stream_size >= 2 )
2187
0
  {
2188
0
    if( ( utf16_stream[ 0 ] == 0x0ff )
2189
0
     && ( utf16_stream[ 1 ] == 0x0fe ) )
2190
0
    {
2191
0
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
2192
0
      utf16_stream_index = 2;
2193
0
    }
2194
0
    else if( ( utf16_stream[ 0 ] == 0x0fe )
2195
0
          && ( utf16_stream[ 1 ] == 0x0ff ) )
2196
0
    {
2197
0
      read_byte_order    = LIBUNA_ENDIAN_BIG;
2198
0
      utf16_stream_index = 2;
2199
0
    }
2200
0
    if( byte_order == 0 )
2201
0
    {
2202
0
      byte_order = read_byte_order;
2203
0
    }
2204
0
  }
2205
0
  if( ( byte_order != LIBUNA_ENDIAN_BIG )
2206
0
   && ( byte_order != LIBUNA_ENDIAN_LITTLE ) )
2207
0
  {
2208
0
    libcerror_error_set(
2209
0
     error,
2210
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2211
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2212
0
     "%s: unsupported byte order.",
2213
0
     function );
2214
2215
0
    return( -1 );
2216
0
  }
2217
0
  while( ( utf16_stream_index + 1 ) < utf16_stream_size )
2218
0
  {
2219
    /* Convert the UTF-16 stream bytes into an Unicode character
2220
     */
2221
0
    if( libuna_unicode_character_copy_from_utf16_stream(
2222
0
         &unicode_character,
2223
0
         utf16_stream,
2224
0
         utf16_stream_size,
2225
0
         &utf16_stream_index,
2226
0
         byte_order,
2227
0
         error ) != 1 )
2228
0
    {
2229
0
      libcerror_error_set(
2230
0
       error,
2231
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2232
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2233
0
       "%s: unable to copy Unicode character from UTF-16 stream.",
2234
0
       function );
2235
2236
0
      return( -1 );
2237
0
    }
2238
    /* Convert the Unicode character into UTF-32 character bytes
2239
     */
2240
0
    if( libuna_unicode_character_copy_to_utf32(
2241
0
         unicode_character,
2242
0
         utf32_string,
2243
0
         utf32_string_size,
2244
0
         utf32_string_index,
2245
0
         error ) != 1 )
2246
0
    {
2247
0
      libcerror_error_set(
2248
0
       error,
2249
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2250
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
2251
0
       "%s: unable to copy Unicode character to UTF-32.",
2252
0
       function );
2253
2254
0
      return( -1 );
2255
0
    }
2256
0
    if( unicode_character == 0 )
2257
0
    {
2258
0
      break;
2259
0
    }
2260
0
  }
2261
  /* Check if the string is terminated with an end-of-string character
2262
   */
2263
0
  if( unicode_character != 0 )
2264
0
  {
2265
0
    if( *utf32_string_index >= utf32_string_size )
2266
0
    {
2267
0
      libcerror_error_set(
2268
0
       error,
2269
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2270
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2271
0
       "%s: UTF-32 string too small.",
2272
0
       function );
2273
2274
0
      return( -1 );
2275
0
    }
2276
0
    utf32_string[ *utf32_string_index ] = 0;
2277
2278
0
    *utf32_string_index += 1;
2279
0
  }
2280
0
  return( 1 );
2281
0
}
2282
2283
/* Compares an UTF-32 string with an UTF-16 stream
2284
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
2285
 */
2286
int libuna_utf32_string_compare_with_utf16_stream(
2287
     const libuna_utf32_character_t *utf32_string,
2288
     size_t utf32_string_size,
2289
     const uint8_t *utf16_stream,
2290
     size_t utf16_stream_size,
2291
     int byte_order,
2292
     libcerror_error_t **error )
2293
0
{
2294
0
  static char *function                                     = "libuna_utf32_string_compare_with_utf16_stream";
2295
0
  size_t utf16_stream_index                                 = 0;
2296
0
  size_t utf32_string_index                                 = 0;
2297
0
  libuna_unicode_character_t utf32_unicode_character        = 0;
2298
0
  libuna_unicode_character_t utf16_stream_unicode_character = 0;
2299
0
  int read_byte_order                                       = 0;
2300
2301
0
  if( utf32_string == NULL )
2302
0
  {
2303
0
    libcerror_error_set(
2304
0
     error,
2305
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2306
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2307
0
     "%s: invalid UTF-32 string.",
2308
0
     function );
2309
2310
0
    return( -1 );
2311
0
  }
2312
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
2313
0
  {
2314
0
    libcerror_error_set(
2315
0
     error,
2316
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2317
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2318
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
2319
0
     function );
2320
2321
0
    return( -1 );
2322
0
  }
2323
0
  if( utf16_stream == NULL )
2324
0
  {
2325
0
    libcerror_error_set(
2326
0
     error,
2327
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2328
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2329
0
     "%s: invalid UTF-16 stream.",
2330
0
     function );
2331
2332
0
    return( -1 );
2333
0
  }
2334
0
  if( utf16_stream_size > (size_t) SSIZE_MAX )
2335
0
  {
2336
0
    libcerror_error_set(
2337
0
     error,
2338
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2339
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2340
0
     "%s: invalid UTF-16 stream size value exceeds maximum.",
2341
0
     function );
2342
2343
0
    return( -1 );
2344
0
  }
2345
0
  if( ( utf16_stream_size == 0 )
2346
0
   || ( ( utf16_stream_size % 2 ) != 0 ) )
2347
0
  {
2348
0
    libcerror_error_set(
2349
0
     error,
2350
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2351
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2352
0
     "%s: missing UTF-16 stream value.",
2353
0
     function );
2354
2355
0
    return( -1 );
2356
0
  }
2357
  /* Check if UTF-16 stream is in big or little endian
2358
   */
2359
0
  if( utf16_stream_size >= 2 )
2360
0
  {
2361
0
    if( ( utf16_stream[ 0 ] == 0xfe )
2362
0
     && ( utf16_stream[ 1 ] == 0xff ) )
2363
0
    {
2364
0
      read_byte_order    = LIBUNA_ENDIAN_BIG;
2365
0
      utf16_stream_index = 2;
2366
0
    }
2367
0
    else if( ( utf16_stream[ 0 ] == 0xff )
2368
0
          && ( utf16_stream[ 1 ] == 0xfe ) )
2369
0
    {
2370
0
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
2371
0
      utf16_stream_index = 2;
2372
0
    }
2373
0
    if( byte_order == 0 )
2374
0
    {
2375
0
      byte_order = read_byte_order;
2376
0
    }
2377
0
  }
2378
0
  if( ( byte_order != LIBUNA_ENDIAN_BIG )
2379
0
   && ( byte_order != LIBUNA_ENDIAN_LITTLE ) )
2380
0
  {
2381
0
    libcerror_error_set(
2382
0
     error,
2383
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2384
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2385
0
     "%s: unsupported byte order.",
2386
0
     function );
2387
2388
0
    return( -1 );
2389
0
  }
2390
0
  if( ( utf32_string_size >= 1 )
2391
0
   && ( utf32_string[ utf32_string_size - 1 ] == 0 ) )
2392
0
  {
2393
0
    utf32_string_size -= 1;
2394
0
  }
2395
  /* Check if the UTF-16 stream is terminated with zero bytes
2396
   */
2397
0
  if( ( utf16_stream_size >= 2 )
2398
0
   && ( utf16_stream[ utf16_stream_size - 2 ] == 0 )
2399
0
   && ( utf16_stream[ utf16_stream_size - 1 ] == 0 ) )
2400
0
  {
2401
0
    utf16_stream_size -= 2;
2402
0
  }
2403
0
  while( ( utf32_string_index < utf32_string_size )
2404
0
      && ( utf16_stream_index < utf16_stream_size ) )
2405
0
  {
2406
    /* Convert the UTF-32 character bytes into an Unicode character
2407
     */
2408
0
    if( libuna_unicode_character_copy_from_utf32(
2409
0
         &utf32_unicode_character,
2410
0
         utf32_string,
2411
0
         utf32_string_size,
2412
0
         &utf32_string_index,
2413
0
         error ) != 1 )
2414
0
    {
2415
0
      libcerror_error_set(
2416
0
       error,
2417
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2418
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
2419
0
       "%s: unable to copy Unicode character from UTF-32.",
2420
0
       function );
2421
2422
0
      return( -1 );
2423
0
    }
2424
    /* Convert the UTF-16 stream bytes into an Unicode character
2425
     */
2426
0
    if( libuna_unicode_character_copy_from_utf16_stream(
2427
0
         &utf16_stream_unicode_character,
2428
0
         utf16_stream,
2429
0
         utf16_stream_size,
2430
0
         &utf16_stream_index,
2431
0
         byte_order,
2432
0
                     error ) != 1 )
2433
0
    {
2434
0
      libcerror_error_set(
2435
0
       error,
2436
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2437
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2438
0
       "%s: unable to copy Unicode character from UTF-16 stream.",
2439
0
       function );
2440
2441
0
      return( -1 );
2442
0
    }
2443
0
    if( utf32_unicode_character < utf16_stream_unicode_character )
2444
0
    {
2445
0
      return( LIBUNA_COMPARE_LESS );
2446
0
    }
2447
0
    else if( utf32_unicode_character > utf16_stream_unicode_character )
2448
0
    {
2449
0
      return( LIBUNA_COMPARE_GREATER );
2450
0
    }
2451
0
  }
2452
  /* Check if both strings were entirely processed
2453
   */
2454
0
  if( utf32_string_index < utf32_string_size )
2455
0
  {
2456
0
    return( LIBUNA_COMPARE_GREATER );
2457
0
  }
2458
0
  else if( utf16_stream_index < utf16_stream_size )
2459
0
  {
2460
0
    return( LIBUNA_COMPARE_LESS );
2461
0
  }
2462
0
  return( LIBUNA_COMPARE_EQUAL );
2463
0
}
2464
2465
/* Determines the size of an UTF-32 string from an UTF-32 stream
2466
 * Returns 1 if successful or -1 on error
2467
 */
2468
int libuna_utf32_string_size_from_utf32_stream(
2469
     const uint8_t *utf32_stream,
2470
     size_t utf32_stream_size,
2471
     int byte_order,
2472
     size_t *utf32_string_size,
2473
     libcerror_error_t **error )
2474
0
{
2475
0
  static char *function                        = "libuna_utf32_string_size_from_utf32_stream";
2476
0
  size_t utf32_stream_index                    = 0;
2477
0
  libuna_unicode_character_t unicode_character = 0;
2478
0
  int read_byte_order                          = 0;
2479
2480
0
  if( utf32_stream == NULL )
2481
0
  {
2482
0
    libcerror_error_set(
2483
0
     error,
2484
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2485
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2486
0
     "%s: invalid UTF-32 stream.",
2487
0
     function );
2488
2489
0
    return( -1 );
2490
0
  }
2491
0
  if( utf32_stream_size > (size_t) SSIZE_MAX )
2492
0
  {
2493
0
    libcerror_error_set(
2494
0
     error,
2495
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2496
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2497
0
     "%s: invalid UTF-32 stream size value exceeds maximum.",
2498
0
     function );
2499
2500
0
    return( -1 );
2501
0
  }
2502
0
  if( ( utf32_stream_size % 4 ) != 0 )
2503
0
  {
2504
0
    libcerror_error_set(
2505
0
     error,
2506
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2507
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2508
0
     "%s: missing UTF-32 stream value.",
2509
0
     function );
2510
2511
0
    return( -1 );
2512
0
  }
2513
0
  if( utf32_string_size == NULL )
2514
0
  {
2515
0
    libcerror_error_set(
2516
0
     error,
2517
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2518
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2519
0
     "%s: invalid UTF-32 string size.",
2520
0
     function );
2521
2522
0
    return( -1 );
2523
0
  }
2524
0
  *utf32_string_size = 0;
2525
2526
0
  if( utf32_stream_size == 0 )
2527
0
  {
2528
0
    return( 1 );
2529
0
  }
2530
  /* Check if UTF-32 stream is in big or little endian
2531
   */
2532
0
  if( utf32_stream_size >= 4 )
2533
0
  {
2534
0
    if( ( utf32_stream[ 0 ] == 0x00 )
2535
0
     && ( utf32_stream[ 1 ] == 0x00 )
2536
0
     && ( utf32_stream[ 2 ] == 0xfe )
2537
0
     && ( utf32_stream[ 3 ] == 0xff ) )
2538
0
    {
2539
0
      read_byte_order    = LIBUNA_ENDIAN_BIG;
2540
0
      utf32_stream_index = 4;
2541
0
    }
2542
0
    else if( ( utf32_stream[ 0 ] == 0xff )
2543
0
          && ( utf32_stream[ 1 ] == 0xfe )
2544
0
          && ( utf32_stream[ 2 ] == 0x00 )
2545
0
          && ( utf32_stream[ 3 ] == 0x00 ) )
2546
0
    {
2547
0
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
2548
0
      utf32_stream_index = 4;
2549
0
    }
2550
0
    if( byte_order == 0 )
2551
0
    {
2552
0
      byte_order = read_byte_order;
2553
0
    }
2554
0
  }
2555
0
  if( ( byte_order != LIBUNA_ENDIAN_BIG )
2556
0
   && ( byte_order != LIBUNA_ENDIAN_LITTLE ) )
2557
0
  {
2558
0
    libcerror_error_set(
2559
0
     error,
2560
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2561
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2562
0
     "%s: unsupported byte order.",
2563
0
     function );
2564
2565
0
    return( -1 );
2566
0
  }
2567
0
  while( ( utf32_stream_index + 1 ) < utf32_stream_size )
2568
0
  {
2569
    /* Convert the UTF-32 stream bytes into an Unicode character
2570
     */
2571
0
    if( libuna_unicode_character_copy_from_utf32_stream(
2572
0
         &unicode_character,
2573
0
         utf32_stream,
2574
0
         utf32_stream_size,
2575
0
         &utf32_stream_index,
2576
0
         byte_order,
2577
0
         error ) != 1 )
2578
0
    {
2579
0
      libcerror_error_set(
2580
0
       error,
2581
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2582
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2583
0
       "%s: unable to copy Unicode character from UTF-32 stream.",
2584
0
       function );
2585
2586
0
      return( -1 );
2587
0
    }
2588
    /* Determine how many UTF-32 character bytes are required
2589
     */
2590
0
    if( libuna_unicode_character_size_to_utf32(
2591
0
         unicode_character,
2592
0
         utf32_string_size,
2593
0
         error ) != 1 )
2594
0
    {
2595
0
      libcerror_error_set(
2596
0
       error,
2597
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2598
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2599
0
       "%s: unable to unable to determine size of Unicode character in UTF-32.",
2600
0
       function );
2601
2602
0
      return( -1 );
2603
0
    }
2604
0
    if( unicode_character == 0 )
2605
0
    {
2606
0
      break;
2607
0
    }
2608
0
  }
2609
  /* Check if the string is terminated with an end-of-string character
2610
   */
2611
0
  if( unicode_character != 0 )
2612
0
  {
2613
0
    *utf32_string_size += 1;
2614
0
  }
2615
0
  return( 1 );
2616
0
}
2617
2618
/* Copies an UTF-32 string from an UTF-32 stream
2619
 * Returns 1 if successful or -1 on error
2620
 */
2621
int libuna_utf32_string_copy_from_utf32_stream(
2622
     libuna_utf32_character_t *utf32_string,
2623
     size_t utf32_string_size,
2624
     const uint8_t *utf32_stream,
2625
     size_t utf32_stream_size,
2626
     int byte_order,
2627
     libcerror_error_t **error )
2628
0
{
2629
0
  static char *function     = "libuna_utf32_string_copy_from_utf32_stream";
2630
0
  size_t utf32_string_index = 0;
2631
2632
0
  if( libuna_utf32_string_with_index_copy_from_utf32_stream(
2633
0
       utf32_string,
2634
0
       utf32_string_size,
2635
0
       &utf32_string_index,
2636
0
       utf32_stream,
2637
0
       utf32_stream_size,
2638
0
       byte_order,
2639
0
       error ) != 1 )
2640
0
  {
2641
0
    libcerror_error_set(
2642
0
     error,
2643
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2644
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
2645
0
     "%s: unable to copy UTF-32 stream to UTF-32 string.",
2646
0
     function );
2647
2648
0
    return( -1 );
2649
0
  }
2650
0
  return( 1 );
2651
0
}
2652
2653
/* Copies an UTF-32 string from an UTF-32 stream
2654
 * Returns 1 if successful or -1 on error
2655
 */
2656
int libuna_utf32_string_with_index_copy_from_utf32_stream(
2657
     libuna_utf32_character_t *utf32_string,
2658
     size_t utf32_string_size,
2659
     size_t *utf32_string_index,
2660
     const uint8_t *utf32_stream,
2661
     size_t utf32_stream_size,
2662
     int byte_order,
2663
     libcerror_error_t **error )
2664
0
{
2665
0
  static char *function                        = "libuna_utf32_string_with_index_copy_from_utf32_stream";
2666
0
  size_t utf32_stream_index                    = 0;
2667
0
  libuna_unicode_character_t unicode_character = 0;
2668
0
  int read_byte_order                          = 0;
2669
2670
0
  if( utf32_string == NULL )
2671
0
  {
2672
0
    libcerror_error_set(
2673
0
     error,
2674
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2675
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2676
0
     "%s: invalid UTF-32 string.",
2677
0
     function );
2678
2679
0
    return( -1 );
2680
0
  }
2681
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
2682
0
  {
2683
0
    libcerror_error_set(
2684
0
     error,
2685
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2686
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2687
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
2688
0
     function );
2689
2690
0
    return( -1 );
2691
0
  }
2692
0
  if( utf32_string_index == NULL )
2693
0
  {
2694
0
    libcerror_error_set(
2695
0
     error,
2696
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2697
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2698
0
     "%s: invalid UTF-32 string index.",
2699
0
     function );
2700
2701
0
    return( -1 );
2702
0
  }
2703
0
  if( utf32_stream == NULL )
2704
0
  {
2705
0
    libcerror_error_set(
2706
0
     error,
2707
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2708
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2709
0
     "%s: invalid UTF-32 stream.",
2710
0
     function );
2711
2712
0
    return( -1 );
2713
0
  }
2714
0
  if( utf32_stream_size > (size_t) SSIZE_MAX )
2715
0
  {
2716
0
    libcerror_error_set(
2717
0
     error,
2718
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2719
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2720
0
     "%s: invalid UTF-32 stream size value exceeds maximum.",
2721
0
     function );
2722
2723
0
    return( -1 );
2724
0
  }
2725
0
  if( ( utf32_stream_size == 0 )
2726
0
   || ( ( utf32_stream_size % 4 ) != 0 ) )
2727
0
  {
2728
0
    libcerror_error_set(
2729
0
     error,
2730
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2731
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2732
0
     "%s: missing UTF-32 stream value.",
2733
0
     function );
2734
2735
0
    return( -1 );
2736
0
  }
2737
  /* Check if UTF-32 stream is in big or little endian
2738
   */
2739
0
  if( utf32_stream_size >= 4 )
2740
0
  {
2741
0
    if( ( utf32_stream[ 0 ] == 0x00 )
2742
0
     && ( utf32_stream[ 1 ] == 0x00 )
2743
0
     && ( utf32_stream[ 2 ] == 0xfe )
2744
0
     && ( utf32_stream[ 3 ] == 0xff ) )
2745
0
    {
2746
0
      read_byte_order    = LIBUNA_ENDIAN_BIG;
2747
0
      utf32_stream_index = 4;
2748
0
    }
2749
0
    else if( ( utf32_stream[ 0 ] == 0xff )
2750
0
          && ( utf32_stream[ 1 ] == 0xfe )
2751
0
          && ( utf32_stream[ 2 ] == 0x00 )
2752
0
          && ( utf32_stream[ 3 ] == 0x00 ) )
2753
0
    {
2754
0
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
2755
0
      utf32_stream_index = 4;
2756
0
    }
2757
0
    if( byte_order == 0 )
2758
0
    {
2759
0
      byte_order = read_byte_order;
2760
0
    }
2761
0
  }
2762
0
  if( ( byte_order != LIBUNA_ENDIAN_BIG )
2763
0
   && ( byte_order != LIBUNA_ENDIAN_LITTLE ) )
2764
0
  {
2765
0
    libcerror_error_set(
2766
0
     error,
2767
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2768
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2769
0
     "%s: unsupported byte order.",
2770
0
     function );
2771
2772
0
    return( -1 );
2773
0
  }
2774
0
  while( ( utf32_stream_index + 1 ) < utf32_stream_size )
2775
0
  {
2776
    /* Convert the UTF-32 stream bytes into an Unicode character
2777
     */
2778
0
    if( libuna_unicode_character_copy_from_utf32_stream(
2779
0
         &unicode_character,
2780
0
         utf32_stream,
2781
0
         utf32_stream_size,
2782
0
         &utf32_stream_index,
2783
0
         byte_order,
2784
0
         error ) != 1 )
2785
0
    {
2786
0
      libcerror_error_set(
2787
0
       error,
2788
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2789
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2790
0
       "%s: unable to copy Unicode character from UTF-32 stream.",
2791
0
       function );
2792
2793
0
      return( -1 );
2794
0
    }
2795
    /* Convert the Unicode character into UTF-32 character bytes
2796
     */
2797
0
    if( libuna_unicode_character_copy_to_utf32(
2798
0
         unicode_character,
2799
0
         utf32_string,
2800
0
         utf32_string_size,
2801
0
         utf32_string_index,
2802
0
         error ) != 1 )
2803
0
    {
2804
0
      libcerror_error_set(
2805
0
       error,
2806
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2807
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
2808
0
       "%s: unable to copy Unicode character to UTF-32.",
2809
0
       function );
2810
2811
0
      return( -1 );
2812
0
    }
2813
0
    if( unicode_character == 0 )
2814
0
    {
2815
0
      break;
2816
0
    }
2817
0
  }
2818
  /* Check if the string is terminated with an end-of-string character
2819
   */
2820
0
  if( unicode_character != 0 )
2821
0
  {
2822
0
    if( *utf32_string_index >= utf32_string_size )
2823
0
    {
2824
0
      libcerror_error_set(
2825
0
       error,
2826
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2827
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2828
0
       "%s: UTF-32 string too small.",
2829
0
       function );
2830
2831
0
      return( -1 );
2832
0
    }
2833
0
    utf32_string[ *utf32_string_index ] = 0;
2834
2835
0
    *utf32_string_index += 1;
2836
0
  }
2837
0
  return( 1 );
2838
0
}
2839
2840
/* Compares an UTF-32 string with an UTF-32 stream
2841
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
2842
 */
2843
int libuna_utf32_string_compare_with_utf32_stream(
2844
     const libuna_utf32_character_t *utf32_string,
2845
     size_t utf32_string_size,
2846
     const uint8_t *utf32_stream,
2847
     size_t utf32_stream_size,
2848
     int byte_order,
2849
     libcerror_error_t **error )
2850
0
{
2851
0
  static char *function                                     = "libuna_utf32_string_compare_with_utf32_stream";
2852
0
  size_t utf32_stream_index                                 = 0;
2853
0
  size_t utf32_string_index                                 = 0;
2854
0
  libuna_unicode_character_t utf32_unicode_character        = 0;
2855
0
  libuna_unicode_character_t utf32_stream_unicode_character = 0;
2856
0
  int read_byte_order                                       = 0;
2857
2858
0
  if( utf32_string == NULL )
2859
0
  {
2860
0
    libcerror_error_set(
2861
0
     error,
2862
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2863
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2864
0
     "%s: invalid UTF-32 string.",
2865
0
     function );
2866
2867
0
    return( -1 );
2868
0
  }
2869
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
2870
0
  {
2871
0
    libcerror_error_set(
2872
0
     error,
2873
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2874
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2875
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
2876
0
     function );
2877
2878
0
    return( -1 );
2879
0
  }
2880
0
  if( utf32_stream == NULL )
2881
0
  {
2882
0
    libcerror_error_set(
2883
0
     error,
2884
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2885
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2886
0
     "%s: invalid UTF-32 stream.",
2887
0
     function );
2888
2889
0
    return( -1 );
2890
0
  }
2891
0
  if( utf32_stream_size > (size_t) SSIZE_MAX )
2892
0
  {
2893
0
    libcerror_error_set(
2894
0
     error,
2895
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2896
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2897
0
     "%s: invalid UTF-32 stream size value exceeds maximum.",
2898
0
     function );
2899
2900
0
    return( -1 );
2901
0
  }
2902
0
  if( ( utf32_stream_size == 0 )
2903
0
   || ( ( utf32_stream_size % 4 ) != 0 ) )
2904
0
  {
2905
0
    libcerror_error_set(
2906
0
     error,
2907
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2908
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2909
0
     "%s: missing UTF-32 stream value.",
2910
0
     function );
2911
2912
0
    return( -1 );
2913
0
  }
2914
  /* Check if UTF-32 stream is in big or little endian
2915
   */
2916
0
  if( utf32_stream_size >= 4 )
2917
0
  {
2918
0
    if( ( utf32_stream[ 0 ] == 0x00 )
2919
0
     && ( utf32_stream[ 1 ] == 0x00 )
2920
0
     && ( utf32_stream[ 2 ] == 0xfe )
2921
0
     && ( utf32_stream[ 3 ] == 0xff ) )
2922
0
    {
2923
0
      read_byte_order    = LIBUNA_ENDIAN_BIG;
2924
0
      utf32_stream_index = 4;
2925
0
    }
2926
0
    else if( ( utf32_stream[ 0 ] == 0xff )
2927
0
          && ( utf32_stream[ 1 ] == 0xfe )
2928
0
          && ( utf32_stream[ 2 ] == 0x00 )
2929
0
          && ( utf32_stream[ 3 ] == 0x00 ) )
2930
0
    {
2931
0
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
2932
0
      utf32_stream_index = 4;
2933
0
    }
2934
0
    if( byte_order == 0 )
2935
0
    {
2936
0
      byte_order = read_byte_order;
2937
0
    }
2938
0
  }
2939
0
  if( ( byte_order != LIBUNA_ENDIAN_BIG )
2940
0
   && ( byte_order != LIBUNA_ENDIAN_LITTLE ) )
2941
0
  {
2942
0
    libcerror_error_set(
2943
0
     error,
2944
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2945
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2946
0
     "%s: unsupported byte order.",
2947
0
     function );
2948
2949
0
    return( -1 );
2950
0
  }
2951
0
  if( ( utf32_string_size >= 1 )
2952
0
   && ( utf32_string[ utf32_string_size - 1 ] == 0 ) )
2953
0
  {
2954
0
    utf32_string_size -= 1;
2955
0
  }
2956
  /* Check if the UTF-32 stream is terminated with zero bytes
2957
   */
2958
0
  if( ( utf32_stream_size >= 4 )
2959
0
   && ( utf32_stream[ utf32_stream_size - 4 ] == 0 )
2960
0
   && ( utf32_stream[ utf32_stream_size - 3 ] == 0 )
2961
0
   && ( utf32_stream[ utf32_stream_size - 2 ] == 0 )
2962
0
   && ( utf32_stream[ utf32_stream_size - 1 ] == 0 ) )
2963
0
  {
2964
0
    utf32_stream_size -= 1;
2965
0
  }
2966
0
  while( ( utf32_string_index < utf32_string_size )
2967
0
      && ( utf32_stream_index < utf32_stream_size ) )
2968
0
  {
2969
    /* Convert the UTF-32 character bytes into an Unicode character
2970
     */
2971
0
    if( libuna_unicode_character_copy_from_utf32(
2972
0
         &utf32_unicode_character,
2973
0
         utf32_string,
2974
0
         utf32_string_size,
2975
0
         &utf32_string_index,
2976
0
         error ) != 1 )
2977
0
    {
2978
0
      libcerror_error_set(
2979
0
       error,
2980
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
2981
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
2982
0
       "%s: unable to copy Unicode character from UTF-32.",
2983
0
       function );
2984
2985
0
      return( -1 );
2986
0
    }
2987
    /* Convert the UTF-32 stream bytes into an Unicode character
2988
     */
2989
0
    if( libuna_unicode_character_copy_from_utf32_stream(
2990
0
         &utf32_stream_unicode_character,
2991
0
         utf32_stream,
2992
0
         utf32_stream_size,
2993
0
         &utf32_stream_index,
2994
0
         byte_order,
2995
0
                     error ) != 1 )
2996
0
    {
2997
0
      libcerror_error_set(
2998
0
       error,
2999
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
3000
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
3001
0
       "%s: unable to copy Unicode character from UTF-32 stream.",
3002
0
       function );
3003
3004
0
      return( -1 );
3005
0
    }
3006
0
    if( utf32_unicode_character < utf32_stream_unicode_character )
3007
0
    {
3008
0
      return( LIBUNA_COMPARE_LESS );
3009
0
    }
3010
0
    else if( utf32_unicode_character > utf32_stream_unicode_character )
3011
0
    {
3012
0
      return( LIBUNA_COMPARE_GREATER );
3013
0
    }
3014
0
  }
3015
  /* Check if both strings were entirely processed
3016
   */
3017
0
  if( utf32_string_index < utf32_string_size )
3018
0
  {
3019
0
    return( LIBUNA_COMPARE_GREATER );
3020
0
  }
3021
0
  else if( utf32_stream_index < utf32_stream_size )
3022
0
  {
3023
0
    return( LIBUNA_COMPARE_LESS );
3024
0
  }
3025
0
  return( LIBUNA_COMPARE_EQUAL );
3026
0
}
3027
3028
/* Determines the size of an UTF-32 string from a Standard Compression Scheme for Unicode (SCSU) stream
3029
 * Returns 1 if successful or -1 on error
3030
 */
3031
int libuna_utf32_string_size_from_scsu_stream(
3032
     const uint8_t *scsu_stream,
3033
     size_t scsu_stream_size,
3034
     size_t *utf32_string_size,
3035
     libcerror_error_t **error )
3036
0
{
3037
0
  uint32_t scsu_dynamic_window_positions[ 8 ] = {
3038
0
    0x0080, 0x00c0, 0x0400, 0x0600, 0x0900, 0x3040, 0x30a0, 0xff00 };
3039
3040
0
  static char *function                        = "libuna_utf8_string_size_from_scsu_stream";
3041
0
  libuna_unicode_character_t unicode_character = 0;
3042
0
  size_t scsu_stream_index                     = 0;
3043
0
  uint32_t scsu_window_position                = 0;
3044
0
  uint8_t byte_value1                          = 0;
3045
0
  uint8_t byte_value2                          = 0;
3046
0
  uint8_t byte_value3                          = 0;
3047
0
  uint8_t dynamic_window_position_index        = 0;
3048
0
  uint8_t in_unicode_mode                      = 0;
3049
0
  uint8_t unicode_character_set                = 0;
3050
3051
0
  if( scsu_stream == NULL )
3052
0
  {
3053
0
    libcerror_error_set(
3054
0
     error,
3055
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3056
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3057
0
     "%s: invalid SCSU stream.",
3058
0
     function );
3059
3060
0
    return( -1 );
3061
0
  }
3062
0
  if( scsu_stream_size > (size_t) SSIZE_MAX )
3063
0
  {
3064
0
    libcerror_error_set(
3065
0
     error,
3066
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3067
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3068
0
     "%s: invalid SCSU stream size value exceeds maximum.",
3069
0
     function );
3070
3071
0
    return( -1 );
3072
0
  }
3073
0
  if( utf32_string_size == NULL )
3074
0
  {
3075
0
    libcerror_error_set(
3076
0
     error,
3077
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3078
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3079
0
     "%s: invalid UTF-32 string size.",
3080
0
     function );
3081
3082
0
    return( -1 );
3083
0
  }
3084
0
  *utf32_string_size = 0;
3085
3086
0
  if( scsu_stream_size == 0 )
3087
0
  {
3088
0
    return( 1 );
3089
0
  }
3090
0
  while( scsu_stream_index < scsu_stream_size )
3091
0
  {
3092
0
    unicode_character_set = 0;
3093
3094
0
    if( scsu_stream_index >= scsu_stream_size )
3095
0
    {
3096
0
      libcerror_error_set(
3097
0
       error,
3098
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3099
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3100
0
       "%s: SCSU stream too small.",
3101
0
       function );
3102
3103
0
      return( -1 );
3104
0
    }
3105
0
    byte_value1 = scsu_stream[ scsu_stream_index++ ];
3106
3107
0
    if( in_unicode_mode != 0 )
3108
0
    {
3109
0
      if( ( byte_value1 <= 0xdf )
3110
0
       || ( byte_value1 >= 0xf3 ) )
3111
0
      {
3112
0
        if( scsu_stream_index >= scsu_stream_size )
3113
0
        {
3114
0
          libcerror_error_set(
3115
0
           error,
3116
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3117
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3118
0
           "%s: SCSU stream too small.",
3119
0
           function );
3120
3121
0
          return( -1 );
3122
0
        }
3123
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3124
3125
0
        unicode_character   = byte_value1;
3126
0
        unicode_character <<= 8;
3127
0
        unicode_character  |= byte_value2;
3128
3129
0
        unicode_character_set = 1;
3130
0
      }
3131
      /* UCn tags
3132
       */
3133
0
      else if( ( byte_value1 >= 0xe0 )
3134
0
            && ( byte_value1 <= 0xe7 ) )
3135
0
      {
3136
0
        dynamic_window_position_index = byte_value1 - 0xe0;
3137
3138
0
        in_unicode_mode = 0;
3139
0
      }
3140
      /* UDn tags
3141
       */
3142
0
      else if( ( byte_value1 >= 0xe8 )
3143
0
            && ( byte_value1 <= 0xef ) )
3144
0
      {
3145
0
        if( scsu_stream_index >= scsu_stream_size )
3146
0
        {
3147
0
          libcerror_error_set(
3148
0
           error,
3149
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3150
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3151
0
           "%s: SCSU stream too small.",
3152
0
           function );
3153
3154
0
          return( -1 );
3155
0
        }
3156
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3157
3158
0
        dynamic_window_position_index = byte_value1 - 0xe8;
3159
0
        scsu_window_position          = libuna_scsu_window_offset_table[ byte_value2 ];
3160
3161
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3162
3163
0
        in_unicode_mode = 0;
3164
0
      }
3165
      /* UQU tag
3166
       */
3167
0
      else if( byte_value1 == 0xf0 )
3168
0
      {
3169
0
        if( ( scsu_stream_size < 2 )
3170
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3171
0
        {
3172
0
          libcerror_error_set(
3173
0
           error,
3174
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3175
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3176
0
           "%s: SCSU stream too small.",
3177
0
           function );
3178
3179
0
          return( -1 );
3180
0
        }
3181
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3182
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3183
3184
0
        unicode_character   = byte_value2;
3185
0
        unicode_character <<= 8;
3186
0
        unicode_character  |= byte_value3;
3187
3188
0
        unicode_character_set = 1;
3189
0
      }
3190
      /* UDX tag
3191
       */
3192
0
      else if( byte_value1 == 0xf1 )
3193
0
      {
3194
0
        if( ( scsu_stream_size < 2 )
3195
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3196
0
        {
3197
0
          libcerror_error_set(
3198
0
           error,
3199
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3200
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3201
0
           "%s: SCSU stream too small.",
3202
0
           function );
3203
3204
0
          return( -1 );
3205
0
        }
3206
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3207
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3208
3209
0
        dynamic_window_position_index = byte_value2 >> 5;
3210
0
        scsu_window_position          = byte_value2 & 0x1f;
3211
0
        scsu_window_position        <<= 8;
3212
0
        scsu_window_position         |= byte_value3;
3213
0
        scsu_window_position        <<= 7;
3214
0
        scsu_window_position         += 0x00010000UL;
3215
3216
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3217
3218
0
        in_unicode_mode = 0;
3219
0
      }
3220
0
    }
3221
0
    else
3222
0
    {
3223
0
      if( ( byte_value1 == 0x00 )
3224
0
       || ( byte_value1 == 0x09 )
3225
0
       || ( byte_value1 == 0x0a )
3226
0
       || ( byte_value1 == 0x0c )
3227
0
       || ( byte_value1 == 0x0d )
3228
0
       || ( ( byte_value1 >= 0x20 )
3229
0
        &&  ( byte_value1 <= 0x7f ) ) )
3230
0
      {
3231
0
        unicode_character = byte_value1;
3232
3233
0
        unicode_character_set = 1;
3234
0
      }
3235
      /* SQn tags
3236
       */
3237
0
      else if( ( byte_value1 >= 0x01 )
3238
0
            && ( byte_value1 <= 0x08 ) )
3239
0
      {
3240
0
        if( scsu_stream_index >= scsu_stream_size )
3241
0
        {
3242
0
          libcerror_error_set(
3243
0
           error,
3244
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3245
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3246
0
           "%s: SCSU stream too small.",
3247
0
           function );
3248
3249
0
          return( -1 );
3250
0
        }
3251
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3252
3253
0
        unicode_character = byte_value2;
3254
3255
0
        if( byte_value2 < 0x80 )
3256
0
        {
3257
0
          unicode_character += libuna_scsu_static_window_positions[ byte_value1 - 0x01 ];
3258
0
        }
3259
0
        else
3260
0
        {
3261
0
          unicode_character -= 0x80;
3262
0
          unicode_character += scsu_dynamic_window_positions[ byte_value1 - 0x01 ];
3263
0
        }
3264
0
        unicode_character_set = 1;
3265
0
      }
3266
      /* SDX tag
3267
       */
3268
0
      else if( byte_value1 == 0x0b )
3269
0
      {
3270
0
        if( ( scsu_stream_size < 2 )
3271
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3272
0
        {
3273
0
          libcerror_error_set(
3274
0
           error,
3275
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3276
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3277
0
           "%s: SCSU stream too small.",
3278
0
           function );
3279
3280
0
          return( -1 );
3281
0
        }
3282
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3283
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3284
3285
0
        dynamic_window_position_index = byte_value2 >> 5;
3286
0
        scsu_window_position          = byte_value2 & 0x1f;
3287
0
        scsu_window_position        <<= 8;
3288
0
        scsu_window_position         |= byte_value3;
3289
0
        scsu_window_position        <<= 7;
3290
0
        scsu_window_position         += 0x00010000UL;
3291
3292
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3293
0
      }
3294
      /* SQU tag
3295
       */
3296
0
      else if( byte_value1 == 0x0e )
3297
0
      {
3298
0
        if( ( scsu_stream_size < 2 )
3299
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3300
0
        {
3301
0
          libcerror_error_set(
3302
0
           error,
3303
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3304
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3305
0
           "%s: SCSU stream too small.",
3306
0
           function );
3307
3308
0
          return( -1 );
3309
0
        }
3310
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3311
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3312
3313
0
        unicode_character   = byte_value2;
3314
0
        unicode_character <<= 8;
3315
0
        unicode_character  |= byte_value3;
3316
3317
0
        unicode_character_set = 1;
3318
0
      }
3319
      /* SCU tag
3320
       */
3321
0
      else if( byte_value1 == 0x0f )
3322
0
      {
3323
0
        in_unicode_mode = 1;
3324
0
      }
3325
      /* SCn tags
3326
       */
3327
0
      else if( ( byte_value1 >= 0x10 )
3328
0
            && ( byte_value1 <= 0x17 ) )
3329
0
      {
3330
0
        dynamic_window_position_index = byte_value1 - 0x10;
3331
0
      }
3332
      /* SDn tags
3333
       */
3334
0
      else if( ( byte_value1 >= 0x18 )
3335
0
            && ( byte_value1 <= 0x1f ) )
3336
0
      {
3337
0
        if( scsu_stream_index >= scsu_stream_size )
3338
0
        {
3339
0
          libcerror_error_set(
3340
0
           error,
3341
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3342
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3343
0
           "%s: SCSU stream too small.",
3344
0
           function );
3345
3346
0
          return( -1 );
3347
0
        }
3348
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3349
3350
0
        dynamic_window_position_index = byte_value1 - 0x18;
3351
0
        scsu_window_position          = libuna_scsu_window_offset_table[ byte_value2 ];
3352
3353
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3354
0
      }
3355
0
      else if( byte_value1 >= 0x80 )
3356
0
      {
3357
0
        unicode_character  = byte_value1 - 0x80;
3358
0
        unicode_character += scsu_dynamic_window_positions[ dynamic_window_position_index ];
3359
3360
0
        unicode_character_set = 1;
3361
0
      }
3362
0
    }
3363
0
    if( unicode_character_set != 0 )
3364
0
    {
3365
      /* Determine how many UTF-32 character bytes are required
3366
       */
3367
0
      if( libuna_unicode_character_size_to_utf32(
3368
0
           unicode_character,
3369
0
           utf32_string_size,
3370
0
           error ) != 1 )
3371
0
      {
3372
0
        libcerror_error_set(
3373
0
         error,
3374
0
         LIBCERROR_ERROR_DOMAIN_CONVERSION,
3375
0
         LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
3376
0
         "%s: unable to unable to determine size of Unicode character in UTF-32.",
3377
0
         function );
3378
3379
0
        return( -1 );
3380
0
      }
3381
0
      if( unicode_character == 0 )
3382
0
      {
3383
0
        break;
3384
0
      }
3385
0
    }
3386
0
  }
3387
  /* Check if the string is terminated with an end-of-string character
3388
   */
3389
0
  if( unicode_character != 0 )
3390
0
  {
3391
0
    *utf32_string_size += 1;
3392
0
  }
3393
0
  return( 1 );
3394
0
}
3395
3396
/* Copies an UTF-32 string from a Standard Compression Scheme for Unicode (SCSU) stream
3397
 * Returns 1 if successful or -1 on error
3398
 */
3399
int libuna_utf32_string_copy_from_scsu_stream(
3400
     libuna_utf32_character_t *utf32_string,
3401
     size_t utf32_string_size,
3402
     const uint8_t *scsu_stream,
3403
     size_t scsu_stream_size,
3404
     libcerror_error_t **error )
3405
0
{
3406
0
  static char *function     = "libuna_utf32_string_copy_from_scsu_stream";
3407
0
  size_t utf32_string_index = 0;
3408
3409
0
  if( libuna_utf32_string_with_index_copy_from_scsu_stream(
3410
0
       utf32_string,
3411
0
       utf32_string_size,
3412
0
       &utf32_string_index,
3413
0
       scsu_stream,
3414
0
       scsu_stream_size,
3415
0
       error ) != 1 )
3416
0
  {
3417
0
    libcerror_error_set(
3418
0
     error,
3419
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3420
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
3421
0
     "%s: unable to SCSU stream to UTF-32 string.",
3422
0
     function );
3423
3424
0
    return( -1 );
3425
0
  }
3426
0
  return( 1 );
3427
0
}
3428
3429
/* Copies an UTF-32 string from a Standard Compression Scheme for Unicode (SCSU) stream
3430
 * Returns 1 if successful or -1 on error
3431
 */
3432
int libuna_utf32_string_with_index_copy_from_scsu_stream(
3433
     libuna_utf32_character_t *utf32_string,
3434
     size_t utf32_string_size,
3435
     size_t *utf32_string_index,
3436
     const uint8_t *scsu_stream,
3437
     size_t scsu_stream_size,
3438
     libcerror_error_t **error )
3439
0
{
3440
0
  uint32_t scsu_dynamic_window_positions[ 8 ] = {
3441
0
    0x0080, 0x00c0, 0x0400, 0x0600, 0x0900, 0x3040, 0x30a0, 0xff00 };
3442
3443
0
  static char *function                        = "libuna_utf32_string_with_index_copy_from_scsu_stream";
3444
0
  libuna_unicode_character_t unicode_character = 0;
3445
0
  size_t scsu_stream_index                     = 0;
3446
0
  uint32_t scsu_window_position                = 0;
3447
0
  uint8_t byte_value1                          = 0;
3448
0
  uint8_t byte_value2                          = 0;
3449
0
  uint8_t byte_value3                          = 0;
3450
0
  uint8_t dynamic_window_position_index        = 0;
3451
0
  uint8_t in_unicode_mode                      = 0;
3452
0
  uint8_t unicode_character_set                = 0;
3453
3454
0
  if( utf32_string == NULL )
3455
0
  {
3456
0
    libcerror_error_set(
3457
0
     error,
3458
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3459
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3460
0
     "%s: invalid UTF-32 string.",
3461
0
     function );
3462
3463
0
    return( -1 );
3464
0
  }
3465
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
3466
0
  {
3467
0
    libcerror_error_set(
3468
0
     error,
3469
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3470
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3471
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
3472
0
     function );
3473
3474
0
    return( -1 );
3475
0
  }
3476
0
  if( utf32_string_index == NULL )
3477
0
  {
3478
0
    libcerror_error_set(
3479
0
     error,
3480
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3481
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3482
0
     "%s: invalid UTF-32 string index.",
3483
0
     function );
3484
3485
0
    return( -1 );
3486
0
  }
3487
0
  if( scsu_stream == NULL )
3488
0
  {
3489
0
    libcerror_error_set(
3490
0
     error,
3491
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3492
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3493
0
     "%s: invalid SCSU stream.",
3494
0
     function );
3495
3496
0
    return( -1 );
3497
0
  }
3498
0
  if( scsu_stream_size > (size_t) SSIZE_MAX )
3499
0
  {
3500
0
    libcerror_error_set(
3501
0
     error,
3502
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3503
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3504
0
     "%s: invalid SCSU stream size value exceeds maximum.",
3505
0
     function );
3506
3507
0
    return( -1 );
3508
0
  }
3509
0
  if( scsu_stream_size == 0 )
3510
0
  {
3511
0
    libcerror_error_set(
3512
0
     error,
3513
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3514
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
3515
0
     "%s: missing SCSU stream value.",
3516
0
     function );
3517
3518
0
    return( -1 );
3519
0
  }
3520
0
  while( scsu_stream_index < scsu_stream_size )
3521
0
  {
3522
0
    unicode_character_set = 0;
3523
3524
0
    if( scsu_stream_index >= scsu_stream_size )
3525
0
    {
3526
0
      libcerror_error_set(
3527
0
       error,
3528
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3529
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3530
0
       "%s: SCSU stream too small.",
3531
0
       function );
3532
3533
0
      return( -1 );
3534
0
    }
3535
0
    byte_value1 = scsu_stream[ scsu_stream_index++ ];
3536
3537
0
    if( in_unicode_mode != 0 )
3538
0
    {
3539
0
      if( ( byte_value1 <= 0xdf )
3540
0
       || ( byte_value1 >= 0xf3 ) )
3541
0
      {
3542
0
        if( scsu_stream_index >= scsu_stream_size )
3543
0
        {
3544
0
          libcerror_error_set(
3545
0
           error,
3546
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3547
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3548
0
           "%s: SCSU stream too small.",
3549
0
           function );
3550
3551
0
          return( -1 );
3552
0
        }
3553
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3554
3555
0
        unicode_character   = byte_value1;
3556
0
        unicode_character <<= 8;
3557
0
        unicode_character  |= byte_value2;
3558
3559
0
        unicode_character_set = 1;
3560
0
      }
3561
      /* UCn tags
3562
       */
3563
0
      else if( ( byte_value1 >= 0xe0 )
3564
0
            && ( byte_value1 <= 0xe7 ) )
3565
0
      {
3566
0
        dynamic_window_position_index = byte_value1 - 0xe0;
3567
3568
0
        in_unicode_mode = 0;
3569
0
      }
3570
      /* UDn tags
3571
       */
3572
0
      else if( ( byte_value1 >= 0xe8 )
3573
0
            && ( byte_value1 <= 0xef ) )
3574
0
      {
3575
0
        if( scsu_stream_index >= scsu_stream_size )
3576
0
        {
3577
0
          libcerror_error_set(
3578
0
           error,
3579
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3580
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3581
0
           "%s: SCSU stream too small.",
3582
0
           function );
3583
3584
0
          return( -1 );
3585
0
        }
3586
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3587
3588
0
        dynamic_window_position_index = byte_value1 - 0xe8;
3589
0
        scsu_window_position          = libuna_scsu_window_offset_table[ byte_value2 ];
3590
3591
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3592
3593
0
        in_unicode_mode = 0;
3594
0
      }
3595
      /* UQU tag
3596
       */
3597
0
      else if( byte_value1 == 0xf0 )
3598
0
      {
3599
0
        if( ( scsu_stream_size < 2 )
3600
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3601
0
        {
3602
0
          libcerror_error_set(
3603
0
           error,
3604
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3605
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3606
0
           "%s: SCSU stream too small.",
3607
0
           function );
3608
3609
0
          return( -1 );
3610
0
        }
3611
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3612
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3613
3614
0
        unicode_character   = byte_value2;
3615
0
        unicode_character <<= 8;
3616
0
        unicode_character  |= byte_value3;
3617
3618
0
        unicode_character_set = 1;
3619
0
      }
3620
      /* UDX tag
3621
       */
3622
0
      else if( byte_value1 == 0xf1 )
3623
0
      {
3624
0
        if( ( scsu_stream_size < 2 )
3625
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3626
0
        {
3627
0
          libcerror_error_set(
3628
0
           error,
3629
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3630
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3631
0
           "%s: SCSU stream too small.",
3632
0
           function );
3633
3634
0
          return( -1 );
3635
0
        }
3636
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3637
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3638
3639
0
        dynamic_window_position_index = byte_value2 >> 5;
3640
0
        scsu_window_position          = byte_value2 & 0x1f;
3641
0
        scsu_window_position        <<= 8;
3642
0
        scsu_window_position         |= byte_value3;
3643
0
        scsu_window_position        <<= 7;
3644
0
        scsu_window_position         += 0x00010000UL;
3645
3646
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3647
3648
0
        in_unicode_mode = 0;
3649
0
      }
3650
0
    }
3651
0
    else
3652
0
    {
3653
0
      if( ( byte_value1 == 0x00 )
3654
0
       || ( byte_value1 == 0x09 )
3655
0
       || ( byte_value1 == 0x0a )
3656
0
       || ( byte_value1 == 0x0c )
3657
0
       || ( byte_value1 == 0x0d )
3658
0
       || ( ( byte_value1 >= 0x20 )
3659
0
        &&  ( byte_value1 <= 0x7f ) ) )
3660
0
      {
3661
0
        unicode_character = byte_value1;
3662
3663
0
        unicode_character_set = 1;
3664
0
      }
3665
      /* SQn tags
3666
       */
3667
0
      else if( ( byte_value1 >= 0x01 )
3668
0
            && ( byte_value1 <= 0x08 ) )
3669
0
      {
3670
0
        if( scsu_stream_index >= scsu_stream_size )
3671
0
        {
3672
0
          libcerror_error_set(
3673
0
           error,
3674
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3675
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3676
0
           "%s: SCSU stream too small.",
3677
0
           function );
3678
3679
0
          return( -1 );
3680
0
        }
3681
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3682
3683
0
        unicode_character = byte_value2;
3684
3685
0
        if( byte_value2 < 0x80 )
3686
0
        {
3687
0
          unicode_character += libuna_scsu_static_window_positions[ byte_value1 - 0x01 ];
3688
0
        }
3689
0
        else
3690
0
        {
3691
0
          unicode_character -= 0x80;
3692
0
          unicode_character += scsu_dynamic_window_positions[ byte_value1 - 0x01 ];
3693
0
        }
3694
0
        unicode_character_set = 1;
3695
0
      }
3696
      /* SDX tag
3697
       */
3698
0
      else if( byte_value1 == 0x0b )
3699
0
      {
3700
0
        if( ( scsu_stream_size < 2 )
3701
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3702
0
        {
3703
0
          libcerror_error_set(
3704
0
           error,
3705
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3706
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3707
0
           "%s: SCSU stream too small.",
3708
0
           function );
3709
3710
0
          return( -1 );
3711
0
        }
3712
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3713
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3714
3715
0
        dynamic_window_position_index = byte_value2 >> 5;
3716
0
        scsu_window_position          = byte_value2 & 0x1f;
3717
0
        scsu_window_position        <<= 8;
3718
0
        scsu_window_position         |= byte_value3;
3719
0
        scsu_window_position        <<= 7;
3720
0
        scsu_window_position         += 0x00010000UL;
3721
3722
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3723
0
      }
3724
      /* SQU tag
3725
       */
3726
0
      else if( byte_value1 == 0x0e )
3727
0
      {
3728
0
        if( ( scsu_stream_size < 2 )
3729
0
         || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3730
0
        {
3731
0
          libcerror_error_set(
3732
0
           error,
3733
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3734
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3735
0
           "%s: SCSU stream too small.",
3736
0
           function );
3737
3738
0
          return( -1 );
3739
0
        }
3740
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3741
0
        byte_value3 = scsu_stream[ scsu_stream_index++ ];
3742
3743
0
        unicode_character   = byte_value2;
3744
0
        unicode_character <<= 8;
3745
0
        unicode_character  |= byte_value3;
3746
3747
0
        unicode_character_set = 1;
3748
0
      }
3749
      /* SCU tag
3750
       */
3751
0
      else if( byte_value1 == 0x0f )
3752
0
      {
3753
0
        in_unicode_mode = 1;
3754
0
      }
3755
      /* SCn tags
3756
       */
3757
0
      else if( ( byte_value1 >= 0x10 )
3758
0
            && ( byte_value1 <= 0x17 ) )
3759
0
      {
3760
0
        dynamic_window_position_index = byte_value1 - 0x10;
3761
0
      }
3762
      /* SDn tags
3763
       */
3764
0
      else if( ( byte_value1 >= 0x18 )
3765
0
            && ( byte_value1 <= 0x1f ) )
3766
0
      {
3767
0
        if( scsu_stream_index >= scsu_stream_size )
3768
0
        {
3769
0
          libcerror_error_set(
3770
0
           error,
3771
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3772
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3773
0
           "%s: SCSU stream too small.",
3774
0
           function );
3775
3776
0
          return( -1 );
3777
0
        }
3778
0
        byte_value2 = scsu_stream[ scsu_stream_index++ ];
3779
3780
0
        dynamic_window_position_index = byte_value1 - 0x18;
3781
0
        scsu_window_position          = libuna_scsu_window_offset_table[ byte_value2 ];
3782
3783
0
        scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3784
0
      }
3785
0
      else if( byte_value1 >= 0x80 )
3786
0
      {
3787
0
        unicode_character  = byte_value1 - 0x80;
3788
0
        unicode_character += scsu_dynamic_window_positions[ dynamic_window_position_index ];
3789
3790
0
        unicode_character_set = 1;
3791
0
      }
3792
0
    }
3793
0
    if( unicode_character_set != 0 )
3794
0
    {
3795
      /* Convert the Unicode character into UTF-32 character bytes
3796
       */
3797
0
      if( libuna_unicode_character_copy_to_utf32(
3798
0
           unicode_character,
3799
0
           utf32_string,
3800
0
           utf32_string_size,
3801
0
           utf32_string_index,
3802
0
           error ) != 1 )
3803
0
      {
3804
0
        libcerror_error_set(
3805
0
         error,
3806
0
         LIBCERROR_ERROR_DOMAIN_CONVERSION,
3807
0
         LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
3808
0
         "%s: unable to copy Unicode character to UTF-32.",
3809
0
         function );
3810
3811
0
        return( -1 );
3812
0
      }
3813
0
      if( unicode_character == 0 )
3814
0
      {
3815
0
        break;
3816
0
      }
3817
0
    }
3818
0
  }
3819
  /* Check if the string is terminated with an end-of-string character
3820
   */
3821
0
  if( unicode_character != 0 )
3822
0
  {
3823
0
    if( *utf32_string_index >= utf32_string_size )
3824
0
    {
3825
0
      libcerror_error_set(
3826
0
       error,
3827
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3828
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3829
0
       "%s: UTF-32 string too small.",
3830
0
       function );
3831
3832
0
      return( -1 );
3833
0
    }
3834
0
    utf32_string[ *utf32_string_index ] = 0;
3835
3836
0
    *utf32_string_index += 1;
3837
0
  }
3838
0
  return( 1 );
3839
0
}
3840