This commit is contained in:
Igor Pavlov
2005-05-30 00:00:00 +00:00
committed by Kornel Lesiński
parent 8c1b5c7b7e
commit 3c510ba80b
926 changed files with 40559 additions and 23519 deletions

View File

@@ -115,130 +115,6 @@ SOURCE=.\StdAfx.cpp
# Begin Source File
SOURCE=.\StdAfx.h
# End Source File
# End Group
# Begin Group "Origianl"
# PROP Default_Filter ""
# Begin Source File
SOURCE=.\Original\blocksort.c
!IF "$(CFG)" == "BZip2 - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "BZip2 - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\Original\bzlib.c
!IF "$(CFG)" == "BZip2 - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "BZip2 - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\Original\bzlib.h
# End Source File
# Begin Source File
SOURCE=.\Original\bzlib_private.h
# End Source File
# Begin Source File
SOURCE=.\Original\compress.c
!IF "$(CFG)" == "BZip2 - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "BZip2 - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\Original\crctable.c
!IF "$(CFG)" == "BZip2 - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "BZip2 - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\Original\decompress.c
!IF "$(CFG)" == "BZip2 - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "BZip2 - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\Original\huffman.c
!IF "$(CFG)" == "BZip2 - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "BZip2 - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\Original\randtable.c
!IF "$(CFG)" == "BZip2 - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "BZip2 - Win32 Debug"
# SUBTRACT CPP /YX /Yc /Yu
!ENDIF
# End Source File
# End Group
# Begin Group "Common"
@@ -246,6 +122,14 @@ SOURCE=.\Original\randtable.c
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\..\Common\Alloc.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\Alloc.h
# End Source File
# Begin Source File
SOURCE=..\..\..\Common\NewHandler.cpp
# End Source File
# Begin Source File
@@ -253,6 +137,94 @@ SOURCE=..\..\..\Common\NewHandler.cpp
SOURCE=..\..\..\Common\NewHandler.h
# End Source File
# End Group
# Begin Group "Huffman"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\Huffman\HuffmanDecoder.h
# End Source File
# Begin Source File
SOURCE=..\Huffman\HuffmanEncoder.cpp
!IF "$(CFG)" == "BZip2 - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "BZip2 - Win32 Debug"
!ENDIF
# End Source File
# Begin Source File
SOURCE=..\Huffman\HuffmanEncoder.h
# End Source File
# End Group
# Begin Group "7-Zip Common"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\Common\InBuffer.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Common\InBuffer.h
# End Source File
# Begin Source File
SOURCE=..\..\Common\MSBFDecoder.h
# End Source File
# Begin Source File
SOURCE=..\..\Common\MSBFEncoder.h
# End Source File
# Begin Source File
SOURCE=..\..\Common\OutBuffer.cpp
# End Source File
# Begin Source File
SOURCE=..\..\Common\OutBuffer.h
# End Source File
# End Group
# Begin Group "BWT"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\BWT\BlockSort.cpp
!IF "$(CFG)" == "BZip2 - Win32 Release"
# ADD CPP /O2
# SUBTRACT CPP /YX /Yc /Yu
!ELSEIF "$(CFG)" == "BZip2 - Win32 Debug"
!ENDIF
# End Source File
# Begin Source File
SOURCE=..\BWT\BlockSort.h
# End Source File
# Begin Source File
SOURCE=..\BWT\Mtf8.h
# End Source File
# End Group
# Begin Source File
SOURCE=.\BZip2CRC.cpp
# End Source File
# Begin Source File
SOURCE=.\BZip2CRC.h
# End Source File
# Begin Source File
SOURCE=.\BZip2Decoder.cpp
@@ -289,9 +261,5 @@ SOURCE=.\BZip2Encoder.cpp
SOURCE=.\BZip2Encoder.h
# End Source File
# Begin Source File
SOURCE=.\BZip2Error.cpp
# End Source File
# End Target
# End Project

View File

@@ -0,0 +1,26 @@
// 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_CRCTableInit;

30
7zip/Compress/BZip2/BZip2CRC.h Executable file
View File

@@ -0,0 +1,30 @@
// 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); }
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

View File

@@ -0,0 +1,54 @@
// 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

View File

