/[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.6 - (hide annotations) (download)
Wed Jul 3 23:39:15 2002 UTC (22 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.5: +6 -3 lines
2002-07-02  Wakaba <w@suika.fam.cx>

	* Util.pm (decide_newline): New function.
	* Entity.pm (parse): Call Message::Util::decide_newline
	instead of local code.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24