Revert "Fixup: tvb_* -> tvb_captured"
[metze/wireshark/wip.git] / epan / dissectors / packet-data.c
1 /* packet-data.c
2  * Routines for raw data (default case)
3  * Gilbert Ramirez <gram@alumni.rice.edu>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #define NEW_PROTO_TREE_API
25
26 #include "config.h"
27
28 #include <glib.h>
29
30 #include <wsutil/md5.h>
31
32 #include <epan/packet.h>
33 #include <epan/prefs.h>
34 #include <epan/to_str.h>
35 #include "packet-data.h"
36
37 /* proto_data cannot be static because it's referenced in the
38  * print routines
39  */
40 void proto_register_data(void);
41
42 int proto_data = -1;
43
44 #define DATA_HFI_INIT HFI_INIT(proto_data)
45
46 static header_field_info hfi_data_data DATA_HFI_INIT =
47           { "Data", "data.data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL };
48
49 static header_field_info hfi_data_text DATA_HFI_INIT =
50           { "Text", "data.text", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL };
51
52 static header_field_info hfi_data_len DATA_HFI_INIT =
53           { "Length", "data.len", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL };
54
55 static header_field_info hfi_data_md5_hash DATA_HFI_INIT =
56           { "Payload MD5 hash", "data.md5_hash", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL };
57
58 static gboolean new_pane = FALSE;
59 static gboolean show_as_text = FALSE;
60 static gboolean generate_md5_hash = FALSE;
61
62 static gint ett_data = -1;
63
64 static void
65 dissect_data(tvbuff_t *tvb, packet_info *pinfo _U_ , proto_tree *tree)
66 {
67         gint bytes;
68
69         if (tree) {
70                 bytes = tvb_length_remaining(tvb, 0);
71                 if (bytes > 0) {
72                         tvbuff_t   *data_tvb;
73                         proto_item *ti;
74                         proto_tree *data_tree;
75                         if (new_pane) {
76                                 guint8 *real_data = (guint8 *)tvb_memdup(NULL, tvb, 0, bytes);
77                                 data_tvb = tvb_new_child_real_data(tvb,real_data,bytes,bytes);
78                                 tvb_set_free_cb(data_tvb, g_free);
79                                 add_new_data_source(pinfo, data_tvb, "Not dissected data bytes");
80                         } else {
81                                 data_tvb = tvb;
82                         }
83                         ti = proto_tree_add_protocol_format(tree, proto_data, tvb,
84                                 0,
85                                 bytes, "Data (%d byte%s)", bytes,
86                                 plurality(bytes, "", "s"));
87                         data_tree = proto_item_add_subtree(ti, ett_data);
88
89                         proto_tree_add_item(data_tree, &hfi_data_data, data_tvb, 0, bytes, ENC_NA);
90
91                         if (show_as_text) {
92                                 proto_tree_add_item(data_tree, &hfi_data_text, data_tvb, 0, bytes, ENC_ASCII|ENC_NA);
93                         }
94
95                         if(generate_md5_hash) {
96                                 const guint8 *cp;
97                                 md5_state_t   md_ctx;
98                                 md5_byte_t    digest[16];
99                                 const gchar  *digest_string;
100
101                                 cp = tvb_get_ptr(tvb, 0, bytes);
102
103                                 md5_init(&md_ctx);
104                                 md5_append(&md_ctx, cp, bytes);
105                                 md5_finish(&md_ctx, digest);
106
107                                 digest_string = bytestring_to_str(wmem_packet_scope(), digest, 16, '\0');
108                                 ti = proto_tree_add_string(data_tree, &hfi_data_md5_hash, tvb, 0, 0, digest_string);
109                                 PROTO_ITEM_SET_GENERATED(ti);
110                         }
111
112                         ti = proto_tree_add_int(data_tree, &hfi_data_len, data_tvb, 0, 0, bytes);
113                         PROTO_ITEM_SET_GENERATED (ti);
114                 }
115         }
116 }
117
118 void
119 proto_register_data(void)
120 {
121 #ifndef HAVE_HFI_SECTION_INIT
122         static header_field_info *hfi[] = {
123                 &hfi_data_data,
124                 &hfi_data_text,
125                 &hfi_data_md5_hash,
126                 &hfi_data_len,
127         };
128 #endif
129
130         static gint *ett[] = {
131                 &ett_data
132         };
133
134         module_t *module_data;
135
136         proto_data = proto_register_protocol (
137                 "Data",         /* name */
138                 "Data",         /* short name */
139                 "data"          /* abbrev */
140                 );
141
142         register_dissector("data", dissect_data, proto_data);
143
144         proto_register_fields(proto_data, hfi, array_length(hfi));
145         proto_register_subtree_array(ett, array_length(ett));
146
147         module_data = prefs_register_protocol( proto_data, NULL);
148         prefs_register_bool_preference(module_data,
149                 "datapref.newpane",
150                 "Show not dissected data on new Packet Bytes pane",
151                 "Show not dissected data on new Packet Bytes pane",
152                 &new_pane);
153         prefs_register_bool_preference(module_data,
154                 "show_as_text",
155                 "Show data as text",
156                 "Show data as text in the Packet Details pane",
157                 &show_as_text);
158         prefs_register_bool_preference(module_data,
159                                        "md5_hash",
160                                        "Generate MD5 hash",
161                                        "Whether or not MD5 hashes should be generated and shown for each payload.",
162                                        &generate_md5_hash);
163
164         /*
165          * "Data" is used to dissect something whose normal dissector
166          * is disabled, so it cannot itself be disabled.
167          */
168         proto_set_cant_toggle(proto_data);
169 }