• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_t 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 
113 #include <assert.h>
114 
115 #include <atomic>
116 #include <climits>
117 #include <cstddef>
118 #include <cstring>
119 #include <limits>
120 #include <string>
121 #include <type_traits>
122 #include <utility>
123 
124 #ifdef _WIN32
125 // Assuming windows is always little-endian.
126 #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
127 #define PROTOBUF_LITTLE_ENDIAN 1
128 #endif
129 #if defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
130 // If MSVC has "/RTCc" set, it will complain about truncating casts at
131 // runtime.  This file contains some intentional truncating casts.
132 #pragma runtime_checks("c", off)
133 #endif
134 #else
135 #ifdef __APPLE__
136 #include <machine/endian.h>  // __BYTE_ORDER
137 #elif defined(__FreeBSD__)
138 #include <sys/endian.h>  // __BYTE_ORDER
139 #elif (defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__))
140 #include <sys/isa_defs.h>  // __BYTE_ORDER
141 #elif defined(_AIX) || defined(__TOS_AIX__)
142 #include <sys/machine.h>  // BYTE_ORDER
143 #else
144 #if !defined(__QNX__)
145 #include <endian.h>  // __BYTE_ORDER
146 #endif
147 #endif
148 #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) ||    \
149      (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \
150     !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
151 #define PROTOBUF_LITTLE_ENDIAN 1
152 #endif
153 #endif
154 #include <google/protobuf/stubs/common.h>
155 #include <google/protobuf/stubs/logging.h>
156 #include <google/protobuf/stubs/strutil.h>
157 #include <google/protobuf/port.h>
158 #include <google/protobuf/stubs/port.h>
159 
160 
161 // Must be included last.
162 #include <google/protobuf/port_def.inc>
163 
164 namespace google {
165 namespace protobuf {
166 
167 class DescriptorPool;
168 class MessageFactory;
169 class ZeroCopyCodedInputStream;
170 
171 namespace internal {
172 void MapTestForceDeterministic();
173 class EpsCopyByteStream;
174 }  // namespace internal
175 
176 namespace io {
177 
178 // Defined in this file.
179 class CodedInputStream;
180 class CodedOutputStream;
181 
182 // Defined in other files.
183 class ZeroCopyInputStream;   // zero_copy_stream.h
184 class ZeroCopyOutputStream;  // zero_copy_stream.h
185 
186 // Class which reads and decodes binary data which is composed of varint-
187 // encoded integers and fixed-width pieces.  Wraps a ZeroCopyInputStream.
188 // Most users will not need to deal with CodedInputStream.
189 //
190 // Most methods of CodedInputStream that return a bool return false if an
191 // underlying I/O error occurs or if the data is malformed.  Once such a
192 // failure occurs, the CodedInputStream is broken and is no longer useful.
193 // After a failure, callers also should assume writes to "out" args may have
194 // occurred, though nothing useful can be determined from those writes.
195 class PROTOBUF_EXPORT CodedInputStream {
196  public:
197   // Create a CodedInputStream that reads from the given ZeroCopyInputStream.
198   explicit CodedInputStream(ZeroCopyInputStream* input);
199 
200   // Create a CodedInputStream that reads from the given flat array.  This is
201   // faster than using an ArrayInputStream.  PushLimit(size) is implied by
202   // this constructor.
203   explicit CodedInputStream(const uint8_t* buffer, int size);
204 
205   // Destroy the CodedInputStream and position the underlying
206   // ZeroCopyInputStream at the first unread byte.  If an error occurred while
207   // reading (causing a method to return false), then the exact position of
208   // the input stream may be anywhere between the last value that was read
209   // successfully and the stream's byte limit.
210   ~CodedInputStream();
211 
212   // Return true if this CodedInputStream reads from a flat array instead of
213   // a ZeroCopyInputStream.
214   inline bool IsFlat() const;
215 
216   // Skips a number of bytes.  Returns false if an underlying read error
217   // occurs.
218   inline bool Skip(int count);
219 
220   // Sets *data to point directly at the unread part of the CodedInputStream's
221   // underlying buffer, and *size to the size of that buffer, but does not
222   // advance the stream's current position.  This will always either produce
223   // a non-empty buffer or return false.  If the caller consumes any of
224   // this data, it should then call Skip() to skip over the consumed bytes.
225   // This may be useful for implementing external fast parsing routines for
226   // types of data not covered by the CodedInputStream interface.
227   bool GetDirectBufferPointer(const void** data, int* size);
228 
229   // Like GetDirectBufferPointer, but this method is inlined, and does not
230   // attempt to Refresh() if the buffer is currently empty.
231   PROTOBUF_ALWAYS_INLINE
232   void GetDirectBufferPointerInline(const void** data, int* size);
233 
234   // Read raw bytes, copying them into the given buffer.
235   bool ReadRaw(void* buffer, int size);
236 
237   // Like ReadRaw, but reads into a string.
238   bool ReadString(std::string* buffer, int size);
239 
240 
241   // Read a 32-bit little-endian integer.
242   bool ReadLittleEndian32(uint32_t* value);
243   // Read a 64-bit little-endian integer.
244   bool ReadLittleEndian64(uint64_t* value);
245 
246   // These methods read from an externally provided buffer. The caller is
247   // responsible for ensuring that the buffer has sufficient space.
248   // Read a 32-bit little-endian integer.
249   static const uint8_t* ReadLittleEndian32FromArray(const uint8_t* buffer,
250                                                     uint32_t* value);
251   // Read a 64-bit little-endian integer.
252   static const uint8_t* ReadLittleEndian64FromArray(const uint8_t* buffer,
253                                                     uint64_t* value);
254 
255   // Read an unsigned integer with Varint encoding, truncating to 32 bits.
256   // Reading a 32-bit value is equivalent to reading a 64-bit one and casting
257   // it to uint32_t, but may be more efficient.
258   bool ReadVarint32(uint32_t* value);
259   // Read an unsigned integer with Varint encoding.
260   bool ReadVarint64(uint64_t* value);
261 
262   // Reads a varint off the wire into an "int". This should be used for reading
263   // sizes off the wire (sizes of strings, submessages, bytes fields, etc).
264   //
265   // The value from the wire is interpreted as unsigned.  If its value exceeds
266   // the representable value of an integer on this platform, instead of
267   // truncating we return false. Truncating (as performed by ReadVarint32()
268   // above) is an acceptable approach for fields representing an integer, but
269   // when we are parsing a size from the wire, truncating the value would result
270   // in us misparsing the payload.
271   bool ReadVarintSizeAsInt(int* value);
272 
273   // Read a tag.  This calls ReadVarint32() and returns the result, or returns
274   // zero (which is not a valid tag) if ReadVarint32() fails.  Also, ReadTag
275   // (but not ReadTagNoLastTag) updates the last tag value, which can be checked
276   // with LastTagWas().
277   //
278   // Always inline because this is only called in one place per parse loop
279   // but it is called for every iteration of said loop, so it should be fast.
280   // GCC doesn't want to inline this by default.
ReadTag()281   PROTOBUF_ALWAYS_INLINE uint32_t ReadTag() {
282     return last_tag_ = ReadTagNoLastTag();
283   }
284 
285   PROTOBUF_ALWAYS_INLINE uint32_t ReadTagNoLastTag();
286 
287   // This usually a faster alternative to ReadTag() when cutoff is a manifest
288   // constant.  It does particularly well for cutoff >= 127.  The first part
289   // of the return value is the tag that was read, though it can also be 0 in
290   // the cases where ReadTag() would return 0.  If the second part is true
291   // then the tag is known to be in [0, cutoff].  If not, the tag either is
292   // above cutoff or is 0.  (There's intentional wiggle room when tag is 0,
293   // because that can arise in several ways, and for best performance we want
294   // to avoid an extra "is tag == 0?" check here.)
295   PROTOBUF_ALWAYS_INLINE
ReadTagWithCutoff(uint32_t cutoff)296   std::pair<uint32_t, bool> ReadTagWithCutoff(uint32_t cutoff) {
297     std::pair<uint32_t, bool> result = ReadTagWithCutoffNoLastTag(cutoff);
298     last_tag_ = result.first;
299     return result;
300   }
301 
302   PROTOBUF_ALWAYS_INLINE
303   std::pair<uint32_t, bool> ReadTagWithCutoffNoLastTag(uint32_t cutoff);
304 
305   // Usually returns true if calling ReadVarint32() now would produce the given
306   // value.  Will always return false if ReadVarint32() would not return the
307   // given value.  If ExpectTag() returns true, it also advances past
308   // the varint.  For best performance, use a compile-time constant as the
309   // parameter.
310   // Always inline because this collapses to a small number of instructions
311   // when given a constant parameter, but GCC doesn't want to inline by default.
312   PROTOBUF_ALWAYS_INLINE bool ExpectTag(uint32_t expected);
313 
314   // Like above, except this reads from the specified buffer. The caller is
315   // responsible for ensuring that the buffer is large enough to read a varint
316   // of the expected size. For best performance, use a compile-time constant as
317   // the expected tag parameter.
318   //
319   // Returns a pointer beyond the expected tag if it was found, or NULL if it
320   // was not.
321   PROTOBUF_ALWAYS_INLINE
322   static const uint8_t* ExpectTagFromArray(const uint8_t* buffer,
323                                            uint32_t expected);
324 
325   // Usually returns true if no more bytes can be read.  Always returns false
326   // if more bytes can be read.  If ExpectAtEnd() returns true, a subsequent
327   // call to LastTagWas() will act as if ReadTag() had been called and returned
328   // zero, and ConsumedEntireMessage() will return true.
329   bool ExpectAtEnd();
330 
331   // If the last call to ReadTag() or ReadTagWithCutoff() returned the given
332   // value, returns true.  Otherwise, returns false.
333   // ReadTagNoLastTag/ReadTagWithCutoffNoLastTag do not preserve the last
334   // returned value.
335   //
336   // This is needed because parsers for some types of embedded messages
337   // (with field type TYPE_GROUP) don't actually know that they've reached the
338   // end of a message until they see an ENDGROUP tag, which was actually part
339   // of the enclosing message.  The enclosing message would like to check that
340   // tag to make sure it had the right number, so it calls LastTagWas() on
341   // return from the embedded parser to check.
342   bool LastTagWas(uint32_t expected);
SetLastTag(uint32_t tag)343   void SetLastTag(uint32_t tag) { last_tag_ = tag; }
344 
345   // When parsing message (but NOT a group), this method must be called
346   // immediately after MergeFromCodedStream() returns (if it returns true)
347   // to further verify that the message ended in a legitimate way.  For
348   // example, this verifies that parsing did not end on an end-group tag.
349   // It also checks for some cases where, due to optimizations,
350   // MergeFromCodedStream() can incorrectly return true.
351   bool ConsumedEntireMessage();
SetConsumed()352   void SetConsumed() { legitimate_message_end_ = true; }
353 
354   // Limits ----------------------------------------------------------
355   // Limits are used when parsing length-delimited embedded messages.
356   // After the message's length is read, PushLimit() is used to prevent
357   // the CodedInputStream from reading beyond that length.  Once the
358   // embedded message has been parsed, PopLimit() is called to undo the
359   // limit.
360 
361   // Opaque type used with PushLimit() and PopLimit().  Do not modify
362   // values of this type yourself.  The only reason that this isn't a
363   // struct with private internals is for efficiency.
364   typedef int Limit;
365 
366   // Places a limit on the number of bytes that the stream may read,
367   // starting from the current position.  Once the stream hits this limit,
368   // it will act like the end of the input has been reached until PopLimit()
369   // is called.
370   //
371   // As the names imply, the stream conceptually has a stack of limits.  The
372   // shortest limit on the stack is always enforced, even if it is not the
373   // top limit.
374   //
375   // The value returned by PushLimit() is opaque to the caller, and must
376   // be passed unchanged to the corresponding call to PopLimit().
377   Limit PushLimit(int byte_limit);
378 
379   // Pops the last limit pushed by PushLimit().  The input must be the value
380   // returned by that call to PushLimit().
381   void PopLimit(Limit limit);
382 
383   // Returns the number of bytes left until the nearest limit on the
384   // stack is hit, or -1 if no limits are in place.
385   int BytesUntilLimit() const;
386 
387   // Returns current position relative to the beginning of the input stream.
388   int CurrentPosition() const;
389 
390   // Total Bytes Limit -----------------------------------------------
391   // To prevent malicious users from sending excessively large messages
392   // and causing memory exhaustion, CodedInputStream imposes a hard limit on
393   // the total number of bytes it will read.
394 
395   // Sets the maximum number of bytes that this CodedInputStream will read
396   // before refusing to continue.  To prevent servers from allocating enormous
397   // amounts of memory to hold parsed messages, the maximum message length
398   // should be limited to the shortest length that will not harm usability.
399   // The default limit is INT_MAX (~2GB) and apps should set shorter limits
400   // if possible. An error will always be printed to stderr if the limit is
401   // reached.
402   //
403   // Note: setting a limit less than the current read position is interpreted
404   // as a limit on the current position.
405   //
406   // This is unrelated to PushLimit()/PopLimit().
407   void SetTotalBytesLimit(int total_bytes_limit);
408 
409   // The Total Bytes Limit minus the Current Position, or -1 if the total bytes
410   // limit is INT_MAX.
411   int BytesUntilTotalBytesLimit() const;
412 
413   // Recursion Limit -------------------------------------------------
414   // To prevent corrupt or malicious messages from causing stack overflows,
415   // we must keep track of the depth of recursion when parsing embedded
416   // messages and groups.  CodedInputStream keeps track of this because it
417   // is the only object that is passed down the stack during parsing.
418 
419   // Sets the maximum recursion depth.  The default is 100.
420   void SetRecursionLimit(int limit);
RecursionBudget()421   int RecursionBudget() { return recursion_budget_; }
422 
GetDefaultRecursionLimit()423   static int GetDefaultRecursionLimit() { return default_recursion_limit_; }
424 
425   // Increments the current recursion depth.  Returns true if the depth is
426   // under the limit, false if it has gone over.
427   bool IncrementRecursionDepth();
428 
429   // Decrements the recursion depth if possible.
430   void DecrementRecursionDepth();
431 
432   // Decrements the recursion depth blindly.  This is faster than
433   // DecrementRecursionDepth().  It should be used only if all previous
434   // increments to recursion depth were successful.
435   void UnsafeDecrementRecursionDepth();
436 
437   // Shorthand for make_pair(PushLimit(byte_limit), --recursion_budget_).
438   // Using this can reduce code size and complexity in some cases.  The caller
439   // is expected to check that the second part of the result is non-negative (to
440   // bail out if the depth of recursion is too high) and, if all is well, to
441   // later pass the first part of the result to PopLimit() or similar.
442   std::pair<CodedInputStream::Limit, int> IncrementRecursionDepthAndPushLimit(
443       int byte_limit);
444 
445   // Shorthand for PushLimit(ReadVarint32(&length) ? length : 0).
446   Limit ReadLengthAndPushLimit();
447 
448   // Helper that is equivalent to: {
449   //  bool result = ConsumedEntireMessage();
450   //  PopLimit(limit);
451   //  UnsafeDecrementRecursionDepth();
452   //  return result; }
453   // Using this can reduce code size and complexity in some cases.
454   // Do not use unless the current recursion depth is greater than zero.
455   bool DecrementRecursionDepthAndPopLimit(Limit limit);
456 
457   // Helper that is equivalent to: {
458   //  bool result = ConsumedEntireMessage();
459   //  PopLimit(limit);
460   //  return result; }
461   // Using this can reduce code size and complexity in some cases.
462   bool CheckEntireMessageConsumedAndPopLimit(Limit limit);
463 
464   // Extension Registry ----------------------------------------------
465   // ADVANCED USAGE:  99.9% of people can ignore this section.
466   //
467   // By default, when parsing extensions, the parser looks for extension
468   // definitions in the pool which owns the outer message's Descriptor.
469   // However, you may call SetExtensionRegistry() to provide an alternative
470   // pool instead.  This makes it possible, for example, to parse a message
471   // using a generated class, but represent some extensions using
472   // DynamicMessage.
473 
474   // Set the pool used to look up extensions.  Most users do not need to call
475   // this as the correct pool will be chosen automatically.
476   //
477   // WARNING:  It is very easy to misuse this.  Carefully read the requirements
478   //   below.  Do not use this unless you are sure you need it.  Almost no one
479   //   does.
480   //
481   // Let's say you are parsing a message into message object m, and you want
482   // to take advantage of SetExtensionRegistry().  You must follow these
483   // requirements:
484   //
485   // The given DescriptorPool must contain m->GetDescriptor().  It is not
486   // sufficient for it to simply contain a descriptor that has the same name
487   // and content -- it must be the *exact object*.  In other words:
488   //   assert(pool->FindMessageTypeByName(m->GetDescriptor()->full_name()) ==
489   //          m->GetDescriptor());
490   // There are two ways to satisfy this requirement:
491   // 1) Use m->GetDescriptor()->pool() as the pool.  This is generally useless
492   //    because this is the pool that would be used anyway if you didn't call
493   //    SetExtensionRegistry() at all.
494   // 2) Use a DescriptorPool which has m->GetDescriptor()->pool() as an
495   //    "underlay".  Read the documentation for DescriptorPool for more
496   //    information about underlays.
497   //
498   // You must also provide a MessageFactory.  This factory will be used to
499   // construct Message objects representing extensions.  The factory's
500   // GetPrototype() MUST return non-NULL for any Descriptor which can be found
501   // through the provided pool.
502   //
503   // If the provided factory might return instances of protocol-compiler-
504   // generated (i.e. compiled-in) types, or if the outer message object m is
505   // a generated type, then the given factory MUST have this property:  If
506   // GetPrototype() is given a Descriptor which resides in
507   // DescriptorPool::generated_pool(), the factory MUST return the same
508   // prototype which MessageFactory::generated_factory() would return.  That
509   // is, given a descriptor for a generated type, the factory must return an
510   // instance of the generated class (NOT DynamicMessage).  However, when
511   // given a descriptor for a type that is NOT in generated_pool, the factory
512   // is free to return any implementation.
513   //
514   // The reason for this requirement is that generated sub-objects may be
515   // accessed via the standard (non-reflection) extension accessor methods,
516   // and these methods will down-cast the object to the generated class type.
517   // If the object is not actually of that type, the results would be undefined.
518   // On the other hand, if an extension is not compiled in, then there is no
519   // way the code could end up accessing it via the standard accessors -- the
520   // only way to access the extension is via reflection.  When using reflection,
521   // DynamicMessage and generated messages are indistinguishable, so it's fine
522   // if these objects are represented using DynamicMessage.
523   //
524   // Using DynamicMessageFactory on which you have called
525   // SetDelegateToGeneratedFactory(true) should be sufficient to satisfy the
526   // above requirement.
527   //
528   // If either pool or factory is NULL, both must be NULL.
529   //
530   // Note that this feature is ignored when parsing "lite" messages as they do
531   // not have descriptors.
532   void SetExtensionRegistry(const DescriptorPool* pool,
533                             MessageFactory* factory);
534 
535   // Get the DescriptorPool set via SetExtensionRegistry(), or NULL if no pool
536   // has been provided.
537   const DescriptorPool* GetExtensionPool();
538 
539   // Get the MessageFactory set via SetExtensionRegistry(), or NULL if no
540   // factory has been provided.
541   MessageFactory* GetExtensionFactory();
542 
543  private:
544   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedInputStream);
545 
546   const uint8_t* buffer_;
547   const uint8_t* buffer_end_;  // pointer to the end of the buffer.
548   ZeroCopyInputStream* input_;
549   int total_bytes_read_;  // total bytes read from input_, including
550                           // the current buffer
551 
552   // If total_bytes_read_ surpasses INT_MAX, we record the extra bytes here
553   // so that we can BackUp() on destruction.
554   int overflow_bytes_;
555 
556   // LastTagWas() stuff.
557   uint32_t last_tag_;  // result of last ReadTag() or ReadTagWithCutoff().
558 
559   // This is set true by ReadTag{Fallback/Slow}() if it is called when exactly
560   // at EOF, or by ExpectAtEnd() when it returns true.  This happens when we
561   // reach the end of a message and attempt to read another tag.
562   bool legitimate_message_end_;
563 
564   // See EnableAliasing().
565   bool aliasing_enabled_;
566 
567   // Limits
568   Limit current_limit_;  // if position = -1, no limit is applied
569 
570   // For simplicity, if the current buffer crosses a limit (either a normal
571   // limit created by PushLimit() or the total bytes limit), buffer_size_
572   // only tracks the number of bytes before that limit.  This field
573   // contains the number of bytes after it.  Note that this implies that if
574   // buffer_size_ == 0 and buffer_size_after_limit_ > 0, we know we've
575   // hit a limit.  However, if both are zero, it doesn't necessarily mean
576   // we aren't at a limit -- the buffer may have ended exactly at the limit.
577   int buffer_size_after_limit_;
578 
579   // Maximum number of bytes to read, period.  This is unrelated to
580   // current_limit_.  Set using SetTotalBytesLimit().
581   int total_bytes_limit_;
582 
583   // Current recursion budget, controlled by IncrementRecursionDepth() and
584   // similar.  Starts at recursion_limit_ and goes down: if this reaches
585   // -1 we are over budget.
586   int recursion_budget_;
587   // Recursion depth limit, set by SetRecursionLimit().
588   int recursion_limit_;
589 
590   // See SetExtensionRegistry().
591   const DescriptorPool* extension_pool_;
592   MessageFactory* extension_factory_;
593 
594   // Private member functions.
595 
596   // Fallback when Skip() goes past the end of the current buffer.
597   bool SkipFallback(int count, int original_buffer_size);
598 
599   // Advance the buffer by a given number of bytes.
600   void Advance(int amount);
601 
602   // Back up input_ to the current buffer position.
603   void BackUpInputToCurrentPosition();
604 
605   // Recomputes the value of buffer_size_after_limit_.  Must be called after
606   // current_limit_ or total_bytes_limit_ changes.
607   void RecomputeBufferLimits();
608 
609   // Writes an error message saying that we hit total_bytes_limit_.
610   void PrintTotalBytesLimitError();
611 
612   // Called when the buffer runs out to request more data.  Implies an
613   // Advance(BufferSize()).
614   bool Refresh();
615 
616   // When parsing varints, we optimize for the common case of small values, and
617   // then optimize for the case when the varint fits within the current buffer
618   // piece. The Fallback method is used when we can't use the one-byte
619   // optimization. The Slow method is yet another fallback when the buffer is
620   // not large enough. Making the slow path out-of-line speeds up the common
621   // case by 10-15%. The slow path is fairly uncommon: it only triggers when a
622   // message crosses multiple buffers.  Note: ReadVarint32Fallback() and
623   // ReadVarint64Fallback() are called frequently and generally not inlined, so
624   // they have been optimized to avoid "out" parameters.  The former returns -1
625   // if it fails and the uint32_t it read otherwise.  The latter has a bool
626   // indicating success or failure as part of its return type.
627   int64_t ReadVarint32Fallback(uint32_t first_byte_or_zero);
628   int ReadVarintSizeAsIntFallback();
629   std::pair<uint64_t, bool> ReadVarint64Fallback();
630   bool ReadVarint32Slow(uint32_t* value);
631   bool ReadVarint64Slow(uint64_t* value);
632   int ReadVarintSizeAsIntSlow();
633   bool ReadLittleEndian32Fallback(uint32_t* value);
634   bool ReadLittleEndian64Fallback(uint64_t* value);
635 
636   // Fallback/slow methods for reading tags. These do not update last_tag_,
637   // but will set legitimate_message_end_ if we are at the end of the input
638   // stream.
639   uint32_t ReadTagFallback(uint32_t first_byte_or_zero);
640   uint32_t ReadTagSlow();
641   bool ReadStringFallback(std::string* buffer, int size);
642 
643   // Return the size of the buffer.
644   int BufferSize() const;
645 
646   static const int kDefaultTotalBytesLimit = INT_MAX;
647 
648   static int default_recursion_limit_;  // 100 by default.
649 
650   friend class google::protobuf::ZeroCopyCodedInputStream;
651   friend class google::protobuf::internal::EpsCopyByteStream;
652 };
653 
654 // EpsCopyOutputStream wraps a ZeroCopyOutputStream and exposes a new stream,
655 // which has the property you can write kSlopBytes (16 bytes) from the current
656 // position without bounds checks. The cursor into the stream is managed by
657 // the user of the class and is an explicit parameter in the methods. Careful
658 // use of this class, ie. keep ptr a local variable, eliminates the need to
659 // for the compiler to sync the ptr value between register and memory.
660 class PROTOBUF_EXPORT EpsCopyOutputStream {
661  public:
662   enum { kSlopBytes = 16 };
663 
664   // Initialize from a stream.
EpsCopyOutputStream(ZeroCopyOutputStream * stream,bool deterministic,uint8_t ** pp)665   EpsCopyOutputStream(ZeroCopyOutputStream* stream, bool deterministic,
666                       uint8_t** pp)
667       : end_(buffer_),
668         stream_(stream),
669         is_serialization_deterministic_(deterministic) {
670     *pp = buffer_;
671   }
672 
673   // Only for array serialization. No overflow protection, end_ will be the
674   // pointed to the end of the array. When using this the total size is already
675   // known, so no need to maintain the slop region.
EpsCopyOutputStream(void * data,int size,bool deterministic)676   EpsCopyOutputStream(void* data, int size, bool deterministic)
677       : end_(static_cast<uint8_t*>(data) + size),
678         buffer_end_(nullptr),
679         stream_(nullptr),
680         is_serialization_deterministic_(deterministic) {}
681 
682   // Initialize from stream but with the first buffer already given (eager).
EpsCopyOutputStream(void * data,int size,ZeroCopyOutputStream * stream,bool deterministic,uint8_t ** pp)683   EpsCopyOutputStream(void* data, int size, ZeroCopyOutputStream* stream,
684                       bool deterministic, uint8_t** pp)
685       : stream_(stream), is_serialization_deterministic_(deterministic) {
686     *pp = SetInitialBuffer(data, size);
687   }
688 
689   // Flush everything that's written into the underlying ZeroCopyOutputStream
690   // and trims the underlying stream to the location of ptr.
691   uint8_t* Trim(uint8_t* ptr);
692 
693   // After this it's guaranteed you can safely write kSlopBytes to ptr. This
694   // will never fail! The underlying stream can produce an error. Use HadError
695   // to check for errors.
EnsureSpace(uint8_t * ptr)696   PROTOBUF_NODISCARD uint8_t* EnsureSpace(uint8_t* ptr) {
697     if (PROTOBUF_PREDICT_FALSE(ptr >= end_)) {
698       return EnsureSpaceFallback(ptr);
699     }
700     return ptr;
701   }
702 
WriteRaw(const void * data,int size,uint8_t * ptr)703   uint8_t* WriteRaw(const void* data, int size, uint8_t* ptr) {
704     if (PROTOBUF_PREDICT_FALSE(end_ - ptr < size)) {
705       return WriteRawFallback(data, size, ptr);
706     }
707     std::memcpy(ptr, data, size);
708     return ptr + size;
709   }
710   // Writes the buffer specified by data, size to the stream. Possibly by
711   // aliasing the buffer (ie. not copying the data). The caller is responsible
712   // to make sure the buffer is alive for the duration of the
713   // ZeroCopyOutputStream.
714 #ifndef NDEBUG
715   PROTOBUF_NOINLINE
716 #endif
WriteRawMaybeAliased(const void * data,int size,uint8_t * ptr)717   uint8_t* WriteRawMaybeAliased(const void* data, int size, uint8_t* ptr) {
718     if (aliasing_enabled_) {
719       return WriteAliasedRaw(data, size, ptr);
720     } else {
721       return WriteRaw(data, size, ptr);
722     }
723   }
724 
725 
726 #ifndef NDEBUG
727   PROTOBUF_NOINLINE
728 #endif
WriteStringMaybeAliased(uint32_t num,const std::string & s,uint8_t * ptr)729   uint8_t* WriteStringMaybeAliased(uint32_t num, const std::string& s,
730                                    uint8_t* ptr) {
731     std::ptrdiff_t size = s.size();
732     if (PROTOBUF_PREDICT_FALSE(
733             size >= 128 || end_ - ptr + 16 - TagSize(num << 3) - 1 < size)) {
734       return WriteStringMaybeAliasedOutline(num, s, ptr);
735     }
736     ptr = UnsafeVarint((num << 3) | 2, ptr);
737     *ptr++ = static_cast<uint8_t>(size);
738     std::memcpy(ptr, s.data(), size);
739     return ptr + size;
740   }
WriteBytesMaybeAliased(uint32_t num,const std::string & s,uint8_t * ptr)741   uint8_t* WriteBytesMaybeAliased(uint32_t num, const std::string& s,
742                                   uint8_t* ptr) {
743     return WriteStringMaybeAliased(num, s, ptr);
744   }
745 
746   template <typename T>
WriteString(uint32_t num,const T & s,uint8_t * ptr)747   PROTOBUF_ALWAYS_INLINE uint8_t* WriteString(uint32_t num, const T& s,
748                                               uint8_t* ptr) {
749     std::ptrdiff_t size = s.size();
750     if (PROTOBUF_PREDICT_FALSE(
751             size >= 128 || end_ - ptr + 16 - TagSize(num << 3) - 1 < size)) {
752       return WriteStringOutline(num, s, ptr);
753     }
754     ptr = UnsafeVarint((num << 3) | 2, ptr);
755     *ptr++ = static_cast<uint8_t>(size);
756     std::memcpy(ptr, s.data(), size);
757     return ptr + size;
758   }
759   template <typename T>
760 #ifndef NDEBUG
761   PROTOBUF_NOINLINE
762 #endif
WriteBytes(uint32_t num,const T & s,uint8_t * ptr)763   uint8_t* WriteBytes(uint32_t num, const T& s, uint8_t* ptr) {
764     return WriteString(num, s, ptr);
765   }
766 
767   template <typename T>
WriteInt32Packed(int num,const T & r,int size,uint8_t * ptr)768   PROTOBUF_ALWAYS_INLINE uint8_t* WriteInt32Packed(int num, const T& r,
769                                                    int size, uint8_t* ptr) {
770     return WriteVarintPacked(num, r, size, ptr, Encode64);
771   }
772   template <typename T>
WriteUInt32Packed(int num,const T & r,int size,uint8_t * ptr)773   PROTOBUF_ALWAYS_INLINE uint8_t* WriteUInt32Packed(int num, const T& r,
774                                                     int size, uint8_t* ptr) {
775     return WriteVarintPacked(num, r, size, ptr, Encode32);
776   }
777   template <typename T>
WriteSInt32Packed(int num,const T & r,int size,uint8_t * ptr)778   PROTOBUF_ALWAYS_INLINE uint8_t* WriteSInt32Packed(int num, const T& r,
779                                                     int size, uint8_t* ptr) {
780     return WriteVarintPacked(num, r, size, ptr, ZigZagEncode32);
781   }
782   template <typename T>
WriteInt64Packed(int num,const T & r,int size,uint8_t * ptr)783   PROTOBUF_ALWAYS_INLINE uint8_t* WriteInt64Packed(int num, const T& r,
784                                                    int size, uint8_t* ptr) {
785     return WriteVarintPacked(num, r, size, ptr, Encode64);
786   }
787   template <typename T>
WriteUInt64Packed(int num,const T & r,int size,uint8_t * ptr)788   PROTOBUF_ALWAYS_INLINE uint8_t* WriteUInt64Packed(int num, const T& r,
789                                                     int size, uint8_t* ptr) {
790     return WriteVarintPacked(num, r, size, ptr, Encode64);
791   }
792   template <typename T>
WriteSInt64Packed(int num,const T & r,int size,uint8_t * ptr)793   PROTOBUF_ALWAYS_INLINE uint8_t* WriteSInt64Packed(int num, const T& r,
794                                                     int size, uint8_t* ptr) {
795     return WriteVarintPacked(num, r, size, ptr, ZigZagEncode64);
796   }
797   template <typename T>
WriteEnumPacked(int num,const T & r,int size,uint8_t * ptr)798   PROTOBUF_ALWAYS_INLINE uint8_t* WriteEnumPacked(int num, const T& r, int size,
799                                                   uint8_t* ptr) {
800     return WriteVarintPacked(num, r, size, ptr, Encode64);
801   }
802 
803   template <typename T>
WriteFixedPacked(int num,const T & r,uint8_t * ptr)804   PROTOBUF_ALWAYS_INLINE uint8_t* WriteFixedPacked(int num, const T& r,
805                                                    uint8_t* ptr) {
806     ptr = EnsureSpace(ptr);
807     constexpr auto element_size = sizeof(typename T::value_type);
808     auto size = r.size() * element_size;
809     ptr = WriteLengthDelim(num, size, ptr);
810     return WriteRawLittleEndian<element_size>(r.data(), static_cast<int>(size),
811                                               ptr);
812   }
813 
814   // Returns true if there was an underlying I/O error since this object was
815   // created.
HadError()816   bool HadError() const { return had_error_; }
817 
818   // Instructs the EpsCopyOutputStream to allow the underlying
819   // ZeroCopyOutputStream to hold pointers to the original structure instead of
820   // copying, if it supports it (i.e. output->AllowsAliasing() is true).  If the
821   // underlying stream does not support aliasing, then enabling it has no
822   // affect.  For now, this only affects the behavior of
823   // WriteRawMaybeAliased().
824   //
825   // NOTE: It is caller's responsibility to ensure that the chunk of memory
826   // remains live until all of the data has been consumed from the stream.
827   void EnableAliasing(bool enabled);
828 
829   // See documentation on CodedOutputStream::SetSerializationDeterministic.
SetSerializationDeterministic(bool value)830   void SetSerializationDeterministic(bool value) {
831     is_serialization_deterministic_ = value;
832   }
833 
834   // See documentation on CodedOutputStream::IsSerializationDeterministic.
IsSerializationDeterministic()835   bool IsSerializationDeterministic() const {
836     return is_serialization_deterministic_;
837   }
838 
839   // The number of bytes written to the stream at position ptr, relative to the
840   // stream's overall position.
841   int64_t ByteCount(uint8_t* ptr) const;
842 
843 
844  private:
845   uint8_t* end_;
846   uint8_t* buffer_end_ = buffer_;
847   uint8_t buffer_[2 * kSlopBytes];
848   ZeroCopyOutputStream* stream_;
849   bool had_error_ = false;
850   bool aliasing_enabled_ = false;  // See EnableAliasing().
851   bool is_serialization_deterministic_;
852   bool skip_check_consistency = false;
853 
854   uint8_t* EnsureSpaceFallback(uint8_t* ptr);
855   inline uint8_t* Next();
856   int Flush(uint8_t* ptr);
GetSize(uint8_t * ptr)857   std::ptrdiff_t GetSize(uint8_t* ptr) const {
858     GOOGLE_DCHECK(ptr <= end_ + kSlopBytes);  // NOLINT
859     return end_ + kSlopBytes - ptr;
860   }
861 
Error()862   uint8_t* Error() {
863     had_error_ = true;
864     // We use the patch buffer to always guarantee space to write to.
865     end_ = buffer_ + kSlopBytes;
866     return buffer_;
867   }
868 
TagSize(uint32_t tag)869   static constexpr int TagSize(uint32_t tag) {
870     return (tag < (1 << 7))    ? 1
871            : (tag < (1 << 14)) ? 2
872            : (tag < (1 << 21)) ? 3
873            : (tag < (1 << 28)) ? 4
874                                : 5;
875   }
876 
WriteTag(uint32_t num,uint32_t wt,uint8_t * ptr)877   PROTOBUF_ALWAYS_INLINE uint8_t* WriteTag(uint32_t num, uint32_t wt,
878                                            uint8_t* ptr) {
879     GOOGLE_DCHECK(ptr < end_);  // NOLINT
880     return UnsafeVarint((num << 3) | wt, ptr);
881   }
882 
WriteLengthDelim(int num,uint32_t size,uint8_t * ptr)883   PROTOBUF_ALWAYS_INLINE uint8_t* WriteLengthDelim(int num, uint32_t size,
884                                                    uint8_t* ptr) {
885     ptr = WriteTag(num, 2, ptr);
886     return UnsafeWriteSize(size, ptr);
887   }
888 
889   uint8_t* WriteRawFallback(const void* data, int size, uint8_t* ptr);
890 
891   uint8_t* WriteAliasedRaw(const void* data, int size, uint8_t* ptr);
892 
893   uint8_t* WriteStringMaybeAliasedOutline(uint32_t num, const std::string& s,
894                                           uint8_t* ptr);
895   uint8_t* WriteStringOutline(uint32_t num, const std::string& s, uint8_t* ptr);
896 
897   template <typename T, typename E>
WriteVarintPacked(int num,const T & r,int size,uint8_t * ptr,const E & encode)898   PROTOBUF_ALWAYS_INLINE uint8_t* WriteVarintPacked(int num, const T& r,
899                                                     int size, uint8_t* ptr,
900                                                     const E& encode) {
901     ptr = EnsureSpace(ptr);
902     ptr = WriteLengthDelim(num, size, ptr);
903     auto it = r.data();
904     auto end = it + r.size();
905     do {
906       ptr = EnsureSpace(ptr);
907       ptr = UnsafeVarint(encode(*it++), ptr);
908     } while (it < end);
909     return ptr;
910   }
911 
Encode32(uint32_t v)912   static uint32_t Encode32(uint32_t v) { return v; }
Encode64(uint64_t v)913   static uint64_t Encode64(uint64_t v) { return v; }
ZigZagEncode32(int32_t v)914   static uint32_t ZigZagEncode32(int32_t v) {
915     return (static_cast<uint32_t>(v) << 1) ^ static_cast<uint32_t>(v >> 31);
916   }
ZigZagEncode64(int64_t v)917   static uint64_t ZigZagEncode64(int64_t v) {
918     return (static_cast<uint64_t>(v) << 1) ^ static_cast<uint64_t>(v >> 63);
919   }
920 
921   template <typename T>
UnsafeVarint(T value,uint8_t * ptr)922   PROTOBUF_ALWAYS_INLINE static uint8_t* UnsafeVarint(T value, uint8_t* ptr) {
923     static_assert(std::is_unsigned<T>::value,
924                   "Varint serialization must be unsigned");
925     ptr[0] = static_cast<uint8_t>(value);
926     if (value < 0x80) {
927       return ptr + 1;
928     }
929     // Turn on continuation bit in the byte we just wrote.
930     ptr[0] |= static_cast<uint8_t>(0x80);
931     value >>= 7;
932     ptr[1] = static_cast<uint8_t>(value);
933     if (value < 0x80) {
934       return ptr + 2;
935     }
936     ptr += 2;
937     do {
938       // Turn on continuation bit in the byte we just wrote.
939       ptr[-1] |= static_cast<uint8_t>(0x80);
940       value >>= 7;
941       *ptr = static_cast<uint8_t>(value);
942       ++ptr;
943     } while (value >= 0x80);
944     return ptr;
945   }
946 
UnsafeWriteSize(uint32_t value,uint8_t * ptr)947   PROTOBUF_ALWAYS_INLINE static uint8_t* UnsafeWriteSize(uint32_t value,
948                                                          uint8_t* ptr) {
949     while (PROTOBUF_PREDICT_FALSE(value >= 0x80)) {
950       *ptr = static_cast<uint8_t>(value | 0x80);
951       value >>= 7;
952       ++ptr;
953     }
954     *ptr++ = static_cast<uint8_t>(value);
955     return ptr;
956   }
957 
958   template <int S>
959   uint8_t* WriteRawLittleEndian(const void* data, int size, uint8_t* ptr);
960 #ifndef PROTOBUF_LITTLE_ENDIAN
961   uint8_t* WriteRawLittleEndian32(const void* data, int size, uint8_t* ptr);
962   uint8_t* WriteRawLittleEndian64(const void* data, int size, uint8_t* ptr);
963 #endif
964 
965   // These methods are for CodedOutputStream. Ideally they should be private
966   // but to match current behavior of CodedOutputStream as close as possible
967   // we allow it some functionality.
968  public:
SetInitialBuffer(void * data,int size)969   uint8_t* SetInitialBuffer(void* data, int size) {
970     auto ptr = static_cast<uint8_t*>(data);
971     if (size > kSlopBytes) {
972       end_ = ptr + size - kSlopBytes;
973       buffer_end_ = nullptr;
974       return ptr;
975     } else {
976       end_ = buffer_ + size;
977       buffer_end_ = ptr;
978       return buffer_;
979     }
980   }
981 
982  private:
983   // Needed by CodedOutputStream HadError. HadError needs to flush the patch
984   // buffers to ensure there is no error as of yet.
985   uint8_t* FlushAndResetBuffer(uint8_t*);
986 
987   // The following functions mimic the old CodedOutputStream behavior as close
988   // as possible. They flush the current state to the stream, behave as
989   // the old CodedOutputStream and then return to normal operation.
990   bool Skip(int count, uint8_t** pp);
991   bool GetDirectBufferPointer(void** data, int* size, uint8_t** pp);
992   uint8_t* GetDirectBufferForNBytesAndAdvance(int size, uint8_t** pp);
993 
994   friend class CodedOutputStream;
995 };
996 
997 template <>
998 inline uint8_t* EpsCopyOutputStream::WriteRawLittleEndian<1>(const void* data,
999                                                              int size,
1000                                                              uint8_t* ptr) {
1001   return WriteRaw(data, size, ptr);
1002 }
1003 template <>
1004 inline uint8_t* EpsCopyOutputStream::WriteRawLittleEndian<4>(const void* data,
1005                                                              int size,
1006                                                              uint8_t* ptr) {
1007 #ifdef PROTOBUF_LITTLE_ENDIAN
1008   return WriteRaw(data, size, ptr);
1009 #else
1010   return WriteRawLittleEndian32(data, size, ptr);
1011 #endif
1012 }
1013 template <>
1014 inline uint8_t* EpsCopyOutputStream::WriteRawLittleEndian<8>(const void* data,
1015                                                              int size,
1016                                                              uint8_t* ptr) {
1017 #ifdef PROTOBUF_LITTLE_ENDIAN
1018   return WriteRaw(data, size, ptr);
1019 #else
1020   return WriteRawLittleEndian64(data, size, ptr);
1021 #endif
1022 }
1023 
1024 // Class which encodes and writes binary data which is composed of varint-
1025 // encoded integers and fixed-width pieces.  Wraps a ZeroCopyOutputStream.
1026 // Most users will not need to deal with CodedOutputStream.
1027 //
1028 // Most methods of CodedOutputStream which return a bool return false if an
1029 // underlying I/O error occurs.  Once such a failure occurs, the
1030 // CodedOutputStream is broken and is no longer useful. The Write* methods do
1031 // not return the stream status, but will invalidate the stream if an error
1032 // occurs. The client can probe HadError() to determine the status.
1033 //
1034 // Note that every method of CodedOutputStream which writes some data has
1035 // a corresponding static "ToArray" version. These versions write directly
1036 // to the provided buffer, returning a pointer past the last written byte.
1037 // They require that the buffer has sufficient capacity for the encoded data.
1038 // This allows an optimization where we check if an output stream has enough
1039 // space for an entire message before we start writing and, if there is, we
1040 // call only the ToArray methods to avoid doing bound checks for each
1041 // individual value.
1042 // i.e., in the example above:
1043 //
1044 //   CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
1045 //   int magic_number = 1234;
1046 //   char text[] = "Hello world!";
1047 //
1048 //   int coded_size = sizeof(magic_number) +
1049 //                    CodedOutputStream::VarintSize32(strlen(text)) +
1050 //                    strlen(text);
1051 //
1052 //   uint8_t* buffer =
1053 //       coded_output->GetDirectBufferForNBytesAndAdvance(coded_size);
1054 //   if (buffer != nullptr) {
1055 //     // The output stream has enough space in the buffer: write directly to
1056 //     // the array.
1057 //     buffer = CodedOutputStream::WriteLittleEndian32ToArray(magic_number,
1058 //                                                            buffer);
1059 //     buffer = CodedOutputStream::WriteVarint32ToArray(strlen(text), buffer);
1060 //     buffer = CodedOutputStream::WriteRawToArray(text, strlen(text), buffer);
1061 //   } else {
1062 //     // Make bound-checked writes, which will ask the underlying stream for
1063 //     // more space as needed.
1064 //     coded_output->WriteLittleEndian32(magic_number);
1065 //     coded_output->WriteVarint32(strlen(text));
1066 //     coded_output->WriteRaw(text, strlen(text));
1067 //   }
1068 //
1069 //   delete coded_output;
1070 class PROTOBUF_EXPORT CodedOutputStream {
1071  public:
1072   // Creates a CodedOutputStream that writes to the given `stream`.
1073   // The provided stream must publicly derive from `ZeroCopyOutputStream`.
1074   template <class Stream, class = typename std::enable_if<std::is_base_of<
1075                               ZeroCopyOutputStream, Stream>::value>::type>
1076   explicit CodedOutputStream(Stream* stream);
1077 
1078   // Creates a CodedOutputStream that writes to the given `stream`, and does
1079   // an 'eager initialization' of the internal state if `eager_init` is true.
1080   // The provided stream must publicly derive from `ZeroCopyOutputStream`.
1081   template <class Stream, class = typename std::enable_if<std::is_base_of<
1082                               ZeroCopyOutputStream, Stream>::value>::type>
1083   CodedOutputStream(Stream* stream, bool eager_init);
1084 
1085   // Destroy the CodedOutputStream and position the underlying
1086   // ZeroCopyOutputStream immediately after the last byte written.
1087   ~CodedOutputStream();
1088 
1089   // Returns true if there was an underlying I/O error since this object was
1090   // created. On should call Trim before this function in order to catch all
1091   // errors.
HadError()1092   bool HadError() {
1093     cur_ = impl_.FlushAndResetBuffer(cur_);
1094     GOOGLE_DCHECK(cur_);
1095     return impl_.HadError();
1096   }
1097 
1098   // Trims any unused space in the underlying buffer so that its size matches
1099   // the number of bytes written by this stream. The underlying buffer will
1100   // automatically be trimmed when this stream is destroyed; this call is only
1101   // necessary if the underlying buffer is accessed *before* the stream is
1102   // destroyed.
Trim()1103   void Trim() { cur_ = impl_.Trim(cur_); }
1104 
1105   // Skips a number of bytes, leaving the bytes unmodified in the underlying
1106   // buffer.  Returns false if an underlying write error occurs.  This is
1107   // mainly useful with GetDirectBufferPointer().
1108   // Note of caution, the skipped bytes may contain uninitialized data. The
1109   // caller must make sure that the skipped bytes are properly initialized,
1110   // otherwise you might leak bytes from your heap.
Skip(int count)1111   bool Skip(int count) { return impl_.Skip(count, &cur_); }
1112 
1113   // Sets *data to point directly at the unwritten part of the
1114   // CodedOutputStream's underlying buffer, and *size to the size of that
1115   // buffer, but does not advance the stream's current position.  This will
1116   // always either produce a non-empty buffer or return false.  If the caller
1117   // writes any data to this buffer, it should then call Skip() to skip over
1118   // the consumed bytes.  This may be useful for implementing external fast
1119   // serialization routines for types of data not covered by the
1120   // CodedOutputStream interface.
GetDirectBufferPointer(void ** data,int * size)1121   bool GetDirectBufferPointer(void** data, int* size) {
1122     return impl_.GetDirectBufferPointer(data, size, &cur_);
1123   }
1124 
1125   // If there are at least "size" bytes available in the current buffer,
1126   // returns a pointer directly into the buffer and advances over these bytes.
1127   // The caller may then write directly into this buffer (e.g. using the
1128   // *ToArray static methods) rather than go through CodedOutputStream.  If
1129   // there are not enough bytes available, returns NULL.  The return pointer is
1130   // invalidated as soon as any other non-const method of CodedOutputStream
1131   // is called.
GetDirectBufferForNBytesAndAdvance(int size)1132   inline uint8_t* GetDirectBufferForNBytesAndAdvance(int size) {
1133     return impl_.GetDirectBufferForNBytesAndAdvance(size, &cur_);
1134   }
1135 
1136   // Write raw bytes, copying them from the given buffer.
WriteRaw(const void * buffer,int size)1137   void WriteRaw(const void* buffer, int size) {
1138     cur_ = impl_.WriteRaw(buffer, size, cur_);
1139   }
1140   // Like WriteRaw()  but will try to write aliased data if aliasing is
1141   // turned on.
1142   void WriteRawMaybeAliased(const void* data, int size);
1143   // Like WriteRaw()  but writing directly to the target array.
1144   // This is _not_ inlined, as the compiler often optimizes memcpy into inline
1145   // copy loops. Since this gets called by every field with string or bytes
1146   // type, inlining may lead to a significant amount of code bloat, with only a
1147   // minor performance gain.
1148   static uint8_t* WriteRawToArray(const void* buffer, int size,
1149                                   uint8_t* target);
1150 
1151   // Equivalent to WriteRaw(str.data(), str.size()).
1152   void WriteString(const std::string& str);
1153   // Like WriteString()  but writing directly to the target array.
1154   static uint8_t* WriteStringToArray(const std::string& str, uint8_t* target);
1155   // Write the varint-encoded size of str followed by str.
1156   static uint8_t* WriteStringWithSizeToArray(const std::string& str,
1157                                              uint8_t* target);
1158 
1159 
1160   // Write a 32-bit little-endian integer.
WriteLittleEndian32(uint32_t value)1161   void WriteLittleEndian32(uint32_t value) {
1162     cur_ = impl_.EnsureSpace(cur_);
1163     SetCur(WriteLittleEndian32ToArray(value, Cur()));
1164   }
1165   // Like WriteLittleEndian32()  but writing directly to the target array.
1166   static uint8_t* WriteLittleEndian32ToArray(uint32_t value, uint8_t* target);
1167   // Write a 64-bit little-endian integer.
WriteLittleEndian64(uint64_t value)1168   void WriteLittleEndian64(uint64_t value) {
1169     cur_ = impl_.EnsureSpace(cur_);
1170     SetCur(WriteLittleEndian64ToArray(value, Cur()));
1171   }
1172   // Like WriteLittleEndian64()  but writing directly to the target array.
1173   static uint8_t* WriteLittleEndian64ToArray(uint64_t value, uint8_t* target);
1174 
1175   // Write an unsigned integer with Varint encoding.  Writing a 32-bit value
1176   // is equivalent to casting it to uint64_t and writing it as a 64-bit value,
1177   // but may be more efficient.
1178   void WriteVarint32(uint32_t value);
1179   // Like WriteVarint32()  but writing directly to the target array.
1180   static uint8_t* WriteVarint32ToArray(uint32_t value, uint8_t* target);
1181   // Like WriteVarint32()  but writing directly to the target array, and with
1182   // the less common-case paths being out of line rather than inlined.
1183   static uint8_t* WriteVarint32ToArrayOutOfLine(uint32_t value,
1184                                                 uint8_t* target);
1185   // Write an unsigned integer with Varint encoding.
1186   void WriteVarint64(uint64_t value);
1187   // Like WriteVarint64()  but writing directly to the target array.
1188   static uint8_t* WriteVarint64ToArray(uint64_t value, uint8_t* target);
1189 
1190   // Equivalent to WriteVarint32() except when the value is negative,
1191   // in which case it must be sign-extended to a full 10 bytes.
1192   void WriteVarint32SignExtended(int32_t value);
1193   // Like WriteVarint32SignExtended()  but writing directly to the target array.
1194   static uint8_t* WriteVarint32SignExtendedToArray(int32_t value,
1195                                                    uint8_t* target);
1196 
1197   // This is identical to WriteVarint32(), but optimized for writing tags.
1198   // In particular, if the input is a compile-time constant, this method
1199   // compiles down to a couple instructions.
1200   // Always inline because otherwise the aforementioned optimization can't work,
1201   // but GCC by default doesn't want to inline this.
1202   void WriteTag(uint32_t value);
1203   // Like WriteTag()  but writing directly to the target array.
1204   PROTOBUF_ALWAYS_INLINE
1205   static uint8_t* WriteTagToArray(uint32_t value, uint8_t* target);
1206 
1207   // Returns the number of bytes needed to encode the given value as a varint.
1208   static size_t VarintSize32(uint32_t value);
1209   // Returns the number of bytes needed to encode the given value as a varint.
1210   static size_t VarintSize64(uint64_t value);
1211 
1212   // If negative, 10 bytes.  Otherwise, same as VarintSize32().
1213   static size_t VarintSize32SignExtended(int32_t value);
1214 
1215   // Same as above, plus one.  The additional one comes at no compute cost.
1216   static size_t VarintSize32PlusOne(uint32_t value);
1217   static size_t VarintSize64PlusOne(uint64_t value);
1218   static size_t VarintSize32SignExtendedPlusOne(int32_t value);
1219 
1220   // Compile-time equivalent of VarintSize32().
1221   template <uint32_t Value>
1222   struct StaticVarintSize32 {
1223     static const size_t value = (Value < (1 << 7))    ? 1
1224                                 : (Value < (1 << 14)) ? 2
1225                                 : (Value < (1 << 21)) ? 3
1226                                 : (Value < (1 << 28)) ? 4
1227                                                       : 5;
1228   };
1229 
1230   // Returns the total number of bytes written since this object was created.
ByteCount()1231   int ByteCount() const {
1232     return static_cast<int>(impl_.ByteCount(cur_) - start_count_);
1233   }
1234 
1235   // Instructs the CodedOutputStream to allow the underlying
1236   // ZeroCopyOutputStream to hold pointers to the original structure instead of
1237   // copying, if it supports it (i.e. output->AllowsAliasing() is true).  If the
1238   // underlying stream does not support aliasing, then enabling it has no
1239   // affect.  For now, this only affects the behavior of
1240   // WriteRawMaybeAliased().
1241   //
1242   // NOTE: It is caller's responsibility to ensure that the chunk of memory
1243   // remains live until all of the data has been consumed from the stream.
EnableAliasing(bool enabled)1244   void EnableAliasing(bool enabled) { impl_.EnableAliasing(enabled); }
1245 
1246   // Indicate to the serializer whether the user wants deterministic
1247   // serialization. The default when this is not called comes from the global
1248   // default, controlled by SetDefaultSerializationDeterministic.
1249   //
1250   // What deterministic serialization means is entirely up to the driver of the
1251   // serialization process (i.e. the caller of methods like WriteVarint32). In
1252   // the case of serializing a proto buffer message using one of the methods of
1253   // MessageLite, this means that for a given binary equal messages will always
1254   // be serialized to the same bytes. This implies:
1255   //
1256   //   * Repeated serialization of a message will return the same bytes.
1257   //
1258   //   * Different processes running the same binary (including on different
1259   //     machines) will serialize equal messages to the same bytes.
1260   //
1261   // Note that this is *not* canonical across languages. It is also unstable
1262   // across different builds with intervening message definition changes, due to
1263   // unknown fields. Users who need canonical serialization (e.g. persistent
1264   // storage in a canonical form, fingerprinting) should define their own
1265   // canonicalization specification and implement the serializer using
1266   // reflection APIs rather than relying on this API.
SetSerializationDeterministic(bool value)1267   void SetSerializationDeterministic(bool value) {
1268     impl_.SetSerializationDeterministic(value);
1269   }
1270 
1271   // Return whether the user wants deterministic serialization. See above.
IsSerializationDeterministic()1272   bool IsSerializationDeterministic() const {
1273     return impl_.IsSerializationDeterministic();
1274   }
1275 
IsDefaultSerializationDeterministic()1276   static bool IsDefaultSerializationDeterministic() {
1277     return default_serialization_deterministic_.load(
1278                std::memory_order_relaxed) != 0;
1279   }
1280 
1281   template <typename Func>
1282   void Serialize(const Func& func);
1283 
Cur()1284   uint8_t* Cur() const { return cur_; }
SetCur(uint8_t * ptr)1285   void SetCur(uint8_t* ptr) { cur_ = ptr; }
EpsCopy()1286   EpsCopyOutputStream* EpsCopy() { return &impl_; }
1287 
1288  private:
1289   template <class Stream>
1290   void InitEagerly(Stream* stream);
1291 
1292   EpsCopyOutputStream impl_;
1293   uint8_t* cur_;
1294   int64_t start_count_;
1295   static std::atomic<bool> default_serialization_deterministic_;
1296 
1297   // See above.  Other projects may use "friend" to allow them to call this.
1298   // After SetDefaultSerializationDeterministic() completes, all protocol
1299   // buffer serializations will be deterministic by default.  Thread safe.
1300   // However, the meaning of "after" is subtle here: to be safe, each thread
1301   // that wants deterministic serialization by default needs to call
1302   // SetDefaultSerializationDeterministic() or ensure on its own that another
1303   // thread has done so.
1304   friend void internal::MapTestForceDeterministic();
SetDefaultSerializationDeterministic()1305   static void SetDefaultSerializationDeterministic() {
1306     default_serialization_deterministic_.store(true, std::memory_order_relaxed);
1307   }
1308   // REQUIRES: value >= 0x80, and that (value & 7f) has been written to *target.
1309   static uint8_t* WriteVarint32ToArrayOutOfLineHelper(uint32_t value,
1310                                                       uint8_t* target);
1311   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream);
1312 };
1313 
1314 // inline methods ====================================================
1315 // The vast majority of varints are only one byte.  These inline
1316 // methods optimize for that case.
1317 
ReadVarint32(uint32_t * value)1318 inline bool CodedInputStream::ReadVarint32(uint32_t* value) {
1319   uint32_t v = 0;
1320   if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) {
1321     v = *buffer_;
1322     if (v < 0x80) {
1323       *value = v;
1324       Advance(1);
1325       return true;
1326     }
1327   }
1328   int64_t result = ReadVarint32Fallback(v);
1329   *value = static_cast<uint32_t>(result);
1330   return result >= 0;
1331 }
1332 
ReadVarint64(uint64_t * value)1333 inline bool CodedInputStream::ReadVarint64(uint64_t* value) {
1334   if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) {
1335     *value = *buffer_;
1336     Advance(1);
1337     return true;
1338   }
1339   std::pair<uint64_t, bool> p = ReadVarint64Fallback();
1340   *value = p.first;
1341   return p.second;
1342 }
1343 
ReadVarintSizeAsInt(int * value)1344 inline bool CodedInputStream::ReadVarintSizeAsInt(int* value) {
1345   if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) {
1346     int v = *buffer_;
1347     if (v < 0x80) {
1348       *value = v;
1349       Advance(1);
1350       return true;
1351     }
1352   }
1353   *value = ReadVarintSizeAsIntFallback();
1354   return *value >= 0;
1355 }
1356 
1357 // static
ReadLittleEndian32FromArray(const uint8_t * buffer,uint32_t * value)1358 inline const uint8_t* CodedInputStream::ReadLittleEndian32FromArray(
1359     const uint8_t* buffer, uint32_t* value) {
1360 #if defined(PROTOBUF_LITTLE_ENDIAN)
1361   memcpy(value, buffer, sizeof(*value));
1362   return buffer + sizeof(*value);
1363 #else
1364   *value = (static_cast<uint32_t>(buffer[0])) |
1365            (static_cast<uint32_t>(buffer[1]) << 8) |
1366            (static_cast<uint32_t>(buffer[2]) << 16) |
1367            (static_cast<uint32_t>(buffer[3]) << 24);
1368   return buffer + sizeof(*value);
1369 #endif
1370 }
1371 // static
ReadLittleEndian64FromArray(const uint8_t * buffer,uint64_t * value)1372 inline const uint8_t* CodedInputStream::ReadLittleEndian64FromArray(
1373     const uint8_t* buffer, uint64_t* value) {
1374 #if defined(PROTOBUF_LITTLE_ENDIAN)
1375   memcpy(value, buffer, sizeof(*value));
1376   return buffer + sizeof(*value);
1377 #else
1378   uint32_t part0 = (static_cast<uint32_t>(buffer[0])) |
1379                    (static_cast<uint32_t>(buffer[1]) << 8) |
1380                    (static_cast<uint32_t>(buffer[2]) << 16) |
1381                    (static_cast<uint32_t>(buffer[3]) << 24);
1382   uint32_t part1 = (static_cast<uint32_t>(buffer[4])) |
1383                    (static_cast<uint32_t>(buffer[5]) << 8) |
1384                    (static_cast<uint32_t>(buffer[6]) << 16) |
1385                    (static_cast<uint32_t>(buffer[7]) << 24);
1386   *value = static_cast<uint64_t>(part0) | (static_cast<uint64_t>(part1) << 32);
1387   return buffer + sizeof(*value);
1388 #endif
1389 }
1390 
ReadLittleEndian32(uint32_t * value)1391 inline bool CodedInputStream::ReadLittleEndian32(uint32_t* value) {
1392 #if defined(PROTOBUF_LITTLE_ENDIAN)
1393   if (PROTOBUF_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
1394     buffer_ = ReadLittleEndian32FromArray(buffer_, value);
1395     return true;
1396   } else {
1397     return ReadLittleEndian32Fallback(value);
1398   }
1399 #else
1400   return ReadLittleEndian32Fallback(value);
1401 #endif
1402 }
1403 
ReadLittleEndian64(uint64_t * value)1404 inline bool CodedInputStream::ReadLittleEndian64(uint64_t* value) {
1405 #if defined(PROTOBUF_LITTLE_ENDIAN)
1406   if (PROTOBUF_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
1407     buffer_ = ReadLittleEndian64FromArray(buffer_, value);
1408     return true;
1409   } else {
1410     return ReadLittleEndian64Fallback(value);
1411   }
1412 #else
1413   return ReadLittleEndian64Fallback(value);
1414 #endif
1415 }
1416 
ReadTagNoLastTag()1417 inline uint32_t CodedInputStream::ReadTagNoLastTag() {
1418   uint32_t v = 0;
1419   if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) {
1420     v = *buffer_;
1421     if (v < 0x80) {
1422       Advance(1);
1423       return v;
1424     }
1425   }
1426   v = ReadTagFallback(v);
1427   return v;
1428 }
1429 
ReadTagWithCutoffNoLastTag(uint32_t cutoff)1430 inline std::pair<uint32_t, bool> CodedInputStream::ReadTagWithCutoffNoLastTag(
1431     uint32_t cutoff) {
1432   // In performance-sensitive code we can expect cutoff to be a compile-time
1433   // constant, and things like "cutoff >= kMax1ByteVarint" to be evaluated at
1434   // compile time.
1435   uint32_t first_byte_or_zero = 0;
1436   if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) {
1437     // Hot case: buffer_ non_empty, buffer_[0] in [1, 128).
1438     // TODO(gpike): Is it worth rearranging this? E.g., if the number of fields
1439     // is large enough then is it better to check for the two-byte case first?
1440     first_byte_or_zero = buffer_[0];
1441     if (static_cast<int8_t>(buffer_[0]) > 0) {
1442       const uint32_t kMax1ByteVarint = 0x7f;
1443       uint32_t tag = buffer_[0];
1444       Advance(1);
1445       return std::make_pair(tag, cutoff >= kMax1ByteVarint || tag <= cutoff);
1446     }
1447     // Other hot case: cutoff >= 0x80, buffer_ has at least two bytes available,
1448     // and tag is two bytes.  The latter is tested by bitwise-and-not of the
1449     // first byte and the second byte.
1450     if (cutoff >= 0x80 && PROTOBUF_PREDICT_TRUE(buffer_ + 1 < buffer_end_) &&
1451         PROTOBUF_PREDICT_TRUE((buffer_[0] & ~buffer_[1]) >= 0x80)) {
1452       const uint32_t kMax2ByteVarint = (0x7f << 7) + 0x7f;
1453       uint32_t tag = (1u << 7) * buffer_[1] + (buffer_[0] - 0x80);
1454       Advance(2);
1455       // It might make sense to test for tag == 0 now, but it is so rare that
1456       // that we don't bother.  A varint-encoded 0 should be one byte unless
1457       // the encoder lost its mind.  The second part of the return value of
1458       // this function is allowed to be either true or false if the tag is 0,
1459       // so we don't have to check for tag == 0.  We may need to check whether
1460       // it exceeds cutoff.
1461       bool at_or_below_cutoff = cutoff >= kMax2ByteVarint || tag <= cutoff;
1462       return std::make_pair(tag, at_or_below_cutoff);
1463     }
1464   }
1465   // Slow path
1466   const uint32_t tag = ReadTagFallback(first_byte_or_zero);
1467   return std::make_pair(tag, static_cast<uint32_t>(tag - 1) < cutoff);
1468 }
1469 
LastTagWas(uint32_t expected)1470 inline bool CodedInputStream::LastTagWas(uint32_t expected) {
1471   return last_tag_ == expected;
1472 }
1473 
ConsumedEntireMessage()1474 inline bool CodedInputStream::ConsumedEntireMessage() {
1475   return legitimate_message_end_;
1476 }
1477 
ExpectTag(uint32_t expected)1478 inline bool CodedInputStream::ExpectTag(uint32_t expected) {
1479   if (expected < (1 << 7)) {
1480     if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_) &&
1481         buffer_[0] == expected) {
1482       Advance(1);
1483       return true;
1484     } else {
1485       return false;
1486     }
1487   } else if (expected < (1 << 14)) {
1488     if (PROTOBUF_PREDICT_TRUE(BufferSize() >= 2) &&
1489         buffer_[0] == static_cast<uint8_t>(expected | 0x80) &&
1490         buffer_[1] == static_cast<uint8_t>(expected >> 7)) {
1491       Advance(2);
1492       return true;
1493     } else {
1494       return false;
1495     }
1496   } else {
1497     // Don't bother optimizing for larger values.
1498     return false;
1499   }
1500 }
1501 
ExpectTagFromArray(const uint8_t * buffer,uint32_t expected)1502 inline const uint8_t* CodedInputStream::ExpectTagFromArray(
1503     const uint8_t* buffer, uint32_t expected) {
1504   if (expected < (1 << 7)) {
1505     if (buffer[0] == expected) {
1506       return buffer + 1;
1507     }
1508   } else if (expected < (1 << 14)) {
1509     if (buffer[0] == static_cast<uint8_t>(expected | 0x80) &&
1510         buffer[1] == static_cast<uint8_t>(expected >> 7)) {
1511       return buffer + 2;
1512     }
1513   }
1514   return nullptr;
1515 }
1516 
GetDirectBufferPointerInline(const void ** data,int * size)1517 inline void CodedInputStream::GetDirectBufferPointerInline(const void** data,
1518                                                            int* size) {
1519   *data = buffer_;
1520   *size = static_cast<int>(buffer_end_ - buffer_);
1521 }
1522 
ExpectAtEnd()1523 inline bool CodedInputStream::ExpectAtEnd() {
1524   // If we are at a limit we know no more bytes can be read.  Otherwise, it's
1525   // hard to say without calling Refresh(), and we'd rather not do that.
1526 
1527   if (buffer_ == buffer_end_ && ((buffer_size_after_limit_ != 0) ||
1528                                  (total_bytes_read_ == current_limit_))) {
1529     last_tag_ = 0;                   // Pretend we called ReadTag()...
1530     legitimate_message_end_ = true;  // ... and it hit EOF.
1531     return true;
1532   } else {
1533     return false;
1534   }
1535 }
1536 
CurrentPosition()1537 inline int CodedInputStream::CurrentPosition() const {
1538   return total_bytes_read_ - (BufferSize() + buffer_size_after_limit_);
1539 }
1540 
Advance(int amount)1541 inline void CodedInputStream::Advance(int amount) { buffer_ += amount; }
1542 
SetRecursionLimit(int limit)1543 inline void CodedInputStream::SetRecursionLimit(int limit) {
1544   recursion_budget_ += limit - recursion_limit_;
1545   recursion_limit_ = limit;
1546 }
1547 
IncrementRecursionDepth()1548 inline bool CodedInputStream::IncrementRecursionDepth() {
1549   --recursion_budget_;
1550   return recursion_budget_ >= 0;
1551 }
1552 
DecrementRecursionDepth()1553 inline void CodedInputStream::DecrementRecursionDepth() {
1554   if (recursion_budget_ < recursion_limit_) ++recursion_budget_;
1555 }
1556 
UnsafeDecrementRecursionDepth()1557 inline void CodedInputStream::UnsafeDecrementRecursionDepth() {
1558   assert(recursion_budget_ < recursion_limit_);
1559   ++recursion_budget_;
1560 }
1561 
SetExtensionRegistry(const DescriptorPool * pool,MessageFactory * factory)1562 inline void CodedInputStream::SetExtensionRegistry(const DescriptorPool* pool,
1563                                                    MessageFactory* factory) {
1564   extension_pool_ = pool;
1565   extension_factory_ = factory;
1566 }
1567 
GetExtensionPool()1568 inline const DescriptorPool* CodedInputStream::GetExtensionPool() {
1569   return extension_pool_;
1570 }
1571 
GetExtensionFactory()1572 inline MessageFactory* CodedInputStream::GetExtensionFactory() {
1573   return extension_factory_;
1574 }
1575 
BufferSize()1576 inline int CodedInputStream::BufferSize() const {
1577   return static_cast<int>(buffer_end_ - buffer_);
1578 }
1579 
CodedInputStream(ZeroCopyInputStream * input)1580 inline CodedInputStream::CodedInputStream(ZeroCopyInputStream* input)
1581     : buffer_(nullptr),
1582       buffer_end_(nullptr),
1583       input_(input),
1584       total_bytes_read_(0),
1585       overflow_bytes_(0),
1586       last_tag_(0),
1587       legitimate_message_end_(false),
1588       aliasing_enabled_(false),
1589       current_limit_(std::numeric_limits<int32_t>::max()),
1590       buffer_size_after_limit_(0),
1591       total_bytes_limit_(kDefaultTotalBytesLimit),
1592       recursion_budget_(default_recursion_limit_),
1593       recursion_limit_(default_recursion_limit_),
1594       extension_pool_(nullptr),
1595       extension_factory_(nullptr) {
1596   // Eagerly Refresh() so buffer space is immediately available.
1597   Refresh();
1598 }
1599 
CodedInputStream(const uint8_t * buffer,int size)1600 inline CodedInputStream::CodedInputStream(const uint8_t* buffer, int size)
1601     : buffer_(buffer),
1602       buffer_end_(buffer + size),
1603       input_(nullptr),
1604       total_bytes_read_(size),
1605       overflow_bytes_(0),
1606       last_tag_(0),
1607       legitimate_message_end_(false),
1608       aliasing_enabled_(false),
1609       current_limit_(size),
1610       buffer_size_after_limit_(0),
1611       total_bytes_limit_(kDefaultTotalBytesLimit),
1612       recursion_budget_(default_recursion_limit_),
1613       recursion_limit_(default_recursion_limit_),
1614       extension_pool_(nullptr),
1615       extension_factory_(nullptr) {
1616   // Note that setting current_limit_ == size is important to prevent some
1617   // code paths from trying to access input_ and segfaulting.
1618 }
1619 
IsFlat()1620 inline bool CodedInputStream::IsFlat() const { return input_ == nullptr; }
1621 
Skip(int count)1622 inline bool CodedInputStream::Skip(int count) {
1623   if (count < 0) return false;  // security: count is often user-supplied
1624 
1625   const int original_buffer_size = BufferSize();
1626 
1627   if (count <= original_buffer_size) {
1628     // Just skipping within the current buffer.  Easy.
1629     Advance(count);
1630     return true;
1631   }
1632 
1633   return SkipFallback(count, original_buffer_size);
1634 }
1635 
1636 template <class Stream, class>
CodedOutputStream(Stream * stream)1637 inline CodedOutputStream::CodedOutputStream(Stream* stream)
1638     : impl_(stream, IsDefaultSerializationDeterministic(), &cur_),
1639       start_count_(stream->ByteCount()) {
1640   InitEagerly(stream);
1641 }
1642 
1643 template <class Stream, class>
CodedOutputStream(Stream * stream,bool eager_init)1644 inline CodedOutputStream::CodedOutputStream(Stream* stream, bool eager_init)
1645     : impl_(stream, IsDefaultSerializationDeterministic(), &cur_),
1646       start_count_(stream->ByteCount()) {
1647   if (eager_init) {
1648     InitEagerly(stream);
1649   }
1650 }
1651 
1652 template <class Stream>
InitEagerly(Stream * stream)1653 inline void CodedOutputStream::InitEagerly(Stream* stream) {
1654   void* data;
1655   int size;
1656   if (PROTOBUF_PREDICT_TRUE(stream->Next(&data, &size) && size > 0)) {
1657     cur_ = impl_.SetInitialBuffer(data, size);
1658   }
1659 }
1660 
WriteVarint32ToArray(uint32_t value,uint8_t * target)1661 inline uint8_t* CodedOutputStream::WriteVarint32ToArray(uint32_t value,
1662                                                         uint8_t* target) {
1663   return EpsCopyOutputStream::UnsafeVarint(value, target);
1664 }
1665 
WriteVarint32ToArrayOutOfLine(uint32_t value,uint8_t * target)1666 inline uint8_t* CodedOutputStream::WriteVarint32ToArrayOutOfLine(
1667     uint32_t value, uint8_t* target) {
1668   target[0] = static_cast<uint8_t>(value);
1669   if (value < 0x80) {
1670     return target + 1;
1671   } else {
1672     return WriteVarint32ToArrayOutOfLineHelper(value, target);
1673   }
1674 }
1675 
WriteVarint64ToArray(uint64_t value,uint8_t * target)1676 inline uint8_t* CodedOutputStream::WriteVarint64ToArray(uint64_t value,
1677                                                         uint8_t* target) {
1678   return EpsCopyOutputStream::UnsafeVarint(value, target);
1679 }
1680 
WriteVarint32SignExtended(int32_t value)1681 inline void CodedOutputStream::WriteVarint32SignExtended(int32_t value) {
1682   WriteVarint64(static_cast<uint64_t>(value));
1683 }
1684 
WriteVarint32SignExtendedToArray(int32_t value,uint8_t * target)1685 inline uint8_t* CodedOutputStream::WriteVarint32SignExtendedToArray(
1686     int32_t value, uint8_t* target) {
1687   return WriteVarint64ToArray(static_cast<uint64_t>(value), target);
1688 }
1689 
WriteLittleEndian32ToArray(uint32_t value,uint8_t * target)1690 inline uint8_t* CodedOutputStream::WriteLittleEndian32ToArray(uint32_t value,
1691                                                               uint8_t* target) {
1692 #if defined(PROTOBUF_LITTLE_ENDIAN)
1693   memcpy(target, &value, sizeof(value));
1694 #else
1695   target[0] = static_cast<uint8_t>(value);
1696   target[1] = static_cast<uint8_t>(value >> 8);
1697   target[2] = static_cast<uint8_t>(value >> 16);
1698   target[3] = static_cast<uint8_t>(value >> 24);
1699 #endif
1700   return target + sizeof(value);
1701 }
1702 
WriteLittleEndian64ToArray(uint64_t value,uint8_t * target)1703 inline uint8_t* CodedOutputStream::WriteLittleEndian64ToArray(uint64_t value,
1704                                                               uint8_t* target) {
1705 #if defined(PROTOBUF_LITTLE_ENDIAN)
1706   memcpy(target, &value, sizeof(value));
1707 #else
1708   uint32_t part0 = static_cast<uint32_t>(value);
1709   uint32_t part1 = static_cast<uint32_t>(value >> 32);
1710 
1711   target[0] = static_cast<uint8_t>(part0);
1712   target[1] = static_cast<uint8_t>(part0 >> 8);
1713   target[2] = static_cast<uint8_t>(part0 >> 16);
1714   target[3] = static_cast<uint8_t>(part0 >> 24);
1715   target[4] = static_cast<uint8_t>(part1);
1716   target[5] = static_cast<uint8_t>(part1 >> 8);
1717   target[6] = static_cast<uint8_t>(part1 >> 16);
1718   target[7] = static_cast<uint8_t>(part1 >> 24);
1719 #endif
1720   return target + sizeof(value);
1721 }
1722 
WriteVarint32(uint32_t value)1723 inline void CodedOutputStream::WriteVarint32(uint32_t value) {
1724   cur_ = impl_.EnsureSpace(cur_);
1725   SetCur(WriteVarint32ToArray(value, Cur()));
1726 }
1727 
WriteVarint64(uint64_t value)1728 inline void CodedOutputStream::WriteVarint64(uint64_t value) {
1729   cur_ = impl_.EnsureSpace(cur_);
1730   SetCur(WriteVarint64ToArray(value, Cur()));
1731 }
1732 
WriteTag(uint32_t value)1733 inline void CodedOutputStream::WriteTag(uint32_t value) {
1734   WriteVarint32(value);
1735 }
1736 
WriteTagToArray(uint32_t value,uint8_t * target)1737 inline uint8_t* CodedOutputStream::WriteTagToArray(uint32_t value,
1738                                                    uint8_t* target) {
1739   return WriteVarint32ToArray(value, target);
1740 }
1741 
VarintSize32(uint32_t value)1742 inline size_t CodedOutputStream::VarintSize32(uint32_t value) {
1743   // This computes value == 0 ? 1 : floor(log2(value)) / 7 + 1
1744   // Use an explicit multiplication to implement the divide of
1745   // a number in the 1..31 range.
1746   // Explicit OR 0x1 to avoid calling Bits::Log2FloorNonZero(0), which is
1747   // undefined.
1748   uint32_t log2value = Bits::Log2FloorNonZero(value | 0x1);
1749   return static_cast<size_t>((log2value * 9 + 73) / 64);
1750 }
1751 
VarintSize32PlusOne(uint32_t value)1752 inline size_t CodedOutputStream::VarintSize32PlusOne(uint32_t value) {
1753   // Same as above, but one more.
1754   uint32_t log2value = Bits::Log2FloorNonZero(value | 0x1);
1755   return static_cast<size_t>((log2value * 9 + 73 + 64) / 64);
1756 }
1757 
VarintSize64(uint64_t value)1758 inline size_t CodedOutputStream::VarintSize64(uint64_t value) {
1759   // This computes value == 0 ? 1 : floor(log2(value)) / 7 + 1
1760   // Use an explicit multiplication to implement the divide of
1761   // a number in the 1..63 range.
1762   // Explicit OR 0x1 to avoid calling Bits::Log2FloorNonZero(0), which is
1763   // undefined.
1764   uint32_t log2value = Bits::Log2FloorNonZero64(value | 0x1);
1765   return static_cast<size_t>((log2value * 9 + 73) / 64);
1766 }
1767 
VarintSize64PlusOne(uint64_t value)1768 inline size_t CodedOutputStream::VarintSize64PlusOne(uint64_t value) {
1769   // Same as above, but one more.
1770   uint32_t log2value = Bits::Log2FloorNonZero64(value | 0x1);
1771   return static_cast<size_t>((log2value * 9 + 73 + 64) / 64);
1772 }
1773 
VarintSize32SignExtended(int32_t value)1774 inline size_t CodedOutputStream::VarintSize32SignExtended(int32_t value) {
1775   return VarintSize64(static_cast<uint64_t>(int64_t{value}));
1776 }
1777 
VarintSize32SignExtendedPlusOne(int32_t value)1778 inline size_t CodedOutputStream::VarintSize32SignExtendedPlusOne(
1779     int32_t value) {
1780   return VarintSize64PlusOne(static_cast<uint64_t>(int64_t{value}));
1781 }
1782 
WriteString(const std::string & str)1783 inline void CodedOutputStream::WriteString(const std::string& str) {
1784   WriteRaw(str.data(), static_cast<int>(str.size()));
1785 }
1786 
WriteRawMaybeAliased(const void * data,int size)1787 inline void CodedOutputStream::WriteRawMaybeAliased(const void* data,
1788                                                     int size) {
1789   cur_ = impl_.WriteRawMaybeAliased(data, size, cur_);
1790 }
1791 
WriteRawToArray(const void * data,int size,uint8_t * target)1792 inline uint8_t* CodedOutputStream::WriteRawToArray(const void* data, int size,
1793                                                    uint8_t* target) {
1794   memcpy(target, data, size);
1795   return target + size;
1796 }
1797 
WriteStringToArray(const std::string & str,uint8_t * target)1798 inline uint8_t* CodedOutputStream::WriteStringToArray(const std::string& str,
1799                                                       uint8_t* target) {
1800   return WriteRawToArray(str.data(), static_cast<int>(str.size()), target);
1801 }
1802 
1803 }  // namespace io
1804 }  // namespace protobuf
1805 }  // namespace google
1806 
1807 #if defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
1808 #pragma runtime_checks("c", restore)
1809 #endif  // _MSC_VER && !defined(__INTEL_COMPILER)
1810 
1811 #include <google/protobuf/port_undef.inc>
1812 
1813 #endif  // GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
1814