4 use vars qw(@ISA @EXPORT $VERSION);
8 @EXPORT = qw(encode_base64 decode_base64);
13 XSLoader::load('MIME::Base64', $VERSION);
15 *encode = \&encode_base64;
16 *decode = \&decode_base64;
24 MIME::Base64 - Encoding and decoding of base64 strings
30 $encoded = encode_base64('Aladdin:open sesame');
31 $decoded = decode_base64($encoded);
35 This module provides functions to encode and decode strings into and from the
36 base64 encoding specified in RFC 2045 - I<MIME (Multipurpose Internet
37 Mail Extensions)>. The base64 encoding is designed to represent
38 arbitrary sequences of octets in a form that need not be humanly
39 readable. A 65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used,
40 enabling 6 bits to be represented per printable character.
42 The following functions are provided:
46 =item encode_base64($str)
48 =item encode_base64($str, $eol);
50 Encode data by calling the encode_base64() function. The first
51 argument is the string to encode. The second argument is the
52 line-ending sequence to use. It is optional and defaults to "\n". The
53 returned encoded string is broken into lines of no more than 76
54 characters each and it will end with $eol unless it is empty. Pass an
55 empty string as second argument if you do not want the encoded string
56 to be broken into lines.
58 =item decode_base64($str)
60 Decode a base64 string by calling the decode_base64() function. This
61 function takes a single argument which is the string to decode and
62 returns the decoded data.
64 Any character not part of the 65-character base64 subset is
65 silently ignored. Characters occurring after a '=' padding character
68 If the length of the string to decode, after ignoring
69 non-base64 chars, is not a multiple of 4 or if padding occurs too early,
70 then a warning is generated if perl is running under C<-w>.
74 If you prefer not to import these routines into your namespace, you can
78 $encoded = MIME::Base64::encode($decoded);
79 $decoded = MIME::Base64::decode($encoded);
83 The following warnings can be generated if perl is invoked with the
88 =item Premature end of base64 data
90 The number of characters to decode is not a multiple of 4. Legal
91 base64 data should be padded with one or two "=" characters to make
92 its length a multiple of 4. The decoded result will be the same
93 whether the padding is present or not.
95 =item Premature padding of base64 data
97 The '=' padding character occurs as the first or second character
102 The following exception can be raised:
106 =item Wide character in subroutine entry
108 The string passed to encode_base64() contains characters with code
109 above 255. The base64 encoding is only defined for single-byte
110 characters. Use the Encode module to select the byte encoding you
117 If you want to encode a large file, you should encode it in chunks
118 that are a multiple of 57 bytes. This ensures that the base64 lines
119 line up and that you do not end up with padding in the middle. 57
120 bytes of data fills one complete base64 line (76 == 57*4/3):
122 use MIME::Base64 qw(encode_base64);
124 open(FILE, "/var/log/wtmp") or die "$!";
125 while (read(FILE, $buf, 60*57)) {
126 print encode_base64($buf);
129 or if you know you have enough memory
131 use MIME::Base64 qw(encode_base64);
132 local($/) = undef; # slurp
133 print encode_base64(<STDIN>);
135 The same approach as a command line:
137 perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' <file
139 Decoding does not need slurp mode if every line contains a multiple
140 of four base64 chars:
142 perl -MMIME::Base64 -ne 'print decode_base64($_)' <file
144 Perl v5.8 and better allow extended Unicode characters in strings.
145 Such strings cannot be encoded directly, as the base64
146 encoding is only defined for single-byte characters. The solution is
147 to use the Encode module to select the byte encoding you want. For
150 use MIME::Base64 qw(encode_base64);
151 use Encode qw(encode);
153 $encoded = encode_base64(encode("UTF-8", "\x{FFFF}\n"));
158 Copyright 1995-1999, 2001-2004 Gisle Aas.
160 This library is free software; you can redistribute it and/or
161 modify it under the same terms as Perl itself.
163 Distantly based on LWP::Base64 written by Martijn Koster
164 <m.koster@nexor.co.uk> and Joerg Reichelt <j.reichelt@nexor.co.uk> and
165 code posted to comp.lang.perl <3pd2lp$6gf@wsinti07.win.tue.nl> by Hans
166 Mulder <hansm@wsinti07.win.tue.nl>
168 The XS implementation uses code from metamail. Copyright 1991 Bell
169 Communications Research, Inc. (Bellcore)