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

View File

@@ -0,0 +1,116 @@
// BinTree.h
// #pragma once
// #ifndef __BINTREE_H
// #define __BINTREE_H
#include "../LZInWindow.h"
// #include "Common/Types.h"
// #include "Windows/Defs.h"
namespace BT_NAMESPACE {
// #define __USE_3_BYTES
#ifdef __USE_3_BYTES
#pragma pack(push, PragmaBinTree, 1)
struct CIndex
{
BYTE Data[3];
CIndex(){}
CIndex(UINT32 value)
{
Data[0] = value & 0xFF;
Data[1] = (value >> 8) & 0xFF;
Data[2] = (value >> 16) & 0xFF;
}
operator UINT32() const { return (*((const UINT32 *)Data)) & 0xFFFFFF; }
};
const UINT32 kMaxValForNormalize = CIndex(-1);
#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
#pragma pack(push, PragmaBinTreePair, 1)
// #pragma pack(push, 1)
struct CPair
{
CIndex Left;
CIndex Right;
};
// #pragma pack(pop)
#pragma pack(pop, PragmaBinTreePair)
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
CPair *_son;
UINT32 _cutValue;
void NormalizeLinks(CIndex *array, UINT32 numItems, UINT32 subValue);
void Normalize();
void FreeMemory();
public:
CInTree();
~CInTree();
HRESULT Create(UINT32 sizeHistory, UINT32 keepAddBufferBefore, UINT32 matchMaxLen,
UINT32 keepAddBufferAfter, UINT32 sizeReserv = (1<<17));
HRESULT Init(ISequentialInStream *stream);
void SetCutValue(UINT32 cutValue) { _cutValue = cutValue; }
UINT32 GetLongestMatch(UINT32 *distances);
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 @@
// BinTree2.h
// #pragma once
#ifndef __BINTREE2__H
#define __BINTREE2__H
#undef BT_CLSID
#define BT_CLSID CLSID_CMatchFinderBT2
#undef BT_NAMESPACE
#define BT_NAMESPACE NBT2
#include "BinTreeMF.h"
#include "BinTreeMFMain.h"
#endif

View File

@@ -0,0 +1,22 @@
// BinTree3.h
// #pragma once
#ifndef __BINTREE3__H
#define __BINTREE3__H
#undef BT_CLSID
#define BT_CLSID CLSID_CMatchFinderBT3
#undef BT_NAMESPACE
#define BT_NAMESPACE NBT3
#define HASH_ARRAY_2
#include "BinTreeMF.h"
#include "BinTreeMFMain.h"
#undef HASH_ARRAY_2
#endif

View File

@@ -0,0 +1,19 @@
// BinTree3Z.h
// #pragma once
#ifndef __BINTREE3Z__H
#define __BINTREE3Z__H
#undef BT_NAMESPACE
#define BT_NAMESPACE NBT3Z
#define HASH_ZIP
#include "BinTree.h"
// #include "BinTreeMain.h"
#undef HASH_ZIP
#endif

View File

@@ -0,0 +1,18 @@
// BinTree3ZMain.h
// #pragma once
#ifndef __BINTREE3ZMAIN__H
#define __BINTREE3ZMAIN__H
#undef BT_NAMESPACE
#define BT_NAMESPACE NBT3Z
#define HASH_ZIP
#include "BinTreeMain.h"
#undef HASH_ZIP
#endif

View File

@@ -0,0 +1,24 @@
// BinTree4.h
// #pragma once
#ifndef __BINTREE4__H
#define __BINTREE4__H
#undef BT_CLSID
#define BT_CLSID CLSID_CMatchFinderBT4
#undef BT_NAMESPACE
#define BT_NAMESPACE NBT4
#define HASH_ARRAY_2
#define HASH_ARRAY_3
#include "BinTreeMF.h"
#include "BinTreeMFMain.h"
#undef HASH_ARRAY_2
#undef HASH_ARRAY_3
#endif

View File

@@ -0,0 +1,26 @@
// BinTree4b.h
// #pragma once
#ifndef __BINTREE4B__H
#define __BINTREE4B__H
#undef BT_CLSID
#define BT_CLSID CLSID_CMatchFinderBT4b
#undef BT_NAMESPACE
#define BT_NAMESPACE NBT4B
#define HASH_ARRAY_2
#define HASH_ARRAY_3
#define HASH_BIG
#include "BinTreeMF.h"
#include "BinTreeMFMain.h"
#undef HASH_ARRAY_2
#undef HASH_ARRAY_3
#undef HASH_BIG
#endif

View File

@@ -0,0 +1,110 @@
// BinTreeMF.h
// #pragma once
// #ifndef __BINTREEMF_H
// #define __BINTREEMF_H
#include "../../../ICoder.h"
#include "BinTree.h"
namespace BT_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.MatchFinderBT") kIDString kIDUse3BytesString
// {23170F69-40C1-278C-02XX-0000000000}
DEFINE_GUID(BT_CLSID,
0x23170F69, 0x40C1, 0x278C, 0x02, kIDByte | kIDUse3BytesByte,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
class CInTree2: public CInTree
{
CMyComPtr<IMatchFinderCallback> _callback;
virtual void BeforeMoveBlock();
virtual void AfterMoveBlock();
public:
void SetCallback(IMatchFinderCallback *callback)
{
_callback = callback;
}
};
class CMatchFinderBinTree:
public IMatchFinder,
public IMatchFinderSetCallback,
public CMyUnknownImp
{
MY_UNKNOWN_IMP1(IMatchFinderSetCallback)
STDMETHOD(Init)(ISequentialInStream *stream);
STDMETHOD_(void, ReleaseStream)();
STDMETHOD(MovePos)();
STDMETHOD_(BYTE, GetIndexByte)(UINT32 index);
STDMETHOD_(UINT32, GetMatchLen)(UINT32 index, UINT32 back, UINT32 limit);
STDMETHOD_(UINT32, GetNumAvailableBytes)();
STDMETHOD_(const BYTE *, GetPointerToCurrentPos)();
STDMETHOD(Create)(UINT32 sizeHistory,
UINT32 keepAddBufferBefore, UINT32 matchMaxLen,
UINT32 keepAddBufferAfter);
STDMETHOD_(UINT32, GetLongestMatch)(UINT32 *distances);
STDMETHOD_(void, DummyLongestMatch)();
// IMatchFinderSetCallback
STDMETHOD(SetCallback)(IMatchFinderCallback *callback);
private:
// UINT32 m_WindowReservSize;
CInTree2 _matchFinder;
public:
// CMatchFinderBinTree(): m_WindowReservSize((1 << 19) + 256) {};
void SetCutValue(UINT32 cutValue)
{ _matchFinder.SetCutValue(cutValue); }
/*
void SetWindowReservSize(UINT32 reservWindowSize)
{ m_WindowReservSize = reservWindowSize; }
*/
};
}
// #endif

View File

@@ -0,0 +1,81 @@
// BinTreeMFMain.h
// #include "StdAfx.h"
// #include "BinTreeMF.h"
#include "BinTreeMain.h"
namespace BT_NAMESPACE {
void CInTree2::BeforeMoveBlock()
{
if (_callback)
_callback->BeforeChangingBufferPos();
CInTree::BeforeMoveBlock();
}
void CInTree2::AfterMoveBlock()
{
CInTree::AfterMoveBlock();
if (_callback)
_callback->AfterChangingBufferPos();
}
STDMETHODIMP CMatchFinderBinTree::Init(ISequentialInStream *stream)
{ return _matchFinder.Init(stream); }
STDMETHODIMP_(void) CMatchFinderBinTree::ReleaseStream()
{
// _matchFinder.ReleaseStream();
}
STDMETHODIMP CMatchFinderBinTree::MovePos()
{ return _matchFinder.MovePos(); }
STDMETHODIMP_(BYTE) CMatchFinderBinTree::GetIndexByte(UINT32 index)
{ return _matchFinder.GetIndexByte(index); }
STDMETHODIMP_(UINT32) CMatchFinderBinTree::GetMatchLen(UINT32 index,
UINT32 back, UINT32 limit)
{ return _matchFinder.GetMatchLen(index, back, limit); }
STDMETHODIMP_(UINT32) CMatchFinderBinTree::GetNumAvailableBytes()
{ return _matchFinder.GetNumAvailableBytes(); }
STDMETHODIMP CMatchFinderBinTree::Create(UINT32 sizeHistory,
UINT32 keepAddBufferBefore, UINT32 matchMaxLen,
UINT32 keepAddBufferAfter)
{
UINT32 windowReservSize = (sizeHistory + keepAddBufferBefore +
matchMaxLen + keepAddBufferAfter) / 2 + 256;
try
{
return _matchFinder.Create(sizeHistory, keepAddBufferBefore,
matchMaxLen, keepAddBufferAfter, windowReservSize);
}
catch(...)
{
return E_OUTOFMEMORY;
}
}
STDMETHODIMP_(UINT32) CMatchFinderBinTree::GetLongestMatch(UINT32 *distances)
{ return _matchFinder.GetLongestMatch(distances); }
STDMETHODIMP_(void) CMatchFinderBinTree::DummyLongestMatch()
{ _matchFinder.DummyLongestMatch(); }
STDMETHODIMP_(const BYTE *) CMatchFinderBinTree::GetPointerToCurrentPos()
{
return _matchFinder.GetPointerToCurrentPos();
}
// IMatchFinderSetCallback
STDMETHODIMP CMatchFinderBinTree::SetCallback(IMatchFinderCallback *callback)
{
_matchFinder.SetCallback(callback);
return S_OK;
}
}

View File

@@ -0,0 +1,542 @@
// BinTreemain.h
// #include "StdAfx.h"
// #include "BinTree.h"
// #include "Common/NewHandler.h"
#include "../../../../Common/Defs.h"
#include "../../../../Common/CRC.h"
namespace BT_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 kNumHashDirectBytes = 3;
static const UINT32 kNumHashBytes = 3;
static const UINT32 kHashSize = 1 << (8 * kNumHashBytes);
#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),
_son(0),
_cutValue(0xFF)
{
}
void CInTree::FreeMemory()
{
#ifdef WIN32
if (_son != 0)
VirtualFree(_son, 0, MEM_RELEASE);
if (_hash != 0)
VirtualFree(_hash, 0, MEM_RELEASE);
#else
delete []_son;
delete []_hash;
#endif
_son = 0;
_hash = 0;
CLZInWindow::Free();
}
CInTree::~CInTree()
{
FreeMemory();
}
HRESULT CInTree::Create(UINT32 sizeHistory, UINT32 keepAddBufferBefore,
UINT32 matchMaxLen, UINT32 keepAddBufferAfter, UINT32 sizeReserv)
{
FreeMemory();
try
{
CLZInWindow::Create(sizeHistory + keepAddBufferBefore,
matchMaxLen + keepAddBufferAfter, sizeReserv);
if (_blockSize + 256 > kMaxValForNormalize)
return E_INVALIDARG;
_historySize = sizeHistory;
_matchMaxLen = matchMaxLen;
_cyclicBufferSize = sizeHistory + 1;
UINT32 size = kHashSize;
#ifdef HASH_ARRAY_2
size += kHash2Size;
#ifdef HASH_ARRAY_3
size += kHash3Size;
#endif
#endif
#ifdef WIN32
_son = (CPair *)::VirtualAlloc(0, (_cyclicBufferSize + 1) * sizeof(CPair), MEM_COMMIT, PAGE_READWRITE);
if (_son == 0)
throw 1; // CNewException();
_hash = (CIndex *)::VirtualAlloc(0, (size + 1) * sizeof(CIndex), MEM_COMMIT, PAGE_READWRITE);
if (_hash == 0)
throw 1; // CNewException();
#else
_son = new CPair[_cyclicBufferSize + 1];
_hash = new CIndex[size + 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 *stream)
{
RINOK(CLZInWindow::Init(stream));
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 &hash3Value)
{
UINT32 temp = CCRC::Table[pointer[0]] ^ pointer[1];
hash2Value = temp & (kHash2Size - 1);
hash3Value = (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)
{
hash2Value = (CCRC::Table[pointer[0]] ^ pointer[1]) & (kHash2Size - 1);
return (*((const UINT32 *)pointer)) & 0xFFFFFF;
}
#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 *distances)
{
UINT32 currentLimit;
if (_pos + _matchMaxLen <= _streamPos)
currentLimit = _matchMaxLen;
else
{
currentLimit = _streamPos - _pos;
if(currentLimit < kNumHashBytes)
return 0;
}
UINT32 matchMinPos = (_pos > _historySize) ? (_pos - _historySize) : 1;
BYTE *cur = _buffer + _pos;
UINT32 matchHashLenMax = 0;
#ifdef HASH_ARRAY_2
UINT32 hash2Value;
#ifdef HASH_ARRAY_3
UINT32 hash3Value;
UINT32 hashValue = Hash(cur, hash2Value, hash3Value);
#else
UINT32 hashValue = Hash(cur, hash2Value);
#endif
#else
UINT32 hashValue = Hash(cur);
#endif
UINT32 curMatch = _hash[hashValue];
#ifdef HASH_ARRAY_2
UINT32 curMatch2 = _hash2[hash2Value];
#ifdef HASH_ARRAY_3
UINT32 curMatch3 = _hash3[hash3Value];
#endif
_hash2[hash2Value] = _pos;
bool matchLen2Exist = false;
UINT32 len2Distance = 0;
if(curMatch2 >= matchMinPos)
{
if (_buffer[curMatch2] == cur[0])
{
len2Distance = _pos - curMatch2 - 1;
matchHashLenMax = 2;
matchLen2Exist = true;
}
}
#ifdef HASH_ARRAY_3
_hash3[hash3Value] = _pos;
UINT32 matchLen3Exist = false;
UINT32 len3Distance = 0;
if(curMatch3 >= matchMinPos)
{
if (_buffer[curMatch3] == cur[0])
{
len3Distance = _pos - curMatch3 - 1;
matchHashLenMax = 3;
matchLen3Exist = true;
if (matchLen2Exist)
{
if (len3Distance < len2Distance)
len2Distance = len3Distance;
}
else
{
len2Distance = len3Distance;
matchLen2Exist = true;
}
}
}
#endif
#endif
_hash[hashValue] = _pos;
if(curMatch < matchMinPos)
{
_son[_cyclicBufferPos].Left = kEmptyHashValue;
_son[_cyclicBufferPos].Right = kEmptyHashValue;
#ifdef HASH_ARRAY_2
distances[2] = len2Distance;
#ifdef HASH_ARRAY_3
distances[3] = len3Distance;
#endif
#endif
return matchHashLenMax;
}
CIndex *ptrLeft = &_son[_cyclicBufferPos].Right;
CIndex *ptrRight = &_son[_cyclicBufferPos].Left;
UINT32 maxLen, minSameLeft, minSameRight, minSame;
maxLen = minSameLeft = minSameRight = minSame = kNumHashDirectBytes;
#ifdef HASH_ARRAY_2
#ifndef HASH_ARRAY_3
if (matchLen2Exist)
distances[2] = len2Distance;
else
if (kNumHashDirectBytes >= 2)
distances[2] = _pos - curMatch - 1;
#endif
#endif
distances[maxLen] = _pos - curMatch - 1;
for(UINT32 count = _cutValue; count > 0; count--)
{
BYTE *pby1 = _buffer + curMatch;
// CIndex left = _son[curMatch].Left; // it's prefetch
UINT32 currentLen;
for(currentLen = minSame; currentLen < currentLimit; currentLen++/*, dwComps++*/)
if (pby1[currentLen] != cur[currentLen])
break;
while (currentLen > maxLen)
distances[++maxLen] = _pos - curMatch - 1;
UINT32 delta = _pos - curMatch;
UINT32 cyclicPos = (delta <= _cyclicBufferPos) ?
(_cyclicBufferPos - delta):
(_cyclicBufferPos - delta + _cyclicBufferSize);
if (currentLen != currentLimit)
{
if (pby1[currentLen] < cur[currentLen])
{
*ptrRight = curMatch;
ptrRight = &_son[cyclicPos].Right;
curMatch = _son[cyclicPos].Right;
if(currentLen > minSameLeft)
{
minSameLeft = currentLen;
minSame = MyMin(minSameLeft, minSameRight);
}
}
else
{
*ptrLeft = curMatch;
ptrLeft = &_son[cyclicPos].Left;
// curMatch = left;
curMatch = _son[cyclicPos].Left;
if(currentLen > minSameRight)
{
minSameRight = currentLen;
minSame = MyMin(minSameLeft, minSameRight);
}
}
}
else
{
if(currentLen < _matchMaxLen)
{
*ptrLeft = curMatch;
ptrLeft = &_son[cyclicPos].Left;
curMatch = _son[cyclicPos].Left;
if(currentLen > minSameRight)
{
minSameRight = currentLen;
minSame = MyMin(minSameLeft, minSameRight);
}
}
else
{
*ptrLeft = _son[cyclicPos].Right;
*ptrRight = _son[cyclicPos].Left;
#ifdef HASH_ARRAY_2
if (matchLen2Exist && len2Distance < distances[2])
distances[2] = len2Distance;
#ifdef HASH_ARRAY_3
if (matchLen3Exist && len3Distance < distances[3])
distances[3] = len3Distance;
#endif
#endif
return maxLen;
}
}
if(curMatch < matchMinPos)
break;
}
*ptrLeft = kEmptyHashValue;
*ptrRight = kEmptyHashValue;
#ifdef HASH_ARRAY_2
if (matchLen2Exist)
{
if (maxLen < 2)
{
distances[2] = len2Distance;
maxLen = 2;
}
else if (len2Distance < distances[2])
distances[2] = len2Distance;
}
#ifdef HASH_ARRAY_3
if (matchLen3Exist)
{
if (maxLen < 3)
{
distances[3] = len3Distance;
maxLen = 3;
}
else if (len3Distance < distances[3])
distances[3] = len3Distance;
}
#endif
#endif
return maxLen;
}
void CInTree::DummyLongestMatch()
{
UINT32 currentLimit;
if (_pos + _matchMaxLen <= _streamPos)
currentLimit = _matchMaxLen;
else
{
currentLimit = _streamPos - _pos;
if(currentLimit < kNumHashBytes)
return;
}
UINT32 matchMinPos = (_pos > _historySize) ? (_pos - _historySize) : 1;
BYTE *cur = _buffer + _pos;
#ifdef HASH_ARRAY_2
UINT32 hash2Value;
#ifdef HASH_ARRAY_3
UINT32 hash3Value;
UINT32 hashValue = Hash(cur, hash2Value, hash3Value);
_hash3[hash3Value] = _pos;
#else
UINT32 hashValue = Hash(cur, hash2Value);
#endif
_hash2[hash2Value] = _pos;
#else
UINT32 hashValue = Hash(cur);
#endif
UINT32 curMatch = _hash[hashValue];
_hash[hashValue] = _pos;
if(curMatch < matchMinPos)
{
_son[_cyclicBufferPos].Left = kEmptyHashValue;
_son[_cyclicBufferPos].Right = kEmptyHashValue;
return;
}
CIndex *ptrLeft = &_son[_cyclicBufferPos].Right;
CIndex *ptrRight = &_son[_cyclicBufferPos].Left;
UINT32 maxLen, minSameLeft, minSameRight, minSame;
maxLen = minSameLeft = minSameRight = minSame = kNumHashDirectBytes;
for(UINT32 count = _cutValue; count > 0; count--)
{
BYTE *pby1 = _buffer + curMatch;
// CIndex left = _son[curMatch].Left; // it's prefetch
UINT32 currentLen;
for(currentLen = minSame; currentLen < currentLimit; currentLen++/*, dwComps++*/)
if (pby1[currentLen] != cur[currentLen])
break;
UINT32 delta = _pos - curMatch;
UINT32 cyclicPos = (delta <= _cyclicBufferPos) ?
(_cyclicBufferPos - delta):
(_cyclicBufferPos - delta + _cyclicBufferSize);
if (currentLen != currentLimit)
{
if (pby1[currentLen] < cur[currentLen])
{
*ptrRight = curMatch;
ptrRight = &_son[cyclicPos].Right;
curMatch = _son[cyclicPos].Right;
if(currentLen > minSameLeft)
{
minSameLeft = currentLen;
minSame = MyMin(minSameLeft, minSameRight);
}
}
else
{
*ptrLeft = curMatch;
ptrLeft = &_son[cyclicPos].Left;
curMatch = _son[cyclicPos].Left;
// curMatch = left;
if(currentLen > minSameRight)
{
minSameRight = currentLen;
minSame = MyMin(minSameLeft, minSameRight);
}
}
}
else
{
if(currentLen < _matchMaxLen)
{
*ptrLeft = curMatch;
ptrLeft = &_son[cyclicPos].Left;
curMatch = _son[cyclicPos].Left;
if(currentLen > minSameRight)
{
minSameRight = currentLen;
minSame = MyMin(minSameLeft, minSameRight);
}
}
else
{
*ptrLeft = _son[cyclicPos].Right;
*ptrRight = _son[cyclicPos].Left;
return;
}
}
if(curMatch < matchMinPos)
break;
}
*ptrLeft = kEmptyHashValue;
*ptrRight = kEmptyHashValue;
}
void CInTree::NormalizeLinks(CIndex *array, UINT32 numItems, UINT32 subValue)
{
for (UINT32 i = 0; i < numItems; i++)
{
UINT32 value = array[i];
if (value <= subValue)
value = kEmptyHashValue;
else
value -= subValue;
array[i] = value;
}
}
void CInTree::Normalize()
{
UINT32 startItem = _pos - _historySize;
UINT32 subValue = startItem - 1;
// NormalizeLinks((CIndex *)(_son + startItem), _historySize * 2, subValue);
NormalizeLinks((CIndex *)_son, _cyclicBufferSize * 2, subValue);
NormalizeLinks(_hash, kHashSize, subValue);
#ifdef HASH_ARRAY_2
NormalizeLinks(_hash2, kHash2Size, subValue);
#ifdef HASH_ARRAY_3
NormalizeLinks(_hash3, kHash3Size, subValue);
#endif
#endif
ReduceOffsets(subValue);
}
}

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);
}
}

