LzInWindow.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.IO;
  3. namespace SharpCompress.Compressors.LZMA.LZ
  4. {
  5. internal class InWindow
  6. {
  7. public Byte[] _bufferBase; // pointer to buffer with data
  8. private Stream _stream;
  9. private UInt32 _posLimit; // offset (from _buffer) of first byte when new block reading must be done
  10. private bool _streamEndWasReached; // if (true) then _streamPos shows real end of stream
  11. private UInt32 _pointerToLastSafePosition;
  12. public UInt32 _bufferOffset;
  13. public UInt32 _blockSize; // Size of Allocated memory block
  14. public UInt32 _pos; // offset (from _buffer) of curent byte
  15. private UInt32 _keepSizeBefore; // how many BYTEs must be kept in buffer before _pos
  16. private UInt32 _keepSizeAfter; // how many BYTEs must be kept buffer after _pos
  17. public UInt32 _streamPos; // offset (from _buffer) of first not read byte from Stream
  18. public void MoveBlock()
  19. {
  20. UInt32 offset = _bufferOffset + _pos - _keepSizeBefore;
  21. // we need one additional byte, since MovePos moves on 1 byte.
  22. if (offset > 0)
  23. {
  24. offset--;
  25. }
  26. UInt32 numBytes = _bufferOffset + _streamPos - offset;
  27. // check negative offset ????
  28. for (UInt32 i = 0; i < numBytes; i++)
  29. {
  30. _bufferBase[i] = _bufferBase[offset + i];
  31. }
  32. _bufferOffset -= offset;
  33. }
  34. public virtual void ReadBlock()
  35. {
  36. if (_streamEndWasReached)
  37. {
  38. return;
  39. }
  40. while (true)
  41. {
  42. int size = (int)((0 - _bufferOffset) + _blockSize - _streamPos);
  43. if (size == 0)
  44. {
  45. return;
  46. }
  47. int numReadBytes = _stream != null
  48. ? _stream.Read(_bufferBase, (int)(_bufferOffset + _streamPos), size)
  49. : 0;
  50. if (numReadBytes == 0)
  51. {
  52. _posLimit = _streamPos;
  53. UInt32 pointerToPostion = _bufferOffset + _posLimit;
  54. if (pointerToPostion > _pointerToLastSafePosition)
  55. {
  56. _posLimit = _pointerToLastSafePosition - _bufferOffset;
  57. }
  58. _streamEndWasReached = true;
  59. return;
  60. }
  61. _streamPos += (UInt32)numReadBytes;
  62. if (_streamPos >= _pos + _keepSizeAfter)
  63. {
  64. _posLimit = _streamPos - _keepSizeAfter;
  65. }
  66. }
  67. }
  68. private void Free()
  69. {
  70. _bufferBase = null;
  71. }
  72. public void Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv)
  73. {
  74. _keepSizeBefore = keepSizeBefore;
  75. _keepSizeAfter = keepSizeAfter;
  76. UInt32 blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
  77. if (_bufferBase == null || _blockSize != blockSize)
  78. {
  79. Free();
  80. _blockSize = blockSize;
  81. _bufferBase = new Byte[_blockSize];
  82. }
  83. _pointerToLastSafePosition = _blockSize - keepSizeAfter;
  84. _streamEndWasReached = false;
  85. }
  86. public void SetStream(Stream stream)
  87. {
  88. _stream = stream;
  89. if (_streamEndWasReached)
  90. {
  91. _streamEndWasReached = false;
  92. if (IsDataStarved)
  93. {
  94. ReadBlock();
  95. }
  96. }
  97. }
  98. public void ReleaseStream()
  99. {
  100. _stream = null;
  101. }
  102. public void Init()
  103. {
  104. _bufferOffset = 0;
  105. _pos = 0;
  106. _streamPos = 0;
  107. _streamEndWasReached = false;
  108. ReadBlock();
  109. }
  110. public void MovePos()
  111. {
  112. _pos++;
  113. if (_pos > _posLimit)
  114. {
  115. UInt32 pointerToPostion = _bufferOffset + _pos;
  116. if (pointerToPostion > _pointerToLastSafePosition)
  117. {
  118. MoveBlock();
  119. }
  120. ReadBlock();
  121. }
  122. }
  123. public Byte GetIndexByte(Int32 index)
  124. {
  125. return _bufferBase[_bufferOffset + _pos + index];
  126. }
  127. // index + limit have not to exceed _keepSizeAfter;
  128. public UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit)
  129. {
  130. if (_streamEndWasReached)
  131. {
  132. if ((_pos + index) + limit > _streamPos)
  133. {
  134. limit = _streamPos - (UInt32)(_pos + index);
  135. }
  136. }
  137. distance++;
  138. // Byte *pby = _buffer + (size_t)_pos + index;
  139. UInt32 pby = _bufferOffset + _pos + (UInt32)index;
  140. UInt32 i;
  141. for (i = 0; i < limit && _bufferBase[pby + i] == _bufferBase[pby + i - distance]; i++)
  142. {
  143. ;
  144. }
  145. return i;
  146. }
  147. public UInt32 GetNumAvailableBytes()
  148. {
  149. return _streamPos - _pos;
  150. }
  151. public void ReduceOffsets(Int32 subValue)
  152. {
  153. _bufferOffset += (UInt32)subValue;
  154. _posLimit -= (UInt32)subValue;
  155. _pos -= (UInt32)subValue;
  156. _streamPos -= (UInt32)subValue;
  157. }
  158. public bool IsDataStarved => _streamPos - _pos < _keepSizeAfter;
  159. }
  160. }