ConvertUTF.cpp 29 KB

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