65
7zip/Compress/LZ/IMatchFinder.h Executable file
View File

@@ -0,0 +1,65 @@
// MatchFinders/IMatchFinder.h
// #pragma once
#ifndef __IMATCHFINDER_H
#define __IMATCHFINDER_H
// {23170F69-40C1-278A-0000-000200010000}
DEFINE_GUID(IID_IInWindowStream,
0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00);
MIDL_INTERFACE("23170F69-40C1-278A-0000-000200010000")
IInWindowStream: public IUnknown
{
STDMETHOD(Init)(ISequentialInStream *inStream) PURE;
STDMETHOD_(void, ReleaseStream)() PURE;
STDMETHOD(MovePos)() PURE;
STDMETHOD_(BYTE, GetIndexByte)(UINT32 index) PURE;
STDMETHOD_(UINT32, GetMatchLen)(UINT32 index, UINT32 distance, UINT32 limit) PURE;
STDMETHOD_(UINT32, GetNumAvailableBytes)() PURE;
STDMETHOD_(const BYTE *, GetPointerToCurrentPos)() PURE;
};
// {23170F69-40C1-278A-0000-000200020000}
DEFINE_GUID(IID_IMatchFinder,
0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00);
MIDL_INTERFACE("23170F69-40C1-278A-0000-000200020000")
IMatchFinder: public IInWindowStream
{
STDMETHOD(Create)(UINT32 historySize, UINT32 keepAddBufferBefore,
UINT32 matchMaxLen, UINT32 keepAddBufferAfter) PURE;
STDMETHOD_(UINT32, GetLongestMatch)(UINT32 *distances) PURE;
STDMETHOD_(void, DummyLongestMatch)() PURE;
};
// {23170F69-40C1-278A-0000-000200020100}
DEFINE_GUID(IID_IMatchFinderCallback,
0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x01, 0x00);
MIDL_INTERFACE("23170F69-40C1-278A-0000-000200020100")
IMatchFinderCallback: public IUnknown
{
STDMETHOD(BeforeChangingBufferPos)() PURE;
STDMETHOD(AfterChangingBufferPos)() PURE;
};
// {23170F69-40C1-278A-0000-000200020200}
DEFINE_GUID(IID_IMatchFinderSetCallback,
0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x02, 0x00);
MIDL_INTERFACE("23170F69-40C1-278A-0000-000200020200")
IMatchFinderSetCallback: public IUnknown
{
STDMETHOD(SetCallback)(IMatchFinderCallback *callback) PURE;
};
/*
// {23170F69-40C1-278A-0000-000200030000}
DEFINE_GUID(IID_IInitMatchFinder,
0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00);
MIDL_INTERFACE("23170F69-40C1-278A-0000-000200030000")
IMatchFinderInit: public IUnknown
{
STDMETHOD(InitMatchFinder)(IMatchFinder *matchFinder) PURE;
};
*/
#endif

