You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

825 lines
27 KiB

  1. /* stringlib: codec implementations */
  2. #if !STRINGLIB_IS_UNICODE
  3. # error "codecs.h is specific to Unicode"
  4. #endif
  5. #include "pycore_bitutils.h" // _Py_bswap32()
  6. /* Mask to quickly check whether a C 'long' contains a
  7. non-ASCII, UTF8-encoded char. */
  8. #if (SIZEOF_LONG == 8)
  9. # define ASCII_CHAR_MASK 0x8080808080808080UL
  10. #elif (SIZEOF_LONG == 4)
  11. # define ASCII_CHAR_MASK 0x80808080UL
  12. #else
  13. # error C 'long' size should be either 4 or 8!
  14. #endif
  15. /* 10xxxxxx */
  16. #define IS_CONTINUATION_BYTE(ch) ((ch) >= 0x80 && (ch) < 0xC0)
  17. Py_LOCAL_INLINE(Py_UCS4)
  18. STRINGLIB(utf8_decode)(const char **inptr, const char *end,
  19. STRINGLIB_CHAR *dest,
  20. Py_ssize_t *outpos)
  21. {
  22. Py_UCS4 ch;
  23. const char *s = *inptr;
  24. const char *aligned_end = (const char *) _Py_ALIGN_DOWN(end, SIZEOF_LONG);
  25. STRINGLIB_CHAR *p = dest + *outpos;
  26. while (s < end) {
  27. ch = (unsigned char)*s;
  28. if (ch < 0x80) {
  29. /* Fast path for runs of ASCII characters. Given that common UTF-8
  30. input will consist of an overwhelming majority of ASCII
  31. characters, we try to optimize for this case by checking
  32. as many characters as a C 'long' can contain.
  33. First, check if we can do an aligned read, as most CPUs have
  34. a penalty for unaligned reads.
  35. */
  36. if (_Py_IS_ALIGNED(s, SIZEOF_LONG)) {
  37. /* Help register allocation */
  38. const char *_s = s;
  39. STRINGLIB_CHAR *_p = p;
  40. while (_s < aligned_end) {
  41. /* Read a whole long at a time (either 4 or 8 bytes),
  42. and do a fast unrolled copy if it only contains ASCII
  43. characters. */
  44. unsigned long value = *(const unsigned long *) _s;
  45. if (value & ASCII_CHAR_MASK)
  46. break;
  47. #if PY_LITTLE_ENDIAN
  48. _p[0] = (STRINGLIB_CHAR)(value & 0xFFu);
  49. _p[1] = (STRINGLIB_CHAR)((value >> 8) & 0xFFu);
  50. _p[2] = (STRINGLIB_CHAR)((value >> 16) & 0xFFu);
  51. _p[3] = (STRINGLIB_CHAR)((value >> 24) & 0xFFu);
  52. # if SIZEOF_LONG == 8
  53. _p[4] = (STRINGLIB_CHAR)((value >> 32) & 0xFFu);
  54. _p[5] = (STRINGLIB_CHAR)((value >> 40) & 0xFFu);
  55. _p[6] = (STRINGLIB_CHAR)((value >> 48) & 0xFFu);
  56. _p[7] = (STRINGLIB_CHAR)((value >> 56) & 0xFFu);
  57. # endif
  58. #else
  59. # if SIZEOF_LONG == 8
  60. _p[0] = (STRINGLIB_CHAR)((value >> 56) & 0xFFu);
  61. _p[1] = (STRINGLIB_CHAR)((value >> 48) & 0xFFu);
  62. _p[2] = (STRINGLIB_CHAR)((value >> 40) & 0xFFu);
  63. _p[3] = (STRINGLIB_CHAR)((value >> 32) & 0xFFu);
  64. _p[4] = (STRINGLIB_CHAR)((value >> 24) & 0xFFu);
  65. _p[5] = (STRINGLIB_CHAR)((value >> 16) & 0xFFu);
  66. _p[6] = (STRINGLIB_CHAR)((value >> 8) & 0xFFu);
  67. _p[7] = (STRINGLIB_CHAR)(value & 0xFFu);
  68. # else
  69. _p[0] = (STRINGLIB_CHAR)((value >> 24) & 0xFFu);
  70. _p[1] = (STRINGLIB_CHAR)((value >> 16) & 0xFFu);
  71. _p[2] = (STRINGLIB_CHAR)((value >> 8) & 0xFFu);
  72. _p[3] = (STRINGLIB_CHAR)(value & 0xFFu);
  73. # endif
  74. #endif
  75. _s += SIZEOF_LONG;
  76. _p += SIZEOF_LONG;
  77. }
  78. s = _s;
  79. p = _p;
  80. if (s == end)
  81. break;
  82. ch = (unsigned char)*s;
  83. }
  84. if (ch < 0x80) {
  85. s++;
  86. *p++ = ch;
  87. continue;
  88. }
  89. }
  90. if (ch < 0xE0) {
  91. /* \xC2\x80-\xDF\xBF -- 0080-07FF */
  92. Py_UCS4 ch2;
  93. if (ch < 0xC2) {
  94. /* invalid sequence
  95. \x80-\xBF -- continuation byte
  96. \xC0-\xC1 -- fake 0000-007F */
  97. goto InvalidStart;
  98. }
  99. if (end - s < 2) {
  100. /* unexpected end of data: the caller will decide whether
  101. it's an error or not */
  102. break;
  103. }
  104. ch2 = (unsigned char)s[1];
  105. if (!IS_CONTINUATION_BYTE(ch2))
  106. /* invalid continuation byte */
  107. goto InvalidContinuation1;
  108. ch = (ch << 6) + ch2 -
  109. ((0xC0 << 6) + 0x80);
  110. assert ((ch > 0x007F) && (ch <= 0x07FF));
  111. s += 2;
  112. if (STRINGLIB_MAX_CHAR <= 0x007F ||
  113. (STRINGLIB_MAX_CHAR < 0x07FF && ch > STRINGLIB_MAX_CHAR))
  114. /* Out-of-range */
  115. goto Return;
  116. *p++ = ch;
  117. continue;
  118. }
  119. if (ch < 0xF0) {
  120. /* \xE0\xA0\x80-\xEF\xBF\xBF -- 0800-FFFF */
  121. Py_UCS4 ch2, ch3;
  122. if (end - s < 3) {
  123. /* unexpected end of data: the caller will decide whether
  124. it's an error or not */
  125. if (end - s < 2)
  126. break;
  127. ch2 = (unsigned char)s[1];
  128. if (!IS_CONTINUATION_BYTE(ch2) ||
  129. (ch2 < 0xA0 ? ch == 0xE0 : ch == 0xED))
  130. /* for clarification see comments below */
  131. goto InvalidContinuation1;
  132. break;
  133. }
  134. ch2 = (unsigned char)s[1];
  135. ch3 = (unsigned char)s[2];
  136. if (!IS_CONTINUATION_BYTE(ch2)) {
  137. /* invalid continuation byte */
  138. goto InvalidContinuation1;
  139. }
  140. if (ch == 0xE0) {
  141. if (ch2 < 0xA0)
  142. /* invalid sequence
  143. \xE0\x80\x80-\xE0\x9F\xBF -- fake 0000-0800 */
  144. goto InvalidContinuation1;
  145. } else if (ch == 0xED && ch2 >= 0xA0) {
  146. /* Decoding UTF-8 sequences in range \xED\xA0\x80-\xED\xBF\xBF
  147. will result in surrogates in range D800-DFFF. Surrogates are
  148. not valid UTF-8 so they are rejected.
  149. See https://www.unicode.org/versions/Unicode5.2.0/ch03.pdf
  150. (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */
  151. goto InvalidContinuation1;
  152. }
  153. if (!IS_CONTINUATION_BYTE(ch3)) {
  154. /* invalid continuation byte */
  155. goto InvalidContinuation2;
  156. }
  157. ch = (ch << 12) + (ch2 << 6) + ch3 -
  158. ((0xE0 << 12) + (0x80 << 6) + 0x80);
  159. assert ((ch > 0x07FF) && (ch <= 0xFFFF));
  160. s += 3;
  161. if (STRINGLIB_MAX_CHAR <= 0x07FF ||
  162. (STRINGLIB_MAX_CHAR < 0xFFFF && ch > STRINGLIB_MAX_CHAR))
  163. /* Out-of-range */
  164. goto Return;
  165. *p++ = ch;
  166. continue;
  167. }
  168. if (ch < 0xF5) {
  169. /* \xF0\x90\x80\x80-\xF4\x8F\xBF\xBF -- 10000-10FFFF */
  170. Py_UCS4 ch2, ch3, ch4;
  171. if (end - s < 4) {
  172. /* unexpected end of data: the caller will decide whether
  173. it's an error or not */
  174. if (end - s < 2)
  175. break;
  176. ch2 = (unsigned char)s[1];
  177. if (!IS_CONTINUATION_BYTE(ch2) ||
  178. (ch2 < 0x90 ? ch == 0xF0 : ch == 0xF4))
  179. /* for clarification see comments below */
  180. goto InvalidContinuation1;
  181. if (end - s < 3)
  182. break;
  183. ch3 = (unsigned char)s[2];
  184. if (!IS_CONTINUATION_BYTE(ch3))
  185. goto InvalidContinuation2;
  186. break;
  187. }
  188. ch2 = (unsigned char)s[1];
  189. ch3 = (unsigned char)s[2];
  190. ch4 = (unsigned char)s[3];
  191. if (!IS_CONTINUATION_BYTE(ch2)) {
  192. /* invalid continuation byte */
  193. goto InvalidContinuation1;
  194. }
  195. if (ch == 0xF0) {
  196. if (ch2 < 0x90)
  197. /* invalid sequence
  198. \xF0\x80\x80\x80-\xF0\x8F\xBF\xBF -- fake 0000-FFFF */
  199. goto InvalidContinuation1;
  200. } else if (ch == 0xF4 && ch2 >= 0x90) {
  201. /* invalid sequence
  202. \xF4\x90\x80\x80- -- 110000- overflow */
  203. goto InvalidContinuation1;
  204. }
  205. if (!IS_CONTINUATION_BYTE(ch3)) {
  206. /* invalid continuation byte */
  207. goto InvalidContinuation2;
  208. }
  209. if (!IS_CONTINUATION_BYTE(ch4)) {
  210. /* invalid continuation byte */
  211. goto InvalidContinuation3;
  212. }
  213. ch = (ch << 18) + (ch2 << 12) + (ch3 << 6) + ch4 -
  214. ((0xF0 << 18) + (0x80 << 12) + (0x80 << 6) + 0x80);
  215. assert ((ch > 0xFFFF) && (ch <= 0x10FFFF));
  216. s += 4;
  217. if (STRINGLIB_MAX_CHAR <= 0xFFFF ||
  218. (STRINGLIB_MAX_CHAR < 0x10FFFF && ch > STRINGLIB_MAX_CHAR))
  219. /* Out-of-range */
  220. goto Return;
  221. *p++ = ch;
  222. continue;
  223. }
  224. goto InvalidStart;
  225. }
  226. ch = 0;
  227. Return:
  228. *inptr = s;
  229. *outpos = p - dest;
  230. return ch;
  231. InvalidStart:
  232. ch = 1;
  233. goto Return;
  234. InvalidContinuation1:
  235. ch = 2;
  236. goto Return;
  237. InvalidContinuation2:
  238. ch = 3;
  239. goto Return;
  240. InvalidContinuation3:
  241. ch = 4;
  242. goto Return;
  243. }
  244. #undef ASCII_CHAR_MASK
  245. /* UTF-8 encoder specialized for a Unicode kind to avoid the slow
  246. PyUnicode_READ() macro. Delete some parts of the code depending on the kind:
  247. UCS-1 strings don't need to handle surrogates for example. */
  248. Py_LOCAL_INLINE(char *)
  249. STRINGLIB(utf8_encoder)(_PyBytesWriter *writer,
  250. PyObject *unicode,
  251. const STRINGLIB_CHAR *data,
  252. Py_ssize_t size,
  253. _Py_error_handler error_handler,
  254. const char *errors)
  255. {
  256. Py_ssize_t i; /* index into data of next input character */
  257. char *p; /* next free byte in output buffer */
  258. #if STRINGLIB_SIZEOF_CHAR > 1
  259. PyObject *error_handler_obj = NULL;
  260. PyObject *exc = NULL;
  261. PyObject *rep = NULL;
  262. #endif
  263. #if STRINGLIB_SIZEOF_CHAR == 1
  264. const Py_ssize_t max_char_size = 2;
  265. #elif STRINGLIB_SIZEOF_CHAR == 2
  266. const Py_ssize_t max_char_size = 3;
  267. #else /* STRINGLIB_SIZEOF_CHAR == 4 */
  268. const Py_ssize_t max_char_size = 4;
  269. #endif
  270. assert(size >= 0);
  271. if (size > PY_SSIZE_T_MAX / max_char_size) {
  272. /* integer overflow */
  273. PyErr_NoMemory();
  274. return NULL;
  275. }
  276. _PyBytesWriter_Init(writer);
  277. p = _PyBytesWriter_Alloc(writer, size * max_char_size);
  278. if (p == NULL)
  279. return NULL;
  280. for (i = 0; i < size;) {
  281. Py_UCS4 ch = data[i++];
  282. if (ch < 0x80) {
  283. /* Encode ASCII */
  284. *p++ = (char) ch;
  285. }
  286. else
  287. #if STRINGLIB_SIZEOF_CHAR > 1
  288. if (ch < 0x0800)
  289. #endif
  290. {
  291. /* Encode Latin-1 */
  292. *p++ = (char)(0xc0 | (ch >> 6));
  293. *p++ = (char)(0x80 | (ch & 0x3f));
  294. }
  295. #if STRINGLIB_SIZEOF_CHAR > 1
  296. else if (Py_UNICODE_IS_SURROGATE(ch)) {
  297. Py_ssize_t startpos, endpos, newpos;
  298. Py_ssize_t k;
  299. if (error_handler == _Py_ERROR_UNKNOWN) {
  300. error_handler = _Py_GetErrorHandler(errors);
  301. }
  302. startpos = i-1;
  303. endpos = startpos+1;
  304. while ((endpos < size) && Py_UNICODE_IS_SURROGATE(data[endpos]))
  305. endpos++;
  306. /* Only overallocate the buffer if it's not the last write */
  307. writer->overallocate = (endpos < size);
  308. switch (error_handler)
  309. {
  310. case _Py_ERROR_REPLACE:
  311. memset(p, '?', endpos - startpos);
  312. p += (endpos - startpos);
  313. /* fall through */
  314. case _Py_ERROR_IGNORE:
  315. i += (endpos - startpos - 1);
  316. break;
  317. case _Py_ERROR_SURROGATEPASS:
  318. for (k=startpos; k<endpos; k++) {
  319. ch = data[k];
  320. *p++ = (char)(0xe0 | (ch >> 12));
  321. *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
  322. *p++ = (char)(0x80 | (ch & 0x3f));
  323. }
  324. i += (endpos - startpos - 1);
  325. break;
  326. case _Py_ERROR_BACKSLASHREPLACE:
  327. /* subtract preallocated bytes */
  328. writer->min_size -= max_char_size * (endpos - startpos);
  329. p = backslashreplace(writer, p,
  330. unicode, startpos, endpos);
  331. if (p == NULL)
  332. goto error;
  333. i += (endpos - startpos - 1);
  334. break;
  335. case _Py_ERROR_XMLCHARREFREPLACE:
  336. /* subtract preallocated bytes */
  337. writer->min_size -= max_char_size * (endpos - startpos);
  338. p = xmlcharrefreplace(writer, p,
  339. unicode, startpos, endpos);
  340. if (p == NULL)
  341. goto error;
  342. i += (endpos - startpos - 1);
  343. break;
  344. case _Py_ERROR_SURROGATEESCAPE:
  345. for (k=startpos; k<endpos; k++) {
  346. ch = data[k];
  347. if (!(0xDC80 <= ch && ch <= 0xDCFF))
  348. break;
  349. *p++ = (char)(ch & 0xff);
  350. }
  351. if (k >= endpos) {
  352. i += (endpos - startpos - 1);
  353. break;
  354. }
  355. startpos = k;
  356. assert(startpos < endpos);
  357. /* fall through */
  358. default:
  359. rep = unicode_encode_call_errorhandler(
  360. errors, &error_handler_obj, "utf-8", "surrogates not allowed",
  361. unicode, &exc, startpos, endpos, &newpos);
  362. if (!rep)
  363. goto error;
  364. /* subtract preallocated bytes */
  365. writer->min_size -= max_char_size * (newpos - startpos);
  366. if (PyBytes_Check(rep)) {
  367. p = _PyBytesWriter_WriteBytes(writer, p,
  368. PyBytes_AS_STRING(rep),
  369. PyBytes_GET_SIZE(rep));
  370. }
  371. else {
  372. /* rep is unicode */
  373. if (PyUnicode_READY(rep) < 0)
  374. goto error;
  375. if (!PyUnicode_IS_ASCII(rep)) {
  376. raise_encode_exception(&exc, "utf-8", unicode,
  377. startpos, endpos,
  378. "surrogates not allowed");
  379. goto error;
  380. }
  381. p = _PyBytesWriter_WriteBytes(writer, p,
  382. PyUnicode_DATA(rep),
  383. PyUnicode_GET_LENGTH(rep));
  384. }
  385. if (p == NULL)
  386. goto error;
  387. Py_CLEAR(rep);
  388. i = newpos;
  389. }
  390. /* If overallocation was disabled, ensure that it was the last
  391. write. Otherwise, we missed an optimization */
  392. assert(writer->overallocate || i == size);
  393. }
  394. else
  395. #if STRINGLIB_SIZEOF_CHAR > 2
  396. if (ch < 0x10000)
  397. #endif
  398. {
  399. *p++ = (char)(0xe0 | (ch >> 12));
  400. *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
  401. *p++ = (char)(0x80 | (ch & 0x3f));
  402. }
  403. #if STRINGLIB_SIZEOF_CHAR > 2
  404. else /* ch >= 0x10000 */
  405. {
  406. assert(ch <= MAX_UNICODE);
  407. /* Encode UCS4 Unicode ordinals */
  408. *p++ = (char)(0xf0 | (ch >> 18));
  409. *p++ = (char)(0x80 | ((ch >> 12) & 0x3f));
  410. *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
  411. *p++ = (char)(0x80 | (ch & 0x3f));
  412. }
  413. #endif /* STRINGLIB_SIZEOF_CHAR > 2 */
  414. #endif /* STRINGLIB_SIZEOF_CHAR > 1 */
  415. }
  416. #if STRINGLIB_SIZEOF_CHAR > 1
  417. Py_XDECREF(error_handler_obj);
  418. Py_XDECREF(exc);
  419. #endif
  420. return p;
  421. #if STRINGLIB_SIZEOF_CHAR > 1
  422. error:
  423. Py_XDECREF(rep);
  424. Py_XDECREF(error_handler_obj);
  425. Py_XDECREF(exc);
  426. return NULL;
  427. #endif
  428. }
  429. /* The pattern for constructing UCS2-repeated masks. */
  430. #if SIZEOF_LONG == 8
  431. # define UCS2_REPEAT_MASK 0x0001000100010001ul
  432. #elif SIZEOF_LONG == 4
  433. # define UCS2_REPEAT_MASK 0x00010001ul
  434. #else
  435. # error C 'long' size should be either 4 or 8!
  436. #endif
  437. /* The mask for fast checking. */
  438. #if STRINGLIB_SIZEOF_CHAR == 1
  439. /* The mask for fast checking of whether a C 'long' contains a
  440. non-ASCII or non-Latin1 UTF16-encoded characters. */
  441. # define FAST_CHAR_MASK (UCS2_REPEAT_MASK * (0xFFFFu & ~STRINGLIB_MAX_CHAR))
  442. #else
  443. /* The mask for fast checking of whether a C 'long' may contain
  444. UTF16-encoded surrogate characters. This is an efficient heuristic,
  445. assuming that non-surrogate characters with a code point >= 0x8000 are
  446. rare in most input.
  447. */
  448. # define FAST_CHAR_MASK (UCS2_REPEAT_MASK * 0x8000u)
  449. #endif
  450. /* The mask for fast byte-swapping. */
  451. #define STRIPPED_MASK (UCS2_REPEAT_MASK * 0x00FFu)
  452. /* Swap bytes. */
  453. #define SWAB(value) ((((value) >> 8) & STRIPPED_MASK) | \
  454. (((value) & STRIPPED_MASK) << 8))
  455. Py_LOCAL_INLINE(Py_UCS4)
  456. STRINGLIB(utf16_decode)(const unsigned char **inptr, const unsigned char *e,
  457. STRINGLIB_CHAR *dest, Py_ssize_t *outpos,
  458. int native_ordering)
  459. {
  460. Py_UCS4 ch;
  461. const unsigned char *aligned_end =
  462. (const unsigned char *) _Py_ALIGN_DOWN(e, SIZEOF_LONG);
  463. const unsigned char *q = *inptr;
  464. STRINGLIB_CHAR *p = dest + *outpos;
  465. /* Offsets from q for retrieving byte pairs in the right order. */
  466. #if PY_LITTLE_ENDIAN
  467. int ihi = !!native_ordering, ilo = !native_ordering;
  468. #else
  469. int ihi = !native_ordering, ilo = !!native_ordering;
  470. #endif
  471. --e;
  472. while (q < e) {
  473. Py_UCS4 ch2;
  474. /* First check for possible aligned read of a C 'long'. Unaligned
  475. reads are more expensive, better to defer to another iteration. */
  476. if (_Py_IS_ALIGNED(q, SIZEOF_LONG)) {
  477. /* Fast path for runs of in-range non-surrogate chars. */
  478. const unsigned char *_q = q;
  479. while (_q < aligned_end) {
  480. unsigned long block = * (const unsigned long *) _q;
  481. if (native_ordering) {
  482. /* Can use buffer directly */
  483. if (block & FAST_CHAR_MASK)
  484. break;
  485. }
  486. else {
  487. /* Need to byte-swap */
  488. if (block & SWAB(FAST_CHAR_MASK))
  489. break;
  490. #if STRINGLIB_SIZEOF_CHAR == 1
  491. block >>= 8;
  492. #else
  493. block = SWAB(block);
  494. #endif
  495. }
  496. #if PY_LITTLE_ENDIAN
  497. # if SIZEOF_LONG == 4
  498. p[0] = (STRINGLIB_CHAR)(block & 0xFFFFu);
  499. p[1] = (STRINGLIB_CHAR)(block >> 16);
  500. # elif SIZEOF_LONG == 8
  501. p[0] = (STRINGLIB_CHAR)(block & 0xFFFFu);
  502. p[1] = (STRINGLIB_CHAR)((block >> 16) & 0xFFFFu);
  503. p[2] = (STRINGLIB_CHAR)((block >> 32) & 0xFFFFu);
  504. p[3] = (STRINGLIB_CHAR)(block >> 48);
  505. # endif
  506. #else
  507. # if SIZEOF_LONG == 4
  508. p[0] = (STRINGLIB_CHAR)(block >> 16);
  509. p[1] = (STRINGLIB_CHAR)(block & 0xFFFFu);
  510. # elif SIZEOF_LONG == 8
  511. p[0] = (STRINGLIB_CHAR)(block >> 48);
  512. p[1] = (STRINGLIB_CHAR)((block >> 32) & 0xFFFFu);
  513. p[2] = (STRINGLIB_CHAR)((block >> 16) & 0xFFFFu);
  514. p[3] = (STRINGLIB_CHAR)(block & 0xFFFFu);
  515. # endif
  516. #endif
  517. _q += SIZEOF_LONG;
  518. p += SIZEOF_LONG / 2;
  519. }
  520. q = _q;
  521. if (q >= e)
  522. break;
  523. }
  524. ch = (q[ihi] << 8) | q[ilo];
  525. q += 2;
  526. if (!Py_UNICODE_IS_SURROGATE(ch)) {
  527. #if STRINGLIB_SIZEOF_CHAR < 2
  528. if (ch > STRINGLIB_MAX_CHAR)
  529. /* Out-of-range */
  530. goto Return;
  531. #endif
  532. *p++ = (STRINGLIB_CHAR)ch;
  533. continue;
  534. }
  535. /* UTF-16 code pair: */
  536. if (!Py_UNICODE_IS_HIGH_SURROGATE(ch))
  537. goto IllegalEncoding;
  538. if (q >= e)
  539. goto UnexpectedEnd;
  540. ch2 = (q[ihi] << 8) | q[ilo];
  541. q += 2;
  542. if (!Py_UNICODE_IS_LOW_SURROGATE(ch2))
  543. goto IllegalSurrogate;
  544. ch = Py_UNICODE_JOIN_SURROGATES(ch, ch2);
  545. #if STRINGLIB_SIZEOF_CHAR < 4
  546. /* Out-of-range */
  547. goto Return;
  548. #else
  549. *p++ = (STRINGLIB_CHAR)ch;
  550. #endif
  551. }
  552. ch = 0;
  553. Return:
  554. *inptr = q;
  555. *outpos = p - dest;
  556. return ch;
  557. UnexpectedEnd:
  558. ch = 1;
  559. goto Return;
  560. IllegalEncoding:
  561. ch = 2;
  562. goto Return;
  563. IllegalSurrogate:
  564. ch = 3;
  565. goto Return;
  566. }
  567. #undef UCS2_REPEAT_MASK
  568. #undef FAST_CHAR_MASK
  569. #undef STRIPPED_MASK
  570. #undef SWAB
  571. #if STRINGLIB_MAX_CHAR >= 0x80
  572. Py_LOCAL_INLINE(Py_ssize_t)
  573. STRINGLIB(utf16_encode)(const STRINGLIB_CHAR *in,
  574. Py_ssize_t len,
  575. unsigned short **outptr,
  576. int native_ordering)
  577. {
  578. unsigned short *out = *outptr;
  579. const STRINGLIB_CHAR *end = in + len;
  580. #if STRINGLIB_SIZEOF_CHAR == 1
  581. if (native_ordering) {
  582. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  583. while (in < unrolled_end) {
  584. out[0] = in[0];
  585. out[1] = in[1];
  586. out[2] = in[2];
  587. out[3] = in[3];
  588. in += 4; out += 4;
  589. }
  590. while (in < end) {
  591. *out++ = *in++;
  592. }
  593. } else {
  594. # define SWAB2(CH) ((CH) << 8) /* high byte is zero */
  595. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  596. while (in < unrolled_end) {
  597. out[0] = SWAB2(in[0]);
  598. out[1] = SWAB2(in[1]);
  599. out[2] = SWAB2(in[2]);
  600. out[3] = SWAB2(in[3]);
  601. in += 4; out += 4;
  602. }
  603. while (in < end) {
  604. Py_UCS4 ch = *in++;
  605. *out++ = SWAB2((Py_UCS2)ch);
  606. }
  607. #undef SWAB2
  608. }
  609. *outptr = out;
  610. return len;
  611. #else
  612. if (native_ordering) {
  613. #if STRINGLIB_MAX_CHAR < 0x10000
  614. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  615. while (in < unrolled_end) {
  616. /* check if any character is a surrogate character */
  617. if (((in[0] ^ 0xd800) &
  618. (in[1] ^ 0xd800) &
  619. (in[2] ^ 0xd800) &
  620. (in[3] ^ 0xd800) & 0xf800) == 0)
  621. break;
  622. out[0] = in[0];
  623. out[1] = in[1];
  624. out[2] = in[2];
  625. out[3] = in[3];
  626. in += 4; out += 4;
  627. }
  628. #endif
  629. while (in < end) {
  630. Py_UCS4 ch;
  631. ch = *in++;
  632. if (ch < 0xd800)
  633. *out++ = ch;
  634. else if (ch < 0xe000)
  635. /* reject surrogate characters (U+D800-U+DFFF) */
  636. goto fail;
  637. #if STRINGLIB_MAX_CHAR >= 0x10000
  638. else if (ch >= 0x10000) {
  639. out[0] = Py_UNICODE_HIGH_SURROGATE(ch);
  640. out[1] = Py_UNICODE_LOW_SURROGATE(ch);
  641. out += 2;
  642. }
  643. #endif
  644. else
  645. *out++ = ch;
  646. }
  647. } else {
  648. #define SWAB2(CH) (((CH) << 8) | ((CH) >> 8))
  649. #if STRINGLIB_MAX_CHAR < 0x10000
  650. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  651. while (in < unrolled_end) {
  652. /* check if any character is a surrogate character */
  653. if (((in[0] ^ 0xd800) &
  654. (in[1] ^ 0xd800) &
  655. (in[2] ^ 0xd800) &
  656. (in[3] ^ 0xd800) & 0xf800) == 0)
  657. break;
  658. out[0] = SWAB2(in[0]);
  659. out[1] = SWAB2(in[1]);
  660. out[2] = SWAB2(in[2]);
  661. out[3] = SWAB2(in[3]);
  662. in += 4; out += 4;
  663. }
  664. #endif
  665. while (in < end) {
  666. Py_UCS4 ch = *in++;
  667. if (ch < 0xd800)
  668. *out++ = SWAB2((Py_UCS2)ch);
  669. else if (ch < 0xe000)
  670. /* reject surrogate characters (U+D800-U+DFFF) */
  671. goto fail;
  672. #if STRINGLIB_MAX_CHAR >= 0x10000
  673. else if (ch >= 0x10000) {
  674. Py_UCS2 ch1 = Py_UNICODE_HIGH_SURROGATE(ch);
  675. Py_UCS2 ch2 = Py_UNICODE_LOW_SURROGATE(ch);
  676. out[0] = SWAB2(ch1);
  677. out[1] = SWAB2(ch2);
  678. out += 2;
  679. }
  680. #endif
  681. else
  682. *out++ = SWAB2((Py_UCS2)ch);
  683. }
  684. #undef SWAB2
  685. }
  686. *outptr = out;
  687. return len;
  688. fail:
  689. *outptr = out;
  690. return len - (end - in + 1);
  691. #endif
  692. }
  693. static inline uint32_t
  694. STRINGLIB(SWAB4)(STRINGLIB_CHAR ch)
  695. {
  696. uint32_t word = ch;
  697. #if STRINGLIB_SIZEOF_CHAR == 1
  698. /* high bytes are zero */
  699. return (word << 24);
  700. #elif STRINGLIB_SIZEOF_CHAR == 2
  701. /* high bytes are zero */
  702. return ((word & 0x00FFu) << 24) | ((word & 0xFF00u) << 8);
  703. #else
  704. return _Py_bswap32(word);
  705. #endif
  706. }
  707. Py_LOCAL_INLINE(Py_ssize_t)
  708. STRINGLIB(utf32_encode)(const STRINGLIB_CHAR *in,
  709. Py_ssize_t len,
  710. uint32_t **outptr,
  711. int native_ordering)
  712. {
  713. uint32_t *out = *outptr;
  714. const STRINGLIB_CHAR *end = in + len;
  715. if (native_ordering) {
  716. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  717. while (in < unrolled_end) {
  718. #if STRINGLIB_SIZEOF_CHAR > 1
  719. /* check if any character is a surrogate character */
  720. if (((in[0] ^ 0xd800) &
  721. (in[1] ^ 0xd800) &
  722. (in[2] ^ 0xd800) &
  723. (in[3] ^ 0xd800) & 0xf800) == 0)
  724. break;
  725. #endif
  726. out[0] = in[0];
  727. out[1] = in[1];
  728. out[2] = in[2];
  729. out[3] = in[3];
  730. in += 4; out += 4;
  731. }
  732. while (in < end) {
  733. Py_UCS4 ch;
  734. ch = *in++;
  735. #if STRINGLIB_SIZEOF_CHAR > 1
  736. if (Py_UNICODE_IS_SURROGATE(ch)) {
  737. /* reject surrogate characters (U+D800-U+DFFF) */
  738. goto fail;
  739. }
  740. #endif
  741. *out++ = ch;
  742. }
  743. } else {
  744. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  745. while (in < unrolled_end) {
  746. #if STRINGLIB_SIZEOF_CHAR > 1
  747. /* check if any character is a surrogate character */
  748. if (((in[0] ^ 0xd800) &
  749. (in[1] ^ 0xd800) &
  750. (in[2] ^ 0xd800) &
  751. (in[3] ^ 0xd800) & 0xf800) == 0)
  752. break;
  753. #endif
  754. out[0] = STRINGLIB(SWAB4)(in[0]);
  755. out[1] = STRINGLIB(SWAB4)(in[1]);
  756. out[2] = STRINGLIB(SWAB4)(in[2]);
  757. out[3] = STRINGLIB(SWAB4)(in[3]);
  758. in += 4; out += 4;
  759. }
  760. while (in < end) {
  761. Py_UCS4 ch = *in++;
  762. #if STRINGLIB_SIZEOF_CHAR > 1
  763. if (Py_UNICODE_IS_SURROGATE(ch)) {
  764. /* reject surrogate characters (U+D800-U+DFFF) */
  765. goto fail;
  766. }
  767. #endif
  768. *out++ = STRINGLIB(SWAB4)(ch);
  769. }
  770. }
  771. *outptr = out;
  772. return len;
  773. #if STRINGLIB_SIZEOF_CHAR > 1
  774. fail:
  775. *outptr = out;
  776. return len - (end - in + 1);
  777. #endif
  778. }
  779. #endif