Skip to content

Commit

Permalink
enabled pkimplode and zstd on zip
Browse files Browse the repository at this point in the history
  • Loading branch information
cielavenir committed Apr 1, 2022
1 parent 37c82c1 commit 081bc28
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 2 deletions.
16 changes: 15 additions & 1 deletion CPP/7zip/7zip_gcc_additional.mak
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ $O/lzham_lib.o: ../../../../Codecs/lzham_codec_devel/lzhamlib/lzham_lib.cpp
$O/LzhamRegister.o: ../../Compress/LzhamRegister.cpp
$(CXX) $(CXXFLAGS) $< -Wno-reorder -Wno-unused-parameter -Wno-unused-variable -I ../../../.. -I ../../../../CPP -I ../../../../Codecs/lzham_codec_devel/include -I ../../../../Codecs/lzham_codec_devel/lzhamdecomp

# Build pkimplode lib
$O/explode.o: ../../../../Codecs/StormLib/src/pklib/explode.c
$(CC) $(CFLAGS) $<
$O/implode.o: ../../../../Codecs/StormLib/src/pklib/implode.c
$(CC) $(CFLAGS) $< -Wno-implicit-fallthrough

# Compile pkimplode method
$O/PKImplodeDecoder.o: ../../Compress/PKImplodeDecoder.cpp
$(CXX) $(CXXFLAGS) $<
$O/PKImplodeEncoder.o: ../../Compress/PKImplodeEncoder.cpp
$(CXX) $(CXXFLAGS) $<
$O/PKImplodeRegister.o: ../../Compress/PKImplodeRegister.cpp
$(CXX) $(CXXFLAGS) $<

# Build hashes lib
$O/md2.o: ../../../../Codecs/hashes/md2.c
$(CC) $(CFLAGS) $<
Expand All @@ -156,7 +170,7 @@ $O/md5.o: ../../../../Codecs/hashes/md5.c
$O/sha512.o: ../../../../Codecs/hashes/sha512.c
$(CC) $(CFLAGS) $<

# Compile hashes Handler
# Compile hashes method
$O/Md2Reg.o: ../../../Common/Md2Reg.cpp
$(CXX) $(CXXFLAGS) $<
$O/Md4Reg.o: ../../../Common/Md4Reg.cpp
Expand Down
16 changes: 16 additions & 0 deletions CPP/7zip/Archive/Zip/ZipAddCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "../../Compress/LzmaEncoder.h"
#include "../../Compress/PpmdZip.h"
#include "../../Compress/XzEncoder.h"
#include "../../Compress/ZstdEncoder.h"
#include "../../Compress/PKImplodeEncoder.h"

#include "../Common/InStreamWithCRC.h"

