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
@@ -0,0 +1,391 @@
1
+ package SevenZip;
2
+
3
+ import java.io.ByteArrayOutputStream;
4
+ import java.io.IOException;
5
+
6
+ public class LzmaBench
7
+ {
8
+ static final int kAdditionalSize = (1 << 21);
9
+ static final int kCompressedAdditionalSize = (1 << 10);
10
+
11
+ static class CRandomGenerator
12
+ {
13
+ int A1;
14
+ int A2;
15
+ public CRandomGenerator() { Init(); }
16
+ public void Init() { A1 = 362436069; A2 = 521288629; }
17
+ public int GetRnd()
18
+ {
19
+ return
20
+ ((A1 = 36969 * (A1 & 0xffff) + (A1 >>> 16)) << 16) ^
21
+ ((A2 = 18000 * (A2 & 0xffff) + (A2 >>> 16)));
22
+ }
23
+ };
24
+
25
+ static class CBitRandomGenerator
26
+ {
27
+ CRandomGenerator RG = new CRandomGenerator();
28
+ int Value;
29
+ int NumBits;
30
+ public void Init()
31
+ {
32
+ Value = 0;
33
+ NumBits = 0;
34
+ }
35
+ public int GetRnd(int numBits)
36
+ {
37
+ int result;
38
+ if (NumBits > numBits)
39
+ {
40
+ result = Value & ((1 << numBits) - 1);
41
+ Value >>>= numBits;
42
+ NumBits -= numBits;
43
+ return result;
44
+ }
45
+ numBits -= NumBits;
46
+ result = (Value << numBits);
47
+ Value = RG.GetRnd();
48
+ result |= Value & (((int)1 << numBits) - 1);
49
+ Value >>>= numBits;
50
+ NumBits = 32 - numBits;
51
+ return result;
52
+ }
53
+ };
54
+
55
+ static class CBenchRandomGenerator
56
+ {
57
+ CBitRandomGenerator RG = new CBitRandomGenerator();
58
+ int Pos;
59
+ int Rep0;
60
+
61
+ public int BufferSize;
62
+ public byte[] Buffer = null;
63
+
64
+ public CBenchRandomGenerator() { }
65
+ public void Set(int bufferSize)
66
+ {
67
+ Buffer = new byte[bufferSize];
68
+ Pos = 0;
69
+ BufferSize = bufferSize;
70
+ }
71
+ int GetRndBit() { return RG.GetRnd(1); }
72
+ int GetLogRandBits(int numBits)
73
+ {
74
+ int len = RG.GetRnd(numBits);
75
+ return RG.GetRnd((int)len);
76
+ }
77
+ int GetOffset()
78
+ {
79
+ if (GetRndBit() == 0)
80
+ return GetLogRandBits(4);
81
+ return (GetLogRandBits(4) << 10) | RG.GetRnd(10);
82
+ }
83
+ int GetLen1() { return RG.GetRnd(1 + (int)RG.GetRnd(2)); }
84
+ int GetLen2() { return RG.GetRnd(2 + (int)RG.GetRnd(2)); }
85
+ public void Generate()
86
+ {
87
+ RG.Init();
88
+ Rep0 = 1;
89
+ while (Pos < BufferSize)
90
+ {
91
+ if (GetRndBit() == 0 || Pos < 1)
92
+ Buffer[Pos++] = (byte)(RG.GetRnd(8));
93
+ else
94
+ {
95
+ int len;
96
+ if (RG.GetRnd(3) == 0)
97
+ len = 1 + GetLen1();
98
+ else
99
+ {
100
+ do
101
+ Rep0 = GetOffset();
102
+ while (Rep0 >= Pos);
103
+ Rep0++;
104
+ len = 2 + GetLen2();
105
+ }
106
+ for (int i = 0; i < len && Pos < BufferSize; i++, Pos++)
107
+ Buffer[Pos] = Buffer[Pos - Rep0];
108
+ }
109
+ }
110
+ }
111
+ };
112
+
113
+ static class CrcOutStream extends java.io.OutputStream
114
+ {
115
+ public CRC CRC = new CRC();
116
+
117
+ public void Init()
118
+ {
119
+ CRC.Init();
120
+ }
121
+ public int GetDigest()
122
+ {
123
+ return CRC.GetDigest();
124
+ }
125
+ public void write(byte[] b)
126
+ {
127
+ CRC.Update(b);
128
+ }
129
+ public void write(byte[] b, int off, int len)
130
+ {
131
+ CRC.Update(b, off, len);
132
+ }
133
+ public void write(int b)
134
+ {
135
+ CRC.UpdateByte(b);
136
+ }
137
+ };
138
+
139
+ static class MyOutputStream extends java.io.OutputStream
140
+ {
141
+ byte[] _buffer;
142
+ int _size;
143
+ int _pos;
144
+
145
+ public MyOutputStream(byte[] buffer)
146
+ {
147
+ _buffer = buffer;
148
+ _size = _buffer.length;
149
+ }
150
+
151
+ public void reset()
152
+ {
153
+ _pos = 0;
154
+ }
155
+
156
+ public void write(int b) throws IOException
157
+ {
158
+ if (_pos >= _size)
159
+ throw new IOException("Error");
160
+ _buffer[_pos++] = (byte)b;
161
+ }
162
+
163
+ public int size()
164
+ {
165
+ return _pos;
166
+ }
167
+ };
168
+
169
+ static class MyInputStream extends java.io.InputStream
170
+ {
171
+ byte[] _buffer;
172
+ int _size;
173
+ int _pos;
174
+
175
+ public MyInputStream(byte[] buffer, int size)
176
+ {
177
+ _buffer = buffer;
178
+ _size = size;
179
+ }
180
+
181
+ public void reset()
182
+ {
183
+ _pos = 0;
184
+ }
185
+
186
+ public int read()
187
+ {
188
+ if (_pos >= _size)
189
+ return -1;
190
+ return _buffer[_pos++] & 0xFF;
191
+ }
192
+ };
193
+
194
+ static class CProgressInfo implements ICodeProgress
195
+ {
196
+ public long ApprovedStart;
197
+ public long InSize;
198
+ public long Time;
199
+ public void Init()
200
+ { InSize = 0; }
201
+ public void SetProgress(long inSize, long outSize)
202
+ {
203
+ if (inSize >= ApprovedStart && InSize == 0)
204
+ {
205
+ Time = System.currentTimeMillis();
206
+ InSize = inSize;
207
+ }
208
+ }
209
+ }
210
+ static final int kSubBits = 8;
211
+
212
+ static int GetLogSize(int size)
213
+ {
214
+ for (int i = kSubBits; i < 32; i++)
215
+ for (int j = 0; j < (1 << kSubBits); j++)
216
+ if (size <= ((1) << i) + (j << (i - kSubBits)))
217
+ return (i << kSubBits) + j;
218
+ return (32 << kSubBits);
219
+ }
220
+
221
+ static long MyMultDiv64(long value, long elapsedTime)
222
+ {
223
+ long freq = 1000; // ms
224
+ long elTime = elapsedTime;
225
+ while (freq > 1000000)
226
+ {
227
+ freq >>>= 1;
228
+ elTime >>>= 1;
229
+ }
230
+ if (elTime == 0)
231
+ elTime = 1;
232
+ return value * freq / elTime;
233
+ }
234
+
235
+ static long GetCompressRating(int dictionarySize, long elapsedTime, long size)
236
+ {
237
+ long t = GetLogSize(dictionarySize) - (18 << kSubBits);
238
+ long numCommandsForOne = 1060 + ((t * t * 10) >> (2 * kSubBits));
239
+ long numCommands = (long)(size) * numCommandsForOne;
240
+ return MyMultDiv64(numCommands, elapsedTime);
241
+ }
242
+
243
+ static long GetDecompressRating(long elapsedTime, long outSize, long inSize)
244
+ {
245
+ long numCommands = inSize * 220 + outSize * 20;
246
+ return MyMultDiv64(numCommands, elapsedTime);
247
+ }
248
+
249
+ static long GetTotalRating(
250
+ int dictionarySize,
251
+ long elapsedTimeEn, long sizeEn,
252
+ long elapsedTimeDe,
253
+ long inSizeDe, long outSizeDe)
254
+ {
255
+ return (GetCompressRating(dictionarySize, elapsedTimeEn, sizeEn) +
256
+ GetDecompressRating(elapsedTimeDe, inSizeDe, outSizeDe)) / 2;
257
+ }
258
+
259
+ static void PrintValue(long v)
260
+ {
261
+ String s = "";
262
+ s += v;
263
+ for (int i = 0; i + s.length() < 6; i++)
264
+ System.out.print(" ");
265
+ System.out.print(s);
266
+ }
267
+
268
+ static void PrintRating(long rating)
269
+ {
270
+ PrintValue(rating / 1000000);
271
+ System.out.print(" MIPS");
272
+ }
273
+
274
+ static void PrintResults(
275
+ int dictionarySize,
276
+ long elapsedTime,
277
+ long size,
278
+ boolean decompressMode, long secondSize)
279
+ {
280
+ long speed = MyMultDiv64(size, elapsedTime);
281
+ PrintValue(speed / 1024);
282
+ System.out.print(" KB/s ");
283
+ long rating;
284
+ if (decompressMode)
285
+ rating = GetDecompressRating(elapsedTime, size, secondSize);
286
+ else
287
+ rating = GetCompressRating(dictionarySize, elapsedTime, size);
288
+ PrintRating(rating);
289
+ }
290
+
291
+ static public int LzmaBenchmark(int numIterations, int dictionarySize) throws Exception
292
+ {
293
+ if (numIterations <= 0)
294
+ return 0;
295
+ if (dictionarySize < (1 << 18))
296
+ {
297
+ System.out.println("\nError: dictionary size for benchmark must be >= 18 (256 KB)");
298
+ return 1;
299
+ }
300
+ System.out.print("\n Compressing Decompressing\n\n");
301
+
302
+ SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
303
+ SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
304
+
305
+ if (!encoder.SetDictionarySize(dictionarySize))
306
+ throw new Exception("Incorrect dictionary size");
307
+
308
+ int kBufferSize = dictionarySize + kAdditionalSize;
309
+ int kCompressedBufferSize = (kBufferSize / 2) + kCompressedAdditionalSize;
310
+
311
+ ByteArrayOutputStream propStream = new ByteArrayOutputStream();
312
+ encoder.WriteCoderProperties(propStream);
313
+ byte[] propArray = propStream.toByteArray();
314
+ decoder.SetDecoderProperties(propArray);
315
+
316
+ CBenchRandomGenerator rg = new CBenchRandomGenerator();
317
+
318
+ rg.Set(kBufferSize);
319
+ rg.Generate();
320
+ CRC crc = new CRC();
321
+ crc.Init();
322
+ crc.Update(rg.Buffer, 0, rg.BufferSize);
323
+
324
+ CProgressInfo progressInfo = new CProgressInfo();
325
+ progressInfo.ApprovedStart = dictionarySize;
326
+
327
+ long totalBenchSize = 0;
328
+ long totalEncodeTime = 0;
329
+ long totalDecodeTime = 0;
330
+ long totalCompressedSize = 0;
331
+
332
+ MyInputStream inStream = new MyInputStream(rg.Buffer, rg.BufferSize);
333
+
334
+ byte[] compressedBuffer = new byte[kCompressedBufferSize];
335
+ MyOutputStream compressedStream = new MyOutputStream(compressedBuffer);
336
+ CrcOutStream crcOutStream = new CrcOutStream();
337
+ MyInputStream inputCompressedStream = null;
338
+ int compressedSize = 0;
339
+ for (int i = 0; i < numIterations; i++)
340
+ {
341
+ progressInfo.Init();
342
+ inStream.reset();
343
+ compressedStream.reset();
344
+ encoder.Code(inStream, compressedStream, -1, -1, progressInfo);
345
+ long encodeTime = System.currentTimeMillis() - progressInfo.Time;
346
+
347
+ if (i == 0)
348
+ {
349
+ compressedSize = compressedStream.size();
350
+ inputCompressedStream = new MyInputStream(compressedBuffer, compressedSize);
351
+ }
352
+ else if (compressedSize != compressedStream.size())
353
+ throw (new Exception("Encoding error"));
354
+
355
+ if (progressInfo.InSize == 0)
356
+ throw (new Exception("Internal ERROR 1282"));
357
+
358
+ long decodeTime = 0;
359
+ for (int j = 0; j < 2; j++)
360
+ {
361
+ inputCompressedStream.reset();
362
+ crcOutStream.Init();
363
+
364
+ long outSize = kBufferSize;
365
+ long startTime = System.currentTimeMillis();
366
+ if (!decoder.Code(inputCompressedStream, crcOutStream, outSize))
367
+ throw (new Exception("Decoding Error"));;
368
+ decodeTime = System.currentTimeMillis() - startTime;
369
+ if (crcOutStream.GetDigest() != crc.GetDigest())
370
+ throw (new Exception("CRC Error"));
371
+ }
372
+ long benchSize = kBufferSize - (long)progressInfo.InSize;
373
+ PrintResults(dictionarySize, encodeTime, benchSize, false, 0);
374
+ System.out.print(" ");
375
+ PrintResults(dictionarySize, decodeTime, kBufferSize, true, compressedSize);
376
+ System.out.println();
377
+
378
+ totalBenchSize += benchSize;
379
+ totalEncodeTime += encodeTime;
380
+ totalDecodeTime += decodeTime;
381
+ totalCompressedSize += compressedSize;
382
+ }
383
+ System.out.println("---------------------------------------------------");
384
+ PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0);
385
+ System.out.print(" ");
386
+ PrintResults(dictionarySize, totalDecodeTime,
387
+ kBufferSize * (long)numIterations, true, totalCompressedSize);
388
+ System.out.println(" Average");
389
+ return 0;
390
+ }
391
+ }
@@ -0,0 +1,104 @@
1
+ package com.ephemeronindustries.lzma;
2
+
3
+ import java.io.ByteArrayInputStream;
4
+ import java.io.ByteArrayOutputStream;
5
+ import java.io.IOException;
6
+ import java.util.Random;
7
+
8
+ import SevenZip.Compression.LZMA.Decoder;
9
+ import SevenZip.Compression.LZMA.Encoder;
10
+
11
+ /**
12
+ * Extremely simple LZMA compressor/decompressor. Encodes/decodes a single
13
+ * buffer.
14
+ *
15
+ * @author Ian Levesque
16
+ *
17
+ * This code, like the underlying LZMA library it was shamelessly cut from, is public domain.
18
+ */
19
+ public class LZMA {
20
+ Encoder encoder = new Encoder();
21
+ Decoder decoder = new Decoder();
22
+
23
+ public LZMA() {
24
+
25
+ }
26
+
27
+ public byte[] compress(byte[] buffer) throws IOException {
28
+ ByteArrayInputStream inStream = new ByteArrayInputStream(buffer);
29
+ ByteArrayOutputStream outStream = new ByteArrayOutputStream();
30
+
31
+ encoder.WriteCoderProperties(outStream);
32
+ long fileSize = buffer.length;
33
+ for (int i = 0; i < 8; i++)
34
+ outStream.write((int)(fileSize >>> (8 * i)) & 0xFF);
35
+ encoder.Code(inStream, outStream, -1, -1, null);
36
+
37
+ outStream.flush();
38
+ outStream.close();
39
+ inStream.close();
40
+
41
+ return outStream.toByteArray();
42
+ }
43
+
44
+ public byte[] decompress(byte[] buffer) throws IOException {
45
+ ByteArrayInputStream inStream = new ByteArrayInputStream(buffer);
46
+ ByteArrayOutputStream outStream = new ByteArrayOutputStream();
47
+
48
+ int propertiesSize = 5;
49
+ byte[] properties = new byte[propertiesSize];
50
+ if (inStream.read(properties, 0, propertiesSize) != propertiesSize)
51
+ throw new IOException("input .lzma file is too short");
52
+ SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
53
+ if (!decoder.SetDecoderProperties(properties))
54
+ throw new IOException("Incorrect stream properties");
55
+ long outSize = 0;
56
+ for (int i = 0; i < 8; i++)
57
+ {
58
+ int v = inStream.read();
59
+ if (v < 0)
60
+ throw new IOException("Can't read stream size");
61
+ outSize |= ((long)v) << (8 * i);
62
+ }
63
+ if (!decoder.Code(inStream, outStream, outSize))
64
+ throw new IOException("Error in data stream");
65
+
66
+ return outStream.toByteArray();
67
+ }
68
+
69
+ /**
70
+ * @param args
71
+ */
72
+ public static void main(String[] args) {
73
+ for(int j = 0; j < 5; j++) {
74
+
75
+ byte[] testArray = new byte[2 * 1024 * 1024];
76
+ new Random(12345).nextBytes(testArray);
77
+
78
+ LZMA lzma = new LZMA();
79
+ try {
80
+ long startTime = System.currentTimeMillis();
81
+
82
+ byte[] compressed = lzma.compress(testArray);
83
+
84
+ long compressedTime = System.currentTimeMillis();
85
+
86
+ byte[] decompressed = lzma.decompress(compressed);
87
+
88
+ long decompressedTime = System.currentTimeMillis();
89
+
90
+ if(testArray.length != decompressed.length) throw new Exception("Wrong Length!");
91
+
92
+ for(int i = 0; i < testArray.length; i++) {
93
+ if(testArray[i] != decompressed[i])
94
+ throw new Exception("Wrong Data!");
95
+ }
96
+
97
+ System.out.println("Compression Ratio: " + (compressed.length / (float)testArray.length) + " Compress Time: " + (compressedTime - startTime) + " ms. Decompress Time: " + (decompressedTime - compressedTime) + " ms.");
98
+ } catch (Exception e) {
99
+ e.printStackTrace();
100
+ }
101
+ }
102
+ }
103
+
104
+ }