| 3 |
|
|
| 4 |
## NOTE: |Message::Charset::Info| uses this module without calling |
## NOTE: |Message::Charset::Info| uses this module without calling |
| 5 |
## the constructor. |
## the constructor. |
| 6 |
|
use Message::Charset::Info; |
| 7 |
|
|
| 8 |
my $XML_AUTO_CHARSET = q<http://suika.fam.cx/www/2006/03/xml-entity/>; |
my $XML_AUTO_CHARSET = q<http://suika.fam.cx/www/2006/03/xml-entity/>; |
| 9 |
my $IANA_CHARSET = q<urn:x-suika-fam-cx:charset:>; |
my $IANA_CHARSET = q<urn:x-suika-fam-cx:charset:>; |
| 14 |
sub create_decode_handle ($$$;$) { |
sub create_decode_handle ($$$;$) { |
| 15 |
my $csdef = $Whatpm::Charset::CharsetDef->{$_[1]}; |
my $csdef = $Whatpm::Charset::CharsetDef->{$_[1]}; |
| 16 |
my $obj = { |
my $obj = { |
| 17 |
|
category => 0, |
| 18 |
char_buffer => \(my $s = ''), |
char_buffer => \(my $s = ''), |
| 19 |
|
char_buffer_pos => 0, |
| 20 |
character_queue => [], |
character_queue => [], |
| 21 |
filehandle => $_[2], |
filehandle => $_[2], |
| 22 |
charset => $_[1], |
charset => $_[1], |
| 441 |
$obj->{perl_encoding_name} = $csdef->{perl_name}->[0]; |
$obj->{perl_encoding_name} = $csdef->{perl_name}->[0]; |
| 442 |
require Encode::EUCJP1997; |
require Encode::EUCJP1997; |
| 443 |
if (Encode::find_encoding ($obj->{perl_encoding_name})) { |
if (Encode::find_encoding ($obj->{perl_encoding_name})) { |
| 444 |
return bless $obj, 'Whatpm::Charset::DecodeHandle::EUCJP'; |
$obj->{category} |= Message::Charset::Info::CHARSET_CATEGORY_EUCJP; |
| 445 |
|
return bless $obj, 'Whatpm::Charset::DecodeHandle::Encode'; |
| 446 |
} |
} |
| 447 |
} elsif ($csdef->{uri}->{$XML_CHARSET.'shift_jis'} or |
} elsif ($csdef->{uri}->{$XML_CHARSET.'shift_jis'} or |
| 448 |
$csdef->{uri}->{$IANA_CHARSET.'shift_jis'}) { |
$csdef->{uri}->{$IANA_CHARSET.'shift_jis'}) { |
| 449 |
$obj->{perl_encoding_name} = $csdef->{perl_name}->[0]; |
$obj->{perl_encoding_name} = $csdef->{perl_name}->[0]; |
| 450 |
require Encode::ShiftJIS1997; |
require Encode::ShiftJIS1997; |
| 451 |
if (Encode::find_encoding ($obj->{perl_encoding_name})) { |
if (Encode::find_encoding ($obj->{perl_encoding_name})) { |
| 452 |
return bless $obj, 'Whatpm::Charset::DecodeHandle::ShiftJIS'; |
return bless $obj, 'Whatpm::Charset::DecodeHandle::Encode'; |
| 453 |
} |
} |
| 454 |
} elsif ($csdef->{is_block_safe}) { |
} elsif ($csdef->{is_block_safe}) { |
| 455 |
$obj->{perl_encoding_name} = $csdef->{perl_name}->[0]; |
$obj->{perl_encoding_name} = $csdef->{perl_name}->[0]; |
| 560 |
|
|
| 561 |
sub close { $_[0]->{filehandle}->close } |
sub close { $_[0]->{filehandle}->close } |
| 562 |
|
|
| 563 |
|
package Whatpm::Charset::DecodeHandle::CharString; |
| 564 |
|
|
| 565 |
|
## NOTE: Same as Perl's standard |open $handle, '<', \$char_string|, |
| 566 |
|
## but supports |ungetc| and other extensions. |
| 567 |
|
|
| 568 |
|
sub new ($$) { |
| 569 |
|
my $self = bless {pos => 0}, shift; |
| 570 |
|
$self->{string} = shift; # must be a scalar ref |
| 571 |
|
return $self; |
| 572 |
|
} # new |
| 573 |
|
|
| 574 |
|
sub getc ($) { |
| 575 |
|
my $self = shift; |
| 576 |
|
if ($self->{pos} < length ${$self->{string}}) { |
| 577 |
|
return substr ${$self->{string}}, $self->{pos}++, 1; |
| 578 |
|
} else { |
| 579 |
|
return undef; |
| 580 |
|
} |
| 581 |
|
} # getc |
| 582 |
|
|
| 583 |
|
sub read ($$$$) { |
| 584 |
|
#my ($self, $scalar, $length, $offset) = @_; |
| 585 |
|
my $self = $_[0]; |
| 586 |
|
my $length = $_[2] || 0; |
| 587 |
|
my $offset = $_[3]; |
| 588 |
|
## NOTE: We don't support standard Perl semantics if $offset is |
| 589 |
|
## greater than the length of $scalar. |
| 590 |
|
substr ($_[1], $offset) = substr (${$self->{string}}, $self->{pos}, $length); |
| 591 |
|
my $count = (length $_[1]) - $offset; |
| 592 |
|
$self->{pos} += $count; |
| 593 |
|
return $count; |
| 594 |
|
} # read |
| 595 |
|
|
| 596 |
|
sub manakai_read_until ($$$;$) { |
| 597 |
|
#my ($self, $scalar, $pattern, $offset) = @_; |
| 598 |
|
my $self = $_[0]; |
| 599 |
|
pos (${$self->{string}}) = $self->{pos}; |
| 600 |
|
if (${$self->{string}} =~ /\G(?>$_[2])+/) { |
| 601 |
|
substr ($_[1], $_[3]) = substr (${$self->{string}}, $-[0], $+[0] - $-[0]); |
| 602 |
|
$self->{pos} += $+[0] - $-[0]; |
| 603 |
|
return $+[0] - $-[0]; |
| 604 |
|
} else { |
| 605 |
|
return 0; |
| 606 |
|
} |
| 607 |
|
} # manakai_read_until |
| 608 |
|
|
| 609 |
|
sub ungetc ($$) { |
| 610 |
|
my $self = shift; |
| 611 |
|
## Ignore second parameter. |
| 612 |
|
$self->{pos}-- if $self->{pos} > 0; |
| 613 |
|
} # ungetc |
| 614 |
|
|
| 615 |
|
sub close ($) { } |
| 616 |
|
|
| 617 |
package Whatpm::Charset::DecodeHandle::Encode; |
package Whatpm::Charset::DecodeHandle::Encode; |
| 618 |
|
|
| 619 |
## NOTE: Provides a Perl |Encode| module wrapper object. |
## NOTE: Provides a Perl |Encode| module wrapper object. |
| 634 |
|
|
| 635 |
sub read ($$$;$) { |
sub read ($$$;$) { |
| 636 |
my $self = $_[0]; |
my $self = $_[0]; |
| 637 |
# $scalar = $_[1]; |
#my $scalar = $_[1]; |
| 638 |
my $length = $_[2]; |
my $length = $_[2]; |
| 639 |
my $offset = $_[3] || 0; |
my $offset = $_[3] || 0; |
| 640 |
my $count = 0; |
my $count = 0; |
| 641 |
my $eof; |
my $eof; |
| 642 |
|
## NOTE: It is incompatible with the standard Perl semantics |
| 643 |
|
## if $offset is greater than the length of $scalar. |
| 644 |
|
|
| 645 |
A: { |
A: { |
| 646 |
return $count if $length < 1; |
return $count if $length < 1; |
| 647 |
|
|
| 648 |
if (my $l = length ${$self->{char_buffer}}) { |
if (my $l = (length ${$self->{char_buffer}}) - $self->{char_buffer_pos}) { |
| 649 |
if ($l >= $length) { |
if ($l >= $length) { |
| 650 |
substr ($_[1], $offset) = substr (${$self->{char_buffer}}, 0, $length); |
substr ($_[1], $offset) |
| 651 |
|
= substr (${$self->{char_buffer}}, $self->{char_buffer_pos}, |
| 652 |
|
$length); |
| 653 |
$count += $length; |
$count += $length; |
| 654 |
substr (${$self->{char_buffer}}, 0, $length) = ''; |
$self->{char_buffer_pos} += $length; |
| 655 |
$length = 0; |
$length = 0; |
| 656 |
return $count; |
return $count; |
| 657 |
} else { |
} else { |
| 658 |
substr ($_[1], $offset) = ${$self->{char_buffer}}; |
substr ($_[1], $offset) |
| 659 |
|
= substr (${$self->{char_buffer}}, $self->{char_buffer_pos}); |
| 660 |
$count += $l; |
$count += $l; |
| 661 |
$length -= $l; |
$length -= $l; |
| 662 |
${$self->{char_buffer}} = ''; |
${$self->{char_buffer}} = ''; |
| 663 |
|
$self->{char_buffer_pos} = 0; |
| 664 |
} |
} |
| 665 |
$offset = length $_[1]; |
$offset = length $_[1]; |
| 666 |
} |
} |
| 668 |
if ($eof) { |
if ($eof) { |
| 669 |
return $count; |
return $count; |
| 670 |
} |
} |
|
|
|
|
my $error; |
|
|
if ($self->{continue}) { |
|
|
if ($self->{filehandle}->read ($self->{byte_buffer}, 256, |
|
|
length $self->{byte_buffer})) { |
|
|
# |
|
|
} else { |
|
|
$error = 1; |
|
|
} |
|
|
$self->{continue} = 0; |
|
|
} elsif (512 > length $self->{byte_buffer}) { |
|
|
if ($self->{filehandle}->read ($self->{byte_buffer}, 256, |
|
|
length $self->{byte_buffer})) { |
|
|
# |
|
|
} else { |
|
|
$eof = 1; |
|
|
} |
|
|
} |
|
| 671 |
|
|
| 672 |
unless ($error) { |
my $error; |
| 673 |
if (not $self->{bom_checked}) { |
if ($self->{continue}) { |
| 674 |
if (defined $self->{bom_pattern}) { |
if ($self->{filehandle}->read ($self->{byte_buffer}, 256, |
| 675 |
if ($self->{byte_buffer} =~ s/^$self->{bom_pattern}//) { |
length $self->{byte_buffer})) { |
| 676 |
$self->{has_bom} = 1; |
# |
| 677 |
} |
} else { |
| 678 |
|
$error = 1; |
| 679 |
|
} |
| 680 |
|
$self->{continue} = 0; |
| 681 |
|
} elsif (512 > length $self->{byte_buffer}) { |
| 682 |
|
if ($self->{filehandle}->read ($self->{byte_buffer}, 256, |
| 683 |
|
length $self->{byte_buffer})) { |
| 684 |
|
# |
| 685 |
|
} else { |
| 686 |
|
$eof = 1; |
| 687 |
} |
} |
|
$self->{bom_checked} = 1; |
|
| 688 |
} |
} |
| 689 |
|
|
| 690 |
my $string = Encode::decode ($self->{perl_encoding_name}, |
unless ($error) { |
| 691 |
$self->{byte_buffer}, |
if (not $self->{bom_checked}) { |
| 692 |
Encode::FB_QUIET ()); |
if (defined $self->{bom_pattern}) { |
| 693 |
if (length $string) { |
if ($self->{byte_buffer} =~ s/^$self->{bom_pattern}//) { |
| 694 |
$self->{char_buffer} = \$string; |
$self->{has_bom} = 1; |
| 695 |
if (length $self->{byte_buffer}) { |
} |
| 696 |
$self->{continue} = 1; |
} |
| 697 |
|
$self->{bom_checked} = 1; |
| 698 |
} |
} |
| 699 |
} else { |
|
| 700 |
if (length $self->{byte_buffer}) { |
my $string = Encode::decode ($self->{perl_encoding_name}, |
| 701 |
$error = 1; |
$self->{byte_buffer}, |
| 702 |
|
Encode::FB_QUIET ()); |
| 703 |
|
if (length $string) { |
| 704 |
|
$self->{char_buffer} = \$string; |
| 705 |
|
$self->{char_buffer_pos} = 0; |
| 706 |
|
if (length $self->{byte_buffer}) { |
| 707 |
|
$self->{continue} = 1; |
| 708 |
|
} |
| 709 |
} else { |
} else { |
| 710 |
## NOTE: No further input |
if (length $self->{byte_buffer}) { |
| 711 |
redo A; |
$error = 1; |
| 712 |
|
} else { |
| 713 |
|
## NOTE: No further input. |
| 714 |
|
redo A; |
| 715 |
|
} |
| 716 |
} |
} |
| 717 |
} |
} |
|
} |
|
| 718 |
|
|
| 719 |
if ($error) { |
if ($error) { |
| 720 |
my $r = substr $self->{byte_buffer}, 0, 1, ''; |
my $r = substr $self->{byte_buffer}, 0, 1, ''; |
| 721 |
my $fallback = $self->{fallback}->{$r}; |
my $fallback; |
| 722 |
if (defined $fallback) { |
my $etype = 'illegal-octets-error'; |
| 723 |
## NOTE: This is an HTML5 parse error. |
my %earg; |
| 724 |
$self->{onerror}->($self, 'fallback-char-error', octets => \$r, |
if ($self->{category} |
| 725 |
char => \$fallback, |
& Message::Charset::Info::CHARSET_CATEGORY_SJIS) { |
| 726 |
level => $self->{level}->{$self->{error_level}->{'fallback-char-error'}}); |
if ($r =~ /^[\x81-\x9F\xE0-\xFC]/) { |
| 727 |
${$self->{char_buffer}} .= $fallback; |
if ($self->{byte_buffer} =~ s/(.)//s) { |
| 728 |
} elsif (exists $self->{fallback}->{$r}) { |
$r .= $1; # not limited to \x40-\xFC - \x7F |
| 729 |
## NOTE: This is an HTML5 parse error. In addition, the octet |
$etype = 'unassigned-code-point-error'; |
| 730 |
## is not assigned with a character. |
} |
| 731 |
$self->{onerror}->($self, 'fallback-unassigned-error', octets => \$r, |
## NOTE: Range [\xF0-\xFC] is unassigned and may be used as a |
| 732 |
level => $self->{level}->{$self->{error_level}->{'fallback-unassigned-error'}}); |
## single-byte character or as the first-byte of a double-byte |
| 733 |
${$self->{char_buffer}} .= $r; |
## character, according to JIS X 0208:1997 Appendix 1. However, the |
| 734 |
} else { |
## current practice is using the range as first-bytes of double-byte |
| 735 |
$self->{onerror}->($self, 'illegal-octets-error', octets => \$r, |
## characters. |
| 736 |
level => $self->{level}->{$self->{error_level}->{'illegal-octets-error'}}); |
} elsif ($r =~ /^[\x80\xA0\xFD-\xFF]/) { |
| 737 |
${$self->{char_buffer}} .= $r; |
$etype = 'unassigned-code-point-error'; |
| 738 |
|
} |
| 739 |
|
} elsif ($self->{category} |
| 740 |
|
& Message::Charset::Info::CHARSET_CATEGORY_EUCJP) { |
| 741 |
|
if ($r =~ /^[\xA1-\xFE]/) { |
| 742 |
|
if ($self->{byte_buffer} =~ s/^([\xA1-\xFE])//) { |
| 743 |
|
$r .= $1; |
| 744 |
|
$etype = 'unassigned-code-point-error'; |
| 745 |
|
} |
| 746 |
|
} elsif ($r eq "\x8F") { |
| 747 |
|
if ($self->{byte_buffer} =~ s/^([\xA1-\xFE][\xA1-\xFE]?)//) { |
| 748 |
|
$r .= $1; |
| 749 |
|
$etype = 'unassigned-code-point-error' if length $1 == 2; |
| 750 |
|
} |
| 751 |
|
} elsif ($r eq "\x8E") { |
| 752 |
|
if ($self->{byte_buffer} =~ s/^([\xA1-\xFE])//) { |
| 753 |
|
$r .= $1; |
| 754 |
|
$etype = 'unassigned-code-point-error'; |
| 755 |
|
} |
| 756 |
|
} elsif ($r eq "\xA0" or $r eq "\xFF") { |
| 757 |
|
$etype = 'unassigned-code-point-error'; |
| 758 |
|
} |
| 759 |
|
} else { |
| 760 |
|
$fallback = $self->{fallback}->{$r}; |
| 761 |
|
if (defined $fallback) { |
| 762 |
|
## NOTE: This is an HTML5 parse error. |
| 763 |
|
$etype = 'fallback-char-error'; |
| 764 |
|
$earg{char} = \$fallback; |
| 765 |
|
} elsif (exists $self->{fallback}->{$r}) { |
| 766 |
|
## NOTE: This is an HTML5 parse error. In addition, the octet |
| 767 |
|
## is not assigned with a character. |
| 768 |
|
$etype = 'fallback-unassigned-error'; |
| 769 |
|
} |
| 770 |
|
} |
| 771 |
|
|
| 772 |
|
## NOTE: Fixup line/column number by counting the number of |
| 773 |
|
## lines/columns in the string that is to be retuend by this |
| 774 |
|
## method call. |
| 775 |
|
my $line_diff = 0; |
| 776 |
|
my $col_diff = 0; |
| 777 |
|
my $set_col; |
| 778 |
|
for (my $i = 0; $i < $count; $i++) { |
| 779 |
|
my $s = substr $_[1], $i - $count, 1; |
| 780 |
|
if ($s eq "\x0D") { |
| 781 |
|
$line_diff++; |
| 782 |
|
$col_diff = 0; |
| 783 |
|
$set_col = 1; |
| 784 |
|
$i++ if substr ($_[1], $i - $count + 1, 1) eq "\x0A"; |
| 785 |
|
} elsif ($s eq "\x0A") { |
| 786 |
|
$line_diff++; |
| 787 |
|
$col_diff = 0; |
| 788 |
|
$set_col = 1; |
| 789 |
|
} else { |
| 790 |
|
$col_diff++; |
| 791 |
|
} |
| 792 |
|
} |
| 793 |
|
my $i = $self->{char_buffer_pos}; |
| 794 |
|
if ($count and substr (${$self->{char_buffer}}, -1, 1) eq "\x0D") { |
| 795 |
|
if (substr (${$self->{char_buffer}}, $i, 1) eq "\x0A") { |
| 796 |
|
$i++; |
| 797 |
|
} |
| 798 |
|
} |
| 799 |
|
my $cb_length = length ${$self->{char_buffer}}; |
| 800 |
|
for (; $i < $cb_length; $i++) { |
| 801 |
|
my $s = substr $_[1], $i, 1; |
| 802 |
|
if ($s eq "\x0D") { |
| 803 |
|
$line_diff++; |
| 804 |
|
$col_diff = 0; |
| 805 |
|
$set_col = 1; |
| 806 |
|
$i++ if substr ($_[1], $i + 1, 1) eq "\x0A"; |
| 807 |
|
} elsif ($s eq "\x0A") { |
| 808 |
|
$line_diff++; |
| 809 |
|
$col_diff = 0; |
| 810 |
|
$set_col = 1; |
| 811 |
|
} else { |
| 812 |
|
$col_diff++; |
| 813 |
|
} |
| 814 |
|
} |
| 815 |
|
$self->{onerror}->($self, $etype, octets => \$r, %earg, |
| 816 |
|
level => $self->{level}->{$self->{error_level}->{$etype}}, |
| 817 |
|
line_diff => $line_diff, |
| 818 |
|
($set_col ? (column => 1) : ()), |
| 819 |
|
column_diff => $col_diff); |
| 820 |
|
## NOTE: Error handler may modify |octets| parameter, which |
| 821 |
|
## would be returned as part of the output. Note that what |
| 822 |
|
## is returned would affect what |manakai_read_until| returns. |
| 823 |
|
${$self->{char_buffer}} .= defined $fallback ? $fallback : $r; |
| 824 |
} |
} |
|
} |
|
| 825 |
|
|
| 826 |
redo A; |
redo A; |
| 827 |
} # A |
} # A |
| 828 |
} # getc |
} # read |
| 829 |
|
|
| 830 |
|
sub manakai_read_until ($$$;$) { |
| 831 |
|
#my ($self, $scalar, $pattern, $offset) = @_; |
| 832 |
|
my $self = $_[0]; |
| 833 |
|
my $s = ''; |
| 834 |
|
$self->read ($s, 255); |
| 835 |
|
if ($s =~ /^(?>$_[2])+/) { |
| 836 |
|
my $rem_length = (length $s) - $+[0]; |
| 837 |
|
if ($rem_length) { |
| 838 |
|
if ($self->{char_buffer_pos} > $rem_length) { |
| 839 |
|
$self->{char_buffer_pos} -= $rem_length; |
| 840 |
|
} else { |
| 841 |
|
substr (${$self->{char_buffer}}, 0, $self->{char_buffer_pos}) |
| 842 |
|
= substr ($s, $+[0]); |
| 843 |
|
$self->{char_buffer_pos} = 0; |
| 844 |
|
} |
| 845 |
|
} |
| 846 |
|
substr ($_[1], $_[3]) = substr ($s, $-[0], $+[0] - $-[0]); |
| 847 |
|
return $+[0]; |
| 848 |
|
} elsif (length $s) { |
| 849 |
|
if ($self->{char_buffer_pos} > length $s) { |
| 850 |
|
$self->{char_buffer_pos} -= length $s; |
| 851 |
|
} else { |
| 852 |
|
substr (${$self->{char_buffer}}, 0, $self->{char_buffer_pos}) = $s; |
| 853 |
|
$self->{char_buffer_pos} = 0; |
| 854 |
|
} |
| 855 |
|
} |
| 856 |
|
return 0; |
| 857 |
|
} # manakai_read_until |
| 858 |
|
|
| 859 |
sub has_bom ($) { $_[0]->{has_bom} } |
sub has_bom ($) { $_[0]->{has_bom} } |
| 860 |
|
|
| 882 |
unshift @{$_[0]->{character_queue}}, chr int ($_[1] or 0); |
unshift @{$_[0]->{character_queue}}, chr int ($_[1] or 0); |
| 883 |
} # ungetc |
} # ungetc |
| 884 |
|
|
|
package Whatpm::Charset::DecodeHandle::EUCJP; |
|
|
push our @ISA, 'Whatpm::Charset::DecodeHandle::Encode'; |
|
|
|
|
|
sub getc ($) { |
|
|
my $self = $_[0]; |
|
|
return shift @{$self->{character_queue}} if @{$self->{character_queue}}; |
|
|
|
|
|
my $error; |
|
|
if ($self->{continue}) { |
|
|
if ($self->{filehandle}->read ($self->{byte_buffer}, 256, |
|
|
length $self->{byte_buffer})) { |
|
|
# |
|
|
} else { |
|
|
$error = 1; |
|
|
} |
|
|
$self->{continue} = 0; |
|
|
} elsif (512 > length $self->{byte_buffer}) { |
|
|
$self->{filehandle}->read ($self->{byte_buffer}, 256, |
|
|
length $self->{byte_buffer}); |
|
|
} |
|
|
|
|
|
my $r; |
|
|
unless ($error) { |
|
|
my $string = Encode::decode ($self->{perl_encoding_name}, |
|
|
$self->{byte_buffer}, |
|
|
Encode::FB_QUIET ()); |
|
|
if (length $string) { |
|
|
push @{$self->{character_queue}}, split //, $string; |
|
|
$r = shift @{$self->{character_queue}}; |
|
|
if (length $self->{byte_buffer}) { |
|
|
$self->{continue} = 1; |
|
|
} |
|
|
} else { |
|
|
if (length $self->{byte_buffer}) { |
|
|
$error = 1; |
|
|
} else { |
|
|
$r = undef; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if ($error) { |
|
|
$r = substr $self->{byte_buffer}, 0, 1, ''; |
|
|
my $etype = 'illegal-octets-error'; |
|
|
if ($r =~ /^[\xA1-\xFE]/) { |
|
|
if ($self->{byte_buffer} =~ s/^([\xA1-\xFE])//) { |
|
|
$r .= $1; |
|
|
$etype = 'unassigned-code-point-error'; |
|
|
} |
|
|
} elsif ($r eq "\x8F") { |
|
|
if ($self->{byte_buffer} =~ s/^([\xA1-\xFE][\xA1-\xFE]?)//) { |
|
|
$r .= $1; |
|
|
$etype = 'unassigned-code-point-error' if length $1 == 2; |
|
|
} |
|
|
} elsif ($r eq "\x8E") { |
|
|
if ($self->{byte_buffer} =~ s/^([\xA1-\xFE])//) { |
|
|
$r .= $1; |
|
|
$etype = 'unassigned-code-point-error'; |
|
|
} |
|
|
} elsif ($r eq "\xA0" or $r eq "\xFF") { |
|
|
$etype = 'unassigned-code-point-error'; |
|
|
} |
|
|
$self->{onerror}->($self, $etype, octets => \$r, |
|
|
level => $self->{level}->{$self->{error_level}->{$etype}}); |
|
|
} |
|
|
|
|
|
return $r; |
|
|
} # getc |
|
|
|
|
|
## TODO: This is not good for performance. Should be replaced |
|
|
## by read-centric implementation. |
|
|
sub read ($$$;$) { |
|
|
#my ($self, $scalar, $length, $offset) = @_; |
|
|
my $length = $_[2]; |
|
|
my $r = ''; |
|
|
while ($length > 0) { |
|
|
my $c = $_[0]->getc; |
|
|
last unless defined $c; |
|
|
$r .= $c; |
|
|
$length--; |
|
|
} |
|
|
substr ($_[1], $_[3]) = $r; |
|
|
## NOTE: This would do different thing from what Perl's |read| do |
|
|
## if $offset points beyond the end of the $scalar. |
|
|
return length $r; |
|
|
} # read |
|
|
|
|
| 885 |
package Whatpm::Charset::DecodeHandle::ISO2022JP; |
package Whatpm::Charset::DecodeHandle::ISO2022JP; |
| 886 |
push our @ISA, 'Whatpm::Charset::DecodeHandle::Encode'; |
push our @ISA, 'Whatpm::Charset::DecodeHandle::Encode'; |
| 887 |
|
|
| 1020 |
return length $r; |
return length $r; |
| 1021 |
} # read |
} # read |
| 1022 |
|
|
| 1023 |
package Whatpm::Charset::DecodeHandle::ShiftJIS; |
sub manakai_read_until ($$$;$) { |
| 1024 |
push our @ISA, 'Whatpm::Charset::DecodeHandle::Encode'; |
#my ($self, $scalar, $pattern, $offset) = @_; |
|
|
|
|
sub getc ($) { |
|
| 1025 |
my $self = $_[0]; |
my $self = $_[0]; |
| 1026 |
return shift @{$self->{character_queue}} if @{$self->{character_queue}}; |
my $c = $self->getc; |
| 1027 |
|
if ($c =~ /^$_[2]/) { |
| 1028 |
my $error; |
substr ($_[1], $_[3]) = $c; |
| 1029 |
if ($self->{continue}) { |
return 1; |
| 1030 |
if ($self->{filehandle}->read ($self->{byte_buffer}, 256, |
} elsif (defined $c) { |
| 1031 |
length $self->{byte_buffer})) { |
$self->ungetc (ord $c); |
| 1032 |
# |
return 0; |
| 1033 |
} else { |
} else { |
| 1034 |
$error = 1; |
return 0; |
|
} |
|
|
$self->{continue} = 0; |
|
|
} elsif (512 > length $self->{byte_buffer}) { |
|
|
$self->{filehandle}->read ($self->{byte_buffer}, 256, |
|
|
length $self->{byte_buffer}); |
|
|
} |
|
|
|
|
|
my $r; |
|
|
unless ($error) { |
|
|
my $string = Encode::decode ($self->{perl_encoding_name}, |
|
|
$self->{byte_buffer}, |
|
|
Encode::FB_QUIET ()); |
|
|
if (length $string) { |
|
|
push @{$self->{character_queue}}, split //, $string; |
|
|
$r = shift @{$self->{character_queue}}; |
|
|
if (length $self->{byte_buffer}) { |
|
|
$self->{continue} = 1; |
|
|
} |
|
|
} else { |
|
|
if (length $self->{byte_buffer}) { |
|
|
$error = 1; |
|
|
} else { |
|
|
$r = undef; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if ($error) { |
|
|
$r = substr $self->{byte_buffer}, 0, 1, ''; |
|
|
my $etype = 'illegal-octets-error'; |
|
|
if ($r =~ /^[\x81-\x9F\xE0-\xFC]/) { |
|
|
if ($self->{byte_buffer} =~ s/(.)//s) { |
|
|
$r .= $1; # not limited to \x40-\xFC - \x7F |
|
|
$etype = 'unassigned-code-point-error'; |
|
|
} |
|
|
## NOTE: Range [\xF0-\xFC] is unassigned and may be used as a single-byte |
|
|
## character or as the first-byte of a double-byte character according |
|
|
## to JIS X 0208:1997 Appendix 1. However, the current practice is |
|
|
## use the range as the first-byte of double-byte characters. |
|
|
} elsif ($r =~ /^[\x80\xA0\xFD-\xFF]/) { |
|
|
$etype = 'unassigned-code-point-error'; |
|
|
} |
|
|
$self->{onerror}->($self, $etype, octets => \$r, |
|
|
level => $self->{level}->{$self->{error_level}->{$etype}}); |
|
|
} |
|
|
|
|
|
return $r; |
|
|
} # getc |
|
|
|
|
|
## TODO: This is not good for performance. Should be replaced |
|
|
## by read-centric implementation. |
|
|
sub read ($$$;$) { |
|
|
#my ($self, $scalar, $length, $offset) = @_; |
|
|
my $length = $_[2]; |
|
|
my $r = ''; |
|
|
while ($length > 0) { |
|
|
my $c = $_[0]->getc; |
|
|
last unless defined $c; |
|
|
$r .= $c; |
|
|
$length--; |
|
| 1035 |
} |
} |
| 1036 |
substr ($_[1], $_[3]) = $r; |
} # manakai_read_until |
|
## NOTE: This would do different thing from what Perl's |read| do |
|
|
## if $offset points beyond the end of the $scalar. |
|
|
return length $r; |
|
|
} # read |
|
| 1037 |
|
|
| 1038 |
$Whatpm::Charset::CharsetDef->{'urn:x-suika-fam-cx:charset:us-ascii'} = |
$Whatpm::Charset::CharsetDef->{'urn:x-suika-fam-cx:charset:us-ascii'} = |
| 1039 |
$Whatpm::Charset::CharsetDef->{'urn:x-suika-fam-cx:charset:us'} = |
$Whatpm::Charset::CharsetDef->{'urn:x-suika-fam-cx:charset:us'} = |