/[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.7 - (hide annotations) (download)
Wed Jul 17 00:33:29 2002 UTC (22 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.6: +17 -7 lines
2002-07-13  Wakaba <w@suika.fam.cx>

	* Util.pm:
	- (get_host_fqdn): New function.
	- (%OPTION): New hash.
	* Entity.pm (stringify): Pass 'format' option
	to the body (when stringify'ing it) with
	-parent_format option, instead of -format option.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24