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