ZOutputStream.cs 7.0 KB

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