/[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.3 - (hide annotations) (download)
Sat Jun 1 05:37:18 2002 UTC (22 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.2: +3 -2 lines
2002-06-01  wakaba <w@suika.fam.cx>

	* Charset.pm:
	- (encode): Returns minimum charset name if possible.
	- (definition of iso-2022-int-1, us-ascii): mime_text = 1.
	* Encoding.pm (decide_coderange): Returns 'binary'
	when there are lines consist of more than 998 octets.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24