blob: e652af0d7acd8273a8f0c7d51b78026fb8a700a8 [file] [log] [blame]
Jon Skeet60c059b2008-10-23 21:17:56 +01001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// http://github.com/jskeet/dotnet-protobufs/
4// Original C++/Java/Python code:
Jon Skeet68036862008-10-22 13:30:34 +01005// http://code.google.com/p/protobuf/
6//
Jon Skeet60c059b2008-10-23 21:17:56 +01007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
Jon Skeet68036862008-10-22 13:30:34 +010010//
Jon Skeet60c059b2008-10-23 21:17:56 +010011// * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// * Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following disclaimer
15// in the documentation and/or other materials provided with the
16// distribution.
17// * Neither the name of Google Inc. nor the names of its
18// contributors may be used to endorse or promote products derived from
19// this software without specific prior written permission.
Jon Skeet68036862008-10-22 13:30:34 +010020//
Jon Skeet60c059b2008-10-23 21:17:56 +010021// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jon Skeet68036862008-10-22 13:30:34 +010032using System;
33using System.Collections.Generic;
34using System.IO;
35using System.Text;
36using Google.ProtocolBuffers.Descriptors;
37
38namespace Google.ProtocolBuffers {
39
40 /// <summary>
41 /// Readings and decodes protocol message fields.
42 /// </summary>
43 /// <remarks>
44 /// This class contains two kinds of methods: methods that read specific
45 /// protocol message constructs and field types (e.g. ReadTag and
46 /// ReadInt32) and methods that read low-level values (e.g.
47 /// ReadRawVarint32 and ReadRawBytes). If you are reading encoded protocol
48 /// messages, you should use the former methods, but if you are reading some
49 /// other format of your own design, use the latter. The names of the former
50 /// methods are taken from the protocol buffer type names, not .NET types.
51 /// (Hence ReadFloat instead of ReadSingle, and ReadBool instead of ReadBoolean.)
52 ///
53 /// TODO(jonskeet): Consider whether recursion and size limits shouldn't be readonly,
54 /// set at construction time.
55 /// </remarks>
56 public sealed class CodedInputStream {
57 private readonly byte[] buffer;
58 private int bufferSize;
59 private int bufferSizeAfterLimit = 0;
60 private int bufferPos = 0;
61 private readonly Stream input;
62 private uint lastTag = 0;
63
Jon Skeet2178b932009-06-25 07:52:07 +010064 internal const int DefaultRecursionLimit = 64;
65 internal const int DefaultSizeLimit = 64 << 20; // 64MB
66 internal const int BufferSize = 4096;
Jon Skeet68036862008-10-22 13:30:34 +010067
68 /// <summary>
69 /// The total number of bytes read before the current buffer. The
70 /// total bytes read up to the current position can be computed as
71 /// totalBytesRetired + bufferPos.
72 /// </summary>
73 private int totalBytesRetired = 0;
74
75 /// <summary>
76 /// The absolute position of the end of the current message.
77 /// </summary>
78 private int currentLimit = int.MaxValue;
79
80 /// <summary>
81 /// <see cref="SetRecursionLimit"/>
82 /// </summary>
83 private int recursionDepth = 0;
84 private int recursionLimit = DefaultRecursionLimit;
85
86 /// <summary>
87 /// <see cref="SetSizeLimit"/>
88 /// </summary>
89 private int sizeLimit = DefaultSizeLimit;
90
91 #region Construction
92 /// <summary>
93 /// Creates a new CodedInputStream reading data from the given
94 /// stream.
95 /// </summary>
96 public static CodedInputStream CreateInstance(Stream input) {
97 return new CodedInputStream(input);
98 }
99
100 /// <summary>
101 /// Creates a new CodedInputStream reading data from the given
102 /// byte array.
103 /// </summary>
104 public static CodedInputStream CreateInstance(byte[] buf) {
105 return new CodedInputStream(buf);
106 }
107
108 private CodedInputStream(byte[] buffer) {
109 this.buffer = buffer;
110 this.bufferSize = buffer.Length;
111 this.input = null;
112 }
113
114 private CodedInputStream(Stream input) {
115 this.buffer = new byte[BufferSize];
116 this.bufferSize = 0;
117 this.input = input;
118 }
119 #endregion
120
121 #region Validation
122 /// <summary>
123 /// Verifies that the last call to ReadTag() returned the given tag value.
124 /// This is used to verify that a nested group ended with the correct
125 /// end tag.
126 /// </summary>
127 /// <exception cref="InvalidProtocolBufferException">The last
128 /// tag read was not the one specified</exception>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100129 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100130 public void CheckLastTagWas(uint value) {
131 if (lastTag != value) {
132 throw InvalidProtocolBufferException.InvalidEndTag();
133 }
134 }
135 #endregion
136
137 #region Reading of tags etc
138 /// <summary>
139 /// Attempt to read a field tag, returning 0 if we have reached the end
140 /// of the input data. Protocol message parsers use this to read tags,
141 /// since a protocol message may legally end wherever a tag occurs, and
142 /// zero is not a valid tag number.
143 /// </summary>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100144 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100145 public uint ReadTag() {
Jon Skeet2e6dc122009-05-29 06:34:52 +0100146 if (IsAtEnd) {
Jon Skeet68036862008-10-22 13:30:34 +0100147 lastTag = 0;
148 return 0;
149 }
150
151 lastTag = ReadRawVarint32();
152 if (lastTag == 0) {
153 // If we actually read zero, that's not a valid tag.
154 throw InvalidProtocolBufferException.InvalidTag();
155 }
156 return lastTag;
157 }
158
159 /// <summary>
160 /// Read a double field from the stream.
161 /// </summary>
162 public double ReadDouble() {
163 // TODO(jonskeet): Test this on different endiannesses
164 return BitConverter.Int64BitsToDouble((long) ReadRawLittleEndian64());
165 }
166
167 /// <summary>
168 /// Read a float field from the stream.
169 /// </summary>
170 public float ReadFloat() {
171 // TODO(jonskeet): Test this on different endiannesses
172 uint raw = ReadRawLittleEndian32();
173 byte[] rawBytes = BitConverter.GetBytes(raw);
174 return BitConverter.ToSingle(rawBytes, 0);
175 }
176
177 /// <summary>
178 /// Read a uint64 field from the stream.
179 /// </summary>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100180 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100181 public ulong ReadUInt64() {
182 return ReadRawVarint64();
183 }
184
185 /// <summary>
186 /// Read an int64 field from the stream.
187 /// </summary>
188 public long ReadInt64() {
189 return (long) ReadRawVarint64();
190 }
191
192 /// <summary>
193 /// Read an int32 field from the stream.
194 /// </summary>
195 public int ReadInt32() {
196 return (int) ReadRawVarint32();
197 }
198
199 /// <summary>
200 /// Read a fixed64 field from the stream.
201 /// </summary>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100202 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100203 public ulong ReadFixed64() {
204 return ReadRawLittleEndian64();
205 }
206
207 /// <summary>
208 /// Read a fixed32 field from the stream.
209 /// </summary>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100210 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100211 public uint ReadFixed32() {
212 return ReadRawLittleEndian32();
213 }
214
215 /// <summary>
216 /// Read a bool field from the stream.
217 /// </summary>
218 public bool ReadBool() {
219 return ReadRawVarint32() != 0;
220 }
221
222 /// <summary>
223 /// Reads a string field from the stream.
224 /// </summary>
225 public String ReadString() {
226 int size = (int) ReadRawVarint32();
Jon Skeet6a60ac32009-01-27 14:47:35 +0000227 // No need to read any data for an empty string.
228 if (size == 0) {
229 return "";
230 }
231 if (size <= bufferSize - bufferPos) {
Jon Skeet68036862008-10-22 13:30:34 +0100232 // Fast path: We already have the bytes in a contiguous buffer, so
233 // just copy directly from it.
234 String result = Encoding.UTF8.GetString(buffer, bufferPos, size);
235 bufferPos += size;
236 return result;
Jon Skeet68036862008-10-22 13:30:34 +0100237 }
Jon Skeet60fb63e2009-06-20 20:46:28 +0100238 // Slow path: Build a byte array first then copy it.
239 return Encoding.UTF8.GetString(ReadRawBytes(size), 0, size);
Jon Skeet68036862008-10-22 13:30:34 +0100240 }
241
242 /// <summary>
243 /// Reads a group field value from the stream.
244 /// </summary>
245 public void ReadGroup(int fieldNumber, IBuilder builder,
246 ExtensionRegistry extensionRegistry) {
247 if (recursionDepth >= recursionLimit) {
248 throw InvalidProtocolBufferException.RecursionLimitExceeded();
249 }
250 ++recursionDepth;
251 builder.WeakMergeFrom(this, extensionRegistry);
252 CheckLastTagWas(WireFormat.MakeTag(fieldNumber, WireFormat.WireType.EndGroup));
253 --recursionDepth;
254 }
255
256 /// <summary>
257 /// Reads a group field value from the stream and merges it into the given
258 /// UnknownFieldSet.
259 /// </summary>
260 public void ReadUnknownGroup(int fieldNumber, UnknownFieldSet.Builder builder) {
261 if (recursionDepth >= recursionLimit) {
262 throw InvalidProtocolBufferException.RecursionLimitExceeded();
263 }
264 ++recursionDepth;
265 builder.MergeFrom(this);
266 CheckLastTagWas(WireFormat.MakeTag(fieldNumber, WireFormat.WireType.EndGroup));
267 --recursionDepth;
268 }
269
270 /// <summary>
271 /// Reads an embedded message field value from the stream.
272 /// </summary>
273 public void ReadMessage(IBuilder builder, ExtensionRegistry extensionRegistry) {
274 int length = (int) ReadRawVarint32();
275 if (recursionDepth >= recursionLimit) {
276 throw InvalidProtocolBufferException.RecursionLimitExceeded();
277 }
278 int oldLimit = PushLimit(length);
279 ++recursionDepth;
280 builder.WeakMergeFrom(this, extensionRegistry);
281 CheckLastTagWas(0);
282 --recursionDepth;
283 PopLimit(oldLimit);
284 }
285
286 /// <summary>
287 /// Reads a bytes field value from the stream.
288 /// </summary>
289 public ByteString ReadBytes() {
290 int size = (int) ReadRawVarint32();
291 if (size < bufferSize - bufferPos && size > 0) {
292 // Fast path: We already have the bytes in a contiguous buffer, so
293 // just copy directly from it.
294 ByteString result = ByteString.CopyFrom(buffer, bufferPos, size);
295 bufferPos += size;
296 return result;
297 } else {
298 // Slow path: Build a byte array first then copy it.
299 return ByteString.CopyFrom(ReadRawBytes(size));
300 }
301 }
302
303 /// <summary>
304 /// Reads a uint32 field value from the stream.
305 /// </summary>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100306 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100307 public uint ReadUInt32() {
308 return ReadRawVarint32();
309 }
310
311 /// <summary>
312 /// Reads an enum field value from the stream. The caller is responsible
313 /// for converting the numeric value to an actual enum.
314 /// </summary>
315 public int ReadEnum() {
316 return (int) ReadRawVarint32();
317 }
318
319 /// <summary>
320 /// Reads an sfixed32 field value from the stream.
321 /// </summary>
322 public int ReadSFixed32() {
323 return (int) ReadRawLittleEndian32();
324 }
325
326 /// <summary>
327 /// Reads an sfixed64 field value from the stream.
328 /// </summary>
329 public long ReadSFixed64() {
330 return (long) ReadRawLittleEndian64();
331 }
332
333 /// <summary>
334 /// Reads an sint32 field value from the stream.
335 /// </summary>
336 public int ReadSInt32() {
337 return DecodeZigZag32(ReadRawVarint32());
338 }
339
340 /// <summary>
341 /// Reads an sint64 field value from the stream.
342 /// </summary>
343 public long ReadSInt64() {
344 return DecodeZigZag64(ReadRawVarint64());
345 }
346
347 /// <summary>
348 /// Reads a field of any primitive type. Enums, groups and embedded
349 /// messages are not handled by this method.
350 /// </summary>
351 public object ReadPrimitiveField(FieldType fieldType) {
352 switch (fieldType) {
353 case FieldType.Double: return ReadDouble();
354 case FieldType.Float: return ReadFloat();
355 case FieldType.Int64: return ReadInt64();
356 case FieldType.UInt64: return ReadUInt64();
357 case FieldType.Int32: return ReadInt32();
358 case FieldType.Fixed64: return ReadFixed64();
359 case FieldType.Fixed32: return ReadFixed32();
360 case FieldType.Bool: return ReadBool();
361 case FieldType.String: return ReadString();
362 case FieldType.Bytes: return ReadBytes();
363 case FieldType.UInt32: return ReadUInt32();
364 case FieldType.SFixed32: return ReadSFixed32();
365 case FieldType.SFixed64: return ReadSFixed64();
366 case FieldType.SInt32: return ReadSInt32();
367 case FieldType.SInt64: return ReadSInt64();
368 case FieldType.Group:
369 throw new ArgumentException("ReadPrimitiveField() cannot handle nested groups.");
370 case FieldType.Message:
371 throw new ArgumentException("ReadPrimitiveField() cannot handle embedded messages.");
372 // We don't handle enums because we don't know what to do if the
373 // value is not recognized.
374 case FieldType.Enum:
375 throw new ArgumentException("ReadPrimitiveField() cannot handle enums.");
376 default:
377 throw new ArgumentOutOfRangeException("Invalid field type " + fieldType);
378 }
379 }
380
381 #endregion
382
383 #region Underlying reading primitives
384
385 /// <summary>
386 /// Same code as ReadRawVarint32, but read each byte individually, checking for
387 /// buffer overflow.
388 /// </summary>
389 private uint SlowReadRawVarint32() {
390 int tmp = ReadRawByte();
391 if (tmp < 128) {
392 return (uint)tmp;
393 }
394 int result = tmp & 0x7f;
395 if ((tmp = ReadRawByte()) < 128) {
396 result |= tmp << 7;
397 } else {
398 result |= (tmp & 0x7f) << 7;
399 if ((tmp = ReadRawByte()) < 128) {
400 result |= tmp << 14;
401 } else {
402 result |= (tmp & 0x7f) << 14;
403 if ((tmp = ReadRawByte()) < 128) {
404 result |= tmp << 21;
405 } else {
406 result |= (tmp & 0x7f) << 21;
407 result |= (tmp = ReadRawByte()) << 28;
408 if (tmp >= 128) {
409 // Discard upper 32 bits.
410 for (int i = 0; i < 5; i++) {
411 if (ReadRawByte() < 128) return (uint)result;
412 }
413 throw InvalidProtocolBufferException.MalformedVarint();
414 }
415 }
416 }
417 }
418 return (uint)result;
419 }
420
421 /// <summary>
422 /// Read a raw Varint from the stream. If larger than 32 bits, discard the upper bits.
423 /// This method is optimised for the case where we've got lots of data in the buffer.
424 /// That means we can check the size just once, then just read directly from the buffer
425 /// without constant rechecking of the buffer length.
426 /// </summary>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100427 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100428 public uint ReadRawVarint32() {
429 if (bufferPos + 5 > bufferSize) {
430 return SlowReadRawVarint32();
431 }
432
433 int tmp = buffer[bufferPos++];
434 if (tmp < 128) {
435 return (uint)tmp;
436 }
437 int result = tmp & 0x7f;
438 if ((tmp = buffer[bufferPos++]) < 128) {
439 result |= tmp << 7;
440 } else {
441 result |= (tmp & 0x7f) << 7;
442 if ((tmp = buffer[bufferPos++]) < 128) {
443 result |= tmp << 14;
444 } else {
445 result |= (tmp & 0x7f) << 14;
446 if ((tmp = buffer[bufferPos++]) < 128) {
447 result |= tmp << 21;
448 } else {
449 result |= (tmp & 0x7f) << 21;
450 result |= (tmp = buffer[bufferPos++]) << 28;
451 if (tmp >= 128) {
452 // Discard upper 32 bits.
453 // Note that this has to use ReadRawByte() as we only ensure we've
454 // got at least 5 bytes at the start of the method. This lets us
455 // use the fast path in more cases, and we rarely hit this section of code.
456 for (int i = 0; i < 5; i++) {
457 if (ReadRawByte() < 128) return (uint)result;
458 }
459 throw InvalidProtocolBufferException.MalformedVarint();
460 }
461 }
462 }
463 }
464 return (uint)result;
465 }
466
467 /// <summary>
Jon Skeet2e6dc122009-05-29 06:34:52 +0100468 /// Reads a varint from the input one byte at a time, so that it does not
469 /// read any bytes after the end of the varint. If you simply wrapped the
470 /// stream in a CodedInputStream and used ReadRawVarint32(Stream)}
471 /// then you would probably end up reading past the end of the varint since
472 /// CodedInputStream buffers its input.
473 /// </summary>
474 /// <param name="input"></param>
475 /// <returns></returns>
Jon Skeetc298c892009-05-30 10:07:09 +0100476 internal static uint ReadRawVarint32(Stream input) {
Jon Skeet2e6dc122009-05-29 06:34:52 +0100477 int result = 0;
478 int offset = 0;
479 for (; offset < 32; offset += 7) {
480 int b = input.ReadByte();
481 if (b == -1) {
482 throw InvalidProtocolBufferException.TruncatedMessage();
483 }
484 result |= (b & 0x7f) << offset;
485 if ((b & 0x80) == 0) {
Jon Skeetc298c892009-05-30 10:07:09 +0100486 return (uint) result;
Jon Skeet2e6dc122009-05-29 06:34:52 +0100487 }
488 }
489 // Keep reading up to 64 bits.
490 for (; offset < 64; offset += 7) {
491 int b = input.ReadByte();
492 if (b == -1) {
493 throw InvalidProtocolBufferException.TruncatedMessage();
494 }
495 if ((b & 0x80) == 0) {
Jon Skeetc298c892009-05-30 10:07:09 +0100496 return (uint) result;
Jon Skeet2e6dc122009-05-29 06:34:52 +0100497 }
498 }
499 throw InvalidProtocolBufferException.MalformedVarint();
500 }
501
502 /// <summary>
Jon Skeet68036862008-10-22 13:30:34 +0100503 /// Read a raw varint from the stream.
504 /// </summary>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100505 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100506 public ulong ReadRawVarint64() {
507 int shift = 0;
508 ulong result = 0;
509 while (shift < 64) {
510 byte b = ReadRawByte();
511 result |= (ulong)(b & 0x7F) << shift;
512 if ((b & 0x80) == 0) {
513 return result;
514 }
515 shift += 7;
516 }
517 throw InvalidProtocolBufferException.MalformedVarint();
518 }
519
520 /// <summary>
521 /// Read a 32-bit little-endian integer from the stream.
522 /// </summary>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100523 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100524 public uint ReadRawLittleEndian32() {
525 uint b1 = ReadRawByte();
526 uint b2 = ReadRawByte();
527 uint b3 = ReadRawByte();
528 uint b4 = ReadRawByte();
529 return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
530 }
531
532 /// <summary>
533 /// Read a 64-bit little-endian integer from the stream.
534 /// </summary>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100535 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100536 public ulong ReadRawLittleEndian64() {
537 ulong b1 = ReadRawByte();
538 ulong b2 = ReadRawByte();
539 ulong b3 = ReadRawByte();
540 ulong b4 = ReadRawByte();
541 ulong b5 = ReadRawByte();
542 ulong b6 = ReadRawByte();
543 ulong b7 = ReadRawByte();
544 ulong b8 = ReadRawByte();
545 return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24)
546 | (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);
547 }
548 #endregion
549
550 /// <summary>
551 /// Decode a 32-bit value with ZigZag encoding.
552 /// </summary>
553 /// <remarks>
554 /// ZigZag encodes signed integers into values that can be efficiently
555 /// encoded with varint. (Otherwise, negative values must be
556 /// sign-extended to 64 bits to be varint encoded, thus always taking
557 /// 10 bytes on the wire.)
558 /// </remarks>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100559 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100560 public static int DecodeZigZag32(uint n) {
561 return (int)(n >> 1) ^ -(int)(n & 1);
562 }
563
564 /// <summary>
565 /// Decode a 32-bit value with ZigZag encoding.
566 /// </summary>
567 /// <remarks>
568 /// ZigZag encodes signed integers into values that can be efficiently
569 /// encoded with varint. (Otherwise, negative values must be
570 /// sign-extended to 64 bits to be varint encoded, thus always taking
571 /// 10 bytes on the wire.)
572 /// </remarks>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100573 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100574 public static long DecodeZigZag64(ulong n) {
575 return (long)(n >> 1) ^ -(long)(n & 1);
576 }
577
578 /// <summary>
579 /// Set the maximum message recursion depth.
580 /// </summary>
581 /// <remarks>
582 /// In order to prevent malicious
583 /// messages from causing stack overflows, CodedInputStream limits
584 /// how deeply messages may be nested. The default limit is 64.
585 /// </remarks>
586 public int SetRecursionLimit(int limit) {
587 if (limit < 0) {
588 throw new ArgumentOutOfRangeException("Recursion limit cannot be negative: " + limit);
589 }
590 int oldLimit = recursionLimit;
591 recursionLimit = limit;
592 return oldLimit;
593 }
594
595 /// <summary>
596 /// Set the maximum message size.
597 /// </summary>
598 /// <remarks>
599 /// In order to prevent malicious messages from exhausting memory or
600 /// causing integer overflows, CodedInputStream limits how large a message may be.
601 /// The default limit is 64MB. You should set this limit as small
602 /// as you can without harming your app's functionality. Note that
603 /// size limits only apply when reading from an InputStream, not
604 /// when constructed around a raw byte array (nor with ByteString.NewCodedInput).
Jon Skeet2e6dc122009-05-29 06:34:52 +0100605 /// If you want to read several messages from a single CodedInputStream, you
606 /// can call ResetSizeCounter() after each message to avoid hitting the
607 /// size limit.
Jon Skeet68036862008-10-22 13:30:34 +0100608 /// </remarks>
609 public int SetSizeLimit(int limit) {
610 if (limit < 0) {
611 throw new ArgumentOutOfRangeException("Size limit cannot be negative: " + limit);
612 }
613 int oldLimit = sizeLimit;
614 sizeLimit = limit;
615 return oldLimit;
616 }
617
618 #region Internal reading and buffer management
619 /// <summary>
Jon Skeet2e6dc122009-05-29 06:34:52 +0100620 /// Resets the current size counter to zero (see SetSizeLimit).
621 /// </summary>
622 public void ResetSizeCounter() {
623 totalBytesRetired = 0;
624 }
625
626 /// <summary>
Jon Skeet68036862008-10-22 13:30:34 +0100627 /// Sets currentLimit to (current position) + byteLimit. This is called
628 /// when descending into a length-delimited embedded message. The previous
629 /// limit is returned.
630 /// </summary>
631 /// <returns>The old limit.</returns>
632 public int PushLimit(int byteLimit) {
633 if (byteLimit < 0) {
634 throw InvalidProtocolBufferException.NegativeSize();
635 }
636 byteLimit += totalBytesRetired + bufferPos;
637 int oldLimit = currentLimit;
638 if (byteLimit > oldLimit) {
639 throw InvalidProtocolBufferException.TruncatedMessage();
640 }
641 currentLimit = byteLimit;
642
643 RecomputeBufferSizeAfterLimit();
644
645 return oldLimit;
646 }
647
648 private void RecomputeBufferSizeAfterLimit() {
649 bufferSize += bufferSizeAfterLimit;
650 int bufferEnd = totalBytesRetired + bufferSize;
651 if (bufferEnd > currentLimit) {
652 // Limit is in current buffer.
653 bufferSizeAfterLimit = bufferEnd - currentLimit;
654 bufferSize -= bufferSizeAfterLimit;
655 } else {
656 bufferSizeAfterLimit = 0;
657 }
658 }
659
660 /// <summary>
661 /// Discards the current limit, returning the previous limit.
662 /// </summary>
663 public void PopLimit(int oldLimit) {
664 currentLimit = oldLimit;
665 RecomputeBufferSizeAfterLimit();
666 }
667
668 /// <summary>
Jon Skeet25a28582009-02-18 16:06:22 +0000669 /// Returns whether or not all the data before the limit has been read.
670 /// </summary>
671 /// <returns></returns>
672 public bool ReachedLimit {
673 get {
674 if (currentLimit == int.MaxValue) {
675 return false;
676 }
677 int currentAbsolutePosition = totalBytesRetired + bufferPos;
678 return currentAbsolutePosition >= currentLimit;
679 }
680 }
Jon Skeet2e6dc122009-05-29 06:34:52 +0100681
682 /// <summary>
683 /// Returns true if the stream has reached the end of the input. This is the
684 /// case if either the end of the underlying input source has been reached or
685 /// the stream has reached a limit created using PushLimit.
686 /// </summary>
687 public bool IsAtEnd {
688 get {
689 return bufferPos == bufferSize && !RefillBuffer(false);
690 }
691 }
Jon Skeet25a28582009-02-18 16:06:22 +0000692
693 /// <summary>
Jon Skeet68036862008-10-22 13:30:34 +0100694 /// Called when buffer is empty to read more bytes from the
695 /// input. If <paramref name="mustSucceed"/> is true, RefillBuffer() gurantees that
696 /// either there will be at least one byte in the buffer when it returns
697 /// or it will throw an exception. If <paramref name="mustSucceed"/> is false,
698 /// RefillBuffer() returns false if no more bytes were available.
699 /// </summary>
700 /// <param name="mustSucceed"></param>
701 /// <returns></returns>
702 private bool RefillBuffer(bool mustSucceed) {
703 if (bufferPos < bufferSize) {
704 throw new InvalidOperationException("RefillBuffer() called when buffer wasn't empty.");
705 }
706
707 if (totalBytesRetired + bufferSize == currentLimit) {
708 // Oops, we hit a limit.
709 if (mustSucceed) {
710 throw InvalidProtocolBufferException.TruncatedMessage();
711 } else {
712 return false;
713 }
714 }
715
716 totalBytesRetired += bufferSize;
717
718 bufferPos = 0;
719 bufferSize = (input == null) ? 0 : input.Read(buffer, 0, buffer.Length);
Jon Skeet2e6dc122009-05-29 06:34:52 +0100720 if (bufferSize < 0) {
721 throw new InvalidOperationException("Stream.Read returned a negative count");
722 }
Jon Skeet68036862008-10-22 13:30:34 +0100723 if (bufferSize == 0) {
724 if (mustSucceed) {
725 throw InvalidProtocolBufferException.TruncatedMessage();
726 } else {
727 return false;
728 }
729 } else {
730 RecomputeBufferSizeAfterLimit();
731 int totalBytesRead =
732 totalBytesRetired + bufferSize + bufferSizeAfterLimit;
733 if (totalBytesRead > sizeLimit || totalBytesRead < 0) {
734 throw InvalidProtocolBufferException.SizeLimitExceeded();
735 }
736 return true;
737 }
738 }
739
740 /// <summary>
741 /// Read one byte from the input.
742 /// </summary>
743 /// <exception cref="InvalidProtocolBufferException">
Jon Skeet2178b932009-06-25 07:52:07 +0100744 /// the end of the stream or the current limit was reached
Jon Skeet68036862008-10-22 13:30:34 +0100745 /// </exception>
746 public byte ReadRawByte() {
747 if (bufferPos == bufferSize) {
748 RefillBuffer(true);
749 }
750 return buffer[bufferPos++];
751 }
752
753 /// <summary>
754 /// Read a fixed size of bytes from the input.
755 /// </summary>
756 /// <exception cref="InvalidProtocolBufferException">
757 /// the end of the stream or the current limit was reached
758 /// </exception>
759 public byte[] ReadRawBytes(int size) {
760 if (size < 0) {
761 throw InvalidProtocolBufferException.NegativeSize();
762 }
763
764 if (totalBytesRetired + bufferPos + size > currentLimit) {
765 // Read to the end of the stream anyway.
766 SkipRawBytes(currentLimit - totalBytesRetired - bufferPos);
767 // Then fail.
768 throw InvalidProtocolBufferException.TruncatedMessage();
769 }
770
771 if (size <= bufferSize - bufferPos) {
772 // We have all the bytes we need already.
773 byte[] bytes = new byte[size];
774 Array.Copy(buffer, bufferPos, bytes, 0, size);
775 bufferPos += size;
776 return bytes;
777 } else if (size < BufferSize) {
778 // Reading more bytes than are in the buffer, but not an excessive number
779 // of bytes. We can safely allocate the resulting array ahead of time.
780
781 // First copy what we have.
782 byte[] bytes = new byte[size];
783 int pos = bufferSize - bufferPos;
784 Array.Copy(buffer, bufferPos, bytes, 0, pos);
785 bufferPos = bufferSize;
786
787 // We want to use RefillBuffer() and then copy from the buffer into our
788 // byte array rather than reading directly into our byte array because
789 // the input may be unbuffered.
790 RefillBuffer(true);
791
792 while (size - pos > bufferSize) {
793 Array.Copy(buffer, 0, bytes, pos, bufferSize);
794 pos += bufferSize;
795 bufferPos = bufferSize;
796 RefillBuffer(true);
797 }
798
799 Array.Copy(buffer, 0, bytes, pos, size - pos);
800 bufferPos = size - pos;
801
802 return bytes;
803 } else {
804 // The size is very large. For security reasons, we can't allocate the
805 // entire byte array yet. The size comes directly from the input, so a
806 // maliciously-crafted message could provide a bogus very large size in
807 // order to trick the app into allocating a lot of memory. We avoid this
808 // by allocating and reading only a small chunk at a time, so that the
809 // malicious message must actually *be* extremely large to cause
810 // problems. Meanwhile, we limit the allowed size of a message elsewhere.
811
812 // Remember the buffer markers since we'll have to copy the bytes out of
813 // it later.
814 int originalBufferPos = bufferPos;
815 int originalBufferSize = bufferSize;
816
817 // Mark the current buffer consumed.
818 totalBytesRetired += bufferSize;
819 bufferPos = 0;
820 bufferSize = 0;
821
822 // Read all the rest of the bytes we need.
823 int sizeLeft = size - (originalBufferSize - originalBufferPos);
824 List<byte[]> chunks = new List<byte[]>();
825
826 while (sizeLeft > 0) {
827 byte[] chunk = new byte[Math.Min(sizeLeft, BufferSize)];
828 int pos = 0;
829 while (pos < chunk.Length) {
830 int n = (input == null) ? -1 : input.Read(chunk, pos, chunk.Length - pos);
831 if (n <= 0) {
832 throw InvalidProtocolBufferException.TruncatedMessage();
833 }
834 totalBytesRetired += n;
835 pos += n;
836 }
837 sizeLeft -= chunk.Length;
838 chunks.Add(chunk);
839 }
840
841 // OK, got everything. Now concatenate it all into one buffer.
842 byte[] bytes = new byte[size];
843
844 // Start by copying the leftover bytes from this.buffer.
845 int newPos = originalBufferSize - originalBufferPos;
846 Array.Copy(buffer, originalBufferPos, bytes, 0, newPos);
847
848 // And now all the chunks.
849 foreach (byte[] chunk in chunks) {
850 Array.Copy(chunk, 0, bytes, newPos, chunk.Length);
851 newPos += chunk.Length;
852 }
853
854 // Done.
855 return bytes;
856 }
857 }
858
859 /// <summary>
860 /// Reads and discards a single field, given its tag value.
861 /// </summary>
862 /// <returns>false if the tag is an end-group tag, in which case
863 /// nothing is skipped. Otherwise, returns true.</returns>
Jon Skeetd6dd0a42009-06-05 22:00:05 +0100864 [CLSCompliant(false)]
Jon Skeet68036862008-10-22 13:30:34 +0100865 public bool SkipField(uint tag) {
866 switch (WireFormat.GetTagWireType(tag)) {
867 case WireFormat.WireType.Varint:
868 ReadInt32();
869 return true;
870 case WireFormat.WireType.Fixed64:
871 ReadRawLittleEndian64();
872 return true;
873 case WireFormat.WireType.LengthDelimited:
874 SkipRawBytes((int) ReadRawVarint32());
875 return true;
876 case WireFormat.WireType.StartGroup:
877 SkipMessage();
878 CheckLastTagWas(
879 WireFormat.MakeTag(WireFormat.GetTagFieldNumber(tag),
880 WireFormat.WireType.EndGroup));
881 return true;
882 case WireFormat.WireType.EndGroup:
883 return false;
884 case WireFormat.WireType.Fixed32:
885 ReadRawLittleEndian32();
886 return true;
887 default:
888 throw InvalidProtocolBufferException.InvalidWireType();
889 }
890 }
891
892 /// <summary>
893 /// Reads and discards an entire message. This will read either until EOF
894 /// or until an endgroup tag, whichever comes first.
895 /// </summary>
896 public void SkipMessage() {
897 while (true) {
898 uint tag = ReadTag();
899 if (tag == 0 || !SkipField(tag)) {
900 return;
901 }
902 }
903 }
904
905 /// <summary>
906 /// Reads and discards <paramref name="size"/> bytes.
907 /// </summary>
908 /// <exception cref="InvalidProtocolBufferException">the end of the stream
909 /// or the current limit was reached</exception>
910 public void SkipRawBytes(int size) {
911 if (size < 0) {
912 throw InvalidProtocolBufferException.NegativeSize();
913 }
914
915 if (totalBytesRetired + bufferPos + size > currentLimit) {
916 // Read to the end of the stream anyway.
917 SkipRawBytes(currentLimit - totalBytesRetired - bufferPos);
918 // Then fail.
919 throw InvalidProtocolBufferException.TruncatedMessage();
920 }
921
Jon Skeet2e6dc122009-05-29 06:34:52 +0100922 if (size <= bufferSize - bufferPos) {
Jon Skeet68036862008-10-22 13:30:34 +0100923 // We have all the bytes we need already.
924 bufferPos += size;
925 } else {
926 // Skipping more bytes than are in the buffer. First skip what we have.
927 int pos = bufferSize - bufferPos;
928 totalBytesRetired += pos;
929 bufferPos = 0;
930 bufferSize = 0;
931
932 // Then skip directly from the InputStream for the rest.
933 if (pos < size) {
Jon Skeet68036862008-10-22 13:30:34 +0100934 if (input == null) {
935 throw InvalidProtocolBufferException.TruncatedMessage();
936 }
Jon Skeetc298c892009-05-30 10:07:09 +0100937 SkipImpl(size - pos);
938 totalBytesRetired += size - pos;
939 }
940 }
941 }
942
943 /// <summary>
944 /// Abstraction of skipping to cope with streams which can't really skip.
945 /// </summary>
946 private void SkipImpl(int amountToSkip) {
947 if (input.CanSeek) {
948 long previousPosition = input.Position;
949 input.Position += amountToSkip;
950 if (input.Position != previousPosition + amountToSkip) {
951 throw InvalidProtocolBufferException.TruncatedMessage();
952 }
953 } else {
954 byte[] skipBuffer = new byte[1024];
955 while (amountToSkip > 0) {
956 int bytesRead = input.Read(skipBuffer, 0, skipBuffer.Length);
957 if (bytesRead <= 0) {
Jon Skeet68036862008-10-22 13:30:34 +0100958 throw InvalidProtocolBufferException.TruncatedMessage();
959 }
Jon Skeetc298c892009-05-30 10:07:09 +0100960 amountToSkip -= bytesRead;
Jon Skeet68036862008-10-22 13:30:34 +0100961 }
962 }
963 }
964 #endregion
965 }
966}