zstd release 0.8.0

- made an extra zstd.mak file
- removed Format7zZStd
- SFXSetup also has zstd now
- the big 7z.dll is backward compatible to: 0.5 .. 0.7
- the small 7za.dll has only support for zstd 0.8
This commit is contained in:
Tino Reichardt
2016-08-09 09:44:05 +02:00
parent 4571689e1f
commit 559693c75b
38 changed files with 7010 additions and 2601 deletions

View File

@@ -174,7 +174,7 @@ $(C_OBJS): ../../../../C/$(*B).c
!ENDIF
!IFDEF ZSTD_OBJS
$(ZSTD_OBJS): ../../../../C/ZStd/$(*B).c
$(ZSTD_OBJS): ../../../../C/zstd/$(*B).c
$(COMPL_O2)
!ENDIF
@@ -240,7 +240,7 @@ $(ZSTD_OBJS): ../../../../C/ZStd/$(*B).c
$(COMPLB_O2)
{../../../../C}.c{$O}.obj::
$(CCOMPLB)
{../../../../C/ZStd}.c{$O}.obj::
{../../../../C/zstd}.c{$O}.obj::
$(CCOMPLB)
!ENDIF

View File

@@ -1,8 +1,8 @@
PROG = 7za.dll
DEF_FILE = ../../Archive/Archive2.def
CFLAGS = $(CFLAGS) \
-DDEFLATE_EXTRACT_ONLY \
-DBZIP2_EXTRACT_ONLY \
CFLAGS = $(CFLAGS)
# -DDEFLATE_EXTRACT_ONLY \
# -DBZIP2_EXTRACT_ONLY \
COMMON_OBJS = \
$O\CRC.obj \
@@ -55,6 +55,7 @@ AR_COMMON_OBJS = \
$O\OutStreamWithCRC.obj \
$O\ParseProperties.obj \
7Z_OBJS = \
$O\7zCompressionMode.obj \
$O\7zDecode.obj \
@@ -71,6 +72,7 @@ AR_COMMON_OBJS = \
$O\7zUpdate.obj \
$O\7zRegister.obj \
COMPRESS_OBJS = \
$O\CodecExports.obj \
$O\Bcj2Coder.obj \
@@ -82,10 +84,12 @@ COMPRESS_OBJS = \
$O\BranchRegister.obj \
$O\ByteSwap.obj \
$O\BZip2Crc.obj \
$O\BZip2Encoder.obj \
$O\BZip2Decoder.obj \
$O\BZip2Register.obj \
$O\CopyCoder.obj \
$O\CopyRegister.obj \
$O\DeflateEncoder.obj \
$O\DeflateDecoder.obj \
$O\DeflateRegister.obj \
$O\DeltaFilter.obj \
@@ -99,9 +103,6 @@ COMPRESS_OBJS = \
$O\PpmdDecoder.obj \
$O\PpmdEncoder.obj \
$O\PpmdRegister.obj \
$O\ZStdDecoder.obj \
$O\ZStdEncoder.obj \
$O\ZStdRegister.obj \
CRYPTO_OBJS = \
$O\7zAes.obj \
@@ -135,20 +136,13 @@ C_OBJS = \
$O\Sort.obj \
$O\Threads.obj \
ZSTD_OBJS = \
$O\entropy_common.obj \
$O\fse_compress.obj \
$O\fse_decompress.obj \
$O\huf_compress.obj \
$O\huf_decompress.obj \
$O\zbuff_compress.obj \
$O\zbuff_decompress.obj \
$O\zstd_common.obj \
$O\zstd_compress.obj \
$O\zstd_decompress.obj \
$O\zstd_v05.obj \
!include "../../Aes.mak"
!include "../../Crc.mak"
!include "../../zstd.mak"
ZSTD_OBJS = $(ZSTD_OBJS) \
$O\fse_compress.obj \
$O\huf_compress.obj \
$O\zbuff_compress.obj \
$O\zstd_compress.obj \
!include "../../7zip.mak"

View File

@@ -1,7 +1,7 @@
PROG = 7z.dll
DEF_FILE = ../../Archive/Archive2.def
CFLAGS = $(CFLAGS) \
-DEXTERNAL_CODECS \
-DEXTERNAL_CODECS -DZSTD_LEGACY \
!IFNDEF UNDER_CE
CFLAGS = $(CFLAGS) -D_7ZIP_LARGE_PAGES
@@ -11,27 +11,20 @@ CFLAGS = $(CFLAGS) -D_7ZIP_LARGE_PAGES
COMPRESS_OBJS = $(COMPRESS_OBJS) \
$O\CodecExports.obj \
$O\ZstdDecoder.obj \
$O\ZstdEncoder.obj \
$O\ZstdRegister.obj \
AR_OBJS = $(AR_OBJS) \
$O\ArchiveExports.obj \
$O\DllExports2.obj \
ZSTD_OBJS = \
$O\entropy_common.obj \
!include "../../zstd.mak"
ZSTD_OBJS = $(ZSTD_OBJS) \
$O\fse_compress.obj \
$O\fse_decompress.obj \
$O\huf_compress.obj \
$O\huf_decompress.obj \
$O\zbuff_compress.obj \
$O\zbuff_decompress.obj \
$O\zstd_common.obj \
$O\zstd_compress.obj \
$O\zstd_decompress.obj \
$O\zstd_v05.obj \
$O\zstd_v07.obj \
$O\zstd_v06.obj \
$O\xxhash.obj \
$O\zstd_v05.obj \
!include "../../7zip.mak"

View File

@@ -1,3 +0,0 @@
// StdAfx.cpp
#include "StdAfx.h"

View File

@@ -1,8 +0,0 @@
// StdAfx.h
#ifndef __STDAFX_H
#define __STDAFX_H
#include "../../../Common/Common.h"
#endif

View File

