Debian Codegraph @

libuna/libuna_utf32_string.c

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