ConvertUTF.cpp 27 KB

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