source: main/trunk/greenstone2/build-src/packages/w3mir/MIME-Base64-2.11/Base64.pm@ 27132

Last change on this file since 27132 was 718, checked in by davidb, 25 years ago

added m3mir package

  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1#
2# $Id: Base64.pm 718 1999-10-19 02:36:47Z davidb $
3
4package MIME::Base64;
5
6=head1 NAME
7
8MIME::Base64 - Encoding and decoding of base64 strings
9
10=head1 SYNOPSIS
11
12 use MIME::Base64;
13
14 $encoded = encode_base64('Aladdin:open sesame');
15 $decoded = decode_base64($encoded);
16
17=head1 DESCRIPTION
18
19This module provides functions to encode and decode strings into the
20Base64 encoding specified in RFC 2045 - I<MIME (Multipurpose Internet
21Mail Extensions)>. The Base64 encoding is designed to represent
22arbitrary sequences of octets in a form that need not be humanly
23readable. A 65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used,
24enabling 6 bits to be represented per printable character.
25
26The following functions are provided:
27
28=over 4
29
30=item encode_base64($str, [$eol])
31
32Encode data by calling the encode_base64() function. The first
33argument is the string to encode. The second argument is the line
34ending sequence to use (it is optional and defaults to C<"\n">). The
35returned encoded string is broken into lines of no more than 76
36characters each and it will end with $eol unless it is empty. Pass an
37empty string as second argument if you do not want the encoded string
38broken into lines.
39
40=item decode_base64($str)
41
42Decode a base64 string by calling the decode_base64() function. This
43function takes a single argument which is the string to decode and
44returns the decoded data.
45
46Any character not part of the 65-character base64 subset set is
47silently ignored. Characters occuring after a '=' padding character
48are never decoded.
49
50If the length of the string to decode (after ignoring
51non-base64 chars) is not a multiple of 4 or padding occurs too ealy,
52then a warning is generated if perl is running under C<-w>.
53
54=back
55
56If you prefer not to import these routines into your namespace you can
57call them as:
58
59 use MIME::Base64 ();
60 $encoded = MIME::Base64::encode($decoded);
61 $decoded = MIME::Base64::decode($encoded);
62
63=head1 DIAGNOSTICS
64
65The following warnings might be generated if perl is invoked with the
66C<-w> switch:
67
68=over 4
69
70=item Premature end of base64 data
71
72The number of characters to decode is not a multiple of 4. Legal
73base64 data should be padded with one or two "=" characters to make
74its length a multiple of 4. The decoded result will anyway be as if
75the padding was there.
76
77=item Premature padding of base64 data
78
79The '=' padding character occurs as the first or second character
80in a base64 quartet.
81
82=back
83
84=head1 EXAMPLES
85
86If you want to encode a large file, you should encode it in chunks
87that are a multiple of 57 bytes. This ensures that the base64 lines
88line up and that you do not end up with padding in the middle. 57
89bytes of data fills one complete base64 line (76 == 57*4/3):
90
91 use MIME::Base64 qw(encode_base64);
92
93 open(FILE, "/var/log/wtmp") or die "$!";
94 while (read(FILE, $buf, 60*57)) {
95 print encode_base64($buf);
96 }
97
98or if you know you have enough memory
99
100 use MIME::Base64 qw(encode_base64);
101 local($/) = undef; # slurp
102 print encode_base64(<STDIN>);
103
104=head1 COPYRIGHT
105
106Copyright 1995-1999 Gisle Aas.
107
108This library is free software; you can redistribute it and/or
109modify it under the same terms as Perl itself.
110
111Distantly based on LWP::Base64 written by Martijn Koster
112<[email protected]> and Joerg Reichelt <[email protected]> and
113code posted to comp.lang.perl <[email protected]> by Hans
114Mulder <[email protected]>
115
116The XS implementation use code from metamail. Copyright 1991 Bell
117Communications Research, Inc. (Bellcore)
118
119=cut
120
121use strict;
122use vars qw(@ISA @EXPORT $VERSION $OLD_CODE);
123
124require Exporter;
125require DynaLoader;
126@ISA = qw(Exporter DynaLoader);
127@EXPORT = qw(encode_base64 decode_base64);
128
129$VERSION = '2.11';
130
131eval { bootstrap MIME::Base64 $VERSION; };
132if ($@) {
133 # can't bootstrap XS implementation, use perl implementation
134 *encode_base64 = \&old_encode_base64;
135 *decode_base64 = \&old_decode_base64;
136
137 $OLD_CODE = $@;
138 #warn $@ if $^W;
139}
140
141# Historically this module has been implemented as pure perl code.
142# The XS implementation runs about 20 times faster, but the Perl
143# code might be more portable, so it is still here.
144
145use integer;
146
147sub old_encode_base64 ($;$)
148{
149 my $res = "";
150 my $eol = $_[1];
151 $eol = "\n" unless defined $eol;
152 pos($_[0]) = 0; # ensure start at the beginning
153 while ($_[0] =~ /(.{1,45})/gs) {
154 $res .= substr(pack('u', $1), 1);
155 chop($res);
156 }
157 $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
158 # fix padding at the end
159 my $padding = (3 - length($_[0]) % 3) % 3;
160 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
161 # break encoded string into lines of no more than 76 characters each
162 if (length $eol) {
163 $res =~ s/(.{1,76})/$1$eol/g;
164 }
165 $res;
166}
167
168
169sub old_decode_base64 ($)
170{
171 local($^W) = 0; # unpack("u",...) gives bogus warning in 5.00[123]
172
173 my $str = shift;
174 my $res = "";
175
176 $str =~ tr|A-Za-z0-9+=/||cd; # remove non-base64 chars
177 if (length($str) % 4) {
178 require Carp;
179 Carp::carp("Length of base64 data not a multiple of 4")
180 }
181 $str =~ s/=+$//; # remove padding
182 $str =~ tr|A-Za-z0-9+/| -_|; # convert to uuencoded format
183 while ($str =~ /(.{1,60})/gs) {
184 my $len = chr(32 + length($1)*3/4); # compute length byte
185 $res .= unpack("u", $len . $1 ); # uudecode
186 }
187 $res;
188}
189
190# Set up aliases so that these functions also can be called as
191#
192# MIME::Base64::encode();
193# MIME::Base64::decode();
194
195*encode = \&encode_base64;
196*decode = \&decode_base64;
197
1981;
Note: See TracBrowser for help on using the repository browser.