MIME/EncWords.pm

filter: Statementstmt 85.4% 410 / 480 Branchbran 70.0% 171 / 244 Conditioncond 68.1% 139 / 204 MC/DCmcdc 40.3% 44 / 109 Subroutinesub 100% 22 / 22 Podpod 100% 3 / 3 totaltotal 74.2% 789 / 1062 CC 207 SCAR 43.4
CC
207
Cov
78%
encode_mimewords
148.1
_utf_to_unicode
39.9
decode_mimewords
37.7
CRAP 76.7
« Encode/MIME/EncWords.pm ↑ Summary
1 #-*- perl -*-
2  
3 package MIME::EncWords;
4 5 require 5.005;
5  
6 =head1 NAME
7  
8 MIME::EncWords - deal with RFC 2047 encoded words (improved)
9  
10 =head1 SYNOPSIS
11  
12 I<L<MIME::EncWords> is aimed to be another implimentation
13 of L<MIME::Words> so that it will achieve more exact conformance with
14 RFC 2047 (formerly RFC 1522) specifications. Additionally, it contains
15 some improvements.
16 Following synopsis and descriptions are inherited from its inspirer,
17 then added descriptions on improvements (B<**>) or changes and
18 clarifications (B<*>).>
19  
20 Before reading further, you should see L<MIME::Tools> to make sure that
21 you understand where this module fits into the grand scheme of things.
22 Go on, do it now. I'll wait.
23  
24 Ready? Ok...
25  
26 use MIME::EncWords qw(:all);
27  
28 ### Decode the string into another string, forgetting the charsets:
29 $decoded = decode_mimewords(
30 'To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>',
31 );
32  
33 ### Split string into array of decoded [DATA,CHARSET] pairs:
34 @decoded = decode_mimewords(
35 'To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>',
36 );
37  
38 ### Encode a single unsafe word:
39 $encoded = encode_mimeword("\xABFran\xE7ois\xBB");
40  
41 ### Encode a string, trying to find the unsafe words inside it:
42 $encoded = encode_mimewords("Me and \xABFran\xE7ois\xBB in town");
43  
44 =head1 DESCRIPTION
45  
46 Fellow Americans, you probably won't know what the hell this module
47 is for. Europeans, Russians, et al, you probably do. C<:-)>.
48  
49 For example, here's a valid MIME header you might get:
50  
51 From: =?US-ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>
52 To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>
53 CC: =?ISO-8859-1?Q?Andr=E9_?= Pirard <PIRARD@vm1.ulg.ac.be>
54 Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=
55 =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=
56 =?US-ASCII?Q?.._cool!?=
57  
58 The fields basically decode to (sorry, I can only approximate the
59 Latin characters with 7 bit sequences /o and 'e):
60  
61 From: Keith Moore <moore@cs.utk.edu>
62 To: Keld J/orn Simonsen <keld@dkuug.dk>
63 CC: Andr'e Pirard <PIRARD@vm1.ulg.ac.be>
64 Subject: If you can read this you understand the example... cool!
65  
66 B<Supplement>: Fellow Americans, Europeans, you probably won't know
67 what the hell this module is for. East Asians, et al, you probably do.
68 C<(^_^)>.
69  
70 For example, here's a valid MIME header you might get:
71  
72 Subject: =?EUC-KR?B?sNTAuLinKGxhemluZXNzKSwgwvzB9ri7seIoaW1w?=
73 =?EUC-KR?B?YXRpZW5jZSksILGzuLgoaHVicmlzKQ==?=
74  
75 The fields basically decode to (sorry, I cannot approximate the
76 non-Latin multibyte characters with any 7 bit sequences):
77  
78 Subject: ???(laziness), ????(impatience), ??(hubris)
79  
80 =head1 PUBLIC INTERFACE
81  
82 =over 4
83  
84 =cut
85  
86 ### Pragmas:
87 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 5 ▶use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @ISA $Config);
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 ### Exporting:
91 5 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 5 %EXPORT_TAGS = (all => [qw(decode_mimewords
94 encode_mimeword
95 encode_mimewords)]);
96 5 Exporter::export_ok_tags(qw(all));
97  
98 ### Inheritance:
99 5 @ISA = qw(Exporter);
100  
101 ### Other modules:
102 5 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
103 5 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 5 ▶use MIME::Charset qw(:trans);
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 5 my @ENCODE_SUBS = qw(FB_CROAK is_utf8 resolve_alias);
107 5 if (MIME::Charset::USE_ENCODE) {
108 5 ▶ eval "use ".MIME::Charset::USE_ENCODE." \@ENCODE_SUBS;";
Subroutine BEGIN: called CC=1 SCAR=0.0
109 5 ▶ if ($@) { # Perl 5.7.3 + Encode 0.40
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
110 0 eval "use ".MIME::Charset::USE_ENCODE." qw(is_utf8);";
111 0 require MIME::Charset::_Compat;
112 0 for my $sub (@ENCODE_SUBS) {
113 5 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
114 0 ▶ *{$sub} = \&{"MIME::Charset::_Compat::$sub"}
Branch: 1 branch
BranchTrueFalseTotal
unless $sub eq "is_utf8" 0 0 0
115 unless $sub eq 'is_utf8';
116 }
117 }
118 } else {
119 require Unicode::String;
120 require MIME::Charset::_Compat;
121 for my $sub (@ENCODE_SUBS) {
122 5 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
123 *{$sub} = \&{"MIME::Charset::_Compat::$sub"};
124 }
125 }
126  
127 #------------------------------
128 #
129 # Globals...
130 #
131 #------------------------------
132  
133 ### The package version, both in 1.23 style *and* usable by MakeMaker:
134 5 $VERSION = '1.015.0';
135  
136 ### Public Configuration Attributes
137 $Config = {
138 5 %{$MIME::Charset::Config}, # Detect7bit, Replacement, Mapping
139 Charset => 'ISO-8859-1',
140 Encoding => 'A',
141 Field => undef,
142 Folding => "\n",
143 MaxLineLen => 76,
144 Minimal => 'YES',
145 };
146 5 eval {
147 5 local @INC = @INC;
148 5 ▶ pop @INC if $INC[-1] eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 5 0 5
149 5 require MIME::EncWords::Defaults;
150 };
151  
152 ### Private Constants
153  
154 5 my $PRINTABLE = "\\x21-\\x7E";
155 #my $NONPRINT = "\\x00-\\x1F\\x7F-\\xFF";
156 5 my $NONPRINT = qr{[^$PRINTABLE]}; # Improvement: Unicode support.
157 5 my $UNSAFE = qr{[^\x01-\x20$PRINTABLE]};
158 5 my $WIDECHAR = qr{[^\x00-\xFF]};
159 5 my $ASCIITRANS = qr{^(?:HZ-GB-2312|UTF-7)$}i;
160 5 my $ASCIIINCOMPAT = qr{^UTF-(?:16|32)(?:BE|LE)?$}i;
161 5 my $DISPNAMESPECIAL = qr{[()<>\[\]:;@\\,."]}; # RFC5322 name-addr specials.
162  
163 #------------------------------
164  
165 # _utf_to_unicode CSETOBJ, STR
166 # Private: Convert UTF-16*/32* to Unicode or UTF-8.
167 90 ▶sub _utf_to_unicode {
Subroutine _utf_to_unicode: called CC=8 SCAR=36.9
168 90 my $csetobj = shift;
169 90 my $str = shift;
170  
171 90 ▶ return $str if is_utf8($str);
Branch: 1 branch
BranchTrueFalseTotal
if &is_utf8($str) 42 48 90
172  
173 48 return $csetobj->decode($str)
174 if MIME::Charset::USE_ENCODE();
175  
176 0 my $cset = $csetobj->as_string;
177 0 my $unistr = Unicode::String->new();
178 0 ▶ if ($cset eq 'UTF-16' or $cset eq 'UTF-16BE') {
Branch: 4 branches
BranchTrueFalseTotal
if ($cset eq 'UTF-16' or $cset eq 'UTF-16BE') { } 0 0 0
elsif ($cset eq 'UTF-16LE') { } 0 0 0
elsif ($cset eq 'UTF-32' or $cset eq 'UTF-32BE') { } 0 0 0
elsif ($cset eq 'UTF-32LE') { } 0 0 0
Condition0%0/3
$cset eq "UTF-16" or $cset eq "UTF-16BE"
l!l&&r!l&&!r
000
MC/DC0%0/2
$cset eq "UTF-16" or $cset eq "UTF-16BE"
$cset eq "UTF-16" $cset eq "UTF-16BE"
Truth table0%0/3
A or B
A= $cset eq "UTF-16"B= $cset eq "UTF-16BE"
ABresult
1X1
011
000
179 0 $unistr->utf16($str);
180 } elsif ($cset eq 'UTF-16LE') {
181 0 $unistr->utf16le($str);
182 ▶ } elsif ($cset eq 'UTF-32' or $cset eq 'UTF-32BE') {
Condition0%0/3
$cset eq "UTF-32" or $cset eq "UTF-32BE"
l!l&&r!l&&!r
000
MC/DC0%0/2
$cset eq "UTF-32" or $cset eq "UTF-32BE"
$cset eq "UTF-32" $cset eq "UTF-32BE"
Truth table0%0/3
A or B
A= $cset eq "UTF-32"B= $cset eq "UTF-32BE"
ABresult
1X1
011
000
183 0 $unistr->utf32($str);
184 } elsif ($cset eq 'UTF-32LE') {
185 0 $unistr->utf32le($str);
186 } else {
187 0 croak "unknown transformation '$cset'";
188 }
189 0 return $unistr->utf8;
190 }
191  
192 #------------------------------
193  
194 # _decode_B STRING
195 # Private: used by _decode_header() to decode "B" encoding.
196 # Improvement by this module: sanity check on encoded sequence.
197 57 ▶sub _decode_B {
Subroutine _decode_B: called CC=3 SCAR=13.3
198 57 my $str = shift;
199 57 ▶ unless ((length($str) % 4 == 0) and
Branch: 1 branch
BranchTrueFalseTotal
unless (length($str) % 4 == 0 and $str =~ m[^[A-Za-z0-9+/]+={0,2}$]) 0 57 57
Condition33%1/3
length($str) % 4 == 0 and $str =~ m[^[A-Za-z0-9+/]+={0,2}$]
!ll&&!rl&&r
0057
MC/DC0%0/2
length($str) % 4 == 0 and $str =~ m[^[A-Za-z0-9+/]+={0,2}$]
length($str) % 4 == 0 $str =~ m[^[A-Za-z0-9+/]+={0,2}$]
Truth table33%1/3
A and B
A= length($str) % 4 == 0B= $str =~ m[^[A-Za-z0-9+/]+={0,2}$]
ABresult
0X0
100
111
200 $str =~ m|^[A-Za-z0-9+/]+={0,2}$|) {
201 0 return undef;
202 }
203 57 return decode_base64($str);
204 }
205  
206 # _decode_Q STRING
207 # Private: used by _decode_header() to decode "Q" encoding, which is
208 # almost, but not exactly, quoted-printable. :-P
209 # Improvement by this module: sanity check on encoded sequence (>=1.012.3).
210 49 ▶sub _decode_Q {
Subroutine _decode_Q: called CC=2 SCAR=7.1
211 49 my $str = shift;
212 49 ▶ if ($str =~ /=(?![0-9a-fA-F][0-9a-fA-F])/) { #XXX:" " and "\t" are allowed
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /=(?![0-9a-fA-F][0-9a-fA-F])/) 0 49 49
213 0 return undef;
214 }
215 49 $str =~ s/_/\x20/g; # RFC 2047, Q rule 2
216 49 $str =~ s/=([0-9a-fA-F]{2})/pack("C", hex($1))/ge; # RFC 2047, Q rule 1
217 49 $str;
218 }
219  
220 # _encode_B STRING
221 # Private: used by encode_mimeword() to encode "B" encoding.
222 73 ▶sub _encode_B {
Subroutine _encode_B: called CC=1 SCAR=0.0
223 73 my $str = shift;
224 73 encode_base64($str, '');
225 }
226  
227 # _encode_Q STRING
228 # Private: used by encode_mimeword() to encode "Q" encoding, which is
229 # almost, but not exactly, quoted-printable. :-P
230 # Improvement by this module: Spaces are escaped by ``_''.
231 75 ▶sub _encode_Q {
Subroutine _encode_Q: called CC=2 SCAR=6.9
232 75 my $str = shift;
233 # Restrict characters to those listed in RFC 2047 section 5 (3)
234 75 $str =~ s{[^-!*+/0-9A-Za-z]}{
235 495 ▶ $& eq "\x20"? "_": sprintf("=%02X", ord($&))
Branch: 1 branch
BranchTrueFalseTotal
$& eq ' ' ? : 27 468 495
236 }eog;
237 75 $str;
238 }
239  
240 #------------------------------
241  
242 =item decode_mimewords ENCODED, [OPTS...]
243  
244 I<Function.>
245 Go through the string looking for RFC 2047-style "Q"
246 (quoted-printable, sort of) or "B" (base64) encoding, and decode them.
247  
248 B<In an array context,> splits the ENCODED string into a list of decoded
249 C<[DATA, CHARSET]> pairs, and returns that list. Unencoded
250 data are returned in a 1-element array C<[DATA]>, giving an effective
251 CHARSET of C<undef>.
252  
253 $enc = '=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>';
254 foreach (decode_mimewords($enc)) {
255 print "", ($_[1] || 'US-ASCII'), ": ", $_[0], "\n";
256 }
257  
258 B<**>
259 However, adjacent encoded-words with same charset will be concatenated
260 to handle multibyte sequences safely.
261  
262 B<**>
263 Language information defined by RFC2231, section 5 will be additional
264 third element, if any.
265  
266 B<*>
267 Whitespaces surrounding unencoded data will not be stripped so that
268 compatibility with L<MIME::Words> will be ensured.
269  
270 B<In a scalar context,> joins the "data" elements of the above
271 list together, and returns that. I<Warning: this is information-lossy,>
272 and probably I<not> what you want, but if you know that all charsets
273 in the ENCODED string are identical, it might be useful to you.
274 (Before you use this, please see L<MIME::WordDecoder/unmime>,
275 which is probably what you want.)
276 B<**>
277 See also "Charset" option below.
278  
279 In the event of a syntax error, $@ will be set to a description
280 of the error, but parsing will continue as best as possible (so as to
281 get I<something> back when decoding headers).
282 $@ will be false if no error was detected.
283  
284 B<*>
285 Malformed encoded-words will be kept encoded.
286 In this case $@ will be set.
287  
288 Any arguments past the ENCODED string are taken to define a hash of options.
289 B<**>
290 When Unicode/multibyte support is disabled
291 (see L<MIME::Charset/USE_ENCODE>),
292 these options will not have any effects.
293  
294 =over 4
295  
296 =item Charset
297 B<**>
298  
299 Name of character set by which data elements in scalar context
300 will be converted.
301 The default is no conversion.
302 If this option is specified as special value C<"_UNICODE_">,
303 returned value will be Unicode string.
304  
305 B<Note>:
306 This feature is still information-lossy, I<except> when C<"_UNICODE_"> is
307 specified.
308  
309 =item Detect7bit
310 B<**>
311  
312 Try to detect 7-bit charset on unencoded portions.
313 Default is C<"YES">.
314  
315 =cut
316  
317 #=item Field
318 #
319 #Name of the mail field this string came from. I<Currently ignored.>
320  
321 =item Mapping
322 B<**>
323  
324 In scalar context, specify mappings actually used for charset names.
325 C<"EXTENDED"> uses extended mappings.
326 C<"STANDARD"> uses standardized strict mappings.
327 Default is C<"EXTENDED">.
328  
329 =back
330  
331 =cut
332  
333 72 ▶sub decode_mimewords {
Subroutine decode_mimewords: called CC=30 SCAR=36.3
Pod documented
334 72 my $encstr = shift;
335 72 my %params = @_;
336 72 my %Params = &_getparams(\%params,
337 NoDefault => [qw(Charset)], # default is no conv.
338 YesNo => [qw(Detect7bit)],
339 Others => [qw(Mapping)],
340 Obsoleted => [qw(Field)],
341 ToUpper => [qw(Charset Mapping)],
342 );
343 my $cset = MIME::Charset->new($Params{Charset},
344 72 Mapping => $Params{Mapping});
345 # unfolding: normalize linear-white-spaces and orphan newlines.
346 72 ▶ $encstr =~ s/(?:[\r\n]+[\t ])*[\r\n]+([\t ]|\Z)/$1? " ": ""/eg;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 39 0 39
347 72 $encstr =~ s/[\r\n]+/ /g;
348  
349 72 my @tokens;
350 72 $@ = ''; ### error-return
351  
352 ### Decode:
353 72 my ($word, $charset, $language, $encoding, $enc, $dec);
354 72 my $spc = '';
355 72 pos($encstr) = 0;
356 72 while (1) {
357 265 ▶ last if (pos($encstr) >= length($encstr));
Branch: 1 branch
BranchTrueFalseTotal
if pos $encstr >= length $encstr 72 193 265
358 193 my $pos = pos($encstr); ### save it
359  
360 ### Case 1: are we looking at "=?..?..?="?
361 193 ▶ if ($encstr =~ m{\G # from where we left off..
Branch: 1 branch
BranchTrueFalseTotal
if ($encstr =~ /\G # from where we left off.. =\?([^?]*) # "=?" + charset + \?([bq]) # "?" + encoding + \?([^?]+) # "?" + data maybe with spcs + \?= # "?=" ([\r\n\t ]*) /gix) 106 87 193
362 =\?([^?]*) # "=?" + charset +
363 \?([bq]) # "?" + encoding +
364 \?([^?]+) # "?" + data maybe with spcs +
365 \?= # "?="
366 ([\r\n\t ]*)
367 }xgi) {
368 106 ($word, $charset, $encoding, $enc) = ($&, $1, lc($2), $3);
369 106 my $tspc = $4;
370  
371 # RFC 2231 section 5 extension
372 106 ▶ if ($charset =~ s/^([^\*]*)\*(.*)/$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charset =~ s/^([^\*]*)\*(.*)/$1/) { } 4 102 106
373 4 ▶ $language = $2 || undef;
Condition50%1/2
$2 || undef
l!l
40
MC/DC0%0/1
$2 || undef
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
374 4 ▶ $charset ||= undef;
Condition50%1/2
$charset ||= undef
l!l
40
MC/DC0%0/1
$charset ||= undef
$charset
Truth table50%1/2
A
A= $charset
Aresult
11
00
375 } else {
376 102 $language = undef;
377 }
378  
379 106 ▶ if ($encoding eq 'q') {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding eq 'q') { } 49 57 106
380 49 $dec = _decode_Q($enc);
381 } else {
382 57 $dec = _decode_B($enc);
383 }
384 106 ▶ unless (defined $dec) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dec) 0 106 106
385 0 $@ .= qq|Illegal sequence in "$word" (pos $pos)\n|;
386 0 push @tokens, [$spc.$word];
387 0 $spc = '';
388 0 next;
389 }
390  
391 106 { local $@;
392 106 ▶ if (scalar(@tokens) and
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @tokens and lc($charset || '') eq lc($tokens[-1][1] || '') and &resolve_alias($charset) and !($tokens[-1][2] || $language) || lc $tokens[-1][2] eq lc $language) { } 24 82 106
elsif ($language) { } 4 78 82
elsif ($charset) { } 78 0 78
Condition63%12/19
scalar @tokens and lc($charset || "") eq lc($tokens[-1][1] || "") and &resolve_alias($charset) and !($tokens[-1][2] || $language) || lc $tokens[-1][2] eq lc $language
!ll&&!rl&&r
82024
scalar @tokens and lc($charset || "") eq lc($tokens[-1][1] || "") and &resolve_alias($charset)
!ll&&!rl&&r
82024
scalar @tokens and lc($charset || "") eq lc($tokens[-1][1] || "")
!ll&&!rl&&r
255724
$charset || ''
l!l
810
$tokens[-1][1] || ''
l!l
3942
not $tokens[-1][2] || $language or lc $tokens[-1][2] eq lc $language
l!l&&r!l&&!r
2400
$tokens[-1][2] || $language
l!l&&r!l&&!r
0024
MC/DC16%1/6
scalar @tokens and lc($charset || "") eq lc($tokens[-1][1] || "") and &resolve_alias($charset) and !($tokens[-1][2] || $language) || lc $tokens[-1][2] eq lc $language
scalar @tokens $charset &resolve_alias($charset) $tokens[-1][2] $language lc $tokens[-1][2] eq lc $language
MC/DC100%1/1
$tokens[-1][1] || ''
$tokens[-1][1]
Truth table25%2/8
A and B and C and not(D || E) or F
A= scalar @tokensB= $charsetC= &resolve_alias($charset)D= $tokens[-1][2]E= $languageF= lc $tokens[-1][2] eq lc $language
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1111X00
1110100
11100X1
1111X11
1110111
Truth table100%2/2
A
A= $tokens[-1][1]
Aresult
11
00
393 lc($charset || "") eq lc($tokens[-1]->[1] || "") and
394 resolve_alias($charset) and
395 (!${tokens[-1]}[2] and !$language or
396 lc(${tokens[-1]}[2]) eq lc($language))) { # Concat words if possible.
397 24 $tokens[-1]->[0] .= $dec;
398 } elsif ($language) {
399 4 push @tokens, [$dec, $charset, $language];
400 } elsif ($charset) {
401 78 push @tokens, [$dec, $charset];
402 } else {
403 0 push @tokens, [$dec];
404 }
405 106 $spc = $tspc;
406 }
407 106 next;
408 }
409  
410 ### Case 2: are we looking at a bad "=?..." prefix?
411 ### We need this to detect problems for case 3, which stops at "=?":
412 87 pos($encstr) = $pos; # reset the pointer.
413 87 ▶ if ($encstr =~ m{\G=\?}xg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encstr =~ /\G=\?/gx) 6 81 87
414 6 $@ .= qq|unterminated "=?..?..?=" in "$encstr" (pos $pos)\n|;
415 6 push @tokens, [$spc.'=?'];
416 6 $spc = '';
417 6 next;
418 }
419  
420 ### Case 3: are we looking at ordinary text?
421 81 pos($encstr) = $pos; # reset the pointer.
422 81 ▶ if ($encstr =~ m{\G # from where we left off...
Branch: 1 branch
BranchTrueFalseTotal
if ($encstr =~ /\G # from where we left off... (.*? # shortest possible string, \n*) # followed by 0 or more NLs, (?=(\Z|=\?)) # terminated by "=?" or EOS /gsx) 81 0 81
423 (.*? # shortest possible string,
424 \n*) # followed by 0 or more NLs,
425 (?=(\Z|=\?)) # terminated by "=?" or EOS
426 }xgs) {
427 81 ▶ length($1) or croak "MIME::EncWords: internal logic err: empty token\n";
Branch: 1 branch
BranchTrueFalseTotal
length $1 or croak("MIME::EncWords: internal logic err: empty token\n") 81 0 81
428 81 push @tokens, [$spc.$1];
429 81 $spc = '';
430 81 next;
431 }
432  
433 ### Case 4: bug!
434 0 croak "MIME::EncWords: unexpected case:\n($encstr) pos $pos\n\t".
435 "Please alert developer.\n";
436 }
437 72 ▶ push @tokens, [$spc] if $spc;
Branch: 1 branch
BranchTrueFalseTotal
if $spc 0 72 72
438  
439 # Detect 7-bit charset
440 72 ▶ if ($Params{Detect7bit} ne "NO") {
Branch: 1 branch
BranchTrueFalseTotal
if ($Params{"Detect7bit"} ne "NO") 72 0 72
441 72 local $@;
442 72 foreach my $t (@tokens) {
443 169 ▶ unless ($t->[0] =~ $UNSAFE or $t->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->[0] =~ /$UNSAFE/ or $t->[1]) 87 82 169
Condition100%3/3
$t->[0] =~ /$UNSAFE/ or $t->[1]
l!l&&r!l&&!r
364687
MC/DC100%2/2
$t->[0] =~ /$UNSAFE/ or $t->[1]
$t->[0] =~ /$UNSAFE/ $t->[1]
Truth table100%3/3
A or B
A= $t->[0] =~ /$UNSAFE/B= $t->[1]
ABresult
1X1
011
000
444 87 my $charset = MIME::Charset::_detect_7bit_charset($t->[0]);
445 87 ▶ if ($charset and $charset ne &MIME::Charset::default()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charset and $charset ne &MIME::Charset::default()) 0 87 87
Condition33%1/3
$charset and $charset ne &MIME::Charset::default()
!ll&&!rl&&r
0870
MC/DC0%0/2
$charset and $charset ne &MIME::Charset::default()
$charset $charset ne &MIME::Charset::default()
Truth table33%1/3
A and B
A= $charsetB= $charset ne &MIME::Charset::default()
ABresult
0X0
100
111
446 0 $t->[1] = $charset;
447 }
448 }
449 }
450 }
451  
452 72 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 24 48 72
453 24 @tokens;
454 } else {
455 join('', map {
456 48 &_convert($_->[0], $_->[1], $cset, $Params{Mapping})
457 111 } @tokens);
458 }
459 }
460  
461 #------------------------------
462  
463 # _convert RAW, FROMCHARSET, TOCHARSET, MAPPING
464 # Private: used by decode_mimewords() to convert string by other charset
465 # or to decode to Unicode.
466 # When source charset is unknown and Unicode string is requested, at first
467 # try well-formed UTF-8 then fallback to ISO-8859-1 so that almost all
468 # non-ASCII bytes will be preserved.
469 111 ▶sub _convert($$$$) {
Subroutine _convert: called CC=15 SCAR=33.8
470 111 my $s = shift;
471 111 my $charset = shift;
472 111 my $cset = shift;
473 111 my $mapping = shift;
474 111 ▶ return $s unless &MIME::Charset::USE_ENCODE;
Branch: 1 branch
BranchTrueFalseTotal
unless &MIME::Charset::USE_ENCODE 0 111 111
475 111 ▶ return $s unless $cset->as_string;
Branch: 1 branch
BranchTrueFalseTotal
unless $cset->as_string 45 66 111
476 66 ▶ croak "unsupported charset ``".$cset->as_string."''"
Branch: 1 branch
BranchTrueFalseTotal
unless $cset->decoder or $cset->as_string eq "_UNICODE_" 0 66 66
Condition66%2/3
$cset->decoder or $cset->as_string eq "_UNICODE_"
l!l&&r!l&&!r
29370
MC/DC0%0/2
$cset->decoder or $cset->as_string eq "_UNICODE_"
$cset->decoder $cset->as_string eq "_UNICODE_"
Truth table66%2/3
A or B
A= $cset->decoderB= $cset->as_string eq "_UNICODE_"
ABresult
1X1
011
000
477 unless $cset->decoder or $cset->as_string eq "_UNICODE_";
478  
479 66 local($@);
480 66 $charset = MIME::Charset->new($charset, Mapping => $mapping);
481 66 ▶ if ($charset->as_string and $charset->as_string eq $cset->as_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charset->as_string and $charset->as_string eq $cset->as_string) 0 66 66
Condition66%2/3
$charset->as_string and $charset->as_string eq $cset->as_string
!ll&&!rl&&r
29370
MC/DC0%0/2
$charset->as_string and $charset->as_string eq $cset->as_string
$charset->as_string $charset->as_string eq $cset->as_string
Truth table66%2/3
A and B
A= $charset->as_stringB= $charset->as_string eq $cset->as_string
ABresult
0X0
100
111
482 0 return $s;
483 }
484 # build charset object to transform string from $charset to $cset.
485 66 $charset->encoder($cset);
486  
487 66 my $converted = $s;
488 66 ▶ if (is_utf8($s) or $s =~ $WIDECHAR) {
Branch: 3 branches
BranchTrueFalseTotal
if (&is_utf8($s) or $s =~ /$WIDECHAR/) { } 0 66 66
elsif ($charset->output_charset eq '_UNICODE_') { } 37 29 66
elsif ($charset->decoder) { } 18 11 29
Condition33%1/3
&is_utf8($s) or $s =~ /$WIDECHAR/
l!l&&r!l&&!r
0066
MC/DC0%0/2
&is_utf8($s) or $s =~ /$WIDECHAR/
&is_utf8($s) $s =~ /$WIDECHAR/
Truth table33%1/3
A or B
A= &is_utf8($s)B= $s =~ /$WIDECHAR/
ABresult
1X1
011
000
489 0 ▶ if ($charset->output_charset ne "_UNICODE_") {
Branch: 1 branch
BranchTrueFalseTotal
if ($charset->output_charset ne "_UNICODE_") 0 0 0
490 0 $converted = $charset->encode($s);
491 }
492 } elsif ($charset->output_charset eq "_UNICODE_") {
493 37 ▶ if (!$charset->decoder) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $charset->decoder) { } 18 19 37
494 18 ▶ if ($s =~ $UNSAFE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /$UNSAFE/) 0 18 18
495 0 $@ = '';
496 0 eval {
497 0 $charset = MIME::Charset->new("UTF-8",
498 Mapping => 'STANDARD');
499 0 $converted = $charset->decode($converted, FB_CROAK());
500 };
501 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
502 0 $converted = $s;
503 0 $charset = MIME::Charset->new("ISO-8859-1",
504 Mapping => 'STANDARD');
505 0 $converted = $charset->decode($converted, 0);
506 }
507 }
508 } else {
509 19 $converted = $charset->decode($s);
510 }
511 } elsif ($charset->decoder) {
512 18 $converted = $charset->encode($s);
513 }
514 66 return $converted;
515 }
516  
517 #------------------------------
518  
519 =item encode_mimeword RAW, [ENCODING], [CHARSET]
520  
521 I<Function.>
522 Encode a single RAW "word" that has unsafe characters.
523 The "word" will be encoded in its entirety.
524  
525 ### Encode "<<Franc,ois>>":
526 $encoded = encode_mimeword("\xABFran\xE7ois\xBB");
527  
528 You may specify the ENCODING (C<"Q"> or C<"B">), which defaults to C<"Q">.
529 B<**>
530 You may also specify it as ``special'' value: C<"S"> to choose shorter
531 one of either C<"Q"> or C<"B">.
532  
533 You may specify the CHARSET, which defaults to C<iso-8859-1>.
534  
535 B<*>
536 Spaces will be escaped with ``_'' by C<"Q"> encoding.
537  
538 =cut
539  
540 148 ▶sub encode_mimeword {
Subroutine encode_mimeword: called CC=11 SCAR=31.1
Pod documented
541 148 my $word = shift;
542 148 ▶ my $encoding = uc(shift || 'Q'); # not overridden.
Condition50%1/2
shift() || 'Q'
l!l
1480
MC/DC0%0/1
shift() || 'Q'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
543 148 ▶ my $charset = shift || 'ISO-8859-1'; # ditto.
Condition50%1/2
shift() || 'ISO-8859-1'
l!l
1480
MC/DC0%0/1
shift() || 'ISO-8859-1'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
544 148 ▶ my $language = uc(shift || ""); # ditto.
Condition50%1/2
shift() || ''
l!l
0148
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
545  
546 148 ▶ if (ref $charset) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $charset) { } 148 0 148
547 148 ▶ if (is_utf8($word) or $word =~ /$WIDECHAR/) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_utf8($word) or $word =~ /$WIDECHAR/) 0 148 148
Condition33%1/3
&is_utf8($word) or $word =~ /$WIDECHAR/
l!l&&r!l&&!r
00148
MC/DC0%0/2
&is_utf8($word) or $word =~ /$WIDECHAR/
&is_utf8($word) $word =~ /$WIDECHAR/
Truth table33%1/3
A or B
A= &is_utf8($word)B= $word =~ /$WIDECHAR/
ABresult
1X1
011
000
548 0 $word = $charset->undecode($word, 0);
549 }
550 148 $charset = $charset->as_string;
551 } else {
552 0 $charset = uc($charset);
553 }
554 148 my $encstr;
555 148 ▶ if ($encoding eq 'Q') {
Branch: 2 branches
BranchTrueFalseTotal
if ($encoding eq 'Q') { } 75 73 148
elsif ($encoding eq 'S') { } 0 73 73
556 75 $encstr = &_encode_Q($word);
557 } elsif ($encoding eq "S") {
558 0 my ($B, $Q) = (&_encode_B($word), &_encode_Q($word));
559 0 ▶ if (length($B) < length($Q)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $B < length $Q) { } 0 0 0
560 0 $encoding = "B";
561 0 $encstr = $B;
562 } else {
563 0 $encoding = "Q";
564 0 $encstr = $Q;
565 }
566 } else { # "B"
567 73 $encoding = "B";
568 73 $encstr = &_encode_B($word);
569 }
570  
571 148 ▶ if ($language) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language) { } 0 148 148
572 0 return "=?$charset*$language?$encoding?$encstr?=";
573 } else {
574 148 return "=?$charset?$encoding?$encstr?=";
575 }
576 }
577  
578 #------------------------------
579  
580 =item encode_mimewords RAW, [OPTS]
581  
582 I<Function.>
583 Given a RAW string, try to find and encode all "unsafe" sequences
584 of characters:
585  
586 ### Encode a string with some unsafe "words":
587 $encoded = encode_mimewords("Me and \xABFran\xE7ois\xBB");
588  
589 Returns the encoded string.
590  
591 B<**>
592 RAW may be a Unicode string when Unicode/multibyte support is enabled
593 (see L<MIME::Charset/USE_ENCODE>).
594 Furthermore, RAW may be a reference to that returned
595 by L</decode_mimewords> on array context. In latter case "Charset"
596 option (see below) will be overridden (see also a note below).
597  
598 B<Note>:
599 B<*>
600 When RAW is an arrayref,
601 adjacent encoded-words (i.e. elements having non-ASCII charset element)
602 are concatenated. Then they are split taking
603 care of character boundaries of multibyte sequences when Unicode/multibyte
604 support is enabled.
605 Portions for unencoded data should include surrounding whitespace(s), or
606 they will be merged into adjoining encoded-word(s).
607  
608 Any arguments past the RAW string are taken to define a hash of options:
609  
610 =over 4
611  
612 =item Charset
613  
614 Encode all unsafe stuff with this charset. Default is 'ISO-8859-1',
615 a.k.a. "Latin-1".
616  
617 =item Detect7bit
618 B<**>
619  
620 When "Encoding" option (see below) is specified as C<"a"> and "Charset"
621 option is unknown, try to detect 7-bit charset on given RAW string.
622 Default is C<"YES">.
623 When Unicode/multibyte support is disabled,
624 this option will not have any effects
625 (see L<MIME::Charset/USE_ENCODE>).
626  
627 =item Encoding
628  
629 The encoding to use, C<"q"> or C<"b">.
630 B<**>
631 You may also specify ``special'' values: C<"a"> will automatically choose
632 recommended encoding to use (with charset conversion if alternative
633 charset is recommended: see L<MIME::Charset>);
634 C<"s"> will choose shorter one of either C<"q"> or C<"b">.
635 B<Note>:
636 B<*>
637 As of release 1.005, The default was changed from C<"q">
638 (the default on MIME::Words) to C<"a">.
639  
640 =item Field
641  
642 Name of the mail field this string will be used in.
643 B<**>
644 Length of mail field name will be considered in the first line of
645 encoded header.
646  
647 =item Folding
648 B<**>
649  
650 A Sequence to fold encoded lines. The default is C<"\n">.
651 If empty string C<""> is specified, encoded-words exceeding line length
652 (see L</MaxLineLen> below) will be split by SPACE.
653  
654 B<Note>:
655 B<*>
656 Though RFC 5322 (formerly RFC 2822) states that the lines in
657 Internet messages are delimited by CRLF (C<"\r\n">),
658 this module chose LF (C<"\n">) as a default to keep backward compatibility.
659 When you use the default, you might need converting newlines
660 before encoded headers are thrown into session.
661  
662 =item Mapping
663 B<**>
664  
665 Specify mappings actually used for charset names.
666 C<"EXTENDED"> uses extended mappings.
667 C<"STANDARD"> uses standardized strict mappings.
668 The default is C<"EXTENDED">.
669 When Unicode/multibyte support is disabled,
670 this option will not have any effects
671 (see L<MIME::Charset/USE_ENCODE>).
672  
673 =item MaxLineLen
674 B<**>
675  
676 Maximum line length excluding newline.
677 The default is 76.
678 Negative value means unlimited line length (as of release 1.012.3).
679  
680 =item Minimal
681 B<**>
682  
683 Takes care of natural word separators (i.e. whitespaces)
684 in the text to be encoded.
685 If C<"NO"> is specified, this module will encode whole text
686 (if encoding needed) not regarding whitespaces;
687 encoded-words exceeding line length will be split based only on their
688 lengths.
689 Default is C<"YES"> by which minimal portions of text are encoded.
690 If C<"DISPNAME"> is specified, portions including special characters
691 described in RFC5322 (formerly RFC2822, RFC822) address specification
692 (section 3.4) are also encoded.
693 This is useful for encoding display-name of address fields.
694  
695 B<Note>:
696 As of release 0.040, default has been changed to C<"YES"> to ensure
697 compatibility with MIME::Words.
698 On earlier releases, this option was fixed to be C<"NO">.
699  
700 B<Note>:
701 C<"DISPNAME"> option was introduced at release 1.012.
702  
703 =item Replacement
704 B<**>
705  
706 See L<MIME::Charset/Error Handling>.
707  
708 =back
709  
710 =cut
711  
712 92 ▶sub encode_mimewords {
Subroutine encode_mimewords: called CC=99 SCAR=50.0
Pod documented
713 92 my $words = shift;
714 92 my %params = @_;
715 92 my %Params = &_getparams(\%params,
716 YesNo => [qw(Detect7bit)],
717 Others => [qw(Charset Encoding Field Folding
718 Mapping MaxLineLen Minimal
719 Replacement)],
720 ToUpper => [qw(Charset Encoding Mapping Minimal
721 Replacement)],
722 );
723 croak "unsupported encoding ``$Params{Encoding}''"
724 92 ▶ unless $Params{Encoding} =~ /^[ABQS]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $Params{"Encoding"} =~ /^[ABQS]$/ 0 92 92
725 # newline and following WSP
726 92 my ($fwsbrk, $fwsspc);
727 92 ▶ if ($Params{Folding} =~ m/^([\r\n]*)([\t ]?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Params{'Folding'} =~ /^([\r\n]*)([\t ]?)$/) { } 92 0 92
728 92 $fwsbrk = $1;
729 92 ▶ $fwsspc = $2 || " ";
Condition50%1/2
$2 || ' '
l!l
092
MC/DC0%0/1
$2 || ' '
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
730 } else {
731 croak sprintf "illegal folding sequence ``\\x%*v02X''", '\\x',
732 0 $Params{Folding};
733 }
734 # charset objects
735 my $charsetobj = MIME::Charset->new($Params{Charset},
736 92 Mapping => $Params{Mapping});
737 92 my $ascii = MIME::Charset->new("US-ASCII", Mapping => 'STANDARD');
738 92 $ascii->encoder($ascii);
739 # lengths
740 my $firstlinelen = $Params{MaxLineLen} -
741 92 ▶ ($Params{Field}? length("$Params{Field}: "): 0);
Branch: 1 branch
BranchTrueFalseTotal
$Params{'Field'} ? : 5 87 92
742 92 my $maxrestlen = $Params{MaxLineLen} - length($fwsspc);
743 # minimal encoding flag
744 92 ▶ if (!$Params{Minimal}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $Params{'Minimal'}) { } 0 92 92
elsif (not $Params{'Minimal'} =~ /^(NO|DISPNAME)$/) { } 90 2 92
745 0 $Params{Minimal} = 'NO';
746 } elsif ($Params{Minimal} !~ /^(NO|DISPNAME)$/) {
747 90 $Params{Minimal} = 'YES';
748 }
749 # unsafe ASCII sequences
750 92 ▶ my $UNSAFEASCII = ($maxrestlen <= 1)?
Branch: 1 branch
BranchTrueFalseTotal
$maxrestlen <= 1 ? : 0 92 92
751 qr{(?: =\? )}ox:
752 qr{(?: =\? | [$PRINTABLE]{$Params{MaxLineLen}} )}x;
753 $UNSAFEASCII = qr{(?: $DISPNAMESPECIAL | $UNSAFEASCII )}x
754 92 ▶ if $Params{Minimal} eq 'DISPNAME';
Branch: 1 branch
BranchTrueFalseTotal
if $Params{"Minimal"} eq "DISPNAME" 2 90 92
755  
756 92 ▶ unless (ref($words) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $words eq "ARRAY") 68 24 92
757 # workaround for UTF-16* & UTF-32*: force UTF-8.
758 68 ▶ if ($charsetobj->as_string
Branch: 1 branch
BranchTrueFalseTotal
if ($charsetobj->as_string and $charsetobj->as_string =~ /$ASCIIINCOMPAT/) 24 44 68
Condition100%3/3
$charsetobj->as_string and $charsetobj->as_string =~ /$ASCIIINCOMPAT/
!ll&&!rl&&r
14324
MC/DC100%2/2
$charsetobj->as_string and $charsetobj->as_string =~ /$ASCIIINCOMPAT/
$charsetobj->as_string $charsetobj->as_string =~ /$ASCIIINCOMPAT/
Truth table100%3/3
A and B
A= $charsetobj->as_stringB= $charsetobj->as_string =~ /$ASCIIINCOMPAT/
ABresult
0X0
100
111
759 and $charsetobj->as_string =~ /$ASCIIINCOMPAT/) {
760 24 $words = _utf_to_unicode($charsetobj, $words);
761 24 $charsetobj = MIME::Charset->new('UTF-8');
762 }
763  
764 68 my @words = ();
765 # unfolding: normalize linear-white-spaces and orphan newlines.
766 68 ▶ $words =~ s/(?:[\r\n]+[\t ])*[\r\n]+([\t ]|\Z)/$1? " ": ""/eg;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 1 0 1
767 68 $words =~ s/[\r\n]+/ /g;
768 # split if required
769 68 ▶ if ($Params{Minimal} =~ /YES|DISPNAME/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Params{'Minimal'} =~ /YES|DISPNAME/) { } 68 0 68
770 68 my ($spc, $unsafe_last) = ('', 0);
771 68 foreach my $w (split(/([\t ]+)/, $words)) {
772 658 ▶ next unless scalar(@words) or length($w); # skip garbage
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @words or length $w 0 658 658
Condition66%2/3
scalar @words or length $w
l!l&&r!l&&!r
590680
MC/DC0%0/2
scalar @words or length $w
scalar @words length $w
Truth table66%2/3
A or B
A= scalar @wordsB= length $w
ABresult
1X1
011
000
773 658 ▶ if ($w =~ /[\t ]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w =~ /[\t ]/) 295 363 658
774 295 $spc = $w;
775 295 next;
776 }
777  
778 # workaround for ``ASCII transformation'' charsets
779 363 my $u = $w;
780 363 ▶ if ($charsetobj->as_string
Branch: 1 branch
BranchTrueFalseTotal
if ($charsetobj->as_string and $charsetobj->as_string =~ /$ASCIITRANS/) 6 357 363
Condition100%3/3
$charsetobj->as_string and $charsetobj->as_string =~ /$ASCIITRANS/
!ll&&!rl&&r
53526
MC/DC100%2/2
$charsetobj->as_string and $charsetobj->as_string =~ /$ASCIITRANS/
$charsetobj->as_string $charsetobj->as_string =~ /$ASCIITRANS/
Truth table100%3/3
A and B
A= $charsetobj->as_stringB= $charsetobj->as_string =~ /$ASCIITRANS/
ABresult
0X0
100
111
781 and $charsetobj->as_string =~ /$ASCIITRANS/) {
782 6 if (MIME::Charset::USE_ENCODE) {
783 6 ▶ if (is_utf8($w) or $w =~ /$WIDECHAR/) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_utf8($w) or $w =~ /$WIDECHAR/) { } 0 6 6
Condition33%1/3
&is_utf8($w) or $w =~ /$WIDECHAR/
l!l&&r!l&&!r
006
MC/DC0%0/2
&is_utf8($w) or $w =~ /$WIDECHAR/
&is_utf8($w) $w =~ /$WIDECHAR/
Truth table33%1/3
A or B
A= &is_utf8($w)B= $w =~ /$WIDECHAR/
ABresult
1X1
011
000
784 0 $w = $charsetobj->undecode($u);
785 } else {
786 6 $u = $charsetobj->decode($w);
787 }
788 } elsif ($w =~ /[+~]/) { #FIXME: for pre-Encode environment
789 $u = "x$w";
790 }
791 }
792 363 ▶ if (scalar(@words)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @words) { } 295 68 363
793 295 ▶ if (($w =~ /$NONPRINT|$UNSAFEASCII/ or $u ne $w) xor
Branch: 1 branch
BranchTrueFalseTotal
if ($w =~ /$NONPRINT|$UNSAFEASCII/ or $u ne $w xor $unsafe_last) { } 65 230 295
Condition100%7/7
$w =~ /$NONPRINT|$UNSAFEASCII/ or $u ne $w xor $unsafe_last
l&&rl&&!r!l&&r!l&&!r
392342191
$w =~ /$NONPRINT|$UNSAFEASCII/ || $u ne $w
l!l&&r!l&&!r
611233
MC/DC100%3/3
$w =~ /$NONPRINT|$UNSAFEASCII/ or $u ne $w xor $unsafe_last
$w =~ /$NONPRINT|$UNSAFEASCII/ $u ne $w $unsafe_last
Truth table83%5/6
A || B xor C
A= $w =~ /$NONPRINT|$UNSAFEASCII/B= $u ne $wC= $unsafe_last
ABCresult
1X10
0110
1X01
0101
0011
0000
794 $unsafe_last) {
795 65 ▶ if ($unsafe_last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unsafe_last) { } 42 23 65
796 42 push @words, $spc.$w;
797 } else {
798 23 $words[-1] .= $spc;
799 23 push @words, $w;
800 }
801 65 $unsafe_last = not $unsafe_last;
802 } else {
803 230 $words[-1] .= $spc.$w;
804 }
805 } else {
806 68 push @words, $spc.$w;
807 68 ▶ $unsafe_last =
Condition66%2/3
$w =~ /$NONPRINT|$UNSAFEASCII/ || $u ne $w
l!l&&r!l&&!r
48020
MC/DC50%1/2
$w =~ /$NONPRINT|$UNSAFEASCII/ || $u ne $w
$w =~ /$NONPRINT|$UNSAFEASCII/ $u ne $w
Truth table66%2/3
A || B
A= $w =~ /$NONPRINT|$UNSAFEASCII/B= $u ne $w
ABresult
1X1
011
000
808 ($w =~ /$NONPRINT|$UNSAFEASCII/ or $u ne $w);
809 }
810 363 $spc = '';
811 }
812 68 ▶ if ($spc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spc) 0 68 68
813 0 ▶ if (scalar(@words)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @words) { } 0 0 0
814 0 $words[-1] .= $spc;
815 } else { # only WSPs
816 0 push @words, $spc;
817 }
818 }
819 } else {
820 0 @words = ($words);
821 }
822 68 $words = [map { [$_, $Params{Charset}] } @words];
823 }
824  
825 # Translate / concatenate words.
826 92 my @triplets;
827 92 foreach (@$words) {
828 217 my ($s, $cset) = @$_;
829 217 ▶ next unless length($s);
Branch: 1 branch
BranchTrueFalseTotal
unless length $s 0 217 217
830 my $csetobj = MIME::Charset->new($cset || "",
831 217 ▶ Mapping => $Params{Mapping});
Condition100%2/2
$cset || ''
l!l
16948
MC/DC100%1/1
$cset || ''
$cset
Truth table100%2/2
A
A= $cset
Aresult
11
00
832  
833 # workaround for UTF-16*/UTF-32*: force UTF-8
834 217 ▶ if ($csetobj->as_string and $csetobj->as_string =~ /$ASCIIINCOMPAT/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($csetobj->as_string and $csetobj->as_string =~ /$ASCIIINCOMPAT/) 66 151 217
Condition100%3/3
$csetobj->as_string and $csetobj->as_string =~ /$ASCIIINCOMPAT/
!ll&&!rl&&r
4810366
MC/DC100%2/2
$csetobj->as_string and $csetobj->as_string =~ /$ASCIIINCOMPAT/
$csetobj->as_string $csetobj->as_string =~ /$ASCIIINCOMPAT/
Truth table100%3/3
A and B
A= $csetobj->as_stringB= $csetobj->as_string =~ /$ASCIIINCOMPAT/
ABresult
0X0
100
111
835 66 $s = _utf_to_unicode($csetobj, $s);
836 66 $csetobj = MIME::Charset->new('UTF-8');
837 }
838  
839 # determine charset and encoding
840 # try defaults only if 7-bit charset detection is not required
841 217 my $enc;
842 217 my $obj = $csetobj;
843 217 ▶ unless ($obj->as_string) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->as_string) 48 169 217
844 48 ▶ if ($Params{Encoding} ne "A" or $Params{Detect7bit} eq "NO" or
Branch: 1 branch
BranchTrueFalseTotal
if ($Params{"Encoding"} ne "A" or $Params{"Detect7bit"} eq "NO" or $s =~ /$UNSAFE/) 6 42 48
Condition50%3/6
$Params{"Encoding"} ne "A" or $Params{"Detect7bit"} eq "NO" or $s =~ /$UNSAFE/
l!l&&r!l&&!r
0642
$Params{"Encoding"} ne "A" or $Params{"Detect7bit"} eq "NO"
l!l&&r!l&&!r
0048
MC/DC33%1/3
$Params{"Encoding"} ne "A" or $Params{"Detect7bit"} eq "NO" or $s =~ /$UNSAFE/
$Params{"Encoding"} ne "A" $Params{"Detect7bit"} eq "NO" $s =~ /$UNSAFE/
Truth table50%2/4
A or B or C
A= $Params{"Encoding"} ne "A"B= $Params{"Detect7bit"} eq "NO"C= $s =~ /$UNSAFE/
ABCresult
1XX1
01X1
0011
0000
845 $s =~ /$UNSAFE/) {
846 6 $obj = $charsetobj;
847 }
848 }
849 ($s, $cset, $enc) =
850 $obj->header_encode($s,
851 Detect7bit => $Params{Detect7bit},
852 Replacement => $Params{Replacement},
853 217 Encoding => $Params{Encoding});
854 # Resolve 'S' encoding based on global length. See (*).
855 $enc = 'S'
856 if defined $enc and
857 ($Params{Encoding} eq 'S' or
858 217 ▶ $Params{Encoding} eq 'A' and $obj->header_encoding eq 'S');
Branch: 1 branch
BranchTrueFalseTotal
if defined $enc and $Params{"Encoding"} eq "S" || $Params{"Encoding"} eq "A" && $obj->header_encoding eq "S" 57 160 217
Condition88%8/9
defined $enc and $Params{"Encoding"} eq "S" || $Params{"Encoding"} eq "A" && $obj->header_encoding eq "S"
!ll&&!rl&&r
1065457
$Params{"Encoding"} eq "S" or $Params{"Encoding"} eq "A" and $obj->header_encoding eq "S"
l!l&&r!l&&!r
05754
$Params{"Encoding"} eq "A" and $obj->header_encoding eq "S"
!ll&&!rl&&r
183657
MC/DC75%3/4
defined $enc and $Params{"Encoding"} eq "S" || $Params{"Encoding"} eq "A" && $obj->header_encoding eq "S"
defined $enc $Params{"Encoding"} eq "S" $Params{"Encoding"} eq "A" $obj->header_encoding eq "S"
Truth table80%4/5
A and B or C and D
A= defined $encB= $Params{"Encoding"} eq "S"C= $Params{"Encoding"} eq "A"D= $obj->header_encoding eq "S"
ABCDresult
0XXX0
100X0
10100
11XX1
10111
859  
860 # pure ASCII
861 217 ▶ if ($cset eq "US-ASCII" and !$enc and $s =~ /$UNSAFEASCII/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cset eq 'US-ASCII' and not $enc and $s =~ /$UNSAFEASCII/) { } 7 210 217
Condition83%5/6
$cset eq "US-ASCII" and not $enc and $s =~ /$UNSAFEASCII/
!ll&&!rl&&r
111997
$cset eq "US-ASCII" and not $enc
!ll&&!rl&&r
1110106
MC/DC66%2/3
$cset eq "US-ASCII" and not $enc and $s =~ /$UNSAFEASCII/
$cset eq "US-ASCII" not $enc $s =~ /$UNSAFEASCII/
Truth table75%3/4
A and not(B) and C
A= $cset eq "US-ASCII"B= not $encC= $s =~ /$UNSAFEASCII/
ABCresult
0XX0
10X0
1100
1111
862 # pure ASCII with unsafe sequences should be encoded
863 7 ▶ $cset = $csetobj->output_charset ||
Condition83%5/6
$csetobj->output_charset || $charsetobj->output_charset || $ascii->output_charset
l!l&&r!l&&!r
520
$csetobj->output_charset or $charsetobj->output_charset
l!l&&r!l&&!r
322
MC/DC0%0/3
$csetobj->output_charset || $charsetobj->output_charset || $ascii->output_charset
$csetobj->output_charset $charsetobj->output_charset $ascii->output_charset
Truth table75%3/4
A or B || C
A= $csetobj->output_charsetB= $charsetobj->output_charsetC= $ascii->output_charset
ABCresult
1XX1
01X1
0011
0000
864 $charsetobj->output_charset ||
865 $ascii->output_charset;
866 $csetobj = MIME::Charset->new($cset,
867 7 Mapping => $Params{Mapping});
868 # Preserve original Encoding option unless it was 'A'.
869 $enc = ($Params{Encoding} eq 'A') ?
870 ($csetobj->header_encoding || 'Q') :
871 7 ▶ $Params{Encoding};
Branch: 1 branch
BranchTrueFalseTotal
$Params{'Encoding'} eq 'A' ? : 5 2 7
Condition100%2/2
$csetobj->header_encoding or "Q"
l!l
32
MC/DC100%1/1
$csetobj->header_encoding or "Q"
$csetobj->header_encoding
Truth table100%2/2
A
A= $csetobj->header_encoding
Aresult
11
00
872 } else {
873 $csetobj = MIME::Charset->new($cset,
874 210 Mapping => $Params{Mapping});
875 }
876  
877 # Now no charset translations are needed.
878 217 $csetobj->encoder($csetobj);
879  
880 # Concatenate adjacent ``words'' so that multibyte sequences will
881 # be handled safely.
882 # Note: Encoded-word and unencoded text must not adjoin without
883 # separating whitespace(s).
884 217 ▶ if (scalar(@triplets)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @triplets) 125 92 217
885 125 my ($last, $lastenc, $lastcsetobj) = @{$triplets[-1]};
886 125 ▶ if ($csetobj->decoder and
Branch: 3 branches
BranchTrueFalseTotal
if ($csetobj->decoder and ($lastcsetobj->as_string || '') eq $csetobj->as_string and ($lastenc || '') eq ($enc || '')) { } 26 99 125
elsif (not $lastenc and $enc and not $last =~ /[\r\n\t ]$/) { } 7 92 99
elsif ($lastenc and not $enc and not $s =~ /^[\r\n\t ]/) { } 16 102 118
Condition83%10/12
$csetobj->decoder and ($lastcsetobj->as_string || "") eq $csetobj->as_string and ($lastenc || "") eq ($enc || "")
!ll&&!rl&&r
97226
$csetobj->decoder and ($lastcsetobj->as_string || "") eq $csetobj->as_string
!ll&&!rl&&r
09728
$lastcsetobj->as_string || ''
l!l
1250
$lastenc || ''
l!l
1612
$enc || ''
l!l
1612
MC/DC33%1/3
$csetobj->decoder and ($lastcsetobj->as_string || "") eq $csetobj->as_string and ($lastenc || "") eq ($enc || "")
$csetobj->decoder $lastcsetobj->as_string $lastenc
MC/DC100%1/1
$enc || ''
$enc
Truth table50%2/4
A and B and C
A= $csetobj->decoderB= $lastcsetobj->as_stringC= $lastenc
ABCresult
0XX0
10X0
1100
1111
Truth table100%2/2
A
A= $enc
Aresult
11
00
887 ($lastcsetobj->as_string || "") eq $csetobj->as_string and
888 ($lastenc || "") eq ($enc || "")) {
889 26 $triplets[-1]->[0] .= $s;
890 26 next;
891 ▶ } elsif (!$lastenc and $enc and $last !~ /[\r\n\t ]$/) {
Condition83%5/6
not $lastenc and $enc and not $last =~ /[\r\n\t ]$/
!ll&&!rl&&r
67257
not $lastenc and $enc
!ll&&!rl&&r
67032
MC/DC66%2/3
not $lastenc and $enc and not $last =~ /[\r\n\t ]$/
not $lastenc $enc not $last =~ /[\r\n\t ]$/
Truth table75%3/4
not(A) and B and not(C)
A= not $lastencB= $encC= not $last =~ /[\r\n\t ]$/
ABCresult
0XX0
10X0
1100
1111
892 7 ▶ if ($last =~ /^(.*)([\r\n\t ])([$PRINTABLE]+)$/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($last =~ /^(.*)([\r\n\t ])([$PRINTABLE]+)$/s) { } 7 0 7
elsif ($lastcsetobj->as_string eq 'US-ASCII') { } 0 0 0
893 7 $triplets[-1]->[0] = $1.$2;
894 7 $s = $3.$s;
895 } elsif ($lastcsetobj->as_string eq "US-ASCII") {
896 0 $triplets[-1]->[0] .= $s;
897 0 $triplets[-1]->[1] = $enc;
898 0 $triplets[-1]->[2] = $csetobj;
899 0 next;
900 }
901 ▶ } elsif ($lastenc and !$enc and $s !~ /^[\r\n\t ]/) {
Condition100%6/6
$lastenc and not $enc and not $s =~ /^[\r\n\t ]/
!ll&&!rl&&r
265016
$lastenc and not $enc
!ll&&!rl&&r
25166
MC/DC100%3/3
$lastenc and not $enc and not $s =~ /^[\r\n\t ]/
$lastenc not $enc not $s =~ /^[\r\n\t ]/
Truth table100%4/4
A and not(B) and not(C)
A= $lastencB= not $encC= not $s =~ /^[\r\n\t ]/
ABCresult
0XX0
10X0
1100
1111
902 16 ▶ if ($s =~ /^([$PRINTABLE]+)([\r\n\t ])(.*)$/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s =~ /^([$PRINTABLE]+)([\r\n\t ])(.*)$/s) { } 16 0 16
elsif ($csetobj->as_string eq 'US-ASCII') { } 0 0 0
903 16 $triplets[-1]->[0] .= $1;
904 16 $s = $2.$3;
905 } elsif ($csetobj->as_string eq "US-ASCII") {
906 0 $triplets[-1]->[0] .= $s;
907 0 next;
908 }
909 }
910 }
911 191 push @triplets, [$s, $enc, $csetobj];
912 }
913  
914 # (*) Resolve 'S' encoding based on global length.
915 92 ▶ my @s_enc = grep { $_->[1] and $_->[1] eq 'S' } @triplets;
Condition100%3/3
$_->[1] && $_->[1] eq 'S'
!ll&&!rl&&r
885944
MC/DC100%2/2
$_->[1] && $_->[1] eq 'S'
$_->[1] $_->[1] eq 'S'
Truth table100%3/3
A && B
A= $_->[1]B= $_->[1] eq 'S'
ABresult
0X0
100
111
916 92 ▶ if (scalar @s_enc) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @s_enc) 43 49 92
917 43 my $enc;
918 43 ▶ my $b = scalar grep { $_->[1] and $_->[1] eq 'B' } @triplets;
Condition100%3/3
$_->[1] && $_->[1] eq 'B'
!ll&&!rl&&r
36447
MC/DC100%2/2
$_->[1] && $_->[1] eq 'B'
$_->[1] $_->[1] eq 'B'
Truth table100%3/3
A && B
A= $_->[1]B= $_->[1] eq 'B'
ABresult
0X0
100
111
919 43 ▶ my $q = scalar grep { $_->[1] and $_->[1] eq 'Q' } @triplets;
Condition66%2/3
$_->[1] && $_->[1] eq 'Q'
!ll&&!rl&&r
36510
MC/DC0%0/2
$_->[1] && $_->[1] eq 'Q'
$_->[1] $_->[1] eq 'Q'
Truth table66%2/3
A && B
A= $_->[1]B= $_->[1] eq 'Q'
ABresult
0X0
100
111
920 # 'A' chooses 'B' or 'Q' when all other encoded-words have same enc.
921 43 ▶ if ($Params{Encoding} eq 'A' and $b and ! $q) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Params{'Encoding'} eq 'A' and $b and not $q) { } 7 36 43
elsif ($Params{'Encoding'} eq 'A' and not $b and $q) { } 0 36 36
Condition66%4/6
$Params{"Encoding"} eq "A" and $b and not $q
!ll&&!rl&&r
3607
$Params{"Encoding"} eq "A" and $b
!ll&&!rl&&r
0367
MC/DC33%1/3
$Params{"Encoding"} eq "A" and $b and not $q
$Params{"Encoding"} eq "A" $b not $q
Truth table50%2/4
A and B and not(C)
A= $Params{"Encoding"} eq "A"B= $bC= not $q
ABCresult
0XX0
10X0
1100
1111
922 7 $enc = 'B';
923 ▶ } elsif ($Params{Encoding} eq 'A' and ! $b and $q) {
Condition33%2/6
$Params{"Encoding"} eq "A" and not $b and $q
!ll&&!rl&&r
0360
$Params{"Encoding"} eq "A" and not $b
!ll&&!rl&&r
0036
MC/DC0%0/3
$Params{"Encoding"} eq "A" and not $b and $q
$Params{"Encoding"} eq "A" not $b $q
Truth table25%1/4
A and not(B) and C
A= $Params{"Encoding"} eq "A"B= not $bC= $q
ABCresult
0XX0
10X0
1100
1111
924 0 $enc = 'Q';
925 # Otherwise, assuming 'Q', when characters to be encoded are more than
926 # 6th of total (plus a little fraction), 'B' will win.
927 # Note: This might give 'Q' so great advantage...
928 } else {
929 36 my @no_enc = grep { ! $_->[1] } @triplets;
930 36 my $total = length join('', map { $_->[0] } (@no_enc, @s_enc));
931 36 my $q = scalar(() = join('', map { $_->[0] } @s_enc) =~
932 m{[^- !*+/0-9A-Za-z]}g);
933 36 ▶ if ($total + 8 < $q * 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total + 8 < $q * 6) { } 21 15 36
934 21 $enc = 'B';
935 } else {
936 15 $enc = 'Q';
937 }
938 }
939 43 foreach (@triplets) {
940 87 ▶ $_->[1] = $enc if $_->[1] and $_->[1] eq 'S';
Branch: 1 branch
BranchTrueFalseTotal
if $_->[1] and $_->[1] eq "S" 44 43 87
Condition100%3/3
$_->[1] and $_->[1] eq "S"
!ll&&!rl&&r
36744
MC/DC100%2/2
$_->[1] and $_->[1] eq "S"
$_->[1] $_->[1] eq "S"
Truth table100%3/3
A and B
A= $_->[1]B= $_->[1] eq "S"
ABresult
0X0
100
111
941 }
942 }
943  
944 # chop leading FWS
945 92 ▶ while (scalar(@triplets) and $triplets[0]->[0] =~ s/^[\r\n\t ]+//) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @triplets and $triplets[0][0] =~ s/^[\r\n\t ]+//) 0 92 92
Condition33%1/3
scalar @triplets and $triplets[0][0] =~ s/^[\r\n\t ]+//
!ll&&!rl&&r
0920
MC/DC0%0/2
scalar @triplets and $triplets[0][0] =~ s/^[\r\n\t ]+//
scalar @triplets $triplets[0][0] =~ s/^[\r\n\t ]+//
Truth table33%1/3
A and B
A= scalar @tripletsB= $triplets[0][0] =~ s/^[\r\n\t ]+//
ABresult
0X0
100
111
946 0 ▶ shift @triplets unless length($triplets[0]->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless length $triplets[0][0] 0 0 0
947 }
948  
949 # Split long ``words''.
950 92 my @splitwords;
951 my $restlen;
952 92 ▶ if ($Params{MaxLineLen} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Params{'MaxLineLen'} < 0) { } 0 92 92
953 0 @splitwords = @triplets;
954 } else {
955 92 $restlen = $firstlinelen;
956 92 foreach (@triplets) {
957 191 my ($s, $enc, $csetobj) = @$_;
958  
959 191 my @s = &_split($s, $enc, $csetobj, $restlen, $maxrestlen);
960 191 push @splitwords, @s;
961 191 my ($last, $lastenc, $lastcsetobj) = @{$s[-1]};
962 191 my $lastlen;
963 191 ▶ if ($lastenc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastenc) { } 103 88 191
964 103 $lastlen = $lastcsetobj->encoded_header_len($last, $lastenc);
965 } else {
966 88 $lastlen = length($last);
967 }
968 191 ▶ $restlen = $maxrestlen if scalar @s > 1; # has split; new line(s) fed
Branch: 1 branch
BranchTrueFalseTotal
if scalar @s > 1 78 113 191
969 191 $restlen -= $lastlen;
970 191 ▶ $restlen = $maxrestlen if $restlen <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $restlen <= 1 4 187 191
971 }
972 }
973  
974 # Do encoding.
975 92 my @lines;
976 92 $restlen = $firstlinelen;
977 92 foreach (@splitwords) {
978 285 my ($str, $encoding, $charsetobj) = @$_;
979 285 ▶ next unless length($str);
Branch: 1 branch
BranchTrueFalseTotal
unless length $str 0 285 285
980  
981 285 my $s;
982 285 ▶ if (!$encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $encoding) { } 137 148 285
983 137 $s = $str;
984 } else {
985 148 $s = encode_mimeword($str, $encoding, $charsetobj);
986 }
987  
988 285 ▶ my $spc = (scalar(@lines) and $lines[-1] =~ /[\r\n\t ]$/ or
Branch: 1 branch
BranchTrueFalseTotal
scalar @lines && $lines[-1] =~ /[\r\n\t ]$/ || $s =~ /^[\r\n\t ]/ ? : 147 138 285
Condition100%6/6
scalar @lines and $lines[-1] =~ /[\r\n\t ]$/ or $s =~ /^[\r\n\t ]/
l!l&&r!l&&!r
32115138
scalar @lines and $lines[-1] =~ /[\r\n\t ]$/
!ll&&!rl&&r
9216132
MC/DC100%3/3
scalar @lines and $lines[-1] =~ /[\r\n\t ]$/ or $s =~ /^[\r\n\t ]/
scalar @lines $lines[-1] =~ /[\r\n\t ]$/ $s =~ /^[\r\n\t ]/
Truth table80%4/5
A and B or C
A= scalar @linesB= $lines[-1] =~ /[\r\n\t ]$/C= $s =~ /^[\r\n\t ]/
ABCresult
11X1
0X11
1011
0X00
1000
989 $s =~ /^[\r\n\t ]/)? '': ' ';
990 285 ▶ if (!scalar(@lines)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not scalar @lines) { } 92 193 285
elsif ($Params{'MaxLineLen'} < 0) { } 0 193 193
elsif (length $lines[-1] . $spc . $s <= $restlen) { } 95 98 193
991 92 push @lines, $s;
992 } elsif ($Params{MaxLineLen} < 0) {
993 0 $lines[-1] .= $spc.$s;
994 } elsif (length($lines[-1].$spc.$s) <= $restlen) {
995 95 $lines[-1] .= $spc.$s;
996 } else {
997 98 ▶ if ($lines[-1] =~ s/([\r\n\t ]+)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[-1] =~ s/([\r\n\t ]+)$//) 4 94 98
998 4 $s = $1.$s;
999 }
1000 98 $s =~ s/^[\r\n]*[\t ]//; # strip only one WSP replaced by FWS
1001 98 push @lines, $s;
1002 98 $restlen = $maxrestlen;
1003 }
1004 }
1005  
1006 92 join($fwsbrk.$fwsspc, @lines);
1007 }
1008  
1009 #------------------------------
1010  
1011 # _split RAW, ENCODING, CHARSET_OBJECT, ROOM_OF_FIRST_LINE, MAXRESTLEN
1012 # Private: used by encode_mimewords() to split a string into
1013 # (encoded or non-encoded) words.
1014 # Returns an array of arrayrefs [SUBSTRING, ENCODING, CHARSET].
1015 191 ▶sub _split {
Subroutine _split: called CC=15 SCAR=28.9
1016 191 my $str = shift;
1017 191 my $encoding = shift;
1018 191 my $charset = shift;
1019 191 my $restlen = shift;
1020 191 my $maxrestlen = shift;
1021  
1022 191 ▶ if (!$charset->as_string or $charset->as_string eq '8BIT') {# Undecodable.
Branch: 1 branch
BranchTrueFalseTotal
if (not $charset->as_string or $charset->as_string eq "8BIT") 0 191 191
Condition33%1/3
not $charset->as_string or $charset->as_string eq "8BIT"
l!l&&r!l&&!r
00191
MC/DC0%0/2
not $charset->as_string or $charset->as_string eq "8BIT"
not $charset->as_string $charset->as_string eq "8BIT"
Truth table33%1/3
not(A) or B
A= not $charset->as_stringB= $charset->as_string eq "8BIT"
ABresult
1X1
011
000
1023 0 $str =~ s/[\r\n]+[\t ]*|\x00/ /g; # Eliminate hostile characters.
1024 0 return ([$str, undef, $charset]);
1025 }
1026 191 ▶ if (!$encoding and $charset->as_string eq 'US-ASCII') { # Pure ASCII.
Branch: 1 branch
BranchTrueFalseTotal
if (not $encoding and $charset->as_string eq "US-ASCII") 88 103 191
Condition66%2/3
not $encoding and $charset->as_string eq "US-ASCII"
!ll&&!rl&&r
103088
MC/DC50%1/2
not $encoding and $charset->as_string eq "US-ASCII"
not $encoding $charset->as_string eq "US-ASCII"
Truth table66%2/3
not(A) and B
A= not $encodingB= $charset->as_string eq "US-ASCII"
ABresult
0X0
100
111
1027 88 return &_split_ascii($str, $restlen, $maxrestlen);
1028 }
1029 103 ▶ if (!$charset->decoder and MIME::Charset::USE_ENCODE) { # Unsupported.
Branch: 1 branch
BranchTrueFalseTotal
if (not $charset->decoder and "Encode") 0 103 103
Condition50%1/2
not $charset->decoder and "Encode"
!ll
1030
MC/DC0%0/1
not $charset->decoder and "Encode"
not $charset->decoder
Truth table50%1/2
not(A)
A= not $charset->decoder
Aresult
00
11
1030 0 return ([$str, $encoding, $charset]);
1031 }
1032  
1033 103 my (@splitwords, $ustr, $first);
1034 103 ▶ while (length($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str) 148 1 149
1035 148 ▶ if ($charset->encoded_header_len($str, $encoding) <= $restlen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charset->encoded_header_len($str, $encoding) <= $restlen) 102 46 148
1036 102 push @splitwords, [$str, $encoding, $charset];
1037 102 last;
1038 }
1039 46 $ustr = $str;
1040 46 ▶ if (!(is_utf8($ustr) or $ustr =~ /$WIDECHAR/) and
Branch: 1 branch
BranchTrueFalseTotal
if (not &is_utf8($ustr) || $ustr =~ /$WIDECHAR/ and "Encode") 46 0 46
Condition40%2/5
not &is_utf8($ustr) || $ustr =~ /$WIDECHAR/ and "Encode"
!ll
046
&is_utf8($ustr) || $ustr =~ /$WIDECHAR/
l!l&&r!l&&!r
0046
MC/DC0%0/2
not &is_utf8($ustr) || $ustr =~ /$WIDECHAR/ and "Encode"
&is_utf8($ustr) $ustr =~ /$WIDECHAR/
Truth table33%1/3
not(A || B)
A= &is_utf8($ustr)B= $ustr =~ /$WIDECHAR/
ABresult
1X0
010
001
1041 MIME::Charset::USE_ENCODE) {
1042 46 $ustr = $charset->decode($ustr);
1043 }
1044 46 ($first, $str) = &_clip_unsafe($ustr, $encoding, $charset, $restlen);
1045 # retry splitting if failed
1046 46 ▶ if ($first and !$str and
Branch: 1 branch
BranchTrueFalseTotal
if ($first and not $str and $maxrestlen < $charset->encoded_header_len($first, $encoding)) 4 42 46
Condition83%5/6
$first and not $str and $maxrestlen < $charset->encoded_header_len($first, $encoding)
!ll&&!rl&&r
4114
$first and not $str
!ll&&!rl&&r
0415
MC/DC66%2/3
$first and not $str and $maxrestlen < $charset->encoded_header_len($first, $encoding)
$first not $str $maxrestlen < $charset->encoded_header_len($first, $encoding)
Truth table75%3/4
A and not(B) and C
A= $firstB= not $strC= $maxrestlen < $charset->encoded_header_len($first, $encoding)
ABCresult
0XX0
10X0
1100
1111
1047 $maxrestlen < $charset->encoded_header_len($first, $encoding)) {
1048 4 ($first, $str) = &_clip_unsafe($ustr, $encoding, $charset,
1049 $maxrestlen);
1050 }
1051 46 push @splitwords, [$first, $encoding, $charset];
1052 46 $restlen = $maxrestlen;
1053 }
1054 103 return @splitwords;
1055 }
1056  
1057 # _split_ascii RAW, ROOM_OF_FIRST_LINE, MAXRESTLEN
1058 # Private: used by encode_mimewords() to split an US-ASCII string into
1059 # (encoded or non-encoded) words.
1060 # Returns an array of arrayrefs [SUBSTRING, undef, "US-ASCII"].
1061 88 ▶sub _split_ascii {
Subroutine _split_ascii: called CC=12 SCAR=25.1
1062 88 my $s = shift;
1063 88 my $restlen = shift;
1064 88 my $maxrestlen = shift;
1065 88 ▶ $restlen ||= $maxrestlen;
Condition33%1/3
$restlen ||= $maxrestlen
l!l&&r!l&&!r
8800
MC/DC0%0/2
$restlen ||= $maxrestlen
$restlen $maxrestlen
Truth table33%1/3
A ||= B
A= $restlenB= $maxrestlen
ABresult
1X1
011
000
1066  
1067 88 my @splitwords;
1068 88 my $ascii = MIME::Charset->new("US-ASCII", Mapping => 'STANDARD');
1069 88 foreach my $line (split(/(?:[\t ]*[\r\n]+)+/, $s)) {
1070 88 my $spc = '';
1071 88 foreach my $word (split(/([\t ]+)/, $line)) {
1072 # skip first garbage
1073 1006 ▶ next unless scalar(@splitwords) or defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @splitwords or defined $word 0 1006 1006
Condition66%2/3
scalar @splitwords or defined $word
l!l&&r!l&&!r
7862200
MC/DC0%0/2
scalar @splitwords or defined $word
scalar @splitwords defined $word
Truth table66%2/3
A or B
A= scalar @splitwordsB= defined $word
ABresult
1X1
011
000
1074 1006 ▶ if ($word =~ /[\t ]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /[\t ]/) 475 531 1006
1075 475 $spc = $word;
1076 475 next;
1077 }
1078  
1079 531 my $cont = $spc.$word;
1080 531 my $elen = length($cont);
1081 531 ▶ next unless $elen;
Branch: 1 branch
BranchTrueFalseTotal
unless $elen 66 465 531
1082 465 ▶ if (scalar(@splitwords)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @splitwords) { } 377 88 465
1083 # Concatenate adjacent words so that encoded-word and
1084 # unencoded text will adjoin with separating whitespace.
1085 377 ▶ if ($elen <= $restlen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elen <= $restlen) { } 328 49 377
1086 328 $splitwords[-1]->[0] .= $cont;
1087 328 $restlen -= $elen;
1088 } else {
1089 49 push @splitwords, [$cont, undef, $ascii];
1090 49 $restlen = $maxrestlen - $elen;
1091 }
1092 } else {
1093 88 push @splitwords, [$cont, undef, $ascii];
1094 88 $restlen -= $elen;
1095 }
1096 465 $spc = '';
1097 }
1098 88 ▶ if ($spc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spc) 32 56 88
1099 32 ▶ if (scalar(@splitwords)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @splitwords) { } 32 0 32
1100 32 $splitwords[-1]->[0] .= $spc;
1101 32 $restlen -= length($spc);
1102 } else { # only WSPs
1103 0 push @splitwords, [$spc, undef, $ascii];
1104 0 $restlen = $maxrestlen - length($spc);
1105 }
1106 }
1107 }
1108 88 return @splitwords;
1109 }
1110  
1111 # _clip_unsafe UNICODE, ENCODING, CHARSET_OBJECT, ROOM_OF_FIRST_LINE
1112 # Private: used by encode_mimewords() to bite off one encodable
1113 # ``word'' from a Unicode string.
1114 # Note: When Unicode/multibyte support is not enabled, character
1115 # boundaries of multibyte string shall be broken!
1116 50 ▶sub _clip_unsafe {
Subroutine _clip_unsafe: called CC=7 SCAR=19.8
1117 50 my $ustr = shift;
1118 50 my $encoding = shift;
1119 50 my $charset = shift;
1120 50 my $restlen = shift;
1121 50 ▶ return ("", "") unless length($ustr);
Branch: 1 branch
BranchTrueFalseTotal
unless length $ustr 0 50 50
1122  
1123 # Seek maximal division point.
1124 50 my ($shorter, $longer) = (0, length($ustr));
1125 50 ▶ while ($shorter < $longer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shorter < $longer) 239 50 289
1126 239 my $cur = ($shorter + $longer + 1) >> 1;
1127 239 my $enc = substr($ustr, 0, $cur);
1128 239 if (MIME::Charset::USE_ENCODE ne '') {
1129 239 $enc = $charset->undecode($enc);
1130 }
1131 239 my $elen = $charset->encoded_header_len($enc, $encoding);
1132 239 ▶ if ($elen <= $restlen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elen <= $restlen) { } 129 110 239
1133 129 $shorter = $cur;
1134 } else {
1135 110 $longer = $cur - 1;
1136 }
1137 }
1138  
1139 # Make sure that combined characters won't be divided.
1140 50 my ($fenc, $renc);
1141 50 my $max = length($ustr);
1142 50 while (1) {
1143 50 $@ = '';
1144 50 eval {
1145 50 ($fenc, $renc) =
1146 (substr($ustr, 0, $shorter), substr($ustr, $shorter));
1147 50 if (MIME::Charset::USE_ENCODE ne '') {
1148 # FIXME: croak if $renc =~ /^\p{M}/
1149 50 $fenc = $charset->undecode($fenc, FB_CROAK());
1150 50 $renc = $charset->undecode($renc, FB_CROAK());
1151 }
1152 };
1153 50 ▶ last unless ($@);
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 50 0 50
1154  
1155 0 $shorter++;
1156 0 ▶ unless ($shorter < $max) { # Unencodable character(s) may be included.
Branch: 1 branch
BranchTrueFalseTotal
unless ($shorter < $max) 0 0 0
1157 0 return ($charset->undecode($ustr), "");
1158 }
1159 }
1160  
1161 50 ▶ if (length($fenc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $fenc) { } 46 4 50
1162 46 return ($fenc, $renc);
1163 } else {
1164 4 return ($renc, "");
1165 }
1166 }
1167  
1168 #------------------------------
1169  
1170 # _getparams HASHREF, OPTS
1171 # Private: used to get option parameters.
1172 164 ▶sub _getparams {
Subroutine _getparams: called CC=14 SCAR=26.7
1173 164 my $params = shift;
1174 164 my %params = @_;
1175 164 my %Params;
1176 my %GotParams;
1177 164 foreach my $k (qw(NoDefault YesNo Others Obsoleted ToUpper)) {
1178 820 ▶ $Params{$k} = $params{$k} || [];
Condition100%2/2
$params{$k} || []
l!l
636184
MC/DC100%1/1
$params{$k} || []
$params{$k}
Truth table100%2/2
A
A= $params{$k}
Aresult
11
00
1179 }
1180 164 foreach my $k (keys %$params) {
1181 342 my $supported = 0;
1182 342 foreach my $i (@{$Params{NoDefault}}, @{$Params{YesNo}},
1183 342 @{$Params{Others}}, @{$Params{Obsoleted}}) {
1184 1306 ▶ if (lc $i eq lc $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $i eq lc $k) 342 964 1306
1185 342 $GotParams{$i} = $params->{$k};
1186 342 $supported = 1;
1187 342 last;
1188 }
1189 }
1190 342 ▶ carp "unknown or deprecated option ``$k''" unless $supported;
Branch: 1 branch
BranchTrueFalseTotal
unless $supported 0 342 342
1191 }
1192 # get defaults
1193 164 foreach my $i (@{$Params{YesNo}}, @{$Params{Others}}) {
1194 972 ▶ $GotParams{$i} = $Config->{$i} unless defined $GotParams{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $GotParams{$i} 684 288 972
1195 }
1196 # yesno params
1197 164 foreach my $i (@{$Params{YesNo}}) {
1198 164 ▶ if (!$GotParams{$i} or uc $GotParams{$i} eq "NO") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $GotParams{$i} or uc $GotParams{$i} eq 'NO') { } 0 164 164
Condition33%1/3
not $GotParams{$i} or uc $GotParams{$i} eq "NO"
l!l&&r!l&&!r
00164
MC/DC0%0/2
not $GotParams{$i} or uc $GotParams{$i} eq "NO"
not $GotParams{$i} uc $GotParams{$i} eq "NO"
Truth table33%1/3
not(A) or B
A= not $GotParams{$i}B= uc $GotParams{$i} eq "NO"
ABresult
1X1
011
000
1199 0 $GotParams{$i} = "NO";
1200 } else {
1201 164 $GotParams{$i} = "YES";
1202 }
1203 }
1204 # normalize case
1205 164 foreach my $i (@{$Params{ToUpper}}) {
1206 604 ▶ $GotParams{$i} &&= uc $GotParams{$i};
Condition66%2/3
$GotParams{$i} &&= uc $GotParams{$i}
!ll&&!rl&&r
670537
MC/DC50%1/2
$GotParams{$i} &&= uc $GotParams{$i}
$GotParams{$i} uc $GotParams{$i}
Truth table66%2/3
A &&= B
A= $GotParams{$i}B= uc $GotParams{$i}
ABresult
0X0
100
111
1207 }
1208 164 return %GotParams;
1209 }
1210  
1211 #------------------------------
1212  
1213 =back
1214  
1215 =head2 Configuration Files
1216 B<**>
1217  
1218 Built-in defaults of option parameters for L</decode_mimewords>
1219 (except 'Charset' option) and
1220 L</encode_mimewords> can be overridden by configuration files:
1221 F<MIME/Charset/Defaults.pm> and F<MIME/EncWords/Defaults.pm>.
1222 For more details read F<MIME/EncWords/Defaults.pm.sample>.
1223  
1224 =head1 VERSION
1225  
1226 Consult C<$VERSION> variable.
1227  
1228 Development versions of this module may be found at
1229 L<http://hatuka.nezumi.nu/repos/MIME-EncWords/>.
1230  
1231 =head1 SEE ALSO
1232  
1233 L<MIME::Charset>,
1234 L<MIME::Tools>
1235  
1236 =head1 AUTHORS
1237  
1238 The original version of function decode_mimewords() is derived from
1239 L<MIME::Words> module that was written by:
1240 Eryq (F<eryq@zeegee.com>), ZeeGee Software Inc (F<http://www.zeegee.com>).
1241 David F. Skoll (dfs@roaringpenguin.com) http://www.roaringpenguin.com
1242  
1243 Other stuff are rewritten or added by:
1244 Hatuka*nezumi - IKEDA Soji <hatuka(at)nezumi.nu>.
1245  
1246 This program is free software; you can redistribute
1247 it and/or modify it under the same terms as Perl itself.
1248  
1249 =cut
1250  
1251 5 1;
« Encode/MIME/EncWords.pm ↑ Summary