@@ -4,122 +4,402 @@
#include "BZip2Decoder.h"
#include "Windows/Defs.h"
#include "Original/bzlib.h"
#include "../../../Common/Alloc.h"
#include "../../../Common/Defs.h"
#include "../BWT/Mtf8.h"
#include "BZip2CRC.h"
namespace NCompress {
namespace NBZip2 {
static const UINT32 kBufferSize = (1 << 20);
static const UInt32 kBufferSize = (1 << 17);
CDecoder::CDecoder()
{
m_InBuffer = new BYTE[kBufferSize];
m_OutBuffer = new BYTE[kBufferSize];
}
CDecoder::~CDecoder()
{
delete []m_OutBuffer;
delete []m_InBuffer;
}
struct CBZip2Decompressor: public bz_stream
{
// bz_stream m_Object;
public:
int Init(int verbosity, int small) { return BZ2_bzDecompressInit(this, verbosity, small); }
int Decompress() { return BZ2_bzDecompress(this); }
int End() { return BZ2_bzDecompressEnd(this); }
UINT64 GetTotalIn() const { return (UINT64(total_in_hi32) << 32) + total_in_lo32; }
UINT64 GetTotalOut() const { return (UINT64(total_out_hi32) << 32) + total_out_lo32; }
static Int16 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
};
class CBZip2DecompressorReleaser
CState::~CState()
{
CBZip2Decompressor *m_Decompressor;
public:
CBZip2DecompressorReleaser(CBZip2Decompressor *aDecompressor): m_Decompressor(aDecompressor) {}
void Diable() { m_Decompressor = NULL; }
~CBZip2DecompressorReleaser() { if (m_Decompressor != NULL) m_Decompressor->End(); }
};
::BigFree(tt);
}
STDMETHODIMP CDecoder::CodeReal(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UINT64 *inSize, const UINT64 *outSize,
ICompressProgressInfo *progress)
bool CState::Alloc()
{
CBZip2Decompressor bzStream;
bzStream.bzalloc = NULL;
bzStream.bzfree = NULL;
bzStream.opaque = NULL;
if (tt == 0)
tt = (UInt32 *)BigAlloc(kBlockSizeMax * sizeof(UInt32));
return (tt != 0);
}
UInt32 CDecoder::ReadBits(int numBits) { return m_InStream.ReadBits(numBits); }
Byte CDecoder::ReadByte() {return (Byte)ReadBits(8); }
bool CDecoder::ReadBit() { return ReadBits(1) != 0; }
int result = bzStream.Init(0, 0);
switch(result)
UInt32 CDecoder::ReadCRC()
{
UInt32 crc = 0;
for (int i = 0; i < 4; i++)
{
case BZ_OK:
break;
case BZ_MEM_ERROR:
return E_OUTOFMEMORY;
default:
return E_FAIL;
crc <<= 8;
crc |= ReadByte();
}
CBZip2DecompressorReleaser releaser(&bzStream);
bzStream.avail_in = 0;
while (true)
return crc;
}
HRESULT CDecoder::ReadBlock(UInt32 blockSizeMax, CState &state)
{
state.BlockRandomised = ReadBit();
state.OrigPtr = ReadBits(kNumOrigBits);
// in original code it compares OrigPtr to (UInt32)(10 + blockSizeMax)) : why ?
if (state.OrigPtr >= blockSizeMax)
return S_FALSE;
CMtf8Decoder mtf;
int numInUse = 0;
{
if (bzStream.avail_in == 0)
{
bzStream.next_in = (char *)m_InBuffer;
UINT32 processedSize;
RINOK(inStream->Read(m_InBuffer, kBufferSize, &processedSize));
bzStream.avail_in = processedSize;
}
bool inUse16[16];
int i;
for (i = 0; i < 16; i++)
inUse16[i] = ReadBit();
for (i = 0; i < 256; i++)
if (inUse16[i >> 4])
if (ReadBit())
mtf.Buffer[numInUse++] = (Byte)i;
if (numInUse == 0)
return S_FALSE;
mtf.Init(numInUse);
}
int alphaSize = numInUse + 2;
bzStream.next_out = (char *)m_OutBuffer;
bzStream.avail_out = kBufferSize;
result = bzStream.Decompress();
UINT32 numBytesToWrite = kBufferSize - bzStream.avail_out;
if (numBytesToWrite > 0)
{
UINT32 processedSize;
RINOK(outStream->Write(m_OutBuffer, numBytesToWrite, &processedSize));
if (numBytesToWrite != processedSize)
return E_FAIL;
}
int numTables = ReadBits(kNumTablesBits);
if (numTables < kNumTablesMin || numTables > kNumTablesMax)
return S_FALSE;
UInt32 numSelectors = ReadBits(kNumSelectorsBits);
if (numSelectors < 1 || numSelectors > kNumSelectorsMax)
return S_FALSE;
if (result == BZ_STREAM_END)
break;
switch(result)
{
Byte mtfPos[kNumTablesMax];
int t = 0;
do
mtfPos[t] = (Byte)t;
while(++t < numTables);
UInt32 i = 0;
do
{
case BZ_DATA_ERROR:
case BZ_DATA_ERROR_MAGIC:
return S_FALSE;
case BZ_OK:
int j = 0;
while (ReadBit())
if (++j >= numTables)
return S_FALSE;
Byte tmp = mtfPos[j];
for (;j > 0; j--)
mtfPos[j] = mtfPos[j - 1];
state.m_Selectors[i] = mtfPos[0] = tmp;
}
while(++i < numSelectors);
}
int t = 0;
do
{
Byte lens[kMaxAlphaSize];
int len = (int)ReadBits(kNumLevelsBits);
int i;
for (i = 0; i < alphaSize; i++)
{
while (true)
{
if (len < 1 || len > kMaxHuffmanLen)
return S_FALSE;
if (!ReadBit())
break;
if (ReadBit())
len--;
else
len++;
}
lens[i] = (Byte)len;
}
for (; i < kMaxAlphaSize; i++)
lens[i] = 0;
m_HuffmanDecoders[t].SetCodeLengths(lens);
}
while(++t < numTables);
{
for (int i = 0; i < 256; i++)
state.CharCounters[i] = 0;
}
UInt32 blockSize = 0;
{
UInt32 groupIndex = 0;
UInt32 groupSize = 0;
CHuffmanDecoder *huffmanDecoder = 0;
int runPower = 0;
UInt32 runCounter = 0;
while (true)
{
if (groupSize == 0)
{
if (groupIndex >= numSelectors)
return S_FALSE;
groupSize = kGroupSize;
huffmanDecoder = &m_HuffmanDecoders[state.m_Selectors[groupIndex++]];
}
groupSize--; \
int nextSym = (int)huffmanDecoder->DecodeSymbol(&m_InStream);
if (nextSym < 2)
{
runCounter += ((UInt32)(nextSym + 1) << runPower++);
if (blockSizeMax - blockSize < runCounter)
return S_FALSE;
continue;
}
if (runCounter != 0)
{
Byte b = mtf.GetHead();
state.CharCounters[b] += runCounter;
do
state.tt[blockSize++] = (UInt32)b;
while(--runCounter != 0);
runPower = 0;
}
if (nextSym > numInUse)
break;
case BZ_MEM_ERROR:
return E_OUTOFMEMORY;
default:
return E_FAIL;
}
if (progress != NULL)
{
UINT64 totalIn = bzStream.GetTotalIn();
UINT64 totalOut = bzStream.GetTotalOut();
RINOK(progress->SetRatioInfo(&totalIn, &totalOut));
Byte b = mtf.GetAndMove(nextSym - 1);
if (blockSize >= blockSizeMax)
return S_FALSE;
state.CharCounters[b]++;
state.tt[blockSize++] = (UInt32)b;
}
}
// result = bzStream.End();
if (state.OrigPtr >= blockSize)
return S_FALSE;
state.BlockSize = blockSize;
return S_OK;
}
HRESULT CState::DecodeBlock(COutBuffer &m_OutStream)
{
UInt32 *charCounters = this->CharCounters;
{
UInt32 sum = 0;
for (UInt32 i = 0; i < 256; i++)
{
sum += charCounters[i];
charCounters[i] = sum - charCounters[i];
}
}
// Compute the T^(-1) vector
UInt32 blockSize = this->BlockSize;
UInt32 i = 0;
do
tt[charCounters[tt[i] & 0xFF]++] |= (i << 8);
while(++i < blockSize);
// Decode
CBZip2CRC crc;
UInt32 randIndex = 1;
UInt32 randToGo = kRandNums[0] - 2;
int numReps = 0;
// it's for speed optimization: prefetch & prevByte_init;
UInt32 tPos = tt[tt[OrigPtr] >> 8];
Byte prevByte = (Byte)(tPos & 0xFF);
do
{
Byte b = (Byte)(tPos & 0xFF);
tPos = tt[tPos >> 8];
if (BlockRandomised)
{
if (randToGo == 0)
{
b ^= 1;
randToGo = kRandNums[randIndex++];
randIndex &= 0x1FF;
}
randToGo--;
}
if (numReps == kRleModeRepSize)
{
for (; b > 0; b--)
{
crc.UpdateByte(prevByte);
m_OutStream.WriteByte(prevByte);
}
numReps = 0;
continue;
}
if (prevByte == b)
numReps++;
else
{
numReps = 1;
prevByte = b;
}
crc.UpdateByte(b);
m_OutStream.WriteByte(b);
}
while(--blockSize != 0);
return (StoredCRC == crc.GetDigest()) ? S_OK : S_FALSE;
}
HRESULT CDecoder::DecodeFile(bool &isBZ, ICompressProgressInfo *progress)
{
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;
if (!m_State.Alloc())
return E_OUTOFMEMORY;
CBZip2CombinedCRC computedCombinedCRC;
while (true)
{
if (progress)
{
UInt64 packSize = m_InStream.GetProcessedSize();
UInt64 unpackSize = m_OutStream.GetProcessedSize();
RINOK(progress->SetRatioInfo(&packSize, &unpackSize));
}
for (i = 0; i < 6; i++)
s[i] = ReadByte();
UInt32 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;
return (crc == computedCombinedCRC.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;
m_State.StoredCRC = crc;
computedCombinedCRC.Update(crc);
RINOK(ReadBlock(dicSize, m_State));
RINOK(m_State.DecodeBlock(m_OutStream));
}
}
HRESULT CDecoder::CodeReal(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize,
ICompressProgressInfo *progress)
{
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();
CDecoderFlusher flusher(this);
bool isBZ;
RINOK(DecodeFile(isBZ, progress));
return isBZ ? S_OK: S_FALSE;
}
STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UINT64 *inSize, const UINT64 *outSize,
ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize,
ICompressProgressInfo *progress)
{
try { return CodeReal(inStream, outStream, inSize, outSize, progress); }
catch(...) { return S_FALSE; }
}
STDMETHODIMP CDecoder::GetInStreamProcessedSize(UInt64 *value)
{
if (value == NULL)
return E_INVALIDARG;
*value = m_InStream.GetProcessedSize();
return S_OK;
}
}}