@@ -1,158 +0,0 @@
MY_STATIC_LINK=1
PROG = 7z.dll
DEF_FILE = ../../Archive/Archive2.def
CFLAGS = $(CFLAGS) -DEXTERNAL_CODECS
COMMON_OBJS = \
$O\CRC.obj \
$O\CrcReg.obj \
$O\IntToString.obj \
$O\NewHandler.obj \
$O\MyString.obj \
$O\Sha256Reg.obj \
$O\StringConvert.obj \
$O\StringToInt.obj \
$O\MyVector.obj \
$O\Wildcard.obj \
WIN_OBJS = \
$O\FileDir.obj \
$O\FileFind.obj \
$O\FileIO.obj \
$O\FileName.obj \
$O\PropVariant.obj \
$O\Synchronization.obj \
$O\System.obj \
7ZIP_COMMON_OBJS = \
$O\CreateCoder.obj \
$O\CWrappers.obj \
$O\InBuffer.obj \
$O\InOutTempBuffer.obj \
$O\FilterCoder.obj \
$O\LimitedStreams.obj \
$O\MethodId.obj \
$O\MethodProps.obj \
$O\OutBuffer.obj \
$O\ProgressUtils.obj \
$O\PropId.obj \
$O\StreamBinder.obj \
$O\StreamObjects.obj \
$O\StreamUtils.obj \
$O\UniqBlocks.obj \
$O\VirtThread.obj \
AR_OBJS = \
$O\ArchiveExports.obj \
$O\DllExports2.obj \
AR_COMMON_OBJS = \
$O\CoderMixer2.obj \
$O\HandlerOut.obj \
$O\InStreamWithCRC.obj \
$O\ItemNameUtils.obj \
$O\OutStreamWithCRC.obj \
$O\ParseProperties.obj \
7Z_OBJS = \
$O\7zCompressionMode.obj \
$O\7zDecode.obj \
$O\7zEncode.obj \
$O\7zExtract.obj \
$O\7zFolderInStream.obj \
$O\7zHandler.obj \
$O\7zHandlerOut.obj \
$O\7zHeader.obj \
$O\7zIn.obj \
$O\7zOut.obj \
$O\7zProperties.obj \
$O\7zSpecStream.obj \
$O\7zUpdate.obj \
$O\7zRegister.obj \
COMPRESS_OBJS = \
$O\CodecExports.obj \
$O\Bcj2Coder.obj \
$O\Bcj2Register.obj \
$O\BcjCoder.obj \
$O\BcjRegister.obj \
$O\BitlDecoder.obj \
$O\BranchMisc.obj \
$O\BranchRegister.obj \
$O\ByteSwap.obj \
$O\BZip2Crc.obj \
$O\BZip2Encoder.obj \
$O\BZip2Decoder.obj \
$O\BZip2Register.obj \
$O\CopyCoder.obj \
$O\CopyRegister.obj \
$O\DeflateEncoder.obj \
$O\DeflateDecoder.obj \
$O\DeflateRegister.obj \
$O\DeltaFilter.obj \
$O\Lzma2Decoder.obj \
$O\Lzma2Encoder.obj \
$O\Lzma2Register.obj \
$O\LzmaDecoder.obj \
$O\LzmaEncoder.obj \
$O\LzmaRegister.obj \
$O\LzOutWindow.obj \
$O\PpmdDecoder.obj \
$O\PpmdEncoder.obj \
$O\PpmdRegister.obj \
$O\ZstdDecoder.obj \
$O\ZstdEncoder.obj \
$O\ZstdRegister.obj \
CRYPTO_OBJS = \
$O\7zAes.obj \
$O\7zAesRegister.obj \
$O\MyAes.obj \
$O\MyAesReg.obj \
$O\RandGen.obj \
C_OBJS = \
$O\Alloc.obj \
$O\Bcj2.obj \
$O\Bcj2Enc.obj \
$O\Bra.obj \
$O\Bra86.obj \
$O\BraIA64.obj \
$O\BwtSort.obj \
$O\CpuArch.obj \
$O\Delta.obj \
$O\HuffEnc.obj \
$O\LzFind.obj \
$O\LzFindMt.obj \
$O\Lzma2Dec.obj \
$O\Lzma2Enc.obj \
$O\LzmaDec.obj \
$O\LzmaEnc.obj \
$O\MtCoder.obj \
$O\Ppmd7.obj \
$O\Ppmd7Dec.obj \
$O\Ppmd7Enc.obj \
$O\Sha256.obj \
$O\Sort.obj \
$O\Threads.obj \
ZSTD_OBJS = \
$O\entropy_common.obj \
$O\fse_compress.obj \
$O\fse_decompress.obj \
$O\huf_compress.obj \
$O\huf_decompress.obj \
$O\zbuff_compress.obj \
$O\zbuff_decompress.obj \
$O\zstd_common.obj \
$O\zstd_compress.obj \
$O\zstd_decompress.obj \
$O\zstd_v05.obj \
$O\zstd_v06.obj \
$O\xxhash.obj \
!include "../../Aes.mak"
!include "../../Crc.mak"
!include "../../7zip.mak"

View File

@@ -1,5 +0,0 @@
#include "../../MyVersionInfo.rc"
MY_VERSION_INFO_DLL("7z Standalone Plugin (ZStd)", "7z")
101 ICON "../../Archive/Icons/7z.ico"

View File

@@ -100,8 +100,6 @@ COMPRESS_OBJS = \
$O\LzmaRegister.obj \
$O\PpmdDecoder.obj \
$O\PpmdRegister.obj \
$O\ZstdDecoder.obj \
$O\ZStdRegister.obj \
CRYPTO_OBJS = \
$O\7zAes.obj \
@@ -123,15 +121,8 @@ C_OBJS = \
$O\Sha256.obj \
$O\Threads.obj \
ZSTD_OBJS = \
$O\entropy_common.obj \
$O\fse_decompress.obj \
$O\huf_decompress.obj \
$O\zbuff_decompress.obj \
$O\zstd_common.obj \
$O\zstd_decompress.obj \
$O\xxhash.obj \
!include "../../Aes.mak"
!include "../../Crc.mak"
!include "../../zstd.mak"
!include "../../7zip.mak"

View File

@@ -106,4 +106,5 @@ C_OBJS = \
!include "../../Crc.mak"
!include "../../zstd.mak"
!include "../../7zip.mak"

View File

@@ -119,8 +119,6 @@ COMPRESS_OBJS = \
$O\LzmaRegister.obj \
$O\PpmdDecoder.obj \
$O\PpmdRegister.obj \
$O\ZstdDecoder.obj \
$O\ZStdRegister.obj \
CRYPTO_OBJS = \
$O\7zAes.obj \
@@ -142,15 +140,8 @@ C_OBJS = \
$O\Sha256.obj \
$O\Threads.obj \
ZSTD_OBJS = \
$O\entropy_common.obj \
$O\fse_decompress.obj \
$O\huf_decompress.obj \
$O\zbuff_decompress.obj \
$O\zstd_common.obj \
$O\zstd_decompress.obj \
$O\xxhash.obj \
!include "../../Aes.mak"
!include "../../Crc.mak"
!include "../../zstd.mak"
!include "../../7zip.mak"

View File

