Debian Codegraph @

libuna/libuna_utf16_stream.c

raw symbols SCIP
1 /*
2 * UTF-16 stream 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_types.h"
28 #include "libuna_unicode_character.h"
29 #include "libuna_utf16_stream.h"
30
31 /* Copies an UTF-16 stream byte order mark (BOM)
32 * Returns 1 if successful or -1 on error
33 */
34 int libuna_utf16_stream_copy_byte_order_mark(
35 uint8_t *utf16_stream,
36 size_t utf16_stream_size,
37 size_t *utf16_stream_index,
38 int byte_order,
39 libcerror_error_t **error )
40 {
41 static char *function = "libuna_utf16_stream_copy_byte_order_mark";
42
43 if( utf16_stream == NULL )
44 {
45 libcerror_error_set(
46 error,
47 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
48 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
49 "%s: invalid UTF-16 stream.",
50 function );
51
52 return( -1 );
53 }
54 if( utf16_stream_size > (size_t) SSIZE_MAX )
55 {
56 libcerror_error_set(
57 error,
58 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
59 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
60 "%s: invalid UTF-16 stream size value exceeds maximum.",
61 function );
62
63 return( -1 );
64 }
65 if( utf16_stream_index == NULL )
66 {
67 libcerror_error_set(
68 error,
69 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
70 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
71 "%s: invalid UTF-16 stream index.",
72 function );
73
74 return( -1 );
75 }
76 if( ( *utf16_stream_index + 2 ) > utf16_stream_size )
77 {
78 libcerror_error_set(
79 error,
80 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
81 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
82 "%s: UTF-16 stream too small.",
83 function );
84
85 return( -1 );
86 }
87 if( ( byte_order != LIBUNA_ENDIAN_BIG )
88 && ( byte_order != LIBUNA_ENDIAN_LITTLE ) )
89 {
90 libcerror_error_set(
91 error,
92 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
93 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
94 "%s: unsupported byte order.",
95 function );
96
97 return( -1 );
98 }
99 if( byte_order == LIBUNA_ENDIAN_BIG )
100 {
101 utf16_stream[ *utf16_stream_index ] = 0xfe;
102 utf16_stream[ *utf16_stream_index + 1 ] = 0xff;
103 }
104 else if( byte_order == LIBUNA_ENDIAN_LITTLE )
105 {
106 utf16_stream[ *utf16_stream_index ] = 0xff;
107 utf16_stream[ *utf16_stream_index + 1 ] = 0xfe;
108 }
109 *utf16_stream_index += 2;
110
111 return( 1 );
112 }
113
114 /* Determines the size of an UTF-16 stream from an UTF-8 string
115 * Returns 1 if successful or -1 on error
116 */
117 int libuna_utf16_stream_size_from_utf8(
118 const libuna_utf8_character_t *utf8_string,
119 size_t utf8_string_size,
120 size_t *utf16_stream_size,
121 libcerror_error_t **error )
122 {
123 static char *function = "libuna_utf16_stream_size_from_utf8";
124 libuna_unicode_character_t unicode_character = 0;
125 size_t safe_utf16_stream_size = 0;
126 size_t utf8_string_index = 0;
127
128 if( utf8_string == NULL )
129 {
130 libcerror_error_set(
131 error,
132 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
133 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
134 "%s: invalid UTF-8 string.",
135 function );
136
137 return( -1 );
138 }
139 if( utf8_string_size > (size_t) SSIZE_MAX )
140 {
141 libcerror_error_set(
142 error,
143 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
144 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
145 "%s: invalid UTF-8 string size value exceeds maximum.",
146 function );
147
148 return( -1 );
149 }
150 if( utf16_stream_size == NULL )
151 {
152 libcerror_error_set(
153 error,
154 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
155 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
156 "%s: invalid UTF-16 stream size.",
157 function );
158
159 return( -1 );
160 }
161 /* Add the byte order mark
162 */
163 safe_utf16_stream_size = 1;
164
165 while( utf8_string_index < utf8_string_size )
166 {
167 /* Convert the UTF-8 character bytes into an Unicode character
168 */
169 if( libuna_unicode_character_copy_from_utf8(
170 &unicode_character,
171 utf8_string,
172 utf8_string_size,
173 &utf8_string_index,
174 error ) != 1 )
175 {
176 libcerror_error_set(
177 error,
178 LIBCERROR_ERROR_DOMAIN_CONVERSION,
179 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
180 "%s: unable to copy Unicode character from UTF-8.",
181 function );
182
183 return( -1 );
184 }
185 /* Determine how many UTF-16 character bytes are required
186 */
187 if( libuna_unicode_character_size_to_utf16(
188 unicode_character,
189 &safe_utf16_stream_size,
190 error ) != 1 )
191 {
192 libcerror_error_set(
193 error,
194 LIBCERROR_ERROR_DOMAIN_CONVERSION,
195 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
196 "%s: unable to determine size of Unicode character in UTF-16.",
197 function );
198
199 return( -1 );
200 }
201 if( unicode_character == 0 )
202 {
203 break;
204 }
205 }
206 /* Convert the number of characters into bytes
207 */
208 *utf16_stream_size = safe_utf16_stream_size * 2;
209
210 return( 1 );
211 }
212
213 /* Copies an UTF-16 stream from an UTF-8 string
214 * Returns 1 if successful or -1 on error
215 */
216 int libuna_utf16_stream_copy_from_utf8(
217 uint8_t *utf16_stream,
218 size_t utf16_stream_size,
219 int byte_order,
220 const libuna_utf8_character_t *utf8_string,
221 size_t utf8_string_size,
222 libcerror_error_t **error )
223 {
224 static char *function = "libuna_utf16_stream_copy_from_utf8";
225 libuna_unicode_character_t unicode_character = 0;
226 size_t utf16_stream_index = 0;
227 size_t utf8_string_index = 0;
228
229 if( utf16_stream == NULL )
230 {
231 libcerror_error_set(
232 error,
233 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
234 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
235 "%s: invalid UTF-16 stream.",
236 function );
237
238 return( -1 );
239 }
240 if( utf16_stream_size > (size_t) SSIZE_MAX )
241 {
242 libcerror_error_set(
243 error,
244 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
245 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
246 "%s: invalid UTF-16 stream size value exceeds maximum.",
247 function );
248
249 return( -1 );
250 }
251 if( ( byte_order != LIBUNA_ENDIAN_BIG )
252 && ( byte_order != LIBUNA_ENDIAN_LITTLE ) )
253 {
254 libcerror_error_set(
255 error,
256 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
257 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
258 "%s: unsupported byte order.",
259 function );
260
261 return( -1 );
262 }
263 if( utf8_string == NULL )
264 {
265 libcerror_error_set(
266 error,
267 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
268 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
269 "%s: invalid UTF-8 string.",
270 function );
271
272 return( -1 );
273 }
274 if( utf8_string_size > (size_t) SSIZE_MAX )
275 {
276 libcerror_error_set(
277 error,
278 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
279 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
280 "%s: invalid UTF-8 string size value exceeds maximum.",
281 function );
282
283 return( -1 );
284 }
285 if( libuna_utf16_stream_copy_byte_order_mark(
286 utf16_stream,
287 utf16_stream_size,
288 &utf16_stream_index,
289 byte_order,
290 error ) != 1 )
291 {
292 libcerror_error_set(
293 error,
294 LIBCERROR_ERROR_DOMAIN_CONVERSION,
295 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
296 "%s: unable to copy UTF-16 byte order mark.",
297 function );
298
299 return( -1 );
300 }
301 while( utf8_string_index < utf8_string_size )
302 {
303 /* Convert the UTF-8 string bytes into an Unicode character
304 */
305 if( libuna_unicode_character_copy_from_utf8(
306 &unicode_character,
307 utf8_string,
308 utf8_string_size,
309 &utf8_string_index,
310 error ) != 1 )
311 {
312 libcerror_error_set(
313 error,
314 LIBCERROR_ERROR_DOMAIN_CONVERSION,
315 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
316 "%s: unable to copy Unicode character from UTF-8 string.",
317 function );
318
319 return( -1 );
320 }
321 /* Convert the Unicode character into UTF-16 stream bytes
322 */
323 if( libuna_unicode_character_copy_to_utf16_stream(
324 unicode_character,
325 utf16_stream,
326 utf16_stream_size,
327 &utf16_stream_index,
328 byte_order,
329 error ) != 1 )
330 {
331 libcerror_error_set(
332 error,
333 LIBCERROR_ERROR_DOMAIN_CONVERSION,
334 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
335 "%s: unable to copy Unicode character to UTF-16 stream.",
336 function );
337
338 return( -1 );
339 }
340 if( unicode_character == 0 )
341 {
342 break;
343 }
344 }
345 return( 1 );
346 }
347
348 /* Determines the size of an UTF-16 stream from an UTF-16 string
349 * Returns 1 if successful or -1 on error
350 */
351 int libuna_utf16_stream_size_from_utf16(
352 const libuna_utf16_character_t *utf16_string,
353 size_t utf16_string_size,
354 size_t *utf16_stream_size,
355 libcerror_error_t **error )
356 {
357 static char *function = "libuna_utf16_stream_size_from_utf16";
358 libuna_unicode_character_t unicode_character = 0;
359 size_t safe_utf16_stream_size = 0;
360 size_t utf16_string_index = 0;
361
362 if( utf16_string == NULL )
363 {
364 libcerror_error_set(
365 error,
366 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
367 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
368 "%s: invalid UTF-16 string.",
369 function );
370
371 return( -1 );
372 }
373 if( utf16_string_size > (size_t) SSIZE_MAX )
374 {
375 libcerror_error_set(
376 error,
377 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
378 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
379 "%s: invalid UTF-16 string size value exceeds maximum.",
380 function );
381
382 return( -1 );
383 }
384 if( utf16_stream_size == NULL )
385 {
386 libcerror_error_set(
387 error,
388 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
389 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
390 "%s: invalid UTF-16 stream size.",
391 function );
392
393 return( -1 );
394 }
395 /* Add the byte order mark
396 */
397 safe_utf16_stream_size = 1;
398
399 while( utf16_string_index < utf16_string_size )
400 {
401 /* Convert the UTF-16 character bytes into an Unicode character
402 */
403 if( libuna_unicode_character_copy_from_utf16(
404 &unicode_character,
405 utf16_string,
406 utf16_string_size,
407 &utf16_string_index,
408 error ) != 1 )
409 {
410 libcerror_error_set(
411 error,
412 LIBCERROR_ERROR_DOMAIN_CONVERSION,
413 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
414 "%s: unable to copy Unicode character from UTF-16.",
415 function );
416
417 return( -1 );
418 }
419 /* Determine how many UTF-16 character bytes are required
420 */
421 if( libuna_unicode_character_size_to_utf16(
422 unicode_character,
423 &safe_utf16_stream_size,
424 error ) != 1 )
425 {
426 libcerror_error_set(
427 error,
428 LIBCERROR_ERROR_DOMAIN_CONVERSION,
429 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
430 "%s: unable to determine size of Unicode character in UTF-16.",
431 function );
432
433 return( -1 );
434 }
435 if( unicode_character == 0 )
436 {
437 break;
438 }
439 }
440 /* Convert the number of characters into bytes
441 */
442 *utf16_stream_size = safe_utf16_stream_size * 2;
443
444 return( 1 );
445 }
446
447 /* Copies an UTF-16 stream from an UTF-16 string
448 * Returns 1 if successful or -1 on error
449 */
450 int libuna_utf16_stream_copy_from_utf16(
451 uint8_t *utf16_stream,
452 size_t utf16_stream_size,
453 int byte_order,
454 const libuna_utf16_character_t *utf16_string,
455 size_t utf16_string_size,
456 libcerror_error_t **error )
457 {
458 static char *function = "libuna_utf16_stream_copy_from_utf16";
459 libuna_unicode_character_t unicode_character = 0;
460 size_t utf16_stream_index = 0;
461 size_t utf16_string_index = 0;
462
463 if( utf16_stream == NULL )
464 {
465 libcerror_error_set(
466 error,
467 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
468 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
469 "%s: invalid UTF-16 stream.",
470 function );
471
472 return( -1 );
473 }
474 if( utf16_stream_size > (size_t) SSIZE_MAX )
475 {
476 libcerror_error_set(
477 error,
478 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
479 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
480 "%s: invalid UTF-16 stream size value exceeds maximum.",
481 function );
482
483 return( -1 );
484 }
485 if( ( byte_order != LIBUNA_ENDIAN_BIG )
486 && ( byte_order != LIBUNA_ENDIAN_LITTLE ) )
487 {
488 libcerror_error_set(
489 error,
490 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
491 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
492 "%s: unsupported byte order.",
493 function );
494
495 return( -1 );
496 }
497 if( utf16_string == NULL )
498 {
499 libcerror_error_set(
500 error,
501 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
502 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
503 "%s: invalid UTF-16 string.",
504 function );
505
506 return( -1 );
507 }
508 if( utf16_string_size > (size_t) SSIZE_MAX )
509 {
510 libcerror_error_set(
511 error,
512 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
513 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
514 "%s: invalid UTF-16 string size value exceeds maximum.",
515 function );
516
517 return( -1 );
518 }
519 if( libuna_utf16_stream_copy_byte_order_mark(
520 utf16_stream,
521 utf16_stream_size,
522 &utf16_stream_index,
523 byte_order,
524 error ) != 1 )
525 {
526 libcerror_error_set(
527 error,
528 LIBCERROR_ERROR_DOMAIN_CONVERSION,
529 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
530 "%s: unable to copy UTF-16 byte order mark.",
531 function );
532
533 return( -1 );
534 }
535 while( utf16_string_index < utf16_string_size )
536 {
537 /* Convert the UTF-16 string bytes into an Unicode character
538 */
539 if( libuna_unicode_character_copy_from_utf16(
540 &unicode_character,
541 utf16_string,
542 utf16_string_size,
543 &utf16_string_index,
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 copy Unicode character from UTF-16 string.",
551 function );
552
553 return( -1 );
554 }
555 /* Convert the Unicode character into UTF-16 stream bytes
556 */
557 if( libuna_unicode_character_copy_to_utf16_stream(
558 unicode_character,
559 utf16_stream,
560 utf16_stream_size,
561 &utf16_stream_index,
562 byte_order,
563 error ) != 1 )
564 {
565 libcerror_error_set(
566 error,
567 LIBCERROR_ERROR_DOMAIN_CONVERSION,
568 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
569 "%s: unable to copy Unicode character to UTF-16 stream.",
570 function );
571
572 return( -1 );
573 }
574 if( unicode_character == 0 )
575 {
576 break;
577 }
578 }
579 return( 1 );
580 }
581
582 /* Determines the size of an UTF-16 stream from an UTF-32 string
583 * Returns 1 if successful or -1 on error
584 */
585 int libuna_utf16_stream_size_from_utf32(
586 const libuna_utf32_character_t *utf32_string,
587 size_t utf32_string_size,
588 size_t *utf16_stream_size,
589 libcerror_error_t **error )
590 {
591 static char *function = "libuna_utf16_stream_size_from_utf32";
592 libuna_unicode_character_t unicode_character = 0;
593 size_t safe_utf16_stream_size = 0;
594 size_t utf32_string_index = 0;
595
596 if( utf32_string == NULL )
597 {
598 libcerror_error_set(
599 error,
600 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
601 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
602 "%s: invalid UTF-32 string.",
603 function );
604
605 return( -1 );
606 }
607 if( utf32_string_size > (size_t) SSIZE_MAX )
608 {
609 libcerror_error_set(
610 error,
611 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
612 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
613 "%s: invalid UTF-32 string size value exceeds maximum.",
614 function );
615
616 return( -1 );
617 }
618 if( utf16_stream_size == NULL )
619 {
620 libcerror_error_set(
621 error,
622 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
623 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
624 "%s: invalid UTF-16 stream size.",
625 function );
626
627 return( -1 );
628 }
629 /* Add the byte order mark
630 */
631 safe_utf16_stream_size += 1;
632
633 while( utf32_string_index < utf32_string_size )
634 {
635 /* Convert the UTF-32 character bytes into an Unicode character
636 */
637 if( libuna_unicode_character_copy_from_utf32(
638 &unicode_character,
639 utf32_string,
640 utf32_string_size,
641 &utf32_string_index,
642 error ) != 1 )
643 {
644 libcerror_error_set(
645 error,
646 LIBCERROR_ERROR_DOMAIN_CONVERSION,
647 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
648 "%s: unable to copy Unicode character from UTF-32.",
649 function );
650
651 return( -1 );
652 }
653 /* Determine how many UTF-16 character bytes are required
654 */
655 if( libuna_unicode_character_size_to_utf16(
656 unicode_character,
657 &safe_utf16_stream_size,
658 error ) != 1 )
659 {
660 libcerror_error_set(
661 error,
662 LIBCERROR_ERROR_DOMAIN_CONVERSION,
663 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
664 "%s: unable to determine size of Unicode character in UTF-16.",
665 function );
666
667 return( -1 );
668 }
669 if( unicode_character == 0 )
670 {
671 break;
672 }
673 }
674 /* Convert the number of characters into bytes
675 */
676 *utf16_stream_size = safe_utf16_stream_size * 2;
677
678 return( 1 );
679 }
680
681 /* Copies an UTF-16 stream from an UTF-32 string
682 * Returns 1 if successful or -1 on error
683 */
684 int libuna_utf16_stream_copy_from_utf32(
685 uint8_t *utf16_stream,
686 size_t utf16_stream_size,
687 int byte_order,
688 const libuna_utf32_character_t *utf32_string,
689 size_t utf32_string_size,
690 libcerror_error_t **error )
691 {
692 static char *function = "libuna_utf16_stream_copy_from_utf32";
693 libuna_unicode_character_t unicode_character = 0;
694 size_t utf16_stream_index = 0;
695 size_t utf32_string_index = 0;
696
697 if( utf16_stream == NULL )
698 {
699 libcerror_error_set(
700 error,
701 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
702 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
703 "%s: invalid UTF-16 stream.",
704 function );
705
706 return( -1 );
707 }
708 if( utf16_stream_size > (size_t) SSIZE_MAX )
709 {
710 libcerror_error_set(
711 error,
712 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
713 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
714 "%s: invalid UTF-16 stream size value exceeds maximum.",
715 function );
716
717 return( -1 );
718 }
719 if( ( byte_order != LIBUNA_ENDIAN_BIG )
720 && ( byte_order != LIBUNA_ENDIAN_LITTLE ) )
721 {
722 libcerror_error_set(
723 error,
724 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
725 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
726 "%s: unsupported byte order.",
727 function );
728
729 return( -1 );
730 }
731 if( utf32_string == NULL )
732 {
733 libcerror_error_set(
734 error,
735 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
736 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
737 "%s: invalid UTF-32 string.",
738 function );
739
740 return( -1 );
741 }
742 if( utf32_string_size > (size_t) SSIZE_MAX )
743 {
744 libcerror_error_set(
745 error,
746 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
747 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
748 "%s: invalid UTF-32 string size value exceeds maximum.",
749 function );
750
751 return( -1 );
752 }
753 if( libuna_utf16_stream_copy_byte_order_mark(
754 utf16_stream,
755 utf16_stream_size,
756 &utf16_stream_index,
757 byte_order,
758 error ) != 1 )
759 {
760 libcerror_error_set(
761 error,
762 LIBCERROR_ERROR_DOMAIN_CONVERSION,
763 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
764 "%s: unable to copy UTF-16 byte order mark.",
765 function );
766
767 return( -1 );
768 }
769 while( utf32_string_index < utf32_string_size )
770 {
771 /* Convert the UTF-32 string bytes into an Unicode character
772 */
773 if( libuna_unicode_character_copy_from_utf32(
774 &unicode_character,
775 utf32_string,
776 utf32_string_size,
777 &utf32_string_index,
778 error ) != 1 )
779 {
780 libcerror_error_set(
781 error,
782 LIBCERROR_ERROR_DOMAIN_CONVERSION,
783 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
784 "%s: unable to copy Unicode character from UTF-32 string.",
785 function );
786
787 return( -1 );
788 }
789 /* Convert the Unicode character into UTF-16 stream bytes
790 */
791 if( libuna_unicode_character_copy_to_utf16_stream(
792 unicode_character,
793 utf16_stream,
794 utf16_stream_size,
795 &utf16_stream_index,
796 byte_order,
797 error ) != 1 )
798 {
799 libcerror_error_set(
800 error,
801 LIBCERROR_ERROR_DOMAIN_CONVERSION,
802 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
803 "%s: unable to copy Unicode character to UTF-16 stream.",
804 function );
805
806 return( -1 );
807 }
808 if( unicode_character == 0 )
809 {
810 break;
811 }
812 }
813 return( 1 );
814 }
815