1 |
wakaba |
1.1 |
|
2 |
|
|
=head1 NAME
|
3 |
|
|
|
4 |
|
|
Message::MIME::Charset Perl module
|
5 |
|
|
|
6 |
|
|
=head1 DESCRIPTION
|
7 |
|
|
|
8 |
|
|
Perl module for MIME charset.
|
9 |
|
|
|
10 |
|
|
=cut
|
11 |
|
|
|
12 |
wakaba |
1.10 |
## NOTE: You should not require/use other module (even it
|
13 |
|
|
## is part of Message::* Perl Modules) as far as possible,
|
14 |
|
|
## to be able to use this module (M::M::Charset) from
|
15 |
|
|
## other (non-Message::*) modules.
|
16 |
|
|
|
17 |
wakaba |
1.1 |
package Message::MIME::Charset;
|
18 |
|
|
use strict;
|
19 |
wakaba |
1.12 |
use vars qw(%CHARSET %MSNAME2IANANAME %REG $VERSION);
|
20 |
wakaba |
1.16 |
$VERSION=do{my @r=(q$Revision: 1.15 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
|
21 |
wakaba |
1.1 |
|
22 |
wakaba |
1.9 |
&_builtin_charset;
|
23 |
|
|
sub _builtin_charset () {
|
24 |
wakaba |
1.4 |
|
25 |
|
|
$CHARSET{'*DEFAULT'} = {
|
26 |
|
|
preferred_name => '',
|
27 |
|
|
|
28 |
|
|
encoder => sub { $_[1] },
|
29 |
|
|
decoder => sub { $_[1] },
|
30 |
|
|
|
31 |
|
|
mime_text => 1, ## Suitability in use as MIME text/* charset
|
32 |
|
|
#accept_cte => [qw/7bit .../],
|
33 |
|
|
cte_7bit_preferred => 'quoted-printable',
|
34 |
|
|
};
|
35 |
|
|
$CHARSET{'*default'} = $CHARSET{'*DEFAULT'};
|
36 |
|
|
|
37 |
|
|
$CHARSET{'us-ascii'} = {
|
38 |
|
|
preferred_name => 'us-ascii',
|
39 |
|
|
|
40 |
|
|
encoder => sub { $_[1] },
|
41 |
|
|
decoder => sub { $_[1] },
|
42 |
wakaba |
1.5 |
|
43 |
|
|
mime_text => 1,
|
44 |
wakaba |
1.13 |
cte_7bit_preferred => 'quoted-printable',
|
45 |
wakaba |
1.4 |
};
|
46 |
|
|
|
47 |
|
|
$CHARSET{'iso-2022-int-1'} = {
|
48 |
|
|
preferred_name => 'iso-2022-int-1',
|
49 |
|
|
|
50 |
|
|
encoder => sub { $_[1] },
|
51 |
|
|
decoder => sub { $_[1] },
|
52 |
wakaba |
1.5 |
|
53 |
|
|
mime_text => 1,
|
54 |
wakaba |
1.4 |
};
|
55 |
|
|
|
56 |
|
|
$CHARSET{'unknown-8bit'} = {
|
57 |
|
|
preferred_name => 'unknown-8bit',
|
58 |
|
|
|
59 |
|
|
encoder => sub { $_[1] },
|
60 |
|
|
decoder => sub { $_[1] },
|
61 |
|
|
|
62 |
wakaba |
1.13 |
mime_text => 1,
|
63 |
|
|
cte_7bit_preferred => 'base64',
|
64 |
|
|
};
|
65 |
|
|
|
66 |
|
|
$CHARSET{'x-unknown'} = {
|
67 |
|
|
preferred_name => 'x-unknown',
|
68 |
|
|
|
69 |
|
|
encoder => sub { $_[1] },
|
70 |
|
|
decoder => sub { $_[1] },
|
71 |
|
|
|
72 |
|
|
mime_text => 0,
|
73 |
|
|
cte_7bit_preferred => 'base64',
|
74 |
|
|
};
|
75 |
|
|
|
76 |
|
|
$CHARSET{'*undef'} = {
|
77 |
|
|
preferred_name => '',
|
78 |
|
|
|
79 |
|
|
#encoder => sub { $_[1] },
|
80 |
|
|
#decoder => sub { $_[1] },
|
81 |
|
|
|
82 |
wakaba |
1.4 |
mime_text => 0,
|
83 |
wakaba |
1.6 |
cte_7bit_preferred => 'base64',
|
84 |
wakaba |
1.4 |
};
|
85 |
|
|
|
86 |
wakaba |
1.9 |
} # /builtin_charset
|
87 |
|
|
|
88 |
|
|
my %_MINIMUMIZER = (
|
89 |
|
|
'euc-jp' => \&_name_euc_japan,
|
90 |
|
|
'euc-jisx0213' => \&_name_euc_japan,
|
91 |
|
|
'euc-jisx0213-plane1' => \&_name_euc_japan,
|
92 |
|
|
'x-euc-jisx0213-packed' => \&_name_euc_japan,
|
93 |
wakaba |
1.11 |
'x-iso-2022' => \&_name_8bit_iso2022,
|
94 |
|
|
'iso-2022-cn' => \&_name_8bit_iso2022,
|
95 |
|
|
'iso-2022-cn-ext' => \&_name_8bit_iso2022,
|
96 |
wakaba |
1.9 |
'iso-2022-int-1' => \&_name_net_ascii_8bit,
|
97 |
wakaba |
1.11 |
'iso-2022-jp' => \&_name_8bit_iso2022,
|
98 |
|
|
'iso-2022-jp-1' => \&_name_8bit_iso2022,
|
99 |
|
|
'iso-2022-jp-2' => \&_name_8bit_iso2022,
|
100 |
|
|
'iso-2022-jp-3' => \&_name_8bit_iso2022,
|
101 |
|
|
'iso-2022-jp-3-plane1' => \&_name_8bit_iso2022,
|
102 |
|
|
'iso-2022-kr' => \&_name_8bit_iso2022,
|
103 |
|
|
'iso-8859-1' => \&_name_8bit_iso2022,
|
104 |
wakaba |
1.9 |
'iso-10646-j-1' => \&_name_utf16be,
|
105 |
|
|
'iso-10646-ucs-2' => \&_name_utf16be,
|
106 |
|
|
'iso-10646-ucs-4' => \&_name_utf32be,
|
107 |
|
|
'iso-10646-ucs-basic' => \&_name_utf16be,
|
108 |
|
|
'iso-10646-unicode-latin1' => \&_name_utf16be,
|
109 |
|
|
jis_x0201 => \&_name_shift_jis,
|
110 |
wakaba |
1.11 |
junet => \&_name_8bit_iso2022,
|
111 |
wakaba |
1.9 |
'x-junet8' => \&_name_net_ascii_8bit,
|
112 |
|
|
shift_jis => \&_name_shift_jis,
|
113 |
|
|
shift_jisx0213 => \&_name_shift_jis,
|
114 |
|
|
'shift_jisx0213-plane1' => \&_name_shift_jis,
|
115 |
|
|
'x-sjis' => \&_name_shift_jis,
|
116 |
|
|
'us-ascii' => \&_name_net_ascii_8bit,
|
117 |
|
|
'utf-8' => \&_name_net_ascii_8bit,
|
118 |
|
|
'utf-16be' => \&_name_utf16be,
|
119 |
|
|
'utf-32be' => \&_name_utf32be,
|
120 |
|
|
);
|
121 |
|
|
|
122 |
wakaba |
1.14 |
my %_IsMimeText;
|
123 |
|
|
for (qw(
|
124 |
|
|
adobe-standard-encoding adobe-symbol-encoding
|
125 |
|
|
big5 big5-eten big5-hkscs
|
126 |
|
|
cp950
|
127 |
|
|
gbk gb18030
|
128 |
|
|
euc-jp euc-jisx0213 euc-kr euc-tw
|
129 |
|
|
hp-roman8
|
130 |
|
|
hz-gb-2312
|
131 |
|
|
ibm437
|
132 |
wakaba |
1.16 |
junet x-junet8 x-iso-2022
|
133 |
wakaba |
1.14 |
iso-2022-cn iso-2022-cn-ext
|
134 |
|
|
iso-2022-int-1
|
135 |
|
|
iso-2022-jp iso-2022-jp-1 iso-2022-jp-2 iso-2022-jp-3
|
136 |
wakaba |
1.15 |
x-iso2022jp-cp932
|
137 |
wakaba |
1.14 |
iso-2022-kr
|
138 |
|
|
iso-8859-1 iso-8859-2 iso-8859-3
|
139 |
|
|
iso-8859-4 iso-8859-5 iso-8859-6
|
140 |
|
|
iso-8859-7 iso-8859-8 iso-8859-9
|
141 |
|
|
iso-8859-10 iso-8859-12 iso-8859-13
|
142 |
|
|
iso-8859-14 iso-8859-15 iso-8859-16
|
143 |
|
|
jis_encoding
|
144 |
|
|
koi8-r koi8-u
|
145 |
|
|
x-mac-arabic x-mac-centralroman x-mac-cyrillic x-mac-greek
|
146 |
|
|
x-mac-hebrew x-mac-icelandic macintosh x-mac-turkish
|
147 |
|
|
x-mac-ukrainian x-mac-chinesesimp x-mac-japanese x-mac-korean
|
148 |
|
|
shift_jis shift_jisx0213 x-sjis
|
149 |
|
|
tis-620
|
150 |
|
|
unicode-1-1-utf-7 unicode-1-1-utf-8
|
151 |
|
|
unicode-2-0-utf-7 unicode-2-0-utf-8
|
152 |
|
|
utf-7 utf-8 utf-9
|
153 |
|
|
viscii
|
154 |
|
|
windows-1250 windows-1251 windows-1252 windows-1253
|
155 |
|
|
windows-1254 windows-1255 windows-1256 windows-1257
|
156 |
|
|
windows-1258 windows-31j windows-949
|
157 |
|
|
)) { $_IsMimeText{$_} = 1 }
|
158 |
|
|
|
159 |
wakaba |
1.12 |
%MSNAME2IANANAME = (
|
160 |
|
|
'iso-2022-jp' => 'x-iso2022jp-cp932',
|
161 |
|
|
'ks_c_5601-1987' => 'windows-949',
|
162 |
|
|
);
|
163 |
|
|
|
164 |
wakaba |
1.4 |
sub make_charset ($%) {
|
165 |
|
|
my $name = shift;
|
166 |
|
|
return unless $name; ## Note: charset "0" is not supported.
|
167 |
|
|
my %definition = @_;
|
168 |
wakaba |
1.6 |
|
169 |
|
|
$definition{preferred_name} ||= $name;
|
170 |
wakaba |
1.4 |
if ($definition{preferred_name} ne $name
|
171 |
|
|
&& ref $CHARSET{$definition{preferred_name}}) {
|
172 |
|
|
## New charset is an alias of defined charset,
|
173 |
|
|
$CHARSET{$name} = $CHARSET{$definition{preferred_name}};
|
174 |
|
|
return;
|
175 |
|
|
} elsif ($definition{alias_of} && ref $CHARSET{$definition{alias_of}}) {
|
176 |
|
|
## New charset is an alias of defined charset,
|
177 |
|
|
$CHARSET{$name} = $CHARSET{$definition{alias_of}};
|
178 |
|
|
return;
|
179 |
|
|
}
|
180 |
|
|
$CHARSET{$name} = \%definition;
|
181 |
|
|
|
182 |
|
|
## Set default values
|
183 |
wakaba |
1.6 |
#$definition{encoder} ||= sub { $_[1] };
|
184 |
|
|
#$definition{decoder} ||= sub { $_[1] };
|
185 |
wakaba |
1.4 |
|
186 |
|
|
$definition{mime_text} = 0 unless defined $definition{mime_text};
|
187 |
|
|
$definition{cte_7bit_preferred} = 'base64'
|
188 |
|
|
unless defined $definition{cte_7bit_preferred};
|
189 |
|
|
}
|
190 |
wakaba |
1.1 |
|
191 |
|
|
sub encode ($$) {
|
192 |
|
|
my ($charset, $s) = (lc shift, shift);
|
193 |
wakaba |
1.13 |
my $c = ref $CHARSET{$charset}->{encoder}? $charset: '*undef';
|
194 |
|
|
if (ref $CHARSET{$c}->{encoder}) {
|
195 |
|
|
my ($t, %r) = &{$CHARSET{$c}->{encoder}} ($charset, $s);
|
196 |
|
|
unless (defined $r{success}) {
|
197 |
|
|
$r{success} = 1;
|
198 |
|
|
}
|
199 |
|
|
return ($t, %r);
|
200 |
wakaba |
1.1 |
}
|
201 |
wakaba |
1.5 |
($s, success => 0);
|
202 |
wakaba |
1.1 |
}
|
203 |
|
|
|
204 |
|
|
sub decode ($$) {
|
205 |
|
|
my ($charset, $s) = (lc shift, shift);
|
206 |
wakaba |
1.13 |
my $c = ref $CHARSET{$charset}->{decoder}? $charset: '*undef';
|
207 |
|
|
if (ref $CHARSET{$c}->{decoder}) {
|
208 |
|
|
my ($t, %r) = &{$CHARSET{$c}->{decoder}} ($charset, $s);
|
209 |
|
|
unless (defined $r{success}) {
|
210 |
|
|
$r{success} = 1;
|
211 |
|
|
}
|
212 |
|
|
return ($t, %r);
|
213 |
wakaba |
1.1 |
}
|
214 |
wakaba |
1.13 |
($s, success => 0);
|
215 |
wakaba |
1.1 |
}
|
216 |
|
|
|
217 |
|
|
sub name_normalize ($) {
|
218 |
|
|
my $name = lc shift;
|
219 |
wakaba |
1.14 |
if (ref $CHARSET{$name}->{preferred_name} eq 'CODE') {
|
220 |
|
|
return &{ $CHARSET{$name}->{preferred_name} } ($name);
|
221 |
|
|
} elsif ($CHARSET{$name}->{preferred_name}) {
|
222 |
|
|
return $CHARSET{$name}->{preferred_name};
|
223 |
|
|
} elsif (ref $CHARSET{'*undef'}->{preferred_name} eq 'CODE') {
|
224 |
|
|
return &{ $CHARSET{'*undef'}->{preferred_name} } ($name);
|
225 |
|
|
}
|
226 |
|
|
$name;
|
227 |
wakaba |
1.1 |
}
|
228 |
|
|
|
229 |
wakaba |
1.6 |
sub name_minimumize ($$) {
|
230 |
|
|
my ($charset, $s) = (lc shift, shift);
|
231 |
wakaba |
1.9 |
if (ref $CHARSET{$charset}->{name_minimumizer} eq 'CODE') {
|
232 |
wakaba |
1.6 |
return &{$CHARSET{$charset}->{name_minimumizer}} ($charset, $s);
|
233 |
wakaba |
1.9 |
} elsif (ref $_MINIMUMIZER{$charset}) {
|
234 |
|
|
return &{$_MINIMUMIZER{$charset}} ($charset, $s);
|
235 |
wakaba |
1.13 |
} elsif (ref $CHARSET{'*undef'}->{name_minimumizer} eq 'CODE') {
|
236 |
|
|
return &{$CHARSET{'*undef'}->{name_minimumizer}} ($charset, $s);
|
237 |
wakaba |
1.9 |
}
|
238 |
|
|
(charset => $charset);
|
239 |
|
|
}
|
240 |
|
|
|
241 |
wakaba |
1.12 |
sub msname2iananame ($) {
|
242 |
|
|
my $mscharset = shift;
|
243 |
|
|
$MSNAME2IANANAME{$mscharset} || $mscharset;
|
244 |
|
|
}
|
245 |
|
|
|
246 |
wakaba |
1.9 |
sub _name_7bit_iso2022 ($$) {shift;
|
247 |
|
|
my $s = shift;
|
248 |
|
|
if ($s =~ /[\x0E\x0F\x1B]/) {
|
249 |
|
|
return (charset => 'iso-2022-jp')
|
250 |
|
|
unless $s =~ /\x1B[^\x24\x28]
|
251 |
|
|
|\x1B\x24[^\x40B]
|
252 |
|
|
|\x1B\x28[^BJ]
|
253 |
|
|
|\x0E|\x0F/x;
|
254 |
|
|
return (charset => 'iso-2022-jp-1')
|
255 |
|
|
unless $s =~ /\x1B[^\x24\x28]
|
256 |
|
|
|\x1B\x24[^\x40B\x28]
|
257 |
|
|
|\x1B\x24\x28[^D]
|
258 |
|
|
|\x1B\x28[^BJ]
|
259 |
|
|
|\x0E|\x0F/x;
|
260 |
|
|
return (charset => 'iso-2022-jp-3-plane1')
|
261 |
|
|
unless $s =~ /\x1B[^\x24\x28]
|
262 |
|
|
|\x1B\x24[^\x28] #[^B\x28]
|
263 |
|
|
|\x1B\x24\x28[^O]
|
264 |
|
|
|\x1B\x28[^B]
|
265 |
|
|
|\x0E|\x0F/x;
|
266 |
|
|
return (charset => 'iso-2022-jp-3')
|
267 |
|
|
unless $s =~ /\x1B[^\x24\x28]
|
268 |
|
|
|\x1B\x24[^\x28] #[^B\x28]
|
269 |
|
|
|\x1B\x24\x28[^OP]
|
270 |
|
|
|\x1B\x28[^B]
|
271 |
|
|
|\x0E|\x0F/x;
|
272 |
|
|
return (charset => 'iso-2022-kr')
|
273 |
|
|
unless $s =~ /\x1B[^\x24]
|
274 |
|
|
|\x1B\x24[^\x29]
|
275 |
|
|
|\x1B\x24\x29[^C]/x;
|
276 |
|
|
return (charset => 'iso-2022-jp-2')
|
277 |
|
|
unless $s =~ /\x1B[^\x24\x28\x2E\x4E]
|
278 |
|
|
|\x1B\x24[^\x40AB\x28]
|
279 |
|
|
|\x1B\x24\x28[^CD]
|
280 |
|
|
|\x1B\x28[^BJ]
|
281 |
|
|
|\x1B\x2E[^AF]
|
282 |
|
|
|\x0E|\x0F/x;
|
283 |
|
|
return (charset => 'iso-2022-cn')
|
284 |
|
|
unless $s =~ /\x1B[^\x4E\x24]
|
285 |
|
|
|\x1B\x24[^\x29\x2A]
|
286 |
|
|
|\x1B\x24\x29[^AG]
|
287 |
|
|
|\x1B\x24\x2A[^H]/x;
|
288 |
|
|
return (charset => 'iso-2022-cn-ext')
|
289 |
|
|
unless $s =~ /\x1B[^\x4E\x4F\x24]
|
290 |
|
|
|\x1B\x24[^\x29\x2A]
|
291 |
|
|
|\x1B\x24\x29[^AEG]
|
292 |
|
|
|\x1B\x24\x2A[^HIJKLM]/x;
|
293 |
|
|
return (charset => 'iso-2022-int-1')
|
294 |
|
|
unless $s =~ /\x1B[^\x24\x28\x2D]
|
295 |
|
|
|\x1B\x24[^\x40AB\x28\x29]
|
296 |
|
|
|\x1B\x24\x28[^DGH]
|
297 |
|
|
|\x1B\x24\x29[^C]
|
298 |
|
|
|\x1B\x28[^BJ]
|
299 |
|
|
|\x1B\x2D[^AF]/x;
|
300 |
|
|
return (charset => 'junet')
|
301 |
|
|
unless $s =~ /\x1B[^\x24\x28\x2C]
|
302 |
|
|
|\x1B\x24[^\x28\x2C\x40-\x42]
|
303 |
|
|
|\x1B\x24[\x28\x2C][^\x20-\x7E]
|
304 |
|
|
|\x1B\x24[\x28\x2C][\x20-\x2F]+[^\x30-\x7E]
|
305 |
|
|
|\x1B[\x28\x2C][^\x20-\x7E]
|
306 |
|
|
|\x1B[\x28\x2C][\x20-\x2F]+[^\x30-\x7E]
|
307 |
|
|
|\x0E|\x0F/x;
|
308 |
|
|
return (charset => 'x-iso-2022');
|
309 |
|
|
} else {
|
310 |
|
|
return (charset => 'us-ascii');
|
311 |
wakaba |
1.6 |
}
|
312 |
|
|
}
|
313 |
|
|
|
314 |
wakaba |
1.9 |
sub _name_net_ascii_8bit ($) {
|
315 |
|
|
my $name = shift; my $s = shift;
|
316 |
wakaba |
1.8 |
return (charset => 'us-ascii') unless $s =~ /[\x1B\x0E\x0F\x80-\xFF]/;
|
317 |
|
|
if ($s =~ /[\x80-\xFF]/) {
|
318 |
|
|
if ($s =~ /[\xC0-\xFD][\x80-\xBF]*[\x80-\x8F]/) {
|
319 |
|
|
if ($s =~ /\x1B/) {
|
320 |
wakaba |
1.9 |
return (charset => 'x-junet8'); ## junet + UTF-8
|
321 |
wakaba |
1.8 |
} else {
|
322 |
|
|
return (charset => 'utf-8');
|
323 |
|
|
}
|
324 |
|
|
} elsif ($s =~ /\x1B/) {
|
325 |
wakaba |
1.9 |
return (charset => 'x-iso-2022'); ## 8bit ISO 2022
|
326 |
|
|
} else {
|
327 |
|
|
return (charset => 'iso-8859-1');
|
328 |
|
|
}
|
329 |
|
|
} else { ## 7bit ISO 2022
|
330 |
|
|
return _name_7bit_iso2022 ($name, $s);
|
331 |
|
|
}
|
332 |
|
|
}
|
333 |
|
|
|
334 |
wakaba |
1.11 |
sub _name_8bit_iso2022 ($$) {
|
335 |
wakaba |
1.9 |
my $name = shift; my $s = shift;
|
336 |
|
|
return (charset => 'us-ascii') unless $s =~ /[\x1B\x0E\x0F\x80-\xFF]/;
|
337 |
|
|
if ($s =~ /[\x80-\xFF]/) {
|
338 |
|
|
if ($s =~ /\x1B/) {
|
339 |
|
|
return (charset => 'x-iso-2022'); ## 8bit ISO 2022
|
340 |
wakaba |
1.8 |
} else {
|
341 |
|
|
return (charset => 'iso-8859-1');
|
342 |
|
|
}
|
343 |
wakaba |
1.9 |
} else { ## 7bit ISO 2022
|
344 |
|
|
return _name_7bit_iso2022 ($name, $s);
|
345 |
|
|
}
|
346 |
|
|
}
|
347 |
|
|
|
348 |
|
|
## Not completed.
|
349 |
|
|
## TODO: gb18030, cn-gb-12345
|
350 |
|
|
## TODO: _name_euc_gbf (cn-gb-12345, gb2312)
|
351 |
|
|
sub _name_euc_gb ($$) {
|
352 |
|
|
my $name = shift; my $s = shift;
|
353 |
|
|
if ($s =~ /[\x80-\xFF]/) {
|
354 |
|
|
if ($s =~ /
|
355 |
|
|
(?:\G|[\x00-\x3F\x7F\x80\xFF])
|
356 |
|
|
(?:[\xA1-\xA9\xB0-\xFE][\xA1-\xFE]
|
357 |
|
|
|[\x40-\x7E])*
|
358 |
|
|
(?:
|
359 |
|
|
[\x81-\xA0\xAA-\xAF][\x40-\xFE]
|
360 |
|
|
|[\xA1-\xFE][\x40-\xA0]
|
361 |
|
|
)
|
362 |
|
|
/x) {
|
363 |
|
|
(charset => 'gbk');
|
364 |
|
|
} elsif ($s =~ /
|
365 |
|
|
(?:\xA2[\xA1-\xAA]
|
366 |
|
|
|\xA6[\xE0-\xF5]
|
367 |
|
|
|\xA8[\xBB-\xC0]
|
368 |
|
|
)
|
369 |
|
|
(?=(?:[\xA1-\xFE][\xA1-\xFE])*(?:[\x00-\xA0\xFF]|\z))
|
370 |
|
|
/x) {
|
371 |
|
|
(charset => 'gbk');
|
372 |
|
|
} elsif ($s =~ /
|
373 |
|
|
(?:\xA3\xE7|\xA7[\xDD-\xF2]
|
374 |
|
|
|\xA8[\xBB-\xC0]
|
375 |
|
|
|[\xAA-\xAF\xF8-\xFE][\xA1-\xFE]
|
376 |
|
|
)
|
377 |
|
|
(?=(?:[\xA1-\xFE][\xA1-\xFE])*(?:[\x00-\xA0\xFF]|\z))
|
378 |
|
|
/x) {
|
379 |
|
|
(charset => 'cn-gb-isoir165', 'charset-edition' => 1992);
|
380 |
|
|
} elsif ($s =~ /\xEF\xF1 ## Typo bug of GB 2312
|
381 |
|
|
(?=(?:[\xA1-\xFE][\xA1-\xFE])*(?:[\x00-\xA0\xFF]|\z))
|
382 |
|
|
/x) {
|
383 |
|
|
(charset => 'gb2312');
|
384 |
|
|
} else {
|
385 |
|
|
(charset => 'gb2312', 'charset-edition' => 1980);
|
386 |
|
|
}
|
387 |
|
|
} elsif ($s =~ /[\x0E\x0F]/) {
|
388 |
|
|
(charset => 'gb2312'); ## Actually, this is not "gb2312"
|
389 |
|
|
} else {
|
390 |
wakaba |
1.11 |
_name_7bit_iso2022 ($name, $s);
|
391 |
wakaba |
1.9 |
}
|
392 |
|
|
}
|
393 |
|
|
|
394 |
|
|
sub _name_euc_japan ($$) {
|
395 |
|
|
my $name = shift; my $s = shift;
|
396 |
|
|
if ($s =~ /[\x80-\xFF]/) {
|
397 |
|
|
if ($s =~ /\x8F[\xA1\xA3-\xA5\xA8\xAC-\xAF\xEE-\xFE][\xA1-\xFE]/) {
|
398 |
|
|
if ($s =~ /\x8F[\xA2\xA6\xA7\xA9-\xAB\xB0-\xED][\xA1-\xFE]/) {
|
399 |
|
|
## JIS X 0213 plane 2 + JIS X 0212
|
400 |
|
|
(charset => 'x-euc-jisx0213-packed');
|
401 |
|
|
} else {
|
402 |
|
|
(charset => 'euc-jisx0213');
|
403 |
|
|
}
|
404 |
|
|
} elsif ($s =~ m{(?<![\x8E\x8F]) ## Not G2/G3 character
|
405 |
|
|
(?: ## JIS X 0213:2000
|
406 |
|
|
[\xA9-\xAF\xF5-\xFE][\xA1-\xFE]
|
407 |
|
|
|\xA2[\xAF-\xB9\xC2-\xC9\xD1-\xDB\xE9-\xF1\xFA-\xFD]
|
408 |
|
|
|\xA3[\xA1-\xAF\xBA-\xC0\xDB-\xE0\xFB-\xFE]
|
409 |
|
|
|\xA4[\xF4-\xFE]|\xA5[\xF7-\xFE]
|
410 |
|
|
|\xA6[\xB9-\xC0\xD9-\xFE]|\xA7[\xC2-\xD0\xF2-\xFE]
|
411 |
|
|
|\xA8[\xC1-\xFE]|\xCF[\xD4-\xFE]|\xF4[\xA7-\xFE]
|
412 |
|
|
)
|
413 |
|
|
(?=(?:[\xA1-\xFE][\xA1-\xFE])*(?:[\x00-\xA0\xFF]|\z))}x) {
|
414 |
|
|
if ($s =~ /\x8F/) { ## JIS X 0213 plane 1 + JIS X 0212
|
415 |
|
|
(charset => 'x-euc-jisx0213-packed');
|
416 |
|
|
} else {
|
417 |
|
|
(charset => 'euc-jisx0213-plane1');
|
418 |
|
|
}
|
419 |
|
|
} else {
|
420 |
|
|
(charset => 'euc-jp');
|
421 |
|
|
}
|
422 |
|
|
} elsif ($s =~ /\x0E|\x0F|\x1B[\x4E\x4F]/) {
|
423 |
|
|
(charset => 'euc-jisx0213'); ## Actually, this is not euc-japan
|
424 |
|
|
} else {
|
425 |
wakaba |
1.11 |
_name_7bit_iso2022 ($name, $s);
|
426 |
wakaba |
1.9 |
}
|
427 |
|
|
}
|
428 |
|
|
|
429 |
|
|
sub _name_shift_jis ($$) {
|
430 |
|
|
my $name = shift; my $s = shift;
|
431 |
|
|
if ($s =~ /[\x80-\xFF]/) {
|
432 |
|
|
if ($s =~ /[\x0E\x0F\x1B]/) {
|
433 |
|
|
(charset => 'x-sjis');
|
434 |
|
|
} elsif ($s =~ /
|
435 |
|
|
(?:\G|[\x00-\x3F\x7F])
|
436 |
|
|
(?:[\x81-\x9F\xE0-\xFC][\x40-\x7E\x80-\xFC]
|
437 |
|
|
|[\x40-\x7E\xA1-\xDF])*
|
438 |
|
|
[\xF0-\xFC][\x40-\x7E\x80-\xFC]
|
439 |
|
|
/x) {
|
440 |
|
|
(charset => 'shift_jisx0213');
|
441 |
|
|
} elsif ($s =~ /
|
442 |
|
|
(?:\G|[\x00-\x3F\x7F])
|
443 |
|
|
(?:[\x81-\x9F\xE0-\xFC][\x40-\x7E\x80-\xFC]
|
444 |
|
|
|[\x40-\x7E\xA1-\xDF])*
|
445 |
|
|
(?:
|
446 |
|
|
[\x85-\x87\xEB-\xEF][\x40-\x7E\x80-\xFC]
|
447 |
|
|
|\x81[\xAD-\xB7\xC0-\xC7\xCF-\xD9\xE9-\xEF\xF8-\xFB]
|
448 |
|
|
|\x82[\x40-\x4E\x59-\x5F\x7A-\x80\x9B-\x9E\xF2-\xFC]
|
449 |
|
|
|\x83[\x97-\x9E\xB7-\xBE\xD7-\xFC]
|
450 |
|
|
|\x84[\x61-\x6F\x72-\x9E\xBF-\xFC]
|
451 |
|
|
|\x88[\x40-\x9E]|\x98[\x73-\x9E]|\xEA[\xA5-\xFC]
|
452 |
|
|
)
|
453 |
|
|
/x) {
|
454 |
|
|
(charset => 'shift_jisx0213-plane1');
|
455 |
|
|
} else {
|
456 |
|
|
(charset => 'shift_jis');
|
457 |
|
|
}
|
458 |
|
|
} elsif ($s =~ /[\x5C\x7E]/) {
|
459 |
|
|
if ($s =~ /\x1B\x0E\x0F/) {
|
460 |
|
|
(charset => 'x-sjis'); ## ISO 2022 with implied "ESC ( J"
|
461 |
|
|
## BUG: "ESC ( B foobar\aaa ESC ( J aiueo" also matchs this
|
462 |
|
|
} else {
|
463 |
|
|
(charset => 'jis_x0201');
|
464 |
|
|
}
|
465 |
|
|
} else {
|
466 |
wakaba |
1.11 |
_name_7bit_iso2022 ($name, $s);
|
467 |
wakaba |
1.9 |
}
|
468 |
|
|
}
|
469 |
|
|
|
470 |
|
|
sub _name_utf16be ($$) {
|
471 |
|
|
shift; my $s = shift;
|
472 |
|
|
if ($s =~ /[\xD8-\xDB][\x00-\xFF][\xDC-\xDF][\x00-\xFF]
|
473 |
|
|
(?=(?:[\x00-\xFF][\x00-\xFF])*\z)/sx) {
|
474 |
|
|
(charset => 'utf-16be');
|
475 |
|
|
} elsif ($s =~ /[\x01-\xFF][\x00-\xFF]
|
476 |
|
|
(?=(?:[\x00-\xFF][\x00-\xFF])*\z)/sx) {
|
477 |
|
|
if ($s =~ /([^\x00\x03\x04\x23\x25\x30\xFE\xFF]
|
478 |
|
|
[\x00-\xFF] # ^\x20\x22\x4E-\x9F\xF9\xFA
|
479 |
|
|
|\x03[^\x00-\x6F\xD0-\xFF]
|
480 |
|
|
#|\x20[^\x00-\x6F]
|
481 |
|
|
|\x25[^\x00-\x7F]
|
482 |
|
|
|\xFE[^\x30-\x4F]
|
483 |
|
|
|\xFF[^\x00-\xEF]
|
484 |
|
|
## note 1 of RFC 1816 is ambitious, so block entire
|
485 |
|
|
## is excepted
|
486 |
|
|
|\x30[\x00-\x3F]
|
487 |
|
|
)
|
488 |
|
|
(?=(?:[\x00-\xFF][\x00-\xFF])*\z)/sx) {
|
489 |
|
|
(charset => 'iso-10646-ucs-2');
|
490 |
|
|
} else {
|
491 |
|
|
(charset => 'iso-10646-j-1');
|
492 |
|
|
}
|
493 |
|
|
} elsif ($s =~ /\x00[\x80-\xFF]
|
494 |
|
|
(?=(?:[\x00-\xFF][\x00-\xFF])*\z)/sx) {
|
495 |
|
|
(charset => 'iso-10646-unicode-latin1');
|
496 |
|
|
} else {
|
497 |
|
|
(charset => 'iso-10646-ucs-basic');
|
498 |
|
|
}
|
499 |
|
|
}
|
500 |
|
|
|
501 |
|
|
sub _name_utf32be ($$) {
|
502 |
|
|
shift; my $s = shift;
|
503 |
|
|
if ($s =~ /
|
504 |
|
|
([\x01-\x7F][\x00-\xFF]{3}
|
505 |
|
|
|\x00[\x11-\xFF][\x00-\xFF][\x00-\xFF])
|
506 |
|
|
(?=(?:[\x00-\xFF]{4})*\z)/sx) {
|
507 |
|
|
(charset => 'iso-10646-ucs-4');
|
508 |
|
|
} else {
|
509 |
|
|
(charset => 'utf-32be');
|
510 |
wakaba |
1.8 |
}
|
511 |
|
|
}
|
512 |
|
|
|
513 |
wakaba |
1.13 |
sub _utf8_on ($) {
|
514 |
|
|
Encode::_utf8_on ($_[0]) if $Encode::VERSION;
|
515 |
|
|
}
|
516 |
|
|
sub _utf8_off ($) {
|
517 |
|
|
Encode::_utf8_off ($_[0]) if $Encode::VERSION;
|
518 |
|
|
}
|
519 |
|
|
|
520 |
wakaba |
1.14 |
sub is_mime_text ($) {
|
521 |
|
|
my $name = lc shift;
|
522 |
|
|
if (ref $CHARSET{$name}->{mime_text} eq 'CODE') {
|
523 |
|
|
return &{ $CHARSET{$name}->{mime_text} } ($name);
|
524 |
|
|
} elsif (defined $CHARSET{$name}->{mime_text}) {
|
525 |
|
|
return $CHARSET{$name}->{mime_text};
|
526 |
|
|
} elsif (defined $_IsMimeText{$name}) {
|
527 |
|
|
return $_IsMimeText{$name};
|
528 |
|
|
} elsif (ref $CHARSET{'*undef'}->{mime_text} eq 'CODE') {
|
529 |
|
|
return &{ $CHARSET{'*undef'}->{mime_text} } ($name);
|
530 |
|
|
}
|
531 |
|
|
0;
|
532 |
|
|
}
|
533 |
|
|
|
534 |
wakaba |
1.1 |
=head1 LICENSE
|
535 |
|
|
|
536 |
|
|
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
|
537 |
|
|
|
538 |
|
|
This program is free software; you can redistribute it and/or modify
|
539 |
|
|
it under the terms of the GNU General Public License as published by
|
540 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
541 |
|
|
(at your option) any later version.
|
542 |
|
|
|
543 |
|
|
This program is distributed in the hope that it will be useful,
|
544 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
545 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
546 |
|
|
GNU General Public License for more details.
|
547 |
|
|
|
548 |
|
|
You should have received a copy of the GNU General Public License
|
549 |
|
|
along with this program; see the file COPYING. If not, write to
|
550 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
551 |
|
|
Boston, MA 02111-1307, USA.
|
552 |
|
|
|
553 |
|
|
=head1 CHANGE
|
554 |
|
|
|
555 |
|
|
See F<ChangeLog>.
|
556 |
wakaba |
1.16 |
$Date: 2002/07/26 12:42:00 $
|
557 |
wakaba |
1.1 |
|
558 |
|
|
=cut
|
559 |
|
|
|
560 |
|
|
1;
|