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/ICoder.h ADDED
@@ -0,0 +1,156 @@
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
+ kNumPasses = 0x460,
52
+ kAlgorithm = 0x470,
53
+ kMultiThread = 0x480,
54
+ kEndMarker = 0x490
55
+ };
56
+ }
57
+
58
+ CODER_INTERFACE(ICompressSetCoderProperties, 0x20)
59
+ {
60
+ STDMETHOD(SetCoderProperties)(const PROPID *propIDs,
61
+ const PROPVARIANT *properties, UInt32 numProperties) PURE;
62
+ };
63
+
64
+ /*
65
+ CODER_INTERFACE(ICompressSetCoderProperties, 0x21)
66
+ {
67
+ STDMETHOD(SetDecoderProperties)(ISequentialInStream *inStream) PURE;
68
+ };
69
+ */
70
+
71
+ CODER_INTERFACE(ICompressSetDecoderProperties2, 0x22)
72
+ {
73
+ STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size) PURE;
74
+ };
75
+
76
+ CODER_INTERFACE(ICompressWriteCoderProperties, 0x23)
77
+ {
78
+ STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStreams) PURE;
79
+ };
80
+
81
+ CODER_INTERFACE(ICompressGetInStreamProcessedSize, 0x24)
82
+ {
83
+ STDMETHOD(GetInStreamProcessedSize)(UInt64 *value) PURE;
84
+ };
85
+
86
+ CODER_INTERFACE(ICompressGetSubStreamSize, 0x30)
87
+ {
88
+ STDMETHOD(GetSubStreamSize)(UInt64 subStream, UInt64 *value) PURE;
89
+ };
90
+
91
+ CODER_INTERFACE(ICompressSetInStream, 0x31)
92
+ {
93
+ STDMETHOD(SetInStream)(ISequentialInStream *inStream) PURE;
94
+ STDMETHOD(ReleaseInStream)() PURE;
95
+ };
96
+
97
+ CODER_INTERFACE(ICompressSetOutStream, 0x32)
98
+ {
99
+ STDMETHOD(SetOutStream)(ISequentialOutStream *outStream) PURE;
100
+ STDMETHOD(ReleaseOutStream)() PURE;
101
+ };
102
+
103
+ CODER_INTERFACE(ICompressSetInStreamSize, 0x33)
104
+ {
105
+ STDMETHOD(SetInStreamSize)(const UInt64 *inSize) PURE;
106
+ };
107
+
108
+ CODER_INTERFACE(ICompressSetOutStreamSize, 0x34)
109
+ {
110
+ STDMETHOD(SetOutStreamSize)(const UInt64 *outSize) PURE;
111
+ };
112
+
113
+ CODER_INTERFACE(ICompressFilter, 0x40)
114
+ {
115
+ STDMETHOD(Init)() PURE;
116
+ STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size) PURE;
117
+ // Filter return outSize (UInt32)
118
+ // if (outSize <= size): Filter have converted outSize bytes
119
+ // if (outSize > size): Filter have not converted anything.
120
+ // and it needs at least outSize bytes to convert one block
121
+ // (it's for crypto block algorithms).
122
+ };
123
+
124
+ CODER_INTERFACE(ICryptoProperties, 0x80)
125
+ {
126
+ STDMETHOD(SetKey)(const Byte *data, UInt32 size) PURE;
127
+ STDMETHOD(SetInitVector)(const Byte *data, UInt32 size) PURE;
128
+ };
129
+
130
+ CODER_INTERFACE(ICryptoSetPassword, 0x90)
131
+ {
132
+ STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size) PURE;
133
+ };
134
+
135
+ CODER_INTERFACE(ICryptoSetCRC, 0xA0)
136
+ {
137
+ STDMETHOD(CryptoSetCRC)(UInt32 crc) PURE;
138
+ };
139
+
140
+ //////////////////////
141
+ // It's for DLL file
142
+ namespace NMethodPropID
143
+ {
144
+ enum EEnum
145
+ {
146
+ kID,
147
+ kName,
148
+ kDecoder,
149
+ kEncoder,
150
+ kInStreams,
151
+ kOutStreams,
152
+ kDescription
153
+ };
154
+ }
155
+
156
+ #endif
@@ -0,0 +1,63 @@
1
+ // MatchFinders/IMatchFinder.h
2
+
3
+ #ifndef __IMATCHFINDER_H
4
+ #define __IMATCHFINDER_H
5
+
6
+ // {23170F69-40C1-278A-0000-000200010000}
7
+ DEFINE_GUID(IID_IInWindowStream,
8
+ 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00);
9
+ MIDL_INTERFACE("23170F69-40C1-278A-0000-000200010000")
10
+ IInWindowStream: public IUnknown
11
+ {
12
+ STDMETHOD(Init)(ISequentialInStream *inStream) PURE;
13
+ STDMETHOD_(void, ReleaseStream)() PURE;
14
+ STDMETHOD(MovePos)() PURE;
15
+ STDMETHOD_(Byte, GetIndexByte)(Int32 index) PURE;
16
+ STDMETHOD_(UInt32, GetMatchLen)(Int32 index, UInt32 distance, UInt32 limit) PURE;
17
+ STDMETHOD_(UInt32, GetNumAvailableBytes)() PURE;
18
+ STDMETHOD_(const Byte *, GetPointerToCurrentPos)() PURE;
19
+ };
20
+
21
+ // {23170F69-40C1-278A-0000-000200020000}
22
+ DEFINE_GUID(IID_IMatchFinder,
23
+ 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00);
24
+ MIDL_INTERFACE("23170F69-40C1-278A-0000-000200020000")
25
+ IMatchFinder: public IInWindowStream
26
+ {
27
+ STDMETHOD(Create)(UInt32 historySize, UInt32 keepAddBufferBefore,
28
+ UInt32 matchMaxLen, UInt32 keepAddBufferAfter) PURE;
29
+ STDMETHOD_(UInt32, GetLongestMatch)(UInt32 *distances) PURE;
30
+ STDMETHOD_(void, DummyLongestMatch)() PURE;
31
+ };
32
+
33
+ // {23170F69-40C1-278A-0000-000200020100}
34
+ DEFINE_GUID(IID_IMatchFinderCallback,
35
+ 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x01, 0x00);
36
+ MIDL_INTERFACE("23170F69-40C1-278A-0000-000200020100")
37
+ IMatchFinderCallback: public IUnknown
38
+ {
39
+ STDMETHOD(BeforeChangingBufferPos)() PURE;
40
+ STDMETHOD(AfterChangingBufferPos)() PURE;
41
+ };
42
+
43
+ // {23170F69-40C1-278A-0000-000200020200}
44
+ DEFINE_GUID(IID_IMatchFinderSetCallback,
45
+ 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x02, 0x00);
46
+ MIDL_INTERFACE("23170F69-40C1-278A-0000-000200020200")
47
+ IMatchFinderSetCallback: public IUnknown
48
+ {
49
+ STDMETHOD(SetCallback)(IMatchFinderCallback *callback) PURE;
50
+ };
51
+
52
+ /*
53
+ // {23170F69-40C1-278A-0000-000200030000}
54
+ DEFINE_GUID(IID_IInitMatchFinder,
55
+ 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00);
56
+ MIDL_INTERFACE("23170F69-40C1-278A-0000-000200030000")
57
+ IMatchFinderInit: public IUnknown
58
+ {
59
+ STDMETHOD(InitMatchFinder)(IMatchFinder *matchFinder) PURE;
60
+ };
61
+ */
62
+
63
+ #endif
data/ext/IStream.h ADDED
@@ -0,0 +1,62 @@
1
+ // IStream.h
2
+
3
+ #ifndef __ISTREAM_H
4
+ #define __ISTREAM_H
5
+
6
+ #include "MyUnknown.h"
7
+ #include "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
data/ext/InBuffer.cpp ADDED
@@ -0,0 +1,80 @@
1
+ // InBuffer.cpp
2
+
3
+ #include "StdAfx.h"
4
+
5
+ #include "InBuffer.h"
6
+
7
+ #include "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
+ }
data/ext/InBuffer.h ADDED
@@ -0,0 +1,76 @@
1
+ // InBuffer.h
2
+
3
+ #ifndef __INBUFFER_H
4
+ #define __INBUFFER_H
5
+
6
+ #include "IStream.h"
7
+ #include "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<ISequentialInStream> _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
@@ -0,0 +1,102 @@
1
+ // LZInWindow.cpp
2
+
3
+ #include "StdAfx.h"
4
+
5
+ #include "LZInWindow.h"
6
+ #include "MyCom.h"
7
+ #include "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
+ _keepSizeReserv = keepSizeReserv;
20
+ UInt32 blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
21
+ if (_bufferBase == 0 || _blockSize != blockSize)
22
+ {
23
+ Free();
24
+ _blockSize = blockSize;
25
+ if (_blockSize != 0)
26
+ _bufferBase = (Byte *)::BigAlloc(_blockSize);
27
+ }
28
+ _pointerToLastSafePosition = _bufferBase + _blockSize - keepSizeAfter;
29
+ if (_blockSize == 0)
30
+ return true;
31
+ return (_bufferBase != 0);
32
+ }
33
+
34
+
35
+ HRESULT CLZInWindow::Init(ISequentialInStream *stream)
36
+ {
37
+ _stream = stream;
38
+ _buffer = _bufferBase;
39
+ _pos = 0;
40
+ _streamPos = 0;
41
+ _streamEndWasReached = false;
42
+ return ReadBlock();
43
+ }
44
+
45
+ /*
46
+ void CLZInWindow::ReleaseStream()
47
+ {
48
+ _stream.Release();
49
+ }
50
+ */
51
+
52
+ ///////////////////////////////////////////
53
+ // ReadBlock
54
+
55
+ // In State:
56
+ // (_buffer + _streamPos) <= (_bufferBase + _blockSize)
57
+ // Out State:
58
+ // _posLimit <= _blockSize - _keepSizeAfter;
59
+ // if(_streamEndWasReached == false):
60
+ // _streamPos >= _pos + _keepSizeAfter
61
+ // _posLimit = _streamPos - _keepSizeAfter;
62
+ // else
63
+ //
64
+
65
+ HRESULT CLZInWindow::ReadBlock()
66
+ {
67
+ if(_streamEndWasReached)
68
+ return S_OK;
69
+ while(true)
70
+ {
71
+ UInt32 size = UInt32(_bufferBase - _buffer) + _blockSize - _streamPos;
72
+ if(size == 0)
73
+ return S_OK;
74
+ UInt32 numReadBytes;
75
+ RINOK(_stream->Read(_buffer + _streamPos, size, &numReadBytes));
76
+ if(numReadBytes == 0)
77
+ {
78
+ _posLimit = _streamPos;
79
+ const Byte *pointerToPostion = _buffer + _posLimit;
80
+ if(pointerToPostion > _pointerToLastSafePosition)
81
+ _posLimit = (UInt32)(_pointerToLastSafePosition - _buffer);
82
+ _streamEndWasReached = true;
83
+ return S_OK;
84
+ }
85
+ _streamPos += numReadBytes;
86
+ if(_streamPos >= _pos + _keepSizeAfter)
87
+ {
88
+ _posLimit = _streamPos - _keepSizeAfter;
89
+ return S_OK;
90
+ }
91
+ }
92
+ }
93
+
94
+ void CLZInWindow::MoveBlock()
95
+ {
96
+ BeforeMoveBlock();
97
+ UInt32 offset = UInt32(_buffer - _bufferBase) + _pos - _keepSizeBefore;
98
+ UInt32 numBytes = UInt32(_buffer - _bufferBase) + _streamPos - offset;
99
+ memmove(_bufferBase, _bufferBase + offset, numBytes);
100
+ _buffer -= offset;
101
+ AfterMoveBlock();
102
+ }