View File

@@ -1,39 +1,88 @@
// Compress/BZip2/Decoder.h
#pragma once
#ifndef __COMPRESS_BZIP2_DECODER_H
#define __COMPRESS_BZIP2_DECODER_H
#include "../../ICoder.h"
// #include "../../Interface/CompressInterface.h"
#include "Common/MyCom.h"
#include "../../../Common/MyCom.h"
#include "../../Common/MSBFDecoder.h"
#include "../../Common/InBuffer.h"
#include "../../Common/OutBuffer.h"
#include "../Huffman/HuffmanDecoder.h"
#include "BZip2Const.h"
namespace NCompress {
namespace NBZip2 {
typedef NCompress::NHuffman::CDecoder<kMaxHuffmanLen, kMaxAlphaSize> CHuffmanDecoder;
struct CState
{
UInt32 *tt;
bool BlockRandomised;
UInt32 OrigPtr;
UInt32 BlockSize;
UInt32 CharCounters[256];
Byte m_Selectors[kNumSelectorsMax];
UInt32 StoredCRC;
CState(): tt(0) {}
~CState();
bool Alloc();
HRESULT DecodeBlock(COutBuffer &m_OutStream);
};
class CDecoder :
public ICompressCoder,
public ICompressGetInStreamProcessedSize,
public CMyUnknownImp
{
BYTE *m_InBuffer;
BYTE *m_OutBuffer;
NStream::NMSBF::CDecoder<CInBuffer> m_InStream;
COutBuffer m_OutStream;
CHuffmanDecoder m_HuffmanDecoders[kNumTablesMax];
CState m_State;
UInt32 ReadBits(int numBits);
Byte ReadByte();
bool ReadBit();
UInt32 ReadCRC();
HRESULT ReadBlock(UInt32 blockSizeMax, CState &state);
HRESULT PrepareBlock(CState &state);
HRESULT DecodeFile(bool &isBZ, ICompressProgressInfo *progress);
HRESULT CodeReal(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize,
ICompressProgressInfo *progress);
class CDecoderFlusher
{
CDecoder *_decoder;
public:
bool NeedFlush;
CDecoderFlusher(CDecoder *decoder): _decoder(decoder), NeedFlush(true) {}
~CDecoderFlusher()
{
if (NeedFlush)
_decoder->Flush();
_decoder->ReleaseStreams();
}
};
public:
CDecoder();
~CDecoder();
MY_UNKNOWN_IMP
HRESULT Flush();
// void (ReleaseStreams)();
STDMETHOD(CodeReal)(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UINT64 *inSize, const UINT64 *outSize,
ICompressProgressInfo *progress);
HRESULT Flush() { return m_OutStream.Flush(); }
void ReleaseStreams()
{
m_InStream.ReleaseStream();
m_OutStream.ReleaseStream();
}
MY_UNKNOWN_IMP1(ICompressGetInStreamProcessedSize)
STDMETHOD(Code)(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UINT64 *inSize, const UINT64 *outSize,
ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize,
ICompressProgressInfo *progress);
STDMETHOD(GetInStreamProcessedSize)(UInt64 *value);
};
}}