98
7zip/Compress/LZ/LZInWindow.cpp Executable file
View File

@@ -0,0 +1,98 @@
// LZInWindow.cpp
#include "StdAfx.h"
#include "LZInWindow.h"
#include "../../../Common/MyCom.h"
CLZInWindow::~CLZInWindow()
{
Free();
}
void CLZInWindow::Free()
{
delete []_bufferBase;
_bufferBase = 0;
}
void CLZInWindow::Create(UINT32 keepSizeBefore, UINT32 keepSizeAfter, UINT32 keepSizeReserv)
{
_keepSizeBefore = keepSizeBefore;
_keepSizeAfter = keepSizeAfter;
_keepSizeReserv = keepSizeReserv;
_blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
Free();
_bufferBase = new BYTE[_blockSize];
_pointerToLastSafePosition = _bufferBase + _blockSize - keepSizeAfter;
}
HRESULT CLZInWindow::Init(ISequentialInStream *stream)
{
_stream = stream;
_buffer = _bufferBase;
_pos = 0;
_streamPos = 0;
_streamEndWasReached = false;
return ReadBlock();
}
/*
void CLZInWindow::ReleaseStream()
{
_stream.Release();
}
*/
///////////////////////////////////////////
// ReadBlock
// In State:
// (_buffer + _streamPos) <= (_bufferBase + _blockSize)
// Out State:
// _posLimit <= _blockSize - _keepSizeAfter;
// if(_streamEndWasReached == false):
// _streamPos >= _pos + _keepSizeAfter
// _posLimit = _streamPos - _keepSizeAfter;
// else
//
HRESULT CLZInWindow::ReadBlock()
{
if(_streamEndWasReached)
return S_OK;
while(true)
{
UINT32 size = (_bufferBase + _blockSize) - (_buffer + _streamPos);
if(size == 0)
return S_OK;
UINT32 numReadBytes;
RINOK(_stream->ReadPart(_buffer + _streamPos, size, &numReadBytes));
if(numReadBytes == 0)
{
_posLimit = _streamPos;
const BYTE *pointerToPostion = _buffer + _posLimit;
if(pointerToPostion > _pointerToLastSafePosition)
_posLimit = _pointerToLastSafePosition - _buffer;
_streamEndWasReached = true;
return S_OK;
}
_streamPos += numReadBytes;
if(_streamPos >= _pos + _keepSizeAfter)
{
_posLimit = _streamPos - _keepSizeAfter;
return S_OK;
}
}
}
void CLZInWindow::MoveBlock()
{
BeforeMoveBlock();
UINT32 offset = (_buffer + _pos - _keepSizeBefore) - _bufferBase;
UINT32 numBytes = (_buffer + _streamPos) - (_bufferBase + offset);
memmove(_bufferBase, _bufferBase + offset, numBytes);
_buffer -= offset;
AfterMoveBlock();
}

89
7zip/Compress/LZ/LZInWindow.h Executable file
View File

@@ -0,0 +1,89 @@
// LZInWindow.h
// #pragma once
#ifndef __LZ_IN_WINDOW_H
#define __LZ_IN_WINDOW_H
#include "../../IStream.h"
class CLZInWindow
{
BYTE *_bufferBase; // pointer to buffer with data
ISequentialInStream *_stream;
UINT32 _posLimit; // offset (from _buffer) of first byte when new block reading must be done
bool _streamEndWasReached; // if (true) then _streamPos shows real end of stream
const BYTE *_pointerToLastSafePosition;
protected:
BYTE *_buffer; // Pointer to virtual Buffer begin
UINT32 _blockSize; // Size of Allocated memory block
UINT32 _pos; // offset (from _buffer) of curent byte
UINT32 _keepSizeBefore; // how many BYTEs must be kept in buffer before _pos
UINT32 _keepSizeAfter; // how many BYTEs must be kept buffer after _pos
UINT32 _keepSizeReserv; // how many BYTEs must be kept as reserv
UINT32 _streamPos; // offset (from _buffer) of first not read byte from Stream
virtual void BeforeMoveBlock() {};
virtual void AfterMoveBlock() {};
void MoveBlock();
virtual HRESULT ReadBlock();
void Free();
public:
CLZInWindow(): _bufferBase(0) {}
~CLZInWindow();
void Create(UINT32 keepSizeBefore, UINT32 keepSizeAfter,
UINT32 keepSizeReserv = (1<<17));
HRESULT Init(ISequentialInStream *stream);
// void ReleaseStream();
BYTE *GetBuffer() const { return _buffer; }
const BYTE *GetPointerToCurrentPos() const { return _buffer + _pos; }
HRESULT MovePos()
{
_pos++;
if (_pos > _posLimit)
{
const BYTE *pointerToPostion = _buffer + _pos;
if(pointerToPostion > _pointerToLastSafePosition)
MoveBlock();
return ReadBlock();
}
else
return S_OK;
}
// BYTE GetCurrentByte()const;
BYTE GetIndexByte(UINT32 index)const
{ return _buffer[_pos + index]; }
// UINT32 GetCurPos()const { return _pos;};
// BYTE *GetBufferBeg()const { return _buffer;};
// index + limit have not to exceed _keepSizeAfter;
UINT32 GetMatchLen(UINT32 index, UINT32 back, UINT32 limit) const
{
if(_streamEndWasReached)
if ((_pos + index) + limit > _streamPos)
limit = _streamPos - (_pos + index);
back++;
BYTE *pby = _buffer + _pos + index;
UINT32 i;
for(i = 0; i < limit && pby[i] == pby[i - back]; i++);
return i;
}
UINT32 GetNumAvailableBytes() const { return _streamPos - _pos; }
void ReduceOffsets(UINT32 subValue)
{
_buffer += subValue;
_posLimit -= subValue;
_pos -= subValue;
_streamPos -= subValue;
}
};
#endif

View File

@@ -0,0 +1,83 @@
// LZOutWindow.cpp
#include "StdAfx.h"
#include "LZOutWindow.h"
void CLZOutWindow::Create(UINT32 windowSize)
{
_pos = 0;
_streamPos = 0;
UINT32 newBlockSize = windowSize;
const UINT32 kMinBlockSize = 1;
if (newBlockSize < kMinBlockSize)
newBlockSize = kMinBlockSize;
if (_buffer != 0 && _windowSize == newBlockSize)
return;
delete []_buffer;
_buffer = 0;
_windowSize = newBlockSize;
_buffer = new BYTE[_windowSize];
}
CLZOutWindow::~CLZOutWindow()
{
// ReleaseStream();
delete []_buffer;
}
/*
void CLZOutWindow::SetWindowSize(UINT32 windowSize)
{
_windowSize = windowSize;
}
*/
void CLZOutWindow::Init(ISequentialOutStream *stream, bool solid)
{
// ReleaseStream();
_stream = stream;
// _stream->AddRef();
if(!solid)
{
_streamPos = 0;
_pos = 0;
}
}
/*
void CLZOutWindow::ReleaseStream()
{
if(_stream != 0)
{
// Flush(); // Test it
_stream->Release();
_stream = 0;
}
}
*/
void CLZOutWindow::FlushWithCheck()
{
HRESULT result = Flush();
if (result != S_OK)
throw CLZOutWindowException(result);
}
HRESULT CLZOutWindow::Flush()
{
UINT32 size = _pos - _streamPos;
if(size == 0)
return S_OK;
UINT32 processedSize;
HRESULT result = _stream->Write(_buffer + _streamPos, size, &processedSize);
if (result != S_OK)
return result;
if (size != processedSize)
return E_FAIL;
if (_pos >= _windowSize)
_pos = 0;
_streamPos = _pos;
return S_OK;
}

78
7zip/Compress/LZ/LZOutWindow.h Executable file
View File

@@ -0,0 +1,78 @@
// LZOutWindow.h
// #pragma once
#ifndef __LZ_OUT_WINDOW_H
#define __LZ_OUT_WINDOW_H
#include "../../IStream.h"
// m_KeepSizeBefore: how mach BYTEs must be in buffer before _pos;
// m_KeepSizeAfter: how mach BYTEs must be in buffer after _pos;
// m_KeepSizeReserv: how mach BYTEs must be in buffer for Moving Reserv;
// must be >= aKeepSizeAfter; // test it
class CLZOutWindowException
{
public:
HRESULT ErrorCode;
CLZOutWindowException(HRESULT errorCode): ErrorCode(errorCode) {}
};
class CLZOutWindow
{
BYTE *_buffer;
UINT32 _pos;
UINT32 _windowSize;
UINT32 _streamPos;
ISequentialOutStream *_stream;
void FlushWithCheck();
public:
CLZOutWindow(): _buffer(0), _stream(0) {}
~CLZOutWindow();
void Create(UINT32 windowSize);
bool IsCreated() const { return _buffer != 0; }
void Init(ISequentialOutStream *stream, bool solid = false);
HRESULT Flush();
// void ReleaseStream();
// UINT32 GetCurPos() const { return _pos; }
// const BYTE *GetPointerToCurrentPos() const { return _buffer + _pos;};
void CopyBackBlock(UINT32 distance, UINT32 len)
{
UINT32 pos = _pos - distance - 1;
if (pos >= _windowSize)
pos += _windowSize;
for(; len > 0; len--)
{
if (pos >= _windowSize)
pos = 0;
_buffer[_pos++] = _buffer[pos++];
if (_pos >= _windowSize)
FlushWithCheck();
// PutOneByte(GetOneByte(0 - distance));
}
}
void PutOneByte(BYTE b)
{
_buffer[_pos++] = b;
if (_pos >= _windowSize)
FlushWithCheck();
}
BYTE GetOneByte(UINT32 index) const
{
UINT32 pos = _pos + index;
if (pos >= _windowSize)
pos += _windowSize;
return _buffer[pos];
}
// BYTE *GetBuffer() const { return _buffer; }
};
#endif

317
7zip/Compress/LZ/MT/MT.cpp Executable file
View File

