You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
7.9 KiB

  1. // Copyright (c) 2006, ComponentAce
  2. // http://www.componentace.com
  3. // All rights reserved.
  4. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  5. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  6. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  7. // Neither the name of ComponentAce nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  8. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9. /*
  10. Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice,
  14. this list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in
  17. the documentation and/or other materials provided with the distribution.
  18. 3. The names of the authors may not be used to endorse or promote products
  19. derived from this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  21. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  22. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  23. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  26. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  29. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * This program is based on zlib-1.1.3, so all credit should go authors
  33. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  34. * and contributors of zlib.
  35. */
  36. using System;
  37. namespace zlib
  38. {
  39. sealed class ZStream
  40. {
  41. private const int MAX_WBITS = 15; // 32K LZ77 window
  42. private static readonly int DEF_WBITS = MAX_WBITS;
  43. private const int Z_NO_FLUSH = 0;
  44. private const int Z_PARTIAL_FLUSH = 1;
  45. private const int Z_SYNC_FLUSH = 2;
  46. private const int Z_FULL_FLUSH = 3;
  47. private const int Z_FINISH = 4;
  48. private const int MAX_MEM_LEVEL = 9;
  49. private const int Z_OK = 0;
  50. private const int Z_STREAM_END = 1;
  51. private const int Z_NEED_DICT = 2;
  52. private const int Z_ERRNO = -1;
  53. private const int Z_STREAM_ERROR = -2;
  54. private const int Z_DATA_ERROR = -3;
  55. private const int Z_MEM_ERROR = -4;
  56. private const int Z_BUF_ERROR = -5;
  57. private const int Z_VERSION_ERROR = -6;
  58. public byte[] next_in; // next input byte
  59. public int next_in_index;
  60. public int avail_in; // number of bytes available at next_in
  61. public long total_in; // total nb of input bytes read so far
  62. public byte[] next_out; // next output byte should be put there
  63. public int next_out_index;
  64. public int avail_out; // remaining free space at next_out
  65. public long total_out; // total nb of bytes output so far
  66. public System.String msg;
  67. internal Deflate dstate;
  68. internal Inflate istate;
  69. internal int data_type; // best guess about the data type: ascii or binary
  70. public long adler;
  71. internal Adler32 _adler = new Adler32();
  72. public int inflateInit()
  73. {
  74. return inflateInit(DEF_WBITS);
  75. }
  76. public int inflateInit(int w)
  77. {
  78. istate = new Inflate();
  79. return istate.inflateInit(this, w);
  80. }
  81. public int inflate(int f)
  82. {
  83. if (istate == null)
  84. return Z_STREAM_ERROR;
  85. return istate.inflate(this, f);
  86. }
  87. public int inflateEnd()
  88. {
  89. if (istate == null)
  90. return Z_STREAM_ERROR;
  91. int ret = istate.inflateEnd(this);
  92. istate = null;
  93. return ret;
  94. }
  95. public int inflateSync()
  96. {
  97. if (istate == null)
  98. return Z_STREAM_ERROR;
  99. return istate.inflateSync(this);
  100. }
  101. public int inflateSetDictionary(byte[] dictionary, int dictLength)
  102. {
  103. if (istate == null)
  104. return Z_STREAM_ERROR;
  105. return istate.inflateSetDictionary(this, dictionary, dictLength);
  106. }
  107. public int deflateInit(int level)
  108. {
  109. return deflateInit(level, MAX_WBITS);
  110. }
  111. public int deflateInit(int level, int bits)
  112. {
  113. dstate = new Deflate();
  114. return dstate.deflateInit(this, level, bits);
  115. }
  116. public int deflate(int flush)
  117. {
  118. if (dstate == null)
  119. {
  120. return Z_STREAM_ERROR;
  121. }
  122. return dstate.deflate(this, flush);
  123. }
  124. public int deflateEnd()
  125. {
  126. if (dstate == null)
  127. return Z_STREAM_ERROR;
  128. int ret = dstate.deflateEnd();
  129. dstate = null;
  130. return ret;
  131. }
  132. public int deflateParams(int level, int strategy)
  133. {
  134. if (dstate == null)
  135. return Z_STREAM_ERROR;
  136. return dstate.deflateParams(this, level, strategy);
  137. }
  138. public int deflateSetDictionary(byte[] dictionary, int dictLength)
  139. {
  140. if (dstate == null)
  141. return Z_STREAM_ERROR;
  142. return dstate.deflateSetDictionary(this, dictionary, dictLength);
  143. }
  144. // Flush as much pending output as possible. All deflate() output goes
  145. // through this function so some applications may wish to modify it
  146. // to avoid allocating a large strm->next_out buffer and copying into it.
  147. // (See also read_buf()).
  148. internal void flush_pending()
  149. {
  150. int len = dstate.pending;
  151. if (len > avail_out)
  152. len = avail_out;
  153. if (len == 0)
  154. return;
  155. if (dstate.pending_buf.Length <= dstate.pending_out || next_out.Length <= next_out_index || dstate.pending_buf.Length < (dstate.pending_out + len) || next_out.Length < (next_out_index + len))
  156. {
  157. Apewer.Logger.Internals.Debug(this, dstate.pending_buf.Length + ", " + dstate.pending_out + ", " + next_out.Length + ", " + next_out_index + ", " + len);
  158. Apewer.Logger.Internals.Debug(this, "avail_out=" + avail_out);
  159. }
  160. Array.Copy(dstate.pending_buf, dstate.pending_out, next_out, next_out_index, len);
  161. next_out_index += len;
  162. dstate.pending_out += len;
  163. total_out += len;
  164. avail_out -= len;
  165. dstate.pending -= len;
  166. if (dstate.pending == 0)
  167. {
  168. dstate.pending_out = 0;
  169. }
  170. }
  171. // Read a new buffer from the current input stream, update the adler32
  172. // and total number of bytes read. All deflate() input goes through
  173. // this function so some applications may wish to modify it to avoid
  174. // allocating a large strm->next_in buffer and copying from it.
  175. // (See also flush_pending()).
  176. internal int read_buf(byte[] buf, int start, int size)
  177. {
  178. int len = avail_in;
  179. if (len > size)
  180. len = size;
  181. if (len == 0)
  182. return 0;
  183. avail_in -= len;
  184. if (dstate.noheader == 0)
  185. {
  186. adler = _adler.adler32(adler, next_in, next_in_index, len);
  187. }
  188. Array.Copy(next_in, next_in_index, buf, start, len);
  189. next_in_index += len;
  190. total_in += len;
  191. return len;
  192. }
  193. public void free()
  194. {
  195. next_in = null;
  196. next_out = null;
  197. msg = null;
  198. _adler = null;
  199. }
  200. }
  201. }