PageRenderTime 37ms CodeModel.GetById 21ms 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
  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
  1681. (never ANALYZE_MORE)*/
  1682. tail); /* The already built
  1683. tail */
  1684. }
  1685. /*
  1686. * This is only used when characters are a plain unicode (utf8) binary.
  1687. * Instead of building an utf8 buffer, we analyze the binary given and use that.
  1688. */
  1689. static BIF_RETTYPE utf8_to_list(BIF_ALIST_1)
  1690. {
  1691. if (!is_binary(BIF_ARG_1) || aligned_binary_size(BIF_ARG_1) < 0) {
  1692. BIF_ERROR(BIF_P,BADARG);
  1693. }
  1694. return do_bif_utf8_to_list(BIF_P, BIF_ARG_1, 0U, 0U, 0U,
  1695. ERTS_UTF8_ANALYZE_MORE,NIL);
  1696. }
  1697. BIF_RETTYPE atom_to_binary_2(BIF_ALIST_2)
  1698. {
  1699. Atom* ap;
  1700. if (is_not_atom(BIF_ARG_1)) {
  1701. goto error;
  1702. }
  1703. ap = atom_tab(atom_val(BIF_ARG_1));
  1704. if (BIF_ARG_2 == am_latin1) {
  1705. BIF_RET(new_binary(BIF_P, ap->name, ap->len));
  1706. } else if (BIF_ARG_2 == am_utf8 || BIF_ARG_2 == am_unicode) {
  1707. int bin_size = 0;
  1708. int i;
  1709. Eterm bin_term;
  1710. byte* bin_p;
  1711. for (i = 0; i < ap->len; i++) {
  1712. bin_size += (ap->name[i] >= 0x80) ? 2 : 1;
  1713. }
  1714. if (bin_size == ap->len) {
  1715. BIF_RET(new_binary(BIF_P, ap->name, ap->len));
  1716. }
  1717. bin_term = new_binary(BIF_P, 0, bin_size);
  1718. bin_p = binary_bytes(bin_term);
  1719. for (i = 0; i < ap->len; i++) {
  1720. byte b = ap->name[i];
  1721. if (b < 0x80) {
  1722. *bin_p++ = b;
  1723. } else {
  1724. *bin_p++ = 0xC0 | (b >> 6);
  1725. *bin_p++ = 0x80 | (b & 0x3F);
  1726. }
  1727. }
  1728. BIF_RET(bin_term);
  1729. } else {
  1730. error:
  1731. BIF_ERROR(BIF_P, BADARG);
  1732. }
  1733. }
  1734. static BIF_RETTYPE
  1735. binary_to_atom(Process* p, Eterm bin, Eterm enc, int must_exist)
  1736. {
  1737. byte* bytes;
  1738. byte *temp_alloc = NULL;
  1739. Uint bin_size;
  1740. if ((bytes = erts_get_aligned_binary_bytes(bin, &temp_alloc)) == 0) {
  1741. BIF_ERROR(p, BADARG);
  1742. }
  1743. bin_size = binary_size(bin);
  1744. if (enc == am_latin1) {
  1745. Eterm a;
  1746. if (bin_size > MAX_ATOM_LENGTH) {
  1747. system_limit:
  1748. erts_free_aligned_binary_bytes(temp_alloc);
  1749. BIF_ERROR(p, SYSTEM_LIMIT);
  1750. }
  1751. if (!must_exist) {
  1752. a = am_atom_put((char *)bytes, bin_size);
  1753. erts_free_aligned_binary_bytes(temp_alloc);
  1754. BIF_RET(a);
  1755. } else if (erts_atom_get((char *)bytes, bin_size, &a)) {
  1756. erts_free_aligned_binary_bytes(temp_alloc);
  1757. BIF_RET(a);
  1758. } else {
  1759. goto badarg;
  1760. }
  1761. } else if (enc == am_utf8 || enc == am_unicode) {
  1762. char *buf;
  1763. char *dst;
  1764. int i;
  1765. int num_chars;
  1766. Eterm res;
  1767. if (bin_size > 2*MAX_ATOM_LENGTH) {
  1768. byte* err_pos;
  1769. Uint n;
  1770. int reds_left = bin_size+1; /* Number of reductions left. */
  1771. if (erts_analyze_utf8(bytes, bin_size, &err_pos,
  1772. &n, &reds_left) == ERTS_UTF8_OK) {
  1773. /*
  1774. * Correct UTF-8 encoding, but too many characters to
  1775. * fit in an atom.
  1776. */
  1777. goto system_limit;
  1778. } else {
  1779. /*
  1780. * Something wrong in the UTF-8 encoding or Unicode code
  1781. * points > 255.
  1782. */
  1783. goto badarg;
  1784. }
  1785. }
  1786. /*
  1787. * Allocate a temporary buffer the same size as the binary,
  1788. * so that we don't need an extra overflow test.
  1789. */
  1790. buf = (char *) erts_alloc(ERTS_ALC_T_TMP, bin_size);
  1791. dst = buf;
  1792. for (i = 0; i < bin_size; i++) {
  1793. int c = bytes[i];
  1794. if (c < 0x80) {
  1795. *dst++ = c;
  1796. } else if (i < bin_size-1) {
  1797. int c2;
  1798. if ((c & 0xE0) != 0xC0) {
  1799. goto free_badarg;
  1800. }
  1801. i++;
  1802. c = (c & 0x3F) << 6;
  1803. c2 = bytes[i];
  1804. if ((c2 & 0xC0) != 0x80) {
  1805. goto free_badarg;
  1806. }
  1807. c = c | (c2 & 0x3F);
  1808. if (0x80 <= c && c < 256) {
  1809. *dst++ = c;
  1810. } else {
  1811. goto free_badarg;
  1812. }
  1813. } else {
  1814. free_badarg:
  1815. erts_free(ERTS_ALC_T_TMP, (void *) buf);
  1816. goto badarg;
  1817. }
  1818. }
  1819. num_chars = dst - buf;
  1820. if (num_chars > MAX_ATOM_LENGTH) {
  1821. erts_free(ERTS_ALC_T_TMP, (void *) buf);
  1822. goto system_limit;
  1823. }
  1824. if (!must_exist) {
  1825. res = am_atom_put(buf, num_chars);
  1826. erts_free(ERTS_ALC_T_TMP, (void *) buf);
  1827. erts_free_aligned_binary_bytes(temp_alloc);
  1828. BIF_RET(res);
  1829. } else {
  1830. int exists = erts_atom_get(buf, num_chars, &res);
  1831. erts_free(ERTS_ALC_T_TMP, (void *) buf);
  1832. if (exists) {
  1833. erts_free_aligned_binary_bytes(temp_alloc);
  1834. BIF_RET(res);
  1835. } else {
  1836. goto badarg;
  1837. }
  1838. }
  1839. } else {
  1840. badarg:
  1841. erts_free_aligned_binary_bytes(temp_alloc);
  1842. BIF_ERROR(p, BADARG);
  1843. }
  1844. }
  1845. BIF_RETTYPE binary_to_atom_2(BIF_ALIST_2)
  1846. {
  1847. return binary_to_atom(BIF_P, BIF_ARG_1, BIF_ARG_2, 0);
  1848. }
  1849. BIF_RETTYPE binary_to_existing_atom_2(BIF_ALIST_2)
  1850. {
  1851. return binary_to_atom(BIF_P, BIF_ARG_1, BIF_ARG_2, 1);
  1852. }
  1853. /**********************************************************
  1854. * Simpler non-interruptable routines for UTF-8 and
  1855. * Windowish UTF-16 (restricted)
  1856. **********************************************************/
  1857. /*
  1858. * This function is the heart of the Unicode support for
  1859. * open_port - spawn_executable. It converts both the name
  1860. * of the executable and the arguments according to the same rules
  1861. * as for filename conversion. That means as if your arguments are
  1862. * to be raw, you supply binaries, else unicode characters are allowed up to
  1863. * the encoding maximum (256 of the unicode max).
  1864. * Depending on the filename encoding standard, the vector is then
  1865. * converted to whatever is used, which might mean win_utf16 if on windows.
  1866. * Do not peek into the argument vector or filenam with ordinary
  1867. * string routines, that will certainly fail on some OS.
  1868. */
  1869. char *erts_convert_filename_to_native(Eterm name, ErtsAlcType_t alloc_type, int allow_empty)
  1870. {
  1871. int encoding = erts_get_native_filename_encoding();
  1872. char* name_buf = NULL;
  1873. if (is_atom(name) || is_list(name) || (allow_empty && is_nil(name))) {
  1874. Sint need;
  1875. if ((need = erts_native_filename_need(name,encoding)) < 0) {
  1876. return NULL;
  1877. }
  1878. if (encoding == ERL_FILENAME_WIN_WCHAR) {
  1879. need += 2;
  1880. } else {
  1881. ++need;
  1882. }
  1883. name_buf = (char *) erts_alloc(alloc_type, need);
  1884. erts_native_filename_put(name,encoding,(byte *)name_buf);
  1885. name_buf[need-1] = 0;
  1886. if (encoding == ERL_FILENAME_WIN_WCHAR) {
  1887. name_buf[need-2] = 0;
  1888. }
  1889. } else if (is_binary(name)) {
  1890. byte *temp_alloc = NULL;
  1891. byte *bytes;
  1892. byte *err_pos;
  1893. Uint size,num_chars;
  1894. size = binary_size(name);
  1895. bytes = erts_get_aligned_binary_bytes(name, &temp_alloc);
  1896. if (encoding != ERL_FILENAME_WIN_WCHAR) {
  1897. /*Add 0 termination only*/
  1898. name_buf = (char *) erts_alloc(alloc_type, size+1);
  1899. memcpy(name_buf,bytes,size);
  1900. name_buf[size]=0;
  1901. } else if (erts_analyze_utf8(bytes,size,&err_pos,&num_chars,NULL) != ERTS_UTF8_OK ||
  1902. erts_get_user_requested_filename_encoding() == ERL_FILENAME_LATIN1) {
  1903. byte *p;
  1904. /* What to do now? Maybe latin1, so just take byte for byte instead */
  1905. name_buf = (char *) erts_alloc(alloc_type, (size+1)*2);
  1906. p = (byte *) name_buf;
  1907. while (size--) {
  1908. *p++ = *bytes++;
  1909. *p++ = 0;
  1910. }
  1911. *p++ = 0;
  1912. *p++ = 0;
  1913. } else { /* WIN_WCHAR and valid UTF8 */
  1914. name_buf = (char *) erts_alloc(alloc_type, (num_chars+1)*2);
  1915. erts_copy_utf8_to_utf16_little((byte *) name_buf, bytes, num_chars);
  1916. name_buf[num_chars*2] = 0;
  1917. name_buf[num_chars*2+1] = 0;
  1918. }
  1919. erts_free_aligned_binary_bytes(temp_alloc);
  1920. } else {
  1921. return NULL;
  1922. }
  1923. return name_buf;
  1924. }
  1925. Sint erts_native_filename_need(Eterm ioterm, int encoding)
  1926. {
  1927. Eterm *objp;
  1928. Eterm obj;
  1929. DECLARE_ESTACK(stack);
  1930. Sint need = 0;
  1931. if (is_atom(ioterm)) {
  1932. Atom* ap;
  1933. int i;
  1934. ap = atom_tab(atom_val(ioterm));
  1935. switch (encoding) {
  1936. case ERL_FILENAME_LATIN1:
  1937. need = ap->len;
  1938. break;
  1939. case ERL_FILENAME_UTF8_MAC:
  1940. case ERL_FILENAME_UTF8:
  1941. for (i = 0; i < ap->len; i++) {
  1942. need += (ap->name[i] >= 0x80) ? 2 : 1;
  1943. }
  1944. break;
  1945. case ERL_FILENAME_WIN_WCHAR:
  1946. need = 2*(ap->len);
  1947. break;
  1948. default:
  1949. need = -1;
  1950. }
  1951. DESTROY_ESTACK(stack);
  1952. return need;
  1953. }
  1954. if (is_nil(ioterm)) {
  1955. DESTROY_ESTACK(stack);
  1956. return need;
  1957. }
  1958. if (!is_list(ioterm)) {
  1959. DESTROY_ESTACK(stack);
  1960. return (Sint) -1;
  1961. }
  1962. /* OK a list, needs to be processed in order, handling each flat list-level
  1963. as they occur, just like io_list_to_binary would */
  1964. ESTACK_PUSH(stack,ioterm);
  1965. while (!ESTACK_ISEMPTY(stack)) {
  1966. ioterm = ESTACK_POP(stack);
  1967. if (is_nil(ioterm)) {
  1968. /* ignore empty lists */
  1969. continue;
  1970. }
  1971. if(is_list(ioterm)) {
  1972. L_Again: /* Restart with sublist, old listend was pushed on stack */
  1973. objp = list_val(ioterm);
  1974. obj = CAR(objp);
  1975. for(;;) { /* loop over one flat list of bytes and binaries
  1976. until sublist or list end is encountered */
  1977. if (is_small(obj)) { /* Always small */
  1978. for(;;) {
  1979. Uint x = unsigned_val(obj);
  1980. switch (encoding) {
  1981. case ERL_FILENAME_LATIN1:
  1982. if (x > 255) {
  1983. DESTROY_ESTACK(stack);
  1984. return ((Sint) -1);
  1985. }
  1986. need += 1;
  1987. break;
  1988. case ERL_FILENAME_UTF8_MAC:
  1989. case ERL_FILENAME_UTF8:
  1990. if (x < 0x80) {
  1991. need +=1;
  1992. } else if (x < 0x800) {
  1993. need += 2;
  1994. } else if (x < 0x10000) {
  1995. if ((x >= 0xD800 && x <= 0xDFFF) ||
  1996. (x == 0xFFFE) ||
  1997. (x == 0xFFFF)) { /* Invalid unicode range */
  1998. DESTROY_ESTACK(stack);
  1999. return ((Sint) -1);
  2000. }
  2001. need += 3;
  2002. } else if (x < 0x110000) {
  2003. need += 4;
  2004. } else {
  2005. DESTROY_ESTACK(stack);
  2006. return ((Sint) -1);
  2007. }
  2008. break;
  2009. case ERL_FILENAME_WIN_WCHAR:
  2010. if (x <= 0xffff) {
  2011. need += 2;
  2012. break;
  2013. } /* else fall throug to error */
  2014. default:
  2015. DESTROY_ESTACK(stack);
  2016. return ((Sint) -1);
  2017. }
  2018. /* everything else will give badarg later
  2019. in the process, so we dont check */
  2020. ioterm = CDR(objp);
  2021. if (!is_list(ioterm)) {
  2022. break;
  2023. }
  2024. objp = list_val(ioterm);
  2025. obj = CAR(objp);
  2026. if (!is_small(obj))
  2027. break;
  2028. }
  2029. } else if (is_nil(obj)) {
  2030. ioterm = CDR(objp);
  2031. if (!is_list(ioterm)) {
  2032. break;
  2033. }
  2034. objp = list_val(ioterm);
  2035. obj = CAR(objp);
  2036. } else if (is_list(obj)) {
  2037. /* push rest of list for later processing, start
  2038. again with sublist */
  2039. ESTACK_PUSH(stack,CDR(objp));
  2040. ioterm = obj;
  2041. goto L_Again;
  2042. } else {
  2043. DESTROY_ESTACK(stack);
  2044. return ((Sint) -1);
  2045. }
  2046. if (is_nil(ioterm) || !is_list(ioterm)) {
  2047. break;
  2048. }
  2049. } /* for(;;) */
  2050. } /* is_list(ioterm) */
  2051. if (!is_list(ioterm) && !is_nil(ioterm)) {
  2052. /* inproper list end */
  2053. DESTROY_ESTACK(stack);
  2054. return ((Sint) -1);
  2055. }
  2056. } /* while not estack empty */
  2057. DESTROY_ESTACK(stack);
  2058. return need;
  2059. }
  2060. void erts_native_filename_put(Eterm ioterm, int encoding, byte *p)
  2061. {
  2062. Eterm *objp;
  2063. Eterm obj;
  2064. DECLARE_ESTACK(stack);
  2065. if (is_atom(ioterm)) {
  2066. Atom* ap;
  2067. int i;
  2068. ap = atom_tab(atom_val(ioterm));
  2069. switch (encoding) {
  2070. case ERL_FILENAME_LATIN1:
  2071. for (i = 0; i < ap->len; i++) {
  2072. *p++ = ap->name[i];
  2073. }
  2074. break;
  2075. case ERL_FILENAME_UTF8_MAC:
  2076. case ERL_FILENAME_UTF8:
  2077. for (i = 0; i < ap->len; i++) {
  2078. if(ap->name[i] < 0x80) {
  2079. *p++ = ap->name[i];
  2080. } else {
  2081. *p++ = (((ap->name[i]) >> 6) | ((byte) 0xC0));
  2082. *p++ = (((ap->name[i]) & 0x3F) | ((byte) 0x80));
  2083. }
  2084. }
  2085. break;
  2086. case ERL_FILENAME_WIN_WCHAR:
  2087. for (i = 0; i < ap->len; i++) {
  2088. /* Little endian */
  2089. *p++ = ap->name[i];
  2090. *p++ = 0;
  2091. }
  2092. break;
  2093. default:
  2094. ASSERT(0);
  2095. }
  2096. DESTROY_ESTACK(stack);
  2097. return;
  2098. }
  2099. if (is_nil(ioterm)) {
  2100. DESTROY_ESTACK(stack);
  2101. return;
  2102. }
  2103. ASSERT(is_list(ioterm));
  2104. /* OK a list, needs to be processed in order, handling each flat list-level
  2105. as they occur, just like io_list_to_binary would */
  2106. ESTACK_PUSH(stack,ioterm);
  2107. while (!ESTACK_ISEMPTY(stack)) {
  2108. ioterm = ESTACK_POP(stack);
  2109. if (is_nil(ioterm)) {
  2110. /* ignore empty lists */
  2111. continue;
  2112. }
  2113. if(is_list(ioterm)) {
  2114. L_Again: /* Restart with sublist, old listend was pushed on stack */
  2115. objp = list_val(ioterm);
  2116. obj = CAR(objp);
  2117. for(;;) { /* loop over one flat list of bytes and binaries
  2118. until sublist or list end is encountered */
  2119. if (is_small(obj)) { /* Always small */
  2120. for(;;) {
  2121. Uint x = unsigned_val(obj);
  2122. switch (encoding) {
  2123. case ERL_FILENAME_LATIN1:
  2124. ASSERT( x < 256);
  2125. *p++ = (byte) x;
  2126. break;
  2127. case ERL_FILENAME_UTF8_MAC:
  2128. case ERL_FILENAME_UTF8:
  2129. if (x < 0x80) {
  2130. *p++ = (byte) x;
  2131. }
  2132. else if (x < 0x800) {
  2133. *p++ = (((byte) (x >> 6)) |
  2134. ((byte) 0xC0));
  2135. *p++ = (((byte) (x & 0x3F)) |
  2136. ((byte) 0x80));
  2137. } else if (x < 0x10000) {
  2138. ASSERT(!((x >= 0xD800 && x <= 0xDFFF) ||
  2139. (x == 0xFFFE) ||
  2140. (x == 0xFFFF)));
  2141. *p++ = (((byte) (x >> 12)) |
  2142. ((byte) 0xE0));
  2143. *p++ = ((((byte) (x >> 6)) & 0x3F) |
  2144. ((byte) 0x80));
  2145. *p++ = (((byte) (x & 0x3F)) |
  2146. ((byte) 0x80));
  2147. } else {
  2148. ASSERT(x < 0x110000);
  2149. *p++ = (((byte) (x >> 18)) |
  2150. ((byte) 0xF0));
  2151. *p++ = ((((byte) (x >> 12)) & 0x3F) |
  2152. ((byte) 0x80));
  2153. *p++ = ((((byte) (x >> 6)) & 0x3F) |
  2154. ((byte) 0x80));
  2155. *p++ = (((byte) (x & 0x3F)) |
  2156. ((byte) 0x80));
  2157. }
  2158. break;
  2159. case ERL_FILENAME_WIN_WCHAR:
  2160. ASSERT(x <= 0xFFFF);
  2161. *p++ = (byte) (x & 0xFFU);
  2162. *p++ = (byte) ((x >> 8) & 0xFFU);
  2163. break;
  2164. default:
  2165. ASSERT(0);
  2166. }
  2167. /* everything else will give badarg later
  2168. in the process, so we dont check */
  2169. ioterm = CDR(objp);
  2170. if (!is_list(ioterm)) {
  2171. break;
  2172. }
  2173. objp = list_val(ioterm);
  2174. obj = CAR(objp);
  2175. if (!is_small(obj))
  2176. break;
  2177. }
  2178. } else if (is_nil(obj)) {
  2179. ioterm = CDR(objp);
  2180. if (!is_list(ioterm)) {
  2181. break;
  2182. }
  2183. objp = list_val(ioterm);
  2184. obj = CAR(objp);
  2185. } else if (is_list(obj)) {
  2186. /* push rest of list for later processing, start
  2187. again with sublist */
  2188. ESTACK_PUSH(stack,CDR(objp));
  2189. ioterm = obj;
  2190. goto L_Again;
  2191. } else {
  2192. ASSERT(0);
  2193. }
  2194. if (is_nil(ioterm) || !is_list(ioterm)) {
  2195. break;
  2196. }
  2197. } /* for(;;) */
  2198. } /* is_list(ioterm) */
  2199. ASSERT(is_list(ioterm) || is_nil(ioterm));
  2200. } /* while not estack empty */
  2201. DESTROY_ESTACK(stack);
  2202. return;
  2203. }
  2204. void erts_copy_utf8_to_utf16_little(byte *target, byte *bytes, int num_chars)
  2205. {
  2206. Uint unipoint;
  2207. while (num_chars--) {
  2208. if (((*bytes) & ((byte) 0x80)) == 0) {
  2209. unipoint = (Uint) *bytes;
  2210. ++bytes;
  2211. } else if (((*bytes) & ((byte) 0xE0)) == 0xC0) {
  2212. unipoint =
  2213. (((Uint) ((*bytes) & ((byte) 0x1F))) << 6) |
  2214. ((Uint) (bytes[1] & ((byte) 0x3F)));
  2215. bytes += 2;
  2216. } else if (((*bytes) & ((byte) 0xF0)) == 0xE0) {
  2217. unipoint =
  2218. (((Uint) ((*bytes) & ((byte) 0xF))) << 12) |
  2219. (((Uint) (bytes[1] & ((byte) 0x3F))) << 6) |
  2220. ((Uint) (bytes[2] & ((byte) 0x3F)));
  2221. bytes +=3;
  2222. } else if (((*bytes) & ((byte) 0xF8)) == 0xF0) {
  2223. unipoint =
  2224. (((Uint) ((*bytes) & ((byte) 0x7))) << 18) |
  2225. (((Uint) (bytes[1] & ((byte) 0x3F))) << 12) |
  2226. (((Uint) (bytes[2] & ((byte) 0x3F))) << 6) |
  2227. ((Uint) (bytes[3] & ((byte) 0x3F)));
  2228. bytes += 4;
  2229. } else {
  2230. erl_exit(1,"Internal unicode error in prim_file:internal_name2native/1");
  2231. }
  2232. *target++ = (byte) (unipoint & 0xFF);
  2233. *target++ = (byte) ((unipoint >> 8) & 0xFF);
  2234. }
  2235. }
  2236. /*
  2237. * This internal bif converts a filename to whatever format is suitable for the file driver
  2238. * It also adds zero termination so that prim_file needn't bother with the character encoding
  2239. * of the file driver
  2240. */
  2241. BIF_RETTYPE prim_file_internal_name2native_1(BIF_ALIST_1)
  2242. {
  2243. int encoding = erts_get_native_filename_encoding();
  2244. Sint need;
  2245. Eterm bin_term;
  2246. byte* bin_p;
  2247. /* Prim file explicitly does not allow atoms, although we could
  2248. very well cope with it. Instead of letting 'file' handle them,
  2249. it would probably be more efficient to handle them here. Subject to
  2250. change in R15. */
  2251. if (is_atom(BIF_ARG_1)) {
  2252. BIF_ERROR(BIF_P,BADARG);
  2253. }
  2254. if (is_binary(BIF_ARG_1)) {
  2255. byte *temp_alloc = NULL;
  2256. byte *bytes;
  2257. byte *err_pos;
  2258. Uint size,num_chars;
  2259. /* Uninterpreted encoding except if windows widechar, in case we convert from
  2260. utf8 to win_wchar */
  2261. size = binary_size(BIF_ARG_1);
  2262. bytes = erts_get_aligned_binary_bytes(BIF_ARG_1, &temp_alloc);
  2263. if (encoding != ERL_FILENAME_WIN_WCHAR) {
  2264. /*Add 0 termination only*/
  2265. bin_term = new_binary(BIF_P, NULL, size+1);
  2266. bin_p = binary_bytes(bin_term);
  2267. memcpy(bin_p,bytes,size);
  2268. bin_p[size]=0;
  2269. erts_free_aligned_binary_bytes(temp_alloc);
  2270. BIF_RET(bin_term);
  2271. }
  2272. /* In a wchar world, the emulator flags only affect how
  2273. binaries are interpreted when sent from the user. */
  2274. /* Determine real length and create a new binary */
  2275. if (erts_analyze_utf8(bytes,size,&err_pos,&num_chars,NULL) != ERTS_UTF8_OK ||
  2276. erts_get_user_requested_filename_encoding() == ERL_FILENAME_LATIN1) {
  2277. /* What to do now? Maybe latin1, so just take byte for byte instead */
  2278. bin_term = new_binary(BIF_P, 0, (size+1)*2);
  2279. bin_p = binary_bytes(bin_term);
  2280. while (size--) {
  2281. *bin_p++ = *bytes++;
  2282. *bin_p++ = 0;
  2283. }
  2284. *bin_p++ = 0;
  2285. *bin_p++ = 0;
  2286. erts_free_aligned_binary_bytes(temp_alloc);
  2287. BIF_RET(bin_term);
  2288. }
  2289. /* OK, UTF8 ok, number of characters is in num_chars */
  2290. bin_term = new_binary(BIF_P, 0, (num_chars+1)*2);
  2291. bin_p = binary_bytes(bin_term);
  2292. erts_copy_utf8_to_utf16_little(bin_p, bytes, num_chars);
  2293. /* zero termination */
  2294. bin_p[num_chars*2] = 0;
  2295. bin_p[num_chars*2+1] = 0;
  2296. erts_free_aligned_binary_bytes(temp_alloc);
  2297. BIF_RET(bin_term);
  2298. } /* binary */
  2299. if ((need = erts_native_filename_need(BIF_ARG_1,encoding)) < 0) {
  2300. BIF_ERROR(BIF_P,BADARG);
  2301. }
  2302. if (encoding == ERL_FILENAME_WIN_WCHAR) {
  2303. need += 2;
  2304. } else {
  2305. ++need;
  2306. }
  2307. bin_term = new_binary(BIF_P, 0, need);
  2308. bin_p = binary_bytes(bin_term);
  2309. erts_native_filename_put(BIF_ARG_1,encoding,bin_p);
  2310. bin_p[need-1] = 0;
  2311. if (encoding == ERL_FILENAME_WIN_WCHAR) {
  2312. bin_p[need-2] = 0;
  2313. }
  2314. BIF_RET(bin_term);
  2315. }
  2316. BIF_RETTYPE prim_file_internal_native2name_1(BIF_ALIST_1)
  2317. {
  2318. Eterm real_bin;
  2319. Uint offset;
  2320. Uint size,num_chars;
  2321. Uint bitsize;
  2322. Uint bitoffs;
  2323. Eterm *hp;
  2324. byte *temp_alloc = NULL;
  2325. byte *bytes;
  2326. byte *err_pos;
  2327. Uint num_built; /* characters */
  2328. Uint num_eaten; /* bytes */
  2329. Eterm ret;
  2330. int mac = 0;
  2331. if (is_not_binary(BIF_ARG_1)) {
  2332. BIF_ERROR(BIF_P,BADARG);
  2333. }
  2334. size = binary_size(BIF_ARG_1);
  2335. ERTS_GET_REAL_BIN(BIF_ARG_1, real_bin, offset, bitoffs, bitsize);
  2336. if (bitsize != 0) {
  2337. BIF_ERROR(BIF_P,BADARG);
  2338. }
  2339. if (size == 0) {
  2340. BIF_RET(NIL);
  2341. }
  2342. switch (erts_get_native_filename_encoding()) {
  2343. case ERL_FILENAME_LATIN1:
  2344. hp = HAlloc(BIF_P, 2 * size);
  2345. bytes = binary_bytes(real_bin)+offset;
  2346. BIF_RET(erts_bin_bytes_to_list(NIL, hp, bytes, size, bitoffs));
  2347. case ERL_FILENAME_UTF8_MAC:
  2348. mac = 1;
  2349. case ERL_FILENAME_UTF8:
  2350. bytes = erts_get_aligned_binary_bytes(BIF_ARG_1, &temp_alloc);
  2351. if (erts_analyze_utf8(bytes,size,&err_pos,&num_chars,NULL) != ERTS_UTF8_OK) {
  2352. erts_free_aligned_binary_bytes(temp_alloc);
  2353. goto noconvert;
  2354. }
  2355. num_built = 0;
  2356. num_eaten = 0;
  2357. if (mac) {
  2358. ret = do_utf8_to_list_normalize(BIF_P, num_chars, bytes, size);
  2359. } else {
  2360. ret = do_utf8_to_list(BIF_P, num_chars, bytes, size, num_chars, &num_built, &num_eaten, NIL);
  2361. }
  2362. erts_free_aligned_binary_bytes(temp_alloc);
  2363. BIF_RET(ret);
  2364. case ERL_FILENAME_WIN_WCHAR:
  2365. bytes = erts_get_aligned_binary_bytes(BIF_ARG_1, &temp_alloc);
  2366. if ((size % 2) != 0) { /* Panic fixup to avoid crashing the emulator */
  2367. size--;
  2368. hp = HAlloc(BIF_P, size+2);
  2369. ret = CONS(hp,make_small((Uint) bytes[size]),NIL);
  2370. hp += 2;
  2371. } else {
  2372. hp = HAlloc(BIF_P, size);
  2373. ret = NIL;
  2374. }
  2375. bytes += size-1;
  2376. while (size > 0) {
  2377. Uint x = ((Uint) *bytes--) << 8;
  2378. x |= ((Uint) *bytes--);
  2379. size -= 2;
  2380. ret = CONS(hp,make_small(x),ret);
  2381. hp += 2;
  2382. }
  2383. erts_free_aligned_binary_bytes(temp_alloc);
  2384. BIF_RET(ret);
  2385. default:
  2386. goto noconvert;
  2387. }
  2388. noconvert:
  2389. BIF_RET(BIF_ARG_1);
  2390. }
  2391. BIF_RETTYPE prim_file_internal_normalize_utf8_1(BIF_ALIST_1)
  2392. {
  2393. Eterm real_bin;
  2394. Uint offset;
  2395. Uint size,num_chars;
  2396. Uint bitsize;
  2397. Uint bitoffs;
  2398. Eterm ret;
  2399. byte *temp_alloc = NULL;
  2400. byte *bytes;
  2401. byte *err_pos;
  2402. if (is_not_binary(BIF_ARG_1)) {
  2403. BIF_ERROR(BIF_P,BADARG);
  2404. }
  2405. size = binary_size(BIF_ARG_1);
  2406. ERTS_GET_REAL_BIN(BIF_ARG_1, real_bin, offset, bitoffs, bitsize);
  2407. if (bitsize != 0) {
  2408. BIF_ERROR(BIF_P,BADARG);
  2409. }
  2410. if (size == 0) {
  2411. BIF_RET(NIL);
  2412. }
  2413. bytes = erts_get_aligned_binary_bytes(BIF_ARG_1, &temp_alloc);
  2414. if (erts_analyze_utf8(bytes,size,&err_pos,&num_chars,NULL) != ERTS_UTF8_OK) {
  2415. erts_free_aligned_binary_bytes(temp_alloc);
  2416. BIF_ERROR(BIF_P,BADARG);
  2417. }
  2418. ret = do_utf8_to_list_normalize(BIF_P, num_chars, bytes, size);
  2419. erts_free_aligned_binary_bytes(temp_alloc);
  2420. BIF_RET(ret);
  2421. }
  2422. BIF_RETTYPE file_native_name_encoding_0(BIF_ALIST_0)
  2423. {
  2424. switch (erts_get_native_filename_encoding()) {
  2425. case ERL_FILENAME_LATIN1:
  2426. BIF_RET(am_latin1);
  2427. case ERL_FILENAME_UTF8_MAC:
  2428. case ERL_FILENAME_UTF8:
  2429. BIF_RET(am_utf8);
  2430. case ERL_FILENAME_WIN_WCHAR:
  2431. if (erts_get_user_requested_filename_encoding() == ERL_FILENAME_LATIN1) {
  2432. BIF_RET(am_latin1);
  2433. } else {
  2434. BIF_RET(am_utf8);
  2435. }
  2436. default:
  2437. BIF_RET(am_undefined);
  2438. }
  2439. }