OutputWindow.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // OutputWindow.cs
  2. //
  3. // Copyright (C) 2001 Mike Krueger
  4. //
  5. // This file was translated from java, it was part of the GNU Classpath
  6. // Copyright (C) 2001 Free Software Foundation, Inc.
  7. //
  8. // This program is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU General Public License
  10. // as published by the Free Software Foundation; either version 2
  11. // of the License, or (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. //
  22. // Linking this library statically or dynamically with other modules is
  23. // making a combined work based on this library. Thus, the terms and
  24. // conditions of the GNU General Public License cover the whole
  25. // combination.
  26. //
  27. // As a special exception, the copyright holders of this library give you
  28. // permission to link this library with independent modules to produce an
  29. // executable, regardless of the license terms of these independent
  30. // modules, and to copy and distribute the resulting executable under
  31. // terms of your choice, provided that you also meet, for each linked
  32. // independent module, the terms and conditions of the license of that
  33. // module. An independent module is a module which is not derived from
  34. // or based on this library. If you modify this library, you may extend
  35. // this exception to your version of the library, but you are not
  36. // obligated to do so. If you do not wish to do so, delete this
  37. // exception statement from your version.
  38. using System;
  39. namespace CommonMPQ.SharpZipLib.Zip.Compression.Streams
  40. {
  41. /// <summary>
  42. /// Contains the output from the Inflation process.
  43. /// We need to have a window so that we can refer backwards into the output stream
  44. /// to repeat stuff.<br/>
  45. /// Author of the original java version : John Leuner
  46. /// </summary>
  47. public class OutputWindow
  48. {
  49. #region Constants
  50. const int WindowSize = 1 << 15;
  51. const int WindowMask = WindowSize - 1;
  52. #endregion
  53. #region Instance Fields
  54. byte[] window = new byte[WindowSize]; //The window is 2^15 bytes
  55. int windowEnd;
  56. int windowFilled;
  57. #endregion
  58. /// <summary>
  59. /// Write a byte to this output window
  60. /// </summary>
  61. /// <param name="value">value to write</param>
  62. /// <exception cref="InvalidOperationException">
  63. /// if window is full
  64. /// </exception>
  65. public void Write(int value)
  66. {
  67. if (windowFilled++ == WindowSize) {
  68. throw new InvalidOperationException("Window full");
  69. }
  70. window[windowEnd++] = (byte) value;
  71. windowEnd &= WindowMask;
  72. }
  73. private void SlowRepeat(int repStart, int length, int distance)
  74. {
  75. while (length-- > 0) {
  76. window[windowEnd++] = window[repStart++];
  77. windowEnd &= WindowMask;
  78. repStart &= WindowMask;
  79. }
  80. }
  81. /// <summary>
  82. /// Append a byte pattern already in the window itself
  83. /// </summary>
  84. /// <param name="length">length of pattern to copy</param>
  85. /// <param name="distance">distance from end of window pattern occurs</param>
  86. /// <exception cref="InvalidOperationException">
  87. /// If the repeated data overflows the window
  88. /// </exception>
  89. public void Repeat(int length, int distance)
  90. {
  91. if ((windowFilled += length) > WindowSize) {
  92. throw new InvalidOperationException("Window full");
  93. }
  94. int repStart = (windowEnd - distance) & WindowMask;
  95. int border = WindowSize - length;
  96. if ( (repStart <= border) && (windowEnd < border) ) {
  97. if (length <= distance) {
  98. System.Array.Copy(window, repStart, window, windowEnd, length);
  99. windowEnd += length;
  100. } else {
  101. // We have to copy manually, since the repeat pattern overlaps.
  102. while (length-- > 0) {
  103. window[windowEnd++] = window[repStart++];
  104. }
  105. }
  106. } else {
  107. SlowRepeat(repStart, length, distance);
  108. }
  109. }
  110. /// <summary>
  111. /// Copy from input manipulator to internal window
  112. /// </summary>
  113. /// <param name="input">source of data</param>
  114. /// <param name="length">length of data to copy</param>
  115. /// <returns>the number of bytes copied</returns>
  116. public int CopyStored(StreamManipulator input, int length)
  117. {
  118. length = Math.Min(Math.Min(length, WindowSize - windowFilled), input.AvailableBytes);
  119. int copied;
  120. int tailLen = WindowSize - windowEnd;
  121. if (length > tailLen) {
  122. copied = input.CopyBytes(window, windowEnd, tailLen);
  123. if (copied == tailLen) {
  124. copied += input.CopyBytes(window, 0, length - tailLen);
  125. }
  126. } else {
  127. copied = input.CopyBytes(window, windowEnd, length);
  128. }
  129. windowEnd = (windowEnd + copied) & WindowMask;
  130. windowFilled += copied;
  131. return copied;
  132. }
  133. /// <summary>
  134. /// Copy dictionary to window
  135. /// </summary>
  136. /// <param name="dictionary">source dictionary</param>
  137. /// <param name="offset">offset of start in source dictionary</param>
  138. /// <param name="length">length of dictionary</param>
  139. /// <exception cref="InvalidOperationException">
  140. /// If window isnt empty
  141. /// </exception>
  142. public void CopyDict(byte[] dictionary, int offset, int length)
  143. {
  144. if ( dictionary == null ) {
  145. throw new ArgumentNullException("dictionary");
  146. }
  147. if (windowFilled > 0) {
  148. throw new InvalidOperationException();
  149. }
  150. if (length > WindowSize) {
  151. offset += length - WindowSize;
  152. length = WindowSize;
  153. }
  154. System.Array.Copy(dictionary, offset, window, 0, length);
  155. windowEnd = length & WindowMask;
  156. }
  157. /// <summary>
  158. /// Get remaining unfilled space in window
  159. /// </summary>
  160. /// <returns>Number of bytes left in window</returns>
  161. public int GetFreeSpace()
  162. {
  163. return WindowSize - windowFilled;
  164. }
  165. /// <summary>
  166. /// Get bytes available for output in window
  167. /// </summary>
  168. /// <returns>Number of bytes filled</returns>
  169. public int GetAvailable()
  170. {
  171. return windowFilled;
  172. }
  173. /// <summary>
  174. /// Copy contents of window to output
  175. /// </summary>
  176. /// <param name="output">buffer to copy to</param>
  177. /// <param name="offset">offset to start at</param>
  178. /// <param name="len">number of bytes to count</param>
  179. /// <returns>The number of bytes copied</returns>
  180. /// <exception cref="InvalidOperationException">
  181. /// If a window underflow occurs
  182. /// </exception>
  183. public int CopyOutput(byte[] output, int offset, int len)
  184. {
  185. int copyEnd = windowEnd;
  186. if (len > windowFilled) {
  187. len = windowFilled;
  188. } else {
  189. copyEnd = (windowEnd - windowFilled + len) & WindowMask;
  190. }
  191. int copied = len;
  192. int tailLen = len - copyEnd;
  193. if (tailLen > 0) {
  194. System.Array.Copy(window, WindowSize - tailLen, output, offset, tailLen);
  195. offset += tailLen;
  196. len = copyEnd;
  197. }
  198. System.Array.Copy(window, copyEnd - len, output, offset, len);
  199. windowFilled -= copied;
  200. if (windowFilled < 0) {
  201. throw new InvalidOperationException();
  202. }
  203. return copied;
  204. }
  205. /// <summary>
  206. /// Reset by clearing window so <see cref="GetAvailable">GetAvailable</see> returns 0
  207. /// </summary>
  208. public void Reset()
  209. {
  210. windowFilled = windowEnd = 0;
  211. }
  212. }
  213. }