View File

@@ -2,119 +2,632 @@
#include "StdAfx.h"
#include "Windows/Defs.h"
#include "BZip2Encoder.h"
#include "Original/bzlib.h"
#include "../../../Common/Alloc.h"
#include "../BWT/Mtf8.h"
#include "BZip2CRC.h"
namespace NCompress {
namespace NBZip2 {
static const UINT32 kBufferSize = (1 << 20);
static const UInt32 kBufferSize = (1 << 17);
static const int kNumHuffPasses = 4;
CEncoder::CEncoder()
{
m_InBuffer = new BYTE[kBufferSize];
m_OutBuffer = new BYTE[kBufferSize];
}
CEncoder::CEncoder():
m_Block(0),
m_NeedHuffmanCreate(true),
m_NumPasses(1),
m_OptimizeNumTables(false),
m_BlockSizeMult(kBlockSizeMultMax)
{}
CEncoder::~CEncoder()
{
delete []m_OutBuffer;
delete []m_InBuffer;
::BigFree(m_Block);
}
struct CBZip2Compressor: public bz_stream
UInt32 CEncoder::ReadRleBlock(Byte *buffer)
{
public:
int Init(int blockSize100k, int verbosity, int small)
{ return BZ2_bzCompressInit(this, blockSize100k, verbosity, small); }
int Compress(int action ) { return BZ2_bzCompress(this, action ); }
int End() { return BZ2_bzCompressEnd(this); }
UINT64 GetTotalIn() const { return (UINT64(total_in_hi32) << 32) + total_in_lo32; }
UINT64 GetTotalOut() const { return (UINT64(total_out_hi32) << 32) + total_out_lo32; }
};
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++] = numReps - kRleModeRepSize;
buffer[i++] = b;
numReps = 1;
prevByte = b;
continue;
}
numReps++;
if (numReps <= kRleModeRepSize)
buffer[i++] = b;
else if (numReps == kRleModeRepSize + 255)
{
buffer[i++] = numReps - kRleModeRepSize;
numReps = 0;
}
}
// it's to support original BZip2 decoder
if (numReps >= kRleModeRepSize)
buffer[i++] = numReps - kRleModeRepSize;
}
return i;
}
class CBZip2CompressorReleaser
void CEncoder::WriteBits2(UInt32 value, UInt32 numBits)
{ m_OutStreamCurrent->WriteBits(value, numBits); }
void CEncoder::WriteByte2(Byte b) { WriteBits2(b , 8); }
void CEncoder::WriteBit2(bool v) { WriteBits2((v ? 1 : 0), 1); }
void CEncoder::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 CEncoder::EncodeBlock(Byte *block, UInt32 blockSize)
{
CBZip2Compressor *m_Compressor;
public:
CBZip2CompressorReleaser(CBZip2Compressor *compressor): m_Compressor(compressor) {}
void Disable() { m_Compressor = NULL; }
~CBZip2CompressorReleaser() { if (m_Compressor != NULL) m_Compressor->End(); }
};
WriteBit2(false); // Randomised = false
{
UInt32 origPtr = m_BlockSorter.Sort(block, 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.Buffer[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;
STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UINT64 *inSize, const UINT64 *outSize,
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;
do
{
UInt32 index = m_BlockSorter.Indices[i];
if (index == 0)
index = blockSize - 1;
else
index--;
int pos = mtf.FindAndMove(block[index]);
if (pos == 0)
rleSize++;
else
{
while (rleSize != 0)
{
rleSize--;
mtfs[mtfArraySize++] = (rleSize & 1);
symbolCounts[rleSize & 1]++;
rleSize >>= 1;
}
if (pos >= 0xFE)
{
mtfs[mtfArraySize++] = 0xFF;
mtfs[mtfArraySize++] = pos - 0xFE;
}
else
mtfs[mtfArraySize++] = pos + 1;
symbolCounts[pos + 1]++;
}
}
while (++i < blockSize);
while (rleSize != 0)
{
rleSize--;
mtfs[mtfArraySize++] = (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();
UInt32 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];
NCompression::NHuffman::CEncoder &huffEncoder = m_HuffEncoders[t - 1];
int i = 0;
do
huffEncoder.m_Items[i].Len = (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
m_HuffEncoders[t].StartNewBlock();
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
{
NCompression::NHuffman::CItem *items = m_HuffEncoders[t].m_Items;
UInt32 price = 0;
int j = 0;
do
price += items[symbols[j]].Len;
while (++j < i);
if (price < bestPrice)
{
m_Selectors[g] = (Byte)t;
bestPrice = price;
}
}
while(++t < numTables);
NCompression::NHuffman::CEncoder &huffEncoder = m_HuffEncoders[m_Selectors[g++]];
int j = 0;
do
huffEncoder.AddSymbol(symbols[j]);
while (++j < i);
}
while (mtfPos < mtfArraySize);
}
int t = 0;
do
{
NCompression::NHuffman::CEncoder &huffEncoder = m_HuffEncoders[t];
int i = 0;
do
if (huffEncoder.m_Items[i].Freq == 0)
huffEncoder.m_Items[i].Freq = 1;
while(++i < alphaSize);
Byte levels[kMaxAlphaSize];
huffEncoder.BuildTree(levels);
}
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
{
NCompression::NHuffman::CItem *items = m_HuffEncoders[t].m_Items;
UInt32 len = items[0].Len;
WriteBits2(len, kNumLevelsBits);
int i = 0;
do
{
UInt32 level = items[i].Len;
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;
NCompression::NHuffman::CEncoder *huffEncoder = 0;
UInt32 mtfPos = 0;
do
{
UInt32 symbol = mtfs[mtfPos++];
if (symbol >= 0xFF)
symbol += mtfs[mtfPos++];
if (groupSize == 0)
{
groupSize = kGroupSize;
huffEncoder = &m_HuffEncoders[m_Selectors[groupIndex++]];
}
groupSize--; \
huffEncoder->CodeOneValue(m_OutStreamCurrent, 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 CEncoder::EncodeBlockWithHeaders(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 CEncoder::EncodeBlock2(CBZip2CombinedCRC &combinedCRC,
Byte *block, UInt32 blockSize, UInt32 numPasses)
{
bool needCompare = false;
CBZip2CombinedCRC specCombinedCRC = combinedCRC;
UInt32 startBytePos = m_OutStreamCurrent->GetBytePos();
UInt32 startPos = m_OutStreamCurrent->GetPos();
UInt32 startCurByte = m_OutStreamCurrent->GetCurByte();
UInt32 endCurByte;
UInt32 endPos;
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(specCombinedCRC, block, blockSize0, numPasses - 1);
EncodeBlock2(specCombinedCRC, 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();
combinedCRC.Update(crcVal);
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);
}
else
{
m_OutStreamCurrent->SetPos(endPos);
m_OutStreamCurrent->SetCurState((endPos & 7), endCurByte);
combinedCRC = specCombinedCRC;
}
}
}
void CEncoder::EncodeBlock3(CBZip2CombinedCRC &combinedCRC, UInt32 blockSize)
{
CMsbfEncoderTemp outStreamTemp;
outStreamTemp.SetStream(m_TempArray);
outStreamTemp.Init();
m_OutStreamCurrent = &outStreamTemp;
EncodeBlock2(combinedCRC, m_Block, blockSize, m_NumPasses);
UInt32 size = outStreamTemp.GetPos();
UInt32 bytesSize = (size / 8);
for (UInt32 i = 0; i < bytesSize; i++)
m_OutStream.WriteBits(m_TempArray[i], 8);
WriteBits(outStreamTemp.GetCurByte(), (size & 7));
}
HRESULT CEncoder::CodeReal(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize,
ICompressProgressInfo *progress)
{
CBZip2Compressor bzStream;
bzStream.bzalloc = NULL;
bzStream.bzfree = NULL;
bzStream.opaque = NULL;
int result = bzStream.Init(9, 0, 0);
switch(result)
if (!m_BlockSorter.Create(kBlockSizeMax))
return E_OUTOFMEMORY;
if (m_Block == 0)
{
case BZ_OK:
break;
case BZ_MEM_ERROR:
m_Block = (Byte *)BigAlloc(kBlockSizeMax * 5 + kBlockSizeMax / 10 + (20 << 10));
if (m_Block == 0)
return E_OUTOFMEMORY;
default:
return E_FAIL;
m_MtfArray = m_Block + kBlockSizeMax;
m_TempArray = m_MtfArray + kBlockSizeMax * 2 + 2;
}
CBZip2CompressorReleaser releaser(&bzStream);
bzStream.avail_in = 0;
if (!m_InStream.Create(kBufferSize))
return E_OUTOFMEMORY;
if (!m_OutStream.Create(kBufferSize))
return E_OUTOFMEMORY;
if (m_NeedHuffmanCreate)
{
for (int i = 0; i < kNumTablesMax; i++)
if (!m_HuffEncoders[i].Create(kMaxAlphaSize, 0, 0, kMaxHuffmanLen))
return E_OUTOFMEMORY;
m_NeedHuffmanCreate = false;
}
m_InStream.SetStream(inStream);
m_InStream.Init();
m_OutStream.SetStream(outStream);
m_OutStream.Init();
CFlusher flusher(this);
CBZip2CombinedCRC combinedCRC;
WriteByte(kArSig0);
WriteByte(kArSig1);
WriteByte(kArSig2);
WriteByte((Byte)(kArSig3 + m_BlockSizeMult));
while (true)
{
if (bzStream.avail_in == 0)
{
bzStream.next_in = (char *)m_InBuffer;
UINT32 processedSize;
RINOK(inStream->Read(m_InBuffer, kBufferSize, &processedSize));
bzStream.avail_in = processedSize;
}
bzStream.next_out = (char *)m_OutBuffer;
bzStream.avail_out = kBufferSize;
bool askFinish = (bzStream.avail_in == 0);
result = bzStream.Compress(askFinish ? BZ_FINISH : BZ_RUN);
UINT32 numBytesToWrite = kBufferSize - bzStream.avail_out;
if (numBytesToWrite > 0)
{
UINT32 processedSize;
RINOK(outStream->Write(m_OutBuffer, numBytesToWrite, &processedSize));
if (numBytesToWrite != processedSize)
return E_FAIL;
}
if (result == BZ_STREAM_END)
UInt32 blockSize = ReadRleBlock(m_Block);
if (blockSize == 0)
break;
switch(result)
EncodeBlock3(combinedCRC, blockSize);
if (progress)
{
case BZ_RUN_OK:
if (!askFinish)
break;
return E_FAIL;
case BZ_FINISH_OK:
if (askFinish)
break;
return E_FAIL;
case BZ_MEM_ERROR:
return E_OUTOFMEMORY;
default:
return E_FAIL;
}
if (progress != NULL)
{
UINT64 totalIn = bzStream.GetTotalIn();
UINT64 totalOut = bzStream.GetTotalOut();
RINOK(progress->SetRatioInfo(&totalIn, &totalOut));
UInt64 packSize = m_InStream.GetProcessedSize();
UInt64 unpackSize = m_OutStream.GetProcessedSize();
RINOK(progress->SetRatioInfo(&packSize, &unpackSize));
}
}
// result = bzStream.End();
WriteByte(kFinSig0);
WriteByte(kFinSig1);
WriteByte(kFinSig2);
WriteByte(kFinSig3);
WriteByte(kFinSig4);
WriteByte(kFinSig5);
WriteCRC(combinedCRC.GetDigest());
return S_OK;
}
STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream,
ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize,
ICompressProgressInfo *progress)
{
try { return CodeReal(inStream, outStream, inSize, outSize, progress); }
catch(...) { return S_FALSE; }
}
HRESULT CEncoder::SetCoderProperties(const PROPID *propIDs,
const PROPVARIANT *properties, UInt32 numProperties)
{
for(UInt32 i = 0; i < numProperties; i++)
{
const PROPVARIANT &property = properties[i];
switch(propIDs[i])
{
case NCoderPropID::kNumPasses:
{
if (property.vt != VT_UI4)
return E_INVALIDARG;
UInt32 numPasses = property.ulVal;
if(numPasses == 0 || numPasses > 10)
return E_INVALIDARG;
m_NumPasses = numPasses;
m_OptimizeNumTables = (m_NumPasses > 1);
break;
}
case NCoderPropID::kDictionarySize:
{
if (property.vt != VT_UI4)
return E_INVALIDARG;
UInt32 dictionary = property.ulVal / kBlockSizeStep;
if (dictionary < kBlockSizeMultMin)
dictionary = kBlockSizeMultMin;
else if (dictionary > kBlockSizeMultMax)
dictionary = kBlockSizeMultMax;
m_BlockSizeMult = dictionary;
break;
}
default:
return E_INVALIDARG;
}
}
return S_OK;
}
}}

View File

@@ -1,34 +1,158 @@
// Compress/BZip2/Encoder.h
#pragma once
#ifndef __COMPRESS_BZIP2_ENCODER_H
#define __COMPRESS_BZIP2_ENCODER_H
#include "Common/MyCom.h"
#include "../../ICoder.h"
#include "../../../Common/MyCom.h"
#include "../../Common/MSBFEncoder.h"
#include "../../Common/InBuffer.h"
#include "../../Common/OutBuffer.h"
#include "../Huffman/HuffmanEncoder.h"
#include "../BWT/BlockSort.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 - (bitPos & 7);
}
void SetCurState(UInt32 bitPos, Byte curByte)
{
m_BitPos = 8 - bitPos;
m_CurByte = curByte;
}
};
class CEncoder :
public ICompressCoder,
public ICompressSetCoderProperties,
public CMyUnknownImp
{
BYTE *m_InBuffer;
BYTE *m_OutBuffer;
Byte *m_Block;
CInBuffer m_InStream;
NStream::NMSBF::CEncoder<COutBuffer> m_OutStream;
CMsbfEncoderTemp *m_OutStreamCurrent;
CBlockSorter m_BlockSorter;
bool m_NeedHuffmanCreate;
NCompression::NHuffman::CEncoder m_HuffEncoders[kNumTablesMax];
Byte *m_MtfArray;
Byte *m_TempArray;
Byte m_Selectors[kNumSelectorsMax];
UInt32 m_BlockSizeMult;
UInt32 m_NumPasses;
bool m_OptimizeNumTables;
UInt32 ReadRleBlock(Byte *buffer);
void WriteBits2(UInt32 value, UInt32 numBits);
void WriteByte2(Byte b);
void WriteBit2(bool v);
void WriteCRC2(UInt32 v);
void WriteBits(UInt32 value, UInt32 numBits);
void WriteByte(Byte b);
void WriteBit(bool v);
void WriteCRC(UInt32 v);
void EncodeBlock(Byte *block, UInt32 blockSize);
UInt32 EncodeBlockWithHeaders(Byte *block, UInt32 blockSize);
void EncodeBlock2(CBZip2CombinedCRC &combinedCRC, Byte *block, UInt32 blockSize, UInt32 numPasses);
void EncodeBlock3(CBZip2CombinedCRC &combinedCRC, UInt32 blockSize);
public:
CEncoder();
~CEncoder();
MY_UNKNOWN_IMP
HRESULT Flush() { return m_OutStream.Flush(); }
void ReleaseStreams()
{
m_InStream.ReleaseStream();
m_OutStream.ReleaseStream();
}
// STDMETHOD(ReleaseStreams)();
class CFlusher
{
CEncoder *_coder;
public:
bool NeedFlush;
CFlusher(CEncoder *coder): _coder(coder), NeedFlush(true) {}
~CFlusher()
{
if (NeedFlush)
_coder->Flush();
_coder->ReleaseStreams();
}
};
MY_UNKNOWN_IMP1(ICompressSetCoderProperties)
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,
ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize,
ICompressProgressInfo *progress);
STDMETHOD(SetCoderProperties)(const PROPID *propIDs,
const PROPVARIANT *properties, UInt32 numProperties);
};
}}

