- lzma exehead is now 34kb instead of 34.5kb thanks to the new LZMA C SDK and should be much faster due to removal of critical section usage (not tested too much, test before usage)

- applied some parts of patch #875485


git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@3508 212acab6-be3b-0410-9dea-997c60f758d6
This commit is contained in:
kichik 2004-03-06 18:37:19 +00:00
parent ca9582c925
commit d18cea205c
23 changed files with 761 additions and 1267 deletions

View file

@ -1,29 +0,0 @@
// Stream/InByte.h
#include "LZMAState.h"
#ifndef __STREAM_INBYTE_H
#define __STREAM_INBYTE_H
class CInBuffer
{
CLZMAStateP m_lzmaState;
public:
void Init(CLZMAStateP lzmaState)
{
m_lzmaState = lzmaState;
}
BYTE ReadByte()
{
if (!m_lzmaState->avail_in)
{
LZMAGetIO(m_lzmaState);
}
if (!m_lzmaState->avail_in)
return 0;
m_lzmaState->avail_in--;
return *m_lzmaState->next_in++;
}
};
#endif

View file

@ -1,62 +0,0 @@
// LZMA.h
#include "LZMALenCoder.h"
#ifndef __LZMA_H
#define __LZMA_H
const int kNumRepDistances = 4;
const int kNumStates = 12;
const BYTE kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
const BYTE kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
const BYTE kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
const BYTE kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
class CState
{
public:
int Index;
void Init() { Index = 0; }
void UpdateChar() { Index = kLiteralNextStates[Index]; }
void UpdateMatch() { Index = kMatchNextStates[Index]; }
void UpdateRep() { Index = kRepNextStates[Index]; }
void UpdateShortRep() { Index = kShortRepNextStates[Index]; }
};
const int kNumPosSlotBits = 6;
const int kNumLenToPosStates = 4;
/*
inline int GetLenToPosState(int len)
{
if (len < kNumLenToPosStates + 2)
return len - 2;
return kNumLenToPosStates - 1;
}
*/
inline int GetLenToPosState2(int len)
{
if (len < kNumLenToPosStates)
return len;
return kNumLenToPosStates - 1;
}
const int kMatchMinLen = 2;
const int kMatchMaxLen = kMatchMinLen + kLenNumSymbolsTotal - 1;
const int kNumAlignBits = 4;
const int kAlignTableSize = 1 << kNumAlignBits;
const int kStartPosModelIndex = 4;
const int kEndPosModelIndex = 14;
const int kNumPosModels = kEndPosModelIndex - kStartPosModelIndex;
const int kNumFullDistances = 1 << (kEndPosModelIndex / 2);
const int kNumLitPosStatesBitsEncodingMax = 4;
const int kNumLitContextBitsMax = 8;
#endif

View file

@ -1,25 +0,0 @@
#ifndef __LZMACONF_H
#define __LZMACONF_H
// define __LOC_OPT for some speed optimization:
// It converts some class-member variables to local variables
// before some loops and it use inline code substitution
#define __LOC_OPT
// #define __UNROLL
#include <windows.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "../../../../exehead/util.h"
#ifdef __cplusplus
}
#endif
#define LZMAAlloc my_GlobalAlloc
#define LZMAFree GlobalFree
#define LZMAMemCopy mini_memcpy
#endif

View file

@ -1,171 +0,0 @@
// LZMADecoder.cpp
#include "LZMADecoder.h"
void CLZMADecoder::Create(BYTE *memoryPointer,
int numLiteralContextBits,
int numLiteralPosStateBits,
int numPosStateBits)
{
int numPosStates = 1 << numPosStateBits;
m_PosStateMask = numPosStates - 1;
m_LiteralDecoder.Create(memoryPointer, numLiteralPosStateBits, numLiteralContextBits);
}
UINT32 CLZMADecoder::Code(CLZMAStateP lzmaState)
{
m_RangeDecoder.Init(lzmaState);
m_OutWindowStream.Init(lzmaState);
int i;
for (i = 0; i < sizeof(m_Decoders) / sizeof(CBitDecoder); i++)
{
((CBitDecoder *) &m_Decoders)[i].Init();
}
m_LiteralDecoder.Init();
for (i = 0; i < kNumLenToPosStates; i++)
m_PosSlotDecoder[i].Init();
m_PosDecoder.Init();
m_PosAlignDecoder.Init();
// m_LenDecoder.Init(m_PosStateMask + 1);
// m_RepMatchLenDecoder.Init(m_PosStateMask + 1);
m_LenDecoder.Init();
m_RepMatchLenDecoder.Init();
////////////////////////
// code
CState state;
state.Init();
bool peviousIsMatch = false;
BYTE previousByte = 0;
// kNumRepDistances == 4
UINT32 repDistances[kNumRepDistances] = {1, 1, 1, 1};
/*for(i = 0 ; i < kNumRepDistances; i++)
repDistances[i] = 1;*/
UINT32 nowPos = 0;
while(nowPos < 0xFFFFFFFF)
{
int posState = nowPos & m_PosStateMask;
if (!m_Decoders.MainChoiceDecoders[state.Index][posState].Decode(&m_RangeDecoder))
{
state.UpdateChar();
if(peviousIsMatch)
{
BYTE matchByte = m_OutWindowStream.GetOneByte(0 - repDistances[0]);
previousByte = m_LiteralDecoder.DecodeWithMatchByte(&m_RangeDecoder,
nowPos, previousByte, matchByte);
peviousIsMatch = false;
}
else
previousByte = m_LiteralDecoder.DecodeNormal(&m_RangeDecoder,
nowPos, previousByte);
m_OutWindowStream.PutOneByte(previousByte);
nowPos++;
}
else
{
peviousIsMatch = true;
UINT32 distance;
int len;
if(m_Decoders.MatchChoiceDecoders[state.Index].Decode(&m_RangeDecoder))
{
if(!m_Decoders.MatchRepChoiceDecoders[state.Index].Decode(&m_RangeDecoder))
{
if(!m_Decoders.MatchRepShortChoiceDecoders[state.Index][posState].Decode(&m_RangeDecoder))
{
state.UpdateShortRep();
previousByte = m_OutWindowStream.GetOneByte(0 - repDistances[0]);
m_OutWindowStream.PutOneByte(previousByte);
nowPos++;
continue;
}
distance = repDistances[0];
}
else
{
if(!m_Decoders.MatchRep1ChoiceDecoders[state.Index].Decode(&m_RangeDecoder))
distance = repDistances[1];
else
{
if (!m_Decoders.MatchRep2ChoiceDecoders[state.Index].Decode(&m_RangeDecoder))
distance = repDistances[2];
else
{
distance = repDistances[3];
repDistances[3] = repDistances[2];
}
repDistances[2] = repDistances[1];
}
repDistances[1] = repDistances[0];
repDistances[0] = distance;
}
len = m_RepMatchLenDecoder.Decode(&m_RangeDecoder, posState);
state.UpdateRep();
}
else
{
len = m_LenDecoder.Decode(&m_RangeDecoder, posState);
state.UpdateMatch();
int posSlot = m_PosSlotDecoder[GetLenToPosState2(len)].Decode(&m_RangeDecoder);
if (posSlot >= kStartPosModelIndex)
{
int numDirectBits = ((posSlot >> 1) - 1);
distance = ((2 | (posSlot & 1)) << numDirectBits);
if (posSlot < kEndPosModelIndex)
distance += m_PosDecoder.Decode(&m_RangeDecoder, posSlot);
else
{
distance += (m_RangeDecoder.DecodeDirectBits(
numDirectBits - kNumAlignBits) << kNumAlignBits);
distance += m_PosAlignDecoder.Decode(&m_RangeDecoder);
}
}
else
distance = posSlot;
distance++;
repDistances[3] = repDistances[2];
repDistances[2] = repDistances[1];
repDistances[1] = repDistances[0];
repDistances[0] = distance;
//LZMAMemCopy(repDistances, repDistances + 1, kNumRepDistances * sizeof(UINT32));
}
// it's for stream version (without knowing uncompressed size)
//if (distance >= _dictionarySizeCheck)
if (!distance)
break;
if (distance > nowPos)
return (-1);
len += kMatchMinLen;
nowPos += len;
// CopyBackBlock
{
UINT32 fromPos = m_OutWindowStream._pos - distance;
if (fromPos >= m_OutWindowStream._windowSize)
fromPos += m_OutWindowStream._windowSize;
while (len--)
{
m_OutWindowStream._buffer[m_OutWindowStream._pos++] = m_OutWindowStream._buffer[fromPos++];
if (fromPos >= m_OutWindowStream._windowSize)
fromPos = 0;
m_OutWindowStream.Flush();
}
}
previousByte = m_OutWindowStream.GetOneByte((UINT32)(-1));
}
}
m_OutWindowStream._windowSize = 0;
m_OutWindowStream.Flush();
return 0;
}

