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