From 604162acdf5283a9759c1b3ce9e90887a6599ce7 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Sun, 29 Sep 2013 21:11:30 +0200 Subject: Turn pack200 into an actual library --- depends/pack200/CMakeLists.txt | 10 +- depends/pack200/anti200.cpp | 28 ++ depends/pack200/include/unpack200.h | 38 +- depends/pack200/src/bands.cpp | 36 +- depends/pack200/src/bands.h | 9 +- depends/pack200/src/bytes.cpp | 4 +- depends/pack200/src/bytes.h | 2 +- depends/pack200/src/coding.cpp | 125 ++++--- depends/pack200/src/coding.h | 29 +- depends/pack200/src/constants.h | 2 +- depends/pack200/src/defines.h | 75 +--- depends/pack200/src/main.cpp | 489 -------------------------- depends/pack200/src/unpack.cpp | 670 ++++++++++-------------------------- depends/pack200/src/unpack.h | 114 ++---- depends/pack200/src/unpack200.cpp | 172 +++++++++ depends/pack200/src/utils.cpp | 28 +- depends/pack200/src/utils.h | 9 +- depends/pack200/src/zip.cpp | 56 +-- depends/pack200/src/zip.h | 28 +- depends/xz-embedded/CMakeLists.txt | 11 +- 20 files changed, 583 insertions(+), 1352 deletions(-) create mode 100644 depends/pack200/anti200.cpp delete mode 100644 depends/pack200/src/main.cpp create mode 100644 depends/pack200/src/unpack200.cpp diff --git a/depends/pack200/CMakeLists.txt b/depends/pack200/CMakeLists.txt index 79c78f80..657e303c 100644 --- a/depends/pack200/CMakeLists.txt +++ b/depends/pack200/CMakeLists.txt @@ -19,6 +19,7 @@ ELSE(UNIX) ENDIF(UNIX) SET(PACK200_SRC +include/unpack200.h src/bands.cpp src/bands.h src/bytes.cpp @@ -27,7 +28,7 @@ src/coding.cpp src/coding.h src/constants.h src/defines.h -src/main.cpp +src/unpack200.cpp src/unpack.cpp src/unpack.h src/utils.cpp @@ -36,7 +37,9 @@ src/zip.cpp src/zip.h ) -add_executable(unpack200 ${PACK200_SRC}) +include_directories(include) + +add_library(unpack200 STATIC ${PACK200_SRC}) IF(UNIX) target_link_libraries(unpack200 ${ZLIB_LIBRARIES}) @@ -44,3 +47,6 @@ ELSE() # zlib is part of Qt on windows. use it. QT5_USE_MODULES(unpack200 Core) ENDIF() + +add_executable(anti200 anti200.cpp) +target_link_libraries(anti200 unpack200) diff --git a/depends/pack200/anti200.cpp b/depends/pack200/anti200.cpp new file mode 100644 index 00000000..3dfdb5dc --- /dev/null +++ b/depends/pack200/anti200.cpp @@ -0,0 +1,28 @@ +/* + * This is trivial. Do what thou wilt with it. Public domain. + */ + +#include +#include +#include "unpack200.h" + +int main(int argc, char **argv) +{ + if (argc == 3) + { + try + { + unpack_200(argv[1], argv[2]); + } + catch (std::runtime_error &e) + { + std::cerr << "Bad things happened: " << e.what() << std::endl; + return EXIT_FAILURE; + } + return EXIT_SUCCESS; + } + else + std::cerr << "Simple pack200 unpacker!" << std::endl << "Run like this:" << std::endl + << " " << argv[0] << " input.jar.lzma output.jar" << std::endl; + return EXIT_FAILURE; +} diff --git a/depends/pack200/include/unpack200.h b/depends/pack200/include/unpack200.h index 8d1c8b69..bcee8009 100644 --- a/depends/pack200/include/unpack200.h +++ b/depends/pack200/include/unpack200.h @@ -1 +1,37 @@ - +/* + * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +#pragma once +#include + +/** + * @brief Unpack a PACK200 file + * + * @param input_path Path to the input file in PACK200 format. System native string encoding. + * @param output_path Path to the output file in PACK200 format. System native string encoding. + * @return void + * @throw std::runtime_error for any error encountered + */ +void unpack_200(std::string input_path, std::string output_path); diff --git a/depends/pack200/src/bands.cpp b/depends/pack200/src/bands.cpp index 6b4e8971..1c10b35b 100644 --- a/depends/pack200/src/bands.cpp +++ b/depends/pack200/src/bands.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include "defines.h" #include "bytes.h" @@ -44,18 +45,8 @@ #include "constants.h" #include "unpack.h" -inline void band::abort(const char *msg) -{ - u->abort(msg); -} -inline bool band::aborting() -{ - return u->aborting(); -} - void band::readData(int expectedLength) { - CHECK; assert(expectedLength >= 0); assert(vs[0].cmk == cmk_ERROR); if (expectedLength != 0) @@ -82,7 +73,7 @@ void band::readData(int expectedLength) // Make a conservatively generous estimate of band size in bytes. // Assume B == 5 everywhere. // Assume awkward pop with all {U} values (2*5 per value) - jlong generous = (jlong)length * (B_MAX * 3 + 1) + C_SLOP; + int64_t generous = (int64_t)length * (B_MAX * 3 + 1) + C_SLOP; u->ensure_input(generous); } @@ -102,7 +93,6 @@ void band::readData(int expectedLength) assert(!valc->isMalloc); } xvs.init(u->rp, u->rplimit, valc); - CHECK; int X = xvs.getInt(); if (valc->S() != 0) { @@ -133,7 +123,6 @@ void band::readData(int expectedLength) byte XB_byte = (byte)XB; byte *XB_ptr = &XB_byte; cm.init(u->rp, u->rplimit, XB_ptr, 0, defc, length, nullptr); - CHECK; } else { @@ -162,7 +151,6 @@ void band::setIndexByTag(byte tag) entry *band::getRefCommon(cpindex *ix_, bool nullOKwithCaller) { - CHECK_0; assert(ix_->ixTag == ixTag || (ixTag == CONSTANT_Literal && ix_->ixTag >= CONSTANT_Integer && ix_->ixTag <= CONSTANT_String)); @@ -171,27 +159,26 @@ entry *band::getRefCommon(cpindex *ix_, bool nullOKwithCaller) // But nullOKwithCaller means caller is willing to tolerate a nullptr. entry *ref = ix_->get(n); if (ref == nullptr && !(nullOKwithCaller && n == -1)) - abort(n == -1 ? "nullptr ref" : "bad ref"); + unpack_abort(n == -1 ? "nullptr ref" : "bad ref"); return ref; } -jlong band::getLong(band &lo_band, bool have_hi) +int64_t band::getLong(band &lo_band, bool have_hi) { band &hi_band = (*this); assert(lo_band.bn == hi_band.bn + 1); - uint lo = lo_band.getInt(); + uint32_t lo = lo_band.getInt(); if (!have_hi) { assert(hi_band.length == 0); return makeLong(0, lo); } - uint hi = hi_band.getInt(); + uint32_t hi = hi_band.getInt(); return makeLong(hi, lo); } int band::getIntTotal() { - CHECK_0; if (length == 0) return 0; if (total_memo > 0) @@ -201,8 +188,7 @@ int band::getIntTotal() // and that the partial sums never overflow (wrap negative) if (total < 0) { - abort("overflow detected"); - return 0; + unpack_abort("overflow detected"); } for (int k = length - 1; k > 0; k--) { @@ -210,8 +196,7 @@ int band::getIntTotal() total += vs[0].getInt(); if (total < prev_total) { - abort("overflow detected"); - return 0; + unpack_abort("overflow detected"); } } rewind(); @@ -221,7 +206,6 @@ int band::getIntTotal() int band::getIntCount(int tag) { - CHECK_0; if (length == 0) return 0; if (tag >= HIST0_MIN && tag <= HIST0_MAX) @@ -230,7 +214,6 @@ int band::getIntCount(int tag) { // Lazily calculate an approximate histogram. hist0 = U_NEW(int, (HIST0_MAX - HIST0_MIN) + 1); - CHECK_0; for (int k = length; k > 0; k--) { int x = vs[0].getInt(); @@ -404,7 +387,6 @@ const band_init all_band_inits[] = BAND_INIT(file_modtime, DELTA5_spec, 0), BAND_INIT(file_options, UNSIGNED5_spec, 0), // BAND_INIT(file_bits, BYTE1_spec, 0), {0, 0}}; -#define NUM_BAND_INITS (sizeof(all_band_inits) / sizeof(all_band_inits[0])) band *band::makeBands(unpacker *u) { @@ -434,7 +416,7 @@ void band::initIndexes(unpacker *u) for (int i = 0; i < BAND_LIMIT; i++) { band *scan = &tmp_all_bands[i]; - uint tag = scan->ixTag; // Cf. #define INDEX(tag) above + uint32_t tag = scan->ixTag; // Cf. #define INDEX(tag) above if (tag != 0 && tag != CONSTANT_Literal && (tag & SUBINDEX_BIT) == 0) { scan->setIndex(u->cp.getIndex(tag)); diff --git a/depends/pack200/src/bands.h b/depends/pack200/src/bands.h index 3f944481..a56cd7d5 100644 --- a/depends/pack200/src/bands.h +++ b/depends/pack200/src/bands.h @@ -150,11 +150,11 @@ struct band return getRefCommon(ix2, true); } entry *getRefCommon(cpindex *ix, bool nullOK); - jlong getLong(band &lo_band, bool have_hi); + int64_t getLong(band &lo_band, bool have_hi); - static jlong makeLong(uint hi, uint lo) + static int64_t makeLong(uint32_t hi, uint32_t lo) { - return ((julong)hi << 32) + (((julong)lo << 32) >> 32); + return ((uint64_t)hi << 32) + (((uint64_t)lo << 32) >> 32); } int getIntTotal(); @@ -162,9 +162,6 @@ struct band static band *makeBands(unpacker *u); static void initIndexes(unpacker *u); - - void abort(const char *msg = nullptr); //{ u->abort(msg); } - bool aborting(); //{ return u->aborting(); } }; extern band all_bands[]; diff --git a/depends/pack200/src/bytes.cpp b/depends/pack200/src/bytes.cpp index b82a987a..d3808afa 100644 --- a/depends/pack200/src/bytes.cpp +++ b/depends/pack200/src/bytes.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include "defines.h" #include "bytes.h" #include "utils.h" @@ -114,7 +115,7 @@ int bytes::compareTo(bytes &other) void bytes::saveFrom(const void *ptr_, size_t len_) { malloc(len_); - // Save as much as possible. (Helps unpacker::abort.) + // Save as much as possible. if (len_ > len) { assert(ptr == dummy); // error recovery @@ -161,7 +162,6 @@ byte *fillbytes::grow(size_t s) allocated = b.len; if (allocated != maxlen) { - assert(unpack_aborting()); b.len = nlen - s; // back up return dummy; // scribble during error recov. } diff --git a/depends/pack200/src/bytes.h b/depends/pack200/src/bytes.h index 3926f9f2..2e4a9daf 100644 --- a/depends/pack200/src/bytes.h +++ b/depends/pack200/src/bytes.h @@ -161,7 +161,7 @@ struct fillbytes b.len = 0; } int8_t *grow(size_t s); // grow so that limit() += s - int getByte(uint i) + int getByte(uint32_t i) { return *loc(i) & 0xFF; } diff --git a/depends/pack200/src/coding.cpp b/depends/pack200/src/coding.cpp index 32977e05..226ba458 100644 --- a/depends/pack200/src/coding.cpp +++ b/depends/pack200/src/coding.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include "defines.h" #include "bytes.h" @@ -53,12 +54,12 @@ extern coding basic_codings[]; #define IS_NEG_CODE(S, codeVal) ((((int)(codeVal) + 1) & ((1 << S) - 1)) == 0) -#define DECODE_SIGN_S1(ux) (((uint)(ux) >> 1) ^ -((int)(ux) & 1)) +#define DECODE_SIGN_S1(ux) (((uint32_t)(ux) >> 1) ^ -((int)(ux) & 1)) -static int decode_sign(int S, uint ux) +static int decode_sign(int S, uint32_t ux) { // == Coding.decodeSign32 assert(S > 0); - uint sigbits = (ux >> S); + uint32_t sigbits = (ux >> S); if (IS_NEG_CODE(S, ux)) return (int)(~sigbits); else @@ -90,9 +91,9 @@ coding *coding::init() return nullptr; // no 5-byte fixed-size coding // first compute the range of the coding, in 64 bits - jlong range = 0; + int64_t range = 0; { - jlong H_i = 1; + int64_t H_i = 1; for (int i = 0; i < B; i++) { range += H_i; @@ -106,7 +107,7 @@ coding *coding::init() int this_umax; // now, compute min and max - if (range >= ((jlong)1 << 32)) + if (range >= ((int64_t)1 << 32)) { this_umax = INT_MAX_VALUE; this->umin = INT_MIN_VALUE; @@ -121,13 +122,13 @@ coding *coding::init() if (S != 0 && range != 0) { int Smask = (1 << S) - 1; - jlong maxPosCode = range - 1; - jlong maxNegCode = range - 1; + int64_t maxPosCode = range - 1; + int64_t maxNegCode = range - 1; while (IS_NEG_CODE(S, maxPosCode)) --maxPosCode; while (!IS_NEG_CODE(S, maxNegCode)) --maxNegCode; - int maxPos = decode_sign(S, (uint)maxPosCode); + int maxPos = decode_sign(S, (uint32_t)maxPosCode); if (maxPos < 0) this->max = INT_MAX_VALUE; // 32-bit wraparound else @@ -135,7 +136,7 @@ coding *coding::init() if (maxNegCode < 0) this->min = 0; // No negative codings at all. else - this->min = decode_sign(S, (uint)maxNegCode); + this->min = decode_sign(S, (uint32_t)maxNegCode); } } @@ -163,7 +164,8 @@ coding *coding::findBySpec(int spec) break; } coding *ptr = NEW(coding, 1); - CHECK_NULL_0(ptr); + if (!ptr) + return nullptr; coding *c = ptr->initFrom(spec); if (c == nullptr) { @@ -207,25 +209,25 @@ void coding_method::reset(value_stream *state) } } -uint coding::parse(byte *&rp, int B, int H) +uint32_t coding::parse(byte *&rp, int B, int H) { int L = 256 - H; byte *ptr = rp; // hand peel the i==0 part of the loop: - uint b_i = *ptr++ & 0xFF; - if (B == 1 || b_i < (uint)L) + uint32_t b_i = *ptr++ & 0xFF; + if (B == 1 || b_i < (uint32_t)L) { rp = ptr; return b_i; } - uint sum = b_i; - uint H_i = H; + uint32_t sum = b_i; + uint32_t H_i = H; assert(B <= B_MAX); for (int i = 2; i <= B_MAX; i++) { // easy for compilers to unroll if desired b_i = *ptr++ & 0xFF; sum += b_i * H_i; - if (i == B || b_i < (uint)L) + if (i == B || b_i < (uint32_t)L) { rp = ptr; return sum; @@ -236,26 +238,26 @@ uint coding::parse(byte *&rp, int B, int H) return 0; } -uint coding::parse_lgH(byte *&rp, int B, int H, int lgH) +uint32_t coding::parse_lgH(byte *&rp, int B, int H, int lgH) { assert(H == (1 << lgH)); int L = 256 - (1 << lgH); byte *ptr = rp; // hand peel the i==0 part of the loop: - uint b_i = *ptr++ & 0xFF; - if (B == 1 || b_i < (uint)L) + uint32_t b_i = *ptr++ & 0xFF; + if (B == 1 || b_i < (uint32_t)L) { rp = ptr; return b_i; } - uint sum = b_i; - uint lg_H_i = lgH; + uint32_t sum = b_i; + uint32_t lg_H_i = lgH; assert(B <= B_MAX); for (int i = 2; i <= B_MAX; i++) { // easy for compilers to unroll if desired b_i = *ptr++ & 0xFF; sum += b_i << lg_H_i; - if (i == B || b_i < (uint)L) + if (i == B || b_i < (uint32_t)L) { rp = ptr; return sum; @@ -272,7 +274,7 @@ void coding::parseMultiple(byte *&rp, int N, byte *limit, int B, int H) { if (N < 0) { - abort("bad value count"); + unpack_abort("bad value count"); return; } byte *ptr = rp; @@ -281,7 +283,7 @@ void coding::parseMultiple(byte *&rp, int N, byte *limit, int B, int H) size_t len = (size_t)N * B; if (len / B != (size_t)N || ptr + len > limit) { - abort(ERB); + unpack_abort(ERB); return; } rp = ptr + len; @@ -312,7 +314,7 @@ void coding::parseMultiple(byte *&rp, int N, byte *limit, int B, int H) // do an error check here if (ptr > limit) { - abort(ERB); + unpack_abort(ERB); return; } } @@ -401,12 +403,12 @@ void value_stream::setCoding(coding *defc) } } -static int getPopValue(value_stream *self, uint uval) +static int getPopValue(value_stream *self, uint32_t uval) { if (uval > 0) { // note that the initial parse performed a range check - assert(uval <= (uint)self->cm->fVlength); + assert(uval <= (uint32_t)self->cm->fVlength); return self->cm->fValues[uval - 1]; } else @@ -422,7 +424,7 @@ int coding::sumInUnsignedRange(int x, int y) int range = (int)(umax + 1); assert(range > 0); x += y; - if (x != (int)((jlong)(x - y) + (jlong)y)) + if (x != (int)((int64_t)(x - y) + (int64_t)y)) { // 32-bit overflow interferes with range reduction. // Back off from the overflow by adding a multiple of range: @@ -461,9 +463,9 @@ int coding::sumInUnsignedRange(int x, int y) return x; } -static int getDeltaValue(value_stream *self, uint uval, bool isSubrange) +static int getDeltaValue(value_stream *self, uint32_t uval, bool isSubrange) { - assert((uint)(self->c.isSubrange) == (uint)isSubrange); + assert((uint32_t)(self->c.isSubrange) == (uint32_t)isSubrange); assert(self->c.isSubrange | self->c.isFullRange); if (isSubrange) return self->sum = self->c.sumInUnsignedRange(self->sum, (int)uval); @@ -499,7 +501,7 @@ int value_stream::getInt() } CODING_PRIVATE(c.spec); - uint uval; + uint32_t uval; enum { B5 = 5, @@ -546,19 +548,19 @@ int value_stream::getInt() assert(D == 1); uval = coding::parse(rp, B, H); if (S != 0) - uval = (uint)decode_sign(S, uval); + uval = (uint32_t)decode_sign(S, uval); return getDeltaValue(this, uval, (bool)c.isSubrange); case cmk_BHS1D1full: assert(S == 1 && D == 1 && c.isFullRange); uval = coding::parse(rp, B, H); - uval = (uint)DECODE_SIGN_S1(uval); + uval = (uint32_t)DECODE_SIGN_S1(uval); return getDeltaValue(this, uval, false); case cmk_BHS1D1sub: assert(S == 1 && D == 1 && c.isSubrange); uval = coding::parse(rp, B, H); - uval = (uint)DECODE_SIGN_S1(uval); + uval = (uint32_t)DECODE_SIGN_S1(uval); return getDeltaValue(this, uval, true); case cmk_DELTA5: @@ -583,7 +585,7 @@ int value_stream::getInt() uval = coding::parse(rp, B, H); if (S != 0) { - uval = (uint)decode_sign(S, uval); + uval = (uint32_t)decode_sign(S, uval); } if (D != 0) { @@ -592,7 +594,7 @@ int value_stream::getInt() sum = c.sumInUnsignedRange(sum, (int)uval); else sum += (int)uval; - uval = (uint)sum; + uval = (uint32_t)sum; } return getPopValue(this, uval); @@ -616,8 +618,8 @@ int value_stream::getInt() static int moreCentral(int x, int y) { // used to find end of Pop.{F} // Suggested implementation from the Pack200 specification: - uint kx = (x >> 31) ^ (x << 1); - uint ky = (y >> 31) ^ (y << 1); + uint32_t kx = (x >> 31) ^ (x << 1); + uint32_t ky = (y >> 31) ^ (y << 1); return (kx < ky ? x : y); } // static maybe_inline @@ -680,7 +682,7 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m to_free = foundc; // findBySpec may dynamically allocate if (foundc == nullptr) { - abort("illegal arb. coding"); + unpack_abort("illegal arbitrary coding"); return; } // and fall through @@ -699,13 +701,11 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m int N2 = (N >= 0) ? N - K : N; if (N == 0 || (N2 <= 0 && N2 != N)) { - abort("illegal run encoding"); - return; + unpack_abort("illegal run encoding"); } if ((mode & DISABLE_RUN) != 0) { - abort("illegal nested run encoding"); - return; + unpack_abort("illegal nested run encoding"); } // & Enc{ ACode } if ADef=0 (ABDef != 1) @@ -719,11 +719,11 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m { this->init(band_rp, band_limit, meta_rp, disRun, defc, K, valueSink); } - CHECK; // & Enc{ BCode } if BDef=0 (ABDef != 2) coding_method *tail = U_NEW(coding_method, 1); - CHECK_NULL(tail); + if (!tail) + return; tail->u = u; // The 'run' codings may be nested indirectly via 'pop' codings. @@ -764,13 +764,11 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m int TH = (256 - TL); if (N <= 0) { - abort("illegal pop encoding"); - return; + unpack_abort("illegal pop encoding"); } if ((mode & DISABLE_POP) != 0) { - abort("illegal nested pop encoding"); - return; + unpack_abort("illegal nested pop encoding"); } // No indirect nesting of 'pop', but 'run' is OK. @@ -796,7 +794,6 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m fValues = (u->saveTo(fvbuf, fValueSink.b), (int *)fvbuf.ptr); fVlength = fValueSink.length(); // i.e., the parameter K fValueSink.free(); - CHECK; // Skip the first {F} run in all subsequent passes. // The next call to this->init(...) will set vs0.rp to point after the {F}. @@ -812,12 +809,12 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m break; // found it tcode->free(); tcode = coding::findBySpec(B, TH); - CHECK_NULL(tcode); + if (!tcode) + return; } if (!(fVlength <= tcode->umax)) { - abort("pop.L value too small"); - return; + unpack_abort("pop.L value too small"); } this->init(band_rp, band_limit, NO_META, disPop, tcode, N, nullptr); tcode->free(); @@ -826,7 +823,6 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m { this->init(band_rp, band_limit, meta_rp, disPop, defc, N, nullptr); } - CHECK; // Count the number of zero tokens right now. // Also verify that they are in bounds. @@ -834,13 +830,12 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m value_stream vs = vs0; for (int i = 0; i < N; i++) { - uint val = vs.getInt(); + uint32_t val = vs.getInt(); if (val == 0) UN += 1; - if (!(val <= (uint)fVlength)) + if (!(val <= (uint32_t)fVlength)) { - abort("pop token out of range"); - return; + unpack_abort("pop token out of range"); } } vs.done(); @@ -849,7 +844,8 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m if (UN != 0) { uValues = U_NEW(coding_method, 1); - CHECK_NULL(uValues); + if (uValues == nullptr) + return; uValues->u = u; if (UDef != 0) { @@ -867,7 +863,7 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m int uop = (*meta_rp++ & 0xFF); if (uop > _meta_canon_max) // %%% Spec. requires the more strict (uop != _meta_default). - abort("bad meta-coding for empty pop/U"); + unpack_abort("bad meta-coding for empty pop/U"); } } @@ -901,8 +897,7 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m } else { - abort("bad meta-coding"); - return; + unpack_abort("bad meta-coding"); } // Common code here skips a series of values with one coding. @@ -926,7 +921,7 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m coding &c = vs0.c; CODING_PRIVATE(c.spec); // assert sane N - assert((uint)N < INT_MAX_VALUE || N == POP_FAVORED_N); + assert((uint32_t)N < INT_MAX_VALUE || N == POP_FAVORED_N); // Look at the values, or at least skip over them quickly. if (valueSink == nullptr) @@ -970,14 +965,12 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m if (valueSink->length() > 0 && (val == last || val == min)) //|| val == min2 break; valueSink->add(val); - CHECK; last = val; min = moreCentral(min, last); // min2 = moreCentral2(min2, last, min); } band_rp = vs.rp; } - CHECK; // Get an accurate upper limit now. vs0.rplimit = band_rp; diff --git a/depends/pack200/src/coding.h b/depends/pack200/src/coding.h index 5f017b9e..f9bd6ca2 100644 --- a/depends/pack200/src/coding.h +++ b/depends/pack200/src/coding.h @@ -84,11 +84,11 @@ struct coding static coding *findBySpec(int B, int H, int S = 0, int D = 0); static coding *findByIndex(int irregularCodingIndex); - static uint parse(byte *&rp, int B, int H); - static uint parse_lgH(byte *&rp, int B, int H, int lgH); + static uint32_t parse(byte *&rp, int B, int H); + static uint32_t parse_lgH(byte *&rp, int B, int H, int lgH); static void parseMultiple(byte *&rp, int N, byte *limit, int B, int H); - uint parse(byte *&rp) + uint32_t parse(byte *&rp) { return parse(rp, CODING_B(spec), CODING_H(spec)); } @@ -116,12 +116,6 @@ struct coding } void free(); // free self if isMalloc - - // error handling - static void abort(const char *msg = nullptr) - { - unpack_abort(msg); - } }; enum coding_method_kind @@ -224,10 +218,6 @@ struct value_stream return this + 1; } bool hasHelper(); - - // error handling - // inline void abort(const char* msg); - // inline void aborting(); }; struct coding_method @@ -254,17 +244,4 @@ struct coding_method // The value sink is used to collect output values, when desired. void init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int mode, coding *defc, int N, intlist *valueSink); - - // error handling - void abort(const char *msg) - { - unpack_abort(msg, u); - } - bool aborting() - { - return unpack_aborting(u); - } }; - -// inline void value_stream::abort(const char* msg) { cm->abort(msg); } -// inline void value_stream::aborting() { cm->aborting(); } diff --git a/depends/pack200/src/constants.h b/depends/pack200/src/constants.h index aeb3335d..2cc14b7d 100644 --- a/depends/pack200/src/constants.h +++ b/depends/pack200/src/constants.h @@ -51,7 +51,7 @@ // magic number for gzip streams (for processing pack200-gzip data) #define GZIP_MAGIC 0x1F8B0800 -#define GZIP_MAGIC_MASK 0xFFFFFF00 // last byte is variable "flg" field +#define GZIP_MAGIC_MASK 0xFFFFFF00 // last \bchar\b is variable "flg" field enum { diff --git a/depends/pack200/src/defines.h b/depends/pack200/src/defines.h index 63abae0a..cfe5fc28 100644 --- a/depends/pack200/src/defines.h +++ b/depends/pack200/src/defines.h @@ -32,39 +32,22 @@ #include #endif -#ifndef FULL -#define FULL 1 /* Adds <500 bytes to the zipped final product. */ -#endif - -#if FULL // define this if you want debugging and/or compile-time attributes -#define IF_FULL(x) x -#else -#define IF_FULL(x) /*x*/ -#endif - // Error messages that we have -#define ERROR_ENOMEM "Native allocation failed" +#define ERROR_ENOMEM "Memory allocation failed" #define ERROR_FORMAT "Corrupted pack file" #define ERROR_RESOURCE "Cannot extract resource file" #define ERROR_OVERFLOW "Internal buffer overflow" #define ERROR_INTERNAL "Internal error" -#define LOGFILE_STDOUT "-" -#define LOGFILE_STDERR "" - #define lengthof(array) (sizeof(array) / sizeof(array[0])) #define NEW(T, n) (T *) must_malloc((int)(scale_size(n, sizeof(T)))) #define U_NEW(T, n) (T *) u->alloc(scale_size(n, sizeof(T))) #define T_NEW(T, n) (T *) u->temp_alloc(scale_size(n, sizeof(T))) -// bytes and byte arrays - -typedef unsigned int uint; +typedef signed char byte; #ifdef _MSC_VER -typedef LONGLONG jlong; -typedef DWORDLONG julong; #define MKDIR(dir) mkdir(dir) #define getpid() _getpid() #define PATH_MAX MAX_PATH @@ -73,64 +56,10 @@ typedef DWORDLONG julong; #define tempname _tempname #define sleep Sleep #else -typedef signed char byte; -#ifdef _LP64 -typedef long jlong; -typedef long unsigned julong; -#else -typedef long long jlong; -typedef long long unsigned julong; -#endif #define MKDIR(dir) mkdir(dir, 0777); #endif /* Must cast to void *, then size_t, then int. */ #define ptrlowbits(x) ((int)(size_t)(void *)(x)) -/* Back and forth from jlong to pointer */ -#define ptr2jlong(x) ((jlong)(size_t)(void *)(x)) -#define jlong2ptr(x) ((void *)(size_t)(x)) - -// Keys used by Java: -#define UNPACK_DEFLATE_HINT "unpack.deflate.hint" - -#define COM_PREFIX "com.sun.java.util.jar.pack." -#define UNPACK_MODIFICATION_TIME COM_PREFIX "unpack.modification.time" -#define DEBUG_VERBOSE COM_PREFIX "verbose" - -#define ZIP_ARCHIVE_MARKER_COMMENT "PACK200" - -// The following are not known to the Java classes: -#define UNPACK_REMOVE_PACKFILE COM_PREFIX "unpack.remove.packfile" - -// Called from unpacker layers -#define _CHECK_DO(t, x) \ - { \ - if (t) \ - { \ - x; \ - } \ - } - -#define CHECK _CHECK_DO(aborting(), return) -#define CHECK_(y) _CHECK_DO(aborting(), return y) -#define CHECK_0 _CHECK_DO(aborting(), return 0) - -#define CHECK_NULL(p) _CHECK_DO((p) == nullptr, return) -#define CHECK_NULL_(y, p) _CHECK_DO((p) == nullptr, return y) -#define CHECK_NULL_0(p) _CHECK_DO((p) == nullptr, return 0) - -#define CHECK_COUNT(t) \ - if (t < 0) \ - { \ - abort("bad value count"); \ - } \ - CHECK - -#define STR_TRUE "true" -#define STR_FALSE "false" - -#define STR_TF(x) ((x) ? STR_TRUE : STR_FALSE) -#define BOOL_TF(x) (((x) != nullptr &&strcmp((x), STR_TRUE) == 0) ? true : false) - #define DEFAULT_ARCHIVE_MODTIME 1060000000 // Aug 04, 2003 5:26 PM PDT diff --git a/depends/pack200/src/main.cpp b/depends/pack200/src/main.cpp deleted file mode 100644 index ad46a2a2..00000000 --- a/depends/pack200/src/main.cpp +++ /dev/null @@ -1,489 +0,0 @@ -/* - * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "defines.h" -#include "bytes.h" -#include "utils.h" -#include "coding.h" -#include "bands.h" - -#include "constants.h" - -#include "zip.h" - -#include "unpack.h" - -int main(int argc, char **argv) -{ - return unpacker::run(argc, argv); -} - -unpacker *unpacker::non_mt_current = nullptr; -unpacker *unpacker::current() -{ - return non_mt_current; -} -static void set_current_unpacker(unpacker *u) -{ - unpacker::non_mt_current = u; -} - -// Callback for fetching data, Unix style. -static jlong read_input_via_stdio(unpacker *u, void *buf, jlong minlen, jlong maxlen) -{ - assert(minlen <= maxlen); // don't talk nonsense - jlong numread = 0; - char *bufptr = (char *)buf; - while (numread < minlen) - { - // read available input, up to buf.length or maxlen - int readlen = (1 << 16); - if (readlen > (maxlen - numread)) - readlen = (int)(maxlen - numread); - int nr = 0; - if (u->infileptr != nullptr) - { - nr = (int)fread(bufptr, 1, readlen, u->infileptr); - } - else - { -#ifndef WIN32 - // we prefer unbuffered inputs - nr = (int)read(u->infileno, bufptr, readlen); -#else - nr = (int)fread(bufptr, 1, readlen, stdin); -#endif - } - if (nr <= 0) - { - if (errno != EINTR) - break; - nr = 0; - } - numread += nr; - bufptr += nr; - assert(numread <= maxlen); - } - // fprintf(u->errstrm, "readInputFn(%d,%d) => %d\n", - // (int)minlen, (int)maxlen, (int)numread); - return numread; -} - -enum -{ - EOF_MAGIC = 0, - BAD_MAGIC = -1 -}; -static int read_magic(unpacker *u, char peek[], int peeklen) -{ - assert(peeklen == 4); // magic numbers are always 4 bytes - jlong nr = (u->read_input_fn)(u, peek, peeklen, peeklen); - if (nr != peeklen) - { - return (nr == 0) ? EOF_MAGIC : BAD_MAGIC; - } - int magic = 0; - for (int i = 0; i < peeklen; i++) - { - magic <<= 8; - magic += peek[i] & 0xFF; - } - return magic; -} - -static void setup_gzin(unpacker *u) -{ - gunzip *gzin = NEW(gunzip, 1); - gzin->init(u); -} - -static const char *nbasename(const char *progname) -{ - const char *slash = strrchr(progname, '/'); - if (slash != nullptr) - progname = ++slash; - return progname; -} - -static const char *usage_lines[] = { - "Usage: %s [-opt... | --option=value]... x.pack[.gz] y.jar\n", "\n", "Unpacking Options\n", - " -H{h}, --deflate-hint={h} override transmitted deflate hint: true, false, or keep " - "(default)\n", - " -r, --remove-pack-file remove input file after unpacking\n", - " -v, --verbose increase program verbosity\n", - " -q, --quiet set verbosity to lowest level\n", - " -l{F}, --log-file={F} output to the given log file, or '-' for standard output " - "(default)\n", - " -?, -h, --help print this message\n", - " -J{X} Java VM argument (ignored)\n", nullptr}; - -static void usage(unpacker *u, const char *progname, bool full = false) -{ - // WinMain does not set argv[0] to the progrname - progname = (progname != nullptr) ? nbasename(progname) : "unpack200"; - for (int i = 0; usage_lines[i] != nullptr; i++) - { - fprintf(stderr, usage_lines[i], progname); - if (!full) - { - fprintf(stderr, "(For more information, run %s --help .)\n", progname); - break; - } - } -} - -// argument parsing -static char **init_args(int argc, char **argv, int &envargc) -{ - const char *env = getenv("UNPACK200_FLAGS"); - ptrlist envargs; - envargs.init(); - if (env != nullptr) - { - char *buf = (char *)strdup(env); - const char *delim = "\n\t "; - for (char *p = strtok(buf, delim); p != nullptr; p = strtok(nullptr, delim)) - { - envargs.add(p); - } - } - // allocate extra margin at both head and tail - char **argp = NEW(char *, envargs.length() + argc + 1); - char **argp0 = argp; - int i; - for (i = 0; i < envargs.length(); i++) - { - *argp++ = (char *)envargs.get(i); - } - for (i = 1; i < argc; i++) - { - // note: skip argv[0] (program name) - *argp++ = (char *)strdup(argv[i]); // make a scratch copy - } - *argp = nullptr; // sentinel - envargc = envargs.length(); // report this count to next_arg - envargs.free(); - return argp0; -} - -static int strpcmp(const char *str, const char *pfx) -{ - return strncmp(str, pfx, strlen(pfx)); -} - -static const char flag_opts[] = "vqrVh?"; -static const char string_opts[] = "HlJ"; - -static int next_arg(char **&argp) -{ - char *arg = *argp; - if (arg == nullptr || arg[0] != '-') - { // end of option list - return 0; - } - // printf("opt: %s\n", arg); - char ach = arg[1]; - if (ach == '\0') - { - // ++argp; // do not pop this arg - return 0; // bare "-" is stdin/stdout - } - else if (arg[1] == '-') - { // --foo option - static const char *keys[] = {"Hdeflate-hint=", "vverbose", "qquiet", - "rremove-pack-file", "llog-file=", "Vversion", - "hhelp", nullptr}; - if (arg[2] == '\0') - { // end of option list - ++argp; // pop the "--" - return 0; - } - for (int i = 0; keys[i] != nullptr; i++) - { - const char *key = keys[i]; - char kch = *key++; - if (strchr(key, '=') == nullptr) - { - if (!strcmp(arg + 2, key)) - { - ++argp; // pop option arg - return kch; - } - } - else - { - if (!strpcmp(arg + 2, key)) - { - *argp += 2 + strlen(key); // remove "--"+key from arg - return kch; - } - } - } - } - else if (strchr(flag_opts, ach) != nullptr) - { // plain option - if (arg[2] == '\0') - { - ++argp; - } - else - { - // in-place edit of "-vxyz" to "-xyz" - arg += 1; // skip original '-' - arg[0] = '-'; - *argp = arg; - } - // printf(" key => %c\n", ach); - return ach; - } - else if (strchr(string_opts, ach) != nullptr) - { // argument-bearing option - if (arg[2] == '\0') - { - if (argp[1] == nullptr) - return -1; // no next arg - ++argp; // leave the argument in place - } - else - { - // in-place edit of "-Hxyz" to "xyz" - arg += 2; // skip original '-H' - *argp = arg; - } - // printf(" key => %c\n", ach); - return ach; - } - return -1; // bad argument -} - -static const char sccsver[] = "1.30, 07/05/05"; - -// Usage: unpackage input.pack output.jar -int unpacker::run(int argc, char **argv) -{ - unpacker u; - u.init(read_input_via_stdio); - set_current_unpacker(&u); - - jar jarout; - jarout.init(&u); - - int envargc = 0; - char **argbuf = init_args(argc, argv, envargc); - char **arg0 = argbuf + envargc; - char **argp = argbuf; - - int verbose = 0; - char *logfile = nullptr; - - for (;;) - { - const char *arg = (*argp == nullptr) ? "" : u.saveStr(*argp); - bool isenvarg = (argp < arg0); - int ach = next_arg(argp); - bool hasoptarg = (ach != 0 && strchr(string_opts, ach) != nullptr); - if (ach == 0 && argp >= arg0) - break; - if (isenvarg && argp == arg0 && hasoptarg) - ach = 0; // don't pull from cmdline - switch (ach) - { - case 'H': - u.set_option(UNPACK_DEFLATE_HINT, *argp++); - break; - case 'v': - ++verbose; - break; - case 'q': - verbose = 0; - break; - case 'r': - u.set_option(UNPACK_REMOVE_PACKFILE, "1"); - break; - case 'l': - logfile = *argp++; - break; - case 'J': - argp += 1; - break; // skip ignored -Jxxx parameter - - case 'h': - case '?': - usage(&u, argv[0], true); - exit(1); - - default: - const char *inenv = isenvarg ? " in ${UNPACK200_FLAGS}" : ""; - if (hasoptarg) - fprintf(stderr, "Missing option string%s: %s\n", inenv, arg); - else - fprintf(stderr, "Unrecognized argument%s: %s\n", inenv, arg); - usage(&u, argv[0]); - exit(2); - } - } - - if (verbose != 0) - { - u.set_option(DEBUG_VERBOSE, u.saveIntStr(verbose)); - } - - const char *source_file = *argp++; - const char *destination_file = *argp++; - - if (source_file == nullptr || destination_file == nullptr || *argp != nullptr) - { - usage(&u, argv[0]); - exit(2); - } - - if (verbose != 0) - { - fprintf(stderr, "Unpacking from %s to %s\n", source_file, destination_file); - } - bool &remove_source = u.remove_packfile; - - if (strcmp(source_file, "-") == 0) - { - remove_source = false; - u.infileno = fileno(stdin); - } - else - { - u.infileptr = fopen(source_file, "rb"); - if (u.infileptr == nullptr) - { - fprintf(stderr, "Error: Could not open input file: %s\n", source_file); - exit(3); // Called only from the native standalone unpacker - } - } - - if (strcmp(destination_file, "-") == 0) - { - jarout.jarfp = stdout; - } - else - { - jarout.openJarFile(destination_file); - assert(jarout.jarfp != nullptr); - } - - if (verbose != 0) - u.dump_options(); - - char peek[4]; - int magic; - - // check for GZIP input - magic = read_magic(&u, peek, (int)sizeof(peek)); - if ((magic & GZIP_MAGIC_MASK) == GZIP_MAGIC) - { - // Oops; must slap an input filter on this data. - setup_gzin(&u); - u.gzin->start(magic); - if (!u.aborting()) - { - u.start(); - } - } - else - { - u.start(peek, sizeof(peek)); - } - - // Note: The checks to u.aborting() are necessary to gracefully - // terminate processing when the first segment throws an error. - - for (;;) - { - if (u.aborting()) - break; - - // Each trip through this loop unpacks one segment - // and then resets the unpacker. - for (unpacker::file *filep; (filep = u.get_next_file()) != nullptr;) - { - if (u.aborting()) - break; - u.write_file_to_jar(filep); - } - if (u.aborting()) - break; - - // Peek ahead for more data. - magic = read_magic(&u, peek, (int)sizeof(peek)); - if (magic != (int)JAVA_PACKAGE_MAGIC) - { - if (magic != EOF_MAGIC) - u.abort("garbage after end of pack archive"); - break; // all done - } - - // Release all storage from parsing the old segment. - u.reset(); - - // Restart, beginning with the peek-ahead. - u.start(peek, sizeof(peek)); - } - - int status = 0; - if (u.aborting()) - { - fprintf(stderr, "Error: %s\n", u.get_abort_message()); - status = 1; - } - - if (u.infileptr != nullptr) - { - fclose(u.infileptr); - u.infileptr = nullptr; - } - - if (!u.aborting() && remove_source) - remove(source_file); - - if (verbose != 0) - { - fprintf(stderr, "unpacker completed with status=%d\n", status); - } - - u.finish(); - - u.free(); // tidy up malloc blocks - set_current_unpacker(nullptr); // clean up global pointer - - return status; -} diff --git a/depends/pack200/src/unpack.cpp b/depends/pack200/src/unpack.cpp index a562d442..8a66d42a 100644 --- a/depends/pack200/src/unpack.cpp +++ b/depends/pack200/src/unpack.cpp @@ -51,6 +51,7 @@ #include #include #include +#include #include "defines.h" #include "bytes.h" @@ -79,23 +80,15 @@ enum REQUESTED_LDC = -1 }; -#define NO_INORD ((uint) - 1) +#define NO_INORD ((uint32_t) - 1) struct entry { byte tag; - -#if 0 - byte bits; - enum { - //EB_EXTRA = 1, - EB_SUPER = 2 - }; -#endif unsigned short nrefs; // pack w/ tag int outputIndex; - uint inord; // &cp.entries[cp.tag_base[this->tag]+this->inord] == this + uint32_t inord; // &cp.entries[cp.tag_base[this->tag]+this->inord] == this entry **refs; @@ -104,10 +97,10 @@ struct entry { bytes b; int i; - jlong l; + int64_t l; } value; - void requestOutputIndex(cpool &cp, int req = REQUESTED); + void requestOutputIndex(constant_pool &cp, int req = REQUESTED); int getOutputIndex() { assert(outputIndex > NOT_REQUESTED); @@ -116,7 +109,7 @@ struct entry entry *ref(int refnum) { - assert((uint)refnum < nrefs); + assert((uint32_t)refnum < nrefs); return refs[refnum]; } @@ -178,11 +171,15 @@ struct entry bool tagMatches(byte tag2) { - return (tag2 == tag) || (tag2 == CONSTANT_Utf8 && tag == CONSTANT_Signature); + return (tag2 == tag) || (tag2 == CONSTANT_Utf8 && tag == CONSTANT_Signature) || + (tag2 == CONSTANT_Literal && tag >= CONSTANT_Integer && tag <= CONSTANT_String && + tag != CONSTANT_Class) || + (tag2 == CONSTANT_Member && tag >= CONSTANT_Fieldref && + tag <= CONSTANT_InterfaceMethodref); } }; -entry *cpindex::get(uint i) +entry *cpindex::get(uint32_t i) { if (i >= len) return nullptr; @@ -250,16 +247,16 @@ int entry::typeSize() } } -inline cpindex *cpool::getFieldIndex(entry *classRef) +inline cpindex *constant_pool::getFieldIndex(entry *classRef) { assert(classRef->tagMatches(CONSTANT_Class)); - assert((uint)classRef->inord < (uint)tag_count[CONSTANT_Class]); + assert((uint32_t)classRef->inord < (uint32_t)tag_count[CONSTANT_Class]); return &member_indexes[classRef->inord * 2 + 0]; } -inline cpindex *cpool::getMethodIndex(entry *classRef) +inline cpindex *constant_pool::getMethodIndex(entry *classRef) { assert(classRef->tagMatches(CONSTANT_Class)); - assert((uint)classRef->inord < (uint)tag_count[CONSTANT_Class]); + assert((uint32_t)classRef->inord < (uint32_t)tag_count[CONSTANT_Class]); return &member_indexes[classRef->inord * 2 + 1]; } @@ -277,7 +274,6 @@ struct inner_class void unpacker::free() { int i; - assert(infileptr == nullptr); // caller resp. if (jarout != nullptr) jarout->reset(); if (gzin != nullptr) @@ -287,7 +283,9 @@ void unpacker::free() } if (free_input) input.free(); - // free everybody ever allocated with U_NEW or (recently) with T_NEW + /* + * free everybody ever allocated with U_NEW or (recently) with T_NEW + */ assert(smallbuf.base() == nullptr || mallocs.contains(smallbuf.base())); assert(tsmallbuf.base() == nullptr || tmallocs.contains(tsmallbuf.base())); mallocs.freeAll(); @@ -318,10 +316,10 @@ void unpacker::free() // Will eagerly read ahead by larger chunks, if possible. // Returns false if (rplimit-rp) is not at least 'more', // unless rplimit hits input.limit(). -bool unpacker::ensure_input(jlong more) +bool unpacker::ensure_input(int64_t more) { - julong want = more - input_remaining(); - if ((jlong)want <= 0) + uint64_t want = more - input_remaining(); + if ((int64_t)want <= 0) return true; // it's already in the buffer if (rplimit == input.limit()) return true; // not expecting any more @@ -333,23 +331,22 @@ bool unpacker::ensure_input(jlong more) rplimit = input.limit(); return true; } - CHECK_0; - julong remaining = (input.limit() - rplimit); // how much left to read? + uint64_t remaining = (input.limit() - rplimit); // how much left to read? byte *rpgoal = (want >= remaining) ? input.limit() : rplimit + (size_t)want; enum { CHUNK_SIZE = (1 << 14) }; - julong fetch = want; + uint64_t fetch = want; if (fetch < CHUNK_SIZE) fetch = CHUNK_SIZE; if (fetch > remaining * 3 / 4) fetch = remaining; // Try to fetch at least "more" bytes. - while ((jlong)fetch > 0) + while ((int64_t)fetch > 0) { - jlong nr = (*read_input_fn)(this, rplimit, fetch, remaining); + int64_t nr = (*read_input_fn)(this, rplimit, fetch, remaining); if (nr <= 0) { return (rplimit >= rpgoal); @@ -358,7 +355,7 @@ bool unpacker::ensure_input(jlong more) rplimit += nr; fetch -= nr; bytes_read += nr; - assert(remaining == (julong)(input.limit() - rplimit)); + assert(remaining == (uint64_t)(input.limit() - rplimit)); } return true; } @@ -434,10 +431,10 @@ void unpacker::putu4_at(byte *wp, int n) wp[3] = (n) >> 0; } -void unpacker::putu8_at(byte *wp, jlong n) +void unpacker::putu8_at(byte *wp, int64_t n) { - putu4_at(wp + 0, (int)((julong)n >> 32)); - putu4_at(wp + 4, (int)((julong)n >> 0)); + putu4_at(wp + 0, (int)((uint64_t)n >> 32)); + putu4_at(wp + 4, (int)((uint64_t)n >> 0)); } void unpacker::putu2(int n) @@ -450,7 +447,7 @@ void unpacker::putu4(int n) putu4_at(put_space(4), n); } -void unpacker::putu8(jlong n) +void unpacker::putu8(int64_t n) { putu8_at(put_space(8), n); } @@ -521,11 +518,6 @@ void *unpacker::alloc_heap(size_t size, bool smallOK, bool temp) void unpacker::saveTo(bytes &b, byte *ptr, size_t len) { b.ptr = U_NEW(byte, add_size(len, 1)); - if (aborting()) - { - b.len = 0; - return; - } b.len = len; b.copyFrom(ptr, len); } @@ -591,8 +583,7 @@ void unpacker::read_file_header() // Therefore, the caller must use only a bare minimum of read-ahead. if (inbytes.len > FIRST_READ) { - abort("too much read-ahead"); - return; + unpack_abort("too much read-ahead"); } input.set(initbuf, sizeof(initbuf)); input.b.clear(); @@ -605,7 +596,7 @@ void unpacker::read_file_header() // but is certain not to overflow past the archive_header. input.b.len = FIRST_READ; if (!ensure_input(FIRST_READ)) - abort("EOF reading archive magic number"); + unpack_abort("EOF reading archive magic number"); if (rp[0] == 'P' && rp[1] == 'K') { @@ -621,7 +612,6 @@ void unpacker::read_file_header() { // Get some breathing room. input.set(U_NEW(byte, (size_t)CHUNK + C_SLOP), (size_t)CHUNK); - CHECK; } rp = rplimit = input.base(); if (!ensure_input(1)) @@ -658,9 +648,8 @@ void unpacker::read_file_header() magic, majver, minver, JAVA_PACKAGE_MAGIC, JAVA5_PACKAGE_MAJOR_VERSION, JAVA5_PACKAGE_MINOR_VERSION, JAVA_PACKAGE_MAGIC, JAVA6_PACKAGE_MAJOR_VERSION, JAVA6_PACKAGE_MINOR_VERSION); - abort(message); + unpack_abort(message); } - CHECK; archive_options = hdr.getInt(); hdrVals += 1; @@ -672,15 +661,15 @@ void unpacker::read_file_header() if ((archive_options & ~OPTION_LIMIT) != 0) { fprintf(stderr, "Warning: Illegal archive options 0x%x\n", archive_options); - abort("illegal archive options"); + unpack_abort("illegal archive options"); return; } if ((archive_options & AO_HAVE_FILE_HEADERS) != 0) { - uint hi = hdr.getInt(); - uint lo = hdr.getInt(); - julong x = band::makeLong(hi, lo); + uint32_t hi = hdr.getInt(); + uint32_t lo = hdr.getInt(); + uint64_t x = band::makeLong(hi, lo); archive_size = (size_t)x; if (archive_size != x) { @@ -701,12 +690,11 @@ void unpacker::read_file_header() int header_size_1 = (int)(rplimit - rp); // buffered unused initial fragment int header_size = header_size_0 + header_size_1; unsized_bytes_read = header_size_0; - CHECK; if (foreign_buf) { if (archive_size > (size_t)header_size_1) { - abort("EOF reading fixed input buffer"); + unpack_abort("EOF reading fixed input buffer"); return; } } @@ -714,17 +702,16 @@ void unpacker::read_file_header() { if (archive_size < ARCHIVE_SIZE_MIN) { - abort("impossible archive size"); // bad input data + unpack_abort("impossible archive size"); // bad input data return; } if (archive_size < header_size_1) { - abort("too much read-ahead"); // somehow we pre-fetched too much? + unpack_abort("too much read-ahead"); // somehow we pre-fetched too much? return; } input.set(U_NEW(byte, add_size(header_size_0, archive_size, C_SLOP)), (size_t)header_size_0 + archive_size); - CHECK; assert(input.limit()[0] == 0); // Move all the bytes we read initially into the real buffer. input.b.copyFrom(initbuf, header_size); @@ -736,19 +723,16 @@ void unpacker::read_file_header() // It's more complicated and painful. // A zero archive_size means that we must read until EOF. input.init(CHUNK * 2); - CHECK; input.b.len = input.allocated; rp = rplimit = input.base(); // Set up input buffer as if we already read the header: input.b.copyFrom(initbuf, header_size); - CHECK; rplimit += header_size; while (ensure_input(input.limit() - rp)) { size_t dataSoFar = input_remaining(); size_t nextSize = add_size(dataSoFar, CHUNK); input.ensureSize(nextSize); - CHECK; input.b.len = input.allocated; rp = rplimit = input.base(); rplimit += dataSoFar; @@ -756,7 +740,6 @@ void unpacker::read_file_header() size_t dataSize = (rplimit - input.base()); input.b.len = dataSize; input.grow(C_SLOP); - CHECK; free_input = true; // free it later input.b.len = dataSize; assert(input.limit()[0] == 0); @@ -765,25 +748,21 @@ void unpacker::read_file_header() rp += header_size_0; // already scanned these bytes... } live_input = true; // mark as "do not reuse" - if (aborting()) - { - abort("cannot allocate large input buffer for package file"); - return; - } // read the rest of the header fields ensure_input((AH_LENGTH - AH_LENGTH_0) * B_MAX); - CHECK; hdr.rp = rp; hdr.rplimit = rplimit; if ((archive_options & AO_HAVE_FILE_HEADERS) != 0) { archive_next_count = hdr.getInt(); - CHECK_COUNT(archive_next_count); + if (archive_next_count < 0) + unpack_abort("bad archive_next_count"); archive_modtime = hdr.getInt(); file_count = hdr.getInt(); - CHECK_COUNT(file_count); + if (file_count < 0) + unpack_abort("bad file_count"); hdrVals += 3; } else @@ -794,9 +773,11 @@ void unpacker::read_file_header() if ((archive_options & AO_HAVE_SPECIAL_FORMATS) != 0) { band_headers_size = hdr.getInt(); - CHECK_COUNT(band_headers_size); + if (band_headers_size < 0) + unpack_abort("bad band_headers_size"); attr_definition_count = hdr.getInt(); - CHECK_COUNT(attr_definition_count); + if (attr_definition_count < 0) + unpack_abort("bad attr_definition_count"); hdrVals += 2; } else @@ -821,16 +802,22 @@ void unpacker::read_file_header() } } cp_counts[k] = hdr.getInt(); - CHECK_COUNT(cp_counts[k]); + if (cp_counts[k] < 0) + unpack_abort("bad cp_counts"); hdrVals += 1; } ic_count = hdr.getInt(); - CHECK_COUNT(ic_count); + if (ic_count < 0) + unpack_abort("bad ic_count"); + default_class_minver = hdr.getInt(); default_class_majver = hdr.getInt(); + class_count = hdr.getInt(); - CHECK_COUNT(class_count); + if (class_count < 0) + unpack_abort("bad class_count"); + hdrVals += 4; // done with archive_header @@ -839,11 +826,10 @@ void unpacker::read_file_header() rp = hdr.rp; if (rp > rplimit) - abort("EOF reading archive header"); + unpack_abort("EOF reading archive header"); // Now size the CP. cp.init(this, cp_counts); - CHECK; default_file_modtime = archive_modtime; if (default_file_modtime == 0 && !(archive_options & AO_HAVE_FILE_MODTIME)) @@ -856,13 +842,13 @@ void unpacker::read_file_header() ensure_input(band_headers_size); if (input_remaining() < (size_t)band_headers_size) { - abort("EOF reading band headers"); + unpack_abort("EOF reading band headers"); return; } bytes band_headers; // The "1+" allows an initial byte to be pushed on the front. band_headers.set(1 + U_NEW(byte, 1 + band_headers_size + C_SLOP), band_headers_size); - CHECK; + // Start scanning band headers here: band_headers.copyFrom(rp, band_headers.len); rp += band_headers.len; @@ -891,7 +877,7 @@ void unpacker::finish() } // Cf. PackageReader.readConstantPoolCounts -void cpool::init(unpacker *u_, int counts[NUM_COUNTS]) +void constant_pool::init(unpacker *u_, int counts[NUM_COUNTS]) { this->u = u_; @@ -915,8 +901,7 @@ void cpool::init(unpacker *u_, int counts[NUM_COUNTS]) }; if (len >= (1 << 29) || len < 0 || next_entry >= CP_SIZE_LIMIT + IMPLICIT_ENTRY_COUNT) { - abort("archive too large: constant pool limit exceeded"); - return; + unpack_abort("archive too large: constant pool limit exceeded"); } } @@ -937,7 +922,6 @@ void cpool::init(unpacker *u_, int counts[NUM_COUNTS]) // the entries are renumbered for classfile output. entries = U_NEW(entry, maxentries); - CHECK; first_extra_entry = &entries[nentries]; @@ -951,8 +935,8 @@ void cpool::init(unpacker *u_, int counts[NUM_COUNTS]) } // Initialize hashTab to a generous power-of-two size. - uint pow2 = 1; - uint target = maxentries + maxentries / 2; // 60% full + uint32_t pow2 = 1; + uint32_t target = maxentries + maxentries / 2; // 60% full while (pow2 < target) pow2 <<= 1; hashTab = U_NEW(entry *, hashTabLength = pow2); @@ -1056,7 +1040,6 @@ void unpacker::read_Utf8_values(entry *cpMap, int len) cp_Utf8_suffix.readData(len - SUFFIX_SKIP_1); bytes *allsuffixes = T_NEW(bytes, len); - CHECK; int nbigsuf = 0; fillbytes charbuf; // buffer to allocate small strings @@ -1069,8 +1052,7 @@ void unpacker::read_Utf8_values(entry *cpMap, int len) int suffix = (i < SUFFIX_SKIP_1) ? 0 : cp_Utf8_suffix.getInt(); if (suffix < 0) { - abort("bad utf8 suffix"); - return; + unpack_abort("bad utf8 suffix"); } if (suffix == 0 && i >= SUFFIX_SKIP_1) { @@ -1079,7 +1061,7 @@ void unpacker::read_Utf8_values(entry *cpMap, int len) continue; } bytes &chars = allsuffixes[i]; - uint size3 = suffix * 3; // max Utf8 length + uint32_t size3 = suffix * 3; // max Utf8 length bool isMalloc = (suffix > SMALL); if (isMalloc) { @@ -1095,7 +1077,7 @@ void unpacker::read_Utf8_values(entry *cpMap, int len) } chars.set(charbuf.grow(size3 + 1), size3); } - CHECK; + byte *chp = chars.ptr; for (int j = 0; j < suffix; j++) { @@ -1106,7 +1088,6 @@ void unpacker::read_Utf8_values(entry *cpMap, int len) if (isMalloc) { chars.realloc(chp - chars.ptr); - CHECK; tmallocs.add(chars.ptr); // free it later } else @@ -1131,8 +1112,7 @@ void unpacker::read_Utf8_values(entry *cpMap, int len) int prefix = (i < PREFIX_SKIP_2) ? 0 : cp_Utf8_prefix.getInt(); if (prefix < 0 || prefix + suffix < 0) { - abort("bad utf8 prefix"); - return; + unpack_abort("bad utf8 prefix"); } bytes &chars = allsuffixes[i]; if (suffix == 0 && i >= SUFFIX_SKIP_1) @@ -1161,7 +1141,7 @@ void unpacker::read_Utf8_values(entry *cpMap, int len) if (chars.ptr != nullptr) continue; // already input int suffix = (int)chars.len; // pick up the hack - uint size3 = suffix * 3; + uint32_t size3 = suffix * 3; if (suffix == 0) continue; // done with empty string chars.malloc(size3); @@ -1174,7 +1154,6 @@ void unpacker::read_Utf8_values(entry *cpMap, int len) chp = store_Utf8_char(chp, ch); } chars.realloc(chp - chars.ptr); - CHECK; tmallocs.add(chars.ptr); // free it later // cp_Utf8_big_chars.done(); cp_Utf8_big_chars = saved_band; // reset the band for the next string @@ -1185,9 +1164,8 @@ void unpacker::read_Utf8_values(entry *cpMap, int len) // Finally, sew together all the prefixes and suffixes. bytes bigbuf; bigbuf.malloc(maxlen * 3 + 1); // max Utf8 length, plus slop for nullptr - CHECK; - int prevlen = 0; // previous string length (in chars) - tmallocs.add(bigbuf.ptr); // free after this block + int prevlen = 0; // previous string length (in chars) + tmallocs.add(bigbuf.ptr); // free after this block cp_Utf8_prefix.rewind(); for (i = 0; i < len; i++) { @@ -1199,7 +1177,7 @@ void unpacker::read_Utf8_values(entry *cpMap, int len) // make sure the prefix value is not corrupted, though: if (prefix > prevlen) { - abort("utf8 prefix overflow"); + unpack_abort("utf8 prefix overflow"); return; } fillp = skip_Utf8_chars(bigbuf.ptr, prefix); @@ -1211,7 +1189,6 @@ void unpacker::read_Utf8_values(entry *cpMap, int len) bytes &value = cpMap[i].value.b; value.set(U_NEW(byte, add_size(length, 1)), length); value.copyFrom(bigbuf.ptr, length); - CHECK; // Index all Utf8 strings entry *&htref = cp.hashTabRef(CONSTANT_Utf8, value); if (htref == nullptr) @@ -1256,14 +1233,12 @@ void unpacker::read_single_refs(band &cp_band, byte refTag, entry *cpMap, int le assert(refTag == CONSTANT_Utf8); cp_band.setIndexByTag(refTag); cp_band.readData(len); - CHECK; int indexTag = (cp_band.bn == e_cp_Class) ? CONSTANT_Class : 0; for (int i = 0; i < len; i++) { entry &e = cpMap[i]; e.refs = U_NEW(entry *, e.nrefs = 1); entry *utf = cp_band.getRef(); - CHECK; e.refs[0] = utf; e.value.b = utf->value.b; // copy value of Utf8 string to self if (indexTag != 0) @@ -1290,14 +1265,12 @@ void unpacker::read_double_refs(band &cp_band, byte ref1Tag, byte ref2Tag, entry cp_band2.setIndexByTag(ref2Tag); cp_band1.readData(len); cp_band2.readData(len); - CHECK; for (int i = 0; i < len; i++) { entry &e = cpMap[i]; e.refs = U_NEW(entry *, e.nrefs = 2); e.refs[0] = cp_band1.getRef(); e.refs[1] = cp_band2.getRef(); - CHECK; } // cp_band1.done(); // cp_band2.done(); @@ -1308,14 +1281,12 @@ void unpacker::read_signature_values(entry *cpMap, int len) { cp_Signature_form.setIndexByTag(CONSTANT_Utf8); cp_Signature_form.readData(len); - CHECK; int ncTotal = 0; int i; for (i = 0; i < len; i++) { entry &e = cpMap[i]; entry &form = *cp_Signature_form.getRef(); - CHECK; int nc = 0; for (const char *ncp = form.utf8String(); *ncp; ncp++) @@ -1326,7 +1297,6 @@ void unpacker::read_signature_values(entry *cpMap, int len) ncTotal += nc; e.refs = U_NEW(entry *, cpMap[i].nrefs = 1 + nc); - CHECK; e.refs[0] = &form; } // cp_Signature_form.done(); @@ -1338,7 +1308,6 @@ void unpacker::read_signature_values(entry *cpMap, int len) for (int j = 1; j < e.nrefs; j++) { e.refs[j] = cp_Signature_classes.getRef(); - CHECK; } } // cp_Signature_classes.done(); @@ -1410,19 +1379,16 @@ void unpacker::read_cp() assert(false); break; } - CHECK; } cp.expandSignatures(); - CHECK; cp.initMemberIndexes(); - CHECK; #define SNAME(n, s) #s "\0" const char *symNames = (ALL_ATTR_DO(SNAME) ""); #undef SNAME - for (int sn = 0; sn < cpool::s_LIMIT; sn++) + for (int sn = 0; sn < constant_pool::s_LIMIT; sn++) { assert(symNames[0] >= '0' && symNames[0] <= 'Z'); // sanity bytes name; @@ -1469,7 +1435,6 @@ unpacker::attr_definitions::defineLayout(int idx, entry *nameEntry, const char * { const char *name = nameEntry->value.b.strval(); layout_definition *lo = defineLayout(idx, name, layout); - CHECK_0; lo->nameEntry = nameEntry; return lo; } @@ -1482,10 +1447,10 @@ unpacker::layout_definition *unpacker::attr_definitions::defineLayout(int idx, c { // Fixed attr. if (idx >= (int)flag_limit) - abort("attribute index too large"); + unpack_abort("attribute index too large"); if (isRedefined(idx)) - abort("redefined attribute index"); - redef |= ((julong)1 << idx); + unpack_abort("redefined attribute index"); + redef |= ((uint64_t)1 << idx); } else { @@ -1493,7 +1458,6 @@ unpacker::layout_definition *unpacker::attr_definitions::defineLayout(int idx, c overflow_count.add(0); // make a new counter } layout_definition *lo = U_NEW(layout_definition, 1); - CHECK_0; lo->idx = idx; lo->name = name; lo->layout = layout; @@ -1501,7 +1465,6 @@ unpacker::layout_definition *unpacker::attr_definitions::defineLayout(int idx, c { layouts.add(nullptr); } - CHECK_0; layouts.get(idx) = lo; return lo; } @@ -1522,13 +1485,11 @@ band **unpacker::attr_definitions::buildBands(unpacker::layout_definition *lo) bands_made = 0x10000; // base number for bands made const char *lp = lo->layout; lp = parseLayout(lp, lo->elems, -1); - CHECK_0; if (lp[0] != '\0' || band_stack.length() > 0) { - abort("garbage at end of layout"); + unpack_abort("garbage at end of layout"); } band_stack.popTo(0); - CHECK_0; // Fix up callables to point at their callees. band **bands = lo->elems; @@ -1540,7 +1501,7 @@ band **unpacker::attr_definitions::buildBands(unpacker::layout_definition *lo) { if (bands[num_callables]->le_kind != EK_CBLE) { - abort("garbage mixed with callables"); + unpack_abort("garbage mixed with callables"); break; } num_callables += 1; @@ -1554,7 +1515,7 @@ band **unpacker::attr_definitions::buildBands(unpacker::layout_definition *lo) int call_num = call.le_len; if (call_num < 0 || call_num >= num_callables) { - abort("bad call in layout"); + unpack_abort("bad call in layout"); break; } band &cble = *bands[call_num]; @@ -1636,7 +1597,6 @@ const char *unpacker::attr_definitions::parseIntLayout(const char *lp, band *&re { const char *lp0 = lp; band *b = U_NEW(band, 1); - CHECK_(lp); char le = *lp++; int spec = UNSIGNED5_spec; if (le == 'S' && can_be_signed) @@ -1667,7 +1627,7 @@ const char *unpacker::attr_definitions::parseIntLayout(const char *lp, band *&re le_len = 0; break; default: - abort("bad layout element"); + unpack_abort("bad layout element"); } b->le_len = le_len; band_stack.add(b); @@ -1704,15 +1664,13 @@ const char *unpacker::attr_definitions::parseNumeral(const char *lp, int &res) } if (lp == dp) { - abort("missing numeral in layout"); - return ""; + unpack_abort("missing numeral in layout"); } lp = dp; if (con < 0 && !(sgn && con == -con)) { // (Portability note: Misses the error if int is not 32 bits.) - abort("numeral overflow"); - return ""; + unpack_abort("numeral overflow"); } if (sgn) con = -con; @@ -1732,7 +1690,6 @@ band **unpacker::attr_definitions::popBody(int bs_base) { int nb = bs_limit - bs_base; band **res = U_NEW(band *, add_size(nb, 1)); - CHECK_(no_bands); for (int i = 0; i < nb; i++) { band *b = (band *)band_stack.get(bs_base + i); @@ -1789,12 +1746,11 @@ const char *unpacker::attr_definitions::parseLayout(const char *lp, band **&res, b->le_bci = EK_BCO; b->defc = coding::findBySpec(BRANCH5_spec); break; - case 'N': // replication: 'N' uint '[' elem ... ']' + case 'N': // replication: 'N' uint32_t '[' elem ... ']' lp = parseIntLayout(lp, b, EK_REPL); assert(*lp == '['); ++lp; lp = parseLayout(lp, b->le_body, curCble); - CHECK_(lp); break; case 'T': // union: 'T' any_int union_case* '(' ')' '[' body ']' lp = parseIntLayout(lp, b, EK_UN, can_be_signed); @@ -1803,13 +1759,12 @@ const char *unpacker::attr_definitions::parseLayout(const char *lp, band **&res, for (;;) { // for each case band &k_case = *U