1*0b57cec5SDimitry Andric /*===--- ConvertUTF.c - Universal Character Names conversions ---------------===
2*0b57cec5SDimitry Andric  *
3*0b57cec5SDimitry Andric  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric  * See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric  *
7*0b57cec5SDimitry Andric  *===------------------------------------------------------------------------=*/
8*0b57cec5SDimitry Andric /*
9*0b57cec5SDimitry Andric  * Copyright 2001-2004 Unicode, Inc.
10*0b57cec5SDimitry Andric  *
11*0b57cec5SDimitry Andric  * Disclaimer
12*0b57cec5SDimitry Andric  *
13*0b57cec5SDimitry Andric  * This source code is provided as is by Unicode, Inc. No claims are
14*0b57cec5SDimitry Andric  * made as to fitness for any particular purpose. No warranties of any
15*0b57cec5SDimitry Andric  * kind are expressed or implied. The recipient agrees to determine
16*0b57cec5SDimitry Andric  * applicability of information provided. If this file has been
17*0b57cec5SDimitry Andric  * purchased on magnetic or optical media from Unicode, Inc., the
18*0b57cec5SDimitry Andric  * sole remedy for any claim will be exchange of defective media
19*0b57cec5SDimitry Andric  * within 90 days of receipt.
20*0b57cec5SDimitry Andric  *
21*0b57cec5SDimitry Andric  * Limitations on Rights to Redistribute This Code
22*0b57cec5SDimitry Andric  *
23*0b57cec5SDimitry Andric  * Unicode, Inc. hereby grants the right to freely use the information
24*0b57cec5SDimitry Andric  * supplied in this file in the creation of products supporting the
25*0b57cec5SDimitry Andric  * Unicode Standard, and to make copies of this file in any form
26*0b57cec5SDimitry Andric  * for internal or external distribution as long as this notice
27*0b57cec5SDimitry Andric  * remains attached.
28*0b57cec5SDimitry Andric  */
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric /* ---------------------------------------------------------------------
31*0b57cec5SDimitry Andric 
32*0b57cec5SDimitry Andric     Conversions between UTF32, UTF-16, and UTF-8. Source code file.
33*0b57cec5SDimitry Andric     Author: Mark E. Davis, 1994.
34*0b57cec5SDimitry Andric     Rev History: Rick McGowan, fixes & updates May 2001.
35*0b57cec5SDimitry Andric     Sept 2001: fixed const & error conditions per
36*0b57cec5SDimitry Andric         mods suggested by S. Parent & A. Lillich.
37*0b57cec5SDimitry Andric     June 2002: Tim Dodd added detection and handling of incomplete
38*0b57cec5SDimitry Andric         source sequences, enhanced error detection, added casts
39*0b57cec5SDimitry Andric         to eliminate compiler warnings.
40*0b57cec5SDimitry Andric     July 2003: slight mods to back out aggressive FFFE detection.
41*0b57cec5SDimitry Andric     Jan 2004: updated switches in from-UTF8 conversions.
42*0b57cec5SDimitry Andric     Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions.
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric     See the header file "ConvertUTF.h" for complete documentation.
45*0b57cec5SDimitry Andric 
46*0b57cec5SDimitry Andric ------------------------------------------------------------------------ */
47*0b57cec5SDimitry Andric 
48*0b57cec5SDimitry Andric #include "llvm/Support/ConvertUTF.h"
49*0b57cec5SDimitry Andric #ifdef CVTUTF_DEBUG
50*0b57cec5SDimitry Andric #include <stdio.h>
51*0b57cec5SDimitry Andric #endif
52*0b57cec5SDimitry Andric #include <assert.h>
53*0b57cec5SDimitry Andric 
54*0b57cec5SDimitry Andric /*
55*0b57cec5SDimitry Andric  * This code extensively uses fall-through switches.
56*0b57cec5SDimitry Andric  * Keep the compiler from warning about that.
57*0b57cec5SDimitry Andric  */
58*0b57cec5SDimitry Andric #if defined(__clang__) && defined(__has_warning)
59*0b57cec5SDimitry Andric # if __has_warning("-Wimplicit-fallthrough")
60*0b57cec5SDimitry Andric #  define ConvertUTF_DISABLE_WARNINGS \
61*0b57cec5SDimitry Andric     _Pragma("clang diagnostic push")  \
62*0b57cec5SDimitry Andric     _Pragma("clang diagnostic ignored \"-Wimplicit-fallthrough\"")
63*0b57cec5SDimitry Andric #  define ConvertUTF_RESTORE_WARNINGS \
64*0b57cec5SDimitry Andric     _Pragma("clang diagnostic pop")
65*0b57cec5SDimitry Andric # endif
66*0b57cec5SDimitry Andric #elif defined(__GNUC__) && __GNUC__ > 6
67*0b57cec5SDimitry Andric # define ConvertUTF_DISABLE_WARNINGS \
68*0b57cec5SDimitry Andric    _Pragma("GCC diagnostic push")    \
69*0b57cec5SDimitry Andric    _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
70*0b57cec5SDimitry Andric # define ConvertUTF_RESTORE_WARNINGS \
71*0b57cec5SDimitry Andric    _Pragma("GCC diagnostic pop")
72*0b57cec5SDimitry Andric #endif
73*0b57cec5SDimitry Andric #ifndef ConvertUTF_DISABLE_WARNINGS
74*0b57cec5SDimitry Andric # define ConvertUTF_DISABLE_WARNINGS
75*0b57cec5SDimitry Andric #endif
76*0b57cec5SDimitry Andric #ifndef ConvertUTF_RESTORE_WARNINGS
77*0b57cec5SDimitry Andric # define ConvertUTF_RESTORE_WARNINGS
78*0b57cec5SDimitry Andric #endif
79*0b57cec5SDimitry Andric 
80*0b57cec5SDimitry Andric ConvertUTF_DISABLE_WARNINGS
81*0b57cec5SDimitry Andric 
82*0b57cec5SDimitry Andric namespace llvm {
83*0b57cec5SDimitry Andric 
84*0b57cec5SDimitry Andric static const int halfShift  = 10; /* used for shifting by 10 bits */
85*0b57cec5SDimitry Andric 
86*0b57cec5SDimitry Andric static const UTF32 halfBase = 0x0010000UL;
87*0b57cec5SDimitry Andric static const UTF32 halfMask = 0x3FFUL;
88*0b57cec5SDimitry Andric 
89*0b57cec5SDimitry Andric #define UNI_SUR_HIGH_START  (UTF32)0xD800
90*0b57cec5SDimitry Andric #define UNI_SUR_HIGH_END    (UTF32)0xDBFF
91*0b57cec5SDimitry Andric #define UNI_SUR_LOW_START   (UTF32)0xDC00
92*0b57cec5SDimitry Andric #define UNI_SUR_LOW_END     (UTF32)0xDFFF
93*0b57cec5SDimitry Andric 
94*0b57cec5SDimitry Andric /* --------------------------------------------------------------------- */
95*0b57cec5SDimitry Andric 
96*0b57cec5SDimitry Andric /*
97*0b57cec5SDimitry Andric  * Index into the table below with the first byte of a UTF-8 sequence to
98*0b57cec5SDimitry Andric  * get the number of trailing bytes that are supposed to follow it.
99*0b57cec5SDimitry Andric  * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
100*0b57cec5SDimitry Andric  * left as-is for anyone who may want to do such conversion, which was
101*0b57cec5SDimitry Andric  * allowed in earlier algorithms.
102*0b57cec5SDimitry Andric  */
103*0b57cec5SDimitry Andric static const char trailingBytesForUTF8[256] = {
104*0b57cec5SDimitry Andric     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,
105*0b57cec5SDimitry Andric     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,
106*0b57cec5SDimitry Andric     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,
107*0b57cec5SDimitry Andric     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,
108*0b57cec5SDimitry Andric     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,
109*0b57cec5SDimitry Andric     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,
110*0b57cec5SDimitry Andric     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,
111*0b57cec5SDimitry Andric     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
112*0b57cec5SDimitry Andric };
113*0b57cec5SDimitry Andric 
114*0b57cec5SDimitry Andric /*
115*0b57cec5SDimitry Andric  * Magic values subtracted from a buffer value during UTF8 conversion.
116*0b57cec5SDimitry Andric  * This table contains as many values as there might be trailing bytes
117*0b57cec5SDimitry Andric  * in a UTF-8 sequence.
118*0b57cec5SDimitry Andric  */
119*0b57cec5SDimitry Andric static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
120*0b57cec5SDimitry Andric                      0x03C82080UL, 0xFA082080UL, 0x82082080UL };
121*0b57cec5SDimitry Andric 
122*0b57cec5SDimitry Andric /*
123*0b57cec5SDimitry Andric  * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
124*0b57cec5SDimitry Andric  * into the first byte, depending on how many bytes follow.  There are
125*0b57cec5SDimitry Andric  * as many entries in this table as there are UTF-8 sequence types.
126*0b57cec5SDimitry Andric  * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
127*0b57cec5SDimitry Andric  * for *legal* UTF-8 will be 4 or fewer bytes total.
128*0b57cec5SDimitry Andric  */
129*0b57cec5SDimitry Andric static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
130*0b57cec5SDimitry Andric 
131*0b57cec5SDimitry Andric /* --------------------------------------------------------------------- */
132*0b57cec5SDimitry Andric 
133*0b57cec5SDimitry Andric /* The interface converts a whole buffer to avoid function-call overhead.
134*0b57cec5SDimitry Andric  * Constants have been gathered. Loops & conditionals have been removed as
135*0b57cec5SDimitry Andric  * much as possible for efficiency, in favor of drop-through switches.
136*0b57cec5SDimitry Andric  * (See "Note A" at the bottom of the file for equivalent code.)
137*0b57cec5SDimitry Andric  * If your compiler supports it, the "isLegalUTF8" call can be turned
138*0b57cec5SDimitry Andric  * into an inline function.
139*0b57cec5SDimitry Andric  */
140*0b57cec5SDimitry Andric 
141*0b57cec5SDimitry Andric 
142*0b57cec5SDimitry Andric /* --------------------------------------------------------------------- */
143*0b57cec5SDimitry Andric 
ConvertUTF32toUTF16(const UTF32 ** sourceStart,const UTF32 * sourceEnd,UTF16 ** targetStart,UTF16 * targetEnd,ConversionFlags flags)144*0b57cec5SDimitry Andric ConversionResult ConvertUTF32toUTF16 (
145*0b57cec5SDimitry Andric         const UTF32** sourceStart, const UTF32* sourceEnd,
146*0b57cec5SDimitry Andric         UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
147*0b57cec5SDimitry Andric     ConversionResult result = conversionOK;
148*0b57cec5SDimitry Andric     const UTF32* source = *sourceStart;
149*0b57cec5SDimitry Andric     UTF16* target = *targetStart;
150*0b57cec5SDimitry Andric     while (source < sourceEnd) {
151*0b57cec5SDimitry Andric         UTF32 ch;
152*0b57cec5SDimitry Andric         if (target >= targetEnd) {
153*0b57cec5SDimitry Andric             result = targetExhausted; break;
154*0b57cec5SDimitry Andric         }
155*0b57cec5SDimitry Andric         ch = *source++;
156*0b57cec5SDimitry Andric         if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
157*0b57cec5SDimitry Andric             /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
158*0b57cec5SDimitry Andric             if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
159*0b57cec5SDimitry Andric                 if (flags == strictConversion) {
160*0b57cec5SDimitry Andric                     --source; /* return to the illegal value itself */
161*0b57cec5SDimitry Andric                     result = sourceIllegal;
162*0b57cec5SDimitry Andric                     break;
163*0b57cec5SDimitry Andric                 } else {
164*0b57cec5SDimitry Andric                     *target++ = UNI_REPLACEMENT_CHAR;
165*0b57cec5SDimitry Andric                 }
166*0b57cec5SDimitry Andric             } else {
167*0b57cec5SDimitry Andric                 *target++ = (UTF16)ch; /* normal case */
168*0b57cec5SDimitry Andric             }
169*0b57cec5SDimitry Andric         } else if (ch > UNI_MAX_LEGAL_UTF32) {
170*0b57cec5SDimitry Andric             if (flags == strictConversion) {
171*0b57cec5SDimitry Andric                 result = sourceIllegal;
172*0b57cec5SDimitry Andric             } else {
173*0b57cec5SDimitry Andric                 *target++ = UNI_REPLACEMENT_CHAR;
174*0b57cec5SDimitry Andric             }
175*0b57cec5SDimitry Andric         } else {
176*0b57cec5SDimitry Andric             /* target is a character in range 0xFFFF - 0x10FFFF. */
177*0b57cec5SDimitry Andric             if (target + 1 >= targetEnd) {
178*0b57cec5SDimitry Andric                 --source; /* Back up source pointer! */
179*0b57cec5SDimitry Andric                 result = targetExhausted; break;
180*0b57cec5SDimitry Andric             }
181*0b57cec5SDimitry Andric             ch -= halfBase;
182*0b57cec5SDimitry Andric             *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
183*0b57cec5SDimitry Andric             *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
184*0b57cec5SDimitry Andric         }
185*0b57cec5SDimitry Andric     }
186*0b57cec5SDimitry Andric     *sourceStart = source;
187*0b57cec5SDimitry Andric     *targetStart = target;
188*0b57cec5SDimitry Andric     return result;
189*0b57cec5SDimitry Andric }
190*0b57cec5SDimitry Andric 
191*0b57cec5SDimitry Andric /* --------------------------------------------------------------------- */
192*0b57cec5SDimitry Andric 
ConvertUTF16toUTF32(const UTF16 ** sourceStart,const UTF16 * sourceEnd,UTF32 ** targetStart,UTF32 * targetEnd,ConversionFlags flags)193*0b57cec5SDimitry Andric ConversionResult ConvertUTF16toUTF32 (
194*0b57cec5SDimitry Andric         const UTF16** sourceStart, const UTF16* sourceEnd,
195*0b57cec5SDimitry Andric         UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
196*0b57cec5SDimitry Andric     ConversionResult result = conversionOK;
197*0b57cec5SDimitry Andric     const UTF16* source = *sourceStart;
198*0b57cec5SDimitry Andric     UTF32* target = *targetStart;
199*0b57cec5SDimitry Andric     UTF32 ch, ch2;
200*0b57cec5SDimitry Andric     while (source < sourceEnd) {
201*0b57cec5SDimitry Andric         const UTF16* oldSource = source; /*  In case we have to back up because of target overflow. */
202*0b57cec5SDimitry Andric         ch = *source++;
203*0b57cec5SDimitry Andric         /* If we have a surrogate pair, convert to UTF32 first. */
204*0b57cec5SDimitry Andric         if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
205*0b57cec5SDimitry Andric             /* If the 16 bits following the high surrogate are in the source buffer... */
206*0b57cec5SDimitry Andric             if (source < sourceEnd) {
207*0b57cec5SDimitry Andric                 ch2 = *source;
208*0b57cec5SDimitry Andric                 /* If it's a low surrogate, convert to UTF32. */
209*0b57cec5SDimitry Andric                 if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
210*0b57cec5SDimitry Andric                     ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
211*0b57cec5SDimitry Andric                         + (ch2 - UNI_SUR_LOW_START) + halfBase;
212*0b57cec5SDimitry Andric                     ++source;
213*0b57cec5SDimitry Andric                 } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
214*0b57cec5SDimitry Andric                     --source; /* return to the illegal value itself */
215*0b57cec5SDimitry Andric                     result = sourceIllegal;
216*0b57cec5SDimitry Andric                     break;
217*0b57cec5SDimitry Andric                 }
218*0b57cec5SDimitry Andric             } else { /* We don't have the 16 bits following the high surrogate. */
219*0b57cec5SDimitry Andric                 --source; /* return to the high surrogate */
220*0b57cec5SDimitry Andric                 result = sourceExhausted;
221*0b57cec5SDimitry Andric                 break;
222*0b57cec5SDimitry Andric             }
223*0b57cec5SDimitry Andric         } else if (flags == strictConversion) {
224*0b57cec5SDimitry Andric             /* UTF-16 surrogate values are illegal in UTF-32 */
225*0b57cec5SDimitry Andric             if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
226*0b57cec5SDimitry Andric                 --source; /* return to the illegal value itself */
227*0b57cec5SDimitry Andric                 result = sourceIllegal;
228*0b57cec5SDimitry Andric                 break;
229*0b57cec5SDimitry Andric             }
230*0b57cec5SDimitry Andric         }
231*0b57cec5SDimitry Andric         if (target >= targetEnd) {
232*0b57cec5SDimitry Andric             source = oldSource; /* Back up source pointer! */
233*0b57cec5SDimitry Andric             result = targetExhausted; break;
234*0b57cec5SDimitry Andric         }
235*0b57cec5SDimitry Andric         *target++ = ch;
236*0b57cec5SDimitry Andric     }
237*0b57cec5SDimitry Andric     *sourceStart = source;
238*0b57cec5SDimitry Andric     *targetStart = target;
239*0b57cec5SDimitry Andric #ifdef CVTUTF_DEBUG
240*0b57cec5SDimitry Andric if (result == sourceIllegal) {
241*0b57cec5SDimitry Andric     fprintf(stderr, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch, ch2);
242*0b57cec5SDimitry Andric     fflush(stderr);
243*0b57cec5SDimitry Andric }
244*0b57cec5SDimitry Andric #endif
245*0b57cec5SDimitry Andric     return result;
246*0b57cec5SDimitry Andric }
ConvertUTF16toUTF8(const UTF16 ** sourceStart,const UTF16 * sourceEnd,UTF8 ** targetStart,UTF8 * targetEnd,ConversionFlags flags)247*0b57cec5SDimitry Andric ConversionResult ConvertUTF16toUTF8 (
248*0b57cec5SDimitry Andric         const UTF16** sourceStart, const UTF16* sourceEnd,
249*0b57cec5SDimitry Andric         UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
250*0b57cec5SDimitry Andric     ConversionResult result = conversionOK;
251*0b57cec5SDimitry Andric     const UTF16* source = *sourceStart;
252*0b57cec5SDimitry Andric     UTF8* target = *targetStart;
253*0b57cec5SDimitry Andric     while (source < sourceEnd) {
254*0b57cec5SDimitry Andric         UTF32 ch;
255*0b57cec5SDimitry Andric         unsigned short bytesToWrite = 0;
256*0b57cec5SDimitry Andric         const UTF32 byteMask = 0xBF;
257*0b57cec5SDimitry Andric         const UTF32 byteMark = 0x80;
258*0b57cec5SDimitry Andric         const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
259*0b57cec5SDimitry Andric         ch = *source++;
260*0b57cec5SDimitry Andric         /* If we have a surrogate pair, convert to UTF32 first. */
261*0b57cec5SDimitry Andric         if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
262*0b57cec5SDimitry Andric             /* If the 16 bits following the high surrogate are in the source buffer... */
263*0b57cec5SDimitry Andric             if (source < sourceEnd) {
264*0b57cec5SDimitry Andric                 UTF32 ch2 = *source;
265*0b57cec5SDimitry Andric                 /* If it's a low surrogate, convert to UTF32. */
266*0b57cec5SDimitry Andric                 if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
267*0b57cec5SDimitry Andric                     ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
268*0b57cec5SDimitry Andric                         + (ch2 - UNI_SUR_LOW_START) + halfBase;
269*0b57cec5SDimitry Andric                     ++source;
270*0b57cec5SDimitry Andric                 } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
271*0b57cec5SDimitry Andric                     --source; /* return to the illegal value itself */
272*0b57cec5SDimitry Andric                     result = sourceIllegal;
273*0b57cec5SDimitry Andric                     break;
274*0b57cec5SDimitry Andric                 }
275*0b57cec5SDimitry Andric             } else { /* We don't have the 16 bits following the high surrogate. */
276*0b57cec5SDimitry Andric                 --source; /* return to the high surrogate */
277*0b57cec5SDimitry Andric                 result = sourceExhausted;
278*0b57cec5SDimitry Andric                 break;
279*0b57cec5SDimitry Andric             }
280*0b57cec5SDimitry Andric         } else if (flags == strictConversion) {
281*0b57cec5SDimitry Andric             /* UTF-16 surrogate values are illegal in UTF-32 */
282*0b57cec5SDimitry Andric             if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
283*0b57cec5SDimitry Andric                 --source; /* return to the illegal value itself */
284*0b57cec5SDimitry Andric                 result = sourceIllegal;
285*0b57cec5SDimitry Andric                 break;
286*0b57cec5SDimitry Andric             }
287*0b57cec5SDimitry Andric         }
288*0b57cec5SDimitry Andric         /* Figure out how many bytes the result will require */
289*0b57cec5SDimitry Andric         if (ch < (UTF32)0x80) {      bytesToWrite = 1;
290*0b57cec5SDimitry Andric         } else if (ch < (UTF32)0x800) {     bytesToWrite = 2;
291*0b57cec5SDimitry Andric         } else if (ch < (UTF32)0x10000) {   bytesToWrite = 3;
292*0b57cec5SDimitry Andric         } else if (ch < (UTF32)0x110000) {  bytesToWrite = 4;
293*0b57cec5SDimitry Andric         } else {                            bytesToWrite = 3;
294*0b57cec5SDimitry Andric                                             ch = UNI_REPLACEMENT_CHAR;
295*0b57cec5SDimitry Andric         }
296*0b57cec5SDimitry Andric 
297*0b57cec5SDimitry Andric         target += bytesToWrite;
298*0b57cec5SDimitry Andric         if (target > targetEnd) {
299*0b57cec5SDimitry Andric             source = oldSource; /* Back up source pointer! */
300*0b57cec5SDimitry Andric             target -= bytesToWrite; result = targetExhausted; break;
301*0b57cec5SDimitry Andric         }
302*0b57cec5SDimitry Andric         switch (bytesToWrite) { /* note: everything falls through. */
303*0b57cec5SDimitry Andric             case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
304*0b57cec5SDimitry Andric             case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
305*0b57cec5SDimitry Andric             case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
306*0b57cec5SDimitry Andric             case 1: *--target =  (UTF8)(ch | firstByteMark[bytesToWrite]);
307*0b57cec5SDimitry Andric         }
308*0b57cec5SDimitry Andric         target += bytesToWrite;
309*0b57cec5SDimitry Andric     }
310*0b57cec5SDimitry Andric     *sourceStart = source;
311*0b57cec5SDimitry Andric     *targetStart = target;
312*0b57cec5SDimitry Andric     return result;
313*0b57cec5SDimitry Andric }
314*0b57cec5SDimitry Andric 
315*0b57cec5SDimitry Andric /* --------------------------------------------------------------------- */
316*0b57cec5SDimitry Andric 
ConvertUTF32toUTF8(const UTF32 ** sourceStart,const UTF32 * sourceEnd,UTF8 ** targetStart,UTF8 * targetEnd,ConversionFlags flags)317*0b57cec5SDimitry Andric ConversionResult ConvertUTF32toUTF8 (
318*0b57cec5SDimitry Andric         const UTF32** sourceStart, const UTF32* sourceEnd,
319*0b57cec5SDimitry Andric         UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
320*0b57cec5SDimitry Andric     ConversionResult result = conversionOK;
321*0b57cec5SDimitry Andric     const UTF32* source = *sourceStart;
322*0b57cec5SDimitry Andric     UTF8* target = *targetStart;
323*0b57cec5SDimitry Andric     while (source < sourceEnd) {
324*0b57cec5SDimitry Andric         UTF32 ch;
325*0b57cec5SDimitry Andric         unsigned short bytesToWrite = 0;
326*0b57cec5SDimitry Andric         const UTF32 byteMask = 0xBF;
327*0b57cec5SDimitry Andric         const UTF32 byteMark = 0x80;
328*0b57cec5SDimitry Andric         ch = *source++;
329*0b57cec5SDimitry Andric         if (flags == strictConversion ) {
330*0b57cec5SDimitry Andric             /* UTF-16 surrogate values are illegal in UTF-32 */
331*0b57cec5SDimitry Andric             if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
332*0b57cec5SDimitry Andric                 --source; /* return to the illegal value itself */
333*0b57cec5SDimitry Andric                 result = sourceIllegal;
334*0b57cec5SDimitry Andric                 break;
335*0b57cec5SDimitry Andric             }
336*0b57cec5SDimitry Andric         }
337*0b57cec5SDimitry Andric         /*
338*0b57cec5SDimitry Andric          * Figure out how many bytes the result will require. Turn any
339*0b57cec5SDimitry Andric          * illegally large UTF32 things (> Plane 17) into replacement chars.
340*0b57cec5SDimitry Andric          */
341*0b57cec5SDimitry Andric         if (ch < (UTF32)0x80) {      bytesToWrite = 1;
342*0b57cec5SDimitry Andric         } else if (ch < (UTF32)0x800) {     bytesToWrite = 2;
343*0b57cec5SDimitry Andric         } else if (ch < (UTF32)0x10000) {   bytesToWrite = 3;
344*0b57cec5SDimitry Andric         } else if (ch <= UNI_MAX_LEGAL_UTF32) {  bytesToWrite = 4;
345*0b57cec5SDimitry Andric         } else {                            bytesToWrite = 3;
346*0b57cec5SDimitry Andric                                             ch = UNI_REPLACEMENT_CHAR;
347*0b57cec5SDimitry Andric                                             result = sourceIllegal;
348*0b57cec5SDimitry Andric         }
349*0b57cec5SDimitry Andric 
350*0b57cec5SDimitry Andric         target += bytesToWrite;
351*0b57cec5SDimitry Andric         if (target > targetEnd) {
352*0b57cec5SDimitry Andric             --source; /* Back up source pointer! */
353*0b57cec5SDimitry Andric             target -= bytesToWrite; result = targetExhausted; break;
354*0b57cec5SDimitry Andric         }
355*0b57cec5SDimitry Andric         switch (bytesToWrite) { /* note: everything falls through. */
356*0b57cec5SDimitry Andric             case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
357*0b57cec5SDimitry Andric             case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
358*0b57cec5SDimitry Andric             case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
359*0b57cec5SDimitry Andric             case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]);
360*0b57cec5SDimitry Andric         }
361*0b57cec5SDimitry Andric         target += bytesToWrite;
362*0b57cec5SDimitry Andric     }
363*0b57cec5SDimitry Andric     *sourceStart = source;
364*0b57cec5SDimitry Andric     *targetStart = target;
365*0b57cec5SDimitry Andric     return result;
366*0b57cec5SDimitry Andric }
367*0b57cec5SDimitry Andric 
368*0b57cec5SDimitry Andric /* --------------------------------------------------------------------- */
369*0b57cec5SDimitry Andric 
370*0b57cec5SDimitry Andric /*
371*0b57cec5SDimitry Andric  * Utility routine to tell whether a sequence of bytes is legal UTF-8.
372*0b57cec5SDimitry Andric  * This must be called with the length pre-determined by the first byte.
373*0b57cec5SDimitry Andric  * If not calling this from ConvertUTF8to*, then the length can be set by:
374*0b57cec5SDimitry Andric  *  length = trailingBytesForUTF8[*source]+1;
375*0b57cec5SDimitry Andric  * and the sequence is illegal right away if there aren't that many bytes
376*0b57cec5SDimitry Andric  * available.
377*0b57cec5SDimitry Andric  * If presented with a length > 4, this returns false.  The Unicode
378*0b57cec5SDimitry Andric  * definition of UTF-8 goes up to 4-byte sequences.
379*0b57cec5SDimitry Andric  */
380*0b57cec5SDimitry Andric 
isLegalUTF8(const UTF8 * source,int length)381*0b57cec5SDimitry Andric static Boolean isLegalUTF8(const UTF8 *source, int length) {
382*0b57cec5SDimitry Andric     UTF8 a;
383*0b57cec5SDimitry Andric     const UTF8 *srcptr = source+length;
384*0b57cec5SDimitry Andric     switch (length) {
385*0b57cec5SDimitry Andric     default: return false;
386*0b57cec5SDimitry Andric         /* Everything else falls through when "true"... */
387*0b57cec5SDimitry Andric     case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
388*0b57cec5SDimitry Andric     case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
389*0b57cec5SDimitry Andric     case 2: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
390*0b57cec5SDimitry Andric 
391*0b57cec5SDimitry Andric         switch (*source) {
392*0b57cec5SDimitry Andric             /* no fall-through in this inner switch */
393*0b57cec5SDimitry Andric             case 0xE0: if (a < 0xA0) return false; break;
394*0b57cec5SDimitry Andric             case 0xED: if (a > 0x9F) return false; break;
395*0b57cec5SDimitry Andric             case 0xF0: if (a < 0x90) return false; break;
396*0b57cec5SDimitry Andric             case 0xF4: if (a > 0x8F) return false; break;
397*0b57cec5SDimitry Andric             default:   if (a < 0x80) return false;
398*0b57cec5SDimitry Andric         }
399*0b57cec5SDimitry Andric 
400*0b57cec5SDimitry Andric     case 1: if (*source >= 0x80 && *source < 0xC2) return false;
401*0b57cec5SDimitry Andric     }
402*0b57cec5SDimitry Andric     if (*source > 0xF4) return false;
403*0b57cec5SDimitry Andric     return true;
404*0b57cec5SDimitry Andric }
405*0b57cec5SDimitry Andric 
406*0b57cec5SDimitry Andric /* --------------------------------------------------------------------- */
407*0b57cec5SDimitry Andric 
408*0b57cec5SDimitry Andric /*
409*0b57cec5SDimitry Andric  * Exported function to return whether a UTF-8 sequence is legal or not.
410*0b57cec5SDimitry Andric  * This is not used here; it's just exported.
411*0b57cec5SDimitry Andric  */
isLegalUTF8Sequence(const UTF8 * source,const UTF8 * sourceEnd)412*0b57cec5SDimitry Andric Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) {
413*0b57cec5SDimitry Andric     int length = trailingBytesForUTF8[*source]+1;
414*0b57cec5SDimitry Andric     if (length > sourceEnd - source) {
415*0b57cec5SDimitry Andric         return false;
416*0b57cec5SDimitry Andric     }
417*0b57cec5SDimitry Andric     return isLegalUTF8(source, length);
418*0b57cec5SDimitry Andric }
419*0b57cec5SDimitry Andric 
420*0b57cec5SDimitry Andric /* --------------------------------------------------------------------- */
421*0b57cec5SDimitry Andric 
422*0b57cec5SDimitry Andric static unsigned
findMaximalSubpartOfIllFormedUTF8Sequence(const UTF8 * source,const UTF8 * sourceEnd)423*0b57cec5SDimitry Andric findMaximalSubpartOfIllFormedUTF8Sequence(const UTF8 *source,
424*0b57cec5SDimitry Andric                                           const UTF8 *sourceEnd) {
425*0b57cec5SDimitry Andric   UTF8 b1, b2, b3;
426*0b57cec5SDimitry Andric 
427*0b57cec5SDimitry Andric   assert(!isLegalUTF8Sequence(source, sourceEnd));
428*0b57cec5SDimitry Andric 
429*0b57cec5SDimitry Andric   /*
430*0b57cec5SDimitry Andric    * Unicode 6.3.0, D93b:
431*0b57cec5SDimitry Andric    *
432*0b57cec5SDimitry Andric    *   Maximal subpart of an ill-formed subsequence: The longest code unit
433*0b57cec5SDimitry Andric    *   subsequence starting at an unconvertible offset that is either:
434*0b57cec5SDimitry Andric    *   a. the initial subsequence of a well-formed code unit sequence, or
435*0b57cec5SDimitry Andric    *   b. a subsequence of length one.
436*0b57cec5SDimitry Andric    */
437*0b57cec5SDimitry Andric 
438*0b57cec5SDimitry Andric   if (source == sourceEnd)
439*0b57cec5SDimitry Andric     return 0;
440*0b57cec5SDimitry Andric 
441*0b57cec5SDimitry Andric   /*
442*0b57cec5SDimitry Andric    * Perform case analysis.  See Unicode 6.3.0, Table 3-7. Well-Formed UTF-8
443*0b57cec5SDimitry Andric    * Byte Sequences.
444*0b57cec5SDimitry Andric    */
445*0b57cec5SDimitry Andric 
446*0b57cec5SDimitry Andric   b1 = *source;
447*0b57cec5SDimitry Andric   ++source;
448*0b57cec5SDimitry Andric   if (b1 >= 0xC2 && b1 <= 0xDF) {
449*0b57cec5SDimitry Andric     /*
450*0b57cec5SDimitry Andric      * First byte is valid, but we know that this code unit sequence is
451*0b57cec5SDimitry Andric      * invalid, so the maximal subpart has to end after the first byte.
452*0b57cec5SDimitry Andric      */
453*0b57cec5SDimitry Andric     return 1;
454*0b57cec5SDimitry Andric   }
455*0b57cec5SDimitry Andric 
456*0b57cec5SDimitry Andric   if (source == sourceEnd)
457*0b57cec5SDimitry Andric     return 1;
458*0b57cec5SDimitry Andric 
459*0b57cec5SDimitry Andric   b2 = *source;
460*0b57cec5SDimitry Andric   ++source;
461*0b57cec5SDimitry Andric 
462*0b57cec5SDimitry Andric   if (b1 == 0xE0) {
463*0b57cec5SDimitry Andric     return (b2 >= 0xA0 && b2 <= 0xBF) ? 2 : 1;
464*0b57cec5SDimitry Andric   }
465*0b57cec5SDimitry Andric   if (b1 >= 0xE1 && b1 <= 0xEC) {
466*0b57cec5SDimitry Andric     return (b2 >= 0x80 && b2 <= 0xBF) ? 2 : 1;
467*0b57cec5SDimitry Andric   }
468*0b57cec5SDimitry Andric   if (b1 == 0xED) {
469*0b57cec5SDimitry Andric     return (b2 >= 0x80 && b2 <= 0x9F) ? 2 : 1;
470*0b57cec5SDimitry Andric   }
471*0b57cec5SDimitry Andric   if (b1 >= 0xEE && b1 <= 0xEF) {
472*0b57cec5SDimitry Andric     return (b2 >= 0x80 && b2 <= 0xBF) ? 2 : 1;
473*0b57cec5SDimitry Andric   }
474*0b57cec5SDimitry Andric   if (b1 == 0xF0) {
475*0b57cec5SDimitry Andric     if (b2 >= 0x90 && b2 <= 0xBF) {
476*0b57cec5SDimitry Andric       if (source == sourceEnd)
477*0b57cec5SDimitry Andric         return 2;
478*0b57cec5SDimitry Andric 
479*0b57cec5SDimitry Andric       b3 = *source;
480*0b57cec5SDimitry Andric       return (b3 >= 0x80 && b3 <= 0xBF) ? 3 : 2;
481*0b57cec5SDimitry Andric     }
482*0b57cec5SDimitry Andric     return 1;
483*0b57cec5SDimitry Andric   }
484*0b57cec5SDimitry Andric   if (b1 >= 0xF1 && b1 <= 0xF3) {
485*0b57cec5SDimitry Andric     if (b2 >= 0x80 && b2 <= 0xBF) {
486*0b57cec5SDimitry Andric       if (source == sourceEnd)
487*0b57cec5SDimitry Andric         return 2;
488*0b57cec5SDimitry Andric 
489*0b57cec5SDimitry Andric       b3 = *source;
490*0b57cec5SDimitry Andric       return (b3 >= 0x80 && b3 <= 0xBF) ? 3 : 2;
491*0b57cec5SDimitry Andric     }
492*0b57cec5SDimitry Andric     return 1;
493*0b57cec5SDimitry Andric   }
494*0b57cec5SDimitry Andric   if (b1 == 0xF4) {
495*0b57cec5SDimitry Andric     if (b2 >= 0x80 && b2 <= 0x8F) {
496*0b57cec5SDimitry Andric       if (source == sourceEnd)
497*0b57cec5SDimitry Andric         return 2;
498*0b57cec5SDimitry Andric 
499*0b57cec5SDimitry Andric       b3 = *source;
500*0b57cec5SDimitry Andric       return (b3 >= 0x80 && b3 <= 0xBF) ? 3 : 2;
501*0b57cec5SDimitry Andric     }
502*0b57cec5SDimitry Andric     return 1;
503*0b57cec5SDimitry Andric   }
504*0b57cec5SDimitry Andric 
505*0b57cec5SDimitry Andric   assert((b1 >= 0x80 && b1 <= 0xC1) || b1 >= 0xF5);
506*0b57cec5SDimitry Andric   /*
507*0b57cec5SDimitry Andric    * There are no valid sequences that start with these bytes.  Maximal subpart
508*0b57cec5SDimitry Andric    * is defined to have length 1 in these cases.
509*0b57cec5SDimitry Andric    */
510*0b57cec5SDimitry Andric   return 1;
511*0b57cec5SDimitry Andric }
512*0b57cec5SDimitry Andric 
513*0b57cec5SDimitry Andric /* --------------------------------------------------------------------- */
514*0b57cec5SDimitry Andric 
515*0b57cec5SDimitry Andric /*
516*0b57cec5SDimitry Andric  * Exported function to return the total number of bytes in a codepoint
517*0b57cec5SDimitry Andric  * represented in UTF-8, given the value of the first byte.
518*0b57cec5SDimitry Andric  */
getNumBytesForUTF8(UTF8 first)519*0b57cec5SDimitry Andric unsigned getNumBytesForUTF8(UTF8 first) {
520*0b57cec5SDimitry Andric   return trailingBytesForUTF8[first] + 1;
521*0b57cec5SDimitry Andric }
522*0b57cec5SDimitry Andric 
523*0b57cec5SDimitry Andric /* --------------------------------------------------------------------- */
524*0b57cec5SDimitry Andric 
525*0b57cec5SDimitry Andric /*
526*0b57cec5SDimitry Andric  * Exported function to return whether a UTF-8 string is legal or not.
527*0b57cec5SDimitry Andric  * This is not used here; it's just exported.
528*0b57cec5SDimitry Andric  */
isLegalUTF8String(const UTF8 ** source,const UTF8 * sourceEnd)529*0b57cec5SDimitry Andric Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd) {
530*0b57cec5SDimitry Andric     while (*source != sourceEnd) {
531*0b57cec5SDimitry Andric         int length = trailingBytesForUTF8[**source] + 1;
532*0b57cec5SDimitry Andric         if (length > sourceEnd - *source || !isLegalUTF8(*source, length))
533*0b57cec5SDimitry Andric             return false;
534*0b57cec5SDimitry Andric         *source += length;
535*0b57cec5SDimitry Andric     }
536*0b57cec5SDimitry Andric     return true;
537*0b57cec5SDimitry Andric }
538*0b57cec5SDimitry Andric 
539*0b57cec5SDimitry Andric /* --------------------------------------------------------------------- */
540*0b57cec5SDimitry Andric 
ConvertUTF8toUTF16(const UTF8 ** sourceStart,const UTF8 * sourceEnd,UTF16 ** targetStart,UTF16 * targetEnd,ConversionFlags flags)541*0b57cec5SDimitry Andric ConversionResult ConvertUTF8toUTF16 (
542*0b57cec5SDimitry Andric         const UTF8** sourceStart, const UTF8* sourceEnd,
543*0b57cec5SDimitry Andric         UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
544*0b57cec5SDimitry Andric     ConversionResult result = conversionOK;
545*0b57cec5SDimitry Andric     const UTF8* source = *sourceStart;
546*0b57cec5SDimitry Andric     UTF16* target = *targetStart;
547*0b57cec5SDimitry Andric     while (source < sourceEnd) {
548*0b57cec5SDimitry Andric         UTF32 ch = 0;
549*0b57cec5SDimitry Andric         unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
550*0b57cec5SDimitry Andric         if (extraBytesToRead >= sourceEnd - source) {
551*0b57cec5SDimitry Andric             result = sourceExhausted; break;
552*0b57cec5SDimitry Andric         }
553*0b57cec5SDimitry Andric         /* Do this check whether lenient or strict */
554*0b57cec5SDimitry Andric         if (!isLegalUTF8(source, extraBytesToRead+1)) {
555*0b57cec5SDimitry Andric             result = sourceIllegal;
556*0b57cec5SDimitry Andric             break;
557*0b57cec5SDimitry Andric         }
558*0b57cec5SDimitry Andric         /*
559*0b57cec5SDimitry Andric          * The cases all fall through. See "Note A" below.
560*0b57cec5SDimitry Andric          */
561*0b57cec5SDimitry Andric         switch (extraBytesToRead) {
562*0b57cec5SDimitry Andric             case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
563*0b57cec5SDimitry Andric             case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
564*0b57cec5SDimitry Andric             case 3: ch += *source++; ch <<= 6;
565*0b57cec5SDimitry Andric             case 2: ch += *source++; ch <<= 6;
566*0b57cec5SDimitry Andric             case 1: ch += *source++; ch <<= 6;
567*0b57cec5SDimitry Andric             case 0: ch += *source++;
568*0b57cec5SDimitry Andric         }
569*0b57cec5SDimitry Andric         ch -= offsetsFromUTF8[extraBytesToRead];
570*0b57cec5SDimitry Andric 
571*0b57cec5SDimitry Andric         if (target >= targetEnd) {
572*0b57cec5SDimitry Andric             source -= (extraBytesToRead+1); /* Back up source pointer! */
573*0b57cec5SDimitry Andric             result = targetExhausted; break;
574*0b57cec5SDimitry Andric         }
575*0b57cec5SDimitry Andric         if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
576*0b57cec5SDimitry Andric             /* UTF-16 surrogate values are illegal in UTF-32 */
577*0b57cec5SDimitry Andric             if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
578*0b57cec5SDimitry Andric                 if (flags == strictConversion) {
579*0b57cec5SDimitry Andric                     source -= (extraBytesToRead+1); /* return to the illegal value itself */
580*0b57cec5SDimitry Andric                     result = sourceIllegal;
581*0b57cec5SDimitry Andric                     break;
582*0b57cec5SDimitry Andric                 } else {
583*0b57cec5SDimitry Andric                     *target++ = UNI_REPLACEMENT_CHAR;
584*0b57cec5SDimitry Andric                 }
585*0b57cec5SDimitry Andric             } else {
586*0b57cec5SDimitry Andric                 *target++ = (UTF16)ch; /* normal case */
587*0b57cec5SDimitry Andric             }
588*0b57cec5SDimitry Andric         } else if (ch > UNI_MAX_UTF16) {
589*0b57cec5SDimitry Andric             if (flags == strictConversion) {
590*0b57cec5SDimitry Andric                 result = sourceIllegal;
591*0b57cec5SDimitry Andric                 source -= (extraBytesToRead+1); /* return to the start */
592*0b57cec5SDimitry Andric                 break; /* Bail out; shouldn't continue */
593*0b57cec5SDimitry Andric             } else {
594*0b57cec5SDimitry Andric                 *target++ = UNI_REPLACEMENT_CHAR;
595*0b57cec5SDimitry Andric             }
596*0b57cec5SDimitry Andric         } else {
597*0b57cec5SDimitry Andric             /* target is a character in range 0xFFFF - 0x10FFFF. */
598*0b57cec5SDimitry Andric             if (target + 1 >= targetEnd) {
599*0b57cec5SDimitry Andric                 source -= (extraBytesToRead+1); /* Back up source pointer! */
600*0b57cec5SDimitry Andric                 result = targetExhausted; break;
601*0b57cec5SDimitry Andric             }
602*0b57cec5SDimitry Andric             ch -= halfBase;
603*0b57cec5SDimitry Andric             *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
604*0b57cec5SDimitry Andric             *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
605*0b57cec5SDimitry Andric         }
606*0b57cec5SDimitry Andric     }
607*0b57cec5SDimitry Andric     *sourceStart = source;
608*0b57cec5SDimitry Andric     *targetStart = target;
609*0b57cec5SDimitry Andric     return result;
610*0b57cec5SDimitry Andric }
611*0b57cec5SDimitry Andric 
612*0b57cec5SDimitry Andric /* --------------------------------------------------------------------- */
613*0b57cec5SDimitry Andric 
ConvertUTF8toUTF32Impl(const UTF8 ** sourceStart,const UTF8 * sourceEnd,UTF32 ** targetStart,UTF32 * targetEnd,ConversionFlags flags,Boolean InputIsPartial)614*0b57cec5SDimitry Andric static ConversionResult ConvertUTF8toUTF32Impl(
615*0b57cec5SDimitry Andric         const UTF8** sourceStart, const UTF8* sourceEnd,
616*0b57cec5SDimitry Andric         UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags,
617*0b57cec5SDimitry Andric         Boolean InputIsPartial) {
618*0b57cec5SDimitry Andric     ConversionResult result = conversionOK;
619*0b57cec5SDimitry Andric     const UTF8* source = *sourceStart;
620*0b57cec5SDimitry Andric     UTF32* target = *targetStart;
621*0b57cec5SDimitry Andric     while (source < sourceEnd) {
622*0b57cec5SDimitry Andric         UTF32 ch = 0;
623*0b57cec5SDimitry Andric         unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
624*0b57cec5SDimitry Andric         if (extraBytesToRead >= sourceEnd - source) {
625*0b57cec5SDimitry Andric             if (flags == strictConversion || InputIsPartial) {
626*0b57cec5SDimitry Andric                 result = sourceExhausted;
627*0b57cec5SDimitry Andric                 break;
628*0b57cec5SDimitry Andric             } else {
629*0b57cec5SDimitry Andric                 result = sourceIllegal;
630*0b57cec5SDimitry Andric 
631*0b57cec5SDimitry Andric                 /*
632*0b57cec5SDimitry Andric                  * Replace the maximal subpart of ill-formed sequence with
633*0b57cec5SDimitry Andric                  * replacement character.
634*0b57cec5SDimitry Andric                  */
635*0b57cec5SDimitry Andric                 source += findMaximalSubpartOfIllFormedUTF8Sequence(source,
636*0b57cec5SDimitry Andric                                                                     sourceEnd);
637*0b57cec5SDimitry Andric                 *target++ = UNI_REPLACEMENT_CHAR;
638*0b57cec5SDimitry Andric                 continue;
639*0b57cec5SDimitry Andric             }
640*0b57cec5SDimitry Andric         }
641*0b57cec5SDimitry Andric         if (target >= targetEnd) {
642*0b57cec5SDimitry Andric             result = targetExhausted; break;
643*0b57cec5SDimitry Andric         }
644*0b57cec5SDimitry Andric 
645*0b57cec5SDimitry Andric         /* Do this check whether lenient or strict */
646*0b57cec5SDimitry Andric         if (!isLegalUTF8(source, extraBytesToRead+1)) {
647*0b57cec5SDimitry Andric             result = sourceIllegal;
648*0b57cec5SDimitry Andric             if (flags == strictConversion) {
649*0b57cec5SDimitry Andric                 /* Abort conversion. */
650*0b57cec5SDimitry Andric                 break;
651*0b57cec5SDimitry Andric             } else {
652*0b57cec5SDimitry Andric                 /*
653*0b57cec5SDimitry Andric                  * Replace the maximal subpart of ill-formed sequence with
654*0b57cec5SDimitry Andric                  * replacement character.
655*0b57cec5SDimitry Andric                  */
656*0b57cec5SDimitry Andric                 source += findMaximalSubpartOfIllFormedUTF8Sequence(source,
657*0b57cec5SDimitry Andric                                                                     sourceEnd);
658*0b57cec5SDimitry Andric                 *target++ = UNI_REPLACEMENT_CHAR;
659*0b57cec5SDimitry Andric                 continue;
660*0b57cec5SDimitry Andric             }
661*0b57cec5SDimitry Andric         }
662*0b57cec5SDimitry Andric         /*
663*0b57cec5SDimitry Andric          * The cases all fall through. See "Note A" below.
664*0b57cec5SDimitry Andric          */
665*0b57cec5SDimitry Andric         switch (extraBytesToRead) {
666*0b57cec5SDimitry Andric             case 5: ch += *source++; ch <<= 6;
667*0b57cec5SDimitry Andric             case 4: ch += *source++; ch <<= 6;
668*0b57cec5SDimitry Andric             case 3: ch += *source++; ch <<= 6;
669*0b57cec5SDimitry Andric             case 2: ch += *source++; ch <<= 6;
670*0b57cec5SDimitry Andric             case 1: ch += *source++; ch <<= 6;
671*0b57cec5SDimitry Andric             case 0: ch += *source++;
672*0b57cec5SDimitry Andric         }
673*0b57cec5SDimitry Andric         ch -= offsetsFromUTF8[extraBytesToRead];
674*0b57cec5SDimitry Andric 
675*0b57cec5SDimitry Andric         if (ch <= UNI_MAX_LEGAL_UTF32) {
676*0b57cec5SDimitry Andric             /*
677*0b57cec5SDimitry Andric              * UTF-16 surrogate values are illegal in UTF-32, and anything
678*0b57cec5SDimitry Andric              * over Plane 17 (> 0x10FFFF) is illegal.
679*0b57cec5SDimitry Andric              */
680*0b57cec5SDimitry Andric             if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
681*0b57cec5SDimitry Andric                 if (flags == strictConversion) {
682*0b57cec5SDimitry Andric                     source -= (extraBytesToRead+1); /* return to the illegal value itself */
683*0b57cec5SDimitry Andric                     result = sourceIllegal;
684*0b57cec5SDimitry Andric                     break;
685*0b57cec5SDimitry Andric                 } else {
686*0b57cec5SDimitry Andric                     *target++ = UNI_REPLACEMENT_CHAR;
687*0b57cec5SDimitry Andric                 }
688*0b57cec5SDimitry Andric             } else {
689*0b57cec5SDimitry Andric                 *target++ = ch;
690*0b57cec5SDimitry Andric             }
691*0b57cec5SDimitry Andric         } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
692*0b57cec5SDimitry Andric             result = sourceIllegal;
693*0b57cec5SDimitry Andric             *target++ = UNI_REPLACEMENT_CHAR;
694*0b57cec5SDimitry Andric         }
695*0b57cec5SDimitry Andric     }
696*0b57cec5SDimitry Andric     *sourceStart = source;
697*0b57cec5SDimitry Andric     *targetStart = target;
698*0b57cec5SDimitry Andric     return result;
699*0b57cec5SDimitry Andric }
700*0b57cec5SDimitry Andric 
ConvertUTF8toUTF32Partial(const UTF8 ** sourceStart,const UTF8 * sourceEnd,UTF32 ** targetStart,UTF32 * targetEnd,ConversionFlags flags)701*0b57cec5SDimitry Andric ConversionResult ConvertUTF8toUTF32Partial(const UTF8 **sourceStart,
702*0b57cec5SDimitry Andric                                            const UTF8 *sourceEnd,
703*0b57cec5SDimitry Andric                                            UTF32 **targetStart,
704*0b57cec5SDimitry Andric                                            UTF32 *targetEnd,
705*0b57cec5SDimitry Andric                                            ConversionFlags flags) {
706*0b57cec5SDimitry Andric   return ConvertUTF8toUTF32Impl(sourceStart, sourceEnd, targetStart, targetEnd,
707*0b57cec5SDimitry Andric                                 flags, /*InputIsPartial=*/true);
708*0b57cec5SDimitry Andric }
709*0b57cec5SDimitry Andric 
ConvertUTF8toUTF32(const UTF8 ** sourceStart,const UTF8 * sourceEnd,UTF32 ** targetStart,UTF32 * targetEnd,ConversionFlags flags)710*0b57cec5SDimitry Andric ConversionResult ConvertUTF8toUTF32(const UTF8 **sourceStart,
711*0b57cec5SDimitry Andric                                     const UTF8 *sourceEnd, UTF32 **targetStart,
712*0b57cec5SDimitry Andric                                     UTF32 *targetEnd, ConversionFlags flags) {
713*0b57cec5SDimitry Andric   return ConvertUTF8toUTF32Impl(sourceStart, sourceEnd, targetStart, targetEnd,
714*0b57cec5SDimitry Andric                                 flags, /*InputIsPartial=*/false);
715*0b57cec5SDimitry Andric }
716*0b57cec5SDimitry Andric 
717*0b57cec5SDimitry Andric /* ---------------------------------------------------------------------
718*0b57cec5SDimitry Andric 
719*0b57cec5SDimitry Andric     Note A.
720*0b57cec5SDimitry Andric     The fall-through switches in UTF-8 reading code save a
721*0b57cec5SDimitry Andric     temp variable, some decrements & conditionals.  The switches
722*0b57cec5SDimitry Andric     are equivalent to the following loop:
723*0b57cec5SDimitry Andric         {
724*0b57cec5SDimitry Andric             int tmpBytesToRead = extraBytesToRead+1;
725*0b57cec5SDimitry Andric             do {
726*0b57cec5SDimitry Andric                 ch += *source++;
727*0b57cec5SDimitry Andric                 --tmpBytesToRead;
728*0b57cec5SDimitry Andric                 if (tmpBytesToRead) ch <<= 6;
729*0b57cec5SDimitry Andric             } while (tmpBytesToRead > 0);
730*0b57cec5SDimitry Andric         }
731*0b57cec5SDimitry Andric     In UTF-8 writing code, the switches on "bytesToWrite" are
732*0b57cec5SDimitry Andric     similarly unrolled loops.
733*0b57cec5SDimitry Andric 
734*0b57cec5SDimitry Andric    --------------------------------------------------------------------- */
735*0b57cec5SDimitry Andric 
736*0b57cec5SDimitry Andric } // namespace llvm
737*0b57cec5SDimitry Andric 
738*0b57cec5SDimitry Andric ConvertUTF_RESTORE_WARNINGS
739