1
0
mirror of https://github.com/upx/upx synced 2025-09-28 19:06:07 +08:00

Improve error code reporting in compression wrappers.

This commit is contained in:
Markus F.X.J. Oberhumer 2006-06-23 15:52:10 +02:00
parent fe057a91c8
commit ba065ec1d8
2 changed files with 53 additions and 18 deletions

View File

@ -143,7 +143,7 @@ struct ProgressInfo : public ICompressProgressInfo, public CMyUnknownImp
STDMETHODIMP ProgressInfo::SetRatioInfo(const UInt64 *inSize, const UInt64 *outSize) STDMETHODIMP ProgressInfo::SetRatioInfo(const UInt64 *inSize, const UInt64 *outSize)
{ {
if (cb && cb->nprogress) if (cb && cb->nprogress)
cb->nprogress(cb, (size_t) *inSize, (size_t) *outSize); cb->nprogress(cb, (unsigned) *inSize, (unsigned) *outSize);
return S_OK; return S_OK;
} }
@ -176,7 +176,7 @@ int upx_lzma_compress ( const upx_bytep src, unsigned src_len,
#ifndef _NO_EXCEPTIONS #ifndef _NO_EXCEPTIONS
try { try {
#else #else
#error # error
#endif #endif
NCompress::NLZMA::CEncoder enc; NCompress::NLZMA::CEncoder enc;
@ -240,8 +240,12 @@ int upx_lzma_compress ( const upx_bytep src, unsigned src_len,
goto error; goto error;
if (enc.WriteCoderProperties(&os) != S_OK) if (enc.WriteCoderProperties(&os) != S_OK)
goto error; goto error;
if (os.Overflow || os.Pos != 5) if (os.Overflow) {
//r = UPX_E_OUTPUT_OVERRUN;
r = UPX_E_NOT_COMPRESSIBLE;
goto error; goto error;
}
assert(os.Pos == 5);
os.Pos -= 4; // do not encode dict_size os.Pos -= 4; // do not encode dict_size
rh = enc.Code(&is, &os, NULL, NULL, &progress); rh = enc.Code(&is, &os, NULL, NULL, &progress);
@ -252,7 +256,8 @@ int upx_lzma_compress ( const upx_bytep src, unsigned src_len,
else if (os.Overflow) else if (os.Overflow)
{ {
assert(os.Pos == *dst_len); assert(os.Pos == *dst_len);
//r = UPX_E_OUTPUT_OVERFLOW; //r = UPX_E_OUTPUT_OVERRUN;
r = UPX_E_NOT_COMPRESSIBLE;
} }
else if (rh == S_OK) else if (rh == S_OK)
{ {
@ -322,7 +327,7 @@ int upx_lzma_decompress ( const upx_bytep src, unsigned src_len,
goto error; goto error;
rh = LzmaDecodeProperties(&s.Properties, src, src_len); rh = LzmaDecodeProperties(&s.Properties, src, src_len);
if (r != 0) if (rh != 0)
goto error; goto error;
src += 1; src_len -= 1; src += 1; src_len -= 1;
if (result) if (result)
@ -343,7 +348,7 @@ int upx_lzma_decompress ( const upx_bytep src, unsigned src_len,
{ {
r = UPX_E_OK; r = UPX_E_OK;
if (src_out != src_len) if (src_out != src_len)
r = UPX_E_ERROR; // UPX_E_INPUT_NOT_CONSUMED; r = UPX_E_INPUT_NOT_CONSUMED;
} }
error: error:

View File

@ -62,7 +62,29 @@ int compress_ucl_dummy = 0;
// //
**************************************************************************/ **************************************************************************/
static void wrap_ucl_nprogress(ucl_uint a, ucl_uint b, int state, ucl_voidp user) static int convert_errno_from_ucl(int r)
{
switch (r)
{
case UCL_E_OK: return UPX_E_OK;
case UCL_E_ERROR: return UPX_E_ERROR;
case UCL_E_OUT_OF_MEMORY: return UPX_E_OUT_OF_MEMORY;
case UCL_E_NOT_COMPRESSIBLE: return UPX_E_NOT_COMPRESSIBLE;
case UCL_E_INPUT_OVERRUN: return UPX_E_INPUT_OVERRUN;
case UCL_E_OUTPUT_OVERRUN: return UPX_E_OUTPUT_OVERRUN;
case UCL_E_LOOKBEHIND_OVERRUN: return UPX_E_LOOKBEHIND_OVERRUN;
case UCL_E_EOF_NOT_FOUND: return UPX_E_EOF_NOT_FOUND;
case UCL_E_INPUT_NOT_CONSUMED: return UPX_E_INPUT_NOT_CONSUMED;
// case UCL_E_NOT_YET_IMPLEMENTED: return UPX_E_NOT_YET_IMPLEMENTED;
case UCL_E_INVALID_ARGUMENT: return UPX_E_INVALID_ARGUMENT;
// UCL extra:
case UCL_E_OVERLAP_OVERRUN: return UPX_E_ERROR;
}
return UPX_E_ERROR;
}
static void wrap_nprogress_ucl(ucl_uint a, ucl_uint b, int state, ucl_voidp user)
{ {
if (state != -1 && state != 3) return; if (state != -1 && state != 3) return;
upx_callback_p cb = (upx_callback_p) user; upx_callback_p cb = (upx_callback_p) user;
@ -71,6 +93,10 @@ static void wrap_ucl_nprogress(ucl_uint a, ucl_uint b, int state, ucl_voidp user
} }
/*************************************************************************
//
**************************************************************************/
int upx_ucl_compress ( const upx_bytep src, unsigned src_len, int upx_ucl_compress ( const upx_bytep src, unsigned src_len,
upx_bytep dst, unsigned* dst_len, upx_bytep dst, unsigned* dst_len,
upx_callback_p cb_parm, upx_callback_p cb_parm,
@ -78,14 +104,14 @@ int upx_ucl_compress ( const upx_bytep src, unsigned src_len,
const struct upx_compress_config_t *conf_parm, const struct upx_compress_config_t *conf_parm,
struct upx_compress_result_t *result ) struct upx_compress_result_t *result )
{ {
int r = UPX_E_ERROR; int r;
assert(level > 0); assert(result != NULL); assert(level > 0); assert(result != NULL);
ucl_progress_callback_t cb; ucl_progress_callback_t cb;
cb.callback = 0; cb.callback = 0;
cb.user = NULL; cb.user = NULL;
if (cb_parm && cb_parm->nprogress) { if (cb_parm && cb_parm->nprogress) {
cb.callback = wrap_ucl_nprogress; cb.callback = wrap_nprogress_ucl;
cb.user = cb_parm; cb.user = cb_parm;
} }
@ -104,8 +130,10 @@ int upx_ucl_compress ( const upx_bytep src, unsigned src_len,
static const unsigned char sizes[3]={32,8,16}; static const unsigned char sizes[3]={32,8,16};
conf.bb_size = sizes[(method - M_NRV2B_LE32) % 3]; conf.bb_size = sizes[(method - M_NRV2B_LE32) % 3];
} }
else else {
throwInternalError("unknown compression method"); throwInternalError("unknown compression method");
return UPX_E_ERROR;
}
// optimize compression parms // optimize compression parms
if (level <= 3 && conf.max_offset == UCL_UINT_MAX) if (level <= 3 && conf.max_offset == UCL_UINT_MAX)
@ -122,10 +150,12 @@ int upx_ucl_compress ( const upx_bytep src, unsigned src_len,
else if M_IS_NRV2E(method) else if M_IS_NRV2E(method)
r = ucl_nrv2e_99_compress(src, src_len, dst, dst_len, r = ucl_nrv2e_99_compress(src, src_len, dst, dst_len,
&cb, level, &conf, res); &cb, level, &conf, res);
else else {
throwInternalError("unknown compression method"); throwInternalError("unknown compression method");
return UPX_E_ERROR;
}
return r; return convert_errno_from_ucl(r);
} }
@ -138,7 +168,7 @@ int upx_ucl_decompress ( const upx_bytep src, unsigned src_len,
int method, int method,
const struct upx_compress_result_t *result ) const struct upx_compress_result_t *result )
{ {
int r = UPX_E_ERROR; int r;
switch (method) switch (method)
{ {
@ -171,11 +201,11 @@ int upx_ucl_decompress ( const upx_bytep src, unsigned src_len,
break; break;
default: default:
throwInternalError("unknown decompression method"); throwInternalError("unknown decompression method");
break; return UPX_E_ERROR;
} }
UNUSED(result); UNUSED(result);
return r; return convert_errno_from_ucl(r);
} }
@ -188,7 +218,7 @@ int upx_ucl_test_overlap ( const upx_bytep buf, unsigned src_off,
int method, int method,
const struct upx_compress_result_t *result ) const struct upx_compress_result_t *result )
{ {
int r = UPX_E_ERROR; int r;
switch (method) switch (method)
{ {
@ -221,11 +251,11 @@ int upx_ucl_test_overlap ( const upx_bytep buf, unsigned src_off,
break; break;
default: default:
throwInternalError("unknown decompression method"); throwInternalError("unknown decompression method");
break; return UPX_E_ERROR;
} }
UNUSED(result); UNUSED(result);
return r; return convert_errno_from_ucl(r);
} }