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