OutputWindow.cs 5.5 KB

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