1 /*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
45 #define THREAD_SELF ((THRTYPE)pthread_self())
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
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
72 // Single-threaded, implementation, not reentrant.
73 // Includes a weak error check against MT access.
74 #ifndef THREAD_SELF
75 #define THREAD_SELF ((THRTYPE) 0)
76 #endif
77 NOT_PRODUCT(static THRTYPE uThread = -1;)
78
79 unpacker* unpacker::non_mt_current = null;
80 unpacker* unpacker::current() {
81 //assert(uThread == THREAD_SELF);
82 return non_mt_current;
83 }
84 static void set_current_unpacker(unpacker* u) {
85 unpacker::non_mt_current = u;
86 assert(((uThread = (u == null) ? (THRTYPE) -1 : THREAD_SELF),
87 true));
88 }
89
90 // Callback for fetching data, Unix style.
91 static jlong read_input_via_stdio(unpacker* u,
349 if (verbose != 0) {
350 fprintf(u.errstrm,
351 "Unpacking from %s to %s\n", source_file, destination_file);
352 }
353 bool& remove_source = u.remove_packfile;
354
355 if (strcmp(source_file, "-") == 0) {
356 remove_source = false;
357 u.infileno = fileno(stdin);
358 } else {
359 u.infileptr = fopen(source_file, "rb");
360 if (u.infileptr == null) {
361 fprintf(u.errstrm,
362 "Error: Could not open input file: %s\n", source_file);
363 exit(3); // Called only from the native standalone unpacker
364 }
365 }
366
367 if (strcmp(destination_file, "-") == 0) {
368 jarout.jarfp = stdout;
369 if (u.errstrm == stdout) // do not mix output
370 u.set_option(UNPACK_LOG_FILE, LOGFILE_STDERR);
371 } else {
372 jarout.openJarFile(destination_file);
373 assert(jarout.jarfp != null);
374 }
375
376 if (verbose != 0)
377 u.dump_options();
378
379 char peek[4];
380 int magic;
381
382 // check for GZIP input
383 magic = read_magic(&u, peek, (int)sizeof(peek));
384 if ((magic & GZIP_MAGIC_MASK) == GZIP_MAGIC) {
385 // Oops; must slap an input filter on this data.
386 setup_gzin(&u);
387 u.gzin->start(magic);
388 if (!u.aborting()) {
389 u.start();
390 }
391 } else {
392 u.gzcrc = 0;
393 u.start(peek, sizeof(peek));
394 }
395
396 // Note: The checks to u.aborting() are necessary to gracefully
397 // terminate processing when the first segment throws an error.
398
399 for (;;) {
400 if (u.aborting()) break;
401
402 // Each trip through this loop unpacks one segment
403 // and then resets the unpacker.
404 for (unpacker::file* filep; (filep = u.get_next_file()) != null; ) {
405 if (u.aborting()) break;
406 u.write_file_to_jar(filep);
407 }
408 if (u.aborting()) break;
409
410 // Peek ahead for more data.
411 magic = read_magic(&u, peek, (int)sizeof(peek));
412 if (magic != (int)JAVA_PACKAGE_MAGIC) {
413 if (magic != EOF_MAGIC)
414 u.abort("garbage after end of pack archive");
415 break; // all done
416 }
417
418 // Release all storage from parsing the old segment.
419 u.reset();
420
421 // Restart, beginning with the peek-ahead.
422 u.start(peek, sizeof(peek));
423 }
424
425
426
427 int status = 0;
428 if (u.aborting()) {
429 fprintf(u.errstrm, "Error: %s\n", u.get_abort_message());
430 status = 1;
431 }
432
433 if (!u.aborting() && u.infileptr != null) {
434 if (u.gzcrc != 0) {
435 // Read the CRC information from the gzip container
436 fseek(u.infileptr, -8, SEEK_END);
437 uint filecrc;
438 fread(&filecrc, sizeof(filecrc), 1, u.infileptr);
439 if (u.gzcrc != SWAP_INT(filecrc)) { // CRC error
440 if (strcmp(destination_file, "-") != 0) {
441 // Output is not stdout, remove it, it's broken
442 if (u.jarout != null)
443 u.jarout->closeJarFile(false);
444 remove(destination_file);
445 }
446 // Print out the error and exit with return code != 0
447 u.abort("CRC error, invalid compressed data.");
448 }
449 }
450 fclose(u.infileptr);
451 u.infileptr = null;
452 }
453
454 if (!u.aborting() && remove_source)
455 remove(source_file);
456
457 if (verbose != 0) {
458 fprintf(u.errstrm, "unpacker completed with status=%d\n", status);
459 }
460
461 u.finish();
462
463 u.free(); // tidy up malloc blocks
464 set_current_unpacker(null); // clean up global pointer
465
466 return status;
467 }
|
1 /*
2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
45 #define THREAD_SELF ((THRTYPE)pthread_self())
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
65 // Single-threaded, implementation, not reentrant.
66 // Includes a weak error check against MT access.
67 #ifndef THREAD_SELF
68 #define THREAD_SELF ((THRTYPE) 0)
69 #endif
70 NOT_PRODUCT(static THRTYPE uThread = -1;)
71
72 unpacker* unpacker::non_mt_current = null;
73 unpacker* unpacker::current() {
74 //assert(uThread == THREAD_SELF);
75 return non_mt_current;
76 }
77 static void set_current_unpacker(unpacker* u) {
78 unpacker::non_mt_current = u;
79 assert(((uThread = (u == null) ? (THRTYPE) -1 : THREAD_SELF),
80 true));
81 }
82
83 // Callback for fetching data, Unix style.
84 static jlong read_input_via_stdio(unpacker* u,
342 if (verbose != 0) {
343 fprintf(u.errstrm,
344 "Unpacking from %s to %s\n", source_file, destination_file);
345 }
346 bool& remove_source = u.remove_packfile;
347
348 if (strcmp(source_file, "-") == 0) {
349 remove_source = false;
350 u.infileno = fileno(stdin);
351 } else {
352 u.infileptr = fopen(source_file, "rb");
353 if (u.infileptr == null) {
354 fprintf(u.errstrm,
355 "Error: Could not open input file: %s\n", source_file);
356 exit(3); // Called only from the native standalone unpacker
357 }
358 }
359
360 if (strcmp(destination_file, "-") == 0) {
361 jarout.jarfp = stdout;
362 jarout.jarname = null;
363 if (u.errstrm == stdout) // do not mix output
364 u.set_option(UNPACK_LOG_FILE, LOGFILE_STDERR);
365 } else {
366 jarout.openJarFile(destination_file);
367 assert(jarout.jarfp != null);
368 }
369
370 if (verbose != 0)
371 u.dump_options();
372
373 char peek[4];
374 int magic;
375
376 // check for GZIP input
377 magic = read_magic(&u, peek, (int)sizeof(peek));
378 if ((magic & GZIP_MAGIC_MASK) == GZIP_MAGIC) {
379 // Oops; must slap an input filter on this data.
380 setup_gzin(&u);
381 u.gzin->start(magic);
382 u.gzin->gzcrc = 0;
383 u.gzin->gzlen = 0;
384 if (!u.aborting()) {
385 u.start();
386 }
387 } else {
388 u.start(peek, sizeof(peek));
389 }
390
391 // Note: The checks to u.aborting() are necessary to gracefully
392 // terminate processing when the first segment throws an error.
393
394 for (;;) {
395 if (u.aborting()) break;
396
397 // Each trip through this loop unpacks one segment
398 // and then resets the unpacker.
399 for (unpacker::file* filep; (filep = u.get_next_file()) != null; ) {
400 if (u.aborting()) break;
401 u.write_file_to_jar(filep);
402 }
403 if (u.aborting()) break;
404
405 // Peek ahead for more data.
406 magic = read_magic(&u, peek, (int)sizeof(peek));
407 if (magic != (int)JAVA_PACKAGE_MAGIC) {
408 if (magic != EOF_MAGIC)
409 u.abort("garbage after end of pack archive");
410 break; // all done
411 }
412
413 // Release all storage from parsing the old segment.
414 u.reset();
415
416 // Restart, beginning with the peek-ahead.
417 u.start(peek, sizeof(peek));
418 }
419
420 int status = 0;
421 if (u.aborting()) {
422 fprintf(u.errstrm, "Error: %s\n", u.get_abort_message());
423 status = 1;
424 }
425
426 if (u.infileptr != null) {
427 fclose(u.infileptr);
428 u.infileptr = null;
429 }
430
431 if (!u.aborting() && remove_source)
432 remove(source_file);
433
434 if (verbose != 0) {
435 fprintf(u.errstrm, "unpacker completed with status=%d\n", status);
436 }
437
438 u.finish();
439
440 u.free(); // tidy up malloc blocks
441 set_current_unpacker(null); // clean up global pointer
442
443 return status;
444 }
|