Coverage Report

Created: 2026-12-01 07:31

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