Turn pack200 into an actual library

This commit is contained in:
Petr Mrázek 2013-09-29 21:11:30 +02:00
parent d267d86f6e
commit 604162acdf
20 changed files with 583 additions and 1352 deletions

View File

@ -19,6 +19,7 @@ ELSE(UNIX)
ENDIF(UNIX) ENDIF(UNIX)
SET(PACK200_SRC SET(PACK200_SRC
include/unpack200.h
src/bands.cpp src/bands.cpp
src/bands.h src/bands.h
src/bytes.cpp src/bytes.cpp
@ -27,7 +28,7 @@ src/coding.cpp
src/coding.h src/coding.h
src/constants.h src/constants.h
src/defines.h src/defines.h
src/main.cpp src/unpack200.cpp
src/unpack.cpp src/unpack.cpp
src/unpack.h src/unpack.h
src/utils.cpp src/utils.cpp
@ -36,7 +37,9 @@ src/zip.cpp
src/zip.h src/zip.h
) )
add_executable(unpack200 ${PACK200_SRC}) include_directories(include)
add_library(unpack200 STATIC ${PACK200_SRC})
IF(UNIX) IF(UNIX)
target_link_libraries(unpack200 ${ZLIB_LIBRARIES}) target_link_libraries(unpack200 ${ZLIB_LIBRARIES})
@ -44,3 +47,6 @@ ELSE()
# zlib is part of Qt on windows. use it. # zlib is part of Qt on windows. use it.
QT5_USE_MODULES(unpack200 Core) QT5_USE_MODULES(unpack200 Core)
ENDIF() ENDIF()
add_executable(anti200 anti200.cpp)
target_link_libraries(anti200 unpack200)

View File

@ -0,0 +1,28 @@
/*
* This is trivial. Do what thou wilt with it. Public domain.
*/
#include <stdexcept>
#include <iostream>
#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;
}

View File

