blib/lib/QRCode/Base45.pm

filter: Statementstmt 100% 56 / 56 Branchbran 100% 12 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 100% 79 / 79 CC 12 SCAR 15.3
CC
12
Cov
100%
decode_base45
7.0
encode_base45
6.0
BEGIN
1.0
CRAP 4.6
↑ Summary
1 package QRCode::Base45;
2  
3 2 ▶use 5.10.0;
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 2 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our @EXPORT = qw(encode_base45 decode_base45);
11  
12 =head1 NAME
13  
14 QRCode::Base45 - Base45 encoding used in QR codes
15  
16 =head1 VERSION
17  
18 Version 0.01
19  
20 =cut
21  
22 2 our $VERSION = '0.01';
23  
24 2 our $ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:';
25 # 0 1 2 3 4
26 # 012345678901234567890123456789012345678901234
27  
28 =head1 SYNOPSIS
29  
30 use QRCode::Base45;
31  
32 my $text_for_qrcode = encode_base45($binary_data);
33 my $binary_data = decode_base45($text_from_qrcode);
34  
35 =head1 DESCRIPTION
36  
37 This module handles encoding/decoding Base45 data,
38 as described in
39 L<draft-faltstrom-base45-06|http://www.watersprings.org/pub/id/draft-faltstrom-base45-06.html>.
40 Base45 is used especially in QR codes.
41  
42 =head2 encode_base45
43  
44 Takes arbitrary string as argument, and returs the Base45 representation
45 of it. Character strings (as opposed to byte strings) are encoded to bytes
46 as UTF-8 first.
47  
48 For zero-length strings (undef or '') the empty string is returned.
49  
50 =cut
51  
52 sub encode_base45 {
53 56 ▶ my ($input) = @_;
Subroutine encode_base45: called CC=6 SCAR=17.9
Pod documented
54  
55 56 ▶ return '' if !length $input;
Branch: 1 branch
BranchTrueFalseTotal
unless length $input 2 54 56
56  
57 54 ▶ $input = Encode::encode('UTF-8', $input)
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($input) 4 50 54
58 if utf8::is_utf8($input);
59  
60 54 my $output = '';
61  
62 54 for my $chunk ($input =~ /..?/msg) {
63 86 my $sum = 0;
64 86 for my $byte (unpack('C*', $chunk)) {
65 121 $sum *= 256;
66 121 $sum += $byte;
67 }
68 86 for (0 .. length($chunk)) {
69 207 $output .= substr($ALPHABET, $sum % 45, 1);
70 207 $sum = int($sum/45);
71 }
72 }
73  
74 54 return $output;
75 }
76  
77 =head2 decode_base45
78  
79 The function takes a textual base45 representation of data,
80 and tries to decode it. Returned value is a byte string (as this function
81 cannot know whether the contents should be interpreted as bytes or UTF-8
82 data (or whatever else). The caller has to decode the characters afterwards,
83 if needed.
84  
85 For zero-length input, the empty string is returned.
86  
87 For invalid inputs, such as strings of length 3n+1 or characters
88 outside of the Base45 alphabet, this function croak()s.
89  
90 =cut
91  
92 sub decode_base45 {
93 68 ▶ my ($input) = @_;
Subroutine decode_base45: called CC=7 SCAR=19.5
Pod documented
94  
95 68 ▶ return '' if !length $input;
Branch: 1 branch
BranchTrueFalseTotal
unless length $input 2 66 68
96  
97 66 ▶ croak "decode_base45(): invalid input length " . length($input)
Branch: 1 branch
BranchTrueFalseTotal
if length($input) % 3 == 1 5 61 66
98 . " is 3*n+1"
99 if length($input) % 3 == 1;
100  
101 61 my $map_count = 0;
102 61 state %value_of = map { $_ => $map_count++ } split //, $ALPHABET;
103  
104 61 my $output = '';
105 61 for my $chunk ($input =~ /(...?)/g) {
106 96 my $sum = 0;
107 96 for my $c (reverse map { $value_of{$_} } split //, $chunk) {
108 224 ▶ croak "decode_base45(): chunk <$chunk> contains invalid character(s)"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 7 217 224
109 if !defined $c;
110 217 $sum *= 45;
111 217 $sum += $c;
112 }
113  
114 89 ▶ if (length $chunk == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $chunk == 3) 38 51 89
115 38 $output .= pack('C', $sum >> 8);
116 38 $sum &= 0x00FF;
117 }
118 89 $output .= pack('C', $sum);
119 }
120  
121 54 return $output;
122 }
123  
124 =head1 AUTHOR
125  
126 Jan "Yenya" Kasprzak, C<< <kas at fi.muni.cz> >>
127  
128 =head1 BUGS
129  
130 Please report any bugs or feature requests to C<bug-qrcode-base45 at rt.cpan.org>, or through
131 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=QRCode-Base45>. I will be notified, and then you'll
132 automatically be notified of progress on your bug as I make changes.
133  
134  
135 =head1 SUPPORT
136  
137 You can find documentation for this module with the perldoc command.
138  
139 perldoc QRCode::Base45
140  
141  
142 You can also look for information at:
143  
144 =over 4
145  
146 =item * RT: CPAN's request tracker (report bugs here)
147  
148 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=QRCode-Base45>
149  
150 =item * CPAN Ratings
151  
152 L<https://cpanratings.perl.org/d/QRCode-Base45>
153  
154 =item * Search CPAN
155  
156 L<https://metacpan.org/release/QRCode-Base45>
157  
158 =back
159  
160  
161 =head1 ACKNOWLEDGEMENTS
162  
163  
164 =head1 LICENSE AND COPYRIGHT
165  
166 This software is copyright (c) 2021 by Jan "Yenya" Kasprzak.
167  
168 This is free software; you can redistribute it and/or modify it under
169 the same terms as the Perl 5 programming language system itself.
170  
171  
172 =cut
173  
174 2 1; # End of QRCode::Base45
↑ Summary