mirror of
https://github.com/Xevion/easy7zip.git
synced 2025-12-08 14:07:00 -06:00
Update to Fast LZMA2 1.0.0
This commit is contained in:
@@ -121,23 +121,39 @@ STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream, ISequentialOutStream
|
||||
return SResToHRESULT(res);
|
||||
}
|
||||
|
||||
CFastEncoder::CFastEncoder()
|
||||
static HRESULT TranslateError(size_t res)
|
||||
{
|
||||
_encoder = NULL;
|
||||
reduceSize = 0;
|
||||
if (FL2_getErrorCode(res) == FL2_error_memory_allocation)
|
||||
return E_OUTOFMEMORY;
|
||||
return S_FALSE;
|
||||
}
|
||||
|
||||
CFastEncoder::~CFastEncoder()
|
||||
#define CHECK_S(f_) do { \
|
||||
size_t r_ = f_; \
|
||||
if (FL2_isError(r_)) \
|
||||
return TranslateError(r_); \
|
||||
} while (false)
|
||||
|
||||
#define CHECK_H(f_) do { \
|
||||
HRESULT r_ = f_; \
|
||||
if (r_ != S_OK) \
|
||||
return r_; \
|
||||
} while (false)
|
||||
|
||||
#define CHECK_P(f) if (FL2_isError(f)) return E_INVALIDARG; /* check and convert error code */
|
||||
|
||||
CFastEncoder::FastLzma2::FastLzma2()
|
||||
: fcs(NULL),
|
||||
dict_pos(0)
|
||||
{
|
||||
if (_encoder)
|
||||
FL2_freeCCtx(_encoder);
|
||||
}
|
||||
|
||||
CFastEncoder::FastLzma2::~FastLzma2()
|
||||
{
|
||||
FL2_freeCCtx(fcs);
|
||||
}
|
||||
|
||||
#define CHECK_F(f) if (FL2_isError(f)) return E_INVALIDARG; /* check and convert error code */
|
||||
|
||||
STDMETHODIMP CFastEncoder::SetCoderProperties(const PROPID *propIDs,
|
||||
const PROPVARIANT *coderProps, UInt32 numProps)
|
||||
HRESULT CFastEncoder::FastLzma2::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *coderProps, UInt32 numProps)
|
||||
{
|
||||
CLzma2EncProps lzma2Props;
|
||||
Lzma2EncProps_Init(&lzma2Props);
|
||||
@@ -146,56 +162,165 @@ STDMETHODIMP CFastEncoder::SetCoderProperties(const PROPID *propIDs,
|
||||
{
|
||||
RINOK(SetLzma2Prop(propIDs[i], coderProps[i], lzma2Props));
|
||||
}
|
||||
if (_encoder == NULL) {
|
||||
_encoder = FL2_createCCtxMt(lzma2Props.numTotalThreads);
|
||||
if (_encoder == NULL)
|
||||
if (fcs == NULL) {
|
||||
fcs = FL2_createCStreamMt(lzma2Props.numTotalThreads, 1);
|
||||
if (fcs == NULL)
|
||||
return E_OUTOFMEMORY;
|
||||
}
|
||||
if (lzma2Props.lzmaProps.algo > 2) {
|
||||
if (lzma2Props.lzmaProps.algo > 3)
|
||||
return E_INVALIDARG;
|
||||
lzma2Props.lzmaProps.algo = 2;
|
||||
FL2_CCtx_setParameter(_encoder, FL2_p_highCompression, 1);
|
||||
FL2_CCtx_setParameter(_encoder, FL2_p_compressionLevel, lzma2Props.lzmaProps.level);
|
||||
FL2_CCtx_setParameter(fcs, FL2_p_highCompression, 1);
|
||||
FL2_CCtx_setParameter(fcs, FL2_p_compressionLevel, lzma2Props.lzmaProps.level);
|
||||
}
|
||||
else {
|
||||
FL2_CCtx_setParameter(_encoder, FL2_p_7zLevel, lzma2Props.lzmaProps.level);
|
||||
FL2_CCtx_setParameter(fcs, FL2_p_compressionLevel, lzma2Props.lzmaProps.level);
|
||||
}
|
||||
dictSize = lzma2Props.lzmaProps.dictSize;
|
||||
size_t dictSize = lzma2Props.lzmaProps.dictSize;
|
||||
if (!dictSize) {
|
||||
dictSize = (UInt32)1 << FL2_CCtx_setParameter(_encoder, FL2_p_dictionaryLog, 0);
|
||||
dictSize = (UInt32)FL2_CCtx_getParameter(fcs, FL2_p_dictionarySize);
|
||||
}
|
||||
reduceSize = lzma2Props.lzmaProps.reduceSize;
|
||||
size_t reduceSize = lzma2Props.lzmaProps.reduceSize;
|
||||
reduceSize += (reduceSize < (UInt64)-1); /* prevent extra buffer shift after read */
|
||||
dictSize = (UInt32)min(dictSize, reduceSize);
|
||||
unsigned dictLog = FL2_DICTLOG_MIN;
|
||||
while (((UInt32)1 << dictLog) < dictSize)
|
||||
++dictLog;
|
||||
CHECK_F(FL2_CCtx_setParameter(_encoder, FL2_p_dictionaryLog, dictLog));
|
||||
dictSize = max(dictSize, FL2_DICTSIZE_MIN);
|
||||
CHECK_P(FL2_CCtx_setParameter(fcs, FL2_p_dictionarySize, dictSize));
|
||||
if (lzma2Props.lzmaProps.algo >= 0) {
|
||||
CHECK_F(FL2_CCtx_setParameter(_encoder, FL2_p_strategy, (unsigned)lzma2Props.lzmaProps.algo));
|
||||
CHECK_P(FL2_CCtx_setParameter(fcs, FL2_p_strategy, (unsigned)lzma2Props.lzmaProps.algo));
|
||||
}
|
||||
if (lzma2Props.lzmaProps.fb > 0)
|
||||
CHECK_F(FL2_CCtx_setParameter(_encoder, FL2_p_fastLength, lzma2Props.lzmaProps.fb));
|
||||
if (lzma2Props.lzmaProps.mc) {
|
||||
unsigned ml = 0;
|
||||
while (((UInt32)1 << ml) < lzma2Props.lzmaProps.mc)
|
||||
++ml;
|
||||
CHECK_F(FL2_CCtx_setParameter(_encoder, FL2_p_searchLog, ml));
|
||||
}
|
||||
CHECK_P(FL2_CCtx_setParameter(fcs, FL2_p_fastLength, lzma2Props.lzmaProps.fb));
|
||||
if (lzma2Props.lzmaProps.mc > 0)
|
||||
CHECK_P(FL2_CCtx_setParameter(fcs, FL2_p_hybridCycles, lzma2Props.lzmaProps.mc));
|
||||
if (lzma2Props.lzmaProps.lc >= 0)
|
||||
CHECK_F(FL2_CCtx_setParameter(_encoder, FL2_p_literalCtxBits, lzma2Props.lzmaProps.lc));
|
||||
CHECK_P(FL2_CCtx_setParameter(fcs, FL2_p_literalCtxBits, lzma2Props.lzmaProps.lc));
|
||||
if (lzma2Props.lzmaProps.lp >= 0)
|
||||
CHECK_F(FL2_CCtx_setParameter(_encoder, FL2_p_literalPosBits, lzma2Props.lzmaProps.lp));
|
||||
CHECK_P(FL2_CCtx_setParameter(fcs, FL2_p_literalPosBits, lzma2Props.lzmaProps.lp));
|
||||
if (lzma2Props.lzmaProps.pb >= 0)
|
||||
CHECK_F(FL2_CCtx_setParameter(_encoder, FL2_p_posBits, lzma2Props.lzmaProps.pb));
|
||||
FL2_CCtx_setParameter(_encoder, FL2_p_omitProperties, 1);
|
||||
#ifndef NO_XXHASH
|
||||
FL2_CCtx_setParameter(_encoder, FL2_p_doXXHash, 0);
|
||||
#endif
|
||||
CHECK_P(FL2_CCtx_setParameter(fcs, FL2_p_posBits, lzma2Props.lzmaProps.pb));
|
||||
FL2_CCtx_setParameter(fcs, FL2_p_omitProperties, 1);
|
||||
FL2_setCStreamTimeout(fcs, 500);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
size_t CFastEncoder::FastLzma2::GetDictSize() const
|
||||
{
|
||||
return FL2_CCtx_getParameter(fcs, FL2_p_dictionarySize);
|
||||
}
|
||||
|
||||
HRESULT CFastEncoder::FastLzma2::Begin()
|
||||
{
|
||||
CHECK_S(FL2_initCStream(fcs, 0));
|
||||
CHECK_S(FL2_getDictionaryBuffer(fcs, &dict));
|
||||
dict_pos = 0;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
BYTE* CFastEncoder::FastLzma2::GetAvailableBuffer(unsigned long& size)
|
||||
{
|
||||
size = static_cast<unsigned long>(dict.size - dict_pos);
|
||||
return reinterpret_cast<BYTE*>(dict.dst) + dict_pos;
|
||||
}
|
||||
|
||||
HRESULT CFastEncoder::FastLzma2::WaitAndReport(size_t& res, ICompressProgressInfo *progress)
|
||||
{
|
||||
while (FL2_isTimedOut(res)) {
|
||||
if (!UpdateProgress(progress))
|
||||
return S_FALSE;
|
||||
res = FL2_waitCStream(fcs);
|
||||
}
|
||||
CHECK_S(res);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT CFastEncoder::FastLzma2::AddByteCount(size_t count, ISequentialOutStream *outStream, ICompressProgressInfo *progress)
|
||||
{
|
||||
dict_pos += count;
|
||||
if (dict_pos == dict.size) {
|
||||
size_t res = FL2_updateDictionary(fcs, dict_pos);
|
||||
CHECK_H(WaitAndReport(res, progress));
|
||||
if (res != 0)
|
||||
CHECK_H(WriteBuffers(outStream));
|
||||
do {
|
||||
res = FL2_getDictionaryBuffer(fcs, &dict);
|
||||
} while (FL2_isTimedOut(res));
|
||||
CHECK_S(res);
|
||||
dict_pos = 0;
|
||||
}
|
||||
if (!UpdateProgress(progress))
|
||||
return S_FALSE;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
bool CFastEncoder::FastLzma2::UpdateProgress(ICompressProgressInfo *progress)
|
||||
{
|
||||
if (progress) {
|
||||
UInt64 outProcessed;
|
||||
UInt64 inProcessed = FL2_getCStreamProgress(fcs, &outProcessed);
|
||||
HRESULT err = progress->SetRatioInfo(&inProcessed, &outProcessed);
|
||||
if (err != S_OK) {
|
||||
FL2_cancelCStream(fcs);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
HRESULT CFastEncoder::FastLzma2::WriteBuffers(ISequentialOutStream *outStream)
|
||||
{
|
||||
size_t csize;
|
||||
for (;;) {
|
||||
FL2_cBuffer cbuf;
|
||||
// Waits if compression in progress
|
||||
csize = FL2_getNextCStreamBuffer(fcs, &cbuf);
|
||||
CHECK_S(csize);
|
||||
if (csize == 0)
|
||||
break;
|
||||
HRESULT err = WriteStream(outStream, cbuf.src, cbuf.size);
|
||||
if (err != S_OK)
|
||||
return err;
|
||||
}
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT CFastEncoder::FastLzma2::End(ISequentialOutStream *outStream, ICompressProgressInfo *progress)
|
||||
{
|
||||
if (dict_pos) {
|
||||
size_t res = FL2_updateDictionary(fcs, dict_pos);
|
||||
CHECK_H(WaitAndReport(res, progress));
|
||||
}
|
||||
|
||||
size_t res = FL2_endStream(fcs, nullptr);
|
||||
CHECK_H(WaitAndReport(res, progress));
|
||||
while (res) {
|
||||
WriteBuffers(outStream);
|
||||
res = FL2_endStream(fcs, nullptr);
|
||||
CHECK_H(WaitAndReport(res, progress));
|
||||
}
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
void CFastEncoder::FastLzma2::Cancel()
|
||||
{
|
||||
FL2_cancelCStream(fcs);
|
||||
}
|
||||
|
||||
CFastEncoder::CFastEncoder()
|
||||
{
|
||||
}
|
||||
|
||||
CFastEncoder::~CFastEncoder()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
STDMETHODIMP CFastEncoder::SetCoderProperties(const PROPID *propIDs,
|
||||
const PROPVARIANT *coderProps, UInt32 numProps)
|
||||
{
|
||||
return _encoder.SetCoderProperties(propIDs, coderProps, numProps);
|
||||
}
|
||||
|
||||
|
||||
#define LZMA2_DIC_SIZE_FROM_PROP(p) (((UInt32)2 | ((p) & 1)) << ((p) / 2 + 11))
|
||||
|
||||
@@ -203,6 +328,7 @@ STDMETHODIMP CFastEncoder::WriteCoderProperties(ISequentialOutStream *outStream)
|
||||
{
|
||||
Byte prop;
|
||||
unsigned i;
|
||||
size_t dictSize = _encoder.GetDictSize();
|
||||
for (i = 0; i < 40; i++)
|
||||
if (dictSize <= LZMA2_DIC_SIZE_FROM_PROP(i))
|
||||
break;
|
||||
@@ -211,79 +337,29 @@ STDMETHODIMP CFastEncoder::WriteCoderProperties(ISequentialOutStream *outStream)
|
||||
}
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ISequentialOutStream* outStream;
|
||||
ICompressProgressInfo* progress;
|
||||
UInt64 in_processed;
|
||||
UInt64 out_processed;
|
||||
HRESULT res;
|
||||
} EncodingObjects;
|
||||
|
||||
static int FL2LIB_CALL Progress(size_t done, void* opaque)
|
||||
{
|
||||
EncodingObjects* p = (EncodingObjects*)opaque;
|
||||
if (p && p->progress) {
|
||||
UInt64 in_processed = p->in_processed + done;
|
||||
p->res = p->progress->SetRatioInfo(&in_processed, &p->out_processed);
|
||||
return p->res != S_OK;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int FL2LIB_CALL Write(const void* src, size_t srcSize, void* opaque)
|
||||
{
|
||||
EncodingObjects* p = (EncodingObjects*)opaque;
|
||||
p->res = WriteStream(p->outStream, src, srcSize);
|
||||
return p->res != S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CFastEncoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream,
|
||||
const UInt64 * /* inSize */, const UInt64 * /* outSize */, ICompressProgressInfo *progress)
|
||||
{
|
||||
HRESULT err = S_OK;
|
||||
inBuffer.AllocAtLeast(dictSize);
|
||||
EncodingObjects objs = { outStream, progress, 0, 0, S_OK };
|
||||
FL2_blockBuffer block = { inBuffer, 0, 0, dictSize };
|
||||
CHECK_H(_encoder.Begin());
|
||||
size_t inSize;
|
||||
unsigned long dSize;
|
||||
do
|
||||
{
|
||||
FL2_shiftBlock(_encoder, &block);
|
||||
size_t inSize = dictSize - block.start;
|
||||
err = ReadStream(inStream, inBuffer + block.start, &inSize);
|
||||
if (err != S_OK)
|
||||
break;
|
||||
block.end += inSize;
|
||||
if (inSize) {
|
||||
size_t cSize = FL2_compressCCtxBlock_toFn(_encoder, Write, &objs, &block, Progress);
|
||||
if (FL2_isError(cSize)) {
|
||||
if (FL2_getErrorCode(cSize) == FL2_error_memory_allocation)
|
||||
return E_OUTOFMEMORY;
|
||||
return objs.res != S_OK ? objs.res : S_FALSE;
|
||||
}
|
||||
if (objs.res != S_OK)
|
||||
return objs.res;
|
||||
objs.out_processed += cSize;
|
||||
objs.in_processed += inSize;
|
||||
if (progress) {
|
||||
err = progress->SetRatioInfo(&objs.in_processed, &objs.out_processed);
|
||||
if (err != S_OK)
|
||||
break;
|
||||
}
|
||||
if (block.end < dictSize)
|
||||
break;
|
||||
BYTE* dict = _encoder.GetAvailableBuffer(dSize);
|
||||
|
||||
inSize = dSize;
|
||||
HRESULT err = ReadStream(inStream, dict, &inSize);
|
||||
if (err != S_OK) {
|
||||
_encoder.Cancel();
|
||||
return err;
|
||||
}
|
||||
else break;
|
||||
CHECK_H(_encoder.AddByteCount(inSize, outStream, progress));
|
||||
|
||||
} while (err == S_OK);
|
||||
} while (inSize == dSize);
|
||||
|
||||
if (err == S_OK) {
|
||||
size_t cSize = FL2_endFrame_toFn(_encoder, Write, &objs);
|
||||
if (FL2_isError(cSize))
|
||||
return S_FALSE;
|
||||
objs.out_processed += cSize;
|
||||
err = objs.res;
|
||||
}
|
||||
return err;
|
||||
CHECK_H(_encoder.End(outStream, progress));
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
@@ -24,13 +24,13 @@ class CEncoder:
|
||||
CLzma2EncHandle _encoder;
|
||||
public:
|
||||
MY_UNKNOWN_IMP4(
|
||||
ICompressCoder,
|
||||
ICompressSetCoderProperties,
|
||||
ICompressWriteCoderProperties,
|
||||
ICompressSetCoderPropertiesOpt)
|
||||
ICompressCoder,
|
||||
ICompressSetCoderProperties,
|
||||
ICompressWriteCoderProperties,
|
||||
ICompressSetCoderPropertiesOpt)
|
||||
|
||||
STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream,
|
||||
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
|
||||
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
|
||||
STDMETHOD(SetCoderProperties)(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps);
|
||||
STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream);
|
||||
STDMETHOD(SetCoderPropertiesOpt)(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps);
|
||||
@@ -45,10 +45,33 @@ class CFastEncoder :
|
||||
public ICompressWriteCoderProperties,
|
||||
public CMyUnknownImp
|
||||
{
|
||||
FL2_CCtx* _encoder;
|
||||
CByteBuffer inBuffer;
|
||||
UInt64 reduceSize;
|
||||
UInt32 dictSize;
|
||||
class FastLzma2
|
||||
{
|
||||
public:
|
||||
FastLzma2();
|
||||
~FastLzma2();
|
||||
HRESULT SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps);
|
||||
size_t GetDictSize() const;
|
||||
HRESULT Begin();
|
||||
BYTE* GetAvailableBuffer(unsigned long& size);
|
||||
HRESULT AddByteCount(size_t count, ISequentialOutStream *outStream, ICompressProgressInfo *progress);
|
||||
HRESULT End(ISequentialOutStream *outStream, ICompressProgressInfo *progress);
|
||||
void Cancel();
|
||||
|
||||
private:
|
||||
bool UpdateProgress(ICompressProgressInfo *progress);
|
||||
HRESULT WaitAndReport(size_t& res, ICompressProgressInfo *progress);
|
||||
HRESULT WriteBuffers(ISequentialOutStream *outStream);
|
||||
|
||||
FL2_CStream* fcs;
|
||||
FL2_dictBuffer dict;
|
||||
size_t dict_pos;
|
||||
|
||||
FastLzma2(const FastLzma2&) = delete;
|
||||
FastLzma2& operator=(const FastLzma2&) = delete;
|
||||
};
|
||||
|
||||
FastLzma2 _encoder;
|
||||
|
||||
public:
|
||||
MY_UNKNOWN_IMP3(
|
||||
@@ -57,7 +80,7 @@ public:
|
||||
ICompressWriteCoderProperties)
|
||||
|
||||
STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream,
|
||||
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
|
||||
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
|
||||
STDMETHOD(SetCoderProperties)(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps);
|
||||
STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user