/[suikacvs]/messaging/manakai/lib/Message/MIME/Encoding.pm
Suika

Contents of /messaging/manakai/lib/Message/MIME/Encoding.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations) (download)
Thu May 30 12:51:05 2002 UTC (24 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.1: +19 -9 lines
2002-05-30  wakaba <w@suika.fam.cx>

	* Charset.pm:
	- (%ENCODER, %DECODER, %N11NTABLE): Removed.
	- (%CHARSET): New hash.
	- (make_charset): New function.
	* Encoding.pm:
	- (decide_coderange): Checks media-type.
	- (uuencode): Typo fix.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24