summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGerald Combs <gerald@wireshark.org>2003-12-29 04:07:06 +0000
committerGerald Combs <gerald@wireshark.org>2003-12-29 04:07:06 +0000
commitd72750578bd90389586b901055b1aee07b2a96bf (patch)
tree821eac650eb552d5d8b552f9877e5c3d74ae989a
parent930410872fa5ddb1ac63e0014e3da17376d72c29 (diff)
downloadwireshark-d72750578bd90389586b901055b1aee07b2a96bf.tar.gz
Pull most of bytes_from_unparsed() into a new routine,
hex_str_to_bytes(). Use the new routine to initialize any WEP keys we have defined. This has the side effect of fixing an overflow if the user entered a long WEP key. svn path=/trunk/; revision=9471
-rw-r--r--epan/ftypes/ftype-bytes.c88
-rw-r--r--epan/strutil.c93
-rw-r--r--epan/strutil.h3
-rw-r--r--packet-ieee80211.c45
4 files changed, 122 insertions, 107 deletions
diff --git a/epan/ftypes/ftype-bytes.c b/epan/ftypes/ftype-bytes.c
index e396f64744..0bf67035ee 100644
--- a/epan/ftypes/ftype-bytes.c
+++ b/epan/ftypes/ftype-bytes.c
@@ -1,5 +1,5 @@
/*
- * $Id: ftype-bytes.c,v 1.20 2003/12/18 13:02:19 obiot Exp $
+ * $Id: ftype-bytes.c,v 1.21 2003/12/29 04:07:06 gerald Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -138,96 +138,16 @@ value_get(fvalue_t *fv)
}
static gboolean
-is_byte_sep(guint8 c)
-{
- return (c == '-' || c == ':' || c == '.');
-}
-
-static gboolean
bytes_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
{
GByteArray *bytes;
- guint8 val;
- guchar *p, *q, *punct;
- char two_digits[3];
- char one_digit[2];
- gboolean fail = FALSE;
+ gboolean res;
bytes = g_byte_array_new();
- p = (guchar *)s;
- while (*p) {
- q = p+1;
- if (*q && isxdigit(*p) && isxdigit(*q)) {
- two_digits[0] = *p;
- two_digits[1] = *q;
- two_digits[2] = '\0';
-
- /*
- * Two or more hex digits in a row.
- * "strtoul()" will succeed, as it'll see at
- * least one hex digit.
- */
- val = (guint8) strtoul(two_digits, NULL, 16);
- g_byte_array_append(bytes, &val, 1);
- punct = q + 1;
- if (*punct) {
- /*
- * Make sure the character after
- * the second hex digit is a byte
- * separator, i.e. that we don't have
- * more than two hex digits, or a
- * bogus character.
- */
- if (is_byte_sep(*punct)) {
- p = punct + 1;
- continue;
- }
- else {
- fail = TRUE;
- break;
- }
- }
- else {
- p = punct;
- continue;
- }
- }
- else if (*q && isxdigit(*p) && is_byte_sep(*q)) {
- one_digit[0] = *p;
- one_digit[1] = '\0';
-
- /*
- * Only one hex digit.
- * "strtoul()" will succeed, as it'll see that
- * hex digit.
- */
- val = (guint8) strtoul(one_digit, NULL, 16);
- g_byte_array_append(bytes, &val, 1);
- p = q + 1;
- continue;
- }
- else if (!*q && isxdigit(*p)) {
- one_digit[0] = *p;
- one_digit[1] = '\0';
-
- /*
- * Only one hex digit.
- * "strtoul()" will succeed, as it'll see that
- * hex digit.
- */
- val = (guint8) strtoul(one_digit, NULL, 16);
- g_byte_array_append(bytes, &val, 1);
- p = q;
- continue;
- }
- else {
- fail = TRUE;
- break;
- }
- }
+ res = hex_str_to_bytes(s, bytes);
- if (fail) {
+ if (!res) {
if (logfunc != NULL)
logfunc("\"%s\" is not a valid byte string.", s);
g_byte_array_free(bytes, TRUE);
diff --git a/epan/strutil.c b/epan/strutil.c
index 96eefb0926..fc2dad6326 100644
--- a/epan/strutil.c
+++ b/epan/strutil.c
@@ -1,7 +1,7 @@
/* strutil.c
* String utility routines
*
- * $Id: strutil.c,v 1.13 2003/12/24 01:12:17 guy Exp $
+ * $Id: strutil.c,v 1.14 2003/12/29 04:06:09 gerald Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -302,6 +302,97 @@ bytes_to_str_punct(const guint8 *bd, int bd_len, gchar punct) {
return cur;
}
+static gboolean
+is_byte_sep(guint8 c)
+{
+ return (c == '-' || c == ':' || c == '.');
+}
+
+/* Turn a string of hex digits with optional separators (defined by
+ * is_byte_sep() into a byte array.
+ */
+gboolean
+hex_str_to_bytes(const guchar *hex_str, const GByteArray *bytes) {
+ guint8 val;
+ guchar *p, *q, *punct;
+ char two_digits[3];
+ char one_digit[2];
+
+ g_byte_array_set_size(bytes, 0);
+ p = (guchar *)hex_str;
+ while (*p) {
+ q = p+1;
+ if (*q && isxdigit(*p) && isxdigit(*q)) {
+ two_digits[0] = *p;
+ two_digits[1] = *q;
+ two_digits[2] = '\0';
+
+ /*
+ * Two or more hex digits in a row.
+ * "strtoul()" will succeed, as it'll see at
+ * least one hex digit.
+ */
+ val = (guint8) strtoul(two_digits, NULL, 16);
+ g_byte_array_append(bytes, &val, 1);
+ punct = q + 1;
+ if (*punct) {
+ /*
+ * Make sure the character after
+ * the second hex digit is a byte
+ * separator, i.e. that we don't have
+ * more than two hex digits, or a
+ * bogus character.
+ */
+ if (is_byte_sep(*punct)) {
+ p = punct + 1;
+ continue;
+ }
+ else {
+ return FALSE;
+ break;
+ }
+ }
+ else {
+ p = punct;
+ continue;
+ }
+ }
+ else if (*q && isxdigit(*p) && is_byte_sep(*q)) {
+ one_digit[0] = *p;
+ one_digit[1] = '\0';
+
+ /*
+ * Only one hex digit.
+ * "strtoul()" will succeed, as it'll see that
+ * hex digit.
+ */
+ val = (guint8) strtoul(one_digit, NULL, 16);
+ g_byte_array_append(bytes, &val, 1);
+ p = q + 1;
+ continue;
+ }
+ else if (!*q && isxdigit(*p)) {
+ one_digit[0] = *p;
+ one_digit[1] = '\0';
+
+ /*
+ * Only one hex digit.
+ * "strtoul()" will succeed, as it'll see that
+ * hex digit.
+ */
+ val = (guint8) strtoul(one_digit, NULL, 16);
+ g_byte_array_append(bytes, &val, 1);
+ p = q;
+ continue;
+ }
+ else {
+ return FALSE;
+ }
+ }
+ return TRUE;
+}
+
+
/* Return the first occurrence of needle in haystack.
* If not found, return NULL.
* If either haystack or needle has 0 length, return NULL.
diff --git a/epan/strutil.h b/epan/strutil.h
index e9792c6e94..fd82d01174 100644
--- a/epan/strutil.h
+++ b/epan/strutil.h
@@ -1,7 +1,7 @@
/* strutil.h
* String utility definitions
*
- * $Id: strutil.h,v 1.11 2003/08/27 15:23:02 gram Exp $
+ * $Id: strutil.h,v 1.12 2003/12/29 04:06:09 gerald Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -34,6 +34,7 @@ int get_token_len(const guchar *linep, const guchar *lineend,
gchar* format_text(const guchar *line, int len);
gchar* bytes_to_str(const guint8 *, int);
gchar* bytes_to_str_punct(const guint8 *, int, gchar punct);
+gboolean hex_str_to_bytes(const guchar *hex_str, const GByteArray *bytes);
const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
const guint8 *needle, guint needle_len);
diff --git a/packet-ieee80211.c b/packet-ieee80211.c
index 03f284249f..18af2b7646 100644
--- a/packet-ieee80211.c
+++ b/packet-ieee80211.c
@@ -3,7 +3,7 @@
* Copyright 2000, Axis Communications AB
* Inquiries/bugreports should be sent to Johan.Jorgensen@axis.com
*
- * $Id: packet-ieee80211.c,v 1.102 2003/12/20 03:21:19 guy Exp $
+ * $Id: packet-ieee80211.c,v 1.103 2003/12/29 04:02:39 gerald Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -57,6 +57,7 @@
#include <epan/proto.h>
#include <epan/packet.h>
#include <epan/resolv.h>
+#include <epan/strutil.h>
#include "prefs.h"
#include "reassemble.h"
#include "packet-ipx.h"
@@ -2695,9 +2696,10 @@ static int wep_decrypt(guint8 *buf, guint32 len, int key_override) {
}
static void init_wepkeys(void) {
- char *tmp, *tmp2;
- guint8 *tmp3;
- guint i, j;
+ char *tmp;
+ guint i;
+ GByteArray *bytes;
+ gboolean res;
#ifdef USE_ENV
guint8 buf[128];
@@ -2724,6 +2726,7 @@ static void init_wepkeys(void) {
wep_keys = g_malloc(num_wepkeys * sizeof(guint8*));
wep_keylens = g_malloc(num_wepkeys * sizeof(int));
+ bytes = g_byte_array_new();
for (i = 0 ; i < num_wepkeys; i++) {
wep_keys[i] = NULL;
@@ -2737,7 +2740,6 @@ static void init_wepkeys(void) {
#endif
if (tmp) {
- j = 0;
#if 0
#ifdef USE_ENV
printf("%s -- %s\n", buf, tmp);
@@ -2746,26 +2748,27 @@ static void init_wepkeys(void) {
#endif
#endif
- if (wep_keys[i])
+ if (wep_keys[i]) {
g_free(wep_keys[i]);
- wep_keys[i] = g_malloc(32 * sizeof(guint8));
- memset(wep_keys[i], 0, 32 * sizeof(guint8));
- tmp3 = wep_keys[i];
- while ((tmp != NULL) && (*tmp != 0)) {
- tmp3[j] = strtoul(tmp, &tmp2, 16) & 0xff;
+ }
+
+ res = hex_str_to_bytes(tmp, bytes);
+ if (res && bytes->len > 0) {
+ if (bytes->len > 32) {
+ bytes->len = 32;
+ }
+ wep_keys[i] = g_malloc(32 * sizeof(guint8));
+ memset(wep_keys[i], 0, 32 * sizeof(guint8));
+ memcpy(wep_keys[i], bytes->data, bytes->len * sizeof(guint8));
+ wep_keylens[i] = bytes->len;
#if 0
- printf("%d %d -- %02x\n", i, j, tmp3[j]);
+ printf("%d: %d bytes\n", i, bytes->len);
+ printf("%d: %s\n", i, bytes_to_str(bytes->data, bytes->len));
#endif
- tmp = tmp2;
- wep_keylens[i]++;
-
- if ((tmp != NULL) && (*tmp == ':'))
- tmp++;
- j++;
+ } else {
+ g_warning("Could not parse WEP key %d: %s", i + 1, tmp);
}
}
-
}
-
- return;
+ g_byte_array_free(bytes, TRUE);
}