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