CodedInputStream.cs 47 KB

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