@@ -0,0 +1,317 @@
// MT_MF.cpp
#include "StdAfx.h"
#include "MT.h"
class CMatchFinderCallback:
public IMatchFinderCallback,
public CMyUnknownImp
{
MY_UNKNOWN_IMP
STDMETHOD(BeforeChangingBufferPos)();
STDMETHOD(AfterChangingBufferPos)();
public:
CMatchFinderMT *m_MatchFinderMT;
const BYTE *m_BufferPosBefore;
};
STDMETHODIMP CMatchFinderCallback::BeforeChangingBufferPos()
{
m_MatchFinderMT->m_AskChangeBufferPos.Set();
m_MatchFinderMT->m_CanChangeBufferPos.Lock();
m_BufferPosBefore = m_MatchFinderMT->m_MatchFinder->GetPointerToCurrentPos();
return S_OK;
}
STDMETHODIMP CMatchFinderCallback::AfterChangingBufferPos()
{
m_MatchFinderMT->m_DataCurrentPos +=
m_MatchFinderMT->m_MatchFinder->GetPointerToCurrentPos() - m_BufferPosBefore;
m_MatchFinderMT->m_BufferPosWasChanged.Set();
return S_OK;
}
HRESULT CMatchFinderMT::SetMatchFinder(IMatchFinder *aMatchFinder,
UINT32 multiThreadMult)
{
_multiThreadMult = multiThreadMult;
m_MatchFinder = aMatchFinder;
CMyComPtr<IMatchFinderSetCallback> matchFinderSetCallback;
if (m_MatchFinder.QueryInterface(IID_IMatchFinderSetCallback,
&matchFinderSetCallback) == S_OK)
{
CMatchFinderCallback *matchFinderCallbackSpec =
new CMatchFinderCallback;
CMyComPtr<IMatchFinderCallback> matchFinderCallback = matchFinderCallbackSpec;
matchFinderCallbackSpec->m_MatchFinderMT = this;
matchFinderSetCallback->SetCallback(matchFinderCallback);
return S_OK;
}
else
return E_FAIL;
}
STDMETHODIMP CMatchFinderMT::Init(ISequentialInStream *aStream)
{
// OutputDebugString("Init\n");
m_AskChangeBufferPos.Reset();
m_CanChangeBufferPos.Reset();
m_BufferPosWasChanged.Reset();
m_StopWriting.Reset();
m_WritingWasStopped.Reset();
m_NeedStart = true;
HRESULT aResult = m_MatchFinder->Init(aStream);
if (aResult == S_OK)
m_DataCurrentPos = m_MatchFinder->GetPointerToCurrentPos();
return aResult;
}
STDMETHODIMP_(void) CMatchFinderMT::ReleaseStream()
{
// OutputDebugString("ReleaseStream\n");
m_StopWriting.Set();
m_WritingWasStopped.Lock();
// OutputDebugString("m_WritingWasStopped\n");
m_MatchFinder->ReleaseStream();
}
STDMETHODIMP CMatchFinderMT::MovePos()
{
m_NumAvailableBytesCurrent--;
m_DataCurrentPos++;
return S_OK;
}
STDMETHODIMP_(BYTE) CMatchFinderMT::GetIndexByte(UINT32 anIndex)
{
return m_DataCurrentPos[anIndex];
}
STDMETHODIMP_(UINT32) CMatchFinderMT::GetMatchLen(UINT32 aIndex,
UINT32 aBack, UINT32 aLimit)
{
if (int(aIndex + aLimit) > m_NumAvailableBytesCurrent)
aLimit = m_NumAvailableBytesCurrent - (aIndex);
aBack++;
const BYTE *pby = m_DataCurrentPos + aIndex;
UINT32 i;
for(i = 0; i < aLimit && pby[i] == pby[i - aBack]; i++);
/*
char aSz[100];
sprintf(aSz, "GetMatchLen = %d", i);
OutputDebugString(aSz);
OutputDebugString("\n");
*/
return i;
// return m_MatchFinder->GetMatchLen(aIndex, aBack, aLimit); }
}
STDMETHODIMP_(const BYTE *) CMatchFinderMT::GetPointerToCurrentPos()
{
return m_DataCurrentPos;
}
STDMETHODIMP_(UINT32) CMatchFinderMT::GetNumAvailableBytes()
{
if (m_NeedStart)
return m_MatchFinder->GetNumAvailableBytes();
else
return m_NumAvailableBytesCurrent;
}
void CMatchFinderMT::FreeMem()
{
delete []m_Buffer;
}
STDMETHODIMP CMatchFinderMT::Create(UINT32 aSizeHistory,
UINT32 aKeepAddBufferBefore, UINT32 matchMaxLen,
UINT32 aKeepAddBufferAfter)
{
FreeMem();
m_MatchMaxLen = matchMaxLen;
m_BlockSize = (matchMaxLen + 1) * _multiThreadMult;
UINT32 aBufferSize = m_BlockSize * kNumMTBlocks;
m_Buffer = new UINT32[aBufferSize];
for (int i = 0; i < kNumMTBlocks; i++)
m_Buffers[i] = &m_Buffer[i * m_BlockSize];
m_NeedStart = true;
aKeepAddBufferBefore += aBufferSize;
return m_MatchFinder->Create(aSizeHistory,
aKeepAddBufferBefore, matchMaxLen,
aKeepAddBufferAfter);
}
static DWORD WINAPI MFThread(void *aThreadCoderInfo)
{
CMatchFinderMT &aMT = *(CMatchFinderMT *)aThreadCoderInfo;
while (true)
{
HANDLE anEvents[3] = { aMT.m_ExitEvent, aMT.m_StopWriting, aMT.m_CanWriteEvents[aMT.m_WriteBufferIndex] } ;
DWORD anWaitResult = ::WaitForMultipleObjects(3, anEvents, FALSE, INFINITE);
if (anWaitResult == WAIT_OBJECT_0 + 0)
return 0;
if (anWaitResult == WAIT_OBJECT_0 + 1)
{
// OutputDebugString("m_StopWriting\n");
aMT.m_WriteBufferIndex = 0;
for (int i = 0; i < kNumMTBlocks; i++)
aMT.m_CanWriteEvents[i].Reset();
aMT.m_WritingWasStopped.Set();
continue;
}
// OutputDebugString("m_CanWriteEvents\n");
UINT32 *aBuffer = aMT.m_Buffers[aMT.m_WriteBufferIndex];
UINT32 aCurPos = 0;
UINT32 aNumBytes = 0;
while (aCurPos + aMT.m_MatchMaxLen + 1 <= aMT.m_BlockSize)
{
if (aMT.m_MatchFinder->GetNumAvailableBytes() == 0)
break;
UINT32 aLen = aMT.m_MatchFinder->GetLongestMatch(aBuffer + aCurPos);
/*
if (aLen == 1)
aLen = 0;
*/
aBuffer[aCurPos] = aLen;
aCurPos += aLen + 1;
HRESULT aResult = aMT.m_MatchFinder->MovePos();
if (aResult != S_OK)
throw 124459;
aNumBytes++;
}
aMT.m_LimitPos[aMT.m_WriteBufferIndex] = aCurPos;
aMT.m_NumAvailableBytes[aMT.m_WriteBufferIndex] =
aNumBytes + aMT.m_MatchFinder->GetNumAvailableBytes();
// char aSz[100];
// sprintf(aSz, "x = %d", aMT.m_WriteBufferIndex);
// OutputDebugString(aSz);
// OutputDebugString("aMT.m_CanReadEvents\n");
aMT.m_CanReadEvents[aMT.m_WriteBufferIndex].Set();
aMT.m_WriteBufferIndex++;
if (aMT.m_WriteBufferIndex == kNumMTBlocks)
aMT.m_WriteBufferIndex = 0;
}
/*
while(true)
{
if (!((CCoderMixer2 *)aThreadCoderInfo)->MyCode())
return 0;
}
*/
}
CMatchFinderMT::CMatchFinderMT():
m_Buffer(0),
_multiThreadMult(100)
{
for (int i = 0; i < kNumMTBlocks; i++)
{
m_CanReadEvents[i].Reset();
m_CanWriteEvents[i].Reset();
}
m_ReadBufferIndex = 0;
m_WriteBufferIndex = 0;
m_ExitEvent.Reset();
if (!m_Thread.Create(MFThread, this))
throw 271826;
}
CMatchFinderMT::~CMatchFinderMT()
{
m_ExitEvent.Set();
if (HANDLE(m_Thread) != 0)
::WaitForSingleObject(m_Thread, INFINITE);
FreeMem();
}
void CMatchFinderMT::Start()
{
// OutputDebugString("Start\n");
m_AskChangeBufferPos.Reset();
m_CanChangeBufferPos.Reset();
m_BufferPosWasChanged.Reset();
m_WriteBufferIndex = 0;
m_ReadBufferIndex = 0;
m_NeedStart = false;
m_CurrentPos = 0;
m_CurrentLimitPos = 0;
int i;
for (i = 0; i < kNumMTBlocks; i++)
m_CanReadEvents[i].Reset();
for (i = kNumMTBlocks - 1; i >= 0; i--)
m_CanWriteEvents[i].Set();
}
STDMETHODIMP_(UINT32) CMatchFinderMT::GetLongestMatch(UINT32 *aDistances)
{
// OutputDebugString("GetLongestMatch\n");
if (m_NeedStart)
Start();
/*
if (m_CurrentPos > m_CurrentLimitPos)
throw 1123324;
*/
if (m_CurrentPos == m_CurrentLimitPos)
{
// OutputDebugString("m_CurrentPos == m_CurrentLimitPos\n");
while (true)
{
/*
char aSz[100];
sprintf(aSz, "m_CanReadEvents[m_ReadBufferIndex] = %d\n", m_ReadBufferIndex);
OutputDebugString(aSz);
OutputDebugString("\n");
*/
HANDLE anEvents[2] = { m_AskChangeBufferPos, m_CanReadEvents[m_ReadBufferIndex] } ;
DWORD anWaitResult = ::WaitForMultipleObjects(2, anEvents, FALSE, INFINITE);
if (anWaitResult == WAIT_OBJECT_0 + 1)
break;
m_BufferPosWasChanged.Reset();
m_CanChangeBufferPos.Set();
m_BufferPosWasChanged.Lock();
}
m_CurrentLimitPos = m_LimitPos[m_ReadBufferIndex];
m_NumAvailableBytesCurrent = m_NumAvailableBytes[m_ReadBufferIndex];
m_CurrentPos = 0;
}
if (m_CurrentPos >= m_CurrentLimitPos)
throw 1123324;
const UINT32 *aBuffer = m_Buffers[m_ReadBufferIndex];
UINT32 aLen = aBuffer[m_CurrentPos++];
for (UINT32 i = 1; i <= aLen; i++)
aDistances[i] = aBuffer[m_CurrentPos++];
if (m_CurrentPos == m_CurrentLimitPos)
{
m_CanWriteEvents[m_ReadBufferIndex].Set();
m_ReadBufferIndex++;
if (m_ReadBufferIndex == kNumMTBlocks)
m_ReadBufferIndex = 0;
}
// char aSz[100];
// sprintf(aSz, "m_NumAvailableBytesCurrent = %d", m_NumAvailableBytesCurrent);
// OutputDebugString(aSz);
// OutputDebugString("\n");
return aLen;
}
STDMETHODIMP_(void) CMatchFinderMT::DummyLongestMatch()
{
UINT32 aBuffer[512];
GetLongestMatch(aBuffer);
// m_MatchFinder->DummyLongestMatch();
}

85
7zip/Compress/LZ/MT/MT.h Executable file
View File

@@ -0,0 +1,85 @@
// MT_MF.h
#pragma once
#ifndef __MT_MF_H
#define __MT_MF_H
#include "Common/MyCom.h"
#include "Windows/Thread.h"
#include "Windows/Synchronization.h"
#include "../../../ICoder.h"
#include "../IMatchFinder.h"
const int kNumMTBlocks = 3;
class CMatchFinderMT:
public IMatchFinder,
public CMyUnknownImp
{
MY_UNKNOWN_IMP
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)();
private:
public:
CMyComPtr<IMatchFinder> m_MatchFinder;
UINT32 m_MatchMaxLen;
UINT32 m_BlockSize;
// UINT32 m_BufferSize;
UINT32 *m_Buffer;
UINT32 *m_Buffers[kNumMTBlocks];
bool m_NeedStart;
UINT32 m_WriteBufferIndex;
UINT32 m_ReadBufferIndex;
NWindows::NSynchronization::CAutoResetEvent m_StopWriting;
NWindows::NSynchronization::CAutoResetEvent m_WritingWasStopped;
NWindows::NSynchronization::CAutoResetEvent m_AskChangeBufferPos;
NWindows::NSynchronization::CAutoResetEvent m_CanChangeBufferPos;
NWindows::NSynchronization::CAutoResetEvent m_BufferPosWasChanged;
NWindows::NSynchronization::CManualResetEvent m_ExitEvent;
// NWindows::NSynchronization::CManualResetEvent m_NewStart;
NWindows::NSynchronization::CAutoResetEvent m_CanReadEvents[kNumMTBlocks];
NWindows::NSynchronization::CAutoResetEvent m_CanWriteEvents[kNumMTBlocks];
UINT32 m_LimitPos[kNumMTBlocks];
UINT32 m_NumAvailableBytes[kNumMTBlocks];
UINT32 m_NumAvailableBytesCurrent;
const BYTE *m_DataCurrentPos;
UINT32 m_CurrentLimitPos;
UINT32 m_CurrentPos;
NWindows::CThread m_Thread;
// bool m_WriteWasClosed;
UINT32 _multiThreadMult;
public:
CMatchFinderMT();
~CMatchFinderMT();
void Start();
void FreeMem();
HRESULT SetMatchFinder(IMatchFinder *aMatchFinder,
UINT32 multiThreadMult = 200);
};
#endif

8
7zip/Compress/LZ/MT/StdAfx.h Executable file
View File

@@ -0,0 +1,8 @@
// stdafx.h
#ifndef __STDAFX_H
#define __STDAFX_H
#include <windows.h>
#endif