@ -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 <string>
/**
* @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);

View File

@ -34,6 +34,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <assert.h> #include <assert.h>
#include <stdint.h>
#include "defines.h" #include "defines.h"
#include "bytes.h" #include "bytes.h"
@ -44,18 +45,8 @@
#include "constants.h" #include "constants.h"
#include "unpack.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) void band::readData(int expectedLength)
{ {
CHECK;
assert(expectedLength >= 0); assert(expectedLength >= 0);
assert(vs[0].cmk == cmk_ERROR); assert(vs[0].cmk == cmk_ERROR);
if (expectedLength != 0) if (expectedLength != 0)
@ -82,7 +73,7 @@ void band::readData(int expectedLength)
// Make a conservatively generous estimate of band size in bytes. // Make a conservatively generous estimate of band size in bytes.
// Assume B == 5 everywhere. // Assume B == 5 everywhere.
// Assume awkward pop with all {U} values (2*5 per value) // 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); u->ensure_input(generous);
} }
@ -102,7 +93,6 @@ void band::readData(int expectedLength)
assert(!valc->isMalloc); assert(!valc->isMalloc);
} }
xvs.init(u->rp, u->rplimit, valc); xvs.init(u->rp, u->rplimit, valc);
CHECK;
int X = xvs.getInt(); int X = xvs.getInt();
if (valc->S() != 0) if (valc->S() != 0)
{ {
@ -133,7 +123,6 @@ void band::readData(int expectedLength)
byte XB_byte = (byte)XB; byte XB_byte = (byte)XB;
byte *XB_ptr = &XB_byte; byte *XB_ptr = &XB_byte;
cm.init(u->rp, u->rplimit, XB_ptr, 0, defc, length, nullptr); cm.init(u->rp, u->rplimit, XB_ptr, 0, defc, length, nullptr);
CHECK;
} }
else else
{ {
@ -162,7 +151,6 @@ void band::setIndexByTag(byte tag)
entry *band::getRefCommon(cpindex *ix_, bool nullOKwithCaller) entry *band::getRefCommon(cpindex *ix_, bool nullOKwithCaller)
{ {
CHECK_0;
assert(ix_->ixTag == ixTag || assert(ix_->ixTag == ixTag ||
(ixTag == CONSTANT_Literal && ix_->ixTag >= CONSTANT_Integer && (ixTag == CONSTANT_Literal && ix_->ixTag >= CONSTANT_Integer &&
ix_->ixTag <= CONSTANT_String)); 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. // But nullOKwithCaller means caller is willing to tolerate a nullptr.
entry *ref = ix_->get(n); entry *ref = ix_->get(n);
if (ref == nullptr && !(nullOKwithCaller && n == -1)) if (ref == nullptr && !(nullOKwithCaller && n == -1))
abort(n == -1 ? "nullptr ref" : "bad ref"); unpack_abort(n == -1 ? "nullptr ref" : "bad ref");
return 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); band &hi_band = (*this);
assert(lo_band.bn == hi_band.bn + 1); assert(lo_band.bn == hi_band.bn + 1);
uint lo = lo_band.getInt(); uint32_t lo = lo_band.getInt();
if (!have_hi) if (!have_hi)
{ {
assert(hi_band.length == 0); assert(hi_band.length == 0);
return makeLong(0, lo); return makeLong(0, lo);
} }
uint hi = hi_band.getInt(); uint32_t hi = hi_band.getInt();
return makeLong(hi, lo); return makeLong(hi, lo);
} }
int band::getIntTotal() int band::getIntTotal()
{ {
CHECK_0;
if (length == 0) if (length == 0)
return 0; return 0;
if (total_memo > 0) if (total_memo > 0)
@ -201,8 +188,7 @@ int band::getIntTotal()
// and that the partial sums never overflow (wrap negative) // and that the partial sums never overflow (wrap negative)
if (total < 0) if (total < 0)
{ {
abort("overflow detected"); unpack_abort("overflow detected");
return 0;
} }
for (int k = length - 1; k > 0; k--) for (int k = length - 1; k > 0; k--)
{ {
@ -210,8 +196,7 @@ int band::getIntTotal()
total += vs[0].getInt(); total += vs[0].getInt();
if (total < prev_total) if (total < prev_total)
{ {
abort("overflow detected"); unpack_abort("overflow detected");
return 0;
} }
} }
rewind(); rewind();
@ -221,7 +206,6 @@ int band::getIntTotal()
int band::getIntCount(int tag) int band::getIntCount(int tag)
{ {
CHECK_0;
if (length == 0) if (length == 0)
return 0; return 0;
if (tag >= HIST0_MIN && tag <= HIST0_MAX) if (tag >= HIST0_MIN && tag <= HIST0_MAX)
@ -230,7 +214,6 @@ int band::getIntCount(int tag)
{ {
// Lazily calculate an approximate histogram. // Lazily calculate an approximate histogram.
hist0 = U_NEW(int, (HIST0_MAX - HIST0_MIN) + 1); hist0 = U_NEW(int, (HIST0_MAX - HIST0_MIN) + 1);
CHECK_0;
for (int k = length; k > 0; k--) for (int k = length; k > 0; k--)
{ {
int x = vs[0].getInt(); 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_modtime, DELTA5_spec, 0), BAND_INIT(file_options, UNSIGNED5_spec, 0),
// BAND_INIT(file_bits, BYTE1_spec, 0), // BAND_INIT(file_bits, BYTE1_spec, 0),
{0, 0}}; {0, 0}};
#define NUM_BAND_INITS (sizeof(all_band_inits) / sizeof(all_band_inits[0]))
band *band::makeBands(unpacker *u) band *band::makeBands(unpacker *u)
{ {
@ -434,7 +416,7 @@ void band::initIndexes(unpacker *u)
for (int i = 0; i < BAND_LIMIT; i++) for (int i = 0; i < BAND_LIMIT; i++)
{ {
band *scan = &tmp_all_bands[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) if (tag != 0 && tag != CONSTANT_Literal && (tag & SUBINDEX_BIT) == 0)
{ {
scan->setIndex(u->cp.getIndex(tag)); scan->setIndex(u->cp.getIndex(tag));

View File

@ -150,11 +150,11 @@ struct band
return getRefCommon(ix2, true); return getRefCommon(ix2, true);
} }
entry *getRefCommon(cpindex *ix, bool nullOK); 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(); int getIntTotal();
@ -162,9 +162,6 @@ struct band
static band *makeBands(unpacker *u); static band *makeBands(unpacker *u);
static void initIndexes(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[]; extern band all_bands[];

View File

@ -27,6 +27,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <stdint.h>
#include "defines.h" #include "defines.h"
#include "bytes.h" #include "bytes.h"
#include "utils.h" #include "utils.h"
@ -114,7 +115,7 @@ int bytes::compareTo(bytes &other)
void bytes::saveFrom(const void *ptr_, size_t len_) void bytes::saveFrom(const void *ptr_, size_t len_)
{ {
malloc(len_); malloc(len_);
// Save as much as possible. (Helps unpacker::abort.) // Save as much as possible.
if (len_ > len) if (len_ > len)
{ {
assert(ptr == dummy); // error recovery assert(ptr == dummy); // error recovery
@ -161,7 +162,6 @@ byte *fillbytes::grow(size_t s)
allocated = b.len; allocated = b.len;
if (allocated != maxlen) if (allocated != maxlen)
{ {
assert(unpack_aborting());
b.len = nlen - s; // back up b.len = nlen - s; // back up
return dummy; // scribble during error recov. return dummy; // scribble during error recov.
} }

View File

@ -161,7 +161,7 @@ struct fillbytes
b.len = 0; b.len = 0;
} }
int8_t *grow(size_t s); // grow so that limit() += s int8_t *grow(size_t s); // grow so that limit() += s
int getByte(uint i) int getByte(uint32_t i)
{ {
return *loc(i) & 0xFF; return *loc(i) & 0xFF;
} }

View File

@ -32,6 +32,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <assert.h> #include <assert.h>
#include <stdint.h>
#include "defines.h" #include "defines.h"
#include "bytes.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 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 { // == Coding.decodeSign32
assert(S > 0); assert(S > 0);
uint sigbits = (ux >> S); uint32_t sigbits = (ux >> S);
if (IS_NEG_CODE(S, ux)) if (IS_NEG_CODE(S, ux))
return (int)(~sigbits); return (int)(~sigbits);
else else
@ -90,9 +91,9 @@ coding *coding::init()
return nullptr; // no 5-byte fixed-size coding return nullptr; // no 5-byte fixed-size coding
// first compute the range of the coding, in 64 bits // 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++) for (int i = 0; i < B; i++)
{ {
range += H_i; range += H_i;
@ -106,7 +107,7 @@ coding *coding::init()
int this_umax; int this_umax;
// now, compute min and max // now, compute min and max
if (range >= ((jlong)1 << 32)) if (range >= ((int64_t)1 << 32))
{ {
this_umax = INT_MAX_VALUE; this_umax = INT_MAX_VALUE;
this->umin = INT_MIN_VALUE; this->umin = INT_MIN_VALUE;
@ -121,13 +122,13 @@ coding *coding::init()
if (S != 0 && range != 0) if (S != 0 && range != 0)
{ {
int Smask = (1 << S) - 1; int Smask = (1 << S) - 1;
jlong maxPosCode = range - 1; int64_t maxPosCode = range - 1;
jlong maxNegCode = range - 1; int64_t maxNegCode = range - 1;
while (IS_NEG_CODE(S, maxPosCode)) while (IS_NEG_CODE(S, maxPosCode))
--maxPosCode; --maxPosCode;
while (!IS_NEG_CODE(S, maxNegCode)) while (!IS_NEG_CODE(S, maxNegCode))
--maxNegCode; --maxNegCode;
int maxPos = decode_sign(S, (uint)maxPosCode); int maxPos = decode_sign(S, (uint32_t)maxPosCode);
if (maxPos < 0) if (maxPos < 0)
this->max = INT_MAX_VALUE; // 32-bit wraparound this->max = INT_MAX_VALUE; // 32-bit wraparound
else else
@ -135,7 +136,7 @@ coding *coding::init()
if (maxNegCode < 0) if (maxNegCode < 0)
this->min = 0; // No negative codings at all. this->min = 0; // No negative codings at all.
else 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; break;
} }
coding *ptr = NEW(coding, 1); coding *ptr = NEW(coding, 1);
CHECK_NULL_0(ptr); if (!ptr)
return nullptr;
coding *c = ptr->initFrom(spec); coding *c = ptr->initFrom(spec);
if (c == nullptr) 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; int L = 256 - H;
byte *ptr = rp; byte *ptr = rp;
// hand peel the i==0 part of the loop: // hand peel the i==0 part of the loop:
uint b_i = *ptr++ & 0xFF; uint32_t b_i = *ptr++ & 0xFF;
if (B == 1 || b_i < (uint)L) if (B == 1 || b_i < (uint32_t)L)
{ {
rp = ptr; rp = ptr;
return b_i; return b_i;
} }
uint sum = b_i; uint32_t sum = b_i;
uint H_i = H; uint32_t H_i = H;
assert(B <= B_MAX); assert(B <= B_MAX);
for (int i = 2; i <= B_MAX; i++) for (int i = 2; i <= B_MAX; i++)
{ // easy for compilers to unroll if desired { // easy for compilers to unroll if desired
b_i = *ptr++ & 0xFF; b_i = *ptr++ & 0xFF;
sum += b_i * H_i; sum += b_i * H_i;
if (i == B || b_i < (uint)L) if (i == B || b_i < (uint32_t)L)
{ {
rp = ptr; rp = ptr;
return sum; return sum;
@ -236,26 +238,26 @@ uint coding::parse(byte *&rp, int B, int H)
return 0; 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)); assert(H == (1 << lgH));
int L = 256 - (1 << lgH); int L = 256 - (1 << lgH);
byte *ptr = rp; byte *ptr = rp;
// hand peel the i==0 part of the loop: // hand peel the i==0 part of the loop:
uint b_i = *ptr++ & 0xFF; uint32_t b_i = *ptr++ & 0xFF;
if (B == 1 || b_i < (uint)L) if (B == 1 || b_i < (uint32_t)L)
{ {
rp = ptr; rp = ptr;
return b_i; return b_i;
} }
uint sum = b_i; uint32_t sum = b_i;
uint lg_H_i = lgH; uint32_t lg_H_i = lgH;
assert(B <= B_MAX); assert(B <= B_MAX);
for (int i = 2; i <= B_MAX; i++) for (int i = 2; i <= B_MAX; i++)
{ // easy for compilers to unroll if desired { // easy for compilers to unroll if desired
b_i = *ptr++ & 0xFF; b_i = *ptr++ & 0xFF;
sum += b_i << lg_H_i; sum += b_i << lg_H_i;
if (i == B || b_i < (uint)L) if (i == B || b_i < (uint32_t)L)
{ {
rp = ptr; rp = ptr;
return sum; return sum;
@ -272,7 +274,7 @@ void coding::parseMultiple(byte *&rp, int N, byte *limit, int B, int H)
{ {
if (N < 0) if (N < 0)
{ {
abort("bad value count"); unpack_abort("bad value count");
return; return;
} }
byte *ptr = rp; 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; size_t len = (size_t)N * B;
if (len / B != (size_t)N || ptr + len > limit) if (len / B != (size_t)N || ptr + len > limit)
{ {
abort(ERB); unpack_abort(ERB);
return; return;
} }
rp = ptr + len; 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 // do an error check here
if (ptr > limit) if (ptr > limit)
{ {
abort(ERB); unpack_abort(ERB);
return; 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) if (uval > 0)
{ {
// note that the initial parse performed a range check // 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]; return self->cm->fValues[uval - 1];
} }
else else
@ -422,7 +424,7 @@ int coding::sumInUnsignedRange(int x, int y)
int range = (int)(umax + 1); int range = (int)(umax + 1);
assert(range > 0); assert(range > 0);
x += y; 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. // 32-bit overflow interferes with range reduction.
// Back off from the overflow by adding a multiple of range: // Back off from the overflow by adding a multiple of range:
@ -461,9 +463,9 @@ int coding::sumInUnsignedRange(int x, int y)
return x; 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); assert(self->c.isSubrange | self->c.isFullRange);
if (isSubrange) if (isSubrange)
return self->sum = self->c.sumInUnsignedRange(self->sum, (int)uval); return self->sum = self->c.sumInUnsignedRange(self->sum, (int)uval);
@ -499,7 +501,7 @@ int value_stream::getInt()
} }
CODING_PRIVATE(c.spec); CODING_PRIVATE(c.spec);
uint uval; uint32_t uval;
enum enum
{ {
B5 = 5, B5 = 5,
@ -546,19 +548,19 @@ int value_stream::getInt()
assert(D == 1); assert(D == 1);
uval = coding::parse(rp, B, H); uval = coding::parse(rp, B, H);
if (S != 0) if (S != 0)
uval = (uint)decode_sign(S, uval); uval = (uint32_t)decode_sign(S, uval);
return getDeltaValue(this, uval, (bool)c.isSubrange); return getDeltaValue(this, uval, (bool)c.isSubrange);
case cmk_BHS1D1full: case cmk_BHS1D1full:
assert(S == 1 && D == 1 && c.isFullRange); assert(S == 1 && D == 1 && c.isFullRange);
uval = coding::parse(rp, B, H); uval = coding::parse(rp, B, H);
uval = (uint)DECODE_SIGN_S1(uval); uval = (uint32_t)DECODE_SIGN_S1(uval);
return getDeltaValue(this, uval, false); return getDeltaValue(this, uval, false);
case cmk_BHS1D1sub: case cmk_BHS1D1sub:
assert(S == 1 && D == 1 && c.isSubrange); assert(S == 1 && D == 1 && c.isSubrange);
uval = coding::parse(rp, B, H); uval = coding::parse(rp, B, H);
uval = (uint)DECODE_SIGN_S1(uval); uval = (uint32_t)DECODE_SIGN_S1(uval);
return getDeltaValue(this, uval, true); return getDeltaValue(this, uval, true);
case cmk_DELTA5: case cmk_DELTA5:
@ -583,7 +585,7 @@ int value_stream::getInt()
uval = coding::parse(rp, B, H); uval = coding::parse(rp, B, H);
if (S != 0) if (S != 0)
{ {
uval = (uint)decode_sign(S, uval); uval = (uint32_t)decode_sign(S, uval);
} }
if (D != 0) if (D != 0)
{ {
@ -592,7 +594,7 @@ int value_stream::getInt()
sum = c.sumInUnsignedRange(sum, (int)uval); sum = c.sumInUnsignedRange(sum, (int)uval);
else else
sum += (int)uval; sum += (int)uval;
uval = (uint)sum; uval = (uint32_t)sum;
} }
return getPopValue(this, uval); return getPopValue(this, uval);
@ -616,8 +618,8 @@ int value_stream::getInt()
static int moreCentral(int x, int y) static int moreCentral(int x, int y)
{ // used to find end of Pop.{F} { // used to find end of Pop.{F}
// Suggested implementation from the Pack200 specification: // Suggested implementation from the Pack200 specification:
uint kx = (x >> 31) ^ (x << 1); uint32_t kx = (x >> 31) ^ (x << 1);
uint ky = (y >> 31) ^ (y << 1); uint32_t ky = (y >> 31) ^ (y << 1);
return (kx < ky ? x : y); return (kx < ky ? x : y);
} }
// static maybe_inline // 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 to_free = foundc; // findBySpec may dynamically allocate
if (foundc == nullptr) if (foundc == nullptr)
{ {
abort("illegal arb. coding"); unpack_abort("illegal arbitrary coding");
return; return;
} }
// and fall through // 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; int N2 = (N >= 0) ? N - K : N;
if (N == 0 || (N2 <= 0 && N2 != N)) if (N == 0 || (N2 <= 0 && N2 != N))
{ {
abort("illegal run encoding"); unpack_abort("illegal run encoding");
return;
} }
if ((mode & DISABLE_RUN) != 0) if ((mode & DISABLE_RUN) != 0)
{ {
abort("illegal nested run encoding"); unpack_abort("illegal nested run encoding");
return;
} }
// & Enc{ ACode } if ADef=0 (ABDef != 1) // & 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); this->init(band_rp, band_limit, meta_rp, disRun, defc, K, valueSink);
} }
CHECK;
// & Enc{ BCode } if BDef=0 (ABDef != 2) // & Enc{ BCode } if BDef=0 (ABDef != 2)
coding_method *tail = U_NEW(coding_method, 1); coding_method *tail = U_NEW(coding_method, 1);
CHECK_NULL(tail); if (!tail)
return;
tail->u = u; tail->u = u;
// The 'run' codings may be nested indirectly via 'pop' codings. // 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); int TH = (256 - TL);
if (N <= 0) if (N <= 0)
{ {
abort("illegal pop encoding"); unpack_abort("illegal pop encoding");
return;
} }
if ((mode & DISABLE_POP) != 0) if ((mode & DISABLE_POP) != 0)
{ {
abort("illegal nested pop encoding"); unpack_abort("illegal nested pop encoding");
return;
} }
// No indirect nesting of 'pop', but 'run' is OK. // 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); fValues = (u->saveTo(fvbuf, fValueSink.b), (int *)fvbuf.ptr);
fVlength = fValueSink.length(); // i.e., the parameter K fVlength = fValueSink.length(); // i.e., the parameter K
fValueSink.free(); fValueSink.free();
CHECK;
// Skip the first {F} run in all subsequent passes. // Skip the first {F} run in all subsequent passes.
// The next call to this->init(...) will set vs0.rp to point after the {F}. // 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 break; // found it
tcode->free(); tcode->free();
tcode = coding::findBySpec(B, TH); tcode = coding::findBySpec(B, TH);
CHECK_NULL(tcode); if (!tcode)
return;
} }
if (!(fVlength <= tcode->umax)) if (!(fVlength <= tcode->umax))
{ {
abort("pop.L value too small"); unpack_abort("pop.L value too small");
return;
} }
this->init(band_rp, band_limit, NO_META, disPop, tcode, N, nullptr); this->init(band_rp, band_limit, NO_META, disPop, tcode, N, nullptr);
tcode->free(); 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); this->init(band_rp, band_limit, meta_rp, disPop, defc, N, nullptr);
} }
CHECK;
// Count the number of zero tokens right now. // Count the number of zero tokens right now.
// Also verify that they are in bounds. // 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; value_stream vs = vs0;
for (int i = 0; i < N; i++) for (int i = 0; i < N; i++)
{ {
uint val = vs.getInt(); uint32_t val = vs.getInt();
if (val == 0) if (val == 0)
UN += 1; UN += 1;
if (!(val <= (uint)fVlength)) if (!(val <= (uint32_t)fVlength))
{ {
abort("pop token out of range"); unpack_abort("pop token out of range");
return;
} }
} }
vs.done(); vs.done();
@ -849,7 +844,8 @@ void coding_method::init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int m
if (UN != 0) if (UN != 0)
{ {
uValues = U_NEW(coding_method, 1); uValues = U_NEW(coding_method, 1);
CHECK_NULL(uValues); if (uValues == nullptr)
return;
uValues->u = u; uValues->u = u;
if (UDef != 0) 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); int uop = (*meta_rp++ & 0xFF);
if (uop > _meta_canon_max) if (uop > _meta_canon_max)
// %%% Spec. requires the more strict (uop != _meta_default). // %%% 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 else
{ {
abort("bad meta-coding"); unpack_abort("bad meta-coding");
return;
} }
// Common code here skips a series of values with one 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 &c = vs0.c;
CODING_PRIVATE(c.spec); CODING_PRIVATE(c.spec);
// assert sane N // 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. // Look at the values, or at least skip over them quickly.
if (valueSink == nullptr) 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 if (valueSink->length() > 0 && (val == last || val == min)) //|| val == min2
break; break;
valueSink->add(val); valueSink->add(val);
CHECK;
last = val; last = val;
min = moreCentral(min, last); min = moreCentral(min, last);
// min2 = moreCentral2(min2, last, min); // min2 = moreCentral2(min2, last, min);
} }
band_rp = vs.rp; band_rp = vs.rp;
} }
CHECK;
// Get an accurate upper limit now. // Get an accurate upper limit now.
vs0.rplimit = band_rp; vs0.rplimit = band_rp;

View File

@ -84,11 +84,11 @@ struct coding
static coding *findBySpec(int B, int H, int S = 0, int D = 0); static coding *findBySpec(int B, int H, int S = 0, int D = 0);
static coding *findByIndex(int irregularCodingIndex); static coding *findByIndex(int irregularCodingIndex);
static uint parse(byte *&rp, int B, int H); static uint32_t parse(byte *&rp, int B, int H);
static uint parse_lgH(byte *&rp, int B, int H, int lgH); 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); 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)); return parse(rp, CODING_B(spec), CODING_H(spec));
} }
@ -116,12 +116,6 @@ struct coding
} }
void free(); // free self if isMalloc void free(); // free self if isMalloc
// error handling
static void abort(const char *msg = nullptr)
{
unpack_abort(msg);
}
}; };
enum coding_method_kind enum coding_method_kind
@ -224,10 +218,6 @@ struct value_stream
return this + 1; return this + 1;
} }
bool hasHelper(); bool hasHelper();
// error handling
// inline void abort(const char* msg);
// inline void aborting();
}; };
struct coding_method struct coding_method
@ -254,17 +244,4 @@ struct coding_method
// The value sink is used to collect output values, when desired. // 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, void init(byte *&band_rp, byte *band_limit, byte *&meta_rp, int mode, coding *defc, int N,
intlist *valueSink); 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(); }

View File

@ -51,7 +51,7 @@
// magic number for gzip streams (for processing pack200-gzip data) // magic number for gzip streams (for processing pack200-gzip data)
#define GZIP_MAGIC 0x1F8B0800 #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 enum
{ {

View File

@ -32,39 +32,22 @@
#include <unistd.h> #include <unistd.h>
#endif #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 // 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_FORMAT "Corrupted pack file"
#define ERROR_RESOURCE "Cannot extract resource file" #define ERROR_RESOURCE "Cannot extract resource file"
#define ERROR_OVERFLOW "Internal buffer overflow" #define ERROR_OVERFLOW "Internal buffer overflow"
#define ERROR_INTERNAL "Internal error" #define ERROR_INTERNAL "Internal error"
#define LOGFILE_STDOUT "-"
#define LOGFILE_STDERR ""
#define lengthof(array) (sizeof(array) / sizeof(array[0])) #define lengthof(array) (sizeof(array) / sizeof(array[0]))
#define NEW(T, n) (T *) must_malloc((int)(scale_size(n, sizeof(T)))) #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 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))) #define T_NEW(T, n) (T *) u->temp_alloc(scale_size(n, sizeof(T)))
// bytes and byte arrays typedef signed char byte;
typedef unsigned int uint;
#ifdef _MSC_VER #ifdef _MSC_VER
typedef LONGLONG jlong;
typedef DWORDLONG julong;
#define MKDIR(dir) mkdir(dir) #define MKDIR(dir) mkdir(dir)
#define getpid() _getpid() #define getpid() _getpid()
#define PATH_MAX MAX_PATH #define PATH_MAX MAX_PATH
@ -73,64 +56,10 @@ typedef DWORDLONG julong;
#define tempname _tempname #define tempname _tempname
#define sleep Sleep #define sleep Sleep
#else #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); #define MKDIR(dir) mkdir(dir, 0777);
#endif #endif
/* Must cast to void *, then size_t, then int. */ /* Must cast to void *, then size_t, then int. */
#define ptrlowbits(x) ((int)(size_t)(void *)(x)) #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 #define DEFAULT_ARCHIVE_MODTIME 1060000000 // Aug 04, 2003 5:26 PM PDT

