diff options
author | Guy Harris <guy@alum.mit.edu> | 2014-04-12 15:26:34 -0700 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2014-04-12 22:27:22 +0000 |
commit | cb16dff992c3844936bf5829f19e5a5247458503 (patch) | |
tree | 901b7be3379f5b94f150de71d1c0f3e2fa2d12e4 /epan/dissectors | |
parent | ef8a0a2ce172810d48371eb65c73b1bd4a6303ca (diff) |
Get rid of more tvb_get_nstringz* calls.
Add an FT_STRINGZPAD type, for null-padded strings (typically
fixed-length fields, where the string can be up to the length of the
field, and is null-padded if it's shorter than that), and use it. Use
IS_FT_STRING() in more cases, so that less code needs to know what types
are string types.
Add a tvb_get_stringzpad() routine, which gets null-padded strings.
Currently, it does the same thing that tvb_get_string_enc() does, but
that might change if we don't store string values as null-terminated
strings.
Change-Id: I46f56e130de8f419a19b56ded914e24cc7518a66
Reviewed-on: https://code.wireshark.org/review/1082
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'epan/dissectors')
-rw-r--r-- | epan/dissectors/packet-fw1.c | 3 | ||||
-rw-r--r-- | epan/dissectors/packet-hartip.c | 52 | ||||
-rw-r--r-- | epan/dissectors/packet-ospf.c | 4 | ||||
-rw-r--r-- | epan/dissectors/packet-quake3.c | 18 | ||||
-rw-r--r-- | epan/dissectors/packet-quakeworld.c | 20 | ||||
-rw-r--r-- | epan/dissectors/packet-rsync.c | 8 | ||||
-rw-r--r-- | epan/dissectors/packet-smb-browse.c | 28 | ||||
-rw-r--r-- | epan/dissectors/packet-tacacs.c | 11 | ||||
-rw-r--r-- | epan/dissectors/packet-who.c | 17 |
9 files changed, 72 insertions, 89 deletions
diff --git a/epan/dissectors/packet-fw1.c b/epan/dissectors/packet-fw1.c index 51ed2942a0..e4ff9e3a7d 100644 --- a/epan/dissectors/packet-fw1.c +++ b/epan/dissectors/packet-fw1.c @@ -160,8 +160,7 @@ dissect_fw1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) if (fw1_with_uuid) iface_len = 6; - interface_name=(char *)wmem_alloc(wmem_packet_scope(), iface_len+1); - tvb_get_nstringz0(tvb, 2, iface_len+1, interface_name); + interface_name=tvb_get_stringzpad(wmem_packet_scope(), tvb, 2, iface_len, ENC_ASCII|ENC_NA); /* Known interface name - if not, remember it */ found=FALSE; diff --git a/epan/dissectors/packet-hartip.c b/epan/dissectors/packet-hartip.c index c8edad2b94..6d6b0d71d3 100644 --- a/epan/dissectors/packet-hartip.c +++ b/epan/dissectors/packet-hartip.c @@ -384,26 +384,7 @@ dissect_float(proto_tree *tree, int hf, tvbuff_t *tvb, gint offset) } static gint -dissect_string(proto_tree *tree, int hf, const char *name, int len, - tvbuff_t *tvb, gint offset) -{ - proto_item *ti; - char *str; - - str = (char *)wmem_alloc(wmem_packet_scope(), 256); - - ti = proto_tree_add_item(tree, hf, tvb, offset, len, ENC_NA); - if (len < 256) { - (void) tvb_get_nstringz0(tvb, offset, len + 1, str); - proto_item_set_text(ti, "%s: %s", name, str); - } - - return len; -} - -static gint -dissect_packAscii(proto_tree *tree, int hf, const char *name, int len, - tvbuff_t *tvb, gint offset) +dissect_packAscii(proto_tree *tree, int hf, tvbuff_t *tvb, gint offset, int len) { gushort usIdx; gushort usGroupCnt; @@ -411,19 +392,16 @@ dissect_packAscii(proto_tree *tree, int hf, const char *name, int len, gushort usMask; gint iIndex; gint i = 0; - proto_item *ti; gushort buf[4]; guint8 *tmp; char *str = NULL; - str = (char *)wmem_alloc(wmem_packet_scope(), 256+1); - - ti = proto_tree_add_item(tree, hf, tvb, offset, len, ENC_NA); - - DISSECTOR_ASSERT(len < 3 * (256/4)); tmp = (guint8 *)wmem_alloc0(wmem_packet_scope(), len); tvb_memcpy(tvb, tmp, offset, len); + /* Maximum possible unpacked length = (len / 3) * 4 */ + str = (char *)wmem_alloc(wmem_packet_scope(), ((len / 3) * 4)+1); + iIndex = 0; usMaxGroups = (gushort)(len / 3); for (usGroupCnt = 0; usGroupCnt < usMaxGroups; usGroupCnt++) { @@ -446,7 +424,8 @@ dissect_packAscii(proto_tree *tree, int hf, const char *name, int len, } } str[i] = '\0'; - proto_item_set_text(ti, "%s: %s", name, str); + + proto_tree_add_string(tree, hf, tvb, offset, len, str); return len; } @@ -630,8 +609,8 @@ static gint dissect_cmd13(proto_tree *body_tree, tvbuff_t *tvb, gint offset, gint bodylen) { if (bodylen >= 21) { - offset += dissect_packAscii(body_tree, hf_hartip_pt_rsp_tag, "Tag", 6, tvb, offset); - offset += dissect_packAscii(body_tree, hf_hartip_pt_rsp_packed_descriptor, "descriptor", 12, tvb, offset); + offset += dissect_packAscii(body_tree, hf_hartip_pt_rsp_tag, tvb, offset, 6); + offset += dissect_packAscii(body_tree, hf_hartip_pt_rsp_packed_descriptor, tvb, offset, 12); offset += dissect_byte(body_tree, hf_hartip_pt_rsp_day, tvb, offset); offset += dissect_byte(body_tree, hf_hartip_pt_rsp_month, tvb, offset); /*offset += */dissect_byte(body_tree, hf_hartip_pt_rsp_year, tvb, offset); @@ -688,13 +667,16 @@ dissect_parse_hart_cmds(proto_tree *body_tree, tvbuff_t *tvb, guint8 cmd, return dissect_cmd9(body_tree, tvb, offset, bodylen); case 12: if (bodylen >= 24) - return dissect_packAscii(body_tree, hf_hartip_pt_rsp_message, "Message", 24, tvb, offset); + return dissect_packAscii(body_tree, hf_hartip_pt_rsp_message, tvb, offset, 24); break; case 13: return dissect_cmd13(body_tree, tvb, offset, bodylen); case 20: - if (bodylen >= 32) - return dissect_string(body_tree, hf_hartip_pt_rsp_tag, "Tag", 32, tvb, offset); + if (bodylen >= 32) { + proto_tree_add_item(body_tree, hf_hartip_pt_rsp_tag, tvb, offset, 32, + ENC_ASCII|ENC_NA); + return 32; + } break; case 48: return dissect_cmd48(body_tree, tvb, offset, bodylen); @@ -1438,7 +1420,7 @@ proto_register_hartip(void) /* command 13 */ { &hf_hartip_pt_rsp_packed_descriptor, { "Descriptor", "hart_ip.pt.rsp.descriptor", - FT_BYTES, BASE_NONE, NULL, 0x0, + FT_STRINGZPAD, BASE_NONE, NULL, 0x0, NULL, HFILL } }, { &hf_hartip_pt_rsp_day, @@ -1460,14 +1442,14 @@ proto_register_hartip(void) /* Tag */ { &hf_hartip_pt_rsp_tag, { "Tag", "hart_ip.pt.rsp.tag", - FT_BYTES, BASE_NONE, NULL, 0x0, + FT_STRINGZPAD, BASE_NONE, NULL, 0x0, NULL, HFILL } }, /* Message */ { &hf_hartip_pt_rsp_message, { "Message", "hart_ip.pt.rsp.message", - FT_BYTES, BASE_NONE, NULL, 0x0, + FT_STRINGZPAD, BASE_NONE, NULL, 0x0, NULL, HFILL } }, diff --git a/epan/dissectors/packet-ospf.c b/epan/dissectors/packet-ospf.c index 281ba265b6..6d1a0a3b20 100644 --- a/epan/dissectors/packet-ospf.c +++ b/epan/dissectors/packet-ospf.c @@ -1042,7 +1042,7 @@ dissect_ospf(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) guint16 cksum, computed_cksum; guint length, reported_length; guint16 auth_type; - char auth_data[8+1]; + guint8 *auth_data; int crypto_len = 0; unsigned int ospf_header_length; guint8 instance_ID; @@ -1210,7 +1210,7 @@ dissect_ospf(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) break; case OSPF_AUTH_SIMPLE: - tvb_get_nstringz0(tvb, 16, 8+1, auth_data); + auth_data = tvb_get_string_enc(wmem_packet_scope(), tvb, 16, 8, ENC_ASCII|ENC_NA); proto_tree_add_text(ospf_header_tree, tvb, 16, 8, "Auth Data: %s", auth_data); break; diff --git a/epan/dissectors/packet-quake3.c b/epan/dissectors/packet-quake3.c index c9d5fad22d..3b8bfc328f 100644 --- a/epan/dissectors/packet-quake3.c +++ b/epan/dissectors/packet-quake3.c @@ -144,7 +144,7 @@ dissect_quake3_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *cl_tree = NULL; proto_item *text_item = NULL; proto_tree *text_tree = NULL; - guint8 text[2048]; + guint8 *text; int len; int offset; guint32 marker; @@ -165,11 +165,21 @@ dissect_quake3_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo _U_, /* all the rest of the packet is just text */ offset = 4; - len = tvb_get_nstringz0(tvb, offset, sizeof(text), text); + /* + * XXX - is there ever more than one null-terminated string in + * the packet? + * + * XXX - is the string guaranteed to be null-terminated (so + * that if there's no NUL at the end, it's an error)? + * + * XXX - are non-ASCII characters supported and, if so, what + * encoding is used for them? + */ + text = tvb_get_stringz_enc(wmem_packet_scope(), tvb, offset, &len, ENC_ASCII|ENC_NA); if (cl_tree) { text_item = proto_tree_add_string(cl_tree, hf_quake3_connectionless_text, - tvb, offset, len + 1, text); + tvb, offset, len, text); text_tree = proto_item_add_subtree(text_item, ett_quake3_connectionless_text); } @@ -321,7 +331,7 @@ dissect_quake3_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo _U_, val_to_str_const(command, names_command, "Unknown")); } - /*offset += len + 1;*/ + /*offset += len;*/ } diff --git a/epan/dissectors/packet-quakeworld.c b/epan/dissectors/packet-quakeworld.c index 16ac74a911..4ceb8a4fad 100644 --- a/epan/dissectors/packet-quakeworld.c +++ b/epan/dissectors/packet-quakeworld.c @@ -90,8 +90,8 @@ static char com_token[MAX_TEXT_SIZE+1]; static int com_token_start; static int com_token_length; -static char * -COM_Parse (char *data) +static const char * +COM_Parse (const char *data) { int c; int len; @@ -198,7 +198,7 @@ Cmd_Argv_length(int arg) static void -Cmd_TokenizeString(char* text) +Cmd_TokenizeString(const char* text) { int start; @@ -349,7 +349,7 @@ dissect_quakeworld_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo, { proto_tree *cl_tree = NULL; proto_tree *text_tree = NULL; - guint8 text[MAX_TEXT_SIZE+1]; + guint8 *text; int len; int offset; guint32 marker; @@ -370,13 +370,13 @@ dissect_quakeworld_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo, /* all the rest of the packet is just text */ offset = 4; - len = tvb_get_nstringz0(tvb, offset, sizeof(text), text); + text = tvb_get_stringz_enc(wmem_packet_scope(), tvb, offset, &len, ENC_ASCII|ENC_NA); /* actually, we should look for a eol char and stop already there */ if (cl_tree) { proto_item *text_item; text_item = proto_tree_add_string(cl_tree, hf_quakeworld_connectionless_text, - tvb, offset, len + 1, text); + tvb, offset, len, text); text_tree = proto_item_add_subtree(text_item, ett_quakeworld_connectionless_text); } @@ -469,7 +469,7 @@ dissect_quakeworld_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo, tvb, offset, command_len, command); argument_item = proto_tree_add_string(text_tree, hf_quakeworld_connectionless_arguments, - tvb, offset + Cmd_Argv_start(1), len + 1 - Cmd_Argv_start(1), + tvb, offset + Cmd_Argv_start(1), len - Cmd_Argv_start(1), text + Cmd_Argv_start(1)); argument_tree = proto_item_add_subtree(argument_item, ett_quakeworld_connectionless_arguments); @@ -504,7 +504,7 @@ dissect_quakeworld_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo, command_len = 1; } else { command = "Unknown"; - command_len = len; + command_len = len - 1; } } else { @@ -529,7 +529,7 @@ dissect_quakeworld_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo, /* string, atoi */ } else { command = "Unknown"; - command_len = len; + command_len = len - 1; } } @@ -539,7 +539,7 @@ dissect_quakeworld_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo, proto_tree_add_string(text_tree, hf_quakeworld_connectionless_command, tvb, offset, command_len, command); } - /*offset += len + 1;*/ + /*offset += len;*/ } diff --git a/epan/dissectors/packet-rsync.c b/epan/dissectors/packet-rsync.c index 95eb7f36c1..9448d70f7e 100644 --- a/epan/dissectors/packet-rsync.c +++ b/epan/dissectors/packet-rsync.c @@ -123,17 +123,19 @@ static dissector_handle_t rsync_handle; static guint glb_rsync_tcp_port = TCP_PORT_RSYNC; +#define VERSION_LEN 4 /* 2 digits for main version; '.'; 1 digit for sub version */ + static void dissect_rsync_version_header(tvbuff_t *tvb, packet_info *pinfo, proto_tree *rsync_tree, enum rsync_who me) { int offset = 0; - gchar version[5]; /* 2 digits for main version; '.'; 1 digit for sub version; NULL */ + guint8 *version; proto_tree_add_item(rsync_tree, &hfi_rsync_hdr_magic, tvb, offset, RSYNCD_MAGIC_HEADER_LEN, ENC_ASCII|ENC_NA); offset += RSYNCD_MAGIC_HEADER_LEN; offset += 1; /* skip the space */ - proto_tree_add_item(rsync_tree, &hfi_rsync_hdr_version, tvb, offset, sizeof(version)-1, ENC_ASCII|ENC_NA); - tvb_get_nstringz0(tvb, offset, sizeof(version), version); + proto_tree_add_item(rsync_tree, &hfi_rsync_hdr_version, tvb, offset, VERSION_LEN, ENC_ASCII|ENC_NA); + version = tvb_get_string_enc(wmem_packet_scope(),tvb, offset, VERSION_LEN, ENC_ASCII|ENC_NA); col_add_fstr(pinfo->cinfo, COL_INFO, "%s Initialisation (Version %s)", (me == SERVER ? "Server" : "Client"), version); } diff --git a/epan/dissectors/packet-smb-browse.c b/epan/dissectors/packet-smb-browse.c index 3c55bd1250..4c556ed0af 100644 --- a/epan/dissectors/packet-smb-browse.c +++ b/epan/dissectors/packet-smb-browse.c @@ -570,6 +570,7 @@ dissect_smb_server_type_flags(tvbuff_t *tvb, int offset, packet_info *pinfo, return offset; } +#define HOST_NAME_LEN 16 static void dissect_mailslot_browse(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree) @@ -579,8 +580,7 @@ dissect_mailslot_browse(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tr proto_tree *tree = NULL; proto_item *item = NULL; guint32 periodicity; - gchar host_name[17]; - gchar *utf8_host_name; + guint8 *host_name; gint namelen; guint8 server_count, reset_cmd; guint8 os_major_ver, os_minor_ver; @@ -621,22 +621,16 @@ dissect_mailslot_browse(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tr offset += 4; /* server name */ - tvb_get_nstringz0(tvb, offset, sizeof(host_name), host_name); - utf8_host_name = g_convert(host_name, strlen(host_name), - "UTF-8", "CP437", NULL, NULL, NULL); - if (utf8_host_name == NULL) - utf8_host_name = host_name; - col_append_fstr(pinfo->cinfo, COL_INFO, " %s", utf8_host_name); + host_name = tvb_get_stringzpad(wmem_packet_scope(), tvb, offset, HOST_NAME_LEN, ENC_CP437|ENC_NA); + col_append_fstr(pinfo->cinfo, COL_INFO, " %s", host_name); proto_tree_add_string_format(tree, hf_server_name, - tvb, offset, 16, - utf8_host_name, + tvb, offset, HOST_NAME_LEN, + host_name, (cmd==BROWSE_DOMAIN_ANNOUNCEMENT)? "Domain/Workgroup: %s": "Host Name: %s", - utf8_host_name); - if (utf8_host_name != host_name) - g_free(utf8_host_name); - offset += 16; + host_name); + offset += HOST_NAME_LEN; /* Windows version (See "OSVERSIONINFO Structure" on MSDN) */ os_major_ver = tvb_get_guint8(tvb, offset); @@ -838,7 +832,6 @@ dissect_mailslot_lanman(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tr /* Put in something, and replace it later */ col_add_str(pinfo->cinfo, COL_INFO, val_to_str(cmd, commands, "Unknown command:0x%02x")); - if (parent_tree) { item = proto_tree_add_item(parent_tree, proto_smb_browse, tvb, offset, -1, ENC_NA); @@ -853,7 +846,6 @@ dissect_mailslot_lanman(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tr case BROWSE_DOMAIN_ANNOUNCEMENT: case BROWSE_LOCAL_MASTER_ANNOUNCEMENT: case BROWSE_HOST_ANNOUNCE: - /* update count */ proto_tree_add_item(tree, hf_update_count, tvb, offset, 1, ENC_LITTLE_ENDIAN); offset += 1; @@ -888,7 +880,7 @@ dissect_mailslot_lanman(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tr offset += 2; /* server name */ - host_name = tvb_get_const_stringz(tvb, offset, &namelen); + host_name = tvb_get_stringz_enc(wmem_packet_scope(), tvb, offset, &namelen, ENC_CP437|ENC_NA); col_append_fstr(pinfo->cinfo, COL_INFO, " %s", host_name); proto_tree_add_item(tree, hf_server_name, @@ -900,7 +892,7 @@ dissect_mailslot_lanman(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tr proto_tree_add_item(tree, (cmd==BROWSE_DOMAIN_ANNOUNCEMENT)? hf_mb_server_name : hf_server_comment, - tvb, offset, namelen, ENC_ASCII|ENC_NA); + tvb, offset, namelen, ENC_CP437|ENC_NA); break; } } diff --git a/epan/dissectors/packet-tacacs.c b/epan/dissectors/packet-tacacs.c index 29339e7f6e..2ea2c8105b 100644 --- a/epan/dissectors/packet-tacacs.c +++ b/epan/dissectors/packet-tacacs.c @@ -396,14 +396,15 @@ static void dissect_tacplus_args_list( tvbuff_t *tvb, proto_tree *tree, int data_off, int len_off, int arg_cnt ) { int i; - guint8 buff[257]; + int len; + guint8 *value; for(i=0;i<arg_cnt;i++){ - int len=tvb_get_guint8(tvb,len_off+i); + len=tvb_get_guint8(tvb,len_off+i); proto_tree_add_uint_format(tree, hf_tacplus_arg_length, tvb, len_off+i, 1, len, "Arg[%d] length: %d", i, len); - tvb_get_nstringz0(tvb, data_off, len+1, buff); - proto_tree_add_string_format(tree, hf_tacplus_arg_value, tvb, data_off, len, buff, - "Arg[%d] value: %s", i, buff); + value=tvb_get_string_enc(wmem_packet_scope(), tvb, data_off, len, ENC_ASCII|ENC_NA); + proto_tree_add_string_format(tree, hf_tacplus_arg_value, tvb, data_off, len, value, + "Arg[%d] value: %s", i, value); data_off+=len; } } diff --git a/epan/dissectors/packet-who.c b/epan/dissectors/packet-who.c index 05afd84cce..d1c604bb5e 100644 --- a/epan/dissectors/packet-who.c +++ b/epan/dissectors/packet-who.c @@ -56,12 +56,9 @@ RWHOD(8) UNIX System Manager's Manual RWHOD(8) (20 each) int we_idle; } wd_we[1024 / sizeof (struct whoent)]; }; - - Linux 2.0 May 13, 1997 2 - * */ - + void proto_register_who(void); void proto_reg_handoff_who(void); @@ -94,7 +91,7 @@ dissect_who(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) int offset = 0; proto_tree *who_tree = NULL; proto_item *who_ti = NULL; - gchar server_name[33]; + guint8 *server_name; double loadav_5 = 0.0, loadav_10 = 0.0, loadav_15 = 0.0; nstime_t ts; @@ -135,7 +132,7 @@ dissect_who(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) } offset += 4; - tvb_get_nstringz0(tvb, offset, sizeof(server_name), (guint8*)server_name); + server_name = tvb_get_stringzpad(wmem_packet_scope(), tvb, offset, 32, ENC_ASCII|ENC_NA); if (tree) proto_tree_add_string(who_tree, hf_who_hostname, tvb, offset, 32, server_name); @@ -184,8 +181,8 @@ dissect_whoent(tvbuff_t *tvb, int offset, proto_tree *tree) proto_tree *whoent_tree = NULL; proto_item *whoent_ti = NULL; int line_offset = offset; - gchar out_line[9]; - gchar out_name[9]; + guint8 *out_line; + guint8 *out_name; nstime_t ts; int whoent_num = 0; guint32 idle_secs; /* say that out loud... */ @@ -198,12 +195,12 @@ dissect_whoent(tvbuff_t *tvb, int offset, proto_tree *tree) line_offset, SIZE_OF_WHOENT, ENC_NA); whoent_tree = proto_item_add_subtree(whoent_ti, ett_whoent); - tvb_get_nstringz0(tvb, line_offset, sizeof(out_line), (guint8*)out_line); + out_line = tvb_get_stringzpad(wmem_packet_scope(), tvb, line_offset, 8, ENC_ASCII|ENC_NA); proto_tree_add_string(whoent_tree, hf_who_tty, tvb, line_offset, 8, out_line); line_offset += 8; - tvb_get_nstringz0(tvb, line_offset, sizeof(out_name), (guint8*)out_name); + out_name = tvb_get_stringzpad(wmem_packet_scope(), tvb, line_offset, 8, ENC_ASCII|ENC_NA); proto_tree_add_string(whoent_tree, hf_who_uid, tvb, line_offset, 8, out_name); line_offset += 8; |