View file

@ -1,85 +0,0 @@
// LZMADecoder.h
#ifndef __LZMADECODER_H
#define __LZMADECODER_H
#include "LZMAConf.h"
#include "LZMAState.h"
#include "LZMA.h"
#include "LZMALiteralCoder.h"
#include "LZOutWindow.h"
class CPosSpecDecoder
{
CBitDecoder m_Models[kNumFullDistances - kEndPosModelIndex];
public:
void Init()
{
for(int i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
m_Models[i].Init();
}
int Decode(CRangeDecoder *rangeDecoder, int slot)
{
int numLeveles = (slot >> 1) - 1;
CBitDecoder *models =
m_Models + (((2 | (slot & 1)) << numLeveles)) - slot - 1;
int modelIndex = 1;
int symbol = 0;
#ifdef __LOC_OPT
RC_INIT_VAR
#endif
for(int bitIndex = 0; bitIndex < numLeveles; bitIndex++)
{
#ifdef __LOC_OPT
RC_GETBIT2(models[modelIndex].Probability, modelIndex, ; , symbol |= (1 << bitIndex))
#else
int bit = models[modelIndex].Decode(rangeDecoder);
modelIndex <<= 1;
modelIndex += bit;
symbol |= (bit << bitIndex);
#endif
}
#ifdef __LOC_OPT
RC_FLUSH_VAR
#endif
return symbol;
};
};
class CLZMADecoder
{
CLZOutWindow m_OutWindowStream;
CRangeDecoder m_RangeDecoder;
struct
{
CBitDecoder MainChoiceDecoders[kNumStates][kLenNumPosStatesMax];
CBitDecoder MatchRepShortChoiceDecoders[kNumStates][kLenNumPosStatesMax];
CBitDecoder MatchChoiceDecoders[kNumStates];
CBitDecoder MatchRepChoiceDecoders[kNumStates];
CBitDecoder MatchRep1ChoiceDecoders[kNumStates];
CBitDecoder MatchRep2ChoiceDecoders[kNumStates];
} m_Decoders;
CLZMALiteralDecoder m_LiteralDecoder;
CBitTreeDecoder6 m_PosSlotDecoder[kNumLenToPosStates];
CPosSpecDecoder m_PosDecoder;
CReverseBitTreeDecoder4 m_PosAlignDecoder;
CLZMALenDecoder m_LenDecoder;
CLZMALenDecoder m_RepMatchLenDecoder;
int m_PosStateMask;
public:
void Create(BYTE *memoryPointer,
int numLiteralContextBits,
int numLiteralPosStateBits,
int numPosStateBits);
UINT32 Code(CLZMAStateP lzmaState);
};
#endif

View file

@ -1,53 +0,0 @@
// LZMALenCoder.h
#ifndef __LZMALENCODER_H
#define __LZMALENCODER_H
#include "RangeCoderBitTree.h"
const int kLenNumPosStatesBitsMax = 4;
const int kLenNumPosStatesMax = (1 << kLenNumPosStatesBitsMax);
const int kLenNumPosStatesBitsEncodingMax = 4;
const int kLenNumPosStatesEncodingMax = (1 << kLenNumPosStatesBitsEncodingMax);
const int kLenNumLowBits = 3;
const int kLenNumLowSymbols = 1 << kLenNumLowBits;
const int kLenNumMidBits = 3;
const int kLenNumMidSymbols = 1 << kLenNumMidBits;
const int kLenNumHighBits = 8;
const int kLenNumSymbolsTotal = kLenNumLowSymbols + kLenNumMidSymbols + (1 << kLenNumHighBits);
#define LOW 0
#define HIGH 1
class CLZMALenDecoder
{
CBitDecoder m_Choice;
CBitDecoder m_Choice2;
CBitTreeDecoder3 m_Coder[2][kLenNumPosStatesMax];
CBitTreeDecoder8 m_HighCoder;
public:
// void Init(int numPosStates)
void Init()
{
m_Choice.Init();
m_Choice2.Init();
for (int posState = 0; posState < kLenNumPosStatesMax * 2; posState++)
{
((CBitTreeDecoder3 *) m_Coder)[posState].Init();
}
m_HighCoder.Init();
}
int Decode(CRangeDecoder *rangeDecoder, int posState)
{
if(m_Choice.Decode(rangeDecoder) == 0)
return m_Coder[LOW][posState].Decode(rangeDecoder);
if(m_Choice2.Decode(rangeDecoder) == 0)
return kLenNumLowSymbols + m_Coder[HIGH][posState].Decode(rangeDecoder);
return kLenNumLowSymbols + kLenNumMidSymbols + m_HighCoder.Decode(rangeDecoder);
}
};
#endif

View file

@ -1,104 +0,0 @@
// LZMALiteralCoder.h
#ifndef __LZMALITERALCODER_H
#define __LZMALITERALCODER_H
#include "RangeCoderBit.h"
#include "RangeCoderOpt.h"
class CLZMALiteralDecoder2
{
CBitDecoder m_Decoders[3][1 << 8];
public:
void Init()
{
for (int i = 0; i < sizeof(m_Decoders) / sizeof(CBitDecoder); i++)
((CBitDecoder *) m_Decoders)[i].Init();
}
BYTE DecodeNormal(CRangeDecoder *rangeDecoder)
{
int symbol = 1;
#ifdef __LOC_OPT
RC_INIT_VAR
#endif
do
{
#ifdef __LOC_OPT
RC_GETBIT(m_Decoders[0][symbol].Probability, symbol)
#else
symbol = (symbol + symbol) | m_Decoders[0][symbol].Decode(rangeDecoder);
#endif
}
while (symbol < 0x100);
#ifdef __LOC_OPT
RC_FLUSH_VAR
#endif
return symbol;
}
BYTE DecodeWithMatchByte(CRangeDecoder *rangeDecoder, BYTE matchByte)
{
int symbol = 1;
#ifdef __LOC_OPT
RC_INIT_VAR
#endif
do
{
int matchBit = (matchByte >> 7) & 1;
matchByte <<= 1;
#ifdef __LOC_OPT
int bit;
RC_GETBIT2(m_Decoders[1 + matchBit][symbol].Probability, symbol,
bit = 0, bit = 1)
#else
int bit = m_Decoders[1 + matchBit][symbol].Decode(rangeDecoder);
symbol = (symbol + symbol) | bit;
#endif
if (matchBit != bit)
{
while (symbol < 0x100)
{
#ifdef __LOC_OPT
RC_GETBIT(m_Decoders[0][symbol].Probability, symbol)
#else
symbol = (symbol + symbol) | m_Decoders[0][symbol].Decode(rangeDecoder);
#endif
}
break;
}
}
while (symbol < 0x100);
#ifdef __LOC_OPT
RC_FLUSH_VAR
#endif
return symbol;
}
};
class CLZMALiteralDecoder
{
CLZMALiteralDecoder2 *m_Coders;
int m_NumPrevBits;
int m_PosMask;
public:
CLZMALiteralDecoder(): m_Coders(0) {}
void Create(BYTE *memory, int numPosBits, int numPrevBits)
{
m_PosMask = (1 << numPosBits) - 1;
m_NumPrevBits = numPrevBits;
m_Coders = (CLZMALiteralDecoder2 *) memory;
}
void Init()
{
int numStates = (m_PosMask + 1) << m_NumPrevBits;
for (int i = 0; i < numStates; i++)
m_Coders[i].Init();
}
int GetState(int pos, BYTE prevByte) const
{ return ((pos & m_PosMask) << m_NumPrevBits) + (prevByte >> (8 - m_NumPrevBits)); }
BYTE DecodeNormal(CRangeDecoder *rangeDecoder, int pos, BYTE prevByte)
{ return m_Coders[GetState(pos, prevByte)].DecodeNormal(rangeDecoder); }
BYTE DecodeWithMatchByte(CRangeDecoder *rangeDecoder, int pos, BYTE prevByte, BYTE matchByte)
{ return m_Coders[GetState(pos, prevByte)].DecodeWithMatchByte(rangeDecoder, matchByte); }
};
#endif

View file

@ -1,39 +0,0 @@
#include <Windows.h>
#ifndef __LZMA_STATE__H___
#define __LZMA_STATE__H___
typedef struct
{
void *lzmaDecoder;
void *DynamicData;
void *Dictionary;
UINT32 DictionarySize;
BYTE FirstProp;
HANDLE hThread; /* decompression thread */
BYTE *next_in; /* next input byte */
UINT avail_in; /* number of bytes available at next_in */
BYTE *next_out; /* next output byte should be put there */
UINT avail_out; /* remaining free space at next_out */
CRITICAL_SECTION cs;
BOOL cs_initialized;
BOOL it_locked; /* installer thread locked */
BOOL dt_locked; /* decompression thread locked */
BOOL finished;
int res;
} CLZMAState;
typedef CLZMAState
#ifndef __cplusplus
FAR
#endif
*CLZMAStateP;
void __stdcall LZMAGetIO(CLZMAStateP lzmaState);
#endif

View file

@ -1,64 +0,0 @@
// LZOutWindow.h
#ifndef __LZOUTWINDOW_H
#define __LZOUTWINDOW_H
#include "Types.h"
#include "LZMAState.h"
#include "LZMAConf.h"
class CLZOutWindow
{
public:
BYTE *_buffer;
UINT32 _pos;
UINT32 _windowSize;
UINT32 _streamPos;
CLZMAStateP m_lzmaState;
void Init(CLZMAStateP lzmaState)
{
m_lzmaState = lzmaState;
_buffer = (LPBYTE) lzmaState->Dictionary;
_windowSize = lzmaState->DictionarySize;
_streamPos = 0;
_pos = 0;
}
void PutOneByte(BYTE b)
{
_buffer[_pos++] = b;
Flush();
}
void Flush()
{
UINT32 size = _pos - _streamPos;
if (size < 65536 && _pos < _windowSize)
return;
CLZMAStateP lzmaState = m_lzmaState;
while (size--)
{
if (!lzmaState->avail_out)
{
LZMAGetIO(lzmaState);
}
*lzmaState->next_out = _buffer[_streamPos];
lzmaState->next_out++;
lzmaState->avail_out--;
_streamPos++;
}
if (_pos >= _windowSize)
{
_pos = 0;
_streamPos = 0;
}
}
BYTE GetOneByte(UINT32 index) const
{
UINT32 pos = _pos + index;
if (pos >= _windowSize)
pos += _windowSize;
return _buffer[pos];
}
};
#endif

View file

@ -1,87 +0,0 @@
// Compression/RangeCoder.h
#ifndef __COMPRESSION_RANGECODER_H
#define __COMPRESSION_RANGECODER_H
#include "InBuffer.h"
const int kNumTopBits = 24;
const UINT32 kTopValue = (1 << kNumTopBits);
class CRangeDecoder
{
public:
CInBuffer Stream;
UINT32 Range;
UINT32 Code;
void Normalize()
{
while (Range < kTopValue)
{
Code = (Code << 8) | Stream.ReadByte();
Range <<= 8;
}
}
void Init(CLZMAStateP state)
{
Stream.Init(state);
Code = 0;
Range = UINT32(-1);
for(int i = 0; i < 5; i++)
Code = (Code << 8) | Stream.ReadByte();
}
UINT32 DecodeDirectBits(int numTotalBits)
{
UINT32 range = Range;
UINT32 code = Code;
UINT32 result = 0;
for (int i = numTotalBits; i > 0; i--)
{
range >>= 1;
result <<= 1;
if (code >= range)
{
code -= range;
result |= 1;
}
/*UINT32 t = (code - range) >> 31;
code -= range & (t - 1);
// range = aRangeTmp + ((range & 1) & (1 - t));
result = (result + result) | (1 - t);*/
if (range < kTopValue)
{
code = (code << 8) | Stream.ReadByte();
range <<= 8;
}
}
Range = range;
Code = code;
return result;
}
int DecodeBit(UINT32 size0, int numTotalBits)
{
UINT32 newBound = (Range >> numTotalBits) * size0;
int symbol;
if (Code < newBound)
{
symbol = 0;
Range = newBound;
}
else
{
symbol = 1;
Code -= newBound;
Range -= newBound;
}
Normalize();
return symbol;
}
};
#endif

View file

@ -1,62 +0,0 @@
// RangeCoderBit.h
#ifndef __RANGECODERBIT_H
#define __RANGECODERBIT_H
#include "Types.h"
#include "RangeCoder.h"
const int kNumBitModelTotalBits = 11;
const int kBitModelTotal = (1 << kNumBitModelTotalBits);
const int kNumMoveBits = 5;
struct CBitModel
{
UINT32 Probability; // it's fast version on 32-bit systems
// unsigned short Probability; // use it if you want to reduce memory twice
void UpdateModel(int symbol)
{
/*
Probability -= (Probability + ((symbol - 1) & ((1 << numMoveBits) - 1))) >> numMoveBits;
Probability += (1 - symbol) << (kNumBitModelTotalBits - numMoveBits);
*/
if (symbol == 0)
Probability += (kBitModelTotal - Probability) >> kNumMoveBits;
else
Probability -= (Probability) >> kNumMoveBits;
}
void Init() { Probability = kBitModelTotal / 2; }
};
struct CBitDecoder: public CBitModel
{
int Decode(CRangeDecoder *rangeDecoder)
{
UINT32 newBound = (rangeDecoder->Range >> kNumBitModelTotalBits) * Probability;
int ret = 0;
if (rangeDecoder->Code < newBound)
{
rangeDecoder->Range = newBound;
Probability += (kBitModelTotal - Probability) >> kNumMoveBits;
}
else
{
rangeDecoder->Range -= newBound;
rangeDecoder->Code -= newBound;
Probability -= (Probability) >> kNumMoveBits;
ret = 1;
}
if (rangeDecoder->Range < kTopValue)
{
rangeDecoder->Code = (rangeDecoder->Code << 8) | rangeDecoder->Stream.ReadByte();
rangeDecoder->Range <<= 8;
}
return ret;
}
};
#endif

View file

@ -1,183 +0,0 @@
// RangeCoderBitTree.h
#ifndef __RANGECODERBITTREE_H
#define __RANGECODERBITTREE_H
#include "RangeCoderBit.h"
#include "RangeCoderOpt.h"
class CBitTreeDecoder3
{
CBitDecoder m_Models[1 << 3];
public:
void Init()
{
for(int i = 1; i < (1 << 3); i++)
m_Models[i].Init();
}
int Decode(CRangeDecoder *rangeDecoder)
{
int modelIndex = 1;
#ifdef __LOC_OPT
RC_INIT_VAR
#endif
#ifndef __UNROLL
for(int bitIndex = 3; bitIndex > 0; bitIndex--)
#endif
{
#ifdef __LOC_OPT
RC_GETBIT(m_Models[modelIndex].Probability, modelIndex)
#ifdef __UNROLL
RC_GETBIT(m_Models[modelIndex].Probability, modelIndex)
RC_GETBIT(m_Models[modelIndex].Probability, modelIndex)
#endif
#else
modelIndex = (modelIndex + modelIndex) + m_Models[modelIndex].Decode(rangeDecoder);
#ifdef __UNROLL
modelIndex = (modelIndex + modelIndex) + m_Models[modelIndex].Decode(rangeDecoder);
modelIndex = (modelIndex + modelIndex) + m_Models[modelIndex].Decode(rangeDecoder);
#endif
#endif
}
#ifdef __LOC_OPT
RC_FLUSH_VAR
#endif
return modelIndex - (1 << 3);
};
};
class CBitTreeDecoder6
{
CBitDecoder m_Models[1 << 6];
public:
void Init()
{
for(int i = 1; i < (1 << 6); i++)
m_Models[i].Init();
}
int Decode(CRangeDecoder *rangeDecoder)
{
int modelIndex = 1;
#ifdef __LOC_OPT
RC_INIT_VAR
#endif
#ifndef __UNROLL
for(int bitIndex = 6; bitIndex > 0; bitIndex--)
#endif
{
#ifdef __LOC_OPT
RC_GETBIT(m_Models[modelIndex].Probability, modelIndex)
#ifdef __UNROLL
RC_GETBIT(m_Models[modelIndex].Probability, modelIndex)
RC_GETBIT(m_Models[modelIndex].Probability, modelIndex)
RC_GETBIT(m_Models[modelIndex].Probability, modelIndex)
RC_GETBIT(m_Models[modelIndex].Probability, modelIndex)
RC_GETBIT(m_Models[modelIndex].Probability, modelIndex)
#endif
#else
modelIndex = (modelIndex + modelIndex) + m_Models[modelIndex].Decode(rangeDecoder);
#ifdef __UNROLL
modelIndex = (modelIndex + modelIndex) + m_Models[modelIndex].Decode(rangeDecoder);
modelIndex = (modelIndex + modelIndex) + m_Models[modelIndex].Decode(rangeDecoder);
modelIndex = (modelIndex + modelIndex) + m_Models[modelIndex].Decode(rangeDecoder);
modelIndex = (modelIndex + modelIndex) + m_Models[modelIndex].Decode(rangeDecoder);
modelIndex = (modelIndex + modelIndex) + m_Models[modelIndex].Decode(rangeDecoder);
#endif
#endif
}
#ifdef __LOC_OPT
RC_FLUSH_VAR
#endif
return modelIndex - (1 << 6);
};
};
class CBitTreeDecoder8
{
CBitDecoder m_Models[1 << 8];
public:
void Init()
{
for(int i = 1; i < (1 << 8); i++)
m_Models[i].Init();
}
int Decode(CRangeDecoder *rangeDecoder)
{
int modelIndex = 1;
#ifdef __LOC_OPT
RC_INIT_VAR
#endif
for(int bitIndex = 8; bitIndex > 0; bitIndex--)
{
#ifdef __LOC_OPT
RC_GETBIT(m_Models[modelIndex].Probability, modelIndex)
#else
modelIndex = (modelIndex + modelIndex) + m_Models[modelIndex].Decode(rangeDecoder);
#endif
}
#ifdef __LOC_OPT
RC_FLUSH_VAR
#endif
return modelIndex - (1 << 8);
};
};
class CReverseBitTreeDecoder4
{
CBitDecoder m_Models[1 << 4];
public:
void Init()
{
for(int i = 1; i < (1 << 4); i++)
m_Models[i].Init();
}
int Decode(CRangeDecoder *rangeDecoder)
{
int modelIndex = 1;
int symbol = 0;
#ifdef __LOC_OPT
RC_INIT_VAR
#endif
#ifndef __UNROLL
for(int bitIndex = 0; bitIndex < 4; bitIndex++)
#endif
{
#ifdef __LOC_OPT
#ifndef __UNROLL
RC_GETBIT2(m_Models[modelIndex].Probability, modelIndex, ; , symbol |= (1 << bitIndex))
#else
RC_GETBIT2(m_Models[modelIndex].Probability, modelIndex, ; , symbol |= (1 << 0))
RC_GETBIT2(m_Models[modelIndex].Probability, modelIndex, ; , symbol |= (1 << 1))
RC_GETBIT2(m_Models[modelIndex].Probability, modelIndex, ; , symbol |= (1 << 2))
RC_GETBIT2(m_Models[modelIndex].Probability, modelIndex, ; , symbol |= (1 << 3))
#endif
#else
#ifndef __UNROLL
int bit = m_Models[modelIndex].Decode(rangeDecoder);
modelIndex = modelIndex + modelIndex + bit;
symbol |= (bit << bitIndex);
#else
int bit = m_Models[modelIndex].Decode(rangeDecoder);
modelIndex = modelIndex + modelIndex + bit;
symbol |= (bit << 0);
bit = m_Models[modelIndex].Decode(rangeDecoder);
modelIndex = modelIndex + modelIndex + bit;
symbol |= (bit << 1);
bit = m_Models[modelIndex].Decode(rangeDecoder);
modelIndex = modelIndex + modelIndex + bit;
symbol |= (bit << 2);
bit = m_Models[modelIndex].Decode(rangeDecoder);
modelIndex = modelIndex + modelIndex + bit;
symbol |= (bit << 3);
#endif
#endif
}
#ifdef __LOC_OPT
RC_FLUSH_VAR
#endif
return symbol;
}
};
#endif

View file

@ -1,43 +0,0 @@
// RangeCoderOpt.h
#ifndef __RANGECODEROPT_H
#define __RANGECODEROPT_H
#include "RangeCoderBit.h"
#define RC_INIT_VAR \
UINT32 range = rangeDecoder->Range; \
UINT32 code = rangeDecoder->Code;
#define RC_FLUSH_VAR \
rangeDecoder->Range = range; \
rangeDecoder->Code = code;
#define RC_NORMALIZE \
if (range < kTopValue) \
{ range <<= 8; code = (code << 8) | rangeDecoder->Stream.ReadByte(); }
#define RC_GETBIT2(prob, modelIndex, Action0, Action1) \
{UINT32 newBound = (range >> kNumBitModelTotalBits) * prob; \
if (code < newBound) \
{ \
Action0; \
range = newBound; \
prob += (kBitModelTotal - prob) >> kNumMoveBits; \
modelIndex <<= 1; \
} \
else \
{ \
Action1; \
range -= newBound; \
code -= newBound; \
prob -= (prob) >> kNumMoveBits; \
modelIndex = (modelIndex + modelIndex) + 1; \
}} \
RC_NORMALIZE
//modelIndex <<= 1; if (code >= newBound) modelIndex++;
#define RC_GETBIT(prob, modelIndex) RC_GETBIT2(prob, modelIndex, ; , ;)
#endif

View file

@ -1,17 +0,0 @@
// Common/Types.h
#ifndef __COMMON_TYPES_H
#define __COMMON_TYPES_H
#ifdef WIN32
#include <basetsd.h>
#else
typedef unsigned int UINT32;
#endif
#ifndef _WINDOWS_
typedef unsigned char BYTE;
#endif
#endif

618
Source/7zip/LZMADecode.c Normal file
View file

@ -0,0 +1,618 @@
/*
LzmaDecode.c
LZMA Decoder
LZMA SDK 4.01 Copyright (c) 1999-2004 Igor Pavlov (2004-02-15)
Modified by Amir Szekely
*/
#include "LzmaDecode.h"
#ifndef Byte
#define Byte unsigned char
#endif
#define kNumTopBits 24
#define kTopValue ((UInt32)1 << kNumTopBits)
#define kNumBitModelTotalBits 11
#define kBitModelTotal (1 << kNumBitModelTotalBits)
#define kNumMoveBits 5
void LZMACALL LZMAGetIO(CLZMAStateP lzmaState)
{
InterlockedExchange(&lzmaState->sync_state, 0);
while (InterlockedCompareExchange(&lzmaState->sync_state, 2, 1) != 1)
Sleep(1);
}
Byte LZMACALL RangeDecoderReadByte(CLZMAStateP lzmaState)
{
if (!lzmaState->avail_in)
{
LZMAGetIO(lzmaState);
if (!lzmaState->avail_in)
{
lzmaState->Result = LZMA_RESULT_DATA_ERROR;
return 0xFF;
}
}
lzmaState->avail_in--;
return (*lzmaState->next_in++);
}
#define ReadByte (RangeDecoderReadByte(lzmaState))
FORCE_INLINE void LZMACALL RangeDecoderInit(CLZMAStateP lzmaState)
{
int i = 5;
lzmaState->Code = 0;
lzmaState->Range = (0xFFFFFFFF);
while (i--)
lzmaState->Code = (lzmaState->Code << 8) | ReadByte;
}
#define RC_INIT_VAR UInt32 range = lzmaState->Range; UInt32 code = lzmaState->Code;
#define RC_FLUSH_VAR lzmaState->Range = range; lzmaState->Code = code;
#define RC_NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | ReadByte; }
FORCE_INLINE UInt32 LZMACALL RangeDecoderDecodeDirectBits(CLZMAStateP lzmaState, int numTotalBits)
{
RC_INIT_VAR
UInt32 result = 0;
int i;
for (i = numTotalBits; i > 0; i--)
{
/* UInt32 t; */
range >>= 1;
result <<= 1;
if (code >= range)
{
code -= range;
result |= 1;
}
/*
t = (code - range) >> 31;
t &= 1;
code -= range & (t - 1);
result = (result + result) | (1 - t);
*/
RC_NORMALIZE
}
RC_FLUSH_VAR
return result;
}
int LZMACALL RangeDecoderBitDecode(CProb *prob, CLZMAStateP lzmaState)
{
UInt32 bound = (lzmaState->Range >> kNumBitModelTotalBits) * *prob;
if (lzmaState->Code < bound)
{
lzmaState->Range = bound;
*prob += (kBitModelTotal - *prob) >> kNumMoveBits;
if (lzmaState->Range < kTopValue)
{
lzmaState->Code = (lzmaState->Code << 8) | ReadByte;
lzmaState->Range <<= 8;
}
return 0;
}
else
{
lzmaState->Range -= bound;
lzmaState->Code -= bound;
*prob -= (*prob) >> kNumMoveBits;
if (lzmaState->Range < kTopValue)
{
lzmaState->Code = (lzmaState->Code << 8) | ReadByte;
lzmaState->Range <<= 8;
}
return 1;
}
}
#define RC_GET_BIT2(prob, mi, A0, A1) \
UInt32 bound = (range >> kNumBitModelTotalBits) * *prob; \
if (code < bound) \
{ A0; range = bound; *prob += (kBitModelTotal - *prob) >> kNumMoveBits; mi <<= 1; } \
else \
{ A1; range -= bound; code -= bound; *prob -= (*prob) >> kNumMoveBits; mi = (mi + mi) + 1; } \
RC_NORMALIZE
#define RC_GET_BIT(prob, mi) RC_GET_BIT2(prob, mi, ; , ;)
int LZMACALL RangeDecoderBitTreeDecode(CProb *probs, int numLevels, CLZMAStateP lzmaState)
{
int mi = 1;
int i;
#ifdef _LZMA_LOC_OPT
RC_INIT_VAR
#endif
for(i = numLevels; i > 0; i--)
{
#ifdef _LZMA_LOC_OPT
CProb *prob = probs + mi;
RC_GET_BIT(prob, mi)
#else
mi = (mi + mi) + RangeDecoderBitDecode(probs + mi, lzmaState);
#endif
}
#ifdef _LZMA_LOC_OPT
RC_FLUSH_VAR
#endif
return mi - (1 << numLevels);
}
int LZMACALL RangeDecoderReverseBitTreeDecode(CProb *probs, int numLevels, CLZMAStateP lzmaState)
{
int mi = 1;
int i;
int symbol = 0;
#ifdef _LZMA_LOC_OPT
RC_INIT_VAR
#endif
for(i = 0; i < numLevels; i++)
{
#ifdef _LZMA_LOC_OPT
CProb *prob = probs + mi;
RC_GET_BIT2(prob, mi, ; , symbol |= (1 << i))
#else
int bit = RangeDecoderBitDecode(probs + mi, lzmaState);
mi = mi + mi + bit;
symbol |= (bit << i);
#endif
}
#ifdef _LZMA_LOC_OPT
RC_FLUSH_VAR
#endif
return symbol;
}
FORCE_INLINE Byte LZMACALL LzmaLiteralDecode(CProb *probs, CLZMAStateP lzmaState)
{
int symbol = 1;
#ifdef _LZMA_LOC_OPT
RC_INIT_VAR
#endif
do
{
#ifdef _LZMA_LOC_OPT
CProb *prob = probs + symbol;
RC_GET_BIT(prob, symbol)
#else
symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, lzmaState);
#endif
}
while (symbol < 0x100);
#ifdef _LZMA_LOC_OPT
RC_FLUSH_VAR
#endif
return symbol;
}
FORCE_INLINE Byte LZMACALL LzmaLiteralDecodeMatch(CProb *probs, CLZMAStateP lzmaState, Byte matchByte)
{
int symbol = 1;
#ifdef _LZMA_LOC_OPT
RC_INIT_VAR
#endif
do
{
int bit;
int matchBit = (matchByte >> 7) & 1;
matchByte <<= 1;
#ifdef _LZMA_LOC_OPT
{
CProb *prob = probs + ((1 + matchBit) << 8) + symbol;
RC_GET_BIT2(prob, symbol, bit = 0, bit = 1)
}
#else
bit = RangeDecoderBitDecode(probs + ((1 + matchBit) << 8) + symbol, lzmaState);
symbol = (symbol << 1) | bit;
#endif
if (matchBit != bit)
{
while (symbol < 0x100)
{
#ifdef _LZMA_LOC_OPT
CProb *prob = probs + symbol;
RC_GET_BIT(prob, symbol)
#else
symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, lzmaState);
#endif
}
break;
}
}
while (symbol < 0x100);
#ifdef _LZMA_LOC_OPT
RC_FLUSH_VAR
#endif
return symbol;
}
#define kNumPosBitsMax 4
#define kNumPosStatesMax (1 << kNumPosBitsMax)
#define kLenNumLowBits 3
#define kLenNumLowSymbols (1 << kLenNumLowBits)
#define kLenNumMidBits 3
#define kLenNumMidSymbols (1 << kLenNumMidBits)
#define kLenNumHighBits 8
#define kLenNumHighSymbols (1 << kLenNumHighBits)
#define LenChoice 0
#define LenChoice2 (LenChoice + 1)
#define LenLow (LenChoice2 + 1)
#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
int LZMACALL LzmaLenDecode(CProb *p, CLZMAStateP lzmaState, int posState)
{
if(RangeDecoderBitDecode(p + LenChoice, lzmaState) == 0)
return RangeDecoderBitTreeDecode(p + LenLow +
(posState << kLenNumLowBits), kLenNumLowBits, lzmaState);
if(RangeDecoderBitDecode(p + LenChoice2, lzmaState) == 0)
return kLenNumLowSymbols + RangeDecoderBitTreeDecode(p + LenMid +
(posState << kLenNumMidBits), kLenNumMidBits, lzmaState);
return kLenNumLowSymbols + kLenNumMidSymbols +
RangeDecoderBitTreeDecode(p + LenHigh, kLenNumHighBits, lzmaState);
}
#define kNumStates 12
#define kStartPosModelIndex 4
#define kEndPosModelIndex 14
#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
#define kNumPosSlotBits 6
#define kNumLenToPosStates 4
#define kNumAlignBits 4
#define kAlignTableSize (1 << kNumAlignBits)
#define kMatchMinLen 2
#define IsMatch 0
#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
#define IsRepG0 (IsRep + kNumStates)
#define IsRepG1 (IsRepG0 + kNumStates)
#define IsRepG2 (IsRepG1 + kNumStates)
#define IsRep0Long (IsRepG2 + kNumStates)
#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
#define LenCoder (Align + kAlignTableSize)
#define RepLenCoder (LenCoder + kNumLenProbs)
#define Literal (RepLenCoder + kNumLenProbs)
#if Literal != LZMA_BASE_SIZE
StopCompilingDueBUG
#endif
int LZMACALL LzmaDecoderInit(CLZMAStateP lzmaState)
{
CProb *p = (CProb *) lzmaState->DynamicData;
UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lzmaState->lc + lzmaState->lp));
if (lzmaState->DynamicDataSize < numProbs * sizeof(CProb))
return LZMA_RESULT_NOT_ENOUGH_MEM;
lzmaState->Result = LZMA_RESULT_OK;
lzmaState->DictionaryPos = 0;
lzmaState->GlobalPos = 0;
lzmaState->Reps[0] = lzmaState->Reps[1] = lzmaState->Reps[2] = lzmaState->Reps[3] = 1;
lzmaState->State = 0;
lzmaState->PreviousIsMatch = 0;
lzmaState->RemainLen = 0;
lzmaState->Dictionary[lzmaState->DictionarySize - 1] = 0;
while (numProbs--)
p[numProbs] = kBitModelTotal >> 1;
RangeDecoderInit(lzmaState);
return LZMA_RESULT_OK;
}
int LZMACALL LzmaDecode(CLZMAStateP lzmaState)
{
CProb *p = (CProb *) lzmaState->DynamicData;
int state = lzmaState->State;
int previousIsMatch = lzmaState->PreviousIsMatch;
Byte previousByte;
UInt32 rep0 = lzmaState->Reps[0], rep1 = lzmaState->Reps[1], rep2 = lzmaState->Reps[2], rep3 = lzmaState->Reps[3];
UInt32 nowPos = 0;
UInt32 posStateMask = (1 << (lzmaState->pb)) - 1;
UInt32 literalPosMask = (1 << (lzmaState->lp)) - 1;
int lc = lzmaState->lc;
int len = lzmaState->RemainLen;
UInt32 globalPos = lzmaState->GlobalPos;
Byte *dictionary = lzmaState->Dictionary;
UInt32 dictionarySize = lzmaState->DictionarySize;
UInt32 dictionaryPos = lzmaState->DictionaryPos;
UInt32 outSize = lzmaState->avail_out;
unsigned char *outStream = lzmaState->next_out;
if (len == -1)
return LZMA_RESULT_OK;
while(len > 0 && nowPos < outSize)
{
UInt32 pos = dictionaryPos - rep0;
if (pos >= dictionarySize)
pos += dictionarySize;
outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
if (++dictionaryPos == dictionarySize)
dictionaryPos = 0;
len--;
}
if (dictionaryPos == 0)
previousByte = dictionary[dictionarySize - 1];
else
previousByte = dictionary[dictionaryPos - 1];
while(nowPos < outSize)
{
int posState = (int)((nowPos + globalPos) & posStateMask);
if (lzmaState->Result != LZMA_RESULT_OK)
return lzmaState->Result;
if (RangeDecoderBitDecode(p + IsMatch + (state << kNumPosBitsMax) + posState, lzmaState) == 0)
{
CProb *probs = p + Literal + (LZMA_LIT_SIZE *
((((nowPos + globalPos) & literalPosMask) << lc) + (previousByte >> (8 - lc))));
if (state < 4) state = 0;
else if (state < 10) state -= 3;
else state -= 6;
if (previousIsMatch)
{
Byte matchByte;
UInt32 pos = dictionaryPos - rep0;
if (pos >= dictionarySize)
pos += dictionarySize;
matchByte = dictionary[pos];
previousByte = LzmaLiteralDecodeMatch(probs, lzmaState, matchByte);
previousIsMatch = 0;
}
else
previousByte = LzmaLiteralDecode(probs, lzmaState);
outStream[nowPos++] = previousByte;
dictionary[dictionaryPos] = previousByte;
if (++dictionaryPos == dictionarySize)
dictionaryPos = 0;
}
else
{
previousIsMatch = 1;
if (RangeDecoderBitDecode(p + IsRep + state, lzmaState) == 1)
{
if (RangeDecoderBitDecode(p + IsRepG0 + state, lzmaState) == 0)
{
if (RangeDecoderBitDecode(p + IsRep0Long + (state << kNumPosBitsMax) + posState, lzmaState) == 0)
{
UInt32 pos;
if ((nowPos + globalPos) == 0)
return LZMA_RESULT_DATA_ERROR;
state = state < 7 ? 9 : 11;
pos = dictionaryPos - rep0;
if (pos >= dictionarySize)
pos += dictionarySize;
previousByte = dictionary[pos];
dictionary[dictionaryPos] = previousByte;
if (++dictionaryPos == dictionarySize)
dictionaryPos = 0;
outStream[nowPos++] = previousByte;
continue;
}
}
else
{
UInt32 distance;
if(RangeDecoderBitDecode(p + IsRepG1 + state, lzmaState) == 0)
distance = rep1;
else
{
if(RangeDecoderBitDecode(p + IsRepG2 + state, lzmaState) == 0)
distance = rep2;
else
{
distance = rep3;
rep3 = rep2;
}
rep2 = rep1;
}
rep1 = rep0;
rep0 = distance;
}
len = LzmaLenDecode(p + RepLenCoder, lzmaState, posState);
state = state < 7 ? 8 : 11;
}
else
{
int posSlot;
rep3 = rep2;
rep2 = rep1;
rep1 = rep0;
state = state < 7 ? 7 : 10;
len = LzmaLenDecode(p + LenCoder, lzmaState, posState);
posSlot = RangeDecoderBitTreeDecode(p + PosSlot +
((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
kNumPosSlotBits), kNumPosSlotBits, lzmaState);
if (posSlot >= kStartPosModelIndex)
{
int numDirectBits = ((posSlot >> 1) - 1);
rep0 = ((2 | ((UInt32)posSlot & 1)) << numDirectBits);
if (posSlot < kEndPosModelIndex)
{
rep0 += RangeDecoderReverseBitTreeDecode(
p + SpecPos + rep0 - posSlot - 1, numDirectBits, lzmaState);
}
else
{
rep0 += RangeDecoderDecodeDirectBits(lzmaState,
numDirectBits - kNumAlignBits) << kNumAlignBits;
rep0 += RangeDecoderReverseBitTreeDecode(p + Align, kNumAlignBits, lzmaState);
}
}
else
rep0 = posSlot;
rep0++;
}
if (rep0 == (UInt32)(0))
{
/* it's for stream version */
len = -1;
break;
}
if (rep0 > nowPos + globalPos)
{
return LZMA_RESULT_DATA_ERROR;
}
len += kMatchMinLen;
do
{
UInt32 pos = dictionaryPos - rep0;
if (pos >= dictionarySize)
pos += dictionarySize;
previousByte = dictionary[pos];
dictionary[dictionaryPos] = previousByte;
if (++dictionaryPos == dictionarySize)
dictionaryPos = 0;
outStream[nowPos++] = previousByte;
len--;
}
while(len > 0 && nowPos < outSize);
}
}
lzmaState->DictionaryPos = dictionaryPos;
lzmaState->GlobalPos = globalPos + nowPos;
lzmaState->Reps[0] = rep0;
lzmaState->Reps[1] = rep1;
lzmaState->Reps[2] = rep2;
lzmaState->Reps[3] = rep3;
lzmaState->State = state;
lzmaState->PreviousIsMatch = previousIsMatch;
lzmaState->RemainLen = len;
return nowPos;
}
// interface
void LZMACALL lzmaInit(CLZMAStateP lzmaState)
{
if (lzmaState->hThread)
{
CloseHandle(lzmaState->hThread);
lzmaState->hThread = NULL;
}
lzmaState->sync_state = 1;
lzmaState->finished = FALSE;
}
#define kPropertiesSize 5
DWORD WINAPI lzmaDecompressThread(LPVOID lpParameter)
{
CLZMAStateP lzmaState = (CLZMAStateP) lpParameter;
LPBYTE properties;
BYTE firstByte;
UINT32 dictionarySize;
int res;
lzmaState->res = -4;
if (lzmaState->avail_in < kPropertiesSize)
{
goto finished;
}
properties = lzmaState->next_in;
lzmaState->avail_in -= kPropertiesSize;
lzmaState->next_in += kPropertiesSize;
firstByte = properties[0];
if (firstByte > (9*5*5))
{
goto finished;
}
lzmaState->pb = firstByte / (9*5);
firstByte %= (9*5);
lzmaState->lp = firstByte / 9;
firstByte %= 9;
lzmaState->lc = firstByte;
lzmaState->DynamicDataSize = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lzmaState->lc + lzmaState->pb));
lzmaState->DynamicDataSize *= sizeof(CProb);
if (!lzmaState->DynamicData || firstByte != lzmaState->FirstProp)
{
if (lzmaState->DynamicData)
GlobalFree(lzmaState->DynamicData);
lzmaState->DynamicData = LZMAAlloc(lzmaState->DynamicDataSize);
lzmaState->FirstProp = firstByte;
}
dictionarySize = *(UINT32 *)(properties + 1);
if (dictionarySize != lzmaState->DictionarySize)
{
if (lzmaState->Dictionary)
GlobalFree(lzmaState->Dictionary);
lzmaState->Dictionary = LZMAAlloc(dictionarySize);
lzmaState->DictionarySize = dictionarySize;
}
LzmaDecoderInit(lzmaState);
for (;;)
{
res = LzmaDecode(lzmaState);
if (res <= 0)
break;
lzmaState->next_out += res;
lzmaState->avail_out -= res;
LZMAGetIO(lzmaState);
}
lzmaState->res = 1;
if (res < 0)
lzmaState->res = res;
finished:
lzmaState->finished = TRUE;
InterlockedExchange(&lzmaState->sync_state, 0);
return 0;
}
int LZMACALL lzmaDecompress(CLZMAStateP lzmaState)
{
if (lzmaState->finished)
return lzmaState->res;
if (!lzmaState->hThread)
{
DWORD dwThreadId;
lzmaState->hThread = CreateThread(0, 0, lzmaDecompressThread, (LPVOID) lzmaState, 0, &dwThreadId);
if (!lzmaState->hThread)
return -4;
}
else
InterlockedExchange(&lzmaState->sync_state, 1);
while (InterlockedCompareExchange(&lzmaState->sync_state, 2, 0) != 0)
Sleep(1);
if (lzmaState->finished)
return lzmaState->res;
return 0;
}

