The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Bencode - BitTorrent serialisation format

VERSION

This document describes Bencode version 1.0

SYNOPSIS

 use Bencode qw( bencode bdecode );
 
 my $bencoded = bencode { 'age' => 25, 'eyes' => 'blue' };
 print $bencoded, "\n";
 my $decoded = bdecode $bencoded;

DESCRIPTION

This module implements the BitTorrent bencode serialisation format as described in http://www.bittorrent.org/protocol.html.

INTERFACE

bencode( $datastructure )

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.

bdecode( $string [, $do_lenient_decode ] )

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 becode format is somewhat common.

Croaks on malformed data.

DIAGNOSTICS

trailing garbage at %s

Your data does not end after the first bencode-serialised item.

You may also get this error if a malformed item follows.

garbage at %s

Your data is malformed.

unexpected end of data at %s

Your data is truncated.

unexpected end of string data starting at %s

Your data includes a string declared to be longer than the available data.

malformed string length at %s

Your data contained a string with negative length or a length with leading zeroes.

malformed integer data at %s

Your data contained something that was supposed to be an integer but didn't make sense.

dict key not in sort order at %s

Your data violates the bencode format constaint that dict keys must appear in lexical sort order.

duplicate dict key at %s

Your data violates the bencode format constaint that all dict keys must be unique.

dict key is not a string at %s

Your data violates the bencode format constaint that all dict keys be strings.

dict key is missing value at %s

Your data contains a dictionary with an odd number of elements.

unhandled data type

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.

BUGS AND LIMITATIONS

Strings and numbers are practically indistinguishable in Perl, so bencode() has to resort to a heuristic to decide how to serialise a scalar. This cannot be fixed.

Error reporting is currently suboptimal. Malformed strings or integers will throw a misleading trailing garbage message instead of a more specific diagnostic.

No bugs have been reported.

Please report any bugs or feature requests to bug-bencode@rt.cpan.org, or through the web interface at http://rt.cpan.org.

AUTHOR

Aristotle Pagaltzis mailto:pagaltzis@gmx.de

LICENCE AND COPYRIGHT

Copyright (c) 2006, Aristotle Pagaltzis. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.