Bittorrent serialisation format
version 1.4
use Bencode qw( bencode bdecode ); my $bencoded = bencode { 'age' => 25, 'eyes' => 'blue' }; print $bencoded, "\n"; my $decoded = bdecode $bencoded;
This module implements the BitTorrent bencode serialisation format as described in <http://www.bittorrent.org/protocol.html>.
Takes a single argument which may be a scalar or a reference to a scalar, array or hash. Arrays and hashes may in turn contain values of these same types. Simple scalars that look like canonically represented integers will be serialised as such. To bypass the heuristic and force serialisation as a string, use a reference to a scalar.
Croaks on unhandled data types. Takes a string and returns the corresponding deserialised data structure.
If you pass a true value for the second option, it will disregard the sort order of dict keys. This violation of the bencode format is somewhat common.
If you pass an integer for the third option, it will croak when attempting to parse dictionaries nested deeper than this level, to prevent DoS attacks using maliciously crafted input.
Croaks on malformed data.
Your data does not end after the first bencode-serialised item. You may also get this error if a malformed item follows. Your data is malformed. Your data is truncated. Your data includes a string declared to be longer than the available data. Your data contained a string with negative length or a length with leading zeroes. Your data contained something that was supposed to be an integer but didn't make sense. Your data violates the bencode format constaint that dict keys must appear in lexical sort order. Your data violates the bencode format constaint that all dict keys must be unique. Your data violates the bencode format constaint that all dict keys be strings. Your data contains a dictionary with an odd number of elements. Your data contains dicts or lists that are nested deeper than the $max_depth passed to \*(C`bdecode()\*(C'. You are trying to serialise a data structure that consists of data types other than
scalars
references to arrays
references to hashes
references to scalars
The format does not support this.
Strings and numbers are practically indistinguishable in Perl, so \*(C`bencode()\*(C' has to resort to a heuristic to decide how to serialise a scalar. This cannot be fixed.
Please report any bugs or feature requests through the web interface at <http://github.com/ap/Bencode/issues>.
Aristotle Pagaltzis <[email protected]>
This software is copyright (c) 2010 by Aristotle Pagaltzis.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.