aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakub Zawadzki <darkjames-ws@darkjames.pl>2014-01-08 00:28:13 +0000
committerJakub Zawadzki <darkjames-ws@darkjames.pl>2014-01-08 00:28:13 +0000
commitd28084d1839bb581229f72acc37608b00c2caba7 (patch)
treefb6ba22256d5334b83e0279879544f28efcb8601
parente8dd800cc16765afadc8543ed9292ea5caf73df1 (diff)
Move UAT xton() to wsutil library
Use ws_xton() in few more places. svn path=/trunk/; revision=54642
-rw-r--r--epan/dissectors/packet-gtp.c1
-rw-r--r--epan/dissectors/packet-http-urlencoded.c20
-rw-r--r--epan/dissectors/packet-json.c22
-rw-r--r--epan/dissectors/packet-ssl-utils.c8
-rw-r--r--epan/strutil.c15
-rw-r--r--epan/uat.c28
-rw-r--r--wsutil/str_util.c24
-rw-r--r--wsutil/str_util.h3
8 files changed, 51 insertions, 70 deletions
diff --git a/epan/dissectors/packet-gtp.c b/epan/dissectors/packet-gtp.c
index aa83b2e4de..9faad3b73c 100644
--- a/epan/dissectors/packet-gtp.c
+++ b/epan/dissectors/packet-gtp.c
@@ -4140,6 +4140,7 @@ decode_gtp_mm_cntxt(tvbuff_t * tvb, int offset, packet_info * pinfo, proto_tree
static guint8
hex2dec(guint8 x)
{
+ /* XXX, ws_xton() */
if ((x >= 'a') && (x <= 'f'))
x = x - 'a' + 10;
else if ((x >= 'A') && (x <= 'F'))
diff --git a/epan/dissectors/packet-http-urlencoded.c b/epan/dissectors/packet-http-urlencoded.c
index 7236022bf9..b24ce507dc 100644
--- a/epan/dissectors/packet-http-urlencoded.c
+++ b/epan/dissectors/packet-http-urlencoded.c
@@ -30,6 +30,7 @@
#include <epan/packet.h>
#include <epan/wmem/wmem.h>
+#include <wsutil/str_util.h>
void proto_register_http_urlencoded(void);
void proto_reg_handoff_http_urlencoded(void);
@@ -52,19 +53,6 @@ static header_field_info hfi_form_value URLENCODED_HFI_INIT =
static gint ett_form_urlencoded = -1;
static gint ett_form_keyvalue = -1;
-static guint8
-get_hexa(guint8 a)
-{
- if (a >= '0' && a <= '9')
- return a - '0';
- if (a >= 'A' && a <= 'F')
- return (a - 'A') + 10;
- if (a >= 'a' && a <= 'f')
- return (a - 'a') + 10;
-
- return 0xff;
-}
-
static int
get_form_key_value(tvbuff_t *tvb, char **ptr, int offset, char stop)
{
@@ -84,12 +72,12 @@ get_form_key_value(tvbuff_t *tvb, char **ptr, int offset, char stop)
if (ch == '%') {
offset++;
ch = tvb_get_guint8(tvb, offset);
- if (get_hexa(ch) > 15)
+ if (ws_xton(ch) == -1)
return -1;
offset++;
ch = tvb_get_guint8(tvb, offset);
- if (get_hexa(ch) > 15)
+ if (ws_xton(ch) == -1)
return -1;
}
@@ -120,7 +108,7 @@ get_form_key_value(tvbuff_t *tvb, char **ptr, int offset, char stop)
offset++;
ch2 = tvb_get_guint8(tvb, offset);
- tmp[len] = get_hexa(ch1) << 4 | get_hexa(ch2);
+ tmp[len] = ws_xton(ch1) << 4 | ws_xton(ch2);
} else if (ch == '+')
tmp[len] = ' ';
diff --git a/epan/dissectors/packet-json.c b/epan/dissectors/packet-json.c
index 3c9f09193d..0d67da312c 100644
--- a/epan/dissectors/packet-json.c
+++ b/epan/dissectors/packet-json.c
@@ -37,6 +37,7 @@
#include <epan/packet.h>
#include <epan/tvbparse.h>
+#include <wsutil/str_util.h>
#include <wsutil/unicode-utils.h>
void proto_register_json(void);
@@ -281,6 +282,7 @@ static char *json_string_unescape(tvbparse_elem_t *tok)
j = 0;
for (i = 1; i < tok->len - 1; i++) {
guint8 ch = tvb_get_guint8(tok->tvb, tok->offset + i);
+ int bin;
if (ch == '\\') {
i++;
@@ -320,16 +322,12 @@ static char *json_string_unescape(tvbparse_elem_t *tok)
unicode_hex <<= 4;
ch = tvb_get_guint8(tok->tvb, tok->offset + i);
- if (ch >= '0' && ch <= '9')
- unicode_hex |= (ch - '0');
- else if (ch >= 'a' && ch <= 'f')
- unicode_hex |= (10 + (ch - 'a'));
- else if (ch >= 'A' && ch <= 'F')
- unicode_hex |= (10 + (ch - 'A'));
- else {
+ bin = ws_xton(ch);
+ if (bin == -1) {
valid = FALSE;
break;
}
+ unicode_hex |= bin;
}
if ((IS_LEAD_SURROGATE(unicode_hex))) {
@@ -348,16 +346,12 @@ static char *json_string_unescape(tvbparse_elem_t *tok)
trail_surrogate <<= 4;
ch = tvb_get_guint8(tok->tvb, tok->offset + i);
- if (ch >= '0' && ch <= '9')
- trail_surrogate |= (ch - '0');
- else if (ch >= 'a' && ch <= 'f')
- trail_surrogate |= (10 + (ch - 'a'));
- else if (ch >= 'A' && ch <= 'F')
- trail_surrogate |= (10 + (ch - 'A'));
- else {
+ bin = ws_xton(ch);
+ if (bin == -1) {
valid = FALSE;
break;
}
+ trail_surrogate |= bin;
}
if ((IS_TRAIL_SURROGATE(trail_surrogate))) {
diff --git a/epan/dissectors/packet-ssl-utils.c b/epan/dissectors/packet-ssl-utils.c
index cd3a79a20a..bb04e00ee6 100644
--- a/epan/dissectors/packet-ssl-utils.c
+++ b/epan/dissectors/packet-ssl-utils.c
@@ -42,6 +42,7 @@
#include <epan/ipv6-utils.h>
#include <epan/expert.h>
#include <wsutil/file_util.h>
+#include <wsutil/str_util.h>
/*
* Lookup tables
@@ -1356,6 +1357,7 @@ ssl_data_set(StringInfo* str, const guchar* data, guint len)
static guint8
from_hex_char(gchar c) {
+ /* XXX, ws_xton() */
if ((c >= '0') && (c <= '9'))
return c - '0';
if ((c >= 'A') && (c <= 'F'))
@@ -1377,9 +1379,9 @@ static gboolean from_hex(StringInfo* out, const char* in, gsize hex_len) {
out->data_len = (guint)hex_len/2;
out->data = (guchar *)wmem_alloc(wmem_file_scope(), out->data_len);
for (i = 0; i < out->data_len; i++) {
- guint8 a = from_hex_char(in[i*2]);
- guint8 b = from_hex_char(in[i*2 + 1]);
- if (a == 16 || b == 16)
+ int a = ws_xton(in[i*2]);
+ int b = ws_xton(in[i*2 + 1]);
+ if (a == -1 || b == -1)
return FALSE;
out->data[i] = a << 4 | b;
}
diff --git a/epan/strutil.c b/epan/strutil.c
index e0c3a7610a..0df9b515c9 100644
--- a/epan/strutil.c
+++ b/epan/strutil.c
@@ -31,6 +31,7 @@
#include "strutil.h"
#include "emem.h"
+#include <wsutil/str_util.h>
#ifdef _WIN32
#include <windows.h>
@@ -899,22 +900,12 @@ convert_string_to_hex(const char *string, size_t *nbytes)
if (c==':' || c=='.' || c=='-')
continue; /* skip any ':', '.', or '-' between bytes */
/* From the loop above, we know this is a hex digit */
- if (isdigit(c))
- byte_val = c - '0';
- else if (c >= 'a')
- byte_val = (c - 'a') + 10;
- else
- byte_val = (c - 'A') + 10;
+ byte_val = ws_xton(c);
byte_val <<= 4;
/* We also know this is a hex digit */
c = *p++;
- if (isdigit(c))
- byte_val |= c - '0';
- else if (c >= 'a')
- byte_val |= (c - 'a') + 10;
- else if (c >= 'A')
- byte_val |= (c - 'A') + 10;
+ byte_val |= ws_xton(c);
*q++ = byte_val;
}
diff --git a/epan/uat.c b/epan/uat.c
index d42854f4ca..a2db7e1874 100644
--- a/epan/uat.c
+++ b/epan/uat.c
@@ -574,28 +574,6 @@ gboolean uat_fld_chk_range(void* u1 _U_, const char* strptr, guint len, const vo
}
}
-static int xton(char d) {
- switch(d) {
- case '0': return 0;
- case '1': return 1;
- case '2': return 2;
- case '3': return 3;
- case '4': return 4;
- case '5': return 5;
- case '6': return 6;
- case '7': return 7;
- case '8': return 8;
- case '9': return 9;
- case 'a': case 'A': return 10;
- case 'b': case 'B': return 11;
- case 'c': case 'C': return 12;
- case 'd': case 'D': return 13;
- case 'e': case 'E': return 14;
- case 'f': case 'F': return 15;
- default: return -1;
- }
-}
-
char* uat_unbinstring(const char* si, guint in_len, guint* len_p) {
guint8* buf;
guint len = in_len/2;
@@ -610,8 +588,8 @@ char* uat_unbinstring(const char* si, guint in_len, guint* len_p) {
if (len_p) *len_p = len;
while(in_len) {
- d1 = xton(*(si++));
- d0 = xton(*(si++));
+ d1 = ws_xton(*(si++));
+ d0 = ws_xton(*(si++));
buf[i++] = (d1 * 16) + d0;
@@ -679,7 +657,7 @@ char* uat_unesc(const char* si, guint in_len, guint* len_p) {
char c0 = *(s+2);
if (isxdigit((guchar)c1) && isxdigit((guchar)c0)) {
- *(p++) = (xton(c1) * 0x10) + xton(c0);
+ *(p++) = (ws_xton(c1) * 0x10) + ws_xton(c0);
s += 2;
} else {
*(p++) = *s;
diff --git a/wsutil/str_util.c b/wsutil/str_util.c
index 9c0aa6e802..15e48dd953 100644
--- a/wsutil/str_util.c
+++ b/wsutil/str_util.c
@@ -29,6 +29,30 @@
#include <ctype.h>
+int
+ws_xton(char ch)
+{
+ switch (ch) {
+ case '0': return 0;
+ case '1': return 1;
+ case '2': return 2;
+ case '3': return 3;
+ case '4': return 4;
+ case '5': return 5;
+ case '6': return 6;
+ case '7': return 7;
+ case '8': return 8;
+ case '9': return 9;
+ case 'a': case 'A': return 10;
+ case 'b': case 'B': return 11;
+ case 'c': case 'C': return 12;
+ case 'd': case 'D': return 13;
+ case 'e': case 'E': return 14;
+ case 'f': case 'F': return 15;
+ default: return -1;
+ }
+}
+
/* Convert all ASCII letters to lower case, in place. */
gchar *
ascii_strdown_inplace(gchar *str)
diff --git a/wsutil/str_util.h b/wsutil/str_util.h
index 505bb9088d..0ee55ea7cb 100644
--- a/wsutil/str_util.h
+++ b/wsutil/str_util.h
@@ -83,6 +83,9 @@ gboolean isprint_string(const gchar *string);
WS_DLL_PUBLIC
gboolean isdigit_string(guchar *string);
+WS_DLL_PUBLIC
+int ws_xton(char ch);
+
typedef enum {
format_size_unit_none = 0, /**< No unit will be appended. You must supply your own. */
format_size_unit_bytes = 1, /**< "bytes" for un-prefixed sizes, "B" otherwise. */