WriteContext.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. using System.Buffers;
  34. using System.Buffers.Binary;
  35. using System.Collections.Generic;
  36. using System.IO;
  37. using System.Runtime.CompilerServices;
  38. using System.Runtime.InteropServices;
  39. using System.Security;
  40. using System.Text;
  41. using Google.Protobuf.Collections;
  42. namespace Google.Protobuf
  43. {
  44. /// <summary>
  45. /// An opaque struct that represents the current serialization state and is passed along
  46. /// as the serialization proceeds.
  47. /// All the public methods are intended to be invoked only by the generated code,
  48. /// users should never invoke them directly.
  49. /// </summary>
  50. [SecuritySafeCritical]
  51. public ref struct WriteContext
  52. {
  53. internal Span<byte> buffer;
  54. internal WriterInternalState state;
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. internal static void Initialize(ref Span<byte> buffer, ref WriterInternalState state, out WriteContext ctx)
  57. {
  58. ctx.buffer = buffer;
  59. ctx.state = state;
  60. }
  61. /// <summary>
  62. /// Creates a WriteContext instance from CodedOutputStream.
  63. /// WARNING: internally this copies the CodedOutputStream's state, so after done with the WriteContext,
  64. /// the CodedOutputStream's state needs to be updated.
  65. /// </summary>
  66. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  67. internal static void Initialize(CodedOutputStream output, out WriteContext ctx)
  68. {
  69. ctx.buffer = new Span<byte>(output.InternalBuffer);
  70. // ideally we would use a reference to the original state, but that doesn't seem possible
  71. // so we just copy the struct that holds the state. We will need to later store the state back
  72. // into CodedOutputStream if we want to keep it usable.
  73. ctx.state = output.InternalState;
  74. }
  75. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76. internal static void Initialize(IBufferWriter<byte> output, out WriteContext ctx)
  77. {
  78. ctx.buffer = default;
  79. ctx.state = default;
  80. WriteBufferHelper.Initialize(output, out ctx.state.writeBufferHelper, out ctx.buffer);
  81. ctx.state.limit = ctx.buffer.Length;
  82. ctx.state.position = 0;
  83. }
  84. /// <summary>
  85. /// Writes a double field value, without a tag.
  86. /// </summary>
  87. /// <param name="value">The value to write</param>
  88. public void WriteDouble(double value)
  89. {
  90. WritingPrimitives.WriteDouble(ref buffer, ref state, value);
  91. }
  92. /// <summary>
  93. /// Writes a float field value, without a tag.
  94. /// </summary>
  95. /// <param name="value">The value to write</param>
  96. public void WriteFloat(float value)
  97. {
  98. WritingPrimitives.WriteFloat(ref buffer, ref state, value);
  99. }
  100. /// <summary>
  101. /// Writes a uint64 field value, without a tag.
  102. /// </summary>
  103. /// <param name="value">The value to write</param>
  104. public void WriteUInt64(ulong value)
  105. {
  106. WritingPrimitives.WriteUInt64(ref buffer, ref state, value);
  107. }
  108. /// <summary>
  109. /// Writes an int64 field value, without a tag.
  110. /// </summary>
  111. /// <param name="value">The value to write</param>
  112. public void WriteInt64(long value)
  113. {
  114. WritingPrimitives.WriteInt64(ref buffer, ref state, value);
  115. }
  116. /// <summary>
  117. /// Writes an int32 field value, without a tag.
  118. /// </summary>
  119. /// <param name="value">The value to write</param>
  120. public void WriteInt32(int value)
  121. {
  122. WritingPrimitives.WriteInt32(ref buffer, ref state, value);
  123. }
  124. /// <summary>
  125. /// Writes a fixed64 field value, without a tag.
  126. /// </summary>
  127. /// <param name="value">The value to write</param>
  128. public void WriteFixed64(ulong value)
  129. {
  130. WritingPrimitives.WriteFixed64(ref buffer, ref state, value);
  131. }
  132. /// <summary>
  133. /// Writes a fixed32 field value, without a tag.
  134. /// </summary>
  135. /// <param name="value">The value to write</param>
  136. public void WriteFixed32(uint value)
  137. {
  138. WritingPrimitives.WriteFixed32(ref buffer, ref state, value);
  139. }
  140. /// <summary>
  141. /// Writes a bool field value, without a tag.
  142. /// </summary>
  143. /// <param name="value">The value to write</param>
  144. public void WriteBool(bool value)
  145. {
  146. WritingPrimitives.WriteBool(ref buffer, ref state, value);
  147. }
  148. /// <summary>
  149. /// Writes a string field value, without a tag.
  150. /// The data is length-prefixed.
  151. /// </summary>
  152. /// <param name="value">The value to write</param>
  153. public void WriteString(string value)
  154. {
  155. WritingPrimitives.WriteString(ref buffer, ref state, value);
  156. }
  157. /// <summary>
  158. /// Writes a message, without a tag.
  159. /// The data is length-prefixed.
  160. /// </summary>
  161. /// <param name="value">The value to write</param>
  162. public void WriteMessage(IMessage value)
  163. {
  164. WritingPrimitivesMessages.WriteMessage(ref this, value);
  165. }
  166. /// <summary>
  167. /// Writes a group, without a tag, to the stream.
  168. /// </summary>
  169. /// <param name="value">The value to write</param>
  170. public void WriteGroup(IMessage value)
  171. {
  172. WritingPrimitivesMessages.WriteGroup(ref this, value);
  173. }
  174. /// <summary>
  175. /// Write a byte string, without a tag, to the stream.
  176. /// The data is length-prefixed.
  177. /// </summary>
  178. /// <param name="value">The value to write</param>
  179. public void WriteBytes(ByteString value)
  180. {
  181. WritingPrimitives.WriteBytes(ref buffer, ref state, value);
  182. }
  183. /// <summary>
  184. /// Writes a uint32 value, without a tag.
  185. /// </summary>
  186. /// <param name="value">The value to write</param>
  187. public void WriteUInt32(uint value)
  188. {
  189. WritingPrimitives.WriteUInt32(ref buffer, ref state, value);
  190. }
  191. /// <summary>
  192. /// Writes an enum value, without a tag.
  193. /// </summary>
  194. /// <param name="value">The value to write</param>
  195. public void WriteEnum(int value)
  196. {
  197. WritingPrimitives.WriteEnum(ref buffer, ref state, value);
  198. }
  199. /// <summary>
  200. /// Writes an sfixed32 value, without a tag.
  201. /// </summary>
  202. /// <param name="value">The value to write.</param>
  203. public void WriteSFixed32(int value)
  204. {
  205. WritingPrimitives.WriteSFixed32(ref buffer, ref state, value);
  206. }
  207. /// <summary>
  208. /// Writes an sfixed64 value, without a tag.
  209. /// </summary>
  210. /// <param name="value">The value to write</param>
  211. public void WriteSFixed64(long value)
  212. {
  213. WritingPrimitives.WriteSFixed64(ref buffer, ref state, value);
  214. }
  215. /// <summary>
  216. /// Writes an sint32 value, without a tag.
  217. /// </summary>
  218. /// <param name="value">The value to write</param>
  219. public void WriteSInt32(int value)
  220. {
  221. WritingPrimitives.WriteSInt32(ref buffer, ref state, value);
  222. }
  223. /// <summary>
  224. /// Writes an sint64 value, without a tag.
  225. /// </summary>
  226. /// <param name="value">The value to write</param>
  227. public void WriteSInt64(long value)
  228. {
  229. WritingPrimitives.WriteSInt64(ref buffer, ref state, value);
  230. }
  231. /// <summary>
  232. /// Writes a length (in bytes) for length-delimited data.
  233. /// </summary>
  234. /// <remarks>
  235. /// This method simply writes a rawint, but exists for clarity in calling code.
  236. /// </remarks>
  237. /// <param name="length">Length value, in bytes.</param>
  238. public void WriteLength(int length)
  239. {
  240. WritingPrimitives.WriteLength(ref buffer, ref state, length);
  241. }
  242. /// <summary>
  243. /// Encodes and writes a tag.
  244. /// </summary>
  245. /// <param name="fieldNumber">The number of the field to write the tag for</param>
  246. /// <param name="type">The wire format type of the tag to write</param>
  247. public void WriteTag(int fieldNumber, WireFormat.WireType type)
  248. {
  249. WritingPrimitives.WriteTag(ref buffer, ref state, fieldNumber, type);
  250. }
  251. /// <summary>
  252. /// Writes an already-encoded tag.
  253. /// </summary>
  254. /// <param name="tag">The encoded tag</param>
  255. public void WriteTag(uint tag)
  256. {
  257. WritingPrimitives.WriteTag(ref buffer, ref state, tag);
  258. }
  259. /// <summary>
  260. /// Writes the given single-byte tag.
  261. /// </summary>
  262. /// <param name="b1">The encoded tag</param>
  263. public void WriteRawTag(byte b1)
  264. {
  265. WritingPrimitives.WriteRawTag(ref buffer, ref state, b1);
  266. }
  267. /// <summary>
  268. /// Writes the given two-byte tag.
  269. /// </summary>
  270. /// <param name="b1">The first byte of the encoded tag</param>
  271. /// <param name="b2">The second byte of the encoded tag</param>
  272. public void WriteRawTag(byte b1, byte b2)
  273. {
  274. WritingPrimitives.WriteRawTag(ref buffer, ref state, b1, b2);
  275. }
  276. /// <summary>
  277. /// Writes the given three-byte tag.
  278. /// </summary>
  279. /// <param name="b1">The first byte of the encoded tag</param>
  280. /// <param name="b2">The second byte of the encoded tag</param>
  281. /// <param name="b3">The third byte of the encoded tag</param>
  282. public void WriteRawTag(byte b1, byte b2, byte b3)
  283. {
  284. WritingPrimitives.WriteRawTag(ref buffer, ref state, b1, b2, b3);
  285. }
  286. /// <summary>
  287. /// Writes the given four-byte tag.
  288. /// </summary>
  289. /// <param name="b1">The first byte of the encoded tag</param>
  290. /// <param name="b2">The second byte of the encoded tag</param>
  291. /// <param name="b3">The third byte of the encoded tag</param>
  292. /// <param name="b4">The fourth byte of the encoded tag</param>
  293. public void WriteRawTag(byte b1, byte b2, byte b3, byte b4)
  294. {
  295. WritingPrimitives.WriteRawTag(ref buffer, ref state, b1, b2, b3, b4);
  296. }
  297. /// <summary>
  298. /// Writes the given five-byte tag.
  299. /// </summary>
  300. /// <param name="b1">The first byte of the encoded tag</param>
  301. /// <param name="b2">The second byte of the encoded tag</param>
  302. /// <param name="b3">The third byte of the encoded tag</param>
  303. /// <param name="b4">The fourth byte of the encoded tag</param>
  304. /// <param name="b5">The fifth byte of the encoded tag</param>
  305. public void WriteRawTag(byte b1, byte b2, byte b3, byte b4, byte b5)
  306. {
  307. WritingPrimitives.WriteRawTag(ref buffer, ref state, b1, b2, b3, b4, b5);
  308. }
  309. internal void CopyStateTo(CodedOutputStream output)
  310. {
  311. output.InternalState = state;
  312. }
  313. internal void LoadStateFrom(CodedOutputStream output)
  314. {
  315. state = output.InternalState;
  316. }
  317. }
  318. }