This commit is contained in:
Igor Pavlov
2003-12-11 00:00:00 +00:00
committed by Kornel Lesiński
commit 8c1b5c7b7e
982 changed files with 118799 additions and 0 deletions

108
7zip/Compress/LZ/HashChain/HC.h Executable file
View File

@@ -0,0 +1,108 @@
// HC.h
// #pragma once
// #ifndef __HC_H
// #define __HC_H
#include "../LZInWindow.h"
#include "Common/Types.h"
#include "Windows/Defs.h"
namespace HC_NAMESPACE {
// #define __USE_3_BYTES
#ifdef __USE_3_BYTES
#pragma pack(push, PragmaBinTree)
#pragma pack(push, 1)
struct CIndex
{
BYTE Data[3];
CIndex(){}
CIndex(UINT32 aValue)
{
Data[0] = aValue & 0xFF;
Data[1] = (aValue >> 8) & 0xFF;
Data[2] = (aValue >> 16) & 0xFF;
}
operator UINT32() const { return (*((const UINT32 *)Data)) & 0xFFFFFF; }
};
const UINT32 kMaxValForNormalize = CIndex(-1);
#pragma pack(pop)
#pragma pack(pop, PragmaBinTree)
#else
typedef UINT32 CIndex;
const UINT32 kMaxValForNormalize = (UINT32(1) << 31) - 1;
#endif
// #define HASH_ARRAY_2
// #ifdef HASH_ARRAY_2
// #define HASH_ARRAY_3
// #else
// #define HASH_ZIP
// #endif
class CInTree: public CLZInWindow
{
UINT32 _cyclicBufferPos;
UINT32 _cyclicBufferSize;
UINT32 _historySize;
UINT32 _matchMaxLen;
CIndex *_hash;
#ifdef HASH_ARRAY_2
CIndex *_hash2;
#ifdef HASH_ARRAY_3
CIndex *_hash3;
#endif
#endif
CIndex *_chain;
UINT32 _cutValue;
void NormalizeLinks(CIndex *anArray, UINT32 aNumItems, UINT32 aSubValue);
void Normalize();
void FreeMemory();
public:
CInTree();
~CInTree();
HRESULT Create(UINT32 aSizeHistory, UINT32 aKeepAddBufferBefore, UINT32 aMatchMaxLen,
UINT32 aKeepAddBufferAfter, UINT32 _dwSizeReserv = (1<<17));
HRESULT Init(ISequentialInStream *aStream);
void SetCutValue(UINT32 aCutValue) { _cutValue = aCutValue; }
UINT32 GetLongestMatch(UINT32 *aDistances);
void DummyLongestMatch();
HRESULT MovePos()
{
_cyclicBufferPos++;
if (_cyclicBufferPos >= _cyclicBufferSize)
_cyclicBufferPos = 0;
RINOK(CLZInWindow::MovePos());
if (_pos == kMaxValForNormalize)
Normalize();
return S_OK;
}
};
}
// #endif

View File

@@ -0,0 +1,18 @@
// HC2.h
#pragma once
#ifndef __HC2__H
#define __HC2__H
#undef HC_CLSID
#define HC_CLSID CLSID_CMatchFinderHC2
#undef HC_NAMESPACE
#define HC_NAMESPACE NHC2
#include "HCMF.h"
#include "HCMFMain.h"
#endif

View File

@@ -0,0 +1,22 @@
// HC3.h
#pragma once
#ifndef __HC3__H
#define __HC3__H
#undef HC_CLSID
#define HC_CLSID CLSID_CMatchFinderHC3
#undef HC_NAMESPACE
#define HC_NAMESPACE NHC3
#define HASH_ARRAY_2
#include "HCMF.h"
#include "HCMFMain.h"
#undef HASH_ARRAY_2
#endif

View File

@@ -0,0 +1,24 @@
// HC4.h
#pragma once
#ifndef __HC4__H
#define __HC4__H
#undef HC_CLSID
#define HC_CLSID CLSID_CMatchFinderHC4
#undef HC_NAMESPACE
#define HC_NAMESPACE NHC4
#define HASH_ARRAY_2
#define HASH_ARRAY_3
#include "HCMF.h"
#include "HCMFMain.h"
#undef HASH_ARRAY_2
#undef HASH_ARRAY_3
#endif

