blib/lib/gb64.pm

filter: Statementstmt 100% 79 / 79 Branchbran 94.4% 17 / 18 Conditioncond 68.0% 17 / 25 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 9 / 9 Podpod 100% 6 / 6 totaltotal 88.6% 133 / 150 CC 22 SCAR 23.5
CC
22
Cov
93%
dec_b64
16.8
enc_b64
6.0
add
2.0
CRAP 10.5
↑ Summary
1 package gb64;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.0.0';
8 1 our @EXPORT_OK = qw(enc_b64 dec_b64);
9  
10 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
11 7 my ($class) = @_;
12 7 my $self = bless { buffer => '' }, $class;
13 7 return $self;
14 }
15  
16 8 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod documented
17 8 my ($self, $data) = @_;
18 8 ▶ die "Input must be defined" unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 7 8
19 7 $self->{buffer} .= $data;
20 7 return $self;
21 }
22  
23 4 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
24 4 my ($self) = @_;
25 4 my $data = $self->{buffer};
26 4 $self->{buffer} = '';
27 4 return enc_b64($data);
28 }
29  
30 4 ▶sub decode {
Subroutine decode: called CC=1 SCAR=0.0
Pod documented
31 4 my ($self) = @_;
32 4 my $data = $self->{buffer};
33 4 $self->{buffer} = '';
34 4 return dec_b64($data);
35 }
36  
37 # Lookup-tabel voor encoderen (ASCII-waarden voor A-Z, a-z, 0-9, +, /)
38 1 my @b64_table = (65..90, 97..122, 48..57, 43, 47);
39  
40 # Lookup-tabel voor decoderen (array in plaats van hash)
41 1 my @b64_decode_array = (undef) x 128;
42 1 @b64_decode_array[65..90] = 0..25; # A-Z
43 1 @b64_decode_array[97..122] = 26..51; # a-z
44 1 @b64_decode_array[48..57] = 52..61; # 0-9
45 1 $b64_decode_array[43] = 62; # +
46 1 $b64_decode_array[47] = 63; # /
47  
48 11 ▶sub enc_b64 {
Subroutine enc_b64: called CC=6 SCAR=17.9
Pod documented
49 11 my ($data) = @_;
50 11 ▶ my $len = length($data) || 0;
Condition100%2/2
length $data || 0
l!l
83
MC/DC100%1/1
length $data || 0
length $data
Truth table100%2/2
A
A= length $data
Aresult
11
00
51 11 my $pos = 0;
52 11 my @bytes;
53  
54 # Verwerk groepen van 3 bytes
55 11 ▶ while ($pos + 2 < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos + 2 < $len) 8 11 19
56 8 my ($b1, $b2, $b3) = unpack("C3", substr($data, $pos, 3));
57 8 push @bytes,
58 ($b1 >> 2) & 0x3F,
59 (($b1 & 0x3) << 4) | (($b2 >> 4) & 0xF),
60 (($b2 & 0xF) << 2) | (($b3 >> 6) & 0x3),
61 $b3 & 0x3F;
62 8 $pos += 3;
63 }
64  
65 # Behandel restanten
66 11 ▶ if ($pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len) 5 6 11
67 5 my $rest = substr($data, $pos);
68 5 ▶ if (length($rest) == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $rest == 1) { } 2 3 5
elsif (length $rest == 2) { } 3 0 3
69 2 my $b1 = ord($rest);
70 2 push @bytes,
71 ($b1 >> 2) & 0x3F,
72 ($b1 & 0x3) << 4;
73 2 return pack("C*", map { $b64_table[$_] } @bytes) . "==";
74 }
75 elsif (length($rest) == 2) {
76 3 my ($b1, $b2) = unpack("C2", $rest);
77 3 push @bytes,
78 ($b1 >> 2) & 0x3F,
79 (($b1 & 0x3) << 4) | (($b2 >> 4) & 0xF),
80 ($b2 & 0xF) << 2;
81 3 return pack("C*", map { $b64_table[$_] } @bytes) . "=";
82 }
83 }
84  
85 6 return pack("C*", map { $b64_table[$_] } @bytes);
86 }
87  
88 13 ▶sub dec_b64 {
Subroutine dec_b64: called CC=16 SCAR=28.2
Pod documented
89 13 my ($data) = @_;
90 13 ▶ my $len = length($data) || 0;
Condition100%2/2
length $data || 0
l!l
103
MC/DC100%1/1
length $data || 0
length $data
Truth table100%2/2
A
A= length $data
Aresult
11
00
91 13 ▶ die "Invalid Base64 length" unless $len == 0 || $len % 4 == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $len == 0 or $len % 4 == 0 1 12 13
Condition100%3/3
$len == 0 or $len % 4 == 0
l!l&&r!l&&!r
391
MC/DC100%2/2
$len == 0 or $len % 4 == 0
$len == 0 $len % 4 == 0
Truth table100%3/3
A or B
A= $len == 0B= $len % 4 == 0
ABresult
1X1
011
000
92 12 my $pos = 0;
93 12 my @bytes;
94  
95 # Verwerk groepen van 4 karakters
96 12 ▶ while ($pos + 3 < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos + 3 < $len) 14 6 20
97 14 my ($c1, $c2, $c3, $c4) = unpack("C4", substr($data, $pos, 4));
98 14 ▶ if ($c4 == ord("=")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c4 == 61) 6 8 14
99 6 ▶ if ($c3 == ord("=")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c3 == 61) { } 3 3 6
100 3 ▶ my $v1 = $b64_decode_array[$c1] // die "Invalid Base64 character at position $pos";
Condition50%1/2
$b64_decode_array[$c1] // die("Invalid Base64 character at position $pos")
l!l
30
MC/DC0%0/1
$b64_decode_array[$c1] // die("Invalid Base64 character at position $pos")
$b64_decode_array[$c1]
Truth table50%1/2
A
A= $b64_decode_array[$c1]
Aresult
11
00
101 3 ▶ my $v2 = $b64_decode_array[$c2] // die "Invalid Base64 character at position " . ($pos + 1);
Condition100%2/2
$b64_decode_array[$c2] // die('Invalid Base64 character at position ' . ($pos + 1))
l!l
21
MC/DC100%1/1
$b64_decode_array[$c2] // die('Invalid Base64 character at position ' . ($pos + 1))
$b64_decode_array[$c2]
Truth table100%2/2
A
A= $b64_decode_array[$c2]
Aresult
11
00
102 2 push @bytes, ($v1 << 2) | (($v2 >> 4) & 0x3);
103 }
104 else {
105 3 ▶ my $v1 = $b64_decode_array[$c1] // die "Invalid Base64 character at position $pos";
Condition50%1/2
$b64_decode_array[$c1] // die("Invalid Base64 character at position $pos")
l!l
30
MC/DC0%0/1
$b64_decode_array[$c1] // die("Invalid Base64 character at position $pos")
$b64_decode_array[$c1]
Truth table50%1/2
A
A= $b64_decode_array[$c1]
Aresult
11
00
106 3 ▶ my $v2 = $b64_decode_array[$c2] // die "Invalid Base64 character at position " . ($pos + 1);
Condition50%1/2
$b64_decode_array[$c2] // die('Invalid Base64 character at position ' . ($pos + 1))
l!l
30
MC/DC0%0/1
$b64_decode_array[$c2] // die('Invalid Base64 character at position ' . ($pos + 1))
$b64_decode_array[$c2]
Truth table50%1/2
A
A= $b64_decode_array[$c2]
Aresult
11
00
107 3 ▶ my $v3 = $b64_decode_array[$c3] // die "Invalid Base64 character at position " . ($pos + 2);
Condition50%1/2
$b64_decode_array[$c3] // die('Invalid Base64 character at position ' . ($pos + 2))
l!l
30
MC/DC0%0/1
$b64_decode_array[$c3] // die('Invalid Base64 character at position ' . ($pos + 2))
$b64_decode_array[$c3]
Truth table50%1/2
A
A= $b64_decode_array[$c3]
Aresult
11
00
108 3 push @bytes,
109 ($v1 << 2) | (($v2 >> 4) & 0x3),
110 (($v2 & 0xF) << 4) | (($v3 >> 2) & 0xF);
111 }
112 5 last;
113 }
114 8 ▶ my $v1 = $b64_decode_array[$c1] // die "Invalid Base64 character at position $pos";
Condition50%1/2
$b64_decode_array[$c1] // die("Invalid Base64 character at position $pos")
l!l
80
MC/DC0%0/1
$b64_decode_array[$c1] // die("Invalid Base64 character at position $pos")
$b64_decode_array[$c1]
Truth table50%1/2
A
A= $b64_decode_array[$c1]
Aresult
11
00
115 8 ▶ my $v2 = $b64_decode_array[$c2] // die "Invalid Base64 character at position " . ($pos + 1);
Condition50%1/2
$b64_decode_array[$c2] // die('Invalid Base64 character at position ' . ($pos + 1))
l!l
80
MC/DC0%0/1
$b64_decode_array[$c2] // die('Invalid Base64 character at position ' . ($pos + 1))
$b64_decode_array[$c2]
Truth table50%1/2
A
A= $b64_decode_array[$c2]
Aresult
11
00
116 8 ▶ my $v3 = $b64_decode_array[$c3] // die "Invalid Base64 character at position " . ($pos + 2);
Condition50%1/2
$b64_decode_array[$c3] // die('Invalid Base64 character at position ' . ($pos + 2))
l!l
80
MC/DC0%0/1
$b64_decode_array[$c3] // die('Invalid Base64 character at position ' . ($pos + 2))
$b64_decode_array[$c3]
Truth table50%1/2
A
A= $b64_decode_array[$c3]
Aresult
11
00
117 8 ▶ my $v4 = $b64_decode_array[$c4] // die "Invalid Base64 character at position " . ($pos + 3);
Condition50%1/2
$b64_decode_array[$c4] // die('Invalid Base64 character at position ' . ($pos + 3))
l!l
80
MC/DC0%0/1
$b64_decode_array[$c4] // die('Invalid Base64 character at position ' . ($pos + 3))
$b64_decode_array[$c4]
Truth table50%1/2
A
A= $b64_decode_array[$c4]
Aresult
11
00
118 8 push @bytes,
119 ($v1 << 2) | (($v2 >> 4) & 0x3),
120 (($v2 & 0xF) << 4) | (($v3 >> 2) & 0xF),
121 (($v3 & 0x3) << 6) | $v4;
122 8 $pos += 4;
123 }
124  
125 11 return pack("C*", @bytes);
126 }
127  
128 1 1; # EOF gb64.pm (C) 2025 OnEhIppY, Domero
↑ Summary