| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Encode::JP::JIS7; | 
| 2 | 10 |  |  | 10 |  | 74 | use strict; | 
|  | 10 |  |  |  |  | 17 |  | 
|  | 10 |  |  |  |  | 238 |  | 
| 3 | 10 |  |  | 10 |  | 43 | use warnings; | 
|  | 10 |  |  |  |  | 16 |  | 
|  | 10 |  |  |  |  | 705 |  | 
| 4 |  |  |  |  |  |  | our $VERSION = do { my @r = ( q$Revision: 2.8 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r }; | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 10 |  |  | 10 |  | 57 | use Encode qw(:fallbacks); | 
|  | 10 |  |  |  |  | 17 |  | 
|  | 10 |  |  |  |  | 1859 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | for my $name ( '7bit-jis', 'iso-2022-jp', 'iso-2022-jp-1' ) { | 
| 9 |  |  |  |  |  |  | my $h2z     = ( $name eq '7bit-jis' )    ? 0 : 1; | 
| 10 |  |  |  |  |  |  | my $jis0212 = ( $name eq 'iso-2022-jp' ) ? 0 : 1; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | my $obj = bless { | 
| 13 |  |  |  |  |  |  | Name    => $name, | 
| 14 |  |  |  |  |  |  | h2z     => $h2z, | 
| 15 |  |  |  |  |  |  | jis0212 => $jis0212, | 
| 16 |  |  |  |  |  |  | } => __PACKAGE__; | 
| 17 |  |  |  |  |  |  | Encode::define_encoding($obj, $name); | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 10 |  |  | 10 |  | 71 | use parent qw(Encode::Encoding); | 
|  | 10 |  |  |  |  | 21 |  | 
|  | 10 |  |  |  |  | 76 |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | # we override this to 1 so PerlIO works | 
| 23 | 12 |  |  | 12 | 1 | 135 | sub needs_lines { 1 } | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 10 |  |  | 10 |  | 3157 | use Encode::CJKConstants qw(:all); | 
|  | 10 |  |  |  |  | 22 |  | 
|  | 10 |  |  |  |  | 4028 |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | # | 
| 28 |  |  |  |  |  |  | # decode is identical for all 2022 variants | 
| 29 |  |  |  |  |  |  | # | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | sub decode($$;$) { | 
| 32 | 47 |  |  | 47 | 1 | 1210 | my ( $obj, $str, $chk ) = @_; | 
| 33 | 47 | 100 |  |  |  | 133 | return undef unless defined $str; | 
| 34 | 44 |  |  |  |  | 75 | my $residue = ''; | 
| 35 | 44 | 100 |  |  |  | 98 | if ($chk) { | 
| 36 | 26 | 100 |  |  |  | 1090 | $str =~ s/([^\x00-\x7f].*)$//so and $residue = $1; | 
| 37 |  |  |  |  |  |  | } | 
| 38 | 44 |  |  |  |  | 125 | $residue .= jis_euc( \$str ); | 
| 39 | 44 | 100 |  |  |  | 174 | $_[1] = $residue if $chk; | 
| 40 | 44 |  |  |  |  | 159 | return Encode::decode( 'euc-jp', $str, FB_PERLQQ ); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | # | 
| 44 |  |  |  |  |  |  | # encode is different | 
| 45 |  |  |  |  |  |  | # | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub encode($$;$) { | 
| 48 | 1397 |  |  | 1397 | 1 | 11107 | require Encode::JP::H2Z; | 
| 49 | 1397 |  |  |  |  | 2698 | my ( $obj, $utf8, $chk ) = @_; | 
| 50 | 1397 | 100 |  |  |  | 2408 | return undef unless defined $utf8; | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | # empty the input string in the stack so perlio is ok | 
| 53 | 1394 | 100 |  |  |  | 2310 | $_[1] = '' if $chk; | 
| 54 | 1394 |  |  |  |  | 2316 | my ( $h2z, $jis0212 ) = @$obj{qw(h2z jis0212)}; | 
| 55 | 1394 |  | 100 |  |  | 3239 | my $octet = Encode::encode( 'euc-jp', $utf8, $chk || 0 ); | 
| 56 | 1394 | 100 |  |  |  | 3182 | $h2z and &Encode::JP::H2Z::h2z( \$octet ); | 
| 57 | 1394 |  |  |  |  | 2613 | euc_jis( \$octet, $jis0212 ); | 
| 58 | 1394 |  |  |  |  | 13102 | return $octet; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | # | 
| 62 |  |  |  |  |  |  | # cat_decode | 
| 63 |  |  |  |  |  |  | # | 
| 64 |  |  |  |  |  |  | my $re_scan_jis_g = qr{ | 
| 65 |  |  |  |  |  |  | \G ( ($RE{JIS_0212}) |  $RE{JIS_0208}  | | 
| 66 |  |  |  |  |  |  | ($RE{ISO_ASC})  | ($RE{JIS_KANA}) | ) | 
| 67 |  |  |  |  |  |  | ([^\e]*) | 
| 68 |  |  |  |  |  |  | }x; | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | sub cat_decode {    # ($obj, $dst, $src, $pos, $trm, $chk) | 
| 71 | 0 |  |  | 0 | 1 | 0 | my ( $obj, undef, undef, $pos, $trm ) = @_;    # currently ignores $chk | 
| 72 | 0 |  |  |  |  | 0 | my ( $rdst, $rsrc, $rpos ) = \@_[ 1, 2, 3 ]; | 
| 73 | 0 |  |  |  |  | 0 | local ${^ENCODING}; | 
| 74 | 10 |  |  | 10 |  | 71 | use bytes; | 
|  | 10 |  |  |  |  | 18 |  | 
|  | 10 |  |  |  |  | 62 |  | 
| 75 | 0 |  |  |  |  | 0 | my $opos = pos($$rsrc); | 
| 76 | 0 |  |  |  |  | 0 | pos($$rsrc) = $pos; | 
| 77 | 0 |  |  |  |  | 0 | while ( $$rsrc =~ /$re_scan_jis_g/gc ) { | 
| 78 | 0 |  |  |  |  | 0 | my ( $esc, $esc_0212, $esc_asc, $esc_kana, $chunk ) = | 
| 79 |  |  |  |  |  |  | ( $1, $2, $3, $4, $5 ); | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 | 0 |  |  |  | 0 | unless ($chunk) { $esc or last; next; } | 
|  | 0 | 0 |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 0 | 0 | 0 |  |  | 0 | if ( $esc && !$esc_asc ) { | 
|  |  | 0 |  |  |  |  |  | 
| 84 | 0 |  |  |  |  | 0 | $chunk =~ tr/\x21-\x7e/\xa1-\xfe/; | 
| 85 | 0 | 0 |  |  |  | 0 | if ($esc_kana) { | 
|  |  | 0 |  |  |  |  |  | 
| 86 | 0 |  |  |  |  | 0 | $chunk =~ s/([\xa1-\xdf])/\x8e$1/og; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  | elsif ($esc_0212) { | 
| 89 | 0 |  |  |  |  | 0 | $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og; | 
| 90 |  |  |  |  |  |  | } | 
| 91 | 0 |  |  |  |  | 0 | $chunk = Encode::decode( 'euc-jp', $chunk, 0 ); | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  | elsif ( ( my $npos = index( $chunk, $trm ) ) >= 0 ) { | 
| 94 | 0 |  |  |  |  | 0 | $$rdst .= substr( $chunk, 0, $npos + length($trm) ); | 
| 95 | 0 |  |  |  |  | 0 | $$rpos += length($esc) + $npos + length($trm); | 
| 96 | 0 |  |  |  |  | 0 | pos($$rsrc) = $opos; | 
| 97 | 0 |  |  |  |  | 0 | return 1; | 
| 98 |  |  |  |  |  |  | } | 
| 99 | 0 |  |  |  |  | 0 | $$rdst .= $chunk; | 
| 100 | 0 |  |  |  |  | 0 | $$rpos = pos($$rsrc); | 
| 101 |  |  |  |  |  |  | } | 
| 102 | 0 |  |  |  |  | 0 | $$rpos = pos($$rsrc); | 
| 103 | 0 |  |  |  |  | 0 | pos($$rsrc) = $opos; | 
| 104 | 0 |  |  |  |  | 0 | return ''; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | # JIS<->EUC | 
| 108 |  |  |  |  |  |  | my $re_scan_jis = qr{ | 
| 109 |  |  |  |  |  |  | (?:($RE{JIS_0212})|$RE{JIS_0208}|($RE{ISO_ASC})|($RE{JIS_KANA}))([^\e]*) | 
| 110 |  |  |  |  |  |  | }x; | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | sub jis_euc { | 
| 113 | 44 |  |  | 44 | 0 | 147 | local ${^ENCODING}; | 
| 114 | 44 |  |  |  |  | 78 | my $r_str = shift; | 
| 115 | 44 |  |  |  |  | 357 | $$r_str =~ s($re_scan_jis) | 
| 116 | 5060 |  |  |  |  | 11804 | { | 
| 117 |  |  |  |  |  |  | my ($esc_0212, $esc_asc, $esc_kana, $chunk) = | 
| 118 | 5060 | 100 |  |  |  | 8030 | ($1, $2, $3, $4); | 
| 119 | 2530 |  |  |  |  | 3992 | if (!$esc_asc) { | 
| 120 | 2530 | 100 |  |  |  | 4658 | $chunk =~ tr/\x21-\x7e/\xa1-\xfe/; | 
|  |  | 100 |  |  |  |  |  | 
| 121 | 2 |  |  |  |  | 69 | if ($esc_kana) { | 
| 122 |  |  |  |  |  |  | $chunk =~ s/([\xa1-\xdf])/\x8e$1/og; | 
| 123 |  |  |  |  |  |  | } | 
| 124 | 426 |  |  |  |  | 10194 | elsif ($esc_0212) { | 
| 125 |  |  |  |  |  |  | $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og; | 
| 126 |  |  |  |  |  |  | } | 
| 127 | 5060 |  |  |  |  | 15710 | } | 
| 128 |  |  |  |  |  |  | $chunk; | 
| 129 | 44 |  |  |  |  | 416 | }geox; | 
| 130 | 44 |  |  |  |  | 152 | my ($residue) = ( $$r_str =~ s/(\e.*)$//so ); | 
| 131 |  |  |  |  |  |  | return $residue; | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 10 |  |  | 10 |  | 4446 | sub euc_jis { | 
|  | 10 |  |  |  |  | 24 |  | 
|  | 10 |  |  |  |  | 2451 |  | 
| 135 | 1394 |  |  | 1394 | 0 | 3241 | no warnings qw(uninitialized); | 
| 136 | 1394 |  |  |  |  | 1708 | local ${^ENCODING}; | 
| 137 | 1394 |  |  |  |  | 1664 | my $r_str   = shift; | 
| 138 | 1394 |  |  |  |  | 9440 | my $jis0212 = shift; | 
| 139 |  |  |  |  |  |  | $$r_str =~ s{ | 
| 140 |  |  |  |  |  |  | ((?:$RE{EUC_C})+|(?:$RE{EUC_KANA})+|(?:$RE{EUC_0212})+) | 
| 141 | 3467 |  |  |  |  | 6066 | }{ | 
| 142 |  |  |  |  |  |  | my $chunk = $1; | 
| 143 |  |  |  |  |  |  | my $esc = | 
| 144 |  |  |  |  |  |  | ( $chunk =~ tr/\x8E//d ) ? $ESC{KANA} : | 
| 145 | 3467 | 100 |  |  |  | 8229 | ( $chunk =~ tr/\x8F//d ) ? $ESC{JIS_0212} : | 
|  |  | 100 |  |  |  |  |  | 
| 146 | 3467 | 50 | 66 |  |  | 7063 | $ESC{JIS_0208}; | 
| 147 |  |  |  |  |  |  | if ($esc eq $ESC{JIS_0212} && !$jis0212){ | 
| 148 | 0 |  |  |  |  | 0 | # fallback to '?' | 
| 149 |  |  |  |  |  |  | $chunk =~ tr/\xA1-\xFE/\x3F/; | 
| 150 | 3467 |  |  |  |  | 4358 | }else{ | 
| 151 |  |  |  |  |  |  | $chunk =~ tr/\xA1-\xFE/\x21-\x7E/; | 
| 152 | 3467 |  |  |  |  | 16478 | } | 
| 153 |  |  |  |  |  |  | $esc . $chunk . $ESC{ASC}; | 
| 154 | 1394 |  |  |  |  | 2787 | }geox; | 
| 155 |  |  |  |  |  |  | $$r_str =~ s/\Q$ESC{ASC}\E | 
| 156 | 1394 |  |  |  |  | 2362 | (\Q$ESC{KANA}\E|\Q$ESC{JIS_0212}\E|\Q$ESC{JIS_0208}\E)/$1/gox; | 
| 157 |  |  |  |  |  |  | $$r_str; | 
| 158 |  |  |  |  |  |  | } | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | 1; | 
| 161 |  |  |  |  |  |  | __END__ |