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/io/coded_stream.h>
47 #include <google/protobuf/message_lite.h>
48 #include <google/protobuf/stubs/port.h>
49 #include <google/protobuf/repeated_field.h>
50 
51 // Do UTF-8 validation on string type in Debug build only
52 #ifndef NDEBUG
53 #define GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
54 #endif
55 
56 // Avoid conflict with iOS where <ConditionalMacros.h> #defines TYPE_BOOL.
57 //
58 // If some one needs the macro TYPE_BOOL in a file that includes this header, it's
59 // possible to bring it back using push/pop_macro as follows.
60 //
61 // #pragma push_macro("TYPE_BOOL")
62 // #include this header and/or all headers that need the macro to be undefined.
63 // #pragma pop_macro("TYPE_BOOL")
64 #undef TYPE_BOOL
65 
66 namespace google {
67 
68 namespace protobuf {
69   template <typename T> class RepeatedField;  // repeated_field.h
70 }
71 
72 namespace protobuf {
73 namespace internal {
74 
75 class StringPieceField;
76 
77 // This class is for internal use by the protocol buffer library and by
78 // protocol-complier-generated message classes.  It must not be called
79 // directly by clients.
80 //
81 // This class contains helpers for implementing the binary protocol buffer
82 // wire format without the need for reflection. Use WireFormat when using
83 // reflection.
84 //
85 // This class is really a namespace that contains only static methods.
86 class LIBPROTOBUF_EXPORT WireFormatLite {
87  public:
88 
89   // -----------------------------------------------------------------
90   // Helper constants and functions related to the format.  These are
91   // mostly meant for internal and generated code to use.
92 
93   // The wire format is composed of a sequence of tag/value pairs, each
94   // of which contains the value of one field (or one element of a repeated
95   // field).  Each tag is encoded as a varint.  The lower bits of the tag
96   // identify its wire type, which specifies the format of the data to follow.
97   // The rest of the bits contain the field number.  Each type of field (as
98   // declared by FieldDescriptor::Type, in descriptor.h) maps to one of
99   // these wire types.  Immediately following each tag is the field's value,
100   // encoded in the format specified by the wire type.  Because the tag
101   // identifies the encoding of this data, it is possible to skip
102   // unrecognized fields for forwards compatibility.
103 
104   enum WireType {
105     WIRETYPE_VARINT           = 0,
106     WIRETYPE_FIXED64          = 1,
107     WIRETYPE_LENGTH_DELIMITED = 2,
108     WIRETYPE_START_GROUP      = 3,
109     WIRETYPE_END_GROUP        = 4,
110     WIRETYPE_FIXED32          = 5,
111   };
112 
113   // Lite alternative to FieldDescriptor::Type.  Must be kept in sync.
114   enum FieldType {
115     TYPE_DOUBLE         = 1,
116     TYPE_FLOAT          = 2,
117     TYPE_INT64          = 3,
118     TYPE_UINT64         = 4,
119     TYPE_INT32          = 5,
120     TYPE_FIXED64        = 6,
121     TYPE_FIXED32        = 7,
122     TYPE_BOOL           = 8,
123     TYPE_STRING         = 9,
124     TYPE_GROUP          = 10,
125     TYPE_MESSAGE        = 11,
126     TYPE_BYTES          = 12,
127     TYPE_UINT32         = 13,
128     TYPE_ENUM           = 14,
129     TYPE_SFIXED32       = 15,
130     TYPE_SFIXED64       = 16,
131     TYPE_SINT32         = 17,
132     TYPE_SINT64         = 18,
133     MAX_FIELD_TYPE      = 18,
134   };
135 
136   // Lite alternative to FieldDescriptor::CppType.  Must be kept in sync.
137   enum CppType {
138     CPPTYPE_INT32       = 1,
139     CPPTYPE_INT64       = 2,
140     CPPTYPE_UINT32      = 3,
141     CPPTYPE_UINT64      = 4,
142     CPPTYPE_DOUBLE      = 5,
143     CPPTYPE_FLOAT       = 6,
144     CPPTYPE_BOOL        = 7,
145     CPPTYPE_ENUM        = 8,
146     CPPTYPE_STRING      = 9,
147     CPPTYPE_MESSAGE     = 10,
148     MAX_CPPTYPE         = 10,
149   };
150 
151   // Helper method to get the CppType for a particular Type.
152   static CppType FieldTypeToCppType(FieldType type);
153 
154   // Given a FieldDescriptor::Type return its WireType
WireTypeForFieldType(WireFormatLite::FieldType type)155   static inline WireFormatLite::WireType WireTypeForFieldType(
156       WireFormatLite::FieldType type) {
157     return kWireTypeForFieldType[type];
158   }
159 
160   // Number of bits in a tag which identify the wire type.
161   static const int kTagTypeBits = 3;
162   // Mask for those bits.
163   static const uint32 kTagTypeMask = (1 << kTagTypeBits) - 1;
164 
165   // Helper functions for encoding and decoding tags.  (Inlined below and in
166   // _inl.h)
167   //
168   // This is different from MakeTag(field->number(), field->type()) in the case
169   // of packed repeated fields.
170   static uint32 MakeTag(int field_number, WireType type);
171   static WireType GetTagWireType(uint32 tag);
172   static int GetTagFieldNumber(uint32 tag);
173 
174   // Compute the byte size of a tag.  For groups, this includes both the start
175   // and end tags.
176   static inline size_t TagSize(int field_number,
177                                WireFormatLite::FieldType type);
178 
179   // Skips a field value with the given tag.  The input should start
180   // positioned immediately after the tag.  Skipped values are simply discarded,
181   // not recorded anywhere.  See WireFormat::SkipField() for a version that
182   // records to an UnknownFieldSet.
183   static bool SkipField(io::CodedInputStream* input, uint32 tag);
184 
185   // Skips a field value with the given tag.  The input should start
186   // positioned immediately after the tag. Skipped values are recorded to a
187   // CodedOutputStream.
188   static bool SkipField(io::CodedInputStream* input, uint32 tag,
189                         io::CodedOutputStream* output);
190 
191   // Reads and ignores a message from the input.  Skipped values are simply
192   // discarded, not recorded anywhere.  See WireFormat::SkipMessage() for a
193   // version that records to an UnknownFieldSet.
194   static bool SkipMessage(io::CodedInputStream* input);
195 
196   // Reads and ignores a message from the input.  Skipped values are recorded
197   // to a CodedOutputStream.
198   static bool SkipMessage(io::CodedInputStream* input,
199                           io::CodedOutputStream* output);
200 
201 // This macro does the same thing as WireFormatLite::MakeTag(), but the
202 // result is usable as a compile-time constant, which makes it usable
203 // as a switch case or a template input.  WireFormatLite::MakeTag() is more
204 // type-safe, though, so prefer it if possible.
205 #define GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(FIELD_NUMBER, TYPE)                  \
206   static_cast<uint32>(                                                   \
207     (static_cast<uint32>(FIELD_NUMBER) << ::google::protobuf::internal::WireFormatLite::kTagTypeBits) \
208       | (TYPE))
209 
210   // These are the tags for the old MessageSet format, which was defined as:
211   //   message MessageSet {
212   //     repeated group Item = 1 {
213   //       required int32 type_id = 2;
214   //       required string message = 3;
215   //     }
216   //   }
217   static const int kMessageSetItemNumber = 1;
218   static const int kMessageSetTypeIdNumber = 2;
219   static const int kMessageSetMessageNumber = 3;
220   static const int kMessageSetItemStartTag =
221     GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetItemNumber,
222                                 WireFormatLite::WIRETYPE_START_GROUP);
223   static const int kMessageSetItemEndTag =
224     GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetItemNumber,
225                                 WireFormatLite::WIRETYPE_END_GROUP);
226   static const int kMessageSetTypeIdTag =
227     GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetTypeIdNumber,
228                                 WireFormatLite::WIRETYPE_VARINT);
229   static const int kMessageSetMessageTag =
230     GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetMessageNumber,
231                                 WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
232 
233   // Byte size of all tags of a MessageSet::Item combined.
234   static const size_t kMessageSetItemTagsSize;
235 
236   // Helper functions for converting between floats/doubles and IEEE-754
237   // uint32s/uint64s so that they can be written.  (Assumes your platform
238   // uses IEEE-754 floats.)
239   static uint32 EncodeFloat(float value);
240   static float DecodeFloat(uint32 value);
241   static uint64 EncodeDouble(double value);
242   static double DecodeDouble(uint64 value);
243 
244   // Helper functions for mapping signed integers to unsigned integers in
245   // such a way that numbers with small magnitudes will encode to smaller
246   // varints.  If you simply static_cast a negative number to an unsigned
247   // number and varint-encode it, it will always take 10 bytes, defeating
248   // the purpose of varint.  So, for the "sint32" and "sint64" field types,
249   // we ZigZag-encode the values.
250   static uint32 ZigZagEncode32(int32 n);
251   static int32  ZigZagDecode32(uint32 n);
252   static uint64 ZigZagEncode64(int64 n);
253   static int64  ZigZagDecode64(uint64 n);
254 
255   // =================================================================
256   // Methods for reading/writing individual field.  The implementations
257   // of these methods are defined in wire_format_lite_inl.h; you must #include
258   // that file to use these.
259 
260 #ifdef NDEBUG
261 #define INL GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE
262 #else
263 // Avoid excessive inlining in non-optimized builds. Without other optimizations
264 // the inlining is not going to provide benefits anyway and the huge resulting
265 // functions, especially in the proto-generated serialization functions, produce
266 // stack frames so large that many tests run into stack overflows (b/32192897).
267 #define INL
268 #endif
269 
270   // Read fields, not including tags.  The assumption is that you already
271   // read the tag to determine what field to read.
272 
273   // For primitive fields, we just use a templatized routine parameterized by
274   // the represented type and the FieldType. These are specialized with the
275   // appropriate definition for each declared type.
276   template <typename CType, enum FieldType DeclaredType>
277   INL static bool ReadPrimitive(io::CodedInputStream* input, CType* value);
278 
279   // Reads repeated primitive values, with optimizations for repeats.
280   // tag_size and tag should both be compile-time constants provided by the
281   // protocol compiler.
282   template <typename CType, enum FieldType DeclaredType>
283   INL static bool ReadRepeatedPrimitive(int tag_size, uint32 tag,
284                                         io::CodedInputStream* input,
285                                         RepeatedField<CType>* value);
286 
287   // Identical to ReadRepeatedPrimitive, except will not inline the
288   // implementation.
289   template <typename CType, enum FieldType DeclaredType>
290   static bool ReadRepeatedPrimitiveNoInline(int tag_size, uint32 tag,
291                                             io::CodedInputStream* input,
292                                             RepeatedField<CType>* value);
293 
294   // Reads a primitive value directly from the provided buffer. It returns a
295   // pointer past the segment of data that was read.
296   //
297   // This is only implemented for the types with fixed wire size, e.g.
298   // float, double, and the (s)fixed* types.
299   template <typename CType, enum FieldType DeclaredType> INL
300   static const uint8* ReadPrimitiveFromArray(const uint8* buffer, CType* value);
301 
302   // Reads a primitive packed field.
303   //
304   // This is only implemented for packable types.
305   template <typename CType, enum FieldType DeclaredType>
306   INL static bool ReadPackedPrimitive(io::CodedInputStream* input,
307                                       RepeatedField<CType>* value);
308 
309   // Identical to ReadPackedPrimitive, except will not inline the
310   // implementation.
311   template <typename CType, enum FieldType DeclaredType>
312   static bool ReadPackedPrimitiveNoInline(io::CodedInputStream* input,
313                                           RepeatedField<CType>* value);
314 
315   // Read a packed enum field. If the is_valid function is not NULL, values for
316   // which is_valid(value) returns false are silently dropped.
317   static bool ReadPackedEnumNoInline(io::CodedInputStream* input,
318                                      bool (*is_valid)(int),
319                                      RepeatedField<int>* values);
320 
321   // Read a packed enum field. If the is_valid function is not NULL, values for
322   // which is_valid(value) returns false are appended to unknown_fields_stream.
323   static bool ReadPackedEnumPreserveUnknowns(
324       io::CodedInputStream* input, int field_number, bool (*is_valid)(int),
325       io::CodedOutputStream* unknown_fields_stream, RepeatedField<int>* values);
326 
327   // Read a string.  ReadString(..., string* value) requires an existing string.
328   static inline bool ReadString(io::CodedInputStream* input, string* value);
329   // ReadString(..., string** p) is internal-only, and should only be called
330   // from generated code. It starts by setting *p to "new string"
331   // if *p == &GetEmptyStringAlreadyInited().  It then invokes
332   // ReadString(io::CodedInputStream* input, *p).  This is useful for reducing
333   // code size.
334   static inline bool ReadString(io::CodedInputStream* input, string** p);
335   // Analogous to ReadString().
336   static bool ReadBytes(io::CodedInputStream* input, string* value);
337   static bool ReadBytes(io::CodedInputStream* input, string** p);
338 
339   enum Operation {
340     PARSE = 0,
341     SERIALIZE = 1,
342   };
343 
344   // Returns true if the data is valid UTF-8.
345   static bool VerifyUtf8String(const char* data, int size,
346                                Operation op,
347                                const char* field_name);
348 
349   static inline bool ReadGroup(int field_number, io::CodedInputStream* input,
350                                MessageLite* value);
351   static inline bool ReadMessage(io::CodedInputStream* input,
352                                  MessageLite* value);
353 
354   // Like above, but de-virtualize the call to MergePartialFromCodedStream().
355   // The pointer must point at an instance of MessageType, *not* a subclass (or
356   // the subclass must not override MergePartialFromCodedStream()).
357   template <typename MessageType>
358   static inline bool ReadGroupNoVirtual(int field_number,
359                                         io::CodedInputStream* input,
360                                         MessageType* value);
361   template<typename MessageType>
362   static inline bool ReadMessageNoVirtual(io::CodedInputStream* input,
363                                           MessageType* value);
364 
365   // The same, but do not modify input's recursion depth.  This is useful
366   // when reading a bunch of groups or messages in a loop, because then the
367   // recursion depth can be incremented before the loop and decremented after.
368   template<typename MessageType>
369   static inline bool ReadGroupNoVirtualNoRecursionDepth(
370       int field_number, io::CodedInputStream* input, MessageType* value);
371 
372   template<typename MessageType>
373   static inline bool ReadMessageNoVirtualNoRecursionDepth(
374       io::CodedInputStream* input, MessageType* value);
375 
376   // Write a tag.  The Write*() functions typically include the tag, so
377   // normally there's no need to call this unless using the Write*NoTag()
378   // variants.
379   INL static void WriteTag(int field_number, WireType type,
380                            io::CodedOutputStream* output);
381 
382   // Write fields, without tags.
383   INL static void WriteInt32NoTag(int32 value, io::CodedOutputStream* output);
384   INL static void WriteInt64NoTag(int64 value, io::CodedOutputStream* output);
385   INL static void WriteUInt32NoTag(uint32 value, io::CodedOutputStream* output);
386   INL static void WriteUInt64NoTag(uint64 value, io::CodedOutputStream* output);
387   INL static void WriteSInt32NoTag(int32 value, io::CodedOutputStream* output);
388   INL static void WriteSInt64NoTag(int64 value, io::CodedOutputStream* output);
389   INL static void WriteFixed32NoTag(uint32 value,
390                                     io::CodedOutputStream* output);
391   INL static void WriteFixed64NoTag(uint64 value,
392                                     io::CodedOutputStream* output);
393   INL static void WriteSFixed32NoTag(int32 value,
394                                      io::CodedOutputStream* output);
395   INL static void WriteSFixed64NoTag(int64 value,
396                                      io::CodedOutputStream* output);
397   INL static void WriteFloatNoTag(float value, io::CodedOutputStream* output);
398   INL static void WriteDoubleNoTag(double value, io::CodedOutputStream* output);
399   INL static void WriteBoolNoTag(bool value, io::CodedOutputStream* output);
400   INL static void WriteEnumNoTag(int value, io::CodedOutputStream* output);
401 
402   // Write array of primitive fields, without tags
403   static void WriteFloatArray(const float* a, int n,
404                               io::CodedOutputStream* output);
405   static void WriteDoubleArray(const double* a, int n,
406                                io::CodedOutputStream* output);
407   static void WriteFixed32Array(const uint32* a, int n,
408                                 io::CodedOutputStream* output);
409   static void WriteFixed64Array(const uint64* a, int n,
410                                 io::CodedOutputStream* output);
411   static void WriteSFixed32Array(const int32* a, int n,
412                                  io::CodedOutputStream* output);
413   static void WriteSFixed64Array(const int64* a, int n,
414                                  io::CodedOutputStream* output);
415   static void WriteBoolArray(const bool* a, int n,
416                              io::CodedOutputStream* output);
417 
418   // Write fields, including tags.
419   static void WriteInt32(int field_number, int32 value,
420                          io::CodedOutputStream* output);
421   static void WriteInt64(int field_number, int64 value,
422                          io::CodedOutputStream* output);
423   static void WriteUInt32(int field_number, uint32 value,
424                           io::CodedOutputStream* output);
425   static void WriteUInt64(int field_number, uint64 value,
426                           io::CodedOutputStream* output);
427   static void WriteSInt32(int field_number, int32 value,
428                           io::CodedOutputStream* output);
429   static void WriteSInt64(int field_number, int64 value,
430                           io::CodedOutputStream* output);
431   static void WriteFixed32(int field_number, uint32 value,
432                            io::CodedOutputStream* output);
433   static void WriteFixed64(int field_number, uint64 value,
434                            io::CodedOutputStream* output);
435   static void WriteSFixed32(int field_number, int32 value,
436                             io::CodedOutputStream* output);
437   static void WriteSFixed64(int field_number, int64 value,
438                             io::CodedOutputStream* output);
439   static void WriteFloat(int field_number, float value,
440                          io::CodedOutputStream* output);
441   static void WriteDouble(int field_number, double value,
442                           io::CodedOutputStream* output);
443   static void WriteBool(int field_number, bool value,
444                         io::CodedOutputStream* output);
445   static void WriteEnum(int field_number, int value,
446                         io::CodedOutputStream* output);
447 
448   static void WriteString(int field_number, const string& value,
449                           io::CodedOutputStream* output);
450   static void WriteBytes(int field_number, const string& value,
451                          io::CodedOutputStream* output);
452   static void WriteStringMaybeAliased(int field_number, const string& value,
453                                       io::CodedOutputStream* output);
454   static void WriteBytesMaybeAliased(int field_number, const string& value,
455                                      io::CodedOutputStream* output);
456 
457   static void WriteGroup(int field_number, const MessageLite& value,
458                          io::CodedOutputStream* output);
459   static void WriteMessage(int field_number, const MessageLite& value,
460                            io::CodedOutputStream* output);
461   // Like above, but these will check if the output stream has enough
462   // space to write directly to a flat array.
463   static void WriteGroupMaybeToArray(int field_number, const MessageLite& value,
464                                      io::CodedOutputStream* output);
465   static void WriteMessageMaybeToArray(int field_number,
466                                        const MessageLite& value,
467                                        io::CodedOutputStream* output);
468 
469   // Like above, but de-virtualize the call to SerializeWithCachedSizes().  The
470   // pointer must point at an instance of MessageType, *not* a subclass (or
471   // the subclass must not override SerializeWithCachedSizes()).
472   template <typename MessageType>
473   static inline void WriteGroupNoVirtual(int field_number,
474                                          const MessageType& value,
475                                          io::CodedOutputStream* output);
476   template <typename MessageType>
477   static inline void WriteMessageNoVirtual(int field_number,
478                                            const MessageType& value,
479                                            io::CodedOutputStream* output);
480 
481   // Like above, but use only *ToArray methods of CodedOutputStream.
482   INL static uint8* WriteTagToArray(int field_number, WireType type,
483                                     uint8* target);
484 
485   // Write fields, without tags.
486   INL static uint8* WriteInt32NoTagToArray(int32 value, uint8* target);
487   INL static uint8* WriteInt64NoTagToArray(int64 value, uint8* target);
488   INL static uint8* WriteUInt32NoTagToArray(uint32 value, uint8* target);
489   INL static uint8* WriteUInt64NoTagToArray(uint64 value, uint8* target);
490   INL static uint8* WriteSInt32NoTagToArray(int32 value, uint8* target);
491   INL static uint8* WriteSInt64NoTagToArray(int64 value, uint8* target);
492   INL static uint8* WriteFixed32NoTagToArray(uint32 value, uint8* target);
493   INL static uint8* WriteFixed64NoTagToArray(uint64 value, uint8* target);
494   INL static uint8* WriteSFixed32NoTagToArray(int32 value, uint8* target);
495   INL static uint8* WriteSFixed64NoTagToArray(int64 value, uint8* target);
496   INL static uint8* WriteFloatNoTagToArray(float value, uint8* target);
497   INL static uint8* WriteDoubleNoTagToArray(double value, uint8* target);
498   INL static uint8* WriteBoolNoTagToArray(bool value, uint8* target);
499   INL static uint8* WriteEnumNoTagToArray(int value, uint8* target);
500 
501   // Write fields, without tags.  These require that value.size() > 0.
502   template<typename T>
503   INL static uint8* WritePrimitiveNoTagToArray(
504       const RepeatedField<T>& value,
505       uint8* (*Writer)(T, uint8*), uint8* target);
506   template<typename T>
507   INL static uint8* WriteFixedNoTagToArray(
508       const RepeatedField<T>& value,
509       uint8* (*Writer)(T, uint8*), uint8* target);
510 
511   INL static uint8* WriteInt32NoTagToArray(
512       const RepeatedField< int32>& value, uint8* output);
513   INL static uint8* WriteInt64NoTagToArray(
514       const RepeatedField< int64>& value, uint8* output);
515   INL static uint8* WriteUInt32NoTagToArray(
516       const RepeatedField<uint32>& value, uint8* output);
517   INL static uint8* WriteUInt64NoTagToArray(
518       const RepeatedField<uint64>& value, uint8* output);
519   INL static uint8* WriteSInt32NoTagToArray(
520       const RepeatedField< int32>& value, uint8* output);
521   INL static uint8* WriteSInt64NoTagToArray(
522       const RepeatedField< int64>& value, uint8* output);
523   INL static uint8* WriteFixed32NoTagToArray(
524       const RepeatedField<uint32>& value, uint8* output);
525   INL static uint8* WriteFixed64NoTagToArray(
526       const RepeatedField<uint64>& value, uint8* output);
527   INL static uint8* WriteSFixed32NoTagToArray(
528       const RepeatedField< int32>& value, uint8* output);
529   INL static uint8* WriteSFixed64NoTagToArray(
530       const RepeatedField< int64>& value, uint8* output);
531   INL static uint8* WriteFloatNoTagToArray(
532       const RepeatedField< float>& value, uint8* output);
533   INL static uint8* WriteDoubleNoTagToArray(
534       const RepeatedField<double>& value, uint8* output);
535   INL static uint8* WriteBoolNoTagToArray(
536       const RepeatedField<  bool>& value, uint8* output);
537   INL static uint8* WriteEnumNoTagToArray(
538       const RepeatedField<   int>& value, uint8* output);
539 
540   // Write fields, including tags.
541   INL static uint8* WriteInt32ToArray(int field_number, int32 value,
542                                       uint8* target);
543   INL static uint8* WriteInt64ToArray(int field_number, int64 value,
544                                       uint8* target);
545   INL static uint8* WriteUInt32ToArray(int field_number, uint32 value,
546                                        uint8* target);
547   INL static uint8* WriteUInt64ToArray(int field_number, uint64 value,
548                                        uint8* target);
549   INL static uint8* WriteSInt32ToArray(int field_number, int32 value,
550                                        uint8* target);
551   INL static uint8* WriteSInt64ToArray(int field_number, int64 value,
552                                        uint8* target);
553   INL static uint8* WriteFixed32ToArray(int field_number, uint32 value,
554                                         uint8* target);
555   INL static uint8* WriteFixed64ToArray(int field_number, uint64 value,
556                                         uint8* target);
557   INL static uint8* WriteSFixed32ToArray(int field_number, int32 value,
558                                          uint8* target);
559   INL static uint8* WriteSFixed64ToArray(int field_number, int64 value,
560                                          uint8* target);
561   INL static uint8* WriteFloatToArray(int field_number, float value,
562                                       uint8* target);
563   INL static uint8* WriteDoubleToArray(int field_number, double value,
564                                        uint8* target);
565   INL static uint8* WriteBoolToArray(int field_number, bool value,
566                                      uint8* target);
567   INL static uint8* WriteEnumToArray(int field_number, int value,
568                                      uint8* target);
569 
570   template<typename T>
571   INL static uint8* WritePrimitiveToArray(
572       int field_number,
573       const RepeatedField<T>& value,
574       uint8* (*Writer)(int, T, uint8*), uint8* target);
575 
576   INL static uint8* WriteInt32ToArray(
577       int field_number, const RepeatedField< int32>& value, uint8* output);
578   INL static uint8* WriteInt64ToArray(
579       int field_number, const RepeatedField< int64>& value, uint8* output);
580   INL static uint8* WriteUInt32ToArray(
581       int field_number, const RepeatedField<uint32>& value, uint8* output);
582   INL static uint8* WriteUInt64ToArray(
583       int field_number, const RepeatedField<uint64>& value, uint8* output);
584   INL static uint8* WriteSInt32ToArray(
585       int field_number, const RepeatedField< int32>& value, uint8* output);
586   INL static uint8* WriteSInt64ToArray(
587       int field_number, const RepeatedField< int64>& value, uint8* output);
588   INL static uint8* WriteFixed32ToArray(
589       int field_number, const RepeatedField<uint32>& value, uint8* output);
590   INL static uint8* WriteFixed64ToArray(
591       int field_number, const RepeatedField<uint64>& value, uint8* output);
592   INL static uint8* WriteSFixed32ToArray(
593       int field_number, const RepeatedField< int32>& value, uint8* output);
594   INL static uint8* WriteSFixed64ToArray(
595       int field_number, const RepeatedField< int64>& value, uint8* output);
596   INL static uint8* WriteFloatToArray(
597       int field_number, const RepeatedField< float>& value, uint8* output);
598   INL static uint8* WriteDoubleToArray(
599       int field_number, const RepeatedField<double>& value, uint8* output);
600   INL static uint8* WriteBoolToArray(
601       int field_number, const RepeatedField<  bool>& value, uint8* output);
602   INL static uint8* WriteEnumToArray(
603       int field_number, const RepeatedField<   int>& value, uint8* output);
604 
605   INL static uint8* WriteStringToArray(int field_number, const string& value,
606                                        uint8* target);
607   INL static uint8* WriteBytesToArray(int field_number, const string& value,
608                                       uint8* target);
609 
610   // Whether to serialize deterministically (e.g., map keys are
611   // sorted) is a property of a CodedOutputStream, and in the process
612   // of serialization, the "ToArray" variants may be invoked.  But they don't
613   // have a CodedOutputStream available, so they get an additional parameter
614   // telling them whether to serialize deterministically.
615   INL static uint8* InternalWriteGroupToArray(int field_number,
616                                               const MessageLite& value,
617                                               bool deterministic,
618                                               uint8* target);
619   INL static uint8* InternalWriteMessageToArray(int field_number,
620                                                 const MessageLite& value,
621                                                 bool deterministic,
622                                                 uint8* target);
623 
624   // Like above, but de-virtualize the call to SerializeWithCachedSizes().  The
625   // pointer must point at an instance of MessageType, *not* a subclass (or
626   // the subclass must not override SerializeWithCachedSizes()).
627   template <typename MessageType>
628   INL static uint8* InternalWriteGroupNoVirtualToArray(int field_number,
629                                                        const MessageType& value,
630                                                        bool deterministic,
631                                                        uint8* target);
632   template <typename MessageType>
633   INL static uint8* InternalWriteMessageNoVirtualToArray(
634       int field_number, const MessageType& value, bool deterministic,
635       uint8* target);
636 
637   // For backward-compatibility, the last four methods also have versions
638   // that are non-deterministic always.
WriteGroupToArray(int field_number,const MessageLite & value,uint8 * target)639   INL static uint8* WriteGroupToArray(int field_number,
640                                       const MessageLite& value, uint8* target) {
641     return InternalWriteGroupToArray(field_number, value, false, target);
642   }
WriteMessageToArray(int field_number,const MessageLite & value,uint8 * target)643   INL static uint8* WriteMessageToArray(int field_number,
644                                         const MessageLite& value,
645                                         uint8* target) {
646     return InternalWriteMessageToArray(field_number, value, false, target);
647   }
648   template <typename MessageType>
WriteGroupNoVirtualToArray(int field_number,const MessageType & value,uint8 * target)649   INL static uint8* WriteGroupNoVirtualToArray(int field_number,
650                                                const MessageType& value,
651                                                uint8* target) {
652     return InternalWriteGroupNoVirtualToArray(field_number, value, false,
653                                               target);
654   }
655   template <typename MessageType>
WriteMessageNoVirtualToArray(int field_number,const MessageType & value,uint8 * target)656   INL static uint8* WriteMessageNoVirtualToArray(int field_number,
657                                                  const MessageType& value,
658                                                  uint8* target) {
659     return InternalWriteMessageNoVirtualToArray(field_number, value, false,
660                                                 target);
661   }
662 
663 #undef INL
664 
665   // Compute the byte size of a field.  The XxSize() functions do NOT include
666   // the tag, so you must also call TagSize().  (This is because, for repeated
667   // fields, you should only call TagSize() once and multiply it by the element
668   // count, but you may have to call XxSize() for each individual element.)
669   static inline size_t Int32Size   ( int32 value);
670   static inline size_t Int64Size   ( int64 value);
671   static inline size_t UInt32Size  (uint32 value);
672   static inline size_t UInt64Size  (uint64 value);
673   static inline size_t SInt32Size  ( int32 value);
674   static inline size_t SInt64Size  ( int64 value);
675   static inline size_t EnumSize    (   int value);
676 
677   static        size_t Int32Size (const RepeatedField< int32>& value);
678   static inline size_t Int64Size (const RepeatedField< int64>& value);
679   static        size_t UInt32Size(const RepeatedField<uint32>& value);
680   static inline size_t UInt64Size(const RepeatedField<uint64>& value);
681   static        size_t SInt32Size(const RepeatedField< int32>& value);
682   static inline size_t SInt64Size(const RepeatedField< int64>& value);
683   static        size_t EnumSize  (const RepeatedField<   int>& value);
684 
685   // These types always have the same size.
686   static const size_t kFixed32Size  = 4;
687   static const size_t kFixed64Size  = 8;
688   static const size_t kSFixed32Size = 4;
689   static const size_t kSFixed64Size = 8;
690   static const size_t kFloatSize    = 4;
691   static const size_t kDoubleSize   = 8;
692   static const size_t kBoolSize     = 1;
693 
694   static inline size_t StringSize(const string& value);
695   static inline size_t BytesSize (const string& value);
696 
697   static inline size_t GroupSize  (const MessageLite& value);
698   static inline size_t MessageSize(const MessageLite& value);
699 
700   // Like above, but de-virtualize the call to ByteSize().  The
701   // pointer must point at an instance of MessageType, *not* a subclass (or
702   // the subclass must not override ByteSize()).
703   template<typename MessageType>
704   static inline size_t GroupSizeNoVirtual  (const MessageType& value);
705   template<typename MessageType>
706   static inline size_t MessageSizeNoVirtual(const MessageType& value);
707 
708   // Given the length of data, calculate the byte size of the data on the
709   // wire if we encode the data as a length delimited field.
710   static inline size_t LengthDelimitedSize(size_t length);
711 
712  private:
713   // A helper method for the repeated primitive reader. This method has
714   // optimizations for primitive types that have fixed size on the wire, and
715   // can be read using potentially faster paths.
716   template <typename CType, enum FieldType DeclaredType>
717   GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE
718   static bool ReadRepeatedFixedSizePrimitive(
719       int tag_size,
720       uint32 tag,
721       google::protobuf::io::CodedInputStream* input,
722       RepeatedField<CType>* value);
723 
724   // Like ReadRepeatedFixedSizePrimitive but for packed primitive fields.
725   template <typename CType, enum FieldType DeclaredType>
726   GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE
727   static bool ReadPackedFixedSizePrimitive(
728       google::protobuf::io::CodedInputStream* input, RepeatedField<CType>* value);
729 
730   static const CppType kFieldTypeToCppTypeMap[];
731   static const WireFormatLite::WireType kWireTypeForFieldType[];
732 
733   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormatLite);
734 };
735 
736 // A class which deals with unknown values.  The default implementation just
737 // discards them.  WireFormat defines a subclass which writes to an
738 // UnknownFieldSet.  This class is used by ExtensionSet::ParseField(), since
739 // ExtensionSet is part of the lite library but UnknownFieldSet is not.
740 class LIBPROTOBUF_EXPORT FieldSkipper {
741  public:
FieldSkipper()742   FieldSkipper() {}
~FieldSkipper()743   virtual ~FieldSkipper() {}
744 
745   // Skip a field whose tag has already been consumed.
746   virtual bool SkipField(io::CodedInputStream* input, uint32 tag);
747 
748   // Skip an entire message or group, up to an end-group tag (which is consumed)
749   // or end-of-stream.
750   virtual bool SkipMessage(io::CodedInputStream* input);
751 
752   // Deal with an already-parsed unrecognized enum value.  The default
753   // implementation does nothing, but the UnknownFieldSet-based implementation
754   // saves it as an unknown varint.
755   virtual void SkipUnknownEnum(int field_number, int value);
756 };
757 
758 // Subclass of FieldSkipper which saves skipped fields to a CodedOutputStream.
759 
760 class LIBPROTOBUF_EXPORT CodedOutputStreamFieldSkipper : public FieldSkipper {
761  public:
CodedOutputStreamFieldSkipper(io::CodedOutputStream * unknown_fields)762   explicit CodedOutputStreamFieldSkipper(io::CodedOutputStream* unknown_fields)
763       : unknown_fields_(unknown_fields) {}
~CodedOutputStreamFieldSkipper()764   virtual ~CodedOutputStreamFieldSkipper() {}
765 
766   // implements FieldSkipper -----------------------------------------
767   virtual bool SkipField(io::CodedInputStream* input, uint32 tag);
768   virtual bool SkipMessage(io::CodedInputStream* input);
769   virtual void SkipUnknownEnum(int field_number, int value);
770 
771  protected:
772   io::CodedOutputStream* unknown_fields_;
773 };
774 
775 
776 // inline methods ====================================================
777 
778 inline WireFormatLite::CppType
FieldTypeToCppType(FieldType type)779 WireFormatLite::FieldTypeToCppType(FieldType type) {
780   return kFieldTypeToCppTypeMap[type];
781 }
782 
MakeTag(int field_number,WireType type)783 inline uint32 WireFormatLite::MakeTag(int field_number, WireType type) {
784   return GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(field_number, type);
785 }
786 
GetTagWireType(uint32 tag)787 inline WireFormatLite::WireType WireFormatLite::GetTagWireType(uint32 tag) {
788   return static_cast<WireType>(tag & kTagTypeMask);
789 }
790 
GetTagFieldNumber(uint32 tag)791 inline int WireFormatLite::GetTagFieldNumber(uint32 tag) {
792   return static_cast<int>(tag >> kTagTypeBits);
793 }
794 
TagSize(int field_number,WireFormatLite::FieldType type)795 inline size_t WireFormatLite::TagSize(int field_number,
796                                       WireFormatLite::FieldType type) {
797   size_t result = io::CodedOutputStream::VarintSize32(
798     static_cast<uint32>(field_number << kTagTypeBits));
799   if (type == TYPE_GROUP) {
800     // Groups have both a start and an end tag.
801     return result * 2;
802   } else {
803     return result;
804   }
805 }
806 
EncodeFloat(float value)807 inline uint32 WireFormatLite::EncodeFloat(float value) {
808   union {float f; uint32 i;};
809   f = value;
810   return i;
811 }
812 
DecodeFloat(uint32 value)813 inline float WireFormatLite::DecodeFloat(uint32 value) {
814   union {float f; uint32 i;};
815   i = value;
816   return f;
817 }
818 
EncodeDouble(double value)819 inline uint64 WireFormatLite::EncodeDouble(double value) {
820   union {double f; uint64 i;};
821   f = value;
822   return i;
823 }
824 
DecodeDouble(uint64 value)825 inline double WireFormatLite::DecodeDouble(uint64 value) {
826   union {double f; uint64 i;};
827   i = value;
828   return f;
829 }
830 
831 // ZigZag Transform:  Encodes signed integers so that they can be
832 // effectively used with varint encoding.
833 //
834 // varint operates on unsigned integers, encoding smaller numbers into
835 // fewer bytes.  If you try to use it on a signed integer, it will treat
836 // this number as a very large unsigned integer, which means that even
837 // small signed numbers like -1 will take the maximum number of bytes
838 // (10) to encode.  ZigZagEncode() maps signed integers to unsigned
839 // in such a way that those with a small absolute value will have smaller
840 // encoded values, making them appropriate for encoding using varint.
841 //
842 //       int32 ->     uint32
843 // -------------------------
844 //           0 ->          0
845 //          -1 ->          1
846 //           1 ->          2
847 //          -2 ->          3
848 //         ... ->        ...
849 //  2147483647 -> 4294967294
850 // -2147483648 -> 4294967295
851 //
852 //        >> encode >>
853 //        << decode <<
854 
ZigZagEncode32(int32 n)855 inline uint32 WireFormatLite::ZigZagEncode32(int32 n) {
856   // Note:  the right-shift must be arithmetic
857   // Note:  left shift must be unsigned because of overflow
858   return (static_cast<uint32>(n) << 1) ^ static_cast<uint32>(n >> 31);
859 }
860 
ZigZagDecode32(uint32 n)861 inline int32 WireFormatLite::ZigZagDecode32(uint32 n) {
862   // Note:  Using unsigned types prevent undefined behavior
863   return static_cast<int32>((n >> 1) ^ -(n & 1));
864 }
865 
ZigZagEncode64(int64 n)866 inline uint64 WireFormatLite::ZigZagEncode64(int64 n) {
867   // Note:  the right-shift must be arithmetic
868   // Note:  left shift must be unsigned because of overflow
869   return (static_cast<uint64>(n) << 1) ^ static_cast<uint64>(n >> 63);
870 }
871 
ZigZagDecode64(uint64 n)872 inline int64 WireFormatLite::ZigZagDecode64(uint64 n) {
873   // Note:  Using unsigned types prevent undefined behavior
874   return static_cast<int64>((n >> 1) ^ -(n & 1));
875 }
876 
877 // String is for UTF-8 text only, but, even so, ReadString() can simply
878 // call ReadBytes().
879 
ReadString(io::CodedInputStream * input,string * value)880 inline bool WireFormatLite::ReadString(io::CodedInputStream* input,
881                                        string* value) {
882   return ReadBytes(input, value);
883 }
884 
ReadString(io::CodedInputStream * input,string ** p)885 inline bool WireFormatLite::ReadString(io::CodedInputStream* input,
886                                        string** p) {
887   return ReadBytes(input, p);
888 }
889 
890 }  // namespace internal
891 }  // namespace protobuf
892 
893 }  // namespace google
894 #endif  // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__
895