xref: /openbsd-src/gnu/usr.bin/perl/cpan/MIME-Base64/lib/MIME/Base64.pm (revision f2a19305cfc49ea4d1a5feb55cd6c283c6f1e031)
1256a93a4Safresh1package MIME::Base64;
2256a93a4Safresh1
3256a93a4Safresh1use strict;
4256a93a4Safresh1use warnings;
5256a93a4Safresh1
6256a93a4Safresh1require Exporter;
7256a93a4Safresh1our @ISA = qw(Exporter);
8256a93a4Safresh1our @EXPORT = qw(encode_base64 decode_base64);
9256a93a4Safresh1our @EXPORT_OK = qw(encode_base64url decode_base64url encoded_base64_length decoded_base64_length);
10256a93a4Safresh1
11*f2a19305Safresh1our $VERSION = '3.16_01';
12256a93a4Safresh1
13256a93a4Safresh1require XSLoader;
14256a93a4Safresh1XSLoader::load('MIME::Base64', $VERSION);
15256a93a4Safresh1
16256a93a4Safresh1*encode = \&encode_base64;
17256a93a4Safresh1*decode = \&decode_base64;
18256a93a4Safresh1
19256a93a4Safresh1sub encode_base64url {
20256a93a4Safresh1    my $e = encode_base64(shift, "");
21256a93a4Safresh1    $e =~ s/=+\z//;
22256a93a4Safresh1    $e =~ tr[+/][-_];
23256a93a4Safresh1    return $e;
24256a93a4Safresh1}
25256a93a4Safresh1
26256a93a4Safresh1sub decode_base64url {
27256a93a4Safresh1    my $s = shift;
28256a93a4Safresh1    $s =~ tr[-_][+/];
29256a93a4Safresh1    $s .= '=' while length($s) % 4;
30256a93a4Safresh1    return decode_base64($s);
31256a93a4Safresh1}
32256a93a4Safresh1
33256a93a4Safresh11;
34256a93a4Safresh1
35256a93a4Safresh1__END__
36256a93a4Safresh1
37256a93a4Safresh1=head1 NAME
38256a93a4Safresh1
39256a93a4Safresh1MIME::Base64 - Encoding and decoding of base64 strings
40256a93a4Safresh1
41256a93a4Safresh1=head1 SYNOPSIS
42256a93a4Safresh1
43256a93a4Safresh1 use MIME::Base64;
44256a93a4Safresh1
45256a93a4Safresh1 $encoded = encode_base64('Aladdin:open sesame');
46256a93a4Safresh1 $decoded = decode_base64($encoded);
47256a93a4Safresh1
48256a93a4Safresh1=head1 DESCRIPTION
49256a93a4Safresh1
50256a93a4Safresh1This module provides functions to encode and decode strings into and from the
51256a93a4Safresh1base64 encoding specified in RFC 2045 - I<MIME (Multipurpose Internet
52256a93a4Safresh1Mail Extensions)>. The base64 encoding is designed to represent
53256a93a4Safresh1arbitrary sequences of octets in a form that need not be humanly
54256a93a4Safresh1readable. A 65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used,
55256a93a4Safresh1enabling 6 bits to be represented per printable character.
56256a93a4Safresh1
57256a93a4Safresh1The following primary functions are provided:
58256a93a4Safresh1
59256a93a4Safresh1=over 4
60256a93a4Safresh1
61256a93a4Safresh1=item encode_base64( $bytes )
62256a93a4Safresh1
63256a93a4Safresh1=item encode_base64( $bytes, $eol );
64256a93a4Safresh1
65256a93a4Safresh1Encode data by calling the encode_base64() function.  The first
66256a93a4Safresh1argument is the byte string to encode.  The second argument is the
67256a93a4Safresh1line-ending sequence to use.  It is optional and defaults to "\n".  The
68256a93a4Safresh1returned encoded string is broken into lines of no more than 76
69256a93a4Safresh1characters each and it will end with $eol unless it is empty.  Pass an
70256a93a4Safresh1empty string as second argument if you do not want the encoded string
71256a93a4Safresh1to be broken into lines.
72256a93a4Safresh1
73256a93a4Safresh1The function will croak with "Wide character in subroutine entry" if $bytes
74256a93a4Safresh1contains characters with code above 255.  The base64 encoding is only defined
75256a93a4Safresh1for single-byte characters.  Use the Encode module to select the byte encoding
76256a93a4Safresh1you want.
77256a93a4Safresh1
78256a93a4Safresh1=item decode_base64( $str )
79256a93a4Safresh1
80256a93a4Safresh1Decode a base64 string by calling the decode_base64() function.  This
81256a93a4Safresh1function takes a single argument which is the string to decode and
82256a93a4Safresh1returns the decoded data.
83256a93a4Safresh1
84256a93a4Safresh1Any character not part of the 65-character base64 subset is
85256a93a4Safresh1silently ignored.  Characters occurring after a '=' padding character
86256a93a4Safresh1are never decoded.
87256a93a4Safresh1
88256a93a4Safresh1=back
89256a93a4Safresh1
90256a93a4Safresh1If you prefer not to import these routines into your namespace, you can
91256a93a4Safresh1call them as:
92256a93a4Safresh1
93256a93a4Safresh1    use MIME::Base64 ();
94256a93a4Safresh1    $encoded = MIME::Base64::encode($decoded);
95256a93a4Safresh1    $decoded = MIME::Base64::decode($encoded);
96256a93a4Safresh1
97256a93a4Safresh1Additional functions not exported by default:
98256a93a4Safresh1
99256a93a4Safresh1=over 4
100256a93a4Safresh1
101256a93a4Safresh1=item encode_base64url( $bytes )
102256a93a4Safresh1
103256a93a4Safresh1=item decode_base64url( $str )
104256a93a4Safresh1
105256a93a4Safresh1Encode and decode according to the base64 scheme for "URL applications" [1].
106256a93a4Safresh1This is a variant of the base64 encoding which does not use padding, does not
107256a93a4Safresh1break the string into multiple lines and use the characters "-" and "_" instead
108256a93a4Safresh1of "+" and "/" to avoid using reserved URL characters.
109256a93a4Safresh1
110256a93a4Safresh1=item encoded_base64_length( $bytes )
111256a93a4Safresh1
112256a93a4Safresh1=item encoded_base64_length( $bytes, $eol )
113256a93a4Safresh1
114256a93a4Safresh1Returns the length that the encoded string would have without actually
115256a93a4Safresh1encoding it.  This will return the same value as C<< length(encode_base64($bytes)) >>,
116256a93a4Safresh1but should be more efficient.
117256a93a4Safresh1
118256a93a4Safresh1=item decoded_base64_length( $str )
119256a93a4Safresh1
120256a93a4Safresh1Returns the length that the decoded string would have without actually
121256a93a4Safresh1decoding it.  This will return the same value as C<< length(decode_base64($str)) >>,
122256a93a4Safresh1but should be more efficient.
123256a93a4Safresh1
124256a93a4Safresh1=back
125256a93a4Safresh1
126256a93a4Safresh1=head1 EXAMPLES
127256a93a4Safresh1
128256a93a4Safresh1If you want to encode a large file, you should encode it in chunks
129256a93a4Safresh1that are a multiple of 57 bytes.  This ensures that the base64 lines
130256a93a4Safresh1line up and that you do not end up with padding in the middle. 57
131256a93a4Safresh1bytes of data fills one complete base64 line (76 == 57*4/3):
132256a93a4Safresh1
133256a93a4Safresh1   use MIME::Base64 qw(encode_base64);
134256a93a4Safresh1
135256a93a4Safresh1   open(FILE, "/var/log/wtmp") or die "$!";
136256a93a4Safresh1   while (read(FILE, $buf, 60*57)) {
137256a93a4Safresh1       print encode_base64($buf);
138256a93a4Safresh1   }
139256a93a4Safresh1
140256a93a4Safresh1or if you know you have enough memory
141256a93a4Safresh1
142256a93a4Safresh1   use MIME::Base64 qw(encode_base64);
143256a93a4Safresh1   local($/) = undef;  # slurp
144256a93a4Safresh1   print encode_base64(<STDIN>);
145256a93a4Safresh1
146256a93a4Safresh1The same approach as a command line:
147256a93a4Safresh1
148256a93a4Safresh1   perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' <file
149256a93a4Safresh1
150256a93a4Safresh1Decoding does not need slurp mode if every line contains a multiple
151256a93a4Safresh1of four base64 chars:
152256a93a4Safresh1
153256a93a4Safresh1   perl -MMIME::Base64 -ne 'print decode_base64($_)' <file
154256a93a4Safresh1
155256a93a4Safresh1Perl v5.8 and better allow extended Unicode characters in strings.
156256a93a4Safresh1Such strings cannot be encoded directly, as the base64
157256a93a4Safresh1encoding is only defined for single-byte characters.  The solution is
158256a93a4Safresh1to use the Encode module to select the byte encoding you want.  For
159256a93a4Safresh1example:
160256a93a4Safresh1
161256a93a4Safresh1    use MIME::Base64 qw(encode_base64);
162256a93a4Safresh1    use Encode qw(encode);
163256a93a4Safresh1
164256a93a4Safresh1    $encoded = encode_base64(encode("UTF-8", "\x{FFFF}\n"));
165256a93a4Safresh1    print $encoded;
166256a93a4Safresh1
167256a93a4Safresh1=head1 COPYRIGHT
168256a93a4Safresh1
169256a93a4Safresh1Copyright 1995-1999, 2001-2004, 2010 Gisle Aas.
170256a93a4Safresh1
171256a93a4Safresh1This library is free software; you can redistribute it and/or
172256a93a4Safresh1modify it under the same terms as Perl itself.
173256a93a4Safresh1
174256a93a4Safresh1Distantly based on LWP::Base64 written by Martijn Koster
175256a93a4Safresh1<m.koster@nexor.co.uk> and Joerg Reichelt <j.reichelt@nexor.co.uk> and
176256a93a4Safresh1code posted to comp.lang.perl <3pd2lp$6gf@wsinti07.win.tue.nl> by Hans
177256a93a4Safresh1Mulder <hansm@wsinti07.win.tue.nl>
178256a93a4Safresh1
179256a93a4Safresh1The XS implementation uses code from metamail.  Copyright 1991 Bell
180256a93a4Safresh1Communications Research, Inc. (Bellcore)
181256a93a4Safresh1
182256a93a4Safresh1=head1 SEE ALSO
183256a93a4Safresh1
184256a93a4Safresh1L<MIME::QuotedPrint>
185256a93a4Safresh1
186256a93a4Safresh1[1] L<http://en.wikipedia.org/wiki/Base64#URL_applications>
187256a93a4Safresh1
188256a93a4Safresh1=cut
189