BcpgOutputStream.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  7. namespace Best.HTTP.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. PartialFlushLast();
  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. {
  196. outStr.WriteByte((byte)(0xE0 | partialPower));
  197. outStr.Write(partialBuffer, 0, partialBufferLength);
  198. partialOffset = 0;
  199. }
  200. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  201. private void PartialFlush(ref ReadOnlySpan<byte> buffer)
  202. {
  203. outStr.WriteByte((byte)(0xE0 | partialPower));
  204. outStr.Write(buffer[..partialBufferLength]);
  205. buffer = buffer[partialBufferLength..];
  206. }
  207. #endif
  208. private void PartialFlushLast()
  209. {
  210. WriteNewPacketLength(partialOffset);
  211. outStr.Write(partialBuffer, 0, partialOffset);
  212. partialOffset = 0;
  213. }
  214. private void PartialWrite(byte[] buffer, int offset, int count)
  215. {
  216. Streams.ValidateBufferArguments(buffer, offset, count);
  217. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  218. PartialWrite(buffer.AsSpan(offset, count));
  219. #else
  220. if (partialOffset == partialBufferLength)
  221. {
  222. PartialFlush();
  223. }
  224. if (count <= (partialBufferLength - partialOffset))
  225. {
  226. Array.Copy(buffer, offset, partialBuffer, partialOffset, count);
  227. partialOffset += count;
  228. return;
  229. }
  230. int diff = partialBufferLength - partialOffset;
  231. Array.Copy(buffer, offset, partialBuffer, partialOffset, diff);
  232. offset += diff;
  233. count -= diff;
  234. PartialFlush();
  235. while (count > partialBufferLength)
  236. {
  237. Array.Copy(buffer, offset, partialBuffer, 0, partialBufferLength);
  238. offset += partialBufferLength;
  239. count -= partialBufferLength;
  240. PartialFlush();
  241. }
  242. Array.Copy(buffer, offset, partialBuffer, 0, count);
  243. partialOffset = count;
  244. #endif
  245. }
  246. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  247. private void PartialWrite(ReadOnlySpan<byte> buffer)
  248. {
  249. if (partialOffset == partialBufferLength)
  250. {
  251. PartialFlush();
  252. }
  253. if (buffer.Length <= (partialBufferLength - partialOffset))
  254. {
  255. buffer.CopyTo(partialBuffer.AsSpan(partialOffset));
  256. partialOffset += buffer.Length;
  257. return;
  258. }
  259. int diff = partialBufferLength - partialOffset;
  260. buffer[..diff].CopyTo(partialBuffer.AsSpan(partialOffset));
  261. buffer = buffer[diff..];
  262. PartialFlush();
  263. while (buffer.Length > partialBufferLength)
  264. {
  265. PartialFlush(ref buffer);
  266. }
  267. buffer.CopyTo(partialBuffer);
  268. partialOffset = buffer.Length;
  269. }
  270. #endif
  271. private void PartialWriteByte(byte value)
  272. {
  273. if (partialOffset == partialBufferLength)
  274. {
  275. PartialFlush();
  276. }
  277. partialBuffer[partialOffset++] = value;
  278. }
  279. public override void Write(byte[] buffer, int offset, int count)
  280. {
  281. if (partialBuffer != null)
  282. {
  283. PartialWrite(buffer, offset, count);
  284. }
  285. else
  286. {
  287. outStr.Write(buffer, offset, count);
  288. }
  289. }
  290. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  291. public override void Write(ReadOnlySpan<byte> buffer)
  292. {
  293. if (partialBuffer != null)
  294. {
  295. PartialWrite(buffer);
  296. }
  297. else
  298. {
  299. outStr.Write(buffer);
  300. }
  301. }
  302. #endif
  303. public override void WriteByte(byte value)
  304. {
  305. if (partialBuffer != null)
  306. {
  307. PartialWriteByte(value);
  308. }
  309. else
  310. {
  311. outStr.WriteByte(value);
  312. }
  313. }
  314. // Additional helper methods to write primitive types
  315. internal virtual void WriteShort(
  316. short n)
  317. {
  318. this.Write(
  319. (byte)(n >> 8),
  320. (byte)n);
  321. }
  322. internal virtual void WriteInt(
  323. int n)
  324. {
  325. this.Write(
  326. (byte)(n >> 24),
  327. (byte)(n >> 16),
  328. (byte)(n >> 8),
  329. (byte)n);
  330. }
  331. internal virtual void WriteLong(
  332. long n)
  333. {
  334. this.Write(
  335. (byte)(n >> 56),
  336. (byte)(n >> 48),
  337. (byte)(n >> 40),
  338. (byte)(n >> 32),
  339. (byte)(n >> 24),
  340. (byte)(n >> 16),
  341. (byte)(n >> 8),
  342. (byte)n);
  343. }
  344. public void WritePacket(
  345. ContainedPacket p)
  346. {
  347. p.Encode(this);
  348. }
  349. internal void WritePacket(
  350. PacketTag tag,
  351. byte[] body,
  352. bool oldFormat)
  353. {
  354. this.WriteHeader(tag, oldFormat, false, body.Length);
  355. this.Write(body);
  356. }
  357. public void WriteObject(
  358. BcpgObject bcpgObject)
  359. {
  360. bcpgObject.Encode(this);
  361. }
  362. public void WriteObjects(
  363. params BcpgObject[] v)
  364. {
  365. foreach (BcpgObject o in v)
  366. {
  367. o.Encode(this);
  368. }
  369. }
  370. /// <summary>Flush the underlying stream.</summary>
  371. public override void Flush()
  372. {
  373. outStr.Flush();
  374. }
  375. /// <summary>Finish writing out the current packet without closing the underlying stream.</summary>
  376. public void Finish()
  377. {
  378. if (partialBuffer != null)
  379. {
  380. PartialFlushLast();
  381. Array.Clear(partialBuffer, 0, partialBuffer.Length);
  382. partialBuffer = null;
  383. }
  384. }
  385. protected override void Dispose(bool disposing)
  386. {
  387. if (disposing)
  388. {
  389. this.Finish();
  390. outStr.Flush();
  391. outStr.Dispose();
  392. }
  393. base.Dispose(disposing);
  394. }
  395. }
  396. }
  397. #pragma warning restore
  398. #endif