1249259Sdim/*===--- ConvertUTF.c - Universal Character Names conversions ---------------===
2249259Sdim *
3249259Sdim *                     The LLVM Compiler Infrastructure
4249259Sdim *
5249259Sdim * This file is distributed under the University of Illinois Open Source
6249259Sdim * License. See LICENSE.TXT for details.
7249259Sdim *
8249259Sdim *===------------------------------------------------------------------------=*/
9249259Sdim/*
10249259Sdim * Copyright 2001-2004 Unicode, Inc.
11249259Sdim *
12249259Sdim * Disclaimer
13249259Sdim *
14249259Sdim * This source code is provided as is by Unicode, Inc. No claims are
15249259Sdim * made as to fitness for any particular purpose. No warranties of any
16249259Sdim * kind are expressed or implied. The recipient agrees to determine
17249259Sdim * applicability of information provided. If this file has been
18249259Sdim * purchased on magnetic or optical media from Unicode, Inc., the
19249259Sdim * sole remedy for any claim will be exchange of defective media
20249259Sdim * within 90 days of receipt.
21249259Sdim *
22249259Sdim * Limitations on Rights to Redistribute This Code
23249259Sdim *
24249259Sdim * Unicode, Inc. hereby grants the right to freely use the information
25249259Sdim * supplied in this file in the creation of products supporting the
26249259Sdim * Unicode Standard, and to make copies of this file in any form
27249259Sdim * for internal or external distribution as long as this notice
28249259Sdim * remains attached.
29249259Sdim */
30249259Sdim
31249259Sdim/* ---------------------------------------------------------------------
32249259Sdim
33249259Sdim    Conversions between UTF32, UTF-16, and UTF-8. Source code file.
34249259Sdim    Author: Mark E. Davis, 1994.
35249259Sdim    Rev History: Rick McGowan, fixes & updates May 2001.
36249259Sdim    Sept 2001: fixed const & error conditions per
37249259Sdim        mods suggested by S. Parent & A. Lillich.
38249259Sdim    June 2002: Tim Dodd added detection and handling of incomplete
39249259Sdim        source sequences, enhanced error detection, added casts
40249259Sdim        to eliminate compiler warnings.
41249259Sdim    July 2003: slight mods to back out aggressive FFFE detection.
42249259Sdim    Jan 2004: updated switches in from-UTF8 conversions.
43249259Sdim    Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions.
44249259Sdim
45249259Sdim    See the header file "ConvertUTF.h" for complete documentation.
46249259Sdim
47249259Sdim------------------------------------------------------------------------ */
48249259Sdim
49249259Sdim
50249259Sdim#include "llvm/Support/ConvertUTF.h"
51249259Sdim#ifdef CVTUTF_DEBUG
52249259Sdim#include <stdio.h>
53249259Sdim#endif
54249259Sdim
55249259Sdimstatic const int halfShift  = 10; /* used for shifting by 10 bits */
56249259Sdim
57249259Sdimstatic const UTF32 halfBase = 0x0010000UL;
58249259Sdimstatic const UTF32 halfMask = 0x3FFUL;
59249259Sdim
60249259Sdim#define UNI_SUR_HIGH_START  (UTF32)0xD800
61249259Sdim#define UNI_SUR_HIGH_END    (UTF32)0xDBFF
62249259Sdim#define UNI_SUR_LOW_START   (UTF32)0xDC00
63249259Sdim#define UNI_SUR_LOW_END     (UTF32)0xDFFF
64249259Sdim#define false      0
65249259Sdim#define true        1
66249259Sdim
67249259Sdim/* --------------------------------------------------------------------- */
68249259Sdim
69249259Sdim/*
70249259Sdim * Index into the table below with the first byte of a UTF-8 sequence to
71249259Sdim * get the number of trailing bytes that are supposed to follow it.
72249259Sdim * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
73249259Sdim * left as-is for anyone who may want to do such conversion, which was
74249259Sdim * allowed in earlier algorithms.
75249259Sdim */
76249259Sdimstatic const char trailingBytesForUTF8[256] = {
77249259Sdim    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,
78249259Sdim    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,
79249259Sdim    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,
80249259Sdim    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,
81249259Sdim    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,
82249259Sdim    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,
83249259Sdim    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,
84249259Sdim    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
85249259Sdim};
86249259Sdim
87249259Sdim/*
88249259Sdim * Magic values subtracted from a buffer value during UTF8 conversion.
89249259Sdim * This table contains as many values as there might be trailing bytes
90249259Sdim * in a UTF-8 sequence.
91249259Sdim */
92249259Sdimstatic const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
93249259Sdim                     0x03C82080UL, 0xFA082080UL, 0x82082080UL };
94249259Sdim
95249259Sdim/*
96249259Sdim * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
97249259Sdim * into the first byte, depending on how many bytes follow.  There are
98249259Sdim * as many entries in this table as there are UTF-8 sequence types.
99249259Sdim * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
100249259Sdim * for *legal* UTF-8 will be 4 or fewer bytes total.
101249259Sdim */
102249259Sdimstatic const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
103249259Sdim
104249259Sdim/* --------------------------------------------------------------------- */
105249259Sdim
106249259Sdim/* The interface converts a whole buffer to avoid function-call overhead.
107249259Sdim * Constants have been gathered. Loops & conditionals have been removed as
108249259Sdim * much as possible for efficiency, in favor of drop-through switches.
109249259Sdim * (See "Note A" at the bottom of the file for equivalent code.)
110249259Sdim * If your compiler supports it, the "isLegalUTF8" call can be turned
111249259Sdim * into an inline function.
112249259Sdim */
113249259Sdim
114249259Sdim
115249259Sdim/* --------------------------------------------------------------------- */
116249259Sdim
117249259SdimConversionResult ConvertUTF32toUTF16 (
118249259Sdim        const UTF32** sourceStart, const UTF32* sourceEnd,
119249259Sdim        UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
120249259Sdim    ConversionResult result = conversionOK;
121249259Sdim    const UTF32* source = *sourceStart;
122249259Sdim    UTF16* target = *targetStart;
123249259Sdim    while (source < sourceEnd) {
124249259Sdim        UTF32 ch;
125249259Sdim        if (target >= targetEnd) {
126249259Sdim            result = targetExhausted; break;
127249259Sdim        }
128249259Sdim        ch = *source++;
129249259Sdim        if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
130249259Sdim            /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
131249259Sdim            if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
132249259Sdim                if (flags == strictConversion) {
133249259Sdim                    --source; /* return to the illegal value itself */
134249259Sdim                    result = sourceIllegal;
135249259Sdim                    break;
136249259Sdim                } else {
137249259Sdim                    *target++ = UNI_REPLACEMENT_CHAR;
138249259Sdim                }
139249259Sdim            } else {
140249259Sdim                *target++ = (UTF16)ch; /* normal case */
141249259Sdim            }
142249259Sdim        } else if (ch > UNI_MAX_LEGAL_UTF32) {
143249259Sdim            if (flags == strictConversion) {
144249259Sdim                result = sourceIllegal;
145249259Sdim            } else {
146249259Sdim                *target++ = UNI_REPLACEMENT_CHAR;
147249259Sdim            }
148249259Sdim        } else {
149249259Sdim            /* target is a character in range 0xFFFF - 0x10FFFF. */
150249259Sdim            if (target + 1 >= targetEnd) {
151249259Sdim                --source; /* Back up source pointer! */
152249259Sdim                result = targetExhausted; break;
153249259Sdim            }
154249259Sdim            ch -= halfBase;
155249259Sdim            *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
156249259Sdim            *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
157249259Sdim        }
158249259Sdim    }
159249259Sdim    *sourceStart = source;
160249259Sdim    *targetStart = target;
161249259Sdim    return result;
162249259Sdim}
163249259Sdim
164249259Sdim/* --------------------------------------------------------------------- */
165249259Sdim
166249259SdimConversionResult ConvertUTF16toUTF32 (
167249259Sdim        const UTF16** sourceStart, const UTF16* sourceEnd,
168249259Sdim        UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
169249259Sdim    ConversionResult result = conversionOK;
170249259Sdim    const UTF16* source = *sourceStart;
171249259Sdim    UTF32* target = *targetStart;
172249259Sdim    UTF32 ch, ch2;
173249259Sdim    while (source < sourceEnd) {
174249259Sdim        const UTF16* oldSource = source; /*  In case we have to back up because of target overflow. */
175249259Sdim        ch = *source++;
176249259Sdim        /* If we have a surrogate pair, convert to UTF32 first. */
177249259Sdim        if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
178249259Sdim            /* If the 16 bits following the high surrogate are in the source buffer... */
179249259Sdim            if (source < sourceEnd) {
180249259Sdim                ch2 = *source;
181249259Sdim                /* If it's a low surrogate, convert to UTF32. */
182249259Sdim                if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
183249259Sdim                    ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
184249259Sdim                        + (ch2 - UNI_SUR_LOW_START) + halfBase;
185249259Sdim                    ++source;
186249259Sdim                } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
187249259Sdim                    --source; /* return to the illegal value itself */
188249259Sdim                    result = sourceIllegal;
189249259Sdim                    break;
190249259Sdim                }
191249259Sdim            } else { /* We don't have the 16 bits following the high surrogate. */
192249259Sdim                --source; /* return to the high surrogate */
193249259Sdim                result = sourceExhausted;
194249259Sdim                break;
195249259Sdim            }
196249259Sdim        } else if (flags == strictConversion) {
197249259Sdim            /* UTF-16 surrogate values are illegal in UTF-32 */
198249259Sdim            if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
199249259Sdim                --source; /* return to the illegal value itself */
200249259Sdim                result = sourceIllegal;
201249259Sdim                break;
202249259Sdim            }
203249259Sdim        }
204249259Sdim        if (target >= targetEnd) {
205249259Sdim            source = oldSource; /* Back up source pointer! */
206249259Sdim            result = targetExhausted; break;
207249259Sdim        }
208249259Sdim        *target++ = ch;
209249259Sdim    }
210249259Sdim    *sourceStart = source;
211249259Sdim    *targetStart = target;
212249259Sdim#ifdef CVTUTF_DEBUG
213249259Sdimif (result == sourceIllegal) {
214249259Sdim    fprintf(stderr, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch, ch2);
215249259Sdim    fflush(stderr);
216249259Sdim}
217249259Sdim#endif
218249259Sdim    return result;
219249259Sdim}
220249259SdimConversionResult ConvertUTF16toUTF8 (
221249259Sdim        const UTF16** sourceStart, const UTF16* sourceEnd,
222249259Sdim        UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
223249259Sdim    ConversionResult result = conversionOK;
224249259Sdim    const UTF16* source = *sourceStart;
225249259Sdim    UTF8* target = *targetStart;
226249259Sdim    while (source < sourceEnd) {
227249259Sdim        UTF32 ch;
228249259Sdim        unsigned short bytesToWrite = 0;
229249259Sdim        const UTF32 byteMask = 0xBF;
230249259Sdim        const UTF32 byteMark = 0x80;
231249259Sdim        const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
232249259Sdim        ch = *source++;
233249259Sdim        /* If we have a surrogate pair, convert to UTF32 first. */
234249259Sdim        if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
235249259Sdim            /* If the 16 bits following the high surrogate are in the source buffer... */
236249259Sdim            if (source < sourceEnd) {
237249259Sdim                UTF32 ch2 = *source;
238249259Sdim                /* If it's a low surrogate, convert to UTF32. */
239249259Sdim                if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
240249259Sdim                    ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
241249259Sdim                        + (ch2 - UNI_SUR_LOW_START) + halfBase;
242249259Sdim                    ++source;
243249259Sdim                } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
244249259Sdim                    --source; /* return to the illegal value itself */
245249259Sdim                    result = sourceIllegal;
246249259Sdim                    break;
247249259Sdim                }
248249259Sdim            } else { /* We don't have the 16 bits following the high surrogate. */
249249259Sdim                --source; /* return to the high surrogate */
250249259Sdim                result = sourceExhausted;
251249259Sdim                break;
252249259Sdim            }
253249259Sdim        } else if (flags == strictConversion) {
254249259Sdim            /* UTF-16 surrogate values are illegal in UTF-32 */
255249259Sdim            if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
256249259Sdim                --source; /* return to the illegal value itself */
257249259Sdim                result = sourceIllegal;
258249259Sdim                break;
259249259Sdim            }
260249259Sdim        }
261249259Sdim        /* Figure out how many bytes the result will require */
262249259Sdim        if (ch < (UTF32)0x80) {      bytesToWrite = 1;
263249259Sdim        } else if (ch < (UTF32)0x800) {     bytesToWrite = 2;
264249259Sdim        } else if (ch < (UTF32)0x10000) {   bytesToWrite = 3;
265249259Sdim        } else if (ch < (UTF32)0x110000) {  bytesToWrite = 4;
266249259Sdim        } else {                            bytesToWrite = 3;
267249259Sdim                                            ch = UNI_REPLACEMENT_CHAR;
268249259Sdim        }
269249259Sdim
270249259Sdim        target += bytesToWrite;
271249259Sdim        if (target > targetEnd) {
272249259Sdim            source = oldSource; /* Back up source pointer! */
273249259Sdim            target -= bytesToWrite; result = targetExhausted; break;
274249259Sdim        }
275249259Sdim        switch (bytesToWrite) { /* note: everything falls through. */
276249259Sdim            case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
277249259Sdim            case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
278249259Sdim            case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
279249259Sdim            case 1: *--target =  (UTF8)(ch | firstByteMark[bytesToWrite]);
280249259Sdim        }
281249259Sdim        target += bytesToWrite;
282249259Sdim    }
283249259Sdim    *sourceStart = source;
284249259Sdim    *targetStart = target;
285249259Sdim    return result;
286249259Sdim}
287249259Sdim
288249259Sdim/* --------------------------------------------------------------------- */
289249259Sdim
290249259SdimConversionResult ConvertUTF32toUTF8 (
291249259Sdim        const UTF32** sourceStart, const UTF32* sourceEnd,
292249259Sdim        UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
293249259Sdim    ConversionResult result = conversionOK;
294249259Sdim    const UTF32* source = *sourceStart;
295249259Sdim    UTF8* target = *targetStart;
296249259Sdim    while (source < sourceEnd) {
297249259Sdim        UTF32 ch;
298249259Sdim        unsigned short bytesToWrite = 0;
299249259Sdim        const UTF32 byteMask = 0xBF;
300249259Sdim        const UTF32 byteMark = 0x80;
301249259Sdim        ch = *source++;
302249259Sdim        if (flags == strictConversion ) {
303249259Sdim            /* UTF-16 surrogate values are illegal in UTF-32 */
304249259Sdim            if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
305249259Sdim                --source; /* return to the illegal value itself */
306249259Sdim                result = sourceIllegal;
307249259Sdim                break;
308249259Sdim            }
309249259Sdim        }
310249259Sdim        /*
311249259Sdim         * Figure out how many bytes the result will require. Turn any
312249259Sdim         * illegally large UTF32 things (> Plane 17) into replacement chars.
313249259Sdim         */
314249259Sdim        if (ch < (UTF32)0x80) {      bytesToWrite = 1;
315249259Sdim        } else if (ch < (UTF32)0x800) {     bytesToWrite = 2;
316249259Sdim        } else if (ch < (UTF32)0x10000) {   bytesToWrite = 3;
317249259Sdim        } else if (ch <= UNI_MAX_LEGAL_UTF32) {  bytesToWrite = 4;
318249259Sdim        } else {                            bytesToWrite = 3;
319249259Sdim                                            ch = UNI_REPLACEMENT_CHAR;
320249259Sdim                                            result = sourceIllegal;
321249259Sdim        }
322249259Sdim
323249259Sdim        target += bytesToWrite;
324249259Sdim        if (target > targetEnd) {
325249259Sdim            --source; /* Back up source pointer! */
326249259Sdim            target -= bytesToWrite; result = targetExhausted; break;
327249259Sdim        }
328249259Sdim        switch (bytesToWrite) { /* note: everything falls through. */
329249259Sdim            case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
330249259Sdim            case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
331249259Sdim            case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
332249259Sdim            case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]);
333249259Sdim        }
334249259Sdim        target += bytesToWrite;
335249259Sdim    }
336249259Sdim    *sourceStart = source;
337249259Sdim    *targetStart = target;
338249259Sdim    return result;
339249259Sdim}
340249259Sdim
341249259Sdim/* --------------------------------------------------------------------- */
342249259Sdim
343249259Sdim/*
344249259Sdim * Utility routine to tell whether a sequence of bytes is legal UTF-8.
345249259Sdim * This must be called with the length pre-determined by the first byte.
346249259Sdim * If not calling this from ConvertUTF8to*, then the length can be set by:
347249259Sdim *  length = trailingBytesForUTF8[*source]+1;
348249259Sdim * and the sequence is illegal right away if there aren't that many bytes
349249259Sdim * available.
350249259Sdim * If presented with a length > 4, this returns false.  The Unicode
351249259Sdim * definition of UTF-8 goes up to 4-byte sequences.
352249259Sdim */
353249259Sdim
354249259Sdimstatic Boolean isLegalUTF8(const UTF8 *source, int length) {
355249259Sdim    UTF8 a;
356249259Sdim    const UTF8 *srcptr = source+length;
357249259Sdim    switch (length) {
358249259Sdim    default: return false;
359249259Sdim        /* Everything else falls through when "true"... */
360249259Sdim    case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
361249259Sdim    case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
362249259Sdim    case 2: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
363249259Sdim
364249259Sdim        switch (*source) {
365249259Sdim            /* no fall-through in this inner switch */
366249259Sdim            case 0xE0: if (a < 0xA0) return false; break;
367249259Sdim            case 0xED: if (a > 0x9F) return false; break;
368249259Sdim            case 0xF0: if (a < 0x90) return false; break;
369249259Sdim            case 0xF4: if (a > 0x8F) return false; break;
370249259Sdim            default:   if (a < 0x80) return false;
371249259Sdim        }
372249259Sdim
373249259Sdim    case 1: if (*source >= 0x80 && *source < 0xC2) return false;
374249259Sdim    }
375249259Sdim    if (*source > 0xF4) return false;
376249259Sdim    return true;
377249259Sdim}
378249259Sdim
379249259Sdim/* --------------------------------------------------------------------- */
380249259Sdim
381249259Sdim/*
382249259Sdim * Exported function to return whether a UTF-8 sequence is legal or not.
383249259Sdim * This is not used here; it's just exported.
384249259Sdim */
385249259SdimBoolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) {
386249259Sdim    int length = trailingBytesForUTF8[*source]+1;
387249259Sdim    if (length > sourceEnd - source) {
388249259Sdim        return false;
389249259Sdim    }
390249259Sdim    return isLegalUTF8(source, length);
391249259Sdim}
392249259Sdim
393249259Sdim/* --------------------------------------------------------------------- */
394249259Sdim
395249259Sdim/*
396249259Sdim * Exported function to return the total number of bytes in a codepoint
397249259Sdim * represented in UTF-8, given the value of the first byte.
398249259Sdim */
399249259Sdimunsigned getNumBytesForUTF8(UTF8 first) {
400249259Sdim  return trailingBytesForUTF8[first] + 1;
401249259Sdim}
402249259Sdim
403249259Sdim/* --------------------------------------------------------------------- */
404249259Sdim
405249259Sdim/*
406249259Sdim * Exported function to return whether a UTF-8 string is legal or not.
407249259Sdim * This is not used here; it's just exported.
408249259Sdim */
409249259SdimBoolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd) {
410249259Sdim    while (*source != sourceEnd) {
411249259Sdim        int length = trailingBytesForUTF8[**source] + 1;
412249259Sdim        if (length > sourceEnd - *source || !isLegalUTF8(*source, length))
413249259Sdim            return false;
414249259Sdim        *source += length;
415249259Sdim    }
416249259Sdim    return true;
417249259Sdim}
418249259Sdim
419249259Sdim/* --------------------------------------------------------------------- */
420249259Sdim
421249259SdimConversionResult ConvertUTF8toUTF16 (
422249259Sdim        const UTF8** sourceStart, const UTF8* sourceEnd,
423249259Sdim        UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
424249259Sdim    ConversionResult result = conversionOK;
425249259Sdim    const UTF8* source = *sourceStart;
426249259Sdim    UTF16* target = *targetStart;
427249259Sdim    while (source < sourceEnd) {
428249259Sdim        UTF32 ch = 0;
429249259Sdim        unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
430249259Sdim        if (extraBytesToRead >= sourceEnd - source) {
431249259Sdim            result = sourceExhausted; break;
432249259Sdim        }
433249259Sdim        /* Do this check whether lenient or strict */
434249259Sdim        if (!isLegalUTF8(source, extraBytesToRead+1)) {
435249259Sdim            result = sourceIllegal;
436249259Sdim            break;
437249259Sdim        }
438249259Sdim        /*
439249259Sdim         * The cases all fall through. See "Note A" below.
440249259Sdim         */
441249259Sdim        switch (extraBytesToRead) {
442249259Sdim            case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
443249259Sdim            case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
444249259Sdim            case 3: ch += *source++; ch <<= 6;
445249259Sdim            case 2: ch += *source++; ch <<= 6;
446249259Sdim            case 1: ch += *source++; ch <<= 6;
447249259Sdim            case 0: ch += *source++;
448249259Sdim        }
449249259Sdim        ch -= offsetsFromUTF8[extraBytesToRead];
450249259Sdim
451249259Sdim        if (target >= targetEnd) {
452249259Sdim            source -= (extraBytesToRead+1); /* Back up source pointer! */
453249259Sdim            result = targetExhausted; break;
454249259Sdim        }
455249259Sdim        if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
456249259Sdim            /* UTF-16 surrogate values are illegal in UTF-32 */
457249259Sdim            if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
458249259Sdim                if (flags == strictConversion) {
459249259Sdim                    source -= (extraBytesToRead+1); /* return to the illegal value itself */
460249259Sdim                    result = sourceIllegal;
461249259Sdim                    break;
462249259Sdim                } else {
463249259Sdim                    *target++ = UNI_REPLACEMENT_CHAR;
464249259Sdim                }
465249259Sdim            } else {
466249259Sdim                *target++ = (UTF16)ch; /* normal case */
467249259Sdim            }
468249259Sdim        } else if (ch > UNI_MAX_UTF16) {
469249259Sdim            if (flags == strictConversion) {
470249259Sdim                result = sourceIllegal;
471249259Sdim                source -= (extraBytesToRead+1); /* return to the start */
472249259Sdim                break; /* Bail out; shouldn't continue */
473249259Sdim            } else {
474249259Sdim                *target++ = UNI_REPLACEMENT_CHAR;
475249259Sdim            }
476249259Sdim        } else {
477249259Sdim            /* target is a character in range 0xFFFF - 0x10FFFF. */
478249259Sdim            if (target + 1 >= targetEnd) {
479249259Sdim                source -= (extraBytesToRead+1); /* Back up source pointer! */
480249259Sdim                result = targetExhausted; break;
481249259Sdim            }
482249259Sdim            ch -= halfBase;
483249259Sdim            *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
484249259Sdim            *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
485249259Sdim        }
486249259Sdim    }
487249259Sdim    *sourceStart = source;
488249259Sdim    *targetStart = target;
489249259Sdim    return result;
490249259Sdim}
491249259Sdim
492249259Sdim/* --------------------------------------------------------------------- */
493249259Sdim
494249259SdimConversionResult ConvertUTF8toUTF32 (
495249259Sdim        const UTF8** sourceStart, const UTF8* sourceEnd,
496249259Sdim        UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
497249259Sdim    ConversionResult result = conversionOK;
498249259Sdim    const UTF8* source = *sourceStart;
499249259Sdim    UTF32* target = *targetStart;
500249259Sdim    while (source < sourceEnd) {
501249259Sdim        UTF32 ch = 0;
502249259Sdim        unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
503249259Sdim        if (extraBytesToRead >= sourceEnd - source) {
504249259Sdim            result = sourceExhausted; break;
505249259Sdim        }
506249259Sdim        /* Do this check whether lenient or strict */
507249259Sdim        if (!isLegalUTF8(source, extraBytesToRead+1)) {
508249259Sdim            result = sourceIllegal;
509249259Sdim            break;
510249259Sdim        }
511249259Sdim        /*
512249259Sdim         * The cases all fall through. See "Note A" below.
513249259Sdim         */
514249259Sdim        switch (extraBytesToRead) {
515249259Sdim            case 5: ch += *source++; ch <<= 6;
516249259Sdim            case 4: ch += *source++; ch <<= 6;
517249259Sdim            case 3: ch += *source++; ch <<= 6;
518249259Sdim            case 2: ch += *source++; ch <<= 6;
519249259Sdim            case 1: ch += *source++; ch <<= 6;
520249259Sdim            case 0: ch += *source++;
521249259Sdim        }
522249259Sdim        ch -= offsetsFromUTF8[extraBytesToRead];
523249259Sdim
524249259Sdim        if (target >= targetEnd) {
525249259Sdim            source -= (extraBytesToRead+1); /* Back up the source pointer! */
526249259Sdim            result = targetExhausted; break;
527249259Sdim        }
528249259Sdim        if (ch <= UNI_MAX_LEGAL_UTF32) {
529249259Sdim            /*
530249259Sdim             * UTF-16 surrogate values are illegal in UTF-32, and anything
531249259Sdim             * over Plane 17 (> 0x10FFFF) is illegal.
532249259Sdim             */
533249259Sdim            if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
534249259Sdim                if (flags == strictConversion) {
535249259Sdim                    source -= (extraBytesToRead+1); /* return to the illegal value itself */
536249259Sdim                    result = sourceIllegal;
537249259Sdim                    break;
538249259Sdim                } else {
539249259Sdim                    *target++ = UNI_REPLACEMENT_CHAR;
540249259Sdim                }
541249259Sdim            } else {
542249259Sdim                *target++ = ch;
543249259Sdim            }
544249259Sdim        } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
545249259Sdim            result = sourceIllegal;
546249259Sdim            *target++ = UNI_REPLACEMENT_CHAR;
547249259Sdim        }
548249259Sdim    }
549249259Sdim    *sourceStart = source;
550249259Sdim    *targetStart = target;
551249259Sdim    return result;
552249259Sdim}
553249259Sdim
554249259Sdim/* ---------------------------------------------------------------------
555249259Sdim
556249259Sdim    Note A.
557249259Sdim    The fall-through switches in UTF-8 reading code save a
558249259Sdim    temp variable, some decrements & conditionals.  The switches
559249259Sdim    are equivalent to the following loop:
560249259Sdim        {
561249259Sdim            int tmpBytesToRead = extraBytesToRead+1;
562249259Sdim            do {
563249259Sdim                ch += *source++;
564249259Sdim                --tmpBytesToRead;
565249259Sdim                if (tmpBytesToRead) ch <<= 6;
566249259Sdim            } while (tmpBytesToRead > 0);
567249259Sdim        }
568249259Sdim    In UTF-8 writing code, the switches on "bytesToWrite" are
569249259Sdim    similarly unrolled loops.
570249259Sdim
571249259Sdim   --------------------------------------------------------------------- */
572