View File

@@ -1,10 +0,0 @@
#include "StdAfx.h"
#include "Original/bzlib.h"
extern "C"
void bz_internal_error (int errcode)
{
throw "error";
}

View File

@@ -2,11 +2,11 @@
#include "StdAfx.h"
#define INITGUID
#include "Common/MyInitGuid.h"
#include "Common/ComTry.h"
#include "BZip2Encoder.h"
#include "BZip2Decoder.h"
#include "Common/ComTry.h"
// {23170F69-40C1-278B-0402-020000000000}
DEFINE_GUID(CLSID_CCompressBZip2Decoder,

View File

@@ -1,3 +1,3 @@
// StdAfx.cpp
#include "stdafx.h"
#include "StdAfx.h"

View File

@@ -1,8 +1,8 @@
// stdafx.h
// StdAfx.h
#ifndef __STDAFX_H
#define __STDAFX_H
#include <windows.h>
#include "../../../Common/MyWindows.h"
#endif
#endif

View File

@@ -66,8 +66,8 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 3,8,1,0
PRODUCTVERSION 3,8,1,0
FILEVERSION 4,19,0,0
PRODUCTVERSION 4,19,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
@@ -85,14 +85,14 @@ BEGIN
VALUE "Comments", "\0"
VALUE "CompanyName", " \0"
VALUE "FileDescription", "BZip2 Coder\0"
VALUE "FileVersion", "3, 8, 1, 0\0"
VALUE "FileVersion", "4, 19, 0, 0\0"
VALUE "InternalName", "BZip2\0"
VALUE "LegalCopyright", "Copyright (C) 1999-2003 Igor Pavlov\0"
VALUE "LegalCopyright", "Copyright (C) 1999-2005 Igor Pavlov\0"
VALUE "LegalTrademarks", "\0"
VALUE "OriginalFilename", "BZip2.dll\0"
VALUE "PrivateBuild", "\0"
VALUE "ProductName", "7-Zip\0"
VALUE "ProductVersion", "3, 8, 1, 0\0"
VALUE "ProductVersion", "4, 19, 0, 0\0"
VALUE "SpecialBuild", "\0"
END
END