blib/lib/Email/MIME/Encodings.pm

filter: Statementstmt 92.6% 38 / 41 Branchbran 78.5% 11 / 14 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 4 totaltotal 75.9% 63 / 83 CC 11 SCAR 21.0
CC
11
Cov
83%
codec
13.3
BEGIN
1.0
BEGIN
1.0
CRAP 8.1
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Email::MIME::Encodings 1.316;
4 # ABSTRACT: A unified interface to MIME encoding and decoding
5  
6 1 ▶use MIME::Base64 3.05;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MIME::QuotedPrint 3.05;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶sub identity { $_[0] }
Subroutine identity: called CC=1 SCAR=0.0
Pod undocumented
10  
11 1 for (qw(7bit 8bit binary)) {
12 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 *{"encode_$_"} = *{"decode_$_"} = \&identity;
14 }
15  
16 sub codec {
17 11 ▶ my ($which, $how, $what, $fb) = @_;
Subroutine codec: called CC=11 SCAR=25.9
Pod undocumented
18 11 $how = lc $how;
19 11 ▶ $how = "qp" if $how eq "quotedprint" or $how eq "quoted-printable";
Branch: 1 branch
BranchTrueFalseTotal
if $how eq "quotedprint" or $how eq "quoted-printable" 2 9 11
Condition66%2/3
$how eq "quotedprint" or $how eq "quoted-printable"
l!l&&r!l&&!r
209
MC/DC50%1/2
$how eq "quotedprint" or $how eq "quoted-printable"
$how eq "quotedprint" $how eq "quoted-printable"
Truth table66%2/3
A or B
A= $how eq "quotedprint"B= $how eq "quoted-printable"
ABresult
1X1
011
000
20 11 my $sub = __PACKAGE__->can("$which\_$how");
21  
22 11 ▶ if (! $sub && $fb) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sub and $fb) 0 11 11
Condition66%2/3
not $sub and $fb
!ll&&!rl&&r
1010
MC/DC0%0/2
not $sub and $fb
not $sub $fb
Truth table66%2/3
not(A) and B
A= not $subB= $fb
ABresult
0X0
100
111
23 0 $fb = lc $fb;
24 0 ▶ $fb = "qp" if $fb eq "quotedprint" or $fb eq "quoted-printable";
Branch: 1 branch
BranchTrueFalseTotal
if $fb eq "quotedprint" or $fb eq "quoted-printable" 0 0 0
Condition0%0/3
$fb eq "quotedprint" or $fb eq "quoted-printable"
l!l&&r!l&&!r
000
MC/DC0%0/2
$fb eq "quotedprint" or $fb eq "quoted-printable"
$fb eq "quotedprint" $fb eq "quoted-printable"
Truth table0%0/3
A or B
A= $fb eq "quotedprint"B= $fb eq "quoted-printable"
ABresult
1X1
011
000
25 0 $sub = __PACKAGE__->can("$which\_$fb");
26 }
27  
28 11 ▶ unless ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub) 1 10 11
29 1 require Carp;
30 1 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 my $eol = "\x0d\x0a";
50 10 ▶ if ($which eq 'encode') {
Branch: 1 branch
BranchTrueFalseTotal
if ($which eq 'encode') { } 5 5 10
51 5 ▶ $what =~ s/$eol/\x0a/sg if $how eq 'qp';
Branch: 1 branch
BranchTrueFalseTotal
if $how eq "qp" 1 4 5
52 5 return $sub->($what, $eol);
53 } else {
54 5 my $txt = $sub->($what);
55 5 ▶ $txt =~ s/\x0a/$eol/sg if $how eq 'qp';
Branch: 1 branch
BranchTrueFalseTotal
if $how eq "qp" 1 4 5
56 5 return $txt;
57 }
58 }
59  
60 5 ▶sub decode { return codec("decode", @_) }
Subroutine decode: called CC=1 SCAR=0.0
Pod undocumented
61 6 ▶sub encode { return codec("encode", @_) }
Subroutine encode: called CC=1 SCAR=0.0
Pod undocumented
62  
63 1 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<MIME::Base64> and C<MIME::QuotedPrint>
78 #pod so that you can throw the contents of a C<Content-Transfer-Encoding>
79 #pod header at some text and have the right thing happen.
80 #pod
81 #pod C<MIME::Base64>, C<MIME::QuotedPrint>, C<Email::MIME>.
82  
83 __END__
↑ Summary