line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Convert::Base32; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
54793
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
78
|
|
4
|
|
|
|
|
|
|
#use warnings; |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
12
|
use Carp qw( ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
43
|
|
7
|
2
|
|
|
2
|
|
9
|
use Exporter qw( ); |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
41
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
11
|
use vars qw( $VERSION @ISA @EXPORT ); |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
2161
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$VERSION = '0.06'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
push @ISA, 'Exporter'; |
14
|
|
|
|
|
|
|
@EXPORT = qw( encode_base32 decode_base32 ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my @syms = ( 'a'..'z', '2'..'7' ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my %bits2char; |
20
|
|
|
|
|
|
|
my @char2bits; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
for (0..$#syms) { |
23
|
|
|
|
|
|
|
my $sym = $syms[$_]; |
24
|
|
|
|
|
|
|
my $bin = sprintf('%05b', $_); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$char2bits[ ord lc $sym ] = $bin; |
27
|
|
|
|
|
|
|
$char2bits[ ord uc $sym ] = $bin; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
do { |
30
|
|
|
|
|
|
|
$bits2char{$bin} = $sym; |
31
|
|
|
|
|
|
|
} while $bin =~ s/(.+)0\z/$1/s; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub encode_base32_pre58($) { |
36
|
516
|
100
|
|
516
|
0
|
717536
|
length($_[0]) == bytes::length($_[0]) |
37
|
|
|
|
|
|
|
or Carp::croak('Data contains non-bytes'); |
38
|
|
|
|
|
|
|
|
39
|
260
|
|
|
|
|
3358
|
my $str = unpack('B*', $_[0]); |
40
|
|
|
|
|
|
|
|
41
|
260
|
50
|
|
|
|
2353
|
if (length($str) < 8*1024) { |
42
|
260
|
|
|
|
|
9698
|
return join '', @bits2char{ $str =~ /.{1,5}/g }; |
43
|
|
|
|
|
|
|
} else { |
44
|
|
|
|
|
|
|
# Slower, but uses less memory |
45
|
0
|
|
|
|
|
0
|
$str =~ s/(.{5})/$bits2char{$1}/sg; |
46
|
0
|
|
|
|
|
0
|
return $str; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub encode_base32_perl58($) { |
52
|
0
|
0
|
|
0
|
0
|
0
|
$_[0] =~ tr/\x00-\xFF//c |
53
|
|
|
|
|
|
|
and Carp::croak('Data contains non-bytes'); |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
0
|
my $str = unpack('B*', $_[0]); |
56
|
|
|
|
|
|
|
|
57
|
0
|
0
|
|
|
|
0
|
if (length($str) < 8*1024) { |
58
|
0
|
|
|
|
|
0
|
return join '', @bits2char{ unpack '(a5)*', $str }; |
59
|
|
|
|
|
|
|
} else { |
60
|
|
|
|
|
|
|
# Slower, but uses less memory |
61
|
0
|
|
|
|
|
0
|
$str =~ s/(.{5})/$bits2char{$1}/sg; |
62
|
0
|
|
|
|
|
0
|
return $str; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub decode_base32_pre58($) { |
68
|
556
|
100
|
66
|
556
|
0
|
569965
|
( length($_[0]) != bytes::length($_[0]) || $_[0] =~ tr/a-zA-Z2-7//c ) |
69
|
|
|
|
|
|
|
and Carp::croak('Data contains non-base32 characters'); |
70
|
|
|
|
|
|
|
|
71
|
102
|
|
|
|
|
2330
|
my $str; |
72
|
102
|
50
|
|
|
|
676
|
if (length($_[0]) < 8*1024) { |
73
|
102
|
|
|
|
|
715
|
$str = join '', @char2bits[ unpack 'C*', $_[0] ]; |
74
|
|
|
|
|
|
|
} else { |
75
|
|
|
|
|
|
|
# Slower, but uses less memory |
76
|
0
|
|
|
|
|
0
|
($str = $_[0]) =~ s/(.)/$char2bits[ord($1)]/sg; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
102
|
|
|
|
|
382
|
my $padding = length($str) % 8; |
80
|
102
|
100
|
|
|
|
293
|
$padding < 5 |
81
|
|
|
|
|
|
|
or Carp::croak('Length of data invalid'); |
82
|
96
|
100
|
|
|
|
14535
|
$str =~ s/0{$padding}\z// |
83
|
|
|
|
|
|
|
or Carp::croak('Padding bits at the end of output buffer are not all zero'); |
84
|
|
|
|
|
|
|
|
85
|
76
|
|
|
|
|
823
|
return pack('B*', $str); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub decode_base32_perl58($) { |
90
|
0
|
0
|
|
0
|
0
|
|
$_[0] =~ tr/a-zA-Z2-7//c |
91
|
|
|
|
|
|
|
and Carp::croak('Data contains non-base32 characters'); |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
my $str; |
94
|
0
|
0
|
|
|
|
|
if (length($_[0]) < 8*1024) { |
95
|
0
|
|
|
|
|
|
$str = join '', @char2bits[ unpack 'C*', $_[0] ]; |
96
|
|
|
|
|
|
|
} else { |
97
|
|
|
|
|
|
|
# Slower, but uses less memory |
98
|
0
|
|
|
|
|
|
($str = $_[0]) =~ s/(.)/$char2bits[ord($1)]/sg; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my $padding = length($str) % 8; |
102
|
0
|
0
|
|
|
|
|
$padding < 5 |
103
|
|
|
|
|
|
|
or Carp::croak('Length of data invalid'); |
104
|
0
|
0
|
|
|
|
|
$str =~ s/0{$padding}\z// |
105
|
|
|
|
|
|
|
or Carp::croak('Padding bits at the end of output buffer are not all zero'); |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
return pack('B*', $str); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
if ($] lt '5.800000') { |
112
|
|
|
|
|
|
|
require bytes; |
113
|
|
|
|
|
|
|
*encode_base32 = \&encode_base32_pre58; |
114
|
|
|
|
|
|
|
*decode_base32 = \&decode_base32_pre58; |
115
|
|
|
|
|
|
|
} else { |
116
|
|
|
|
|
|
|
*encode_base32 = \&encode_base32_perl58; |
117
|
|
|
|
|
|
|
*decode_base32 = \&decode_base32_perl58; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; |
122
|
|
|
|
|
|
|
__END__ |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 NAME |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Convert::Base32 - Encoding and decoding of base32 strings |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 SYNOPSIS |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
use Convert::Base32; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
$encoded = encode_base32("\x3a\x27\x0f\x93"); |
133
|
|
|
|
|
|
|
$decoded = decode_base32($encoded); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 DESCRIPTION |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
This module provides functions to convert string from / to Base32 |
139
|
|
|
|
|
|
|
encoding, specified in RACE internet-draft. The Base32 encoding is |
140
|
|
|
|
|
|
|
designed to encode non-ASCII characters in DNS-compatible host name |
141
|
|
|
|
|
|
|
parts. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
See http://tools.ietf.org/html/draft-ietf-idn-race-03 for more details. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 FUNCTIONS |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Following functions are provided; like C<MIME::Base64>, they are in |
148
|
|
|
|
|
|
|
B<@EXPORT> array. See L<Exporter> for details. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=over 4 |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item encode_base32($str) |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Encode data by calling the encode_base32() function. This function |
155
|
|
|
|
|
|
|
takes a string of bytes to encode and returns the encoded base32 string. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item decode_base32($str) |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Decode a base32 string by calling the decode_base32() function. This |
160
|
|
|
|
|
|
|
function takes a string to decode and returns the decoded string. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
This function might throw the exceptions such as "Data contains |
163
|
|
|
|
|
|
|
non-base32 characters", "Length of data invalid" and "Padding |
164
|
|
|
|
|
|
|
bits at the end of output buffer are not all zero". |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
decode_base32 differs from the specification in that upper case |
167
|
|
|
|
|
|
|
letters are treated as their lower case equivalent rather than |
168
|
|
|
|
|
|
|
producing an error. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=back |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 AUTHOR |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Tatsuhiko Miyagawa <miyagawa@bulknews.net> |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Eric Brine <ikegami@adaelis.com> |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or |
179
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 SEE ALSO |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
http://www.ietf.org/internet-drafts/draft-ietf-idn-race-03.txt, L<MIME::Base64>, L<Convert::RACE>. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=cut |