@@ -1,405 +1,419 @@
// ZstdDecoder.cpp
// (C) 2016 Rich Geldreich, Tino Reichardt
#include "StdAfx.h"
#include "ZstdDecoder.h"
// #define SHOW_DEBUG_INFO
#ifdef SHOW_DEBUG_INFO
#include <stdio.h>
#define PRF(x) x
#else
#define PRF(x)
#endif
#define ZSTD_DEFAULT_BUFFER_SIZE (1U << 22U)
namespace NCompress {
namespace NZSTD {
CDecoder::CDecoder ():
_inBuf (NULL),
_outBuf (NULL),
_inPos (0),
_inSize (0),
_eofFlag (false),
_state (NULL),
_propsWereSet (false),
_outSizeDefined (false),
_outSize (0),
_inSizeProcessed (0),
_outSizeProcessed (0),
_inBufSizeAllocated (0),
_outBufSizeAllocated (0),
_inBufSize (ZSTD_DEFAULT_BUFFER_SIZE),
_outBufSize (ZSTD_DEFAULT_BUFFER_SIZE)
{
_props.clear ();
}
CDecoder::~CDecoder ()
{
if (_state)
ZB_freeDCtx(_state);
MyFree (_inBuf);
MyFree (_outBuf);
}
STDMETHODIMP CDecoder::SetInBufSize (UInt32, UInt32 size)
{
_inBufSize = size;
return S_OK;
}
STDMETHODIMP CDecoder::SetOutBufSize (UInt32, UInt32 size)
{
_outBufSize = size;
return S_OK;
}
HRESULT CDecoder::CreateBuffers ()
{
if (_inBuf == 0 || _inBufSize != _inBufSizeAllocated)
{
MyFree (_inBuf);
_inBuf = (Byte *) MyAlloc (_inBufSize);
if (_inBuf == 0)
return E_OUTOFMEMORY;
_inBufSizeAllocated = _inBufSize;
}
if (_outBuf == 0 || _outBufSize != _outBufSizeAllocated)
{
MyFree (_outBuf);
_outBuf = (Byte *) MyAlloc (_outBufSize);
if (_outBuf == 0)
return E_OUTOFMEMORY;
_outBufSizeAllocated = _outBufSize;
}
return S_OK;
}
STDMETHODIMP CDecoder::SetDecoderProperties2 (const Byte * prop, UInt32 size)
{
DProps *pProps = (DProps *) prop;
if (size != sizeof (DProps))
return E_FAIL;
// version 0.x currently
if (pProps->_ver_major != ZSTD_VERSION_MAJOR)
return E_FAIL;
switch (pProps->_ver_minor) {
case ZSTD_VERSION_MINOR:
break;
case 6:
break;
case 5:
break;
default:
return E_FAIL;
}
memcpy(&_props, pProps, sizeof (DProps));
_propsWereSet = true;
return CreateBuffers();
}
HRESULT CDecoder::CreateDecompressor()
{
if (!_propsWereSet)
return E_FAIL;
if (!_state) {
_state = ZB_createDCtx();
if (!_state)
return E_FAIL;
}
if (ZBUFF_isError(ZB_decompressInit(_state)))
return E_FAIL;
_eofFlag = false;
return S_OK;
}
HRESULT CDecoder::SetOutStreamSizeResume(const UInt64 * outSize)
{
_outSizeDefined = (outSize != NULL);
if (_outSizeDefined)
_outSize = *outSize;
_outSizeProcessed = 0;
RINOK (CreateDecompressor());
return S_OK;
}
STDMETHODIMP CDecoder::SetOutStreamSize (const UInt64 * outSize)
{
_inSizeProcessed = 0;
_inPos = _inSize = 0;
RINOK (SetOutStreamSizeResume (outSize));
return S_OK;
}
HRESULT CDecoder::CodeSpec (ISequentialInStream * inStream,
ISequentialOutStream * outStream,
ICompressProgressInfo * progress)
{
if (_inBuf == 0 || !_propsWereSet)
return S_FALSE;
if (!_state)
{
if (CreateDecompressor () != S_OK)
return E_FAIL;
}
UInt64 startInProgress = _inSizeProcessed;
for (;;)
{
if ((!_eofFlag) && (_inPos == _inSize))
{
_inPos = _inSize = 0;
RINOK (inStream->Read (_inBuf, _inBufSizeAllocated, &_inSize));
if (!_inSize)
_eofFlag = true;
}
Byte *pIn_bytes = _inBuf + _inPos;
size_t num_in_bytes = _inSize - _inPos;
Byte *pOut_bytes = _outBuf;
size_t num_out_bytes = _outBufSize;
if (_outSizeDefined)
{
UInt64 out_remaining = _outSize - _outSizeProcessed;
if (out_remaining == 0)
return S_OK;
if (num_out_bytes > out_remaining)
num_out_bytes = static_cast < size_t > (out_remaining);
}
size_t decomp_status =
ZB_decompressContinue (_state, pOut_bytes, &num_out_bytes,
pIn_bytes, &num_in_bytes);
bool decomp_finished = (decomp_status == 0);
bool decomp_failed = ZBUFF_isError (decomp_status) != 0;
if (num_in_bytes)
{
_inPos += (UInt32) num_in_bytes;
_inSizeProcessed += (UInt32) num_in_bytes;
}
if (num_out_bytes)
{
_outSizeProcessed += num_out_bytes;
RINOK (WriteStream (outStream, _outBuf, num_out_bytes));
}
if (decomp_failed)
{
PRF(fprintf(stderr, "zstdcodec: ZB_decompressContinue() failed: %s\n", ZBUFF_getErrorName(decomp_status)));
return S_FALSE;
}
if (decomp_finished)
break;
// This check is to prevent locking up if the input file is accidently truncated.
bool made_forward_progress = (num_out_bytes != 0) || (num_in_bytes != 0);
if ((!made_forward_progress) && (_eofFlag))
{
return S_FALSE;
}
UInt64 inSize = _inSizeProcessed - startInProgress;
if (progress)
{
RINOK (progress->SetRatioInfo (&inSize, &_outSizeProcessed));
}
}
return S_OK;
}
STDMETHODIMP CDecoder::Code (ISequentialInStream * inStream,
ISequentialOutStream * outStream,
const UInt64 * inSize,
const UInt64 * outSize, ICompressProgressInfo * progress)
{
(void) inSize;
if (_inBuf == 0)
return E_INVALIDARG;
SetOutStreamSize (outSize);
return CodeSpec (inStream, outStream, progress);
}
// wrapper for different versions
void *CDecoder::ZB_createDCtx(void)
{
PRF(fprintf(stderr, "zstdcodec: ZB_createDCtx(v=%d)\n", _props._ver_minor));
#ifndef EXTRACT_ONLY
switch (_props._ver_minor) {
case 5:
return (void*)ZBUFFv05_createDCtx();
break;
case 6:
return (void*)ZBUFFv06_createDCtx();
break;
}
#endif
return (void*)ZBUFF_createDCtx();
}
size_t CDecoder::ZB_freeDCtx(void *dctx)
{
PRF(fprintf(stderr, "zstdcodec: ZB_freeDCtx(v=%d)\n", _props._ver_minor));
#ifndef EXTRACT_ONLY
switch (_props._ver_minor) {
case 5:
return ZBUFFv05_freeDCtx((ZBUFFv05_DCtx *)dctx);
break;
case 6:
return ZBUFFv06_freeDCtx((ZBUFFv06_DCtx *)dctx);
break;
}
#endif
return ZBUFF_freeDCtx((ZBUFF_DCtx *)dctx);
}
size_t CDecoder::ZB_decompressInit(void *dctx)
{
PRF(fprintf(stderr, "zstdcodec: ZB_decompressInit(v=%d)\n", _props._ver_minor));
#ifndef EXTRACT_ONLY
switch (_props._ver_minor) {
case 5:
return ZBUFFv05_decompressInit((ZBUFFv05_DCtx *)dctx);
break;
case 6:
return ZBUFFv06_decompressInit((ZBUFFv06_DCtx *)dctx);
break;
}
#endif
return ZBUFF_decompressInit((ZBUFF_DCtx *)dctx);
}
size_t CDecoder::ZB_decompressContinue(void *dctx, void* dst, size_t *dstCapacityPtr, const void* src, size_t *srcSizePtr)
{
PRF(fprintf(stderr, "zstdcodec: ZB_decompressContinue(v=%d)\n", _props._ver_minor));
#ifndef EXTRACT_ONLY
switch (_props._ver_minor) {
case 5:
return ZBUFFv05_decompressContinue((ZBUFFv05_DCtx *)dctx, dst, dstCapacityPtr, src, srcSizePtr);
break;
case 6:
return ZBUFFv06_decompressContinue((ZBUFFv06_DCtx *)dctx, dst, dstCapacityPtr, src, srcSizePtr);
break;
}
#endif
return ZBUFF_decompressContinue((ZBUFF_DCtx *)dctx, dst, dstCapacityPtr, src, srcSizePtr);
}
#ifndef NO_READ_FROM_CODER
STDMETHODIMP CDecoder::SetInStream (ISequentialInStream * inStream)
{
_inStream = inStream;
return S_OK;
}
STDMETHODIMP CDecoder::ReleaseInStream ()
{
_inStream.Release ();
return S_OK;
}
STDMETHODIMP CDecoder::Read (void *data, UInt32 size, UInt32 * processedSize)
{
if (processedSize)
*processedSize = 0;
if (_inBuf == 0 || !_propsWereSet)
return S_FALSE;
if (!_state)
{
if (CreateDecompressor () != S_OK)
return E_FAIL;
}
while (size != 0)
{
if ((!_eofFlag) && (_inPos == _inSize))
{
_inPos = _inSize = 0;
RINOK (_inStream->Read (_inBuf, _inBufSizeAllocated, &_inSize));
if (!_inSize)
_eofFlag = true;
}
Byte *pIn_bytes = _inBuf + _inPos;
size_t num_in_bytes = _inSize - _inPos;
Byte *pOut_bytes = (Byte *) data;
size_t num_out_bytes = size;
size_t decomp_status =
ZB_decompressContinue (_state, pOut_bytes, &num_out_bytes,
pIn_bytes, &num_in_bytes);
bool
decomp_finished = (decomp_status == 0);
bool
decomp_failed = ZBUFF_isError (decomp_status) != 0;
if (num_in_bytes)
{
_inPos += (UInt32) num_in_bytes;
_inSizeProcessed += num_in_bytes;
}
if (num_out_bytes)
{
_outSizeProcessed += num_out_bytes;
size -= (UInt32) num_out_bytes;
if (processedSize)
*processedSize += (UInt32) num_out_bytes;
}
if (decomp_failed)
{
return S_FALSE;
}
if (decomp_finished)
break;
// This check is to prevent locking up if the input file is accidently truncated.
bool made_forward_progress = (num_out_bytes != 0) || (num_in_bytes != 0);
if ((!made_forward_progress) && (_eofFlag))
{
return S_FALSE;
}
}
return S_OK;
}
HRESULT CDecoder::CodeResume (ISequentialOutStream * outStream,
const UInt64 * outSize,
ICompressProgressInfo * progress)
{
RINOK (SetOutStreamSizeResume (outSize));
return CodeSpec (_inStream, outStream, progress);
}
#endif
}}
// ZstdDecoder.cpp
// (C) 2016 Rich Geldreich, Tino Reichardt
#include "StdAfx.h"
#include "ZstdDecoder.h"
// #define SHOW_DEBUG_INFO
#ifdef SHOW_DEBUG_INFO
#include <stdio.h>
#define PRF(x) x
#else
#define PRF(x)
#endif
namespace NCompress {
namespace NZSTD {
CDecoder::CDecoder ():
_inBuf (NULL),
_outBuf (NULL),
_inPos (0),
_inSize (0),
_eofFlag (false),
_state (NULL),
_propsWereSet (false),
_outSizeDefined (false),
_outSize (0),
_inSizeProcessed (0),
_outSizeProcessed (0),
_inBufSizeAllocated (0),
_outBufSizeAllocated (0),
_inBufSize (ZBUFF_recommendedDInSize()),
_outBufSize (ZBUFF_recommendedDOutSize())
{
_props.clear ();
}
CDecoder::~CDecoder ()
{
if (_state)
ZB_freeDCtx(_state);
MyFree (_inBuf);
MyFree (_outBuf);
}
STDMETHODIMP CDecoder::SetInBufSize (UInt32, UInt32 size)
{
_inBufSize = size;
return S_OK;
}
STDMETHODIMP CDecoder::SetOutBufSize (UInt32, UInt32 size)
{
_outBufSize = size;
return S_OK;
}
HRESULT CDecoder::CreateBuffers ()
{
if (_inBuf == 0 || _inBufSize != _inBufSizeAllocated)
{
MyFree (_inBuf);
_inBuf = (Byte *) MyAlloc (_inBufSize);
if (_inBuf == 0)
return E_OUTOFMEMORY;
_inBufSizeAllocated = (UInt32)_inBufSize;
}
if (_outBuf == 0 || _outBufSize != _outBufSizeAllocated)
{
MyFree (_outBuf);
_outBuf = (Byte *) MyAlloc (_outBufSize);
if (_outBuf == 0)
return E_OUTOFMEMORY;
_outBufSizeAllocated = (UInt32)_outBufSize;
}
return S_OK;
}
STDMETHODIMP CDecoder::SetDecoderProperties2 (const Byte * prop, UInt32 size)
{
DProps *pProps = (DProps *) prop;
if (size != sizeof (DProps))
return E_FAIL;
// version 0.x currently
if (pProps->_ver_major != ZSTD_VERSION_MAJOR)
return E_FAIL;
switch (pProps->_ver_minor) {
case ZSTD_VERSION_MINOR:
break;
#ifdef ZSTD_LEGACY
case 5:
break;
case 6:
break;
case 7:
break;
#endif
default:
return E_FAIL;
}
memcpy(&_props, pProps, sizeof (DProps));
_propsWereSet = true;
return CreateBuffers();
}
HRESULT CDecoder::CreateDecompressor()
{
if (!_propsWereSet)
return E_FAIL;
if (!_state) {
_state = ZB_createDCtx();
if (!_state)
return E_FAIL;
}
if (ZBUFF_isError(ZB_decompressInit(_state)))
return E_FAIL;
_eofFlag = false;
return S_OK;
}
HRESULT CDecoder::SetOutStreamSizeResume(const UInt64 * outSize)
{
_outSizeDefined = (outSize != NULL);
if (_outSizeDefined)
_outSize = *outSize;
_outSizeProcessed = 0;
RINOK (CreateDecompressor());
return S_OK;
}
STDMETHODIMP CDecoder::SetOutStreamSize (const UInt64 * outSize)
{
_inSizeProcessed = 0;
_inPos = _inSize = 0;
RINOK (SetOutStreamSizeResume (outSize));
return S_OK;
}
HRESULT CDecoder::CodeSpec (ISequentialInStream * inStream,
ISequentialOutStream * outStream,
ICompressProgressInfo * progress)
{
if (_inBuf == 0 || !_propsWereSet)
return S_FALSE;
if (!_state)
{
if (CreateDecompressor () != S_OK)
return E_FAIL;
}
UInt64 startInProgress = _inSizeProcessed;
for (;;)
{
if ((!_eofFlag) && (_inPos == _inSize))
{
_inPos = _inSize = 0;
RINOK (inStream->Read (_inBuf, _inBufSizeAllocated, &_inSize));
if (!_inSize)
_eofFlag = true;
}
Byte *pIn_bytes = _inBuf + _inPos;
size_t num_in_bytes = _inSize - _inPos;
Byte *pOut_bytes = _outBuf;
size_t num_out_bytes = _outBufSize;
if (_outSizeDefined)
{
UInt64 out_remaining = _outSize - _outSizeProcessed;
if (out_remaining == 0)
return S_OK;
if (num_out_bytes > out_remaining)
num_out_bytes = static_cast < size_t > (out_remaining);
}
size_t decomp_status =
ZB_decompressContinue (_state, pOut_bytes, &num_out_bytes,
pIn_bytes, &num_in_bytes);
bool decomp_finished = (decomp_status == 0);
bool decomp_failed = ZBUFF_isError (decomp_status) != 0;
if (num_in_bytes)
{
_inPos += (UInt32) num_in_bytes;
_inSizeProcessed += (UInt32) num_in_bytes;
}
if (num_out_bytes)
{
_outSizeProcessed += num_out_bytes;
RINOK (WriteStream (outStream, _outBuf, num_out_bytes));
}
if (decomp_failed)
{
PRF(fprintf(stderr, "zstdcodec: ZB_decompressContinue() failed: %s\n", ZBUFF_getErrorName(decomp_status)));
return S_FALSE;
}
if (decomp_finished)
break;
// This check is to prevent locking up if the input file is accidently truncated.
bool made_forward_progress = (num_out_bytes != 0) || (num_in_bytes != 0);
if ((!made_forward_progress) && (_eofFlag))
{
return S_FALSE;
}
UInt64 inSize = _inSizeProcessed - startInProgress;
if (progress)
{
RINOK (progress->SetRatioInfo (&inSize, &_outSizeProcessed));
}
}
return S_OK;
}
STDMETHODIMP CDecoder::Code (ISequentialInStream * inStream,
ISequentialOutStream * outStream,
const UInt64 * inSize,
const UInt64 * outSize, ICompressProgressInfo * progress)
{
(void) inSize;
if (_inBuf == 0)
return E_INVALIDARG;
SetOutStreamSize (outSize);
return CodeSpec (inStream, outStream, progress);
}
// wrapper for different versions
void *CDecoder::ZB_createDCtx(void)
{
PRF(fprintf(stderr, "zstdcodec: ZB_createDCtx(v=%d)\n", _props._ver_minor));
#ifdef ZSTD_LEGACY
switch (_props._ver_minor) {
case 5:
return (void*)ZBUFFv05_createDCtx();
break;
case 6:
return (void*)ZBUFFv06_createDCtx();
break;
case 7:
return (void*)ZBUFFv07_createDCtx();
break;
}
#endif
return (void*)ZBUFF_createDCtx();
}
size_t CDecoder::ZB_freeDCtx(void *dctx)
{
PRF(fprintf(stderr, "zstdcodec: ZB_freeDCtx(v=%d)\n", _props._ver_minor));
#ifdef ZSTD_LEGACY
switch (_props._ver_minor) {
case 5:
return ZBUFFv05_freeDCtx((ZBUFFv05_DCtx *)dctx);
break;
case 6:
return ZBUFFv06_freeDCtx((ZBUFFv06_DCtx *)dctx);
break;
case 7:
return ZBUFFv07_freeDCtx((ZBUFFv07_DCtx *)dctx);
break;
}
#endif
return ZBUFF_freeDCtx((ZBUFF_DCtx *)dctx);
}
size_t CDecoder::ZB_decompressInit(void *dctx)
{
PRF(fprintf(stderr, "zstdcodec: ZB_decompressInit(v=%d)\n", _props._ver_minor));
#ifdef ZSTD_LEGACY
switch (_props._ver_minor) {
case 5:
return ZBUFFv05_decompressInit((ZBUFFv05_DCtx *)dctx);
break;
case 6:
return ZBUFFv06_decompressInit((ZBUFFv06_DCtx *)dctx);
break;
case 7:
return ZBUFFv07_decompressInit((ZBUFFv07_DCtx *)dctx);
break;
}
#endif
return ZBUFF_decompressInit((ZBUFF_DCtx *)dctx);
}
size_t CDecoder::ZB_decompressContinue(void *dctx, void* dst, size_t *dstCapacityPtr, const void* src, size_t *srcSizePtr)
{
PRF(fprintf(stderr, "zstdcodec: ZB_decompressContinue(v=%d)\n", _props._ver_minor));
#ifdef ZSTD_LEGACY
switch (_props._ver_minor) {
case 5:
return ZBUFFv05_decompressContinue((ZBUFFv05_DCtx *)dctx, dst, dstCapacityPtr, src, srcSizePtr);
break;
case 6:
return ZBUFFv06_decompressContinue((ZBUFFv06_DCtx *)dctx, dst, dstCapacityPtr, src, srcSizePtr);
break;
case 7:
return ZBUFFv07_decompressContinue((ZBUFFv07_DCtx *)dctx, dst, dstCapacityPtr, src, srcSizePtr);
break;
}
#endif
return ZBUFF_decompressContinue((ZBUFF_DCtx *)dctx, dst, dstCapacityPtr, src, srcSizePtr);
}
#ifndef NO_READ_FROM_CODER
STDMETHODIMP CDecoder::SetInStream (ISequentialInStream * inStream)
{
_inStream = inStream;
return S_OK;
}
STDMETHODIMP CDecoder::ReleaseInStream ()
{
_inStream.Release ();
return S_OK;
}
STDMETHODIMP CDecoder::Read (void *data, UInt32 size, UInt32 * processedSize)
{
if (processedSize)
*processedSize = 0;
if (_inBuf == 0 || !_propsWereSet)
return S_FALSE;
if (!_state)
{
if (CreateDecompressor () != S_OK)
return E_FAIL;
}
while (size != 0)
{
if ((!_eofFlag) && (_inPos == _inSize))
{
_inPos = _inSize = 0;
RINOK (_inStream->Read (_inBuf, _inBufSizeAllocated, &_inSize));
if (!_inSize)
_eofFlag = true;
}
Byte *pIn_bytes = _inBuf + _inPos;
size_t num_in_bytes = _inSize - _inPos;
Byte *pOut_bytes = (Byte *) data;
size_t num_out_bytes = size;
size_t decomp_status =
ZB_decompressContinue (_state, pOut_bytes, &num_out_bytes,
pIn_bytes, &num_in_bytes);
bool
decomp_finished = (decomp_status == 0);
bool
decomp_failed = ZBUFF_isError (decomp_status) != 0;
if (num_in_bytes)
{
_inPos += (UInt32) num_in_bytes;
_inSizeProcessed += num_in_bytes;
}
if (num_out_bytes)
{
_outSizeProcessed += num_out_bytes;
size -= (UInt32) num_out_bytes;
if (processedSize)
*processedSize += (UInt32) num_out_bytes;
}
if (decomp_failed)
{
return S_FALSE;
}
if (decomp_finished)
break;
// This check is to prevent locking up if the input file is accidently truncated.
bool made_forward_progress = (num_out_bytes != 0) || (num_in_bytes != 0);
if ((!made_forward_progress) && (_eofFlag))
{
return S_FALSE;
}
}
return S_OK;
}
HRESULT CDecoder::CodeResume (ISequentialOutStream * outStream,
const UInt64 * outSize,
ICompressProgressInfo * progress)
{
RINOK (SetOutStreamSizeResume (outSize));
return CodeSpec (_inStream, outStream, progress);
}
#endif
}}

