package MIME::Decoder::Base64;
=head1 NAME
MIME::Decoder::Base64 - encode/decode a "base64" stream
=head1 SYNOPSIS
A generic decoder object; see L<MIME::Decoder> for usage.
=head1 DESCRIPTION
A L<MIME::Decoder> subclass for the C<"base64"> encoding.
The name was chosen to jibe with the pre-existing MIME::Base64
utility package, which this class actually uses to translate each chunk.
=over 4
=item *
When B<decoding>, the input is read one line at a time.
The input accumulates in an internal buffer, which is decoded in
multiple-of-4-sized chunks (plus a possible "leftover" input chunk,
of course).
=item *
When B<encoding>, the input is read 45 bytes at a time: this ensures
that the output lines are not too long. We chose 45 since it is
a multiple of 3 and produces lines under 76 characters, as RFC-1521
specifies.
=back
=head1 AUTHOR
Eryq (F<eryq@zeegee.com>), ZeeGee Software Inc (F<http://www.zeegee.com>).
All rights reserved. This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
=head1 VERSION
$Revision: 1.1 $ $Date: 2004/04/09 17:04:46 $
=cut
use vars qw(@ISA $VERSION);
use MIME::Decoder;
use MIME::Base64 2.04;
use MIME::Tools qw(debug);
@ISA = qw(MIME::Decoder);
$VERSION = substr q$Revision: 1.1 $, 10;
my $EncodeChunkLength = 45;
my $DecodeChunkLength = 32 * 1024;
sub decode_it {
my ($self, $in, $out) = @_;
my $len_4xN;
my $buffer = ' ' x (120 + $DecodeChunkLength); $buffer = '';
debug "in = $in; out = $out";
local($_) = ' ' x $DecodeChunkLength;
while ($in->read($_, $DecodeChunkLength)) {
tr{A-Za-z0-9+/}{}cd;
$buffer .= $_;
length($buffer) >= $DecodeChunkLength or next;
$len_4xN = length($buffer) & ~3;
$out->print(decode_base64(substr($buffer, 0, $len_4xN)));
$buffer = substr($buffer, $len_4xN);
}
if (length($buffer)) {
$buffer .= "==="; $len_4xN = length($buffer) & ~3;
$out->print(decode_base64(substr($buffer, 0, $len_4xN)));
}
1;
}
sub encode_it {
my ($self, $in, $out) = @_;
my $encoded;
my $nread;
my $buf = '';
while ($nread = $in->read($buf, $EncodeChunkLength)) {
$encoded = encode_base64($buf);
$encoded .= "\n" unless ($encoded =~ /\n\Z/); $out->print($encoded);
}
1;
}
1;