View File

@@ -0,0 +1,26 @@
// HC4b.h
#pragma once
#ifndef __HC4B__H
#define __HC4B__H
#undef HC_CLSID
#define HC_CLSID CLSID_CMatchFinderHC4b
#undef HC_NAMESPACE
#define HC_NAMESPACE NHC4b
#define HASH_ARRAY_2
#define HASH_ARRAY_3
#define HASH_BIG
#include "HCMF.h"
#include "HCMFMain.h"
#undef HASH_ARRAY_2
#undef HASH_ARRAY_3
#undef HASH_BIG
#endif

111
7zip/Compress/LZ/HashChain/HCMF.h Executable file
View File

@@ -0,0 +1,111 @@
// HCMF.h
// #pragma once
// #ifndef __HCMF_H
// #define __HCMF_H
#include "Common/MyCom.h"
// #include "../../../Interface/CompressInterface.h"
#include "HC.h"
namespace HC_NAMESPACE {
#undef kIDByte
#undef kIDString
#ifdef HASH_ARRAY_2
#ifdef HASH_ARRAY_3
#ifdef HASH_BIG
#define kIDByte 0x4
#define kIDString TEXT("4b")
#else
#define kIDByte 0x3
#define kIDString TEXT("4")
#endif
#else
#define kIDByte 0x2
#define kIDString TEXT("3")
#endif
#else
#ifdef HASH_ZIP
#define kIDByte 0x0
#define kIDString TEXT("3Z")
#else
#define kIDByte 0x1
#define kIDString TEXT("2")
#endif
#endif
#undef kIDUse3BytesByte
#undef kIDUse3BytesString
#ifdef __USE_3_BYTES
#define kIDUse3BytesByte 0x80
#define kIDUse3BytesString TEXT("T")
#else
#define kIDUse3BytesByte 0x00
#define kIDUse3BytesString TEXT("")
#endif
// #undef kIDStringFull
// #define kIDStringFull TEXT("Compress.MatchFinderHC") kIDString kIDUse3BytesString
// {23170F69-40C1-278C-03XX-0000000000}
DEFINE_GUID(HC_CLSID,
0x23170F69, 0x40C1, 0x278C, 0x03, kIDByte | kIDUse3BytesByte,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
class CInTree2: public CInTree
{
CMyComPtr<IMatchFinderCallback> m_Callback;
virtual void BeforeMoveBlock();
virtual void AfterMoveBlock();
public:
void SetCallback(IMatchFinderCallback *aCallback)
{
m_Callback = aCallback;
}
};
class CMatchFinderHC:
public IMatchFinder,
public IMatchFinderSetCallback,
public CMyUnknownImp
{
MY_UNKNOWN_IMP1(IMatchFinderSetCallback)
STDMETHOD(Init)(ISequentialInStream *aStream);
STDMETHOD_(void, ReleaseStream)();
STDMETHOD(MovePos)();
STDMETHOD_(BYTE, GetIndexByte)(UINT32 anIndex);
STDMETHOD_(UINT32, GetMatchLen)(UINT32 aIndex, UINT32 aBack, UINT32 aLimit);
STDMETHOD_(UINT32, GetNumAvailableBytes)();
STDMETHOD_(const BYTE *, GetPointerToCurrentPos)();
STDMETHOD(Create)(UINT32 aSizeHistory,
UINT32 aKeepAddBufferBefore, UINT32 aMatchMaxLen,
UINT32 aKeepAddBufferAfter);
STDMETHOD_(UINT32, GetLongestMatch)(UINT32 *aDistances);
STDMETHOD_(void, DummyLongestMatch)();
// IMatchFinderSetCallback
STDMETHOD(SetCallback)(IMatchFinderCallback *aCallback);
private:
// UINT32 m_WindowReservSize;
CInTree2 m_MatchFinder;
public:
// CMatchFinderHC(): m_WindowReservSize((1 << 19) + 256) {};
void SetCutValue(UINT32 aCutValue)
{ m_MatchFinder.SetCutValue(aCutValue); }
/*
void SetWindowReservSize(UINT32 aReservWindowSize)
{ m_WindowReservSize = aReservWindowSize; }
*/
};
}
// #endif

View File

@@ -0,0 +1,78 @@
// HCMFMain.h
#include "HCMain.h"
namespace HC_NAMESPACE {
void CInTree2::BeforeMoveBlock()
{
if (m_Callback)
m_Callback->BeforeChangingBufferPos();
CInTree::BeforeMoveBlock();
}
void CInTree2::AfterMoveBlock()
{
if (m_Callback)
m_Callback->AfterChangingBufferPos();
CInTree::AfterMoveBlock();
}
STDMETHODIMP CMatchFinderHC::Init(ISequentialInStream *aStream)
{ return m_MatchFinder.Init(aStream); }
STDMETHODIMP_(void) CMatchFinderHC::ReleaseStream()
{
// m_MatchFinder.ReleaseStream();
}
STDMETHODIMP CMatchFinderHC::MovePos()
{ return m_MatchFinder.MovePos(); }
STDMETHODIMP_(BYTE) CMatchFinderHC::GetIndexByte(UINT32 anIndex)
{ return m_MatchFinder.GetIndexByte(anIndex); }
STDMETHODIMP_(UINT32) CMatchFinderHC::GetMatchLen(UINT32 aIndex,
UINT32 aBack, UINT32 aLimit)
{ return m_MatchFinder.GetMatchLen(aIndex, aBack, aLimit); }
STDMETHODIMP_(UINT32) CMatchFinderHC::GetNumAvailableBytes()
{ return m_MatchFinder.GetNumAvailableBytes(); }
STDMETHODIMP CMatchFinderHC::Create(UINT32 aSizeHistory,
UINT32 aKeepAddBufferBefore, UINT32 aMatchMaxLen,
UINT32 aKeepAddBufferAfter)
{
UINT32 aWindowReservSize = (aSizeHistory + aKeepAddBufferBefore +
aMatchMaxLen + aKeepAddBufferAfter) / 2 + 256;
try
{
return m_MatchFinder.Create(aSizeHistory, aKeepAddBufferBefore, aMatchMaxLen,
aKeepAddBufferAfter, aWindowReservSize);
}
catch(...)
{
return E_OUTOFMEMORY;
}
}
STDMETHODIMP_(UINT32) CMatchFinderHC::GetLongestMatch(UINT32 *aDistances)
{ return m_MatchFinder.GetLongestMatch(aDistances); }
STDMETHODIMP_(void) CMatchFinderHC::DummyLongestMatch()
{ m_MatchFinder.DummyLongestMatch(); }
STDMETHODIMP_(const BYTE *) CMatchFinderHC::GetPointerToCurrentPos()
{
return m_MatchFinder.GetPointerToCurrentPos();
}
// IMatchFinderSetCallback
STDMETHODIMP CMatchFinderHC::SetCallback(IMatchFinderCallback *aCallback)
{
m_MatchFinder.SetCallback(aCallback);
return S_OK;
}
}

View File

@@ -0,0 +1,436 @@
// HC.h
// #include "StdAfx.h"
// #include "BinTree.h"
#include "Common/NewHandler.h"
#include "Common/Defs.h"
#include "Common/CRC.h"
namespace HC_NAMESPACE {
#ifdef HASH_ARRAY_2
static const UINT32 kHash2Size = 1 << 10;
#ifdef HASH_ARRAY_3
static const UINT32 kNumHashDirectBytes = 0;
static const UINT32 kNumHashBytes = 4;
static const UINT32 kHash3Size = 1 << 18;
#ifdef HASH_BIG
static const UINT32 kHashSize = 1 << 23;
#else
static const UINT32 kHashSize = 1 << 20;
#endif
#else
static const UINT32 kNumHashBytes = 3;
// static const UINT32 kNumHashDirectBytes = 3;
// static const UINT32 kHashSize = 1 << (8 * kNumHashBytes);
static const UINT32 kNumHashDirectBytes = 0;
static const UINT32 kHashSize = 1 << (16);
#endif
#else
#ifdef HASH_ZIP
static const UINT32 kNumHashDirectBytes = 0;
static const UINT32 kNumHashBytes = 3;
static const UINT32 kHashSize = 1 << 16;
#else
static const UINT32 kNumHashDirectBytes = 2;
static const UINT32 kNumHashBytes = 2;
static const UINT32 kHashSize = 1 << (8 * kNumHashBytes);
#endif
#endif
CInTree::CInTree():
#ifdef HASH_ARRAY_2
_hash2(0),
#ifdef HASH_ARRAY_3
_hash3(0),
#endif
#endif
_hash(0),
_chain(0),
_cutValue(16)
{
}
void CInTree::FreeMemory()
{
#ifdef WIN32
if (_chain != 0)
VirtualFree(_chain, 0, MEM_RELEASE);
if (_hash != 0)
VirtualFree(_hash, 0, MEM_RELEASE);
#else
delete []_chain;
delete []_hash;
#endif
_chain = 0;
_hash = 0;
CLZInWindow::Free();
}
CInTree::~CInTree()
{
FreeMemory();
}
HRESULT CInTree::Create(UINT32 aSizeHistory, UINT32 aKeepAddBufferBefore,
UINT32 aMatchMaxLen, UINT32 aKeepAddBufferAfter, UINT32 aSizeReserv)
{
FreeMemory();
try
{
CLZInWindow::Create(aSizeHistory + aKeepAddBufferBefore,
aMatchMaxLen + aKeepAddBufferAfter, aSizeReserv);
if (_blockSize + 256 > kMaxValForNormalize)
return E_INVALIDARG;
_historySize = aSizeHistory;
_matchMaxLen = aMatchMaxLen;
_cyclicBufferSize = aSizeHistory + 1;
UINT32 aSize = kHashSize;
#ifdef HASH_ARRAY_2
aSize += kHash2Size;
#ifdef HASH_ARRAY_3
aSize += kHash3Size;
#endif
#endif
#ifdef WIN32
_chain = (CIndex *)::VirtualAlloc(0, (_cyclicBufferSize + 1) * sizeof(CIndex), MEM_COMMIT, PAGE_READWRITE);
if (_chain == 0)
throw CNewException();
_hash = (CIndex *)::VirtualAlloc(0, (aSize + 1) * sizeof(CIndex), MEM_COMMIT, PAGE_READWRITE);
if (_hash == 0)
throw CNewException();
#else
_chain = new CIndex[_cyclicBufferSize + 1];
_hash = new CIndex[aSize + 1];
#endif
// m_RightBase = &m_LeftBase[_blockSize];
// _hash = &m_RightBase[_blockSize];
#ifdef HASH_ARRAY_2
_hash2 = &_hash[kHashSize];
#ifdef HASH_ARRAY_3
_hash3 = &_hash2[kHash2Size];
#endif
#endif
return S_OK;
}
catch(...)
{
FreeMemory();
return E_OUTOFMEMORY;
}
}
static const UINT32 kEmptyHashValue = 0;
HRESULT CInTree::Init(ISequentialInStream *aStream)
{
RINOK(CLZInWindow::Init(aStream));
int i;
for(i = 0; i < kHashSize; i++)
_hash[i] = kEmptyHashValue;
#ifdef HASH_ARRAY_2
for(i = 0; i < kHash2Size; i++)
_hash2[i] = kEmptyHashValue;
#ifdef HASH_ARRAY_3
for(i = 0; i < kHash3Size; i++)
_hash3[i] = kEmptyHashValue;
#endif
#endif
_cyclicBufferPos = 0;
ReduceOffsets(0 - 1);
return S_OK;
}
#ifdef HASH_ARRAY_2
#ifdef HASH_ARRAY_3
inline UINT32 Hash(const BYTE *pointer, UINT32 &hash2Value, UINT32 &aHash3Value)
{
UINT32 temp = CCRC::Table[pointer[0]] ^ pointer[1];
hash2Value = temp & (kHash2Size - 1);
aHash3Value = (temp ^ (UINT32(pointer[2]) << 8)) & (kHash3Size - 1);
return (temp ^ (UINT32(pointer[2]) << 8) ^ (CCRC::Table[pointer[3]] << 5)) &
(kHashSize - 1);
}
#else // no HASH_ARRAY_3
inline UINT32 Hash(const BYTE *pointer, UINT32 &hash2Value)
{
UINT32 temp = CCRC::Table[pointer[0]] ^ pointer[1];
hash2Value = temp & (kHash2Size - 1);
return (temp ^ (UINT32(pointer[2]) << 8)) & (kHashSize - 1);;
}
#endif // HASH_ARRAY_3
#else // no HASH_ARRAY_2
#ifdef HASH_ZIP
inline UINT32 Hash(const BYTE *pointer)
{
return ((UINT32(pointer[0]) << 8) ^
CCRC::Table[pointer[1]] ^ pointer[2]) & (kHashSize - 1);
}
#else // no HASH_ZIP
inline UINT32 Hash(const BYTE *pointer)
{
return pointer[0] ^ (UINT32(pointer[1]) << 8);
}
#endif // HASH_ZIP
#endif // HASH_ARRAY_2
UINT32 CInTree::GetLongestMatch(UINT32 *aDistances)
{
UINT32 aCurrentLimit;
if (_pos + _matchMaxLen <= _streamPos)
aCurrentLimit = _matchMaxLen;
else
{
aCurrentLimit = _streamPos - _pos;
if(aCurrentLimit < kNumHashBytes)
return 0;
}
UINT32 aMatchMinPos = (_pos > _historySize) ? (_pos - _historySize) : 1;
BYTE *aCur = _buffer + _pos;
UINT32 aMatchHashLenMax = 0;
#ifdef HASH_ARRAY_2
UINT32 hash2Value;
#ifdef HASH_ARRAY_3
UINT32 aHash3Value;
UINT32 hashValue = Hash(aCur, hash2Value, aHash3Value);
#else // no HASH_ARRAY_3
UINT32 hashValue = Hash(aCur, hash2Value);
#endif // HASH_ARRAY_3
#else // no HASH_ARRAY_2
UINT32 hashValue = Hash(aCur);
#endif
#ifdef HASH_ARRAY_2
UINT32 aCurMatch2 = _hash2[hash2Value];
_hash2[hash2Value] = _pos;
bool aMatchLen2Exist = false;
UINT32 aLen2Distance = 0;
if(aCurMatch2 >= aMatchMinPos)
{
if (_buffer[aCurMatch2] == aCur[0])
{
aLen2Distance = _pos - aCurMatch2 - 1;
aMatchHashLenMax = 2;
aMatchLen2Exist = true;
}
}
#ifdef HASH_ARRAY_3
UINT32 aCurMatch3 = _hash3[aHash3Value];
_hash3[aHash3Value] = _pos;
UINT32 aMatchLen3Exist = false;
UINT32 aLen3Distance = 0;
if(aCurMatch3 >= aMatchMinPos)
{
if (_buffer[aCurMatch3] == aCur[0])
{
aLen3Distance = _pos - aCurMatch3 - 1;
aMatchHashLenMax = 3;
aMatchLen3Exist = true;
if (aMatchLen2Exist)
{
if (aLen3Distance < aLen2Distance)
aLen2Distance = aLen3Distance;
}
else
{
aLen2Distance = aLen3Distance;
aMatchLen2Exist = true;
}
}
}
#endif
#endif
UINT32 aCurMatch = _hash[hashValue];
_hash[hashValue] = _pos;
if(aCurMatch < aMatchMinPos)
{
_chain[_cyclicBufferPos] = kEmptyHashValue;
#ifdef HASH_ARRAY_2
aDistances[2] = aLen2Distance;
#ifdef HASH_ARRAY_3
aDistances[3] = aLen3Distance;
#endif
#endif
return aMatchHashLenMax;
}
_chain[_cyclicBufferPos] = aCurMatch;
#ifdef HASH_ARRAY_2
#ifndef HASH_ARRAY_3
if (aMatchLen2Exist)
aDistances[2] = aLen2Distance;
else
if (kNumHashDirectBytes >= 2)
aDistances[2] = _pos - aCurMatch - 1;
#endif
#endif
UINT32 aMax, aMinSame;
aMax = aMinSame = kNumHashDirectBytes;
aDistances[aMax] = _pos - aCurMatch - 1;
for(UINT32 aCount = _cutValue; aCount > 0; aCount--)
{
BYTE *pby1 = _buffer + aCurMatch;
UINT32 aCurrentLen;
for(aCurrentLen = aMinSame; aCurrentLen < aCurrentLimit; aCurrentLen++/*, dwComps++*/)
if (pby1[aCurrentLen] != aCur[aCurrentLen])
break;
if (aCurrentLen > aMax)
{
UINT32 dwBack = _pos - aCurMatch - 1;
for(UINT32 dwLen = aMax + 1; dwLen <= aCurrentLen; dwLen++)
aDistances[dwLen] = dwBack;
aMax = aCurrentLen;
}
if(aCurrentLen == aCurrentLimit)
break;
UINT32 aDelta = _pos - aCurMatch;
UINT32 aCyclicPos = (aDelta <= _cyclicBufferPos) ?
(_cyclicBufferPos - aDelta):
(_cyclicBufferPos - aDelta + _cyclicBufferSize);
aCurMatch = _chain[aCyclicPos];
if(aCurMatch < aMatchMinPos)
break;
}
#ifdef HASH_ARRAY_2
if (aMatchLen2Exist)
{
if (aMax < 2)
{
aDistances[2] = aLen2Distance;
aMax = 2;
}
else if (aLen2Distance < aDistances[2])
aDistances[2] = aLen2Distance;
}
#ifdef HASH_ARRAY_3
if (aMatchLen3Exist)
{
if (aMax < 3)
{
aDistances[3] = aLen3Distance;
aMax = 3;
}
else if (aLen3Distance < aDistances[3])
aDistances[3] = aLen3Distance;
}
#endif
#endif
return aMax;
}
void CInTree::DummyLongestMatch()
{
UINT32 aCurrentLimit;
if (_pos + _matchMaxLen <= _streamPos)
aCurrentLimit = _matchMaxLen;
else
{
aCurrentLimit = _streamPos - _pos;
if(aCurrentLimit < kNumHashBytes)
return;
}
UINT32 aMatchMinPos = (_pos > _historySize) ? (_pos - _historySize) : 1;
BYTE *aCur = _buffer + _pos;
#ifdef HASH_ARRAY_2
UINT32 hash2Value;
#ifdef HASH_ARRAY_3
UINT32 aHash3Value;
UINT32 hashValue = Hash(aCur, hash2Value, aHash3Value);
_hash3[aHash3Value] = _pos;
#else
UINT32 hashValue = Hash(aCur, hash2Value);
#endif
_hash2[hash2Value] = _pos;
#else // no hash
UINT32 hashValue = Hash(aCur);
#endif
UINT32 aCurMatch = _hash[hashValue];
_hash[hashValue] = _pos;
if(aCurMatch < aMatchMinPos)
{
_chain[_cyclicBufferPos] = kEmptyHashValue;
return;
}
_chain[_cyclicBufferPos] = aCurMatch;
}
void CInTree::NormalizeLinks(CIndex *anArray, UINT32 aNumItems, UINT32 aSubValue)
{
for (UINT32 i = 0; i < aNumItems; i++)
{
UINT32 aValue = anArray[i];
if (aValue <= aSubValue)
aValue = kEmptyHashValue;
else
aValue -= aSubValue;
anArray[i] = aValue;
}
}
void CInTree::Normalize()
{
UINT32 aStartItem = _pos - _historySize;
UINT32 aSubValue = aStartItem - 1;
// NormalizeLinks(_chain + aStartItem, _historySize, aSubValue);
NormalizeLinks(_chain, _cyclicBufferSize, aSubValue);
NormalizeLinks(_hash, kHashSize, aSubValue);
#ifdef HASH_ARRAY_2
NormalizeLinks(_hash2, kHash2Size, aSubValue);
#ifdef HASH_ARRAY_3
NormalizeLinks(_hash3, kHash3Size, aSubValue);
#endif
#endif
ReduceOffsets(aSubValue);
}
}