364
7zip/Compress/LZ/Patricia/Pat.h Executable file
View File

@@ -0,0 +1,364 @@
// Pat.h
// #pragma once
// #ifndef __PATRICIA__H
// #define __PATRICIA__H
#include "../../../../Common/AlignedBuffer.h"
#include "../../../../Common/MyCom.h"
#include "../LZInWindow.h"
namespace PAT_NAMESPACE {
struct CNode;
typedef CNode *CNodePointer;
#pragma pack(push, PragmaPatrTree)
#pragma pack(push, 1)
// #define __AUTO_REMOVE
// #define __USE_3_BYTES
// #define __NODE_4_BITS
// #define __NODE_3_BITS
// #define __NODE_2_BITS
// #define __NODE_2_BITS_PADDING
// #define __HASH_3
#ifdef __USE_3_BYTES
struct CIndex
{
BYTE Data[3];
operator =(UINT32 aValue)
{
Data[0] = aValue & 0xFF;
Data[1] = (aValue >> 8) & 0xFF;
Data[2] = (aValue >> 16) & 0xFF;
}
operator UINT32() const { return (*((const UINT32 *)Data)) & 0xFFFFFF; }
};
#else
typedef UINT32 CIndex;
#endif
#pragma pack(pop)
#pragma pack(pop, PragmaPatrTree)
#ifdef __NODE_4_BITS
typedef UINT32 CIndex2;
typedef UINT32 CSameBitsType;
#else
#ifdef __NODE_3_BITS
typedef UINT32 CIndex2;
typedef UINT32 CSameBitsType;
#else
#ifdef __USE_3_BYTES
typedef BYTE CSameBitsType;
#else
typedef UINT32 CIndex;
typedef UINT32 CSameBitsType;
#endif
typedef CIndex CIndex2;
#endif
#endif
const UINT32 kNumBitsInIndex = sizeof(CIndex) * 8;
const UINT32 kMatchStartValue = UINT32(1) << (kNumBitsInIndex - 1);
typedef CIndex CMatchPointer;
const UINT32 kDescendantEmptyValue = kMatchStartValue - 1;
#pragma pack(push, PragmaPatrTree2)
#pragma pack(push, 1)
union CDescendant
{
CIndex NodePointer;
CMatchPointer MatchPointer;
bool IsEmpty() const { return NodePointer == kDescendantEmptyValue; }
bool IsNode() const { return NodePointer < kDescendantEmptyValue; }
bool IsMatch() const { return NodePointer > kDescendantEmptyValue; }
void MakeEmpty() { NodePointer = kDescendantEmptyValue; }
};
#pragma pack(pop)
#pragma pack(pop, PragmaPatrTree2)
#pragma pack( push, PragmaBackNode)
#pragma pack( push, 1)
#undef MY_BYTE_SIZE
#ifdef __NODE_4_BITS
#define MY_BYTE_SIZE 8
const UINT32 kNumSubBits = 4;
#else
#ifdef __NODE_3_BITS
#define MY_BYTE_SIZE 9
const UINT32 kNumSubBits = 3;
#else
#define MY_BYTE_SIZE 8
#ifdef __NODE_2_BITS
const UINT32 kNumSubBits = 2;
#else
const UINT32 kNumSubBits = 1;
#endif
#endif
#endif
const UINT32 kNumSubNodes = 1 << kNumSubBits;
const UINT32 kSubNodesMask = kNumSubNodes - 1;
struct CNode
{
CIndex2 LastMatch;
CSameBitsType NumSameBits;
union
{
CDescendant Descendants[kNumSubNodes];
UINT32 NextFreeNode;
};
#ifdef __NODE_2_BITS
#ifdef __NODE_2_BITS_PADDING
UINT32 Padding[2];
#endif
#endif
};
#pragma pack(pop)
#pragma pack(pop, PragmaBackNode)
#undef kIDNumBitsByte
#undef kIDNumBitsString
#ifdef __NODE_4_BITS
#define kIDNumBitsByte 0x30
#define kIDNumBitsString TEXT("4")
#else
#ifdef __NODE_3_BITS
#define kIDNumBitsByte 0x20
#define kIDNumBitsString TEXT("3")
#else
#ifdef __NODE_2_BITS
#define kIDNumBitsByte 0x10
#define kIDNumBitsString TEXT("2")
#else
#define kIDNumBitsByte 0x00
#define kIDNumBitsString TEXT("1")
#endif
#endif
#endif
#undef kIDManualRemoveByte
#undef kIDManualRemoveString
#ifdef __AUTO_REMOVE
#define kIDManualRemoveByte 0x00
#define kIDManualRemoveString TEXT("")
#else
#define kIDManualRemoveByte 0x08
#define kIDManualRemoveString TEXT("R")
#endif
#undef kIDHash3Byte
#undef kIDHash3String
#ifdef __HASH_3
#define kIDHash3Byte 0x04
#define kIDHash3String TEXT("H")
#else
#define kIDHash3Byte 0x00
#define kIDHash3String TEXT("")
#endif
#undef kIDUse3BytesByte
#undef kIDUse3BytesString
#ifdef __USE_3_BYTES
#define kIDUse3BytesByte 0x02
#define kIDUse3BytesString TEXT("T")
#else
#define kIDUse3BytesByte 0x00
#define kIDUse3BytesString TEXT("")
#endif
#undef kIDPaddingByte
#undef kIDPaddingString
#ifdef __NODE_2_BITS_PADDING
#define kIDPaddingByte 0x01
#define kIDPaddingString TEXT("P")
#else
#define kIDPaddingByte 0x00
#define kIDPaddingString TEXT("")
#endif
// #undef kIDString
// #define kIDString TEXT("Compress.MatchFinderPat") kIDNumBitsString kIDManualRemoveString kIDUse3BytesString kIDPaddingString kIDHash3String
// {23170F69-40C1-278C-01XX-0000000000}
DEFINE_GUID(PAT_CLSID,
0x23170F69, 0x40C1, 0x278C, 0x01,
kIDNumBitsByte |
kIDManualRemoveByte | kIDHash3Byte | kIDUse3BytesByte | kIDPaddingByte,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
// III(PAT_NAMESPACE)
class CPatricia:
public IMatchFinder,
public IMatchFinderSetCallback,
public CMyUnknownImp,
CLZInWindow
{
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(Create)(UINT32 aSizeHistory,
UINT32 aKeepAddBufferBefore, UINT32 aMatchMaxLen,
UINT32 aKeepAddBufferAfter);
STDMETHOD_(UINT32, GetLongestMatch)(UINT32 *aDistances);
STDMETHOD_(void, DummyLongestMatch)();
STDMETHOD_(const BYTE *, GetPointerToCurrentPos)();
void FreeMemory();
public:
CPatricia();
~CPatricia();
UINT32 _sizeHistory;
UINT32 _matchMaxLen;
CDescendant *m_HashDescendants;
#ifdef __HASH_3
CDescendant *m_Hash2Descendants;
#endif
CNode *m_Nodes;
UINT32 m_FreeNode;
UINT32 m_FreeNodeMax;
#ifdef __AUTO_REMOVE
UINT32 m_NumUsedNodes;
UINT32 m_NumNodes;
#else
bool m_SpecialRemoveMode;
#endif
bool m_SpecialMode;
UINT32 m_NumNotChangedCycles;
UINT32 *m_TmpBacks;
CAlignedBuffer m_AlignBuffer;
CMyComPtr<IMatchFinderCallback> m_Callback;
virtual void BeforeMoveBlock();
virtual void AfterMoveBlock();
// IMatchFinderSetCallback
STDMETHOD(SetCallback)(IMatchFinderCallback *aCallback);
void ChangeLastMatch(UINT32 aHashValue);
#ifdef __AUTO_REMOVE
void TestRemoveDescendant(CDescendant &aDescendant, UINT32 aLimitPos);
void TestRemoveNodes();
void RemoveNode(UINT32 anIndex);
void TestRemoveAndNormalizeDescendant(CDescendant &aDescendant,
UINT32 aLimitPos, UINT32 aSubValue);
void TestRemoveNodesAndNormalize();
#else
void NormalizeDescendant(CDescendant &aDescendant, UINT32 aSubValue);
void Normalize();
void RemoveMatch();
#endif
private:
void AddInternalNode(CNodePointer aNode, CIndex *aNodePointerPointer,
BYTE aByte, BYTE aByteXOR, UINT32 aNumSameBits, UINT32 aPos)
{
while((aByteXOR & kSubNodesMask) == 0)
{
aByteXOR >>= kNumSubBits;
aByte >>= kNumSubBits;
aNumSameBits -= kNumSubBits;
}
// Insert New Node
CNodePointer aNewNode = &m_Nodes[m_FreeNode];
UINT32 aNodeIndex = *aNodePointerPointer;
*aNodePointerPointer = m_FreeNode;
m_FreeNode = aNewNode->NextFreeNode;
#ifdef __AUTO_REMOVE
m_NumUsedNodes++;
#endif
if (m_FreeNode > m_FreeNodeMax)
{
m_FreeNodeMax = m_FreeNode;
m_Nodes[m_FreeNode].NextFreeNode = m_FreeNode + 1;
}
UINT32 aBitsNew = aByte & kSubNodesMask;
UINT32 aBitsOld = (aByte ^ aByteXOR) & kSubNodesMask;
for (UINT32 i = 0; i < kNumSubNodes; i++)
aNewNode->Descendants[i].NodePointer = kDescendantEmptyValue;
aNewNode->Descendants[aBitsNew].MatchPointer = aPos + kMatchStartValue;
aNewNode->Descendants[aBitsOld].NodePointer = aNodeIndex;
aNewNode->NumSameBits = CSameBitsType(aNode->NumSameBits - aNumSameBits);
aNewNode->LastMatch = aPos;
aNode->NumSameBits = CSameBitsType(aNumSameBits - kNumSubBits);
}
void AddLeafNode(CNodePointer aNode, BYTE aByte, BYTE aByteXOR,
UINT32 aNumSameBits, UINT32 aPos, UINT32 aDescendantIndex)
{
for(;(aByteXOR & kSubNodesMask) == 0; aNumSameBits += kNumSubBits)
{
aByte >>= kNumSubBits;
aByteXOR >>= kNumSubBits;
}
UINT32 aNewNodeIndex = m_FreeNode;
CNodePointer aNewNode = &m_Nodes[m_FreeNode];
m_FreeNode = aNewNode->NextFreeNode;
#ifdef __AUTO_REMOVE
m_NumUsedNodes++;
#endif
if (m_FreeNode > m_FreeNodeMax)
{
m_FreeNodeMax = m_FreeNode;
m_Nodes[m_FreeNode].NextFreeNode = m_FreeNode + 1;
}
UINT32 aBitsNew = (aByte & kSubNodesMask);
UINT32 aBitsOld = (aByte ^ aByteXOR) & kSubNodesMask;
for (UINT32 i = 0; i < kNumSubNodes; i++)
aNewNode->Descendants[i].NodePointer = kDescendantEmptyValue;
aNewNode->Descendants[aBitsNew].MatchPointer = aPos + kMatchStartValue;
aNewNode->Descendants[aBitsOld].MatchPointer =
aNode->Descendants[aDescendantIndex].MatchPointer;
aNewNode->NumSameBits = CSameBitsType(aNumSameBits);
aNewNode->LastMatch = aPos;
aNode->Descendants[aDescendantIndex].NodePointer = aNewNodeIndex;
}
};
}
// #endif

View File

@@ -0,0 +1,24 @@
// Pat2.h
#pragma once
#ifndef __PAT2__H
#define __PAT2__H
#undef PAT_CLSID
#define PAT_CLSID CLSID_CMatchFinderPat2
#undef PAT_NAMESPACE
#define PAT_NAMESPACE NPat2
#define __AUTO_REMOVE
#define __NODE_2_BITS
#include "Pat.h"
#include "PatMain.h"
#undef __AUTO_REMOVE
#undef __NODE_2_BITS
#endif

View File

@@ -0,0 +1,26 @@
// Pat2H.h
#pragma once
#ifndef __PAT2H__H
#define __PAT2H__H
#undef PAT_CLSID
#define PAT_CLSID CLSID_CMatchFinderPat2H
#undef PAT_NAMESPACE
#define PAT_NAMESPACE NPat2H
#define __AUTO_REMOVE
#define __NODE_2_BITS
#define __HASH_3
#include "Pat.h"
#include "PatMain.h"
#undef __AUTO_REMOVE
#undef __NODE_2_BITS
#undef __HASH_3
#endif

View File

@@ -0,0 +1,22 @@
// Pat2R.h
#pragma once
#ifndef __PAT2R__H
#define __PAT2R__H
#undef PAT_CLSID
#define PAT_CLSID CLSID_CMatchFinderPat2R
#undef PAT_NAMESPACE
#define PAT_NAMESPACE NPat2R
#define __NODE_2_BITS
#include "Pat.h"
#include "PatMain.h"
#undef __NODE_2_BITS
#endif

View File

@@ -0,0 +1,26 @@
// Pat3H.h
#pragma once
#ifndef __PAT3H__H
#define __PAT3H__H
#undef PAT_CLSID
#define PAT_CLSID CLSID_CMatchFinderPat3H
#undef PAT_NAMESPACE
#define PAT_NAMESPACE NPat3H
#define __AUTO_REMOVE
#define __NODE_3_BITS
#define __HASH_3
#include "Pat.h"
#include "PatMain.h"
#undef __AUTO_REMOVE
#undef __NODE_3_BITS
#undef __HASH_3
#endif

View File

@@ -0,0 +1,26 @@
// Pat4H.h
#pragma once
#ifndef __PAT4H__H
#define __PAT4H__H
#undef PAT_CLSID
#define PAT_CLSID CLSID_CMatchFinderPat4H
#undef PAT_NAMESPACE
#define PAT_NAMESPACE NPat4H
#define __AUTO_REMOVE
#define __NODE_4_BITS
#define __HASH_3
#include "Pat.h"
#include "PatMain.h"
#undef __AUTO_REMOVE
#undef __NODE_4_BITS
#undef __HASH_3
#endif

View File

@@ -0,0 +1,976 @@
// PatMain.h
#include "../../../../Common/Defs.h"
#include "../../../../Common/NewHandler.h"
namespace PAT_NAMESPACE {
STDMETHODIMP CPatricia::SetCallback(IMatchFinderCallback *aCallback)
{
m_Callback = aCallback;
return S_OK;
}
void CPatricia::BeforeMoveBlock()
{
if (m_Callback)
m_Callback->BeforeChangingBufferPos();
CLZInWindow::BeforeMoveBlock();
}
void CPatricia::AfterMoveBlock()
{
if (m_Callback)
m_Callback->AfterChangingBufferPos();
CLZInWindow::AfterMoveBlock();
}
const UINT32 kMatchStartValue2 = 2;
const UINT32 kDescendantEmptyValue2 = kMatchStartValue2 - 1;
const UINT32 kDescendantsNotInitilized2 = kDescendantEmptyValue2 - 1;
#ifdef __HASH_3
static const UINT32 kNumHashBytes = 3;
static const UINT32 kHashSize = 1 << (8 * kNumHashBytes);
static const UINT32 kNumHash2Bytes = 2;
static const UINT32 kHash2Size = 1 << (8 * kNumHash2Bytes);
static const UINT32 kPrevHashSize = kNumHash2Bytes;
#else
static const UINT32 kNumHashBytes = 2;
static const UINT32 kHashSize = 1 << (8 * kNumHashBytes);
static const UINT32 kPrevHashSize = 0;
#endif
CPatricia::CPatricia():
m_HashDescendants(0),
#ifdef __HASH_3
m_Hash2Descendants(0),
#endif
m_TmpBacks(0),
m_Nodes(0)
{
}
CPatricia::~CPatricia()
{
FreeMemory();
}
void CPatricia::FreeMemory()
{
delete []m_TmpBacks;
m_TmpBacks = 0;
#ifdef WIN32
if (m_Nodes != 0)
VirtualFree(m_Nodes, 0, MEM_RELEASE);
m_Nodes = 0;
#else
m_AlignBuffer.Free();
#endif
delete []m_HashDescendants;
m_HashDescendants = 0;
#ifdef __HASH_3
delete []m_Hash2Descendants;
m_Hash2Descendants = 0;
#endif
}
STDMETHODIMP CPatricia::Create(UINT32 aSizeHistory, UINT32 aKeepAddBufferBefore,
UINT32 aMatchMaxLen, UINT32 aKeepAddBufferAfter)
{
FreeMemory();
const int kNumBitsInNumSameBits = sizeof(CSameBitsType) * 8;
if (kNumBitsInNumSameBits < 32 && ((aMatchMaxLen * MY_BYTE_SIZE) > (1 << kNumBitsInNumSameBits)))
return E_INVALIDARG;
const UINT32 kAlignMask = (1 << 16) - 1;
UINT32 aWindowReservSize = aSizeHistory;
aWindowReservSize += kAlignMask;
aWindowReservSize &= ~(kAlignMask);
const UINT32 kMinReservSize = (1 << 19);
if (aWindowReservSize < kMinReservSize)
aWindowReservSize = kMinReservSize;
aWindowReservSize += 256;
try
{
CLZInWindow::Create(aSizeHistory + aKeepAddBufferBefore,
aMatchMaxLen + aKeepAddBufferAfter, aWindowReservSize);
_sizeHistory = aSizeHistory;
_matchMaxLen = aMatchMaxLen;
m_HashDescendants = new CDescendant[kHashSize + 1];
#ifdef __HASH_3
m_Hash2Descendants = new CDescendant[kHash2Size + 1];
#endif
#ifdef __AUTO_REMOVE
#ifdef __HASH_3
m_NumNodes = aSizeHistory + _sizeHistory * 4 / 8 + (1 << 19);
#else
m_NumNodes = aSizeHistory + _sizeHistory * 4 / 8 + (1 << 10);
#endif
#else
UINT32 m_NumNodes = aSizeHistory;
#endif
const UINT32 kMaxNumNodes = UINT32(1) << (sizeof(CIndex) * 8 - 1);
if (m_NumNodes + 32 > kMaxNumNodes)
return E_INVALIDARG;
#ifdef WIN32
m_Nodes = (CNode *)::VirtualAlloc(0, (m_NumNodes + 2) * sizeof(CNode), MEM_COMMIT, PAGE_READWRITE);
if (m_Nodes == 0)
throw CNewException();
#else
m_Nodes = (CNode *)m_AlignBuffer.Allocate(m_NumNodes + 2, sizeof(CNode), 0x40);
#endif
m_TmpBacks = new UINT32[_matchMaxLen + 1];
return S_OK;
}
catch(...)
{
FreeMemory();
return E_OUTOFMEMORY;
}
}
STDMETHODIMP CPatricia::Init(ISequentialInStream *aStream)
{
RINOK(CLZInWindow::Init(aStream));
// memset(m_HashDescendants, 0xFF, kHashSize * sizeof(m_HashDescendants[0]));
#ifdef __HASH_3
for (UINT32 i = 0; i < kHash2Size; i++)
m_Hash2Descendants[i].MatchPointer = kDescendantsNotInitilized2;
#else
for (UINT32 i = 0; i < kHashSize; i++)
m_HashDescendants[i].MakeEmpty();
#endif
m_Nodes[0].NextFreeNode = 1;
m_FreeNode = 0;
m_FreeNodeMax = 0;
#ifdef __AUTO_REMOVE
m_NumUsedNodes = 0;
#else
m_SpecialRemoveMode = false;
#endif
m_SpecialMode = false;
return S_OK;
}
STDMETHODIMP_(void) CPatricia::ReleaseStream()
{
// CLZInWindow::ReleaseStream();
}
// pos = _pos + kNumHashBytes
// aFullCurrentLimit = aCurrentLimit + kNumHashBytes
// aFullMatchLen = aMatchLen + kNumHashBytes
void CPatricia::ChangeLastMatch(UINT32 aHashValue)
{
UINT32 pos = _pos + kNumHashBytes - 1;
UINT32 descendantIndex;
const BYTE *aCurrentBytePointer = _buffer + pos;
UINT32 aNumLoadedBits = 0;
BYTE aByte;
CNodePointer aNode = &m_Nodes[m_HashDescendants[aHashValue].NodePointer];
while(true)
{
UINT32 aNumSameBits = aNode->NumSameBits;
if(aNumSameBits > 0)
{
if (aNumLoadedBits < aNumSameBits)
{
aNumSameBits -= aNumLoadedBits;
aCurrentBytePointer += (aNumSameBits / MY_BYTE_SIZE);
aNumSameBits %= MY_BYTE_SIZE;
aByte = *aCurrentBytePointer++;
aNumLoadedBits = MY_BYTE_SIZE;
}
aByte >>= aNumSameBits;
aNumLoadedBits -= aNumSameBits;
}
if(aNumLoadedBits == 0)
{
aByte = *aCurrentBytePointer++;
aNumLoadedBits = MY_BYTE_SIZE;
}
descendantIndex = (aByte & kSubNodesMask);
aNode->LastMatch = pos;
aNumLoadedBits -= kNumSubBits;
aByte >>= kNumSubBits;
if(aNode->Descendants[descendantIndex].IsNode())
aNode = &m_Nodes[aNode->Descendants[descendantIndex].NodePointer];
else
break;
}
aNode->Descendants[descendantIndex].MatchPointer = pos + kMatchStartValue;
}
UINT32 CPatricia::GetLongestMatch(UINT32 *aBacks)
{
UINT32 aFullCurrentLimit;
if (_pos + _matchMaxLen <= _streamPos)
aFullCurrentLimit = _matchMaxLen;
else
{
aFullCurrentLimit = _streamPos - _pos;
if(aFullCurrentLimit < kNumHashBytes)
return 0;
}
UINT32 pos = _pos + kNumHashBytes;
#ifdef __HASH_3
UINT32 aHashValueTemp = (*((UINT32 *)(_buffer + _pos)));
UINT32 aHashValue = ((aHashValueTemp << 8) |
((aHashValueTemp & 0xFFFFFF)>> 16)) & 0xFFFFFF;
CDescendant &aHashDescendant = m_HashDescendants[aHashValue];
CDescendant &aHash2Descendant = m_Hash2Descendants[aHashValueTemp & 0xFFFF];
if(aHash2Descendant.MatchPointer <= kDescendantEmptyValue2)
{
if(aHash2Descendant.MatchPointer == kDescendantsNotInitilized2)
{
UINT32 aBase = aHashValue & 0xFFFF00;
for (UINT32 i = 0; i < 0x100; i++)
m_HashDescendants[aBase + i].MakeEmpty();
}
aHash2Descendant.MatchPointer = pos + kMatchStartValue2;
aHashDescendant.MatchPointer = pos + kMatchStartValue;
return 0;
}
aBacks[kNumHash2Bytes] = pos - (aHash2Descendant.MatchPointer - kMatchStartValue2) - 1;
aHash2Descendant.MatchPointer = pos + kMatchStartValue2;
#ifdef __AUTO_REMOVE
if (aBacks[kNumHash2Bytes] >= _sizeHistory)
{
if (aHashDescendant.IsNode())
RemoveNode(aHashDescendant.NodePointer);
aHashDescendant.MatchPointer = pos + kMatchStartValue;
return 0;
}
#endif
if (aFullCurrentLimit == kNumHash2Bytes)
return kNumHash2Bytes;
#else
UINT32 aHashValue = UINT32(GetIndexByte(1)) | (UINT32(GetIndexByte(0)) << 8);
CDescendant &aHashDescendant = m_HashDescendants[aHashValue];
#endif
if(m_SpecialMode)
{
if(aHashDescendant.IsMatch())
m_NumNotChangedCycles = 0;
if(m_NumNotChangedCycles >= _sizeHistory - 1)
{
ChangeLastMatch(aHashValue);
m_NumNotChangedCycles = 0;
}
if(GetIndexByte(aFullCurrentLimit - 1) == GetIndexByte(aFullCurrentLimit - 2))
{
if(aHashDescendant.IsMatch())
aHashDescendant.MatchPointer = pos + kMatchStartValue;
else
m_NumNotChangedCycles++;
for(UINT32 i = kNumHashBytes; i <= aFullCurrentLimit; i++)
aBacks[i] = 0;
return aFullCurrentLimit;
}
else if(m_NumNotChangedCycles > 0)
ChangeLastMatch(aHashValue);
m_SpecialMode = false;
}
if(aHashDescendant.IsEmpty())
{
aHashDescendant.MatchPointer = pos + kMatchStartValue;
return kPrevHashSize;
}
UINT32 aCurrentLimit = aFullCurrentLimit - kNumHashBytes;
if(aHashDescendant.IsMatch())
{
CMatchPointer aMatchPointer = aHashDescendant.MatchPointer;
UINT32 aBackReal = pos - (aMatchPointer - kMatchStartValue);
UINT32 aBack = aBackReal - 1;
#ifdef __AUTO_REMOVE
if (aBack >= _sizeHistory)
{
aHashDescendant.MatchPointer = pos + kMatchStartValue;
return kPrevHashSize;
}
#endif
UINT32 aMatchLen;
aBacks += kNumHashBytes;
BYTE *aBuffer = _buffer + pos;
for(aMatchLen = 0; true; aMatchLen++)
{
*aBacks++ = aBack;
if (aMatchLen == aCurrentLimit)
{
aHashDescendant.MatchPointer = pos + kMatchStartValue;
return kNumHashBytes + aMatchLen;
}
if (aBuffer[aMatchLen] != aBuffer[aMatchLen - aBackReal])
break;
}
// UINT32 aMatchLen = GetMatchLen(kNumHashBytes, aBack, aCurrentLimit);
UINT32 aFullMatchLen = aMatchLen + kNumHashBytes;
aHashDescendant.NodePointer = m_FreeNode;
CNodePointer aNode = &m_Nodes[m_FreeNode];
m_FreeNode = aNode->NextFreeNode;
#ifdef __AUTO_REMOVE
m_NumUsedNodes++;
#endif
if (m_FreeNode > m_FreeNodeMax)
{
m_FreeNodeMax = m_FreeNode;
m_Nodes[m_FreeNode].NextFreeNode = m_FreeNode + 1;
}
for (UINT32 i = 0; i < kNumSubNodes; i++)
aNode->Descendants[i].NodePointer = kDescendantEmptyValue;
aNode->LastMatch = pos;
BYTE aByteNew = GetIndexByte(aFullMatchLen);
BYTE aByteOld = GetIndexByte(aFullMatchLen - aBackReal);
BYTE aBitsNew, aBitsOld;
UINT32 aNumSameBits = aMatchLen * MY_BYTE_SIZE;
while (true)
{
aBitsNew = (aByteNew & kSubNodesMask);
aBitsOld = (aByteOld & kSubNodesMask);
if(aBitsNew != aBitsOld)
break;
aByteNew >>= kNumSubBits;
aByteOld >>= kNumSubBits;
aNumSameBits += kNumSubBits;
}
aNode->NumSameBits = CSameBitsType(aNumSameBits);
aNode->Descendants[aBitsNew].MatchPointer = pos + kMatchStartValue;
aNode->Descendants[aBitsOld].MatchPointer = aMatchPointer;
return aFullMatchLen;
}
const BYTE *aBaseCurrentBytePointer = _buffer + pos;
const BYTE *aCurrentBytePointer = aBaseCurrentBytePointer;
UINT32 aNumLoadedBits = 0;
BYTE aByte = 0;
CIndex *aNodePointerPointer = &aHashDescendant.NodePointer;
CNodePointer aNode = &m_Nodes[*aNodePointerPointer];
aBacks += kNumHashBytes;
const BYTE *aBytePointerLimit = aBaseCurrentBytePointer + aCurrentLimit;
const BYTE *aCurrentAddingOffset = _buffer;
#ifdef __AUTO_REMOVE
UINT32 aLowPos;
if (pos > _sizeHistory)
aLowPos = pos - _sizeHistory;
else
aLowPos = 0;
#endif
while(true)
{
#ifdef __AUTO_REMOVE
if (aNode->LastMatch < aLowPos)
{
RemoveNode(*aNodePointerPointer);
*aNodePointerPointer = pos + kMatchStartValue;
if (aCurrentBytePointer == aBaseCurrentBytePointer)
return kPrevHashSize;
return kNumHashBytes + (aCurrentBytePointer - aBaseCurrentBytePointer - 1);
}
#endif
if(aNumLoadedBits == 0)
{
*aBacks++ = pos - aNode->LastMatch - 1;
if(aCurrentBytePointer >= aBytePointerLimit)
{
for (UINT32 i = 0; i < kNumSubNodes; i++)
aNode->Descendants[i].MatchPointer = pos + kMatchStartValue;
aNode->LastMatch = pos;
aNode->NumSameBits = 0;
return aFullCurrentLimit;
}
aByte = (*aCurrentBytePointer++);
aCurrentAddingOffset++;
aNumLoadedBits = MY_BYTE_SIZE;
}
UINT32 aNumSameBits = aNode->NumSameBits;
if(aNumSameBits > 0)
{
BYTE aByteXOR = ((*(aCurrentAddingOffset + aNode->LastMatch -1)) >>
(MY_BYTE_SIZE - aNumLoadedBits)) ^ aByte;
while(aNumLoadedBits <= aNumSameBits)
{
if(aByteXOR != 0)
{
AddInternalNode(aNode, aNodePointerPointer, aByte, aByteXOR,
aNumSameBits, pos);
return kNumHashBytes + (aCurrentBytePointer - aBaseCurrentBytePointer - 1);
}
*aBacks++ = pos - aNode->LastMatch - 1;
aNumSameBits -= aNumLoadedBits;
if(aCurrentBytePointer >= aBytePointerLimit)
{
for (UINT32 i = 0; i < kNumSubNodes; i++)
aNode->Descendants[i].MatchPointer = pos + kMatchStartValue;
aNode->LastMatch = pos;
aNode->NumSameBits = CSameBitsType(aNode->NumSameBits - aNumSameBits);
return aFullCurrentLimit;
}
aNumLoadedBits = MY_BYTE_SIZE;
aByte = (*aCurrentBytePointer++);
aByteXOR = aByte ^ (*(aCurrentAddingOffset + aNode->LastMatch));
aCurrentAddingOffset++;
}
if((aByteXOR & ((1 << aNumSameBits) - 1)) != 0)
{
AddInternalNode(aNode, aNodePointerPointer, aByte, aByteXOR,
aNumSameBits, pos);
return kNumHashBytes + (aCurrentBytePointer - aBaseCurrentBytePointer - 1);
}
aByte >>= aNumSameBits;
aNumLoadedBits -= aNumSameBits;
}
UINT32 descendantIndex = (aByte & kSubNodesMask);
aNumLoadedBits -= kNumSubBits;
aNodePointerPointer = &aNode->Descendants[descendantIndex].NodePointer;
UINT32 aNextNodeIndex = *aNodePointerPointer;
aNode->LastMatch = pos;
if (aNextNodeIndex < kDescendantEmptyValue)
{
aByte >>= kNumSubBits;
aNode = &m_Nodes[aNextNodeIndex];
}
else if (aNextNodeIndex == kDescendantEmptyValue)
{
aNode->Descendants[descendantIndex].MatchPointer = pos + kMatchStartValue;
return kNumHashBytes + (aCurrentBytePointer - aBaseCurrentBytePointer - 1);
}
else
break;
}
UINT32 descendantIndex = (aByte & kSubNodesMask);
aByte >>= kNumSubBits;
CMatchPointer aMatchPointer = aNode->Descendants[descendantIndex].MatchPointer;
CMatchPointer aRealMatchPointer;
aRealMatchPointer = aMatchPointer - kMatchStartValue;
#ifdef __AUTO_REMOVE
if (aRealMatchPointer < aLowPos)
{
aNode->Descendants[descendantIndex].MatchPointer = pos + kMatchStartValue;
return kNumHashBytes + (aCurrentBytePointer - aBaseCurrentBytePointer - 1);
}
#endif
BYTE aByteXOR;
UINT32 aNumSameBits = 0;
if(aNumLoadedBits != 0)
{
BYTE aMatchByte = *(aCurrentAddingOffset + aRealMatchPointer -1);
aMatchByte >>= (MY_BYTE_SIZE - aNumLoadedBits);
aByteXOR = aMatchByte ^ aByte;
if(aByteXOR != 0)
{
AddLeafNode(aNode, aByte, aByteXOR, aNumSameBits, pos, descendantIndex);
return kNumHashBytes + (aCurrentBytePointer - aBaseCurrentBytePointer - 1);
}
aNumSameBits += aNumLoadedBits;
}
const BYTE *aMatchBytePointer = _buffer + aRealMatchPointer +
(aCurrentBytePointer - aBaseCurrentBytePointer);
for(; aCurrentBytePointer < aBytePointerLimit; aNumSameBits += MY_BYTE_SIZE)
{
aByte = (*aCurrentBytePointer++);
*aBacks++ = pos - aRealMatchPointer - 1;
aByteXOR = aByte ^ (*aMatchBytePointer++);
if(aByteXOR != 0)
{
AddLeafNode(aNode, aByte, aByteXOR, aNumSameBits, pos, descendantIndex);
return kNumHashBytes + (aCurrentBytePointer - aBaseCurrentBytePointer - 1);
}
}
*aBacks = pos - aRealMatchPointer - 1;
aNode->Descendants[descendantIndex].MatchPointer = pos + kMatchStartValue;
if(*aBacks == 0)
{
m_SpecialMode = true;
m_NumNotChangedCycles = 0;
}
return aFullCurrentLimit;
}
STDMETHODIMP_(void) CPatricia::DummyLongestMatch()
{
GetLongestMatch(m_TmpBacks);
}
// ------------------------------------
// Remove Match
typedef BYTE CRemoveDataWord;
static const int kSizeRemoveDataWordInBits = MY_BYTE_SIZE * sizeof(CRemoveDataWord);
#ifndef __AUTO_REMOVE
void CPatricia::RemoveMatch()
{
if(m_SpecialRemoveMode)
{
if(GetIndexByte(_matchMaxLen - 1 - _sizeHistory) ==
GetIndexByte(_matchMaxLen - _sizeHistory))
return;
m_SpecialRemoveMode = false;
}
UINT32 pos = _pos + kNumHashBytes - _sizeHistory;
#ifdef __HASH_3
// UINT32 aHashValue = (*((UINT32 *)(_buffer + _pos - _sizeHistory))) & 0xFFFFFF;
UINT32 aHashValueTemp = *((UINT32 *)(_buffer + _pos - _sizeHistory));
UINT32 aHashValue = ((aHashValueTemp << 8) |
((aHashValueTemp & 0xFFFFFF)>> 16)) & 0xFFFFFF;
CDescendant &aHashDescendant = m_HashDescendants[aHashValue];
CDescendant &aHash2Descendant = m_Hash2Descendants[aHashValueTemp & 0xFFFF];
if (aHash2Descendant >= kMatchStartValue2)
if(aHash2Descendant.MatchPointer == pos + kMatchStartValue2)
aHash2Descendant.MatchPointer = kDescendantEmptyValue2;
#else
UINT32 aHashValue = UINT32(GetIndexByte(1 - _sizeHistory)) |
(UINT32(GetIndexByte(0 - _sizeHistory)) << 8);
CDescendant &aHashDescendant = m_HashDescendants[aHashValue];
#endif
if(aHashDescendant.IsEmpty())
return;
if(aHashDescendant.IsMatch())
{
if(aHashDescendant.MatchPointer == pos + kMatchStartValue)
aHashDescendant.MakeEmpty();
return;
}
UINT32 descendantIndex;
const CRemoveDataWord *aCurrentPointer = (const CRemoveDataWord *)(_buffer + pos);
UINT32 aNumLoadedBits = 0;
CRemoveDataWord aWord;
CIndex *aNodePointerPointer = &aHashDescendant.NodePointer;
CNodePointer aNode = &m_Nodes[aHashDescendant.NodePointer];
while(true)
{
if(aNumLoadedBits == 0)
{
aWord = *aCurrentPointer++;
aNumLoadedBits = kSizeRemoveDataWordInBits;
}
UINT32 aNumSameBits = aNode->NumSameBits;
if(aNumSameBits > 0)
{
if (aNumLoadedBits <= aNumSameBits)
{
aNumSameBits -= aNumLoadedBits;
aCurrentPointer += (aNumSameBits / kSizeRemoveDataWordInBits);
aNumSameBits %= kSizeRemoveDataWordInBits;
aWord = *aCurrentPointer++;
aNumLoadedBits = kSizeRemoveDataWordInBits;
}
aWord >>= aNumSameBits;
aNumLoadedBits -= aNumSameBits;
}
descendantIndex = (aWord & kSubNodesMask);
aNumLoadedBits -= kNumSubBits;
aWord >>= kNumSubBits;
UINT32 aNextNodeIndex = aNode->Descendants[descendantIndex].NodePointer;
if (aNextNodeIndex < kDescendantEmptyValue)
{
aNodePointerPointer = &aNode->Descendants[descendantIndex].NodePointer;
aNode = &m_Nodes[aNextNodeIndex];
}
else
break;
}
if (aNode->Descendants[descendantIndex].MatchPointer != pos + kMatchStartValue)
{
const BYTE *aCurrentBytePointer = _buffer + _pos - _sizeHistory;
const BYTE *aCurrentBytePointerLimit = aCurrentBytePointer + _matchMaxLen;
for(;aCurrentBytePointer < aCurrentBytePointerLimit; aCurrentBytePointer++)
if(*aCurrentBytePointer != *(aCurrentBytePointer+1))
return;
m_SpecialRemoveMode = true;
return;
}
UINT32 aNumNodes = 0, aNumMatches = 0;
UINT32 i;
for (i = 0; i < kNumSubNodes; i++)
{
UINT32 aNodeIndex = aNode->Descendants[i].NodePointer;
if (aNodeIndex < kDescendantEmptyValue)
aNumNodes++;
else if (aNodeIndex > kDescendantEmptyValue)
aNumMatches++;
}
aNumMatches -= 1;
if (aNumNodes + aNumMatches > 1)
{
aNode->Descendants[descendantIndex].MakeEmpty();
return;
}
if(aNumNodes == 1)
{
UINT32 i;
for (i = 0; i < kNumSubNodes; i++)
if (aNode->Descendants[i].IsNode())
break;
UINT32 aNextNodeIndex = aNode->Descendants[i].NodePointer;
CNodePointer aNextNode = &m_Nodes[aNextNodeIndex];
aNextNode->NumSameBits += aNode->NumSameBits + kNumSubBits;
*aNode = *aNextNode;
aNextNode->NextFreeNode = m_FreeNode;
m_FreeNode = aNextNodeIndex;
return;
}
UINT32 aMatchPointer;
for (i = 0; i < kNumSubNodes; i++)
if (aNode->Descendants[i].IsMatch() && i != descendantIndex)
{
aMatchPointer = aNode->Descendants[i].MatchPointer;
break;
}
aNode->NextFreeNode = m_FreeNode;
m_FreeNode = *aNodePointerPointer;
*aNodePointerPointer = aMatchPointer;
}
#endif
const UINT32 kNormalizeStartPos = (UINT32(1) << (kNumBitsInIndex)) -
kMatchStartValue - kNumHashBytes - 1;
STDMETHODIMP CPatricia::MovePos()
{
#ifndef __AUTO_REMOVE
if(_pos >= _sizeHistory)
RemoveMatch();
#endif
RINOK(CLZInWindow::MovePos());
#ifdef __AUTO_REMOVE
if (m_NumUsedNodes >= m_NumNodes)
TestRemoveNodes();
#endif
if (_pos >= kNormalizeStartPos)
{
#ifdef __AUTO_REMOVE
TestRemoveNodesAndNormalize();
#else
Normalize();
#endif
}
return S_OK;
}
#ifndef __AUTO_REMOVE
void CPatricia::NormalizeDescendant(CDescendant &aDescendant, UINT32 aSubValue)
{
if (aDescendant.IsEmpty())
return;
if (aDescendant.IsMatch())
aDescendant.MatchPointer = aDescendant.MatchPointer - aSubValue;
else
{
CNode &aNode = m_Nodes[aDescendant.NodePointer];
aNode.LastMatch = aNode.LastMatch - aSubValue;
for (UINT32 i = 0; i < kNumSubNodes; i++)
NormalizeDescendant(aNode.Descendants[i], aSubValue);
}
}
void CPatricia::Normalize()
{
UINT32 aSubValue = _pos - _sizeHistory;
CLZInWindow::ReduceOffsets(aSubValue);
#ifdef __HASH_3
for(UINT32 aHash = 0; aHash < kHash2Size; aHash++)
{
CDescendant &aDescendant = m_Hash2Descendants[aHash];
if (aDescendant.MatchPointer != kDescendantsNotInitilized2)
{
UINT32 aBase = aHash << 8;
for (UINT32 i = 0; i < 0x100; i++)
NormalizeDescendant(m_HashDescendants[aBase + i], aSubValue);
}
if (aDescendant.MatchPointer < kMatchStartValue2)
continue;
aDescendant.MatchPointer = aDescendant.MatchPointer - aSubValue;
}
#else
for(UINT32 aHash = 0; aHash < kHashSize; aHash++)
NormalizeDescendant(m_HashDescendants[aHash], aSubValue);
#endif
}
#else
void CPatricia::TestRemoveDescendant(CDescendant &aDescendant, UINT32 aLimitPos)
{
CNode &aNode = m_Nodes[aDescendant.NodePointer];
UINT32 aNumChilds = 0;
UINT32 aChildIndex;
for (UINT32 i = 0; i < kNumSubNodes; i++)
{
CDescendant &aDescendant2 = aNode.Descendants[i];
if (aDescendant2.IsEmpty())
continue;
if (aDescendant2.IsMatch())
{
if (aDescendant2.MatchPointer < aLimitPos)
aDescendant2.MakeEmpty();
else
{
aNumChilds++;
aChildIndex = i;
}
}
else
{
TestRemoveDescendant(aDescendant2, aLimitPos);
if (!aDescendant2.IsEmpty())
{
aNumChilds++;
aChildIndex = i;
}
}
}
if (aNumChilds > 1)
return;
CIndex aNodePointerTemp = aDescendant.NodePointer;
if (aNumChilds == 1)
{
const CDescendant &aDescendant2 = aNode.Descendants[aChildIndex];
if (aDescendant2.IsNode())
m_Nodes[aDescendant2.NodePointer].NumSameBits += aNode.NumSameBits + kNumSubBits;
aDescendant = aDescendant2;
}
else
aDescendant.MakeEmpty();
aNode.NextFreeNode = m_FreeNode;
m_FreeNode = aNodePointerTemp;
m_NumUsedNodes--;
}
void CPatricia::RemoveNode(UINT32 anIndex)
{
CNode &aNode = m_Nodes[anIndex];
for (UINT32 i = 0; i < kNumSubNodes; i++)
{
CDescendant &aDescendant2 = aNode.Descendants[i];
if (aDescendant2.IsNode())
RemoveNode(aDescendant2.NodePointer);
}
aNode.NextFreeNode = m_FreeNode;
m_FreeNode = anIndex;
m_NumUsedNodes--;
}
void CPatricia::TestRemoveNodes()
{
UINT32 aLimitPos = kMatchStartValue + _pos - _sizeHistory + kNumHashBytes;
#ifdef __HASH_3
UINT32 aLimitPos2 = kMatchStartValue2 + _pos - _sizeHistory + kNumHashBytes;
for(UINT32 aHash = 0; aHash < kHash2Size; aHash++)
{
CDescendant &aDescendant = m_Hash2Descendants[aHash];
if (aDescendant.MatchPointer != kDescendantsNotInitilized2)
{
UINT32 aBase = aHash << 8;
for (UINT32 i = 0; i < 0x100; i++)
{
CDescendant &aDescendant = m_HashDescendants[aBase + i];
if (aDescendant.IsEmpty())
continue;
if (aDescendant.IsMatch())
{
if (aDescendant.MatchPointer < aLimitPos)
aDescendant.MakeEmpty();
}
else
TestRemoveDescendant(aDescendant, aLimitPos);
}
}
if (aDescendant.MatchPointer < kMatchStartValue2)
continue;
if (aDescendant.MatchPointer < aLimitPos2)
aDescendant.MatchPointer = kDescendantEmptyValue2;
}
#else
for(UINT32 aHash = 0; aHash < kHashSize; aHash++)
{
CDescendant &aDescendant = m_HashDescendants[aHash];
if (aDescendant.IsEmpty())
continue;
if (aDescendant.IsMatch())
{
if (aDescendant.MatchPointer < aLimitPos)
aDescendant.MakeEmpty();
}
else
TestRemoveDescendant(aDescendant, aLimitPos);
}
#endif
}
void CPatricia::TestRemoveAndNormalizeDescendant(CDescendant &aDescendant,
UINT32 aLimitPos, UINT32 aSubValue)
{
if (aDescendant.IsEmpty())
return;
if (aDescendant.IsMatch())
{
if (aDescendant.MatchPointer < aLimitPos)
aDescendant.MakeEmpty();
else
aDescendant.MatchPointer = aDescendant.MatchPointer - aSubValue;
return;
}
CNode &aNode = m_Nodes[aDescendant.NodePointer];
UINT32 aNumChilds = 0;
UINT32 aChildIndex;
for (UINT32 i = 0; i < kNumSubNodes; i++)
{
CDescendant &aDescendant2 = aNode.Descendants[i];
TestRemoveAndNormalizeDescendant(aDescendant2, aLimitPos, aSubValue);
if (!aDescendant2.IsEmpty())
{
aNumChilds++;
aChildIndex = i;
}
}
if (aNumChilds > 1)
{
aNode.LastMatch = aNode.LastMatch - aSubValue;
return;
}
CIndex aNodePointerTemp = aDescendant.NodePointer;
if (aNumChilds == 1)
{
const CDescendant &aDescendant2 = aNode.Descendants[aChildIndex];
if (aDescendant2.IsNode())
m_Nodes[aDescendant2.NodePointer].NumSameBits += aNode.NumSameBits + kNumSubBits;
aDescendant = aDescendant2;
}
else
aDescendant.MakeEmpty();
aNode.NextFreeNode = m_FreeNode;
m_FreeNode = aNodePointerTemp;
m_NumUsedNodes--;
}
void CPatricia::TestRemoveNodesAndNormalize()
{
UINT32 aSubValue = _pos - _sizeHistory;
UINT32 aLimitPos = kMatchStartValue + _pos - _sizeHistory + kNumHashBytes;
CLZInWindow::ReduceOffsets(aSubValue);
#ifdef __HASH_3
UINT32 aLimitPos2 = kMatchStartValue2 + _pos - _sizeHistory + kNumHashBytes;
for(UINT32 aHash = 0; aHash < kHash2Size; aHash++)
{
CDescendant &aDescendant = m_Hash2Descendants[aHash];
if (aDescendant.MatchPointer != kDescendantsNotInitilized2)
{
UINT32 aBase = aHash << 8;
for (UINT32 i = 0; i < 0x100; i++)
TestRemoveAndNormalizeDescendant(m_HashDescendants[aBase + i], aLimitPos, aSubValue);
}
if (aDescendant.MatchPointer < kMatchStartValue2)
continue;
if (aDescendant.MatchPointer < aLimitPos2)
aDescendant.MatchPointer = kDescendantEmptyValue2;
else
aDescendant.MatchPointer = aDescendant.MatchPointer - aSubValue;
}
#else
for(UINT32 aHash = 0; aHash < kHashSize; aHash++)
TestRemoveAndNormalizeDescendant(m_HashDescendants[aHash], aLimitPos, aSubValue);
#endif
}
#endif
STDMETHODIMP_(BYTE) CPatricia::GetIndexByte(UINT32 anIndex)
{
return CLZInWindow::GetIndexByte(anIndex);
}
STDMETHODIMP_(UINT32) CPatricia::GetMatchLen(UINT32 aIndex, UINT32 aBack, UINT32 aLimit)
{
return CLZInWindow::GetMatchLen(aIndex, aBack, aLimit);
}
STDMETHODIMP_(UINT32) CPatricia::GetNumAvailableBytes()
{
return CLZInWindow::GetNumAvailableBytes();
}
STDMETHODIMP_(const BYTE *) CPatricia::GetPointerToCurrentPos()
{
return CLZInWindow::GetPointerToCurrentPos();
}
}

8
7zip/Compress/LZ/StdAfx.h Executable file
View File

@@ -0,0 +1,8 @@
// stdafx.h
#ifndef __STDAFX_H
#define __STDAFX_H
#include <windows.h>
#endif