| 1 | package Email::MIME::RFC2047::Encoder; | ||||||||||||||||||||||||||||
| 2 | 5 | $Email::MIME::RFC2047::Encoder::VERSION = '0.96'; | |||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Encoding of non-ASCII MIME headers | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Encode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use MIME::Base64 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | my $rfc_specials = '()<>\[\]:;\@\\,."'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||||||||||||
| 14 | 11 | ▶ | my $package = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 11 | ▶ | my $options = ref($_[0]) ? $_[0] : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 11 | my ($encoding, $method) = ($options->{encoding}, $options->{method}); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 11 | ▶ | if (!defined($encoding)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 6 | $encoding = 'utf-8'; | |||||||||||||||||||||||||||
| 21 | 6 | ▶ | $method = 'Q' if !defined($method); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | else { | ||||||||||||||||||||||||||||
| 24 | 5 | ▶ | $method = 'B' if !defined($method); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 11 | ▶ | my $encoder = Encode::find_encoding($encoding) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | or die("encoding '$encoding' not found"); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 11 | my $self = { | |||||||||||||||||||||||||||
| 31 | encoding => $encoding, | ||||||||||||||||||||||||||||
| 32 | encoder => $encoder, | ||||||||||||||||||||||||||||
| 33 | method => uc($method), | ||||||||||||||||||||||||||||
| 34 | }; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 11 | return bless($self, $package); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub encode_text { | ||||||||||||||||||||||||||||
| 40 | 13 | ▶ | my ($self, $string) = @_; | ||||||||||||||||||||||||||
|
Subroutine
encode_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 13 | return $self->_encode('text', $string); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub encode_phrase { | ||||||||||||||||||||||||||||
| 46 | 28 | ▶ | my ($self, $string) = @_; | ||||||||||||||||||||||||||
|
Subroutine
encode_phrase: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 28 | return $self->_encode('phrase', $string); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub _encode { | ||||||||||||||||||||||||||||
| 52 | 41 | ▶ | my ($self, $mode, $string) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=21 SCAR=30.5
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 41 | my $encoder = $self->{encoder}; | |||||||||||||||||||||||||||
| 55 | 41 | my $result = ''; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # $string is split on whitespace. Each $word is categorized into | ||||||||||||||||||||||||||||
| 58 | # 'mime', 'quoted' or 'text'. The intermediate result of the conversion of | ||||||||||||||||||||||||||||
| 59 | # consecutive words of the same types is accumulated in $buffer. | ||||||||||||||||||||||||||||
| 60 | # The type of the buffer is tracked in $buffer_type. | ||||||||||||||||||||||||||||
| 61 | # The method _finish_buffer is called to finish the encoding of the | ||||||||||||||||||||||||||||
| 62 | # buffered content and append to the result. | ||||||||||||||||||||||||||||
| 63 | 41 | my $buffer = ''; | |||||||||||||||||||||||||||
| 64 | 41 | my $buffer_type; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 41 | for my $word (split(/\s+/, $string)) { | |||||||||||||||||||||||||||
| 67 | 109 | ▶ | next if $word eq ''; # ignore leading white space | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 107 | $word =~ s/[\x00-\x1f\x7f]//g; # better remove control chars | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 107 | my $word_type; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 107 | ▶ | if ($word =~ /[\x80-\x{10ffff}]|(^=\?.*\?=\z)/s) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 74 | # also encode any word that starts with '=?' and ends with '?=' | ||||||||||||||||||||||||||||
| 75 | 54 | $word_type = 'mime'; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | elsif ($mode eq 'phrase') { | ||||||||||||||||||||||||||||
| 78 | 40 | $word_type = 'quoted'; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | else { | ||||||||||||||||||||||||||||
| 81 | 13 | $word_type = 'text'; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 107 | ▶ | $self->_finish_buffer(\$result, $buffer_type, \$buffer) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$buffer ne "" and $buffer_type ne $word_type
MC/DC100%2/2
$buffer ne "" and $buffer_type ne $word_type
$buffer ne ""
$buffer_type ne $word_type
Truth table100%3/3
A and B
A= $buffer ne ""B= $buffer_type ne $word_type
| |||||||||||||||||||||||||||||
| 85 | if $buffer ne '' && $buffer_type ne $word_type; | ||||||||||||||||||||||||||||
| 86 | 107 | $buffer_type = $word_type; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 107 | ▶ | if ($word_type eq 'text') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 89 | 13 | ▶ | $result .= ' ' if $result ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 13 | $result .= $word; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | elsif ($word_type eq 'quoted') { | ||||||||||||||||||||||||||||
| 93 | 40 | ▶ | $buffer .= ' ' if $buffer ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 40 | $buffer .= $word; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 54 | my $max_len = 75 - 7 - length($self->{encoding}); | |||||||||||||||||||||||||||
| 98 | 54 | ▶ | $max_len = 3 * ($max_len >> 2) if $self->{method} eq 'B'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 54 | my @chars; | |||||||||||||||||||||||||||
| 101 | 54 | ▶ | push(@chars, ' ') if $buffer ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 54 | push(@chars, split(//, $word)); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 54 | for my $char (@chars) { | |||||||||||||||||||||||||||
| 105 | 224 | my $chunk; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 224 | ▶ | if ($self->{method} eq 'B') { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 108 | 0 | $chunk = $encoder->encode($char); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | elsif ($char =~ /[()<>@,;:\\".\[\]=?_]/) { | ||||||||||||||||||||||||||||
| 111 | # special character | ||||||||||||||||||||||||||||
| 112 | 16 | $chunk = sprintf('=%02x', ord($char)); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | elsif ($char =~ /[\x80-\x{10ffff}]/) { | ||||||||||||||||||||||||||||
| 115 | # non-ASCII character | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 100 | my $enc_char = $encoder->encode($char); | |||||||||||||||||||||||||||
| 118 | 100 | $chunk = ''; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 100 | for my $byte (unpack('C*', $enc_char)) { | |||||||||||||||||||||||||||
| 121 | 196 | $chunk .= sprintf('=%02x', $byte); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | elsif ($char eq ' ') { | ||||||||||||||||||||||||||||
| 125 | 28 | $chunk = '_'; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | else { | ||||||||||||||||||||||||||||
| 128 | 80 | $chunk = $char; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 224 | ▶ | if (length($buffer) + length($chunk) <= $max_len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 218 | $buffer .= $chunk; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||
| 135 | 6 | $self->_finish_buffer(\$result, 'mime', \$buffer); | |||||||||||||||||||||||||||
| 136 | 6 | $buffer = $chunk; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 41 | ▶ | $self->_finish_buffer(\$result, $buffer_type, \$buffer) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | if $buffer ne ''; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 41 | return $result; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | sub _finish_buffer { | ||||||||||||||||||||||||||||
| 149 | 59 | ▶ | my ($self, $result, $buffer_type, $buffer) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_finish_buffer: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 59 | ▶ | $$result .= ' ' if $$result ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 59 | ▶ | if ($buffer_type eq 'quoted') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 154 | 27 | ▶ | if ($$buffer =~ /[$rfc_specials]/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | # use quoted string if buffer contains special chars | ||||||||||||||||||||||||||||
| 156 | 9 | $$buffer =~ s/[\\"]/\\$&/g; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 9 | $$result .= qq("$$buffer"); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||
| 161 | 18 | $$result .= $$buffer; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | elsif ($buffer_type eq 'mime') { | ||||||||||||||||||||||||||||
| 165 | 32 | $$result .= "=?$self->{encoding}?$self->{method}?"; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 32 | ▶ | if ($self->{method} eq 'B') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | $$result .= MIME::Base64::encode_base64($$buffer, ''); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | else { | ||||||||||||||||||||||||||||
| 171 | 32 | $$result .= $$buffer; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 32 | $$result .= '?='; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 59 | $$buffer = ''; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 59 | return; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 5 | 1; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||||||||||||