Expand Down Expand Up @@ -183,6 +185,8 @@ HRESULT CAddCommon::Set_Pre_CompressionResult(bool inSeqMode, bool outSeqMode, U
case NCompressionMethod::kXz : ver = NCompressionMethod::kExtractVersion_Xz; break;
case NCompressionMethod::kPPMd : ver = NCompressionMethod::kExtractVersion_PPMd; break;
case NCompressionMethod::kBZip2: ver = NCompressionMethod::kExtractVersion_BZip2; break;
case NCompressionMethod::kZstdWz: ver = NCompressionMethod::kExtractVersion_Zstd; break;
case NCompressionMethod::kPKImploding: ver = NCompressionMethod::kExtractVersion_PKImploding; break;
case NCompressionMethod::kLZMA :
{
ver = NCompressionMethod::kExtractVersion_LZMA;
Expand Down Expand Up @@ -376,6 +380,12 @@ HRESULT CAddCommon::Compress(
_lzmaEncoder = new CLzmaEncoder();
_compressEncoder = _lzmaEncoder;
}
else if (method == NCompressionMethod::kZstdWz)
{
_compressExtractVersion = NCompressionMethod::kExtractVersion_Zstd;
NCompress::NZSTD::CEncoder *encoder = new NCompress::NZSTD::CEncoder();
_compressEncoder = encoder;
}
else if (method == NCompressionMethod::kXz)
{
_compressExtractVersion = NCompressionMethod::kExtractVersion_Xz;
Expand All @@ -388,6 +398,12 @@ HRESULT CAddCommon::Compress(
NCompress::NPpmdZip::CEncoder *encoder = new NCompress::NPpmdZip::CEncoder();
_compressEncoder = encoder;
}
else if (method == NCompressionMethod::kPKImploding)
{
_compressExtractVersion = NCompressionMethod::kExtractVersion_PKImploding;
NCompress::NPKImplode::NEncoder::CEncoder *encoder = new NCompress::NPKImplode::NEncoder::CEncoder();
_compressEncoder = encoder;
}
else
{
CMethodId methodId;
Expand Down
8 changes: 8 additions & 0 deletions CPP/7zip/Archive/Zip/ZipHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "../../Compress/PpmdZip.h"
#include "../../Compress/ShrinkDecoder.h"
#include "../../Compress/XzDecoder.h"
#include "../../Compress/ZstdDecoder.h"
#include "../../Compress/PKImplodeDecoder.h"

#include "../../Crypto/WzAes.h"
#include "../../Crypto/ZipCrypto.h"
Expand Down Expand Up @@ -1174,8 +1176,14 @@ HRESULT CZipDecoder::Decode(
lzmaDecoderSpec = new CLzmaDecoder;
mi.Coder = lzmaDecoderSpec;
}
else if (id ==NFileHeader::NCompressionMethod::kZstdPk)
mi.Coder = new NCompress::NZSTD::CDecoder();
else if (id ==NFileHeader::NCompressionMethod::kZstdWz)
mi.Coder = new NCompress::NZSTD::CDecoder();
else if (id == NFileHeader::NCompressionMethod::kXz)
mi.Coder = new NCompress::NXz::CComDecoder;
else if (id == NFileHeader::NCompressionMethod::kPKImploding)
mi.Coder = new NCompress::NPKImplode::NDecoder::CDecoder;
else if (id == NFileHeader::NCompressionMethod::kPPMd)
mi.Coder = new NCompress::NPpmdZip::CDecoder(true);
#ifdef SUPPORT_LZFSE
Expand Down
3 changes: 3 additions & 0 deletions CPP/7zip/Archive/Zip/ZipHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ namespace NFileHeader
const Byte kExtractVersion_LZMA = 63;
const Byte kExtractVersion_PPMd = 63;
const Byte kExtractVersion_Xz = 20; // test it

const Byte kExtractVersion_Zstd = 20; // WinZip mark it
const Byte kExtractVersion_PKImploding = 25;
}

namespace NExtraID
Expand Down
8 changes: 7 additions & 1 deletion CPP/7zip/Bundles/Format7zF/Arc_gcc.mak
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ ADDITIONAL_CODECS_OBJS = \
$O/lz5.o \
$O/lz5hc.o \
$O/lz5frame.o \
$O/explode.o \
$O/implode.o \

COMMON_OBJS = \
$O/CRC.o \
Expand Down Expand Up @@ -295,6 +297,10 @@ COMPRESS_OBJS = \
$O/Lz5Decoder.o \
$O/Lz5Encoder.o \
$O/Lz5Register.o \
$O/LzhamRegister.o \
$O/PKImplodeDecoder.o \
$O/PKImplodeEncoder.o \
$O/PKImplodeRegister.o \

ifdef DISABLE_RAR
DISABLE_RAR_COMPRESS=1
Expand Down Expand Up @@ -395,7 +401,7 @@ BROTLI_STATIC_LIB = $O/libbrotlienc-static.a \
$O/libbrotlicommon-static.a
LIZARD_STATIC_LIB = $O/liblizard.a
LZ5_STATIC_LIB = $O/liblz5.a
LZHAM_STATIC_LIB = $O/LzhamRegister.o $O/lzham_lib.o $O/liblzhamcomp.a $O/liblzhamdecomp.a
LZHAM_STATIC_LIB = $O/lzham_lib.o $O/liblzhamcomp.a $O/liblzhamdecomp.a

ARC_OBJS = \
$(LZMA_DEC_OPT_OBJS) \
Expand Down
69 changes: 69 additions & 0 deletions CPP/7zip/Compress/PKImplodeDecoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// ImplodeDecoder.cpp

#include <algorithm>
#include "StdAfx.h"

#include "../../Common/Defs.h"

#include "PKImplodeDecoder.h"
#include "../Common/StreamUtils.h"

namespace NCompress {
namespace NPKImplode {
namespace NDecoder {

static void C_put(char *buf, unsigned int *size, void *param){
CDecoder *w = (CDecoder*)param;
UInt32 rlen=0;
/*HRESULT res =*/ w->outS->Write(buf, *size, &rlen);
//*size = rlen;
w->processedOut += rlen;
if(w->progr)
w->progr->SetRatioInfo(&w->processedIn, &w->processedOut);
//return res != S_OK || rlen != len;
}

static unsigned int C_get(char *buf, unsigned int *size, void *param){
CDecoder *r = (CDecoder*)param;
size_t readsize = *size;
HRESULT res = ReadStream(r->inS,buf,&readsize);
//*size = readsize;
r->processedIn += readsize;
return res != S_OK ? 0 : readsize;
}

CDecoder::CDecoder(){
memset(&DcmpStruct, 0, sizeof(TDcmpStruct));
}

HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64* /* inSize */, const UInt64* /* outSize */, ICompressProgressInfo *progress)
{
inS = inStream;
outS = outStream;
progr = progress;
processedIn = processedOut = 0;
int x = explode(C_get, C_put, (char*)&DcmpStruct, this);
if(x<0)return E_FAIL;
if(x==2)return E_ABORT;
if(x==1)return E_OUTOFMEMORY;
return x==0 ? S_OK : E_FAIL;
}


STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress)
{
try { return CodeReal(inStream, outStream, inSize, outSize, progress); }
catch(const CInBufferException &e) { return e.ErrorCode; }
catch(const CSystemException &e) { return e.ErrorCode; }
catch(...) { return S_FALSE; }
}

STDMETHODIMP CDecoder::GetInStreamProcessedSize(UInt64 *value)
{
*value = processedIn;
return S_OK;
}

}}}
51 changes: 51 additions & 0 deletions CPP/7zip/Compress/PKImplodeDecoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// ImplodeDecoder.h

#ifndef __COMPRESS_PKIMPLODE_DECODER_H
#define __COMPRESS_PKIMPLODE_DECODER_H

#include "../../Common/MyCom.h"

#include "../ICoder.h"

#include "../Common/InBuffer.h"

extern "C" {
#include "../../../Codecs/StormLib/src/pklib/pklib.h"
}

namespace NCompress {
namespace NPKImplode {
namespace NDecoder {

static const unsigned int kS = 65536;

class CDecoder:
public ICompressCoder,
public ICompressGetInStreamProcessedSize,
public CMyUnknownImp
{
HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);

public:
unsigned char buf[kS];
UInt64 processedIn;
UInt64 processedOut;
ISequentialInStream *inS;
ISequentialOutStream *outS;
ICompressProgressInfo *progr;

TDcmpStruct DcmpStruct;

MY_UNKNOWN_IMP1(ICompressGetInStreamProcessedSize)

STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
STDMETHOD(GetInStreamProcessedSize)(UInt64 *value);

CDecoder();
};

}}}

