CodedInputStream.cs 47 KB

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