• 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 //         atenasio@google.com (Chris Atenasio) (ZigZag transform)
33 //         wink@google.com (Wink Saville) (refactored from wire_format.h)
34 //  Based on original Protocol Buffers design by
35 //  Sanjay Ghemawat, Jeff Dean, and others.
36 //
37 // This header is logically internal, but is made public because it is used
38 // from protocol-compiler-generated code, which may reside in other components.
39 
40 #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__
41 #define GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__
42 
43 #include <string>
44 
45 #include <google/protobuf/stubs/common.h>
46 #include <google/protobuf/stubs/logging.h>
47 #include <google/protobuf/io/coded_stream.h>
48 #include <google/protobuf/arenastring.h>
49 #include <google/protobuf/message_lite.h>
50 #include <google/protobuf/port.h>
51 #include <google/protobuf/repeated_field.h>
52 
53 // Do UTF-8 validation on string type in Debug build only
54 #ifndef NDEBUG
55 #define GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
56 #endif
57 
58 // Avoid conflict with iOS where <ConditionalMacros.h> #defines TYPE_BOOL.
59 //
60 // If some one needs the macro TYPE_BOOL in a file that includes this header,
61 // it's possible to bring it back using push/pop_macro as follows.
62 //
63 // #pragma push_macro("TYPE_BOOL")
64 // #include this header and/or all headers that need the macro to be undefined.
65 // #pragma pop_macro("TYPE_BOOL")
66 #undef TYPE_BOOL
67 
68 
69 namespace google {
70 namespace protobuf {
71 namespace internal {
72 
73 #include <google/protobuf/port_def.inc>
74 
75 // This class is for internal use by the protocol buffer library and by
76 // protocol-complier-generated message classes.  It must not be called
77 // directly by clients.
78 //
79 // This class contains helpers for implementing the binary protocol buffer
80 // wire format without the need for reflection. Use WireFormat when using
81 // reflection.
82 //
83 // This class is really a namespace that contains only static methods.
84 class PROTOBUF_EXPORT WireFormatLite {
85  public:
86   // -----------------------------------------------------------------
87   // Helper constants and functions related to the format.  These are
88   // mostly meant for internal and generated code to use.
89 
90   // The wire format is composed of a sequence of tag/value pairs, each
91   // of which contains the value of one field (or one element of a repeated
92   // field).  Each tag is encoded as a varint.  The lower bits of the tag
93   // identify its wire type, which specifies the format of the data to follow.
94   // The rest of the bits contain the field number.  Each type of field (as
95   // declared by FieldDescriptor::Type, in descriptor.h) maps to one of
96   // these wire types.  Immediately following each tag is the field's value,
97   // encoded in the format specified by the wire type.  Because the tag
98   // identifies the encoding of this data, it is possible to skip
99   // unrecognized fields for forwards compatibility.
100 
101   enum WireType {
102     WIRETYPE_VARINT = 0,
103     WIRETYPE_FIXED64 = 1,
104     WIRETYPE_LENGTH_DELIMITED = 2,
105     WIRETYPE_START_GROUP = 3,
106     WIRETYPE_END_GROUP = 4,
107     WIRETYPE_FIXED32 = 5,
108   };
109 
110   // Lite alternative to FieldDescriptor::Type.  Must be kept in sync.
111   enum FieldType {
112     TYPE_DOUBLE = 1,
113     TYPE_FLOAT = 2,
114     TYPE_INT64 = 3,
115     TYPE_UINT64 = 4,
116     TYPE_INT32 = 5,
117     TYPE_FIXED64 = 6,
118     TYPE_FIXED32 = 7,
119     TYPE_BOOL = 8,
120     TYPE_STRING = 9,
121     TYPE_GROUP = 10,
122     TYPE_MESSAGE = 11,
123     TYPE_BYTES = 12,
124     TYPE_UINT32 = 13,
125     TYPE_ENUM = 14,
126     TYPE_SFIXED32 = 15,
127     TYPE_SFIXED64 = 16,
128     TYPE_SINT32 = 17,
129     TYPE_SINT64 = 18,
130     MAX_FIELD_TYPE = 18,
131   };
132 
133   // Lite alternative to FieldDescriptor::CppType.  Must be kept in sync.
134   enum CppType {
135     CPPTYPE_INT32 = 1,
136     CPPTYPE_INT64 = 2,
137     CPPTYPE_UINT32 = 3,
138     CPPTYPE_UINT64 = 4,
139     CPPTYPE_DOUBLE = 5,
140     CPPTYPE_FLOAT = 6,
141     CPPTYPE_BOOL = 7,
142     CPPTYPE_ENUM = 8,
143     CPPTYPE_STRING = 9,
144     CPPTYPE_MESSAGE = 10,
145     MAX_CPPTYPE = 10,
146   };
147 
148   // Helper method to get the CppType for a particular Type.
149   static CppType FieldTypeToCppType(FieldType type);
150 
151   // Given a FieldDescriptor::Type return its WireType
WireTypeForFieldType(WireFormatLite::FieldType type)152   static inline WireFormatLite::WireType WireTypeForFieldType(
153       WireFormatLite::FieldType type) {
154     return kWireTypeForFieldType[type];
155   }
156 
157   // Number of bits in a tag which identify the wire type.
158   static const int kTagTypeBits = 3;
159   // Mask for those bits.
160   static const uint32 kTagTypeMask = (1 << kTagTypeBits) - 1;
161 
162   // Helper functions for encoding and decoding tags.  (Inlined below and in
163   // _inl.h)
164   //
165   // This is different from MakeTag(field->number(), field->type()) in the
166   // case of packed repeated fields.
167   constexpr static uint32 MakeTag(int field_number, WireType type);
168   static WireType GetTagWireType(uint32 tag);
169   static int GetTagFieldNumber(uint32 tag);
170 
171   // Compute the byte size of a tag.  For groups, this includes both the start
172   // and end tags.
173   static inline size_t TagSize(int field_number,
174                                WireFormatLite::FieldType type);
175 
176   // Skips a field value with the given tag.  The input should start
177   // positioned immediately after the tag.  Skipped values are simply
178   // discarded, not recorded anywhere.  See WireFormat::SkipField() for a
179   // version that records to an UnknownFieldSet.
180   static bool SkipField(io::CodedInputStream* input, uint32 tag);
181 
182   // Skips a field value with the given tag.  The input should start
183   // positioned immediately after the tag. Skipped values are recorded to a
184   // CodedOutputStream.
185   static bool SkipField(io::CodedInputStream* input, uint32 tag,
186                         io::CodedOutputStream* output);
187 
188   // Reads and ignores a message from the input.  Skipped values are simply
189   // discarded, not recorded anywhere.  See WireFormat::SkipMessage() for a
190   // version that records to an UnknownFieldSet.
191   static bool SkipMessage(io::CodedInputStream* input);
192 
193   // Reads and ignores a message from the input.  Skipped values are recorded
194   // to a CodedOutputStream.
195   static bool SkipMessage(io::CodedInputStream* input,
196                           io::CodedOutputStream* output);
197 
198   // This macro does the same thing as WireFormatLite::MakeTag(), but the
199   // result is usable as a compile-time constant, which makes it usable
200   // as a switch case or a template input.  WireFormatLite::MakeTag() is more
201   // type-safe, though, so prefer it if possible.
202 #define GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(FIELD_NUMBER, TYPE) \
203   static_cast<uint32>((static_cast<uint32>(FIELD_NUMBER) << 3) | (TYPE))
204 
205   // These are the tags for the old MessageSet format, which was defined as:
206   //   message MessageSet {
207   //     repeated group Item = 1 {
208   //       required int32 type_id = 2;
209   //       required string message = 3;
210   //     }
211   //   }
212   static const int kMessageSetItemNumber = 1;
213   static const int kMessageSetTypeIdNumber = 2;
214   static const int kMessageSetMessageNumber = 3;
215   static const int kMessageSetItemStartTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(
216       kMessageSetItemNumber, WireFormatLite::WIRETYPE_START_GROUP);
217   static const int kMessageSetItemEndTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(
218       kMessageSetItemNumber, WireFormatLite::WIRETYPE_END_GROUP);
219   static const int kMessageSetTypeIdTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(
220       kMessageSetTypeIdNumber, WireFormatLite::WIRETYPE_VARINT);
221   static const int kMessageSetMessageTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(
222       kMessageSetMessageNumber, WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
223 
224   // Byte size of all tags of a MessageSet::Item combined.
225   static const size_t kMessageSetItemTagsSize;
226 
227   // Helper functions for converting between floats/doubles and IEEE-754
228   // uint32s/uint64s so that they can be written.  (Assumes your platform
229   // uses IEEE-754 floats.)
230   static uint32 EncodeFloat(float value);
231   static float DecodeFloat(uint32 value);
232   static uint64 EncodeDouble(double value);
233   static double DecodeDouble(uint64 value);
234 
235   // Helper functions for mapping signed integers to unsigned integers in
236   // such a way that numbers with small magnitudes will encode to smaller
237   // varints.  If you simply static_cast a negative number to an unsigned
238   // number and varint-encode it, it will always take 10 bytes, defeating
239   // the purpose of varint.  So, for the "sint32" and "sint64" field types,
240   // we ZigZag-encode the values.
241   static uint32 ZigZagEncode32(int32 n);
242   static int32 ZigZagDecode32(uint32 n);
243   static uint64 ZigZagEncode64(int64 n);
244   static int64 ZigZagDecode64(uint64 n);
245 
246   // =================================================================
247   // Methods for reading/writing individual field.
248 
249   // Read fields, not including tags.  The assumption is that you already
250   // read the tag to determine what field to read.
251 
252   // For primitive fields, we just use a templatized routine parameterized by
253   // the represented type and the FieldType. These are specialized with the
254   // appropriate definition for each declared type.
255   template <typename CType, enum FieldType DeclaredType>
256   PROTOBUF_ALWAYS_INLINE static bool ReadPrimitive(io::CodedInputStream* input,
257                                                    CType* value);
258 
259   // Reads repeated primitive values, with optimizations for repeats.
260   // tag_size and tag should both be compile-time constants provided by the
261   // protocol compiler.
262   template <typename CType, enum FieldType DeclaredType>
263   PROTOBUF_ALWAYS_INLINE static bool ReadRepeatedPrimitive(
264       int tag_size, uint32 tag, io::CodedInputStream* input,
265       RepeatedField<CType>* value);
266 
267   // Identical to ReadRepeatedPrimitive, except will not inline the
268   // implementation.
269   template <typename CType, enum FieldType DeclaredType>
270   static bool ReadRepeatedPrimitiveNoInline(int tag_size, uint32 tag,
271                                             io::CodedInputStream* input,
272                                             RepeatedField<CType>* value);
273 
274   // Reads a primitive value directly from the provided buffer. It returns a
275   // pointer past the segment of data that was read.
276   //
277   // This is only implemented for the types with fixed wire size, e.g.
278   // float, double, and the (s)fixed* types.
279   template <typename CType, enum FieldType DeclaredType>
280   PROTOBUF_ALWAYS_INLINE static const uint8* ReadPrimitiveFromArray(
281       const uint8* buffer, CType* value);
282 
283   // Reads a primitive packed field.
284   //
285   // This is only implemented for packable types.
286   template <typename CType, enum FieldType DeclaredType>
287   PROTOBUF_ALWAYS_INLINE static bool ReadPackedPrimitive(
288       io::CodedInputStream* input, RepeatedField<CType>* value);
289 
290   // Identical to ReadPackedPrimitive, except will not inline the
291   // implementation.
292   template <typename CType, enum FieldType DeclaredType>
293   static bool ReadPackedPrimitiveNoInline(io::CodedInputStream* input,
294                                           RepeatedField<CType>* value);
295 
296   // Read a packed enum field. If the is_valid function is not NULL, values for
297   // which is_valid(value) returns false are silently dropped.
298   static bool ReadPackedEnumNoInline(io::CodedInputStream* input,
299                                      bool (*is_valid)(int),
300                                      RepeatedField<int>* values);
301 
302   // Read a packed enum field. If the is_valid function is not NULL, values for
303   // which is_valid(value) returns false are appended to unknown_fields_stream.
304   static bool ReadPackedEnumPreserveUnknowns(
305       io::CodedInputStream* input, int field_number, bool (*is_valid)(int),
306       io::CodedOutputStream* unknown_fields_stream, RepeatedField<int>* values);
307 
308   // Read a string.  ReadString(..., std::string* value) requires an
309   // existing std::string.
310   static inline bool ReadString(io::CodedInputStream* input,
311                                 std::string* value);
312   // ReadString(..., std::string** p) is internal-only, and should only be
313   // called from generated code. It starts by setting *p to "new std::string" if
314   // *p == &GetEmptyStringAlreadyInited().  It then invokes
315   // ReadString(io::CodedInputStream* input, *p).  This is useful for reducing
316   // code size.
317   static inline bool ReadString(io::CodedInputStream* input, std::string** p);
318   // Analogous to ReadString().
319   static bool ReadBytes(io::CodedInputStream* input, std::string* value);
320   static bool ReadBytes(io::CodedInputStream* input, std::string** p);
321 
322   enum Operation {
323     PARSE = 0,
324     SERIALIZE = 1,
325   };
326 
327   // Returns true if the data is valid UTF-8.
328   static bool VerifyUtf8String(const char* data, int size, Operation op,
329                                const char* field_name);
330 
331   template <typename MessageType>
332   static inline bool ReadGroup(int field_number, io::CodedInputStream* input,
333                                MessageType* value);
334 
335   template <typename MessageType>
336   static inline bool ReadMessage(io::CodedInputStream* input,
337                                  MessageType* value);
338 
339   template <typename MessageType>
ReadMessageNoVirtual(io::CodedInputStream * input,MessageType * value)340   static inline bool ReadMessageNoVirtual(io::CodedInputStream* input,
341                                           MessageType* value) {
342     return ReadMessage(input, value);
343   }
344 
345   // Write a tag.  The Write*() functions typically include the tag, so
346   // normally there's no need to call this unless using the Write*NoTag()
347   // variants.
348   PROTOBUF_ALWAYS_INLINE static void WriteTag(int field_number, WireType type,
349                                               io::CodedOutputStream* output);
350 
351   // Write fields, without tags.
352   PROTOBUF_ALWAYS_INLINE static void WriteInt32NoTag(
353       int32 value, io::CodedOutputStream* output);
354   PROTOBUF_ALWAYS_INLINE static void WriteInt64NoTag(
355       int64 value, io::CodedOutputStream* output);
356   PROTOBUF_ALWAYS_INLINE static void WriteUInt32NoTag(
357       uint32 value, io::CodedOutputStream* output);
358   PROTOBUF_ALWAYS_INLINE static void WriteUInt64NoTag(
359       uint64 value, io::CodedOutputStream* output);
360   PROTOBUF_ALWAYS_INLINE static void WriteSInt32NoTag(
361       int32 value, io::CodedOutputStream* output);
362   PROTOBUF_ALWAYS_INLINE static void WriteSInt64NoTag(
363       int64 value, io::CodedOutputStream* output);
364   PROTOBUF_ALWAYS_INLINE static void WriteFixed32NoTag(
365       uint32 value, io::CodedOutputStream* output);
366   PROTOBUF_ALWAYS_INLINE static void WriteFixed64NoTag(
367       uint64 value, io::CodedOutputStream* output);
368   PROTOBUF_ALWAYS_INLINE static void WriteSFixed32NoTag(
369       int32 value, io::CodedOutputStream* output);
370   PROTOBUF_ALWAYS_INLINE static void WriteSFixed64NoTag(
371       int64 value, io::CodedOutputStream* output);
372   PROTOBUF_ALWAYS_INLINE static void WriteFloatNoTag(
373       float value, io::CodedOutputStream* output);
374   PROTOBUF_ALWAYS_INLINE static void WriteDoubleNoTag(
375       double value, io::CodedOutputStream* output);
376   PROTOBUF_ALWAYS_INLINE static void WriteBoolNoTag(
377       bool value, io::CodedOutputStream* output);
378   PROTOBUF_ALWAYS_INLINE static void WriteEnumNoTag(
379       int value, io::CodedOutputStream* output);
380 
381   // Write array of primitive fields, without tags
382   static void WriteFloatArray(const float* a, int n,
383                               io::CodedOutputStream* output);
384   static void WriteDoubleArray(const double* a, int n,
385                                io::CodedOutputStream* output);
386   static void WriteFixed32Array(const uint32* a, int n,
387                                 io::CodedOutputStream* output);
388   static void WriteFixed64Array(const uint64* a, int n,
389                                 io::CodedOutputStream* output);
390   static void WriteSFixed32Array(const int32* a, int n,
391                                  io::CodedOutputStream* output);
392   static void WriteSFixed64Array(const int64* a, int n,
393                                  io::CodedOutputStream* output);
394   static void WriteBoolArray(const bool* a, int n,
395                              io::CodedOutputStream* output);
396 
397   // Write fields, including tags.
398   static void WriteInt32(int field_number, int32 value,
399                          io::CodedOutputStream* output);
400   static void WriteInt64(int field_number, int64 value,
401                          io::CodedOutputStream* output);
402   static void WriteUInt32(int field_number, uint32 value,
403                           io::CodedOutputStream* output);
404   static void WriteUInt64(int field_number, uint64 value,
405                           io::CodedOutputStream* output);
406   static void WriteSInt32(int field_number, int32 value,
407                           io::CodedOutputStream* output);
408   static void WriteSInt64(int field_number, int64 value,
409                           io::CodedOutputStream* output);
410   static void WriteFixed32(int field_number, uint32 value,
411                            io::CodedOutputStream* output);
412   static void WriteFixed64(int field_number, uint64 value,
413                            io::CodedOutputStream* output);
414   static void WriteSFixed32(int field_number, int32 value,
415                             io::CodedOutputStream* output);
416   static void WriteSFixed64(int field_number, int64 value,
417                             io::CodedOutputStream* output);
418   static void WriteFloat(int field_number, float value,
419                          io::CodedOutputStream* output);
420   static void WriteDouble(int field_number, double value,
421                           io::CodedOutputStream* output);
422   static void WriteBool(int field_number, bool value,
423                         io::CodedOutputStream* output);
424   static void WriteEnum(int field_number, int value,
425                         io::CodedOutputStream* output);
426 
427   static void WriteString(int field_number, const std::string& value,
428                           io::CodedOutputStream* output);
429   static void WriteBytes(int field_number, const std::string& value,
430                          io::CodedOutputStream* output);
431   static void WriteStringMaybeAliased(int field_number,
432                                       const std::string& value,
433                                       io::CodedOutputStream* output);
434   static void WriteBytesMaybeAliased(int field_number, const std::string& value,
435                                      io::CodedOutputStream* output);
436 
437   static void WriteGroup(int field_number, const MessageLite& value,
438                          io::CodedOutputStream* output);
439   static void WriteMessage(int field_number, const MessageLite& value,
440                            io::CodedOutputStream* output);
441   // Like above, but these will check if the output stream has enough
442   // space to write directly to a flat array.
443   static void WriteGroupMaybeToArray(int field_number, const MessageLite& value,
444                                      io::CodedOutputStream* output);
445   static void WriteMessageMaybeToArray(int field_number,
446                                        const MessageLite& value,
447                                        io::CodedOutputStream* output);
448 
449   // Like above, but de-virtualize the call to SerializeWithCachedSizes().  The
450   // pointer must point at an instance of MessageType, *not* a subclass (or
451   // the subclass must not override SerializeWithCachedSizes()).
452   template <typename MessageType>
453   static inline void WriteGroupNoVirtual(int field_number,
454                                          const MessageType& value,
455                                          io::CodedOutputStream* output);
456   template <typename MessageType>
457   static inline void WriteMessageNoVirtual(int field_number,
458                                            const MessageType& value,
459                                            io::CodedOutputStream* output);
460 
461   // Like above, but use only *ToArray methods of CodedOutputStream.
462   PROTOBUF_ALWAYS_INLINE static uint8* WriteTagToArray(int field_number,
463                                                        WireType type,
464                                                        uint8* target);
465 
466   // Write fields, without tags.
467   PROTOBUF_ALWAYS_INLINE static uint8* WriteInt32NoTagToArray(int32 value,
468                                                               uint8* target);
469   PROTOBUF_ALWAYS_INLINE static uint8* WriteInt64NoTagToArray(int64 value,
470                                                               uint8* target);
471   PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt32NoTagToArray(uint32 value,
472                                                                uint8* target);
473   PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt64NoTagToArray(uint64 value,
474                                                                uint8* target);
475   PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt32NoTagToArray(int32 value,
476                                                                uint8* target);
477   PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt64NoTagToArray(int64 value,
478                                                                uint8* target);
479   PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed32NoTagToArray(uint32 value,
480                                                                 uint8* target);
481   PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed64NoTagToArray(uint64 value,
482                                                                 uint8* target);
483   PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed32NoTagToArray(int32 value,
484                                                                  uint8* target);
485   PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed64NoTagToArray(int64 value,
486                                                                  uint8* target);
487   PROTOBUF_ALWAYS_INLINE static uint8* WriteFloatNoTagToArray(float value,
488                                                               uint8* target);
489   PROTOBUF_ALWAYS_INLINE static uint8* WriteDoubleNoTagToArray(double value,
490                                                                uint8* target);
491   PROTOBUF_ALWAYS_INLINE static uint8* WriteBoolNoTagToArray(bool value,
492                                                              uint8* target);
493   PROTOBUF_ALWAYS_INLINE static uint8* WriteEnumNoTagToArray(int value,
494                                                              uint8* target);
495 
496   // Write fields, without tags.  These require that value.size() > 0.
497   template <typename T>
498   PROTOBUF_ALWAYS_INLINE static uint8* WritePrimitiveNoTagToArray(
499       const RepeatedField<T>& value, uint8* (*Writer)(T, uint8*),
500       uint8* target);
501   template <typename T>
502   PROTOBUF_ALWAYS_INLINE static uint8* WriteFixedNoTagToArray(
503       const RepeatedField<T>& value, uint8* (*Writer)(T, uint8*),
504       uint8* target);
505 
506   PROTOBUF_ALWAYS_INLINE static uint8* WriteInt32NoTagToArray(
507       const RepeatedField<int32>& value, uint8* output);
508   PROTOBUF_ALWAYS_INLINE static uint8* WriteInt64NoTagToArray(
509       const RepeatedField<int64>& value, uint8* output);
510   PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt32NoTagToArray(
511       const RepeatedField<uint32>& value, uint8* output);
512   PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt64NoTagToArray(
513       const RepeatedField<uint64>& value, uint8* output);
514   PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt32NoTagToArray(
515       const RepeatedField<int32>& value, uint8* output);
516   PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt64NoTagToArray(
517       const RepeatedField<int64>& value, uint8* output);
518   PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed32NoTagToArray(
519       const RepeatedField<uint32>& value, uint8* output);
520   PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed64NoTagToArray(
521       const RepeatedField<uint64>& value, uint8* output);
522   PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed32NoTagToArray(
523       const RepeatedField<int32>& value, uint8* output);
524   PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed64NoTagToArray(
525       const RepeatedField<int64>& value, uint8* output);
526   PROTOBUF_ALWAYS_INLINE static uint8* WriteFloatNoTagToArray(
527       const RepeatedField<float>& value, uint8* output);
528   PROTOBUF_ALWAYS_INLINE static uint8* WriteDoubleNoTagToArray(
529       const RepeatedField<double>& value, uint8* output);
530   PROTOBUF_ALWAYS_INLINE static uint8* WriteBoolNoTagToArray(
531       const RepeatedField<bool>& value, uint8* output);
532   PROTOBUF_ALWAYS_INLINE static uint8* WriteEnumNoTagToArray(
533       const RepeatedField<int>& value, uint8* output);
534 
535   // Write fields, including tags.
536   PROTOBUF_ALWAYS_INLINE static uint8* WriteInt32ToArray(int field_number,
537                                                          int32 value,
538                                                          uint8* target);
539   PROTOBUF_ALWAYS_INLINE static uint8* WriteInt64ToArray(int field_number,
540                                                          int64 value,
541                                                          uint8* target);
542   PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt32ToArray(int field_number,
543                                                           uint32 value,
544                                                           uint8* target);
545   PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt64ToArray(int field_number,
546                                                           uint64 value,
547                                                           uint8* target);
548   PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt32ToArray(int field_number,
549                                                           int32 value,
550                                                           uint8* target);
551   PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt64ToArray(int field_number,
552                                                           int64 value,
553                                                           uint8* target);
554   PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed32ToArray(int field_number,
555                                                            uint32 value,
556                                                            uint8* target);
557   PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed64ToArray(int field_number,
558                                                            uint64 value,
559                                                            uint8* target);
560   PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed32ToArray(int field_number,
561                                                             int32 value,
562                                                             uint8* target);
563   PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed64ToArray(int field_number,
564                                                             int64 value,
565                                                             uint8* target);
566   PROTOBUF_ALWAYS_INLINE static uint8* WriteFloatToArray(int field_number,
567                                                          float value,
568                                                          uint8* target);
569   PROTOBUF_ALWAYS_INLINE static uint8* WriteDoubleToArray(int field_number,
570                                                           double value,
571                                                           uint8* target);
572   PROTOBUF_ALWAYS_INLINE static uint8* WriteBoolToArray(int field_number,
573                                                         bool value,
574                                                         uint8* target);
575   PROTOBUF_ALWAYS_INLINE static uint8* WriteEnumToArray(int field_number,
576                                                         int value,
577                                                         uint8* target);
578 
579   template <typename T>
580   PROTOBUF_ALWAYS_INLINE static uint8* WritePrimitiveToArray(
581       int field_number, const RepeatedField<T>& value,
582       uint8* (*Writer)(int, T, uint8*), uint8* target);
583 
584   PROTOBUF_ALWAYS_INLINE static uint8* WriteInt32ToArray(
585       int field_number, const RepeatedField<int32>& value, uint8* output);
586   PROTOBUF_ALWAYS_INLINE static uint8* WriteInt64ToArray(
587       int field_number, const RepeatedField<int64>& value, uint8* output);
588   PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt32ToArray(
589       int field_number, const RepeatedField<uint32>& value, uint8* output);
590   PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt64ToArray(
591       int field_number, const RepeatedField<uint64>& value, uint8* output);
592   PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt32ToArray(
593       int field_number, const RepeatedField<int32>& value, uint8* output);
594   PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt64ToArray(
595       int field_number, const RepeatedField<int64>& value, uint8* output);
596   PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed32ToArray(
597       int field_number, const RepeatedField<uint32>& value, uint8* output);
598   PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed64ToArray(
599       int field_number, const RepeatedField<uint64>& value, uint8* output);
600   PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed32ToArray(
601       int field_number, const RepeatedField<int32>& value, uint8* output);
602   PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed64ToArray(
603       int field_number, const RepeatedField<int64>& value, uint8* output);
604   PROTOBUF_ALWAYS_INLINE static uint8* WriteFloatToArray(
605       int field_number, const RepeatedField<float>& value, uint8* output);
606   PROTOBUF_ALWAYS_INLINE static uint8* WriteDoubleToArray(
607       int field_number, const RepeatedField<double>& value, uint8* output);
608   PROTOBUF_ALWAYS_INLINE static uint8* WriteBoolToArray(
609       int field_number, const RepeatedField<bool>& value, uint8* output);
610   PROTOBUF_ALWAYS_INLINE static uint8* WriteEnumToArray(
611       int field_number, const RepeatedField<int>& value, uint8* output);
612 
613   PROTOBUF_ALWAYS_INLINE static uint8* WriteStringToArray(
614       int field_number, const std::string& value, uint8* target);
615   PROTOBUF_ALWAYS_INLINE static uint8* WriteBytesToArray(
616       int field_number, const std::string& value, uint8* target);
617 
618   // Whether to serialize deterministically (e.g., map keys are
619   // sorted) is a property of a CodedOutputStream, and in the process
620   // of serialization, the "ToArray" variants may be invoked.  But they don't
621   // have a CodedOutputStream available, so they get an additional parameter
622   // telling them whether to serialize deterministically.
623   template <typename MessageType>
624   PROTOBUF_ALWAYS_INLINE static uint8* InternalWriteGroupToArray(
625       int field_number, const MessageType& value, uint8* target);
626   template <typename MessageType>
627   PROTOBUF_ALWAYS_INLINE static uint8* InternalWriteMessageToArray(
628       int field_number, const MessageType& value, uint8* target);
629 
630   // Like above, but de-virtualize the call to SerializeWithCachedSizes().  The
631   // pointer must point at an instance of MessageType, *not* a subclass (or
632   // the subclass must not override SerializeWithCachedSizes()).
633   template <typename MessageType>
634   PROTOBUF_ALWAYS_INLINE static uint8* InternalWriteGroupNoVirtualToArray(
635       int field_number, const MessageType& value, uint8* target);
636   template <typename MessageType>
637   PROTOBUF_ALWAYS_INLINE static uint8* InternalWriteMessageNoVirtualToArray(
638       int field_number, const MessageType& value, uint8* target);
639 
640   // For backward-compatibility, the last four methods also have versions
641   // that are non-deterministic always.
WriteGroupToArray(int field_number,const MessageLite & value,uint8 * target)642   PROTOBUF_ALWAYS_INLINE static uint8* WriteGroupToArray(
643       int field_number, const MessageLite& value, uint8* target) {
644     return InternalWriteGroupToArray(field_number, value, target);
645   }
WriteMessageToArray(int field_number,const MessageLite & value,uint8 * target)646   PROTOBUF_ALWAYS_INLINE static uint8* WriteMessageToArray(
647       int field_number, const MessageLite& value, uint8* target) {
648     return InternalWriteMessageToArray(field_number, value, target);
649   }
650 
651   // Compute the byte size of a field.  The XxSize() functions do NOT include
652   // the tag, so you must also call TagSize().  (This is because, for repeated
653   // fields, you should only call TagSize() once and multiply it by the element
654   // count, but you may have to call XxSize() for each individual element.)
655   static inline size_t Int32Size(int32 value);
656   static inline size_t Int64Size(int64 value);
657   static inline size_t UInt32Size(uint32 value);
658   static inline size_t UInt64Size(uint64 value);
659   static inline size_t SInt32Size(int32 value);
660   static inline size_t SInt64Size(int64 value);
661   static inline size_t EnumSize(int value);
662 
663   static size_t Int32Size(const RepeatedField<int32>& value);
664   static size_t Int64Size(const RepeatedField<int64>& value);
665   static size_t UInt32Size(const RepeatedField<uint32>& value);
666   static size_t UInt64Size(const RepeatedField<uint64>& value);
667   static size_t SInt32Size(const RepeatedField<int32>& value);
668   static size_t SInt64Size(const RepeatedField<int64>& value);
669   static size_t EnumSize(const RepeatedField<int>& value);
670 
671   // These types always have the same size.
672   static const size_t kFixed32Size = 4;
673   static const size_t kFixed64Size = 8;
674   static const size_t kSFixed32Size = 4;
675   static const size_t kSFixed64Size = 8;
676   static const size_t kFloatSize = 4;
677   static const size_t kDoubleSize = 8;
678   static const size_t kBoolSize = 1;
679 
680   static inline size_t StringSize(const std::string& value);
681   static inline size_t BytesSize(const std::string& value);
682 
683   template <typename MessageType>
684   static inline size_t GroupSize(const MessageType& value);
685   template <typename MessageType>
686   static inline size_t MessageSize(const MessageType& value);
687 
688   // Like above, but de-virtualize the call to ByteSize().  The
689   // pointer must point at an instance of MessageType, *not* a subclass (or
690   // the subclass must not override ByteSize()).
691   template <typename MessageType>
692   static inline size_t GroupSizeNoVirtual(const MessageType& value);
693   template <typename MessageType>
694   static inline size_t MessageSizeNoVirtual(const MessageType& value);
695 
696   // Given the length of data, calculate the byte size of the data on the
697   // wire if we encode the data as a length delimited field.
698   static inline size_t LengthDelimitedSize(size_t length);
699 
700  private:
701   // A helper method for the repeated primitive reader. This method has
702   // optimizations for primitive types that have fixed size on the wire, and
703   // can be read using potentially faster paths.
704   template <typename CType, enum FieldType DeclaredType>
705   PROTOBUF_ALWAYS_INLINE static bool ReadRepeatedFixedSizePrimitive(
706       int tag_size, uint32 tag, io::CodedInputStream* input,
707       RepeatedField<CType>* value);
708 
709   // Like ReadRepeatedFixedSizePrimitive but for packed primitive fields.
710   template <typename CType, enum FieldType DeclaredType>
711   PROTOBUF_ALWAYS_INLINE static bool ReadPackedFixedSizePrimitive(
712       io::CodedInputStream* input, RepeatedField<CType>* value);
713 
714   static const CppType kFieldTypeToCppTypeMap[];
715   static const WireFormatLite::WireType kWireTypeForFieldType[];
716   static void WriteSubMessageMaybeToArray(int size, const MessageLite& value,
717                                           io::CodedOutputStream* output);
718 
719   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormatLite);
720 };
721 
722 // A class which deals with unknown values.  The default implementation just
723 // discards them.  WireFormat defines a subclass which writes to an
724 // UnknownFieldSet.  This class is used by ExtensionSet::ParseField(), since
725 // ExtensionSet is part of the lite library but UnknownFieldSet is not.
726 class PROTOBUF_EXPORT FieldSkipper {
727  public:
FieldSkipper()728   FieldSkipper() {}
~FieldSkipper()729   virtual ~FieldSkipper() {}
730 
731   // Skip a field whose tag has already been consumed.
732   virtual bool SkipField(io::CodedInputStream* input, uint32 tag);
733 
734   // Skip an entire message or group, up to an end-group tag (which is consumed)
735   // or end-of-stream.
736   virtual bool SkipMessage(io::CodedInputStream* input);
737 
738   // Deal with an already-parsed unrecognized enum value.  The default
739   // implementation does nothing, but the UnknownFieldSet-based implementation
740   // saves it as an unknown varint.
741   virtual void SkipUnknownEnum(int field_number, int value);
742 };
743 
744 // Subclass of FieldSkipper which saves skipped fields to a CodedOutputStream.
745 
746 class PROTOBUF_EXPORT CodedOutputStreamFieldSkipper : public FieldSkipper {
747  public:
CodedOutputStreamFieldSkipper(io::CodedOutputStream * unknown_fields)748   explicit CodedOutputStreamFieldSkipper(io::CodedOutputStream* unknown_fields)
749       : unknown_fields_(unknown_fields) {}
~CodedOutputStreamFieldSkipper()750   ~CodedOutputStreamFieldSkipper() override {}
751 
752   // implements FieldSkipper -----------------------------------------
753   bool SkipField(io::CodedInputStream* input, uint32 tag) override;
754   bool SkipMessage(io::CodedInputStream* input) override;
755   void SkipUnknownEnum(int field_number, int value) override;
756 
757  protected:
758   io::CodedOutputStream* unknown_fields_;
759 };
760 
761 // inline methods ====================================================
762 
FieldTypeToCppType(FieldType type)763 inline WireFormatLite::CppType WireFormatLite::FieldTypeToCppType(
764     FieldType type) {
765   return kFieldTypeToCppTypeMap[type];
766 }
767 
MakeTag(int field_number,WireType type)768 constexpr inline uint32 WireFormatLite::MakeTag(int field_number,
769                                                 WireType type) {
770   return GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(field_number, type);
771 }
772 
GetTagWireType(uint32 tag)773 inline WireFormatLite::WireType WireFormatLite::GetTagWireType(uint32 tag) {
774   return static_cast<WireType>(tag & kTagTypeMask);
775 }
776 
GetTagFieldNumber(uint32 tag)777 inline int WireFormatLite::GetTagFieldNumber(uint32 tag) {
778   return static_cast<int>(tag >> kTagTypeBits);
779 }
780 
TagSize(int field_number,WireFormatLite::FieldType type)781 inline size_t WireFormatLite::TagSize(int field_number,
782                                       WireFormatLite::FieldType type) {
783   size_t result = io::CodedOutputStream::VarintSize32(
784       static_cast<uint32>(field_number << kTagTypeBits));
785   if (type == TYPE_GROUP) {
786     // Groups have both a start and an end tag.
787     return result * 2;
788   } else {
789     return result;
790   }
791 }
792 
EncodeFloat(float value)793 inline uint32 WireFormatLite::EncodeFloat(float value) {
794   union {
795     float f;
796     uint32 i;
797   };
798   f = value;
799   return i;
800 }
801 
DecodeFloat(uint32 value)802 inline float WireFormatLite::DecodeFloat(uint32 value) {
803   union {
804     float f;
805     uint32 i;
806   };
807   i = value;
808   return f;
809 }
810 
EncodeDouble(double value)811 inline uint64 WireFormatLite::EncodeDouble(double value) {
812   union {
813     double f;
814     uint64 i;
815   };
816   f = value;
817   return i;
818 }
819 
DecodeDouble(uint64 value)820 inline double WireFormatLite::DecodeDouble(uint64 value) {
821   union {
822     double f;
823     uint64 i;
824   };
825   i = value;
826   return f;
827 }
828 
829 // ZigZag Transform:  Encodes signed integers so that they can be
830 // effectively used with varint encoding.
831 //
832 // varint operates on unsigned integers, encoding smaller numbers into
833 // fewer bytes.  If you try to use it on a signed integer, it will treat
834 // this number as a very large unsigned integer, which means that even
835 // small signed numbers like -1 will take the maximum number of bytes
836 // (10) to encode.  ZigZagEncode() maps signed integers to unsigned
837 // in such a way that those with a small absolute value will have smaller
838 // encoded values, making them appropriate for encoding using varint.
839 //
840 //       int32 ->     uint32
841 // -------------------------
842 //           0 ->          0
843 //          -1 ->          1
844 //           1 ->          2
845 //          -2 ->          3
846 //         ... ->        ...
847 //  2147483647 -> 4294967294
848 // -2147483648 -> 4294967295
849 //
850 //        >> encode >>
851 //        << decode <<
852 
ZigZagEncode32(int32 n)853 inline uint32 WireFormatLite::ZigZagEncode32(int32 n) {
854   // Note:  the right-shift must be arithmetic
855   // Note:  left shift must be unsigned because of overflow
856   return (static_cast<uint32>(n) << 1) ^ static_cast<uint32>(n >> 31);
857 }
858 
ZigZagDecode32(uint32 n)859 inline int32 WireFormatLite::ZigZagDecode32(uint32 n) {
860   // Note:  Using unsigned types prevent undefined behavior
861   return static_cast<int32>((n >> 1) ^ (~(n & 1) + 1));
862 }
863 
ZigZagEncode64(int64 n)864 inline uint64 WireFormatLite::ZigZagEncode64(int64 n) {
865   // Note:  the right-shift must be arithmetic
866   // Note:  left shift must be unsigned because of overflow
867   return (static_cast<uint64>(n) << 1) ^ static_cast<uint64>(n >> 63);
868 }
869 
ZigZagDecode64(uint64 n)870 inline int64 WireFormatLite::ZigZagDecode64(uint64 n) {
871   // Note:  Using unsigned types prevent undefined behavior
872   return static_cast<int64>((n >> 1) ^ (~(n & 1) + 1));
873 }
874 
875 // String is for UTF-8 text only, but, even so, ReadString() can simply
876 // call ReadBytes().
877 
ReadString(io::CodedInputStream * input,std::string * value)878 inline bool WireFormatLite::ReadString(io::CodedInputStream* input,
879                                        std::string* value) {
880   return ReadBytes(input, value);
881 }
882 
ReadString(io::CodedInputStream * input,std::string ** p)883 inline bool WireFormatLite::ReadString(io::CodedInputStream* input,
884                                        std::string** p) {
885   return ReadBytes(input, p);
886 }
887 
SerializeUnknownMessageSetItems(const std::string & unknown_fields,io::CodedOutputStream * output)888 inline void SerializeUnknownMessageSetItems(const std::string& unknown_fields,
889                                             io::CodedOutputStream* output) {
890   output->WriteString(unknown_fields);
891 }
892 
ComputeUnknownMessageSetItemsSize(const std::string & unknown_fields)893 inline size_t ComputeUnknownMessageSetItemsSize(
894     const std::string& unknown_fields) {
895   return unknown_fields.size();
896 }
897 
898 // Implementation details of ReadPrimitive.
899 
900 template <>
901 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_INT32>(
902     io::CodedInputStream* input, int32* value) {
903   uint32 temp;
904   if (!input->ReadVarint32(&temp)) return false;
905   *value = static_cast<int32>(temp);
906   return true;
907 }
908 template <>
909 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_INT64>(
910     io::CodedInputStream* input, int64* value) {
911   uint64 temp;
912   if (!input->ReadVarint64(&temp)) return false;
913   *value = static_cast<int64>(temp);
914   return true;
915 }
916 template <>
917 inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_UINT32>(
918     io::CodedInputStream* input, uint32* value) {
919   return input->ReadVarint32(value);
920 }
921 template <>
922 inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_UINT64>(
923     io::CodedInputStream* input, uint64* value) {
924   return input->ReadVarint64(value);
925 }
926 template <>
927 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SINT32>(
928     io::CodedInputStream* input, int32* value) {
929   uint32 temp;
930   if (!input->ReadVarint32(&temp)) return false;
931   *value = ZigZagDecode32(temp);
932   return true;
933 }
934 template <>
935 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SINT64>(
936     io::CodedInputStream* input, int64* value) {
937   uint64 temp;
938   if (!input->ReadVarint64(&temp)) return false;
939   *value = ZigZagDecode64(temp);
940   return true;
941 }
942 template <>
943 inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_FIXED32>(
944     io::CodedInputStream* input, uint32* value) {
945   return input->ReadLittleEndian32(value);
946 }
947 template <>
948 inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_FIXED64>(
949     io::CodedInputStream* input, uint64* value) {
950   return input->ReadLittleEndian64(value);
951 }
952 template <>
953 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SFIXED32>(
954     io::CodedInputStream* input, int32* value) {
955   uint32 temp;
956   if (!input->ReadLittleEndian32(&temp)) return false;
957   *value = static_cast<int32>(temp);
958   return true;
959 }
960 template <>
961 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SFIXED64>(
962     io::CodedInputStream* input, int64* value) {
963   uint64 temp;
964   if (!input->ReadLittleEndian64(&temp)) return false;
965   *value = static_cast<int64>(temp);
966   return true;
967 }
968 template <>
969 inline bool WireFormatLite::ReadPrimitive<float, WireFormatLite::TYPE_FLOAT>(
970     io::CodedInputStream* input, float* value) {
971   uint32 temp;
972   if (!input->ReadLittleEndian32(&temp)) return false;
973   *value = DecodeFloat(temp);
974   return true;
975 }
976 template <>
977 inline bool WireFormatLite::ReadPrimitive<double, WireFormatLite::TYPE_DOUBLE>(
978     io::CodedInputStream* input, double* value) {
979   uint64 temp;
980   if (!input->ReadLittleEndian64(&temp)) return false;
981   *value = DecodeDouble(temp);
982   return true;
983 }
984 template <>
985 inline bool WireFormatLite::ReadPrimitive<bool, WireFormatLite::TYPE_BOOL>(
986     io::CodedInputStream* input, bool* value) {
987   uint64 temp;
988   if (!input->ReadVarint64(&temp)) return false;
989   *value = temp != 0;
990   return true;
991 }
992 template <>
993 inline bool WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
994     io::CodedInputStream* input, int* value) {
995   uint32 temp;
996   if (!input->ReadVarint32(&temp)) return false;
997   *value = static_cast<int>(temp);
998   return true;
999 }
1000 
1001 template <>
1002 inline const uint8*
1003 WireFormatLite::ReadPrimitiveFromArray<uint32, WireFormatLite::TYPE_FIXED32>(
1004     const uint8* buffer, uint32* value) {
1005   return io::CodedInputStream::ReadLittleEndian32FromArray(buffer, value);
1006 }
1007 template <>
1008 inline const uint8*
1009 WireFormatLite::ReadPrimitiveFromArray<uint64, WireFormatLite::TYPE_FIXED64>(
1010     const uint8* buffer, uint64* value) {
1011   return io::CodedInputStream::ReadLittleEndian64FromArray(buffer, value);
1012 }
1013 template <>
1014 inline const uint8*
1015 WireFormatLite::ReadPrimitiveFromArray<int32, WireFormatLite::TYPE_SFIXED32>(
1016     const uint8* buffer, int32* value) {
1017   uint32 temp;
1018   buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
1019   *value = static_cast<int32>(temp);
1020   return buffer;
1021 }
1022 template <>
1023 inline const uint8*
1024 WireFormatLite::ReadPrimitiveFromArray<int64, WireFormatLite::TYPE_SFIXED64>(
1025     const uint8* buffer, int64* value) {
1026   uint64 temp;
1027   buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
1028   *value = static_cast<int64>(temp);
1029   return buffer;
1030 }
1031 template <>
1032 inline const uint8*
1033 WireFormatLite::ReadPrimitiveFromArray<float, WireFormatLite::TYPE_FLOAT>(
1034     const uint8* buffer, float* value) {
1035   uint32 temp;
1036   buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
1037   *value = DecodeFloat(temp);
1038   return buffer;
1039 }
1040 template <>
1041 inline const uint8*
1042 WireFormatLite::ReadPrimitiveFromArray<double, WireFormatLite::TYPE_DOUBLE>(
1043     const uint8* buffer, double* value) {
1044   uint64 temp;
1045   buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
1046   *value = DecodeDouble(temp);
1047   return buffer;
1048 }
1049 
1050 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
ReadRepeatedPrimitive(int,uint32 tag,io::CodedInputStream * input,RepeatedField<CType> * values)1051 inline bool WireFormatLite::ReadRepeatedPrimitive(
1052     int,  // tag_size, unused.
1053     uint32 tag, io::CodedInputStream* input, RepeatedField<CType>* values) {
1054   CType value;
1055   if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
1056   values->Add(value);
1057   int elements_already_reserved = values->Capacity() - values->size();
1058   while (elements_already_reserved > 0 && input->ExpectTag(tag)) {
1059     if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
1060     values->AddAlreadyReserved(value);
1061     elements_already_reserved--;
1062   }
1063   return true;
1064 }
1065 
1066 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
ReadRepeatedFixedSizePrimitive(int tag_size,uint32 tag,io::CodedInputStream * input,RepeatedField<CType> * values)1067 inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive(
1068     int tag_size, uint32 tag, io::CodedInputStream* input,
1069     RepeatedField<CType>* values) {
1070   GOOGLE_DCHECK_EQ(UInt32Size(tag), static_cast<size_t>(tag_size));
1071   CType value;
1072   if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
1073   values->Add(value);
1074 
1075   // For fixed size values, repeated values can be read more quickly by
1076   // reading directly from a raw array.
1077   //
1078   // We can get a tight loop by only reading as many elements as can be
1079   // added to the RepeatedField without having to do any resizing. Additionally,
1080   // we only try to read as many elements as are available from the current
1081   // buffer space. Doing so avoids having to perform boundary checks when
1082   // reading the value: the maximum number of elements that can be read is
1083   // known outside of the loop.
1084   const void* void_pointer;
1085   int size;
1086   input->GetDirectBufferPointerInline(&void_pointer, &size);
1087   if (size > 0) {
1088     const uint8* buffer = reinterpret_cast<const uint8*>(void_pointer);
1089     // The number of bytes each type occupies on the wire.
1090     const int per_value_size = tag_size + static_cast<int>(sizeof(value));
1091 
1092     // parentheses around (std::min) prevents macro expansion of min(...)
1093     int elements_available =
1094         (std::min)(values->Capacity() - values->size(), size / per_value_size);
1095     int num_read = 0;
1096     while (num_read < elements_available &&
1097            (buffer = io::CodedInputStream::ExpectTagFromArray(buffer, tag)) !=
1098                NULL) {
1099       buffer = ReadPrimitiveFromArray<CType, DeclaredType>(buffer, &value);
1100       values->AddAlreadyReserved(value);
1101       ++num_read;
1102     }
1103     const int read_bytes = num_read * per_value_size;
1104     if (read_bytes > 0) {
1105       input->Skip(read_bytes);
1106     }
1107   }
1108   return true;
1109 }
1110 
1111 // Specializations of ReadRepeatedPrimitive for the fixed size types, which use
1112 // the optimized code path.
1113 #define READ_REPEATED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE)        \
1114   template <>                                                             \
1115   inline bool WireFormatLite::ReadRepeatedPrimitive<                      \
1116       CPPTYPE, WireFormatLite::DECLARED_TYPE>(                            \
1117       int tag_size, uint32 tag, io::CodedInputStream* input,              \
1118       RepeatedField<CPPTYPE>* values) {                                   \
1119     return ReadRepeatedFixedSizePrimitive<CPPTYPE,                        \
1120                                           WireFormatLite::DECLARED_TYPE>( \
1121         tag_size, tag, input, values);                                    \
1122   }
1123 
READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint32,TYPE_FIXED32)1124 READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32)
1125 READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64)
1126 READ_REPEATED_FIXED_SIZE_PRIMITIVE(int32, TYPE_SFIXED32)
1127 READ_REPEATED_FIXED_SIZE_PRIMITIVE(int64, TYPE_SFIXED64)
1128 READ_REPEATED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT)
1129 READ_REPEATED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE)
1130 
1131 #undef READ_REPEATED_FIXED_SIZE_PRIMITIVE
1132 
1133 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
1134 bool WireFormatLite::ReadRepeatedPrimitiveNoInline(
1135     int tag_size, uint32 tag, io::CodedInputStream* input,
1136     RepeatedField<CType>* value) {
1137   return ReadRepeatedPrimitive<CType, DeclaredType>(tag_size, tag, input,
1138                                                     value);
1139 }
1140 
1141 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
ReadPackedPrimitive(io::CodedInputStream * input,RepeatedField<CType> * values)1142 inline bool WireFormatLite::ReadPackedPrimitive(io::CodedInputStream* input,
1143                                                 RepeatedField<CType>* values) {
1144   int length;
1145   if (!input->ReadVarintSizeAsInt(&length)) return false;
1146   io::CodedInputStream::Limit limit = input->PushLimit(length);
1147   while (input->BytesUntilLimit() > 0) {
1148     CType value;
1149     if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
1150     values->Add(value);
1151   }
1152   input->PopLimit(limit);
1153   return true;
1154 }
1155 
1156 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
ReadPackedFixedSizePrimitive(io::CodedInputStream * input,RepeatedField<CType> * values)1157 inline bool WireFormatLite::ReadPackedFixedSizePrimitive(
1158     io::CodedInputStream* input, RepeatedField<CType>* values) {
1159   int length;
1160   if (!input->ReadVarintSizeAsInt(&length)) return false;
1161   const int old_entries = values->size();
1162   const int new_entries = length / static_cast<int>(sizeof(CType));
1163   const int new_bytes = new_entries * static_cast<int>(sizeof(CType));
1164   if (new_bytes != length) return false;
1165   // We would *like* to pre-allocate the buffer to write into (for
1166   // speed), but *must* avoid performing a very large allocation due
1167   // to a malicious user-supplied "length" above.  So we have a fast
1168   // path that pre-allocates when the "length" is less than a bound.
1169   // We determine the bound by calling BytesUntilTotalBytesLimit() and
1170   // BytesUntilLimit().  These return -1 to mean "no limit set".
1171   // There are four cases:
1172   // TotalBytesLimit  Limit
1173   // -1               -1     Use slow path.
1174   // -1               >= 0   Use fast path if length <= Limit.
1175   // >= 0             -1     Use slow path.
1176   // >= 0             >= 0   Use fast path if length <= min(both limits).
1177   int64 bytes_limit = input->BytesUntilTotalBytesLimit();
1178   if (bytes_limit == -1) {
1179     bytes_limit = input->BytesUntilLimit();
1180   } else {
1181     // parentheses around (std::min) prevents macro expansion of min(...)
1182     bytes_limit =
1183         (std::min)(bytes_limit, static_cast<int64>(input->BytesUntilLimit()));
1184   }
1185   if (bytes_limit >= new_bytes) {
1186     // Fast-path that pre-allocates *values to the final size.
1187 #if defined(PROTOBUF_LITTLE_ENDIAN)
1188     values->Resize(old_entries + new_entries, 0);
1189     // values->mutable_data() may change after Resize(), so do this after:
1190     void* dest = reinterpret_cast<void*>(values->mutable_data() + old_entries);
1191     if (!input->ReadRaw(dest, new_bytes)) {
1192       values->Truncate(old_entries);
1193       return false;
1194     }
1195 #else
1196     values->Reserve(old_entries + new_entries);
1197     CType value;
1198     for (int i = 0; i < new_entries; ++i) {
1199       if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
1200       values->AddAlreadyReserved(value);
1201     }
1202 #endif
1203   } else {
1204     // This is the slow-path case where "length" may be too large to
1205     // safely allocate.  We read as much as we can into *values
1206     // without pre-allocating "length" bytes.
1207     CType value;
1208     for (int i = 0; i < new_entries; ++i) {
1209       if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
1210       values->Add(value);
1211     }
1212   }
1213   return true;
1214 }
1215 
1216 // Specializations of ReadPackedPrimitive for the fixed size types, which use
1217 // an optimized code path.
1218 #define READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE)      \
1219   template <>                                                                  \
1220   inline bool                                                                  \
1221   WireFormatLite::ReadPackedPrimitive<CPPTYPE, WireFormatLite::DECLARED_TYPE>( \
1222       io::CodedInputStream * input, RepeatedField<CPPTYPE> * values) {         \
1223     return ReadPackedFixedSizePrimitive<CPPTYPE,                               \
1224                                         WireFormatLite::DECLARED_TYPE>(        \
1225         input, values);                                                        \
1226   }
1227 
READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint32,TYPE_FIXED32)1228 READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32)
1229 READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64)
1230 READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(int32, TYPE_SFIXED32)
1231 READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(int64, TYPE_SFIXED64)
1232 READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT)
1233 READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE)
1234 
1235 #undef READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE
1236 
1237 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
1238 bool WireFormatLite::ReadPackedPrimitiveNoInline(io::CodedInputStream* input,
1239                                                  RepeatedField<CType>* values) {
1240   return ReadPackedPrimitive<CType, DeclaredType>(input, values);
1241 }
1242 
1243 
1244 template <typename MessageType>
ReadGroup(int field_number,io::CodedInputStream * input,MessageType * value)1245 inline bool WireFormatLite::ReadGroup(int field_number,
1246                                       io::CodedInputStream* input,
1247                                       MessageType* value) {
1248   if (!input->IncrementRecursionDepth()) return false;
1249   if (!value->MergePartialFromCodedStream(input)) return false;
1250   input->UnsafeDecrementRecursionDepth();
1251   // Make sure the last thing read was an end tag for this group.
1252   if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) {
1253     return false;
1254   }
1255   return true;
1256 }
1257 template <typename MessageType>
ReadMessage(io::CodedInputStream * input,MessageType * value)1258 inline bool WireFormatLite::ReadMessage(io::CodedInputStream* input,
1259                                         MessageType* value) {
1260   int length;
1261   if (!input->ReadVarintSizeAsInt(&length)) return false;
1262   std::pair<io::CodedInputStream::Limit, int> p =
1263       input->IncrementRecursionDepthAndPushLimit(length);
1264   if (p.second < 0 || !value->MergePartialFromCodedStream(input)) return false;
1265   // Make sure that parsing stopped when the limit was hit, not at an endgroup
1266   // tag.
1267   return input->DecrementRecursionDepthAndPopLimit(p.first);
1268 }
1269 
1270 // ===================================================================
1271 
WriteTag(int field_number,WireType type,io::CodedOutputStream * output)1272 inline void WireFormatLite::WriteTag(int field_number, WireType type,
1273                                      io::CodedOutputStream* output) {
1274   output->WriteTag(MakeTag(field_number, type));
1275 }
1276 
WriteInt32NoTag(int32 value,io::CodedOutputStream * output)1277 inline void WireFormatLite::WriteInt32NoTag(int32 value,
1278                                             io::CodedOutputStream* output) {
1279   output->WriteVarint32SignExtended(value);
1280 }
WriteInt64NoTag(int64 value,io::CodedOutputStream * output)1281 inline void WireFormatLite::WriteInt64NoTag(int64 value,
1282                                             io::CodedOutputStream* output) {
1283   output->WriteVarint64(static_cast<uint64>(value));
1284 }
WriteUInt32NoTag(uint32 value,io::CodedOutputStream * output)1285 inline void WireFormatLite::WriteUInt32NoTag(uint32 value,
1286                                              io::CodedOutputStream* output) {
1287   output->WriteVarint32(value);
1288 }
WriteUInt64NoTag(uint64 value,io::CodedOutputStream * output)1289 inline void WireFormatLite::WriteUInt64NoTag(uint64 value,
1290                                              io::CodedOutputStream* output) {
1291   output->WriteVarint64(value);
1292 }
WriteSInt32NoTag(int32 value,io::CodedOutputStream * output)1293 inline void WireFormatLite::WriteSInt32NoTag(int32 value,
1294                                              io::CodedOutputStream* output) {
1295   output->WriteVarint32(ZigZagEncode32(value));
1296 }
WriteSInt64NoTag(int64 value,io::CodedOutputStream * output)1297 inline void WireFormatLite::WriteSInt64NoTag(int64 value,
1298                                              io::CodedOutputStream* output) {
1299   output->WriteVarint64(ZigZagEncode64(value));
1300 }
WriteFixed32NoTag(uint32 value,io::CodedOutputStream * output)1301 inline void WireFormatLite::WriteFixed32NoTag(uint32 value,
1302                                               io::CodedOutputStream* output) {
1303   output->WriteLittleEndian32(value);
1304 }
WriteFixed64NoTag(uint64 value,io::CodedOutputStream * output)1305 inline void WireFormatLite::WriteFixed64NoTag(uint64 value,
1306                                               io::CodedOutputStream* output) {
1307   output->WriteLittleEndian64(value);
1308 }
WriteSFixed32NoTag(int32 value,io::CodedOutputStream * output)1309 inline void WireFormatLite::WriteSFixed32NoTag(int32 value,
1310                                                io::CodedOutputStream* output) {
1311   output->WriteLittleEndian32(static_cast<uint32>(value));
1312 }
WriteSFixed64NoTag(int64 value,io::CodedOutputStream * output)1313 inline void WireFormatLite::WriteSFixed64NoTag(int64 value,
1314                                                io::CodedOutputStream* output) {
1315   output->WriteLittleEndian64(static_cast<uint64>(value));
1316 }
WriteFloatNoTag(float value,io::CodedOutputStream * output)1317 inline void WireFormatLite::WriteFloatNoTag(float value,
1318                                             io::CodedOutputStream* output) {
1319   output->WriteLittleEndian32(EncodeFloat(value));
1320 }
WriteDoubleNoTag(double value,io::CodedOutputStream * output)1321 inline void WireFormatLite::WriteDoubleNoTag(double value,
1322                                              io::CodedOutputStream* output) {
1323   output->WriteLittleEndian64(EncodeDouble(value));
1324 }
WriteBoolNoTag(bool value,io::CodedOutputStream * output)1325 inline void WireFormatLite::WriteBoolNoTag(bool value,
1326                                            io::CodedOutputStream* output) {
1327   output->WriteVarint32(value ? 1 : 0);
1328 }
WriteEnumNoTag(int value,io::CodedOutputStream * output)1329 inline void WireFormatLite::WriteEnumNoTag(int value,
1330                                            io::CodedOutputStream* output) {
1331   output->WriteVarint32SignExtended(value);
1332 }
1333 
1334 // See comment on ReadGroupNoVirtual to understand the need for this template
1335 // parameter name.
1336 template <typename MessageType_WorkAroundCppLookupDefect>
WriteGroupNoVirtual(int field_number,const MessageType_WorkAroundCppLookupDefect & value,io::CodedOutputStream * output)1337 inline void WireFormatLite::WriteGroupNoVirtual(
1338     int field_number, const MessageType_WorkAroundCppLookupDefect& value,
1339     io::CodedOutputStream* output) {
1340   WriteTag(field_number, WIRETYPE_START_GROUP, output);
1341   value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output);
1342   WriteTag(field_number, WIRETYPE_END_GROUP, output);
1343 }
1344 template <typename MessageType_WorkAroundCppLookupDefect>
WriteMessageNoVirtual(int field_number,const MessageType_WorkAroundCppLookupDefect & value,io::CodedOutputStream * output)1345 inline void WireFormatLite::WriteMessageNoVirtual(
1346     int field_number, const MessageType_WorkAroundCppLookupDefect& value,
1347     io::CodedOutputStream* output) {
1348   WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
1349   output->WriteVarint32(
1350       value.MessageType_WorkAroundCppLookupDefect::GetCachedSize());
1351   value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output);
1352 }
1353 
1354 // ===================================================================
1355 
WriteTagToArray(int field_number,WireType type,uint8 * target)1356 inline uint8* WireFormatLite::WriteTagToArray(int field_number, WireType type,
1357                                               uint8* target) {
1358   return io::CodedOutputStream::WriteTagToArray(MakeTag(field_number, type),
1359                                                 target);
1360 }
1361 
WriteInt32NoTagToArray(int32 value,uint8 * target)1362 inline uint8* WireFormatLite::WriteInt32NoTagToArray(int32 value,
1363                                                      uint8* target) {
1364   return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
1365 }
WriteInt64NoTagToArray(int64 value,uint8 * target)1366 inline uint8* WireFormatLite::WriteInt64NoTagToArray(int64 value,
1367                                                      uint8* target) {
1368   return io::CodedOutputStream::WriteVarint64ToArray(static_cast<uint64>(value),
1369                                                      target);
1370 }
WriteUInt32NoTagToArray(uint32 value,uint8 * target)1371 inline uint8* WireFormatLite::WriteUInt32NoTagToArray(uint32 value,
1372                                                       uint8* target) {
1373   return io::CodedOutputStream::WriteVarint32ToArray(value, target);
1374 }
WriteUInt64NoTagToArray(uint64 value,uint8 * target)1375 inline uint8* WireFormatLite::WriteUInt64NoTagToArray(uint64 value,
1376                                                       uint8* target) {
1377   return io::CodedOutputStream::WriteVarint64ToArray(value, target);
1378 }
WriteSInt32NoTagToArray(int32 value,uint8 * target)1379 inline uint8* WireFormatLite::WriteSInt32NoTagToArray(int32 value,
1380                                                       uint8* target) {
1381   return io::CodedOutputStream::WriteVarint32ToArray(ZigZagEncode32(value),
1382                                                      target);
1383 }
WriteSInt64NoTagToArray(int64 value,uint8 * target)1384 inline uint8* WireFormatLite::WriteSInt64NoTagToArray(int64 value,
1385                                                       uint8* target) {
1386   return io::CodedOutputStream::WriteVarint64ToArray(ZigZagEncode64(value),
1387                                                      target);
1388 }
WriteFixed32NoTagToArray(uint32 value,uint8 * target)1389 inline uint8* WireFormatLite::WriteFixed32NoTagToArray(uint32 value,
1390                                                        uint8* target) {
1391   return io::CodedOutputStream::WriteLittleEndian32ToArray(value, target);
1392 }
WriteFixed64NoTagToArray(uint64 value,uint8 * target)1393 inline uint8* WireFormatLite::WriteFixed64NoTagToArray(uint64 value,
1394                                                        uint8* target) {
1395   return io::CodedOutputStream::WriteLittleEndian64ToArray(value, target);
1396 }
WriteSFixed32NoTagToArray(int32 value,uint8 * target)1397 inline uint8* WireFormatLite::WriteSFixed32NoTagToArray(int32 value,
1398                                                         uint8* target) {
1399   return io::CodedOutputStream::WriteLittleEndian32ToArray(
1400       static_cast<uint32>(value), target);
1401 }
WriteSFixed64NoTagToArray(int64 value,uint8 * target)1402 inline uint8* WireFormatLite::WriteSFixed64NoTagToArray(int64 value,
1403                                                         uint8* target) {
1404   return io::CodedOutputStream::WriteLittleEndian64ToArray(
1405       static_cast<uint64>(value), target);
1406 }
WriteFloatNoTagToArray(float value,uint8 * target)1407 inline uint8* WireFormatLite::WriteFloatNoTagToArray(float value,
1408                                                      uint8* target) {
1409   return io::CodedOutputStream::WriteLittleEndian32ToArray(EncodeFloat(value),
1410                                                            target);
1411 }
WriteDoubleNoTagToArray(double value,uint8 * target)1412 inline uint8* WireFormatLite::WriteDoubleNoTagToArray(double value,
1413                                                       uint8* target) {
1414   return io::CodedOutputStream::WriteLittleEndian64ToArray(EncodeDouble(value),
1415                                                            target);
1416 }
WriteBoolNoTagToArray(bool value,uint8 * target)1417 inline uint8* WireFormatLite::WriteBoolNoTagToArray(bool value, uint8* target) {
1418   return io::CodedOutputStream::WriteVarint32ToArray(value ? 1 : 0, target);
1419 }
WriteEnumNoTagToArray(int value,uint8 * target)1420 inline uint8* WireFormatLite::WriteEnumNoTagToArray(int value, uint8* target) {
1421   return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
1422 }
1423 
1424 template <typename T>
WritePrimitiveNoTagToArray(const RepeatedField<T> & value,uint8 * (* Writer)(T,uint8 *),uint8 * target)1425 inline uint8* WireFormatLite::WritePrimitiveNoTagToArray(
1426     const RepeatedField<T>& value, uint8* (*Writer)(T, uint8*), uint8* target) {
1427   const int n = value.size();
1428   GOOGLE_DCHECK_GT(n, 0);
1429 
1430   const T* ii = value.data();
1431   int i = 0;
1432   do {
1433     target = Writer(ii[i], target);
1434   } while (++i < n);
1435 
1436   return target;
1437 }
1438 
1439 template <typename T>
WriteFixedNoTagToArray(const RepeatedField<T> & value,uint8 * (* Writer)(T,uint8 *),uint8 * target)1440 inline uint8* WireFormatLite::WriteFixedNoTagToArray(
1441     const RepeatedField<T>& value, uint8* (*Writer)(T, uint8*), uint8* target) {
1442 #if defined(PROTOBUF_LITTLE_ENDIAN)
1443   (void)Writer;
1444 
1445   const int n = value.size();
1446   GOOGLE_DCHECK_GT(n, 0);
1447 
1448   const T* ii = value.data();
1449   const int bytes = n * static_cast<int>(sizeof(ii[0]));
1450   memcpy(target, ii, static_cast<size_t>(bytes));
1451   return target + bytes;
1452 #else
1453   return WritePrimitiveNoTagToArray(value, Writer, target);
1454 #endif
1455 }
1456 
WriteInt32NoTagToArray(const RepeatedField<int32> & value,uint8 * target)1457 inline uint8* WireFormatLite::WriteInt32NoTagToArray(
1458     const RepeatedField<int32>& value, uint8* target) {
1459   return WritePrimitiveNoTagToArray(value, WriteInt32NoTagToArray, target);
1460 }
WriteInt64NoTagToArray(const RepeatedField<int64> & value,uint8 * target)1461 inline uint8* WireFormatLite::WriteInt64NoTagToArray(
1462     const RepeatedField<int64>& value, uint8* target) {
1463   return WritePrimitiveNoTagToArray(value, WriteInt64NoTagToArray, target);
1464 }
WriteUInt32NoTagToArray(const RepeatedField<uint32> & value,uint8 * target)1465 inline uint8* WireFormatLite::WriteUInt32NoTagToArray(
1466     const RepeatedField<uint32>& value, uint8* target) {
1467   return WritePrimitiveNoTagToArray(value, WriteUInt32NoTagToArray, target);
1468 }
WriteUInt64NoTagToArray(const RepeatedField<uint64> & value,uint8 * target)1469 inline uint8* WireFormatLite::WriteUInt64NoTagToArray(
1470     const RepeatedField<uint64>& value, uint8* target) {
1471   return WritePrimitiveNoTagToArray(value, WriteUInt64NoTagToArray, target);
1472 }
WriteSInt32NoTagToArray(const RepeatedField<int32> & value,uint8 * target)1473 inline uint8* WireFormatLite::WriteSInt32NoTagToArray(
1474     const RepeatedField<int32>& value, uint8* target) {
1475   return WritePrimitiveNoTagToArray(value, WriteSInt32NoTagToArray, target);
1476 }
WriteSInt64NoTagToArray(const RepeatedField<int64> & value,uint8 * target)1477 inline uint8* WireFormatLite::WriteSInt64NoTagToArray(
1478     const RepeatedField<int64>& value, uint8* target) {
1479   return WritePrimitiveNoTagToArray(value, WriteSInt64NoTagToArray, target);
1480 }
WriteFixed32NoTagToArray(const RepeatedField<uint32> & value,uint8 * target)1481 inline uint8* WireFormatLite::WriteFixed32NoTagToArray(
1482     const RepeatedField<uint32>& value, uint8* target) {
1483   return WriteFixedNoTagToArray(value, WriteFixed32NoTagToArray, target);
1484 }
WriteFixed64NoTagToArray(const RepeatedField<uint64> & value,uint8 * target)1485 inline uint8* WireFormatLite::WriteFixed64NoTagToArray(
1486     const RepeatedField<uint64>& value, uint8* target) {
1487   return WriteFixedNoTagToArray(value, WriteFixed64NoTagToArray, target);
1488 }
WriteSFixed32NoTagToArray(const RepeatedField<int32> & value,uint8 * target)1489 inline uint8* WireFormatLite::WriteSFixed32NoTagToArray(
1490     const RepeatedField<int32>& value, uint8* target) {
1491   return WriteFixedNoTagToArray(value, WriteSFixed32NoTagToArray, target);
1492 }
WriteSFixed64NoTagToArray(const RepeatedField<int64> & value,uint8 * target)1493 inline uint8* WireFormatLite::WriteSFixed64NoTagToArray(
1494     const RepeatedField<int64>& value, uint8* target) {
1495   return WriteFixedNoTagToArray(value, WriteSFixed64NoTagToArray, target);
1496 }
WriteFloatNoTagToArray(const RepeatedField<float> & value,uint8 * target)1497 inline uint8* WireFormatLite::WriteFloatNoTagToArray(
1498     const RepeatedField<float>& value, uint8* target) {
1499   return WriteFixedNoTagToArray(value, WriteFloatNoTagToArray, target);
1500 }
WriteDoubleNoTagToArray(const RepeatedField<double> & value,uint8 * target)1501 inline uint8* WireFormatLite::WriteDoubleNoTagToArray(
1502     const RepeatedField<double>& value, uint8* target) {
1503   return WriteFixedNoTagToArray(value, WriteDoubleNoTagToArray, target);
1504 }
WriteBoolNoTagToArray(const RepeatedField<bool> & value,uint8 * target)1505 inline uint8* WireFormatLite::WriteBoolNoTagToArray(
1506     const RepeatedField<bool>& value, uint8* target) {
1507   return WritePrimitiveNoTagToArray(value, WriteBoolNoTagToArray, target);
1508 }
WriteEnumNoTagToArray(const RepeatedField<int> & value,uint8 * target)1509 inline uint8* WireFormatLite::WriteEnumNoTagToArray(
1510     const RepeatedField<int>& value, uint8* target) {
1511   return WritePrimitiveNoTagToArray(value, WriteEnumNoTagToArray, target);
1512 }
1513 
WriteInt32ToArray(int field_number,int32 value,uint8 * target)1514 inline uint8* WireFormatLite::WriteInt32ToArray(int field_number, int32 value,
1515                                                 uint8* target) {
1516   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
1517   return WriteInt32NoTagToArray(value, target);
1518 }
WriteInt64ToArray(int field_number,int64 value,uint8 * target)1519 inline uint8* WireFormatLite::WriteInt64ToArray(int field_number, int64 value,
1520                                                 uint8* target) {
1521   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
1522   return WriteInt64NoTagToArray(value, target);
1523 }
WriteUInt32ToArray(int field_number,uint32 value,uint8 * target)1524 inline uint8* WireFormatLite::WriteUInt32ToArray(int field_number, uint32 value,
1525                                                  uint8* target) {
1526   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
1527   return WriteUInt32NoTagToArray(value, target);
1528 }
WriteUInt64ToArray(int field_number,uint64 value,uint8 * target)1529 inline uint8* WireFormatLite::WriteUInt64ToArray(int field_number, uint64 value,
1530                                                  uint8* target) {
1531   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
1532   return WriteUInt64NoTagToArray(value, target);
1533 }
WriteSInt32ToArray(int field_number,int32 value,uint8 * target)1534 inline uint8* WireFormatLite::WriteSInt32ToArray(int field_number, int32 value,
1535                                                  uint8* target) {
1536   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
1537   return WriteSInt32NoTagToArray(value, target);
1538 }
WriteSInt64ToArray(int field_number,int64 value,uint8 * target)1539 inline uint8* WireFormatLite::WriteSInt64ToArray(int field_number, int64 value,
1540                                                  uint8* target) {
1541   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
1542   return WriteSInt64NoTagToArray(value, target);
1543 }
WriteFixed32ToArray(int field_number,uint32 value,uint8 * target)1544 inline uint8* WireFormatLite::WriteFixed32ToArray(int field_number,
1545                                                   uint32 value, uint8* target) {
1546   target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
1547   return WriteFixed32NoTagToArray(value, target);
1548 }
WriteFixed64ToArray(int field_number,uint64 value,uint8 * target)1549 inline uint8* WireFormatLite::WriteFixed64ToArray(int field_number,
1550                                                   uint64 value, uint8* target) {
1551   target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
1552   return WriteFixed64NoTagToArray(value, target);
1553 }
WriteSFixed32ToArray(int field_number,int32 value,uint8 * target)1554 inline uint8* WireFormatLite::WriteSFixed32ToArray(int field_number,
1555                                                    int32 value, uint8* target) {
1556   target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
1557   return WriteSFixed32NoTagToArray(value, target);
1558 }
WriteSFixed64ToArray(int field_number,int64 value,uint8 * target)1559 inline uint8* WireFormatLite::WriteSFixed64ToArray(int field_number,
1560                                                    int64 value, uint8* target) {
1561   target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
1562   return WriteSFixed64NoTagToArray(value, target);
1563 }
WriteFloatToArray(int field_number,float value,uint8 * target)1564 inline uint8* WireFormatLite::WriteFloatToArray(int field_number, float value,
1565                                                 uint8* target) {
1566   target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
1567   return WriteFloatNoTagToArray(value, target);
1568 }
WriteDoubleToArray(int field_number,double value,uint8 * target)1569 inline uint8* WireFormatLite::WriteDoubleToArray(int field_number, double value,
1570                                                  uint8* target) {
1571   target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
1572   return WriteDoubleNoTagToArray(value, target);
1573 }
WriteBoolToArray(int field_number,bool value,uint8 * target)1574 inline uint8* WireFormatLite::WriteBoolToArray(int field_number, bool value,
1575                                                uint8* target) {
1576   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
1577   return WriteBoolNoTagToArray(value, target);
1578 }
WriteEnumToArray(int field_number,int value,uint8 * target)1579 inline uint8* WireFormatLite::WriteEnumToArray(int field_number, int value,
1580                                                uint8* target) {
1581   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
1582   return WriteEnumNoTagToArray(value, target);
1583 }
1584 
1585 template <typename T>
WritePrimitiveToArray(int field_number,const RepeatedField<T> & value,uint8 * (* Writer)(int,T,uint8 *),uint8 * target)1586 inline uint8* WireFormatLite::WritePrimitiveToArray(
1587     int field_number, const RepeatedField<T>& value,
1588     uint8* (*Writer)(int, T, uint8*), uint8* target) {
1589   const int n = value.size();
1590   if (n == 0) {
1591     return target;
1592   }
1593 
1594   const T* ii = value.data();
1595   int i = 0;
1596   do {
1597     target = Writer(field_number, ii[i], target);
1598   } while (++i < n);
1599 
1600   return target;
1601 }
1602 
WriteInt32ToArray(int field_number,const RepeatedField<int32> & value,uint8 * target)1603 inline uint8* WireFormatLite::WriteInt32ToArray(
1604     int field_number, const RepeatedField<int32>& value, uint8* target) {
1605   return WritePrimitiveToArray(field_number, value, WriteInt32ToArray, target);
1606 }
WriteInt64ToArray(int field_number,const RepeatedField<int64> & value,uint8 * target)1607 inline uint8* WireFormatLite::WriteInt64ToArray(
1608     int field_number, const RepeatedField<int64>& value, uint8* target) {
1609   return WritePrimitiveToArray(field_number, value, WriteInt64ToArray, target);
1610 }
WriteUInt32ToArray(int field_number,const RepeatedField<uint32> & value,uint8 * target)1611 inline uint8* WireFormatLite::WriteUInt32ToArray(
1612     int field_number, const RepeatedField<uint32>& value, uint8* target) {
1613   return WritePrimitiveToArray(field_number, value, WriteUInt32ToArray, target);
1614 }
WriteUInt64ToArray(int field_number,const RepeatedField<uint64> & value,uint8 * target)1615 inline uint8* WireFormatLite::WriteUInt64ToArray(
1616     int field_number, const RepeatedField<uint64>& value, uint8* target) {
1617   return WritePrimitiveToArray(field_number, value, WriteUInt64ToArray, target);
1618 }
WriteSInt32ToArray(int field_number,const RepeatedField<int32> & value,uint8 * target)1619 inline uint8* WireFormatLite::WriteSInt32ToArray(
1620     int field_number, const RepeatedField<int32>& value, uint8* target) {
1621   return WritePrimitiveToArray(field_number, value, WriteSInt32ToArray, target);
1622 }
WriteSInt64ToArray(int field_number,const RepeatedField<int64> & value,uint8 * target)1623 inline uint8* WireFormatLite::WriteSInt64ToArray(
1624     int field_number, const RepeatedField<int64>& value, uint8* target) {
1625   return WritePrimitiveToArray(field_number, value, WriteSInt64ToArray, target);
1626 }
WriteFixed32ToArray(int field_number,const RepeatedField<uint32> & value,uint8 * target)1627 inline uint8* WireFormatLite::WriteFixed32ToArray(
1628     int field_number, const RepeatedField<uint32>& value, uint8* target) {
1629   return WritePrimitiveToArray(field_number, value, WriteFixed32ToArray,
1630                                target);
1631 }
WriteFixed64ToArray(int field_number,const RepeatedField<uint64> & value,uint8 * target)1632 inline uint8* WireFormatLite::WriteFixed64ToArray(
1633     int field_number, const RepeatedField<uint64>& value, uint8* target) {
1634   return WritePrimitiveToArray(field_number, value, WriteFixed64ToArray,
1635                                target);
1636 }
WriteSFixed32ToArray(int field_number,const RepeatedField<int32> & value,uint8 * target)1637 inline uint8* WireFormatLite::WriteSFixed32ToArray(
1638     int field_number, const RepeatedField<int32>& value, uint8* target) {
1639   return WritePrimitiveToArray(field_number, value, WriteSFixed32ToArray,
1640                                target);
1641 }
WriteSFixed64ToArray(int field_number,const RepeatedField<int64> & value,uint8 * target)1642 inline uint8* WireFormatLite::WriteSFixed64ToArray(
1643     int field_number, const RepeatedField<int64>& value, uint8* target) {
1644   return WritePrimitiveToArray(field_number, value, WriteSFixed64ToArray,
1645                                target);
1646 }
WriteFloatToArray(int field_number,const RepeatedField<float> & value,uint8 * target)1647 inline uint8* WireFormatLite::WriteFloatToArray(
1648     int field_number, const RepeatedField<float>& value, uint8* target) {
1649   return WritePrimitiveToArray(field_number, value, WriteFloatToArray, target);
1650 }
WriteDoubleToArray(int field_number,const RepeatedField<double> & value,uint8 * target)1651 inline uint8* WireFormatLite::WriteDoubleToArray(
1652     int field_number, const RepeatedField<double>& value, uint8* target) {
1653   return WritePrimitiveToArray(field_number, value, WriteDoubleToArray, target);
1654 }
WriteBoolToArray(int field_number,const RepeatedField<bool> & value,uint8 * target)1655 inline uint8* WireFormatLite::WriteBoolToArray(int field_number,
1656                                                const RepeatedField<bool>& value,
1657                                                uint8* target) {
1658   return WritePrimitiveToArray(field_number, value, WriteBoolToArray, target);
1659 }
WriteEnumToArray(int field_number,const RepeatedField<int> & value,uint8 * target)1660 inline uint8* WireFormatLite::WriteEnumToArray(int field_number,
1661                                                const RepeatedField<int>& value,
1662                                                uint8* target) {
1663   return WritePrimitiveToArray(field_number, value, WriteEnumToArray, target);
1664 }
WriteStringToArray(int field_number,const std::string & value,uint8 * target)1665 inline uint8* WireFormatLite::WriteStringToArray(int field_number,
1666                                                  const std::string& value,
1667                                                  uint8* target) {
1668   // String is for UTF-8 text only
1669   // WARNING:  In wire_format.cc, both strings and bytes are handled by
1670   //   WriteString() to avoid code duplication.  If the implementations become
1671   //   different, you will need to update that usage.
1672   target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
1673   return io::CodedOutputStream::WriteStringWithSizeToArray(value, target);
1674 }
WriteBytesToArray(int field_number,const std::string & value,uint8 * target)1675 inline uint8* WireFormatLite::WriteBytesToArray(int field_number,
1676                                                 const std::string& value,
1677                                                 uint8* target) {
1678   target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
1679   return io::CodedOutputStream::WriteStringWithSizeToArray(value, target);
1680 }
1681 
1682 
1683 template <typename MessageType>
InternalWriteGroupToArray(int field_number,const MessageType & value,uint8 * target)1684 inline uint8* WireFormatLite::InternalWriteGroupToArray(
1685     int field_number, const MessageType& value, uint8* target) {
1686   target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
1687   target = value.InternalSerializeWithCachedSizesToArray(target);
1688   return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
1689 }
1690 template <typename MessageType>
InternalWriteMessageToArray(int field_number,const MessageType & value,uint8 * target)1691 inline uint8* WireFormatLite::InternalWriteMessageToArray(
1692     int field_number, const MessageType& value, uint8* target) {
1693   target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
1694   target = io::CodedOutputStream::WriteVarint32ToArray(
1695       static_cast<uint32>(value.GetCachedSize()), target);
1696   return value.InternalSerializeWithCachedSizesToArray(target);
1697 }
1698 
1699 // See comment on ReadGroupNoVirtual to understand the need for this template
1700 // parameter name.
1701 template <typename MessageType_WorkAroundCppLookupDefect>
InternalWriteGroupNoVirtualToArray(int field_number,const MessageType_WorkAroundCppLookupDefect & value,uint8 * target)1702 inline uint8* WireFormatLite::InternalWriteGroupNoVirtualToArray(
1703     int field_number, const MessageType_WorkAroundCppLookupDefect& value,
1704     uint8* target) {
1705   target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
1706   target = value.MessageType_WorkAroundCppLookupDefect::
1707                InternalSerializeWithCachedSizesToArray(target);
1708   return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
1709 }
1710 template <typename MessageType_WorkAroundCppLookupDefect>
InternalWriteMessageNoVirtualToArray(int field_number,const MessageType_WorkAroundCppLookupDefect & value,uint8 * target)1711 inline uint8* WireFormatLite::InternalWriteMessageNoVirtualToArray(
1712     int field_number, const MessageType_WorkAroundCppLookupDefect& value,
1713     uint8* target) {
1714   target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
1715   target = io::CodedOutputStream::WriteVarint32ToArray(
1716       static_cast<uint32>(
1717           value.MessageType_WorkAroundCppLookupDefect::GetCachedSize()),
1718       target);
1719   return value.MessageType_WorkAroundCppLookupDefect::
1720       InternalSerializeWithCachedSizesToArray(target);
1721 }
1722 
1723 // ===================================================================
1724 
Int32Size(int32 value)1725 inline size_t WireFormatLite::Int32Size(int32 value) {
1726   return io::CodedOutputStream::VarintSize32SignExtended(value);
1727 }
Int64Size(int64 value)1728 inline size_t WireFormatLite::Int64Size(int64 value) {
1729   return io::CodedOutputStream::VarintSize64(static_cast<uint64>(value));
1730 }
UInt32Size(uint32 value)1731 inline size_t WireFormatLite::UInt32Size(uint32 value) {
1732   return io::CodedOutputStream::VarintSize32(value);
1733 }
UInt64Size(uint64 value)1734 inline size_t WireFormatLite::UInt64Size(uint64 value) {
1735   return io::CodedOutputStream::VarintSize64(value);
1736 }
SInt32Size(int32 value)1737 inline size_t WireFormatLite::SInt32Size(int32 value) {
1738   return io::CodedOutputStream::VarintSize32(ZigZagEncode32(value));
1739 }
SInt64Size(int64 value)1740 inline size_t WireFormatLite::SInt64Size(int64 value) {
1741   return io::CodedOutputStream::VarintSize64(ZigZagEncode64(value));
1742 }
EnumSize(int value)1743 inline size_t WireFormatLite::EnumSize(int value) {
1744   return io::CodedOutputStream::VarintSize32SignExtended(value);
1745 }
1746 
StringSize(const std::string & value)1747 inline size_t WireFormatLite::StringSize(const std::string& value) {
1748   return LengthDelimitedSize(value.size());
1749 }
BytesSize(const std::string & value)1750 inline size_t WireFormatLite::BytesSize(const std::string& value) {
1751   return LengthDelimitedSize(value.size());
1752 }
1753 
1754 
1755 template <typename MessageType>
GroupSize(const MessageType & value)1756 inline size_t WireFormatLite::GroupSize(const MessageType& value) {
1757   return value.ByteSizeLong();
1758 }
1759 template <typename MessageType>
MessageSize(const MessageType & value)1760 inline size_t WireFormatLite::MessageSize(const MessageType& value) {
1761   return LengthDelimitedSize(value.ByteSizeLong());
1762 }
1763 
1764 // See comment on ReadGroupNoVirtual to understand the need for this template
1765 // parameter name.
1766 template <typename MessageType_WorkAroundCppLookupDefect>
GroupSizeNoVirtual(const MessageType_WorkAroundCppLookupDefect & value)1767 inline size_t WireFormatLite::GroupSizeNoVirtual(
1768     const MessageType_WorkAroundCppLookupDefect& value) {
1769   return value.MessageType_WorkAroundCppLookupDefect::ByteSizeLong();
1770 }
1771 template <typename MessageType_WorkAroundCppLookupDefect>
MessageSizeNoVirtual(const MessageType_WorkAroundCppLookupDefect & value)1772 inline size_t WireFormatLite::MessageSizeNoVirtual(
1773     const MessageType_WorkAroundCppLookupDefect& value) {
1774   return LengthDelimitedSize(
1775       value.MessageType_WorkAroundCppLookupDefect::ByteSizeLong());
1776 }
1777 
LengthDelimitedSize(size_t length)1778 inline size_t WireFormatLite::LengthDelimitedSize(size_t length) {
1779   // The static_cast here prevents an error in certain compiler configurations
1780   // but is not technically correct--if length is too large to fit in a uint32
1781   // then it will be silently truncated. We will need to fix this if we ever
1782   // decide to start supporting serialized messages greater than 2 GiB in size.
1783   return length +
1784          io::CodedOutputStream::VarintSize32(static_cast<uint32>(length));
1785 }
1786 
1787 template <typename MS>
ParseMessageSetItemImpl(io::CodedInputStream * input,MS ms)1788 bool ParseMessageSetItemImpl(io::CodedInputStream* input, MS ms) {
1789   // This method parses a group which should contain two fields:
1790   //   required int32 type_id = 2;
1791   //   required data message = 3;
1792 
1793   uint32 last_type_id = 0;
1794 
1795   // If we see message data before the type_id, we'll append it to this so
1796   // we can parse it later.
1797   std::string message_data;
1798 
1799   while (true) {
1800     const uint32 tag = input->ReadTagNoLastTag();
1801     if (tag == 0) return false;
1802 
1803     switch (tag) {
1804       case WireFormatLite::kMessageSetTypeIdTag: {
1805         uint32 type_id;
1806         if (!input->ReadVarint32(&type_id)) return false;
1807         last_type_id = type_id;
1808 
1809         if (!message_data.empty()) {
1810           // We saw some message data before the type_id.  Have to parse it
1811           // now.
1812           io::CodedInputStream sub_input(
1813               reinterpret_cast<const uint8*>(message_data.data()),
1814               static_cast<int>(message_data.size()));
1815           sub_input.SetRecursionLimit(input->RecursionBudget());
1816           if (!ms.ParseField(last_type_id, &sub_input)) {
1817             return false;
1818           }
1819           message_data.clear();
1820         }
1821 
1822         break;
1823       }
1824 
1825       case WireFormatLite::kMessageSetMessageTag: {
1826         if (last_type_id == 0) {
1827           // We haven't seen a type_id yet.  Append this data to message_data.
1828           uint32 length;
1829           if (!input->ReadVarint32(&length)) return false;
1830           if (static_cast<int32>(length) < 0) return false;
1831           uint32 size = static_cast<uint32>(
1832               length + io::CodedOutputStream::VarintSize32(length));
1833           message_data.resize(size);
1834           auto ptr = reinterpret_cast<uint8*>(&message_data[0]);
1835           ptr = io::CodedOutputStream::WriteVarint32ToArray(length, ptr);
1836           if (!input->ReadRaw(ptr, length)) return false;
1837         } else {
1838           // Already saw type_id, so we can parse this directly.
1839           if (!ms.ParseField(last_type_id, input)) {
1840             return false;
1841           }
1842         }
1843 
1844         break;
1845       }
1846 
1847       case WireFormatLite::kMessageSetItemEndTag: {
1848         return true;
1849       }
1850 
1851       default: {
1852         if (!ms.SkipField(tag, input)) return false;
1853       }
1854     }
1855   }
1856 }
1857 
1858 }  // namespace internal
1859 }  // namespace protobuf
1860 }  // namespace google
1861 
1862 #include <google/protobuf/port_undef.inc>
1863 
1864 #endif  // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__
1865