codecs.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 'size_t' contains a
  7. non-ASCII, UTF8-encoded char. */
  8. #if (SIZEOF_SIZE_T == 8)
  9. # define ASCII_CHAR_MASK 0x8080808080808080ULL
  10. #elif (SIZEOF_SIZE_T == 4)
  11. # define ASCII_CHAR_MASK 0x80808080U
  12. #else
  13. # error C 'size_t' 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. 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 'size_t' 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, ALIGNOF_SIZE_T)) {
  36. /* Help register allocation */
  37. const char *_s = s;
  38. STRINGLIB_CHAR *_p = p;
  39. while (_s + SIZEOF_SIZE_T <= end) {
  40. /* Read a whole size_t at a time (either 4 or 8 bytes),
  41. and do a fast unrolled copy if it only contains ASCII
  42. characters. */
  43. size_t value = *(const size_t *) _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_SIZE_T == 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_SIZE_T == 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_SIZE_T;
  75. _p += SIZEOF_SIZE_T;
  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. if (newpos < startpos) {
  364. writer->overallocate = 1;
  365. p = _PyBytesWriter_Prepare(writer, p,
  366. max_char_size * (startpos - newpos));
  367. if (p == NULL)
  368. goto error;
  369. }
  370. else {
  371. /* subtract preallocated bytes */
  372. writer->min_size -= max_char_size * (newpos - startpos);
  373. /* Only overallocate the buffer if it's not the last write */
  374. writer->overallocate = (newpos < size);
  375. }
  376. if (PyBytes_Check(rep)) {
  377. p = _PyBytesWriter_WriteBytes(writer, p,
  378. PyBytes_AS_STRING(rep),
  379. PyBytes_GET_SIZE(rep));
  380. }
  381. else {
  382. /* rep is unicode */
  383. if (PyUnicode_READY(rep) < 0)
  384. goto error;
  385. if (!PyUnicode_IS_ASCII(rep)) {
  386. raise_encode_exception(&exc, "utf-8", unicode,
  387. startpos, endpos,
  388. "surrogates not allowed");
  389. goto error;
  390. }
  391. p = _PyBytesWriter_WriteBytes(writer, p,
  392. PyUnicode_DATA(rep),
  393. PyUnicode_GET_LENGTH(rep));
  394. }
  395. if (p == NULL)
  396. goto error;
  397. Py_CLEAR(rep);
  398. i = newpos;
  399. }
  400. /* If overallocation was disabled, ensure that it was the last
  401. write. Otherwise, we missed an optimization */
  402. assert(writer->overallocate || i == size);
  403. }
  404. else
  405. #if STRINGLIB_SIZEOF_CHAR > 2
  406. if (ch < 0x10000)
  407. #endif
  408. {
  409. *p++ = (char)(0xe0 | (ch >> 12));
  410. *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
  411. *p++ = (char)(0x80 | (ch & 0x3f));
  412. }
  413. #if STRINGLIB_SIZEOF_CHAR > 2
  414. else /* ch >= 0x10000 */
  415. {
  416. assert(ch <= MAX_UNICODE);
  417. /* Encode UCS4 Unicode ordinals */
  418. *p++ = (char)(0xf0 | (ch >> 18));
  419. *p++ = (char)(0x80 | ((ch >> 12) & 0x3f));
  420. *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
  421. *p++ = (char)(0x80 | (ch & 0x3f));
  422. }
  423. #endif /* STRINGLIB_SIZEOF_CHAR > 2 */
  424. #endif /* STRINGLIB_SIZEOF_CHAR > 1 */
  425. }
  426. #if STRINGLIB_SIZEOF_CHAR > 1
  427. Py_XDECREF(error_handler_obj);
  428. Py_XDECREF(exc);
  429. #endif
  430. return p;
  431. #if STRINGLIB_SIZEOF_CHAR > 1
  432. error:
  433. Py_XDECREF(rep);
  434. Py_XDECREF(error_handler_obj);
  435. Py_XDECREF(exc);
  436. return NULL;
  437. #endif
  438. }
  439. /* The pattern for constructing UCS2-repeated masks. */
  440. #if SIZEOF_LONG == 8
  441. # define UCS2_REPEAT_MASK 0x0001000100010001ul
  442. #elif SIZEOF_LONG == 4
  443. # define UCS2_REPEAT_MASK 0x00010001ul
  444. #else
  445. # error C 'long' size should be either 4 or 8!
  446. #endif
  447. /* The mask for fast checking. */
  448. #if STRINGLIB_SIZEOF_CHAR == 1
  449. /* The mask for fast checking of whether a C 'long' contains a
  450. non-ASCII or non-Latin1 UTF16-encoded characters. */
  451. # define FAST_CHAR_MASK (UCS2_REPEAT_MASK * (0xFFFFu & ~STRINGLIB_MAX_CHAR))
  452. #else
  453. /* The mask for fast checking of whether a C 'long' may contain
  454. UTF16-encoded surrogate characters. This is an efficient heuristic,
  455. assuming that non-surrogate characters with a code point >= 0x8000 are
  456. rare in most input.
  457. */
  458. # define FAST_CHAR_MASK (UCS2_REPEAT_MASK * 0x8000u)
  459. #endif
  460. /* The mask for fast byte-swapping. */
  461. #define STRIPPED_MASK (UCS2_REPEAT_MASK * 0x00FFu)
  462. /* Swap bytes. */
  463. #define SWAB(value) ((((value) >> 8) & STRIPPED_MASK) | \
  464. (((value) & STRIPPED_MASK) << 8))
  465. Py_LOCAL_INLINE(Py_UCS4)
  466. STRINGLIB(utf16_decode)(const unsigned char **inptr, const unsigned char *e,
  467. STRINGLIB_CHAR *dest, Py_ssize_t *outpos,
  468. int native_ordering)
  469. {
  470. Py_UCS4 ch;
  471. const unsigned char *q = *inptr;
  472. STRINGLIB_CHAR *p = dest + *outpos;
  473. /* Offsets from q for retrieving byte pairs in the right order. */
  474. #if PY_LITTLE_ENDIAN
  475. int ihi = !!native_ordering, ilo = !native_ordering;
  476. #else
  477. int ihi = !native_ordering, ilo = !!native_ordering;
  478. #endif
  479. --e;
  480. while (q < e) {
  481. Py_UCS4 ch2;
  482. /* First check for possible aligned read of a C 'long'. Unaligned
  483. reads are more expensive, better to defer to another iteration. */
  484. if (_Py_IS_ALIGNED(q, ALIGNOF_LONG)) {
  485. /* Fast path for runs of in-range non-surrogate chars. */
  486. const unsigned char *_q = q;
  487. while (_q + SIZEOF_LONG <= e) {
  488. unsigned long block = * (const unsigned long *) _q;
  489. if (native_ordering) {
  490. /* Can use buffer directly */
  491. if (block & FAST_CHAR_MASK)
  492. break;
  493. }
  494. else {
  495. /* Need to byte-swap */
  496. if (block & SWAB(FAST_CHAR_MASK))
  497. break;
  498. #if STRINGLIB_SIZEOF_CHAR == 1
  499. block >>= 8;
  500. #else
  501. block = SWAB(block);
  502. #endif
  503. }
  504. #if PY_LITTLE_ENDIAN
  505. # if SIZEOF_LONG == 4
  506. p[0] = (STRINGLIB_CHAR)(block & 0xFFFFu);
  507. p[1] = (STRINGLIB_CHAR)(block >> 16);
  508. # elif SIZEOF_LONG == 8
  509. p[0] = (STRINGLIB_CHAR)(block & 0xFFFFu);
  510. p[1] = (STRINGLIB_CHAR)((block >> 16) & 0xFFFFu);
  511. p[2] = (STRINGLIB_CHAR)((block >> 32) & 0xFFFFu);
  512. p[3] = (STRINGLIB_CHAR)(block >> 48);
  513. # endif
  514. #else
  515. # if SIZEOF_LONG == 4
  516. p[0] = (STRINGLIB_CHAR)(block >> 16);
  517. p[1] = (STRINGLIB_CHAR)(block & 0xFFFFu);
  518. # elif SIZEOF_LONG == 8
  519. p[0] = (STRINGLIB_CHAR)(block >> 48);
  520. p[1] = (STRINGLIB_CHAR)((block >> 32) & 0xFFFFu);
  521. p[2] = (STRINGLIB_CHAR)((block >> 16) & 0xFFFFu);
  522. p[3] = (STRINGLIB_CHAR)(block & 0xFFFFu);
  523. # endif
  524. #endif
  525. _q += SIZEOF_LONG;
  526. p += SIZEOF_LONG / 2;
  527. }
  528. q = _q;
  529. if (q >= e)
  530. break;
  531. }
  532. ch = (q[ihi] << 8) | q[ilo];
  533. q += 2;
  534. if (!Py_UNICODE_IS_SURROGATE(ch)) {
  535. #if STRINGLIB_SIZEOF_CHAR < 2
  536. if (ch > STRINGLIB_MAX_CHAR)
  537. /* Out-of-range */
  538. goto Return;
  539. #endif
  540. *p++ = (STRINGLIB_CHAR)ch;
  541. continue;
  542. }
  543. /* UTF-16 code pair: */
  544. if (!Py_UNICODE_IS_HIGH_SURROGATE(ch))
  545. goto IllegalEncoding;
  546. if (q >= e)
  547. goto UnexpectedEnd;
  548. ch2 = (q[ihi] << 8) | q[ilo];
  549. q += 2;
  550. if (!Py_UNICODE_IS_LOW_SURROGATE(ch2))
  551. goto IllegalSurrogate;
  552. ch = Py_UNICODE_JOIN_SURROGATES(ch, ch2);
  553. #if STRINGLIB_SIZEOF_CHAR < 4
  554. /* Out-of-range */
  555. goto Return;
  556. #else
  557. *p++ = (STRINGLIB_CHAR)ch;
  558. #endif
  559. }
  560. ch = 0;
  561. Return:
  562. *inptr = q;
  563. *outpos = p - dest;
  564. return ch;
  565. UnexpectedEnd:
  566. ch = 1;
  567. goto Return;
  568. IllegalEncoding:
  569. ch = 2;
  570. goto Return;
  571. IllegalSurrogate:
  572. ch = 3;
  573. goto Return;
  574. }
  575. #undef UCS2_REPEAT_MASK
  576. #undef FAST_CHAR_MASK
  577. #undef STRIPPED_MASK
  578. #undef SWAB
  579. #if STRINGLIB_MAX_CHAR >= 0x80
  580. Py_LOCAL_INLINE(Py_ssize_t)
  581. STRINGLIB(utf16_encode)(const STRINGLIB_CHAR *in,
  582. Py_ssize_t len,
  583. unsigned short **outptr,
  584. int native_ordering)
  585. {
  586. unsigned short *out = *outptr;
  587. const STRINGLIB_CHAR *end = in + len;
  588. #if STRINGLIB_SIZEOF_CHAR == 1
  589. if (native_ordering) {
  590. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  591. while (in < unrolled_end) {
  592. out[0] = in[0];
  593. out[1] = in[1];
  594. out[2] = in[2];
  595. out[3] = in[3];
  596. in += 4; out += 4;
  597. }
  598. while (in < end) {
  599. *out++ = *in++;
  600. }
  601. } else {
  602. # define SWAB2(CH) ((CH) << 8) /* high byte is zero */
  603. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  604. while (in < unrolled_end) {
  605. out[0] = SWAB2(in[0]);
  606. out[1] = SWAB2(in[1]);
  607. out[2] = SWAB2(in[2]);
  608. out[3] = SWAB2(in[3]);
  609. in += 4; out += 4;
  610. }
  611. while (in < end) {
  612. Py_UCS4 ch = *in++;
  613. *out++ = SWAB2((Py_UCS2)ch);
  614. }
  615. #undef SWAB2
  616. }
  617. *outptr = out;
  618. return len;
  619. #else
  620. if (native_ordering) {
  621. #if STRINGLIB_MAX_CHAR < 0x10000
  622. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  623. while (in < unrolled_end) {
  624. /* check if any character is a surrogate character */
  625. if (((in[0] ^ 0xd800) &
  626. (in[1] ^ 0xd800) &
  627. (in[2] ^ 0xd800) &
  628. (in[3] ^ 0xd800) & 0xf800) == 0)
  629. break;
  630. out[0] = in[0];
  631. out[1] = in[1];
  632. out[2] = in[2];
  633. out[3] = in[3];
  634. in += 4; out += 4;
  635. }
  636. #endif
  637. while (in < end) {
  638. Py_UCS4 ch;
  639. ch = *in++;
  640. if (ch < 0xd800)
  641. *out++ = ch;
  642. else if (ch < 0xe000)
  643. /* reject surrogate characters (U+D800-U+DFFF) */
  644. goto fail;
  645. #if STRINGLIB_MAX_CHAR >= 0x10000
  646. else if (ch >= 0x10000) {
  647. out[0] = Py_UNICODE_HIGH_SURROGATE(ch);
  648. out[1] = Py_UNICODE_LOW_SURROGATE(ch);
  649. out += 2;
  650. }
  651. #endif
  652. else
  653. *out++ = ch;
  654. }
  655. } else {
  656. #define SWAB2(CH) (((CH) << 8) | ((CH) >> 8))
  657. #if STRINGLIB_MAX_CHAR < 0x10000
  658. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  659. while (in < unrolled_end) {
  660. /* check if any character is a surrogate character */
  661. if (((in[0] ^ 0xd800) &
  662. (in[1] ^ 0xd800) &
  663. (in[2] ^ 0xd800) &
  664. (in[3] ^ 0xd800) & 0xf800) == 0)
  665. break;
  666. out[0] = SWAB2(in[0]);
  667. out[1] = SWAB2(in[1]);
  668. out[2] = SWAB2(in[2]);
  669. out[3] = SWAB2(in[3]);
  670. in += 4; out += 4;
  671. }
  672. #endif
  673. while (in < end) {
  674. Py_UCS4 ch = *in++;
  675. if (ch < 0xd800)
  676. *out++ = SWAB2((Py_UCS2)ch);
  677. else if (ch < 0xe000)
  678. /* reject surrogate characters (U+D800-U+DFFF) */
  679. goto fail;
  680. #if STRINGLIB_MAX_CHAR >= 0x10000
  681. else if (ch >= 0x10000) {
  682. Py_UCS2 ch1 = Py_UNICODE_HIGH_SURROGATE(ch);
  683. Py_UCS2 ch2 = Py_UNICODE_LOW_SURROGATE(ch);
  684. out[0] = SWAB2(ch1);
  685. out[1] = SWAB2(ch2);
  686. out += 2;
  687. }
  688. #endif
  689. else
  690. *out++ = SWAB2((Py_UCS2)ch);
  691. }
  692. #undef SWAB2
  693. }
  694. *outptr = out;
  695. return len;
  696. fail:
  697. *outptr = out;
  698. return len - (end - in + 1);
  699. #endif
  700. }
  701. static inline uint32_t
  702. STRINGLIB(SWAB4)(STRINGLIB_CHAR ch)
  703. {
  704. uint32_t word = ch;
  705. #if STRINGLIB_SIZEOF_CHAR == 1
  706. /* high bytes are zero */
  707. return (word << 24);
  708. #elif STRINGLIB_SIZEOF_CHAR == 2
  709. /* high bytes are zero */
  710. return ((word & 0x00FFu) << 24) | ((word & 0xFF00u) << 8);
  711. #else
  712. return _Py_bswap32(word);
  713. #endif
  714. }
  715. Py_LOCAL_INLINE(Py_ssize_t)
  716. STRINGLIB(utf32_encode)(const STRINGLIB_CHAR *in,
  717. Py_ssize_t len,
  718. uint32_t **outptr,
  719. int native_ordering)
  720. {
  721. uint32_t *out = *outptr;
  722. const STRINGLIB_CHAR *end = in + len;
  723. if (native_ordering) {
  724. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  725. while (in < unrolled_end) {
  726. #if STRINGLIB_SIZEOF_CHAR > 1
  727. /* check if any character is a surrogate character */
  728. if (((in[0] ^ 0xd800) &
  729. (in[1] ^ 0xd800) &
  730. (in[2] ^ 0xd800) &
  731. (in[3] ^ 0xd800) & 0xf800) == 0)
  732. break;
  733. #endif
  734. out[0] = in[0];
  735. out[1] = in[1];
  736. out[2] = in[2];
  737. out[3] = in[3];
  738. in += 4; out += 4;
  739. }
  740. while (in < end) {
  741. Py_UCS4 ch;
  742. ch = *in++;
  743. #if STRINGLIB_SIZEOF_CHAR > 1
  744. if (Py_UNICODE_IS_SURROGATE(ch)) {
  745. /* reject surrogate characters (U+D800-U+DFFF) */
  746. goto fail;
  747. }
  748. #endif
  749. *out++ = ch;
  750. }
  751. } else {
  752. const STRINGLIB_CHAR *unrolled_end = in + _Py_SIZE_ROUND_DOWN(len, 4);
  753. while (in < unrolled_end) {
  754. #if STRINGLIB_SIZEOF_CHAR > 1
  755. /* check if any character is a surrogate character */
  756. if (((in[0] ^ 0xd800) &
  757. (in[1] ^ 0xd800) &
  758. (in[2] ^ 0xd800) &
  759. (in[3] ^ 0xd800) & 0xf800) == 0)
  760. break;
  761. #endif
  762. out[0] = STRINGLIB(SWAB4)(in[0]);
  763. out[1] = STRINGLIB(SWAB4)(in[1]);
  764. out[2] = STRINGLIB(SWAB4)(in[2]);
  765. out[3] = STRINGLIB(SWAB4)(in[3]);
  766. in += 4; out += 4;
  767. }
  768. while (in < end) {
  769. Py_UCS4 ch = *in++;
  770. #if STRINGLIB_SIZEOF_CHAR > 1
  771. if (Py_UNICODE_IS_SURROGATE(ch)) {
  772. /* reject surrogate characters (U+D800-U+DFFF) */
  773. goto fail;
  774. }
  775. #endif
  776. *out++ = STRINGLIB(SWAB4)(ch);
  777. }
  778. }
  779. *outptr = out;
  780. return len;
  781. #if STRINGLIB_SIZEOF_CHAR > 1
  782. fail:
  783. *outptr = out;
  784. return len - (end - in + 1);
  785. #endif
  786. }
  787. #endif