Install Perl 5.8.8
[msysgit.git] / mingw / html / lib / MIME / Base64.html
blobfd313b4fbaec542baf0b31f23f2d4910eaf550a3
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>MIME::Base64 - Encoding and decoding of base64 strings</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;MIME::Base64 - Encoding and decoding of base64 strings</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#synopsis">SYNOPSIS</a></li>
24 <li><a href="#description">DESCRIPTION</a></li>
25 <li><a href="#diagnostics">DIAGNOSTICS</a></li>
26 <li><a href="#examples">EXAMPLES</a></li>
27 <li><a href="#copyright">COPYRIGHT</a></li>
28 <li><a href="#see_also">SEE ALSO</a></li>
29 </ul>
30 <!-- INDEX END -->
32 <hr />
33 <p>
34 </p>
35 <h1><a name="name">NAME</a></h1>
36 <p>MIME::Base64 - Encoding and decoding of base64 strings</p>
37 <p>
38 </p>
39 <hr />
40 <h1><a name="synopsis">SYNOPSIS</a></h1>
41 <pre>
42 use MIME::Base64;</pre>
43 <pre>
44 $encoded = encode_base64('Aladdin:open sesame');
45 $decoded = decode_base64($encoded);</pre>
46 <p>
47 </p>
48 <hr />
49 <h1><a name="description">DESCRIPTION</a></h1>
50 <p>This module provides functions to encode and decode strings into and from the
51 base64 encoding specified in RFC 2045 - <em>MIME (Multipurpose Internet
52 Mail Extensions)</em>. The base64 encoding is designed to represent
53 arbitrary sequences of octets in a form that need not be humanly
54 readable. A 65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used,
55 enabling 6 bits to be represented per printable character.</p>
56 <p>The following functions are provided:</p>
57 <dl>
58 <dt><strong><a name="item_encode_base64"><code>encode_base64($str)</code></a></strong>
60 <dt><strong>encode_base64($str, $eol);</strong>
62 <dd>
63 <p>Encode data by calling the <a href="#item_encode_base64"><code>encode_base64()</code></a> function. The first
64 argument is the string to encode. The second argument is the
65 line-ending sequence to use. It is optional and defaults to ``\n''. The
66 returned encoded string is broken into lines of no more than 76
67 characters each and it will end with $eol unless it is empty. Pass an
68 empty string as second argument if you do not want the encoded string
69 to be broken into lines.</p>
70 </dd>
71 </li>
72 <dt><strong><a name="item_decode_base64"><code>decode_base64($str)</code></a></strong>
74 <dd>
75 <p>Decode a base64 string by calling the <a href="#item_decode_base64"><code>decode_base64()</code></a> function. This
76 function takes a single argument which is the string to decode and
77 returns the decoded data.</p>
78 </dd>
79 <dd>
80 <p>Any character not part of the 65-character base64 subset is
81 silently ignored. Characters occurring after a '=' padding character
82 are never decoded.</p>
83 </dd>
84 <dd>
85 <p>If the length of the string to decode, after ignoring
86 non-base64 chars, is not a multiple of 4 or if padding occurs too early,
87 then a warning is generated if perl is running under <code>-w</code>.</p>
88 </dd>
89 </li>
90 </dl>
91 <p>If you prefer not to import these routines into your namespace, you can
92 call them as:</p>
93 <pre>
94 use MIME::Base64 ();
95 $encoded = MIME::Base64::encode($decoded);
96 $decoded = MIME::Base64::decode($encoded);</pre>
97 <p>
98 </p>
99 <hr />
100 <h1><a name="diagnostics">DIAGNOSTICS</a></h1>
101 <p>The following warnings can be generated if perl is invoked with the
102 <code>-w</code> switch:</p>
103 <dl>
104 <dt><strong><a name="item_premature_end_of_base64_data">Premature end of base64 data</a></strong>
106 <dd>
107 <p>The number of characters to decode is not a multiple of 4. Legal
108 base64 data should be padded with one or two ``='' characters to make
109 its length a multiple of 4. The decoded result will be the same
110 whether the padding is present or not.</p>
111 </dd>
112 </li>
113 <dt><strong><a name="item_premature_padding_of_base64_data">Premature padding of base64 data</a></strong>
115 <dd>
116 <p>The '=' padding character occurs as the first or second character
117 in a base64 quartet.</p>
118 </dd>
119 </li>
120 </dl>
121 <p>The following exception can be raised:</p>
122 <dl>
123 <dt><strong><a name="item_wide_character_in_subroutine_entry">Wide character in subroutine entry</a></strong>
125 <dd>
126 <p>The string passed to <a href="#item_encode_base64"><code>encode_base64()</code></a> contains characters with code
127 above 255. The base64 encoding is only defined for single-byte
128 characters. Use the Encode module to select the byte encoding you
129 want.</p>
130 </dd>
131 </li>
132 </dl>
134 </p>
135 <hr />
136 <h1><a name="examples">EXAMPLES</a></h1>
137 <p>If you want to encode a large file, you should encode it in chunks
138 that are a multiple of 57 bytes. This ensures that the base64 lines
139 line up and that you do not end up with padding in the middle. 57
140 bytes of data fills one complete base64 line (76 == 57*4/3):</p>
141 <pre>
142 use MIME::Base64 qw(encode_base64);</pre>
143 <pre>
144 open(FILE, &quot;/var/log/wtmp&quot;) or die &quot;$!&quot;;
145 while (read(FILE, $buf, 60*57)) {
146 print encode_base64($buf);
147 }</pre>
148 <p>or if you know you have enough memory</p>
149 <pre>
150 use MIME::Base64 qw(encode_base64);
151 local($/) = undef; # slurp
152 print encode_base64(&lt;STDIN&gt;);</pre>
153 <p>The same approach as a command line:</p>
154 <pre>
155 perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' &lt;file</pre>
156 <p>Decoding does not need slurp mode if every line contains a multiple
157 of four base64 chars:</p>
158 <pre>
159 perl -MMIME::Base64 -ne 'print decode_base64($_)' &lt;file</pre>
160 <p>Perl v5.8 and better allow extended Unicode characters in strings.
161 Such strings cannot be encoded directly, as the base64
162 encoding is only defined for single-byte characters. The solution is
163 to use the Encode module to select the byte encoding you want. For
164 example:</p>
165 <pre>
166 use MIME::Base64 qw(encode_base64);
167 use Encode qw(encode);</pre>
168 <pre>
169 $encoded = encode_base64(encode(&quot;UTF-8&quot;, &quot;\x{FFFF}\n&quot;));
170 print $encoded;</pre>
172 </p>
173 <hr />
174 <h1><a name="copyright">COPYRIGHT</a></h1>
175 <p>Copyright 1995-1999, 2001-2004 Gisle Aas.</p>
176 <p>This library is free software; you can redistribute it and/or
177 modify it under the same terms as Perl itself.</p>
178 <p>Distantly based on LWP::Base64 written by Martijn Koster
179 &lt;<a href="mailto:m.koster@nexor.co.uk">m.koster@nexor.co.uk</a>&gt; and Joerg Reichelt &lt;<a href="mailto:j.reichelt@nexor.co.uk">j.reichelt@nexor.co.uk</a>&gt; and
180 code posted to comp.lang.perl &lt;<a href="mailto:3pd2lp$6gf@wsinti07.win.tue.nl">3pd2lp$6gf@wsinti07.win.tue.nl</a>&gt; by Hans
181 Mulder &lt;<a href="mailto:hansm@wsinti07.win.tue.nl">hansm@wsinti07.win.tue.nl</a>&gt;</p>
182 <p>The XS implementation uses code from metamail. Copyright 1991 Bell
183 Communications Research, Inc. (Bellcore)</p>
185 </p>
186 <hr />
187 <h1><a name="see_also">SEE ALSO</a></h1>
188 <p><a href="file://C|\msysgit\mingw\html/lib/MIME/QuotedPrint.html">the MIME::QuotedPrint manpage</a></p>
189 <table border="0" width="100%" cellspacing="0" cellpadding="3">
190 <tr><td class="block" style="background-color: #cccccc" valign="middle">
191 <big><strong><span class="block">&nbsp;MIME::Base64 - Encoding and decoding of base64 strings</span></strong></big>
192 </td></tr>
193 </table>
195 </body>
197 </html>