1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
#!perl
#
# compile_encoding
#
# Copyright (C) 1998 Clark Cooper. All rights reserved.
# Copyright (C) 2007-2008, 2014 Steve Hay. All rights reserved.
#
# This script is free software; you can redistribute it and/or modify it under
# the same terms as Perl itself, i.e. under the terms of either the GNU General
# Public License or the Artistic License, as specified in the LICENCE file.
#
use 5.008001;
use strict;
use warnings;
my $Usage=<<'End_of_Usage;';
Usage is:
compile_encoding [-h] [-o output_file] input_file
Compiles the input XML encmap file into a binary encoding file usable
by XML::Parser.
-h Print this message.
-o output_file Put compiled binary into given output file. By
default, a file that has the same basename as
the input file, but with an extension of .enc
is the output.
End_of_Usage;
package Pfxmap;
use fields qw(min max map explen);
sub new {
my $class = shift;
no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
my $pfxmap = fields::new($class);
while (@_) {
my $key = shift;
$pfxmap->{$key} = shift;
}
$pfxmap;
}
package main;
use XML::Encoding;
use integer;
################################################################
# See the encoding.h file in the top level XML::Encoding directory
# to see the format of generated file
my $magic = 0xfeebface;
my $namelength = 40;
my $ofile;
while (defined($ARGV[0]) and $ARGV[0] =~ /^-/) {
my $opt = shift;
if ($opt eq '-o') {
$ofile = shift;
}
elsif ($opt eq '-h') {
print $Usage;
exit 1;
}
else {
$! = 2;
die "Unrecognized option: $opt\n$Usage";
}
}
my $infile = shift;
if (not defined($infile)) {
$! = 3;
die "Encmap XML file not provided\n$Usage";
}
unless (defined($ofile)) {
my $base = $infile;
$base =~ s!^.*/!!;
if ($base =~ /(.*)\.xml$/i) {
$base = $1;
}
$ofile = $base . '.enc';
}
# Do initializations
my @firstbyte;
$#firstbyte = 255;
my $pfxcount = 0;
my $totcount = 0;
my @stack = ();
my $pfxlenref;
my $currmap = new Pfxmap(min => 255, max => 0, map => \@firstbyte);
my $p = new XML::Encoding(ErrorContext => 2,
ExpatRequired => 1,
PushPrefixFcn => \&push_prefix,
PopPrefixFcn => \&pop_prefix,
RangeSetFcn => \&range_set
);
my $name = $p->parsefile($infile);
if (length($name) > $namelength) {
$! = 4;
die "Encoding name too long (> $namelength)\n";
}
my @prefixes;
my $maplen = 0;
my $pflen = 0;
if ($pfxcount) {
push(@prefixes, $currmap);
$currmap->{map} = [];
$maplen = $totcount + $currmap->{max} - $currmap->{min} + 1;
$pflen = $pfxcount + 1;
}
my $i;
for ($i = 0; $i < 256; $i++) {
if (defined($firstbyte[$i])) {
if ($pfxcount) {
$currmap->{map}->[$i] = $firstbyte[$i];
$firstbyte[$i] = - ($firstbyte[$i]->{explen} + 1)
if ref($firstbyte[$i]);
}
}
else {
$firstbyte[$i] = $i < 128 ? $i : -1;
}
}
my $enc;
open($enc, '>', $ofile) or do {
$! = 5;
die "Couldn't open $ofile for writing:\n$!\n";
};
binmode($enc);
#Note the use of network order packings
print $enc pack("Na${namelength}nnN256",
$magic, $name, $pflen, $maplen, @firstbyte);
my @map = ();
my $head = 0;
while (@prefixes) {
my $pfxmap = shift @prefixes;
$head++;
my $len = $pfxmap->{max} - $pfxmap->{min} + 1;
my $mapstart = @map;
my $ispfx = '';
vec($ispfx, 255, 1) = 0;
my $ischar = '';
vec($ischar, 255, 1) = 0;
for ($i = $pfxmap->{min}; $i <= $pfxmap->{max}; $i++) {
my $entry = $pfxmap->{map}->[$i];
if (defined($entry)) {
if (ref($entry)) {
my $pfxent = $entry;
$entry = $head + @prefixes;
push(@prefixes, $pfxent);
vec($ispfx, $i, 1) = 1;
}
else {
vec($ischar, $i, 1) = 1;
}
}
else {
$entry = 0xFFFF;
}
push(@map, $entry);
}
print $enc pack('CCn', $pfxmap->{min}, $len, $mapstart), $ispfx, $ischar;
}
if (@map) {
my $packlist = 'n' . int(@map);
print $enc pack($packlist, @map);
}
close($enc);
exit 0;
################
## End main
################
sub push_prefix {
my ($byte) = @_;
return "Prefix too long"
if (@stack >= 3);
return "Different lengths for same first byte"
if (defined($pfxlenref)
and defined($$pfxlenref)
and $$pfxlenref < @stack);
my $pfxmap = $currmap->{map}->[$byte];
if (defined($pfxmap)) {
return "Prefix already mapped to a character"
unless ref($pfxmap);
# Remove what we've already added in for this prefix so we don't
# count it twice
$totcount -= $pfxmap->{max} - $pfxmap->{min} + 1;
}
else {
$pfxmap = new Pfxmap(min => 255, max => 0, map => []);
$currmap->{map}->[$byte] = $pfxmap;
}
unless (@stack) {
$pfxlenref = \$pfxmap->{explen};
}
$currmap->{min} = $byte
if $byte < $currmap->{min};
$currmap->{max} = $byte
if $byte > $currmap->{max};
$pfxcount++;
push(@stack, $currmap);
$currmap = $pfxmap;
return;
} # End push_prefix
sub pop_prefix {
return "Attempt to pop un-pushed prefix"
unless (@stack);
my $count = $currmap->{max} - $currmap->{min} + 1;
return "Empty prefix not allowed"
unless $count > 0;
$totcount += $count;
$currmap = pop(@stack);
$pfxlenref = undef
unless @stack;
return;
} # End pop_prefix
sub range_set {
my ($byte, $uni, $len) = @_;
my $limit = $byte + $len;
return "Range too long"
if $limit > 256;
if (defined($pfxlenref)) {
if (defined($$pfxlenref)) {
return "Different for same 1st byte"
unless $$pfxlenref == @stack;
}
else {
$$pfxlenref = @stack;
}
}
my $i;
for ($i = $byte; $i < $limit; $i++, $uni++) {
return "Byte already mapped"
if defined($currmap->{map}->[$i]);
$currmap->{map}->[$i] = $uni;
}
$currmap->{min} = $byte
if $byte < $currmap->{min};
$currmap->{max} = $limit - 1
if $limit >= $currmap->{max};
return;
} # End range_set
__END__
=head1 NAME
compile_encoding - compile XML encmap into a binary encoded file for XML::Parser
=head1 SYNOPSIS
compile_encoding [-h] [-o <output_file>] <input_file>
=head1 DESCRIPTION
B<compile_encoding> compiles an input XML encmap file into a binary encoded file
usable by L<XML::Parser|XML::Parser(3pm)>.
=head1 ARGUMENTS
=over 4
=item E<lt>input_fileE<gt>
The XML encmap file to compile.
=back
=head1 OPTIONS
=over 4
=item B<-o E<lt>output_fileE<gt>>
Put compiled binary into given output file. By default, a file that has the
same basename as the input file, but with an extension of F<.enc> is output.
=item B<-h>
Print usage information.
=back
=head1 EXIT STATUS
0 The script exited normally.
1 The script exited after printing the help.
2 Invalid command-line arguments.
>2 An error occurred.
=head1 KNOWN BUGS
I<None>.
=head1 SEE ALSO
L<make_encmap(1p)>,
L<XML::Encoding(3pm)>,
L<XML::Parser(3pm)>.
=head1 AUTHOR
Clark Cooper E<lt>L<coopercc@netheaven.com|mailto:coopercc@netheaven.com>E<gt>.
Steve Hay E<lt>L<shay@cpan.org|mailto:shay@cpan.org>E<gt> is now maintaining
XML::Encoding as of version 2.00.
This manual page was written by Daniel Leidert
E<lt>L<daniel.leidert@wgdd.de|mailto:daniel.leidert@wgdd.de>E<gt> for the Debian
project (but may be used by others).
=head1 COPYRIGHT
Copyright (C) 1998 Clark Cooper. All rights reserved.
Copyright (C) 2007-2008, 2014 Steve Hay. All rights reserved.
=head1 LICENCE
This script is free software; you can redistribute it and/or modify it under the
same terms as Perl itself, i.e. under the terms of either the GNU General Public
License or the Artistic License, as specified in the F<LICENCE> file.
=head1 VERSION
Version 2.11
=head1 DATE
08 Dec 2020
=head1 HISTORY
See the F<Changes> file.
=cut
# Tell Emacs that this is really a perl script
# Local Variables:
# mode:perl
# End:
|