1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: kenton@google.com (Kenton Varda)
32 // wink@google.com (Wink Saville) (refactored from wire_format.h)
33 // Based on original Protocol Buffers design by
34 // Sanjay Ghemawat, Jeff Dean, and others.
36 #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__
37 #define GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__
40 // This is required for min/max on VS2013 only.
45 #include <google/protobuf/stubs/common.h>
46 #include <google/protobuf/message_lite.h>
47 #include <google/protobuf/repeated_field.h>
48 #include <google/protobuf/wire_format_lite.h>
49 #include <google/protobuf/io/coded_stream.h>
56 // Implementation details of ReadPrimitive.
59 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_INT32>(
60 io::CodedInputStream* input,
63 if (!input->ReadVarint32(&temp)) return false;
64 *value = static_cast<int32>(temp);
68 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_INT64>(
69 io::CodedInputStream* input,
72 if (!input->ReadVarint64(&temp)) return false;
73 *value = static_cast<int64>(temp);
77 inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_UINT32>(
78 io::CodedInputStream* input,
80 return input->ReadVarint32(value);
83 inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_UINT64>(
84 io::CodedInputStream* input,
86 return input->ReadVarint64(value);
89 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SINT32>(
90 io::CodedInputStream* input,
93 if (!input->ReadVarint32(&temp)) return false;
94 *value = ZigZagDecode32(temp);
98 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SINT64>(
99 io::CodedInputStream* input,
102 if (!input->ReadVarint64(&temp)) return false;
103 *value = ZigZagDecode64(temp);
107 inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_FIXED32>(
108 io::CodedInputStream* input,
110 return input->ReadLittleEndian32(value);
113 inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_FIXED64>(
114 io::CodedInputStream* input,
116 return input->ReadLittleEndian64(value);
119 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SFIXED32>(
120 io::CodedInputStream* input,
123 if (!input->ReadLittleEndian32(&temp)) return false;
124 *value = static_cast<int32>(temp);
128 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SFIXED64>(
129 io::CodedInputStream* input,
132 if (!input->ReadLittleEndian64(&temp)) return false;
133 *value = static_cast<int64>(temp);
137 inline bool WireFormatLite::ReadPrimitive<float, WireFormatLite::TYPE_FLOAT>(
138 io::CodedInputStream* input,
141 if (!input->ReadLittleEndian32(&temp)) return false;
142 *value = DecodeFloat(temp);
146 inline bool WireFormatLite::ReadPrimitive<double, WireFormatLite::TYPE_DOUBLE>(
147 io::CodedInputStream* input,
150 if (!input->ReadLittleEndian64(&temp)) return false;
151 *value = DecodeDouble(temp);
155 inline bool WireFormatLite::ReadPrimitive<bool, WireFormatLite::TYPE_BOOL>(
156 io::CodedInputStream* input,
159 if (!input->ReadVarint64(&temp)) return false;
164 inline bool WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
165 io::CodedInputStream* input,
168 if (!input->ReadVarint32(&temp)) return false;
169 *value = static_cast<int>(temp);
174 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
175 uint32, WireFormatLite::TYPE_FIXED32>(
178 return io::CodedInputStream::ReadLittleEndian32FromArray(buffer, value);
181 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
182 uint64, WireFormatLite::TYPE_FIXED64>(
185 return io::CodedInputStream::ReadLittleEndian64FromArray(buffer, value);
188 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
189 int32, WireFormatLite::TYPE_SFIXED32>(
193 buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
194 *value = static_cast<int32>(temp);
198 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
199 int64, WireFormatLite::TYPE_SFIXED64>(
203 buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
204 *value = static_cast<int64>(temp);
208 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
209 float, WireFormatLite::TYPE_FLOAT>(
213 buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
214 *value = DecodeFloat(temp);
218 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
219 double, WireFormatLite::TYPE_DOUBLE>(
223 buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
224 *value = DecodeDouble(temp);
228 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
229 inline bool WireFormatLite::ReadRepeatedPrimitive(
230 int, // tag_size, unused.
232 io::CodedInputStream* input,
233 RepeatedField<CType>* values) {
235 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
237 int elements_already_reserved = values->Capacity() - values->size();
238 while (elements_already_reserved > 0 && input->ExpectTag(tag)) {
239 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
240 values->AddAlreadyReserved(value);
241 elements_already_reserved--;
246 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
247 inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive(
250 io::CodedInputStream* input,
251 RepeatedField<CType>* values) {
252 GOOGLE_DCHECK_EQ(UInt32Size(tag), tag_size);
254 if (!ReadPrimitive<CType, DeclaredType>(input, &value))
258 // For fixed size values, repeated values can be read more quickly by
259 // reading directly from a raw array.
261 // We can get a tight loop by only reading as many elements as can be
262 // added to the RepeatedField without having to do any resizing. Additionally,
263 // we only try to read as many elements as are available from the current
264 // buffer space. Doing so avoids having to perform boundary checks when
265 // reading the value: the maximum number of elements that can be read is
266 // known outside of the loop.
267 const void* void_pointer;
269 input->GetDirectBufferPointerInline(&void_pointer, &size);
271 const uint8* buffer = reinterpret_cast<const uint8*>(void_pointer);
272 // The number of bytes each type occupies on the wire.
273 const int per_value_size = tag_size + sizeof(value);
275 int elements_available = min(values->Capacity() - values->size(),
276 size / per_value_size);
278 while (num_read < elements_available &&
279 (buffer = io::CodedInputStream::ExpectTagFromArray(
280 buffer, tag)) != NULL) {
281 buffer = ReadPrimitiveFromArray<CType, DeclaredType>(buffer, &value);
282 values->AddAlreadyReserved(value);
285 const int read_bytes = num_read * per_value_size;
286 if (read_bytes > 0) {
287 input->Skip(read_bytes);
293 // Specializations of ReadRepeatedPrimitive for the fixed size types, which use
294 // the optimized code path.
295 #define READ_REPEATED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \
297 inline bool WireFormatLite::ReadRepeatedPrimitive< \
298 CPPTYPE, WireFormatLite::DECLARED_TYPE>( \
301 io::CodedInputStream* input, \
302 RepeatedField<CPPTYPE>* values) { \
303 return ReadRepeatedFixedSizePrimitive< \
304 CPPTYPE, WireFormatLite::DECLARED_TYPE>( \
305 tag_size, tag, input, values); \
308 READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32)
309 READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64)
310 READ_REPEATED_FIXED_SIZE_PRIMITIVE(int32, TYPE_SFIXED32)
311 READ_REPEATED_FIXED_SIZE_PRIMITIVE(int64, TYPE_SFIXED64)
312 READ_REPEATED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT)
313 READ_REPEATED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE)
315 #undef READ_REPEATED_FIXED_SIZE_PRIMITIVE
317 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
318 bool WireFormatLite::ReadRepeatedPrimitiveNoInline(
321 io::CodedInputStream* input,
322 RepeatedField<CType>* value) {
323 return ReadRepeatedPrimitive<CType, DeclaredType>(
324 tag_size, tag, input, value);
327 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
328 inline bool WireFormatLite::ReadPackedPrimitive(io::CodedInputStream* input,
329 RepeatedField<CType>* values) {
331 if (!input->ReadVarint32(&length)) return false;
332 io::CodedInputStream::Limit limit = input->PushLimit(length);
333 while (input->BytesUntilLimit() > 0) {
335 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
338 input->PopLimit(limit);
342 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
343 inline bool WireFormatLite::ReadPackedFixedSizePrimitive(
344 io::CodedInputStream* input, RepeatedField<CType>* values) {
346 if (!input->ReadVarint32(&length)) return false;
347 const uint32 old_entries = values->size();
348 const uint32 new_entries = length / sizeof(CType);
349 const uint32 new_bytes = new_entries * sizeof(CType);
350 if (new_bytes != length) return false;
351 // We would *like* to pre-allocate the buffer to write into (for
352 // speed), but *must* avoid performing a very large allocation due
353 // to a malicious user-supplied "length" above. So we have a fast
354 // path that pre-allocates when the "length" is less than a bound.
355 // We determine the bound by calling BytesUntilTotalBytesLimit() and
356 // BytesUntilLimit(). These return -1 to mean "no limit set".
357 // There are four cases:
358 // TotalBytesLimit Limit
359 // -1 -1 Use slow path.
360 // -1 >= 0 Use fast path if length <= Limit.
361 // >= 0 -1 Use slow path.
362 // >= 0 >= 0 Use fast path if length <= min(both limits).
363 int64 bytes_limit = input->BytesUntilTotalBytesLimit();
364 if (bytes_limit == -1) {
365 bytes_limit = input->BytesUntilLimit();
368 min(bytes_limit, static_cast<int64>(input->BytesUntilLimit()));
370 if (bytes_limit >= new_bytes) {
371 // Fast-path that pre-allocates *values to the final size.
372 #if defined(PROTOBUF_LITTLE_ENDIAN)
373 values->Resize(old_entries + new_entries, 0);
374 // values->mutable_data() may change after Resize(), so do this after:
375 void* dest = reinterpret_cast<void*>(values->mutable_data() + old_entries);
376 if (!input->ReadRaw(dest, new_bytes)) {
377 values->Truncate(old_entries);
381 values->Reserve(old_entries + new_entries);
383 for (int i = 0; i < new_entries; ++i) {
384 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
385 values->AddAlreadyReserved(value);
389 // This is the slow-path case where "length" may be too large to
390 // safely allocate. We read as much as we can into *values
391 // without pre-allocating "length" bytes.
393 for (uint32 i = 0; i < new_entries; ++i) {
394 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
401 // Specializations of ReadPackedPrimitive for the fixed size types, which use
402 // an optimized code path.
403 #define READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \
405 inline bool WireFormatLite::ReadPackedPrimitive< \
406 CPPTYPE, WireFormatLite::DECLARED_TYPE>( \
407 io::CodedInputStream* input, \
408 RepeatedField<CPPTYPE>* values) { \
409 return ReadPackedFixedSizePrimitive< \
410 CPPTYPE, WireFormatLite::DECLARED_TYPE>(input, values); \
413 READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32);
414 READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64);
415 READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(int32, TYPE_SFIXED32);
416 READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(int64, TYPE_SFIXED64);
417 READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT);
418 READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE);
420 #undef READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE
422 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
423 bool WireFormatLite::ReadPackedPrimitiveNoInline(io::CodedInputStream* input,
424 RepeatedField<CType>* values) {
425 return ReadPackedPrimitive<CType, DeclaredType>(input, values);
429 inline bool WireFormatLite::ReadGroup(int field_number,
430 io::CodedInputStream* input,
431 MessageLite* value) {
432 if (!input->IncrementRecursionDepth()) return false;
433 if (!value->MergePartialFromCodedStream(input)) return false;
434 input->DecrementRecursionDepth();
435 // Make sure the last thing read was an end tag for this group.
436 if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) {
441 inline bool WireFormatLite::ReadMessage(io::CodedInputStream* input,
442 MessageLite* value) {
444 if (!input->ReadVarint32(&length)) return false;
445 if (!input->IncrementRecursionDepth()) return false;
446 io::CodedInputStream::Limit limit = input->PushLimit(length);
447 if (!value->MergePartialFromCodedStream(input)) return false;
448 // Make sure that parsing stopped when the limit was hit, not at an endgroup
450 if (!input->ConsumedEntireMessage()) return false;
451 input->PopLimit(limit);
452 input->DecrementRecursionDepth();
456 // We name the template parameter something long and extremely unlikely to occur
457 // elsewhere because a *qualified* member access expression designed to avoid
458 // virtual dispatch, C++03 [basic.lookup.classref] 3.4.5/4 requires that the
459 // name of the qualifying class to be looked up both in the context of the full
460 // expression (finding the template parameter) and in the context of the object
461 // whose member we are accessing. This could potentially find a nested type
462 // within that object. The standard goes on to require these names to refer to
463 // the same entity, which this collision would violate. The lack of a safe way
464 // to avoid this collision appears to be a defect in the standard, but until it
465 // is corrected, we choose the name to avoid accidental collisions.
466 template<typename MessageType_WorkAroundCppLookupDefect>
467 inline bool WireFormatLite::ReadGroupNoVirtual(
468 int field_number, io::CodedInputStream* input,
469 MessageType_WorkAroundCppLookupDefect* value) {
470 if (!input->IncrementRecursionDepth()) return false;
472 MessageType_WorkAroundCppLookupDefect::MergePartialFromCodedStream(input))
474 input->DecrementRecursionDepth();
475 // Make sure the last thing read was an end tag for this group.
476 if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) {
481 template<typename MessageType_WorkAroundCppLookupDefect>
482 inline bool WireFormatLite::ReadMessageNoVirtual(
483 io::CodedInputStream* input, MessageType_WorkAroundCppLookupDefect* value) {
485 if (!input->ReadVarint32(&length)) return false;
486 if (!input->IncrementRecursionDepth()) return false;
487 io::CodedInputStream::Limit limit = input->PushLimit(length);
489 MessageType_WorkAroundCppLookupDefect::MergePartialFromCodedStream(input))
491 // Make sure that parsing stopped when the limit was hit, not at an endgroup
493 if (!input->ConsumedEntireMessage()) return false;
494 input->PopLimit(limit);
495 input->DecrementRecursionDepth();
499 // ===================================================================
501 inline void WireFormatLite::WriteTag(int field_number, WireType type,
502 io::CodedOutputStream* output) {
503 output->WriteTag(MakeTag(field_number, type));
506 inline void WireFormatLite::WriteInt32NoTag(int32 value,
507 io::CodedOutputStream* output) {
508 output->WriteVarint32SignExtended(value);
510 inline void WireFormatLite::WriteInt64NoTag(int64 value,
511 io::CodedOutputStream* output) {
512 output->WriteVarint64(static_cast<uint64>(value));
514 inline void WireFormatLite::WriteUInt32NoTag(uint32 value,
515 io::CodedOutputStream* output) {
516 output->WriteVarint32(value);
518 inline void WireFormatLite::WriteUInt64NoTag(uint64 value,
519 io::CodedOutputStream* output) {
520 output->WriteVarint64(value);
522 inline void WireFormatLite::WriteSInt32NoTag(int32 value,
523 io::CodedOutputStream* output) {
524 output->WriteVarint32(ZigZagEncode32(value));
526 inline void WireFormatLite::WriteSInt64NoTag(int64 value,
527 io::CodedOutputStream* output) {
528 output->WriteVarint64(ZigZagEncode64(value));
530 inline void WireFormatLite::WriteFixed32NoTag(uint32 value,
531 io::CodedOutputStream* output) {
532 output->WriteLittleEndian32(value);
534 inline void WireFormatLite::WriteFixed64NoTag(uint64 value,
535 io::CodedOutputStream* output) {
536 output->WriteLittleEndian64(value);
538 inline void WireFormatLite::WriteSFixed32NoTag(int32 value,
539 io::CodedOutputStream* output) {
540 output->WriteLittleEndian32(static_cast<uint32>(value));
542 inline void WireFormatLite::WriteSFixed64NoTag(int64 value,
543 io::CodedOutputStream* output) {
544 output->WriteLittleEndian64(static_cast<uint64>(value));
546 inline void WireFormatLite::WriteFloatNoTag(float value,
547 io::CodedOutputStream* output) {
548 output->WriteLittleEndian32(EncodeFloat(value));
550 inline void WireFormatLite::WriteDoubleNoTag(double value,
551 io::CodedOutputStream* output) {
552 output->WriteLittleEndian64(EncodeDouble(value));
554 inline void WireFormatLite::WriteBoolNoTag(bool value,
555 io::CodedOutputStream* output) {
556 output->WriteVarint32(value ? 1 : 0);
558 inline void WireFormatLite::WriteEnumNoTag(int value,
559 io::CodedOutputStream* output) {
560 output->WriteVarint32SignExtended(value);
563 // See comment on ReadGroupNoVirtual to understand the need for this template
565 template<typename MessageType_WorkAroundCppLookupDefect>
566 inline void WireFormatLite::WriteGroupNoVirtual(
567 int field_number, const MessageType_WorkAroundCppLookupDefect& value,
568 io::CodedOutputStream* output) {
569 WriteTag(field_number, WIRETYPE_START_GROUP, output);
570 value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output);
571 WriteTag(field_number, WIRETYPE_END_GROUP, output);
573 template<typename MessageType_WorkAroundCppLookupDefect>
574 inline void WireFormatLite::WriteMessageNoVirtual(
575 int field_number, const MessageType_WorkAroundCppLookupDefect& value,
576 io::CodedOutputStream* output) {
577 WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
578 output->WriteVarint32(
579 value.MessageType_WorkAroundCppLookupDefect::GetCachedSize());
580 value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output);
583 // ===================================================================
585 inline uint8* WireFormatLite::WriteTagToArray(int field_number,
588 return io::CodedOutputStream::WriteTagToArray(MakeTag(field_number, type),
592 inline uint8* WireFormatLite::WriteInt32NoTagToArray(int32 value,
594 return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
596 inline uint8* WireFormatLite::WriteInt64NoTagToArray(int64 value,
598 return io::CodedOutputStream::WriteVarint64ToArray(
599 static_cast<uint64>(value), target);
601 inline uint8* WireFormatLite::WriteUInt32NoTagToArray(uint32 value,
603 return io::CodedOutputStream::WriteVarint32ToArray(value, target);
605 inline uint8* WireFormatLite::WriteUInt64NoTagToArray(uint64 value,
607 return io::CodedOutputStream::WriteVarint64ToArray(value, target);
609 inline uint8* WireFormatLite::WriteSInt32NoTagToArray(int32 value,
611 return io::CodedOutputStream::WriteVarint32ToArray(ZigZagEncode32(value),
614 inline uint8* WireFormatLite::WriteSInt64NoTagToArray(int64 value,
616 return io::CodedOutputStream::WriteVarint64ToArray(ZigZagEncode64(value),
619 inline uint8* WireFormatLite::WriteFixed32NoTagToArray(uint32 value,
621 return io::CodedOutputStream::WriteLittleEndian32ToArray(value, target);
623 inline uint8* WireFormatLite::WriteFixed64NoTagToArray(uint64 value,
625 return io::CodedOutputStream::WriteLittleEndian64ToArray(value, target);
627 inline uint8* WireFormatLite::WriteSFixed32NoTagToArray(int32 value,
629 return io::CodedOutputStream::WriteLittleEndian32ToArray(
630 static_cast<uint32>(value), target);
632 inline uint8* WireFormatLite::WriteSFixed64NoTagToArray(int64 value,
634 return io::CodedOutputStream::WriteLittleEndian64ToArray(
635 static_cast<uint64>(value), target);
637 inline uint8* WireFormatLite::WriteFloatNoTagToArray(float value,
639 return io::CodedOutputStream::WriteLittleEndian32ToArray(EncodeFloat(value),
642 inline uint8* WireFormatLite::WriteDoubleNoTagToArray(double value,
644 return io::CodedOutputStream::WriteLittleEndian64ToArray(EncodeDouble(value),
647 inline uint8* WireFormatLite::WriteBoolNoTagToArray(bool value,
649 return io::CodedOutputStream::WriteVarint32ToArray(value ? 1 : 0, target);
651 inline uint8* WireFormatLite::WriteEnumNoTagToArray(int value,
653 return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
656 inline uint8* WireFormatLite::WriteInt32ToArray(int field_number,
659 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
660 return WriteInt32NoTagToArray(value, target);
662 inline uint8* WireFormatLite::WriteInt64ToArray(int field_number,
665 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
666 return WriteInt64NoTagToArray(value, target);
668 inline uint8* WireFormatLite::WriteUInt32ToArray(int field_number,
671 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
672 return WriteUInt32NoTagToArray(value, target);
674 inline uint8* WireFormatLite::WriteUInt64ToArray(int field_number,
677 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
678 return WriteUInt64NoTagToArray(value, target);
680 inline uint8* WireFormatLite::WriteSInt32ToArray(int field_number,
683 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
684 return WriteSInt32NoTagToArray(value, target);
686 inline uint8* WireFormatLite::WriteSInt64ToArray(int field_number,
689 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
690 return WriteSInt64NoTagToArray(value, target);
692 inline uint8* WireFormatLite::WriteFixed32ToArray(int field_number,
695 target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
696 return WriteFixed32NoTagToArray(value, target);
698 inline uint8* WireFormatLite::WriteFixed64ToArray(int field_number,
701 target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
702 return WriteFixed64NoTagToArray(value, target);
704 inline uint8* WireFormatLite::WriteSFixed32ToArray(int field_number,
707 target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
708 return WriteSFixed32NoTagToArray(value, target);
710 inline uint8* WireFormatLite::WriteSFixed64ToArray(int field_number,
713 target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
714 return WriteSFixed64NoTagToArray(value, target);
716 inline uint8* WireFormatLite::WriteFloatToArray(int field_number,
719 target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
720 return WriteFloatNoTagToArray(value, target);
722 inline uint8* WireFormatLite::WriteDoubleToArray(int field_number,
725 target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
726 return WriteDoubleNoTagToArray(value, target);
728 inline uint8* WireFormatLite::WriteBoolToArray(int field_number,
731 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
732 return WriteBoolNoTagToArray(value, target);
734 inline uint8* WireFormatLite::WriteEnumToArray(int field_number,
737 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
738 return WriteEnumNoTagToArray(value, target);
741 inline uint8* WireFormatLite::WriteStringToArray(int field_number,
744 // String is for UTF-8 text only
745 // WARNING: In wire_format.cc, both strings and bytes are handled by
746 // WriteString() to avoid code duplication. If the implementations become
747 // different, you will need to update that usage.
748 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
749 return io::CodedOutputStream::WriteStringWithSizeToArray(value, target);
751 inline uint8* WireFormatLite::WriteBytesToArray(int field_number,
754 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
755 return io::CodedOutputStream::WriteStringWithSizeToArray(value, target);
759 inline uint8* WireFormatLite::WriteGroupToArray(int field_number,
760 const MessageLite& value,
762 target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
763 target = value.SerializeWithCachedSizesToArray(target);
764 return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
766 inline uint8* WireFormatLite::WriteMessageToArray(int field_number,
767 const MessageLite& value,
769 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
770 target = io::CodedOutputStream::WriteVarint32ToArray(
771 value.GetCachedSize(), target);
772 return value.SerializeWithCachedSizesToArray(target);
775 // See comment on ReadGroupNoVirtual to understand the need for this template
777 template<typename MessageType_WorkAroundCppLookupDefect>
778 inline uint8* WireFormatLite::WriteGroupNoVirtualToArray(
779 int field_number, const MessageType_WorkAroundCppLookupDefect& value,
781 target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
782 target = value.MessageType_WorkAroundCppLookupDefect
783 ::SerializeWithCachedSizesToArray(target);
784 return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
786 template<typename MessageType_WorkAroundCppLookupDefect>
787 inline uint8* WireFormatLite::WriteMessageNoVirtualToArray(
788 int field_number, const MessageType_WorkAroundCppLookupDefect& value,
790 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
791 target = io::CodedOutputStream::WriteVarint32ToArray(
792 value.MessageType_WorkAroundCppLookupDefect::GetCachedSize(), target);
793 return value.MessageType_WorkAroundCppLookupDefect
794 ::SerializeWithCachedSizesToArray(target);
797 // ===================================================================
799 inline int WireFormatLite::Int32Size(int32 value) {
800 return io::CodedOutputStream::VarintSize32SignExtended(value);
802 inline int WireFormatLite::Int64Size(int64 value) {
803 return io::CodedOutputStream::VarintSize64(static_cast<uint64>(value));
805 inline int WireFormatLite::UInt32Size(uint32 value) {
806 return io::CodedOutputStream::VarintSize32(value);
808 inline int WireFormatLite::UInt64Size(uint64 value) {
809 return io::CodedOutputStream::VarintSize64(value);
811 inline int WireFormatLite::SInt32Size(int32 value) {
812 return io::CodedOutputStream::VarintSize32(ZigZagEncode32(value));
814 inline int WireFormatLite::SInt64Size(int64 value) {
815 return io::CodedOutputStream::VarintSize64(ZigZagEncode64(value));
817 inline int WireFormatLite::EnumSize(int value) {
818 return io::CodedOutputStream::VarintSize32SignExtended(value);
821 inline int WireFormatLite::StringSize(const string& value) {
822 return io::CodedOutputStream::VarintSize32(value.size()) +
825 inline int WireFormatLite::BytesSize(const string& value) {
826 return io::CodedOutputStream::VarintSize32(value.size()) +
831 inline int WireFormatLite::GroupSize(const MessageLite& value) {
832 return value.ByteSize();
834 inline int WireFormatLite::MessageSize(const MessageLite& value) {
835 return LengthDelimitedSize(value.ByteSize());
838 // See comment on ReadGroupNoVirtual to understand the need for this template
840 template<typename MessageType_WorkAroundCppLookupDefect>
841 inline int WireFormatLite::GroupSizeNoVirtual(
842 const MessageType_WorkAroundCppLookupDefect& value) {
843 return value.MessageType_WorkAroundCppLookupDefect::ByteSize();
845 template<typename MessageType_WorkAroundCppLookupDefect>
846 inline int WireFormatLite::MessageSizeNoVirtual(
847 const MessageType_WorkAroundCppLookupDefect& value) {
848 return LengthDelimitedSize(
849 value.MessageType_WorkAroundCppLookupDefect::ByteSize());
852 inline int WireFormatLite::LengthDelimitedSize(int length) {
853 return io::CodedOutputStream::VarintSize32(length) + length;
856 } // namespace internal
857 } // namespace protobuf
859 } // namespace google
860 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__