StreamManipulator.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // StreamManipulator.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. /// This class allows us to retrieve a specified number of bits from
  43. /// the input buffer, as well as copy big byte blocks.
  44. ///
  45. /// It uses an int buffer to store up to 31 bits for direct
  46. /// manipulation. This guarantees that we can get at least 16 bits,
  47. /// but we only need at most 15, so this is all safe.
  48. ///
  49. /// There are some optimizations in this class, for example, you must
  50. /// never peek more than 8 bits more than needed, and you must first
  51. /// peek bits before you may drop them. This is not a general purpose
  52. /// class but optimized for the behaviour of the Inflater.
  53. ///
  54. /// authors of the original java version : John Leuner, Jochen Hoenicke
  55. /// </summary>
  56. public class StreamManipulator
  57. {
  58. #region Constructors
  59. /// <summary>
  60. /// Constructs a default StreamManipulator with all buffers empty
  61. /// </summary>
  62. public StreamManipulator()
  63. {
  64. }
  65. #endregion
  66. /// <summary>
  67. /// Get the next sequence of bits but don't increase input pointer. bitCount must be
  68. /// less or equal 16 and if this call succeeds, you must drop
  69. /// at least n - 8 bits in the next call.
  70. /// </summary>
  71. /// <param name="bitCount">The number of bits to peek.</param>
  72. /// <returns>
  73. /// the value of the bits, or -1 if not enough bits available. */
  74. /// </returns>
  75. // public int PeekBits(int bitCount)
  76. // {
  77. // if (bitsInBuffer_ < bitCount) {
  78. // if (windowStart_ == windowEnd_) {
  79. // return -1; // ok
  80. // }
  81. // buffer_ |= (uint)((window_[windowStart_++] & 0xff |
  82. // (window_[windowStart_++] & 0xff) << 8) << bitsInBuffer_);
  83. // bitsInBuffer_ += 16;
  84. // }
  85. // return (int)(buffer_ & ((1 << bitCount) - 1));
  86. // }
  87. // 由于il2cpp在4.6.3下存在如下bug导致sharpzipLib无法使用:
  88. // bug如下:
  89. // byte b = 205;
  90. // Debug.Log( (b & 0xff) << 8)
  91. // 在il2cpp下表达式计算是0
  92. //
  93. // 修改sharpzipLib源码,规避上述bug
  94. // 1.StreamManipulator类PeekBits函数修改如下
  95. public int PeekBits(int bitCount)
  96. {
  97. if (bitsInBuffer_ < bitCount) {
  98. if (windowStart_ == windowEnd_) {
  99. return -1; // ok
  100. }
  101. int tmp1 = window_[windowStart_++] & 0xff;
  102. int tmp2 = window_[windowStart_++] & 0xff;
  103. buffer_ |= (uint)((tmp1 | tmp2 << 8) << bitsInBuffer_);
  104. bitsInBuffer_ += 16;
  105. }
  106. return (int)(buffer_ & ((1 << bitCount) - 1));
  107. }
  108. /// <summary>
  109. /// Drops the next n bits from the input. You should have called PeekBits
  110. /// with a bigger or equal n before, to make sure that enough bits are in
  111. /// the bit buffer.
  112. /// </summary>
  113. /// <param name="bitCount">The number of bits to drop.</param>
  114. public void DropBits(int bitCount)
  115. {
  116. buffer_ >>= bitCount;
  117. bitsInBuffer_ -= bitCount;
  118. }
  119. /// <summary>
  120. /// Gets the next n bits and increases input pointer. This is equivalent
  121. /// to <see cref="PeekBits"/> followed by <see cref="DropBits"/>, except for correct error handling.
  122. /// </summary>
  123. /// <param name="bitCount">The number of bits to retrieve.</param>
  124. /// <returns>
  125. /// the value of the bits, or -1 if not enough bits available.
  126. /// </returns>
  127. public int GetBits(int bitCount)
  128. {
  129. int bits = PeekBits(bitCount);
  130. if (bits >= 0) {
  131. DropBits(bitCount);
  132. }
  133. return bits;
  134. }
  135. /// <summary>
  136. /// Gets the number of bits available in the bit buffer. This must be
  137. /// only called when a previous PeekBits() returned -1.
  138. /// </summary>
  139. /// <returns>
  140. /// the number of bits available.
  141. /// </returns>
  142. public int AvailableBits {
  143. get {
  144. return bitsInBuffer_;
  145. }
  146. }
  147. /// <summary>
  148. /// Gets the number of bytes available.
  149. /// </summary>
  150. /// <returns>
  151. /// The number of bytes available.
  152. /// </returns>
  153. public int AvailableBytes {
  154. get {
  155. return windowEnd_ - windowStart_ + (bitsInBuffer_ >> 3);
  156. }
  157. }
  158. /// <summary>
  159. /// Skips to the next byte boundary.
  160. /// </summary>
  161. public void SkipToByteBoundary()
  162. {
  163. buffer_ >>= (bitsInBuffer_ & 7);
  164. bitsInBuffer_ &= ~7;
  165. }
  166. /// <summary>
  167. /// Returns true when SetInput can be called
  168. /// </summary>
  169. public bool IsNeedingInput {
  170. get {
  171. return windowStart_ == windowEnd_;
  172. }
  173. }
  174. /// <summary>
  175. /// Copies bytes from input buffer to output buffer starting
  176. /// at output[offset]. You have to make sure, that the buffer is
  177. /// byte aligned. If not enough bytes are available, copies fewer
  178. /// bytes.
  179. /// </summary>
  180. /// <param name="output">
  181. /// The buffer to copy bytes to.
  182. /// </param>
  183. /// <param name="offset">
  184. /// The offset in the buffer at which copying starts
  185. /// </param>
  186. /// <param name="length">
  187. /// The length to copy, 0 is allowed.
  188. /// </param>
  189. /// <returns>
  190. /// The number of bytes copied, 0 if no bytes were available.
  191. /// </returns>
  192. /// <exception cref="ArgumentOutOfRangeException">
  193. /// Length is less than zero
  194. /// </exception>
  195. /// <exception cref="InvalidOperationException">
  196. /// Bit buffer isnt byte aligned
  197. /// </exception>
  198. public int CopyBytes(byte[] output, int offset, int length)
  199. {
  200. if (length < 0) {
  201. throw new ArgumentOutOfRangeException("length");
  202. }
  203. if ((bitsInBuffer_ & 7) != 0) {
  204. // bits_in_buffer may only be 0 or a multiple of 8
  205. throw new InvalidOperationException("Bit buffer is not byte aligned!");
  206. }
  207. int count = 0;
  208. while ((bitsInBuffer_ > 0) && (length > 0)) {
  209. output[offset++] = (byte) buffer_;
  210. buffer_ >>= 8;
  211. bitsInBuffer_ -= 8;
  212. length--;
  213. count++;
  214. }
  215. if (length == 0) {
  216. return count;
  217. }
  218. int avail = windowEnd_ - windowStart_;
  219. if (length > avail) {
  220. length = avail;
  221. }
  222. System.Array.Copy(window_, windowStart_, output, offset, length);
  223. windowStart_ += length;
  224. if (((windowStart_ - windowEnd_) & 1) != 0) {
  225. // We always want an even number of bytes in input, see peekBits
  226. buffer_ = (uint)(window_[windowStart_++] & 0xff);
  227. bitsInBuffer_ = 8;
  228. }
  229. return count + length;
  230. }
  231. /// <summary>
  232. /// Resets state and empties internal buffers
  233. /// </summary>
  234. public void Reset()
  235. {
  236. buffer_ = 0;
  237. windowStart_ = windowEnd_ = bitsInBuffer_ = 0;
  238. }
  239. /// <summary>
  240. /// Add more input for consumption.
  241. /// Only call when IsNeedingInput returns true
  242. /// </summary>
  243. /// <param name="buffer">data to be input</param>
  244. /// <param name="offset">offset of first byte of input</param>
  245. /// <param name="count">number of bytes of input to add.</param>
  246. public void SetInput(byte[] buffer, int offset, int count)
  247. {
  248. if ( buffer == null ) {
  249. throw new ArgumentNullException("buffer");
  250. }
  251. if ( offset < 0 ) {
  252. #if NETCF_1_0
  253. throw new ArgumentOutOfRangeException("offset");
  254. #else
  255. throw new ArgumentOutOfRangeException("offset", "Cannot be negative");
  256. #endif
  257. }
  258. if ( count < 0 ) {
  259. #if NETCF_1_0
  260. throw new ArgumentOutOfRangeException("count");
  261. #else
  262. throw new ArgumentOutOfRangeException("count", "Cannot be negative");
  263. #endif
  264. }
  265. if (windowStart_ < windowEnd_) {
  266. throw new InvalidOperationException("Old input was not completely processed");
  267. }
  268. int end = offset + count;
  269. // We want to throw an ArrayIndexOutOfBoundsException early.
  270. // Note the check also handles integer wrap around.
  271. if ((offset > end) || (end > buffer.Length) ) {
  272. throw new ArgumentOutOfRangeException("count");
  273. }
  274. if ((count & 1) != 0) {
  275. // We always want an even number of bytes in input, see PeekBits
  276. buffer_ |= (uint)((buffer[offset++] & 0xff) << bitsInBuffer_);
  277. bitsInBuffer_ += 8;
  278. }
  279. window_ = buffer;
  280. windowStart_ = offset;
  281. windowEnd_ = end;
  282. }
  283. #region Instance Fields
  284. private byte[] window_;
  285. private int windowStart_;
  286. private int windowEnd_;
  287. private uint buffer_;
  288. private int bitsInBuffer_;
  289. #endregion
  290. }
  291. }