1 /*===--- ConvertUTF.c - Universal Character Names conversions ---------------===
3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 *===------------------------------------------------------------------------=*/
9 * Copyright © 1991-2015 Unicode, Inc. All rights reserved.
10 * Distributed under the Terms of Use in
11 * http://www.unicode.org/copyright.html.
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of the Unicode data files and any associated documentation
15 * (the "Data Files") or Unicode software and any associated documentation
16 * (the "Software") to deal in the Data Files or Software
17 * without restriction, including without limitation the rights to use,
18 * copy, modify, merge, publish, distribute, and/or sell copies of
19 * the Data Files or Software, and to permit persons to whom the Data Files
20 * or Software are furnished to do so, provided that
21 * (a) this copyright and permission notice appear with all copies
22 * of the Data Files or Software,
23 * (b) this copyright and permission notice appear in associated
25 * (c) there is clear notice in each modified Data File or in the Software
26 * as well as in the documentation associated with the Data File(s) or
27 * Software that the data or software has been modified.
29 * THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF
30 * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
31 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 * NONINFRINGEMENT OF THIRD PARTY RIGHTS.
33 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
34 * NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
35 * DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
36 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
37 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
38 * PERFORMANCE OF THE DATA FILES OR SOFTWARE.
40 * Except as contained in this notice, the name of a copyright holder
41 * shall not be used in advertising or otherwise to promote the sale,
42 * use or other dealings in these Data Files or Software without prior
43 * written authorization of the copyright holder.
46 /* ---------------------------------------------------------------------
48 Conversions between UTF32, UTF-16, and UTF-8. Source code file.
49 Author: Mark E. Davis, 1994.
50 Rev History: Rick McGowan, fixes & updates May 2001.
51 Sept 2001: fixed const & error conditions per
52 mods suggested by S. Parent & A. Lillich.
53 June 2002: Tim Dodd added detection and handling of incomplete
54 source sequences, enhanced error detection, added casts
55 to eliminate compiler warnings.
56 July 2003: slight mods to back out aggressive FFFE detection.
57 Jan 2004: updated switches in from-UTF8 conversions.
58 Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions.
60 See the header file "ConvertUTF.h" for complete documentation.
62 ------------------------------------------------------------------------ */
64 #include "llvm/Support/ConvertUTF.h"
71 * This code extensively uses fall-through switches.
72 * Keep the compiler from warning about that.
74 #if defined(__clang__) && defined(__has_warning)
75 # if __has_warning("-Wimplicit-fallthrough")
76 # define ConvertUTF_DISABLE_WARNINGS \
77 _Pragma("clang diagnostic push") \
78 _Pragma("clang diagnostic ignored \"-Wimplicit-fallthrough\"")
79 # define ConvertUTF_RESTORE_WARNINGS \
80 _Pragma("clang diagnostic pop")
82 #elif defined(__GNUC__) && __GNUC__ > 6
83 # define ConvertUTF_DISABLE_WARNINGS \
84 _Pragma("GCC diagnostic push") \
85 _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
86 # define ConvertUTF_RESTORE_WARNINGS \
87 _Pragma("GCC diagnostic pop")
89 #ifndef ConvertUTF_DISABLE_WARNINGS
90 # define ConvertUTF_DISABLE_WARNINGS
92 #ifndef ConvertUTF_RESTORE_WARNINGS
93 # define ConvertUTF_RESTORE_WARNINGS
96 ConvertUTF_DISABLE_WARNINGS
100 static const int halfShift
= 10; /* used for shifting by 10 bits */
102 static const UTF32 halfBase
= 0x0010000UL
;
103 static const UTF32 halfMask
= 0x3FFUL
;
105 #define UNI_SUR_HIGH_START (UTF32)0xD800
106 #define UNI_SUR_HIGH_END (UTF32)0xDBFF
107 #define UNI_SUR_LOW_START (UTF32)0xDC00
108 #define UNI_SUR_LOW_END (UTF32)0xDFFF
110 /* --------------------------------------------------------------------- */
113 * Index into the table below with the first byte of a UTF-8 sequence to
114 * get the number of trailing bytes that are supposed to follow it.
115 * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
116 * left as-is for anyone who may want to do such conversion, which was
117 * allowed in earlier algorithms.
119 static const char trailingBytesForUTF8
[256] = {
120 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 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 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 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 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 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 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 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
131 * Magic values subtracted from a buffer value during UTF8 conversion.
132 * This table contains as many values as there might be trailing bytes
133 * in a UTF-8 sequence.
135 static const UTF32 offsetsFromUTF8
[6] = { 0x00000000UL
, 0x00003080UL
, 0x000E2080UL
,
136 0x03C82080UL
, 0xFA082080UL
, 0x82082080UL
};
139 * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
140 * into the first byte, depending on how many bytes follow. There are
141 * as many entries in this table as there are UTF-8 sequence types.
142 * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
143 * for *legal* UTF-8 will be 4 or fewer bytes total.
145 static const UTF8 firstByteMark
[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
147 /* --------------------------------------------------------------------- */
149 /* The interface converts a whole buffer to avoid function-call overhead.
150 * Constants have been gathered. Loops & conditionals have been removed as
151 * much as possible for efficiency, in favor of drop-through switches.
152 * (See "Note A" at the bottom of the file for equivalent code.)
153 * If your compiler supports it, the "isLegalUTF8" call can be turned
154 * into an inline function.
158 /* --------------------------------------------------------------------- */
160 ConversionResult
ConvertUTF32toUTF16 (
161 const UTF32
** sourceStart
, const UTF32
* sourceEnd
,
162 UTF16
** targetStart
, UTF16
* targetEnd
, ConversionFlags flags
) {
163 ConversionResult result
= conversionOK
;
164 const UTF32
* source
= *sourceStart
;
165 UTF16
* target
= *targetStart
;
166 while (source
< sourceEnd
) {
168 if (target
>= targetEnd
) {
169 result
= targetExhausted
; break;
172 if (ch
<= UNI_MAX_BMP
) { /* Target is a character <= 0xFFFF */
173 /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
174 if (ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
) {
175 if (flags
== strictConversion
) {
176 --source
; /* return to the illegal value itself */
177 result
= sourceIllegal
;
180 *target
++ = UNI_REPLACEMENT_CHAR
;
183 *target
++ = (UTF16
)ch
; /* normal case */
185 } else if (ch
> UNI_MAX_LEGAL_UTF32
) {
186 if (flags
== strictConversion
) {
187 result
= sourceIllegal
;
189 *target
++ = UNI_REPLACEMENT_CHAR
;
192 /* target is a character in range 0xFFFF - 0x10FFFF. */
193 if (target
+ 1 >= targetEnd
) {
194 --source
; /* Back up source pointer! */
195 result
= targetExhausted
; break;
198 *target
++ = (UTF16
)((ch
>> halfShift
) + UNI_SUR_HIGH_START
);
199 *target
++ = (UTF16
)((ch
& halfMask
) + UNI_SUR_LOW_START
);
202 *sourceStart
= source
;
203 *targetStart
= target
;
207 /* --------------------------------------------------------------------- */
209 ConversionResult
ConvertUTF16toUTF32 (
210 const UTF16
** sourceStart
, const UTF16
* sourceEnd
,
211 UTF32
** targetStart
, UTF32
* targetEnd
, ConversionFlags flags
) {
212 ConversionResult result
= conversionOK
;
213 const UTF16
* source
= *sourceStart
;
214 UTF32
* target
= *targetStart
;
216 while (source
< sourceEnd
) {
217 const UTF16
* oldSource
= source
; /* In case we have to back up because of target overflow. */
219 /* If we have a surrogate pair, convert to UTF32 first. */
220 if (ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_HIGH_END
) {
221 /* If the 16 bits following the high surrogate are in the source buffer... */
222 if (source
< sourceEnd
) {
224 /* If it's a low surrogate, convert to UTF32. */
225 if (ch2
>= UNI_SUR_LOW_START
&& ch2
<= UNI_SUR_LOW_END
) {
226 ch
= ((ch
- UNI_SUR_HIGH_START
) << halfShift
)
227 + (ch2
- UNI_SUR_LOW_START
) + halfBase
;
229 } else if (flags
== strictConversion
) { /* it's an unpaired high surrogate */
230 --source
; /* return to the illegal value itself */
231 result
= sourceIllegal
;
234 } else { /* We don't have the 16 bits following the high surrogate. */
235 --source
; /* return to the high surrogate */
236 result
= sourceExhausted
;
239 } else if (flags
== strictConversion
) {
240 /* UTF-16 surrogate values are illegal in UTF-32 */
241 if (ch
>= UNI_SUR_LOW_START
&& ch
<= UNI_SUR_LOW_END
) {
242 --source
; /* return to the illegal value itself */
243 result
= sourceIllegal
;
247 if (target
>= targetEnd
) {
248 source
= oldSource
; /* Back up source pointer! */
249 result
= targetExhausted
; break;
253 *sourceStart
= source
;
254 *targetStart
= target
;
256 if (result
== sourceIllegal
) {
257 fprintf(stderr
, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch
, ch2
);
263 ConversionResult
ConvertUTF16toUTF8 (
264 const UTF16
** sourceStart
, const UTF16
* sourceEnd
,
265 UTF8
** targetStart
, UTF8
* targetEnd
, ConversionFlags flags
) {
266 ConversionResult result
= conversionOK
;
267 const UTF16
* source
= *sourceStart
;
268 UTF8
* target
= *targetStart
;
269 while (source
< sourceEnd
) {
271 unsigned short bytesToWrite
= 0;
272 const UTF32 byteMask
= 0xBF;
273 const UTF32 byteMark
= 0x80;
274 const UTF16
* oldSource
= source
; /* In case we have to back up because of target overflow. */
276 /* If we have a surrogate pair, convert to UTF32 first. */
277 if (ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_HIGH_END
) {
278 /* If the 16 bits following the high surrogate are in the source buffer... */
279 if (source
< sourceEnd
) {
281 /* If it's a low surrogate, convert to UTF32. */
282 if (ch2
>= UNI_SUR_LOW_START
&& ch2
<= UNI_SUR_LOW_END
) {
283 ch
= ((ch
- UNI_SUR_HIGH_START
) << halfShift
)
284 + (ch2
- UNI_SUR_LOW_START
) + halfBase
;
286 } else if (flags
== strictConversion
) { /* it's an unpaired high surrogate */
287 --source
; /* return to the illegal value itself */
288 result
= sourceIllegal
;
291 } else { /* We don't have the 16 bits following the high surrogate. */
292 --source
; /* return to the high surrogate */
293 result
= sourceExhausted
;
296 } else if (flags
== strictConversion
) {
297 /* UTF-16 surrogate values are illegal in UTF-32 */
298 if (ch
>= UNI_SUR_LOW_START
&& ch
<= UNI_SUR_LOW_END
) {
299 --source
; /* return to the illegal value itself */
300 result
= sourceIllegal
;
304 /* Figure out how many bytes the result will require */
305 if (ch
< (UTF32
)0x80) { bytesToWrite
= 1;
306 } else if (ch
< (UTF32
)0x800) { bytesToWrite
= 2;
307 } else if (ch
< (UTF32
)0x10000) { bytesToWrite
= 3;
308 } else if (ch
< (UTF32
)0x110000) { bytesToWrite
= 4;
309 } else { bytesToWrite
= 3;
310 ch
= UNI_REPLACEMENT_CHAR
;
313 target
+= bytesToWrite
;
314 if (target
> targetEnd
) {
315 source
= oldSource
; /* Back up source pointer! */
316 target
-= bytesToWrite
; result
= targetExhausted
; break;
318 switch (bytesToWrite
) { /* note: everything falls through. */
319 case 4: *--target
= (UTF8
)((ch
| byteMark
) & byteMask
); ch
>>= 6;
320 case 3: *--target
= (UTF8
)((ch
| byteMark
) & byteMask
); ch
>>= 6;
321 case 2: *--target
= (UTF8
)((ch
| byteMark
) & byteMask
); ch
>>= 6;
322 case 1: *--target
= (UTF8
)(ch
| firstByteMark
[bytesToWrite
]);
324 target
+= bytesToWrite
;
326 *sourceStart
= source
;
327 *targetStart
= target
;
331 /* --------------------------------------------------------------------- */
333 ConversionResult
ConvertUTF32toUTF8 (
334 const UTF32
** sourceStart
, const UTF32
* sourceEnd
,
335 UTF8
** targetStart
, UTF8
* targetEnd
, ConversionFlags flags
) {
336 ConversionResult result
= conversionOK
;
337 const UTF32
* source
= *sourceStart
;
338 UTF8
* target
= *targetStart
;
339 while (source
< sourceEnd
) {
341 unsigned short bytesToWrite
= 0;
342 const UTF32 byteMask
= 0xBF;
343 const UTF32 byteMark
= 0x80;
345 if (flags
== strictConversion
) {
346 /* UTF-16 surrogate values are illegal in UTF-32 */
347 if (ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
) {
348 --source
; /* return to the illegal value itself */
349 result
= sourceIllegal
;
354 * Figure out how many bytes the result will require. Turn any
355 * illegally large UTF32 things (> Plane 17) into replacement chars.
357 if (ch
< (UTF32
)0x80) { bytesToWrite
= 1;
358 } else if (ch
< (UTF32
)0x800) { bytesToWrite
= 2;
359 } else if (ch
< (UTF32
)0x10000) { bytesToWrite
= 3;
360 } else if (ch
<= UNI_MAX_LEGAL_UTF32
) { bytesToWrite
= 4;
361 } else { bytesToWrite
= 3;
362 ch
= UNI_REPLACEMENT_CHAR
;
363 result
= sourceIllegal
;
366 target
+= bytesToWrite
;
367 if (target
> targetEnd
) {
368 --source
; /* Back up source pointer! */
369 target
-= bytesToWrite
; result
= targetExhausted
; break;
371 switch (bytesToWrite
) { /* note: everything falls through. */
372 case 4: *--target
= (UTF8
)((ch
| byteMark
) & byteMask
); ch
>>= 6;
373 case 3: *--target
= (UTF8
)((ch
| byteMark
) & byteMask
); ch
>>= 6;
374 case 2: *--target
= (UTF8
)((ch
| byteMark
) & byteMask
); ch
>>= 6;
375 case 1: *--target
= (UTF8
) (ch
| firstByteMark
[bytesToWrite
]);
377 target
+= bytesToWrite
;
379 *sourceStart
= source
;
380 *targetStart
= target
;
384 /* --------------------------------------------------------------------- */
387 * Utility routine to tell whether a sequence of bytes is legal UTF-8.
388 * This must be called with the length pre-determined by the first byte.
389 * If not calling this from ConvertUTF8to*, then the length can be set by:
390 * length = trailingBytesForUTF8[*source]+1;
391 * and the sequence is illegal right away if there aren't that many bytes
393 * If presented with a length > 4, this returns false. The Unicode
394 * definition of UTF-8 goes up to 4-byte sequences.
397 static Boolean
isLegalUTF8(const UTF8
*source
, int length
) {
399 const UTF8
*srcptr
= source
+length
;
401 default: return false;
402 /* Everything else falls through when "true"... */
403 case 4: if ((a
= (*--srcptr
)) < 0x80 || a
> 0xBF) return false;
404 case 3: if ((a
= (*--srcptr
)) < 0x80 || a
> 0xBF) return false;
405 case 2: if ((a
= (*--srcptr
)) < 0x80 || a
> 0xBF) return false;
408 /* no fall-through in this inner switch */
409 case 0xE0: if (a
< 0xA0) return false; break;
410 case 0xED: if (a
> 0x9F) return false; break;
411 case 0xF0: if (a
< 0x90) return false; break;
412 case 0xF4: if (a
> 0x8F) return false; break;
413 default: if (a
< 0x80) return false;
416 case 1: if (*source
>= 0x80 && *source
< 0xC2) return false;
418 if (*source
> 0xF4) return false;
422 /* --------------------------------------------------------------------- */
425 * Exported function to return whether a UTF-8 sequence is legal or not.
426 * This is not used here; it's just exported.
428 Boolean
isLegalUTF8Sequence(const UTF8
*source
, const UTF8
*sourceEnd
) {
429 int length
= trailingBytesForUTF8
[*source
]+1;
430 if (length
> sourceEnd
- source
) {
433 return isLegalUTF8(source
, length
);
437 * Exported function to return the size of the first utf-8 code unit sequence,
438 * Or 0 if the sequence is not valid;
440 unsigned getUTF8SequenceSize(const UTF8
*source
, const UTF8
*sourceEnd
) {
441 int length
= trailingBytesForUTF8
[*source
] + 1;
442 return (length
<= sourceEnd
- source
&& isLegalUTF8(source
, length
)) ? length
446 /* --------------------------------------------------------------------- */
449 findMaximalSubpartOfIllFormedUTF8Sequence(const UTF8
*source
,
450 const UTF8
*sourceEnd
) {
453 assert(!isLegalUTF8Sequence(source
, sourceEnd
));
456 * Unicode 6.3.0, D93b:
458 * Maximal subpart of an ill-formed subsequence: The longest code unit
459 * subsequence starting at an unconvertible offset that is either:
460 * a. the initial subsequence of a well-formed code unit sequence, or
461 * b. a subsequence of length one.
464 if (source
== sourceEnd
)
468 * Perform case analysis. See Unicode 6.3.0, Table 3-7. Well-Formed UTF-8
474 if (b1
>= 0xC2 && b1
<= 0xDF) {
476 * First byte is valid, but we know that this code unit sequence is
477 * invalid, so the maximal subpart has to end after the first byte.
482 if (source
== sourceEnd
)
489 return (b2
>= 0xA0 && b2
<= 0xBF) ? 2 : 1;
491 if (b1
>= 0xE1 && b1
<= 0xEC) {
492 return (b2
>= 0x80 && b2
<= 0xBF) ? 2 : 1;
495 return (b2
>= 0x80 && b2
<= 0x9F) ? 2 : 1;
497 if (b1
>= 0xEE && b1
<= 0xEF) {
498 return (b2
>= 0x80 && b2
<= 0xBF) ? 2 : 1;
501 if (b2
>= 0x90 && b2
<= 0xBF) {
502 if (source
== sourceEnd
)
506 return (b3
>= 0x80 && b3
<= 0xBF) ? 3 : 2;
510 if (b1
>= 0xF1 && b1
<= 0xF3) {
511 if (b2
>= 0x80 && b2
<= 0xBF) {
512 if (source
== sourceEnd
)
516 return (b3
>= 0x80 && b3
<= 0xBF) ? 3 : 2;
521 if (b2
>= 0x80 && b2
<= 0x8F) {
522 if (source
== sourceEnd
)
526 return (b3
>= 0x80 && b3
<= 0xBF) ? 3 : 2;
531 assert((b1
>= 0x80 && b1
<= 0xC1) || b1
>= 0xF5);
533 * There are no valid sequences that start with these bytes. Maximal subpart
534 * is defined to have length 1 in these cases.
539 /* --------------------------------------------------------------------- */
542 * Exported function to return the total number of bytes in a codepoint
543 * represented in UTF-8, given the value of the first byte.
545 unsigned getNumBytesForUTF8(UTF8 first
) {
546 return trailingBytesForUTF8
[first
] + 1;
549 /* --------------------------------------------------------------------- */
552 * Exported function to return whether a UTF-8 string is legal or not.
553 * This is not used here; it's just exported.
555 Boolean
isLegalUTF8String(const UTF8
**source
, const UTF8
*sourceEnd
) {
556 while (*source
!= sourceEnd
) {
557 int length
= trailingBytesForUTF8
[**source
] + 1;
558 if (length
> sourceEnd
- *source
|| !isLegalUTF8(*source
, length
))
565 /* --------------------------------------------------------------------- */
567 ConversionResult
ConvertUTF8toUTF16 (
568 const UTF8
** sourceStart
, const UTF8
* sourceEnd
,
569 UTF16
** targetStart
, UTF16
* targetEnd
, ConversionFlags flags
) {
570 ConversionResult result
= conversionOK
;
571 const UTF8
* source
= *sourceStart
;
572 UTF16
* target
= *targetStart
;
573 while (source
< sourceEnd
) {
575 unsigned short extraBytesToRead
= trailingBytesForUTF8
[*source
];
576 if (extraBytesToRead
>= sourceEnd
- source
) {
577 result
= sourceExhausted
; break;
579 /* Do this check whether lenient or strict */
580 if (!isLegalUTF8(source
, extraBytesToRead
+1)) {
581 result
= sourceIllegal
;
585 * The cases all fall through. See "Note A" below.
587 switch (extraBytesToRead
) {
588 case 5: ch
+= *source
++; ch
<<= 6; /* remember, illegal UTF-8 */
589 case 4: ch
+= *source
++; ch
<<= 6; /* remember, illegal UTF-8 */
590 case 3: ch
+= *source
++; ch
<<= 6;
591 case 2: ch
+= *source
++; ch
<<= 6;
592 case 1: ch
+= *source
++; ch
<<= 6;
593 case 0: ch
+= *source
++;
595 ch
-= offsetsFromUTF8
[extraBytesToRead
];
597 if (target
>= targetEnd
) {
598 source
-= (extraBytesToRead
+1); /* Back up source pointer! */
599 result
= targetExhausted
; break;
601 if (ch
<= UNI_MAX_BMP
) { /* Target is a character <= 0xFFFF */
602 /* UTF-16 surrogate values are illegal in UTF-32 */
603 if (ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
) {
604 if (flags
== strictConversion
) {
605 source
-= (extraBytesToRead
+1); /* return to the illegal value itself */
606 result
= sourceIllegal
;
609 *target
++ = UNI_REPLACEMENT_CHAR
;
612 *target
++ = (UTF16
)ch
; /* normal case */
614 } else if (ch
> UNI_MAX_UTF16
) {
615 if (flags
== strictConversion
) {
616 result
= sourceIllegal
;
617 source
-= (extraBytesToRead
+1); /* return to the start */
618 break; /* Bail out; shouldn't continue */
620 *target
++ = UNI_REPLACEMENT_CHAR
;
623 /* target is a character in range 0xFFFF - 0x10FFFF. */
624 if (target
+ 1 >= targetEnd
) {
625 source
-= (extraBytesToRead
+1); /* Back up source pointer! */
626 result
= targetExhausted
; break;
629 *target
++ = (UTF16
)((ch
>> halfShift
) + UNI_SUR_HIGH_START
);
630 *target
++ = (UTF16
)((ch
& halfMask
) + UNI_SUR_LOW_START
);
633 *sourceStart
= source
;
634 *targetStart
= target
;
638 /* --------------------------------------------------------------------- */
640 static ConversionResult
ConvertUTF8toUTF32Impl(
641 const UTF8
** sourceStart
, const UTF8
* sourceEnd
,
642 UTF32
** targetStart
, UTF32
* targetEnd
, ConversionFlags flags
,
643 Boolean InputIsPartial
) {
644 ConversionResult result
= conversionOK
;
645 const UTF8
* source
= *sourceStart
;
646 UTF32
* target
= *targetStart
;
647 while (source
< sourceEnd
) {
649 unsigned short extraBytesToRead
= trailingBytesForUTF8
[*source
];
650 if (extraBytesToRead
>= sourceEnd
- source
) {
651 if (flags
== strictConversion
|| InputIsPartial
) {
652 result
= sourceExhausted
;
655 result
= sourceIllegal
;
658 * Replace the maximal subpart of ill-formed sequence with
659 * replacement character.
661 source
+= findMaximalSubpartOfIllFormedUTF8Sequence(source
,
663 *target
++ = UNI_REPLACEMENT_CHAR
;
667 if (target
>= targetEnd
) {
668 result
= targetExhausted
; break;
671 /* Do this check whether lenient or strict */
672 if (!isLegalUTF8(source
, extraBytesToRead
+1)) {
673 result
= sourceIllegal
;
674 if (flags
== strictConversion
) {
675 /* Abort conversion. */
679 * Replace the maximal subpart of ill-formed sequence with
680 * replacement character.
682 source
+= findMaximalSubpartOfIllFormedUTF8Sequence(source
,
684 *target
++ = UNI_REPLACEMENT_CHAR
;
689 * The cases all fall through. See "Note A" below.
691 switch (extraBytesToRead
) {
692 case 5: ch
+= *source
++; ch
<<= 6;
693 case 4: ch
+= *source
++; ch
<<= 6;
694 case 3: ch
+= *source
++; ch
<<= 6;
695 case 2: ch
+= *source
++; ch
<<= 6;
696 case 1: ch
+= *source
++; ch
<<= 6;
697 case 0: ch
+= *source
++;
699 ch
-= offsetsFromUTF8
[extraBytesToRead
];
701 if (ch
<= UNI_MAX_LEGAL_UTF32
) {
703 * UTF-16 surrogate values are illegal in UTF-32, and anything
704 * over Plane 17 (> 0x10FFFF) is illegal.
706 if (ch
>= UNI_SUR_HIGH_START
&& ch
<= UNI_SUR_LOW_END
) {
707 if (flags
== strictConversion
) {
708 source
-= (extraBytesToRead
+1); /* return to the illegal value itself */
709 result
= sourceIllegal
;
712 *target
++ = UNI_REPLACEMENT_CHAR
;
717 } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
718 result
= sourceIllegal
;
719 *target
++ = UNI_REPLACEMENT_CHAR
;
722 *sourceStart
= source
;
723 *targetStart
= target
;
727 ConversionResult
ConvertUTF8toUTF32Partial(const UTF8
**sourceStart
,
728 const UTF8
*sourceEnd
,
731 ConversionFlags flags
) {
732 return ConvertUTF8toUTF32Impl(sourceStart
, sourceEnd
, targetStart
, targetEnd
,
733 flags
, /*InputIsPartial=*/true);
736 ConversionResult
ConvertUTF8toUTF32(const UTF8
**sourceStart
,
737 const UTF8
*sourceEnd
, UTF32
**targetStart
,
738 UTF32
*targetEnd
, ConversionFlags flags
) {
739 return ConvertUTF8toUTF32Impl(sourceStart
, sourceEnd
, targetStart
, targetEnd
,
740 flags
, /*InputIsPartial=*/false);
743 /* ---------------------------------------------------------------------
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:
750 int tmpBytesToRead = extraBytesToRead+1;
754 if (tmpBytesToRead) ch <<= 6;
755 } while (tmpBytesToRead > 0);
757 In UTF-8 writing code, the switches on "bytesToWrite" are
758 similarly unrolled loops.
760 --------------------------------------------------------------------- */
764 ConvertUTF_RESTORE_WARNINGS