blib/lib/Encode/Base32/Crockford.pm

filter: Statementstmt 100% 68 / 68 Branchbran 100% 26 / 26 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 98.2% 114 / 116 CC 17 SCAR 14.0
CC
17
Cov
99%
base32_decode
7.0
base32_encode
5.0
_normalize_actions
4.0
CRAP 4.0
↑ Summary
1 package Encode::Base32::Crockford;
2 {
3 4 $Encode::Base32::Crockford::VERSION = '2.112991';
4 }
5  
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 our @EXPORT_OK = qw(
11 base32_encode base32_encode_with_checksum
12 base32_decode base32_decode_with_checksum
13 normalize
14 );
15 4 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
16  
17 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 my %SYMBOLS;
21  
22 # Note: regular digits do not include I, L, O or U. See spec in documentation.
23 4 @SYMBOLS{0..9,'A'..'H','J','K','M','N','P'..'T','V'..'Z'} = 0..31;
24  
25 # checksum symbols only from here
26 4 @SYMBOLS{'*','~','$','=','U'} = 32..36;
27  
28 4 my %SYMBOLS_INVERSE = reverse %SYMBOLS;
29  
30 2055 ▶sub base32_encode {
Subroutine base32_encode: called CC=5 SCAR=16.1
Pod documented
31 2055 my $number = shift;
32  
33 2055 ▶ die qq("$number" isn't a number) unless looks_like_number($number);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($number) 1 2054 2055
34  
35 2054 ▶ return '0' unless $number;
Branch: 1 branch
BranchTrueFalseTotal
unless $number 2 2052 2054
36  
37 2052 my @digits;
38  
39 # Cut a long story short: keep dividing by 32. Use the remainders to make the
40 # digits of the converted number, right to left; the quotient goes to the next
41 # iteration to be divided again. When the quotient hits zero, i.e. there's not
42 # enough for 32 to be a divisor, the value being divided is the final digit.
43 2052 ▶ while ($number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number) 4043 2052 6095
44 4043 my $remainder = $number % 32;
45 4043 $number = int($number / 32);
46 4043 push @digits, $SYMBOLS_INVERSE{$remainder};
47 }
48  
49 2052 ▶ return join('', reverse @digits) || '0';
Condition50%1/2
join('', reverse(@digits)) || '0'
l!l
20520
MC/DC0%0/1
join('', reverse(@digits)) || '0'
join('', reverse(@digits))
Truth table50%1/2
A
A= join('', reverse(@digits))
Aresult
11
00
50 }
51  
52 1027 ▶sub base32_encode_with_checksum {
Subroutine base32_encode_with_checksum: called CC=1 SCAR=0.0
Pod documented
53 1027 my $number = shift;
54  
55 1027 my $modulo = $number % 37;
56
57 1027 return base32_encode($number) . $SYMBOLS_INVERSE{$modulo};
58 }
59  
60 3088 ▶sub normalize {
Subroutine normalize: called CC=3 SCAR=11.0
Pod documented
61 3088 my ($string, $options) = @_;
62  
63 3088 my $orig_string = $string;
64  
65 3088 $string = uc($string);
66 3088 ▶ _normalize_actions($orig_string, $string, $options->{"mode"}) if $string ne $orig_string;
Branch: 1 branch
BranchTrueFalseTotal
if $string ne $orig_string 1 3087 3088
67  
68 # fix possible transcription errors and remove chunking symbols
69 3088 ▶ _normalize_actions($orig_string, $string, $options->{"mode"}) if $string =~ tr/IiLlOo-/111100/d;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ tr/ILOilo-/110110/d 3 3085 3088
70  
71 3087 $string;
72 }
73  
74 # Actions to carry out if normalize() is operating in a particular mode.
75 4 ▶sub _normalize_actions {
Subroutine _normalize_actions: called CC=4 SCAR=13.9
76 4 my ($old_string, $new_string, $mode) = @_;
77  
78 4 ▶ $mode ||= '';
Condition100%2/2
$mode ||= ""
l!l
22
MC/DC100%1/1
$mode ||= ""
$mode
Truth table100%2/2
A
A= $mode
Aresult
11
00
79  
80 4 ▶ warn qq(String "$old_string" corrected to "$new_string") if $mode eq "warn";
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "warn" 1 3 4
81 4 ▶ die qq(String "$old_string" requires normalization) if $mode eq "strict";
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "strict" 1 3 4
82 }
83  
84 3090 ▶sub base32_decode {
Subroutine base32_decode: called CC=7 SCAR=19.5
Pod documented
85 3090 my ($string, $options) = @_;
86  
87 3090 ▶ croak "string is undefined" if not defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 1 3089 3090
88 3089 ▶ croak "string is empty" if $string eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "" 1 3088 3089
89  
90 3088 $string = normalize($string, $options);
91  
92 3087 my $valid;
93  
94 3087 ▶ if ($options->{"is_checksum"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'is_checksum'}) { } 1030 2057 3087
95 1030 ▶ die qq(Checksum "$string" is too long; should be one character)
Branch: 1 branch
BranchTrueFalseTotal
if length $string > 1 1 1029 1030
96 if length($string) > 1;
97  
98 1029 $valid = qr/^[A-Z0-9\*\~\$=U]$/;
99  
100 } else {
101 # 'U' is only valid as a checksum symbol.
102 2057 $valid = qr/^[A-TV-Z0-9]+$/;
103 }
104  
105 3086 ▶ croak qq(String "$string" contains invalid characters) if $string !~ /$valid/;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /$valid/ 2 3084 3086
106
107
108 3084 my $total = 0;
109  
110 # For each base32 digit B of position P counted (using zero-based counting)
111 # from right in a number, its decimal value D is calculated with the
112 # following expression:
113 # D = B * 32^P.
114 # As any number raised to the power of 0 is 1, we can define an "offset" value
115 # of 1 for the first digit calculated and simply multiply the offset by 32
116 # after deriving the value for each digit.
117  
118 3084 foreach my $symbol (split(//, $string)) {
119 5084 $total = $total * 32 + $SYMBOLS{$symbol};
120 }
121
122 3084 $total;
123 }
124  
125 1027 ▶sub base32_decode_with_checksum {
Subroutine base32_decode_with_checksum: called CC=2 SCAR=6.9
Pod documented
126 1027 my ($string, $options) = @_;
127 1027 my $check_string = $string;
128  
129 1027 my $checksum = substr($check_string, (length($check_string) - 1), 1, "");
130  
131 1027 my $value = base32_decode($check_string, $options);
132 1027 my $checksum_value = base32_decode($checksum, { "is_checksum" => 1 });
133 1027 my $modulo = $value % 37;
134  
135 1027 ▶ croak qq(Checksum symbol "$checksum" is not correct for value "$check_string".)
Branch: 1 branch
BranchTrueFalseTotal
if $checksum_value != $modulo 1 1026 1027
136 if $checksum_value != $modulo;
137
138 1026 $value;
139 }
140  
141 4 1;
142  
143 __END__
↑ Summary