Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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 |
|
| 33 | using Google.Protobuf.Collections;
|
| 34 | using System;
|
| 35 | using System.Collections.Generic;
|
| 36 | using System.IO;
|
| 37 |
|
| 38 | namespace Google.Protobuf
|
| 39 | {
|
| 40 | /// <summary>
|
| 41 | /// Reads and decodes protocol message fields.
|
| 42 | /// </summary>
|
| 43 | /// <remarks>
|
| 44 | /// <para>
|
| 45 | /// This class is generally used by generated code to read appropriate
|
| 46 | /// primitives from the stream. It effectively encapsulates the lowest
|
| 47 | /// levels of protocol buffer format.
|
| 48 | /// </para>
|
| 49 | /// <para>
|
| 50 | /// Repeated fields and map fields are not handled by this class; use <see cref="RepeatedField{T}"/>
|
| 51 | /// and <see cref="MapField{TKey, TValue}"/> to serialize such fields.
|
| 52 | /// </para>
|
| 53 | /// </remarks>
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 54 | public sealed class CodedInputStream : IDisposable
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 55 | {
|
| 56 | /// <summary>
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 57 | /// Whether to leave the underlying stream open when disposing of this stream.
|
| 58 | /// This is always true when there's no stream.
|
| 59 | /// </summary>
|
| 60 | private readonly bool leaveOpen;
|
| 61 |
|
| 62 | /// <summary>
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 63 | /// Buffer of data read from the stream or provided at construction time.
|
| 64 | /// </summary>
|
| 65 | private readonly byte[] buffer;
|
| 66 |
|
| 67 | /// <summary>
|
| 68 | /// The index of the buffer at which we need to refill from the stream (if there is one).
|
| 69 | /// </summary>
|
| 70 | private int bufferSize;
|
| 71 |
|
| 72 | private int bufferSizeAfterLimit = 0;
|
| 73 | /// <summary>
|
| 74 | /// The position within the current buffer (i.e. the next byte to read)
|
| 75 | /// </summary>
|
| 76 | private int bufferPos = 0;
|
| 77 |
|
| 78 | /// <summary>
|
| 79 | /// The stream to read further input from, or null if the byte array buffer was provided
|
| 80 | /// directly on construction, with no further data available.
|
| 81 | /// </summary>
|
| 82 | private readonly Stream input;
|
| 83 |
|
| 84 | /// <summary>
|
| 85 | /// The last tag we read. 0 indicates we've read to the end of the stream
|
| 86 | /// (or haven't read anything yet).
|
| 87 | /// </summary>
|
| 88 | private uint lastTag = 0;
|
| 89 |
|
| 90 | /// <summary>
|
| 91 | /// The next tag, used to store the value read by PeekTag.
|
| 92 | /// </summary>
|
| 93 | private uint nextTag = 0;
|
| 94 | private bool hasNextTag = false;
|
| 95 |
|
| 96 | internal const int DefaultRecursionLimit = 64;
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 97 | internal const int DefaultSizeLimit = Int32.MaxValue;
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 98 | internal const int BufferSize = 4096;
|
| 99 |
|
| 100 | /// <summary>
|
| 101 | /// The total number of bytes read before the current buffer. The
|
| 102 | /// total bytes read up to the current position can be computed as
|
| 103 | /// totalBytesRetired + bufferPos.
|
| 104 | /// </summary>
|
| 105 | private int totalBytesRetired = 0;
|
| 106 |
|
| 107 | /// <summary>
|
| 108 | /// The absolute position of the end of the current message.
|
| 109 | /// </summary>
|
| 110 | private int currentLimit = int.MaxValue;
|
| 111 |
|
| 112 | private int recursionDepth = 0;
|
| 113 |
|
| 114 | private readonly int recursionLimit;
|
| 115 | private readonly int sizeLimit;
|
| 116 |
|
| 117 | #region Construction
|
| 118 | // Note that the checks are performed such that we don't end up checking obviously-valid things
|
| 119 | // like non-null references for arrays we've just created.
|
| 120 |
|
| 121 | /// <summary>
|
| 122 | /// Creates a new CodedInputStream reading data from the given byte array.
|
| 123 | /// </summary>
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 124 | public CodedInputStream(byte[] buffer) : this(null, ProtoPreconditions.CheckNotNull(buffer, "buffer"), 0, buffer.Length, true)
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 125 | {
|
| 126 | }
|
| 127 |
|
| 128 | /// <summary>
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 129 | /// Creates a new <see cref="CodedInputStream"/> that reads from the given byte array slice.
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 130 | /// </summary>
|
| 131 | public CodedInputStream(byte[] buffer, int offset, int length)
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 132 | : this(null, ProtoPreconditions.CheckNotNull(buffer, "buffer"), offset, offset + length, true)
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 133 | {
|
| 134 | if (offset < 0 || offset > buffer.Length)
|
| 135 | {
|
| 136 | throw new ArgumentOutOfRangeException("offset", "Offset must be within the buffer");
|
| 137 | }
|
| 138 | if (length < 0 || offset + length > buffer.Length)
|
| 139 | {
|
| 140 | throw new ArgumentOutOfRangeException("length", "Length must be non-negative and within the buffer");
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | /// <summary>
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 145 | /// Creates a new <see cref="CodedInputStream"/> reading data from the given stream, which will be disposed
|
| 146 | /// when the returned object is disposed.
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 147 | /// </summary>
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 148 | /// <param name="input">The stream to read from.</param>
|
| 149 | public CodedInputStream(Stream input) : this(input, false)
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 150 | {
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 151 | }
|
| 152 |
|
| 153 | /// <summary>
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 154 | /// Creates a new <see cref="CodedInputStream"/> reading data from the given stream.
|
| 155 | /// </summary>
|
| 156 | /// <param name="input">The stream to read from.</param>
|
| 157 | /// <param name="leaveOpen"><c>true</c> to leave <paramref name="input"/> open when the returned
|
| 158 | /// <c cref="CodedInputStream"/> is disposed; <c>false</c> to dispose of the given stream when the
|
| 159 | /// returned object is disposed.</param>
|
| 160 | public CodedInputStream(Stream input, bool leaveOpen)
|
| 161 | : this(ProtoPreconditions.CheckNotNull(input, "input"), new byte[BufferSize], 0, 0, leaveOpen)
|
| 162 | {
|
| 163 | }
|
| 164 |
|
| 165 | /// <summary>
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 166 | /// Creates a new CodedInputStream reading data from the given
|
| 167 | /// stream and buffer, using the default limits.
|
| 168 | /// </summary>
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 169 | internal CodedInputStream(Stream input, byte[] buffer, int bufferPos, int bufferSize, bool leaveOpen)
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 170 | {
|
| 171 | this.input = input;
|
| 172 | this.buffer = buffer;
|
| 173 | this.bufferPos = bufferPos;
|
| 174 | this.bufferSize = bufferSize;
|
| 175 | this.sizeLimit = DefaultSizeLimit;
|
| 176 | this.recursionLimit = DefaultRecursionLimit;
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 177 | this.leaveOpen = leaveOpen;
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 178 | }
|
| 179 |
|
| 180 | /// <summary>
|
| 181 | /// Creates a new CodedInputStream reading data from the given
|
| 182 | /// stream and buffer, using the specified limits.
|
| 183 | /// </summary>
|
| 184 | /// <remarks>
|
| 185 | /// This chains to the version with the default limits instead of vice versa to avoid
|
| 186 | /// having to check that the default values are valid every time.
|
| 187 | /// </remarks>
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 188 | internal CodedInputStream(Stream input, byte[] buffer, int bufferPos, int bufferSize, int sizeLimit, int recursionLimit, bool leaveOpen)
|
| 189 | : this(input, buffer, bufferPos, bufferSize, leaveOpen)
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 190 | {
|
| 191 | if (sizeLimit <= 0)
|
| 192 | {
|
| 193 | throw new ArgumentOutOfRangeException("sizeLimit", "Size limit must be positive");
|
| 194 | }
|
| 195 | if (recursionLimit <= 0)
|
| 196 | {
|
| 197 | throw new ArgumentOutOfRangeException("recursionLimit!", "Recursion limit must be positive");
|
| 198 | }
|
| 199 | this.sizeLimit = sizeLimit;
|
| 200 | this.recursionLimit = recursionLimit;
|
| 201 | }
|
| 202 | #endregion
|
| 203 |
|
| 204 | /// <summary>
|
| 205 | /// Creates a <see cref="CodedInputStream"/> with the specified size and recursion limits, reading
|
| 206 | /// from an input stream.
|
| 207 | /// </summary>
|
| 208 | /// <remarks>
|
| 209 | /// This method exists separately from the constructor to reduce the number of constructor overloads.
|
| 210 | /// It is likely to be used considerably less frequently than the constructors, as the default limits
|
| 211 | /// are suitable for most use cases.
|
| 212 | /// </remarks>
|
| 213 | /// <param name="input">The input stream to read from</param>
|
| 214 | /// <param name="sizeLimit">The total limit of data to read from the stream.</param>
|
| 215 | /// <param name="recursionLimit">The maximum recursion depth to allow while reading.</param>
|
| 216 | /// <returns>A <c>CodedInputStream</c> reading from <paramref name="input"/> with the specified size
|
| 217 | /// and recursion limits.</returns>
|
| 218 | public static CodedInputStream CreateWithLimits(Stream input, int sizeLimit, int recursionLimit)
|
| 219 | {
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 220 | // Note: we may want an overload accepting leaveOpen
|
| 221 | return new CodedInputStream(input, new byte[BufferSize], 0, 0, sizeLimit, recursionLimit, false);
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 222 | }
|
| 223 |
|
| 224 | /// <summary>
|
| 225 | /// Returns the current position in the input stream, or the position in the input buffer
|
| 226 | /// </summary>
|
| 227 | public long Position
|
| 228 | {
|
| 229 | get
|
| 230 | {
|
| 231 | if (input != null)
|
| 232 | {
|
| 233 | return input.Position - ((bufferSize + bufferSizeAfterLimit) - bufferPos);
|
| 234 | }
|
| 235 | return bufferPos;
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | /// <summary>
|
| 240 | /// Returns the last tag read, or 0 if no tags have been read or we've read beyond
|
| 241 | /// the end of the stream.
|
| 242 | /// </summary>
|
| 243 | internal uint LastTag { get { return lastTag; } }
|
| 244 |
|
| 245 | /// <summary>
|
| 246 | /// Returns the size limit for this stream.
|
| 247 | /// </summary>
|
| 248 | /// <remarks>
|
| 249 | /// This limit is applied when reading from the underlying stream, as a sanity check. It is
|
| 250 | /// not applied when reading from a byte array data source without an underlying stream.
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 251 | /// The default value is Int32.MaxValue.
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 252 | /// </remarks>
|
| 253 | /// <value>
|
| 254 | /// The size limit.
|
| 255 | /// </value>
|
| 256 | public int SizeLimit { get { return sizeLimit; } }
|
| 257 |
|
| 258 | /// <summary>
|
| 259 | /// Returns the recursion limit for this stream. This limit is applied whilst reading messages,
|
| 260 | /// to avoid maliciously-recursive data.
|
| 261 | /// </summary>
|
| 262 | /// <remarks>
|
| 263 | /// The default limit is 64.
|
| 264 | /// </remarks>
|
| 265 | /// <value>
|
| 266 | /// The recursion limit for this stream.
|
| 267 | /// </value>
|
| 268 | public int RecursionLimit { get { return recursionLimit; } }
|
| 269 |
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 270 | /// <summary>
|
| 271 | /// Internal-only property; when set to true, unknown fields will be discarded while parsing.
|
| 272 | /// </summary>
|
| 273 | internal bool DiscardUnknownFields { get; set; }
|
| 274 |
|
| 275 | /// <summary>
|
| 276 | /// Disposes of this instance, potentially closing any underlying stream.
|
| 277 | /// </summary>
|
| 278 | /// <remarks>
|
| 279 | /// As there is no flushing to perform here, disposing of a <see cref="CodedInputStream"/> which
|
| 280 | /// was constructed with the <c>leaveOpen</c> option parameter set to <c>true</c> (or one which
|
| 281 | /// was constructed to read from a byte array) has no effect.
|
| 282 | /// </remarks>
|
| 283 | public void Dispose()
|
| 284 | {
|
| 285 | if (!leaveOpen)
|
| 286 | {
|
| 287 | input.Dispose();
|
| 288 | }
|
| 289 | }
|
| 290 |
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 291 | #region Validation
|
| 292 | /// <summary>
|
| 293 | /// Verifies that the last call to ReadTag() returned tag 0 - in other words,
|
| 294 | /// we've reached the end of the stream when we expected to.
|
| 295 | /// </summary>
|
| 296 | /// <exception cref="InvalidProtocolBufferException">The
|
| 297 | /// tag read was not the one specified</exception>
|
| 298 | internal void CheckReadEndOfStreamTag()
|
| 299 | {
|
| 300 | if (lastTag != 0)
|
| 301 | {
|
| 302 | throw InvalidProtocolBufferException.MoreDataAvailable();
|
| 303 | }
|
| 304 | }
|
| 305 | #endregion
|
| 306 |
|
| 307 | #region Reading of tags etc
|
| 308 |
|
| 309 | /// <summary>
|
| 310 | /// Peeks at the next field tag. This is like calling <see cref="ReadTag"/>, but the
|
| 311 | /// tag is not consumed. (So a subsequent call to <see cref="ReadTag"/> will return the
|
| 312 | /// same value.)
|
| 313 | /// </summary>
|
| 314 | public uint PeekTag()
|
| 315 | {
|
| 316 | if (hasNextTag)
|
| 317 | {
|
| 318 | return nextTag;
|
| 319 | }
|
| 320 |
|
| 321 | uint savedLast = lastTag;
|
| 322 | nextTag = ReadTag();
|
| 323 | hasNextTag = true;
|
| 324 | lastTag = savedLast; // Undo the side effect of ReadTag
|
| 325 | return nextTag;
|
| 326 | }
|
| 327 |
|
| 328 | /// <summary>
|
| 329 | /// Reads a field tag, returning the tag of 0 for "end of stream".
|
| 330 | /// </summary>
|
| 331 | /// <remarks>
|
| 332 | /// If this method returns 0, it doesn't necessarily mean the end of all
|
| 333 | /// the data in this CodedInputStream; it may be the end of the logical stream
|
| 334 | /// for an embedded message, for example.
|
| 335 | /// </remarks>
|
| 336 | /// <returns>The next field tag, or 0 for end of stream. (0 is never a valid tag.)</returns>
|
| 337 | public uint ReadTag()
|
| 338 | {
|
| 339 | if (hasNextTag)
|
| 340 | {
|
| 341 | lastTag = nextTag;
|
| 342 | hasNextTag = false;
|
| 343 | return lastTag;
|
| 344 | }
|
| 345 |
|
| 346 | // Optimize for the incredibly common case of having at least two bytes left in the buffer,
|
| 347 | // and those two bytes being enough to get the tag. This will be true for fields up to 4095.
|
| 348 | if (bufferPos + 2 <= bufferSize)
|
| 349 | {
|
| 350 | int tmp = buffer[bufferPos++];
|
| 351 | if (tmp < 128)
|
| 352 | {
|
| 353 | lastTag = (uint)tmp;
|
| 354 | }
|
| 355 | else
|
| 356 | {
|
| 357 | int result = tmp & 0x7f;
|
| 358 | if ((tmp = buffer[bufferPos++]) < 128)
|
| 359 | {
|
| 360 | result |= tmp << 7;
|
| 361 | lastTag = (uint) result;
|
| 362 | }
|
| 363 | else
|
| 364 | {
|
| 365 | // Nope, rewind and go the potentially slow route.
|
| 366 | bufferPos -= 2;
|
| 367 | lastTag = ReadRawVarint32();
|
| 368 | }
|
| 369 | }
|
| 370 | }
|
| 371 | else
|
| 372 | {
|
| 373 | if (IsAtEnd)
|
| 374 | {
|
| 375 | lastTag = 0;
|
| 376 | return 0; // This is the only case in which we return 0.
|
| 377 | }
|
| 378 |
|
| 379 | lastTag = ReadRawVarint32();
|
| 380 | }
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 381 | if (WireFormat.GetTagFieldNumber(lastTag) == 0)
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 382 | {
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 383 | // If we actually read a tag with a field of 0, that's not a valid tag.
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 384 | throw InvalidProtocolBufferException.InvalidTag();
|
| 385 | }
|
| 386 | return lastTag;
|
| 387 | }
|
| 388 |
|
| 389 | /// <summary>
|
| 390 | /// Skips the data for the field with the tag we've just read.
|
| 391 | /// This should be called directly after <see cref="ReadTag"/>, when
|
| 392 | /// the caller wishes to skip an unknown field.
|
| 393 | /// </summary>
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 394 | /// <remarks>
|
| 395 | /// This method throws <see cref="InvalidProtocolBufferException"/> if the last-read tag was an end-group tag.
|
| 396 | /// If a caller wishes to skip a group, they should skip the whole group, by calling this method after reading the
|
| 397 | /// start-group tag. This behavior allows callers to call this method on any field they don't understand, correctly
|
| 398 | /// resulting in an error if an end-group tag has not been paired with an earlier start-group tag.
|
| 399 | /// </remarks>
|
| 400 | /// <exception cref="InvalidProtocolBufferException">The last tag was an end-group tag</exception>
|
| 401 | /// <exception cref="InvalidOperationException">The last read operation read to the end of the logical stream</exception>
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 402 | public void SkipLastField()
|
| 403 | {
|
| 404 | if (lastTag == 0)
|
| 405 | {
|
| 406 | throw new InvalidOperationException("SkipLastField cannot be called at the end of a stream");
|
| 407 | }
|
| 408 | switch (WireFormat.GetTagWireType(lastTag))
|
| 409 | {
|
| 410 | case WireFormat.WireType.StartGroup:
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 411 | SkipGroup(lastTag);
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 412 | break;
|
| 413 | case WireFormat.WireType.EndGroup:
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 414 | throw new InvalidProtocolBufferException(
|
| 415 | "SkipLastField called on an end-group tag, indicating that the corresponding start-group was missing");
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 416 | case WireFormat.WireType.Fixed32:
|
| 417 | ReadFixed32();
|
| 418 | break;
|
| 419 | case WireFormat.WireType.Fixed64:
|
| 420 | ReadFixed64();
|
| 421 | break;
|
| 422 | case WireFormat.WireType.LengthDelimited:
|
| 423 | var length = ReadLength();
|
| 424 | SkipRawBytes(length);
|
| 425 | break;
|
| 426 | case WireFormat.WireType.Varint:
|
| 427 | ReadRawVarint32();
|
| 428 | break;
|
| 429 | }
|
| 430 | }
|
| 431 |
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 432 | /// <summary>
|
| 433 | /// Skip a group.
|
| 434 | /// </summary>
|
| 435 | internal void SkipGroup(uint startGroupTag)
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 436 | {
|
| 437 | // Note: Currently we expect this to be the way that groups are read. We could put the recursion
|
| 438 | // depth changes into the ReadTag method instead, potentially...
|
| 439 | recursionDepth++;
|
| 440 | if (recursionDepth >= recursionLimit)
|
| 441 | {
|
| 442 | throw InvalidProtocolBufferException.RecursionLimitExceeded();
|
| 443 | }
|
| 444 | uint tag;
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 445 | while (true)
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 446 | {
|
| 447 | tag = ReadTag();
|
| 448 | if (tag == 0)
|
| 449 | {
|
| 450 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 451 | }
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 452 | // Can't call SkipLastField for this case- that would throw.
|
| 453 | if (WireFormat.GetTagWireType(tag) == WireFormat.WireType.EndGroup)
|
| 454 | {
|
| 455 | break;
|
| 456 | }
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 457 | // This recursion will allow us to handle nested groups.
|
| 458 | SkipLastField();
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 459 | }
|
| 460 | int startField = WireFormat.GetTagFieldNumber(startGroupTag);
|
| 461 | int endField = WireFormat.GetTagFieldNumber(tag);
|
| 462 | if (startField != endField)
|
| 463 | {
|
| 464 | throw new InvalidProtocolBufferException(
|
| 465 | $"Mismatched end-group tag. Started with field {startField}; ended with field {endField}");
|
| 466 | }
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 467 | recursionDepth--;
|
| 468 | }
|
| 469 |
|
| 470 | /// <summary>
|
| 471 | /// Reads a double field from the stream.
|
| 472 | /// </summary>
|
| 473 | public double ReadDouble()
|
| 474 | {
|
| 475 | return BitConverter.Int64BitsToDouble((long) ReadRawLittleEndian64());
|
| 476 | }
|
| 477 |
|
| 478 | /// <summary>
|
| 479 | /// Reads a float field from the stream.
|
| 480 | /// </summary>
|
| 481 | public float ReadFloat()
|
| 482 | {
|
| 483 | if (BitConverter.IsLittleEndian && 4 <= bufferSize - bufferPos)
|
| 484 | {
|
| 485 | float ret = BitConverter.ToSingle(buffer, bufferPos);
|
| 486 | bufferPos += 4;
|
| 487 | return ret;
|
| 488 | }
|
| 489 | else
|
| 490 | {
|
| 491 | byte[] rawBytes = ReadRawBytes(4);
|
| 492 | if (!BitConverter.IsLittleEndian)
|
| 493 | {
|
| 494 | ByteArray.Reverse(rawBytes);
|
| 495 | }
|
| 496 | return BitConverter.ToSingle(rawBytes, 0);
|
| 497 | }
|
| 498 | }
|
| 499 |
|
| 500 | /// <summary>
|
| 501 | /// Reads a uint64 field from the stream.
|
| 502 | /// </summary>
|
| 503 | public ulong ReadUInt64()
|
| 504 | {
|
| 505 | return ReadRawVarint64();
|
| 506 | }
|
| 507 |
|
| 508 | /// <summary>
|
| 509 | /// Reads an int64 field from the stream.
|
| 510 | /// </summary>
|
| 511 | public long ReadInt64()
|
| 512 | {
|
| 513 | return (long) ReadRawVarint64();
|
| 514 | }
|
| 515 |
|
| 516 | /// <summary>
|
| 517 | /// Reads an int32 field from the stream.
|
| 518 | /// </summary>
|
| 519 | public int ReadInt32()
|
| 520 | {
|
| 521 | return (int) ReadRawVarint32();
|
| 522 | }
|
| 523 |
|
| 524 | /// <summary>
|
| 525 | /// Reads a fixed64 field from the stream.
|
| 526 | /// </summary>
|
| 527 | public ulong ReadFixed64()
|
| 528 | {
|
| 529 | return ReadRawLittleEndian64();
|
| 530 | }
|
| 531 |
|
| 532 | /// <summary>
|
| 533 | /// Reads a fixed32 field from the stream.
|
| 534 | /// </summary>
|
| 535 | public uint ReadFixed32()
|
| 536 | {
|
| 537 | return ReadRawLittleEndian32();
|
| 538 | }
|
| 539 |
|
| 540 | /// <summary>
|
| 541 | /// Reads a bool field from the stream.
|
| 542 | /// </summary>
|
| 543 | public bool ReadBool()
|
| 544 | {
|
| 545 | return ReadRawVarint32() != 0;
|
| 546 | }
|
| 547 |
|
| 548 | /// <summary>
|
| 549 | /// Reads a string field from the stream.
|
| 550 | /// </summary>
|
| 551 | public string ReadString()
|
| 552 | {
|
| 553 | int length = ReadLength();
|
| 554 | // No need to read any data for an empty string.
|
| 555 | if (length == 0)
|
| 556 | {
|
| 557 | return "";
|
| 558 | }
|
| 559 | if (length <= bufferSize - bufferPos)
|
| 560 | {
|
| 561 | // Fast path: We already have the bytes in a contiguous buffer, so
|
| 562 | // just copy directly from it.
|
| 563 | String result = CodedOutputStream.Utf8Encoding.GetString(buffer, bufferPos, length);
|
| 564 | bufferPos += length;
|
| 565 | return result;
|
| 566 | }
|
| 567 | // Slow path: Build a byte array first then copy it.
|
| 568 | return CodedOutputStream.Utf8Encoding.GetString(ReadRawBytes(length), 0, length);
|
| 569 | }
|
| 570 |
|
| 571 | /// <summary>
|
| 572 | /// Reads an embedded message field value from the stream.
|
| 573 | /// </summary>
|
| 574 | public void ReadMessage(IMessage builder)
|
| 575 | {
|
| 576 | int length = ReadLength();
|
| 577 | if (recursionDepth >= recursionLimit)
|
| 578 | {
|
| 579 | throw InvalidProtocolBufferException.RecursionLimitExceeded();
|
| 580 | }
|
| 581 | int oldLimit = PushLimit(length);
|
| 582 | ++recursionDepth;
|
| 583 | builder.MergeFrom(this);
|
| 584 | CheckReadEndOfStreamTag();
|
| 585 | // Check that we've read exactly as much data as expected.
|
| 586 | if (!ReachedLimit)
|
| 587 | {
|
| 588 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 589 | }
|
| 590 | --recursionDepth;
|
| 591 | PopLimit(oldLimit);
|
| 592 | }
|
| 593 |
|
| 594 | /// <summary>
|
| 595 | /// Reads a bytes field value from the stream.
|
| 596 | /// </summary>
|
| 597 | public ByteString ReadBytes()
|
| 598 | {
|
| 599 | int length = ReadLength();
|
| 600 | if (length <= bufferSize - bufferPos && length > 0)
|
| 601 | {
|
| 602 | // Fast path: We already have the bytes in a contiguous buffer, so
|
| 603 | // just copy directly from it.
|
| 604 | ByteString result = ByteString.CopyFrom(buffer, bufferPos, length);
|
| 605 | bufferPos += length;
|
| 606 | return result;
|
| 607 | }
|
| 608 | else
|
| 609 | {
|
| 610 | // Slow path: Build a byte array and attach it to a new ByteString.
|
| 611 | return ByteString.AttachBytes(ReadRawBytes(length));
|
| 612 | }
|
| 613 | }
|
| 614 |
|
| 615 | /// <summary>
|
| 616 | /// Reads a uint32 field value from the stream.
|
| 617 | /// </summary>
|
| 618 | public uint ReadUInt32()
|
| 619 | {
|
| 620 | return ReadRawVarint32();
|
| 621 | }
|
| 622 |
|
| 623 | /// <summary>
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 624 | /// Reads an enum field value from the stream.
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 625 | /// </summary>
|
| 626 | public int ReadEnum()
|
| 627 | {
|
| 628 | // Currently just a pass-through, but it's nice to separate it logically from WriteInt32.
|
| 629 | return (int) ReadRawVarint32();
|
| 630 | }
|
| 631 |
|
| 632 | /// <summary>
|
| 633 | /// Reads an sfixed32 field value from the stream.
|
| 634 | /// </summary>
|
| 635 | public int ReadSFixed32()
|
| 636 | {
|
| 637 | return (int) ReadRawLittleEndian32();
|
| 638 | }
|
| 639 |
|
| 640 | /// <summary>
|
| 641 | /// Reads an sfixed64 field value from the stream.
|
| 642 | /// </summary>
|
| 643 | public long ReadSFixed64()
|
| 644 | {
|
| 645 | return (long) ReadRawLittleEndian64();
|
| 646 | }
|
| 647 |
|
| 648 | /// <summary>
|
| 649 | /// Reads an sint32 field value from the stream.
|
| 650 | /// </summary>
|
| 651 | public int ReadSInt32()
|
| 652 | {
|
| 653 | return DecodeZigZag32(ReadRawVarint32());
|
| 654 | }
|
| 655 |
|
| 656 | /// <summary>
|
| 657 | /// Reads an sint64 field value from the stream.
|
| 658 | /// </summary>
|
| 659 | public long ReadSInt64()
|
| 660 | {
|
| 661 | return DecodeZigZag64(ReadRawVarint64());
|
| 662 | }
|
| 663 |
|
| 664 | /// <summary>
|
| 665 | /// Reads a length for length-delimited data.
|
| 666 | /// </summary>
|
| 667 | /// <remarks>
|
| 668 | /// This is internally just reading a varint, but this method exists
|
| 669 | /// to make the calling code clearer.
|
| 670 | /// </remarks>
|
| 671 | public int ReadLength()
|
| 672 | {
|
| 673 | return (int) ReadRawVarint32();
|
| 674 | }
|
| 675 |
|
| 676 | /// <summary>
|
| 677 | /// Peeks at the next tag in the stream. If it matches <paramref name="tag"/>,
|
| 678 | /// the tag is consumed and the method returns <c>true</c>; otherwise, the
|
| 679 | /// stream is left in the original position and the method returns <c>false</c>.
|
| 680 | /// </summary>
|
| 681 | public bool MaybeConsumeTag(uint tag)
|
| 682 | {
|
| 683 | if (PeekTag() == tag)
|
| 684 | {
|
| 685 | hasNextTag = false;
|
| 686 | return true;
|
| 687 | }
|
| 688 | return false;
|
| 689 | }
|
| 690 |
|
| 691 | #endregion
|
| 692 |
|
| 693 | #region Underlying reading primitives
|
| 694 |
|
| 695 | /// <summary>
|
| 696 | /// Same code as ReadRawVarint32, but read each byte individually, checking for
|
| 697 | /// buffer overflow.
|
| 698 | /// </summary>
|
| 699 | private uint SlowReadRawVarint32()
|
| 700 | {
|
| 701 | int tmp = ReadRawByte();
|
| 702 | if (tmp < 128)
|
| 703 | {
|
| 704 | return (uint) tmp;
|
| 705 | }
|
| 706 | int result = tmp & 0x7f;
|
| 707 | if ((tmp = ReadRawByte()) < 128)
|
| 708 | {
|
| 709 | result |= tmp << 7;
|
| 710 | }
|
| 711 | else
|
| 712 | {
|
| 713 | result |= (tmp & 0x7f) << 7;
|
| 714 | if ((tmp = ReadRawByte()) < 128)
|
| 715 | {
|
| 716 | result |= tmp << 14;
|
| 717 | }
|
| 718 | else
|
| 719 | {
|
| 720 | result |= (tmp & 0x7f) << 14;
|
| 721 | if ((tmp = ReadRawByte()) < 128)
|
| 722 | {
|
| 723 | result |= tmp << 21;
|
| 724 | }
|
| 725 | else
|
| 726 | {
|
| 727 | result |= (tmp & 0x7f) << 21;
|
| 728 | result |= (tmp = ReadRawByte()) << 28;
|
| 729 | if (tmp >= 128)
|
| 730 | {
|
| 731 | // Discard upper 32 bits.
|
| 732 | for (int i = 0; i < 5; i++)
|
| 733 | {
|
| 734 | if (ReadRawByte() < 128)
|
| 735 | {
|
| 736 | return (uint) result;
|
| 737 | }
|
| 738 | }
|
| 739 | throw InvalidProtocolBufferException.MalformedVarint();
|
| 740 | }
|
| 741 | }
|
| 742 | }
|
| 743 | }
|
| 744 | return (uint) result;
|
| 745 | }
|
| 746 |
|
| 747 | /// <summary>
|
| 748 | /// Reads a raw Varint from the stream. If larger than 32 bits, discard the upper bits.
|
| 749 | /// This method is optimised for the case where we've got lots of data in the buffer.
|
| 750 | /// That means we can check the size just once, then just read directly from the buffer
|
| 751 | /// without constant rechecking of the buffer length.
|
| 752 | /// </summary>
|
| 753 | internal uint ReadRawVarint32()
|
| 754 | {
|
| 755 | if (bufferPos + 5 > bufferSize)
|
| 756 | {
|
| 757 | return SlowReadRawVarint32();
|
| 758 | }
|
| 759 |
|
| 760 | int tmp = buffer[bufferPos++];
|
| 761 | if (tmp < 128)
|
| 762 | {
|
| 763 | return (uint) tmp;
|
| 764 | }
|
| 765 | int result = tmp & 0x7f;
|
| 766 | if ((tmp = buffer[bufferPos++]) < 128)
|
| 767 | {
|
| 768 | result |= tmp << 7;
|
| 769 | }
|
| 770 | else
|
| 771 | {
|
| 772 | result |= (tmp & 0x7f) << 7;
|
| 773 | if ((tmp = buffer[bufferPos++]) < 128)
|
| 774 | {
|
| 775 | result |= tmp << 14;
|
| 776 | }
|
| 777 | else
|
| 778 | {
|
| 779 | result |= (tmp & 0x7f) << 14;
|
| 780 | if ((tmp = buffer[bufferPos++]) < 128)
|
| 781 | {
|
| 782 | result |= tmp << 21;
|
| 783 | }
|
| 784 | else
|
| 785 | {
|
| 786 | result |= (tmp & 0x7f) << 21;
|
| 787 | result |= (tmp = buffer[bufferPos++]) << 28;
|
| 788 | if (tmp >= 128)
|
| 789 | {
|
| 790 | // Discard upper 32 bits.
|
| 791 | // Note that this has to use ReadRawByte() as we only ensure we've
|
| 792 | // got at least 5 bytes at the start of the method. This lets us
|
| 793 | // use the fast path in more cases, and we rarely hit this section of code.
|
| 794 | for (int i = 0; i < 5; i++)
|
| 795 | {
|
| 796 | if (ReadRawByte() < 128)
|
| 797 | {
|
| 798 | return (uint) result;
|
| 799 | }
|
| 800 | }
|
| 801 | throw InvalidProtocolBufferException.MalformedVarint();
|
| 802 | }
|
| 803 | }
|
| 804 | }
|
| 805 | }
|
| 806 | return (uint) result;
|
| 807 | }
|
| 808 |
|
| 809 | /// <summary>
|
| 810 | /// Reads a varint from the input one byte at a time, so that it does not
|
| 811 | /// read any bytes after the end of the varint. If you simply wrapped the
|
| 812 | /// stream in a CodedInputStream and used ReadRawVarint32(Stream)
|
| 813 | /// then you would probably end up reading past the end of the varint since
|
| 814 | /// CodedInputStream buffers its input.
|
| 815 | /// </summary>
|
| 816 | /// <param name="input"></param>
|
| 817 | /// <returns></returns>
|
| 818 | internal static uint ReadRawVarint32(Stream input)
|
| 819 | {
|
| 820 | int result = 0;
|
| 821 | int offset = 0;
|
| 822 | for (; offset < 32; offset += 7)
|
| 823 | {
|
| 824 | int b = input.ReadByte();
|
| 825 | if (b == -1)
|
| 826 | {
|
| 827 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 828 | }
|
| 829 | result |= (b & 0x7f) << offset;
|
| 830 | if ((b & 0x80) == 0)
|
| 831 | {
|
| 832 | return (uint) result;
|
| 833 | }
|
| 834 | }
|
| 835 | // Keep reading up to 64 bits.
|
| 836 | for (; offset < 64; offset += 7)
|
| 837 | {
|
| 838 | int b = input.ReadByte();
|
| 839 | if (b == -1)
|
| 840 | {
|
| 841 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 842 | }
|
| 843 | if ((b & 0x80) == 0)
|
| 844 | {
|
| 845 | return (uint) result;
|
| 846 | }
|
| 847 | }
|
| 848 | throw InvalidProtocolBufferException.MalformedVarint();
|
| 849 | }
|
| 850 |
|
| 851 | /// <summary>
|
| 852 | /// Reads a raw varint from the stream.
|
| 853 | /// </summary>
|
| 854 | internal ulong ReadRawVarint64()
|
| 855 | {
|
| 856 | int shift = 0;
|
| 857 | ulong result = 0;
|
| 858 | while (shift < 64)
|
| 859 | {
|
| 860 | byte b = ReadRawByte();
|
| 861 | result |= (ulong) (b & 0x7F) << shift;
|
| 862 | if ((b & 0x80) == 0)
|
| 863 | {
|
| 864 | return result;
|
| 865 | }
|
| 866 | shift += 7;
|
| 867 | }
|
| 868 | throw InvalidProtocolBufferException.MalformedVarint();
|
| 869 | }
|
| 870 |
|
| 871 | /// <summary>
|
| 872 | /// Reads a 32-bit little-endian integer from the stream.
|
| 873 | /// </summary>
|
| 874 | internal uint ReadRawLittleEndian32()
|
| 875 | {
|
| 876 | uint b1 = ReadRawByte();
|
| 877 | uint b2 = ReadRawByte();
|
| 878 | uint b3 = ReadRawByte();
|
| 879 | uint b4 = ReadRawByte();
|
| 880 | return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
|
| 881 | }
|
| 882 |
|
| 883 | /// <summary>
|
| 884 | /// Reads a 64-bit little-endian integer from the stream.
|
| 885 | /// </summary>
|
| 886 | internal ulong ReadRawLittleEndian64()
|
| 887 | {
|
| 888 | ulong b1 = ReadRawByte();
|
| 889 | ulong b2 = ReadRawByte();
|
| 890 | ulong b3 = ReadRawByte();
|
| 891 | ulong b4 = ReadRawByte();
|
| 892 | ulong b5 = ReadRawByte();
|
| 893 | ulong b6 = ReadRawByte();
|
| 894 | ulong b7 = ReadRawByte();
|
| 895 | ulong b8 = ReadRawByte();
|
| 896 | return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24)
|
| 897 | | (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);
|
| 898 | }
|
| 899 |
|
| 900 | /// <summary>
|
| 901 | /// Decode a 32-bit value with ZigZag encoding.
|
| 902 | /// </summary>
|
| 903 | /// <remarks>
|
| 904 | /// ZigZag encodes signed integers into values that can be efficiently
|
| 905 | /// encoded with varint. (Otherwise, negative values must be
|
| 906 | /// sign-extended to 64 bits to be varint encoded, thus always taking
|
| 907 | /// 10 bytes on the wire.)
|
| 908 | /// </remarks>
|
| 909 | internal static int DecodeZigZag32(uint n)
|
| 910 | {
|
| 911 | return (int)(n >> 1) ^ -(int)(n & 1);
|
| 912 | }
|
| 913 |
|
| 914 | /// <summary>
|
| 915 | /// Decode a 32-bit value with ZigZag encoding.
|
| 916 | /// </summary>
|
| 917 | /// <remarks>
|
| 918 | /// ZigZag encodes signed integers into values that can be efficiently
|
| 919 | /// encoded with varint. (Otherwise, negative values must be
|
| 920 | /// sign-extended to 64 bits to be varint encoded, thus always taking
|
| 921 | /// 10 bytes on the wire.)
|
| 922 | /// </remarks>
|
| 923 | internal static long DecodeZigZag64(ulong n)
|
| 924 | {
|
| 925 | return (long)(n >> 1) ^ -(long)(n & 1);
|
| 926 | }
|
| 927 | #endregion
|
| 928 |
|
| 929 | #region Internal reading and buffer management
|
| 930 |
|
| 931 | /// <summary>
|
| 932 | /// Sets currentLimit to (current position) + byteLimit. This is called
|
| 933 | /// when descending into a length-delimited embedded message. The previous
|
| 934 | /// limit is returned.
|
| 935 | /// </summary>
|
| 936 | /// <returns>The old limit.</returns>
|
| 937 | internal int PushLimit(int byteLimit)
|
| 938 | {
|
| 939 | if (byteLimit < 0)
|
| 940 | {
|
| 941 | throw InvalidProtocolBufferException.NegativeSize();
|
| 942 | }
|
| 943 | byteLimit += totalBytesRetired + bufferPos;
|
| 944 | int oldLimit = currentLimit;
|
| 945 | if (byteLimit > oldLimit)
|
| 946 | {
|
| 947 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 948 | }
|
| 949 | currentLimit = byteLimit;
|
| 950 |
|
| 951 | RecomputeBufferSizeAfterLimit();
|
| 952 |
|
| 953 | return oldLimit;
|
| 954 | }
|
| 955 |
|
| 956 | private void RecomputeBufferSizeAfterLimit()
|
| 957 | {
|
| 958 | bufferSize += bufferSizeAfterLimit;
|
| 959 | int bufferEnd = totalBytesRetired + bufferSize;
|
| 960 | if (bufferEnd > currentLimit)
|
| 961 | {
|
| 962 | // Limit is in current buffer.
|
| 963 | bufferSizeAfterLimit = bufferEnd - currentLimit;
|
| 964 | bufferSize -= bufferSizeAfterLimit;
|
| 965 | }
|
| 966 | else
|
| 967 | {
|
| 968 | bufferSizeAfterLimit = 0;
|
| 969 | }
|
| 970 | }
|
| 971 |
|
| 972 | /// <summary>
|
| 973 | /// Discards the current limit, returning the previous limit.
|
| 974 | /// </summary>
|
| 975 | internal void PopLimit(int oldLimit)
|
| 976 | {
|
| 977 | currentLimit = oldLimit;
|
| 978 | RecomputeBufferSizeAfterLimit();
|
| 979 | }
|
| 980 |
|
| 981 | /// <summary>
|
| 982 | /// Returns whether or not all the data before the limit has been read.
|
| 983 | /// </summary>
|
| 984 | /// <returns></returns>
|
| 985 | internal bool ReachedLimit
|
| 986 | {
|
| 987 | get
|
| 988 | {
|
| 989 | if (currentLimit == int.MaxValue)
|
| 990 | {
|
| 991 | return false;
|
| 992 | }
|
| 993 | int currentAbsolutePosition = totalBytesRetired + bufferPos;
|
| 994 | return currentAbsolutePosition >= currentLimit;
|
| 995 | }
|
| 996 | }
|
| 997 |
|
| 998 | /// <summary>
|
| 999 | /// Returns true if the stream has reached the end of the input. This is the
|
| 1000 | /// case if either the end of the underlying input source has been reached or
|
| 1001 | /// the stream has reached a limit created using PushLimit.
|
| 1002 | /// </summary>
|
| 1003 | public bool IsAtEnd
|
| 1004 | {
|
| 1005 | get { return bufferPos == bufferSize && !RefillBuffer(false); }
|
| 1006 | }
|
| 1007 |
|
| 1008 | /// <summary>
|
| 1009 | /// Called when buffer is empty to read more bytes from the
|
| 1010 | /// input. If <paramref name="mustSucceed"/> is true, RefillBuffer() gurantees that
|
| 1011 | /// either there will be at least one byte in the buffer when it returns
|
| 1012 | /// or it will throw an exception. If <paramref name="mustSucceed"/> is false,
|
| 1013 | /// RefillBuffer() returns false if no more bytes were available.
|
| 1014 | /// </summary>
|
| 1015 | /// <param name="mustSucceed"></param>
|
| 1016 | /// <returns></returns>
|
| 1017 | private bool RefillBuffer(bool mustSucceed)
|
| 1018 | {
|
| 1019 | if (bufferPos < bufferSize)
|
| 1020 | {
|
| 1021 | throw new InvalidOperationException("RefillBuffer() called when buffer wasn't empty.");
|
| 1022 | }
|
| 1023 |
|
| 1024 | if (totalBytesRetired + bufferSize == currentLimit)
|
| 1025 | {
|
| 1026 | // Oops, we hit a limit.
|
| 1027 | if (mustSucceed)
|
| 1028 | {
|
| 1029 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 1030 | }
|
| 1031 | else
|
| 1032 | {
|
| 1033 | return false;
|
| 1034 | }
|
| 1035 | }
|
| 1036 |
|
| 1037 | totalBytesRetired += bufferSize;
|
| 1038 |
|
| 1039 | bufferPos = 0;
|
| 1040 | bufferSize = (input == null) ? 0 : input.Read(buffer, 0, buffer.Length);
|
| 1041 | if (bufferSize < 0)
|
| 1042 | {
|
| 1043 | throw new InvalidOperationException("Stream.Read returned a negative count");
|
| 1044 | }
|
| 1045 | if (bufferSize == 0)
|
| 1046 | {
|
| 1047 | if (mustSucceed)
|
| 1048 | {
|
| 1049 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 1050 | }
|
| 1051 | else
|
| 1052 | {
|
| 1053 | return false;
|
| 1054 | }
|
| 1055 | }
|
| 1056 | else
|
| 1057 | {
|
| 1058 | RecomputeBufferSizeAfterLimit();
|
| 1059 | int totalBytesRead =
|
| 1060 | totalBytesRetired + bufferSize + bufferSizeAfterLimit;
|
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1061 | if (totalBytesRead < 0 || totalBytesRead > sizeLimit)
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1062 | {
|
| 1063 | throw InvalidProtocolBufferException.SizeLimitExceeded();
|
| 1064 | }
|
| 1065 | return true;
|
| 1066 | }
|
| 1067 | }
|
| 1068 |
|
| 1069 | /// <summary>
|
| 1070 | /// Read one byte from the input.
|
| 1071 | /// </summary>
|
| 1072 | /// <exception cref="InvalidProtocolBufferException">
|
| 1073 | /// the end of the stream or the current limit was reached
|
| 1074 | /// </exception>
|
| 1075 | internal byte ReadRawByte()
|
| 1076 | {
|
| 1077 | if (bufferPos == bufferSize)
|
| 1078 | {
|
| 1079 | RefillBuffer(true);
|
| 1080 | }
|
| 1081 | return buffer[bufferPos++];
|
| 1082 | }
|
| 1083 |
|
| 1084 | /// <summary>
|
| 1085 | /// Reads a fixed size of bytes from the input.
|
| 1086 | /// </summary>
|
| 1087 | /// <exception cref="InvalidProtocolBufferException">
|
| 1088 | /// the end of the stream or the current limit was reached
|
| 1089 | /// </exception>
|
| 1090 | internal byte[] ReadRawBytes(int size)
|
| 1091 | {
|
| 1092 | if (size < 0)
|
| 1093 | {
|
| 1094 | throw InvalidProtocolBufferException.NegativeSize();
|
| 1095 | }
|
| 1096 |
|
| 1097 | if (totalBytesRetired + bufferPos + size > currentLimit)
|
| 1098 | {
|
| 1099 | // Read to the end of the stream (up to the current limit) anyway.
|
| 1100 | SkipRawBytes(currentLimit - totalBytesRetired - bufferPos);
|
| 1101 | // Then fail.
|
| 1102 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 1103 | }
|
| 1104 |
|
| 1105 | if (size <= bufferSize - bufferPos)
|
| 1106 | {
|
| 1107 | // We have all the bytes we need already.
|
| 1108 | byte[] bytes = new byte[size];
|
| 1109 | ByteArray.Copy(buffer, bufferPos, bytes, 0, size);
|
| 1110 | bufferPos += size;
|
| 1111 | return bytes;
|
| 1112 | }
|
| 1113 | else if (size < buffer.Length)
|
| 1114 | {
|
| 1115 | // Reading more bytes than are in the buffer, but not an excessive number
|
| 1116 | // of bytes. We can safely allocate the resulting array ahead of time.
|
| 1117 |
|
| 1118 | // First copy what we have.
|
| 1119 | byte[] bytes = new byte[size];
|
| 1120 | int pos = bufferSize - bufferPos;
|
| 1121 | ByteArray.Copy(buffer, bufferPos, bytes, 0, pos);
|
| 1122 | bufferPos = bufferSize;
|
| 1123 |
|
| 1124 | // We want to use RefillBuffer() and then copy from the buffer into our
|
| 1125 | // byte array rather than reading directly into our byte array because
|
| 1126 | // the input may be unbuffered.
|
| 1127 | RefillBuffer(true);
|
| 1128 |
|
| 1129 | while (size - pos > bufferSize)
|
| 1130 | {
|
| 1131 | Buffer.BlockCopy(buffer, 0, bytes, pos, bufferSize);
|
| 1132 | pos += bufferSize;
|
| 1133 | bufferPos = bufferSize;
|
| 1134 | RefillBuffer(true);
|
| 1135 | }
|
| 1136 |
|
| 1137 | ByteArray.Copy(buffer, 0, bytes, pos, size - pos);
|
| 1138 | bufferPos = size - pos;
|
| 1139 |
|
| 1140 | return bytes;
|
| 1141 | }
|
| 1142 | else
|
| 1143 | {
|
| 1144 | // The size is very large. For security reasons, we can't allocate the
|
| 1145 | // entire byte array yet. The size comes directly from the input, so a
|
| 1146 | // maliciously-crafted message could provide a bogus very large size in
|
| 1147 | // order to trick the app into allocating a lot of memory. We avoid this
|
| 1148 | // by allocating and reading only a small chunk at a time, so that the
|
| 1149 | // malicious message must actually *be* extremely large to cause
|
| 1150 | // problems. Meanwhile, we limit the allowed size of a message elsewhere.
|
| 1151 |
|
| 1152 | // Remember the buffer markers since we'll have to copy the bytes out of
|
| 1153 | // it later.
|
| 1154 | int originalBufferPos = bufferPos;
|
| 1155 | int originalBufferSize = bufferSize;
|
| 1156 |
|
| 1157 | // Mark the current buffer consumed.
|
| 1158 | totalBytesRetired += bufferSize;
|
| 1159 | bufferPos = 0;
|
| 1160 | bufferSize = 0;
|
| 1161 |
|
| 1162 | // Read all the rest of the bytes we need.
|
| 1163 | int sizeLeft = size - (originalBufferSize - originalBufferPos);
|
| 1164 | List<byte[]> chunks = new List<byte[]>();
|
| 1165 |
|
| 1166 | while (sizeLeft > 0)
|
| 1167 | {
|
| 1168 | byte[] chunk = new byte[Math.Min(sizeLeft, buffer.Length)];
|
| 1169 | int pos = 0;
|
| 1170 | while (pos < chunk.Length)
|
| 1171 | {
|
| 1172 | int n = (input == null) ? -1 : input.Read(chunk, pos, chunk.Length - pos);
|
| 1173 | if (n <= 0)
|
| 1174 | {
|
| 1175 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 1176 | }
|
| 1177 | totalBytesRetired += n;
|
| 1178 | pos += n;
|
| 1179 | }
|
| 1180 | sizeLeft -= chunk.Length;
|
| 1181 | chunks.Add(chunk);
|
| 1182 | }
|
| 1183 |
|
| 1184 | // OK, got everything. Now concatenate it all into one buffer.
|
| 1185 | byte[] bytes = new byte[size];
|
| 1186 |
|
| 1187 | // Start by copying the leftover bytes from this.buffer.
|
| 1188 | int newPos = originalBufferSize - originalBufferPos;
|
| 1189 | ByteArray.Copy(buffer, originalBufferPos, bytes, 0, newPos);
|
| 1190 |
|
| 1191 | // And now all the chunks.
|
| 1192 | foreach (byte[] chunk in chunks)
|
| 1193 | {
|
| 1194 | Buffer.BlockCopy(chunk, 0, bytes, newPos, chunk.Length);
|
| 1195 | newPos += chunk.Length;
|
| 1196 | }
|
| 1197 |
|
| 1198 | // Done.
|
| 1199 | return bytes;
|
| 1200 | }
|
| 1201 | }
|
| 1202 |
|
| 1203 | /// <summary>
|
| 1204 | /// Reads and discards <paramref name="size"/> bytes.
|
| 1205 | /// </summary>
|
| 1206 | /// <exception cref="InvalidProtocolBufferException">the end of the stream
|
| 1207 | /// or the current limit was reached</exception>
|
| 1208 | private void SkipRawBytes(int size)
|
| 1209 | {
|
| 1210 | if (size < 0)
|
| 1211 | {
|
| 1212 | throw InvalidProtocolBufferException.NegativeSize();
|
| 1213 | }
|
| 1214 |
|
| 1215 | if (totalBytesRetired + bufferPos + size > currentLimit)
|
| 1216 | {
|
| 1217 | // Read to the end of the stream anyway.
|
| 1218 | SkipRawBytes(currentLimit - totalBytesRetired - bufferPos);
|
| 1219 | // Then fail.
|
| 1220 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 1221 | }
|
| 1222 |
|
| 1223 | if (size <= bufferSize - bufferPos)
|
| 1224 | {
|
| 1225 | // We have all the bytes we need already.
|
| 1226 | bufferPos += size;
|
| 1227 | }
|
| 1228 | else
|
| 1229 | {
|
| 1230 | // Skipping more bytes than are in the buffer. First skip what we have.
|
| 1231 | int pos = bufferSize - bufferPos;
|
| 1232 |
|
| 1233 | // ROK 5/7/2013 Issue #54: should retire all bytes in buffer (bufferSize)
|
| 1234 | // totalBytesRetired += pos;
|
| 1235 | totalBytesRetired += bufferSize;
|
| 1236 |
|
| 1237 | bufferPos = 0;
|
| 1238 | bufferSize = 0;
|
| 1239 |
|
| 1240 | // Then skip directly from the InputStream for the rest.
|
| 1241 | if (pos < size)
|
| 1242 | {
|
| 1243 | if (input == null)
|
| 1244 | {
|
| 1245 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 1246 | }
|
| 1247 | SkipImpl(size - pos);
|
| 1248 | totalBytesRetired += size - pos;
|
| 1249 | }
|
| 1250 | }
|
| 1251 | }
|
| 1252 |
|
| 1253 | /// <summary>
|
| 1254 | /// Abstraction of skipping to cope with streams which can't really skip.
|
| 1255 | /// </summary>
|
| 1256 | private void SkipImpl(int amountToSkip)
|
| 1257 | {
|
| 1258 | if (input.CanSeek)
|
| 1259 | {
|
| 1260 | long previousPosition = input.Position;
|
| 1261 | input.Position += amountToSkip;
|
| 1262 | if (input.Position != previousPosition + amountToSkip)
|
| 1263 | {
|
| 1264 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 1265 | }
|
| 1266 | }
|
| 1267 | else
|
| 1268 | {
|
| 1269 | byte[] skipBuffer = new byte[Math.Min(1024, amountToSkip)];
|
| 1270 | while (amountToSkip > 0)
|
| 1271 | {
|
| 1272 | int bytesRead = input.Read(skipBuffer, 0, Math.Min(skipBuffer.Length, amountToSkip));
|
| 1273 | if (bytesRead <= 0)
|
| 1274 | {
|
| 1275 | throw InvalidProtocolBufferException.TruncatedMessage();
|
| 1276 | }
|
| 1277 | amountToSkip -= bytesRead;
|
| 1278 | }
|
| 1279 | }
|
| 1280 | }
|
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1281 | #endregion
|
| 1282 | }
|
| 1283 | } |