提交 377663b3 authored 作者: Andrew Thompson's avatar Andrew Thompson

Allow certain tuple elements to be binaries or strings, to reduce conversion…

Allow certain tuple elements to be binaries or strings, to reduce conversion requirements on the erlang side


git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@11496 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 462db06b
...@@ -272,6 +272,28 @@ int ei_compare_pids(erlang_pid *pid1, erlang_pid *pid2) ...@@ -272,6 +272,28 @@ int ei_compare_pids(erlang_pid *pid1, erlang_pid *pid2)
} }
int ei_decode_string_or_binary(char *buf, int *index, int maxlen, char *dst) {
int type, size, res;
long len;
ei_get_type(buf, index, &type, &size);
if (type != ERL_STRING_EXT && type != ERL_BINARY_EXT) {
return -1;
} else if (size > maxlen) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Requested decoding of %s with size %d into a buffer of size %d\n", type == ERL_BINARY_EXT ? "binary" : "string", size, maxlen);
return -1;
} else if (type == ERL_BINARY_EXT) {
res = ei_decode_binary(buf, index, dst, &len);
dst[len] = '\0'; /* binaries aren't null terminated */
} else {
res = ei_decode_string(buf, index, dst);
}
return res;
}
switch_status_t initialise_ei(struct ei_cnode_s *ec) switch_status_t initialise_ei(struct ei_cnode_s *ec)
{ {
switch_status_t rv; switch_status_t rv;
......
...@@ -164,12 +164,11 @@ static switch_status_t handle_msg_fetch_reply(listener_t *listener, ei_x_buff * ...@@ -164,12 +164,11 @@ static switch_status_t handle_msg_fetch_reply(listener_t *listener, ei_x_buff *
{ {
char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
if (ei_decode_string(buf->buff, &buf->index, uuid_str)) { if (ei_decode_string_or_binary(buf->buff, &buf->index, SWITCH_UUID_FORMATTED_LENGTH, uuid_str)) {
ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_tuple_header(rbuf, 2);
ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "error");
ei_x_encode_atom(rbuf, "badarg"); ei_x_encode_atom(rbuf, "badarg");
} } else {
else {
ei_x_buff *nbuf = switch_core_alloc(listener->pool, sizeof(nbuf)); ei_x_buff *nbuf = switch_core_alloc(listener->pool, sizeof(nbuf));
nbuf->buff = switch_core_alloc(listener->pool, buf->buffsz); nbuf->buff = switch_core_alloc(listener->pool, buf->buffsz);
memcpy(nbuf->buff, buf->buff, buf->buffsz); memcpy(nbuf->buff, buf->buff, buf->buffsz);
...@@ -414,10 +413,10 @@ static switch_status_t handle_msg_sendevent(listener_t *listener, int arity, ei_ ...@@ -414,10 +413,10 @@ static switch_status_t handle_msg_sendevent(listener_t *listener, int arity, ei_
static switch_status_t handle_msg_sendmsg(listener_t *listener, int arity, ei_x_buff *buf, ei_x_buff *rbuf) static switch_status_t handle_msg_sendmsg(listener_t *listener, int arity, ei_x_buff *buf, ei_x_buff *rbuf)
{ {
char uuid[37]; char uuid[SWITCH_UUID_FORMATTED_LENGTH + 1];
int headerlength; int headerlength;
if (ei_decode_string(buf->buff, &buf->index, uuid) || if (ei_decode_string_or_binary(buf->buff, &buf->index, SWITCH_UUID_FORMATTED_LENGTH, uuid) ||
ei_decode_list_header(buf->buff, &buf->index, &headerlength)) { ei_decode_list_header(buf->buff, &buf->index, &headerlength)) {
ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_tuple_header(rbuf, 2);
ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "error");
...@@ -527,7 +526,7 @@ static switch_status_t handle_msg_handlecall(listener_t *listener, int arity, ei ...@@ -527,7 +526,7 @@ static switch_status_t handle_msg_handlecall(listener_t *listener, int arity, ei
char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
if (arity != 3 || if (arity != 3 ||
ei_decode_string(buf->buff, &buf->index, uuid_str) || ei_decode_string_or_binary(buf->buff, &buf->index, SWITCH_UUID_FORMATTED_LENGTH, uuid_str) ||
ei_decode_atom(buf->buff, &buf->index, reg_name)) { ei_decode_atom(buf->buff, &buf->index, reg_name)) {
ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_tuple_header(rbuf, 2);
ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "error");
......
...@@ -408,12 +408,12 @@ static switch_xml_t erlang_fetch(const char *sectionstr, const char *tag_name, c ...@@ -408,12 +408,12 @@ static switch_xml_t erlang_fetch(const char *sectionstr, const char *tag_name, c
ei_get_type(rep->buff, &rep->index, &type, &size); ei_get_type(rep->buff, &rep->index, &type, &size);
if (type != ERL_STRING_EXT) /* XXX no unicode or character codes > 255 */ if (type != ERL_STRING_EXT && type != ERL_BINARY_EXT) /* XXX no unicode or character codes > 255 */
return NULL; return NULL;
char *xmlstr = switch_core_alloc(ptr->listener->pool, size + 1); char *xmlstr = switch_core_alloc(ptr->listener->pool, size + 1);
ei_decode_string(rep->buff, &rep->index, xmlstr); ei_decode_string_or_binary(rep->buff, &rep->index, size, xmlstr);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "got data %s after %d milliseconds!\n", xmlstr, i*10); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "got data %s after %d milliseconds!\n", xmlstr, i*10);
......
...@@ -200,6 +200,7 @@ void ei_x_print_msg(ei_x_buff *buf, erlang_pid *pid, int send); ...@@ -200,6 +200,7 @@ void ei_x_print_msg(ei_x_buff *buf, erlang_pid *pid, int send);
int ei_sendto(ei_cnode *ec, int fd, struct erlang_process *process, ei_x_buff *buf); int ei_sendto(ei_cnode *ec, int fd, struct erlang_process *process, ei_x_buff *buf);
void ei_hash_ref(erlang_ref *ref, char *output); void ei_hash_ref(erlang_ref *ref, char *output);
int ei_compare_pids(erlang_pid *pid1, erlang_pid *pid2); int ei_compare_pids(erlang_pid *pid1, erlang_pid *pid2);
int ei_decode_string_or_binary(char *buf, int *index, int maxlen, char *dst);
switch_status_t initialise_ei(struct ei_cnode_s *ec); switch_status_t initialise_ei(struct ei_cnode_s *ec);
#define ei_encode_switch_event(_b, _e) ei_encode_switch_event_tag(_b, _e, "event") #define ei_encode_switch_event(_b, _e) ei_encode_switch_event_tag(_b, _e, "event")
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论