.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Bencode 3pm" .TH Bencode 3pm "2022-08-18" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Bencode \- BitTorrent serialisation format .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Bencode qw( bencode bdecode ); \& \& my $bencoded = bencode { \*(Aqage\*(Aq => 25, \*(Aqeyes\*(Aq => \*(Aqblue\*(Aq }; \& print $bencoded, "\en"; \& my $decoded = bdecode $bencoded; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module implements the BitTorrent \fIbencode\fR serialisation format, as described in . .SH "INTERFACE" .IX Header "INTERFACE" .ie n .SS """bencode( $datastructure [, $undef_mode ] )""" .el .SS "\f(CWbencode( $datastructure [, $undef_mode ] )\fP" .IX Subsection "bencode( $datastructure [, $undef_mode ] )" 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. .PP The second argument is optional (in which case it defaults to \f(CW\*(C`str\*(C'\fR) and specifies how to treat \f(CW\*(C`undef\*(C'\fR values. You can pick one of three options: .ie n .IP """str""" 6 .el .IP "\f(CWstr\fR" 6 .IX Item "str" to encode \f(CW\*(C`undef\*(C'\fRs as empty strings; .ie n .IP """num""" 6 .el .IP "\f(CWnum\fR" 6 .IX Item "num" to encode \f(CW\*(C`undef\*(C'\fRs as zeroes; .ie n .IP """die""" 6 .el .IP "\f(CWdie\fR" 6 .IX Item "die" to croak upon encountering an \f(CW\*(C`undef\*(C'\fR value. .PP Croaks on unhandled data types. .ie n .SS """bdecode( $string [, $do_lenient_decode [, $max_depth ] ] )""" .el .SS "\f(CWbdecode( $string [, $do_lenient_decode [, $max_depth ] ] )\fP" .IX Subsection "bdecode( $string [, $do_lenient_decode [, $max_depth ] ] )" Takes a string and returns the corresponding deserialised data structure. .PP If you pass a true value for the second option, it will disregard the sort order of dict keys. This violation of the \fIbencode\fR format is somewhat common. .PP 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. .PP Croaks on malformed data. .SH "DIAGNOSTICS" .IX Header "DIAGNOSTICS" .ie n .IP """trailing garbage at %s""" 4 .el .IP "\f(CWtrailing garbage at %s\fR" 4 .IX Item "trailing garbage at %s" Your data does not end after the first \fIbencode\fR\-serialised item. .Sp You may also get this error if a malformed item follows. .ie n .IP """garbage at %s""" 4 .el .IP "\f(CWgarbage at %s\fR" 4 .IX Item "garbage at %s" Your data is malformed. .ie n .IP """unexpected end of data at %s""" 4 .el .IP "\f(CWunexpected end of data at %s\fR" 4 .IX Item "unexpected end of data at %s" Your data is truncated. .ie n .IP """unexpected end of string data starting at %s""" 4 .el .IP "\f(CWunexpected end of string data starting at %s\fR" 4 .IX Item "unexpected end of string data starting at %s" Your data includes a string declared to be longer than the available data. .ie n .IP """malformed string length at %s""" 4 .el .IP "\f(CWmalformed string length at %s\fR" 4 .IX Item "malformed string length at %s" Your data contained a string with negative length or a length with leading zeroes. .ie n .IP """malformed integer data at %s""" 4 .el .IP "\f(CWmalformed integer data at %s\fR" 4 .IX Item "malformed integer data at %s" Your data contained something that was supposed to be an integer but didn't make sense. .ie n .IP """dict key not in sort order at %s""" 4 .el .IP "\f(CWdict key not in sort order at %s\fR" 4 .IX Item "dict key not in sort order at %s" Your data violates the \fIbencode\fR format constaint that dict keys must appear in lexical sort order. .ie n .IP """duplicate dict key at %s""" 4 .el .IP "\f(CWduplicate dict key at %s\fR" 4 .IX Item "duplicate dict key at %s" Your data violates the \fIbencode\fR format constaint that all dict keys must be unique. .ie n .IP """dict key is not a string at %s""" 4 .el .IP "\f(CWdict key is not a string at %s\fR" 4 .IX Item "dict key is not a string at %s" Your data violates the \fIbencode\fR format constaint that all dict keys be strings. .ie n .IP """dict key is missing value at %s""" 4 .el .IP "\f(CWdict key is missing value at %s\fR" 4 .IX Item "dict key is missing value at %s" Your data contains a dictionary with an odd number of elements. .ie n .IP """nesting depth exceeded at %s""" 4 .el .IP "\f(CWnesting depth exceeded at %s\fR" 4 .IX Item "nesting depth exceeded at %s" Your data contains dicts or lists that are nested deeper than the \f(CW$max_depth\fR passed to \f(CW\*(C`bdecode()\*(C'\fR. .ie n .IP """unhandled data type""" 4 .el .IP "\f(CWunhandled data type\fR" 4 .IX Item "unhandled data type" You are trying to serialise a data structure that consists of data types other than .RS 4 .IP "\(bu" 4 scalars .IP "\(bu" 4 references to arrays .IP "\(bu" 4 references to hashes .IP "\(bu" 4 references to scalars .RE .RS 4 .Sp The format does not support this. .RE .SH "BUGS AND LIMITATIONS" .IX Header "BUGS AND LIMITATIONS" Strings and numbers are practically indistinguishable in Perl, so \f(CW\*(C`bencode()\*(C'\fR has to resort to a heuristic to decide how to serialise a scalar. This cannot be fixed. .SH "AUTHOR" .IX Header "AUTHOR" Aristotle Pagaltzis .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2017 by Aristotle Pagaltzis. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.