diff options
author | kh1 <karsten.heimrich@digia.com> | 2013-05-02 15:28:22 +0200 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@digia.com> | 2013-05-08 13:01:55 +0200 |
commit | d89674f944c419c9473da688993ee69671f2c295 (patch) | |
tree | 8422461a5b45d97b6d32dfa8e060e69d600210c9 /src/libs/7zip/win/CPP/7zip/Compress | |
parent | 5ed380ebcf8ef1157f651cbdef8393ccac897ee2 (diff) |
Reset to only use the basic LZMA SDK (Windows).
Change-Id: I8088cc4775f6c5397991f00512354836d614ea4e
Reviewed-by: Kai Koehne <kai.koehne@digia.com>
Reviewed-by: Tim Jenssen <tim.jenssen@digia.com>
Diffstat (limited to 'src/libs/7zip/win/CPP/7zip/Compress')
67 files changed, 0 insertions, 12488 deletions
diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ArjDecoder1.cpp b/src/libs/7zip/win/CPP/7zip/Compress/ArjDecoder1.cpp deleted file mode 100644 index 2c354e1eb..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ArjDecoder1.cpp +++ /dev/null @@ -1,309 +0,0 @@ -// ArjDecoder1.cpp - -#include "StdAfx.h" - -#include "ArjDecoder1.h" - -namespace NCompress{ -namespace NArj { -namespace NDecoder1 { - -static const UInt32 kHistorySize = 26624; -static const UInt32 kMatchMinLen = 3; -static const UInt32 kMatchMaxLen = 256; - -// static const UInt32 kNC = 255 + kMatchMaxLen + 2 - kMatchMinLen; - -void CCoder::MakeTable(int nchar, Byte *bitlen, int tablebits, - UInt32 *table, int tablesize) -{ - UInt32 count[17], weight[17], start[18], *p; - UInt32 i, k, len, ch, jutbits, avail, nextcode, mask; - - for (i = 1; i <= 16; i++) - count[i] = 0; - for (i = 0; (int)i < nchar; i++) - count[bitlen[i]]++; - - start[1] = 0; - for (i = 1; i <= 16; i++) - start[i + 1] = start[i] + (count[i] << (16 - i)); - if (start[17] != (UInt32) (1 << 16)) - throw "Data error"; - - jutbits = 16 - tablebits; - for (i = 1; (int)i <= tablebits; i++) - { - start[i] >>= jutbits; - weight[i] = 1 << (tablebits - i); - } - while (i <= 16) - { - weight[i] = 1 << (16 - i); - i++; - } - - i = start[tablebits + 1] >> jutbits; - if (i != (UInt32) (1 << 16)) - { - k = 1 << tablebits; - while (i != k) - table[i++] = 0; - } - - avail = nchar; - mask = 1 << (15 - tablebits); - for (ch = 0; (int)ch < nchar; ch++) - { - if ((len = bitlen[ch]) == 0) - continue; - k = start[len]; - nextcode = k + weight[len]; - if ((int)len <= tablebits) - { - if (nextcode > (UInt32)tablesize) - throw "Data error"; - for (i = start[len]; i < nextcode; i++) - table[i] = ch; - } - else - { - p = &table[k >> jutbits]; - i = len - tablebits; - while (i != 0) - { - if (*p == 0) - { - right[avail] = left[avail] = 0; - *p = avail++; - } - if (k & mask) - p = &right[*p]; - else - p = &left[*p]; - k <<= 1; - i--; - } - *p = ch; - } - start[len] = nextcode; - } -} - -void CCoder::read_pt_len(int nn, int nbit, int i_special) -{ - UInt32 n = m_InBitStream.ReadBits(nbit); - if (n == 0) - { - UInt32 c = m_InBitStream.ReadBits(nbit); - int i; - for (i = 0; i < nn; i++) - pt_len[i] = 0; - for (i = 0; i < 256; i++) - pt_table[i] = c; - } - else - { - UInt32 i = 0; - while (i < n) - { - UInt32 bitBuf = m_InBitStream.GetValue(16); - int c = bitBuf >> 13; - if (c == 7) - { - UInt32 mask = 1 << (12); - while (mask & bitBuf) - { - mask >>= 1; - c++; - } - } - m_InBitStream.MovePos((c < 7) ? 3 : (int)(c - 3)); - pt_len[i++] = (Byte)c; - if (i == (UInt32)i_special) - { - c = m_InBitStream.ReadBits(2); - while (--c >= 0) - pt_len[i++] = 0; - } - } - while (i < (UInt32)nn) - pt_len[i++] = 0; - MakeTable(nn, pt_len, 8, pt_table, PTABLESIZE); - } -} - -void CCoder::read_c_len() -{ - int i, c, n; - UInt32 mask; - - n = m_InBitStream.ReadBits(CBIT); - if (n == 0) - { - c = m_InBitStream.ReadBits(CBIT); - for (i = 0; i < NC; i++) - c_len[i] = 0; - for (i = 0; i < CTABLESIZE; i++) - c_table[i] = c; - } - else - { - i = 0; - while (i < n) - { - UInt32 bitBuf = m_InBitStream.GetValue(16); - c = pt_table[bitBuf >> (8)]; - if (c >= NT) - { - mask = 1 << (7); - do - { - if (bitBuf & mask) - c = right[c]; - else - c = left[c]; - mask >>= 1; - } while (c >= NT); - } - m_InBitStream.MovePos((int)(pt_len[c])); - if (c <= 2) - { - if (c == 0) - c = 1; - else if (c == 1) - c = m_InBitStream.ReadBits(4) + 3; - else - c = m_InBitStream.ReadBits(CBIT) + 20; - while (--c >= 0) - c_len[i++] = 0; - } - else - c_len[i++] = (Byte)(c - 2); - } - while (i < NC) - c_len[i++] = 0; - MakeTable(NC, c_len, 12, c_table, CTABLESIZE); - } -} - -UInt32 CCoder::decode_c() -{ - UInt32 j, mask; - UInt32 bitbuf = m_InBitStream.GetValue(16); - j = c_table[bitbuf >> 4]; - if (j >= NC) - { - mask = 1 << (3); - do - { - if (bitbuf & mask) - j = right[j]; - else - j = left[j]; - mask >>= 1; - } while (j >= NC); - } - m_InBitStream.MovePos((int)(c_len[j])); - return j; -} - -UInt32 CCoder::decode_p() -{ - UInt32 j, mask; - UInt32 bitbuf = m_InBitStream.GetValue(16); - j = pt_table[bitbuf >> (8)]; - if (j >= NP) - { - mask = 1 << (7); - do - { - if (bitbuf & mask) - j = right[j]; - else - j = left[j]; - mask >>= 1; - } while (j >= NP); - } - m_InBitStream.MovePos((int)(pt_len[j])); - if (j != 0) - { - j--; - j = (1 << j) + m_InBitStream.ReadBits((int)j); - } - return j; -} - - -HRESULT CCoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - if (outSize == NULL) - return E_INVALIDARG; - - if (!m_OutWindowStream.Create(kHistorySize)) - return E_OUTOFMEMORY; - if (!m_InBitStream.Create(1 << 20)) - return E_OUTOFMEMORY; - - // check it - for (int i = 0; i < CTABLESIZE; i++) - c_table[i] = 0; - - UInt64 pos = 0; - m_OutWindowStream.SetStream(outStream); - m_OutWindowStream.Init(false); - m_InBitStream.SetStream(inStream); - m_InBitStream.Init(); - - CCoderReleaser coderReleaser(this); - - UInt32 blockSize = 0; - - while(pos < *outSize) - { - if (blockSize == 0) - { - if (progress != NULL) - { - UInt64 packSize = m_InBitStream.GetProcessedSize(); - RINOK(progress->SetRatioInfo(&packSize, &pos)); - } - blockSize = m_InBitStream.ReadBits(16); - read_pt_len(NT, TBIT, 3); - read_c_len(); - read_pt_len(NP, PBIT, -1); - } - blockSize--; - - UInt32 number = decode_c(); - if (number < 256) - { - m_OutWindowStream.PutByte((Byte)number); - pos++; - continue; - } - else - { - UInt32 len = number - 256 + kMatchMinLen; - UInt32 distance = decode_p(); - if (distance >= pos) - return S_FALSE; - m_OutWindowStream.CopyBlock(distance, len); - pos += len; - } - } - coderReleaser.NeedFlush = false; - return m_OutWindowStream.Flush(); -} - -STDMETHODIMP CCoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - try { return CodeReal(inStream, outStream, inSize, outSize, progress);} - catch(const CInBufferException &e) { return e.ErrorCode; } - catch(const CLzOutWindowException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } -} - -}}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ArjDecoder1.h b/src/libs/7zip/win/CPP/7zip/Compress/ArjDecoder1.h deleted file mode 100644 index 17939dafb..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ArjDecoder1.h +++ /dev/null @@ -1,98 +0,0 @@ -// ArjDecoder1.h - -#ifndef __COMPRESS_ARJ_DECODER1_H -#define __COMPRESS_ARJ_DECODER1_H - -#include "../../Common/MyCom.h" - -#include "../ICoder.h" - -#include "../Common/InBuffer.h" - -#include "BitmDecoder.h" -#include "LzOutWindow.h" - -namespace NCompress { -namespace NArj { -namespace NDecoder1 { - -#define CODE_BIT 16 - -#define THRESHOLD 3 -#define DDICSIZ 26624 -#define MAXDICBIT 16 -#define MATCHBIT 8 -#define MAXMATCH 256 -#define NC (0xFF + MAXMATCH + 2 - THRESHOLD) -#define NP (MAXDICBIT + 1) -#define CBIT 9 -#define NT (CODE_BIT + 3) -#define PBIT 5 -#define TBIT 5 - -#if NT > NP -#define NPT NT -#else -#define NPT NP -#endif - -#define CTABLESIZE 4096 -#define PTABLESIZE 256 - - -class CCoder : - public ICompressCoder, - public CMyUnknownImp -{ - CLzOutWindow m_OutWindowStream; - NBitm::CDecoder<CInBuffer> m_InBitStream; - - UInt32 left[2 * NC - 1]; - UInt32 right[2 * NC - 1]; - Byte c_len[NC]; - Byte pt_len[NPT]; - - UInt32 c_table[CTABLESIZE]; - UInt32 pt_table[PTABLESIZE]; - - void ReleaseStreams() - { - m_OutWindowStream.ReleaseStream(); - m_InBitStream.ReleaseStream(); - } - - class CCoderReleaser - { - CCoder *m_Coder; - public: - bool NeedFlush; - CCoderReleaser(CCoder *coder): m_Coder(coder), NeedFlush(true) {} - ~CCoderReleaser() - { - if (NeedFlush) - m_Coder->m_OutWindowStream.Flush(); - m_Coder->ReleaseStreams(); - } - }; - friend class CCoderReleaser; - - void MakeTable(int nchar, Byte *bitlen, int tablebits, UInt32 *table, int tablesize); - - void read_c_len(); - void read_pt_len(int nn, int nbit, int i_special); - UInt32 decode_c(); - UInt32 decode_p(); - - HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); -public: - MY_UNKNOWN_IMP - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - -}; - -}}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ArjDecoder2.cpp b/src/libs/7zip/win/CPP/7zip/Compress/ArjDecoder2.cpp deleted file mode 100644 index 365993bc5..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ArjDecoder2.cpp +++ /dev/null @@ -1,90 +0,0 @@ -// ArjDecoder2.cpp - -#include "StdAfx.h" - -#include "ArjDecoder2.h" - -namespace NCompress{ -namespace NArj { -namespace NDecoder2 { - -static const UInt32 kHistorySize = 26624; -static const UInt32 kMatchMinLen = 3; - -HRESULT CCoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo * /* progress */) -{ - if (outSize == NULL) - return E_INVALIDARG; - - if (!m_OutWindowStream.Create(kHistorySize)) - return E_OUTOFMEMORY; - if (!m_InBitStream.Create(1 << 20)) - return E_OUTOFMEMORY; - - UInt64 pos = 0; - m_OutWindowStream.SetStream(outStream); - m_OutWindowStream.Init(false); - m_InBitStream.SetStream(inStream); - m_InBitStream.Init(); - CCoderReleaser coderReleaser(this); - - while(pos < *outSize) - { - const UInt32 kStartWidth = 0; - const UInt32 kStopWidth = 7; - UInt32 power = 1 << kStartWidth; - UInt32 width; - UInt32 len = 0; - for (width = kStartWidth; width < kStopWidth; width++) - { - if (m_InBitStream.ReadBits(1) == 0) - break; - len += power; - power <<= 1; - } - if (width != 0) - len += m_InBitStream.ReadBits(width); - if (len == 0) - { - m_OutWindowStream.PutByte((Byte)m_InBitStream.ReadBits(8)); - pos++; - continue; - } - else - { - len = len - 1 + kMatchMinLen; - const UInt32 kStartWidth = 9; - const UInt32 kStopWidth = 13; - UInt32 power = 1 << kStartWidth; - UInt32 width; - UInt32 distance = 0; - for (width = kStartWidth; width < kStopWidth; width++) - { - if (m_InBitStream.ReadBits(1) == 0) - break; - distance += power; - power <<= 1; - } - if (width != 0) - distance += m_InBitStream.ReadBits(width); - if (distance >= pos) - return S_FALSE; - m_OutWindowStream.CopyBlock(distance, len); - pos += len; - } - } - coderReleaser.NeedFlush = false; - return m_OutWindowStream.Flush(); -} - -STDMETHODIMP CCoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - try { return CodeReal(inStream, outStream, inSize, outSize, progress);} - catch(const CInBufferException &e) { return e.ErrorCode; } - catch(const CLzOutWindowException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } -} - -}}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ArjDecoder2.h b/src/libs/7zip/win/CPP/7zip/Compress/ArjDecoder2.h deleted file mode 100644 index cf00975da..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ArjDecoder2.h +++ /dev/null @@ -1,59 +0,0 @@ -// ArjDecoder2.h - -#ifndef __COMPRESS_ARJ_DECODER2_H -#define __COMPRESS_ARJ_DECODER2_H - -#include "../../Common/MyCom.h" - -#include "../ICoder.h" - -#include "../Common/InBuffer.h" - -#include "BitmDecoder.h" -#include "LzOutWindow.h" - -namespace NCompress { -namespace NArj { -namespace NDecoder2 { - -class CCoder : - public ICompressCoder, - public CMyUnknownImp -{ - CLzOutWindow m_OutWindowStream; - NBitm::CDecoder<CInBuffer> m_InBitStream; - - void ReleaseStreams() - { - m_OutWindowStream.ReleaseStream(); - m_InBitStream.ReleaseStream(); - } - - class CCoderReleaser - { - CCoder *m_Coder; - public: - bool NeedFlush; - CCoderReleaser(CCoder *coder): m_Coder(coder), NeedFlush(true) {} - ~CCoderReleaser() - { - if (NeedFlush) - m_Coder->m_OutWindowStream.Flush(); - m_Coder->ReleaseStreams(); - } - }; - friend class CCoderReleaser; - - HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); -public: - MY_UNKNOWN_IMP - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - -}; - -}}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Const.h b/src/libs/7zip/win/CPP/7zip/Compress/BZip2Const.h deleted file mode 100644 index 62427aa68..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Const.h +++ /dev/null @@ -1,54 +0,0 @@ -// Compress/BZip2Const.h - -#ifndef __COMPRESS_BZIP2_CONST_H -#define __COMPRESS_BZIP2_CONST_H - -namespace NCompress { -namespace NBZip2 { - -const Byte kArSig0 = 'B'; -const Byte kArSig1 = 'Z'; -const Byte kArSig2 = 'h'; -const Byte kArSig3 = '0'; - -const Byte kFinSig0 = 0x17; -const Byte kFinSig1 = 0x72; -const Byte kFinSig2 = 0x45; -const Byte kFinSig3 = 0x38; -const Byte kFinSig4 = 0x50; -const Byte kFinSig5 = 0x90; - -const Byte kBlockSig0 = 0x31; -const Byte kBlockSig1 = 0x41; -const Byte kBlockSig2 = 0x59; -const Byte kBlockSig3 = 0x26; -const Byte kBlockSig4 = 0x53; -const Byte kBlockSig5 = 0x59; - -const int kNumOrigBits = 24; - -const int kNumTablesBits = 3; -const int kNumTablesMin = 2; -const int kNumTablesMax = 6; - -const int kNumLevelsBits = 5; - -const int kMaxHuffmanLen = 20; // Check it - -const int kMaxAlphaSize = 258; - -const int kGroupSize = 50; - -const int kBlockSizeMultMin = 1; -const int kBlockSizeMultMax = 9; -const UInt32 kBlockSizeStep = 100000; -const UInt32 kBlockSizeMax = kBlockSizeMultMax * kBlockSizeStep; - -const int kNumSelectorsBits = 15; -const UInt32 kNumSelectorsMax = (2 + (kBlockSizeMax / kGroupSize)); - -const int kRleModeRepSize = 4; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Crc.cpp b/src/libs/7zip/win/CPP/7zip/Compress/BZip2Crc.cpp deleted file mode 100644 index 4e4741f40..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Crc.cpp +++ /dev/null @@ -1,26 +0,0 @@ -// BZip2Crc.cpp - -#include "StdAfx.h" - -#include "BZip2Crc.h" - -UInt32 CBZip2Crc::Table[256]; - -static const UInt32 kBZip2CrcPoly = 0x04c11db7; /* AUTODIN II, Ethernet, & FDDI */ - -void CBZip2Crc::InitTable() -{ - for (UInt32 i = 0; i < 256; i++) - { - UInt32 r = (i << 24); - for (int j = 8; j > 0; j--) - r = (r & 0x80000000) ? ((r << 1) ^ kBZip2CrcPoly) : (r << 1); - Table[i] = r; - } -} - -class CBZip2CrcTableInit -{ -public: - CBZip2CrcTableInit() { CBZip2Crc::InitTable(); } -} g_BZip2CrcTableInit; diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Crc.h b/src/libs/7zip/win/CPP/7zip/Compress/BZip2Crc.h deleted file mode 100644 index 876945b5f..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Crc.h +++ /dev/null @@ -1,31 +0,0 @@ -// BZip2Crc.h - -#ifndef __BZIP2_CRC_H -#define __BZIP2_CRC_H - -#include "Common/Types.h" - -class CBZip2Crc -{ - UInt32 _value; - static UInt32 Table[256]; -public: - static void InitTable(); - CBZip2Crc(): _value(0xFFFFFFFF) {}; - void Init() { _value = 0xFFFFFFFF; } - void UpdateByte(Byte b) { _value = Table[(_value >> 24) ^ b] ^ (_value << 8); } - void UpdateByte(unsigned int b) { _value = Table[(_value >> 24) ^ b] ^ (_value << 8); } - UInt32 GetDigest() const { return _value ^ 0xFFFFFFFF; } -}; - -class CBZip2CombinedCrc -{ - UInt32 _value; -public: - CBZip2CombinedCrc(): _value(0){}; - void Init() { _value = 0; } - void Update(UInt32 v) { _value = ((_value << 1) | (_value >> 31)) ^ v; } - UInt32 GetDigest() const { return _value ; } -}; - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Decoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/BZip2Decoder.cpp deleted file mode 100644 index cb1f981a7..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Decoder.cpp +++ /dev/null @@ -1,943 +0,0 @@ -// BZip2Decoder.cpp - -#include "StdAfx.h" - -#include "../../../C/Alloc.h" - -#include "BZip2Decoder.h" -#include "Mtf8.h" - -namespace NCompress { -namespace NBZip2 { - -#undef NO_INLINE -#define NO_INLINE - -static const UInt32 kNumThreadsMax = 4; - -static const UInt32 kBufferSize = (1 << 17); - -static const UInt16 kRandNums[512] = { - 619, 720, 127, 481, 931, 816, 813, 233, 566, 247, - 985, 724, 205, 454, 863, 491, 741, 242, 949, 214, - 733, 859, 335, 708, 621, 574, 73, 654, 730, 472, - 419, 436, 278, 496, 867, 210, 399, 680, 480, 51, - 878, 465, 811, 169, 869, 675, 611, 697, 867, 561, - 862, 687, 507, 283, 482, 129, 807, 591, 733, 623, - 150, 238, 59, 379, 684, 877, 625, 169, 643, 105, - 170, 607, 520, 932, 727, 476, 693, 425, 174, 647, - 73, 122, 335, 530, 442, 853, 695, 249, 445, 515, - 909, 545, 703, 919, 874, 474, 882, 500, 594, 612, - 641, 801, 220, 162, 819, 984, 589, 513, 495, 799, - 161, 604, 958, 533, 221, 400, 386, 867, 600, 782, - 382, 596, 414, 171, 516, 375, 682, 485, 911, 276, - 98, 553, 163, 354, 666, 933, 424, 341, 533, 870, - 227, 730, 475, 186, 263, 647, 537, 686, 600, 224, - 469, 68, 770, 919, 190, 373, 294, 822, 808, 206, - 184, 943, 795, 384, 383, 461, 404, 758, 839, 887, - 715, 67, 618, 276, 204, 918, 873, 777, 604, 560, - 951, 160, 578, 722, 79, 804, 96, 409, 713, 940, - 652, 934, 970, 447, 318, 353, 859, 672, 112, 785, - 645, 863, 803, 350, 139, 93, 354, 99, 820, 908, - 609, 772, 154, 274, 580, 184, 79, 626, 630, 742, - 653, 282, 762, 623, 680, 81, 927, 626, 789, 125, - 411, 521, 938, 300, 821, 78, 343, 175, 128, 250, - 170, 774, 972, 275, 999, 639, 495, 78, 352, 126, - 857, 956, 358, 619, 580, 124, 737, 594, 701, 612, - 669, 112, 134, 694, 363, 992, 809, 743, 168, 974, - 944, 375, 748, 52, 600, 747, 642, 182, 862, 81, - 344, 805, 988, 739, 511, 655, 814, 334, 249, 515, - 897, 955, 664, 981, 649, 113, 974, 459, 893, 228, - 433, 837, 553, 268, 926, 240, 102, 654, 459, 51, - 686, 754, 806, 760, 493, 403, 415, 394, 687, 700, - 946, 670, 656, 610, 738, 392, 760, 799, 887, 653, - 978, 321, 576, 617, 626, 502, 894, 679, 243, 440, - 680, 879, 194, 572, 640, 724, 926, 56, 204, 700, - 707, 151, 457, 449, 797, 195, 791, 558, 945, 679, - 297, 59, 87, 824, 713, 663, 412, 693, 342, 606, - 134, 108, 571, 364, 631, 212, 174, 643, 304, 329, - 343, 97, 430, 751, 497, 314, 983, 374, 822, 928, - 140, 206, 73, 263, 980, 736, 876, 478, 430, 305, - 170, 514, 364, 692, 829, 82, 855, 953, 676, 246, - 369, 970, 294, 750, 807, 827, 150, 790, 288, 923, - 804, 378, 215, 828, 592, 281, 565, 555, 710, 82, - 896, 831, 547, 261, 524, 462, 293, 465, 502, 56, - 661, 821, 976, 991, 658, 869, 905, 758, 745, 193, - 768, 550, 608, 933, 378, 286, 215, 979, 792, 961, - 61, 688, 793, 644, 986, 403, 106, 366, 905, 644, - 372, 567, 466, 434, 645, 210, 389, 550, 919, 135, - 780, 773, 635, 389, 707, 100, 626, 958, 165, 504, - 920, 176, 193, 713, 857, 265, 203, 50, 668, 108, - 645, 990, 626, 197, 510, 357, 358, 850, 858, 364, - 936, 638 -}; - -bool CState::Alloc() -{ - if (!Counters) - Counters = (UInt32 *)::BigAlloc((256 + kBlockSizeMax) * sizeof(UInt32)); - return (Counters != 0); -} - -void CState::Free() -{ - ::BigFree(Counters); - Counters = 0; -} - -UInt32 CDecoder::ReadBits(unsigned numBits) { return m_InStream.ReadBits(numBits); } -Byte CDecoder::ReadByte() {return (Byte)ReadBits(8); } -bool CDecoder::ReadBit() { return ReadBits(1) != 0; } - -UInt32 CDecoder::ReadCrc() -{ - UInt32 crc = 0; - for (int i = 0; i < 4; i++) - { - crc <<= 8; - crc |= ReadByte(); - } - return crc; -} - -static UInt32 NO_INLINE ReadBits(NBitm::CDecoder<CInBuffer> *m_InStream, unsigned num) -{ - return m_InStream->ReadBits(num); -} - -static UInt32 NO_INLINE ReadBit(NBitm::CDecoder<CInBuffer> *m_InStream) -{ - return m_InStream->ReadBits(1); -} - -static HRESULT NO_INLINE ReadBlock(NBitm::CDecoder<CInBuffer> *m_InStream, - UInt32 *CharCounters, UInt32 blockSizeMax, Byte *m_Selectors, CHuffmanDecoder *m_HuffmanDecoders, - UInt32 *blockSizeRes, UInt32 *origPtrRes, bool *randRes) -{ - if (randRes) - *randRes = ReadBit(m_InStream) ? true : false; - *origPtrRes = ReadBits(m_InStream, kNumOrigBits); - - // in original code it compares OrigPtr to (UInt32)(10 + blockSizeMax)) : why ? - if (*origPtrRes >= blockSizeMax) - return S_FALSE; - - CMtf8Decoder mtf; - mtf.StartInit(); - - int numInUse = 0; - { - Byte inUse16[16]; - int i; - for (i = 0; i < 16; i++) - inUse16[i] = (Byte)ReadBit(m_InStream); - for (i = 0; i < 256; i++) - if (inUse16[i >> 4]) - { - if (ReadBit(m_InStream)) - mtf.Add(numInUse++, (Byte)i); - } - if (numInUse == 0) - return S_FALSE; - // mtf.Init(numInUse); - } - int alphaSize = numInUse + 2; - - int numTables = ReadBits(m_InStream, kNumTablesBits); - if (numTables < kNumTablesMin || numTables > kNumTablesMax) - return S_FALSE; - - UInt32 numSelectors = ReadBits(m_InStream, kNumSelectorsBits); - if (numSelectors < 1 || numSelectors > kNumSelectorsMax) - return S_FALSE; - - { - Byte mtfPos[kNumTablesMax]; - int t = 0; - do - mtfPos[t] = (Byte)t; - while(++t < numTables); - UInt32 i = 0; - do - { - int j = 0; - while (ReadBit(m_InStream)) - if (++j >= numTables) - return S_FALSE; - Byte tmp = mtfPos[j]; - for (;j > 0; j--) - mtfPos[j] = mtfPos[j - 1]; - m_Selectors[i] = mtfPos[0] = tmp; - } - while(++i < numSelectors); - } - - int t = 0; - do - { - Byte lens[kMaxAlphaSize]; - int len = (int)ReadBits(m_InStream, kNumLevelsBits); - int i; - for (i = 0; i < alphaSize; i++) - { - for (;;) - { - if (len < 1 || len > kMaxHuffmanLen) - return S_FALSE; - if (!ReadBit(m_InStream)) - break; - len += 1 - (int)(ReadBit(m_InStream) << 1); - } - lens[i] = (Byte)len; - } - for (; i < kMaxAlphaSize; i++) - lens[i] = 0; - if(!m_HuffmanDecoders[t].SetCodeLengths(lens)) - return S_FALSE; - } - while(++t < numTables); - - { - for (int i = 0; i < 256; i++) - CharCounters[i] = 0; - } - - UInt32 blockSize = 0; - { - UInt32 groupIndex = 0; - UInt32 groupSize = 0; - CHuffmanDecoder *huffmanDecoder = 0; - int runPower = 0; - UInt32 runCounter = 0; - - for (;;) - { - if (groupSize == 0) - { - if (groupIndex >= numSelectors) - return S_FALSE; - groupSize = kGroupSize; - huffmanDecoder = &m_HuffmanDecoders[m_Selectors[groupIndex++]]; - } - groupSize--; - - UInt32 nextSym = huffmanDecoder->DecodeSymbol(m_InStream); - - if (nextSym < 2) - { - runCounter += ((UInt32)(nextSym + 1) << runPower++); - if (blockSizeMax - blockSize < runCounter) - return S_FALSE; - continue; - } - if (runCounter != 0) - { - UInt32 b = (UInt32)mtf.GetHead(); - CharCounters[b] += runCounter; - do - CharCounters[256 + blockSize++] = b; - while(--runCounter != 0); - runPower = 0; - } - if (nextSym <= (UInt32)numInUse) - { - UInt32 b = (UInt32)mtf.GetAndMove((int)nextSym - 1); - if (blockSize >= blockSizeMax) - return S_FALSE; - CharCounters[b]++; - CharCounters[256 + blockSize++] = b; - } - else if (nextSym == (UInt32)numInUse + 1) - break; - else - return S_FALSE; - } - } - *blockSizeRes = blockSize; - return (*origPtrRes < blockSize) ? S_OK : S_FALSE; -} - -static void NO_INLINE DecodeBlock1(UInt32 *charCounters, UInt32 blockSize) -{ - { - UInt32 sum = 0; - for (UInt32 i = 0; i < 256; i++) - { - sum += charCounters[i]; - charCounters[i] = sum - charCounters[i]; - } - } - - UInt32 *tt = charCounters + 256; - // Compute the T^(-1) vector - UInt32 i = 0; - do - tt[charCounters[tt[i] & 0xFF]++] |= (i << 8); - while(++i < blockSize); -} - -static UInt32 NO_INLINE DecodeBlock2(const UInt32 *tt, UInt32 blockSize, UInt32 OrigPtr, COutBuffer &m_OutStream) -{ - CBZip2Crc crc; - - // it's for speed optimization: prefetch & prevByte_init; - UInt32 tPos = tt[tt[OrigPtr] >> 8]; - unsigned prevByte = (unsigned)(tPos & 0xFF); - - unsigned numReps = 0; - - do - { - unsigned b = (unsigned)(tPos & 0xFF); - tPos = tt[tPos >> 8]; - - if (numReps == kRleModeRepSize) - { - for (; b > 0; b--) - { - crc.UpdateByte(prevByte); - m_OutStream.WriteByte((Byte)prevByte); - } - numReps = 0; - continue; - } - if (b != prevByte) - numReps = 0; - numReps++; - prevByte = b; - crc.UpdateByte(b); - m_OutStream.WriteByte((Byte)b); - - /* - prevByte = b; - crc.UpdateByte(b); - m_OutStream.WriteByte((Byte)b); - for (; --blockSize != 0;) - { - b = (unsigned)(tPos & 0xFF); - tPos = tt[tPos >> 8]; - crc.UpdateByte(b); - m_OutStream.WriteByte((Byte)b); - if (b != prevByte) - { - prevByte = b; - continue; - } - if (--blockSize == 0) - break; - - b = (unsigned)(tPos & 0xFF); - tPos = tt[tPos >> 8]; - crc.UpdateByte(b); - m_OutStream.WriteByte((Byte)b); - if (b != prevByte) - { - prevByte = b; - continue; - } - if (--blockSize == 0) - break; - - b = (unsigned)(tPos & 0xFF); - tPos = tt[tPos >> 8]; - crc.UpdateByte(b); - m_OutStream.WriteByte((Byte)b); - if (b != prevByte) - { - prevByte = b; - continue; - } - --blockSize; - break; - } - if (blockSize == 0) - break; - - b = (unsigned)(tPos & 0xFF); - tPos = tt[tPos >> 8]; - - for (; b > 0; b--) - { - crc.UpdateByte(prevByte); - m_OutStream.WriteByte((Byte)prevByte); - } - */ - } - while(--blockSize != 0); - return crc.GetDigest(); -} - -static UInt32 NO_INLINE DecodeBlock2Rand(const UInt32 *tt, UInt32 blockSize, UInt32 OrigPtr, COutBuffer &m_OutStream) -{ - CBZip2Crc crc; - - UInt32 randIndex = 1; - UInt32 randToGo = kRandNums[0] - 2; - - unsigned numReps = 0; - - // it's for speed optimization: prefetch & prevByte_init; - UInt32 tPos = tt[tt[OrigPtr] >> 8]; - unsigned prevByte = (unsigned)(tPos & 0xFF); - - do - { - unsigned b = (unsigned)(tPos & 0xFF); - tPos = tt[tPos >> 8]; - - { - if (randToGo == 0) - { - b ^= 1; - randToGo = kRandNums[randIndex++]; - randIndex &= 0x1FF; - } - randToGo--; - } - - if (numReps == kRleModeRepSize) - { - for (; b > 0; b--) - { - crc.UpdateByte(prevByte); - m_OutStream.WriteByte((Byte)prevByte); - } - numReps = 0; - continue; - } - if (b != prevByte) - numReps = 0; - numReps++; - prevByte = b; - crc.UpdateByte(b); - m_OutStream.WriteByte((Byte)b); - } - while(--blockSize != 0); - return crc.GetDigest(); -} - - -CDecoder::CDecoder() -{ - #ifndef _7ZIP_ST - m_States = 0; - m_NumThreadsPrev = 0; - NumThreads = 1; - #endif - _needInStreamInit = true; -} - -#ifndef _7ZIP_ST - -CDecoder::~CDecoder() -{ - Free(); -} - -#define RINOK_THREAD(x) { WRes __result_ = (x); if(__result_ != 0) return __result_; } - -HRESULT CDecoder::Create() -{ - RINOK_THREAD(CanProcessEvent.CreateIfNotCreated()); - RINOK_THREAD(CanStartWaitingEvent.CreateIfNotCreated()); - if (m_States != 0 && m_NumThreadsPrev == NumThreads) - return S_OK; - Free(); - MtMode = (NumThreads > 1); - m_NumThreadsPrev = NumThreads; - try - { - m_States = new CState[NumThreads]; - if (!m_States) - return E_OUTOFMEMORY; - } - catch(...) { return E_OUTOFMEMORY; } - for (UInt32 t = 0; t < NumThreads; t++) - { - CState &ti = m_States[t]; - ti.Decoder = this; - if (MtMode) - { - HRESULT res = ti.Create(); - if (res != S_OK) - { - NumThreads = t; - Free(); - return res; - } - } - } - return S_OK; -} - -void CDecoder::Free() -{ - if (!m_States) - return; - CloseThreads = true; - CanProcessEvent.Set(); - for (UInt32 t = 0; t < NumThreads; t++) - { - CState &s = m_States[t]; - if (MtMode) - s.Thread.Wait(); - s.Free(); - } - delete []m_States; - m_States = 0; -} - -#endif - -HRESULT CDecoder::ReadSignatures(bool &wasFinished, UInt32 &crc) -{ - wasFinished = false; - Byte s[6]; - for (int i = 0; i < 6; i++) - s[i] = ReadByte(); - crc = ReadCrc(); - if (s[0] == kFinSig0) - { - if (s[1] != kFinSig1 || - s[2] != kFinSig2 || - s[3] != kFinSig3 || - s[4] != kFinSig4 || - s[5] != kFinSig5) - return S_FALSE; - - wasFinished = true; - return (crc == CombinedCrc.GetDigest()) ? S_OK : S_FALSE; - } - if (s[0] != kBlockSig0 || - s[1] != kBlockSig1 || - s[2] != kBlockSig2 || - s[3] != kBlockSig3 || - s[4] != kBlockSig4 || - s[5] != kBlockSig5) - return S_FALSE; - CombinedCrc.Update(crc); - return S_OK; -} - -HRESULT CDecoder::DecodeFile(bool &isBZ, ICompressProgressInfo *progress) -{ - Progress = progress; - #ifndef _7ZIP_ST - RINOK(Create()); - for (UInt32 t = 0; t < NumThreads; t++) - { - CState &s = m_States[t]; - if (!s.Alloc()) - return E_OUTOFMEMORY; - if (MtMode) - { - RINOK(s.StreamWasFinishedEvent.Reset()); - RINOK(s.WaitingWasStartedEvent.Reset()); - RINOK(s.CanWriteEvent.Reset()); - } - } - #else - if (!m_States[0].Alloc()) - return E_OUTOFMEMORY; - #endif - - isBZ = false; - Byte s[6]; - int i; - for (i = 0; i < 4; i++) - s[i] = ReadByte(); - if (s[0] != kArSig0 || - s[1] != kArSig1 || - s[2] != kArSig2 || - s[3] <= kArSig3 || - s[3] > kArSig3 + kBlockSizeMultMax) - return S_OK; - isBZ = true; - UInt32 dicSize = (UInt32)(s[3] - kArSig3) * kBlockSizeStep; - - CombinedCrc.Init(); - #ifndef _7ZIP_ST - if (MtMode) - { - NextBlockIndex = 0; - StreamWasFinished1 = StreamWasFinished2 = false; - CloseThreads = false; - CanStartWaitingEvent.Reset(); - m_States[0].CanWriteEvent.Set(); - BlockSizeMax = dicSize; - Result1 = Result2 = S_OK; - CanProcessEvent.Set(); - UInt32 t; - for (t = 0; t < NumThreads; t++) - m_States[t].StreamWasFinishedEvent.Lock(); - CanProcessEvent.Reset(); - CanStartWaitingEvent.Set(); - for (t = 0; t < NumThreads; t++) - m_States[t].WaitingWasStartedEvent.Lock(); - CanStartWaitingEvent.Reset(); - RINOK(Result2); - RINOK(Result1); - } - else - #endif - { - CState &state = m_States[0]; - for (;;) - { - RINOK(SetRatioProgress(m_InStream.GetProcessedSize())); - bool wasFinished; - UInt32 crc; - RINOK(ReadSignatures(wasFinished, crc)); - if (wasFinished) - return S_OK; - - UInt32 blockSize, origPtr; - bool randMode; - RINOK(ReadBlock(&m_InStream, state.Counters, dicSize, - m_Selectors, m_HuffmanDecoders, - &blockSize, &origPtr, &randMode)); - DecodeBlock1(state.Counters, blockSize); - if ((randMode ? - DecodeBlock2Rand(state.Counters + 256, blockSize, origPtr, m_OutStream) : - DecodeBlock2(state.Counters + 256, blockSize, origPtr, m_OutStream)) != crc) - return S_FALSE; - } - } - return SetRatioProgress(m_InStream.GetProcessedSize()); -} - -HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - bool &isBZ, ICompressProgressInfo *progress) -{ - isBZ = false; - try - { - - if (!m_InStream.Create(kBufferSize)) - return E_OUTOFMEMORY; - if (!m_OutStream.Create(kBufferSize)) - return E_OUTOFMEMORY; - - if (inStream) - m_InStream.SetStream(inStream); - - CDecoderFlusher flusher(this, inStream != NULL); - - if (_needInStreamInit) - { - m_InStream.Init(); - _needInStreamInit = false; - } - _inStart = m_InStream.GetProcessedSize(); - - m_InStream.AlignToByte(); - - m_OutStream.SetStream(outStream); - m_OutStream.Init(); - - RINOK(DecodeFile(isBZ, progress)); - flusher.NeedFlush = false; - return Flush(); - - } - catch(const CInBufferException &e) { return e.ErrorCode; } - catch(const COutBufferException &e) { return e.ErrorCode; } - catch(...) { return E_FAIL; } -} - -STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */, const UInt64 * /* outSize */, ICompressProgressInfo *progress) -{ - _needInStreamInit = true; - bool isBZ; - RINOK(CodeReal(inStream, outStream, isBZ, progress)); - return isBZ ? S_OK : S_FALSE; -} - -HRESULT CDecoder::CodeResume(ISequentialOutStream *outStream, bool &isBZ, ICompressProgressInfo *progress) -{ - return CodeReal(NULL, outStream, isBZ, progress); -} - -STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream) { m_InStream.SetStream(inStream); return S_OK; } -STDMETHODIMP CDecoder::ReleaseInStream() { m_InStream.ReleaseStream(); return S_OK; } - -#ifndef _7ZIP_ST - -static THREAD_FUNC_DECL MFThread(void *p) { ((CState *)p)->ThreadFunc(); return 0; } - -HRESULT CState::Create() -{ - RINOK_THREAD(StreamWasFinishedEvent.CreateIfNotCreated()); - RINOK_THREAD(WaitingWasStartedEvent.CreateIfNotCreated()); - RINOK_THREAD(CanWriteEvent.CreateIfNotCreated()); - RINOK_THREAD(Thread.Create(MFThread, this)); - return S_OK; -} - -void CState::FinishStream() -{ - Decoder->StreamWasFinished1 = true; - StreamWasFinishedEvent.Set(); - Decoder->CS.Leave(); - Decoder->CanStartWaitingEvent.Lock(); - WaitingWasStartedEvent.Set(); -} - -void CState::ThreadFunc() -{ - for (;;) - { - Decoder->CanProcessEvent.Lock(); - Decoder->CS.Enter(); - if (Decoder->CloseThreads) - { - Decoder->CS.Leave(); - return; - } - if (Decoder->StreamWasFinished1) - { - FinishStream(); - continue; - } - HRESULT res = S_OK; - - UInt32 blockIndex = Decoder->NextBlockIndex; - UInt32 nextBlockIndex = blockIndex + 1; - if (nextBlockIndex == Decoder->NumThreads) - nextBlockIndex = 0; - Decoder->NextBlockIndex = nextBlockIndex; - UInt32 crc; - UInt64 packSize = 0; - UInt32 blockSize = 0, origPtr = 0; - bool randMode = false; - - try - { - bool wasFinished; - res = Decoder->ReadSignatures(wasFinished, crc); - if (res != S_OK) - { - Decoder->Result1 = res; - FinishStream(); - continue; - } - if (wasFinished) - { - Decoder->Result1 = res; - FinishStream(); - continue; - } - - res = ReadBlock(&Decoder->m_InStream, Counters, Decoder->BlockSizeMax, - Decoder->m_Selectors, Decoder->m_HuffmanDecoders, - &blockSize, &origPtr, &randMode); - if (res != S_OK) - { - Decoder->Result1 = res; - FinishStream(); - continue; - } - packSize = Decoder->m_InStream.GetProcessedSize(); - } - catch(const CInBufferException &e) { res = e.ErrorCode; if (res != S_OK) res = E_FAIL; } - catch(...) { res = E_FAIL; } - if (res != S_OK) - { - Decoder->Result1 = res; - FinishStream(); - continue; - } - - Decoder->CS.Leave(); - - DecodeBlock1(Counters, blockSize); - - bool needFinish = true; - try - { - Decoder->m_States[blockIndex].CanWriteEvent.Lock(); - needFinish = Decoder->StreamWasFinished2; - if (!needFinish) - { - if ((randMode ? - DecodeBlock2Rand(Counters + 256, blockSize, origPtr, Decoder->m_OutStream) : - DecodeBlock2(Counters + 256, blockSize, origPtr, Decoder->m_OutStream)) == crc) - res = Decoder->SetRatioProgress(packSize); - else - res = S_FALSE; - } - } - catch(const COutBufferException &e) { res = e.ErrorCode; if (res != S_OK) res = E_FAIL; } - catch(...) { res = E_FAIL; } - if (res != S_OK) - { - Decoder->Result2 = res; - Decoder->StreamWasFinished2 = true; - } - Decoder->m_States[nextBlockIndex].CanWriteEvent.Set(); - if (res != S_OK || needFinish) - { - StreamWasFinishedEvent.Set(); - Decoder->CanStartWaitingEvent.Lock(); - WaitingWasStartedEvent.Set(); - } - } -} - -STDMETHODIMP CDecoder::SetNumberOfThreads(UInt32 numThreads) -{ - NumThreads = numThreads; - if (NumThreads < 1) - NumThreads = 1; - if (NumThreads > kNumThreadsMax) - NumThreads = kNumThreadsMax; - return S_OK; -} - -#endif - -HRESULT CDecoder::SetRatioProgress(UInt64 packSize) -{ - if (!Progress) - return S_OK; - packSize -= _inStart; - UInt64 unpackSize = m_OutStream.GetProcessedSize(); - return Progress->SetRatioInfo(&packSize, &unpackSize); -} - - -// ---------- NSIS ---------- - -enum -{ - NSIS_STATE_INIT, - NSIS_STATE_NEW_BLOCK, - NSIS_STATE_DATA, - NSIS_STATE_FINISHED, - NSIS_STATE_ERROR -}; - -STDMETHODIMP CNsisDecoder::SetInStream(ISequentialInStream *inStream) { m_InStream.SetStream(inStream); return S_OK; } -STDMETHODIMP CNsisDecoder::ReleaseInStream() { m_InStream.ReleaseStream(); return S_OK; } - -STDMETHODIMP CNsisDecoder::SetOutStreamSize(const UInt64 * /* outSize */) -{ - _nsisState = NSIS_STATE_INIT; - return S_OK; -} - -STDMETHODIMP CNsisDecoder::Read(void *data, UInt32 size, UInt32 *processedSize) -{ - try { - - *processedSize = 0; - if (_nsisState == NSIS_STATE_FINISHED) - return S_OK; - if (_nsisState == NSIS_STATE_ERROR) - return S_FALSE; - if (size == 0) - return S_OK; - - CState &state = m_State; - - if (_nsisState == NSIS_STATE_INIT) - { - if (!m_InStream.Create(kBufferSize)) - return E_OUTOFMEMORY; - if (!state.Alloc()) - return E_OUTOFMEMORY; - m_InStream.Init(); - _nsisState = NSIS_STATE_NEW_BLOCK; - } - - if (_nsisState == NSIS_STATE_NEW_BLOCK) - { - Byte b = (Byte)m_InStream.ReadBits(8); - if (b == kFinSig0) - { - _nsisState = NSIS_STATE_FINISHED; - return S_OK; - } - if (b != kBlockSig0) - { - _nsisState = NSIS_STATE_ERROR; - return S_FALSE; - } - UInt32 origPtr; - RINOK(ReadBlock(&m_InStream, state.Counters, 9 * kBlockSizeStep, - m_Selectors, m_HuffmanDecoders, &_blockSize, &origPtr, NULL)); - DecodeBlock1(state.Counters, _blockSize); - const UInt32 *tt = state.Counters + 256; - _tPos = tt[tt[origPtr] >> 8]; - _prevByte = (unsigned)(_tPos & 0xFF); - _numReps = 0; - _repRem = 0; - _nsisState = NSIS_STATE_DATA; - } - - UInt32 tPos = _tPos; - unsigned prevByte = _prevByte; - unsigned numReps = _numReps; - UInt32 blockSize = _blockSize; - const UInt32 *tt = state.Counters + 256; - - while (_repRem) - { - _repRem--; - *(Byte *)data = (Byte)prevByte; - data = (Byte *)data + 1; - (*processedSize)++; - if (--size == 0) - return S_OK; - } - - if (blockSize == 0) - { - _nsisState = NSIS_STATE_NEW_BLOCK; - return S_OK; - } - - do - { - unsigned b = (unsigned)(tPos & 0xFF); - tPos = tt[tPos >> 8]; - blockSize--; - - if (numReps == kRleModeRepSize) - { - numReps = 0; - while (b) - { - b--; - *(Byte *)data = (Byte)prevByte; - data = (Byte *)data + 1; - (*processedSize)++; - if (--size == 0) - break; - } - _repRem = b; - continue; - } - if (b != prevByte) - numReps = 0; - numReps++; - prevByte = b; - *(Byte *)data = (Byte)b; - data = (Byte *)data + 1; - (*processedSize)++; - size--; - } - while (size && blockSize); - _tPos = tPos; - _prevByte = prevByte; - _numReps = numReps; - _blockSize = blockSize; - return S_OK; - - } - catch(const CInBufferException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Decoder.h b/src/libs/7zip/win/CPP/7zip/Compress/BZip2Decoder.h deleted file mode 100644 index e6dec1eaa..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Decoder.h +++ /dev/null @@ -1,205 +0,0 @@ -// Compress/BZip2Decoder.h - -#ifndef __COMPRESS_BZIP2_DECODER_H -#define __COMPRESS_BZIP2_DECODER_H - -#include "../../Common/MyCom.h" - -#ifndef _7ZIP_ST -#include "../../Windows/Synchronization.h" -#include "../../Windows/Thread.h" -#endif - -#include "../ICoder.h" - -#include "../Common/InBuffer.h" -#include "../Common/OutBuffer.h" - -#include "BitmDecoder.h" -#include "BZip2Const.h" -#include "BZip2Crc.h" -#include "HuffmanDecoder.h" - -namespace NCompress { -namespace NBZip2 { - -typedef NCompress::NHuffman::CDecoder<kMaxHuffmanLen, kMaxAlphaSize> CHuffmanDecoder; - -class CDecoder; - -struct CState -{ - UInt32 *Counters; - - #ifndef _7ZIP_ST - - CDecoder *Decoder; - NWindows::CThread Thread; - bool m_OptimizeNumTables; - - NWindows::NSynchronization::CAutoResetEvent StreamWasFinishedEvent; - NWindows::NSynchronization::CAutoResetEvent WaitingWasStartedEvent; - - // it's not member of this thread. We just need one event per thread - NWindows::NSynchronization::CAutoResetEvent CanWriteEvent; - - Byte MtPad[1 << 8]; // It's pad for Multi-Threading. Must be >= Cache_Line_Size. - - HRESULT Create(); - void FinishStream(); - void ThreadFunc(); - - #endif - - CState(): Counters(0) {} - ~CState() { Free(); } - bool Alloc(); - void Free(); -}; - -class CDecoder : - public ICompressCoder, - #ifndef _7ZIP_ST - public ICompressSetCoderMt, - #endif - public CMyUnknownImp -{ -public: - COutBuffer m_OutStream; - Byte MtPad[1 << 8]; // It's pad for Multi-Threading. Must be >= Cache_Line_Size. - NBitm::CDecoder<CInBuffer> m_InStream; - Byte m_Selectors[kNumSelectorsMax]; - CHuffmanDecoder m_HuffmanDecoders[kNumTablesMax]; - UInt64 _inStart; - -private: - - bool _needInStreamInit; - - UInt32 ReadBits(unsigned numBits); - Byte ReadByte(); - bool ReadBit(); - UInt32 ReadCrc(); - HRESULT DecodeFile(bool &isBZ, ICompressProgressInfo *progress); - HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - bool &isBZ, ICompressProgressInfo *progress); - class CDecoderFlusher - { - CDecoder *_decoder; - public: - bool NeedFlush; - bool ReleaseInStream; - CDecoderFlusher(CDecoder *decoder, bool releaseInStream): - _decoder(decoder), - ReleaseInStream(releaseInStream), - NeedFlush(true) {} - ~CDecoderFlusher() - { - if (NeedFlush) - _decoder->Flush(); - _decoder->ReleaseStreams(ReleaseInStream); - } - }; - -public: - CBZip2CombinedCrc CombinedCrc; - ICompressProgressInfo *Progress; - - #ifndef _7ZIP_ST - CState *m_States; - UInt32 m_NumThreadsPrev; - - NWindows::NSynchronization::CManualResetEvent CanProcessEvent; - NWindows::NSynchronization::CCriticalSection CS; - UInt32 NumThreads; - bool MtMode; - UInt32 NextBlockIndex; - bool CloseThreads; - bool StreamWasFinished1; - bool StreamWasFinished2; - NWindows::NSynchronization::CManualResetEvent CanStartWaitingEvent; - - HRESULT Result1; - HRESULT Result2; - - UInt32 BlockSizeMax; - ~CDecoder(); - HRESULT Create(); - void Free(); - - #else - CState m_States[1]; - #endif - - CDecoder(); - - HRESULT SetRatioProgress(UInt64 packSize); - HRESULT ReadSignatures(bool &wasFinished, UInt32 &crc); - - HRESULT Flush() { return m_OutStream.Flush(); } - void ReleaseStreams(bool releaseInStream) - { - if (releaseInStream) - m_InStream.ReleaseStream(); - m_OutStream.ReleaseStream(); - } - - MY_QUERYINTERFACE_BEGIN2(ICompressCoder) - #ifndef _7ZIP_ST - MY_QUERYINTERFACE_ENTRY(ICompressSetCoderMt) - #endif - - MY_QUERYINTERFACE_END - MY_ADDREF_RELEASE - - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(SetInStream)(ISequentialInStream *inStream); - STDMETHOD(ReleaseInStream)(); - - HRESULT CodeResume(ISequentialOutStream *outStream, bool &isBZ, ICompressProgressInfo *progress); - UInt64 GetInputProcessedSize() const { return m_InStream.GetProcessedSize(); } - - #ifndef _7ZIP_ST - STDMETHOD(SetNumberOfThreads)(UInt32 numThreads); - #endif -}; - - -class CNsisDecoder : - public ISequentialInStream, - public ICompressSetInStream, - public ICompressSetOutStreamSize, - public CMyUnknownImp -{ - NBitm::CDecoder<CInBuffer> m_InStream; - Byte m_Selectors[kNumSelectorsMax]; - CHuffmanDecoder m_HuffmanDecoders[kNumTablesMax]; - CState m_State; - - int _nsisState; - UInt32 _tPos; - unsigned _prevByte; - unsigned _repRem; - unsigned _numReps; - UInt32 _blockSize; - -public: - - MY_QUERYINTERFACE_BEGIN2(ISequentialInStream) - MY_QUERYINTERFACE_ENTRY(ICompressSetInStream) - MY_QUERYINTERFACE_ENTRY(ICompressSetOutStreamSize) - MY_QUERYINTERFACE_END - MY_ADDREF_RELEASE - - STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); - STDMETHOD(SetInStream)(ISequentialInStream *inStream); - STDMETHOD(ReleaseInStream)(); - STDMETHOD(SetOutStreamSize)(const UInt64 *outSize); -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Encoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/BZip2Encoder.cpp deleted file mode 100644 index eaa108558..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Encoder.cpp +++ /dev/null @@ -1,895 +0,0 @@ -// BZip2Encoder.cpp - -#include "StdAfx.h" - -#include "../../../C/Alloc.h" -#include "../../../C/BwtSort.h" -#include "../../../C/HuffEnc.h" - -#include "BZip2Crc.h" -#include "BZip2Encoder.h" -#include "Mtf8.h" - -namespace NCompress { -namespace NBZip2 { - -const int kMaxHuffmanLenForEncoding = 16; // it must be < kMaxHuffmanLen = 20 - -static const UInt32 kBufferSize = (1 << 17); -static const int kNumHuffPasses = 4; - -bool CThreadInfo::Alloc() -{ - if (m_BlockSorterIndex == 0) - { - m_BlockSorterIndex = (UInt32 *)::BigAlloc(BLOCK_SORT_BUF_SIZE(kBlockSizeMax) * sizeof(UInt32)); - if (m_BlockSorterIndex == 0) - return false; - } - - if (m_Block == 0) - { - m_Block = (Byte *)::MidAlloc(kBlockSizeMax * 5 + kBlockSizeMax / 10 + (20 << 10)); - if (m_Block == 0) - return false; - m_MtfArray = m_Block + kBlockSizeMax; - m_TempArray = m_MtfArray + kBlockSizeMax * 2 + 2; - } - return true; -} - -void CThreadInfo::Free() -{ - ::BigFree(m_BlockSorterIndex); - m_BlockSorterIndex = 0; - ::MidFree(m_Block); - m_Block = 0; -} - -#ifndef _7ZIP_ST - -static THREAD_FUNC_DECL MFThread(void *threadCoderInfo) -{ - return ((CThreadInfo *)threadCoderInfo)->ThreadFunc(); -} - -#define RINOK_THREAD(x) { WRes __result_ = (x); if(__result_ != 0) return __result_; } - -HRESULT CThreadInfo::Create() -{ - RINOK_THREAD(StreamWasFinishedEvent.Create()); - RINOK_THREAD(WaitingWasStartedEvent.Create()); - RINOK_THREAD(CanWriteEvent.Create()); - RINOK_THREAD(Thread.Create(MFThread, this)); - return S_OK; -} - -void CThreadInfo::FinishStream(bool needLeave) -{ - Encoder->StreamWasFinished = true; - StreamWasFinishedEvent.Set(); - if (needLeave) - Encoder->CS.Leave(); - Encoder->CanStartWaitingEvent.Lock(); - WaitingWasStartedEvent.Set(); -} - -DWORD CThreadInfo::ThreadFunc() -{ - for (;;) - { - Encoder->CanProcessEvent.Lock(); - Encoder->CS.Enter(); - if (Encoder->CloseThreads) - { - Encoder->CS.Leave(); - return 0; - } - if (Encoder->StreamWasFinished) - { - FinishStream(true); - continue; - } - HRESULT res = S_OK; - bool needLeave = true; - try - { - UInt32 blockSize = Encoder->ReadRleBlock(m_Block); - m_PackSize = Encoder->m_InStream.GetProcessedSize(); - m_BlockIndex = Encoder->NextBlockIndex; - if (++Encoder->NextBlockIndex == Encoder->NumThreads) - Encoder->NextBlockIndex = 0; - if (blockSize == 0) - { - FinishStream(true); - continue; - } - Encoder->CS.Leave(); - needLeave = false; - res = EncodeBlock3(blockSize); - } - catch(const CInBufferException &e) { res = e.ErrorCode; } - catch(const COutBufferException &e) { res = e.ErrorCode; } - catch(...) { res = E_FAIL; } - if (res != S_OK) - { - Encoder->Result = res; - FinishStream(needLeave); - continue; - } - } -} - -#endif - -CEncoder::CEncoder(): - NumPasses(1), - m_OptimizeNumTables(false), - m_BlockSizeMult(kBlockSizeMultMax) -{ - #ifndef _7ZIP_ST - ThreadsInfo = 0; - m_NumThreadsPrev = 0; - NumThreads = 1; - #endif -} - -#ifndef _7ZIP_ST -CEncoder::~CEncoder() -{ - Free(); -} - -HRESULT CEncoder::Create() -{ - RINOK_THREAD(CanProcessEvent.CreateIfNotCreated()); - RINOK_THREAD(CanStartWaitingEvent.CreateIfNotCreated()); - if (ThreadsInfo != 0 && m_NumThreadsPrev == NumThreads) - return S_OK; - try - { - Free(); - MtMode = (NumThreads > 1); - m_NumThreadsPrev = NumThreads; - ThreadsInfo = new CThreadInfo[NumThreads]; - if (ThreadsInfo == 0) - return E_OUTOFMEMORY; - } - catch(...) { return E_OUTOFMEMORY; } - for (UInt32 t = 0; t < NumThreads; t++) - { - CThreadInfo &ti = ThreadsInfo[t]; - ti.Encoder = this; - if (MtMode) - { - HRESULT res = ti.Create(); - if (res != S_OK) - { - NumThreads = t; - Free(); - return res; - } - } - } - return S_OK; -} - -void CEncoder::Free() -{ - if (!ThreadsInfo) - return; - CloseThreads = true; - CanProcessEvent.Set(); - for (UInt32 t = 0; t < NumThreads; t++) - { - CThreadInfo &ti = ThreadsInfo[t]; - if (MtMode) - ti.Thread.Wait(); - ti.Free(); - } - delete []ThreadsInfo; - ThreadsInfo = 0; -} -#endif - -UInt32 CEncoder::ReadRleBlock(Byte *buffer) -{ - UInt32 i = 0; - Byte prevByte; - if (m_InStream.ReadByte(prevByte)) - { - UInt32 blockSize = m_BlockSizeMult * kBlockSizeStep - 1; - int numReps = 1; - buffer[i++] = prevByte; - while (i < blockSize) // "- 1" to support RLE - { - Byte b; - if (!m_InStream.ReadByte(b)) - break; - if (b != prevByte) - { - if (numReps >= kRleModeRepSize) - buffer[i++] = (Byte)(numReps - kRleModeRepSize); - buffer[i++] = b; - numReps = 1; - prevByte = b; - continue; - } - numReps++; - if (numReps <= kRleModeRepSize) - buffer[i++] = b; - else if (numReps == kRleModeRepSize + 255) - { - buffer[i++] = (Byte)(numReps - kRleModeRepSize); - numReps = 0; - } - } - // it's to support original BZip2 decoder - if (numReps >= kRleModeRepSize) - buffer[i++] = (Byte)(numReps - kRleModeRepSize); - } - return i; -} - -void CThreadInfo::WriteBits2(UInt32 value, UInt32 numBits) - { m_OutStreamCurrent->WriteBits(value, numBits); } -void CThreadInfo::WriteByte2(Byte b) { WriteBits2(b , 8); } -void CThreadInfo::WriteBit2(bool v) { WriteBits2((v ? 1 : 0), 1); } -void CThreadInfo::WriteCrc2(UInt32 v) -{ - for (int i = 0; i < 4; i++) - WriteByte2(((Byte)(v >> (24 - i * 8)))); -} - -void CEncoder::WriteBits(UInt32 value, UInt32 numBits) - { m_OutStream.WriteBits(value, numBits); } -void CEncoder::WriteByte(Byte b) { WriteBits(b , 8); } -void CEncoder::WriteBit(bool v) { WriteBits((v ? 1 : 0), 1); } -void CEncoder::WriteCrc(UInt32 v) -{ - for (int i = 0; i < 4; i++) - WriteByte(((Byte)(v >> (24 - i * 8)))); -} - - -// blockSize > 0 -void CThreadInfo::EncodeBlock(const Byte *block, UInt32 blockSize) -{ - WriteBit2(false); // Randomised = false - - { - UInt32 origPtr = BlockSort(m_BlockSorterIndex, block, blockSize); - // if (m_BlockSorterIndex[origPtr] != 0) throw 1; - m_BlockSorterIndex[origPtr] = blockSize; - WriteBits2(origPtr, kNumOrigBits); - } - - CMtf8Encoder mtf; - int numInUse = 0; - { - bool inUse[256]; - bool inUse16[16]; - UInt32 i; - for (i = 0; i < 256; i++) - inUse[i] = false; - for (i = 0; i < 16; i++) - inUse16[i] = false; - for (i = 0; i < blockSize; i++) - inUse[block[i]] = true; - for (i = 0; i < 256; i++) - if (inUse[i]) - { - inUse16[i >> 4] = true; - mtf.Buf[numInUse++] = (Byte)i; - } - for (i = 0; i < 16; i++) - WriteBit2(inUse16[i]); - for (i = 0; i < 256; i++) - if (inUse16[i >> 4]) - WriteBit2(inUse[i]); - } - int alphaSize = numInUse + 2; - - Byte *mtfs = m_MtfArray; - UInt32 mtfArraySize = 0; - UInt32 symbolCounts[kMaxAlphaSize]; - { - for (int i = 0; i < kMaxAlphaSize; i++) - symbolCounts[i] = 0; - } - - { - UInt32 rleSize = 0; - UInt32 i = 0; - const UInt32 *bsIndex = m_BlockSorterIndex; - block--; - do - { - int pos = mtf.FindAndMove(block[bsIndex[i]]); - if (pos == 0) - rleSize++; - else - { - while (rleSize != 0) - { - rleSize--; - mtfs[mtfArraySize++] = (Byte)(rleSize & 1); - symbolCounts[rleSize & 1]++; - rleSize >>= 1; - } - if (pos >= 0xFE) - { - mtfs[mtfArraySize++] = 0xFF; - mtfs[mtfArraySize++] = (Byte)(pos - 0xFE); - } - else - mtfs[mtfArraySize++] = (Byte)(pos + 1); - symbolCounts[pos + 1]++; - } - } - while (++i < blockSize); - - while (rleSize != 0) - { - rleSize--; - mtfs[mtfArraySize++] = (Byte)(rleSize & 1); - symbolCounts[rleSize & 1]++; - rleSize >>= 1; - } - - if (alphaSize < 256) - mtfs[mtfArraySize++] = (Byte)(alphaSize - 1); - else - { - mtfs[mtfArraySize++] = 0xFF; - mtfs[mtfArraySize++] = (Byte)(alphaSize - 256); - } - symbolCounts[alphaSize - 1]++; - } - - UInt32 numSymbols = 0; - { - for (int i = 0; i < kMaxAlphaSize; i++) - numSymbols += symbolCounts[i]; - } - - int bestNumTables = kNumTablesMin; - UInt32 bestPrice = 0xFFFFFFFF; - UInt32 startPos = m_OutStreamCurrent->GetPos(); - Byte startCurByte = m_OutStreamCurrent->GetCurByte(); - for (int nt = kNumTablesMin; nt <= kNumTablesMax + 1; nt++) - { - int numTables; - - if(m_OptimizeNumTables) - { - m_OutStreamCurrent->SetPos(startPos); - m_OutStreamCurrent->SetCurState((startPos & 7), startCurByte); - if (nt <= kNumTablesMax) - numTables = nt; - else - numTables = bestNumTables; - } - else - { - if (numSymbols < 200) numTables = 2; - else if (numSymbols < 600) numTables = 3; - else if (numSymbols < 1200) numTables = 4; - else if (numSymbols < 2400) numTables = 5; - else numTables = 6; - } - - WriteBits2(numTables, kNumTablesBits); - - UInt32 numSelectors = (numSymbols + kGroupSize - 1) / kGroupSize; - WriteBits2(numSelectors, kNumSelectorsBits); - - { - UInt32 remFreq = numSymbols; - int gs = 0; - int t = numTables; - do - { - UInt32 tFreq = remFreq / t; - int ge = gs; - UInt32 aFreq = 0; - while (aFreq < tFreq) // && ge < alphaSize) - aFreq += symbolCounts[ge++]; - - if (ge - 1 > gs && t != numTables && t != 1 && (((numTables - t) & 1) == 1)) - aFreq -= symbolCounts[--ge]; - - Byte *lens = Lens[t - 1]; - int i = 0; - do - lens[i] = (i >= gs && i < ge) ? 0 : 1; - while (++i < alphaSize); - gs = ge; - remFreq -= aFreq; - } - while(--t != 0); - } - - - for (int pass = 0; pass < kNumHuffPasses; pass++) - { - { - int t = 0; - do - memset(Freqs[t], 0, sizeof(Freqs[t])); - while(++t < numTables); - } - - { - UInt32 mtfPos = 0; - UInt32 g = 0; - do - { - UInt32 symbols[kGroupSize]; - int i = 0; - do - { - UInt32 symbol = mtfs[mtfPos++]; - if (symbol >= 0xFF) - symbol += mtfs[mtfPos++]; - symbols[i] = symbol; - } - while (++i < kGroupSize && mtfPos < mtfArraySize); - - UInt32 bestPrice = 0xFFFFFFFF; - int t = 0; - do - { - const Byte *lens = Lens[t]; - UInt32 price = 0; - int j = 0; - do - price += lens[symbols[j]]; - while (++j < i); - if (price < bestPrice) - { - m_Selectors[g] = (Byte)t; - bestPrice = price; - } - } - while(++t < numTables); - UInt32 *freqs = Freqs[m_Selectors[g++]]; - int j = 0; - do - freqs[symbols[j]]++; - while (++j < i); - } - while (mtfPos < mtfArraySize); - } - - int t = 0; - do - { - UInt32 *freqs = Freqs[t]; - int i = 0; - do - if (freqs[i] == 0) - freqs[i] = 1; - while(++i < alphaSize); - Huffman_Generate(freqs, Codes[t], Lens[t], kMaxAlphaSize, kMaxHuffmanLenForEncoding); - } - while(++t < numTables); - } - - { - Byte mtfSel[kNumTablesMax]; - { - int t = 0; - do - mtfSel[t] = (Byte)t; - while(++t < numTables); - } - - UInt32 i = 0; - do - { - Byte sel = m_Selectors[i]; - int pos; - for (pos = 0; mtfSel[pos] != sel; pos++) - WriteBit2(true); - WriteBit2(false); - for (; pos > 0; pos--) - mtfSel[pos] = mtfSel[pos - 1]; - mtfSel[0] = sel; - } - while(++i < numSelectors); - } - - { - int t = 0; - do - { - const Byte *lens = Lens[t]; - UInt32 len = lens[0]; - WriteBits2(len, kNumLevelsBits); - int i = 0; - do - { - UInt32 level = lens[i]; - while (len != level) - { - WriteBit2(true); - if (len < level) - { - WriteBit2(false); - len++; - } - else - { - WriteBit2(true); - len--; - } - } - WriteBit2(false); - } - while (++i < alphaSize); - } - while(++t < numTables); - } - - { - UInt32 groupSize = 0; - UInt32 groupIndex = 0; - const Byte *lens = 0; - const UInt32 *codes = 0; - UInt32 mtfPos = 0; - do - { - UInt32 symbol = mtfs[mtfPos++]; - if (symbol >= 0xFF) - symbol += mtfs[mtfPos++]; - if (groupSize == 0) - { - groupSize = kGroupSize; - int t = m_Selectors[groupIndex++]; - lens = Lens[t]; - codes = Codes[t]; - } - groupSize--; - m_OutStreamCurrent->WriteBits(codes[symbol], lens[symbol]); - } - while (mtfPos < mtfArraySize); - } - - if (!m_OptimizeNumTables) - break; - UInt32 price = m_OutStreamCurrent->GetPos() - startPos; - if (price <= bestPrice) - { - if (nt == kNumTablesMax) - break; - bestPrice = price; - bestNumTables = nt; - } - } -} - -// blockSize > 0 -UInt32 CThreadInfo::EncodeBlockWithHeaders(const Byte *block, UInt32 blockSize) -{ - WriteByte2(kBlockSig0); - WriteByte2(kBlockSig1); - WriteByte2(kBlockSig2); - WriteByte2(kBlockSig3); - WriteByte2(kBlockSig4); - WriteByte2(kBlockSig5); - - CBZip2Crc crc; - int numReps = 0; - Byte prevByte = block[0]; - UInt32 i = 0; - do - { - Byte b = block[i]; - if (numReps == kRleModeRepSize) - { - for (; b > 0; b--) - crc.UpdateByte(prevByte); - numReps = 0; - continue; - } - if (prevByte == b) - numReps++; - else - { - numReps = 1; - prevByte = b; - } - crc.UpdateByte(b); - } - while (++i < blockSize); - UInt32 crcRes = crc.GetDigest(); - WriteCrc2(crcRes); - EncodeBlock(block, blockSize); - return crcRes; -} - -void CThreadInfo::EncodeBlock2(const Byte *block, UInt32 blockSize, UInt32 numPasses) -{ - UInt32 numCrcs = m_NumCrcs; - bool needCompare = false; - - UInt32 startBytePos = m_OutStreamCurrent->GetBytePos(); - UInt32 startPos = m_OutStreamCurrent->GetPos(); - Byte startCurByte = m_OutStreamCurrent->GetCurByte(); - Byte endCurByte = 0; - UInt32 endPos = 0; - if (numPasses > 1 && blockSize >= (1 << 10)) - { - UInt32 blockSize0 = blockSize / 2; - for (;(block[blockSize0] == block[blockSize0 - 1] || - block[blockSize0 - 1] == block[blockSize0 - 2]) && - blockSize0 < blockSize; blockSize0++); - if (blockSize0 < blockSize) - { - EncodeBlock2(block, blockSize0, numPasses - 1); - EncodeBlock2(block + blockSize0, blockSize - blockSize0, numPasses - 1); - endPos = m_OutStreamCurrent->GetPos(); - endCurByte = m_OutStreamCurrent->GetCurByte(); - if ((endPos & 7) > 0) - WriteBits2(0, 8 - (endPos & 7)); - m_OutStreamCurrent->SetCurState((startPos & 7), startCurByte); - needCompare = true; - } - } - - UInt32 startBytePos2 = m_OutStreamCurrent->GetBytePos(); - UInt32 startPos2 = m_OutStreamCurrent->GetPos(); - UInt32 crcVal = EncodeBlockWithHeaders(block, blockSize); - UInt32 endPos2 = m_OutStreamCurrent->GetPos(); - - if (needCompare) - { - UInt32 size2 = endPos2 - startPos2; - if (size2 < endPos - startPos) - { - UInt32 numBytes = m_OutStreamCurrent->GetBytePos() - startBytePos2; - Byte *buffer = m_OutStreamCurrent->GetStream(); - for (UInt32 i = 0; i < numBytes; i++) - buffer[startBytePos + i] = buffer[startBytePos2 + i]; - m_OutStreamCurrent->SetPos(startPos + endPos2 - startPos2); - m_NumCrcs = numCrcs; - m_CRCs[m_NumCrcs++] = crcVal; - } - else - { - m_OutStreamCurrent->SetPos(endPos); - m_OutStreamCurrent->SetCurState((endPos & 7), endCurByte); - } - } - else - { - m_NumCrcs = numCrcs; - m_CRCs[m_NumCrcs++] = crcVal; - } -} - -HRESULT CThreadInfo::EncodeBlock3(UInt32 blockSize) -{ - CMsbfEncoderTemp outStreamTemp; - outStreamTemp.SetStream(m_TempArray); - outStreamTemp.Init(); - m_OutStreamCurrent = &outStreamTemp; - - m_NumCrcs = 0; - - EncodeBlock2(m_Block, blockSize, Encoder->NumPasses); - - #ifndef _7ZIP_ST - if (Encoder->MtMode) - Encoder->ThreadsInfo[m_BlockIndex].CanWriteEvent.Lock(); - #endif - for (UInt32 i = 0; i < m_NumCrcs; i++) - Encoder->CombinedCrc.Update(m_CRCs[i]); - Encoder->WriteBytes(m_TempArray, outStreamTemp.GetPos(), outStreamTemp.GetCurByte()); - HRESULT res = S_OK; - #ifndef _7ZIP_ST - if (Encoder->MtMode) - { - UInt32 blockIndex = m_BlockIndex + 1; - if (blockIndex == Encoder->NumThreads) - blockIndex = 0; - - if (Encoder->Progress) - { - UInt64 unpackSize = Encoder->m_OutStream.GetProcessedSize(); - res = Encoder->Progress->SetRatioInfo(&m_PackSize, &unpackSize); - } - - Encoder->ThreadsInfo[blockIndex].CanWriteEvent.Set(); - } - #endif - return res; -} - -void CEncoder::WriteBytes(const Byte *data, UInt32 sizeInBits, Byte lastByte) -{ - UInt32 bytesSize = (sizeInBits / 8); - for (UInt32 i = 0; i < bytesSize; i++) - m_OutStream.WriteBits(data[i], 8); - WriteBits(lastByte, (sizeInBits & 7)); -} - - -HRESULT CEncoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */, const UInt64 * /* outSize */, ICompressProgressInfo *progress) -{ - #ifndef _7ZIP_ST - Progress = progress; - RINOK(Create()); - for (UInt32 t = 0; t < NumThreads; t++) - #endif - { - #ifndef _7ZIP_ST - CThreadInfo &ti = ThreadsInfo[t]; - if (MtMode) - { - RINOK(ti.StreamWasFinishedEvent.Reset()); - RINOK(ti.WaitingWasStartedEvent.Reset()); - RINOK(ti.CanWriteEvent.Reset()); - } - #else - CThreadInfo &ti = ThreadsInfo; - ti.Encoder = this; - #endif - - ti.m_OptimizeNumTables = m_OptimizeNumTables; - - if (!ti.Alloc()) - return E_OUTOFMEMORY; - } - - - if (!m_InStream.Create(kBufferSize)) - return E_OUTOFMEMORY; - if (!m_OutStream.Create(kBufferSize)) - return E_OUTOFMEMORY; - - - m_InStream.SetStream(inStream); - m_InStream.Init(); - - m_OutStream.SetStream(outStream); - m_OutStream.Init(); - - CFlusher flusher(this); - - CombinedCrc.Init(); - #ifndef _7ZIP_ST - NextBlockIndex = 0; - StreamWasFinished = false; - CloseThreads = false; - CanStartWaitingEvent.Reset(); - #endif - - WriteByte(kArSig0); - WriteByte(kArSig1); - WriteByte(kArSig2); - WriteByte((Byte)(kArSig3 + m_BlockSizeMult)); - - #ifndef _7ZIP_ST - - if (MtMode) - { - ThreadsInfo[0].CanWriteEvent.Set(); - Result = S_OK; - CanProcessEvent.Set(); - UInt32 t; - for (t = 0; t < NumThreads; t++) - ThreadsInfo[t].StreamWasFinishedEvent.Lock(); - CanProcessEvent.Reset(); - CanStartWaitingEvent.Set(); - for (t = 0; t < NumThreads; t++) - ThreadsInfo[t].WaitingWasStartedEvent.Lock(); - CanStartWaitingEvent.Reset(); - RINOK(Result); - } - else - #endif - { - for (;;) - { - CThreadInfo &ti = - #ifndef _7ZIP_ST - ThreadsInfo[0]; - #else - ThreadsInfo; - #endif - UInt32 blockSize = ReadRleBlock(ti.m_Block); - if (blockSize == 0) - break; - RINOK(ti.EncodeBlock3(blockSize)); - if (progress) - { - UInt64 packSize = m_InStream.GetProcessedSize(); - UInt64 unpackSize = m_OutStream.GetProcessedSize(); - RINOK(progress->SetRatioInfo(&packSize, &unpackSize)); - } - } - } - WriteByte(kFinSig0); - WriteByte(kFinSig1); - WriteByte(kFinSig2); - WriteByte(kFinSig3); - WriteByte(kFinSig4); - WriteByte(kFinSig5); - - WriteCrc(CombinedCrc.GetDigest()); - return Flush(); -} - -STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - try { return CodeReal(inStream, outStream, inSize, outSize, progress); } - catch(const CInBufferException &e) { return e.ErrorCode; } - catch(const COutBufferException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } -} - -HRESULT CEncoder::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps) -{ - for(UInt32 i = 0; i < numProps; i++) - { - const PROPVARIANT &prop = props[i]; - switch(propIDs[i]) - { - case NCoderPropID::kNumPasses: - { - if (prop.vt != VT_UI4) - return E_INVALIDARG; - UInt32 numPasses = prop.ulVal; - if (numPasses == 0) - numPasses = 1; - if (numPasses > kNumPassesMax) - numPasses = kNumPassesMax; - NumPasses = numPasses; - m_OptimizeNumTables = (NumPasses > 1); - break; - } - case NCoderPropID::kDictionarySize: - { - if (prop.vt != VT_UI4) - return E_INVALIDARG; - UInt32 dictionary = prop.ulVal / kBlockSizeStep; - if (dictionary < kBlockSizeMultMin) - dictionary = kBlockSizeMultMin; - else if (dictionary > kBlockSizeMultMax) - dictionary = kBlockSizeMultMax; - m_BlockSizeMult = dictionary; - break; - } - case NCoderPropID::kNumThreads: - { - #ifndef _7ZIP_ST - if (prop.vt != VT_UI4) - return E_INVALIDARG; - NumThreads = prop.ulVal; - if (NumThreads < 1) - NumThreads = 1; - #endif - break; - } - default: - return E_INVALIDARG; - } - } - return S_OK; -} - -#ifndef _7ZIP_ST -STDMETHODIMP CEncoder::SetNumberOfThreads(UInt32 numThreads) -{ - NumThreads = numThreads; - if (NumThreads < 1) - NumThreads = 1; - return S_OK; -} -#endif - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Encoder.h b/src/libs/7zip/win/CPP/7zip/Compress/BZip2Encoder.h deleted file mode 100644 index a863172fe..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Encoder.h +++ /dev/null @@ -1,245 +0,0 @@ -// BZip2Encoder.h - -#ifndef __COMPRESS_BZIP2_ENCODER_H -#define __COMPRESS_BZIP2_ENCODER_H - -#include "../../Common/Defs.h" -#include "../../Common/MyCom.h" - -#ifndef _7ZIP_ST -#include "../../Windows/Synchronization.h" -#include "../../Windows/Thread.h" -#endif - -#include "../ICoder.h" - -#include "../Common/InBuffer.h" -#include "../Common/OutBuffer.h" - -#include "BitmEncoder.h" -#include "BZip2Const.h" -#include "BZip2Crc.h" - -namespace NCompress { -namespace NBZip2 { - -class CMsbfEncoderTemp -{ - UInt32 m_Pos; - int m_BitPos; - Byte m_CurByte; - Byte *Buffer; -public: - void SetStream(Byte *buffer) { Buffer = buffer; } - Byte *GetStream() const { return Buffer; } - - void Init() - { - m_Pos = 0; - m_BitPos = 8; - m_CurByte = 0; - } - - void Flush() - { - if (m_BitPos < 8) - WriteBits(0, m_BitPos); - } - - void WriteBits(UInt32 value, int numBits) - { - while (numBits > 0) - { - int numNewBits = MyMin(numBits, m_BitPos); - numBits -= numNewBits; - - m_CurByte <<= numNewBits; - UInt32 newBits = value >> numBits; - m_CurByte |= Byte(newBits); - value -= (newBits << numBits); - - m_BitPos -= numNewBits; - - if (m_BitPos == 0) - { - Buffer[m_Pos++] = m_CurByte; - m_BitPos = 8; - } - } - } - - UInt32 GetBytePos() const { return m_Pos ; } - UInt32 GetPos() const { return m_Pos * 8 + (8 - m_BitPos); } - Byte GetCurByte() const { return m_CurByte; } - void SetPos(UInt32 bitPos) - { - m_Pos = bitPos / 8; - m_BitPos = 8 - ((int)bitPos & 7); - } - void SetCurState(int bitPos, Byte curByte) - { - m_BitPos = 8 - bitPos; - m_CurByte = curByte; - } -}; - -class CEncoder; - -const int kNumPassesMax = 10; - -class CThreadInfo -{ -public: - Byte *m_Block; -private: - Byte *m_MtfArray; - Byte *m_TempArray; - UInt32 *m_BlockSorterIndex; - - CMsbfEncoderTemp *m_OutStreamCurrent; - - Byte Lens[kNumTablesMax][kMaxAlphaSize]; - UInt32 Freqs[kNumTablesMax][kMaxAlphaSize]; - UInt32 Codes[kNumTablesMax][kMaxAlphaSize]; - - Byte m_Selectors[kNumSelectorsMax]; - - UInt32 m_CRCs[1 << kNumPassesMax]; - UInt32 m_NumCrcs; - - UInt32 m_BlockIndex; - - void WriteBits2(UInt32 value, UInt32 numBits); - void WriteByte2(Byte b); - void WriteBit2(bool v); - void WriteCrc2(UInt32 v); - - void EncodeBlock(const Byte *block, UInt32 blockSize); - UInt32 EncodeBlockWithHeaders(const Byte *block, UInt32 blockSize); - void EncodeBlock2(const Byte *block, UInt32 blockSize, UInt32 numPasses); -public: - bool m_OptimizeNumTables; - CEncoder *Encoder; - #ifndef _7ZIP_ST - NWindows::CThread Thread; - - NWindows::NSynchronization::CAutoResetEvent StreamWasFinishedEvent; - NWindows::NSynchronization::CAutoResetEvent WaitingWasStartedEvent; - - // it's not member of this thread. We just need one event per thread - NWindows::NSynchronization::CAutoResetEvent CanWriteEvent; - - UInt64 m_PackSize; - - Byte MtPad[1 << 8]; // It's pad for Multi-Threading. Must be >= Cache_Line_Size. - HRESULT Create(); - void FinishStream(bool needLeave); - DWORD ThreadFunc(); - #endif - - CThreadInfo(): m_BlockSorterIndex(0), m_Block(0) {} - ~CThreadInfo() { Free(); } - bool Alloc(); - void Free(); - - HRESULT EncodeBlock3(UInt32 blockSize); -}; - -class CEncoder : - public ICompressCoder, - public ICompressSetCoderProperties, - #ifndef _7ZIP_ST - public ICompressSetCoderMt, - #endif - public CMyUnknownImp -{ - UInt32 m_BlockSizeMult; - bool m_OptimizeNumTables; - - UInt32 m_NumPassesPrev; - - UInt32 m_NumThreadsPrev; -public: - CInBuffer m_InStream; - Byte MtPad[1 << 8]; // It's pad for Multi-Threading. Must be >= Cache_Line_Size. - CBitmEncoder<COutBuffer> m_OutStream; - UInt32 NumPasses; - CBZip2CombinedCrc CombinedCrc; - - #ifndef _7ZIP_ST - CThreadInfo *ThreadsInfo; - NWindows::NSynchronization::CManualResetEvent CanProcessEvent; - NWindows::NSynchronization::CCriticalSection CS; - UInt32 NumThreads; - bool MtMode; - UInt32 NextBlockIndex; - - bool CloseThreads; - bool StreamWasFinished; - NWindows::NSynchronization::CManualResetEvent CanStartWaitingEvent; - - HRESULT Result; - ICompressProgressInfo *Progress; - #else - CThreadInfo ThreadsInfo; - #endif - - UInt32 ReadRleBlock(Byte *buffer); - void WriteBytes(const Byte *data, UInt32 sizeInBits, Byte lastByte); - - void WriteBits(UInt32 value, UInt32 numBits); - void WriteByte(Byte b); - void WriteBit(bool v); - void WriteCrc(UInt32 v); - - #ifndef _7ZIP_ST - HRESULT Create(); - void Free(); - #endif - -public: - CEncoder(); - #ifndef _7ZIP_ST - ~CEncoder(); - #endif - - HRESULT Flush() { return m_OutStream.Flush(); } - - void ReleaseStreams() - { - m_InStream.ReleaseStream(); - m_OutStream.ReleaseStream(); - } - - class CFlusher - { - CEncoder *_coder; - public: - CFlusher(CEncoder *coder): _coder(coder) {} - ~CFlusher() - { - _coder->ReleaseStreams(); - } - }; - - #ifndef _7ZIP_ST - MY_UNKNOWN_IMP2(ICompressSetCoderMt, ICompressSetCoderProperties) - #else - MY_UNKNOWN_IMP1(ICompressSetCoderProperties) - #endif - - HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - STDMETHOD(SetCoderProperties)(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps); - - #ifndef _7ZIP_ST - STDMETHOD(SetNumberOfThreads)(UInt32 numThreads); - #endif -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Register.cpp b/src/libs/7zip/win/CPP/7zip/Compress/BZip2Register.cpp deleted file mode 100644 index ef14204b0..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BZip2Register.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// BZip2Register.cpp - -#include "StdAfx.h" - -#include "../Common/RegisterCodec.h" - -#include "BZip2Decoder.h" - -static void *CreateCodec() { return (void *)(ICompressCoder *)(new NCompress::NBZip2::CDecoder); } -#if !defined(EXTRACT_ONLY) && !defined(BZIP2_EXTRACT_ONLY) -#include "BZip2Encoder.h" -static void *CreateCodecOut() { return (void *)(ICompressCoder *)(new NCompress::NBZip2::CEncoder); } -#else -#define CreateCodecOut 0 -#endif - -static CCodecInfo g_CodecInfo = - { CreateCodec, CreateCodecOut, 0x040202, L"BZip2", 1, false }; - -REGISTER_CODEC(BZip2) diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BitlDecoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/BitlDecoder.cpp deleted file mode 100644 index 78665be8f..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BitlDecoder.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// BitlDecoder.cpp - -#include "StdAfx.h" - -#include "BitlDecoder.h" - -namespace NBitl { - -Byte kInvertTable[256]; - -struct CInverterTableInitializer -{ - CInverterTableInitializer() - { - for (int i = 0; i < 256; i++) - { - int x = ((i & 0x55) << 1) | ((i & 0xAA) >> 1); - x = ((x & 0x33) << 2) | ((x & 0xCC) >> 2); - kInvertTable[i] = (Byte)(((x & 0x0F) << 4) | ((x & 0xF0) >> 4)); - } - } -} g_InverterTableInitializer; - -} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BitlDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/BitlDecoder.h deleted file mode 100644 index ff373bac6..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BitlDecoder.h +++ /dev/null @@ -1,141 +0,0 @@ -// BitlDecoder.h -- the Least Significant Bit of byte is First - -#ifndef __BITL_DECODER_H -#define __BITL_DECODER_H - -#include "../IStream.h" - -namespace NBitl { - -const unsigned kNumBigValueBits = 8 * 4; -const unsigned kNumValueBytes = 3; -const unsigned kNumValueBits = 8 * kNumValueBytes; - -const UInt32 kMask = (1 << kNumValueBits) - 1; - -extern Byte kInvertTable[256]; - -template<class TInByte> -class CBaseDecoder -{ -protected: - unsigned m_BitPos; - UInt32 m_Value; - TInByte m_Stream; -public: - UInt32 NumExtraBytes; - bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } - void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream); } - void ReleaseStream() { m_Stream.ReleaseStream(); } - void Init() - { - m_Stream.Init(); - m_BitPos = kNumBigValueBits; - m_Value = 0; - NumExtraBytes = 0; - } - UInt64 GetProcessedSize() const { return m_Stream.GetProcessedSize() + NumExtraBytes - (kNumBigValueBits - m_BitPos) / 8; } - - void Normalize() - { - for (; m_BitPos >= 8; m_BitPos -= 8) - { - Byte b = 0; - if (!m_Stream.ReadByte(b)) - { - b = 0xFF; // check it - NumExtraBytes++; - } - m_Value = (b << (kNumBigValueBits - m_BitPos)) | m_Value; - } - } - - UInt32 ReadBits(unsigned numBits) - { - Normalize(); - UInt32 res = m_Value & ((1 << numBits) - 1); - m_BitPos += numBits; - m_Value >>= numBits; - return res; - } - - bool ExtraBitsWereRead() const - { - if (NumExtraBytes == 0) - return false; - return ((UInt32)(kNumBigValueBits - m_BitPos) < (NumExtraBytes << 3)); - } -}; - -template<class TInByte> -class CDecoder: public CBaseDecoder<TInByte> -{ - UInt32 m_NormalValue; - -public: - void Init() - { - CBaseDecoder<TInByte>::Init(); - m_NormalValue = 0; - } - - void Normalize() - { - for (; this->m_BitPos >= 8; this->m_BitPos -= 8) - { - Byte b = 0; - if (!this->m_Stream.ReadByte(b)) - { - b = 0xFF; // check it - this->NumExtraBytes++; - } - m_NormalValue = (b << (kNumBigValueBits - this->m_BitPos)) | m_NormalValue; - this->m_Value = (this->m_Value << 8) | kInvertTable[b]; - } - } - - UInt32 GetValue(unsigned numBits) - { - Normalize(); - return ((this->m_Value >> (8 - this->m_BitPos)) & kMask) >> (kNumValueBits - numBits); - } - - void MovePos(unsigned numBits) - { - this->m_BitPos += numBits; - m_NormalValue >>= numBits; - } - - UInt32 ReadBits(unsigned numBits) - { - Normalize(); - UInt32 res = m_NormalValue & ((1 << numBits) - 1); - MovePos(numBits); - return res; - } - - void AlignToByte() { MovePos((32 - this->m_BitPos) & 7); } - - Byte ReadByte() - { - if (this->m_BitPos == kNumBigValueBits) - { - Byte b = 0; - if (!this->m_Stream.ReadByte(b)) - { - b = 0xFF; - this->NumExtraBytes++; - } - return b; - } - { - Byte b = (Byte)(m_NormalValue & 0xFF); - MovePos(8); - return b; - } - } -}; - -} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BitlEncoder.h b/src/libs/7zip/win/CPP/7zip/Compress/BitlEncoder.h deleted file mode 100644 index 7de575456..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BitlEncoder.h +++ /dev/null @@ -1,57 +0,0 @@ -// BitlEncoder.h -- the Least Significant Bit of byte is First - -#ifndef __BITL_ENCODER_H -#define __BITL_ENCODER_H - -#include "../Common/OutBuffer.h" - -class CBitlEncoder -{ - COutBuffer m_Stream; - unsigned m_BitPos; - Byte m_CurByte; -public: - bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } - void SetStream(ISequentialOutStream *outStream) { m_Stream.SetStream(outStream); } - void ReleaseStream() { m_Stream.ReleaseStream(); } - UInt32 GetBitPosition() const { return (8 - m_BitPos); } - UInt64 GetProcessedSize() const { return m_Stream.GetProcessedSize() + (8 - m_BitPos + 7) /8; } - void Init() - { - m_Stream.Init(); - m_BitPos = 8; - m_CurByte = 0; - } - HRESULT Flush() - { - FlushByte(); - return m_Stream.Flush(); - } - void FlushByte() - { - if (m_BitPos < 8) - m_Stream.WriteByte(m_CurByte); - m_BitPos = 8; - m_CurByte = 0; - } - void WriteBits(UInt32 value, unsigned numBits) - { - while (numBits > 0) - { - if (numBits < m_BitPos) - { - m_CurByte |= (value & ((1 << numBits) - 1)) << (8 - m_BitPos); - m_BitPos -= numBits; - return; - } - numBits -= m_BitPos; - m_Stream.WriteByte((Byte)(m_CurByte | (value << (8 - m_BitPos)))); - value >>= m_BitPos; - m_BitPos = 8; - m_CurByte = 0; - } - } - void WriteByte(Byte b) { m_Stream.WriteByte(b);} -}; - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BitmDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/BitmDecoder.h deleted file mode 100644 index 4369b4524..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BitmDecoder.h +++ /dev/null @@ -1,66 +0,0 @@ -// BitmDecoder.h -- the Most Significant Bit of byte is First - -#ifndef __BITM_DECODER_H -#define __BITM_DECODER_H - -#include "../IStream.h" - -namespace NBitm { - -const unsigned kNumBigValueBits = 8 * 4; -const unsigned kNumValueBytes = 3; -const unsigned kNumValueBits = 8 * kNumValueBytes; - -const UInt32 kMask = (1 << kNumValueBits) - 1; - -template<class TInByte> -class CDecoder -{ - unsigned m_BitPos; - UInt32 m_Value; -public: - TInByte m_Stream; - bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } - void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream);} - void ReleaseStream() { m_Stream.ReleaseStream();} - - void Init() - { - m_Stream.Init(); - m_BitPos = kNumBigValueBits; - Normalize(); - } - - UInt64 GetProcessedSize() const { return m_Stream.GetProcessedSize() - (kNumBigValueBits - m_BitPos) / 8; } - - void Normalize() - { - for (;m_BitPos >= 8; m_BitPos -= 8) - m_Value = (m_Value << 8) | m_Stream.ReadByte(); - } - - UInt32 GetValue(unsigned numBits) const - { - // return (m_Value << m_BitPos) >> (kNumBigValueBits - numBits); - return ((m_Value >> (8 - m_BitPos)) & kMask) >> (kNumValueBits - numBits); - } - - void MovePos(unsigned numBits) - { - m_BitPos += numBits; - Normalize(); - } - - UInt32 ReadBits(unsigned numBits) - { - UInt32 res = GetValue(numBits); - MovePos(numBits); - return res; - } - - void AlignToByte() { MovePos((32 - m_BitPos) & 7); } -}; - -} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/BitmEncoder.h b/src/libs/7zip/win/CPP/7zip/Compress/BitmEncoder.h deleted file mode 100644 index a85dbff89..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/BitmEncoder.h +++ /dev/null @@ -1,50 +0,0 @@ -// BitmEncoder.h -- the Most Significant Bit of byte is First - -#ifndef __BITM_ENCODER_H -#define __BITM_ENCODER_H - -#include "../IStream.h" - -template<class TOutByte> -class CBitmEncoder -{ - TOutByte m_Stream; - unsigned m_BitPos; - Byte m_CurByte; -public: - bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } - void SetStream(ISequentialOutStream *outStream) { m_Stream.SetStream(outStream);} - void ReleaseStream() { m_Stream.ReleaseStream(); } - UInt64 GetProcessedSize() const { return m_Stream.GetProcessedSize() + (8 - m_BitPos + 7) / 8; } - void Init() - { - m_Stream.Init(); - m_BitPos = 8; - m_CurByte = 0; - } - HRESULT Flush() - { - if (m_BitPos < 8) - WriteBits(0, m_BitPos); - return m_Stream.Flush(); - } - void WriteBits(UInt32 value, unsigned numBits) - { - while (numBits > 0) - { - if (numBits < m_BitPos) - { - m_CurByte |= ((Byte)value << (m_BitPos -= numBits)); - return; - } - numBits -= m_BitPos; - UInt32 newBits = (value >> numBits); - value -= (newBits << numBits); - m_Stream.WriteByte((Byte)(m_CurByte | newBits)); - m_BitPos = 8; - m_CurByte = 0; - } - } -}; - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Deflate64Register.cpp b/src/libs/7zip/win/CPP/7zip/Compress/Deflate64Register.cpp deleted file mode 100644 index 509e675a5..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Deflate64Register.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// Deflate64Register.cpp - -#include "StdAfx.h" - -#include "../Common/RegisterCodec.h" - -#include "DeflateDecoder.h" - -static void *CreateCodecDeflate64() { return (void *)(ICompressCoder *)(new NCompress::NDeflate::NDecoder::CCOMCoder64); } -#if !defined(EXTRACT_ONLY) && !defined(DEFLATE_EXTRACT_ONLY) -#include "DeflateEncoder.h" -static void *CreateCodecOutDeflate64() { return (void *)(ICompressCoder *)(new NCompress::NDeflate::NEncoder::CCOMCoder64); } -#else -#define CreateCodecOutDeflate64 0 -#endif - -static CCodecInfo g_CodecInfo = - { CreateCodecDeflate64, CreateCodecOutDeflate64, 0x040109, L"Deflate64", 1, false }; - -REGISTER_CODEC(Deflate64) diff --git a/src/libs/7zip/win/CPP/7zip/Compress/DeflateConst.h b/src/libs/7zip/win/CPP/7zip/Compress/DeflateConst.h deleted file mode 100644 index 00e5ab8bf..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/DeflateConst.h +++ /dev/null @@ -1,134 +0,0 @@ -// DeflateConst.h - -#ifndef __DEFLATE_CONST_H -#define __DEFLATE_CONST_H - -namespace NCompress { -namespace NDeflate { - -const int kNumHuffmanBits = 15; - -const UInt32 kHistorySize32 = (1 << 15); -const UInt32 kHistorySize64 = (1 << 16); - -const UInt32 kDistTableSize32 = 30; -const UInt32 kDistTableSize64 = 32; - -const UInt32 kNumLenSymbols32 = 256; -const UInt32 kNumLenSymbols64 = 255; // don't change it. It must be <= 255. -const UInt32 kNumLenSymbolsMax = kNumLenSymbols32; - -const UInt32 kNumLenSlots = 29; - -const UInt32 kFixedDistTableSize = 32; -const UInt32 kFixedLenTableSize = 31; - -const UInt32 kSymbolEndOfBlock = 0x100; -const UInt32 kSymbolMatch = kSymbolEndOfBlock + 1; - -const UInt32 kMainTableSize = kSymbolMatch + kNumLenSlots; -const UInt32 kFixedMainTableSize = kSymbolMatch + kFixedLenTableSize; - -const UInt32 kLevelTableSize = 19; - -const UInt32 kTableDirectLevels = 16; -const UInt32 kTableLevelRepNumber = kTableDirectLevels; -const UInt32 kTableLevel0Number = kTableLevelRepNumber + 1; -const UInt32 kTableLevel0Number2 = kTableLevel0Number + 1; - -const UInt32 kLevelMask = 0xF; - -const Byte kLenStart32[kFixedLenTableSize] = - {0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224, 255, 0, 0}; -const Byte kLenStart64[kFixedLenTableSize] = - {0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224, 0, 0, 0}; - -const Byte kLenDirectBits32[kFixedLenTableSize] = - {0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0}; -const Byte kLenDirectBits64[kFixedLenTableSize] = - {0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 16, 0, 0}; - -const UInt32 kDistStart[kDistTableSize64] = - {0,1,2,3,4,6,8,12,16,24,32,48,64,96,128,192,256,384,512,768, - 1024,1536,2048,3072,4096,6144,8192,12288,16384,24576,32768,49152}; -const Byte kDistDirectBits[kDistTableSize64] = - {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14}; - -const Byte kLevelDirectBits[3] = {2, 3, 7}; - -const Byte kCodeLengthAlphabetOrder[kLevelTableSize] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; - -const UInt32 kMatchMinLen = 3; -const UInt32 kMatchMaxLen32 = kNumLenSymbols32 + kMatchMinLen - 1; //256 + 2 -const UInt32 kMatchMaxLen64 = kNumLenSymbols64 + kMatchMinLen - 1; //255 + 2 -const UInt32 kMatchMaxLen = kMatchMaxLen32; - -const int kFinalBlockFieldSize = 1; - -namespace NFinalBlockField -{ - enum - { - kNotFinalBlock = 0, - kFinalBlock = 1 - }; -} - -const int kBlockTypeFieldSize = 2; - -namespace NBlockType -{ - enum - { - kStored = 0, - kFixedHuffman = 1, - kDynamicHuffman = 2 - }; -} - -const int kNumLenCodesFieldSize = 5; -const int kNumDistCodesFieldSize = 5; -const int kNumLevelCodesFieldSize = 4; - -const UInt32 kNumLitLenCodesMin = 257; -const UInt32 kNumDistCodesMin = 1; -const UInt32 kNumLevelCodesMin = 4; - -const int kLevelFieldSize = 3; - -const int kStoredBlockLengthFieldSize = 16; - -struct CLevels -{ - Byte litLenLevels[kFixedMainTableSize]; - Byte distLevels[kFixedDistTableSize]; - - void SubClear() - { - UInt32 i; - for(i = kNumLitLenCodesMin; i < kFixedMainTableSize; i++) - litLenLevels[i] = 0; - for(i = 0; i < kFixedDistTableSize; i++) - distLevels[i] = 0; - } - - void SetFixedLevels() - { - int i; - - for (i = 0; i < 144; i++) - litLenLevels[i] = 8; - for (; i < 256; i++) - litLenLevels[i] = 9; - for (; i < 280; i++) - litLenLevels[i] = 7; - for (; i < 288; i++) - litLenLevels[i] = 8; - for (i = 0; i < kFixedDistTableSize; i++) // test it: InfoZip only uses kDistTableSize - distLevels[i] = 5; - } -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/DeflateDecoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/DeflateDecoder.cpp deleted file mode 100644 index 2848cd812..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/DeflateDecoder.cpp +++ /dev/null @@ -1,353 +0,0 @@ -// DeflateDecoder.cpp - -#include "StdAfx.h" - -#include "DeflateDecoder.h" - -namespace NCompress { -namespace NDeflate { -namespace NDecoder { - -static const int kLenIdFinished = -1; -static const int kLenIdNeedInit = -2; - -CCoder::CCoder(bool deflate64Mode, bool deflateNSIS): - _deflate64Mode(deflate64Mode), - _deflateNSIS(deflateNSIS), - _keepHistory(false), - _needInitInStream(true), - ZlibMode(false) {} - -UInt32 CCoder::ReadBits(int numBits) -{ - return m_InBitStream.ReadBits(numBits); -} - -bool CCoder::DeCodeLevelTable(Byte *values, int numSymbols) -{ - int i = 0; - do - { - UInt32 number = m_LevelDecoder.DecodeSymbol(&m_InBitStream); - if (number < kTableDirectLevels) - values[i++] = (Byte)number; - else if (number < kLevelTableSize) - { - if (number == kTableLevelRepNumber) - { - if (i == 0) - return false; - int num = ReadBits(2) + 3; - for (; num > 0 && i < numSymbols; num--, i++) - values[i] = values[i - 1]; - } - else - { - int num; - if (number == kTableLevel0Number) - num = ReadBits(3) + 3; - else - num = ReadBits(7) + 11; - for (;num > 0 && i < numSymbols; num--) - values[i++] = 0; - } - } - else - return false; - } - while(i < numSymbols); - return true; -} - -#define RIF(x) { if (!(x)) return false; } - -bool CCoder::ReadTables(void) -{ - m_FinalBlock = (ReadBits(kFinalBlockFieldSize) == NFinalBlockField::kFinalBlock); - UInt32 blockType = ReadBits(kBlockTypeFieldSize); - if (blockType > NBlockType::kDynamicHuffman) - return false; - - if (blockType == NBlockType::kStored) - { - m_StoredMode = true; - m_InBitStream.AlignToByte(); - m_StoredBlockSize = ReadBits(kStoredBlockLengthFieldSize); - if (_deflateNSIS) - return true; - return (m_StoredBlockSize == (UInt16)~ReadBits(kStoredBlockLengthFieldSize)); - } - - m_StoredMode = false; - - CLevels levels; - if (blockType == NBlockType::kFixedHuffman) - { - levels.SetFixedLevels(); - _numDistLevels = _deflate64Mode ? kDistTableSize64 : kDistTableSize32; - } - else - { - int numLitLenLevels = ReadBits(kNumLenCodesFieldSize) + kNumLitLenCodesMin; - _numDistLevels = ReadBits(kNumDistCodesFieldSize) + kNumDistCodesMin; - int numLevelCodes = ReadBits(kNumLevelCodesFieldSize) + kNumLevelCodesMin; - - if (!_deflate64Mode) - if (_numDistLevels > kDistTableSize32) - return false; - - Byte levelLevels[kLevelTableSize]; - for (int i = 0; i < kLevelTableSize; i++) - { - int position = kCodeLengthAlphabetOrder[i]; - if(i < numLevelCodes) - levelLevels[position] = (Byte)ReadBits(kLevelFieldSize); - else - levelLevels[position] = 0; - } - - RIF(m_LevelDecoder.SetCodeLengths(levelLevels)); - - Byte tmpLevels[kFixedMainTableSize + kFixedDistTableSize]; - if (!DeCodeLevelTable(tmpLevels, numLitLenLevels + _numDistLevels)) - return false; - - levels.SubClear(); - memcpy(levels.litLenLevels, tmpLevels, numLitLenLevels); - memcpy(levels.distLevels, tmpLevels + numLitLenLevels, _numDistLevels); - } - RIF(m_MainDecoder.SetCodeLengths(levels.litLenLevels)); - return m_DistDecoder.SetCodeLengths(levels.distLevels); -} - -HRESULT CCoder::CodeSpec(UInt32 curSize) -{ - if (_remainLen == kLenIdFinished) - return S_OK; - if (_remainLen == kLenIdNeedInit) - { - if (!_keepHistory) - if (!m_OutWindowStream.Create(_deflate64Mode ? kHistorySize64: kHistorySize32)) - return E_OUTOFMEMORY; - RINOK(InitInStream(_needInitInStream)); - m_OutWindowStream.Init(_keepHistory); - m_FinalBlock = false; - _remainLen = 0; - _needReadTable = true; - } - - if (curSize == 0) - return S_OK; - - while(_remainLen > 0 && curSize > 0) - { - _remainLen--; - Byte b = m_OutWindowStream.GetByte(_rep0); - m_OutWindowStream.PutByte(b); - curSize--; - } - - while(curSize > 0) - { - if (_needReadTable) - { - if (m_FinalBlock) - { - _remainLen = kLenIdFinished; - break; - } - if (!ReadTables()) - return S_FALSE; - _needReadTable = false; - } - - if(m_StoredMode) - { - for (; m_StoredBlockSize > 0 && curSize > 0; m_StoredBlockSize--, curSize--) - m_OutWindowStream.PutByte(m_InBitStream.ReadByte()); - _needReadTable = (m_StoredBlockSize == 0); - continue; - } - while(curSize > 0) - { - if (m_InBitStream.NumExtraBytes > 4) - return S_FALSE; - - UInt32 number = m_MainDecoder.DecodeSymbol(&m_InBitStream); - if (number < 0x100) - { - m_OutWindowStream.PutByte((Byte)number); - curSize--; - continue; - } - else if (number == kSymbolEndOfBlock) - { - _needReadTable = true; - break; - } - else if (number < kMainTableSize) - { - number -= kSymbolMatch; - UInt32 len; - { - int numBits; - if (_deflate64Mode) - { - len = kLenStart64[number]; - numBits = kLenDirectBits64[number]; - } - else - { - len = kLenStart32[number]; - numBits = kLenDirectBits32[number]; - } - len += kMatchMinLen + m_InBitStream.ReadBits(numBits); - } - UInt32 locLen = len; - if (locLen > curSize) - locLen = (UInt32)curSize; - number = m_DistDecoder.DecodeSymbol(&m_InBitStream); - if (number >= _numDistLevels) - return S_FALSE; - UInt32 distance = kDistStart[number] + m_InBitStream.ReadBits(kDistDirectBits[number]); - if (!m_OutWindowStream.CopyBlock(distance, locLen)) - return S_FALSE; - curSize -= locLen; - len -= locLen; - if (len != 0) - { - _remainLen = (Int32)len; - _rep0 = distance; - break; - } - } - else - return S_FALSE; - } - } - return S_OK; -} - -#ifdef _NO_EXCEPTIONS - -#define DEFLATE_TRY_BEGIN -#define DEFLATE_TRY_END - -#else - -#define DEFLATE_TRY_BEGIN try { -#define DEFLATE_TRY_END } \ - catch(const CInBufferException &e) { return e.ErrorCode; } \ - catch(const CLzOutWindowException &e) { return e.ErrorCode; } \ - catch(...) { return S_FALSE; } - -#endif - -HRESULT CCoder::CodeReal(ISequentialOutStream *outStream, - const UInt64 *outSize, ICompressProgressInfo *progress) -{ - DEFLATE_TRY_BEGIN - m_OutWindowStream.SetStream(outStream); - CCoderReleaser flusher(this); - - const UInt64 inStart = _needInitInStream ? 0 : m_InBitStream.GetProcessedSize(); - const UInt64 start = m_OutWindowStream.GetProcessedSize(); - for (;;) - { - UInt32 curSize = 1 << 18; - if (outSize != 0) - { - const UInt64 rem = *outSize - (m_OutWindowStream.GetProcessedSize() - start); - if (curSize > rem) - curSize = (UInt32)rem; - } - if (curSize == 0) - break; - RINOK(CodeSpec(curSize)); - if (_remainLen == kLenIdFinished) - break; - if (progress != NULL) - { - const UInt64 inSize = m_InBitStream.GetProcessedSize() - inStart; - const UInt64 nowPos64 = m_OutWindowStream.GetProcessedSize() - start; - RINOK(progress->SetRatioInfo(&inSize, &nowPos64)); - } - } - if (_remainLen == kLenIdFinished && ZlibMode) - { - m_InBitStream.AlignToByte(); - for (int i = 0; i < 4; i++) - ZlibFooter[i] = m_InBitStream.ReadByte(); - } - flusher.NeedFlush = false; - HRESULT res = Flush(); - if (res == S_OK && InputEofError()) - return S_FALSE; - return res; - DEFLATE_TRY_END -} - -HRESULT CCoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - SetInStream(inStream); - SetOutStreamSize(outSize); - HRESULT res = CodeReal(outStream, outSize, progress); - ReleaseInStream(); - return res; -} - -STDMETHODIMP CCoder::GetInStreamProcessedSize(UInt64 *value) -{ - if (value == NULL) - return E_INVALIDARG; - *value = m_InBitStream.GetProcessedSize(); - return S_OK; -} - -STDMETHODIMP CCoder::SetInStream(ISequentialInStream *inStream) -{ - m_InBitStream.SetStream(inStream); - return S_OK; -} - -STDMETHODIMP CCoder::ReleaseInStream() -{ - m_InBitStream.ReleaseStream(); - return S_OK; -} - -STDMETHODIMP CCoder::SetOutStreamSize(const UInt64 * /* outSize */) -{ - _remainLen = kLenIdNeedInit; - _needInitInStream = true; - m_OutWindowStream.Init(_keepHistory); - return S_OK; -} - -#ifndef NO_READ_FROM_CODER - -STDMETHODIMP CCoder::Read(void *data, UInt32 size, UInt32 *processedSize) -{ - DEFLATE_TRY_BEGIN - if (processedSize) - *processedSize = 0; - const UInt64 startPos = m_OutWindowStream.GetProcessedSize(); - m_OutWindowStream.SetMemStream((Byte *)data); - RINOK(CodeSpec(size)); - if (processedSize) - *processedSize = (UInt32)(m_OutWindowStream.GetProcessedSize() - startPos); - return Flush(); - DEFLATE_TRY_END -} - -#endif - -STDMETHODIMP CCoder::CodeResume(ISequentialOutStream *outStream, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - _remainLen = kLenIdNeedInit; - m_OutWindowStream.Init(_keepHistory); - return CodeReal(outStream, outSize, progress); -} - -}}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/DeflateDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/DeflateDecoder.h deleted file mode 100644 index 56ab2bea2..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/DeflateDecoder.h +++ /dev/null @@ -1,157 +0,0 @@ -// DeflateDecoder.h - -#ifndef __DEFLATE_DECODER_H -#define __DEFLATE_DECODER_H - -#include "../../Common/MyCom.h" - -#include "../ICoder.h" - -#include "../Common/InBuffer.h" - -#include "BitlDecoder.h" -#include "DeflateConst.h" -#include "HuffmanDecoder.h" -#include "LzOutWindow.h" - -namespace NCompress { -namespace NDeflate { -namespace NDecoder { - -class CCoder: - public ICompressCoder, - public ICompressGetInStreamProcessedSize, - #ifndef NO_READ_FROM_CODER - public ICompressSetInStream, - public ICompressSetOutStreamSize, - public ISequentialInStream, - #endif - public CMyUnknownImp -{ - CLzOutWindow m_OutWindowStream; - NBitl::CDecoder<CInBuffer> m_InBitStream; - NCompress::NHuffman::CDecoder<kNumHuffmanBits, kFixedMainTableSize> m_MainDecoder; - NCompress::NHuffman::CDecoder<kNumHuffmanBits, kFixedDistTableSize> m_DistDecoder; - NCompress::NHuffman::CDecoder<kNumHuffmanBits, kLevelTableSize> m_LevelDecoder; - - UInt32 m_StoredBlockSize; - - bool m_FinalBlock; - bool m_StoredMode; - UInt32 _numDistLevels; - - - bool _deflateNSIS; - bool _deflate64Mode; - bool _keepHistory; - bool _needInitInStream; - Int32 _remainLen; - UInt32 _rep0; - bool _needReadTable; - - UInt32 ReadBits(int numBits); - - bool DeCodeLevelTable(Byte *values, int numSymbols); - bool ReadTables(); - - HRESULT Flush() { return m_OutWindowStream.Flush(); } - class CCoderReleaser - { - CCoder *_coder; - public: - bool NeedFlush; - CCoderReleaser(CCoder *coder): _coder(coder), NeedFlush(true) {} - ~CCoderReleaser() - { - if (NeedFlush) - _coder->Flush(); - _coder->ReleaseOutStream(); - } - }; - friend class CCoderReleaser; - - HRESULT CodeSpec(UInt32 curSize); -public: - bool ZlibMode; - Byte ZlibFooter[4]; - - CCoder(bool deflate64Mode, bool deflateNSIS = false); - virtual ~CCoder() {}; - - void SetKeepHistory(bool keepHistory) { _keepHistory = keepHistory; } - - void ReleaseOutStream() - { - m_OutWindowStream.ReleaseStream(); - } - - HRESULT CodeReal(ISequentialOutStream *outStream, - const UInt64 *outSize, ICompressProgressInfo *progress); - - #ifndef NO_READ_FROM_CODER - MY_UNKNOWN_IMP4( - ICompressGetInStreamProcessedSize, - ICompressSetInStream, - ICompressSetOutStreamSize, - ISequentialInStream - ) - #else - MY_UNKNOWN_IMP1( - ICompressGetInStreamProcessedSize) - #endif - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(SetInStream)(ISequentialInStream *inStream); - STDMETHOD(ReleaseInStream)(); - STDMETHOD(SetOutStreamSize)(const UInt64 *outSize); - - #ifndef NO_READ_FROM_CODER - STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); - #endif - - STDMETHOD(CodeResume)(ISequentialOutStream *outStream, const UInt64 *outSize, ICompressProgressInfo *progress); - - HRESULT InitInStream(bool needInit) - { - if (!m_InBitStream.Create(1 << 17)) - return E_OUTOFMEMORY; - if (needInit) - { - m_InBitStream.Init(); - _needInitInStream = false; - } - return S_OK; - } - - void AlignToByte() { m_InBitStream.AlignToByte(); } - Byte ReadByte() { return (Byte)m_InBitStream.ReadBits(8); } - bool InputEofError() const { return m_InBitStream.ExtraBitsWereRead(); } - UInt64 GetInputProcessedSize() const { return m_InBitStream.GetProcessedSize(); } - - // IGetInStreamProcessedSize - STDMETHOD(GetInStreamProcessedSize)(UInt64 *value); -}; - -class CCOMCoder : public CCoder -{ -public: - CCOMCoder(): CCoder(false) {} -}; - -class CNsisCOMCoder : public CCoder -{ -public: - CNsisCOMCoder(): CCoder(false, true) {} -}; - -class CCOMCoder64 : public CCoder -{ -public: - CCOMCoder64(): CCoder(true) {} -}; - -}}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/DeflateEncoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/DeflateEncoder.cpp deleted file mode 100644 index 35a81cae4..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/DeflateEncoder.cpp +++ /dev/null @@ -1,986 +0,0 @@ -// DeflateEncoder.cpp - -#include "StdAfx.h" - -#include "../../../C/Alloc.h" -#include "../../../C/HuffEnc.h" - -#include "Common/ComTry.h" - -#include "DeflateEncoder.h" - -#undef NO_INLINE - -#ifdef _MSC_VER -#define NO_INLINE MY_NO_INLINE -#else -#define NO_INLINE -#endif - -namespace NCompress { -namespace NDeflate { -namespace NEncoder { - -const int kNumDivPassesMax = 10; // [0, 16); ratio/speed/ram tradeoff; use big value for better compression ratio. -const UInt32 kNumTables = (1 << kNumDivPassesMax); - -static UInt32 kFixedHuffmanCodeBlockSizeMax = (1 << 8); // [0, (1 << 32)); ratio/speed tradeoff; use big value for better compression ratio. -static UInt32 kDivideCodeBlockSizeMin = (1 << 7); // [1, (1 << 32)); ratio/speed tradeoff; use small value for better compression ratio. -static UInt32 kDivideBlockSizeMin = (1 << 6); // [1, (1 << 32)); ratio/speed tradeoff; use small value for better compression ratio. - -static const UInt32 kMaxUncompressedBlockSize = ((1 << 16) - 1) * 1; // [1, (1 << 32)) -static const UInt32 kMatchArraySize = kMaxUncompressedBlockSize * 10; // [kMatchMaxLen * 2, (1 << 32)) -static const UInt32 kMatchArrayLimit = kMatchArraySize - kMatchMaxLen * 4 * sizeof(UInt16); -static const UInt32 kBlockUncompressedSizeThreshold = kMaxUncompressedBlockSize - - kMatchMaxLen - kNumOpts; - -static const int kMaxCodeBitLength = 11; -static const int kMaxLevelBitLength = 7; - -static Byte kNoLiteralStatPrice = 11; -static Byte kNoLenStatPrice = 11; -static Byte kNoPosStatPrice = 6; - -static Byte g_LenSlots[kNumLenSymbolsMax]; -static Byte g_FastPos[1 << 9]; - -class CFastPosInit -{ -public: - CFastPosInit() - { - int i; - for(i = 0; i < kNumLenSlots; i++) - { - int c = kLenStart32[i]; - int j = 1 << kLenDirectBits32[i]; - for(int k = 0; k < j; k++, c++) - g_LenSlots[c] = (Byte)i; - } - - const int kFastSlots = 18; - int c = 0; - for (Byte slotFast = 0; slotFast < kFastSlots; slotFast++) - { - UInt32 k = (1 << kDistDirectBits[slotFast]); - for (UInt32 j = 0; j < k; j++, c++) - g_FastPos[c] = slotFast; - } - } -}; - -static CFastPosInit g_FastPosInit; - - -inline UInt32 GetPosSlot(UInt32 pos) -{ - if (pos < 0x200) - return g_FastPos[pos]; - return g_FastPos[pos >> 8] + 16; -} - -static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); } -static void SzFree(void *p, void *address) { p = p; MyFree(address); } -static ISzAlloc g_Alloc = { SzAlloc, SzFree }; - -CCoder::CCoder(bool deflate64Mode): - m_Deflate64Mode(deflate64Mode), - m_NumPasses(1), - m_NumDivPasses(1), - m_NumFastBytes(32), - _fastMode(false), - _btMode(true), - m_OnePosMatchesMemory(0), - m_DistanceMemory(0), - m_Created(false), - m_Values(0), - m_Tables(0), - m_MatchFinderCycles(0) - // m_SetMfPasses(0) -{ - m_MatchMaxLen = deflate64Mode ? kMatchMaxLen64 : kMatchMaxLen32; - m_NumLenCombinations = deflate64Mode ? kNumLenSymbols64 : kNumLenSymbols32; - m_LenStart = deflate64Mode ? kLenStart64 : kLenStart32; - m_LenDirectBits = deflate64Mode ? kLenDirectBits64 : kLenDirectBits32; - MatchFinder_Construct(&_lzInWindow); -} - -HRESULT CCoder::Create() -{ - COM_TRY_BEGIN - if (m_Values == 0) - { - m_Values = (CCodeValue *)MyAlloc((kMaxUncompressedBlockSize) * sizeof(CCodeValue)); - if (m_Values == 0) - return E_OUTOFMEMORY; - } - if (m_Tables == 0) - { - m_Tables = (CTables *)MyAlloc((kNumTables) * sizeof(CTables)); - if (m_Tables == 0) - return E_OUTOFMEMORY; - } - - if (m_IsMultiPass) - { - if (m_OnePosMatchesMemory == 0) - { - m_OnePosMatchesMemory = (UInt16 *)::MidAlloc(kMatchArraySize * sizeof(UInt16)); - if (m_OnePosMatchesMemory == 0) - return E_OUTOFMEMORY; - } - } - else - { - if (m_DistanceMemory == 0) - { - m_DistanceMemory = (UInt16 *)MyAlloc((kMatchMaxLen + 2) * 2 * sizeof(UInt16)); - if (m_DistanceMemory == 0) - return E_OUTOFMEMORY; - m_MatchDistances = m_DistanceMemory; - } - } - - if (!m_Created) - { - _lzInWindow.btMode = _btMode ? 1 : 0; - _lzInWindow.numHashBytes = 3; - if (!MatchFinder_Create(&_lzInWindow, - m_Deflate64Mode ? kHistorySize64 : kHistorySize32, - kNumOpts + kMaxUncompressedBlockSize, - m_NumFastBytes, m_MatchMaxLen - m_NumFastBytes, &g_Alloc)) - return E_OUTOFMEMORY; - if (!m_OutStream.Create(1 << 20)) - return E_OUTOFMEMORY; - } - if (m_MatchFinderCycles != 0) - _lzInWindow.cutValue = m_MatchFinderCycles; - m_Created = true; - return S_OK; - COM_TRY_END -} - -HRESULT CCoder::BaseSetEncoderProperties2(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps) -{ - for (UInt32 i = 0; i < numProps; i++) - { - const PROPVARIANT &prop = props[i]; - switch(propIDs[i]) - { - case NCoderPropID::kNumPasses: - if (prop.vt != VT_UI4) - return E_INVALIDARG; - m_NumDivPasses = prop.ulVal; - if (m_NumDivPasses == 0) - m_NumDivPasses = 1; - if (m_NumDivPasses == 1) - m_NumPasses = 1; - else if (m_NumDivPasses <= kNumDivPassesMax) - m_NumPasses = 2; - else - { - m_NumPasses = 2 + (m_NumDivPasses - kNumDivPassesMax); - m_NumDivPasses = kNumDivPassesMax; - } - break; - case NCoderPropID::kNumFastBytes: - if (prop.vt != VT_UI4) - return E_INVALIDARG; - m_NumFastBytes = prop.ulVal; - if(m_NumFastBytes < kMatchMinLen || m_NumFastBytes > m_MatchMaxLen) - return E_INVALIDARG; - break; - case NCoderPropID::kMatchFinderCycles: - { - if (prop.vt != VT_UI4) - return E_INVALIDARG; - m_MatchFinderCycles = prop.ulVal; - break; - } - case NCoderPropID::kAlgorithm: - { - if (prop.vt != VT_UI4) - return E_INVALIDARG; - UInt32 maximize = prop.ulVal; - _fastMode = (maximize == 0); - _btMode = !_fastMode; - break; - } - default: - return E_INVALIDARG; - } - } - return S_OK; -} - -void CCoder::Free() -{ - ::MidFree(m_OnePosMatchesMemory); m_OnePosMatchesMemory = 0; - ::MyFree(m_DistanceMemory); m_DistanceMemory = 0; - ::MyFree(m_Values); m_Values = 0; - ::MyFree(m_Tables); m_Tables = 0; -} - -CCoder::~CCoder() -{ - Free(); - MatchFinder_Free(&_lzInWindow, &g_Alloc); -} - -NO_INLINE void CCoder::GetMatches() -{ - if (m_IsMultiPass) - { - m_MatchDistances = m_OnePosMatchesMemory + m_Pos; - if (m_SecondPass) - { - m_Pos += *m_MatchDistances + 1; - return; - } - } - - UInt32 distanceTmp[kMatchMaxLen * 2 + 3]; - - UInt32 numPairs = (_btMode) ? - Bt3Zip_MatchFinder_GetMatches(&_lzInWindow, distanceTmp): - Hc3Zip_MatchFinder_GetMatches(&_lzInWindow, distanceTmp); - - *m_MatchDistances = (UInt16)numPairs; - - if (numPairs > 0) - { - UInt32 i; - for(i = 0; i < numPairs; i += 2) - { - m_MatchDistances[i + 1] = (UInt16)distanceTmp[i]; - m_MatchDistances[i + 2] = (UInt16)distanceTmp[i + 1]; - } - UInt32 len = distanceTmp[numPairs - 2]; - if (len == m_NumFastBytes && m_NumFastBytes != m_MatchMaxLen) - { - UInt32 numAvail = Inline_MatchFinder_GetNumAvailableBytes(&_lzInWindow) + 1; - const Byte *pby = Inline_MatchFinder_GetPointerToCurrentPos(&_lzInWindow) - 1; - const Byte *pby2 = pby - (distanceTmp[numPairs - 1] + 1); - if (numAvail > m_MatchMaxLen) - numAvail = m_MatchMaxLen; - for (; len < numAvail && pby[len] == pby2[len]; len++); - m_MatchDistances[i - 1] = (UInt16)len; - } - } - if (m_IsMultiPass) - m_Pos += numPairs + 1; - if (!m_SecondPass) - m_AdditionalOffset++; -} - -void CCoder::MovePos(UInt32 num) -{ - if (!m_SecondPass && num > 0) - { - if (_btMode) - Bt3Zip_MatchFinder_Skip(&_lzInWindow, num); - else - Hc3Zip_MatchFinder_Skip(&_lzInWindow, num); - m_AdditionalOffset += num; - } -} - -static const UInt32 kIfinityPrice = 0xFFFFFFF; - -NO_INLINE UInt32 CCoder::Backward(UInt32 &backRes, UInt32 cur) -{ - m_OptimumEndIndex = cur; - UInt32 posMem = m_Optimum[cur].PosPrev; - UInt16 backMem = m_Optimum[cur].BackPrev; - do - { - UInt32 posPrev = posMem; - UInt16 backCur = backMem; - backMem = m_Optimum[posPrev].BackPrev; - posMem = m_Optimum[posPrev].PosPrev; - m_Optimum[posPrev].BackPrev = backCur; - m_Optimum[posPrev].PosPrev = (UInt16)cur; - cur = posPrev; - } - while(cur > 0); - backRes = m_Optimum[0].BackPrev; - m_OptimumCurrentIndex = m_Optimum[0].PosPrev; - return m_OptimumCurrentIndex; -} - -NO_INLINE UInt32 CCoder::GetOptimal(UInt32 &backRes) -{ - if(m_OptimumEndIndex != m_OptimumCurrentIndex) - { - UInt32 len = m_Optimum[m_OptimumCurrentIndex].PosPrev - m_OptimumCurrentIndex; - backRes = m_Optimum[m_OptimumCurrentIndex].BackPrev; - m_OptimumCurrentIndex = m_Optimum[m_OptimumCurrentIndex].PosPrev; - return len; - } - m_OptimumCurrentIndex = m_OptimumEndIndex = 0; - - GetMatches(); - - UInt32 numDistancePairs = m_MatchDistances[0]; - if(numDistancePairs == 0) - return 1; - - const UInt16 *matchDistances = m_MatchDistances + 1; - UInt32 lenMain = matchDistances[numDistancePairs - 2]; - - if(lenMain > m_NumFastBytes) - { - backRes = matchDistances[numDistancePairs - 1]; - MovePos(lenMain - 1); - return lenMain; - } - m_Optimum[1].Price = m_LiteralPrices[Inline_MatchFinder_GetIndexByte(&_lzInWindow, 0 - m_AdditionalOffset)]; - m_Optimum[1].PosPrev = 0; - - m_Optimum[2].Price = kIfinityPrice; - m_Optimum[2].PosPrev = 1; - - - UInt32 offs = 0; - for(UInt32 i = kMatchMinLen; i <= lenMain; i++) - { - UInt32 distance = matchDistances[offs + 1]; - m_Optimum[i].PosPrev = 0; - m_Optimum[i].BackPrev = (UInt16)distance; - m_Optimum[i].Price = m_LenPrices[i - kMatchMinLen] + m_PosPrices[GetPosSlot(distance)]; - if (i == matchDistances[offs]) - offs += 2; - } - - UInt32 cur = 0; - UInt32 lenEnd = lenMain; - for (;;) - { - ++cur; - if(cur == lenEnd || cur == kNumOptsBase || m_Pos >= kMatchArrayLimit) - return Backward(backRes, cur); - GetMatches(); - matchDistances = m_MatchDistances + 1; - - UInt32 numDistancePairs = m_MatchDistances[0]; - UInt32 newLen = 0; - if(numDistancePairs != 0) - { - newLen = matchDistances[numDistancePairs - 2]; - if(newLen > m_NumFastBytes) - { - UInt32 len = Backward(backRes, cur); - m_Optimum[cur].BackPrev = matchDistances[numDistancePairs - 1]; - m_OptimumEndIndex = cur + newLen; - m_Optimum[cur].PosPrev = (UInt16)m_OptimumEndIndex; - MovePos(newLen - 1); - return len; - } - } - UInt32 curPrice = m_Optimum[cur].Price; - UInt32 curAnd1Price = curPrice + m_LiteralPrices[Inline_MatchFinder_GetIndexByte(&_lzInWindow, cur - m_AdditionalOffset)]; - COptimal &optimum = m_Optimum[cur + 1]; - if (curAnd1Price < optimum.Price) - { - optimum.Price = curAnd1Price; - optimum.PosPrev = (UInt16)cur; - } - if(numDistancePairs == 0) - continue; - while(lenEnd < cur + newLen) - m_Optimum[++lenEnd].Price = kIfinityPrice; - offs = 0; - UInt32 distance = matchDistances[offs + 1]; - curPrice += m_PosPrices[GetPosSlot(distance)]; - for(UInt32 lenTest = kMatchMinLen; ; lenTest++) - { - UInt32 curAndLenPrice = curPrice + m_LenPrices[lenTest - kMatchMinLen]; - COptimal &optimum = m_Optimum[cur + lenTest]; - if (curAndLenPrice < optimum.Price) - { - optimum.Price = curAndLenPrice; - optimum.PosPrev = (UInt16)cur; - optimum.BackPrev = (UInt16)distance; - } - if (lenTest == matchDistances[offs]) - { - offs += 2; - if (offs == numDistancePairs) - break; - curPrice -= m_PosPrices[GetPosSlot(distance)]; - distance = matchDistances[offs + 1]; - curPrice += m_PosPrices[GetPosSlot(distance)]; - } - } - } -} - -UInt32 CCoder::GetOptimalFast(UInt32 &backRes) -{ - GetMatches(); - UInt32 numDistancePairs = m_MatchDistances[0]; - if (numDistancePairs == 0) - return 1; - UInt32 lenMain = m_MatchDistances[numDistancePairs - 1]; - backRes = m_MatchDistances[numDistancePairs]; - MovePos(lenMain - 1); - return lenMain; -} - -void CTables::InitStructures() -{ - UInt32 i; - for(i = 0; i < 256; i++) - litLenLevels[i] = 8; - litLenLevels[i++] = 13; - for(;i < kFixedMainTableSize; i++) - litLenLevels[i] = 5; - for(i = 0; i < kFixedDistTableSize; i++) - distLevels[i] = 5; -} - -NO_INLINE void CCoder::LevelTableDummy(const Byte *levels, int numLevels, UInt32 *freqs) -{ - int prevLen = 0xFF; - int nextLen = levels[0]; - int count = 0; - int maxCount = 7; - int minCount = 4; - if (nextLen == 0) - { - maxCount = 138; - minCount = 3; - } - for (int n = 0; n < numLevels; n++) - { - int curLen = nextLen; - nextLen = (n < numLevels - 1) ? levels[n + 1] : 0xFF; - count++; - if (count < maxCount && curLen == nextLen) - continue; - - if (count < minCount) - freqs[curLen] += (UInt32)count; - else if (curLen != 0) - { - if (curLen != prevLen) - { - freqs[curLen]++; - count--; - } - freqs[kTableLevelRepNumber]++; - } - else if (count <= 10) - freqs[kTableLevel0Number]++; - else - freqs[kTableLevel0Number2]++; - - count = 0; - prevLen = curLen; - - if (nextLen == 0) - { - maxCount = 138; - minCount = 3; - } - else if (curLen == nextLen) - { - maxCount = 6; - minCount = 3; - } - else - { - maxCount = 7; - minCount = 4; - } - } -} - -NO_INLINE void CCoder::WriteBits(UInt32 value, int numBits) -{ - m_OutStream.WriteBits(value, numBits); -} - -#define WRITE_HF2(codes, lens, i) m_OutStream.WriteBits(codes[i], lens[i]) -#define WRITE_HF(i) WriteBits(codes[i], lens[i]) - -NO_INLINE void CCoder::LevelTableCode(const Byte *levels, int numLevels, const Byte *lens, const UInt32 *codes) -{ - int prevLen = 0xFF; - int nextLen = levels[0]; - int count = 0; - int maxCount = 7; - int minCount = 4; - if (nextLen == 0) - { - maxCount = 138; - minCount = 3; - } - for (int n = 0; n < numLevels; n++) - { - int curLen = nextLen; - nextLen = (n < numLevels - 1) ? levels[n + 1] : 0xFF; - count++; - if (count < maxCount && curLen == nextLen) - continue; - - if (count < minCount) - for(int i = 0; i < count; i++) - WRITE_HF(curLen); - else if (curLen != 0) - { - if (curLen != prevLen) - { - WRITE_HF(curLen); - count--; - } - WRITE_HF(kTableLevelRepNumber); - WriteBits(count - 3, 2); - } - else if (count <= 10) - { - WRITE_HF(kTableLevel0Number); - WriteBits(count - 3, 3); - } - else - { - WRITE_HF(kTableLevel0Number2); - WriteBits(count - 11, 7); - } - - count = 0; - prevLen = curLen; - - if (nextLen == 0) - { - maxCount = 138; - minCount = 3; - } - else if (curLen == nextLen) - { - maxCount = 6; - minCount = 3; - } - else - { - maxCount = 7; - minCount = 4; - } - } -} - -NO_INLINE void CCoder::MakeTables(unsigned maxHuffLen) -{ - Huffman_Generate(mainFreqs, mainCodes, m_NewLevels.litLenLevels, kFixedMainTableSize, maxHuffLen); - Huffman_Generate(distFreqs, distCodes, m_NewLevels.distLevels, kDistTableSize64, maxHuffLen); -} - -NO_INLINE UInt32 Huffman_GetPrice(const UInt32 *freqs, const Byte *lens, UInt32 num) -{ - UInt32 price = 0; - UInt32 i; - for (i = 0; i < num; i++) - price += lens[i] * freqs[i]; - return price; -} - -NO_INLINE UInt32 Huffman_GetPrice_Spec(const UInt32 *freqs, const Byte *lens, UInt32 num, const Byte *extraBits, UInt32 extraBase) -{ - return Huffman_GetPrice(freqs, lens, num) + - Huffman_GetPrice(freqs + extraBase, extraBits, num - extraBase); -} - -NO_INLINE UInt32 CCoder::GetLzBlockPrice() const -{ - return - Huffman_GetPrice_Spec(mainFreqs, m_NewLevels.litLenLevels, kFixedMainTableSize, m_LenDirectBits, kSymbolMatch) + - Huffman_GetPrice_Spec(distFreqs, m_NewLevels.distLevels, kDistTableSize64, kDistDirectBits, 0); -} - -NO_INLINE void CCoder::TryBlock() -{ - memset(mainFreqs, 0, sizeof(mainFreqs)); - memset(distFreqs, 0, sizeof(distFreqs)); - - m_ValueIndex = 0; - UInt32 blockSize = BlockSizeRes; - BlockSizeRes = 0; - for (;;) - { - if (m_OptimumCurrentIndex == m_OptimumEndIndex) - { - if (m_Pos >= kMatchArrayLimit || BlockSizeRes >= blockSize || !m_SecondPass && - ((Inline_MatchFinder_GetNumAvailableBytes(&_lzInWindow) == 0) || m_ValueIndex >= m_ValueBlockSize)) - break; - } - UInt32 pos; - UInt32 len; - if (_fastMode) - len = GetOptimalFast(pos); - else - len = GetOptimal(pos); - CCodeValue &codeValue = m_Values[m_ValueIndex++]; - if (len >= kMatchMinLen) - { - UInt32 newLen = len - kMatchMinLen; - codeValue.Len = (UInt16)newLen; - mainFreqs[kSymbolMatch + g_LenSlots[newLen]]++; - codeValue.Pos = (UInt16)pos; - distFreqs[GetPosSlot(pos)]++; - } - else - { - Byte b = Inline_MatchFinder_GetIndexByte(&_lzInWindow, 0 - m_AdditionalOffset); - mainFreqs[b]++; - codeValue.SetAsLiteral(); - codeValue.Pos = b; - } - m_AdditionalOffset -= len; - BlockSizeRes += len; - } - mainFreqs[kSymbolEndOfBlock]++; - m_AdditionalOffset += BlockSizeRes; - m_SecondPass = true; -} - -NO_INLINE void CCoder::SetPrices(const CLevels &levels) -{ - if (_fastMode) - return; - UInt32 i; - for(i = 0; i < 256; i++) - { - Byte price = levels.litLenLevels[i]; - m_LiteralPrices[i] = ((price != 0) ? price : kNoLiteralStatPrice); - } - - for(i = 0; i < m_NumLenCombinations; i++) - { - UInt32 slot = g_LenSlots[i]; - Byte price = levels.litLenLevels[kSymbolMatch + slot]; - m_LenPrices[i] = (Byte)(((price != 0) ? price : kNoLenStatPrice) + m_LenDirectBits[slot]); - } - - for(i = 0; i < kDistTableSize64; i++) - { - Byte price = levels.distLevels[i]; - m_PosPrices[i] = (Byte)(((price != 0) ? price: kNoPosStatPrice) + kDistDirectBits[i]); - } -} - -NO_INLINE void Huffman_ReverseBits(UInt32 *codes, const Byte *lens, UInt32 num) -{ - for (UInt32 i = 0; i < num; i++) - { - UInt32 x = codes[i]; - x = ((x & 0x5555) << 1) | ((x & 0xAAAA) >> 1); - x = ((x & 0x3333) << 2) | ((x & 0xCCCC) >> 2); - x = ((x & 0x0F0F) << 4) | ((x & 0xF0F0) >> 4); - codes[i] = (((x & 0x00FF) << 8) | ((x & 0xFF00) >> 8)) >> (16 - lens[i]); - } -} - -NO_INLINE void CCoder::WriteBlock() -{ - Huffman_ReverseBits(mainCodes, m_NewLevels.litLenLevels, kFixedMainTableSize); - Huffman_ReverseBits(distCodes, m_NewLevels.distLevels, kDistTableSize64); - - for (UInt32 i = 0; i < m_ValueIndex; i++) - { - const CCodeValue &codeValue = m_Values[i]; - if (codeValue.IsLiteral()) - WRITE_HF2(mainCodes, m_NewLevels.litLenLevels, codeValue.Pos); - else - { - UInt32 len = codeValue.Len; - UInt32 lenSlot = g_LenSlots[len]; - WRITE_HF2(mainCodes, m_NewLevels.litLenLevels, kSymbolMatch + lenSlot); - m_OutStream.WriteBits(len - m_LenStart[lenSlot], m_LenDirectBits[lenSlot]); - UInt32 dist = codeValue.Pos; - UInt32 posSlot = GetPosSlot(dist); - WRITE_HF2(distCodes, m_NewLevels.distLevels, posSlot); - m_OutStream.WriteBits(dist - kDistStart[posSlot], kDistDirectBits[posSlot]); - } - } - WRITE_HF2(mainCodes, m_NewLevels.litLenLevels, kSymbolEndOfBlock); -} - -static UInt32 GetStorePrice(UInt32 blockSize, int bitPosition) -{ - UInt32 price = 0; - do - { - UInt32 nextBitPosition = (bitPosition + kFinalBlockFieldSize + kBlockTypeFieldSize) & 7; - int numBitsForAlign = nextBitPosition > 0 ? (8 - nextBitPosition): 0; - UInt32 curBlockSize = (blockSize < (1 << 16)) ? blockSize : (1 << 16) - 1; - price += kFinalBlockFieldSize + kBlockTypeFieldSize + numBitsForAlign + (2 + 2) * 8 + curBlockSize * 8; - bitPosition = 0; - blockSize -= curBlockSize; - } - while(blockSize != 0); - return price; -} - -void CCoder::WriteStoreBlock(UInt32 blockSize, UInt32 additionalOffset, bool finalBlock) -{ - do - { - UInt32 curBlockSize = (blockSize < (1 << 16)) ? blockSize : (1 << 16) - 1; - blockSize -= curBlockSize; - WriteBits((finalBlock && (blockSize == 0) ? NFinalBlockField::kFinalBlock: NFinalBlockField::kNotFinalBlock), kFinalBlockFieldSize); - WriteBits(NBlockType::kStored, kBlockTypeFieldSize); - m_OutStream.FlushByte(); - WriteBits((UInt16)curBlockSize, kStoredBlockLengthFieldSize); - WriteBits((UInt16)~curBlockSize, kStoredBlockLengthFieldSize); - const Byte *data = Inline_MatchFinder_GetPointerToCurrentPos(&_lzInWindow)- additionalOffset; - for(UInt32 i = 0; i < curBlockSize; i++) - m_OutStream.WriteByte(data[i]); - additionalOffset -= curBlockSize; - } - while(blockSize != 0); -} - -NO_INLINE UInt32 CCoder::TryDynBlock(int tableIndex, UInt32 numPasses) -{ - CTables &t = m_Tables[tableIndex]; - BlockSizeRes = t.BlockSizeRes; - UInt32 posTemp = t.m_Pos; - SetPrices(t); - - for (UInt32 p = 0; p < numPasses; p++) - { - m_Pos = posTemp; - TryBlock(); - unsigned numHuffBits = - (m_ValueIndex > 18000 ? 12 : - (m_ValueIndex > 7000 ? 11 : - (m_ValueIndex > 2000 ? 10 : 9))); - MakeTables(numHuffBits); - SetPrices(m_NewLevels); - } - - (CLevels &)t = m_NewLevels; - - m_NumLitLenLevels = kMainTableSize; - while(m_NumLitLenLevels > kNumLitLenCodesMin && m_NewLevels.litLenLevels[m_NumLitLenLevels - 1] == 0) - m_NumLitLenLevels--; - - m_NumDistLevels = kDistTableSize64; - while(m_NumDistLevels > kNumDistCodesMin && m_NewLevels.distLevels[m_NumDistLevels - 1] == 0) - m_NumDistLevels--; - - UInt32 levelFreqs[kLevelTableSize]; - memset(levelFreqs, 0, sizeof(levelFreqs)); - - LevelTableDummy(m_NewLevels.litLenLevels, m_NumLitLenLevels, levelFreqs); - LevelTableDummy(m_NewLevels.distLevels, m_NumDistLevels, levelFreqs); - - Huffman_Generate(levelFreqs, levelCodes, levelLens, kLevelTableSize, kMaxLevelBitLength); - - m_NumLevelCodes = kNumLevelCodesMin; - for (UInt32 i = 0; i < kLevelTableSize; i++) - { - Byte level = levelLens[kCodeLengthAlphabetOrder[i]]; - if (level > 0 && i >= m_NumLevelCodes) - m_NumLevelCodes = i + 1; - m_LevelLevels[i] = level; - } - - return GetLzBlockPrice() + - Huffman_GetPrice_Spec(levelFreqs, levelLens, kLevelTableSize, kLevelDirectBits, kTableDirectLevels) + - kNumLenCodesFieldSize + kNumDistCodesFieldSize + kNumLevelCodesFieldSize + - m_NumLevelCodes * kLevelFieldSize + kFinalBlockFieldSize + kBlockTypeFieldSize; -} - -NO_INLINE UInt32 CCoder::TryFixedBlock(int tableIndex) -{ - CTables &t = m_Tables[tableIndex]; - BlockSizeRes = t.BlockSizeRes; - m_Pos = t.m_Pos; - m_NewLevels.SetFixedLevels(); - SetPrices(m_NewLevels); - TryBlock(); - return kFinalBlockFieldSize + kBlockTypeFieldSize + GetLzBlockPrice(); -} - -NO_INLINE UInt32 CCoder::GetBlockPrice(int tableIndex, int numDivPasses) -{ - CTables &t = m_Tables[tableIndex]; - t.StaticMode = false; - UInt32 price = TryDynBlock(tableIndex, m_NumPasses); - t.BlockSizeRes = BlockSizeRes; - UInt32 numValues = m_ValueIndex; - UInt32 posTemp = m_Pos; - UInt32 additionalOffsetEnd = m_AdditionalOffset; - - if (m_CheckStatic && m_ValueIndex <= kFixedHuffmanCodeBlockSizeMax) - { - const UInt32 fixedPrice = TryFixedBlock(tableIndex); - t.StaticMode = (fixedPrice < price); - if (t.StaticMode) - price = fixedPrice; - } - - const UInt32 storePrice = GetStorePrice(BlockSizeRes, 0); // bitPosition - t.StoreMode = (storePrice <= price); - if (t.StoreMode) - price = storePrice; - - t.UseSubBlocks = false; - - if (numDivPasses > 1 && numValues >= kDivideCodeBlockSizeMin) - { - CTables &t0 = m_Tables[(tableIndex << 1)]; - (CLevels &)t0 = t; - t0.BlockSizeRes = t.BlockSizeRes >> 1; - t0.m_Pos = t.m_Pos; - UInt32 subPrice = GetBlockPrice((tableIndex << 1), numDivPasses - 1); - - UInt32 blockSize2 = t.BlockSizeRes - t0.BlockSizeRes; - if (t0.BlockSizeRes >= kDivideBlockSizeMin && blockSize2 >= kDivideBlockSizeMin) - { - CTables &t1 = m_Tables[(tableIndex << 1) + 1]; - (CLevels &)t1 = t; - t1.BlockSizeRes = blockSize2; - t1.m_Pos = m_Pos; - m_AdditionalOffset -= t0.BlockSizeRes; - subPrice += GetBlockPrice((tableIndex << 1) + 1, numDivPasses - 1); - t.UseSubBlocks = (subPrice < price); - if (t.UseSubBlocks) - price = subPrice; - } - } - m_AdditionalOffset = additionalOffsetEnd; - m_Pos = posTemp; - return price; -} - -void CCoder::CodeBlock(int tableIndex, bool finalBlock) -{ - CTables &t = m_Tables[tableIndex]; - if (t.UseSubBlocks) - { - CodeBlock((tableIndex << 1), false); - CodeBlock((tableIndex << 1) + 1, finalBlock); - } - else - { - if (t.StoreMode) - WriteStoreBlock(t.BlockSizeRes, m_AdditionalOffset, finalBlock); - else - { - WriteBits((finalBlock ? NFinalBlockField::kFinalBlock: NFinalBlockField::kNotFinalBlock), kFinalBlockFieldSize); - if (t.StaticMode) - { - WriteBits(NBlockType::kFixedHuffman, kBlockTypeFieldSize); - TryFixedBlock(tableIndex); - int i; - const int kMaxStaticHuffLen = 9; - for (i = 0; i < kFixedMainTableSize; i++) - mainFreqs[i] = (UInt32)1 << (kMaxStaticHuffLen - m_NewLevels.litLenLevels[i]); - for (i = 0; i < kFixedDistTableSize; i++) - distFreqs[i] = (UInt32)1 << (kMaxStaticHuffLen - m_NewLevels.distLevels[i]); - MakeTables(kMaxStaticHuffLen); - } - else - { - if (m_NumDivPasses > 1 || m_CheckStatic) - TryDynBlock(tableIndex, 1); - WriteBits(NBlockType::kDynamicHuffman, kBlockTypeFieldSize); - WriteBits(m_NumLitLenLevels - kNumLitLenCodesMin, kNumLenCodesFieldSize); - WriteBits(m_NumDistLevels - kNumDistCodesMin, kNumDistCodesFieldSize); - WriteBits(m_NumLevelCodes - kNumLevelCodesMin, kNumLevelCodesFieldSize); - - for (UInt32 i = 0; i < m_NumLevelCodes; i++) - WriteBits(m_LevelLevels[i], kLevelFieldSize); - - Huffman_ReverseBits(levelCodes, levelLens, kLevelTableSize); - LevelTableCode(m_NewLevels.litLenLevels, m_NumLitLenLevels, levelLens, levelCodes); - LevelTableCode(m_NewLevels.distLevels, m_NumDistLevels, levelLens, levelCodes); - } - WriteBlock(); - } - m_AdditionalOffset -= t.BlockSizeRes; - } -} - -SRes Read(void *object, void *data, size_t *size) -{ - const UInt32 kStepSize = (UInt32)1 << 31; - UInt32 curSize = ((*size < kStepSize) ? (UInt32)*size : kStepSize); - HRESULT res = ((CSeqInStream *)object)->RealStream->Read(data, curSize, &curSize); - *size = curSize; - return (SRes)res; -} - -HRESULT CCoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */ , const UInt64 * /* outSize */ , ICompressProgressInfo *progress) -{ - m_CheckStatic = (m_NumPasses != 1 || m_NumDivPasses != 1); - m_IsMultiPass = (m_CheckStatic || (m_NumPasses != 1 || m_NumDivPasses != 1)); - - RINOK(Create()); - - m_ValueBlockSize = (7 << 10) + (1 << 12) * m_NumDivPasses; - - UInt64 nowPos = 0; - - _seqInStream.RealStream = inStream; - _seqInStream.SeqInStream.Read = Read; - _lzInWindow.stream = &_seqInStream.SeqInStream; - - MatchFinder_Init(&_lzInWindow); - m_OutStream.SetStream(outStream); - m_OutStream.Init(); - - CCoderReleaser coderReleaser(this); - - m_OptimumEndIndex = m_OptimumCurrentIndex = 0; - - CTables &t = m_Tables[1]; - t.m_Pos = 0; - t.InitStructures(); - - m_AdditionalOffset = 0; - do - { - t.BlockSizeRes = kBlockUncompressedSizeThreshold; - m_SecondPass = false; - GetBlockPrice(1, m_NumDivPasses); - CodeBlock(1, Inline_MatchFinder_GetNumAvailableBytes(&_lzInWindow) == 0); - nowPos += m_Tables[1].BlockSizeRes; - if (progress != NULL) - { - UInt64 packSize = m_OutStream.GetProcessedSize(); - RINOK(progress->SetRatioInfo(&nowPos, &packSize)); - } - } - while (Inline_MatchFinder_GetNumAvailableBytes(&_lzInWindow) != 0); - if (_lzInWindow.result != SZ_OK) - return _lzInWindow.result; - return m_OutStream.Flush(); -} - -HRESULT CCoder::BaseCode(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - try { return CodeReal(inStream, outStream, inSize, outSize, progress); } - catch(const COutBufferException &e) { return e.ErrorCode; } - catch(...) { return E_FAIL; } -} - -STDMETHODIMP CCOMCoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) - { return BaseCode(inStream, outStream, inSize, outSize, progress); } - -STDMETHODIMP CCOMCoder::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps) - { return BaseSetEncoderProperties2(propIDs, props, numProps); } - -STDMETHODIMP CCOMCoder64::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) - { return BaseCode(inStream, outStream, inSize, outSize, progress); } - -STDMETHODIMP CCOMCoder64::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps) - { return BaseSetEncoderProperties2(propIDs, props, numProps); } - -}}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/DeflateEncoder.h b/src/libs/7zip/win/CPP/7zip/Compress/DeflateEncoder.h deleted file mode 100644 index 71c39e4e5..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/DeflateEncoder.h +++ /dev/null @@ -1,211 +0,0 @@ -// DeflateEncoder.h - -#ifndef __DEFLATE_ENCODER_H -#define __DEFLATE_ENCODER_H - -#include "../../../C/LzFind.h" - -#include "Common/MyCom.h" - -#include "../ICoder.h" - -#include "BitlEncoder.h" -#include "DeflateConst.h" - -namespace NCompress { -namespace NDeflate { -namespace NEncoder { - -struct CCodeValue -{ - UInt16 Len; - UInt16 Pos; - void SetAsLiteral() { Len = (1 << 15); } - bool IsLiteral() const { return (Len >= (1 << 15)); } -}; - -struct COptimal -{ - UInt32 Price; - UInt16 PosPrev; - UInt16 BackPrev; -}; - -const UInt32 kNumOptsBase = 1 << 12; -const UInt32 kNumOpts = kNumOptsBase + kMatchMaxLen; - -class CCoder; - -struct CTables: public CLevels -{ - bool UseSubBlocks; - bool StoreMode; - bool StaticMode; - UInt32 BlockSizeRes; - UInt32 m_Pos; - void InitStructures(); -}; - -typedef struct _CSeqInStream -{ - ISeqInStream SeqInStream; - CMyComPtr<ISequentialInStream> RealStream; -} CSeqInStream; - -class CCoder -{ - CMatchFinder _lzInWindow; - CBitlEncoder m_OutStream; - - CSeqInStream _seqInStream; - -public: - CCodeValue *m_Values; - - UInt16 *m_MatchDistances; - UInt32 m_NumFastBytes; - bool _fastMode; - bool _btMode; - - UInt16 *m_OnePosMatchesMemory; - UInt16 *m_DistanceMemory; - - UInt32 m_Pos; - - int m_NumPasses; - int m_NumDivPasses; - bool m_CheckStatic; - bool m_IsMultiPass; - UInt32 m_ValueBlockSize; - - UInt32 m_NumLenCombinations; - UInt32 m_MatchMaxLen; - const Byte *m_LenStart; - const Byte *m_LenDirectBits; - - bool m_Created; - bool m_Deflate64Mode; - - Byte m_LevelLevels[kLevelTableSize]; - int m_NumLitLenLevels; - int m_NumDistLevels; - UInt32 m_NumLevelCodes; - UInt32 m_ValueIndex; - - bool m_SecondPass; - UInt32 m_AdditionalOffset; - - UInt32 m_OptimumEndIndex; - UInt32 m_OptimumCurrentIndex; - - Byte m_LiteralPrices[256]; - Byte m_LenPrices[kNumLenSymbolsMax]; - Byte m_PosPrices[kDistTableSize64]; - - CLevels m_NewLevels; - UInt32 mainFreqs[kFixedMainTableSize]; - UInt32 distFreqs[kDistTableSize64]; - UInt32 mainCodes[kFixedMainTableSize]; - UInt32 distCodes[kDistTableSize64]; - UInt32 levelCodes[kLevelTableSize]; - Byte levelLens[kLevelTableSize]; - - UInt32 BlockSizeRes; - - CTables *m_Tables; - COptimal m_Optimum[kNumOpts]; - - UInt32 m_MatchFinderCycles; - // IMatchFinderSetNumPasses *m_SetMfPasses; - - void GetMatches(); - void MovePos(UInt32 num); - UInt32 Backward(UInt32 &backRes, UInt32 cur); - UInt32 GetOptimal(UInt32 &backRes); - UInt32 GetOptimalFast(UInt32 &backRes); - - void LevelTableDummy(const Byte *levels, int numLevels, UInt32 *freqs); - - void WriteBits(UInt32 value, int numBits); - void LevelTableCode(const Byte *levels, int numLevels, const Byte *lens, const UInt32 *codes); - - void MakeTables(unsigned maxHuffLen); - UInt32 GetLzBlockPrice() const; - void TryBlock(); - UInt32 TryDynBlock(int tableIndex, UInt32 numPasses); - - UInt32 TryFixedBlock(int tableIndex); - - void SetPrices(const CLevels &levels); - void WriteBlock(); - - HRESULT Create(); - void Free(); - - void WriteStoreBlock(UInt32 blockSize, UInt32 additionalOffset, bool finalBlock); - void WriteTables(bool writeMode, bool finalBlock); - - void WriteBlockData(bool writeMode, bool finalBlock); - - void ReleaseStreams() - { - _seqInStream.RealStream.Release(); - m_OutStream.ReleaseStream(); - } - class CCoderReleaser - { - CCoder *m_Coder; - public: - CCoderReleaser(CCoder *coder): m_Coder(coder) {} - ~CCoderReleaser() { m_Coder->ReleaseStreams(); } - }; - friend class CCoderReleaser; - - UInt32 GetBlockPrice(int tableIndex, int numDivPasses); - void CodeBlock(int tableIndex, bool finalBlock); - -public: - CCoder(bool deflate64Mode = false); - ~CCoder(); - - HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - HRESULT BaseCode(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - HRESULT BaseSetEncoderProperties2(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps); -}; - - -class CCOMCoder : - public ICompressCoder, - public ICompressSetCoderProperties, - public CMyUnknownImp, - public CCoder -{ -public: - MY_UNKNOWN_IMP1(ICompressSetCoderProperties) - CCOMCoder(): CCoder(false) {}; - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - STDMETHOD(SetCoderProperties)(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps); -}; - -class CCOMCoder64 : - public ICompressCoder, - public ICompressSetCoderProperties, - public CMyUnknownImp, - public CCoder -{ -public: - MY_UNKNOWN_IMP1(ICompressSetCoderProperties) - CCOMCoder64(): CCoder(true) {}; - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - STDMETHOD(SetCoderProperties)(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps); -}; - -}}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/DeflateRegister.cpp b/src/libs/7zip/win/CPP/7zip/Compress/DeflateRegister.cpp deleted file mode 100644 index 45f55219e..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/DeflateRegister.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// DeflateRegister.cpp - -#include "StdAfx.h" - -#include "../Common/RegisterCodec.h" - -#include "DeflateDecoder.h" - -static void *CreateCodecDeflate() { return (void *)(ICompressCoder *)(new NCompress::NDeflate::NDecoder::CCOMCoder); } - -#if !defined(EXTRACT_ONLY) && !defined(DEFLATE_EXTRACT_ONLY) -#include "DeflateEncoder.h" -static void *CreateCodecOutDeflate() { return (void *)(ICompressCoder *)(new NCompress::NDeflate::NEncoder::CCOMCoder); } -#else -#define CreateCodecOutDeflate 0 -#endif - -static CCodecInfo g_CodecInfo = - { CreateCodecDeflate, CreateCodecOutDeflate, 0x040108, L"Deflate", 1, false }; - -REGISTER_CODEC(Deflate) diff --git a/src/libs/7zip/win/CPP/7zip/Compress/DllExports.cpp b/src/libs/7zip/win/CPP/7zip/Compress/DllExports.cpp deleted file mode 100644 index 6af7e5ec5..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/DllExports.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// DllExports.cpp - -#include "StdAfx.h" - -#include "../../Common/MyInitGuid.h" - -#include "../ICoder.h" - -#include "../Common/RegisterCodec.h" - -static const unsigned int kNumCodecsMax = 32; -unsigned int g_NumCodecs = 0; -const CCodecInfo *g_Codecs[kNumCodecsMax]; -void RegisterCodec(const CCodecInfo *codecInfo) -{ - if (g_NumCodecs < kNumCodecsMax) - g_Codecs[g_NumCodecs++] = codecInfo; -} - -#ifdef _WIN32 -extern "C" -BOOL WINAPI DllMain( - #ifdef UNDER_CE - HANDLE - #else - HINSTANCE - #endif - , DWORD /* dwReason */, LPVOID /*lpReserved*/) -{ - return TRUE; -} -#endif - -static const UInt16 kDecodeId = 0x2790; - -DEFINE_GUID(CLSID_CCodec, -0x23170F69, 0x40C1, kDecodeId, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); - -STDAPI CreateCoder(const GUID *clsid, const GUID *iid, void **outObject); - -STDAPI CreateObject(const GUID *clsid, const GUID *iid, void **outObject) -{ - return CreateCoder(clsid, iid, outObject); -} - diff --git a/src/libs/7zip/win/CPP/7zip/Compress/DllExports2.cpp b/src/libs/7zip/win/CPP/7zip/Compress/DllExports2.cpp deleted file mode 100644 index 836f3fa42..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/DllExports2.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// DllExports.cpp - -#include "StdAfx.h" - -#include "../../Common/MyInitGuid.h" - -#include "../ICoder.h" - -#include "../Common/RegisterCodec.h" - -extern "C" -BOOL WINAPI DllMain(HINSTANCE /* hInstance */, DWORD /* dwReason */, LPVOID /*lpReserved*/) -{ - return TRUE; -} - -static const UInt16 kDecodeId = 0x2790; - -DEFINE_GUID(CLSID_CCodec, -0x23170F69, 0x40C1, kDecodeId, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); - -STDAPI CreateCoder(const GUID *clsid, const GUID *iid, void **outObject); - -STDAPI CreateObject(const GUID *clsid, const GUID *iid, void **outObject) -{ - return CreateCoder(clsid, iid, outObject); -} - diff --git a/src/libs/7zip/win/CPP/7zip/Compress/HuffmanDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/HuffmanDecoder.h deleted file mode 100644 index 82a1e6f2d..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/HuffmanDecoder.h +++ /dev/null @@ -1,89 +0,0 @@ -// Compress/HuffmanDecoder.h - -#ifndef __COMPRESS_HUFFMAN_DECODER_H -#define __COMPRESS_HUFFMAN_DECODER_H - -#include "../../Common/Types.h" - -namespace NCompress { -namespace NHuffman { - -const int kNumTableBits = 9; - -template <int kNumBitsMax, UInt32 m_NumSymbols> -class CDecoder -{ - UInt32 m_Limits[kNumBitsMax + 1]; // m_Limits[i] = value limit for symbols with length = i - UInt32 m_Positions[kNumBitsMax + 1]; // m_Positions[i] = index in m_Symbols[] of first symbol with length = i - UInt32 m_Symbols[m_NumSymbols]; - Byte m_Lengths[1 << kNumTableBits]; // Table oh length for short codes. - -public: - - bool SetCodeLengths(const Byte *codeLengths) - { - int lenCounts[kNumBitsMax + 1]; - UInt32 tmpPositions[kNumBitsMax + 1]; - int i; - for(i = 1; i <= kNumBitsMax; i++) - lenCounts[i] = 0; - UInt32 symbol; - for (symbol = 0; symbol < m_NumSymbols; symbol++) - { - int len = codeLengths[symbol]; - if (len > kNumBitsMax) - return false; - lenCounts[len]++; - m_Symbols[symbol] = 0xFFFFFFFF; - } - lenCounts[0] = 0; - m_Positions[0] = m_Limits[0] = 0; - UInt32 startPos = 0; - UInt32 index = 0; - const UInt32 kMaxValue = (1 << kNumBitsMax); - for (i = 1; i <= kNumBitsMax; i++) - { - startPos += lenCounts[i] << (kNumBitsMax - i); - if (startPos > kMaxValue) - return false; - m_Limits[i] = (i == kNumBitsMax) ? kMaxValue : startPos; - m_Positions[i] = m_Positions[i - 1] + lenCounts[i - 1]; - tmpPositions[i] = m_Positions[i]; - if(i <= kNumTableBits) - { - UInt32 limit = (m_Limits[i] >> (kNumBitsMax - kNumTableBits)); - for (; index < limit; index++) - m_Lengths[index] = (Byte)i; - } - } - for (symbol = 0; symbol < m_NumSymbols; symbol++) - { - int len = codeLengths[symbol]; - if (len != 0) - m_Symbols[tmpPositions[len]++] = symbol; - } - return true; - } - - template <class TBitDecoder> - UInt32 DecodeSymbol(TBitDecoder *bitStream) - { - int numBits; - UInt32 value = bitStream->GetValue(kNumBitsMax); - if (value < m_Limits[kNumTableBits]) - numBits = m_Lengths[value >> (kNumBitsMax - kNumTableBits)]; - else - for (numBits = kNumTableBits + 1; value >= m_Limits[numBits]; numBits++); - bitStream->MovePos(numBits); - UInt32 index = m_Positions[numBits] + - ((value - m_Limits[numBits - 1]) >> (kNumBitsMax - numBits)); - if (index >= m_NumSymbols) - // throw CDecoderException(); // test it - return 0xFFFFFFFF; - return m_Symbols[index]; - } -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ImplodeDecoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/ImplodeDecoder.cpp deleted file mode 100644 index f84a013c2..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ImplodeDecoder.cpp +++ /dev/null @@ -1,219 +0,0 @@ -// Implode/Decoder.cpp - -#include "StdAfx.h" - -#include "ImplodeDecoder.h" -#include "Common/Defs.h" - -namespace NCompress { -namespace NImplode { -namespace NDecoder { - -class CException -{ -public: - enum ECauseType - { - kData - } m_Cause; - CException(ECauseType cause): m_Cause(cause) {} -}; - -static const int kNumDistanceLowDirectBitsForBigDict = 7; -static const int kNumDistanceLowDirectBitsForSmallDict = 6; - -static const int kNumBitsInByte = 8; - -// static const int kLevelStructuresNumberFieldSize = kNumBitsInByte; -static const int kLevelStructuresNumberAdditionalValue = 1; - -static const int kNumLevelStructureLevelBits = 4; -static const int kLevelStructureLevelAdditionalValue = 1; - -static const int kNumLevelStructureRepNumberBits = 4; -static const int kLevelStructureRepNumberAdditionalValue = 1; - - -static const int kLiteralTableSize = (1 << kNumBitsInByte); -static const int kDistanceTableSize = 64; -static const int kLengthTableSize = 64; - -static const UInt32 kHistorySize = - (1 << MyMax(kNumDistanceLowDirectBitsForBigDict, - kNumDistanceLowDirectBitsForSmallDict)) * - kDistanceTableSize; // = 8 KB; - -static const int kNumAdditionalLengthBits = 8; - -static const UInt32 kMatchMinLenWhenLiteralsOn = 3; -static const UInt32 kMatchMinLenWhenLiteralsOff = 2; - -static const UInt32 kMatchMinLenMax = MyMax(kMatchMinLenWhenLiteralsOn, - kMatchMinLenWhenLiteralsOff); // 3 - -// static const UInt32 kMatchMaxLenMax = kMatchMinLenMax + (kLengthTableSize - 1) + (1 << kNumAdditionalLengthBits) - 1; // or 2 - -enum -{ - kMatchId = 0, - kLiteralId = 1 -}; - - -CCoder::CCoder(): - m_LiteralDecoder(kLiteralTableSize), - m_LengthDecoder(kLengthTableSize), - m_DistanceDecoder(kDistanceTableSize) -{ -} - -void CCoder::ReleaseStreams() -{ - m_OutWindowStream.ReleaseStream(); - m_InBitStream.ReleaseStream(); -} - -bool CCoder::ReadLevelItems(NImplode::NHuffman::CDecoder &decoder, - Byte *levels, int numLevelItems) -{ - int numCodedStructures = m_InBitStream.ReadBits(kNumBitsInByte) + - kLevelStructuresNumberAdditionalValue; - int currentIndex = 0; - for(int i = 0; i < numCodedStructures; i++) - { - int level = m_InBitStream.ReadBits(kNumLevelStructureLevelBits) + - kLevelStructureLevelAdditionalValue; - int rep = m_InBitStream.ReadBits(kNumLevelStructureRepNumberBits) + - kLevelStructureRepNumberAdditionalValue; - if (currentIndex + rep > numLevelItems) - throw CException(CException::kData); - for(int j = 0; j < rep; j++) - levels[currentIndex++] = (Byte)level; - } - if (currentIndex != numLevelItems) - return false; - return decoder.SetCodeLengths(levels); -} - - -bool CCoder::ReadTables(void) -{ - if (m_LiteralsOn) - { - Byte literalLevels[kLiteralTableSize]; - if (!ReadLevelItems(m_LiteralDecoder, literalLevels, kLiteralTableSize)) - return false; - } - - Byte lengthLevels[kLengthTableSize]; - if (!ReadLevelItems(m_LengthDecoder, lengthLevels, kLengthTableSize)) - return false; - - Byte distanceLevels[kDistanceTableSize]; - return ReadLevelItems(m_DistanceDecoder, distanceLevels, kDistanceTableSize); -} - -class CCoderReleaser -{ - CCoder *m_Coder; -public: - CCoderReleaser(CCoder *coder): m_Coder(coder) {} - ~CCoderReleaser() { m_Coder->ReleaseStreams(); } -}; - -HRESULT CCoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - if (!m_InBitStream.Create(1 << 20)) - return E_OUTOFMEMORY; - if (!m_OutWindowStream.Create(kHistorySize)) - return E_OUTOFMEMORY; - if (outSize == NULL) - return E_INVALIDARG; - UInt64 pos = 0, unPackSize = *outSize; - - m_OutWindowStream.SetStream(outStream); - m_OutWindowStream.Init(false); - m_InBitStream.SetStream(inStream); - m_InBitStream.Init(); - CCoderReleaser coderReleaser(this); - - if (!ReadTables()) - return S_FALSE; - - while(pos < unPackSize) - { - if (progress != NULL && pos % (1 << 16) == 0) - { - UInt64 packSize = m_InBitStream.GetProcessedSize(); - RINOK(progress->SetRatioInfo(&packSize, &pos)); - } - if(m_InBitStream.ReadBits(1) == kMatchId) // match - { - UInt32 lowDistBits = m_InBitStream.ReadBits(m_NumDistanceLowDirectBits); - UInt32 distance = m_DistanceDecoder.DecodeSymbol(&m_InBitStream); - if (distance >= kDistanceTableSize) - return S_FALSE; - distance = (distance << m_NumDistanceLowDirectBits) + lowDistBits; - UInt32 lengthSymbol = m_LengthDecoder.DecodeSymbol(&m_InBitStream); - if (lengthSymbol >= kLengthTableSize) - return S_FALSE; - UInt32 length = lengthSymbol + m_MinMatchLength; - if (lengthSymbol == kLengthTableSize - 1) // special symbol = 63 - length += m_InBitStream.ReadBits(kNumAdditionalLengthBits); - while(distance >= pos && length > 0) - { - m_OutWindowStream.PutByte(0); - pos++; - length--; - } - if (length > 0) - m_OutWindowStream.CopyBlock(distance, length); - pos += length; - } - else - { - Byte b; - if (m_LiteralsOn) - { - UInt32 temp = m_LiteralDecoder.DecodeSymbol(&m_InBitStream); - if (temp >= kLiteralTableSize) - return S_FALSE; - b = (Byte)temp; - } - else - b = (Byte)m_InBitStream.ReadBits(kNumBitsInByte); - m_OutWindowStream.PutByte(b); - pos++; - } - } - if (pos > unPackSize) - return S_FALSE; - return m_OutWindowStream.Flush(); -} - -STDMETHODIMP CCoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - try { return CodeReal(inStream, outStream, inSize, outSize, progress); } - catch(const CLzOutWindowException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } -} - -STDMETHODIMP CCoder::SetDecoderProperties2(const Byte *data, UInt32 size) -{ - if (size < 1) - return E_INVALIDARG; - Byte flag = data[0]; - m_BigDictionaryOn = ((flag & 2) != 0); - m_NumDistanceLowDirectBits = m_BigDictionaryOn ? - kNumDistanceLowDirectBitsForBigDict: - kNumDistanceLowDirectBitsForSmallDict; - m_LiteralsOn = ((flag & 4) != 0); - m_MinMatchLength = m_LiteralsOn ? - kMatchMinLenWhenLiteralsOn : - kMatchMinLenWhenLiteralsOff; - return S_OK; -} - -}}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ImplodeDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/ImplodeDecoder.h deleted file mode 100644 index 7ea1553ee..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ImplodeDecoder.h +++ /dev/null @@ -1,57 +0,0 @@ -// ImplodeDecoder.h - -#ifndef __COMPRESS_IMPLODE_DECODER_H -#define __COMPRESS_IMPLODE_DECODER_H - -#include "../../Common/MyCom.h" - -#include "../ICoder.h" - -#include "ImplodeHuffmanDecoder.h" -#include "LzOutWindow.h" - -namespace NCompress { -namespace NImplode { -namespace NDecoder { - -class CCoder: - public ICompressCoder, - public ICompressSetDecoderProperties2, - public CMyUnknownImp -{ - CLzOutWindow m_OutWindowStream; - NBitl::CDecoder<CInBuffer> m_InBitStream; - - NImplode::NHuffman::CDecoder m_LiteralDecoder; - NImplode::NHuffman::CDecoder m_LengthDecoder; - NImplode::NHuffman::CDecoder m_DistanceDecoder; - - bool m_BigDictionaryOn; - bool m_LiteralsOn; - - int m_NumDistanceLowDirectBits; - UInt32 m_MinMatchLength; - - bool ReadLevelItems(NImplode::NHuffman::CDecoder &table, Byte *levels, int numLevelItems); - bool ReadTables(); - void DeCodeLevelTable(Byte *newLevels, int numLevels); -public: - CCoder(); - - MY_UNKNOWN_IMP1(ICompressSetDecoderProperties2) - - void ReleaseStreams(); - HRESULT Flush() { return m_OutWindowStream.Flush(); } - - HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); -}; - -}}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ImplodeHuffmanDecoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/ImplodeHuffmanDecoder.cpp deleted file mode 100644 index 64345e08b..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ImplodeHuffmanDecoder.cpp +++ /dev/null @@ -1,89 +0,0 @@ -// ImplodeHuffmanDecoder.cpp - -#include "StdAfx.h" - -#include "ImplodeHuffmanDecoder.h" - -namespace NCompress { -namespace NImplode { -namespace NHuffman { - -CDecoder::CDecoder(UInt32 numSymbols): - m_NumSymbols(numSymbols) -{ - m_Symbols = new UInt32[m_NumSymbols]; -} - -CDecoder::~CDecoder() -{ - delete []m_Symbols; -} - -bool CDecoder::SetCodeLengths(const Byte *codeLengths) -{ - // int lenCounts[kNumBitsInLongestCode + 1], tmpPositions[kNumBitsInLongestCode + 1]; - int lenCounts[kNumBitsInLongestCode + 2], tmpPositions[kNumBitsInLongestCode + 1]; - int i; - for(i = 0; i <= kNumBitsInLongestCode; i++) - lenCounts[i] = 0; - UInt32 symbolIndex; - for (symbolIndex = 0; symbolIndex < m_NumSymbols; symbolIndex++) - lenCounts[codeLengths[symbolIndex]]++; - // lenCounts[0] = 0; - - // tmpPositions[0] = m_Positions[0] = m_Limitits[0] = 0; - m_Limitits[kNumBitsInLongestCode + 1] = 0; - m_Positions[kNumBitsInLongestCode + 1] = 0; - lenCounts[kNumBitsInLongestCode + 1] = 0; - - - UInt32 startPos = 0; - static const UInt32 kMaxValue = (1 << kNumBitsInLongestCode); - - for (i = kNumBitsInLongestCode; i > 0; i--) - { - startPos += lenCounts[i] << (kNumBitsInLongestCode - i); - if (startPos > kMaxValue) - return false; - m_Limitits[i] = startPos; - m_Positions[i] = m_Positions[i + 1] + lenCounts[i + 1]; - tmpPositions[i] = m_Positions[i] + lenCounts[i]; - - } - - // if _ZIP_MODE do not throw exception for trees containing only one node - // #ifndef _ZIP_MODE - if (startPos != kMaxValue) - return false; - // #endif - - for (symbolIndex = 0; symbolIndex < m_NumSymbols; symbolIndex++) - if (codeLengths[symbolIndex] != 0) - m_Symbols[--tmpPositions[codeLengths[symbolIndex]]] = symbolIndex; - return true; -} - -UInt32 CDecoder::DecodeSymbol(CInBit *inStream) -{ - UInt32 numBits = 0; - UInt32 value = inStream->GetValue(kNumBitsInLongestCode); - int i; - for(i = kNumBitsInLongestCode; i > 0; i--) - { - if (value < m_Limitits[i]) - { - numBits = i; - break; - } - } - if (i == 0) - return 0xFFFFFFFF; - inStream->MovePos(numBits); - UInt32 index = m_Positions[numBits] + - ((value - m_Limitits[numBits + 1]) >> (kNumBitsInLongestCode - numBits)); - if (index >= m_NumSymbols) - return 0xFFFFFFFF; - return m_Symbols[index]; -} - -}}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ImplodeHuffmanDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/ImplodeHuffmanDecoder.h deleted file mode 100644 index 6120a35ab..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ImplodeHuffmanDecoder.h +++ /dev/null @@ -1,34 +0,0 @@ -// ImplodeHuffmanDecoder.h - -#ifndef __IMPLODE_HUFFMAN_DECODER_H -#define __IMPLODE_HUFFMAN_DECODER_H - -#include "../Common/InBuffer.h" - -#include "BitlDecoder.h" - -namespace NCompress { -namespace NImplode { -namespace NHuffman { - -const int kNumBitsInLongestCode = 16; - -typedef NBitl::CDecoder<CInBuffer> CInBit; - -class CDecoder -{ - UInt32 m_Limitits[kNumBitsInLongestCode + 2]; // m_Limitits[i] = value limit for symbols with length = i - UInt32 m_Positions[kNumBitsInLongestCode + 2]; // m_Positions[i] = index in m_Symbols[] of first symbol with length = i - UInt32 m_NumSymbols; // number of symbols in m_Symbols - UInt32 *m_Symbols; // symbols: at first with len=1 then 2, ... 15. -public: - CDecoder(UInt32 numSymbols); - ~CDecoder(); - - bool SetCodeLengths(const Byte *codeLengths); - UInt32 DecodeSymbol(CInBit *inStream); -}; - -}}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/LzOutWindow.cpp b/src/libs/7zip/win/CPP/7zip/Compress/LzOutWindow.cpp deleted file mode 100644 index df46295bd..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/LzOutWindow.cpp +++ /dev/null @@ -1,14 +0,0 @@ -// LzOutWindow.cpp - -#include "StdAfx.h" - -#include "LzOutWindow.h" - -void CLzOutWindow::Init(bool solid) -{ - if (!solid) - COutBuffer::Init(); - #ifdef _NO_EXCEPTIONS - ErrorCode = S_OK; - #endif -} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/LzOutWindow.h b/src/libs/7zip/win/CPP/7zip/Compress/LzOutWindow.h deleted file mode 100644 index d8d13c225..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/LzOutWindow.h +++ /dev/null @@ -1,66 +0,0 @@ -// LzOutWindow.h - -#ifndef __LZ_OUT_WINDOW_H -#define __LZ_OUT_WINDOW_H - -#include "../IStream.h" - -#include "../Common/OutBuffer.h" - -#ifndef _NO_EXCEPTIONS -typedef COutBufferException CLzOutWindowException; -#endif - -class CLzOutWindow: public COutBuffer -{ -public: - void Init(bool solid = false); - - // distance >= 0, len > 0, - bool CopyBlock(UInt32 distance, UInt32 len) - { - UInt32 pos = _pos - distance - 1; - if (distance >= _pos) - { - if (!_overDict || distance >= _bufferSize) - return false; - pos += _bufferSize; - } - if (_limitPos - _pos > len && _bufferSize - pos > len) - { - const Byte *src = _buffer + pos; - Byte *dest = _buffer + _pos; - _pos += len; - do - *dest++ = *src++; - while(--len != 0); - } - else do - { - if (pos == _bufferSize) - pos = 0; - _buffer[_pos++] = _buffer[pos++]; - if (_pos == _limitPos) - FlushWithCheck(); - } - while(--len != 0); - return true; - } - - void PutByte(Byte b) - { - _buffer[_pos++] = b; - if (_pos == _limitPos) - FlushWithCheck(); - } - - Byte GetByte(UInt32 distance) const - { - UInt32 pos = _pos - distance - 1; - if (pos >= _bufferSize) - pos += _bufferSize; - return _buffer[pos]; - } -}; - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/LzhDecoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/LzhDecoder.cpp deleted file mode 100644 index 6f9b5065e..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/LzhDecoder.cpp +++ /dev/null @@ -1,220 +0,0 @@ -// LzhDecoder.cpp - -#include "StdAfx.h" - -#include "LzhDecoder.h" - -#include "Windows/Defs.h" - -namespace NCompress{ -namespace NLzh { -namespace NDecoder { - -static const UInt32 kHistorySize = (1 << 16); - -static const int kBlockSizeBits = 16; -static const int kNumCBits = 9; -static const int kNumLevelBits = 5; // smallest integer such that (1 << kNumLevelBits) > kNumLevelSymbols/ - -UInt32 CCoder::ReadBits(int numBits) { return m_InBitStream.ReadBits(numBits); } - -HRESULT CCoder::ReadLevelTable() -{ - int n = ReadBits(kNumLevelBits); - if (n == 0) - { - m_LevelHuffman.Symbol = ReadBits(kNumLevelBits); - if (m_LevelHuffman.Symbol >= kNumLevelSymbols) - return S_FALSE; - } - else - { - if (n > kNumLevelSymbols) - return S_FALSE; - m_LevelHuffman.Symbol = -1; - Byte lens[kNumLevelSymbols]; - int i = 0; - while (i < n) - { - int c = m_InBitStream.ReadBits(3); - if (c == 7) - while (ReadBits(1)) - if (c++ > kMaxHuffmanLen) - return S_FALSE; - lens[i++] = (Byte)c; - if (i == kNumSpecLevelSymbols) - { - c = ReadBits(2); - while (--c >= 0) - lens[i++] = 0; - } - } - while (i < kNumLevelSymbols) - lens[i++] = 0; - m_LevelHuffman.SetCodeLengths(lens); - } - return S_OK; -} - -HRESULT CCoder::ReadPTable(int numBits) -{ - int n = ReadBits(numBits); - if (n == 0) - { - m_PHuffmanDecoder.Symbol = ReadBits(numBits); - if (m_PHuffmanDecoder.Symbol >= kNumDistanceSymbols) - return S_FALSE; - } - else - { - if (n > kNumDistanceSymbols) - return S_FALSE; - m_PHuffmanDecoder.Symbol = -1; - Byte lens[kNumDistanceSymbols]; - int i = 0; - while (i < n) - { - int c = m_InBitStream.ReadBits(3); - if (c == 7) - while (ReadBits(1)) - { - if (c > kMaxHuffmanLen) - return S_FALSE; - c++; - } - lens[i++] = (Byte)c; - } - while (i < kNumDistanceSymbols) - lens[i++] = 0; - m_PHuffmanDecoder.SetCodeLengths(lens); - } - return S_OK; -} - -HRESULT CCoder::ReadCTable() -{ - int n = ReadBits(kNumCBits); - if (n == 0) - { - m_CHuffmanDecoder.Symbol = ReadBits(kNumCBits); - if (m_CHuffmanDecoder.Symbol >= kNumCSymbols) - return S_FALSE; - } - else - { - if (n > kNumCSymbols) - return S_FALSE; - m_CHuffmanDecoder.Symbol = -1; - Byte lens[kNumCSymbols]; - int i = 0; - while (i < n) - { - int c = m_LevelHuffman.Decode(&m_InBitStream); - if (c < kNumSpecLevelSymbols) - { - if (c == 0) - c = 1; - else if (c == 1) - c = ReadBits(4) + 3; - else - c = ReadBits(kNumCBits) + 20; - while (--c >= 0) - { - if (i > kNumCSymbols) - return S_FALSE; - lens[i++] = 0; - } - } - else - lens[i++] = (Byte)(c - 2); - } - while (i < kNumCSymbols) - lens[i++] = 0; - m_CHuffmanDecoder.SetCodeLengths(lens); - } - return S_OK; -} - -STDMETHODIMP CCoder::CodeReal(ISequentialInStream *inStream, - ISequentialOutStream *outStream, const UInt64 * /* inSize */, const UInt64 *outSize, - ICompressProgressInfo *progress) -{ - if (outSize == NULL) - return E_INVALIDARG; - - if (!m_OutWindowStream.Create(kHistorySize)) - return E_OUTOFMEMORY; - if (!m_InBitStream.Create(1 << 20)) - return E_OUTOFMEMORY; - - UInt64 pos = 0; - m_OutWindowStream.SetStream(outStream); - m_OutWindowStream.Init(false); - m_InBitStream.SetStream(inStream); - m_InBitStream.Init(); - - CCoderReleaser coderReleaser(this); - - int pbit; - if (m_NumDictBits <= 13) - pbit = 4; - else - pbit = 5; - - UInt32 blockSize = 0; - - while(pos < *outSize) - { - // for (i = 0; i < dictSize; i++) dtext[i] = 0x20; - - if (blockSize == 0) - { - if (progress != NULL) - { - UInt64 packSize = m_InBitStream.GetProcessedSize(); - RINOK(progress->SetRatioInfo(&packSize, &pos)); - } - blockSize = ReadBits(kBlockSizeBits); - ReadLevelTable(); - ReadCTable(); - RINOK(ReadPTable(pbit)); - } - blockSize--; - UInt32 c = m_CHuffmanDecoder.Decode(&m_InBitStream); - if (c < 256) - { - m_OutWindowStream.PutByte((Byte)c); - pos++; - } - else if (c >= kNumCSymbols) - return S_FALSE; - else - { - // offset = (interface->method == LARC_METHOD_NUM) ? 0x100 - 2 : 0x100 - 3; - UInt32 len = c - 256 + kMinMatch; - UInt32 distance = m_PHuffmanDecoder.Decode(&m_InBitStream); - if (distance != 0) - distance = (1 << (distance - 1)) + ReadBits(distance - 1); - if (distance >= pos) - return S_FALSE; - if (pos + len > *outSize) - len = (UInt32)(*outSize - pos); - pos += len; - m_OutWindowStream.CopyBlock(distance, len); - } - } - coderReleaser.NeedFlush = false; - return m_OutWindowStream.Flush(); -} - -STDMETHODIMP CCoder::Code(ISequentialInStream *inStream, - ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, - ICompressProgressInfo *progress) -{ - try { return CodeReal(inStream, outStream, inSize, outSize, progress);} - catch(const CInBufferException &e) { return e.ErrorCode; } - catch(const CLzOutWindowException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } -} - -}}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/LzhDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/LzhDecoder.h deleted file mode 100644 index ed631e20d..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/LzhDecoder.h +++ /dev/null @@ -1,106 +0,0 @@ -// LzhDecoder.h - -#ifndef __COMPRESS_LZH_DECODER_H -#define __COMPRESS_LZH_DECODER_H - -#include "../../Common/MyCom.h" - -#include "../ICoder.h" - -#include "../Common/InBuffer.h" - -#include "BitmDecoder.h" -#include "HuffmanDecoder.h" -#include "LzOutWindow.h" - -namespace NCompress { -namespace NLzh { -namespace NDecoder { - -const int kMaxHuffmanLen = 16; // Check it - -const int kNumSpecLevelSymbols = 3; -const int kNumLevelSymbols = kNumSpecLevelSymbols + kMaxHuffmanLen; - -const int kDictBitsMax = 16; -const int kNumDistanceSymbols = kDictBitsMax + 1; - -const int kMaxMatch = 256; -const int kMinMatch = 3; -const int kNumCSymbols = 256 + kMaxMatch + 2 - kMinMatch; - -template <UInt32 m_NumSymbols> -class CHuffmanDecoder:public NCompress::NHuffman::CDecoder<kMaxHuffmanLen, m_NumSymbols> -{ -public: - int Symbol; - template <class TBitDecoder> - UInt32 Decode(TBitDecoder *bitStream) - { - if (Symbol >= 0) - return (UInt32)Symbol; - return DecodeSymbol(bitStream); - } -}; - -class CCoder : - public ICompressCoder, - public CMyUnknownImp -{ - CLzOutWindow m_OutWindowStream; - NBitm::CDecoder<CInBuffer> m_InBitStream; - - int m_NumDictBits; - - CHuffmanDecoder<kNumLevelSymbols> m_LevelHuffman; - CHuffmanDecoder<kNumDistanceSymbols> m_PHuffmanDecoder; - CHuffmanDecoder<kNumCSymbols> m_CHuffmanDecoder; - - void ReleaseStreams() - { - m_OutWindowStream.ReleaseStream(); - m_InBitStream.ReleaseStream(); - } - - class CCoderReleaser - { - CCoder *m_Coder; - public: - bool NeedFlush; - CCoderReleaser(CCoder *coder): m_Coder(coder), NeedFlush(true) {} - ~CCoderReleaser() - { - if (NeedFlush) - m_Coder->m_OutWindowStream.Flush(); - m_Coder->ReleaseStreams(); - } - }; - friend class CCoderReleaser; - - void MakeTable(int nchar, Byte *bitlen, int tablebits, - UInt32 *table, int tablesize); - - UInt32 ReadBits(int numBits); - HRESULT ReadLevelTable(); - HRESULT ReadPTable(int numBits); - HRESULT ReadCTable(); - -public: - - MY_UNKNOWN_IMP - - STDMETHOD(CodeReal)(ISequentialInStream *inStream, - ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, - ICompressProgressInfo *progress); - - STDMETHOD(Code)(ISequentialInStream *inStream, - ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, - ICompressProgressInfo *progress); - - void SetDictionary(int numDictBits) { m_NumDictBits = numDictBits; } - CCoder(): m_NumDictBits(0) {} -}; - -}}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Lzx.h b/src/libs/7zip/win/CPP/7zip/Compress/Lzx.h deleted file mode 100644 index 09ab7f075..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Lzx.h +++ /dev/null @@ -1,61 +0,0 @@ -// Lzx.h - -#ifndef __COMPRESS_LZX_H -#define __COMPRESS_LZX_H - -namespace NCompress { -namespace NLzx { - -const unsigned kNumHuffmanBits = 16; -const UInt32 kNumRepDistances = 3; - -const UInt32 kNumLenSlots = 8; -const UInt32 kMatchMinLen = 2; -const UInt32 kNumLenSymbols = 249; -const UInt32 kMatchMaxLen = kMatchMinLen + (kNumLenSlots - 1) + kNumLenSymbols - 1; - -const unsigned kNumAlignBits = 3; -const UInt32 kAlignTableSize = 1 << kNumAlignBits; - -const UInt32 kNumPosSlots = 50; -const UInt32 kNumPosLenSlots = kNumPosSlots * kNumLenSlots; - -const UInt32 kMainTableSize = 256 + kNumPosLenSlots; -const UInt32 kLevelTableSize = 20; -const UInt32 kMaxTableSize = kMainTableSize; - -const unsigned kNumBlockTypeBits = 3; -const unsigned kBlockTypeVerbatim = 1; -const unsigned kBlockTypeAligned = 2; -const unsigned kBlockTypeUncompressed = 3; - -const unsigned kUncompressedBlockSizeNumBits = 24; - -const unsigned kNumBitsForPreTreeLevel = 4; - -const unsigned kLevelSymbolZeros = 17; -const unsigned kLevelSymbolZerosBig = 18; -const unsigned kLevelSymbolSame = 19; - -const unsigned kLevelSymbolZerosStartValue = 4; -const unsigned kLevelSymbolZerosNumBits = 4; - -const unsigned kLevelSymbolZerosBigStartValue = kLevelSymbolZerosStartValue + - (1 << kLevelSymbolZerosNumBits); -const unsigned kLevelSymbolZerosBigNumBits = 5; - -const unsigned kLevelSymbolSameNumBits = 1; -const unsigned kLevelSymbolSameStartValue = 4; - -const unsigned kNumBitsForAlignLevel = 3; - -const unsigned kNumDictionaryBitsMin = 15; -const unsigned kNumDictionaryBitsMax = 21; -const UInt32 kDictionarySizeMax = (1 << kNumDictionaryBitsMax); - -const unsigned kNumLinearPosSlotBits = 17; -const UInt32 kNumPowerPosSlots = 0x26; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Lzx86Converter.cpp b/src/libs/7zip/win/CPP/7zip/Compress/Lzx86Converter.cpp deleted file mode 100644 index 9e53f18a9..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Lzx86Converter.cpp +++ /dev/null @@ -1,90 +0,0 @@ -// Lzx86Converter.cpp - -#include "StdAfx.h" - -#include "../../Common/Defs.h" - -#include "Lzx86Converter.h" - -namespace NCompress { -namespace NLzx { - -static const int kResidue = 6 + 4; - -void Cx86ConvertOutStream::MakeTranslation() -{ - if (m_Pos <= kResidue) - return; - UInt32 numBytes = m_Pos - kResidue; - Byte *buffer = m_Buffer; - for (UInt32 i = 0; i < numBytes;) - { - if (buffer[i++] == 0xE8) - { - Int32 absValue = 0; - int j; - for(j = 0; j < 4; j++) - absValue += (UInt32)buffer[i + j] << (j * 8); - Int32 pos = (Int32)(m_ProcessedSize + i - 1); - if (absValue >= -pos && absValue < (Int32)m_TranslationSize) - { - UInt32 offset = (absValue >= 0) ? - absValue - pos : - absValue + m_TranslationSize; - for(j = 0; j < 4; j++) - { - buffer[i + j] = (Byte)(offset & 0xFF); - offset >>= 8; - } - } - i += 4; - } - } -} - -STDMETHODIMP Cx86ConvertOutStream::Write(const void *data, UInt32 size, UInt32 *processedSize) -{ - if (processedSize != NULL) - *processedSize = 0; - if (!m_TranslationMode) - return m_Stream->Write(data, size, processedSize); - UInt32 realProcessedSize = 0; - while (realProcessedSize < size) - { - UInt32 writeSize = MyMin(size - realProcessedSize, kUncompressedBlockSize - m_Pos); - memmove(m_Buffer + m_Pos, (const Byte *)data + realProcessedSize, writeSize); - m_Pos += writeSize; - realProcessedSize += writeSize; - if (m_Pos == kUncompressedBlockSize) - { - RINOK(Flush()); - } - } - if (processedSize != NULL) - *processedSize = realProcessedSize; - return S_OK; -} - -HRESULT Cx86ConvertOutStream::Flush() -{ - if (m_Pos == 0) - return S_OK; - if (m_TranslationMode) - MakeTranslation(); - UInt32 pos = 0; - do - { - UInt32 processed; - RINOK(m_Stream->Write(m_Buffer + pos, m_Pos - pos, &processed)); - if (processed == 0) - return E_FAIL; - pos += processed; - } - while(pos < m_Pos); - m_ProcessedSize += m_Pos; - m_Pos = 0; - m_TranslationMode = (m_TranslationMode && (m_ProcessedSize < (1 << 30))); - return S_OK; -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Lzx86Converter.h b/src/libs/7zip/win/CPP/7zip/Compress/Lzx86Converter.h deleted file mode 100644 index 9f110c29b..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Lzx86Converter.h +++ /dev/null @@ -1,46 +0,0 @@ -// Lzx86Converter.h - -#ifndef __LZX_86_CONVERTER_H -#define __LZX_86_CONVERTER_H - -#include "../../Common/MyCom.h" - -#include "../IStream.h" - -namespace NCompress { -namespace NLzx { - -const int kUncompressedBlockSize = 1 << 15; - -class Cx86ConvertOutStream: - public ISequentialOutStream, - public CMyUnknownImp -{ - CMyComPtr<ISequentialOutStream> m_Stream; - UInt32 m_ProcessedSize; - UInt32 m_Pos; - UInt32 m_TranslationSize; - bool m_TranslationMode; - Byte m_Buffer[kUncompressedBlockSize]; - - void MakeTranslation(); -public: - void SetStream(ISequentialOutStream *outStream) { m_Stream = outStream; } - void ReleaseStream() { m_Stream.Release(); } - void Init(bool translationMode, UInt32 translationSize) - { - m_TranslationMode = translationMode; - m_TranslationSize = translationSize; - m_ProcessedSize = 0; - m_Pos = 0; - } - HRESULT Flush(); - - MY_UNKNOWN_IMP - - STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/LzxDecoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/LzxDecoder.cpp deleted file mode 100644 index d1027f1f3..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/LzxDecoder.cpp +++ /dev/null @@ -1,387 +0,0 @@ -// LzxDecoder.cpp - -#include "StdAfx.h" - -#include "../../Common/Defs.h" - -#include "LzxDecoder.h" - -namespace NCompress { -namespace NLzx { - -const int kLenIdNeedInit = -2; - -CDecoder::CDecoder(bool wimMode): - _keepHistory(false), - _skipByte(false), - _wimMode(wimMode) -{ - m_x86ConvertOutStreamSpec = new Cx86ConvertOutStream; - m_x86ConvertOutStream = m_x86ConvertOutStreamSpec; -} - -void CDecoder::ReleaseStreams() -{ - m_OutWindowStream.ReleaseStream(); - m_InBitStream.ReleaseStream(); - m_x86ConvertOutStreamSpec->ReleaseStream(); -} - -STDMETHODIMP CDecoder::Flush() -{ - RINOK(m_OutWindowStream.Flush()); - return m_x86ConvertOutStreamSpec->Flush(); -} - -UInt32 CDecoder::ReadBits(unsigned numBits) { return m_InBitStream.ReadBits(numBits); } - -#define RIF(x) { if (!(x)) return false; } - -bool CDecoder::ReadTable(Byte *lastLevels, Byte *newLevels, UInt32 numSymbols) -{ - Byte levelLevels[kLevelTableSize]; - UInt32 i; - for (i = 0; i < kLevelTableSize; i++) - levelLevels[i] = (Byte)ReadBits(kNumBitsForPreTreeLevel); - RIF(m_LevelDecoder.SetCodeLengths(levelLevels)); - unsigned num = 0; - Byte symbol = 0; - for (i = 0; i < numSymbols;) - { - if (num != 0) - { - lastLevels[i] = newLevels[i] = symbol; - i++; - num--; - continue; - } - UInt32 number = m_LevelDecoder.DecodeSymbol(&m_InBitStream); - if (number == kLevelSymbolZeros) - { - num = kLevelSymbolZerosStartValue + (unsigned)ReadBits(kLevelSymbolZerosNumBits); - symbol = 0; - } - else if (number == kLevelSymbolZerosBig) - { - num = kLevelSymbolZerosBigStartValue + (unsigned)ReadBits(kLevelSymbolZerosBigNumBits); - symbol = 0; - } - else if (number == kLevelSymbolSame || number <= kNumHuffmanBits) - { - if (number <= kNumHuffmanBits) - num = 1; - else - { - num = kLevelSymbolSameStartValue + (unsigned)ReadBits(kLevelSymbolSameNumBits); - number = m_LevelDecoder.DecodeSymbol(&m_InBitStream); - if (number > kNumHuffmanBits) - return false; - } - symbol = Byte((17 + lastLevels[i] - number) % (kNumHuffmanBits + 1)); - } - else - return false; - } - return true; -} - -bool CDecoder::ReadTables(void) -{ - Byte newLevels[kMaxTableSize]; - { - if (_skipByte) - m_InBitStream.DirectReadByte(); - m_InBitStream.Normalize(); - - unsigned blockType = (unsigned)ReadBits(kNumBlockTypeBits); - if (blockType > kBlockTypeUncompressed) - return false; - if (_wimMode) - if (ReadBits(1) == 1) - m_UnCompressedBlockSize = (1 << 15); - else - m_UnCompressedBlockSize = ReadBits(16); - else - m_UnCompressedBlockSize = m_InBitStream.ReadBitsBig(kUncompressedBlockSizeNumBits); - - m_IsUncompressedBlock = (blockType == kBlockTypeUncompressed); - - _skipByte = (m_IsUncompressedBlock && ((m_UnCompressedBlockSize & 1) != 0)); - - if (m_IsUncompressedBlock) - { - ReadBits(16 - m_InBitStream.GetBitPosition()); - if (!m_InBitStream.ReadUInt32(m_RepDistances[0])) - return false; - m_RepDistances[0]--; - for (unsigned i = 1; i < kNumRepDistances; i++) - { - UInt32 rep = 0; - for (unsigned j = 0; j < 4; j++) - rep |= (UInt32)m_InBitStream.DirectReadByte() << (8 * j); - m_RepDistances[i] = rep - 1; - } - return true; - } - m_AlignIsUsed = (blockType == kBlockTypeAligned); - if (m_AlignIsUsed) - { - for (unsigned i = 0; i < kAlignTableSize; i++) - newLevels[i] = (Byte)ReadBits(kNumBitsForAlignLevel); - RIF(m_AlignDecoder.SetCodeLengths(newLevels)); - } - } - - RIF(ReadTable(m_LastMainLevels, newLevels, 256)); - RIF(ReadTable(m_LastMainLevels + 256, newLevels + 256, m_NumPosLenSlots)); - for (UInt32 i = 256 + m_NumPosLenSlots; i < kMainTableSize; i++) - newLevels[i] = 0; - RIF(m_MainDecoder.SetCodeLengths(newLevels)); - - RIF(ReadTable(m_LastLenLevels, newLevels, kNumLenSymbols)); - return m_LenDecoder.SetCodeLengths(newLevels); -} - -class CDecoderFlusher -{ - CDecoder *m_Decoder; -public: - bool NeedFlush; - CDecoderFlusher(CDecoder *decoder): m_Decoder(decoder), NeedFlush(true) {} - ~CDecoderFlusher() - { - if (NeedFlush) - m_Decoder->Flush(); - m_Decoder->ReleaseStreams(); - } -}; - - -void CDecoder::ClearPrevLevels() -{ - unsigned i; - for (i = 0; i < kMainTableSize; i++) - m_LastMainLevels[i] = 0; - for (i = 0; i < kNumLenSymbols; i++) - m_LastLenLevels[i] = 0; -} - - -HRESULT CDecoder::CodeSpec(UInt32 curSize) -{ - if (_remainLen == kLenIdNeedInit) - { - _remainLen = 0; - m_InBitStream.Init(); - if (!_keepHistory || !m_IsUncompressedBlock) - m_InBitStream.Normalize(); - if (!_keepHistory) - { - _skipByte = false; - m_UnCompressedBlockSize = 0; - ClearPrevLevels(); - UInt32 i86TranslationSize = 12000000; - bool translationMode = true; - if (!_wimMode) - { - translationMode = (ReadBits(1) != 0); - if (translationMode) - { - i86TranslationSize = ReadBits(16) << 16; - i86TranslationSize |= ReadBits(16); - } - } - m_x86ConvertOutStreamSpec->Init(translationMode, i86TranslationSize); - - for (unsigned i = 0 ; i < kNumRepDistances; i++) - m_RepDistances[i] = 0; - } - } - - while (_remainLen > 0 && curSize > 0) - { - m_OutWindowStream.PutByte(m_OutWindowStream.GetByte(m_RepDistances[0])); - _remainLen--; - curSize--; - } - - while (curSize > 0) - { - if (m_UnCompressedBlockSize == 0) - if (!ReadTables()) - return S_FALSE; - UInt32 next = (Int32)MyMin(m_UnCompressedBlockSize, curSize); - curSize -= next; - m_UnCompressedBlockSize -= next; - if (m_IsUncompressedBlock) - { - while (next > 0) - { - m_OutWindowStream.PutByte(m_InBitStream.DirectReadByte()); - next--; - } - } - else while (next > 0) - { - UInt32 number = m_MainDecoder.DecodeSymbol(&m_InBitStream); - if (number < 256) - { - m_OutWindowStream.PutByte((Byte)number); - next--; - } - else - { - UInt32 posLenSlot = number - 256; - if (posLenSlot >= m_NumPosLenSlots) - return S_FALSE; - UInt32 posSlot = posLenSlot / kNumLenSlots; - UInt32 lenSlot = posLenSlot % kNumLenSlots; - UInt32 len = kMatchMinLen + lenSlot; - if (lenSlot == kNumLenSlots - 1) - { - UInt32 lenTemp = m_LenDecoder.DecodeSymbol(&m_InBitStream); - if (lenTemp >= kNumLenSymbols) - return S_FALSE; - len += lenTemp; - } - - if (posSlot < kNumRepDistances) - { - UInt32 distance = m_RepDistances[posSlot]; - m_RepDistances[posSlot] = m_RepDistances[0]; - m_RepDistances[0] = distance; - } - else - { - UInt32 distance; - unsigned numDirectBits; - if (posSlot < kNumPowerPosSlots) - { - numDirectBits = (unsigned)(posSlot >> 1) - 1; - distance = ((2 | (posSlot & 1)) << numDirectBits); - } - else - { - numDirectBits = kNumLinearPosSlotBits; - distance = ((posSlot - 0x22) << kNumLinearPosSlotBits); - } - - if (m_AlignIsUsed && numDirectBits >= kNumAlignBits) - { - distance += (m_InBitStream.ReadBits(numDirectBits - kNumAlignBits) << kNumAlignBits); - UInt32 alignTemp = m_AlignDecoder.DecodeSymbol(&m_InBitStream); - if (alignTemp >= kAlignTableSize) - return S_FALSE; - distance += alignTemp; - } - else - distance += m_InBitStream.ReadBits(numDirectBits); - m_RepDistances[2] = m_RepDistances[1]; - m_RepDistances[1] = m_RepDistances[0]; - m_RepDistances[0] = distance - kNumRepDistances; - } - - UInt32 locLen = len; - if (locLen > next) - locLen = next; - - if (!m_OutWindowStream.CopyBlock(m_RepDistances[0], locLen)) - return S_FALSE; - - len -= locLen; - next -= locLen; - if (len != 0) - { - _remainLen = (int)len; - return S_OK; - } - } - } - } - return S_OK; -} - -HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - if (outSize == NULL) - return E_INVALIDARG; - UInt64 size = *outSize; - - RINOK(SetInStream(inStream)); - m_x86ConvertOutStreamSpec->SetStream(outStream); - m_OutWindowStream.SetStream(m_x86ConvertOutStream); - RINOK(SetOutStreamSize(outSize)); - - CDecoderFlusher flusher(this); - - const UInt64 start = m_OutWindowStream.GetProcessedSize(); - for (;;) - { - UInt32 curSize = 1 << 18; - UInt64 rem = size - (m_OutWindowStream.GetProcessedSize() - start); - if (curSize > rem) - curSize = (UInt32)rem; - if (curSize == 0) - break; - RINOK(CodeSpec(curSize)); - if (progress != NULL) - { - UInt64 inSize = m_InBitStream.GetProcessedSize(); - UInt64 nowPos64 = m_OutWindowStream.GetProcessedSize() - start; - RINOK(progress->SetRatioInfo(&inSize, &nowPos64)); - } - } - flusher.NeedFlush = false; - return Flush(); -} - -HRESULT CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - try { return CodeReal(inStream, outStream, inSize, outSize, progress); } - catch(const CLzOutWindowException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } -} - -STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream) -{ - m_InBitStream.SetStream(inStream); - return S_OK; -} - -STDMETHODIMP CDecoder::ReleaseInStream() -{ - m_InBitStream.ReleaseStream(); - return S_OK; -} - -STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize) -{ - if (outSize == NULL) - return E_FAIL; - _remainLen = kLenIdNeedInit; - m_OutWindowStream.Init(_keepHistory); - return S_OK; -} - -HRESULT CDecoder::SetParams(unsigned numDictBits) -{ - if (numDictBits < kNumDictionaryBitsMin || numDictBits > kNumDictionaryBitsMax) - return E_INVALIDARG; - UInt32 numPosSlots; - if (numDictBits < 20) - numPosSlots = 30 + (numDictBits - 15) * 2; - else if (numDictBits == 20) - numPosSlots = 42; - else - numPosSlots = 50; - m_NumPosLenSlots = numPosSlots * kNumLenSlots; - if (!m_OutWindowStream.Create(kDictionarySizeMax)) - return E_OUTOFMEMORY; - if (!m_InBitStream.Create(1 << 16)) - return E_OUTOFMEMORY; - return S_OK; -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/LzxDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/LzxDecoder.h deleted file mode 100644 index 73a050619..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/LzxDecoder.h +++ /dev/null @@ -1,159 +0,0 @@ -// LzxDecoder.h - -#ifndef __LZX_DECODER_H -#define __LZX_DECODER_H - -#include "../ICoder.h" - -#include "../Common/InBuffer.h" - -#include "HuffmanDecoder.h" -#include "LzOutWindow.h" -#include "Lzx.h" -#include "Lzx86Converter.h" - -namespace NCompress { -namespace NLzx { - -namespace NBitStream { - -const unsigned kNumBigValueBits = 8 * 4; -const unsigned kNumValueBits = 17; -const UInt32 kBitDecoderValueMask = (1 << kNumValueBits) - 1; - -class CDecoder -{ - CInBuffer m_Stream; - UInt32 m_Value; - unsigned m_BitPos; -public: - CDecoder() {} - bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } - - void SetStream(ISequentialInStream *s) { m_Stream.SetStream(s); } - void ReleaseStream() { m_Stream.ReleaseStream(); } - - void Init() - { - m_Stream.Init(); - m_BitPos = kNumBigValueBits; - } - - UInt64 GetProcessedSize() const { return m_Stream.GetProcessedSize() - (kNumBigValueBits - m_BitPos) / 8; } - - unsigned GetBitPosition() const { return m_BitPos & 0xF; } - - void Normalize() - { - for (; m_BitPos >= 16; m_BitPos -= 16) - { - Byte b0 = m_Stream.ReadByte(); - Byte b1 = m_Stream.ReadByte(); - m_Value = (m_Value << 8) | b1; - m_Value = (m_Value << 8) | b0; - } - } - - UInt32 GetValue(unsigned numBits) const - { - return ((m_Value >> ((32 - kNumValueBits) - m_BitPos)) & kBitDecoderValueMask) >> (kNumValueBits - numBits); - } - - void MovePos(unsigned numBits) - { - m_BitPos += numBits; - Normalize(); - } - - UInt32 ReadBits(unsigned numBits) - { - UInt32 res = GetValue(numBits); - MovePos(numBits); - return res; - } - - UInt32 ReadBitsBig(unsigned numBits) - { - unsigned numBits0 = numBits / 2; - unsigned numBits1 = numBits - numBits0; - UInt32 res = ReadBits(numBits0) << numBits1; - return res + ReadBits(numBits1); - } - - bool ReadUInt32(UInt32 &v) - { - if (m_BitPos != 0) - return false; - v = ((m_Value >> 16) & 0xFFFF) | ((m_Value << 16) & 0xFFFF0000); - m_BitPos = kNumBigValueBits; - return true; - } - - Byte DirectReadByte() { return m_Stream.ReadByte(); } - -}; -} - -class CDecoder : - public ICompressCoder, - public CMyUnknownImp -{ - NBitStream::CDecoder m_InBitStream; - CLzOutWindow m_OutWindowStream; - - UInt32 m_RepDistances[kNumRepDistances]; - UInt32 m_NumPosLenSlots; - - bool m_IsUncompressedBlock; - bool m_AlignIsUsed; - - NCompress::NHuffman::CDecoder<kNumHuffmanBits, kMainTableSize> m_MainDecoder; - NCompress::NHuffman::CDecoder<kNumHuffmanBits, kNumLenSymbols> m_LenDecoder; - NCompress::NHuffman::CDecoder<kNumHuffmanBits, kAlignTableSize> m_AlignDecoder; - NCompress::NHuffman::CDecoder<kNumHuffmanBits, kLevelTableSize> m_LevelDecoder; - - Byte m_LastMainLevels[kMainTableSize]; - Byte m_LastLenLevels[kNumLenSymbols]; - - Cx86ConvertOutStream *m_x86ConvertOutStreamSpec; - CMyComPtr<ISequentialOutStream> m_x86ConvertOutStream; - - UInt32 m_UnCompressedBlockSize; - - bool _keepHistory; - int _remainLen; - bool _skipByte; - - bool _wimMode; - - UInt32 ReadBits(unsigned numBits); - bool ReadTable(Byte *lastLevels, Byte *newLevels, UInt32 numSymbols); - bool ReadTables(); - void ClearPrevLevels(); - - HRESULT CodeSpec(UInt32 size); - - HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); -public: - CDecoder(bool wimMode = false); - - MY_UNKNOWN_IMP - - void ReleaseStreams(); - STDMETHOD(Flush)(); - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(SetInStream)(ISequentialInStream *inStream); - STDMETHOD(ReleaseInStream)(); - STDMETHOD(SetOutStreamSize)(const UInt64 *outSize); - - HRESULT SetParams(unsigned numDictBits); - void SetKeepHistory(bool keepHistory) { _keepHistory = keepHistory; } -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Mtf8.h b/src/libs/7zip/win/CPP/7zip/Compress/Mtf8.h deleted file mode 100644 index d15dd4a56..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Mtf8.h +++ /dev/null @@ -1,193 +0,0 @@ -// Mtf8.h - -#ifndef __COMPRESS_MTF8_H -#define __COMPRESS_MTF8_H - -#include "../../../C/CpuArch.h" - -#include "../../Common/Types.h" - -namespace NCompress { - -struct CMtf8Encoder -{ - Byte Buf[256]; - - int FindAndMove(Byte v) - { - int pos; - for (pos = 0; Buf[pos] != v; pos++); - int resPos = pos; - for (; pos >= 8; pos -= 8) - { - Buf[pos] = Buf[pos - 1]; - Buf[pos - 1] = Buf[pos - 2]; - Buf[pos - 2] = Buf[pos - 3]; - Buf[pos - 3] = Buf[pos - 4]; - Buf[pos - 4] = Buf[pos - 5]; - Buf[pos - 5] = Buf[pos - 6]; - Buf[pos - 6] = Buf[pos - 7]; - Buf[pos - 7] = Buf[pos - 8]; - } - for (; pos > 0; pos--) - Buf[pos] = Buf[pos - 1]; - Buf[0] = v; - return resPos; - } -}; - -/* -struct CMtf8Decoder -{ - Byte Buf[256]; - - void Init(int) {}; - Byte GetHead() const { return Buf[0]; } - Byte GetAndMove(int pos) - { - Byte res = Buf[pos]; - for (; pos >= 8; pos -= 8) - { - Buf[pos] = Buf[pos - 1]; - Buf[pos - 1] = Buf[pos - 2]; - Buf[pos - 2] = Buf[pos - 3]; - Buf[pos - 3] = Buf[pos - 4]; - Buf[pos - 4] = Buf[pos - 5]; - Buf[pos - 5] = Buf[pos - 6]; - Buf[pos - 6] = Buf[pos - 7]; - Buf[pos - 7] = Buf[pos - 8]; - } - for (; pos > 0; pos--) - Buf[pos] = Buf[pos - 1]; - Buf[0] = res; - return res; - } -}; -*/ - -#ifdef MY_CPU_64BIT -typedef UInt64 CMtfVar; -#define MTF_MOVS 3 -#else -typedef UInt32 CMtfVar; -#define MTF_MOVS 2 -#endif - -#define MTF_MASK ((1 << MTF_MOVS) - 1) - - -struct CMtf8Decoder -{ - CMtfVar Buf[256 >> MTF_MOVS]; - - void StartInit() { memset(Buf, 0, sizeof(Buf)); } - void Add(unsigned int pos, Byte val) { Buf[pos >> MTF_MOVS] |= ((CMtfVar)val << ((pos & MTF_MASK) << 3)); } - Byte GetHead() const { return (Byte)Buf[0]; } - Byte GetAndMove(unsigned int pos) - { - UInt32 lim = ((UInt32)pos >> MTF_MOVS); - pos = (pos & MTF_MASK) << 3; - CMtfVar prev = (Buf[lim] >> pos) & 0xFF; - - UInt32 i = 0; - if ((lim & 1) != 0) - { - CMtfVar next = Buf[0]; - Buf[0] = (next << 8) | prev; - prev = (next >> (MTF_MASK << 3)); - i = 1; - lim -= 1; - } - for (; i < lim; i += 2) - { - CMtfVar n0 = Buf[i]; - CMtfVar n1 = Buf[i + 1]; - Buf[i ] = (n0 << 8) | prev; - Buf[i + 1] = (n1 << 8) | (n0 >> (MTF_MASK << 3)); - prev = (n1 >> (MTF_MASK << 3)); - } - CMtfVar next = Buf[i]; - CMtfVar mask = (((CMtfVar)0x100 << pos) - 1); - Buf[i] = (next & ~mask) | (((next << 8) | prev) & mask); - return (Byte)Buf[0]; - } -}; - -/* -const int kSmallSize = 64; -class CMtf8Decoder -{ - Byte SmallBuffer[kSmallSize]; - int SmallSize; - Byte Counts[16]; - int Size; -public: - Byte Buf[256]; - - Byte GetHead() const - { - if (SmallSize > 0) - return SmallBuffer[kSmallSize - SmallSize]; - return Buf[0]; - } - - void Init(int size) - { - Size = size; - SmallSize = 0; - for (int i = 0; i < 16; i++) - { - Counts[i] = ((size >= 16) ? 16 : size); - size -= Counts[i]; - } - } - - Byte GetAndMove(int pos) - { - if (pos < SmallSize) - { - Byte *p = SmallBuffer + kSmallSize - SmallSize; - Byte res = p[pos]; - for (; pos > 0; pos--) - p[pos] = p[pos - 1]; - SmallBuffer[kSmallSize - SmallSize] = res; - return res; - } - if (SmallSize == kSmallSize) - { - int i = Size - 1; - int g = 16; - do - { - g--; - int offset = (g << 4); - for (int t = Counts[g] - 1; t >= 0; t--, i--) - Buf[i] = Buf[offset + t]; - } - while(g != 0); - - for (i = kSmallSize - 1; i >= 0; i--) - Buf[i] = SmallBuffer[i]; - Init(Size); - } - pos -= SmallSize; - int g; - for (g = 0; pos >= Counts[g]; g++) - pos -= Counts[g]; - int offset = (g << 4); - Byte res = Buf[offset + pos]; - for (pos; pos < 16 - 1; pos++) - Buf[offset + pos] = Buf[offset + pos + 1]; - - SmallSize++; - SmallBuffer[kSmallSize - SmallSize] = res; - - Counts[g]--; - return res; - } -}; -*/ - -} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/PpmdDecoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/PpmdDecoder.cpp deleted file mode 100644 index c02f44f16..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/PpmdDecoder.cpp +++ /dev/null @@ -1,167 +0,0 @@ -// PpmdDecoder.cpp -// 2009-03-11 : Igor Pavlov : Public domain - -#include "StdAfx.h" - -#include "../../../C/Alloc.h" -#include "../../../C/CpuArch.h" - -#include "../Common/StreamUtils.h" - -#include "PpmdDecoder.h" - -namespace NCompress { -namespace NPpmd { - -static const UInt32 kBufSize = (1 << 20); - -enum -{ - kStatus_NeedInit, - kStatus_Normal, - kStatus_Finished, - kStatus_Error -}; - -static void *SzBigAlloc(void *, size_t size) { return BigAlloc(size); } -static void SzBigFree(void *, void *address) { BigFree(address); } -static ISzAlloc g_BigAlloc = { SzBigAlloc, SzBigFree }; - -CDecoder::~CDecoder() -{ - ::MidFree(_outBuf); - Ppmd7_Free(&_ppmd, &g_BigAlloc); -} - -STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *props, UInt32 size) -{ - if (size < 5) - return E_INVALIDARG; - _order = props[0]; - UInt32 memSize = GetUi32(props + 1); - if (_order < PPMD7_MIN_ORDER || - _order > PPMD7_MAX_ORDER || - memSize < PPMD7_MIN_MEM_SIZE || - memSize > PPMD7_MAX_MEM_SIZE) - return E_NOTIMPL; - if (!_inStream.Alloc(1 << 20)) - return E_OUTOFMEMORY; - if (!Ppmd7_Alloc(&_ppmd, memSize, &g_BigAlloc)) - return E_OUTOFMEMORY; - return S_OK; -} - -HRESULT CDecoder::CodeSpec(Byte *memStream, UInt32 size) -{ - switch(_status) - { - case kStatus_Finished: return S_OK; - case kStatus_Error: return S_FALSE; - case kStatus_NeedInit: - _inStream.Init(); - if (!Ppmd7z_RangeDec_Init(&_rangeDec)) - { - _status = kStatus_Error; - return S_FALSE; - } - _status = kStatus_Normal; - Ppmd7_Init(&_ppmd, _order); - break; - } - if (_outSizeDefined) - { - const UInt64 rem = _outSize - _processedSize; - if (size > rem) - size = (UInt32)rem; - } - - UInt32 i; - int sym = 0; - for (i = 0; i != size; i++) - { - sym = Ppmd7_DecodeSymbol(&_ppmd, &_rangeDec.p); - if (_inStream.Extra || sym < 0) - break; - memStream[i] = (Byte)sym; - } - - _processedSize += i; - if (_inStream.Extra) - { - _status = kStatus_Error; - return _inStream.Res; - } - if (sym < 0) - _status = (sym < -1) ? kStatus_Error : kStatus_Finished; - return S_OK; -} - -STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - if (!_outBuf) - { - _outBuf = (Byte *)::MidAlloc(kBufSize); - if (!_outBuf) - return E_OUTOFMEMORY; - } - - _inStream.Stream = inStream; - SetOutStreamSize(outSize); - - do - { - const UInt64 startPos = _processedSize; - HRESULT res = CodeSpec(_outBuf, kBufSize); - size_t processed = (size_t)(_processedSize - startPos); - RINOK(WriteStream(outStream, _outBuf, processed)); - RINOK(res); - if (_status == kStatus_Finished) - break; - if (progress) - { - UInt64 inSize = _inStream.GetProcessed(); - RINOK(progress->SetRatioInfo(&inSize, &_processedSize)); - } - } - while (!_outSizeDefined || _processedSize < _outSize); - return S_OK; -} - -STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize) -{ - _outSizeDefined = (outSize != NULL); - if (_outSizeDefined) - _outSize = *outSize; - _processedSize = 0; - _status = kStatus_NeedInit; - return S_OK; -} - -#ifndef NO_READ_FROM_CODER - -STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream) -{ - InSeqStream = inStream; - _inStream.Stream = inStream; - return S_OK; -} - -STDMETHODIMP CDecoder::ReleaseInStream() -{ - InSeqStream.Release(); - return S_OK; -} - -STDMETHODIMP CDecoder::Read(void *data, UInt32 size, UInt32 *processedSize) -{ - const UInt64 startPos = _processedSize; - HRESULT res = CodeSpec((Byte *)data, size); - if (processedSize) - *processedSize = (UInt32)(_processedSize - startPos); - return res; -} - -#endif - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/PpmdDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/PpmdDecoder.h deleted file mode 100644 index 8ebcd700c..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/PpmdDecoder.h +++ /dev/null @@ -1,78 +0,0 @@ -// PpmdDecoder.h -// 2009-03-11 : Igor Pavlov : Public domain - -#ifndef __COMPRESS_PPMD_DECODER_H -#define __COMPRESS_PPMD_DECODER_H - -#include "../../../C/Ppmd7.h" - -#include "../../Common/MyCom.h" - -#include "../Common/CWrappers.h" - -#include "../ICoder.h" - -namespace NCompress { -namespace NPpmd { - -class CDecoder : - public ICompressCoder, - public ICompressSetDecoderProperties2, - #ifndef NO_READ_FROM_CODER - public ICompressSetInStream, - public ICompressSetOutStreamSize, - public ISequentialInStream, - #endif - public CMyUnknownImp -{ - Byte *_outBuf; - CPpmd7z_RangeDec _rangeDec; - CByteInBufWrap _inStream; - CPpmd7 _ppmd; - - Byte _order; - bool _outSizeDefined; - int _status; - UInt64 _outSize; - UInt64 _processedSize; - - HRESULT CodeSpec(Byte *memStream, UInt32 size); - -public: - - #ifndef NO_READ_FROM_CODER - CMyComPtr<ISequentialInStream> InSeqStream; - MY_UNKNOWN_IMP4( - ICompressSetDecoderProperties2, - ICompressSetInStream, - ICompressSetOutStreamSize, - ISequentialInStream) - #else - MY_UNKNOWN_IMP1( - ICompressSetDecoderProperties2) - #endif - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); - STDMETHOD(SetOutStreamSize)(const UInt64 *outSize); - - #ifndef NO_READ_FROM_CODER - STDMETHOD(SetInStream)(ISequentialInStream *inStream); - STDMETHOD(ReleaseInStream)(); - STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); - #endif - - CDecoder(): _outBuf(NULL), _outSizeDefined(false) - { - Ppmd7z_RangeDec_CreateVTable(&_rangeDec); - _rangeDec.Stream = &_inStream.p; - Ppmd7_Construct(&_ppmd); - } - - ~CDecoder(); -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/PpmdEncoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/PpmdEncoder.cpp deleted file mode 100644 index d823ffe85..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/PpmdEncoder.cpp +++ /dev/null @@ -1,119 +0,0 @@ -// PpmdEncoder.cpp -// 2009-03-11 : Igor Pavlov : Public domain - -#include "StdAfx.h" - -#include "../../../C/Alloc.h" -#include "../../../C/CpuArch.h" - -#include "../Common/StreamUtils.h" - -#include "PpmdEncoder.h" - -namespace NCompress { -namespace NPpmd { - -static const UInt32 kBufSize = (1 << 20); - -static void *SzBigAlloc(void *, size_t size) { return BigAlloc(size); } -static void SzBigFree(void *, void *address) { BigFree(address); } -static ISzAlloc g_BigAlloc = { SzBigAlloc, SzBigFree }; - -CEncoder::CEncoder(): - _inBuf(NULL), - _usedMemSize(1 << 24), - _order(6) -{ - _rangeEnc.Stream = &_outStream.p; - Ppmd7_Construct(&_ppmd); -} - -CEncoder::~CEncoder() -{ - ::MidFree(_inBuf); - Ppmd7_Free(&_ppmd, &g_BigAlloc); -} - -STDMETHODIMP CEncoder::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps) -{ - for (UInt32 i = 0; i < numProps; i++) - { - const PROPVARIANT &prop = props[i]; - if (prop.vt != VT_UI4) - return E_INVALIDARG; - UInt32 v = (UInt32)prop.ulVal; - switch(propIDs[i]) - { - case NCoderPropID::kUsedMemorySize: - if (v < (1 << 16) || v > PPMD7_MAX_MEM_SIZE || (v & 3) != 0) - return E_INVALIDARG; - _usedMemSize = v; - break; - case NCoderPropID::kOrder: - if (v < 2 || v > 32) - return E_INVALIDARG; - _order = (Byte)v; - break; - default: - return E_INVALIDARG; - } - } - return S_OK; -} - -STDMETHODIMP CEncoder::WriteCoderProperties(ISequentialOutStream *outStream) -{ - const UInt32 kPropSize = 5; - Byte props[kPropSize]; - props[0] = _order; - SetUi32(props + 1, _usedMemSize); - return WriteStream(outStream, props, kPropSize); -} - -HRESULT CEncoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */, const UInt64 * /* outSize */, ICompressProgressInfo *progress) -{ - if (!_inBuf) - { - _inBuf = (Byte *)::MidAlloc(kBufSize); - if (!_inBuf) - return E_OUTOFMEMORY; - } - if (!_outStream.Alloc(1 << 20)) - return E_OUTOFMEMORY; - if (!Ppmd7_Alloc(&_ppmd, _usedMemSize, &g_BigAlloc)) - return E_OUTOFMEMORY; - - _outStream.Stream = outStream; - _outStream.Init(); - - Ppmd7z_RangeEnc_Init(&_rangeEnc); - Ppmd7_Init(&_ppmd, _order); - - UInt64 processed = 0; - for (;;) - { - UInt32 size; - RINOK(inStream->Read(_inBuf, kBufSize, &size)); - if (size == 0) - { - // We don't write EndMark in PPMD-7z. - // Ppmd7_EncodeSymbol(&_ppmd, &_rangeEnc, -1); - Ppmd7z_RangeEnc_FlushData(&_rangeEnc); - return _outStream.Flush(); - } - for (UInt32 i = 0; i < size; i++) - { - Ppmd7_EncodeSymbol(&_ppmd, &_rangeEnc, _inBuf[i]); - RINOK(_outStream.Res); - } - processed += size; - if (progress) - { - UInt64 outSize = _outStream.GetProcessed(); - RINOK(progress->SetRatioInfo(&processed, &outSize)); - } - } -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/PpmdEncoder.h b/src/libs/7zip/win/CPP/7zip/Compress/PpmdEncoder.h deleted file mode 100644 index ed8b37131..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/PpmdEncoder.h +++ /dev/null @@ -1,48 +0,0 @@ -// PpmdEncoder.h -// 2009-03-11 : Igor Pavlov : Public domain - -#ifndef __COMPRESS_PPMD_ENCODER_H -#define __COMPRESS_PPMD_ENCODER_H - -#include "../../../C/Ppmd7.h" - -#include "../../Common/MyCom.h" - -#include "../ICoder.h" - -#include "../Common/CWrappers.h" - -namespace NCompress { -namespace NPpmd { - -class CEncoder : - public ICompressCoder, - public ICompressSetCoderProperties, - public ICompressWriteCoderProperties, - public CMyUnknownImp -{ - Byte *_inBuf; - CByteOutBufWrap _outStream; - CPpmd7z_RangeEnc _rangeEnc; - CPpmd7 _ppmd; - - UInt32 _usedMemSize; - Byte _order; - -public: - MY_UNKNOWN_IMP2( - ICompressSetCoderProperties, - ICompressWriteCoderProperties) - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - STDMETHOD(SetCoderProperties)(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps); - STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream); - - CEncoder(); - ~CEncoder(); -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/PpmdRegister.cpp b/src/libs/7zip/win/CPP/7zip/Compress/PpmdRegister.cpp deleted file mode 100644 index 9f59fcdd3..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/PpmdRegister.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// PpmdRegister.cpp -// 2009-05-30 : Igor Pavlov : Public domain - -#include "StdAfx.h" - -#include "../Common/RegisterCodec.h" - -#include "PpmdDecoder.h" - -static void *CreateCodec() { return (void *)(ICompressCoder *)(new NCompress::NPpmd::CDecoder); } -#ifndef EXTRACT_ONLY -#include "PpmdEncoder.h" -static void *CreateCodecOut() { return (void *)(ICompressCoder *)(new NCompress::NPpmd::CEncoder); } -#else -#define CreateCodecOut 0 -#endif - -static CCodecInfo g_CodecInfo = - { CreateCodec, CreateCodecOut, 0x030401, L"PPMD", 1, false }; - -REGISTER_CODEC(PPMD) diff --git a/src/libs/7zip/win/CPP/7zip/Compress/PpmdZip.cpp b/src/libs/7zip/win/CPP/7zip/Compress/PpmdZip.cpp deleted file mode 100644 index e83d979c3..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/PpmdZip.cpp +++ /dev/null @@ -1,223 +0,0 @@ -// PpmdZip.cpp -// 2010-03-24 : Igor Pavlov : Public domain - -#include "StdAfx.h" - -#include "../../../C/CpuArch.h" - -#include "../Common/StreamUtils.h" - -#include "PpmdZip.h" - -namespace NCompress { -namespace NPpmdZip { - -static void *SzBigAlloc(void *, size_t size) { return BigAlloc(size); } -static void SzBigFree(void *, void *address) { BigFree(address); } -static ISzAlloc g_BigAlloc = { SzBigAlloc, SzBigFree }; - -CDecoder::CDecoder(bool fullFileMode): - _fullFileMode(fullFileMode) -{ - _ppmd.Stream.In = &_inStream.p; - Ppmd8_Construct(&_ppmd); -} - -CDecoder::~CDecoder() -{ - Ppmd8_Free(&_ppmd, &g_BigAlloc); -} - -STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - if (!_outStream.Alloc()) - return E_OUTOFMEMORY; - if (!_inStream.Alloc(1 << 20)) - return E_OUTOFMEMORY; - - _inStream.Stream = inStream; - _inStream.Init(); - - { - Byte buf[2]; - for (int i = 0; i < 2; i++) - buf[i] = _inStream.ReadByte(); - if (_inStream.Extra) - return S_FALSE; - - UInt32 val = GetUi16(buf); - UInt32 order = (val & 0xF) + 1; - UInt32 mem = ((val >> 4) & 0xFF) + 1; - UInt32 restor = (val >> 12); - if (order < 2 || restor > 2) - return S_FALSE; - - #ifndef PPMD8_FREEZE_SUPPORT - if (restor == 2) - return E_NOTIMPL; - #endif - - if (!Ppmd8_Alloc(&_ppmd, mem << 20, &g_BigAlloc)) - return E_OUTOFMEMORY; - - if (!Ppmd8_RangeDec_Init(&_ppmd)) - return S_FALSE; - Ppmd8_Init(&_ppmd, order, restor); - } - - bool wasFinished = false; - UInt64 processedSize = 0; - while (!outSize || processedSize < *outSize) - { - size_t size = kBufSize; - if (outSize != NULL) - { - const UInt64 rem = *outSize - processedSize; - if (size > rem) - size = (size_t)rem; - } - Byte *data = _outStream.Buf; - size_t i = 0; - int sym = 0; - do - { - sym = Ppmd8_DecodeSymbol(&_ppmd); - if (_inStream.Extra || sym < 0) - break; - data[i] = (Byte)sym; - } - while (++i != size); - processedSize += i; - - RINOK(WriteStream(outStream, _outStream.Buf, i)); - - RINOK(_inStream.Res); - if (_inStream.Extra) - return S_FALSE; - - if (sym < 0) - { - if (sym != -1) - return S_FALSE; - wasFinished = true; - break; - } - if (progress) - { - UInt64 inSize = _inStream.GetProcessed(); - RINOK(progress->SetRatioInfo(&inSize, &processedSize)); - } - } - RINOK(_inStream.Res); - if (_fullFileMode) - { - if (!wasFinished) - { - int res = Ppmd8_DecodeSymbol(&_ppmd); - RINOK(_inStream.Res); - if (_inStream.Extra || res != -1) - return S_FALSE; - } - if (!Ppmd8_RangeDec_IsFinishedOK(&_ppmd)) - return S_FALSE; - } - return S_OK; -} - - -// ---------- Encoder ---------- - -CEncoder::~CEncoder() -{ - Ppmd8_Free(&_ppmd, &g_BigAlloc); -} - -HRESULT CEncoder::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps) -{ - for (UInt32 i = 0; i < numProps; i++) - { - const PROPVARIANT &prop = props[i]; - if (prop.vt != VT_UI4) - return E_INVALIDARG; - UInt32 v = (UInt32)prop.ulVal; - switch(propIDs[i]) - { - case NCoderPropID::kAlgorithm: - if (v > 1) - return E_INVALIDARG; - _restor = v; - break; - case NCoderPropID::kUsedMemorySize: - if (v < (1 << 20) || v > (1 << 28)) - return E_INVALIDARG; - _usedMemInMB = v >> 20; - break; - case NCoderPropID::kOrder: - if (v < PPMD8_MIN_ORDER || v > PPMD8_MAX_ORDER) - return E_INVALIDARG; - _order = (Byte)v; - break; - default: - return E_INVALIDARG; - } - } - return S_OK; -} - -CEncoder::CEncoder(): - _usedMemInMB(16), - _order(6), - _restor(PPMD8_RESTORE_METHOD_RESTART) -{ - _ppmd.Stream.Out = &_outStream.p; - Ppmd8_Construct(&_ppmd); -} - -HRESULT CEncoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */, const UInt64 * /* outSize */, ICompressProgressInfo *progress) -{ - if (!_inStream.Alloc()) - return E_OUTOFMEMORY; - if (!_outStream.Alloc(1 << 20)) - return E_OUTOFMEMORY; - if (!Ppmd8_Alloc(&_ppmd, _usedMemInMB << 20, &g_BigAlloc)) - return E_OUTOFMEMORY; - - _outStream.Stream = outStream; - _outStream.Init(); - - Ppmd8_RangeEnc_Init(&_ppmd); - Ppmd8_Init(&_ppmd, _order, _restor); - - UInt32 val = (UInt32)((_order - 1) + ((_usedMemInMB - 1) << 4) + (_restor << 12)); - _outStream.WriteByte((Byte)(val & 0xFF)); - _outStream.WriteByte((Byte)(val >> 8)); - RINOK(_outStream.Res); - - UInt64 processed = 0; - for (;;) - { - UInt32 size; - RINOK(inStream->Read(_inStream.Buf, kBufSize, &size)); - if (size == 0) - { - Ppmd8_EncodeSymbol(&_ppmd, -1); - Ppmd8_RangeEnc_FlushData(&_ppmd); - return _outStream.Flush(); - } - for (UInt32 i = 0; i < size; i++) - { - Ppmd8_EncodeSymbol(&_ppmd, _inStream.Buf[i]); - RINOK(_outStream.Res); - } - processed += size; - if (progress != NULL) - { - UInt64 outSize = _outStream.GetProcessed(); - RINOK(progress->SetRatioInfo(&processed, &outSize)); - } - } -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/PpmdZip.h b/src/libs/7zip/win/CPP/7zip/Compress/PpmdZip.h deleted file mode 100644 index c2288b5af..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/PpmdZip.h +++ /dev/null @@ -1,72 +0,0 @@ -// PpmdZip.h -// 2010-03-11 : Igor Pavlov : Public domain - -#ifndef __COMPRESS_PPMD_ZIP_H -#define __COMPRESS_PPMD_ZIP_H - -#include "../../../C/Alloc.h" -#include "../../../C/Ppmd8.h" - -#include "../../Common/MyCom.h" - -#include "../Common/CWrappers.h" - -#include "../ICoder.h" - -namespace NCompress { -namespace NPpmdZip { - -static const UInt32 kBufSize = (1 << 20); - -struct CBuf -{ - Byte *Buf; - - CBuf(): Buf(0) {} - ~CBuf() { ::MidFree(Buf); } - bool Alloc() - { - if (!Buf) - Buf = (Byte *)::MidAlloc(kBufSize); - return (Buf != 0); - } -}; - -class CDecoder : - public ICompressCoder, - public CMyUnknownImp -{ - CByteInBufWrap _inStream; - CBuf _outStream; - CPpmd8 _ppmd; - bool _fullFileMode; -public: - MY_UNKNOWN_IMP - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - CDecoder(bool fullFileMode); - ~CDecoder(); -}; - -class CEncoder : - public ICompressCoder, - public CMyUnknownImp -{ - CByteOutBufWrap _outStream; - CBuf _inStream; - CPpmd8 _ppmd; - UInt32 _usedMemInMB; - unsigned _order; - unsigned _restor; -public: - MY_UNKNOWN_IMP - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - HRESULT SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps); - CEncoder(); - ~CEncoder(); -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/QuantumDecoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/QuantumDecoder.cpp deleted file mode 100644 index 501bd0e1f..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/QuantumDecoder.cpp +++ /dev/null @@ -1,175 +0,0 @@ -// QuantumDecoder.cpp - -#include "StdAfx.h" - -#include "../../Common/Defs.h" - -#include "QuantumDecoder.h" - -namespace NCompress { -namespace NQuantum { - -static const int kLenIdNeedInit = -2; - -static const unsigned kNumLenSymbols = 27; -static const unsigned kMatchMinLen = 3; -static const unsigned kNumSimplePosSlots = 4; -static const unsigned kNumSimpleLenSlots = 6; - -void CDecoder::Init() -{ - m_Selector.Init(kNumSelectors); - unsigned i; - for (i = 0; i < kNumLitSelectors; i++) - m_Literals[i].Init(kNumLitSymbols); - unsigned numItems = (_numDictBits == 0 ? 1 : (_numDictBits << 1)); - const unsigned kNumPosSymbolsMax[kNumMatchSelectors] = { 24, 36, 42 }; - for (i = 0; i < kNumMatchSelectors; i++) - m_PosSlot[i].Init(MyMin(numItems, kNumPosSymbolsMax[i])); - m_LenSlot.Init(kNumLenSymbols); -} - -HRESULT CDecoder::CodeSpec(UInt32 curSize) -{ - if (_remainLen == kLenIdNeedInit) - { - if (!_keepHistory) - { - if (!_outWindowStream.Create((UInt32)1 << _numDictBits)) - return E_OUTOFMEMORY; - Init(); - } - if (!_rangeDecoder.Create(1 << 20)) - return E_OUTOFMEMORY; - _rangeDecoder.Init(); - _remainLen = 0; - } - if (curSize == 0) - return S_OK; - - while (_remainLen > 0 && curSize > 0) - { - _remainLen--; - Byte b = _outWindowStream.GetByte(_rep0); - _outWindowStream.PutByte(b); - curSize--; - } - - while (curSize > 0) - { - if (_rangeDecoder.Stream.WasFinished()) - return S_FALSE; - - unsigned selector = m_Selector.Decode(&_rangeDecoder); - if (selector < kNumLitSelectors) - { - Byte b = (Byte)((selector << (8 - kNumLitSelectorBits)) + m_Literals[selector].Decode(&_rangeDecoder)); - _outWindowStream.PutByte(b); - curSize--; - } - else - { - selector -= kNumLitSelectors; - unsigned len = selector + kMatchMinLen; - if (selector == 2) - { - unsigned lenSlot = m_LenSlot.Decode(&_rangeDecoder); - if (lenSlot >= kNumSimpleLenSlots) - { - lenSlot -= 2; - int numDirectBits = (int)(lenSlot >> 2); - len += ((4 | (lenSlot & 3)) << numDirectBits) - 2; - if (numDirectBits < 6) - len += _rangeDecoder.Stream.ReadBits(numDirectBits); - } - else - len += lenSlot; - } - UInt32 rep0 = m_PosSlot[selector].Decode(&_rangeDecoder); - if (rep0 >= kNumSimplePosSlots) - { - int numDirectBits = (int)((rep0 >> 1) - 1); - rep0 = ((2 | (rep0 & 1)) << numDirectBits) + _rangeDecoder.Stream.ReadBits(numDirectBits); - } - unsigned locLen = len; - if (len > curSize) - locLen = (unsigned)curSize; - if (!_outWindowStream.CopyBlock(rep0, locLen)) - return S_FALSE; - curSize -= locLen; - len -= locLen; - if (len != 0) - { - _remainLen = (int)len; - _rep0 = rep0; - break; - } - } - } - return _rangeDecoder.Stream.WasFinished() ? S_FALSE : S_OK; -} - -HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - if (outSize == NULL) - return E_INVALIDARG; - UInt64 size = *outSize; - - SetInStream(inStream); - _outWindowStream.SetStream(outStream); - SetOutStreamSize(outSize); - CDecoderFlusher flusher(this); - - const UInt64 start = _outWindowStream.GetProcessedSize(); - for (;;) - { - UInt32 curSize = 1 << 18; - UInt64 rem = size - (_outWindowStream.GetProcessedSize() - start); - if (curSize > rem) - curSize = (UInt32)rem; - if (curSize == 0) - break; - RINOK(CodeSpec(curSize)); - if (progress != NULL) - { - UInt64 inSize = _rangeDecoder.GetProcessedSize(); - UInt64 nowPos64 = _outWindowStream.GetProcessedSize() - start; - RINOK(progress->SetRatioInfo(&inSize, &nowPos64)); - } - } - flusher.NeedFlush = false; - return Flush(); -} - -STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - try { return CodeReal(inStream, outStream, inSize, outSize, progress); } - catch(const CInBufferException &e) { return e.ErrorCode; } - catch(const CLzOutWindowException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } -} - -STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream) -{ - _rangeDecoder.SetStream(inStream); - return S_OK; -} - -STDMETHODIMP CDecoder::ReleaseInStream() -{ - _rangeDecoder.ReleaseStream(); - return S_OK; -} - -STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize) -{ - if (outSize == NULL) - return E_FAIL; - _remainLen = kLenIdNeedInit; - _outWindowStream.Init(_keepHistory); - return S_OK; -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/QuantumDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/QuantumDecoder.h deleted file mode 100644 index e9ab023ba..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/QuantumDecoder.h +++ /dev/null @@ -1,264 +0,0 @@ -// QuantumDecoder.h - -#ifndef __COMPRESS_QUANTUM_DECODER_H -#define __COMPRESS_QUANTUM_DECODER_H - -#include "../../Common/MyCom.h" - -#include "../ICoder.h" - -#include "../Common/InBuffer.h" - -#include "LzOutWindow.h" - -namespace NCompress { -namespace NQuantum { - -class CStreamBitDecoder -{ - UInt32 Value; - CInBuffer Stream; -public: - bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); } - void SetStream(ISequentialInStream *stream) { Stream.SetStream(stream); } - void ReleaseStream() { Stream.ReleaseStream(); } - - void Finish() { Value = 0x10000; } - - void Init() - { - Stream.Init(); - Value = 0x10000; - } - - UInt64 GetProcessedSize() const { return Stream.GetProcessedSize(); } - bool WasFinished() const { return Stream.WasFinished(); } - - UInt32 ReadBit() - { - if (Value >= 0x10000) - Value = 0x100 | Stream.ReadByte(); - UInt32 res = (Value >> 7) & 1; - Value <<= 1; - return res; - } - - UInt32 ReadBits(int numBits) // numBits > 0 - { - UInt32 res = 0; - do - res = (res << 1) | ReadBit(); - while (--numBits != 0); - return res; - } -}; - -const unsigned kNumLitSelectorBits = 2; -const unsigned kNumLitSelectors = (1 << kNumLitSelectorBits); -const unsigned kNumLitSymbols = 1 << (8 - kNumLitSelectorBits); -const unsigned kNumMatchSelectors = 3; -const unsigned kNumSelectors = kNumLitSelectors + kNumMatchSelectors; -const unsigned kNumSymbolsMax = kNumLitSymbols; // 64 - -namespace NRangeCoder { - -class CDecoder -{ - UInt32 Low; - UInt32 Range; - UInt32 Code; -public: - CStreamBitDecoder Stream; - bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); } - void SetStream(ISequentialInStream *stream) { Stream.SetStream(stream); } - void ReleaseStream() { Stream.ReleaseStream(); } - - void Init() - { - Stream.Init(); - Low = 0; - Range = 0x10000; - Code = Stream.ReadBits(16); - } - - void Finish() - { - // we need these extra two Bit_reads - Stream.ReadBit(); - Stream.ReadBit(); - Stream.Finish(); - } - - UInt64 GetProcessedSize() const { return Stream.GetProcessedSize(); } - - UInt32 GetThreshold(UInt32 total) const - { - return ((Code + 1) * total - 1) / Range; // & 0xFFFF is not required; - } - - void Decode(UInt32 start, UInt32 end, UInt32 total) - { - UInt32 high = Low + end * Range / total - 1; - UInt32 offset = start * Range / total; - Code -= offset; - Low += offset; - for (;;) - { - if ((Low & 0x8000) != (high & 0x8000)) - { - if ((Low & 0x4000) == 0 || (high & 0x4000) != 0) - break; - Low &= 0x3FFF; - high |= 0x4000; - } - Low = (Low << 1) & 0xFFFF; - high = ((high << 1) | 1) & 0xFFFF; - Code = ((Code << 1) | Stream.ReadBit()); - } - Range = high - Low + 1; - } -}; - -const UInt16 kUpdateStep = 8; -const UInt16 kFreqSumMax = 3800; -const UInt16 kReorderCountStart = 4; -const UInt16 kReorderCount = 50; - -class CModelDecoder -{ - unsigned NumItems; - unsigned ReorderCount; - UInt16 Freqs[kNumSymbolsMax + 1]; - Byte Values[kNumSymbolsMax]; -public: - void Init(unsigned numItems) - { - NumItems = numItems; - ReorderCount = kReorderCountStart; - for (unsigned i = 0; i < numItems; i++) - { - Freqs[i] = (UInt16)(numItems - i); - Values[i] = (Byte)i; - } - Freqs[numItems] = 0; - } - - unsigned Decode(CDecoder *rangeDecoder) - { - UInt32 threshold = rangeDecoder->GetThreshold(Freqs[0]); - unsigned i; - for (i = 1; Freqs[i] > threshold; i++); - rangeDecoder->Decode(Freqs[i], Freqs[i - 1], Freqs[0]); - unsigned res = Values[--i]; - do - Freqs[i] += kUpdateStep; - while (i-- != 0); - - if (Freqs[0] > kFreqSumMax) - { - if (--ReorderCount == 0) - { - ReorderCount = kReorderCount; - for (i = 0; i < NumItems; i++) - Freqs[i] = (UInt16)(((Freqs[i] - Freqs[i + 1]) + 1) >> 1); - for (i = 0; i < NumItems - 1; i++) - for (unsigned j = i + 1; j < NumItems; j++) - if (Freqs[i] < Freqs[j]) - { - UInt16 tmpFreq = Freqs[i]; - Byte tmpVal = Values[i]; - Freqs[i] = Freqs[j]; - Values[i] = Values[j]; - Freqs[j] = tmpFreq; - Values[j] = tmpVal; - } - do - Freqs[i] = (UInt16)(Freqs[i] + Freqs[i + 1]); - while (i-- != 0); - } - else - { - i = NumItems - 1; - do - { - Freqs[i] >>= 1; - if (Freqs[i] <= Freqs[i + 1]) - Freqs[i] = (UInt16)(Freqs[i + 1] + 1); - } - while (i-- != 0); - } - } - return res; - } -}; - -} - -class CDecoder: - public ICompressCoder, - public ICompressSetInStream, - public ICompressSetOutStreamSize, - public CMyUnknownImp -{ - CLzOutWindow _outWindowStream; - NRangeCoder::CDecoder _rangeDecoder; - - UInt64 _outSize; - int _remainLen; // -1 means end of stream. // -2 means need Init - UInt32 _rep0; - - int _numDictBits; - bool _keepHistory; - - NRangeCoder::CModelDecoder m_Selector; - NRangeCoder::CModelDecoder m_Literals[kNumLitSelectors]; - NRangeCoder::CModelDecoder m_PosSlot[kNumMatchSelectors]; - NRangeCoder::CModelDecoder m_LenSlot; - void Init(); - HRESULT CodeSpec(UInt32 size); -public: - MY_UNKNOWN_IMP2( - ICompressSetInStream, - ICompressSetOutStreamSize) - - void ReleaseStreams() - { - _outWindowStream.ReleaseStream(); - ReleaseInStream(); - } - - class CDecoderFlusher - { - CDecoder *_decoder; - public: - bool NeedFlush; - CDecoderFlusher(CDecoder *decoder): _decoder(decoder), NeedFlush(true) {} - ~CDecoderFlusher() - { - if (NeedFlush) - _decoder->Flush(); - _decoder->ReleaseStreams(); - } - }; - - HRESULT Flush() { return _outWindowStream.Flush(); } - - HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(SetInStream)(ISequentialInStream *inStream); - STDMETHOD(ReleaseInStream)(); - STDMETHOD(SetOutStreamSize)(const UInt64 *outSize); - - void SetParams(int numDictBits) { _numDictBits = numDictBits; } - void SetKeepHistory(bool keepHistory) { _keepHistory = keepHistory; } - CDecoder(): _keepHistory(false) {} - virtual ~CDecoder() {} -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Rar1Decoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/Rar1Decoder.cpp deleted file mode 100644 index eadca7b3d..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Rar1Decoder.cpp +++ /dev/null @@ -1,480 +0,0 @@ -// Rar1Decoder.cpp -// According to unRAR license, this code may not be used to develop -// a program that creates RAR archives - -#include "StdAfx.h" - -#include "Rar1Decoder.h" - -namespace NCompress { -namespace NRar1 { - -static UInt32 PosL1[]={0,0,0,2,3,5,7,11,16,20,24,32,32, 256}; -static UInt32 PosL2[]={0,0,0,0,5,7,9,13,18,22,26,34,36, 256}; -static UInt32 PosHf0[]={0,0,0,0,0,8,16,24,33,33,33,33,33, 257}; -static UInt32 PosHf1[]={0,0,0,0,0,0,4,44,60,76,80,80,127, 257}; -static UInt32 PosHf2[]={0,0,0,0,0,0,2,7,53,117,233, 257,0}; -static UInt32 PosHf3[]={0,0,0,0,0,0,0,2,16,218,251, 257,0}; -static UInt32 PosHf4[]={0,0,0,0,0,0,0,0,0,255, 257,0,0}; - -static const UInt32 kHistorySize = (1 << 16); - -class CCoderReleaser -{ - CDecoder *m_Coder; -public: - CCoderReleaser(CDecoder *coder): m_Coder(coder) {} - ~CCoderReleaser() { m_Coder->ReleaseStreams(); } -}; - -CDecoder::CDecoder(): m_IsSolid(false) { } - -void CDecoder::InitStructures() -{ - for(int i = 0; i < kNumRepDists; i++) - m_RepDists[i] = 0; - m_RepDistPtr = 0; - LastLength = 0; - LastDist = 0; -} - -UInt32 CDecoder::ReadBits(int numBits) { return m_InBitStream.ReadBits(numBits); } - -HRESULT CDecoder::CopyBlock(UInt32 distance, UInt32 len) -{ - if (len == 0) - return S_FALSE; - m_UnpackSize -= len; - return m_OutWindowStream.CopyBlock(distance, len) ? S_OK : S_FALSE; -} - - -UInt32 CDecoder::DecodeNum(const UInt32 *posTab) -{ - UInt32 startPos = 2; - UInt32 num = m_InBitStream.GetValue(12); - for (;;) - { - UInt32 cur = (posTab[startPos + 1] - posTab[startPos]) << (12 - startPos); - if (num < cur) - break; - startPos++; - num -= cur; - } - m_InBitStream.MovePos(startPos); - return((num >> (12 - startPos)) + posTab[startPos]); -} - -static Byte kShortLen1[] = {1,3,4,4,5,6,7,8,8,4,4,5,6,6 }; -static Byte kShortLen1a[] = {1,4,4,4,5,6,7,8,8,4,4,5,6,6,4 }; -static Byte kShortLen2[] = {2,3,3,3,4,4,5,6,6,4,4,5,6,6 }; -static Byte kShortLen2a[] = {2,3,3,4,4,4,5,6,6,4,4,5,6,6,4 }; -static UInt32 kShortXor1[] = {0,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xc0,0x80,0x90,0x98,0x9c,0xb0}; -static UInt32 kShortXor2[] = {0,0x40,0x60,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xc0,0x80,0x90,0x98,0x9c,0xb0}; - -HRESULT CDecoder::ShortLZ() -{ - UInt32 len, saveLen, dist; - int distancePlace; - Byte *kShortLen; - const UInt32 *kShortXor; - NumHuf = 0; - - if (LCount == 2) - { - if (ReadBits(1)) - return CopyBlock(LastDist, LastLength); - LCount = 0; - } - - UInt32 bitField = m_InBitStream.GetValue(8); - - if (AvrLn1 < 37) - { - kShortLen = Buf60 ? kShortLen1a : kShortLen1; - kShortXor = kShortXor1; - } - else - { - kShortLen = Buf60 ? kShortLen2a : kShortLen2; - kShortXor = kShortXor2; - } - - for (len = 0; ((bitField ^ kShortXor[len]) & (~(0xff >> kShortLen[len]))) != 0; len++); - m_InBitStream.MovePos(kShortLen[len]); - - if (len >= 9) - { - if (len == 9) - { - LCount++; - return CopyBlock(LastDist, LastLength); - } - if (len == 14) - { - LCount = 0; - len = DecodeNum(PosL2) + 5; - dist = 0x8000 + ReadBits(15) - 1; - LastLength = len; - LastDist = dist; - return CopyBlock(dist, len); - } - - LCount = 0; - saveLen = len; - dist = m_RepDists[(m_RepDistPtr - (len - 9)) & 3]; - len = DecodeNum(PosL1) + 2; - if (len == 0x101 && saveLen == 10) - { - Buf60 ^= 1; - return S_OK; - } - if (dist >= 256) - len++; - if (dist >= MaxDist3 - 1) - len++; - } - else - { - LCount = 0; - AvrLn1 += len; - AvrLn1 -= AvrLn1 >> 4; - - distancePlace = DecodeNum(PosHf2) & 0xff; - dist = ChSetA[distancePlace]; - if (--distancePlace != -1) - { - PlaceA[dist]--; - UInt32 lastDistance = ChSetA[distancePlace]; - PlaceA[lastDistance]++; - ChSetA[distancePlace + 1] = lastDistance; - ChSetA[distancePlace] = dist; - } - len += 2; - } - m_RepDists[m_RepDistPtr++] = dist; - m_RepDistPtr &= 3; - LastLength = len; - LastDist = dist; - return CopyBlock(dist, len); -} - - -HRESULT CDecoder::LongLZ() -{ - UInt32 len; - UInt32 dist; - UInt32 distancePlace, newDistancePlace; - UInt32 oldAvr2, oldAvr3; - - NumHuf = 0; - Nlzb += 16; - if (Nlzb > 0xff) - { - Nlzb = 0x90; - Nhfb >>= 1; - } - oldAvr2=AvrLn2; - - if (AvrLn2 >= 122) - len = DecodeNum(PosL2); - else if (AvrLn2 >= 64) - len = DecodeNum(PosL1); - else - { - UInt32 bitField = m_InBitStream.GetValue(16); - if (bitField < 0x100) - { - len = bitField; - m_InBitStream.MovePos(16); - } - else - { - for (len = 0; ((bitField << len) & 0x8000) == 0; len++) - ; - m_InBitStream.MovePos(len + 1); - } - } - - AvrLn2 += len; - AvrLn2 -= AvrLn2 >> 5; - - if (AvrPlcB > 0x28ff) - distancePlace = DecodeNum(PosHf2); - else if (AvrPlcB > 0x6ff) - distancePlace = DecodeNum(PosHf1); - else - distancePlace = DecodeNum(PosHf0); - - AvrPlcB += distancePlace; - AvrPlcB -= AvrPlcB >> 8; - for (;;) - { - dist = ChSetB[distancePlace & 0xff]; - newDistancePlace = NToPlB[dist++ & 0xff]++; - if (!(dist & 0xff)) - CorrHuff(ChSetB,NToPlB); - else - break; - } - - ChSetB[distancePlace] = ChSetB[newDistancePlace]; - ChSetB[newDistancePlace] = dist; - - dist = ((dist & 0xff00) >> 1) | ReadBits(7); - - oldAvr3 = AvrLn3; - if (len != 1 && len != 4) - if (len == 0 && dist <= MaxDist3) - { - AvrLn3++; - AvrLn3 -= AvrLn3 >> 8; - } - else - if (AvrLn3 > 0) - AvrLn3--; - len += 3; - if (dist >= MaxDist3) - len++; - if (dist <= 256) - len += 8; - if (oldAvr3 > 0xb0 || AvrPlc >= 0x2a00 && oldAvr2 < 0x40) - MaxDist3 = 0x7f00; - else - MaxDist3 = 0x2001; - m_RepDists[m_RepDistPtr++] = --dist; - m_RepDistPtr &= 3; - LastLength = len; - LastDist = dist; - return CopyBlock(dist, len); -} - - -HRESULT CDecoder::HuffDecode() -{ - UInt32 curByte, newBytePlace; - UInt32 len; - UInt32 dist; - int bytePlace; - - if (AvrPlc > 0x75ff) bytePlace = DecodeNum(PosHf4); - else if (AvrPlc > 0x5dff) bytePlace = DecodeNum(PosHf3); - else if (AvrPlc > 0x35ff) bytePlace = DecodeNum(PosHf2); - else if (AvrPlc > 0x0dff) bytePlace = DecodeNum(PosHf1); - else bytePlace = DecodeNum(PosHf0); - if (StMode) - { - if (--bytePlace == -1) - { - if (ReadBits(1)) - { - NumHuf = StMode = 0; - return S_OK; - } - else - { - len = (ReadBits(1)) ? 4 : 3; - dist = DecodeNum(PosHf2); - dist = (dist << 5) | ReadBits(5); - return CopyBlock(dist - 1, len); - } - } - } - else if (NumHuf++ >= 16 && FlagsCnt == 0) - StMode = 1; - bytePlace &= 0xff; - AvrPlc += bytePlace; - AvrPlc -= AvrPlc >> 8; - Nhfb+=16; - if (Nhfb > 0xff) - { - Nhfb=0x90; - Nlzb >>= 1; - } - - m_UnpackSize --; - m_OutWindowStream.PutByte((Byte)(ChSet[bytePlace] >> 8)); - - for (;;) - { - curByte = ChSet[bytePlace]; - newBytePlace = NToPl[curByte++ & 0xff]++; - if ((curByte & 0xff) > 0xa1) - CorrHuff(ChSet, NToPl); - else - break; - } - - ChSet[bytePlace] = ChSet[newBytePlace]; - ChSet[newBytePlace] = curByte; - return S_OK; -} - - -void CDecoder::GetFlagsBuf() -{ - UInt32 flags, newFlagsPlace; - UInt32 flagsPlace = DecodeNum(PosHf2); - - for (;;) - { - flags = ChSetC[flagsPlace]; - FlagBuf = flags >> 8; - newFlagsPlace = NToPlC[flags++ & 0xff]++; - if ((flags & 0xff) != 0) - break; - CorrHuff(ChSetC, NToPlC); - } - - ChSetC[flagsPlace] = ChSetC[newFlagsPlace]; - ChSetC[newFlagsPlace] = flags; -} - -void CDecoder::InitData() -{ - if (!m_IsSolid) - { - AvrPlcB = AvrLn1 = AvrLn2 = AvrLn3 = NumHuf = Buf60 = 0; - AvrPlc = 0x3500; - MaxDist3 = 0x2001; - Nhfb = Nlzb = 0x80; - } - FlagsCnt = 0; - FlagBuf = 0; - StMode = 0; - LCount = 0; -} - -void CDecoder::CorrHuff(UInt32 *CharSet,UInt32 *NumToPlace) -{ - int i; - for (i = 7; i >= 0; i--) - for (int j = 0; j < 32; j++, CharSet++) - *CharSet = (*CharSet & ~0xff) | i; - memset(NumToPlace, 0, sizeof(NToPl)); - for (i = 6; i >= 0; i--) - NumToPlace[i] = (7 - i) * 32; -} - -void CDecoder::InitHuff() -{ - for (UInt32 i = 0; i < 256; i++) - { - Place[i] = PlaceA[i] = PlaceB[i] = i; - PlaceC[i] = (~i + 1) & 0xff; - ChSet[i] = ChSetB[i] = i << 8; - ChSetA[i] = i; - ChSetC[i] = ((~i + 1) & 0xff) << 8; - } - memset(NToPl, 0, sizeof(NToPl)); - memset(NToPlB, 0, sizeof(NToPlB)); - memset(NToPlC, 0, sizeof(NToPlC)); - CorrHuff(ChSetB, NToPlB); -} - -HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo * /* progress */) -{ - if (inSize == NULL || outSize == NULL) - return E_INVALIDARG; - - if (!m_OutWindowStream.Create(kHistorySize)) - return E_OUTOFMEMORY; - if (!m_InBitStream.Create(1 << 20)) - return E_OUTOFMEMORY; - - m_UnpackSize = (Int64)*outSize; - m_OutWindowStream.SetStream(outStream); - m_OutWindowStream.Init(m_IsSolid); - m_InBitStream.SetStream(inStream); - m_InBitStream.Init(); - - CCoderReleaser coderReleaser(this); - InitData(); - if (!m_IsSolid) - { - InitStructures(); - InitHuff(); - } - if (m_UnpackSize > 0) - { - GetFlagsBuf(); - FlagsCnt = 8; - } - - while (m_UnpackSize > 0) - { - if (StMode) - { - RINOK(HuffDecode()); - continue; - } - - if (--FlagsCnt < 0) - { - GetFlagsBuf(); - FlagsCnt=7; - } - - if (FlagBuf & 0x80) - { - FlagBuf <<= 1; - if (Nlzb > Nhfb) - { - RINOK(LongLZ()); - } - else - { - RINOK(HuffDecode()); - } - } - else - { - FlagBuf <<= 1; - if (--FlagsCnt < 0) - { - GetFlagsBuf(); - FlagsCnt = 7; - } - if (FlagBuf & 0x80) - { - FlagBuf <<= 1; - if (Nlzb > Nhfb) - { - RINOK(HuffDecode()); - } - else - { - RINOK(LongLZ()); - } - } - else - { - FlagBuf <<= 1; - RINOK(ShortLZ()); - } - } - } - if (m_UnpackSize < 0) - return S_FALSE; - return m_OutWindowStream.Flush(); -} - -STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - try { return CodeReal(inStream, outStream, inSize, outSize, progress); } - catch(const CInBufferException &e) { return e.ErrorCode; } - catch(const CLzOutWindowException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } -} - -STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size) -{ - if (size < 1) - return E_INVALIDARG; - m_IsSolid = (data[0] != 0); - return S_OK; -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Rar1Decoder.h b/src/libs/7zip/win/CPP/7zip/Compress/Rar1Decoder.h deleted file mode 100644 index f7c08b386..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Rar1Decoder.h +++ /dev/null @@ -1,88 +0,0 @@ -// Rar1Decoder.h -// According to unRAR license, this code may not be used to develop -// a program that creates RAR archives - -#ifndef __COMPRESS_RAR1_DECODER_H -#define __COMPRESS_RAR1_DECODER_H - -#include "../../Common/MyCom.h" - -#include "../ICoder.h" - -#include "../Common/InBuffer.h" - -#include "BitmDecoder.h" -#include "HuffmanDecoder.h" -#include "LzOutWindow.h" - -namespace NCompress { -namespace NRar1 { - -const UInt32 kNumRepDists = 4; - -typedef NBitm::CDecoder<CInBuffer> CBitDecoder; - -class CDecoder : - public ICompressCoder, - public ICompressSetDecoderProperties2, - public CMyUnknownImp -{ -public: - CLzOutWindow m_OutWindowStream; - CBitDecoder m_InBitStream; - - UInt32 m_RepDists[kNumRepDists]; - UInt32 m_RepDistPtr; - - UInt32 LastDist; - UInt32 LastLength; - - Int64 m_UnpackSize; - bool m_IsSolid; - - UInt32 ReadBits(int numBits); - HRESULT CopyBlock(UInt32 distance, UInt32 len); - - UInt32 DecodeNum(const UInt32 *posTab); - HRESULT ShortLZ(); - HRESULT LongLZ(); - HRESULT HuffDecode(); - void GetFlagsBuf(); - void InitData(); - void InitHuff(); - void CorrHuff(UInt32 *CharSet, UInt32 *NumToPlace); - void OldUnpWriteBuf(); - - UInt32 ChSet[256],ChSetA[256],ChSetB[256],ChSetC[256]; - UInt32 Place[256],PlaceA[256],PlaceB[256],PlaceC[256]; - UInt32 NToPl[256],NToPlB[256],NToPlC[256]; - UInt32 FlagBuf,AvrPlc,AvrPlcB,AvrLn1,AvrLn2,AvrLn3; - int Buf60,NumHuf,StMode,LCount,FlagsCnt; - UInt32 Nhfb,Nlzb,MaxDist3; - - void InitStructures(); - - HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - -public: - CDecoder(); - - MY_UNKNOWN_IMP1(ICompressSetDecoderProperties2) - - void ReleaseStreams() - { - m_OutWindowStream.ReleaseStream(); - m_InBitStream.ReleaseStream(); - } - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); - -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Rar2Decoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/Rar2Decoder.cpp deleted file mode 100644 index 4e669bd64..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Rar2Decoder.cpp +++ /dev/null @@ -1,391 +0,0 @@ -// Rar2Decoder.cpp -// According to unRAR license, this code may not be used to develop -// a program that creates RAR archives - -#include "StdAfx.h" - -#include "Rar2Decoder.h" - -namespace NCompress { -namespace NRar2 { - -namespace NMultimedia { - -Byte CFilter::Decode(int &channelDelta, Byte deltaByte) -{ - D4 = D3; - D3 = D2; - D2 = LastDelta - D1; - D1 = LastDelta; - int predictedValue = ((8 * LastChar + K1 * D1 + K2 * D2 + K3 * D3 + K4 * D4 + K5 * channelDelta) >> 3); - - Byte realValue = (Byte)(predictedValue - deltaByte); - int i = ((int)(signed char)deltaByte) << 3; - - Dif[0] += abs(i); - Dif[1] += abs(i - D1); - Dif[2] += abs(i + D1); - Dif[3] += abs(i - D2); - Dif[4] += abs(i + D2); - Dif[5] += abs(i - D3); - Dif[6] += abs(i + D3); - Dif[7] += abs(i - D4); - Dif[8] += abs(i + D4); - Dif[9] += abs(i - channelDelta); - Dif[10] += abs(i + channelDelta); - - channelDelta = LastDelta = (signed char)(realValue - LastChar); - LastChar = realValue; - - if (((++ByteCount) & 0x1F) == 0) - { - UInt32 minDif = Dif[0]; - UInt32 numMinDif = 0; - Dif[0] = 0; - for (i = 1; i < sizeof(Dif) / sizeof(Dif[0]); i++) - { - if (Dif[i] < minDif) - { - minDif = Dif[i]; - numMinDif = i; - } - Dif[i] = 0; - } - switch(numMinDif) - { - case 1: if (K1 >= -16) K1--; break; - case 2: if (K1 < 16) K1++; break; - case 3: if (K2 >= -16) K2--; break; - case 4: if (K2 < 16) K2++; break; - case 5: if (K3 >= -16) K3--; break; - case 6: if (K3 < 16) K3++; break; - case 7: if (K4 >= -16) K4--; break; - case 8: if (K4 < 16) K4++; break; - case 9: if (K5 >= -16) K5--; break; - case 10:if (K5 < 16) K5++; break; - } - } - return realValue; -} -} - -static const char *kNumberErrorMessage = "Number error"; - -static const UInt32 kHistorySize = 1 << 20; - -static const int kNumStats = 11; - -static const UInt32 kWindowReservSize = (1 << 22) + 256; - -CDecoder::CDecoder(): - m_IsSolid(false) -{ -} - -void CDecoder::InitStructures() -{ - m_MmFilter.Init(); - for(int i = 0; i < kNumRepDists; i++) - m_RepDists[i] = 0; - m_RepDistPtr = 0; - m_LastLength = 0; - memset(m_LastLevels, 0, kMaxTableSize); -} - -UInt32 CDecoder::ReadBits(int numBits) { return m_InBitStream.ReadBits(numBits); } - -#define RIF(x) { if (!(x)) return false; } - -bool CDecoder::ReadTables(void) -{ - Byte levelLevels[kLevelTableSize]; - Byte newLevels[kMaxTableSize]; - m_AudioMode = (ReadBits(1) == 1); - - if (ReadBits(1) == 0) - memset(m_LastLevels, 0, kMaxTableSize); - int numLevels; - if (m_AudioMode) - { - m_NumChannels = ReadBits(2) + 1; - if (m_MmFilter.CurrentChannel >= m_NumChannels) - m_MmFilter.CurrentChannel = 0; - numLevels = m_NumChannels * kMMTableSize; - } - else - numLevels = kHeapTablesSizesSum; - - int i; - for (i = 0; i < kLevelTableSize; i++) - levelLevels[i] = (Byte)ReadBits(4); - RIF(m_LevelDecoder.SetCodeLengths(levelLevels)); - i = 0; - while (i < numLevels) - { - UInt32 number = m_LevelDecoder.DecodeSymbol(&m_InBitStream); - if (number < kTableDirectLevels) - { - newLevels[i] = (Byte)((number + m_LastLevels[i]) & kLevelMask); - i++; - } - else - { - if (number == kTableLevelRepNumber) - { - int t = ReadBits(2) + 3; - for (int reps = t; reps > 0 && i < numLevels ; reps--, i++) - newLevels[i] = newLevels[i - 1]; - } - else - { - int num; - if (number == kTableLevel0Number) - num = ReadBits(3) + 3; - else if (number == kTableLevel0Number2) - num = ReadBits(7) + 11; - else - return false; - for (;num > 0 && i < numLevels; num--) - newLevels[i++] = 0; - } - } - } - if (m_AudioMode) - for (i = 0; i < m_NumChannels; i++) - { - RIF(m_MMDecoders[i].SetCodeLengths(&newLevels[i * kMMTableSize])); - } - else - { - RIF(m_MainDecoder.SetCodeLengths(&newLevels[0])); - RIF(m_DistDecoder.SetCodeLengths(&newLevels[kMainTableSize])); - RIF(m_LenDecoder.SetCodeLengths(&newLevels[kMainTableSize + kDistTableSize])); - } - memcpy(m_LastLevels, newLevels, kMaxTableSize); - return true; -} - -bool CDecoder::ReadLastTables() -{ - // it differs a little from pure RAR sources; - // UInt64 ttt = m_InBitStream.GetProcessedSize() + 2; - // + 2 works for: return 0xFF; in CInBuffer::ReadByte. - if (m_InBitStream.GetProcessedSize() + 7 <= m_PackSize) // test it: probably incorrect; - // if (m_InBitStream.GetProcessedSize() + 2 <= m_PackSize) // test it: probably incorrect; - if (m_AudioMode) - { - UInt32 symbol = m_MMDecoders[m_MmFilter.CurrentChannel].DecodeSymbol(&m_InBitStream); - if (symbol == 256) - return ReadTables(); - if (symbol >= kMMTableSize) - return false; - } - else - { - UInt32 number = m_MainDecoder.DecodeSymbol(&m_InBitStream); - if (number == kReadTableNumber) - return ReadTables(); - if (number >= kMainTableSize) - return false; - } - return true; -} - -class CCoderReleaser -{ - CDecoder *m_Coder; -public: - CCoderReleaser(CDecoder *coder): m_Coder(coder) {} - ~CCoderReleaser() - { - m_Coder->ReleaseStreams(); - } -}; - -bool CDecoder::DecodeMm(UInt32 pos) -{ - while (pos-- > 0) - { - UInt32 symbol = m_MMDecoders[m_MmFilter.CurrentChannel].DecodeSymbol(&m_InBitStream); - if (symbol == 256) - return true; - if (symbol >= kMMTableSize) - return false; - /* - Byte byPredict = m_Predictor.Predict(); - Byte byReal = (Byte)(byPredict - (Byte)symbol); - m_Predictor.Update(byReal, byPredict); - */ - Byte byReal = m_MmFilter.Decode((Byte)symbol); - m_OutWindowStream.PutByte(byReal); - if (++m_MmFilter.CurrentChannel == m_NumChannels) - m_MmFilter.CurrentChannel = 0; - } - return true; -} - -bool CDecoder::DecodeLz(Int32 pos) -{ - while (pos > 0) - { - UInt32 number = m_MainDecoder.DecodeSymbol(&m_InBitStream); - UInt32 length, distance; - if (number < 256) - { - m_OutWindowStream.PutByte(Byte(number)); - pos--; - continue; - } - else if (number >= kMatchNumber) - { - number -= kMatchNumber; - length = kNormalMatchMinLen + UInt32(kLenStart[number]) + - m_InBitStream.ReadBits(kLenDirectBits[number]); - number = m_DistDecoder.DecodeSymbol(&m_InBitStream); - if (number >= kDistTableSize) - return false; - distance = kDistStart[number] + m_InBitStream.ReadBits(kDistDirectBits[number]); - if (distance >= kDistLimit3) - { - length += 2 - ((distance - kDistLimit4) >> 31); - // length++; - // if (distance >= kDistLimit4) - // length++; - } - } - else if (number == kRepBothNumber) - { - length = m_LastLength; - if (length == 0) - return false; - distance = m_RepDists[(m_RepDistPtr + 4 - 1) & 3]; - } - else if (number < kLen2Number) - { - distance = m_RepDists[(m_RepDistPtr - (number - kRepNumber + 1)) & 3]; - number = m_LenDecoder.DecodeSymbol(&m_InBitStream); - if (number >= kLenTableSize) - return false; - length = 2 + kLenStart[number] + m_InBitStream.ReadBits(kLenDirectBits[number]); - if (distance >= kDistLimit2) - { - length++; - if (distance >= kDistLimit3) - { - length += 2 - ((distance - kDistLimit4) >> 31); - // length++; - // if (distance >= kDistLimit4) - // length++; - } - } - } - else if (number < kReadTableNumber) - { - number -= kLen2Number; - distance = kLen2DistStarts[number] + - m_InBitStream.ReadBits(kLen2DistDirectBits[number]); - length = 2; - } - else if (number == kReadTableNumber) - return true; - else - return false; - m_RepDists[m_RepDistPtr++ & 3] = distance; - m_LastLength = length; - if (!m_OutWindowStream.CopyBlock(distance, length)) - return false; - pos -= length; - } - return true; -} - -HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - if (inSize == NULL || outSize == NULL) - return E_INVALIDARG; - - if (!m_OutWindowStream.Create(kHistorySize)) - return E_OUTOFMEMORY; - if (!m_InBitStream.Create(1 << 20)) - return E_OUTOFMEMORY; - - m_PackSize = *inSize; - - UInt64 pos = 0, unPackSize = *outSize; - - m_OutWindowStream.SetStream(outStream); - m_OutWindowStream.Init(m_IsSolid); - m_InBitStream.SetStream(inStream); - m_InBitStream.Init(); - - CCoderReleaser coderReleaser(this); - if (!m_IsSolid) - { - InitStructures(); - if (unPackSize == 0) - { - if (m_InBitStream.GetProcessedSize() + 2 <= m_PackSize) // test it: probably incorrect; - if (!ReadTables()) - return S_FALSE; - return S_OK; - } - if (!ReadTables()) - return S_FALSE; - } - - UInt64 startPos = m_OutWindowStream.GetProcessedSize(); - while(pos < unPackSize) - { - UInt32 blockSize = 1 << 20; - if (blockSize > unPackSize - pos) - blockSize = (UInt32)(unPackSize - pos); - UInt64 blockStartPos = m_OutWindowStream.GetProcessedSize(); - if (m_AudioMode) - { - if (!DecodeMm(blockSize)) - return S_FALSE; - } - else - { - if (!DecodeLz((Int32)blockSize)) - return S_FALSE; - } - UInt64 globalPos = m_OutWindowStream.GetProcessedSize(); - pos = globalPos - blockStartPos; - if (pos < blockSize) - if (!ReadTables()) - return S_FALSE; - pos = globalPos - startPos; - if (progress != 0) - { - UInt64 packSize = m_InBitStream.GetProcessedSize(); - RINOK(progress->SetRatioInfo(&packSize, &pos)); - } - } - if (pos > unPackSize) - return S_FALSE; - - if (!ReadLastTables()) - return S_FALSE; - return m_OutWindowStream.Flush(); -} - -STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - try { return CodeReal(inStream, outStream, inSize, outSize, progress); } - catch(const CInBufferException &e) { return e.ErrorCode; } - catch(const CLzOutWindowException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } -} - -STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size) -{ - if (size < 1) - return E_INVALIDARG; - m_IsSolid = (data[0] != 0); - return S_OK; -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Rar2Decoder.h b/src/libs/7zip/win/CPP/7zip/Compress/Rar2Decoder.h deleted file mode 100644 index 61a8b4dab..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Rar2Decoder.h +++ /dev/null @@ -1,174 +0,0 @@ -// Rar2Decoder.h -// According to unRAR license, this code may not be used to develop -// a program that creates RAR archives - -#ifndef __COMPRESS_RAR2_DECODER_H -#define __COMPRESS_RAR2_DECODER_H - -#include "../../Common/MyCom.h" - -#include "../ICoder.h" - -#include "../Common/InBuffer.h" - -#include "BitmDecoder.h" -#include "HuffmanDecoder.h" -#include "LzOutWindow.h" - -namespace NCompress { -namespace NRar2 { - -const UInt32 kNumRepDists = 4; -const UInt32 kDistTableSize = 48; - -const int kMMTableSize = 256 + 1; - -const UInt32 kMainTableSize = 298; -const UInt32 kLenTableSize = 28; - -const UInt32 kDistTableStart = kMainTableSize; -const UInt32 kLenTableStart = kDistTableStart + kDistTableSize; - -const UInt32 kHeapTablesSizesSum = kMainTableSize + kDistTableSize + kLenTableSize; - -const UInt32 kLevelTableSize = 19; - -const UInt32 kMMTablesSizesSum = kMMTableSize * 4; - -const UInt32 kMaxTableSize = kMMTablesSizesSum; - -const UInt32 kTableDirectLevels = 16; -const UInt32 kTableLevelRepNumber = kTableDirectLevels; -const UInt32 kTableLevel0Number = kTableLevelRepNumber + 1; -const UInt32 kTableLevel0Number2 = kTableLevel0Number + 1; - -const UInt32 kLevelMask = 0xF; - - -const UInt32 kRepBothNumber = 256; -const UInt32 kRepNumber = kRepBothNumber + 1; -const UInt32 kLen2Number = kRepNumber + 4; - -const UInt32 kLen2NumNumbers = 8; -const UInt32 kReadTableNumber = kLen2Number + kLen2NumNumbers; -const UInt32 kMatchNumber = kReadTableNumber + 1; - -const Byte kLenStart[kLenTableSize] = {0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224}; -const Byte kLenDirectBits[kLenTableSize] = {0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5}; - -const UInt32 kDistStart[kDistTableSize] = {0,1,2,3,4,6,8,12,16,24,32,48,64,96,128,192,256,384,512,768,1024,1536,2048,3072,4096,6144,8192,12288,16384,24576,32768U,49152U,65536,98304,131072,196608,262144,327680,393216,458752,524288,589824,655360,720896,786432,851968,917504,983040}; -const Byte kDistDirectBits[kDistTableSize] = {0,0,0,0,1,1,2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16}; - -const Byte kLevelDirectBits[kLevelTableSize] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7}; - -const Byte kLen2DistStarts[kLen2NumNumbers]={0,4,8,16,32,64,128,192}; -const Byte kLen2DistDirectBits[kLen2NumNumbers]={2,2,3, 4, 5, 6, 6, 6}; - -const UInt32 kDistLimit2 = 0x101 - 1; -const UInt32 kDistLimit3 = 0x2000 - 1; -const UInt32 kDistLimit4 = 0x40000 - 1; - -const UInt32 kMatchMaxLen = 255 + 2; -const UInt32 kMatchMaxLenMax = 255 + 5; -const UInt32 kNormalMatchMinLen = 3; - -namespace NMultimedia { - -struct CFilter -{ - int K1,K2,K3,K4,K5; - int D1,D2,D3,D4; - int LastDelta; - UInt32 Dif[11]; - UInt32 ByteCount; - int LastChar; - - Byte Decode(int &channelDelta, Byte delta); - - void Init() { memset(this, 0, sizeof(*this)); } - -}; - -const int kNumChanelsMax = 4; - -class CFilter2 -{ -public: - CFilter m_Filters[kNumChanelsMax]; - int m_ChannelDelta; - int CurrentChannel; - - void Init() { memset(this, 0, sizeof(*this)); } - Byte Decode(Byte delta) - { - return m_Filters[CurrentChannel].Decode(m_ChannelDelta, delta); - } - -}; - -} - -typedef NBitm::CDecoder<CInBuffer> CBitDecoder; - -const int kNumHuffmanBits = 15; - -class CDecoder : - public ICompressCoder, - public ICompressSetDecoderProperties2, - public CMyUnknownImp -{ - CLzOutWindow m_OutWindowStream; - CBitDecoder m_InBitStream; - NHuffman::CDecoder<kNumHuffmanBits, kMainTableSize> m_MainDecoder; - NHuffman::CDecoder<kNumHuffmanBits, kDistTableSize> m_DistDecoder; - NHuffman::CDecoder<kNumHuffmanBits, kLenTableSize> m_LenDecoder; - NHuffman::CDecoder<kNumHuffmanBits, kMMTableSize> m_MMDecoders[NMultimedia::kNumChanelsMax]; - NHuffman::CDecoder<kNumHuffmanBits, kLevelTableSize> m_LevelDecoder; - - bool m_AudioMode; - - NMultimedia::CFilter2 m_MmFilter; - int m_NumChannels; - - UInt32 m_RepDists[kNumRepDists]; - UInt32 m_RepDistPtr; - - UInt32 m_LastLength; - - Byte m_LastLevels[kMaxTableSize]; - - UInt64 m_PackSize; - bool m_IsSolid; - - void InitStructures(); - UInt32 ReadBits(int numBits); - bool ReadTables(); - bool ReadLastTables(); - - bool DecodeMm(UInt32 pos); - bool DecodeLz(Int32 pos); - - HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - -public: - CDecoder(); - - MY_UNKNOWN_IMP1(ICompressSetDecoderProperties2) - - void ReleaseStreams() - { - m_OutWindowStream.ReleaseStream(); - m_InBitStream.ReleaseStream(); - } - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); - -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Rar3Decoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/Rar3Decoder.cpp deleted file mode 100644 index dde7c6de3..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Rar3Decoder.cpp +++ /dev/null @@ -1,897 +0,0 @@ -// Rar3Decoder.cpp -// According to unRAR license, this code may not be used to develop -// a program that creates RAR archives - -/* This code uses Carryless rangecoder (1999): Dmitry Subbotin : Public domain */ - -#include "StdAfx.h" - -#include "../../../C/Alloc.h" - -#include "../Common/StreamUtils.h" - -#include "Rar3Decoder.h" - -namespace NCompress { -namespace NRar3 { - -static void *SzBigAlloc(void *, size_t size) { return BigAlloc(size); } -static void SzBigFree(void *, void *address) { BigFree(address); } -static ISzAlloc g_BigAlloc = { SzBigAlloc, SzBigFree }; - -static const UInt32 kNumAlignReps = 15; - -static const UInt32 kSymbolReadTable = 256; -static const UInt32 kSymbolRep = 259; -static const UInt32 kSymbolLen2 = kSymbolRep + kNumReps; - -static const Byte kLenStart[kLenTableSize] = {0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224}; -static const Byte kLenDirectBits[kLenTableSize] = {0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5}; - -static const Byte kDistDirectBits[kDistTableSize] = - {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15, - 16,16,16,16,16,16,16,16,16,16,16,16,16,16, - 18,18,18,18,18,18,18,18,18,18,18,18}; - -static const Byte kLevelDirectBits[kLevelTableSize] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}; - -static const Byte kLen2DistStarts[kNumLen2Symbols]={0,4,8,16,32,64,128,192}; -static const Byte kLen2DistDirectBits[kNumLen2Symbols]={2,2,3, 4, 5, 6, 6, 6}; - -static const UInt32 kDistLimit3 = 0x2000 - 2; -static const UInt32 kDistLimit4 = 0x40000 - 2; - -static const UInt32 kNormalMatchMinLen = 3; - -static const UInt32 kVmDataSizeMax = 1 << 16; -static const UInt32 kVmCodeSizeMax = 1 << 16; - -extern "C" { - -static UInt32 Range_GetThreshold(void *pp, UInt32 total) -{ - CRangeDecoder *p = (CRangeDecoder *)pp; - return p->Code / (p->Range /= total); -} - -static void Range_Decode(void *pp, UInt32 start, UInt32 size) -{ - CRangeDecoder *p = (CRangeDecoder *)pp; - start *= p->Range; - p->Low += start; - p->Code -= start; - p->Range *= size; - p->Normalize(); -} - -static UInt32 Range_DecodeBit(void *pp, UInt32 size0) -{ - CRangeDecoder *p = (CRangeDecoder *)pp; - if (p->Code / (p->Range >>= 14) < size0) - { - Range_Decode(p, 0, size0); - return 0; - } - else - { - Range_Decode(p, size0, (1 << 14) - size0); - return 1; - } -} - -} - -CRangeDecoder::CRangeDecoder() -{ - s.GetThreshold = Range_GetThreshold; - s.Decode = Range_Decode; - s.DecodeBit = Range_DecodeBit; -} - -CDecoder::CDecoder(): - _window(0), - _winPos(0), - _wrPtr(0), - _lzSize(0), - _writtenFileSize(0), - _vmData(0), - _vmCode(0), - m_IsSolid(false) -{ - Ppmd7_Construct(&_ppmd); -} - -CDecoder::~CDecoder() -{ - InitFilters(); - ::MidFree(_vmData); - ::MidFree(_window); - Ppmd7_Free(&_ppmd, &g_BigAlloc); -} - -HRESULT CDecoder::WriteDataToStream(const Byte *data, UInt32 size) -{ - return WriteStream(_outStream, data, size); -} - -HRESULT CDecoder::WriteData(const Byte *data, UInt32 size) -{ - HRESULT res = S_OK; - if (_writtenFileSize < _unpackSize) - { - UInt32 curSize = size; - UInt64 remain = _unpackSize - _writtenFileSize; - if (remain < curSize) - curSize = (UInt32)remain; - res = WriteDataToStream(data, curSize); - } - _writtenFileSize += size; - return res; -} - -HRESULT CDecoder::WriteArea(UInt32 startPtr, UInt32 endPtr) -{ - if (startPtr <= endPtr) - return WriteData(_window + startPtr, endPtr - startPtr); - RINOK(WriteData(_window + startPtr, kWindowSize - startPtr)); - return WriteData(_window, endPtr); -} - -void CDecoder::ExecuteFilter(int tempFilterIndex, NVm::CBlockRef &outBlockRef) -{ - CTempFilter *tempFilter = _tempFilters[tempFilterIndex]; - tempFilter->InitR[6] = (UInt32)_writtenFileSize; - NVm::SetValue32(&tempFilter->GlobalData[0x24], (UInt32)_writtenFileSize); - NVm::SetValue32(&tempFilter->GlobalData[0x28], (UInt32)(_writtenFileSize >> 32)); - CFilter *filter = _filters[tempFilter->FilterIndex]; - _vm.Execute(filter, tempFilter, outBlockRef, filter->GlobalData); - delete tempFilter; - _tempFilters[tempFilterIndex] = 0; -} - -HRESULT CDecoder::WriteBuf() -{ - UInt32 writtenBorder = _wrPtr; - UInt32 writeSize = (_winPos - writtenBorder) & kWindowMask; - for (int i = 0; i < _tempFilters.Size(); i++) - { - CTempFilter *filter = _tempFilters[i]; - if (filter == NULL) - continue; - if (filter->NextWindow) - { - filter->NextWindow = false; - continue; - } - UInt32 blockStart = filter->BlockStart; - UInt32 blockSize = filter->BlockSize; - if (((blockStart - writtenBorder) & kWindowMask) < writeSize) - { - if (writtenBorder != blockStart) - { - RINOK(WriteArea(writtenBorder, blockStart)); - writtenBorder = blockStart; - writeSize = (_winPos - writtenBorder) & kWindowMask; - } - if (blockSize <= writeSize) - { - UInt32 blockEnd = (blockStart + blockSize) & kWindowMask; - if (blockStart < blockEnd || blockEnd == 0) - _vm.SetMemory(0, _window + blockStart, blockSize); - else - { - UInt32 tailSize = kWindowSize - blockStart; - _vm.SetMemory(0, _window + blockStart, tailSize); - _vm.SetMemory(tailSize, _window, blockEnd); - } - NVm::CBlockRef outBlockRef; - ExecuteFilter(i, outBlockRef); - while (i + 1 < _tempFilters.Size()) - { - CTempFilter *nextFilter = _tempFilters[i + 1]; - if (nextFilter == NULL || nextFilter->BlockStart != blockStart || - nextFilter->BlockSize != outBlockRef.Size || nextFilter->NextWindow) - break; - _vm.SetMemory(0, _vm.GetDataPointer(outBlockRef.Offset), outBlockRef.Size); - ExecuteFilter(++i, outBlockRef); - } - WriteDataToStream(_vm.GetDataPointer(outBlockRef.Offset), outBlockRef.Size); - _writtenFileSize += outBlockRef.Size; - writtenBorder = blockEnd; - writeSize = (_winPos - writtenBorder) & kWindowMask; - } - else - { - for (int j = i; j < _tempFilters.Size(); j++) - { - CTempFilter *filter = _tempFilters[j]; - if (filter != NULL && filter->NextWindow) - filter->NextWindow = false; - } - _wrPtr = writtenBorder; - return S_OK; // check it - } - } - } - - _wrPtr = _winPos; - return WriteArea(writtenBorder, _winPos); -} - -void CDecoder::InitFilters() -{ - _lastFilter = 0; - int i; - for (i = 0; i < _tempFilters.Size(); i++) - delete _tempFilters[i]; - _tempFilters.Clear(); - for (i = 0; i < _filters.Size(); i++) - delete _filters[i]; - _filters.Clear(); -} - -bool CDecoder::AddVmCode(UInt32 firstByte, UInt32 codeSize) -{ - CMemBitDecoder inp; - inp.Init(_vmData, codeSize); - - UInt32 filterIndex; - if (firstByte & 0x80) - { - filterIndex = NVm::ReadEncodedUInt32(inp); - if (filterIndex == 0) - InitFilters(); - else - filterIndex--; - } - else - filterIndex = _lastFilter; - if (filterIndex > (UInt32)_filters.Size()) - return false; - _lastFilter = filterIndex; - bool newFilter = (filterIndex == (UInt32)_filters.Size()); - - CFilter *filter; - if (newFilter) - { - // check if too many filters - if (filterIndex > 1024) - return false; - filter = new CFilter; - _filters.Add(filter); - } - else - { - filter = _filters[filterIndex]; - filter->ExecCount++; - } - - int numEmptyItems = 0; - int i; - for (i = 0; i < _tempFilters.Size(); i++) - { - _tempFilters[i - numEmptyItems] = _tempFilters[i]; - if (_tempFilters[i] == NULL) - numEmptyItems++; - if (numEmptyItems > 0) - _tempFilters[i] = NULL; - } - if (numEmptyItems == 0) - { - _tempFilters.Add(NULL); - numEmptyItems = 1; - } - CTempFilter *tempFilter = new CTempFilter; - _tempFilters[_tempFilters.Size() - numEmptyItems] = tempFilter; - tempFilter->FilterIndex = filterIndex; - tempFilter->ExecCount = filter->ExecCount; - - UInt32 blockStart = NVm::ReadEncodedUInt32(inp); - if (firstByte & 0x40) - blockStart += 258; - tempFilter->BlockStart = (blockStart + _winPos) & kWindowMask; - if (firstByte & 0x20) - filter->BlockSize = NVm::ReadEncodedUInt32(inp); - tempFilter->BlockSize = filter->BlockSize; - tempFilter->NextWindow = _wrPtr != _winPos && ((_wrPtr - _winPos) & kWindowMask) <= blockStart; - - memset(tempFilter->InitR, 0, sizeof(tempFilter->InitR)); - tempFilter->InitR[3] = NVm::kGlobalOffset; - tempFilter->InitR[4] = tempFilter->BlockSize; - tempFilter->InitR[5] = tempFilter->ExecCount; - if (firstByte & 0x10) - { - UInt32 initMask = inp.ReadBits(NVm::kNumGpRegs); - for (int i = 0; i < NVm::kNumGpRegs; i++) - if (initMask & (1 << i)) - tempFilter->InitR[i] = NVm::ReadEncodedUInt32(inp); - } - if (newFilter) - { - UInt32 vmCodeSize = NVm::ReadEncodedUInt32(inp); - if (vmCodeSize >= kVmCodeSizeMax || vmCodeSize == 0) - return false; - for (UInt32 i = 0; i < vmCodeSize; i++) - _vmCode[i] = (Byte)inp.ReadBits(8); - _vm.PrepareProgram(_vmCode, vmCodeSize, filter); - } - - tempFilter->AllocateEmptyFixedGlobal(); - - Byte *globalData = &tempFilter->GlobalData[0]; - for (i = 0; i < NVm::kNumGpRegs; i++) - NVm::SetValue32(&globalData[i * 4], tempFilter->InitR[i]); - NVm::SetValue32(&globalData[NVm::NGlobalOffset::kBlockSize], tempFilter->BlockSize); - NVm::SetValue32(&globalData[NVm::NGlobalOffset::kBlockPos], 0); // It was commented. why? - NVm::SetValue32(&globalData[NVm::NGlobalOffset::kExecCount], tempFilter->ExecCount); - - if (firstByte & 8) - { - UInt32 dataSize = NVm::ReadEncodedUInt32(inp); - if (dataSize > NVm::kGlobalSize - NVm::kFixedGlobalSize) - return false; - CRecordVector<Byte> &globalData = tempFilter->GlobalData; - int requredSize = (int)(dataSize + NVm::kFixedGlobalSize); - if (globalData.Size() < requredSize) - { - globalData.Reserve(requredSize); - for (; globalData.Size() < requredSize; i++) - globalData.Add(0); - } - for (UInt32 i = 0; i < dataSize; i++) - globalData[NVm::kFixedGlobalSize + i] = (Byte)inp.ReadBits(8); - } - return true; -} - -bool CDecoder::ReadVmCodeLZ() -{ - UInt32 firstByte = ReadBits(8); - UInt32 length = (firstByte & 7) + 1; - if (length == 7) - length = ReadBits(8) + 7; - else if (length == 8) - length = ReadBits(16); - if (length > kVmDataSizeMax) - return false; - for (UInt32 i = 0; i < length; i++) - _vmData[i] = (Byte)ReadBits(8); - return AddVmCode(firstByte, length); -} - -bool CDecoder::ReadVmCodePPM() -{ - int firstByte = DecodePpmSymbol(); - if (firstByte < 0) - return false; - UInt32 length = (firstByte & 7) + 1; - if (length == 7) - { - int b1 = DecodePpmSymbol(); - if (b1 < 0) - return false; - length = b1 + 7; - } - else if (length == 8) - { - int b1 = DecodePpmSymbol(); - if (b1 < 0) - return false; - int b2 = DecodePpmSymbol(); - if (b2 < 0) - return false; - length = b1 * 256 + b2; - } - if (length > kVmDataSizeMax) - return false; - for (UInt32 i = 0; i < length; i++) - { - int b = DecodePpmSymbol(); - if (b < 0) - return false; - _vmData[i] = (Byte)b; - } - return AddVmCode(firstByte, length); -} - -#define RIF(x) { if (!(x)) return S_FALSE; } - -UInt32 CDecoder::ReadBits(int numBits) { return m_InBitStream.bitDecoder.ReadBits(numBits); } - -///////////////////////////////////////////////// -// PPM - -HRESULT CDecoder::InitPPM() -{ - Byte maxOrder = (Byte)ReadBits(7); - - bool reset = ((maxOrder & 0x20) != 0); - int maxMB = 0; - if (reset) - maxMB = (Byte)ReadBits(8); - else - { - if (PpmError || !Ppmd7_WasAllocated(&_ppmd)) - return S_FALSE; - } - if (maxOrder & 0x40) - PpmEscChar = (Byte)ReadBits(8); - m_InBitStream.InitRangeCoder(); - /* - if (m_InBitStream.m_BitPos != 0) - return S_FALSE; - */ - if (reset) - { - PpmError = true; - maxOrder = (maxOrder & 0x1F) + 1; - if (maxOrder > 16) - maxOrder = 16 + (maxOrder - 16) * 3; - if (maxOrder == 1) - { - Ppmd7_Free(&_ppmd, &g_BigAlloc); - return S_FALSE; - } - if (!Ppmd7_Alloc(&_ppmd, (maxMB + 1) << 20, &g_BigAlloc)) - return E_OUTOFMEMORY; - Ppmd7_Init(&_ppmd, maxOrder); - PpmError = false; - } - return S_OK; -} - -int CDecoder::DecodePpmSymbol() { return Ppmd7_DecodeSymbol(&_ppmd, &m_InBitStream.s); } - -HRESULT CDecoder::DecodePPM(Int32 num, bool &keepDecompressing) -{ - keepDecompressing = false; - if (PpmError) - return S_FALSE; - do - { - if (((_wrPtr - _winPos) & kWindowMask) < 260 && _wrPtr != _winPos) - { - RINOK(WriteBuf()); - if (_writtenFileSize > _unpackSize) - { - keepDecompressing = false; - return S_OK; - } - } - int c = DecodePpmSymbol(); - if (c < 0) - { - PpmError = true; - return S_FALSE; - } - if (c == PpmEscChar) - { - int nextCh = DecodePpmSymbol(); - if (nextCh < 0) - { - PpmError = true; - return S_FALSE; - } - if (nextCh == 0) - return ReadTables(keepDecompressing); - if (nextCh == 2 || nextCh == -1) - return S_OK; - if (nextCh == 3) - { - if (!ReadVmCodePPM()) - { - PpmError = true; - return S_FALSE; - } - continue; - } - if (nextCh == 4 || nextCh == 5) - { - UInt32 distance = 0; - UInt32 length = 4; - if (nextCh == 4) - { - for (int i = 0; i < 3; i++) - { - int c = DecodePpmSymbol(); - if (c < 0) - { - PpmError = true; - return S_FALSE; - } - distance = (distance << 8) + (Byte)c; - } - distance++; - length += 28; - } - int c = DecodePpmSymbol(); - if (c < 0) - { - PpmError = true; - return S_FALSE; - } - length += c; - if (distance >= _lzSize) - return S_FALSE; - CopyBlock(distance, length); - num -= (Int32)length; - continue; - } - } - PutByte((Byte)c); - num--; - } - while (num >= 0); - keepDecompressing = true; - return S_OK; -} - -///////////////////////////////////////////////// -// LZ - -HRESULT CDecoder::ReadTables(bool &keepDecompressing) -{ - keepDecompressing = true; - ReadBits((8 - m_InBitStream.bitDecoder.GetBitPosition()) & 7); - if (ReadBits(1) != 0) - { - _lzMode = false; - return InitPPM(); - } - - _lzMode = true; - PrevAlignBits = 0; - PrevAlignCount = 0; - - Byte levelLevels[kLevelTableSize]; - Byte newLevels[kTablesSizesSum]; - - if (ReadBits(1) == 0) - memset(m_LastLevels, 0, kTablesSizesSum); - - int i; - for (i = 0; i < kLevelTableSize; i++) - { - UInt32 length = ReadBits(4); - if (length == 15) - { - UInt32 zeroCount = ReadBits(4); - if (zeroCount != 0) - { - zeroCount += 2; - while (zeroCount-- > 0 && i < kLevelTableSize) - levelLevels[i++]=0; - i--; - continue; - } - } - levelLevels[i] = (Byte)length; - } - RIF(m_LevelDecoder.SetCodeLengths(levelLevels)); - i = 0; - while (i < kTablesSizesSum) - { - UInt32 number = m_LevelDecoder.DecodeSymbol(&m_InBitStream.bitDecoder); - if (number < 16) - { - newLevels[i] = Byte((number + m_LastLevels[i]) & 15); - i++; - } - else if (number > kLevelTableSize) - return S_FALSE; - else - { - int num; - if (((number - 16) & 1) == 0) - num = ReadBits(3) + 3; - else - num = ReadBits(7) + 11; - if (number < 18) - { - if (i == 0) - return S_FALSE; - for (; num > 0 && i < kTablesSizesSum; num--, i++) - newLevels[i] = newLevels[i - 1]; - } - else - { - for (; num > 0 && i < kTablesSizesSum; num--) - newLevels[i++] = 0; - } - } - } - TablesRead = true; - - // original code has check here: - /* - if (InAddr > ReadTop) - { - keepDecompressing = false; - return true; - } - */ - - RIF(m_MainDecoder.SetCodeLengths(&newLevels[0])); - RIF(m_DistDecoder.SetCodeLengths(&newLevels[kMainTableSize])); - RIF(m_AlignDecoder.SetCodeLengths(&newLevels[kMainTableSize + kDistTableSize])); - RIF(m_LenDecoder.SetCodeLengths(&newLevels[kMainTableSize + kDistTableSize + kAlignTableSize])); - - memcpy(m_LastLevels, newLevels, kTablesSizesSum); - return S_OK; -} - -class CCoderReleaser -{ - CDecoder *m_Coder; -public: - CCoderReleaser(CDecoder *coder): m_Coder(coder) {} - ~CCoderReleaser() - { - m_Coder->ReleaseStreams(); - } -}; - -HRESULT CDecoder::ReadEndOfBlock(bool &keepDecompressing) -{ - if (ReadBits(1) != 0) - { - // old file - TablesRead = false; - return ReadTables(keepDecompressing); - } - // new file - keepDecompressing = false; - TablesRead = (ReadBits(1) == 0); - return S_OK; -} - -UInt32 kDistStart[kDistTableSize]; - -class CDistInit -{ -public: - CDistInit() { Init(); } - void Init() - { - UInt32 start = 0; - for (UInt32 i = 0; i < kDistTableSize; i++) - { - kDistStart[i] = start; - start += (1 << kDistDirectBits[i]); - } - } -} g_DistInit; - -HRESULT CDecoder::DecodeLZ(bool &keepDecompressing) -{ - UInt32 rep0 = _reps[0]; - UInt32 rep1 = _reps[1]; - UInt32 rep2 = _reps[2]; - UInt32 rep3 = _reps[3]; - UInt32 length = _lastLength; - for (;;) - { - if (((_wrPtr - _winPos) & kWindowMask) < 260 && _wrPtr != _winPos) - { - RINOK(WriteBuf()); - if (_writtenFileSize > _unpackSize) - { - keepDecompressing = false; - return S_OK; - } - } - UInt32 number = m_MainDecoder.DecodeSymbol(&m_InBitStream.bitDecoder); - if (number < 256) - { - PutByte((Byte)number); - continue; - } - else if (number == kSymbolReadTable) - { - RINOK(ReadEndOfBlock(keepDecompressing)); - break; - } - else if (number == 257) - { - if (!ReadVmCodeLZ()) - return S_FALSE; - continue; - } - else if (number == 258) - { - if (length == 0) - return S_FALSE; - } - else if (number < kSymbolRep + 4) - { - if (number != kSymbolRep) - { - UInt32 distance; - if (number == kSymbolRep + 1) - distance = rep1; - else - { - if (number == kSymbolRep + 2) - distance = rep2; - else - { - distance = rep3; - rep3 = rep2; - } - rep2 = rep1; - } - rep1 = rep0; - rep0 = distance; - } - - UInt32 number = m_LenDecoder.DecodeSymbol(&m_InBitStream.bitDecoder); - if (number >= kLenTableSize) - return S_FALSE; - length = 2 + kLenStart[number] + m_InBitStream.bitDecoder.ReadBits(kLenDirectBits[number]); - } - else - { - rep3 = rep2; - rep2 = rep1; - rep1 = rep0; - if (number < 271) - { - number -= 263; - rep0 = kLen2DistStarts[number] + m_InBitStream.bitDecoder.ReadBits(kLen2DistDirectBits[number]); - length = 2; - } - else if (number < 299) - { - number -= 271; - length = kNormalMatchMinLen + (UInt32)kLenStart[number] + m_InBitStream.bitDecoder.ReadBits(kLenDirectBits[number]); - UInt32 number = m_DistDecoder.DecodeSymbol(&m_InBitStream.bitDecoder); - if (number >= kDistTableSize) - return S_FALSE; - rep0 = kDistStart[number]; - int numBits = kDistDirectBits[number]; - if (number >= (kNumAlignBits * 2) + 2) - { - if (numBits > kNumAlignBits) - rep0 += (m_InBitStream.bitDecoder.ReadBits(numBits - kNumAlignBits) << kNumAlignBits); - if (PrevAlignCount > 0) - { - PrevAlignCount--; - rep0 += PrevAlignBits; - } - else - { - UInt32 number = m_AlignDecoder.DecodeSymbol(&m_InBitStream.bitDecoder); - if (number < (1 << kNumAlignBits)) - { - rep0 += number; - PrevAlignBits = number; - } - else if (number == (1 << kNumAlignBits)) - { - PrevAlignCount = kNumAlignReps; - rep0 += PrevAlignBits; - } - else - return S_FALSE; - } - } - else - rep0 += m_InBitStream.bitDecoder.ReadBits(numBits); - length += ((kDistLimit4 - rep0) >> 31) + ((kDistLimit3 - rep0) >> 31); - } - else - return S_FALSE; - } - if (rep0 >= _lzSize) - return S_FALSE; - CopyBlock(rep0, length); - } - _reps[0] = rep0; - _reps[1] = rep1; - _reps[2] = rep2; - _reps[3] = rep3; - _lastLength = length; - - return S_OK; -} - -HRESULT CDecoder::CodeReal(ICompressProgressInfo *progress) -{ - _writtenFileSize = 0; - if (!m_IsSolid) - { - _lzSize = 0; - _winPos = 0; - _wrPtr = 0; - for (int i = 0; i < kNumReps; i++) - _reps[i] = 0; - _lastLength = 0; - memset(m_LastLevels, 0, kTablesSizesSum); - TablesRead = false; - PpmEscChar = 2; - PpmError = true; - InitFilters(); - } - if (!m_IsSolid || !TablesRead) - { - bool keepDecompressing; - RINOK(ReadTables(keepDecompressing)); - if (!keepDecompressing) - return S_OK; - } - - for (;;) - { - bool keepDecompressing; - if (_lzMode) - { - RINOK(DecodeLZ(keepDecompressing)) - } - else - { - RINOK(DecodePPM(1 << 18, keepDecompressing)) - } - UInt64 packSize = m_InBitStream.bitDecoder.GetProcessedSize(); - RINOK(progress->SetRatioInfo(&packSize, &_writtenFileSize)); - if (!keepDecompressing) - break; - } - RINOK(WriteBuf()); - UInt64 packSize = m_InBitStream.bitDecoder.GetProcessedSize(); - RINOK(progress->SetRatioInfo(&packSize, &_writtenFileSize)); - if (_writtenFileSize < _unpackSize) - return S_FALSE; - return S_OK; -} - -STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - try - { - if (inSize == NULL || outSize == NULL) - return E_INVALIDARG; - - if (_vmData == 0) - { - _vmData = (Byte *)::MidAlloc(kVmDataSizeMax + kVmCodeSizeMax); - if (_vmData == 0) - return E_OUTOFMEMORY; - _vmCode = _vmData + kVmDataSizeMax; - } - - if (_window == 0) - { - _window = (Byte *)::MidAlloc(kWindowSize); - if (_window == 0) - return E_OUTOFMEMORY; - } - if (!m_InBitStream.bitDecoder.Create(1 << 20)) - return E_OUTOFMEMORY; - if (!_vm.Create()) - return E_OUTOFMEMORY; - - - m_InBitStream.bitDecoder.SetStream(inStream); - m_InBitStream.bitDecoder.Init(); - _outStream = outStream; - - CCoderReleaser coderReleaser(this); - _unpackSize = *outSize; - return CodeReal(progress); - } - catch(const CInBufferException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } - // CNewException is possible here. But probably CNewException is caused - // by error in data stream. -} - -STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size) -{ - if (size < 1) - return E_INVALIDARG; - m_IsSolid = (data[0] != 0); - return S_OK; -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Rar3Decoder.h b/src/libs/7zip/win/CPP/7zip/Compress/Rar3Decoder.h deleted file mode 100644 index 99b647dc7..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Rar3Decoder.h +++ /dev/null @@ -1,267 +0,0 @@ -// Rar3Decoder.h -// According to unRAR license, this code may not be used to develop -// a program that creates RAR archives - -/* This code uses Carryless rangecoder (1999): Dmitry Subbotin : Public domain */ - -#ifndef __COMPRESS_RAR3_DECODER_H -#define __COMPRESS_RAR3_DECODER_H - -#include "../../../C/Ppmd7.h" - -#include "../../Common/MyCom.h" - -#include "../ICoder.h" - -#include "../Common/InBuffer.h" - -#include "BitmDecoder.h" -#include "HuffmanDecoder.h" -#include "Rar3Vm.h" - -namespace NCompress { -namespace NRar3 { - -const UInt32 kWindowSize = 1 << 22; -const UInt32 kWindowMask = (kWindowSize - 1); - -const UInt32 kNumReps = 4; -const UInt32 kNumLen2Symbols = 8; -const UInt32 kLenTableSize = 28; -const UInt32 kMainTableSize = 256 + 1 + 1 + 1 + kNumReps + kNumLen2Symbols + kLenTableSize; -const UInt32 kDistTableSize = 60; - -const int kNumAlignBits = 4; -const UInt32 kAlignTableSize = (1 << kNumAlignBits) + 1; - -const UInt32 kLevelTableSize = 20; - -const UInt32 kTablesSizesSum = kMainTableSize + kDistTableSize + kAlignTableSize + kLenTableSize; - -class CBitDecoder -{ - UInt32 m_Value; - unsigned m_BitPos; -public: - CInBuffer m_Stream; - bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } - void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream);} - void ReleaseStream() { m_Stream.ReleaseStream();} - - void Init() - { - m_Stream.Init(); - m_BitPos = 0; - m_Value = 0; - } - - UInt64 GetProcessedSize() const { return m_Stream.GetProcessedSize() - (m_BitPos) / 8; } - UInt32 GetBitPosition() const { return ((8 - m_BitPos) & 7); } - - UInt32 GetValue(unsigned numBits) - { - if (m_BitPos < numBits) - { - m_BitPos += 8; - m_Value = (m_Value << 8) | m_Stream.ReadByte(); - if (m_BitPos < numBits) - { - m_BitPos += 8; - m_Value = (m_Value << 8) | m_Stream.ReadByte(); - } - } - return m_Value >> (m_BitPos - numBits); - } - - void MovePos(unsigned numBits) - { - m_BitPos -= numBits; - m_Value = m_Value & ((1 << m_BitPos) - 1); - } - - UInt32 ReadBits(unsigned numBits) - { - UInt32 res = GetValue(numBits); - MovePos(numBits); - return res; - } -}; - -const UInt32 kTopValue = (1 << 24); -const UInt32 kBot = (1 << 15); - -struct CRangeDecoder -{ - IPpmd7_RangeDec s; - UInt32 Range; - UInt32 Code; - UInt32 Low; - CBitDecoder bitDecoder; - SRes Res; - -public: - void InitRangeCoder() - { - Code = 0; - Low = 0; - Range = 0xFFFFFFFF; - for (int i = 0; i < 4; i++) - Code = (Code << 8) | bitDecoder.ReadBits(8); - } - - void Normalize() - { - while ((Low ^ (Low + Range)) < kTopValue || - Range < kBot && ((Range = (0 - Low) & (kBot - 1)), 1)) - { - Code = (Code << 8) | bitDecoder.m_Stream.ReadByte(); - Range <<= 8; - Low <<= 8; - } - } - - CRangeDecoder(); -}; - -struct CFilter: public NVm::CProgram -{ - CRecordVector<Byte> GlobalData; - UInt32 BlockStart; - UInt32 BlockSize; - UInt32 ExecCount; - CFilter(): BlockStart(0), BlockSize(0), ExecCount(0) {} -}; - -struct CTempFilter: public NVm::CProgramInitState -{ - UInt32 BlockStart; - UInt32 BlockSize; - UInt32 ExecCount; - bool NextWindow; - - UInt32 FilterIndex; -}; - -const int kNumHuffmanBits = 15; - -class CDecoder: - public ICompressCoder, - public ICompressSetDecoderProperties2, - public CMyUnknownImp -{ - CRangeDecoder m_InBitStream; - Byte *_window; - UInt32 _winPos; - UInt32 _wrPtr; - UInt64 _lzSize; - UInt64 _unpackSize; - UInt64 _writtenFileSize; // if it's > _unpackSize, then _unpackSize only written - CMyComPtr<ISequentialOutStream> _outStream; - NHuffman::CDecoder<kNumHuffmanBits, kMainTableSize> m_MainDecoder; - NHuffman::CDecoder<kNumHuffmanBits, kDistTableSize> m_DistDecoder; - NHuffman::CDecoder<kNumHuffmanBits, kAlignTableSize> m_AlignDecoder; - NHuffman::CDecoder<kNumHuffmanBits, kLenTableSize> m_LenDecoder; - NHuffman::CDecoder<kNumHuffmanBits, kLevelTableSize> m_LevelDecoder; - - UInt32 _reps[kNumReps]; - UInt32 _lastLength; - - Byte m_LastLevels[kTablesSizesSum]; - - Byte *_vmData; - Byte *_vmCode; - NVm::CVm _vm; - CRecordVector<CFilter *> _filters; - CRecordVector<CTempFilter *> _tempFilters; - UInt32 _lastFilter; - - bool m_IsSolid; - - bool _lzMode; - - UInt32 PrevAlignBits; - UInt32 PrevAlignCount; - - bool TablesRead; - - CPpmd7 _ppmd; - int PpmEscChar; - bool PpmError; - - HRESULT WriteDataToStream(const Byte *data, UInt32 size); - HRESULT WriteData(const Byte *data, UInt32 size); - HRESULT WriteArea(UInt32 startPtr, UInt32 endPtr); - void ExecuteFilter(int tempFilterIndex, NVm::CBlockRef &outBlockRef); - HRESULT WriteBuf(); - - void InitFilters(); - bool AddVmCode(UInt32 firstByte, UInt32 codeSize); - bool ReadVmCodeLZ(); - bool ReadVmCodePPM(); - - UInt32 ReadBits(int numBits); - - HRESULT InitPPM(); - int DecodePpmSymbol(); - HRESULT DecodePPM(Int32 num, bool &keepDecompressing); - - HRESULT ReadTables(bool &keepDecompressing); - HRESULT ReadEndOfBlock(bool &keepDecompressing); - HRESULT DecodeLZ(bool &keepDecompressing); - HRESULT CodeReal(ICompressProgressInfo *progress); -public: - CDecoder(); - ~CDecoder(); - - MY_UNKNOWN_IMP1(ICompressSetDecoderProperties2) - - void ReleaseStreams() - { - _outStream.Release(); - m_InBitStream.bitDecoder.ReleaseStream(); - } - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); - - void CopyBlock(UInt32 distance, UInt32 len) - { - _lzSize += len; - UInt32 pos = (_winPos - distance - 1) & kWindowMask; - Byte *window = _window; - UInt32 winPos = _winPos; - if (kWindowSize - winPos > len && kWindowSize - pos > len) - { - const Byte *src = window + pos; - Byte *dest = window + winPos; - _winPos += len; - do - *dest++ = *src++; - while(--len != 0); - return; - } - do - { - window[winPos] = window[pos]; - winPos = (winPos + 1) & kWindowMask; - pos = (pos + 1) & kWindowMask; - } - while(--len != 0); - _winPos = winPos; - } - - void PutByte(Byte b) - { - _window[_winPos] = b; - _winPos = (_winPos + 1) & kWindowMask; - _lzSize++; - } - - -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Rar3Vm.cpp b/src/libs/7zip/win/CPP/7zip/Compress/Rar3Vm.cpp deleted file mode 100644 index 74051dd79..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Rar3Vm.cpp +++ /dev/null @@ -1,1091 +0,0 @@ -// Rar3Vm.cpp -// According to unRAR license, this code may not be used to develop -// a program that creates RAR archives - -/* -Note: - Due to performance considerations Rar VM may set Flags C incorrectly - for some operands (SHL x, 0, ... ). - Check implementation of concrete VM command - to see if it sets flags right. -*/ - -#include "StdAfx.h" - -#include "../../../C/7zCrc.h" -#include "../../../C/Alloc.h" - -#include "Rar3Vm.h" - -namespace NCompress { -namespace NRar3 { - -UInt32 CMemBitDecoder::ReadBits(int numBits) -{ - UInt32 res = 0; - for (;;) - { - Byte b = _bitPos < _bitSize ? _data[_bitPos >> 3] : 0; - int avail = (int)(8 - (_bitPos & 7)); - if (numBits <= avail) - { - _bitPos += numBits; - return res | (b >> (avail - numBits)) & ((1 << numBits) - 1); - } - numBits -= avail; - res |= (UInt32)(b & ((1 << avail) - 1)) << numBits; - _bitPos += avail; - } -} - -UInt32 CMemBitDecoder::ReadBit() { return ReadBits(1); } - -namespace NVm { - -static const UInt32 kStackRegIndex = kNumRegs - 1; - -static const UInt32 FLAG_C = 1; -static const UInt32 FLAG_Z = 2; -static const UInt32 FLAG_S = 0x80000000; - -static const Byte CF_OP0 = 0; -static const Byte CF_OP1 = 1; -static const Byte CF_OP2 = 2; -static const Byte CF_OPMASK = 3; -static const Byte CF_BYTEMODE = 4; -static const Byte CF_JUMP = 8; -static const Byte CF_PROC = 16; -static const Byte CF_USEFLAGS = 32; -static const Byte CF_CHFLAGS = 64; - -static Byte kCmdFlags[]= -{ - /* CMD_MOV */ CF_OP2 | CF_BYTEMODE, - /* CMD_CMP */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_ADD */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_SUB */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_JZ */ CF_OP1 | CF_JUMP | CF_USEFLAGS, - /* CMD_JNZ */ CF_OP1 | CF_JUMP | CF_USEFLAGS, - /* CMD_INC */ CF_OP1 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_DEC */ CF_OP1 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_JMP */ CF_OP1 | CF_JUMP, - /* CMD_XOR */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_AND */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_OR */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_TEST */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_JS */ CF_OP1 | CF_JUMP | CF_USEFLAGS, - /* CMD_JNS */ CF_OP1 | CF_JUMP | CF_USEFLAGS, - /* CMD_JB */ CF_OP1 | CF_JUMP | CF_USEFLAGS, - /* CMD_JBE */ CF_OP1 | CF_JUMP | CF_USEFLAGS, - /* CMD_JA */ CF_OP1 | CF_JUMP | CF_USEFLAGS, - /* CMD_JAE */ CF_OP1 | CF_JUMP | CF_USEFLAGS, - /* CMD_PUSH */ CF_OP1, - /* CMD_POP */ CF_OP1, - /* CMD_CALL */ CF_OP1 | CF_PROC, - /* CMD_RET */ CF_OP0 | CF_PROC, - /* CMD_NOT */ CF_OP1 | CF_BYTEMODE, - /* CMD_SHL */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_SHR */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_SAR */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_NEG */ CF_OP1 | CF_BYTEMODE | CF_CHFLAGS, - /* CMD_PUSHA */ CF_OP0, - /* CMD_POPA */ CF_OP0, - /* CMD_PUSHF */ CF_OP0 | CF_USEFLAGS, - /* CMD_POPF */ CF_OP0 | CF_CHFLAGS, - /* CMD_MOVZX */ CF_OP2, - /* CMD_MOVSX */ CF_OP2, - /* CMD_XCHG */ CF_OP2 | CF_BYTEMODE, - /* CMD_MUL */ CF_OP2 | CF_BYTEMODE, - /* CMD_DIV */ CF_OP2 | CF_BYTEMODE, - /* CMD_ADC */ CF_OP2 | CF_BYTEMODE | CF_USEFLAGS | CF_CHFLAGS , - /* CMD_SBB */ CF_OP2 | CF_BYTEMODE | CF_USEFLAGS | CF_CHFLAGS , - /* CMD_PRINT */ CF_OP0 -}; - -CVm::CVm(): Mem(NULL) {} - -bool CVm::Create() -{ - if (Mem == NULL) - Mem = (Byte *)::MyAlloc(kSpaceSize + 4); - return (Mem != NULL); -} - -CVm::~CVm() -{ - ::MyFree(Mem); -} - -// CVm::Execute can change CProgram object: it clears progarm if VM returns error. - -bool CVm::Execute(CProgram *prg, const CProgramInitState *initState, - CBlockRef &outBlockRef, CRecordVector<Byte> &outGlobalData) -{ - memcpy(R, initState->InitR, sizeof(initState->InitR)); - R[kStackRegIndex] = kSpaceSize; - R[kNumRegs] = 0; - Flags = 0; - - UInt32 globalSize = MyMin((UInt32)initState->GlobalData.Size(), kGlobalSize); - if (globalSize != 0) - memcpy(Mem + kGlobalOffset, &initState->GlobalData[0], globalSize); - UInt32 staticSize = MyMin((UInt32)prg->StaticData.Size(), kGlobalSize - globalSize); - if (staticSize != 0) - memcpy(Mem + kGlobalOffset + globalSize, &prg->StaticData[0], staticSize); - - bool res = true; - #ifdef RARVM_STANDARD_FILTERS - if (prg->StandardFilterIndex >= 0) - ExecuteStandardFilter(prg->StandardFilterIndex); - else - #endif - { - res = ExecuteCode(prg); - if (!res) - prg->Commands[0].OpCode = CMD_RET; - } - UInt32 newBlockPos = GetFixedGlobalValue32(NGlobalOffset::kBlockPos) & kSpaceMask; - UInt32 newBlockSize = GetFixedGlobalValue32(NGlobalOffset::kBlockSize) & kSpaceMask; - if (newBlockPos + newBlockSize >= kSpaceSize) - newBlockPos = newBlockSize = 0; - outBlockRef.Offset = newBlockPos; - outBlockRef.Size = newBlockSize; - - outGlobalData.Clear(); - UInt32 dataSize = GetFixedGlobalValue32(NGlobalOffset::kGlobalMemOutSize); - dataSize = MyMin(dataSize, kGlobalSize - kFixedGlobalSize); - if (dataSize != 0) - { - dataSize += kFixedGlobalSize; - outGlobalData.Reserve(dataSize); - for (UInt32 i = 0; i < dataSize; i++) - outGlobalData.Add(Mem[kGlobalOffset + i]); - } - return res; -} - - -#define SET_IP(IP) \ - if ((IP) >= numCommands) return true; \ - if (--maxOpCount <= 0) return false; \ - cmd = commands + (IP); - -#define GET_FLAG_S_B(res) (((res) & 0x80) ? FLAG_S : 0) -#define SET_IP_OP1 { UInt32 val = GetOperand32(&cmd->Op1); SET_IP(val); } -#define FLAGS_UPDATE_SZ Flags = res == 0 ? FLAG_Z : res & FLAG_S -#define FLAGS_UPDATE_SZ_B Flags = (res & 0xFF) == 0 ? FLAG_Z : GET_FLAG_S_B(res) - -UInt32 CVm::GetOperand32(const COperand *op) const -{ - switch(op->Type) - { - case OP_TYPE_REG: return R[op->Data]; - case OP_TYPE_REGMEM: return GetValue32(&Mem[(op->Base + R[op->Data]) & kSpaceMask]); - default: return op->Data; - } -} - -void CVm::SetOperand32(const COperand *op, UInt32 val) -{ - switch(op->Type) - { - case OP_TYPE_REG: R[op->Data] = val; return; - case OP_TYPE_REGMEM: SetValue32(&Mem[(op->Base + R[op->Data]) & kSpaceMask], val); return; - } -} - -Byte CVm::GetOperand8(const COperand *op) const -{ - switch(op->Type) - { - case OP_TYPE_REG: return (Byte)R[op->Data]; - case OP_TYPE_REGMEM: return Mem[(op->Base + R[op->Data]) & kSpaceMask];; - default: return (Byte)op->Data; - } -} - -void CVm::SetOperand8(const COperand *op, Byte val) -{ - switch(op->Type) - { - case OP_TYPE_REG: R[op->Data] = (R[op->Data] & 0xFFFFFF00) | val; return; - case OP_TYPE_REGMEM: Mem[(op->Base + R[op->Data]) & kSpaceMask] = val; return; - } -} - -UInt32 CVm::GetOperand(bool byteMode, const COperand *op) const -{ - if (byteMode) - return GetOperand8(op); - return GetOperand32(op); -} - -void CVm::SetOperand(bool byteMode, const COperand *op, UInt32 val) -{ - if (byteMode) - SetOperand8(op, (Byte)(val & 0xFF)); - else - SetOperand32(op, val); -} - -bool CVm::ExecuteCode(const CProgram *prg) -{ - Int32 maxOpCount = 25000000; - const CCommand *commands = &prg->Commands[0]; - const CCommand *cmd = commands; - UInt32 numCommands = prg->Commands.Size(); - for (;;) - { - switch(cmd->OpCode) - { - #ifndef RARVM_NO_VM - - case CMD_MOV: - SetOperand32(&cmd->Op1, GetOperand32(&cmd->Op2)); - break; - case CMD_MOVB: - SetOperand8(&cmd->Op1, GetOperand8(&cmd->Op2)); - break; - case CMD_CMP: - { - UInt32 v1 = GetOperand32(&cmd->Op1); - UInt32 res = v1 - GetOperand32(&cmd->Op2); - Flags = res == 0 ? FLAG_Z : (res > v1) | (res & FLAG_S); - } - break; - case CMD_CMPB: - { - Byte v1 = GetOperand8(&cmd->Op1); - Byte res = v1 - GetOperand8(&cmd->Op2); - res &= 0xFF; - Flags = res == 0 ? FLAG_Z : (res > v1) | GET_FLAG_S_B(res); - } - break; - case CMD_ADD: - { - UInt32 v1 = GetOperand32(&cmd->Op1); - UInt32 res = v1 + GetOperand32(&cmd->Op2); - SetOperand32(&cmd->Op1, res); - Flags = (res < v1) | (res == 0 ? FLAG_Z : (res & FLAG_S)); - } - break; - case CMD_ADDB: - { - Byte v1 = GetOperand8(&cmd->Op1); - Byte res = v1 + GetOperand8(&cmd->Op2); - res &= 0xFF; - SetOperand8(&cmd->Op1, (Byte)res); - Flags = (res < v1) | (res == 0 ? FLAG_Z : GET_FLAG_S_B(res)); - } - break; - case CMD_ADC: - { - UInt32 v1 = GetOperand(cmd->ByteMode, &cmd->Op1); - UInt32 FC = (Flags & FLAG_C); - UInt32 res = v1 + GetOperand(cmd->ByteMode, &cmd->Op2) + FC; - if (cmd->ByteMode) - res &= 0xFF; - SetOperand(cmd->ByteMode, &cmd->Op1, res); - Flags = (res < v1 || res == v1 && FC) | (res == 0 ? FLAG_Z : (res & FLAG_S)); - } - break; - case CMD_SUB: - { - UInt32 v1 = GetOperand32(&cmd->Op1); - UInt32 res = v1 - GetOperand32(&cmd->Op2); - SetOperand32(&cmd->Op1, res); - Flags = res == 0 ? FLAG_Z : (res > v1) | (res & FLAG_S); - } - break; - case CMD_SUBB: - { - UInt32 v1 = GetOperand8(&cmd->Op1); - UInt32 res = v1 - GetOperand8(&cmd->Op2); - SetOperand8(&cmd->Op1, (Byte)res); - Flags = res == 0 ? FLAG_Z : (res > v1) | (res & FLAG_S); - } - break; - case CMD_SBB: - { - UInt32 v1 = GetOperand(cmd->ByteMode, &cmd->Op1); - UInt32 FC = (Flags & FLAG_C); - UInt32 res = v1 - GetOperand(cmd->ByteMode, &cmd->Op2) - FC; - // Flags = res == 0 ? FLAG_Z : (res > v1 || res == v1 && FC) | (res & FLAG_S); - if (cmd->ByteMode) - res &= 0xFF; - SetOperand(cmd->ByteMode, &cmd->Op1, res); - Flags = (res > v1 || res == v1 && FC) | (res == 0 ? FLAG_Z : (res & FLAG_S)); - } - break; - case CMD_INC: - { - UInt32 res = GetOperand32(&cmd->Op1) + 1; - SetOperand32(&cmd->Op1, res); - FLAGS_UPDATE_SZ; - } - break; - case CMD_INCB: - { - Byte res = GetOperand8(&cmd->Op1) + 1; - SetOperand8(&cmd->Op1, res);; - FLAGS_UPDATE_SZ_B; - } - break; - case CMD_DEC: - { - UInt32 res = GetOperand32(&cmd->Op1) - 1; - SetOperand32(&cmd->Op1, res); - FLAGS_UPDATE_SZ; - } - break; - case CMD_DECB: - { - Byte res = GetOperand8(&cmd->Op1) - 1; - SetOperand8(&cmd->Op1, res);; - FLAGS_UPDATE_SZ_B; - } - break; - case CMD_XOR: - { - UInt32 res = GetOperand32(&cmd->Op1) ^ GetOperand32(&cmd->Op2); - SetOperand32(&cmd->Op1, res); - FLAGS_UPDATE_SZ; - } - break; - case CMD_XORB: - { - Byte res = GetOperand8(&cmd->Op1) ^ GetOperand8(&cmd->Op2); - SetOperand8(&cmd->Op1, res); - FLAGS_UPDATE_SZ_B; - } - break; - case CMD_AND: - { - UInt32 res = GetOperand32(&cmd->Op1) & GetOperand32(&cmd->Op2); - SetOperand32(&cmd->Op1, res); - FLAGS_UPDATE_SZ; - } - break; - case CMD_ANDB: - { - Byte res = GetOperand8(&cmd->Op1) & GetOperand8(&cmd->Op2); - SetOperand8(&cmd->Op1, res); - FLAGS_UPDATE_SZ_B; - } - break; - case CMD_OR: - { - UInt32 res = GetOperand32(&cmd->Op1) | GetOperand32(&cmd->Op2); - SetOperand32(&cmd->Op1, res); - FLAGS_UPDATE_SZ; - } - break; - case CMD_ORB: - { - Byte res = GetOperand8(&cmd->Op1) | GetOperand8(&cmd->Op2); - SetOperand8(&cmd->Op1, res); - FLAGS_UPDATE_SZ_B; - } - break; - case CMD_TEST: - { - UInt32 res = GetOperand32(&cmd->Op1) & GetOperand32(&cmd->Op2); - FLAGS_UPDATE_SZ; - } - break; - case CMD_TESTB: - { - Byte res = GetOperand8(&cmd->Op1) & GetOperand8(&cmd->Op2); - FLAGS_UPDATE_SZ_B; - } - break; - case CMD_NOT: - SetOperand(cmd->ByteMode, &cmd->Op1, ~GetOperand(cmd->ByteMode, &cmd->Op1)); - break; - case CMD_NEG: - { - UInt32 res = 0 - GetOperand32(&cmd->Op1); - SetOperand32(&cmd->Op1, res); - Flags = res == 0 ? FLAG_Z : FLAG_C | (res & FLAG_S); - } - break; - case CMD_NEGB: - { - Byte res = (Byte)(0 - GetOperand8(&cmd->Op1)); - SetOperand8(&cmd->Op1, res); - Flags = res == 0 ? FLAG_Z : FLAG_C | GET_FLAG_S_B(res); - } - break; - - case CMD_SHL: - { - UInt32 v1 = GetOperand32(&cmd->Op1); - int v2 = (int)GetOperand32(&cmd->Op2); - UInt32 res = v1 << v2; - SetOperand32(&cmd->Op1, res); - Flags = (res == 0 ? FLAG_Z : (res & FLAG_S)) | ((v1 << (v2 - 1)) & 0x80000000 ? FLAG_C : 0); - } - break; - case CMD_SHLB: - { - Byte v1 = GetOperand8(&cmd->Op1); - int v2 = (int)GetOperand8(&cmd->Op2); - Byte res = (Byte)(v1 << v2); - SetOperand8(&cmd->Op1, res); - Flags = (res == 0 ? FLAG_Z : GET_FLAG_S_B(res)) | ((v1 << (v2 - 1)) & 0x80 ? FLAG_C : 0); - } - break; - case CMD_SHR: - { - UInt32 v1 = GetOperand32(&cmd->Op1); - int v2 = (int)GetOperand32(&cmd->Op2); - UInt32 res = v1 >> v2; - SetOperand32(&cmd->Op1, res); - Flags = (res == 0 ? FLAG_Z : (res & FLAG_S)) | ((v1 >> (v2 - 1)) & FLAG_C); - } - break; - case CMD_SHRB: - { - Byte v1 = GetOperand8(&cmd->Op1); - int v2 = (int)GetOperand8(&cmd->Op2); - Byte res = (Byte)(v1 >> v2); - SetOperand8(&cmd->Op1, res); - Flags = (res == 0 ? FLAG_Z : GET_FLAG_S_B(res)) | ((v1 >> (v2 - 1)) & FLAG_C); - } - break; - case CMD_SAR: - { - UInt32 v1 = GetOperand32(&cmd->Op1); - int v2 = (int)GetOperand32(&cmd->Op2); - UInt32 res = UInt32(((Int32)v1) >> v2); - SetOperand32(&cmd->Op1, res); - Flags= (res == 0 ? FLAG_Z : (res & FLAG_S)) | ((v1 >> (v2 - 1)) & FLAG_C); - } - break; - case CMD_SARB: - { - Byte v1 = GetOperand8(&cmd->Op1); - int v2 = (int)GetOperand8(&cmd->Op2); - Byte res = (Byte)(((signed char)v1) >> v2); - SetOperand8(&cmd->Op1, res); - Flags= (res == 0 ? FLAG_Z : GET_FLAG_S_B(res)) | ((v1 >> (v2 - 1)) & FLAG_C); - } - break; - - case CMD_JMP: - SET_IP_OP1; - continue; - case CMD_JZ: - if ((Flags & FLAG_Z) != 0) - { - SET_IP_OP1; - continue; - } - break; - case CMD_JNZ: - if ((Flags & FLAG_Z) == 0) - { - SET_IP_OP1; - continue; - } - break; - case CMD_JS: - if ((Flags & FLAG_S) != 0) - { - SET_IP_OP1; - continue; - } - break; - case CMD_JNS: - if ((Flags & FLAG_S) == 0) - { - SET_IP_OP1; - continue; - } - break; - case CMD_JB: - if ((Flags & FLAG_C) != 0) - { - SET_IP_OP1; - continue; - } - break; - case CMD_JBE: - if ((Flags & (FLAG_C | FLAG_Z)) != 0) - { - SET_IP_OP1; - continue; - } - break; - case CMD_JA: - if ((Flags & (FLAG_C | FLAG_Z)) == 0) - { - SET_IP_OP1; - continue; - } - break; - case CMD_JAE: - if ((Flags & FLAG_C) == 0) - { - SET_IP_OP1; - continue; - } - break; - - case CMD_PUSH: - R[kStackRegIndex] -= 4; - SetValue32(&Mem[R[kStackRegIndex] & kSpaceMask], GetOperand32(&cmd->Op1)); - break; - case CMD_POP: - SetOperand32(&cmd->Op1, GetValue32(&Mem[R[kStackRegIndex] & kSpaceMask])); - R[kStackRegIndex] += 4; - break; - case CMD_CALL: - R[kStackRegIndex] -= 4; - SetValue32(&Mem[R[kStackRegIndex] & kSpaceMask], (UInt32)(cmd - commands + 1)); - SET_IP_OP1; - continue; - - case CMD_PUSHA: - { - for (UInt32 i = 0, SP = R[kStackRegIndex] - 4; i < kNumRegs; i++, SP -= 4) - SetValue32(&Mem[SP & kSpaceMask], R[i]); - R[kStackRegIndex] -= kNumRegs * 4; - } - break; - case CMD_POPA: - { - for (UInt32 i = 0, SP = R[kStackRegIndex]; i < kNumRegs; i++, SP += 4) - R[kStackRegIndex - i] = GetValue32(&Mem[SP & kSpaceMask]); - } - break; - case CMD_PUSHF: - R[kStackRegIndex] -= 4; - SetValue32(&Mem[R[kStackRegIndex]&kSpaceMask], Flags); - break; - case CMD_POPF: - Flags = GetValue32(&Mem[R[kStackRegIndex] & kSpaceMask]); - R[kStackRegIndex] += 4; - break; - - case CMD_MOVZX: - SetOperand32(&cmd->Op1, GetOperand8(&cmd->Op2)); - break; - case CMD_MOVSX: - SetOperand32(&cmd->Op1, (UInt32)(Int32)(signed char)GetOperand8(&cmd->Op2)); - break; - case CMD_XCHG: - { - UInt32 v1 = GetOperand(cmd->ByteMode, &cmd->Op1); - SetOperand(cmd->ByteMode, &cmd->Op1, GetOperand(cmd->ByteMode, &cmd->Op2)); - SetOperand(cmd->ByteMode, &cmd->Op2, v1); - } - break; - case CMD_MUL: - { - UInt32 res = GetOperand32(&cmd->Op1) * GetOperand32(&cmd->Op2); - SetOperand32(&cmd->Op1, res); - } - break; - case CMD_MULB: - { - Byte res = GetOperand8(&cmd->Op1) * GetOperand8(&cmd->Op2); - SetOperand8(&cmd->Op1, res); - } - break; - case CMD_DIV: - { - UInt32 divider = GetOperand(cmd->ByteMode, &cmd->Op2); - if (divider != 0) - { - UInt32 res = GetOperand(cmd->ByteMode, &cmd->Op1) / divider; - SetOperand(cmd->ByteMode, &cmd->Op1, res); - } - } - break; - - #endif - - case CMD_RET: - { - if (R[kStackRegIndex] >= kSpaceSize) - return true; - UInt32 ip = GetValue32(&Mem[R[kStackRegIndex] & kSpaceMask]); - SET_IP(ip); - R[kStackRegIndex] += 4; - continue; - } - case CMD_PRINT: - break; - } - cmd++; - --maxOpCount; - } -} - - -////////////////////////////////////////////////////// -// Read program - -UInt32 ReadEncodedUInt32(CMemBitDecoder &inp) -{ - switch(inp.ReadBits(2)) - { - case 0: - return inp.ReadBits(4); - case 1: - { - UInt32 v = inp.ReadBits(4); - if (v == 0) - return 0xFFFFFF00 | inp.ReadBits(8); - else - return (v << 4) | inp.ReadBits(4); - } - case 2: - return inp.ReadBits(16); - default: - return inp.ReadBits(32); - } -} - -void CVm::DecodeArg(CMemBitDecoder &inp, COperand &op, bool byteMode) -{ - if (inp.ReadBit()) - { - op.Type = OP_TYPE_REG; - op.Data = inp.ReadBits(kNumRegBits); - } - else if (inp.ReadBit() == 0) - { - op.Type = OP_TYPE_INT; - if (byteMode) - op.Data = inp.ReadBits(8); - else - op.Data = ReadEncodedUInt32(inp); - } - else - { - op.Type = OP_TYPE_REGMEM; - if (inp.ReadBit() == 0) - { - op.Data = inp.ReadBits(kNumRegBits); - op.Base = 0; - } - else - { - if (inp.ReadBit() == 0) - op.Data = inp.ReadBits(kNumRegBits); - else - op.Data = kNumRegs; - op.Base = ReadEncodedUInt32(inp); - } - } -} - -void CVm::ReadVmProgram(const Byte *code, UInt32 codeSize, CProgram *prg) -{ - CMemBitDecoder inp; - inp.Init(code, codeSize); - - prg->StaticData.Clear(); - if (inp.ReadBit()) - { - UInt32 dataSize = ReadEncodedUInt32(inp) + 1; - for (UInt32 i = 0; inp.Avail() && i < dataSize; i++) - prg->StaticData.Add((Byte)inp.ReadBits(8)); - } - while (inp.Avail()) - { - prg->Commands.Add(CCommand()); - CCommand *cmd = &prg->Commands.Back(); - if (inp.ReadBit() == 0) - cmd->OpCode = (ECommand)inp.ReadBits(3); - else - cmd->OpCode = (ECommand)(8 + inp.ReadBits(5)); - if (kCmdFlags[cmd->OpCode] & CF_BYTEMODE) - cmd->ByteMode = (inp.ReadBit()) ? true : false; - else - cmd->ByteMode = 0; - int opNum = (kCmdFlags[cmd->OpCode] & CF_OPMASK); - if (opNum > 0) - { - DecodeArg(inp, cmd->Op1, cmd->ByteMode); - if (opNum == 2) - DecodeArg(inp, cmd->Op2, cmd->ByteMode); - else - { - if (cmd->Op1.Type == OP_TYPE_INT && (kCmdFlags[cmd->OpCode] & (CF_JUMP | CF_PROC))) - { - int Distance = cmd->Op1.Data; - if (Distance >= 256) - Distance -= 256; - else - { - if (Distance >= 136) - Distance -= 264; - else if (Distance >= 16) - Distance -= 8; - else if (Distance >= 8) - Distance -= 16; - Distance += prg->Commands.Size() - 1; - } - cmd->Op1.Data = Distance; - } - } - } - if (cmd->ByteMode) - { - switch (cmd->OpCode) - { - case CMD_MOV: cmd->OpCode = CMD_MOVB; break; - case CMD_CMP: cmd->OpCode = CMD_CMPB; break; - case CMD_ADD: cmd->OpCode = CMD_ADDB; break; - case CMD_SUB: cmd->OpCode = CMD_SUBB; break; - case CMD_INC: cmd->OpCode = CMD_INCB; break; - case CMD_DEC: cmd->OpCode = CMD_DECB; break; - case CMD_XOR: cmd->OpCode = CMD_XORB; break; - case CMD_AND: cmd->OpCode = CMD_ANDB; break; - case CMD_OR: cmd->OpCode = CMD_ORB; break; - case CMD_TEST: cmd->OpCode = CMD_TESTB; break; - case CMD_NEG: cmd->OpCode = CMD_NEGB; break; - case CMD_SHL: cmd->OpCode = CMD_SHLB; break; - case CMD_SHR: cmd->OpCode = CMD_SHRB; break; - case CMD_SAR: cmd->OpCode = CMD_SARB; break; - case CMD_MUL: cmd->OpCode = CMD_MULB; break; - } - } - } -} - -#ifdef RARVM_STANDARD_FILTERS - -enum EStandardFilter -{ - SF_E8, - SF_E8E9, - SF_ITANIUM, - SF_RGB, - SF_AUDIO, - SF_DELTA, - SF_UPCASE -}; - -struct StandardFilterSignature -{ - UInt32 Length; - UInt32 CRC; - EStandardFilter Type; -} -kStdFilters[]= -{ - { 53, 0xad576887, SF_E8 }, - { 57, 0x3cd7e57e, SF_E8E9 }, - { 120, 0x3769893f, SF_ITANIUM }, - { 29, 0x0e06077d, SF_DELTA }, - { 149, 0x1c2c5dc8, SF_RGB }, - { 216, 0xbc85e701, SF_AUDIO }, - { 40, 0x46b9c560, SF_UPCASE } -}; - -static int FindStandardFilter(const Byte *code, UInt32 codeSize) -{ - UInt32 crc = CrcCalc(code, codeSize); - for (int i = 0; i < sizeof(kStdFilters) / sizeof(kStdFilters[0]); i++) - { - StandardFilterSignature &sfs = kStdFilters[i]; - if (sfs.CRC == crc && sfs.Length == codeSize) - return i; - } - return -1; -} - -#endif - -void CVm::PrepareProgram(const Byte *code, UInt32 codeSize, CProgram *prg) -{ - Byte xorSum = 0; - for (UInt32 i = 1; i < codeSize; i++) - xorSum ^= code[i]; - - prg->Commands.Clear(); - #ifdef RARVM_STANDARD_FILTERS - prg->StandardFilterIndex = -1; - #endif - - if (xorSum == code[0] && codeSize > 0) - { - #ifdef RARVM_STANDARD_FILTERS - prg->StandardFilterIndex = FindStandardFilter(code, codeSize); - if (prg->StandardFilterIndex >= 0) - return; - #endif - // 1 byte for checksum - ReadVmProgram(code + 1, codeSize - 1, prg); - } - prg->Commands.Add(CCommand()); - CCommand *cmd = &prg->Commands.Back(); - cmd->OpCode = CMD_RET; -} - -void CVm::SetMemory(UInt32 pos, const Byte *data, UInt32 dataSize) -{ - if (pos < kSpaceSize && data != Mem + pos) - memmove(Mem + pos, data, MyMin(dataSize, kSpaceSize - pos)); -} - -#ifdef RARVM_STANDARD_FILTERS - -static void E8E9Decode(Byte *data, UInt32 dataSize, UInt32 fileOffset, bool e9) -{ - if (dataSize <= 4) - return; - dataSize -= 4; - const UInt32 kFileSize = 0x1000000; - Byte cmpByte2 = (e9 ? 0xE9 : 0xE8); - for (UInt32 curPos = 0; curPos < dataSize;) - { - Byte curByte = *(data++); - curPos++; - if (curByte == 0xE8 || curByte == cmpByte2) - { - UInt32 offset = curPos + fileOffset; - UInt32 addr = (Int32)GetValue32(data); - if (addr < kFileSize) - SetValue32(data, addr - offset); - else if ((Int32)addr < 0 && (Int32)(addr + offset) >= 0) - SetValue32(data, addr + kFileSize); - data += 4; - curPos += 4; - } - } -} - -static inline UInt32 ItaniumGetOpType(const Byte *data, int bitPos) -{ - return (data[(unsigned int)bitPos >> 3] >> (bitPos & 7)) & 0xF; -} - - -static void ItaniumDecode(Byte *data, UInt32 dataSize, UInt32 fileOffset) -{ - UInt32 curPos = 0; - fileOffset >>= 4; - while (curPos < dataSize - 21) - { - int b = (data[0] & 0x1F) - 0x10; - if (b >= 0) - { - static Byte kCmdMasks[16] = {4,4,6,6,0,0,7,7,4,4,0,0,4,4,0,0}; - Byte cmdMask = kCmdMasks[b]; - if (cmdMask != 0) - for (int i = 0; i < 3; i++) - if (cmdMask & (1 << i)) - { - int startPos = i * 41 + 18; - if (ItaniumGetOpType(data, startPos + 24) == 5) - { - const UInt32 kMask = 0xFFFFF; - Byte *p = data + ((unsigned int)startPos >> 3); - UInt32 bitField = ((UInt32)p[0]) | ((UInt32)p[1] << 8) | ((UInt32)p[2] << 16); - int inBit = (startPos & 7); - UInt32 offset = (bitField >> inBit) & kMask; - UInt32 andMask = ~(kMask << inBit); - bitField = ((offset - fileOffset) & kMask) << inBit; - for (int j = 0; j < 3; j++) - { - p[j] &= andMask; - p[j] |= bitField; - andMask >>= 8; - bitField >>= 8; - } - } - } - } - data += 16; - curPos += 16; - fileOffset++; - } -} - -static void DeltaDecode(Byte *data, UInt32 dataSize, UInt32 numChannels) -{ - UInt32 srcPos = 0; - UInt32 border = dataSize * 2; - for (UInt32 curChannel = 0; curChannel < numChannels; curChannel++) - { - Byte prevByte = 0; - for (UInt32 destPos = dataSize + curChannel; destPos < border; destPos += numChannels) - data[destPos] = (prevByte = prevByte - data[srcPos++]); - } -} - -static void RgbDecode(Byte *srcData, UInt32 dataSize, UInt32 width, UInt32 posR) -{ - Byte *destData = srcData + dataSize; - const UInt32 numChannels = 3; - for (UInt32 curChannel = 0; curChannel < numChannels; curChannel++) - { - Byte prevByte = 0; - - for (UInt32 i = curChannel; i < dataSize; i+= numChannels) - { - unsigned int predicted; - if (i < width) - predicted = prevByte; - else - { - unsigned int upperLeftByte = destData[i - width]; - unsigned int upperByte = destData[i - width + 3]; - predicted = prevByte + upperByte - upperLeftByte; - int pa = abs((int)(predicted - prevByte)); - int pb = abs((int)(predicted - upperByte)); - int pc = abs((int)(predicted - upperLeftByte)); - if (pa <= pb && pa <= pc) - predicted = prevByte; - else - if (pb <= pc) - predicted = upperByte; - else - predicted = upperLeftByte; - } - destData[i] = prevByte = (Byte)(predicted - *(srcData++)); - } - } - if (dataSize < 3) - return; - for (UInt32 i = posR, border = dataSize - 2; i < border; i += 3) - { - Byte g = destData[i + 1]; - destData[i] = destData[i] + g; - destData[i + 2] = destData[i + 2] + g; - } -} - -static void AudioDecode(Byte *srcData, UInt32 dataSize, UInt32 numChannels) -{ - Byte *destData = srcData + dataSize; - for (UInt32 curChannel = 0; curChannel < numChannels; curChannel++) - { - UInt32 prevByte = 0, prevDelta = 0, dif[7]; - Int32 D1 = 0, D2 = 0, D3; - Int32 K1 = 0, K2 = 0, K3 = 0; - memset(dif, 0, sizeof(dif)); - - for (UInt32 i = curChannel, byteCount = 0; i < dataSize; i += numChannels, byteCount++) - { - D3 = D2; - D2 = prevDelta - D1; - D1 = prevDelta; - - UInt32 predicted = 8 * prevByte + K1 * D1 + K2 * D2 + K3 * D3; - predicted = (predicted >> 3) & 0xFF; - - UInt32 curByte = *(srcData++); - - predicted -= curByte; - destData[i] = (Byte)predicted; - prevDelta = (UInt32)(Int32)(signed char)(predicted - prevByte); - prevByte = predicted; - - Int32 D = ((Int32)(signed char)curByte) << 3; - - dif[0] += abs(D); - dif[1] += abs(D - D1); - dif[2] += abs(D + D1); - dif[3] += abs(D - D2); - dif[4] += abs(D + D2); - dif[5] += abs(D - D3); - dif[6] += abs(D + D3); - - if ((byteCount & 0x1F) == 0) - { - UInt32 minDif = dif[0], numMinDif = 0; - dif[0] = 0; - for (int j = 1; j < sizeof(dif) / sizeof(dif[0]); j++) - { - if (dif[j] < minDif) - { - minDif = dif[j]; - numMinDif = j; - } - dif[j] = 0; - } - switch (numMinDif) - { - case 1: if (K1 >= -16) K1--; break; - case 2: if (K1 < 16) K1++; break; - case 3: if (K2 >= -16) K2--; break; - case 4: if (K2 < 16) K2++; break; - case 5: if (K3 >= -16) K3--; break; - case 6: if (K3 < 16) K3++; break; - } - } - } - } -} - -static UInt32 UpCaseDecode(Byte *data, UInt32 dataSize) -{ - UInt32 srcPos = 0, destPos = dataSize; - while (srcPos < dataSize) - { - Byte curByte = data[srcPos++]; - if (curByte == 2 && (curByte = data[srcPos++]) != 2) - curByte -= 32; - data[destPos++] = curByte; - } - return destPos - dataSize; -} - -void CVm::ExecuteStandardFilter(int filterIndex) -{ - UInt32 dataSize = R[4]; - if (dataSize >= kGlobalOffset) - return; - EStandardFilter filterType = kStdFilters[filterIndex].Type; - - switch (filterType) - { - case SF_E8: - case SF_E8E9: - E8E9Decode(Mem, dataSize, R[6], (filterType == SF_E8E9)); - break; - case SF_ITANIUM: - ItaniumDecode(Mem, dataSize, R[6]); - break; - case SF_DELTA: - if (dataSize >= kGlobalOffset / 2) - break; - SetBlockPos(dataSize); - DeltaDecode(Mem, dataSize, R[0]); - break; - case SF_RGB: - if (dataSize >= kGlobalOffset / 2) - break; - { - UInt32 width = R[0]; - if (width <= 3) - break; - SetBlockPos(dataSize); - RgbDecode(Mem, dataSize, width, R[1]); - } - break; - case SF_AUDIO: - if (dataSize >= kGlobalOffset / 2) - break; - SetBlockPos(dataSize); - AudioDecode(Mem, dataSize, R[0]); - break; - case SF_UPCASE: - if (dataSize >= kGlobalOffset / 2) - break; - UInt32 destSize = UpCaseDecode(Mem, dataSize); - SetBlockSize(destSize); - SetBlockPos(dataSize); - break; - } -} - -#endif - -}}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/Rar3Vm.h b/src/libs/7zip/win/CPP/7zip/Compress/Rar3Vm.h deleted file mode 100644 index c02534c61..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/Rar3Vm.h +++ /dev/null @@ -1,179 +0,0 @@ -// Rar3Vm.h -// According to unRAR license, this code may not be used to develop -// a program that creates RAR archives - -#ifndef __COMPRESS_RAR3_VM_H -#define __COMPRESS_RAR3_VM_H - -#include "../../../C/CpuArch.h" - -#include "Common/MyVector.h" -#include "Common/Types.h" - -#define RARVM_STANDARD_FILTERS - -namespace NCompress { -namespace NRar3 { - -class CMemBitDecoder -{ - const Byte *_data; - UInt32 _bitSize; - UInt32 _bitPos; -public: - void Init(const Byte *data, UInt32 byteSize) - { - _data = data; - _bitSize = (byteSize << 3); - _bitPos = 0; - } - UInt32 ReadBits(int numBits); - UInt32 ReadBit(); - bool Avail() const { return (_bitPos < _bitSize); } -}; - -namespace NVm { - -inline UInt32 GetValue32(const void *addr) { return GetUi32(addr); } -inline void SetValue32(void *addr, UInt32 value) { SetUi32(addr, value); } - -UInt32 ReadEncodedUInt32(CMemBitDecoder &inp); - -const int kNumRegBits = 3; -const UInt32 kNumRegs = 1 << kNumRegBits; -const UInt32 kNumGpRegs = kNumRegs - 1; - -const UInt32 kSpaceSize = 0x40000; -const UInt32 kSpaceMask = kSpaceSize -1; -const UInt32 kGlobalOffset = 0x3C000; -const UInt32 kGlobalSize = 0x2000; -const UInt32 kFixedGlobalSize = 64; - -namespace NGlobalOffset -{ - const UInt32 kBlockSize = 0x1C; - const UInt32 kBlockPos = 0x20; - const UInt32 kExecCount = 0x2C; - const UInt32 kGlobalMemOutSize = 0x30; -} - -enum ECommand -{ - CMD_MOV, CMD_CMP, CMD_ADD, CMD_SUB, CMD_JZ, CMD_JNZ, CMD_INC, CMD_DEC, - CMD_JMP, CMD_XOR, CMD_AND, CMD_OR, CMD_TEST, CMD_JS, CMD_JNS, CMD_JB, - CMD_JBE, CMD_JA, CMD_JAE, CMD_PUSH, CMD_POP, CMD_CALL, CMD_RET, CMD_NOT, - CMD_SHL, CMD_SHR, CMD_SAR, CMD_NEG, CMD_PUSHA,CMD_POPA, CMD_PUSHF,CMD_POPF, - CMD_MOVZX,CMD_MOVSX,CMD_XCHG, CMD_MUL, CMD_DIV, CMD_ADC, CMD_SBB, CMD_PRINT, - - CMD_MOVB, CMD_CMPB, CMD_ADDB, CMD_SUBB, CMD_INCB, CMD_DECB, - CMD_XORB, CMD_ANDB, CMD_ORB, CMD_TESTB,CMD_NEGB, - CMD_SHLB, CMD_SHRB, CMD_SARB, CMD_MULB -}; - -enum EOpType {OP_TYPE_REG, OP_TYPE_INT, OP_TYPE_REGMEM, OP_TYPE_NONE}; - -// Addr in COperand object can link (point) to CVm object!!! - -struct COperand -{ - EOpType Type; - UInt32 Data; - UInt32 Base; - COperand(): Type(OP_TYPE_NONE), Data(0), Base(0) {} -}; - -struct CCommand -{ - ECommand OpCode; - bool ByteMode; - COperand Op1, Op2; -}; - -struct CBlockRef -{ - UInt32 Offset; - UInt32 Size; -}; - -struct CProgram -{ - CRecordVector<CCommand> Commands; - #ifdef RARVM_STANDARD_FILTERS - int StandardFilterIndex; - #endif - CRecordVector<Byte> StaticData; -}; - -struct CProgramInitState -{ - UInt32 InitR[kNumGpRegs]; - CRecordVector<Byte> GlobalData; - - void AllocateEmptyFixedGlobal() - { - GlobalData.Clear(); - GlobalData.Reserve(NVm::kFixedGlobalSize); - for (UInt32 i = 0; i < NVm::kFixedGlobalSize; i++) - GlobalData.Add(0); - } -}; - -class CVm -{ - static UInt32 GetValue(bool byteMode, const void *addr) - { - if (byteMode) - return(*(const Byte *)addr); - else - return GetUi32(addr); - } - - static void SetValue(bool byteMode, void *addr, UInt32 value) - { - if (byteMode) - *(Byte *)addr = (Byte)value; - else - SetUi32(addr, value); - } - - UInt32 GetFixedGlobalValue32(UInt32 globalOffset) { return GetValue(false, &Mem[kGlobalOffset + globalOffset]); } - - void SetBlockSize(UInt32 v) { SetValue(&Mem[kGlobalOffset + NGlobalOffset::kBlockSize], v); } - void SetBlockPos(UInt32 v) { SetValue(&Mem[kGlobalOffset + NGlobalOffset::kBlockPos], v); } -public: - static void SetValue(void *addr, UInt32 value) { SetValue(false, addr, value); } -private: - UInt32 GetOperand32(const COperand *op) const; - void SetOperand32(const COperand *op, UInt32 val); - Byte GetOperand8(const COperand *op) const; - void SetOperand8(const COperand *op, Byte val); - UInt32 GetOperand(bool byteMode, const COperand *op) const; - void SetOperand(bool byteMode, const COperand *op, UInt32 val); - - void DecodeArg(CMemBitDecoder &inp, COperand &op, bool byteMode); - - bool ExecuteCode(const CProgram *prg); - - #ifdef RARVM_STANDARD_FILTERS - void ExecuteStandardFilter(int filterIndex); - #endif - - Byte *Mem; - UInt32 R[kNumRegs + 1]; // R[kNumRegs] = 0 always (speed optimization) - UInt32 Flags; - void ReadVmProgram(const Byte *code, UInt32 codeSize, CProgram *prg); -public: - CVm(); - ~CVm(); - bool Create(); - void PrepareProgram(const Byte *code, UInt32 codeSize, CProgram *prg); - void SetMemory(UInt32 pos, const Byte *data, UInt32 dataSize); - bool Execute(CProgram *prg, const CProgramInitState *initState, - CBlockRef &outBlockRef, CRecordVector<Byte> &outGlobalData); - const Byte *GetDataPointer(UInt32 offset) const { return Mem + offset; } - -}; - -#endif - -}}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/RarCodecsRegister.cpp b/src/libs/7zip/win/CPP/7zip/Compress/RarCodecsRegister.cpp deleted file mode 100644 index e3f6a05c4..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/RarCodecsRegister.cpp +++ /dev/null @@ -1,26 +0,0 @@ -// RarCodecsRegister.cpp - -#include "StdAfx.h" - -#include "../Common/RegisterCodec.h" - -#include "Rar1Decoder.h" -#include "Rar2Decoder.h" -#include "Rar3Decoder.h" - -#define CREATE_CODEC(x) static void *CreateCodec ## x() { return (void *)(ICompressCoder *)(new NCompress::NRar ## x::CDecoder); } - -CREATE_CODEC(1) -CREATE_CODEC(2) -CREATE_CODEC(3) - -#define RAR_CODEC(x, name) { CreateCodec ## x, 0, 0x040300 + x, L"Rar" name, 1, false } - -static CCodecInfo g_CodecsInfo[] = -{ - RAR_CODEC(1, L"1"), - RAR_CODEC(2, L"2"), - RAR_CODEC(3, L"3"), -}; - -REGISTER_CODECS(Rar) diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ShrinkDecoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/ShrinkDecoder.cpp deleted file mode 100644 index a89d323c6..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ShrinkDecoder.cpp +++ /dev/null @@ -1,145 +0,0 @@ -// ShrinkDecoder.cpp - -#include "StdAfx.h" - -#include "../../../C/Alloc.h" - -#include "../Common/InBuffer.h" -#include "../Common/OutBuffer.h" - -#include "BitlDecoder.h" -#include "ShrinkDecoder.h" - -namespace NCompress { -namespace NShrink { - -static const UInt32 kBufferSize = (1 << 20); -static const int kNumMinBits = 9; - -HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */, const UInt64 * /* outSize */, ICompressProgressInfo *progress) -{ - NBitl::CBaseDecoder<CInBuffer> inBuffer; - COutBuffer outBuffer; - - if (!inBuffer.Create(kBufferSize)) - return E_OUTOFMEMORY; - inBuffer.SetStream(inStream); - inBuffer.Init(); - - if (!outBuffer.Create(kBufferSize)) - return E_OUTOFMEMORY; - outBuffer.SetStream(outStream); - outBuffer.Init(); - - UInt64 prevPos = 0; - int numBits = kNumMinBits; - UInt32 head = 257; - bool needPrev = false; - UInt32 lastSymbol = 0; - - int i; - for (i = 0; i < kNumItems; i++) - _parents[i] = 0; - for (i = 0; i < kNumItems; i++) - _suffixes[i] = 0; - for (i = 0; i < 257; i++) - _isFree[i] = false; - for (; i < kNumItems; i++) - _isFree[i] = true; - - for (;;) - { - UInt32 symbol = inBuffer.ReadBits(numBits); - if (inBuffer.ExtraBitsWereRead()) - break; - if (_isFree[symbol]) - return S_FALSE; - if (symbol == 256) - { - UInt32 symbol = inBuffer.ReadBits(numBits); - if (symbol == 1) - { - if (numBits < kNumMaxBits) - numBits++; - } - else if (symbol == 2) - { - if (needPrev) - _isFree[head - 1] = true; - for (i = 257; i < kNumItems; i++) - _isParent[i] = false; - for (i = 257; i < kNumItems; i++) - if (!_isFree[i]) - _isParent[_parents[i]] = true; - for (i = 257; i < kNumItems; i++) - if (!_isParent[i]) - _isFree[i] = true; - head = 257; - while (head < kNumItems && !_isFree[head]) - head++; - if (head < kNumItems) - { - needPrev = true; - _isFree[head] = false; - _parents[head] = (UInt16)lastSymbol; - head++; - } - } - else - return S_FALSE; - continue; - } - UInt32 cur = symbol; - i = 0; - int corectionIndex = -1; - while (cur >= 256) - { - if (cur == head - 1) - corectionIndex = i; - _stack[i++] = _suffixes[cur]; - cur = _parents[cur]; - } - _stack[i++] = (Byte)cur; - if (needPrev) - { - _suffixes[head - 1] = (Byte)cur; - if (corectionIndex >= 0) - _stack[corectionIndex] = (Byte)cur; - } - while (i > 0) - outBuffer.WriteByte((_stack[--i])); - while (head < kNumItems && !_isFree[head]) - head++; - if (head < kNumItems) - { - needPrev = true; - _isFree[head] = false; - _parents[head] = (UInt16)symbol; - head++; - } - else - needPrev = false; - lastSymbol = symbol; - - UInt64 nowPos = outBuffer.GetProcessedSize(); - if (progress != NULL && nowPos - prevPos > (1 << 18)) - { - prevPos = nowPos; - UInt64 packSize = inBuffer.GetProcessedSize(); - RINOK(progress->SetRatioInfo(&packSize, &nowPos)); - } - } - return outBuffer.Flush(); -} - -STDMETHODIMP CDecoder ::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - try { return CodeReal(inStream, outStream, inSize, outSize, progress); } - catch(const CInBufferException &e) { return e.ErrorCode; } - catch(const COutBufferException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ShrinkDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/ShrinkDecoder.h deleted file mode 100644 index 9bbecd41b..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ShrinkDecoder.h +++ /dev/null @@ -1,38 +0,0 @@ -// ShrinkDecoder.h - -#ifndef __COMPRESS_SHRINK_DECODER_H -#define __COMPRESS_SHRINK_DECODER_H - -#include "../../Common/MyCom.h" - -#include "../ICoder.h" - -namespace NCompress { -namespace NShrink { - -const int kNumMaxBits = 13; -const UInt32 kNumItems = 1 << kNumMaxBits; - -class CDecoder : - public ICompressCoder, - public CMyUnknownImp -{ - UInt16 _parents[kNumItems]; - Byte _suffixes[kNumItems]; - Byte _stack[kNumItems]; - bool _isFree[kNumItems]; - bool _isParent[kNumItems]; - -public: - MY_UNKNOWN_IMP - - HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ZDecoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/ZDecoder.cpp deleted file mode 100644 index e28c64518..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ZDecoder.cpp +++ /dev/null @@ -1,159 +0,0 @@ -// ZDecoder.cpp - -#include "StdAfx.h" - -#include "../../../C/Alloc.h" - -#include "../Common/InBuffer.h" -#include "../Common/OutBuffer.h" - -#include "ZDecoder.h" - -namespace NCompress { -namespace NZ { - -static const UInt32 kBufferSize = (1 << 20); -static const Byte kNumBitsMask = 0x1F; -static const Byte kBlockModeMask = 0x80; -static const int kNumMinBits = 9; -static const int kNumMaxBits = 16; - -void CDecoder::Free() -{ - MyFree(_parents); _parents = 0; - MyFree(_suffixes); _suffixes = 0; - MyFree(_stack); _stack = 0; -} - -CDecoder::~CDecoder() { Free(); } - -HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 * /* inSize */, const UInt64 * /* outSize */, ICompressProgressInfo *progress) -{ - CInBuffer inBuffer; - COutBuffer outBuffer; - - if (!inBuffer.Create(kBufferSize)) - return E_OUTOFMEMORY; - inBuffer.SetStream(inStream); - inBuffer.Init(); - - if (!outBuffer.Create(kBufferSize)) - return E_OUTOFMEMORY; - outBuffer.SetStream(outStream); - outBuffer.Init(); - - int maxbits = _properties & kNumBitsMask; - if (maxbits < kNumMinBits || maxbits > kNumMaxBits) - return S_FALSE; - UInt32 numItems = 1 << maxbits; - bool blockMode = ((_properties & kBlockModeMask) != 0); - - if (maxbits != _numMaxBits || _parents == 0 || _suffixes == 0 || _stack == 0) - { - Free(); - _parents = (UInt16 *)MyAlloc(numItems * sizeof(UInt16)); if (_parents == 0) return E_OUTOFMEMORY; - _suffixes = (Byte *)MyAlloc(numItems * sizeof(Byte)); if (_suffixes == 0) return E_OUTOFMEMORY; - _stack = (Byte *)MyAlloc(numItems * sizeof(Byte)); if (_stack == 0) return E_OUTOFMEMORY; - _numMaxBits = maxbits; - } - - UInt64 prevPos = 0; - int numBits = kNumMinBits; - UInt32 head = blockMode ? 257 : 256; - - bool needPrev = false; - - unsigned bitPos = 0; - unsigned numBufBits = 0; - - Byte buf[kNumMaxBits + 4]; - - _parents[256] = 0; // virus protection - _suffixes[256] = 0; - - for (;;) - { - if (numBufBits == bitPos) - { - numBufBits = (unsigned)inBuffer.ReadBytes(buf, numBits) * 8; - bitPos = 0; - UInt64 nowPos = outBuffer.GetProcessedSize(); - if (progress != NULL && nowPos - prevPos >= (1 << 18)) - { - prevPos = nowPos; - UInt64 packSize = inBuffer.GetProcessedSize(); - RINOK(progress->SetRatioInfo(&packSize, &nowPos)); - } - } - unsigned bytePos = bitPos >> 3; - UInt32 symbol = buf[bytePos] | ((UInt32)buf[bytePos + 1] << 8) | ((UInt32)buf[bytePos + 2] << 16); - symbol >>= (bitPos & 7); - symbol &= (1 << numBits) - 1; - bitPos += numBits; - if (bitPos > numBufBits) - break; - if (symbol >= head) - return S_FALSE; - if (blockMode && symbol == 256) - { - numBufBits = bitPos = 0; - numBits = kNumMinBits; - head = 257; - needPrev = false; - continue; - } - UInt32 cur = symbol; - int i = 0; - while (cur >= 256) - { - _stack[i++] = _suffixes[cur]; - cur = _parents[cur]; - } - _stack[i++] = (Byte)cur; - if (needPrev) - { - _suffixes[head - 1] = (Byte)cur; - if (symbol == head - 1) - _stack[0] = (Byte)cur; - } - do - outBuffer.WriteByte((_stack[--i])); - while (i > 0); - if (head < numItems) - { - needPrev = true; - _parents[head++] = (UInt16)symbol; - if (head > ((UInt32)1 << numBits)) - { - if (numBits < maxbits) - { - numBufBits = bitPos = 0; - numBits++; - } - } - } - else - needPrev = false; - } - return outBuffer.Flush(); -} - -STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - try { return CodeReal(inStream, outStream, inSize, outSize, progress); } - catch(const CInBufferException &e) { return e.ErrorCode; } - catch(const COutBufferException &e) { return e.ErrorCode; } - catch(...) { return S_FALSE; } -} - -STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size) -{ - if (size < 1) - return E_INVALIDARG; - _properties = data[0]; - return S_OK; -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ZDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/ZDecoder.h deleted file mode 100644 index 2bd83a177..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ZDecoder.h +++ /dev/null @@ -1,42 +0,0 @@ -// ZDecoder.h - -#ifndef __COMPRESS_Z_DECODER_H -#define __COMPRESS_Z_DECODER_H - -#include "../../Common/MyCom.h" - -#include "../ICoder.h" - -namespace NCompress { -namespace NZ { - -class CDecoder: - public ICompressCoder, - public ICompressSetDecoderProperties2, - public CMyUnknownImp -{ - UInt16 *_parents; - Byte *_suffixes; - Byte *_stack; - Byte _properties; - int _numMaxBits; - -public: - CDecoder(): _parents(0), _suffixes(0), _stack(0), _properties(0), _numMaxBits(0) {}; - ~CDecoder(); - void Free(); - - MY_UNKNOWN_IMP1(ICompressSetDecoderProperties2) - - HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ZlibDecoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/ZlibDecoder.cpp deleted file mode 100644 index 90d6715db..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ZlibDecoder.cpp +++ /dev/null @@ -1,89 +0,0 @@ -// ZlibDecoder.cpp - -#include "StdAfx.h" - -#include "../Common/StreamUtils.h" - -#include "ZlibDecoder.h" - -namespace NCompress { -namespace NZlib { - -#define DEFLATE_TRY_BEGIN try { -#define DEFLATE_TRY_END } catch(...) { return S_FALSE; } - -#define ADLER_MOD 65521 -#define ADLER_LOOP_MAX 5550 - -UInt32 Adler32_Update(UInt32 adler, const Byte *buf, size_t size) -{ - UInt32 a = adler & 0xFFFF; - UInt32 b = (adler >> 16) & 0xFFFF; - while (size > 0) - { - unsigned curSize = (size > ADLER_LOOP_MAX) ? ADLER_LOOP_MAX : (unsigned )size; - unsigned i; - for (i = 0; i < curSize; i++) - { - a += buf[i]; - b += a; - } - buf += curSize; - size -= curSize; - a %= ADLER_MOD; - b %= ADLER_MOD; - } - return (b << 16) + a; -} - -STDMETHODIMP COutStreamWithAdler::Write(const void *data, UInt32 size, UInt32 *processedSize) -{ - HRESULT result = _stream->Write(data, size, &size); - _adler = Adler32_Update(_adler, (const Byte *)data, size); - if (processedSize != NULL) - *processedSize = size; - return result; -} - -STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) -{ - DEFLATE_TRY_BEGIN - if (!AdlerStream) - AdlerStream = AdlerSpec = new COutStreamWithAdler; - if (!DeflateDecoder) - { - DeflateDecoderSpec = new NDeflate::NDecoder::CCOMCoder; - DeflateDecoderSpec->ZlibMode = true; - DeflateDecoder = DeflateDecoderSpec; - } - - Byte buf[2]; - RINOK(ReadStream_FALSE(inStream, buf, 2)); - int method = buf[0] & 0xF; - if (method != 8) - return S_FALSE; - // int dicSize = buf[0] >> 4; - if ((((UInt32)buf[0] << 8) + buf[1]) % 31 != 0) - return S_FALSE; - if ((buf[1] & 0x20) != 0) // dictPresent - return S_FALSE; - // int level = (buf[1] >> 6); - - AdlerSpec->SetStream(outStream); - AdlerSpec->Init(); - HRESULT res = DeflateDecoder->Code(inStream, AdlerStream, inSize, outSize, progress); - AdlerSpec->ReleaseStream(); - - if (res == S_OK) - { - const Byte *p = DeflateDecoderSpec->ZlibFooter; - UInt32 adler = ((UInt32)p[0] << 24) | ((UInt32)p[1] << 16) | ((UInt32)p[2] << 8) | p[3]; - if (adler != AdlerSpec->GetAdler()) - return S_FALSE; - } - return res; - DEFLATE_TRY_END -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ZlibDecoder.h b/src/libs/7zip/win/CPP/7zip/Compress/ZlibDecoder.h deleted file mode 100644 index 95c110022..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ZlibDecoder.h +++ /dev/null @@ -1,48 +0,0 @@ -// ZlibDecoder.h - -#ifndef __ZLIB_DECODER_H -#define __ZLIB_DECODER_H - -#include "DeflateDecoder.h" - -namespace NCompress { -namespace NZlib { - -const UInt32 ADLER_INIT_VAL = 1; - -class COutStreamWithAdler: - public ISequentialOutStream, - public CMyUnknownImp -{ - CMyComPtr<ISequentialOutStream> _stream; - UInt32 _adler; -public: - MY_UNKNOWN_IMP - STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); - void SetStream(ISequentialOutStream *stream) { _stream = stream; } - void ReleaseStream() { _stream.Release(); } - void Init() { _adler = ADLER_INIT_VAL; } - UInt32 GetAdler() const { return _adler; } -}; - -class CDecoder: - public ICompressCoder, - public CMyUnknownImp -{ - COutStreamWithAdler *AdlerSpec; - CMyComPtr<ISequentialOutStream> AdlerStream; - - NCompress::NDeflate::NDecoder::CCOMCoder *DeflateDecoderSpec; - CMyComPtr<ICompressCoder> DeflateDecoder; -public: - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - - UInt64 GetInputProcessedSize() const { return DeflateDecoderSpec->GetInputProcessedSize() + 2; } - - MY_UNKNOWN_IMP -}; - -}} - -#endif diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ZlibEncoder.cpp b/src/libs/7zip/win/CPP/7zip/Compress/ZlibEncoder.cpp deleted file mode 100644 index 09235c337..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ZlibEncoder.cpp +++ /dev/null @@ -1,61 +0,0 @@ -// ZlibEncoder.cpp - -#include "StdAfx.h" - -#include "../Common/StreamUtils.h" - -#include "ZlibEncoder.h" - -namespace NCompress { -namespace NZlib { - -#define DEFLATE_TRY_BEGIN try { -#define DEFLATE_TRY_END } catch(...) { return S_FALSE; } - -UInt32 Adler32_Update(UInt32 adler, const Byte *buf, size_t size); - -STDMETHODIMP CInStreamWithAdler::Read(void *data, UInt32 size, UInt32 *processedSize) -{ - HRESULT result = _stream->Read(data, size, &size); - _adler = Adler32_Update(_adler, (const Byte *)data, size); - _size += size; - if (processedSize != NULL) - *processedSize = size; - return result; -} - -void CEncoder::Create() -{ - if (!DeflateEncoder) - DeflateEncoder = DeflateEncoderSpec = new NDeflate::NEncoder::CCOMCoder; -} - -STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 * /* outSize */, ICompressProgressInfo *progress) -{ - DEFLATE_TRY_BEGIN - if (!AdlerStream) - AdlerStream = AdlerSpec = new CInStreamWithAdler; - Create(); - - { - Byte buf[2] = { 0x78, 0xDA }; - RINOK(WriteStream(outStream, buf, 2)); - } - - AdlerSpec->SetStream(inStream); - AdlerSpec->Init(); - HRESULT res = DeflateEncoder->Code(AdlerStream, outStream, inSize, NULL, progress); - AdlerSpec->ReleaseStream(); - - RINOK(res); - - { - UInt32 a = AdlerSpec->GetAdler(); - Byte buf[4] = { (Byte)(a >> 24), (Byte)(a >> 16), (Byte)(a >> 8), (Byte)(a) }; - return WriteStream(outStream, buf, 4); - } - DEFLATE_TRY_END -} - -}} diff --git a/src/libs/7zip/win/CPP/7zip/Compress/ZlibEncoder.h b/src/libs/7zip/win/CPP/7zip/Compress/ZlibEncoder.h deleted file mode 100644 index 621cc1d08..000000000 --- a/src/libs/7zip/win/CPP/7zip/Compress/ZlibEncoder.h +++ /dev/null @@ -1,48 +0,0 @@ -// ZlibEncoder.h - -#ifndef __ZLIB_ENCODER_H -#define __ZLIB_ENCODER_H - -#include "DeflateEncoder.h" - -namespace NCompress { -namespace NZlib { - -class CInStreamWithAdler: - public ISequentialInStream, - public CMyUnknownImp -{ - CMyComPtr<ISequentialInStream> _stream; - UInt32 _adler; - UInt64 _size; -public: - MY_UNKNOWN_IMP - STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); - void SetStream(ISequentialInStream *stream) { _stream = stream; } - void ReleaseStream() { _stream.Release(); } - void Init() { _adler = 1; _size = 0; } // ADLER_INIT_VAL - UInt32 GetAdler() const { return _adler; } - UInt64 GetSize() const { return _size; } -}; - -class CEncoder: - public ICompressCoder, - public CMyUnknownImp -{ - CInStreamWithAdler *AdlerSpec; - CMyComPtr<ISequentialInStream> AdlerStream; - CMyComPtr<ICompressCoder> DeflateEncoder; -public: - NCompress::NDeflate::NEncoder::CCOMCoder *DeflateEncoderSpec; - - void Create(); - STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, - const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); - UInt64 GetInputProcessedSize() const { return AdlerSpec->GetSize(); } - - MY_UNKNOWN_IMP -}; - -}} - -#endif |