Commit | Line | Data |
---|---|---|
6fba102d JH |
1 | package MIME::Base64; |
2 | ||
0a362e9d RGS |
3 | use strict; |
4 | use vars qw(@ISA @EXPORT $VERSION); | |
5 | ||
6 | require 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 |
12 | require XSLoader; |
13 | XSLoader::load('MIME::Base64', $VERSION); | |
0a362e9d RGS |
14 | |
15 | *encode = \&encode_base64; | |
16 | *decode = \&decode_base64; | |
17 | ||
18 | 1; | |
19 | ||
20 | __END__ | |
21 | ||
6fba102d JH |
22 | =head1 NAME |
23 | ||
24 | MIME::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 |
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 | |
6fba102d JH |
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. | |
41 | ||
42 | The 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 | |
50 | Encode data by calling the encode_base64() function. The first | |
691d66bd RGS |
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 | |
6fba102d JH |
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 | |
691d66bd | 56 | to be broken into lines. |
6fba102d JH |
57 | |
58 | =item decode_base64($str) | |
59 | ||
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. | |
63 | ||
691d66bd RGS |
64 | Any character not part of the 65-character base64 subset is |
65 | silently ignored. Characters occurring after a '=' padding character | |
6fba102d JH |
66 | are never decoded. |
67 | ||
8be5f608 | 68 | If the length of the string to decode, after ignoring |
691d66bd | 69 | non-base64 chars, is not a multiple of 4 or if padding occurs too early, |
6fba102d JH |
70 | then a warning is generated if perl is running under C<-w>. |
71 | ||
72 | =back | |
73 | ||
691d66bd | 74 | If you prefer not to import these routines into your namespace, you can |
6fba102d JH |
75 | call 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 | 83 | The following warnings can be generated if perl is invoked with the |
6fba102d JH |
84 | C<-w> switch: |
85 | ||
86 | =over 4 | |
87 | ||
88 | =item Premature end of base64 data | |
89 | ||
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 | |
4e2e3d15 SP |
92 | its length a multiple of 4. The decoded result will be the same |
93 | whether the padding is present or not. | |
6fba102d JH |
94 | |
95 | =item Premature padding of base64 data | |
96 | ||
97 | The '=' padding character occurs as the first or second character | |
98 | in a base64 quartet. | |
99 | ||
100 | =back | |
101 | ||
e1839706 SP |
102 | The following exception can be raised: |
103 | ||
104 | =over 4 | |
105 | ||
106 | =item Wide character in subroutine entry | |
107 | ||
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 | |
111 | want. | |
112 | ||
113 | =back | |
114 | ||
6fba102d JH |
115 | =head1 EXAMPLES |
116 | ||
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): | |
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 | ||
129 | or 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 | ||
135 | The same approach as a command line: | |
136 | ||
137 | perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' <file | |
138 | ||
691d66bd RGS |
139 | Decoding does not need slurp mode if every line contains a multiple |
140 | of four base64 chars: | |
6fba102d JH |
141 | |
142 | perl -MMIME::Base64 -ne 'print decode_base64($_)' <file | |
143 | ||
e1839706 SP |
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 | |
148 | example: | |
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 | 158 | Copyright 1995-1999, 2001-2004 Gisle Aas. |
6fba102d JH |
159 | |
160 | This library is free software; you can redistribute it and/or | |
161 | modify it under the same terms as Perl itself. | |
162 | ||
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> | |
167 | ||
691d66bd | 168 | The XS implementation uses code from metamail. Copyright 1991 Bell |
6fba102d JH |
169 | Communications Research, Inc. (Bellcore) |
170 | ||
8be5f608 RGS |
171 | =head1 SEE ALSO |
172 | ||
173 | L<MIME::QuotedPrint> | |
174 | ||
6fba102d | 175 | =cut |