Line data Source code
1 : // Protocol Buffers - Google's data interchange format
2 : // Copyright 2008 Google Inc. All rights reserved.
3 : // https://developers.google.com/protocol-buffers/
4 : //
5 : // Redistribution and use in source and binary forms, with or without
6 : // modification, are permitted provided that the following conditions are
7 : // met:
8 : //
9 : // * Redistributions of source code must retain the above copyright
10 : // notice, this list of conditions and the following disclaimer.
11 : // * Redistributions in binary form must reproduce the above
12 : // copyright notice, this list of conditions and the following disclaimer
13 : // in the documentation and/or other materials provided with the
14 : // distribution.
15 : // * Neither the name of Google Inc. nor the names of its
16 : // contributors may be used to endorse or promote products derived from
17 : // this software without specific prior written permission.
18 : //
19 : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 :
31 : // Author: kenton@google.com (Kenton Varda)
32 : // 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
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 : template <typename MessageType>
350 : static inline bool ReadGroup(int field_number, io::CodedInputStream* input,
351 : MessageType* value);
352 :
353 : template <typename MessageType>
354 : static inline bool ReadMessage(io::CodedInputStream* input,
355 : MessageType* value);
356 :
357 : // Do not use.
358 : template <typename MessageType>
359 : static inline bool ReadGroupNoVirtual(int field_number,
360 : io::CodedInputStream* input,
361 : MessageType* value) {
362 : return ReadGroup(field_number, input, value);
363 : }
364 :
365 : template<typename MessageType>
366 : static inline bool ReadMessageNoVirtual(io::CodedInputStream* input,
367 : MessageType* value) {
368 : return ReadMessage(input, value);
369 : }
370 :
371 : // Write a tag. The Write*() functions typically include the tag, so
372 : // normally there's no need to call this unless using the Write*NoTag()
373 : // variants.
374 : INL static void WriteTag(int field_number, WireType type,
375 : io::CodedOutputStream* output);
376 :
377 : // Write fields, without tags.
378 : INL static void WriteInt32NoTag(int32 value, io::CodedOutputStream* output);
379 : INL static void WriteInt64NoTag(int64 value, io::CodedOutputStream* output);
380 : INL static void WriteUInt32NoTag(uint32 value, io::CodedOutputStream* output);
381 : INL static void WriteUInt64NoTag(uint64 value, io::CodedOutputStream* output);
382 : INL static void WriteSInt32NoTag(int32 value, io::CodedOutputStream* output);
383 : INL static void WriteSInt64NoTag(int64 value, io::CodedOutputStream* output);
384 : INL static void WriteFixed32NoTag(uint32 value,
385 : io::CodedOutputStream* output);
386 : INL static void WriteFixed64NoTag(uint64 value,
387 : io::CodedOutputStream* output);
388 : INL static void WriteSFixed32NoTag(int32 value,
389 : io::CodedOutputStream* output);
390 : INL static void WriteSFixed64NoTag(int64 value,
391 : io::CodedOutputStream* output);
392 : INL static void WriteFloatNoTag(float value, io::CodedOutputStream* output);
393 : INL static void WriteDoubleNoTag(double value, io::CodedOutputStream* output);
394 : INL static void WriteBoolNoTag(bool value, io::CodedOutputStream* output);
395 : INL static void WriteEnumNoTag(int value, io::CodedOutputStream* output);
396 :
397 : // Write array of primitive fields, without tags
398 : static void WriteFloatArray(const float* a, int n,
399 : io::CodedOutputStream* output);
400 : static void WriteDoubleArray(const double* a, int n,
401 : io::CodedOutputStream* output);
402 : static void WriteFixed32Array(const uint32* a, int n,
403 : io::CodedOutputStream* output);
404 : static void WriteFixed64Array(const uint64* a, int n,
405 : io::CodedOutputStream* output);
406 : static void WriteSFixed32Array(const int32* a, int n,
407 : io::CodedOutputStream* output);
408 : static void WriteSFixed64Array(const int64* a, int n,
409 : io::CodedOutputStream* output);
410 : static void WriteBoolArray(const bool* a, int n,
411 : io::CodedOutputStream* output);
412 :
413 : // Write fields, including tags.
414 : static void WriteInt32(int field_number, int32 value,
415 : io::CodedOutputStream* output);
416 : static void WriteInt64(int field_number, int64 value,
417 : io::CodedOutputStream* output);
418 : static void WriteUInt32(int field_number, uint32 value,
419 : io::CodedOutputStream* output);
420 : static void WriteUInt64(int field_number, uint64 value,
421 : io::CodedOutputStream* output);
422 : static void WriteSInt32(int field_number, int32 value,
423 : io::CodedOutputStream* output);
424 : static void WriteSInt64(int field_number, int64 value,
425 : io::CodedOutputStream* output);
426 : static void WriteFixed32(int field_number, uint32 value,
427 : io::CodedOutputStream* output);
428 : static void WriteFixed64(int field_number, uint64 value,
429 : io::CodedOutputStream* output);
430 : static void WriteSFixed32(int field_number, int32 value,
431 : io::CodedOutputStream* output);
432 : static void WriteSFixed64(int field_number, int64 value,
433 : io::CodedOutputStream* output);
434 : static void WriteFloat(int field_number, float value,
435 : io::CodedOutputStream* output);
436 : static void WriteDouble(int field_number, double value,
437 : io::CodedOutputStream* output);
438 : static void WriteBool(int field_number, bool value,
439 : io::CodedOutputStream* output);
440 : static void WriteEnum(int field_number, int value,
441 : io::CodedOutputStream* output);
442 :
443 : static void WriteString(int field_number, const string& value,
444 : io::CodedOutputStream* output);
445 : static void WriteBytes(int field_number, const string& value,
446 : io::CodedOutputStream* output);
447 : static void WriteStringMaybeAliased(int field_number, const string& value,
448 : io::CodedOutputStream* output);
449 : static void WriteBytesMaybeAliased(int field_number, const string& value,
450 : io::CodedOutputStream* output);
451 :
452 : static void WriteGroup(int field_number, const MessageLite& value,
453 : io::CodedOutputStream* output);
454 : static void WriteMessage(int field_number, const MessageLite& value,
455 : io::CodedOutputStream* output);
456 : // Like above, but these will check if the output stream has enough
457 : // space to write directly to a flat array.
458 : static void WriteGroupMaybeToArray(int field_number, const MessageLite& value,
459 : io::CodedOutputStream* output);
460 : static void WriteMessageMaybeToArray(int field_number,
461 : const MessageLite& value,
462 : io::CodedOutputStream* output);
463 :
464 : // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The
465 : // pointer must point at an instance of MessageType, *not* a subclass (or
466 : // the subclass must not override SerializeWithCachedSizes()).
467 : template <typename MessageType>
468 : static inline void WriteGroupNoVirtual(int field_number,
469 : const MessageType& value,
470 : io::CodedOutputStream* output);
471 : template <typename MessageType>
472 : static inline void WriteMessageNoVirtual(int field_number,
473 : const MessageType& value,
474 : io::CodedOutputStream* output);
475 :
476 : // Like above, but use only *ToArray methods of CodedOutputStream.
477 : INL static uint8* WriteTagToArray(int field_number, WireType type,
478 : uint8* target);
479 :
480 : // Write fields, without tags.
481 : INL static uint8* WriteInt32NoTagToArray(int32 value, uint8* target);
482 : INL static uint8* WriteInt64NoTagToArray(int64 value, uint8* target);
483 : INL static uint8* WriteUInt32NoTagToArray(uint32 value, uint8* target);
484 : INL static uint8* WriteUInt64NoTagToArray(uint64 value, uint8* target);
485 : INL static uint8* WriteSInt32NoTagToArray(int32 value, uint8* target);
486 : INL static uint8* WriteSInt64NoTagToArray(int64 value, uint8* target);
487 : INL static uint8* WriteFixed32NoTagToArray(uint32 value, uint8* target);
488 : INL static uint8* WriteFixed64NoTagToArray(uint64 value, uint8* target);
489 : INL static uint8* WriteSFixed32NoTagToArray(int32 value, uint8* target);
490 : INL static uint8* WriteSFixed64NoTagToArray(int64 value, uint8* target);
491 : INL static uint8* WriteFloatNoTagToArray(float value, uint8* target);
492 : INL static uint8* WriteDoubleNoTagToArray(double value, uint8* target);
493 : INL static uint8* WriteBoolNoTagToArray(bool value, uint8* target);
494 : INL static uint8* WriteEnumNoTagToArray(int value, uint8* target);
495 :
496 : // Write fields, without tags. These require that value.size() > 0.
497 : template<typename T>
498 : INL static uint8* WritePrimitiveNoTagToArray(
499 : const RepeatedField<T>& value,
500 : uint8* (*Writer)(T, uint8*), uint8* target);
501 : template<typename T>
502 : INL static uint8* WriteFixedNoTagToArray(
503 : const RepeatedField<T>& value,
504 : uint8* (*Writer)(T, uint8*), uint8* target);
505 :
506 : INL static uint8* WriteInt32NoTagToArray(
507 : const RepeatedField< int32>& value, uint8* output);
508 : INL static uint8* WriteInt64NoTagToArray(
509 : const RepeatedField< int64>& value, uint8* output);
510 : INL static uint8* WriteUInt32NoTagToArray(
511 : const RepeatedField<uint32>& value, uint8* output);
512 : INL static uint8* WriteUInt64NoTagToArray(
513 : const RepeatedField<uint64>& value, uint8* output);
514 : INL static uint8* WriteSInt32NoTagToArray(
515 : const RepeatedField< int32>& value, uint8* output);
516 : INL static uint8* WriteSInt64NoTagToArray(
517 : const RepeatedField< int64>& value, uint8* output);
518 : INL static uint8* WriteFixed32NoTagToArray(
519 : const RepeatedField<uint32>& value, uint8* output);
520 : INL static uint8* WriteFixed64NoTagToArray(
521 : const RepeatedField<uint64>& value, uint8* output);
522 : INL static uint8* WriteSFixed32NoTagToArray(
523 : const RepeatedField< int32>& value, uint8* output);
524 : INL static uint8* WriteSFixed64NoTagToArray(
525 : const RepeatedField< int64>& value, uint8* output);
526 : INL static uint8* WriteFloatNoTagToArray(
527 : const RepeatedField< float>& value, uint8* output);
528 : INL static uint8* WriteDoubleNoTagToArray(
529 : const RepeatedField<double>& value, uint8* output);
530 : INL static uint8* WriteBoolNoTagToArray(
531 : const RepeatedField< bool>& value, uint8* output);
532 : INL static uint8* WriteEnumNoTagToArray(
533 : const RepeatedField< int>& value, uint8* output);
534 :
535 : // Write fields, including tags.
536 : INL static uint8* WriteInt32ToArray(int field_number, int32 value,
537 : uint8* target);
538 : INL static uint8* WriteInt64ToArray(int field_number, int64 value,
539 : uint8* target);
540 : INL static uint8* WriteUInt32ToArray(int field_number, uint32 value,
541 : uint8* target);
542 : INL static uint8* WriteUInt64ToArray(int field_number, uint64 value,
543 : uint8* target);
544 : INL static uint8* WriteSInt32ToArray(int field_number, int32 value,
545 : uint8* target);
546 : INL static uint8* WriteSInt64ToArray(int field_number, int64 value,
547 : uint8* target);
548 : INL static uint8* WriteFixed32ToArray(int field_number, uint32 value,
549 : uint8* target);
550 : INL static uint8* WriteFixed64ToArray(int field_number, uint64 value,
551 : uint8* target);
552 : INL static uint8* WriteSFixed32ToArray(int field_number, int32 value,
553 : uint8* target);
554 : INL static uint8* WriteSFixed64ToArray(int field_number, int64 value,
555 : uint8* target);
556 : INL static uint8* WriteFloatToArray(int field_number, float value,
557 : uint8* target);
558 : INL static uint8* WriteDoubleToArray(int field_number, double value,
559 : uint8* target);
560 : INL static uint8* WriteBoolToArray(int field_number, bool value,
561 : uint8* target);
562 : INL static uint8* WriteEnumToArray(int field_number, int value,
563 : uint8* target);
564 :
565 : template<typename T>
566 : INL static uint8* WritePrimitiveToArray(
567 : int field_number,
568 : const RepeatedField<T>& value,
569 : uint8* (*Writer)(int, T, uint8*), uint8* target);
570 :
571 : INL static uint8* WriteInt32ToArray(
572 : int field_number, const RepeatedField< int32>& value, uint8* output);
573 : INL static uint8* WriteInt64ToArray(
574 : int field_number, const RepeatedField< int64>& value, uint8* output);
575 : INL static uint8* WriteUInt32ToArray(
576 : int field_number, const RepeatedField<uint32>& value, uint8* output);
577 : INL static uint8* WriteUInt64ToArray(
578 : int field_number, const RepeatedField<uint64>& value, uint8* output);
579 : INL static uint8* WriteSInt32ToArray(
580 : int field_number, const RepeatedField< int32>& value, uint8* output);
581 : INL static uint8* WriteSInt64ToArray(
582 : int field_number, const RepeatedField< int64>& value, uint8* output);
583 : INL static uint8* WriteFixed32ToArray(
584 : int field_number, const RepeatedField<uint32>& value, uint8* output);
585 : INL static uint8* WriteFixed64ToArray(
586 : int field_number, const RepeatedField<uint64>& value, uint8* output);
587 : INL static uint8* WriteSFixed32ToArray(
588 : int field_number, const RepeatedField< int32>& value, uint8* output);
589 : INL static uint8* WriteSFixed64ToArray(
590 : int field_number, const RepeatedField< int64>& value, uint8* output);
591 : INL static uint8* WriteFloatToArray(
592 : int field_number, const RepeatedField< float>& value, uint8* output);
593 : INL static uint8* WriteDoubleToArray(
594 : int field_number, const RepeatedField<double>& value, uint8* output);
595 : INL static uint8* WriteBoolToArray(
596 : int field_number, const RepeatedField< bool>& value, uint8* output);
597 : INL static uint8* WriteEnumToArray(
598 : int field_number, const RepeatedField< int>& value, uint8* output);
599 :
600 : INL static uint8* WriteStringToArray(int field_number, const string& value,
601 : uint8* target);
602 : INL static uint8* WriteBytesToArray(int field_number, const string& value,
603 : uint8* target);
604 :
605 : // Whether to serialize deterministically (e.g., map keys are
606 : // sorted) is a property of a CodedOutputStream, and in the process
607 : // of serialization, the "ToArray" variants may be invoked. But they don't
608 : // have a CodedOutputStream available, so they get an additional parameter
609 : // telling them whether to serialize deterministically.
610 : template<typename MessageType>
611 : INL static uint8* InternalWriteGroupToArray(int field_number,
612 : const MessageType& value,
613 : bool deterministic,
614 : uint8* target);
615 : template<typename MessageType>
616 : INL static uint8* InternalWriteMessageToArray(int field_number,
617 : const MessageType& value,
618 : bool deterministic,
619 : uint8* target);
620 :
621 : // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The
622 : // pointer must point at an instance of MessageType, *not* a subclass (or
623 : // the subclass must not override SerializeWithCachedSizes()).
624 : template <typename MessageType>
625 : INL static uint8* InternalWriteGroupNoVirtualToArray(int field_number,
626 : const MessageType& value,
627 : bool deterministic,
628 : uint8* target);
629 : template <typename MessageType>
630 : INL static uint8* InternalWriteMessageNoVirtualToArray(
631 : int field_number, const MessageType& value, bool deterministic,
632 : uint8* target);
633 :
634 : // For backward-compatibility, the last four methods also have versions
635 : // that are non-deterministic always.
636 : INL static uint8* WriteGroupToArray(int field_number,
637 : const MessageLite& value, uint8* target) {
638 : return InternalWriteGroupToArray(field_number, value, false, target);
639 : }
640 : INL static uint8* WriteMessageToArray(int field_number,
641 : const MessageLite& value,
642 : uint8* target) {
643 : return InternalWriteMessageToArray(field_number, value, false, target);
644 : }
645 : template <typename MessageType>
646 : INL static uint8* WriteGroupNoVirtualToArray(int field_number,
647 : const MessageType& value,
648 : uint8* target) {
649 : return InternalWriteGroupNoVirtualToArray(field_number, value, false,
650 : target);
651 : }
652 : template <typename MessageType>
653 : INL static uint8* WriteMessageNoVirtualToArray(int field_number,
654 : const MessageType& value,
655 : uint8* target) {
656 : return InternalWriteMessageNoVirtualToArray(field_number, value, false,
657 : target);
658 : }
659 :
660 : #undef INL
661 :
662 : // Compute the byte size of a field. The XxSize() functions do NOT include
663 : // the tag, so you must also call TagSize(). (This is because, for repeated
664 : // fields, you should only call TagSize() once and multiply it by the element
665 : // count, but you may have to call XxSize() for each individual element.)
666 : static inline size_t Int32Size ( int32 value);
667 : static inline size_t Int64Size ( int64 value);
668 : static inline size_t UInt32Size (uint32 value);
669 : static inline size_t UInt64Size (uint64 value);
670 : static inline size_t SInt32Size ( int32 value);
671 : static inline size_t SInt64Size ( int64 value);
672 : static inline size_t EnumSize ( int value);
673 :
674 : static size_t Int32Size (const RepeatedField< int32>& value);
675 : static size_t Int64Size (const RepeatedField< int64>& value);
676 : static size_t UInt32Size(const RepeatedField<uint32>& value);
677 : static size_t UInt64Size(const RepeatedField<uint64>& value);
678 : static size_t SInt32Size(const RepeatedField< int32>& value);
679 : static size_t SInt64Size(const RepeatedField< int64>& value);
680 : static size_t EnumSize (const RepeatedField< int>& value);
681 :
682 : // These types always have the same size.
683 : static const size_t kFixed32Size = 4;
684 : static const size_t kFixed64Size = 8;
685 : static const size_t kSFixed32Size = 4;
686 : static const size_t kSFixed64Size = 8;
687 : static const size_t kFloatSize = 4;
688 : static const size_t kDoubleSize = 8;
689 : static const size_t kBoolSize = 1;
690 :
691 : static inline size_t StringSize(const string& value);
692 : static inline size_t BytesSize (const string& value);
693 :
694 : template<typename MessageType>
695 : static inline size_t GroupSize (const MessageType& value);
696 : template<typename MessageType>
697 : static inline size_t MessageSize(const MessageType& value);
698 :
699 : // Like above, but de-virtualize the call to ByteSize(). The
700 : // pointer must point at an instance of MessageType, *not* a subclass (or
701 : // the subclass must not override ByteSize()).
702 : template<typename MessageType>
703 : static inline size_t GroupSizeNoVirtual (const MessageType& value);
704 : template<typename MessageType>
705 : static inline size_t MessageSizeNoVirtual(const MessageType& value);
706 :
707 : // Given the length of data, calculate the byte size of the data on the
708 : // wire if we encode the data as a length delimited field.
709 : static inline size_t LengthDelimitedSize(size_t length);
710 :
711 : private:
712 : // A helper method for the repeated primitive reader. This method has
713 : // optimizations for primitive types that have fixed size on the wire, and
714 : // can be read using potentially faster paths.
715 : template <typename CType, enum FieldType DeclaredType>
716 : GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE
717 : static bool ReadRepeatedFixedSizePrimitive(
718 : int tag_size,
719 : uint32 tag,
720 : google::protobuf::io::CodedInputStream* input,
721 : RepeatedField<CType>* value);
722 :
723 : // Like ReadRepeatedFixedSizePrimitive but for packed primitive fields.
724 : template <typename CType, enum FieldType DeclaredType>
725 : GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE
726 : static bool ReadPackedFixedSizePrimitive(
727 : google::protobuf::io::CodedInputStream* input, RepeatedField<CType>* value);
728 :
729 : static const CppType kFieldTypeToCppTypeMap[];
730 : static const WireFormatLite::WireType kWireTypeForFieldType[];
731 :
732 : GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormatLite);
733 : };
734 :
735 : // A class which deals with unknown values. The default implementation just
736 : // discards them. WireFormat defines a subclass which writes to an
737 : // UnknownFieldSet. This class is used by ExtensionSet::ParseField(), since
738 : // ExtensionSet is part of the lite library but UnknownFieldSet is not.
739 : class LIBPROTOBUF_EXPORT FieldSkipper {
740 : public:
741 : FieldSkipper() {}
742 : virtual ~FieldSkipper() {}
743 :
744 : // Skip a field whose tag has already been consumed.
745 : virtual bool SkipField(io::CodedInputStream* input, uint32 tag);
746 :
747 : // Skip an entire message or group, up to an end-group tag (which is consumed)
748 : // or end-of-stream.
749 : virtual bool SkipMessage(io::CodedInputStream* input);
750 :
751 : // Deal with an already-parsed unrecognized enum value. The default
752 : // implementation does nothing, but the UnknownFieldSet-based implementation
753 : // saves it as an unknown varint.
754 : virtual void SkipUnknownEnum(int field_number, int value);
755 : };
756 :
757 : // Subclass of FieldSkipper which saves skipped fields to a CodedOutputStream.
758 :
759 : class LIBPROTOBUF_EXPORT CodedOutputStreamFieldSkipper : public FieldSkipper {
760 : public:
761 : explicit CodedOutputStreamFieldSkipper(io::CodedOutputStream* unknown_fields)
762 : : unknown_fields_(unknown_fields) {}
763 : virtual ~CodedOutputStreamFieldSkipper() {}
764 :
765 : // implements FieldSkipper -----------------------------------------
766 : virtual bool SkipField(io::CodedInputStream* input, uint32 tag);
767 : virtual bool SkipMessage(io::CodedInputStream* input);
768 : virtual void SkipUnknownEnum(int field_number, int value);
769 :
770 : protected:
771 : io::CodedOutputStream* unknown_fields_;
772 : };
773 :
774 :
775 : // inline methods ====================================================
776 :
777 : inline WireFormatLite::CppType
778 : WireFormatLite::FieldTypeToCppType(FieldType type) {
779 : return kFieldTypeToCppTypeMap[type];
780 : }
781 :
782 0 : inline uint32 WireFormatLite::MakeTag(int field_number, WireType type) {
783 0 : return GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(field_number, type);
784 : }
785 :
786 : inline WireFormatLite::WireType WireFormatLite::GetTagWireType(uint32 tag) {
787 : return static_cast<WireType>(tag & kTagTypeMask);
788 : }
789 :
790 0 : inline int WireFormatLite::GetTagFieldNumber(uint32 tag) {
791 0 : return static_cast<int>(tag >> kTagTypeBits);
792 : }
793 :
794 : inline size_t WireFormatLite::TagSize(int field_number,
795 : WireFormatLite::FieldType type) {
796 : size_t result = io::CodedOutputStream::VarintSize32(
797 : static_cast<uint32>(field_number << kTagTypeBits));
798 : if (type == TYPE_GROUP) {
799 : // Groups have both a start and an end tag.
800 : return result * 2;
801 : } else {
802 : return result;
803 : }
804 : }
805 :
806 0 : inline uint32 WireFormatLite::EncodeFloat(float value) {
807 : union {float f; uint32 i;};
808 0 : f = value;
809 0 : return i;
810 : }
811 :
812 0 : inline float WireFormatLite::DecodeFloat(uint32 value) {
813 : union {float f; uint32 i;};
814 0 : i = value;
815 0 : return f;
816 : }
817 :
818 0 : inline uint64 WireFormatLite::EncodeDouble(double value) {
819 : union {double f; uint64 i;};
820 0 : f = value;
821 0 : return i;
822 : }
823 :
824 0 : inline double WireFormatLite::DecodeDouble(uint64 value) {
825 : union {double f; uint64 i;};
826 0 : i = value;
827 0 : return f;
828 : }
829 :
830 : // ZigZag Transform: Encodes signed integers so that they can be
831 : // effectively used with varint encoding.
832 : //
833 : // varint operates on unsigned integers, encoding smaller numbers into
834 : // fewer bytes. If you try to use it on a signed integer, it will treat
835 : // this number as a very large unsigned integer, which means that even
836 : // small signed numbers like -1 will take the maximum number of bytes
837 : // (10) to encode. ZigZagEncode() maps signed integers to unsigned
838 : // in such a way that those with a small absolute value will have smaller
839 : // encoded values, making them appropriate for encoding using varint.
840 : //
841 : // int32 -> uint32
842 : // -------------------------
843 : // 0 -> 0
844 : // -1 -> 1
845 : // 1 -> 2
846 : // -2 -> 3
847 : // ... -> ...
848 : // 2147483647 -> 4294967294
849 : // -2147483648 -> 4294967295
850 : //
851 : // >> encode >>
852 : // << decode <<
853 :
854 : inline uint32 WireFormatLite::ZigZagEncode32(int32 n) {
855 : // Note: the right-shift must be arithmetic
856 : // Note: left shift must be unsigned because of overflow
857 : return (static_cast<uint32>(n) << 1) ^ static_cast<uint32>(n >> 31);
858 : }
859 :
860 : inline int32 WireFormatLite::ZigZagDecode32(uint32 n) {
861 : // Note: Using unsigned types prevent undefined behavior
862 : return static_cast<int32>((n >> 1) ^ (~(n & 1) + 1));
863 : }
864 :
865 : inline uint64 WireFormatLite::ZigZagEncode64(int64 n) {
866 : // Note: the right-shift must be arithmetic
867 : // Note: left shift must be unsigned because of overflow
868 : return (static_cast<uint64>(n) << 1) ^ static_cast<uint64>(n >> 63);
869 : }
870 :
871 : inline int64 WireFormatLite::ZigZagDecode64(uint64 n) {
872 : // Note: Using unsigned types prevent undefined behavior
873 : return static_cast<int64>((n >> 1) ^ (~(n & 1) + 1));
874 : }
875 :
876 : // String is for UTF-8 text only, but, even so, ReadString() can simply
877 : // call ReadBytes().
878 :
879 0 : inline bool WireFormatLite::ReadString(io::CodedInputStream* input,
880 : string* value) {
881 0 : return ReadBytes(input, value);
882 : }
883 :
884 : inline bool WireFormatLite::ReadString(io::CodedInputStream* input,
885 : string** p) {
886 : return ReadBytes(input, p);
887 : }
888 :
889 : } // namespace internal
890 : } // namespace protobuf
891 :
892 : } // namespace google
893 : #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__
|