MIME/EncWords.pm

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