/usr/share/perl5/MIME/Decoder.pm is in libmime-tools-perl 5.503-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 | package MIME::Decoder;
=head1 NAME
MIME::Decoder - an object for decoding the body part of a MIME stream
=head1 SYNOPSIS
Before reading further, you should see L<MIME::Tools> to make sure that
you understand where this module fits into the grand scheme of things.
Go on, do it now. I'll wait.
Ready? Ok...
=head2 Decoding a data stream
Here's a simple filter program to read quoted-printable data from STDIN
(until EOF) and write the decoded data to STDOUT:
use MIME::Decoder;
$decoder = new MIME::Decoder 'quoted-printable' or die "unsupported";
$decoder->decode(\*STDIN, \*STDOUT);
=head2 Encoding a data stream
Here's a simple filter program to read binary data from STDIN
(until EOF) and write base64-encoded data to STDOUT:
use MIME::Decoder;
$decoder = new MIME::Decoder 'base64' or die "unsupported";
$decoder->encode(\*STDIN, \*STDOUT);
=head2 Non-standard encodings
You can B<write and install> your own decoders so that
MIME::Decoder will know about them:
use MyBase64Decoder;
install MyBase64Decoder 'base64';
You can also B<test> if a given encoding is supported:
if (supported MIME::Decoder 'x-uuencode') {
### we can uuencode!
}
=head1 DESCRIPTION
This abstract class, and its private concrete subclasses (see below)
provide an OO front end to the actions of...
=over 4
=item *
Decoding a MIME-encoded stream
=item *
Encoding a raw data stream into a MIME-encoded stream.
=back
The constructor for MIME::Decoder takes the name of an encoding
(C<base64>, C<7bit>, etc.), and returns an instance of a I<subclass>
of MIME::Decoder whose C<decode()> method will perform the appropriate
decoding action, and whose C<encode()> method will perform the appropriate
encoding action.
=cut
### Pragmas:
use strict;
use vars qw($VERSION %DecoderFor);
### System modules:
use IPC::Open2;
use IO::Select;
use FileHandle;
### Kit modules:
use MIME::Tools qw(:config :msgs);
use Carp;
#------------------------------
#
# Globals
#
#------------------------------
### The stream decoders:
%DecoderFor = (
### Standard...
'7bit' => 'MIME::Decoder::NBit',
'8bit' => 'MIME::Decoder::NBit',
'base64' => 'MIME::Decoder::Base64',
'binary' => 'MIME::Decoder::Binary',
'none' => 'MIME::Decoder::Binary',
'quoted-printable' => 'MIME::Decoder::QuotedPrint',
### Non-standard...
'binhex' => 'MIME::Decoder::BinHex',
'binhex40' => 'MIME::Decoder::BinHex',
'mac-binhex40' => 'MIME::Decoder::BinHex',
'mac-binhex' => 'MIME::Decoder::BinHex',
'x-uu' => 'MIME::Decoder::UU',
'x-uuencode' => 'MIME::Decoder::UU',
### This was removed, since I fear that x-gzip != x-gzip64...
### 'x-gzip' => 'MIME::Decoder::Gzip64',
### This is no longer installed by default, since not all folks have gzip:
### 'x-gzip64' => 'MIME::Decoder::Gzip64',
);
### The package version, both in 1.23 style *and* usable by MakeMaker:
$VERSION = "5.503";
### Me:
my $ME = 'MIME::Decoder';
#------------------------------
=head1 PUBLIC INTERFACE
=head2 Standard interface
If all you are doing is I<using> this class, here's all you'll need...
=over 4
=cut
#------------------------------
=item new ENCODING
I<Class method, constructor.>
Create and return a new decoder object which can handle the
given ENCODING.
my $decoder = new MIME::Decoder "7bit";
Returns the undefined value if no known decoders are appropriate.
=cut
sub new {
my ($class, @args) = @_;
my ($encoding) = @args;
### Coerce the type to be legit:
$encoding = lc($encoding || '');
### Get the class:
my $concrete_name = $DecoderFor{$encoding};
if( ! $concrete_name ) {
carp "no decoder for $encoding";
return undef;
}
### Create the new object (if we can):
my $self = { MD_Encoding => lc($encoding) };
unless (eval "require $concrete_name;") {
carp $@;
return undef;
}
bless $self, $concrete_name;
$self->init(@args);
}
#------------------------------
=item best ENCODING
I<Class method, constructor.>
Exactly like new(), except that this defaults any unsupported encoding to
"binary", after raising a suitable warning (it's a fatal error if there's
no binary decoder).
my $decoder = best MIME::Decoder "x-gzip64";
Will either return a decoder, or a raise a fatal exception.
=cut
sub best {
my ($class, $enc, @args) = @_;
my $self = $class->new($enc, @args);
if (!$self) {
usage "unsupported encoding '$enc': using 'binary'";
$self = $class->new('binary') || croak "ack! no binary decoder!";
}
$self;
}
#------------------------------
=item decode INSTREAM,OUTSTREAM
I<Instance method.>
Decode the document waiting in the input handle INSTREAM,
writing the decoded information to the output handle OUTSTREAM.
Read the section in this document on I/O handles for more information
about the arguments. Note that you can still supply old-style
unblessed filehandles for INSTREAM and OUTSTREAM.
Returns true on success, throws exception on failure.
=cut
sub decode {
my ($self, $in, $out) = @_;
### Set up the default input record separator to be CRLF:
### $in->input_record_separator("\012\015");
### Invoke back-end method to do the work:
$self->decode_it($in, $out) ||
die "$ME: ".$self->encoding." decoding failed\n";
1;
}
#------------------------------
=item encode INSTREAM,OUTSTREAM
I<Instance method.>
Encode the document waiting in the input filehandle INSTREAM,
writing the encoded information to the output stream OUTSTREAM.
Read the section in this document on I/O handles for more information
about the arguments. Note that you can still supply old-style
unblessed filehandles for INSTREAM and OUTSTREAM.
Returns true on success, throws exception on failure.
=cut
sub encode {
my ($self, $in, $out, $textual_type) = @_;
### Invoke back-end method to do the work:
$self->encode_it($in, $out, $self->encoding eq 'quoted-printable' ? ($textual_type) : ()) ||
die "$ME: ".$self->encoding." encoding failed\n";
}
#------------------------------
=item encoding
I<Instance method.>
Return the encoding that this object was created to handle,
coerced to all lowercase (e.g., C<"base64">).
=cut
sub encoding {
shift->{MD_Encoding};
}
#------------------------------
=item head [HEAD]
I<Instance method.>
Completely optional: some decoders need to know a little about the file
they are encoding/decoding; e.g., x-uu likes to have the filename.
The HEAD is any object which responds to messages like:
$head->mime_attr('content-disposition.filename');
=cut
sub head {
my ($self, $head) = @_;
$self->{MD_Head} = $head if @_ > 1;
$self->{MD_Head};
}
#------------------------------
=item supported [ENCODING]
I<Class method.>
With one arg (an ENCODING name), returns truth if that encoding
is currently handled, and falsity otherwise. The ENCODING will
be automatically coerced to lowercase:
if (supported MIME::Decoder '7BIT') {
### yes, we can handle it...
}
else {
### drop back six and punt...
}
With no args, returns a reference to a hash of all available decoders,
where the key is the encoding name (all lowercase, like '7bit'),
and the value is true (it happens to be the name of the class
that handles the decoding, but you probably shouldn't rely on that).
You may safely modify this hash; it will I<not> change the way the
module performs its lookups. Only C<install> can do that.
I<Thanks to Achim Bohnet for suggesting this method.>
=cut
sub supported {
my ($class, $decoder) = @_;
defined($decoder) ? $DecoderFor{lc($decoder)}: { %DecoderFor };
}
#------------------------------
=back
=head2 Subclass interface
If you are writing (or installing) a new decoder subclass, there
are some other methods you'll need to know about:
=over 4
=item decode_it INSTREAM,OUTSTREAM
I<Abstract instance method.>
The back-end of the B<decode> method. It takes an input handle
opened for reading (INSTREAM), and an output handle opened for
writing (OUTSTREAM).
If you are writing your own decoder subclass, you must override this
method in your class. Your method should read from the input
handle via C<getline()> or C<read()>, decode this input, and print the
decoded data to the output handle via C<print()>. You may do this
however you see fit, so long as the end result is the same.
Note that unblessed references and globrefs are automatically turned
into I/O handles for you by C<decode()>, so you don't need to worry
about it.
Your method must return either C<undef> (to indicate failure),
or C<1> (to indicate success).
It may also throw an exception to indicate failure.
=cut
sub decode_it {
die "attempted to use abstract 'decode_it' method!";
}
=item encode_it INSTREAM,OUTSTREAM
I<Abstract instance method.>
The back-end of the B<encode> method. It takes an input handle
opened for reading (INSTREAM), and an output handle opened for
writing (OUTSTREAM).
If you are writing your own decoder subclass, you must override this
method in your class. Your method should read from the input
handle via C<getline()> or C<read()>, encode this input, and print the
encoded data to the output handle via C<print()>. You may do this
however you see fit, so long as the end result is the same.
Note that unblessed references and globrefs are automatically turned
into I/O handles for you by C<encode()>, so you don't need to worry
about it.
Your method must return either C<undef> (to indicate failure),
or C<1> (to indicate success).
It may also throw an exception to indicate failure.
=cut
sub encode_it {
die "attempted to use abstract 'encode_it' method!";
}
=item filter IN, OUT, COMMAND...
I<Class method, utility.>
If your decoder involves an external program, you can invoke
them easily through this method. The command must be a "filter": a
command that reads input from its STDIN (which will come from the IN argument)
and writes output to its STDOUT (which will go to the OUT argument).
For example, here's a decoder that un-gzips its data:
sub decode_it {
my ($self, $in, $out) = @_;
$self->filter($in, $out, "gzip -d -");
}
The usage is similar to IPC::Open2::open2 (which it uses internally),
so you can specify COMMAND as a single argument or as an array.
=cut
sub filter
{
my ($self, $in, $out, @cmd) = @_;
my $buf = '';
### Open pipe:
STDOUT->flush; ### very important, or else we get duplicate output!
my $kidpid = open2(my $child_out, my $child_in, @cmd) || die "@cmd: open2 failed: $!";
### We have to use select() for doing both reading and writing.
my $rsel = IO::Select->new( $child_out );
my $wsel = IO::Select->new( $child_in );
while (1) {
### Wait for one hour; if that fails, it's too bad.
my ($read, $write) = IO::Select->select( $rsel, $wsel, undef, 3600);
if( !defined $read && !defined $write ) {
kill 1, $kidpid;
waitpid $kidpid, 0;
die "@cmd: select failed: $!";
}
### If can read from child:
if( my $fh = shift @$read ) {
if( $fh->sysread(my $buf, 1024) ) {
$out->print($buf);
} else {
$rsel->remove($fh);
$fh->close();
}
}
### If can write to child:
if( my $fh = shift @$write ) {
if($in->read(my $buf, 1024)) {
local $SIG{PIPE} = sub {
warn "got SIGPIPE from @cmd";
$wsel->remove($fh);
$fh->close();
};
$fh->syswrite( $buf );
} else {
$wsel->remove($fh);
$fh->close();
}
}
### If both $child_out and $child_in are done:
last unless ($rsel->count() || $wsel->count());
}
### Wait for it:
waitpid($kidpid, 0) == $kidpid or die "@cmd: couldn't reap child $kidpid";
### Check if it failed:
$? == 0 or die "@cmd: bad exit status: \$? = $?";
1;
}
#------------------------------
=item init ARGS...
I<Instance method.>
Do any necessary initialization of the new instance,
taking whatever arguments were given to C<new()>.
Should return the self object on success, undef on failure.
=cut
sub init {
$_[0];
}
#------------------------------
=item install ENCODINGS...
I<Class method>.
Install this class so that each encoding in ENCODINGS is handled by it:
install MyBase64Decoder 'base64', 'x-base64super';
You should not override this method.
=cut
sub install {
my $class = shift;
$DecoderFor{lc(shift @_)} = $class while (@_);
}
#------------------------------
=item uninstall ENCODINGS...
I<Class method>.
Uninstall support for encodings. This is a way to turn off the decoding
of "experimental" encodings. For safety, always use MIME::Decoder directly:
uninstall MIME::Decoder 'x-uu', 'x-uuencode';
You should not override this method.
=cut
sub uninstall {
shift;
$DecoderFor{lc(shift @_)} = undef while (@_);
}
1;
__END__
#------------------------------
=back
=head1 DECODER SUBCLASSES
You don't need to C<"use"> any other Perl modules; the
following "standard" subclasses are included as part of MIME::Decoder:
Class: Handles encodings:
------------------------------------------------------------
MIME::Decoder::Binary binary
MIME::Decoder::NBit 7bit, 8bit
MIME::Decoder::Base64 base64
MIME::Decoder::QuotedPrint quoted-printable
The following "non-standard" subclasses are also included:
Class: Handles encodings:
------------------------------------------------------------
MIME::Decoder::UU x-uu, x-uuencode
MIME::Decoder::Gzip64 x-gzip64 ** requires gzip!
=head1 NOTES
=head2 Input/Output handles
As of MIME-tools 2.0, this class has to play nice with the new MIME::Body
class... which means that input and output routines cannot just assume that
they are dealing with filehandles.
Therefore, all that MIME::Decoder and its subclasses require (and, thus,
all that they can assume) is that INSTREAMs and OUTSTREAMs are objects
which respond to a subset of the messages defined in the IO::Handle
interface; minimally:
print
getline
read(BUF,NBYTES)
I<Thanks to Achim Bohnet for suggesting this more-generic I/O model.>
=head2 Writing a decoder
If you're experimenting with your own encodings, you'll probably want
to write a decoder. Here are the basics:
=over 4
=item 1.
Create a module, like "MyDecoder::", for your decoder.
Declare it to be a subclass of MIME::Decoder.
=item 2.
Create the following instance methods in your class, as described above:
decode_it
encode_it
init
=item 3.
In your application program, activate your decoder for one or
more encodings like this:
require MyDecoder;
install MyDecoder "7bit"; ### use MyDecoder to decode "7bit"
install MyDecoder "x-foo"; ### also use MyDecoder to decode "x-foo"
=back
To illustrate, here's a custom decoder class for the C<quoted-printable>
encoding:
package MyQPDecoder;
@ISA = qw(MIME::Decoder);
use MIME::Decoder;
use MIME::QuotedPrint;
### decode_it - the private decoding method
sub decode_it {
my ($self, $in, $out) = @_;
local $_;
while (defined($_ = $in->getline)) {
my $decoded = decode_qp($_);
$out->print($decoded);
}
1;
}
### encode_it - the private encoding method
sub encode_it {
my ($self, $in, $out) = @_;
my ($buf, $nread) = ('', 0);
while ($in->read($buf, 60)) {
my $encoded = encode_qp($buf);
$out->print($encoded);
}
1;
}
That's it. The task was pretty simple because the C<"quoted-printable">
encoding can easily be converted line-by-line... as can
even C<"7bit"> and C<"8bit"> (since all these encodings guarantee
short lines, with a max of 1000 characters).
The good news is: it is very likely that it will be similarly-easy to
write a MIME::Decoder for any future standard encodings.
The C<"binary"> decoder, however, really required block reads and writes:
see L<"MIME::Decoder::Binary"> for details.
=head1 SEE ALSO
L<MIME::Tools>, other MIME::Decoder subclasses.
=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.
1;
|