BcpgOutputStream.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Bcpg
  8. {
  9. /// <remarks>Basic output stream.</remarks>
  10. public class BcpgOutputStream
  11. : BaseOutputStream
  12. {
  13. internal static BcpgOutputStream Wrap(
  14. Stream outStr)
  15. {
  16. if (outStr is BcpgOutputStream)
  17. {
  18. return (BcpgOutputStream) outStr;
  19. }
  20. return new BcpgOutputStream(outStr);
  21. }
  22. private Stream outStr;
  23. private byte[] partialBuffer;
  24. private int partialBufferLength;
  25. private int partialPower;
  26. private int partialOffset;
  27. private const int BufferSizePower = 16; // 2^16 size buffer on long files
  28. /// <summary>Create a stream representing a general packet.</summary>
  29. /// <param name="outStr">Output stream to write to.</param>
  30. public BcpgOutputStream(
  31. Stream outStr)
  32. {
  33. if (outStr == null)
  34. throw new ArgumentNullException("outStr");
  35. this.outStr = outStr;
  36. }
  37. /// <summary>Create a stream representing an old style partial object.</summary>
  38. /// <param name="outStr">Output stream to write to.</param>
  39. /// <param name="tag">The packet tag for the object.</param>
  40. public BcpgOutputStream(
  41. Stream outStr,
  42. PacketTag tag)
  43. {
  44. if (outStr == null)
  45. throw new ArgumentNullException("outStr");
  46. this.outStr = outStr;
  47. this.WriteHeader(tag, true, true, 0);
  48. }
  49. /// <summary>Create a stream representing a general packet.</summary>
  50. /// <param name="outStr">Output stream to write to.</param>
  51. /// <param name="tag">Packet tag.</param>
  52. /// <param name="length">Size of chunks making up the packet.</param>
  53. /// <param name="oldFormat">If true, the header is written out in old format.</param>
  54. public BcpgOutputStream(
  55. Stream outStr,
  56. PacketTag tag,
  57. long length,
  58. bool oldFormat)
  59. {
  60. if (outStr == null)
  61. throw new ArgumentNullException("outStr");
  62. this.outStr = outStr;
  63. if (length > 0xFFFFFFFFL)
  64. {
  65. this.WriteHeader(tag, false, true, 0);
  66. this.partialBufferLength = 1 << BufferSizePower;
  67. this.partialBuffer = new byte[partialBufferLength];
  68. this.partialPower = BufferSizePower;
  69. this.partialOffset = 0;
  70. }
  71. else
  72. {
  73. this.WriteHeader(tag, oldFormat, false, length);
  74. }
  75. }
  76. /// <summary>Create a new style partial input stream buffered into chunks.</summary>
  77. /// <param name="outStr">Output stream to write to.</param>
  78. /// <param name="tag">Packet tag.</param>
  79. /// <param name="length">Size of chunks making up the packet.</param>
  80. public BcpgOutputStream(
  81. Stream outStr,
  82. PacketTag tag,
  83. long length)
  84. {
  85. if (outStr == null)
  86. throw new ArgumentNullException("outStr");
  87. this.outStr = outStr;
  88. this.WriteHeader(tag, false, false, length);
  89. }
  90. /// <summary>Create a new style partial input stream buffered into chunks.</summary>
  91. /// <param name="outStr">Output stream to write to.</param>
  92. /// <param name="tag">Packet tag.</param>
  93. /// <param name="buffer">Buffer to use for collecting chunks.</param>
  94. public BcpgOutputStream(
  95. Stream outStr,
  96. PacketTag tag,
  97. byte[] buffer)
  98. {
  99. if (outStr == null)
  100. throw new ArgumentNullException("outStr");
  101. this.outStr = outStr;
  102. this.WriteHeader(tag, false, true, 0);
  103. this.partialBuffer = buffer;
  104. uint length = (uint) partialBuffer.Length;
  105. for (partialPower = 0; length != 1; partialPower++)
  106. {
  107. length >>= 1;
  108. }
  109. if (partialPower > 30)
  110. {
  111. throw new IOException("Buffer cannot be greater than 2^30 in length.");
  112. }
  113. this.partialBufferLength = 1 << partialPower;
  114. this.partialOffset = 0;
  115. }
  116. private void WriteNewPacketLength(
  117. long bodyLen)
  118. {
  119. if (bodyLen < 192)
  120. {
  121. outStr.WriteByte((byte)bodyLen);
  122. }
  123. else if (bodyLen <= 8383)
  124. {
  125. bodyLen -= 192;
  126. outStr.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
  127. outStr.WriteByte((byte)bodyLen);
  128. }
  129. else
  130. {
  131. outStr.WriteByte(0xff);
  132. outStr.WriteByte((byte)(bodyLen >> 24));
  133. outStr.WriteByte((byte)(bodyLen >> 16));
  134. outStr.WriteByte((byte)(bodyLen >> 8));
  135. outStr.WriteByte((byte)bodyLen);
  136. }
  137. }
  138. private void WriteHeader(
  139. PacketTag tag,
  140. bool oldPackets,
  141. bool partial,
  142. long bodyLen)
  143. {
  144. int hdr = 0x80;
  145. if (partialBuffer != null)
  146. {
  147. PartialFlush(true);
  148. partialBuffer = null;
  149. }
  150. if (oldPackets)
  151. {
  152. hdr |= ((int) tag) << 2;
  153. if (partial)
  154. {
  155. this.WriteByte((byte)(hdr | 0x03));
  156. }
  157. else
  158. {
  159. if (bodyLen <= 0xff)
  160. {
  161. this.WriteByte((byte) hdr);
  162. this.WriteByte((byte)bodyLen);
  163. }
  164. else if (bodyLen <= 0xffff)
  165. {
  166. this.WriteByte((byte)(hdr | 0x01));
  167. this.WriteByte((byte)(bodyLen >> 8));
  168. this.WriteByte((byte)(bodyLen));
  169. }
  170. else
  171. {
  172. this.WriteByte((byte)(hdr | 0x02));
  173. this.WriteByte((byte)(bodyLen >> 24));
  174. this.WriteByte((byte)(bodyLen >> 16));
  175. this.WriteByte((byte)(bodyLen >> 8));
  176. this.WriteByte((byte)bodyLen);
  177. }
  178. }
  179. }
  180. else
  181. {
  182. hdr |= 0x40 | (int) tag;
  183. this.WriteByte((byte) hdr);
  184. if (partial)
  185. {
  186. partialOffset = 0;
  187. }
  188. else
  189. {
  190. this.WriteNewPacketLength(bodyLen);
  191. }
  192. }
  193. }
  194. private void PartialFlush(
  195. bool isLast)
  196. {
  197. if (isLast)
  198. {
  199. WriteNewPacketLength(partialOffset);
  200. outStr.Write(partialBuffer, 0, partialOffset);
  201. }
  202. else
  203. {
  204. outStr.WriteByte((byte)(0xE0 | partialPower));
  205. outStr.Write(partialBuffer, 0, partialBufferLength);
  206. }
  207. partialOffset = 0;
  208. }
  209. private void WritePartial(
  210. byte b)
  211. {
  212. if (partialOffset == partialBufferLength)
  213. {
  214. PartialFlush(false);
  215. }
  216. partialBuffer[partialOffset++] = b;
  217. }
  218. private void WritePartial(
  219. byte[] buffer,
  220. int off,
  221. int len)
  222. {
  223. if (partialOffset == partialBufferLength)
  224. {
  225. PartialFlush(false);
  226. }
  227. if (len <= (partialBufferLength - partialOffset))
  228. {
  229. Array.Copy(buffer, off, partialBuffer, partialOffset, len);
  230. partialOffset += len;
  231. }
  232. else
  233. {
  234. int diff = partialBufferLength - partialOffset;
  235. Array.Copy(buffer, off, partialBuffer, partialOffset, diff);
  236. off += diff;
  237. len -= diff;
  238. PartialFlush(false);
  239. while (len > partialBufferLength)
  240. {
  241. Array.Copy(buffer, off, partialBuffer, 0, partialBufferLength);
  242. off += partialBufferLength;
  243. len -= partialBufferLength;
  244. PartialFlush(false);
  245. }
  246. Array.Copy(buffer, off, partialBuffer, 0, len);
  247. partialOffset += len;
  248. }
  249. }
  250. public override void WriteByte(
  251. byte value)
  252. {
  253. if (partialBuffer != null)
  254. {
  255. WritePartial(value);
  256. }
  257. else
  258. {
  259. outStr.WriteByte(value);
  260. }
  261. }
  262. public override void Write(
  263. byte[] buffer,
  264. int offset,
  265. int count)
  266. {
  267. if (partialBuffer != null)
  268. {
  269. WritePartial(buffer, offset, count);
  270. }
  271. else
  272. {
  273. outStr.Write(buffer, offset, count);
  274. }
  275. }
  276. // Additional helper methods to write primitive types
  277. internal virtual void WriteShort(
  278. short n)
  279. {
  280. this.Write(
  281. (byte)(n >> 8),
  282. (byte)n);
  283. }
  284. internal virtual void WriteInt(
  285. int n)
  286. {
  287. this.Write(
  288. (byte)(n >> 24),
  289. (byte)(n >> 16),
  290. (byte)(n >> 8),
  291. (byte)n);
  292. }
  293. internal virtual void WriteLong(
  294. long n)
  295. {
  296. this.Write(
  297. (byte)(n >> 56),
  298. (byte)(n >> 48),
  299. (byte)(n >> 40),
  300. (byte)(n >> 32),
  301. (byte)(n >> 24),
  302. (byte)(n >> 16),
  303. (byte)(n >> 8),
  304. (byte)n);
  305. }
  306. public void WritePacket(
  307. ContainedPacket p)
  308. {
  309. p.Encode(this);
  310. }
  311. internal void WritePacket(
  312. PacketTag tag,
  313. byte[] body,
  314. bool oldFormat)
  315. {
  316. this.WriteHeader(tag, oldFormat, false, body.Length);
  317. this.Write(body);
  318. }
  319. public void WriteObject(
  320. BcpgObject bcpgObject)
  321. {
  322. bcpgObject.Encode(this);
  323. }
  324. public void WriteObjects(
  325. params BcpgObject[] v)
  326. {
  327. foreach (BcpgObject o in v)
  328. {
  329. o.Encode(this);
  330. }
  331. }
  332. /// <summary>Flush the underlying stream.</summary>
  333. public override void Flush()
  334. {
  335. outStr.Flush();
  336. }
  337. /// <summary>Finish writing out the current packet without closing the underlying stream.</summary>
  338. public void Finish()
  339. {
  340. if (partialBuffer != null)
  341. {
  342. PartialFlush(true);
  343. Array.Clear(partialBuffer, 0, partialBuffer.Length);
  344. partialBuffer = null;
  345. }
  346. }
  347. #if PORTABLE || NETFX_CORE
  348. protected override void Dispose(bool disposing)
  349. {
  350. if (disposing)
  351. {
  352. this.Finish();
  353. outStr.Flush();
  354. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(outStr);
  355. }
  356. base.Dispose(disposing);
  357. }
  358. #else
  359. public override void Close()
  360. {
  361. this.Finish();
  362. outStr.Flush();
  363. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(outStr);
  364. base.Close();
  365. }
  366. #endif
  367. }
  368. }
  369. #pragma warning restore
  370. #endif