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.

828 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 = *(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 http://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\80- -- 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(PyObject *)
  248. STRINGLIB(utf8_encoder)(PyObject *unicode,
  249. STRINGLIB_CHAR *data,
  250. Py_ssize_t size,
  251. const char *errors)
  252. {
  253. #define MAX_SHORT_UNICHARS 300 /* largest size we'll do on the stack */
  254. Py_ssize_t i; /* index into s of next input byte */
  255. char *p; /* next free byte in output buffer */
  256. #if STRINGLIB_SIZEOF_CHAR > 1
  257. PyObject *error_handler_obj = NULL;
  258. PyObject *exc = NULL;
  259. PyObject *rep = NULL;
  260. _Py_error_handler error_handler = _Py_ERROR_UNKNOWN;
  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. _PyBytesWriter writer;
  270. assert(size >= 0);
  271. _PyBytesWriter_Init(&writer);
  272. if (size > PY_SSIZE_T_MAX / max_char_size) {
  273. /* integer overflow */
  274. return PyErr_NoMemory();
  275. }
  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 = get_error_handler(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 the ignore handler */
  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 the default handler */
  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;
  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",
  376. unicode,
  377. i-1, i,
  378. "surrogates not allowed");
  379. goto error;
  380. }
  381. assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND);
  382. p = _PyBytesWriter_WriteBytes(&writer, p,
  383. PyUnicode_DATA(rep),
  384. PyUnicode_GET_LENGTH(rep));
  385. }
  386. if (p == NULL)
  387. goto error;
  388. Py_CLEAR(rep);
  389. i = newpos;
  390. }
  391. /* If overallocation was disabled, ensure that it was the last
  392. write. Otherwise, we missed an optimization */
  393. assert(writer.overallocate || i == size);
  394. }
  395. else
  396. #if STRINGLIB_SIZEOF_CHAR > 2
  397. if (ch < 0x10000)
  398. #endif
  399. {
  400. *p++ = (char)(0xe0 | (ch >> 12));
  401. *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
  402. *p++ = (char)(0x80 | (ch & 0x3f));
  403. }
  404. #if STRINGLIB_SIZEOF_CHAR > 2
  405. else /* ch >= 0x10000 */
  406. {
  407. assert(ch <= MAX_UNICODE);
  408. /* Encode UCS4 Unicode ordinals */
  409. *p++ = (char)(0xf0 | (ch >> 18));
  410. *p++ = (char)(0x80 | ((ch >> 12) & 0x3f));
  411. *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
  412. *p++ = (char)(0x80 | (ch & 0x3f));
  413. }
  414. #endif /* STRINGLIB_SIZEOF_CHAR > 2 */
  415. #endif /* STRINGLIB_SIZEOF_CHAR > 1 */
  416. }
  417. #if STRINGLIB_SIZEOF_CHAR > 1
  418. Py_XDECREF(error_handler_obj);
  419. Py_XDECREF(exc);
  420. #endif
  421. return _PyBytesWriter_Finish(&writer, p);
  422. #if STRINGLIB_SIZEOF_CHAR > 1
  423. error:
  424. Py_XDECREF(rep);
  425. Py_XDECREF(error_handler_obj);
  426. Py_XDECREF(exc);
  427. _PyBytesWriter_Dealloc(&writer);
  428. return NULL;
  429. #endif
  430. #undef MAX_SHORT_UNICHARS
  431. }
  432. /* The pattern for constructing UCS2-repeated masks. */
  433. #if SIZEOF_LONG == 8
  434. # define UCS2_REPEAT_MASK 0x0001000100010001ul
  435. #elif SIZEOF_LONG == 4
  436. # define UCS2_REPEAT_MASK 0x00010001ul
  437. #else
  438. # error C 'long' size should be either 4 or 8!
  439. #endif
  440. /* The mask for fast checking. */
  441. #if STRINGLIB_SIZEOF_CHAR == 1
  442. /* The mask for fast checking of whether a C 'long' contains a
  443. non-ASCII or non-Latin1 UTF16-encoded characters. */
  444. # define FAST_CHAR_MASK (UCS2_REPEAT_MASK * (0xFFFFu & ~STRINGLIB_MAX_CHAR))
  445. #else
  446. /* The mask for fast checking of whether a C 'long' may contain
  447. UTF16-encoded surrogate characters. This is an efficient heuristic,
  448. assuming that non-surrogate characters with a code point >= 0x8000 are
  449. rare in most input.
  450. */
  451. # define FAST_CHAR_MASK (UCS2_REPEAT_MASK * 0x8000u)
  452. #endif
  453. /* The mask for fast byte-swapping. */
  454. #define STRIPPED_MASK (UCS2_REPEAT_MASK * 0x00FFu)
  455. /* Swap bytes. */
  456. #define SWAB(value) ((((value) >> 8) & STRIPPED_MASK) | \
  457. (((value) & STRIPPED_MASK) << 8))
  458. Py_LOCAL_INLINE(Py_UCS4)
  459. STRINGLIB(utf16_decode)(const unsigned char **inptr, const unsigned char *e,
  460. STRINGLIB_CHAR *dest, Py_ssize_t *outpos,
  461. int native_ordering)
  462. {
  463. Py_UCS4 ch;
  464. const unsigned char *aligned_end =
  465. (const unsigned char *) _Py_ALIGN_DOWN(e, SIZEOF_LONG);
  466. const unsigned char *q = *inptr;
  467. STRINGLIB_CHAR *p = dest + *outpos;
  468. /* Offsets from q for retrieving byte pairs in the right order. */
  469. #if PY_LITTLE_ENDIAN
  470. int ihi = !!native_ordering, ilo = !native_ordering;
  471. #else
  472. int ihi = !native_ordering, ilo = !!native_ordering;
  473. #endif
  474. --e;
  475. while (q < e) {
  476. Py_UCS4 ch2;
  477. /* First check for possible aligned read of a C 'long'. Unaligned
  478. reads are more expensive, better to defer to another iteration. */
  479. if (_Py_IS_ALIGNED(q, SIZEOF_LONG)) {
  480. /* Fast path for runs of in-range non-surrogate chars. */
  481. const unsigned char *_q = q;
  482. while (_q < aligned_end) {
  483. unsigned long block = * (unsigned long *) _q;
  484. if (native_ordering) {
  485. /* Can use buffer directly */
  486. if (block & FAST_CHAR_MASK)
  487. break;
  488. }
  489. else {
  490. /* Need to byte-swap */
  491. if (block & SWAB(FAST_CHAR_MASK))
  492. break;
  493. #if STRINGLIB_SIZEOF_CHAR == 1
  494. block >>= 8;
  495. #else
  496. block = SWAB(block);
  497. #endif
  498. }
  499. #if PY_LITTLE_ENDIAN
  500. # if SIZEOF_LONG == 4
  501. p[0] = (STRINGLIB_CHAR)(block & 0xFFFFu);
  502. p[1] = (STRINGLIB_CHAR)(block >> 16);
  503. # elif SIZEOF_LONG == 8
  504. p[0] = (STRINGLIB_CHAR)(block & 0xFFFFu);
  505. p[1] = (STRINGLIB_CHAR)((block >> 16) & 0xFFFFu);
  506. p[2] = (STRINGLIB_CHAR)((block >> 32) & 0xFFFFu);
  507. p[3] = (STRINGLIB_CHAR)(block >> 48);
  508. # endif
  509. #else
  510. # if SIZEOF_LONG == 4
  511. p[0] = (STRINGLIB_CHAR)(block >> 16);
  512. p[1] = (STRINGLIB_CHAR)(block & 0xFFFFu);
  513. # elif SIZEOF_LONG == 8
  514. p[0] = (STRINGLIB_CHAR)(block >> 48);
  515. p[1] = (STRINGLIB_CHAR)((block >> 32) & 0xFFFFu);
  516. p[2] = (STRINGLIB_CHAR)((block >> 16) & 0xFFFFu);
  517. p[3] = (STRINGLIB_CHAR)(block & 0xFFFFu);
  518. # endif
  519. #endif
  520. _q += SIZEOF_LONG;
  521. p += SIZEOF_LONG / 2;
  522. }
  523. q = _q;
  524. if (q >= e)
  525. break;
  526. }
  527. ch = (q[ihi] << 8) | q[ilo];
  528. q += 2;
  529. if (!Py_UNICODE_IS_SURROGATE(ch)) {
  530. #if STRINGLIB_SIZEOF_CHAR < 2
  531. if (ch > STRINGLIB_MAX_CHAR)
  532. /* Out-of-range */
  533. goto Return;
  534. #endif
  535. *p++ = (STRINGLIB_CHAR)ch;
  536. continue;
  537. }
  538. /* UTF-16 code pair: */
  539. if (q >= e)
  540. goto UnexpectedEnd;
  541. if (!Py_UNICODE_IS_HIGH_SURROGATE(ch))
  542. goto IllegalEncoding;
  543. ch2 = (q[ihi] << 8) | q[ilo];
  544. q += 2;
  545. if (!Py_UNICODE_IS_LOW_SURROGATE(ch2))
  546. goto IllegalSurrogate;
  547. ch = Py_UNICODE_JOIN_SURROGATES(ch, ch2);
  548. #if STRINGLIB_SIZEOF_CHAR < 4
  549. /* Out-of-range */
  550. goto Return;
  551. #else
  552. *p++ = (STRINGLIB_CHAR)ch;
  553. #endif
  554. }
  555. ch = 0;
  556. Return:
  557. *inptr = q;
  558. *outpos = p - dest;
  559. return ch;
  560. UnexpectedEnd:
  561. ch = 1;
  562. goto Return;
  563. IllegalEncoding:
  564. ch = 2;
  565. goto Return;
  566. IllegalSurrogate:
  567. ch = 3;
  568. goto Return;
  569. }
  570. #undef UCS2_REPEAT_MASK
  571. #undef FAST_CHAR_MASK
  572. #undef STRIPPED_MASK
  573. #undef SWAB
  574. #if STRINGLIB_MAX_CHAR >= 0x80
  575. Py_LOCAL_INLINE(Py_ssize_t)
  576. STRINGLIB(utf16_encode)(const STRINGLIB_CHAR *in,
  577. Py_ssize_t len,
  578. unsigned short **outptr,
  579. int native_ordering)
  580. {
  581. unsigned short *out = *outptr;
  582. const STRINGLIB_CHAR *end = in + len;
  583. #if STRINGLIB_SIZEOF_CHAR == 1
  584. if (native_ordering) {
  585. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  586. while (in < unrolled_end) {
  587. out[0] = in[0];
  588. out[1] = in[1];
  589. out[2] = in[2];
  590. out[3] = in[3];
  591. in += 4; out += 4;
  592. }
  593. while (in < end) {
  594. *out++ = *in++;
  595. }
  596. } else {
  597. # define SWAB2(CH) ((CH) << 8) /* high byte is zero */
  598. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  599. while (in < unrolled_end) {
  600. out[0] = SWAB2(in[0]);
  601. out[1] = SWAB2(in[1]);
  602. out[2] = SWAB2(in[2]);
  603. out[3] = SWAB2(in[3]);
  604. in += 4; out += 4;
  605. }
  606. while (in < end) {
  607. Py_UCS4 ch = *in++;
  608. *out++ = SWAB2((Py_UCS2)ch);
  609. }
  610. #undef SWAB2
  611. }
  612. *outptr = out;
  613. return len;
  614. #else
  615. if (native_ordering) {
  616. #if STRINGLIB_MAX_CHAR < 0x10000
  617. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  618. while (in < unrolled_end) {
  619. /* check if any character is a surrogate character */
  620. if (((in[0] ^ 0xd800) &
  621. (in[1] ^ 0xd800) &
  622. (in[2] ^ 0xd800) &
  623. (in[3] ^ 0xd800) & 0xf800) == 0)
  624. break;
  625. out[0] = in[0];
  626. out[1] = in[1];
  627. out[2] = in[2];
  628. out[3] = in[3];
  629. in += 4; out += 4;
  630. }
  631. #endif
  632. while (in < end) {
  633. Py_UCS4 ch;
  634. ch = *in++;
  635. if (ch < 0xd800)
  636. *out++ = ch;
  637. else if (ch < 0xe000)
  638. /* reject surrogate characters (U+D800-U+DFFF) */
  639. goto fail;
  640. #if STRINGLIB_MAX_CHAR >= 0x10000
  641. else if (ch >= 0x10000) {
  642. out[0] = Py_UNICODE_HIGH_SURROGATE(ch);
  643. out[1] = Py_UNICODE_LOW_SURROGATE(ch);
  644. out += 2;
  645. }
  646. #endif
  647. else
  648. *out++ = ch;
  649. }
  650. } else {
  651. #define SWAB2(CH) (((CH) << 8) | ((CH) >> 8))
  652. #if STRINGLIB_MAX_CHAR < 0x10000
  653. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  654. while (in < unrolled_end) {
  655. /* check if any character is a surrogate character */
  656. if (((in[0] ^ 0xd800) &
  657. (in[1] ^ 0xd800) &
  658. (in[2] ^ 0xd800) &
  659. (in[3] ^ 0xd800) & 0xf800) == 0)
  660. break;
  661. out[0] = SWAB2(in[0]);
  662. out[1] = SWAB2(in[1]);
  663. out[2] = SWAB2(in[2]);
  664. out[3] = SWAB2(in[3]);
  665. in += 4; out += 4;
  666. }
  667. #endif
  668. while (in < end) {
  669. Py_UCS4 ch = *in++;
  670. if (ch < 0xd800)
  671. *out++ = SWAB2((Py_UCS2)ch);
  672. else if (ch < 0xe000)
  673. /* reject surrogate characters (U+D800-U+DFFF) */
  674. goto fail;
  675. #if STRINGLIB_MAX_CHAR >= 0x10000
  676. else if (ch >= 0x10000) {
  677. Py_UCS2 ch1 = Py_UNICODE_HIGH_SURROGATE(ch);
  678. Py_UCS2 ch2 = Py_UNICODE_LOW_SURROGATE(ch);
  679. out[0] = SWAB2(ch1);
  680. out[1] = SWAB2(ch2);
  681. out += 2;
  682. }
  683. #endif
  684. else
  685. *out++ = SWAB2((Py_UCS2)ch);
  686. }
  687. #undef SWAB2
  688. }
  689. *outptr = out;
  690. return len;
  691. fail:
  692. *outptr = out;
  693. return len - (end - in + 1);
  694. #endif
  695. }
  696. #if STRINGLIB_SIZEOF_CHAR == 1
  697. # define SWAB4(CH, tmp) ((CH) << 24) /* high bytes are zero */
  698. #elif STRINGLIB_SIZEOF_CHAR == 2
  699. # define SWAB4(CH, tmp) (tmp = (CH), \
  700. ((tmp & 0x00FFu) << 24) + ((tmp & 0xFF00u) << 8))
  701. /* high bytes are zero */
  702. #else
  703. # define SWAB4(CH, tmp) (tmp = (CH), \
  704. tmp = ((tmp & 0x00FF00FFu) << 8) + ((tmp >> 8) & 0x00FF00FFu), \
  705. ((tmp & 0x0000FFFFu) << 16) + ((tmp >> 16) & 0x0000FFFFu))
  706. #endif
  707. Py_LOCAL_INLINE(Py_ssize_t)
  708. STRINGLIB(utf32_encode)(const STRINGLIB_CHAR *in,
  709. Py_ssize_t len,
  710. PY_UINT32_T **outptr,
  711. int native_ordering)
  712. {
  713. PY_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. Py_UCS4 ch1, ch2, ch3, ch4;
  748. /* check if any character is a surrogate character */
  749. if (((in[0] ^ 0xd800) &
  750. (in[1] ^ 0xd800) &
  751. (in[2] ^ 0xd800) &
  752. (in[3] ^ 0xd800) & 0xf800) == 0)
  753. break;
  754. #endif
  755. out[0] = SWAB4(in[0], ch1);
  756. out[1] = SWAB4(in[1], ch2);
  757. out[2] = SWAB4(in[2], ch3);
  758. out[3] = SWAB4(in[3], ch4);
  759. in += 4; out += 4;
  760. }
  761. while (in < end) {
  762. Py_UCS4 ch = *in++;
  763. #if STRINGLIB_SIZEOF_CHAR > 1
  764. if (Py_UNICODE_IS_SURROGATE(ch)) {
  765. /* reject surrogate characters (U+D800-U+DFFF) */
  766. goto fail;
  767. }
  768. #endif
  769. *out++ = SWAB4(ch, ch);
  770. }
  771. }
  772. *outptr = out;
  773. return len;
  774. #if STRINGLIB_SIZEOF_CHAR > 1
  775. fail:
  776. *outptr = out;
  777. return len - (end - in + 1);
  778. #endif
  779. }
  780. #undef SWAB4
  781. #endif