MIME/Encode.pm

filter: Statementstmt 100% 96 / 96 Branchbran 73.8% 31 / 42 Conditioncond 86.3% 19 / 22 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 4 totaltotal 89.1% 172 / 193 CC 30 SCAR 15.8
CC
30
Cov
91%
mime_encode
9.0
maybe_mime_encode_header
7.0
_object_encode
5.1
CRAP 4.9
« MIME/Header/AddressList.pm ↑ Summary MIME/Header.pm »
1 20 ▶use v5.12.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Email::MIME::Encode 1.954;
4 # ABSTRACT: a private helper for MIME header encoding
5  
6 20 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Email::MIME::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use MIME::Base64();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 20 our @CARP_NOT;
14  
15 20 my %no_mime_headers = map { $_ => undef } qw(date message-id in-reply-to references downgraded-message-id downgraded-in-reply-to downgraded-references);
16  
17 44 ▶sub maybe_mime_encode_header {
Subroutine maybe_mime_encode_header: called CC=7 SCAR=19.5
Pod undocumented
18 44 my ($header, $val, $charset) = @_;
19  
20 44 $header = lc $header;
21  
22 44 my $header_name_length = length($header) + length(": ");
23  
24 44 ▶ if (Scalar::Util::blessed($val) && $val->can("as_mime_string")) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($val) and $val->can("as_mime_string")) 2 42 44
Condition100%3/3
builtin::blessed($val) and $val->can("as_mime_string")
!ll&&!rl&&r
4022
MC/DC100%2/2
builtin::blessed($val) and $val->can("as_mime_string")
builtin::blessed($val) $val->can("as_mime_string")
Truth table100%3/3
A and B
A= builtin::blessed($val)B= $val->can("as_mime_string")
ABresult
0X0
100
111
25 2 return $val->as_mime_string({
26 charset => $charset,
27 header_name_length => $header_name_length,
28 });
29 }
30  
31 return _object_encode($val, $charset, $header_name_length, $Email::MIME::Header::header_to_class_map{$header})
32 42 ▶ if exists $Email::MIME::Header::header_to_class_map{$header};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Email::MIME::Header::header_to_class_map{$header} 22 20 42
33  
34 20 my $min_wrap_length = 78 - $header_name_length + 1;
35  
36 20 ▶ return $val
Branch: 1 branch
BranchTrueFalseTotal
unless _needs_mime_encode($val) or $val =~ /[^\s]{$min_wrap_length,}/u 9 11 20
Condition66%2/3
_needs_mime_encode($val) or $val =~ /[^\s]{$min_wrap_length,}/u
l!l&&r!l&&!r
1109
MC/DC50%1/2
_needs_mime_encode($val) or $val =~ /[^\s]{$min_wrap_length,}/u
_needs_mime_encode($val) $val =~ /[^\s]{$min_wrap_length,}/u
Truth table66%2/3
A or B
A= _needs_mime_encode($val)B= $val =~ /[^\s]{$min_wrap_length,}/u
ABresult
1X1
011
000
37 unless _needs_mime_encode($val) || $val =~ /[^\s]{$min_wrap_length,}/;
38  
39 return $val
40 11 ▶ if exists $no_mime_headers{$header};
Branch: 1 branch
BranchTrueFalseTotal
if exists $no_mime_headers{$header} 0 11 11
41  
42 11 return mime_encode($val, $charset, $header_name_length);
43 }
44  
45 100 ▶sub _needs_mime_encode {
Subroutine _needs_mime_encode: called CC=2 SCAR=6.9
46 100 my ($val) = @_;
47 100 ▶ return defined $val && $val =~ /(?:\P{ASCII}|=\?|[^\s]{79,}|^\s+|\s+$)/s;
Condition100%3/3
defined $val && $val =~ /(?:\P{ASCII}|=\?|[^\s]{79,}|^\s+|\s+$)/su
!ll&&!rl&&r
502624
MC/DC100%2/2
defined $val && $val =~ /(?:\P{ASCII}|=\?|[^\s]{79,}|^\s+|\s+$)/su
defined $val $val =~ /(?:\P{ASCII}|=\?|[^\s]{79,}|^\s+|\s+$)/su
Truth table100%3/3
A && B
A= defined $valB= $val =~ /(?:\P{ASCII}|=\?|[^\s]{79,}|^\s+|\s+$)/su
ABresult
0X0
100
111
48 }
49  
50 80 ▶sub _needs_mime_encode_addr {
Subroutine _needs_mime_encode_addr: called CC=3 SCAR=11.0
51 80 my ($val) = @_;
52 80 ▶ return _needs_mime_encode($val) || ( defined $val && $val =~ /[:;,]/ );
Condition100%6/6
_needs_mime_encode($val) || defined $val && $val =~ /[:;,]/u
l!l&&r!l&&!r
131255
defined $val and $val =~ /[:;,]/u
!ll&&!rl&&r
50512
MC/DC100%3/3
_needs_mime_encode($val) || defined $val && $val =~ /[:;,]/u
_needs_mime_encode($val) defined $val $val =~ /[:;,]/u
Truth table100%4/4
A || B and C
A= _needs_mime_encode($val)B= defined $valC= $val =~ /[:;,]/u
ABCresult
1XX1
0111
00X0
0100
53 }
54  
55 22 ▶sub _object_encode {
Subroutine _object_encode: called CC=5 SCAR=16.3
56 22 my ($val, $charset, $header_name_length, $class) = @_;
57  
58 22 local @CARP_NOT = qw(Email::MIME Email::MIME::Header);
59  
60 {
61 22 local $@;
62 22 ▶ Carp::croak("Cannot load package '$class': $@") unless eval { Module::Runtime::require_module($class) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { &Module::Runtime::require_module($class) } } 0 22 22
63 }
64  
65 22 ▶ Carp::croak("Class '$class' does not have method 'from_string'") unless $class->can('from_string');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("from_string") 0 22 22
66  
67 22 ▶ my $object = $class->from_string(ref $val eq 'ARRAY' ? @{$val} : $val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 2 20 22
68  
69 22 ▶ Carp::croak("Object from class '$class' does not have method 'as_mime_string'") unless $object->can('as_mime_string');
Branch: 1 branch
BranchTrueFalseTotal
unless $object->can("as_mime_string") 0 22 22
70  
71 22 return $object->as_mime_string({
72 charset => $charset,
73 header_name_length => $header_name_length,
74 });
75 }
76  
77 # XXX this is copied directly out of Courriel::Header
78 # eventually, this should be extracted out into something that could be shared
79 36 ▶sub mime_encode {
Subroutine mime_encode: called CC=9 SCAR=22.0
Pod undocumented
80 36 my ($text, $charset, $header_name_length) = @_;
81  
82 36 ▶ $header_name_length //= 0;
Condition100%2/2
$header_name_length //= 0
l!l
1125
MC/DC100%1/1
$header_name_length //= 0
$header_name_length
Truth table100%2/2
A
A= $header_name_length
Aresult
11
00
83 36 ▶ $charset //= 'UTF-8';
Condition100%2/2
$charset //= "UTF-8"
l!l
351
MC/DC100%1/1
$charset //= "UTF-8"
$charset
Truth table100%2/2
A
A= $charset
Aresult
11
00
84  
85 36 my $enc_obj = Encode::find_encoding($charset);
86  
87 36 my $head = '=?' . $enc_obj->mime_name() . '?B?';
88 36 my $tail = '?=';
89  
90 36 my $mime_length = length($head) + length($tail);
91  
92 # This code is copied from Mail::Message::Field::Full in the Mail-Box
93 # distro.
94 36 my $real_length = int( ( 75 - $mime_length ) / 4 ) * 3;
95 36 my $first_length = int( ( 75 - $header_name_length - $mime_length ) / 4 ) * 3;
96  
97 36 my @result;
98 36 my $chunk = q{};
99 36 my $first_processed = 0;
100 36 ▶ while ( length( my $chr = substr( $text, 0, 1, '' ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $chr = substr($text, 0, 1, ''))) 832 36 868
101 832 my $chr = $enc_obj->encode( $chr, 0 );
102  
103 832 ▶ if ( length($chunk) + length($chr) > ( $first_processed ? $real_length : $first_length ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length($chunk) + length($chr) > ($first_processed ? $real_length : $first_length)) 8 824 832
$first_processed ? : 120 712 832
104 8 ▶ if ( length($chunk) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $chunk > 0) 8 0 8
105 8 push @result, $head . MIME::Base64::encode_base64( $chunk, q{} ) . $tail;
106 8 $chunk = q{};
107 }
108 8 ▶ $first_processed = 1
Branch: 1 branch
BranchTrueFalseTotal
unless $first_processed 8 0 8
109 unless $first_processed;
110 }
111  
112 832 $chunk .= $chr;
113 }
114  
115 36 ▶ push @result, $head . MIME::Base64::encode_base64( $chunk, q{} ) . $tail
Branch: 1 branch
BranchTrueFalseTotal
if length $chunk 36 0 36
116 if length $chunk;
117  
118 36 return join q{ }, @result;
119 }
120  
121 37 ▶sub maybe_mime_decode_header {
Subroutine maybe_mime_decode_header: called CC=4 SCAR=13.9
Pod undocumented
122 37 my ($header, $val) = @_;
123  
124 37 $header = lc $header;
125  
126 return _object_decode($val, $Email::MIME::Header::header_to_class_map{$header})
127 37 ▶ if exists $Email::MIME::Header::header_to_class_map{$header};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Email::MIME::Header::header_to_class_map{$header} 21 16 37
128  
129 return $val
130 16 ▶ if exists $no_mime_headers{$header};
Branch: 1 branch
BranchTrueFalseTotal
if exists $no_mime_headers{$header} 1 15 16
131  
132 15 ▶ return $val
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /=\?/u 1 14 15
133 unless $val =~ /=\?/;
134  
135 14 return mime_decode($val);
136 }
137  
138 21 ▶sub _object_decode {
Subroutine _object_decode: called CC=4 SCAR=14.1
139 21 my ($string, $class) = @_;
140  
141 21 local @CARP_NOT = qw(Email::MIME Email::MIME::Header);
142  
143 {
144 21 local $@;
145 21 ▶ Carp::croak("Cannot load package '$class': $@") unless eval { Module::Runtime::require_module($class) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { &Module::Runtime::require_module($class) } } 0 21 21
146 }
147  
148 21 ▶ Carp::croak("Class '$class' does not have method 'from_mime_string'") unless $class->can('from_mime_string');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("from_mime_string") 0 21 21
149  
150 21 my $object = $class->from_mime_string($string);
151  
152 21 ▶ Carp::croak("Object from class '$class' does not have method 'as_string'") unless $object->can('as_string');
Branch: 1 branch
BranchTrueFalseTotal
unless $object->can("as_string") 0 21 21
153  
154 21 return $object->as_string();
155 }
156  
157 64 ▶sub mime_decode {
Subroutine mime_decode: called CC=3 SCAR=11.6
Pod undocumented
158 64 my ($text) = @_;
159 64 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 64 64
160  
161 # The eval is to cope with unknown encodings, like Latin-62, or other
162 # nonsense that gets put in there by spammers and weirdos
163 # -- rjbs, 2014-12-04
164 64 local $@;
165 64 my $result = eval { Encode::decode("MIME-Header", $text) };
166 64 ▶ return $result // $text;
Condition33%1/3
$result // $text
l!l&&r!l&&!r
6400
MC/DC0%0/2
$result // $text
$result $text
Truth table33%1/3
A // B
A= $resultB= $text
ABresult
1X1
011
000
167 }
168  
169 20 1;
170  
171 __END__
« MIME/Header/AddressList.pm ↑ Summary MIME/Header.pm »