root/Convert-Base32/trunk/lib/Convert/Base32.pm

Revision 173 (checked in by miyagawa, 19 years ago)

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 package Convert::Base32;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT);
5
6 BEGIN {
7     require Exporter;
8     @ISA = qw(Exporter);
9     @EXPORT = qw(encode_base32 decode_base32);
10    
11     $VERSION = '0.02';
12 }
13
14 use Carp ();
15
16 my %bits2char = qw@
17 00000 a
18 00001 b
19 00010 c
20 00011 d
21 00100 e
22 00101 f
23 00110 g
24 00111 h
25 01000 i
26 01001 j
27 01010 k
28 01011 l
29 01100 m
30 01101 n
31 01110 o
32 01111 p
33 10000 q
34 10001 r
35 10010 s
36 10011 t
37 10100 u
38 10101 v
39 10110 w
40 10111 x
41 11000 y
42 11001 z
43 11010 2
44 11011 3
45 11100 4
46 11101 5
47 11110 6
48 11111 7
49     @; # End of qw
50
51 my %char2bits = reverse %bits2char;
52
53 sub encode_base32($) {
54     my $str = shift;
55
56     my $stream_bits = '';
57     my $res = '';
58
59     for my $pos (0 .. length($str)-1) {
60         $stream_bits .= unpack('B*',substr($str,$pos,1));
61     }
62    
63     # If the input stream is not an even multiple of 5 bits,
64     # pad the input stream with 0 bits
65     if (my $remainder = length($stream_bits) % 5) {
66         $stream_bits .= '0' x (5 - $remainder);
67     }
68
69     while ($stream_bits =~ m/(.{5})/g) {
70         $res .= $bits2char{$1};
71     }
72
73     return $res;
74 }
75
76 sub decode_base32($) {
77     my $str = shift;
78
79     # non-base32 chars
80     if ($str =~ tr/a-z2-7//c) {
81         Carp::croak('Data contains non-base32 characters');
82     }
83
84     my $input_check = length($str) %8;
85     if ($input_check == 1 || $input_check == 3 || $input_check == 8) {
86         Carp::croak('Length of data invalid');
87     }
88    
89     my $buffer = '';
90     for my $pos (0..length($str)-1) {
91         $buffer .= $char2bits{substr($str, $pos, 1)};
92     }
93
94     my $padding = length($buffer) % 8;
95     $buffer =~ s/0{$padding}$// or Carp::croak('PADDING number of bits at the end of output buffer are not all zero');
96
97     return pack('B*', $buffer);
98 }
99
100
101 1;
102 __END__
103
104 =head1 NAME
105
106 Convert::Base32 - Encoding and decoding of base32 strings
107
108 =head1 SYNOPSIS
109
110   use Convert::Base32;
111
112   $encoded = encode_base32("\x3a\x27\x0f\x93");
113   $decoded = decode_base32($encoded);
114
115
116 =head1 DESCRIPTION
117
118 This module provides functions to convert string from / to Base32
119 encoding, specified in RACE internet-draft. The Base32 encoding is
120 designed to encode non-ASCII characters in DNS-compatible host name
121 parts.
122
123 See http://www.ietf.org/internet-drafts/draft-ietf-idn-race-03.txt for
124 more details.
125
126 =head1 FUNCTIONS
127
128 Following functions are provided; like C<MIME::Base64>, they are in
129 B<@EXPORT> array. See L<Exporter> for details.
130
131 =over 4
132
133 =item encode_base32($str)
134
135 Encode data by calling the encode_base32() function. This function
136 takes a string to encode and returns the encoded base32 string.
137
138 =item decode_base32($str)
139
140 Decode a base32 string by calling the decode_base32() function. This
141 function takes a string to decode and returns the decoded string.
142
143 This function might throw the exceptions such as "Data contains
144 non-base32 characters", "Length of data invalid" and "PADDING number
145 of bits at the end of output buffer are not all zero".
146
147 =head1 AUTHOR
148
149 Tatsuhiko Miyagawa <miyagawa@bulknews.net>
150
151 This library is free software; you can redistribute it and/or
152 modify it under the same terms as Perl itself.
153
154 =head1 SEE ALSO
155
156 http://www.ietf.org/internet-drafts/draft-ietf-idn-race-03.txt, L<MIME::Base64>, L<Convert::RACE>.
157
158 =cut
Note: See TracBrowser for help on using the browser.