xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/ext/MIME/Base64/Base64.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage MIME::Base64;
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gate# $Id: Base64.pm,v 3.1 2004/03/29 11:55:49 gisle Exp $
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gateuse strict;
6*0Sstevel@tonic-gateuse vars qw(@ISA @EXPORT $VERSION);
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gaterequire Exporter;
9*0Sstevel@tonic-gaterequire DynaLoader;
10*0Sstevel@tonic-gate@ISA = qw(Exporter DynaLoader);
11*0Sstevel@tonic-gate@EXPORT = qw(encode_base64 decode_base64);
12*0Sstevel@tonic-gate
13*0Sstevel@tonic-gate$VERSION = '3.01';
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gateMIME::Base64->bootstrap($VERSION);
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gate*encode = \&encode_base64;
18*0Sstevel@tonic-gate*decode = \&decode_base64;
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gate1;
21*0Sstevel@tonic-gate
22*0Sstevel@tonic-gate__END__
23*0Sstevel@tonic-gate
24*0Sstevel@tonic-gate=head1 NAME
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gateMIME::Base64 - Encoding and decoding of base64 strings
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate=head1 SYNOPSIS
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate use MIME::Base64;
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate $encoded = encode_base64('Aladdin:open sesame');
33*0Sstevel@tonic-gate $decoded = decode_base64($encoded);
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate=head1 DESCRIPTION
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gateThis module provides functions to encode and decode strings into and from the
38*0Sstevel@tonic-gatebase64 encoding specified in RFC 2045 - I<MIME (Multipurpose Internet
39*0Sstevel@tonic-gateMail Extensions)>. The base64 encoding is designed to represent
40*0Sstevel@tonic-gatearbitrary sequences of octets in a form that need not be humanly
41*0Sstevel@tonic-gatereadable. A 65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used,
42*0Sstevel@tonic-gateenabling 6 bits to be represented per printable character.
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gateThe following functions are provided:
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate=over 4
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate=item encode_base64($str)
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate=item encode_base64($str, $eol);
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gateEncode data by calling the encode_base64() function.  The first
53*0Sstevel@tonic-gateargument is the string to encode.  The second argument is the
54*0Sstevel@tonic-gateline-ending sequence to use.  It is optional and defaults to "\n".  The
55*0Sstevel@tonic-gatereturned encoded string is broken into lines of no more than 76
56*0Sstevel@tonic-gatecharacters each and it will end with $eol unless it is empty.  Pass an
57*0Sstevel@tonic-gateempty string as second argument if you do not want the encoded string
58*0Sstevel@tonic-gateto be broken into lines.
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate=item decode_base64($str)
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gateDecode a base64 string by calling the decode_base64() function.  This
63*0Sstevel@tonic-gatefunction takes a single argument which is the string to decode and
64*0Sstevel@tonic-gatereturns the decoded data.
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gateAny character not part of the 65-character base64 subset is
67*0Sstevel@tonic-gatesilently ignored.  Characters occurring after a '=' padding character
68*0Sstevel@tonic-gateare never decoded.
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gateIf the length of the string to decode, after ignoring
71*0Sstevel@tonic-gatenon-base64 chars, is not a multiple of 4 or if padding occurs too early,
72*0Sstevel@tonic-gatethen a warning is generated if perl is running under C<-w>.
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate=back
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gateIf you prefer not to import these routines into your namespace, you can
77*0Sstevel@tonic-gatecall them as:
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate    use MIME::Base64 ();
80*0Sstevel@tonic-gate    $encoded = MIME::Base64::encode($decoded);
81*0Sstevel@tonic-gate    $decoded = MIME::Base64::decode($encoded);
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gate=head1 DIAGNOSTICS
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gateThe following warnings can be generated if perl is invoked with the
86*0Sstevel@tonic-gateC<-w> switch:
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate=over 4
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate=item Premature end of base64 data
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gateThe number of characters to decode is not a multiple of 4.  Legal
93*0Sstevel@tonic-gatebase64 data should be padded with one or two "=" characters to make
94*0Sstevel@tonic-gateits length a multiple of 4.  The decoded result will anyway be as if
95*0Sstevel@tonic-gatethe padding was there.
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate=item Premature padding of base64 data
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gateThe '=' padding character occurs as the first or second character
100*0Sstevel@tonic-gatein a base64 quartet.
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate=back
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate=head1 EXAMPLES
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gateIf you want to encode a large file, you should encode it in chunks
107*0Sstevel@tonic-gatethat are a multiple of 57 bytes.  This ensures that the base64 lines
108*0Sstevel@tonic-gateline up and that you do not end up with padding in the middle. 57
109*0Sstevel@tonic-gatebytes of data fills one complete base64 line (76 == 57*4/3):
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate   use MIME::Base64 qw(encode_base64);
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate   open(FILE, "/var/log/wtmp") or die "$!";
114*0Sstevel@tonic-gate   while (read(FILE, $buf, 60*57)) {
115*0Sstevel@tonic-gate       print encode_base64($buf);
116*0Sstevel@tonic-gate   }
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gateor if you know you have enough memory
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate   use MIME::Base64 qw(encode_base64);
121*0Sstevel@tonic-gate   local($/) = undef;  # slurp
122*0Sstevel@tonic-gate   print encode_base64(<STDIN>);
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gateThe same approach as a command line:
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate   perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' <file
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gateDecoding does not need slurp mode if every line contains a multiple
129*0Sstevel@tonic-gateof four base64 chars:
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate   perl -MMIME::Base64 -ne 'print decode_base64($_)' <file
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate=head1 COPYRIGHT
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gateCopyright 1995-1999, 2001-2004 Gisle Aas.
136*0Sstevel@tonic-gate
137*0Sstevel@tonic-gateThis library is free software; you can redistribute it and/or
138*0Sstevel@tonic-gatemodify it under the same terms as Perl itself.
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gateDistantly based on LWP::Base64 written by Martijn Koster
141*0Sstevel@tonic-gate<m.koster@nexor.co.uk> and Joerg Reichelt <j.reichelt@nexor.co.uk> and
142*0Sstevel@tonic-gatecode posted to comp.lang.perl <3pd2lp$6gf@wsinti07.win.tue.nl> by Hans
143*0Sstevel@tonic-gateMulder <hansm@wsinti07.win.tue.nl>
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gateThe XS implementation uses code from metamail.  Copyright 1991 Bell
146*0Sstevel@tonic-gateCommunications Research, Inc. (Bellcore)
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate=head1 SEE ALSO
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gateL<MIME::QuotedPrint>
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate=cut
153