Debian Codegraph @

texk/texlive/linked_scripts/dosepsbin/dosepsbin.pl

raw
1 #!/usr/bin/env perl
2 #
3 use strict;
4 $^W=1;
5
6 my $prj = 'dosepsbin';
7 my $version = '1.2';
8 my $date = '2012/03/22';
9 my $author = 'Heiko Oberdiek';
10 my $copyright = "Copyright 2011-2012 $author";
11
12 my $verbose = 0;
13 my $quiet = 0;
14 my $inputfile = '';
15 my $file_eps = '';
16 my $file_wmf= '';
17 my $file_tiff = '';
18 my $buf_size = 0x1000000;
19
20 my $title = "\U$prj\E $date v$version, $copyright\n";
21
22 sub die_error ($;$) {
23 my $msg = shift;
24 $msg = "\n!!! Error: $msg!\n";
25 if (@_) {
26 my $ref = shift;
27 foreach my $key (sort keys %$ref) {
28 my $value = $$ref{$key};
29 $key =~ s/ /_/g;
30 $key =~ s/^\d+_?//;
31 $msg .= " [$key: $value]\n";
32 }
33 }
34 die $msg;
35 }
36
37 sub warning ($) {
38 my $msg = shift;
39 print "!!! Warning: $msg!\n";
40 }
41
42 sub verbose (@) {
43 return unless $verbose;
44 my @msg = @_;
45 print "* @msg\n";
46 }
47
48 sub verbose_kv ($$) {
49 return unless $verbose;
50 my $key = shift;
51 my $value = shift;
52 $key =~ s/ /_/g;
53 print " [$key: $value]\n";
54 }
55
56 sub die_usage {
57 my $msg = $_[0];
58 pod2usage(
59 -exitstatus => 2,
60 -msg => "\n==> $msg!\n");
61 }
62
63 use Getopt::Long;
64 use Pod::Usage;
65
66 GetOptions(
67 'verbose' => sub {
68 $verbose = 1;
69 $quiet = 0;
70 },
71 'quiet' => sub {
72 $quiet = 1;
73 $verbose = 0;
74 },
75 'help|?' => sub {
76 print $title;
77 pod2usage(1);
78 },
79 'man' => sub {
80 pod2usage(-exitstatus => 0, -verbose => 2);
81 },
82 'version' => sub {
83 print "$prj $date v$version\n";
84 exit(0);
85 },
86 'inputfile=s' => \$inputfile,
87 'eps-file=s' => \$file_eps,
88 'wmf-file=s' => \$file_wmf,
89 'tiff-file=s' => \$file_tiff,
90 ) or die_usage('Unknown option');
91
92 print $title unless $quiet;
93
94 verbose_kv 'program name', $0;
95 verbose_kv 'osname', $^O;
96 verbose_kv 'perl version', $^V ? sprintf('v%vd', $^V) : $];
97
98 $inputfile = shift @ARGV if not $inputfile and @ARGV;
99 $inputfile or die_error "Missing input file";
100
101 # If input file does not exist, try with extension ".eps"
102 if (not -f $inputfile) {
103 my $file = "$inputfile.eps";
104 $inputfile = $file if -f $file;
105 }
106 -f $inputfile or
107 die_error "Input file not found", {
108 'input file' => $inputfile
109 };
110 verbose_kv 'input file', $inputfile;
111
112 # Get file size of input file
113 my @tmp = stat $inputfile;
114 @tmp or die_error "Getting size of Input file `$inputfile' failed";
115 my $inputfile_size = $tmp[7];
116 $inputfile_size > 30 or
117 die_error "Input file size is too small", {
118 '1 input file' => $inputfile,
119 '2 file size' => $inputfile_size,
120 };
121 verbose_kv 'input file size', $inputfile_size;
122
123 # Open input file
124 open(IN, '<', $inputfile) or # die_error "Input file `$inputfile' not found";
125 die_error "Cannot open input file", {
126 '1 input file' => $inputfile,
127 '2 os error' => $!,
128 };
129 binmode(IN);
130
131 # Read header
132 my $header;
133 read IN, $header, 30 or
134 die_error "Reading the header of the input file failed", {
135 'file' => $inputfile,
136 'os error' => $!,
137 };
138 # Check ASCII PS header
139 if ('%!' eq substr $header, 0, 2) {
140 $header =~ m/^(%![A-Za-z0-9\.\- \t]+)/;
141 my $first_line = $1;
142 die_error "Input file seems to be an ASCII PostScript file", {
143 '1 input file' => $inputfile,
144 '2 file header' => $first_line,
145 }
146 }
147 my ($id,
148 $offset_ps, $length_ps,
149 $offset_wmf, $length_wmf,
150 $offset_tiff, $length_tiff,
151 $checksum) = unpack 'NV6n', $header;
152
153 my $id_hex = unpack 'H8', pack 'N', $id;
154 $id_hex = "0x\U$id_hex\E";
155 verbose_kv 'header id', $id_hex;
156 $id eq 0xC5D0D3C6 or
157 die_error "Input file is not a `DOS EPS binary file'", {
158 '1 input file' => $inputfile,
159 '2 file header id' => $id_hex,
160 };
161
162 # Check checksum
163 if ($checksum == 0xFFFF) {
164 verbose_kv 'checksum', '0xFFFF (ignored)';
165 }
166 else {
167 my $cs = 0;
168 map { $cs ^= $_ } unpack 'n14', $header;
169 if ($cs != $checksum) {
170 my $cs_hex = unpack 'H4', pack 'n', $cs;
171 $cs_hex = "0x\U$cs_hex\E";
172 my $cs_found = unpack 'H4', pack 'n', $checksum;
173 $cs_found = "0x\U$cs_found\E";
174 die_error "Checksum mismatch", {
175 '1 input file' => $inputfile,
176 '2 checksum found' => $cs_found,
177 '3 checksum expected' => $cs_hex,
178 };
179 }
180 }
181
182 verbose_kv 'offset of PS section', $offset_ps;
183 verbose_kv 'length of PS section', $length_ps;
184 verbose_kv 'offset of WMF section', $offset_wmf;
185 verbose_kv 'length of WMF section', $length_wmf;
186 verbose_kv 'offset of TIFF section', $offset_tiff;
187 verbose_kv 'length of TIFF section', $length_tiff;
188
189 {
190 my %check_positions;
191 sub check_section ($$$) {
192 my $type = shift;
193 my $ref_off = shift;
194 my $ref_len = shift;
195 if ($$ref_off == 0 and $$ref_len == 0) {
196 print "--> $type section is not available.\n" unless $quiet;
197 return '<not present>';
198 }
199 $$ref_off >= 30 and $$ref_off < $inputfile_size or
200 die_error "Invalid offset of PS section", {
201 '1 input file' => $inputfile,
202 "2 offset of $type section" => $$ref_off,
203 "3 length of $type section" => $$ref_len,
204 };
205 $$ref_len >= 0 and $$ref_off + $$ref_len <= $inputfile_size or
206 die_error "Invalid length of PS section", {
207 '1 input file' => $inputfile,
208 "2 offset of $type section" => $$ref_off,
209 "3 length of $type section" => $$ref_len,
210 };
211 $check_positions{$$ref_off} = $$ref_off + $$ref_len;
212 print "--> $type section with $$ref_len bytes.\n" unless $quiet;
213 return sprintf '%d..%d', $$ref_off, $$ref_off + $$ref_len - 1;
214 }
215 my $sec_ps = check_section 'PS', \$offset_ps, \$length_ps;
216 verbose_kv 'position of PS section', $sec_ps;
217 my $sec_wmf = check_section 'WMF', \$offset_wmf, \$length_wmf;
218 verbose_kv 'position of WMF section', $sec_wmf;
219 my $sec_tiff = check_section 'TIFF', \$offset_tiff, \$length_tiff;
220 verbose_kv 'position of TIFF section', $sec_tiff;
221 my $pos = 30;
222 foreach my $beg (sort keys %check_positions) {
223 $check_positions{$beg} >= $pos or
224 die_error "Section overlap detected", {
225 '1 input file' => $inputfile,
226 '2 position of header' => "0..29",
227 '3 position of PS section' => $sec_ps,
228 '4 position of WMF section' => $sec_wmf,
229 '5 position of TIFF section' => $sec_tiff,
230 };
231 }
232 }
233
234 sub write_file ($$$$) {
235 my $type = shift;
236 my $file = shift;
237 my $off = shift;
238 my $len = shift;
239 my $org_len = $len;
240 return unless $file;
241 verbose_kv "$type file", $file;
242 if ($off <= 0 or $len <= 0) {
243 warning "No $type section found";
244 return;
245 }
246 my $position = $off;
247 seek IN, $off, 0 or
248 die_error "Moving to $type section offset failed", {
249 '1 input file' => $inputfile,
250 '2 position' => $off,
251 '3 os error' => $!,
252 };
253 open(OUT, '>', $file) or
254 die_error "Opening $type file for writing failed", {
255 "1 $type file" => $file,
256 '2 os error' => $!,
257 };
258 binmode(OUT);
259 while ($len > 0) {
260 my $read_len = ($len > $buf_size) ? $buf_size : $len;
261 my $buf;
262 my $read_count = read IN, $buf, $read_len;
263 $read_count > 0 or
264 die_error "Reading file failed", {
265 '1 input file' => $inputfile,
266 "2 $type file" => $file,
267 '3 offset' => $position,
268 '4 length' => $read_len,
269 '5 os error' => $!,
270 };
271 $position += $read_count;
272 print OUT $buf or
273 die_error "Writing $type file failed", {
274 "1 $type file" => $file,
275 '2 offset' => $org_len - $len,
276 '3 length' => $read_count,
277 '4 os error' => $!,
278 };
279 $len -= $read_count;
280 }
281 close(OUT);
282 print "==> $type file written: [$file]\n" unless $quiet;
283 }
284
285 write_file 'TIFF', $file_tiff, $offset_tiff, $length_tiff;
286 write_file 'WMF', $file_wmf, $offset_wmf, $length_wmf;
287 write_file 'EPS', $file_eps, $offset_ps, $length_ps;
288
289 close(IN);
290
291 1;
292
293 __DATA__
294
295 =head1 NAME
296
297 dosepsbin -- Extract PS/WMF/TIFF sections from DOS EPS binary files
298
299 =head1 VERSION
300
301 2012-03-22 v1.2
302
303 =head1 SYNOPSIS
304
305 The progam B<dosepsbin> analyses an EPS file that is not
306 a plain ASCII PostScript file but given as DOS EPS binary file.
307
308 dosepsbin [options] <input file>
309
310 First it analyzes the I<input file>, validates its header
311 and summarizes the available sections. Depending on the
312 given options, the sections are then written to files.
313
314 Options:
315
316 --eps-file <file> Write PS section to <file>.
317 --wmf-file <file> Write WMF section to <file>.
318 --tiff-file <file> Write TIFF section to <file>.
319 --inputfile <file> The name of the input file.
320 --verbose Verbose output.
321 --quiet Only errors and warnings are printed.
322 --help Brief help message.
323 --man Full documentation.
324 --version Print version identification.
325
326 The files for output must be different from the input file.
327
328 =head1 DESCRIPTION
329
330 =head2 DOS EPS Binary File Format
331
332 A Encapsulated PostScript (EPS) file can also given in a special
333 binary format to support the inclusion of a thumbnail. The file
334 format starts with a binary header that contains the positions of
335 the possible sections:
336
337 =over
338
339 =item * Postscript (PS)
340
341 =item * Windows Metafile Format (WMF)
342
343 =item * Tag Image File Format (TIFF)
344
345 =back
346
347 The PS section must be present and either the WMF file or the TIFF
348 file should be given.
349
350 =head1 OPTIONS
351
352 =over
353
354 =item B<->B<-eps-file>=<I<file>>
355
356 The PS section is written to <I<file>>. The output file must
357 be different from the input file.
358
359 =item B<->B<-wmf-file>=<I<file>>
360
361 The WMF section is written to <I<file>> if present. The output
362 file must be different from the input file.
363
364 =item B<->B<-tiff-file>=<I<file>>
365
366 The TIFF section is written to <I<file>> if present. The output
367 file must be different from the input file.
368
369 =item B<->B<-inputfile>=<I<file>>
370
371 The input file can also be given directly on the command line.
372 If the file does not exist, then the file with extension `.eps'
373 is tried.
374
375 =item B<->B<-verbose>
376
377 Verbose messages.
378
379 =item B<->B<-quiet>
380
381 No messages are printed except for errors and warnings.
382
383 =item B<->B<-help>
384
385 Display help screen.
386
387 =item B<->B<-man>
388
389 Prints manual page.
390
391 =item B<->B<-version>
392
393 Print version identification and exit.
394
395 =back
396
397 =head1 EXAMPLES
398
399 The following command extracts the PS section from file F<test.eps>
400 and stores the result in file F<test-ps.eps>:
401
402 dosepsbin --eps-file test-ps.eps test.eps
403
404 =head1 AUTHOR
405
406 Heiko Oberdiek, email: heiko.oberdiek at googlemail.com
407
408 =head1 COPYRIGHT AND LICENSE
409
410 Copyright 2011-2012 by Heiko Oberdiek.
411
412 This library is free software; you may redistribute it and/or
413 modify it under the same terms as Perl itself
414 (Perl Artistic License/GNU General Public License, version 2).
415
416 =head1 SEE ALSO
417
418 The DOS EPS binary file format is described
419 in section "5.2 Windows Metafile or TIFF":
420
421 Adobe Developer Support,
422 Encapsulated PostScript File Format Specification,
423 Version 3.0,
424 1992-05-01,
425 http://partners.adobe.com/public/developer/en/ps/5002.EPSF_Spec.pdf
426
427 =head1 HISTORY
428
429 =over 2
430
431 =item B<2011/11/10 v1.0>
432
433 =over 2
434
435 =item * First version.
436
437 =back
438
439 =item B<2011/12/05 v1.1>
440
441 =over 2
442
443 =item * Typo fixed in help text (thanks Peter Breitenlohner).
444
445 =back
446
447 =item B<2012/03/22 v1.2>
448
449 =over 2
450
451 =item * Fix in validation test for offset of PS section.
452
453 =back
454
455 =back
456
457 =cut