View File

@@ -1,111 +1,109 @@
// ZstdDecoder.h
// (C) 2016 Rich Geldreich, Tino Reichardt
#include "StdAfx.h"
#define ZSTD_STATIC_LINKING_ONLY
#include "../../../C/Alloc.h"
#include "../../../C/ZStd/zstd.h"
#include "../../../C/ZStd/zbuff.h"
#include "../../../C/ZStd/zstd_legacy.h"
#include "../../Common/Common.h"
#include "../../Common/MyCom.h"
#include "../ICoder.h"
#include "../Common/StreamUtils.h"
#include "../Common/RegisterCodec.h"
namespace NCompress {
namespace NZSTD {
struct DProps
{
DProps() { clear (); }
void clear ()
{
memset (this, 0, sizeof (*this));
_ver_major = ZSTD_VERSION_MAJOR;
_ver_minor = ZSTD_VERSION_MINOR;
_level = 1;
}
Byte _ver_major;
Byte _ver_minor;
Byte _level;
Byte _reserved[2];
};
class CDecoder:public ICompressCoder,
public ICompressSetDecoderProperties2, public ICompressSetBufSize,
#ifndef NO_READ_FROM_CODER
public ICompressSetInStream,
public ICompressSetOutStreamSize, public ISequentialInStream,
#endif
public CMyUnknownImp
{
CMyComPtr < ISequentialInStream > _inStream;
Byte *_inBuf;
Byte *_outBuf;
UInt32 _inPos;
UInt32 _inSize;
bool _eofFlag;
void *_state;
DProps _props;
bool _propsWereSet;
bool _outSizeDefined;
UInt64 _outSize;
UInt64 _inSizeProcessed;
UInt64 _outSizeProcessed;
UInt32 _inBufSizeAllocated;
UInt32 _outBufSizeAllocated;
UInt32 _inBufSize;
UInt32 _outBufSize;
HRESULT CreateBuffers ();
HRESULT CodeSpec (ISequentialInStream * inStream, ISequentialOutStream * outStream, ICompressProgressInfo * progress);
HRESULT SetOutStreamSizeResume (const UInt64 * outSize);
HRESULT CreateDecompressor ();
// wrapper for different versions
void *ZB_createDCtx(void);
size_t ZB_freeDCtx(void *dctx);
size_t ZB_decompressInit(void *dctx);
size_t ZB_decompressContinue(void *dctx, void* dst, size_t *dstCapacityPtr, const void* src, size_t *srcSizePtr);
public:
MY_QUERYINTERFACE_BEGIN2 (ICompressCoder)
MY_QUERYINTERFACE_ENTRY (ICompressSetDecoderProperties2)
MY_QUERYINTERFACE_ENTRY (ICompressSetBufSize)
#ifndef NO_READ_FROM_CODER
MY_QUERYINTERFACE_ENTRY (ICompressSetInStream)
MY_QUERYINTERFACE_ENTRY (ICompressSetOutStreamSize)
MY_QUERYINTERFACE_ENTRY (ISequentialInStream)
#endif
MY_QUERYINTERFACE_END
MY_ADDREF_RELEASE
STDMETHOD (Code)(ISequentialInStream * inStream, ISequentialOutStream * outStream, const UInt64 * inSize, const UInt64 * outSize, ICompressProgressInfo * progress);
STDMETHOD (SetDecoderProperties2) (const Byte * data, UInt32 size);
STDMETHOD (SetOutStreamSize) (const UInt64 * outSize);
STDMETHOD (SetInBufSize) (UInt32 streamIndex, UInt32 size);
STDMETHOD (SetOutBufSize) (UInt32 streamIndex, UInt32 size);
#ifndef NO_READ_FROM_CODER
STDMETHOD (SetInStream) (ISequentialInStream * inStream);
STDMETHOD (ReleaseInStream) ();
STDMETHOD (Read) (void *data, UInt32 size, UInt32 * processedSize);
HRESULT CodeResume (ISequentialOutStream * outStream, const UInt64 * outSize, ICompressProgressInfo * progress);
UInt64 GetInputProcessedSize () const { return _inSizeProcessed; }
#endif
CDecoder();
virtual ~CDecoder();
};
}}
// ZstdDecoder.h
// (C) 2016 Rich Geldreich, Tino Reichardt
#define ZSTD_STATIC_LINKING_ONLY
#include "../../../C/Alloc.h"
#include "../../../C/ZStd/zstd.h"
#include "../../../C/ZStd/zbuff.h"
#include "../../../C/ZStd/zstd_legacy.h"
#include "../../Common/Common.h"
#include "../../Common/MyCom.h"
#include "../ICoder.h"
#include "../Common/StreamUtils.h"
#include "../Common/RegisterCodec.h"
namespace NCompress {
namespace NZSTD {
struct DProps
{
DProps() { clear (); }
void clear ()
{
memset (this, 0, sizeof (*this));
_ver_major = ZSTD_VERSION_MAJOR;
_ver_minor = ZSTD_VERSION_MINOR;
_level = 1;
}
Byte _ver_major;
Byte _ver_minor;
Byte _level;
Byte _reserved[2];
};
class CDecoder:public ICompressCoder,
public ICompressSetDecoderProperties2, public ICompressSetBufSize,
#ifndef NO_READ_FROM_CODER
public ICompressSetInStream,
public ICompressSetOutStreamSize, public ISequentialInStream,
#endif
public CMyUnknownImp
{
CMyComPtr < ISequentialInStream > _inStream;
Byte *_inBuf;
Byte *_outBuf;
UInt32 _inPos;
UInt32 _inSize;
bool _eofFlag;
void *_state;
DProps _props;
bool _propsWereSet;
bool _outSizeDefined;
UInt64 _outSize;
UInt64 _inSizeProcessed;
UInt64 _outSizeProcessed;
UInt32 _inBufSizeAllocated;
UInt32 _outBufSizeAllocated;
size_t _inBufSize;
size_t _outBufSize;
HRESULT CreateBuffers ();
HRESULT CodeSpec (ISequentialInStream * inStream, ISequentialOutStream * outStream, ICompressProgressInfo * progress);
HRESULT SetOutStreamSizeResume (const UInt64 * outSize);
HRESULT CreateDecompressor ();
// wrapper for different versions
void *ZB_createDCtx(void);
size_t ZB_freeDCtx(void *dctx);
size_t ZB_decompressInit(void *dctx);
size_t ZB_decompressContinue(void *dctx, void* dst, size_t *dstCapacityPtr, const void* src, size_t *srcSizePtr);
public:
MY_QUERYINTERFACE_BEGIN2 (ICompressCoder)
MY_QUERYINTERFACE_ENTRY (ICompressSetDecoderProperties2)
MY_QUERYINTERFACE_ENTRY (ICompressSetBufSize)
#ifndef NO_READ_FROM_CODER
MY_QUERYINTERFACE_ENTRY (ICompressSetInStream)
MY_QUERYINTERFACE_ENTRY (ICompressSetOutStreamSize)
MY_QUERYINTERFACE_ENTRY (ISequentialInStream)
#endif
MY_QUERYINTERFACE_END
MY_ADDREF_RELEASE
STDMETHOD (Code)(ISequentialInStream * inStream, ISequentialOutStream * outStream, const UInt64 * inSize, const UInt64 * outSize, ICompressProgressInfo * progress);
STDMETHOD (SetDecoderProperties2) (const Byte * data, UInt32 size);
STDMETHOD (SetOutStreamSize) (const UInt64 * outSize);
STDMETHOD (SetInBufSize) (UInt32 streamIndex, UInt32 size);
STDMETHOD (SetOutBufSize) (UInt32 streamIndex, UInt32 size);
#ifndef NO_READ_FROM_CODER
STDMETHOD (SetInStream) (ISequentialInStream * inStream);
STDMETHOD (ReleaseInStream) ();
STDMETHOD (Read) (void *data, UInt32 size, UInt32 * processedSize);
HRESULT CodeResume (ISequentialOutStream * outStream, const UInt64 * outSize, ICompressProgressInfo * progress);
UInt64 GetInputProcessedSize () const { return _inSizeProcessed; }
#endif
CDecoder();
virtual ~CDecoder();
};
}}

