PageRenderTime 73ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/erts/emulator/beam/erl_unicode.c

https://github.com/Bwooce/otp
C | 2633 lines | 2232 code | 194 blank | 207 comment | 642 complexity | 4e37128d66f367a7e9201c2dcb98a781 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-2-Clause

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * %CopyrightBegin%
  3. *
  4. * Copyright Ericsson AB 2008-2010. All Rights Reserved.
  5. *
  6. * The contents of this file are subject to the Erlang Public License,
  7. * Version 1.1, (the "License"); you may not use this file except in
  8. * compliance with the License. You should have received a copy of the
  9. * Erlang Public License along with this software. If not, it can be
  10. * retrieved online at http://www.erlang.org/.
  11. *
  12. * Software distributed under the License is distributed on an "AS IS"
  13. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  14. * the License for the specific language governing rights and limitations
  15. * under the License.
  16. *
  17. * %CopyrightEnd%
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #include "sys.h"
  23. #include "erl_vm.h"
  24. #include "global.h"
  25. #include "erl_process.h"
  26. #include "error.h"
  27. #include "bif.h"
  28. #include "erl_binary.h"
  29. #include "big.h"
  30. #include "erl_unicode.h"
  31. #include "erl_unicode_normalize.h"
  32. typedef struct _restart_context {
  33. byte *bytes;
  34. Uint num_processed_bytes;
  35. Uint num_bytes_to_process;
  36. Uint num_resulting_chars;
  37. int state;
  38. } RestartContext;
  39. #define LOOP_FACTOR 10
  40. #define LOOP_FACTOR_SIMPLE 50 /* When just counting */
  41. static Uint max_loop_limit;
  42. static BIF_RETTYPE utf8_to_list(BIF_ALIST_1);
  43. static BIF_RETTYPE finalize_list_to_list(Process *p,
  44. byte *bytes,
  45. Eterm rest,
  46. Uint num_processed_bytes,
  47. Uint num_bytes_to_process,
  48. Uint num_resulting_chars,
  49. int state, int left,
  50. Eterm tail);
  51. static BIF_RETTYPE characters_to_utf8_trap(BIF_ALIST_3);
  52. static BIF_RETTYPE characters_to_list_trap_1(BIF_ALIST_3);
  53. static BIF_RETTYPE characters_to_list_trap_2(BIF_ALIST_3);
  54. static BIF_RETTYPE characters_to_list_trap_3(BIF_ALIST_3);
  55. static BIF_RETTYPE characters_to_list_trap_4(BIF_ALIST_1);
  56. static Export characters_to_utf8_trap_exp;
  57. static Export characters_to_list_trap_1_exp;
  58. static Export characters_to_list_trap_2_exp;
  59. static Export characters_to_list_trap_3_exp;
  60. static Export characters_to_list_trap_4_exp;
  61. static Export *c_to_b_int_trap_exportp = NULL;
  62. static Export *c_to_l_int_trap_exportp = NULL;
  63. void erts_init_unicode(void)
  64. {
  65. max_loop_limit = CONTEXT_REDS * LOOP_FACTOR;
  66. /* Non visual BIFs to trap to. */
  67. memset(&characters_to_utf8_trap_exp, 0, sizeof(Export));
  68. characters_to_utf8_trap_exp.address =
  69. &characters_to_utf8_trap_exp.code[3];
  70. characters_to_utf8_trap_exp.code[0] = am_erlang;
  71. characters_to_utf8_trap_exp.code[1] =
  72. am_atom_put("characters_to_utf8_trap",23);
  73. characters_to_utf8_trap_exp.code[2] = 3;
  74. characters_to_utf8_trap_exp.code[3] =
  75. (BeamInstr) em_apply_bif;
  76. characters_to_utf8_trap_exp.code[4] =
  77. (BeamInstr) &characters_to_utf8_trap;
  78. memset(&characters_to_list_trap_1_exp, 0, sizeof(Export));
  79. characters_to_list_trap_1_exp.address =
  80. &characters_to_list_trap_1_exp.code[3];
  81. characters_to_list_trap_1_exp.code[0] = am_erlang;
  82. characters_to_list_trap_1_exp.code[1] =
  83. am_atom_put("characters_to_list_trap_1",25);
  84. characters_to_list_trap_1_exp.code[2] = 3;
  85. characters_to_list_trap_1_exp.code[3] =
  86. (BeamInstr) em_apply_bif;
  87. characters_to_list_trap_1_exp.code[4] =
  88. (BeamInstr) &characters_to_list_trap_1;
  89. memset(&characters_to_list_trap_2_exp, 0, sizeof(Export));
  90. characters_to_list_trap_2_exp.address =
  91. &characters_to_list_trap_2_exp.code[3];
  92. characters_to_list_trap_2_exp.code[0] = am_erlang;
  93. characters_to_list_trap_2_exp.code[1] =
  94. am_atom_put("characters_to_list_trap_2",25);
  95. characters_to_list_trap_2_exp.code[2] = 3;
  96. characters_to_list_trap_2_exp.code[3] =
  97. (BeamInstr) em_apply_bif;
  98. characters_to_list_trap_2_exp.code[4] =
  99. (BeamInstr) &characters_to_list_trap_2;
  100. memset(&characters_to_list_trap_3_exp, 0, sizeof(Export));
  101. characters_to_list_trap_3_exp.address =
  102. &characters_to_list_trap_3_exp.code[3];
  103. characters_to_list_trap_3_exp.code[0] = am_erlang;
  104. characters_to_list_trap_3_exp.code[1] =
  105. am_atom_put("characters_to_list_trap_3",25);
  106. characters_to_list_trap_3_exp.code[2] = 3;
  107. characters_to_list_trap_3_exp.code[3] =
  108. (BeamInstr) em_apply_bif;
  109. characters_to_list_trap_3_exp.code[4] =
  110. (BeamInstr) &characters_to_list_trap_3;
  111. memset(&characters_to_list_trap_4_exp, 0, sizeof(Export));
  112. characters_to_list_trap_4_exp.address =
  113. &characters_to_list_trap_4_exp.code[3];
  114. characters_to_list_trap_4_exp.code[0] = am_erlang;
  115. characters_to_list_trap_4_exp.code[1] =
  116. am_atom_put("characters_to_list_trap_4",25);
  117. characters_to_list_trap_4_exp.code[2] = 1;
  118. characters_to_list_trap_4_exp.code[3] =
  119. (BeamInstr) em_apply_bif;
  120. characters_to_list_trap_4_exp.code[4] =
  121. (BeamInstr) &characters_to_list_trap_4;
  122. c_to_b_int_trap_exportp = erts_export_put(am_unicode,am_characters_to_binary_int,2);
  123. c_to_l_int_trap_exportp = erts_export_put(am_unicode,am_characters_to_list_int,2);
  124. }
  125. static ERTS_INLINE void *alloc_restart(size_t size)
  126. {
  127. return erts_alloc(ERTS_ALC_T_UNICODE_BUFFER,size);
  128. }
  129. static ERTS_INLINE void free_restart(void *ptr)
  130. {
  131. erts_free(ERTS_ALC_T_UNICODE_BUFFER, ptr);
  132. }
  133. static void cleanup_restart_context(RestartContext *rc)
  134. {
  135. if (rc->bytes != NULL) {
  136. free_restart(rc->bytes);
  137. rc->bytes = NULL;
  138. }
  139. }
  140. static void cleanup_restart_context_bin(Binary *bp)
  141. {
  142. RestartContext *rc = ERTS_MAGIC_BIN_DATA(bp);
  143. cleanup_restart_context(rc);
  144. }
  145. static RestartContext *get_rc_from_bin(Eterm bin)
  146. {
  147. Binary *mbp;
  148. ASSERT(ERTS_TERM_IS_MAGIC_BINARY(bin));
  149. mbp = ((ProcBin *) binary_val(bin))->val;
  150. ASSERT(ERTS_MAGIC_BIN_DESTRUCTOR(mbp)
  151. == cleanup_restart_context_bin);
  152. return (RestartContext *) ERTS_MAGIC_BIN_DATA(mbp);
  153. }
  154. static Eterm make_magic_bin_for_restart(Process *p, RestartContext *rc)
  155. {
  156. Binary *mbp = erts_create_magic_binary(sizeof(RestartContext),
  157. cleanup_restart_context_bin);
  158. RestartContext *restartp = ERTS_MAGIC_BIN_DATA(mbp);
  159. Eterm *hp;
  160. memcpy(restartp,rc,sizeof(RestartContext));
  161. hp = HAlloc(p, PROC_BIN_SIZE);
  162. return erts_mk_magic_binary_term(&hp, &MSO(p), mbp);
  163. }
  164. Sint erts_unicode_set_loop_limit(Sint limit)
  165. {
  166. Sint save = (Sint) max_loop_limit;
  167. if (limit <= 0) {
  168. max_loop_limit = CONTEXT_REDS * LOOP_FACTOR;
  169. } else {
  170. max_loop_limit = (Uint) limit;
  171. }
  172. return save;
  173. }
  174. static ERTS_INLINE int allowed_iterations(Process *p)
  175. {
  176. int tmp = ERTS_BIF_REDS_LEFT(p) * LOOP_FACTOR;
  177. int tmp2 = max_loop_limit;
  178. if (tmp2 < tmp)
  179. return tmp2;
  180. else
  181. return tmp;
  182. }
  183. static ERTS_INLINE int cost_to_proc(Process *p, int cost)
  184. {
  185. int x = (cost / LOOP_FACTOR);
  186. BUMP_REDS(p,x);
  187. return x;
  188. }
  189. static ERTS_INLINE int simple_loops_to_common(int cost)
  190. {
  191. int factor = (LOOP_FACTOR_SIMPLE / LOOP_FACTOR);
  192. return (cost / factor);
  193. }
  194. static Sint aligned_binary_size(Eterm binary)
  195. {
  196. unsigned char *bytes;
  197. Uint bitoffs;
  198. Uint bitsize;
  199. ERTS_GET_BINARY_BYTES(binary, bytes, bitoffs, bitsize);
  200. if (bitsize != 0) {
  201. return (Sint) -1;
  202. }
  203. return binary_size(binary);
  204. }
  205. static Sint latin1_binary_need(Eterm binary)
  206. {
  207. unsigned char *bytes;
  208. byte *temp_alloc = NULL;
  209. Uint bitoffs;
  210. Uint bitsize;
  211. Uint size;
  212. Sint need = 0;
  213. Sint i;
  214. ERTS_GET_BINARY_BYTES(binary, bytes, bitoffs, bitsize);
  215. if (bitsize != 0) {
  216. return (Sint) -1;
  217. }
  218. if (bitoffs != 0) {
  219. bytes = erts_get_aligned_binary_bytes(binary, &temp_alloc);
  220. /* The call to erts_get_aligned_binary_bytes cannot fail as
  221. we'we already checked bitsize and that this is a binary */
  222. }
  223. size = binary_size(binary);
  224. for(i = 0; i < size; ++i) {
  225. if (bytes[i] & ((byte) 0x80)) {
  226. need += 2;
  227. } else {
  228. need += 1;
  229. }
  230. }
  231. erts_free_aligned_binary_bytes(temp_alloc);
  232. return need;
  233. }
  234. static int utf8_len(byte first)
  235. {
  236. if ((first & ((byte) 0x80)) == 0) {
  237. return 1;
  238. } else if ((first & ((byte) 0xE0)) == 0xC0) {
  239. return 2;
  240. } else if ((first & ((byte) 0xF0)) == 0xE0) {
  241. return 3;
  242. } else if ((first & ((byte) 0xF8)) == 0xF0) {
  243. return 4;
  244. }
  245. return -1;
  246. }
  247. static int copy_utf8_bin(byte *target, byte *source, Uint size,
  248. byte *leftover, int *num_leftovers,
  249. byte **err_pos, Uint *characters) {
  250. int copied = 0;
  251. if (leftover != NULL && *num_leftovers) {
  252. int need = utf8_len(leftover[0]);
  253. int from_source = need - (*num_leftovers);
  254. int c;
  255. byte *tmp_err_pos = NULL;
  256. ASSERT(need > 0);
  257. ASSERT(from_source > 0);
  258. if (size < from_source) {
  259. memcpy(leftover + (*num_leftovers), source, size);
  260. *num_leftovers += size;
  261. return 0;
  262. }
  263. /* leftover has room for four bytes (see bif) */
  264. memcpy(leftover + (*num_leftovers),source,from_source);
  265. c = copy_utf8_bin(target, leftover, need, NULL, NULL, &tmp_err_pos, characters);
  266. if (tmp_err_pos != 0) {
  267. *err_pos = source;
  268. return 0;
  269. }
  270. copied += c;
  271. *num_leftovers = 0;
  272. size -= from_source;
  273. target += c;
  274. source += from_source;
  275. }
  276. while (size) {
  277. if (((*source) & ((byte) 0x80)) == 0) {
  278. *(target++) = *(source++);
  279. --size; ++copied;
  280. } else if (((*source) & ((byte) 0xE0)) == 0xC0) {
  281. if (leftover && size < 2) {
  282. *leftover = *source;
  283. *num_leftovers = 1;
  284. break;
  285. }
  286. if (size < 2 || ((source[1] & ((byte) 0xC0)) != 0x80) ||
  287. ((*source) < 0xC2) /* overlong */) {
  288. *err_pos = source;
  289. return copied;
  290. }
  291. *(target++) = *(source++);
  292. *(target++) = *(source++);
  293. size -= 2; copied += 2;
  294. } else if (((*source) & ((byte) 0xF0)) == 0xE0) {
  295. if (leftover && size < 3) {
  296. memcpy(leftover, source, (int) size);
  297. *num_leftovers = (int) size;
  298. break;
  299. }
  300. if (size < 3 || ((source[1] & ((byte) 0xC0)) != 0x80) ||
  301. ((source[2] & ((byte) 0xC0)) != 0x80) ||
  302. (((*source) == 0xE0) && (source[1] < 0xA0)) /* overlong */ ) {
  303. *err_pos = source;
  304. return copied;
  305. }
  306. if ((((*source) & ((byte) 0xF)) == 0xD) &&
  307. ((source[1] & 0x20) != 0)) {
  308. *err_pos = source;
  309. return copied;
  310. }
  311. if (((*source) == 0xEF) && (source[1] == 0xBF) &&
  312. ((source[2] == 0xBE) || (source[2] == 0xBF))) {
  313. *err_pos = source;
  314. return copied;
  315. }
  316. *(target++) = *(source++);
  317. *(target++) = *(source++);
  318. *(target++) = *(source++);
  319. size -= 3; copied += 3;
  320. } else if (((*source) & ((byte) 0xF8)) == 0xF0) {
  321. if (leftover && size < 4) {
  322. memcpy(leftover, source, (int) size);
  323. *num_leftovers = (int) size;
  324. break;
  325. }
  326. if (size < 4 || ((source[1] & ((byte) 0xC0)) != 0x80) ||
  327. ((source[2] & ((byte) 0xC0)) != 0x80) ||
  328. ((source[3] & ((byte) 0xC0)) != 0x80) ||
  329. (((*source) == 0xF0) && (source[1] < 0x90)) /* overlong */) {
  330. *err_pos = source;
  331. return copied;
  332. }
  333. if ((((*source) & ((byte)0x7)) > 0x4U) ||
  334. ((((*source) & ((byte)0x7)) == 0x4U) &&
  335. ((source[1] & ((byte)0x3F)) > 0xFU))) {
  336. *err_pos = source;
  337. return copied;
  338. }
  339. *(target++) = *(source++);
  340. *(target++) = *(source++);
  341. *(target++) = *(source++);
  342. *(target++) = *(source++);
  343. size -= 4; copied +=4;
  344. } else {
  345. *err_pos = source;
  346. return copied;
  347. }
  348. ++(*characters);
  349. }
  350. return copied;
  351. }
  352. static Sint utf8_need(Eterm ioterm, int latin1, Uint *costp)
  353. {
  354. Eterm *objp;
  355. Eterm obj;
  356. DECLARE_ESTACK(stack);
  357. Sint need = 0;
  358. Uint cost = 0;
  359. if (is_nil(ioterm)) {
  360. DESTROY_ESTACK(stack);
  361. *costp = 0;
  362. return need;
  363. }
  364. if(is_binary(ioterm)) {
  365. DESTROY_ESTACK(stack);
  366. if (latin1) {
  367. Sint x = latin1_binary_need(ioterm);
  368. *costp = x;
  369. return x;
  370. } else {
  371. *costp = 1;
  372. return aligned_binary_size(ioterm);
  373. }
  374. }
  375. if (!is_list(ioterm)) {
  376. DESTROY_ESTACK(stack);
  377. *costp = 0;
  378. return (Sint) -1;
  379. }
  380. /* OK a list, needs to be processed in order, handling each flat list-level
  381. as they occur, just like io_list_to_binary would */
  382. ESTACK_PUSH(stack,ioterm);
  383. while (!ESTACK_ISEMPTY(stack)) {
  384. ioterm = ESTACK_POP(stack);
  385. if (is_nil(ioterm)) {
  386. /* ignore empty lists */
  387. continue;
  388. }
  389. if(is_list(ioterm)) {
  390. L_Again: /* Restart with sublist, old listend was pushed on stack */
  391. objp = list_val(ioterm);
  392. obj = CAR(objp);
  393. for(;;) { /* loop over one flat list of bytes and binaries
  394. until sublist or list end is encountered */
  395. if (is_small(obj)) { /* Always small */
  396. for(;;) {
  397. Uint x = unsigned_val(obj);
  398. if (x < 0x80)
  399. need +=1;
  400. else if (x < 0x800)
  401. need += 2;
  402. else if (x < 0x10000)
  403. need += 3;
  404. else
  405. need += 4;
  406. /* everything else will give badarg later
  407. in the process, so we dont check */
  408. ++cost;
  409. ioterm = CDR(objp);
  410. if (!is_list(ioterm)) {
  411. break;
  412. }
  413. objp = list_val(ioterm);
  414. obj = CAR(objp);
  415. if (!is_small(obj))
  416. break;
  417. }
  418. } else if (is_nil(obj)) {
  419. ioterm = CDR(objp);
  420. if (!is_list(ioterm)) {
  421. break;
  422. }
  423. objp = list_val(ioterm);
  424. obj = CAR(objp);
  425. } else if (is_list(obj)) {
  426. /* push rest of list for later processing, start
  427. again with sublist */
  428. ESTACK_PUSH(stack,CDR(objp));
  429. ioterm = obj;
  430. goto L_Again;
  431. } else if (is_binary(obj)) {
  432. Sint x;
  433. if (latin1) {
  434. x = latin1_binary_need(obj);
  435. if (x < 0) {
  436. DESTROY_ESTACK(stack);
  437. *costp = cost;
  438. return x;
  439. }
  440. cost += x;
  441. } else {
  442. x = aligned_binary_size(obj);
  443. if (x < 0) {
  444. DESTROY_ESTACK(stack);
  445. *costp = cost;
  446. return x;
  447. }
  448. ++cost;
  449. }
  450. need += x;
  451. ioterm = CDR(objp);
  452. if (is_list(ioterm)) {
  453. /* objp and obj need to be updated if
  454. loop is to continue */
  455. objp = list_val(ioterm);
  456. obj = CAR(objp);
  457. }
  458. } else {
  459. DESTROY_ESTACK(stack);
  460. *costp = cost;
  461. return ((Sint) -1);
  462. }
  463. if (is_nil(ioterm) || !is_list(ioterm)) {
  464. break;
  465. }
  466. } /* for(;;) */
  467. } /* is_list(ioterm) */
  468. if (!is_list(ioterm) && !is_nil(ioterm)) {
  469. /* inproper list end */
  470. if (is_binary(ioterm)) {
  471. Sint x;
  472. if (latin1) {
  473. x = latin1_binary_need(ioterm);
  474. if (x < 0) {
  475. DESTROY_ESTACK(stack);
  476. *costp = cost;
  477. return x;
  478. }
  479. cost += x;
  480. } else {
  481. x = aligned_binary_size(ioterm);
  482. if (x < 0) {
  483. DESTROY_ESTACK(stack);
  484. *costp = cost;
  485. return x;
  486. }
  487. ++cost;
  488. }
  489. need += x;
  490. } else {
  491. DESTROY_ESTACK(stack);
  492. *costp = cost;
  493. return ((Sint) -1);
  494. }
  495. }
  496. } /* while not estack empty */
  497. DESTROY_ESTACK(stack);
  498. *costp = cost;
  499. return need;
  500. }
  501. static Eterm do_build_utf8(Process *p, Eterm ioterm, int *left, int latin1,
  502. byte *target, int *pos, Uint *characters, int *err,
  503. byte *leftover, int *num_leftovers)
  504. {
  505. int c;
  506. Eterm *objp;
  507. Eterm obj;
  508. DECLARE_ESTACK(stack);
  509. *err = 0;
  510. if ((*left) <= 0 || is_nil(ioterm)) {
  511. DESTROY_ESTACK(stack);
  512. return ioterm;
  513. }
  514. if(is_binary(ioterm)) {
  515. Uint bitoffs;
  516. Uint bitsize;
  517. Uint size;
  518. Uint i;
  519. Eterm res_term = NIL;
  520. unsigned char *bytes;
  521. byte *temp_alloc = NULL;
  522. Uint orig_size;
  523. ERTS_GET_BINARY_BYTES(ioterm, bytes, bitoffs, bitsize);
  524. if (bitsize != 0) {
  525. *err = 1;
  526. DESTROY_ESTACK(stack);
  527. return ioterm;
  528. }
  529. if (bitoffs != 0) {
  530. bytes = erts_get_aligned_binary_bytes(ioterm, &temp_alloc);
  531. /* The call to erts_get_aligned_binary_bytes cannot fail as
  532. we'we already checked bitsize and that this is a binary */
  533. }
  534. orig_size = size = binary_size(ioterm);
  535. /* This is done to avoid splitting binaries in two
  536. and then create an unnecessary rest that eventually gives an error.
  537. For cases where errors are not returned this is unnecessary */
  538. if (!latin1) {
  539. /* Find a valid character boundary */
  540. while (size > (*left) &&
  541. (((byte) bytes[(*left)]) & ((byte) 0xC0)) == ((byte) 0x80)) {
  542. ++(*left);
  543. }
  544. }
  545. if (size > (*left)) {
  546. Eterm *hp;
  547. ErlSubBin *sb;
  548. Eterm orig;
  549. Uint offset;
  550. /* Split the binary in two parts, of which we
  551. only process the first */
  552. hp = HAlloc(p, ERL_SUB_BIN_SIZE);
  553. sb = (ErlSubBin *) hp;
  554. ERTS_GET_REAL_BIN(ioterm, orig, offset, bitoffs, bitsize);
  555. sb->thing_word = HEADER_SUB_BIN;
  556. sb->size = size - (*left);
  557. sb->offs = offset + (*left);
  558. sb->orig = orig;
  559. sb->bitoffs = bitoffs;
  560. sb->bitsize = bitsize;
  561. sb->is_writable = 0;
  562. res_term = make_binary(sb);
  563. size = (*left);
  564. }
  565. if (!latin1) {
  566. int num;
  567. byte *err_pos = NULL;
  568. num = copy_utf8_bin(target + (*pos), bytes,
  569. size, leftover, num_leftovers,&err_pos,characters);
  570. *pos += num;
  571. if (err_pos != NULL) {
  572. int rest_bin_offset;
  573. int rest_bin_size;
  574. Eterm *hp;
  575. ErlSubBin *sb;
  576. Eterm orig;
  577. Uint offset;
  578. *err = 1;
  579. /* we have no real stack, just build a list of the binaries
  580. we have not decoded... */
  581. DESTROY_ESTACK(stack);
  582. rest_bin_offset = (err_pos - bytes);
  583. rest_bin_size = orig_size - rest_bin_offset;
  584. hp = HAlloc(p, ERL_SUB_BIN_SIZE);
  585. sb = (ErlSubBin *) hp;
  586. ERTS_GET_REAL_BIN(ioterm, orig, offset, bitoffs, bitsize);
  587. sb->thing_word = HEADER_SUB_BIN;
  588. sb->size = rest_bin_size;
  589. sb->offs = offset + rest_bin_offset;
  590. sb->orig = orig;
  591. sb->bitoffs = bitoffs;
  592. sb->bitsize = bitsize;
  593. sb->is_writable = 0;
  594. res_term = make_binary(sb);
  595. erts_free_aligned_binary_bytes(temp_alloc);
  596. return res_term;
  597. }
  598. } else {
  599. i = 0;
  600. while(i < size) {
  601. if (bytes[i] < 0x80) {
  602. target[(*pos)++] = bytes[i++];
  603. } else {
  604. target[(*pos)++] = ((bytes[i] >> 6) | ((byte) 0xC0));
  605. target[(*pos)++] = ((bytes[i] & 0x3F) | ((byte) 0x80));
  606. ++i;
  607. }
  608. ++(*characters);
  609. }
  610. }
  611. *left -= size;
  612. DESTROY_ESTACK(stack);
  613. erts_free_aligned_binary_bytes(temp_alloc);
  614. return res_term;
  615. }
  616. if (!is_list(ioterm)) {
  617. *err = 1;
  618. goto done;
  619. }
  620. /* OK a list, needs to be processed in order, handling each flat list-level
  621. as they occur, just like io_list_to_binary would */
  622. ESTACK_PUSH(stack,ioterm);
  623. while (!ESTACK_ISEMPTY(stack) && (*left)) {
  624. ioterm = ESTACK_POP(stack);
  625. if (is_nil(ioterm)) {
  626. /* ignore empty lists */
  627. continue;
  628. }
  629. if(is_list(ioterm)) {
  630. L_Again: /* Restart with sublist, old listend was pushed on stack */
  631. objp = list_val(ioterm);
  632. obj = CAR(objp);
  633. for(;;) { /* loop over one flat list of bytes and binaries
  634. until sublist or list end is encountered */
  635. if (is_small(obj)) { /* Always small in unicode*/
  636. if (*num_leftovers) {
  637. /* Have rest from previous bin and this is an integer, not allowed */
  638. *err = 1;
  639. goto done;
  640. }
  641. for(;;) {
  642. Uint x = unsigned_val(obj);
  643. if (latin1 && x > 255) {
  644. *err = 1;
  645. goto done;
  646. }
  647. if (x < 0x80) {
  648. target[(*pos)++] = (byte) x;
  649. }
  650. else if (x < 0x800) {
  651. target[(*pos)++] = (((byte) (x >> 6)) |
  652. ((byte) 0xC0));
  653. target[(*pos)++] = (((byte) (x & 0x3F)) |
  654. ((byte) 0x80));
  655. } else if (x < 0x10000) {
  656. if ((x >= 0xD800 && x <= 0xDFFF) ||
  657. (x == 0xFFFE) ||
  658. (x == 0xFFFF)) { /* Invalid unicode range */
  659. *err = 1;
  660. goto done;
  661. }
  662. target[(*pos)++] = (((byte) (x >> 12)) |
  663. ((byte) 0xE0));
  664. target[(*pos)++] = ((((byte) (x >> 6)) & 0x3F) |
  665. ((byte) 0x80));
  666. target[(*pos)++] = (((byte) (x & 0x3F)) |
  667. ((byte) 0x80));
  668. } else if (x < 0x110000) { /* Standard imposed max */
  669. target[(*pos)++] = (((byte) (x >> 18)) |
  670. ((byte) 0xF0));
  671. target[(*pos)++] = ((((byte) (x >> 12)) & 0x3F) |
  672. ((byte) 0x80));
  673. target[(*pos)++] = ((((byte) (x >> 6)) & 0x3F) |
  674. ((byte) 0x80));
  675. target[(*pos)++] = (((byte) (x & 0x3F)) |
  676. ((byte) 0x80));
  677. } else {
  678. *err = 1;
  679. goto done;
  680. }
  681. ++(*characters);
  682. --(*left);
  683. ioterm = CDR(objp);
  684. if (!is_list(ioterm) || !(*left)) {
  685. break;
  686. }
  687. objp = list_val(ioterm);
  688. obj = CAR(objp);
  689. if (!is_small(obj))
  690. break;
  691. }
  692. } else if (is_nil(obj)) {
  693. ioterm = CDR(objp);
  694. if (!is_list(ioterm)) {
  695. break;
  696. }
  697. objp = list_val(ioterm);
  698. obj = CAR(objp);
  699. } else if (is_list(obj)) {
  700. /* push rest of list for later processing, start
  701. again with sublist */
  702. ESTACK_PUSH(stack,CDR(objp));
  703. ioterm = obj;
  704. goto L_Again;
  705. } else if (is_binary(obj)) {
  706. Eterm rest_term;
  707. rest_term = do_build_utf8(p,obj,left,latin1,target,pos, characters, err,
  708. leftover, num_leftovers);
  709. if ((*err) != 0) {
  710. Eterm *hp;
  711. hp = HAlloc(p, 2);
  712. obj = CDR(objp);
  713. ioterm = CONS(hp, rest_term, obj);
  714. //(*left) = 0;
  715. goto done;
  716. }
  717. if (rest_term != NIL) {
  718. Eterm *hp;
  719. hp = HAlloc(p, 2);
  720. obj = CDR(objp);
  721. ioterm = CONS(hp, rest_term, obj);
  722. (*left) = 0;
  723. break;
  724. }
  725. ioterm = CDR(objp);
  726. if (is_list(ioterm)) {
  727. /* objp and obj need to be updated if
  728. loop is to continue */
  729. objp = list_val(ioterm);
  730. obj = CAR(objp);
  731. }
  732. } else {
  733. *err = 1;
  734. goto done;
  735. }
  736. if (!(*left) || is_nil(ioterm) || !is_list(ioterm)) {
  737. break;
  738. }
  739. } /* for(;;) */
  740. } /* is_list(ioterm) */
  741. if ((*left) && !is_list(ioterm) && !is_nil(ioterm)) {
  742. /* inproper list end */
  743. if (is_binary(ioterm)) {
  744. ioterm = do_build_utf8(p,ioterm,left,latin1,target,pos,characters,err,leftover,num_leftovers);
  745. if ((*err) != 0) {
  746. goto done;
  747. }
  748. } else {
  749. *err = 1;
  750. goto done;
  751. }
  752. }
  753. } /* while left and not estack empty */
  754. done:
  755. c = ESTACK_COUNT(stack);
  756. if (c > 0) {
  757. Eterm *hp = HAlloc(p,2*c);
  758. while(!ESTACK_ISEMPTY(stack)) {
  759. Eterm st = ESTACK_POP(stack);
  760. ioterm = CONS(hp, ioterm, st);
  761. hp += 2;
  762. }
  763. }
  764. DESTROY_ESTACK(stack);
  765. return ioterm;
  766. }
  767. static int check_leftovers(byte *source, int size)
  768. {
  769. if (((*source) & ((byte) 0xE0)) == 0xC0) {
  770. return 0;
  771. } else if (((*source) & ((byte) 0xF0)) == 0xE0) {
  772. if (size < 2 ||
  773. (size < 3 && ((source[1] & ((byte) 0xC0)) == 0x80))) {
  774. return 0;
  775. }
  776. } else if (((*source) & ((byte) 0xF8)) == 0xF0) {
  777. if (size < 2 ||
  778. (size < 3 && ((source[1] & ((byte) 0xC0)) == 0x80)) ||
  779. (size < 4 &&
  780. ((source[1] & ((byte) 0xC0)) == 0x80) &&
  781. ((source[2] & ((byte) 0xC0)) == 0x80))) {
  782. return 0;
  783. }
  784. }
  785. return -1;
  786. }
  787. static BIF_RETTYPE build_utf8_return(Process *p,Eterm bin,int pos,
  788. Eterm rest_term,int err,
  789. byte *leftover,int num_leftovers,Eterm latin1)
  790. {
  791. Eterm *hp;
  792. Eterm ret;
  793. binary_size(bin) = pos;
  794. if (err) {
  795. if (num_leftovers > 0) {
  796. Eterm leftover_bin = new_binary(p, leftover, num_leftovers);
  797. hp = HAlloc(p,8);
  798. rest_term = CONS(hp,rest_term,NIL);
  799. hp += 2;
  800. rest_term = CONS(hp,leftover_bin,rest_term);
  801. hp += 2;
  802. } else {
  803. hp = HAlloc(p,4);
  804. }
  805. ret = TUPLE3(hp,am_error,bin,rest_term);
  806. } else if (rest_term == NIL && num_leftovers != 0) {
  807. Eterm leftover_bin = new_binary(p, leftover, num_leftovers);
  808. if (check_leftovers(leftover,num_leftovers) != 0) {
  809. hp = HAlloc(p,4);
  810. ret = TUPLE3(hp,am_error,bin,leftover_bin);
  811. } else {
  812. hp = HAlloc(p,4);
  813. ret = TUPLE3(hp,am_incomplete,bin,leftover_bin);
  814. }
  815. } else { /* All OK */
  816. if (rest_term != NIL) { /* Trap */
  817. if (num_leftovers > 0) {
  818. Eterm rest_bin = new_binary(p, leftover, num_leftovers);
  819. hp = HAlloc(p,2);
  820. rest_term = CONS(hp,rest_bin,rest_term);
  821. }
  822. BUMP_ALL_REDS(p);
  823. BIF_TRAP3(&characters_to_utf8_trap_exp, p, bin, rest_term, latin1);
  824. } else { /* Success */
  825. /*hp = HAlloc(p,5);
  826. ret = TUPLE4(hp,bin,rest_term,make_small(pos),make_small(err));*/
  827. ret = bin;
  828. }
  829. }
  830. BIF_RET(ret);
  831. }
  832. static BIF_RETTYPE characters_to_utf8_trap(BIF_ALIST_3)
  833. {
  834. Eterm *real_bin;
  835. Sint need;
  836. byte* bytes;
  837. Eterm rest_term;
  838. int left, sleft;
  839. int pos;
  840. int err;
  841. byte leftover[4]; /* used for temp buffer too,
  842. otherwise 3 bytes would have been enough */
  843. int num_leftovers = 0;
  844. int latin1 = 0;
  845. Uint characters = 0;
  846. /*erts_printf("Trap %T!\r\n",BIF_ARG_2);*/
  847. ASSERT(is_binary(BIF_ARG_1));
  848. real_bin = binary_val(BIF_ARG_1);
  849. ASSERT(*real_bin == HEADER_PROC_BIN);
  850. need = ((ProcBin *) real_bin)->val->orig_size;
  851. pos = (int) binary_size(BIF_ARG_1);
  852. bytes = binary_bytes(BIF_ARG_1);
  853. sleft = left = allowed_iterations(BIF_P);
  854. err = 0;
  855. if (BIF_ARG_3 == am_latin1) {
  856. latin1 = 1;
  857. }
  858. rest_term = do_build_utf8(BIF_P, BIF_ARG_2, &left, latin1,
  859. bytes, &pos, &characters, &err, leftover, &num_leftovers);
  860. cost_to_proc(BIF_P, sleft - left);
  861. return build_utf8_return(BIF_P,BIF_ARG_1,pos,rest_term,err,
  862. leftover,num_leftovers,BIF_ARG_3);
  863. }
  864. BIF_RETTYPE unicode_bin_is_7bit_1(BIF_ALIST_1)
  865. {
  866. Sint need;
  867. if(!is_binary(BIF_ARG_1)) {
  868. BIF_RET(am_false);
  869. }
  870. need = latin1_binary_need(BIF_ARG_1);
  871. if(need >= 0 && aligned_binary_size(BIF_ARG_1) == need) {
  872. BIF_RET(am_true);
  873. }
  874. BIF_RET(am_false);
  875. }
  876. static int is_valid_utf8(Eterm orig_bin)
  877. {
  878. Uint bitoffs;
  879. Uint bitsize;
  880. Uint size;
  881. byte *temp_alloc = NULL;
  882. byte *endpos;
  883. Uint numchar;
  884. byte *bytes;
  885. int ret;
  886. ERTS_GET_BINARY_BYTES(orig_bin, bytes, bitoffs, bitsize);
  887. if (bitsize != 0) {
  888. return 0;
  889. }
  890. if (bitoffs != 0) {
  891. bytes = erts_get_aligned_binary_bytes(orig_bin, &temp_alloc);
  892. }
  893. size = binary_size(orig_bin);
  894. ret = erts_analyze_utf8(bytes,
  895. size,
  896. &endpos,&numchar,NULL);
  897. erts_free_aligned_binary_bytes(temp_alloc);
  898. return (ret == ERTS_UTF8_OK);
  899. }
  900. BIF_RETTYPE unicode_characters_to_binary_2(BIF_ALIST_2)
  901. {
  902. Sint need;
  903. Uint characters;
  904. int latin1;
  905. Eterm bin;
  906. byte *bytes;
  907. int pos;
  908. int err;
  909. int left, sleft;
  910. Eterm rest_term, subject;
  911. byte leftover[4]; /* used for temp buffer too, o
  912. therwise 3 bytes would have been enough */
  913. int num_leftovers = 0;
  914. Uint cost_of_utf8_need;
  915. if (BIF_ARG_2 == am_latin1) {
  916. latin1 = 1;
  917. } else if (BIF_ARG_2 == am_unicode || BIF_ARG_2 == am_utf8) {
  918. latin1 = 0;
  919. } else {
  920. BIF_TRAP2(c_to_b_int_trap_exportp, BIF_P, BIF_ARG_1, BIF_ARG_2);
  921. }
  922. if (is_list(BIF_ARG_1) && is_binary(CAR(list_val(BIF_ARG_1))) &&
  923. is_nil(CDR(list_val(BIF_ARG_1)))) {
  924. subject = CAR(list_val(BIF_ARG_1));
  925. } else {
  926. subject = BIF_ARG_1;
  927. }
  928. need = utf8_need(subject,latin1,&cost_of_utf8_need);
  929. if (need < 0) {
  930. BIF_ERROR(BIF_P,BADARG);
  931. }
  932. if (is_binary(subject) && need >= 0 && aligned_binary_size(subject) == need
  933. && (latin1 || is_valid_utf8(subject))) {
  934. cost_to_proc(BIF_P, simple_loops_to_common(cost_of_utf8_need));
  935. BIF_RET(subject);
  936. }
  937. bin = erts_new_mso_binary(BIF_P, (byte *)NULL, need);
  938. bytes = binary_bytes(bin);
  939. cost_to_proc(BIF_P, simple_loops_to_common(cost_of_utf8_need));
  940. left = allowed_iterations(BIF_P) -
  941. simple_loops_to_common(cost_of_utf8_need);
  942. if (left <= 0) {
  943. /* simplified - let everything be setup by setting left to 1 */
  944. left = 1;
  945. }
  946. sleft = left;
  947. pos = 0;
  948. err = 0;
  949. rest_term = do_build_utf8(BIF_P, subject, &left, latin1,
  950. bytes, &pos, &characters, &err, leftover, &num_leftovers);
  951. #ifdef HARDDEBUG
  952. if (left == 0) {
  953. Eterm bin;
  954. if (is_binary(subject)) {
  955. bin = subject;
  956. } else if(is_list(subject) && is_binary(CAR(list_val(subject)))) {
  957. bin = CAR(list_val(subject));
  958. } else {
  959. bin = NIL;
  960. }
  961. if (is_binary(bin)) {
  962. byte *t = NULL;
  963. Uint sz = binary_size(bin);
  964. byte *by = erts_get_aligned_binary_bytes(bin,&t);
  965. int i;
  966. erts_printf("<<");
  967. for (i = 0;i < sz; ++i) {
  968. erts_printf((i == sz -1) ? "0x%X" : "0x%X, ", (unsigned) by[i]);
  969. }
  970. erts_printf(">>: ");
  971. erts_free_aligned_binary_bytes(t);
  972. }
  973. erts_printf("%d - %d = %d\n",sleft,left,sleft - left);
  974. }
  975. #endif
  976. cost_to_proc(BIF_P, sleft - left);
  977. return build_utf8_return(BIF_P,bin,pos,rest_term,err,
  978. leftover,num_leftovers,BIF_ARG_2);
  979. }
  980. static BIF_RETTYPE build_list_return(Process *p, byte *bytes, int pos, Uint characters,
  981. Eterm rest_term, int err,
  982. byte *leftover, int num_leftovers,
  983. Eterm latin1, int left)
  984. {
  985. Eterm *hp;
  986. if (left <= 0) {
  987. left = 1;
  988. }
  989. if (err) {
  990. if (num_leftovers > 0) {
  991. Eterm leftover_bin = new_binary(p, leftover, num_leftovers);
  992. hp = HAlloc(p,4);
  993. rest_term = CONS(hp,rest_term,NIL);
  994. hp += 2;
  995. rest_term = CONS(hp,leftover_bin,rest_term);
  996. }
  997. BIF_RET(finalize_list_to_list(p, bytes, rest_term, 0U, pos, characters, ERTS_UTF8_ERROR, left, NIL));
  998. } else if (rest_term == NIL && num_leftovers != 0) {
  999. Eterm leftover_bin = new_binary(p, leftover, num_leftovers);
  1000. if (check_leftovers(leftover,num_leftovers) != 0) {
  1001. BIF_RET(finalize_list_to_list(p, bytes, leftover_bin, 0U, pos, characters, ERTS_UTF8_ERROR,
  1002. left, NIL));
  1003. } else {
  1004. BIF_RET(finalize_list_to_list(p, bytes, leftover_bin, 0U, pos, characters, ERTS_UTF8_INCOMPLETE,
  1005. left, NIL));
  1006. }
  1007. } else { /* All OK */
  1008. if (rest_term != NIL) { /* Trap */
  1009. RestartContext rc;
  1010. if (num_leftovers > 0) {
  1011. Eterm rest_bin = new_binary(p, leftover, num_leftovers);
  1012. hp = HAlloc(p,2);
  1013. rest_term = CONS(hp,rest_bin,rest_term);
  1014. }
  1015. BUMP_ALL_REDS(p);
  1016. rc.bytes = bytes;
  1017. rc.num_processed_bytes = 0; /* not used */
  1018. rc.num_bytes_to_process = pos;
  1019. rc.num_resulting_chars = characters;
  1020. rc.state = ERTS_UTF8_OK; /* not used */
  1021. BIF_TRAP3(&characters_to_list_trap_1_exp, p, make_magic_bin_for_restart(p,&rc),
  1022. rest_term, latin1);
  1023. } else { /* Success */
  1024. BIF_RET(finalize_list_to_list(p, bytes, NIL, 0U, pos, characters, ERTS_UTF8_OK, left, NIL));
  1025. }
  1026. }
  1027. }
  1028. static BIF_RETTYPE characters_to_list_trap_1(BIF_ALIST_3)
  1029. {
  1030. RestartContext *rc;
  1031. byte* bytes;
  1032. int pos;
  1033. Uint characters;
  1034. int err;
  1035. Eterm rest_term;
  1036. int left, sleft;
  1037. int latin1 = 0;
  1038. byte leftover[4]; /* used for temp buffer too,
  1039. otherwise 3 bytes would have been enough */
  1040. int num_leftovers = 0;
  1041. rc = get_rc_from_bin(BIF_ARG_1);
  1042. bytes = rc->bytes;
  1043. rc->bytes = NULL; /* to avoid free due to later GC */
  1044. pos = rc->num_bytes_to_process;
  1045. characters = rc->num_resulting_chars;
  1046. sleft = left = allowed_iterations(BIF_P);
  1047. err = 0;
  1048. if (BIF_ARG_3 == am_latin1) {
  1049. latin1 = 1;
  1050. }
  1051. rest_term = do_build_utf8(BIF_P, BIF_ARG_2, &left, latin1,
  1052. bytes, &pos, &characters, &err, leftover, &num_leftovers);
  1053. cost_to_proc(BIF_P, sleft - left);
  1054. return build_list_return(BIF_P,bytes,pos,characters,rest_term,err,
  1055. leftover,num_leftovers,BIF_ARG_3,left);
  1056. }
  1057. BIF_RETTYPE unicode_characters_to_list_2(BIF_ALIST_2)
  1058. {
  1059. Sint need;
  1060. int latin1;
  1061. Uint characters = 0;
  1062. byte *bytes;
  1063. int pos;
  1064. int err;
  1065. int left, sleft;
  1066. Eterm rest_term;
  1067. byte leftover[4]; /* used for temp buffer too, o
  1068. therwise 3 bytes would have been enough */
  1069. int num_leftovers = 0;
  1070. Uint cost_of_utf8_need;
  1071. if (BIF_ARG_2 == am_latin1) {
  1072. latin1 = 1;
  1073. } else if (BIF_ARG_2 == am_unicode || BIF_ARG_2 == am_utf8) {
  1074. latin1 = 0;
  1075. } else {
  1076. BIF_TRAP2(c_to_l_int_trap_exportp, BIF_P, BIF_ARG_1, BIF_ARG_2);
  1077. }
  1078. if (is_binary(BIF_ARG_1) && !latin1) { /* Optimized behaviour for this case */
  1079. return utf8_to_list(BIF_P,BIF_ARG_1);
  1080. }
  1081. need = utf8_need(BIF_ARG_1,latin1,&cost_of_utf8_need);
  1082. if (need < 0) {
  1083. BIF_ERROR(BIF_P,BADARG);
  1084. }
  1085. bytes = alloc_restart(need);
  1086. cost_to_proc(BIF_P, simple_loops_to_common(cost_of_utf8_need));
  1087. left = allowed_iterations(BIF_P) -
  1088. simple_loops_to_common(cost_of_utf8_need);
  1089. if (left <= 0) {
  1090. /* simplified - let everything be setup by setting left to 1 */
  1091. left = 1;
  1092. }
  1093. sleft = left;
  1094. pos = 0;
  1095. err = 0;
  1096. rest_term = do_build_utf8(BIF_P, BIF_ARG_1, &left, latin1,
  1097. bytes, &pos, &characters, &err, leftover, &num_leftovers);
  1098. cost_to_proc(BIF_P, sleft - left);
  1099. return build_list_return(BIF_P,bytes,pos,characters,rest_term,err,
  1100. leftover,num_leftovers,BIF_ARG_2,left);
  1101. }
  1102. /*
  1103. * When input to characters_to_list is a plain binary and the format is 'unicode', we do
  1104. * a faster analyze and size count with this function.
  1105. */
  1106. int erts_analyze_utf8(byte *source, Uint size,
  1107. byte **err_pos, Uint *num_chars, int *left)
  1108. {
  1109. *err_pos = source;
  1110. *num_chars = 0;
  1111. while (size) {
  1112. if (((*source) & ((byte) 0x80)) == 0) {
  1113. source++;
  1114. --size;
  1115. } else if (((*source) & ((byte) 0xE0)) == 0xC0) {
  1116. if (size < 2) {
  1117. return ERTS_UTF8_INCOMPLETE;
  1118. }
  1119. if (((source[1] & ((byte) 0xC0)) != 0x80) ||
  1120. ((*source) < 0xC2) /* overlong */) {
  1121. return ERTS_UTF8_ERROR;
  1122. }
  1123. source += 2;
  1124. size -= 2;
  1125. } else if (((*source) & ((byte) 0xF0)) == 0xE0) {
  1126. if (size < 3) {
  1127. return ERTS_UTF8_INCOMPLETE;
  1128. }
  1129. if (((source[1] & ((byte) 0xC0)) != 0x80) ||
  1130. ((source[2] & ((byte) 0xC0)) != 0x80) ||
  1131. (((*source) == 0xE0) && (source[1] < 0xA0)) /* overlong */ ) {
  1132. return ERTS_UTF8_ERROR;
  1133. }
  1134. if ((((*source) & ((byte) 0xF)) == 0xD) &&
  1135. ((source[1] & 0x20) != 0)) {
  1136. return ERTS_UTF8_ERROR;
  1137. }
  1138. if (((*source) == 0xEF) && (source[1] == 0xBF) &&
  1139. ((source[2] == 0xBE) || (source[2] == 0xBF))) {
  1140. return ERTS_UTF8_ERROR;
  1141. }
  1142. source += 3;
  1143. size -= 3;
  1144. } else if (((*source) & ((byte) 0xF8)) == 0xF0) {
  1145. if (size < 4) {
  1146. return ERTS_UTF8_INCOMPLETE;
  1147. }
  1148. if (((source[1] & ((byte) 0xC0)) != 0x80) ||
  1149. ((source[2] & ((byte) 0xC0)) != 0x80) ||
  1150. ((source[3] & ((byte) 0xC0)) != 0x80) ||
  1151. (((*source) == 0xF0) && (source[1] < 0x90)) /* overlong */) {
  1152. return ERTS_UTF8_ERROR;
  1153. }
  1154. if ((((*source) & ((byte)0x7)) > 0x4U) ||
  1155. ((((*source) & ((byte)0x7)) == 0x4U) &&
  1156. ((source[1] & ((byte)0x3F)) > 0xFU))) {
  1157. return ERTS_UTF8_ERROR;
  1158. }
  1159. source += 4;
  1160. size -= 4;
  1161. } else {
  1162. return ERTS_UTF8_ERROR;
  1163. }
  1164. ++(*num_chars);
  1165. *err_pos = source;
  1166. if (left && --(*left) <= 0) {
  1167. return ERTS_UTF8_ANALYZE_MORE;
  1168. }
  1169. }
  1170. return ERTS_UTF8_OK;
  1171. }
  1172. /*
  1173. * No errors should be able to occur - no overlongs, no malformed, no nothing
  1174. */
  1175. static Eterm do_utf8_to_list(Process *p, Uint num, byte *bytes, Uint sz,
  1176. Uint left,
  1177. Uint *num_built, Uint *num_eaten, Eterm tail)
  1178. {
  1179. Eterm *hp;
  1180. Eterm ret;
  1181. byte *source, *ssource;
  1182. Uint unipoint;
  1183. ASSERT(num > 0);
  1184. if (left < num) {
  1185. if (left > 0)
  1186. num = left;
  1187. else
  1188. num = 1;
  1189. }
  1190. *num_built = num; /* Always */
  1191. hp = HAlloc(p,num * 2);
  1192. ret = tail;
  1193. source = bytes + sz;
  1194. ssource = source;
  1195. while(--source >= bytes) {
  1196. if (((*source) & ((byte) 0x80)) == 0) {
  1197. unipoint = (Uint) *source;
  1198. } else if (((*source) & ((byte) 0xE0)) == 0xC0) {
  1199. unipoint =
  1200. (((Uint) ((*source) & ((byte) 0x1F))) << 6) |
  1201. ((Uint) (source[1] & ((byte) 0x3F)));
  1202. } else if (((*source) & ((byte) 0xF0)) == 0xE0) {
  1203. unipoint =
  1204. (((Uint) ((*source) & ((byte) 0xF))) << 12) |
  1205. (((Uint) (source[1] & ((byte) 0x3F))) << 6) |
  1206. ((Uint) (source[2] & ((byte) 0x3F)));
  1207. } else if (((*source) & ((byte) 0xF8)) == 0xF0) {
  1208. unipoint =
  1209. (((Uint) ((*source) & ((byte) 0x7))) << 18) |
  1210. (((Uint) (source[1] & ((byte) 0x3F))) << 12) |
  1211. (((Uint) (source[2] & ((byte) 0x3F))) << 6) |
  1212. ((Uint) (source[3] & ((byte) 0x3F)));
  1213. } else {
  1214. /* ignore 2#10XXXXXX */
  1215. continue;
  1216. }
  1217. ret = CONS(hp,make_small(unipoint),ret);
  1218. hp += 2;
  1219. if (--num <= 0) {
  1220. break;
  1221. }
  1222. }
  1223. *num_eaten = (ssource - source);
  1224. return ret;
  1225. }
  1226. static int is_candidate(Uint cp)
  1227. {
  1228. int index,pos;
  1229. if (cp < 768) return 0;
  1230. if (cp > 4023) {
  1231. if (cp == 12441 || cp == 12442) return 1;
  1232. return 0;
  1233. }
  1234. index = cp / 32 - COMP_CANDIDATE_MAP_OFFSET;
  1235. pos = cp % 32;
  1236. return !!(comp_candidate_map[index] & (1UL << pos));
  1237. }
  1238. static int hashsearch(int *htab, int htab_size, CompEntry *cv, Uint16 c)
  1239. {
  1240. int bucket = c % htab_size;
  1241. while (htab[bucket] != -1 && cv[htab[bucket]].c != c)
  1242. bucket = (bucket + 1) % htab_size;
  1243. return htab[bucket];
  1244. }
  1245. #define TRANSLATE_NO 0
  1246. #define TRANSLATE_MAYBE -1
  1247. /* The s array is reversed */
  1248. static int translate(Uint16 *s, int slen, Uint16 *res)
  1249. {
  1250. /* Go backwards through buffer and match against tree */
  1251. int pos = 0;
  1252. CompEntry *cv = compose_tab;
  1253. int *hc = hash_compose_tab;
  1254. int cvs = compose_tab_size;
  1255. int x;
  1256. while (pos < slen) {
  1257. x = hashsearch(hc,cvs*HASH_SIZE_FACTOR,cv,s[pos]);
  1258. if (x < 0) {
  1259. return TRANSLATE_NO;
  1260. }
  1261. if (cv[x].res) {
  1262. *res = cv[x].res;
  1263. return pos;
  1264. }
  1265. cvs = cv[x].num_subs;
  1266. hc = cv[x].hash;
  1267. cv = cv[x].subs;
  1268. ++pos;
  1269. }
  1270. return TRANSLATE_MAYBE;
  1271. }
  1272. static void handle_first_norm(Uint16 *savepoints, int *numpointsp, Uint unipoint)
  1273. {
  1274. /*erts_fprintf(stderr,"CP = %d, numpoints = %d\n",(int) unipoint,(int) *numpointsp);*/
  1275. *numpointsp = 1;
  1276. savepoints[0] = (Uint16) unipoint;
  1277. }
  1278. static void cleanup_norm(Eterm **hpp, Uint16 *savepoints, int numpoints, Eterm *retp)
  1279. {
  1280. Eterm *hp = *hpp;
  1281. int res,i;
  1282. Uint16 newpoint;
  1283. Eterm ret = *retp;
  1284. ret = CONS(hp,make_small((Uint) savepoints[0]),ret);
  1285. hp += 2;
  1286. for (i = 1;i < numpoints;) {
  1287. if(!is_candidate(savepoints[i]) ||
  1288. ((res = translate(savepoints+i,numpoints - i, &newpoint)) <= 0)) {
  1289. ret = CONS(hp,make_small((Uint) savepoints[i]),ret);
  1290. hp += 2;
  1291. ++i;
  1292. } else {
  1293. ret = CONS(hp,make_small((Uint) newpoint),ret);
  1294. hp += 2;
  1295. i += res;
  1296. }
  1297. }
  1298. *retp = ret;
  1299. }
  1300. static void handle_potential_norm(Eterm **hpp, Uint16 *savepoints, int *numpointsp, Uint unipoint, Eterm *retp)
  1301. {
  1302. Eterm *hp = *hpp;
  1303. int numpoints = *numpointsp;
  1304. int res,i;
  1305. Uint16 newpoint;
  1306. Eterm ret = *retp;
  1307. /* erts_fprintf(stderr,"CP = %d, numpoints = %d\n",(int) unipoint,(int) numpoints);*/
  1308. if ((unipoint >> 16) == 0) { /* otherwise we're done here */
  1309. savepoints[numpoints++] = (Uint16) unipoint;
  1310. res = translate(savepoints,numpoints,&newpoint);
  1311. if (res == TRANSLATE_NO) {
  1312. ret = CONS(hp,make_small((Uint) savepoints[0]),ret);
  1313. hp += 2;
  1314. for (i = 1;i < numpoints;) {
  1315. if(!is_candidate(savepoints[i]) ||
  1316. ((res = translate(savepoints+i,numpoints - i, &newpoint)) == 0)) {
  1317. ret = CONS(hp,make_small((Uint) savepoints[i]),ret);
  1318. hp += 2;
  1319. ++i;
  1320. } else if (res > 0) {
  1321. ret = CONS(hp,make_small((Uint) newpoint),ret);
  1322. hp += 2;
  1323. i += res;
  1324. } else { /* res < 0 */
  1325. /* A "maybe", means we are not done yet */
  1326. int j = 0;
  1327. while (i < numpoints) {
  1328. savepoints[j++] = savepoints[i++];
  1329. }
  1330. numpoints = j;
  1331. goto breakaway;
  1332. }
  1333. }
  1334. numpoints = 0;
  1335. breakaway:
  1336. ;
  1337. } else if (res > 0) {
  1338. numpoints = 0;
  1339. ret = CONS(hp,make_small((Uint) newpoint),ret);
  1340. hp += 2;
  1341. } /* < 0 means go on */
  1342. } else {
  1343. /* Unconditional rollup, this character is larger than 16 bit */
  1344. ret = CONS(hp,make_small((Uint) savepoints[0]),ret);
  1345. hp += 2;
  1346. for (i = 1;i < numpoints;) {
  1347. if(!is_candidate(savepoints[i]) ||
  1348. ((res = translate(savepoints+i,numpoints - i, &newpoint)) <= 0)) {
  1349. ret = CONS(hp,make_small((Uint) savepoints[i]),ret);
  1350. hp += 2;
  1351. ++i;
  1352. } else {
  1353. ret = CONS(hp,make_small((Uint) newpoint),ret);
  1354. hp += 2;
  1355. i += res;
  1356. }
  1357. }
  1358. ret = CONS(hp,make_small(unipoint),ret);
  1359. hp += 2;
  1360. numpoints = 0;
  1361. }
  1362. *hpp = hp;
  1363. *numpointsp = numpoints;
  1364. *retp = ret;
  1365. }
  1366. static Eterm do_utf8_to_list_normalize(Process *p, Uint num, byte *bytes, Uint sz)
  1367. {
  1368. Eterm *hp,*hp_end;
  1369. Eterm ret;
  1370. byte *source;
  1371. Uint unipoint;
  1372. Uint16 savepoints[4];
  1373. int numpoints = 0;
  1374. ASSERT(num > 0);
  1375. hp = HAlloc(p,num * 2); /* May be to much */
  1376. hp_end = hp + num * 2;
  1377. ret = NIL;
  1378. source = bytes + sz;
  1379. while(--source >= bytes) {
  1380. if (((*source) & ((byte) 0x80)) == 0) {
  1381. unipoint = (Uint) *source;
  1382. } else if (((*source) & ((byte) 0xE0)) == 0xC0) {
  1383. unipoint =
  1384. (((Uint) ((*source) & ((byte) 0x1F))) << 6) |
  1385. ((Uint) (source[1] & ((byte) 0x3F)));
  1386. } else if (((*source) & ((byte) 0xF0)) == 0xE0) {
  1387. unipoint =
  1388. (((Uint) ((*source) & ((byte) 0xF))) << 12) |
  1389. (((Uint) (source[1] & ((byte) 0x3F))) << 6) |
  1390. ((Uint) (source[2] & ((byte) 0x3F)));
  1391. } else if (((*source) & ((byte) 0xF8)) == 0xF0) {
  1392. unipoint =
  1393. (((Uint) ((*source) & ((byte) 0x7))) << 18) |
  1394. (((Uint) (source[1] & ((byte) 0x3F))) << 12) |
  1395. (((Uint) (source[2] & ((byte) 0x3F))) << 6) |
  1396. ((Uint) (source[3] & ((byte) 0x3F)));
  1397. } else {
  1398. /* ignore 2#10XXXXXX */
  1399. continue;
  1400. }
  1401. if (numpoints) {
  1402. handle_potential_norm(&hp,savepoints,&numpoints,unipoint,&ret);
  1403. continue;
  1404. }
  1405. /* We are not building up any normalizations yet, look that we shouldn't start... */
  1406. if (is_candidate(unipoint)) {
  1407. handle_first_norm(savepoints,&numpoints,unipoint);
  1408. continue;
  1409. }
  1410. ret = CONS(hp,make_small(unipoint),ret);
  1411. hp += 2;
  1412. }
  1413. /* so, we'we looped to the beginning, do we have anything saved? */
  1414. if (numpoints) {
  1415. cleanup_norm(&hp,savepoints,numpoints,&ret);
  1416. }
  1417. if (hp_end != hp) {
  1418. HRelease(p,hp_end,hp);
  1419. }
  1420. return ret;
  1421. }
  1422. /*
  1423. * The last step of characters_to_list, build a list from the buffer 'bytes' (created in the same way
  1424. * as for characters_to_utf8). All sizes are known in advance and most data will be held in a
  1425. * "magic binary" during trapping.
  1426. */
  1427. static BIF_RETTYPE finalize_list_to_list(Process *p,
  1428. byte *bytes,
  1429. Eterm rest,
  1430. Uint num_processed_bytes,
  1431. Uint num_bytes_to_process,
  1432. Uint num_resulting_chars,
  1433. int state, int left,
  1434. Eterm tail)
  1435. {
  1436. Uint num_built; /* characters */
  1437. Uint num_eaten; /* bytes */
  1438. Eterm *hp;
  1439. Eterm converted,ret;
  1440. if (!num_bytes_to_process) {
  1441. converted = tail;
  1442. } else {
  1443. num_built = 0;
  1444. num_eaten = 0;
  1445. converted = do_utf8_to_list(p, num_resulting_chars,
  1446. bytes, num_bytes_to_process,
  1447. left, &num_built, &num_eaten, tail);
  1448. cost_to_proc(p,num_built);
  1449. if (num_built != num_resulting_chars) { /* work left to do */
  1450. RestartContext rc;
  1451. rc.num_resulting_chars = num_resulting_chars - num_built;
  1452. rc.num_bytes_to_process = num_bytes_to_process - num_eaten;
  1453. rc.num_processed_bytes = num_processed_bytes + num_eaten;
  1454. rc.state = state;
  1455. rc.bytes = bytes;
  1456. BUMP_ALL_REDS(p);
  1457. BIF_TRAP3(&characters_to_list_trap_2_exp, p,
  1458. make_magic_bin_for_restart(p, &rc), rest, converted);
  1459. }
  1460. }
  1461. /*
  1462. * OK, no more trapping, let's get rid of the temporary array...
  1463. */
  1464. free_restart(bytes);
  1465. if (state == ERTS_UTF8_INCOMPLETE) {
  1466. hp = HAlloc(p,4);
  1467. ret = TUPLE3(hp,am_incomplete,converted,rest);
  1468. } else if (state == ERTS_UTF8_ERROR) {
  1469. hp = HAlloc(p,4);
  1470. ret = TUPLE3(hp,am_error,converted,rest);
  1471. } else {
  1472. ret = converted;
  1473. }
  1474. BIF_RET(ret);
  1475. }
  1476. static BIF_RETTYPE characters_to_list_trap_2(BIF_ALIST_3)
  1477. {
  1478. RestartContext *rc;
  1479. byte *bytes;
  1480. rc = get_rc_from_bin(BIF_ARG_1);
  1481. bytes = rc->bytes;
  1482. rc->bytes = NULL; /* Don't want this freed just yet... */
  1483. return finalize_list_to_list(BIF_P, bytes, BIF_ARG_2, rc->num_processed_bytes,
  1484. rc->num_bytes_to_process, rc->num_resulting_chars,
  1485. rc->state, allowed_iterations(BIF_P), BIF_ARG_3);
  1486. }
  1487. /*
  1488. * Hooks into the process of decoding a binary depending on state.
  1489. * If last_state is ERTS_UTF8_ANALYZE_MORE, num_bytes_to_process
  1490. * and num_resulting_chars will grow
  1491. * until we're done analyzing the binary. Then we'll eat
  1492. * the bytes to process, lowering num_bytes_to_process and num_resulting_chars,
  1493. * while increasing num_processed_bytes until we're done. the state
  1494. * indicates how to return (error, incomplete or ok) in this stage.
  1495. * note that num_processed_bytes and num_bytes_to_process will make up the
  1496. * length of the binary part to process, not necessarily the length of the
  1497. * whole binary (if there are errors or an incomplete tail).
  1498. *
  1499. * Analyzing happens from the beginning of the binary towards the end,
  1500. * while result is built from the end of the analyzed/accepted part
  1501. * towards the beginning.
  1502. *
  1503. * Note that this routine is *only* called when original input was a plain utf8 binary,
  1504. * otherwise the rest and the sizes are known in advance, so finalize_list_to_list is
  1505. * used to build the resulting list (no analyzing needed).
  1506. */
  1507. static BIF_RETTYPE do_bif_utf8_to_list(Process *p,
  1508. Eterm orig_bin,
  1509. Uint num_processed_bytes,
  1510. Uint num_bytes_to_process,
  1511. Uint num_resulting_chars,
  1512. int state,
  1513. Eterm tail)
  1514. {
  1515. int left;
  1516. Uint bitoffs;
  1517. Uint bitsize;
  1518. Uint size;
  1519. byte *bytes;
  1520. Eterm converted = NIL;
  1521. Eterm rest = NIL;
  1522. Eterm *hp;
  1523. Eterm ret;
  1524. byte *temp_alloc = NULL;
  1525. byte *endpos;
  1526. Uint numchar;
  1527. Uint b_sz; /* size of the non analyzed tail */
  1528. Uint num_built; /* characters */
  1529. Uint num_eaten; /* bytes */
  1530. ERTS_GET_BINARY_BYTES(orig_bin, bytes, bitoffs, bitsize);
  1531. if (bitsize != 0) {
  1532. converted = NIL;
  1533. rest = orig_bin;
  1534. goto error_return;
  1535. }
  1536. if (bitoffs != 0) {
  1537. bytes = erts_get_aligned_binary_bytes(orig_bin, &temp_alloc);
  1538. }
  1539. size = binary_size(orig_bin);
  1540. left = allowed_iterations(p);
  1541. if (state == ERTS_UTF8_ANALYZE_MORE) {
  1542. state = erts_analyze_utf8(bytes + num_bytes_to_process,
  1543. size - num_bytes_to_process,
  1544. &endpos,&numchar,&left);
  1545. cost_to_proc(p,numchar);
  1546. num_resulting_chars += numchar;
  1547. num_bytes_to_process = endpos - bytes;
  1548. if (state == ERTS_UTF8_ANALYZE_MORE) {
  1549. Eterm epos = erts_make_integer(num_bytes_to_process,p);
  1550. Eterm enumchar = erts_make_integer(num_resulting_chars,p);
  1551. erts_free_aligned_binary_bytes(temp_alloc);
  1552. BUMP_ALL_REDS(p);
  1553. BIF_TRAP3(&characters_to_list_trap_3_exp, p, orig_bin, epos,
  1554. enumchar);
  1555. }
  1556. }
  1557. /*
  1558. * If we're here, we have everything analyzed and are instead building
  1559. */
  1560. if (!num_bytes_to_process) {
  1561. converted = tail;
  1562. } else {
  1563. num_built = 0;
  1564. num_eaten = 0;
  1565. converted = do_utf8_to_list(p, num_resulting_chars,
  1566. bytes, num_bytes_to_process,
  1567. left, &num_built, &num_eaten, tail);
  1568. cost_to_proc(p,num_built);
  1569. if (num_built != num_resulting_chars) { /* work left to do */
  1570. Eterm newnum_resulting_chars =
  1571. erts_make_integer(num_resulting_chars - num_built,p);
  1572. Eterm newnum_bytes_to_process =
  1573. erts_make_integer(num_bytes_to_process - num_eaten,p);
  1574. Eterm newnum_processed_bytes =
  1575. erts_make_integer(num_processed_bytes + num_eaten,p);
  1576. Eterm traptuple;
  1577. hp = HAlloc(p,7);
  1578. traptuple = TUPLE6(hp,orig_bin,newnum_processed_bytes,
  1579. newnum_bytes_to_process,
  1580. newnum_resulting_chars,
  1581. make_small(state),
  1582. converted);
  1583. BUMP_ALL_REDS(p);
  1584. erts_free_aligned_binary_bytes(temp_alloc);
  1585. BIF_TRAP1(&characters_to_list_trap_4_exp,p,traptuple);
  1586. }
  1587. }
  1588. /*
  1589. * OK, no more trapping, let's build rest binary if there should
  1590. * be one.
  1591. */
  1592. b_sz = size - (num_bytes_to_process + num_processed_bytes);
  1593. if (b_sz) {
  1594. ErlSubBin *sb;
  1595. Eterm orig;
  1596. Uint offset;
  1597. ASSERT(state != ERTS_UTF8_OK);
  1598. hp = HAlloc(p, ERL_SUB_BIN_SIZE);
  1599. sb = (ErlSubBin *) hp;
  1600. ERTS_GET_REAL_BIN(orig_bin, orig, offset, bitoffs, bitsize);
  1601. sb->thing_word = HEADER_SUB_BIN;
  1602. sb->size = b_sz;
  1603. sb->offs = num_bytes_to_process + num_processed_bytes;
  1604. sb->orig = orig;
  1605. sb->bitoffs = bitoffs;
  1606. sb->bitsize = bitsize;
  1607. sb->is_writable = 0;
  1608. rest = make_binary(sb);
  1609. }
  1610. /* Done */
  1611. if (state == ERTS_UTF8_INCOMPLETE) {
  1612. if (check_leftovers(bytes + num_bytes_to_process + num_processed_bytes,
  1613. b_sz) != 0) {
  1614. goto error_return;
  1615. }
  1616. hp = HAlloc(p,4);
  1617. ret = TUPLE3(hp,am_incomplete,converted,rest);
  1618. } else if (state == ERTS_UTF8_ERROR) {
  1619. error_return:
  1620. hp = HAlloc(p,4);
  1621. ret = TUPLE3(hp,am_error,converted,rest);
  1622. } else {
  1623. ret = converted;
  1624. }
  1625. erts_free_aligned_binary_bytes(temp_alloc);
  1626. BIF_RET(ret);
  1627. }
  1628. /*
  1629. * This is called when there's still analyzing left to do,
  1630. * we only reach this if original input was a binary.
  1631. */
  1632. static BIF_RETTYPE characters_to_list_trap_3(BIF_ALIST_3)
  1633. {
  1634. Uint num_bytes_to_process;
  1635. Uint num_resulting_chars;
  1636. term_to_Uint(BIF_ARG_2, &num_bytes_to_process); /* The number of already
  1637. analyzed and accepted
  1638. bytes */
  1639. term_to_Uint(BIF_ARG_3, &num_resulting_chars); /* The number of chars
  1640. procuced by the
  1641. already analyzed
  1642. part of the binary */
  1643. /*erts_printf("Trap: %T, %T, %T\n",BIF_ARG_1, BIF_ARG_2, BIF_ARG_3);*/
  1644. return do_bif_utf8_to_list(BIF_P,
  1645. BIF_ARG_1, /* the binary */
  1646. 0U, /* nothing processed yet */
  1647. num_bytes_to_process,
  1648. num_resulting_chars,
  1649. ERTS_UTF8_ANALYZE_MORE, /* always this state here */
  1650. NIL); /* Nothing built -> no tail yet */
  1651. }
  1652. /*
  1653. * This is called when analyzing is done and we are trapped during building,
  1654. * we only reach this if original input was a binary.
  1655. */
  1656. static BIF_RETTYPE characters_to_list_trap_4(BIF_ALIST_1)
  1657. {
  1658. Uint num_processed_bytes;
  1659. Uint num_bytes_to_process;
  1660. Uint num_resulting_chars;
  1661. Eterm orig_bin, tail;
  1662. int last_state;
  1663. Eterm *tplp = tuple_val(BIF_ARG_1);
  1664. orig_bin = tplp[1];
  1665. term_to_Uint(tplp[2], &num_processed_bytes);
  1666. term_to_Uint(tplp[3], &num_bytes_to_process);
  1667. term_to_Uint(tplp[4], &num_resulting_chars);
  1668. last_state = (int) signed_val(tplp[5]);
  1669. tail = tplp[6];
  1670. /*erts_printf("Trap: {%T, %lu, %lu, %lu, %d, %T}\n",
  1671. orig_bin, num_processed_bytes, num_bytes_to_process,
  1672. num_resulting_chars, last_state, tail);*/
  1673. return do_bif_utf8_to_list(BIF_P,
  1674. orig_bin, /* The whole binary */
  1675. num_processed_bytes, /* Number of bytes
  1676. already processed */
  1677. num_bytes_to_process, /* Bytes left to proc. */
  1678. num_resulting_chars, /* Num chars left to
  1679. build */
  1680. last_state, /* The current state

Large files files are truncated, but you can click here to view the full file