1 |
wakaba |
1.1 |
|
2 |
|
|
=head1 NAME
|
3 |
|
|
|
4 |
|
|
Message::MIME::Encoding --- Encoding (MIME CTE, HTTP encodings, etc) definitions
|
5 |
|
|
|
6 |
|
|
=cut
|
7 |
|
|
|
8 |
|
|
package Message::MIME::Encoding;
|
9 |
|
|
use strict;
|
10 |
|
|
use vars qw($VERSION);
|
11 |
wakaba |
1.5 |
$VERSION=do{my @r=(q$Revision: 1.4 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
|
12 |
wakaba |
1.1 |
|
13 |
|
|
our %ENCODER = (
|
14 |
wakaba |
1.2 |
'7bit' => sub { ($_[1], decide_coderange (@_[0,1,2])) },
|
15 |
|
|
'8bit' => sub { ($_[1], decide_coderange (@_[0,1,2])) },
|
16 |
|
|
binary => sub { ($_[1], decide_coderange (@_[0,1,2])) },
|
17 |
wakaba |
1.4 |
base64 => sub {
|
18 |
|
|
require MIME::Base64;
|
19 |
|
|
my $s = MIME::Base64::encode ($_[1]);
|
20 |
|
|
$s =~ s/\x0D(?!\x0A)/\x0D\x0A/gs;
|
21 |
|
|
$s =~ s/(?<!\x0D)\x0A/\x0D\x0A/gs;
|
22 |
|
|
($s, 'base64');
|
23 |
|
|
},
|
24 |
wakaba |
1.1 |
'quoted-printable' => \&encode_qp,
|
25 |
|
|
# => sub { require MIME::QuotedPrint;
|
26 |
|
|
# (MIME::QuotedPrint::encode ($_[1]), 'quoted-printable') },
|
27 |
|
|
'x-gzip64' => sub {
|
28 |
|
|
if (eval {require Compress::Zlib}) {
|
29 |
|
|
require MIME::Base64;
|
30 |
|
|
my $s = Compress::Zlib::memGzip ($_[1]);
|
31 |
wakaba |
1.4 |
$s = MIME::Base64::encode ($s);
|
32 |
|
|
$s =~ s/\x0D(?!\x0A)/\x0D\x0A/gs;
|
33 |
|
|
$s =~ s/(?<!\x0D)\x0A/\x0D\x0A/gs;
|
34 |
|
|
($s, 'x-gzip64');
|
35 |
wakaba |
1.1 |
} else {
|
36 |
|
|
Carp::carp "gzip64 encode: $@";
|
37 |
|
|
($_[1], 'binary');
|
38 |
|
|
}
|
39 |
|
|
},
|
40 |
|
|
'x-uu' => \&uuencode,
|
41 |
|
|
'x-uue' => \&uuencode,
|
42 |
|
|
'x-uuencode' => \&uuencode,
|
43 |
|
|
'x-uuencoded' => \&uuencode,
|
44 |
|
|
);
|
45 |
|
|
our %DECODER = (
|
46 |
|
|
'7bit' => sub { ($_[1], 'binary') },
|
47 |
|
|
'8bit' => sub { ($_[1], 'binary') },
|
48 |
|
|
binary => sub { ($_[1], 'binary') },
|
49 |
|
|
base64 => sub { require MIME::Base64;
|
50 |
|
|
(MIME::Base64::decode ($_[1]), 'binary') },
|
51 |
|
|
'quoted-printable' => \&decode_qp,
|
52 |
|
|
# => sub { require MIME::QuotedPrint;
|
53 |
|
|
# (MIME::QuotedPrint::decode ($_[1]), 'binary') },
|
54 |
|
|
'x-gzip64' => sub {
|
55 |
|
|
require MIME::Base64;
|
56 |
|
|
my $s = MIME::Base64::decode ($_[1]);
|
57 |
|
|
my ($t, $e) = uncompress_gzip ($_[0], $s);
|
58 |
|
|
if ($e eq 'identity') { return ($t, 'binary') }
|
59 |
|
|
else { return ($_[1], 'x-gzip64') }
|
60 |
|
|
},
|
61 |
|
|
'x-uu' => \&uudecode,
|
62 |
|
|
'x-uue' => \&uudecode,
|
63 |
|
|
'x-uuencode' => \&uudecode,
|
64 |
|
|
'x-uuencoded' => \&uudecode,
|
65 |
|
|
);
|
66 |
|
|
|
67 |
wakaba |
1.2 |
sub decide_coderange ($$\%) {
|
68 |
wakaba |
1.1 |
my $yourself = shift;
|
69 |
wakaba |
1.2 |
my $s = shift;
|
70 |
|
|
my $option = shift;
|
71 |
|
|
if (!defined $option->{mt_is_text}) {
|
72 |
|
|
my $mt; $mt = ($yourself->content_type)[0] if ref $yourself;
|
73 |
|
|
$option->{mt_is_text} = 1
|
74 |
|
|
if $mt eq 'text' || $mt eq 'multipart' || $mt eq 'message';
|
75 |
|
|
}
|
76 |
wakaba |
1.1 |
return 'binary' if $s =~ /\x00/;
|
77 |
wakaba |
1.2 |
if ($option->{mt_is_text}) {
|
78 |
|
|
return 'binary' if $s =~ /\x0D(?!\x0A)/s;
|
79 |
|
|
return 'binary' if $s =~ /(?<!\x0D)\x0A/s;
|
80 |
|
|
} else {
|
81 |
|
|
return 'binary' if $s =~ /\x0D|\x0A/s;
|
82 |
|
|
}
|
83 |
wakaba |
1.3 |
return 'binary' if $s =~ /[^\x0D\x0A]{999}/;
|
84 |
wakaba |
1.1 |
return '8bit' if $s =~ /[\x80-\xFF]/;
|
85 |
|
|
'7bit';
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
## Original: MIME::QuotedPrint Revision: 2.3 1997/12/02 10:24:27
|
89 |
|
|
## by Gisle Aas
|
90 |
|
|
sub encode_qp ($$) {
|
91 |
|
|
my $yourself = shift;
|
92 |
|
|
my $s = shift;
|
93 |
|
|
my $nl = "\x0D\x0A";
|
94 |
|
|
my $mt_is_text = 0;
|
95 |
|
|
my $mt; $mt = ($yourself->content_type)[0] if ref $yourself;
|
96 |
|
|
$mt_is_text = 1 if $mt eq 'text' || $mt eq 'multipart' || $mt eq 'message';
|
97 |
|
|
## RFC 2045 [^\x09\x20\x21-\x3C\x3E-\x7E]
|
98 |
|
|
## - RFC 2049 "mail-safe" [^\x09\x20\x25-\x3C\x3E\x3F\x41-\x5A\x5F\x61-\x7A]
|
99 |
|
|
$s =~ s/([^\x09\x20\x25-\x3C\x3E\x3F\x41-\x5A\x5F\x61-\x7A])/sprintf('=%02X', ord($1))/eg; # rule #2,#3
|
100 |
|
|
if ($mt_is_text) {
|
101 |
wakaba |
1.4 |
$s =~ s/([\x09\x20])(?==0D=0A|$)/
|
102 |
|
|
sprintf '=%02X', ord($1)
|
103 |
|
|
#join('', map { sprintf('=%02X', ord($_)) } split('', $1) )
|
104 |
|
|
/egm; # rule #3 (encode whitespace at eol)
|
105 |
wakaba |
1.5 |
$s =~ s/=0D=0AFrom/\x0D\x0A=46rom/g;
|
106 |
wakaba |
1.1 |
$s =~ s/=0D=0A/\x0D\x0A/g;
|
107 |
|
|
} else {
|
108 |
wakaba |
1.4 |
$s =~ s/([\x09\x20])$/
|
109 |
|
|
sprintf '=%02X', ord($1)
|
110 |
|
|
#join('', map { sprintf('=%02X', ord($_)) } split('', $1) )
|
111 |
|
|
/egm; # rule #3 (encode whitespace at eol)
|
112 |
wakaba |
1.1 |
}
|
113 |
|
|
|
114 |
|
|
# rule #5 (lines must be shorter than 76 chars, but we are not allowed
|
115 |
|
|
# to break =XX escapes. This makes things complicated :-( )
|
116 |
|
|
my $brokenlines = "";
|
117 |
|
|
$brokenlines .= $1.'='.$nl
|
118 |
|
|
while $s =~ s/(.*?^[^$nl]{73} (?:
|
119 |
|
|
[^=$nl]{2} (?! [^=$nl]{0,1} $) # 75 not followed by .?\n
|
120 |
|
|
|[^=$nl] (?! [^=$nl]{0,2} $) # 74 not followed by .?.?\n
|
121 |
|
|
| (?! [^=$nl]{0,3} $) # 73 not followed by .?.?.?\n
|
122 |
|
|
))//xsm;
|
123 |
|
|
($brokenlines.$s, 'quoted-printable');
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
## Original: MIME::QuotedPrint Revision: 2.3 1997/12/02 10:24:27
|
128 |
|
|
## by Gisle Aas
|
129 |
|
|
sub decode_qp ($$) {
|
130 |
|
|
my $yourself = shift;
|
131 |
|
|
my $s = shift;
|
132 |
|
|
$s =~ s/[\x09\x20]+(\x0D?\x0A)/$1/g; # rule #3 (trailing space must be deleted)
|
133 |
|
|
$s =~ s/[\x09\x20]+$//g;
|
134 |
|
|
$s =~ s/=\x0D?\x0A//g; # rule #5 (soft line breaks)
|
135 |
|
|
$s =~ s/=([0-9A-Fa-f][0-9A-Fa-f])/pack('C', hex($1))/ge;
|
136 |
|
|
## Strictly, smallcases are not allowed
|
137 |
|
|
($s, 'binary');
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
sub uuencode ($$;%) {
|
142 |
|
|
my $yourself = shift;
|
143 |
|
|
my $s = shift; my %p = @_;
|
144 |
|
|
my %option = (mode => 644, ## mode as (if:-)) decimal number
|
145 |
|
|
filename => '', preamble => '', postamble => '',
|
146 |
|
|
newline => "\x0D\x0A");
|
147 |
|
|
for (grep {/^-/} keys %p) {$option{substr ($_, 1)} = $p{$_}}
|
148 |
|
|
|
149 |
|
|
my $r = '';
|
150 |
|
|
if (length $option{preamble}) {
|
151 |
|
|
$option{preamble} .= $option{newline}
|
152 |
|
|
unless $option{preamble} =~ /$option{newline}$/s;
|
153 |
|
|
$r .= $option{preamble} . $option{newline};
|
154 |
|
|
}
|
155 |
|
|
$option{filename} = 'encoded-data' unless length $option{filename};
|
156 |
|
|
$r .= sprintf 'begin %03d %s%s', @option{'mode', 'filename', 'newline'};
|
157 |
|
|
my $u = pack 'u', $s;
|
158 |
|
|
$u =~ s/\x0D?\x0A/$option{newline}/g;
|
159 |
|
|
$r .= $u;
|
160 |
|
|
$r .= 'end' . $option{newline};
|
161 |
|
|
if (length $option{postamble}) {
|
162 |
|
|
$option{postamble} .= $option{newline}
|
163 |
|
|
unless $option{postamble} =~ /$option{newline}$/s;
|
164 |
|
|
$r .= $option{newline} . $option{postamble};
|
165 |
|
|
}
|
166 |
wakaba |
1.2 |
($r, 'x-uuencode');
|
167 |
wakaba |
1.1 |
}
|
168 |
|
|
|
169 |
|
|
sub uudecode ($$) {
|
170 |
|
|
my $yourself = shift;
|
171 |
|
|
my $s = shift;
|
172 |
|
|
my @s = split /\x0D?\x0A/, $s;
|
173 |
|
|
|
174 |
|
|
## Taken from MIME::Decoder::UU by Eryq (<eryq@zeegee.com>),
|
175 |
|
|
## Revision: 5.403 / Date: 2000/11/04 19:54:49
|
176 |
|
|
my ($mode, $filename, @preamble) = (0, '');
|
177 |
|
|
while (defined ($_ = shift (@s))) {
|
178 |
|
|
if (/^begin(.*)/) { ### found it: now decode it...
|
179 |
|
|
my $modefile = $1;
|
180 |
|
|
if ($modefile =~ /^(?:\s+(\d+))?(?:\s+(.*?\S))?\s*\Z/) {
|
181 |
|
|
($mode, $filename) = ($1, $2);
|
182 |
|
|
}
|
183 |
|
|
last; ### decoded or not, we're done
|
184 |
|
|
}
|
185 |
|
|
push @preamble, $_;
|
186 |
|
|
}
|
187 |
|
|
if (!defined ($_)) { # hit eof!
|
188 |
|
|
Carp::carp "uu decode: No begin found";
|
189 |
|
|
return ($s, 'x-uuencode');
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
### Decode:
|
193 |
|
|
my $r = '';
|
194 |
|
|
while (defined ($_ = shift (@s))) {
|
195 |
|
|
last if /^end/;
|
196 |
|
|
next if /[a-z]/;
|
197 |
|
|
next unless int((((ord() - 32) & 077) + 2) / 3) == int(length() / 4);
|
198 |
|
|
$r .= (unpack('u', $_));
|
199 |
|
|
}
|
200 |
|
|
return ($r, 'binary', -filename => $filename, -mode => $mode,
|
201 |
|
|
-preamble => join ("\x0D\x0A", @preamble),
|
202 |
|
|
-postamble => join ("\x0D\x0A", @s));
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
sub uncompress_gzip ($$) {
|
206 |
|
|
my $yourself = shift;
|
207 |
|
|
my ($s) = @_;
|
208 |
|
|
if (eval {require Compress::Zlib}) {
|
209 |
|
|
## Taken from Namazu <http://www.namazu.org/>, filter/gzip.pl
|
210 |
|
|
my $flags = unpack('C', substr($s, 3, 1));
|
211 |
|
|
$s = substr($s, 10);
|
212 |
|
|
$s = substr($s, 2) if ($flags & 0x04);
|
213 |
|
|
$s =~ s/^[^\0]*\0// if ($flags & 0x08);
|
214 |
|
|
$s =~ s/^[^\0]*\0// if ($flags & 0x10);
|
215 |
|
|
$s = substr($s, 2) if ($flags & 0x02);
|
216 |
|
|
|
217 |
|
|
my $zl = Compress::Zlib::inflateInit
|
218 |
|
|
(-WindowBits => - Compress::Zlib::MAX_WBITS());
|
219 |
|
|
my ($inf, $stat) = $zl->inflate ($s);
|
220 |
|
|
if ($stat == Compress::Zlib::Z_OK() || $stat == Compress::Zlib::Z_STREAM_END()) {
|
221 |
|
|
return ($inf, 'identity');
|
222 |
|
|
} else {
|
223 |
|
|
Carp::carp 'uncompress_gzip: Bad compressed data';
|
224 |
|
|
}
|
225 |
|
|
} else {
|
226 |
|
|
Carp::carp "gzip64 decode: $@";
|
227 |
|
|
}
|
228 |
|
|
($_[1], 'gzip'); ## failue
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
=head1 SEE ALSO
|
232 |
|
|
|
233 |
|
|
For charset ENCODINGs, see Message::MIME::Charset.
|
234 |
|
|
|
235 |
|
|
=head1 LICENSE
|
236 |
|
|
|
237 |
|
|
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
|
238 |
|
|
|
239 |
|
|
This program is free software; you can redistribute it and/or modify
|
240 |
|
|
it under the terms of the GNU General Public License as published by
|
241 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
242 |
|
|
(at your option) any later version.
|
243 |
|
|
|
244 |
|
|
This program is distributed in the hope that it will be useful,
|
245 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
246 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
247 |
|
|
GNU General Public License for more details.
|
248 |
|
|
|
249 |
|
|
You should have received a copy of the GNU General Public License
|
250 |
|
|
along with this program; see the file COPYING. If not, write to
|
251 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
252 |
|
|
Boston, MA 02111-1307, USA.
|
253 |
|
|
|
254 |
|
|
=head1 CHANGE
|
255 |
|
|
|
256 |
|
|
See F<ChangeLog>.
|
257 |
wakaba |
1.5 |
$Date: 2002/06/09 11:13:14 $
|
258 |
wakaba |
1.1 |
|
259 |
|
|
=cut
|
260 |
|
|
|
261 |
|
|
1;
|