coded_stream.h revision fbaaef999ba563838ebd00874ed8a1c01fbf286d
1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// http://code.google.com/p/protobuf/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9//     * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11//     * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15//     * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// Author: kenton@google.com (Kenton Varda)
32//  Based on original Protocol Buffers design by
33//  Sanjay Ghemawat, Jeff Dean, and others.
34//
35// This file contains the CodedInputStream and CodedOutputStream classes,
36// which wrap a ZeroCopyInputStream or ZeroCopyOutputStream, respectively,
37// and allow you to read or write individual pieces of data in various
38// formats.  In particular, these implement the varint encoding for
39// integers, a simple variable-length encoding in which smaller numbers
40// take fewer bytes.
41//
42// Typically these classes will only be used internally by the protocol
43// buffer library in order to encode and decode protocol buffers.  Clients
44// of the library only need to know about this class if they wish to write
45// custom message parsing or serialization procedures.
46//
47// CodedOutputStream example:
48//   // Write some data to "myfile".  First we write a 4-byte "magic number"
49//   // to identify the file type, then write a length-delimited string.  The
50//   // string is composed of a varint giving the length followed by the raw
51//   // bytes.
52//   int fd = open("myfile", O_WRONLY);
53//   ZeroCopyOutputStream* raw_output = new FileOutputStream(fd);
54//   CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
55//
56//   int magic_number = 1234;
57//   char text[] = "Hello world!";
58//   coded_output->WriteLittleEndian32(magic_number);
59//   coded_output->WriteVarint32(strlen(text));
60//   coded_output->WriteRaw(text, strlen(text));
61//
62//   delete coded_output;
63//   delete raw_output;
64//   close(fd);
65//
66// CodedInputStream example:
67//   // Read a file created by the above code.
68//   int fd = open("myfile", O_RDONLY);
69//   ZeroCopyInputStream* raw_input = new FileInputStream(fd);
70//   CodedInputStream coded_input = new CodedInputStream(raw_input);
71//
72//   coded_input->ReadLittleEndian32(&magic_number);
73//   if (magic_number != 1234) {
74//     cerr << "File not in expected format." << endl;
75//     return;
76//   }
77//
78//   uint32 size;
79//   coded_input->ReadVarint32(&size);
80//
81//   char* text = new char[size + 1];
82//   coded_input->ReadRaw(buffer, size);
83//   text[size] = '\0';
84//
85//   delete coded_input;
86//   delete raw_input;
87//   close(fd);
88//
89//   cout << "Text is: " << text << endl;
90//   delete [] text;
91//
92// For those who are interested, varint encoding is defined as follows:
93//
94// The encoding operates on unsigned integers of up to 64 bits in length.
95// Each byte of the encoded value has the format:
96// * bits 0-6: Seven bits of the number being encoded.
97// * bit 7: Zero if this is the last byte in the encoding (in which
98//   case all remaining bits of the number are zero) or 1 if
99//   more bytes follow.
100// The first byte contains the least-significant 7 bits of the number, the
101// second byte (if present) contains the next-least-significant 7 bits,
102// and so on.  So, the binary number 1011000101011 would be encoded in two
103// bytes as "10101011 00101100".
104//
105// In theory, varint could be used to encode integers of any length.
106// However, for practicality we set a limit at 64 bits.  The maximum encoded
107// length of a number is thus 10 bytes.
108
109#ifndef GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
110#define GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
111
112#include <string>
113#ifndef _MSC_VER
114#include <sys/param.h>
115#endif  // !_MSC_VER
116#include <google/protobuf/stubs/common.h>
117
118namespace google {
119
120namespace protobuf {
121namespace io {
122
123// Defined in this file.
124class CodedInputStream;
125class CodedOutputStream;
126
127// Defined in other files.
128class ZeroCopyInputStream;           // zero_copy_stream.h
129class ZeroCopyOutputStream;          // zero_copy_stream.h
130
131// Class which reads and decodes binary data which is composed of varint-
132// encoded integers and fixed-width pieces.  Wraps a ZeroCopyInputStream.
133// Most users will not need to deal with CodedInputStream.
134//
135// Most methods of CodedInputStream that return a bool return false if an
136// underlying I/O error occurs or if the data is malformed.  Once such a
137// failure occurs, the CodedInputStream is broken and is no longer useful.
138class LIBPROTOBUF_EXPORT CodedInputStream {
139 public:
140  // Create a CodedInputStream that reads from the given ZeroCopyInputStream.
141  explicit CodedInputStream(ZeroCopyInputStream* input);
142
143  // Create a CodedInputStream that reads from the given flat array.  This is
144  // faster than using an ArrayInputStream.  PushLimit(size) is implied by
145  // this constructor.
146  explicit CodedInputStream(const uint8* buffer, int size);
147
148  // Destroy the CodedInputStream and position the underlying
149  // ZeroCopyInputStream at the first unread byte.  If an error occurred while
150  // reading (causing a method to return false), then the exact position of
151  // the input stream may be anywhere between the last value that was read
152  // successfully and the stream's byte limit.
153  ~CodedInputStream();
154
155
156  // Skips a number of bytes.  Returns false if an underlying read error
157  // occurs.
158  bool Skip(int count);
159
160  // Sets *data to point directly at the unread part of the CodedInputStream's
161  // underlying buffer, and *size to the size of that buffer, but does not
162  // advance the stream's current position.  This will always either produce
163  // a non-empty buffer or return false.  If the caller consumes any of
164  // this data, it should then call Skip() to skip over the consumed bytes.
165  // This may be useful for implementing external fast parsing routines for
166  // types of data not covered by the CodedInputStream interface.
167  bool GetDirectBufferPointer(const void** data, int* size);
168
169  // Read raw bytes, copying them into the given buffer.
170  bool ReadRaw(void* buffer, int size);
171
172  // Like ReadRaw, but reads into a string.
173  //
174  // Implementation Note:  ReadString() grows the string gradually as it
175  // reads in the data, rather than allocating the entire requested size
176  // upfront.  This prevents denial-of-service attacks in which a client
177  // could claim that a string is going to be MAX_INT bytes long in order to
178  // crash the server because it can't allocate this much space at once.
179  bool ReadString(string* buffer, int size);
180
181
182  // Read a 32-bit little-endian integer.
183  bool ReadLittleEndian32(uint32* value);
184  // Read a 64-bit little-endian integer.
185  bool ReadLittleEndian64(uint64* value);
186
187  // Read an unsigned integer with Varint encoding, truncating to 32 bits.
188  // Reading a 32-bit value is equivalent to reading a 64-bit one and casting
189  // it to uint32, but may be more efficient.
190  bool ReadVarint32(uint32* value);
191  // Read an unsigned integer with Varint encoding.
192  bool ReadVarint64(uint64* value);
193
194  // Read a tag.  This calls ReadVarint32() and returns the result, or returns
195  // zero (which is not a valid tag) if ReadVarint32() fails.  Also, it updates
196  // the last tag value, which can be checked with LastTagWas().
197  // Always inline because this is only called in once place per parse loop
198  // but it is called for every iteration of said loop, so it should be fast.
199  // GCC doesn't want to inline this by default.
200  uint32 ReadTag() GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
201
202  // Usually returns true if calling ReadVarint32() now would produce the given
203  // value.  Will always return false if ReadVarint32() would not return the
204  // given value.  If ExpectTag() returns true, it also advances past
205  // the varint.  For best performance, use a compile-time constant as the
206  // parameter.
207  // Always inline because this collapses to a small number of instructions
208  // when given a constant parameter, but GCC doesn't want to inline by default.
209  bool ExpectTag(uint32 expected) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
210
211  // Usually returns true if no more bytes can be read.  Always returns false
212  // if more bytes can be read.  If ExpectAtEnd() returns true, a subsequent
213  // call to LastTagWas() will act as if ReadTag() had been called and returned
214  // zero, and ConsumedEntireMessage() will return true.
215  bool ExpectAtEnd();
216
217  // If the last call to ReadTag() returned the given value, returns true.
218  // Otherwise, returns false;
219  //
220  // This is needed because parsers for some types of embedded messages
221  // (with field type TYPE_GROUP) don't actually know that they've reached the
222  // end of a message until they see an ENDGROUP tag, which was actually part
223  // of the enclosing message.  The enclosing message would like to check that
224  // tag to make sure it had the right number, so it calls LastTagWas() on
225  // return from the embedded parser to check.
226  bool LastTagWas(uint32 expected);
227
228  // When parsing message (but NOT a group), this method must be called
229  // immediately after MergeFromCodedStream() returns (if it returns true)
230  // to further verify that the message ended in a legitimate way.  For
231  // example, this verifies that parsing did not end on an end-group tag.
232  // It also checks for some cases where, due to optimizations,
233  // MergeFromCodedStream() can incorrectly return true.
234  bool ConsumedEntireMessage();
235
236  // Limits ----------------------------------------------------------
237  // Limits are used when parsing length-delimited embedded messages.
238  // After the message's length is read, PushLimit() is used to prevent
239  // the CodedInputStream from reading beyond that length.  Once the
240  // embedded message has been parsed, PopLimit() is called to undo the
241  // limit.
242
243  // Opaque type used with PushLimit() and PopLimit().  Do not modify
244  // values of this type yourself.  The only reason that this isn't a
245  // struct with private internals is for efficiency.
246  typedef int Limit;
247
248  // Places a limit on the number of bytes that the stream may read,
249  // starting from the current position.  Once the stream hits this limit,
250  // it will act like the end of the input has been reached until PopLimit()
251  // is called.
252  //
253  // As the names imply, the stream conceptually has a stack of limits.  The
254  // shortest limit on the stack is always enforced, even if it is not the
255  // top limit.
256  //
257  // The value returned by PushLimit() is opaque to the caller, and must
258  // be passed unchanged to the corresponding call to PopLimit().
259  Limit PushLimit(int byte_limit);
260
261  // Pops the last limit pushed by PushLimit().  The input must be the value
262  // returned by that call to PushLimit().
263  void PopLimit(Limit limit);
264
265  // Returns the number of bytes left until the nearest limit on the
266  // stack is hit, or -1 if no limits are in place.
267  int BytesUntilLimit();
268
269  // Total Bytes Limit -----------------------------------------------
270  // To prevent malicious users from sending excessively large messages
271  // and causing integer overflows or memory exhaustion, CodedInputStream
272  // imposes a hard limit on the total number of bytes it will read.
273
274  // Sets the maximum number of bytes that this CodedInputStream will read
275  // before refusing to continue.  To prevent integer overflows in the
276  // protocol buffers implementation, as well as to prevent servers from
277  // allocating enormous amounts of memory to hold parsed messages, the
278  // maximum message length should be limited to the shortest length that
279  // will not harm usability.  The theoretical shortest message that could
280  // cause integer overflows is 512MB.  The default limit is 64MB.  Apps
281  // should set shorter limits if possible.  If warning_threshold is not -1,
282  // a warning will be printed to stderr after warning_threshold bytes are
283  // read.  An error will always be printed to stderr if the limit is
284  // reached.
285  //
286  // This is unrelated to PushLimit()/PopLimit().
287  //
288  // Hint:  If you are reading this because your program is printing a
289  //   warning about dangerously large protocol messages, you may be
290  //   confused about what to do next.  The best option is to change your
291  //   design such that excessively large messages are not necessary.
292  //   For example, try to design file formats to consist of many small
293  //   messages rather than a single large one.  If this is infeasible,
294  //   you will need to increase the limit.  Chances are, though, that
295  //   your code never constructs a CodedInputStream on which the limit
296  //   can be set.  You probably parse messages by calling things like
297  //   Message::ParseFromString().  In this case, you will need to change
298  //   your code to instead construct some sort of ZeroCopyInputStream
299  //   (e.g. an ArrayInputStream), construct a CodedInputStream around
300  //   that, then call Message::ParseFromCodedStream() instead.  Then
301  //   you can adjust the limit.  Yes, it's more work, but you're doing
302  //   something unusual.
303  void SetTotalBytesLimit(int total_bytes_limit, int warning_threshold);
304
305  // Recursion Limit -------------------------------------------------
306  // To prevent corrupt or malicious messages from causing stack overflows,
307  // we must keep track of the depth of recursion when parsing embedded
308  // messages and groups.  CodedInputStream keeps track of this because it
309  // is the only object that is passed down the stack during parsing.
310
311  // Sets the maximum recursion depth.  The default is 64.
312  void SetRecursionLimit(int limit);
313
314  // Increments the current recursion depth.  Returns true if the depth is
315  // under the limit, false if it has gone over.
316  bool IncrementRecursionDepth();
317
318  // Decrements the recursion depth.
319  void DecrementRecursionDepth();
320
321 private:
322  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedInputStream);
323
324  ZeroCopyInputStream* input_;
325  const uint8* buffer_;
326  int buffer_size_;       // size of current buffer
327  int total_bytes_read_;  // total bytes read from input_, including
328                          // the current buffer
329
330  // If total_bytes_read_ surpasses INT_MAX, we record the extra bytes here
331  // so that we can BackUp() on destruction.
332  int overflow_bytes_;
333
334  // LastTagWas() stuff.
335  uint32 last_tag_;         // result of last ReadTag().
336
337  // This is set true by ReadVarint32Fallback() if it is called when exactly
338  // at EOF, or by ExpectAtEnd() when it returns true.  This happens when we
339  // reach the end of a message and attempt to read another tag.
340  bool legitimate_message_end_;
341
342  // See EnableAliasing().
343  bool aliasing_enabled_;
344
345  // Limits
346  Limit current_limit_;   // if position = -1, no limit is applied
347
348  // For simplicity, if the current buffer crosses a limit (either a normal
349  // limit created by PushLimit() or the total bytes limit), buffer_size_
350  // only tracks the number of bytes before that limit.  This field
351  // contains the number of bytes after it.  Note that this implies that if
352  // buffer_size_ == 0 and buffer_size_after_limit_ > 0, we know we've
353  // hit a limit.  However, if both are zero, it doesn't necessarily mean
354  // we aren't at a limit -- the buffer may have ended exactly at the limit.
355  int buffer_size_after_limit_;
356
357  // Maximum number of bytes to read, period.  This is unrelated to
358  // current_limit_.  Set using SetTotalBytesLimit().
359  int total_bytes_limit_;
360  int total_bytes_warning_threshold_;
361
362  // Current recursion depth, controlled by IncrementRecursionDepth() and
363  // DecrementRecursionDepth().
364  int recursion_depth_;
365  // Recursion depth limit, set by SetRecursionLimit().
366  int recursion_limit_;
367
368  // Advance the buffer by a given number of bytes.
369  void Advance(int amount);
370
371  // Back up input_ to the current buffer position.
372  void BackUpInputToCurrentPosition();
373
374  // Recomputes the value of buffer_size_after_limit_.  Must be called after
375  // current_limit_ or total_bytes_limit_ changes.
376  void RecomputeBufferLimits();
377
378  // Writes an error message saying that we hit total_bytes_limit_.
379  void PrintTotalBytesLimitError();
380
381  // Called when the buffer runs out to request more data.  Implies an
382  // Advance(buffer_size_).
383  bool Refresh();
384
385  bool ReadVarint32Fallback(uint32* value);
386};
387
388// Class which encodes and writes binary data which is composed of varint-
389// encoded integers and fixed-width pieces.  Wraps a ZeroCopyOutputStream.
390// Most users will not need to deal with CodedOutputStream.
391//
392// Most methods of CodedOutputStream which return a bool return false if an
393// underlying I/O error occurs.  Once such a failure occurs, the
394// CodedOutputStream is broken and is no longer useful. The Write* methods do
395// not return the stream status, but will invalidate the stream if an error
396// occurs. The client can probe HadError() to determine the status.
397//
398// Note that every method of CodedOutputStream which writes some data has
399// a corresponding static "ToArray" version. These versions write directly
400// to the provided buffer, returning a pointer past the last written byte.
401// They require that the buffer has sufficient capacity for the encoded data.
402// This allows an optimization where we check if an output stream has enough
403// space for an entire message before we start writing and, if there is, we
404// call only the ToArray methods to avoid doing bound checks for each
405// individual value.
406// i.e., in the example above:
407//
408//   CodedOutputStream coded_output = new CodedOutputStream(raw_output);
409//   int magic_number = 1234;
410//   char text[] = "Hello world!";
411//
412//   int coded_size = sizeof(magic_number) +
413//                    CodedOutputStream::Varint32Size(strlen(text)) +
414//                    strlen(text);
415//
416//   uint8* buffer =
417//       coded_output->GetDirectBufferForNBytesAndAdvance(coded_size);
418//   if (buffer != NULL) {
419//     // The output stream has enough space in the buffer: write directly to
420//     // the array.
421//     buffer = CodedOutputStream::WriteLittleEndian32ToArray(magic_number,
422//                                                            buffer);
423//     buffer = CodedOutputStream::WriteVarint32ToArray(strlen(text), buffer);
424//     buffer = CodedOutputStream::WriteRawToArray(text, strlen(text), buffer);
425//   } else {
426//     // Make bound-checked writes, which will ask the underlying stream for
427//     // more space as needed.
428//     coded_output->WriteLittleEndian32(magic_number);
429//     coded_output->WriteVarint32(strlen(text));
430//     coded_output->WriteRaw(text, strlen(text));
431//   }
432//
433//   delete coded_output;
434class LIBPROTOBUF_EXPORT CodedOutputStream {
435 public:
436  // Create an CodedOutputStream that writes to the given ZeroCopyOutputStream.
437  explicit CodedOutputStream(ZeroCopyOutputStream* output);
438
439  // Destroy the CodedOutputStream and position the underlying
440  // ZeroCopyOutputStream immediately after the last byte written.
441  ~CodedOutputStream();
442
443  // Skips a number of bytes, leaving the bytes unmodified in the underlying
444  // buffer.  Returns false if an underlying write error occurs.  This is
445  // mainly useful with GetDirectBufferPointer().
446  bool Skip(int count);
447
448  // Sets *data to point directly at the unwritten part of the
449  // CodedOutputStream's underlying buffer, and *size to the size of that
450  // buffer, but does not advance the stream's current position.  This will
451  // always either produce a non-empty buffer or return false.  If the caller
452  // writes any data to this buffer, it should then call Skip() to skip over
453  // the consumed bytes.  This may be useful for implementing external fast
454  // serialization routines for types of data not covered by the
455  // CodedOutputStream interface.
456  bool GetDirectBufferPointer(void** data, int* size);
457
458  // If there are at least "size" bytes available in the current buffer,
459  // returns a pointer directly into the buffer and advances over these bytes.
460  // The caller may then write directly into this buffer (e.g. using the
461  // *ToArray static methods) rather than go through CodedOutputStream.  If
462  // there are not enough bytes available, returns NULL.  The return pointer is
463  // invalidated as soon as any other non-const method of CodedOutputStream
464  // is called.
465  inline uint8* GetDirectBufferForNBytesAndAdvance(int size);
466
467  // Write raw bytes, copying them from the given buffer.
468  void WriteRaw(const void* buffer, int size);
469  // Like WriteRaw()  but writing directly to the target array.
470  // This is _not_ inlined, as the compiler often optimizes memcpy into inline
471  // copy loops. Since this gets called by every field with string or bytes
472  // type, inlining may lead to a significant amount of code bloat, with only a
473  // minor performance gain.
474  static uint8* WriteRawToArray(const void* buffer, int size, uint8* target);
475
476  // Equivalent to WriteRaw(str.data(), str.size()).
477  void WriteString(const string& str);
478  // Like WriteString()  but writing directly to the target array.
479  static uint8* WriteStringToArray(const string& str, uint8* target);
480
481
482  // Write a 32-bit little-endian integer.
483  void WriteLittleEndian32(uint32 value);
484  // Like WriteLittleEndian32()  but writing directly to the target array.
485  static uint8* WriteLittleEndian32ToArray(uint32 value, uint8* target);
486  // Write a 64-bit little-endian integer.
487  void WriteLittleEndian64(uint64 value);
488  // Like WriteLittleEndian64()  but writing directly to the target array.
489  static uint8* WriteLittleEndian64ToArray(uint64 value, uint8* target);
490
491  // Write an unsigned integer with Varint encoding.  Writing a 32-bit value
492  // is equivalent to casting it to uint64 and writing it as a 64-bit value,
493  // but may be more efficient.
494  void WriteVarint32(uint32 value);
495  // Like WriteVarint32()  but writing directly to the target array.
496  static uint8* WriteVarint32ToArray(uint32 value, uint8* target);
497  // Write an unsigned integer with Varint encoding.
498  void WriteVarint64(uint64 value);
499  // Like WriteVarint64()  but writing directly to the target array.
500  static uint8* WriteVarint64ToArray(uint64 value, uint8* target);
501
502  // Equivalent to WriteVarint32() except when the value is negative,
503  // in which case it must be sign-extended to a full 10 bytes.
504  void WriteVarint32SignExtended(int32 value);
505  // Like WriteVarint32SignExtended()  but writing directly to the target array.
506  static uint8* WriteVarint32SignExtendedToArray(int32 value, uint8* target);
507
508  // This is identical to WriteVarint32(), but optimized for writing tags.
509  // In particular, if the input is a compile-time constant, this method
510  // compiles down to a couple instructions.
511  // Always inline because otherwise the aformentioned optimization can't work,
512  // but GCC by default doesn't want to inline this.
513  void WriteTag(uint32 value);
514  // Like WriteTag()  but writing directly to the target array.
515  static uint8* WriteTagToArray(
516      uint32 value, uint8* target) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
517
518  // Returns the number of bytes needed to encode the given value as a varint.
519  static int VarintSize32(uint32 value);
520  // Returns the number of bytes needed to encode the given value as a varint.
521  static int VarintSize64(uint64 value);
522
523  // If negative, 10 bytes.  Otheriwse, same as VarintSize32().
524  static int VarintSize32SignExtended(int32 value);
525
526  // Returns the total number of bytes written since this object was created.
527  inline int ByteCount() const;
528
529  // Returns true if there was an underlying I/O error since this object was
530  // created.
531  bool HadError() const { return had_error_; }
532
533 private:
534  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream);
535
536  ZeroCopyOutputStream* output_;
537  uint8* buffer_;
538  int buffer_size_;
539  int total_bytes_;  // Sum of sizes of all buffers seen so far.
540  bool had_error_;   // Whether an error occurred during output.
541
542  // Advance the buffer by a given number of bytes.
543  void Advance(int amount);
544
545  // Called when the buffer runs out to request more data.  Implies an
546  // Advance(buffer_size_).
547  bool Refresh();
548
549  static uint8* WriteVarint32FallbackToArray(uint32 value, uint8* target);
550
551  // Always-inlined versions of WriteVarint* functions so that code can be
552  // reused, while still controlling size. For instance, WriteVarint32ToArray()
553  // should not directly call this: since it is inlined itself, doing so
554  // would greatly increase the size of generated code. Instead, it should call
555  // WriteVarint32FallbackToArray.  Meanwhile, WriteVarint32() is already
556  // out-of-line, so it should just invoke this directly to avoid any extra
557  // function call overhead.
558  static uint8* WriteVarint32FallbackToArrayInline(
559      uint32 value, uint8* target) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
560  static uint8* WriteVarint64ToArrayInline(
561      uint64 value, uint8* target) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
562
563  static int VarintSize32Fallback(uint32 value);
564};
565
566// inline methods ====================================================
567// The vast majority of varints are only one byte.  These inline
568// methods optimize for that case.
569
570inline bool CodedInputStream::ReadVarint32(uint32* value) {
571  if (buffer_size_ != 0 && *buffer_ < 0x80) {
572    *value = *buffer_;
573    Advance(1);
574    return true;
575  } else {
576    return ReadVarint32Fallback(value);
577  }
578}
579
580inline uint32 CodedInputStream::ReadTag() {
581  if (buffer_size_ != 0 && buffer_[0] < 0x80) {
582    last_tag_ = buffer_[0];
583    Advance(1);
584    return last_tag_;
585  } else if (buffer_size_ >= 2 && buffer_[1] < 0x80) {
586    last_tag_ = (buffer_[0] & 0x7f) + (buffer_[1] << 7);
587    Advance(2);
588    return last_tag_;
589  } else if (ReadVarint32Fallback(&last_tag_)) {
590    return last_tag_;
591  } else {
592    last_tag_ = 0;
593    return 0;
594  }
595}
596
597inline bool CodedInputStream::LastTagWas(uint32 expected) {
598  return last_tag_ == expected;
599}
600
601inline bool CodedInputStream::ConsumedEntireMessage() {
602  return legitimate_message_end_;
603}
604
605inline bool CodedInputStream::ExpectTag(uint32 expected) {
606  if (expected < (1 << 7)) {
607    if (buffer_size_ != 0 && buffer_[0] == expected) {
608      Advance(1);
609      return true;
610    } else {
611      return false;
612    }
613  } else if (expected < (1 << 14)) {
614    if (buffer_size_ >= 2 &&
615        buffer_[0] == static_cast<uint8>(expected | 0x80) &&
616        buffer_[1] == static_cast<uint8>(expected >> 7)) {
617      Advance(2);
618      return true;
619    } else {
620      return false;
621    }
622  } else {
623    // Don't bother optimizing for larger values.
624    return false;
625  }
626}
627
628inline bool CodedInputStream::ExpectAtEnd() {
629  // If we are at a limit we know no more bytes can be read.  Otherwise, it's
630  // hard to say without calling Refresh(), and we'd rather not do that.
631
632  if (buffer_size_ == 0 && buffer_size_after_limit_ != 0) {
633    last_tag_ = 0;                   // Pretend we called ReadTag()...
634    legitimate_message_end_ = true;  // ... and it hit EOF.
635    return true;
636  } else {
637    return false;
638  }
639}
640
641inline uint8* CodedOutputStream::GetDirectBufferForNBytesAndAdvance(int size) {
642  if (buffer_size_ < size) {
643    return NULL;
644  } else {
645    uint8* result = buffer_;
646    Advance(size);
647    return result;
648  }
649}
650
651inline uint8* CodedOutputStream::WriteVarint32ToArray(uint32 value,
652                                                        uint8* target) {
653  if (value < 0x80) {
654    *target = value;
655    return target + 1;
656  } else {
657    return WriteVarint32FallbackToArray(value, target);
658  }
659}
660
661inline void CodedOutputStream::WriteVarint32SignExtended(int32 value) {
662  if (value < 0) {
663    WriteVarint64(static_cast<uint64>(value));
664  } else {
665    WriteVarint32(static_cast<uint32>(value));
666  }
667}
668
669inline uint8* CodedOutputStream::WriteVarint32SignExtendedToArray(
670    int32 value, uint8* target) {
671  if (value < 0) {
672    return WriteVarint64ToArray(static_cast<uint64>(value), target);
673  } else {
674    return WriteVarint32ToArray(static_cast<uint32>(value), target);
675  }
676}
677
678inline uint8* CodedOutputStream::WriteLittleEndian32ToArray(uint32 value,
679                                                            uint8* target) {
680#if !defined(PROTOBUF_TEST_NOT_LITTLE_ENDIAN) && \
681    defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN
682  memcpy(target, &value, sizeof(value));
683#else
684  target[0] = static_cast<uint8>(value      );
685  target[1] = static_cast<uint8>(value >>  8);
686  target[2] = static_cast<uint8>(value >> 16);
687  target[3] = static_cast<uint8>(value >> 24);
688#endif
689  return target + sizeof(value);
690}
691
692inline uint8* CodedOutputStream::WriteLittleEndian64ToArray(uint64 value,
693                                                            uint8* target) {
694#if !defined(PROTOBUF_TEST_NOT_LITTLE_ENDIAN) && \
695    defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN
696  memcpy(target, &value, sizeof(value));
697#else
698  uint32 part0 = static_cast<uint32>(value);
699  uint32 part1 = static_cast<uint32>(value >> 32);
700
701  target[0] = static_cast<uint8>(part0      );
702  target[1] = static_cast<uint8>(part0 >>  8);
703  target[2] = static_cast<uint8>(part0 >> 16);
704  target[3] = static_cast<uint8>(part0 >> 24);
705  target[4] = static_cast<uint8>(part1      );
706  target[5] = static_cast<uint8>(part1 >>  8);
707  target[6] = static_cast<uint8>(part1 >> 16);
708  target[7] = static_cast<uint8>(part1 >> 24);
709#endif
710  return target + sizeof(value);
711}
712
713inline void CodedOutputStream::WriteTag(uint32 value) {
714  WriteVarint32(value);
715}
716
717inline uint8* CodedOutputStream::WriteTagToArray(
718    uint32 value, uint8* target) {
719  if (value < (1 << 7)) {
720    target[0] = value;
721    return target + 1;
722  } else if (value < (1 << 14)) {
723    target[0] = static_cast<uint8>(value | 0x80);
724    target[1] = static_cast<uint8>(value >> 7);
725    return target + 2;
726  } else {
727    return WriteVarint32FallbackToArray(value, target);
728  }
729}
730
731inline int CodedOutputStream::VarintSize32(uint32 value) {
732  if (value < (1 << 7)) {
733    return 1;
734  } else  {
735    return VarintSize32Fallback(value);
736  }
737}
738
739inline int CodedOutputStream::VarintSize32SignExtended(int32 value) {
740  if (value < 0) {
741    return 10;     // TODO(kenton):  Make this a symbolic constant.
742  } else {
743    return VarintSize32(static_cast<uint32>(value));
744  }
745}
746
747inline void CodedOutputStream::WriteString(const string& str) {
748  WriteRaw(str.data(), str.size());
749}
750
751inline uint8* CodedOutputStream::WriteStringToArray(
752    const string& str, uint8* target) {
753  return WriteRawToArray(str.data(), str.size(), target);
754}
755
756inline int CodedOutputStream::ByteCount() const {
757  return total_bytes_ - buffer_size_;
758}
759
760inline void CodedInputStream::Advance(int amount) {
761  buffer_ += amount;
762  buffer_size_ -= amount;
763}
764
765inline void CodedOutputStream::Advance(int amount) {
766  buffer_ += amount;
767  buffer_size_ -= amount;
768}
769
770inline void CodedInputStream::SetRecursionLimit(int limit) {
771  recursion_limit_ = limit;
772}
773
774inline bool CodedInputStream::IncrementRecursionDepth() {
775  ++recursion_depth_;
776  return recursion_depth_ <= recursion_limit_;
777}
778
779inline void CodedInputStream::DecrementRecursionDepth() {
780  if (recursion_depth_ > 0) --recursion_depth_;
781}
782
783}  // namespace io
784}  // namespace protobuf
785
786}  // namespace google
787#endif  // GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
788