|
|
1 | /* |
|
|
2 | * Copyright (c) 2012 Marc Alexander Lehmann <schmorp@schmorp.de> |
|
|
3 | * |
|
|
4 | * Redistribution and use in source and binary forms, with or without modifica- |
|
|
5 | * tion, are permitted provided that the following conditions are met: |
|
|
6 | * |
|
|
7 | * 1. Redistributions of source code must retain the above copyright notice, |
|
|
8 | * this list of conditions and the following disclaimer. |
|
|
9 | * |
|
|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
|
|
11 | * notice, this list of conditions and the following disclaimer in the |
|
|
12 | * documentation and/or other materials provided with the distribution. |
|
|
13 | * |
|
|
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
|
15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
|
|
16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
|
17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
|
|
18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
|
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
|
20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
|
21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
|
|
22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|
|
23 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
24 | * |
|
|
25 | * Alternatively, the contents of this file may be used under the terms of |
|
|
26 | * the GNU General Public License ("GPL") version 2 or any later version, |
|
|
27 | * in which case the provisions of the GPL are applicable instead of |
|
|
28 | * the above. If you wish to allow the use of your version of this file |
|
|
29 | * only under the terms of the GPL and not to allow others to use your |
|
|
30 | * version of this file under the BSD license, indicate your decision |
|
|
31 | * by deleting the provisions above and replace them with the notice |
|
|
32 | * and other provisions required by the GPL. If you do not delete the |
|
|
33 | * provisions above, a recipient may use your version of this file under |
|
|
34 | * either the BSD or the GPL. |
|
|
35 | */ |
|
|
36 | |
1 | #include "urlib.h" |
37 | #include "urlib.h" |
2 | #include "urlib.c" |
38 | #include "urlib.c" |
3 | |
39 | |
4 | #include <stdio.h> |
40 | #include <stdio.h> |
5 | #include <stdlib.h> |
41 | #include <stdlib.h> |
… | |
… | |
14 | static u_handle pack_handle; |
50 | static u_handle pack_handle; |
15 | static u_handle lock_handle; |
51 | static u_handle lock_handle; |
16 | static char tmppath[MAX_PATH]; |
52 | static char tmppath[MAX_PATH]; |
17 | |
53 | |
18 | static int exe_argc; |
54 | static int exe_argc; |
19 | static const char *exe_argv[MAX_ARGC]; |
55 | static u_dynbuf exe_argv; |
20 | static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */ |
56 | static u_dynbuf exe_args; /* actual arguments strings copied here */ |
21 | static unsigned int exe_argo; |
|
|
22 | |
57 | |
23 | static void |
58 | static void |
24 | tmpdir (const char *dir) |
59 | tmpdir (const char *dir) |
25 | { |
60 | { |
26 | static int cnt; |
61 | static int cnt; |
… | |
… | |
72 | #endif |
107 | #endif |
73 | systemv (argv); |
108 | systemv (argv); |
74 | } |
109 | } |
75 | |
110 | |
76 | static char *pack_base, *pack_end; |
111 | static char *pack_base, *pack_end; |
|
|
112 | static struct u_pack_tail *pack_tail; |
77 | static struct u_pack_hdr *pack_cur; |
113 | static struct u_pack_hdr *pack_cur; |
78 | static char *scratch; |
114 | static char *scratch; |
79 | static unsigned int scratch_size; |
115 | static unsigned int scratch_size; |
80 | |
116 | |
81 | #define PACK_NAME ((char *)(pack_cur + 1)) |
117 | #define PACK_NAME ((char *)(pack_cur + 1)) |
… | |
… | |
125 | |
161 | |
126 | addr = u_mmap (pack_handle, size); |
162 | addr = u_mmap (pack_handle, size); |
127 | if (!addr) |
163 | if (!addr) |
128 | return 0; |
164 | return 0; |
129 | |
165 | |
130 | pack_unmap (); |
166 | /*pack_unmap ();*/ |
131 | |
167 | |
132 | pack_base = addr; |
168 | pack_base = addr; |
133 | pack_end = pack_base + size; |
169 | pack_end = pack_base + size; |
134 | |
170 | |
135 | struct u_pack_tail *tail; |
|
|
136 | |
|
|
137 | tail = (void *)(pack_end - sizeof (*tail)); |
171 | pack_tail = (void *)(pack_end - sizeof (*pack_tail)); |
138 | |
172 | |
139 | if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) |
173 | if (memcmp (pack_tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) |
140 | return 0; |
174 | return 0; |
141 | |
175 | |
142 | pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size)); |
176 | pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (pack_tail->size)); |
143 | |
177 | |
|
|
178 | if (pack_cur->type != T_META) |
|
|
179 | return 0; |
|
|
180 | |
144 | scratch = u_malloc (scratch_size = u_32 (tail->max_uncompressed)); |
181 | scratch = u_malloc (scratch_size = u_32 (pack_tail->max_uncompressed)); |
145 | if (!scratch) |
182 | if (!scratch) |
146 | return 0; |
183 | return 0; |
147 | |
184 | |
148 | return 1; |
185 | return 1; |
149 | } |
186 | } |
150 | |
187 | |
151 | static void |
188 | static void |
152 | exe_info (void) |
189 | add_arg (char *arg, unsigned int len) |
153 | { |
190 | { |
154 | if (!pack_map ()) |
191 | char *addr = u_dynbuf_append (&exe_args, arg, len); |
155 | u_fatal ("unable to locate packfile in executable - executable corrupted?"); |
192 | u_dynbuf_append (&exe_argv, &addr, sizeof (addr)); |
156 | |
|
|
157 | if (pack_cur->type != T_META) |
|
|
158 | u_fatal ("unable to locate executable metadata - executable corrupted?"); |
|
|
159 | |
|
|
160 | strcpy (exe_id, PACK_NAME); |
|
|
161 | } |
193 | } |
162 | |
194 | |
163 | static void |
195 | static void |
164 | load (void) |
196 | load (void) |
165 | { |
197 | { |
|
|
198 | strcpy (exe_id , PACK_NAME); |
|
|
199 | strcpy (exe_ver, PACK_DATA); |
|
|
200 | u_set_exe_info (); |
|
|
201 | |
166 | if (u_chdir (exe_dir)) |
202 | if (u_chdir (exe_dir)) |
167 | u_fatal ("unable to change to application data directory"); |
203 | u_fatal ("unable to change to application data directory"); |
168 | |
204 | |
169 | u_handle h = u_open ("override"); |
205 | u_handle h = u_open ("override"); |
170 | if (u_valid (h)) |
206 | if (u_valid (h)) |
171 | { |
207 | { |
172 | u_handle oh = pack_handle; |
208 | u_handle oh = pack_handle; |
173 | |
209 | |
|
|
210 | pack_unmap (); |
174 | pack_handle = h; |
211 | pack_handle = h; |
|
|
212 | |
175 | if (!pack_map ()) |
213 | if (pack_map () |
|
|
214 | && strcmp (exe_id , PACK_NAME) == 0 |
|
|
215 | && strcmp (exe_ver, PACK_DATA) <= 0) |
|
|
216 | u_setenv ("URLADER_OVERRIDE", "override"); |
|
|
217 | else |
176 | { |
218 | { |
|
|
219 | pack_unmap (); |
177 | pack_handle = oh; |
220 | pack_handle = oh; |
178 | oh = h; |
221 | oh = h; |
|
|
222 | pack_map (); |
179 | } |
223 | } |
180 | |
224 | |
181 | u_close (oh); |
225 | u_close (oh); |
182 | } |
226 | } |
183 | |
|
|
184 | if (pack_cur->type != T_META) |
|
|
185 | u_fatal ("unable to locate override metadata"); |
|
|
186 | |
227 | |
187 | strcpy (exe_ver, PACK_DATA); |
228 | strcpy (exe_ver, PACK_DATA); |
188 | u_set_exe_info (); |
229 | u_set_exe_info (); |
189 | pack_next (); |
230 | pack_next (); |
190 | |
231 | |
191 | for (;;) |
232 | for (;;) |
192 | { |
233 | { |
193 | if (pack_cur->type == T_ENV) |
234 | if (pack_cur->type == T_ENV) |
194 | u_setenv (PACK_NAME, PACK_DATA); |
235 | u_setenv (PACK_NAME, PACK_DATA); |
195 | else if (pack_cur->type == T_ARG) |
236 | else if (pack_cur->type == T_ARG) |
196 | { |
237 | add_arg (PACK_NAME, u_16 (pack_cur->namelen) + 1); |
197 | int len = u_16 (pack_cur->namelen) + 1; |
|
|
198 | exe_argv [exe_argc++] = exe_args + exe_argo; |
|
|
199 | memcpy (exe_args + exe_argo, PACK_NAME, len); |
|
|
200 | exe_argo += len; |
|
|
201 | } |
|
|
202 | else |
238 | else |
203 | break; |
239 | break; |
204 | |
240 | |
205 | pack_next (); |
241 | pack_next (); |
206 | } |
242 | } |
… | |
… | |
293 | } |
329 | } |
294 | |
330 | |
295 | static void |
331 | static void |
296 | execute (void) |
332 | execute (void) |
297 | { |
333 | { |
298 | exe_argv [exe_argc] = 0; |
334 | char *null = 0; |
299 | systemv (exe_argv); |
335 | u_dynbuf_append (&exe_argv, &null, sizeof (null)); |
|
|
336 | systemv ((const char *const *)exe_argv.addr); |
300 | } |
337 | } |
301 | |
338 | |
|
|
339 | // this argc/argv is without argv [0] |
302 | static void |
340 | static void |
303 | doit (void) |
341 | doit (int argc, char *argv[]) |
304 | { |
342 | { |
|
|
343 | int i; |
|
|
344 | |
|
|
345 | u_setenv ("URLADER_CURRDIR", currdir); |
|
|
346 | |
305 | u_set_datadir (); |
347 | u_set_datadir (); |
306 | u_mkdir (datadir); |
348 | u_mkdir (datadir); |
307 | |
349 | |
308 | exe_info (); |
350 | if (!pack_map ()) |
309 | u_set_exe_info (); |
351 | u_fatal ("unable to map pack file - executable corrupted?"); |
310 | |
352 | |
311 | load (); |
353 | load (); |
|
|
354 | |
|
|
355 | while (argc--) |
|
|
356 | { |
|
|
357 | add_arg (*argv, strlen (*argv) + 1); |
|
|
358 | ++argv; |
|
|
359 | } |
|
|
360 | |
312 | execute (); |
361 | execute (); |
313 | } |
362 | } |
314 | |
363 | |
315 | #ifdef _WIN32 |
364 | #ifdef _WIN32 |
316 | |
365 | |
317 | int APIENTRY |
366 | int APIENTRY |
318 | WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) |
367 | WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) |
319 | { |
368 | { |
320 | if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) |
369 | if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) |
321 | u_fatal ("unable to find executable pack"); |
370 | u_fatal ("unable to find executable pack"); |
|
|
371 | |
|
|
372 | u_setenv ("URLADER_EXEPATH", tmppath); |
322 | |
373 | |
323 | pack_handle = u_open (tmppath); |
374 | pack_handle = u_open (tmppath); |
324 | if (!u_valid (pack_handle)) |
375 | if (!u_valid (pack_handle)) |
325 | u_fatal ("unable to open executable pack"); |
376 | u_fatal ("unable to open executable pack"); |
326 | |
377 | |
327 | if (!GetCurrentDirectory (sizeof (currdir), currdir)) |
378 | if (!GetCurrentDirectory (sizeof (currdir), currdir)) |
328 | strcpy (currdir, "."); |
379 | strcpy (currdir, "."); |
329 | |
380 | |
|
|
381 | doit (1, &argv); |
|
|
382 | |
330 | return 0; |
383 | return 0; |
331 | } |
384 | } |
332 | |
385 | |
333 | #else |
386 | #else |
334 | |
387 | |
335 | int |
388 | int |
336 | main (int argc, char *argv[]) |
389 | main (int argc, char *argv[]) |
337 | { |
390 | { |
338 | pack_handle = u_open (argv [0]); |
|
|
339 | if (!u_valid (pack_handle)) |
|
|
340 | u_fatal ("unable to open executable pack"); |
|
|
341 | |
|
|
342 | if (!getcwd (currdir, sizeof (currdir))) |
391 | if (!getcwd (currdir, sizeof (currdir))) |
343 | strcpy (currdir, "."); |
392 | strcpy (currdir, "."); |
344 | |
393 | |
|
|
394 | { |
|
|
395 | const char *exe_path = 0; |
|
|
396 | |
|
|
397 | if (strchr (argv [0], '/')) |
|
|
398 | exe_path = argv [0]; |
|
|
399 | else |
|
|
400 | { |
|
|
401 | const char *p, *path = getenv ("PATH"); |
|
|
402 | |
|
|
403 | if (!path) |
|
|
404 | u_fatal ("unable to find executable, try running with full path."); |
|
|
405 | |
|
|
406 | for (p = path; ; ) |
|
|
407 | { |
|
|
408 | const char *e = p; |
|
|
409 | int l; |
|
|
410 | |
|
|
411 | while (*e && *e != ':') |
|
|
412 | ++e; |
|
|
413 | |
|
|
414 | l = e - p; |
|
|
415 | memcpy (tmppath, p, l); |
|
|
416 | |
|
|
417 | if (!l) |
|
|
418 | tmppath [l++] = '.'; |
|
|
419 | |
|
|
420 | tmppath [l++] = '/'; |
|
|
421 | |
|
|
422 | strcpy (tmppath + l, argv [0]); |
|
|
423 | |
|
|
424 | if (!access (tmppath, X_OK)) |
|
|
425 | break; |
|
|
426 | |
|
|
427 | p = e; |
|
|
428 | if (!*p) |
|
|
429 | u_fatal ("unable to find executable, try running with full path."); |
|
|
430 | |
|
|
431 | ++p; |
|
|
432 | } |
|
|
433 | |
|
|
434 | exe_path = tmppath; |
|
|
435 | } |
|
|
436 | |
|
|
437 | pack_handle = u_open (exe_path); |
|
|
438 | if (!u_valid (pack_handle)) |
|
|
439 | u_fatal ("unable to open executable for reading - permissions problem?"); |
|
|
440 | |
|
|
441 | u_setenv ("URLADER_EXEPATH", exe_path); |
|
|
442 | } |
|
|
443 | |
345 | #if 0 |
444 | #if 0 |
|
|
445 | /* intersperse hostname, for whatever reason */ |
|
|
446 | |
346 | if (gethostname (tmppath, sizeof (tmppath))) |
447 | if (gethostname (tmppath, sizeof (tmppath))) |
347 | strcpy (tmppath, "default"); |
448 | strcpy (tmppath, "default"); |
348 | |
449 | |
349 | u_append (datadir, tmppath); |
450 | u_append (datadir, tmppath); |
350 | #endif |
451 | #endif |
351 | |
452 | |
352 | doit (); |
453 | doit (argc - 1, argv + 1); |
353 | |
454 | |
354 | return 0; |
455 | return 0; |
355 | } |
456 | } |
356 | |
457 | |
357 | #endif |
458 | #endif |