| 1 | # -*- perl -*- | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Encode::MIME::EncWords; | ||||||||||||||||||||||||||||
| 4 | 2 | require 5.007003; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use MIME::EncWords; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Default of options | ||||||||||||||||||||||||||||
| 14 | my $Config = { | ||||||||||||||||||||||||||||
| 15 | Charset => 'UTF-8', | ||||||||||||||||||||||||||||
| 16 | # Encoding => specified by each subclass. | ||||||||||||||||||||||||||||
| 17 | # Folding => fixes to "\n". | ||||||||||||||||||||||||||||
| 18 | # Replacement => given by encode()/decode(). | ||||||||||||||||||||||||||||
| 19 | # others => derived from MIME::EncWords: | ||||||||||||||||||||||||||||
| 20 | 2 | map { ($_ => $MIME::EncWords::Config->{$_}) } | |||||||||||||||||||||||||||
| 21 | qw(Detect7bit Field Mapping MaxLineLen Minimal) | ||||||||||||||||||||||||||||
| 22 | }; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | $Encode::Encoding{'MIME-EncWords'} = bless { | |||||||||||||||||||||||||||
| 25 | Encoding => 'A', | ||||||||||||||||||||||||||||
| 26 | Name => 'MIME-EncWords', | ||||||||||||||||||||||||||||
| 27 | } => __PACKAGE__; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | $Encode::Encoding{'MIME-EncWords-B'} = bless { | |||||||||||||||||||||||||||
| 30 | Encoding => 'B', | ||||||||||||||||||||||||||||
| 31 | Name => 'MIME-EncWords-B', | ||||||||||||||||||||||||||||
| 32 | } => __PACKAGE__; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | $Encode::Encoding{'MIME-EncWords-Q'} = bless { | |||||||||||||||||||||||||||
| 35 | Encoding => 'Q', | ||||||||||||||||||||||||||||
| 36 | Name => 'MIME-EncWords-Q', | ||||||||||||||||||||||||||||
| 37 | } => __PACKAGE__; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | $Encode::Encoding{'MIME-EncWords-ISO_2022_JP'} = bless { | |||||||||||||||||||||||||||
| 40 | Charset => 'ISO-2022-JP', | ||||||||||||||||||||||||||||
| 41 | Encoding => 'B', | ||||||||||||||||||||||||||||
| 42 | Name => 'MIME-EncWords-ISO_2022_JP', | ||||||||||||||||||||||||||||
| 43 | } => __PACKAGE__; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | use base qw(Encode::Encoding); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub needs_lines { 1 } | ||||||||||||||||||||||||||
|
Subroutine
needs_lines: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub perlio_ok { 0 } | ||||||||||||||||||||||||||
|
Subroutine
perlio_ok: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 11 | ▶ | sub decode($$;$) { | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=27 SCAR=51.2
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 11 | my ($obj, $str, $chk) = @_; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 11 | ▶ | my %opts = map { ($_ => ($obj->{$_} || $Config->{$_})) } | ||||||||||||||||||||||||||
|
Condition33%1/3
$obj->{$_} || $Config->{$_}
MC/DC0%0/2
$obj->{$_} || $Config->{$_}
$obj->{$_}
$Config->{$_}
Truth table33%1/3
A || B
A= $obj->{$_}B= $Config->{$_}
| |||||||||||||||||||||||||||||
| 54 | qw(Detect7bit Mapping); | ||||||||||||||||||||||||||||
| 55 | 11 | ▶ | $chk = 0 if ref $chk; # coderef not supported. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 11 | ▶ | my $repl = ($chk & 4) ? ($chk & ~4 | 1) : $chk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 11 | local $@; | |||||||||||||||||||||||||||
| 59 | 11 | my $skip = 0; # for RETURN_ON_ERR | |||||||||||||||||||||||||||
| 60 | 11 | my $ret = undef; | |||||||||||||||||||||||||||
| 61 | 11 | pos($str) = 0; | |||||||||||||||||||||||||||
| 62 | 11 | foreach my $line ( | |||||||||||||||||||||||||||
| 63 | $str =~ m{ \G (.*?) (?:\r\n|[\r\n]) (?![ \t]) }cgsx, | ||||||||||||||||||||||||||||
| 64 | substr($str, pos($str)) | ||||||||||||||||||||||||||||
| 65 | ) { | ||||||||||||||||||||||||||||
| 66 | 32 | ▶ | if (defined $ret) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 21 | ▶ | $ret .= "\n" unless $skip; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||||||||||
| 69 | 11 | $ret = ''; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 32 | ▶ | if ($skip) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $_[1] .= "\n"; | |||||||||||||||||||||||||||
| 73 | 0 | $_[1] .= $line; | |||||||||||||||||||||||||||
| 74 | 0 | next; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 32 | ▶ | next unless length $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 24 | my @words = MIME::EncWords::decode_mimewords($line, %opts); | |||||||||||||||||||||||||||
| 79 | 24 | ▶ | if ($@) { # broken MIME encoding. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | croak $@ if $chk & 1; # DIE_ON_ERR | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | carp $@ if $chk & 2; # WARN_ON_ERR | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ($chk & 4) { # RETURN_ON_ERR | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | $_[1] = $line; | |||||||||||||||||||||||||||
| 84 | 0 | $skip = 1; | |||||||||||||||||||||||||||
| 85 | 0 | next; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 24 | ▶ | for (my $i = 0; $i <= $#words; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 58 | my $word = $words[$i]; | |||||||||||||||||||||||||||
| 90 | my $cset = MIME::Charset->new(($word->[1] || 'US-ASCII'), | ||||||||||||||||||||||||||||
| 91 | 58 | ▶ | Mapping => $opts{Mapping}); | ||||||||||||||||||||||||||
|
Condition100%2/2
$word->[1] || 'US-ASCII'
MC/DC100%1/1
$word->[1] || 'US-ASCII'
$word->[1]
Truth table100%2/2
A
A= $word->[1]
| |||||||||||||||||||||||||||||
| 92 | 58 | ▶ | if (! $cset->decoder) { # unknown charset or ``8BIT''. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | $@ = 'Unknown charset "'.$cset->as_string.'"'; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | croak $@ if $chk & 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | carp $@ if $chk & 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ($chk & 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | # already decoded... re-encoding | ||||||||||||||||||||||||||||
| 98 | 0 | $_[1] = | |||||||||||||||||||||||||||
| 99 | MIME::EncWords::encode_mimewords([splice @words, $i], | ||||||||||||||||||||||||||||
| 100 | Encoding => 'B', | ||||||||||||||||||||||||||||
| 101 | Folding => '', | ||||||||||||||||||||||||||||
| 102 | MaxLineLen => -1); | ||||||||||||||||||||||||||||
| 103 | 0 | $skip = 1; | |||||||||||||||||||||||||||
| 104 | 0 | last; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 0 | $ret .= Encode::decode("ISO-8859-1", $word->[0], 0); #FIXME | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | next; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 58 | eval { | |||||||||||||||||||||||||||
| 111 | 58 | $ret .= $cset->decode($word->[0], $repl); | |||||||||||||||||||||||||||
| 112 | }; | ||||||||||||||||||||||||||||
| 113 | 58 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | $@ =~ s/ at .+? line \d+[.\n]*$//; | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | croak $@ if $chk & 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | carp $@ if $chk & 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ($chk & 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | # already decoded... re-encoding | ||||||||||||||||||||||||||||
| 119 | 0 | $_[1] = | |||||||||||||||||||||||||||
| 120 | MIME::EncWords::encode_mimewords([splice @words, $i], | ||||||||||||||||||||||||||||
| 121 | Encoding => 'B', | ||||||||||||||||||||||||||||
| 122 | Folding => '', | ||||||||||||||||||||||||||||
| 123 | MaxLineLen => -1); | ||||||||||||||||||||||||||||
| 124 | 0 | $skip = 1; | |||||||||||||||||||||||||||
| 125 | 0 | last; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 11 | ▶ | if ($chk & 4) { # RETURN_ON_ERR | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | $_[1] = '' unless $skip; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | } elsif ($chk) { # ! LEAVE_SRC | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $_[1] = $ret unless $chk & 8; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 11 | return $ret; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 18 | ▶ | sub encode($$;$) { | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=20 SCAR=38.7
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 18 | my ($obj, $str, $chk) = @_; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 18 | ▶ | my %opts = map { ($_ => ($obj->{$_} || $Config->{$_})) } | ||||||||||||||||||||||||||
|
Condition100%3/3
$obj->{$_} || $Config->{$_}
MC/DC100%2/2
$obj->{$_} || $Config->{$_}
$obj->{$_}
$Config->{$_}
Truth table100%3/3
A || B
A= $obj->{$_}B= $Config->{$_}
| |||||||||||||||||||||||||||||
| 143 | qw(Charset Detect7bit Encoding Field Mapping MaxLineLen Minimal); | ||||||||||||||||||||||||||||
| 144 | 18 | ▶ | $opts{Charset} ||= 'UTF-8'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"Charset"} ||= "UTF-8"
MC/DC0%0/1
$opts{"Charset"} ||= "UTF-8"
$opts{"Charset"}
Truth table50%1/2
A
A= $opts{"Charset"}
| |||||||||||||||||||||||||||||
| 145 | 18 | $opts{Folding} = "\n"; | |||||||||||||||||||||||||||
| 146 | 18 | ▶ | $chk = 0 if ref $chk; # coderef not supported. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 18 | ▶ | my $repl = ($chk & 4) ? ($chk & ~4 | 1) : $chk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 18 | ▶ | $str = Encode::decode('ISO-8859-1', $str) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not &Encode::is_utf8($str) and $str =~ /[^\x00-\x7F]/
MC/DC100%2/2
not &Encode::is_utf8($str) and $str =~ /[^\x00-\x7F]/
not &Encode::is_utf8($str)
$str =~ /[^\x00-\x7F]/
Truth table100%3/3
not(A) and B
A= not &Encode::is_utf8($str)B= $str =~ /[^\x00-\x7F]/
| |||||||||||||||||||||||||||||
| 150 | if ! Encode::is_utf8($str) and $str =~ /[^\x00-\x7F]/; | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 18 | local $@; | |||||||||||||||||||||||||||
| 153 | 18 | my $skip = 0; # for RETURN_ON_ERR | |||||||||||||||||||||||||||
| 154 | 18 | my $ret = undef; | |||||||||||||||||||||||||||
| 155 | 18 | pos($str) = 0; | |||||||||||||||||||||||||||
| 156 | 18 | foreach my $line ( | |||||||||||||||||||||||||||
| 157 | $str =~ m{ \G (.*?) (?:\r\n|[\r\n]) (?![ \t]) }cgsx, | ||||||||||||||||||||||||||||
| 158 | substr($str, pos($str)) | ||||||||||||||||||||||||||||
| 159 | ) { | ||||||||||||||||||||||||||||
| 160 | 27 | ▶ | if (defined $ret) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 9 | ▶ | $ret .= "\n" unless $skip; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | } else { | ||||||||||||||||||||||||||||
| 163 | 18 | $ret = ''; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 27 | ▶ | if ($skip) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | $_[1] .= "\n"; | |||||||||||||||||||||||||||
| 167 | 0 | $_[1] .= $line; | |||||||||||||||||||||||||||
| 168 | 0 | next; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 27 | ▶ | next unless length $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 22 | eval { | |||||||||||||||||||||||||||
| 173 | 22 | $ret .= MIME::EncWords::encode_mimewords($line, %opts, | |||||||||||||||||||||||||||
| 174 | Replacement => $repl); | ||||||||||||||||||||||||||||
| 175 | }; | ||||||||||||||||||||||||||||
| 176 | 22 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 0 | $@ =~ s/ at .+? line \d+[.\n]*$//; | |||||||||||||||||||||||||||
| 178 | 0 | ▶ | croak $@ if $chk & 1; # DIE_ON_ERR | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | carp $@ if $chk & 2; # WARN_ON_ERR | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | if ($chk & 4) { # RETURN_ON_ERR | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | $_[1] = $line; | |||||||||||||||||||||||||||
| 182 | 0 | $skip = 1; | |||||||||||||||||||||||||||
| 183 | 0 | next; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 18 | ▶ | if ($chk & 4) { # RETURN_ON_ERR | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | $_[1] = '' unless $skip; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | } elsif ($chk) { # ! LEAVE_SRC | ||||||||||||||||||||||||||||
| 191 | 0 | ▶ | $_[1] = '' unless $chk & 8; # FIXME:spec? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | 18 | return $ret; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 2 | ▶ | sub config { | ||||||||||||||||||||||||||
|
Subroutine
config: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||||||
| 197 | 2 | ▶ | my $klass = shift if scalar @_ % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 2 | my %opts = @_; | |||||||||||||||||||||||||||
| 199 | 2 | foreach my $key (keys %opts) { | |||||||||||||||||||||||||||
| 200 | 2 | ▶ | croak "Unknown config option: $key" unless exists $Config->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 2 | $Config->{$key} = $opts{$key}; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 2 | 1; | |||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||