4 # Scan downloaded Microsoft files to build the .captivemodid.xml file core.
5 # Copyright (C) 2005 Jan Kratochvil <project-captive@jankratochvil.net>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; exactly version 2 of June 1991 is required
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 use Carp qw(confess cluck);
26 use Digest::MD5 qw(md5_hex);
27 use File::Remove qw(remove);
32 my $tmp="/tmp/captivemodid";
38 "q|quiet+",\$opt_quiet,
43 do { $dirnew="."; warn "Defaulting ARGV[0] to: $dirnew"; } if !$dirnew;
45 warn "No output md5 directory, not copying." if !$out;
49 "ntoskrnl.exe"=>"Kernel",
50 "ntkrnlpa.exe"=>"Kernel PA",
51 "ntkrnlmp.exe"=>"Kernel SMP",
52 "ntkrpamp.exe"=>"Kernel SMP PA",
53 "cdfs.sys" =>"CD-ROM/iso-9660",
54 "fastfat.sys" =>"FastFAT/vfat",
60 "hk"=>"Chinese (Hong Kong SAR)",
61 "cn"=>"Chinese (Simplified)",
62 "tw"=>"Chinese (Traditional)",
78 "br"=>"Portugese (Brazil)",
79 "pt"=>"Portugese (Portugal)",
85 "chh"=>"Chinese (Hong Kong SAR)",
86 "chs"=>"Chinese (Simplified)",
87 "cht"=>"Chinese (Traditional)",
103 "ptb"=>"Portugese (Brazil)",
104 "ptg"=>"Portugese (Portugal)",
119 while (my($subst,$long)=each(%lang)) {
120 next if $name!~/[^a-z]$subst[^a-z]/;
122 cluck "$name: $subst" if $r;
130 my $ver_prefix="5.1.2600.";
132 my $STDERR_needs_eol;
133 my %md5sum_printed_global;
137 return if $opt_quiet;
138 # FIXME: Proper nesting, not just these 2 levels:
139 my %md5sum_printed_local;
140 while (my $stack=shift @stack_out) {
141 for my $stacki (0..$#$stack) {
142 my $this=$stack->[$stacki];
143 next if $md5sum_printed_local{$this->{"md5"}}++;
144 my $dupe_global=$md5sum_printed_global{$this->{"md5"}}++;
147 <!-- @{[ $this->{"relname"} ]} -->
149 $print.=<<"HERE" if !$dupe_global;
150 <module type="@{[ $this->{"type"} ]}" length="@{[ $this->{"length"} ]}" priority="@{[ $this->{"priority"} ]}" md5="@{[ $this->{"md5"} ]}"
151 id="MS-Windows XP @{[ $this->{"dist"} ]} @{[ $this->{"lang"} ]} $ver_prefix@{[ $this->{"ver"} ]} @{[ $this->{"this_name"} ]}" />
153 $print=~s/^/"\t" x (1+$stacki)/egm;
154 print STDERR "\n" if $STDERR_needs_eol;
164 my $last_stack0_filename;
168 my($filename_unused,$basename_orig)=@_;
170 return if !$type{lc $basename_orig};
171 my $final_type=$basename_orig;
172 my $final_name=$type{$final_type} or confess;
173 $final_type="ntoskrnl.exe" if $final_type=~/^nt.*[.]exe$/;
174 (my $basename_orig_=$basename_orig)=~s/.$/_/;
175 (my $basename0=$stack[0]->{"filename"})=~s{^.*/}{} or confess;
176 stack0_flush() if $last_stack0_filename && $last_stack0_filename ne $stack[0]->{"filename"};
177 $last_stack0_filename=$stack[0]->{"filename"};
178 for my $stacki (0..$#stack) {
179 my $this=$stack[$stacki];
180 my $filename=$this->{"filename"} or confess;
181 my $relname=$this->{"relname"} or confess;
182 (my $basename=$filename)=~s{^.*/}{} or confess;
183 my $expanded=$stack[$#stack]->{"filename"};
186 if ($stacki==$#stack-1 && $basename eq $basename_orig_) {
187 $this->{"type"} eq "cabinet" or confess;
188 $type=$this->{"type"} or confess;
189 $this_name="$final_name Cabinet";
191 elsif ($stacki<$#stack) {
192 $type=$this->{"type"} or confess;
193 $this_name="Cabinet";
196 $type=$final_type or confess;
197 $this_name=$final_name;
199 my $length=(stat $filename)[7] or confess;
200 my $dist=$stack[0]->{"filename"};
201 $dist=~s{^.*/}{} or confess;
202 my $lang=name_to_lang $basename0;
204 open F,$expanded or confess;
208 while (my $got=read F,$buf2,0x1000) {
209 confess $expanded if !defined $got;
211 confess $expanded if $got<0;
212 confess $expanded if length($buf2)!=$got;
215 $ver=~s/\x00\x00+/!/g;
218 last if $ver=~s/^.*\x00\Q$ver_prefix\E(\d+)[ \x00].*$/$1/s;
220 $buf=substr($buf,-0x100);
222 confess $expanded if !$ver;
224 $ver=~/^\d+$/ or confess "$expanded: $ver";
225 my $pri=sprintf "510%04u00",$ver;
227 $pri-=10 if $lang ne "English";
228 $pri-=20 if $basename_orig=~/^ntkrnlpa/;
229 $pri-=40 if $basename_orig=~/^ntkrnlmp/;
230 $pri-=60 if $basename_orig=~/^ntkrpamp/;
232 open F,$filename or confess;
233 my $md5obj=Digest::MD5->new();
234 $md5obj->addfile(*F);
236 my $md5sum=lc $md5obj->hexdigest();
237 spawn("cp -p '$filename' '$out/$md5sum'") if !$md5sum_stored{$md5sum}++ && $out && $stacki==$#stack;
248 "this_name"=>$this_name,
250 # Highest priority/version of all the files in the cabinet:
251 for (qw(ver priority)) {
252 next if !$this->{$_};
253 $new{$_}=$this->{$_} if $new{$_}<$this->{$_};
254 delete $this->{$_} if $this->{$_}<$new{$_};
256 do { cluck Dumper($this,\%new) if $new{$_} ne $this->{$_}; } for keys(%$this);
259 push @stack_out,[@stack];
266 my $code=system "($command)".' >&2';
267 confess "$code: $command" if $code;
275 # Do not: Can't remove directory xyzzy: Directory not empty
277 # remove \1,$dir or confess "$dir: $!";
278 spawn "rm -rf '$dir'";
286 mkdir $_ or confess "$_: $!";
296 $dirnew="$tmp/$depth";
297 chdir "/" or confess;
299 mkdir_checked $dirnew;
300 chdir $dirnew or confess "chdir: $dirnew";
308 return $filename if $filename=~s{^\Q$tmp\E/\d+/}{};
309 (my $basename=$filename)=~s{^.*/}{} or confess;
317 my $dirname=$dirnew or confess;
319 local $depth=$depth+1;
320 my @stack_orig=@stack;
323 open FIND,"find '$dirname' -type f|sort|" or confess $dirname;
333 $filename=~m{^/} or confess $filename;
334 (my $basename=$filename)=~s{^.*/}{} or confess;
335 -r $filename or do { cluck $filename; system("ls -l '$filename'"); };
337 "filename"=>$filename,
338 "relname"=>relname($filename),
340 @stack=(@stack_orig,$this);
342 check $filename,$basename;
345 open MIME,"file -b -i '$filename'|" or confess;
346 my $mime=do { local $/; <MIME> or confess; };
347 close MIME or confess;
349 next if $mime=~m{^text/};
350 next if $mime=~m{^image/};
351 next if $mime=~m{^audio/};
352 next if $mime eq 'application/x-empty';
353 next if $mime eq 'application/msaccess';
356 open ID,"file -b '$filename'|" or confess;
357 my $id=do { local $/; <ID> or confess; };
360 next if $id eq 'Microsoft Office Document';
361 next if $id=~/^PDF document, version 1[.]\d+$/;
362 next if $id eq 'Rich Text Format data, version 1,';
363 next if $id=~/^x86 boot sector, /;
364 next if $id eq 'data';
365 next if $id eq 'MZ executable for MS-DOS';
366 next if $id eq 'XML document text';
367 next if $id eq 'MS Windows HtmlHelp Data';
368 next if $id eq 'MPEG sequence';
369 next if $id=~/^MSVC program database ver /;
370 next if $id eq 'Lotus 1-2-3';
371 next if $id eq 'MS Windows Help Data';
372 next if $id=~/^Macromedia Flash data, /;
373 next if $id eq 'Microsoft ASF'; # .wmv
374 next if $id eq 'GLF_BINARY_LSB_FIRST'; # .hlp
375 next if $id=~/^Sendmail frozen configuration /;
376 next if $id eq 'Assembler source';
377 next if $id=~/^DBase 3 data file/;
378 next if $id=~/^Macintosh HFS Extended version /;
379 next if $id=~/^MPEG ADTS, /; # .txt?,.hlp?
380 next if $id eq 'MS-DOS executable (COM)';
381 next if $id eq 'NE executable for MS Windows 3.x (driver)';
382 next if $id eq 'TrueType font data';
383 next if $id eq 'Windows NT registry file';
384 next if $id eq 'lif file'; # .sig?
385 next if $id=~/^Infocom game data /; # ._p?
386 next if $id=~/^Sun disk label /; # .dl_?
387 next if $id=~/^compiled Java class data, /;
388 next if $id=~/^Minix filesystem/; # .dl_?,_p?
389 next if $id eq 'DBase 3 index file'; # .nls?
390 next if $id=~/^Macintosh MFS data /; # .cp_?
391 next if $id eq 'ACB archive data'; # .cat?
392 next if $id eq 'COM executable for MS-DOS'; # .edb?
393 next if $id=~/^Bio-Rad .PIC Image File/; # ._p?
394 next if $id eq 'SysEx File - Gulbransen'; # .edb?
397 #next if $id=~/^PE executable for MS Windows .* 32-bit$/; # TODO
399 if ($id eq 'PE executable for MS Windows (GUI) Intel 80386 32-bit, InnoSetup self-extracting archive') {
400 # FIXME: http://innounp.sourceforge.net/
404 || $id=~/^Microsoft Cabinet archive data, \d+ byte(?:s)?, \d+ file(?:s)?$/
405 || $id eq 'PE executable for MS Windows (GUI) Intel 80386 32-bit, MS CAB-Installer self-extracting archive'
406 # Catch all unidentified flying cabinets:
407 || $id=~/^PE executable for MS Windows .* 32-bit$/
409 $this->{"type"}="cabinet";
411 spawn q{cabextract -q '}.$filename.q{' 2>&1|grep -v ': \(}.join(q{\|},
412 q{WARNING; possible [0-9]* extra bytes at end of file.},
413 q{no valid cabinets found},
415 q{error in CAB data format},
420 # non-"32-bit" (amd64 in fact)
421 next if $id=~/^PE executable for MS Windows/;
423 || $id eq 'PE executable for MS Windows (GUI) Intel 80386 32-bit, ZIP self-extracting archive (WinZip)'
424 || $id=~/^Zip archive data, /
427 spawn "unzip -q '$filename'";
431 if ($id=~m{^gzip compressed data, was "\S+", from Win/32}) {
433 spawn "gzip -d <'$filename' >'$basename'";
437 if ($id eq 'tar archive') {
439 spawn "tar xf '$filename'";
443 warn "$filename: $mime - $id" if !$unknown{$mime,$id}++;
445 close FIND or confess;
450 $dirnew=getcwd()."/".$dirnew if $dirnew!~m{^/};
451 $out =getcwd()."/".$out if $out && $out!~m{^/};
459 stack0_flush() if $last_stack0_filename;
460 print STDERR "\n" if $STDERR_needs_eol;