Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-bencode.c
blob36dda2536215d0712ff078d3d69a4309e535ce2e
1 /* packet-bencode.c
2 * Routines for bencode dissection
3 * Copyright (C) 2004,2013 Jelmer Vernooij <jelmer@samba.org>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * Copied from packet-pop.c
11 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include "config.h"
16 #include <epan/packet.h>
17 #include <epan/expert.h>
18 #include <epan/strutil.h>
20 void proto_register_bencode(void);
22 static int proto_bencode;
24 static int hf_bencode_str_length;
25 static int hf_bencode_str;
26 static int hf_bencode_int;
27 static int hf_bencode_dict;
28 static int hf_bencode_dict_entry;
29 static int hf_bencode_list;
30 static int hf_bencode_truncated_data;
32 static int ett_bencode_dict;
33 static int ett_bencode_dict_entry;
34 static int ett_bencode_list;
36 static expert_field ei_bencode_str;
37 static expert_field ei_bencode_str_length;
38 static expert_field ei_bencode_int;
39 static expert_field ei_bencode_nest;
40 static expert_field ei_bencode_dict_key;
41 static expert_field ei_bencode_dict_value;
42 static expert_field ei_bencode_invalid;
44 static int dissect_bencoding_str(tvbuff_t *tvb, packet_info *pinfo,
45 int offset, int length, proto_tree *tree, proto_item *ti, int treeadd)
47 uint8_t ch;
48 int stringlen = 0, nextstringlen;
49 int used;
50 int izero = 0;
52 if (length < 2) {
53 proto_tree_add_expert(tree, pinfo, &ei_bencode_str, tvb, offset, length);
54 return -1;
57 used = 0;
59 while (length >= 1) {
60 ch = tvb_get_uint8(tvb, offset + used);
61 length--;
62 used++;
64 if ((ch == ':') && (used > 1)) {
65 if ((stringlen > length) || (stringlen < 0)) {
66 proto_tree_add_expert(tree, pinfo, &ei_bencode_str_length, tvb, offset, length);
67 return -1;
69 if (tree) {
70 proto_tree_add_uint(tree, hf_bencode_str_length, tvb, offset, used, stringlen);
71 proto_tree_add_item(tree, hf_bencode_str, tvb, offset + used, stringlen, ENC_ASCII);
73 if (treeadd == 1) {
74 proto_item_append_text(ti, " Key: %s",
75 tvb_format_text(pinfo->pool, tvb, offset + used, stringlen));
77 if (treeadd == 2) {
78 proto_item_append_text(ti, " Value: %s",
79 tvb_format_text(pinfo->pool, tvb, offset + used, stringlen));
82 return used + stringlen;
85 if (!izero && (ch >= '0') && (ch <= '9')) {
86 if ((ch == '0') && (used == 1)) {
87 izero = 1;
90 nextstringlen = (stringlen * 10) + (ch - '0');
91 if (nextstringlen >= stringlen) {
92 stringlen = nextstringlen;
93 continue;
97 proto_tree_add_expert(tree, pinfo, &ei_bencode_str, tvb, offset, length);
98 return -1;
101 proto_tree_add_item(tree, hf_bencode_truncated_data, tvb, offset, length, ENC_NA);
102 return -1;
105 static int dissect_bencoding_int(tvbuff_t *tvb, packet_info *pinfo,
106 int offset, int length, proto_tree *tree, proto_item *ti, int treeadd)
108 int32_t ival = 0;
109 int neg = 0;
110 int izero = 0;
111 int used;
112 uint8_t ch;
114 if (length<3) {
115 proto_tree_add_expert(tree, pinfo, &ei_bencode_int, tvb, offset, length);
116 return -1;
119 length--;
120 used = 1;
122 while (length >= 1) {
123 ch = tvb_get_uint8(tvb, offset + used);
124 length--;
125 used++;
127 switch (ch) {
128 case 'e':
129 if (tree) {
130 if (neg) ival = -ival;
131 proto_tree_add_int(tree, hf_bencode_int, tvb, offset, used, ival);
132 if (treeadd == 2) {
133 proto_item_append_text(ti, " Value: %d", ival);
136 return used;
138 case '-':
139 if (used == 2) {
140 neg = 1;
141 break;
143 /* Fall through */
145 default:
146 if (!((ch == '0') && (used == 3) && neg)) { /* -0 is invalid */
147 if ((ch == '0') && (used == 2)) { /* as is 0[0-9]+ */
148 izero = 1;
149 break;
151 if (!izero && (ch >= '0') && (ch <= '9')) {
152 ival = (ival * 10) + (ch - '0');
153 break;
157 proto_tree_add_expert(tree, pinfo, &ei_bencode_int, tvb, offset, length);
158 return -1;
162 proto_tree_add_item(tree, hf_bencode_truncated_data, tvb, offset, length, ENC_NA);
163 return -1;
166 // NOLINTNEXTLINE(misc-no-recursion)
167 static int dissect_bencoding_rec(tvbuff_t *tvb, packet_info *pinfo,
168 int offset, int length, proto_tree *tree, int level, proto_item *treei, int treeadd)
170 uint8_t op;
171 int oplen = 0, op1len, op2len;
172 int used;
174 proto_item *ti = NULL, *td = NULL;
175 proto_tree *itree = NULL, *dtree = NULL;
177 if (level > 10) {
178 proto_tree_add_expert(tree, pinfo, &ei_bencode_nest, tvb, offset, -1);
179 return -1;
181 if (length < 1) {
182 proto_tree_add_item(tree, hf_bencode_truncated_data, tvb, offset, -1, ENC_NA);
183 return length;
186 op = tvb_get_uint8(tvb, offset);
187 oplen = length;
189 switch (op) {
190 case 'd':
191 td = proto_tree_add_item(tree, hf_bencode_dict, tvb, offset, oplen, ENC_NA);
192 dtree = proto_item_add_subtree(td, ett_bencode_dict);
194 used = 1;
195 length--;
197 while (length >= 1) {
198 op = tvb_get_uint8(tvb, offset + used);
200 if (op == 'e') {
201 return used + 1;
204 op1len = dissect_bencoding_str(tvb, pinfo, offset + used, length, NULL, NULL, 0);
205 if (op1len < 0) {
206 proto_tree_add_expert(dtree, pinfo, &ei_bencode_dict_key, tvb, offset + used, -1);
207 return op1len;
210 op2len = -1;
211 if ((length - op1len) > 2) {
212 increment_dissection_depth(pinfo);
213 op2len = dissect_bencoding_rec(tvb, pinfo, offset + used + op1len, length - op1len, NULL, level + 1, NULL, 0);
214 decrement_dissection_depth(pinfo);
217 if (op2len < 0) {
218 proto_tree_add_expert(dtree, pinfo, &ei_bencode_dict_value, tvb, offset + used + op1len, -1);
219 return op2len;
222 ti = proto_tree_add_item(dtree, hf_bencode_dict_entry, tvb, offset + used, op1len + op2len, ENC_NA);
223 itree = proto_item_add_subtree(ti, ett_bencode_dict_entry);
225 dissect_bencoding_str(tvb, pinfo, offset + used, length, itree, ti, 1);
226 increment_dissection_depth(pinfo);
227 dissect_bencoding_rec(tvb, pinfo, offset + used + op1len, length - op1len, itree, level + 1, ti, 2);
228 decrement_dissection_depth(pinfo);
230 used += op1len + op2len;
231 length -= op1len + op2len;
234 proto_tree_add_item(dtree, hf_bencode_truncated_data, tvb, offset + used, length ? -1 : 0, ENC_NA);
235 return -1;
237 case 'l':
238 ti = proto_tree_add_item(tree, hf_bencode_list, tvb, offset, oplen, ENC_NA);
239 itree = proto_item_add_subtree(ti, ett_bencode_list);
241 used = 1;
242 length--;
244 increment_dissection_depth(pinfo);
245 while (length >= 1) {
246 op = tvb_get_uint8(tvb, offset + used);
248 if (op == 'e') {
249 return used + 1;
252 oplen = dissect_bencoding_rec(tvb, pinfo, offset + used, length, itree, level + 1, ti, 0);
254 if (oplen < 1) {
255 decrement_dissection_depth(pinfo);
256 return oplen;
259 used += oplen;
260 length -= oplen;
262 decrement_dissection_depth(pinfo);
264 proto_tree_add_item(itree, hf_bencode_truncated_data, tvb, offset + used, -1, ENC_NA);
265 return -1;
267 case 'i':
268 return dissect_bencoding_int(tvb, pinfo, offset, length, tree, treei, treeadd);
270 default:
271 if ((op >= '1') && (op <= '9')) {
272 return dissect_bencoding_str(tvb, pinfo, offset, length, tree, treei, treeadd);
275 proto_tree_add_expert(tree, pinfo, &ei_bencode_invalid, tvb, offset, -1);
278 return -1;
281 static int dissect_bencoding(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
283 dissect_bencoding_rec(tvb, pinfo, 0, tvb_reported_length(tvb), tree, 0, NULL, 0);
284 return tvb_captured_length(tvb);
287 void
288 proto_register_bencode(void)
290 static hf_register_info hf[] = {
291 { &hf_bencode_str_length,
292 { "String Length", "bencode.str.length", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }
294 { &hf_bencode_str,
295 { "String", "bencode.str", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }
297 { &hf_bencode_int,
298 { "Integer", "bencode.int", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL }
300 { &hf_bencode_dict,
301 { "Dictionary", "bencode.dict", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
303 { &hf_bencode_dict_entry,
304 { "Entry", "bencode.dict.entry", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
306 { &hf_bencode_list,
307 { "List", "bencode.list", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
309 { &hf_bencode_truncated_data,
310 { "Truncated Data", "bencode.truncated_data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }
314 static int *ett[] = {
315 &ett_bencode_dict,
316 &ett_bencode_dict_entry,
317 &ett_bencode_list,
320 static ei_register_info ei[] = {
321 { &ei_bencode_str, { "bencode.str.invalid", PI_MALFORMED, PI_ERROR, "Decode Aborted: Invalid String", EXPFILL }},
322 { &ei_bencode_str_length, { "bencode.str.length.invalid", PI_MALFORMED, PI_ERROR, "Decode Aborted: Invalid String Length", EXPFILL }},
323 { &ei_bencode_int, { "bencode.int.invalid", PI_MALFORMED, PI_ERROR, "Decode Aborted: Invalid Integer", EXPFILL }},
324 { &ei_bencode_nest, { "bencode.nest", PI_MALFORMED, PI_ERROR, "Decode Aborted: Nested Too Deep", EXPFILL }},
325 { &ei_bencode_dict_key, { "bencode.dict.key_invalid", PI_MALFORMED, PI_ERROR, "Decode Aborted: Invalid Dictionary Key", EXPFILL }},
326 { &ei_bencode_dict_value, { "bencode.dict.value_invalid", PI_MALFORMED, PI_ERROR, "Decode Aborted: Invalid Dictionary Value", EXPFILL }},
327 { &ei_bencode_invalid, { "bencode.invalid", PI_MALFORMED, PI_ERROR, "Invalid Bencoding", EXPFILL }},
330 expert_module_t* expert_bencode;
332 proto_bencode = proto_register_protocol("Bencode", "Bencode", "bencode");
333 register_dissector("bencode", dissect_bencoding, proto_bencode);
334 proto_register_field_array(proto_bencode, hf, array_length(hf));
335 proto_register_subtree_array(ett, array_length(ett));
336 expert_bencode = expert_register_protocol(proto_bencode);
337 expert_register_field_array(expert_bencode, ei, array_length(ei));
341 * Editor modelines - https://www.wireshark.org/tools/modelines.html
343 * Local variables:
344 * c-basic-offset: 3
345 * tab-width: 8
346 * indent-tabs-mode: nil
347 * End:
349 * vi: set shiftwidth=3 tabstop=8 expandtab:
350 * :indentSize=3:tabSize=8:noTabs=true: