author | kevinw |
Tue, 03 Mar 2015 19:42:09 +0000 | |
changeset 29457 | 7cfe74042cef |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
15261 | 2 |
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
25 |
#ifdef _ALLBSD_SOURCE |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
26 |
#include <stdint.h> |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
27 |
#define THRTYPE intptr_t |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
28 |
#else |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
29 |
#define THRTYPE int |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
30 |
#endif |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
31 |
|
2 | 32 |
#include <sys/types.h> |
33 |
||
34 |
#include <stdio.h> |
|
35 |
#include <string.h> |
|
36 |
#include <stdlib.h> |
|
37 |
#include <stdarg.h> |
|
38 |
#include <errno.h> |
|
39 |
||
40 |
#include <limits.h> |
|
41 |
#include <time.h> |
|
42 |
||
43 |
#if defined(unix) && !defined(PRODUCT) |
|
44 |
#include "pthread.h" |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
45 |
#define THREAD_SELF ((THRTYPE)pthread_self()) |
2 | 46 |
#endif |
47 |
||
48 |
#include "defines.h" |
|
49 |
#include "bytes.h" |
|
50 |
#include "utils.h" |
|
51 |
#include "coding.h" |
|
52 |
#include "bands.h" |
|
53 |
||
54 |
#include "constants.h" |
|
55 |
||
56 |
#include "zip.h" |
|
57 |
||
58 |
#include "unpack.h" |
|
59 |
||
60 |
||
61 |
int main(int argc, char **argv) { |
|
62 |
return unpacker::run(argc, argv); |
|
63 |
} |
|
64 |
||
25149 | 65 |
// Dealing with big-endian arch |
66 |
#ifdef _BIG_ENDIAN |
|
67 |
#define SWAP_INT(a) (((a>>24)&0xff) | ((a<<8)&0xff0000) | ((a>>8)&0xff00) | ((a<<24)&0xff000000)) |
|
68 |
#else |
|
69 |
#define SWAP_INT(a) (a) |
|
70 |
#endif |
|
71 |
||
2 | 72 |
// Single-threaded, implementation, not reentrant. |
73 |
// Includes a weak error check against MT access. |
|
74 |
#ifndef THREAD_SELF |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
75 |
#define THREAD_SELF ((THRTYPE) 0) |
2 | 76 |
#endif |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
77 |
NOT_PRODUCT(static THRTYPE uThread = -1;) |
2 | 78 |
|
79 |
unpacker* unpacker::non_mt_current = null; |
|
80 |
unpacker* unpacker::current() { |
|
15261 | 81 |
//assert(uThread == THREAD_SELF); |
2 | 82 |
return non_mt_current; |
83 |
} |
|
84 |
static void set_current_unpacker(unpacker* u) { |
|
85 |
unpacker::non_mt_current = u; |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
86 |
assert(((uThread = (u == null) ? (THRTYPE) -1 : THREAD_SELF), |
2 | 87 |
true)); |
88 |
} |
|
89 |
||
90 |
// Callback for fetching data, Unix style. |
|
91 |
static jlong read_input_via_stdio(unpacker* u, |
|
92 |
void* buf, jlong minlen, jlong maxlen) { |
|
93 |
assert(minlen <= maxlen); // don't talk nonsense |
|
94 |
jlong numread = 0; |
|
95 |
char* bufptr = (char*) buf; |
|
96 |
while (numread < minlen) { |
|
97 |
// read available input, up to buf.length or maxlen |
|
98 |
int readlen = (1<<16); |
|
99 |
if (readlen > (maxlen - numread)) |
|
100 |
readlen = (int)(maxlen - numread); |
|
101 |
int nr = 0; |
|
102 |
if (u->infileptr != null) { |
|
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
103 |
nr = (int)fread(bufptr, 1, readlen, u->infileptr); |
2 | 104 |
} else { |
105 |
#ifndef WIN32 |
|
106 |
// we prefer unbuffered inputs |
|
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
107 |
nr = (int)read(u->infileno, bufptr, readlen); |
2 | 108 |
#else |
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
109 |
nr = (int)fread(bufptr, 1, readlen, stdin); |
2 | 110 |
#endif |
111 |
} |
|
112 |
if (nr <= 0) { |
|
113 |
if (errno != EINTR) |
|
114 |
break; |
|
115 |
nr = 0; |
|
116 |
} |
|
117 |
numread += nr; |
|
118 |
bufptr += nr; |
|
119 |
assert(numread <= maxlen); |
|
120 |
} |
|
121 |
//fprintf(u->errstrm, "readInputFn(%d,%d) => %d\n", |
|
122 |
// (int)minlen, (int)maxlen, (int)numread); |
|
123 |
return numread; |
|
124 |
} |
|
125 |
||
126 |
enum { EOF_MAGIC = 0, BAD_MAGIC = -1 }; |
|
127 |
static int read_magic(unpacker* u, char peek[], int peeklen) { |
|
128 |
assert(peeklen == 4); // magic numbers are always 4 bytes |
|
129 |
jlong nr = (u->read_input_fn)(u, peek, peeklen, peeklen); |
|
130 |
if (nr != peeklen) { |
|
131 |
return (nr == 0) ? EOF_MAGIC : BAD_MAGIC; |
|
132 |
} |
|
133 |
int magic = 0; |
|
134 |
for (int i = 0; i < peeklen; i++) { |
|
135 |
magic <<= 8; |
|
136 |
magic += peek[i] & 0xFF; |
|
137 |
} |
|
138 |
return magic; |
|
139 |
} |
|
140 |
||
141 |
static void setup_gzin(unpacker* u) { |
|
142 |
gunzip* gzin = NEW(gunzip, 1); |
|
143 |
gzin->init(u); |
|
144 |
} |
|
145 |
||
146 |
static const char* nbasename(const char* progname) { |
|
147 |
const char* slash = strrchr(progname, '/'); |
|
148 |
if (slash != null) progname = ++slash; |
|
149 |
return progname; |
|
150 |
} |
|
151 |
||
152 |
static const char* usage_lines[] = { |
|
153 |
"Usage: %s [-opt... | --option=value]... x.pack[.gz] y.jar\n", |
|
154 |
"\n", |
|
155 |
"Unpacking Options\n", |
|
156 |
" -H{h}, --deflate-hint={h} override transmitted deflate hint: true, false, or keep (default)\n", |
|
157 |
" -r, --remove-pack-file remove input file after unpacking\n", |
|
158 |
" -v, --verbose increase program verbosity\n", |
|
159 |
" -q, --quiet set verbosity to lowest level\n", |
|
160 |
" -l{F}, --log-file={F} output to the given log file, or '-' for standard output (default)\n", |
|
161 |
" -?, -h, --help print this message\n", |
|
162 |
" -V, --version print program version\n", |
|
163 |
" -J{X} Java VM argument (ignored)\n", |
|
164 |
null |
|
165 |
}; |
|
166 |
||
167 |
static void usage(unpacker* u, const char* progname, bool full = false) { |
|
168 |
// WinMain does not set argv[0] to the progrname |
|
169 |
progname = (progname != null) ? nbasename(progname) : "unpack200"; |
|
170 |
for (int i = 0; usage_lines[i] != null; i++) { |
|
171 |
fprintf(u->errstrm, usage_lines[i], progname); |
|
172 |
if (!full) { |
|
173 |
fprintf(u->errstrm, |
|
174 |
"(For more information, run %s --help .)\n", progname); |
|
175 |
break; |
|
176 |
} |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
// argument parsing |
|
181 |
static char** init_args(int argc, char** argv, int &envargc) { |
|
182 |
const char* env = getenv("UNPACK200_FLAGS"); |
|
183 |
ptrlist envargs; |
|
184 |
envargs.init(); |
|
185 |
if (env != null) { |
|
186 |
char* buf = (char*) strdup(env); |
|
187 |
const char* delim = "\n\t "; |
|
188 |
for (char* p = strtok(buf, delim); p != null; p = strtok(null, delim)) { |
|
189 |
envargs.add(p); |
|
190 |
} |
|
191 |
} |
|
192 |
// allocate extra margin at both head and tail |
|
193 |
char** argp = NEW(char*, envargs.length()+argc+1); |
|
194 |
char** argp0 = argp; |
|
195 |
int i; |
|
196 |
for (i = 0; i < envargs.length(); i++) { |
|
197 |
*argp++ = (char*) envargs.get(i); |
|
198 |
} |
|
199 |
for (i = 1; i < argc; i++) { |
|
200 |
// note: skip argv[0] (program name) |
|
201 |
*argp++ = (char*) strdup(argv[i]); // make a scratch copy |
|
202 |
} |
|
203 |
*argp = null; // sentinel |
|
204 |
envargc = envargs.length(); // report this count to next_arg |
|
205 |
envargs.free(); |
|
206 |
return argp0; |
|
207 |
} |
|
208 |
||
209 |
static int strpcmp(const char* str, const char* pfx) { |
|
210 |
return strncmp(str, pfx, strlen(pfx)); |
|
211 |
} |
|
212 |
||
213 |
static const char flag_opts[] = "vqrVh?"; |
|
214 |
static const char string_opts[] = "HlJ"; |
|
215 |
||
216 |
static int next_arg(char** &argp) { |
|
217 |
char* arg = *argp; |
|
218 |
if (arg == null || arg[0] != '-') { // end of option list |
|
219 |
return 0; |
|
220 |
} |
|
221 |
//printf("opt: %s\n", arg); |
|
222 |
char ach = arg[1]; |
|
223 |
if (ach == '\0') { |
|
224 |
// ++argp; // do not pop this arg |
|
225 |
return 0; // bare "-" is stdin/stdout |
|
226 |
} else if (arg[1] == '-') { // --foo option |
|
227 |
static const char* keys[] = { |
|
228 |
"Hdeflate-hint=", |
|
229 |
"vverbose", |
|
230 |
"qquiet", |
|
231 |
"rremove-pack-file", |
|
232 |
"llog-file=", |
|
233 |
"Vversion", |
|
234 |
"hhelp", |
|
235 |
null }; |
|
236 |
if (arg[2] == '\0') { // end of option list |
|
237 |
++argp; // pop the "--" |
|
238 |
return 0; |
|
239 |
} |
|
240 |
for (int i = 0; keys[i] != null; i++) { |
|
241 |
const char* key = keys[i]; |
|
242 |
char kch = *key++; |
|
243 |
if (strchr(key, '=') == null) { |
|
244 |
if (!strcmp(arg+2, key)) { |
|
245 |
++argp; // pop option arg |
|
246 |
return kch; |
|
247 |
} |
|
248 |
} else { |
|
249 |
if (!strpcmp(arg+2, key)) { |
|
250 |
*argp += 2 + strlen(key); // remove "--"+key from arg |
|
251 |
return kch; |
|
252 |
} |
|
253 |
} |
|
254 |
} |
|
255 |
} else if (strchr(flag_opts, ach) != null) { // plain option |
|
256 |
if (arg[2] == '\0') { |
|
257 |
++argp; |
|
258 |
} else { |
|
259 |
// in-place edit of "-vxyz" to "-xyz" |
|
260 |
arg += 1; // skip original '-' |
|
261 |
arg[0] = '-'; |
|
262 |
*argp = arg; |
|
263 |
} |
|
264 |
//printf(" key => %c\n", ach); |
|
265 |
return ach; |
|
266 |
} else if (strchr(string_opts, ach) != null) { // argument-bearing option |
|
267 |
if (arg[2] == '\0') { |
|
268 |
if (argp[1] == null) return -1; // no next arg |
|
269 |
++argp; // leave the argument in place |
|
270 |
} else { |
|
271 |
// in-place edit of "-Hxyz" to "xyz" |
|
272 |
arg += 2; // skip original '-H' |
|
273 |
*argp = arg; |
|
274 |
} |
|
275 |
//printf(" key => %c\n", ach); |
|
276 |
return ach; |
|
277 |
} |
|
278 |
return -1; // bad argument |
|
279 |
} |
|
280 |
||
281 |
static const char sccsver[] = "1.30, 07/05/05"; |
|
282 |
||
283 |
// Usage: unpackage input.pack output.jar |
|
284 |
int unpacker::run(int argc, char **argv) { |
|
285 |
unpacker u; |
|
286 |
u.init(read_input_via_stdio); |
|
287 |
set_current_unpacker(&u); |
|
288 |
||
289 |
jar jarout; |
|
290 |
jarout.init(&u); |
|
291 |
||
292 |
int envargc = 0; |
|
293 |
char** argbuf = init_args(argc, argv, envargc); |
|
294 |
char** arg0 = argbuf+envargc; |
|
295 |
char** argp = argbuf; |
|
296 |
||
297 |
int verbose = 0; |
|
298 |
char* logfile = null; |
|
299 |
||
300 |
for (;;) { |
|
301 |
const char* arg = (*argp == null)? "": u.saveStr(*argp); |
|
302 |
bool isenvarg = (argp < arg0); |
|
303 |
int ach = next_arg(argp); |
|
304 |
bool hasoptarg = (ach != 0 && strchr(string_opts, ach) != null); |
|
305 |
if (ach == 0 && argp >= arg0) break; |
|
306 |
if (isenvarg && argp == arg0 && hasoptarg) ach = 0; // don't pull from cmdline |
|
307 |
switch (ach) { |
|
308 |
case 'H': u.set_option(UNPACK_DEFLATE_HINT,*argp++); break; |
|
309 |
case 'v': ++verbose; break; |
|
310 |
case 'q': verbose = 0; break; |
|
311 |
case 'r': u.set_option(UNPACK_REMOVE_PACKFILE,"1"); break; |
|
312 |
case 'l': logfile = *argp++; break; |
|
313 |
case 'J': argp += 1; break; // skip ignored -Jxxx parameter |
|
314 |
||
315 |
case 'V': |
|
2602
5b394a4b6ce1
6755943: Java JAR Pack200 Decompression should enforce stricter header checks
ksrini
parents:
2
diff
changeset
|
316 |
fprintf(u.errstrm, VERSION_STRING, nbasename(argv[0]), sccsver); |
2 | 317 |
exit(0); |
318 |
||
319 |
case 'h': |
|
320 |
case '?': |
|
321 |
usage(&u, argv[0], true); |
|
322 |
exit(1); |
|
323 |
||
324 |
default: |
|
325 |
const char* inenv = isenvarg? " in ${UNPACK200_FLAGS}": ""; |
|
326 |
if (hasoptarg) |
|
327 |
fprintf(u.errstrm, "Missing option string%s: %s\n", inenv, arg); |
|
328 |
else |
|
329 |
fprintf(u.errstrm, "Unrecognized argument%s: %s\n", inenv, arg); |
|
330 |
usage(&u, argv[0]); |
|
331 |
exit(2); |
|
332 |
} |
|
333 |
} |
|
334 |
||
335 |
if (verbose != 0) { |
|
336 |
u.set_option(DEBUG_VERBOSE, u.saveIntStr(verbose)); |
|
337 |
} |
|
338 |
if (logfile != null) { |
|
339 |
u.set_option(UNPACK_LOG_FILE, logfile); |
|
340 |
} |
|
341 |
||
342 |
u.redirect_stdio(); |
|
343 |
||
344 |
const char* source_file = *argp++; |
|
345 |
const char* destination_file = *argp++; |
|
346 |
||
347 |
if (source_file == null || destination_file == null || *argp != null) { |
|
348 |
usage(&u, argv[0]); |
|
349 |
exit(2); |
|
350 |
} |
|
351 |
||
352 |
if (verbose != 0) { |
|
353 |
fprintf(u.errstrm, |
|
354 |
"Unpacking from %s to %s\n", source_file, destination_file); |
|
355 |
} |
|
356 |
bool& remove_source = u.remove_packfile; |
|
357 |
||
358 |
if (strcmp(source_file, "-") == 0) { |
|
359 |
remove_source = false; |
|
360 |
u.infileno = fileno(stdin); |
|
361 |
} else { |
|
362 |
u.infileptr = fopen(source_file, "rb"); |
|
363 |
if (u.infileptr == null) { |
|
364 |
fprintf(u.errstrm, |
|
365 |
"Error: Could not open input file: %s\n", source_file); |
|
366 |
exit(3); // Called only from the native standalone unpacker |
|
367 |
} |
|
368 |
} |
|
369 |
||
370 |
if (strcmp(destination_file, "-") == 0) { |
|
371 |
jarout.jarfp = stdout; |
|
372 |
if (u.errstrm == stdout) // do not mix output |
|
373 |
u.set_option(UNPACK_LOG_FILE, LOGFILE_STDERR); |
|
374 |
} else { |
|
375 |
jarout.openJarFile(destination_file); |
|
376 |
assert(jarout.jarfp != null); |
|
377 |
} |
|
378 |
||
379 |
if (verbose != 0) |
|
380 |
u.dump_options(); |
|
381 |
||
382 |
char peek[4]; |
|
383 |
int magic; |
|
384 |
||
385 |
// check for GZIP input |
|
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
386 |
magic = read_magic(&u, peek, (int)sizeof(peek)); |
2 | 387 |
if ((magic & GZIP_MAGIC_MASK) == GZIP_MAGIC) { |
388 |
// Oops; must slap an input filter on this data. |
|
389 |
setup_gzin(&u); |
|
390 |
u.gzin->start(magic); |
|
391 |
if (!u.aborting()) { |
|
392 |
u.start(); |
|
393 |
} |
|
394 |
} else { |
|
25149 | 395 |
u.gzcrc = 0; |
2 | 396 |
u.start(peek, sizeof(peek)); |
397 |
} |
|
398 |
||
399 |
// Note: The checks to u.aborting() are necessary to gracefully |
|
400 |
// terminate processing when the first segment throws an error. |
|
401 |
||
402 |
for (;;) { |
|
403 |
if (u.aborting()) break; |
|
404 |
||
405 |
// Each trip through this loop unpacks one segment |
|
406 |
// and then resets the unpacker. |
|
407 |
for (unpacker::file* filep; (filep = u.get_next_file()) != null; ) { |
|
408 |
if (u.aborting()) break; |
|
409 |
u.write_file_to_jar(filep); |
|
410 |
} |
|
411 |
if (u.aborting()) break; |
|
412 |
||
413 |
// Peek ahead for more data. |
|
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
414 |
magic = read_magic(&u, peek, (int)sizeof(peek)); |
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
415 |
if (magic != (int)JAVA_PACKAGE_MAGIC) { |
2 | 416 |
if (magic != EOF_MAGIC) |
417 |
u.abort("garbage after end of pack archive"); |
|
418 |
break; // all done |
|
419 |
} |
|
420 |
||
421 |
// Release all storage from parsing the old segment. |
|
422 |
u.reset(); |
|
423 |
||
424 |
// Restart, beginning with the peek-ahead. |
|
425 |
u.start(peek, sizeof(peek)); |
|
426 |
} |
|
427 |
||
428 |
||
429 |
||
430 |
int status = 0; |
|
431 |
if (u.aborting()) { |
|
432 |
fprintf(u.errstrm, "Error: %s\n", u.get_abort_message()); |
|
433 |
status = 1; |
|
434 |
} |
|
435 |
||
25149 | 436 |
if (!u.aborting() && u.infileptr != null) { |
437 |
if (u.gzcrc != 0) { |
|
438 |
// Read the CRC information from the gzip container |
|
439 |
fseek(u.infileptr, -8, SEEK_END); |
|
440 |
uint filecrc; |
|
441 |
fread(&filecrc, sizeof(filecrc), 1, u.infileptr); |
|
442 |
if (u.gzcrc != SWAP_INT(filecrc)) { // CRC error |
|
443 |
if (strcmp(destination_file, "-") != 0) { |
|
444 |
// Output is not stdout, remove it, it's broken |
|
445 |
if (u.jarout != null) |
|
446 |
u.jarout->closeJarFile(false); |
|
447 |
remove(destination_file); |
|
448 |
} |
|
449 |
// Print out the error and exit with return code != 0 |
|
450 |
u.abort("CRC error, invalid compressed data."); |
|
451 |
} |
|
452 |
} |
|
2 | 453 |
fclose(u.infileptr); |
454 |
u.infileptr = null; |
|
455 |
} |
|
456 |
||
457 |
if (!u.aborting() && remove_source) |
|
458 |
remove(source_file); |
|
459 |
||
460 |
if (verbose != 0) { |
|
461 |
fprintf(u.errstrm, "unpacker completed with status=%d\n", status); |
|
462 |
} |
|
463 |
||
464 |
u.finish(); |
|
465 |
||
466 |
u.free(); // tidy up malloc blocks |
|
467 |
set_current_unpacker(null); // clean up global pointer |
|
468 |
||
469 |
return status; |
|
470 |
} |