| 1 | package Encode::Guess; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Encode qw(:fallbacks find_encoding); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | our $VERSION = '2.09'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | my $Canon = 'Guess'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use constant DEBUG => !!$ENV{PERL_ENCODE_DEBUG}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our %DEF_SUSPECTS = map { $_ => find_encoding($_) } qw(ascii utf8); | |||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | my $obj = bless { | |||||||||||||||||||||||||||||||||||||||||
| 11 | Name => $Canon, | ||||||||||||||||||||||||||||||||||||||||||
| 12 | Suspects => {%DEF_SUSPECTS}, | ||||||||||||||||||||||||||||||||||||||||||
| 13 | } => __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | Encode::define_encoding($obj, $Canon); | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use parent qw(Encode::Encoding); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub needs_lines { 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
needs_lines: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub perlio_ok { 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
perlio_ok: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | our @EXPORT = qw(guess_encoding); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | our $NoUTFAutoGuess = 0; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | our $UTF8_BOM = pack( "C3", 0xef, 0xbb, 0xbf ); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub import { # Exporter not used so we do it on our own | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | my $callpkg = caller; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | for my $item (@EXPORT) { | |||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | *{"$callpkg\::$item"} = \&{"$item"}; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | set_suspects(@_); | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 4 | ▶ | sub set_suspects { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_suspects: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | ▶ | my $self = ref($class) ? $class : $Encode::Encoding{$Canon}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 4 | $self->{Suspects} = {%DEF_SUSPECTS}; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | $self->add_suspects(@_); | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 4 | ▶ | sub add_suspects { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_suspects: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | ▶ | my $self = ref($class) ? $class : $Encode::Encoding{$Canon}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 4 | for my $c (@_) { | |||||||||||||||||||||||||||||||||||||||||
| 44 | 7 | ▶ | my $e = find_encoding($c) or die "Unknown encoding: $c"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 7 | $self->{Suspects}{ $e->name } = $e; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 7 | DEBUG and warn "Added: ", $e->name; | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | ▶ | sub decode($$;$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decode: called CC=4 SCAR=15.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | my ( $obj, $octet, $chk ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | my $guessed = guess( $obj, $octet ); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | unless ( ref($guessed) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | Carp::croak($guessed); | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | my $utf8 = $guessed->decode( $octet, $chk || 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$chk || 0
MC/DC0%0/1
$chk || 0
$chk
Truth table50%1/2
A
A= $chk
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | $_[1] = $octet if $chk; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | return $utf8; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 27 | ▶ | sub guess_encoding { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
guess_encoding: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 27 | guess( $Encode::Encoding{$Canon}, @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 29 | ▶ | sub guess { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
guess: called CC=36 SCAR=36.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 29 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 29 | ▶ | my $obj = ref($class) ? $class : $Encode::Encoding{$Canon}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 29 | my $octet = shift; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | # sanity check | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 29 | ▶ | return "Empty string, empty guess" unless defined $octet and length $octet; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $octet and length $octet
MC/DC50%1/2
defined $octet and length $octet
defined $octet
length $octet
Truth table66%2/3
A and B
A= defined $octetB= length $octet
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # cheat 0: utf8 flag; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 28 | ▶ | if ( Encode::is_utf8($octet) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | ▶ | return find_encoding('utf8') unless $NoUTFAutoGuess; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | Encode::_utf8_off($octet); | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | # cheat 1: BOM | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | ▶ | use Encode::Unicode; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 27 | ▶ | unless ($NoUTFAutoGuess) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 27 | my $BOM = pack( 'C3', unpack( "C3", $octet ) ); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 27 | ▶ | return find_encoding('utf8') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $BOM and $BOM eq $Encode::Guess::UTF8_BOM
MC/DC0%0/2
defined $BOM and $BOM eq $Encode::Guess::UTF8_BOM
defined $BOM
$BOM eq $Encode::Guess::UTF8_BOM
Truth table33%1/3
A and B
A= defined $BOMB= $BOM eq $Encode::Guess::UTF8_BOM
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | if ( defined $BOM and $BOM eq $UTF8_BOM ); | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 27 | $BOM = unpack( 'N', $octet ); | |||||||||||||||||||||||||||||||||||||||||
| 87 | 27 | ▶ | return find_encoding('UTF-32') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $BOM and $BOM == 65279 || $BOM == 4294836224
$BOM == 65279 or $BOM == 4294836224
MC/DC33%1/3
defined $BOM and $BOM == 65279 || $BOM == 4294836224
defined $BOM
$BOM == 65279
$BOM == 4294836224
Truth table50%2/4
A and B or C
A= defined $BOMB= $BOM == 65279C= $BOM == 4294836224
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | if ( defined $BOM and ( $BOM == 0xFeFF or $BOM == 0xFFFe0000 ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 26 | $BOM = unpack( 'n', $octet ); | |||||||||||||||||||||||||||||||||||||||||
| 90 | 26 | ▶ | return find_encoding('UTF-16') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $BOM and $BOM == 65279 || $BOM == 65534
$BOM == 65279 or $BOM == 65534
MC/DC33%1/3
defined $BOM and $BOM == 65279 || $BOM == 65534
defined $BOM
$BOM == 65279
$BOM == 65534
Truth table50%2/4
A and B or C
A= defined $BOMB= $BOM == 65279C= $BOM == 65534
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | if ( defined $BOM and ( $BOM == 0xFeFF or $BOM == 0xFFFe ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 25 | ▶ | if ( $octet =~ /\x00/o ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | { # if \x00 found, we assume UTF-(16|32)(BE|LE) | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | my $utf; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | my ( $be, $le ) = ( 0, 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | ▶ | if ( $octet =~ /\x00\x00/o ) { # UTF-32(BE|LE) assumed | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 2 | $utf = "UTF-32"; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | for my $char ( unpack( 'N*', $octet ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 99 | 100 | ▶ | $char & 0x0000ffff and $be++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 100 | ▶ | $char & 0xffff0000 and $le++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | else { # UTF-16(BE|LE) assumed | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 2 | $utf = "UTF-16"; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | for my $char ( unpack( 'n*', $octet ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 106 | 100 | ▶ | $char & 0x00ff and $be++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 100 | ▶ | $char & 0xff00 and $le++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | DEBUG and warn "$utf, be == $be, le == $le"; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 4 | ▶ | $be == $le | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | and return | ||||||||||||||||||||||||||||||||||||||||||
| 113 | "Encodings ambiguous between $utf BE and LE ($be, $le)"; | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | ▶ | $utf .= ( $be > $le ) ? 'BE' : 'LE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 4 | return find_encoding($utf); | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 21 | my %try = %{ $obj->{Suspects} }; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 21 | for my $c (@_) { | |||||||||||||||||||||||||||||||||||||||||
| 120 | 7 | ▶ | my $e = find_encoding($c) or die "Unknown encoding: $c"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 7 | $try{ $e->name } = $e; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 7 | DEBUG and warn "Added: ", $e->name; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 21 | my $nline = 1; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 21 | for my $line ( split /\r\n?|\n/, $octet ) { | |||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | # cheat 2 -- \e in the string | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 69 | ▶ | if ( $line =~ /\e/o ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | my @keys = keys %try; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | delete @try{qw/utf8 ascii/}; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 2 | for my $k (@keys) { | |||||||||||||||||||||||||||||||||||||||||
| 132 | 8 | ▶ | ref( $try{$k} ) eq 'Encode::XS' and delete $try{$k}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 69 | my %ok = %try; | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | # warn join(",", keys %try); | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 69 | for my $k ( keys %try ) { | |||||||||||||||||||||||||||||||||||||||||
| 139 | 211 | my $scratch = $line; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 211 | $try{$k}->decode( $scratch, FB_QUIET ); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 211 | ▶ | if ( $scratch eq '' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 154 | DEBUG and warn sprintf( "%4d:%-24s ok\n", $nline, $k ); | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | ▶ | use bytes (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 57 | DEBUG | |||||||||||||||||||||||||||||||||||||||||
| 147 | and warn sprintf( "%4d:%-24s not ok; %d bytes left\n", | ||||||||||||||||||||||||||||||||||||||||||
| 148 | $nline, $k, bytes::length($scratch) ); | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 57 | delete $ok{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 69 | ▶ | %ok or return "No appropriate encodings found!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 66 | ▶ | if ( scalar( keys(%ok) ) == 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 14 | my ($retval) = values(%ok); | |||||||||||||||||||||||||||||||||||||||||
| 155 | 14 | return $retval; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 52 | %try = %ok; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 52 | $nline++; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | $try{ascii} | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 4 | ▶ | or return "Encodings too ambiguous: " . join( " or ", keys %try ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 4 | return $try{ascii}; | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 166 | __END__ | ||||||||||||||||||||||||||||||||||||||||||