From 32d13d59910a384ad09682cafc83c7300c96c694 Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Wed, 8 Mar 2017 14:46:24 +0100 Subject: erl_interface: Do not generate atoms on old latin1 ext format Solved by letting ei_encode_atom_as ignore 'to_enc' argument and always encode in UTF8 format. --- lib/erl_interface/src/legacy/erl_eterm.c | 16 +++++++++++----- lib/erl_interface/src/legacy/erl_marshal.c | 13 ++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) (limited to 'lib/erl_interface/src/legacy') diff --git a/lib/erl_interface/src/legacy/erl_eterm.c b/lib/erl_interface/src/legacy/erl_eterm.c index e4b3b49c7d..5153d0f2e7 100644 --- a/lib/erl_interface/src/legacy/erl_eterm.c +++ b/lib/erl_interface/src/legacy/erl_eterm.c @@ -188,14 +188,20 @@ char* erl_atom_ptr_latin1(Erl_Atom_data* a) char* erl_atom_ptr_utf8(Erl_Atom_data* a) { if (a->utf8 == NULL) { - int dlen = a->lenL * 2; /* over estimation */ - a->utf8 = malloc(dlen + 1); - a->lenU = latin1_to_utf8(a->utf8, a->latin1, a->lenL, dlen, NULL); - a->utf8[a->lenU] = '\0'; + erlang_char_encoding enc; + a->lenU = latin1_to_utf8(NULL, a->latin1, a->lenL, a->lenL*2, &enc); + if (enc == ERLANG_ASCII) { + a->utf8 = a->latin1; + } + else { + a->utf8 = malloc(a->lenU + 1); + latin1_to_utf8(a->utf8, a->latin1, a->lenL, a->lenU, NULL); + a->utf8[a->lenU] = '\0'; + } } return a->utf8; - } + int erl_atom_size_latin1(Erl_Atom_data* a) { if (a->latin1 == NULL) { diff --git a/lib/erl_interface/src/legacy/erl_marshal.c b/lib/erl_interface/src/legacy/erl_marshal.c index 527ae0ef8f..b7a8455313 100644 --- a/lib/erl_interface/src/legacy/erl_marshal.c +++ b/lib/erl_interface/src/legacy/erl_marshal.c @@ -175,10 +175,9 @@ static void encode_atom(Erl_Atom_data* a, unsigned char **ext) int ix = 0; if (a->latin1) { ei_encode_atom_len_as((char*)*ext, &ix, a->latin1, a->lenL, - ERLANG_LATIN1, ERLANG_LATIN1); + ERLANG_LATIN1, ERLANG_UTF8); } - else if (ei_encode_atom_len_as((char*)*ext, &ix, a->utf8, a->lenU, - ERLANG_UTF8, ERLANG_LATIN1) < 0) { + else { ei_encode_atom_len_as((char*)*ext, &ix, a->utf8, a->lenU, ERLANG_UTF8, ERLANG_UTF8); } @@ -542,12 +541,8 @@ int erl_term_len(ETERM *ep) static int atom_len_helper(Erl_Atom_data* a) { - if (erl_atom_ptr_latin1(a)) { - return 1 + 2 + a->lenL; /* ERL_ATOM_EXT */ - } - else { - return 1 + 1 + (a->lenU > 255) + a->lenU; - } + (void) erl_atom_ptr_utf8(a); + return 1 + 1 + (a->lenU > 255) + a->lenU; } static int erl_term_len_helper(ETERM *ep, int dist) -- cgit v1.2.3