ZOutputStream.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  36. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Zlib
  37. {
  38. public class ZOutputStream
  39. : BaseOutputStream
  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 output;
  55. protected bool closed;
  56. public ZOutputStream(Stream output)
  57. : this(output, false)
  58. {
  59. }
  60. public ZOutputStream(Stream output, bool nowrap)
  61. : this(output, GetDefaultZStream(nowrap))
  62. {
  63. }
  64. public ZOutputStream(Stream output, ZStream z)
  65. : base()
  66. {
  67. Debug.Assert(output.CanWrite);
  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.output = output;
  77. this.compress = (z.istate == null);
  78. this.z = z;
  79. }
  80. public ZOutputStream(Stream output, int level)
  81. : this(output, level, false)
  82. {
  83. }
  84. public ZOutputStream(Stream output, int level, bool nowrap)
  85. : base()
  86. {
  87. Debug.Assert(output.CanWrite);
  88. this.output = output;
  89. this.compress = true;
  90. this.z = new ZStream();
  91. this.z.deflateInit(level, nowrap);
  92. }
  93. protected void Detach(bool disposing)
  94. {
  95. if (disposing)
  96. {
  97. if (!closed)
  98. {
  99. try
  100. {
  101. try
  102. {
  103. Finish();
  104. }
  105. catch (IOException)
  106. {
  107. // Ignore
  108. }
  109. }
  110. finally
  111. {
  112. this.closed = true;
  113. End();
  114. output = null;
  115. }
  116. }
  117. }
  118. base.Dispose(disposing);
  119. }
  120. protected override void Dispose(bool disposing)
  121. {
  122. if (disposing)
  123. {
  124. if (!closed)
  125. {
  126. try
  127. {
  128. try
  129. {
  130. Finish();
  131. }
  132. catch (IOException)
  133. {
  134. // Ignore
  135. }
  136. }
  137. finally
  138. {
  139. this.closed = true;
  140. End();
  141. output.Dispose();
  142. output = null;
  143. }
  144. }
  145. }
  146. base.Dispose(disposing);
  147. }
  148. public virtual void End()
  149. {
  150. if (z == null)
  151. return;
  152. if (compress)
  153. z.deflateEnd();
  154. else
  155. z.inflateEnd();
  156. z.free();
  157. z = null;
  158. }
  159. public virtual void Finish()
  160. {
  161. do
  162. {
  163. z.next_out = buf;
  164. z.next_out_index = 0;
  165. z.avail_out = buf.Length;
  166. int err = compress
  167. ? z.deflate(JZlib.Z_FINISH)
  168. : z.inflate(JZlib.Z_FINISH);
  169. if (err != JZlib.Z_STREAM_END && err != JZlib.Z_OK)
  170. // TODO
  171. //throw new ZStreamException((compress?"de":"in")+"flating: "+z.msg);
  172. throw new IOException((compress ? "de" : "in") + "flating: " + z.msg);
  173. int count = buf.Length - z.avail_out;
  174. if (count > 0)
  175. {
  176. output.Write(buf, 0, count);
  177. }
  178. }
  179. while (z.avail_in > 0 || z.avail_out == 0);
  180. Flush();
  181. }
  182. public override void Flush()
  183. {
  184. output.Flush();
  185. }
  186. public virtual int FlushMode
  187. {
  188. get { return flushLevel; }
  189. set { this.flushLevel = value; }
  190. }
  191. public virtual long TotalIn
  192. {
  193. get { return z.total_in; }
  194. }
  195. public virtual long TotalOut
  196. {
  197. get { return z.total_out; }
  198. }
  199. public override void Write(byte[] buffer, int offset, int count)
  200. {
  201. Streams.ValidateBufferArguments(buffer, offset, count);
  202. if (count == 0)
  203. return;
  204. z.next_in = buffer;
  205. z.next_in_index = offset;
  206. z.avail_in = count;
  207. do
  208. {
  209. z.next_out = buf;
  210. z.next_out_index = 0;
  211. z.avail_out = buf.Length;
  212. int err = compress
  213. ? z.deflate(flushLevel)
  214. : z.inflate(flushLevel);
  215. if (err != JZlib.Z_OK)
  216. // TODO
  217. //throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
  218. throw new IOException((compress ? "de" : "in") + "flating: " + z.msg);
  219. output.Write(buf, 0, buf.Length - z.avail_out);
  220. }
  221. while (z.avail_in > 0 || z.avail_out == 0);
  222. }
  223. public override void WriteByte(byte value)
  224. {
  225. buf1[0] = value;
  226. Write(buf1, 0, 1);
  227. }
  228. }
  229. public class ZOutputStreamLeaveOpen
  230. : ZOutputStream
  231. {
  232. public ZOutputStreamLeaveOpen(Stream output)
  233. : base(output)
  234. {
  235. }
  236. public ZOutputStreamLeaveOpen(Stream output, bool nowrap)
  237. : base(output, nowrap)
  238. {
  239. }
  240. public ZOutputStreamLeaveOpen(Stream output, ZStream z)
  241. : base(output, z)
  242. {
  243. }
  244. public ZOutputStreamLeaveOpen(Stream output, int level)
  245. : base(output, level)
  246. {
  247. }
  248. public ZOutputStreamLeaveOpen(Stream output, int level, bool nowrap)
  249. : base(output, level, nowrap)
  250. {
  251. }
  252. protected override void Dispose(bool disposing)
  253. {
  254. Detach(disposing);
  255. }
  256. }
  257. }
  258. #pragma warning restore
  259. #endif