| 1 | package Encode::CN::HZ; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 9 | ▶ | use utf8 (); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 9 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 9 | $VERSION = '2.11'; | |||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | 9 | ▶ | use Encode qw(:fallbacks); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | 9 | ▶ | use parent qw(Encode::Encoding); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | 9 | __PACKAGE__->Define('hz'); | |||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 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. | ||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | # not ported for EBCDIC. Which should be used, "~" or "\x7E"? | ||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | 4 | ▶ | sub needs_lines { 1 } | ||||||||||||||||||||
|
Subroutine
needs_lines: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | 13 | ▶ | sub decode ($$;$) { | ||||||||||||||||||||
|
Subroutine
decode: called CC=11 SCAR=24.7
Pod
documented
| |||||||||||||||||||||||
| 23 | 13 | my ( $obj, $str, $chk ) = @_; | |||||||||||||||||||||
| 24 | 13 | ▶ | return undef unless defined $str; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | 11 | my $GB = Encode::find_encoding('gb2312-raw'); | |||||||||||||||||||||
| 27 | 11 | my $ret = substr($str, 0, 0); # to propagate taintedness | |||||||||||||||||||||
| 28 | 11 | my $in_ascii = 1; # default mode is ASCII. | |||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | 11 | ▶ | while ( length $str ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 31 | 3011 | ▶ | if ($in_ascii) { # ASCII mode | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 32 | 1509 | ▶ | if ( $str =~ s/^([\x00-\x7D\x7F]+)// ) { # no '~' => ASCII | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 33 | 758 | $ret .= $1; | |||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | # EBCDIC should need ascii2native, but not ported. | ||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||
| 37 | elsif ( $str =~ s/^\x7E\x7E// ) { # escaped tilde | ||||||||||||||||||||||
| 38 | 0 | $ret .= '~'; | |||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||
| 40 | elsif ( $str =~ s/^\x7E\cJ// ) { # '\cJ' == LF in ASCII | ||||||||||||||||||||||
| 41 | 0 | 1; # no-op | |||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||
| 43 | elsif ( $str =~ s/^\x7E\x7B// ) { # '~{' | ||||||||||||||||||||||
| 44 | 751 | $in_ascii = 0; # to GB | |||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||
| 46 | else { # encounters an invalid escape, \x80 or greater | ||||||||||||||||||||||
| 47 | 0 | last; | |||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||
| 50 | else { # GB mode; the byte ranges are as in RFC 1843. | ||||||||||||||||||||||
| 51 | 9 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 52 | 1502 | ▶ | if ( $str =~ s/^((?:[\x21-\x77][\x21-\x7E])+)// ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 53 | 751 | my $prefix = $1; | |||||||||||||||||||||
| 54 | 751 | $ret .= $GB->decode( $prefix, $chk ); | |||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||
| 56 | elsif ( $str =~ s/^\x7E\x7D// ) { # '~}' | ||||||||||||||||||||||
| 57 | 751 | $in_ascii = 1; | |||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||
| 59 | else { # invalid | ||||||||||||||||||||||
| 60 | 0 | last; | |||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||
| 64 | 11 | ▶ | $_[1] = '' if $chk; # needs_lines guarantees no partial character | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 65 | 11 | return $ret; | |||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | 0 | ▶ | sub cat_decode { | ||||||||||||||||||||
|
Subroutine
cat_decode: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||
| 69 | 0 | my ( $obj, undef, $src, $pos, $trm, $chk ) = @_; | |||||||||||||||||||||
| 70 | 0 | my ( $rdst, $rsrc, $rpos ) = \@_[ 1 .. 3 ]; | |||||||||||||||||||||
| 71 | |||||||||||||||||||||||
| 72 | 0 | my $GB = Encode::find_encoding('gb2312-raw'); | |||||||||||||||||||||
| 73 | 0 | my $ret = ''; | |||||||||||||||||||||
| 74 | 0 | my $in_ascii = 1; # default mode is ASCII. | |||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | 0 | my $ini_pos = pos($$rsrc); | |||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | 0 | substr( $src, 0, $pos ) = ''; | |||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | 0 | my $ini_len = bytes::length($src); | |||||||||||||||||||||
| 81 | |||||||||||||||||||||||
| 82 | # $trm is the first of the pair '~~', then 2nd tilde is to be removed. | ||||||||||||||||||||||
| 83 | # XXX: Is better C<$src =~ s/^\x7E// or die if ...>? | ||||||||||||||||||||||
| 84 | 0 | ▶ | $src =~ s/^\x7E// if $trm eq "\x7E"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | 0 | ▶ | while ( length $src ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 87 | 0 | my $now; | |||||||||||||||||||||
| 88 | 0 | ▶ | if ($in_ascii) { # ASCII mode | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 89 | 0 | ▶ | if ( $src =~ s/^([\x00-\x7D\x7F])// ) { # no '~' => ASCII | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 90 | 0 | $now = $1; | |||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||
| 92 | elsif ( $src =~ s/^\x7E\x7E// ) { # escaped tilde | ||||||||||||||||||||||
| 93 | 0 | $now = '~'; | |||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||
| 95 | elsif ( $src =~ s/^\x7E\cJ// ) { # '\cJ' == LF in ASCII | ||||||||||||||||||||||
| 96 | 0 | next; | |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||
| 98 | elsif ( $src =~ s/^\x7E\x7B// ) { # '~{' | ||||||||||||||||||||||
| 99 | 0 | $in_ascii = 0; # to GB | |||||||||||||||||||||
| 100 | 0 | next; | |||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||
| 102 | else { # encounters an invalid escape, \x80 or greater | ||||||||||||||||||||||
| 103 | 0 | last; | |||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||
| 106 | else { # GB mode; the byte ranges are as in RFC 1843. | ||||||||||||||||||||||
| 107 | 0 | ▶ | if ( $src =~ s/^((?:[\x21-\x77][\x21-\x7F])+)// ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 108 | 0 | $now = $GB->decode( $1, $chk ); | |||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||
| 110 | elsif ( $src =~ s/^\x7E\x7D// ) { # '~}' | ||||||||||||||||||||||
| 111 | 0 | $in_ascii = 1; | |||||||||||||||||||||
| 112 | 0 | next; | |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||
| 114 | else { # invalid | ||||||||||||||||||||||
| 115 | 0 | last; | |||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | 0 | ▶ | next if !defined $now; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 120 | |||||||||||||||||||||||
| 121 | 0 | $ret .= $now; | |||||||||||||||||||||
| 122 | |||||||||||||||||||||||
| 123 | 0 | ▶ | if ( $now eq $trm ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 124 | 0 | $$rdst .= $ret; | |||||||||||||||||||||
| 125 | 0 | $$rpos = $ini_pos + $pos + $ini_len - bytes::length($src); | |||||||||||||||||||||
| 126 | 0 | pos($$rsrc) = $ini_pos; | |||||||||||||||||||||
| 127 | 0 | return 1; | |||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | 0 | $$rdst .= $ret; | |||||||||||||||||||||
| 132 | 0 | $$rpos = $ini_pos + $pos + $ini_len - bytes::length($src); | |||||||||||||||||||||
| 133 | 0 | pos($$rsrc) = $ini_pos; | |||||||||||||||||||||
| 134 | 0 | return ''; # terminator not found | |||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | 492 | ▶ | sub encode($$;$) { | ||||||||||||||||||||
|
Subroutine
encode: called CC=14 SCAR=26.6
Pod
documented
| |||||||||||||||||||||||
| 138 | 492 | my ( $obj, $str, $chk ) = @_; | |||||||||||||||||||||
| 139 | 492 | ▶ | return undef unless defined $str; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | 490 | my $GB = Encode::find_encoding('gb2312-raw'); | |||||||||||||||||||||
| 142 | 490 | my $ret = substr($str, 0, 0); # to propagate taintedness; | |||||||||||||||||||||
| 143 | 490 | my $in_ascii = 1; # default mode is ASCII. | |||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | 9 | ▶ | no warnings 'utf8'; # $str may be malformed UTF8 at the end of a chunk. | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 146 | |||||||||||||||||||||||
| 147 | 490 | ▶ | while ( length $str ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 148 | 31438 | ▶ | if ( $str =~ s/^([[:ascii:]]+)// ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 149 | 1482 | my $tmp = $1; | |||||||||||||||||||||
| 150 | 1482 | $tmp =~ s/~/~~/g; # escapes tildes | |||||||||||||||||||||
| 151 | 1482 | ▶ | if ( !$in_ascii ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 152 | 994 | $ret .= "\x7E\x7D"; # '~}' | |||||||||||||||||||||
| 153 | 994 | $in_ascii = 1; | |||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||
| 155 | 1482 | $ret .= pack 'a*', $tmp; # remove UTF8 flag. | |||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||
| 157 | elsif ( $str =~ s/(.)// ) { | ||||||||||||||||||||||
| 158 | 29956 | my $s = $1; | |||||||||||||||||||||
| 159 | 29956 | ▶ | my $tmp = $GB->encode( $s, $chk || 0 ); | ||||||||||||||||||||
|
Condition100%2/2
$chk || 0
MC/DC100%1/1
$chk || 0
$chk
Truth table100%2/2
A
A= $chk
| |||||||||||||||||||||||
| 160 | 29956 | ▶ | last if !defined $tmp; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 161 | 29956 | ▶ | if ( length $tmp == 2 ) { # maybe a valid GB char (XXX) | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 162 | 29954 | ▶ | if ($in_ascii) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 163 | 996 | $ret .= "\x7E\x7B"; # '~{' | |||||||||||||||||||||
| 164 | 996 | $in_ascii = 0; | |||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||
| 166 | 29954 | $ret .= $tmp; | |||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||
| 168 | elsif ( length $tmp ) { # maybe FALLBACK in ASCII (XXX) | ||||||||||||||||||||||
| 169 | 2 | ▶ | if ( !$in_ascii ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 170 | 0 | $ret .= "\x7E\x7D"; # '~}' | |||||||||||||||||||||
| 171 | 0 | $in_ascii = 1; | |||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||
| 173 | 2 | $ret .= $tmp; | |||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||
| 176 | else { # if $str is malformed UTF8 *and* if length $str != 0. | ||||||||||||||||||||||
| 177 | 0 | last; | |||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||
| 180 | 490 | ▶ | $_[1] = $str if $chk; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 181 | |||||||||||||||||||||||
| 182 | # The state at the end of the chunk is discarded, even if in GB mode. | ||||||||||||||||||||||
| 183 | # That results in the combination of GB-OUT and GB-IN, i.e. "~}~{". | ||||||||||||||||||||||
| 184 | # Parhaps it is harmless, but further investigations may be required... | ||||||||||||||||||||||
| 185 | |||||||||||||||||||||||
| 186 | 490 | ▶ | if ( !$in_ascii ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 187 | 2 | $ret .= "\x7E\x7D"; # '~}' | |||||||||||||||||||||
| 188 | 2 | $in_ascii = 1; | |||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||
| 190 | 490 | utf8::encode($ret); # https://rt.cpan.org/Ticket/Display.html?id=35120 | |||||||||||||||||||||
| 191 | 490 | return $ret; | |||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||
| 193 | |||||||||||||||||||||||
| 194 | 9 | 1; | |||||||||||||||||||||
| 195 | __END__ | ||||||||||||||||||||||