1 package Encode::CN::HZ;
6 $VERSION = do { my @r = (q$Revision: 2.0 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
8 use Encode qw(:fallbacks);
10 use base qw(Encode::Encoding);
11 __PACKAGE__->Define('hz');
13 # HZ is a combination of ASCII and escaped GB, so we implement it
14 # with the GB2312(raw) encoding here. Cf. RFCs 1842 & 1843.
16 # not ported for EBCDIC. Which should be used, "~" or "\x7E"?
22 my ($obj,$str,$chk) = @_;
24 my $GB = Encode::find_encoding('gb2312-raw');
26 my $in_ascii = 1; # default mode is ASCII.
29 if ($in_ascii) { # ASCII mode
30 if ($str =~ s/^([\x00-\x7D\x7F]+)//) { # no '~' => ASCII
32 # EBCDIC should need ascii2native, but not ported.
34 elsif ($str =~ s/^\x7E\x7E//) { # escaped tilde
37 elsif ($str =~ s/^\x7E\cJ//) { # '\cJ' == LF in ASCII
40 elsif ($str =~ s/^\x7E\x7B//) { # '~{'
41 $in_ascii = 0; # to GB
43 else { # encounters an invalid escape, \x80 or greater
47 else { # GB mode; the byte ranges are as in RFC 1843.
48 if ($str =~ s/^((?:[\x21-\x77][\x21-\x7E])+)//) {
49 $ret .= $GB->decode($1, $chk);
51 elsif ($str =~ s/^\x7E\x7D//) { # '~}'
59 $_[1] = '' if $chk; # needs_lines guarantees no partial character
64 my ($obj, undef, $src, $pos, $trm, $chk) = @_;
65 my ($rdst, $rsrc, $rpos) = \@_[1..3];
67 my $GB = Encode::find_encoding('gb2312-raw');
69 my $in_ascii = 1; # default mode is ASCII.
71 my $ini_pos = pos($$rsrc);
73 substr($src, 0, $pos) = '';
75 my $ini_len = bytes::length($src);
77 # $trm is the first of the pair '~~', then 2nd tilde is to be removed.
78 # XXX: Is better C<$src =~ s/^\x7E// or die if ...>?
79 $src =~ s/^\x7E// if $trm eq "\x7E";
83 if ($in_ascii) { # ASCII mode
84 if ($src =~ s/^([\x00-\x7D\x7F])//) { # no '~' => ASCII
87 elsif ($src =~ s/^\x7E\x7E//) { # escaped tilde
90 elsif ($src =~ s/^\x7E\cJ//) { # '\cJ' == LF in ASCII
93 elsif ($src =~ s/^\x7E\x7B//) { # '~{'
94 $in_ascii = 0; # to GB
97 else { # encounters an invalid escape, \x80 or greater
101 else { # GB mode; the byte ranges are as in RFC 1843.
102 if ($src =~ s/^((?:[\x21-\x77][\x21-\x7F])+)//) {
103 $now = $GB->decode($1, $chk);
105 elsif ($src =~ s/^\x7E\x7D//) { # '~}'
114 next if ! defined $now;
120 $$rpos = $ini_pos + $pos + $ini_len - bytes::length($src);
121 pos($$rsrc) = $ini_pos;
127 $$rpos = $ini_pos + $pos + $ini_len - bytes::length($src);
128 pos($$rsrc) = $ini_pos;
129 return ''; # terminator not found
135 my ($obj,$str,$chk) = @_;
137 my $GB = Encode::find_encoding('gb2312-raw');
139 my $in_ascii = 1; # default mode is ASCII.
141 no warnings 'utf8'; # $str may be malformed UTF8 at the end of a chunk.
143 while (length $str) {
144 if ($str =~ s/^([[:ascii:]]+)//) {
146 $tmp =~ s/~/~~/g; # escapes tildes
148 $ret .= "\x7E\x7D"; # '~}'
151 $ret .= pack 'a*', $tmp; # remove UTF8 flag.
153 elsif ($str =~ s/(.)//) {
154 my $tmp = $GB->encode($1, $chk);
155 last if !defined $tmp;
156 if (length $tmp == 2) { # maybe a valid GB char (XXX)
158 $ret .= "\x7E\x7B"; # '~{'
163 elsif (length $tmp) { # maybe FALLBACK in ASCII (XXX)
165 $ret .= "\x7E\x7D"; # '~}'
171 else { # if $str is malformed UTF8 *and* if length $str != 0.
175 $_[1] = $str if $chk;
177 # The state at the end of the chunk is discarded, even if in GB mode.
178 # That results in the combination of GB-OUT and GB-IN, i.e. "~}~{".
179 # Parhaps it is harmless, but further investigations may be required...
182 $ret .= "\x7E\x7D"; # '~}'
193 Encode::CN::HZ -- internally used by Encode::CN