Update to 7-Zip Version 18.05

This commit is contained in:
Tino Reichardt
2018-10-21 14:23:28 +02:00
parent 51dc99984a
commit 78fc3c9bc5
208 changed files with 13958 additions and 3588 deletions

View File

@@ -13,7 +13,9 @@ struct CMethodFull: public CMethodProps
{
CMethodId Id;
UInt32 NumStreams;
int CodecIndex;
CMethodFull(): CodecIndex(-1) {}
bool IsSimpleCoder() const { return NumStreams == 1; }
};

View File

@@ -236,8 +236,8 @@ HRESULT CDecoder::Decode(
_7Z_DECODER_CRYPRO_VARS_DECL
#if !defined(_7ZIP_ST) && !defined(_SFX)
, bool mtMode, UInt32 numThreads
#if !defined(_7ZIP_ST)
, bool mtMode, UInt32 numThreads, UInt64 memUsage
#endif
)
{
@@ -312,7 +312,7 @@ HRESULT CDecoder::Decode(
#endif
CCreatedCoder cod;
RINOK(CreateCoder(
RINOK(CreateCoder_Id(
EXTERNAL_CODECS_LOC_VARS
coderInfo.MethodID, false, cod));
@@ -355,11 +355,39 @@ HRESULT CDecoder::Decode(
unsigned i;
bool mt_wasUsed = false;
for (i = 0; i < folderInfo.Coders.Size(); i++)
{
const CCoderInfo &coderInfo = folderInfo.Coders[i];
IUnknown *decoder = _mixer->GetCoder(i).GetUnknown();
#if !defined(_7ZIP_ST)
if (!mt_wasUsed)
{
if (mtMode)
{
CMyComPtr<ICompressSetCoderMt> setCoderMt;
decoder->QueryInterface(IID_ICompressSetCoderMt, (void **)&setCoderMt);
if (setCoderMt)
{
mt_wasUsed = true;
RINOK(setCoderMt->SetNumberOfThreads(numThreads));
}
}
// if (memUsage != 0)
{
CMyComPtr<ICompressSetMemLimit> setMemLimit;
decoder->QueryInterface(IID_ICompressSetMemLimit, (void **)&setMemLimit);
if (setMemLimit)
{
mt_wasUsed = true;
RINOK(setMemLimit->SetMemLimit(memUsage));
}
}
}
#endif
{
CMyComPtr<ICompressSetDecoderProperties2> setDecoderProperties;
decoder->QueryInterface(IID_ICompressSetDecoderProperties2, (void **)&setDecoderProperties);
@@ -376,18 +404,6 @@ HRESULT CDecoder::Decode(
}
}
#if !defined(_7ZIP_ST) && !defined(_SFX)
if (mtMode)
{
CMyComPtr<ICompressSetCoderMt> setCoderMt;
decoder->QueryInterface(IID_ICompressSetCoderMt, (void **)&setCoderMt);
if (setCoderMt)
{
RINOK(setCoderMt->SetNumberOfThreads(numThreads));
}
}
#endif
#ifndef _NO_CRYPTO
{
CMyComPtr<ICryptoSetPassword> cryptoSetPassword;

View File

@@ -59,8 +59,8 @@ public:
_7Z_DECODER_CRYPRO_VARS_DECL
#if !defined(_7ZIP_ST) && !defined(_SFX)
, bool mtMode, UInt32 numThreads
#if !defined(_7ZIP_ST)
, bool mtMode, UInt32 numThreads, UInt64 memUsage
#endif
);
};

View File

@@ -154,9 +154,18 @@ HRESULT CEncoder::CreateMixerCoder(
CCreatedCoder cod;
RINOK(CreateCoder(
if (methodFull.CodecIndex >= 0)
{
RINOK(CreateCoder_Index(
EXTERNAL_CODECS_LOC_VARS
methodFull.CodecIndex, true, cod));
}
else
{
RINOK(CreateCoder_Id(
EXTERNAL_CODECS_LOC_VARS
methodFull.Id, true, cod));
}
if (cod.NumStreams != methodFull.NumStreams)
return E_FAIL;

View File

@@ -152,6 +152,12 @@ STDMETHODIMP CFolderOutStream::Write(const void *data, UInt32 size, UInt32 *proc
if (_fileIsOpen)
{
UInt32 cur = (size < _rem ? size : (UInt32)_rem);
if (_calcCrc)
{
const UInt32 k_Step = (UInt32)1 << 20;
if (cur > k_Step)
cur = k_Step;
}
HRESULT result = S_OK;
if (_stream)
result = _stream->Write(data, cur, &cur);
@@ -363,8 +369,8 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
, dataAfterEnd_Error
_7Z_DECODER_CRYPRO_VARS
#if !defined(_7ZIP_ST) && !defined(_SFX)
, true, _numThreads
#if !defined(_7ZIP_ST)
, true, _numThreads, _memUsage
#endif
);

View File

@@ -40,7 +40,6 @@ CHandler::CHandler()
_crcSize = 4;
#ifdef __7Z_SET_PROPERTIES
_numThreads = NSystem::GetNumberOfProcessors();
_useMultiThreadMixer = true;
#endif
@@ -823,8 +822,8 @@ STDMETHODIMP CHandler::Close()
STDMETHODIMP CHandler::SetProperties(const wchar_t * const *names, const PROPVARIANT *values, UInt32 numProps)
{
COM_TRY_BEGIN
const UInt32 numProcessors = NSystem::GetNumberOfProcessors();
_numThreads = numProcessors;
InitCommon();
_useMultiThreadMixer = true;
for (UInt32 i = 0; i < numProps; i++)
@@ -843,13 +842,15 @@ STDMETHODIMP CHandler::SetProperties(const wchar_t * const *names, const PROPVAR
RINOK(PROPVARIANT_to_bool(value, _useMultiThreadMixer));
continue;
}
if (name.IsPrefixedBy_Ascii_NoCase("mt"))
{
RINOK(ParseMtProp(name.Ptr(2), value, numProcessors, _numThreads));
continue;
HRESULT hres;
if (SetCommonProperty(name, value, hres))
{
RINOK(hres);
continue;
}
}
else
return E_INVALIDARG;
return E_INVALIDARG;
}
}
return S_OK;

View File

@@ -8,16 +8,6 @@
#include "../../Common/CreateCoder.h"
#ifndef EXTRACT_ONLY
#include "../Common/HandlerOut.h"
#endif
#include "7zCompressionMode.h"
#include "7zIn.h"
namespace NArchive {
namespace N7z {
#ifndef __7Z_SET_PROPERTIES
#ifdef EXTRACT_ONLY
@@ -30,6 +20,16 @@ namespace N7z {
#endif
// #ifdef __7Z_SET_PROPERTIES
#include "../Common/HandlerOut.h"
// #endif
#include "7zCompressionMode.h"
#include "7zIn.h"
namespace NArchive {
namespace N7z {
#ifndef EXTRACT_ONLY
@@ -38,8 +38,6 @@ class COutHandler: public CMultiMethodProps
HRESULT SetSolidFromString(const UString &s);
HRESULT SetSolidFromPROPVARIANT(const PROPVARIANT &value);
public:
bool _removeSfxBlock;
UInt64 _numSolidFiles;
UInt64 _numSolidBytes;
bool _numSolidBytesDefined;
@@ -58,6 +56,8 @@ public:
bool _useMultiThreadMixer;
bool _removeSfxBlock;
// bool _volumeMode;
void InitSolidFiles() { _numSolidFiles = (UInt64)(Int64)(-1); }
@@ -70,9 +70,10 @@ public:
_numSolidBytesDefined = false;
}
void InitProps7z();
void InitProps();
COutHandler() { InitProps(); }
COutHandler() { InitProps7z(); }
HRESULT SetProperty(const wchar_t *name, const PROPVARIANT &value);
};
@@ -82,16 +83,23 @@ public:
class CHandler:
public IInArchive,
public IArchiveGetRawProps,
#ifdef __7Z_SET_PROPERTIES
public ISetProperties,
#endif
#ifndef EXTRACT_ONLY
public IOutArchive,
#endif
PUBLIC_ISetCompressCodecsInfo
public CMyUnknownImp
public CMyUnknownImp,
#ifndef EXTRACT_ONLY
, public COutHandler
public COutHandler
#else
public CCommonMethodProps
#endif
{
public:
@@ -135,7 +143,6 @@ private:
#ifdef EXTRACT_ONLY
#ifdef __7Z_SET_PROPERTIES
UInt32 _numThreads;
bool _useMultiThreadMixer;
#endif

View File

@@ -13,6 +13,8 @@
#include "7zOut.h"
#include "7zUpdate.h"
#ifndef EXTRACT_ONLY
using namespace NWindows;
namespace NArchive {
@@ -41,9 +43,11 @@ STDMETHODIMP CHandler::GetFileTimeType(UInt32 *type)
HRESULT CHandler::PropsMethod_To_FullMethod(CMethodFull &dest, const COneMethodInfo &m)
{
if (!FindMethod(
dest.CodecIndex = FindMethod_Index(
EXTERNAL_CODECS_VARS
m.MethodName, dest.Id, dest.NumStreams))
m.MethodName, true,
dest.Id, dest.NumStreams);
if (dest.CodecIndex < 0)
return E_INVALIDARG;
(CProps &)dest = (CProps &)m;
return S_OK;
@@ -700,10 +704,8 @@ static HRESULT ParseBond(UString &srcString, UInt32 &coder, UInt32 &stream)
return S_OK;
}
void COutHandler::InitProps()
void COutHandler::InitProps7z()
{
CMultiMethodProps::Init();
_removeSfxBlock = false;
_compressHeaders = true;
_encryptHeadersSpecified = false;
@@ -723,6 +725,14 @@ void COutHandler::InitProps()
_useTypeSorting = false;
}
void COutHandler::InitProps()
{
CMultiMethodProps::Init();
InitProps7z();
}
HRESULT COutHandler::SetSolidFromString(const UString &s)
{
UString s2 = s;
@@ -763,6 +773,10 @@ HRESULT COutHandler::SetSolidFromString(const UString &s)
}
_numSolidBytes = (v << numBits);
_numSolidBytesDefined = true;
/*
if (_numSolidBytes == 0)
_numSolidFiles = 1;
*/
}
}
return S_OK;
@@ -811,7 +825,7 @@ HRESULT COutHandler::SetProperty(const wchar_t *nameSpec, const PROPVARIANT &val
return E_INVALIDARG;
return SetSolidFromString(name);
}
UInt32 number;
int index = ParseStringToUInt32(name, number);
// UString realName = name.Ptr(index);
@@ -922,3 +936,5 @@ STDMETHODIMP CHandler::SetProperties(const wchar_t * const *names, const PROPVAR
}
}}
#endif

View File

@@ -1115,11 +1115,11 @@ HRESULT CInArchive::ReadAndDecodePackedStreams(
, dataAfterEnd_Error
_7Z_DECODER_CRYPRO_VARS
#if !defined(_7ZIP_ST) && !defined(_SFX)
#if !defined(_7ZIP_ST)
, false // mtMode
, 1 // numThreads
, 0 // memUsage
#endif
);
RINOK(result);

View File

@@ -1507,7 +1507,8 @@ void CThreadDecoder::Execute()
_7Z_DECODER_CRYPRO_VARS
#ifndef _7ZIP_ST
, MtMode, NumThreads
, MtMode, NumThreads,
0 // MemUsage
#endif
);
@@ -1696,13 +1697,14 @@ HRESULT Update(
UInt64 inSizeForReduce = 0;
{
bool isSolid = (numSolidFiles > 1 && options.NumSolidBytes != 0);
FOR_VECTOR (i, updateItems)
{
const CUpdateItem &ui = updateItems[i];
if (ui.NewData)
{
complexity += ui.Size;
if (numSolidFiles != 1)
if (isSolid)
inSizeForReduce += ui.Size;
else if (inSizeForReduce < ui.Size)
inSizeForReduce = ui.Size;
@@ -2142,8 +2144,8 @@ HRESULT Update(
#ifndef _7ZIP_ST
, false // mtMode
, 1 // numThreads
, 0 // memUsage
#endif
);
RINOK(res);
@@ -2293,7 +2295,8 @@ HRESULT Update(
continue;
CRecordVector<CRefItem> refItems;
refItems.ClearAndSetSize(numFiles);
bool sortByType = (options.UseTypeSorting && numSolidFiles > 1);
// bool sortByType = (options.UseTypeSorting && isSoid); // numSolidFiles > 1
bool sortByType = options.UseTypeSorting;
unsigned i;

View File

@@ -549,9 +549,9 @@ private:
Byte *TempBuf;
UInt32 TempBufSize;
UInt32 TempBufWritten;
unsigned NumIdenticalFiles;
bool TempBufMode;
UInt32 m_BufStartFolderOffset;
unsigned m_StartIndex;
unsigned m_CurrentIndex;
@@ -575,7 +575,6 @@ private:
HRESULT OpenFile();
HRESULT CloseFileWithResOp(Int32 resOp);
HRESULT CloseFile();
HRESULT Write2(const void *data, UInt32 size, UInt32 *processedSize, bool isOK);
public:
HRESULT WriteEmptyFiles();
@@ -672,11 +671,11 @@ HRESULT CFolderOutStream::OpenFile()
FreeTempBuf();
TempBuf = (Byte *)MyAlloc(item.Size);
TempBufSize = item.Size;
if (TempBuf == NULL)
if (!TempBuf)
return E_OUTOFMEMORY;
}
TempBufMode = true;
m_BufStartFolderOffset = item.Offset;
TempBufWritten = 0;
}
else if (numExtractItems == 1)
{
@@ -725,8 +724,9 @@ HRESULT CFolderOutStream::WriteEmptyFiles()
}
HRESULT CFolderOutStream::Write2(const void *data, UInt32 size, UInt32 *processedSize, bool isOK)
HRESULT CFolderOutStream::Write(const void *data, UInt32 size, UInt32 *processedSize)
{
// (data == NULL) means Error_Data for solid folder flushing
COM_TRY_BEGIN
UInt32 realProcessed = 0;
@@ -741,21 +741,34 @@ HRESULT CFolderOutStream::Write2(const void *data, UInt32 size, UInt32 *processe
HRESULT res = S_OK;
if (numBytesToWrite != 0)
{
if (!isOK)
if (!data)
m_IsOk = false;
if (m_RealOutStream)
{
UInt32 processedSizeLocal = 0;
res = m_RealOutStream->Write((const Byte *)data, numBytesToWrite, &processedSizeLocal);
// 18.01 : we don't want ZEROs instead of missing data
if (data)
res = m_RealOutStream->Write((const Byte *)data, numBytesToWrite, &processedSizeLocal);
else
processedSizeLocal = numBytesToWrite;
numBytesToWrite = processedSizeLocal;
}
if (TempBufMode && TempBuf)
memcpy(TempBuf + (m_PosInFolder - m_BufStartFolderOffset), data, numBytesToWrite);
{
if (data)
{
memcpy(TempBuf + TempBufWritten, data, numBytesToWrite);
TempBufWritten += numBytesToWrite;
}
}
}
realProcessed += numBytesToWrite;
if (processedSize)
*processedSize = realProcessed;
data = (const void *)((const Byte *)data + numBytesToWrite);
if (data)
data = (const void *)((const Byte *)data + numBytesToWrite);
size -= numBytesToWrite;
m_RemainFileSize -= numBytesToWrite;
m_PosInFolder += numBytesToWrite;
@@ -773,7 +786,7 @@ HRESULT CFolderOutStream::Write2(const void *data, UInt32 size, UInt32 *processe
m_FileIsOpen = true;
m_CurrentIndex++;
if (result == S_OK && m_RealOutStream && TempBuf)
result = WriteStream(m_RealOutStream, TempBuf, (size_t)(m_PosInFolder - m_BufStartFolderOffset));
result = WriteStream(m_RealOutStream, TempBuf, TempBufWritten);
if (!TempBuf && TempBufMode && m_RealOutStream)
{
@@ -822,7 +835,8 @@ HRESULT CFolderOutStream::Write2(const void *data, UInt32 size, UInt32 *processe
realProcessed += numBytesToWrite;
if (processedSize)
*processedSize = realProcessed;
data = (const void *)((const Byte *)data + numBytesToWrite);
if (data)
data = (const void *)((const Byte *)data + numBytesToWrite);
size -= numBytesToWrite;
m_PosInFolder += numBytesToWrite;
}
@@ -843,12 +857,6 @@ HRESULT CFolderOutStream::Write2(const void *data, UInt32 size, UInt32 *processe
}
STDMETHODIMP CFolderOutStream::Write(const void *data, UInt32 size, UInt32 *processedSize)
{
return Write2(data, size, processedSize, true);
}
HRESULT CFolderOutStream::FlushCorrupted(unsigned folderIndex)
{
if (!NeedMoreWrite())
@@ -862,19 +870,16 @@ HRESULT CFolderOutStream::FlushCorrupted(unsigned folderIndex)
return S_OK;
}
const unsigned kBufSize = (1 << 12);
Byte buf[kBufSize];
for (unsigned i = 0; i < kBufSize; i++)
buf[i] = 0;
for (;;)
{
if (!NeedMoreWrite())
return S_OK;
UInt64 remain = GetRemain();
UInt32 size = (remain < kBufSize ? (UInt32)remain : (UInt32)kBufSize);
UInt32 size = (UInt32)1 << 20;
if (size > remain)
size = (UInt32)remain;
UInt32 processedSizeLocal = 0;
RINOK(Write2(buf, size, &processedSizeLocal, false));
RINOK(Write(NULL, size, &processedSizeLocal));
}
}

View File

@@ -2,18 +2,92 @@
#include "StdAfx.h"
#ifndef _7ZIP_ST
#include "../../../Windows/System.h"
#endif
#include "../../../Common/StringToInt.h"
#include "../Common/ParseProperties.h"
#include "HandlerOut.h"
using namespace NWindows;
namespace NArchive {
bool ParseSizeString(const wchar_t *s, const PROPVARIANT &prop, UInt64 percentsBase, UInt64 &res)
{
if (*s == 0)
{
switch (prop.vt)
{
case VT_UI4: res = prop.ulVal; return true;
case VT_UI8: res = prop.uhVal.QuadPart; return true;
case VT_BSTR:
s = prop.bstrVal;
break;
default: return false;
}
}
else if (prop.vt != VT_EMPTY)
return false;
const wchar_t *end;
UInt64 v = ConvertStringToUInt64(s, &end);
if (s == end)
return false;
wchar_t c = *end;
if (c == 0)
{
res = v;
return true;
}
if (end[1] != 0)
return false;
if (c == '%')
{
res = percentsBase / 100 * v;
return true;
}
unsigned numBits;
switch (MyCharLower_Ascii(c))
{
case 'b': numBits = 0; break;
case 'k': numBits = 10; break;
case 'm': numBits = 20; break;
case 'g': numBits = 30; break;
case 't': numBits = 40; break;
default: return false;
}
UInt64 val2 = v << numBits;
if ((val2 >> numBits) != v)
return false;
res = val2;
return true;
}
bool CCommonMethodProps::SetCommonProperty(const UString &name, const PROPVARIANT &value, HRESULT &hres)
{
hres = S_OK;
if (name.IsPrefixedBy_Ascii_NoCase("mt"))
{
#ifndef _7ZIP_ST
hres = ParseMtProp(name.Ptr(2), value, _numProcessors, _numThreads);
#endif
return true;
}
if (name.IsPrefixedBy_Ascii_NoCase("memuse"))
{
if (!ParseSizeString(name.Ptr(6), value, _memAvail, _memUsage))
hres = E_INVALIDARG;
return true;
}
return false;
}
#ifndef EXTRACT_ONLY
static void SetMethodProp32(COneMethodInfo &m, PROPID propID, UInt32 value)
{
if (m.FindProp(propID) < 0)
@@ -34,21 +108,23 @@ void CMultiMethodProps::SetMethodThreadsTo(COneMethodInfo &oneMethodInfo, UInt32
}
#endif
void CMultiMethodProps::Init()
void CMultiMethodProps::InitMulti()
{
#ifndef _7ZIP_ST
_numProcessors = _numThreads = NSystem::GetNumberOfProcessors();
#endif
_level = (UInt32)(Int32)-1;
_analysisLevel = -1;
_autoFilter = true;
_crcSize = 4;
_filterMethod.Clear();
_methods.Clear();
_autoFilter = true;
}
void CMultiMethodProps::Init()
{
InitCommon();
InitMulti();
_methods.Clear();
_filterMethod.Clear();
}
HRESULT CMultiMethodProps::SetProperty(const wchar_t *nameSpec, const PROPVARIANT &value)
{
UString name = nameSpec;
@@ -78,20 +154,18 @@ HRESULT CMultiMethodProps::SetProperty(const wchar_t *nameSpec, const PROPVARIAN
_crcSize = 4;
return ParsePropToUInt32(name, value, _crcSize);
}
{
HRESULT hres;
if (SetCommonProperty(name, value, hres))
return hres;
}
UInt32 number;
unsigned index = ParseStringToUInt32(name, number);
UString realName = name.Ptr(index);
if (index == 0)
{
if (name.IsPrefixedBy_Ascii_NoCase("mt"))
{
#ifndef _7ZIP_ST
RINOK(ParseMtProp(name.Ptr(2), value, _numProcessors, _numThreads));
#endif
return S_OK;
}
if (name.IsEqualTo("f"))
{
HRESULT res = PROPVARIANT_to_bool(value, _autoFilter);
@@ -110,20 +184,20 @@ HRESULT CMultiMethodProps::SetProperty(const wchar_t *nameSpec, const PROPVARIAN
return _methods[number].ParseMethodFromPROPVARIANT(realName, value);
}
void CSingleMethodProps::Init()
{
InitCommon();
InitSingle();
Clear();
_level = (UInt32)(Int32)-1;
#ifndef _7ZIP_ST
_numProcessors = _numThreads = NWindows::NSystem::GetNumberOfProcessors();
AddProp_NumThreads(_numThreads);
#endif
}
HRESULT CSingleMethodProps::SetProperties(const wchar_t * const *names, const PROPVARIANT *values, UInt32 numProps)
{
Init();
for (UInt32 i = 0; i < numProps; i++)
{
UString name = names[i];
@@ -137,20 +211,22 @@ HRESULT CSingleMethodProps::SetProperties(const wchar_t * const *names, const PR
RINOK(ParsePropToUInt32(name.Ptr(1), value, a));
_level = a;
AddProp_Level(a);
continue;
}
else if (name.IsPrefixedBy_Ascii_NoCase("mt"))
{
#ifndef _7ZIP_ST
RINOK(ParseMtProp(name.Ptr(2), value, _numProcessors, _numThreads));
AddProp_NumThreads(_numThreads);
#endif
}
else
{
RINOK(ParseMethodFromPROPVARIANT(names[i], value));
HRESULT hres;
if (SetCommonProperty(name, value, hres))
{
RINOK(hres)
continue;
}
}
RINOK(ParseMethodFromPROPVARIANT(names[i], value));
}
return S_OK;
}
#endif
}

View File

@@ -3,20 +3,57 @@
#ifndef __HANDLER_OUT_H
#define __HANDLER_OUT_H
#include "../../../Windows/System.h"
#include "../../Common/MethodProps.h"
namespace NArchive {
class CMultiMethodProps
bool ParseSizeString(const wchar_t *name, const PROPVARIANT &prop, UInt64 percentsBase, UInt64 &res);
class CCommonMethodProps
{
UInt32 _level;
int _analysisLevel;
protected:
void InitCommon()
{
#ifndef _7ZIP_ST
_numProcessors = _numThreads = NWindows::NSystem::GetNumberOfProcessors();
#endif
UInt64 memAvail = (UInt64)(sizeof(size_t)) << 28;
_memAvail = memAvail;
_memUsage = memAvail;
if (NWindows::NSystem::GetRamSize(memAvail))
{
_memAvail = memAvail;
_memUsage = memAvail / 32 * 17;
}
}
public:
#ifndef _7ZIP_ST
UInt32 _numThreads;
UInt32 _numProcessors;
#endif
UInt64 _memUsage;
UInt64 _memAvail;
bool SetCommonProperty(const UString &name, const PROPVARIANT &value, HRESULT &hres);
CCommonMethodProps() { InitCommon(); }
};
#ifndef EXTRACT_ONLY
class CMultiMethodProps: public CCommonMethodProps
{
UInt32 _level;
int _analysisLevel;
void InitMulti();
public:
UInt32 _crcSize;
CObjectVector<COneMethodInfo> _methods;
COneMethodInfo _filterMethod;
@@ -43,27 +80,31 @@ public:
int GetAnalysisLevel() const { return _analysisLevel; }
void Init();
CMultiMethodProps() { InitMulti(); }
CMultiMethodProps() { Init(); }
HRESULT SetProperty(const wchar_t *name, const PROPVARIANT &value);
};
class CSingleMethodProps: public COneMethodInfo
class CSingleMethodProps: public COneMethodInfo, public CCommonMethodProps
{
UInt32 _level;
public:
#ifndef _7ZIP_ST
UInt32 _numThreads;
UInt32 _numProcessors;
#endif
void InitSingle()
{
_level = (UInt32)(Int32)-1;
}
public:
void Init();
CSingleMethodProps() { Init(); }
CSingleMethodProps() { InitSingle(); }
int GetLevel() const { return _level == (UInt32)(Int32)-1 ? 5 : (int)_level; }
HRESULT SetProperties(const wchar_t * const *names, const PROPVARIANT *values, UInt32 numProps);
};
#endif
}
#endif

View File

@@ -19,6 +19,7 @@
#include "../Compress/BZip2Decoder.h"
#include "../Compress/CopyCoder.h"
#include "../Compress/LzfseDecoder.h"
#include "../Compress/ZlibDecoder.h"
#include "Common/OutStreamWithCRC.h"
@@ -121,6 +122,7 @@ enum
METHOD_ADC = 0x80000004,
METHOD_ZLIB = 0x80000005,
METHOD_BZIP2 = 0x80000006,
METHOD_LZFSE = 0x80000007,
METHOD_COMMENT = 0x7FFFFFFE, // is used to comment "+beg" and "+end" in extra field.
METHOD_END = 0xFFFFFFFF
};
@@ -217,6 +219,7 @@ class CHandler:
bool _masterCrcError;
bool _headersError;
UInt32 _dataStartOffset;
UInt64 _startPos;
UInt64 _phySize;
@@ -276,6 +279,7 @@ void CMethods::GetString(AString &res) const
case METHOD_ADC: s = "ADC"; break;
case METHOD_ZLIB: s = "ZLIB"; break;
case METHOD_BZIP2: s = "BZip2"; break;
case METHOD_LZFSE: s = "LZFSE"; break;
default: ConvertUInt32ToString(type, buf); s = buf;
}
res.Add_OptSpaced(s);
@@ -307,6 +311,10 @@ static const CAppleName k_Names[] =
{ true, "hfs", "Apple_HFS" },
{ true, "hfsx", "Apple_HFSX" },
{ true, "ufs", "Apple_UFS" },
// efi_sys partition is FAT32, but it's not main file. So we use (IsFs = false)
{ false, "efi_sys", "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" },
{ false, "free", "Apple_Free" },
{ false, "ddm", "DDM" },
{ false, NULL, "Apple_partition_map" },
@@ -326,6 +334,7 @@ static const Byte kProps[] =
kpidCRC,
kpidComment,
kpidMethod
// kpidOffset
};
IMP_IInArchive_Props
@@ -624,17 +633,40 @@ bool CHandler::ParseBlob(const CByteBuffer &data)
HRESULT CHandler::Open2(IInStream *stream)
{
/*
- usual dmg contains Koly Header at the end:
- rare case dmg contains Koly Header at the start.
*/
_dataStartOffset = 0;
RINOK(stream->Seek(0, STREAM_SEEK_CUR, &_startPos));
UInt64 fileSize = 0;
RINOK(stream->Seek(0, STREAM_SEEK_END, &fileSize));
RINOK(stream->Seek(_startPos, STREAM_SEEK_SET, NULL));
Byte buf[HEADER_SIZE];
RINOK(ReadStream_FALSE(stream, buf, HEADER_SIZE));
UInt64 headerPos;
bool startKolyMode = false;
if (IsKoly(buf))
{
// it can be normal koly-at-the-end or koly-at-the-start
headerPos = _startPos;
if (_startPos <= (1 << 8))
{
// we want to support startKolyMode, even if there is
// some data before dmg file, like 128 bytes MacBin header
_dataStartOffset = HEADER_SIZE;
startKolyMode = true;
}
}
else
{
RINOK(stream->Seek(0, STREAM_SEEK_END, &headerPos));
// we check only koly-at-the-end
headerPos = fileSize;
if (headerPos < HEADER_SIZE)
return S_FALSE;
headerPos -= HEADER_SIZE;
@@ -660,24 +692,35 @@ HRESULT CHandler::Open2(IInStream *stream)
// CChecksum dataForkChecksum;
// dataForkChecksum.Parse(buf + 0x50);
_startPos = 0;
UInt64 top = 0;
if (!dataForkPair.UpdateTop(headerPos, top)) return S_FALSE;
if (!xmlPair.UpdateTop(headerPos, top)) return S_FALSE;
if (!rsrcPair.UpdateTop(headerPos, top)) return S_FALSE;
UInt64 limit = startKolyMode ? fileSize : headerPos;
if (!dataForkPair.UpdateTop(limit, top)) return S_FALSE;
if (!xmlPair.UpdateTop(limit, top)) return S_FALSE;
if (!rsrcPair.UpdateTop(limit, top)) return S_FALSE;
/* Some old dmg files contain garbage data in blobPair field.
So we need to ignore such garbage case;
And we still need to detect offset of start of archive for "parser" mode. */
bool useBlob = blobPair.UpdateTop(headerPos, top);
bool useBlob = blobPair.UpdateTop(limit, top);
_startPos = 0;
_phySize = headerPos + HEADER_SIZE;
if (top != headerPos)
if (startKolyMode)
_phySize = top;
else
{
_phySize = headerPos + HEADER_SIZE;
_startPos = 0;
if (top != headerPos)
{
/*
if expected absolute offset is not equal to real header offset,
2 cases are possible:
- additional (unknown) headers
- archive with offset.
So we try to read XML with absolute offset to select from these two ways.
*/
CForkPair xmlPair2 = xmlPair;
const char *sz = "<?xml version";
const unsigned len = (unsigned)strlen(sz);
@@ -687,9 +730,11 @@ HRESULT CHandler::Open2(IInStream *stream)
if (ReadData(stream, xmlPair2, buf2) != S_OK
|| memcmp(buf2, sz, len) != 0)
{
// if absolute offset is not OK, probably it's archive with offset
_startPos = headerPos - top;
_phySize = top + HEADER_SIZE;
}
}
}
// Byte reserved[0x78]
@@ -1034,6 +1079,14 @@ STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *val
break;
}
/*
case kpidOffset:
{
prop = item.StartPos;
break;
}
*/
case kpidMethod:
{
CMethods m;
@@ -1247,6 +1300,11 @@ STDMETHODIMP CAdcDecoder::Code(ISequentialInStream *inStream,
}
STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
Int32 testMode, IArchiveExtractCallback *extractCallback)
{
@@ -1292,6 +1350,9 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
CAdcDecoder *adcCoderSpec = new CAdcDecoder();
CMyComPtr<ICompressCoder> adcCoder = adcCoderSpec;
NCompress::NLzfse::CDecoder *lzfseCoderSpec = new NCompress::NLzfse::CDecoder();
CMyComPtr<ICompressCoder> lzfseCoder = lzfseCoderSpec;
CLocalProgress *lps = new CLocalProgress;
CMyComPtr<ICompressProgressInfo> progress = lps;
lps->Init(extractCallback, false);
@@ -1369,7 +1430,7 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
break;
}
RINOK(_inStream->Seek(_startPos + item.StartPos + block.PackPos, STREAM_SEEK_SET, NULL));
RINOK(_inStream->Seek(_startPos + _dataStartOffset + item.StartPos + block.PackPos, STREAM_SEEK_SET, NULL));
streamSpec->Init(block.PackSize);
bool realMethod = true;
outStreamSpec->Init(block.UnpSize);
@@ -1419,6 +1480,12 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
opRes = NExtract::NOperationResult::kDataError;
break;
}
case METHOD_LZFSE:
{
res = lzfseCoder->Code(inStream, outStream, &block.PackSize, &block.UnpSize, progress);
break;
}
default:
opRes = NExtract::NOperationResult::kUnsupportedMethod;
@@ -1490,6 +1557,9 @@ class CInStream:
CAdcDecoder *adcCoderSpec;
CMyComPtr<ICompressCoder> adcCoder;
NCompress::NLzfse::CDecoder *lzfseCoderSpec;
CMyComPtr<ICompressCoder> lzfseCoder;
CBufPtrSeqOutStream *outStreamSpec;
CMyComPtr<ISequentialOutStream> outStream;
@@ -1651,6 +1721,15 @@ STDMETHODIMP CInStream::Read(void *data, UInt32 size, UInt32 *processedSize)
if (res == S_OK && bzip2CoderSpec->GetInputProcessedSize() != block.PackSize)
res = S_FALSE;
break;
case METHOD_LZFSE:
if (!lzfseCoder)
{
lzfseCoderSpec = new NCompress::NLzfse::CDecoder();
lzfseCoder = lzfseCoderSpec;
}
res = lzfseCoder->Code(inStream, outStream, &block.PackSize, &block.UnpSize, NULL);
break;
default:
return E_FAIL;
@@ -1738,6 +1817,7 @@ STDMETHODIMP CHandler::GetStream(UInt32 index, ISequentialInStream **stream)
case METHOD_ADC:
case METHOD_ZLIB:
case METHOD_BZIP2:
case METHOD_LZFSE:
case METHOD_END:
break;
default:
@@ -1747,7 +1827,7 @@ STDMETHODIMP CHandler::GetStream(UInt32 index, ISequentialInStream **stream)
spec->Stream = _inStream;
spec->Size = spec->File->Size;
RINOK(spec->InitAndSeek(_startPos));
RINOK(spec->InitAndSeek(_startPos + _dataStartOffset));
*stream = specStream.Detach();
return S_OK;

View File

@@ -567,6 +567,7 @@ static const char * const g_Machines[] =
static const CUInt32PCharPair g_MachinePairs[] =
{
{ 243, "RISC-V" },
{ 47787, "Xilinx MicroBlaze" }
// { 0x9026, "Alpha" }
};

View File

@@ -317,11 +317,15 @@ public:
// bool CaseSensetive;
UString ResFileName;
UInt64 SpecOffset;
UInt64 PhySize;
UInt64 PhySize2;
void Clear()
{
SpecOffset = 0;
PhySize = 0;
PhySize2 = 0;
HeadersError = false;
ThereAreAltStreams = false;
// CaseSensetive = false;
@@ -444,7 +448,7 @@ HRESULT CDatabase::ReadFile(const CFork &fork, CByteBuffer &buf, IInStream *inSt
e.NumBlocks > fork.NumBlocks - curBlock ||
e.NumBlocks > Header.NumBlocks - e.Pos)
return S_FALSE;
RINOK(inStream->Seek((UInt64)e.Pos << Header.BlockSizeLog, STREAM_SEEK_SET, NULL));
RINOK(inStream->Seek(SpecOffset + ((UInt64)e.Pos << Header.BlockSizeLog), STREAM_SEEK_SET, NULL));
RINOK(ReadStream_FALSE(inStream,
(Byte *)buf + ((size_t)curBlock << Header.BlockSizeLog),
(size_t)e.NumBlocks << Header.BlockSizeLog));
@@ -1154,13 +1158,36 @@ HRESULT CDatabase::LoadCatalog(const CFork &fork, const CObjectVector<CIdExtents
}
static const unsigned kHeaderPadSize = (1 << 10);
static const unsigned kMainHeaderSize = 512;
static const unsigned kHfsHeaderSize = kHeaderPadSize + kMainHeaderSize;
API_FUNC_static_IsArc IsArc_HFS(const Byte *p, size_t size)
{
if (size < kHfsHeaderSize)
return k_IsArc_Res_NEED_MORE;
p += kHeaderPadSize;
if (p[0] == 'B' && p[1] == 'D')
{
if (p[0x7C] != 'H' || p[0x7C + 1] != '+')
return k_IsArc_Res_NO;
}
else
{
if (p[0] != 'H' || (p[1] != '+' && p[1] != 'X'))
return k_IsArc_Res_NO;
UInt32 version = Get16(p + 2);
if (version < 4 || version > 5)
return k_IsArc_Res_NO;
}
return k_IsArc_Res_YES;
}
}
HRESULT CDatabase::Open2(IInStream *inStream, IArchiveOpenCallback *progress)
{
Clear();
static const unsigned kHeaderSize = kHeaderPadSize + 512;
Byte buf[kHeaderSize];
RINOK(ReadStream_FALSE(inStream, buf, kHeaderSize));
Byte buf[kHfsHeaderSize];
RINOK(ReadStream_FALSE(inStream, buf, kHfsHeaderSize));
{
for (unsigned i = 0; i < kHeaderPadSize; i++)
if (buf[i] != 0)
@@ -1171,6 +1198,67 @@ HRESULT CDatabase::Open2(IInStream *inStream, IArchiveOpenCallback *progress)
h.Header[0] = p[0];
h.Header[1] = p[1];
if (p[0] == 'B' && p[1] == 'D')
{
/*
It's header for old HFS format.
We don't support old HFS format, but we support
special HFS volume that contains embedded HFS+ volume
*/
if (p[0x7C] != 'H' || p[0x7C + 1] != '+')
return S_FALSE;
/*
h.CTime = Get32(p + 0x2);
h.MTime = Get32(p + 0x6);
h.NumFiles = Get32(p + 0x54);
h.NumFolders = Get32(p + 0x58);
if (h.NumFolders > ((UInt32)1 << 29) ||
h.NumFiles > ((UInt32)1 << 30))
return S_FALSE;
if (progress)
{
UInt64 numFiles = (UInt64)h.NumFiles + h.NumFolders + 1;
RINOK(progress->SetTotal(&numFiles, NULL));
}
h.NumFreeBlocks = Get16(p + 0x22);
*/
UInt32 blockSize = Get32(p + 0x14);
{
unsigned i;
for (i = 9; ((UInt32)1 << i) != blockSize; i++)
if (i == 31)
return S_FALSE;
h.BlockSizeLog = i;
}
h.NumBlocks = Get16(p + 0x12);
/*
we suppose that it has the follwing layout
{
start block with header
[h.NumBlocks]
end block with header
}
*/
PhySize2 = ((UInt64)h.NumBlocks + 2) << h.BlockSizeLog;
UInt32 startBlock = Get16(p + 0x7C + 2);
UInt32 blockCount = Get16(p + 0x7C + 4);
SpecOffset = (UInt64)(1 + startBlock) << h.BlockSizeLog;
UInt64 phy = SpecOffset + ((UInt64)blockCount << h.BlockSizeLog);
if (PhySize2 < phy)
PhySize2 = phy;
RINOK(inStream->Seek(SpecOffset, STREAM_SEEK_SET, NULL));
RINOK(ReadStream_FALSE(inStream, buf, kHfsHeaderSize));
}
if (p[0] != 'H' || (p[1] != '+' && p[1] != 'X'))
return S_FALSE;
h.Version = Get16(p + 2);
@@ -1330,7 +1418,14 @@ STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
{
case kpidExtension: prop = Header.IsHfsX() ? "hfsx" : "hfs"; break;
case kpidMethod: prop = Header.IsHfsX() ? "HFSX" : "HFS+"; break;
case kpidPhySize: prop = PhySize; break;
case kpidPhySize:
{
UInt64 v = SpecOffset + PhySize;
if (v < PhySize2)
v = PhySize2;
prop = v;
break;
}
case kpidClusterSize: prop = (UInt32)1 << Header.BlockSizeLog; break;
case kpidFreeSpace: prop = (UInt64)Header.GetFreeSize(); break;
case kpidMTime: HfsTimeToProp(Header.MTime, prop); break;
@@ -1754,7 +1849,7 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
if (fork.Size == pos)
break;
const CExtent &e = fork.Extents[extentIndex];
RINOK(_stream->Seek((UInt64)e.Pos << Header.BlockSizeLog, STREAM_SEEK_SET, NULL));
RINOK(_stream->Seek(SpecOffset + ((UInt64)e.Pos << Header.BlockSizeLog), STREAM_SEEK_SET, NULL));
UInt64 extentRem = (UInt64)e.NumBlocks << Header.BlockSizeLog;
while (extentRem != 0)
{
@@ -1865,6 +1960,7 @@ STDMETHODIMP CHandler::GetStream(UInt32 index, ISequentialInStream **stream)
}
static const Byte k_Signature[] = {
2, 'B', 'D',
4, 'H', '+', 0, 4,
4, 'H', 'X', 0, 5 };
@@ -1873,6 +1969,6 @@ REGISTER_ARC_I(
k_Signature,
kHeaderPadSize,
NArcInfoFlags::kMultiSignature,
NULL)
IsArc_HFS)
}}

View File

@@ -488,6 +488,16 @@ ARCHIVE_INTERFACE(IOutArchive, 0xA0)
};
/*
ISetProperties::SetProperties()
PROPVARIANT values[i].vt:
VT_EMPTY
VT_BOOL
VT_UI4 - if 32-bit number
VT_UI8 - if 64-bit number
VT_BSTR
*/
ARCHIVE_INTERFACE(ISetProperties, 0x03)
{
STDMETHOD(SetProperties)(const wchar_t * const *names, const PROPVARIANT *values, UInt32 numProps) PURE;

View File

@@ -180,7 +180,7 @@ UInt32 CInArchive::ReadDigits(int numDigits)
Byte b = ReadByte();
if (b < '0' || b > '9')
{
if (b == 0) // it's bug in some CD's
if (b == 0 || b == ' ') // it's bug in some CD's
b = '0';
else
throw CHeaderErrorException();

View File

@@ -109,7 +109,8 @@ void CVersion::ToProp(NCOM::CPropVariant &prop)
prop = sz;
}
static const unsigned kHeaderSize = 4 + 20;
static const unsigned kCoffHeaderSize = 20;
static const unsigned kPeHeaderSize = 4 + kCoffHeaderSize;
static const unsigned k_OptHeader32_Size_MIN = 96;
static const unsigned k_OptHeader64_Size_MIN = 112;
@@ -125,15 +126,14 @@ struct CHeader
UInt16 OptHeaderSize;
UInt16 Flags;
bool Parse(const Byte *p);
void ParseBase(const Byte *p);
bool ParseCoff(const Byte *p);
bool ParsePe(const Byte *p);
bool IsDll() const { return (Flags & PE_IMAGE_FILE_DLL) != 0; }
};
bool CHeader::Parse(const Byte *p)
void CHeader::ParseBase(const Byte *p)
{
if (Get32(p) != k_Signature32)
return false;
p += 4;
G16( 0, Machine);
G16( 2, NumSections);
G32( 4, Time);
@@ -141,6 +141,13 @@ bool CHeader::Parse(const Byte *p)
G32(12, NumSymbols);
G16(16, OptHeaderSize);
G16(18, Flags);
}
bool CHeader::ParsePe(const Byte *p)
{
if (Get32(p) != k_Signature32)
return false;
ParseBase(p + 4);
return OptHeaderSize >= k_OptHeader32_Size_MIN;
}
@@ -380,6 +387,10 @@ void CSection::Parse(const Byte *p)
G32(36, Flags);
}
// IMAGE_FILE_*
static const CUInt32PCharPair g_HeaderCharacts[] =
{
{ 1, "Executable" },
@@ -399,39 +410,65 @@ static const CUInt32PCharPair g_HeaderCharacts[] =
{ 15, "Big-Endian" }
};
static const CUInt32PCharPair g_DllCharacts[] =
// IMAGE_DLLCHARACTERISTICS_*
static const char * const g_DllCharacts[] =
{
{ 5, "HighEntropyVA" },
{ 6, "Relocated" },
{ 7, "Integrity" },
{ 8, "NX-Compatible" },
{ 9, "NoIsolation" },
{ 10, "NoSEH" },
{ 11, "NoBind" },
{ 12, "AppContainer" },
{ 13, "WDM" },
{ 14, "GuardCF" },
{ 15, "TerminalServerAware" }
NULL
, NULL
, NULL
, NULL
, NULL
, "HighEntropyVA"
, "Relocated"
, "Integrity"
, "NX-Compatible"
, "NoIsolation"
, "NoSEH"
, "NoBind"
, "AppContainer"
, "WDM"
, "GuardCF"
, "TerminalServerAware"
};
static const CUInt32PCharPair g_SectFlags[] =
// IMAGE_SCN_* constants:
static const char * const g_SectFlags[] =
{
{ 3, "NoPad" },
{ 5, "Code" },
{ 6, "InitializedData" },
{ 7, "UninitializedData" },
{ 9, "Comments" },
{ 11, "Remove" },
{ 12, "COMDAT" },
{ 15, "GP" },
{ 24, "ExtendedRelocations" },
{ 25, "Discardable" },
{ 26, "NotCached" },
{ 27, "NotPaged" },
{ 28, "Shared" },
{ 29, "Execute" },
{ 30, "Read" },
{ 31, "Write" }
NULL
, NULL
, NULL
, "NoPad"
, NULL
, "Code"
, "InitializedData"
, "UninitializedData"
, "Other"
, "Comments"
, NULL // OVER
, "Remove"
, "COMDAT"
, NULL
, "NO_DEFER_SPEC_EXC"
, "GP" // MEM_FARDATA
, NULL // SYSHEAP
, "PURGEABLE" // 16BIT
, "LOCKED"
, "PRELOAD"
, NULL
, NULL
, NULL
, NULL
, "ExtendedRelocations"
, "Discardable"
, "NotCached"
, "NotPaged"
, "Shared"
, "Execute"
, "Read"
, "Write"
};
static const CUInt32PCharPair g_MachinePairs[] =
@@ -699,7 +736,6 @@ class CHandler:
{
CMyComPtr<IInStream> _stream;
CObjectVector<CSection> _sections;
UInt32 _peOffset;
CHeader _header;
UInt32 _totalSize;
Int32 _mainSubfile;
@@ -720,9 +756,12 @@ class CHandler:
bool _parseResources;
bool _checksumError;
bool IsOpt() const { return _header.OptHeaderSize != 0; }
COptHeader _optHeader;
bool _allowTail;
bool _coffMode;
HRESULT LoadDebugSections(IInStream *stream, bool &thereIsSection);
HRESULT Open2(IInStream *stream, IArchiveOpenCallback *callback);
@@ -742,7 +781,10 @@ class CHandler:
}
public:
CHandler(): _allowTail(false) {}
CHandler(bool coffMode = false):
_coffMode(coffMode),
_allowTail(coffMode)
{}
MY_UNKNOWN_IMP3(IInArchive, IInArchiveGetStream, IArchiveAllowTail)
INTERFACE_IInArchive(;)
@@ -841,6 +883,34 @@ STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
NCOM::CPropVariant prop;
switch (propID)
{
case kpidPhySize: prop = _totalSize; break;
case kpidComment: if (!_versionFullString.IsEmpty()) prop = _versionFullString; break;
case kpidShortComment:
if (!_versionShortString.IsEmpty())
prop = _versionShortString;
else
{
PAIR_TO_PROP(g_MachinePairs, _header.Machine, prop);
}
break;
case kpidName: if (!_originalFilename.IsEmpty()) prop = _originalFilename; break;
// case kpidIsSelfExe: prop = !_header.IsDll(); break;
// case kpidError:
case kpidWarning: if (_checksumError) prop = "Checksum error"; break;
case kpidCpu: PAIR_TO_PROP(g_MachinePairs, _header.Machine, prop); break;
case kpidMTime:
case kpidCTime: TimeToProp(_header.Time, prop); break;
case kpidCharacts: FLAGS_TO_PROP(g_HeaderCharacts, _header.Flags, prop); break;
case kpidMainSubfile: if (_mainSubfile >= 0) prop = (UInt32)_mainSubfile; break;
default:
if (IsOpt())
switch (propID)
{
case kpidSectAlign: prop = _optHeader.SectAlign; break;
case kpidFileAlign: prop = _optHeader.FileAlign; break;
case kpidLinkerVer:
@@ -857,37 +927,19 @@ STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
case kpidInitDataSize: prop = _optHeader.InitDataSize; break;
case kpidUnInitDataSize: prop = _optHeader.UninitDataSize; break;
case kpidImageSize: prop = _optHeader.ImageSize; break;
case kpidPhySize: prop = _totalSize; break;
case kpidHeadersSize: prop = _optHeader.HeadersSize; break;
case kpidChecksum: prop = _optHeader.CheckSum; break;
case kpidComment: if (!_versionFullString.IsEmpty()) prop = _versionFullString; break;
case kpidShortComment:
if (!_versionShortString.IsEmpty())
prop = _versionShortString;
else
{
PAIR_TO_PROP(g_MachinePairs, _header.Machine, prop);
}
break;
case kpidName: if (!_originalFilename.IsEmpty()) prop = _originalFilename; break;
case kpidExtension:
if (_header.IsDll())
prop = _optHeader.IsSybSystem_EFI() ? "efi" : "dll";
prop = "dll";
else if (_optHeader.IsSybSystem_EFI())
prop = "efi";
break;
// case kpidIsSelfExe: prop = !_header.IsDll(); break;
// case kpidError:
case kpidWarning: if (_checksumError) prop = "Checksum error"; break;
case kpidCpu: PAIR_TO_PROP(g_MachinePairs, _header.Machine, prop); break;
case kpidBit64: if (_optHeader.Is64Bit()) prop = true; break;
case kpidSubSystem: TYPE_TO_PROP(g_SubSystems, _optHeader.SubSystem, prop); break;
case kpidMTime:
case kpidCTime: TimeToProp(_header.Time, prop); break;
case kpidCharacts: FLAGS_TO_PROP(g_HeaderCharacts, _header.Flags, prop); break;
case kpidDllCharacts: FLAGS_TO_PROP(g_DllCharacts, _optHeader.DllCharacts, prop); break;
case kpidStackReserve: prop = _optHeader.StackReserve; break;
case kpidStackCommit: prop = _optHeader.StackCommit; break;
@@ -898,8 +950,7 @@ STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
// case kpidAddressOfEntryPoint: prop = _optHeader.AddressOfEntryPoint; break;
// case kpidBaseOfCode: prop = _optHeader.BaseOfCode; break;
// case kpidBaseOfData32: if (!_optHeader.Is64Bit()) prop = _optHeader.BaseOfData32; break;
case kpidMainSubfile: if (_mainSubfile >= 0) prop = (UInt32)_mainSubfile; break;
}
}
prop.Detach(value);
return S_OK;
@@ -1056,7 +1107,24 @@ STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *val
case kpidMTime:
case kpidCTime:
TimeToProp(item.IsDebug ? item.Time : _header.Time, prop); break;
case kpidCharacts: if (item.IsRealSect) FLAGS_TO_PROP(g_SectFlags, item.Flags, prop); break;
case kpidCharacts:
if (item.IsRealSect)
{
UInt32 flags = item.Flags;
const UInt32 MY__IMAGE_SCN_ALIGN_MASK = 0x00F00000;
AString s = FlagsToString(g_SectFlags, ARRAY_SIZE(g_SectFlags), item.Flags & ~MY__IMAGE_SCN_ALIGN_MASK);
const UInt32 align = ((flags >> 20) & 0xF);
if (align != 0)
{
char sz[32];
ConvertUInt32ToString(1 << (align - 1), sz);
s.Add_Space();
s += "align_";
s += sz;
}
prop = s;
}
break;
case kpidZerosTailIsAllowed: if (!item.IsRealSect) prop = true; break;
}
}
@@ -1073,8 +1141,17 @@ HRESULT CHandler::LoadDebugSections(IInStream *stream, bool &thereIsSection)
return S_OK;
const unsigned kEntrySize = 28;
UInt32 numItems = debugLink.Size / kEntrySize;
if (numItems * kEntrySize != debugLink.Size || numItems > 16)
if (numItems > 16)
return S_FALSE;
// MAC's EFI file: numItems can be incorrect. Only first CDebugEntry entry is correct.
// debugLink.Size = kEntrySize + some_data, pointed by entry[0].
if (numItems * kEntrySize != debugLink.Size)
{
// return S_FALSE;
if (numItems > 1)
numItems = 1;
}
UInt64 pa = 0;
unsigned i;
@@ -2114,6 +2191,31 @@ HRESULT CHandler::OpenResources(unsigned sectionIndex, IInStream *stream, IArchi
return S_OK;
}
bool CHeader::ParseCoff(const Byte *p)
{
ParseBase(p);
if (PointerToSymbolTable < kCoffHeaderSize)
return false;
if (NumSymbols >= (1 << 24))
return false;
if (OptHeaderSize != 0 && OptHeaderSize < k_OptHeader32_Size_MIN)
return false;
// 18.04: we reduce false detections
if (NumSections == 0 && OptHeaderSize == 0)
return false;
for (unsigned i = 0; i < ARRAY_SIZE(g_MachinePairs); i++)
if (Machine == g_MachinePairs[i].Value)
return true;
if (Machine == 0)
return true;
return false;
}
static inline bool CheckPeOffset(UInt32 pe)
{
// ((pe & 7) == 0) is for most PE files. But there is unusual EFI-PE file that uses unaligned pe value.
@@ -2133,10 +2235,10 @@ API_FUNC_static_IsArc IsArc_Pe(const Byte *p, size_t size)
UInt32 pe = Get32(p + 0x3C);
if (!CheckPeOffset(pe))
return k_IsArc_Res_NO;
if (pe + kHeaderSize > size)
if (pe + kPeHeaderSize > size)
return k_IsArc_Res_NEED_MORE;
CHeader header;
if (!header.Parse(p + pe))
if (!header.ParsePe(p + pe))
return k_IsArc_Res_NO;
return k_IsArc_Res_YES;
}
@@ -2144,32 +2246,47 @@ API_FUNC_static_IsArc IsArc_Pe(const Byte *p, size_t size)
HRESULT CHandler::Open2(IInStream *stream, IArchiveOpenCallback *callback)
{
UInt32 coffOffset = 0;
if (_coffMode)
{
Byte h[kStartSize];
_mainSubfile = -1;
RINOK(ReadStream_FALSE(stream, h, kStartSize));
if (h[0] != 'M' || h[1] != 'Z')
return S_FALSE;
/* most of PE files contain 0x0090 at offset 2.
But some rare PE files contain another values. So we don't use that check.
if (Get16(h + 2) != 0x90) return false; */
_peOffset = Get32(h + 0x3C);
if (!CheckPeOffset(_peOffset))
Byte h[kCoffHeaderSize];
RINOK(ReadStream_FALSE(stream, h, kCoffHeaderSize));
if (!_header.ParseCoff(h))
return S_FALSE;
}
else
{
Byte h[kHeaderSize];
RINOK(stream->Seek(_peOffset, STREAM_SEEK_SET, NULL));
RINOK(ReadStream_FALSE(stream, h, kHeaderSize));
if (!_header.Parse(h))
return S_FALSE;
UInt32 _peOffset;
{
Byte h[kStartSize];
RINOK(ReadStream_FALSE(stream, h, kStartSize));
if (h[0] != 'M' || h[1] != 'Z')
return S_FALSE;
/* most of PE files contain 0x0090 at offset 2.
But some rare PE files contain another values. So we don't use that check.
if (Get16(h + 2) != 0x90) return false; */
_peOffset = Get32(h + 0x3C);
if (!CheckPeOffset(_peOffset))
return S_FALSE;
coffOffset = _peOffset + 4;
}
{
Byte h[kPeHeaderSize];
RINOK(stream->Seek(_peOffset, STREAM_SEEK_SET, NULL));
RINOK(ReadStream_FALSE(stream, h, kPeHeaderSize));
if (!_header.ParsePe(h))
return S_FALSE;
}
}
UInt32 bufSize = _header.OptHeaderSize + (UInt32)_header.NumSections * kSectionSize;
_totalSize = _peOffset + kHeaderSize + bufSize;
const UInt32 optStart = coffOffset + kCoffHeaderSize;
const UInt32 bufSize = _header.OptHeaderSize + (UInt32)_header.NumSections * kSectionSize;
_totalSize = optStart + bufSize;
CByteBuffer buffer(bufSize);
RINOK(ReadStream_FALSE(stream, buffer, bufSize));
if (_header.OptHeaderSize != 0)
if (!_optHeader.Parse(buffer, _header.OptHeaderSize))
return S_FALSE;
@@ -2207,7 +2324,9 @@ HRESULT CHandler::Open2(IInStream *stream, IArchiveOpenCallback *callback)
for (i = 0; i < _sections.Size(); i++)
_sections[i].UpdateTotalSize(_totalSize);
bool thereISDebug;
bool thereISDebug = false;
if (IsOpt())
{
RINOK(LoadDebugSections(stream, thereISDebug));
const CDirLink &certLink = _optHeader.DirItems[kDirLink_Certificate];
@@ -2256,8 +2375,9 @@ HRESULT CHandler::Open2(IInStream *stream, IArchiveOpenCallback *callback)
_totalSize += (UInt32)k;
}
}
}
if (_header.NumSymbols > 0 && _header.PointerToSymbolTable >= 512)
if (_header.NumSymbols > 0 && _header.PointerToSymbolTable >= optStart)
{
if (_header.NumSymbols >= (1 << 24))
return S_FALSE;
@@ -2306,11 +2426,12 @@ HRESULT CHandler::Open2(IInStream *stream, IArchiveOpenCallback *callback)
}
if (IsOpt())
if (_optHeader.CheckSum != 0)
{
RINOK(stream->Seek(0, STREAM_SEEK_SET, NULL));
UInt32 checkSum = 0;
RINOK(CalcCheckSum(stream, _totalSize, _peOffset + kHeaderSize + k_CheckSum_Field_Offset, checkSum));
RINOK(CalcCheckSum(stream, _totalSize, optStart + k_CheckSum_Field_Offset, checkSum));
_checksumError = (checkSum != _optHeader.CheckSum);
}
@@ -2333,6 +2454,7 @@ HRESULT CHandler::Open2(IInStream *stream, IArchiveOpenCallback *callback)
const CSection &sect = _sections[i];
CMixItem mixItem;
mixItem.SectionIndex = i;
if (IsOpt())
if (_parseResources && sect.Name == ".rsrc" && _items.IsEmpty())
{
const unsigned numMixItems = _mixItems.Size();
@@ -2480,6 +2602,8 @@ STDMETHODIMP CHandler::Close()
{
_totalSize = 0;
_checksumError = false;
_mainSubfile = -1;
_stream.Release();
_sections.Clear();
_mixItems.Clear();
@@ -2675,10 +2799,41 @@ REGISTER_ARC_I(
0,
NArcInfoFlags::kPreArc,
IsArc_Pe)
}
namespace NCoff {
API_FUNC_static_IsArc IsArc_Coff(const Byte *p, size_t size)
{
if (size < NPe::kCoffHeaderSize)
return k_IsArc_Res_NEED_MORE;
NPe::CHeader header;
if (!header.ParseCoff(p))
return k_IsArc_Res_NO;
return k_IsArc_Res_YES;
}
}
/*
static const Byte k_Signature[] =
{
2, 0x4C, 0x01, // x86
2, 0x64, 0x86, // x64
2, 0x64, 0xAA // ARM64
};
REGISTER_ARC_I_CLS(
*/
REGISTER_ARC_I_CLS_NO_SIG(
NPe::CHandler(true),
"COFF", "obj", 0, 0xC6,
// k_Signature,
0,
// NArcInfoFlags::kMultiSignature |
NArcInfoFlags::kStartOpen,
IsArc_Coff)
}
namespace NTe {

View File

@@ -139,11 +139,10 @@ static unsigned ReadVarInt(const Byte *p, size_t maxSize, UInt64 *val)
{
*val = 0;
for (unsigned i = 0; i < maxSize;)
for (unsigned i = 0; i < maxSize && i < 10;)
{
Byte b = p[i];
if (i < 10)
*val |= (UInt64)(b & 0x7F) << (7 * i);
*val |= (UInt64)(b & 0x7F) << (7 * i);
i++;
if ((b & 0x80) == 0)
return i;
@@ -1086,7 +1085,7 @@ HRESULT CUnpacker::Create(DECL_EXTERNAL_CODECS_LOC_VARS const CItem &item, bool
if (!lzCoder)
{
const UInt32 methodID = 0x40305;
RINOK(CreateCoder(EXTERNAL_CODECS_LOC_VARS methodID, false, lzCoder));
RINOK(CreateCoder_Id(EXTERNAL_CODECS_LOC_VARS methodID, false, lzCoder));
if (!lzCoder)
return E_NOTIMPL;
}
@@ -1363,7 +1362,9 @@ static const Byte kProps[] =
kpidCharacts,
kpidSymLink,
kpidHardLink,
kpidCopyLink
kpidCopyLink,
kpidVolumeIndex
};
@@ -1794,6 +1795,18 @@ STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *val
case kpidSplitBefore: prop = item.IsSplitBefore(); break;
case kpidSplitAfter: prop = lastItem.IsSplitAfter(); break;
case kpidVolumeIndex:
{
if (item.VolIndex < _arcs.Size())
{
const CInArcInfo &arcInfo = _arcs[item.VolIndex].Info;
if (arcInfo.IsVolume())
prop = (UInt64)arcInfo.GetVolIndex();
}
break;
}
case kpidCRC:
{
const CItem *item2 = (lastItem.IsSplitAfter() ? &item : &lastItem);

View File

@@ -180,7 +180,7 @@ struct CItem
AString Name;
int VolIndex;
unsigned VolIndex;
int NextItem;
UInt32 UnixMTime;

View File

@@ -777,7 +777,9 @@ static const Byte kProps[] =
kpidCRC,
kpidHostOS,
kpidMethod,
kpidUnpackVer
kpidUnpackVer,
kpidVolumeIndex
};
static const Byte kArcProps[] =
@@ -995,6 +997,12 @@ STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *val
case kpidCommented: prop = item.IsCommented(); break;
case kpidSplitBefore: prop = item.IsSplitBefore(); break;
case kpidSplitAfter: prop = _items[refItem.ItemIndex + refItem.NumItems - 1].IsSplitAfter(); break;
case kpidVolumeIndex:
if (_arcInfo.Is_VolNumber_Defined())
prop = (UInt32)(_arcInfo.VolNumber + refItem.VolumeIndex);
break;
case kpidCRC:
{
prop = ((lastItem.IsSplitAfter()) ? item.FileCRC : lastItem.FileCRC);
@@ -1691,7 +1699,7 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
methodID += 2;
else
methodID += 3;
RINOK(CreateCoder(EXTERNAL_CODECS_VARS methodID, false, mi.Coder));
RINOK(CreateCoder_Id(EXTERNAL_CODECS_VARS methodID, false, mi.Coder));
}
if (mi.Coder == 0)

View File

@@ -26,7 +26,7 @@ struct CInArcInfo
UInt32 DataCRC;
bool EndOfArchive_was_Read;
CInArcInfo(): EndFlags(0), EndOfArchive_was_Read(false) {}
CInArcInfo(): EndFlags(0), EndOfArchive_was_Read(false), VolNumber(0) {}
UInt64 GetPhySize() const { return EndPos - StartPos; }

View File

@@ -2,7 +2,6 @@
#include "StdAfx.h"
#include "../../../C/7zCrc.h"
#include "../../../C/Alloc.h"
#include "../../../C/CpuArch.h"
#include "../../../C/Xz.h"
@@ -1209,8 +1208,10 @@ HRESULT CHandler::Decompress(ISequentialOutStream *outStream, Byte *outBuf, bool
else
{
ECoderStatus status;
XzUnpacker_Init(&_xz);
SRes res = XzUnpacker_Code(&_xz, dest, &destLen, _inputBuffer, &srcLen, CODER_FINISH_END, &status);
SRes res = XzUnpacker_CodeFull(&_xz,
dest, &destLen,
_inputBuffer, &srcLen,
CODER_FINISH_END, &status);
if (res != 0)
return SResToHRESULT(res);
if (status != CODER_STATUS_NEEDS_MORE_INPUT || !XzUnpacker_IsStreamWasFinished(&_xz))

View File

@@ -94,7 +94,9 @@ static bool ParseInt64(const char *p, Int64 &val)
static bool ParseInt64_MTime(const char *p, Int64 &val)
{
// rare case tar contains spaces instead of MTime
// rare case tar : ZEROs in Docker-Windows TARs
// rare case tar : spaces
if (GetUi32(p) != 0)
for (unsigned i = 0; i < 12; i++)
if (p[i] != ' ')
return ParseInt64(p, val);

View File

@@ -24,9 +24,7 @@
#include "IArchive.h"
#ifndef EXTRACT_ONLY
#include "Common/HandlerOut.h"
#endif
using namespace NWindows;
@@ -49,14 +47,22 @@ class CHandler:
public IInArchive,
public IArchiveOpenSeq,
public IInArchiveGetStream,
public ISetProperties,
#ifndef EXTRACT_ONLY
public IOutArchive,
public ISetProperties,
public CMultiMethodProps,
#endif
public CMyUnknownImp
public CMyUnknownImp,
#ifndef EXTRACT_ONLY
public CMultiMethodProps
#else
public CCommonMethodProps
#endif
{
NCompress::NXz::CStatInfo _stat;
CXzStatInfo _stat;
SRes MainDecodeSRes;
bool _isArc;
bool _needSeekToStart;
@@ -71,34 +77,48 @@ class CHandler:
UInt64 _numSolidBytes;
HRESULT SetSolidFromString(const UString &s);
HRESULT SetSolidFromPROPVARIANT(const PROPVARIANT &value);
HRESULT SetProperty(const wchar_t *name, const PROPVARIANT &value);
void InitSolid()
void InitXz()
{
_filterId = 0;
_numSolidBytes = XZ_PROPS__BLOCK_SIZE__AUTO;
}
#endif
void Init()
{
InitSolid();
_filterId = 0;
CMultiMethodProps::Init();
#ifndef EXTRACT_ONLY
InitXz();
CMultiMethodProps::Init();
#else
CCommonMethodProps::InitCommon();
#endif
}
#endif
HRESULT SetProperty(const wchar_t *name, const PROPVARIANT &value);
HRESULT Open2(IInStream *inStream, /* UInt32 flags, */ IArchiveOpenCallback *callback);
HRESULT Decode2(ISequentialInStream *seqInStream, ISequentialOutStream *outStream,
NCompress::NXz::CDecoder &decoder, ICompressProgressInfo *progress)
HRESULT Decode(NCompress::NXz::CDecoder &decoder,
ISequentialInStream *seqInStream,
ISequentialOutStream *outStream,
ICompressProgressInfo *progress)
{
#ifndef _7ZIP_ST
decoder._numThreads = _numThreads;
#endif
decoder._memUsage = _memUsage;
MainDecodeSRes = SZ_OK;
RINOK(decoder.Decode(seqInStream, outStream,
NULL, // *outSizeLimit
true, // finishStream
progress));
_stat = decoder;
_stat = decoder.Stat;
MainDecodeSRes = decoder.MainDecodeSRes;
_phySize_Defined = true;
return S_OK;
}
@@ -107,9 +127,9 @@ public:
MY_QUERYINTERFACE_BEGIN2(IInArchive)
MY_QUERYINTERFACE_ENTRY(IArchiveOpenSeq)
MY_QUERYINTERFACE_ENTRY(IInArchiveGetStream)
MY_QUERYINTERFACE_ENTRY(ISetProperties)
#ifndef EXTRACT_ONLY
MY_QUERYINTERFACE_ENTRY(IOutArchive)
MY_QUERYINTERFACE_ENTRY(ISetProperties)
#endif
MY_QUERYINTERFACE_END
MY_ADDREF_RELEASE
@@ -117,10 +137,10 @@ public:
INTERFACE_IInArchive(;)
STDMETHOD(OpenSeq)(ISequentialInStream *stream);
STDMETHOD(GetStream)(UInt32 index, ISequentialInStream **stream);
STDMETHOD(SetProperties)(const wchar_t * const *names, const PROPVARIANT *values, UInt32 numProps);
#ifndef EXTRACT_ONLY
INTERFACE_IOutArchive(;)
STDMETHOD(SetProperties)(const wchar_t * const *names, const PROPVARIANT *values, UInt32 numProps);
#endif
size_t _blocksArraySize;
@@ -146,7 +166,7 @@ CHandler::CHandler():
_blocksArraySize(0)
{
#ifndef EXTRACT_ONLY
Init();
InitXz();
#endif
}
@@ -307,7 +327,7 @@ STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
NCOM::CPropVariant prop;
switch (propID)
{
case kpidPhySize: if (_phySize_Defined) prop = _stat.PhySize; break;
case kpidPhySize: if (_phySize_Defined) prop = _stat.InSize; break;
case kpidNumStreams: if (_stat.NumStreams_Defined) prop = _stat.NumStreams; break;
case kpidNumBlocks: if (_stat.NumBlocks_Defined) prop = _stat.NumBlocks; break;
case kpidUnpackSize: if (_stat.UnpackSize_Defined) prop = _stat.OutSize; break;
@@ -330,19 +350,22 @@ STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
case kpidErrorFlags:
{
UInt32 v = 0;
SRes sres = MainDecodeSRes; // _stat.DecodeRes2; //
if (!_isArc) v |= kpv_ErrorFlags_IsNotArc;
if (_stat.UnexpectedEnd) v |= kpv_ErrorFlags_UnexpectedEnd;
if (/*_stat.UnexpectedEnd */ sres == SZ_ERROR_INPUT_EOF) v |= kpv_ErrorFlags_UnexpectedEnd;
if (_stat.DataAfterEnd) v |= kpv_ErrorFlags_DataAfterEnd;
if (_stat.HeadersError) v |= kpv_ErrorFlags_HeadersError;
if (_stat.Unsupported) v |= kpv_ErrorFlags_UnsupportedMethod;
if (_stat.DataError) v |= kpv_ErrorFlags_DataError;
if (_stat.CrcError) v |= kpv_ErrorFlags_CrcError;
prop = v;
if (/* _stat.HeadersError */ sres == SZ_ERROR_ARCHIVE) v |= kpv_ErrorFlags_HeadersError;
if (/* _stat.Unsupported */ sres == SZ_ERROR_UNSUPPORTED) v |= kpv_ErrorFlags_UnsupportedMethod;
if (/* _stat.DataError */ sres == SZ_ERROR_DATA) v |= kpv_ErrorFlags_DataError;
if (/* _stat.CrcError */ sres == SZ_ERROR_CRC) v |= kpv_ErrorFlags_CrcError;
if (v != 0)
prop = v;
break;
}
case kpidMainSubfile:
{
// debug only, comment it:
// if (_blocks) prop = (UInt32)0;
break;
}
@@ -365,7 +388,7 @@ STDMETHODIMP CHandler::GetProperty(UInt32, PROPID propID, PROPVARIANT *value)
switch (propID)
{
case kpidSize: if (_stat.UnpackSize_Defined) prop = _stat.OutSize; break;
case kpidPackSize: if (_phySize_Defined) prop = _stat.PhySize; break;
case kpidPackSize: if (_phySize_Defined) prop = _stat.InSize; break;
case kpidMethod: if (!_methodsString.IsEmpty()) prop = _methodsString; break;
}
prop.Detach(value);
@@ -483,10 +506,10 @@ HRESULT CHandler::Open2(IInStream *inStream, /* UInt32 flags, */ IArchiveOpenCal
}
}
RINOK(inStream->Seek(0, STREAM_SEEK_END, &_stat.PhySize));
RINOK(inStream->Seek(0, STREAM_SEEK_END, &_stat.InSize));
if (callback)
{
RINOK(callback->SetTotal(NULL, &_stat.PhySize));
RINOK(callback->SetTotal(NULL, &_stat.InSize));
}
CSeekInStreamWrap inStreamImp;
@@ -621,7 +644,7 @@ STDMETHODIMP CHandler::OpenSeq(ISequentialInStream *stream)
STDMETHODIMP CHandler::Close()
{
_stat.Clear();
XzStatInfo_Clear(&_stat);
_isArc = false;
_needSeekToStart = false;
@@ -637,6 +660,8 @@ STDMETHODIMP CHandler::Close()
_blocksArraySize = 0;
_maxBlocksSize = 0;
MainDecodeSRes = SZ_OK;
return S_OK;
}
@@ -743,6 +768,8 @@ static HRESULT DecodeBlock(CXzUnpackerCPP2 &xzu,
xzu.p.streamFlags = (UInt16)streamFlags;
XzUnpacker_PrepareToRandomBlockDecoding(&xzu.p);
XzUnpacker_SetOutBuf(&xzu.p, dest, unpackSize);
const UInt64 packSizeAligned = packSize + ((0 - (unsigned)packSize) & 3);
UInt64 packRem = packSizeAligned;
@@ -771,8 +798,11 @@ static HRESULT DecodeBlock(CXzUnpackerCPP2 &xzu,
ECoderStatus status;
SRes res = XzUnpacker_Code(&xzu.p,
dest + outPos, &outLen,
// dest + outPos,
NULL,
&outLen,
xzu.InBuf + inPos, &inLen,
(inLen == 0), // srcFinished
CODER_FINISH_END, &status);
// return E_OUTOFMEMORY;
@@ -890,15 +920,16 @@ STDMETHODIMP CHandler::GetStream(UInt32 index, ISequentialInStream **stream)
return E_INVALIDARG;
if (!_stat.UnpackSize_Defined
|| _maxBlocksSize == 0 // 18.02
|| _maxBlocksSize > kMaxBlockSize_for_GetStream
|| _maxBlocksSize != (size_t)_maxBlocksSize)
return S_FALSE;
UInt64 physSize = (UInt64)(sizeof(size_t)) << 29;
bool ramSize_Defined = NSystem::GetRamSize(physSize);
if (ramSize_Defined)
UInt64 memSize;
if (!NSystem::GetRamSize(memSize))
memSize = (UInt64)(sizeof(size_t)) << 28;
{
if (_maxBlocksSize > physSize / 4)
if (_maxBlocksSize > memSize / 4)
return S_FALSE;
}
@@ -917,6 +948,31 @@ STDMETHODIMP CHandler::GetStream(UInt32 index, ISequentialInStream **stream)
}
static Int32 Get_Extract_OperationResult(const NCompress::NXz::CDecoder &decoder)
{
Int32 opRes;
SRes sres = decoder.MainDecodeSRes; // decoder.Stat.DecodeRes2;
if (sres == SZ_ERROR_NO_ARCHIVE) // (!IsArc)
opRes = NExtract::NOperationResult::kIsNotArc;
else if (sres == SZ_ERROR_INPUT_EOF) // (UnexpectedEnd)
opRes = NExtract::NOperationResult::kUnexpectedEnd;
else if (decoder.Stat.DataAfterEnd)
opRes = NExtract::NOperationResult::kDataAfterEnd;
else if (sres == SZ_ERROR_CRC) // (CrcError)
opRes = NExtract::NOperationResult::kCRCError;
else if (sres == SZ_ERROR_UNSUPPORTED) // (Unsupported)
opRes = NExtract::NOperationResult::kUnsupportedMethod;
else if (sres == SZ_ERROR_ARCHIVE) // (HeadersError)
opRes = NExtract::NOperationResult::kDataError;
else if (sres == SZ_ERROR_DATA) // (DataError)
opRes = NExtract::NOperationResult::kDataError;
else if (sres != SZ_OK)
opRes = NExtract::NOperationResult::kDataError;
else
opRes = NExtract::NOperationResult::kOK;
return opRes;
}
@@ -930,7 +986,7 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
return E_INVALIDARG;
if (_phySize_Defined)
extractCallback->SetTotal(_stat.PhySize);
extractCallback->SetTotal(_stat.InSize);
UInt64 currentTotalPacked = 0;
RINOK(extractCallback->SetCompleted(&currentTotalPacked));
@@ -959,9 +1015,18 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
else
_needSeekToStart = true;
NCompress::NXz::CDecoder decoder;
RINOK(Decode2(_seqStream, realOutStream, decoder, lpsRef));
Int32 opRes = decoder.Get_Extract_OperationResult();
HRESULT hres = Decode(decoder, _seqStream, realOutStream, lpsRef);
if (!decoder.MainDecodeSRes_wasUsed)
return hres == S_OK ? E_FAIL : hres;
Int32 opRes = Get_Extract_OperationResult(decoder);
if (opRes == NExtract::NOperationResult::kOK
&& hres != S_OK)
opRes = NExtract::NOperationResult::kDataError;
realOutStream.Release();
return extractCallback->SetOperationResult(opRes);
@@ -1112,7 +1177,7 @@ STDMETHODIMP CHandler::UpdateItems(ISequentialOutStream *outStream, UInt32 numIt
if (_stream)
{
if (_phySize_Defined)
RINOK(updateCallback->SetTotal(_stat.PhySize));
RINOK(updateCallback->SetTotal(_stat.InSize));
RINOK(_stream->Seek(0, STREAM_SEEK_SET, NULL));
}
@@ -1125,55 +1190,7 @@ STDMETHODIMP CHandler::UpdateItems(ISequentialOutStream *outStream, UInt32 numIt
COM_TRY_END
}
HRESULT CHandler::SetSolidFromString(const UString &s)
{
UString s2 = s;
s2.MakeLower_Ascii();
{
const wchar_t *start = ((const wchar_t *)s2);
const wchar_t *end;
UInt64 v = ConvertStringToUInt64(start, &end);
if (start == end)
return E_INVALIDARG;
if ((unsigned)(end - start) + 1 != s2.Len())
return E_INVALIDARG;
wchar_t c = *end;
{
unsigned numBits;
switch (c)
{
case 'b': numBits = 0; break;
case 'k': numBits = 10; break;
case 'm': numBits = 20; break;
case 'g': numBits = 30; break;
case 't': numBits = 40; break;
default: return E_INVALIDARG;
}
_numSolidBytes = (v << numBits);
}
}
return S_OK;
}
HRESULT CHandler::SetSolidFromPROPVARIANT(const PROPVARIANT &value)
{
bool isSolid;
switch (value.vt)
{
case VT_EMPTY: isSolid = true; break;
case VT_BOOL: isSolid = (value.boolVal != VARIANT_FALSE); break;
case VT_BSTR:
if (StringToBool(value.bstrVal, isSolid))
break;
return SetSolidFromString(value.bstrVal);
default: return E_INVALIDARG;
}
_numSolidBytes = (isSolid ? XZ_PROPS__BLOCK_SIZE__SOLID : XZ_PROPS__BLOCK_SIZE__AUTO);
return S_OK;
}
#endif
HRESULT CHandler::SetProperty(const wchar_t *nameSpec, const PROPVARIANT &value)
@@ -1183,20 +1200,53 @@ HRESULT CHandler::SetProperty(const wchar_t *nameSpec, const PROPVARIANT &value)
if (name.IsEmpty())
return E_INVALIDARG;
#ifndef EXTRACT_ONLY
if (name[0] == L's')
{
name.Delete(0);
if (name.IsEmpty())
return SetSolidFromPROPVARIANT(value);
if (value.vt != VT_EMPTY)
return E_INVALIDARG;
return SetSolidFromString(name);
const wchar_t *s = name.Ptr(1);
if (*s == 0)
{
bool useStr = false;
bool isSolid;
switch (value.vt)
{
case VT_EMPTY: isSolid = true; break;
case VT_BOOL: isSolid = (value.boolVal != VARIANT_FALSE); break;
case VT_BSTR:
if (!StringToBool(value.bstrVal, isSolid))
useStr = true;
break;
default: return E_INVALIDARG;
}
if (!useStr)
{
_numSolidBytes = (isSolid ? XZ_PROPS__BLOCK_SIZE__SOLID : XZ_PROPS__BLOCK_SIZE__AUTO);
return S_OK;
}
}
return ParseSizeString(s, value,
0, // percentsBase
_numSolidBytes) ? S_OK: E_INVALIDARG;
}
return CMultiMethodProps::SetProperty(name, value);
#else
{
HRESULT hres;
if (SetCommonProperty(name, value, hres))
return hres;
}
return E_INVALIDARG;
#endif
}
STDMETHODIMP CHandler::SetProperties(const wchar_t * const *names, const PROPVARIANT *values, UInt32 numProps)
{
COM_TRY_BEGIN
@@ -1208,6 +1258,8 @@ STDMETHODIMP CHandler::SetProperties(const wchar_t * const *names, const PROPVAR
RINOK(SetProperty(names[i], values[i]));
}
#ifndef EXTRACT_ONLY
if (!_filterMethod.MethodName.IsEmpty())
{
unsigned k;
@@ -1238,12 +1290,13 @@ STDMETHODIMP CHandler::SetProperties(const wchar_t * const *names, const PROPVAR
return E_INVALIDARG;
}
#endif
return S_OK;
COM_TRY_END
}
#endif
REGISTER_ARC_IO(
"xz", "xz txz", "* .tar", 0xC,

View File

@@ -384,7 +384,7 @@ HRESULT CAddCommon::Compress(
methodId = kMethodId_ZipBase + method;
break;
}
RINOK(CreateCoder(
RINOK(CreateCoder_Id(
EXTERNAL_CODECS_LOC_VARS
methodId, true, _compressEncoder));
if (!_compressEncoder)

View File

@@ -112,7 +112,8 @@ static const CUInt32PCharPair g_HeaderCharacts[] =
{ 3, "Descriptor" },
// { 5, "Patched" },
{ 6, kMethod_StrongCrypto },
{ 11, "UTF8" }
{ 11, "UTF8" },
{ 14, "Alt" }
};
struct CIdToNamePair
@@ -169,6 +170,7 @@ static const Byte kProps[] =
kpidUnpackVer,
kpidVolumeIndex,
kpidOffset
// kpidIsAltStream
};
static const Byte kArcProps[] =
@@ -307,6 +309,8 @@ STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
prop = true;
break;
}
// case kpidIsAltStream: prop = true; break;
}
prop.Detach(value);
COM_TRY_END
@@ -333,6 +337,21 @@ STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *val
UString res;
item.GetUnicodeString(res, item.Name, false, _forceCodePage, _specifiedCodePage);
NItemName::ReplaceToOsSlashes_Remove_TailSlash(res);
/*
if (item.ParentOfAltStream >= 0)
{
const CItemEx &prevItem = m_Items[item.ParentOfAltStream];
UString prevName;
prevItem.GetUnicodeString(prevName, prevItem.Name, false, _forceCodePage, _specifiedCodePage);
NItemName::ReplaceToOsSlashes_Remove_TailSlash(prevName);
if (res.IsPrefixedBy(prevName))
if (IsString1PrefixedByString2(res.Ptr(prevName.Len()), k_SpecName_NTFS_STREAM))
{
res.Delete(prevName.Len(), (unsigned)strlen(k_SpecName_NTFS_STREAM));
res.Insert(prevName.Len(), L":");
}
}
*/
prop = res;
break;
}
@@ -596,6 +615,19 @@ STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *val
case kpidOffset:
prop = item.LocalHeaderPos;
break;
/*
case kpidIsAltStream:
prop = (bool)(item.ParentOfAltStream >= 0); // item.IsAltStream();
break;
case kpidName:
if (item.ParentOfAltStream >= 0)
{
// extract name of stream here
}
break;
*/
}
prop.Detach(value);
@@ -604,6 +636,85 @@ STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *val
}
/*
STDMETHODIMP CHandler::GetNumRawProps(UInt32 *numProps)
{
*numProps = 0;
return S_OK;
}
STDMETHODIMP CHandler::GetRawPropInfo(UInt32 index, BSTR *name, PROPID *propID)
{
UNUSED_VAR(index);
*propID = 0;
*name = 0;
return S_OK;
}
STDMETHODIMP CHandler::GetParent(UInt32 index, UInt32 *parent, UInt32 *parentType)
{
*parentType = NParentType::kDir;
*parent = (UInt32)(Int32)-1;
if (index >= m_Items.Size())
return S_OK;
const CItemEx &item = m_Items[index];
if (item.ParentOfAltStream >= 0)
{
*parentType = NParentType::kAltStream;
*parent = item.ParentOfAltStream;
}
return S_OK;
}
STDMETHODIMP CHandler::GetRawProp(UInt32 index, PROPID propID, const void **data, UInt32 *dataSize, UInt32 *propType)
{
UNUSED_VAR(index);
UNUSED_VAR(propID);
*data = NULL;
*dataSize = 0;
*propType = 0;
return S_OK;
}
void CHandler::MarkAltStreams(CObjectVector<CItemEx> &items)
{
int prevIndex = -1;
UString prevName;
UString name;
for (unsigned i = 0; i < items.Size(); i++)
{
CItemEx &item = m_Items[i];
if (item.IsAltStream())
{
if (prevIndex == -1)
continue;
if (prevName.IsEmpty())
{
const CItemEx &prevItem = m_Items[prevIndex];
prevItem.GetUnicodeString(prevName, prevItem.Name, false, _forceCodePage, _specifiedCodePage);
NItemName::ReplaceToOsSlashes_Remove_TailSlash(prevName);
}
name.Empty();
item.GetUnicodeString(name, item.Name, false, _forceCodePage, _specifiedCodePage);
NItemName::ReplaceToOsSlashes_Remove_TailSlash(name);
if (name.IsPrefixedBy(prevName))
if (IsString1PrefixedByString2(name.Ptr(prevName.Len()), k_SpecName_NTFS_STREAM))
item.ParentOfAltStream = prevIndex;
}
else
{
prevIndex = i;
prevName.Empty();
}
}
}
*/
STDMETHODIMP CHandler::Open(IInStream *inStream,
const UInt64 *maxCheckStartPosition, IArchiveOpenCallback *callback)
{
@@ -617,6 +728,7 @@ STDMETHODIMP CHandler::Open(IInStream *inStream,
m_Items.Clear();
m_Archive.ClearRefs(); // we don't want to clear error flags
}
// MarkAltStreams(m_Items);
return res;
}
catch(...) { Close(); throw; }
@@ -738,7 +850,7 @@ public:
IArchiveExtractCallback *extractCallback,
ICompressProgressInfo *compressProgress,
#ifndef _7ZIP_ST
UInt32 numThreads,
UInt32 numThreads, UInt64 memUsage,
#endif
Int32 &res);
};
@@ -767,7 +879,7 @@ HRESULT CZipDecoder::Decode(
IArchiveExtractCallback *extractCallback,
ICompressProgressInfo *compressProgress,
#ifndef _7ZIP_ST
UInt32 numThreads,
UInt32 numThreads, UInt64 memUsage,
#endif
Int32 &res)
{
@@ -962,7 +1074,7 @@ HRESULT CZipDecoder::Decode(
szMethodID = kMethodId_ZipBase + (Byte)id;
}
RINOK(CreateCoder(EXTERNAL_CODECS_LOC_VARS szMethodID, false, mi.Coder));
RINOK(CreateCoder_Id(EXTERNAL_CODECS_LOC_VARS szMethodID, false, mi.Coder));
if (!mi.Coder)
{
@@ -974,16 +1086,7 @@ HRESULT CZipDecoder::Decode(
}
ICompressCoder *coder = methodItems[m].Coder;
{
CMyComPtr<ICompressSetDecoderProperties2> setDecoderProperties;
coder->QueryInterface(IID_ICompressSetDecoderProperties2, (void **)&setDecoderProperties);
if (setDecoderProperties)
{
Byte properties = (Byte)item.Flags;
RINOK(setDecoderProperties->SetDecoderProperties2(&properties, 1));
}
}
#ifndef _7ZIP_ST
{
@@ -994,7 +1097,27 @@ HRESULT CZipDecoder::Decode(
RINOK(setCoderMt->SetNumberOfThreads(numThreads));
}
}
// if (memUsage != 0)
{
CMyComPtr<ICompressSetMemLimit> setMemLimit;
coder->QueryInterface(IID_ICompressSetMemLimit, (void **)&setMemLimit);
if (setMemLimit)
{
RINOK(setMemLimit->SetMemLimit(memUsage));
}
}
#endif
{
CMyComPtr<ICompressSetDecoderProperties2> setDecoderProperties;
coder->QueryInterface(IID_ICompressSetDecoderProperties2, (void **)&setDecoderProperties);
if (setDecoderProperties)
{
Byte properties = (Byte)item.Flags;
RINOK(setDecoderProperties->SetDecoderProperties2(&properties, 1));
}
}
CMyComPtr<ISequentialInStream> inStreamNew;
@@ -1319,7 +1442,7 @@ STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
m_Archive, item, realOutStream, extractCallback,
progress,
#ifndef _7ZIP_ST
_props._numThreads,
_props._numThreads, _props._memUsage,
#endif
res);

View File

@@ -25,6 +25,7 @@ extern const char * const kMethodNames2[kNumMethodNames2];
class CHandler:
public IInArchive,
// public IArchiveGetRawProps,
public IOutArchive,
public ISetProperties,
PUBLIC_ISetCompressCodecsInfo
@@ -32,6 +33,7 @@ class CHandler:
{
public:
MY_QUERYINTERFACE_BEGIN2(IInArchive)
// MY_QUERYINTERFACE_ENTRY(IArchiveGetRawProps)
MY_QUERYINTERFACE_ENTRY(IOutArchive)
MY_QUERYINTERFACE_ENTRY(ISetProperties)
QUERY_ENTRY_ISetCompressCodecsInfo
@@ -39,6 +41,7 @@ public:
MY_ADDREF_RELEASE
INTERFACE_IInArchive(;)
// INTERFACE_IArchiveGetRawProps(;)
INTERFACE_IOutArchive(;)
STDMETHOD(SetProperties)(const wchar_t * const *names, const PROPVARIANT *values, UInt32 numProps);
@@ -75,6 +78,10 @@ private:
_forceCodePage = false;
_specifiedCodePage = CP_OEMCP;
}
// void MarkAltStreams(CObjectVector<CItemEx> &items);
HRESULT GetOutProperty(IArchiveUpdateCallback *callback, UInt32 callbackIndex, Int32 arcIndex, PROPID propID, PROPVARIANT *value);
};
}}

View File

@@ -136,6 +136,7 @@ STDMETHODIMP CHandler::UpdateItems(ISequentialOutStream *outStream, UInt32 numIt
thereAreAesUpdates = true;
if (!IntToBool(newProps))
ui.IsDir = inputItem.IsDir();
// ui.IsAltStream = inputItem.IsAltStream();
}
if (IntToBool(newProps))
@@ -175,6 +176,33 @@ STDMETHODIMP CHandler::UpdateItems(ISequentialOutStream *outStream, UInt32 numIt
ui.IsDir = (prop.boolVal != VARIANT_FALSE);
}
/*
{
bool isAltStream = false;
{
NCOM::CPropVariant prop;
RINOK(callback->GetProperty(i, kpidIsAltStream, &prop));
if (prop.vt == VT_BOOL)
isAltStream = (prop.boolVal != VARIANT_FALSE);
else if (prop.vt != VT_EMPTY)
return E_INVALIDARG;
}
if (isAltStream)
{
if (ui.IsDir)
return E_INVALIDARG;
int delim = name.ReverseFind(L':');
if (delim >= 0)
{
name.Delete(delim, 1);
name.Insert(delim, UString(k_SpecName_NTFS_STREAM));
ui.IsAltStream = true;
}
}
}
*/
{
CPropVariant prop;
RINOK(callback->GetProperty(i, kpidTimeType, &prop));
@@ -348,7 +376,8 @@ STDMETHODIMP CHandler::UpdateItems(ISequentialOutStream *outStream, UInt32 numIt
{
CMethodId methodId;
UInt32 numStreams;
if (!FindMethod(EXTERNAL_CODECS_VARS methodName, methodId, numStreams))
if (FindMethod_Index(EXTERNAL_CODECS_VARS methodName, true,
methodId, numStreams) < 0)
return E_NOTIMPL;
if (numStreams != 1)
return E_NOTIMPL;

View File

@@ -131,6 +131,7 @@ namespace NFileHeader
const unsigned kDescriptorUsedMask = 1 << 3;
const unsigned kStrongEncrypted = 1 << 6;
const unsigned kUtf8 = 1 << 11;
const unsigned kAltStream = 1 << 14;
const unsigned kImplodeDictionarySizeMask = 1 << 1;
const unsigned kImplodeLiteralsOnMask = 1 << 2;

View File

@@ -1993,6 +1993,13 @@ HRESULT CVols::ParseArcName(IArchiveOpenVolumeCallback *volCallback)
}
else if (ext.IsEqualTo_Ascii_NoCase("exe"))
{
/* possible cases:
- exe with zip inside
- sfx: a.exe, a.z02, a.z03,... , a.zip
a.exe is start volume.
- zip renamed to exe
*/
StartIsExe = true;
BaseName = name;
StartVolIndex = 0;
@@ -2000,7 +2007,22 @@ HRESULT CVols::ParseArcName(IArchiveOpenVolumeCallback *volCallback)
We can open arc.zip, if it was requesed to open arc.exe.
But it's possible that arc.exe and arc.zip are not parts of same archive.
So we can disable such operation */
return S_FALSE; // don't open arc.zip instead of arc.exe
// 18.04: we still want to open zip renamed to exe.
/*
{
UString volName = name;
volName += IsUpperCase ? "Z01" : "z01";
{
CMyComPtr<IInStream> stream;
HRESULT res2 = volCallback->GetStream(volName, &stream);
if (res2 == S_OK)
DisableVolsSearch = true;
}
}
*/
DisableVolsSearch = true;
return S_OK;
}
else if (ext[0] == 'z' || ext[0] == 'Z')
{
@@ -2040,6 +2062,9 @@ HRESULT CVols::ParseArcName(IArchiveOpenVolumeCallback *volCallback)
HRESULT CInArchive::ReadVols2(IArchiveOpenVolumeCallback *volCallback,
unsigned start, int lastDisk, int zipDisk, unsigned numMissingVolsMax, unsigned &numMissingVols)
{
if (Vols.DisableVolsSearch)
return S_OK;
numMissingVols = 0;
for (unsigned i = start;; i++)
@@ -2090,6 +2115,8 @@ HRESULT CInArchive::ReadVols2(IArchiveOpenVolumeCallback *volCallback,
}
if (res == S_FALSE || !stream)
{
if (i == 1 && Vols.StartIsExe)
return S_OK;
if (Vols.MissingName.IsEmpty())
Vols.MissingName = volName;
numMissingVols++;

View File

@@ -20,10 +20,13 @@ class CItemEx: public CItem
{
public:
UInt32 LocalFullHeaderSize; // including Name and Extra
// int ParentOfAltStream; // -1, if not AltStream
bool DescriptorWasRead;
CItemEx(): DescriptorWasRead(false) {}
CItemEx():
// ParentOfAltStream(-1),
DescriptorWasRead(false) {}
UInt64 GetLocalFullSize() const
{ return LocalFullHeaderSize + GetPackSizeWithDescriptor(); }
@@ -159,6 +162,7 @@ struct CVols
bool NeedSeek;
bool DisableVolsSearch;
bool StartIsExe; // is .exe
bool StartIsZ; // is .zip or .zNN
bool StartIsZip; // is .zip
@@ -198,6 +202,7 @@ struct CVols
StreamIndex = -1;
NeedSeek = false;
DisableVolsSearch = false;
StartIsExe = false;
StartIsZ = false;
StartIsZip = false;

View File

@@ -20,6 +20,12 @@ namespace NZip {
using namespace NFileHeader;
/*
const char *k_SpecName_NTFS_STREAM = "@@NTFS@STREAM@";
const char *k_SpecName_MAC_RESOURCE_FORK = "@@MAC@RESOURCE-FORK@";
*/
static const CUInt32PCharPair g_ExtraTypes[] =
{
{ NExtraID::kZip64, "Zip64" },

View File

@@ -14,6 +14,11 @@
namespace NArchive {
namespace NZip {
/*
extern const char *k_SpecName_NTFS_STREAM;
extern const char *k_SpecName_MAC_RESOURCE_FORK;
*/
struct CVersion
{
Byte Version;
@@ -233,6 +238,7 @@ public:
bool IsAesEncrypted() const { return IsEncrypted() && (IsStrongEncrypted() || Method == NFileHeader::NCompressionMethod::kWzAES); }
bool IsLzmaEOS() const { return (Flags & NFileHeader::NFlags::kLzmaEOS) != 0; }
bool HasDescriptor() const { return (Flags & NFileHeader::NFlags::kDescriptorUsedMask) != 0; }
// bool IsAltStream() const { return (Flags & NFileHeader::NFlags::kAltStream) != 0; }
unsigned GetDeflateLevel() const { return (Flags >> 1) & 3; }
@@ -264,6 +270,7 @@ public:
void ClearFlags() { Flags = 0; }
void SetEncrypted(bool encrypted) { SetFlag(NFileHeader::NFlags::kEncrypted, encrypted); }
void SetUtf8(bool isUtf8) { SetFlag(NFileHeader::NFlags::kUtf8, isUtf8); }
// void SetFlag_AltStream(bool isAltStream) { SetFlag(NFileHeader::NFlags::kAltStream, isAltStream); }
void SetDescriptorMode(bool useDescriptor) { SetFlag(NFileHeader::NFlags::kDescriptorUsedMask, useDescriptor); }
UINT GetCodePage() const { return CP_OEMCP; }

View File

@@ -75,6 +75,7 @@ static void SetFileHeader(
item.Name = ui.Name;
item.Comment = ui.Comment;
item.SetUtf8(ui.IsUtf8);
// item.SetFlag_AltStream(ui.IsAltStream);
item.ExternalAttrib = ui.Attrib;
item.Time = ui.Time;
item.Ntfs_MTime = ui.Ntfs_MTime;
@@ -280,6 +281,7 @@ public:
MY_UNKNOWN_IMP
void Create(IProgress *progress, bool inSizeIsMain);
void SetProgressOffset(UInt64 progressOffset);
void SetProgressOffset_NoLock(UInt64 progressOffset);
HRESULT SetRatioInfo(unsigned index, const UInt64 *inSize, const UInt64 *outSize);
STDMETHOD(SetRatioInfo)(const UInt64 *inSize, const UInt64 *outSize);
};
@@ -292,11 +294,16 @@ void CMtProgressMixer2::Create(IProgress *progress, bool inSizeIsMain)
ProgressOffset = InSizes[0] = InSizes[1] = OutSizes[0] = OutSizes[1] = 0;
}
void CMtProgressMixer2::SetProgressOffset_NoLock(UInt64 progressOffset)
{
InSizes[1] = OutSizes[1] = 0;
ProgressOffset = progressOffset;
}
void CMtProgressMixer2::SetProgressOffset(UInt64 progressOffset)
{
CriticalSection.Enter();
InSizes[1] = OutSizes[1] = 0;
ProgressOffset = progressOffset;
SetProgressOffset_NoLock(progressOffset);
CriticalSection.Leave();
}
@@ -384,6 +391,7 @@ static HRESULT UpdateItemOldData(
item.Comment = ui.Comment;
item.Name = ui.Name;
item.SetUtf8(ui.IsUtf8);
// item.SetFlag_AltStream(ui.IsAltStream);
item.Time = ui.Time;
item.Ntfs_MTime = ui.Ntfs_MTime;
item.Ntfs_ATime = ui.Ntfs_ATime;
@@ -602,8 +610,11 @@ static HRESULT Update2St(
lps->InSize = unpackSizeTotal;
lps->OutSize = packSizeTotal;
RINOK(lps->SetCur());
archive.WriteCentralDir(items, comment);
return S_OK;
lps->ProgressOffset += kCentralHeaderSize * updateItems.Size() + 1;
return lps->SetCur();
}
@@ -897,7 +908,7 @@ static HRESULT Update2(
{
complexity += ui.Size;
complexity += kLocalHeaderSize;
mtProgressMixerSpec->Mixer2->SetProgressOffset(complexity);
mtProgressMixerSpec->Mixer2->SetProgressOffset_NoLock(complexity);
RINOK(updateCallback->SetOperationResult(NArchive::NUpdate::NOperationResult::kOK));
memRef2.Skip = true;
continue;
@@ -1107,8 +1118,13 @@ static HRESULT Update2(
}
RINOK(mtCompressProgressMixer.SetRatioInfo(0, NULL, NULL));
archive.WriteCentralDir(items, comment);
return S_OK;
complexity += kCentralHeaderSize * updateItems.Size() + 1;
mtProgressMixerSpec->Mixer2->SetProgressOffset(complexity);
return mtCompressProgressMixer.SetRatioInfo(0, NULL, NULL);
#endif
}

View File

@@ -32,6 +32,7 @@ struct CUpdateItem
bool IsDir;
bool NtfsTimeIsDefined;
bool IsUtf8;
// bool IsAltStream;
int IndexInArc;
int IndexInClient;
UInt32 Attrib;
@@ -50,12 +51,19 @@ struct CUpdateItem
IsDir = false;
NtfsTimeIsDefined = false;
IsUtf8 = false;
// IsAltStream = false;
Size = 0;
Name.Empty();
Comment.Free();
}
CUpdateItem(): NtfsTimeIsDefined(false), IsUtf8(false), Size(0) {}
CUpdateItem():
IsDir(false),
NtfsTimeIsDefined(false),
IsUtf8(false),
// IsAltStream(false),
Size(0)
{}
};
HRESULT Update(

View File

@@ -1112,18 +1112,6 @@ SOURCE=..\..\Compress\ShrinkDecoder.cpp
SOURCE=..\..\Compress\ShrinkDecoder.h
# End Source File
# End Group
# Begin Group "Z"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\Compress\ZDecoder.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Compress\ZDecoder.h
# End Source File
# End Group
# Begin Group "BWT"
# PROP Default_Filter ""
@@ -1652,10 +1640,6 @@ SOURCE=..\..\Archive\SplitHandler.cpp
SOURCE=..\..\Archive\XzHandler.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Archive\ZHandler.cpp
# End Source File
# End Group
# Begin Group "UI Common"
@@ -2636,6 +2620,34 @@ SOURCE=..\..\..\..\C\Lzma2Dec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.c
!IF "$(CFG)" == "Alone - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Alone - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Alone - Win32 ReleaseU"
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Alone - Win32 DebugU"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2Enc.c
!IF "$(CFG)" == "Alone - Win32 Release"
@@ -2748,6 +2760,34 @@ SOURCE=..\..\..\..\C\MtCoder.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.c
!IF "$(CFG)" == "Alone - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Alone - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Alone - Win32 ReleaseU"
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Alone - Win32 DebugU"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Ppmd.h
# End Source File
# Begin Source File

View File

@@ -76,7 +76,6 @@ AR_OBJS = \
$O\LzmaHandler.obj \
$O\SplitHandler.obj \
$O\XzHandler.obj \
$O\ZHandler.obj \
AR_COMMON_OBJS = \
$O\CoderMixer2.obj \
@@ -170,7 +169,6 @@ COMPRESS_OBJS = \
$O\ShrinkDecoder.obj \
$O\XzDecoder.obj \
$O\XzEncoder.obj \
$O\ZDecoder.obj \
CRYPTO_OBJS = \
$O\7zAes.obj \
@@ -199,10 +197,12 @@ C_OBJS = \
$O\LzFind.obj \
$O\LzFindMt.obj \
$O\Lzma2Dec.obj \
$O\Lzma2DecMt.obj \
$O\Lzma2Enc.obj \
$O\LzmaDec.obj \
$O\LzmaEnc.obj \
$O\MtCoder.obj \
$O\MtDec.obj \
$O\Ppmd7.obj \
$O\Ppmd7Dec.obj \
$O\Ppmd7Enc.obj \
@@ -324,5 +324,6 @@ ZSTDMT_OBJS = \
!include "../../Aes.mak"
!include "../../Crc.mak"
!include "../../Crc64.mak"
!include "../../LzmaDec.mak"
!include "../../7zip.mak"

View File

@@ -1,3 +1,7 @@
#include "../../MyVersionInfo.rc"
MY_VERSION_INFO_APP("7-Zip Standalone Console", "7za")
#ifndef UNDER_CE
1 24 MOVEABLE PURE "../../UI/Console/Console.manifest"
#endif

View File

@@ -44,7 +44,7 @@ RSC=rc.exe
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /Gr /MT /W3 /GX /O1 /I "..\..\..\\" /D "NDEBUG" /D "_MBCS" /D "WIN32" /D "_CONSOLE" /FAc /Yu"StdAfx.h" /FD /c
# ADD CPP /nologo /Gr /MT /W3 /GX /O1 /I "..\..\..\\" /D "NDEBUG" /D "_MBCS" /D "WIN32" /D "_CONSOLE" /D "_7ZIP_LARGE_PAGES" /D "SUPPORT_DEVICE_FILE" /FAc /Yu"StdAfx.h" /FD /c
# ADD BASE RSC /l 0x419 /d "NDEBUG"
# ADD RSC /l 0x419 /d "NDEBUG"
BSC32=bscmake.exe
@@ -69,7 +69,7 @@ LINK32=link.exe
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /Gr /MDd /W3 /Gm /GX /ZI /Od /I "..\..\..\\" /D "_DEBUG" /D "_MBCS" /D "WIN32" /D "_CONSOLE" /Yu"StdAfx.h" /FD /GZ /c
# ADD CPP /nologo /Gr /MDd /W3 /Gm /GX /ZI /Od /I "..\..\..\\" /D "_DEBUG" /D "_MBCS" /D "WIN32" /D "_CONSOLE" /D "_7ZIP_LARGE_PAGES" /D "SUPPORT_DEVICE_FILE" /Yu"StdAfx.h" /FD /GZ /c
# ADD BASE RSC /l 0x419 /d "_DEBUG"
# ADD RSC /l 0x419 /d "_DEBUG"
BSC32=bscmake.exe
@@ -94,7 +94,7 @@ LINK32=link.exe
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MD /W3 /GX /O2 /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "EXCLUDE_COM" /D "NO_REGISTRY" /Yu"StdAfx.h" /FD /c
# ADD CPP /nologo /Gr /MD /W4 /GX /O1 /I "..\..\..\\" /D "NDEBUG" /D "UNICODE" /D "_UNICODE" /D "WIN32" /D "_CONSOLE" /Yu"StdAfx.h" /FD /c
# ADD CPP /nologo /Gr /MD /W4 /GX /O1 /I "..\..\..\\" /D "NDEBUG" /D "UNICODE" /D "_UNICODE" /D "WIN32" /D "_CONSOLE" /D "_7ZIP_LARGE_PAGES" /D "SUPPORT_DEVICE_FILE" /Yu"StdAfx.h" /FD /c
# ADD BASE RSC /l 0x419 /d "NDEBUG"
# ADD RSC /l 0x419 /d "NDEBUG"
BSC32=bscmake.exe
@@ -121,7 +121,7 @@ LINK32=link.exe
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "_DEBUG" /D "WIN32" /D "_CONSOLE" /D "EXCLUDE_COM" /D "NO_REGISTRY" /D "_MBCS" /Yu"StdAfx.h" /FD /GZ /c
# ADD CPP /nologo /Gr /MDd /W4 /Gm /GX /ZI /Od /I "..\..\..\\" /D "_DEBUG" /D "_UNICODE" /D "UNICODE" /D "WIN32" /D "_CONSOLE" /Yu"StdAfx.h" /FD /GZ /c
# ADD CPP /nologo /Gr /MDd /W4 /Gm /GX /ZI /Od /I "..\..\..\\" /D "_DEBUG" /D "_UNICODE" /D "UNICODE" /D "WIN32" /D "_CONSOLE" /D "_7ZIP_LARGE_PAGES" /D "SUPPORT_DEVICE_FILE" /Yu"StdAfx.h" /FD /GZ /c
# ADD BASE RSC /l 0x419 /d "_DEBUG"
# ADD RSC /l 0x419 /d "_DEBUG"
BSC32=bscmake.exe
@@ -482,6 +482,14 @@ SOURCE=..\..\..\Windows\FileName.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\FileSystem.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\FileSystem.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\Handle.h
# End Source File
# Begin Source File
@@ -1696,6 +1704,34 @@ SOURCE=..\..\..\..\C\Lzma2Dec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.c
!IF "$(CFG)" == "Alone - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Alone - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Alone - Win32 ReleaseU"
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Alone - Win32 DebugU"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2Enc.c
!IF "$(CFG)" == "Alone - Win32 Release"
@@ -1789,6 +1825,34 @@ SOURCE=..\..\..\..\C\MtCoder.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.c
!IF "$(CFG)" == "Alone - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Alone - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Alone - Win32 ReleaseU"
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "Alone - Win32 DebugU"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Threads.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File

View File

@@ -3,7 +3,7 @@ MY_CONSOLE = 1
CFLAGS = $(CFLAGS) -DPROG_VARIANT_R
!IFNDEF UNDER_CE
CFLAGS = $(CFLAGS) -DWIN_LONG_PATH
CFLAGS = $(CFLAGS) -DWIN_LONG_PATH -D_7ZIP_LARGE_PAGES -DSUPPORT_DEVICE_FILE
!ENDIF
COMMON_OBJS = \
@@ -33,6 +33,7 @@ WIN_OBJS = \
$O\FileIO.obj \
$O\FileLink.obj \
$O\FileName.obj \
$O\FileSystem.obj \
$O\MemoryLock.obj \
$O\PropVariant.obj \
$O\PropVariantConv.obj \
@@ -133,10 +134,12 @@ C_OBJS = \
$O\LzFind.obj \
$O\LzFindMt.obj \
$O\Lzma2Dec.obj \
$O\Lzma2DecMt.obj \
$O\Lzma2Enc.obj \
$O\LzmaDec.obj \
$O\LzmaEnc.obj \
$O\MtCoder.obj \
$O\MtDec.obj \
$O\Sha256.obj \
$O\Sort.obj \
$O\Threads.obj \
@@ -150,5 +153,6 @@ C_OBJS = \
!include "../../Aes.mak"
!include "../../Crc.mak"
!include "../../Crc64.mak"
!include "../../LzmaDec.mak"
!include "../../7zip.mak"

View File

@@ -1,3 +1,7 @@
#include "../../../../C/7zVersion.rc"
MY_VERSION_INFO_APP("7-Zip Reduced Standalone Console", "7zr")
#ifndef UNDER_CE
1 24 MOVEABLE PURE "../../UI/Console/Console.manifest"
#endif

View File

@@ -677,6 +677,14 @@ SOURCE=..\..\UI\FileManager\DialogSize.h
# End Source File
# Begin Source File
SOURCE=..\..\UI\FileManager\EditDialog.cpp
# End Source File
# Begin Source File
SOURCE=..\..\UI\FileManager\EditDialog.h
# End Source File
# Begin Source File
SOURCE=..\..\UI\FileManager\LinkDialog.cpp
# End Source File
# Begin Source File
@@ -1004,6 +1012,15 @@ SOURCE=..\..\..\..\C\Lzma2Dec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2Enc.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
@@ -1040,6 +1057,15 @@ SOURCE=..\..\..\..\C\MtCoder.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Sha256.c
!IF "$(CFG)" == "FM - Win32 Release"
@@ -1549,14 +1575,6 @@ SOURCE=..\..\..\Common\Wildcard.h
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\UI\Common\ArchiveCommandLine.cpp
# End Source File
# Begin Source File
SOURCE=..\..\UI\Common\ArchiveCommandLine.h
# End Source File
# Begin Source File
SOURCE=..\..\UI\Common\ArchiveExtractCallback.cpp
# End Source File
# Begin Source File

View File

@@ -48,7 +48,6 @@ WIN_CTRL_OBJS = \
$O\FileStreams.obj \
UI_COMMON_OBJS = \
$O\ArchiveCommandLine.obj \
$O\ArchiveExtractCallback.obj \
$O\ArchiveName.obj \
$O\ArchiveOpenCallback.obj \

View File

@@ -122,10 +122,12 @@ C_OBJS = \
$O\LzFind.obj \
$O\LzFindMt.obj \
$O\Lzma2Dec.obj \
$O\Lzma2DecMt.obj \
$O\Lzma2Enc.obj \
$O\LzmaDec.obj \
$O\LzmaEnc.obj \
$O\MtCoder.obj \
$O\MtDec.obj \
$O\Ppmd7.obj \
$O\Ppmd7Dec.obj \
$O\Ppmd7Enc.obj \
@@ -135,6 +137,7 @@ C_OBJS = \
!include "../../Aes.mak"
!include "../../Crc.mak"
!include "../../LzmaDec.mak"
COMPRESS_OBJS = $(COMPRESS_OBJS) \
$O\BrotliDecoder.obj \

View File

@@ -43,6 +43,7 @@ AR_OBJS = \
AR_COMMON_OBJS = \
$O\CoderMixer2.obj \
$O\HandlerOut.obj \
$O\ItemNameUtils.obj \
$O\OutStreamWithCRC.obj \
$O\ParseProperties.obj \
@@ -99,7 +100,9 @@ C_OBJS = \
$O\CpuArch.obj \
$O\Delta.obj \
$O\Lzma2Dec.obj \
$O\Lzma2DecMt.obj \
$O\LzmaDec.obj \
$O\MtDec.obj \
$O\Ppmd7.obj \
$O\Ppmd7Dec.obj \
$O\Sha256.obj \
@@ -198,5 +201,6 @@ ZSTDMT_OBJS = \
!include "../../Aes.mak"
!include "../../Crc.mak"
!include "../../LzmaDec.mak"
!include "../../7zip.mak"

View File

@@ -85,9 +85,12 @@ C_OBJS = \
$O\CpuArch.obj \
$O\Delta.obj \
$O\Lzma2Dec.obj \
$O\Lzma2DecMt.obj \
$O\LzmaDec.obj \
$O\MtDec.obj \
$O\Threads.obj \
!include "../../Crc.mak"
!include "../../LzmaDec.mak"
!include "../../7zip.mak"

View File

@@ -202,6 +202,7 @@ COMPRESS_OBJS = \
$O\DeflateRegister.obj \
$O\DeltaFilter.obj \
$O\ImplodeDecoder.obj \
$O\LzfseDecoder.obj \
$O\LzhDecoder.obj \
$O\Lzma2Decoder.obj \
$O\Lzma2Encoder.obj \
@@ -265,10 +266,12 @@ C_OBJS = \
$O\LzFind.obj \
$O\LzFindMt.obj \
$O\Lzma2Dec.obj \
$O\Lzma2DecMt.obj \
$O\Lzma2Enc.obj \
$O\LzmaDec.obj \
$O\LzmaEnc.obj \
$O\MtCoder.obj \
$O\MtDec.obj \
$O\Ppmd7.obj \
$O\Ppmd7Dec.obj \
$O\Ppmd7Enc.obj \
@@ -287,3 +290,4 @@ C_OBJS = \
!include "../../Aes.mak"
!include "../../Crc.mak"
!include "../../Crc64.mak"
!include "../../LzmaDec.mak"

View File

@@ -961,6 +961,24 @@ SOURCE=..\..\Compress\HuffmanDecoder.h
# End Source File
# Begin Source File
SOURCE=..\..\Compress\LzfseDecoder.cpp
!IF "$(CFG)" == "7z - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "7z - Win32 Debug"
!ENDIF
# End Source File
# Begin Source File
SOURCE=..\..\Compress\LzfseDecoder.h
# End Source File
# Begin Source File
SOURCE=..\..\Compress\LzhDecoder.cpp
# End Source File
# Begin Source File
@@ -1787,6 +1805,26 @@ SOURCE=..\..\..\..\C\Lzma2Dec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.c
!IF "$(CFG)" == "7z - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "7z - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2Enc.c
!IF "$(CFG)" == "7z - Win32 Release"
@@ -1856,6 +1894,26 @@ SOURCE=..\..\..\..\C\MtCoder.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.c
!IF "$(CFG)" == "7z - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "7z - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Ppmd.h
# End Source File
# Begin Source File

View File

@@ -102,12 +102,15 @@ C_OBJS = \
$O\LzFind.obj \
$O\LzFindMt.obj \
$O\Lzma2Dec.obj \
$O\Lzma2DecMt.obj \
$O\Lzma2Enc.obj \
$O\LzmaDec.obj \
$O\LzmaEnc.obj \
$O\MtCoder.obj \
$O\MtDec.obj \
$O\Threads.obj \
!include "../../Crc.mak"
!include "../../LzmaDec.mak"
!include "../../7zip.mak"

View File

@@ -117,6 +117,10 @@ SOURCE=..\..\Archive\Common\CoderMixer2.h
# End Source File
# Begin Source File
SOURCE=..\..\Archive\Common\HandlerOut.h
# End Source File
# Begin Source File
SOURCE=..\..\Archive\Common\ItemNameUtils.cpp
# End Source File
# Begin Source File
@@ -293,6 +297,10 @@ SOURCE=..\..\Compress\Lzma2Decoder.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Compress\Lzma2Decoder.h
# End Source File
# Begin Source File
SOURCE=..\..\Compress\Lzma2Register.cpp
# End Source File
# Begin Source File
@@ -411,6 +419,14 @@ SOURCE=..\..\..\Windows\Synchronization.cpp
SOURCE=..\..\..\Windows\Synchronization.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\System.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\System.h
# End Source File
# End Group
# Begin Group "Common"
@@ -822,6 +838,15 @@ SOURCE=..\..\..\..\C\Lzma2Dec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\LzmaDec.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
@@ -831,6 +856,15 @@ SOURCE=..\..\..\..\C\LzmaDec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Ppmd7.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File

View File

@@ -1,5 +1,7 @@
PROG = 7zCon.sfx
MY_CONSOLE = 1
MY_FIXED = 1
CFLAGS = $(CFLAGS) \
-DEXTRACT_ONLY \
-DNO_READ_FROM_CODER \
@@ -118,7 +120,9 @@ C_OBJS = \
$O\Delta.obj \
$O\DllSecur.obj \
$O\Lzma2Dec.obj \
$O\Lzma2DecMt.obj \
$O\LzmaDec.obj \
$O\MtDec.obj \
$O\Ppmd7.obj \
$O\Ppmd7Dec.obj \
$O\Sha256.obj \
@@ -126,6 +130,7 @@ C_OBJS = \
!include "../../Aes.mak"
!include "../../Crc.mak"
!include "../../LzmaDec.mak"
COMPRESS_OBJS = $(COMPRESS_OBJS) \
$O\ZstdDecoder.obj \

View File

@@ -449,6 +449,14 @@ SOURCE=..\..\..\Windows\Synchronization.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\System.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\System.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\Window.cpp
# End Source File
# Begin Source File
@@ -469,6 +477,14 @@ SOURCE=..\..\Common\CreateCoder.h
# End Source File
# Begin Source File
SOURCE=..\..\Common\CWrappers.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Common\CWrappers.h
# End Source File
# Begin Source File
SOURCE=..\..\Common\FileStreams.cpp
# End Source File
# Begin Source File
@@ -724,6 +740,15 @@ SOURCE=..\..\..\..\C\Lzma2Dec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\LzmaDec.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
@@ -733,6 +758,15 @@ SOURCE=..\..\..\..\C\LzmaDec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Threads.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File

View File

@@ -1,4 +1,6 @@
PROG = 7zS.sfx
MY_FIXED = 1
CFLAGS = $(CFLAGS) \
-DNO_REGISTRY \
-DEXTRACT_ONLY \
@@ -33,6 +35,7 @@ WIN_OBJS = \
$O\PropVariant.obj \
$O\ResourceString.obj \
$O\Synchronization.obj \
$O\System.obj \
$O\Window.obj \
WIN_CTRL_OBJS = \
@@ -40,6 +43,7 @@ WIN_CTRL_OBJS = \
7ZIP_COMMON_OBJS = \
$O\CreateCoder.obj \
$O\CWrappers.obj \
$O\FileStreams.obj \
$O\InBuffer.obj \
$O\FilterCoder.obj \
@@ -102,8 +106,11 @@ C_OBJS = \
$O\Delta.obj \
$O\DllSecur.obj \
$O\Lzma2Dec.obj \
$O\Lzma2DecMt.obj \
$O\LzmaDec.obj \
$O\MtDec.obj \
$O\Threads.obj \
!include "../../Crc.mak"
!include "../../7zip.mak"
!include "../../LzmaDec.mak"
!include "../../7zip.mak"

View File

@@ -633,6 +633,14 @@ SOURCE=..\..\..\Windows\Synchronization.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\System.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\System.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Windows\Window.cpp
# End Source File
# Begin Source File
@@ -906,6 +914,15 @@ SOURCE=..\..\..\..\C\Lzma2Dec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Lzma2DecMt.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\LzmaDec.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
@@ -915,6 +932,15 @@ SOURCE=..\..\..\..\C\LzmaDec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\MtDec.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\C\Ppmd7.c
# SUBTRACT CPP /YX /Yc /Yu
# End Source File

View File

@@ -1,4 +1,6 @@
PROG = 7z.sfx
MY_FIXED = 1
CFLAGS = $(CFLAGS) \
-DNO_REGISTRY \
-DEXTRACT_ONLY \
@@ -42,6 +44,7 @@ WIN_OBJS = \
$O\Shell.obj \
$O\System.obj \
$O\Synchronization.obj \
$O\System.obj \
$O\Window.obj \
WIN_CTRL_OBJS = \
@@ -136,7 +139,9 @@ C_OBJS = \
$O\Delta.obj \
$O\DllSecur.obj \
$O\Lzma2Dec.obj \
$O\Lzma2DecMt.obj \
$O\LzmaDec.obj \
$O\MtDec.obj \
$O\Ppmd7.obj \
$O\Ppmd7Dec.obj \
$O\Sha256.obj \
@@ -144,6 +149,7 @@ C_OBJS = \
!include "../../Aes.mak"
!include "../../Crc.mak"
!include "../../LzmaDec.mak"
COMPRESS_OBJS = $(COMPRESS_OBJS) \
$O\ZstdDecoder.obj \

View File

@@ -28,14 +28,23 @@ HRESULT SResToHRESULT(SRes res) throw()
switch (res)
{
case SZ_OK: return S_OK;
case SZ_ERROR_DATA: return S_FALSE;
case SZ_ERROR_CRC: return S_FALSE;
case SZ_ERROR_DATA:
case SZ_ERROR_CRC:
case SZ_ERROR_INPUT_EOF:
return S_FALSE;
case SZ_ERROR_MEM: return E_OUTOFMEMORY;
case SZ_ERROR_PARAM: return E_INVALIDARG;
case SZ_ERROR_PROGRESS: return E_ABORT;
case SZ_ERROR_UNSUPPORTED: return E_NOTIMPL;
// case SZ_ERROR_THREAD: return E_FAIL;
// case SZ_ERROR_READ: return E_FAIL;
// case SZ_ERROR_OUTPUT_EOF:
// case SZ_ERROR_READ:
// case SZ_ERROR_WRITE:
// case SZ_ERROR_THREAD:
// case SZ_ERROR_ARCHIVE:
// case SZ_ERROR_NO_ARCHIVE:
// return E_FAIL;
}
if (res < 0)
return res;

View File

@@ -148,20 +148,23 @@ HRESULT CExternalCodecs::Load()
#endif
bool FindMethod(
int FindMethod_Index(
DECL_EXTERNAL_CODECS_LOC_VARS
const AString &name,
CMethodId &methodId, UInt32 &numStreams)
bool encode,
CMethodId &methodId,
UInt32 &numStreams)
{
unsigned i;
for (i = 0; i < g_NumCodecs; i++)
{
const CCodecInfo &codec = *g_Codecs[i];
if (StringsAreEqualNoCase_Ascii(name, codec.Name))
if ((encode ? codec.CreateEncoder : codec.CreateDecoder)
&& StringsAreEqualNoCase_Ascii(name, codec.Name))
{
methodId = codec.Id;
numStreams = codec.NumStreams;
return true;
return i;
}
}
@@ -173,19 +176,51 @@ bool FindMethod(
for (i = 0; i < __externalCodecs->Codecs.Size(); i++)
{
const CCodecInfoEx &codec = __externalCodecs->Codecs[i];
if (StringsAreEqualNoCase_Ascii(name, codec.Name))
if ((encode ? codec.EncoderIsAssigned : codec.DecoderIsAssigned)
&& StringsAreEqualNoCase_Ascii(name, codec.Name))
{
methodId = codec.Id;
numStreams = codec.NumStreams;
return true;
return g_NumCodecs + i;
}
}
#endif
return false;
return -1;
}
static int FindMethod_Index(
DECL_EXTERNAL_CODECS_LOC_VARS
CMethodId methodId, bool encode)
{
unsigned i;
for (i = 0; i < g_NumCodecs; i++)
{
const CCodecInfo &codec = *g_Codecs[i];
if (codec.Id == methodId && (encode ? codec.CreateEncoder : codec.CreateDecoder))
return i;
}
#ifdef EXTERNAL_CODECS
CHECK_GLOBAL_CODECS
if (__externalCodecs)
for (i = 0; i < __externalCodecs->Codecs.Size(); i++)
{
const CCodecInfoEx &codec = __externalCodecs->Codecs[i];
if (codec.Id == methodId && (encode ? codec.EncoderIsAssigned : codec.DecoderIsAssigned))
return g_NumCodecs + i;
}
#endif
return -1;
}
bool FindMethod(
DECL_EXTERNAL_CODECS_LOC_VARS
CMethodId methodId,
@@ -280,9 +315,11 @@ void GetHashMethods(
#endif
}
HRESULT CreateCoder(
HRESULT CreateCoder_Index(
DECL_EXTERNAL_CODECS_LOC_VARS
CMethodId methodId, bool encode,
unsigned i, bool encode,
CMyComPtr<ICompressFilter> &filter,
CCreatedCoder &cod)
{
@@ -290,11 +327,10 @@ HRESULT CreateCoder(
cod.IsFilter = false;
cod.NumStreams = 1;
unsigned i;
for (i = 0; i < g_NumCodecs; i++)
if (i < g_NumCodecs)
{
const CCodecInfo &codec = *g_Codecs[i];
if (codec.Id == methodId)
// if (codec.Id == methodId)
{
if (encode)
{
@@ -325,11 +361,12 @@ HRESULT CreateCoder(
if (__externalCodecs)
{
i -= g_NumCodecs;
cod.IsExternal = true;
for (i = 0; i < __externalCodecs->Codecs.Size(); i++)
if (i < __externalCodecs->Codecs.Size())
{
const CCodecInfoEx &codec = __externalCodecs->Codecs[i];
if (codec.Id == methodId)
// if (codec.Id == methodId)
{
if (encode)
{
@@ -371,13 +408,50 @@ HRESULT CreateCoder(
return S_OK;
}
HRESULT CreateCoder(
HRESULT CreateCoder_Index(
DECL_EXTERNAL_CODECS_LOC_VARS
unsigned index, bool encode,
CCreatedCoder &cod)
{
CMyComPtr<ICompressFilter> filter;
HRESULT res = CreateCoder_Index(
EXTERNAL_CODECS_LOC_VARS
index, encode,
filter, cod);
if (filter)
{
cod.IsFilter = true;
CFilterCoder *coderSpec = new CFilterCoder(encode);
cod.Coder = coderSpec;
coderSpec->Filter = filter;
}
return res;
}
HRESULT CreateCoder_Id(
DECL_EXTERNAL_CODECS_LOC_VARS
CMethodId methodId, bool encode,
CMyComPtr<ICompressFilter> &filter,
CCreatedCoder &cod)
{
int index = FindMethod_Index(EXTERNAL_CODECS_LOC_VARS methodId, encode);
if (index < 0)
return S_OK;
return CreateCoder_Index(EXTERNAL_CODECS_LOC_VARS index, encode, filter, cod);
}
HRESULT CreateCoder_Id(
DECL_EXTERNAL_CODECS_LOC_VARS
CMethodId methodId, bool encode,
CCreatedCoder &cod)
{
CMyComPtr<ICompressFilter> filter;
HRESULT res = CreateCoder(
HRESULT res = CreateCoder_Id(
EXTERNAL_CODECS_LOC_VARS
methodId, encode,
filter, cod);
@@ -393,13 +467,14 @@ HRESULT CreateCoder(
return res;
}
HRESULT CreateCoder(
HRESULT CreateCoder_Id(
DECL_EXTERNAL_CODECS_LOC_VARS
CMethodId methodId, bool encode,
CMyComPtr<ICompressCoder> &coder)
{
CCreatedCoder cod;
HRESULT res = CreateCoder(
HRESULT res = CreateCoder_Id(
EXTERNAL_CODECS_LOC_VARS
methodId, encode,
cod);
@@ -413,7 +488,7 @@ HRESULT CreateFilter(
CMyComPtr<ICompressFilter> &filter)
{
CCreatedCoder cod;
return CreateCoder(
return CreateCoder_Id(
EXTERNAL_CODECS_LOC_VARS
methodId, encode,
filter, cod);

View File

@@ -116,13 +116,12 @@ extern CExternalCodecs g_ExternalCodecs;
#endif
bool FindMethod(
int FindMethod_Index(
DECL_EXTERNAL_CODECS_LOC_VARS
const AString &name,
CMethodId &methodId, UInt32 &numStreams);
bool encode,
CMethodId &methodId,
UInt32 &numStreams);
bool FindMethod(
DECL_EXTERNAL_CODECS_LOC_VARS
@@ -152,18 +151,29 @@ struct CCreatedCoder
};
HRESULT CreateCoder(
HRESULT CreateCoder_Index(
DECL_EXTERNAL_CODECS_LOC_VARS
unsigned codecIndex, bool encode,
CMyComPtr<ICompressFilter> &filter,
CCreatedCoder &cod);
HRESULT CreateCoder_Index(
DECL_EXTERNAL_CODECS_LOC_VARS
unsigned index, bool encode,
CCreatedCoder &cod);
HRESULT CreateCoder_Id(
DECL_EXTERNAL_CODECS_LOC_VARS
CMethodId methodId, bool encode,
CMyComPtr<ICompressFilter> &filter,
CCreatedCoder &cod);
HRESULT CreateCoder(
HRESULT CreateCoder_Id(
DECL_EXTERNAL_CODECS_LOC_VARS
CMethodId methodId, bool encode,
CCreatedCoder &cod);
HRESULT CreateCoder(
HRESULT CreateCoder_Id(
DECL_EXTERNAL_CODECS_LOC_VARS
CMethodId methodId, bool encode,
CMyComPtr<ICompressCoder> &coder);

View File

@@ -97,6 +97,33 @@ Byte CInBufferBase::ReadByte_FromNewBlock()
size_t CInBufferBase::ReadBytes(Byte *buf, size_t size)
{
size_t num = 0;
for (;;)
{
const size_t rem = _bufLim - _buf;
if (size <= rem)
{
if (size != 0)
{
memcpy(buf, _buf, size);
_buf += size;
num += size;
}
return num;
}
if (rem != 0)
{
memcpy(buf, _buf, rem);
_buf += rem;
buf += rem;
num += rem;
size -= rem;
}
if (!ReadBlock())
return num;
}
/*
if ((size_t)(_bufLim - _buf) >= size)
{
const Byte *src = _buf;
@@ -113,6 +140,7 @@ size_t CInBufferBase::ReadBytes(Byte *buf, size_t size)
buf[i] = *_buf++;
}
return size;
*/
}
size_t CInBufferBase::Skip(size_t size)

View File

@@ -253,6 +253,9 @@ struct CNameToPropID
const char *Name;
};
// the following are related to NCoderPropID::EEnum values
static const CNameToPropID g_NameToPropID[] =
{
{ VT_UI4, "" },
@@ -275,7 +278,8 @@ static const CNameToPropID g_NameToPropID[] =
{ VT_UI8, "expect" },
{ VT_UI4, "b" },
{ VT_UI4, "check" },
{ VT_BSTR, "filter" }
{ VT_BSTR, "filter" },
{ VT_UI8, "memuse" }
};
static int FindPropIdExact(const UString &name)
@@ -293,6 +297,13 @@ static bool ConvertProperty(const PROPVARIANT &srcProp, VARTYPE varType, NCOM::C
destProp = srcProp;
return true;
}
if (varType == VT_UI8 && srcProp.vt == VT_UI4)
{
destProp = (UInt64)srcProp.ulVal;
return true;
}
if (varType == VT_BOOL)
{
bool res;

View File

@@ -50,6 +50,17 @@ const UInt32 kNumSelectorsMax = (2 + (kBlockSizeMax / kGroupSize));
const unsigned kRleModeRepSize = 4;
/*
The number of selectors stored in bzip2 block:
(numSelectors <= 18001) - must work with any decoder.
(numSelectors == 18002) - works with bzip2 1.0.6 decoder and all derived decoders.
(numSelectors > 18002)
7-Zip decoder doesn't support it.
bzip2 1.0.6 decoder can overflow selector[18002] arrays. But there are another
arrays after selector arrays. So the compiled code works.
lbzip2 2.5 encoder can write up to (18001 + 7) selectors.
*/
}}
#endif

View File

@@ -411,9 +411,13 @@ SRes CBase::ReadBlock2()
lens[state4] = (Byte)state3;
state5 = 0;
}
// lbzip2 2.5 can produce dummy tree, where lens[i] = kMaxHuffmanLen
// BuildFull() returns error for such tree
/*
for (unsigned i = state4; i < kMaxAlphaSize; i++)
lens[i] = 0;
if (!huffs[state2].Build(lens))
*/
if (!huffs[state2].BuildFull(lens, state4))
return SZ_ERROR_DATA;
@@ -474,7 +478,7 @@ SRes CBase::ReadBlock2()
for (len = kNumTableBits + 1; val >= huff->_limits[len]; len++);
/*
if (len > kNumBitsMax)
return SZ_ERROR_DATA;
return SZ_ERROR_DATA; // that check is required, if NHuffman::Build() was used instead of BuildFull()
*/
if (_numBits < len)
{

View File

@@ -969,6 +969,10 @@ HRESULT CCoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *ou
}
}
while (Inline_MatchFinder_GetNumAvailableBytes(&_lzInWindow) != 0);
if (_seqInStream.Res != S_OK)
return _seqInStream.Res;
if (_lzInWindow.result != SZ_OK)
return SResToHRESULT(_lzInWindow.result);
return m_OutStream.Flush();

View File

@@ -56,6 +56,39 @@ public:
if (pos == _limitPos)
FlushWithCheck();
}
void PutBytes(const Byte *data, UInt32 size)
{
if (size == 0)
return;
UInt32 pos = _pos;
Byte *buf = _buf;
buf[pos++] = *data++;
size--;
for (;;)
{
UInt32 limitPos = _limitPos;
UInt32 rem = limitPos - pos;
if (rem == 0)
{
_pos = pos;
FlushWithCheck();
pos = _pos;
continue;
}
if (size == 0)
break;
if (rem > size)
rem = size;
size -= rem;
do
buf[pos++] = *data++;
while (--rem);
}
_pos = pos;
}
Byte GetByte(UInt32 distance) const
{

View File

@@ -0,0 +1,925 @@
// LzfseDecoder.cpp
/*
This code implements LZFSE data decompressing.
The code from "LZFSE compression library" was used.
2018 : Igor Pavlov : BSD 3-clause License : the code in this file
2015-2017 : Apple Inc : BSD 3-clause License : original "LZFSE compression library" code
The code in the "LZFSE compression library" is licensed under the "BSD 3-clause License":
----
Copyright (c) 2015-2016, Apple Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----
*/
#include "StdAfx.h"
// #define SHOW_DEBUG_INFO
#ifdef SHOW_DEBUG_INFO
#include <stdio.h>
#endif
#ifdef SHOW_DEBUG_INFO
#define PRF(x) x
#else
#define PRF(x)
#endif
#include "../../../C/CpuArch.h"
#include "LzfseDecoder.h"
namespace NCompress {
namespace NLzfse {
static const Byte kSignature_LZFSE_V1 = 0x31; // '1'
static const Byte kSignature_LZFSE_V2 = 0x32; // '2'
HRESULT CDecoder::GetUInt32(UInt32 &val)
{
Byte b[4];
for (unsigned i = 0; i < 4; i++)
if (!m_InStream.ReadByte(b[i]))
return S_FALSE;
val = GetUi32(b);
return S_OK;
}
HRESULT CDecoder::DecodeUncompressed(UInt32 unpackSize)
{
PRF(printf("\nUncompressed %7u\n", unpackSize));
const unsigned kBufSize = 1 << 8;
Byte buf[kBufSize];
for (;;)
{
if (unpackSize == 0)
return S_OK;
UInt32 cur = unpackSize;
if (cur > kBufSize)
cur = kBufSize;
UInt32 cur2 = (UInt32)m_InStream.ReadBytes(buf, cur);
m_OutWindowStream.PutBytes(buf, cur2);
if (cur != cur2)
return S_FALSE;
}
}
HRESULT CDecoder::DecodeLzvn(UInt32 unpackSize)
{
UInt32 packSize;
RINOK(GetUInt32(packSize));
PRF(printf("\nLZVN %7u %7u", unpackSize, packSize));
UInt32 D = 0;
for (;;)
{
if (packSize == 0)
return S_FALSE;
Byte b;
if (!m_InStream.ReadByte(b))
return S_FALSE;
packSize--;
UInt32 M;
UInt32 L;
if (b >= 0xE0)
{
/*
large L - 11100000 LLLLLLLL <LITERALS>
small L - 1110LLLL <LITERALS>
large Rep - 11110000 MMMMMMMM
small Rep - 1111MMMM
*/
M = b & 0xF;
if (M == 0)
{
if (packSize == 0)
return S_FALSE;
Byte b1;
if (!m_InStream.ReadByte(b1))
return S_FALSE;
packSize--;
M = (UInt32)b1 + 16;
}
L = 0;
if ((b & 0x10) == 0)
{
// Literals only
L = M;
M = 0;
}
}
// ERROR codes
else if ((b & 0xF0) == 0x70) // 0111xxxx
return S_FALSE;
else if ((b & 0xF0) == 0xD0) // 1101xxxx
return S_FALSE;
else
{
if ((b & 0xE0) == 0xA0)
{
// medium - 101LLMMM DDDDDDMM DDDDDDDD <LITERALS>
if (packSize < 2)
return S_FALSE;
Byte b1;
if (!m_InStream.ReadByte(b1))
return S_FALSE;
packSize--;
Byte b2;
if (!m_InStream.ReadByte(b2))
return S_FALSE;
packSize--;
L = (((UInt32)b >> 3) & 3);
M = (((UInt32)b & 7) << 2) + (b1 & 3);
D = ((UInt32)b1 >> 2) + ((UInt32)b2 << 6);
}
else
{
L = (UInt32)b >> 6;
M = ((UInt32)b >> 3) & 7;
if ((b & 0x7) == 6)
{
// REP - LLMMM110 <LITERALS>
if (L == 0)
{
// spec
if (M == 0)
break; // EOS
if (M <= 2)
continue; // NOP
return S_FALSE; // UNDEFINED
}
}
else
{
if (packSize == 0)
return S_FALSE;
Byte b1;
if (!m_InStream.ReadByte(b1))
return S_FALSE;
packSize--;
// large - LLMMM111 DDDDDDDD DDDDDDDD <LITERALS>
// small - LLMMMDDD DDDDDDDD <LITERALS>
D = ((UInt32)b & 7);
if (D == 7)
{
if (packSize == 0)
return S_FALSE;
Byte b2;
if (!m_InStream.ReadByte(b2))
return S_FALSE;
packSize--;
D = b2;
}
D = (D << 8) + b1;
}
}
M += 3;
}
{
for (unsigned i = 0; i < L; i++)
{
if (packSize == 0 || unpackSize == 0)
return S_FALSE;
Byte b1;
if (!m_InStream.ReadByte(b1))
return S_FALSE;
packSize--;
m_OutWindowStream.PutByte(b1);
unpackSize--;
}
}
if (M != 0)
{
if (unpackSize == 0 || D == 0)
return S_FALSE;
unsigned cur = M;
if (cur > unpackSize)
cur = (unsigned)unpackSize;
if (!m_OutWindowStream.CopyBlock(D - 1, cur))
return S_FALSE;
unpackSize -= cur;
if (cur != M)
return S_FALSE;
}
}
if (unpackSize != 0)
return S_FALSE;
// LZVN encoder writes 7 additional zero bytes
if (packSize != 7)
return S_FALSE;
do
{
Byte b;
if (!m_InStream.ReadByte(b))
return S_FALSE;
packSize--;
if (b != 0)
return S_FALSE;
}
while (packSize != 0);
return S_OK;
}
// ---------- LZFSE ----------
#define MATCHES_PER_BLOCK 10000
#define LITERALS_PER_BLOCK (4 * MATCHES_PER_BLOCK)
#define NUM_L_SYMBOLS 20
#define NUM_M_SYMBOLS 20
#define NUM_D_SYMBOLS 64
#define NUM_LIT_SYMBOLS 256
#define NUM_SYMBOLS ( \
NUM_L_SYMBOLS + \
NUM_M_SYMBOLS + \
NUM_D_SYMBOLS + \
NUM_LIT_SYMBOLS)
#define NUM_L_STATES (1 << 6)
#define NUM_M_STATES (1 << 6)
#define NUM_D_STATES (1 << 8)
#define NUM_LIT_STATES (1 << 10)
typedef UInt32 CFseState;
static UInt32 SumFreqs(const UInt16 *freqs, unsigned num)
{
UInt32 sum = 0;
for (unsigned i = 0; i < num; i++)
sum += (UInt32)freqs[i];
return sum;
}
static MY_FORCE_INLINE unsigned CountZeroBits(UInt32 val, UInt32 mask)
{
for (unsigned i = 0;;)
{
if (val & mask)
return i;
i++;
mask >>= 1;
}
}
static MY_FORCE_INLINE void InitLitTable(const UInt16 *freqs, UInt32 *table)
{
for (unsigned i = 0; i < NUM_LIT_SYMBOLS; i++)
{
unsigned f = freqs[i];
if (f == 0)
continue;
// 0 < f <= numStates
// 0 <= k <= numStatesLog
// numStates <= (f<<k) < numStates * 2
// 0 < j0 <= f
// (f + j0) = next_power_of_2 for f
unsigned k = CountZeroBits(f, NUM_LIT_STATES);
unsigned j0 = (((unsigned)NUM_LIT_STATES * 2) >> k) - f;
/*
CEntry
{
Byte k;
Byte symbol;
UInt16 delta;
};
*/
UInt32 e = ((UInt32)i << 8) + k;
k += 16;
UInt32 d = e + ((UInt32)f << k) - ((UInt32)NUM_LIT_STATES << 16);
UInt32 step = (UInt32)1 << k;
unsigned j = 0;
do
{
*table++ = d;
d += step;
}
while (++j < j0);
e--;
step >>= 1;
for (j = j0; j < f; j++)
{
*table++ = e;
e += step;
}
}
}
typedef struct
{
Byte totalBits;
Byte extraBits;
UInt16 delta;
UInt32 vbase;
} CExtraEntry;
static void InitExtraDecoderTable(unsigned numStates,
unsigned numSymbols,
const UInt16 *freqs,
const Byte *vbits,
CExtraEntry *table)
{
UInt32 vbase = 0;
for (unsigned i = 0; i < numSymbols; i++)
{
unsigned f = freqs[i];
unsigned extraBits = vbits[i];
if (f != 0)
{
unsigned k = CountZeroBits(f, numStates);
unsigned j0 = ((2 * numStates) >> k) - f;
unsigned j = 0;
do
{
CExtraEntry *e = table++;
e->totalBits = (Byte)(k + extraBits);
e->extraBits = (Byte)extraBits;
e->delta = (UInt16)(((f + j) << k) - numStates);
e->vbase = vbase;
}
while (++j < j0);
f -= j0;
k--;
for (j = 0; j < f; j++)
{
CExtraEntry *e = table++;
e->totalBits = (Byte)(k + extraBits);
e->extraBits = (Byte)extraBits;
e->delta = (UInt16)(j << k);
e->vbase = vbase;
}
}
vbase += ((UInt32)1 << extraBits);
}
}
static const Byte k_L_extra[NUM_L_SYMBOLS] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 5, 8
};
static const Byte k_M_extra[NUM_M_SYMBOLS] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 8, 11
};
static const Byte k_D_extra[NUM_D_SYMBOLS] =
{
0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11,
12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15
};
// ---------- CBitStream ----------
typedef struct
{
UInt32 accum;
unsigned numBits; // [0, 31] - Number of valid bits in (accum), other bits are 0
} CBitStream;
static MY_FORCE_INLINE int FseInStream_Init(CBitStream *s,
int n, // [-7, 0], (-n == number_of_unused_bits) in last byte
const Byte **pbuf)
{
*pbuf -= 4;
s->accum = GetUi32(*pbuf);
if (n)
{
s->numBits = n + 32;
if ((s->accum >> s->numBits) != 0)
return -1; // ERROR, encoder should have zeroed the upper bits
}
else
{
*pbuf += 1;
s->accum >>= 8;
s->numBits = 24;
}
return 0; // OK
}
// 0 <= numBits < 32
#define mask31(x, numBits) ((x) & (((UInt32)1 << (numBits)) - 1))
#define FseInStream_FLUSH \
{ unsigned nbits = (31 - in.numBits) & -8; \
if (nbits) { \
buf -= (nbits >> 3); \
if (buf < buf_check) return S_FALSE; \
UInt32 v = GetUi32(buf); \
in.accum = (in.accum << nbits) | mask31(v, nbits); \
in.numBits += nbits; }}
static MY_FORCE_INLINE UInt32 BitStream_Pull(CBitStream *s, unsigned numBits)
{
s->numBits -= numBits;
UInt32 v = s->accum >> s->numBits;
s->accum = mask31(s->accum, s->numBits);
return v;
}
#define DECODE_LIT(dest, pstate) { \
UInt32 e = lit_decoder[pstate]; \
pstate = (CFseState)((e >> 16) + BitStream_Pull(&in, e & 0xff)); \
dest = (Byte)(e >> 8); }
static MY_FORCE_INLINE UInt32 FseDecodeExtra(CFseState *pstate,
const CExtraEntry *table,
CBitStream *s)
{
const CExtraEntry *e = &table[*pstate];
UInt32 v = BitStream_Pull(s, e->totalBits);
unsigned extraBits = e->extraBits;
*pstate = (CFseState)(e->delta + (v >> extraBits));
return e->vbase + mask31(v, extraBits);
}
#define freqs_L (freqs)
#define freqs_M (freqs_L + NUM_L_SYMBOLS)
#define freqs_D (freqs_M + NUM_M_SYMBOLS)
#define freqs_LIT (freqs_D + NUM_D_SYMBOLS)
#define GET_BITS_64(v, offset, num, dest) dest = (UInt32) ((v >> (offset)) & ((1 << (num)) - 1));
#define GET_BITS_32(v, offset, num, dest) dest = (CFseState)((v >> (offset)) & ((1 << (num)) - 1));
HRESULT CDecoder::DecodeLzfse(UInt32 unpackSize, Byte version)
{
PRF(printf("\nLZFSE-%d %7u", version - '0', unpackSize));
UInt32 numLiterals;
UInt32 litPayloadSize;
Int32 literal_bits;
UInt32 lit_state_0;
UInt32 lit_state_1;
UInt32 lit_state_2;
UInt32 lit_state_3;
UInt32 numMatches;
UInt32 lmdPayloadSize;
Int32 lmd_bits;
CFseState l_state;
CFseState m_state;
CFseState d_state;
UInt16 freqs[NUM_SYMBOLS];
if (version == kSignature_LZFSE_V1)
{
return E_NOTIMPL;
// we need examples to test LZFSE-V1 code
/*
const unsigned k_v1_SubHeaderSize = 7 * 4 + 7 * 2;
const unsigned k_v1_HeaderSize = k_v1_SubHeaderSize + NUM_SYMBOLS * 2;
_buffer.AllocAtLeast(k_v1_HeaderSize);
if (m_InStream.ReadBytes(_buffer, k_v1_HeaderSize) != k_v1_HeaderSize)
return S_FALSE;
const Byte *buf = _buffer;
#define GET_32(offs, dest) dest = GetUi32(buf + offs)
#define GET_16(offs, dest) dest = GetUi16(buf + offs)
UInt32 payload_bytes;
GET_32(0, payload_bytes);
GET_32(4, numLiterals);
GET_32(8, numMatches);
GET_32(12, litPayloadSize);
GET_32(16, lmdPayloadSize);
if (litPayloadSize > (1 << 20) || lmdPayloadSize > (1 << 20))
return S_FALSE;
GET_32(20, literal_bits);
if (literal_bits < -7 || literal_bits > 0)
return S_FALSE;
GET_16(24, lit_state_0);
GET_16(26, lit_state_1);
GET_16(28, lit_state_2);
GET_16(30, lit_state_3);
GET_32(32, lmd_bits);
if (lmd_bits < -7 || lmd_bits > 0)
return S_FALSE;
GET_16(36, l_state);
GET_16(38, m_state);
GET_16(40, d_state);
for (unsigned i = 0; i < NUM_SYMBOLS; i++)
freqs[i] = GetUi16(buf + k_v1_SubHeaderSize + i * 2);
*/
}
else
{
UInt32 headerSize;
{
const unsigned kPreHeaderSize = 4 * 2; // signature and upackSize
const unsigned kHeaderSize = 8 * 3;
Byte temp[kHeaderSize];
if (m_InStream.ReadBytes(temp, kHeaderSize) != kHeaderSize)
return S_FALSE;
UInt64 v;
v = GetUi64(temp);
GET_BITS_64(v, 0, 20, numLiterals);
GET_BITS_64(v, 20, 20, litPayloadSize);
GET_BITS_64(v, 40, 20, numMatches);
GET_BITS_64(v, 60, 3 + 1, literal_bits); // (NumberOfUsedBits - 1)
literal_bits -= 7; // (-NumberOfUnusedBits)
if (literal_bits > 0)
return S_FALSE;
// GET_BITS_64(v, 63, 1, unused);
v = GetUi64(temp + 8);
GET_BITS_64(v, 0, 10, lit_state_0);
GET_BITS_64(v, 10, 10, lit_state_1);
GET_BITS_64(v, 20, 10, lit_state_2);
GET_BITS_64(v, 30, 10, lit_state_3);
GET_BITS_64(v, 40, 20, lmdPayloadSize);
GET_BITS_64(v, 60, 3 + 1, lmd_bits);
lmd_bits -= 7;
if (lmd_bits > 0)
return S_FALSE;
// GET_BITS_64(v, 63, 1, unused)
UInt32 v32 = GetUi32(temp + 20);
// (total header size in bytes; this does not
// correspond to a field in the uncompressed header version,
// but is required; we wouldn't know the size of the
// compresssed header otherwise.
GET_BITS_32(v32, 0, 10, l_state);
GET_BITS_32(v32, 10, 10, m_state);
GET_BITS_32(v32, 20, 10 + 2, d_state);
// GET_BITS_64(v, 62, 2, unused);
headerSize = GetUi32(temp + 16);
if (headerSize <= kPreHeaderSize + kHeaderSize)
return S_FALSE;
headerSize -= kPreHeaderSize + kHeaderSize;
}
// no freqs case is not allowed ?
// memset(freqs, 0, sizeof(freqs));
// if (headerSize != 0)
{
static const Byte numBitsTable[32] =
{
2, 3, 2, 5, 2, 3, 2, 8, 2, 3, 2, 5, 2, 3, 2, 14,
2, 3, 2, 5, 2, 3, 2, 8, 2, 3, 2, 5, 2, 3, 2, 14
};
static const Byte valueTable[32] =
{
0, 2, 1, 4, 0, 3, 1, 8, 0, 2, 1, 5, 0, 3, 1, 24,
0, 2, 1, 6, 0, 3, 1, 8, 0, 2, 1, 7, 0, 3, 1, 24
};
UInt32 accum = 0;
unsigned numBits = 0;
for (unsigned i = 0; i < NUM_SYMBOLS; i++)
{
while (numBits <= 14 && headerSize != 0)
{
Byte b;
if (!m_InStream.ReadByte(b))
return S_FALSE;
accum |= (UInt32)b << numBits;
numBits += 8;
headerSize--;
}
unsigned b = (unsigned)accum & 31;
unsigned n = numBitsTable[b];
if (numBits < n)
return S_FALSE;
numBits -= n;
UInt32 f = valueTable[b];
if (n >= 8)
f += ((accum >> 4) & (0x3ff >> (14 - n)));
accum >>= n;
freqs[i] = (UInt16)f;
}
if (numBits >= 8 || headerSize != 0)
return S_FALSE;
}
}
PRF(printf(" Literals=%6u Matches=%6u", numLiterals, numMatches));
if (numLiterals > LITERALS_PER_BLOCK
|| (numLiterals & 3) != 0
|| numMatches > MATCHES_PER_BLOCK
|| lit_state_0 >= NUM_LIT_STATES
|| lit_state_1 >= NUM_LIT_STATES
|| lit_state_2 >= NUM_LIT_STATES
|| lit_state_3 >= NUM_LIT_STATES
|| l_state >= NUM_L_STATES
|| m_state >= NUM_M_STATES
|| d_state >= NUM_D_STATES)
return S_FALSE;
// only full table is allowed ?
if ( SumFreqs(freqs_L, NUM_L_SYMBOLS) != NUM_L_STATES
|| SumFreqs(freqs_M, NUM_M_SYMBOLS) != NUM_M_STATES
|| SumFreqs(freqs_D, NUM_D_SYMBOLS) != NUM_D_STATES
|| SumFreqs(freqs_LIT, NUM_LIT_SYMBOLS) != NUM_LIT_STATES)
return S_FALSE;
const unsigned kPad = 16;
// ---------- Decode literals ----------
{
_literals.AllocAtLeast(LITERALS_PER_BLOCK + 16);
_buffer.AllocAtLeast(kPad + litPayloadSize);
memset(_buffer, 0, kPad);
if (m_InStream.ReadBytes(_buffer + kPad, litPayloadSize) != litPayloadSize)
return S_FALSE;
UInt32 lit_decoder[NUM_LIT_STATES];
InitLitTable(freqs_LIT, lit_decoder);
const Byte *buf_start = _buffer + kPad;
const Byte *buf_check = buf_start - 4;
const Byte *buf = buf_start + litPayloadSize;
CBitStream in;
if (FseInStream_Init(&in, literal_bits, &buf) != 0)
return S_FALSE;
Byte *lit = _literals;
const Byte *lit_limit = lit + numLiterals;
for (; lit < lit_limit; lit += 4)
{
FseInStream_FLUSH
DECODE_LIT (lit[0], lit_state_0);
DECODE_LIT (lit[1], lit_state_1);
FseInStream_FLUSH
DECODE_LIT (lit[2], lit_state_2);
DECODE_LIT (lit[3], lit_state_3);
}
if ((buf_start - buf) * 8 != (int)in.numBits)
return S_FALSE;
}
// ---------- Decode LMD ----------
_buffer.AllocAtLeast(kPad + lmdPayloadSize);
memset(_buffer, 0, kPad);
if (m_InStream.ReadBytes(_buffer + kPad, lmdPayloadSize) != lmdPayloadSize)
return S_FALSE;
CExtraEntry l_decoder[NUM_L_STATES];
CExtraEntry m_decoder[NUM_M_STATES];
CExtraEntry d_decoder[NUM_D_STATES];
InitExtraDecoderTable(NUM_L_STATES, NUM_L_SYMBOLS, freqs_L, k_L_extra, l_decoder);
InitExtraDecoderTable(NUM_M_STATES, NUM_M_SYMBOLS, freqs_M, k_M_extra, m_decoder);
InitExtraDecoderTable(NUM_D_STATES, NUM_D_SYMBOLS, freqs_D, k_D_extra, d_decoder);
const Byte *buf_start = _buffer + kPad;
const Byte *buf_check = buf_start - 4;
const Byte *buf = buf_start + lmdPayloadSize;
CBitStream in;
if (FseInStream_Init(&in, lmd_bits, &buf))
return S_FALSE;
const Byte *lit = _literals;
const Byte *lit_limit = lit + numLiterals;
UInt32 D = 0;
for (;;)
{
if (numMatches == 0)
break;
numMatches--;
FseInStream_FLUSH
unsigned L = (unsigned)FseDecodeExtra(&l_state, l_decoder, &in);
FseInStream_FLUSH
unsigned M = (unsigned)FseDecodeExtra(&m_state, m_decoder, &in);
FseInStream_FLUSH
{
UInt32 new_D = FseDecodeExtra(&d_state, d_decoder, &in);
if (new_D)
D = new_D;
}
if (L != 0)
{
if (L > (size_t)(lit_limit - lit))
return S_FALSE;
unsigned cur = L;
if (cur > unpackSize)
cur = (unsigned)unpackSize;
m_OutWindowStream.PutBytes(lit, cur);
unpackSize -= cur;
lit += cur;
if (cur != L)
return S_FALSE;
}
if (M != 0)
{
if (unpackSize == 0 || D == 0)
return S_FALSE;
unsigned cur = M;
if (cur > unpackSize)
cur = (unsigned)unpackSize;
if (!m_OutWindowStream.CopyBlock(D - 1, cur))
return S_FALSE;
unpackSize -= cur;
if (cur != M)
return S_FALSE;
}
}
if (unpackSize != 0)
return S_FALSE;
// LZFSE encoder writes 8 additional zero bytes before LMD payload
// We test it:
if ((buf - buf_start) * 8 + in.numBits != 64)
return S_FALSE;
if (GetUi64(buf_start) != 0)
return S_FALSE;
return S_OK;
}
STDMETHODIMP CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress)
{
PRF(printf("\n\nLzfseDecoder %7u %7u\n", (unsigned)*outSize, (unsigned)*inSize));
const UInt32 kLzfseDictSize = 1 << 18;
if (!m_OutWindowStream.Create(kLzfseDictSize))
return E_OUTOFMEMORY;
if (!m_InStream.Create(1 << 18))
return E_OUTOFMEMORY;
m_OutWindowStream.SetStream(outStream);
m_OutWindowStream.Init(false);
m_InStream.SetStream(inStream);
m_InStream.Init();
CCoderReleaser coderReleaser(this);
UInt64 prevOut = 0;
UInt64 prevIn = 0;
for (;;)
{
const UInt64 pos = m_OutWindowStream.GetProcessedSize();
const UInt64 packPos = m_InStream.GetProcessedSize();
if (progress && ((pos - prevOut) >= (1 << 22) || (packPos - prevIn) >= (1 << 22)))
{
RINOK(progress->SetRatioInfo(&packPos, &pos));
prevIn = packPos;
prevOut = pos;
}
const UInt64 rem = *outSize - pos;
UInt32 v;
RINOK(GetUInt32(v))
if ((v & 0xFFFFFF) != 0x787662) // bvx
return S_FALSE;
v >>= 24;
if (v == 0x24) // '$', end of stream
break;
UInt32 unpackSize;
RINOK(GetUInt32(unpackSize));
UInt32 cur = unpackSize;
if (cur > rem)
cur = (UInt32)rem;
unpackSize -= cur;
HRESULT res;
if (v == kSignature_LZFSE_V1 || v == kSignature_LZFSE_V2)
res = DecodeLzfse(cur, (Byte)v);
else if (v == 0x6E) // 'n'
res = DecodeLzvn(cur);
else if (v == 0x2D) // '-'
res = DecodeUncompressed(cur);
else
return E_NOTIMPL;
if (res != S_OK)
return res;
if (unpackSize != 0)
return S_FALSE;
}
coderReleaser.NeedFlush = false;
HRESULT res = m_OutWindowStream.Flush();
if (res == S_OK)
if (*inSize != m_InStream.GetProcessedSize()
|| *outSize != m_OutWindowStream.GetProcessedSize())
res = S_FALSE;
return res;
}
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 CLzOutWindowException &e) { return e.ErrorCode; }
catch(...) { return E_OUTOFMEMORY; }
// catch(...) { return S_FALSE; }
}
}}

View File

@@ -0,0 +1,58 @@
// LzfseDecoder.h
#ifndef __LZFSE_DECODER_H
#define __LZFSE_DECODER_H
#include "../../Common/MyBuffer.h"
#include "../../Common/MyCom.h"
#include "../ICoder.h"
#include "../Common/InBuffer.h"
#include "LzOutWindow.h"
namespace NCompress {
namespace NLzfse {
class CDecoder:
public ICompressCoder,
public CMyUnknownImp
{
CLzOutWindow m_OutWindowStream;
CInBuffer m_InStream;
CByteBuffer _literals;
CByteBuffer _buffer;
class CCoderReleaser
{
CDecoder *m_Coder;
public:
bool NeedFlush;
CCoderReleaser(CDecoder *coder): m_Coder(coder), NeedFlush(true) {}
~CCoderReleaser()
{
if (NeedFlush)
m_Coder->m_OutWindowStream.Flush();
}
};
friend class CCoderReleaser;
HRESULT GetUInt32(UInt32 &val);
HRESULT DecodeUncompressed(UInt32 unpackSize);
HRESULT DecodeLzvn(UInt32 unpackSize);
HRESULT DecodeLzfse(UInt32 unpackSize, Byte version);
STDMETHOD(CodeReal)(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
public:
MY_UNKNOWN_IMP
STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, const UInt64 *inSize,
const UInt64 *outSize, ICompressProgressInfo *progress);
};
}}
#endif

View File

@@ -2,81 +2,48 @@
#include "StdAfx.h"
// #include <stdio.h>
#include "../../../C/Alloc.h"
// #include "../../../C/CpuTicks.h"
#include "../Common/StreamUtils.h"
#include "Lzma2Decoder.h"
static HRESULT SResToHRESULT(SRes res)
{
switch (res)
{
case SZ_OK: return S_OK;
case SZ_ERROR_MEM: return E_OUTOFMEMORY;
case SZ_ERROR_PARAM: return E_INVALIDARG;
case SZ_ERROR_UNSUPPORTED: return E_NOTIMPL;
case SZ_ERROR_DATA: return S_FALSE;
}
return E_FAIL;
}
namespace NCompress {
namespace NLzma2 {
CDecoder::CDecoder():
_inBuf(NULL),
_finishMode(false),
_outSizeDefined(false),
_outStep(1 << 22),
_inBufSize(0),
_inBufSizeNew(1 << 20)
{
Lzma2Dec_Construct(&_state);
}
_dec(NULL)
, _inProcessed(0)
, _prop(0xFF)
, _finishMode(false)
, _inBufSize(1 << 20)
, _outStep(1 << 20)
#ifndef _7ZIP_ST
, _tryMt(1)
, _numThreads(1)
, _memUsage((UInt64)(sizeof(size_t)) << 28)
#endif
{}
CDecoder::~CDecoder()
{
Lzma2Dec_Free(&_state, &g_Alloc);
MidFree(_inBuf);
if (_dec)
Lzma2DecMt_Destroy(_dec);
}
STDMETHODIMP CDecoder::SetInBufSize(UInt32 , UInt32 size) { _inBufSizeNew = size; return S_OK; }
STDMETHODIMP CDecoder::SetInBufSize(UInt32 , UInt32 size) { _inBufSize = size; return S_OK; }
STDMETHODIMP CDecoder::SetOutBufSize(UInt32 , UInt32 size) { _outStep = size; return S_OK; }
STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *prop, UInt32 size)
{
if (size != 1)
return E_NOTIMPL;
RINOK(SResToHRESULT(Lzma2Dec_Allocate(&_state, prop[0], &g_Alloc)));
if (!_inBuf || _inBufSize != _inBufSizeNew)
{
MidFree(_inBuf);
_inBufSize = 0;
_inBuf = (Byte *)MidAlloc(_inBufSizeNew);
if (!_inBuf)
return E_OUTOFMEMORY;
_inBufSize = _inBufSizeNew;
}
return S_OK;
}
STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize)
{
_outSizeDefined = (outSize != NULL);
_outSize = 0;
if (_outSizeDefined)
_outSize = *outSize;
_inPos = _inLim = 0;
_inProcessed = 0;
_outProcessed = 0;
Lzma2Dec_Init(&_state);
if (prop[0] > 40)
return E_NOTIMPL;
_prop = prop[0];
return S_OK;
}
@@ -88,6 +55,137 @@ STDMETHODIMP CDecoder::SetFinishMode(UInt32 finishMode)
}
#ifndef _7ZIP_ST
static UInt64 Get_ExpectedBlockSize_From_Dict(UInt32 dictSize)
{
const UInt32 kMinSize = (UInt32)1 << 20;
const UInt32 kMaxSize = (UInt32)1 << 28;
UInt64 blockSize = (UInt64)dictSize << 2;
if (blockSize < kMinSize) blockSize = kMinSize;
if (blockSize > kMaxSize) blockSize = kMaxSize;
if (blockSize < dictSize) blockSize = dictSize;
blockSize += (kMinSize - 1);
blockSize &= ~(UInt64)(kMinSize - 1);
return blockSize;
}
#define LZMA2_DIC_SIZE_FROM_PROP_FULL(p) ((p) == 40 ? 0xFFFFFFFF : (((UInt32)2 | ((p) & 1)) << ((p) / 2 + 11)))
#endif
#define RET_IF_WRAP_ERROR_CONFIRMED(wrapRes, sRes, sResErrorCode) \
if (wrapRes != S_OK && sRes == sResErrorCode) return wrapRes;
#define RET_IF_WRAP_ERROR(wrapRes, sRes, sResErrorCode) \
if (wrapRes != S_OK /* && (sRes == SZ_OK || sRes == sResErrorCode) */) return wrapRes;
STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress)
{
_inProcessed = 0;
if (!_dec)
{
_dec = Lzma2DecMt_Create(
// &g_AlignedAlloc,
&g_Alloc,
&g_MidAlloc);
if (!_dec)
return E_OUTOFMEMORY;
}
CLzma2DecMtProps props;
Lzma2DecMtProps_Init(&props);
props.inBufSize_ST = _inBufSize;
props.outStep_ST = _outStep;
#ifndef _7ZIP_ST
{
props.numThreads = 1;
UInt32 numThreads = _numThreads;
if (_tryMt && numThreads >= 1)
{
UInt64 useLimit = _memUsage;
UInt32 dictSize = LZMA2_DIC_SIZE_FROM_PROP_FULL(_prop);
UInt64 expectedBlockSize64 = Get_ExpectedBlockSize_From_Dict(dictSize);
size_t expectedBlockSize = (size_t)expectedBlockSize64;
size_t inBlockMax = expectedBlockSize + expectedBlockSize / 16;
if (expectedBlockSize == expectedBlockSize64 && inBlockMax >= expectedBlockSize)
{
props.outBlockMax = expectedBlockSize;
props.inBlockMax = inBlockMax;
const size_t kOverheadSize = props.inBufSize_MT + (1 << 16);
UInt64 okThreads = useLimit / (props.outBlockMax + props.inBlockMax + kOverheadSize);
if (numThreads > okThreads)
numThreads = (UInt32)okThreads;
if (numThreads == 0)
numThreads = 1;
props.numThreads = numThreads;
}
}
}
#endif
CSeqInStreamWrap inWrap;
CSeqOutStreamWrap outWrap;
CCompressProgressWrap progressWrap;
inWrap.Init(inStream);
outWrap.Init(outStream);
progressWrap.Init(progress);
SRes res;
UInt64 inProcessed = 0;
int isMT = False;
#ifndef _7ZIP_ST
isMT = _tryMt;
#endif
// UInt64 cpuTicks = GetCpuTicks();
res = Lzma2DecMt_Decode(_dec, _prop, &props,
&outWrap.vt, outSize, _finishMode,
&inWrap.vt,
&inProcessed,
&isMT,
progress ? &progressWrap.vt : NULL);
/*
cpuTicks = GetCpuTicks() - cpuTicks;
printf("\n ticks = %10I64u\n", cpuTicks / 1000000);
*/
#ifndef _7ZIP_ST
/* we reset _tryMt, only if p->props.numThreads was changed */
if (props.numThreads > 1)
_tryMt = isMT;
#endif
_inProcessed = inProcessed;
RET_IF_WRAP_ERROR(progressWrap.Res, res, SZ_ERROR_PROGRESS)
RET_IF_WRAP_ERROR(outWrap.Res, res, SZ_ERROR_WRITE)
RET_IF_WRAP_ERROR_CONFIRMED(inWrap.Res, res, SZ_ERROR_READ)
if (res == SZ_OK && _finishMode)
{
if (inSize && *inSize != inProcessed)
res = SZ_ERROR_DATA;
if (outSize && *outSize != outWrap.Processed)
res = SZ_ERROR_DATA;
}
return SResToHRESULT(res);
}
STDMETHODIMP CDecoder::GetInStreamProcessedSize(UInt64 *value)
{
*value = _inProcessed;
@@ -95,109 +193,51 @@ STDMETHODIMP CDecoder::GetInStreamProcessedSize(UInt64 *value)
}
STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress)
#ifndef _7ZIP_ST
STDMETHODIMP CDecoder::SetNumberOfThreads(UInt32 numThreads)
{
if (!_inBuf)
return S_FALSE;
SetOutStreamSize(outSize);
SizeT wrPos = _state.decoder.dicPos;
HRESULT readRes = S_OK;
for (;;)
{
if (_inPos == _inLim && readRes == S_OK)
{
_inPos = _inLim = 0;
readRes = inStream->Read(_inBuf, _inBufSize, &_inLim);
}
const SizeT dicPos = _state.decoder.dicPos;
SizeT size;
{
SizeT next = _state.decoder.dicBufSize;
if (next - wrPos > _outStep)
next = wrPos + _outStep;
size = next - dicPos;
}
ELzmaFinishMode finishMode = LZMA_FINISH_ANY;
if (_outSizeDefined)
{
const UInt64 rem = _outSize - _outProcessed;
if (size >= rem)
{
size = (SizeT)rem;
if (_finishMode)
finishMode = LZMA_FINISH_END;
}
}
SizeT inProcessed = _inLim - _inPos;
ELzmaStatus status;
SRes res = Lzma2Dec_DecodeToDic(&_state, dicPos + size, _inBuf + _inPos, &inProcessed, finishMode, &status);
_inPos += (UInt32)inProcessed;
_inProcessed += inProcessed;
const SizeT outProcessed = _state.decoder.dicPos - dicPos;
_outProcessed += outProcessed;
bool outFinished = (_outSizeDefined && _outProcessed >= _outSize);
bool needStop = (res != 0
|| (inProcessed == 0 && outProcessed == 0)
|| status == LZMA_STATUS_FINISHED_WITH_MARK
|| (!_finishMode && outFinished));
if (needStop || outProcessed >= size)
{
HRESULT res2 = WriteStream(outStream, _state.decoder.dic + wrPos, _state.decoder.dicPos - wrPos);
if (_state.decoder.dicPos == _state.decoder.dicBufSize)
_state.decoder.dicPos = 0;
wrPos = _state.decoder.dicPos;
RINOK(res2);
if (needStop)
{
if (res != 0)
return S_FALSE;
if (status == LZMA_STATUS_FINISHED_WITH_MARK)
{
if (_finishMode)
{
if (inSize && *inSize != _inProcessed)
return S_FALSE;
if (_outSizeDefined && _outSize != _outProcessed)
return S_FALSE;
}
return readRes;
}
if (!_finishMode && outFinished)
return readRes;
return S_FALSE;
}
}
if (progress)
{
RINOK(progress->SetRatioInfo(&_inProcessed, &_outProcessed));
}
}
_numThreads = numThreads;
return S_OK;
}
STDMETHODIMP CDecoder::SetMemLimit(UInt64 memUsage)
{
_memUsage = memUsage;
return S_OK;
}
#endif
#ifndef NO_READ_FROM_CODER
STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize)
{
CLzma2DecMtProps props;
Lzma2DecMtProps_Init(&props);
props.inBufSize_ST = _inBufSize;
props.outStep_ST = _outStep;
_inProcessed = 0;
if (!_dec)
{
_dec = Lzma2DecMt_Create(&g_AlignedAlloc, &g_MidAlloc);
if (!_dec)
return E_OUTOFMEMORY;
}
_inWrap.Init(_inStream);
SRes res = Lzma2DecMt_Init(_dec, _prop, &props, outSize, _finishMode, &_inWrap.vt);
if (res != SZ_OK)
return SResToHRESULT(res);
return S_OK;
}
STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream) { _inStream = inStream; return S_OK; }
STDMETHODIMP CDecoder::ReleaseInStream() { _inStream.Release(); return S_OK; }
@@ -207,62 +247,17 @@ STDMETHODIMP CDecoder::Read(void *data, UInt32 size, UInt32 *processedSize)
if (processedSize)
*processedSize = 0;
ELzmaFinishMode finishMode = LZMA_FINISH_ANY;
if (_outSizeDefined)
{
const UInt64 rem = _outSize - _outProcessed;
if (size >= rem)
{
size = (UInt32)rem;
if (_finishMode)
finishMode = LZMA_FINISH_END;
}
}
size_t size2 = size;
UInt64 inProcessed = 0;
HRESULT readRes = S_OK;
SRes res = Lzma2DecMt_Read(_dec, (Byte *)data, &size2, &inProcessed);
for (;;)
{
if (_inPos == _inLim && readRes == S_OK)
{
_inPos = _inLim = 0;
readRes = _inStream->Read(_inBuf, _inBufSize, &_inLim);
}
SizeT inProcessed = _inLim - _inPos;
SizeT outProcessed = size;
ELzmaStatus status;
SRes res = Lzma2Dec_DecodeToBuf(&_state, (Byte *)data, &outProcessed,
_inBuf + _inPos, &inProcessed, finishMode, &status);
_inPos += (UInt32)inProcessed;
_inProcessed += inProcessed;
_outProcessed += outProcessed;
size -= (UInt32)outProcessed;
data = (Byte *)data + outProcessed;
if (processedSize)
*processedSize += (UInt32)outProcessed;
if (res != 0)
return S_FALSE;
/*
if (status == LZMA_STATUS_FINISHED_WITH_MARK)
return readRes;
if (size == 0 && status != LZMA_STATUS_NEEDS_MORE_INPUT)
{
if (_finishMode && _outSizeDefined && _outProcessed >= _outSize)
return S_FALSE;
return readRes;
}
*/
if (inProcessed == 0 && outProcessed == 0)
return readRes;
}
_inProcessed += inProcessed;
if (processedSize)
*processedSize = (UInt32)size2;
if (res != SZ_OK)
return SResToHRESULT(res);
return S_OK;
}
#endif

View File

@@ -3,10 +3,9 @@
#ifndef __LZMA2_DECODER_H
#define __LZMA2_DECODER_H
#include "../../../C/Lzma2Dec.h"
#include "../../../C/Lzma2DecMt.h"
#include "../../Common/MyCom.h"
#include "../ICoder.h"
#include "../Common/CWrappers.h"
namespace NCompress {
namespace NLzma2 {
@@ -17,28 +16,26 @@ class CDecoder:
public ICompressSetFinishMode,
public ICompressGetInStreamProcessedSize,
public ICompressSetBufSize,
#ifndef NO_READ_FROM_CODER
public ICompressSetInStream,
public ICompressSetOutStreamSize,
public ISequentialInStream,
#endif
#ifndef _7ZIP_ST
public ICompressSetCoderMt,
public ICompressSetMemLimit,
#endif
public CMyUnknownImp
{
Byte *_inBuf;
UInt32 _inPos;
UInt32 _inLim;
bool _finishMode;
bool _outSizeDefined;
UInt64 _outSize;
CLzma2DecMtHandle _dec;
UInt64 _inProcessed;
UInt64 _outProcessed;
UInt32 _outStep;
Byte _prop;
int _finishMode;
UInt32 _inBufSize;
UInt32 _inBufSizeNew;
CLzma2Dec _state;
UInt32 _outStep;
public:
MY_QUERYINTERFACE_BEGIN2(ICompressCoder)
@@ -46,11 +43,18 @@ public:
MY_QUERYINTERFACE_ENTRY(ICompressSetFinishMode)
MY_QUERYINTERFACE_ENTRY(ICompressGetInStreamProcessedSize)
MY_QUERYINTERFACE_ENTRY(ICompressSetBufSize)
#ifndef NO_READ_FROM_CODER
MY_QUERYINTERFACE_ENTRY(ICompressSetInStream)
MY_QUERYINTERFACE_ENTRY(ICompressSetOutStreamSize)
MY_QUERYINTERFACE_ENTRY(ISequentialInStream)
#endif
#ifndef _7ZIP_ST
MY_QUERYINTERFACE_ENTRY(ICompressSetCoderMt)
MY_QUERYINTERFACE_ENTRY(ICompressSetMemLimit)
#endif
MY_QUERYINTERFACE_END
MY_ADDREF_RELEASE
@@ -59,20 +63,28 @@ public:
STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size);
STDMETHOD(SetFinishMode)(UInt32 finishMode);
STDMETHOD(GetInStreamProcessedSize)(UInt64 *value);
STDMETHOD(SetOutStreamSize)(const UInt64 *outSize);
STDMETHOD(SetInBufSize)(UInt32 streamIndex, UInt32 size);
STDMETHOD(SetOutBufSize)(UInt32 streamIndex, UInt32 size);
#ifndef NO_READ_FROM_CODER
#ifndef _7ZIP_ST
private:
int _tryMt;
UInt32 _numThreads;
UInt64 _memUsage;
public:
STDMETHOD(SetNumberOfThreads)(UInt32 numThreads);
STDMETHOD(SetMemLimit)(UInt64 memUsage);
#endif
#ifndef NO_READ_FROM_CODER
private:
CMyComPtr<ISequentialInStream> _inStream;
CSeqInStreamWrap _inWrap;
public:
STDMETHOD(SetOutStreamSize)(const UInt64 *outSize);
STDMETHOD(SetInStream)(ISequentialInStream *inStream);
STDMETHOD(ReleaseInStream)();
STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
#endif
CDecoder();

View File

@@ -22,7 +22,7 @@ namespace NLzma2 {
CEncoder::CEncoder()
{
_encoder = NULL;
_encoder = Lzma2Enc_Create(&g_Alloc, &g_BigAlloc);
_encoder = Lzma2Enc_Create(&g_AlignedAlloc, &g_BigAlloc);
if (!_encoder)
throw 1;
}

View File

@@ -30,19 +30,24 @@ CDecoder::CDecoder():
FinishStream(false),
_propsWereSet(false),
_outSizeDefined(false),
_outStep(1 << 22),
_outStep(1 << 20),
_inBufSize(0),
_inBufSizeNew(1 << 20)
{
_inProcessed = 0;
_inPos = _inLim = 0;
/*
AlignOffsetAlloc_CreateVTable(&_alloc);
_alloc.numAlignBits = 7;
_alloc.offset = 0;
*/
LzmaDec_Construct(&_state);
}
CDecoder::~CDecoder()
{
LzmaDec_Free(&_state, &g_Alloc);
LzmaDec_Free(&_state, &g_AlignedAlloc); // &_alloc.vt
MyFree(_inBuf);
}
@@ -66,7 +71,7 @@ HRESULT CDecoder::CreateInputBuffer()
STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *prop, UInt32 size)
{
RINOK(SResToHRESULT(LzmaDec_Allocate(&_state, prop, size, &g_Alloc)));
RINOK(SResToHRESULT(LzmaDec_Allocate(&_state, prop, size, &g_AlignedAlloc))) // &_alloc.vt
_propsWereSet = true;
return CreateInputBuffer();
}

View File

@@ -3,6 +3,7 @@
#ifndef __LZMA_DECODER_H
#define __LZMA_DECODER_H
// #include "../../../C/Alloc.h"
#include "../../../C/LzmaDec.h"
#include "../../Common/MyCom.h"
@@ -28,7 +29,6 @@ class CDecoder:
UInt32 _inPos;
UInt32 _inLim;
CLzmaDec _state;
ELzmaStatus _lzmaStatus;
public:
@@ -45,6 +45,10 @@ private:
UInt32 _inBufSize;
UInt32 _inBufSizeNew;
// CAlignOffsetAlloc _alloc;
CLzmaDec _state;
HRESULT CreateInputBuffer();
HRESULT CodeSpec(ISequentialInStream *inStream, ISequentialOutStream *outStream, ICompressProgressInfo *progress);
void SetOutStreamSizeResume(const UInt64 *outSize);

View File

@@ -15,7 +15,7 @@ namespace NLzma {
CEncoder::CEncoder()
{
_encoder = NULL;
_encoder = LzmaEnc_Create(&g_Alloc);
_encoder = LzmaEnc_Create(&g_AlignedAlloc);
if (!_encoder)
throw 1;
}
@@ -23,7 +23,7 @@ CEncoder::CEncoder()
CEncoder::~CEncoder()
{
if (_encoder)
LzmaEnc_Destroy(_encoder, &g_Alloc, &g_BigAlloc);
LzmaEnc_Destroy(_encoder, &g_AlignedAlloc, &g_BigAlloc);
}
static inline wchar_t GetUpperChar(wchar_t c)
@@ -168,7 +168,7 @@ STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream, ISequentialOutStream
progressWrap.Init(progress);
SRes res = LzmaEnc_Encode(_encoder, &outWrap.vt, &inWrap.vt,
progress ? &progressWrap.vt : NULL, &g_Alloc, &g_BigAlloc);
progress ? &progressWrap.vt : NULL, &g_AlignedAlloc, &g_BigAlloc);
_inputProcessed = inWrap.Processed;

View File

@@ -9,77 +9,85 @@
namespace NCompress {
namespace NRar1 {
static const UInt32 PosL1[] = {0,0,0,2,3,5,7,11,16,20,24,32,32, 256};
static const UInt32 PosL2[] = {0,0,0,0,5,7,9,13,18,22,26,34,36, 256};
static const UInt32 PosHf0[] = {0,0,0,0,0,8,16,24,33,33,33,33,33, 257};
static const UInt32 PosHf1[] = {0,0,0,0,0,0,4,44,60,76,80,80,127, 257};
static const UInt32 PosHf2[] = {0,0,0,0,0,0,2,7,53,117,233, 257,0};
static const UInt32 PosHf3[] = {0,0,0,0,0,0,0,2,16,218,251, 257,0};
static const UInt32 PosHf4[] = {0,0,0,0,0,0,0,0,0,255, 257,0,0};
static const unsigned kNumBits = 12;
static const Byte kShortLen1[16 * 3] =
{
0,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xc0,0x80,0x90,0x98,0x9c,0xb0,0,
1,3,4,4,5,6,7,8,8,4,4,5,6,6,0,0,
1,4,4,4,5,6,7,8,8,4,4,5,6,6,4,0
};
static const Byte kShortLen2[16 * 3] =
{
0,0x40,0x60,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xc0,0x80,0x90,0x98,0x9c,0xb0,0,
2,3,3,3,4,4,5,6,6,4,4,5,6,6,0,0,
2,3,3,4,4,4,5,6,6,4,4,5,6,6,4,0
};
static const Byte PosL1[kNumBits + 1] = { 0,0,2,1,2,2,4,5,4,4,8,0,224 };
static const Byte PosL2[kNumBits + 1] = { 0,0,0,5,2,2,4,5,4,4,8,2,220 };
static const Byte PosHf0[kNumBits + 1] = { 0,0,0,0,8,8,8,9,0,0,0,0,224 };
static const Byte PosHf1[kNumBits + 1] = { 0,0,0,0,0,4,40,16,16,4,0,47,130 };
static const Byte PosHf2[kNumBits + 1] = { 0,0,0,0,0,2,5,46,64,116,24,0,0 };
static const Byte PosHf3[kNumBits + 1] = { 0,0,0,0,0,0,2,14,202,33,6,0,0 };
static const Byte PosHf4[kNumBits + 1] = { 0,0,0,0,0,0,0,0,255,2,0,0,0 };
static const UInt32 kHistorySize = (1 << 16);
/*
class CCoderReleaser
{
CDecoder *m_Coder;
public:
CCoderReleaser(CDecoder *coder): m_Coder(coder) {}
~CCoderReleaser() { m_Coder->ReleaseStreams(); }
};
*/
CDecoder::CDecoder():
_isSolid(false),
_solidAllowed(false)
{ }
CDecoder::CDecoder(): m_IsSolid(false) { }
void CDecoder::InitStructures()
{
for (int i = 0; i < kNumRepDists; i++)
m_RepDists[i] = 0;
m_RepDistPtr = 0;
LastLength = 0;
LastDist = 0;
}
UInt32 CDecoder::ReadBits(int numBits) { return m_InBitStream.ReadBits(numBits); }
UInt32 CDecoder::ReadBits(unsigned numBits) { return m_InBitStream.ReadBits(numBits); }
HRESULT CDecoder::CopyBlock(UInt32 distance, UInt32 len)
{
if (len == 0)
return S_FALSE;
if (m_UnpackSize < len)
return S_FALSE;
m_UnpackSize -= len;
return m_OutWindowStream.CopyBlock(distance, len) ? S_OK : S_FALSE;
}
UInt32 CDecoder::DecodeNum(const UInt32 *posTab)
UInt32 CDecoder::DecodeNum(const Byte *numTab)
{
UInt32 startPos = 2;
UInt32 num = m_InBitStream.GetValue(12);
/*
{
// we can check that tables are correct
UInt32 sum = 0;
for (unsigned i = 0; i <= kNumBits; i++)
sum += ((UInt32)numTab[i] << (kNumBits - i));
if (sum != (1 << kNumBits))
throw 111;
}
*/
UInt32 val = m_InBitStream.GetValue(kNumBits);
UInt32 sum = 0;
unsigned i = 2;
for (;;)
{
UInt32 cur = (posTab[(size_t)startPos + 1] - posTab[startPos]) << (12 - startPos);
if (num < cur)
UInt32 num = numTab[i];
UInt32 cur = num << (kNumBits - i);
if (val < cur)
break;
startPos++;
num -= cur;
i++;
val -= cur;
sum += num;
}
m_InBitStream.MovePos(startPos);
return((num >> (12 - startPos)) + posTab[startPos]);
m_InBitStream.MovePos(i);
return ((val >> (kNumBits - i)) + sum);
}
static const Byte kShortLen1 [] = {1,3,4,4,5,6,7,8,8,4,4,5,6,6 };
static const Byte kShortLen1a[] = {1,4,4,4,5,6,7,8,8,4,4,5,6,6,4 };
static const Byte kShortLen2 [] = {2,3,3,3,4,4,5,6,6,4,4,5,6,6 };
static const Byte kShortLen2a[] = {2,3,3,4,4,4,5,6,6,4,4,5,6,6,4 };
static const UInt32 kShortXor1[] = {0,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xc0,0x80,0x90,0x98,0x9c,0xb0};
static const UInt32 kShortXor2[] = {0,0x40,0x60,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xc0,0x80,0x90,0x98,0x9c,0xb0};
HRESULT CDecoder::ShortLZ()
{
UInt32 len, saveLen, dist;
int distancePlace;
const Byte *kShortLen;
const UInt32 *kShortXor;
NumHuf = 0;
if (LCount == 2)
@@ -91,20 +99,14 @@ HRESULT CDecoder::ShortLZ()
UInt32 bitField = m_InBitStream.GetValue(8);
if (AvrLn1 < 37)
UInt32 len, dist;
{
kShortLen = Buf60 ? kShortLen1a : kShortLen1;
kShortXor = kShortXor1;
const Byte *xors = (AvrLn1 < 37) ? kShortLen1 : kShortLen2;
const Byte *lens = xors + 16 + Buf60;
for (len = 0; ((bitField ^ xors[len]) >> (8 - lens[len])) != 0; len++);
m_InBitStream.MovePos(lens[len]);
}
else
{
kShortLen = Buf60 ? kShortLen2a : kShortLen2;
kShortXor = kShortXor2;
}
for (len = 0; ((bitField ^ kShortXor[len]) & (~(0xff >> kShortLen[len]))) != 0; len++);
m_InBitStream.MovePos(kShortLen[len]);
if (len >= 9)
{
if (len == 9)
@@ -112,9 +114,11 @@ HRESULT CDecoder::ShortLZ()
LCount++;
return CopyBlock(LastDist, LastLength);
}
LCount = 0;
if (len == 14)
{
LCount = 0;
len = DecodeNum(PosL2) + 5;
dist = 0x8000 + ReadBits(15) - 1;
LastLength = len;
@@ -122,19 +126,22 @@ HRESULT CDecoder::ShortLZ()
return CopyBlock(dist, len);
}
LCount = 0;
saveLen = len;
UInt32 saveLen = len;
dist = m_RepDists[(m_RepDistPtr - (len - 9)) & 3];
len = DecodeNum(PosL1) + 2;
if (len == 0x101 && saveLen == 10)
len = DecodeNum(PosL1);
if (len == 0xff && saveLen == 10)
{
Buf60 ^= 1;
Buf60 ^= 16;
return S_OK;
}
if (dist >= 256)
{
len++;
if (dist >= MaxDist3 - 1)
len++;
if (dist >= MaxDist3 - 1)
len++;
}
}
else
{
@@ -142,21 +149,23 @@ HRESULT CDecoder::ShortLZ()
AvrLn1 += len;
AvrLn1 -= AvrLn1 >> 4;
distancePlace = DecodeNum(PosHf2) & 0xff;
dist = ChSetA[(unsigned)distancePlace];
if (--distancePlace != -1)
unsigned distancePlace = DecodeNum(PosHf2) & 0xff;
dist = ChSetA[distancePlace];
if (distancePlace != 0)
{
PlaceA[dist]--;
UInt32 lastDistance = ChSetA[(unsigned)distancePlace];
UInt32 lastDistance = ChSetA[(size_t)distancePlace - 1];
PlaceA[lastDistance]++;
ChSetA[(size_t)(unsigned)distancePlace + 1] = lastDistance;
ChSetA[(unsigned)distancePlace] = dist;
ChSetA[distancePlace] = lastDistance;
ChSetA[(size_t)distancePlace - 1] = dist;
}
len += 2;
}
m_RepDists[m_RepDistPtr++] = dist;
m_RepDistPtr &= 3;
len += 2;
LastLength = len;
LastDist = dist;
return CopyBlock(dist, len);
@@ -177,12 +186,10 @@ HRESULT CDecoder::LongLZ()
Nlzb = 0x90;
Nhfb >>= 1;
}
oldAvr2=AvrLn2;
oldAvr2 = AvrLn2;
if (AvrLn2 >= 122)
len = DecodeNum(PosL2);
else if (AvrLn2 >= 64)
len = DecodeNum(PosL1);
if (AvrLn2 >= 64)
len = DecodeNum(AvrLn2 < 122 ? PosL1 : PosL2);
else
{
UInt32 bitField = m_InBitStream.GetValue(16);
@@ -193,8 +200,8 @@ HRESULT CDecoder::LongLZ()
}
else
{
for (len = 0; ((bitField << len) & 0x8000) == 0; len++)
;
for (len = 0; ((bitField << len) & 0x8000) == 0; len++);
m_InBitStream.MovePos(len + 1);
}
}
@@ -202,24 +209,26 @@ HRESULT CDecoder::LongLZ()
AvrLn2 += len;
AvrLn2 -= AvrLn2 >> 5;
if (AvrPlcB > 0x28ff)
distancePlace = DecodeNum(PosHf2);
else if (AvrPlcB > 0x6ff)
distancePlace = DecodeNum(PosHf1);
else
distancePlace = DecodeNum(PosHf0);
{
const Byte *tab;
if (AvrPlcB >= 0x2900) tab = PosHf2;
else if (AvrPlcB >= 0x0700) tab = PosHf1;
else tab = PosHf0;
distancePlace = DecodeNum(tab); // [0, 256]
}
AvrPlcB += distancePlace;
AvrPlcB -= AvrPlcB >> 8;
distancePlace &= 0xff;
for (;;)
{
dist = ChSetB[distancePlace & 0xff];
dist = ChSetB[distancePlace];
newDistancePlace = NToPlB[dist++ & 0xff]++;
if (!(dist & 0xff))
CorrHuff(ChSetB,NToPlB);
else
if (dist & 0xff)
break;
CorrHuff(ChSetB,NToPlB);
}
ChSetB[distancePlace] = ChSetB[newDistancePlace];
@@ -235,9 +244,8 @@ HRESULT CDecoder::LongLZ()
AvrLn3++;
AvrLn3 -= AvrLn3 >> 8;
}
else
if (AvrLn3 > 0)
AvrLn3--;
else if (AvrLn3 > 0)
AvrLn3--;
len += 3;
@@ -265,57 +273,62 @@ HRESULT CDecoder::HuffDecode()
UInt32 curByte, newBytePlace;
UInt32 len;
UInt32 dist;
int bytePlace;
if (AvrPlc > 0x75ff) bytePlace = DecodeNum(PosHf4);
else if (AvrPlc > 0x5dff) bytePlace = DecodeNum(PosHf3);
else if (AvrPlc > 0x35ff) bytePlace = DecodeNum(PosHf2);
else if (AvrPlc > 0x0dff) bytePlace = DecodeNum(PosHf1);
else bytePlace = DecodeNum(PosHf0);
unsigned bytePlace;
{
const Byte *tab;
if (AvrPlc >= 0x7600) tab = PosHf4;
else if (AvrPlc >= 0x5e00) tab = PosHf3;
else if (AvrPlc >= 0x3600) tab = PosHf2;
else if (AvrPlc >= 0x0e00) tab = PosHf1;
else tab = PosHf0;
bytePlace = DecodeNum(tab); // [0, 256]
}
if (StMode)
{
if (--bytePlace == -1)
if (bytePlace == 0)
{
if (ReadBits(1))
{
NumHuf = StMode = 0;
NumHuf = 0;
StMode = false;
return S_OK;
}
else
{
len = (ReadBits(1)) ? 4 : 3;
dist = DecodeNum(PosHf2);
dist = (dist << 5) | ReadBits(5);
return CopyBlock(dist - 1, len);
}
len = ReadBits(1) + 3;
dist = DecodeNum(PosHf2);
dist = (dist << 5) | ReadBits(5);
if (dist == 0)
return S_FALSE;
return CopyBlock(dist - 1, len);
}
bytePlace--; // bytePlace is [0, 255]
}
else if (NumHuf++ >= 16 && FlagsCnt == 0)
StMode = 1;
StMode = true;
bytePlace &= 0xff;
AvrPlc += bytePlace;
AvrPlc -= AvrPlc >> 8;
Nhfb+=16;
Nhfb += 16;
if (Nhfb > 0xff)
{
Nhfb=0x90;
Nhfb = 0x90;
Nlzb >>= 1;
}
m_UnpackSize --;
m_UnpackSize--;
m_OutWindowStream.PutByte((Byte)(ChSet[bytePlace] >> 8));
for (;;)
{
curByte = ChSet[bytePlace];
newBytePlace = NToPl[curByte++ & 0xff]++;
if ((curByte & 0xff) > 0xa1)
CorrHuff(ChSet, NToPl);
else
if ((curByte & 0xff) <= 0xa1)
break;
CorrHuff(ChSet, NToPl);
}
ChSet[bytePlace] = ChSet[newBytePlace];
@@ -327,7 +340,10 @@ HRESULT CDecoder::HuffDecode()
void CDecoder::GetFlagsBuf()
{
UInt32 flags, newFlagsPlace;
UInt32 flagsPlace = DecodeNum(PosHf2);
UInt32 flagsPlace = DecodeNum(PosHf2); // [0, 256]
if (flagsPlace >= ARRAY_SIZE(ChSetC))
return;
for (;;)
{
@@ -343,20 +359,6 @@ void CDecoder::GetFlagsBuf()
ChSetC[newFlagsPlace] = flags;
}
void CDecoder::InitData()
{
if (!m_IsSolid)
{
AvrPlcB = AvrLn1 = AvrLn2 = AvrLn3 = NumHuf = Buf60 = 0;
AvrPlc = 0x3500;
MaxDist3 = 0x2001;
Nhfb = Nlzb = 0x80;
}
FlagsCnt = 0;
FlagBuf = 0;
StMode = 0;
LCount = 0;
}
void CDecoder::CorrHuff(UInt32 *CharSet,UInt32 *NumToPlace)
{
@@ -369,110 +371,131 @@ void CDecoder::CorrHuff(UInt32 *CharSet,UInt32 *NumToPlace)
NumToPlace[i] = (7 - i) * 32;
}
void CDecoder::InitHuff()
{
for (UInt32 i = 0; i < 256; i++)
{
Place[i] = PlaceA[i] = PlaceB[i] = i;
PlaceC[i] = (~i + 1) & 0xff;
ChSet[i] = ChSetB[i] = i << 8;
ChSetA[i] = i;
ChSetC[i] = ((~i + 1) & 0xff) << 8;
}
memset(NToPl, 0, sizeof(NToPl));
memset(NToPlB, 0, sizeof(NToPlB));
memset(NToPlC, 0, sizeof(NToPlC));
CorrHuff(ChSetB, NToPlB);
}
HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo * /* progress */)
{
if (inSize == NULL || outSize == NULL)
if (!inSize || !outSize)
return E_INVALIDARG;
if (_isSolid && !_solidAllowed)
return S_FALSE;
_solidAllowed = false;
if (!m_OutWindowStream.Create(kHistorySize))
return E_OUTOFMEMORY;
if (!m_InBitStream.Create(1 << 20))
return E_OUTOFMEMORY;
m_UnpackSize = (Int64)*outSize;
m_UnpackSize = *outSize;
m_OutWindowStream.SetStream(outStream);
m_OutWindowStream.Init(m_IsSolid);
m_OutWindowStream.Init(_isSolid);
m_InBitStream.SetStream(inStream);
m_InBitStream.Init();
// CCoderReleaser coderReleaser(this);
InitData();
if (!m_IsSolid)
// InitData
FlagsCnt = 0;
FlagBuf = 0;
StMode = false;
LCount = 0;
if (!_isSolid)
{
InitStructures();
InitHuff();
AvrPlcB = AvrLn1 = AvrLn2 = AvrLn3 = NumHuf = Buf60 = 0;
AvrPlc = 0x3500;
MaxDist3 = 0x2001;
Nhfb = Nlzb = 0x80;
{
// InitStructures
for (int i = 0; i < kNumRepDists; i++)
m_RepDists[i] = 0;
m_RepDistPtr = 0;
LastLength = 0;
LastDist = 0;
}
// InitHuff
for (UInt32 i = 0; i < 256; i++)
{
Place[i] = PlaceA[i] = PlaceB[i] = i;
UInt32 c = (~i + 1) & 0xff;
PlaceC[i] = c;
ChSet[i] = ChSetB[i] = i << 8;
ChSetA[i] = i;
ChSetC[i] = c << 8;
}
memset(NToPl, 0, sizeof(NToPl));
memset(NToPlB, 0, sizeof(NToPlB));
memset(NToPlC, 0, sizeof(NToPlC));
CorrHuff(ChSetB, NToPlB);
}
if (m_UnpackSize > 0)
{
GetFlagsBuf();
FlagsCnt = 8;
}
while (m_UnpackSize > 0)
while (m_UnpackSize != 0)
{
if (StMode)
if (!StMode)
{
RINOK(HuffDecode());
continue;
}
if (--FlagsCnt < 0)
{
GetFlagsBuf();
FlagsCnt=7;
}
if (FlagBuf & 0x80)
{
FlagBuf <<= 1;
if (Nlzb > Nhfb)
{
RINOK(LongLZ());
}
else
{
RINOK(HuffDecode());
}
}
else
{
FlagBuf <<= 1;
if (--FlagsCnt < 0)
{
GetFlagsBuf();
FlagsCnt = 7;
}
if (FlagBuf & 0x80)
{
FlagBuf <<= 1;
if (Nlzb > Nhfb)
{
RINOK(HuffDecode());
}
else
{
RINOK(LongLZ());
continue;
}
}
else
{
FlagBuf <<= 1;
RINOK(ShortLZ());
if (--FlagsCnt < 0)
{
GetFlagsBuf();
FlagsCnt = 7;
}
if ((FlagBuf & 0x80) == 0)
{
FlagBuf <<= 1;
RINOK(ShortLZ());
continue;
}
FlagBuf <<= 1;
if (Nlzb <= Nhfb)
{
RINOK(LongLZ());
continue;
}
}
}
RINOK(HuffDecode());
}
if (m_UnpackSize < 0)
return S_FALSE;
_solidAllowed = true;
return m_OutWindowStream.Flush();
}
STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress)
{
@@ -486,7 +509,7 @@ STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size)
{
if (size < 1)
return E_INVALIDARG;
m_IsSolid = ((data[0] & 1) != 0);
_isSolid = ((data[0] & 1) != 0);
return S_OK;
}

View File

@@ -20,48 +20,45 @@ namespace NRar1 {
const UInt32 kNumRepDists = 4;
typedef NBitm::CDecoder<CInBuffer> CBitDecoder;
class CDecoder :
public ICompressCoder,
public ICompressSetDecoderProperties2,
public CMyUnknownImp
{
public:
CLzOutWindow m_OutWindowStream;
CBitDecoder m_InBitStream;
NBitm::CDecoder<CInBuffer> m_InBitStream;
UInt32 m_RepDists[kNumRepDists];
UInt32 m_RepDistPtr;
UInt64 m_UnpackSize;
UInt32 LastDist;
UInt32 LastLength;
Int64 m_UnpackSize;
bool m_IsSolid;
UInt32 m_RepDistPtr;
UInt32 m_RepDists[kNumRepDists];
UInt32 ReadBits(int numBits);
bool _isSolid;
bool _solidAllowed;
bool StMode;
int FlagsCnt;
UInt32 FlagBuf, AvrPlc, AvrPlcB, AvrLn1, AvrLn2, AvrLn3;
unsigned Buf60, NumHuf, LCount;
UInt32 Nhfb, Nlzb, MaxDist3;
UInt32 ChSet[256], ChSetA[256], ChSetB[256], ChSetC[256];
UInt32 Place[256], PlaceA[256], PlaceB[256], PlaceC[256];
UInt32 NToPl[256], NToPlB[256], NToPlC[256];
UInt32 ReadBits(unsigned numBits);
HRESULT CopyBlock(UInt32 distance, UInt32 len);
UInt32 DecodeNum(const UInt32 *posTab);
UInt32 DecodeNum(const Byte *numTab);
HRESULT ShortLZ();
HRESULT LongLZ();
HRESULT HuffDecode();
void GetFlagsBuf();
void InitData();
void InitHuff();
void CorrHuff(UInt32 *CharSet, UInt32 *NumToPlace);
void OldUnpWriteBuf();
UInt32 ChSet[256],ChSetA[256],ChSetB[256],ChSetC[256];
UInt32 Place[256],PlaceA[256],PlaceB[256],PlaceC[256];
UInt32 NToPl[256],NToPlB[256],NToPlC[256];
UInt32 FlagBuf,AvrPlc,AvrPlcB,AvrLn1,AvrLn2,AvrLn3;
int Buf60,NumHuf,StMode,LCount,FlagsCnt;
UInt32 Nhfb,Nlzb,MaxDist3;
void InitStructures();
HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
@@ -70,14 +67,6 @@ public:
MY_UNKNOWN_IMP1(ICompressSetDecoderProperties2)
/*
void ReleaseStreams()
{
m_OutWindowStream.ReleaseStream();
m_InBitStream.ReleaseStream();
}
*/
STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);

View File

@@ -80,7 +80,8 @@ static const UInt32 kHistorySize = 1 << 20;
static const UInt32 kWindowReservSize = (1 << 22) + 256;
CDecoder::CDecoder():
m_IsSolid(false),
_isSolid(false),
_solidAllowed(false),
m_TablesOK(false)
{
}
@@ -130,7 +131,7 @@ bool CDecoder::ReadTables(void)
i = 0;
while (i < numLevels)
do
{
UInt32 sym = m_LevelDecoder.Decode(&m_InBitStream);
if (sym < kTableDirectLevels)
@@ -144,10 +145,7 @@ bool CDecoder::ReadTables(void)
{
unsigned num = ReadBits(2) + 3;
if (i == 0)
{
// return false;
continue; // original unRAR
}
return false;
num += i;
if (num > numLevels)
{
@@ -180,6 +178,10 @@ bool CDecoder::ReadTables(void)
}
}
}
while (i < numLevels);
if (m_InBitStream.ExtraBitsWereRead())
return false;
if (m_AudioMode)
for (i = 0; i < m_NumChannels; i++)
@@ -226,24 +228,14 @@ bool CDecoder::ReadLastTables()
return true;
}
/*
class CCoderReleaser
{
CDecoder *m_Coder;
public:
CCoderReleaser(CDecoder *coder): m_Coder(coder) {}
~CCoderReleaser()
{
m_Coder->ReleaseStreams();
}
};
*/
bool CDecoder::DecodeMm(UInt32 pos)
{
while (pos-- != 0)
{
UInt32 symbol = m_MMDecoders[m_MmFilter.CurrentChannel].Decode(&m_InBitStream);
if (m_InBitStream.ExtraBitsWereRead())
return false;
if (symbol >= 256)
return symbol == 256;
/*
@@ -264,6 +256,8 @@ bool CDecoder::DecodeLz(Int32 pos)
while (pos > 0)
{
UInt32 sym = m_MainDecoder.Decode(&m_InBitStream);
if (m_InBitStream.ExtraBitsWereRead())
return false;
UInt32 length, distance;
if (sym < 256)
{
@@ -338,9 +332,13 @@ bool CDecoder::DecodeLz(Int32 pos)
HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress)
{
if (inSize == NULL || outSize == NULL)
if (!inSize || !outSize)
return E_INVALIDARG;
if (_isSolid && !_solidAllowed)
return S_FALSE;
_solidAllowed = false;
if (!m_OutWindowStream.Create(kHistorySize))
return E_OUTOFMEMORY;
if (!m_InBitStream.Create(1 << 20))
@@ -351,12 +349,12 @@ HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *
UInt64 pos = 0, unPackSize = *outSize;
m_OutWindowStream.SetStream(outStream);
m_OutWindowStream.Init(m_IsSolid);
m_OutWindowStream.Init(_isSolid);
m_InBitStream.SetStream(inStream);
m_InBitStream.Init();
// CCoderReleaser coderReleaser(this);
if (!m_IsSolid)
if (!_isSolid)
{
InitStructures();
if (unPackSize == 0)
@@ -364,6 +362,7 @@ HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *
if (m_InBitStream.GetProcessedSize() + 2 <= m_PackSize) // test it: probably incorrect;
if (!ReadTables())
return S_FALSE;
_solidAllowed = true;
return S_OK;
}
ReadTables();
@@ -389,15 +388,19 @@ HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *
if (!DecodeLz((Int32)blockSize))
return S_FALSE;
}
if (m_InBitStream.ExtraBitsWereRead())
return S_FALSE;
UInt64 globalPos = m_OutWindowStream.GetProcessedSize();
pos = globalPos - blockStartPos;
if (pos < blockSize)
if (!ReadTables())
return S_FALSE;
pos = globalPos - startPos;
if (progress != 0)
if (progress)
{
UInt64 packSize = m_InBitStream.GetProcessedSize();
const UInt64 packSize = m_InBitStream.GetProcessedSize();
RINOK(progress->SetRatioInfo(&packSize, &pos));
}
}
@@ -406,6 +409,9 @@ HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *
if (!ReadLastTables())
return S_FALSE;
_solidAllowed = true;
return m_OutWindowStream.Flush();
}
@@ -422,7 +428,7 @@ STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size)
{
if (size < 1)
return E_INVALIDARG;
m_IsSolid = ((data[0] & 1) != 0);
_isSolid = ((data[0] & 1) != 0);
return S_OK;
}

View File

@@ -125,7 +125,8 @@ class CDecoder :
UInt32 m_LastLength;
bool m_IsSolid;
bool _isSolid;
bool _solidAllowed;
bool m_TablesOK;
bool m_AudioMode;
@@ -159,14 +160,6 @@ public:
MY_UNKNOWN_IMP1(ICompressSetDecoderProperties2)
/*
void ReleaseStreams()
{
m_OutWindowStream.ReleaseStream();
m_InBitStream.ReleaseStream();
}
*/
STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);

View File

@@ -94,7 +94,8 @@ CDecoder::CDecoder():
_writtenFileSize(0),
_vmData(0),
_vmCode(0),
m_IsSolid(false)
_isSolid(false),
_solidAllowed(false)
{
Ppmd7_Construct(&_ppmd);
}
@@ -135,7 +136,7 @@ HRESULT CDecoder::WriteArea(UInt32 startPtr, UInt32 endPtr)
return WriteData(_window, endPtr);
}
void CDecoder::ExecuteFilter(int tempFilterIndex, NVm::CBlockRef &outBlockRef)
void CDecoder::ExecuteFilter(unsigned tempFilterIndex, NVm::CBlockRef &outBlockRef)
{
CTempFilter *tempFilter = _tempFilters[tempFilterIndex];
tempFilter->InitR[6] = (UInt32)_writtenFileSize;
@@ -405,7 +406,7 @@ bool CDecoder::ReadVmCodePPM()
#define RIF(x) { if (!(x)) return S_FALSE; }
UInt32 CDecoder::ReadBits(int numBits) { return m_InBitStream.BitDecoder.ReadBits(numBits); }
UInt32 CDecoder::ReadBits(unsigned numBits) { return m_InBitStream.BitDecoder.ReadBits(numBits); }
// ---------- PPM ----------
@@ -414,7 +415,7 @@ HRESULT CDecoder::InitPPM()
unsigned maxOrder = (unsigned)ReadBits(7);
bool reset = ((maxOrder & 0x20) != 0);
int maxMB = 0;
UInt32 maxMB = 0;
if (reset)
maxMB = (Byte)ReadBits(8);
else
@@ -556,12 +557,13 @@ HRESULT CDecoder::ReadTables(bool &keepDecompressing)
PrevAlignCount = 0;
Byte levelLevels[kLevelTableSize];
Byte newLevels[kTablesSizesSum];
Byte lens[kTablesSizesSum];
if (ReadBits(1) == 0)
memset(m_LastLevels, 0, kTablesSizesSum);
int i;
unsigned i;
for (i = 0; i < kLevelTableSize; i++)
{
UInt32 length = ReadBits(4);
@@ -579,39 +581,44 @@ HRESULT CDecoder::ReadTables(bool &keepDecompressing)
}
levelLevels[i] = (Byte)length;
}
RIF(m_LevelDecoder.Build(levelLevels));
i = 0;
while (i < kTablesSizesSum)
do
{
UInt32 sym = m_LevelDecoder.Decode(&m_InBitStream.BitDecoder);
if (sym < 16)
{
newLevels[i] = Byte((sym + m_LastLevels[i]) & 15);
lens[i] = Byte((sym + m_LastLevels[i]) & 15);
i++;
}
else if (sym > kLevelTableSize)
return S_FALSE;
else
{
int num;
if (((sym - 16) & 1) == 0)
num = ReadBits(3) + 3;
else
num = ReadBits(7) + 11;
if (sym < 18)
unsigned num = ((sym - 16) & 1) * 4;
num += num + 3 + (unsigned)ReadBits(num + 3);
num += i;
if (num > kTablesSizesSum)
num = kTablesSizesSum;
Byte v = 0;
if (sym < 16 + 2)
{
if (i == 0)
return S_FALSE;
for (; num > 0 && i < kTablesSizesSum; num--, i++)
newLevels[i] = newLevels[(size_t)i - 1];
}
else
{
for (; num > 0 && i < kTablesSizesSum; num--)
newLevels[i++] = 0;
v = lens[(size_t)i - 1];
}
do
lens[i++] = v;
while (i < num);
}
}
while (i < kTablesSizesSum);
if (InputEofError())
return S_FALSE;
TablesRead = true;
@@ -624,12 +631,12 @@ HRESULT CDecoder::ReadTables(bool &keepDecompressing)
}
*/
RIF(m_MainDecoder.Build(&newLevels[0]));
RIF(m_DistDecoder.Build(&newLevels[kMainTableSize]));
RIF(m_AlignDecoder.Build(&newLevels[kMainTableSize + kDistTableSize]));
RIF(m_LenDecoder.Build(&newLevels[kMainTableSize + kDistTableSize + kAlignTableSize]));
RIF(m_MainDecoder.Build(&lens[0]));
RIF(m_DistDecoder.Build(&lens[kMainTableSize]));
RIF(m_AlignDecoder.Build(&lens[kMainTableSize + kDistTableSize]));
RIF(m_LenDecoder.Build(&lens[kMainTableSize + kDistTableSize + kAlignTableSize]));
memcpy(m_LastLevels, newLevels, kTablesSizesSum);
memcpy(m_LastLevels, lens, kTablesSizesSum);
TablesOK = true;
@@ -769,7 +776,7 @@ HRESULT CDecoder::DecodeLZ(bool &keepDecompressing)
if (sym2 >= kDistTableSize)
return S_FALSE;
rep0 = kDistStart[sym2];
int numBits = kDistDirectBits[sym2];
unsigned numBits = kDistDirectBits[sym2];
if (sym2 >= (kNumAlignBits * 2) + 2)
{
if (numBits > kNumAlignBits)
@@ -822,7 +829,7 @@ HRESULT CDecoder::CodeReal(ICompressProgressInfo *progress)
_writtenFileSize = 0;
_unsupportedFilter = false;
if (!m_IsSolid)
if (!_isSolid)
{
_lzSize = 0;
_winPos = 0;
@@ -835,14 +842,23 @@ HRESULT CDecoder::CodeReal(ICompressProgressInfo *progress)
PpmEscChar = 2;
PpmError = true;
InitFilters();
// _errorMode = false;
}
if (!m_IsSolid || !TablesRead)
/*
if (_errorMode)
return S_FALSE;
*/
if (!_isSolid || !TablesRead)
{
bool keepDecompressing;
RINOK(ReadTables(keepDecompressing));
if (!keepDecompressing)
{
_solidAllowed = true;
return S_OK;
}
}
for (;;)
@@ -867,6 +883,9 @@ HRESULT CDecoder::CodeReal(ICompressProgressInfo *progress)
if (!keepDecompressing)
break;
}
_solidAllowed = true;
RINOK(WriteBuf());
UInt64 packSize = m_InBitStream.BitDecoder.GetProcessedSize();
RINOK(progress->SetRatioInfo(&packSize, &_writtenFileSize));
@@ -887,6 +906,10 @@ STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream
if (!inSize)
return E_INVALIDARG;
if (_isSolid && !_solidAllowed)
return S_FALSE;
_solidAllowed = false;
if (!_vmData)
{
_vmData = (Byte *)::MidAlloc(kVmDataSizeMax + kVmCodeSizeMax);
@@ -915,8 +938,8 @@ STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream
_unpackSize = outSize ? *outSize : (UInt64)(Int64)-1;
return CodeReal(progress);
}
catch(const CInBufferException &e) { return e.ErrorCode; }
catch(...) { return S_FALSE; }
catch(const CInBufferException &e) { /* _errorMode = true; */ return e.ErrorCode; }
catch(...) { /* _errorMode = true; */ return S_FALSE; }
// CNewException is possible here. But probably CNewException is caused
// by error in data stream.
}
@@ -925,7 +948,7 @@ STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size)
{
if (size < 1)
return E_INVALIDARG;
m_IsSolid = ((data[0] & 1) != 0);
_isSolid = ((data[0] & 1) != 0);
return S_OK;
}

View File

@@ -31,7 +31,7 @@ const UInt32 kLenTableSize = 28;
const UInt32 kMainTableSize = 256 + 1 + 1 + 1 + kNumReps + kNumLen2Symbols + kLenTableSize;
const UInt32 kDistTableSize = 60;
const int kNumAlignBits = 4;
const unsigned kNumAlignBits = 4;
const UInt32 kAlignTableSize = (1 << kNumAlignBits) + 1;
const UInt32 kLevelTableSize = 20;
@@ -158,7 +158,7 @@ struct CTempFilter: public NVm::CProgramInitState
}
};
const int kNumHuffmanBits = 15;
const unsigned kNumHuffmanBits = 15;
class CDecoder:
public ICompressCoder,
@@ -191,7 +191,9 @@ class CDecoder:
CRecordVector<CTempFilter *> _tempFilters;
UInt32 _lastFilter;
bool m_IsSolid;
bool _isSolid;
bool _solidAllowed;
// bool _errorMode;
bool _lzMode;
bool _unsupportedFilter;
@@ -209,7 +211,7 @@ class CDecoder:
HRESULT WriteDataToStream(const Byte *data, UInt32 size);
HRESULT WriteData(const Byte *data, UInt32 size);
HRESULT WriteArea(UInt32 startPtr, UInt32 endPtr);
void ExecuteFilter(int tempFilterIndex, NVm::CBlockRef &outBlockRef);
void ExecuteFilter(unsigned tempFilterIndex, NVm::CBlockRef &outBlockRef);
HRESULT WriteBuf();
void InitFilters();
@@ -217,7 +219,7 @@ class CDecoder:
bool ReadVmCodeLZ();
bool ReadVmCodePPM();
UInt32 ReadBits(int numBits);
UInt32 ReadBits(unsigned numBits);
HRESULT InitPPM();
int DecodePpmSymbol();

View File

@@ -72,6 +72,7 @@ CDecoder::CDecoder():
_writtenFileSize(0),
_dictSizeLog(0),
_isSolid(false),
_solidAllowed(false),
_wasInit(false),
_inputBuf(NULL)
{
@@ -334,58 +335,63 @@ HRESULT CDecoder::ReadTables(CBitDecoder &_bitStream)
{
if (_progress)
{
UInt64 packSize = _bitStream.GetProcessedSize();
const UInt64 packSize = _bitStream.GetProcessedSize();
RINOK(_progress->SetRatioInfo(&packSize, &_writtenFileSize));
}
_bitStream.AlignToByte();
_bitStream.Prepare();
unsigned flags = _bitStream.ReadByteInAligned();
unsigned checkSum = _bitStream.ReadByteInAligned();
checkSum ^= flags;
UInt32 blockSize;
{
unsigned flags = _bitStream.ReadByteInAligned();
unsigned checkSum = _bitStream.ReadByteInAligned();
checkSum ^= flags;
unsigned num = (flags >> 3) & 3;
if (num == 3)
return S_FALSE;
blockSize = _bitStream.ReadByteInAligned();
if (num > 0)
UInt32 blockSize = _bitStream.ReadByteInAligned();
checkSum ^= blockSize;
if (num != 0)
{
blockSize += (UInt32)_bitStream.ReadByteInAligned() << 8;
unsigned b = _bitStream.ReadByteInAligned();
checkSum ^= b;
blockSize += (UInt32)b << 8;
if (num > 1)
blockSize += (UInt32)_bitStream.ReadByteInAligned() << 16;
{
b = _bitStream.ReadByteInAligned();
checkSum ^= b;
blockSize += (UInt32)b << 16;
}
}
}
checkSum ^= blockSize ^ (blockSize >> 8) ^ (blockSize >> 16);
if ((Byte)checkSum != 0x5A)
return S_FALSE;
unsigned blockSizeBits7 = (flags & 7) + 1;
if (blockSize == 0 && blockSizeBits7 != 8)
return S_FALSE;
blockSize += (blockSizeBits7 >> 3);
blockSize--;
_bitStream._blockEndBits7 = (Byte)(blockSizeBits7 & 7);
_bitStream._blockEnd = _bitStream.GetProcessedSize_Round() + blockSize;
_bitStream.SetCheck2();
_isLastBlock = ((flags & 0x40) != 0);
if ((flags & 0x80) == 0)
{
if (!_tableWasFilled && blockSize != 0)
if (checkSum != 0x5A)
return S_FALSE;
return S_OK;
}
_tableWasFilled = false;
unsigned blockSizeBits7 = (flags & 7) + 1;
blockSize += (blockSizeBits7 >> 3);
if (blockSize == 0)
return S_FALSE;
blockSize--;
blockSizeBits7 &= 7;
_bitStream._blockEndBits7 = (Byte)blockSizeBits7;
_bitStream._blockEnd = _bitStream.GetProcessedSize_Round() + blockSize;
_bitStream.SetCheck2();
_isLastBlock = ((flags & 0x40) != 0);
if ((flags & 0x80) == 0)
{
if (!_tableWasFilled)
if (blockSize != 0 || blockSizeBits7 != 0)
return S_FALSE;
return S_OK;
}
_tableWasFilled = false;
}
{
Byte lens2[kLevelTableSize];
@@ -421,7 +427,7 @@ HRESULT CDecoder::ReadTables(CBitDecoder &_bitStream)
Byte lens[kTablesSizesSum];
unsigned i = 0;
while (i < kTablesSizesSum)
do
{
if (_bitStream._buf >= _bitStream._bufCheck2)
{
@@ -439,34 +445,24 @@ HRESULT CDecoder::ReadTables(CBitDecoder &_bitStream)
return S_FALSE;
else
{
sym -= 16;
unsigned sh = ((sym & 1) << 2);
unsigned num = (unsigned)_bitStream.ReadBits9(3 + sh) + 3 + (sh << 1);
unsigned num = ((sym - 16) & 1) * 4;
num += num + 3 + (unsigned)_bitStream.ReadBits9(num + 3);
num += i;
if (num > kTablesSizesSum)
num = kTablesSizesSum;
if (sym < 2)
Byte v = 0;
if (sym < 16 + 2)
{
if (i == 0)
{
// return S_FALSE;
continue; // original unRAR
}
Byte v = lens[(size_t)i - 1];
do
lens[i++] = v;
while (i < num);
}
else
{
do
lens[i++] = 0;
while (i < num);
return S_FALSE;
v = lens[(size_t)i - 1];
}
do
lens[i++] = v;
while (i < num);
}
}
while (i < kTablesSizesSum);
if (_bitStream.IsBlockOverRead())
return S_FALSE;
@@ -606,6 +602,10 @@ HRESULT CDecoder::DecodeLZ()
}
}
}
// that check is not required, but it can help, if there is BUG in another code
if (!_tableWasFilled)
break; // return S_FALSE;
}
UInt32 sym = m_MainDecoder.Decode(&_bitStream);
@@ -807,7 +807,10 @@ HRESULT CDecoder::CodeReal()
*/
if (res == S_OK)
{
_solidAllowed = true;
res = res2;
}
if (res == S_OK && _unpackSize_Defined && _writtenFileSize != _unpackSize)
return S_FALSE;
@@ -827,6 +830,10 @@ STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream
{
try
{
if (_isSolid && !_solidAllowed)
return S_FALSE;
_solidAllowed = false;
if (_dictSizeLog >= sizeof(size_t) * 8)
return E_NOTIMPL;

View File

@@ -127,7 +127,7 @@ public:
return *_buf++;
}
UInt32 GetValue(unsigned numBits)
UInt32 GetValue(unsigned numBits) const
{
UInt32 v = ((UInt32)_buf[0] << 16) | ((UInt32)_buf[1] << 8) | (UInt32)_buf[2];
v >>= (24 - numBits - _bitPos);
@@ -218,6 +218,13 @@ class CDecoder:
bool _unsupportedFilter;
bool _lzError;
bool _writeError;
bool _isSolid;
bool _solidAllowed;
bool _tableWasFilled;
bool _wasInit;
Byte _dictSizeLog;
// CBitDecoder _bitStream;
Byte *_window;
@@ -238,11 +245,6 @@ class CDecoder:
UInt64 _writtenFileSize;
size_t _winSizeAllocated;
Byte _dictSizeLog;
bool _tableWasFilled;
bool _isSolid;
bool _wasInit;
UInt32 _reps[kNumReps];
UInt32 _lastLen;

View File

@@ -13,6 +13,7 @@
namespace NCompress {
namespace NShrink {
static const UInt32 kEmpty = 256; // kNumItems;
static const UInt32 kBufferSize = (1 << 18);
static const unsigned kNumMinBits = 9;
@@ -34,20 +35,15 @@ HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *
outBuffer.Init();
{
unsigned i;
for (i = 0; i < 257; i++)
_parents[i] = (UInt16)i;
for (; i < kNumItems; i++)
_parents[i] = kNumItems;
for (i = 0; i < kNumItems; i++)
_suffixes[i] = 0;
for (unsigned i = 0; i < kNumItems; i++)
_parents[i] = kEmpty;
}
UInt64 prevPos = 0, inPrev = 0;
UInt64 outPrev = 0, inPrev = 0;
unsigned numBits = kNumMinBits;
unsigned head = 257;
int lastSym = -1;
Byte lastChar2 = 0;
Byte lastChar = 0;
bool moreOut = false;
HRESULT res = S_FALSE;
@@ -67,18 +63,22 @@ HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *
break;
}
eofCheck = true;
// Is specSym(=256) allowed after end of stream
// Do we need to read it here
// Is specSym(=256) allowed after end of stream ?
// Do we need to read it here ?
}
if (progress)
{
if (nowPos - prevPos >= (1 << 18)
|| _inProcessed - inPrev >= (1 << 20))
if (nowPos - outPrev >= (1 << 20) || _inProcessed - inPrev >= (1 << 20))
{
prevPos = nowPos;
outPrev = nowPos;
inPrev = _inProcessed;
RINOK(progress->SetRatioInfo(&_inProcessed, &nowPos));
res = progress->SetRatioInfo(&_inProcessed, &nowPos);
if (res != SZ_OK)
{
// break;
return res;
}
}
}
@@ -105,23 +105,30 @@ HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *
continue;
}
if (sym != 2)
break;
{
break;
// continue; // info-zip just ignores such code
}
{
/*
---------- Free leaf nodes ----------
Note : that code can mark _parents[lastSym] as free, and next
inserted node will be Orphan in that case.
*/
unsigned i;
for (i = 257; i < kNumItems; i++)
for (i = 256; i < kNumItems; i++)
_stack[i] = 0;
for (i = 257; i < kNumItems; i++)
{
unsigned par = _parents[i];
if (par != kNumItems)
if (par != kEmpty)
_stack[par] = 1;
}
for (i = 257; i < kNumItems; i++)
if (_stack[i] == 0)
_parents[i] = kNumItems;
_parents[i] = kEmpty;
head = 257;
continue;
}
}
@@ -137,27 +144,22 @@ HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *
bool needPrev = false;
if (head < kNumItems && lastSym >= 0)
{
while (head < kNumItems && _parents[head] != kNumItems)
while (head < kNumItems && _parents[head] != kEmpty)
head++;
if (head < kNumItems)
{
if (head == (unsigned)lastSym)
{
// we need to fix the code for that case
// _parents[head] is not allowed to link to itself
res = E_NOTIMPL;
break;
}
/*
if (head == lastSym), it updates Orphan to self-linked Orphan and creates two problems:
1) we must check _stack[i++] overflow in code that walks tree nodes.
2) self-linked node can not be removed. So such self-linked nodes can occupy all _parents items.
*/
needPrev = true;
_parents[head] = (UInt16)lastSym;
_suffixes[head] = (Byte)lastChar2;
_suffixes[head] = (Byte)lastChar;
head++;
}
}
if (_parents[sym] == kNumItems)
break;
lastSym = sym;
unsigned cur = sym;
unsigned i = 0;
@@ -166,10 +168,17 @@ HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *
{
_stack[i++] = _suffixes[cur];
cur = _parents[cur];
// don't change that code:
// Orphan Check and self-linked Orphan check (_stack overflow check);
if (cur == kEmpty || i >= kNumItems)
break;
}
if (cur == kEmpty || i >= kNumItems)
break;
_stack[i++] = (Byte)cur;
lastChar2 = (Byte)cur;
lastChar = (Byte)cur;
if (needPrev)
_suffixes[(size_t)head - 1] = (Byte)cur;

View File

@@ -19,8 +19,8 @@ class CDecoder :
public ICompressGetInStreamProcessedSize,
public CMyUnknownImp
{
UInt64 _inProcessed;
bool _fullStreamMode;
UInt64 _inProcessed;
UInt16 _parents[kNumItems];
Byte _suffixes[kNumItems];

View File

@@ -4,248 +4,119 @@
#include "../../../C/Alloc.h"
#include "../Common/StreamUtils.h"
#include "../Archive/IArchive.h"
#include "../Common/CWrappers.h"
#include "XzDecoder.h"
using namespace NArchive;
namespace NCompress {
namespace NXz {
#define RET_IF_WRAP_ERROR_CONFIRMED(wrapRes, sRes, sResErrorCode) \
if (wrapRes != S_OK && sRes == sResErrorCode) return wrapRes;
CXzUnpackerCPP::CXzUnpackerCPP(): InBuf(NULL), OutBuf(NULL)
#define RET_IF_WRAP_ERROR(wrapRes, sRes, sResErrorCode) \
if (wrapRes != S_OK /* && (sRes == SZ_OK || sRes == sResErrorCode) */) return wrapRes;
static HRESULT SResToHRESULT_Code(SRes res) throw()
{
XzUnpacker_Construct(&p, &g_Alloc);
}
CXzUnpackerCPP::~CXzUnpackerCPP()
{
XzUnpacker_Free(&p);
MidFree(InBuf);
MidFree(OutBuf);
}
void CStatInfo::Clear()
{
InSize = 0;
OutSize = 0;
PhySize = 0;
NumStreams = 0;
NumBlocks = 0;
UnpackSize_Defined = false;
NumStreams_Defined = false;
NumBlocks_Defined = false;
IsArc = false;
UnexpectedEnd = false;
DataAfterEnd = false;
Unsupported = false;
HeadersError = false;
DataError = false;
CrcError = false;
if (res < 0)
return res;
switch (res)
{
case SZ_OK: return S_OK;
case SZ_ERROR_MEM: return E_OUTOFMEMORY;
case SZ_ERROR_UNSUPPORTED: return E_NOTIMPL;
}
return S_FALSE;
}
HRESULT CDecoder::Decode(ISequentialInStream *seqInStream, ISequentialOutStream *outStream,
const UInt64 *outSizeLimit, bool finishStream, ICompressProgressInfo *progress)
{
const size_t kInBufSize = (size_t)1 << 20;
const size_t kOutBufSize = (size_t)1 << 21;
MainDecodeSRes = S_OK;
MainDecodeSRes_wasUsed = false;
XzStatInfo_Clear(&Stat);
Clear();
DecodeRes = SZ_OK;
XzUnpacker_Init(&xzu.p);
if (!xzu.InBuf)
if (!xz)
{
xzu.InBuf = (Byte *)MidAlloc(kInBufSize);
if (!xzu.InBuf)
xz = XzDecMt_Create(&g_Alloc, &g_MidAlloc);
if (!xz)
return E_OUTOFMEMORY;
}
if (!xzu.OutBuf)
{
xzu.OutBuf = (Byte *)MidAlloc(kOutBufSize);
if (!xzu.OutBuf)
return E_OUTOFMEMORY;
}
UInt32 inSize = 0;
UInt32 inPos = 0;
SizeT outPos = 0;
HRESULT readRes = S_OK;
CXzDecMtProps props;
XzDecMtProps_Init(&props);
for (;;)
int isMT = False;
#ifndef _7ZIP_ST
{
if (inPos == inSize && readRes == S_OK)
props.numThreads = 1;
UInt32 numThreads = _numThreads;
if (_tryMt && numThreads > 1)
{
inPos = inSize = 0;
readRes = seqInStream->Read(xzu.InBuf, kInBufSize, &inSize);
size_t memUsage = (size_t)_memUsage;
if (memUsage != _memUsage)
memUsage = (size_t)0 - 1;
props.memUseMax = memUsage;
isMT = (numThreads > 1);
}
SizeT inLen = inSize - inPos;
SizeT outLen = kOutBufSize - outPos;
ECoderFinishMode finishMode = CODER_FINISH_ANY;
props.numThreads = numThreads;
}
#endif
CSeqInStreamWrap inWrap;
CSeqOutStreamWrap outWrap;
CCompressProgressWrap progressWrap;
inWrap.Init(seqInStream);
outWrap.Init(outStream);
progressWrap.Init(progress);
SRes res = XzDecMt_Decode(xz,
&props,
outSizeLimit, finishStream,
&outWrap.vt,
&inWrap.vt,
&Stat,
&isMT,
progress ? &progressWrap.vt : NULL);
MainDecodeSRes = res;
#ifndef _7ZIP_ST
// _tryMt = isMT;
#endif
RET_IF_WRAP_ERROR(outWrap.Res, res, SZ_ERROR_WRITE)
RET_IF_WRAP_ERROR(progressWrap.Res, res, SZ_ERROR_PROGRESS)
RET_IF_WRAP_ERROR_CONFIRMED(inWrap.Res, res, SZ_ERROR_READ)
// return E_OUTOFMEMORY;
MainDecodeSRes_wasUsed = true;
if (res == SZ_OK && finishStream)
{
/*
// 17.01 : the code was disabled:
if (inSize == 0)
finishMode = CODER_FINISH_END;
if (inSize && *inSize != Stat.PhySize)
res = SZ_ERROR_DATA;
*/
if (outSizeLimit)
{
const UInt64 rem = *outSizeLimit - OutSize;
if (outLen >= rem)
{
outLen = (SizeT)rem;
if (finishStream)
finishMode = CODER_FINISH_END;
}
}
ECoderStatus status;
const SizeT outLenRequested = outLen;
SRes res = XzUnpacker_Code(&xzu.p,
xzu.OutBuf + outPos, &outLen,
xzu.InBuf + inPos, &inLen,
finishMode, &status);
DecodeRes = res;
inPos += (UInt32)inLen;
outPos += outLen;
InSize += inLen;
OutSize += outLen;
bool finished = ((inLen == 0 && outLen == 0) || res != SZ_OK);
if (outLen >= outLenRequested || finished)
{
if (outStream && outPos != 0)
{
RINOK(WriteStream(outStream, xzu.OutBuf, outPos));
}
outPos = 0;
}
if (progress)
{
RINOK(progress->SetRatioInfo(&InSize, &OutSize));
}
if (!finished)
continue;
{
PhySize = InSize;
NumStreams = xzu.p.numStartedStreams;
if (NumStreams > 0)
IsArc = true;
NumBlocks = xzu.p.numTotalBlocks;
UnpackSize_Defined = true;
NumStreams_Defined = true;
NumBlocks_Defined = true;
UInt64 extraSize = XzUnpacker_GetExtraSize(&xzu.p);
if (res == SZ_OK)
{
if (status == CODER_STATUS_NEEDS_MORE_INPUT)
{
extraSize = 0;
if (!XzUnpacker_IsStreamWasFinished(&xzu.p))
{
// finished at padding bytes, but padding is not aligned for 4
UnexpectedEnd = true;
res = SZ_ERROR_DATA;
}
}
else // status == CODER_STATUS_NOT_FINISHED
res = SZ_ERROR_DATA;
}
else if (res == SZ_ERROR_NO_ARCHIVE)
{
if (InSize == extraSize)
IsArc = false;
else
{
if (extraSize != 0 || inPos != inSize)
{
DataAfterEnd = true;
res = SZ_OK;
}
}
}
DecodeRes = res;
PhySize -= extraSize;
switch (res)
{
case SZ_OK: break;
case SZ_ERROR_NO_ARCHIVE: IsArc = false; break;
case SZ_ERROR_ARCHIVE: HeadersError = true; break;
case SZ_ERROR_UNSUPPORTED: Unsupported = true; break;
case SZ_ERROR_CRC: CrcError = true; break;
case SZ_ERROR_DATA: DataError = true; break;
default: DataError = true; break;
}
return readRes;
}
if (outSizeLimit && *outSizeLimit != outWrap.Processed)
res = SZ_ERROR_DATA;
}
return SResToHRESULT_Code(res);
}
Int32 CDecoder::Get_Extract_OperationResult() const
{
Int32 opRes;
if (!IsArc)
opRes = NExtract::NOperationResult::kIsNotArc;
else if (UnexpectedEnd)
opRes = NExtract::NOperationResult::kUnexpectedEnd;
else if (DataAfterEnd)
opRes = NExtract::NOperationResult::kDataAfterEnd;
else if (CrcError)
opRes = NExtract::NOperationResult::kCRCError;
else if (Unsupported)
opRes = NExtract::NOperationResult::kUnsupportedMethod;
else if (HeadersError)
opRes = NExtract::NOperationResult::kDataError;
else if (DataError)
opRes = NExtract::NOperationResult::kDataError;
else if (DecodeRes != SZ_OK)
opRes = NExtract::NOperationResult::kDataError;
else
opRes = NExtract::NOperationResult::kOK;
return opRes;
}
HRESULT CComDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *progress)
{
RINOK(_decoder.Decode(inStream, outStream, outSize, _finishStream, progress));
Int32 opRes = _decoder.Get_Extract_OperationResult();
if (opRes == NArchive::NExtract::NOperationResult::kUnsupportedMethod)
return E_NOTIMPL;
if (opRes != NArchive::NExtract::NOperationResult::kOK)
return S_FALSE;
return S_OK;
return Decode(inStream, outStream, outSize, _finishStream, progress);
}
STDMETHODIMP CComDecoder::SetFinishMode(UInt32 finishMode)
@@ -256,8 +127,24 @@ STDMETHODIMP CComDecoder::SetFinishMode(UInt32 finishMode)
STDMETHODIMP CComDecoder::GetInStreamProcessedSize(UInt64 *value)
{
*value = _decoder.InSize;
*value = Stat.InSize;
return S_OK;
}
#ifndef _7ZIP_ST
STDMETHODIMP CComDecoder::SetNumberOfThreads(UInt32 numThreads)
{
_numThreads = numThreads;
return S_OK;
}
STDMETHODIMP CComDecoder::SetMemLimit(UInt64 memUsage)
{
_memUsage = memUsage;
return S_OK;
}
#endif
}}

View File

@@ -12,57 +12,36 @@
namespace NCompress {
namespace NXz {
struct CXzUnpackerCPP
struct CDecoder
{
Byte *InBuf;
Byte *OutBuf;
CXzUnpacker p;
CXzDecMtHandle xz;
int _tryMt;
UInt32 _numThreads;
UInt64 _memUsage;
SRes MainDecodeSRes; // it's not HRESULT
bool MainDecodeSRes_wasUsed;
CXzStatInfo Stat;
CDecoder():
xz(NULL),
_tryMt(True),
_numThreads(1),
_memUsage((UInt64)(sizeof(size_t)) << 28),
MainDecodeSRes(SZ_OK),
MainDecodeSRes_wasUsed(false)
{}
CXzUnpackerCPP();
~CXzUnpackerCPP();
};
struct CStatInfo
{
UInt64 InSize;
UInt64 OutSize;
UInt64 PhySize;
UInt64 NumStreams;
UInt64 NumBlocks;
bool UnpackSize_Defined;
bool NumStreams_Defined;
bool NumBlocks_Defined;
bool IsArc;
bool UnexpectedEnd;
bool DataAfterEnd;
bool Unsupported;
bool HeadersError;
bool DataError;
bool CrcError;
CStatInfo() { Clear(); }
void Clear();
};
struct CDecoder: public CStatInfo
{
CXzUnpackerCPP xzu;
SRes DecodeRes; // it's not HRESULT
CDecoder(): DecodeRes(SZ_OK) {}
~CDecoder()
{
if (xz)
XzDecMt_Destroy(xz);
}
/* Decode() can return ERROR code only if there is progress or stream error.
Decode() returns S_OK in case of xz decoding error, but DecodeRes and CStatInfo contain error information */
HRESULT Decode(ISequentialInStream *seqInStream, ISequentialOutStream *outStream,
const UInt64 *outSizeLimit, bool finishStream, ICompressProgressInfo *compressProgress);
Int32 Get_Extract_OperationResult() const;
};
@@ -70,21 +49,41 @@ class CComDecoder:
public ICompressCoder,
public ICompressSetFinishMode,
public ICompressGetInStreamProcessedSize,
public CMyUnknownImp
#ifndef _7ZIP_ST
public ICompressSetCoderMt,
public ICompressSetMemLimit,
#endif
public CMyUnknownImp,
public CDecoder
{
CDecoder _decoder;
bool _finishStream;
public:
MY_UNKNOWN_IMP2(
ICompressSetFinishMode,
ICompressGetInStreamProcessedSize)
MY_QUERYINTERFACE_BEGIN2(ICompressCoder)
MY_QUERYINTERFACE_ENTRY(ICompressSetFinishMode)
MY_QUERYINTERFACE_ENTRY(ICompressGetInStreamProcessedSize)
#ifndef _7ZIP_ST
MY_QUERYINTERFACE_ENTRY(ICompressSetCoderMt)
MY_QUERYINTERFACE_ENTRY(ICompressSetMemLimit)
#endif
MY_QUERYINTERFACE_END
MY_ADDREF_RELEASE
STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress);
STDMETHOD(SetFinishMode)(UInt32 finishMode);
STDMETHOD(GetInStreamProcessedSize)(UInt64 *value);
#ifndef _7ZIP_ST
STDMETHOD(SetNumberOfThreads)(UInt32 numThreads);
STDMETHOD(SetMemLimit)(UInt64 memUsage);
#endif
CComDecoder(): _finishStream(false) {}
};

View File

@@ -30,14 +30,13 @@ CDecoder::CDecoder(): CAesCbcDecoder(kAesKeySize) {}
static unsigned ReadVarInt(const Byte *p, unsigned maxSize, UInt64 *val)
{
unsigned i;
*val = 0;
for (i = 0; i < maxSize;)
for (unsigned i = 0; i < maxSize && i < 10;)
{
Byte b = p[i];
if (i < 10)
*val |= (UInt64)(b & 0x7F) << (7 * i++);
*val |= (UInt64)(b & 0x7F) << (7 * i);
i++;
if ((b & 0x80) == 0)
return i;
}
@@ -162,8 +161,9 @@ void CDecoder::Hmac_Convert_32Bytes(Byte *data) const
};
static CKey g_Key;
#ifndef _7ZIP_ST
static CKey g_Key;
static NWindows::NSynchronization::CCriticalSection g_GlobalKeyCacheCriticalSection;
#define MT_LOCK NWindows::NSynchronization::CCriticalSectionLock lock(g_GlobalKeyCacheCriticalSection);
#else

View File

@@ -71,6 +71,9 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
DEFPUSHBUTTON "OK", IDOK, bx2, by, bxs, bys \
PUSHBUTTON "Cancel", IDCANCEL, bx1, by, bxs, bys
#define MY_BUTTON__CLOSE \
DEFPUSHBUTTON "&Close", IDCLOSE, bx1, by, bxs, bys
#define MY_COMBO CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
#define MY_COMBO_SORTED MY_COMBO | CBS_SORT

View File

@@ -49,6 +49,7 @@
25 ICompressSetCoderMt
26 ICompressSetFinishMode
27 ICompressGetInStreamProcessedSize2
28 ICompressSetMemLimit
30 ICompressGetSubStreamSize
31 ICompressSetInStream
@@ -169,7 +170,8 @@ Handler GUIDs:
10 lz5
11 liz
C6 Lzip
C5 Lzip
C6 COFF
C7 Ext
C8 VMDK
C9 VDI

View File

@@ -43,6 +43,7 @@ CODER_INTERFACE(ICompressCoder2, 0x18)
S_OK : OK
S_FALSE : data error (for decoders)
E_OUTOFMEMORY : memory allocation error
E_NOTIMPL : unsupported encoding method (for decoders)
another error code : some error. For example, it can be error code received from inStream or outStream function.
Parameters:
@@ -129,7 +130,8 @@ namespace NCoderPropID
kBlockSize2, // VT_UI4 or VT_UI8
kCheckSize, // VT_UI4 : size of digest in bytes
kFilter // VT_BSTR
kFilter, // VT_BSTR
kMemUse // VT_UI8
};
}
@@ -190,6 +192,12 @@ CODER_INTERFACE(ICompressGetInStreamProcessedSize2, 0x27)
STDMETHOD(GetInStreamProcessedSize2)(UInt32 streamIndex, UInt64 *value) PURE;
};
CODER_INTERFACE(ICompressSetMemLimit, 0x28)
{
STDMETHOD(SetMemLimit)(UInt64 memUsage) PURE;
};
CODER_INTERFACE(ICompressGetSubStreamSize, 0x30)
{

5
CPP/7zip/LzmaDec.mak Normal file
View File

@@ -0,0 +1,5 @@
!IF "$(CPU)" == "AMD64"
CFLAGS_C_SPEC = -D_LZMA_DEC_OPT
ASM_OBJS = $(ASM_OBJS) \
$O\LzmaDecOpt.obj
!ENDIF

View File

@@ -1217,7 +1217,12 @@ STDMETHODIMP CAgentFolder::GetFolderProperty(PROPID propID, PROPVARIANT *value)
NWindows::NCOM::CPropVariant prop;
if (propID == kpidReadOnly)
prop = _agentSpec->IsThereReadOnlyArc();
{
if (_agentSpec->Is_Attrib_ReadOnly())
prop = true;
else
prop = _agentSpec->IsThereReadOnlyArc();
}
else if (_proxy2)
{
const CProxyDir2 &dir = _proxy2->Dirs[_proxyDirIndex];
@@ -1507,11 +1512,18 @@ STDMETHODIMP CAgentFolder::Extract(const UInt32 *indices,
#endif
HRESULT result = _agentSpec->GetArchive()->Extract(&realIndices.Front(),
realIndices.Size(), testMode, extractCallback);
if (result == S_OK)
result = extractCallbackSpec->SetDirsTimes();
return result;
{
CArchiveExtractCallback_Closer ecsCloser(extractCallbackSpec);
HRESULT res = _agentSpec->GetArchive()->Extract(&realIndices.Front(),
realIndices.Size(), testMode, extractCallback);
HRESULT res2 = ecsCloser.Close();
if (res == S_OK)
res = res2;
return res;
}
COM_TRY_END
}
@@ -1557,6 +1569,7 @@ STDMETHODIMP CAgent::Open(
{
COM_TRY_BEGIN
_archiveFilePath = filePath;
_attrib = 0;
NFile::NFind::CFileInfo fi;
_isDeviceFile = false;
if (!inStream)
@@ -1565,6 +1578,7 @@ STDMETHODIMP CAgent::Open(
return ::GetLastError();
if (fi.IsDir())
return E_FAIL;
_attrib = fi.Attrib;
_isDeviceFile = fi.IsDevice;
}
CArcInfoEx archiverInfo0, archiverInfo1;

View File

@@ -241,6 +241,7 @@ public:
CAgentFolder *_agentFolder;
UString _archiveFilePath;
DWORD _attrib;
bool _isDeviceFile;
#ifndef EXTRACT_ONLY
@@ -252,6 +253,11 @@ public:
IInArchive *GetArchive() const { if ( _archiveLink.Arcs.IsEmpty()) return 0; return GetArc().Archive; }
bool CanUpdate() const;
bool Is_Attrib_ReadOnly() const
{
return _attrib != INVALID_FILE_ATTRIBUTES && (_attrib & FILE_ATTRIBUTE_READONLY);
}
bool IsThereReadOnlyArc() const
{
FOR_VECTOR (i, _archiveLink.Arcs)

View File

@@ -8,9 +8,17 @@
#ifndef UNDER_CE
#include <io.h>
#endif
#else
// for isatty()
#include <unistd.h>
#endif
#include <stdio.h>
#ifdef _7ZIP_LARGE_PAGES
#include "../../../../C/Alloc.h"
#endif
#include "../../../Common/ListFileUtils.h"
#include "../../../Common/StringConvert.h"
#include "../../../Common/StringToInt.h"
@@ -19,18 +27,20 @@
#include "../../../Windows/FileName.h"
#ifdef _WIN32
#include "../../../Windows/FileMapping.h"
#include "../../../Windows/MemoryLock.h"
#include "../../../Windows/Synchronization.h"
#endif
#include "ArchiveCommandLine.h"
#include "EnumDirItems.h"
#include "SortUtils.h"
#include "Update.h"
#include "UpdateAction.h"
extern bool g_CaseSensitive;
extern bool g_PathTrailReplaceMode;
bool g_LargePagesMode = false;
#ifdef UNDER_CE
#define MY_IS_TERMINAL(x) false;
@@ -60,15 +70,6 @@ static bool StringToUInt32(const wchar_t *s, UInt32 &v)
return *end == 0;
}
CArcCmdLineException::CArcCmdLineException(const char *a, const wchar_t *u)
{
(*this) += a;
if (u)
{
Add_LF();
(*this) += u;
}
}
int g_CodePage = -1;
@@ -124,6 +125,7 @@ enum Enum
kTechMode,
kShareForWrite,
kStopAfterOpenError,
kCaseSensitive,
kArcNameMode,
@@ -239,12 +241,13 @@ static const CSwitchForm kSwitchForms[] =
{ "si", NSwitchType::kString },
{ "so" },
{ "slp", NSwitchType::kMinus },
{ "slp", NSwitchType::kString },
{ "scs", NSwitchType::kString },
{ "scc", NSwitchType::kString },
{ "slt" },
{ "ssw" },
{ "sse" },
{ "ssc", NSwitchType::kMinus },
{ "sa", NSwitchType::kChar, false, 1, k_ArcNameMode_PostCharSet },
@@ -280,7 +283,6 @@ static const char * const kIncorrectListFile = "Incorrect item in listfile.\nChe
static const char * const kTerminalOutError = "I won't write compressed data to a terminal";
static const char * const kSameTerminalError = "I won't write data and program's messages to same stream";
static const char * const kEmptyFilePath = "Empty file path";
static const char * const kCannotFindArchive = "Cannot find archive";
bool CArcCommand::IsFromExtractGroup() const
{
@@ -613,84 +615,6 @@ static void AddSwitchWildcardsToCensor(
throw CArcCmdLineException(errorMessage, strings[i]);
}
#ifdef _WIN32
// This code converts all short file names to long file names.
static void ConvertToLongName(const UString &prefix, UString &name)
{
if (name.IsEmpty() || DoesNameContainWildcard(name))
return;
NFind::CFileInfo fi;
const FString path (us2fs(prefix + name));
#ifndef UNDER_CE
if (NFile::NName::IsDevicePath(path))
return;
#endif
if (fi.Find(path))
name = fs2us(fi.Name);
}
static void ConvertToLongNames(const UString &prefix, CObjectVector<NWildcard::CItem> &items)
{
FOR_VECTOR (i, items)
{
NWildcard::CItem &item = items[i];
if (item.Recursive || item.PathParts.Size() != 1)
continue;
if (prefix.IsEmpty() && item.IsDriveItem())
continue;
ConvertToLongName(prefix, item.PathParts.Front());
}
}
static void ConvertToLongNames(const UString &prefix, NWildcard::CCensorNode &node)
{
ConvertToLongNames(prefix, node.IncludeItems);
ConvertToLongNames(prefix, node.ExcludeItems);
unsigned i;
for (i = 0; i < node.SubNodes.Size(); i++)
{
UString &name = node.SubNodes[i].Name;
if (prefix.IsEmpty() && NWildcard::IsDriveColonName(name))
continue;
ConvertToLongName(prefix, name);
}
// mix folders with same name
for (i = 0; i < node.SubNodes.Size(); i++)
{
NWildcard::CCensorNode &nextNode1 = node.SubNodes[i];
for (unsigned j = i + 1; j < node.SubNodes.Size();)
{
const NWildcard::CCensorNode &nextNode2 = node.SubNodes[j];
if (nextNode1.Name.IsEqualTo_NoCase(nextNode2.Name))
{
nextNode1.IncludeItems += nextNode2.IncludeItems;
nextNode1.ExcludeItems += nextNode2.ExcludeItems;
node.SubNodes.Delete(j);
}
else
j++;
}
}
for (i = 0; i < node.SubNodes.Size(); i++)
{
NWildcard::CCensorNode &nextNode = node.SubNodes[i];
ConvertToLongNames(prefix + nextNode.Name + WCHAR_PATH_SEPARATOR, nextNode);
}
}
void ConvertToLongNames(NWildcard::CCensor &censor)
{
FOR_VECTOR (i, censor.Pairs)
{
NWildcard::CPair &pair = censor.Pairs[i];
ConvertToLongNames(pair.Prefix, pair.Head);
}
}
#endif
/*
static NUpdateArchive::NPairAction::EEnum GetUpdatePairActionType(int i)
{
@@ -920,9 +844,45 @@ void CArcCmdLineParser::Parse1(const UStringVector &commandStrings,
options.CaseSensitive = g_CaseSensitive;
}
options.LargePages = false;
#if defined(_WIN32) && !defined(UNDER_CE)
NSecurity::EnablePrivilege_SymLink();
#endif
// options.LargePages = false;
if (parser[NKey::kLargePages].ThereIs)
options.LargePages = !parser[NKey::kLargePages].WithMinus;
{
unsigned slp = 0;
const UString &s = parser[NKey::kLargePages].PostStrings[0];
if (s.IsEmpty())
slp = 1;
else if (s != L"-")
{
if (!StringToUInt32(s, slp))
throw CArcCmdLineException("Unsupported switch postfix for -slp", s);
}
#ifdef _7ZIP_LARGE_PAGES
if (slp >
#ifndef UNDER_CE
(unsigned)NSecurity::Get_LargePages_RiskLevel()
#else
0
#endif
)
{
SetLargePageSize();
// note: this process also can inherit that Privilege from parent process
g_LargePagesMode =
#if defined(_WIN32) && !defined(UNDER_CE)
NSecurity::EnablePrivilege_LockMemory();
#else
true;
#endif
}
#endif
}
#ifndef UNDER_CE
@@ -994,64 +954,6 @@ static Int32 FindCharset(const NCommandLineParser::CParser &parser, unsigned key
}
}
HRESULT EnumerateDirItemsAndSort(
NWildcard::CCensor &censor,
NWildcard::ECensorPathMode censorPathMode,
const UString &addPathPrefix,
UStringVector &sortedPaths,
UStringVector &sortedFullPaths,
CDirItemsStat &st,
IDirItemsCallback *callback)
{
FStringVector paths;
{
CDirItems dirItems;
dirItems.Callback = callback;
{
HRESULT res = EnumerateItems(censor, censorPathMode, addPathPrefix, dirItems);
st = dirItems.Stat;
RINOK(res);
}
FOR_VECTOR (i, dirItems.Items)
{
const CDirItem &dirItem = dirItems.Items[i];
if (!dirItem.IsDir())
paths.Add(dirItems.GetPhyPath(i));
}
}
if (paths.Size() == 0)
throw CArcCmdLineException(kCannotFindArchive);
UStringVector fullPaths;
unsigned i;
for (i = 0; i < paths.Size(); i++)
{
FString fullPath;
NFile::NDir::MyGetFullPathName(paths[i], fullPath);
fullPaths.Add(fs2us(fullPath));
}
CUIntVector indices;
SortFileNames(fullPaths, indices);
sortedPaths.ClearAndReserve(indices.Size());
sortedFullPaths.ClearAndReserve(indices.Size());
for (i = 0; i < indices.Size(); i++)
{
unsigned index = indices[i];
sortedPaths.AddInReserved(fs2us(paths[index]));
sortedFullPaths.AddInReserved(fullPaths[index]);
if (i > 0 && CompareFileNames(sortedFullPaths[i], sortedFullPaths[i - 1]) == 0)
throw CArcCmdLineException("Duplicate archive path:", sortedFullPaths[i]);
}
return S_OK;
}
static void SetBoolPair(NCommandLineParser::CParser &parser, unsigned switchID, CBoolPair &bp)
{
@@ -1299,6 +1201,8 @@ void CArcCmdLineParser::Parse2(CArcCmdLineOptions &options)
if (parser[NKey::kShareForWrite].ThereIs)
updateOptions.OpenShareForWrite = true;
if (parser[NKey::kStopAfterOpenError].ThereIs)
updateOptions.StopAfterOpenError = true;
updateOptions.PathMode = censorPathMode;

View File

@@ -6,14 +6,13 @@
#include "../../../Common/CommandLineParser.h"
#include "../../../Common/Wildcard.h"
#include "EnumDirItems.h"
#include "Extract.h"
#include "HashCalc.h"
#include "Update.h"
struct CArcCmdLineException: public UString
{
CArcCmdLineException(const char *a, const wchar_t *u = NULL);
};
typedef CMessagePathException CArcCmdLineException;
namespace NCommandType { enum EEnum
{
@@ -51,7 +50,7 @@ struct CArcCmdLineOptions
{
bool HelpMode;
bool LargePages;
// bool LargePages;
bool CaseSensitiveChange;
bool CaseSensitive;
@@ -110,7 +109,7 @@ struct CArcCmdLineOptions
UInt32 NumIterations;
CArcCmdLineOptions():
LargePages(false),
// LargePages(false),
CaseSensitiveChange(false),
CaseSensitive(false),
@@ -134,13 +133,4 @@ public:
void Parse2(CArcCmdLineOptions &options);
};
HRESULT EnumerateDirItemsAndSort(
NWildcard::CCensor &censor,
NWildcard::ECensorPathMode pathMode,
const UString &addPathPrefix,
UStringVector &sortedPaths,
UStringVector &sortedFullPaths,
CDirItemsStat &st,
IDirItemsCallback *callback);
#endif

View File

@@ -182,6 +182,7 @@ HRESULT CArchiveExtractCallback::PrepareHardLinks(const CRecordVector<UInt32> *r
#endif
CArchiveExtractCallback::CArchiveExtractCallback():
_arc(NULL),
WriteCTime(true),
WriteATime(true),
WriteMTime(true),
@@ -205,8 +206,8 @@ void CArchiveExtractCallback::Init(
const UStringVector &removePathParts, bool removePartsForAltStreams,
UInt64 packSize)
{
_extractedFolderPaths.Clear();
_extractedFolderIndices.Clear();
ClearExtractedDirsInfo();
_outFileStream.Release();
#ifdef SUPPORT_LINKS
_hardLinks.Clear();
@@ -368,9 +369,11 @@ void CArchiveExtractCallback::CreateComplexDirectory(const UStringVector &dirPat
}
}
HRESULT CArchiveExtractCallback::GetTime(int index, PROPID propID, FILETIME &filetime, bool &filetimeIsDefined)
HRESULT CArchiveExtractCallback::GetTime(UInt32 index, PROPID propID, FILETIME &filetime, bool &filetimeIsDefined)
{
filetimeIsDefined = false;
filetime.dwLowDateTime = 0;
filetime.dwHighDateTime = 0;
NCOM::CPropVariant prop;
RINOK(_arc->Archive->GetProperty(index, propID, &prop));
if (prop.vt == VT_FILETIME)
@@ -734,7 +737,8 @@ STDMETHODIMP CArchiveExtractCallback::GetStream(UInt32 index, ISequentialOutStre
return E_FAIL;
UString s;
CReparseAttr reparse;
isOkReparse = reparse.Parse((const Byte *)data, dataSize);
DWORD errorCode = 0;
isOkReparse = reparse.Parse((const Byte *)data, dataSize, errorCode);
if (isOkReparse)
{
isHardLink = false;
@@ -1030,14 +1034,36 @@ if (askExtractMode == NArchive::NExtract::NAskMode::kExtract && !_testMode)
{
FString fullPathNew;
CreateComplexDirectory(pathParts, fullPathNew);
if (_item.IsDir)
{
_extractedFolderPaths.Add(fullPathNew);
_extractedFolderIndices.Add(index);
SetDirTime(fullPathNew,
(WriteCTime && _fi.CTimeDefined) ? &_fi.CTime : NULL,
(WriteATime && _fi.ATimeDefined) ? &_fi.ATime : NULL,
(WriteMTime && _fi.MTimeDefined) ? &_fi.MTime : (_arc->MTimeDefined ? &_arc->MTime : NULL));
CDirPathTime &pt = _extractedFolders.AddNew();
pt.CTime = _fi.CTime;
pt.CTimeDefined = (WriteCTime && _fi.CTimeDefined);
pt.ATime = _fi.ATime;
pt.ATimeDefined = (WriteATime && _fi.ATimeDefined);
pt.MTimeDefined = false;
if (WriteMTime)
{
if (_fi.MTimeDefined)
{
pt.MTime = _fi.MTime;
pt.MTimeDefined = true;
}
else if (_arc->MTimeDefined)
{
pt.MTime = _arc->MTime;
pt.MTimeDefined = true;
}
}
pt.Path = fullPathNew;
pt.SetDirTime();
}
}
}
@@ -1270,7 +1296,8 @@ if (askExtractMode == NArchive::NExtract::NAskMode::kExtract && !_testMode)
if (FillLinkData(data, fs2us(existPath), !isJunction))
{
CReparseAttr attr;
if (!attr.Parse(data, data.Size()))
DWORD errorCode = 0;
if (!attr.Parse(data, data.Size(), errorCode))
{
RINOK(SendMessageError("Internal error for symbolic link file", us2fs(_item.Path)));
// return E_FAIL;
@@ -1447,6 +1474,33 @@ STDMETHODIMP CArchiveExtractCallback::PrepareOperation(Int32 askExtractMode)
}
HRESULT CArchiveExtractCallback::CloseFile()
{
if (!_outFileStream)
return S_OK;
HRESULT hres = S_OK;
_outFileStreamSpec->SetTime(
(WriteCTime && _fi.CTimeDefined) ? &_fi.CTime : NULL,
(WriteATime && _fi.ATimeDefined) ? &_fi.ATime : NULL,
(WriteMTime && _fi.MTimeDefined) ? &_fi.MTime : (_arc->MTimeDefined ? &_arc->MTime : NULL));
const UInt64 processedSize = _outFileStreamSpec->ProcessedSize;
if (_fileLengthWasSet && _curSize > processedSize)
{
bool res = _outFileStreamSpec->File.SetLength(processedSize);
_fileLengthWasSet = res;
if (!res)
hres = SendMessageError_with_LastError(kCantSetFileLen, us2fs(_item.Path));
}
_curSize = processedSize;
_curSizeDefined = true;
RINOK(_outFileStreamSpec->Close());
_outFileStream.Release();
return hres;
}
STDMETHODIMP CArchiveExtractCallback::SetOperationResult(Int32 opRes)
{
COM_TRY_BEGIN
@@ -1475,27 +1529,7 @@ STDMETHODIMP CArchiveExtractCallback::SetOperationResult(Int32 opRes)
#endif
if (_outFileStream)
{
HRESULT hres = S_OK;
_outFileStreamSpec->SetTime(
(WriteCTime && _fi.CTimeDefined) ? &_fi.CTime : NULL,
(WriteATime && _fi.ATimeDefined) ? &_fi.ATime : NULL,
(WriteMTime && _fi.MTimeDefined) ? &_fi.MTime : (_arc->MTimeDefined ? &_arc->MTime : NULL));
const UInt64 processedSize = _outFileStreamSpec->ProcessedSize;
if (_fileLengthWasSet && _curSize > processedSize)
{
bool res = _outFileStreamSpec->File.SetLength(processedSize);
_fileLengthWasSet = res;
if (!res)
hres = SendMessageError_with_LastError(kCantSetFileLen, us2fs(_item.Path));
}
_curSize = processedSize;
_curSizeDefined = true;
RINOK(_outFileStreamSpec->Close());
_outFileStream.Release();
RINOK(hres);
}
RINOK(CloseFile());
#ifdef _USE_SECURITY_CODE
if (!_stdOutMode && _extractMode && _ntOptions.NtSecurity.Val && _arc->GetRawProps)
@@ -1592,71 +1626,66 @@ STDMETHODIMP CArchiveExtractCallback::CryptoGetTextPassword(BSTR *password)
}
struct CExtrRefSortPair
{
unsigned Len;
unsigned Index;
int Compare(const CExtrRefSortPair &a) const;
};
#define RINOZ(x) { int __tt = (x); if (__tt != 0) return __tt; }
int CExtrRefSortPair::Compare(const CExtrRefSortPair &a) const
{
RINOZ(-MyCompare(Len, a.Len));
return MyCompare(Index, a.Index);
}
static unsigned GetNumSlashes(const FChar *s)
void CDirPathSortPair::SetNumSlashes(const FChar *s)
{
for (unsigned numSlashes = 0;;)
{
FChar c = *s++;
if (c == 0)
return numSlashes;
{
Len = numSlashes;
return;
}
if (IS_PATH_SEPAR(c))
numSlashes++;
}
}
bool CDirPathTime::SetDirTime()
{
return NDir::SetDirTime(Path,
CTimeDefined ? &CTime : NULL,
ATimeDefined ? &ATime : NULL,
MTimeDefined ? &MTime : NULL);
}
HRESULT CArchiveExtractCallback::SetDirsTimes()
{
CRecordVector<CExtrRefSortPair> pairs;
pairs.ClearAndSetSize(_extractedFolderPaths.Size());
if (!_arc)
return S_OK;
CRecordVector<CDirPathSortPair> pairs;
pairs.ClearAndSetSize(_extractedFolders.Size());
unsigned i;
for (i = 0; i < _extractedFolderPaths.Size(); i++)
for (i = 0; i < _extractedFolders.Size(); i++)
{
CExtrRefSortPair &pair = pairs[i];
CDirPathSortPair &pair = pairs[i];
pair.Index = i;
pair.Len = GetNumSlashes(_extractedFolderPaths[i]);
pair.SetNumSlashes(_extractedFolders[i].Path);
}
pairs.Sort2();
for (i = 0; i < pairs.Size(); i++)
{
int pairIndex = pairs[i].Index;
int index = _extractedFolderIndices[pairIndex];
FILETIME CTime;
FILETIME ATime;
FILETIME MTime;
bool CTimeDefined;
bool ATimeDefined;
bool MTimeDefined;
RINOK(GetTime(index, kpidCTime, CTime, CTimeDefined));
RINOK(GetTime(index, kpidATime, ATime, ATimeDefined));
RINOK(GetTime(index, kpidMTime, MTime, MTimeDefined));
// printf("\n%S", _extractedFolderPaths[pairIndex]);
SetDirTime(_extractedFolderPaths[pairIndex],
(WriteCTime && CTimeDefined) ? &CTime : NULL,
(WriteATime && ATimeDefined) ? &ATime : NULL,
(WriteMTime && MTimeDefined) ? &MTime : (_arc->MTimeDefined ? &_arc->MTime : NULL));
_extractedFolders[pairs[i].Index].SetDirTime();
// if (!) return GetLastError();
}
ClearExtractedDirsInfo();
return S_OK;
}
HRESULT CArchiveExtractCallback::CloseArc()
{
HRESULT res = CloseFile();
HRESULT res2 = SetDirsTimes();
if (res == S_OK)
res = res2;
_arc = NULL;
return res;
}

View File

@@ -151,6 +151,25 @@ struct CIndexToPathPair
#endif
struct CDirPathTime
{
FILETIME CTime;
FILETIME ATime;
FILETIME MTime;
bool CTimeDefined;
bool ATimeDefined;
bool MTimeDefined;
FString Path;
bool SetDirTime();
};
class CArchiveExtractCallback:
public IArchiveExtractCallback,
public IArchiveExtractCallbackMessage,
@@ -241,15 +260,14 @@ class CArchiveExtractCallback:
UInt64 _progressTotal;
bool _progressTotal_Defined;
FStringVector _extractedFolderPaths;
CRecordVector<UInt32> _extractedFolderIndices;
CObjectVector<CDirPathTime> _extractedFolders;
#if defined(_WIN32) && !defined(UNDER_CE) && !defined(_SFX)
bool _saclEnabled;
#endif
void CreateComplexDirectory(const UStringVector &dirPathParts, FString &fullPath);
HRESULT GetTime(int index, PROPID propID, FILETIME &filetime, bool &filetimeIsDefined);
HRESULT GetTime(UInt32 index, PROPID propID, FILETIME &filetime, bool &filetimeIsDefined);
HRESULT GetUnpackSize();
HRESULT SendMessageError(const char *message, const FString &path);
@@ -343,9 +361,43 @@ public:
}
#endif
HRESULT CloseArc();
private:
void ClearExtractedDirsInfo()
{
_extractedFolders.Clear();
}
HRESULT CloseFile();
HRESULT SetDirsTimes();
};
struct CArchiveExtractCallback_Closer
{
CArchiveExtractCallback *_ref;
CArchiveExtractCallback_Closer(CArchiveExtractCallback *ref): _ref(ref) {}
HRESULT Close()
{
HRESULT res = S_OK;
if (_ref)
{
res = _ref->CloseArc();
_ref = NULL;
}
return res;
}
~CArchiveExtractCallback_Closer()
{
Close();
}
};
bool CensorNode_CheckPath(const NWildcard::CCensorNode &node, const CReadArcItem &item);
#endif

View File

@@ -1183,9 +1183,11 @@ static HRESULT MethodBench(
COneMethodInfo method = method2;
UInt64 methodId;
UInt32 numStreams;
if (!FindMethod(
int codecIndex = FindMethod_Index(
EXTERNAL_CODECS_LOC_VARS
method.MethodName, methodId, numStreams))
method.MethodName, true,
methodId, numStreams);
if (codecIndex < 0)
return E_NOTIMPL;
if (numStreams != 1)
return E_INVALIDARG;
@@ -1222,7 +1224,7 @@ static HRESULT MethodBench(
{
CCreatedCoder cod;
RINOK(CreateCoder(EXTERNAL_CODECS_LOC_VARS methodId, true, encoder._encoderFilter, cod));
RINOK(CreateCoder_Index(EXTERNAL_CODECS_LOC_VARS codecIndex, true, encoder._encoderFilter, cod));
encoder._encoder = cod.Coder;
if (!encoder._encoder && !encoder._encoderFilter)
return E_NOTIMPL;
@@ -1239,7 +1241,7 @@ static HRESULT MethodBench(
{
CCreatedCoder cod;
CMyComPtr<ICompressCoder> &decoder = encoder._decoders[j];
RINOK(CreateCoder(EXTERNAL_CODECS_LOC_VARS methodId, false, encoder._decoderFilter, cod));
RINOK(CreateCoder_Id(EXTERNAL_CODECS_LOC_VARS methodId, false, encoder._decoderFilter, cod));
decoder = cod.Coder;
if (!encoder._decoderFilter && !decoder)
return E_NOTIMPL;
@@ -2600,7 +2602,8 @@ static void SysInfo_To_String(AString &s, const SYSTEM_INFO &si)
PrintHex(s, si.wProcessorLevel);
s += ".";
PrintHex(s, si.wProcessorRevision);
if (si.dwActiveProcessorMask + 1 != ((UInt64)1 << si.dwNumberOfProcessors))
if ((UInt64)si.dwActiveProcessorMask + 1 != ((UInt64)1 << si.dwNumberOfProcessors))
if ((UInt64)si.dwActiveProcessorMask + 1 != 0 || si.dwNumberOfProcessors != sizeof(UInt64) * 8)
{
s += " act:";
PrintHex(s, si.dwActiveProcessorMask);
@@ -2684,13 +2687,15 @@ void GetCpuName(AString &s)
AString s2;
x86cpuid_to_String(cpuid, s2);
s += s2;
return;
}
else
{
#ifdef MY_CPU_AMD64
s += "x64";
#else
s += "x86";
#endif
}
}
#else
@@ -2701,6 +2706,9 @@ void GetCpuName(AString &s)
#endif
#endif
if (g_LargePagesMode)
s += " (LP)";
}
@@ -2723,6 +2731,27 @@ void GetCpuFeatures(AString &s)
}
#ifdef _WIN32
#ifndef UNDER_CE
typedef void (WINAPI * Func_RtlGetVersion) (OSVERSIONINFOEXW *);
static BOOL My_RtlGetVersion(OSVERSIONINFOEXW *vi)
{
HMODULE ntdll = ::GetModuleHandleW(L"ntdll.dll");
if (!ntdll)
return FALSE;
Func_RtlGetVersion func = (Func_RtlGetVersion)GetProcAddress(ntdll, "RtlGetVersion");
if (!func)
return FALSE;
func(vi);
return TRUE;
}
#endif
#endif
HRESULT Bench(
DECL_EXTERNAL_CODECS_LOC_VARS
IBenchPrintCallback *printCallback,
@@ -2857,6 +2886,30 @@ HRESULT Bench(
if (printCallback)
{
#ifdef _WIN32
#ifndef UNDER_CE
{
AString s;
// OSVERSIONINFO vi;
OSVERSIONINFOEXW vi;
vi.dwOSVersionInfoSize = sizeof(vi);
// if (::GetVersionEx(&vi))
if (My_RtlGetVersion(&vi))
{
s += "Windows";
if (vi.dwPlatformId != VER_PLATFORM_WIN32_NT)
s.Add_UInt32(vi.dwPlatformId);
s += " "; s.Add_UInt32(vi.dwMajorVersion);
s += "."; s.Add_UInt32(vi.dwMinorVersion);
s += " "; s.Add_UInt32(vi.dwBuildNumber);
// s += " "; s += GetAnsiString(vi.szCSDVersion);
}
printCallback->Print(s);
printCallback->NewLine();
}
#endif
#endif
{
AString s1, s2;
GetSysInfo(s1, s2);

View File

@@ -13,6 +13,7 @@
#include "../../../Windows/ErrorMsg.h"
#include "../../../Windows/FileDir.h"
#include "../../../Windows/FileMapping.h"
#include "../../../Windows/MemoryLock.h"
#include "../../../Windows/ProcessUtils.h"
#include "../../../Windows/Synchronization.h"
@@ -96,6 +97,9 @@ static HRESULT Call7zGui(const UString &params,
static void AddLagePagesSwitch(UString &params)
{
if (ReadLockMemoryEnable())
#ifndef UNDER_CE
if (NSecurity::Get_LargePages_RiskLevel() == 0)
#endif
params += " -slp";
}

View File

@@ -1,10 +1,10 @@
// CompressCall.cpp
// CompressCall2.cpp
#include "StdAfx.h"
#include "../../../Common/MyException.h"
#include "../../UI/common/ArchiveCommandLine.h"
#include "../../UI/Common/EnumDirItems.h"
#include "../../UI/GUI/BenchmarkDialog.h"
#include "../../UI/GUI/ExtractGUI.h"
@@ -20,6 +20,7 @@ extern HWND g_HWND;
#define MY_TRY_BEGIN HRESULT result; try {
#define MY_TRY_FINISH } \
catch(CSystemException &e) { result = e.ErrorCode; } \
catch(UString &s) { ErrorMessage(s); result = E_FAIL; } \
catch(...) { result = E_FAIL; } \
if (result != S_OK && result != E_ABORT) \
ErrorMessageHRESULT(result);

Some files were not shown because too many files have changed in this diff Show More