File Coverage

blib/lib/Email/MIME/Encodings.pm
Criterion Covered Total %
statement 33 36 91.6
branch 11 14 78.5
condition 4 9 44.4
subroutine 9 9 100.0
pod 0 4 0.0
total 57 72 79.1


line stmt bran cond sub pod time code
1 1     1   599 use strict;
  1         2  
  1         25  
2 1     1   4 use warnings;
  1         2  
  1         40  
3             package Email::MIME::Encodings 1.317;
4             # ABSTRACT: A unified interface to MIME encoding and decoding
5              
6 1     1   353 use MIME::Base64 3.05;
  1         534  
  1         51  
7 1     1   351 use MIME::QuotedPrint 3.05;
  1         213  
  1         63  
8              
9 6     6 0 18 sub identity { $_[0] }
10              
11             for (qw(7bit 8bit binary)) {
12 1     1   5 no strict 'refs';
  1         2  
  1         300  
13             *{"encode_$_"} = *{"decode_$_"} = \&identity;
14             }
15              
16             sub codec {
17 11     11 0 25 my ($which, $how, $what, $fb) = @_;
18 11         17 $how = lc $how;
19 11 100 66     43 $how = "qp" if $how eq "quotedprint" or $how eq "quoted-printable";
20 11         56 my $sub = __PACKAGE__->can("$which\_$how");
21              
22 11 50 66     29 if (! $sub && $fb) {
23 0         0 $fb = lc $fb;
24 0 0 0     0 $fb = "qp" if $fb eq "quotedprint" or $fb eq "quoted-printable";
25 0         0 $sub = __PACKAGE__->can("$which\_$fb");
26             }
27              
28 11 100       16 unless ($sub) {
29 1         6 require Carp;
30 1         163 Carp::croak("Don't know how to $which $how");
31             }
32              
33             # RFC2822 requires all email lines to end in CRLF. The Quoted-Printable
34             # RFC requires CRLF to not be encoded, when representing newlines. We will
35             # assume, in this code, that QP is being used for plain text and not binary
36             # data. This may, someday, be wrong -- but if you are using QP to encode
37             # binary data, you are already doing something bizarre.
38             #
39             # The only way to achieve this with MIME::QuotedPrint is to replace all
40             # CRLFs with just LF and then let MIME::QuotedPrint replace all LFs with
41             # CRLF. Otherwise MIME::QuotedPrint (by default) encodes CR as =0D, which
42             # is against RFCs and breaks MUAs (such as Thunderbird).
43             #
44             # We don't modify data before Base64 encoding it because that is usually
45             # binary data and modifying it at all is a bad idea. We do however specify
46             # that the encoder should end lines with CRLF (since that's the email
47             # standard).
48             # -- rjbs and mkanat, 2009-04-16
49 10         16 my $eol = "\x0d\x0a";
50 10 100       16 if ($which eq 'encode') {
51 5 100       21 $what =~ s/$eol/\x0a/sg if $how eq 'qp';
52 5         18 return $sub->($what, $eol);
53             } else {
54 5         11 my $txt = $sub->($what);
55 5 100       15 $txt =~ s/\x0a/$eol/sg if $how eq 'qp';
56 5         27 return $txt;
57             }
58             }
59              
60 5     5 0 13 sub decode { return codec("decode", @_) }
61 6     6 0 1729 sub encode { return codec("encode", @_) }
62              
63             1;
64              
65             #pod =head1 SYNOPSIS
66             #pod
67             #pod use Email::MIME::Encodings;
68             #pod my $encoded = Email::MIME::Encodings::encode(base64 => $body);
69             #pod my $decoded = Email::MIME::Encodings::decode(base64 => $encoded);
70             #pod
71             #pod If a third argument is given, it is the encoding to which to fall back. If no
72             #pod valid codec can be found (considering both the first and third arguments) then
73             #pod an exception is raised.
74             #pod
75             #pod =head1 DESCRIPTION
76             #pod
77             #pod This module simply wraps C and C
78             #pod so that you can throw the contents of a C
79             #pod header at some text and have the right thing happen.
80             #pod
81             #pod C, C, C.
82              
83             __END__