#endif
94 changes: 94 additions & 0 deletions CPP/7zip/Compress/PKImplodeEncoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// ImplodeDecoder.cpp

#include <algorithm>
#include "StdAfx.h"

#include "../../Common/Defs.h"

#include "PKImplodeEncoder.h"
#include "../Common/StreamUtils.h"

namespace NCompress {
namespace NPKImplode {
namespace NEncoder {

static void C_put(char *buf, unsigned int *size, void *param){
CEncoder *w = (CEncoder*)param;
UInt32 rlen=0;
/*HRESULT res =*/ w->outS->Write(buf, *size, &rlen);
//*size = rlen;
w->processedOut += rlen;
if(w->progr)
w->progr->SetRatioInfo(&w->processedIn, &w->processedOut);
//return res != S_OK || rlen != len;
}

static unsigned int C_get(char *buf, unsigned int *size, void *param){
CEncoder *r = (CEncoder*)param;
size_t readsize = *size;
HRESULT res = ReadStream(r->inS,buf,&readsize);
//*size = readsize;
r->processedIn += readsize;
return res != S_OK ? 0 : readsize;
}

CEncoder::CEncoder(): typ(0), dsize(4096){
memset(&CmpStruct, 0, sizeof(TCmpStruct));
}

STDMETHODIMP CEncoder::SetCoderProperties(const PROPID * propIDs, const PROPVARIANT * coderProps, UInt32 numProps)
{
typ = 0;
dsize = 4096;
for (UInt32 i = 0; i < numProps; i++){
const PROPVARIANT & prop = coderProps[i];
PROPID propID = propIDs[i];
UInt32 v = (UInt32)prop.ulVal;
switch (propID)
{
case NCoderPropID::kLevel:
{
typ = v/10;
int dsize_ = v%10;
//if(typ<0)typ=0;
if(typ>1)typ=1;
if(dsize<1)dsize=1;
if(dsize>3)dsize=3;
dsize = 1<<(9+dsize_);
}
}
}
return S_OK;
}

HRESULT CEncoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64* /* inSize */, const UInt64* /* outSize */, ICompressProgressInfo *progress)
{
inS = inStream;
outS = outStream;
progr = progress;
processedIn = processedOut = 0;
int x = implode(C_get, C_put, (char*)&CmpStruct, this, &typ, &dsize);
if(x<0)return E_FAIL;
if(x==2)return E_ABORT;
if(x==1)return E_OUTOFMEMORY;
return x==0 ? S_OK : E_FAIL;
}


STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress)
{
try { return CodeReal(inStream, outStream, inSize, outSize, progress); }
catch(const CInBufferException &e) { return e.ErrorCode; }
catch(const CSystemException &e) { return e.ErrorCode; }
catch(...) { return S_FALSE; }
}

STDMETHODIMP CEncoder::GetInStreamProcessedSize(UInt64 *value)
{
*value = processedIn;
return S_OK;
}

}}}
Loading

0 comments on commit 081bc28

Please sign in to comment.