| 1 | #!/usr/local/bin/perl | ||
| 2 | use FindBin; | ||
| 3 | use lib "$FindBin::Bin/../lib"; | ||
| 4 | use lib "$FindBin::Bin/../../Parse-Binary/lib"; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Parse::AFP; | ||
| 8 | use Parse::AFP::PTX; | ||
| 9 | use Getopt::Std; | ||
| 10 | use File::Path 'rmtree'; | ||
| 11 | |||
| 12 | { | ||
| 13 | package Parse::AFP::Record; | ||
| 14 | sub new { | ||
| 15 | my ($self, $buf, $attr) = @_; | ||
| 16 | if (substr($$buf, 3, 3) eq "\xD3\xEE\x9B") { | ||
| 17 | return bless($buf, 'PTX'); | ||
| 18 | } | ||
| 19 | return $self->SUPER::new($buf, $attr); | ||
| 20 | } | ||
| 21 | sub PTX::done { return } | ||
| 22 | sub PTX::callback { main::PTX($_[0], $_[0]) } | ||
| 23 | } | ||
| 24 | |||
| 25 | my %NoUDC = ( | ||
| 26 | 947 => qr{ | ||
| 27 | ^ | ||
| 28 | (?: | ||
| 29 | [\x00-\x7f]+ | ||
| 30 | | | ||
| 31 | (?:[\xA1-\xC5\xC9-\xF9].)+ | ||
| 32 | | | ||
| 33 | (?:\xC6[^\xA1-\xFE])+ | ||
| 34 | )* | ||
| 35 | $ | ||
| 36 | }x, | ||
| 37 | 835 => qr{^[^\x92-\xFE]*$}x, | ||
| 38 | ); | ||
| 39 | |||
| 40 | my %opts; | ||
| 41 | getopts('i:o:c:', \%opts); | ||
| 42 | my $input = $opts{i} || shift; | ||
| 43 | my $output = $opts{o} || shift || 'udcdir'; | ||
| 44 | my $codepage = $opts{c} || 947; | ||
| 45 | |||
| 46 | die "Usage: $0 -c [947|835] -i input.afp -o udcdir\n" | ||
| 47 | if grep !defined, $input, $codepage, $output; | ||
| 48 | |||
| 49 | rmtree([ $output ]) if -d $output; | ||
| 50 | |||
| 51 | my $NoUDC = $NoUDC{$codepage} or die "Unknown codepage: $codepage\n"; | ||
| 52 | my ($has_udc, $name, $prev, $has_BNG, $PTX_cnt); | ||
| 53 | $name = $prev = 0; | ||
| 54 | |||
| 55 | mkdir $output; | ||
| 56 | my $afp = Parse::AFP->new($input, { lazy => 1, output_file => "$output/0" }); | ||
| 57 | $afp->callback_members([qw( BMO BNG BPG PTX * )]); | ||
| 58 | |||
| 59 | if ($has_udc) { | ||
| 60 | rename("$output/$name" => "$output/$name.udc") or die $!; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub BNG { | ||
| 64 | $prev = $name; $name++; | ||
| 65 | $has_BNG = 1; | ||
| 66 | |||
| 67 | $afp->set_output_file("$output/$name"); | ||
| 68 | |||
| 69 | if ($has_udc) { | ||
| 70 | print STDERR '.'; | ||
| 71 | rename("$output/$prev" => "$output/$prev.udc") or die $!; | ||
| 72 | $has_udc = 0; | ||
| 73 | } | ||
| 74 | |||
| 75 | $_[0]->done; | ||
| 76 | } | ||
| 77 | |||
| 78 | BEGIN { *BMO = *BPG; } | ||
| 79 | |||
| 80 | sub BPG { | ||
| 81 | if( !$has_BNG ) { | ||
| 82 | |||
| 83 | $prev = $name; $name++; | ||
| 84 | $afp->set_output_file("$output/$name"); | ||
| 85 | |||
| 86 | if ($has_udc) { | ||
| 87 | print STDERR '.'; | ||
| 88 | rename("$output/$prev" => "$output/$prev.udc") or die $!; | ||
| 89 | $has_udc = 0; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | $has_BNG = 0; | ||
| 94 | } | ||
| 95 | |||
| 96 | $_[0]->done; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub PTX { | ||
| 100 | my ($rec, $buf) = @_; | ||
| 101 | |||
| 102 | return $rec->done if $has_udc; | ||
| 103 | |||
| 104 | # Now iterate over $$buf. | ||
| 105 | my $pos = 11; | ||
| 106 | my $len = length($$buf); | ||
| 107 | |||
| 108 | while ($pos < $len) { | ||
| 109 | my ($size, $code) = unpack("x${pos}CC", $$buf); | ||
| 110 | |||
| 111 | $size or die "Incorrect parsing: $pos\n"; | ||
| 112 | |||
| 113 | if ($code == 0xDA or $code == 0xDB) { | ||
| 114 | if ( substr($$buf, $pos + 2, $size - 2) !~ /$NoUDC/o) { | ||
| 115 | $has_udc = 1; | ||
| 116 | last; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | $pos += $size; | ||
| 121 | } | ||
| 122 | |||
| 123 | $rec->done; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub __ { $_[0]->done } |