1 package Encode::CN::HZ;
8 $VERSION = do { my @r = ( q$Revision: 2.5 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r };
10 use Encode qw(:fallbacks);
12 use base qw(Encode::Encoding);
13 __PACKAGE__->Define('hz');
15 # HZ is a combination of ASCII and escaped GB, so we implement it
16 # with the GB2312(raw) encoding here. Cf. RFCs 1842 & 1843.
18 # not ported for EBCDIC. Which should be used, "~" or "\x7E"?
23 my ( $obj, $str, $chk ) = @_;
25 my $GB = Encode::find_encoding('gb2312-raw');
27 my $in_ascii = 1; # default mode is ASCII.
29 while ( length $str ) {
30 if ($in_ascii) { # ASCII mode
31 if ( $str =~ s/^([\x00-\x7D\x7F]+)// ) { # no '~' => ASCII
34 # EBCDIC should need ascii2native, but not ported.
36 elsif ( $str =~ s/^\x7E\x7E// ) { # escaped tilde
39 elsif ( $str =~ s/^\x7E\cJ// ) { # '\cJ' == LF in ASCII
42 elsif ( $str =~ s/^\x7E\x7B// ) { # '~{'
43 $in_ascii = 0; # to GB
45 else { # encounters an invalid escape, \x80 or greater
49 else { # GB mode; the byte ranges are as in RFC 1843.
50 no warnings 'uninitialized';
51 if ( $str =~ s/^((?:[\x21-\x77][\x21-\x7E])+)// ) {
52 $ret .= $GB->decode( $1, $chk );
54 elsif ( $str =~ s/^\x7E\x7D// ) { # '~}'
62 $_[1] = '' if $chk; # needs_lines guarantees no partial character
67 my ( $obj, undef, $src, $pos, $trm, $chk ) = @_;
68 my ( $rdst, $rsrc, $rpos ) = \@_[ 1 .. 3 ];
70 my $GB = Encode::find_encoding('gb2312-raw');
72 my $in_ascii = 1; # default mode is ASCII.
74 my $ini_pos = pos($$rsrc);
76 substr( $src, 0, $pos ) = '';
78 my $ini_len = bytes::length($src);
80 # $trm is the first of the pair '~~', then 2nd tilde is to be removed.
81 # XXX: Is better C<$src =~ s/^\x7E// or die if ...>?
82 $src =~ s/^\x7E// if $trm eq "\x7E";
84 while ( length $src ) {
86 if ($in_ascii) { # ASCII mode
87 if ( $src =~ s/^([\x00-\x7D\x7F])// ) { # no '~' => ASCII
90 elsif ( $src =~ s/^\x7E\x7E// ) { # escaped tilde
93 elsif ( $src =~ s/^\x7E\cJ// ) { # '\cJ' == LF in ASCII
96 elsif ( $src =~ s/^\x7E\x7B// ) { # '~{'
97 $in_ascii = 0; # to GB
100 else { # encounters an invalid escape, \x80 or greater
104 else { # GB mode; the byte ranges are as in RFC 1843.
105 if ( $src =~ s/^((?:[\x21-\x77][\x21-\x7F])+)// ) {
106 $now = $GB->decode( $1, $chk );
108 elsif ( $src =~ s/^\x7E\x7D// ) { # '~}'
117 next if !defined $now;
121 if ( $now eq $trm ) {
123 $$rpos = $ini_pos + $pos + $ini_len - bytes::length($src);
124 pos($$rsrc) = $ini_pos;
130 $$rpos = $ini_pos + $pos + $ini_len - bytes::length($src);
131 pos($$rsrc) = $ini_pos;
132 return ''; # terminator not found
136 my ( $obj, $str, $chk ) = @_;
138 my $GB = Encode::find_encoding('gb2312-raw');
140 my $in_ascii = 1; # default mode is ASCII.
142 no warnings 'utf8'; # $str may be malformed UTF8 at the end of a chunk.
144 while ( length $str ) {
145 if ( $str =~ s/^([[:ascii:]]+)// ) {
147 $tmp =~ s/~/~~/g; # escapes tildes
149 $ret .= "\x7E\x7D"; # '~}'
152 $ret .= pack 'a*', $tmp; # remove UTF8 flag.
154 elsif ( $str =~ s/(.)// ) {
156 my $tmp = $GB->encode( $s, $chk );
157 last if !defined $tmp;
158 if ( length $tmp == 2 ) { # maybe a valid GB char (XXX)
160 $ret .= "\x7E\x7B"; # '~{'
165 elsif ( length $tmp ) { # maybe FALLBACK in ASCII (XXX)
167 $ret .= "\x7E\x7D"; # '~}'
173 else { # if $str is malformed UTF8 *and* if length $str != 0.
177 $_[1] = $str if $chk;
179 # The state at the end of the chunk is discarded, even if in GB mode.
180 # That results in the combination of GB-OUT and GB-IN, i.e. "~}~{".
181 # Parhaps it is harmless, but further investigations may be required...
184 $ret .= "\x7E\x7D"; # '~}'
187 utf8::encode($ret); # https://rt.cpan.org/Ticket/Display.html?id=35120
196 Encode::CN::HZ -- internally used by Encode::CN