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.

821 lines
27 KiB

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