View File

@@ -1,192 +1,190 @@
// ZstdEncoder.cpp
// (C) 2016 Rich Geldreich, Tino Reichardt
#include "StdAfx.h"
#include "ZstdEncoder.h"
// #define SHOW_DEBUG_INFO
#ifdef SHOW_DEBUG_INFO
#include <stdio.h>
#define PRF(x) x
#else
#define PRF(x)
#endif
#define ZSTD_DEFAULT_BUFFER_SIZE (1U << 22U)
#ifndef EXTRACT_ONLY
namespace NCompress {
namespace NZSTD {
CEncoder::CEncoder():
_state (NULL),
_inBuf (NULL),
_outBuf (NULL),
_inPos (0),
_inSize (0),
_inBufSizeAllocated (0),
_outBufSizeAllocated (0),
_inBufSize (ZSTD_DEFAULT_BUFFER_SIZE),
_outBufSize (ZSTD_DEFAULT_BUFFER_SIZE),
_inSizeProcessed (0),
_outSizeProcessed (0)
{
_props.clear ();
}
CEncoder::~CEncoder ()
{
if (_state)
ZBUFF_freeCCtx(_state);
MyFree (_inBuf);
MyFree (_outBuf);
}
STDMETHODIMP CEncoder::SetCoderProperties (const PROPID * propIDs,
const PROPVARIANT * coderProps, UInt32 numProps)
{
_props.clear ();
for (UInt32 i = 0; i < numProps; i++)
{
const PROPVARIANT & prop = coderProps[i];
PROPID propID = propIDs[i];
switch (propID)
{
case NCoderPropID::kLevel:
{
if (prop.vt != VT_UI4)
return E_INVALIDARG;
_props._level = static_cast < Byte > (prop.ulVal);
Byte zstd_level = static_cast < Byte > (ZSTD_maxCLevel());
if (_props._level > zstd_level)
_props._level = zstd_level;
break;
}
default:
{
break;
}
}
}
return S_OK;
}
STDMETHODIMP CEncoder::WriteCoderProperties (ISequentialOutStream * outStream)
{
return WriteStream (outStream, &_props, sizeof (_props));
}
HRESULT CEncoder::CreateBuffers ()
{
if (_inBuf == 0 || _inBufSize != _inBufSizeAllocated)
{
MyFree (_inBuf);
_inBuf = (Byte *) MyAlloc (_inBufSize);
if (_inBuf == 0)
return E_OUTOFMEMORY;
_inBufSizeAllocated = _inBufSize;
}
if (_outBuf == 0 || _outBufSize != _outBufSizeAllocated)
{
MyFree (_outBuf);
_outBuf = (Byte *) MyAlloc (_outBufSize);
if (_outBuf == 0)
return E_OUTOFMEMORY;
_outBufSizeAllocated = _outBufSize;
}
return S_OK;
}
HRESULT CEncoder::CreateCompressor ()
{
if (!_state) {
_state = ZBUFF_createCCtx();
if (!_state)
return S_FALSE;
}
if (ZBUFF_compressInit(_state, _props._level))
return S_FALSE;
return S_OK;
}
STDMETHODIMP CEncoder::Code (ISequentialInStream * inStream,
ISequentialOutStream * outStream, const UInt64 * /* inSize */ ,
const UInt64 * /* outSize */ , ICompressProgressInfo * progress)
{
RINOK (CreateCompressor());
RINOK (CreateBuffers());
UInt64 startInProgress = _inSizeProcessed;
UInt64 startOutProgress = _outSizeProcessed;
for (;;)
{
bool eofFlag = false;
if (_inPos == _inSize)
{
_inPos = _inSize = 0;
RINOK (inStream->Read (_inBuf, _inBufSizeAllocated, &_inSize));
if (!_inSize)
eofFlag = true;
}
Byte *pIn_bytes = _inBuf + _inPos;
size_t num_in_bytes = _inSize - _inPos;
Byte *pOut_bytes = _outBuf;
size_t num_out_bytes = _outBufSize;
size_t comp_status;
bool comp_finished = false;
if (eofFlag)
{
comp_status = ZBUFF_compressEnd (_state, pOut_bytes, &num_out_bytes);
comp_finished = (comp_status == 0);
}
else
{
comp_status = ZBUFF_compressContinue(_state, pOut_bytes, &num_out_bytes, pIn_bytes, &num_in_bytes);
}
bool comp_failed = ZBUFF_isError (comp_status) != 0;
if (num_in_bytes)
{
_inPos += (UInt32) num_in_bytes;
_inSizeProcessed += (UInt32) num_in_bytes;
}
if (num_out_bytes)
{
_outSizeProcessed += num_out_bytes;
RINOK (WriteStream (outStream, _outBuf, num_out_bytes));
}
if (comp_failed)
return S_FALSE;
if (comp_finished)
break;
UInt64 inSize = _inSizeProcessed - startInProgress;
UInt64 outSize = _outSizeProcessed - startOutProgress;
if (progress)
{
RINOK (progress->SetRatioInfo (&inSize, &outSize));
}
}
return S_OK;
}
}}
#endif
// ZstdEncoder.cpp
// (C) 2016 Rich Geldreich, Tino Reichardt
#include "StdAfx.h"
#include "ZstdEncoder.h"
// #define SHOW_DEBUG_INFO
#ifdef SHOW_DEBUG_INFO
#include <stdio.h>
#define PRF(x) x
#else
#define PRF(x)
#endif
#ifndef EXTRACT_ONLY
namespace NCompress {
namespace NZSTD {
CEncoder::CEncoder():
_state (NULL),
_inBuf (NULL),
_outBuf (NULL),
_inPos (0),
_inSize (0),
_inBufSizeAllocated (0),
_outBufSizeAllocated (0),
_inBufSize (ZBUFF_recommendedCInSize()),
_outBufSize (ZBUFF_recommendedCOutSize()),
_inSizeProcessed (0),
_outSizeProcessed (0)
{
_props.clear ();
}
CEncoder::~CEncoder ()
{
if (_state)
ZBUFF_freeCCtx(_state);
MyFree (_inBuf);
MyFree (_outBuf);
}
STDMETHODIMP CEncoder::SetCoderProperties (const PROPID * propIDs,
const PROPVARIANT * coderProps, UInt32 numProps)
{
_props.clear ();
for (UInt32 i = 0; i < numProps; i++)
{
const PROPVARIANT & prop = coderProps[i];
PROPID propID = propIDs[i];
switch (propID)
{
case NCoderPropID::kLevel:
{
if (prop.vt != VT_UI4)
return E_INVALIDARG;
_props._level = static_cast < Byte > (prop.ulVal);
Byte zstd_level = static_cast < Byte > (ZSTD_maxCLevel());
if (_props._level > zstd_level)
_props._level = zstd_level;
break;
}
default:
{
break;
}
}
}
return S_OK;
}
STDMETHODIMP CEncoder::WriteCoderProperties (ISequentialOutStream * outStream)
{
return WriteStream (outStream, &_props, sizeof (_props));
}
HRESULT CEncoder::CreateBuffers ()
{
if (_inBuf == 0 || _inBufSize != _inBufSizeAllocated)
{
MyFree (_inBuf);
_inBuf = (Byte *) MyAlloc (_inBufSize);
if (_inBuf == 0)
return E_OUTOFMEMORY;
_inBufSizeAllocated = (UInt32)_inBufSize;
}
if (_outBuf == 0 || _outBufSize != _outBufSizeAllocated)
{
MyFree (_outBuf);
_outBuf = (Byte *) MyAlloc (_outBufSize);
if (_outBuf == 0)
return E_OUTOFMEMORY;
_outBufSizeAllocated = (UInt32)_outBufSize;
}
return S_OK;
}
HRESULT CEncoder::CreateCompressor ()
{
if (!_state) {
_state = ZBUFF_createCCtx();
if (!_state)
return S_FALSE;
}
if (ZBUFF_compressInit(_state, _props._level))
return S_FALSE;
return S_OK;
}
STDMETHODIMP CEncoder::Code (ISequentialInStream * inStream,
ISequentialOutStream * outStream, const UInt64 * /* inSize */ ,
const UInt64 * /* outSize */ , ICompressProgressInfo * progress)
{
RINOK (CreateCompressor());
RINOK (CreateBuffers());
UInt64 startInProgress = _inSizeProcessed;
UInt64 startOutProgress = _outSizeProcessed;
for (;;)
{
bool eofFlag = false;
if (_inPos == _inSize)
{
_inPos = _inSize = 0;
RINOK (inStream->Read (_inBuf, _inBufSizeAllocated, &_inSize));
if (!_inSize)
eofFlag = true;
}
Byte *pIn_bytes = _inBuf + _inPos;
size_t num_in_bytes = _inSize - _inPos;
Byte *pOut_bytes = _outBuf;
size_t num_out_bytes = _outBufSize;
size_t comp_status;
bool comp_finished = false;
if (eofFlag)
{
comp_status = ZBUFF_compressEnd (_state, pOut_bytes, &num_out_bytes);
comp_finished = (comp_status == 0);
}
else
{
comp_status = ZBUFF_compressContinue(_state, pOut_bytes, &num_out_bytes, pIn_bytes, &num_in_bytes);
}
bool comp_failed = ZBUFF_isError (comp_status) != 0;
if (num_in_bytes)
{
_inPos += (UInt32) num_in_bytes;
_inSizeProcessed += (UInt32) num_in_bytes;
}
if (num_out_bytes)
{
_outSizeProcessed += num_out_bytes;
RINOK (WriteStream (outStream, _outBuf, num_out_bytes));
}
if (comp_failed)
return S_FALSE;
if (comp_finished)
break;
UInt64 inSize = _inSizeProcessed - startInProgress;
UInt64 outSize = _outSizeProcessed - startOutProgress;
if (progress)
{
RINOK (progress->SetRatioInfo (&inSize, &outSize));
}
}
return S_OK;
}
}}
#endif

