1N/Apackage MIME::Base64;
1N/A
1N/A# $Id: Base64.pm,v 3.1 2004/03/29 11:55:49 gisle Exp $
1N/A
1N/Ause strict;
1N/Ause vars qw(@ISA @EXPORT $VERSION);
1N/A
1N/Arequire Exporter;
1N/Arequire DynaLoader;
1N/A@ISA = qw(Exporter DynaLoader);
1N/A@EXPORT = qw(encode_base64 decode_base64);
1N/A
1N/A$VERSION = '3.01';
1N/A
1N/AMIME::Base64->bootstrap($VERSION);
1N/A
1N/A*encode = \&encode_base64;
1N/A*decode = \&decode_base64;
1N/A
1N/A1;
1N/A
1N/A__END__
1N/A
1N/A=head1 NAME
1N/A
1N/AMIME::Base64 - Encoding and decoding of base64 strings
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/A use MIME::Base64;
1N/A
1N/A $encoded = encode_base64('Aladdin:open sesame');
1N/A $decoded = decode_base64($encoded);
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AThis module provides functions to encode and decode strings into and from the
1N/Abase64 encoding specified in RFC 2045 - I<MIME (Multipurpose Internet
1N/AMail Extensions)>. The base64 encoding is designed to represent
1N/Aarbitrary sequences of octets in a form that need not be humanly
1N/Areadable. A 65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used,
1N/Aenabling 6 bits to be represented per printable character.
1N/A
1N/AThe following functions are provided:
1N/A
1N/A=over 4
1N/A
1N/A=item encode_base64($str)
1N/A
1N/A=item encode_base64($str, $eol);
1N/A
1N/AEncode data by calling the encode_base64() function. The first
1N/Aargument is the string to encode. The second argument is the
1N/Aline-ending sequence to use. It is optional and defaults to "\n". The
1N/Areturned encoded string is broken into lines of no more than 76
1N/Acharacters each and it will end with $eol unless it is empty. Pass an
1N/Aempty string as second argument if you do not want the encoded string
1N/Ato be broken into lines.
1N/A
1N/A=item decode_base64($str)
1N/A
1N/ADecode a base64 string by calling the decode_base64() function. This
1N/Afunction takes a single argument which is the string to decode and
1N/Areturns the decoded data.
1N/A
1N/AAny character not part of the 65-character base64 subset is
1N/Asilently ignored. Characters occurring after a '=' padding character
1N/Aare never decoded.
1N/A
1N/AIf the length of the string to decode, after ignoring
1N/Anon-base64 chars, is not a multiple of 4 or if padding occurs too early,
1N/Athen a warning is generated if perl is running under C<-w>.
1N/A
1N/A=back
1N/A
1N/AIf you prefer not to import these routines into your namespace, you can
1N/Acall them as:
1N/A
1N/A use MIME::Base64 ();
1N/A $encoded = MIME::Base64::encode($decoded);
1N/A $decoded = MIME::Base64::decode($encoded);
1N/A
1N/A=head1 DIAGNOSTICS
1N/A
1N/AThe following warnings can be generated if perl is invoked with the
1N/AC<-w> switch:
1N/A
1N/A=over 4
1N/A
1N/A=item Premature end of base64 data
1N/A
1N/AThe number of characters to decode is not a multiple of 4. Legal
1N/Abase64 data should be padded with one or two "=" characters to make
1N/Aits length a multiple of 4. The decoded result will anyway be as if
1N/Athe padding was there.
1N/A
1N/A=item Premature padding of base64 data
1N/A
1N/AThe '=' padding character occurs as the first or second character
1N/Ain a base64 quartet.
1N/A
1N/A=back
1N/A
1N/A=head1 EXAMPLES
1N/A
1N/AIf you want to encode a large file, you should encode it in chunks
1N/Athat are a multiple of 57 bytes. This ensures that the base64 lines
1N/Aline up and that you do not end up with padding in the middle. 57
1N/Abytes of data fills one complete base64 line (76 == 57*4/3):
1N/A
1N/A use MIME::Base64 qw(encode_base64);
1N/A
1N/A open(FILE, "/var/log/wtmp") or die "$!";
1N/A while (read(FILE, $buf, 60*57)) {
1N/A print encode_base64($buf);
1N/A }
1N/A
1N/Aor if you know you have enough memory
1N/A
1N/A use MIME::Base64 qw(encode_base64);
1N/A local($/) = undef; # slurp
1N/A print encode_base64(<STDIN>);
1N/A
1N/AThe same approach as a command line:
1N/A
1N/A perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' <file
1N/A
1N/ADecoding does not need slurp mode if every line contains a multiple
1N/Aof four base64 chars:
1N/A
1N/A perl -MMIME::Base64 -ne 'print decode_base64($_)' <file
1N/A
1N/A=head1 COPYRIGHT
1N/A
1N/ACopyright 1995-1999, 2001-2004 Gisle Aas.
1N/A
1N/AThis library is free software; you can redistribute it and/or
1N/Amodify it under the same terms as Perl itself.
1N/A
1N/ADistantly based on LWP::Base64 written by Martijn Koster
1N/A<m.koster@nexor.co.uk> and Joerg Reichelt <j.reichelt@nexor.co.uk> and
1N/Acode posted to comp.lang.perl <3pd2lp$6gf@wsinti07.win.tue.nl> by Hans
1N/AMulder <hansm@wsinti07.win.tue.nl>
1N/A
1N/AThe XS implementation uses code from metamail. Copyright 1991 Bell
1N/ACommunications Research, Inc. (Bellcore)
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/AL<MIME::QuotedPrint>
1N/A
1N/A=cut