2 * Copyright 2001-2004 Unicode, Inc.
6 * This source code is provided as is by Unicode, Inc. No claims are
7 * made as to fitness for any particular purpose. No warranties of any
8 * kind are expressed or implied. The recipient agrees to determine
9 * applicability of information provided. If this file has been
10 * purchased on magnetic or optical media from Unicode, Inc., the
11 * sole remedy for any claim will be exchange of defective media
12 * within 90 days of receipt.
14 * Limitations on Rights to Redistribute This Code
16 * Unicode, Inc. hereby grants the right to freely use the information
17 * supplied in this file in the creation of products supporting the
18 * Unicode Standard, and to make copies of this file in any form
19 * for internal or external distribution as long as this notice
23 /* ---------------------------------------------------------------------
25 Conversions between UTF32, UTF-16, and UTF-8. Source code file.
26 Author: Mark E. Davis, 1994.
27 Rev History: Rick McGowan, fixes & updates May 2001.
28 Sept 2001: fixed const & error conditions per
29 mods suggested by S. Parent & A. Lillich.
30 June 2002: Tim Dodd added detection and handling of incomplete
31 source sequences, enhanced error detection, added casts
32 to eliminate compiler warnings.
33 July 2003: slight mods to back out aggressive FFFE detection.
34 Jan 2004: updated switches in from-UTF8 conversions.
35 Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions.
37 See the header file "ConvertUTF.h" for complete documentation.
39 ------------------------------------------------------------------------ */
42 #include "convertUTF.h"
50 /***********************************************************************/
52 static const int halfShift
= 10; /* used for shifting by 10 bits */
54 static const UTF32 halfBase
= 0x0010000UL
;
55 static const UTF32 halfMask
= 0x3FFUL
;
57 #define UNI_SUR_HIGH_START (UTF32)0xD800
58 #define UNI_SUR_HIGH_END (UTF32)0xDBFF
59 #define UNI_SUR_LOW_START (UTF32)0xDC00
60 #define UNI_SUR_LOW_END (UTF32)0xDFFF
62 /***********************************************************************/
65 AROS_LH5(ULONG
, CodesetsConvertUTF32toUTF16
,
66 AROS_LHA(const UTF32
**, sourceStart
, A0
),
67 AROS_LHA(const UTF32
*, sourceEnd
, A1
),
68 AROS_LHA(UTF16
**, targetStart
, A2
),
69 AROS_LHA(UTF16
*, targetEnd
, A3
),
70 AROS_LHA(ULONG
, flags
, D0
),
71 struct LibraryHeader
*, library
, 5, Codesets
77 CodesetsConvertUTF32toUTF16(REG(a0
, const UTF32
** sourceStart
),
78 REG(a1
, const UTF32
* sourceEnd
),
79 REG(a2
, UTF16
** targetStart
),
80 REG(a3
, UTF16
* targetEnd
),
84 ULONG result
= CSR_ConversionOK
;
85 const UTF32
*source
= *sourceStart
;
86 UTF16
*target
= *targetStart
;
90 while(source
< sourceEnd
)
94 if(target
>= targetEnd
)
96 result
= CSR_TargetExhausted
;
101 if(ch
<= UNI_MAX_BMP
)
103 /* Target is a character <= 0xFFFF */
104 /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
105 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
)
107 if(flags
== CSF_StrictConversion
)
109 --source
; /* return to the illegal value itself */
110 result
= CSR_SourceIllegal
;
115 *target
++ = UNI_REPLACEMENT_CHAR
;
120 *target
++ = (UTF16
)ch
; /* normal case */
123 else if(ch
> UNI_MAX_LEGAL_UTF32
)
125 if(flags
== CSF_StrictConversion
)
127 result
= CSR_SourceIllegal
;
131 *target
++ = UNI_REPLACEMENT_CHAR
;
136 /* target is a character in range 0xFFFF - 0x10FFFF. */
137 if(target
+ 1 >= targetEnd
)
139 --source
; /* Back up source pointer! */
140 result
= CSR_TargetExhausted
;
144 *target
++ = (UTF16
) ((ch
>> halfShift
) + UNI_SUR_HIGH_START
);
145 *target
++ = (UTF16
) ((ch
& halfMask
) + UNI_SUR_LOW_START
);
149 *sourceStart
= source
;
150 *targetStart
= target
;
160 LIBSTUB(CodesetsConvertUTF32toUTF16
, ULONG
, REG(a0
, const UTF32
** sourceStart
),
161 REG(a1
, const UTF32
* sourceEnd
),
162 REG(a2
, UTF16
** targetStart
),
163 REG(a3
, UTF16
* targetEnd
),
164 REG(d0
, ULONG flags
))
167 return CodesetsConvertUTF32toUTF16((const UTF32
**)REG_A0
, (const UTF32
*)REG_A1
, (UTF16
**)REG_A2
, (UTF16
*)REG_A3
, (ULONG
)REG_D0
);
169 return CodesetsConvertUTF32toUTF16(sourceStart
, sourceEnd
, targetStart
, targetEnd
, flags
);
174 /***********************************************************************/
177 AROS_LH5(ULONG
, CodesetsConvertUTF16toUTF32
,
178 AROS_LHA(const UTF16
**, sourceStart
, A0
),
179 AROS_LHA(const UTF16
*, sourceEnd
, A1
),
180 AROS_LHA(UTF32
**, targetStart
, A2
),
181 AROS_LHA(UTF32
*, targetEnd
, A3
),
182 AROS_LHA(ULONG
, flags
, D0
),
183 struct LibraryHeader
*, library
, 6, Codesets
189 CodesetsConvertUTF16toUTF32(REG(a0
, const UTF16
** sourceStart
),
190 REG(a1
, const UTF16
* sourceEnd
),
191 REG(a2
, UTF32
** targetStart
),
192 REG(a3
, UTF32
* targetEnd
),
193 REG(d0
, ULONG flags
))
196 ULONG result
= CSR_ConversionOK
;
197 const UTF16
*source
= *sourceStart
;
198 UTF32
*target
= *targetStart
;
203 while(source
< sourceEnd
)
205 const UTF16
*oldSource
= source
; /* In case we have to back up because of target overflow. */
208 /* If we have a surrogate pair, convert to UTF32 first. */
209 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_HIGH_END
)
211 /* If the 16 bits following the high surrogate are in the source buffer... */
212 if(source
< sourceEnd
)
216 /* If it's a low surrogate, convert to UTF32. */
217 if(ch2
>= UNI_SUR_LOW_START
&& ch2
<= UNI_SUR_LOW_END
)
219 ch
= ((ch
- UNI_SUR_HIGH_START
) << halfShift
)
220 + (ch2
- UNI_SUR_LOW_START
) + halfBase
;
224 else if(flags
== CSF_StrictConversion
)
226 /* it's an unpaired high surrogate */
227 --source
; /* return to the illegal value itself */
228 result
= CSR_SourceIllegal
;
235 /* We don't have the 16 bits following the high surrogate. */
236 --source
; /* return to the high surrogate */
237 result
= CSR_SourceExhausted
;
242 else if (flags
== CSF_StrictConversion
)
244 /* UTF-16 surrogate values are illegal in UTF-32 */
245 if(ch
>= UNI_SUR_LOW_START
&& ch
<= UNI_SUR_LOW_END
)
247 --source
; /* return to the illegal value itself */
248 result
= CSR_SourceIllegal
;
254 if(target
>= targetEnd
)
256 source
= oldSource
; /* Back up source pointer! */
257 result
= CSR_TargetExhausted
;
264 *sourceStart
= source
;
265 *targetStart
= target
;
268 if(result
== CSR_SourceIllegal
)
270 E(DBF_UTF
, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x", ch
, ch2
);
282 LIBSTUB(CodesetsConvertUTF16toUTF32
, ULONG
, REG(a0
, const UTF16
** sourceStart
),
283 REG(a1
, const UTF16
* sourceEnd
),
284 REG(a2
, UTF32
** targetStart
),
285 REG(a3
, UTF32
* targetEnd
),
286 REG(d0
, ULONG flags
))
289 return CodesetsConvertUTF16toUTF32((const UTF16
**)REG_A0
, (const UTF16
*)REG_A1
, (UTF32
**)REG_A2
, (UTF32
*)REG_A3
, (ULONG
)REG_D0
);
291 return CodesetsConvertUTF16toUTF32(sourceStart
, sourceEnd
, targetStart
, targetEnd
, flags
);
296 /***********************************************************************/
299 * Index into the table below with the first byte of a UTF-8 sequence to
300 * get the number of trailing bytes that are supposed to follow it.
301 * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
302 * left as-is for anyone who may want to do such conversion, which was
303 * allowed in earlier algorithms.
305 const char trailingBytesForUTF8
[256] = {
306 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
307 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
308 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
309 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
310 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
311 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
312 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
313 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
317 * Magic values subtracted from a buffer value during UTF8 conversion.
318 * This table contains as many values as there might be trailing bytes
319 * in a UTF-8 sequence.
321 static const UTF32 offsetsFromUTF8
[6] = {
322 0x00000000UL
, 0x00003080UL
, 0x000E2080UL
,
323 0x03C82080UL
, 0xFA082080UL
, 0x82082080UL
327 * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
328 * into the first byte, depending on how many bytes follow. There are
329 * as many entries in this table as there are UTF-8 sequence types.
330 * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
331 * for *legal* UTF-8 will be 4 or fewer bytes total.
333 static const UTF8 firstByteMark
[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
335 /***********************************************************************/
337 /* The interface converts a whole buffer to avoid function-call overhead.
338 * Constants have been gathered. Loops & conditionals have been removed as
339 * much as possible for efficiency, in favor of drop-through switches.
340 * (See "Note A" at the bottom of the file for equivalent code.)
341 * If your compiler supports it, the "isLegalUTF8" call can be turned
342 * into an inline function.
345 /***********************************************************************/
348 AROS_LH5(ULONG
, CodesetsConvertUTF16toUTF8
,
349 AROS_LHA(const UTF16
**, sourceStart
, A0
),
350 AROS_LHA(const UTF16
*, sourceEnd
, A1
),
351 AROS_LHA(UTF8
**, targetStart
, A2
),
352 AROS_LHA(UTF8
*, targetEnd
, A3
),
353 AROS_LHA(ULONG
, flags
, D0
),
354 struct LibraryHeader
*, library
, 7, Codesets
360 CodesetsConvertUTF16toUTF8(REG(a0
, const UTF16
** sourceStart
),
361 REG(a1
, const UTF16
* sourceEnd
),
362 REG(a2
, UTF8
** targetStart
),
363 REG(a3
, UTF8
* targetEnd
),
364 REG(d0
, ULONG flags
))
367 ULONG result
= CSR_ConversionOK
;
368 const UTF16
*source
= *sourceStart
;
369 UTF8
*target
= *targetStart
;
373 while(source
< sourceEnd
)
376 unsigned short bytesToWrite
= 0;
377 const UTF32 byteMask
= 0xBF;
378 const UTF32 byteMark
= 0x80;
379 const UTF16
*oldSource
= source
; /* In case we have to back up because of target overflow. */
383 /* If we have a surrogate pair, convert to UTF32 first. */
384 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_HIGH_END
)
386 /* If the 16 bits following the high surrogate are in the source buffer... */
387 if(source
< sourceEnd
)
391 /* If it's a low surrogate, convert to UTF32. */
392 if(ch2
>= UNI_SUR_LOW_START
&& ch2
<= UNI_SUR_LOW_END
)
394 ch
= ((ch
- UNI_SUR_HIGH_START
) << halfShift
)
395 + (ch2
- UNI_SUR_LOW_START
) + halfBase
;
399 else if(flags
== CSF_StrictConversion
)
401 /* it's an unpaired high surrogate */
402 --source
; /* return to the illegal value itself */
403 result
= CSR_SourceIllegal
;
409 /* We don't have the 16 bits following the high surrogate. */
410 --source
; /* return to the high surrogate */
411 result
= CSR_SourceExhausted
;
416 else if(flags
== CSF_StrictConversion
)
418 /* UTF-16 surrogate values are illegal in UTF-32 */
419 if(ch
>= UNI_SUR_LOW_START
&& ch
<= UNI_SUR_LOW_END
)
421 --source
; /* return to the illegal value itself */
422 result
= CSR_SourceIllegal
;
427 /* Figure out how many bytes the result will require */
428 if(ch
< (UTF32
) 0x80)
432 else if (ch
< (UTF32
) 0x800)
436 else if (ch
< (UTF32
) 0x10000)
440 else if (ch
< (UTF32
) 0x110000)
447 ch
= UNI_REPLACEMENT_CHAR
;
450 target
+= bytesToWrite
;
451 if(target
> targetEnd
)
453 source
= oldSource
; /* Back up source pointer! */
454 target
-= bytesToWrite
;
455 result
= CSR_TargetExhausted
;
461 /* note: everything falls through. */
463 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
467 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
471 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
475 *--target
= (UTF8
) (ch
| firstByteMark
[bytesToWrite
]);
478 target
+= bytesToWrite
;
481 *sourceStart
= source
;
482 *targetStart
= target
;
492 LIBSTUB(CodesetsConvertUTF16toUTF8
, ULONG
, REG(a0
, const UTF16
** sourceStart
),
493 REG(a1
, const UTF16
* sourceEnd
),
494 REG(a2
, UTF8
** targetStart
),
495 REG(a3
, UTF8
* targetEnd
),
496 REG(d0
, ULONG flags
))
499 return CodesetsConvertUTF16toUTF8((const UTF16
**)REG_A0
, (const UTF16
*)REG_A1
, (UTF8
**)REG_A2
, (UTF8
*)REG_A3
, (ULONG
)REG_D0
);
501 return CodesetsConvertUTF16toUTF8(sourceStart
, sourceEnd
, targetStart
, targetEnd
, flags
);
506 /***********************************************************************/
509 * Utility routine to tell whether a sequence of bytes is legal UTF-8.
510 * This must be called with the length pre-determined by the first byte.
511 * If not calling this from ConvertUTF8to*, then the length can be set by:
512 * length = trailingBytesForUTF8[*source]+1;
513 * and the sequence is illegal right away if there aren't that many bytes
515 * If presented with a length > 4, this returns FALSE. The Unicode
516 * definition of UTF-8 goes up to 4-byte sequences.
520 AROS_LH2(BOOL
, CodesetsIsLegalUTF8
,
521 AROS_LHA(const UTF8
*, source
, A0
),
522 AROS_LHA(ULONG
, length
, D0
),
523 struct LibraryHeader
*, library
, 8, Codesets
529 CodesetsIsLegalUTF8(REG(a0
, const UTF8
* source
),
530 REG(d0
, ULONG length
))
534 const UTF8
*srcptr
= source
+ length
;
544 /* Everything else falls through when "TRUE"... */
546 if((a
= (*--srcptr
)) < 0x80 || a
> 0xBF)
553 if((a
= (*--srcptr
)) < 0x80 || a
> 0xBF)
560 if((a
= (*--srcptr
)) > 0xBF)
568 /* no fall-through in this inner switch */
610 if(*source
>= 0x80 && *source
< 0xC2)
631 LIBSTUB(CodesetsIsLegalUTF8
, BOOL
, REG(a0
, const UTF8
* source
),
632 REG(d0
, ULONG length
))
635 return CodesetsIsLegalUTF8((const UTF8
*)REG_A0
,(ULONG
)REG_D0
);
637 return CodesetsIsLegalUTF8(source
, length
);
642 /***********************************************************************/
645 * Exported function to return whether a UTF-8 sequence is legal or not.
646 * This is not used here; it's just exported.
650 AROS_LH2(BOOL
, CodesetsIsLegalUTF8Sequence
,
651 AROS_LHA(const UTF8
*, source
, A0
),
652 AROS_LHA(const UTF8
*, sourceEnd
, D1
),
653 struct LibraryHeader
*, library
, 9, Codesets
659 CodesetsIsLegalUTF8Sequence(REG(a0
, const UTF8
* source
),
660 REG(a1
, const UTF8
* sourceEnd
))
663 int length
= trailingBytesForUTF8
[*source
] + 1;
668 if(source
+ length
> sourceEnd
)
674 res
= CodesetsIsLegalUTF8(source
, length
);
684 LIBSTUB(CodesetsIsLegalUTF8Sequence
, BOOL
, REG(a0
, const UTF8
* source
),
685 REG(a1
, const UTF8
* sourceEnd
))
688 return CodesetsIsLegalUTF8Sequence((const UTF8
*)REG_A0
,(const UTF8
*)REG_A1
);
690 return CodesetsIsLegalUTF8Sequence(source
, sourceEnd
);
695 /***********************************************************************/
698 AROS_LH5(ULONG
, CodesetsConvertUTF8toUTF16
,
699 AROS_LHA(const UTF8
**, sourceStart
, A0
),
700 AROS_LHA(const UTF8
*, sourceEnd
, A1
),
701 AROS_LHA(UTF16
**, targetStart
, A2
),
702 AROS_LHA(UTF16
*, targetEnd
, A3
),
703 AROS_LHA(ULONG
, flags
, D0
),
704 struct LibraryHeader
*, library
, 10, Codesets
710 CodesetsConvertUTF8toUTF16(REG(a0
, const UTF8
** sourceStart
),
711 REG(a1
, const UTF8
* sourceEnd
),
712 REG(a2
, UTF16
** targetStart
),
713 REG(a3
, UTF16
* targetEnd
),
714 REG(d0
, ULONG flags
))
717 ULONG result
= CSR_ConversionOK
;
718 const UTF8
*source
= *sourceStart
;
719 UTF16
*target
= *targetStart
;
723 while(source
< sourceEnd
)
726 unsigned short extraBytesToRead
= trailingBytesForUTF8
[*source
];
728 if(source
+ extraBytesToRead
>= sourceEnd
)
730 result
= CSR_SourceExhausted
;
734 /* Do this check whether lenient or strict */
735 if(!CodesetsIsLegalUTF8 (source
, extraBytesToRead
+ 1))
737 result
= CSR_SourceIllegal
;
742 * The cases all fall through. See "Note A" below.
744 switch (extraBytesToRead
)
748 ch
<<= 6; /* remember, illegal UTF-8 */
752 ch
<<= 6; /* remember, illegal UTF-8 */
770 ch
-= offsetsFromUTF8
[extraBytesToRead
];
772 if(target
>= targetEnd
)
774 source
-= (extraBytesToRead
+ 1); /* Back up source pointer! */
775 result
= CSR_TargetExhausted
;
780 if(ch
<= UNI_MAX_BMP
)
782 /* Target is a character <= 0xFFFF */
783 /* UTF-16 surrogate values are illegal in UTF-32 */
784 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
)
786 if(flags
== CSF_StrictConversion
)
788 source
-= (extraBytesToRead
+ 1); /* return to the illegal value itself */
789 result
= CSR_SourceIllegal
;
795 *target
++ = UNI_REPLACEMENT_CHAR
;
800 *target
++ = (UTF16
) ch
; /* normal case */
803 else if(ch
> UNI_MAX_UTF16
)
805 if(flags
== CSF_StrictConversion
)
807 result
= CSR_SourceIllegal
;
808 source
-= (extraBytesToRead
+ 1); /* return to the start */
810 break; /* Bail out; shouldn't continue */
814 *target
++ = UNI_REPLACEMENT_CHAR
;
819 /* target is a character in range 0xFFFF - 0x10FFFF. */
820 if(target
+ 1 >= targetEnd
)
822 source
-= (extraBytesToRead
+ 1); /* Back up source pointer! */
823 result
= CSR_TargetExhausted
;
829 *target
++ = (UTF16
) ((ch
>> halfShift
) + UNI_SUR_HIGH_START
);
830 *target
++ = (UTF16
) ((ch
& halfMask
) + UNI_SUR_LOW_START
);
834 *sourceStart
= source
;
835 *targetStart
= target
;
845 LIBSTUB(CodesetsConvertUTF8toUTF16
, ULONG
, REG(a0
, const UTF8
** sourceStart
),
846 REG(a1
, const UTF8
* sourceEnd
),
847 REG(a2
, UTF16
** targetStart
),
848 REG(a3
, UTF16
* targetEnd
),
849 REG(d0
, ULONG flags
))
852 return CodesetsConvertUTF8toUTF16((const UTF8
**)REG_A0
, (const UTF8
*)REG_A1
, (UTF16
**)REG_A2
, (UTF16
*)REG_A3
, (ULONG
)REG_D0
);
854 return CodesetsConvertUTF8toUTF16(sourceStart
, sourceEnd
, targetStart
, targetEnd
, flags
);
859 /***********************************************************************/
862 AROS_LH5(ULONG
, CodesetsConvertUTF32toUTF8
,
863 AROS_LHA(const UTF32
**, sourceStart
, A0
),
864 AROS_LHA(const UTF32
*, sourceEnd
, A1
),
865 AROS_LHA(UTF8
**, targetStart
, A2
),
866 AROS_LHA(UTF8
*, targetEnd
, A3
),
867 AROS_LHA(ULONG
, flags
, D0
),
868 struct LibraryHeader
*, library
, 11, Codesets
874 CodesetsConvertUTF32toUTF8(REG(a0
, const UTF32
** sourceStart
),
875 REG(a1
, const UTF32
* sourceEnd
),
876 REG(a2
, UTF8
** targetStart
),
877 REG(a3
, UTF8
* targetEnd
),
878 REG(d0
, ULONG flags
))
881 ULONG result
= CSR_ConversionOK
;
882 const UTF32
*source
= *sourceStart
;
883 UTF8
*target
= *targetStart
;
887 while(source
< sourceEnd
)
890 unsigned short bytesToWrite
= 0;
891 const UTF32 byteMask
= 0xBF;
892 const UTF32 byteMark
= 0x80;
896 if(flags
== CSF_StrictConversion
)
898 /* UTF-16 surrogate values are illegal in UTF-32 */
899 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
)
901 --source
; /* return to the illegal value itself */
902 result
= CSR_SourceIllegal
;
909 * Figure out how many bytes the result will require. Turn any
910 * illegally large UTF32 things (> Plane 17) into replacement chars.
912 if(ch
< (UTF32
) 0x80)
916 else if(ch
< (UTF32
) 0x800)
920 else if(ch
< (UTF32
) 0x10000)
924 else if(ch
<= UNI_MAX_LEGAL_UTF32
)
931 ch
= UNI_REPLACEMENT_CHAR
;
932 result
= CSR_SourceIllegal
;
935 target
+= bytesToWrite
;
936 if(target
> targetEnd
)
938 --source
; /* Back up source pointer! */
939 target
-= bytesToWrite
;
940 result
= CSR_TargetExhausted
;
946 /* note: everything falls through. */
948 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
952 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
956 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
960 *--target
= (UTF8
) (ch
| firstByteMark
[bytesToWrite
]);
963 target
+= bytesToWrite
;
966 *sourceStart
= source
;
967 *targetStart
= target
;
977 LIBSTUB(CodesetsConvertUTF32toUTF8
, ULONG
, REG(a0
, const UTF32
** sourceStart
),
978 REG(a1
, const UTF32
* sourceEnd
),
979 REG(a2
, UTF8
** targetStart
),
980 REG(a3
, UTF8
* targetEnd
),
981 REG(d0
, ULONG flags
))
984 return CodesetsConvertUTF32toUTF8((const UTF32
**)REG_A0
, (const UTF32
*)REG_A1
, (UTF8
**)REG_A2
, (UTF8
*)REG_A3
, (ULONG
)REG_D0
);
986 return CodesetsConvertUTF32toUTF8(sourceStart
, sourceEnd
, targetStart
, targetEnd
, flags
);
991 /***********************************************************************/
994 AROS_LH5(ULONG
, CodesetsConvertUTF8toUTF32
,
995 AROS_LHA(const UTF8
**, sourceStart
, A0
),
996 AROS_LHA(const UTF8
*, sourceEnd
, A1
),
997 AROS_LHA(UTF32
**, targetStart
, A2
),
998 AROS_LHA(UTF32
*, targetEnd
, A3
),
999 AROS_LHA(ULONG
, flags
, D0
),
1000 struct LibraryHeader
*, library
, 12, Codesets
1006 CodesetsConvertUTF8toUTF32(REG(a0
, const UTF8
** sourceStart
),
1007 REG(a1
, const UTF8
* sourceEnd
),
1008 REG(a2
, UTF32
** targetStart
),
1009 REG(a3
, UTF32
* targetEnd
),
1010 REG(d0
, ULONG flags
))
1013 ULONG result
= CSR_ConversionOK
;
1014 const UTF8
*source
= *sourceStart
;
1015 UTF32
*target
= *targetStart
;
1019 while(source
< sourceEnd
)
1022 unsigned short extraBytesToRead
= trailingBytesForUTF8
[*source
];
1024 if(source
+ extraBytesToRead
>= sourceEnd
)
1026 result
= CSR_SourceExhausted
;
1030 /* Do this check whether lenient or strict */
1031 if(!CodesetsIsLegalUTF8(source
, extraBytesToRead
+ 1))
1033 result
= CSR_SourceIllegal
;
1038 * The cases all fall through. See "Note A" below.
1040 switch (extraBytesToRead
)
1066 ch
-= offsetsFromUTF8
[extraBytesToRead
];
1068 if(target
>= targetEnd
)
1070 source
-= (extraBytesToRead
+ 1); /* Back up the source pointer! */
1071 result
= CSR_TargetExhausted
;
1076 if(ch
<= UNI_MAX_LEGAL_UTF32
)
1079 * UTF-16 surrogate values are illegal in UTF-32, and anything
1080 * over Plane 17 (> 0x10FFFF) is illegal.
1082 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
)
1084 if(flags
== CSF_StrictConversion
)
1086 source
-= (extraBytesToRead
+ 1); /* return to the illegal value itself */
1087 result
= CSR_SourceIllegal
;
1093 *target
++ = UNI_REPLACEMENT_CHAR
;
1103 /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
1104 result
= CSR_SourceIllegal
;
1105 *target
++ = UNI_REPLACEMENT_CHAR
;
1109 *sourceStart
= source
;
1110 *targetStart
= target
;
1120 LIBSTUB(CodesetsConvertUTF8toUTF32
, ULONG
, REG(a0
, const UTF8
** sourceStart
),
1121 REG(a1
, const UTF8
* sourceEnd
),
1122 REG(a2
, UTF32
** targetStart
),
1123 REG(a3
, UTF32
* targetEnd
),
1124 REG(d0
, ULONG flags
))
1127 return CodesetsConvertUTF8toUTF32((const UTF8
**)REG_A0
, (const UTF8
*)REG_A1
, (UTF32
**)REG_A2
, (UTF32
*)REG_A3
, (ULONG
)REG_D0
);
1129 return CodesetsConvertUTF8toUTF32(sourceStart
, sourceEnd
, targetStart
, targetEnd
, flags
);
1134 /***********************************************************************
1137 The fall-through switches in UTF-8 reading code save a
1138 temp variable, some decrements & conditionals. The switches
1139 are equivalent to the following loop:
1141 int tmpBytesToRead = extraBytesToRead+1;
1145 if (tmpBytesToRead) ch <<= 6;
1146 } while (tmpBytesToRead > 0);
1148 In UTF-8 writing code, the switches on "bytesToWrite" are
1149 similarly unrolled loops.
1151 ***********************************************************************/