This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move I18N::LangTags from ext/ to dist/
[perl5.git] / ext / MIME-Base64 / Base64.pm
CommitLineData
6fba102d
JH
1package MIME::Base64;
2
0a362e9d
RGS
3use strict;
4use vars qw(@ISA @EXPORT $VERSION);
5
6require Exporter;
e1839706 7@ISA = qw(Exporter);
0a362e9d
RGS
8@EXPORT = qw(encode_base64 decode_base64);
9
dfed8d37 10$VERSION = '3.08';
0a362e9d 11
e1839706
SP
12require XSLoader;
13XSLoader::load('MIME::Base64', $VERSION);
0a362e9d
RGS
14
15*encode = \&encode_base64;
16*decode = \&decode_base64;
17
181;
19
20__END__
21
6fba102d
JH
22=head1 NAME
23
24MIME::Base64 - Encoding and decoding of base64 strings
25
26=head1 SYNOPSIS
27
28 use MIME::Base64;
29
30 $encoded = encode_base64('Aladdin:open sesame');
31 $decoded = decode_base64($encoded);
32
33=head1 DESCRIPTION
34
691d66bd
RGS
35This module provides functions to encode and decode strings into and from the
36base64 encoding specified in RFC 2045 - I<MIME (Multipurpose Internet
37Mail Extensions)>. The base64 encoding is designed to represent
6fba102d
JH
38arbitrary sequences of octets in a form that need not be humanly
39readable. A 65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used,
40enabling 6 bits to be represented per printable character.
41
42The following functions are provided:
43
44=over 4
45
6a63fb82
AMS
46=item encode_base64($str)
47
48=item encode_base64($str, $eol);
6fba102d
JH
49
50Encode data by calling the encode_base64() function. The first
691d66bd
RGS
51argument is the string to encode. The second argument is the
52line-ending sequence to use. It is optional and defaults to "\n". The
6fba102d
JH
53returned encoded string is broken into lines of no more than 76
54characters each and it will end with $eol unless it is empty. Pass an
55empty string as second argument if you do not want the encoded string
691d66bd 56to be broken into lines.
6fba102d
JH
57
58=item decode_base64($str)
59
60Decode a base64 string by calling the decode_base64() function. This
61function takes a single argument which is the string to decode and
62returns the decoded data.
63
691d66bd
RGS
64Any character not part of the 65-character base64 subset is
65silently ignored. Characters occurring after a '=' padding character
6fba102d
JH
66are never decoded.
67
8be5f608 68If the length of the string to decode, after ignoring
691d66bd 69non-base64 chars, is not a multiple of 4 or if padding occurs too early,
6fba102d
JH
70then a warning is generated if perl is running under C<-w>.
71
72=back
73
691d66bd 74If you prefer not to import these routines into your namespace, you can
6fba102d
JH
75call them as:
76
77 use MIME::Base64 ();
78 $encoded = MIME::Base64::encode($decoded);
79 $decoded = MIME::Base64::decode($encoded);
80
81=head1 DIAGNOSTICS
82
691d66bd 83The following warnings can be generated if perl is invoked with the
6fba102d
JH
84C<-w> switch:
85
86=over 4
87
88=item Premature end of base64 data
89
90The number of characters to decode is not a multiple of 4. Legal
91base64 data should be padded with one or two "=" characters to make
4e2e3d15
SP
92its length a multiple of 4. The decoded result will be the same
93whether the padding is present or not.
6fba102d
JH
94
95=item Premature padding of base64 data
96
97The '=' padding character occurs as the first or second character
98in a base64 quartet.
99
100=back
101
e1839706
SP
102The following exception can be raised:
103
104=over 4
105
106=item Wide character in subroutine entry
107
108The string passed to encode_base64() contains characters with code
109above 255. The base64 encoding is only defined for single-byte
110characters. Use the Encode module to select the byte encoding you
111want.
112
113=back
114
6fba102d
JH
115=head1 EXAMPLES
116
117If you want to encode a large file, you should encode it in chunks
118that are a multiple of 57 bytes. This ensures that the base64 lines
119line up and that you do not end up with padding in the middle. 57
120bytes of data fills one complete base64 line (76 == 57*4/3):
121
122 use MIME::Base64 qw(encode_base64);
123
124 open(FILE, "/var/log/wtmp") or die "$!";
125 while (read(FILE, $buf, 60*57)) {
126 print encode_base64($buf);
127 }
128
129or if you know you have enough memory
130
131 use MIME::Base64 qw(encode_base64);
132 local($/) = undef; # slurp
133 print encode_base64(<STDIN>);
134
135The same approach as a command line:
136
137 perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' <file
138
691d66bd
RGS
139Decoding does not need slurp mode if every line contains a multiple
140of four base64 chars:
6fba102d
JH
141
142 perl -MMIME::Base64 -ne 'print decode_base64($_)' <file
143
e1839706
SP
144Perl v5.8 and better allow extended Unicode characters in strings.
145Such strings cannot be encoded directly, as the base64
146encoding is only defined for single-byte characters. The solution is
147to use the Encode module to select the byte encoding you want. For
148example:
149
150 use MIME::Base64 qw(encode_base64);
151 use Encode qw(encode);
152
153 $encoded = encode_base64(encode("UTF-8", "\x{FFFF}\n"));
154 print $encoded;
155
6fba102d
JH
156=head1 COPYRIGHT
157
691d66bd 158Copyright 1995-1999, 2001-2004 Gisle Aas.
6fba102d
JH
159
160This library is free software; you can redistribute it and/or
161modify it under the same terms as Perl itself.
162
163Distantly based on LWP::Base64 written by Martijn Koster
164<m.koster@nexor.co.uk> and Joerg Reichelt <j.reichelt@nexor.co.uk> and
165code posted to comp.lang.perl <3pd2lp$6gf@wsinti07.win.tue.nl> by Hans
166Mulder <hansm@wsinti07.win.tue.nl>
167
691d66bd 168The XS implementation uses code from metamail. Copyright 1991 Bell
6fba102d
JH
169Communications Research, Inc. (Bellcore)
170
8be5f608
RGS
171=head1 SEE ALSO
172
173L<MIME::QuotedPrint>
174
6fba102d 175=cut