OutputWindow.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // OutputWindow.cs
  2. // Copyright (C) 2001 Mike Krueger
  3. //
  4. // This file was translated from java, it was part of the GNU Classpath
  5. // Copyright (C) 2001 Free Software Foundation, Inc.
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. //
  21. // Linking this library statically or dynamically with other modules is
  22. // making a combined work based on this library. Thus, the terms and
  23. // conditions of the GNU General Public License cover the whole
  24. // combination.
  25. //
  26. // As a special exception, the copyright holders of this library give you
  27. // permission to link this library with independent modules to produce an
  28. // executable, regardless of the license terms of these independent
  29. // modules, and to copy and distribute the resulting executable under
  30. // terms of your choice, provided that you also meet, for each linked
  31. // independent module, the terms and conditions of the license of that
  32. // module. An independent module is a module which is not derived from
  33. // or based on this library. If you modify this library, you may extend
  34. // this exception to your version of the library, but you are not
  35. // obligated to do so. If you do not wish to do so, delete this
  36. // exception statement from your version.
  37. using System;
  38. namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams
  39. {
  40. /// <summary>
  41. /// Contains the output from the Inflation process.
  42. /// We need to have a window so that we can refer backwards into the output stream
  43. /// to repeat stuff.
  44. ///
  45. /// author of the original java version : John Leuner
  46. /// </summary>
  47. public class OutputWindow
  48. {
  49. private static int WINDOW_SIZE = 1 << 15;
  50. private static int WINDOW_MASK = WINDOW_SIZE - 1;
  51. private byte[] window = new byte[WINDOW_SIZE]; //The window is 2^15 bytes
  52. private int windowEnd = 0;
  53. private int windowFilled = 0;
  54. public void Write(int abyte)
  55. {
  56. if (windowFilled++ == WINDOW_SIZE) {
  57. throw new InvalidOperationException("Window full");
  58. }
  59. window[windowEnd++] = (byte) abyte;
  60. windowEnd &= WINDOW_MASK;
  61. }
  62. private void SlowRepeat(int repStart, int len, int dist)
  63. {
  64. while (len-- > 0) {
  65. window[windowEnd++] = window[repStart++];
  66. windowEnd &= WINDOW_MASK;
  67. repStart &= WINDOW_MASK;
  68. }
  69. }
  70. public void Repeat(int len, int dist)
  71. {
  72. if ((windowFilled += len) > WINDOW_SIZE) {
  73. throw new InvalidOperationException("Window full");
  74. }
  75. int rep_start = (windowEnd - dist) & WINDOW_MASK;
  76. int border = WINDOW_SIZE - len;
  77. if (rep_start <= border && windowEnd < border) {
  78. if (len <= dist) {
  79. System.Array.Copy(window, rep_start, window, windowEnd, len);
  80. windowEnd += len;
  81. } else {
  82. /* We have to copy manually, since the repeat pattern overlaps.
  83. */
  84. while (len-- > 0) {
  85. window[windowEnd++] = window[rep_start++];
  86. }
  87. }
  88. } else {
  89. SlowRepeat(rep_start, len, dist);
  90. }
  91. }
  92. public int CopyStored(StreamManipulator input, int len)
  93. {
  94. len = Math.Min(Math.Min(len, WINDOW_SIZE - windowFilled), input.AvailableBytes);
  95. int copied;
  96. int tailLen = WINDOW_SIZE - windowEnd;
  97. if (len > tailLen) {
  98. copied = input.CopyBytes(window, windowEnd, tailLen);
  99. if (copied == tailLen) {
  100. copied += input.CopyBytes(window, 0, len - tailLen);
  101. }
  102. } else {
  103. copied = input.CopyBytes(window, windowEnd, len);
  104. }
  105. windowEnd = (windowEnd + copied) & WINDOW_MASK;
  106. windowFilled += copied;
  107. return copied;
  108. }
  109. public void CopyDict(byte[] dict, int offset, int len)
  110. {
  111. if (windowFilled > 0) {
  112. throw new InvalidOperationException();
  113. }
  114. if (len > WINDOW_SIZE) {
  115. offset += len - WINDOW_SIZE;
  116. len = WINDOW_SIZE;
  117. }
  118. System.Array.Copy(dict, offset, window, 0, len);
  119. windowEnd = len & WINDOW_MASK;
  120. }
  121. public int GetFreeSpace()
  122. {
  123. return WINDOW_SIZE - windowFilled;
  124. }
  125. public int GetAvailable()
  126. {
  127. return windowFilled;
  128. }
  129. public int CopyOutput(byte[] output, int offset, int len)
  130. {
  131. int copy_end = windowEnd;
  132. if (len > windowFilled) {
  133. len = windowFilled;
  134. } else {
  135. copy_end = (windowEnd - windowFilled + len) & WINDOW_MASK;
  136. }
  137. int copied = len;
  138. int tailLen = len - copy_end;
  139. if (tailLen > 0) {
  140. System.Array.Copy(window, WINDOW_SIZE - tailLen, output, offset, tailLen);
  141. offset += tailLen;
  142. len = copy_end;
  143. }
  144. System.Array.Copy(window, copy_end - len, output, offset, len);
  145. windowFilled -= copied;
  146. if (windowFilled < 0) {
  147. throw new InvalidOperationException();
  148. }
  149. return copied;
  150. }
  151. public void Reset()
  152. {
  153. windowFilled = windowEnd = 0;
  154. }
  155. }
  156. }