Move UAT xton() to wsutil library
authorJakub Zawadzki <darkjames-ws@darkjames.pl>
Wed, 8 Jan 2014 00:28:13 +0000 (00:28 -0000)
committerJakub Zawadzki <darkjames-ws@darkjames.pl>
Wed, 8 Jan 2014 00:28:13 +0000 (00:28 -0000)
Use ws_xton() in few more places.

svn path=/trunk/; revision=54642

epan/dissectors/packet-gtp.c
epan/dissectors/packet-http-urlencoded.c
epan/dissectors/packet-json.c
epan/dissectors/packet-ssl-utils.c
epan/strutil.c
epan/uat.c
wsutil/str_util.c
wsutil/str_util.h

index aa83b2e4de1d5a6d06f7bfd8e5d94add6dfe85b2..9faad3b73c0a84e8e3bd9e641b7afb24333b3f4c 100644 (file)
@@ -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'))
index 7236022bf9384e4cb52c6b7def98e8445be748f8..b24ce507dc9ce891e08d911aea4b9832244accb5 100644 (file)
@@ -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] = ' ';
index 3c9f09193d373017d6b75853748aca089cab09ec..0d67da312c3b4bb278d38644c011566df656e270 100644 (file)
@@ -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))) {
index cd3a79a20ade084442483e30e4b683219022adb6..bb04e00ee6796feb4799ec8ee660ae3014cb4385 100644 (file)
@@ -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;
     }
index e0c3a7610a81cf46eac5a8b8ef713cbf3e6b62d6..0df9b515c9d9de59de1b7d12c684ef2a7b3322d8 100644 (file)
@@ -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;
     }
index d42854f4ca8b5830b08c6a2ab8b5d219c6517a63..a2db7e1874339c3403fbbbd84f92eaa281c293ae 100644 (file)
@@ -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;
index 9c0aa6e8021e76bce1ac662efce9deec33867d41..15e48dd9533507e6120da8488c0eef9f54d877f4 100644 (file)
 
 #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)
index 505bb9088d71337826d0d65dacbc58d2ecbd7358..0ee55ea7cbb43bc2837d96b347f4423bd35f7b69 100644 (file)
@@ -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. */