+Microsoft files downloader/extractor/.captivemodid.xml builders.
[captive.git] / src / install / acquire / captivemodid-print.pl
1 #! /usr/bin/perl
2 #
3 # $Id$
4 # Scan downloaded Microsoft files to build the .captivemodid.xml file core.
5 # Copyright (C) 2005 Jan Kratochvil <project-captive@jankratochvil.net>
6
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
10
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.
15
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
19
20
21 use strict;
22 use warnings;
23
24 use Cwd;
25 use Carp qw(confess cluck);
26 use Digest::MD5 qw(md5_hex);
27 use File::Remove qw(remove);
28 use Getopt::Long;
29 use Data::Dumper;
30
31
32 my $tmp="/tmp/captivemodid";
33 my $out;
34
35
36 my $opt_quiet;
37 die if !GetOptions(
38                 "q|quiet+",\$opt_quiet,
39                 );
40
41
42 our $dirnew=$ARGV[0];
43 do { $dirnew="."; warn "Defaulting ARGV[0] to: $dirnew"; } if !$dirnew;
44 our $out=$ARGV[1];
45 warn "No output md5 directory, not copying." if !$out;
46
47
48 my %type=(
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",
55         "ntfs.sys"    =>"NTFS",
56         );
57
58 my %lang=(
59         "ar"=>"Arabic",
60         "hk"=>"Chinese (Hong Kong SAR)",
61         "cn"=>"Chinese (Simplified)",
62         "tw"=>"Chinese (Traditional)",
63         "cs"=>"Czech",
64         "da"=>"Danish",
65         "nl"=>"Dutch",
66         "en"=>"English",
67         "fi"=>"Finnish",
68         "fr"=>"French",
69         "de"=>"German",
70         "el"=>"Greek",
71         "he"=>"Hebrew",
72         "hu"=>"Hungarian",
73         "it"=>"Italian",
74         "ja"=>"Japanese",
75         "ko"=>"Korean",
76         "no"=>"Norwegian",
77         "pl"=>"Polish",
78         "br"=>"Portugese (Brazil)",
79         "pt"=>"Portugese (Portugal)",
80         "ru"=>"Russian",
81         "es"=>"Spanish",
82         "sv"=>"Swedish",
83         "tr"=>"Turkish",
84         "ara"=>"Arabic",
85         "chh"=>"Chinese (Hong Kong SAR)",
86         "chs"=>"Chinese (Simplified)",
87         "cht"=>"Chinese (Traditional)",
88         "csy"=>"Czech",
89         "dan"=>"Danish",
90         "nld"=>"Dutch",
91         "enu"=>"English",
92         "fin"=>"Finnish",
93         "fra"=>"French",
94         "deu"=>"German",
95         "ell"=>"Greek",
96         "heb"=>"Hebrew",
97         "hun"=>"Hungarian",
98         "ita"=>"Italian",
99         "jpn"=>"Japanese",
100         "kor"=>"Korean",
101         "nor"=>"Norwegian",
102         "plk"=>"Polish",
103         "ptb"=>"Portugese (Brazil)",
104         "ptg"=>"Portugese (Portugal)",
105         "rus"=>"Russian",
106         "esn"=>"Spanish",
107         "sve"=>"Swedish",
108         "trk"=>"Turkish",
109         );
110
111
112 sub name_to_lang($)
113 {
114 my($name)=@_;
115
116         $name=~tr/-/_/;
117         $name=lc $name;
118         my $r;
119         while (my($subst,$long)=each(%lang)) {
120                 next if $name!~/[^a-z]$subst[^a-z]/;
121 # FIXME
122                 cluck "$name: $subst" if $r;
123                 $r=$long;
124                 }
125 # FIXME
126         cluck $name if !$r;
127         return $r;
128 }
129
130 my $ver_prefix="5.1.2600.";
131 my @stack_out;
132 my $STDERR_needs_eol;
133 my %md5sum_printed_global;
134
135 sub stack0_flush()
136 {
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"}}++;
145                         my $print="";
146                         $print.=<<"HERE";
147 <!-- @{[ $this->{"relname"} ]} -->
148 HERE
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"} ]}" />
152 HERE
153                         $print=~s/^/"\t" x (1+$stacki)/egm;
154                         print STDERR "\n" if $STDERR_needs_eol;
155                         $STDERR_needs_eol=0;
156                         print $print;
157                         }
158                 }
159         print "\n";
160 }
161
162 our @stack;
163 my %md5sum_stored;
164 my $last_stack0_filename;
165
166 sub check($$)
167 {
168 my($filename_unused,$basename_orig)=@_;
169
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"};
184                 my $this_name;
185                 my $type;
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";
190                         }
191                 elsif ($stacki<$#stack) {
192                         $type=$this->{"type"} or confess;
193                         $this_name="Cabinet";
194                         }
195                 else {
196                         $type=$final_type or confess;
197                         $this_name=$final_name;
198                         }
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;
203                 local *F;
204                 open F,$expanded or confess;
205                 my $buf="";
206                 my $buf2;
207                 my $ver;
208                 while (my $got=read F,$buf2,0x1000) {
209                         confess $expanded if !defined $got;
210                         last if !$got;
211                         confess $expanded if $got<0;
212                         confess $expanded if length($buf2)!=$got;
213                         $buf.=$buf2;
214                         $ver=$buf;
215                         $ver=~s/\x00\x00+/!/g;
216                         $ver=~tr/\x00//d;
217                         $ver=~tr/!/\x00/;
218                         last if $ver=~s/^.*\x00\Q$ver_prefix\E(\d+)[ \x00].*$/$1/s;
219                         $ver=undef();
220                         $buf=substr($buf,-0x100);
221                         }
222                 confess $expanded if !$ver;
223                 close F or confess;
224                 $ver=~/^\d+$/ or confess "$expanded: $ver";
225                 my $pri=sprintf "510%04u00",$ver;
226                 $pri+=90;
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/;
231                 local *F;
232                 open F,$filename or confess;
233                 my $md5obj=Digest::MD5->new();
234                 $md5obj->addfile(*F);
235                 close F or confess;
236                 my $md5sum=lc $md5obj->hexdigest();
237                 spawn("cp -p '$filename' '$out/$md5sum'") if !$md5sum_stored{$md5sum}++ && $out && $stacki==$#stack;
238
239                 my %new=(
240                         %$this,
241                         "type"=>$type,
242                         "length"=>$length,
243                         "priority"=>$pri,
244                         "md5"=>$md5sum,
245                         "dist"=>$dist,
246                         "lang"=>$lang,
247                         "ver"=>$ver,
248                         "this_name"=>$this_name,
249                         );
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{$_};
255                         }
256                 do { cluck Dumper($this,\%new) if $new{$_} ne $this->{$_}; } for keys(%$this);
257                 %$this=%new;
258                 }
259         push @stack_out,[@stack];
260 }
261
262 sub spawn($)
263 {
264 my($command)=@_;
265
266         my $code=system "($command)".' >&2';
267         confess "$code: $command" if $code;
268 }
269
270 sub rm_rf($)
271 {
272 my($dir)=@_;
273
274         return if !-e $dir;
275         # Do not: Can't remove directory xyzzy: Directory not empty
276         #         -r--r--r-- xyzzy
277         #         remove \1,$dir or confess "$dir: $!";
278         spawn "rm -rf '$dir'";
279 }
280
281 sub mkdir_checked
282 {
283 my(@dirs)=@_;
284
285         for (@dirs) {
286                 mkdir $_ or confess "$_: $!";
287                 }
288 }
289
290
291 our $depth=-1;
292
293 sub prep()
294 {
295         confess if $dirnew;
296         $dirnew="$tmp/$depth";
297         chdir "/" or confess;
298         rm_rf $dirnew;
299         mkdir_checked $dirnew;
300         chdir $dirnew or confess "chdir: $dirnew";
301         return $dirnew;
302 }
303
304 sub relname($)
305 {
306 my($filename)=@_;
307
308         return $filename if $filename=~s{^\Q$tmp\E/\d+/}{};
309         (my $basename=$filename)=~s{^.*/}{} or confess;
310         return $basename;
311 }
312
313 my %unknown;
314 sub process();
315 sub process()
316 {
317         my $dirname=$dirnew or confess;
318         $dirnew=undef();
319         local $depth=$depth+1;
320         my @stack_orig=@stack;
321
322         local *FIND;
323         open FIND,"find '$dirname' -type f|sort|" or confess $dirname;
324         local $_;
325         while (<FIND>) {
326                 next if $_ eq "";
327                 if (!$depth) {
328                         print STDERR ".";
329                         $STDERR_needs_eol=1;
330                         }
331                 chomp;
332                 my $filename=$_;
333                 $filename=~m{^/} or confess $filename;
334                 (my $basename=$filename)=~s{^.*/}{} or confess;
335                 -r $filename or do { cluck $filename; system("ls -l '$filename'"); };
336                 my $this={
337                         "filename"=>$filename,
338                         "relname"=>relname($filename),
339                         };
340                 @stack=(@stack_orig,$this);
341
342                 check $filename,$basename;
343
344                 local *MIME;
345                 open MIME,"file -b -i '$filename'|" or confess;
346                 my $mime=do { local $/; <MIME> or confess; };
347                 close MIME or confess;
348                 chomp $mime;
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';
354
355                 local *ID;
356                 open ID,"file -b '$filename'|" or confess;
357                 my $id=do { local $/; <ID> or confess; };
358                 close ID or confess;
359                 chomp $id;
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?
395
396                 # Do not:
397                 #next if $id=~/^PE executable for MS Windows .* 32-bit$/;       # TODO
398
399                 if ($id eq 'PE executable for MS Windows (GUI) Intel 80386 32-bit, InnoSetup self-extracting archive') {
400                         # FIXME: http://innounp.sourceforge.net/
401                         next;
402                         }
403                 if (0
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$/
408                                 ) {
409                         $this->{"type"}="cabinet";
410                         prep();
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},
414                                         q{checksum error},
415                                         q{error in CAB data format},
416                                         ).q{\)$';true};
417                         process();
418                         next;
419                         }
420                 # non-"32-bit" (amd64 in fact)
421                 next if $id=~/^PE executable for MS Windows/;
422                 if (0
423                                 || $id eq 'PE executable for MS Windows (GUI) Intel 80386 32-bit, ZIP self-extracting archive (WinZip)'
424                                 || $id=~/^Zip archive data, /
425                                 ) {
426                         prep();
427                         spawn "unzip -q '$filename'";
428                         process();
429                         next;
430                         }
431                 if ($id=~m{^gzip compressed data, was "\S+", from Win/32}) {
432                         prep();
433                         spawn "gzip -d <'$filename' >'$basename'";
434                         process();
435                         next;
436                         }
437                 if ($id eq 'tar archive') {
438                         prep();
439                         spawn "tar xf '$filename'";
440                         process();
441                         next;
442                         }
443                 warn "$filename: $mime - $id" if !$unknown{$mime,$id}++;
444                 }
445         close FIND or confess;
446         $dirnew=undef();
447 }
448
449
450 $dirnew=getcwd()."/".$dirnew if $dirnew!~m{^/};
451 $out   =getcwd()."/".$out    if $out && $out!~m{^/};
452 rm_rf $tmp;
453 mkdir_checked $tmp;
454 if ($out) {
455         rm_rf $out;
456         mkdir_checked $out;
457         }
458 process;
459 stack0_flush() if $last_stack0_filename;
460 print STDERR "\n" if $STDERR_needs_eol;
461 $STDERR_needs_eol=0;