Debian Codegraph @

bin/compile_encoding

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