ruby-lzma 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. data/.gitignore +6 -0
  2. data/README.markdown +15 -0
  3. data/Rakefile +53 -0
  4. data/VERSION +1 -0
  5. data/ext/Alloc.cpp +118 -0
  6. data/ext/Alloc.h +29 -0
  7. data/ext/BinTree.h +55 -0
  8. data/ext/BinTree2.h +12 -0
  9. data/ext/BinTree3.h +16 -0
  10. data/ext/BinTree3Z.h +16 -0
  11. data/ext/BinTree4.h +18 -0
  12. data/ext/BinTree4b.h +20 -0
  13. data/ext/BinTreeMain.h +444 -0
  14. data/ext/BranchX86.c +101 -0
  15. data/ext/BranchX86.h +19 -0
  16. data/ext/CRC.cpp +61 -0
  17. data/ext/CRC.h +36 -0
  18. data/ext/C_FileIO.h +45 -0
  19. data/ext/CommandLineParser.h +82 -0
  20. data/ext/Defs.h +20 -0
  21. data/ext/FileStreams.h +98 -0
  22. data/ext/HC.h +55 -0
  23. data/ext/HC2.h +13 -0
  24. data/ext/HC3.h +17 -0
  25. data/ext/HC4.h +19 -0
  26. data/ext/HC4b.h +21 -0
  27. data/ext/HCMain.h +350 -0
  28. data/ext/ICoder.h +156 -0
  29. data/ext/IMatchFinder.h +63 -0
  30. data/ext/IStream.h +62 -0
  31. data/ext/InBuffer.cpp +80 -0
  32. data/ext/InBuffer.h +76 -0
  33. data/ext/LZInWindow.cpp +102 -0
  34. data/ext/LZInWindow.h +84 -0
  35. data/ext/LZMA.h +82 -0
  36. data/ext/LZMADecoder.h +248 -0
  37. data/ext/LZMAEncoder.cpp +1504 -0
  38. data/ext/LZMAEncoder.h +416 -0
  39. data/ext/LZOutWindow.cpp +17 -0
  40. data/ext/LZOutWindow.h +66 -0
  41. data/ext/LzmaBench.h +11 -0
  42. data/ext/LzmaDecode.c +588 -0
  43. data/ext/LzmaDecode.h +131 -0
  44. data/ext/LzmaRam.cpp +228 -0
  45. data/ext/LzmaRam.h +46 -0
  46. data/ext/LzmaRamDecode.c +79 -0
  47. data/ext/LzmaRamDecode.h +55 -0
  48. data/ext/MyCom.h +203 -0
  49. data/ext/MyGuidDef.h +54 -0
  50. data/ext/MyInitGuid.h +13 -0
  51. data/ext/MyString.h +631 -0
  52. data/ext/MyUnknown.h +24 -0
  53. data/ext/MyWindows.h +183 -0
  54. data/ext/OutBuffer.cpp +117 -0
  55. data/ext/OutBuffer.h +64 -0
  56. data/ext/Pat.h +318 -0
  57. data/ext/Pat2.h +22 -0
  58. data/ext/Pat2H.h +24 -0
  59. data/ext/Pat2R.h +20 -0
  60. data/ext/Pat3H.h +24 -0
  61. data/ext/Pat4H.h +24 -0
  62. data/ext/PatMain.h +989 -0
  63. data/ext/RangeCoder.h +205 -0
  64. data/ext/RangeCoderBit.cpp +80 -0
  65. data/ext/RangeCoderBit.h +120 -0
  66. data/ext/RangeCoderBitTree.h +161 -0
  67. data/ext/RangeCoderOpt.h +31 -0
  68. data/ext/StdAfx.h +8 -0
  69. data/ext/StreamUtils.cpp +44 -0
  70. data/ext/StreamUtils.h +11 -0
  71. data/ext/StringConvert.h +71 -0
  72. data/ext/StringToInt.h +17 -0
  73. data/ext/Types.h +19 -0
  74. data/ext/Vector.h +211 -0
  75. data/ext/extconf.rb +7 -0
  76. data/ext/lzma_ruby.cpp +51 -0
  77. data/ext/lzmalib.h +64 -0
  78. data/ext/mylib.cpp +81 -0
  79. data/java/SevenZip/CRC.java +52 -0
  80. data/java/SevenZip/Compression/LZ/BinTree.java +382 -0
  81. data/java/SevenZip/Compression/LZ/InWindow.java +131 -0
  82. data/java/SevenZip/Compression/LZ/OutWindow.java +85 -0
  83. data/java/SevenZip/Compression/LZMA/Base.java +88 -0
  84. data/java/SevenZip/Compression/LZMA/Decoder.java +329 -0
  85. data/java/SevenZip/Compression/LZMA/Encoder.java +1415 -0
  86. data/java/SevenZip/Compression/RangeCoder/BitTreeDecoder.java +55 -0
  87. data/java/SevenZip/Compression/RangeCoder/BitTreeEncoder.java +99 -0
  88. data/java/SevenZip/Compression/RangeCoder/Decoder.java +88 -0
  89. data/java/SevenZip/Compression/RangeCoder/Encoder.java +151 -0
  90. data/java/SevenZip/ICodeProgress.java +6 -0
  91. data/java/SevenZip/LzmaAlone.java +253 -0
  92. data/java/SevenZip/LzmaBench.java +391 -0
  93. data/java/com/ephemeronindustries/lzma/LZMA.java +104 -0
  94. data/lib/lzma.rb +32 -0
  95. data/ruby-lzma.gemspec +136 -0
  96. data/test/test_lzma.rb +42 -0
  97. metadata +157 -0
data/ext/MyUnknown.h ADDED
@@ -0,0 +1,24 @@
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 <basetyps.h>
11
+ #else
12
+ #define MIDL_INTERFACE(x) struct
13
+ #endif
14
+ #else
15
+ #include <basetyps.h>
16
+ #endif
17
+
18
+ #include <unknwn.h>
19
+
20
+ #else
21
+ #include "MyWindows.h"
22
+ #endif
23
+
24
+ #endif
data/ext/MyWindows.h ADDED
@@ -0,0 +1,183 @@
1
+ // MyWindows.h
2
+
3
+ #ifndef __MYWINDOWS_H
4
+ #define __MYWINDOWS_H
5
+
6
+ #ifdef _WIN32
7
+
8
+ #include <windows.h>
9
+
10
+ #else
11
+
12
+ #include <stddef.h> // for wchar_t
13
+ #include <string.h>
14
+
15
+ #include "MyGuidDef.h"
16
+
17
+ typedef char CHAR;
18
+ typedef unsigned char UCHAR;
19
+ typedef unsigned char BYTE;
20
+
21
+ typedef short SHORT;
22
+ typedef unsigned short USHORT;
23
+ typedef unsigned short WORD;
24
+ typedef short VARIANT_BOOL;
25
+
26
+ typedef int INT;
27
+ typedef Int32 INT32;
28
+ typedef unsigned int UINT;
29
+ typedef UInt32 UINT32;
30
+ typedef INT32 LONG; // LONG, ULONG and DWORD must be 32-bit
31
+ typedef UINT32 ULONG;
32
+ typedef UINT32 DWORD;
33
+
34
+ typedef Int64 LONGLONG;
35
+ typedef UInt64 ULONGLONG;
36
+
37
+ typedef struct LARGE_INTEGER { LONGLONG QuadPart; }LARGE_INTEGER;
38
+ typedef struct _ULARGE_INTEGER { ULONGLONG QuadPart;} ULARGE_INTEGER;
39
+
40
+ typedef const CHAR *LPCSTR;
41
+ typedef CHAR TCHAR;
42
+ typedef const TCHAR *LPCTSTR;
43
+ typedef wchar_t WCHAR;
44
+ typedef WCHAR OLECHAR;
45
+ typedef const WCHAR *LPCWSTR;
46
+ typedef OLECHAR *BSTR;
47
+ typedef const OLECHAR *LPCOLESTR;
48
+ typedef OLECHAR *LPOLESTR;
49
+
50
+ typedef struct _FILETIME
51
+ {
52
+ DWORD dwLowDateTime;
53
+ DWORD dwHighDateTime;
54
+ }FILETIME;
55
+
56
+ #define HRESULT LONG
57
+ #define FAILED(Status) ((HRESULT)(Status)<0)
58
+ typedef ULONG PROPID;
59
+ typedef LONG SCODE;
60
+
61
+ #define S_OK ((HRESULT)0x00000000L)
62
+ #define S_FALSE ((HRESULT)0x00000001L)
63
+ #define E_NOINTERFACE ((HRESULT)0x80004002L)
64
+ #define E_ABORT ((HRESULT)0x80004004L)
65
+ #define E_FAIL ((HRESULT)0x80004005L)
66
+ #define STG_E_INVALIDFUNCTION ((HRESULT)0x80030001L)
67
+ #define E_OUTOFMEMORY ((HRESULT)0x8007000EL)
68
+ #define E_INVALIDARG ((HRESULT)0x80070057L)
69
+
70
+ #ifdef _MSC_VER
71
+ #define STDMETHODCALLTYPE __stdcall
72
+ #else
73
+ #define STDMETHODCALLTYPE
74
+ #endif
75
+
76
+ #define STDMETHOD_(t, f) virtual t STDMETHODCALLTYPE f
77
+ #define STDMETHOD(f) STDMETHOD_(HRESULT, f)
78
+ #define STDMETHODIMP_(type) type STDMETHODCALLTYPE
79
+ #define STDMETHODIMP STDMETHODIMP_(HRESULT)
80
+
81
+ #define PURE = 0
82
+
83
+ #define MIDL_INTERFACE(x) struct
84
+
85
+ struct IUnknown
86
+ {
87
+ STDMETHOD(QueryInterface) (REFIID iid, void **outObject) PURE;
88
+ STDMETHOD_(ULONG, AddRef)() PURE;
89
+ STDMETHOD_(ULONG, Release)() PURE;
90
+ };
91
+
92
+ typedef IUnknown *LPUNKNOWN;
93
+
94
+ #define VARIANT_TRUE ((VARIANT_BOOL)-1)
95
+ #define VARIANT_FALSE ((VARIANT_BOOL)0)
96
+
97
+ enum VARENUM
98
+ {
99
+ VT_EMPTY = 0,
100
+ VT_NULL = 1,
101
+ VT_I2 = 2,
102
+ VT_I4 = 3,
103
+ VT_R4 = 4,
104
+ VT_R8 = 5,
105
+ VT_CY = 6,
106
+ VT_DATE = 7,
107
+ VT_BSTR = 8,
108
+ VT_DISPATCH = 9,
109
+ VT_ERROR = 10,
110
+ VT_BOOL = 11,
111
+ VT_VARIANT = 12,
112
+ VT_UNKNOWN = 13,
113
+ VT_DECIMAL = 14,
114
+ VT_I1 = 16,
115
+ VT_UI1 = 17,
116
+ VT_UI2 = 18,
117
+ VT_UI4 = 19,
118
+ VT_I8 = 20,
119
+ VT_UI8 = 21,
120
+ VT_INT = 22,
121
+ VT_UINT = 23,
122
+ VT_VOID = 24,
123
+ VT_HRESULT = 25,
124
+ VT_FILETIME = 64
125
+ };
126
+
127
+ typedef unsigned short VARTYPE;
128
+ typedef WORD PROPVAR_PAD1;
129
+ typedef WORD PROPVAR_PAD2;
130
+ typedef WORD PROPVAR_PAD3;
131
+
132
+ typedef struct tagPROPVARIANT
133
+ {
134
+ VARTYPE vt;
135
+ PROPVAR_PAD1 wReserved1;
136
+ PROPVAR_PAD2 wReserved2;
137
+ PROPVAR_PAD3 wReserved3;
138
+ union
139
+ {
140
+ CHAR cVal;
141
+ UCHAR bVal;
142
+ SHORT iVal;
143
+ USHORT uiVal;
144
+ LONG lVal;
145
+ ULONG ulVal;
146
+ INT intVal;
147
+ UINT uintVal;
148
+ LARGE_INTEGER hVal;
149
+ ULARGE_INTEGER uhVal;
150
+ VARIANT_BOOL boolVal;
151
+ SCODE scode;
152
+ FILETIME filetime;
153
+ BSTR bstrVal;
154
+ };
155
+ } PROPVARIANT;
156
+
157
+ typedef PROPVARIANT tagVARIANT;
158
+ typedef tagVARIANT VARIANT;
159
+ typedef VARIANT VARIANTARG;
160
+
161
+ MY_EXTERN_C BSTR SysAllocStringByteLen(LPCSTR psz, UINT len);
162
+ MY_EXTERN_C BSTR SysAllocString(const OLECHAR *sz);
163
+ MY_EXTERN_C void SysFreeString(BSTR bstr);
164
+ MY_EXTERN_C UINT SysStringByteLen(BSTR bstr);
165
+ MY_EXTERN_C UINT SysStringLen(BSTR bstr);
166
+
167
+ MY_EXTERN_C DWORD GetLastError();
168
+ MY_EXTERN_C HRESULT VariantClear(VARIANTARG *prop);
169
+ MY_EXTERN_C HRESULT VariantCopy(VARIANTARG *dest, VARIANTARG *src);
170
+ MY_EXTERN_C LONG CompareFileTime(const FILETIME* ft1, const FILETIME* ft2);
171
+
172
+ #define CP_ACP 0
173
+ #define CP_OEMCP 1
174
+
175
+ typedef enum tagSTREAM_SEEK
176
+ {
177
+ STREAM_SEEK_SET = 0,
178
+ STREAM_SEEK_CUR = 1,
179
+ STREAM_SEEK_END = 2
180
+ } STREAM_SEEK;
181
+
182
+ #endif
183
+ #endif
data/ext/OutBuffer.cpp ADDED
@@ -0,0 +1,117 @@
1
+ // OutByte.cpp
2
+
3
+ #include "StdAfx.h"
4
+
5
+ #include "OutBuffer.h"
6
+
7
+ #include "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
+ }
data/ext/OutBuffer.h ADDED
@@ -0,0 +1,64 @@
1
+ // OutBuffer.h
2
+
3
+ #ifndef __OUTBUFFER_H
4
+ #define __OUTBUFFER_H
5
+
6
+ #include "IStream.h"
7
+ #include "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<ISequentialOutStream> _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
data/ext/Pat.h ADDED
@@ -0,0 +1,318 @@
1
+ // Pat.h
2
+
3
+ // #ifndef __PATRICIA__H
4
+ // #define __PATRICIA__H
5
+
6
+ #include "MyCom.h"
7
+ #include "Types.h"
8
+ #include "LZInWindow.h"
9
+
10
+ namespace PAT_NAMESPACE {
11
+
12
+ struct CNode;
13
+
14
+ typedef CNode *CNodePointer;
15
+
16
+ // #define __AUTO_REMOVE
17
+
18
+ // #define __NODE_4_BITS
19
+ // #define __NODE_3_BITS
20
+ // #define __NODE_2_BITS
21
+ // #define __NODE_2_BITS_PADDING
22
+
23
+ // #define __HASH_3
24
+
25
+
26
+ typedef UInt32 CIndex;
27
+
28
+ #ifdef __NODE_4_BITS
29
+ typedef UInt32 CIndex2;
30
+ typedef UInt32 CSameBitsType;
31
+ #else
32
+ #ifdef __NODE_3_BITS
33
+ typedef UInt32 CIndex2;
34
+ typedef UInt32 CSameBitsType;
35
+ #else
36
+
37
+ typedef UInt32 CIndex;
38
+ typedef UInt32 CSameBitsType;
39
+
40
+ typedef CIndex CIndex2;
41
+ #endif
42
+ #endif
43
+
44
+ const UInt32 kNumBitsInIndex = sizeof(CIndex) * 8;
45
+ const UInt32 kMatchStartValue = UInt32(1) << (kNumBitsInIndex - 1);
46
+ // don't change kMatchStartValue definition, since it is used in
47
+ // PatMain.h:
48
+
49
+ typedef CIndex CMatchPointer;
50
+
51
+ const UInt32 kDescendantEmptyValue = kMatchStartValue - 1;
52
+
53
+ union CDescendant
54
+ {
55
+ CIndex NodePointer;
56
+ CMatchPointer MatchPointer;
57
+ bool IsEmpty() const { return NodePointer == kDescendantEmptyValue; }
58
+ bool IsNode() const { return NodePointer < kDescendantEmptyValue; }
59
+ bool IsMatch() const { return NodePointer > kDescendantEmptyValue; }
60
+ void MakeEmpty() { NodePointer = kDescendantEmptyValue; }
61
+ };
62
+
63
+ #undef MY_BYTE_SIZE
64
+
65
+ #ifdef __NODE_4_BITS
66
+ #define MY_BYTE_SIZE 8
67
+ const UInt32 kNumSubBits = 4;
68
+ #else
69
+ #ifdef __NODE_3_BITS
70
+ #define MY_BYTE_SIZE 9
71
+ const UInt32 kNumSubBits = 3;
72
+ #else
73
+ #define MY_BYTE_SIZE 8
74
+ #ifdef __NODE_2_BITS
75
+ const UInt32 kNumSubBits = 2;
76
+ #else
77
+ const UInt32 kNumSubBits = 1;
78
+ #endif
79
+ #endif
80
+ #endif
81
+
82
+ const UInt32 kNumSubNodes = 1 << kNumSubBits;
83
+ const UInt32 kSubNodesMask = kNumSubNodes - 1;
84
+
85
+ struct CNode
86
+ {
87
+ CIndex2 LastMatch;
88
+ CSameBitsType NumSameBits;
89
+ union
90
+ {
91
+ CDescendant Descendants[kNumSubNodes];
92
+ UInt32 NextFreeNode;
93
+ };
94
+ #ifdef __NODE_2_BITS
95
+ #ifdef __NODE_2_BITS_PADDING
96
+ UInt32 Padding[2];
97
+ #endif
98
+ #endif
99
+ };
100
+
101
+ #undef kIDNumBitsByte
102
+ #undef kIDNumBitsString
103
+
104
+ #ifdef __NODE_4_BITS
105
+ #define kIDNumBitsByte 0x30
106
+ #define kIDNumBitsString TEXT("4")
107
+ #else
108
+ #ifdef __NODE_3_BITS
109
+ #define kIDNumBitsByte 0x20
110
+ #define kIDNumBitsString TEXT("3")
111
+ #else
112
+ #ifdef __NODE_2_BITS
113
+ #define kIDNumBitsByte 0x10
114
+ #define kIDNumBitsString TEXT("2")
115
+ #else
116
+ #define kIDNumBitsByte 0x00
117
+ #define kIDNumBitsString TEXT("1")
118
+ #endif
119
+ #endif
120
+ #endif
121
+
122
+ #undef kIDManualRemoveByte
123
+ #undef kIDManualRemoveString
124
+
125
+ #ifdef __AUTO_REMOVE
126
+ #define kIDManualRemoveByte 0x00
127
+ #define kIDManualRemoveString TEXT("")
128
+ #else
129
+ #define kIDManualRemoveByte 0x08
130
+ #define kIDManualRemoveString TEXT("R")
131
+ #endif
132
+
133
+ #undef kIDHash3Byte
134
+ #undef kIDHash3String
135
+
136
+ #ifdef __HASH_3
137
+ #define kIDHash3Byte 0x04
138
+ #define kIDHash3String TEXT("H")
139
+ #else
140
+ #define kIDHash3Byte 0x00
141
+ #define kIDHash3String TEXT("")
142
+ #endif
143
+
144
+ #undef kIDUse3BytesByte
145
+ #undef kIDUse3BytesString
146
+
147
+ #define kIDUse3BytesByte 0x00
148
+ #define kIDUse3BytesString TEXT("")
149
+
150
+ #undef kIDPaddingByte
151
+ #undef kIDPaddingString
152
+
153
+ #ifdef __NODE_2_BITS_PADDING
154
+ #define kIDPaddingByte 0x01
155
+ #define kIDPaddingString TEXT("P")
156
+ #else
157
+ #define kIDPaddingByte 0x00
158
+ #define kIDPaddingString TEXT("")
159
+ #endif
160
+
161
+
162
+ // #undef kIDString
163
+ // #define kIDString TEXT("Compress.MatchFinderPat") kIDNumBitsString kIDManualRemoveString kIDUse3BytesString kIDPaddingString kIDHash3String
164
+
165
+ // {23170F69-40C1-278C-01XX-0000000000}
166
+
167
+ DEFINE_GUID(PAT_CLSID,
168
+ 0x23170F69, 0x40C1, 0x278C, 0x01,
169
+ kIDNumBitsByte |
170
+ kIDManualRemoveByte | kIDHash3Byte | kIDUse3BytesByte | kIDPaddingByte,
171
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
172
+
173
+ // III(PAT_NAMESPACE)
174
+
175
+ class CPatricia:
176
+ public IMatchFinder,
177
+ public IMatchFinderSetCallback,
178
+ public CMyUnknownImp,
179
+ CLZInWindow
180
+ {
181
+ MY_UNKNOWN_IMP1(IMatchFinderSetCallback)
182
+
183
+ STDMETHOD(Init)(ISequentialInStream *aStream);
184
+ STDMETHOD_(void, ReleaseStream)();
185
+ STDMETHOD(MovePos)();
186
+ STDMETHOD_(Byte, GetIndexByte)(Int32 index);
187
+ STDMETHOD_(UInt32, GetMatchLen)(Int32 index, UInt32 back, UInt32 limit);
188
+ STDMETHOD_(UInt32, GetNumAvailableBytes)();
189
+ STDMETHOD(Create)(UInt32 historySize,
190
+ UInt32 keepAddBufferBefore, UInt32 matchMaxLen,
191
+ UInt32 keepAddBufferAfter);
192
+ STDMETHOD_(UInt32, GetLongestMatch)(UInt32 *distances);
193
+ STDMETHOD_(void, DummyLongestMatch)();
194
+ STDMETHOD_(const Byte *, GetPointerToCurrentPos)();
195
+
196
+ void FreeMemory();
197
+ public:
198
+ CPatricia();
199
+ ~CPatricia();
200
+
201
+ UInt32 _sizeHistory;
202
+ UInt32 _matchMaxLen;
203
+
204
+ CDescendant *m_HashDescendants;
205
+ #ifdef __HASH_3
206
+ CDescendant *m_Hash2Descendants;
207
+ #endif
208
+
209
+ CNode *m_Nodes;
210
+
211
+ UInt32 m_FreeNode;
212
+ UInt32 m_FreeNodeMax;
213
+
214
+ #ifdef __AUTO_REMOVE
215
+ UInt32 m_NumUsedNodes;
216
+ UInt32 m_NumNodes;
217
+ #else
218
+ bool m_SpecialRemoveMode;
219
+ #endif
220
+
221
+ bool m_SpecialMode;
222
+ UInt32 m_NumNotChangedCycles;
223
+ UInt32 *m_TmpBacks;
224
+
225
+ CMyComPtr<IMatchFinderCallback> m_Callback;
226
+
227
+ virtual void BeforeMoveBlock();
228
+ virtual void AfterMoveBlock();
229
+
230
+ // IMatchFinderSetCallback
231
+ STDMETHOD(SetCallback)(IMatchFinderCallback *callback);
232
+
233
+ void ChangeLastMatch(UInt32 hashValue);
234
+
235
+ #ifdef __AUTO_REMOVE
236
+ void TestRemoveDescendant(CDescendant &descendant, UInt32 limitPos);
237
+ void TestRemoveNodes();
238
+ void RemoveNode(UInt32 index);
239
+ void TestRemoveAndNormalizeDescendant(CDescendant &descendant,
240
+ UInt32 limitPos, UInt32 subValue);
241
+ void TestRemoveNodesAndNormalize();
242
+ #else
243
+ void NormalizeDescendant(CDescendant &descendant, UInt32 subValue);
244
+ void Normalize();
245
+ void RemoveMatch();
246
+ #endif
247
+ private:
248
+ void AddInternalNode(CNodePointer aNode, CIndex *aNodePointerPointer,
249
+ Byte aByte, Byte aByteXOR, UInt32 aNumSameBits, UInt32 aPos)
250
+ {
251
+ while((aByteXOR & kSubNodesMask) == 0)
252
+ {
253
+ aByteXOR >>= kNumSubBits;
254
+ aByte >>= kNumSubBits;
255
+ aNumSameBits -= kNumSubBits;
256
+ }
257
+ // Insert New Node
258
+ CNodePointer aNewNode = &m_Nodes[m_FreeNode];
259
+ UInt32 aNodeIndex = *aNodePointerPointer;
260
+ *aNodePointerPointer = m_FreeNode;
261
+ m_FreeNode = aNewNode->NextFreeNode;
262
+ #ifdef __AUTO_REMOVE
263
+ m_NumUsedNodes++;
264
+ #endif
265
+ if (m_FreeNode > m_FreeNodeMax)
266
+ {
267
+ m_FreeNodeMax = m_FreeNode;
268
+ m_Nodes[m_FreeNode].NextFreeNode = m_FreeNode + 1;
269
+ }
270
+
271
+ UInt32 aBitsNew = aByte & kSubNodesMask;
272
+ UInt32 aBitsOld = (aByte ^ aByteXOR) & kSubNodesMask;
273
+ for (UInt32 i = 0; i < kNumSubNodes; i++)
274
+ aNewNode->Descendants[i].NodePointer = kDescendantEmptyValue;
275
+ aNewNode->Descendants[aBitsNew].MatchPointer = aPos + kMatchStartValue;
276
+ aNewNode->Descendants[aBitsOld].NodePointer = aNodeIndex;
277
+ aNewNode->NumSameBits = CSameBitsType(aNode->NumSameBits - aNumSameBits);
278
+ aNewNode->LastMatch = aPos;
279
+
280
+ aNode->NumSameBits = CSameBitsType(aNumSameBits - kNumSubBits);
281
+ }
282
+
283
+ void AddLeafNode(CNodePointer aNode, Byte aByte, Byte aByteXOR,
284
+ UInt32 aNumSameBits, UInt32 aPos, UInt32 aDescendantIndex)
285
+ {
286
+ for(;(aByteXOR & kSubNodesMask) == 0; aNumSameBits += kNumSubBits)
287
+ {
288
+ aByte >>= kNumSubBits;
289
+ aByteXOR >>= kNumSubBits;
290
+ }
291
+ UInt32 aNewNodeIndex = m_FreeNode;
292
+ CNodePointer aNewNode = &m_Nodes[m_FreeNode];
293
+ m_FreeNode = aNewNode->NextFreeNode;
294
+ #ifdef __AUTO_REMOVE
295
+ m_NumUsedNodes++;
296
+ #endif
297
+ if (m_FreeNode > m_FreeNodeMax)
298
+ {
299
+ m_FreeNodeMax = m_FreeNode;
300
+ m_Nodes[m_FreeNode].NextFreeNode = m_FreeNode + 1;
301
+ }
302
+
303
+ UInt32 aBitsNew = (aByte & kSubNodesMask);
304
+ UInt32 aBitsOld = (aByte ^ aByteXOR) & kSubNodesMask;
305
+ for (UInt32 i = 0; i < kNumSubNodes; i++)
306
+ aNewNode->Descendants[i].NodePointer = kDescendantEmptyValue;
307
+ aNewNode->Descendants[aBitsNew].MatchPointer = aPos + kMatchStartValue;
308
+ aNewNode->Descendants[aBitsOld].MatchPointer =
309
+ aNode->Descendants[aDescendantIndex].MatchPointer;
310
+ aNewNode->NumSameBits = CSameBitsType(aNumSameBits);
311
+ aNewNode->LastMatch = aPos;
312
+ aNode->Descendants[aDescendantIndex].NodePointer = aNewNodeIndex;
313
+ }
314
+ };
315
+
316
+ }
317
+
318
+ // #endif