CodedInputStream.cs 46 KB

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