ZInputStream.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. /*
  4. Copyright (c) 2001 Lapo Luchini.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. 1. Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the distribution.
  12. 3. The names of the authors may not be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS
  17. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  20. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  23. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. * This program is based on zlib-1.1.3, so all credit should go authors
  27. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  28. * and contributors of zlib.
  29. */
  30. /* This file is a port of jzlib v1.0.7, com.jcraft.jzlib.ZInputStream.java
  31. */
  32. using System;
  33. using System.Diagnostics;
  34. using System.IO;
  35. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  36. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Zlib
  37. {
  38. public class ZInputStream
  39. : BaseInputStream
  40. {
  41. private static ZStream GetDefaultZStream(bool nowrap)
  42. {
  43. ZStream z = new ZStream();
  44. z.inflateInit(nowrap);
  45. return z;
  46. }
  47. private const int BufferSize = 4096;
  48. protected ZStream z;
  49. protected int flushLevel = JZlib.Z_NO_FLUSH;
  50. // TODO Allow custom buf
  51. protected byte[] buf = new byte[BufferSize];
  52. protected byte[] buf1 = new byte[1];
  53. protected bool compress;
  54. protected Stream input;
  55. protected bool closed;
  56. private bool nomoreinput = false;
  57. public ZInputStream(Stream input)
  58. : this(input, false)
  59. {
  60. }
  61. public ZInputStream(Stream input, bool nowrap)
  62. : this(input, GetDefaultZStream(nowrap))
  63. {
  64. }
  65. public ZInputStream(Stream input, ZStream z)
  66. : base()
  67. {
  68. Debug.Assert(input.CanRead);
  69. if (z == null)
  70. {
  71. z = new ZStream();
  72. }
  73. if (z.istate == null && z.dstate == null)
  74. {
  75. z.inflateInit();
  76. }
  77. this.input = input;
  78. this.compress = (z.istate == null);
  79. this.z = z;
  80. this.z.next_in = buf;
  81. this.z.next_in_index = 0;
  82. this.z.avail_in = 0;
  83. }
  84. public ZInputStream(Stream input, int level)
  85. : this(input, level, false)
  86. {
  87. }
  88. public ZInputStream(Stream input, int level, bool nowrap)
  89. {
  90. Debug.Assert(input.CanRead);
  91. this.input = input;
  92. this.compress = true;
  93. this.z = new ZStream();
  94. this.z.deflateInit(level, nowrap);
  95. this.z.next_in = buf;
  96. this.z.next_in_index = 0;
  97. this.z.avail_in = 0;
  98. }
  99. protected override void Dispose(bool disposing)
  100. {
  101. if (disposing)
  102. {
  103. if (!closed)
  104. {
  105. closed = true;
  106. input.Dispose();
  107. }
  108. }
  109. base.Dispose(disposing);
  110. }
  111. public virtual int FlushMode
  112. {
  113. get { return flushLevel; }
  114. set { this.flushLevel = value; }
  115. }
  116. public override int Read(byte[] buffer, int offset, int count)
  117. {
  118. Streams.ValidateBufferArguments(buffer, offset, count);
  119. if (count == 0)
  120. return 0;
  121. z.next_out = buffer;
  122. z.next_out_index = offset;
  123. z.avail_out = count;
  124. int err;
  125. do
  126. {
  127. if (z.avail_in == 0 && !nomoreinput)
  128. {
  129. // if buffer is empty and more input is available, refill it
  130. z.next_in_index = 0;
  131. z.avail_in = input.Read(buf, 0, buf.Length); //(bufsize<z.avail_out ? bufsize : z.avail_out));
  132. if (z.avail_in <= 0)
  133. {
  134. z.avail_in = 0;
  135. nomoreinput = true;
  136. }
  137. }
  138. err = compress
  139. ? z.deflate(flushLevel)
  140. : z.inflate(flushLevel);
  141. if (nomoreinput && err == JZlib.Z_BUF_ERROR)
  142. return 0;
  143. if (err != JZlib.Z_OK && err != JZlib.Z_STREAM_END)
  144. // TODO
  145. //throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
  146. throw new IOException((compress ? "de" : "in") + "flating: " + z.msg);
  147. if ((nomoreinput || err == JZlib.Z_STREAM_END) && z.avail_out == count)
  148. return 0;
  149. }
  150. while (z.avail_out == count && err == JZlib.Z_OK);
  151. //Console.Error.WriteLine("("+(len-z.avail_out)+")");
  152. return count - z.avail_out;
  153. }
  154. public override int ReadByte()
  155. {
  156. if (Read(buf1, 0, 1) <= 0)
  157. return -1;
  158. return buf1[0];
  159. }
  160. // public long skip(long n) throws IOException {
  161. // int len=512;
  162. // if(n<len)
  163. // len=(int)n;
  164. // byte[] tmp=new byte[len];
  165. // return((long)read(tmp));
  166. // }
  167. public virtual long TotalIn
  168. {
  169. get { return z.total_in; }
  170. }
  171. public virtual long TotalOut
  172. {
  173. get { return z.total_out; }
  174. }
  175. }
  176. }
  177. #pragma warning restore
  178. #endif