Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-data.c
blob78ac3d9e0ff07d5ec3efd9b095c70859a248b693
1 /* packet-data.c
2 * Routines for raw data (default case)
3 * Gilbert Ramirez <gram@alumni.rice.edu>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
11 #include "config.h"
13 #include <epan/packet.h>
14 #include <epan/prefs.h>
15 #include <epan/to_str.h>
16 #include <wsutil/wsgcrypt.h>
17 #include <wsutil/str_util.h>
19 #include "packet-tls.h"
20 #include "packet-dtls.h"
22 void proto_register_data(void);
23 void proto_reg_handoff_data(void);
26 static int proto_data;
28 static int hf_data_data;
29 static int hf_data_len;
30 static int hf_data_md5_hash;
31 static int hf_data_text;
32 static int hf_data_uncompressed_data;
33 static int hf_data_uncompressed_len;
35 static bool new_pane;
36 static bool uncompress_data;
37 static bool show_as_text;
38 static bool generate_md5_hash;
40 static int ett_data;
42 static dissector_handle_t data_handle;
44 static int
45 dissect_data(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
47 int bytes;
48 char *display_str;
50 if (tree) {
51 bytes = tvb_captured_length(tvb);
52 if (bytes > 0) {
53 tvbuff_t *data_tvb;
54 tvbuff_t *uncompr_tvb = NULL;
55 int uncompr_len = 0;
56 proto_item *ti;
57 proto_tree *data_tree;
58 if (new_pane) {
59 uint8_t *real_data = (uint8_t *)tvb_memdup(pinfo->pool, tvb, 0, bytes);
60 data_tvb = tvb_new_child_real_data(tvb,real_data,bytes,bytes);
61 add_new_data_source(pinfo, data_tvb, "Not dissected data bytes");
62 } else {
63 data_tvb = tvb;
65 ti = proto_tree_add_protocol_format(tree, proto_data, tvb,
67 bytes, "Data (%d byte%s)", bytes,
68 plurality(bytes, "", "s"));
69 data_tree = proto_item_add_subtree(ti, ett_data);
71 proto_tree_add_item(data_tree, hf_data_data, data_tvb, 0, bytes, ENC_NA);
73 if (uncompress_data) {
74 uncompr_tvb = tvb_child_uncompress_zlib(data_tvb, data_tvb, 0, tvb_reported_length(data_tvb));
76 if (uncompr_tvb) {
77 uncompr_len = tvb_reported_length(uncompr_tvb);
78 add_new_data_source(pinfo, uncompr_tvb, "Uncompressed Data");
79 proto_tree_add_item(data_tree, hf_data_uncompressed_data, uncompr_tvb, 0, uncompr_len, ENC_NA);
80 ti = proto_tree_add_int(data_tree, hf_data_uncompressed_len, uncompr_tvb, 0, 0, uncompr_len);
81 proto_item_set_generated (ti);
85 if (show_as_text) {
86 tvbuff_t *text_tvb;
87 int text_length;
88 if (uncompr_tvb && uncompr_len > 0) {
89 text_tvb = uncompr_tvb;
90 text_length = uncompr_len;
91 } else {
92 text_tvb = data_tvb;
93 text_length = bytes;
95 proto_tree_add_item_ret_display_string(data_tree, hf_data_text, text_tvb, 0, text_length, ENC_UTF_8, pinfo->pool, &display_str);
96 col_add_str(pinfo->cinfo, COL_INFO, display_str);
99 if(generate_md5_hash) {
100 const uint8_t *cp;
101 uint8_t digest[HASH_MD5_LENGTH];
102 const char *digest_string;
104 cp = tvb_get_ptr(tvb, 0, bytes);
106 gcry_md_hash_buffer(GCRY_MD_MD5, digest, cp, bytes);
107 digest_string = bytes_to_str_punct(pinfo->pool, digest, HASH_MD5_LENGTH, '\0');
108 ti = proto_tree_add_string(data_tree, hf_data_md5_hash, tvb, 0, 0, digest_string);
109 proto_item_set_generated(ti);
112 ti = proto_tree_add_int(data_tree, hf_data_len, data_tvb, 0, 0, bytes);
113 proto_item_set_generated (ti);
116 return tvb_captured_length(tvb);
119 void
120 proto_register_data(void)
122 static hf_register_info hf[] = {
123 { &hf_data_data,
124 { "Data", "data.data",
125 FT_BYTES, BASE_NONE, NULL, 0x0,
126 NULL, HFILL }
128 { &hf_data_text,
129 { "Text", "data.text",
130 FT_STRING, BASE_NONE, NULL, 0x0,
131 NULL, HFILL }
133 { &hf_data_uncompressed_data,
134 { "Uncompressed Data", "data.uncompressed.data",
135 FT_BYTES, BASE_NONE, NULL, 0x0,
136 NULL, HFILL }
138 { &hf_data_uncompressed_len,
139 { "Uncompressed Length", "data.uncompressed.len",
140 FT_INT32, BASE_DEC, NULL, 0x0,
141 NULL, HFILL }
143 { &hf_data_len,
144 { "Length", "data.len",
145 FT_INT32, BASE_DEC, NULL, 0x0,
146 NULL, HFILL }
148 { &hf_data_md5_hash,
149 { "Payload MD5 hash", "data.md5_hash",
150 FT_STRING, BASE_NONE, NULL, 0x0,
151 NULL, HFILL }
155 static int *ett[] = {
156 &ett_data
159 module_t *module_data;
161 proto_data = proto_register_protocol (
162 "Data", /* name */
163 "Data", /* short name */
164 "data" /* abbrev */
167 data_handle = register_dissector("data", dissect_data, proto_data);
169 proto_register_field_array(proto_data, hf, array_length(hf));
170 proto_register_subtree_array(ett, array_length(ett));
172 module_data = prefs_register_protocol( proto_data, NULL);
173 prefs_register_bool_preference(module_data,
174 "datapref.newpane",
175 "Show not dissected data on new Packet Bytes pane",
176 "Show not dissected data on new Packet Bytes pane",
177 &new_pane);
178 #if defined (HAVE_ZLIB) || defined (HAVE_ZLIBNG)
179 prefs_register_bool_preference(module_data,
180 "uncompress_data",
181 "Try to uncompress zlib compressed data",
182 "Try to uncompress zlib compressed data and show as uncompressed if successful",
183 &uncompress_data);
184 #endif
185 prefs_register_bool_preference(module_data,
186 "show_as_text",
187 "Show data as text",
188 "Show data as text in the Packet Details pane",
189 &show_as_text);
190 prefs_register_bool_preference(module_data,
191 "md5_hash",
192 "Generate MD5 hash",
193 "Whether or not MD5 hashes should be generated and shown for each payload.",
194 &generate_md5_hash);
197 * "Data" is used to dissect something whose normal dissector
198 * is disabled, so it cannot itself be disabled.
200 proto_set_cant_toggle(proto_data);
203 static void
204 add_foreach_decode_as(const char *table_name, const char *ui_name _U_, void *user_data)
206 dissector_handle_t handle = (dissector_handle_t) user_data;
207 dissector_table_t dissector_table = find_dissector_table(table_name);
210 if (dissector_table_supports_decode_as(dissector_table))
211 dissector_add_for_decode_as(table_name, handle);
214 void
215 proto_reg_handoff_data(void)
217 dissector_add_string("media_type", "application/octet-stream", data_handle);
218 ssl_dissector_add(0, data_handle);
219 dtls_dissector_add(0, data_handle);
221 dissector_all_tables_foreach_table(add_foreach_decode_as, (void *)data_handle, NULL);
225 * Editor modelines - https://www.wireshark.org/tools/modelines.html
227 * Local variables:
228 * c-basic-offset: 8
229 * tab-width: 8
230 * indent-tabs-mode: t
231 * End:
233 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
234 * :indentSize=8:tabSize=8:noTabs=false: