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"
49 #include <proto/codesets.h>
51 /***********************************************************************/
53 static const int halfShift
= 10; /* used for shifting by 10 bits */
55 static const UTF32 halfBase
= 0x0010000UL
;
56 static const UTF32 halfMask
= 0x3FFUL
;
58 #define UNI_SUR_HIGH_START (UTF32)0xD800
59 #define UNI_SUR_HIGH_END (UTF32)0xDBFF
60 #define UNI_SUR_LOW_START (UTF32)0xDC00
61 #define UNI_SUR_LOW_END (UTF32)0xDFFF
63 /***********************************************************************/
65 LIBPROTO(CodesetsConvertUTF32toUTF16
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, const UTF32
**sourceStart
), REG(a1
, const UTF32
*sourceEnd
), REG(a2
, UTF16
**targetStart
), REG(a3
, UTF16
*targetEnd
), REG(d0
, ULONG flags
))
67 ULONG result
= CSR_ConversionOK
;
68 const UTF32
*source
= *sourceStart
;
69 UTF16
*target
= *targetStart
;
73 while(source
< sourceEnd
)
77 if(target
>= targetEnd
)
79 result
= CSR_TargetExhausted
;
86 /* Target is a character <= 0xFFFF */
87 /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
88 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
)
90 if(flags
== CSF_StrictConversion
)
92 --source
; /* return to the illegal value itself */
93 result
= CSR_SourceIllegal
;
98 *target
++ = UNI_REPLACEMENT_CHAR
;
103 *target
++ = (UTF16
)ch
; /* normal case */
106 else if(ch
> UNI_MAX_LEGAL_UTF32
)
108 if(flags
== CSF_StrictConversion
)
110 result
= CSR_SourceIllegal
;
114 *target
++ = UNI_REPLACEMENT_CHAR
;
119 /* target is a character in range 0xFFFF - 0x10FFFF. */
120 if(target
+ 1 >= targetEnd
)
122 --source
; /* Back up source pointer! */
123 result
= CSR_TargetExhausted
;
127 *target
++ = (UTF16
) ((ch
>> halfShift
) + UNI_SUR_HIGH_START
);
128 *target
++ = (UTF16
) ((ch
& halfMask
) + UNI_SUR_LOW_START
);
132 *sourceStart
= source
;
133 *targetStart
= target
;
139 /***********************************************************************/
141 LIBPROTO(CodesetsConvertUTF16toUTF32
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, const UTF16
**sourceStart
), REG(a1
, const UTF16
*sourceEnd
), REG(a2
, UTF32
**targetStart
), REG(a3
, UTF32
*targetEnd
), REG(d0
, ULONG flags
))
143 ULONG result
= CSR_ConversionOK
;
144 const UTF16
*source
= *sourceStart
;
145 UTF32
*target
= *targetStart
;
150 while(source
< sourceEnd
)
152 const UTF16
*oldSource
= source
; /* In case we have to back up because of target overflow. */
155 /* If we have a surrogate pair, convert to UTF32 first. */
156 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_HIGH_END
)
158 /* If the 16 bits following the high surrogate are in the source buffer... */
159 if(source
< sourceEnd
)
163 /* If it's a low surrogate, convert to UTF32. */
164 if(ch2
>= UNI_SUR_LOW_START
&& ch2
<= UNI_SUR_LOW_END
)
166 ch
= ((ch
- UNI_SUR_HIGH_START
) << halfShift
)
167 + (ch2
- UNI_SUR_LOW_START
) + halfBase
;
171 else if(flags
== CSF_StrictConversion
)
173 /* it's an unpaired high surrogate */
174 --source
; /* return to the illegal value itself */
175 result
= CSR_SourceIllegal
;
182 /* We don't have the 16 bits following the high surrogate. */
183 --source
; /* return to the high surrogate */
184 result
= CSR_SourceExhausted
;
189 else if (flags
== CSF_StrictConversion
)
191 /* UTF-16 surrogate values are illegal in UTF-32 */
192 if(ch
>= UNI_SUR_LOW_START
&& ch
<= UNI_SUR_LOW_END
)
194 --source
; /* return to the illegal value itself */
195 result
= CSR_SourceIllegal
;
201 if(target
>= targetEnd
)
203 source
= oldSource
; /* Back up source pointer! */
204 result
= CSR_TargetExhausted
;
211 *sourceStart
= source
;
212 *targetStart
= target
;
215 if(result
== CSR_SourceIllegal
)
217 E(DBF_UTF
, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x", ch
, ch2
);
225 /***********************************************************************/
228 * Index into the table below with the first byte of a UTF-8 sequence to
229 * get the number of trailing bytes that are supposed to follow it.
230 * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
231 * left as-is for anyone who may want to do such conversion, which was
232 * allowed in earlier algorithms.
234 const char trailingBytesForUTF8
[256] = {
235 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,
236 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,
237 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,
238 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,
239 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,
240 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,
241 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,
242 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
246 * Magic values subtracted from a buffer value during UTF8 conversion.
247 * This table contains as many values as there might be trailing bytes
248 * in a UTF-8 sequence.
250 static const UTF32 offsetsFromUTF8
[6] = {
251 0x00000000UL
, 0x00003080UL
, 0x000E2080UL
,
252 0x03C82080UL
, 0xFA082080UL
, 0x82082080UL
256 * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
257 * into the first byte, depending on how many bytes follow. There are
258 * as many entries in this table as there are UTF-8 sequence types.
259 * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
260 * for *legal* UTF-8 will be 4 or fewer bytes total.
262 static const UTF8 firstByteMark
[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
264 /***********************************************************************/
266 /* The interface converts a whole buffer to avoid function-call overhead.
267 * Constants have been gathered. Loops & conditionals have been removed as
268 * much as possible for efficiency, in favor of drop-through switches.
269 * (See "Note A" at the bottom of the file for equivalent code.)
270 * If your compiler supports it, the "isLegalUTF8" call can be turned
271 * into an inline function.
274 /***********************************************************************/
276 LIBPROTO(CodesetsConvertUTF16toUTF8
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, const UTF16
**sourceStart
), REG(a1
, const UTF16
*sourceEnd
) , REG(a2
, UTF8
**targetStart
), REG(a3
, UTF8
*targetEnd
), REG(d0
, ULONG flags
))
278 ULONG result
= CSR_ConversionOK
;
279 const UTF16
*source
= *sourceStart
;
280 UTF8
*target
= *targetStart
;
281 UTF8
*start
= target
;
285 while(source
< sourceEnd
)
288 unsigned short bytesToWrite
= 0;
289 const UTF32 byteMask
= 0xBF;
290 const UTF32 byteMark
= 0x80;
291 const UTF16
*oldSource
= source
; /* In case we have to back up because of target overflow. */
295 /* If we have a surrogate pair, convert to UTF32 first. */
296 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_HIGH_END
)
298 /* If the 16 bits following the high surrogate are in the source buffer... */
299 if(source
< sourceEnd
)
303 /* If it's a low surrogate, convert to UTF32. */
304 if(ch2
>= UNI_SUR_LOW_START
&& ch2
<= UNI_SUR_LOW_END
)
306 ch
= ((ch
- UNI_SUR_HIGH_START
) << halfShift
)
307 + (ch2
- UNI_SUR_LOW_START
) + halfBase
;
311 else if(flags
== CSF_StrictConversion
)
313 /* it's an unpaired high surrogate */
314 --source
; /* return to the illegal value itself */
315 result
= CSR_SourceIllegal
;
321 /* We don't have the 16 bits following the high surrogate. */
322 --source
; /* return to the high surrogate */
323 result
= CSR_SourceExhausted
;
328 else if(flags
== CSF_StrictConversion
)
330 /* UTF-16 surrogate values are illegal in UTF-32 */
331 if(ch
>= UNI_SUR_LOW_START
&& ch
<= UNI_SUR_LOW_END
)
333 --source
; /* return to the illegal value itself */
334 result
= CSR_SourceIllegal
;
339 /* Figure out how many bytes the result will require */
340 if(ch
< (UTF32
) 0x80)
344 else if (ch
< (UTF32
) 0x800)
348 else if (ch
< (UTF32
) 0x10000)
352 else if (ch
< (UTF32
) 0x110000)
359 ch
= UNI_REPLACEMENT_CHAR
;
362 target
+= bytesToWrite
;
365 if(target
> targetEnd
)
367 source
= oldSource
; /* Back up source pointer! */
368 target
-= bytesToWrite
;
369 result
= CSR_TargetExhausted
;
375 /* note: everything falls through. */
377 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
381 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
385 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
389 *--target
= (UTF8
) (ch
| firstByteMark
[bytesToWrite
]);
392 target
+= bytesToWrite
;
396 *sourceStart
= source
;
397 *targetStart
= target
;
403 /***********************************************************************/
406 * Utility routine to tell whether a sequence of bytes is legal UTF-8.
407 * This must be called with the length pre-determined by the first byte.
408 * If not calling this from ConvertUTF8to*, then the length can be set by:
409 * length = trailingBytesForUTF8[*source]+1;
410 * and the sequence is illegal right away if there aren't that many bytes
412 * If presented with a length > 4, this returns FALSE. The Unicode
413 * definition of UTF-8 goes up to 4-byte sequences.
416 LIBPROTO(CodesetsIsLegalUTF8
, BOOL
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, const UTF8
*source
), REG(d0
, ULONG length
))
419 const UTF8
*srcptr
= source
+ length
;
429 /* Everything else falls through when "TRUE"... */
431 if((a
= (*--srcptr
)) < 0x80 || a
> 0xBF)
438 if((a
= (*--srcptr
)) < 0x80 || a
> 0xBF)
445 if((a
= (*--srcptr
)) > 0xBF)
453 /* no fall-through in this inner switch */
495 if(*source
>= 0x80 && *source
< 0xC2)
512 /***********************************************************************/
515 * Exported function to return whether a UTF-8 sequence is legal or not.
516 * This is not used here; it's just exported.
519 LIBPROTO(CodesetsIsLegalUTF8Sequence
, BOOL
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, const UTF8
*source
), REG(a1
, const UTF8
*sourceEnd
))
521 int length
= trailingBytesForUTF8
[*source
] + 1;
526 if(source
+ length
> sourceEnd
)
532 res
= CodesetsIsLegalUTF8(source
, length
);
538 /***********************************************************************/
540 LIBPROTO(CodesetsConvertUTF8toUTF16
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, const UTF8
**sourceStart
), REG(a1
, const UTF8
*sourceEnd
), REG(a2
, UTF16
**targetStart
), REG(a3
, UTF16
*targetEnd
), REG(d0
, ULONG flags
))
542 ULONG result
= CSR_ConversionOK
;
543 const UTF8
*source
= *sourceStart
;
544 UTF16
*target
= *targetStart
;
545 UTF16
*start
= target
;
549 while(source
< sourceEnd
)
552 unsigned short extraBytesToRead
= trailingBytesForUTF8
[*source
];
554 if(source
+ extraBytesToRead
>= sourceEnd
)
556 result
= CSR_SourceExhausted
;
560 /* Do this check whether lenient or strict */
561 if(!CodesetsIsLegalUTF8 (source
, extraBytesToRead
+ 1))
563 result
= CSR_SourceIllegal
;
568 * The cases all fall through. See "Note A" below.
570 switch (extraBytesToRead
)
574 ch
<<= 6; /* remember, illegal UTF-8 */
578 ch
<<= 6; /* remember, illegal UTF-8 */
596 ch
-= offsetsFromUTF8
[extraBytesToRead
];
598 if(start
&& (target
>= targetEnd
))
600 source
-= (extraBytesToRead
+ 1); /* Back up source pointer! */
601 result
= CSR_TargetExhausted
;
606 if(ch
<= UNI_MAX_BMP
)
608 /* Target is a character <= 0xFFFF */
609 /* UTF-16 surrogate values are illegal in UTF-32 */
610 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
)
612 if(flags
== CSF_StrictConversion
)
614 source
-= (extraBytesToRead
+ 1); /* return to the illegal value itself */
615 result
= CSR_SourceIllegal
;
620 ch
= UNI_REPLACEMENT_CHAR
;
623 *target
= (UTF16
) ch
; /* normal case */
626 else if(ch
> UNI_MAX_UTF16
)
628 if(flags
== CSF_StrictConversion
)
630 result
= CSR_SourceIllegal
;
631 source
-= (extraBytesToRead
+ 1); /* return to the start */
633 break; /* Bail out; shouldn't continue */
636 *target
= UNI_REPLACEMENT_CHAR
;
641 /* target is a character in range 0xFFFF - 0x10FFFF. */
644 if(target
+ 1 >= targetEnd
)
646 source
-= (extraBytesToRead
+ 1); /* Back up source pointer! */
647 result
= CSR_TargetExhausted
;
653 target
[0] = (UTF16
) ((ch
>> halfShift
) + UNI_SUR_HIGH_START
);
654 target
[1] = (UTF16
) ((ch
& halfMask
) + UNI_SUR_LOW_START
);
660 *sourceStart
= source
;
661 *targetStart
= target
;
667 /***********************************************************************/
669 LIBPROTO(CodesetsConvertUTF32toUTF8
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, const UTF32
**sourceStart
), REG(a1
, const UTF32
*sourceEnd
), REG(a2
, UTF8
**targetStart
), REG(a3
, UTF8
*targetEnd
), REG(d0
, ULONG flags
))
671 ULONG result
= CSR_ConversionOK
;
672 const UTF32
*source
= *sourceStart
;
673 UTF8
*target
= *targetStart
;
674 UTF8
*start
= target
;
678 while(source
< sourceEnd
)
681 unsigned short bytesToWrite
= 0;
682 const UTF32 byteMask
= 0xBF;
683 const UTF32 byteMark
= 0x80;
687 if(flags
== CSF_StrictConversion
)
689 /* UTF-16 surrogate values are illegal in UTF-32 */
690 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
)
692 --source
; /* return to the illegal value itself */
693 result
= CSR_SourceIllegal
;
700 * Figure out how many bytes the result will require. Turn any
701 * illegally large UTF32 things (> Plane 17) into replacement chars.
703 if(ch
< (UTF32
) 0x80)
707 else if(ch
< (UTF32
) 0x800)
711 else if(ch
< (UTF32
) 0x10000)
715 else if(ch
<= UNI_MAX_LEGAL_UTF32
)
722 ch
= UNI_REPLACEMENT_CHAR
;
723 result
= CSR_SourceIllegal
;
726 target
+= bytesToWrite
;
729 if(target
> targetEnd
)
731 --source
; /* Back up source pointer! */
732 target
-= bytesToWrite
;
733 result
= CSR_TargetExhausted
;
739 /* note: everything falls through. */
741 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
745 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
749 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
753 *--target
= (UTF8
) (ch
| firstByteMark
[bytesToWrite
]);
756 target
+= bytesToWrite
;
760 *sourceStart
= source
;
761 *targetStart
= target
;
767 /***********************************************************************/
769 LIBPROTO(CodesetsConvertUTF8toUTF32
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, const UTF8
**sourceStart
), REG(a1
, const UTF8
*sourceEnd
), REG(a2
, UTF32
**targetStart
), REG(a3
, UTF32
*targetEnd
), REG(d0
, ULONG flags
))
771 ULONG result
= CSR_ConversionOK
;
772 const UTF8
*source
= *sourceStart
;
773 UTF32
*target
= *targetStart
;
774 UTF32
*start
= target
;
778 while(source
< sourceEnd
)
781 unsigned short extraBytesToRead
= trailingBytesForUTF8
[*source
];
783 if(source
+ extraBytesToRead
>= sourceEnd
)
785 result
= CSR_SourceExhausted
;
789 /* Do this check whether lenient or strict */
790 if(!CodesetsIsLegalUTF8(source
, extraBytesToRead
+ 1))
792 result
= CSR_SourceIllegal
;
797 * The cases all fall through. See "Note A" below.
799 switch (extraBytesToRead
)
825 ch
-= offsetsFromUTF8
[extraBytesToRead
];
829 if(target
>= targetEnd
)
831 source
-= (extraBytesToRead
+ 1); /* Back up the source pointer! */
832 result
= CSR_TargetExhausted
;
837 if(ch
<= UNI_MAX_LEGAL_UTF32
)
840 * UTF-16 surrogate values are illegal in UTF-32, and anything
841 * over Plane 17 (> 0x10FFFF) is illegal.
843 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
)
845 if(flags
== CSF_StrictConversion
)
847 source
-= (extraBytesToRead
+ 1); /* return to the illegal value itself */
848 result
= CSR_SourceIllegal
;
854 *target
++ = UNI_REPLACEMENT_CHAR
;
864 /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
865 result
= CSR_SourceIllegal
;
866 *target
++ = UNI_REPLACEMENT_CHAR
;
873 *sourceStart
= source
;
874 *targetStart
= target
;
880 /***********************************************************************
883 The fall-through switches in UTF-8 reading code save a
884 temp variable, some decrements & conditionals. The switches
885 are equivalent to the following loop:
887 int tmpBytesToRead = extraBytesToRead+1;
891 if (tmpBytesToRead) ch <<= 6;
892 } while (tmpBytesToRead > 0);
894 In UTF-8 writing code, the switches on "bytesToWrite" are
895 similarly unrolled loops.
897 ***********************************************************************/