| 1 | package Convert::Base32; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | #use warnings; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp qw( ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Exporter qw( ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use vars qw( $VERSION @ISA @EXPORT ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | push @ISA, 'Exporter'; | |||||||||||||||||||||||||||
| 14 | 2 | @EXPORT = qw( encode_base32 decode_base32 ); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | my @syms = ( 'a'..'z', '2'..'7' ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | my %bits2char; | |||||||||||||||||||||||||||
| 20 | my @char2bits; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | for (0..$#syms) { | |||||||||||||||||||||||||||
| 23 | 64 | my $sym = $syms[$_]; | |||||||||||||||||||||||||||
| 24 | 64 | my $bin = sprintf('%05b', $_); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 64 | $char2bits[ ord lc $sym ] = $bin; | |||||||||||||||||||||||||||
| 27 | 64 | $char2bits[ ord uc $sym ] = $bin; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 64 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 124 | $bits2char{$bin} = $sym; | |||||||||||||||||||||||||||
| 31 | } while $bin =~ s/(.+)0\z/$1/s; | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 516 | ▶ | sub encode_base32_pre58($) { | ||||||||||||||||||||||||||
|
Subroutine
encode_base32_pre58: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 516 | ▶ | length($_[0]) == bytes::length($_[0]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | or Carp::croak('Data contains non-bytes'); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 260 | my $str = unpack('B*', $_[0]); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 260 | ▶ | if (length($str) < 8*1024) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 260 | return join '', @bits2char{ $str =~ /.{1,5}/g }; | |||||||||||||||||||||||||||
| 43 | } else { | ||||||||||||||||||||||||||||
| 44 | # Slower, but uses less memory | ||||||||||||||||||||||||||||
| 45 | 0 | $str =~ s/(.{5})/$bits2char{$1}/sg; | |||||||||||||||||||||||||||
| 46 | 0 | return $str; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub encode_base32_perl58($) { | ||||||||||||||||||||||||||
|
Subroutine
encode_base32_perl58: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $_[0] =~ tr/\x00-\xFF//c | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | and Carp::croak('Data contains non-bytes'); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | my $str = unpack('B*', $_[0]); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if (length($str) < 8*1024) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | return join '', @bits2char{ unpack '(a5)*', $str }; | |||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||
| 60 | # Slower, but uses less memory | ||||||||||||||||||||||||||||
| 61 | 0 | $str =~ s/(.{5})/$bits2char{$1}/sg; | |||||||||||||||||||||||||||
| 62 | 0 | return $str; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 556 | ▶ | sub decode_base32_pre58($) { | ||||||||||||||||||||||||||
|
Subroutine
decode_base32_pre58: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 556 | ▶ | ( length($_[0]) != bytes::length($_[0]) || $_[0] =~ tr/a-zA-Z2-7//c ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $_[0] != &bytes::length($_[0]) or $_[0] =~ tr/2-7A-Za-z//c
MC/DC100%2/2
length $_[0] != &bytes::length($_[0]) or $_[0] =~ tr/2-7A-Za-z//c
length $_[0] != &bytes::length($_[0])
$_[0] =~ tr/2-7A-Za-z//c
Truth table100%3/3
A or B
A= length $_[0] != &bytes::length($_[0])B= $_[0] =~ tr/2-7A-Za-z//c
| |||||||||||||||||||||||||||||
| 69 | and Carp::croak('Data contains non-base32 characters'); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 102 | my $str; | |||||||||||||||||||||||||||
| 72 | 102 | ▶ | if (length($_[0]) < 8*1024) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 102 | $str = join '', @char2bits[ unpack 'C*', $_[0] ]; | |||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||
| 75 | # Slower, but uses less memory | ||||||||||||||||||||||||||||
| 76 | 0 | ($str = $_[0]) =~ s/(.)/$char2bits[ord($1)]/sg; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 102 | my $padding = length($str) % 8; | |||||||||||||||||||||||||||
| 80 | 102 | ▶ | $padding < 5 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | or Carp::croak('Length of data invalid'); | ||||||||||||||||||||||||||||
| 82 | 96 | ▶ | $str =~ s/0{$padding}\z// | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | or Carp::croak('Padding bits at the end of output buffer are not all zero'); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 76 | return pack('B*', $str); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub decode_base32_perl58($) { | ||||||||||||||||||||||||||
|
Subroutine
decode_base32_perl58: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $_[0] =~ tr/a-zA-Z2-7//c | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | and Carp::croak('Data contains non-base32 characters'); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | my $str; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | if (length($_[0]) < 8*1024) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | $str = join '', @char2bits[ unpack 'C*', $_[0] ]; | |||||||||||||||||||||||||||
| 96 | } else { | ||||||||||||||||||||||||||||
| 97 | # Slower, but uses less memory | ||||||||||||||||||||||||||||
| 98 | 0 | ($str = $_[0]) =~ s/(.)/$char2bits[ord($1)]/sg; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | my $padding = length($str) % 8; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | $padding < 5 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | or Carp::croak('Length of data invalid'); | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $str =~ s/0{$padding}\z// | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | or Carp::croak('Padding bits at the end of output buffer are not all zero'); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | return pack('B*', $str); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 2 | ▶ | if ($] lt '5.800000') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 2 | require bytes; | |||||||||||||||||||||||||||
| 113 | 2 | *encode_base32 = \&encode_base32_pre58; | |||||||||||||||||||||||||||
| 114 | 2 | *decode_base32 = \&decode_base32_pre58; | |||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||
| 116 | 0 | *encode_base32 = \&encode_base32_perl58; | |||||||||||||||||||||||||||
| 117 | 0 | *decode_base32 = \&decode_base32_perl58; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | 1; | |||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||