/[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.1 - (hide annotations) (download)
Wed May 29 10:54:49 2002 UTC (22 years, 6 months ago) by wakaba
Branch: MAIN
2002-05-29  wakaba <w@suika.fam.cx>

	* MediaType.pm, Encoding.pm: New modules.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24