109
Source/7zip/LZMADecode.h Normal file
View file

@ -0,0 +1,109 @@
/*
LzmaDecode.h
LZMA Decoder interface
LZMA SDK 4.01 Copyright (c) 1999-2004 Igor Pavlov (2004-02-15)
*/
#ifndef __LZMADECODE_H
#define __LZMADECODE_H
#include "../Platform.h"
#include "../exehead/util.h"
/* #define _LZMA_PROB32 */
/* It can increase speed on some 32-bit CPUs,
but memory usage will be doubled in that case */
#define _LZMA_LOC_OPT
/* Enable local speed optimizations inside code */
#ifndef LZMACALL
# define LZMACALL stdcall
#endif
#ifndef FORCE_INLINE
# define FORCE_INLINE
#endif
#ifndef UInt32
#ifdef _LZMA_UINT32_IS_ULONG
#define UInt32 unsigned long
#else
#define UInt32 unsigned int
#endif
#endif
#ifdef _LZMA_PROB32
#define CProb UInt32
#else
#define CProb unsigned short
#endif
#define LZMA_RESULT_OK 0
#define LZMA_RESULT_DATA_ERROR -1
// we don't really care what the problem is...
// #define LZMA_RESULT_NOT_ENOUGH_MEM -2
#define LZMA_RESULT_NOT_ENOUGH_MEM LZMA_RESULT_DATA_ERROR
#define LZMA_BASE_SIZE 1846
#define LZMA_LIT_SIZE 768
/*
bufferSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb)
by default CProb is unsigned short,
but if specify _LZMA_PROB_32, CProb will be UInt32(unsigned int)
*/
typedef struct
{
unsigned char FirstProp;
unsigned char *DynamicData;
UInt32 DynamicDataSize;
unsigned char *Dictionary;
UInt32 DictionarySize;
UInt32 DictionaryPos;
// range coder
UInt32 Range;
UInt32 Code;
int Result;
// others
UInt32 GlobalPos;
UInt32 Reps[4];
int lc;
int lp;
int pb;
int State;
int PreviousIsMatch;
int RemainLen;
// io
unsigned char *next_in; /* next input byte */
unsigned int avail_in; /* number of bytes available at next_in */
unsigned char *next_out; /* next output byte should be put there */
unsigned int avail_out; /* remaining free space at next_out */
// sync
HANDLE hThread;
long sync_state;
// finish
int finished;
int res;
} CLZMAState, *CLZMAStateP;
int LZMACALL LzmaDecoderInit(CLZMAStateP lzmaState);
int LZMACALL LzmaDecode(CLZMAStateP lzmaState);
void LZMACALL lzmaInit(CLZMAStateP lzmaState);
int LZMACALL lzmaDecompress(CLZMAStateP lzmaState);
#define LZMAAlloc my_GlobalAlloc
#define LZMAFree GlobalFree
#define LZMAMemCopy mini_memcpy
#endif

