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/BranchX86.c ADDED
@@ -0,0 +1,101 @@
1
+ /* BranchX86.c */
2
+
3
+ #include "BranchX86.h"
4
+
5
+ /*
6
+ static int inline Test86MSByte(Byte b)
7
+ {
8
+ return (b == 0 || b == 0xFF);
9
+ }
10
+ */
11
+ #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
12
+
13
+ const int kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
14
+ const Byte kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
15
+
16
+ /*
17
+ void x86_Convert_Init(UInt32 *prevMask, UInt32 *prevPos)
18
+ {
19
+ *prevMask = 0;
20
+ *prevPos = (UInt32)(-5);
21
+ }
22
+ */
23
+
24
+ UInt32 x86_Convert(Byte *buffer, UInt32 endPos, UInt32 nowPos,
25
+ UInt32 *prevMask, UInt32 *prevPos, int encoding)
26
+ {
27
+ UInt32 bufferPos = 0;
28
+ UInt32 limit;
29
+
30
+ if (endPos < 5)
31
+ return 0;
32
+
33
+ if (nowPos - *prevPos > 5)
34
+ *prevPos = nowPos - 5;
35
+
36
+ limit = endPos - 5;
37
+ while(bufferPos <= limit)
38
+ {
39
+ Byte b = buffer[bufferPos];
40
+ UInt32 offset;
41
+ if (b != 0xE8 && b != 0xE9)
42
+ {
43
+ bufferPos++;
44
+ continue;
45
+ }
46
+ offset = (nowPos + bufferPos - *prevPos);
47
+ *prevPos = (nowPos + bufferPos);
48
+ if (offset > 5)
49
+ *prevMask = 0;
50
+ else
51
+ {
52
+ UInt32 i;
53
+ for (i = 0; i < offset; i++)
54
+ {
55
+ *prevMask &= 0x77;
56
+ *prevMask <<= 1;
57
+ }
58
+ }
59
+ b = buffer[bufferPos + 4];
60
+ if (Test86MSByte(b) && kMaskToAllowedStatus[(*prevMask >> 1) & 0x7] &&
61
+ (*prevMask >> 1) < 0x10)
62
+ {
63
+ UInt32 src =
64
+ ((UInt32)(b) << 24) |
65
+ ((UInt32)(buffer[bufferPos + 3]) << 16) |
66
+ ((UInt32)(buffer[bufferPos + 2]) << 8) |
67
+ (buffer[bufferPos + 1]);
68
+
69
+ UInt32 dest;
70
+ while(1)
71
+ {
72
+ UInt32 index;
73
+ if (encoding)
74
+ dest = (nowPos + bufferPos + 5) + src;
75
+ else
76
+ dest = src - (nowPos + bufferPos + 5);
77
+ if (*prevMask == 0)
78
+ break;
79
+ index = kMaskToBitNumber[*prevMask >> 1];
80
+ b = (Byte)(dest >> (24 - index * 8));
81
+ if (!Test86MSByte(b))
82
+ break;
83
+ src = dest ^ ((1 << (32 - index * 8)) - 1);
84
+ }
85
+ buffer[bufferPos + 4] = (Byte)(~(((dest >> 24) & 1) - 1));
86
+ buffer[bufferPos + 3] = (Byte)(dest >> 16);
87
+ buffer[bufferPos + 2] = (Byte)(dest >> 8);
88
+ buffer[bufferPos + 1] = (Byte)dest;
89
+ bufferPos += 5;
90
+ *prevMask = 0;
91
+ }
92
+ else
93
+ {
94
+ bufferPos++;
95
+ *prevMask |= 1;
96
+ if (Test86MSByte(b))
97
+ *prevMask |= 0x10;
98
+ }
99
+ }
100
+ return bufferPos;
101
+ }
data/ext/BranchX86.h ADDED
@@ -0,0 +1,19 @@
1
+ /* BranchX86.h */
2
+
3
+ #ifndef __BRANCHX86_H
4
+ #define __BRANCHX86_H
5
+
6
+ #ifndef UInt32
7
+ #define UInt32 unsigned int
8
+ #endif
9
+
10
+ #ifndef Byte
11
+ #define Byte unsigned char
12
+ #endif
13
+
14
+ #define x86_Convert_Init(prevMask, prevPos) { prevMask = 0; prevPos = (UInt32)(-5); }
15
+
16
+ UInt32 x86_Convert(Byte *buffer, UInt32 endPos, UInt32 nowPos,
17
+ UInt32 *prevMask, UInt32 *prevPos, int encoding);
18
+
19
+ #endif
data/ext/CRC.cpp ADDED
@@ -0,0 +1,61 @@
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
+ }
data/ext/CRC.h ADDED
@@ -0,0 +1,36 @@
1
+ // Common/CRC.h
2
+
3
+ #ifndef __COMMON_CRC_H
4
+ #define __COMMON_CRC_H
5
+
6
+ #include <stddef.h>
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
data/ext/C_FileIO.h ADDED
@@ -0,0 +1,45 @@
1
+ // Common/C_FileIO.h
2
+
3
+ #ifndef __COMMON_C_FILEIO_H
4
+ #define __COMMON_C_FILEIO_H
5
+
6
+ #include <stdio.h>
7
+ #include <sys/types.h>
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
+ off_t Seek(off_t distanceToMove, int moveMethod) const;
27
+ };
28
+
29
+ class CInFile: public CFileBase
30
+ {
31
+ public:
32
+ bool Open(const char *name);
33
+ ssize_t Read(void *data, size_t size);
34
+ };
35
+
36
+ class COutFile: public CFileBase
37
+ {
38
+ public:
39
+ bool Create(const char *name, bool createAlways);
40
+ ssize_t Write(const void *data, size_t size);
41
+ };
42
+
43
+ }}}
44
+
45
+ #endif
@@ -0,0 +1,82 @@
1
+ // Common/CommandLineParser.h
2
+
3
+ #ifndef __COMMON_COMMANDLINEPARSER_H
4
+ #define __COMMON_COMMANDLINEPARSER_H
5
+
6
+ #include "MyString.h"
7
+
8
+ namespace NCommandLineParser {
9
+
10
+ void SplitCommandLine(const UString &src, UString &dest1, UString &dest2);
11
+ void SplitCommandLine(const UString &s, UStringVector &parts);
12
+
13
+ namespace NSwitchType {
14
+ enum EEnum
15
+ {
16
+ kSimple,
17
+ kPostMinus,
18
+ kLimitedPostString,
19
+ kUnLimitedPostString,
20
+ kPostChar
21
+ };
22
+ }
23
+
24
+ struct CSwitchForm
25
+ {
26
+ const wchar_t *IDString;
27
+ NSwitchType::EEnum Type;
28
+ bool Multi;
29
+ int MinLen;
30
+ int MaxLen;
31
+ const wchar_t *PostCharSet;
32
+ };
33
+
34
+ struct CSwitchResult
35
+ {
36
+ bool ThereIs;
37
+ bool WithMinus;
38
+ UStringVector PostStrings;
39
+ int PostCharIndex;
40
+ CSwitchResult(): ThereIs(false) {};
41
+ };
42
+
43
+ class CParser
44
+ {
45
+ int _numSwitches;
46
+ CSwitchResult *_switches;
47
+ bool ParseString(const UString &s, const CSwitchForm *switchForms);
48
+ public:
49
+ UStringVector NonSwitchStrings;
50
+ CParser(int numSwitches);
51
+ ~CParser();
52
+ void ParseStrings(const CSwitchForm *switchForms,
53
+ const UStringVector &commandStrings);
54
+ const CSwitchResult& operator[](size_t index) const;
55
+ };
56
+
57
+ /////////////////////////////////
58
+ // Command parsing procedures
59
+
60
+ struct CCommandForm
61
+ {
62
+ wchar_t *IDString;
63
+ bool PostStringMode;
64
+ };
65
+
66
+ // Returns: Index of form and postString; -1, if there is no match
67
+ int ParseCommand(int numCommandForms, const CCommandForm *commandForms,
68
+ const UString &commandString, UString &postString);
69
+
70
+ struct CCommandSubCharsSet
71
+ {
72
+ wchar_t *Chars;
73
+ bool EmptyAllowed;
74
+ };
75
+
76
+ // Returns: indices of finded chars; -1 if there is no match
77
+ bool ParseSubCharsCommand(int numForms, const CCommandSubCharsSet *forms,
78
+ const UString &commandString, CIntVector &indices);
79
+
80
+ }
81
+
82
+ #endif
data/ext/Defs.h ADDED
@@ -0,0 +1,20 @@
1
+ // Common/Defs.h
2
+
3
+ #ifndef __COMMON_DEFS_H
4
+ #define __COMMON_DEFS_H
5
+
6
+ template <class T> inline T MyMin(T a, T b)
7
+ { return a < b ? a : b; }
8
+ template <class T> inline T MyMax(T a, T b)
9
+ { return a > b ? a : b; }
10
+
11
+ template <class T> 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
data/ext/FileStreams.h ADDED
@@ -0,0 +1,98 @@
1
+ // FileStreams.h
2
+
3
+ #ifndef __FILESTREAMS_H
4
+ #define __FILESTREAMS_H
5
+
6
+ #ifdef _WIN32
7
+ #include "FileIO.h"
8
+ #else
9
+ #include "C_FileIO.h"
10
+ #endif
11
+
12
+ #include "IStream.h"
13
+ #include "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