View File

@@ -1,77 +1,75 @@
// ZstdEncoder.h
// (C) 2016 Rich Geldreich, Tino Reichardt
#include "StdAfx.h"
#define ZSTD_STATIC_LINKING_ONLY
#include "../../../C/Alloc.h"
#include "../../../C/ZStd/zstd.h"
#include "../../../C/ZStd/zbuff.h"
#include "../../Common/Common.h"
#include "../../Common/MyCom.h"
#include "../ICoder.h"
#include "../Common/StreamUtils.h"
#ifndef EXTRACT_ONLY
namespace NCompress {
namespace NZSTD {
struct CProps
{
CProps() { clear (); }
void clear ()
{
memset (this, 0, sizeof (*this));
_ver_major = ZSTD_VERSION_MAJOR;
_ver_minor = ZSTD_VERSION_MINOR;
_level = 3;
}
Byte _ver_major;
Byte _ver_minor;
Byte _level;
Byte _reserved[2];
};
class CEncoder:
public ICompressCoder,
public ICompressSetCoderProperties,
public ICompressWriteCoderProperties,
public CMyUnknownImp
{
ZBUFF_CCtx *_state;
CProps _props;
Byte *_inBuf;
Byte *_outBuf;
UInt32 _inPos;
UInt32 _inSize;
UInt32 _inBufSizeAllocated;
UInt32 _outBufSizeAllocated;
UInt32 _inBufSize;
UInt32 _outBufSize;
UInt64 _inSizeProcessed;
UInt64 _outSizeProcessed;
HRESULT CreateCompressor ();
HRESULT CreateBuffers ();
public:
MY_UNKNOWN_IMP2 (ICompressSetCoderProperties, ICompressWriteCoderProperties)
STDMETHOD (Code) (ISequentialInStream * inStream, ISequentialOutStream *
outStream, const UInt64 * inSize, const UInt64 * outSize,
ICompressProgressInfo * progress);
STDMETHOD (SetCoderProperties) (const PROPID * propIDs,
const PROPVARIANT *props, UInt32 numProps);
STDMETHOD (WriteCoderProperties) (ISequentialOutStream * outStream);
CEncoder();
virtual ~CEncoder();
};
}}
#endif
// ZstdEncoder.h
// (C) 2016 Rich Geldreich, Tino Reichardt
#define ZSTD_STATIC_LINKING_ONLY
#include "../../../C/Alloc.h"
#include "../../../C/ZStd/zstd.h"
#include "../../../C/ZStd/zbuff.h"
#include "../../Common/Common.h"
#include "../../Common/MyCom.h"
#include "../ICoder.h"
#include "../Common/StreamUtils.h"
#ifndef EXTRACT_ONLY
namespace NCompress {
namespace NZSTD {
struct CProps
{
CProps() { clear (); }
void clear ()
{
memset (this, 0, sizeof (*this));
_ver_major = ZSTD_VERSION_MAJOR;
_ver_minor = ZSTD_VERSION_MINOR;
_level = 3;
}
Byte _ver_major;
Byte _ver_minor;
Byte _level;
Byte _reserved[2];
};
class CEncoder:
public ICompressCoder,
public ICompressSetCoderProperties,
public ICompressWriteCoderProperties,
public CMyUnknownImp
{
ZBUFF_CCtx *_state;
CProps _props;
Byte *_inBuf;
Byte *_outBuf;
UInt32 _inPos;
UInt32 _inSize;
UInt32 _inBufSizeAllocated;
UInt32 _outBufSizeAllocated;
size_t _inBufSize;
size_t _outBufSize;
UInt64 _inSizeProcessed;
UInt64 _outSizeProcessed;
HRESULT CreateCompressor ();
HRESULT CreateBuffers ();
public:
MY_UNKNOWN_IMP2 (ICompressSetCoderProperties, ICompressWriteCoderProperties)
STDMETHOD (Code) (ISequentialInStream * inStream, ISequentialOutStream *
outStream, const UInt64 * inSize, const UInt64 * outSize,
ICompressProgressInfo * progress);
STDMETHOD (SetCoderProperties) (const PROPID * propIDs,
const PROPVARIANT *props, UInt32 numProps);
STDMETHOD (WriteCoderProperties) (ISequentialOutStream * outStream);
CEncoder();
virtual ~CEncoder();
};
}}
#endif

