ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Urlader/urlader.c
(Generate patch)

Comparing Urlader/urlader.c (file contents):
Revision 1.11 by root, Thu Jan 5 06:00:13 2012 UTC vs.
Revision 1.13 by root, Tue Jan 17 18:38:37 2012 UTC

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>
14static u_handle pack_handle; 50static u_handle pack_handle;
15static u_handle lock_handle; 51static u_handle lock_handle;
16static char tmppath[MAX_PATH]; 52static char tmppath[MAX_PATH];
17 53
18static int exe_argc; 54static int exe_argc;
19static const char *exe_argv[MAX_ARGC]; 55static u_dynbuf exe_argv;
20static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */ 56static u_dynbuf exe_args; /* actual arguments strings copied here */
21static unsigned int exe_argo;
22 57
23static void 58static void
24tmpdir (const char *dir) 59tmpdir (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
76static char *pack_base, *pack_end; 111static char *pack_base, *pack_end;
112static struct u_pack_tail *pack_tail;
77static struct u_pack_hdr *pack_cur; 113static struct u_pack_hdr *pack_cur;
78static char *scratch; 114static char *scratch;
79static unsigned int scratch_size; 115static 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
151static void 188static void
152exe_info (void) 189add_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
163static void 195static void
164load (void) 196load (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)
176 u_setenv ("URLADER_OVERRIDE", "override"); 216 u_setenv ("URLADER_OVERRIDE", "override");
177 else 217 else
178 { 218 {
219 pack_unmap ();
179 pack_handle = oh; 220 pack_handle = oh;
180 oh = h; 221 oh = h;
222 pack_map ();
181 } 223 }
182 224
183 u_close (oh); 225 u_close (oh);
184 } 226 }
185
186 if (pack_cur->type != T_META)
187 u_fatal ("unable to locate override metadata");
188 227
189 strcpy (exe_ver, PACK_DATA); 228 strcpy (exe_ver, PACK_DATA);
190 u_set_exe_info (); 229 u_set_exe_info ();
191 pack_next (); 230 pack_next ();
192 231
193 for (;;) 232 for (;;)
194 { 233 {
195 if (pack_cur->type == T_ENV) 234 if (pack_cur->type == T_ENV)
196 u_setenv (PACK_NAME, PACK_DATA); 235 u_setenv (PACK_NAME, PACK_DATA);
197 else if (pack_cur->type == T_ARG) 236 else if (pack_cur->type == T_ARG)
198 { 237 add_arg (PACK_NAME, u_16 (pack_cur->namelen) + 1);
199 int len = u_16 (pack_cur->namelen) + 1;
200 exe_argv [exe_argc++] = exe_args + exe_argo;
201 memcpy (exe_args + exe_argo, PACK_NAME, len);
202 exe_argo += len;
203 }
204 else 238 else
205 break; 239 break;
206 240
207 pack_next (); 241 pack_next ();
208 } 242 }
295} 329}
296 330
297static void 331static void
298execute (void) 332execute (void)
299{ 333{
300 exe_argv [exe_argc] = 0; 334 char *null = 0;
301 systemv (exe_argv); 335 u_dynbuf_append (&exe_argv, &null, sizeof (null));
336 systemv ((const char *const *)exe_argv.addr);
302} 337}
303 338
339// this argc/argv is without argv [0]
304static void 340static void
305doit (void) 341doit (int argc, char *argv[])
306{ 342{
343 int i;
344
345 u_setenv ("URLADER_CURRDIR", currdir);
346
307 u_set_datadir (); 347 u_set_datadir ();
308 u_mkdir (datadir); 348 u_mkdir (datadir);
309 349
310 exe_info (); 350 if (!pack_map ())
311 u_set_exe_info (); 351 u_fatal ("unable to map pack file - executable corrupted?");
312 352
313 load (); 353 load ();
354
355 while (argc--)
356 {
357 add_arg (*argv, strlen (*argv) + 1);
358 ++argv;
359 }
360
314 execute (); 361 execute ();
315} 362}
316 363
317#ifdef _WIN32 364#ifdef _WIN32
318 365
319int APIENTRY 366int APIENTRY
320WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) 367WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show)
321{ 368{
322 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) 369 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath)))
323 u_fatal ("unable to find executable pack"); 370 u_fatal ("unable to find executable pack");
371
372 u_setenv ("URLADER_EXEPATH", tmppath);
324 373
325 pack_handle = u_open (tmppath); 374 pack_handle = u_open (tmppath);
326 if (!u_valid (pack_handle)) 375 if (!u_valid (pack_handle))
327 u_fatal ("unable to open executable pack"); 376 u_fatal ("unable to open executable pack");
328 377
329 if (!GetCurrentDirectory (sizeof (currdir), currdir)) 378 if (!GetCurrentDirectory (sizeof (currdir), currdir))
330 strcpy (currdir, "."); 379 strcpy (currdir, ".");
331 380
332 doit (); 381 doit (1, &argv);
333 382
334 return 0; 383 return 0;
335} 384}
336 385
337#else 386#else
338 387
339int 388int
340main (int argc, char *argv[]) 389main (int argc, char *argv[])
341{ 390{
391 u_setenv ("URLADER_EXEPATH", argv [0]);
392
342 pack_handle = u_open (argv [0]); 393 pack_handle = u_open (argv [0]);
343 if (!u_valid (pack_handle)) 394 if (!u_valid (pack_handle))
344 u_fatal ("unable to open executable pack"); 395 u_fatal ("unable to open executable pack");
345 396
346 if (!getcwd (currdir, sizeof (currdir))) 397 if (!getcwd (currdir, sizeof (currdir)))
353 strcpy (tmppath, "default"); 404 strcpy (tmppath, "default");
354 405
355 u_append (datadir, tmppath); 406 u_append (datadir, tmppath);
356#endif 407#endif
357 408
358 doit (); 409 doit (argc - 1, argv + 1);
359 410
360 return 0; 411 return 0;
361} 412}
362 413
363#endif 414#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines