blib/lib/Convert/Bencode.pm

filter: Statementstmt 99.0% 99 / 100 Branchbran 95.4% 21 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 2 totaltotal 97.0% 130 / 134 CC 15 SCAR 17.9
CC
15
Cov
98%
_dechunk
10.0
bencode
6.0
BEGIN
1.0
CRAP 6.0
↑ Summary
1 #!/usr/bin/perl -w
2 package Convert::Bencode;
3  
4 =head1 NAME
5  
6 Convert::Bencode - Functions for converting to/from bencoded strings
7  
8 =head1 SYNOPSIS
9  
10 use Convert::Bencode qw(bencode bdecode);
11  
12 my $string = "d4:ainti12345e3:key5:value4:type4:teste";
13 my $hashref = bdecode($string);
14  
15 foreach my $key (keys(%{$hashref})) {
16 print "Key: $key, Value: ${$hashref}{$key}\n";
17 }
18  
19 my $encoded_string = bencode($hashref);
20 print $encoded_string."\n";
21  
22 =head1 DESCRIPTION
23  
24 This module provides two functions, C<bencode> and C<bdecode>, which
25 encode and decode bencoded strings respectivly.
26  
27 =head2 Encoding
28  
29 C<bencode()> expects to be passed a single value, which is either a scalar,
30 a arrary ref, or a hash ref, and it returns a scalar containing the bencoded
31 representation of the data structure it was passed. If the value passed was
32 a scalar, it returns either a bencoded string, or a bencoded integer (floating
33 points are not implemented, and would be returned as a string rather than a
34 integer). If the value was a array ref, it returns a bencoded list, with all
35 the values of that array also bencoded recursivly. If the value was a hash ref,
36 it returns a bencoded dictionary (which for all intents and purposes can be
37 thought of as a synonym for hash) containing the recursivly bencoded key and
38 value pairs of the hash.
39  
40 =head2 Decoding
41  
42 C<bdecode()> expects to be passed a single scalar containing the bencoded string
43 to be decoded. Its return value will be either a hash ref, a array ref, or a
44 scalar, depending on whether the outer most element of the bencoded string
45 was a dictionary, list, or a string/integer respectivly.
46  
47 =head1 SEE ALSO
48  
49 The description of bencode is part of the bittorrent protocol specification
50 which can be found at http://bitconjurer.org/BitTorrent/protocol.html
51  
52 =head1 BUGS
53  
54 No error detection of bencoded data. Damaged input will most likely cause very bad things to happen, up to and including causeing the bdecode function to recurse infintly.
55  
56 =head1 AUTHOR & COPYRIGHT
57  
58 Created by R. Kyle Murphy <orclev@rejectedmaterial.com>, aka Orclev.
59  
60 Copyright 2003 R. Kyle Murphy. All rights reserved. Convert::Bencode
61 is free software; you may redistribute it and/or modify it under the
62 same terms as Perl itself.
63  
64 =cut
65  
66 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
72 1 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, @EXPORT_FAIL, %EXPORT_TAGS);
73  
74 1 $VERSION = 1.03;
75 1 @ISA = qw(Exporter);
76 1 @EXPORT_OK = qw(&bencode &bdecode);
77 1 @EXPORT_FAIL = qw(&_dechunk);
78 1 %EXPORT_TAGS = (all => [qw(&bencode &bdecode)]);
79 }
80 1 our @EXPORT_OK;
81  
82 1 ▶END { }
Subroutine END: called CC=1 SCAR=0.0
83  
84 10 ▶sub bencode {
Subroutine bencode: called CC=6 SCAR=17.9
Pod undocumented
85 1 ▶ no locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 10 my $item = shift;
87 10 my $line = '';
88 10 ▶ if(ref($item) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq "HASH") 1 9 10
89 1 $line = 'd';
90 1 foreach my $key (sort(keys %{$item})) {
91 3 $line .= bencode($key);
92 3 $line .= bencode(${$item}{$key});
93 }
94 1 $line .= 'e';
95 1 return $line;
96 }
97 9 ▶ if(ref($item) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq "ARRAY") 1 8 9
98 1 $line = 'l';
99 1 foreach my $l (@{$item}) {
100 3 $line .= bencode($l);
101 }
102 1 $line .= 'e';
103 1 return $line;
104 }
105 8 ▶ if($item =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /^\d+$/) 3 5 8
106 3 $line = 'i';
107 3 $line .= $item;
108 3 $line .= 'e';
109 3 return $line;
110 }
111 5 $line = length($item).":";
112 5 $line .= $item;
113 5 return $line;
114 }
115  
116 1 ▶sub bdecode {
Subroutine bdecode: called CC=1 SCAR=0.0
Pod undocumented
117 1 my $string = shift;
118 1 my @chunks = split(//, $string);
119 1 my $root = _dechunk(\@chunks);
120 1 return $root;
121 }
122  
123 10 ▶sub _dechunk {
Subroutine _dechunk: called CC=10 SCAR=23.0
124 10 my $chunks = shift;
125  
126 10 my $item = shift(@{$chunks});
127 10 ▶ if($item eq 'd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item eq "d") 1 9 10
128 1 $item = shift(@{$chunks});
129 1 my %hash;
130 1 ▶ while($item ne 'e') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item ne "e") 3 1 4
131 3 unshift(@{$chunks}, $item);
132 3 my $key = _dechunk($chunks);
133 3 $hash{$key} = _dechunk($chunks);
134 3 $item = shift(@{$chunks});
135 }
136 1 return \%hash;
137 }
138 9 ▶ if($item eq 'l') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item eq "l") 1 8 9
139 1 $item = shift(@{$chunks});
140 1 my @list;
141 1 ▶ while($item ne 'e') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item ne "e") 3 1 4
142 3 unshift(@{$chunks}, $item);
143 3 push(@list, _dechunk($chunks));
144 3 $item = shift(@{$chunks});
145 }
146 1 return \@list;
147 }
148 8 ▶ if($item eq 'i') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item eq "i") 3 5 8
149 3 my $num;
150 3 $item = shift(@{$chunks});
151 3 ▶ while($item ne 'e') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item ne "e") 4 3 7
152 4 $num .= $item;
153 4 $item = shift(@{$chunks});
154 }
155 3 return $num;
156 }
157 5 ▶ if($item =~ /\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /\d/) 5 0 5
158 5 my $num;
159 5 ▶ while($item =~ /\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /\d/) 5 5 10
160 5 $num .= $item;
161 5 $item = shift(@{$chunks});
162 }
163 5 my $line = '';
164 5 for(1 .. $num) {
165 32 $line .= shift(@{$chunks});
166 }
167 5 return $line;
168 }
169 0 return $chunks;
170 }
171  
172 1 1;
↑ Summary