1Bencode(3) User Contributed Perl Documentation Bencode(3)
2
3
4
6 Bencode - BitTorrent serialisation format
7
9 version 1.501
10
12 use Bencode qw( bencode bdecode );
13
14 my $bencoded = bencode { 'age' => 25, 'eyes' => 'blue' };
15 print $bencoded, "\n";
16 my $decoded = bdecode $bencoded;
17
19 This module implements the BitTorrent bencode serialisation format, as
20 described in <http://www.bittorrent.org/beps/bep_0003.html#bencoding>.
21
23 "bencode( $datastructure [, $undef_mode ] )"
24 Takes data to be encoded as a single argument which may be a scalar, or
25 may be a reference to either a scalar, an array or a hash. Arrays and
26 hashes may in turn contain values of these same types. Plain scalars
27 that look like canonically represented integers will be serialised as
28 such. To bypass the heuristic and force serialisation as a string, use
29 a reference to a scalar.
30
31 The second argument is optional (in which case it defaults to "str")
32 and specifies how to treat "undef" values. You can pick one of three
33 options:
34
35 "str" to encode "undef"s as empty strings;
36
37 "num" to encode "undef"s as zeroes;
38
39 "die" to croak upon encountering an "undef" value.
40
41 Croaks on unhandled data types.
42
43 "bdecode( $string [, $do_lenient_decode [, $max_depth ] ] )"
44 Takes a string and returns the corresponding deserialised data
45 structure.
46
47 If you pass a true value for the second option, it will disregard the
48 sort order of dict keys. This violation of the bencode format is
49 somewhat common.
50
51 If you pass an integer for the third option, it will croak when
52 attempting to parse dictionaries nested deeper than this level, to
53 prevent DoS attacks using maliciously crafted input.
54
55 Croaks on malformed data.
56
58 "trailing garbage at %s"
59 Your data does not end after the first bencode-serialised item.
60
61 You may also get this error if a malformed item follows.
62
63 "garbage at %s"
64 Your data is malformed.
65
66 "unexpected end of data at %s"
67 Your data is truncated.
68
69 "unexpected end of string data starting at %s"
70 Your data includes a string declared to be longer than the
71 available data.
72
73 "malformed string length at %s"
74 Your data contained a string with negative length or a length with
75 leading zeroes.
76
77 "malformed integer data at %s"
78 Your data contained something that was supposed to be an integer
79 but didn't make sense.
80
81 "dict key not in sort order at %s"
82 Your data violates the bencode format constaint that dict keys must
83 appear in lexical sort order.
84
85 "duplicate dict key at %s"
86 Your data violates the bencode format constaint that all dict keys
87 must be unique.
88
89 "dict key is not a string at %s"
90 Your data violates the bencode format constaint that all dict keys
91 be strings.
92
93 "dict key is missing value at %s"
94 Your data contains a dictionary with an odd number of elements.
95
96 "nesting depth exceeded at %s"
97 Your data contains dicts or lists that are nested deeper than the
98 $max_depth passed to "bdecode()".
99
100 "unhandled data type"
101 You are trying to serialise a data structure that consists of data
102 types other than
103
104 • scalars
105
106 • references to arrays
107
108 • references to hashes
109
110 • references to scalars
111
112 The format does not support this.
113
115 Strings and numbers are practically indistinguishable in Perl, so
116 "bencode()" has to resort to a heuristic to decide how to serialise a
117 scalar. This cannot be fixed.
118
120 Aristotle Pagaltzis <pagaltzis@gmx.de>
121
123 This software is copyright (c) 2017 by Aristotle Pagaltzis.
124
125 This is free software; you can redistribute it and/or modify it under
126 the same terms as the Perl 5 programming language system itself.
127
128
129
130perl v5.34.0 2021-07-22 Bencode(3)