1 | #ifndef URLADER |
1 | #include "urlib.c" |
2 | # define URLADER "urlader" |
|
|
3 | #endif |
|
|
4 | #define URLADER_VERSION "1.0" // a decimal number, not a version string |
|
|
5 | |
|
|
6 | #define MAX_ARGC 32 |
|
|
7 | #define MAX_ARGS 256 |
|
|
8 | |
2 | |
9 | #include <stdio.h> |
3 | #include <stdio.h> |
10 | #include <stdlib.h> |
4 | #include <stdlib.h> |
11 | #include <unistd.h> |
5 | #include <unistd.h> |
12 | #include <errno.h> |
6 | #include <errno.h> |
13 | #include <string.h> |
7 | #include <string.h> |
14 | #include <time.h> |
8 | #include <time.h> |
15 | #include <fcntl.h> |
|
|
16 | |
9 | |
17 | #include "liblzf/lzf_d.c" |
10 | #include "liblzf/lzf_d.c" |
18 | |
11 | |
19 | #define TAIL_MAGIC "SCHMORPPACK0" |
12 | #include "urlib.h" |
20 | |
|
|
21 | #ifdef _WIN32 |
|
|
22 | |
|
|
23 | #include <windows.h> |
|
|
24 | //#include <winbase.h> |
|
|
25 | #include <shlobj.h> |
|
|
26 | #include <shlwapi.h> |
|
|
27 | #include <wininet.h> |
|
|
28 | |
|
|
29 | static DWORD dword; |
|
|
30 | |
|
|
31 | #define u_handle HANDLE |
|
|
32 | #define u_invalid_handle 0 |
|
|
33 | #define u_valid(handle) (!!handle) |
|
|
34 | |
|
|
35 | #define u_setenv(name,value) SetEnvironmentVariable (name, value) |
|
|
36 | #define u_mkdir(path) !CreateDirectory (path, NULL) |
|
|
37 | #define u_chdir(path) !SetCurrentDirectory (path) |
|
|
38 | #define u_rename(fr,to) !MoveFile (fr, to) |
|
|
39 | #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
|
|
40 | #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
|
|
41 | #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
|
|
42 | #define u_close(handle) CloseHandle (handle) |
|
|
43 | #define u_append(path,add) PathAppend (path, add) |
|
|
44 | #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1) |
|
|
45 | |
|
|
46 | #define u_fsync(handle) FlushFileBuffers (handle) |
|
|
47 | #define u_sync() |
|
|
48 | |
|
|
49 | #define u_lockfile(path) CreateFile (path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) |
|
|
50 | #define u_cloexec(handle) |
|
|
51 | |
|
|
52 | #else |
|
|
53 | |
|
|
54 | #define _GNU_SOURCE 1 |
|
|
55 | #define _BSD_SOURCE 1 |
|
|
56 | // the above increases our chances of getting MAP_ANONYMOUS |
|
|
57 | |
|
|
58 | #include <sys/mman.h> |
|
|
59 | #include <sys/types.h> |
|
|
60 | #include <sys/stat.h> |
|
|
61 | #include <unistd.h> |
|
|
62 | #include <pwd.h> |
|
|
63 | |
|
|
64 | #if defined (MAP_ANON) && !defined (MAP_ANONYMOUS) |
|
|
65 | #define MAP_ANONYMOUS MAP_ANON |
|
|
66 | #endif |
|
|
67 | |
|
|
68 | #ifdef PATH_MAX |
|
|
69 | #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX) |
|
|
70 | #else |
|
|
71 | #define MAX_PATH 4096 |
|
|
72 | #endif |
|
|
73 | |
|
|
74 | #define u_handle int |
|
|
75 | #define u_invalid_handle -1 |
|
|
76 | #define u_valid(fd) ((fd) >= 0) |
|
|
77 | |
|
|
78 | #define u_setenv(name,value) setenv (name, value, 1) |
|
|
79 | #define u_mkdir(path) mkdir (path, 0777) |
|
|
80 | #define u_chdir(path) chdir (path) |
|
|
81 | #define u_rename(fr,to) rename (fr, to) |
|
|
82 | #define u_open(path) open (path, O_RDONLY) |
|
|
83 | #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666) |
|
|
84 | #define u_close(handle) close (handle) |
|
|
85 | #define u_append(path,add) strcat (strcat (path, "/"), add) |
|
|
86 | #define u_write(handle,data,len) write (handle, data, len) |
|
|
87 | |
|
|
88 | // on a mostly idle system, a sync at the end is certainly faster, hope for the best |
|
|
89 | #define u_fsync(handle) |
|
|
90 | #define u_sync() sync () |
|
|
91 | |
|
|
92 | #define u_lockfile(path) open (path, O_RDWR | O_CREAT, 0666) |
|
|
93 | #define u_cloexec(handle) fcntl (handle, F_SETFD, FD_CLOEXEC) |
|
|
94 | |
|
|
95 | #endif |
|
|
96 | |
|
|
97 | #define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1]) |
|
|
98 | #define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3]) |
|
|
99 | |
13 | |
100 | /* some simple? dynamic memory management for paths might save ltos of ram */ |
14 | /* some simple? dynamic memory management for paths might save ltos of ram */ |
101 | static u_handle pack_handle; |
15 | static u_handle pack_handle; |
102 | static u_handle lock_handle; |
16 | static u_handle lock_handle; |
103 | static char tmppath[MAX_PATH]; |
17 | static char tmppath[MAX_PATH]; |
… | |
… | |
125 | #endif |
39 | #endif |
126 | |
40 | |
127 | _exit (1); |
41 | _exit (1); |
128 | } |
42 | } |
129 | |
43 | |
130 | static void * |
|
|
131 | u_malloc (unsigned int size) |
|
|
132 | { |
|
|
133 | void *addr; |
|
|
134 | |
|
|
135 | #ifdef _WIN32 |
|
|
136 | HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL); |
|
|
137 | |
|
|
138 | if (!handle) |
|
|
139 | return 0; |
|
|
140 | |
|
|
141 | addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size); |
|
|
142 | |
|
|
143 | CloseHandle (handle); |
|
|
144 | #elif defined (MAP_ANONYMOUS) |
|
|
145 | addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); |
|
|
146 | |
|
|
147 | if (addr == (void *)-1) |
|
|
148 | addr = 0; |
|
|
149 | #else |
|
|
150 | addr = malloc (size); |
|
|
151 | #endif |
|
|
152 | |
|
|
153 | return addr; |
|
|
154 | } |
|
|
155 | |
|
|
156 | static void |
|
|
157 | u_free (void *addr, unsigned int size) |
|
|
158 | { |
|
|
159 | #ifdef _WIN32 |
|
|
160 | UnmapViewOfFile (addr); |
|
|
161 | #elif defined (MAP_ANONYMOUS) |
|
|
162 | munmap (addr, size); |
|
|
163 | #else |
|
|
164 | free (addr); |
|
|
165 | #endif |
|
|
166 | } |
|
|
167 | |
|
|
168 | static void * |
|
|
169 | u_mmap (u_handle h, unsigned int size) |
|
|
170 | { |
|
|
171 | void *addr; |
|
|
172 | |
|
|
173 | #ifdef _WIN32 |
|
|
174 | HANDLE handle = CreateFileMapping (h, 0, PAGE_READONLY, 0, size, NULL); |
|
|
175 | |
|
|
176 | if (!handle) |
|
|
177 | return 0; |
|
|
178 | |
|
|
179 | addr = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, size); |
|
|
180 | |
|
|
181 | CloseHandle (handle); |
|
|
182 | #else |
|
|
183 | addr = mmap (0, size, PROT_READ, MAP_SHARED, h, 0); |
|
|
184 | |
|
|
185 | if (addr == (void *)-1) |
|
|
186 | addr = 0; |
|
|
187 | #endif |
|
|
188 | |
|
|
189 | return addr; |
|
|
190 | } |
|
|
191 | |
|
|
192 | static void |
|
|
193 | u_munmap (void *addr, unsigned int len) |
|
|
194 | { |
|
|
195 | #ifdef _WIN32 |
|
|
196 | UnmapViewOfFile (addr); |
|
|
197 | #else |
|
|
198 | munmap (addr, len); |
|
|
199 | #endif |
|
|
200 | } |
|
|
201 | |
|
|
202 | static void |
44 | static void |
203 | tmpdir (const char *dir) |
45 | tmpdir (const char *dir) |
204 | { |
46 | { |
205 | static int cnt; |
47 | static int cnt; |
206 | |
48 | |
… | |
… | |
213 | // #endif |
55 | // #endif |
214 | |
56 | |
215 | if (!u_mkdir (tmppath)) |
57 | if (!u_mkdir (tmppath)) |
216 | return; |
58 | return; |
217 | } |
59 | } |
218 | } |
|
|
219 | |
|
|
220 | static u_handle |
|
|
221 | u_lock (const char *path, int excl, int dowait) |
|
|
222 | { |
|
|
223 | u_handle h; |
|
|
224 | |
|
|
225 | h = u_lockfile (path); |
|
|
226 | if (!u_valid (h)) |
|
|
227 | return h; |
|
|
228 | |
|
|
229 | u_cloexec (h); |
|
|
230 | |
|
|
231 | for (;;) |
|
|
232 | { |
|
|
233 | int success; |
|
|
234 | |
|
|
235 | // acquire the lock |
|
|
236 | #ifdef _WIN32 |
|
|
237 | OVERLAPPED ov = { 0 }; |
|
|
238 | |
|
|
239 | success = LockFileEx (h, |
|
|
240 | (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) |
|
|
241 | | (dowait ? 0 : LOCKFILE_FAIL_IMMEDIATELY), |
|
|
242 | 0, |
|
|
243 | 1, 0, |
|
|
244 | &ov); |
|
|
245 | #else |
|
|
246 | struct flock lck = { 0 }; |
|
|
247 | |
|
|
248 | lck.l_type = excl ? F_WRLCK : F_RDLCK; |
|
|
249 | lck.l_whence = SEEK_SET; |
|
|
250 | lck.l_len = 1; |
|
|
251 | |
|
|
252 | success = !fcntl (h, dowait ? F_SETLKW : F_SETLK, &lck); |
|
|
253 | #endif |
|
|
254 | |
|
|
255 | if (!success) |
|
|
256 | break; |
|
|
257 | |
|
|
258 | // we have the lock, now verify that the lockfile still exists |
|
|
259 | |
|
|
260 | #ifdef _WIN32 |
|
|
261 | // apparently, we have to open the file to get its info :( |
|
|
262 | BY_HANDLE_FILE_INFORMATION s1, s2; |
|
|
263 | u_handle h2 = u_lockfile (path); |
|
|
264 | if (!u_valid (h)) |
|
|
265 | break; |
|
|
266 | |
|
|
267 | success = GetFileInformationByHandle (h, &s1) |
|
|
268 | && GetFileInformationByHandle (h2, &s2); |
|
|
269 | |
|
|
270 | u_close (h2); |
|
|
271 | |
|
|
272 | if (!success) |
|
|
273 | break; |
|
|
274 | |
|
|
275 | success = s1.dwVolumeSerialNumber == s2.dwVolumeSerialNumber |
|
|
276 | && s1.nFileIndexHigh == s2.nFileIndexHigh |
|
|
277 | && s1.nFileIndexLow == s2.nFileIndexLow; |
|
|
278 | #else |
|
|
279 | struct stat s1, s2; |
|
|
280 | |
|
|
281 | if (fstat (h, &s1) || stat (path, &s2)) |
|
|
282 | break; |
|
|
283 | |
|
|
284 | success = s1.st_dev == s2.st_dev |
|
|
285 | && s1.st_ino == s2.st_ino; |
|
|
286 | #endif |
|
|
287 | |
|
|
288 | if (success) |
|
|
289 | return h; // lock successfully acquired |
|
|
290 | |
|
|
291 | // files differ, close and retry - should be very rare |
|
|
292 | u_close (h); |
|
|
293 | } |
|
|
294 | |
|
|
295 | // failure |
|
|
296 | u_close (h); |
|
|
297 | return u_invalid_handle; |
|
|
298 | } |
60 | } |
299 | |
61 | |
300 | static void |
62 | static void |
301 | systemv (const char *const argv[]) |
63 | systemv (const char *const argv[]) |
302 | { |
64 | { |
… | |
… | |
330 | const char *argv[] = { "/bin/rm", "-rf", path, 0 }; |
92 | const char *argv[] = { "/bin/rm", "-rf", path, 0 }; |
331 | #endif |
93 | #endif |
332 | systemv (argv); |
94 | systemv (argv); |
333 | } |
95 | } |
334 | |
96 | |
335 | enum |
|
|
336 | { |
|
|
337 | T_NULL, // 5 |
|
|
338 | T_META, // 1 : exe_id, exe_ver |
|
|
339 | T_ENV, // 2 : name, value |
|
|
340 | T_ARG, // 3 : arg |
|
|
341 | T_DIR, // 4+: path |
|
|
342 | T_FILE, // 4+: path, data |
|
|
343 | T_NUM |
|
|
344 | }; |
|
|
345 | |
|
|
346 | enum |
|
|
347 | { |
|
|
348 | F_EXEC = 1, |
|
|
349 | F_LZF = 2, |
|
|
350 | F_NULL = 0 |
|
|
351 | }; |
|
|
352 | |
|
|
353 | struct hdr |
|
|
354 | { |
|
|
355 | unsigned char type; |
|
|
356 | unsigned char flags; |
|
|
357 | unsigned char namelen[2]; |
|
|
358 | unsigned char datalen[4]; |
|
|
359 | }; |
|
|
360 | |
|
|
361 | struct tail { |
|
|
362 | unsigned char max_filesize[4]; |
|
|
363 | unsigned char size[4]; |
|
|
364 | char magic[12]; |
|
|
365 | }; |
|
|
366 | |
|
|
367 | static char *pack_base, *pack_end; |
97 | static char *pack_base, *pack_end; |
368 | static struct hdr *pack_cur; |
98 | static struct u_pack_hdr *pack_cur; |
369 | static char *scratch; |
99 | static char *scratch; |
370 | static unsigned int scratch_size; |
100 | static unsigned int scratch_size; |
371 | |
101 | |
372 | #define PACK_NAME ((char *)(pack_cur + 1)) |
102 | #define PACK_NAME ((char *)(pack_cur + 1)) |
373 | #define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) |
103 | #define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) |
… | |
… | |
376 | static void |
106 | static void |
377 | pack_next (void) |
107 | pack_next (void) |
378 | { |
108 | { |
379 | unsigned int d = u_32 (pack_cur->datalen); |
109 | unsigned int d = u_32 (pack_cur->datalen); |
380 | |
110 | |
381 | pack_cur = (struct hdr *)(PACK_DATA + d); |
111 | pack_cur = (struct u_pack_hdr *)(PACK_DATA + d); |
382 | } |
112 | } |
383 | |
113 | |
384 | static void |
114 | static void |
385 | pack_unmap (void) |
115 | pack_unmap (void) |
386 | { |
116 | { |
… | |
… | |
421 | pack_unmap (); |
151 | pack_unmap (); |
422 | |
152 | |
423 | pack_base = addr; |
153 | pack_base = addr; |
424 | pack_end = pack_base + size; |
154 | pack_end = pack_base + size; |
425 | |
155 | |
426 | struct tail *tail; |
156 | struct u_pack_tail *tail; |
427 | |
157 | |
428 | tail = (void *)(pack_end - sizeof (*tail)); |
158 | tail = (void *)(pack_end - sizeof (*tail)); |
429 | |
159 | |
430 | if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) |
160 | if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) |
431 | return 0; |
161 | return 0; |
432 | |
162 | |
433 | pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); |
163 | pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size)); |
434 | |
164 | |
435 | scratch = u_malloc (scratch_size = u_32 (tail->max_filesize)); |
165 | scratch = u_malloc (scratch_size = u_32 (tail->max_filesize)); |
436 | if (!scratch) |
166 | if (!scratch) |
437 | return 0; |
167 | return 0; |
438 | |
168 | |
… | |
… | |
443 | exe_info (void) |
173 | exe_info (void) |
444 | { |
174 | { |
445 | if (!pack_map ()) |
175 | if (!pack_map ()) |
446 | fatal ("unable to locate packfile in executable - executable corrupted?"); |
176 | fatal ("unable to locate packfile in executable - executable corrupted?"); |
447 | |
177 | |
448 | if (pack_cur->type != T_META) |
178 | if (pack_cur->type != U_T_META) |
449 | fatal ("unable to locate executable metadata - executable corrupted?"); |
179 | fatal ("unable to locate executable metadata - executable corrupted?"); |
450 | |
180 | |
451 | strcpy (exe_id, PACK_NAME); |
181 | strcpy (exe_id, PACK_NAME); |
452 | } |
182 | } |
453 | |
183 | |
… | |
… | |
476 | } |
206 | } |
477 | |
207 | |
478 | u_close (oh); |
208 | u_close (oh); |
479 | } |
209 | } |
480 | |
210 | |
481 | if (pack_cur->type != T_META) |
211 | if (pack_cur->type != U_T_META) |
482 | fatal ("unable to locate override metadata"); |
212 | fatal ("unable to locate override metadata"); |
483 | |
213 | |
484 | strcpy (exe_ver, PACK_DATA); |
214 | strcpy (exe_ver, PACK_DATA); |
485 | pack_next (); |
215 | pack_next (); |
486 | |
216 | |
487 | for (;;) |
217 | for (;;) |
488 | { |
218 | { |
489 | if (pack_cur->type == T_ENV) |
219 | if (pack_cur->type == U_T_ENV) |
490 | u_setenv (PACK_NAME, PACK_DATA); |
220 | u_setenv (PACK_NAME, PACK_DATA); |
491 | else if (pack_cur->type == T_ARG) |
221 | else if (pack_cur->type == U_T_ARG) |
492 | { |
222 | { |
493 | int len = u_16 (pack_cur->namelen) + 1; |
223 | int len = u_16 (pack_cur->namelen) + 1; |
494 | exe_argv [exe_argc++] = exe_args + exe_argo; |
224 | exe_argv [exe_argc++] = exe_args + exe_argo; |
495 | memcpy (exe_args + exe_argo, PACK_NAME, len); |
225 | memcpy (exe_args + exe_argo, PACK_NAME, len); |
496 | exe_argo += len; |
226 | exe_argo += len; |
… | |
… | |
521 | |
251 | |
522 | for (;;) |
252 | for (;;) |
523 | { |
253 | { |
524 | switch (pack_cur->type) |
254 | switch (pack_cur->type) |
525 | { |
255 | { |
526 | case T_DIR: |
256 | case U_T_DIR: |
527 | u_mkdir (PACK_NAME); |
257 | u_mkdir (PACK_NAME); |
528 | break; |
258 | break; |
529 | |
259 | |
530 | case T_FILE: |
260 | case U_T_FILE: |
531 | { |
261 | { |
532 | u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); |
262 | u_handle h = u_creat (PACK_NAME, pack_cur->flags & U_F_EXEC); |
533 | unsigned int dlen, len = u_32 (pack_cur->datalen); |
263 | unsigned int dlen, len = u_32 (pack_cur->datalen); |
534 | char *data = PACK_DATA; |
264 | char *data = PACK_DATA; |
535 | |
265 | |
536 | if (pack_cur->flags & F_LZF) |
266 | if (pack_cur->flags & U_F_LZF) |
537 | if (dlen = lzf_decompress (data, len, scratch, scratch_size)) |
267 | if (dlen = lzf_decompress (data, len, scratch, scratch_size)) |
538 | { |
268 | { |
539 | data = scratch; |
269 | data = scratch; |
540 | len = dlen; |
270 | len = dlen; |
541 | } |
271 | } |
… | |
… | |
551 | u_fsync (h); |
281 | u_fsync (h); |
552 | u_close (h); |
282 | u_close (h); |
553 | } |
283 | } |
554 | break; |
284 | break; |
555 | |
285 | |
556 | case T_NULL: |
286 | case U_T_NULL: |
557 | goto done; |
287 | goto done; |
558 | } |
288 | } |
559 | |
289 | |
560 | pack_next (); |
290 | pack_next (); |
561 | } |
291 | } |