MIME::Decoder

- NAME
- DESCRIPTION
- SYNOPSIS
- PUBLIC INTERFACE
- SUBCLASS INTERFACE
- PRIVATE BUILT-IN DECODERS
  - MIME::Decoder::Base64
  - MIME::Decoder::Binary
  - MIME::Decoder::QuotedPrint
  - MIME::Decoder::Xbit
- WRITING A DECODER
- SEE ALSO
- AUTHOR
- VERSION

# NAME

MIME::Decoder - an object for decoding the body part of a MIME stream

WARNING: This code is in an evaluation phase until 1 August 1996. Depending on any comments/complaints received before this cutoff date, the interface may change in a non-backwards-compatible manner.

# DESCRIPTION

This abstract class, and its private concrete subclasses (see below) provide an OO front end to the action of decoding a MIME-encoded stream.

The constructor for MIME::Decoder takes the name of an encoding (`base64`, `7bit`, etc.), and returns an instance of a subclass of MIME::Decoder whose `decode()` method will perform the appropriate decoding action.

You can even create your own subclasses and ``install'' them so that MIME::Decoder will know about them, via the `install()` method

Want to know if a given encoding is currently supported? Use the `supported()` class method.

# SYNOPSIS

Here's a simple filter program to read quoted-printable data from STDIN and write the decoded data to STDOUT:

        #!/usr/bin/perl
        use MIME::Decoder;

        $encoding = 'quoted-printable';

        $decoder = new MIME::Decoder $encoding or die "$encoding unsupported";
        $decoder->decode(\*STDIN, \*STDOUT);

The `decode()` method will always eat up all input to the end of file.

# PUBLIC INTERFACE

If all you are doing is using this class, here's all you'll need...

new ENCODING
  Class method . 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.

decode INSTREAM,OUTSTREAM
  Decode the document waiting in the MIME input filehandle INSTREAM, writing the decoded information to the output filehandle OUTSTREAM.

supported [ENCODING]
  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 (MIME::Decoder->supported('7BIT')) {
              # yes, we can handle it...
          }
          else {
              # drop back six and punt...
          }

  With no args, returns all the available decoders as a hash reference... where the key is the encoding name (all lowercase, like '7bit'), and the associated value is true (it happens to be the name of the class that handles the decoding, but you probably shouldn't rely on that). Hence:

          my $supported = MIME::Decoder->supported;
          if ($supported->{7bit}) {
              # yes, we can handle it...
          }
          elsif ($supported->{8bit}) {
              # yes, we can handle it...
          }

  You may safely modify this hash; it will not change the way the module performs its lookups. Only `install` can do that.

  Thanks to Achim Bohnet for suggesting this method.

# SUBCLASS INTERFACE

If you are writing/installing a new decoder, here's some other stuff you'll need:

install ENCODING
  Class method . Install this class so that ENCODING is handled by it.

# PRIVATE BUILT-IN DECODERS

You don't need to `"use"` any other Perl modules; the following are included as part of MIME::Decoder.

## MIME::Decoder::Base64

The built-in decoder for the `"base64"` encoding.

The name was chosen to jibe with the pre-existing MIME::Base64 utility package, which this class actually uses to translate each line.

## MIME::Decoder::Binary

The built-in decoder for a `"binary"` encoding (in other words, no encoding).

The `"binary"` decoder is a special case, since it's ill-advised to read the input line-by-line: after all, an uncompressed image file might conceivably have loooooooooong stretches of bytes without a `"\n"` among them, and we don't want to risk blowing out our core. So, we read-and-write fixed-size chunks.

## MIME::Decoder::QuotedPrint

The built-in decoder the for `"quoted-printable"` encoding.

The name was chosen to jibe with the pre-existing MIME::QuotedPrint utility package, which this class actually uses to translate each line.

## MIME::Decoder::Xbit

The built-in decoder for both `"7bit"` and `"8bit"` encodings, which guarantee short lines (a maximum of 1000 characters per line) of US-ASCII data compatible with RFC-821.

This decoder does a line-by-line pass-through from input to output, leaving the data unchanged except that an end-of-line sequence of CRLF is converted to a newline ``\n''.

# WRITING A DECODER

If you're experimenting with your own encodings, you'll probably want to write a decoder. Here are the basics:

1. Create a module, like ``MyDecoder::'', for your decoder. Declare it to be a subclass of MIME::Decoder.

2. Create the instance method `MyDecoder::decode_it()`, as follows:

   Your method should take as arguments the `$self` object (natch), a filehandle opened for input, called `$in`, and a filehandle opened for output, called `$out`.

   Your method should read from the input filehandle, decode this input, and print its decoded output to the `$out` filehandle. You may do this however you see fit, so long as the end result is the same.

   Your method must return either `undef` (to indicate failure), or `1` (to indicate success).

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"

To illustrate, here's a custom decoder class for the `base64` encoding:

        package MyBase64Decoder;

        @ISA = qw(MIME::Decoder);
        use MIME::Decoder;
        use MIME::Base64;

        # decode_it - the private decoding method
        sub decode_it {
            my ($self, $in, $out) = @_;

            while (<$in>) {
                my $decoded = decode_base64($_);
    	    print $out $decoded;
            }
            1;
        }

That's it.

The task was pretty simple because the `"base64"` encoding can easily and efficiently be parsed line-by-line... as can `"quoted-printable"`, and even `"7bit"` and `"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 `"binary"` decoder, however, really required block reads and writes: see MIME/Decoder::Binary for details.

# SEE ALSO

MIME::Decoder, MIME::Entity, MIME::Head, MIME::Parser.

# AUTHOR

Copyright (c) 1996 by Eryq / eryq@rhine.gsfc.nasa.gov

All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

# VERSION

$Revision: 1.9 $ $Date: 1996/06/24 19:02:31 $