/[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.4 - (hide annotations) (download)
Sun Jun 9 11:13:14 2002 UTC (22 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.3: +21 -13 lines
2002-06-09  wakaba <w@suika.fam.cx>

	* Charset.pm:
	- Support of 'name_minimumizer' property of charset object.
	- (name_minimumize): New function.
	* Encoding.pm (encode_qp): Don't encode inner WSPs within
	tailing WSPs.
	* MediaType.pm: Support of some new media types
	and their parameters.

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.4 $VERSION=do{my @r=(q$Revision: 1.3 $=~/\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.1 $s =~ s/=0D=0A/\x0D\x0A/g;
106     } else {
107 wakaba 1.4 $s =~ s/([\x09\x20])$/
108     sprintf '=%02X', ord($1)
109     #join('', map { sprintf('=%02X', ord($_)) } split('', $1) )
110     /egm; # rule #3 (encode whitespace at eol)
111 wakaba 1.1 }
112    
113     # rule #5 (lines must be shorter than 76 chars, but we are not allowed
114     # to break =XX escapes. This makes things complicated :-( )
115     my $brokenlines = "";
116     $brokenlines .= $1.'='.$nl
117     while $s =~ s/(.*?^[^$nl]{73} (?:
118     [^=$nl]{2} (?! [^=$nl]{0,1} $) # 75 not followed by .?\n
119     |[^=$nl] (?! [^=$nl]{0,2} $) # 74 not followed by .?.?\n
120     | (?! [^=$nl]{0,3} $) # 73 not followed by .?.?.?\n
121     ))//xsm;
122     ($brokenlines.$s, 'quoted-printable');
123     }
124    
125    
126     ## Original: MIME::QuotedPrint Revision: 2.3 1997/12/02 10:24:27
127     ## by Gisle Aas
128     sub decode_qp ($$) {
129     my $yourself = shift;
130     my $s = shift;
131     $s =~ s/[\x09\x20]+(\x0D?\x0A)/$1/g; # rule #3 (trailing space must be deleted)
132     $s =~ s/[\x09\x20]+$//g;
133     $s =~ s/=\x0D?\x0A//g; # rule #5 (soft line breaks)
134     $s =~ s/=([0-9A-Fa-f][0-9A-Fa-f])/pack('C', hex($1))/ge;
135     ## Strictly, smallcases are not allowed
136     ($s, 'binary');
137     }
138    
139    
140     sub uuencode ($$;%) {
141     my $yourself = shift;
142     my $s = shift; my %p = @_;
143     my %option = (mode => 644, ## mode as (if:-)) decimal number
144     filename => '', preamble => '', postamble => '',
145     newline => "\x0D\x0A");
146     for (grep {/^-/} keys %p) {$option{substr ($_, 1)} = $p{$_}}
147    
148     my $r = '';
149     if (length $option{preamble}) {
150     $option{preamble} .= $option{newline}
151     unless $option{preamble} =~ /$option{newline}$/s;
152     $r .= $option{preamble} . $option{newline};
153     }
154     $option{filename} = 'encoded-data' unless length $option{filename};
155     $r .= sprintf 'begin %03d %s%s', @option{'mode', 'filename', 'newline'};
156     my $u = pack 'u', $s;
157     $u =~ s/\x0D?\x0A/$option{newline}/g;
158     $r .= $u;
159     $r .= 'end' . $option{newline};
160     if (length $option{postamble}) {
161     $option{postamble} .= $option{newline}
162     unless $option{postamble} =~ /$option{newline}$/s;
163     $r .= $option{newline} . $option{postamble};
164     }
165 wakaba 1.2 ($r, 'x-uuencode');
166 wakaba 1.1 }
167    
168     sub uudecode ($$) {
169     my $yourself = shift;
170     my $s = shift;
171     my @s = split /\x0D?\x0A/, $s;
172    
173     ## Taken from MIME::Decoder::UU by Eryq (<eryq@zeegee.com>),
174     ## Revision: 5.403 / Date: 2000/11/04 19:54:49
175     my ($mode, $filename, @preamble) = (0, '');
176     while (defined ($_ = shift (@s))) {
177     if (/^begin(.*)/) { ### found it: now decode it...
178     my $modefile = $1;
179     if ($modefile =~ /^(?:\s+(\d+))?(?:\s+(.*?\S))?\s*\Z/) {
180     ($mode, $filename) = ($1, $2);
181     }
182     last; ### decoded or not, we're done
183     }
184     push @preamble, $_;
185     }
186     if (!defined ($_)) { # hit eof!
187     Carp::carp "uu decode: No begin found";
188     return ($s, 'x-uuencode');
189     }
190    
191     ### Decode:
192     my $r = '';
193     while (defined ($_ = shift (@s))) {
194     last if /^end/;
195     next if /[a-z]/;
196     next unless int((((ord() - 32) & 077) + 2) / 3) == int(length() / 4);
197     $r .= (unpack('u', $_));
198     }
199     return ($r, 'binary', -filename => $filename, -mode => $mode,
200     -preamble => join ("\x0D\x0A", @preamble),
201     -postamble => join ("\x0D\x0A", @s));
202     }
203    
204     sub uncompress_gzip ($$) {
205     my $yourself = shift;
206     my ($s) = @_;
207     if (eval {require Compress::Zlib}) {
208     ## Taken from Namazu <http://www.namazu.org/>, filter/gzip.pl
209     my $flags = unpack('C', substr($s, 3, 1));
210     $s = substr($s, 10);
211     $s = substr($s, 2) if ($flags & 0x04);
212     $s =~ s/^[^\0]*\0// if ($flags & 0x08);
213     $s =~ s/^[^\0]*\0// if ($flags & 0x10);
214     $s = substr($s, 2) if ($flags & 0x02);
215    
216     my $zl = Compress::Zlib::inflateInit
217     (-WindowBits => - Compress::Zlib::MAX_WBITS());
218     my ($inf, $stat) = $zl->inflate ($s);
219     if ($stat == Compress::Zlib::Z_OK() || $stat == Compress::Zlib::Z_STREAM_END()) {
220     return ($inf, 'identity');
221     } else {
222     Carp::carp 'uncompress_gzip: Bad compressed data';
223     }
224     } else {
225     Carp::carp "gzip64 decode: $@";
226     }
227     ($_[1], 'gzip'); ## failue
228     }
229    
230     =head1 SEE ALSO
231    
232     For charset ENCODINGs, see Message::MIME::Charset.
233    
234     =head1 LICENSE
235    
236     Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
237    
238     This program is free software; you can redistribute it and/or modify
239     it under the terms of the GNU General Public License as published by
240     the Free Software Foundation; either version 2 of the License, or
241     (at your option) any later version.
242    
243     This program is distributed in the hope that it will be useful,
244     but WITHOUT ANY WARRANTY; without even the implied warranty of
245     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
246     GNU General Public License for more details.
247    
248     You should have received a copy of the GNU General Public License
249     along with this program; see the file COPYING. If not, write to
250     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
251     Boston, MA 02111-1307, USA.
252    
253     =head1 CHANGE
254    
255     See F<ChangeLog>.
256 wakaba 1.4 $Date: 2002/06/01 05:37:18 $
257 wakaba 1.1
258     =cut
259    
260     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24