RFC2047/Decoder.pm

filter: Statementstmt 94.2% 66 / 70 Branchbran 95.4% 21 / 22 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 95.3% 103 / 108 SCAR 25.7
CC
13
Cov
95%
_decode
13.1
BEGIN
1.0
BEGIN
1.0
CRAP 13.0
« RFC2047/Mailbox.pm ↑ Summary RFC2047/Address.pm »
1 package Email::MIME::RFC2047::Decoder;
2 6 $Email::MIME::RFC2047::Decoder::VERSION = '0.96';
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Decoding of non-ASCII MIME headers
7  
8 6 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Don't include period "." to correctly handle obs-phrase.
12 6 my $rfc_specials = '()<>\[\]:;\@\\,"';
13 6 my $rfc_specials_no_quote = '()<>\[\]:;\@\\,';
14  
15 # Regex for encoded words.
16 # This also checks the validity of base64 encoded data because MIME::Base64
17 # silently ignores invalid characters.
18 # Captures ($encoding, $content_b, $content_q)
19 6 my $encoded_word_text_re = qr/
20 (?: ^ | (?<= [\r\n\t ] ) )
21 = \? ( [A-Za-z0-9_-]++ ) \?
22 (?:
23 [Bb] \?
24 (
25 (?:
26 [A-Za-z0-9+\/]{2}
27 (?: == | [A-Za-z0-9+\/] [A-Za-z0-9+\/=] )
28 )++
29 ) |
30 [Qq] \?
31 ( [\x21-\x3E\x40-\x7E]++ )
32 )
33 \? =
34 (?= \z | [\r\n\t ] )
35 /x;
36  
37 # Same as $encoded_word_text_re but excluding RFC 2822 special chars
38 # Also matches after and before special chars (why?).
39 6 my $encoded_word_phrase_re = qr/
40 (?: ^ | (?<= [\r\n\t $rfc_specials_no_quote] ) )
41 = \? ( [A-Za-z0-9_-]++ ) \?
42 (?:
43 [Bb] \?
44 (
45 (?:
46 [A-Za-z0-9+\/]{2}
47 (?: == | [A-Za-z0-9+\/] [A-Za-z0-9+\/=] )
48 )++
49 ) |
50 [Qq] \?
51 ( [A-Za-z0-9!*+\/=_-]++ )
52 )
53 \? =
54 (?= \z | [\r\n\t $rfc_specials_no_quote] )
55 /x;
56  
57 # Same as $encoded_word_text_re but excluding parens and backslash.
58 # Also matches after and before parens.
59 #my $encoded_word_comment_re = qr/
60 # (?<= [\r\n\t ()] )
61 # = \? ( [A-Za-z0-9_-]++ ) \?
62 # (?:
63 # [Bb] \?
64 # (
65 # (?:
66 # [A-Za-z0-9+\/]{2}
67 # (?: == | [A-Za-z0-9+\/] [A-Za-z0-9+\/=] )
68 # )++
69 # ) |
70 # [Qq] \?
71 # ( [\x21-\x27\x2A-\x3E\x40-\x5B\x5D-\x7E]++ )
72 # )
73 # \? =
74 # (?= [\r\n\t ()] )
75 #/x;
76  
77 6 my $quoted_string_re = qr/
78 "
79 (
80 (?:
81 [^"\\]++ |
82 \\ .
83 )*+
84 )
85 "
86 /sx;
87  
88 6 my $comment_re = qr/
89 (
90 \(
91 (?:
92 [^()\\]++ |
93 \\ . |
94 (?-1)
95 )*+
96 \)
97 )
98 /sx;
99  
100 sub new {
101 39 ▶ my $package = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
102  
103 39 my $self = {};
104  
105 39 return bless($self, $package);
106 }
107  
108 sub decode_text {
109 13 ▶ my $self = shift;
Subroutine decode_text: called CC=1 SCAR=0.0
Pod documented
110  
111 13 return $self->_decode('text', @_);
112 }
113  
114 sub decode_phrase {
115 101 ▶ my $self = shift;
Subroutine decode_phrase: called CC=1 SCAR=0.0
Pod documented
116  
117 101 return $self->_decode('phrase', @_);
118 }
119  
120 sub _decode {
121 114 ▶ my ($self, $mode, $encoded) = @_;
Subroutine _decode: called CC=13 SCAR=25.7
122 114 ▶ my $encoded_ref = ref($encoded) ? $encoded : \$encoded;
Branch: 1 branch
BranchTrueFalseTotal
ref $encoded ? : 52 62 114
123  
124 114 my $result = '';
125 114 my $enc_flag;
126 # use shortest match on any characters we don't want to decode
127 114 ▶ my $regex = $mode eq 'phrase' ?
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'phrase' ? : 101 13 114
128 qr/([^$rfc_specials]*?)($encoded_word_phrase_re|$quoted_string_re|$comment_re)/ :
129 qr/(.*?)($encoded_word_text_re)/s;
130  
131 114 ▶ while ($$encoded_ref =~ /\G$regex/cg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$encoded_ref =~ /\G$regex/cg) 104 114 218
132 104 my ($text, $match,
133 $encoding, $b_content, $q_content,
134 $qs_content) =
135 ($1, $2, $3, $4, $5, $6, $7);
136  
137 104 ▶ if (defined($encoding)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $encoding) { } 67 37 104
elsif (defined $qs_content) { } 28 9 37
138 # encoded words shouldn't be longer than 75 chars but
139 # let's allow up to 255 chars
140 67 ▶ if (length($match) > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $match > 255) 0 67 67
141 0 $result .= $text;
142 0 $result .= $match;
143 0 $enc_flag = undef;
144 0 next;
145 }
146  
147 67 my $content;
148  
149 67 ▶ if (defined($b_content)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $b_content) { } 3 64 67
150 # MIME B
151 3 $content = MIME::Base64::decode_base64($b_content);
152 }
153 else {
154 # MIME Q
155 64 $content = $q_content;
156 64 $content =~ tr/_/ /;
157 64 $content =~ s/=([0-9A-Fa-f]{2})/chr(hex($1))/eg;
158 }
159  
160 67 my $chunk;
161 67 eval {
162 67 $chunk = Encode::decode(
163 $encoding,
164 $content,
165 Encode::FB_CROAK
166 );
167 };
168  
169 67 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 65 67
170 2 warn($@);
171 # display raw encoded word in case of errors
172 2 $result .= $text;
173 2 $result .= $match;
174 2 $enc_flag = undef;
175 2 next;
176 }
177  
178 # ignore whitespace between encoded words
179 65 ▶ $result .= $text if !$enc_flag || $text =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if not $enc_flag or $text =~ /\S/ 54 11 65
Condition100%3/3
not $enc_flag or $text =~ /\S/
l!l&&r!l&&!r
53111
MC/DC100%2/2
not $enc_flag or $text =~ /\S/
not $enc_flag $text =~ /\S/
Truth table100%3/3
not(A) or B
A= not $enc_flagB= $text =~ /\S/
ABresult
1X1
011
000
180  
181 65 $result .= $chunk;
182  
183 65 $enc_flag = 1;
184 }
185 elsif (defined($qs_content)) {
186 # quoted string
187  
188 28 $result .= $text;
189  
190 # unquote
191 28 $qs_content =~ s/\\(.)/$1/gs;
192 28 $result .= $qs_content;
193  
194 28 $enc_flag = undef;
195 }
196 }
197  
198 114 ▶ $regex = $mode eq 'phrase' ?
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'phrase' ? : 101 13 114
199 qr/[^$rfc_specials]+/ :
200 qr/.+/s;
201 114 ▶ $result .= $& if $$encoded_ref =~ /\G$regex/cg;
Branch: 1 branch
BranchTrueFalseTotal
if $$encoded_ref =~ /\G$regex/cg 72 42 114
202  
203 # normalize whitespace
204 114 $result =~ s/^[\r\n\t ]+//;
205 114 $result =~ s/[\r\n\t ]+\z//;
206 114 $result =~ s/[\r\n\t ]+/ /g;
207  
208 # remove potentially dangerous ASCII control chars
209 114 $result =~ s/[\x00-\x1f\x7f]//g;
210  
211 114 return $result;
212 }
213  
214 6 1;
215  
216 __END__
« RFC2047/Mailbox.pm ↑ Summary RFC2047/Address.pm »