View file

@ -1,155 +0,0 @@
// lzmaNSIS.h
#include <windows.h>
#include "lzmaNSIS.h"
#include "7zip/Compress/LZMA_SMALL/LZMAConf.h"
#include "7zip/Compress/LZMA_SMALL/LZMADecoder.h"
void __stdcall LZMAGetIO(CLZMAStateP lzmaState)
{
LeaveCriticalSection(&lzmaState->cs);
while (!lzmaState->it_locked)
Sleep(0);
lzmaState->dt_locked = FALSE;
EnterCriticalSection(&lzmaState->cs);
lzmaState->dt_locked = TRUE;
while (lzmaState->it_locked)
Sleep(0);
}
extern "C"
{
void __stdcall lzmaInit(CLZMAStateP lzmaState)
{
if (lzmaState->hThread)
{
CloseHandle(lzmaState->hThread);
lzmaState->hThread = NULL;
}
if (!lzmaState->lzmaDecoder)
lzmaState->lzmaDecoder = LZMAAlloc(sizeof(CLZMADecoder));
if (!lzmaState->cs_initialized)
{
InitializeCriticalSection(&lzmaState->cs);
lzmaState->cs_initialized = TRUE;
}
lzmaState->it_locked = TRUE;
lzmaState->dt_locked = FALSE;
lzmaState->finished = FALSE;
}
DWORD WINAPI lzmaDecompressThread(LPVOID lpParameter)
{
CLZMAStateP lzmaState = (CLZMAStateP) lpParameter;
CLZMADecoder *lzmaDecodeder = (CLZMADecoder *) lzmaState->lzmaDecoder;
EnterCriticalSection(&lzmaState->cs);
lzmaState->dt_locked = TRUE;
while (lzmaState->it_locked)
Sleep(0);
{
const kPropertiesSize = 5;
lzmaState->res = -4;
if (lzmaState->avail_in < kPropertiesSize)
{
goto finished;
}
LPBYTE properties = lzmaState->next_in;
lzmaState->avail_in -= kPropertiesSize;
lzmaState->next_in += kPropertiesSize;
BYTE firstByte = properties[0];
if (firstByte > (9*5*5))
{
goto finished;
}
int numPosStateBits = firstByte / (9*5);
firstByte %= (9*5);
int numLiteralPosStateBits = firstByte / 9;
firstByte %= 9;
int numLiteralContextBits = firstByte;
int memSize = (1 << (numLiteralContextBits + numLiteralPosStateBits)) * sizeof(CLZMALiteralDecoder2);
if (lzmaState->DynamicData == 0 || firstByte != lzmaState->FirstProp)
{
if (lzmaState->DynamicData != 0)
LZMAFree(lzmaState->DynamicData);
lzmaState->DynamicData = LZMAAlloc(memSize);
lzmaState->FirstProp = firstByte;
}
lzmaDecodeder->Create((LPBYTE) lzmaState->DynamicData,
numLiteralContextBits, numLiteralPosStateBits, numPosStateBits);
UINT32 dictionarySize = *(UINT32 *)(properties + 1);
if (dictionarySize != lzmaState->DictionarySize)
{
if (lzmaState->Dictionary)
LZMAFree(lzmaState->Dictionary);
lzmaState->Dictionary = LZMAAlloc(dictionarySize);
lzmaState->DictionarySize = dictionarySize;
}
UINT32 res = lzmaDecodeder->Code(lzmaState);
lzmaState->res = 1;
if (res != 0)
lzmaState->res = res;
}
finished:
lzmaState->finished = TRUE;
LeaveCriticalSection(&lzmaState->cs);
lzmaState->dt_locked = FALSE;
return 0;
}
int __stdcall lzmaDecompress(CLZMAStateP lzmaState)
{
if (!lzmaState->hThread)
{
DWORD dwThreadId;
lzmaState->hThread = CreateThread(0, 0, lzmaDecompressThread, (LPVOID) lzmaState, 0, &dwThreadId);
if (!lzmaState->hThread)
return -4;
}
else
LeaveCriticalSection(&lzmaState->cs);
while (!lzmaState->dt_locked)
Sleep(0);
lzmaState->it_locked = FALSE;
EnterCriticalSection(&lzmaState->cs);
lzmaState->it_locked = TRUE;
while (lzmaState->dt_locked)
Sleep(0);
if (lzmaState->finished)
{
LeaveCriticalSection(&lzmaState->cs);
return lzmaState->res;
}
return 0;
}
} // extern "C"

