1MIME::Base64(3pm) Perl Programmers Reference Guide MIME::Base64(3pm)
2
3
4
6 MIME::Base64 - Encoding and decoding of base64 strings
7
9 use MIME::Base64;
10
11 $encoded = encode_base64('Aladdin:open sesame');
12 $decoded = decode_base64($encoded);
13
15 This module provides functions to encode and decode strings into and
16 from the base64 encoding specified in RFC 2045 - MIME (Multipurpose
17 Internet Mail Extensions). The base64 encoding is designed to represent
18 arbitrary sequences of octets in a form that need not be humanly read‐
19 able. A 65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used,
20 enabling 6 bits to be represented per printable character.
21
22 The following functions are provided:
23
24 encode_base64($str)
25 encode_base64($str, $eol);
26 Encode data by calling the encode_base64() function. The first
27 argument is the string to encode. The second argument is the line-
28 ending sequence to use. It is optional and defaults to "\n". The
29 returned encoded string is broken into lines of no more than 76
30 characters each and it will end with $eol unless it is empty. Pass
31 an empty string as second argument if you do not want the encoded
32 string to be broken into lines.
33
34 decode_base64($str)
35 Decode a base64 string by calling the decode_base64() function.
36 This function takes a single argument which is the string to decode
37 and returns the decoded data.
38
39 Any character not part of the 65-character base64 subset is
40 silently ignored. Characters occurring after a '=' padding charac‐
41 ter are never decoded.
42
43 If the length of the string to decode, after ignoring non-base64
44 chars, is not a multiple of 4 or if padding occurs too early, then
45 a warning is generated if perl is running under "-w".
46
47 If you prefer not to import these routines into your namespace, you can
48 call them as:
49
50 use MIME::Base64 ();
51 $encoded = MIME::Base64::encode($decoded);
52 $decoded = MIME::Base64::decode($encoded);
53
55 The following warnings can be generated if perl is invoked with the
56 "-w" switch:
57
58 Premature end of base64 data
59 The number of characters to decode is not a multiple of 4. Legal
60 base64 data should be padded with one or two "=" characters to make
61 its length a multiple of 4. The decoded result will be the same
62 whether the padding is present or not.
63
64 Premature padding of base64 data
65 The '=' padding character occurs as the first or second character
66 in a base64 quartet.
67
68 The following exception can be raised:
69
70 Wide character in subroutine entry
71 The string passed to encode_base64() contains characters with code
72 above 255. The base64 encoding is only defined for single-byte
73 characters. Use the Encode module to select the byte encoding you
74 want.
75
77 If you want to encode a large file, you should encode it in chunks that
78 are a multiple of 57 bytes. This ensures that the base64 lines line up
79 and that you do not end up with padding in the middle. 57 bytes of data
80 fills one complete base64 line (76 == 57*4/3):
81
82 use MIME::Base64 qw(encode_base64);
83
84 open(FILE, "/var/log/wtmp") or die "$!";
85 while (read(FILE, $buf, 60*57)) {
86 print encode_base64($buf);
87 }
88
89 or if you know you have enough memory
90
91 use MIME::Base64 qw(encode_base64);
92 local($/) = undef; # slurp
93 print encode_base64(<STDIN>);
94
95 The same approach as a command line:
96
97 perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' <file
98
99 Decoding does not need slurp mode if every line contains a multiple of
100 four base64 chars:
101
102 perl -MMIME::Base64 -ne 'print decode_base64($_)' <file
103
104 Perl v5.8 and better allow extended Unicode characters in strings.
105 Such strings cannot be encoded directly, as the base64 encoding is only
106 defined for single-byte characters. The solution is to use the Encode
107 module to select the byte encoding you want. For example:
108
109 use MIME::Base64 qw(encode_base64);
110 use Encode qw(encode);
111
112 $encoded = encode_base64(encode("UTF-8", "\x{FFFF}\n"));
113 print $encoded;
114
116 Copyright 1995-1999, 2001-2004 Gisle Aas.
117
118 This library is free software; you can redistribute it and/or modify it
119 under the same terms as Perl itself.
120
121 Distantly based on LWP::Base64 written by Martijn Koster
122 <m.koster@nexor.co.uk> and Joerg Reichelt <j.reichelt@nexor.co.uk> and
123 code posted to comp.lang.perl <3pd2lp$6gf@wsinti07.win.tue.nl> by Hans
124 Mulder <hansm@wsinti07.win.tue.nl>
125
126 The XS implementation uses code from metamail. Copyright 1991 Bell
127 Communications Research, Inc. (Bellcore)
128
130 MIME::QuotedPrint
131
132
133
134perl v5.8.8 2001-09-21 MIME::Base64(3pm)