GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Bencode(3) User Contributed Perl Documentation Bencode(3)

Bencode - BitTorrent serialisation format

version 1.500

 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/beps/bep_0003.html#bencoding>.

Takes data to be encoded as a single argument which may be a scalar, or may be a reference to either a scalar, an array or a hash. Arrays and hashes may in turn contain values of these same types. Plain 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.

The second argument is optional (in which case it defaults to "str") and specifies how to treat "undef" values. You can pick one of three options:

"str"
to encode "undef"s as empty strings;
"num"
to encode "undef"s as zeroes;
"die"
to croak upon encountering an "undef" value.

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.

"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.
"nesting depth exceeded at %s"
Your data contains dicts or lists that are nested deeper than the $max_depth passed to "bdecode()".
"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.

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.

Aristotle Pagaltzis <pagaltzis@gmx.de>

This software is copyright (c) 2017 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.

2017-09-27 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.