1package MIME::Base32;
2
3use 5.008001;
4use strict;
5use warnings;
6
7require Exporter;
8our @ISA       = qw(Exporter);
9our @EXPORT    = qw(encode_base32 decode_base32);
10our @EXPORT_OK = qw(
11    encode_rfc3548 decode_rfc3548 encode_09AV decode_09AV
12    encode_base32hex decode_base32hex
13);
14
15our $VERSION = "1.303";
16$VERSION = eval $VERSION;
17
18sub encode         { return encode_base32(@_) }
19sub encode_rfc3548 { return encode_base32(@_) }
20
21sub encode_base32 {
22    my $arg = shift;
23    return '' unless defined($arg);    # mimic MIME::Base64
24
25    $arg = unpack('B*', $arg);
26    $arg =~ s/(.....)/000$1/g;
27    my $l = length($arg);
28    if ($l & 7) {
29        my $e = substr($arg, $l & ~7);
30        $arg = substr($arg, 0, $l & ~7);
31        $arg .= "000$e" . '0' x (5 - length $e);
32    }
33    $arg = pack('B*', $arg);
34    $arg =~ tr|\0-\37|A-Z2-7|;
35    return $arg;
36}
37
38sub decode         { return decode_base32(@_) }
39sub decode_rfc3548 { return decode_base32(@_) }
40
41sub decode_base32 {
42    my $arg = uc(shift || '');    # mimic MIME::Base64
43
44    $arg =~ tr|A-Z2-7|\0-\37|;
45    $arg = unpack('B*', $arg);
46    $arg =~ s/000(.....)/$1/g;
47    my $l = length $arg;
48    $arg = substr($arg, 0, $l & ~7) if $l & 7;
49    $arg = pack('B*', $arg);
50    return $arg;
51}
52
53sub encode_09AV { return encode_base32hex(@_) }
54
55sub encode_base32hex {
56    my $arg = shift;
57    return '' unless defined($arg);    # mimic MIME::Base64
58
59    $arg = unpack('B*', $arg);
60    $arg =~ s/(.....)/000$1/g;
61    my $l = length($arg);
62    if ($l & 7) {
63        my $e = substr($arg, $l & ~7);
64        $arg = substr($arg, 0, $l & ~7);
65        $arg .= "000$e" . '0' x (5 - length $e);
66    }
67    $arg = pack('B*', $arg);
68    $arg =~ tr|\0-\37|0-9A-V|;
69    return $arg;
70}
71
72sub decode_09AV { return decode_base32hex(@_) }
73
74sub decode_base32hex {
75    my $arg = uc(shift || '');    # mimic MIME::Base64
76
77    $arg =~ tr|0-9A-V|\0-\37|;
78    $arg = unpack('B*', $arg);
79    $arg =~ s/000(.....)/$1/g;
80    my $l = length($arg);
81    $arg = substr($arg, 0, $l & ~7) if $l & 7;
82    $arg = pack('B*', $arg);
83    return $arg;
84}
85
861;
87
88=encoding utf8
89
90=head1 NAME
91
92MIME::Base32 - Base32 encoder and decoder
93
94=head1 SYNOPSIS
95
96    #!/usr/bin/env perl
97    use strict;
98    use warnings;
99    use MIME::Base32;
100
101    my $encoded = encode_base32('Aladdin: open sesame');
102    my $decoded = decode_base32($encoded);
103
104=head1 DESCRIPTION
105
106This module is for encoding/decoding data much the way that L<MIME::Base64> does.
107
108Prior to version 1.0, L<MIME::Base32> used the C<base32hex> (or C<[0-9A-V]>) encoding and
109decoding methods by default. If you need to maintain that behavior, please call
110C<encode_base32hex> or C<decode_base32hex> functions directly.
111
112Now, in accordance with L<RFC-3548, Section 5|https://tools.ietf.org/html/rfc3548#section-5>,
113L<MIME::Base32> uses the C<encode_base32> and C<decode_base32> functions by default.
114
115=head1 FUNCTIONS
116
117The following primary functions are provided:
118
119=head2 decode
120
121Synonym for C<decode_base32>
122
123=head2 decode_rfc3548
124
125Synonym for C<decode_base32>
126
127=head2 decode_base32
128
129    my $string = decode_base32($encoded_data);
130
131Decode some encoded data back into a string of text or binary data.
132
133=head2 decode_09AV
134
135Synonym for C<decode_base32hex>
136
137=head2 decode_base32hex
138
139    my $string_or_binary_data = MIME::Base32::decode_base32hex($encoded_data);
140
141Decode some encoded data back into a string of text or binary data.
142
143=head2 encode
144
145Synonym for C<encode_base32>
146
147=head2 encode_rfc3548
148
149Synonym for C<encode_base32>
150
151=head2 encode_base32
152
153    my $encoded = encode_base32("some string");
154
155Encode a string of text or binary data.
156
157=head2 encode_09AV
158
159Synonym for C<encode_base32hex>
160
161=head2 encode_base32hex
162
163    my $encoded = MIME::Base32::encode_base32hex("some string");
164
165Encode a string of text or binary data. This uses the C<hex> (or C<[0-9A-V]>) method.
166
167=head1 AUTHORS
168
169Jens Rehsack - <rehsack@cpan.org> - Current maintainer
170
171Chase Whitener
172
173Daniel Peder - sponsored by Infoset s.r.o., Czech Republic
174 - <Daniel.Peder@InfoSet.COM> http://www.infoset.com - Original author
175
176=head1 BUGS
177
178Before reporting any new issue, bug or alike, please check
179L<https://rt.cpan.org/Dist/Display.html?Queue=MIME-Base32>,
180L<https://github.com/perl5-utils/MIME-Base32/issues> or
181L<https://github.com/perl5-utils/MIME-Base32/pulls>, respectively, whether
182the issue is already reported.
183
184Please report any bugs or feature requests to
185C<bug-mime-base32 at rt.cpan.org>, or through the web interface at
186L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=MIME-Base32>.
187I will be notified, and then you'll automatically be notified of progress
188on your bug as I make changes.
189
190Any and all criticism, bug reports, enhancements, fixes, etc. are appreciated.
191
192=head1 SUPPORT
193
194You can find documentation for this module with the perldoc command.
195
196    perldoc MIME::Base32
197
198You can also look for information at:
199
200=over 4
201
202=item * RT: CPAN's request tracker
203
204L<https://rt.cpan.org/Dist/Display.html?Name=MIME-Base32>
205
206=item * AnnoCPAN: Annotated CPAN documentation
207
208L<http://annocpan.org/dist/MIME-Base32>
209
210=item * MetaCPAN
211
212L<https://metacpan.org/release/MIME-Base32>
213
214=back
215
216=head1 COPYRIGHT AND LICENSE INFORMATION
217
218Copyright (c) 2003-2010 Daniel Peder.  All rights reserved.
219Copyright (c) 2015-2016 Chase Whitener.  All rights reserved.
220Copyright (c) 2016 Jens Rehsack.  All rights reserved.
221
222This library is free software; you can redistribute it and/or
223modify it under the same terms as Perl itself.
224
225=head1 SEE ALSO
226
227L<MIME::Base64>, L<RFC-3548|https://tools.ietf.org/html/rfc3548#section-5>
228
229=cut
230