From 3374548567e5255d17c558bad2235a9a325d0466 Mon Sep 17 00:00:00 2001 From: Eugene Tarassov Date: Tue, 27 Aug 2013 23:42:35 -0700 Subject: TCF Agent: performance improvements in the protocol handling code --- agent/tcf/framework/json.c | 58 +++++++++++++++++++++++++----------------- agent/tcf/framework/protocol.c | 4 +-- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/agent/tcf/framework/json.c b/agent/tcf/framework/json.c index 6f80a6f1..4b35f5b8 100644 --- a/agent/tcf/framework/json.c +++ b/agent/tcf/framework/json.c @@ -820,34 +820,43 @@ static void skip_object(InputStream * inp) { int ch; read_whitespace(inp); ch = skip_char(inp); - if (ch == 'n') { + switch (ch) { + case 'n': check_char(skip_char(inp), 'u'); check_char(skip_char(inp), 'l'); check_char(skip_char(inp), 'l'); return; - } - if (ch == 'f') { + case 'f': check_char(skip_char(inp), 'a'); check_char(skip_char(inp), 'l'); check_char(skip_char(inp), 's'); check_char(skip_char(inp), 'e'); return; - } - if (ch == 't') { + case 't': check_char(skip_char(inp), 'r'); check_char(skip_char(inp), 'u'); check_char(skip_char(inp), 'e'); return; - } - if (ch == '"') { + case '"': for (;;) { - ch = skip_char(inp); + ch = read_stream(inp); + if (ch < 0) exception(ERR_JSON_SYNTAX); + buf_add(ch); if (ch == '"') break; if (ch == '\\') skip_char(inp); } return; - } - if (ch == '-' || (ch >= '0' && ch <= '9')) { + case '-': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': for (;;) { ch = peek_stream(inp); if ((ch < '0' || ch > '9') && ch != '.' @@ -855,8 +864,7 @@ static void skip_object(InputStream * inp) { skip_char(inp); } return; - } - if (ch == '[') { + case '[': if (json_peek(inp) == ']') { skip_char(inp); } @@ -872,14 +880,12 @@ static void skip_object(InputStream * inp) { } } return; - } - if (ch == '{') { + case '{': if (json_peek(inp) == '}') { skip_char(inp); } else { for (;;) { - int ch; skip_object(inp); read_whitespace(inp); ch = skip_char(inp); @@ -893,18 +899,22 @@ static void skip_object(InputStream * inp) { } } return; - } - if (ch == '(') { - unsigned long size = json_read_ulong(inp); - ch = skip_char(inp); - check_char(ch, ')'); - while (size) { - skip_char(inp); - size--; + case '(': + { + unsigned long size = json_read_ulong(inp); + ch = skip_char(inp); + check_char(ch, ')'); + while (size) { + ch = read_stream(inp); + if (ch < 0) exception(ERR_JSON_SYNTAX); + buf_add(ch); + size--; + } } return; + default: + exception(ERR_JSON_SYNTAX); } - exception(ERR_JSON_SYNTAX); } char * json_read_object(InputStream * inp) { diff --git a/agent/tcf/framework/protocol.c b/agent/tcf/framework/protocol.c index 3e1228aa..f1e72851 100644 --- a/agent/tcf/framework/protocol.c +++ b/agent/tcf/framework/protocol.c @@ -97,8 +97,8 @@ static void read_stringz(InputStream * inp, char * str, size_t size) { unsigned len = 0; for (;;) { int ch = read_stream(inp); - if (ch == 0) break; - if (ch < 0) { + if (ch <= 0) { + if (ch == 0) break; trace(LOG_ALWAYS, "Unexpected end of message"); exception(ERR_PROTOCOL); } -- cgit v1.2.3