View file

@ -1,20 +0,0 @@
// lzmaNSIS.h
#include "7zip/Compress/LZMA_SMALL/LZMAState.h"
#ifndef __LZMANSIS_H
#define __LZMANSIS_H
#ifdef __cplusplus
extern "C" {
#endif
void __stdcall lzmaInit(CLZMAStateP lzmaState);
int __stdcall lzmaDecompress(CLZMAStateP lzmaState);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,8 +1,33 @@
#ifndef ___PLATFORM__H___
#define ___PLATFORM__H___
#include <Windows.h>
#include <commctrl.h>
// includes
#ifdef _WIN32
# include <Windows.h>
# include <commctrl.h>
#else
# define WORD unsigned short
# define DWORD unsigned long
#endif
// attributes
#ifdef _MSC_VER
# define FORCE_INLINE __forceinline
#else
# ifdef __GNUC__
# if __GNUC__ < 3
# define FORCE_INLINE inline
# else
# define FORCE_INLINE inline __attribute__ ((always_inline))
# endif
# else
# define FORCE_INLINE inline
# endif
#endif
// defines
#ifndef FOF_NOERRORUI
#define FOF_NOERRORUI 0x0400

View file

@ -204,7 +204,7 @@ lang_again:
}
}
__forceinline int NSISCALL ui_doinstall(void)
FORCE_INLINE int NSISCALL ui_doinstall(void)
{
header *header = g_header;
static WNDCLASS wc; // richedit subclassing and bgbg creation

View file

@ -39,7 +39,7 @@ RSC=rc.exe
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O1 /Oy /D "_WINDOWS" /D "EXEHEAD" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "WIN32_LEAN_AND_MEAN" /D "NSIS_COMPRESS_USE_LZMA" /D ZEXPORT=__stdcall /FD /c
# ADD CPP /nologo /W3 /GX /O1 /Oy /D "_WINDOWS" /D "EXEHEAD" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "WIN32_LEAN_AND_MEAN" /D "NSIS_COMPRESS_USE_LZMA" /D LZMACALL=__stdcall /FD /c
# SUBTRACT CPP /Fr /YX /Yc /Yu
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
@ -67,65 +67,12 @@ PostBuild_Cmds=bin2h Release-lzma\exehead_lzma.exe Release-lzma\exehead_lzma.h l
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\7zip\7zip\Compress\LZMA_SMALL\InBuffer.h
SOURCE=..\7zip\LZMADecode.c
# ADD CPP /Gd
# End Source File
# Begin Source File
SOURCE=..\7zip\7zip\Compress\LZMA_SMALL\LZMA.h
# End Source File
# Begin Source File
SOURCE=..\7zip\7zip\Compress\LZMA_SMALL\LZMADecoder.cpp
# ADD CPP /D "__STREAM_VERSION"
# End Source File
# Begin Source File
SOURCE=..\7zip\7zip\Compress\LZMA_SMALL\LZMADecoder.h
# End Source File
# Begin Source File
SOURCE=..\7zip\7zip\Compress\LZMA_SMALL\LZMALenCoder.h
# End Source File
# Begin Source File
SOURCE=..\7zip\7zip\Compress\LZMA_SMALL\LZMALiteralCoder.h
# End Source File
# Begin Source File
SOURCE=..\7zip\7zip\Compress\LZMA_SMALL\LZOutWindow.h
# End Source File
# Begin Source File
SOURCE=..\7zip\7zip\Compress\LZMA_SMALL\RangeCoder.h
# End Source File
# Begin Source File
SOURCE=..\7zip\7zip\Compress\LZMA_SMALL\RangeCoderBit.h
# End Source File
# Begin Source File
SOURCE=..\7zip\7zip\Compress\LZMA_SMALL\RangeCoderBitTree.h
# End Source File
# Begin Source File
SOURCE=..\7zip\7zip\Compress\LZMA_SMALL\RangeCoderOpt.h
# End Source File
# Begin Source File
SOURCE=..\7zip\7zip\Compress\LZMA_SMALL\Types.h
# End Source File
# End Group
# Begin Group "lzmaNSIS"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\7zip\lzmaNSIS.cpp
# ADD CPP /D "__STREAM_VERSION"
# End Source File
# Begin Source File
SOURCE=..\7zip\lzmaNSIS.h
SOURCE=..\7zip\LZMADecode.h
# End Source File
# End Group
# Begin Source File

View file

@ -13,7 +13,7 @@
#endif
#ifdef NSIS_COMPRESS_USE_LZMA
#include "../7zip/lzmaNSIS.h"
#include "../7zip/LZMADecode.h"
#define z_stream CLZMAState
#define inflateInit(x) lzmaInit(x)
#define inflateReset(x) lzmaInit(x)
@ -352,12 +352,6 @@ int NSISCALL _dodecomp(int offset, HANDLE hFileOut, char *outbuf, int outbuflen)
{
int u;
#ifdef NSIS_COMPRESS_USE_LZMA
// lzma decompressor doesn't like to stay dry
if (!g_inflate_stream.avail_in && input_len)
break;
#endif
g_inflate_stream.next_out = outbuffer;
g_inflate_stream.avail_out = (unsigned int)outbuffer_len;

View file

@ -638,7 +638,7 @@ class MMapFile : public IMMap
size += offset - alignedoffset;
m_pView = MapViewOfFile(m_hFileMap, m_bReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, alignedoffset, size);
if (!m_pView)
{
extern FILE *g_output;