OutputWindow.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams
  3. {
  4. /// <summary>
  5. /// Contains the output from the Inflation process.
  6. /// We need to have a window so that we can refer backwards into the output stream
  7. /// to repeat stuff.<br/>
  8. /// Author of the original java version : John Leuner
  9. /// </summary>
  10. public class OutputWindow
  11. {
  12. #region Constants
  13. private const int WindowSize = 1 << 15;
  14. private const int WindowMask = WindowSize - 1;
  15. #endregion Constants
  16. #region Instance Fields
  17. private byte[] window = new byte[WindowSize]; //The window is 2^15 bytes
  18. private int windowEnd;
  19. private int windowFilled;
  20. #endregion Instance Fields
  21. /// <summary>
  22. /// Write a byte to this output window
  23. /// </summary>
  24. /// <param name="value">value to write</param>
  25. /// <exception cref="InvalidOperationException">
  26. /// if window is full
  27. /// </exception>
  28. public void Write(int value)
  29. {
  30. if (windowFilled++ == WindowSize)
  31. {
  32. throw new InvalidOperationException("Window full");
  33. }
  34. window[windowEnd++] = (byte)value;
  35. windowEnd &= WindowMask;
  36. }
  37. private void SlowRepeat(int repStart, int length, int distance)
  38. {
  39. while (length-- > 0)
  40. {
  41. window[windowEnd++] = window[repStart++];
  42. windowEnd &= WindowMask;
  43. repStart &= WindowMask;
  44. }
  45. }
  46. /// <summary>
  47. /// Append a byte pattern already in the window itself
  48. /// </summary>
  49. /// <param name="length">length of pattern to copy</param>
  50. /// <param name="distance">distance from end of window pattern occurs</param>
  51. /// <exception cref="InvalidOperationException">
  52. /// If the repeated data overflows the window
  53. /// </exception>
  54. public void Repeat(int length, int distance)
  55. {
  56. if ((windowFilled += length) > WindowSize)
  57. {
  58. throw new InvalidOperationException("Window full");
  59. }
  60. int repStart = (windowEnd - distance) & WindowMask;
  61. int border = WindowSize - length;
  62. if ((repStart <= border) && (windowEnd < border))
  63. {
  64. if (length <= distance)
  65. {
  66. System.Array.Copy(window, repStart, window, windowEnd, length);
  67. windowEnd += length;
  68. }
  69. else
  70. {
  71. // We have to copy manually, since the repeat pattern overlaps.
  72. while (length-- > 0)
  73. {
  74. window[windowEnd++] = window[repStart++];
  75. }
  76. }
  77. }
  78. else
  79. {
  80. SlowRepeat(repStart, length, distance);
  81. }
  82. }
  83. /// <summary>
  84. /// Copy from input manipulator to internal window
  85. /// </summary>
  86. /// <param name="input">source of data</param>
  87. /// <param name="length">length of data to copy</param>
  88. /// <returns>the number of bytes copied</returns>
  89. public int CopyStored(StreamManipulator input, int length)
  90. {
  91. length = Math.Min(Math.Min(length, WindowSize - windowFilled), input.AvailableBytes);
  92. int copied;
  93. int tailLen = WindowSize - windowEnd;
  94. if (length > tailLen)
  95. {
  96. copied = input.CopyBytes(window, windowEnd, tailLen);
  97. if (copied == tailLen)
  98. {
  99. copied += input.CopyBytes(window, 0, length - tailLen);
  100. }
  101. }
  102. else
  103. {
  104. copied = input.CopyBytes(window, windowEnd, length);
  105. }
  106. windowEnd = (windowEnd + copied) & WindowMask;
  107. windowFilled += copied;
  108. return copied;
  109. }
  110. /// <summary>
  111. /// Copy dictionary to window
  112. /// </summary>
  113. /// <param name="dictionary">source dictionary</param>
  114. /// <param name="offset">offset of start in source dictionary</param>
  115. /// <param name="length">length of dictionary</param>
  116. /// <exception cref="InvalidOperationException">
  117. /// If window isnt empty
  118. /// </exception>
  119. public void CopyDict(byte[] dictionary, int offset, int length)
  120. {
  121. if (dictionary == null)
  122. {
  123. throw new ArgumentNullException(nameof(dictionary));
  124. }
  125. if (windowFilled > 0)
  126. {
  127. throw new InvalidOperationException();
  128. }
  129. if (length > WindowSize)
  130. {
  131. offset += length - WindowSize;
  132. length = WindowSize;
  133. }
  134. System.Array.Copy(dictionary, offset, window, 0, length);
  135. windowEnd = length & WindowMask;
  136. }
  137. /// <summary>
  138. /// Get remaining unfilled space in window
  139. /// </summary>
  140. /// <returns>Number of bytes left in window</returns>
  141. public int GetFreeSpace()
  142. {
  143. return WindowSize - windowFilled;
  144. }
  145. /// <summary>
  146. /// Get bytes available for output in window
  147. /// </summary>
  148. /// <returns>Number of bytes filled</returns>
  149. public int GetAvailable()
  150. {
  151. return windowFilled;
  152. }
  153. /// <summary>
  154. /// Copy contents of window to output
  155. /// </summary>
  156. /// <param name="output">buffer to copy to</param>
  157. /// <param name="offset">offset to start at</param>
  158. /// <param name="len">number of bytes to count</param>
  159. /// <returns>The number of bytes copied</returns>
  160. /// <exception cref="InvalidOperationException">
  161. /// If a window underflow occurs
  162. /// </exception>
  163. public int CopyOutput(byte[] output, int offset, int len)
  164. {
  165. int copyEnd = windowEnd;
  166. if (len > windowFilled)
  167. {
  168. len = windowFilled;
  169. }
  170. else
  171. {
  172. copyEnd = (windowEnd - windowFilled + len) & WindowMask;
  173. }
  174. int copied = len;
  175. int tailLen = len - copyEnd;
  176. if (tailLen > 0)
  177. {
  178. System.Array.Copy(window, WindowSize - tailLen, output, offset, tailLen);
  179. offset += tailLen;
  180. len = copyEnd;
  181. }
  182. System.Array.Copy(window, copyEnd - len, output, offset, len);
  183. windowFilled -= copied;
  184. if (windowFilled < 0)
  185. {
  186. throw new InvalidOperationException();
  187. }
  188. return copied;
  189. }
  190. /// <summary>
  191. /// Reset by clearing window so <see cref="GetAvailable">GetAvailable</see> returns 0
  192. /// </summary>
  193. public void Reset()
  194. {
  195. windowFilled = windowEnd = 0;
  196. }
  197. }
  198. }