ZOutputStream.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.ZOutputStream.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 ZOutputStream
  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 output;
  54. protected bool closed;
  55. public ZOutputStream(Stream output)
  56. : this(output, false)
  57. {
  58. }
  59. public ZOutputStream(Stream output, bool nowrap)
  60. : this(output, GetDefaultZStream(nowrap))
  61. {
  62. }
  63. public ZOutputStream(Stream output, ZStream z)
  64. : base()
  65. {
  66. Debug.Assert(output.CanWrite);
  67. if (z == null)
  68. {
  69. z = new ZStream();
  70. }
  71. if (z.istate == null && z.dstate == null)
  72. {
  73. z.inflateInit();
  74. }
  75. this.output = output;
  76. this.compress = (z.istate == null);
  77. this.z = z;
  78. }
  79. public ZOutputStream(Stream output, int level)
  80. : this(output, level, false)
  81. {
  82. }
  83. public ZOutputStream(Stream output, int level, bool nowrap)
  84. : base()
  85. {
  86. Debug.Assert(output.CanWrite);
  87. this.output = output;
  88. this.compress = true;
  89. this.z = new ZStream();
  90. this.z.deflateInit(level, nowrap);
  91. }
  92. public sealed override bool CanRead { get { return false; } }
  93. public sealed override bool CanSeek { get { return false; } }
  94. public sealed override bool CanWrite { get { return !closed; } }
  95. #if PORTABLE || NETFX_CORE
  96. protected override void Dispose(bool disposing)
  97. {
  98. if (disposing)
  99. {
  100. if (closed)
  101. return;
  102. DoClose();
  103. }
  104. base.Dispose(disposing);
  105. }
  106. #else
  107. public override void Close()
  108. {
  109. if (closed)
  110. return;
  111. DoClose();
  112. base.Close();
  113. }
  114. #endif
  115. private void DoClose()
  116. {
  117. try
  118. {
  119. try
  120. {
  121. Finish();
  122. }
  123. catch (IOException)
  124. {
  125. // Ignore
  126. }
  127. }
  128. finally
  129. {
  130. this.closed = true;
  131. End();
  132. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(output);
  133. output = null;
  134. }
  135. }
  136. public virtual void End()
  137. {
  138. if (z == null)
  139. return;
  140. if (compress)
  141. z.deflateEnd();
  142. else
  143. z.inflateEnd();
  144. z.free();
  145. z = null;
  146. }
  147. public virtual void Finish()
  148. {
  149. do
  150. {
  151. z.next_out = buf;
  152. z.next_out_index = 0;
  153. z.avail_out = buf.Length;
  154. int err = compress
  155. ? z.deflate(JZlib.Z_FINISH)
  156. : z.inflate(JZlib.Z_FINISH);
  157. if (err != JZlib.Z_STREAM_END && err != JZlib.Z_OK)
  158. // TODO
  159. // throw new ZStreamException((compress?"de":"in")+"flating: "+z.msg);
  160. throw new IOException((compress ? "de" : "in") + "flating: " + z.msg);
  161. int count = buf.Length - z.avail_out;
  162. if (count > 0)
  163. {
  164. output.Write(buf, 0, count);
  165. }
  166. }
  167. while (z.avail_in > 0 || z.avail_out == 0);
  168. Flush();
  169. }
  170. public override void Flush()
  171. {
  172. output.Flush();
  173. }
  174. public virtual int FlushMode
  175. {
  176. get { return flushLevel; }
  177. set { this.flushLevel = value; }
  178. }
  179. public sealed override long Length { get { throw new NotSupportedException(); } }
  180. public sealed override long Position
  181. {
  182. get { throw new NotSupportedException(); }
  183. set { throw new NotSupportedException(); }
  184. }
  185. public sealed override int Read(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
  186. public sealed override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
  187. public sealed override void SetLength(long value) { throw new NotSupportedException(); }
  188. public virtual long TotalIn
  189. {
  190. get { return z.total_in; }
  191. }
  192. public virtual long TotalOut
  193. {
  194. get { return z.total_out; }
  195. }
  196. public override void Write(byte[] b, int off, int len)
  197. {
  198. if (len == 0)
  199. return;
  200. z.next_in = b;
  201. z.next_in_index = off;
  202. z.avail_in = len;
  203. do
  204. {
  205. z.next_out = buf;
  206. z.next_out_index = 0;
  207. z.avail_out = buf.Length;
  208. int err = compress
  209. ? z.deflate(flushLevel)
  210. : z.inflate(flushLevel);
  211. if (err != JZlib.Z_OK)
  212. // TODO
  213. // throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
  214. throw new IOException((compress ? "de" : "in") + "flating: " + z.msg);
  215. output.Write(buf, 0, buf.Length - z.avail_out);
  216. }
  217. while (z.avail_in > 0 || z.avail_out == 0);
  218. }
  219. public override void WriteByte(byte b)
  220. {
  221. buf1[0] = b;
  222. Write(buf1, 0, 1);
  223. }
  224. }
  225. }
  226. #pragma warning restore
  227. #endif