ParseContext.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 parsing state and is passed along
  46. /// as the parsing 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 ParseContext
  52. {
  53. internal const int DefaultRecursionLimit = 100;
  54. internal const int DefaultSizeLimit = Int32.MaxValue;
  55. internal ReadOnlySpan<byte> buffer;
  56. internal ParserInternalState state;
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. internal static void Initialize(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state, out ParseContext ctx)
  59. {
  60. ctx.buffer = buffer;
  61. ctx.state = state;
  62. }
  63. /// <summary>
  64. /// Creates a ParseContext instance from CodedInputStream.
  65. /// WARNING: internally this copies the CodedInputStream's state, so after done with the ParseContext,
  66. /// the CodedInputStream's state needs to be updated.
  67. /// </summary>
  68. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. internal static void Initialize(CodedInputStream input, out ParseContext ctx)
  70. {
  71. ctx.buffer = new ReadOnlySpan<byte>(input.InternalBuffer);
  72. // ideally we would use a reference to the original state, but that doesn't seem possible
  73. // so we just copy the struct that holds the state. We will need to later store the state back
  74. // into CodedInputStream if we want to keep it usable.
  75. ctx.state = input.InternalState;
  76. }
  77. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  78. internal static void Initialize(ReadOnlySequence<byte> input, out ParseContext ctx)
  79. {
  80. Initialize(input, DefaultRecursionLimit, out ctx);
  81. }
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. internal static void Initialize(ReadOnlySequence<byte> input, int recursionLimit, out ParseContext ctx)
  84. {
  85. ctx.buffer = default;
  86. ctx.state = default;
  87. ctx.state.lastTag = 0;
  88. ctx.state.recursionDepth = 0;
  89. ctx.state.sizeLimit = DefaultSizeLimit;
  90. ctx.state.recursionLimit = recursionLimit;
  91. ctx.state.currentLimit = int.MaxValue;
  92. SegmentedBufferHelper.Initialize(input, out ctx.state.segmentedBufferHelper, out ctx.buffer);
  93. ctx.state.bufferPos = 0;
  94. ctx.state.bufferSize = ctx.buffer.Length;
  95. ctx.state.codedInputStream = null;
  96. ctx.state.DiscardUnknownFields = false;
  97. ctx.state.ExtensionRegistry = null;
  98. }
  99. /// <summary>
  100. /// Returns the last tag read, or 0 if no tags have been read or we've read beyond
  101. /// the end of the input.
  102. /// </summary>
  103. internal uint LastTag { get { return state.lastTag; } }
  104. /// <summary>
  105. /// Internal-only property; when set to true, unknown fields will be discarded while parsing.
  106. /// </summary>
  107. internal bool DiscardUnknownFields {
  108. get { return state.DiscardUnknownFields; }
  109. set { state.DiscardUnknownFields = value; }
  110. }
  111. /// <summary>
  112. /// Internal-only property; provides extension identifiers to compatible messages while parsing.
  113. /// </summary>
  114. internal ExtensionRegistry ExtensionRegistry
  115. {
  116. get { return state.ExtensionRegistry; }
  117. set { state.ExtensionRegistry = value; }
  118. }
  119. /// <summary>
  120. /// Reads a field tag, returning the tag of 0 for "end of input".
  121. /// </summary>
  122. /// <remarks>
  123. /// If this method returns 0, it doesn't necessarily mean the end of all
  124. /// the data in this CodedInputReader; it may be the end of the logical input
  125. /// for an embedded message, for example.
  126. /// </remarks>
  127. /// <returns>The next field tag, or 0 for end of input. (0 is never a valid tag.)</returns>
  128. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  129. public uint ReadTag()
  130. {
  131. return ParsingPrimitives.ParseTag(ref buffer, ref state);
  132. }
  133. /// <summary>
  134. /// Reads a double field from the input.
  135. /// </summary>
  136. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  137. public double ReadDouble()
  138. {
  139. return ParsingPrimitives.ParseDouble(ref buffer, ref state);
  140. }
  141. /// <summary>
  142. /// Reads a float field from the input.
  143. /// </summary>
  144. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  145. public float ReadFloat()
  146. {
  147. return ParsingPrimitives.ParseFloat(ref buffer, ref state);
  148. }
  149. /// <summary>
  150. /// Reads a uint64 field from the input.
  151. /// </summary>
  152. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  153. public ulong ReadUInt64()
  154. {
  155. return ParsingPrimitives.ParseRawVarint64(ref buffer, ref state);
  156. }
  157. /// <summary>
  158. /// Reads an int64 field from the input.
  159. /// </summary>
  160. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  161. public long ReadInt64()
  162. {
  163. return (long)ParsingPrimitives.ParseRawVarint64(ref buffer, ref state);
  164. }
  165. /// <summary>
  166. /// Reads an int32 field from the input.
  167. /// </summary>
  168. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  169. public int ReadInt32()
  170. {
  171. return (int)ParsingPrimitives.ParseRawVarint32(ref buffer, ref state);
  172. }
  173. /// <summary>
  174. /// Reads a fixed64 field from the input.
  175. /// </summary>
  176. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  177. public ulong ReadFixed64()
  178. {
  179. return ParsingPrimitives.ParseRawLittleEndian64(ref buffer, ref state);
  180. }
  181. /// <summary>
  182. /// Reads a fixed32 field from the input.
  183. /// </summary>
  184. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  185. public uint ReadFixed32()
  186. {
  187. return ParsingPrimitives.ParseRawLittleEndian32(ref buffer, ref state);
  188. }
  189. /// <summary>
  190. /// Reads a bool field from the input.
  191. /// </summary>
  192. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  193. public bool ReadBool()
  194. {
  195. return ParsingPrimitives.ParseRawVarint64(ref buffer, ref state) != 0;
  196. }
  197. /// <summary>
  198. /// Reads a string field from the input.
  199. /// </summary>
  200. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  201. public string ReadString()
  202. {
  203. int length = ParsingPrimitives.ParseLength(ref buffer, ref state);
  204. return ParsingPrimitives.ReadRawString(ref buffer, ref state, length);
  205. }
  206. /// <summary>
  207. /// Reads an embedded message field value from the input.
  208. /// </summary>
  209. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  210. public void ReadMessage(IMessage message)
  211. {
  212. ParsingPrimitivesMessages.ReadMessage(ref this, message);
  213. }
  214. /// <summary>
  215. /// Reads an embedded group field from the input.
  216. /// </summary>
  217. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  218. public void ReadGroup(IMessage message)
  219. {
  220. ParsingPrimitivesMessages.ReadGroup(ref this, message);
  221. }
  222. /// <summary>
  223. /// Reads a bytes field value from the input.
  224. /// </summary>
  225. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  226. public ByteString ReadBytes()
  227. {
  228. int length = ParsingPrimitives.ParseLength(ref buffer, ref state);
  229. return ByteString.AttachBytes(ParsingPrimitives.ReadRawBytes(ref buffer, ref state, length));
  230. }
  231. /// <summary>
  232. /// Reads a uint32 field value from the input.
  233. /// </summary>
  234. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  235. public uint ReadUInt32()
  236. {
  237. return ParsingPrimitives.ParseRawVarint32(ref buffer, ref state);
  238. }
  239. /// <summary>
  240. /// Reads an enum field value from the input.
  241. /// </summary>
  242. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  243. public int ReadEnum()
  244. {
  245. // Currently just a pass-through, but it's nice to separate it logically from WriteInt32.
  246. return (int)ParsingPrimitives.ParseRawVarint32(ref buffer, ref state);
  247. }
  248. /// <summary>
  249. /// Reads an sfixed32 field value from the input.
  250. /// </summary>
  251. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  252. public int ReadSFixed32()
  253. {
  254. return (int)ParsingPrimitives.ParseRawLittleEndian32(ref buffer, ref state);
  255. }
  256. /// <summary>
  257. /// Reads an sfixed64 field value from the input.
  258. /// </summary>
  259. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  260. public long ReadSFixed64()
  261. {
  262. return (long)ParsingPrimitives.ParseRawLittleEndian64(ref buffer, ref state);
  263. }
  264. /// <summary>
  265. /// Reads an sint32 field value from the input.
  266. /// </summary>
  267. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  268. public int ReadSInt32()
  269. {
  270. return ParsingPrimitives.DecodeZigZag32(ParsingPrimitives.ParseRawVarint32(ref buffer, ref state));
  271. }
  272. /// <summary>
  273. /// Reads an sint64 field value from the input.
  274. /// </summary>
  275. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  276. public long ReadSInt64()
  277. {
  278. return ParsingPrimitives.DecodeZigZag64(ParsingPrimitives.ParseRawVarint64(ref buffer, ref state));
  279. }
  280. /// <summary>
  281. /// Reads a length for length-delimited data.
  282. /// </summary>
  283. /// <remarks>
  284. /// This is internally just reading a varint, but this method exists
  285. /// to make the calling code clearer.
  286. /// </remarks>
  287. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  288. public int ReadLength()
  289. {
  290. return (int)ParsingPrimitives.ParseRawVarint32(ref buffer, ref state);
  291. }
  292. internal void CopyStateTo(CodedInputStream input)
  293. {
  294. input.InternalState = state;
  295. }
  296. internal void LoadStateFrom(CodedInputStream input)
  297. {
  298. state = input.InternalState;
  299. }
  300. }
  301. }