aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/src/encode
diff options
context:
space:
mode:
authorSverker Eriksson <[email protected]>2013-02-18 15:09:00 +0100
committerSverker Eriksson <[email protected]>2013-02-18 15:20:47 +0100
commit9a35c01873fb56316136e1314ad6adffe97b9fa5 (patch)
treea67f86b77f1e08049b51e766468fb63a139d666e /lib/erl_interface/src/encode
parent499eef0cd693b2f96ec19148d2f6666c3df7d834 (diff)
downloadotp-9a35c01873fb56316136e1314ad6adffe97b9fa5.tar.gz
otp-9a35c01873fb56316136e1314ad6adffe97b9fa5.tar.bz2
otp-9a35c01873fb56316136e1314ad6adffe97b9fa5.zip
erl_interface,ic: Remove node_org_enc from erlang_{pid,port,ref}
in order to be backward compatible with user code that accesses the members of erlang_pid and friend. The documentation does not mention the content of these structs, but we have example code that does. So the safe way it the revert the node_org_enc field (added in R16A) and instead determine in runtime which atom encoding to use depending on if the node atom contains unicode (>255) characters or not.
Diffstat (limited to 'lib/erl_interface/src/encode')
-rw-r--r--lib/erl_interface/src/encode/encode_atom.c24
-rw-r--r--lib/erl_interface/src/encode/encode_pid.c3
-rw-r--r--lib/erl_interface/src/encode/encode_port.c2
-rw-r--r--lib/erl_interface/src/encode/encode_ref.c2
4 files changed, 25 insertions, 6 deletions
diff --git a/lib/erl_interface/src/encode/encode_atom.c b/lib/erl_interface/src/encode/encode_atom.c
index df4b0af5db..46d34c3bf0 100644
--- a/lib/erl_interface/src/encode/encode_atom.c
+++ b/lib/erl_interface/src/encode/encode_atom.c
@@ -25,7 +25,7 @@
static int verify_ascii_atom(const char* src, int slen);
static int verify_utf8_atom(const char* src, int slen);
-
+static int is_latin1_as_utf8(const char *p, int len);
int ei_encode_atom(char *buf, int *index, const char *p)
{
@@ -63,6 +63,14 @@ int ei_encode_atom_len_as(char *buf, int *index, const char *p, int len,
return -1;
}
+ if (to_enc == (ERLANG_LATIN1 | ERLANG_UTF8)) {
+ if (from_enc == ERLANG_UTF8) {
+ to_enc = is_latin1_as_utf8(p, len) ? ERLANG_LATIN1 : ERLANG_UTF8;
+ }
+ else {
+ to_enc = from_enc;
+ }
+ }
switch(to_enc) {
case ERLANG_LATIN1:
if (buf) {
@@ -148,7 +156,7 @@ ei_internal_put_atom(char** bufp, const char* p, int slen,
}
-int verify_ascii_atom(const char* src, int slen)
+static int verify_ascii_atom(const char* src, int slen)
{
while (slen > 0) {
if ((src[0] & 0x80) != 0) return -1;
@@ -158,7 +166,7 @@ int verify_ascii_atom(const char* src, int slen)
return 0;
}
-int verify_utf8_atom(const char* src, int slen)
+static int verify_utf8_atom(const char* src, int slen)
{
int num_chars = 0;
@@ -188,3 +196,13 @@ int verify_utf8_atom(const char* src, int slen)
return 0;
}
+/* Only latin1 code points in utf8 string?
+ */
+static int is_latin1_as_utf8(const char *p, int len)
+{
+ int i;
+ for (i=0; i<len; i++) {
+ if ((unsigned char)p[i] > 0xC3) return 0;
+ }
+ return 1;
+}
diff --git a/lib/erl_interface/src/encode/encode_pid.c b/lib/erl_interface/src/encode/encode_pid.c
index 903c9cce00..86d0f393e5 100644
--- a/lib/erl_interface/src/encode/encode_pid.c
+++ b/lib/erl_interface/src/encode/encode_pid.c
@@ -26,7 +26,8 @@ int ei_encode_pid(char *buf, int *index, const erlang_pid *p)
char *s = buf + *index;
++(*index); /* skip ERL_PID_EXT */
- if (ei_encode_atom_len_as(buf, index, p->node, strlen(p->node), ERLANG_UTF8, p->node_org_enc) < 0)
+ if (ei_encode_atom_len_as(buf, index, p->node, strlen(p->node),
+ ERLANG_UTF8, ERLANG_LATIN1|ERLANG_UTF8) < 0)
return -1;
if (buf) {
diff --git a/lib/erl_interface/src/encode/encode_port.c b/lib/erl_interface/src/encode/encode_port.c
index c729aeb4eb..a206de56c7 100644
--- a/lib/erl_interface/src/encode/encode_port.c
+++ b/lib/erl_interface/src/encode/encode_port.c
@@ -27,7 +27,7 @@ int ei_encode_port(char *buf, int *index, const erlang_port *p)
++(*index); /* skip ERL_PORT_EXT */
if (ei_encode_atom_len_as(buf, index, p->node, strlen(p->node), ERLANG_UTF8,
- p->node_org_enc) < 0) {
+ ERLANG_LATIN1|ERLANG_UTF8) < 0) {
return -1;
}
if (buf) {
diff --git a/lib/erl_interface/src/encode/encode_ref.c b/lib/erl_interface/src/encode/encode_ref.c
index 3511366bef..9855231848 100644
--- a/lib/erl_interface/src/encode/encode_ref.c
+++ b/lib/erl_interface/src/encode/encode_ref.c
@@ -28,7 +28,7 @@ int ei_encode_ref(char *buf, int *index, const erlang_ref *p)
(*index) += 1 + 2; /* skip to node atom */
if (ei_encode_atom_len_as(buf, index, p->node, strlen(p->node), ERLANG_UTF8,
- p->node_org_enc) < 0) {
+ ERLANG_LATIN1|ERLANG_UTF8) < 0) {
return -1;
}