blib/lib/Document/Info.pm untested

filter: Statementstmt 0.0% 0 / 276 Branchbran 0.0% 0 / 174 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 59 Podpod 0.0% 0 / 51 totaltotal 0.0% 0 / 589 CC 88 SCAR 49.6
CC
88
Cov
0%
_checkFiles
600.0
_checkOleFiles
272.0
_tryMSExcel
90.0
CRAP 142.9
1 #$Id$
2 #
3 # Document::Info - determine the document type of office files
4 #
5 # To use it you need perl >=5.004 and additionally the three modules:
6 #
7 # OLE::Storage
8 # Unicode::Map
9 # Startup
10 #
11 # to be found at my directory at your favorite CPAN location or at:
12 #
13 # http://wwwwbs.cs.tu-berlin.de/~schwartz/perl/
14 #
15 # Copyright (C) 1999 Martin Schwartz
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 #
27 # You should have received a copy of the GNU General Public License
28 # along with this program; if not, you should find it at:
29 #
30 # http://wwwwbs.cs.tu-berlin.de/~schwartz/pmh/COPYING
31 #
32 # You can contact me via schwartz@cs.tu-berlin.de
33 #
34  
35 package Document::Info;
36  
37 use strict;
38 use vars qw($VERSION @ISA @EXPORT_OK);
39 require Exporter;
40 require AutoLoader;
41 @ISA = qw(Exporter AutoLoader);
42 $VERSION = '0.01';
43  
44 use Startup;
45 use OLE::PropertySet;
46 use OLE::Storage;
47 use FileHandle;
48  
49 ##
50 ## PUBLIC
51 ##
52  
53 sub new {
54 my ( $proto, $fileName ) = @_;
55 my $class = ref ($proto) || $proto;
56 my $S = bless ( {}, $class );
57 return 0 unless $S -> _Startup ( new Startup() );
58 return 0 unless $S -> _Var ( OLE::Storage->NewVar() );
59 $S -> _fileName ( $fileName );
60 $S -> _Doc ( "" );
61 return 0 unless $S -> _go ( );
62 $S}
63  
64 sub getLocale { $_[0]->_DocLoc() };
65 sub getOS { $_[0]->_DocOS() };
66 sub getRevision { $_[0]->_DocRev() };
67 sub getType { $_[0]->_DocType() }
68  
69 #
70 # Document type.
71 #
72 sub cAstound { "Astound" }
73 sub cMSExcel { "MS Excel" }
74 sub cMSPP { "MS PowerPoint" }
75 sub cMSWord { "MS Word" }
76 sub cMSWorks { "MS Works" }
77 sub cPDF { "PDF" }
78 sub cStarWriter { "StarWriter" }
79 sub cWordPerfect { "WordPerfect" }
80  
81 #
82 # OS type
83 #
84 sub cDOS { "DOS" }
85 sub cMacOS { "MacOS" }
86 sub cWindows { "Windows" }
87  
88  
89 ##
90 ## REST IS PRIVATE
91 ##
92  
93 # Runtime
94 sub _Doc { my $S=shift; $S->{DOC} = shift if @_; $S->{DOC} }
95 sub _Startup { my $S=shift; $S->{STARTUP} = shift if @_; $S->{STARTUP} }
96 sub _Var { my $S=shift; $S->{VAR} = shift if @_; $S->{VAR} }
97  
98 sub _fileName { my $S=shift; $S->{FILENAME} = shift if @_; $S->{FILENAME} }
99 sub _fh { my $S=shift; $S->{FH} = shift if @_; $S->{FH} }
100 sub _pps { my $S=shift; $S->{PPS} = shift if @_; $S->{PPS} }
101  
102 # Properties
103 sub _DocLoc { my $S=shift; $S->{DOCLOC} = shift if @_; $S->{DOCLOC} }
104 sub _DocOS { my $S=shift; $S->{DOCOS} = shift if @_; $S->{DOCOS} }
105 sub _DocRev { my $S=shift; $S->{DOCREV} = shift if @_; $S->{DOCREV} }
106 sub _DocType { my $S=shift; $S->{DOCTYPE} = shift if @_; $S->{DOCTYPE} }
107  
108 sub _go {
109 my ( $S ) = @_;
110 my $status = 0;
111  
112 if (
113 $S->_Doc (
114 OLE::Storage -> open (
115 $S->_Startup(), $S->_Var(), $S->_fileName()
116 )
117 )
118 ) {
119 $status = $S -> _checkOleFiles ( );
120 $S->_Doc() -> close ( );
121 } else {
122 return 0 unless $S->_fh (
123 new FileHandle $S->_fileName(), "r"
124 );
125 $status = $S -> _checkFiles ( );
126 $S->_fh ( undef );
127 }
128 $status;
129 }
130  
131 sub _checkFiles {
132 my ( $S ) = @_;
133 my $id00 = $S -> _fSubstr (0,4);
134 return 0 unless length ( $id00 );
135  
136 if ( 0 ) {
137 } elsif ( unpack("V",$id00)==0xBadDeed ) {
138 return 1 if $S->_tryMSPowerPoint30(0);
139 } elsif ( unpack("N",$id00)==0xBadDeed ) {
140 return 1 if $S->_tryMSPowerPoint30(1);
141 } elsif ( unpack("V",$id00)==0x002da5db ) {
142 return 1 if $S->_tryMSWord20Win(0);
143 } elsif ( unpack("V",$id00)==0x0021a59b ) {
144 return 1 if $S->_tryMSWord10Win(0);
145 } elsif ( unpack("V",$id00)==0x230037fe ) {
146 return 1 if $S->_tryMSWord51Mac(0);
147 } elsif ( unpack("V",$id00)==0x1c0037fe ) {
148 return 1 if $S->_tryMSWord40Mac(0);
149 } elsif ( unpack("N",$id00)==0xff575043 ) {
150 return 1 if $S->_tryWordPerfect(1);
151 } elsif ( (unpack("V",$id00)&0xffff)==0xbe31 ) {
152 return 1 if $S->_tryMSWriteAndMSWordDOS(1);
153 } elsif ( (unpack("V",$id00)&0xffff)==0xbe32 ) {
154 return 1 if $S->_tryMSWrite(1);
155 } elsif ( $id00 eq "%PDF" ) {
156 return 1 if $S->_tryPDF();
157 } else {
158 return 1 if $S->_tryMSExcel ( );
159 }
160 }
161  
162 sub _checkOleFiles {
163 my ( $S ) = @_;
164 my %dir = ( );
165 return 0 unless $S->_Doc()->directory ( 0, \%dir, "string" );
166 if ( $S->_pps($dir{StarWriterDocument}) ) {
167 return 1 if $S -> _tryOleStarWriter ( \%dir );
168 }
169 if ( $S->_pps($dir{'Astound Data'}) ) {
170 return 1 if $S -> _tryOleAstound ( \%dir );
171 }
172 if ( $S->_pps($dir{WordDocument}) ) {
173 return 1 if $S -> _tryOleMSWord ( \%dir );
174 }
175 if ( $S->_pps($dir{PP40}) ) {
176 return 1 if $S -> _tryOleMSPowerPoint40 ( );
177 }
178 if ( $S->_pps($dir{'PowerPoint Document'}) ) {
179 return 1 if $S -> _tryOleMSPowerPoint95 ( \%dir );
180 }
181 if ( $S->_pps($dir{MatOST}) ) {
182 return 1 if $S -> _tryOleMSWorks40 ( );
183 }
184 if ( $dir{Workbook}||$dir{Book} ) {
185 return 1 if $S -> _tryOleMSExcel ( \%dir );
186 }
187 return 0;
188 }
189  
190 ##
191 ## -- File types -------------------------------------------------
192 ##
193  
194 #
195 # Astound
196 #
197  
198 sub _tryOleAstound {
199 my ( $S ) = @_;
200 $S -> _DocType ( cAstound() );
201 }
202  
203 #
204 # MS Excel
205 #
206  
207 sub _tryOleMSExcel {
208 my ( $S, $dir ) = @_;
209 $S -> _DocType ( cMSExcel() );
210 if ($dir->{Workbook} && $dir->{Book}) {
211 $S -> _DocRev ( "5.0,95,97,2000" );
212 } elsif ( $S->_pps($dir->{Workbook}) ) {
213 $S -> _tryMSExcel ( );
214 if ( !$S->_DocRev() ) {
215 $S -> _DocRev ( "97,2000" );
216 }
217 } else {
218 $S -> _DocRev ( "5.0,95" );
219 }
220 $S -> _setMacOrWin ( );
221 1}
222  
223 sub _tryMSExcel {
224 my ( $S ) = @_;
225  
226 my ($type, $len);
227 my $o = 0;
228 my $max = $S -> _fLen() - 1;
229 while ( $o<=$max ) {
230 return 0 if ( ($o+4)>$max ); # corrupt => not an excel file
231 $type = unpack("v",$S->_fSubstr($o,2)); $o+=2;
232 $len = unpack("v",$S->_fSubstr($o,2)); $o+=2;
233 return 0 if ( ($o+$len)>$max ); # corrupt => not an excel file
234 if ($type==0x0009) {
235 $S -> _DocType ( cMSExcel() );
236 $S -> _DocRev ( "2.0" );
237 return 1;
238 } elsif ($type==0x0209) {
239 $S -> _DocType ( cMSExcel() );
240 $S -> _DocRev ( "3.0" );
241 return 1;
242 } elsif ($type==0x0409) {
243 $S -> _DocType ( cMSExcel() );
244 $S -> _DocRev ( "4.0" );
245 return 1;
246 } elsif ($type==0x0809) {
247 my $biffType = unpack("v",$S->_fSubstr($o,2));
248 $S -> _DocType ( cMSExcel() );
249 if ($biffType==0x500) {
250 $S -> _DocRev ( "5.0,95" );
251 } elsif ($biffType=0x600) {
252 $S -> _DocRev ( "97,2000" );
253 } else {
254 $S -> _DocRev ( ">2000" );
255 }
256 return 1;
257 }
258 $o += $len;
259 }
260 0}
261  
262 #
263 # MS PowerPoint
264 #
265  
266 sub _tryOleMSPowerPoint95 {
267 my ( $S, $dir ) = @_;
268 $S -> _DocType ( cMSPP() );
269 if ( $dir->{PP97_DUALSTORAGE} ) {
270 $S -> _DocRev ( "95,97,2000" );
271 } else {
272 if ( $dir->{Header} ) {
273 $S -> _DocRev ( "95" );
274 } else {
275 $S -> _DocRev ( "97,2000" );
276 }
277 }
278 $S -> _setMacOrWin ( );
279 1}
280  
281 sub _tryOleMSPowerPoint40 {
282 my ( $S ) = @_;
283 $S -> _DocType ( cMSPP() );
284 $S -> _DocRev ( "4.0" );
285 $S -> _setMacOrWin ( );
286 1}
287  
288 sub _tryMSPowerPoint30 {
289 my ( $S, $byteOrder ) = @_;
290 $S -> _DocType ( cMSPP() );
291 $S -> _DocRev ( "3.0" );
292 $S -> _DocOS ( $byteOrder ? cMacOS() : cWindows() );
293 1}
294  
295 #
296 # MS Word
297 #
298  
299 sub _tryMSWriteAndMSWordDOS {
300 my ( $S ) = @_;
301 my $type = unpack("v",$S->_fSubstr(2,2));
302 if ( ($type==1) || ($type==2) || ($type==3) ) {
303 return $S -> _tryMSWordDOS ( );
304 } else {
305 if ( unpack("v",$S->_fSubstr(0x60,2)) ) {
306 return $S -> _tryMSWrite ( );
307 } else {
308 return $S -> _tryMSWordDOS ( );
309 }
310 }
311 1}
312  
313 sub _tryMSWordDOS {
314 my ( $S ) = @_;
315 $S -> _DocType ( cMSWord() );
316 $S -> _DocRev ( "5.0" );
317 $S -> _DocOS ( cDOS() );
318 1}
319  
320 sub _tryMSWord40Mac {
321 my ( $S, $byteOrder ) = @_;
322 $S -> _DocType ( cMSWord() );
323 $S -> _DocRev ( "4.0" );
324 $S -> _DocOS ( cMacOS() );
325 1}
326  
327 sub _tryMSWord51Mac {
328 my ( $S, $byteOrder ) = @_;
329 $S -> _DocType ( cMSWord() );
330 $S -> _DocRev ( "5.1" );
331 $S -> _DocOS ( cMacOS() );
332 1}
333  
334 sub _tryMSWord10Win {
335 my ( $S, $byteOrder ) = @_;
336 $S -> _DocType ( cMSWord() );
337 $S -> _DocRev ( "1.0" );
338 $S -> _DocOS ( cWindows() );
339 $S -> _DocLoc ( $S->_getLocaleForMS_C_L(unpack("v",$S->_fSubstr(6,2))) );
340 1}
341  
342 sub _tryMSWord20Win {
343 my ( $S, $byteOrder ) = @_;
344 $S -> _DocType ( cMSWord() );
345 $S -> _DocRev ( "2.0" );
346 $S -> _DocOS ( cWindows() );
347 $S -> _DocLoc ( $S->_getLocaleForMS_C_L(unpack("v",$S->_fSubstr(6,2))) );
348 1}
349  
350 sub _tryOleMSWord {
351 my ( $S, $dir ) = @_;
352 $S -> _DocType ( cMSWord() );
353 my $buf = $S->_fSubstr ( 5, 1 );
354 if ( length($buf) ) {
355 my $id = unpack ( "C", $buf );
356 if ( $id==0xc0 ) {
357 $S -> _DocRev ( "6.0" );
358 } elsif ( $id==0xe0 ) {
359 $S -> _DocRev ( "95" );
360 } elsif ( $id==0x00 ) {
361 $S -> _DocRev ( "97" );
362 } elsif ( $id==0x20 ) {
363 $S -> _DocRev ( "2000" );
364 } else {
365 # Nanu?
366 }
367 }
368 if ( !$S->_DocRev() ) { # Hoops?
369 if ( $dir->{'1Table'} || $dir->{'2Table'} ) {
370 $S -> _DocRev ( "97,2000" );
371 } else {
372 $S -> _DocRev ( "6.0,95" );
373 }
374 }
375 $S -> _DocLoc ( $S->_getLocaleForMS_C_L(unpack("v",$S->_fSubstr(6,2))) );
376 $S -> _setMacOrWin ( );
377 1}
378  
379 #
380 # MS Works
381 #
382  
383 sub _tryOleMSWorks40 {
384 my ( $S ) = @_;
385 $S -> _DocType ( cMSWorks() );
386 $S -> _DocRev ( "4.0" );
387 $S -> _DocOS ( cWindows() );
388 1}
389  
390 #
391 # MS Write
392 #
393  
394 sub _tryMSWrite {
395 my ( $S ) = @_;
396 $S -> _DocType ( cMSWrite() );
397 $S -> _DocOS ( cWindows() );
398 1}
399  
400 #
401 # PDF
402 #
403  
404 sub _tryPDF {
405 my ( $S ) = @_;
406 if ( $S->_fSubstr(4,10) =~ /-(\d+\.\d+)\b/ ) {
407 $S -> _DocRev ( $1 );
408 $S -> _DocType ( cPDF() );
409 } else {
410 return 0;
411 }
412 1}
413  
414 #
415 # StarWriter
416 #
417  
418 sub _tryOleStarWriter {
419 my ( $S, $dir ) = @_;
420 $S -> _DocType ( cStarWriter() );
421  
422 if ( my $pps=$dir->{"\01CompObj"} ) {
423 if (
424 tie my %P, 'OLE::PropertySet',
425 $S->_Startup(), $S->_Var(), $S->_pps(), $S->_Doc(), "string"
426 ) {
427 $S -> _DocRev ( (split/\s+/,$P{0})[1] );
428 }
429 }
430 }
431  
432 #
433 # WordPerfect
434 #
435  
436 sub _tryWordPerfect {
437 my ( $S, $byteOrder ) = @_;
438 $S -> _DocType ( cWordPerfect() );
439 1}
440  
441 ##
442 ## -- Tools ------------------------------------------------------
443 ##
444  
445 sub _getLocaleForMS_C_L {
446 my ( $S, $id ) = @_;
447 # 2do!
448 return $id ? "($id)" : "";
449 }
450  
451 #
452 # When other tests fail this might give a clue about the origin OS.
453 # The root entry of MacOS-Ole-Files has no name.
454 #
455 sub _setMacOrWin {
456 my ( $S ) = @_;
457 if ( !$S->_DocOS() ) {
458 if ( $S->_Doc()->name(0)->string() eq 'Root Entry' ) {
459 $S -> _DocOS ( cWindows() );
460 } else {
461 $S -> _DocOS ( cMacOS() );
462 }
463 }
464 1}
465  
466 sub _fSubstr {
467 my ( $S, $o, $lmax, $lmin ) = @_;
468 $lmax = $lmin if $lmax < $lmin;
469 my $buf = "";
470 my $read;
471 if ( !$S->_pps() ) {
472 my $fh = $S -> _fh ( );
473 $fh -> seek ( $o, 0 );
474 $read = $fh -> read ($buf, $lmax);
475 if ( $read < $lmin ) {
476 $buf = "";
477 }
478 } else {
479 $S->_Doc()->read($S->_pps(),\$buf,$o,$lmax);
480 if ( length($buf)<$lmin ) {
481 $buf = "";
482 }
483 }
484 return $buf;
485 }
486  
487 sub _fLen {
488 my ( $S ) = @_;
489 if ( !$S->_pps() ) {
490 return -s $S->_fileName();
491 } else {
492 return $S->_Doc() -> size ( $S->_pps() );
493 }
494 }
495  
496 "Atomkraft? Nein, danke!"
497  
498 __END__