View File

@@ -1,18 +1,18 @@
// ZstdRegister.cpp
// (C) 2016 Rich Geldreich, Tino Reichardt
#include "StdAfx.h"
#include "../Common/RegisterCodec.h"
#include "ZstdDecoder.h"
#ifndef EXTRACT_ONLY
#include "ZstdEncoder.h"
#endif
REGISTER_CODEC_E(
ZSTD,
NCompress::NZSTD::CDecoder(),
NCompress::NZSTD::CEncoder(),
0x4F71101, "ZSTD")
// ZstdRegister.cpp
// (C) 2016 Rich Geldreich, Tino Reichardt
#include "StdAfx.h"
#include "../Common/RegisterCodec.h"
#include "ZstdDecoder.h"
#ifndef EXTRACT_ONLY
#include "ZstdEncoder.h"
#endif
REGISTER_CODEC_E(
ZSTD,
NCompress::NZSTD::CDecoder(),
NCompress::NZSTD::CEncoder(),
0x4F71101, "ZSTD")

14
CPP/7zip/zstd.mak Normal file
View File

@@ -0,0 +1,14 @@
COMPRESS_OBJS = $(COMPRESS_OBJS) \
$O\ZstdDecoder.obj \
$O\ZstdEncoder.obj \
$O\ZstdRegister.obj \
ZSTD_OBJS = \
$O\entropy_common.obj \
$O\fse_decompress.obj \
$O\huf_decompress.obj \
$O\zbuff_decompress.obj \
$O\zstd_common.obj \
$O\zstd_decompress.obj \
$O\xxhash.obj \