ZInputStream.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Zlib
  36. {
  37. public class ZInputStream
  38. : Stream
  39. {
  40. private static ZStream GetDefaultZStream(bool nowrap)
  41. {
  42. ZStream z = new ZStream();
  43. z.inflateInit(nowrap);
  44. return z;
  45. }
  46. private const int BufferSize = 4096;
  47. protected ZStream z;
  48. protected int flushLevel = JZlib.Z_NO_FLUSH;
  49. // TODO Allow custom buf
  50. protected byte[] buf = new byte[BufferSize];
  51. protected byte[] buf1 = new byte[1];
  52. protected bool compress;
  53. protected Stream input;
  54. protected bool closed;
  55. private bool nomoreinput = false;
  56. public ZInputStream(Stream input)
  57. : this(input, false)
  58. {
  59. }
  60. public ZInputStream(Stream input, bool nowrap)
  61. : this(input, GetDefaultZStream(nowrap))
  62. {
  63. }
  64. public ZInputStream(Stream input, ZStream z)
  65. : base()
  66. {
  67. Debug.Assert(input.CanRead);
  68. if (z == null)
  69. {
  70. z = new ZStream();
  71. }
  72. if (z.istate == null && z.dstate == null)
  73. {
  74. z.inflateInit();
  75. }
  76. this.input = input;
  77. this.compress = (z.istate == null);
  78. this.z = z;
  79. this.z.next_in = buf;
  80. this.z.next_in_index = 0;
  81. this.z.avail_in = 0;
  82. }
  83. public ZInputStream(Stream input, int level)
  84. : this(input, level, false)
  85. {
  86. }
  87. public ZInputStream(Stream input, int level, bool nowrap)
  88. {
  89. Debug.Assert(input.CanRead);
  90. this.input = input;
  91. this.compress = true;
  92. this.z = new ZStream();
  93. this.z.deflateInit(level, nowrap);
  94. this.z.next_in = buf;
  95. this.z.next_in_index = 0;
  96. this.z.avail_in = 0;
  97. }
  98. /*public int available() throws IOException {
  99. return inf.finished() ? 0 : 1;
  100. }*/
  101. public sealed override bool CanRead { get { return !closed; } }
  102. public sealed override bool CanSeek { get { return false; } }
  103. public sealed override bool CanWrite { get { return false; } }
  104. #if PORTABLE || NETFX_CORE
  105. protected override void Dispose(bool disposing)
  106. {
  107. if (disposing)
  108. {
  109. if (closed)
  110. return;
  111. closed = true;
  112. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(input);
  113. }
  114. base.Dispose(disposing);
  115. }
  116. #else
  117. public override void Close()
  118. {
  119. if (closed)
  120. return;
  121. closed = true;
  122. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(input);
  123. base.Close();
  124. }
  125. #endif
  126. public sealed override void Flush() {}
  127. public virtual int FlushMode
  128. {
  129. get { return flushLevel; }
  130. set { this.flushLevel = value; }
  131. }
  132. public sealed override long Length { get { throw new NotSupportedException(); } }
  133. public sealed override long Position
  134. {
  135. get { throw new NotSupportedException(); }
  136. set { throw new NotSupportedException(); }
  137. }
  138. public override int Read(byte[] b, int off, int len)
  139. {
  140. if (len==0)
  141. return 0;
  142. z.next_out = b;
  143. z.next_out_index = off;
  144. z.avail_out = len;
  145. int err;
  146. do
  147. {
  148. if (z.avail_in == 0 && !nomoreinput)
  149. {
  150. // if buffer is empty and more input is available, refill it
  151. z.next_in_index = 0;
  152. z.avail_in = input.Read(buf, 0, buf.Length); //(bufsize<z.avail_out ? bufsize : z.avail_out));
  153. if (z.avail_in <= 0)
  154. {
  155. z.avail_in = 0;
  156. nomoreinput = true;
  157. }
  158. }
  159. err = compress
  160. ? z.deflate(flushLevel)
  161. : z.inflate(flushLevel);
  162. if (nomoreinput && err == JZlib.Z_BUF_ERROR)
  163. return 0;
  164. if (err != JZlib.Z_OK && err != JZlib.Z_STREAM_END)
  165. // TODO
  166. // throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
  167. throw new IOException((compress ? "de" : "in") + "flating: " + z.msg);
  168. if ((nomoreinput || err == JZlib.Z_STREAM_END) && z.avail_out == len)
  169. return 0;
  170. }
  171. while(z.avail_out == len && err == JZlib.Z_OK);
  172. //Console.Error.WriteLine("("+(len-z.avail_out)+")");
  173. return len - z.avail_out;
  174. }
  175. public override int ReadByte()
  176. {
  177. if (Read(buf1, 0, 1) <= 0)
  178. return -1;
  179. return buf1[0];
  180. }
  181. // public long skip(long n) throws IOException {
  182. // int len=512;
  183. // if(n<len)
  184. // len=(int)n;
  185. // byte[] tmp=new byte[len];
  186. // return((long)read(tmp));
  187. // }
  188. public sealed override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
  189. public sealed override void SetLength(long value) { throw new NotSupportedException(); }
  190. public virtual long TotalIn
  191. {
  192. get { return z.total_in; }
  193. }
  194. public virtual long TotalOut
  195. {
  196. get { return z.total_out; }
  197. }
  198. public sealed override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
  199. }
  200. }
  201. #pragma warning restore
  202. #endif