diff options
author | Peter Wu <peter@lekensteyn.nl> | 2017-03-04 01:40:51 +0100 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2017-03-08 23:04:12 +0000 |
commit | 540b5557291ad0960ce566adc3f24100aac946de (patch) | |
tree | 6c5aac3a5e7cce9c17f12980a6d22c0007375f15 | |
parent | 2b06c17592e792224779355c2e47d776ba2f9af6 (diff) | |
download | wireshark-540b5557291ad0960ce566adc3f24100aac946de.tar.gz wireshark-540b5557291ad0960ce566adc3f24100aac946de.tar.bz2 wireshark-540b5557291ad0960ce566adc3f24100aac946de.zip |
Introduce "bytes_string" type, similar to "value_string"
In order to map arbitrary byte buffers to strings, introduce a new
"bytes_string" type. Since "bytes_to_str" is already used for other
purposes, name the generic function "bytesval_to_str" instead similar to
the name( "val_to_str").
Accept "size_t" as length parameter since this is what is returned by
the "sizeof" operator. Do not add a "try_bytesval_to_str_idx" variant
since this pattern is not needed for now. Add a variant to match the
prefix since this is currently expected by the ISAKMP dissector.
Change-Id: I6d790325e85d9fb1384330f28a8c36e2057fdf30
Reviewed-on: https://code.wireshark.org/review/20386
Petri-Dish: Alexis La Goutte <alexis.lagoutte@gmail.com>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Alexis La Goutte <alexis.lagoutte@gmail.com>
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
-rw-r--r-- | debian/libwireshark0.symbols | 4 | ||||
-rw-r--r-- | epan/dissectors/packet-isakmp.c | 57 | ||||
-rw-r--r-- | epan/value_string.c | 77 | ||||
-rw-r--r-- | epan/value_string.h | 26 |
4 files changed, 109 insertions, 55 deletions
diff --git a/debian/libwireshark0.symbols b/debian/libwireshark0.symbols index ed43473c56..b9525096e4 100644 --- a/debian/libwireshark0.symbols +++ b/debian/libwireshark0.symbols @@ -76,6 +76,8 @@ libwireshark.so.0 libwireshark0 #MINVER# byte_array_equal@Base 1.9.1 bytes_to_hexstr@Base 2.1.0 bytes_to_str@Base 1.99.2 + bytesprefix_to_str@Base 2.3.0 + bytesval_to_str@Base 2.3.0 bytestring_to_str@Base 1.9.1 call_ber_oid_callback@Base 1.9.1 call_capture_dissector@Base 2.3.0 @@ -1507,6 +1509,8 @@ libwireshark.so.0 libwireshark0 #MINVER# tree_expanded@Base 1.12.0~rc1 tree_expanded_set@Base 1.12.0~rc1 try_capture_dissector@Base 2.1.0 + try_bytesprefix_to_str@Base 2.3.0 + try_bytesval_to_str@Base 2.3.0 try_rval_to_str@Base 1.9.1 try_rval_to_str_idx@Base 1.9.1 try_rval64_to_str@Base 2.3.0 diff --git a/epan/dissectors/packet-isakmp.c b/epan/dissectors/packet-isakmp.c index d324d447fa..25bb1427bf 100644 --- a/epan/dissectors/packet-isakmp.c +++ b/epan/dissectors/packet-isakmp.c @@ -72,14 +72,6 @@ void proto_register_isakmp(void); void proto_reg_handoff_isakmp(void); -/* Struct for the byte_to_str, match_bytestr_idx, and match_bytestr functions */ - -typedef struct _byte_string { - const gchar *value; - const guint16 len; - const gchar *strptr; -} byte_string; - typedef struct _attribute_common_fields { int all; int format; @@ -2826,8 +2818,7 @@ static const guint8 VID_FORTINET_ENDPOINT_CONTROL[] = { /* Endpoint Control (For 0x0B, 0xAF, 0xBB, 0xD3, 0x4A, 0xD3, 0x04, 0x4E }; -/* Based from value_string.c/h */ -static const byte_string vendor_id[] = { +static const bytes_string vendor_id[] = { { VID_SSH_IPSEC_EXPRESS_1_1_0, sizeof(VID_SSH_IPSEC_EXPRESS_1_1_0), "Ssh Communications Security IPSEC Express version 1.1.0" }, { VID_SSH_IPSEC_EXPRESS_1_1_1, sizeof(VID_SSH_IPSEC_EXPRESS_1_1_1), "Ssh Communications Security IPSEC Express version 1.1.1" }, { VID_SSH_IPSEC_EXPRESS_1_1_2, sizeof(VID_SSH_IPSEC_EXPRESS_1_1_2), "Ssh Communications Security IPSEC Express version 1.1.2" }, @@ -2941,50 +2932,6 @@ static const byte_string vendor_id[] = { }; -/* Tries to match val against each element in the value_string array vs. - Returns the associated string ptr, and sets "*idx" to the index in - that table, on a match, and returns NULL, and sets "*idx" to -1, - on failure. */ -static const gchar* -match_strbyte_idx(const guint8 *val, const gint val_len, const byte_string *vs, gint *idx) { - gint i = 0; - - if (vs) { - while (vs[i].strptr) { - if (val_len >= vs[i].len && !memcmp(vs[i].value, val, vs[i].len)) { - *idx = i; - return(vs[i].strptr); - } - i++; - } - } - - *idx = -1; - return NULL; -} -/* Like match_strbyte_idx(), but doesn't return the index. */ -static const gchar* -match_strbyte(const guint8 *val,const gint val_len, const byte_string *vs) { - gint ignore_me; - return match_strbyte_idx(val, val_len, vs, &ignore_me); -} - -/* Tries to match val against each element in the value_string array vs. - Returns the associated string ptr on a match. - Formats val with fmt, and returns the resulting string, on failure. */ -static const gchar* -byte_to_str(const guint8 *val,const gint val_len, const byte_string *vs, const char *fmt) { - const gchar *ret; - - DISSECTOR_ASSERT(fmt != NULL); - ret = match_strbyte(val, val_len, vs); - if (ret != NULL) - return ret; - - return wmem_strdup_printf(wmem_packet_scope(), fmt, val); -} - - static void dissect_payloads(tvbuff_t *tvb, proto_tree *tree, @@ -4777,7 +4724,7 @@ dissect_vid(tvbuff_t *tvb, int offset, int length, proto_tree *tree) pVID = tvb_get_ptr(tvb, offset, length); - vendorstring = byte_to_str(pVID, (gint)length, vendor_id, "Unknown Vendor ID"); + vendorstring = bytesprefix_to_str(pVID, (size_t)length, vendor_id, "Unknown Vendor ID"); proto_tree_add_item(tree, hf_isakmp_vid_bytes, tvb, offset, length, ENC_NA); proto_tree_add_string(tree, hf_isakmp_vid_string, tvb, offset, length, vendorstring); proto_item_append_text(tree," : %s", vendorstring); diff --git a/epan/value_string.c b/epan/value_string.c index c6aafbcfd4..858a6d25fd 100644 --- a/epan/value_string.c +++ b/epan/value_string.c @@ -641,6 +641,83 @@ try_rval64_to_str(const guint64 val, const range_string *rs) return try_rval64_to_str_idx(val, rs, &ignore_me); } + +/* BYTE BUFFER TO STRING MATCHING */ + +/* Like val_to_str except for bytes_string */ +const gchar * +bytesval_to_str(const guint8 *val, const size_t val_len, const bytes_string *bs, const char *fmt) +{ + const gchar *ret; + + DISSECTOR_ASSERT(fmt != NULL); + + ret = try_bytesval_to_str(val, val_len, bs); + if (ret != NULL) + return ret; + + /* + * XXX should this use bytes_to_str as format parameter for consistency? + * Though for bytes I guess most of the time you want to show "Unknown" + * anyway rather than "Unknown (\x13\x37...)" + */ + return wmem_strdup(wmem_packet_scope(), fmt); +} + +/* Like try_val_to_str except for bytes_string */ +const gchar * +try_bytesval_to_str(const guint8 *val, const size_t val_len, const bytes_string *bs) +{ + guint i = 0; + + if (bs) { + while (bs[i].strptr) { + if (bs[i].value_length == val_len && !memcmp(bs[i].value, val, val_len)) { + return bs[i].strptr; + } + i++; + } + } + + return NULL; +} + +/* Like val_to_str, but tries to find a prefix (instead of an exact) match + against any element of the bytes_string array bs. */ +const gchar * +bytesprefix_to_str(const guint8 *prefix, const size_t prefix_len, const bytes_string *bs, const char *fmt) +{ + const gchar *ret; + + DISSECTOR_ASSERT(fmt != NULL); + + ret = try_bytesprefix_to_str(prefix, prefix_len, bs); + if (ret != NULL) + return ret; + + /* XXX See note at bytesval_to_str. */ + return wmem_strdup(wmem_packet_scope(), fmt); +} + +/* Like try_val_to_str, but tries to find a prefix (instead of an exact) match + against any element of the bytes_string array bs. */ +const gchar * +try_bytesprefix_to_str(const guint8 *prefix, const size_t prefix_len, const bytes_string *bs) +{ + guint i = 0; + + if (bs) { + while (bs[i].strptr) { + if (prefix_len >= bs[i].value_length && !memcmp(bs[i].value, prefix, prefix_len)) { + return bs[i].strptr; + } + i++; + } + } + + return NULL; +} + /* MISC */ /* Functions for use by proto_registrar_dump_values(), see proto.c */ diff --git a/epan/value_string.h b/epan/value_string.h index 90b40f51b0..92d24deb97 100644 --- a/epan/value_string.h +++ b/epan/value_string.h @@ -274,6 +274,32 @@ WS_DLL_PUBLIC const gchar * try_rval64_to_str_idx(const guint64 val, const range_string *rs, gint *idx); +/* BYTES TO STRING MATCHING */ + +typedef struct _bytes_string { + const guint8 *value; + const size_t value_length; + const gchar *strptr; +} bytes_string; + +WS_DLL_PUBLIC +const gchar * +bytesval_to_str(const guint8 *val, const size_t val_len, const bytes_string *bs, const char *fmt) +G_GNUC_PRINTF(4, 0); + +WS_DLL_PUBLIC +const gchar * +try_bytesval_to_str(const guint8 *val, const size_t val_len, const bytes_string *bs); + +WS_DLL_PUBLIC +const gchar * +bytesprefix_to_str(const guint8 *prefix, const size_t prefix_len, const bytes_string *bs, const char *fmt) +G_GNUC_PRINTF(4, 0); + +WS_DLL_PUBLIC +const gchar * +try_bytesprefix_to_str(const guint8 *prefix, const size_t prefix_len, const bytes_string *bs); + /* MISC (generally do not use) */ WS_DLL_LOCAL |