From 834543d7c779bdcd0a49536e91c0021c17264477 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Tue, 23 Mar 2004 18:06:29 +0000 Subject: [PATCH] Use "tvb_format_text()" to display strings, so we handle non-printable characters. Some strings appear to be null-padded; add a "tvb_format_stringzpad()" routine to handle them, so that we don't show the padding characters as "\000". svn path=/trunk/; revision=10461 --- epan/tvbuff.c | 26 +++++++++++++++++++- epan/tvbuff.h | 8 ++++++- packet-cdp.c | 66 +++++++++++++++++++++------------------------------ 3 files changed, 59 insertions(+), 41 deletions(-) diff --git a/epan/tvbuff.c b/epan/tvbuff.c index 4f68c8ffc1..c3511c3ba0 100644 --- a/epan/tvbuff.c +++ b/epan/tvbuff.c @@ -9,7 +9,7 @@ * the data of a backing tvbuff, or can be a composite of * other tvbuffs. * - * $Id: tvbuff.c,v 1.60 2004/02/19 05:19:10 guy Exp $ + * $Id: tvbuff.c,v 1.61 2004/03/23 18:06:29 guy Exp $ * * Copyright (c) 2000 by Gilbert Ramirez * @@ -1653,6 +1653,30 @@ tvb_format_text(tvbuff_t *tvb, gint offset, gint size) } +/* + * Like "tvb_format_text()", but for null-padded strings; don't show + * the null padding characters as "\000". + */ +gchar * +tvb_format_stringzpad(tvbuff_t *tvb, gint offset, gint size) +{ + const guint8 *ptr, *p; + gint len = size; + gint stringlen; + + if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) { + + len = tvb_length_remaining(tvb, offset); + ptr = ensure_contiguous(tvb, offset, len); + + } + + for (p = ptr, stringlen = 0; stringlen < len && *p != '\0'; p++, stringlen++) + ; + return format_text(ptr, stringlen); + +} + /* * Given a tvbuff, an offset, and a length, allocate a buffer big enough * to hold a non-null-terminated string of that length at that offset, diff --git a/epan/tvbuff.h b/epan/tvbuff.h index c1a77481ce..bf9715dc32 100644 --- a/epan/tvbuff.h +++ b/epan/tvbuff.h @@ -9,7 +9,7 @@ * the data of a backing tvbuff, or can be a composite of * other tvbuffs. * - * $Id: tvbuff.h,v 1.40 2004/02/19 05:19:10 guy Exp $ + * $Id: tvbuff.h,v 1.41 2004/03/23 18:06:29 guy Exp $ * * Copyright (c) 2000 by Gilbert Ramirez * @@ -382,6 +382,12 @@ extern char *tvb_fake_unicode(tvbuff_t *tvb, int offset, int len, */ extern gchar * tvb_format_text(tvbuff_t *tvb, gint offset, gint size); +/* + * Like "tvb_format_text()", but for null-padded strings; don't show + * the null padding characters as "\000". + */ +extern gchar *tvb_format_stringzpad(tvbuff_t *tvb, gint offset, gint size); + /* * Given a tvbuff, an offset, and a length, allocate a buffer big enough * to hold a non-null-terminated string of that length at that offset, diff --git a/packet-cdp.c b/packet-cdp.c index 33be1114eb..e428811955 100644 --- a/packet-cdp.c +++ b/packet-cdp.c @@ -2,7 +2,7 @@ * Routines for the disassembly of the "Cisco Discovery Protocol" * (c) Copyright Hannes R. Boehm * - * $Id: packet-cdp.c,v 1.54 2004/03/22 01:18:35 guy Exp $ + * $Id: packet-cdp.c,v 1.55 2004/03/23 18:06:29 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -188,18 +188,16 @@ dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) case TYPE_DEVICE_ID: /* Device ID */ tlvi = proto_tree_add_text(cdp_tree, tvb, offset, - length, "Device ID: %.*s", - length - 4, - tvb_get_ptr(tvb, offset + 4, length - 4)); + length, "Device ID: %s", + tvb_format_stringzpad(tvb, offset + 4, length - 4)); tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv); proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, type); proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, length); proto_tree_add_text(tlv_tree, tvb, offset + 4, - length - 4, "Device ID: %.*s", - length - 4, - tvb_get_ptr(tvb, offset + 4, length - 4)); + length - 4, "Device ID: %s", + tvb_format_stringzpad(tvb, offset + 4, length - 4)); offset += length; break; @@ -244,9 +242,8 @@ dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) real_length = length + 3; } tlvi = proto_tree_add_text(cdp_tree, tvb, offset, - real_length, "Port ID: %.*s", - real_length - 4, - tvb_get_ptr(tvb, offset + 4, real_length - 4)); + real_length, "Port ID: %s", + tvb_format_text(tvb, offset + 4, real_length - 4)); tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv); proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, type); @@ -254,9 +251,8 @@ dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) offset + TLV_LENGTH, 2, length); proto_tree_add_text(tlv_tree, tvb, offset + 4, real_length - 4, - "Sent through Interface: %.*s", - real_length - 4, - tvb_get_ptr(tvb, offset + 4, real_length - 4)); + "Sent through Interface: %s", + tvb_format_text(tvb, offset + 4, real_length - 4)); offset += real_length; break; @@ -290,18 +286,16 @@ dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) case TYPE_PLATFORM: /* ??? platform */ tlvi = proto_tree_add_text(cdp_tree, tvb, - offset, length, "Platform: %.*s", - length - 4, - tvb_get_ptr(tvb, offset + 4, length - 4)); + offset, length, "Platform: %s", + tvb_format_text(tvb, offset + 4, length - 4)); tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv); proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, type); proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, length); proto_tree_add_text(tlv_tree, tvb, offset + 4, - length - 4, "Platform: %.*s", - length - 4, - tvb_get_ptr(tvb, offset + 4, length - 4)); + length - 4, "Platform: %s", + tvb_format_text(tvb, offset + 4, length - 4)); offset += length; break; case TYPE_IP_PREFIX: @@ -407,18 +401,16 @@ dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) break; case TYPE_VTP_MGMT_DOMAIN: tlvi = proto_tree_add_text(cdp_tree, tvb, - offset, length, "VTP Management Domain: %.*s", - length - 4, - tvb_get_ptr(tvb, offset + 4, length - 4)); + offset, length, "VTP Management Domain: %s", + tvb_format_text(tvb, offset + 4, length - 4)); tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv); proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, type); proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, length); proto_tree_add_text(tlv_tree, tvb, offset + 4, - length - 4, "VTP Management Domain: %.*s", - length - 4, - tvb_get_ptr(tvb, offset + 4, length - 4)); + length - 4, "VTP Management Domain: %s", + tvb_format_text(tvb, offset + 4, length - 4)); offset += length; break; case TYPE_NATIVE_VLAN: @@ -519,18 +511,16 @@ dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) break; case TYPE_SYSTEM_NAME: tlvi = proto_tree_add_text(cdp_tree, tvb, - offset, length, "System Name: %.*s", - length - 4, - tvb_get_ptr(tvb, offset + 4, length - 4)); + offset, length, "System Name: %s", + tvb_format_text(tvb, offset + 4, length - 4)); tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv); proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, type); proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, length); proto_tree_add_text(tlv_tree, tvb, offset + 4, - length - 4, "System Name: %.*s", - length - 4, - tvb_get_ptr(tvb, offset + 4, length - 4)); + length - 4, "System Name: %s", + tvb_format_text(tvb, offset + 4, length - 4)); offset += length; break; case TYPE_SYSTEM_OID: @@ -575,9 +565,8 @@ dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) break; case TYPE_LOCATION: tlvi = proto_tree_add_text(cdp_tree, tvb, - offset, length, "Location: %.*s", - length - 5, - tvb_get_ptr(tvb, offset + 5, length - 5)); + offset, length, "Location: %s", + tvb_format_text(tvb, offset + 5, length - 5)); tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv); proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, type); @@ -587,9 +576,8 @@ dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) 1 , "UNKNOWN: 0x%02X", tvb_get_guint8(tvb, offset + 4)); proto_tree_add_text(tlv_tree, tvb, offset + 5, - length - 5, "Location: %.*s", - length - 5, - tvb_get_ptr(tvb, offset + 5, length - 5)); + length - 5, "Location: %s", + tvb_format_text(tvb, offset + 5, length - 5)); offset += length; break; default: @@ -786,8 +774,8 @@ add_multi_line_string_to_tree(proto_tree *tree, tvbuff_t *tvb, gint start, while (len > 0) { line_len = tvb_find_line_end(tvb, start, len, &next, FALSE); data_len = next - start; - proto_tree_add_text(tree, tvb, start, data_len, "%s%.*s", prefix, - line_len, tvb_get_ptr(tvb, start, line_len)); + proto_tree_add_text(tree, tvb, start, data_len, "%s%s", prefix, + tvb_format_stringzpad(tvb, start, line_len)); start += data_len; len -= data_len; prefix = blanks;