├── .gitattributes ├── .gitignore ├── HISTORY.md ├── LZMA ├── 7zip │ ├── Common │ │ ├── FileStreams.cpp │ │ ├── FileStreams.h │ │ ├── InBuffer.cpp │ │ ├── InBuffer.h │ │ ├── OutBuffer.cpp │ │ ├── OutBuffer.h │ │ ├── StdAfx.h │ │ ├── StreamUtils.cpp │ │ └── StreamUtils.h │ ├── Compress │ │ ├── LZ │ │ │ ├── BinTree │ │ │ │ ├── BinTree.h │ │ │ │ ├── BinTree2.h │ │ │ │ ├── BinTree3.h │ │ │ │ ├── BinTree3Z.h │ │ │ │ ├── BinTree4.h │ │ │ │ └── BinTreeMain.h │ │ │ ├── HashChain │ │ │ │ ├── HC2.h │ │ │ │ ├── HC3.h │ │ │ │ ├── HC4.h │ │ │ │ └── HCMain.h │ │ │ ├── IMatchFinder.h │ │ │ ├── LZInWindow.cpp │ │ │ ├── LZInWindow.h │ │ │ ├── LZOutWindow.cpp │ │ │ ├── LZOutWindow.h │ │ │ ├── Patricia │ │ │ │ ├── Pat.h │ │ │ │ ├── Pat2.h │ │ │ │ ├── Pat2H.h │ │ │ │ ├── Pat2R.h │ │ │ │ ├── Pat3H.h │ │ │ │ ├── Pat4H.h │ │ │ │ └── PatMain.h │ │ │ └── StdAfx.h │ │ └── RangeCoder │ │ │ ├── RangeCoder.h │ │ │ ├── RangeCoderBit.cpp │ │ │ ├── RangeCoderBit.h │ │ │ ├── RangeCoderBitTree.h │ │ │ ├── RangeCoderOpt.h │ │ │ └── StdAfx.h │ ├── ICoder.h │ └── IStream.h ├── Common │ ├── Alloc.cpp │ ├── Alloc.h │ ├── CRC.cpp │ ├── CRC.h │ ├── C_FileIO.cpp │ ├── C_FileIO.h │ ├── Defs.h │ ├── MyCom.h │ ├── MyGuidDef.h │ ├── MyInitGuid.h │ ├── MyUnknown.h │ ├── MyWindows.h │ ├── NewHandler.cpp │ ├── NewHandler.h │ ├── StdAfx.h │ ├── String.h │ ├── StringConvert.cpp │ ├── StringConvert.h │ └── Types.h ├── LZMA.h ├── LZMADecoder.cpp ├── LZMADecoder.h ├── LZMAEncoder.cpp ├── LZMAEncoder.h ├── LZMAlib.cpp ├── LZMAlib.h ├── StdAfx.cpp ├── StdAfx.h ├── StreamRam.cpp ├── StreamRam.h ├── StreamRamSolid.h └── Windows │ ├── Defs.h │ ├── FileIO.cpp │ ├── FileIO.h │ └── StdAfx.h ├── Makefile ├── PPMd ├── Coder.hpp ├── Model.cpp ├── PPMd.h ├── PPMdType.h ├── PPMd_read_me.txt ├── PPMdlib.cpp ├── PPMdlib.h └── SubAlloc.hpp ├── README.md ├── src ├── Common.cpp ├── Common.h ├── Decoder.cpp ├── Decoder.h ├── Encoder.cpp ├── Encoder.h ├── LICENSE ├── MemBuffer.cpp ├── MemBuffer.h ├── XWRT.cpp ├── XWRT.h ├── lpaq6.h └── lpaq6.hpp ├── wrt-eng.dic └── zlib ├── adler32.c ├── compress.c ├── crc32.c ├── crc32.h ├── deflate.c ├── deflate.h ├── gzclose.c ├── gzguts.h ├── gzlib.c ├── gzread.c ├── gzwrite.c ├── infback.c ├── inffast.c ├── inffast.h ├── inffixed.h ├── inflate.c ├── inflate.h ├── inftrees.c ├── inftrees.h ├── trees.c ├── trees.h ├── uncompr.c ├── zconf.h ├── zconf2.h ├── zlib.h ├── zutil.c └── zutil.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Directories 2 | _codelite/ 3 | PPMVC/ 4 | 5 | # Archives 6 | *.zip 7 | 8 | # Object files 9 | *.o 10 | *.ko 11 | 12 | # Libraries 13 | *.lib 14 | *.a 15 | 16 | # Shared objects (inc. Windows DLLs) 17 | *.dll 18 | *.so 19 | *.so.* 20 | *.dylib 21 | 22 | # Executables 23 | *.exe 24 | *.out 25 | *.app 26 | 27 | # Visual solution files 28 | *.suo 29 | *.user 30 | -------------------------------------------------------------------------------- /HISTORY.md: -------------------------------------------------------------------------------- 1 | XWRT 3.4 (5.11.2007) 2 | -PPMVC replaced with PPMd var J1 3 | -added support for LZMA and PPMd back-end compression on Linux 4 | 5 | XWRT 3.2 (25.10.2007) 6 | -FastPAQ8 replaced with lpaq6 (compression level 10-14) 7 | 8 | XWRT 3.1 (05.06.2007) 9 | -improved support for XML files encoded in UTF-8 10 | -dictionary is compressed using front compression 11 | -added little-endian/big-endian Unicode (UCS-2) support 12 | -non-textual files are compressed/stored without using a filter 13 | -64-bit compiler support 14 | 15 | XML-WRT 3.0 (14.09.2006) 16 | -internal PPMVC and FastPAQ8 compression 17 | 18 | XML-WRT 2.0 (14.06.2006) 19 | -internal zlib and LZMA compression 20 | -input XML file is split into containers depend on start-tags and end-tags and content under the same tag is sent to the same container 21 | -container for dates in format 1980-02-31 and 01-MAR-1920 22 | -container for times in format 11:30pm 23 | -container for numbers from 1900 to 2155 (years) 24 | -container for pages in format "x-y", where y-x<256, eg. "120-148", "1480-1600" 25 | -container for numbers in format "x-y", eg. "1234-0", "87-623" 26 | -container for two digits after period, eg. "102.00", "12.01" 27 | -container for numbers from 0.0 to 24.9 (one digit after period), eg. "12.0", "9.9" 28 | -urls (statring from "http:"), e-mails (x@y.z), "ü" added to dynamic dictionary 29 | 30 | XML-WRT 1.0 (27.03.2006) 31 | -first public release 32 | -------------------------------------------------------------------------------- /LZMA/7zip/Common/FileStreams.cpp: -------------------------------------------------------------------------------- 1 | // FileStreams.cpp 2 | 3 | #include "StdAfx.h" 4 | 5 | #ifndef _WIN32 6 | #include 7 | #include 8 | #include 9 | #endif 10 | 11 | #include "FileStreams.h" 12 | 13 | static inline HRESULT ConvertBoolToHRESULT(bool result) 14 | { 15 | // return result ? S_OK: E_FAIL; 16 | #ifdef _WIN32 17 | return result ? S_OK: (::GetLastError()); 18 | #else 19 | return result ? S_OK: E_FAIL; 20 | #endif 21 | } 22 | 23 | bool CInFileStream::Open(LPCTSTR fileName) 24 | { 25 | return File.Open(fileName); 26 | } 27 | 28 | #ifdef _WIN32 29 | #ifndef _UNICODE 30 | bool CInFileStream::Open(LPCWSTR fileName) 31 | { 32 | return File.Open(fileName); 33 | } 34 | #endif 35 | #endif 36 | 37 | STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize) 38 | { 39 | #ifdef _WIN32 40 | 41 | UInt32 realProcessedSize; 42 | bool result = File.ReadPart(data, size, realProcessedSize); 43 | if(processedSize != NULL) 44 | *processedSize = realProcessedSize; 45 | return ConvertBoolToHRESULT(result); 46 | 47 | #else 48 | 49 | if(processedSize != NULL) 50 | *processedSize = 0; 51 | ssize_t res = File.Read(data, (size_t)size); 52 | if (res == -1) 53 | return E_FAIL; 54 | if(processedSize != NULL) 55 | *processedSize = (UInt32)res; 56 | return S_OK; 57 | 58 | #endif 59 | } 60 | 61 | #ifndef _WIN32_WCE 62 | STDMETHODIMP CStdInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize) 63 | { 64 | #ifdef _WIN32 65 | UInt32 realProcessedSize; 66 | BOOL res = ::ReadFile(GetStdHandle(STD_INPUT_HANDLE), 67 | data, size, (DWORD *)&realProcessedSize, NULL); 68 | if(processedSize != NULL) 69 | *processedSize = realProcessedSize; 70 | if (res == FALSE && GetLastError() == ERROR_BROKEN_PIPE) 71 | return S_OK; 72 | return ConvertBoolToHRESULT(res != FALSE); 73 | 74 | #else 75 | 76 | if(processedSize != NULL) 77 | *processedSize = 0; 78 | ssize_t res; 79 | do 80 | { 81 | res = read(0, data, (size_t)size); 82 | } 83 | while (res < 0 && (errno == EINTR)); 84 | if (res == -1) 85 | return E_FAIL; 86 | if(processedSize != NULL) 87 | *processedSize = (UInt32)res; 88 | return S_OK; 89 | 90 | #endif 91 | } 92 | 93 | #endif 94 | 95 | STDMETHODIMP CInFileStream::Seek(Int64 offset, UInt32 seekOrigin, 96 | UInt64 *newPosition) 97 | { 98 | if(seekOrigin >= 3) 99 | return STG_E_INVALIDFUNCTION; 100 | 101 | #ifdef _WIN32 102 | 103 | UInt64 realNewPosition; 104 | bool result = File.Seek(offset, seekOrigin, realNewPosition); 105 | if(newPosition != NULL) 106 | *newPosition = realNewPosition; 107 | return ConvertBoolToHRESULT(result); 108 | 109 | #else 110 | 111 | off_t res = File.Seek(offset, seekOrigin); 112 | if (res == -1) 113 | return E_FAIL; 114 | if(newPosition != NULL) 115 | *newPosition = (UInt64)res; 116 | return S_OK; 117 | 118 | #endif 119 | } 120 | 121 | STDMETHODIMP CInFileStream::GetSize(UInt64 *size) 122 | { 123 | return ConvertBoolToHRESULT(File.GetLength(*size)); 124 | } 125 | 126 | 127 | ////////////////////////// 128 | // COutFileStream 129 | 130 | bool COutFileStream::Create(LPCTSTR fileName, bool createAlways) 131 | { 132 | return File.Create(fileName, createAlways); 133 | } 134 | 135 | #ifdef _WIN32 136 | #ifndef _UNICODE 137 | bool COutFileStream::Create(LPCWSTR fileName, bool createAlways) 138 | { 139 | return File.Create(fileName, createAlways); 140 | } 141 | #endif 142 | #endif 143 | 144 | STDMETHODIMP COutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize) 145 | { 146 | #ifdef _WIN32 147 | 148 | UInt32 realProcessedSize; 149 | bool result = File.WritePart(data, size, realProcessedSize); 150 | if(processedSize != NULL) 151 | *processedSize = realProcessedSize; 152 | return ConvertBoolToHRESULT(result); 153 | 154 | #else 155 | 156 | if(processedSize != NULL) 157 | *processedSize = 0; 158 | ssize_t res = File.Write(data, (size_t)size); 159 | if (res == -1) 160 | return E_FAIL; 161 | if(processedSize != NULL) 162 | *processedSize = (UInt32)res; 163 | return S_OK; 164 | 165 | #endif 166 | } 167 | 168 | STDMETHODIMP COutFileStream::Seek(Int64 offset, UInt32 seekOrigin, 169 | UInt64 *newPosition) 170 | { 171 | if(seekOrigin >= 3) 172 | return STG_E_INVALIDFUNCTION; 173 | #ifdef _WIN32 174 | 175 | UInt64 realNewPosition; 176 | bool result = File.Seek(offset, seekOrigin, realNewPosition); 177 | if(newPosition != NULL) 178 | *newPosition = realNewPosition; 179 | return ConvertBoolToHRESULT(result); 180 | 181 | #else 182 | 183 | off_t res = File.Seek(offset, seekOrigin); 184 | if (res == -1) 185 | return E_FAIL; 186 | if(newPosition != NULL) 187 | *newPosition = (UInt64)res; 188 | return S_OK; 189 | 190 | #endif 191 | } 192 | 193 | STDMETHODIMP COutFileStream::SetSize(Int64 newSize) 194 | { 195 | #ifdef _WIN32 196 | UInt64 currentPos; 197 | if(!File.Seek(0, FILE_CURRENT, currentPos)) 198 | return E_FAIL; 199 | bool result = File.SetLength(newSize); 200 | UInt64 currentPos2; 201 | result = result && File.Seek(currentPos, currentPos2); 202 | return result ? S_OK : E_FAIL; 203 | #else 204 | return E_FAIL; 205 | #endif 206 | } 207 | 208 | #ifndef _WIN32_WCE 209 | STDMETHODIMP CStdOutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize) 210 | { 211 | if(processedSize != NULL) 212 | *processedSize = 0; 213 | 214 | #ifdef _WIN32 215 | UInt32 realProcessedSize; 216 | BOOL res = TRUE; 217 | if (size > 0) 218 | { 219 | // Seems that Windows doesn't like big amounts writing to stdout. 220 | // So we limit portions by 32KB. 221 | UInt32 sizeTemp = (1 << 15); 222 | if (sizeTemp > size) 223 | sizeTemp = size; 224 | res = ::WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), 225 | data, sizeTemp, (DWORD *)&realProcessedSize, NULL); 226 | size -= realProcessedSize; 227 | data = (const void *)((const Byte *)data + realProcessedSize); 228 | if(processedSize != NULL) 229 | *processedSize += realProcessedSize; 230 | } 231 | return ConvertBoolToHRESULT(res != FALSE); 232 | 233 | #else 234 | 235 | ssize_t res; 236 | do 237 | { 238 | res = write(1, data, (size_t)size); 239 | } 240 | while (res < 0 && (errno == EINTR)); 241 | if (res == -1) 242 | return E_FAIL; 243 | if(processedSize != NULL) 244 | *processedSize = (UInt32)res; 245 | return S_OK; 246 | 247 | return S_OK; 248 | #endif 249 | } 250 | 251 | #endif 252 | -------------------------------------------------------------------------------- /LZMA/7zip/Common/FileStreams.h: -------------------------------------------------------------------------------- 1 | // FileStreams.h 2 | 3 | #ifndef __FILESTREAMS_H 4 | #define __FILESTREAMS_H 5 | 6 | #ifdef _WIN32 7 | #include "../../Windows/FileIO.h" 8 | #else 9 | #include "../../Common/C_FileIO.h" 10 | #endif 11 | 12 | #include "../IStream.h" 13 | #include "../../Common/MyCom.h" 14 | 15 | class CInFileStream: 16 | public IInStream, 17 | public IStreamGetSize, 18 | public CMyUnknownImp 19 | { 20 | public: 21 | #ifdef _WIN32 22 | NWindows::NFile::NIO::CInFile File; 23 | #else 24 | NC::NFile::NIO::CInFile File; 25 | #endif 26 | CInFileStream() {} 27 | virtual ~CInFileStream() {} 28 | 29 | bool Open(LPCTSTR fileName); 30 | #ifdef _WIN32 31 | #ifndef _UNICODE 32 | bool Open(LPCWSTR fileName); 33 | #endif 34 | #endif 35 | 36 | MY_UNKNOWN_IMP2(IInStream, IStreamGetSize) 37 | 38 | STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); 39 | STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition); 40 | 41 | STDMETHOD(GetSize)(UInt64 *size); 42 | }; 43 | 44 | #ifndef _WIN32_WCE 45 | class CStdInFileStream: 46 | public ISequentialInStream, 47 | public CMyUnknownImp 48 | { 49 | public: 50 | // HANDLE File; 51 | // CStdInFileStream() File(INVALID_HANDLE_VALUE): {} 52 | // void Open() { File = GetStdHandle(STD_INPUT_HANDLE); }; 53 | MY_UNKNOWN_IMP 54 | 55 | virtual ~CStdInFileStream() {} 56 | STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); 57 | }; 58 | #endif 59 | 60 | class COutFileStream: 61 | public IOutStream, 62 | public CMyUnknownImp 63 | { 64 | public: 65 | #ifdef _WIN32 66 | NWindows::NFile::NIO::COutFile File; 67 | #else 68 | NC::NFile::NIO::COutFile File; 69 | #endif 70 | virtual ~COutFileStream() {} 71 | bool Create(LPCTSTR fileName, bool createAlways); 72 | #ifdef _WIN32 73 | #ifndef _UNICODE 74 | bool Create(LPCWSTR fileName, bool createAlways); 75 | #endif 76 | #endif 77 | 78 | MY_UNKNOWN_IMP1(IOutStream) 79 | 80 | STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); 81 | STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition); 82 | STDMETHOD(SetSize)(Int64 newSize); 83 | }; 84 | 85 | #ifndef _WIN32_WCE 86 | class CStdOutFileStream: 87 | public ISequentialOutStream, 88 | public CMyUnknownImp 89 | { 90 | public: 91 | MY_UNKNOWN_IMP 92 | 93 | virtual ~CStdOutFileStream() {} 94 | STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); 95 | }; 96 | #endif 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /LZMA/7zip/Common/InBuffer.cpp: -------------------------------------------------------------------------------- 1 | // InBuffer.cpp 2 | 3 | #include "StdAfx.h" 4 | 5 | #include "InBuffer.h" 6 | 7 | #include "../../Common/Alloc.h" 8 | 9 | CInBuffer::CInBuffer(): 10 | _buffer(0), 11 | _bufferLimit(0), 12 | _bufferBase(0), 13 | _stream(0), 14 | _bufferSize(0) 15 | {} 16 | 17 | bool CInBuffer::Create(UInt32 bufferSize) 18 | { 19 | const UInt32 kMinBlockSize = 1; 20 | if (bufferSize < kMinBlockSize) 21 | bufferSize = kMinBlockSize; 22 | if (_bufferBase != 0 && _bufferSize == bufferSize) 23 | return true; 24 | Free(); 25 | _bufferSize = bufferSize; 26 | _bufferBase = (Byte *)::MidAlloc(bufferSize); 27 | return (_bufferBase != 0); 28 | } 29 | 30 | void CInBuffer::Free() 31 | { 32 | ::MidFree(_bufferBase); 33 | _bufferBase = 0; 34 | } 35 | 36 | void CInBuffer::SetStream(ISequentialInStream *stream) 37 | { 38 | _stream = stream; 39 | } 40 | 41 | void CInBuffer::Init() 42 | { 43 | _processedSize = 0; 44 | _buffer = _bufferBase; 45 | _bufferLimit = _buffer; 46 | _wasFinished = false; 47 | #ifdef _NO_EXCEPTIONS 48 | ErrorCode = S_OK; 49 | #endif 50 | } 51 | 52 | bool CInBuffer::ReadBlock() 53 | { 54 | #ifdef _NO_EXCEPTIONS 55 | if (ErrorCode != S_OK) 56 | return false; 57 | #endif 58 | if (_wasFinished) 59 | return false; 60 | _processedSize += (_buffer - _bufferBase); 61 | UInt32 numProcessedBytes; 62 | HRESULT result = _stream->Read(_bufferBase, _bufferSize, &numProcessedBytes); 63 | #ifdef _NO_EXCEPTIONS 64 | ErrorCode = result; 65 | #else 66 | if (result != S_OK) 67 | throw CInBufferException(result); 68 | #endif 69 | _buffer = _bufferBase; 70 | _bufferLimit = _buffer + numProcessedBytes; 71 | _wasFinished = (numProcessedBytes == 0); 72 | return (!_wasFinished); 73 | } 74 | 75 | Byte CInBuffer::ReadBlock2() 76 | { 77 | if(!ReadBlock()) 78 | return 0xFF; 79 | return *_buffer++; 80 | } 81 | -------------------------------------------------------------------------------- /LZMA/7zip/Common/InBuffer.h: -------------------------------------------------------------------------------- 1 | // InBuffer.h 2 | 3 | #ifndef __INBUFFER_H 4 | #define __INBUFFER_H 5 | 6 | #include "../IStream.h" 7 | #include "../../Common/MyCom.h" 8 | 9 | #ifndef _NO_EXCEPTIONS 10 | class CInBufferException 11 | { 12 | public: 13 | HRESULT ErrorCode; 14 | CInBufferException(HRESULT errorCode): ErrorCode(errorCode) {} 15 | }; 16 | #endif 17 | 18 | class CInBuffer 19 | { 20 | Byte *_buffer; 21 | Byte *_bufferLimit; 22 | Byte *_bufferBase; 23 | CMyComPtr _stream; 24 | UInt64 _processedSize; 25 | UInt32 _bufferSize; 26 | bool _wasFinished; 27 | 28 | bool ReadBlock(); 29 | Byte ReadBlock2(); 30 | 31 | public: 32 | #ifdef _NO_EXCEPTIONS 33 | HRESULT ErrorCode; 34 | #endif 35 | 36 | CInBuffer(); 37 | ~CInBuffer() { Free(); } 38 | 39 | bool Create(UInt32 bufferSize); 40 | void Free(); 41 | 42 | void SetStream(ISequentialInStream *stream); 43 | void Init(); 44 | void ReleaseStream() { _stream.Release(); } 45 | 46 | bool ReadByte(Byte &b) 47 | { 48 | if(_buffer >= _bufferLimit) 49 | if(!ReadBlock()) 50 | return false; 51 | b = *_buffer++; 52 | return true; 53 | } 54 | Byte ReadByte() 55 | { 56 | if(_buffer >= _bufferLimit) 57 | return ReadBlock2(); 58 | return *_buffer++; 59 | } 60 | void ReadBytes(void *data, UInt32 size, UInt32 &processedSize) 61 | { 62 | for(processedSize = 0; processedSize < size; processedSize++) 63 | if (!ReadByte(((Byte *)data)[processedSize])) 64 | return; 65 | } 66 | bool ReadBytes(void *data, UInt32 size) 67 | { 68 | UInt32 processedSize; 69 | ReadBytes(data, size, processedSize); 70 | return (processedSize == size); 71 | } 72 | UInt64 GetProcessedSize() const { return _processedSize + (_buffer - _bufferBase); } 73 | bool WasFinished() const { return _wasFinished; } 74 | }; 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /LZMA/7zip/Common/OutBuffer.cpp: -------------------------------------------------------------------------------- 1 | // OutByte.cpp 2 | 3 | #include "StdAfx.h" 4 | 5 | #include "OutBuffer.h" 6 | 7 | #include "../../Common/Alloc.h" 8 | 9 | bool COutBuffer::Create(UInt32 bufferSize) 10 | { 11 | const UInt32 kMinBlockSize = 1; 12 | if (bufferSize < kMinBlockSize) 13 | bufferSize = kMinBlockSize; 14 | if (_buffer != 0 && _bufferSize == bufferSize) 15 | return true; 16 | Free(); 17 | _bufferSize = bufferSize; 18 | _buffer = (Byte *)::MidAlloc(bufferSize); 19 | return (_buffer != 0); 20 | } 21 | 22 | void COutBuffer::Free() 23 | { 24 | ::MidFree(_buffer); 25 | _buffer = 0; 26 | } 27 | 28 | void COutBuffer::SetStream(ISequentialOutStream *stream) 29 | { 30 | _stream = stream; 31 | } 32 | 33 | void COutBuffer::Init() 34 | { 35 | _streamPos = 0; 36 | _limitPos = _bufferSize; 37 | _pos = 0; 38 | _processedSize = 0; 39 | _overDict = false; 40 | #ifdef _NO_EXCEPTIONS 41 | ErrorCode = S_OK; 42 | #endif 43 | } 44 | 45 | UInt64 COutBuffer::GetProcessedSize() const 46 | { 47 | UInt64 res = _processedSize + _pos - _streamPos; 48 | if (_streamPos > _pos) 49 | res += _bufferSize; 50 | return res; 51 | } 52 | 53 | 54 | HRESULT COutBuffer::FlushPart() 55 | { 56 | // _streamPos < _bufferSize 57 | UInt32 size = (_streamPos >= _pos) ? (_bufferSize - _streamPos) : (_pos - _streamPos); 58 | HRESULT result = S_OK; 59 | #ifdef _NO_EXCEPTIONS 60 | if (ErrorCode != S_OK) 61 | result = ErrorCode; 62 | #endif 63 | if (_buffer2 != 0) 64 | { 65 | memmove(_buffer2, _buffer + _streamPos, size); 66 | _buffer2 += size; 67 | } 68 | 69 | if (_stream != 0 70 | #ifdef _NO_EXCEPTIONS 71 | && (ErrorCode != S_OK) 72 | #endif 73 | ) 74 | { 75 | UInt32 processedSize = 0; 76 | result = _stream->Write(_buffer + _streamPos, size, &processedSize); 77 | size = processedSize; 78 | } 79 | _streamPos += size; 80 | if (_streamPos == _bufferSize) 81 | _streamPos = 0; 82 | if (_pos == _bufferSize) 83 | { 84 | _overDict = true; 85 | _pos = 0; 86 | } 87 | _limitPos = (_streamPos > _pos) ? _streamPos : _bufferSize; 88 | _processedSize += size; 89 | return result; 90 | } 91 | 92 | HRESULT COutBuffer::Flush() 93 | { 94 | #ifdef _NO_EXCEPTIONS 95 | if (ErrorCode != S_OK) 96 | return ErrorCode; 97 | #endif 98 | 99 | while(_streamPos != _pos) 100 | { 101 | HRESULT result = FlushPart(); 102 | if (result != S_OK) 103 | return result; 104 | } 105 | return S_OK; 106 | } 107 | 108 | void COutBuffer::FlushWithCheck() 109 | { 110 | HRESULT result = FlushPart(); 111 | #ifdef _NO_EXCEPTIONS 112 | ErrorCode = result; 113 | #else 114 | if (result != S_OK) 115 | throw COutBufferException(result); 116 | #endif 117 | } 118 | -------------------------------------------------------------------------------- /LZMA/7zip/Common/OutBuffer.h: -------------------------------------------------------------------------------- 1 | // OutBuffer.h 2 | 3 | #ifndef __OUTBUFFER_H 4 | #define __OUTBUFFER_H 5 | 6 | #include "../IStream.h" 7 | #include "../../Common/MyCom.h" 8 | 9 | #ifndef _NO_EXCEPTIONS 10 | struct COutBufferException 11 | { 12 | HRESULT ErrorCode; 13 | COutBufferException(HRESULT errorCode): ErrorCode(errorCode) {} 14 | }; 15 | #endif 16 | 17 | class COutBuffer 18 | { 19 | protected: 20 | Byte *_buffer; 21 | UInt32 _pos; 22 | UInt32 _limitPos; 23 | UInt32 _streamPos; 24 | UInt32 _bufferSize; 25 | CMyComPtr _stream; 26 | UInt64 _processedSize; 27 | Byte *_buffer2; 28 | bool _overDict; 29 | 30 | HRESULT FlushPart(); 31 | void FlushWithCheck(); 32 | public: 33 | #ifdef _NO_EXCEPTIONS 34 | HRESULT ErrorCode; 35 | #endif 36 | 37 | COutBuffer(): _buffer(0), _pos(0), _stream(0), _buffer2(0) {} 38 | ~COutBuffer() { Free(); } 39 | 40 | bool Create(UInt32 bufferSize); 41 | void Free(); 42 | 43 | void SetMemStream(Byte *buffer) { _buffer2 = buffer; } 44 | void SetStream(ISequentialOutStream *stream); 45 | void Init(); 46 | HRESULT Flush(); 47 | void ReleaseStream() { _stream.Release(); } 48 | 49 | void WriteByte(Byte b) 50 | { 51 | _buffer[_pos++] = b; 52 | if(_pos == _limitPos) 53 | FlushWithCheck(); 54 | } 55 | void WriteBytes(const void *data, size_t size) 56 | { 57 | for (size_t i = 0; i < size; i++) 58 | WriteByte(((const Byte *)data)[i]); 59 | } 60 | 61 | UInt64 GetProcessedSize() const; 62 | }; 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /LZMA/7zip/Common/StdAfx.h: -------------------------------------------------------------------------------- 1 | // StdAfx.h 2 | 3 | #ifndef __STDAFX_H 4 | #define __STDAFX_H 5 | 6 | #include "../../Common/MyWindows.h" 7 | #include "../../Common/NewHandler.h" 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /LZMA/7zip/Common/StreamUtils.cpp: -------------------------------------------------------------------------------- 1 | // StreamUtils.cpp 2 | 3 | #include "StdAfx.h" 4 | 5 | #include "../../Common/MyCom.h" 6 | #include "StreamUtils.h" 7 | 8 | HRESULT ReadStream(ISequentialInStream *stream, void *data, UInt32 size, UInt32 *processedSize) 9 | { 10 | if (processedSize != 0) 11 | *processedSize = 0; 12 | while(size != 0) 13 | { 14 | UInt32 processedSizeLoc; 15 | HRESULT res = stream->Read(data, size, &processedSizeLoc); 16 | if (processedSize != 0) 17 | *processedSize += processedSizeLoc; 18 | data = (Byte *)((Byte *)data + processedSizeLoc); 19 | size -= processedSizeLoc; 20 | RINOK(res); 21 | if (processedSizeLoc == 0) 22 | return S_OK; 23 | } 24 | return S_OK; 25 | } 26 | 27 | HRESULT WriteStream(ISequentialOutStream *stream, const void *data, UInt32 size, UInt32 *processedSize) 28 | { 29 | if (processedSize != 0) 30 | *processedSize = 0; 31 | while(size != 0) 32 | { 33 | UInt32 processedSizeLoc; 34 | HRESULT res = stream->Write(data, size, &processedSizeLoc); 35 | if (processedSize != 0) 36 | *processedSize += processedSizeLoc; 37 | data = (const void *)((const Byte *)data + processedSizeLoc); 38 | size -= processedSizeLoc; 39 | RINOK(res); 40 | if (processedSizeLoc == 0) 41 | break; 42 | } 43 | return S_OK; 44 | } 45 | -------------------------------------------------------------------------------- /LZMA/7zip/Common/StreamUtils.h: -------------------------------------------------------------------------------- 1 | // StreamUtils.h 2 | 3 | #ifndef __STREAMUTILS_H 4 | #define __STREAMUTILS_H 5 | 6 | #include "../IStream.h" 7 | 8 | HRESULT ReadStream(ISequentialInStream *stream, void *data, UInt32 size, UInt32 *processedSize); 9 | HRESULT WriteStream(ISequentialOutStream *stream, const void *data, UInt32 size, UInt32 *processedSize); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/BinTree/BinTree.h: -------------------------------------------------------------------------------- 1 | // BinTree.h 2 | 3 | #include "../LZInWindow.h" 4 | #include "../IMatchFinder.h" 5 | 6 | namespace BT_NAMESPACE { 7 | 8 | typedef UInt32 CIndex; 9 | const UInt32 kMaxValForNormalize = (UInt32(1) << 31) - 1; 10 | 11 | class CMatchFinder: 12 | public IMatchFinder, 13 | public CLZInWindow, 14 | public CMyUnknownImp, 15 | public IMatchFinderSetNumPasses 16 | { 17 | UInt32 _cyclicBufferPos; 18 | UInt32 _cyclicBufferSize; // it must be historySize + 1 19 | UInt32 _matchMaxLen; 20 | CIndex *_hash; 21 | CIndex *_son; 22 | UInt32 _hashMask; 23 | UInt32 _cutValue; 24 | UInt32 _hashSizeSum; 25 | 26 | void Normalize(); 27 | void FreeThisClassMemory(); 28 | void FreeMemory(); 29 | 30 | MY_UNKNOWN_IMP 31 | 32 | STDMETHOD(SetStream)(ISequentialInStream *inStream); 33 | STDMETHOD_(void, ReleaseStream)(); 34 | STDMETHOD(Init)(); 35 | HRESULT MovePos(); 36 | STDMETHOD_(Byte, GetIndexByte)(Int32 index); 37 | STDMETHOD_(UInt32, GetMatchLen)(Int32 index, UInt32 back, UInt32 limit); 38 | STDMETHOD_(UInt32, GetNumAvailableBytes)(); 39 | STDMETHOD_(const Byte *, GetPointerToCurrentPos)(); 40 | STDMETHOD_(Int32, NeedChangeBufferPos)(UInt32 numCheckBytes); 41 | STDMETHOD_(void, ChangeBufferPos)(); 42 | 43 | STDMETHOD(Create)(UInt32 historySize, UInt32 keepAddBufferBefore, 44 | UInt32 matchMaxLen, UInt32 keepAddBufferAfter); 45 | STDMETHOD(GetMatches)(UInt32 *distances); 46 | STDMETHOD(Skip)(UInt32 num); 47 | 48 | public: 49 | CMatchFinder(); 50 | virtual ~CMatchFinder(); 51 | virtual void SetNumPasses(UInt32 numPasses) { _cutValue = numPasses; } 52 | }; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/BinTree/BinTree2.h: -------------------------------------------------------------------------------- 1 | // BinTree2.h 2 | 3 | #ifndef __BINTREE2_H 4 | #define __BINTREE2_H 5 | 6 | #define BT_NAMESPACE NBT2 7 | 8 | #include "BinTreeMain.h" 9 | 10 | #undef BT_NAMESPACE 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/BinTree/BinTree3.h: -------------------------------------------------------------------------------- 1 | // BinTree3.h 2 | 3 | #ifndef __BINTREE3_H 4 | #define __BINTREE3_H 5 | 6 | #define BT_NAMESPACE NBT3 7 | 8 | #define HASH_ARRAY_2 9 | 10 | #include "BinTreeMain.h" 11 | 12 | #undef HASH_ARRAY_2 13 | 14 | #undef BT_NAMESPACE 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/BinTree/BinTree3Z.h: -------------------------------------------------------------------------------- 1 | // BinTree3Z.h 2 | 3 | #ifndef __BINTREE3Z_H 4 | #define __BINTREE3Z_H 5 | 6 | #define BT_NAMESPACE NBT3Z 7 | 8 | #define HASH_ZIP 9 | 10 | #include "BinTreeMain.h" 11 | 12 | #undef HASH_ZIP 13 | 14 | #undef BT_NAMESPACE 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/BinTree/BinTree4.h: -------------------------------------------------------------------------------- 1 | // BinTree4.h 2 | 3 | #ifndef __BINTREE4_H 4 | #define __BINTREE4_H 5 | 6 | #define BT_NAMESPACE NBT4 7 | 8 | #define HASH_ARRAY_2 9 | #define HASH_ARRAY_3 10 | 11 | #include "BinTreeMain.h" 12 | 13 | #undef HASH_ARRAY_2 14 | #undef HASH_ARRAY_3 15 | 16 | #undef BT_NAMESPACE 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/HashChain/HC2.h: -------------------------------------------------------------------------------- 1 | // HC2.h 2 | 3 | #ifndef __HC2_H 4 | #define __HC2_H 5 | 6 | #define BT_NAMESPACE NHC2 7 | 8 | #include "HCMain.h" 9 | 10 | #undef BT_NAMESPACE 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/HashChain/HC3.h: -------------------------------------------------------------------------------- 1 | // HC3.h 2 | 3 | #ifndef __HC3_H 4 | #define __HC3_H 5 | 6 | #define BT_NAMESPACE NHC3 7 | 8 | #define HASH_ARRAY_2 9 | 10 | #include "HCMain.h" 11 | 12 | #undef HASH_ARRAY_2 13 | #undef BT_NAMESPACE 14 | 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/HashChain/HC4.h: -------------------------------------------------------------------------------- 1 | // HC4.h 2 | 3 | #ifndef __HC4_H 4 | #define __HC4_H 5 | 6 | #define BT_NAMESPACE NHC4 7 | 8 | #define HASH_ARRAY_2 9 | #define HASH_ARRAY_3 10 | 11 | #include "HCMain.h" 12 | 13 | #undef HASH_ARRAY_2 14 | #undef HASH_ARRAY_3 15 | 16 | #undef BT_NAMESPACE 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/HashChain/HCMain.h: -------------------------------------------------------------------------------- 1 | // HCMain.h 2 | 3 | #define _HASH_CHAIN 4 | #include "../BinTree/BinTreeMain.h" 5 | #undef _HASH_CHAIN 6 | 7 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/IMatchFinder.h: -------------------------------------------------------------------------------- 1 | // MatchFinders/IMatchFinder.h 2 | 3 | #ifndef __IMATCHFINDER_H 4 | #define __IMATCHFINDER_H 5 | 6 | struct IInWindowStream: public IUnknown 7 | { 8 | STDMETHOD(SetStream)(ISequentialInStream *inStream) PURE; 9 | STDMETHOD_(void, ReleaseStream)() PURE; 10 | STDMETHOD(Init)() PURE; 11 | STDMETHOD_(Byte, GetIndexByte)(Int32 index) PURE; 12 | STDMETHOD_(UInt32, GetMatchLen)(Int32 index, UInt32 distance, UInt32 limit) PURE; 13 | STDMETHOD_(UInt32, GetNumAvailableBytes)() PURE; 14 | STDMETHOD_(const Byte *, GetPointerToCurrentPos)() PURE; 15 | STDMETHOD_(Int32, NeedChangeBufferPos)(UInt32 numCheckBytes) PURE; 16 | STDMETHOD_(void, ChangeBufferPos)() PURE; 17 | }; 18 | 19 | struct IMatchFinder: public IInWindowStream 20 | { 21 | STDMETHOD(Create)(UInt32 historySize, UInt32 keepAddBufferBefore, 22 | UInt32 matchMaxLen, UInt32 keepAddBufferAfter) PURE; 23 | STDMETHOD(GetMatches)(UInt32 *distances) PURE; 24 | STDMETHOD(Skip)(UInt32 num) PURE; 25 | }; 26 | 27 | struct IMatchFinderSetNumPasses 28 | { 29 | virtual void SetNumPasses(UInt32 numPasses) PURE; 30 | }; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/LZInWindow.cpp: -------------------------------------------------------------------------------- 1 | // LZInWindow.cpp 2 | 3 | #include "StdAfx.h" 4 | 5 | #include "LZInWindow.h" 6 | #include "../../../Common/MyCom.h" 7 | #include "../../../Common/Alloc.h" 8 | 9 | void CLZInWindow::Free() 10 | { 11 | ::BigFree(_bufferBase); 12 | _bufferBase = 0; 13 | } 14 | 15 | bool CLZInWindow::Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv) 16 | { 17 | _keepSizeBefore = keepSizeBefore; 18 | _keepSizeAfter = keepSizeAfter; 19 | UInt32 blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv; 20 | if (_bufferBase == 0 || _blockSize != blockSize) 21 | { 22 | Free(); 23 | _blockSize = blockSize; 24 | if (_blockSize != 0) 25 | _bufferBase = (Byte *)::BigAlloc(_blockSize); 26 | } 27 | _pointerToLastSafePosition = _bufferBase + _blockSize - keepSizeAfter; 28 | if (_blockSize == 0) 29 | return true; 30 | return (_bufferBase != 0); 31 | } 32 | 33 | void CLZInWindow::SetStream(ISequentialInStream *stream) 34 | { 35 | _stream = stream; 36 | } 37 | 38 | HRESULT CLZInWindow::Init() 39 | { 40 | _buffer = _bufferBase; 41 | _pos = 0; 42 | _streamPos = 0; 43 | _streamEndWasReached = false; 44 | return ReadBlock(); 45 | } 46 | 47 | /* 48 | void CLZInWindow::ReleaseStream() 49 | { 50 | _stream.Release(); 51 | } 52 | */ 53 | 54 | /////////////////////////////////////////// 55 | // ReadBlock 56 | 57 | // In State: 58 | // (_buffer + _streamPos) <= (_bufferBase + _blockSize) 59 | // Out State: 60 | // _posLimit <= _blockSize - _keepSizeAfter; 61 | // if(_streamEndWasReached == false): 62 | // _streamPos >= _pos + _keepSizeAfter 63 | // _posLimit = _streamPos - _keepSizeAfter; 64 | // else 65 | // 66 | 67 | HRESULT CLZInWindow::ReadBlock() 68 | { 69 | if(_streamEndWasReached) 70 | return S_OK; 71 | while(true) 72 | { 73 | UInt32 size = (UInt32)(_bufferBase - _buffer) + _blockSize - _streamPos; 74 | if(size == 0) 75 | return S_OK; 76 | UInt32 numReadBytes; 77 | RINOK(_stream->Read(_buffer + _streamPos, size, &numReadBytes)); 78 | if(numReadBytes == 0) 79 | { 80 | _posLimit = _streamPos; 81 | const Byte *pointerToPostion = _buffer + _posLimit; 82 | if(pointerToPostion > _pointerToLastSafePosition) 83 | _posLimit = (UInt32)(_pointerToLastSafePosition - _buffer); 84 | _streamEndWasReached = true; 85 | return S_OK; 86 | } 87 | _streamPos += numReadBytes; 88 | if(_streamPos >= _pos + _keepSizeAfter) 89 | { 90 | _posLimit = _streamPos - _keepSizeAfter; 91 | return S_OK; 92 | } 93 | } 94 | } 95 | 96 | void CLZInWindow::MoveBlock() 97 | { 98 | UInt32 offset = (UInt32)(_buffer - _bufferBase) + _pos - _keepSizeBefore; 99 | // we need one additional byte, since MovePos moves on 1 byte. 100 | if (offset > 0) 101 | offset--; 102 | UInt32 numBytes = (UInt32)(_buffer - _bufferBase) + _streamPos - offset; 103 | memmove(_bufferBase, _bufferBase + offset, numBytes); 104 | _buffer -= offset; 105 | } 106 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/LZInWindow.h: -------------------------------------------------------------------------------- 1 | // LZInWindow.h 2 | 3 | #ifndef __LZ_IN_WINDOW_H 4 | #define __LZ_IN_WINDOW_H 5 | 6 | #include "../../IStream.h" 7 | 8 | class CLZInWindow 9 | { 10 | Byte *_bufferBase; // pointer to buffer with data 11 | ISequentialInStream *_stream; 12 | UInt32 _posLimit; // offset (from _buffer) when new block reading must be done 13 | bool _streamEndWasReached; // if (true) then _streamPos shows real end of stream 14 | const Byte *_pointerToLastSafePosition; 15 | protected: 16 | Byte *_buffer; // Pointer to virtual Buffer begin 17 | UInt32 _blockSize; // Size of Allocated memory block 18 | UInt32 _pos; // offset (from _buffer) of curent byte 19 | UInt32 _keepSizeBefore; // how many BYTEs must be kept in buffer before _pos 20 | UInt32 _keepSizeAfter; // how many BYTEs must be kept buffer after _pos 21 | UInt32 _streamPos; // offset (from _buffer) of first not read byte from Stream 22 | 23 | void MoveBlock(); 24 | HRESULT ReadBlock(); 25 | void Free(); 26 | public: 27 | CLZInWindow(): _bufferBase(0) {} 28 | virtual ~CLZInWindow() { Free(); } 29 | 30 | // keepSizeBefore + keepSizeAfter + keepSizeReserv < 4G) 31 | bool Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv = (1<<17)); 32 | 33 | void SetStream(ISequentialInStream *stream); 34 | HRESULT Init(); 35 | // void ReleaseStream(); 36 | 37 | Byte *GetBuffer() const { return _buffer; } 38 | 39 | const Byte *GetPointerToCurrentPos() const { return _buffer + _pos; } 40 | 41 | HRESULT MovePos() 42 | { 43 | _pos++; 44 | if (_pos > _posLimit) 45 | { 46 | const Byte *pointerToPostion = _buffer + _pos; 47 | if(pointerToPostion > _pointerToLastSafePosition) 48 | MoveBlock(); 49 | return ReadBlock(); 50 | } 51 | else 52 | return S_OK; 53 | } 54 | Byte GetIndexByte(Int32 index) const { return _buffer[(size_t)_pos + index]; } 55 | 56 | // index + limit have not to exceed _keepSizeAfter; 57 | // -2G <= index < 2G 58 | UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit) const 59 | { 60 | if(_streamEndWasReached) 61 | if ((_pos + index) + limit > _streamPos) 62 | limit = _streamPos - (_pos + index); 63 | distance++; 64 | const Byte *pby = _buffer + (size_t)_pos + index; 65 | UInt32 i; 66 | for(i = 0; i < limit && pby[i] == pby[(size_t)i - distance]; i++); 67 | return i; 68 | } 69 | 70 | UInt32 GetNumAvailableBytes() const { return _streamPos - _pos; } 71 | 72 | void ReduceOffsets(Int32 subValue) 73 | { 74 | _buffer += subValue; 75 | _posLimit -= subValue; 76 | _pos -= subValue; 77 | _streamPos -= subValue; 78 | } 79 | 80 | bool NeedMove(UInt32 numCheckBytes) 81 | { 82 | UInt32 reserv = _pointerToLastSafePosition - (_buffer + _pos); 83 | return (reserv <= numCheckBytes); 84 | } 85 | }; 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/LZOutWindow.cpp: -------------------------------------------------------------------------------- 1 | // LZOutWindow.cpp 2 | 3 | #include "StdAfx.h" 4 | 5 | #include "../../../Common/Alloc.h" 6 | #include "LZOutWindow.h" 7 | 8 | void CLZOutWindow::Init(bool solid) 9 | { 10 | if(!solid) 11 | COutBuffer::Init(); 12 | #ifdef _NO_EXCEPTIONS 13 | ErrorCode = S_OK; 14 | #endif 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/LZOutWindow.h: -------------------------------------------------------------------------------- 1 | // LZOutWindow.h 2 | 3 | #ifndef __LZ_OUT_WINDOW_H 4 | #define __LZ_OUT_WINDOW_H 5 | 6 | #include "../../IStream.h" 7 | #include "../../Common/OutBuffer.h" 8 | 9 | /* 10 | #ifndef _NO_EXCEPTIONS 11 | class CLZOutWindowException 12 | { 13 | public: 14 | HRESULT ErrorCode; 15 | CLZOutWindowException(HRESULT errorCode): ErrorCode(errorCode) {} 16 | }; 17 | #endif 18 | */ 19 | typedef COutBufferException CLZOutWindowException; 20 | 21 | class CLZOutWindow: public COutBuffer 22 | { 23 | public: 24 | void Init(bool solid = false); 25 | 26 | // distance >= 0, len > 0, 27 | bool CopyBlock(UInt32 distance, UInt32 len) 28 | { 29 | UInt32 pos = _pos - distance - 1; 30 | if (distance >= _pos) 31 | { 32 | if (!_overDict || distance >= _bufferSize) 33 | return false; 34 | pos += _bufferSize; 35 | } 36 | do 37 | { 38 | if (pos == _bufferSize) 39 | pos = 0; 40 | _buffer[_pos++] = _buffer[pos++]; 41 | if (_pos == _limitPos) 42 | FlushWithCheck(); 43 | } 44 | while(--len != 0); 45 | return true; 46 | } 47 | 48 | void PutByte(Byte b) 49 | { 50 | _buffer[_pos++] = b; 51 | if (_pos == _limitPos) 52 | FlushWithCheck(); 53 | } 54 | 55 | Byte GetByte(UInt32 distance) const 56 | { 57 | UInt32 pos = _pos - distance - 1; 58 | if (pos >= _bufferSize) 59 | pos += _bufferSize; 60 | return _buffer[pos]; 61 | } 62 | }; 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/Patricia/Pat2.h: -------------------------------------------------------------------------------- 1 | // Pat2.h 2 | 3 | #ifndef __PAT2__H 4 | #define __PAT2__H 5 | 6 | #undef PAT_CLSID 7 | #define PAT_CLSID CLSID_CMatchFinderPat2 8 | 9 | #undef PAT_NAMESPACE 10 | #define PAT_NAMESPACE NPat2 11 | 12 | #define __AUTO_REMOVE 13 | #define __NODE_2_BITS 14 | 15 | #include "Pat.h" 16 | #include "PatMain.h" 17 | 18 | #undef __AUTO_REMOVE 19 | #undef __NODE_2_BITS 20 | 21 | #endif 22 | 23 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/Patricia/Pat2H.h: -------------------------------------------------------------------------------- 1 | // Pat2H.h 2 | 3 | #ifndef __PAT2H__H 4 | #define __PAT2H__H 5 | 6 | #undef PAT_CLSID 7 | #define PAT_CLSID CLSID_CMatchFinderPat2H 8 | 9 | #undef PAT_NAMESPACE 10 | #define PAT_NAMESPACE NPat2H 11 | 12 | #define __AUTO_REMOVE 13 | #define __NODE_2_BITS 14 | #define __HASH_3 15 | 16 | #include "Pat.h" 17 | #include "PatMain.h" 18 | 19 | #undef __AUTO_REMOVE 20 | #undef __NODE_2_BITS 21 | #undef __HASH_3 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/Patricia/Pat2R.h: -------------------------------------------------------------------------------- 1 | // Pat2R.h 2 | 3 | #ifndef __PAT2R__H 4 | #define __PAT2R__H 5 | 6 | #undef PAT_CLSID 7 | #define PAT_CLSID CLSID_CMatchFinderPat2R 8 | 9 | #undef PAT_NAMESPACE 10 | #define PAT_NAMESPACE NPat2R 11 | 12 | #define __NODE_2_BITS 13 | 14 | #include "Pat.h" 15 | #include "PatMain.h" 16 | 17 | #undef __NODE_2_BITS 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/Patricia/Pat3H.h: -------------------------------------------------------------------------------- 1 | // Pat3H.h 2 | 3 | #ifndef __PAT3H__H 4 | #define __PAT3H__H 5 | 6 | #undef PAT_CLSID 7 | #define PAT_CLSID CLSID_CMatchFinderPat3H 8 | 9 | #undef PAT_NAMESPACE 10 | #define PAT_NAMESPACE NPat3H 11 | 12 | #define __AUTO_REMOVE 13 | #define __NODE_3_BITS 14 | #define __HASH_3 15 | 16 | #include "Pat.h" 17 | #include "PatMain.h" 18 | 19 | #undef __AUTO_REMOVE 20 | #undef __NODE_3_BITS 21 | #undef __HASH_3 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/Patricia/Pat4H.h: -------------------------------------------------------------------------------- 1 | // Pat4H.h 2 | 3 | #ifndef __PAT4H__H 4 | #define __PAT4H__H 5 | 6 | #undef PAT_CLSID 7 | #define PAT_CLSID CLSID_CMatchFinderPat4H 8 | 9 | #undef PAT_NAMESPACE 10 | #define PAT_NAMESPACE NPat4H 11 | 12 | #define __AUTO_REMOVE 13 | #define __NODE_4_BITS 14 | #define __HASH_3 15 | 16 | #include "Pat.h" 17 | #include "PatMain.h" 18 | 19 | #undef __AUTO_REMOVE 20 | #undef __NODE_4_BITS 21 | #undef __HASH_3 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/LZ/StdAfx.h: -------------------------------------------------------------------------------- 1 | // StdAfx.h 2 | 3 | #ifndef __STDAFX_H 4 | #define __STDAFX_H 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/RangeCoder/RangeCoder.h: -------------------------------------------------------------------------------- 1 | // Compress/RangeCoder/RangeCoder.h 2 | 3 | #ifndef __COMPRESS_RANGECODER_H 4 | #define __COMPRESS_RANGECODER_H 5 | 6 | #include "../../Common/InBuffer.h" 7 | #include "../../Common/OutBuffer.h" 8 | 9 | namespace NCompress { 10 | namespace NRangeCoder { 11 | 12 | const int kNumTopBits = 24; 13 | const UInt32 kTopValue = (1 << kNumTopBits); 14 | 15 | class CEncoder 16 | { 17 | UInt32 _cacheSize; 18 | Byte _cache; 19 | public: 20 | UInt64 Low; 21 | UInt32 Range; 22 | COutBuffer Stream; 23 | bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); } 24 | 25 | void SetStream(ISequentialOutStream *stream) { Stream.SetStream(stream); } 26 | void Init() 27 | { 28 | Stream.Init(); 29 | Low = 0; 30 | Range = 0xFFFFFFFF; 31 | _cacheSize = 1; 32 | _cache = 0; 33 | } 34 | 35 | void FlushData() 36 | { 37 | // Low += 1; 38 | for(int i = 0; i < 5; i++) 39 | ShiftLow(); 40 | } 41 | 42 | HRESULT FlushStream() { return Stream.Flush(); } 43 | 44 | void ReleaseStream() { Stream.ReleaseStream(); } 45 | 46 | void Encode(UInt32 start, UInt32 size, UInt32 total) 47 | { 48 | Low += start * (Range /= total); 49 | Range *= size; 50 | while (Range < kTopValue) 51 | { 52 | Range <<= 8; 53 | ShiftLow(); 54 | } 55 | } 56 | 57 | void ShiftLow() 58 | { 59 | if ((UInt32)Low < (UInt32)0xFF000000 || (int)(Low >> 32) != 0) 60 | { 61 | Byte temp = _cache; 62 | do 63 | { 64 | Stream.WriteByte((Byte)(temp + (Byte)(Low >> 32))); 65 | temp = 0xFF; 66 | } 67 | while(--_cacheSize != 0); 68 | _cache = (Byte)((UInt32)Low >> 24); 69 | } 70 | _cacheSize++; 71 | Low = (UInt32)Low << 8; 72 | } 73 | 74 | void EncodeDirectBits(UInt32 value, int numTotalBits) 75 | { 76 | for (int i = numTotalBits - 1; i >= 0; i--) 77 | { 78 | Range >>= 1; 79 | if (((value >> i) & 1) == 1) 80 | Low += Range; 81 | if (Range < kTopValue) 82 | { 83 | Range <<= 8; 84 | ShiftLow(); 85 | } 86 | } 87 | } 88 | 89 | void EncodeBit(UInt32 size0, UInt32 numTotalBits, UInt32 symbol) 90 | { 91 | UInt32 newBound = (Range >> numTotalBits) * size0; 92 | if (symbol == 0) 93 | Range = newBound; 94 | else 95 | { 96 | Low += newBound; 97 | Range -= newBound; 98 | } 99 | while (Range < kTopValue) 100 | { 101 | Range <<= 8; 102 | ShiftLow(); 103 | } 104 | } 105 | 106 | UInt64 GetProcessedSize() { return Stream.GetProcessedSize() + _cacheSize + 4; } 107 | }; 108 | 109 | class CDecoder 110 | { 111 | public: 112 | CInBuffer Stream; 113 | UInt32 Range; 114 | UInt32 Code; 115 | bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); } 116 | 117 | void Normalize() 118 | { 119 | while (Range < kTopValue) 120 | { 121 | Code = (Code << 8) | Stream.ReadByte(); 122 | Range <<= 8; 123 | } 124 | } 125 | 126 | void SetStream(ISequentialInStream *stream) { Stream.SetStream(stream); } 127 | void Init() 128 | { 129 | Stream.Init(); 130 | Code = 0; 131 | Range = 0xFFFFFFFF; 132 | for(int i = 0; i < 5; i++) 133 | Code = (Code << 8) | Stream.ReadByte(); 134 | } 135 | 136 | void ReleaseStream() { Stream.ReleaseStream(); } 137 | 138 | UInt32 GetThreshold(UInt32 total) 139 | { 140 | return (Code) / ( Range /= total); 141 | } 142 | 143 | void Decode(UInt32 start, UInt32 size) 144 | { 145 | Code -= start * Range; 146 | Range *= size; 147 | Normalize(); 148 | } 149 | 150 | UInt32 DecodeDirectBits(int numTotalBits) 151 | { 152 | UInt32 range = Range; 153 | UInt32 code = Code; 154 | UInt32 result = 0; 155 | for (int i = numTotalBits; i != 0; i--) 156 | { 157 | range >>= 1; 158 | /* 159 | result <<= 1; 160 | if (code >= range) 161 | { 162 | code -= range; 163 | result |= 1; 164 | } 165 | */ 166 | UInt32 t = (code - range) >> 31; 167 | code -= range & (t - 1); 168 | result = (result << 1) | (1 - t); 169 | 170 | if (range < kTopValue) 171 | { 172 | code = (code << 8) | Stream.ReadByte(); 173 | range <<= 8; 174 | } 175 | } 176 | Range = range; 177 | Code = code; 178 | return result; 179 | } 180 | 181 | UInt32 DecodeBit(UInt32 size0, UInt32 numTotalBits) 182 | { 183 | UInt32 newBound = (Range >> numTotalBits) * size0; 184 | UInt32 symbol; 185 | if (Code < newBound) 186 | { 187 | symbol = 0; 188 | Range = newBound; 189 | } 190 | else 191 | { 192 | symbol = 1; 193 | Code -= newBound; 194 | Range -= newBound; 195 | } 196 | Normalize(); 197 | return symbol; 198 | } 199 | 200 | UInt64 GetProcessedSize() {return Stream.GetProcessedSize(); } 201 | }; 202 | 203 | }} 204 | 205 | #endif 206 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/RangeCoder/RangeCoderBit.cpp: -------------------------------------------------------------------------------- 1 | // Compress/RangeCoder/RangeCoderBit.cpp 2 | 3 | #include "StdAfx.h" 4 | 5 | #include "RangeCoderBit.h" 6 | 7 | namespace NCompress { 8 | namespace NRangeCoder { 9 | 10 | UInt32 CPriceTables::ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; 11 | static CPriceTables g_PriceTables; 12 | 13 | CPriceTables::CPriceTables() { Init(); } 14 | 15 | void CPriceTables::Init() 16 | { 17 | const int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits); 18 | for(int i = kNumBits - 1; i >= 0; i--) 19 | { 20 | UInt32 start = 1 << (kNumBits - i - 1); 21 | UInt32 end = 1 << (kNumBits - i); 22 | for (UInt32 j = start; j < end; j++) 23 | ProbPrices[j] = (i << kNumBitPriceShiftBits) + 24 | (((end - j) << kNumBitPriceShiftBits) >> (kNumBits - i - 1)); 25 | } 26 | 27 | /* 28 | // simplest: bad solution 29 | for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++) 30 | ProbPrices[i] = kBitPrice; 31 | */ 32 | 33 | /* 34 | const double kDummyMultMid = (1.0 / kBitPrice) / 2; 35 | const double kDummyMultMid = 0; 36 | // float solution 37 | double ln2 = log(double(2)); 38 | double lnAll = log(double(kBitModelTotal >> kNumMoveReducingBits)); 39 | for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++) 40 | ProbPrices[i] = UInt32((fabs(lnAll - log(double(i))) / ln2 + kDummyMultMid) * kBitPrice); 41 | */ 42 | 43 | /* 44 | // experimental, slow, solution: 45 | for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++) 46 | { 47 | const int kCyclesBits = 5; 48 | const UInt32 kCycles = (1 << kCyclesBits); 49 | 50 | UInt32 range = UInt32(-1); 51 | UInt32 bitCount = 0; 52 | for (UInt32 j = 0; j < kCycles; j++) 53 | { 54 | range >>= (kNumBitModelTotalBits - kNumMoveReducingBits); 55 | range *= i; 56 | while(range < (1 << 31)) 57 | { 58 | range <<= 1; 59 | bitCount++; 60 | } 61 | } 62 | bitCount <<= kNumBitPriceShiftBits; 63 | range -= (1 << 31); 64 | for (int k = kNumBitPriceShiftBits - 1; k >= 0; k--) 65 | { 66 | range <<= 1; 67 | if (range > (1 << 31)) 68 | { 69 | bitCount += (1 << k); 70 | range -= (1 << 31); 71 | } 72 | } 73 | ProbPrices[i] = (bitCount 74 | // + (1 << (kCyclesBits - 1)) 75 | ) >> kCyclesBits; 76 | } 77 | */ 78 | } 79 | 80 | }} 81 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/RangeCoder/RangeCoderBit.h: -------------------------------------------------------------------------------- 1 | // Compress/RangeCoder/RangeCoderBit.h 2 | 3 | #ifndef __COMPRESS_RANGECODER_BIT_H 4 | #define __COMPRESS_RANGECODER_BIT_H 5 | 6 | #include "RangeCoder.h" 7 | 8 | namespace NCompress { 9 | namespace NRangeCoder { 10 | 11 | const int kNumBitModelTotalBits = 11; 12 | const UInt32 kBitModelTotal = (1 << kNumBitModelTotalBits); 13 | 14 | const int kNumMoveReducingBits = 2; 15 | 16 | const int kNumBitPriceShiftBits = 6; 17 | const UInt32 kBitPrice = 1 << kNumBitPriceShiftBits; 18 | 19 | class CPriceTables 20 | { 21 | public: 22 | static UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; 23 | static void Init(); 24 | CPriceTables(); 25 | }; 26 | 27 | template 28 | class CBitModel 29 | { 30 | public: 31 | UInt32 Prob; 32 | void UpdateModel(UInt32 symbol) 33 | { 34 | /* 35 | Prob -= (Prob + ((symbol - 1) & ((1 << numMoveBits) - 1))) >> numMoveBits; 36 | Prob += (1 - symbol) << (kNumBitModelTotalBits - numMoveBits); 37 | */ 38 | if (symbol == 0) 39 | Prob += (kBitModelTotal - Prob) >> numMoveBits; 40 | else 41 | Prob -= (Prob) >> numMoveBits; 42 | } 43 | public: 44 | void Init() { Prob = kBitModelTotal / 2; } 45 | }; 46 | 47 | template 48 | class CBitEncoder: public CBitModel 49 | { 50 | public: 51 | void Encode(CEncoder *encoder, UInt32 symbol) 52 | { 53 | /* 54 | encoder->EncodeBit(this->Prob, kNumBitModelTotalBits, symbol); 55 | this->UpdateModel(symbol); 56 | */ 57 | UInt32 newBound = (encoder->Range >> kNumBitModelTotalBits) * this->Prob; 58 | if (symbol == 0) 59 | { 60 | encoder->Range = newBound; 61 | this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits; 62 | } 63 | else 64 | { 65 | encoder->Low += newBound; 66 | encoder->Range -= newBound; 67 | this->Prob -= (this->Prob) >> numMoveBits; 68 | } 69 | if (encoder->Range < kTopValue) 70 | { 71 | encoder->Range <<= 8; 72 | encoder->ShiftLow(); 73 | } 74 | } 75 | UInt32 GetPrice(UInt32 symbol) const 76 | { 77 | return CPriceTables::ProbPrices[ 78 | (((this->Prob - symbol) ^ ((-(int)symbol))) & (kBitModelTotal - 1)) >> kNumMoveReducingBits]; 79 | } 80 | UInt32 GetPrice0() const { return CPriceTables::ProbPrices[this->Prob >> kNumMoveReducingBits]; } 81 | UInt32 GetPrice1() const { return CPriceTables::ProbPrices[(kBitModelTotal - this->Prob) >> kNumMoveReducingBits]; } 82 | }; 83 | 84 | 85 | template 86 | class CBitDecoder: public CBitModel 87 | { 88 | public: 89 | UInt32 Decode(CDecoder *decoder) 90 | { 91 | UInt32 newBound = (decoder->Range >> kNumBitModelTotalBits) * this->Prob; 92 | if (decoder->Code < newBound) 93 | { 94 | decoder->Range = newBound; 95 | this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits; 96 | if (decoder->Range < kTopValue) 97 | { 98 | decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte(); 99 | decoder->Range <<= 8; 100 | } 101 | return 0; 102 | } 103 | else 104 | { 105 | decoder->Range -= newBound; 106 | decoder->Code -= newBound; 107 | this->Prob -= (this->Prob) >> numMoveBits; 108 | if (decoder->Range < kTopValue) 109 | { 110 | decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte(); 111 | decoder->Range <<= 8; 112 | } 113 | return 1; 114 | } 115 | } 116 | }; 117 | 118 | }} 119 | 120 | #endif 121 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/RangeCoder/RangeCoderBitTree.h: -------------------------------------------------------------------------------- 1 | // Compress/RangeCoder/RangeCoderBitTree.h 2 | 3 | #ifndef __COMPRESS_RANGECODER_BIT_TREE_H 4 | #define __COMPRESS_RANGECODER_BIT_TREE_H 5 | 6 | #include "RangeCoderBit.h" 7 | #include "RangeCoderOpt.h" 8 | 9 | namespace NCompress { 10 | namespace NRangeCoder { 11 | 12 | template 13 | class CBitTreeEncoder 14 | { 15 | CBitEncoder Models[1 << NumBitLevels]; 16 | public: 17 | void Init() 18 | { 19 | for(UInt32 i = 1; i < (1 << NumBitLevels); i++) 20 | Models[i].Init(); 21 | } 22 | void Encode(CEncoder *rangeEncoder, UInt32 symbol) 23 | { 24 | UInt32 modelIndex = 1; 25 | for (int bitIndex = NumBitLevels; bitIndex != 0 ;) 26 | { 27 | bitIndex--; 28 | UInt32 bit = (symbol >> bitIndex) & 1; 29 | Models[modelIndex].Encode(rangeEncoder, bit); 30 | modelIndex = (modelIndex << 1) | bit; 31 | } 32 | }; 33 | void ReverseEncode(CEncoder *rangeEncoder, UInt32 symbol) 34 | { 35 | UInt32 modelIndex = 1; 36 | for (int i = 0; i < NumBitLevels; i++) 37 | { 38 | UInt32 bit = symbol & 1; 39 | Models[modelIndex].Encode(rangeEncoder, bit); 40 | modelIndex = (modelIndex << 1) | bit; 41 | symbol >>= 1; 42 | } 43 | } 44 | UInt32 GetPrice(UInt32 symbol) const 45 | { 46 | symbol |= (1 << NumBitLevels); 47 | UInt32 price = 0; 48 | while (symbol != 1) 49 | { 50 | price += Models[symbol >> 1].GetPrice(symbol & 1); 51 | symbol >>= 1; 52 | } 53 | return price; 54 | } 55 | UInt32 ReverseGetPrice(UInt32 symbol) const 56 | { 57 | UInt32 price = 0; 58 | UInt32 modelIndex = 1; 59 | for (int i = NumBitLevels; i != 0; i--) 60 | { 61 | UInt32 bit = symbol & 1; 62 | symbol >>= 1; 63 | price += Models[modelIndex].GetPrice(bit); 64 | modelIndex = (modelIndex << 1) | bit; 65 | } 66 | return price; 67 | } 68 | }; 69 | 70 | template 71 | class CBitTreeDecoder 72 | { 73 | CBitDecoder Models[1 << NumBitLevels]; 74 | public: 75 | void Init() 76 | { 77 | for(UInt32 i = 1; i < (1 << NumBitLevels); i++) 78 | Models[i].Init(); 79 | } 80 | UInt32 Decode(CDecoder *rangeDecoder) 81 | { 82 | UInt32 modelIndex = 1; 83 | RC_INIT_VAR 84 | for(int bitIndex = NumBitLevels; bitIndex != 0; bitIndex--) 85 | { 86 | // modelIndex = (modelIndex << 1) + Models[modelIndex].Decode(rangeDecoder); 87 | RC_GETBIT(numMoveBits, Models[modelIndex].Prob, modelIndex) 88 | } 89 | RC_FLUSH_VAR 90 | return modelIndex - (1 << NumBitLevels); 91 | }; 92 | UInt32 ReverseDecode(CDecoder *rangeDecoder) 93 | { 94 | UInt32 modelIndex = 1; 95 | UInt32 symbol = 0; 96 | RC_INIT_VAR 97 | for(int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++) 98 | { 99 | // UInt32 bit = Models[modelIndex].Decode(rangeDecoder); 100 | // modelIndex <<= 1; 101 | // modelIndex += bit; 102 | // symbol |= (bit << bitIndex); 103 | RC_GETBIT2(numMoveBits, Models[modelIndex].Prob, modelIndex, ; , symbol |= (1 << bitIndex)) 104 | } 105 | RC_FLUSH_VAR 106 | return symbol; 107 | } 108 | }; 109 | 110 | template 111 | void ReverseBitTreeEncode(CBitEncoder *Models, 112 | CEncoder *rangeEncoder, int NumBitLevels, UInt32 symbol) 113 | { 114 | UInt32 modelIndex = 1; 115 | for (int i = 0; i < NumBitLevels; i++) 116 | { 117 | UInt32 bit = symbol & 1; 118 | Models[modelIndex].Encode(rangeEncoder, bit); 119 | modelIndex = (modelIndex << 1) | bit; 120 | symbol >>= 1; 121 | } 122 | } 123 | 124 | template 125 | UInt32 ReverseBitTreeGetPrice(CBitEncoder *Models, 126 | UInt32 NumBitLevels, UInt32 symbol) 127 | { 128 | UInt32 price = 0; 129 | UInt32 modelIndex = 1; 130 | for (int i = NumBitLevels; i != 0; i--) 131 | { 132 | UInt32 bit = symbol & 1; 133 | symbol >>= 1; 134 | price += Models[modelIndex].GetPrice(bit); 135 | modelIndex = (modelIndex << 1) | bit; 136 | } 137 | return price; 138 | } 139 | 140 | template 141 | UInt32 ReverseBitTreeDecode(CBitDecoder *Models, 142 | CDecoder *rangeDecoder, int NumBitLevels) 143 | { 144 | UInt32 modelIndex = 1; 145 | UInt32 symbol = 0; 146 | RC_INIT_VAR 147 | for(int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++) 148 | { 149 | // UInt32 bit = Models[modelIndex].Decode(rangeDecoder); 150 | // modelIndex <<= 1; 151 | // modelIndex += bit; 152 | // symbol |= (bit << bitIndex); 153 | RC_GETBIT2(numMoveBits, Models[modelIndex].Prob, modelIndex, ; , symbol |= (1 << bitIndex)) 154 | } 155 | RC_FLUSH_VAR 156 | return symbol; 157 | } 158 | 159 | }} 160 | 161 | #endif 162 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/RangeCoder/RangeCoderOpt.h: -------------------------------------------------------------------------------- 1 | // Compress/RangeCoder/RangeCoderOpt.h 2 | 3 | #ifndef __COMPRESS_RANGECODER_OPT_H 4 | #define __COMPRESS_RANGECODER_OPT_H 5 | 6 | #define RC_INIT_VAR \ 7 | UInt32 range = rangeDecoder->Range; \ 8 | UInt32 code = rangeDecoder->Code; 9 | 10 | #define RC_FLUSH_VAR \ 11 | rangeDecoder->Range = range; \ 12 | rangeDecoder->Code = code; 13 | 14 | #define RC_NORMALIZE \ 15 | if (range < NCompress::NRangeCoder::kTopValue) \ 16 | { code = (code << 8) | rangeDecoder->Stream.ReadByte(); range <<= 8; } 17 | 18 | #define RC_GETBIT2(numMoveBits, prob, mi, A0, A1) \ 19 | { UInt32 bound = (range >> NCompress::NRangeCoder::kNumBitModelTotalBits) * prob; \ 20 | if (code < bound) \ 21 | { A0; range = bound; \ 22 | prob += (NCompress::NRangeCoder::kBitModelTotal - prob) >> numMoveBits; \ 23 | mi <<= 1; } \ 24 | else \ 25 | { A1; range -= bound; code -= bound; prob -= (prob) >> numMoveBits; \ 26 | mi = (mi + mi) + 1; }} \ 27 | RC_NORMALIZE 28 | 29 | #define RC_GETBIT(numMoveBits, prob, mi) RC_GETBIT2(numMoveBits, prob, mi, ; , ;) 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /LZMA/7zip/Compress/RangeCoder/StdAfx.h: -------------------------------------------------------------------------------- 1 | // StdAfx.h 2 | 3 | #ifndef __STDAFX_H 4 | #define __STDAFX_H 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /LZMA/7zip/ICoder.h: -------------------------------------------------------------------------------- 1 | // ICoder.h 2 | 3 | #ifndef __ICODER_H 4 | #define __ICODER_H 5 | 6 | #include "IStream.h" 7 | 8 | // "23170F69-40C1-278A-0000-000400xx0000" 9 | #define CODER_INTERFACE(i, x) \ 10 | DEFINE_GUID(IID_ ## i, \ 11 | 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x04, 0x00, x, 0x00, 0x00); \ 12 | struct i: public IUnknown 13 | 14 | CODER_INTERFACE(ICompressProgressInfo, 0x04) 15 | { 16 | STDMETHOD(SetRatioInfo)(const UInt64 *inSize, const UInt64 *outSize) PURE; 17 | }; 18 | 19 | CODER_INTERFACE(ICompressCoder, 0x05) 20 | { 21 | STDMETHOD(Code)(ISequentialInStream *inStream, 22 | ISequentialOutStream *outStream, 23 | const UInt64 *inSize, 24 | const UInt64 *outSize, 25 | ICompressProgressInfo *progress) PURE; 26 | }; 27 | 28 | CODER_INTERFACE(ICompressCoder2, 0x18) 29 | { 30 | STDMETHOD(Code)(ISequentialInStream **inStreams, 31 | const UInt64 **inSizes, 32 | UInt32 numInStreams, 33 | ISequentialOutStream **outStreams, 34 | const UInt64 **outSizes, 35 | UInt32 numOutStreams, 36 | ICompressProgressInfo *progress) PURE; 37 | }; 38 | 39 | namespace NCoderPropID 40 | { 41 | enum EEnum 42 | { 43 | kDictionarySize = 0x400, 44 | kUsedMemorySize, 45 | kOrder, 46 | kPosStateBits = 0x440, 47 | kLitContextBits, 48 | kLitPosBits, 49 | kNumFastBytes = 0x450, 50 | kMatchFinder, 51 | kMatchFinderCycles, 52 | kNumPasses = 0x460, 53 | kAlgorithm = 0x470, 54 | kMultiThread = 0x480, 55 | kNumThreads, 56 | kEndMarker = 0x490 57 | }; 58 | } 59 | 60 | CODER_INTERFACE(ICompressSetCoderProperties, 0x20) 61 | { 62 | STDMETHOD(SetCoderProperties)(const PROPID *propIDs, 63 | const PROPVARIANT *properties, UInt32 numProperties) PURE; 64 | }; 65 | 66 | /* 67 | CODER_INTERFACE(ICompressSetCoderProperties, 0x21) 68 | { 69 | STDMETHOD(SetDecoderProperties)(ISequentialInStream *inStream) PURE; 70 | }; 71 | */ 72 | 73 | CODER_INTERFACE(ICompressSetDecoderProperties2, 0x22) 74 | { 75 | STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size) PURE; 76 | }; 77 | 78 | CODER_INTERFACE(ICompressWriteCoderProperties, 0x23) 79 | { 80 | STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStreams) PURE; 81 | }; 82 | 83 | CODER_INTERFACE(ICompressGetInStreamProcessedSize, 0x24) 84 | { 85 | STDMETHOD(GetInStreamProcessedSize)(UInt64 *value) PURE; 86 | }; 87 | 88 | CODER_INTERFACE(ICompressSetCoderMt, 0x25) 89 | { 90 | STDMETHOD(SetNumberOfThreads)(UInt32 numThreads) PURE; 91 | }; 92 | 93 | CODER_INTERFACE(ICompressGetSubStreamSize, 0x30) 94 | { 95 | STDMETHOD(GetSubStreamSize)(UInt64 subStream, UInt64 *value) PURE; 96 | }; 97 | 98 | CODER_INTERFACE(ICompressSetInStream, 0x31) 99 | { 100 | STDMETHOD(SetInStream)(ISequentialInStream *inStream) PURE; 101 | STDMETHOD(ReleaseInStream)() PURE; 102 | }; 103 | 104 | CODER_INTERFACE(ICompressSetOutStream, 0x32) 105 | { 106 | STDMETHOD(SetOutStream)(ISequentialOutStream *outStream) PURE; 107 | STDMETHOD(ReleaseOutStream)() PURE; 108 | }; 109 | 110 | CODER_INTERFACE(ICompressSetInStreamSize, 0x33) 111 | { 112 | STDMETHOD(SetInStreamSize)(const UInt64 *inSize) PURE; 113 | }; 114 | 115 | CODER_INTERFACE(ICompressSetOutStreamSize, 0x34) 116 | { 117 | STDMETHOD(SetOutStreamSize)(const UInt64 *outSize) PURE; 118 | }; 119 | 120 | CODER_INTERFACE(ICompressFilter, 0x40) 121 | { 122 | STDMETHOD(Init)() PURE; 123 | STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size) PURE; 124 | // Filter return outSize (UInt32) 125 | // if (outSize <= size): Filter have converted outSize bytes 126 | // if (outSize > size): Filter have not converted anything. 127 | // and it needs at least outSize bytes to convert one block 128 | // (it's for crypto block algorithms). 129 | }; 130 | 131 | CODER_INTERFACE(ICryptoProperties, 0x80) 132 | { 133 | STDMETHOD(SetKey)(const Byte *data, UInt32 size) PURE; 134 | STDMETHOD(SetInitVector)(const Byte *data, UInt32 size) PURE; 135 | }; 136 | 137 | CODER_INTERFACE(ICryptoSetPassword, 0x90) 138 | { 139 | STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size) PURE; 140 | }; 141 | 142 | CODER_INTERFACE(ICryptoSetCRC, 0xA0) 143 | { 144 | STDMETHOD(CryptoSetCRC)(UInt32 crc) PURE; 145 | }; 146 | 147 | ////////////////////// 148 | // It's for DLL file 149 | namespace NMethodPropID 150 | { 151 | enum EEnum 152 | { 153 | kID, 154 | kName, 155 | kDecoder, 156 | kEncoder, 157 | kInStreams, 158 | kOutStreams, 159 | kDescription 160 | }; 161 | } 162 | 163 | #endif 164 | -------------------------------------------------------------------------------- /LZMA/7zip/IStream.h: -------------------------------------------------------------------------------- 1 | // IStream.h 2 | 3 | #ifndef __ISTREAM_H 4 | #define __ISTREAM_H 5 | 6 | #include "../Common/MyUnknown.h" 7 | #include "../Common/Types.h" 8 | 9 | // "23170F69-40C1-278A-0000-000300xx0000" 10 | 11 | #define STREAM_INTERFACE_SUB(i, b, x) \ 12 | DEFINE_GUID(IID_ ## i, \ 13 | 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x03, 0x00, x, 0x00, 0x00); \ 14 | struct i: public b 15 | 16 | #define STREAM_INTERFACE(i, x) STREAM_INTERFACE_SUB(i, IUnknown, x) 17 | 18 | STREAM_INTERFACE(ISequentialInStream, 0x01) 19 | { 20 | STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize) PURE; 21 | /* 22 | Out: if size != 0, return_value = S_OK and (*processedSize == 0), 23 | then there are no more bytes in stream. 24 | if (size > 0) && there are bytes in stream, 25 | this function must read at least 1 byte. 26 | This function is allowed to read less than number of remaining bytes in stream. 27 | You must call Read function in loop, if you need exact amount of data 28 | */ 29 | }; 30 | 31 | STREAM_INTERFACE(ISequentialOutStream, 0x02) 32 | { 33 | STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize) PURE; 34 | /* 35 | if (size > 0) this function must write at least 1 byte. 36 | This function is allowed to write less than "size". 37 | You must call Write function in loop, if you need to write exact amount of data 38 | */ 39 | }; 40 | 41 | STREAM_INTERFACE_SUB(IInStream, ISequentialInStream, 0x03) 42 | { 43 | STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition) PURE; 44 | }; 45 | 46 | STREAM_INTERFACE_SUB(IOutStream, ISequentialOutStream, 0x04) 47 | { 48 | STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition) PURE; 49 | STDMETHOD(SetSize)(Int64 newSize) PURE; 50 | }; 51 | 52 | STREAM_INTERFACE(IStreamGetSize, 0x06) 53 | { 54 | STDMETHOD(GetSize)(UInt64 *size) PURE; 55 | }; 56 | 57 | STREAM_INTERFACE(IOutStreamFlush, 0x07) 58 | { 59 | STDMETHOD(Flush)() PURE; 60 | }; 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /LZMA/Common/Alloc.cpp: -------------------------------------------------------------------------------- 1 | // Common/Alloc.cpp 2 | 3 | #include "StdAfx.h" 4 | 5 | #ifdef _WIN32 6 | #include "MyWindows.h" 7 | #else 8 | #include 9 | #endif 10 | 11 | #include "Alloc.h" 12 | 13 | /* #define _SZ_ALLOC_DEBUG */ 14 | /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ 15 | #ifdef _SZ_ALLOC_DEBUG 16 | #include 17 | int g_allocCount = 0; 18 | int g_allocCountMid = 0; 19 | int g_allocCountBig = 0; 20 | #endif 21 | 22 | void *MyAlloc(size_t size) throw() 23 | { 24 | if (size == 0) 25 | return 0; 26 | #ifdef _SZ_ALLOC_DEBUG 27 | fprintf(stderr, "\nAlloc %10d bytes; count = %10d", size, g_allocCount++); 28 | #endif 29 | return ::malloc(size); 30 | } 31 | 32 | void MyFree(void *address) throw() 33 | { 34 | #ifdef _SZ_ALLOC_DEBUG 35 | if (address != 0) 36 | fprintf(stderr, "\nFree; count = %10d", --g_allocCount); 37 | #endif 38 | 39 | ::free(address); 40 | } 41 | 42 | #ifdef _WIN32 43 | 44 | void *MidAlloc(size_t size) throw() 45 | { 46 | if (size == 0) 47 | return 0; 48 | #ifdef _SZ_ALLOC_DEBUG 49 | fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++); 50 | #endif 51 | return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); 52 | } 53 | 54 | void MidFree(void *address) throw() 55 | { 56 | #ifdef _SZ_ALLOC_DEBUG 57 | if (address != 0) 58 | fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid); 59 | #endif 60 | if (address == 0) 61 | return; 62 | ::VirtualFree(address, 0, MEM_RELEASE); 63 | } 64 | 65 | static SIZE_T g_LargePageSize = 66 | #ifdef _WIN64 67 | (1 << 21); 68 | #else 69 | (1 << 22); 70 | #endif 71 | 72 | typedef SIZE_T (WINAPI *GetLargePageMinimumP)(); 73 | 74 | bool SetLargePageSize() 75 | { 76 | GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP) 77 | ::GetProcAddress(::GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum"); 78 | if (largePageMinimum == 0) 79 | return false; 80 | SIZE_T size = largePageMinimum(); 81 | if (size == 0 || (size & (size - 1)) != 0) 82 | return false; 83 | g_LargePageSize = size; 84 | return true; 85 | } 86 | 87 | 88 | void *BigAlloc(size_t size) throw() 89 | { 90 | if (size == 0) 91 | return 0; 92 | #ifdef _SZ_ALLOC_DEBUG 93 | fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++); 94 | #endif 95 | 96 | if (size >= (1 << 18)) 97 | { 98 | void *res = ::VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)), 99 | MEM_COMMIT /*| MEM_LARGE_PAGES*/, PAGE_READWRITE); 100 | if (res != 0) 101 | return res; 102 | } 103 | return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); 104 | } 105 | 106 | void BigFree(void *address) throw() 107 | { 108 | #ifdef _SZ_ALLOC_DEBUG 109 | if (address != 0) 110 | fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig); 111 | #endif 112 | 113 | if (address == 0) 114 | return; 115 | ::VirtualFree(address, 0, MEM_RELEASE); 116 | } 117 | 118 | #endif 119 | -------------------------------------------------------------------------------- /LZMA/Common/Alloc.h: -------------------------------------------------------------------------------- 1 | // Common/Alloc.h 2 | 3 | #ifndef __COMMON_ALLOC_H 4 | #define __COMMON_ALLOC_H 5 | 6 | #include 7 | 8 | void *MyAlloc(size_t size) throw(); 9 | void MyFree(void *address) throw(); 10 | 11 | #ifdef _WIN32 12 | 13 | bool SetLargePageSize(); 14 | 15 | void *MidAlloc(size_t size) throw(); 16 | void MidFree(void *address) throw(); 17 | void *BigAlloc(size_t size) throw(); 18 | void BigFree(void *address) throw(); 19 | 20 | #else 21 | 22 | #define MidAlloc(size) MyAlloc(size) 23 | #define MidFree(address) MyFree(address) 24 | #define BigAlloc(size) MyAlloc(size) 25 | #define BigFree(address) MyFree(address) 26 | 27 | #endif 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /LZMA/Common/CRC.cpp: -------------------------------------------------------------------------------- 1 | // Common/CRC.cpp 2 | 3 | #include "StdAfx.h" 4 | 5 | #include "CRC.h" 6 | 7 | static const UInt32 kCRCPoly = 0xEDB88320; 8 | 9 | UInt32 CCRC::Table[256]; 10 | 11 | void CCRC::InitTable() 12 | { 13 | for (UInt32 i = 0; i < 256; i++) 14 | { 15 | UInt32 r = i; 16 | for (int j = 0; j < 8; j++) 17 | if (r & 1) 18 | r = (r >> 1) ^ kCRCPoly; 19 | else 20 | r >>= 1; 21 | CCRC::Table[i] = r; 22 | } 23 | } 24 | 25 | class CCRCTableInit 26 | { 27 | public: 28 | CCRCTableInit() { CCRC::InitTable(); } 29 | } g_CRCTableInit; 30 | 31 | void CCRC::UpdateByte(Byte b) 32 | { 33 | _value = Table[((Byte)(_value)) ^ b] ^ (_value >> 8); 34 | } 35 | 36 | void CCRC::UpdateUInt16(UInt16 v) 37 | { 38 | UpdateByte(Byte(v)); 39 | UpdateByte(Byte(v >> 8)); 40 | } 41 | 42 | void CCRC::UpdateUInt32(UInt32 v) 43 | { 44 | for (int i = 0; i < 4; i++) 45 | UpdateByte((Byte)(v >> (8 * i))); 46 | } 47 | 48 | void CCRC::UpdateUInt64(UInt64 v) 49 | { 50 | for (int i = 0; i < 8; i++) 51 | UpdateByte((Byte)(v >> (8 * i))); 52 | } 53 | 54 | void CCRC::Update(const void *data, size_t size) 55 | { 56 | UInt32 v = _value; 57 | const Byte *p = (const Byte *)data; 58 | for (; size > 0 ; size--, p++) 59 | v = Table[((Byte)(v)) ^ *p] ^ (v >> 8); 60 | _value = v; 61 | } 62 | -------------------------------------------------------------------------------- /LZMA/Common/CRC.h: -------------------------------------------------------------------------------- 1 | // Common/CRC.h 2 | 3 | #ifndef __COMMON_CRC_H 4 | #define __COMMON_CRC_H 5 | 6 | #include 7 | #include "Types.h" 8 | 9 | class CCRC 10 | { 11 | UInt32 _value; 12 | public: 13 | static UInt32 Table[256]; 14 | static void InitTable(); 15 | 16 | CCRC(): _value(0xFFFFFFFF){}; 17 | void Init() { _value = 0xFFFFFFFF; } 18 | void UpdateByte(Byte v); 19 | void UpdateUInt16(UInt16 v); 20 | void UpdateUInt32(UInt32 v); 21 | void UpdateUInt64(UInt64 v); 22 | void Update(const void *data, size_t size); 23 | UInt32 GetDigest() const { return _value ^ 0xFFFFFFFF; } 24 | static UInt32 CalculateDigest(const void *data, size_t size) 25 | { 26 | CCRC crc; 27 | crc.Update(data, size); 28 | return crc.GetDigest(); 29 | } 30 | static bool VerifyDigest(UInt32 digest, const void *data, size_t size) 31 | { 32 | return (CalculateDigest(data, size) == digest); 33 | } 34 | }; 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /LZMA/Common/C_FileIO.cpp: -------------------------------------------------------------------------------- 1 | // Common/C_FileIO.h 2 | 3 | #include "C_FileIO.h" 4 | 5 | #include 6 | #include 7 | 8 | namespace NC { 9 | namespace NFile { 10 | namespace NIO { 11 | 12 | bool CFileBase::OpenBinary(const char *name, int flags) 13 | { 14 | #ifdef O_BINARY 15 | flags |= O_BINARY; 16 | #endif 17 | Close(); 18 | _handle = ::open(name, flags, 0666); 19 | return _handle != -1; 20 | } 21 | 22 | bool CFileBase::Close() 23 | { 24 | if(_handle == -1) 25 | return true; 26 | if (close(_handle) != 0) 27 | return false; 28 | _handle = -1; 29 | return true; 30 | } 31 | 32 | bool CFileBase::GetPosition(UInt64 &position) const 33 | { 34 | position = ::lseek(_handle, 0L, SEEK_CUR); 35 | return true; 36 | } 37 | 38 | bool CFileBase::GetLength(UInt64 &length) const 39 | { 40 | off_t curPos = Seek(0, SEEK_CUR); 41 | off_t lengthTemp = Seek(0, SEEK_END); 42 | Seek(curPos, SEEK_SET); 43 | length = (UInt64)lengthTemp; 44 | return true; 45 | } 46 | 47 | off_t CFileBase::Seek(off_t distanceToMove, int moveMethod) const 48 | { 49 | return ::lseek(_handle, distanceToMove, moveMethod); 50 | } 51 | 52 | ///////////////////////// 53 | // CInFile 54 | 55 | bool CInFile::Open(const char *name) 56 | { 57 | return CFileBase::OpenBinary(name, O_RDONLY); 58 | } 59 | 60 | ssize_t CInFile::Read(void *data, size_t size) 61 | { 62 | return read(_handle, data, size); 63 | } 64 | 65 | ///////////////////////// 66 | // COutFile 67 | 68 | bool COutFile::Create(const char *name, bool createAlways) 69 | { 70 | if (createAlways) 71 | { 72 | Close(); 73 | _handle = ::creat(name, 0666); 74 | return _handle != -1; 75 | } 76 | return OpenBinary(name, O_CREAT | O_EXCL | O_WRONLY); 77 | } 78 | 79 | ssize_t COutFile::Write(const void *data, size_t size) 80 | { 81 | return write(_handle, data, size); 82 | } 83 | 84 | }}} 85 | -------------------------------------------------------------------------------- /LZMA/Common/C_FileIO.h: -------------------------------------------------------------------------------- 1 | // Common/C_FileIO.h 2 | 3 | #ifndef __COMMON_C_FILEIO_H 4 | #define __COMMON_C_FILEIO_H 5 | 6 | #include 7 | #include 8 | 9 | #include "Types.h" 10 | #include "MyWindows.h" 11 | 12 | namespace NC { 13 | namespace NFile { 14 | namespace NIO { 15 | 16 | class CFileBase 17 | { 18 | protected: 19 | int _handle; 20 | bool OpenBinary(const char *name, int flags); 21 | public: 22 | CFileBase(): _handle(-1) {}; 23 | ~CFileBase() { Close(); } 24 | bool Close(); 25 | bool GetLength(UInt64 &length) const; 26 | bool GetPosition(UInt64 &position) const; 27 | off_t Seek(off_t distanceToMove, int moveMethod) const; 28 | }; 29 | 30 | class CInFile: public CFileBase 31 | { 32 | public: 33 | bool Open(const char *name); 34 | ssize_t Read(void *data, size_t size); 35 | }; 36 | 37 | class COutFile: public CFileBase 38 | { 39 | public: 40 | bool Create(const char *name, bool createAlways); 41 | ssize_t Write(const void *data, size_t size); 42 | }; 43 | 44 | }}} 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /LZMA/Common/Defs.h: -------------------------------------------------------------------------------- 1 | // Common/Defs.h 2 | 3 | #ifndef __COMMON_DEFS_H 4 | #define __COMMON_DEFS_H 5 | 6 | template inline T MyMin(T a, T b) 7 | { return a < b ? a : b; } 8 | template inline T MyMax(T a, T b) 9 | { return a > b ? a : b; } 10 | 11 | template inline int MyCompare(T a, T b) 12 | { return a < b ? -1 : (a == b ? 0 : 1); } 13 | 14 | inline int BoolToInt(bool value) 15 | { return (value ? 1: 0); } 16 | 17 | inline bool IntToBool(int value) 18 | { return (value != 0); } 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /LZMA/Common/MyCom.h: -------------------------------------------------------------------------------- 1 | // MyCom.h 2 | 3 | #ifndef __MYCOM_H 4 | #define __MYCOM_H 5 | 6 | #include "MyWindows.h" 7 | 8 | #define RINOK(x) { HRESULT __result_ = (x); if(__result_ != S_OK) return __result_; } 9 | 10 | template 11 | class CMyComPtr 12 | { 13 | T* _p; 14 | public: 15 | // typedef T _PtrClass; 16 | CMyComPtr() { _p = NULL;} 17 | CMyComPtr(T* p) {if ((_p = p) != NULL) p->AddRef(); } 18 | CMyComPtr(const CMyComPtr& lp) 19 | { 20 | if ((_p = lp._p) != NULL) 21 | _p->AddRef(); 22 | } 23 | ~CMyComPtr() { if (_p) _p->Release(); } 24 | void Release() { if (_p) { _p->Release(); _p = NULL; } } 25 | operator T*() const { return (T*)_p; } 26 | // T& operator*() const { return *_p; } 27 | T** operator&() { return &_p; } 28 | T* operator->() const { return _p; } 29 | T* operator=(T* p) 30 | { 31 | if (p != 0) 32 | p->AddRef(); 33 | if (_p) 34 | _p->Release(); 35 | _p = p; 36 | return p; 37 | } 38 | T* operator=(const CMyComPtr& lp) { return (*this = lp._p); } 39 | bool operator!() const { return (_p == NULL); } 40 | // bool operator==(T* pT) const { return _p == pT; } 41 | // Compare two objects for equivalence 42 | void Attach(T* p2) 43 | { 44 | Release(); 45 | _p = p2; 46 | } 47 | T* Detach() 48 | { 49 | T* pt = _p; 50 | _p = NULL; 51 | return pt; 52 | } 53 | #ifdef _WIN32 54 | HRESULT CoCreateInstance(REFCLSID rclsid, REFIID iid, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) 55 | { 56 | return ::CoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, (void**)&_p); 57 | } 58 | #endif 59 | /* 60 | HRESULT CoCreateInstance(LPCOLESTR szProgID, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) 61 | { 62 | CLSID clsid; 63 | HRESULT hr = CLSIDFromProgID(szProgID, &clsid); 64 | ATLASSERT(_p == NULL); 65 | if (SUCCEEDED(hr)) 66 | hr = ::CoCreateInstance(clsid, pUnkOuter, dwClsContext, __uuidof(T), (void**)&_p); 67 | return hr; 68 | } 69 | */ 70 | template 71 | HRESULT QueryInterface(REFGUID iid, Q** pp) const 72 | { 73 | return _p->QueryInterface(iid, (void**)pp); 74 | } 75 | }; 76 | 77 | ////////////////////////////////////////////////////////// 78 | 79 | class CMyComBSTR 80 | { 81 | public: 82 | BSTR m_str; 83 | CMyComBSTR() { m_str = NULL; } 84 | CMyComBSTR(LPCOLESTR pSrc) { m_str = ::SysAllocString(pSrc); } 85 | // CMyComBSTR(int nSize) { m_str = ::SysAllocStringLen(NULL, nSize); } 86 | // CMyComBSTR(int nSize, LPCOLESTR sz) { m_str = ::SysAllocStringLen(sz, nSize); } 87 | CMyComBSTR(const CMyComBSTR& src) { m_str = src.MyCopy(); } 88 | /* 89 | CMyComBSTR(REFGUID src) 90 | { 91 | LPOLESTR szGuid; 92 | StringFromCLSID(src, &szGuid); 93 | m_str = ::SysAllocString(szGuid); 94 | CoTaskMemFree(szGuid); 95 | } 96 | */ 97 | ~CMyComBSTR() { ::SysFreeString(m_str); } 98 | CMyComBSTR& operator=(const CMyComBSTR& src) 99 | { 100 | if (m_str != src.m_str) 101 | { 102 | if (m_str) 103 | ::SysFreeString(m_str); 104 | m_str = src.MyCopy(); 105 | } 106 | return *this; 107 | } 108 | CMyComBSTR& operator=(LPCOLESTR pSrc) 109 | { 110 | ::SysFreeString(m_str); 111 | m_str = ::SysAllocString(pSrc); 112 | return *this; 113 | } 114 | unsigned int Length() const { return ::SysStringLen(m_str); } 115 | operator BSTR() const { return m_str; } 116 | BSTR* operator&() { return &m_str; } 117 | BSTR MyCopy() const 118 | { 119 | int byteLen = ::SysStringByteLen(m_str); 120 | BSTR res = ::SysAllocStringByteLen(NULL, byteLen); 121 | memmove(res, m_str, byteLen); 122 | return res; 123 | } 124 | void Attach(BSTR src) { m_str = src; } 125 | BSTR Detach() 126 | { 127 | BSTR s = m_str; 128 | m_str = NULL; 129 | return s; 130 | } 131 | void Empty() 132 | { 133 | ::SysFreeString(m_str); 134 | m_str = NULL; 135 | } 136 | bool operator!() const { return (m_str == NULL); } 137 | }; 138 | 139 | 140 | ////////////////////////////////////////////////////////// 141 | 142 | class CMyUnknownImp 143 | { 144 | public: 145 | ULONG __m_RefCount; 146 | CMyUnknownImp(): __m_RefCount(0) {} 147 | }; 148 | 149 | #define MY_QUERYINTERFACE_BEGIN STDMETHOD(QueryInterface) \ 150 | (REFGUID iid, void **outObject) { 151 | 152 | #define MY_QUERYINTERFACE_ENTRY(i) if (iid == IID_ ## i) \ 153 | { *outObject = (void *)(i *)this; AddRef(); return S_OK; } 154 | 155 | #define MY_QUERYINTERFACE_END return E_NOINTERFACE; } 156 | 157 | #define MY_ADDREF_RELEASE \ 158 | STDMETHOD_(ULONG, AddRef)() { return ++__m_RefCount; } \ 159 | STDMETHOD_(ULONG, Release)() { if (--__m_RefCount != 0) \ 160 | return __m_RefCount; delete this; return 0; } 161 | 162 | #define MY_UNKNOWN_IMP_SPEC(i) \ 163 | MY_QUERYINTERFACE_BEGIN \ 164 | i \ 165 | MY_QUERYINTERFACE_END \ 166 | MY_ADDREF_RELEASE 167 | 168 | 169 | #define MY_UNKNOWN_IMP STDMETHOD(QueryInterface)(REFGUID, void **) { \ 170 | MY_QUERYINTERFACE_END \ 171 | MY_ADDREF_RELEASE 172 | 173 | #define MY_UNKNOWN_IMP1(i) MY_UNKNOWN_IMP_SPEC( \ 174 | MY_QUERYINTERFACE_ENTRY(i) \ 175 | ) 176 | 177 | #define MY_UNKNOWN_IMP2(i1, i2) MY_UNKNOWN_IMP_SPEC( \ 178 | MY_QUERYINTERFACE_ENTRY(i1) \ 179 | MY_QUERYINTERFACE_ENTRY(i2) \ 180 | ) 181 | 182 | #define MY_UNKNOWN_IMP3(i1, i2, i3) MY_UNKNOWN_IMP_SPEC( \ 183 | MY_QUERYINTERFACE_ENTRY(i1) \ 184 | MY_QUERYINTERFACE_ENTRY(i2) \ 185 | MY_QUERYINTERFACE_ENTRY(i3) \ 186 | ) 187 | 188 | #define MY_UNKNOWN_IMP4(i1, i2, i3, i4) MY_UNKNOWN_IMP_SPEC( \ 189 | MY_QUERYINTERFACE_ENTRY(i1) \ 190 | MY_QUERYINTERFACE_ENTRY(i2) \ 191 | MY_QUERYINTERFACE_ENTRY(i3) \ 192 | MY_QUERYINTERFACE_ENTRY(i4) \ 193 | ) 194 | 195 | #define MY_UNKNOWN_IMP5(i1, i2, i3, i4, i5) MY_UNKNOWN_IMP_SPEC( \ 196 | MY_QUERYINTERFACE_ENTRY(i1) \ 197 | MY_QUERYINTERFACE_ENTRY(i2) \ 198 | MY_QUERYINTERFACE_ENTRY(i3) \ 199 | MY_QUERYINTERFACE_ENTRY(i4) \ 200 | MY_QUERYINTERFACE_ENTRY(i5) \ 201 | ) 202 | 203 | #endif 204 | -------------------------------------------------------------------------------- /LZMA/Common/MyGuidDef.h: -------------------------------------------------------------------------------- 1 | // Common/MyGuidDef.h 2 | 3 | #ifndef GUID_DEFINED 4 | #define GUID_DEFINED 5 | 6 | #include "Types.h" 7 | 8 | typedef struct { 9 | UInt32 Data1; 10 | UInt16 Data2; 11 | UInt16 Data3; 12 | unsigned char Data4[8]; 13 | } GUID; 14 | 15 | #ifdef __cplusplus 16 | #define REFGUID const GUID & 17 | #else 18 | #define REFGUID const GUID * 19 | #endif 20 | 21 | #define REFCLSID REFGUID 22 | #define REFIID REFGUID 23 | 24 | #ifdef __cplusplus 25 | inline bool operator==(REFGUID g1, REFGUID g2) 26 | { 27 | for (int i = 0; i < (int)sizeof(g1); i++) 28 | if (((unsigned char *)&g1)[i] != ((unsigned char *)&g2)[i]) 29 | return false; 30 | return true; 31 | } 32 | inline bool operator!=(REFGUID g1, REFGUID g2) { return !(g1 == g2); } 33 | #endif 34 | 35 | #ifdef __cplusplus 36 | #define MY_EXTERN_C extern "C" 37 | #else 38 | #define MY_EXTERN_C extern 39 | #endif 40 | 41 | #endif // GUID_DEFINED 42 | 43 | 44 | #ifdef DEFINE_GUID 45 | #undef DEFINE_GUID 46 | #endif 47 | 48 | #ifdef INITGUID 49 | #define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ 50 | MY_EXTERN_C const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } } 51 | #else 52 | #define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ 53 | MY_EXTERN_C const GUID name 54 | #endif 55 | -------------------------------------------------------------------------------- /LZMA/Common/MyInitGuid.h: -------------------------------------------------------------------------------- 1 | // Common/MyInitGuid.h 2 | 3 | #ifndef __COMMON_MYINITGUID_H 4 | #define __COMMON_MYINITGUID_H 5 | 6 | #ifdef _WIN32 7 | #include 8 | #else 9 | #define INITGUID 10 | #include "MyGuidDef.h" 11 | #endif 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /LZMA/Common/MyUnknown.h: -------------------------------------------------------------------------------- 1 | // MyUnknown.h 2 | 3 | #ifndef __MYUNKNOWN_H 4 | #define __MYUNKNOWN_H 5 | 6 | #ifdef _WIN32 7 | 8 | #ifdef _WIN32_WCE 9 | #if (_WIN32_WCE > 300) 10 | #include 11 | #else 12 | #define MIDL_INTERFACE(x) struct 13 | #endif 14 | #else 15 | #include 16 | #endif 17 | 18 | #include 19 | 20 | #else 21 | #include "MyWindows.h" 22 | #endif 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /LZMA/Common/MyWindows.h: -------------------------------------------------------------------------------- 1 | // MyWindows.h 2 | 3 | #ifndef __MYWINDOWS_H 4 | #define __MYWINDOWS_H 5 | 6 | #ifdef _WIN32 7 | 8 | #include 9 | 10 | #else 11 | 12 | #include // for wchar_t 13 | #include 14 | #include 15 | 16 | #include "MyGuidDef.h" 17 | 18 | typedef char CHAR; 19 | typedef unsigned char UCHAR; 20 | typedef unsigned char BYTE; 21 | 22 | typedef short SHORT; 23 | typedef unsigned short USHORT; 24 | typedef unsigned short WORD; 25 | typedef short VARIANT_BOOL; 26 | 27 | typedef int INT; 28 | typedef int32_t INT32; 29 | typedef unsigned int UINT; 30 | typedef uint32_t UINT32; 31 | typedef INT32 LONG; // LONG, ULONG and DWORD must be 32-bit 32 | typedef UINT32 ULONG; 33 | typedef UINT32 DWORD; 34 | 35 | typedef int64_t LONGLONG; 36 | typedef uint64_t ULONGLONG; 37 | 38 | typedef struct LARGE_INTEGER { LONGLONG QuadPart; }LARGE_INTEGER; 39 | typedef struct _ULARGE_INTEGER { ULONGLONG QuadPart;} ULARGE_INTEGER; 40 | 41 | typedef const CHAR *LPCSTR; 42 | typedef CHAR TCHAR; 43 | typedef const TCHAR *LPCTSTR; 44 | typedef wchar_t WCHAR; 45 | typedef WCHAR OLECHAR; 46 | typedef const WCHAR *LPCWSTR; 47 | typedef OLECHAR *BSTR; 48 | typedef const OLECHAR *LPCOLESTR; 49 | typedef OLECHAR *LPOLESTR; 50 | 51 | typedef struct _FILETIME 52 | { 53 | DWORD dwLowDateTime; 54 | DWORD dwHighDateTime; 55 | }FILETIME; 56 | 57 | #define HRESULT LONG 58 | #define FAILED(Status) ((HRESULT)(Status)<0) 59 | typedef ULONG PROPID; 60 | typedef LONG SCODE; 61 | 62 | #define S_OK ((HRESULT)0x00000000L) 63 | #define S_FALSE ((HRESULT)0x00000001L) 64 | #define E_NOINTERFACE ((HRESULT)0x80004002L) 65 | #define E_ABORT ((HRESULT)0x80004004L) 66 | #define E_FAIL ((HRESULT)0x80004005L) 67 | #define STG_E_INVALIDFUNCTION ((HRESULT)0x80030001L) 68 | #define E_OUTOFMEMORY ((HRESULT)0x8007000EL) 69 | #define E_INVALIDARG ((HRESULT)0x80070057L) 70 | 71 | #ifdef _MSC_VER 72 | #define STDMETHODCALLTYPE __stdcall 73 | #else 74 | #define STDMETHODCALLTYPE 75 | #endif 76 | 77 | #define STDMETHOD_(t, f) virtual t STDMETHODCALLTYPE f 78 | #define STDMETHOD(f) STDMETHOD_(HRESULT, f) 79 | #define STDMETHODIMP_(type) type STDMETHODCALLTYPE 80 | #define STDMETHODIMP STDMETHODIMP_(HRESULT) 81 | 82 | #define PURE = 0 83 | 84 | #define MIDL_INTERFACE(x) struct 85 | 86 | struct IUnknown 87 | { 88 | STDMETHOD(QueryInterface) (REFIID iid, void **outObject) PURE; 89 | STDMETHOD_(ULONG, AddRef)() PURE; 90 | STDMETHOD_(ULONG, Release)() PURE; 91 | }; 92 | 93 | typedef IUnknown *LPUNKNOWN; 94 | 95 | #define VARIANT_TRUE ((VARIANT_BOOL)-1) 96 | #define VARIANT_FALSE ((VARIANT_BOOL)0) 97 | 98 | enum VARENUM 99 | { 100 | VT_EMPTY = 0, 101 | VT_NULL = 1, 102 | VT_I2 = 2, 103 | VT_I4 = 3, 104 | VT_R4 = 4, 105 | VT_R8 = 5, 106 | VT_CY = 6, 107 | VT_DATE = 7, 108 | VT_BSTR = 8, 109 | VT_DISPATCH = 9, 110 | VT_ERROR = 10, 111 | VT_BOOL = 11, 112 | VT_VARIANT = 12, 113 | VT_UNKNOWN = 13, 114 | VT_DECIMAL = 14, 115 | VT_I1 = 16, 116 | VT_UI1 = 17, 117 | VT_UI2 = 18, 118 | VT_UI4 = 19, 119 | VT_I8 = 20, 120 | VT_UI8 = 21, 121 | VT_INT = 22, 122 | VT_UINT = 23, 123 | VT_VOID = 24, 124 | VT_HRESULT = 25, 125 | VT_FILETIME = 64 126 | }; 127 | 128 | typedef unsigned short VARTYPE; 129 | typedef WORD PROPVAR_PAD1; 130 | typedef WORD PROPVAR_PAD2; 131 | typedef WORD PROPVAR_PAD3; 132 | 133 | typedef struct tagPROPVARIANT 134 | { 135 | VARTYPE vt; 136 | PROPVAR_PAD1 wReserved1; 137 | PROPVAR_PAD2 wReserved2; 138 | PROPVAR_PAD3 wReserved3; 139 | union 140 | { 141 | CHAR cVal; 142 | UCHAR bVal; 143 | SHORT iVal; 144 | USHORT uiVal; 145 | LONG lVal; 146 | ULONG ulVal; 147 | INT intVal; 148 | UINT uintVal; 149 | LARGE_INTEGER hVal; 150 | ULARGE_INTEGER uhVal; 151 | VARIANT_BOOL boolVal; 152 | SCODE scode; 153 | FILETIME filetime; 154 | BSTR bstrVal; 155 | }; 156 | } PROPVARIANT; 157 | 158 | typedef PROPVARIANT tagVARIANT; 159 | typedef tagVARIANT VARIANT; 160 | typedef VARIANT VARIANTARG; 161 | 162 | MY_EXTERN_C BSTR SysAllocStringByteLen(LPCSTR psz, UINT len); 163 | MY_EXTERN_C BSTR SysAllocString(const OLECHAR *sz); 164 | MY_EXTERN_C void SysFreeString(BSTR bstr); 165 | MY_EXTERN_C UINT SysStringByteLen(BSTR bstr); 166 | MY_EXTERN_C UINT SysStringLen(BSTR bstr); 167 | 168 | MY_EXTERN_C DWORD GetLastError(); 169 | MY_EXTERN_C HRESULT VariantClear(VARIANTARG *prop); 170 | MY_EXTERN_C HRESULT VariantCopy(VARIANTARG *dest, VARIANTARG *src); 171 | MY_EXTERN_C LONG CompareFileTime(const FILETIME* ft1, const FILETIME* ft2); 172 | 173 | #define CP_ACP 0 174 | #define CP_OEMCP 1 175 | 176 | typedef enum tagSTREAM_SEEK 177 | { 178 | STREAM_SEEK_SET = 0, 179 | STREAM_SEEK_CUR = 1, 180 | STREAM_SEEK_END = 2 181 | } STREAM_SEEK; 182 | 183 | #endif 184 | #endif 185 | -------------------------------------------------------------------------------- /LZMA/Common/NewHandler.cpp: -------------------------------------------------------------------------------- 1 | // NewHandler.cpp 2 | 3 | #include "StdAfx.h" 4 | 5 | #include 6 | 7 | #include "NewHandler.h" 8 | 9 | // #define DEBUG_MEMORY_LEAK 10 | 11 | #ifndef DEBUG_MEMORY_LEAK 12 | 13 | void * 14 | #ifdef _MSC_VER 15 | __cdecl 16 | #endif 17 | operator new(size_t size) 18 | { 19 | // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size); 20 | void *p = ::malloc(size); 21 | if (p == 0) 22 | throw CNewException(); 23 | return p; 24 | } 25 | 26 | void 27 | #ifdef _MSC_VER 28 | __cdecl 29 | #endif 30 | operator delete(void *p) throw() 31 | { 32 | /* 33 | if (p == 0) 34 | return; 35 | ::HeapFree(::GetProcessHeap(), 0, p); 36 | */ 37 | ::free(p); 38 | } 39 | 40 | #else 41 | 42 | #pragma init_seg(lib) 43 | const int kDebugSize = 1000000; 44 | static void *a[kDebugSize]; 45 | static int index = 0; 46 | 47 | static int numAllocs = 0; 48 | void * __cdecl operator new(size_t size) 49 | { 50 | numAllocs++; 51 | void *p = HeapAlloc(GetProcessHeap(), 0, size); 52 | if (index == 40) 53 | { 54 | int t = 1; 55 | } 56 | if (index < kDebugSize) 57 | { 58 | a[index] = p; 59 | index++; 60 | } 61 | if (p == 0) 62 | throw CNewException(); 63 | printf("Alloc %6d, size = %8d\n", numAllocs, size); 64 | return p; 65 | } 66 | 67 | class CC 68 | { 69 | public: 70 | CC() 71 | { 72 | for (int i = 0; i < kDebugSize; i++) 73 | a[i] = 0; 74 | } 75 | ~CC() 76 | { 77 | for (int i = 0; i < kDebugSize; i++) 78 | if (a[i] != 0) 79 | return; 80 | } 81 | } g_CC; 82 | 83 | 84 | void __cdecl operator delete(void *p) 85 | { 86 | if (p == 0) 87 | return; 88 | /* 89 | for (int i = 0; i < index; i++) 90 | if (a[i] == p) 91 | a[i] = 0; 92 | */ 93 | HeapFree(GetProcessHeap(), 0, p); 94 | numAllocs--; 95 | printf("Free %d\n", numAllocs); 96 | } 97 | 98 | #endif 99 | 100 | /* 101 | int MemErrorVC(size_t) 102 | { 103 | throw CNewException(); 104 | // return 1; 105 | } 106 | CNewHandlerSetter::CNewHandlerSetter() 107 | { 108 | // MemErrorOldVCFunction = _set_new_handler(MemErrorVC); 109 | } 110 | CNewHandlerSetter::~CNewHandlerSetter() 111 | { 112 | // _set_new_handler(MemErrorOldVCFunction); 113 | } 114 | */ 115 | -------------------------------------------------------------------------------- /LZMA/Common/NewHandler.h: -------------------------------------------------------------------------------- 1 | // Common/NewHandler.h 2 | 3 | #ifndef __COMMON_NEWHANDLER_H 4 | #define __COMMON_NEWHANDLER_H 5 | 6 | class CNewException {}; 7 | 8 | void 9 | #ifdef _MSC_VER 10 | __cdecl 11 | #endif 12 | operator delete(void *p) throw(); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /LZMA/Common/StdAfx.h: -------------------------------------------------------------------------------- 1 | // StdAfx.h 2 | 3 | #ifndef __STDAFX_H 4 | #define __STDAFX_H 5 | 6 | // #include "MyWindows.h" 7 | #include "NewHandler.h" 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /LZMA/Common/StringConvert.cpp: -------------------------------------------------------------------------------- 1 | // Common/StringConvert.cpp 2 | 3 | #include "StdAfx.h" 4 | 5 | #include "StringConvert.h" 6 | 7 | #ifndef _WIN32 8 | #include 9 | #endif 10 | 11 | #ifdef _WIN32 12 | UString MultiByteToUnicodeString(const AString &srcString, UINT codePage) 13 | { 14 | UString resultString; 15 | if(!srcString.IsEmpty()) 16 | { 17 | int numChars = MultiByteToWideChar(codePage, 0, srcString, 18 | srcString.Length(), resultString.GetBuffer(srcString.Length()), 19 | srcString.Length() + 1); 20 | #ifndef _WIN32_WCE 21 | if(numChars == 0) 22 | throw 282228; 23 | #endif 24 | resultString.ReleaseBuffer(numChars); 25 | } 26 | return resultString; 27 | } 28 | 29 | AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage) 30 | { 31 | AString resultString; 32 | if(!srcString.IsEmpty()) 33 | { 34 | int numRequiredBytes = srcString.Length() * 2; 35 | int numChars = WideCharToMultiByte(codePage, 0, srcString, 36 | srcString.Length(), resultString.GetBuffer(numRequiredBytes), 37 | numRequiredBytes + 1, NULL, NULL); 38 | #ifndef _WIN32_WCE 39 | if(numChars == 0) 40 | throw 282229; 41 | #endif 42 | resultString.ReleaseBuffer(numChars); 43 | } 44 | return resultString; 45 | } 46 | 47 | #ifndef _WIN32_WCE 48 | AString SystemStringToOemString(const CSysString &srcString) 49 | { 50 | AString result; 51 | CharToOem(srcString, result.GetBuffer(srcString.Length() * 2)); 52 | result.ReleaseBuffer(); 53 | return result; 54 | } 55 | #endif 56 | 57 | #else 58 | 59 | UString MultiByteToUnicodeString(const AString &srcString, UINT codePage) 60 | { 61 | UString resultString; 62 | for (int i = 0; i < srcString.Length(); i++) 63 | resultString += wchar_t(srcString[i]); 64 | /* 65 | if(!srcString.IsEmpty()) 66 | { 67 | int numChars = mbstowcs(resultString.GetBuffer(srcString.Length()), srcString, srcString.Length() + 1); 68 | if (numChars < 0) throw "Your environment does not support UNICODE"; 69 | resultString.ReleaseBuffer(numChars); 70 | } 71 | */ 72 | return resultString; 73 | } 74 | 75 | AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage) 76 | { 77 | AString resultString; 78 | for (int i = 0; i < srcString.Length(); i++) 79 | resultString += char(srcString[i]); 80 | /* 81 | if(!srcString.IsEmpty()) 82 | { 83 | int numRequiredBytes = srcString.Length() * 6 + 1; 84 | int numChars = wcstombs(resultString.GetBuffer(numRequiredBytes), srcString, numRequiredBytes); 85 | if (numChars < 0) throw "Your environment does not support UNICODE"; 86 | resultString.ReleaseBuffer(numChars); 87 | } 88 | */ 89 | return resultString; 90 | } 91 | 92 | #endif 93 | 94 | -------------------------------------------------------------------------------- /LZMA/Common/StringConvert.h: -------------------------------------------------------------------------------- 1 | // Common/StringConvert.h 2 | 3 | #ifndef __COMMON_STRINGCONVERT_H 4 | #define __COMMON_STRINGCONVERT_H 5 | 6 | #include "MyWindows.h" 7 | #include "String.h" 8 | #include "Types.h" 9 | 10 | UString MultiByteToUnicodeString(const AString &srcString, UINT codePage = CP_ACP); 11 | AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage = CP_ACP); 12 | 13 | inline const wchar_t* GetUnicodeString(const wchar_t* unicodeString) 14 | { return unicodeString; } 15 | inline const UString& GetUnicodeString(const UString &unicodeString) 16 | { return unicodeString; } 17 | inline UString GetUnicodeString(const AString &ansiString) 18 | { return MultiByteToUnicodeString(ansiString); } 19 | inline UString GetUnicodeString(const AString &multiByteString, UINT codePage) 20 | { return MultiByteToUnicodeString(multiByteString, codePage); } 21 | inline const wchar_t* GetUnicodeString(const wchar_t* unicodeString, UINT) 22 | { return unicodeString; } 23 | inline const UString& GetUnicodeString(const UString &unicodeString, UINT) 24 | { return unicodeString; } 25 | 26 | inline const char* GetAnsiString(const char* ansiString) 27 | { return ansiString; } 28 | inline const AString& GetAnsiString(const AString &ansiString) 29 | { return ansiString; } 30 | inline AString GetAnsiString(const UString &unicodeString) 31 | { return UnicodeStringToMultiByte(unicodeString); } 32 | 33 | inline const char* GetOemString(const char* oemString) 34 | { return oemString; } 35 | inline const AString& GetOemString(const AString &oemString) 36 | { return oemString; } 37 | inline AString GetOemString(const UString &unicodeString) 38 | { return UnicodeStringToMultiByte(unicodeString, CP_OEMCP); } 39 | 40 | 41 | #ifdef _UNICODE 42 | inline const wchar_t* GetSystemString(const wchar_t* unicodeString) 43 | { return unicodeString;} 44 | inline const UString& GetSystemString(const UString &unicodeString) 45 | { return unicodeString;} 46 | inline const wchar_t* GetSystemString(const wchar_t* unicodeString, UINT codePage) 47 | { return unicodeString;} 48 | inline const UString& GetSystemString(const UString &unicodeString, UINT codePage) 49 | { return unicodeString;} 50 | inline UString GetSystemString(const AString &multiByteString, UINT codePage) 51 | { return MultiByteToUnicodeString(multiByteString, codePage);} 52 | inline UString GetSystemString(const AString &multiByteString) 53 | { return MultiByteToUnicodeString(multiByteString);} 54 | #else 55 | inline const char* GetSystemString(const char *ansiString) 56 | { return ansiString; } 57 | inline const AString& GetSystemString(const AString &multiByteString, UINT) 58 | { return multiByteString; } 59 | inline const char * GetSystemString(const char *multiByteString, UINT) 60 | { return multiByteString; } 61 | inline AString GetSystemString(const UString &unicodeString) 62 | { return UnicodeStringToMultiByte(unicodeString); } 63 | inline AString GetSystemString(const UString &unicodeString, UINT codePage) 64 | { return UnicodeStringToMultiByte(unicodeString, codePage); } 65 | #endif 66 | 67 | #ifndef _WIN32_WCE 68 | AString SystemStringToOemString(const CSysString &srcString); 69 | #endif 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /LZMA/Common/Types.h: -------------------------------------------------------------------------------- 1 | // Common/Types.h 2 | 3 | #ifndef __COMMON_TYPES_H 4 | #define __COMMON_TYPES_H 5 | 6 | typedef unsigned char Byte; 7 | typedef short Int16; 8 | typedef unsigned short UInt16; 9 | typedef int Int32; 10 | typedef unsigned int UInt32; 11 | #ifdef _MSC_VER 12 | typedef __int64 Int64; 13 | typedef unsigned __int64 UInt64; 14 | #else 15 | typedef long long int Int64; 16 | typedef unsigned long long int UInt64; 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /LZMA/LZMA.h: -------------------------------------------------------------------------------- 1 | // LZMA.h 2 | 3 | #ifndef __LZMA_H 4 | #define __LZMA_H 5 | 6 | namespace NCompress { 7 | namespace NLZMA { 8 | 9 | const UInt32 kNumRepDistances = 4; 10 | 11 | const int kNumStates = 12; 12 | 13 | const Byte kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5}; 14 | const Byte kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10}; 15 | const Byte kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11}; 16 | const Byte kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11}; 17 | 18 | class CState 19 | { 20 | public: 21 | Byte Index; 22 | void Init() { Index = 0; } 23 | void UpdateChar() { Index = kLiteralNextStates[Index]; } 24 | void UpdateMatch() { Index = kMatchNextStates[Index]; } 25 | void UpdateRep() { Index = kRepNextStates[Index]; } 26 | void UpdateShortRep() { Index = kShortRepNextStates[Index]; } 27 | bool IsCharState() const { return Index < 7; } 28 | }; 29 | 30 | const int kNumPosSlotBits = 6; 31 | const int kDicLogSizeMin = 0; 32 | const int kDicLogSizeMax = 32; 33 | const int kDistTableSizeMax = kDicLogSizeMax * 2; 34 | 35 | const UInt32 kNumLenToPosStates = 4; 36 | 37 | inline UInt32 GetLenToPosState(UInt32 len) 38 | { 39 | len -= 2; 40 | if (len < kNumLenToPosStates) 41 | return len; 42 | return kNumLenToPosStates - 1; 43 | } 44 | 45 | namespace NLength { 46 | 47 | const int kNumPosStatesBitsMax = 4; 48 | const UInt32 kNumPosStatesMax = (1 << kNumPosStatesBitsMax); 49 | 50 | const int kNumPosStatesBitsEncodingMax = 4; 51 | const UInt32 kNumPosStatesEncodingMax = (1 << kNumPosStatesBitsEncodingMax); 52 | 53 | const int kNumLowBits = 3; 54 | const int kNumMidBits = 3; 55 | const int kNumHighBits = 8; 56 | const UInt32 kNumLowSymbols = 1 << kNumLowBits; 57 | const UInt32 kNumMidSymbols = 1 << kNumMidBits; 58 | const UInt32 kNumSymbolsTotal = kNumLowSymbols + kNumMidSymbols + (1 << kNumHighBits); 59 | 60 | } 61 | 62 | const UInt32 kMatchMinLen = 2; 63 | const UInt32 kMatchMaxLen = kMatchMinLen + NLength::kNumSymbolsTotal - 1; 64 | 65 | const int kNumAlignBits = 4; 66 | const UInt32 kAlignTableSize = 1 << kNumAlignBits; 67 | const UInt32 kAlignMask = (kAlignTableSize - 1); 68 | 69 | const UInt32 kStartPosModelIndex = 4; 70 | const UInt32 kEndPosModelIndex = 14; 71 | const UInt32 kNumPosModels = kEndPosModelIndex - kStartPosModelIndex; 72 | 73 | const UInt32 kNumFullDistances = 1 << (kEndPosModelIndex / 2); 74 | 75 | const int kNumLitPosStatesBitsEncodingMax = 4; 76 | const int kNumLitContextBitsMax = 8; 77 | 78 | const int kNumMoveBits = 5; 79 | 80 | }} 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /LZMA/LZMADecoder.h: -------------------------------------------------------------------------------- 1 | // LZMA/Decoder.h 2 | 3 | #ifndef __LZMA_DECODER_H 4 | #define __LZMA_DECODER_H 5 | 6 | #include "./Common/MyCom.h" 7 | #include "./Common/Alloc.h" 8 | #include "./7zip/ICoder.h" 9 | #include "./7zip/Compress/LZ/LZOutWindow.h" 10 | #include "./7zip/Compress/RangeCoder/RangeCoderBitTree.h" 11 | 12 | #include "LZMA.h" 13 | 14 | namespace NCompress { 15 | namespace NLZMA { 16 | 17 | typedef NRangeCoder::CBitDecoder CMyBitDecoder; 18 | 19 | class CLiteralDecoder2 20 | { 21 | CMyBitDecoder _decoders[0x300]; 22 | public: 23 | void Init() 24 | { 25 | for (int i = 0; i < 0x300; i++) 26 | _decoders[i].Init(); 27 | } 28 | Byte DecodeNormal(NRangeCoder::CDecoder *rangeDecoder) 29 | { 30 | UInt32 symbol = 1; 31 | RC_INIT_VAR 32 | do 33 | { 34 | // symbol = (symbol << 1) | _decoders[0][symbol].Decode(rangeDecoder); 35 | RC_GETBIT(kNumMoveBits, _decoders[symbol].Prob, symbol) 36 | } 37 | while (symbol < 0x100); 38 | RC_FLUSH_VAR 39 | return (Byte)symbol; 40 | } 41 | Byte DecodeWithMatchByte(NRangeCoder::CDecoder *rangeDecoder, Byte matchByte) 42 | { 43 | UInt32 symbol = 1; 44 | RC_INIT_VAR 45 | do 46 | { 47 | UInt32 matchBit = (matchByte >> 7) & 1; 48 | matchByte <<= 1; 49 | // UInt32 bit = _decoders[1 + matchBit][symbol].Decode(rangeDecoder); 50 | // symbol = (symbol << 1) | bit; 51 | UInt32 bit; 52 | RC_GETBIT2(kNumMoveBits, _decoders[0x100 + (matchBit << 8) + symbol].Prob, symbol, 53 | bit = 0, bit = 1) 54 | if (matchBit != bit) 55 | { 56 | while (symbol < 0x100) 57 | { 58 | // symbol = (symbol << 1) | _decoders[0][symbol].Decode(rangeDecoder); 59 | RC_GETBIT(kNumMoveBits, _decoders[symbol].Prob, symbol) 60 | } 61 | break; 62 | } 63 | } 64 | while (symbol < 0x100); 65 | RC_FLUSH_VAR 66 | return (Byte)symbol; 67 | } 68 | }; 69 | 70 | class CLiteralDecoder 71 | { 72 | CLiteralDecoder2 *_coders; 73 | int _numPrevBits; 74 | int _numPosBits; 75 | UInt32 _posMask; 76 | public: 77 | CLiteralDecoder(): _coders(0) {} 78 | ~CLiteralDecoder() { Free(); } 79 | void Free() 80 | { 81 | MyFree(_coders); 82 | _coders = 0; 83 | } 84 | bool Create(int numPosBits, int numPrevBits) 85 | { 86 | if (_coders == 0 || (numPosBits + numPrevBits) != 87 | (_numPrevBits + _numPosBits) ) 88 | { 89 | Free(); 90 | UInt32 numStates = 1 << (numPosBits + numPrevBits); 91 | _coders = (CLiteralDecoder2 *)MyAlloc(numStates * sizeof(CLiteralDecoder2)); 92 | } 93 | _numPosBits = numPosBits; 94 | _posMask = (1 << numPosBits) - 1; 95 | _numPrevBits = numPrevBits; 96 | return (_coders != 0); 97 | } 98 | void Init() 99 | { 100 | UInt32 numStates = 1 << (_numPrevBits + _numPosBits); 101 | for (UInt32 i = 0; i < numStates; i++) 102 | _coders[i].Init(); 103 | } 104 | UInt32 GetState(UInt32 pos, Byte prevByte) const 105 | { return ((pos & _posMask) << _numPrevBits) + (prevByte >> (8 - _numPrevBits)); } 106 | Byte DecodeNormal(NRangeCoder::CDecoder *rangeDecoder, UInt32 pos, Byte prevByte) 107 | { return _coders[GetState(pos, prevByte)].DecodeNormal(rangeDecoder); } 108 | Byte DecodeWithMatchByte(NRangeCoder::CDecoder *rangeDecoder, UInt32 pos, Byte prevByte, Byte matchByte) 109 | { return _coders[GetState(pos, prevByte)].DecodeWithMatchByte(rangeDecoder, matchByte); } 110 | }; 111 | 112 | namespace NLength { 113 | 114 | class CDecoder 115 | { 116 | CMyBitDecoder _choice; 117 | CMyBitDecoder _choice2; 118 | NRangeCoder::CBitTreeDecoder _lowCoder[kNumPosStatesMax]; 119 | NRangeCoder::CBitTreeDecoder _midCoder[kNumPosStatesMax]; 120 | NRangeCoder::CBitTreeDecoder _highCoder; 121 | public: 122 | void Init(UInt32 numPosStates) 123 | { 124 | _choice.Init(); 125 | _choice2.Init(); 126 | for (UInt32 posState = 0; posState < numPosStates; posState++) 127 | { 128 | _lowCoder[posState].Init(); 129 | _midCoder[posState].Init(); 130 | } 131 | _highCoder.Init(); 132 | } 133 | UInt32 Decode(NRangeCoder::CDecoder *rangeDecoder, UInt32 posState) 134 | { 135 | if(_choice.Decode(rangeDecoder) == 0) 136 | return _lowCoder[posState].Decode(rangeDecoder); 137 | if(_choice2.Decode(rangeDecoder) == 0) 138 | return kNumLowSymbols + _midCoder[posState].Decode(rangeDecoder); 139 | return kNumLowSymbols + kNumMidSymbols + _highCoder.Decode(rangeDecoder); 140 | } 141 | }; 142 | 143 | } 144 | 145 | class CDecoder: 146 | public ICompressCoder, 147 | public ICompressSetDecoderProperties2, 148 | #ifdef _ST_MODE 149 | public ICompressSetInStream, 150 | public ICompressSetOutStreamSize, 151 | public ISequentialInStream, 152 | #endif 153 | public CMyUnknownImp 154 | { 155 | CLZOutWindow _outWindowStream; 156 | NRangeCoder::CDecoder _rangeDecoder; 157 | 158 | CMyBitDecoder _isMatch[kNumStates][NLength::kNumPosStatesMax]; 159 | CMyBitDecoder _isRep[kNumStates]; 160 | CMyBitDecoder _isRepG0[kNumStates]; 161 | CMyBitDecoder _isRepG1[kNumStates]; 162 | CMyBitDecoder _isRepG2[kNumStates]; 163 | CMyBitDecoder _isRep0Long[kNumStates][NLength::kNumPosStatesMax]; 164 | 165 | NRangeCoder::CBitTreeDecoder _posSlotDecoder[kNumLenToPosStates]; 166 | 167 | CMyBitDecoder _posDecoders[kNumFullDistances - kEndPosModelIndex]; 168 | NRangeCoder::CBitTreeDecoder _posAlignDecoder; 169 | 170 | NLength::CDecoder _lenDecoder; 171 | NLength::CDecoder _repMatchLenDecoder; 172 | 173 | CLiteralDecoder _literalDecoder; 174 | 175 | UInt32 _posStateMask; 176 | 177 | /////////////////// 178 | // State 179 | UInt32 _reps[4]; 180 | CState _state; 181 | Int32 _remainLen; // -1 means end of stream. // -2 means need Init 182 | UInt64 _outSize; 183 | bool _outSizeDefined; 184 | 185 | void Init(); 186 | HRESULT CodeSpec(UInt32 size); 187 | public: 188 | 189 | #ifdef _ST_MODE 190 | MY_UNKNOWN_IMP4( 191 | ICompressSetDecoderProperties2, 192 | ICompressSetInStream, 193 | ICompressSetOutStreamSize, 194 | ISequentialInStream) 195 | #else 196 | MY_UNKNOWN_IMP1( 197 | ICompressSetDecoderProperties2) 198 | #endif 199 | 200 | void ReleaseStreams(); 201 | int filePos; 202 | UInt32 GetFilePos(); 203 | 204 | class CDecoderFlusher 205 | { 206 | CDecoder *_decoder; 207 | public: 208 | bool NeedFlush; 209 | CDecoderFlusher(CDecoder *decoder): _decoder(decoder), NeedFlush(true) {} 210 | ~CDecoderFlusher() 211 | { 212 | if (NeedFlush) 213 | _decoder->Flush(); 214 | _decoder->ReleaseStreams(); 215 | } 216 | }; 217 | 218 | HRESULT Flush() { return _outWindowStream.Flush(); } 219 | 220 | STDMETHOD(CodeReal)(ISequentialInStream *inStream, 221 | ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, 222 | ICompressProgressInfo *progress); 223 | 224 | STDMETHOD(Code)(ISequentialInStream *inStream, 225 | ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, 226 | ICompressProgressInfo *progress); 227 | 228 | STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); 229 | 230 | STDMETHOD(GetInStreamProcessedSize)(UInt64 *value); 231 | 232 | STDMETHOD(SetInStream)(ISequentialInStream *inStream); 233 | STDMETHOD(ReleaseInStream)(); 234 | STDMETHOD(SetOutStreamSize)(const UInt64 *outSize); 235 | 236 | #ifdef _ST_MODE 237 | STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); 238 | #endif 239 | 240 | void SetDictionarySize(int powerOfTwo); 241 | 242 | UInt32 _numLiteralPosStateBits; 243 | UInt32 _numLiteralContextBits; 244 | UInt32 _dictionarySize; 245 | #define D_kDefaultDictionaryLogSize 23 246 | 247 | CDecoder(): 248 | _posStateMask(4 - 1), 249 | _numLiteralPosStateBits(0), 250 | _numLiteralContextBits(3), 251 | _dictionarySize(1 << D_kDefaultDictionaryLogSize), 252 | _outSizeDefined(false) 253 | { 254 | } 255 | virtual ~CDecoder(); 256 | }; 257 | 258 | 259 | }} 260 | 261 | #endif 262 | -------------------------------------------------------------------------------- /LZMA/LZMAlib.cpp: -------------------------------------------------------------------------------- 1 | // LzmaLib.cpp 2 | 3 | #include "StdAfx.h" 4 | 5 | #include "./Common/MyWindows.h" 6 | #include "./Common/MyInitGuid.h" 7 | 8 | #include 9 | 10 | #if defined(_WIN32) || defined(OS2) || defined(MSDOS) 11 | #include 12 | #include 13 | #define MY_SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY) 14 | #else 15 | #define MY_SET_BINARY_MODE(file) 16 | #endif 17 | 18 | #include "./7zip/Common/FileStreams.h" 19 | #include "./7zip/Common/StreamUtils.h" 20 | 21 | #include "./LZMADecoder.h" 22 | #include "./LZMAEncoder.h" 23 | 24 | 25 | #include "StreamRamSolid.h" 26 | 27 | 28 | #ifdef _WIN32 29 | bool g_IsNT = false; 30 | static inline bool IsItWindowsNT() 31 | { 32 | OSVERSIONINFO versionInfo; 33 | versionInfo.dwOSVersionInfoSize = sizeof(versionInfo); 34 | if (!::GetVersionEx(&versionInfo)) 35 | return false; 36 | return (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT); 37 | } 38 | #endif 39 | 40 | 41 | 42 | 43 | 44 | void LZMAlib_putc(unsigned char b, COutFileStream * outStream) 45 | { 46 | outStream->Write(&b, 1, 0); 47 | } 48 | 49 | int LZMAlib_getc(CInFileStream * inStream) 50 | { 51 | Byte b; 52 | inStream->Read(&b, 1, 0); 53 | 54 | return b; 55 | } 56 | 57 | unsigned int LZMAlib_GetFileSize(CInFileStream * inStream) 58 | { 59 | Byte b; 60 | UInt32 outSize=0; 61 | 62 | for (int i=0; i<4; i++) 63 | { 64 | inStream->Read(&b, 1, 0); 65 | outSize=outSize*256+b; 66 | } 67 | 68 | // printf("LZMAlib outSize=%d\n",outSize); 69 | 70 | return outSize; 71 | } 72 | 73 | unsigned int LZMAlib_GetInputFilePos(CInFileStream * inStream) 74 | { 75 | UInt64 pos; 76 | inStream->File.GetPosition(pos); 77 | 78 | return (unsigned int) pos; 79 | } 80 | 81 | unsigned int LZMAlib_GetOutputFilePos(COutFileStream* outStream) 82 | { 83 | UInt64 pos; 84 | outStream->File.GetPosition(pos); 85 | 86 | return (unsigned int) pos; 87 | } 88 | 89 | int LZMAlib_PrepareInputFile(char* inputName, CInFileStream *inStream) 90 | { 91 | // CInFileStream *inStreamSpec=NULL; 92 | 93 | // inStreamSpec = new CInFileStream; 94 | // inStream = inStreamSpec; 95 | return (inStream->Open(inputName)); 96 | } 97 | 98 | 99 | int LZMAlib_PrepareOutputFile(char* outputName,COutFileStream *outStream) 100 | { 101 | // COutFileStream *outStreamSpec; 102 | 103 | // outStreamSpec = new COutFileStream; 104 | // outStream = outStreamSpec; 105 | return (outStream->Create(outputName, true)); 106 | } 107 | 108 | int LZMAlib_CloseInputFile(CInFileStream * inStream) 109 | { 110 | inStream->File.Close(); 111 | 112 | return 0; 113 | } 114 | 115 | int LZMAlib_CloseOutputFile(COutFileStream* outStream) 116 | { 117 | outStream->File.Close(); 118 | 119 | return 0; 120 | } 121 | 122 | 123 | NCompress::NLZMA::CDecoder decoder; 124 | int g_dictionarySize=23; 125 | 126 | int LZMAlib_DecodeFileToMem(CInFileStream * inStream,unsigned char* outBuffer,UInt32& outSize) 127 | { 128 | // prepare output 129 | CMyComPtr outStream; 130 | 131 | COutStreamRam *outStreamSpec = new COutStreamRam; 132 | outStream = outStreamSpec; 133 | 134 | outStreamSpec->Init(outBuffer, outSize); 135 | 136 | UInt64 outSize64=outSize; 137 | 138 | 139 | // printf("g_dictionarySize=%d\n",g_dictionarySize); 140 | 141 | decoder.SetDictionarySize(g_dictionarySize); 142 | 143 | // decode 144 | UInt64 before; 145 | inStream->File.GetPosition(before); 146 | 147 | if (decoder.Code(inStream, outStream, 0, &outSize64, 0) != S_OK) 148 | { 149 | fprintf(stderr, "Decoder error\n"); 150 | return 1; 151 | } 152 | 153 | UInt32 pos=decoder.GetFilePos(); 154 | 155 | inStream->Seek(pos+before,0,0); 156 | 157 | return 0; 158 | } 159 | 160 | NCompress::NLZMA::CEncoder encoder; 161 | 162 | int LZMAlib_EncodeMemToFile(unsigned char* inBuffer, int inSize, COutFileStream* outStream) 163 | { 164 | // prepare input 165 | CMyComPtr inStream; 166 | 167 | CInStreamRam *inStreamSpec = new CInStreamRam; 168 | inStream = inStreamSpec; 169 | 170 | inStreamSpec->Init(inBuffer, inSize); 171 | 172 | 173 | // LZMACoderProperties(encoderSpec); 174 | 175 | 176 | // write len 177 | Byte b; 178 | // printf("LZMAlib inSize=%d\n",inSize); 179 | b=inSize>>24; outStream->Write(&b, 1, 0); 180 | b=inSize>>16; outStream->Write(&b, 1, 0); 181 | b=inSize>>8; outStream->Write(&b, 1, 0); 182 | b=inSize; outStream->Write(&b, 1, 0); 183 | 184 | 185 | // CMyComPtr oStream; 186 | // oStream=outStream; 187 | 188 | encoder.SetDictionarySize(g_dictionarySize); 189 | 190 | HRESULT result = encoder.Code(inStream, outStream, 0, 0, 0); 191 | if (result == E_OUTOFMEMORY) 192 | { 193 | fprintf(stdout, "\nError: Can not allocate memory\n"); 194 | return 1; 195 | } 196 | else if (result != S_OK) 197 | { 198 | fprintf(stdout, "\nEncoder error = %X\n", (unsigned int)result); 199 | return 1; 200 | } 201 | 202 | return 0; 203 | } 204 | 205 | int LZMAlib_EncodeSolidMemToFile(unsigned char* inBuffer[], size_t inSize[], int count, COutFileStream* outStream) 206 | { 207 | // prepare input 208 | CMyComPtr inStream; 209 | 210 | CInStreamRamSolid *inStreamSpec = new CInStreamRamSolid; 211 | inStream = inStreamSpec; 212 | 213 | inStreamSpec->Init(inBuffer, inSize, count); 214 | 215 | 216 | // LZMACoderProperties(encoderSpec); 217 | 218 | int i,insize; 219 | for (i=0,insize=0; i>24; outStream->Write(&b, 1, 0); 226 | b=insize>>16; outStream->Write(&b, 1, 0); 227 | b=insize>>8; outStream->Write(&b, 1, 0); 228 | b=insize; outStream->Write(&b, 1, 0); 229 | 230 | 231 | // CMyComPtr oStream; 232 | // oStream=outStream; 233 | 234 | 235 | encoder.SetDictionarySize(g_dictionarySize); 236 | 237 | HRESULT result = encoder.Code(inStream, outStream, 0, 0, 0); 238 | if (result == E_OUTOFMEMORY) 239 | { 240 | fprintf(stdout, "\nError: Can not allocate memory\n"); 241 | return 1; 242 | } 243 | else if (result != S_OK) 244 | { 245 | fprintf(stdout, "\nEncoder error = %X\n", (unsigned int)result); 246 | return 1; 247 | } 248 | 249 | return 0; 250 | } 251 | 252 | void LZMAlib_Init(int dictionarySizeInMB) 253 | { 254 | #ifdef _WIN32 255 | g_IsNT = IsItWindowsNT(); 256 | #endif 257 | 258 | if (dictionarySizeInMB>=64) 259 | g_dictionarySize=26; 260 | else 261 | if (dictionarySizeInMB>=32) 262 | g_dictionarySize=25; 263 | else 264 | if (dictionarySizeInMB>=16) 265 | g_dictionarySize=24; 266 | else 267 | if (dictionarySizeInMB>=8) 268 | g_dictionarySize=23; 269 | else 270 | if (dictionarySizeInMB>=4) 271 | g_dictionarySize=22; 272 | else 273 | if (dictionarySizeInMB>=2) 274 | g_dictionarySize=21; 275 | else 276 | if (dictionarySizeInMB>=1) 277 | g_dictionarySize=20; 278 | else 279 | g_dictionarySize=16; 280 | } 281 | -------------------------------------------------------------------------------- /LZMA/LZMAlib.h: -------------------------------------------------------------------------------- 1 | // LzmaLib.h 2 | 3 | #ifndef __LzmaLib_h 4 | #define __LzmaLib_h 5 | 6 | //#include 7 | #include "./Common/MyCom.h" 8 | #include "./7zip/IStream.h" 9 | #include "./7zip/Common/FileStreams.h" 10 | 11 | void LZMAlib_Init(int dictionarySizeInMB); 12 | 13 | int LZMAlib_EncodeMemToFile(unsigned char* inBuffer, int inSize, COutFileStream* outStream); 14 | int LZMAlib_EncodeSolidMemToFile(unsigned char* inBuffer[], size_t inSize[], int count, COutFileStream* outStream); 15 | int LZMAlib_PrepareOutputFile(char* outputName,COutFileStream *outStream); 16 | int LZMAlib_CloseOutputFile(COutFileStream* outStream); 17 | 18 | int LZMAlib_DecodeFileToMem(CInFileStream * inStream,unsigned char* outBuffer,UInt32& outSize); 19 | int LZMAlib_PrepareInputFile(char* inputName, CInFileStream *inStream); 20 | int LZMAlib_CloseInputFile(CInFileStream * inStream); 21 | 22 | unsigned int LZMAlib_GetFileSize(CInFileStream * inStream); 23 | unsigned int LZMAlib_GetInputFilePos(CInFileStream * inStream); 24 | unsigned int LZMAlib_GetOutputFilePos(COutFileStream* outStream); 25 | void LZMAlib_putc(unsigned char b, COutFileStream * outStream); 26 | int LZMAlib_getc(CInFileStream * inStream); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /LZMA/StdAfx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // LZMAlib.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | -------------------------------------------------------------------------------- /LZMA/StdAfx.h: -------------------------------------------------------------------------------- 1 | // StdAfx.h 2 | 3 | #ifndef __STDAFX_H 4 | #define __STDAFX_H 5 | 6 | #include "./Common/MyWindows.h" 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /LZMA/StreamRam.cpp: -------------------------------------------------------------------------------- 1 | // StreamRam.cpp 2 | 3 | #include "StdAfx.h" 4 | #include "./Common/Types.h" 5 | #include "./LZMADecoder.h" 6 | #include "./LZMAEncoder.h" 7 | #include "StreamRam.h" 8 | 9 | 10 | 11 | STDMETHODIMP CInStreamRam::Read(void *data, UInt32 size, UInt32 *processedSize) 12 | { 13 | UInt32 remain = Size - Pos; 14 | if (size > remain) 15 | size = remain; 16 | for (UInt32 i = 0; i < size; i++) 17 | ((Byte *)data)[i] = Data[Pos + i]; 18 | Pos += size; 19 | if(processedSize != NULL) 20 | *processedSize = size; 21 | return S_OK; 22 | } 23 | 24 | STDMETHODIMP COutStreamRam::Write(const void *data, UInt32 size, UInt32 *processedSize) 25 | { 26 | UInt32 i; 27 | for (i = 0; i < size && Pos < Size; i++) 28 | Data[Pos++] = ((const Byte *)data)[i]; 29 | if(processedSize != NULL) 30 | *processedSize = i; 31 | if (i != size) 32 | { 33 | Overflow = true; 34 | return E_FAIL; 35 | } 36 | return S_OK; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /LZMA/StreamRam.h: -------------------------------------------------------------------------------- 1 | // StreamRam.h 2 | 3 | #ifndef __LzmaRam_h 4 | #define __LzmaRam_h 5 | 6 | #include 7 | #include "./Common/Types.h" 8 | 9 | class CInStreamRam: 10 | public ISequentialInStream, 11 | public CMyUnknownImp 12 | { 13 | const Byte *Data; 14 | size_t Size; 15 | size_t Pos; 16 | public: 17 | MY_UNKNOWN_IMP 18 | void Init(const Byte *data, size_t size) 19 | { 20 | Data = data; 21 | Size = size; 22 | Pos = 0; 23 | } 24 | STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); 25 | }; 26 | 27 | 28 | class COutStreamRam: 29 | public ISequentialOutStream, 30 | public CMyUnknownImp 31 | { 32 | size_t Size; 33 | public: 34 | Byte *Data; 35 | size_t Pos; 36 | bool Overflow; 37 | void Init(Byte *data, size_t size) 38 | { 39 | Data = data; 40 | Size = size; 41 | Pos = 0; 42 | Overflow = false; 43 | } 44 | void SetPos(size_t pos) 45 | { 46 | Overflow = false; 47 | Pos = pos; 48 | } 49 | MY_UNKNOWN_IMP 50 | HRESULT WriteByte(Byte b) 51 | { 52 | if (Pos >= Size) 53 | { 54 | Overflow = true; 55 | return E_FAIL; 56 | } 57 | Data[Pos++] = b; 58 | return S_OK; 59 | } 60 | STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); 61 | }; 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /LZMA/StreamRamSolid.h: -------------------------------------------------------------------------------- 1 | // StreamRamSolid.h 2 | 3 | #ifndef __StreamRamSolid_h 4 | #define __StreamRamSolid_h 5 | 6 | #include 7 | #include "StreamRam.h" 8 | 9 | class CInStreamRamSolid: 10 | public CInStreamRam 11 | { 12 | Byte **Data; 13 | size_t* Size; 14 | size_t* Pos; 15 | int Count; 16 | int CountMax; 17 | public: 18 | MY_UNKNOWN_IMP 19 | void Init(Byte *data[], size_t size[], int count) 20 | { 21 | Pos=new size_t[count]; 22 | Size=new size_t[count]; 23 | Data=(unsigned char**) malloc(sizeof(unsigned char*)*count); 24 | 25 | for (int i=0; i remain) 42 | size = remain; 43 | Byte * Dat=Data[Count]; 44 | for (UInt32 i = 0; i < size; i++) 45 | ((Byte *)data)[i] = Dat[Pos[Count] + i]; 46 | Pos[Count] += size; 47 | 48 | if(processedSize != NULL) *processedSize = size; 49 | 50 | if (Size[Count] - Pos[Count]==0 && Count kChunkSizeMax) 144 | size = kChunkSizeMax; 145 | DWORD processedLoc = 0; 146 | bool res = BOOLToBool(::ReadFile(_handle, data, size, &processedLoc, NULL)); 147 | processedSize = (UInt32)processedLoc; 148 | return res; 149 | } 150 | 151 | bool CInFile::Read(void *data, UInt32 size, UInt32 &processedSize) 152 | { 153 | processedSize = 0; 154 | do 155 | { 156 | UInt32 processedLoc = 0; 157 | bool res = ReadPart(data, size, processedLoc); 158 | processedSize += processedLoc; 159 | if (!res) 160 | return false; 161 | if (processedLoc == 0) 162 | return true; 163 | data = (void *)((unsigned char *)data + processedLoc); 164 | size -= processedLoc; 165 | } 166 | while (size > 0); 167 | return true; 168 | } 169 | 170 | ///////////////////////// 171 | // COutFile 172 | 173 | bool COutFile::Open(LPCTSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes) 174 | { return CFileBase::Create(fileName, GENERIC_WRITE, shareMode, creationDisposition, flagsAndAttributes); } 175 | 176 | static inline DWORD GetCreationDisposition(bool createAlways) 177 | { return createAlways? CREATE_ALWAYS: CREATE_NEW; } 178 | 179 | bool COutFile::Open(LPCTSTR fileName, DWORD creationDisposition) 180 | { return Open(fileName, FILE_SHARE_READ, creationDisposition, FILE_ATTRIBUTE_NORMAL); } 181 | 182 | bool COutFile::Create(LPCTSTR fileName, bool createAlways) 183 | { return Open(fileName, GetCreationDisposition(createAlways)); } 184 | 185 | #ifndef _UNICODE 186 | 187 | bool COutFile::Open(LPCWSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes) 188 | { return CFileBase::Create(fileName, GENERIC_WRITE, shareMode, creationDisposition, flagsAndAttributes); } 189 | 190 | bool COutFile::Open(LPCWSTR fileName, DWORD creationDisposition) 191 | { return Open(fileName, FILE_SHARE_READ, creationDisposition, FILE_ATTRIBUTE_NORMAL); } 192 | 193 | bool COutFile::Create(LPCWSTR fileName, bool createAlways) 194 | { return Open(fileName, GetCreationDisposition(createAlways)); } 195 | 196 | #endif 197 | 198 | bool COutFile::SetTime(const FILETIME *creationTime, const FILETIME *lastAccessTime, const FILETIME *lastWriteTime) 199 | { return BOOLToBool(::SetFileTime(_handle, creationTime, lastAccessTime, lastWriteTime)); } 200 | 201 | bool COutFile::SetLastWriteTime(const FILETIME *lastWriteTime) 202 | { return SetTime(NULL, NULL, lastWriteTime); } 203 | 204 | bool COutFile::WritePart(const void *data, UInt32 size, UInt32 &processedSize) 205 | { 206 | if (size > kChunkSizeMax) 207 | size = kChunkSizeMax; 208 | DWORD processedLoc = 0; 209 | bool res = BOOLToBool(::WriteFile(_handle, data, size, &processedLoc, NULL)); 210 | processedSize = (UInt32)processedLoc; 211 | return res; 212 | } 213 | 214 | bool COutFile::Write(const void *data, UInt32 size, UInt32 &processedSize) 215 | { 216 | processedSize = 0; 217 | do 218 | { 219 | UInt32 processedLoc = 0; 220 | bool res = WritePart(data, size, processedLoc); 221 | processedSize += processedLoc; 222 | if (!res) 223 | return false; 224 | if (processedLoc == 0) 225 | return true; 226 | data = (const void *)((const unsigned char *)data + processedLoc); 227 | size -= processedLoc; 228 | } 229 | while (size > 0); 230 | return true; 231 | } 232 | 233 | bool COutFile::SetEndOfFile() { return BOOLToBool(::SetEndOfFile(_handle)); } 234 | 235 | bool COutFile::SetLength(UInt64 length) 236 | { 237 | UInt64 newPosition; 238 | if(!Seek(length, newPosition)) 239 | return false; 240 | if(newPosition != length) 241 | return false; 242 | return SetEndOfFile(); 243 | } 244 | 245 | }}} 246 | -------------------------------------------------------------------------------- /LZMA/Windows/FileIO.h: -------------------------------------------------------------------------------- 1 | // Windows/FileIO.h 2 | 3 | #ifndef __WINDOWS_FILEIO_H 4 | #define __WINDOWS_FILEIO_H 5 | 6 | #include "../Common/Types.h" 7 | 8 | namespace NWindows { 9 | namespace NFile { 10 | namespace NIO { 11 | 12 | struct CByHandleFileInfo 13 | { 14 | DWORD Attributes; 15 | FILETIME CreationTime; 16 | FILETIME LastAccessTime; 17 | FILETIME LastWriteTime; 18 | DWORD VolumeSerialNumber; 19 | UInt64 Size; 20 | DWORD NumberOfLinks; 21 | UInt64 FileIndex; 22 | }; 23 | 24 | class CFileBase 25 | { 26 | protected: 27 | bool _fileIsOpen; 28 | HANDLE _handle; 29 | bool Create(LPCTSTR fileName, DWORD desiredAccess, 30 | DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); 31 | #ifndef _UNICODE 32 | bool Create(LPCWSTR fileName, DWORD desiredAccess, 33 | DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); 34 | #endif 35 | 36 | public: 37 | CFileBase(): _fileIsOpen(false){}; 38 | virtual ~CFileBase(); 39 | 40 | virtual bool Close(); 41 | 42 | bool GetPosition(UInt64 &position) const; 43 | bool GetLength(UInt64 &length) const; 44 | 45 | bool Seek(Int64 distanceToMove, DWORD moveMethod, UInt64 &newPosition) const; 46 | bool Seek(UInt64 position, UInt64 &newPosition); 47 | bool SeekToBegin(); 48 | bool SeekToEnd(UInt64 &newPosition); 49 | 50 | bool GetFileInformation(CByHandleFileInfo &fileInfo) const; 51 | }; 52 | 53 | class CInFile: public CFileBase 54 | { 55 | public: 56 | bool Open(LPCTSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); 57 | bool Open(LPCTSTR fileName); 58 | #ifndef _UNICODE 59 | bool Open(LPCWSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); 60 | bool Open(LPCWSTR fileName); 61 | #endif 62 | bool ReadPart(void *data, UInt32 size, UInt32 &processedSize); 63 | bool Read(void *data, UInt32 size, UInt32 &processedSize); 64 | }; 65 | 66 | class COutFile: public CFileBase 67 | { 68 | // DWORD m_CreationDisposition; 69 | public: 70 | // COutFile(): m_CreationDisposition(CREATE_NEW){}; 71 | bool Open(LPCTSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); 72 | bool Open(LPCTSTR fileName, DWORD creationDisposition); 73 | bool Create(LPCTSTR fileName, bool createAlways); 74 | 75 | #ifndef _UNICODE 76 | bool Open(LPCWSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); 77 | bool Open(LPCWSTR fileName, DWORD creationDisposition); 78 | bool Create(LPCWSTR fileName, bool createAlways); 79 | #endif 80 | 81 | /* 82 | void SetOpenCreationDisposition(DWORD creationDisposition) 83 | { m_CreationDisposition = creationDisposition; } 84 | void SetOpenCreationDispositionCreateAlways() 85 | { m_CreationDisposition = CREATE_ALWAYS; } 86 | */ 87 | 88 | bool SetTime(const FILETIME *creationTime, const FILETIME *lastAccessTime, const FILETIME *lastWriteTime); 89 | bool SetLastWriteTime(const FILETIME *lastWriteTime); 90 | bool WritePart(const void *data, UInt32 size, UInt32 &processedSize); 91 | bool Write(const void *data, UInt32 size, UInt32 &processedSize); 92 | bool SetEndOfFile(); 93 | bool SetLength(UInt64 length); 94 | }; 95 | 96 | }}} 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /LZMA/Windows/StdAfx.h: -------------------------------------------------------------------------------- 1 | // StdAfx.h 2 | 3 | #ifndef __STDAFX_H 4 | #define __STDAFX_H 5 | 6 | #include "../Common/MyWindows.h" 7 | #include "../Common/NewHandler.h" 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #BUILD_SYSTEM = linux 2 | #BUILD_ARCH = 32-bit 3 | 4 | ifeq ($(BUILD_SYSTEM),linux) 5 | DEFINES += -D_POSIX_ENVIRONMENT_ -DZ_HAVE_UNISTD_H 6 | LZMA_FILES = LZMA/Common/C_FileIO.o 7 | LDFLAGS = -lrt 8 | GCC = gcc 9 | GPP = g++ 10 | else 11 | DEFINES += -D_WIN32_ENVIRONMENT_ 12 | LZMA_FILES = LZMA/Windows/FileIO.o 13 | # DEFINES = -march=core2 -march=nocona -march=k8 -march=native 14 | LDFLAGS = -lshell32 -lole32 -loleaut32 15 | GCC = gcc.exe 16 | GPP = g++.exe 17 | endif 18 | 19 | ifneq ($(BUILD_ARCH),32-bit) 20 | DEFINES += -D__x86_64__ 21 | LDFLAGS += -static -L C:\Aplikacje\win-builds64\lib 22 | else 23 | LDFLAGS += -static -L C:\Aplikacje\win-builds32\lib 24 | endif 25 | 26 | 27 | #DEFINES += -DBENCH_REMOVE_XXX 28 | DEFINES += -I. 29 | CODE_FLAGS = -Wno-unknown-pragmas -Wno-sign-compare -Wno-conversion # -Wall # -Wextra 30 | OPT_FLAGS = -fomit-frame-pointer -fstrict-aliasing -fforce-addr -ffast-math 31 | 32 | #BUILD_TYPE = debug 33 | 34 | ifeq ($(BUILD_TYPE),debug) 35 | OPT_FLAGS += -g 36 | else 37 | OPT_FLAGS += -O3 38 | endif 39 | 40 | CFLAGS = $(CODE_FLAGS) $(OPT_FLAGS) $(DEFINES) 41 | 42 | 43 | all: xwrt 44 | 45 | PPMD_FILES = PPMd/Model.o PPMd/PPMdlib.o 46 | 47 | ZLIB_FILES = zlib/adler32.o zlib/compress.o zlib/crc32.o zlib/deflate.o zlib/gzclose.o zlib/gzlib.o zlib/gzread.o 48 | ZLIB_FILES += zlib/gzwrite.o zlib/infback.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o zlib/trees.o zlib/uncompr.o zlib/zutil.o 49 | 50 | LZMA_FILES += LZMA/LZMAlib.o LZMA/LZMAEncoder.o LZMA/LZMADecoder.o LZMA/7zip/Common/FileStreams.o 51 | LZMA_FILES += LZMA/7zip/Common/StreamUtils.o LZMA/7zip/Common/OutBuffer.o LZMA/7zip/Common/InBuffer.o LZMA/StreamRam.o 52 | LZMA_FILES += LZMA/Common/Alloc.o LZMA/7zip/Compress/LZ/LZInWindow.o LZMA/7zip/Compress/LZ/LZOutWindow.o 53 | LZMA_FILES += LZMA/7zip/Compress/RangeCoder/RangeCoderBit.o LZMA/Common/CRC.o LZMA/Common/StringConvert.o LZMA/Common/NewHandler.o 54 | 55 | xwrt: $(LZMA_FILES) $(ZLIB_FILES) src/XWRT.o src/Encoder.o src/Decoder.o src/MemBuffer.o src/Common.o $(PPMD_FILES) 56 | $(GPP) $^ -o $@ $(LDFLAGS) 57 | 58 | .c.o: 59 | $(GCC) $(CFLAGS) $< -c -o $@ 60 | 61 | .cc.o: 62 | $(GPP) $(CFLAGS) $< -c -o $@ 63 | 64 | .cpp.o: 65 | $(GPP) $(CFLAGS) $< -c -o $@ 66 | 67 | clean: 68 | rm -f xwrt.exe xwrt LZMA/*.o LZMA/Windows/*.o LZMA/Common/*.o LZMA/7zip/Common/*.o LZMA/7zip/Compress/LZ/*.o LZMA/7zip/Compress/RangeCoder/*.o zlib/*.o src/*.o PPMd/*.o *.o 69 | -------------------------------------------------------------------------------- /PPMd/Coder.hpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This file is part of PPMd project * 3 | * Contents: 'Carryless rangecoder' by Dmitry Subbotin * 4 | * Comments: this implementation is claimed to be a public domain * 5 | ****************************************************************************/ 6 | /********************** Original text ************************************* 7 | //////// Carryless rangecoder (c) 1999 by Dmitry Subbotin //////// 8 | 9 | typedef unsigned int uint; 10 | typedef unsigned char uc; 11 | 12 | #define DO(n) for (int _=0; _>24), low<<=8; } 30 | void StartDecode (FILE *F) { passed=low=code=0; range= (uint) -1; 31 | f=F; DO(4) code= code<<8 | InByte(); 32 | } 33 | 34 | void Encode (uint cumFreq, uint freq, uint totFreq) { 35 | assert(cumFreq+freq>24), range<<=8, low<<=8; 40 | } 41 | 42 | uint GetFreq (uint totFreq) { 43 | uint tmp= (code-low) / (range/= totFreq); 44 | if (tmp >= totFreq) throw ("Input data corrupt"); // or force it to return 45 | return tmp; // a valid value :) 46 | } 47 | 48 | void Decode (uint cumFreq, uint freq, uint totFreq) { 49 | assert(cumFreq+freq> 24,stream); \ 66 | range <<= 8; low <<= 8; \ 67 | } \ 68 | } 69 | inline void rcEncodeSymbol() 70 | { 71 | low += Range.low*(range/=Range.scale); range *= Range.high-Range.low; 72 | } 73 | inline void rcFlushEncoder(_PPMD_FILE* stream) 74 | { 75 | for (UINT i=0;i < 4;i++) { 76 | _PPMD_E_PUTC(low >> 24,stream); 77 | low <<= 8; 78 | } 79 | } 80 | inline void rcInitDecoder(_PPMD_FILE* stream) 81 | { 82 | low=code=0; range=DWORD(-1); 83 | for (UINT i=0;i < 4;i++) 84 | code=(code << 8) | _PPMD_D_GETC(stream); 85 | } 86 | #define RC_DEC_NORMALIZE(stream) { \ 87 | while (((low ^ (low+range)) < TOP) || ((range < BOT) && ((range= -low & (BOT-1)),1))) { \ 88 | code=(code << 8) | _PPMD_D_GETC(stream); \ 89 | range <<= 8; low <<= 8; \ 90 | } \ 91 | } 92 | inline UINT rcGetCurrentCount() { return (code-low)/(range /= Range.scale); } 93 | inline void rcRemoveSubrange() 94 | { 95 | low += range*Range.low; range *= Range.high-Range.low; 96 | } 97 | inline UINT rcBinStart(UINT f0,UINT Shift) { return f0*(range >>= Shift); } 98 | inline UINT rcBinDecode (UINT tmp) { return (code-low >= tmp); } 99 | inline void rcBinCorrect0(UINT tmp) { range=tmp; } 100 | inline void rcBinCorrect1(UINT tmp,UINT f1) { low += tmp; range *= f1; } 101 | -------------------------------------------------------------------------------- /PPMd/PPMd.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This file is part of PPMd project * 3 | * Written and distributed to public domain by Dmitry Shkarin 1997, * 4 | * 1999-2001, 2006 * 5 | * Contents: interface to encoding/decoding routines * 6 | * Comments: this file can be used as an interface to PPMd module * 7 | * (consisting of Model.cpp) from external program * 8 | ****************************************************************************/ 9 | #if !defined(_PPMD_H_) 10 | #define _PPMD_H_ 11 | 12 | #include "PPMdType.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | BOOL _STDCALL StartSubAllocator(UINT SubAllocatorSize); 19 | void _STDCALL StopSubAllocator(); /* it can be called once */ 20 | UINT _STDCALL GetUsedMemory(); /* for information only */ 21 | 22 | /**************************************************************************** 23 | * (MaxOrder == 1) parameter value has special meaning, it does not restart * 24 | * model and can be used for solid mode archives; * 25 | * Call sequence: * 26 | * StartSubAllocator(SubAllocatorSize); * 27 | * EncodeFile(SolidArcFile,File1,MaxOrder,TRUE); * 28 | * EncodeFile(SolidArcFile,File2, 1,TRUE); * 29 | * ... * 30 | * EncodeFile(SolidArcFile,FileN, 1,TRUE); * 31 | * StopSubAllocator(); * 32 | ****************************************************************************/ 33 | void _STDCALL EncodeFile(_PPMD_FILE* EncodedFile,_PPMD_FILE* DecodedFile, 34 | int MaxOrder,BOOL CutOff); 35 | void _STDCALL DecodeFile(_PPMD_FILE* DecodedFile,_PPMD_FILE* EncodedFile, 36 | int MaxOrder,BOOL CutOff); 37 | 38 | /* imported function */ 39 | void _STDCALL PrintInfo(_PPMD_FILE* DecodedFile,_PPMD_FILE* EncodedFile); 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif /* !defined(_PPMD_H_) */ 46 | -------------------------------------------------------------------------------- /PPMd/PPMdType.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This file is part of PPMd project * 3 | * Written and distributed to public domain by Dmitry Shkarin 1997, * 4 | * 1999-2001, 2006 * 5 | * Contents: compilation parameters and miscelaneous definitions * 6 | * Comments: system & compiler dependent file * 7 | ****************************************************************************/ 8 | #if !defined(_PPMDTYPE_H_) 9 | #define _PPMDTYPE_H_ 10 | #define NDEBUG 11 | #include 12 | #include 13 | 14 | //#define _WIN32_ENVIRONMENT_ 15 | //#define _DOS32_ENVIRONMENT_ 16 | //#define _POSIX_ENVIRONMENT_ 17 | //#define _UNKNOWN_ENVIRONMENT_ 18 | #if defined(_WIN32_ENVIRONMENT_)+defined(_DOS32_ENVIRONMENT_)+defined(_POSIX_ENVIRONMENT_)+defined(_UNKNOWN_ENVIRONMENT_) != 1 19 | #error Only one environment must be defined 20 | #endif /* defined(_WIN32_ENVIRONMENT_)+defined(_DOS32_ENVIRONMENT_)+defined(_POSIX_ENVIRONMENT_)+defined(_UNKNOWN_ENVIRONMENT_) != 1 */ 21 | 22 | #if defined(_WIN32_ENVIRONMENT_) 23 | #include 24 | #else /* _DOS32_ENVIRONMENT_ || _POSIX_ENVIRONMENT_ || _UNKNOWN_ENVIRONMENT_ */ 25 | typedef int BOOL; 26 | #define FALSE 0 27 | #define TRUE 1 28 | typedef unsigned char BYTE; // it must be equal to uint8_t 29 | typedef unsigned short WORD; // it must be equal to uint16_t 30 | typedef unsigned int DWORD; // it must be equal to uint32_t 31 | typedef unsigned long QWORD; // it must be equal to uint64_t 32 | typedef unsigned int UINT; 33 | #endif /* defined(_WIN32_ENVIRONMENT_) */ 34 | 35 | #ifdef __x86_64__ 36 | #define _64_NORMAL 37 | #else 38 | #define _32_NORMAL 39 | #endif 40 | 41 | /* Optimal definitions for processors: */ 42 | //#define _32_NORMAL /* IA-32 */ 43 | //#define _64_NORMAL /* AMD64/EM64T */ 44 | //#define _32_EXOTIC /* with request for 32bit alignment for uint32_t */ 45 | //#define _64_EXOTIC /* some unknown to me processors */ 46 | #if defined(_32_NORMAL)+defined(_64_NORMAL)+defined(_32_EXOTIC)+defined(_64_EXOTIC) != 1 47 | #error Only one processor type must be defined 48 | #endif /* defined(_32_NORMAL)+defined(_64_NORMAL)+defined(_32_EXOTIC)+defined(_64_EXOTIC) != 1 */ 49 | 50 | #define _PAD_TO_64(Dummy) 51 | #if defined(_32_NORMAL) || defined(_64_NORMAL) 52 | typedef BYTE _BYTE; 53 | typedef WORD _WORD; 54 | typedef DWORD _DWORD; 55 | #else 56 | #pragma message ("Warning: real memory usage will be twice larger") 57 | typedef WORD _BYTE; 58 | typedef DWORD _WORD; 59 | #if defined(_32_EXOTIC) 60 | typedef DWORD _DWORD; 61 | #undef _PAD_TO_64 62 | #define _PAD_TO_64(Dummy) DWORD Dummy; 63 | #else 64 | typedef QWORD _DWORD; 65 | #endif 66 | #endif /* defined(_32_NORMAL) || defined(_64_NORMAL) */ 67 | 68 | #if !defined(NDEBUG) 69 | BOOL TestCompilation(); /* for testing our data types */ 70 | #endif /* !defined(NDEBUG) */ 71 | 72 | #if !defined(_UNKNOWN_ENVIRONMENT_) && !defined(__GNUC__) 73 | #define _FASTCALL __fastcall 74 | #define _STDCALL __stdcall 75 | #else 76 | #define _FASTCALL 77 | #define _STDCALL 78 | #endif /* !defined(_UNKNOWN_ENVIRONMENT_) && !defined(__GNUC__) */ 79 | 80 | /* _USE_THREAD_KEYWORD macro must be defined at compilation for creation * 81 | * of multithreading applications. Some compilers generate correct code * 82 | * with the use of standard '__thread' keyword (GNU C), some others use it * 83 | * in non-standard way (BorlandC) and some use __declspec(thread) keyword * 84 | * (IntelC, VisualC). */ 85 | //#define _USE_THREAD_KEYWORD 86 | #if defined(_USE_THREAD_KEYWORD) 87 | #if defined(_MSC_VER) 88 | #define _THREAD 89 | #define _THREAD1 __declspec(thread) 90 | #elif defined(__GNUC__) 91 | #define _THREAD 92 | #define _THREAD1 __thread 93 | #else /* __BORLANDC__ */ 94 | #define _THREAD __thread 95 | #define _THREAD1 96 | #endif /* defined(_MSC_VER) */ 97 | #else 98 | #define _THREAD 99 | #define _THREAD1 100 | #endif /* defined(_USE_THREAD_KEYWORD) */ 101 | 102 | const DWORD PPMdSignature=0x84ACAF8F; 103 | enum { PROG_VAR='J', MAX_O=16 }; /* maximum allowed model order */ 104 | #define _USE_PREFETCHING /* it gives 2-6% speed gain */ 105 | 106 | template 107 | inline T CLAMP(const T& X,const T& LoX,const T& HiX) { return (X >= LoX)?((X <= HiX)?(X):(HiX)):(LoX); } 108 | template 109 | inline void SWAP(T& t1,T& t2) { T tmp=t1; t1=t2; t2=tmp; } 110 | 111 | /* PPMd module works with file streams via ...GETC/...PUTC macros only */ 112 | #if 0 113 | typedef FILE _PPMD_FILE; 114 | #define _PPMD_E_GETC(fp) getc(fp) 115 | #define _PPMD_E_PUTC(c,fp) putc((c),fp) 116 | #define _PPMD_D_GETC(fp) getc(fp) 117 | #define _PPMD_D_PUTC(c,fp) putc((c),fp) 118 | #else 119 | //****************** Example of C++ mem stream ************************ 120 | class PRIME_STREAM { 121 | public: 122 | PRIME_STREAM(bool compress, BYTE* buffer, size_t size, FILE* file): compr(compress), Count(size), startCount(size), f(file), p(buffer) {} 123 | int get() 124 | { 125 | if (compr) 126 | return (--Count >= 0)?(*p++):(-1); 127 | else 128 | return getc(f); 129 | } 130 | void put(int c) 131 | { 132 | if (compr) 133 | putc(c, f); 134 | else 135 | (--Count >= 0)?(*p++ = c):(0); 136 | } 137 | bool compr; 138 | int Count, startCount; 139 | FILE* f; 140 | protected: 141 | BYTE *p; 142 | }; 143 | 144 | typedef PRIME_STREAM _PPMD_FILE; 145 | #define _PPMD_E_GETC(pps) (pps)->get() 146 | #define _PPMD_E_PUTC(c,pps) (pps)->put(c) 147 | #define _PPMD_D_GETC(pps) (pps)->get() 148 | #define _PPMD_D_PUTC(c,pps) (pps)->put(c) 149 | //************************** End of example ********************************* 150 | #endif 151 | 152 | #endif /* !defined(_PPMDTYPE_H_) */ 153 | -------------------------------------------------------------------------------- /PPMd/PPMd_read_me.txt: -------------------------------------------------------------------------------- 1 | 1. Short DESCRIPTION. 2 | PPMd program is file-to-file compressor, it is written for embedding in 3 | user programs mainly and it is not intended for immediate use. I was 4 | interested in speed and performance improvements of abstract PPM model [1-6] 5 | only, without tuning it to particular data types, therefore compressor works 6 | good enough for texts, but it is not so good for nonhomogeneous files 7 | (executables) and for noisy analog data (sounds, pictures etc.). Program is 8 | very memory consuming, You can choose balance between execution speed and 9 | memory economy, on one hand, and compression performance, on another hand, 10 | with the help of model order selection option (-o). 11 | Methods of restoration of model correctness at memory insufficiency: 12 | '-r0 - restart model from scratch'. This method is not optimal for any 13 | type of data sources, but it works fast and efficient in average. 14 | '-r1 - cut off model'. This method is optimal for quasistationary sources 15 | when the period of stationarity is much larger than period between cutoffs. 16 | As a rule, it gives better results, but it is slower than '-r0' and it is 17 | unstable against fragmentation of memory heap at high model orders and low 18 | memory. 19 | 20 | 2. Distribution CONTENTS. 21 | read_me.txt - this file; 22 | PPMd.h, PPMdType.h - header files; 23 | Coder.hpp, SubAlloc.hpp, Model.cpp, PPMd.cpp - code sources; 24 | makefile.imk - makefile for Intel C/C++ compiler; 25 | makefile.mak - makefile for Borland C/C++ compiler; 26 | makefile.L64.gmk - makefile for GNU C/C++ compiler under Linux64; 27 | makefile.L64.imk - makefile for Intel C/C++ compiler under Linux64; 28 | 29 | 3. LEGAL stuff. 30 | You can not misattribute authorship on algorithm or code sources, You can 31 | not patent algorithm or its parts, all other things are allowed and welcomed. 32 | Dmitry Subbotin and me have authorship rights on code sources. Dmitry 33 | Subbotin owns authorship rights on his variation of rangecoder algorithm and 34 | I own authorship rights on my variation of PPM algorithm. This variation is 35 | named PPMII (PPM with Information Inheritance). If You use PPMd, our 36 | authorship must be mentioned somewhere in documentation on your program. 37 | PPMonstr program is distributed for experiments and noncommercial use 38 | only. 39 | 40 | 4. DIFFERENCES between variants. 41 | Jun 13, 1999 var.A 42 | Initial release; 43 | Jun 30, 1999 var.B 44 | Arithmetic coder was changed to newer version; 45 | Simplified LOE was tested (Model1.cpp file); 46 | Some small improvements were done; 47 | Aug 22, 1999 var.C 48 | Rudimentary SEE was added; 49 | Some small improvements were done; 50 | Oct 6, 1999 var.D 51 | Inherited probabilities (IPs) were added; 52 | Memory requirements were reduced a bit; 53 | Small improvements were continued; 54 | Dec 3, 1999 var.E 55 | Program name was changed from PPM, escape method D (PPMD) to PPM by 56 | Dmitry (PPMd). Pronounce correctly! ;-) 57 | Bug in ARI_FLUSH_ENCODER was crushed; 58 | Model1.cpp file was removed from package due to LOE gives no gain; 59 | Apr 7, 2000 var.F(inal?) 60 | Michael Schindler`s rangecoder implementation was replaced with 61 | 'carryless rangecoder' by Dmitry Subbotin. Now, PPMd is pure public domain 62 | program; 63 | CC rate - 2.123/2.089bpb; 64 | Nov 26, 2000 var.G(rand final) 65 | Memory requirements were reduced; 66 | CC rate - 2.121/2.056bpb; 67 | Apr 21, 2001 var.H(ard run to final) 68 | Memory requirements were reduced a bit; 69 | CC rate - 2.104/2.041bpb; 70 | Apr 28, 2002 var.I(t is final too) 71 | CutOff and Freeze modes were added; 72 | References to papers were corrected; 73 | CC rate - 2.097/1.963bpb; 74 | Apr 30, 2002 var.I rev.1 75 | One defect in PPMonstr was fixed, this revision of PPMonstr is not 76 | compatible with previous one; 77 | Feb ??, 2006 var.J(ust a final) 78 | Support for 64-bit processors and multi-threading was added; 79 | CC rate - 2.093/1.891bpb; 80 | 81 | 5. REFERENCES. 82 | [1] Excellent introductory review T.Bell, I.H.Witten, J.G.Cleary 83 | 'MODELING FOR TEXT COMPRESSION'. Russian translation is placed at 84 | http://www.compression.ru/download/articles/rev_univ/modeling.rar 85 | [2] Very descriptive M.R.Nelson`s COMP-2 program (PPMd is based on it). 86 | COMP-2 is in http://www.compression.ru/download/articles/ppm/ 87 | nelson_1991_ddj_arithmetic_modeling_html.rar 88 | [3] P.G.Howard PhD thesis 'The Design and Analysis of Efficient Lossless 89 | Data Compression Systems', is available in 90 | http://www.compression.ru/download/articles/ppm/howard_phd_1993_pdf.rar 91 | [4] S.Bunton PhD thesis 'On-Line Stochastic Processes in Data 92 | Compression', is available in 93 | http://www.compression.ru/download/articles/ppm/bunton_phd_1996_pdf.rar 94 | 95 | PPMII algorithm: 96 | [5] D.Shkarin 'Improving the efficiency of PPM algorithm', in Russian, 97 | http://compression.graphicon.ru/download/articles/ppm/shkarin_ppi2001_pdf.rar 98 | [6] D.Shkarin 'PPM: one step to practicality', in English, 99 | http://compression.graphicon.ru/download/articles/ppm/ 100 | shkarin_2002dcc_ppmii_pdf.rar 101 | 102 | AUTHOR SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL, 103 | OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THIS SOFTWARE. YOU USE 104 | THIS PROGRAM AT YOUR OWN RISK. 105 | 106 | Dancy compression! 107 | Dmitry Shkarin 108 | E-mail: dmitry.shkarin@mtu-net.ru 109 | -------------------------------------------------------------------------------- /PPMd/PPMdlib.cpp: -------------------------------------------------------------------------------- 1 | #include "PPMd.h" 2 | #include "PPMdType.h" 3 | #include "PPMdlib.h" 4 | 5 | 6 | void printStatus(int add_read,int add_written,int encoding); 7 | size_t last_ftell = 0; 8 | 9 | void _STDCALL PrintInfo(PRIME_STREAM* DecodedFile,PRIME_STREAM* EncodedFile) 10 | { 11 | #if 1 12 | if (EncodedFile->compr) 13 | printStatus(0, ftell(EncodedFile->f)-last_ftell, EncodedFile->compr); 14 | else 15 | printStatus(ftell(EncodedFile->f)-last_ftell, 0, EncodedFile->compr); 16 | last_ftell = ftell(EncodedFile->f); 17 | #else 18 | char WrkStr[320]; 19 | UINT NDec=(DecodedFile->compr)?(DecodedFile->startCount - DecodedFile->Count):ftell(EncodedFile->f); 20 | NDec += (NDec == 0); 21 | UINT NEnc=(EncodedFile->compr)?ftell(EncodedFile->f):(DecodedFile->startCount - DecodedFile->Count); 22 | UINT n1=(8U*NEnc)/NDec; 23 | UINT n2=(100U*(8U*NEnc-NDec*n1)+NDec/2U)/NDec; 24 | if (n2 == 100) { n1++; n2=0; } 25 | UINT UsedMemory=GetUsedMemory() >> 10; 26 | UINT m1=UsedMemory >> 10; 27 | UINT m2=(10U*(UsedMemory-(m1 << 10))+(1 << 9)) >> 10; 28 | if (m2 == 10) { m1++; m2=0; } 29 | 30 | sprintf(WrkStr,"%8d ->%8d, %1d.%03d bpc, used %dMB", NDec,NEnc,n1,n2,m1); 31 | printf("%-79.79s\r",WrkStr); 32 | #endif 33 | fflush(stdout); 34 | } 35 | 36 | 37 | void PPMDlib_Init(int memSizeInMB) 38 | { 39 | if ( !StartSubAllocator(memSizeInMB) ) { 40 | printf("Not enough memory!\n"); 41 | exit(-1); 42 | } 43 | } 44 | 45 | void PPMDlib_Deinit() 46 | { 47 | StopSubAllocator(); 48 | } 49 | 50 | 51 | 52 | int PPMDlib_EncodeMemToFile(int maxOrder,unsigned char* inBuffer, unsigned int inSize, FILE* outStream) 53 | { 54 | PRIME_STREAM in(true, inBuffer, inSize, NULL); 55 | PRIME_STREAM out(true, NULL, 0, outStream); 56 | 57 | last_ftell = ftell(outStream); 58 | 59 | EncodeFile(&out, &in, maxOrder, FALSE); 60 | 61 | return 1; 62 | }; 63 | 64 | int PPMDlib_DecodeFileToMem(int maxOrder, FILE* inStream, unsigned char* outBuffer, unsigned int outSize) 65 | { 66 | PRIME_STREAM in(false, NULL, 0, inStream); 67 | PRIME_STREAM out(false, outBuffer, outSize, NULL); 68 | 69 | last_ftell = ftell(inStream); 70 | 71 | DecodeFile(&out, &in, maxOrder, FALSE); 72 | 73 | return out.startCount - out.Count; 74 | } 75 | 76 | /* 77 | int main8(int argc, const char **argv) 78 | { 79 | FILE *fpOut; 80 | unsigned char* PPMDmem; 81 | unsigned int PPMDsize=0; 82 | int MaxOrder=8, SASize=32; 83 | 84 | PPMDlib_Init(MaxOrder,SASize); 85 | 86 | 87 | fpOut=fopen("ppmvc_out","wb"); 88 | if (fpOut==NULL) 89 | exit(1); 90 | 91 | unsigned char test[]="\000\000\0001211112345678901234567890123456-78901234567890123456789012345+678901123456789012345678901_2345678901234567890123*45678901234567890112345%6789012345678901234567890123456789$01234567890123456789011234567890123#45678901234567890123456789012345678901234567@89011234567890123456789012345678901234!\000\000\000"; 92 | 93 | PPMDmem=test+3; 94 | PPMDsize=sizeof(test)-6; 95 | 96 | PPMDlib_EncodeMemToFile(PPMDmem,PPMDsize,fpOut); 97 | 98 | unsigned char test2[]="\000\000\00091211112345678901234567890123456-78901234567890123456789012345+678901123456789012345678901_2345678901234567890123*45678901234567890112345%6789012345678901234567890123456789$01234567890123456789011234567890123#45678901234567890123456789012345678901234567@89011234567890123456789012345678901234!\000\000\000"; 99 | 100 | PPMDmem=test2+3; 101 | PPMDsize=sizeof(test2)-6; 102 | 103 | PPMDlib_EncodeMemToFile(PPMDmem,PPMDsize,fpOut); 104 | 105 | fclose(fpOut); 106 | 107 | 108 | 109 | fpOut=fopen("ppmvc_out","rb"); 110 | if (fpOut==NULL) 111 | exit(1); 112 | 113 | unsigned char testd[1024]; 114 | memset(testd,0,sizeof(testd)); 115 | 116 | PPMDmem=testd+3; 117 | PPMDsize=sizeof(testd)-6; 118 | 119 | int size=PPMDlib_DecodeFileToMem(fpOut,PPMDmem,PPMDsize); 120 | 121 | printf("PPMDsize=%d size=%d PPMDmem=%s\n",PPMDsize,size,PPMDmem); 122 | 123 | 124 | memset(testd,0,sizeof(testd)); 125 | PPMDmem=testd+3; 126 | PPMDsize=sizeof(testd)-6; 127 | 128 | size=PPMDlib_DecodeFileToMem(fpOut,PPMDmem,PPMDsize); 129 | 130 | printf("PPMDsize=%d size=%d PPMDmem=%s\n",PPMDsize,size,PPMDmem); 131 | 132 | fclose(fpOut); 133 | 134 | 135 | PPMDlib_Deinit(); 136 | 137 | return 0; 138 | } 139 | */ -------------------------------------------------------------------------------- /PPMd/PPMdlib.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | extern "C" 6 | { 7 | 8 | void PPMDlib_Init(int memSizeInMB); 9 | void PPMDlib_Deinit(); 10 | 11 | // EncodeMemToFile requires allocatef memory from inBuffer-3 to inBuffer+inSize+3 12 | int PPMDlib_EncodeMemToFile(int maxOrder,unsigned char* inBuffer, unsigned int inSize, FILE* outStream); 13 | 14 | // DecodeFileToMem requires allocatef memory from outBuffer-3 to outBuffer+outSize+3 15 | int PPMDlib_DecodeFileToMem(int maxOrder,FILE* inStream,unsigned char* outBuffer,unsigned int outSize); 16 | 17 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Introduction 2 | ----------------- 3 | 4 | XWRT (XML-WRT) is an efficient XML/HTML compressor (actually it works well with all textual files). 5 | It transforms XML to more compressible form and uses zlib (default), LZMA, PPMd, or lpaq6 as 6 | back-end compressor. This idea is based on well-known XML compressor: XMill. 7 | Moreover XML-WRT creates a semi-dynamic dictionary and replaces frequently 8 | used words with shorter codes. There are additional techniques to improve 9 | compression ratio: 10 | - word alphabet can consist of start tags (like ``), urls, e-mails 11 | - special model for numbers encoding 12 | - input XML file is split into containers 13 | - there are special containers for dates, time, pages and fractional numbers 14 | - end tags (``) are replaced with a single char 15 | - end tags + EOL symbols can also be replaced with a single char 16 | - spaceless words model 17 | - very effective methods for white-space preserving 18 | - quotes modeling (`="` and `">` replaced with a single char) 19 | 20 | 21 | Comparision to other XML compressors 22 | ------------------------- 23 | 24 | All files used for comparision can be downloaded from [Wratislavia XML Corpus]. Results are given in bpc (bits ber character). Tested with XWRT 3.1: 25 | 26 | |file |gzip |XMill 0.9 zip|XWRT -l2 (zlib)|LZMA -a1|XWRT -l6 (LZMA)|PPMdJ -o8 -m64|XMill 0.9 PPMd|XMLPPM -l 9|SCMPPM -l 9|XWRT -l9 (PPM)|FastPAQ8 74 MB|XWRT -l11 (FastPAQ8)| 27 | |-----------|-----|-------------|---------------|--------|---------------|--------------|--------------|-----------|-----------|--------------|--------------|--------------------| 28 | |dblp |1.463| 1.250| 0.865| 0.943| 0.747| 0.724| 0.940| 0.802| 0.693| 0.690| 0.659| 0.597| 29 | |enwikibooks|2.339| 2.295| 1.742| 1.686| 1.504| 1.565| 1.838| 1.621| 1.621| 1.481| 1.357| 1.269| 30 | |enwikinews |2.248| 2.198| 1.597| 1.462| 1.301| 1.291| 1.746| 1.379| 1.398| 1.202| 1.172| 1.090| 31 | |lineitem |0.721| 0.380| 0.276| 0.421| 0.243| 0.359| 0.270| 0.261| 0.242| 0.243| 0.236| 0.226| 32 | |Shakespeare|2.182| 2.044| 1.481| 1.646| 1.349| 1.245| 1.584| 1.295| 1.293| 1.204| 1.220| 1.185| 33 | |SwissProt |0.985| 0.619| 0.475| 0.478| 0.388| 0.490| 0.477| 0.416| 0.417| 0.363| 0.395| 0.313| 34 | |uwm |0.553| 0.382| 0.315| 0.368| 0.278| 0.426| 0.310| 0.259| 0.274| 0.240| 0.254| 0.228| 35 | |**average**|**1.499**|**1.310**|**0.964**|**1.001**|**0.830**|**0.871**|**1.024**|**0.862**|**0.848**|**0.775**|**0.756**|**0.701**| 36 | 37 | [Wratislavia XML Corpus]: http://pskibinski.pl/research/Wratislavia/ 38 | 39 | 40 | 41 | Usage 42 | ----------------- 43 | 44 | ``` 45 | Usage: XWRT.exe [options] [file2] [file3] ... 46 | where is a XML file or a XWRT compressed file (it's auto-detected) 47 | you can also use wildcards (e.g., "*.xml") 48 | 49 | GENERAL OPTIONS (which also set default additional options): 50 | -l0 = no compression (memory usage up to 16 MB) 51 | -l1 = zlib fast (memory usage 16+1 MB) 52 | -l2 = zlib normal (default, memory usage 16+1 MB) 53 | -l3 = zlib best (memory usage 16+1 MB) 54 | -l4 = LZMA dict size 64 KB (memory usage 16+9 MB for compression and 16+3 MB for decompression) 55 | -l5 = LZMA dict size 1 MB (memory usage 16+18 MB for compression and 16+3 MB for decompression) 56 | -l6 = LZMA dict size 8 MB (memory usage 16+84 MB for compression and 16+10 MB for decompression) 57 | -l7 = PPMd model size 16 MB (memory usage 16+20 MB) 58 | -l8 = PPMd model size 32 MB (memory usage 16+36 MB) 59 | -l9 = PPMd model size 64 MB (memory usage 16+70 MB) 60 | -l10 = lpaq6 model size 120 MB (memory usage 16+104 MB) 61 | -l11 = lpaq6 model size 214 MB (memory usage 16+198 MB) 62 | -l12 = lpaq6 model size 406 MB (memory usage 16+390 MB) 63 | -l13 = lpaq6 model size 790 MB (memory usage 16+774 MB) 64 | -l14 = lpaq6 model size 1560 MB (memory usage 16+1542 MB) 65 | -0 = preprocessed and uncompressed output optimized for further LZ77 compression 66 | -1 = preprocessed and uncompressed output optimized for further LZMA compression 67 | -2 = preprocessed and uncompressed output optimized for further PPM compression 68 | -3 = preprocessed and uncompressed output optimized for further PAQ compression 69 | 70 | ADDITIONAL OPTIONS: 71 | -bX = Set maximum buffer size while creating dynamic dictionary to X MB 72 | -c = Turn off containers (without number and word containers) 73 | +d = Turn on usage of the static dictionary (requires wrt-eng.dic, 74 | which is available at http://pskibinski.pl/research) 75 | -eX = Set maximum dictionary size to X words 76 | -fX = Set minimal word frequency to X 77 | -i = Delete input files 78 | -mX = Set maximum memory buffer size to X MB (default=8) 79 | -n = Turn off number containers 80 | -o = Force overwrite of output files 81 | -pX = Preprocess only (file_size/X) bytes in a first pass 82 | -s = Turn off spaces modeling option 83 | -t = Turn off "try shorter word" option 84 | -w = Turn off word containers 85 | ``` 86 | 87 | 88 | Compilation 89 | ------------------------- 90 | For Linux/Unix: 91 | ``` 92 | make BUILD_SYSTEM=linux 93 | ``` 94 | 95 | For Windows (MinGW) 96 | ``` 97 | make 98 | ``` 99 | 100 | 101 | Used libraries 102 | --------------------- 103 | ``` 104 | zlib (C) 1995-2005 Jean-loup Gailly and Mark Adler 105 | LZMA (C) 1999-2006 Igor Pavlov 106 | PPMd (C) 1997-2006 Dmitry Shkarin 107 | lpaq6 (C) 2007 Matt Mahoney and Alexander Ratushnyak 108 | ``` 109 | -------------------------------------------------------------------------------- /src/Common.h: -------------------------------------------------------------------------------- 1 | #if !defined common_h_included 2 | #define common_h_included 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "MemBuffer.h" 9 | 10 | 11 | class XWRT_Common 12 | { 13 | public: 14 | XWRT_Common(int fileBufferSize=17); // 128 kb 15 | ~XWRT_Common(); 16 | 17 | int defaultSettings(int argc, char* argv[]); 18 | unsigned int flen( FILE* &f ); 19 | void getAlgName(std::string& compName); 20 | 21 | CContainers cont; 22 | bool deleteInputFiles,WRT_verbose,YesToAll; 23 | EPreprocessType preprocType; 24 | int preprocFlag; 25 | 26 | protected: 27 | void init_PPMD(int fileSizeInMB,int mode); 28 | inline void stringHash(const unsigned char *ptr, int len,int& hash); 29 | int addWord(unsigned char* &mem,int &i); 30 | unsigned char* loadDynamicDictionary(unsigned char* mem,unsigned char* mem_end); 31 | unsigned char* loadDictionary(FILE* file,unsigned char* mem,int word_count); 32 | void loadCharset(FILE* file); 33 | void tryAddSymbol(int c); 34 | void initializeLetterSet(); 35 | void initializeCodeWords(int word_count,bool initMem=true); 36 | bool initialize(unsigned char* dictName,bool encoding); 37 | void WRT_deinitialize(); 38 | 39 | int getSourcePath(char* buf, int buf_size); 40 | void WRT_print_options(); 41 | int minSpacesFreq(); 42 | 43 | int* word_hash; 44 | EPreprocessType codewordType; 45 | EWordType subWordType; 46 | bool decoding,fileCorrupted,detect,firstWarn; 47 | int maxDynDictBuf,minWordFreq,compLevel,maxDictSize,additionalParam,firstPassBlock; 48 | int tryShorterBound,spaces,fileLenMB,beforeWord,PPMDlib_order; 49 | int spacesCodeword[256]; 50 | int spacesCont[256]; 51 | std::vector sortedDict; 52 | 53 | ELetterType letterType; 54 | ELetterType letterSet[256]; 55 | 56 | int sizeDict,sizeDynDict; 57 | unsigned char* dictmem; 58 | unsigned char* dictmem_end; 59 | unsigned char* mem; 60 | 61 | std::vector stack; 62 | 63 | int addSymbols[256]; // reserved symbols in output alphabet 64 | int reservedSet[256]; // reserved symbols in input alphabet 65 | int outputSet[256]; 66 | int reservedFlags[256]; 67 | int wordSet[256]; 68 | int startTagSet[256]; 69 | int urlSet[256]; 70 | int whiteSpaceSet[256]; 71 | int sym2codeword[256]; 72 | int codeword2sym[256]; 73 | int value[256]; 74 | int detectedSymbols[32]; 75 | unsigned char num[16]; 76 | size_t mFileBufferSize; 77 | 78 | int dictionary,dict1size,dict2size,dict3size,dict4size,dict1plus2plus3,dict1plus2; 79 | int bound4,bound3,dict123size,dict12size,collision,quoteOpen,quoteClose,detectedSym; 80 | int maxMemSize; 81 | int sortedDictSize; 82 | std::string compName; 83 | 84 | public: 85 | Encoder* PAQ_encoder; 86 | }; 87 | 88 | size_t fread_fast(unsigned char* dst, int len, FILE* file); 89 | size_t fwrite_fast(unsigned char* dst, int len, FILE* file); 90 | 91 | extern unsigned char** dict; 92 | extern int* dictfreq; 93 | extern unsigned char* dictlen; 94 | 95 | #endif 96 | -------------------------------------------------------------------------------- /src/Decoder.h: -------------------------------------------------------------------------------- 1 | #if !defined decoder_h_included 2 | #define decoder_h_included 3 | 4 | #include "Common.h" 5 | 6 | 7 | class XWRT_Decoder : public XWRT_Common 8 | { 9 | public: 10 | 11 | XWRT_Decoder(); 12 | ~XWRT_Decoder(); 13 | 14 | void WRT_start_decoding(int c,char* filename,char* filenameOut); 15 | 16 | private: 17 | 18 | inline void toUpper(unsigned char* s,int &s_size); 19 | inline unsigned char* uint2string(unsigned int x,unsigned char* buf,int size); 20 | inline void hook_putc(int& c); 21 | void WRT_decode(); 22 | 23 | void read_dict(); 24 | void WRT_set_options(char c,char c2); 25 | inline int decodeCodeWord(unsigned char* &s,int& c); 26 | inline int decodeCodeWord_LZMA(unsigned char* &s,int& c); 27 | inline int decodeCodeWord_LZ(unsigned char* &s,int& c); 28 | 29 | void writePutcBuffer(FILE* &fileout); 30 | 31 | inline unsigned char mask8(unsigned char oc); 32 | inline int sequence_length(unsigned char lead_it); 33 | inline unsigned int utf8_to_unicode(unsigned char* it); 34 | 35 | unsigned char utf8buff[4]; 36 | int utf8pos; 37 | 38 | unsigned char* putcBufferData; 39 | size_t putcBufferSize; 40 | unsigned char* putcBuffer; 41 | unsigned char* zlibBuffer; 42 | 43 | enum EUpperType { UFALSE, UTRUE, FORCE }; 44 | 45 | int s_size,WRTd_c; 46 | int last_c,quotes,lastAll,WRTd_xs_size; 47 | bool WRTd_upper,forceSpace; 48 | bool WRTd_initialized; 49 | unsigned char WRTd_xs[STRING_MAX_SIZE]; 50 | unsigned char WRTd_data[STRING_MAX_SIZE]; 51 | unsigned char *WRTd_s; 52 | EUpperType upperWord; 53 | EXMLState XMLState; 54 | 55 | public: 56 | #ifdef USE_LZMA_LIBRARY 57 | CInFileStream* inStream; 58 | CMyComPtr iStream; 59 | #else 60 | CInFileStream* inStream; 61 | #endif 62 | 63 | }; // end class 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /src/Encoder.h: -------------------------------------------------------------------------------- 1 | #if !defined encoder_h_included 2 | #define encoder_h_included 3 | 4 | #include "Common.h" 5 | 6 | 7 | class XWRT_Encoder : public XWRT_Common 8 | { 9 | public: 10 | 11 | XWRT_Encoder(); 12 | ~XWRT_Encoder(); 13 | 14 | void WRT_start_encoding(unsigned int fileLen,bool type_detected); 15 | 16 | private: 17 | 18 | void WRT_encode(size_t bufferSize); 19 | inline void encodeCodeWord(int &i); 20 | inline void encodeSpaces(); 21 | inline void encodeWord(unsigned char* s,int s_size,EWordType wordType,EXMLState& XMLState,int& c); 22 | inline void encodeCodeWord_PPM(int &i); 23 | inline void encodeCodeWord_LZMA(int &i); 24 | inline void encodeCodeWord_LZ(int &i); 25 | inline void encodeAsText(unsigned char* &s,int &s_size,EWordType wordType); 26 | inline int findShorterWord(unsigned char* &s,int &s_size); 27 | inline void toLower(unsigned char* s,int &s_size); 28 | inline void toUpper(unsigned char* s,int &s_size); 29 | inline void checkWord(unsigned char* &s,int &s_size,EXMLState& XMLState,int& c); 30 | inline void setSpaces(int c); 31 | inline void checkHashExactly(unsigned char* &s,int &s_size,int& i); 32 | inline int checkHash(unsigned char* &s,int &s_size,int h); 33 | inline void stringHash(const unsigned char *ptr, int len,int& hash); 34 | inline int unicode2utf8(unsigned int cp, unsigned char* result); 35 | void encodeMixed(unsigned char* s,int s_size,EXMLState& XMLState,int& c); 36 | void sortDict(int size); 37 | 38 | void write_dict(int comprLevel); 39 | int WRT_detectFileType(); 40 | void WRT_detectFinish(); 41 | void WRT_get_options(int& c,int& c2); 42 | inline void readGetcBuffer(FILE* &file,int &c); 43 | 44 | unsigned char utf8buff[4]; 45 | int utf8cached; 46 | int utf8pos; 47 | 48 | int s_size,binCount; 49 | int last_c_bak,last_c,last_last_c,quotes,lastAll; 50 | EXMLState XMLState; 51 | 52 | unsigned char* getcBufferData; 53 | size_t getcBufferSize; 54 | size_t getcBufferSizeBak; 55 | int getcBufferDataParts; 56 | unsigned char* getcBuffer; 57 | unsigned char* zlibBuffer; 58 | 59 | unsigned char* dynmem; 60 | unsigned char *dictbound; 61 | 62 | public: 63 | #ifdef USE_LZMA_LIBRARY 64 | COutFileStream* outStream; 65 | CMyComPtr oStream; 66 | #else 67 | COutFileStream* outStream; 68 | #endif 69 | 70 | }; // end class 71 | 72 | int compare_freq( const void *arg1, const void *arg2 ); 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /src/LICENSE: -------------------------------------------------------------------------------- 1 | XWRT (XML-WRT) - an efficient XML/HTML/text compressor 2 | is available for use under the following license, commonly known 3 | as the 3-clause (or "modified") BSD license: 4 | 5 | ========================================================================= 6 | Copyright (c) 2006-2007 Przemyslaw Skibinski, inikep@gmail.com 7 | All rights reserved. 8 | 9 | Redistribution and use in source and binary forms, with or without 10 | modification, are permitted provided that the following conditions 11 | are met: 12 | 1. Redistributions of source code must retain the above copyright 13 | notice, this list of conditions and the following disclaimer. 14 | 2. Redistributions in binary form must reproduce the above copyright 15 | notice, this list of conditions and the following disclaimer in the 16 | documentation and/or other materials provided with the distribution. 17 | 3. The name of the author may not be used to endorse or promote products 18 | derived from this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | ========================================================================= 31 | 32 | -------------------------------------------------------------------------------- /src/MemBuffer.h: -------------------------------------------------------------------------------- 1 | #if !defined membuffer_h_included 2 | #define membuffer_h_included 3 | 4 | #pragma warning(disable:4786) 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define OUTPUT_BUFFER_MIN_SIZE 10240 12 | 13 | 14 | // Input/Output using dynamic memory allocation 15 | class CMemoryBuffer 16 | { 17 | public: 18 | CMemoryBuffer(std::string mname=""); 19 | ~CMemoryBuffer(); 20 | 21 | void OutTgtByte( unsigned char c ); 22 | int InpSrcByte( void ); 23 | void UndoSrcByte( void ); 24 | inline int Size(); 25 | inline int Allocated(); 26 | inline void AllocSrcBuf( unsigned int len ); 27 | inline void Clear(); 28 | inline void SetSrcBuf( unsigned char* SrcBuf, unsigned int len ); 29 | 30 | static unsigned int memsize; 31 | unsigned char* TargetBuf; 32 | unsigned char* SourceBuf; 33 | unsigned int SrcLen, TgtLen; 34 | unsigned int SrcPtr, TgtPtr; 35 | std::string name; 36 | 37 | private: 38 | inline void AllocTgtBuf( unsigned int len = OUTPUT_BUFFER_MIN_SIZE ); 39 | inline void ReallocTgtBuf(unsigned int len); 40 | }; 41 | 42 | class CContainers 43 | { 44 | public: 45 | CContainers(); 46 | void prepareMemBuffers(); 47 | void writeMemBuffers(int preprocFlag, int PPMDlib_order, int comprLevel, Encoder* PAQ_encoder, unsigned char* zlibBuffer,COutFileStream* outStream); 48 | void readMemBuffers(int preprocFlag, int maxMemSize, int PPMDlib_order, Encoder* PAQ_encoder, unsigned char* zlibBuffer,CInFileStream* inStream); 49 | void freeMemBuffers(bool freeMem); 50 | void selectMemBuffer(unsigned char* s,int s_size); 51 | void MemBufferPopBack(); 52 | 53 | CMemoryBuffer *memout,*memout_letters,*memout_ip,*memout_hm,*memout_hms; 54 | CMemoryBuffer *memout_words,*memout_words2,*memout_words3,*memout_words4; 55 | CMemoryBuffer *memout_num,*memout_numb,*memout_numc,*memout_num2,*memout_num2b,*memout_num2c,*memout_num3,*memout_num3b,*memout_num3c,*memout_num4,*memout_num4b,*memout_num4c; 56 | CMemoryBuffer *memout_year,*memout_pages,*memout_remain,*memout_date,*memout_date2,*memout_date3,*memout_time,*memout_remain2; 57 | unsigned char *bigBuffer; 58 | 59 | private: 60 | std::vector mem_stack; 61 | std::map memmap; 62 | }; 63 | 64 | #ifdef USE_ZLIB_LIBRARY 65 | 66 | int Zlib_decompress(FILE* file,Byte *compr, uLong comprLen,Byte *uncompr, uLong uncomprLen,int& totalIn); 67 | int Zlib_compress(FILE* fileout,Byte *uncompr, uLong uncomprLen,Byte *compr, uLong comprLen, int comprLevel); 68 | 69 | #endif 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /src/XWRT.h: -------------------------------------------------------------------------------- 1 | #if !defined xwrt_h_included 2 | #define xwrt_h_included 3 | 4 | #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(WIN64) || defined(_WIN64) 5 | #define WINDOWS 6 | #endif 7 | 8 | //#define __STDC_FORMAT_MACROS // now PRIu64 will work 9 | #define _FILE_OFFSET_BITS 64 // turn off_t into a 64-bit type for ftello() and fseeko() 10 | //#include 11 | 12 | #define PRG_NAME "XWRT 3.4 (5.11.2007) - XML compressor by P.Skibinski, inikep@gmail.com" 13 | #define XWRT_VERSION 340 // 150-405 14 | 15 | #define XWRT_HEADER "XWRC" 16 | #define ADD_FILENAME_EXT ".xwrt" 17 | #define CUT_FILENAME_CHAR '.' 18 | 19 | #define USE_ZLIB_LIBRARY 20 | #define USE_LZMA_LIBRARY 21 | #define USE_PPMD_LIBRARY 22 | #define USE_PAQ_LIBRARY 23 | 24 | #define PRINT_CHARS(data) ;//printf data 25 | #define PRINT_CODEWORDS(data) ;//printf data 26 | #define PRINT_STACK(data) ;//printf data 27 | #define PRINT_DICT(data) ;//printf data 28 | #define PRINT_CONTAINERS(data) ;//printf data 29 | #define PRINT_STATUS(data) printf data; 30 | 31 | #pragma warning(disable:4244) // '=' : conversion from ... to ..., possible loss of data 32 | #pragma warning(disable:4786) // STL warnings 33 | #pragma warning(disable:4996) // '_getch' was declared deprecated 34 | #pragma warning(disable:4503) // STL 35 | #pragma warning(disable:4390) // empty controlled statement found; is this the intent? 36 | #pragma warning(disable:4018) // signed/unsigned mismatch 37 | #define _CRT_SECURE_NO_DEPRECATE // VC++ 2005 deprecate warnings 38 | 39 | 40 | 41 | 42 | #ifdef WINDOWS 43 | #define getch _getch 44 | #else 45 | #define getch getchar 46 | #endif 47 | 48 | 49 | #ifdef USE_LZMA_LIBRARY 50 | #include "LZMA/LZMAlib.h" 51 | #else 52 | class CInFileStream { }; 53 | class COutFileStream { }; 54 | #endif 55 | 56 | #ifdef USE_ZLIB_LIBRARY 57 | #include "zlib/zlib.h" 58 | #endif 59 | 60 | #ifdef USE_PPMD_LIBRARY 61 | #include "PPMd/PPMdlib.h" 62 | #endif 63 | 64 | #ifdef USE_PAQ_LIBRARY 65 | #include "lpaq6.h" 66 | #else 67 | class Encoder {}; 68 | #endif 69 | 70 | class Encoder; 71 | 72 | #define CHAR_FIRSTUPPER 1 // for encode lower word with first capital letter 73 | #define CHAR_UPPERWORD 2 // for encode upper word 74 | #define CHAR_NOSPACE 3 75 | #define CHAR_END_TAG 4 76 | #define CHAR_END_TAG_EOL 5 77 | #define CHAR_ESCAPE 6 // for encode reserved chars (CHAR_ESCAPE,CHAR_FIRSTUPPER,...) 78 | #define CHAR_CRLF 7 79 | #define CHAR_NEWWORD 8 80 | #define CHAR_IP 25 81 | #define CHAR_HOURMIN 26 82 | #define CHAR_HOURMINSEC 27 83 | #define CHAR_PAGES 28 84 | #define CHAR_REMAIN 29 85 | #define CHAR_DATE_ENG 30 86 | #define CHAR_TIME 31 87 | #define BINARY_FIRST 128 88 | #define BINARY_LAST 255 89 | #define LETTER_LAST 'E' 90 | 91 | #define OPTION_SPACELESS_WORDS 1 92 | #define OPTION_SPACE_AFTER_CC_FLAG 2 93 | #define OPTION_TRY_SHORTER_WORD 4 94 | #define OPTION_ADD_SYMBOLS_0_5 8 95 | #define OPTION_ADD_SYMBOLS_14_31 16 96 | #define OPTION_ADD_SYMBOLS_MISC 32 97 | #define OPTION_ZLIB 64 98 | #define OPTION_LZMA 128 99 | #define OPTION_PPMD 256 100 | #define OPTION_PAQ 512 101 | #define OPTION_USE_DICTIONARY 1024 102 | #define OPTION_CRLF 2048 103 | #define OPTION_SPACES_MODELING 4096 104 | #define OPTION_QUOTES_MODELING 8192 105 | #define OPTION_USE_CONTAINERS 16384 106 | #define OPTION_LETTER_CONTAINER 32768 107 | #define OPTION_NUMBER_CONTAINER 65536 108 | #define OPTION_BINARY_DATA 131072 109 | #define OPTION_UNICODE_LE 262144 110 | #define OPTION_UNICODE_BE 524288 111 | 112 | 113 | #if !defined MIN 114 | #define MIN(a,b) (((a)>(b))?(b):(a)) 115 | #endif 116 | #define IF_OPTION(option) (preprocFlag & option) 117 | #define OPTION(option) (xml_wrt.preprocFlag & option) 118 | #define TURN_OFF(option) {if (preprocFlag & option) preprocFlag-=option;} 119 | #define TURN_ON(option) {if ((preprocFlag & option)==0) preprocFlag+=option;} 120 | #define RESET_OPTIONS (preprocFlag=0) 121 | template inline T CLAMP(const T& X,const T& LoX,const T& HiX) { return (X >= LoX)?((X <= HiX)?(X):(HiX)):(LoX); } 122 | 123 | #define WORD_MIN_SIZE 2 124 | #define WORD_AVG_SIZE 8 125 | #define WORD_MAX_SIZE 48 126 | #define STRING_MAX_SIZE 255 // 1-byte for container.size() 127 | 128 | #define COMPRESS 1 129 | #define DECOMPRESS 2 130 | 131 | #define MAX_DYNAMIC_DICT_COUNT (65536*256) 132 | #define HASH_TABLE_SIZE (1<<20) //1MB*4 133 | #define ZLIB_BUFFER_SIZE (512*1024) 134 | #define BYTES_TO_DETECT (50*1024) 135 | 136 | #define NUM_BASE 256 137 | #define HASH_DOUBLE_MULT 37 138 | #define HASH_MULT 23 139 | #define CHARSET_COUNT 6 140 | 141 | 142 | enum EWordType { LOWERWORD, FIRSTUPPER, UPPERWORD, VARWORD, NUMBER, NUMBER2, NUMBER3 }; 143 | enum EXMLState { UNKNOWN, OPEN, CLOSE, CLOSE_EOL, CLOSED, ADDED, ADDED2, INSIDE }; 144 | enum EPreprocessType { LZ77, LZMA, PPM, PAQ }; 145 | enum ELetterType { XMLCHAR, LOWERCHAR, UPPERCHAR, UNKNOWNCHAR, RESERVEDCHAR, NUMBERCHAR }; 146 | 147 | 148 | void printStatus(int add_read,int add_written,int encoding); 149 | 150 | #define OUT_OF_MEMORY() \ 151 | { \ 152 | printf("Not enough memory!\n"); \ 153 | exit(0); \ 154 | } 155 | 156 | #ifdef USE_LZMA_LIBRARY 157 | 158 | #define PUTC(c) \ 159 | { \ 160 | if (IF_OPTION(OPTION_LZMA)) \ 161 | LZMAlib_putc(c,outStream); \ 162 | else \ 163 | putc(c,XWRT_fileout); \ 164 | } 165 | #define GETC(c) \ 166 | { \ 167 | if (IF_OPTION(OPTION_LZMA)) \ 168 | c=LZMAlib_getc(inStream); \ 169 | else \ 170 | c=getc(XWRT_file); \ 171 | } 172 | #else 173 | #define PUTC(c) { putc(c,XWRT_fileout); } 174 | #define GETC(c) { c=getc(XWRT_file); } 175 | #endif 176 | 177 | #include 178 | 179 | extern FILE* XWRT_file; 180 | extern FILE* XWRT_fileout; 181 | 182 | #endif 183 | -------------------------------------------------------------------------------- /src/lpaq6.h: -------------------------------------------------------------------------------- 1 | #if !defined lpaq6_h_included 2 | #define lpaq6_h_included 3 | 4 | #pragma warning(disable:4554) 5 | 6 | #define WIKI 7 | 8 | #include 9 | 10 | // 8, 16, 32 bit unsigned types (adjust as appropriate) 11 | typedef unsigned char U8; 12 | typedef unsigned short U16; 13 | typedef unsigned int U32; 14 | 15 | 16 | template 17 | class HashTable { 18 | U8* t; // table: 1 element = B bytes: checksum,priority,data,data,... 19 | const int NB; // size in bytes 20 | public: 21 | HashTable(int n); 22 | U8* get(U32 i); 23 | #ifdef WIKI 24 | U8* get0(U32 i); 25 | U8* get1(U32 i); 26 | #endif 27 | }; 28 | 29 | #define COMPRESS 1 30 | #define DECOMPRESS 2 31 | 32 | class Predictor; 33 | class MatchModel; 34 | 35 | class Encoder { 36 | public: 37 | #ifdef WIKI 38 | HashTable<16> t4a; // cxt -> state 39 | HashTable<16> t4b; // cxt -> state 40 | #define t1a t4a 41 | #define t2a t4a 42 | #define t3a t4a 43 | #define t1b t4b 44 | #define t2b t4b 45 | #define t3b t4b 46 | #else 47 | HashTable<16> t1; // cxt -> state 48 | HashTable<16> t2; // cxt -> state 49 | HashTable<16> t3; // cxt -> state 50 | #define t1a t1 51 | #define t2a t2 52 | #define t3a t3 53 | #define t1b t1 54 | #define t2b t2 55 | #define t3b t3 56 | #endif 57 | MatchModel* mm; // predicts next bit by matching context 58 | FILE *archive; // Compressed data file 59 | U32 x1, x2; // Range, initially [0, 1), scaled by 2^32 60 | U32 x; // Decompress mode: last 4 input bytes of archive 61 | U32 p; 62 | int *add2order; 63 | U32 Predictor_upd(int y); 64 | Encoder(int m, FILE* f); 65 | ~Encoder(); 66 | void flush(); // call this when compression is finished 67 | void compress(int c); 68 | int decompress(); 69 | }; 70 | 71 | void set_PAQ_level(int level); 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /zlib/adler32.c: -------------------------------------------------------------------------------- 1 | /* adler32.c -- compute the Adler-32 checksum of a data stream 2 | * Copyright (C) 1995-2011 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #include "zutil.h" 9 | 10 | #define local static 11 | 12 | local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2)); 13 | 14 | #define BASE 65521 /* largest prime smaller than 65536 */ 15 | #define NMAX 5552 16 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ 17 | 18 | #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} 19 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); 20 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); 21 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); 22 | #define DO16(buf) DO8(buf,0); DO8(buf,8); 23 | 24 | /* use NO_DIVIDE if your processor does not do division in hardware -- 25 | try it both ways to see which is faster */ 26 | #ifdef NO_DIVIDE 27 | /* note that this assumes BASE is 65521, where 65536 % 65521 == 15 28 | (thank you to John Reiser for pointing this out) */ 29 | # define CHOP(a) \ 30 | do { \ 31 | unsigned long tmp = a >> 16; \ 32 | a &= 0xffffUL; \ 33 | a += (tmp << 4) - tmp; \ 34 | } while (0) 35 | # define MOD28(a) \ 36 | do { \ 37 | CHOP(a); \ 38 | if (a >= BASE) a -= BASE; \ 39 | } while (0) 40 | # define MOD(a) \ 41 | do { \ 42 | CHOP(a); \ 43 | MOD28(a); \ 44 | } while (0) 45 | # define MOD63(a) \ 46 | do { /* this assumes a is not negative */ \ 47 | z_off64_t tmp = a >> 32; \ 48 | a &= 0xffffffffL; \ 49 | a += (tmp << 8) - (tmp << 5) + tmp; \ 50 | tmp = a >> 16; \ 51 | a &= 0xffffL; \ 52 | a += (tmp << 4) - tmp; \ 53 | tmp = a >> 16; \ 54 | a &= 0xffffL; \ 55 | a += (tmp << 4) - tmp; \ 56 | if (a >= BASE) a -= BASE; \ 57 | } while (0) 58 | #else 59 | # define MOD(a) a %= BASE 60 | # define MOD28(a) a %= BASE 61 | # define MOD63(a) a %= BASE 62 | #endif 63 | 64 | /* ========================================================================= */ 65 | uLong ZEXPORT adler32(adler, buf, len) 66 | uLong adler; 67 | const Bytef *buf; 68 | uInt len; 69 | { 70 | unsigned long sum2; 71 | unsigned n; 72 | 73 | /* split Adler-32 into component sums */ 74 | sum2 = (adler >> 16) & 0xffff; 75 | adler &= 0xffff; 76 | 77 | /* in case user likes doing a byte at a time, keep it fast */ 78 | if (len == 1) { 79 | adler += buf[0]; 80 | if (adler >= BASE) 81 | adler -= BASE; 82 | sum2 += adler; 83 | if (sum2 >= BASE) 84 | sum2 -= BASE; 85 | return adler | (sum2 << 16); 86 | } 87 | 88 | /* initial Adler-32 value (deferred check for len == 1 speed) */ 89 | if (buf == Z_NULL) 90 | return 1L; 91 | 92 | /* in case short lengths are provided, keep it somewhat fast */ 93 | if (len < 16) { 94 | while (len--) { 95 | adler += *buf++; 96 | sum2 += adler; 97 | } 98 | if (adler >= BASE) 99 | adler -= BASE; 100 | MOD28(sum2); /* only added so many BASE's */ 101 | return adler | (sum2 << 16); 102 | } 103 | 104 | /* do length NMAX blocks -- requires just one modulo operation */ 105 | while (len >= NMAX) { 106 | len -= NMAX; 107 | n = NMAX / 16; /* NMAX is divisible by 16 */ 108 | do { 109 | DO16(buf); /* 16 sums unrolled */ 110 | buf += 16; 111 | } while (--n); 112 | MOD(adler); 113 | MOD(sum2); 114 | } 115 | 116 | /* do remaining bytes (less than NMAX, still just one modulo) */ 117 | if (len) { /* avoid modulos if none remaining */ 118 | while (len >= 16) { 119 | len -= 16; 120 | DO16(buf); 121 | buf += 16; 122 | } 123 | while (len--) { 124 | adler += *buf++; 125 | sum2 += adler; 126 | } 127 | MOD(adler); 128 | MOD(sum2); 129 | } 130 | 131 | /* return recombined sums */ 132 | return adler | (sum2 << 16); 133 | } 134 | 135 | /* ========================================================================= */ 136 | local uLong adler32_combine_(adler1, adler2, len2) 137 | uLong adler1; 138 | uLong adler2; 139 | z_off64_t len2; 140 | { 141 | unsigned long sum1; 142 | unsigned long sum2; 143 | unsigned rem; 144 | 145 | /* for negative len, return invalid adler32 as a clue for debugging */ 146 | if (len2 < 0) 147 | return 0xffffffffUL; 148 | 149 | /* the derivation of this formula is left as an exercise for the reader */ 150 | MOD63(len2); /* assumes len2 >= 0 */ 151 | rem = (unsigned)len2; 152 | sum1 = adler1 & 0xffff; 153 | sum2 = rem * sum1; 154 | MOD(sum2); 155 | sum1 += (adler2 & 0xffff) + BASE - 1; 156 | sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; 157 | if (sum1 >= BASE) sum1 -= BASE; 158 | if (sum1 >= BASE) sum1 -= BASE; 159 | if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1); 160 | if (sum2 >= BASE) sum2 -= BASE; 161 | return sum1 | (sum2 << 16); 162 | } 163 | 164 | /* ========================================================================= */ 165 | uLong ZEXPORT adler32_combine(adler1, adler2, len2) 166 | uLong adler1; 167 | uLong adler2; 168 | z_off_t len2; 169 | { 170 | return adler32_combine_(adler1, adler2, len2); 171 | } 172 | 173 | uLong ZEXPORT adler32_combine64(adler1, adler2, len2) 174 | uLong adler1; 175 | uLong adler2; 176 | z_off64_t len2; 177 | { 178 | return adler32_combine_(adler1, adler2, len2); 179 | } 180 | -------------------------------------------------------------------------------- /zlib/compress.c: -------------------------------------------------------------------------------- 1 | /* compress.c -- compress a memory buffer 2 | * Copyright (C) 1995-2005 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | /* =========================================================================== 12 | Compresses the source buffer into the destination buffer. The level 13 | parameter has the same meaning as in deflateInit. sourceLen is the byte 14 | length of the source buffer. Upon entry, destLen is the total size of the 15 | destination buffer, which must be at least 0.1% larger than sourceLen plus 16 | 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. 17 | 18 | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 19 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, 20 | Z_STREAM_ERROR if the level parameter is invalid. 21 | */ 22 | int ZEXPORT compress2 (dest, destLen, source, sourceLen, level) 23 | Bytef *dest; 24 | uLongf *destLen; 25 | const Bytef *source; 26 | uLong sourceLen; 27 | int level; 28 | { 29 | z_stream stream; 30 | int err; 31 | 32 | stream.next_in = (z_const Bytef *)source; 33 | stream.avail_in = (uInt)sourceLen; 34 | #ifdef MAXSEG_64K 35 | /* Check for source > 64K on 16-bit machine: */ 36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 37 | #endif 38 | stream.next_out = dest; 39 | stream.avail_out = (uInt)*destLen; 40 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 41 | 42 | stream.zalloc = (alloc_func)0; 43 | stream.zfree = (free_func)0; 44 | stream.opaque = (voidpf)0; 45 | 46 | err = deflateInit(&stream, level); 47 | if (err != Z_OK) return err; 48 | 49 | err = deflate(&stream, Z_FINISH); 50 | if (err != Z_STREAM_END) { 51 | deflateEnd(&stream); 52 | return err == Z_OK ? Z_BUF_ERROR : err; 53 | } 54 | *destLen = stream.total_out; 55 | 56 | err = deflateEnd(&stream); 57 | return err; 58 | } 59 | 60 | /* =========================================================================== 61 | */ 62 | int ZEXPORT compress (dest, destLen, source, sourceLen) 63 | Bytef *dest; 64 | uLongf *destLen; 65 | const Bytef *source; 66 | uLong sourceLen; 67 | { 68 | return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); 69 | } 70 | 71 | /* =========================================================================== 72 | If the default memLevel or windowBits for deflateInit() is changed, then 73 | this function needs to be updated. 74 | */ 75 | uLong ZEXPORT compressBound (sourceLen) 76 | uLong sourceLen; 77 | { 78 | return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 79 | (sourceLen >> 25) + 13; 80 | } 81 | -------------------------------------------------------------------------------- /zlib/gzclose.c: -------------------------------------------------------------------------------- 1 | /* gzclose.c -- zlib gzclose() function 2 | * Copyright (C) 2004, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #include "gzguts.h" 7 | 8 | /* gzclose() is in a separate file so that it is linked in only if it is used. 9 | That way the other gzclose functions can be used instead to avoid linking in 10 | unneeded compression or decompression routines. */ 11 | int ZEXPORT gzclose(file) 12 | gzFile file; 13 | { 14 | #ifndef NO_GZCOMPRESS 15 | gz_statep state; 16 | 17 | if (file == NULL) 18 | return Z_STREAM_ERROR; 19 | state = (gz_statep)file; 20 | 21 | return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file); 22 | #else 23 | return gzclose_r(file); 24 | #endif 25 | } 26 | -------------------------------------------------------------------------------- /zlib/gzguts.h: -------------------------------------------------------------------------------- 1 | /* gzguts.h -- zlib internal header definitions for gz* operations 2 | * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #ifdef _LARGEFILE64_SOURCE 7 | # ifndef _LARGEFILE_SOURCE 8 | # define _LARGEFILE_SOURCE 1 9 | # endif 10 | # ifdef _FILE_OFFSET_BITS 11 | # undef _FILE_OFFSET_BITS 12 | # endif 13 | #endif 14 | 15 | #ifdef HAVE_HIDDEN 16 | # define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) 17 | #else 18 | # define ZLIB_INTERNAL 19 | #endif 20 | 21 | #include 22 | #include "zlib.h" 23 | #ifdef STDC 24 | # include 25 | # include 26 | # include 27 | #endif 28 | #include 29 | 30 | #ifdef _WIN32 31 | # include 32 | #endif 33 | 34 | #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) 35 | # include 36 | #endif 37 | 38 | #ifdef WINAPI_FAMILY 39 | # define open _open 40 | # define read _read 41 | # define write _write 42 | # define close _close 43 | #endif 44 | 45 | #ifdef NO_DEFLATE /* for compatibility with old definition */ 46 | # define NO_GZCOMPRESS 47 | #endif 48 | 49 | #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) 50 | # ifndef HAVE_VSNPRINTF 51 | # define HAVE_VSNPRINTF 52 | # endif 53 | #endif 54 | 55 | #if defined(__CYGWIN__) 56 | # ifndef HAVE_VSNPRINTF 57 | # define HAVE_VSNPRINTF 58 | # endif 59 | #endif 60 | 61 | #if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410) 62 | # ifndef HAVE_VSNPRINTF 63 | # define HAVE_VSNPRINTF 64 | # endif 65 | #endif 66 | 67 | #ifndef HAVE_VSNPRINTF 68 | # ifdef MSDOS 69 | /* vsnprintf may exist on some MS-DOS compilers (DJGPP?), 70 | but for now we just assume it doesn't. */ 71 | # define NO_vsnprintf 72 | # endif 73 | # ifdef __TURBOC__ 74 | # define NO_vsnprintf 75 | # endif 76 | # ifdef WIN32 77 | /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ 78 | # if !defined(vsnprintf) && !defined(NO_vsnprintf) 79 | # if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 ) 80 | # define vsnprintf _vsnprintf 81 | # endif 82 | # endif 83 | # endif 84 | # ifdef __SASC 85 | # define NO_vsnprintf 86 | # endif 87 | # ifdef VMS 88 | # define NO_vsnprintf 89 | # endif 90 | # ifdef __OS400__ 91 | # define NO_vsnprintf 92 | # endif 93 | # ifdef __MVS__ 94 | # define NO_vsnprintf 95 | # endif 96 | #endif 97 | 98 | /* unlike snprintf (which is required in C99, yet still not supported by 99 | Microsoft more than a decade later!), _snprintf does not guarantee null 100 | termination of the result -- however this is only used in gzlib.c where 101 | the result is assured to fit in the space provided */ 102 | #ifdef _MSC_VER 103 | # define snprintf _snprintf 104 | #endif 105 | 106 | #ifndef local 107 | # define local static 108 | #endif 109 | /* compile with -Dlocal if your debugger can't find static symbols */ 110 | 111 | /* gz* functions always use library allocation functions */ 112 | #ifndef STDC 113 | extern voidp malloc OF((uInt size)); 114 | extern void free OF((voidpf ptr)); 115 | #endif 116 | 117 | /* get errno and strerror definition */ 118 | #if defined UNDER_CE 119 | # include 120 | # define zstrerror() gz_strwinerror((DWORD)GetLastError()) 121 | #else 122 | # ifndef NO_STRERROR 123 | # include 124 | # define zstrerror() strerror(errno) 125 | # else 126 | # define zstrerror() "stdio error (consult errno)" 127 | # endif 128 | #endif 129 | 130 | /* provide prototypes for these when building zlib without LFS */ 131 | #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 132 | ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); 133 | ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); 134 | ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); 135 | ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); 136 | #endif 137 | 138 | /* default memLevel */ 139 | #if MAX_MEM_LEVEL >= 8 140 | # define DEF_MEM_LEVEL 8 141 | #else 142 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 143 | #endif 144 | 145 | /* default i/o buffer size -- double this for output when reading (this and 146 | twice this must be able to fit in an unsigned type) */ 147 | #define GZBUFSIZE 8192 148 | 149 | /* gzip modes, also provide a little integrity check on the passed structure */ 150 | #define GZ_NONE 0 151 | #define GZ_READ 7247 152 | #define GZ_WRITE 31153 153 | #define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */ 154 | 155 | /* values for gz_state how */ 156 | #define LOOK 0 /* look for a gzip header */ 157 | #define COPY 1 /* copy input directly */ 158 | #define GZIP 2 /* decompress a gzip stream */ 159 | 160 | /* internal gzip file state data structure */ 161 | typedef struct { 162 | /* exposed contents for gzgetc() macro */ 163 | struct gzFile_s x; /* "x" for exposed */ 164 | /* x.have: number of bytes available at x.next */ 165 | /* x.next: next output data to deliver or write */ 166 | /* x.pos: current position in uncompressed data */ 167 | /* used for both reading and writing */ 168 | int mode; /* see gzip modes above */ 169 | int fd; /* file descriptor */ 170 | char *path; /* path or fd for error messages */ 171 | unsigned size; /* buffer size, zero if not allocated yet */ 172 | unsigned want; /* requested buffer size, default is GZBUFSIZE */ 173 | unsigned char *in; /* input buffer */ 174 | unsigned char *out; /* output buffer (double-sized when reading) */ 175 | int direct; /* 0 if processing gzip, 1 if transparent */ 176 | /* just for reading */ 177 | int how; /* 0: get header, 1: copy, 2: decompress */ 178 | z_off64_t start; /* where the gzip data started, for rewinding */ 179 | int eof; /* true if end of input file reached */ 180 | int past; /* true if read requested past end */ 181 | /* just for writing */ 182 | int level; /* compression level */ 183 | int strategy; /* compression strategy */ 184 | /* seek request */ 185 | z_off64_t skip; /* amount to skip (already rewound if backwards) */ 186 | int seek; /* true if seek request pending */ 187 | /* error information */ 188 | int err; /* error code */ 189 | char *msg; /* error message */ 190 | /* zlib inflate or deflate stream */ 191 | z_stream strm; /* stream structure in-place (not a pointer) */ 192 | } gz_state; 193 | typedef gz_state FAR *gz_statep; 194 | 195 | /* shared functions */ 196 | void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *)); 197 | #if defined UNDER_CE 198 | char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error)); 199 | #endif 200 | 201 | /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t 202 | value -- needed when comparing unsigned to z_off64_t, which is signed 203 | (possible z_off64_t types off_t, off64_t, and long are all signed) */ 204 | #ifdef INT_MAX 205 | # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX) 206 | #else 207 | unsigned ZLIB_INTERNAL gz_intmax OF((void)); 208 | # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) 209 | #endif 210 | -------------------------------------------------------------------------------- /zlib/inffast.h: -------------------------------------------------------------------------------- 1 | /* inffast.h -- header to use inffast.c 2 | * Copyright (C) 1995-2003, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start)); 12 | -------------------------------------------------------------------------------- /zlib/inffixed.h: -------------------------------------------------------------------------------- 1 | /* inffixed.h -- table for decoding fixed codes 2 | * Generated automatically by makefixed(). 3 | */ 4 | 5 | /* WARNING: this file should *not* be used by applications. 6 | It is part of the implementation of this library and is 7 | subject to change. Applications should only use zlib.h. 8 | */ 9 | 10 | static const code lenfix[512] = { 11 | {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48}, 12 | {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128}, 13 | {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59}, 14 | {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176}, 15 | {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20}, 16 | {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100}, 17 | {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8}, 18 | {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216}, 19 | {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76}, 20 | {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114}, 21 | {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2}, 22 | {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148}, 23 | {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42}, 24 | {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86}, 25 | {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15}, 26 | {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236}, 27 | {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62}, 28 | {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142}, 29 | {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31}, 30 | {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162}, 31 | {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25}, 32 | {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105}, 33 | {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4}, 34 | {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202}, 35 | {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69}, 36 | {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125}, 37 | {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13}, 38 | {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195}, 39 | {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35}, 40 | {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91}, 41 | {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19}, 42 | {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246}, 43 | {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55}, 44 | {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135}, 45 | {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99}, 46 | {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190}, 47 | {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16}, 48 | {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96}, 49 | {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6}, 50 | {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209}, 51 | {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72}, 52 | {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116}, 53 | {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4}, 54 | {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153}, 55 | {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44}, 56 | {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82}, 57 | {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11}, 58 | {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229}, 59 | {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58}, 60 | {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138}, 61 | {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51}, 62 | {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173}, 63 | {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30}, 64 | {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110}, 65 | {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0}, 66 | {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195}, 67 | {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65}, 68 | {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121}, 69 | {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9}, 70 | {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258}, 71 | {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37}, 72 | {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93}, 73 | {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23}, 74 | {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251}, 75 | {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51}, 76 | {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131}, 77 | {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67}, 78 | {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183}, 79 | {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23}, 80 | {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103}, 81 | {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9}, 82 | {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223}, 83 | {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79}, 84 | {0,9,255} 85 | }; 86 | 87 | static const code distfix[32] = { 88 | {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025}, 89 | {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193}, 90 | {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385}, 91 | {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577}, 92 | {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073}, 93 | {22,5,193},{64,5,0} 94 | }; 95 | -------------------------------------------------------------------------------- /zlib/inflate.h: -------------------------------------------------------------------------------- 1 | /* inflate.h -- internal inflate state definition 2 | * Copyright (C) 1995-2009 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* define NO_GZIP when compiling if you want to disable gzip header and 12 | trailer decoding by inflate(). NO_GZIP would be used to avoid linking in 13 | the crc code when it is not needed. For shared libraries, gzip decoding 14 | should be left enabled. */ 15 | #ifndef NO_GZIP 16 | # define GUNZIP 17 | #endif 18 | 19 | /* Possible inflate modes between inflate() calls */ 20 | typedef enum { 21 | HEAD, /* i: waiting for magic header */ 22 | FLAGS, /* i: waiting for method and flags (gzip) */ 23 | TIME, /* i: waiting for modification time (gzip) */ 24 | OS, /* i: waiting for extra flags and operating system (gzip) */ 25 | EXLEN, /* i: waiting for extra length (gzip) */ 26 | EXTRA, /* i: waiting for extra bytes (gzip) */ 27 | NAME, /* i: waiting for end of file name (gzip) */ 28 | COMMENT, /* i: waiting for end of comment (gzip) */ 29 | HCRC, /* i: waiting for header crc (gzip) */ 30 | DICTID, /* i: waiting for dictionary check value */ 31 | DICT, /* waiting for inflateSetDictionary() call */ 32 | TYPE, /* i: waiting for type bits, including last-flag bit */ 33 | TYPEDO, /* i: same, but skip check to exit inflate on new block */ 34 | STORED, /* i: waiting for stored size (length and complement) */ 35 | COPY_, /* i/o: same as COPY below, but only first time in */ 36 | COPY, /* i/o: waiting for input or output to copy stored block */ 37 | TABLE, /* i: waiting for dynamic block table lengths */ 38 | LENLENS, /* i: waiting for code length code lengths */ 39 | CODELENS, /* i: waiting for length/lit and distance code lengths */ 40 | LEN_, /* i: same as LEN below, but only first time in */ 41 | LEN, /* i: waiting for length/lit/eob code */ 42 | LENEXT, /* i: waiting for length extra bits */ 43 | DIST, /* i: waiting for distance code */ 44 | DISTEXT, /* i: waiting for distance extra bits */ 45 | MATCH, /* o: waiting for output space to copy string */ 46 | LIT, /* o: waiting for output space to write literal */ 47 | CHECK, /* i: waiting for 32-bit check value */ 48 | LENGTH, /* i: waiting for 32-bit length (gzip) */ 49 | DONE, /* finished check, done -- remain here until reset */ 50 | BAD, /* got a data error -- remain here until reset */ 51 | MEM, /* got an inflate() memory error -- remain here until reset */ 52 | SYNC /* looking for synchronization bytes to restart inflate() */ 53 | } inflate_mode; 54 | 55 | /* 56 | State transitions between above modes - 57 | 58 | (most modes can go to BAD or MEM on error -- not shown for clarity) 59 | 60 | Process header: 61 | HEAD -> (gzip) or (zlib) or (raw) 62 | (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT -> 63 | HCRC -> TYPE 64 | (zlib) -> DICTID or TYPE 65 | DICTID -> DICT -> TYPE 66 | (raw) -> TYPEDO 67 | Read deflate blocks: 68 | TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK 69 | STORED -> COPY_ -> COPY -> TYPE 70 | TABLE -> LENLENS -> CODELENS -> LEN_ 71 | LEN_ -> LEN 72 | Read deflate codes in fixed or dynamic block: 73 | LEN -> LENEXT or LIT or TYPE 74 | LENEXT -> DIST -> DISTEXT -> MATCH -> LEN 75 | LIT -> LEN 76 | Process trailer: 77 | CHECK -> LENGTH -> DONE 78 | */ 79 | 80 | /* state maintained between inflate() calls. Approximately 10K bytes. */ 81 | struct inflate_state { 82 | inflate_mode mode; /* current inflate mode */ 83 | int last; /* true if processing last block */ 84 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 85 | int havedict; /* true if dictionary provided */ 86 | int flags; /* gzip header method and flags (0 if zlib) */ 87 | unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ 88 | unsigned long check; /* protected copy of check value */ 89 | unsigned long total; /* protected copy of output count */ 90 | gz_headerp head; /* where to save gzip header information */ 91 | /* sliding window */ 92 | unsigned wbits; /* log base 2 of requested window size */ 93 | unsigned wsize; /* window size or zero if not using window */ 94 | unsigned whave; /* valid bytes in the window */ 95 | unsigned wnext; /* window write index */ 96 | unsigned char FAR *window; /* allocated sliding window, if needed */ 97 | /* bit accumulator */ 98 | unsigned long hold; /* input bit accumulator */ 99 | unsigned bits; /* number of bits in "in" */ 100 | /* for string and stored block copying */ 101 | unsigned length; /* literal or length of data to copy */ 102 | unsigned offset; /* distance back to copy string from */ 103 | /* for table and code decoding */ 104 | unsigned extra; /* extra bits needed */ 105 | /* fixed and dynamic code tables */ 106 | code const FAR *lencode; /* starting table for length/literal codes */ 107 | code const FAR *distcode; /* starting table for distance codes */ 108 | unsigned lenbits; /* index bits for lencode */ 109 | unsigned distbits; /* index bits for distcode */ 110 | /* dynamic table building */ 111 | unsigned ncode; /* number of code length code lengths */ 112 | unsigned nlen; /* number of length code lengths */ 113 | unsigned ndist; /* number of distance code lengths */ 114 | unsigned have; /* number of code lengths in lens[] */ 115 | code FAR *next; /* next available space in codes[] */ 116 | unsigned short lens[320]; /* temporary storage for code lengths */ 117 | unsigned short work[288]; /* work area for code table building */ 118 | code codes[ENOUGH]; /* space for code tables */ 119 | int sane; /* if false, allow invalid distance too far */ 120 | int back; /* bits back of last unprocessed length/lit */ 121 | unsigned was; /* initial length of match */ 122 | }; 123 | -------------------------------------------------------------------------------- /zlib/inftrees.h: -------------------------------------------------------------------------------- 1 | /* inftrees.h -- header to use inftrees.c 2 | * Copyright (C) 1995-2005, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* Structure for decoding tables. Each entry provides either the 12 | information needed to do the operation requested by the code that 13 | indexed that table entry, or it provides a pointer to another 14 | table that indexes more bits of the code. op indicates whether 15 | the entry is a pointer to another table, a literal, a length or 16 | distance, an end-of-block, or an invalid code. For a table 17 | pointer, the low four bits of op is the number of index bits of 18 | that table. For a length or distance, the low four bits of op 19 | is the number of extra bits to get after the code. bits is 20 | the number of bits in this code or part of the code to drop off 21 | of the bit buffer. val is the actual byte to output in the case 22 | of a literal, the base length or distance, or the offset from 23 | the current table to the next table. Each entry is four bytes. */ 24 | typedef struct { 25 | unsigned char op; /* operation, extra bits, table bits */ 26 | unsigned char bits; /* bits in this part of the code */ 27 | unsigned short val; /* offset in table or code value */ 28 | } code; 29 | 30 | /* op values as set by inflate_table(): 31 | 00000000 - literal 32 | 0000tttt - table link, tttt != 0 is the number of table index bits 33 | 0001eeee - length or distance, eeee is the number of extra bits 34 | 01100000 - end of block 35 | 01000000 - invalid code 36 | */ 37 | 38 | /* Maximum size of the dynamic table. The maximum number of code structures is 39 | 1444, which is the sum of 852 for literal/length codes and 592 for distance 40 | codes. These values were found by exhaustive searches using the program 41 | examples/enough.c found in the zlib distribtution. The arguments to that 42 | program are the number of symbols, the initial root table size, and the 43 | maximum bit length of a code. "enough 286 9 15" for literal/length codes 44 | returns returns 852, and "enough 30 6 15" for distance codes returns 592. 45 | The initial root table size (9 or 6) is found in the fifth argument of the 46 | inflate_table() calls in inflate.c and infback.c. If the root table size is 47 | changed, then these maximum sizes would be need to be recalculated and 48 | updated. */ 49 | #define ENOUGH_LENS 852 50 | #define ENOUGH_DISTS 592 51 | #define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS) 52 | 53 | /* Type of code to build for inflate_table() */ 54 | typedef enum { 55 | CODES, 56 | LENS, 57 | DISTS 58 | } codetype; 59 | 60 | int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens, 61 | unsigned codes, code FAR * FAR *table, 62 | unsigned FAR *bits, unsigned short FAR *work)); 63 | -------------------------------------------------------------------------------- /zlib/uncompr.c: -------------------------------------------------------------------------------- 1 | /* uncompr.c -- decompress a memory buffer 2 | * Copyright (C) 1995-2003, 2010 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | /* =========================================================================== 12 | Decompresses the source buffer into the destination buffer. sourceLen is 13 | the byte length of the source buffer. Upon entry, destLen is the total 14 | size of the destination buffer, which must be large enough to hold the 15 | entire uncompressed data. (The size of the uncompressed data must have 16 | been saved previously by the compressor and transmitted to the decompressor 17 | by some mechanism outside the scope of this compression library.) 18 | Upon exit, destLen is the actual size of the compressed buffer. 19 | 20 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not 21 | enough memory, Z_BUF_ERROR if there was not enough room in the output 22 | buffer, or Z_DATA_ERROR if the input data was corrupted. 23 | */ 24 | int ZEXPORT uncompress (dest, destLen, source, sourceLen) 25 | Bytef *dest; 26 | uLongf *destLen; 27 | const Bytef *source; 28 | uLong sourceLen; 29 | { 30 | z_stream stream; 31 | int err; 32 | 33 | stream.next_in = (z_const Bytef *)source; 34 | stream.avail_in = (uInt)sourceLen; 35 | /* Check for source > 64K on 16-bit machine: */ 36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 37 | 38 | stream.next_out = dest; 39 | stream.avail_out = (uInt)*destLen; 40 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 41 | 42 | stream.zalloc = (alloc_func)0; 43 | stream.zfree = (free_func)0; 44 | 45 | err = inflateInit(&stream); 46 | if (err != Z_OK) return err; 47 | 48 | err = inflate(&stream, Z_FINISH); 49 | if (err != Z_STREAM_END) { 50 | inflateEnd(&stream); 51 | if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) 52 | return Z_DATA_ERROR; 53 | return err; 54 | } 55 | *destLen = stream.total_out; 56 | 57 | err = inflateEnd(&stream); 58 | return err; 59 | } 60 | -------------------------------------------------------------------------------- /zlib/zutil.h: -------------------------------------------------------------------------------- 1 | /* zutil.h -- internal interface and configuration of the compression library 2 | * Copyright (C) 1995-2013 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* @(#) $Id$ */ 12 | 13 | #ifndef ZUTIL_H 14 | #define ZUTIL_H 15 | 16 | #ifdef HAVE_HIDDEN 17 | # define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) 18 | #else 19 | # define ZLIB_INTERNAL 20 | #endif 21 | 22 | #include "zlib.h" 23 | 24 | #if defined(STDC) && !defined(Z_SOLO) 25 | # if !(defined(_WIN32_WCE) && defined(_MSC_VER)) 26 | # include 27 | # endif 28 | # include 29 | # include 30 | #endif 31 | 32 | #ifdef Z_SOLO 33 | typedef long ptrdiff_t; /* guess -- will be caught if guess is wrong */ 34 | #endif 35 | 36 | #ifndef local 37 | # define local static 38 | #endif 39 | /* compile with -Dlocal if your debugger can't find static symbols */ 40 | 41 | typedef unsigned char uch; 42 | typedef uch FAR uchf; 43 | typedef unsigned short ush; 44 | typedef ush FAR ushf; 45 | typedef unsigned long ulg; 46 | 47 | extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ 48 | /* (size given to avoid silly warnings with Visual C++) */ 49 | 50 | #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)] 51 | 52 | #define ERR_RETURN(strm,err) \ 53 | return (strm->msg = ERR_MSG(err), (err)) 54 | /* To be used only when the state is known to be valid */ 55 | 56 | /* common constants */ 57 | 58 | #ifndef DEF_WBITS 59 | # define DEF_WBITS MAX_WBITS 60 | #endif 61 | /* default windowBits for decompression. MAX_WBITS is for compression only */ 62 | 63 | #if MAX_MEM_LEVEL >= 8 64 | # define DEF_MEM_LEVEL 8 65 | #else 66 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 67 | #endif 68 | /* default memLevel */ 69 | 70 | #define STORED_BLOCK 0 71 | #define STATIC_TREES 1 72 | #define DYN_TREES 2 73 | /* The three kinds of block type */ 74 | 75 | #define MIN_MATCH 3 76 | #define MAX_MATCH 258 77 | /* The minimum and maximum match lengths */ 78 | 79 | #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ 80 | 81 | /* target dependencies */ 82 | 83 | #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) 84 | # define OS_CODE 0x00 85 | # ifndef Z_SOLO 86 | # if defined(__TURBOC__) || defined(__BORLANDC__) 87 | # if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) 88 | /* Allow compilation with ANSI keywords only enabled */ 89 | void _Cdecl farfree( void *block ); 90 | void *_Cdecl farmalloc( unsigned long nbytes ); 91 | # else 92 | # include 93 | # endif 94 | # else /* MSC or DJGPP */ 95 | # include 96 | # endif 97 | # endif 98 | #endif 99 | 100 | #ifdef AMIGA 101 | # define OS_CODE 0x01 102 | #endif 103 | 104 | #if defined(VAXC) || defined(VMS) 105 | # define OS_CODE 0x02 106 | # define F_OPEN(name, mode) \ 107 | fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") 108 | #endif 109 | 110 | #if defined(ATARI) || defined(atarist) 111 | # define OS_CODE 0x05 112 | #endif 113 | 114 | #ifdef OS2 115 | # define OS_CODE 0x06 116 | # if defined(M_I86) && !defined(Z_SOLO) 117 | # include 118 | # endif 119 | #endif 120 | 121 | #if defined(MACOS) || defined(TARGET_OS_MAC) 122 | # define OS_CODE 0x07 123 | # ifndef Z_SOLO 124 | # if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os 125 | # include /* for fdopen */ 126 | # else 127 | # ifndef fdopen 128 | # define fdopen(fd,mode) NULL /* No fdopen() */ 129 | # endif 130 | # endif 131 | # endif 132 | #endif 133 | 134 | #ifdef TOPS20 135 | # define OS_CODE 0x0a 136 | #endif 137 | 138 | #ifdef WIN32 139 | # ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */ 140 | # define OS_CODE 0x0b 141 | # endif 142 | #endif 143 | 144 | #ifdef __50SERIES /* Prime/PRIMOS */ 145 | # define OS_CODE 0x0f 146 | #endif 147 | 148 | #if defined(_BEOS_) || defined(RISCOS) 149 | # define fdopen(fd,mode) NULL /* No fdopen() */ 150 | #endif 151 | 152 | #if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX 153 | # if defined(_WIN32_WCE) 154 | # define fdopen(fd,mode) NULL /* No fdopen() */ 155 | # ifndef _PTRDIFF_T_DEFINED 156 | typedef int ptrdiff_t; 157 | # define _PTRDIFF_T_DEFINED 158 | # endif 159 | # else 160 | # define fdopen(fd,type) _fdopen(fd,type) 161 | # endif 162 | #endif 163 | 164 | #if defined(__BORLANDC__) && !defined(MSDOS) 165 | #pragma warn -8004 166 | #pragma warn -8008 167 | #pragma warn -8066 168 | #endif 169 | 170 | /* provide prototypes for these when building zlib without LFS */ 171 | #if !defined(_WIN32) && \ 172 | (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0) 173 | ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); 174 | ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); 175 | #endif 176 | 177 | /* common defaults */ 178 | 179 | #ifndef OS_CODE 180 | # define OS_CODE 0x03 /* assume Unix */ 181 | #endif 182 | 183 | #ifndef F_OPEN 184 | # define F_OPEN(name, mode) fopen((name), (mode)) 185 | #endif 186 | 187 | /* functions */ 188 | 189 | #if defined(pyr) || defined(Z_SOLO) 190 | # define NO_MEMCPY 191 | #endif 192 | #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) 193 | /* Use our own functions for small and medium model with MSC <= 5.0. 194 | * You may have to use the same strategy for Borland C (untested). 195 | * The __SC__ check is for Symantec. 196 | */ 197 | # define NO_MEMCPY 198 | #endif 199 | #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) 200 | # define HAVE_MEMCPY 201 | #endif 202 | #ifdef HAVE_MEMCPY 203 | # ifdef SMALL_MEDIUM /* MSDOS small or medium model */ 204 | # define zmemcpy _fmemcpy 205 | # define zmemcmp _fmemcmp 206 | # define zmemzero(dest, len) _fmemset(dest, 0, len) 207 | # else 208 | # define zmemcpy memcpy 209 | # define zmemcmp memcmp 210 | # define zmemzero(dest, len) memset(dest, 0, len) 211 | # endif 212 | #else 213 | void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len)); 214 | int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len)); 215 | void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len)); 216 | #endif 217 | 218 | /* Diagnostic functions */ 219 | #ifdef DEBUG 220 | # include 221 | extern int ZLIB_INTERNAL z_verbose; 222 | extern void ZLIB_INTERNAL z_error OF((char *m)); 223 | # define Assert(cond,msg) {if(!(cond)) z_error(msg);} 224 | # define Trace(x) {if (z_verbose>=0) fprintf x ;} 225 | # define Tracev(x) {if (z_verbose>0) fprintf x ;} 226 | # define Tracevv(x) {if (z_verbose>1) fprintf x ;} 227 | # define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;} 228 | # define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;} 229 | #else 230 | # define Assert(cond,msg) 231 | # define Trace(x) 232 | # define Tracev(x) 233 | # define Tracevv(x) 234 | # define Tracec(c,x) 235 | # define Tracecv(c,x) 236 | #endif 237 | 238 | #ifndef Z_SOLO 239 | voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items, 240 | unsigned size)); 241 | void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr)); 242 | #endif 243 | 244 | #define ZALLOC(strm, items, size) \ 245 | (*((strm)->zalloc))((strm)->opaque, (items), (size)) 246 | #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) 247 | #define TRY_FREE(s, p) {if (p) ZFREE(s, p);} 248 | 249 | /* Reverse the bytes in a 32-bit value */ 250 | #define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ 251 | (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) 252 | 253 | #endif /* ZUTIL_H */ 254 | --------------------------------------------------------------------------------