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