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   646 use strict;
  1         2  
  1         31  
2 1     1   5 use warnings;
  1         1  
  1         57  
3             package Email::MIME::Encodings;
4             {
5             $Email::MIME::Encodings::VERSION = '1.315';
6             }
7             # ABSTRACT: A unified interface to MIME encoding and decoding
8              
9 1     1   805 use MIME::Base64 3.05;
  1         720  
  1         71  
10 1     1   663 use MIME::QuotedPrint 3.05;
  1         209  
  1         66  
11              
12 6     6 0 18 sub identity { $_[0] }
13              
14             for (qw(7bit 8bit binary)) {
15 1     1   4 no strict 'refs';
  1         2  
  1         305  
16             *{"encode_$_"} = *{"decode_$_"} = \&identity;
17             }
18              
19             sub codec {
20 11     11 0 20 my ($which, $how, $what, $fb) = @_;
21 11         17 $how = lc $how;
22 11 100 66     47 $how = "qp" if $how eq "quotedprint" or $how eq "quoted-printable";
23 11         59 my $sub = __PACKAGE__->can("$which\_$how");
24              
25 11 50 66     34 if (! $sub && $fb) {
26 0         0 $fb = lc $fb;
27 0 0 0     0 $fb = "qp" if $fb eq "quotedprint" or $fb eq "quoted-printable";
28 0         0 $sub = __PACKAGE__->can("$which\_$fb");
29             }
30              
31 11 100       17 unless ($sub) {
32 1         9 require Carp;
33 1         205 Carp::croak("Don't know how to $which $how");
34             }
35              
36             # RFC2822 requires all email lines to end in CRLF. The Quoted-Printable
37             # RFC requires CRLF to not be encoded, when representing newlins. We will
38             # assume, in this code, that QP is being used for plain text and not binary
39             # data. This may, someday, be wrong -- but if you are using QP to encode
40             # binary data, you are already doing something bizarre.
41             #
42             # The only way to achieve this with MIME::QuotedPrint is to replace all
43             # CRLFs with just LF and then let MIME::QuotedPrint replace all LFs with
44             # CRLF. Otherwise MIME::QuotedPrint (by default) encodes CR as =0D, which
45             # is against RFCs and breaks MUAs (such as Thunderbird).
46             #
47             # We don't modify data before Base64 encoding it because that is usually
48             # binary data and modifying it at all is a bad idea. We do however specify
49             # that the encoder should end lines with CRLF (since that's the email
50             # standard).
51             # -- rjbs and mkanat, 2009-04-16
52 10         13 my $eol = "\x0d\x0a";
53 10 100       21 if ($which eq 'encode') {
54 5 100       18 $what =~ s/$eol/\x0a/sg if $how eq 'qp';
55 5         28 return $sub->($what, $eol);
56             } else {
57 5         13 my $txt = $sub->($what);
58 5 100       12 $txt =~ s/\x0a/$eol/sg if $how eq 'qp';
59 5         22 return $txt;
60             }
61             }
62              
63 5     5 0 11 sub decode { return codec("decode", @_) }
64 6     6 0 1318 sub encode { return codec("encode", @_) }
65              
66             1;
67              
68             __END__