StreamManipulator.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // StreamManipulator.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. /// This class allows us to retrieve a specified amount of bits from
  42. /// the input buffer, as well as copy big byte blocks.
  43. ///
  44. /// It uses an int buffer to store up to 31 bits for direct
  45. /// manipulation. This guarantees that we can get at least 16 bits,
  46. /// but we only need at most 15, so this is all safe.
  47. ///
  48. /// There are some optimizations in this class, for example, you must
  49. /// never peek more then 8 bits more than needed, and you must first
  50. /// peek bits before you may drop them. This is not a general purpose
  51. /// class but optimized for the behaviour of the Inflater.
  52. ///
  53. /// authors of the original java version : John Leuner, Jochen Hoenicke
  54. /// </summary>
  55. public class StreamManipulator
  56. {
  57. private byte[] window;
  58. private int window_start = 0;
  59. private int window_end = 0;
  60. private uint buffer = 0;
  61. private int bits_in_buffer = 0;
  62. /// <summary>
  63. /// Get the next n bits but don't increase input pointer. n must be
  64. /// less or equal 16 and if you if this call succeeds, you must drop
  65. /// at least n-8 bits in the next call.
  66. /// </summary>
  67. /// <returns>
  68. /// the value of the bits, or -1 if not enough bits available. */
  69. /// </returns>
  70. public int PeekBits(int n)
  71. {
  72. if (bits_in_buffer < n) {
  73. if (window_start == window_end) {
  74. return -1; // ok
  75. }
  76. buffer |= (uint)((window[window_start++] & 0xff |
  77. (window[window_start++] & 0xff) << 8) << bits_in_buffer);
  78. bits_in_buffer += 16;
  79. }
  80. return (int)(buffer & ((1 << n) - 1));
  81. }
  82. /// <summary>
  83. /// Drops the next n bits from the input. You should have called peekBits
  84. /// with a bigger or equal n before, to make sure that enough bits are in
  85. /// the bit buffer.
  86. /// </summary>
  87. public void DropBits(int n)
  88. {
  89. buffer >>= n;
  90. bits_in_buffer -= n;
  91. }
  92. /// <summary>
  93. /// Gets the next n bits and increases input pointer. This is equivalent
  94. /// to peekBits followed by dropBits, except for correct error handling.
  95. /// </summary>
  96. /// <returns>
  97. /// the value of the bits, or -1 if not enough bits available.
  98. /// </returns>
  99. public int GetBits(int n)
  100. {
  101. int bits = PeekBits(n);
  102. if (bits >= 0) {
  103. DropBits(n);
  104. }
  105. return bits;
  106. }
  107. /// <summary>
  108. /// Gets the number of bits available in the bit buffer. This must be
  109. /// only called when a previous peekBits() returned -1.
  110. /// </summary>
  111. /// <returns>
  112. /// the number of bits available.
  113. /// </returns>
  114. public int AvailableBits {
  115. get {
  116. return bits_in_buffer;
  117. }
  118. }
  119. /// <summary>
  120. /// Gets the number of bytes available.
  121. /// </summary>
  122. /// <returns>
  123. /// the number of bytes available.
  124. /// </returns>
  125. public int AvailableBytes {
  126. get {
  127. return window_end - window_start + (bits_in_buffer >> 3);
  128. }
  129. }
  130. /// <summary>
  131. /// Skips to the next byte boundary.
  132. /// </summary>
  133. public void SkipToByteBoundary()
  134. {
  135. buffer >>= (bits_in_buffer & 7);
  136. bits_in_buffer &= ~7;
  137. }
  138. public bool IsNeedingInput {
  139. get {
  140. return window_start == window_end;
  141. }
  142. }
  143. /// <summary>
  144. /// Copies length bytes from input buffer to output buffer starting
  145. /// at output[offset]. You have to make sure, that the buffer is
  146. /// byte aligned. If not enough bytes are available, copies fewer
  147. /// bytes.
  148. /// </summary>
  149. /// <param name="output">
  150. /// the buffer.
  151. /// </param>
  152. /// <param name="offset">
  153. /// the offset in the buffer.
  154. /// </param>
  155. /// <param name="length">
  156. /// the length to copy, 0 is allowed.
  157. /// </param>
  158. /// <returns>
  159. /// the number of bytes copied, 0 if no byte is available.
  160. /// </returns>
  161. public int CopyBytes(byte[] output, int offset, int length)
  162. {
  163. if (length < 0) {
  164. throw new ArgumentOutOfRangeException("length negative");
  165. }
  166. if ((bits_in_buffer & 7) != 0) {
  167. /* bits_in_buffer may only be 0 or 8 */
  168. throw new InvalidOperationException("Bit buffer is not aligned!");
  169. }
  170. int count = 0;
  171. while (bits_in_buffer > 0 && length > 0) {
  172. output[offset++] = (byte) buffer;
  173. buffer >>= 8;
  174. bits_in_buffer -= 8;
  175. length--;
  176. count++;
  177. }
  178. if (length == 0) {
  179. return count;
  180. }
  181. int avail = window_end - window_start;
  182. if (length > avail) {
  183. length = avail;
  184. }
  185. System.Array.Copy(window, window_start, output, offset, length);
  186. window_start += length;
  187. if (((window_start - window_end) & 1) != 0) {
  188. /* We always want an even number of bytes in input, see peekBits */
  189. buffer = (uint)(window[window_start++] & 0xff);
  190. bits_in_buffer = 8;
  191. }
  192. return count + length;
  193. }
  194. public StreamManipulator()
  195. {
  196. }
  197. public void Reset()
  198. {
  199. buffer = (uint)(window_start = window_end = bits_in_buffer = 0);
  200. }
  201. public void SetInput(byte[] buf, int off, int len)
  202. {
  203. if (window_start < window_end) {
  204. throw new InvalidOperationException("Old input was not completely processed");
  205. }
  206. int end = off + len;
  207. /* We want to throw an ArrayIndexOutOfBoundsException early. The
  208. * check is very tricky: it also handles integer wrap around.
  209. */
  210. if (0 > off || off > end || end > buf.Length) {
  211. throw new ArgumentOutOfRangeException();
  212. }
  213. if ((len & 1) != 0) {
  214. /* We always want an even number of bytes in input, see peekBits */
  215. buffer |= (uint)((buf[off++] & 0xff) << bits_in_buffer);
  216. bits_in_buffer += 8;
  217. }
  218. window = buf;
  219. window_start = off;
  220. window_end = end;
  221. }
  222. }
  223. }