From ba065ec1d8ecddf4e068fbcf25913f0114f964ca Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Fri, 23 Jun 2006 15:52:10 +0200 Subject: [PATCH] Improve error code reporting in compression wrappers. --- src/compress_lzma.cpp | 17 +++++++++----- src/compress_ucl.cpp | 54 +++++++++++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/src/compress_lzma.cpp b/src/compress_lzma.cpp index 051370aa..481cd1ee 100644 --- a/src/compress_lzma.cpp +++ b/src/compress_lzma.cpp @@ -143,7 +143,7 @@ struct ProgressInfo : public ICompressProgressInfo, public CMyUnknownImp STDMETHODIMP ProgressInfo::SetRatioInfo(const UInt64 *inSize, const UInt64 *outSize) { if (cb && cb->nprogress) - cb->nprogress(cb, (size_t) *inSize, (size_t) *outSize); + cb->nprogress(cb, (unsigned) *inSize, (unsigned) *outSize); return S_OK; } @@ -176,7 +176,7 @@ int upx_lzma_compress ( const upx_bytep src, unsigned src_len, #ifndef _NO_EXCEPTIONS try { #else -#error +# error #endif NCompress::NLZMA::CEncoder enc; @@ -240,8 +240,12 @@ int upx_lzma_compress ( const upx_bytep src, unsigned src_len, goto error; if (enc.WriteCoderProperties(&os) != S_OK) goto error; - if (os.Overflow || os.Pos != 5) + if (os.Overflow) { + //r = UPX_E_OUTPUT_OVERRUN; + r = UPX_E_NOT_COMPRESSIBLE; goto error; + } + assert(os.Pos == 5); os.Pos -= 4; // do not encode dict_size 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) { 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) { @@ -322,7 +327,7 @@ int upx_lzma_decompress ( const upx_bytep src, unsigned src_len, goto error; rh = LzmaDecodeProperties(&s.Properties, src, src_len); - if (r != 0) + if (rh != 0) goto error; src += 1; src_len -= 1; if (result) @@ -343,7 +348,7 @@ int upx_lzma_decompress ( const upx_bytep src, unsigned src_len, { r = UPX_E_OK; if (src_out != src_len) - r = UPX_E_ERROR; // UPX_E_INPUT_NOT_CONSUMED; + r = UPX_E_INPUT_NOT_CONSUMED; } error: diff --git a/src/compress_ucl.cpp b/src/compress_ucl.cpp index 6e1aa004..87f0e866 100644 --- a/src/compress_ucl.cpp +++ b/src/compress_ucl.cpp @@ -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; 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, upx_bytep dst, unsigned* dst_len, 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, struct upx_compress_result_t *result ) { - int r = UPX_E_ERROR; + int r; assert(level > 0); assert(result != NULL); ucl_progress_callback_t cb; cb.callback = 0; cb.user = NULL; if (cb_parm && cb_parm->nprogress) { - cb.callback = wrap_ucl_nprogress; + cb.callback = wrap_nprogress_ucl; 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}; conf.bb_size = sizes[(method - M_NRV2B_LE32) % 3]; } - else + else { throwInternalError("unknown compression method"); + return UPX_E_ERROR; + } // optimize compression parms 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) r = ucl_nrv2e_99_compress(src, src_len, dst, dst_len, &cb, level, &conf, res); - else + else { 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, const struct upx_compress_result_t *result ) { - int r = UPX_E_ERROR; + int r; switch (method) { @@ -171,11 +201,11 @@ int upx_ucl_decompress ( const upx_bytep src, unsigned src_len, break; default: throwInternalError("unknown decompression method"); - break; + return UPX_E_ERROR; } 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, const struct upx_compress_result_t *result ) { - int r = UPX_E_ERROR; + int r; switch (method) { @@ -221,11 +251,11 @@ int upx_ucl_test_overlap ( const upx_bytep buf, unsigned src_off, break; default: throwInternalError("unknown decompression method"); - break; + return UPX_E_ERROR; } UNUSED(result); - return r; + return convert_errno_from_ucl(r); }