Debian Codegraph @

libuna/libuna_utf8_string.c

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