Bencode
view release on metacpan or search on metacpan
lib/Bencode.pm view on Meta::CPAN
$hash{ $key } = _bdecode_chunk();
}
return \%hash;
}
else {
croak m/ \G \z /xgc ? 'unexpected end of data' : 'garbage', ' at ', 0+pos;
}
}
sub bdecode {
local $_ = shift;
local $do_lenient_decode = shift;
local $max_depth = shift;
my $deserialised_data = _bdecode_chunk();
croak 'trailing garbage at ', 0+pos if $_ !~ m/ \G \z /xgc;
return $deserialised_data;
}
sub _bencode;
sub _bencode {
map
+( ( not defined ) ? ( $undef_encoding or croak 'unhandled data type' )
: ( not ref ) ? ( m/\A (?: 0 | -? [1-9] \d* ) \z/x ? 'i' . $_ . 'e' : length . ':' . $_ )
: ( 'SCALAR' eq ref ) ? ( length $$_ ) . ':' . $$_ # escape hatch -- use this to avoid num/str heuristics
: ( 'ARRAY' eq ref ) ? 'l' . ( join '', _bencode @$_ ) . 'e'
: ( 'HASH' eq ref ) ? 'd' . do { my @k = sort keys %$_; join '', map +( length $k[0] ) . ':' . ( shift @k ) . $_, _bencode @$_{ @k } } . 'e'
: croak 'unhandled data type'
), @_
}
sub bencode {
my $undef_mode = @_ == 2 ? pop : 'str';
$undef_mode = 'str' unless defined $undef_mode;
local $undef_encoding
= 'str' eq $undef_mode ? '0:'
: 'num' eq $undef_mode ? 'i0e'
: 'die' eq $undef_mode ? undef
: croak qq'undef_mode argument must be "str", "num", "die" or undefined, not "$undef_mode"';
croak 'need exactly one or two arguments' if @_ != 1;
( &_bencode )[0];
}
bdecode( 'i1e' );
__END__
=pod
=encoding UTF-8
=head1 NAME
Bencode - BitTorrent serialisation format
=head1 SYNOPSIS
use Bencode qw( bencode bdecode );
my $bencoded = bencode { 'age' => 25, 'eyes' => 'blue' };
print $bencoded, "\n";
my $decoded = bdecode $bencoded;
=head1 DESCRIPTION
This module implements the BitTorrent I<bencode> serialisation format,
as described in L<http://www.bittorrent.org/beps/bep_0003.html#bencoding>.
=head1 INTERFACE
=head2 C<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.
The second argument is optional (in which case it defaults to C<str>) and
specifies how to treat C<undef> values. You can pick one of three options:
=over 6
=item C<str>
to encode C<undef>s as empty strings;
=item C<num>
to encode C<undef>s as zeroes;
=item C<die>
to croak upon encountering an C<undef> value.
=back
Croaks on unhandled data types.
=head2 C<bdecode( $string [, $do_lenient_decode [, $max_depth ] ] )>
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 I<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.
=head1 DIAGNOSTICS
=over
=item C<trailing garbage at %s>
Your data does not end after the first I<bencode>-serialised item.
( run in 0.673 second using v1.01-cache-2.11-cpan-df04353d9ac )