ParserInternalState.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // warning: this is a mutable struct, so it needs to be only passed as a ref!
  45. internal struct ParserInternalState
  46. {
  47. // NOTE: the Span representing the current buffer is kept separate so that this doesn't have to be a ref struct and so it can
  48. // be included in CodedInputStream's internal state
  49. /// <summary>
  50. /// The position within the current buffer (i.e. the next byte to read)
  51. /// </summary>
  52. internal int bufferPos;
  53. /// <summary>
  54. /// Size of the current buffer
  55. /// </summary>
  56. internal int bufferSize;
  57. /// <summary>
  58. /// If we are currently inside a length-delimited block, this is the number of
  59. /// bytes in the buffer that are still available once we leave the delimited block.
  60. /// </summary>
  61. internal int bufferSizeAfterLimit;
  62. /// <summary>
  63. /// The absolute position of the end of the current length-delimited block (including totalBytesRetired)
  64. /// </summary>
  65. internal int currentLimit;
  66. /// <summary>
  67. /// The total number of consumed before the start of the current buffer. The
  68. /// total bytes read up to the current position can be computed as
  69. /// totalBytesRetired + bufferPos.
  70. /// </summary>
  71. internal int totalBytesRetired;
  72. internal int recursionDepth; // current recursion depth
  73. internal SegmentedBufferHelper segmentedBufferHelper;
  74. /// <summary>
  75. /// The last tag we read. 0 indicates we've read to the end of the stream
  76. /// (or haven't read anything yet).
  77. /// </summary>
  78. internal uint lastTag;
  79. /// <summary>
  80. /// The next tag, used to store the value read by PeekTag.
  81. /// </summary>
  82. internal uint nextTag;
  83. internal bool hasNextTag;
  84. // these fields are configuration, they should be readonly
  85. internal int sizeLimit;
  86. internal int recursionLimit;
  87. // If non-null, the top level parse method was started with given coded input stream as an argument
  88. // which also means we can potentially fallback to calling MergeFrom(CodedInputStream cis) if needed.
  89. internal CodedInputStream CodedInputStream => segmentedBufferHelper.CodedInputStream;
  90. /// <summary>
  91. /// Internal-only property; when set to true, unknown fields will be discarded while parsing.
  92. /// </summary>
  93. internal bool DiscardUnknownFields { get; set; }
  94. /// <summary>
  95. /// Internal-only property; provides extension identifiers to compatible messages while parsing.
  96. /// </summary>
  97. internal ExtensionRegistry ExtensionRegistry { get; set; }
  98. }
  99. }