ZStream.cs 8.0 KB

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