View File

@ -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 <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include <time.h>
#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;
}

File diff suppressed because it is too large Load Diff

View File

@ -27,7 +27,7 @@
struct jar; struct jar;
struct gunzip; struct gunzip;
struct band; struct band;
struct cpool; struct constant_pool;
struct entry; struct entry;
struct cpindex; struct cpindex;
struct inner_class; struct inner_class;
@ -35,7 +35,7 @@ struct value_stream;
struct cpindex struct cpindex
{ {
uint len; uint32_t len;
entry *base1; // base of primary index entry *base1; // base of primary index
entry **base2; // base of secondary index entry **base2; // base of secondary index
byte ixTag; // type of entries (!= CONSTANT_None), plus 64 if sub-index byte ixTag; // type of entries (!= CONSTANT_None), plus 64 if sub-index
@ -44,7 +44,7 @@ struct cpindex
SUB_TAG = 64 SUB_TAG = 64
}; };
entry *get(uint i); entry *get(uint32_t i);
void init(int len_, entry *base1_, int ixTag_) void init(int len_, entry *base1_, int ixTag_)
{ {
@ -62,12 +62,12 @@ struct cpindex
} }
}; };
struct cpool struct constant_pool
{ {
uint nentries; uint32_t nentries;
entry *entries; entry *entries;
entry *first_extra_entry; entry *first_extra_entry;
uint maxentries; // total allocated size of entries uint32_t maxentries; // total allocated size of entries
// Position and size of each homogeneous subrange: // Position and size of each homogeneous subrange:
int tag_count[CONSTANT_Limit]; int tag_count[CONSTANT_Limit];
@ -89,7 +89,7 @@ struct cpool
ptrlist outputEntries; // list of entry* needing output idx assigned ptrlist outputEntries; // list of entry* needing output idx assigned
entry **hashTab; entry **hashTab;
uint hashTabLength; uint32_t hashTabLength;
entry *&hashTabRef(byte tag, bytes &b); entry *&hashTabRef(byte tag, bytes &b);
entry *ensureUtf8(bytes &b); entry *ensureUtf8(bytes &b);
entry *ensureClass(bytes &b); entry *ensureClass(bytes &b);
@ -117,12 +117,12 @@ struct cpool
int getCount(byte tag) int getCount(byte tag)
{ {
assert((uint)tag < CONSTANT_Limit); assert((uint32_t)tag < CONSTANT_Limit);
return tag_count[tag]; return tag_count[tag];
} }
cpindex *getIndex(byte tag) cpindex *getIndex(byte tag)
{ {
assert((uint)tag < CONSTANT_Limit); assert((uint32_t)tag < CONSTANT_Limit);
return &tag_index[tag]; return &tag_index[tag];
} }
cpindex *getKQIndex(); // uses cur_descr cpindex *getKQIndex(); // uses cur_descr
@ -133,10 +133,6 @@ struct cpool
void computeOutputOrder(); void computeOutputOrder();
void computeOutputIndexes(); void computeOutputIndexes();
void resetOutputIndexes(); void resetOutputIndexes();
// error handling
inline void abort(const char *msg);
inline bool aborting();
}; };
/* /*
@ -149,7 +145,7 @@ struct unpacker
struct file struct file
{ {
const char *name; const char *name;
julong size; uint64_t size;
int modtime; int modtime;
int options; int options;
bytes data[2]; bytes data[2];
@ -161,12 +157,8 @@ struct unpacker
} }
}; };
// global pointer to self, if not running under JNI (not multi-thread safe)
static unpacker *non_mt_current;
// if running Unix-style, here are the inputs and outputs // if running Unix-style, here are the inputs and outputs
FILE *infileptr; // buffered FILE *infileptr; // buffered
int infileno; // unbuffered
bytes inbytes; // direct bytes inbytes; // direct
gunzip *gzin; // gunzip filter, if any gunzip *gzin; // gunzip filter, if any
jar *jarout; // output JAR file jar *jarout; // output JAR file
@ -174,19 +166,13 @@ struct unpacker
// pointer to self, for U_NEW macro // pointer to self, for U_NEW macro
unpacker *u; unpacker *u;
// private abort message string, allocated to PATH_MAX*2
const char *abort_message;
ptrlist mallocs; // list of guys to free when we are all done ptrlist mallocs; // list of guys to free when we are all done
ptrlist tmallocs; // list of guys to free on next client request ptrlist tmallocs; // list of guys to free on next client request
fillbytes smallbuf; // supplies small alloc requests fillbytes smallbuf; // supplies small alloc requests
fillbytes tsmallbuf; // supplies temporary small alloc requests fillbytes tsmallbuf; // supplies temporary small alloc requests
// option management members // option management members
int verbose; // verbose level, 0 means no output int verbose; // verbose level, 0 means no output
bool strip_compile;
bool strip_debug;
bool strip_jcov;
bool remove_packfile;
int deflate_hint_or_zero; // ==0 means not set, otherwise -1 or 1 int deflate_hint_or_zero; // ==0 means not set, otherwise -1 or 1
int modification_time_or_zero; int modification_time_or_zero;
@ -196,11 +182,12 @@ struct unpacker
bool free_input; // must the input buffer be freed? bool free_input; // must the input buffer be freed?
byte *rp; // read pointer (< rplimit <= input.limit()) byte *rp; // read pointer (< rplimit <= input.limit())
byte *rplimit; // how much of the input block has been read? byte *rplimit; // how much of the input block has been read?
julong bytes_read; uint64_t bytes_read;
int unsized_bytes_read; int unsized_bytes_read;
// callback to read at least one byte, up to available input // callback to read at least one byte, up to available input
typedef jlong (*read_input_fn_t)(unpacker *self, void *buf, jlong minlen, jlong maxlen); typedef int64_t (*read_input_fn_t)(unpacker *self, void *buf, int64_t minlen,
int64_t maxlen);
read_input_fn_t read_input_fn; read_input_fn_t read_input_fn;
// archive header fields // archive header fields
@ -218,7 +205,7 @@ struct unpacker
// engine state // engine state
band *all_bands; // indexed by band_number band *all_bands; // indexed by band_number
byte *meta_rp; // read-pointer into (copy of) band_headers byte *meta_rp; // read-pointer into (copy of) band_headers
cpool cp; // all constant pool information constant_pool cp; // all constant pool information
inner_class *ics; // InnerClasses inner_class *ics; // InnerClasses
// output stream // output stream
@ -239,7 +226,7 @@ struct unpacker
fillbytes cur_classfile_tail; fillbytes cur_classfile_tail;
int files_written; // also tells which file we're working on int files_written; // also tells which file we're working on
int classes_written; // also tells which class we're working on int classes_written; // also tells which class we're working on
julong bytes_written; uint64_t bytes_written;
intlist bcimap; intlist bcimap;
fillbytes class_fixup_type; fillbytes class_fixup_type;
intlist class_fixup_offset; intlist class_fixup_offset;
@ -250,8 +237,8 @@ struct unpacker
ptrlist requested_ics; // which ics need output? ptrlist requested_ics; // which ics need output?
// stats pertaining to multiple segments (updated on reset) // stats pertaining to multiple segments (updated on reset)
julong bytes_read_before_reset; uint64_t bytes_read_before_reset;
julong bytes_written_before_reset; uint64_t bytes_written_before_reset;
int files_written_before_reset; int files_written_before_reset;
int classes_written_before_reset; int classes_written_before_reset;
int segments_read_before_reset; int segments_read_before_reset;
@ -259,7 +246,7 @@ struct unpacker
// attribute state // attribute state
struct layout_definition struct layout_definition
{ {
uint idx; // index (0..31...) which identifies this layout uint32_t idx; // index (0..31...) which identifies this layout
const char *name; // name of layout const char *name; // name of layout
entry *nameEntry; entry *nameEntry;
const char *layout; // string of layout (not yet parsed) const char *layout; // string of layout (not yet parsed)
@ -280,9 +267,9 @@ struct unpacker
unpacker *u; // pointer to self, for U_NEW macro unpacker *u; // pointer to self, for U_NEW macro
int xxx_flags_hi_bn; // locator for flags, count, indexes, calls bands int xxx_flags_hi_bn; // locator for flags, count, indexes, calls bands
int attrc; // ATTR_CONTEXT_CLASS, etc. int attrc; // ATTR_CONTEXT_CLASS, etc.
uint flag_limit; // 32 or 63, depending on archive_options bit uint32_t flag_limit; // 32 or 63, depending on archive_options bit
julong predef; // mask of built-in definitions uint64_t predef; // mask of built-in definitions
julong redef; // mask of local flag definitions or redefinitions uint64_t redef; // mask of local flag definitions or redefinitions
ptrlist layouts; // local (compressor-defined) defs, in index order ptrlist layouts; // local (compressor-defined) defs, in index order
int flag_count[X_ATTR_LIMIT_FLAGS_HI]; int flag_count[X_ATTR_LIMIT_FLAGS_HI];
intlist overflow_count; intlist overflow_count;
@ -321,12 +308,12 @@ struct unpacker
band **popBody(int band_stack_base); // pops a body off band_stack band **popBody(int band_stack_base); // pops a body off band_stack
// Read data into the bands of the idx-th layout. // Read data into the bands of the idx-th layout.
void readBandData(int idx); // parse layout, make bands, read data void readBandData(int idx); // parse layout, make bands, read data
void readBandData(band **body, uint count); // recursive helper void readBandData(band **body, uint32_t count); // recursive helper
layout_definition *getLayout(uint idx) layout_definition *getLayout(uint32_t idx)
{ {
if (idx >= (uint)layouts.length()) if (idx >= (uint32_t)layouts.length())
return nullptr; return nullptr;
return (layout_definition *)layouts.get(idx); return (layout_definition *)layouts.get(idx);
} }
@ -344,33 +331,33 @@ struct unpacker
} }
// Return flag_count if idx is predef and not redef, else zero. // Return flag_count if idx is predef and not redef, else zero.
int predefCount(uint idx); int predefCount(uint32_t idx);
bool isRedefined(uint idx) bool isRedefined(uint32_t idx)
{ {
if (idx >= flag_limit) if (idx >= flag_limit)
return false; return false;
return (bool)((redef >> idx) & 1); return (bool)((redef >> idx) & 1);
} }
bool isPredefined(uint idx) bool isPredefined(uint32_t idx)
{ {
if (idx >= flag_limit) if (idx >= flag_limit)
return false; return false;
return (bool)(((predef & ~redef) >> idx) & 1); return (bool)(((predef & ~redef) >> idx) & 1);
} }
julong flagIndexMask() uint64_t flagIndexMask()
{ {
return (predef | redef); return (predef | redef);
} }
bool isIndex(uint idx) bool isIndex(uint32_t idx)
{ {
assert(flag_limit != 0); // must be set up already assert(flag_limit != 0); // must be set up already
if (idx < flag_limit) if (idx < flag_limit)
return (bool)(((predef | redef) >> idx) & 1); return (bool)(((predef | redef) >> idx) & 1);
else else
return (idx - flag_limit < (uint)overflow_count.length()); return (idx - flag_limit < (uint32_t)overflow_count.length());
} }
int &getCount(uint idx) int &getCount(uint32_t idx)
{ {
assert(isIndex(idx)); assert(isIndex(idx));
if (idx < flag_limit) if (idx < flag_limit)
@ -378,14 +365,6 @@ struct unpacker
else else
return overflow_count.get(idx - flag_limit); return overflow_count.get(idx - flag_limit);
} }
bool aborting()
{
return u->aborting();
}
void abort(const char *msg)
{
u->abort(msg);
}
}; };
attr_definitions attr_defs[ATTR_CONTEXT_LIMIT]; attr_definitions attr_defs[ATTR_CONTEXT_LIMIT];
@ -407,10 +386,8 @@ struct unpacker
bool set_option(const char *option, const char *value); bool set_option(const char *option, const char *value);
const char *get_option(const char *option); const char *get_option(const char *option);
void dump_options();
// Fetching input. // Fetching input.
bool ensure_input(jlong more); bool ensure_input(int64_t more);
byte *input_scan() byte *input_scan()
{ {
return rp; return rp;
@ -473,12 +450,6 @@ struct unpacker
sprintf(buf, "%d", num); sprintf(buf, "%d", num);
return saveStr(buf); return saveStr(buf);
} }
const char *get_abort_message();
void abort(const char *s = nullptr);
bool aborting()
{
return abort_message != nullptr;
}
static unpacker *current(); // find current instance static unpacker *current(); // find current instance
// Output management // Output management
@ -514,7 +485,7 @@ struct unpacker
} }
void putu2(int n); // { putu2_at(put_space(2), n); } void putu2(int n); // { putu2_at(put_space(2), n); }
void putu4(int n); // { putu4_at(put_space(4), n); } void putu4(int n); // { putu4_at(put_space(4), n); }
void putu8(jlong n); // { putu8_at(put_space(8), n); } void putu8(int64_t n); // { putu8_at(put_space(8), n); }
void putref(entry *e); // { putu2_at(put_space(2), putref_index(e, 2)); } void putref(entry *e); // { putu2_at(put_space(2), putref_index(e, 2)); }
void putu1ref(entry *e); // { putu1_at(put_space(1), putref_index(e, 1)); } void putu1ref(entry *e); // { putu1_at(put_space(1), putref_index(e, 1)); }
int putref_index(entry *e, int size); // size in [1..2] int putref_index(entry *e, int size); // size in [1..2]
@ -530,7 +501,7 @@ struct unpacker
{ {
return wpbase + offset; return wpbase + offset;
} }
uint to_bci(uint bii); uint32_t to_bci(uint32_t bii);
void get_code_header(int &max_stack, int &max_na_locals, int &handler_count, int &cflags); void get_code_header(int &max_stack, int &max_na_locals, int &handler_count, int &cflags);
band *ref_band_for_self_op(int bc, bool &isAloadVar, int &origBCVar); band *ref_band_for_self_op(int bc, bool &isAloadVar, int &origBCVar);
band *ref_band_for_op(int bc); band *ref_band_for_op(int bc);
@ -543,7 +514,7 @@ struct unpacker
} }
static void putu2_at(byte *wp, int n); static void putu2_at(byte *wp, int n);
static void putu4_at(byte *wp, int n); static void putu4_at(byte *wp, int n);
static void putu8_at(byte *wp, jlong n); static void putu8_at(byte *wp, int64_t n);
// Private stuff // Private stuff
void reset_cur_classfile(); void reset_cur_classfile();
@ -552,7 +523,7 @@ struct unpacker
void write_code(); void write_code();
void write_bc_ops(); void write_bc_ops();
void write_members(int num, int attrc); // attrc=ATTR_CONTEXT_FIELD/METHOD void write_members(int num, int attrc); // attrc=ATTR_CONTEXT_FIELD/METHOD
int write_attrs(int attrc, julong indexBits); int write_attrs(int attrc, uint64_t indexBits);
// The readers // The readers
void read_bands(); void read_bands();
@ -574,12 +545,3 @@ struct unpacker
void read_double_refs(band &cp_band, byte ref1Tag, byte ref2Tag, entry *cpMap, int len); void read_double_refs(band &cp_band, byte ref1Tag, byte ref2Tag, entry *cpMap, int len);
void read_signature_values(entry *cpMap, int len); void read_signature_values(entry *cpMap, int len);
}; };
inline void cpool::abort(const char *msg)
{
u->abort(msg);
}
inline bool cpool::aborting()
{
return u->aborting();
}

View File

@ -0,0 +1,172 @@
/*
* 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 <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include <time.h>
#include <stdint.h>
#include "constants.h"
#include "utils.h"
#include "defines.h"
#include "bytes.h"
#include "coding.h"
#include "unpack200.h"
#include "unpack.h"
#include "zip.h"
// Callback for fetching data, Unix style.
static int64_t read_input_via_stdio(unpacker *u, void *buf, int64_t minlen, int64_t maxlen)
{
assert(u->infileptr != nullptr);
assert(minlen <= maxlen); // don't talk nonsense
int64_t 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;
nr = (int)fread(bufptr, 1, readlen, u->infileptr);
if (nr <= 0)
{
if (errno != EINTR)
break;
nr = 0;
}
numread += nr;
bufptr += nr;
assert(numread <= maxlen);
}
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
int64_t 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;
}
void unpack_200(std::string input_path, std::string output_path)
{
unpacker u;
int status = 0;
FILE *input = fopen(input_path.c_str(), "rb");
if (!input)
{
throw std::runtime_error("Can't open input file" + input_path);
}
FILE *output = fopen(output_path.c_str(), "wb");
if (!output)
{
fclose(output);
throw std::runtime_error("Can't open output file" + output_path);
}
u.init(read_input_via_stdio);
// initialize jar output
// the output takes ownership of the file handle
jar jarout;
jarout.init(&u);
jarout.jarfp = output;
// the input doesn't
u.infileptr = input;
// read the magic!
char peek[4];
int magic;
magic = read_magic(&u, peek, (int)sizeof(peek));
// if it is a gzip encoded file, we need an extra gzip input filter
if ((magic & GZIP_MAGIC_MASK) == GZIP_MAGIC)
{
gunzip *gzin = NEW(gunzip, 1);
gzin->init(&u);
// FIXME: why the side effects? WHY?
u.gzin->start(magic);
u.start();
}
else
{
// otherwise, feed the bytes to the unpacker directly
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 (;;)
{
// Each trip through this loop unpacks one segment
// and then resets the unpacker.
for (unpacker::file *filep; (filep = u.get_next_file()) != nullptr;)
{
u.write_file_to_jar(filep);
}
// Peek ahead for more data.
magic = read_magic(&u, peek, (int)sizeof(peek));
if (magic != (int)JAVA_PACKAGE_MAGIC)
{
if (magic != EOF_MAGIC)
unpack_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));
}
u.finish();
u.free(); // tidy up malloc blocks
fclose(input);
}

View File

@ -29,6 +29,7 @@
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
#include <assert.h> #include <assert.h>
#include <stdint.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -57,35 +58,14 @@ void *must_malloc(size_t size)
} }
else else
{ {
unpack_abort(ERROR_ENOMEM); throw std::runtime_error(ERROR_ENOMEM);
} }
return ptr; return ptr;
} }
void unpack_abort(const char *msg, unpacker *u) void unpack_abort(const char *msg)
{ {
if (msg == nullptr) if (msg == nullptr)
msg = "corrupt pack file or internal error"; msg = "corrupt pack file or internal error";
if (u == nullptr) throw std::runtime_error(msg);
u = unpacker::current();
if (u == nullptr)
{
fprintf(stderr, "Error: unpacker: %s\n", msg);
::abort();
return;
}
u->abort(msg);
}
bool unpack_aborting(unpacker *u)
{
if (u == nullptr)
u = unpacker::current();
if (u == nullptr)
{
fprintf(stderr, "Error: unpacker: no current instance\n");
::abort();
return true;
}
return u->aborting();
} }

View File

@ -25,6 +25,8 @@
// Definitions of our util functions // Definitions of our util functions
#include <stdexcept>
void *must_malloc(size_t size); void *must_malloc(size_t size);
// overflow management // overflow management
@ -46,9 +48,6 @@ inline size_t add_size(size_t size1, size_t size2, int size3)
return add_size(add_size(size1, size2), size3); return add_size(add_size(size1, size2), size3);
} }
// These may be expensive, because they have to go via Java TSD,
// if the optional u argument is missing.
struct unpacker; struct unpacker;
extern void unpack_abort(const char *msg, unpacker *u = nullptr); /// This throws an exception!
extern bool unpack_aborting(unpacker *u = nullptr); extern void unpack_abort(const char *msg = nullptr);

View File

@ -30,6 +30,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <time.h> #include <time.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
@ -47,29 +48,14 @@
#include "zip.h" #include "zip.h"
#ifdef NO_ZLIB
inline bool jar::deflate_bytes(bytes &head, bytes &tail)
{
return false;
}
inline uint jar::get_crc32(uint c, uchar *ptr, uint len)
{
return 0;
}
#define Z_NULL NULL
#else // Have ZLIB
#include <zlib.h> #include <zlib.h>
inline uint jar::get_crc32(uint c, uchar *ptr, uint len) inline uint32_t jar::get_crc32(uint32_t c, uchar *ptr, uint32_t len)
{ {
return crc32(c, ptr, len); return crc32(c, ptr, len);
} }
#endif // End of ZLIB // FIXME: this is bullshit. Do real endianness detection.
#ifdef sparc #ifdef sparc
#define SWAP_BYTES(a) ((((a) << 8) & 0xff00) | 0x00ff) & (((a) >> 8) | 0xff00) #define SWAP_BYTES(a) ((((a) << 8) & 0xff00) | 0x00ff) & (((a) >> 8) | 0xff00)
#else #else
@ -107,7 +93,7 @@ void jar::write_data(void *buff, int len)
void jar::add_to_jar_directory(const char *fname, bool store, int modtime, int len, int clen, void jar::add_to_jar_directory(const char *fname, bool store, int modtime, int len, int clen,
uint32_t crc) uint32_t crc)
{ {
uint fname_length = (uint)strlen(fname); uint32_t fname_length = (uint32_t)strlen(fname);
ushort header[23]; ushort header[23];
if (modtime == 0) if (modtime == 0)
modtime = default_modtime; modtime = default_modtime;
@ -169,9 +155,9 @@ void jar::add_to_jar_directory(const char *fname, bool store, int modtime, int l
} }
void jar::write_jar_header(const char *fname, bool store, int modtime, int len, int clen, void jar::write_jar_header(const char *fname, bool store, int modtime, int len, int clen,
uint crc) uint32_t crc)
{ {
uint fname_length = (uint)strlen(fname); uint32_t fname_length = (uint32_t)strlen(fname);
ushort header[15]; ushort header[15];
if (modtime == 0) if (modtime == 0)
modtime = default_modtime; modtime = default_modtime;
@ -218,12 +204,10 @@ void jar::write_jar_header(const char *fname, bool store, int modtime, int len,
write_data((char *)fname, (int)fname_length); write_data((char *)fname, (int)fname_length);
} }
static const char marker_comment[] = ZIP_ARCHIVE_MARKER_COMMENT;
void jar::write_central_directory() void jar::write_central_directory()
{ {
bytes mc; bytes mc;
mc.set(marker_comment); mc.set("PACK200");
ushort header[11]; ushort header[11];
@ -278,11 +262,11 @@ void jar::addJarEntry(const char *fname, bool deflate_hint, int modtime, bytes &
int len = (int)(head.len + tail.len); int len = (int)(head.len + tail.len);
int clen = 0; int clen = 0;
uint crc = get_crc32(0, Z_NULL, 0); uint32_t crc = get_crc32(0, Z_NULL, 0);
if (head.len != 0) if (head.len != 0)
crc = get_crc32(crc, (uchar *)head.ptr, (uint)head.len); crc = get_crc32(crc, (uchar *)head.ptr, (uint32_t)head.len);
if (tail.len != 0) if (tail.len != 0)
crc = get_crc32(crc, (uchar *)tail.ptr, (uint)tail.len); crc = get_crc32(crc, (uchar *)tail.ptr, (uint32_t)tail.len);
bool deflate = (deflate_hint && len > 0); bool deflate = (deflate_hint && len > 0);
@ -452,10 +436,10 @@ bool jar::deflate_bytes(bytes &head, bytes &tail)
} }
// Callback for fetching data from a GZIP input stream // Callback for fetching data from a GZIP input stream
static jlong read_input_via_gzip(unpacker *u, void *buf, jlong minlen, jlong maxlen) static int64_t read_input_via_gzip(unpacker *u, void *buf, int64_t minlen, int64_t maxlen)
{ {
assert(minlen <= maxlen); // don't talk nonsense assert(minlen <= maxlen); // don't talk nonsense
jlong numread = 0; int64_t numread = 0;
char *bufptr = (char *)buf; char *bufptr = (char *)buf;
char *inbuf = u->gzin->inbuf; char *inbuf = u->gzin->inbuf;
size_t inbuflen = sizeof(u->gzin->inbuf); size_t inbuflen = sizeof(u->gzin->inbuf);
@ -476,7 +460,7 @@ static jlong read_input_via_gzip(unpacker *u, void *buf, jlong minlen, jlong max
int error = inflate(&zs, Z_NO_FLUSH); int error = inflate(&zs, Z_NO_FLUSH);
if (error != Z_OK && error != Z_STREAM_END) if (error != Z_OK && error != Z_STREAM_END)
{ {
u->abort("error inflating input"); unpack_abort("error inflating input");
break; break;
} }
int nr = readlen - zs.avail_out; int nr = readlen - zs.avail_out;
@ -505,7 +489,7 @@ static jlong read_input_via_gzip(unpacker *u, void *buf, jlong minlen, jlong max
// %%% should check final CRC and length here // %%% should check final CRC and length here
// %%% should check for concatenated *.gz files here // %%% should check for concatenated *.gz files here
if (zs.avail_in > 0) if (zs.avail_in > 0)
u->abort("garbage after end of deflated input stream"); unpack_abort("garbage after end of deflated input stream");
// pop this filter off: // pop this filter off:
u->gzin->free(); u->gzin->free();
break; break;
@ -577,15 +561,11 @@ void gunzip::start(int magic)
if (gz_flg & FHCRC) if (gz_flg & FHCRC)
read_fixed_field(gz_hcrc, sizeof(gz_hcrc)); read_fixed_field(gz_hcrc, sizeof(gz_hcrc));
if (aborting())
return;
// now the input stream is ready to read into the inflater // now the input stream is ready to read into the inflater
int error = inflateInit2((z_stream *)zstream, -MAX_WBITS); int error = inflateInit2((z_stream *)zstream, -MAX_WBITS);
if (error != Z_OK) if (error != Z_OK)
{ {
abort("cannot create input"); unpack_abort("cannot create input");
return;
} }
} }
@ -602,9 +582,7 @@ void gunzip::free()
void gunzip::read_fixed_field(char *buf, size_t buflen) void gunzip::read_fixed_field(char *buf, size_t buflen)
{ {
if (aborting()) int64_t nr = ((unpacker::read_input_fn_t)read_input_fn)(u, buf, buflen, buflen);
return;
jlong nr = ((unpacker::read_input_fn_t)read_input_fn)(u, buf, buflen, buflen);
if ((size_t)nr != buflen) if ((size_t)nr != buflen)
u->abort("short stream header"); unpack_abort("short stream header");
} }

View File

@ -24,7 +24,7 @@
*/ */
#include <stdint.h> #include <stdint.h>
typedef unsigned short ushort; typedef unsigned short ushort;
typedef unsigned int uint; typedef unsigned int uint32_t;
typedef unsigned char uchar; typedef unsigned char uchar;
struct unpacker; struct unpacker;
@ -42,7 +42,7 @@ struct jar
// Private members // Private members
fillbytes central_directory; fillbytes central_directory;
ushort central_directory_count; ushort central_directory_count;
uint output_file_offset; uint32_t output_file_offset;
fillbytes deflated; // temporary buffer fillbytes deflated; // temporary buffer
// pointer to outer unpacker, for error checks etc. // pointer to outer unpacker, for error checks etc.
@ -85,17 +85,7 @@ struct jar
// The definitions of these depend on the NO_ZLIB option: // The definitions of these depend on the NO_ZLIB option:
bool deflate_bytes(bytes &head, bytes &tail); bool deflate_bytes(bytes &head, bytes &tail);
static uint get_crc32(uint c, unsigned char *ptr, uint len); static uint32_t get_crc32(uint32_t c, unsigned char *ptr, uint32_t len);
// error handling
void abort(const char *msg)
{
unpack_abort(msg, u);
}
bool aborting()
{
return unpack_aborting(u);
}
}; };
struct gunzip struct gunzip
@ -105,7 +95,7 @@ struct gunzip
// pointer to outer unpacker, for error checks etc. // pointer to outer unpacker, for error checks etc.
unpacker *u; unpacker *u;
void *read_input_fn; // underlying byte stream void *read_input_fn; // underlying \bchar\b stream
void *zstream; // inflater state void *zstream; // inflater state
char inbuf[1 << 14]; // input buffer char inbuf[1 << 14]; // input buffer
@ -117,14 +107,4 @@ struct gunzip
// private stuff // private stuff
void read_fixed_field(char *buf, size_t buflen); void read_fixed_field(char *buf, size_t buflen);
// error handling
void abort(const char *msg)
{
unpack_abort(msg, u);
}
bool aborting()
{
return unpack_aborting(u);
}
}; };

