blib/lib/Convert/zBase32.pm

filter: Statementstmt 88.6% 70 / 79 Branchbran 75.0% 12 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 7 / 9 Podpod 100% 4 / 4 totaltotal 86.1% 93 / 108 CC 11 SCAR 14.8
CC
11
Cov
86%
encode_base32
12.0
_join_string
4.0
_split_string
4.0
CRAP 4.4
↑ Summary
1 package Convert::zBase32;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 require Exporter;
8  
9 2 our @ISA = qw(Exporter);
10  
11 2 our %EXPORT_TAGS = ( 'all' => [ qw(
12 encode_zbase32 decode_zbase32 encode_base32 decode_base32
13 ) ] );
14  
15 2 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
16  
17 2 our @EXPORT = qw( encode_zbase32 decode_zbase32 );
18  
19 2 our $VERSION = '0.0201';
20 2 $VERSION = eval $VERSION;
21  
22 2 our @zBASE32 = qw( y b n d r f g 8 e j k m c p q x
23 o t 1 u w i s z a 3 4 5 h 7 6 9 );
24 2 my $q=0;
25 2 our %zB2N = map { $_ => $q++ } @zBASE32;
26  
27 2 our @BASE32 = qw( a b c d e f g h i j k l m n o p
28 q r s t u v w x y z 2 3 4 5 6 7 );
29 2 $q=0;
30 2 our %B2N = map { $_ => $q++ } @BASE32;
31  
32 # masks to use if the begining of 5-bit is w/in this octet
33 # keyed on the position w/in the octet
34 2 my @masks = ( 0x1f, 0x3e, 0x7c, 0xf8, # all 5 bits in the octet
35 0xf0, 0xe0, 0xc0, 0x80 # into the next one
36 );
37  
38 # masks of up to 4 bits in the next octet
39 # keyed on the sub offset
40 2 my @more_masks = ( 0x1, 0x3, 0x7, 0xf );
41  
42 ##################################################################
43 sub encode_zbase32
44 4 ▶{
Subroutine encode_zbase32: called CC=3 SCAR=11.0
Pod documented
45 4 my( $string ) = @_;
46  
47 4 my $ret;
48 4 foreach my $part ( _split_string( $string ) ) {
49 61 ▶ die "There is no $part" unless $part < 32;
Branch: 1 branch
BranchTrueFalseTotal
unless $part < 32 0 61 61
50 61 $ret .= $zBASE32[ $part ];
51 }
52 4 return $ret;
53 }
54  
55 ##################################################################
56 sub decode_zbase32
57 8 ▶{
Subroutine decode_zbase32: called CC=1 SCAR=0.0
Pod documented
58 8 my( $string ) = @_;
59 8 return _join_string( map { $zB2N{$_} } split '', lc $string );
60 }
61  
62  
63 ##################################################################
64 sub encode_base32
65 0 ▶{
Subroutine encode_base32: not called CC=3 SCAR=24.8
Pod documented
66 0 my( $string ) = @_;
67  
68 0 my $ret;
69 0 foreach my $part ( _split_string( $string ) ) {
70 0 ▶ die "There is no $part" unless $part < 32;
Branch: 1 branch
BranchTrueFalseTotal
unless $part < 32 0 0 0
71 0 $ret .= $BASE32[ $part ];
72 }
73 0 return $ret;
74 }
75  
76 ##################################################################
77 sub decode_base32
78 0 ▶{
Subroutine decode_base32: not called CC=1 SCAR=6.9
Pod documented
79 0 my( $string ) = @_;
80 0 return _join_string( map { $B2N{$_} } split '', lc $string );
81 }
82  
83  
84 ##################################################################
85 sub _split_string
86 14 ▶{
Subroutine _split_string: called CC=4 SCAR=13.9
87 14 my( $string ) = @_;
88 14 my $len = 8 * length $string;
89 14 my( @output, $chunk, $part, $offset, $suboffset );
90 # we want to build an array of 5 bit numbers
91 14 ▶ foreach( my $q=0; $q < $len ; $q+=5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q < $len) 131 14 145
92 131 $offset = int $q / 8;
93 131 $suboffset = $q % 8;
94 # warn "$offset, $suboffset";
95 # first part
96 131 $part = ord substr $string, $offset, 1;
97 # lower bits
98 131 $chunk = ( $part & $masks[ $suboffset ] ) >> $suboffset;
99 # is this all we need?
100 131 $suboffset -= 4;
101 131 ▶ if( $suboffset >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suboffset >= 0) 71 60 131
102 # next part
103 71 ▶ if( $q + 5 > $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q + 5 > $len) { } 14 57 71
104 14 $part = 0; # past the end
105 }
106 else {
107 57 $part = ord substr $string, $offset+1, 1;
108 }
109 71 $chunk |= ( $part & $more_masks[ $suboffset ] )
110 << (4- $suboffset);
111 }
112 131 push @output, $chunk;
113 }
114 14 return @output;
115 }
116  
117 ##################################################################
118 sub _join_string
119 15 ▶{
Subroutine _join_string: called CC=4 SCAR=13.9
120 15 my( @output ) = @_;
121 15 my $len = 5 * @output;
122 15 my @ret = (0) x int( $len / 8);
123  
124 15 my $n = 0;
125 15 my( $offset, $suboffset, $part, $chunk );
126 15 ▶ foreach( my $q=0; $q < $len ; $q+=5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q < $len) 171 15 186
127 171 $offset = int $q / 8;
128 171 $suboffset = $q % 8;
129  
130 # warn "$offset, $suboffset";
131 # first part
132 171 $part = $output[ $n ];
133 # lower bits
134 171 $chunk = ($part << $suboffset ) & $masks[ $suboffset ];
135 171 $ret[ $offset ] |= $chunk;
136  
137 # is this all we needed?
138 171 $suboffset -= 4;
139 171 ▶ if( $suboffset >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suboffset >= 0) 90 81 171
140 90 $ret[ $offset +1 ] |=
141 ( $part >> (4-$suboffset) ) & $more_masks[ $suboffset ];
142 }
143 171 $n++;
144 }
145 15 my $ret = join '', map chr, @ret;
146 # remove any padding...
147 15 ▶ substr( $ret, -1, 1, '' ) if 0 == ord substr( $ret, -1 );
Branch: 1 branch
BranchTrueFalseTotal
if 0 == ord substr($ret, -1) 15 0 15
148 15 return $ret;
149 }
150  
151  
152 ##################################################################
153  
154  
155 2 1;
156 __END__
↑ Summary