Debian Codegraph @

libuna/libuna_utf16_string.c

raw symbols SCIP
1 /*
2 * UTF-16 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_utf16_string.h"
31
32 /* Determines the size of an UTF-16 string from a byte stream
33 * Returns 1 if successful or -1 on error
34 */
35 int libuna_utf16_string_size_from_byte_stream(
36 const uint8_t *byte_stream,
37 size_t byte_stream_size,
38 int codepage,
39 size_t *utf16_string_size,
40 libcerror_error_t **error )
41 {
42 static char *function = "libuna_utf16_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( utf16_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-16 string size.",
75 function );
76
77 return( -1 );
78 }
79 *utf16_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-16 character byte words are required
107 */
108 if( libuna_unicode_character_size_to_utf16(
109 unicode_character,
110 utf16_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-16.",
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 *utf16_string_size += 1;
132 }
133 return( 1 );
134 }
135
136 /* Copies an UTF-16 string from a byte stream
137 * Returns 1 if successful or -1 on error
138 */
139 int libuna_utf16_string_copy_from_byte_stream(
140 libuna_utf16_character_t *utf16_string,
141 size_t utf16_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_utf16_string_copy_from_byte_stream";
148 size_t utf16_string_index = 0;
149
150 if( libuna_utf16_string_with_index_copy_from_byte_stream(
151 utf16_string,
152 utf16_string_size,
153 &utf16_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-16 string.",
164 function );
165
166 return( -1 );
167 }
168 return( 1 );
169 }
170
171 /* Copies an UTF-16 string from a byte stream
172 * Returns 1 if successful or -1 on error
173 */
174 int libuna_utf16_string_with_index_copy_from_byte_stream(
175 libuna_utf16_character_t *utf16_string,
176 size_t utf16_string_size,
177 size_t *utf16_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_utf16_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( utf16_string == NULL )
188 {
189 libcerror_error_set(
190 error,
191 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
192 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
193 "%s: invalid UTF-16 string.",
194 function );
195
196 return( -1 );
197 }
198 if( utf16_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-16 string size value exceeds maximum.",
205 function );
206
207 return( -1 );
208 }
209 if( utf16_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-16 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-16 character byte words
275 */
276 if( libuna_unicode_character_copy_to_utf16(
277 unicode_character,
278 utf16_string,
279 utf16_string_size,
280 utf16_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-16.",
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( *utf16_string_index >= utf16_string_size )
302 {
303 libcerror_error_set(
304 error,
305 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
306 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
307 "%s: UTF-16 string too small.",
308 function );
309
310 return( -1 );
311 }
312 utf16_string[ *utf16_string_index ] = 0;
313
314 *utf16_string_index += 1;
315 }
316 return( 1 );
317 }
318
319 /* Compares an UTF-16 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_utf16_string_compare_with_byte_stream(
323 const libuna_utf16_character_t *utf16_string,
324 size_t utf16_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_utf16_string_compare_with_byte_stream";
331 size_t byte_stream_index = 0;
332 size_t utf16_string_index = 0;
333 libuna_unicode_character_t utf16_unicode_character = 0;
334 libuna_unicode_character_t byte_stream_unicode_character = 0;
335
336 if( utf16_string == NULL )
337 {
338 libcerror_error_set(
339 error,
340 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
341 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
342 "%s: invalid UTF-16 string.",
343 function );
344
345 return( -1 );
346 }
347 if( utf16_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-16 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( ( utf16_string_size >= 1 )
392 && ( utf16_string[ utf16_string_size - 1 ] == 0 ) )
393 {
394 utf16_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( ( utf16_string_index < utf16_string_size )
404 && ( byte_stream_index < byte_stream_size ) )
405 {
406 /* Convert the UTF-16 character bytes into an Unicode character
407 */
408 if( libuna_unicode_character_copy_from_utf16(
409 &utf16_unicode_character,
410 utf16_string,
411 utf16_string_size,
412 &utf16_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-16.",
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( utf16_unicode_character < byte_stream_unicode_character )
444 {
445 return( LIBUNA_COMPARE_LESS );
446 }
447 else if( utf16_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( utf16_string_index < utf16_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-16 string from an UTF-7 stream
466 * Returns 1 if successful or -1 on error
467 */
468 int libuna_utf16_string_size_from_utf7_stream(
469 const uint8_t *utf7_stream,
470 size_t utf7_stream_size,
471 size_t *utf16_string_size,
472 libcerror_error_t **error )
473 {
474 static char *function = "libuna_utf16_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( utf16_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-16 string size.",
508 function );
509
510 return( -1 );
511 }
512 *utf16_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-16 character bytes are required
540 */
541 if( libuna_unicode_character_size_to_utf16(
542 unicode_character,
543 utf16_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-16.",
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 *utf16_string_size += 1;
565 }
566 return( 1 );
567 }
568
569 /* Copies an UTF-16 string from an UTF-7 stream
570 * Returns 1 if successful or -1 on error
571 */
572 int libuna_utf16_string_copy_from_utf7_stream(
573 libuna_utf16_character_t *utf16_string,
574 size_t utf16_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_utf16_string_copy_from_utf7_stream";
580 size_t utf16_string_index = 0;
581
582 if( libuna_utf16_string_with_index_copy_from_utf7_stream(
583 utf16_string,
584 utf16_string_size,
585 &utf16_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-16 string.",
595 function );
596
597 return( -1 );
598 }
599 return( 1 );
600 }
601
602 /* Copies an UTF-16 string from an UTF-7 stream
603 * Returns 1 if successful or -1 on error
604 */
605 int libuna_utf16_string_with_index_copy_from_utf7_stream(
606 libuna_utf16_character_t *utf16_string,
607 size_t utf16_string_size,
608 size_t *utf16_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_utf16_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( utf16_string == NULL )
619 {
620 libcerror_error_set(
621 error,
622 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
623 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
624 "%s: invalid UTF-16 string.",
625 function );
626
627 return( -1 );
628 }
629 if( utf16_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-16 string size value exceeds maximum.",
636 function );
637
638 return( -1 );
639 }
640 if( utf16_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-16 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-16 character bytes
706 */
707 if( libuna_unicode_character_copy_to_utf16(
708 unicode_character,
709 utf16_string,
710 utf16_string_size,
711 utf16_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-16.",
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( *utf16_string_index >= utf16_string_size )
733 {
734 libcerror_error_set(
735 error,
736 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
737 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
738 "%s: UTF-16 string too small.",
739 function );
740
741 return( -1 );
742 }
743 utf16_string[ *utf16_string_index ] = 0;
744
745 *utf16_string_index += 1;
746 }
747 return( 1 );
748 }
749
750 /* Compares an UTF-16 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_utf16_string_compare_with_utf7_stream(
754 const libuna_utf16_character_t *utf16_string,
755 size_t utf16_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_utf16_string_compare_with_utf7_stream";
761 size_t utf16_string_index = 0;
762 size_t utf7_stream_index = 0;
763 libuna_unicode_character_t utf16_unicode_character = 0;
764 libuna_unicode_character_t utf7_stream_unicode_character = 0;
765 uint32_t utf7_stream_base64_data = 0;
766
767 if( utf16_string == NULL )
768 {
769 libcerror_error_set(
770 error,
771 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
772 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
773 "%s: invalid UTF-16 string.",
774 function );
775
776 return( -1 );
777 }
778 if( utf16_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-16 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( ( utf16_string_size >= 1 )
823 && ( utf16_string[ utf16_string_size - 1 ] == 0 ) )
824 {
825 utf16_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( ( utf16_string_index < utf16_string_size )
835 && ( utf7_stream_index < utf7_stream_size ) )
836 {
837 /* Convert the UTF-16 character bytes into an Unicode character
838 */
839 if( libuna_unicode_character_copy_from_utf16(
840 &utf16_unicode_character,
841 utf16_string,
842 utf16_string_size,
843 &utf16_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-16.",
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( utf16_unicode_character < utf7_stream_unicode_character )
875 {
876 return( LIBUNA_COMPARE_LESS );
877 }
878 else if( utf16_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( utf16_string_index < utf16_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-16 string from an UTF-8 string
897 * Returns 1 if successful or -1 on error
898 */
899 int libuna_utf16_string_size_from_utf8(
900 const libuna_utf8_character_t *utf8_string,
901 size_t utf8_string_size,
902 size_t *utf16_string_size,
903 libcerror_error_t **error )
904 {
905 static char *function = "libuna_utf16_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( utf16_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-16 string size.",
938 function );
939
940 return( -1 );
941 }
942 *utf16_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-16 character bytes are required
969 */
970 if( libuna_unicode_character_size_to_utf16(
971 unicode_character,
972 utf16_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-16.",
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 *utf16_string_size += 1;
994 }
995 return( 1 );
996 }
997
998 /* Copies an UTF-16 string from an UTF-8 string
999 * Returns 1 if successful or -1 on error
1000 */
1001 int libuna_utf16_string_copy_from_utf8(
1002 libuna_utf16_character_t *utf16_string,
1003 size_t utf16_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_utf16_string_copy_from_utf8";
1009 size_t utf16_string_index = 0;
1010
1011 if( libuna_utf16_string_with_index_copy_from_utf8(
1012 utf16_string,
1013 utf16_string_size,
1014 &utf16_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-16 string.",
1024 function );
1025
1026 return( -1 );
1027 }
1028 return( 1 );
1029 }
1030
1031 /* Copies an UTF-16 string from an UTF-8 string
1032 * Returns 1 if successful or -1 on error
1033 */
1034 int libuna_utf16_string_with_index_copy_from_utf8(
1035 libuna_utf16_character_t *utf16_string,
1036 size_t utf16_string_size,
1037 size_t *utf16_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_utf16_string_with_index_copy_from_utf8";
1043 size_t utf8_string_index = 0;
1044 libuna_unicode_character_t unicode_character = 0;
1045
1046 if( utf16_string == NULL )
1047 {
1048 libcerror_error_set(
1049 error,
1050 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1051 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1052 "%s: invalid UTF-16 string.",
1053 function );
1054
1055 return( -1 );
1056 }
1057 if( utf16_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-16 string size value exceeds maximum.",
1064 function );
1065
1066 return( -1 );
1067 }
1068 if( utf16_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-16 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-16 character bytes
1133 */
1134 if( libuna_unicode_character_copy_to_utf16(
1135 unicode_character,
1136 utf16_string,
1137 utf16_string_size,
1138 utf16_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-16.",
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( *utf16_string_index >= utf16_string_size )
1160 {
1161 libcerror_error_set(
1162 error,
1163 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1164 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1165 "%s: UTF-16 string too small.",
1166 function );
1167
1168 return( -1 );
1169 }
1170 utf16_string[ *utf16_string_index ] = 0;
1171
1172 *utf16_string_index += 1;
1173 }
1174 return( 1 );
1175 }
1176
1177 /* Determines the size of an UTF-16 string from an UTF-8 stream
1178 * Returns 1 if successful or -1 on error
1179 */
1180 int libuna_utf16_string_size_from_utf8_stream(
1181 const uint8_t *utf8_stream,
1182 size_t utf8_stream_size,
1183 size_t *utf16_string_size,
1184 libcerror_error_t **error )
1185 {
1186 static char *function = "libuna_utf16_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( utf16_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-16 string size.",
1219 function );
1220
1221 return( -1 );
1222 }
1223 *utf16_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-16 character bytes are required
1261 */
1262 if( libuna_unicode_character_size_to_utf16(
1263 unicode_character,
1264 utf16_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-16.",
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 *utf16_string_size += 1;
1286 }
1287 return( 1 );
1288 }
1289
1290 /* Copies an UTF-16 string from an UTF-8 stream
1291 * Returns 1 if successful or -1 on error
1292 */
1293 int libuna_utf16_string_copy_from_utf8_stream(
1294 libuna_utf16_character_t *utf16_string,
1295 size_t utf16_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_utf16_string_copy_from_utf8_stream";
1301 size_t utf16_string_index = 0;
1302
1303 if( libuna_utf16_string_with_index_copy_from_utf8_stream(
1304 utf16_string,
1305 utf16_string_size,
1306 &utf16_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-16 string.",
1316 function );
1317
1318 return( -1 );
1319 }
1320 return( 1 );
1321 }
1322
1323 /* Copies an UTF-16 string from an UTF-8 stream
1324 * Returns 1 if successful or -1 on error
1325 */
1326 int libuna_utf16_string_with_index_copy_from_utf8_stream(
1327 libuna_utf16_character_t *utf16_string,
1328 size_t utf16_string_size,
1329 size_t *utf16_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_utf16_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( utf16_string == NULL )
1339 {
1340 libcerror_error_set(
1341 error,
1342 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1343 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1344 "%s: invalid UTF-16 string.",
1345 function );
1346
1347 return( -1 );
1348 }
1349 if( utf16_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-16 string size value exceeds maximum.",
1356 function );
1357
1358 return( -1 );
1359 }
1360 if( utf16_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-16 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-16 character bytes
1436 */
1437 if( libuna_unicode_character_copy_to_utf16(
1438 unicode_character,
1439 utf16_string,
1440 utf16_string_size,
1441 utf16_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-16.",
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( *utf16_string_index >= utf16_string_size )
1463 {
1464 libcerror_error_set(
1465 error,
1466 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1467 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1468 "%s: UTF-16 string too small.",
1469 function );
1470
1471 return( -1 );
1472 }
1473 utf16_string[ *utf16_string_index ] = 0;
1474
1475 *utf16_string_index += 1;
1476 }
1477 return( 1 );
1478 }
1479
1480 /* Compares an UTF-16 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_utf16_string_compare_with_utf8_stream(
1484 const libuna_utf16_character_t *utf16_string,
1485 size_t utf16_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_utf16_string_compare_with_utf8_stream";
1491 size_t utf16_string_index = 0;
1492 size_t utf8_stream_index = 0;
1493 libuna_unicode_character_t utf16_unicode_character = 0;
1494 libuna_unicode_character_t utf8_stream_unicode_character = 0;
1495
1496 if( utf16_string == NULL )
1497 {
1498 libcerror_error_set(
1499 error,
1500 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1501 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1502 "%s: invalid UTF-16 string.",
1503 function );
1504
1505 return( -1 );
1506 }
1507 if( utf16_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-16 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( ( utf16_string_size >= 1 )
1563 && ( utf16_string[ utf16_string_size - 1 ] == 0 ) )
1564 {
1565 utf16_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( ( utf16_string_index < utf16_string_size )
1575 && ( utf8_stream_index < utf8_stream_size ) )
1576 {
1577 /* Convert the UTF-16 character bytes into an Unicode character
1578 */
1579 if( libuna_unicode_character_copy_from_utf16(
1580 &utf16_unicode_character,
1581 utf16_string,
1582 utf16_string_size,
1583 &utf16_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-16.",
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( utf16_unicode_character < utf8_stream_unicode_character )
1614 {
1615 return( LIBUNA_COMPARE_LESS );
1616 }
1617 else if( utf16_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( utf16_string_index < utf16_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-16 string from an UTF-16 stream
1636 * Returns 1 if successful or -1 on error
1637 */
1638 int libuna_utf16_string_size_from_utf16_stream(
1639 const uint8_t *utf16_stream,
1640 size_t utf16_stream_size,
1641 int byte_order,
1642 size_t *utf16_string_size,
1643 libcerror_error_t **error )
1644 {
1645 static char *function = "libuna_utf16_string_size_from_utf16_stream";
1646 libuna_unicode_character_t unicode_character = 0;
1647 size_t utf16_stream_index = 0;
1648 int read_byte_order = 0;
1649 int result = 0;
1650
1651 if( utf16_stream == NULL )
1652 {
1653 libcerror_error_set(
1654 error,
1655 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1656 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1657 "%s: invalid UTF-16 stream.",
1658 function );
1659
1660 return( -1 );
1661 }
1662 if( utf16_stream_size > (size_t) SSIZE_MAX )
1663 {
1664 libcerror_error_set(
1665 error,
1666 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1667 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1668 "%s: invalid UTF-16 stream size value exceeds maximum.",
1669 function );
1670
1671 return( -1 );
1672 }
1673 if( ( utf16_stream_size % 2 ) != 0 )
1674 {
1675 libcerror_error_set(
1676 error,
1677 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1678 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1679 "%s: missing UTF-16 stream value.",
1680 function );
1681
1682 return( -1 );
1683 }
1684 if( utf16_string_size == NULL )
1685 {
1686 libcerror_error_set(
1687 error,
1688 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1689 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1690 "%s: invalid UTF-16 string size.",
1691 function );
1692
1693 return( -1 );
1694 }
1695 *utf16_string_size = 0;
1696
1697 if( utf16_stream_size == 0 )
1698 {
1699 return( 1 );
1700 }
1701 /* Check if UTF-16 stream is in big or little endian
1702 */
1703 if( utf16_stream_size >= 2 )
1704 {
1705 if( ( utf16_stream[ 0 ] == 0x0ff )
1706 && ( utf16_stream[ 1 ] == 0x0fe ) )
1707 {
1708 read_byte_order = LIBUNA_ENDIAN_LITTLE;
1709 utf16_stream_index = 2;
1710 }
1711 else if( ( utf16_stream[ 0 ] == 0x0fe )
1712 && ( utf16_stream[ 1 ] == 0x0ff ) )
1713 {
1714 read_byte_order = LIBUNA_ENDIAN_BIG;
1715 utf16_stream_index = 2;
1716 }
1717 if( byte_order == 0 )
1718 {
1719 byte_order = read_byte_order;
1720 }
1721 }
1722 while( ( utf16_stream_index + 1 ) < utf16_stream_size )
1723 {
1724 /* Convert the UTF-16 stream bytes into an Unicode character
1725 */
1726 if( libuna_unicode_character_copy_from_utf16_stream(
1727 &unicode_character,
1728 utf16_stream,
1729 utf16_stream_size,
1730 &utf16_stream_index,
1731 byte_order,
1732 error ) != 1 )
1733 {
1734 libcerror_error_set(
1735 error,
1736 LIBCERROR_ERROR_DOMAIN_CONVERSION,
1737 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
1738 "%s: unable to copy Unicode character from UTF-16 stream.",
1739 function );
1740
1741 return( -1 );
1742 }
1743 /* Determine how many UTF-16 character bytes are required
1744 */
1745 if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
1746 {
1747 result = libuna_unicode_character_size_to_utf16(
1748 unicode_character,
1749 utf16_string_size,
1750 error );
1751 }
1752 else
1753 {
1754 result = libuna_unicode_character_size_to_ucs2(
1755 unicode_character,
1756 utf16_string_size,
1757 error );
1758 }
1759 if( result != 1 )
1760 {
1761 libcerror_error_set(
1762 error,
1763 LIBCERROR_ERROR_DOMAIN_CONVERSION,
1764 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
1765 "%s: unable to unable to determine size of Unicode character in UTF-16.",
1766 function );
1767
1768 return( -1 );
1769 }
1770 if( unicode_character == 0 )
1771 {
1772 break;
1773 }
1774 }
1775 /* Check if the string is terminated with an end-of-string character
1776 */
1777 if( unicode_character != 0 )
1778 {
1779 *utf16_string_size += 1;
1780 }
1781 return( 1 );
1782 }
1783
1784 /* Copies an UTF-16 string from an UTF-16 stream
1785 * Returns 1 if successful or -1 on error
1786 */
1787 int libuna_utf16_string_copy_from_utf16_stream(
1788 libuna_utf16_character_t *utf16_string,
1789 size_t utf16_string_size,
1790 const uint8_t *utf16_stream,
1791 size_t utf16_stream_size,
1792 int byte_order,
1793 libcerror_error_t **error )
1794 {
1795 static char *function = "libuna_utf16_string_copy_from_utf16_stream";
1796 size_t utf16_string_index = 0;
1797
1798 if( libuna_utf16_string_with_index_copy_from_utf16_stream(
1799 utf16_string,
1800 utf16_string_size,
1801 &utf16_string_index,
1802 utf16_stream,
1803 utf16_stream_size,
1804 byte_order,
1805 error ) != 1 )
1806 {
1807 libcerror_error_set(
1808 error,
1809 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1810 LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
1811 "%s: unable to copy UTF-16 stream to UTF-16 string.",
1812 function );
1813
1814 return( -1 );
1815 }
1816 return( 1 );
1817 }
1818
1819 /* Copies an UTF-16 string from an UTF-16 stream
1820 * Returns 1 if successful or -1 on error
1821 */
1822 int libuna_utf16_string_with_index_copy_from_utf16_stream(
1823 libuna_utf16_character_t *utf16_string,
1824 size_t utf16_string_size,
1825 size_t *utf16_string_index,
1826 const uint8_t *utf16_stream,
1827 size_t utf16_stream_size,
1828 int byte_order,
1829 libcerror_error_t **error )
1830 {
1831 static char *function = "libuna_utf16_string_with_index_copy_from_utf16_stream";
1832 libuna_unicode_character_t unicode_character = 0;
1833 size_t utf16_stream_index = 0;
1834 int read_byte_order = 0;
1835 int result = 0;
1836
1837 if( utf16_string == NULL )
1838 {
1839 libcerror_error_set(
1840 error,
1841 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1842 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1843 "%s: invalid UTF-16 string.",
1844 function );
1845
1846 return( -1 );
1847 }
1848 if( utf16_string_size > (size_t) SSIZE_MAX )
1849 {
1850 libcerror_error_set(
1851 error,
1852 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1853 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1854 "%s: invalid UTF-16 string size value exceeds maximum.",
1855 function );
1856
1857 return( -1 );
1858 }
1859 if( utf16_string_index == NULL )
1860 {
1861 libcerror_error_set(
1862 error,
1863 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1864 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1865 "%s: invalid UTF-16 string index.",
1866 function );
1867
1868 return( -1 );
1869 }
1870 if( utf16_stream == NULL )
1871 {
1872 libcerror_error_set(
1873 error,
1874 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1875 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1876 "%s: invalid UTF-16 stream.",
1877 function );
1878
1879 return( -1 );
1880 }
1881 if( utf16_stream_size > (size_t) SSIZE_MAX )
1882 {
1883 libcerror_error_set(
1884 error,
1885 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1886 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1887 "%s: invalid UTF-16 stream size value exceeds maximum.",
1888 function );
1889
1890 return( -1 );
1891 }
1892 if( ( utf16_stream_size == 0 )
1893 || ( ( utf16_stream_size % 2 ) != 0 ) )
1894 {
1895 libcerror_error_set(
1896 error,
1897 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1898 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1899 "%s: missing UTF-16 stream value.",
1900 function );
1901
1902 return( -1 );
1903 }
1904 /* Check if UTF-16 stream is in big or little endian
1905 */
1906 if( utf16_stream_size >= 2 )
1907 {
1908 if( ( utf16_stream[ 0 ] == 0x0ff )
1909 && ( utf16_stream[ 1 ] == 0x0fe ) )
1910 {
1911 read_byte_order = LIBUNA_ENDIAN_LITTLE;
1912 utf16_stream_index = 2;
1913 }
1914 else if( ( utf16_stream[ 0 ] == 0x0fe )
1915 && ( utf16_stream[ 1 ] == 0x0ff ) )
1916 {
1917 read_byte_order = LIBUNA_ENDIAN_BIG;
1918 utf16_stream_index = 2;
1919 }
1920 if( byte_order == 0 )
1921 {
1922 byte_order = read_byte_order;
1923 }
1924 }
1925 while( ( utf16_stream_index + 1 ) < utf16_stream_size )
1926 {
1927 /* Convert the UTF-16 stream bytes into an Unicode character
1928 */
1929 if( libuna_unicode_character_copy_from_utf16_stream(
1930 &unicode_character,
1931 utf16_stream,
1932 utf16_stream_size,
1933 &utf16_stream_index,
1934 byte_order,
1935 error ) != 1 )
1936 {
1937 libcerror_error_set(
1938 error,
1939 LIBCERROR_ERROR_DOMAIN_CONVERSION,
1940 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
1941 "%s: unable to copy Unicode character from UTF-16 stream.",
1942 function );
1943
1944 return( -1 );
1945 }
1946 /* Convert the Unicode character into UTF-16 character bytes
1947 */
1948 if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
1949 {
1950 result = libuna_unicode_character_copy_to_utf16(
1951 unicode_character,
1952 utf16_string,
1953 utf16_string_size,
1954 utf16_string_index,
1955 error );
1956 }
1957 else
1958 {
1959 result = libuna_unicode_character_copy_to_ucs2(
1960 unicode_character,
1961 utf16_string,
1962 utf16_string_size,
1963 utf16_string_index,
1964 error );
1965 }
1966 if( result != 1 )
1967 {
1968 libcerror_error_set(
1969 error,
1970 LIBCERROR_ERROR_DOMAIN_CONVERSION,
1971 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
1972 "%s: unable to copy Unicode character to UTF-16.",
1973 function );
1974
1975 return( -1 );
1976 }
1977 if( unicode_character == 0 )
1978 {
1979 break;
1980 }
1981 }
1982 /* Check if the string is terminated with an end-of-string character
1983 */
1984 if( unicode_character != 0 )
1985 {
1986 if( *utf16_string_index >= utf16_string_size )
1987 {
1988 libcerror_error_set(
1989 error,
1990 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1991 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1992 "%s: UTF-16 string too small.",
1993 function );
1994
1995 return( -1 );
1996 }
1997 utf16_string[ *utf16_string_index ] = 0;
1998
1999 *utf16_string_index += 1;
2000 }
2001 return( 1 );
2002 }
2003
2004 /* Compares an UTF-16 string with an UTF-16 stream
2005 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
2006 */
2007 int libuna_utf16_string_compare_with_utf16_stream(
2008 const libuna_utf16_character_t *utf16_string,
2009 size_t utf16_string_size,
2010 const uint8_t *utf16_stream,
2011 size_t utf16_stream_size,
2012 int byte_order,
2013 libcerror_error_t **error )
2014 {
2015 static char *function = "libuna_utf16_string_compare_with_utf16_stream";
2016 libuna_unicode_character_t utf16_stream_unicode_character = 0;
2017 libuna_unicode_character_t utf16_unicode_character = 0;
2018 size_t utf16_stream_index = 0;
2019 size_t utf16_string_index = 0;
2020 int read_byte_order = 0;
2021 int result = 0;
2022
2023 if( utf16_string == NULL )
2024 {
2025 libcerror_error_set(
2026 error,
2027 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2028 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2029 "%s: invalid UTF-16 string.",
2030 function );
2031
2032 return( -1 );
2033 }
2034 if( utf16_string_size > (size_t) SSIZE_MAX )
2035 {
2036 libcerror_error_set(
2037 error,
2038 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2039 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2040 "%s: invalid UTF-16 string size value exceeds maximum.",
2041 function );
2042
2043 return( -1 );
2044 }
2045 if( utf16_stream == NULL )
2046 {
2047 libcerror_error_set(
2048 error,
2049 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2050 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2051 "%s: invalid UTF-16 stream.",
2052 function );
2053
2054 return( -1 );
2055 }
2056 if( utf16_stream_size > (size_t) SSIZE_MAX )
2057 {
2058 libcerror_error_set(
2059 error,
2060 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2061 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2062 "%s: invalid UTF-16 stream size value exceeds maximum.",
2063 function );
2064
2065 return( -1 );
2066 }
2067 if( ( utf16_stream_size == 0 )
2068 || ( ( utf16_stream_size % 2 ) != 0 ) )
2069 {
2070 libcerror_error_set(
2071 error,
2072 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2073 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2074 "%s: missing UTF-16 stream value.",
2075 function );
2076
2077 return( -1 );
2078 }
2079 /* Check if UTF-16 stream is in big or little endian
2080 */
2081 if( utf16_stream_size >= 2 )
2082 {
2083 if( ( utf16_stream[ 0 ] == 0xfe )
2084 && ( utf16_stream[ 1 ] == 0xff ) )
2085 {
2086 read_byte_order = LIBUNA_ENDIAN_BIG;
2087 utf16_stream_index = 2;
2088 }
2089 else if( ( utf16_stream[ 0 ] == 0xff )
2090 && ( utf16_stream[ 1 ] == 0xfe ) )
2091 {
2092 read_byte_order = LIBUNA_ENDIAN_LITTLE;
2093 utf16_stream_index = 2;
2094 }
2095 if( byte_order == 0 )
2096 {
2097 byte_order = read_byte_order;
2098 }
2099 }
2100 if( ( utf16_string_size >= 1 )
2101 && ( utf16_string[ utf16_string_size - 1 ] == 0 ) )
2102 {
2103 utf16_string_size -= 1;
2104 }
2105 /* Check if the UTF-16 stream is terminated with zero bytes
2106 */
2107 if( ( utf16_stream_size >= 2 )
2108 && ( utf16_stream[ utf16_stream_size - 2 ] == 0 )
2109 && ( utf16_stream[ utf16_stream_size - 1 ] == 0 ) )
2110 {
2111 utf16_stream_size -= 2;
2112 }
2113 while( ( utf16_string_index < utf16_string_size )
2114 && ( utf16_stream_index < utf16_stream_size ) )
2115 {
2116 /* Convert the UTF-16 character bytes into an Unicode character
2117 */
2118 if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
2119 {
2120 result = libuna_unicode_character_copy_from_utf16(
2121 &utf16_unicode_character,
2122 utf16_string,
2123 utf16_string_size,
2124 &utf16_string_index,
2125 error );
2126 }
2127 else
2128 {
2129 result = libuna_unicode_character_copy_from_ucs2(
2130 &utf16_unicode_character,
2131 utf16_string,
2132 utf16_string_size,
2133 &utf16_string_index,
2134 error );
2135 }
2136 if( result != 1 )
2137 {
2138 libcerror_error_set(
2139 error,
2140 LIBCERROR_ERROR_DOMAIN_CONVERSION,
2141 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
2142 "%s: unable to copy Unicode character from UTF-16.",
2143 function );
2144
2145 return( -1 );
2146 }
2147 /* Convert the UTF-16 stream bytes into an Unicode character
2148 */
2149 if( libuna_unicode_character_copy_from_utf16_stream(
2150 &utf16_stream_unicode_character,
2151 utf16_stream,
2152 utf16_stream_size,
2153 &utf16_stream_index,
2154 byte_order,
2155 error ) != 1 )
2156 {
2157 libcerror_error_set(
2158 error,
2159 LIBCERROR_ERROR_DOMAIN_CONVERSION,
2160 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2161 "%s: unable to copy Unicode character from UTF-16 stream.",
2162 function );
2163
2164 return( -1 );
2165 }
2166 if( utf16_unicode_character < utf16_stream_unicode_character )
2167 {
2168 return( LIBUNA_COMPARE_LESS );
2169 }
2170 else if( utf16_unicode_character > utf16_stream_unicode_character )
2171 {
2172 return( LIBUNA_COMPARE_GREATER );
2173 }
2174 }
2175 /* Check if both strings were entirely processed
2176 */
2177 if( utf16_string_index < utf16_string_size )
2178 {
2179 return( LIBUNA_COMPARE_GREATER );
2180 }
2181 else if( utf16_stream_index < utf16_stream_size )
2182 {
2183 return( LIBUNA_COMPARE_LESS );
2184 }
2185 return( LIBUNA_COMPARE_EQUAL );
2186 }
2187
2188 /* Determines the size of an UTF-16 string from an UTF-32 string
2189 * Returns 1 if successful or -1 on error
2190 */
2191 int libuna_utf16_string_size_from_utf32(
2192 const libuna_utf32_character_t *utf32_string,
2193 size_t utf32_string_size,
2194 size_t *utf16_string_size,
2195 libcerror_error_t **error )
2196 {
2197 static char *function = "libuna_utf16_string_size_from_utf32";
2198 size_t utf32_string_index = 0;
2199 libuna_unicode_character_t unicode_character = 0;
2200
2201 if( utf32_string == NULL )
2202 {
2203 libcerror_error_set(
2204 error,
2205 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2206 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2207 "%s: invalid UTF-32 string.",
2208 function );
2209
2210 return( -1 );
2211 }
2212 if( utf32_string_size > (size_t) SSIZE_MAX )
2213 {
2214 libcerror_error_set(
2215 error,
2216 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2217 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2218 "%s: invalid UTF-32 string size value exceeds maximum.",
2219 function );
2220
2221 return( -1 );
2222 }
2223 if( utf16_string_size == NULL )
2224 {
2225 libcerror_error_set(
2226 error,
2227 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2228 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2229 "%s: invalid UTF-16 string size.",
2230 function );
2231
2232 return( -1 );
2233 }
2234 *utf16_string_size = 0;
2235
2236 if( utf32_string_size == 0 )
2237 {
2238 return( 1 );
2239 }
2240 while( utf32_string_index < utf32_string_size )
2241 {
2242 /* Convert the UTF-32 character bytes into an Unicode character
2243 */
2244 if( libuna_unicode_character_copy_from_utf32(
2245 &unicode_character,
2246 utf32_string,
2247 utf32_string_size,
2248 &utf32_string_index,
2249 error ) != 1 )
2250 {
2251 libcerror_error_set(
2252 error,
2253 LIBCERROR_ERROR_DOMAIN_CONVERSION,
2254 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2255 "%s: unable to copy Unicode character from UTF-32.",
2256 function );
2257
2258 return( -1 );
2259 }
2260 /* Determine how many UTF-16 character bytes are required
2261 */
2262 if( libuna_unicode_character_size_to_utf16(
2263 unicode_character,
2264 utf16_string_size,
2265 error ) != 1 )
2266 {
2267 libcerror_error_set(
2268 error,
2269 LIBCERROR_ERROR_DOMAIN_CONVERSION,
2270 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2271 "%s: unable to unable to determine size of Unicode character in UTF-16.",
2272 function );
2273
2274 return( -1 );
2275 }
2276 if( unicode_character == 0 )
2277 {
2278 break;
2279 }
2280 }
2281 /* Check if the string is terminated with an end-of-string character
2282 */
2283 if( unicode_character != 0 )
2284 {
2285 *utf16_string_size += 1;
2286 }
2287 return( 1 );
2288 }
2289
2290 /* Copies an UTF-16 string from an UTF-32 string
2291 * Returns 1 if successful or -1 on error
2292 */
2293 int libuna_utf16_string_copy_from_utf32(
2294 libuna_utf16_character_t *utf16_string,
2295 size_t utf16_string_size,
2296 const libuna_utf32_character_t *utf32_string,
2297 size_t utf32_string_size,
2298 libcerror_error_t **error )
2299 {
2300 static char *function = "libuna_utf16_string_copy_from_utf32";
2301 size_t utf16_string_index = 0;
2302
2303 if( libuna_utf16_string_with_index_copy_from_utf32(
2304 utf16_string,
2305 utf16_string_size,
2306 &utf16_string_index,
2307 utf32_string,
2308 utf32_string_size,
2309 error ) != 1 )
2310 {
2311 libcerror_error_set(
2312 error,
2313 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2314 LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
2315 "%s: unable to copy UTF-32 string to UTF-16 string.",
2316 function );
2317
2318 return( -1 );
2319 }
2320 return( 1 );
2321 }
2322
2323 /* Copies an UTF-16 string from an UTF-32 string
2324 * Returns 1 if successful or -1 on error
2325 */
2326 int libuna_utf16_string_with_index_copy_from_utf32(
2327 libuna_utf16_character_t *utf16_string,
2328 size_t utf16_string_size,
2329 size_t *utf16_string_index,
2330 const libuna_utf32_character_t *utf32_string,
2331 size_t utf32_string_size,
2332 libcerror_error_t **error )
2333 {
2334 static char *function = "libuna_utf16_string_with_index_copy_from_utf32";
2335 size_t utf32_string_index = 0;
2336 libuna_unicode_character_t unicode_character = 0;
2337
2338 if( utf16_string == NULL )
2339 {
2340 libcerror_error_set(
2341 error,
2342 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2343 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2344 "%s: invalid UTF-16 string.",
2345 function );
2346
2347 return( -1 );
2348 }
2349 if( utf16_string_size > (size_t) SSIZE_MAX )
2350 {
2351 libcerror_error_set(
2352 error,
2353 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2354 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2355 "%s: invalid UTF-16 string size value exceeds maximum.",
2356 function );
2357
2358 return( -1 );
2359 }
2360 if( utf16_string_index == NULL )
2361 {
2362 libcerror_error_set(
2363 error,
2364 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2365 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2366 "%s: invalid UTF-16 string index.",
2367 function );
2368
2369 return( -1 );
2370 }
2371 if( utf32_string == NULL )
2372 {
2373 libcerror_error_set(
2374 error,
2375 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2376 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2377 "%s: invalid UTF-32 string.",
2378 function );
2379
2380 return( -1 );
2381 }
2382 if( utf32_string_size > (size_t) SSIZE_MAX )
2383 {
2384 libcerror_error_set(
2385 error,
2386 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2387 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2388 "%s: invalid UTF-32 string size value exceeds maximum.",
2389 function );
2390
2391 return( -1 );
2392 }
2393 if( utf32_string_size == 0 )
2394 {
2395 libcerror_error_set(
2396 error,
2397 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2398 LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
2399 "%s: missing UTF-32 string value.",
2400 function );
2401
2402 return( -1 );
2403 }
2404 while( utf32_string_index < utf32_string_size )
2405 {
2406 /* Convert the UTF-32 character bytes into an Unicode character
2407 */
2408 if( libuna_unicode_character_copy_from_utf32(
2409 &unicode_character,
2410 utf32_string,
2411 utf32_string_size,
2412 &utf32_string_index,
2413 error ) != 1 )
2414 {
2415 libcerror_error_set(
2416 error,
2417 LIBCERROR_ERROR_DOMAIN_CONVERSION,
2418 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2419 "%s: unable to copy Unicode character from UTF-32.",
2420 function );
2421
2422 return( -1 );
2423 }
2424 /* Convert the Unicode character into UTF-16 character bytes
2425 */
2426 if( libuna_unicode_character_copy_to_utf16(
2427 unicode_character,
2428 utf16_string,
2429 utf16_string_size,
2430 utf16_string_index,
2431 error ) != 1 )
2432 {
2433 libcerror_error_set(
2434 error,
2435 LIBCERROR_ERROR_DOMAIN_CONVERSION,
2436 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
2437 "%s: unable to copy Unicode character to UTF-16.",
2438 function );
2439
2440 return( -1 );
2441 }
2442 if( unicode_character == 0 )
2443 {
2444 break;
2445 }
2446 }
2447 /* Check if the string is terminated with an end-of-string character
2448 */
2449 if( unicode_character != 0 )
2450 {
2451 if( *utf16_string_index >= utf16_string_size )
2452 {
2453 libcerror_error_set(
2454 error,
2455 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2456 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2457 "%s: UTF-16 string too small.",
2458 function );
2459
2460 return( -1 );
2461 }
2462 utf16_string[ *utf16_string_index ] = 0;
2463
2464 *utf16_string_index += 1;
2465 }
2466 return( 1 );
2467 }
2468
2469 /* Compares an UTF-16 string with an UTF-32 string
2470 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
2471 */
2472 int libuna_utf16_string_compare_with_utf32(
2473 const libuna_utf16_character_t *utf16_string,
2474 size_t utf16_string_size,
2475 const libuna_utf32_character_t *utf32_string,
2476 size_t utf32_string_size,
2477 libcerror_error_t **error )
2478 {
2479 static char *function = "libuna_utf16_string_compare_with_utf32";
2480 size_t utf16_string_index = 0;
2481 size_t utf32_string_index = 0;
2482 libuna_unicode_character_t utf16_unicode_character = 0;
2483 libuna_unicode_character_t utf32_unicode_character = 0;
2484
2485 if( utf16_string == NULL )
2486 {
2487 libcerror_error_set(
2488 error,
2489 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2490 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2491 "%s: invalid UTF-16 string.",
2492 function );
2493
2494 return( -1 );
2495 }
2496 if( utf16_string_size > (size_t) SSIZE_MAX )
2497 {
2498 libcerror_error_set(
2499 error,
2500 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2501 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2502 "%s: invalid UTF-16 string size value exceeds maximum.",
2503 function );
2504
2505 return( -1 );
2506 }
2507 if( utf32_string == NULL )
2508 {
2509 libcerror_error_set(
2510 error,
2511 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2512 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2513 "%s: invalid UTF-32 string.",
2514 function );
2515
2516 return( -1 );
2517 }
2518 if( utf32_string_size > (size_t) SSIZE_MAX )
2519 {
2520 libcerror_error_set(
2521 error,
2522 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2523 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2524 "%s: invalid UTF-32 string size value exceeds maximum.",
2525 function );
2526
2527 return( -1 );
2528 }
2529 if( utf32_string_size == 0 )
2530 {
2531 libcerror_error_set(
2532 error,
2533 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2534 LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
2535 "%s: missing UTF-32 string value.",
2536 function );
2537
2538 return( -1 );
2539 }
2540 if( ( utf16_string_size >= 1 )
2541 && ( utf16_string[ utf16_string_size - 1 ] == 0 ) )
2542 {
2543 utf16_string_size -= 1;
2544 }
2545 if( ( utf32_string_size >= 1 )
2546 && ( utf32_string[ utf32_string_size - 1 ] == 0 ) )
2547 {
2548 utf32_string_size -= 1;
2549 }
2550 while( ( utf16_string_index < utf16_string_size )
2551 && ( utf32_string_index < utf32_string_size ) )
2552 {
2553 /* Convert the UTF-16 character bytes into an Unicode character
2554 */
2555 if( libuna_unicode_character_copy_from_utf16(
2556 &utf16_unicode_character,
2557 utf16_string,
2558 utf16_string_size,
2559 &utf16_string_index,
2560 error ) != 1 )
2561 {
2562 libcerror_error_set(
2563 error,
2564 LIBCERROR_ERROR_DOMAIN_CONVERSION,
2565 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
2566 "%s: unable to copy Unicode character from UTF-16.",
2567 function );
2568
2569 return( -1 );
2570 }
2571 /* Convert the UTF-32 character bytes into an Unicode character
2572 */
2573 if( libuna_unicode_character_copy_from_utf32(
2574 &utf32_unicode_character,
2575 utf32_string,
2576 utf32_string_size,
2577 &utf32_string_index,
2578 error ) != 1 )
2579 {
2580 libcerror_error_set(
2581 error,
2582 LIBCERROR_ERROR_DOMAIN_CONVERSION,
2583 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2584 "%s: unable to copy Unicode character from UTF-32.",
2585 function );
2586
2587 return( -1 );
2588 }
2589 if( utf16_unicode_character < utf32_unicode_character )
2590 {
2591 return( LIBUNA_COMPARE_LESS );
2592 }
2593 else if( utf16_unicode_character > utf32_unicode_character )
2594 {
2595 return( LIBUNA_COMPARE_GREATER );
2596 }
2597 }
2598 /* Check if both strings were entirely processed
2599 */
2600 if( utf16_string_index < utf16_string_size )
2601 {
2602 return( LIBUNA_COMPARE_GREATER );
2603 }
2604 else if( utf32_string_index < utf32_string_size )
2605 {
2606 return( LIBUNA_COMPARE_LESS );
2607 }
2608 return( LIBUNA_COMPARE_EQUAL );
2609 }
2610
2611 /* Determines the size of an UTF-16 string from an UTF-32 stream
2612 * Returns 1 if successful or -1 on error
2613 */
2614 int libuna_utf16_string_size_from_utf32_stream(
2615 const uint8_t *utf32_stream,
2616 size_t utf32_stream_size,
2617 int byte_order,
2618 size_t *utf16_string_size,
2619 libcerror_error_t **error )
2620 {
2621 static char *function = "libuna_utf16_string_size_from_utf32_stream";
2622 size_t utf32_stream_index = 0;
2623 libuna_unicode_character_t unicode_character = 0;
2624 int read_byte_order = 0;
2625
2626 if( utf32_stream == NULL )
2627 {
2628 libcerror_error_set(
2629 error,
2630 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2631 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2632 "%s: invalid UTF-32 stream.",
2633 function );
2634
2635 return( -1 );
2636 }
2637 if( utf32_stream_size > (size_t) SSIZE_MAX )
2638 {
2639 libcerror_error_set(
2640 error,
2641 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2642 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2643 "%s: invalid UTF-32 stream size value exceeds maximum.",
2644 function );
2645
2646 return( -1 );
2647 }
2648 if( ( utf32_stream_size % 4 ) != 0 )
2649 {
2650 libcerror_error_set(
2651 error,
2652 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2653 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2654 "%s: missing UTF-32 stream value.",
2655 function );
2656
2657 return( -1 );
2658 }
2659 if( utf16_string_size == NULL )
2660 {
2661 libcerror_error_set(
2662 error,
2663 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2664 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2665 "%s: invalid UTF-16 string size.",
2666 function );
2667
2668 return( -1 );
2669 }
2670 *utf16_string_size = 0;
2671
2672 if( utf32_stream_size == 0 )
2673 {
2674 return( 1 );
2675 }
2676 /* Check if UTF-32 stream is in big or little endian
2677 */
2678 if( utf32_stream_size >= 4 )
2679 {
2680 if( ( utf32_stream[ 0 ] == 0x00 )
2681 && ( utf32_stream[ 1 ] == 0x00 )
2682 && ( utf32_stream[ 2 ] == 0xfe )
2683 && ( utf32_stream[ 3 ] == 0xff ) )
2684 {
2685 read_byte_order = LIBUNA_ENDIAN_BIG;
2686 utf32_stream_index = 4;
2687 }
2688 else if( ( utf32_stream[ 0 ] == 0xff )
2689 && ( utf32_stream[ 1 ] == 0xfe )
2690 && ( utf32_stream[ 2 ] == 0x00 )
2691 && ( utf32_stream[ 3 ] == 0x00 ) )
2692 {
2693 read_byte_order = LIBUNA_ENDIAN_LITTLE;
2694 utf32_stream_index = 4;
2695 }
2696 if( byte_order == 0 )
2697 {
2698 byte_order = read_byte_order;
2699 }
2700 }
2701 while( ( utf32_stream_index + 3 ) < utf32_stream_size )
2702 {
2703 /* Convert the UTF-32 stream bytes into an Unicode character
2704 */
2705 if( libuna_unicode_character_copy_from_utf32_stream(
2706 &unicode_character,
2707 utf32_stream,
2708 utf32_stream_size,
2709 &utf32_stream_index,
2710 byte_order,
2711 error ) != 1 )
2712 {
2713 libcerror_error_set(
2714 error,
2715 LIBCERROR_ERROR_DOMAIN_CONVERSION,
2716 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2717 "%s: unable to copy Unicode character from UTF-32 stream.",
2718 function );
2719
2720 return( -1 );
2721 }
2722 /* Determine how many UTF-16 character bytes are required
2723 */
2724 if( libuna_unicode_character_size_to_utf16(
2725 unicode_character,
2726 utf16_string_size,
2727 error ) != 1 )
2728 {
2729 libcerror_error_set(
2730 error,
2731 LIBCERROR_ERROR_DOMAIN_CONVERSION,
2732 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2733 "%s: unable to unable to determine size of Unicode character in UTF-16.",
2734 function );
2735
2736 return( -1 );
2737 }
2738 if( unicode_character == 0 )
2739 {
2740 break;
2741 }
2742 }
2743 /* Check if the string is terminated with an end-of-string character
2744 */
2745 if( unicode_character != 0 )
2746 {
2747 *utf16_string_size += 1;
2748 }
2749 return( 1 );
2750 }
2751
2752 /* Copies an UTF-16 string from an UTF-32 stream
2753 * Returns 1 if successful or -1 on error
2754 */
2755 int libuna_utf16_string_copy_from_utf32_stream(
2756 libuna_utf16_character_t *utf16_string,
2757 size_t utf16_string_size,
2758 const uint8_t *utf32_stream,
2759 size_t utf32_stream_size,
2760 int byte_order,
2761 libcerror_error_t **error )
2762 {
2763 static char *function = "libuna_utf16_string_copy_from_utf32_stream";
2764 size_t utf16_string_index = 0;
2765
2766 if( libuna_utf16_string_with_index_copy_from_utf32_stream(
2767 utf16_string,
2768 utf16_string_size,
2769 &utf16_string_index,
2770 utf32_stream,
2771 utf32_stream_size,
2772 byte_order,
2773 error ) != 1 )
2774 {
2775 libcerror_error_set(
2776 error,
2777 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2778 LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
2779 "%s: unable to copy UTF-32 stream to UTF-16 string.",
2780 function );
2781
2782 return( -1 );
2783 }
2784 return( 1 );
2785 }
2786
2787 /* Copies an UTF-16 string from an UTF-32 stream
2788 * Returns 1 if successful or -1 on error
2789 */
2790 int libuna_utf16_string_with_index_copy_from_utf32_stream(
2791 libuna_utf16_character_t *utf16_string,
2792 size_t utf16_string_size,
2793 size_t *utf16_string_index,
2794 const uint8_t *utf32_stream,
2795 size_t utf32_stream_size,
2796 int byte_order,
2797 libcerror_error_t **error )
2798 {
2799 static char *function = "libuna_utf16_string_with_index_copy_from_utf32_stream";
2800 size_t utf32_stream_index = 0;
2801 libuna_unicode_character_t unicode_character = 0;
2802 int read_byte_order = 0;
2803
2804 if( utf16_string == NULL )
2805 {
2806 libcerror_error_set(
2807 error,
2808 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2809 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2810 "%s: invalid UTF-16 string.",
2811 function );
2812
2813 return( -1 );
2814 }
2815 if( utf16_string_size > (size_t) SSIZE_MAX )
2816 {
2817 libcerror_error_set(
2818 error,
2819 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2820 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2821 "%s: invalid UTF-16 string size value exceeds maximum.",
2822 function );
2823
2824 return( -1 );
2825 }
2826 if( utf16_string_index == NULL )
2827 {
2828 libcerror_error_set(
2829 error,
2830 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2831 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2832 "%s: invalid UTF-16 string index.",
2833 function );
2834
2835 return( -1 );
2836 }
2837 if( utf32_stream == NULL )
2838 {
2839 libcerror_error_set(
2840 error,
2841 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2842 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2843 "%s: invalid UTF-32 stream.",
2844 function );
2845
2846 return( -1 );
2847 }
2848 if( utf32_stream_size > (size_t) SSIZE_MAX )
2849 {
2850 libcerror_error_set(
2851 error,
2852 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2853 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2854 "%s: invalid UTF-32 stream size value exceeds maximum.",
2855 function );
2856
2857 return( -1 );
2858 }
2859 if( ( utf32_stream_size == 0 )
2860 || ( ( utf32_stream_size % 4 ) != 0 ) )
2861 {
2862 libcerror_error_set(
2863 error,
2864 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2865 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2866 "%s: missing UTF-32 stream value.",
2867 function );
2868
2869 return( -1 );
2870 }
2871 /* Check if UTF-32 stream is in big or little endian
2872 */
2873 if( utf32_stream_size >=4 )
2874 {
2875 if( ( utf32_stream[ 0 ] == 0x00 )
2876 && ( utf32_stream[ 1 ] == 0x00 )
2877 && ( utf32_stream[ 2 ] == 0xfe )
2878 && ( utf32_stream[ 3 ] == 0xff ) )
2879 {
2880 read_byte_order = LIBUNA_ENDIAN_BIG;
2881 utf32_stream_index = 4;
2882 }
2883 else if( ( utf32_stream[ 0 ] == 0xff )
2884 && ( utf32_stream[ 1 ] == 0xfe )
2885 && ( utf32_stream[ 2 ] == 0x00 )
2886 && ( utf32_stream[ 3 ] == 0x00 ) )
2887 {
2888 read_byte_order = LIBUNA_ENDIAN_LITTLE;
2889 utf32_stream_index = 4;
2890 }
2891 if( byte_order == 0 )
2892 {
2893 byte_order = read_byte_order;
2894 }
2895 }
2896 while( ( utf32_stream_index + 3 ) < utf32_stream_size )
2897 {
2898 /* Convert the UTF-32 stream bytes into an Unicode character
2899 */
2900 if( libuna_unicode_character_copy_from_utf32_stream(
2901 &unicode_character,
2902 utf32_stream,
2903 utf32_stream_size,
2904 &utf32_stream_index,
2905 byte_order,
2906 error ) != 1 )
2907 {
2908 libcerror_error_set(
2909 error,
2910 LIBCERROR_ERROR_DOMAIN_CONVERSION,
2911 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2912 "%s: unable to copy Unicode character from UTF-32 stream.",
2913 function );
2914
2915 return( -1 );
2916 }
2917 /* Convert the Unicode character into UTF-16 character bytes
2918 */
2919 if( libuna_unicode_character_copy_to_utf16(
2920 unicode_character,
2921 utf16_string,
2922 utf16_string_size,
2923 utf16_string_index,
2924 error ) != 1 )
2925 {
2926 libcerror_error_set(
2927 error,
2928 LIBCERROR_ERROR_DOMAIN_CONVERSION,
2929 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
2930 "%s: unable to copy Unicode character to UTF-16.",
2931 function );
2932
2933 return( -1 );
2934 }
2935 if( unicode_character == 0 )
2936 {
2937 break;
2938 }
2939 }
2940 /* Check if the string is terminated with an end-of-string character
2941 */
2942 if( unicode_character != 0 )
2943 {
2944 if( *utf16_string_index >= utf16_string_size )
2945 {
2946 libcerror_error_set(
2947 error,
2948 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2949 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2950 "%s: UTF-16 string too small.",
2951 function );
2952
2953 return( -1 );
2954 }
2955 utf16_string[ *utf16_string_index ] = 0;
2956
2957 *utf16_string_index += 1;
2958 }
2959 return( 1 );
2960 }
2961
2962 /* Compares an UTF-16 string with an UTF-32 stream
2963 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
2964 */
2965 int libuna_utf16_string_compare_with_utf32_stream(
2966 const libuna_utf16_character_t *utf16_string,
2967 size_t utf16_string_size,
2968 const uint8_t *utf32_stream,
2969 size_t utf32_stream_size,
2970 int byte_order,
2971 libcerror_error_t **error )
2972 {
2973 static char *function = "libuna_utf16_string_compare_with_utf32_stream";
2974 size_t utf16_string_index = 0;
2975 size_t utf32_stream_index = 0;
2976 libuna_unicode_character_t utf16_unicode_character = 0;
2977 libuna_unicode_character_t utf32_stream_unicode_character = 0;
2978 int read_byte_order = 0;
2979
2980 if( utf16_string == NULL )
2981 {
2982 libcerror_error_set(
2983 error,
2984 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2985 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2986 "%s: invalid UTF-16 string.",
2987 function );
2988
2989 return( -1 );
2990 }
2991 if( utf16_string_size > (size_t) SSIZE_MAX )
2992 {
2993 libcerror_error_set(
2994 error,
2995 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2996 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2997 "%s: invalid UTF-16 string size value exceeds maximum.",
2998 function );
2999
3000 return( -1 );
3001 }
3002 if( utf32_stream == NULL )
3003 {
3004 libcerror_error_set(
3005 error,
3006 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3007 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3008 "%s: invalid UTF-32 stream.",
3009 function );
3010
3011 return( -1 );
3012 }
3013 if( utf32_stream_size > (size_t) SSIZE_MAX )
3014 {
3015 libcerror_error_set(
3016 error,
3017 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3018 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3019 "%s: invalid UTF-32 stream size value exceeds maximum.",
3020 function );
3021
3022 return( -1 );
3023 }
3024 if( ( utf32_stream_size == 0 )
3025 || ( ( utf32_stream_size % 4 ) != 0 ) )
3026 {
3027 libcerror_error_set(
3028 error,
3029 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3030 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3031 "%s: missing UTF-32 stream value.",
3032 function );
3033
3034 return( -1 );
3035 }
3036 /* Check if UTF-32 stream is in big or little endian
3037 */
3038 if( utf32_stream_size >= 4 )
3039 {
3040 if( ( utf32_stream[ 0 ] == 0x00 )
3041 && ( utf32_stream[ 1 ] == 0x00 )
3042 && ( utf32_stream[ 2 ] == 0xfe )
3043 && ( utf32_stream[ 3 ] == 0xff ) )
3044 {
3045 read_byte_order = LIBUNA_ENDIAN_BIG;
3046 utf32_stream_index = 4;
3047 }
3048 else if( ( utf32_stream[ 0 ] == 0xff )
3049 && ( utf32_stream[ 1 ] == 0xfe )
3050 && ( utf32_stream[ 2 ] == 0x00 )
3051 && ( utf32_stream[ 3 ] == 0x00 ) )
3052 {
3053 read_byte_order = LIBUNA_ENDIAN_LITTLE;
3054 utf32_stream_index = 4;
3055 }
3056 if( byte_order == 0 )
3057 {
3058 byte_order = read_byte_order;
3059 }
3060 }
3061 if( ( utf16_string_size >= 1 )
3062 && ( utf16_string[ utf16_string_size - 1 ] == 0 ) )
3063 {
3064 utf16_string_size -= 1;
3065 }
3066 /* Check if the UTF-32 stream is terminated with zero bytes
3067 */
3068 if( ( utf32_stream_size >= 4 )
3069 && ( utf32_stream[ utf32_stream_size - 4 ] == 0 )
3070 && ( utf32_stream[ utf32_stream_size - 3 ] == 0 )
3071 && ( utf32_stream[ utf32_stream_size - 2 ] == 0 )
3072 && ( utf32_stream[ utf32_stream_size - 1 ] == 0 ) )
3073 {
3074 utf32_stream_size -= 1;
3075 }
3076 while( ( utf16_string_index < utf16_string_size )
3077 && ( utf32_stream_index < utf32_stream_size ) )
3078 {
3079 /* Convert the UTF-16 character bytes into an Unicode character
3080 */
3081 if( libuna_unicode_character_copy_from_utf16(
3082 &utf16_unicode_character,
3083 utf16_string,
3084 utf16_string_size,
3085 &utf16_string_index,
3086 error ) != 1 )
3087 {
3088 libcerror_error_set(
3089 error,
3090 LIBCERROR_ERROR_DOMAIN_CONVERSION,
3091 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
3092 "%s: unable to copy Unicode character from UTF-16.",
3093 function );
3094
3095 return( -1 );
3096 }
3097 /* Convert the UTF-32 stream bytes into an Unicode character
3098 */
3099 if( libuna_unicode_character_copy_from_utf32_stream(
3100 &utf32_stream_unicode_character,
3101 utf32_stream,
3102 utf32_stream_size,
3103 &utf32_stream_index,
3104 byte_order,
3105 error ) != 1 )
3106 {
3107 libcerror_error_set(
3108 error,
3109 LIBCERROR_ERROR_DOMAIN_CONVERSION,
3110 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
3111 "%s: unable to copy Unicode character from UTF-32 stream.",
3112 function );
3113
3114 return( -1 );
3115 }
3116 if( utf16_unicode_character < utf32_stream_unicode_character )
3117 {
3118 return( LIBUNA_COMPARE_LESS );
3119 }
3120 else if( utf16_unicode_character > utf32_stream_unicode_character )
3121 {
3122 return( LIBUNA_COMPARE_GREATER );
3123 }
3124 }
3125 /* Check if both strings were entirely processed
3126 */
3127 if( utf16_string_index < utf16_string_size )
3128 {
3129 return( LIBUNA_COMPARE_GREATER );
3130 }
3131 else if( utf32_stream_index < utf32_stream_size )
3132 {
3133 return( LIBUNA_COMPARE_LESS );
3134 }
3135 return( LIBUNA_COMPARE_EQUAL );
3136 }
3137
3138 /* Determines the size of an UTF-16 string from a Standard Compression Scheme for Unicode (SCSU) stream
3139 * Returns 1 if successful or -1 on error
3140 */
3141 int libuna_utf16_string_size_from_scsu_stream(
3142 const uint8_t *scsu_stream,
3143 size_t scsu_stream_size,
3144 size_t *utf16_string_size,
3145 libcerror_error_t **error )
3146 {
3147 uint32_t scsu_dynamic_window_positions[ 8 ] = {
3148 0x0080, 0x00c0, 0x0400, 0x0600, 0x0900, 0x3040, 0x30a0, 0xff00 };
3149
3150 static char *function = "libuna_utf8_string_size_from_scsu_stream";
3151 libuna_unicode_character_t unicode_character = 0;
3152 size_t scsu_stream_index = 0;
3153 uint32_t scsu_window_position = 0;
3154 uint8_t byte_value1 = 0;
3155 uint8_t byte_value2 = 0;
3156 uint8_t byte_value3 = 0;
3157 uint8_t dynamic_window_position_index = 0;
3158 uint8_t in_unicode_mode = 0;
3159 uint8_t unicode_character_set = 0;
3160
3161 if( scsu_stream == NULL )
3162 {
3163 libcerror_error_set(
3164 error,
3165 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3166 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3167 "%s: invalid SCSU stream.",
3168 function );
3169
3170 return( -1 );
3171 }
3172 if( scsu_stream_size > (size_t) SSIZE_MAX )
3173 {
3174 libcerror_error_set(
3175 error,
3176 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3177 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3178 "%s: invalid SCSU stream size value exceeds maximum.",
3179 function );
3180
3181 return( -1 );
3182 }
3183 if( utf16_string_size == NULL )
3184 {
3185 libcerror_error_set(
3186 error,
3187 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3188 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3189 "%s: invalid UTF-16 string size.",
3190 function );
3191
3192 return( -1 );
3193 }
3194 *utf16_string_size = 0;
3195
3196 if( scsu_stream_size == 0 )
3197 {
3198 return( 1 );
3199 }
3200 while( scsu_stream_index < scsu_stream_size )
3201 {
3202 unicode_character_set = 0;
3203
3204 if( scsu_stream_index >= scsu_stream_size )
3205 {
3206 libcerror_error_set(
3207 error,
3208 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3209 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3210 "%s: SCSU stream too small.",
3211 function );
3212
3213 return( -1 );
3214 }
3215 byte_value1 = scsu_stream[ scsu_stream_index++ ];
3216
3217 if( in_unicode_mode != 0 )
3218 {
3219 if( ( byte_value1 <= 0xdf )
3220 || ( byte_value1 >= 0xf3 ) )
3221 {
3222 if( scsu_stream_index >= scsu_stream_size )
3223 {
3224 libcerror_error_set(
3225 error,
3226 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3227 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3228 "%s: SCSU stream too small.",
3229 function );
3230
3231 return( -1 );
3232 }
3233 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3234
3235 unicode_character = byte_value1;
3236 unicode_character <<= 8;
3237 unicode_character |= byte_value2;
3238
3239 unicode_character_set = 1;
3240 }
3241 /* UCn tags
3242 */
3243 else if( ( byte_value1 >= 0xe0 )
3244 && ( byte_value1 <= 0xe7 ) )
3245 {
3246 dynamic_window_position_index = byte_value1 - 0xe0;
3247
3248 in_unicode_mode = 0;
3249 }
3250 /* UDn tags
3251 */
3252 else if( ( byte_value1 >= 0xe8 )
3253 && ( byte_value1 <= 0xef ) )
3254 {
3255 if( scsu_stream_index >= scsu_stream_size )
3256 {
3257 libcerror_error_set(
3258 error,
3259 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3260 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3261 "%s: SCSU stream too small.",
3262 function );
3263
3264 return( -1 );
3265 }
3266 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3267
3268 dynamic_window_position_index = byte_value1 - 0xe8;
3269 scsu_window_position = libuna_scsu_window_offset_table[ byte_value2 ];
3270
3271 scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3272
3273 in_unicode_mode = 0;
3274 }
3275 /* UQU tag
3276 */
3277 else if( byte_value1 == 0xf0 )
3278 {
3279 if( ( scsu_stream_size < 2 )
3280 || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3281 {
3282 libcerror_error_set(
3283 error,
3284 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3285 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3286 "%s: SCSU stream too small.",
3287 function );
3288
3289 return( -1 );
3290 }
3291 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3292 byte_value3 = scsu_stream[ scsu_stream_index++ ];
3293
3294 unicode_character = byte_value2;
3295 unicode_character <<= 8;
3296 unicode_character |= byte_value3;
3297
3298 unicode_character_set = 1;
3299 }
3300 /* UDX tag
3301 */
3302 else if( byte_value1 == 0xf1 )
3303 {
3304 if( ( scsu_stream_size < 2 )
3305 || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
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 byte_value3 = scsu_stream[ scsu_stream_index++ ];
3318
3319 dynamic_window_position_index = byte_value2 >> 5;
3320 scsu_window_position = byte_value2 & 0x1f;
3321 scsu_window_position <<= 8;
3322 scsu_window_position |= byte_value3;
3323 scsu_window_position <<= 7;
3324 scsu_window_position += 0x00010000UL;
3325
3326 scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3327
3328 in_unicode_mode = 0;
3329 }
3330 }
3331 else
3332 {
3333 if( ( byte_value1 == 0x00 )
3334 || ( byte_value1 == 0x09 )
3335 || ( byte_value1 == 0x0a )
3336 || ( byte_value1 == 0x0c )
3337 || ( byte_value1 == 0x0d )
3338 || ( ( byte_value1 >= 0x20 )
3339 && ( byte_value1 <= 0x7f ) ) )
3340 {
3341 unicode_character = byte_value1;
3342
3343 unicode_character_set = 1;
3344 }
3345 /* SQn tags
3346 */
3347 else if( ( byte_value1 >= 0x01 )
3348 && ( byte_value1 <= 0x08 ) )
3349 {
3350 if( scsu_stream_index >= scsu_stream_size )
3351 {
3352 libcerror_error_set(
3353 error,
3354 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3355 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3356 "%s: SCSU stream too small.",
3357 function );
3358
3359 return( -1 );
3360 }
3361 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3362
3363 unicode_character = byte_value2;
3364
3365 if( byte_value2 < 0x80 )
3366 {
3367 unicode_character += libuna_scsu_static_window_positions[ byte_value1 - 0x01 ];
3368 }
3369 else
3370 {
3371 unicode_character -= 0x80;
3372 unicode_character += scsu_dynamic_window_positions[ byte_value1 - 0x01 ];
3373 }
3374 unicode_character_set = 1;
3375 }
3376 /* SDX tag
3377 */
3378 else if( byte_value1 == 0x0b )
3379 {
3380 if( ( scsu_stream_size < 2 )
3381 || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3382 {
3383 libcerror_error_set(
3384 error,
3385 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3386 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3387 "%s: SCSU stream too small.",
3388 function );
3389
3390 return( -1 );
3391 }
3392 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3393 byte_value3 = scsu_stream[ scsu_stream_index++ ];
3394
3395 dynamic_window_position_index = byte_value2 >> 5;
3396 scsu_window_position = byte_value2 & 0x1f;
3397 scsu_window_position <<= 8;
3398 scsu_window_position |= byte_value3;
3399 scsu_window_position <<= 7;
3400 scsu_window_position += 0x00010000UL;
3401
3402 scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3403 }
3404 /* SQU tag
3405 */
3406 else if( byte_value1 == 0x0e )
3407 {
3408 if( ( scsu_stream_size < 2 )
3409 || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3410 {
3411 libcerror_error_set(
3412 error,
3413 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3414 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3415 "%s: SCSU stream too small.",
3416 function );
3417
3418 return( -1 );
3419 }
3420 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3421 byte_value3 = scsu_stream[ scsu_stream_index++ ];
3422
3423 unicode_character = byte_value2;
3424 unicode_character <<= 8;
3425 unicode_character |= byte_value3;
3426
3427 unicode_character_set = 1;
3428 }
3429 /* SCU tag
3430 */
3431 else if( byte_value1 == 0x0f )
3432 {
3433 in_unicode_mode = 1;
3434 }
3435 /* SCn tags
3436 */
3437 else if( ( byte_value1 >= 0x10 )
3438 && ( byte_value1 <= 0x17 ) )
3439 {
3440 dynamic_window_position_index = byte_value1 - 0x10;
3441 }
3442 /* SDn tags
3443 */
3444 else if( ( byte_value1 >= 0x18 )
3445 && ( byte_value1 <= 0x1f ) )
3446 {
3447 if( scsu_stream_index >= scsu_stream_size )
3448 {
3449 libcerror_error_set(
3450 error,
3451 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3452 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3453 "%s: SCSU stream too small.",
3454 function );
3455
3456 return( -1 );
3457 }
3458 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3459
3460 dynamic_window_position_index = byte_value1 - 0x18;
3461 scsu_window_position = libuna_scsu_window_offset_table[ byte_value2 ];
3462
3463 scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3464 }
3465 else if( byte_value1 >= 0x80 )
3466 {
3467 unicode_character = byte_value1 - 0x80;
3468 unicode_character += scsu_dynamic_window_positions[ dynamic_window_position_index ];
3469
3470 unicode_character_set = 1;
3471 }
3472 }
3473 if( unicode_character_set != 0 )
3474 {
3475 /* Determine how many UTF-16 character bytes are required
3476 */
3477 if( libuna_unicode_character_size_to_utf16(
3478 unicode_character,
3479 utf16_string_size,
3480 error ) != 1 )
3481 {
3482 libcerror_error_set(
3483 error,
3484 LIBCERROR_ERROR_DOMAIN_CONVERSION,
3485 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
3486 "%s: unable to unable to determine size of Unicode character in UTF-16.",
3487 function );
3488
3489 return( -1 );
3490 }
3491 if( unicode_character == 0 )
3492 {
3493 break;
3494 }
3495 }
3496 }
3497 /* Check if the string is terminated with an end-of-string character
3498 */
3499 if( unicode_character != 0 )
3500 {
3501 *utf16_string_size += 1;
3502 }
3503 return( 1 );
3504 }
3505
3506 /* Copies an UTF-16 string from a Standard Compression Scheme for Unicode (SCSU) stream
3507 * Returns 1 if successful or -1 on error
3508 */
3509 int libuna_utf16_string_copy_from_scsu_stream(
3510 libuna_utf16_character_t *utf16_string,
3511 size_t utf16_string_size,
3512 const uint8_t *scsu_stream,
3513 size_t scsu_stream_size,
3514 libcerror_error_t **error )
3515 {
3516 static char *function = "libuna_utf16_string_copy_from_scsu_stream";
3517 size_t utf16_string_index = 0;
3518
3519 if( libuna_utf16_string_with_index_copy_from_scsu_stream(
3520 utf16_string,
3521 utf16_string_size,
3522 &utf16_string_index,
3523 scsu_stream,
3524 scsu_stream_size,
3525 error ) != 1 )
3526 {
3527 libcerror_error_set(
3528 error,
3529 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3530 LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
3531 "%s: unable to SCSU stream to UTF-16 string.",
3532 function );
3533
3534 return( -1 );
3535 }
3536 return( 1 );
3537 }
3538
3539 /* Copies an UTF-16 string from a Standard Compression Scheme for Unicode (SCSU) stream
3540 * Returns 1 if successful or -1 on error
3541 */
3542 int libuna_utf16_string_with_index_copy_from_scsu_stream(
3543 libuna_utf16_character_t *utf16_string,
3544 size_t utf16_string_size,
3545 size_t *utf16_string_index,
3546 const uint8_t *scsu_stream,
3547 size_t scsu_stream_size,
3548 libcerror_error_t **error )
3549 {
3550 uint32_t scsu_dynamic_window_positions[ 8 ] = {
3551 0x0080, 0x00c0, 0x0400, 0x0600, 0x0900, 0x3040, 0x30a0, 0xff00 };
3552
3553 static char *function = "libuna_utf16_string_with_index_copy_from_scsu_stream";
3554 libuna_unicode_character_t unicode_character = 0;
3555 size_t scsu_stream_index = 0;
3556 uint32_t scsu_window_position = 0;
3557 uint8_t byte_value1 = 0;
3558 uint8_t byte_value2 = 0;
3559 uint8_t byte_value3 = 0;
3560 uint8_t dynamic_window_position_index = 0;
3561 uint8_t in_unicode_mode = 0;
3562 uint8_t unicode_character_set = 0;
3563
3564 if( utf16_string == NULL )
3565 {
3566 libcerror_error_set(
3567 error,
3568 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3569 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3570 "%s: invalid UTF-16 string.",
3571 function );
3572
3573 return( -1 );
3574 }
3575 if( utf16_string_size > (size_t) SSIZE_MAX )
3576 {
3577 libcerror_error_set(
3578 error,
3579 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3580 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3581 "%s: invalid UTF-16 string size value exceeds maximum.",
3582 function );
3583
3584 return( -1 );
3585 }
3586 if( utf16_string_index == NULL )
3587 {
3588 libcerror_error_set(
3589 error,
3590 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3591 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3592 "%s: invalid UTF-16 string index.",
3593 function );
3594
3595 return( -1 );
3596 }
3597 if( scsu_stream == NULL )
3598 {
3599 libcerror_error_set(
3600 error,
3601 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3602 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3603 "%s: invalid SCSU stream.",
3604 function );
3605
3606 return( -1 );
3607 }
3608 if( scsu_stream_size > (size_t) SSIZE_MAX )
3609 {
3610 libcerror_error_set(
3611 error,
3612 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3613 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3614 "%s: invalid SCSU stream size value exceeds maximum.",
3615 function );
3616
3617 return( -1 );
3618 }
3619 if( scsu_stream_size == 0 )
3620 {
3621 libcerror_error_set(
3622 error,
3623 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3624 LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
3625 "%s: missing SCSU stream value.",
3626 function );
3627
3628 return( -1 );
3629 }
3630 while( scsu_stream_index < scsu_stream_size )
3631 {
3632 unicode_character_set = 0;
3633
3634 if( scsu_stream_index >= scsu_stream_size )
3635 {
3636 libcerror_error_set(
3637 error,
3638 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3639 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3640 "%s: SCSU stream too small.",
3641 function );
3642
3643 return( -1 );
3644 }
3645 byte_value1 = scsu_stream[ scsu_stream_index++ ];
3646
3647 if( in_unicode_mode != 0 )
3648 {
3649 if( ( byte_value1 <= 0xdf )
3650 || ( byte_value1 >= 0xf3 ) )
3651 {
3652 if( scsu_stream_index >= scsu_stream_size )
3653 {
3654 libcerror_error_set(
3655 error,
3656 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3657 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3658 "%s: SCSU stream too small.",
3659 function );
3660
3661 return( -1 );
3662 }
3663 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3664
3665 unicode_character = byte_value1;
3666 unicode_character <<= 8;
3667 unicode_character |= byte_value2;
3668
3669 unicode_character_set = 1;
3670 }
3671 /* UCn tags
3672 */
3673 else if( ( byte_value1 >= 0xe0 )
3674 && ( byte_value1 <= 0xe7 ) )
3675 {
3676 dynamic_window_position_index = byte_value1 - 0xe0;
3677
3678 in_unicode_mode = 0;
3679 }
3680 /* UDn tags
3681 */
3682 else if( ( byte_value1 >= 0xe8 )
3683 && ( byte_value1 <= 0xef ) )
3684 {
3685 if( scsu_stream_index >= scsu_stream_size )
3686 {
3687 libcerror_error_set(
3688 error,
3689 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3690 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3691 "%s: SCSU stream too small.",
3692 function );
3693
3694 return( -1 );
3695 }
3696 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3697
3698 dynamic_window_position_index = byte_value1 - 0xe8;
3699 scsu_window_position = libuna_scsu_window_offset_table[ byte_value2 ];
3700
3701 scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3702
3703 in_unicode_mode = 0;
3704 }
3705 /* UQU tag
3706 */
3707 else if( byte_value1 == 0xf0 )
3708 {
3709 if( ( scsu_stream_size < 2 )
3710 || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3711 {
3712 libcerror_error_set(
3713 error,
3714 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3715 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3716 "%s: SCSU stream too small.",
3717 function );
3718
3719 return( -1 );
3720 }
3721 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3722 byte_value3 = scsu_stream[ scsu_stream_index++ ];
3723
3724 unicode_character = byte_value2;
3725 unicode_character <<= 8;
3726 unicode_character |= byte_value3;
3727
3728 unicode_character_set = 1;
3729 }
3730 /* UDX tag
3731 */
3732 else if( byte_value1 == 0xf1 )
3733 {
3734 if( ( scsu_stream_size < 2 )
3735 || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
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 byte_value3 = scsu_stream[ scsu_stream_index++ ];
3748
3749 dynamic_window_position_index = byte_value2 >> 5;
3750 scsu_window_position = byte_value2 & 0x1f;
3751 scsu_window_position <<= 8;
3752 scsu_window_position |= byte_value3;
3753 scsu_window_position <<= 7;
3754 scsu_window_position += 0x00010000UL;
3755
3756 scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3757
3758 in_unicode_mode = 0;
3759 }
3760 }
3761 else
3762 {
3763 if( ( byte_value1 == 0x00 )
3764 || ( byte_value1 == 0x09 )
3765 || ( byte_value1 == 0x0a )
3766 || ( byte_value1 == 0x0c )
3767 || ( byte_value1 == 0x0d )
3768 || ( ( byte_value1 >= 0x20 )
3769 && ( byte_value1 <= 0x7f ) ) )
3770 {
3771 unicode_character = byte_value1;
3772
3773 unicode_character_set = 1;
3774 }
3775 /* SQn tags
3776 */
3777 else if( ( byte_value1 >= 0x01 )
3778 && ( byte_value1 <= 0x08 ) )
3779 {
3780 if( scsu_stream_index >= scsu_stream_size )
3781 {
3782 libcerror_error_set(
3783 error,
3784 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3785 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3786 "%s: SCSU stream too small.",
3787 function );
3788
3789 return( -1 );
3790 }
3791 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3792
3793 unicode_character = byte_value2;
3794
3795 if( byte_value2 < 0x80 )
3796 {
3797 unicode_character += libuna_scsu_static_window_positions[ byte_value1 - 0x01 ];
3798 }
3799 else
3800 {
3801 unicode_character -= 0x80;
3802 unicode_character += scsu_dynamic_window_positions[ byte_value1 - 0x01 ];
3803 }
3804 unicode_character_set = 1;
3805 }
3806 /* SDX tag
3807 */
3808 else if( byte_value1 == 0x0b )
3809 {
3810 if( ( scsu_stream_size < 2 )
3811 || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3812 {
3813 libcerror_error_set(
3814 error,
3815 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3816 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3817 "%s: SCSU stream too small.",
3818 function );
3819
3820 return( -1 );
3821 }
3822 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3823 byte_value3 = scsu_stream[ scsu_stream_index++ ];
3824
3825 dynamic_window_position_index = byte_value2 >> 5;
3826 scsu_window_position = byte_value2 & 0x1f;
3827 scsu_window_position <<= 8;
3828 scsu_window_position |= byte_value3;
3829 scsu_window_position <<= 7;
3830 scsu_window_position += 0x00010000UL;
3831
3832 scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3833 }
3834 /* SQU tag
3835 */
3836 else if( byte_value1 == 0x0e )
3837 {
3838 if( ( scsu_stream_size < 2 )
3839 || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) )
3840 {
3841 libcerror_error_set(
3842 error,
3843 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3844 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3845 "%s: SCSU stream too small.",
3846 function );
3847
3848 return( -1 );
3849 }
3850 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3851 byte_value3 = scsu_stream[ scsu_stream_index++ ];
3852
3853 unicode_character = byte_value2;
3854 unicode_character <<= 8;
3855 unicode_character |= byte_value3;
3856
3857 unicode_character_set = 1;
3858 }
3859 /* SCU tag
3860 */
3861 else if( byte_value1 == 0x0f )
3862 {
3863 in_unicode_mode = 1;
3864 }
3865 /* SCn tags
3866 */
3867 else if( ( byte_value1 >= 0x10 )
3868 && ( byte_value1 <= 0x17 ) )
3869 {
3870 dynamic_window_position_index = byte_value1 - 0x10;
3871 }
3872 /* SDn tags
3873 */
3874 else if( ( byte_value1 >= 0x18 )
3875 && ( byte_value1 <= 0x1f ) )
3876 {
3877 if( scsu_stream_index >= scsu_stream_size )
3878 {
3879 libcerror_error_set(
3880 error,
3881 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3882 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3883 "%s: SCSU stream too small.",
3884 function );
3885
3886 return( -1 );
3887 }
3888 byte_value2 = scsu_stream[ scsu_stream_index++ ];
3889
3890 dynamic_window_position_index = byte_value1 - 0x18;
3891 scsu_window_position = libuna_scsu_window_offset_table[ byte_value2 ];
3892
3893 scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position;
3894 }
3895 else if( byte_value1 >= 0x80 )
3896 {
3897 unicode_character = byte_value1 - 0x80;
3898 unicode_character += scsu_dynamic_window_positions[ dynamic_window_position_index ];
3899
3900 unicode_character_set = 1;
3901 }
3902 }
3903 if( unicode_character_set != 0 )
3904 {
3905 /* Convert the Unicode character into UTF-16 character bytes
3906 */
3907 if( libuna_unicode_character_copy_to_utf16(
3908 unicode_character,
3909 utf16_string,
3910 utf16_string_size,
3911 utf16_string_index,
3912 error ) != 1 )
3913 {
3914 libcerror_error_set(
3915 error,
3916 LIBCERROR_ERROR_DOMAIN_CONVERSION,
3917 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
3918 "%s: unable to copy Unicode character to UTF-16.",
3919 function );
3920
3921 return( -1 );
3922 }
3923 if( unicode_character == 0 )
3924 {
3925 break;
3926 }
3927 }
3928 }
3929 /* Check if the string is terminated with an end-of-string character
3930 */
3931 if( unicode_character != 0 )
3932 {
3933 if( *utf16_string_index >= utf16_string_size )
3934 {
3935 libcerror_error_set(
3936 error,
3937 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3938 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3939 "%s: UTF-16 string too small.",
3940 function );
3941
3942 return( -1 );
3943 }
3944 utf16_string[ *utf16_string_index ] = 0;
3945
3946 *utf16_string_index += 1;
3947 }
3948 return( 1 );
3949 }
3950