1 |
#!/opt/bin/perl |
2 |
|
3 |
use Compress::LZF; |
4 |
|
5 |
my $verbose = 1; |
6 |
|
7 |
my $exe_id = shift; |
8 |
my $exe_ver = shift; |
9 |
my $dir = shift; |
10 |
|
11 |
my $size; |
12 |
my $max; |
13 |
|
14 |
sub ent { |
15 |
my ($type, $flags, $name, $data) = @_; |
16 |
|
17 |
warn "add record type $type, path $name\n" if $verbose >= 2; |
18 |
|
19 |
my $ent = pack "CCnN Z* a*", |
20 |
$type, |
21 |
$flags, |
22 |
(length $name), |
23 |
(length $data), |
24 |
$name, |
25 |
$data; |
26 |
|
27 |
print $ent; |
28 |
|
29 |
$size += length $ent; |
30 |
} |
31 |
|
32 |
sub scandir { |
33 |
my ($path, $pfx) = @_; |
34 |
|
35 |
$pfx =~ s%^/%%; |
36 |
|
37 |
opendir my $fh, $path |
38 |
or die "$path: $!"; |
39 |
|
40 |
for my $file (sort readdir $fh) { |
41 |
next if $file eq "." || $file eq ".."; |
42 |
|
43 |
lstat "$path/$file" |
44 |
or die "$path/$file: $!"; |
45 |
|
46 |
if (-d _) { |
47 |
ent 4, 0, "$pfx$file"; |
48 |
&scandir ("$path/$file", "$pfx$file/"); |
49 |
} elsif (-f _) { |
50 |
my $len = -s _; |
51 |
|
52 |
$max = $len if $max < $len; |
53 |
|
54 |
open my $fh2, "<", "$path/$file" |
55 |
or die "$path/$file: $!"; |
56 |
|
57 |
$len == sysread $fh2, my $data, $len |
58 |
or die "$path/$file: read error"; |
59 |
|
60 |
my $flags = (-x _) ? 0x10 : 0; |
61 |
|
62 |
my $comp = Compress::LZF::compress $data; |
63 |
$comp =~ s/^(?:[\x00-\x7f]|[\x80-\xff]+)//; # remove length prefix |
64 |
|
65 |
if ($len > 1 + length $comp) { |
66 |
$flags |= 0x01; |
67 |
$data = $comp; |
68 |
} |
69 |
|
70 |
ent 5, $flags, "$pfx$file", $data; |
71 |
} else { |
72 |
warn "$path/$file: unsupported filetype, skipping.\n"; |
73 |
} |
74 |
} |
75 |
} |
76 |
|
77 |
ent 1, 0, $exe_id, "$exe_ver\x00"; |
78 |
|
79 |
shift, ent 2, 0, $1, "$2\x00" |
80 |
while $ARGV[0] =~ /^([^=]+)=(.*)$/s; |
81 |
|
82 |
ent 3, 0, shift |
83 |
while @ARGV; |
84 |
|
85 |
scandir $dir, ""; |
86 |
|
87 |
ent 0, 0; |
88 |
|
89 |
print pack "NN a12", |
90 |
$max, |
91 |
$size + 20, # sizeof this pack, sorry... |
92 |
"SCHMORPPACK0"; |
93 |
|
94 |
printf STDERR "%d bytes written, biggest single file is %d bytes.\n", $size, $max |
95 |
if $verbose; |