View File

@ -3,7 +3,7 @@ project(xz-embedded)
option(XZ_BUILD_BCJ "Build xz-embedded with BCJ support (native binary optimization)" OFF) option(XZ_BUILD_BCJ "Build xz-embedded with BCJ support (native binary optimization)" OFF)
option(XZ_BUILD_CRC64 "Build xz-embedded with CRC64 checksum support" ON) option(XZ_BUILD_CRC64 "Build xz-embedded with CRC64 checksum support" ON)
option(XZ_BUILD_MINIDEC "Build a tiny utility that decompresses xz streams" ON) option(XZ_BUILD_MINIDEC "Build a tiny utility that decompresses xz streams" OFF)
set(CMAKE_C_FLAGS "-std=c99") set(CMAKE_C_FLAGS "-std=c99")
@ -19,6 +19,7 @@ src/xz_lzma2.h
src/xz_private.h src/xz_private.h
src/xz_stream.h src/xz_stream.h
) )
# TODO: look into what would be needed for plain old lzma
# checksum checks # checksum checks
add_definitions(-DXZ_DEC_ANY_CHECK) add_definitions(-DXZ_DEC_ANY_CHECK)
@ -28,18 +29,12 @@ if(XZ_BUILD_CRC64)
endif() endif()
# TODO: add SHA256 # TODO: add SHA256
# uncomment these, if required.
if(XZ_BUILD_BCJ) if(XZ_BUILD_BCJ)
add_definitions(-DXZ_DEC_X86 -DXZ_DEC_POWERPC -DXZ_DEC_IA64) add_definitions(-DXZ_DEC_X86 -DXZ_DEC_POWERPC -DXZ_DEC_IA64)
add_definitions(-DXZ_DEC_ARM -DXZ_DEC_ARMTHUMB -DXZ_DEC_SPARC) add_definitions(-DXZ_DEC_ARM -DXZ_DEC_ARMTHUMB -DXZ_DEC_SPARC)
LIST(APPEND XZ_SOURCES src/xz_dec_bcj.c) LIST(APPEND XZ_SOURCES src/xz_dec_bcj.c)
endif() endif()
# Static link! add_library(xz-embedded STATIC ${XZ_SOURCES})
ADD_DEFINITIONS(-DXZ_STATIC)
add_definitions(-DXZ_LIBRARY)
add_library(xz-embedded SHARED ${XZ_SOURCES})
add_executable(xzminidec xzminidec.c) add_executable(xzminidec xzminidec.c)
target_link_libraries(xzminidec xz-embedded) target_link_libraries(xzminidec xz-embedded)