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"
48 /***********************************************************************/
50 static const int halfShift
= 10; /* used for shifting by 10 bits */
52 static const UTF32 halfBase
= 0x0010000UL
;
53 static const UTF32 halfMask
= 0x3FFUL
;
55 #define UNI_SUR_HIGH_START (UTF32)0xD800
56 #define UNI_SUR_HIGH_END (UTF32)0xDBFF
57 #define UNI_SUR_LOW_START (UTF32)0xDC00
58 #define UNI_SUR_LOW_END (UTF32)0xDFFF
60 /***********************************************************************/
63 AROS_LH5(ULONG
, CodesetsConvertUTF32toUTF16
,
64 AROS_LHA(const UTF32
**, sourceStart
, A0
),
65 AROS_LHA(const UTF32
*, sourceEnd
, A1
),
66 AROS_LHA(UTF16
**, targetStart
, A2
),
67 AROS_LHA(UTF16
*, targetEnd
, A3
),
68 AROS_LHA(ULONG
, flags
, D0
),
69 struct CodesetsBase
*, library
, 5, Codesets
75 CodesetsConvertUTF32toUTF16(REG(a0
, const UTF32
** sourceStart
),
76 REG(a1
, const UTF32
* sourceEnd
),
77 REG(a2
, UTF16
** targetStart
),
78 REG(a3
, UTF16
* targetEnd
),
82 ULONG result
= CSR_ConversionOK
;
83 const UTF32
*source
= *sourceStart
;
84 UTF16
*target
= *targetStart
;
88 while(source
< sourceEnd
)
92 if(target
>= targetEnd
)
94 result
= CSR_TargetExhausted
;
101 /* Target is a character <= 0xFFFF */
102 /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
103 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
)
105 if(flags
== CSF_StrictConversion
)
107 --source
; /* return to the illegal value itself */
108 result
= CSR_SourceIllegal
;
113 *target
++ = UNI_REPLACEMENT_CHAR
;
118 *target
++ = (UTF16
)ch
; /* normal case */
121 else if(ch
> UNI_MAX_LEGAL_UTF32
)
123 if(flags
== CSF_StrictConversion
)
125 result
= CSR_SourceIllegal
;
129 *target
++ = UNI_REPLACEMENT_CHAR
;
134 /* target is a character in range 0xFFFF - 0x10FFFF. */
135 if(target
+ 1 >= targetEnd
)
137 --source
; /* Back up source pointer! */
138 result
= CSR_TargetExhausted
;
142 *target
++ = (UTF16
) ((ch
>> halfShift
) + UNI_SUR_HIGH_START
);
143 *target
++ = (UTF16
) ((ch
& halfMask
) + UNI_SUR_LOW_START
);
147 *sourceStart
= source
;
148 *targetStart
= target
;
158 LIBSTUB(CodesetsConvertUTF32toUTF16
, ULONG
, REG(a0
, const UTF32
** sourceStart
),
159 REG(a1
, const UTF32
* sourceEnd
),
160 REG(a2
, UTF16
** targetStart
),
161 REG(a3
, UTF16
* targetEnd
),
162 REG(d0
, ULONG flags
))
165 return CodesetsConvertUTF32toUTF16((const UTF32
**)REG_A0
, (const UTF32
*)REG_A1
, (UTF16
**)REG_A2
, (UTF16
*)REG_A3
, (ULONG
)REG_D0
);
167 return CodesetsConvertUTF32toUTF16(sourceStart
, sourceEnd
, targetStart
, targetEnd
, flags
);
172 /***********************************************************************/
175 AROS_LH5(ULONG
, CodesetsConvertUTF16toUTF32
,
176 AROS_LHA(const UTF16
**, sourceStart
, A0
),
177 AROS_LHA(const UTF16
*, sourceEnd
, A1
),
178 AROS_LHA(UTF32
**, targetStart
, A2
),
179 AROS_LHA(UTF32
*, targetEnd
, A3
),
180 AROS_LHA(ULONG
, flags
, D0
),
181 struct CodesetsBase
*, library
, 6, Codesets
187 CodesetsConvertUTF16toUTF32(REG(a0
, const UTF16
** sourceStart
),
188 REG(a1
, const UTF16
* sourceEnd
),
189 REG(a2
, UTF32
** targetStart
),
190 REG(a3
, UTF32
* targetEnd
),
191 REG(d0
, ULONG flags
))
194 ULONG result
= CSR_ConversionOK
;
195 const UTF16
*source
= *sourceStart
;
196 UTF32
*target
= *targetStart
;
201 while(source
< sourceEnd
)
203 const UTF16
*oldSource
= source
; /* In case we have to back up because of target overflow. */
206 /* If we have a surrogate pair, convert to UTF32 first. */
207 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_HIGH_END
)
209 /* If the 16 bits following the high surrogate are in the source buffer... */
210 if(source
< sourceEnd
)
214 /* If it's a low surrogate, convert to UTF32. */
215 if(ch2
>= UNI_SUR_LOW_START
&& ch2
<= UNI_SUR_LOW_END
)
217 ch
= ((ch
- UNI_SUR_HIGH_START
) << halfShift
)
218 + (ch2
- UNI_SUR_LOW_START
) + halfBase
;
222 else if(flags
== CSF_StrictConversion
)
224 /* it's an unpaired high surrogate */
225 --source
; /* return to the illegal value itself */
226 result
= CSR_SourceIllegal
;
233 /* We don't have the 16 bits following the high surrogate. */
234 --source
; /* return to the high surrogate */
235 result
= CSR_SourceExhausted
;
240 else if (flags
== CSF_StrictConversion
)
242 /* UTF-16 surrogate values are illegal in UTF-32 */
243 if(ch
>= UNI_SUR_LOW_START
&& ch
<= UNI_SUR_LOW_END
)
245 --source
; /* return to the illegal value itself */
246 result
= CSR_SourceIllegal
;
252 if(target
>= targetEnd
)
254 source
= oldSource
; /* Back up source pointer! */
255 result
= CSR_TargetExhausted
;
262 *sourceStart
= source
;
263 *targetStart
= target
;
266 if(result
== CSR_SourceIllegal
)
268 E(DBF_UTF
, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x", ch
, ch2
);
280 LIBSTUB(CodesetsConvertUTF16toUTF32
, ULONG
, REG(a0
, const UTF16
** sourceStart
),
281 REG(a1
, const UTF16
* sourceEnd
),
282 REG(a2
, UTF32
** targetStart
),
283 REG(a3
, UTF32
* targetEnd
),
284 REG(d0
, ULONG flags
))
287 return CodesetsConvertUTF16toUTF32((const UTF16
**)REG_A0
, (const UTF16
*)REG_A1
, (UTF32
**)REG_A2
, (UTF32
*)REG_A3
, (ULONG
)REG_D0
);
289 return CodesetsConvertUTF16toUTF32(sourceStart
, sourceEnd
, targetStart
, targetEnd
, flags
);
294 /***********************************************************************/
297 * Index into the table below with the first byte of a UTF-8 sequence to
298 * get the number of trailing bytes that are supposed to follow it.
299 * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
300 * left as-is for anyone who may want to do such conversion, which was
301 * allowed in earlier algorithms.
303 const char trailingBytesForUTF8
[256] = {
304 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,
305 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,
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 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,
311 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
315 * Magic values subtracted from a buffer value during UTF8 conversion.
316 * This table contains as many values as there might be trailing bytes
317 * in a UTF-8 sequence.
319 static const UTF32 offsetsFromUTF8
[6] = {
320 0x00000000UL
, 0x00003080UL
, 0x000E2080UL
,
321 0x03C82080UL
, 0xFA082080UL
, 0x82082080UL
325 * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
326 * into the first byte, depending on how many bytes follow. There are
327 * as many entries in this table as there are UTF-8 sequence types.
328 * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
329 * for *legal* UTF-8 will be 4 or fewer bytes total.
331 static const UTF8 firstByteMark
[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
333 /***********************************************************************/
335 /* The interface converts a whole buffer to avoid function-call overhead.
336 * Constants have been gathered. Loops & conditionals have been removed as
337 * much as possible for efficiency, in favor of drop-through switches.
338 * (See "Note A" at the bottom of the file for equivalent code.)
339 * If your compiler supports it, the "isLegalUTF8" call can be turned
340 * into an inline function.
343 /***********************************************************************/
346 AROS_LH5(ULONG
, CodesetsConvertUTF16toUTF8
,
347 AROS_LHA(const UTF16
**, sourceStart
, A0
),
348 AROS_LHA(const UTF16
*, sourceEnd
, A1
),
349 AROS_LHA(UTF8
**, targetStart
, A2
),
350 AROS_LHA(UTF8
*, targetEnd
, A3
),
351 AROS_LHA(ULONG
, flags
, D0
),
352 struct CodesetsBase
*, library
, 7, Codesets
358 CodesetsConvertUTF16toUTF8(REG(a0
, const UTF16
** sourceStart
),
359 REG(a1
, const UTF16
* sourceEnd
),
360 REG(a2
, UTF8
** targetStart
),
361 REG(a3
, UTF8
* targetEnd
),
362 REG(d0
, ULONG flags
))
365 ULONG result
= CSR_ConversionOK
;
366 const UTF16
*source
= *sourceStart
;
367 UTF8
*target
= *targetStart
;
371 while(source
< sourceEnd
)
374 unsigned short bytesToWrite
= 0;
375 const UTF32 byteMask
= 0xBF;
376 const UTF32 byteMark
= 0x80;
377 const UTF16
*oldSource
= source
; /* In case we have to back up because of target overflow. */
381 /* If we have a surrogate pair, convert to UTF32 first. */
382 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_HIGH_END
)
384 /* If the 16 bits following the high surrogate are in the source buffer... */
385 if(source
< sourceEnd
)
389 /* If it's a low surrogate, convert to UTF32. */
390 if(ch2
>= UNI_SUR_LOW_START
&& ch2
<= UNI_SUR_LOW_END
)
392 ch
= ((ch
- UNI_SUR_HIGH_START
) << halfShift
)
393 + (ch2
- UNI_SUR_LOW_START
) + halfBase
;
397 else if(flags
== CSF_StrictConversion
)
399 /* it's an unpaired high surrogate */
400 --source
; /* return to the illegal value itself */
401 result
= CSR_SourceIllegal
;
407 /* We don't have the 16 bits following the high surrogate. */
408 --source
; /* return to the high surrogate */
409 result
= CSR_SourceExhausted
;
414 else if(flags
== CSF_StrictConversion
)
416 /* UTF-16 surrogate values are illegal in UTF-32 */
417 if(ch
>= UNI_SUR_LOW_START
&& ch
<= UNI_SUR_LOW_END
)
419 --source
; /* return to the illegal value itself */
420 result
= CSR_SourceIllegal
;
425 /* Figure out how many bytes the result will require */
426 if(ch
< (UTF32
) 0x80)
430 else if (ch
< (UTF32
) 0x800)
434 else if (ch
< (UTF32
) 0x10000)
438 else if (ch
< (UTF32
) 0x110000)
445 ch
= UNI_REPLACEMENT_CHAR
;
448 target
+= bytesToWrite
;
449 if(target
> targetEnd
)
451 source
= oldSource
; /* Back up source pointer! */
452 target
-= bytesToWrite
;
453 result
= CSR_TargetExhausted
;
459 /* note: everything falls through. */
461 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
465 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
469 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
473 *--target
= (UTF8
) (ch
| firstByteMark
[bytesToWrite
]);
476 target
+= bytesToWrite
;
479 *sourceStart
= source
;
480 *targetStart
= target
;
490 LIBSTUB(CodesetsConvertUTF16toUTF8
, ULONG
, REG(a0
, const UTF16
** sourceStart
),
491 REG(a1
, const UTF16
* sourceEnd
),
492 REG(a2
, UTF8
** targetStart
),
493 REG(a3
, UTF8
* targetEnd
),
494 REG(d0
, ULONG flags
))
497 return CodesetsConvertUTF16toUTF8((const UTF16
**)REG_A0
, (const UTF16
*)REG_A1
, (UTF8
**)REG_A2
, (UTF8
*)REG_A3
, (ULONG
)REG_D0
);
499 return CodesetsConvertUTF16toUTF8(sourceStart
, sourceEnd
, targetStart
, targetEnd
, flags
);
504 /***********************************************************************/
507 * Utility routine to tell whether a sequence of bytes is legal UTF-8.
508 * This must be called with the length pre-determined by the first byte.
509 * If not calling this from ConvertUTF8to*, then the length can be set by:
510 * length = trailingBytesForUTF8[*source]+1;
511 * and the sequence is illegal right away if there aren't that many bytes
513 * If presented with a length > 4, this returns FALSE. The Unicode
514 * definition of UTF-8 goes up to 4-byte sequences.
518 AROS_LH2(BOOL
, CodesetsIsLegalUTF8
,
519 AROS_LHA(const UTF8
*, source
, A0
),
520 AROS_LHA(ULONG
, length
, D0
),
521 struct CodesetsBase
*, library
, 8, Codesets
527 CodesetsIsLegalUTF8(REG(a0
, const UTF8
* source
),
528 REG(d0
, ULONG length
))
532 const UTF8
*srcptr
= source
+ length
;
542 /* Everything else falls through when "TRUE"... */
544 if((a
= (*--srcptr
)) < 0x80 || a
> 0xBF)
551 if((a
= (*--srcptr
)) < 0x80 || a
> 0xBF)
558 if((a
= (*--srcptr
)) > 0xBF)
566 /* no fall-through in this inner switch */
608 if(*source
>= 0x80 && *source
< 0xC2)
629 LIBSTUB(CodesetsIsLegalUTF8
, BOOL
, REG(a0
, const UTF8
* source
),
630 REG(d0
, ULONG length
))
633 return CodesetsIsLegalUTF8((const UTF8
*)REG_A0
,(ULONG
)REG_D0
);
635 return CodesetsIsLegalUTF8(source
, length
);
640 /***********************************************************************/
643 * Exported function to return whether a UTF-8 sequence is legal or not.
644 * This is not used here; it's just exported.
648 AROS_LH2(BOOL
, CodesetsIsLegalUTF8Sequence
,
649 AROS_LHA(const UTF8
*, source
, A0
),
650 AROS_LHA(const UTF8
*, sourceEnd
, D1
),
651 struct CodesetsBase
*, library
, 9, Codesets
657 CodesetsIsLegalUTF8Sequence(REG(a0
, const UTF8
* source
),
658 REG(a1
, const UTF8
* sourceEnd
))
661 int length
= trailingBytesForUTF8
[*source
] + 1;
666 if(source
+ length
> sourceEnd
)
672 res
= CodesetsIsLegalUTF8(source
, length
);
682 LIBSTUB(CodesetsIsLegalUTF8Sequence
, BOOL
, REG(a0
, const UTF8
* source
),
683 REG(a1
, const UTF8
* sourceEnd
))
686 return CodesetsIsLegalUTF8Sequence((const UTF8
*)REG_A0
,(const UTF8
*)REG_A1
);
688 return CodesetsIsLegalUTF8Sequence(source
, sourceEnd
);
693 /***********************************************************************/
696 AROS_LH5(ULONG
, CodesetsConvertUTF8toUTF16
,
697 AROS_LHA(const UTF8
**, sourceStart
, A0
),
698 AROS_LHA(const UTF8
*, sourceEnd
, A1
),
699 AROS_LHA(UTF16
**, targetStart
, A2
),
700 AROS_LHA(UTF16
*, targetEnd
, A3
),
701 AROS_LHA(ULONG
, flags
, D0
),
702 struct CodesetsBase
*, library
, 10, Codesets
708 CodesetsConvertUTF8toUTF16(REG(a0
, const UTF8
** sourceStart
),
709 REG(a1
, const UTF8
* sourceEnd
),
710 REG(a2
, UTF16
** targetStart
),
711 REG(a3
, UTF16
* targetEnd
),
712 REG(d0
, ULONG flags
))
715 ULONG result
= CSR_ConversionOK
;
716 const UTF8
*source
= *sourceStart
;
717 UTF16
*target
= *targetStart
;
721 while(source
< sourceEnd
)
724 unsigned short extraBytesToRead
= trailingBytesForUTF8
[*source
];
726 if(source
+ extraBytesToRead
>= sourceEnd
)
728 result
= CSR_SourceExhausted
;
732 /* Do this check whether lenient or strict */
733 if(!CodesetsIsLegalUTF8 (source
, extraBytesToRead
+ 1))
735 result
= CSR_SourceIllegal
;
740 * The cases all fall through. See "Note A" below.
742 switch (extraBytesToRead
)
746 ch
<<= 6; /* remember, illegal UTF-8 */
750 ch
<<= 6; /* remember, illegal UTF-8 */
768 ch
-= offsetsFromUTF8
[extraBytesToRead
];
770 if(target
>= targetEnd
)
772 source
-= (extraBytesToRead
+ 1); /* Back up source pointer! */
773 result
= CSR_TargetExhausted
;
778 if(ch
<= UNI_MAX_BMP
)
780 /* Target is a character <= 0xFFFF */
781 /* UTF-16 surrogate values are illegal in UTF-32 */
782 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
)
784 if(flags
== CSF_StrictConversion
)
786 source
-= (extraBytesToRead
+ 1); /* return to the illegal value itself */
787 result
= CSR_SourceIllegal
;
793 *target
++ = UNI_REPLACEMENT_CHAR
;
798 *target
++ = (UTF16
) ch
; /* normal case */
801 else if(ch
> UNI_MAX_UTF16
)
803 if(flags
== CSF_StrictConversion
)
805 result
= CSR_SourceIllegal
;
806 source
-= (extraBytesToRead
+ 1); /* return to the start */
808 break; /* Bail out; shouldn't continue */
812 *target
++ = UNI_REPLACEMENT_CHAR
;
817 /* target is a character in range 0xFFFF - 0x10FFFF. */
818 if(target
+ 1 >= targetEnd
)
820 source
-= (extraBytesToRead
+ 1); /* Back up source pointer! */
821 result
= CSR_TargetExhausted
;
827 *target
++ = (UTF16
) ((ch
>> halfShift
) + UNI_SUR_HIGH_START
);
828 *target
++ = (UTF16
) ((ch
& halfMask
) + UNI_SUR_LOW_START
);
832 *sourceStart
= source
;
833 *targetStart
= target
;
843 LIBSTUB(CodesetsConvertUTF8toUTF16
, ULONG
, REG(a0
, const UTF8
** sourceStart
),
844 REG(a1
, const UTF8
* sourceEnd
),
845 REG(a2
, UTF16
** targetStart
),
846 REG(a3
, UTF16
* targetEnd
),
847 REG(d0
, ULONG flags
))
850 return CodesetsConvertUTF8toUTF16((const UTF8
**)REG_A0
, (const UTF8
*)REG_A1
, (UTF16
**)REG_A2
, (UTF16
*)REG_A3
, (ULONG
)REG_D0
);
852 return CodesetsConvertUTF8toUTF16(sourceStart
, sourceEnd
, targetStart
, targetEnd
, flags
);
857 /***********************************************************************/
860 AROS_LH5(ULONG
, CodesetsConvertUTF32toUTF8
,
861 AROS_LHA(const UTF32
**, sourceStart
, A0
),
862 AROS_LHA(const UTF32
*, sourceEnd
, A1
),
863 AROS_LHA(UTF8
**, targetStart
, A2
),
864 AROS_LHA(UTF8
*, targetEnd
, A3
),
865 AROS_LHA(ULONG
, flags
, D0
),
866 struct CodesetsBase
*, library
, 11, Codesets
872 CodesetsConvertUTF32toUTF8(REG(a0
, const UTF32
** sourceStart
),
873 REG(a1
, const UTF32
* sourceEnd
),
874 REG(a2
, UTF8
** targetStart
),
875 REG(a3
, UTF8
* targetEnd
),
876 REG(d0
, ULONG flags
))
879 ULONG result
= CSR_ConversionOK
;
880 const UTF32
*source
= *sourceStart
;
881 UTF8
*target
= *targetStart
;
885 while(source
< sourceEnd
)
888 unsigned short bytesToWrite
= 0;
889 const UTF32 byteMask
= 0xBF;
890 const UTF32 byteMark
= 0x80;
894 if(flags
== CSF_StrictConversion
)
896 /* UTF-16 surrogate values are illegal in UTF-32 */
897 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
)
899 --source
; /* return to the illegal value itself */
900 result
= CSR_SourceIllegal
;
907 * Figure out how many bytes the result will require. Turn any
908 * illegally large UTF32 things (> Plane 17) into replacement chars.
910 if(ch
< (UTF32
) 0x80)
914 else if(ch
< (UTF32
) 0x800)
918 else if(ch
< (UTF32
) 0x10000)
922 else if(ch
<= UNI_MAX_LEGAL_UTF32
)
929 ch
= UNI_REPLACEMENT_CHAR
;
930 result
= CSR_SourceIllegal
;
933 target
+= bytesToWrite
;
934 if(target
> targetEnd
)
936 --source
; /* Back up source pointer! */
937 target
-= bytesToWrite
;
938 result
= CSR_TargetExhausted
;
944 /* note: everything falls through. */
946 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
950 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
954 *--target
= (UTF8
) ((ch
| byteMark
) & byteMask
);
958 *--target
= (UTF8
) (ch
| firstByteMark
[bytesToWrite
]);
961 target
+= bytesToWrite
;
964 *sourceStart
= source
;
965 *targetStart
= target
;
975 LIBSTUB(CodesetsConvertUTF32toUTF8
, ULONG
, REG(a0
, const UTF32
** sourceStart
),
976 REG(a1
, const UTF32
* sourceEnd
),
977 REG(a2
, UTF8
** targetStart
),
978 REG(a3
, UTF8
* targetEnd
),
979 REG(d0
, ULONG flags
))
982 return CodesetsConvertUTF32toUTF8((const UTF32
**)REG_A0
, (const UTF32
*)REG_A1
, (UTF8
**)REG_A2
, (UTF8
*)REG_A3
, (ULONG
)REG_D0
);
984 return CodesetsConvertUTF32toUTF8(sourceStart
, sourceEnd
, targetStart
, targetEnd
, flags
);
989 /***********************************************************************/
992 AROS_LH5(ULONG
, CodesetsConvertUTF8toUTF32
,
993 AROS_LHA(const UTF8
**, sourceStart
, A0
),
994 AROS_LHA(const UTF8
*, sourceEnd
, A1
),
995 AROS_LHA(UTF32
**, targetStart
, A2
),
996 AROS_LHA(UTF32
*, targetEnd
, A3
),
997 AROS_LHA(ULONG
, flags
, D0
),
998 struct CodesetsBase
*, library
, 12, Codesets
1004 CodesetsConvertUTF8toUTF32(REG(a0
, const UTF8
** sourceStart
),
1005 REG(a1
, const UTF8
* sourceEnd
),
1006 REG(a2
, UTF32
** targetStart
),
1007 REG(a3
, UTF32
* targetEnd
),
1008 REG(d0
, ULONG flags
))
1011 ULONG result
= CSR_ConversionOK
;
1012 const UTF8
*source
= *sourceStart
;
1013 UTF32
*target
= *targetStart
;
1017 while(source
< sourceEnd
)
1020 unsigned short extraBytesToRead
= trailingBytesForUTF8
[*source
];
1022 if(source
+ extraBytesToRead
>= sourceEnd
)
1024 result
= CSR_SourceExhausted
;
1028 /* Do this check whether lenient or strict */
1029 if(!CodesetsIsLegalUTF8(source
, extraBytesToRead
+ 1))
1031 result
= CSR_SourceIllegal
;
1036 * The cases all fall through. See "Note A" below.
1038 switch (extraBytesToRead
)
1064 ch
-= offsetsFromUTF8
[extraBytesToRead
];
1066 if(target
>= targetEnd
)
1068 source
-= (extraBytesToRead
+ 1); /* Back up the source pointer! */
1069 result
= CSR_TargetExhausted
;
1074 if(ch
<= UNI_MAX_LEGAL_UTF32
)
1077 * UTF-16 surrogate values are illegal in UTF-32, and anything
1078 * over Plane 17 (> 0x10FFFF) is illegal.
1080 if(ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
)
1082 if(flags
== CSF_StrictConversion
)
1084 source
-= (extraBytesToRead
+ 1); /* return to the illegal value itself */
1085 result
= CSR_SourceIllegal
;
1091 *target
++ = UNI_REPLACEMENT_CHAR
;
1101 /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
1102 result
= CSR_SourceIllegal
;
1103 *target
++ = UNI_REPLACEMENT_CHAR
;
1107 *sourceStart
= source
;
1108 *targetStart
= target
;
1118 LIBSTUB(CodesetsConvertUTF8toUTF32
, ULONG
, REG(a0
, const UTF8
** sourceStart
),
1119 REG(a1
, const UTF8
* sourceEnd
),
1120 REG(a2
, UTF32
** targetStart
),
1121 REG(a3
, UTF32
* targetEnd
),
1122 REG(d0
, ULONG flags
))
1125 return CodesetsConvertUTF8toUTF32((const UTF8
**)REG_A0
, (const UTF8
*)REG_A1
, (UTF32
**)REG_A2
, (UTF32
*)REG_A3
, (ULONG
)REG_D0
);
1127 return CodesetsConvertUTF8toUTF32(sourceStart
, sourceEnd
, targetStart
, targetEnd
, flags
);
1132 /***********************************************************************
1135 The fall-through switches in UTF-8 reading code save a
1136 temp variable, some decrements & conditionals. The switches
1137 are equivalent to the following loop:
1139 int tmpBytesToRead = extraBytesToRead+1;
1143 if (tmpBytesToRead) ch <<= 6;
1144 } while (tmpBytesToRead > 0);
1146 In UTF-8 writing code, the switches on "bytesToWrite" are
1147 similarly unrolled loops.
1149 ***********************************************************************/