1 |
#!/opt/bin/perl |
2 |
|
3 |
use common::sense; |
4 |
use Getopt::Long; |
5 |
use Digest::MD5; |
6 |
use Urlader; |
7 |
|
8 |
my $verbose = 1; |
9 |
|
10 |
my $URLADER; |
11 |
my $OUTPUT; |
12 |
my $WINDOWS_ICON; |
13 |
my $MODE = "pack"; |
14 |
|
15 |
Getopt::Long::Configure ("bundling", "no_ignore_case"); |
16 |
GetOptions( |
17 |
"verbose|v" => sub { |
18 |
$verbose++; |
19 |
}, |
20 |
"quiet|q" => sub { |
21 |
$verbose = 0; |
22 |
}, |
23 |
"urlader=s" => \$URLADER, |
24 |
"output|o=s" => \$OUTPUT, |
25 |
"windows-icon=s" => \$WINDOWS_ICON, |
26 |
"pack" => sub { $MODE = "pack" }, |
27 |
"help|h" => sub { |
28 |
require Pod::Usage; |
29 |
Pod::Usage::pod2usage (-verbose => 1, -exitval => 0, -noperldoc => 1); |
30 |
}, |
31 |
) or exit 1; |
32 |
|
33 |
############################################################################# |
34 |
|
35 |
my $out = *STDOUT; |
36 |
|
37 |
if (length $OUTPUT) { |
38 |
undef $out; |
39 |
open $out, ">:raw:perlio", $OUTPUT |
40 |
or die "$OUTPUT: $!"; |
41 |
} |
42 |
|
43 |
############################################################################# |
44 |
# for now, $MODE is --pack |
45 |
|
46 |
@ARGV >= 4 |
47 |
or die "not enough arguments for --pack.\n"; |
48 |
|
49 |
############################################################################# |
50 |
|
51 |
my $urlader_md5 = "\x00" x 16; |
52 |
|
53 |
if (length $URLADER) { |
54 |
open my $fh, "<:raw:perlio", $URLADER |
55 |
or die "$URLADER: $!"; |
56 |
my $urlader = do { local $/; <$fh> }; |
57 |
|
58 |
if (length $WINDOWS_ICON) { |
59 |
require Win32::Exe; |
60 |
if (my $exe = eval { Win32::Exe->new (\$urlader) }) { |
61 |
$exe->set_icons ([Win32::Exe::IconFile->new ($WINDOWS_ICON)->icons]); |
62 |
$exe->write (\$urlader); |
63 |
} else { |
64 |
print "unable to set icon ($@)\n" if $verbose >= 2; |
65 |
} |
66 |
} |
67 |
|
68 |
$urlader_md5 = Digest::MD5::md5 $urlader; |
69 |
|
70 |
syswrite $out, $urlader; |
71 |
} |
72 |
|
73 |
############################################################################# |
74 |
|
75 |
my $exe_id = shift; |
76 |
my $exe_ver = shift; |
77 |
my $dir = shift; |
78 |
|
79 |
my $size; |
80 |
my $max_uncomp; |
81 |
my $datasize; |
82 |
|
83 |
my $md5 = new Digest::MD5; |
84 |
|
85 |
sub wr { |
86 |
syswrite $out, $_[0]; |
87 |
$size += length $_[0]; |
88 |
$md5->add ($_[0]); |
89 |
} |
90 |
|
91 |
sub ent { |
92 |
my ($type, $flags, $name, $data) = @_; |
93 |
|
94 |
warn "add record type $type, path $name\n" if $verbose >= 2; |
95 |
|
96 |
my $ent = pack "CCnN Z* a*", |
97 |
$type, |
98 |
$flags, |
99 |
(length $name), |
100 |
(length $data), |
101 |
$name, |
102 |
$data; |
103 |
|
104 |
wr $ent; |
105 |
} |
106 |
|
107 |
sub scandir { |
108 |
my ($path, $pfx) = @_; |
109 |
|
110 |
$pfx =~ s%^/%%; |
111 |
|
112 |
opendir my $fh, $path |
113 |
or die "$path: $!"; |
114 |
|
115 |
for my $file (sort readdir $fh) { |
116 |
next if $file eq "." || $file eq ".."; |
117 |
|
118 |
lstat "$path/$file" |
119 |
or die "$path/$file: $!"; |
120 |
|
121 |
if (-d _) { |
122 |
ent Urlader::T_DIR, 0, "$pfx$file"; |
123 |
&scandir ("$path/$file", "$pfx$file/"); |
124 |
} elsif (-f _) { |
125 |
my $len = -s _; |
126 |
|
127 |
$datasize += $len; |
128 |
$max_uncomp = $len if $max_uncomp < $len; |
129 |
|
130 |
open my $fh2, "<:raw:perlio", "$path/$file" |
131 |
or die "$path/$file: $!"; |
132 |
|
133 |
$len == sysread $fh2, my $data, $len |
134 |
or die "$path/$file: read error"; |
135 |
|
136 |
my $flags = (-x _) ? Urlader::F_EXEC : 0; |
137 |
|
138 |
if (Urlader::lzf_compress $data) { |
139 |
$flags |= Urlader::F_LZF; |
140 |
} |
141 |
|
142 |
ent Urlader::T_FILE, $flags, "$pfx$file", $data; |
143 |
} else { |
144 |
warn "$path/$file: unsupported filetype, skipping.\n"; |
145 |
} |
146 |
} |
147 |
} |
148 |
|
149 |
ent Urlader::T_META, 0, $exe_id, "$exe_ver\x00"; |
150 |
|
151 |
shift, ent Urlader::T_ENV, 0, $1, "$2\x00" |
152 |
while $ARGV[0] =~ /^([^=]+)=(.*)$/s; |
153 |
|
154 |
ent Urlader::T_ARG, 0, shift |
155 |
while @ARGV; |
156 |
|
157 |
scandir $dir, ""; |
158 |
|
159 |
ent Urlader::T_NULL, 0; |
160 |
|
161 |
wr pack "NN x8 a16", |
162 |
$max_uncomp, |
163 |
$size + 4 + 4 + 8 + 16 + 16 + 16, # sizeof this pack + md5s, ugly |
164 |
Urlader::TAIL_MAGIC; |
165 |
|
166 |
wr $urlader_md5; |
167 |
wr $md5->digest; |
168 |
|
169 |
printf STDERR "%d bytes written (file data before compression: %d, biggest file %d bytes)\n", $size, $datasize, $max_uncomp |
170 |
if $verbose; |
171 |
|
172 |
exit 0; |
173 |
|
174 |
=head1 NAME |
175 |
|
176 |
urlader-util - generate new urladers, maybe other stuff |
177 |
|
178 |
=head1 SYNOPSIS |
179 |
|
180 |
urlader-util --urlader myurlader.exe --windows-icon myicon.ico \ |
181 |
--pack exe_id exe_ver directory \ |
182 |
LD_LIBRARY_PATH=. ./perl run >myprog.exe |
183 |
|
184 |
=head1 DESCRIPTION |
185 |
|
186 |
Urlader utility program - see the L<Urlader> module manpage for more info. |
187 |
|
188 |
=head1 OPTIONS |
189 |
|
190 |
=head2 SWITCHES |
191 |
|
192 |
=over 4 |
193 |
|
194 |
=item --help |
195 |
|
196 |
Display this manual page. |
197 |
|
198 |
=item -v, --verbose |
199 |
|
200 |
Increase verbosity (the default verbosity level is 1). |
201 |
|
202 |
=item -q, --quiet |
203 |
|
204 |
Set verbosity to zero, that is, only errors and warnings will be printed. |
205 |
|
206 |
=item -o, --output <path> |
207 |
|
208 |
Write output (e.g. the generated executable) to the given path instead of |
209 |
to standard output. |
210 |
|
211 |
=item --urlader <path> |
212 |
|
213 |
Use the given file as urlader. What this means depends on the execution mode. |
214 |
|
215 |
=item --windows-icon <path> |
216 |
|
217 |
If the urlader executable is a windows executable, patch it with this |
218 |
icon resource (which must be in .ICO format). For other executables, this |
219 |
switch is ignored. |
220 |
|
221 |
=back |
222 |
|
223 |
=head2 MODES |
224 |
|
225 |
=over 4 |
226 |
|
227 |
=item --pack <exe_id> <exe_ver> <path> [env-vars...] <program> [arguments...] |
228 |
|
229 |
Packs the given directory tree, env-variable assignments, program path |
230 |
and arguments into an urlader archive and optionally prepend the urlader |
231 |
binary (see C<--urlader>) and output the whole blob. |
232 |
|
233 |
See the L<Urlader> manual page for an explanation of C<exe_id>, C<exe_ver> |
234 |
and other concepts. |
235 |
|
236 |
=back |
237 |
|
238 |
=head1 AUTHOR |
239 |
|
240 |
Marc Lehmann <schmorp@schmorp.de> |
241 |
http://home.schmorp.de/ |
242 |
|
243 |
=head1 SEE ALSO |
244 |
|
245 |
L<Urlader>, L<PAR> - for more features and easier usage. |
246 |
|
247 |
=cut |