LCOV - code coverage report
Current view: top level - third_party/protobuf/src/google/protobuf/io - coded_stream.h (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 142 158 89.9 %
Date: 2015-10-10 Functions: 29 34 85.3 %

          Line data    Source code
       1             : // Protocol Buffers - Google's data interchange format
       2             : // Copyright 2008 Google Inc.  All rights reserved.
       3             : // https://developers.google.com/protocol-buffers/
       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_CREAT | 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             : #include <utility>
     114             : #ifdef _MSC_VER
     115             :   // Assuming windows is always little-endian.
     116             :   #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
     117             :     #define PROTOBUF_LITTLE_ENDIAN 1
     118             :   #endif
     119             :   #if _MSC_VER >= 1300
     120             :     // If MSVC has "/RTCc" set, it will complain about truncating casts at
     121             :     // runtime.  This file contains some intentional truncating casts.
     122             :     #pragma runtime_checks("c", off)
     123             :   #endif
     124             : #else
     125             :   #include <sys/param.h>   // __BYTE_ORDER
     126             :   #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
     127             :          (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \
     128             :       !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
     129             :     #define PROTOBUF_LITTLE_ENDIAN 1
     130             :   #endif
     131             : #endif
     132             : #include <google/protobuf/stubs/common.h>
     133             : 
     134             : namespace google {
     135             : 
     136             : namespace protobuf {
     137             : 
     138             : class DescriptorPool;
     139             : class MessageFactory;
     140             : 
     141             : namespace io {
     142             : 
     143             : // Defined in this file.
     144             : class CodedInputStream;
     145             : class CodedOutputStream;
     146             : 
     147             : // Defined in other files.
     148             : class ZeroCopyInputStream;           // zero_copy_stream.h
     149             : class ZeroCopyOutputStream;          // zero_copy_stream.h
     150             : 
     151             : // Class which reads and decodes binary data which is composed of varint-
     152             : // encoded integers and fixed-width pieces.  Wraps a ZeroCopyInputStream.
     153             : // Most users will not need to deal with CodedInputStream.
     154             : //
     155             : // Most methods of CodedInputStream that return a bool return false if an
     156             : // underlying I/O error occurs or if the data is malformed.  Once such a
     157             : // failure occurs, the CodedInputStream is broken and is no longer useful.
     158             : class LIBPROTOBUF_EXPORT CodedInputStream {
     159             :  public:
     160             :   // Create a CodedInputStream that reads from the given ZeroCopyInputStream.
     161             :   explicit CodedInputStream(ZeroCopyInputStream* input);
     162             : 
     163             :   // Create a CodedInputStream that reads from the given flat array.  This is
     164             :   // faster than using an ArrayInputStream.  PushLimit(size) is implied by
     165             :   // this constructor.
     166             :   explicit CodedInputStream(const uint8* buffer, int size);
     167             : 
     168             :   // Destroy the CodedInputStream and position the underlying
     169             :   // ZeroCopyInputStream at the first unread byte.  If an error occurred while
     170             :   // reading (causing a method to return false), then the exact position of
     171             :   // the input stream may be anywhere between the last value that was read
     172             :   // successfully and the stream's byte limit.
     173             :   ~CodedInputStream();
     174             : 
     175             :   // Return true if this CodedInputStream reads from a flat array instead of
     176             :   // a ZeroCopyInputStream.
     177             :   inline bool IsFlat() const;
     178             : 
     179             :   // Skips a number of bytes.  Returns false if an underlying read error
     180             :   // occurs.
     181             :   bool Skip(int count);
     182             : 
     183             :   // Sets *data to point directly at the unread part of the CodedInputStream's
     184             :   // underlying buffer, and *size to the size of that buffer, but does not
     185             :   // advance the stream's current position.  This will always either produce
     186             :   // a non-empty buffer or return false.  If the caller consumes any of
     187             :   // this data, it should then call Skip() to skip over the consumed bytes.
     188             :   // This may be useful for implementing external fast parsing routines for
     189             :   // types of data not covered by the CodedInputStream interface.
     190             :   bool GetDirectBufferPointer(const void** data, int* size);
     191             : 
     192             :   // Like GetDirectBufferPointer, but this method is inlined, and does not
     193             :   // attempt to Refresh() if the buffer is currently empty.
     194             :   GOOGLE_ATTRIBUTE_ALWAYS_INLINE void GetDirectBufferPointerInline(const void** data,
     195             :                                                             int* size);
     196             : 
     197             :   // Read raw bytes, copying them into the given buffer.
     198             :   bool ReadRaw(void* buffer, int size);
     199             : 
     200             :   // Like the above, with inlined optimizations. This should only be used
     201             :   // by the protobuf implementation.
     202             :   GOOGLE_ATTRIBUTE_ALWAYS_INLINE bool InternalReadRawInline(void* buffer, int size);
     203             : 
     204             :   // Like ReadRaw, but reads into a string.
     205             :   //
     206             :   // Implementation Note:  ReadString() grows the string gradually as it
     207             :   // reads in the data, rather than allocating the entire requested size
     208             :   // upfront.  This prevents denial-of-service attacks in which a client
     209             :   // could claim that a string is going to be MAX_INT bytes long in order to
     210             :   // crash the server because it can't allocate this much space at once.
     211             :   bool ReadString(string* buffer, int size);
     212             :   // Like the above, with inlined optimizations. This should only be used
     213             :   // by the protobuf implementation.
     214             :   GOOGLE_ATTRIBUTE_ALWAYS_INLINE bool InternalReadStringInline(string* buffer,
     215             :                                                         int size);
     216             : 
     217             : 
     218             :   // Read a 32-bit little-endian integer.
     219             :   bool ReadLittleEndian32(uint32* value);
     220             :   // Read a 64-bit little-endian integer.
     221             :   bool ReadLittleEndian64(uint64* value);
     222             : 
     223             :   // These methods read from an externally provided buffer. The caller is
     224             :   // responsible for ensuring that the buffer has sufficient space.
     225             :   // Read a 32-bit little-endian integer.
     226             :   static const uint8* ReadLittleEndian32FromArray(const uint8* buffer,
     227             :                                                    uint32* value);
     228             :   // Read a 64-bit little-endian integer.
     229             :   static const uint8* ReadLittleEndian64FromArray(const uint8* buffer,
     230             :                                                    uint64* value);
     231             : 
     232             :   // Read an unsigned integer with Varint encoding, truncating to 32 bits.
     233             :   // Reading a 32-bit value is equivalent to reading a 64-bit one and casting
     234             :   // it to uint32, but may be more efficient.
     235             :   bool ReadVarint32(uint32* value);
     236             :   // Read an unsigned integer with Varint encoding.
     237             :   bool ReadVarint64(uint64* value);
     238             : 
     239             :   // Read a tag.  This calls ReadVarint32() and returns the result, or returns
     240             :   // zero (which is not a valid tag) if ReadVarint32() fails.  Also, it updates
     241             :   // the last tag value, which can be checked with LastTagWas().
     242             :   // Always inline because this is only called in one place per parse loop
     243             :   // but it is called for every iteration of said loop, so it should be fast.
     244             :   // GCC doesn't want to inline this by default.
     245             :   GOOGLE_ATTRIBUTE_ALWAYS_INLINE uint32 ReadTag();
     246             : 
     247             :   // This usually a faster alternative to ReadTag() when cutoff is a manifest
     248             :   // constant.  It does particularly well for cutoff >= 127.  The first part
     249             :   // of the return value is the tag that was read, though it can also be 0 in
     250             :   // the cases where ReadTag() would return 0.  If the second part is true
     251             :   // then the tag is known to be in [0, cutoff].  If not, the tag either is
     252             :   // above cutoff or is 0.  (There's intentional wiggle room when tag is 0,
     253             :   // because that can arise in several ways, and for best performance we want
     254             :   // to avoid an extra "is tag == 0?" check here.)
     255             :   GOOGLE_ATTRIBUTE_ALWAYS_INLINE std::pair<uint32, bool> ReadTagWithCutoff(
     256             :       uint32 cutoff);
     257             : 
     258             :   // Usually returns true if calling ReadVarint32() now would produce the given
     259             :   // value.  Will always return false if ReadVarint32() would not return the
     260             :   // given value.  If ExpectTag() returns true, it also advances past
     261             :   // the varint.  For best performance, use a compile-time constant as the
     262             :   // parameter.
     263             :   // Always inline because this collapses to a small number of instructions
     264             :   // when given a constant parameter, but GCC doesn't want to inline by default.
     265             :   GOOGLE_ATTRIBUTE_ALWAYS_INLINE bool ExpectTag(uint32 expected);
     266             : 
     267             :   // Like above, except this reads from the specified buffer. The caller is
     268             :   // responsible for ensuring that the buffer is large enough to read a varint
     269             :   // of the expected size. For best performance, use a compile-time constant as
     270             :   // the expected tag parameter.
     271             :   //
     272             :   // Returns a pointer beyond the expected tag if it was found, or NULL if it
     273             :   // was not.
     274             :   GOOGLE_ATTRIBUTE_ALWAYS_INLINE static const uint8* ExpectTagFromArray(
     275             :       const uint8* buffer,
     276             :       uint32 expected);
     277             : 
     278             :   // Usually returns true if no more bytes can be read.  Always returns false
     279             :   // if more bytes can be read.  If ExpectAtEnd() returns true, a subsequent
     280             :   // call to LastTagWas() will act as if ReadTag() had been called and returned
     281             :   // zero, and ConsumedEntireMessage() will return true.
     282             :   bool ExpectAtEnd();
     283             : 
     284             :   // If the last call to ReadTag() or ReadTagWithCutoff() returned the
     285             :   // given value, returns true.  Otherwise, returns false;
     286             :   //
     287             :   // This is needed because parsers for some types of embedded messages
     288             :   // (with field type TYPE_GROUP) don't actually know that they've reached the
     289             :   // end of a message until they see an ENDGROUP tag, which was actually part
     290             :   // of the enclosing message.  The enclosing message would like to check that
     291             :   // tag to make sure it had the right number, so it calls LastTagWas() on
     292             :   // return from the embedded parser to check.
     293             :   bool LastTagWas(uint32 expected);
     294             : 
     295             :   // When parsing message (but NOT a group), this method must be called
     296             :   // immediately after MergeFromCodedStream() returns (if it returns true)
     297             :   // to further verify that the message ended in a legitimate way.  For
     298             :   // example, this verifies that parsing did not end on an end-group tag.
     299             :   // It also checks for some cases where, due to optimizations,
     300             :   // MergeFromCodedStream() can incorrectly return true.
     301             :   bool ConsumedEntireMessage();
     302             : 
     303             :   // Limits ----------------------------------------------------------
     304             :   // Limits are used when parsing length-delimited embedded messages.
     305             :   // After the message's length is read, PushLimit() is used to prevent
     306             :   // the CodedInputStream from reading beyond that length.  Once the
     307             :   // embedded message has been parsed, PopLimit() is called to undo the
     308             :   // limit.
     309             : 
     310             :   // Opaque type used with PushLimit() and PopLimit().  Do not modify
     311             :   // values of this type yourself.  The only reason that this isn't a
     312             :   // struct with private internals is for efficiency.
     313             :   typedef int Limit;
     314             : 
     315             :   // Places a limit on the number of bytes that the stream may read,
     316             :   // starting from the current position.  Once the stream hits this limit,
     317             :   // it will act like the end of the input has been reached until PopLimit()
     318             :   // is called.
     319             :   //
     320             :   // As the names imply, the stream conceptually has a stack of limits.  The
     321             :   // shortest limit on the stack is always enforced, even if it is not the
     322             :   // top limit.
     323             :   //
     324             :   // The value returned by PushLimit() is opaque to the caller, and must
     325             :   // be passed unchanged to the corresponding call to PopLimit().
     326             :   Limit PushLimit(int byte_limit);
     327             : 
     328             :   // Pops the last limit pushed by PushLimit().  The input must be the value
     329             :   // returned by that call to PushLimit().
     330             :   void PopLimit(Limit limit);
     331             : 
     332             :   // Returns the number of bytes left until the nearest limit on the
     333             :   // stack is hit, or -1 if no limits are in place.
     334             :   int BytesUntilLimit() const;
     335             : 
     336             :   // Returns current position relative to the beginning of the input stream.
     337             :   int CurrentPosition() const;
     338             : 
     339             :   // Total Bytes Limit -----------------------------------------------
     340             :   // To prevent malicious users from sending excessively large messages
     341             :   // and causing integer overflows or memory exhaustion, CodedInputStream
     342             :   // imposes a hard limit on the total number of bytes it will read.
     343             : 
     344             :   // Sets the maximum number of bytes that this CodedInputStream will read
     345             :   // before refusing to continue.  To prevent integer overflows in the
     346             :   // protocol buffers implementation, as well as to prevent servers from
     347             :   // allocating enormous amounts of memory to hold parsed messages, the
     348             :   // maximum message length should be limited to the shortest length that
     349             :   // will not harm usability.  The theoretical shortest message that could
     350             :   // cause integer overflows is 512MB.  The default limit is 64MB.  Apps
     351             :   // should set shorter limits if possible.  If warning_threshold is not -1,
     352             :   // a warning will be printed to stderr after warning_threshold bytes are
     353             :   // read.  For backwards compatibility all negative values get squashed to -1,
     354             :   // as other negative values might have special internal meanings.
     355             :   // An error will always be printed to stderr if the limit is reached.
     356             :   //
     357             :   // This is unrelated to PushLimit()/PopLimit().
     358             :   //
     359             :   // Hint:  If you are reading this because your program is printing a
     360             :   //   warning about dangerously large protocol messages, you may be
     361             :   //   confused about what to do next.  The best option is to change your
     362             :   //   design such that excessively large messages are not necessary.
     363             :   //   For example, try to design file formats to consist of many small
     364             :   //   messages rather than a single large one.  If this is infeasible,
     365             :   //   you will need to increase the limit.  Chances are, though, that
     366             :   //   your code never constructs a CodedInputStream on which the limit
     367             :   //   can be set.  You probably parse messages by calling things like
     368             :   //   Message::ParseFromString().  In this case, you will need to change
     369             :   //   your code to instead construct some sort of ZeroCopyInputStream
     370             :   //   (e.g. an ArrayInputStream), construct a CodedInputStream around
     371             :   //   that, then call Message::ParseFromCodedStream() instead.  Then
     372             :   //   you can adjust the limit.  Yes, it's more work, but you're doing
     373             :   //   something unusual.
     374             :   void SetTotalBytesLimit(int total_bytes_limit, int warning_threshold);
     375             : 
     376             :   // The Total Bytes Limit minus the Current Position, or -1 if there
     377             :   // is no Total Bytes Limit.
     378             :   int BytesUntilTotalBytesLimit() const;
     379             : 
     380             :   // Recursion Limit -------------------------------------------------
     381             :   // To prevent corrupt or malicious messages from causing stack overflows,
     382             :   // we must keep track of the depth of recursion when parsing embedded
     383             :   // messages and groups.  CodedInputStream keeps track of this because it
     384             :   // is the only object that is passed down the stack during parsing.
     385             : 
     386             :   // Sets the maximum recursion depth.  The default is 100.
     387             :   void SetRecursionLimit(int limit);
     388             : 
     389             : 
     390             :   // Increments the current recursion depth.  Returns true if the depth is
     391             :   // under the limit, false if it has gone over.
     392             :   bool IncrementRecursionDepth();
     393             : 
     394             :   // Decrements the recursion depth if possible.
     395             :   void DecrementRecursionDepth();
     396             : 
     397             :   // Decrements the recursion depth blindly.  This is faster than
     398             :   // DecrementRecursionDepth().  It should be used only if all previous
     399             :   // increments to recursion depth were successful.
     400             :   void UnsafeDecrementRecursionDepth();
     401             : 
     402             :   // Shorthand for make_pair(PushLimit(byte_limit), --recursion_budget_).
     403             :   // Using this can reduce code size and complexity in some cases.  The caller
     404             :   // is expected to check that the second part of the result is non-negative (to
     405             :   // bail out if the depth of recursion is too high) and, if all is well, to
     406             :   // later pass the first part of the result to PopLimit() or similar.
     407             :   std::pair<CodedInputStream::Limit, int> IncrementRecursionDepthAndPushLimit(
     408             :       int byte_limit);
     409             : 
     410             :   // Shorthand for PushLimit(ReadVarint32(&length) ? length : 0).
     411             :   Limit ReadLengthAndPushLimit();
     412             : 
     413             :   // Helper that is equivalent to: {
     414             :   //  bool result = ConsumedEntireMessage();
     415             :   //  PopLimit(limit);
     416             :   //  UnsafeDecrementRecursionDepth();
     417             :   //  return result; }
     418             :   // Using this can reduce code size and complexity in some cases.
     419             :   // Do not use unless the current recursion depth is greater than zero.
     420             :   bool DecrementRecursionDepthAndPopLimit(Limit limit);
     421             : 
     422             :   // Helper that is equivalent to: {
     423             :   //  bool result = ConsumedEntireMessage();
     424             :   //  PopLimit(limit);
     425             :   //  return result; }
     426             :   // Using this can reduce code size and complexity in some cases.
     427             :   bool CheckEntireMessageConsumedAndPopLimit(Limit limit);
     428             : 
     429             :   // Extension Registry ----------------------------------------------
     430             :   // ADVANCED USAGE:  99.9% of people can ignore this section.
     431             :   //
     432             :   // By default, when parsing extensions, the parser looks for extension
     433             :   // definitions in the pool which owns the outer message's Descriptor.
     434             :   // However, you may call SetExtensionRegistry() to provide an alternative
     435             :   // pool instead.  This makes it possible, for example, to parse a message
     436             :   // using a generated class, but represent some extensions using
     437             :   // DynamicMessage.
     438             : 
     439             :   // Set the pool used to look up extensions.  Most users do not need to call
     440             :   // this as the correct pool will be chosen automatically.
     441             :   //
     442             :   // WARNING:  It is very easy to misuse this.  Carefully read the requirements
     443             :   //   below.  Do not use this unless you are sure you need it.  Almost no one
     444             :   //   does.
     445             :   //
     446             :   // Let's say you are parsing a message into message object m, and you want
     447             :   // to take advantage of SetExtensionRegistry().  You must follow these
     448             :   // requirements:
     449             :   //
     450             :   // The given DescriptorPool must contain m->GetDescriptor().  It is not
     451             :   // sufficient for it to simply contain a descriptor that has the same name
     452             :   // and content -- it must be the *exact object*.  In other words:
     453             :   //   assert(pool->FindMessageTypeByName(m->GetDescriptor()->full_name()) ==
     454             :   //          m->GetDescriptor());
     455             :   // There are two ways to satisfy this requirement:
     456             :   // 1) Use m->GetDescriptor()->pool() as the pool.  This is generally useless
     457             :   //    because this is the pool that would be used anyway if you didn't call
     458             :   //    SetExtensionRegistry() at all.
     459             :   // 2) Use a DescriptorPool which has m->GetDescriptor()->pool() as an
     460             :   //    "underlay".  Read the documentation for DescriptorPool for more
     461             :   //    information about underlays.
     462             :   //
     463             :   // You must also provide a MessageFactory.  This factory will be used to
     464             :   // construct Message objects representing extensions.  The factory's
     465             :   // GetPrototype() MUST return non-NULL for any Descriptor which can be found
     466             :   // through the provided pool.
     467             :   //
     468             :   // If the provided factory might return instances of protocol-compiler-
     469             :   // generated (i.e. compiled-in) types, or if the outer message object m is
     470             :   // a generated type, then the given factory MUST have this property:  If
     471             :   // GetPrototype() is given a Descriptor which resides in
     472             :   // DescriptorPool::generated_pool(), the factory MUST return the same
     473             :   // prototype which MessageFactory::generated_factory() would return.  That
     474             :   // is, given a descriptor for a generated type, the factory must return an
     475             :   // instance of the generated class (NOT DynamicMessage).  However, when
     476             :   // given a descriptor for a type that is NOT in generated_pool, the factory
     477             :   // is free to return any implementation.
     478             :   //
     479             :   // The reason for this requirement is that generated sub-objects may be
     480             :   // accessed via the standard (non-reflection) extension accessor methods,
     481             :   // and these methods will down-cast the object to the generated class type.
     482             :   // If the object is not actually of that type, the results would be undefined.
     483             :   // On the other hand, if an extension is not compiled in, then there is no
     484             :   // way the code could end up accessing it via the standard accessors -- the
     485             :   // only way to access the extension is via reflection.  When using reflection,
     486             :   // DynamicMessage and generated messages are indistinguishable, so it's fine
     487             :   // if these objects are represented using DynamicMessage.
     488             :   //
     489             :   // Using DynamicMessageFactory on which you have called
     490             :   // SetDelegateToGeneratedFactory(true) should be sufficient to satisfy the
     491             :   // above requirement.
     492             :   //
     493             :   // If either pool or factory is NULL, both must be NULL.
     494             :   //
     495             :   // Note that this feature is ignored when parsing "lite" messages as they do
     496             :   // not have descriptors.
     497             :   void SetExtensionRegistry(const DescriptorPool* pool,
     498             :                             MessageFactory* factory);
     499             : 
     500             :   // Get the DescriptorPool set via SetExtensionRegistry(), or NULL if no pool
     501             :   // has been provided.
     502             :   const DescriptorPool* GetExtensionPool();
     503             : 
     504             :   // Get the MessageFactory set via SetExtensionRegistry(), or NULL if no
     505             :   // factory has been provided.
     506             :   MessageFactory* GetExtensionFactory();
     507             : 
     508             :  private:
     509             :   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedInputStream);
     510             : 
     511             :   const uint8* buffer_;
     512             :   const uint8* buffer_end_;     // pointer to the end of the buffer.
     513             :   ZeroCopyInputStream* input_;
     514             :   int total_bytes_read_;  // total bytes read from input_, including
     515             :                           // the current buffer
     516             : 
     517             :   // If total_bytes_read_ surpasses INT_MAX, we record the extra bytes here
     518             :   // so that we can BackUp() on destruction.
     519             :   int overflow_bytes_;
     520             : 
     521             :   // LastTagWas() stuff.
     522             :   uint32 last_tag_;         // result of last ReadTag() or ReadTagWithCutoff().
     523             : 
     524             :   // This is set true by ReadTag{Fallback/Slow}() if it is called when exactly
     525             :   // at EOF, or by ExpectAtEnd() when it returns true.  This happens when we
     526             :   // reach the end of a message and attempt to read another tag.
     527             :   bool legitimate_message_end_;
     528             : 
     529             :   // See EnableAliasing().
     530             :   bool aliasing_enabled_;
     531             : 
     532             :   // Limits
     533             :   Limit current_limit_;   // if position = -1, no limit is applied
     534             : 
     535             :   // For simplicity, if the current buffer crosses a limit (either a normal
     536             :   // limit created by PushLimit() or the total bytes limit), buffer_size_
     537             :   // only tracks the number of bytes before that limit.  This field
     538             :   // contains the number of bytes after it.  Note that this implies that if
     539             :   // buffer_size_ == 0 and buffer_size_after_limit_ > 0, we know we've
     540             :   // hit a limit.  However, if both are zero, it doesn't necessarily mean
     541             :   // we aren't at a limit -- the buffer may have ended exactly at the limit.
     542             :   int buffer_size_after_limit_;
     543             : 
     544             :   // Maximum number of bytes to read, period.  This is unrelated to
     545             :   // current_limit_.  Set using SetTotalBytesLimit().
     546             :   int total_bytes_limit_;
     547             : 
     548             :   // If positive/0: Limit for bytes read after which a warning due to size
     549             :   // should be logged.
     550             :   // If -1: Printing of warning disabled. Can be set by client.
     551             :   // If -2: Internal: Limit has been reached, print full size when destructing.
     552             :   int total_bytes_warning_threshold_;
     553             : 
     554             :   // Current recursion budget, controlled by IncrementRecursionDepth() and
     555             :   // similar.  Starts at recursion_limit_ and goes down: if this reaches
     556             :   // -1 we are over budget.
     557             :   int recursion_budget_;
     558             :   // Recursion depth limit, set by SetRecursionLimit().
     559             :   int recursion_limit_;
     560             : 
     561             :   // See SetExtensionRegistry().
     562             :   const DescriptorPool* extension_pool_;
     563             :   MessageFactory* extension_factory_;
     564             : 
     565             :   // Private member functions.
     566             : 
     567             :   // Advance the buffer by a given number of bytes.
     568             :   void Advance(int amount);
     569             : 
     570             :   // Back up input_ to the current buffer position.
     571             :   void BackUpInputToCurrentPosition();
     572             : 
     573             :   // Recomputes the value of buffer_size_after_limit_.  Must be called after
     574             :   // current_limit_ or total_bytes_limit_ changes.
     575             :   void RecomputeBufferLimits();
     576             : 
     577             :   // Writes an error message saying that we hit total_bytes_limit_.
     578             :   void PrintTotalBytesLimitError();
     579             : 
     580             :   // Called when the buffer runs out to request more data.  Implies an
     581             :   // Advance(BufferSize()).
     582             :   bool Refresh();
     583             : 
     584             :   // When parsing varints, we optimize for the common case of small values, and
     585             :   // then optimize for the case when the varint fits within the current buffer
     586             :   // piece. The Fallback method is used when we can't use the one-byte
     587             :   // optimization. The Slow method is yet another fallback when the buffer is
     588             :   // not large enough. Making the slow path out-of-line speeds up the common
     589             :   // case by 10-15%. The slow path is fairly uncommon: it only triggers when a
     590             :   // message crosses multiple buffers.  Note: ReadVarint32Fallback() and
     591             :   // ReadVarint64Fallback() are called frequently and generally not inlined, so
     592             :   // they have been optimized to avoid "out" parameters.  The former returns -1
     593             :   // if it fails and the uint32 it read otherwise.  The latter has a bool
     594             :   // indicating success or failure as part of its return type.
     595             :   int64 ReadVarint32Fallback(uint32 first_byte_or_zero);
     596             :   std::pair<uint64, bool> ReadVarint64Fallback();
     597             :   bool ReadVarint32Slow(uint32* value);
     598             :   bool ReadVarint64Slow(uint64* value);
     599             :   bool ReadLittleEndian32Fallback(uint32* value);
     600             :   bool ReadLittleEndian64Fallback(uint64* value);
     601             :   // Fallback/slow methods for reading tags. These do not update last_tag_,
     602             :   // but will set legitimate_message_end_ if we are at the end of the input
     603             :   // stream.
     604             :   uint32 ReadTagFallback(uint32 first_byte_or_zero);
     605             :   uint32 ReadTagSlow();
     606             :   bool ReadStringFallback(string* buffer, int size);
     607             : 
     608             :   // Return the size of the buffer.
     609             :   int BufferSize() const;
     610             : 
     611             :   static const int kDefaultTotalBytesLimit = 64 << 20;  // 64MB
     612             : 
     613             :   static const int kDefaultTotalBytesWarningThreshold = 32 << 20;  // 32MB
     614             : 
     615             :   static int default_recursion_limit_;  // 100 by default.
     616             : };
     617             : 
     618             : // Class which encodes and writes binary data which is composed of varint-
     619             : // encoded integers and fixed-width pieces.  Wraps a ZeroCopyOutputStream.
     620             : // Most users will not need to deal with CodedOutputStream.
     621             : //
     622             : // Most methods of CodedOutputStream which return a bool return false if an
     623             : // underlying I/O error occurs.  Once such a failure occurs, the
     624             : // CodedOutputStream is broken and is no longer useful. The Write* methods do
     625             : // not return the stream status, but will invalidate the stream if an error
     626             : // occurs. The client can probe HadError() to determine the status.
     627             : //
     628             : // Note that every method of CodedOutputStream which writes some data has
     629             : // a corresponding static "ToArray" version. These versions write directly
     630             : // to the provided buffer, returning a pointer past the last written byte.
     631             : // They require that the buffer has sufficient capacity for the encoded data.
     632             : // This allows an optimization where we check if an output stream has enough
     633             : // space for an entire message before we start writing and, if there is, we
     634             : // call only the ToArray methods to avoid doing bound checks for each
     635             : // individual value.
     636             : // i.e., in the example above:
     637             : //
     638             : //   CodedOutputStream coded_output = new CodedOutputStream(raw_output);
     639             : //   int magic_number = 1234;
     640             : //   char text[] = "Hello world!";
     641             : //
     642             : //   int coded_size = sizeof(magic_number) +
     643             : //                    CodedOutputStream::VarintSize32(strlen(text)) +
     644             : //                    strlen(text);
     645             : //
     646             : //   uint8* buffer =
     647             : //       coded_output->GetDirectBufferForNBytesAndAdvance(coded_size);
     648             : //   if (buffer != NULL) {
     649             : //     // The output stream has enough space in the buffer: write directly to
     650             : //     // the array.
     651             : //     buffer = CodedOutputStream::WriteLittleEndian32ToArray(magic_number,
     652             : //                                                            buffer);
     653             : //     buffer = CodedOutputStream::WriteVarint32ToArray(strlen(text), buffer);
     654             : //     buffer = CodedOutputStream::WriteRawToArray(text, strlen(text), buffer);
     655             : //   } else {
     656             : //     // Make bound-checked writes, which will ask the underlying stream for
     657             : //     // more space as needed.
     658             : //     coded_output->WriteLittleEndian32(magic_number);
     659             : //     coded_output->WriteVarint32(strlen(text));
     660             : //     coded_output->WriteRaw(text, strlen(text));
     661             : //   }
     662             : //
     663             : //   delete coded_output;
     664             : class LIBPROTOBUF_EXPORT CodedOutputStream {
     665             :  public:
     666             :   // Create an CodedOutputStream that writes to the given ZeroCopyOutputStream.
     667             :   explicit CodedOutputStream(ZeroCopyOutputStream* output);
     668             : 
     669             :   // Destroy the CodedOutputStream and position the underlying
     670             :   // ZeroCopyOutputStream immediately after the last byte written.
     671             :   ~CodedOutputStream();
     672             : 
     673             :   // Trims any unused space in the underlying buffer so that its size matches
     674             :   // the number of bytes written by this stream. The underlying buffer will
     675             :   // automatically be trimmed when this stream is destroyed; this call is only
     676             :   // necessary if the underlying buffer is accessed *before* the stream is
     677             :   // destroyed.
     678             :   void Trim();
     679             : 
     680             :   // Skips a number of bytes, leaving the bytes unmodified in the underlying
     681             :   // buffer.  Returns false if an underlying write error occurs.  This is
     682             :   // mainly useful with GetDirectBufferPointer().
     683             :   bool Skip(int count);
     684             : 
     685             :   // Sets *data to point directly at the unwritten part of the
     686             :   // CodedOutputStream's underlying buffer, and *size to the size of that
     687             :   // buffer, but does not advance the stream's current position.  This will
     688             :   // always either produce a non-empty buffer or return false.  If the caller
     689             :   // writes any data to this buffer, it should then call Skip() to skip over
     690             :   // the consumed bytes.  This may be useful for implementing external fast
     691             :   // serialization routines for types of data not covered by the
     692             :   // CodedOutputStream interface.
     693             :   bool GetDirectBufferPointer(void** data, int* size);
     694             : 
     695             :   // If there are at least "size" bytes available in the current buffer,
     696             :   // returns a pointer directly into the buffer and advances over these bytes.
     697             :   // The caller may then write directly into this buffer (e.g. using the
     698             :   // *ToArray static methods) rather than go through CodedOutputStream.  If
     699             :   // there are not enough bytes available, returns NULL.  The return pointer is
     700             :   // invalidated as soon as any other non-const method of CodedOutputStream
     701             :   // is called.
     702             :   inline uint8* GetDirectBufferForNBytesAndAdvance(int size);
     703             : 
     704             :   // Write raw bytes, copying them from the given buffer.
     705             :   void WriteRaw(const void* buffer, int size);
     706             :   // Like WriteRaw()  but will try to write aliased data if aliasing is
     707             :   // turned on.
     708             :   void WriteRawMaybeAliased(const void* data, int size);
     709             :   // Like WriteRaw()  but writing directly to the target array.
     710             :   // This is _not_ inlined, as the compiler often optimizes memcpy into inline
     711             :   // copy loops. Since this gets called by every field with string or bytes
     712             :   // type, inlining may lead to a significant amount of code bloat, with only a
     713             :   // minor performance gain.
     714             :   static uint8* WriteRawToArray(const void* buffer, int size, uint8* target);
     715             : 
     716             :   // Equivalent to WriteRaw(str.data(), str.size()).
     717             :   void WriteString(const string& str);
     718             :   // Like WriteString()  but writing directly to the target array.
     719             :   static uint8* WriteStringToArray(const string& str, uint8* target);
     720             :   // Write the varint-encoded size of str followed by str.
     721             :   static uint8* WriteStringWithSizeToArray(const string& str, uint8* target);
     722             : 
     723             : 
     724             :   // Instructs the CodedOutputStream to allow the underlying
     725             :   // ZeroCopyOutputStream to hold pointers to the original structure instead of
     726             :   // copying, if it supports it (i.e. output->AllowsAliasing() is true).  If the
     727             :   // underlying stream does not support aliasing, then enabling it has no
     728             :   // affect.  For now, this only affects the behavior of
     729             :   // WriteRawMaybeAliased().
     730             :   //
     731             :   // NOTE: It is caller's responsibility to ensure that the chunk of memory
     732             :   // remains live until all of the data has been consumed from the stream.
     733             :   void EnableAliasing(bool enabled);
     734             : 
     735             :   // Write a 32-bit little-endian integer.
     736             :   void WriteLittleEndian32(uint32 value);
     737             :   // Like WriteLittleEndian32()  but writing directly to the target array.
     738             :   static uint8* WriteLittleEndian32ToArray(uint32 value, uint8* target);
     739             :   // Write a 64-bit little-endian integer.
     740             :   void WriteLittleEndian64(uint64 value);
     741             :   // Like WriteLittleEndian64()  but writing directly to the target array.
     742             :   static uint8* WriteLittleEndian64ToArray(uint64 value, uint8* target);
     743             : 
     744             :   // Write an unsigned integer with Varint encoding.  Writing a 32-bit value
     745             :   // is equivalent to casting it to uint64 and writing it as a 64-bit value,
     746             :   // but may be more efficient.
     747             :   void WriteVarint32(uint32 value);
     748             :   // Like WriteVarint32()  but writing directly to the target array.
     749             :   static uint8* WriteVarint32ToArray(uint32 value, uint8* target);
     750             :   // Write an unsigned integer with Varint encoding.
     751             :   void WriteVarint64(uint64 value);
     752             :   // Like WriteVarint64()  but writing directly to the target array.
     753             :   static uint8* WriteVarint64ToArray(uint64 value, uint8* target);
     754             : 
     755             :   // Equivalent to WriteVarint32() except when the value is negative,
     756             :   // in which case it must be sign-extended to a full 10 bytes.
     757             :   void WriteVarint32SignExtended(int32 value);
     758             :   // Like WriteVarint32SignExtended()  but writing directly to the target array.
     759             :   static uint8* WriteVarint32SignExtendedToArray(int32 value, uint8* target);
     760             : 
     761             :   // This is identical to WriteVarint32(), but optimized for writing tags.
     762             :   // In particular, if the input is a compile-time constant, this method
     763             :   // compiles down to a couple instructions.
     764             :   // Always inline because otherwise the aformentioned optimization can't work,
     765             :   // but GCC by default doesn't want to inline this.
     766             :   void WriteTag(uint32 value);
     767             :   // Like WriteTag()  but writing directly to the target array.
     768             :   GOOGLE_ATTRIBUTE_ALWAYS_INLINE static uint8* WriteTagToArray(uint32 value,
     769             :                                                         uint8* target);
     770             : 
     771             :   // Returns the number of bytes needed to encode the given value as a varint.
     772             :   static int VarintSize32(uint32 value);
     773             :   // Returns the number of bytes needed to encode the given value as a varint.
     774             :   static int VarintSize64(uint64 value);
     775             : 
     776             :   // If negative, 10 bytes.  Otheriwse, same as VarintSize32().
     777             :   static int VarintSize32SignExtended(int32 value);
     778             : 
     779             :   // Compile-time equivalent of VarintSize32().
     780             :   template <uint32 Value>
     781             :   struct StaticVarintSize32 {
     782             :     static const int value =
     783             :         (Value < (1 << 7))
     784             :             ? 1
     785             :             : (Value < (1 << 14))
     786             :                 ? 2
     787             :                 : (Value < (1 << 21))
     788             :                     ? 3
     789             :                     : (Value < (1 << 28))
     790             :                         ? 4
     791             :                         : 5;
     792             :   };
     793             : 
     794             :   // Returns the total number of bytes written since this object was created.
     795             :   inline int ByteCount() const;
     796             : 
     797             :   // Returns true if there was an underlying I/O error since this object was
     798             :   // created.
     799             :   bool HadError() const { return had_error_; }
     800             : 
     801             :  private:
     802             :   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream);
     803             : 
     804             :   ZeroCopyOutputStream* output_;
     805             :   uint8* buffer_;
     806             :   int buffer_size_;
     807             :   int total_bytes_;  // Sum of sizes of all buffers seen so far.
     808             :   bool had_error_;   // Whether an error occurred during output.
     809             :   bool aliasing_enabled_;  // See EnableAliasing().
     810             : 
     811             :   // Advance the buffer by a given number of bytes.
     812             :   void Advance(int amount);
     813             : 
     814             :   // Called when the buffer runs out to request more data.  Implies an
     815             :   // Advance(buffer_size_).
     816             :   bool Refresh();
     817             : 
     818             :   // Like WriteRaw() but may avoid copying if the underlying
     819             :   // ZeroCopyOutputStream supports it.
     820             :   void WriteAliasedRaw(const void* buffer, int size);
     821             : 
     822             :   // If this write might cross the end of the buffer, we compose the bytes first
     823             :   // then use WriteRaw().
     824             :   void WriteVarint32SlowPath(uint32 value);
     825             : 
     826             :   // Always-inlined versions of WriteVarint* functions so that code can be
     827             :   // reused, while still controlling size. For instance, WriteVarint32ToArray()
     828             :   // should not directly call this: since it is inlined itself, doing so
     829             :   // would greatly increase the size of generated code. Instead, it should call
     830             :   // WriteVarint32FallbackToArray.  Meanwhile, WriteVarint32() is already
     831             :   // out-of-line, so it should just invoke this directly to avoid any extra
     832             :   // function call overhead.
     833             :   GOOGLE_ATTRIBUTE_ALWAYS_INLINE static uint8* WriteVarint64ToArrayInline(
     834             :       uint64 value, uint8* target);
     835             : 
     836             :   static int VarintSize32Fallback(uint32 value);
     837             : };
     838             : 
     839             : // inline methods ====================================================
     840             : // The vast majority of varints are only one byte.  These inline
     841             : // methods optimize for that case.
     842             : 
     843     4166152 : inline bool CodedInputStream::ReadVarint32(uint32* value) {
     844     4166152 :   uint32 v = 0;
     845     4166152 :   if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_)) {
     846     4167786 :     v = *buffer_;
     847     4167786 :     if (v < 0x80) {
     848     4125649 :       *value = v;
     849     4125649 :       Advance(1);
     850     4125674 :       return true;
     851             :     }
     852             :   }
     853       40839 :   int64 result = ReadVarint32Fallback(v);
     854       42225 :   *value = static_cast<uint32>(result);
     855       42225 :   return result >= 0;
     856             : }
     857             : 
     858        1069 : inline bool CodedInputStream::ReadVarint64(uint64* value) {
     859        1069 :   if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) {
     860         974 :     *value = *buffer_;
     861         974 :     Advance(1);
     862         974 :     return true;
     863             :   }
     864          95 :   std::pair<uint64, bool> p = ReadVarint64Fallback();
     865          95 :   *value = p.first;
     866          95 :   return p.second;
     867             : }
     868             : 
     869             : // static
     870             : inline const uint8* CodedInputStream::ReadLittleEndian32FromArray(
     871             :     const uint8* buffer,
     872             :     uint32* value) {
     873             : #if defined(PROTOBUF_LITTLE_ENDIAN)
     874             :   memcpy(value, buffer, sizeof(*value));
     875             :   return buffer + sizeof(*value);
     876             : #else
     877             :   *value = (static_cast<uint32>(buffer[0])      ) |
     878             :            (static_cast<uint32>(buffer[1]) <<  8) |
     879             :            (static_cast<uint32>(buffer[2]) << 16) |
     880             :            (static_cast<uint32>(buffer[3]) << 24);
     881             :   return buffer + sizeof(*value);
     882             : #endif
     883             : }
     884             : // static
     885             : inline const uint8* CodedInputStream::ReadLittleEndian64FromArray(
     886             :     const uint8* buffer,
     887             :     uint64* value) {
     888             : #if defined(PROTOBUF_LITTLE_ENDIAN)
     889             :   memcpy(value, buffer, sizeof(*value));
     890             :   return buffer + sizeof(*value);
     891             : #else
     892             :   uint32 part0 = (static_cast<uint32>(buffer[0])      ) |
     893             :                  (static_cast<uint32>(buffer[1]) <<  8) |
     894             :                  (static_cast<uint32>(buffer[2]) << 16) |
     895             :                  (static_cast<uint32>(buffer[3]) << 24);
     896             :   uint32 part1 = (static_cast<uint32>(buffer[4])      ) |
     897             :                  (static_cast<uint32>(buffer[5]) <<  8) |
     898             :                  (static_cast<uint32>(buffer[6]) << 16) |
     899             :                  (static_cast<uint32>(buffer[7]) << 24);
     900             :   *value = static_cast<uint64>(part0) |
     901             :           (static_cast<uint64>(part1) << 32);
     902             :   return buffer + sizeof(*value);
     903             : #endif
     904             : }
     905             : 
     906          18 : inline bool CodedInputStream::ReadLittleEndian32(uint32* value) {
     907             : #if defined(PROTOBUF_LITTLE_ENDIAN)
     908          27 :   if (GOOGLE_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
     909           9 :     memcpy(value, buffer_, sizeof(*value));
     910           9 :     Advance(sizeof(*value));
     911           9 :     return true;
     912             :   } else {
     913           0 :     return ReadLittleEndian32Fallback(value);
     914             :   }
     915             : #else
     916             :   return ReadLittleEndian32Fallback(value);
     917             : #endif
     918             : }
     919             : 
     920         155 : inline bool CodedInputStream::ReadLittleEndian64(uint64* value) {
     921             : #if defined(PROTOBUF_LITTLE_ENDIAN)
     922         166 :   if (GOOGLE_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
     923         144 :     memcpy(value, buffer_, sizeof(*value));
     924         144 :     Advance(sizeof(*value));
     925         144 :     return true;
     926             :   } else {
     927           0 :     return ReadLittleEndian64Fallback(value);
     928             :   }
     929             : #else
     930             :   return ReadLittleEndian64Fallback(value);
     931             : #endif
     932             : }
     933             : 
     934             : inline uint32 CodedInputStream::ReadTag() {
     935          94 :   uint32 v = 0;
     936          94 :   if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_)) {
     937          48 :     v = *buffer_;
     938          48 :     if (v < 0x80) {
     939          30 :       last_tag_ = v;
     940          30 :       Advance(1);
     941             :       return v;
     942             :     }
     943             :   }
     944          64 :   last_tag_ = ReadTagFallback(v);
     945             :   return last_tag_;
     946             : }
     947             : 
     948             : inline std::pair<uint32, bool> CodedInputStream::ReadTagWithCutoff(
     949             :     uint32 cutoff) {
     950             :   // In performance-sensitive code we can expect cutoff to be a compile-time
     951             :   // constant, and things like "cutoff >= kMax1ByteVarint" to be evaluated at
     952             :   // compile time.
     953     6902612 :   uint32 first_byte_or_zero = 0;
     954     6902612 :   if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_)) {
     955             :     // Hot case: buffer_ non_empty, buffer_[0] in [1, 128).
     956             :     // TODO(gpike): Is it worth rearranging this? E.g., if the number of fields
     957             :     // is large enough then is it better to check for the two-byte case first?
     958     4096423 :     first_byte_or_zero = buffer_[0];
     959     4096423 :     if (static_cast<int8>(buffer_[0]) > 0) {
     960     4095974 :       const uint32 kMax1ByteVarint = 0x7f;
     961     4095974 :       uint32 tag = last_tag_ = buffer_[0];
     962     4095974 :       Advance(1);
     963     4095918 :       return std::make_pair(tag, cutoff >= kMax1ByteVarint || tag <= cutoff);
     964             :     }
     965             :     // Other hot case: cutoff >= 0x80, buffer_ has at least two bytes available,
     966             :     // and tag is two bytes.  The latter is tested by bitwise-and-not of the
     967             :     // first byte and the second byte.
     968         449 :     if (cutoff >= 0x80 &&
     969         898 :         GOOGLE_PREDICT_TRUE(buffer_ + 1 < buffer_end_) &&
     970         449 :         GOOGLE_PREDICT_TRUE((buffer_[0] & ~buffer_[1]) >= 0x80)) {
     971         378 :       const uint32 kMax2ByteVarint = (0x7f << 7) + 0x7f;
     972         378 :       uint32 tag = last_tag_ = (1u << 7) * buffer_[1] + (buffer_[0] - 0x80);
     973         378 :       Advance(2);
     974             :       // It might make sense to test for tag == 0 now, but it is so rare that
     975             :       // that we don't bother.  A varint-encoded 0 should be one byte unless
     976             :       // the encoder lost its mind.  The second part of the return value of
     977             :       // this function is allowed to be either true or false if the tag is 0,
     978             :       // so we don't have to check for tag == 0.  We may need to check whether
     979             :       // it exceeds cutoff.
     980         378 :       bool at_or_below_cutoff = cutoff >= kMax2ByteVarint || tag <= cutoff;
     981         378 :       return std::make_pair(tag, at_or_below_cutoff);
     982             :     }
     983             :   }
     984             :   // Slow path
     985     2806260 :   last_tag_ = ReadTagFallback(first_byte_or_zero);
     986     2808369 :   return std::make_pair(last_tag_, static_cast<uint32>(last_tag_ - 1) < cutoff);
     987             : }
     988             : 
     989             : inline bool CodedInputStream::LastTagWas(uint32 expected) {
     990             :   return last_tag_ == expected;
     991             : }
     992             : 
     993     2795976 : inline bool CodedInputStream::ConsumedEntireMessage() {
     994     2795976 :   return legitimate_message_end_;
     995             : }
     996             : 
     997             : inline bool CodedInputStream::ExpectTag(uint32 expected) {
     998     1517882 :   if (expected < (1 << 7)) {
     999     1573464 :     if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && buffer_[0] == expected) {
    1000       31754 :       Advance(1);
    1001         155 :       return true;
    1002             :     } else {
    1003     1517660 :       return false;
    1004             :     }
    1005           0 :   } else if (expected < (1 << 14)) {
    1006        1676 :     if (GOOGLE_PREDICT_TRUE(BufferSize() >= 2) &&
    1007         864 :         buffer_[0] == static_cast<uint8>(expected | 0x80) &&
    1008         140 :         buffer_[1] == static_cast<uint8>(expected >> 7)) {
    1009         140 :       Advance(2);
    1010           0 :       return true;
    1011             :     } else {
    1012           0 :       return false;
    1013             :     }
    1014             :   } else {
    1015             :     // Don't bother optimizing for larger values.
    1016           0 :     return false;
    1017             :   }
    1018             : }
    1019             : 
    1020             : inline const uint8* CodedInputStream::ExpectTagFromArray(
    1021             :     const uint8* buffer, uint32 expected) {
    1022             :   if (expected < (1 << 7)) {
    1023             :     if (buffer[0] == expected) {
    1024             :       return buffer + 1;
    1025             :     }
    1026             :   } else if (expected < (1 << 14)) {
    1027             :     if (buffer[0] == static_cast<uint8>(expected | 0x80) &&
    1028             :         buffer[1] == static_cast<uint8>(expected >> 7)) {
    1029             :       return buffer + 2;
    1030             :     }
    1031             :   }
    1032             :   return NULL;
    1033             : }
    1034             : 
    1035             : inline void CodedInputStream::GetDirectBufferPointerInline(const void** data,
    1036             :                                                            int* size) {
    1037             :   *data = buffer_;
    1038             :   *size = buffer_end_ - buffer_;
    1039             : }
    1040             : 
    1041     2556223 : inline bool CodedInputStream::ExpectAtEnd() {
    1042             :   // If we are at a limit we know no more bytes can be read.  Otherwise, it's
    1043             :   // hard to say without calling Refresh(), and we'd rather not do that.
    1044             : 
    1045     5114063 :   if (buffer_ == buffer_end_ &&
    1046     5112178 :       ((buffer_size_after_limit_ != 0) ||
    1047     2555634 :        (total_bytes_read_ == current_limit_))) {
    1048     1279372 :     last_tag_ = 0;                   // Pretend we called ReadTag()...
    1049     1279372 :     legitimate_message_end_ = true;  // ... and it hit EOF.
    1050     1278084 :     return true;
    1051             :   } else {
    1052     1278139 :     return false;
    1053             :   }
    1054             : }
    1055             : 
    1056     2908372 : inline int CodedInputStream::CurrentPosition() const {
    1057     5816744 :   return total_bytes_read_ - (BufferSize() + buffer_size_after_limit_);
    1058             : }
    1059             : 
    1060             : inline uint8* CodedOutputStream::GetDirectBufferForNBytesAndAdvance(int size) {
    1061          38 :   if (buffer_size_ < size) {
    1062             :     return NULL;
    1063             :   } else {
    1064           5 :     uint8* result = buffer_;
    1065           5 :     Advance(size);
    1066             :     return result;
    1067             :   }
    1068             : }
    1069             : 
    1070     6685675 : inline uint8* CodedOutputStream::WriteVarint32ToArray(uint32 value,
    1071             :                                                       uint8* target) {
    1072    14962807 :   while (value >= 0x80) {
    1073       43627 :     *target = static_cast<uint8>(value | 0x80);
    1074       43627 :     value >>= 7;
    1075       43627 :     ++target;
    1076             :   }
    1077     8233505 :   *target = static_cast<uint8>(value);
    1078     6685675 :   return target + 1;
    1079             : }
    1080             : 
    1081           8 : inline void CodedOutputStream::WriteVarint32SignExtended(int32 value) {
    1082           8 :   if (value < 0) {
    1083           0 :     WriteVarint64(static_cast<uint64>(value));
    1084             :   } else {
    1085           8 :     WriteVarint32(static_cast<uint32>(value));
    1086             :   }
    1087           8 : }
    1088             : 
    1089     1298462 : inline uint8* CodedOutputStream::WriteVarint32SignExtendedToArray(
    1090             :     int32 value, uint8* target) {
    1091     1298462 :   if (value < 0) {
    1092           6 :     return WriteVarint64ToArray(static_cast<uint64>(value), target);
    1093             :   } else {
    1094     1318759 :     return WriteVarint32ToArray(static_cast<uint32>(value), target);
    1095             :   }
    1096             : }
    1097             : 
    1098          18 : inline uint8* CodedOutputStream::WriteLittleEndian32ToArray(uint32 value,
    1099             :                                                             uint8* target) {
    1100             : #if defined(PROTOBUF_LITTLE_ENDIAN)
    1101             :   memcpy(target, &value, sizeof(value));
    1102             : #else
    1103             :   target[0] = static_cast<uint8>(value);
    1104             :   target[1] = static_cast<uint8>(value >>  8);
    1105             :   target[2] = static_cast<uint8>(value >> 16);
    1106             :   target[3] = static_cast<uint8>(value >> 24);
    1107             : #endif
    1108          18 :   return target + sizeof(value);
    1109             : }
    1110             : 
    1111         153 : inline uint8* CodedOutputStream::WriteLittleEndian64ToArray(uint64 value,
    1112             :                                                             uint8* target) {
    1113             : #if defined(PROTOBUF_LITTLE_ENDIAN)
    1114         133 :   memcpy(target, &value, sizeof(value));
    1115             : #else
    1116             :   uint32 part0 = static_cast<uint32>(value);
    1117             :   uint32 part1 = static_cast<uint32>(value >> 32);
    1118             : 
    1119             :   target[0] = static_cast<uint8>(part0);
    1120             :   target[1] = static_cast<uint8>(part0 >>  8);
    1121             :   target[2] = static_cast<uint8>(part0 >> 16);
    1122             :   target[3] = static_cast<uint8>(part0 >> 24);
    1123             :   target[4] = static_cast<uint8>(part1);
    1124             :   target[5] = static_cast<uint8>(part1 >>  8);
    1125             :   target[6] = static_cast<uint8>(part1 >> 16);
    1126             :   target[7] = static_cast<uint8>(part1 >> 24);
    1127             : #endif
    1128         153 :   return target + sizeof(value);
    1129             : }
    1130             : 
    1131         177 : inline void CodedOutputStream::WriteVarint32(uint32 value) {
    1132         177 :   if (buffer_size_ >= 5) {
    1133             :     // Fast path:  We have enough bytes left in the buffer to guarantee that
    1134             :     // this write won't cross the end, so we can skip the checks.
    1135         172 :     uint8* target = buffer_;
    1136         172 :     uint8* end = WriteVarint32ToArray(value, target);
    1137         172 :     int size = end - target;
    1138         172 :     Advance(size);
    1139             :   } else {
    1140           5 :     WriteVarint32SlowPath(value);
    1141             :   }
    1142         177 : }
    1143             : 
    1144          64 : inline void CodedOutputStream::WriteTag(uint32 value) {
    1145          64 :   WriteVarint32(value);
    1146          64 : }
    1147             : 
    1148             : inline uint8* CodedOutputStream::WriteTagToArray(
    1149             :     uint32 value, uint8* target) {
    1150     4099526 :   return WriteVarint32ToArray(value, target);
    1151             : }
    1152             : 
    1153     4098322 : inline int CodedOutputStream::VarintSize32(uint32 value) {
    1154     4144594 :   if (value < (1 << 7)) {
    1155     4058689 :     return 1;
    1156             :   } else  {
    1157       48742 :     return VarintSize32Fallback(value);
    1158             :   }
    1159             : }
    1160             : 
    1161     1277392 : inline int CodedOutputStream::VarintSize32SignExtended(int32 value) {
    1162     1300846 :   if (value < 0) {
    1163           0 :     return 10;     // TODO(kenton):  Make this a symbolic constant.
    1164             :   } else {
    1165     1300840 :     return VarintSize32(static_cast<uint32>(value));
    1166             :   }
    1167             : }
    1168             : 
    1169             : inline void CodedOutputStream::WriteString(const string& str) {
    1170          26 :   WriteRaw(str.data(), static_cast<int>(str.size()));
    1171             : }
    1172             : 
    1173          40 : inline void CodedOutputStream::WriteRawMaybeAliased(
    1174             :     const void* data, int size) {
    1175          40 :   if (aliasing_enabled_) {
    1176           0 :     WriteAliasedRaw(data, size);
    1177             :   } else {
    1178          40 :     WriteRaw(data, size);
    1179             :   }
    1180          40 : }
    1181             : 
    1182             : inline uint8* CodedOutputStream::WriteStringToArray(
    1183             :     const string& str, uint8* target) {
    1184     1524209 :   return WriteRawToArray(str.data(), static_cast<int>(str.size()), target);
    1185             : }
    1186             : 
    1187             : inline int CodedOutputStream::ByteCount() const {
    1188          66 :   return total_bytes_ - buffer_size_;
    1189             : }
    1190             : 
    1191     8208072 : inline void CodedInputStream::Advance(int amount) {
    1192     9787652 :   buffer_ += amount;
    1193     8208072 : }
    1194             : 
    1195           0 : inline void CodedOutputStream::Advance(int amount) {
    1196         281 :   buffer_ += amount;
    1197         281 :   buffer_size_ -= amount;
    1198           0 : }
    1199             : 
    1200             : inline void CodedInputStream::SetRecursionLimit(int limit) {
    1201             :   recursion_budget_ += limit - recursion_limit_;
    1202             :   recursion_limit_ = limit;
    1203             : }
    1204             : 
    1205           0 : inline bool CodedInputStream::IncrementRecursionDepth() {
    1206        3084 :   --recursion_budget_;
    1207           0 :   return recursion_budget_ >= 0;
    1208             : }
    1209             : 
    1210             : inline void CodedInputStream::DecrementRecursionDepth() {
    1211           1 :   if (recursion_budget_ < recursion_limit_) ++recursion_budget_;
    1212             : }
    1213             : 
    1214           0 : inline void CodedInputStream::UnsafeDecrementRecursionDepth() {
    1215           0 :   assert(recursion_budget_ < recursion_limit_);
    1216        3083 :   ++recursion_budget_;
    1217           0 : }
    1218             : 
    1219             : inline void CodedInputStream::SetExtensionRegistry(const DescriptorPool* pool,
    1220             :                                                    MessageFactory* factory) {
    1221             :   extension_pool_ = pool;
    1222             :   extension_factory_ = factory;
    1223             : }
    1224             : 
    1225             : inline const DescriptorPool* CodedInputStream::GetExtensionPool() {
    1226             :   return extension_pool_;
    1227             : }
    1228             : 
    1229             : inline MessageFactory* CodedInputStream::GetExtensionFactory() {
    1230             :   return extension_factory_;
    1231             : }
    1232             : 
    1233     1533814 : inline int CodedInputStream::BufferSize() const {
    1234     8669455 :   return buffer_end_ - buffer_;
    1235             : }
    1236             : 
    1237     2794044 : inline CodedInputStream::CodedInputStream(ZeroCopyInputStream* input)
    1238             :   : buffer_(NULL),
    1239             :     buffer_end_(NULL),
    1240             :     input_(input),
    1241             :     total_bytes_read_(0),
    1242             :     overflow_bytes_(0),
    1243             :     last_tag_(0),
    1244             :     legitimate_message_end_(false),
    1245             :     aliasing_enabled_(false),
    1246             :     current_limit_(kint32max),
    1247             :     buffer_size_after_limit_(0),
    1248             :     total_bytes_limit_(kDefaultTotalBytesLimit),
    1249             :     total_bytes_warning_threshold_(kDefaultTotalBytesWarningThreshold),
    1250             :     recursion_budget_(default_recursion_limit_),
    1251             :     recursion_limit_(default_recursion_limit_),
    1252             :     extension_pool_(NULL),
    1253     2794044 :     extension_factory_(NULL) {
    1254             :   // Eagerly Refresh() so buffer space is immediately available.
    1255     2794044 :   Refresh();
    1256     2794702 : }
    1257             : 
    1258         830 : inline CodedInputStream::CodedInputStream(const uint8* buffer, int size)
    1259             :   : buffer_(buffer),
    1260         830 :     buffer_end_(buffer + size),
    1261             :     input_(NULL),
    1262             :     total_bytes_read_(size),
    1263             :     overflow_bytes_(0),
    1264             :     last_tag_(0),
    1265             :     legitimate_message_end_(false),
    1266             :     aliasing_enabled_(false),
    1267             :     current_limit_(size),
    1268             :     buffer_size_after_limit_(0),
    1269             :     total_bytes_limit_(kDefaultTotalBytesLimit),
    1270             :     total_bytes_warning_threshold_(kDefaultTotalBytesWarningThreshold),
    1271             :     recursion_budget_(default_recursion_limit_),
    1272             :     recursion_limit_(default_recursion_limit_),
    1273             :     extension_pool_(NULL),
    1274        1660 :     extension_factory_(NULL) {
    1275             :   // Note that setting current_limit_ == size is important to prevent some
    1276             :   // code paths from trying to access input_ and segfaulting.
    1277         830 : }
    1278             : 
    1279             : inline bool CodedInputStream::IsFlat() const {
    1280             :   return input_ == NULL;
    1281             : }
    1282             : 
    1283             : }  // namespace io
    1284             : }  // namespace protobuf
    1285             : 
    1286             : 
    1287             : #if defined(_MSC_VER) && _MSC_VER >= 1300
    1288             :   #pragma runtime_checks("c", restore)
    1289             : #endif  // _MSC_VER
    1290             : 
    1291             : }  // namespace google
    1292             : #endif  // GOOGLE_PROTOBUF_IO_CODED_STREAM_H__

Generated by: LCOV version 1.10