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
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
)
48 int stringlen
= 0, nextstringlen
;
53 proto_tree_add_expert(tree
, pinfo
, &ei_bencode_str
, tvb
, offset
, length
);
60 ch
= tvb_get_uint8(tvb
, offset
+ 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
);
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
);
74 proto_item_append_text(ti
, " Key: %s",
75 tvb_format_text(pinfo
->pool
, tvb
, offset
+ used
, stringlen
));
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)) {
90 nextstringlen
= (stringlen
* 10) + (ch
- '0');
91 if (nextstringlen
>= stringlen
) {
92 stringlen
= nextstringlen
;
97 proto_tree_add_expert(tree
, pinfo
, &ei_bencode_str
, tvb
, offset
, length
);
101 proto_tree_add_item(tree
, hf_bencode_truncated_data
, tvb
, offset
, length
, ENC_NA
);
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
)
115 proto_tree_add_expert(tree
, pinfo
, &ei_bencode_int
, tvb
, offset
, length
);
122 while (length
>= 1) {
123 ch
= tvb_get_uint8(tvb
, offset
+ used
);
130 if (neg
) ival
= -ival
;
131 proto_tree_add_int(tree
, hf_bencode_int
, tvb
, offset
, used
, ival
);
133 proto_item_append_text(ti
, " Value: %d", ival
);
146 if (!((ch
== '0') && (used
== 3) && neg
)) { /* -0 is invalid */
147 if ((ch
== '0') && (used
== 2)) { /* as is 0[0-9]+ */
151 if (!izero
&& (ch
>= '0') && (ch
<= '9')) {
152 ival
= (ival
* 10) + (ch
- '0');
157 proto_tree_add_expert(tree
, pinfo
, &ei_bencode_int
, tvb
, offset
, length
);
162 proto_tree_add_item(tree
, hf_bencode_truncated_data
, tvb
, offset
, length
, ENC_NA
);
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
)
171 int oplen
= 0, op1len
, op2len
;
174 proto_item
*ti
= NULL
, *td
= NULL
;
175 proto_tree
*itree
= NULL
, *dtree
= NULL
;
178 proto_tree_add_expert(tree
, pinfo
, &ei_bencode_nest
, tvb
, offset
, -1);
182 proto_tree_add_item(tree
, hf_bencode_truncated_data
, tvb
, offset
, -1, ENC_NA
);
186 op
= tvb_get_uint8(tvb
, offset
);
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
);
197 while (length
>= 1) {
198 op
= tvb_get_uint8(tvb
, offset
+ used
);
204 op1len
= dissect_bencoding_str(tvb
, pinfo
, offset
+ used
, length
, NULL
, NULL
, 0);
206 proto_tree_add_expert(dtree
, pinfo
, &ei_bencode_dict_key
, tvb
, offset
+ used
, -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
);
218 proto_tree_add_expert(dtree
, pinfo
, &ei_bencode_dict_value
, tvb
, offset
+ used
+ op1len
, -1);
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
);
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
);
244 increment_dissection_depth(pinfo
);
245 while (length
>= 1) {
246 op
= tvb_get_uint8(tvb
, offset
+ used
);
252 oplen
= dissect_bencoding_rec(tvb
, pinfo
, offset
+ used
, length
, itree
, level
+ 1, ti
, 0);
255 decrement_dissection_depth(pinfo
);
262 decrement_dissection_depth(pinfo
);
264 proto_tree_add_item(itree
, hf_bencode_truncated_data
, tvb
, offset
+ used
, -1, ENC_NA
);
268 return dissect_bencoding_int(tvb
, pinfo
, offset
, length
, tree
, treei
, treeadd
);
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);
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
);
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
}
295 { "String", "bencode.str", FT_STRING
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
298 { "Integer", "bencode.int", FT_INT32
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
}
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
}
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
[] = {
316 &ett_bencode_dict_entry
,
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
346 * indent-tabs-mode: nil
349 * vi: set shiftwidth=3 tabstop=8 expandtab:
350 * :indentSize=3:tabSize=8:noTabs=true: