ruby-lzma 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
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/HC.h ADDED
@@ -0,0 +1,55 @@
1
+ // HC.h
2
+
3
+ #include "LZInWindow.h"
4
+ #include "IMatchFinder.h"
5
+
6
+ namespace HC_NAMESPACE {
7
+
8
+ typedef UInt32 CIndex;
9
+ const UInt32 kMaxValForNormalize = (UInt32(1) << 31) - 1;
10
+
11
+ class CMatchFinderHC:
12
+ public IMatchFinder,
13
+ public IMatchFinderSetCallback,
14
+ public CLZInWindow,
15
+ public CMyUnknownImp
16
+ {
17
+ UInt32 _cyclicBufferPos;
18
+ UInt32 _cyclicBufferSize; // it must be historySize + 1
19
+ UInt32 _matchMaxLen;
20
+ CIndex *_hash;
21
+ UInt32 _cutValue;
22
+
23
+ CMyComPtr<IMatchFinderCallback> m_Callback;
24
+
25
+ void Normalize();
26
+ void FreeThisClassMemory();
27
+ void FreeMemory();
28
+
29
+ MY_UNKNOWN_IMP1(IMatchFinderSetCallback)
30
+
31
+ STDMETHOD(Init)(ISequentialInStream *inStream);
32
+ STDMETHOD_(void, ReleaseStream)();
33
+ STDMETHOD(MovePos)();
34
+ STDMETHOD_(Byte, GetIndexByte)(Int32 index);
35
+ STDMETHOD_(UInt32, GetMatchLen)(Int32 index, UInt32 back, UInt32 limit);
36
+ STDMETHOD_(UInt32, GetNumAvailableBytes)();
37
+ STDMETHOD_(const Byte *, GetPointerToCurrentPos)();
38
+ STDMETHOD(Create)(UInt32 historySize, UInt32 keepAddBufferBefore,
39
+ UInt32 matchMaxLen, UInt32 keepAddBufferAfter);
40
+ STDMETHOD_(UInt32, GetLongestMatch)(UInt32 *distances);
41
+ STDMETHOD_(void, DummyLongestMatch)();
42
+
43
+ // IMatchFinderSetCallback
44
+ STDMETHOD(SetCallback)(IMatchFinderCallback *callback);
45
+
46
+ virtual void BeforeMoveBlock();
47
+ virtual void AfterMoveBlock();
48
+
49
+ public:
50
+ CMatchFinderHC();
51
+ virtual ~CMatchFinderHC();
52
+ void SetCutValue(UInt32 cutValue) { _cutValue = cutValue; }
53
+ };
54
+
55
+ }
data/ext/HC2.h ADDED
@@ -0,0 +1,13 @@
1
+ // HC2.h
2
+
3
+ #ifndef __HC2_H
4
+ #define __HC2_H
5
+
6
+ #undef HC_NAMESPACE
7
+ #define HC_NAMESPACE NHC2
8
+
9
+ #include "HCMF.h"
10
+ #include "HCMFMain.h"
11
+
12
+ #endif
13
+
data/ext/HC3.h ADDED
@@ -0,0 +1,17 @@
1
+ // HC3.h
2
+
3
+ #ifndef __HC3_H
4
+ #define __HC3_H
5
+
6
+ #undef HC_NAMESPACE
7
+ #define HC_NAMESPACE NHC3
8
+
9
+ #define HASH_ARRAY_2
10
+
11
+ #include "HC.h"
12
+ #include "HCMain.h"
13
+
14
+ #undef HASH_ARRAY_2
15
+
16
+ #endif
17
+
data/ext/HC4.h ADDED
@@ -0,0 +1,19 @@
1
+ // HC4.h
2
+
3
+ #ifndef __HC4_H
4
+ #define __HC4_H
5
+
6
+ #undef HC_NAMESPACE
7
+ #define HC_NAMESPACE NHC4
8
+
9
+ #define HASH_ARRAY_2
10
+ #define HASH_ARRAY_3
11
+
12
+ #include "HC.h"
13
+ #include "HCMain.h"
14
+
15
+ #undef HASH_ARRAY_2
16
+ #undef HASH_ARRAY_3
17
+
18
+ #endif
19
+
data/ext/HC4b.h ADDED
@@ -0,0 +1,21 @@
1
+ // HC4b.h
2
+
3
+ #ifndef __HC4B__H
4
+ #define __HC4B__H
5
+
6
+ #undef HC_NAMESPACE
7
+ #define HC_NAMESPACE NHC4b
8
+
9
+ #define HASH_ARRAY_2
10
+ #define HASH_ARRAY_3
11
+ #define HASH_BIG
12
+
13
+ #include "HC.h"
14
+ #include "HCMain.h"
15
+
16
+ #undef HASH_ARRAY_2
17
+ #undef HASH_ARRAY_3
18
+ #undef HASH_BIG
19
+
20
+ #endif
21
+
data/ext/HCMain.h ADDED
@@ -0,0 +1,350 @@
1
+ // HC.h
2
+
3
+ #include "Defs.h"
4
+ #include "CRC.h"
5
+ #include "Alloc.h"
6
+
7
+ namespace HC_NAMESPACE {
8
+
9
+ #ifdef HASH_ARRAY_2
10
+ static const UInt32 kHash2Size = 1 << 10;
11
+ #ifdef HASH_ARRAY_3
12
+ static const UInt32 kNumHashDirectBytes = 0;
13
+ static const UInt32 kNumHashBytes = 4;
14
+ static const UInt32 kHash3Size = 1 << 18;
15
+ #ifdef HASH_BIG
16
+ static const UInt32 kHashSize = 1 << 23;
17
+ #else
18
+ static const UInt32 kHashSize = 1 << 20;
19
+ #endif
20
+ #else
21
+ static const UInt32 kNumHashDirectBytes = 0;
22
+ static const UInt32 kNumHashBytes = 3;
23
+ static const UInt32 kHashSize = 1 << (16);
24
+ #endif
25
+ #else
26
+ #ifdef HASH_ZIP
27
+ static const UInt32 kNumHashDirectBytes = 0;
28
+ static const UInt32 kNumHashBytes = 3;
29
+ static const UInt32 kHashSize = 1 << 16;
30
+ #else
31
+ #define THERE_ARE_DIRECT_HASH_BYTES
32
+ static const UInt32 kNumHashDirectBytes = 2;
33
+ static const UInt32 kNumHashBytes = 2;
34
+ static const UInt32 kHashSize = 1 << (8 * kNumHashBytes);
35
+ #endif
36
+ #endif
37
+
38
+ static const UInt32 kHashSizeSum = kHashSize
39
+ #ifdef HASH_ARRAY_2
40
+ + kHash2Size
41
+ #ifdef HASH_ARRAY_3
42
+ + kHash3Size
43
+ #endif
44
+ #endif
45
+ ;
46
+
47
+ #ifdef HASH_ARRAY_2
48
+ static const UInt32 kHash2Offset = kHashSize;
49
+ #ifdef HASH_ARRAY_3
50
+ static const UInt32 kHash3Offset = kHashSize + kHash2Size;
51
+ #endif
52
+ #endif
53
+
54
+ CMatchFinderHC::CMatchFinderHC():
55
+ _hash(0),
56
+ _cutValue(16)
57
+ {
58
+ }
59
+
60
+ void CMatchFinderHC::FreeThisClassMemory()
61
+ {
62
+ BigFree(_hash);
63
+ _hash = 0;
64
+ }
65
+
66
+ void CMatchFinderHC::FreeMemory()
67
+ {
68
+ FreeThisClassMemory();
69
+ CLZInWindow::Free();
70
+ }
71
+
72
+ CMatchFinderHC::~CMatchFinderHC()
73
+ {
74
+ FreeMemory();
75
+ }
76
+
77
+ STDMETHODIMP CMatchFinderHC::Create(UInt32 historySize, UInt32 keepAddBufferBefore,
78
+ UInt32 matchMaxLen, UInt32 keepAddBufferAfter)
79
+ {
80
+ UInt32 sizeReserv = (historySize + keepAddBufferBefore +
81
+ matchMaxLen + keepAddBufferAfter) / 2 + 256;
82
+ if (CLZInWindow::Create(historySize + keepAddBufferBefore,
83
+ matchMaxLen + keepAddBufferAfter, sizeReserv))
84
+ {
85
+ if (historySize + 256 > kMaxValForNormalize)
86
+ {
87
+ FreeMemory();
88
+ return E_INVALIDARG;
89
+ }
90
+ _matchMaxLen = matchMaxLen;
91
+ UInt32 newCyclicBufferSize = historySize + 1;
92
+ if (_hash != 0 && newCyclicBufferSize == _cyclicBufferSize)
93
+ return S_OK;
94
+ FreeThisClassMemory();
95
+ _cyclicBufferSize = newCyclicBufferSize; // don't change it
96
+ _hash = (CIndex *)BigAlloc((kHashSizeSum + _cyclicBufferSize) * sizeof(CIndex));
97
+ if (_hash != 0)
98
+ return S_OK;
99
+ }
100
+ FreeMemory();
101
+ return E_OUTOFMEMORY;
102
+ }
103
+
104
+ static const UInt32 kEmptyHashValue = 0;
105
+
106
+ STDMETHODIMP CMatchFinderHC::Init(ISequentialInStream *stream)
107
+ {
108
+ RINOK(CLZInWindow::Init(stream));
109
+ for(UInt32 i = 0; i < kHashSizeSum; i++)
110
+ _hash[i] = kEmptyHashValue;
111
+ _cyclicBufferPos = 0;
112
+ ReduceOffsets(-1);
113
+ return S_OK;
114
+ }
115
+
116
+ STDMETHODIMP_(void) CMatchFinderHC::ReleaseStream()
117
+ {
118
+ // ReleaseStream();
119
+ }
120
+
121
+ #ifdef HASH_ARRAY_2
122
+ #ifdef HASH_ARRAY_3
123
+ inline UInt32 Hash(const Byte *pointer, UInt32 &hash2Value, UInt32 &hash3Value)
124
+ {
125
+ UInt32 temp = CCRC::Table[pointer[0]] ^ pointer[1];
126
+ hash2Value = temp & (kHash2Size - 1);
127
+ hash3Value = (temp ^ (UInt32(pointer[2]) << 8)) & (kHash3Size - 1);
128
+ return (temp ^ (UInt32(pointer[2]) << 8) ^ (CCRC::Table[pointer[3]] << 5)) &
129
+ (kHashSize - 1);
130
+ }
131
+ #else // no HASH_ARRAY_3
132
+ inline UInt32 Hash(const Byte *pointer, UInt32 &hash2Value)
133
+ {
134
+ UInt32 temp = CCRC::Table[pointer[0]] ^ pointer[1];
135
+ hash2Value = temp & (kHash2Size - 1);
136
+ return (temp ^ (UInt32(pointer[2]) << 8)) & (kHashSize - 1);;
137
+ }
138
+ #endif // HASH_ARRAY_3
139
+ #else // no HASH_ARRAY_2
140
+ #ifdef HASH_ZIP
141
+ inline UInt32 Hash(const Byte *pointer)
142
+ {
143
+ return ((UInt32(pointer[0]) << 8) ^
144
+ CCRC::Table[pointer[1]] ^ pointer[2]) & (kHashSize - 1);
145
+ }
146
+ #else // no HASH_ZIP
147
+ inline UInt32 Hash(const Byte *pointer)
148
+ {
149
+ return pointer[0] ^ (UInt32(pointer[1]) << 8);
150
+ }
151
+ #endif // HASH_ZIP
152
+ #endif // HASH_ARRAY_2
153
+
154
+
155
+ STDMETHODIMP_(UInt32) CMatchFinderHC::GetLongestMatch(UInt32 *distances)
156
+ {
157
+ UInt32 lenLimit;
158
+ if (_pos + _matchMaxLen <= _streamPos)
159
+ lenLimit = _matchMaxLen;
160
+ else
161
+ {
162
+ lenLimit = _streamPos - _pos;
163
+ if(lenLimit < kNumHashBytes)
164
+ return 0;
165
+ }
166
+
167
+ UInt32 matchMinPos = (_pos > _cyclicBufferSize) ? (_pos - _cyclicBufferSize) : 0;
168
+ Byte *cur = _buffer + _pos;
169
+
170
+ UInt32 maxLen = 0;
171
+
172
+ #ifdef HASH_ARRAY_2
173
+ UInt32 hash2Value;
174
+ #ifdef HASH_ARRAY_3
175
+ UInt32 hash3Value;
176
+ UInt32 hashValue = Hash(cur, hash2Value, hash3Value);
177
+ #else
178
+ UInt32 hashValue = Hash(cur, hash2Value);
179
+ #endif
180
+ #else
181
+ UInt32 hashValue = Hash(cur);
182
+ #endif
183
+ #ifdef HASH_ARRAY_2
184
+
185
+ UInt32 curMatch2 = _hash[kHash2Offset + hash2Value];
186
+ _hash[kHash2Offset + hash2Value] = _pos;
187
+ distances[2] = 0xFFFFFFFF;
188
+ if(curMatch2 > matchMinPos)
189
+ if (_buffer[curMatch2] == cur[0])
190
+ {
191
+ distances[2] = _pos - curMatch2 - 1;
192
+ maxLen = 2;
193
+ }
194
+
195
+ #ifdef HASH_ARRAY_3
196
+
197
+ UInt32 curMatch3 = _hash[kHash3Offset + hash3Value];
198
+ _hash[kHash3Offset + hash3Value] = _pos;
199
+ distances[3] = 0xFFFFFFFF;
200
+ if(curMatch3 > matchMinPos)
201
+ if (_buffer[curMatch3] == cur[0])
202
+ {
203
+ distances[3] = _pos - curMatch3 - 1;
204
+ maxLen = 3;
205
+ }
206
+
207
+ #endif
208
+ #endif
209
+
210
+ UInt32 curMatch = _hash[hashValue];
211
+ _hash[hashValue] = _pos;
212
+ CIndex *chain = _hash + kHashSizeSum;
213
+ chain[_cyclicBufferPos] = curMatch;
214
+ distances[kNumHashBytes] = 0xFFFFFFFF;
215
+ #ifdef THERE_ARE_DIRECT_HASH_BYTES
216
+ if (lenLimit == kNumHashDirectBytes)
217
+ {
218
+ if(curMatch > matchMinPos)
219
+ while (maxLen < kNumHashDirectBytes)
220
+ distances[++maxLen] = _pos - curMatch - 1;
221
+ }
222
+ else
223
+ #endif
224
+ {
225
+ UInt32 count = _cutValue;
226
+ do
227
+ {
228
+ if(curMatch <= matchMinPos)
229
+ break;
230
+ Byte *pby1 = _buffer + curMatch;
231
+ UInt32 currentLen = kNumHashDirectBytes;
232
+ do
233
+ {
234
+ if (pby1[currentLen] != cur[currentLen])
235
+ break;
236
+ }
237
+ while(++currentLen != lenLimit);
238
+
239
+ UInt32 delta = _pos - curMatch;
240
+ while (maxLen < currentLen)
241
+ distances[++maxLen] = delta - 1;
242
+ if(currentLen == lenLimit)
243
+ break;
244
+
245
+ UInt32 cyclicPos = (delta <= _cyclicBufferPos) ?
246
+ (_cyclicBufferPos - delta):
247
+ (_cyclicBufferPos - delta + _cyclicBufferSize);
248
+
249
+ curMatch = chain[cyclicPos];
250
+ }
251
+ while(--count != 0);
252
+ }
253
+ #ifdef HASH_ARRAY_2
254
+ #ifdef HASH_ARRAY_3
255
+ if (distances[4] < distances[3])
256
+ distances[3] = distances[4];
257
+ #endif
258
+ if (distances[3] < distances[2])
259
+ distances[2] = distances[3];
260
+ #endif
261
+ return maxLen;
262
+ }
263
+
264
+ STDMETHODIMP_(void) CMatchFinderHC::DummyLongestMatch()
265
+ {
266
+ if (_streamPos - _pos < kNumHashBytes)
267
+ return;
268
+
269
+ Byte *cur = _buffer + _pos;
270
+
271
+ #ifdef HASH_ARRAY_2
272
+ UInt32 hash2Value;
273
+ #ifdef HASH_ARRAY_3
274
+ UInt32 hash3Value;
275
+ UInt32 hashValue = Hash(cur, hash2Value, hash3Value);
276
+ _hash[kHash3Offset + hash3Value] = _pos;
277
+ #else
278
+ UInt32 hashValue = Hash(cur, hash2Value);
279
+ #endif
280
+ _hash[kHash2Offset + hash2Value] = _pos;
281
+ #else
282
+ UInt32 hashValue = Hash(cur);
283
+ #endif
284
+
285
+ _hash[kHashSizeSum + _cyclicBufferPos] = _hash[hashValue];
286
+ _hash[hashValue] = _pos;
287
+ }
288
+
289
+ void CMatchFinderHC::Normalize()
290
+ {
291
+ UInt32 subValue = _pos - _cyclicBufferSize;
292
+ CIndex *items = _hash;
293
+ UInt32 numItems = kHashSizeSum + _cyclicBufferSize;
294
+ for (UInt32 i = 0; i < numItems; i++)
295
+ {
296
+ UInt32 value = items[i];
297
+ if (value <= subValue)
298
+ value = kEmptyHashValue;
299
+ else
300
+ value -= subValue;
301
+ items[i] = value;
302
+ }
303
+ ReduceOffsets(subValue);
304
+ }
305
+
306
+ STDMETHODIMP CMatchFinderHC::MovePos()
307
+ {
308
+ if (++_cyclicBufferPos == _cyclicBufferSize)
309
+ _cyclicBufferPos = 0;
310
+ RINOK(CLZInWindow::MovePos());
311
+ if (_pos == kMaxValForNormalize)
312
+ Normalize();
313
+ return S_OK;
314
+ }
315
+
316
+ STDMETHODIMP_(Byte) CMatchFinderHC::GetIndexByte(Int32 index)
317
+ { return CLZInWindow::GetIndexByte(index); }
318
+
319
+ STDMETHODIMP_(UInt32) CMatchFinderHC::GetMatchLen(Int32 index,
320
+ UInt32 back, UInt32 limit)
321
+ { return CLZInWindow::GetMatchLen(index, back, limit); }
322
+
323
+ STDMETHODIMP_(UInt32) CMatchFinderHC::GetNumAvailableBytes()
324
+ { return CLZInWindow::GetNumAvailableBytes(); }
325
+
326
+ STDMETHODIMP_(const Byte *) CMatchFinderHC::GetPointerToCurrentPos()
327
+ { return CLZInWindow::GetPointerToCurrentPos(); }
328
+
329
+ // IMatchFinderSetCallback
330
+ STDMETHODIMP CMatchFinderHC::SetCallback(IMatchFinderCallback *callback)
331
+ {
332
+ m_Callback = callback;
333
+ return S_OK;
334
+ }
335
+
336
+ void CMatchFinderHC::BeforeMoveBlock()
337
+ {
338
+ if (m_Callback)
339
+ m_Callback->BeforeChangingBufferPos();
340
+ CLZInWindow::BeforeMoveBlock();
341
+ }
342
+
343
+ void CMatchFinderHC::AfterMoveBlock()
344
+ {
345
+ if (m_Callback)
346
+ m_Callback->AfterChangingBufferPos();
347
+ CLZInWindow::AfterMoveBlock();
348
+ }
349
+
350
+ }