1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #include <google/protobuf/wire_format_lite.h>
36 
37 #include <stack>
38 #include <string>
39 #include <vector>
40 #include <google/protobuf/stubs/logging.h>
41 #include <google/protobuf/stubs/common.h>
42 #include <google/protobuf/stubs/stringprintf.h>
43 #include <google/protobuf/io/coded_stream_inl.h>
44 #include <google/protobuf/io/zero_copy_stream.h>
45 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
46 #include <google/protobuf/port_def.inc>
47 
48 
49 namespace google {
50 namespace protobuf {
51 namespace internal {
52 
53 #if !defined(_MSC_VER) || _MSC_VER >= 1900
54 // Old version of MSVC doesn't like definitions of inline constants, GCC
55 // requires them.
56 const int WireFormatLite::kMessageSetItemStartTag;
57 const int WireFormatLite::kMessageSetItemEndTag;
58 const int WireFormatLite::kMessageSetTypeIdTag;
59 const int WireFormatLite::kMessageSetMessageTag;
60 
61 #endif
62 
63 // IBM xlC requires prefixing constants with WireFormatLite::
64 const size_t WireFormatLite::kMessageSetItemTagsSize =
65     io::CodedOutputStream::StaticVarintSize32<
66         WireFormatLite::kMessageSetItemStartTag>::value +
67     io::CodedOutputStream::StaticVarintSize32<
68         WireFormatLite::kMessageSetItemEndTag>::value +
69     io::CodedOutputStream::StaticVarintSize32<
70         WireFormatLite::kMessageSetTypeIdTag>::value +
71     io::CodedOutputStream::StaticVarintSize32<
72         WireFormatLite::kMessageSetMessageTag>::value;
73 
74 const WireFormatLite::CppType
75     WireFormatLite::kFieldTypeToCppTypeMap[MAX_FIELD_TYPE + 1] = {
76         static_cast<CppType>(0),  // 0 is reserved for errors
77 
78         CPPTYPE_DOUBLE,   // TYPE_DOUBLE
79         CPPTYPE_FLOAT,    // TYPE_FLOAT
80         CPPTYPE_INT64,    // TYPE_INT64
81         CPPTYPE_UINT64,   // TYPE_UINT64
82         CPPTYPE_INT32,    // TYPE_INT32
83         CPPTYPE_UINT64,   // TYPE_FIXED64
84         CPPTYPE_UINT32,   // TYPE_FIXED32
85         CPPTYPE_BOOL,     // TYPE_BOOL
86         CPPTYPE_STRING,   // TYPE_STRING
87         CPPTYPE_MESSAGE,  // TYPE_GROUP
88         CPPTYPE_MESSAGE,  // TYPE_MESSAGE
89         CPPTYPE_STRING,   // TYPE_BYTES
90         CPPTYPE_UINT32,   // TYPE_UINT32
91         CPPTYPE_ENUM,     // TYPE_ENUM
92         CPPTYPE_INT32,    // TYPE_SFIXED32
93         CPPTYPE_INT64,    // TYPE_SFIXED64
94         CPPTYPE_INT32,    // TYPE_SINT32
95         CPPTYPE_INT64,    // TYPE_SINT64
96 };
97 
98 const WireFormatLite::WireType
99     WireFormatLite::kWireTypeForFieldType[MAX_FIELD_TYPE + 1] = {
100         static_cast<WireFormatLite::WireType>(-1),  // invalid
101         WireFormatLite::WIRETYPE_FIXED64,           // TYPE_DOUBLE
102         WireFormatLite::WIRETYPE_FIXED32,           // TYPE_FLOAT
103         WireFormatLite::WIRETYPE_VARINT,            // TYPE_INT64
104         WireFormatLite::WIRETYPE_VARINT,            // TYPE_UINT64
105         WireFormatLite::WIRETYPE_VARINT,            // TYPE_INT32
106         WireFormatLite::WIRETYPE_FIXED64,           // TYPE_FIXED64
107         WireFormatLite::WIRETYPE_FIXED32,           // TYPE_FIXED32
108         WireFormatLite::WIRETYPE_VARINT,            // TYPE_BOOL
109         WireFormatLite::WIRETYPE_LENGTH_DELIMITED,  // TYPE_STRING
110         WireFormatLite::WIRETYPE_START_GROUP,       // TYPE_GROUP
111         WireFormatLite::WIRETYPE_LENGTH_DELIMITED,  // TYPE_MESSAGE
112         WireFormatLite::WIRETYPE_LENGTH_DELIMITED,  // TYPE_BYTES
113         WireFormatLite::WIRETYPE_VARINT,            // TYPE_UINT32
114         WireFormatLite::WIRETYPE_VARINT,            // TYPE_ENUM
115         WireFormatLite::WIRETYPE_FIXED32,           // TYPE_SFIXED32
116         WireFormatLite::WIRETYPE_FIXED64,           // TYPE_SFIXED64
117         WireFormatLite::WIRETYPE_VARINT,            // TYPE_SINT32
118         WireFormatLite::WIRETYPE_VARINT,            // TYPE_SINT64
119 };
120 
SkipField(io::CodedInputStream * input,uint32 tag)121 bool WireFormatLite::SkipField(io::CodedInputStream* input, uint32 tag) {
122   // Field number 0 is illegal.
123   if (WireFormatLite::GetTagFieldNumber(tag) == 0) return false;
124   switch (WireFormatLite::GetTagWireType(tag)) {
125     case WireFormatLite::WIRETYPE_VARINT: {
126       uint64 value;
127       if (!input->ReadVarint64(&value)) return false;
128       return true;
129     }
130     case WireFormatLite::WIRETYPE_FIXED64: {
131       uint64 value;
132       if (!input->ReadLittleEndian64(&value)) return false;
133       return true;
134     }
135     case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: {
136       uint32 length;
137       if (!input->ReadVarint32(&length)) return false;
138       if (!input->Skip(length)) return false;
139       return true;
140     }
141     case WireFormatLite::WIRETYPE_START_GROUP: {
142       if (!input->IncrementRecursionDepth()) return false;
143       if (!SkipMessage(input)) return false;
144       input->DecrementRecursionDepth();
145       // Check that the ending tag matched the starting tag.
146       if (!input->LastTagWas(
147               WireFormatLite::MakeTag(WireFormatLite::GetTagFieldNumber(tag),
148                                       WireFormatLite::WIRETYPE_END_GROUP))) {
149         return false;
150       }
151       return true;
152     }
153     case WireFormatLite::WIRETYPE_END_GROUP: {
154       return false;
155     }
156     case WireFormatLite::WIRETYPE_FIXED32: {
157       uint32 value;
158       if (!input->ReadLittleEndian32(&value)) return false;
159       return true;
160     }
161     default: {
162       return false;
163     }
164   }
165 }
166 
SkipField(io::CodedInputStream * input,uint32 tag,io::CodedOutputStream * output)167 bool WireFormatLite::SkipField(io::CodedInputStream* input, uint32 tag,
168                                io::CodedOutputStream* output) {
169   // Field number 0 is illegal.
170   if (WireFormatLite::GetTagFieldNumber(tag) == 0) return false;
171   switch (WireFormatLite::GetTagWireType(tag)) {
172     case WireFormatLite::WIRETYPE_VARINT: {
173       uint64 value;
174       if (!input->ReadVarint64(&value)) return false;
175       output->WriteVarint32(tag);
176       output->WriteVarint64(value);
177       return true;
178     }
179     case WireFormatLite::WIRETYPE_FIXED64: {
180       uint64 value;
181       if (!input->ReadLittleEndian64(&value)) return false;
182       output->WriteVarint32(tag);
183       output->WriteLittleEndian64(value);
184       return true;
185     }
186     case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: {
187       uint32 length;
188       if (!input->ReadVarint32(&length)) return false;
189       output->WriteVarint32(tag);
190       output->WriteVarint32(length);
191       // TODO(mkilavuz): Provide API to prevent extra string copying.
192       std::string temp;
193       if (!input->ReadString(&temp, length)) return false;
194       output->WriteString(temp);
195       return true;
196     }
197     case WireFormatLite::WIRETYPE_START_GROUP: {
198       output->WriteVarint32(tag);
199       if (!input->IncrementRecursionDepth()) return false;
200       if (!SkipMessage(input, output)) return false;
201       input->DecrementRecursionDepth();
202       // Check that the ending tag matched the starting tag.
203       if (!input->LastTagWas(
204               WireFormatLite::MakeTag(WireFormatLite::GetTagFieldNumber(tag),
205                                       WireFormatLite::WIRETYPE_END_GROUP))) {
206         return false;
207       }
208       return true;
209     }
210     case WireFormatLite::WIRETYPE_END_GROUP: {
211       return false;
212     }
213     case WireFormatLite::WIRETYPE_FIXED32: {
214       uint32 value;
215       if (!input->ReadLittleEndian32(&value)) return false;
216       output->WriteVarint32(tag);
217       output->WriteLittleEndian32(value);
218       return true;
219     }
220     default: {
221       return false;
222     }
223   }
224 }
225 
SkipMessage(io::CodedInputStream * input)226 bool WireFormatLite::SkipMessage(io::CodedInputStream* input) {
227   while (true) {
228     uint32 tag = input->ReadTag();
229     if (tag == 0) {
230       // End of input.  This is a valid place to end, so return true.
231       return true;
232     }
233 
234     WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
235 
236     if (wire_type == WireFormatLite::WIRETYPE_END_GROUP) {
237       // Must be the end of the message.
238       return true;
239     }
240 
241     if (!SkipField(input, tag)) return false;
242   }
243 }
244 
SkipMessage(io::CodedInputStream * input,io::CodedOutputStream * output)245 bool WireFormatLite::SkipMessage(io::CodedInputStream* input,
246                                  io::CodedOutputStream* output) {
247   while (true) {
248     uint32 tag = input->ReadTag();
249     if (tag == 0) {
250       // End of input.  This is a valid place to end, so return true.
251       return true;
252     }
253 
254     WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
255 
256     if (wire_type == WireFormatLite::WIRETYPE_END_GROUP) {
257       output->WriteVarint32(tag);
258       // Must be the end of the message.
259       return true;
260     }
261 
262     if (!SkipField(input, tag, output)) return false;
263   }
264 }
265 
SkipField(io::CodedInputStream * input,uint32 tag)266 bool FieldSkipper::SkipField(io::CodedInputStream* input, uint32 tag) {
267   return WireFormatLite::SkipField(input, tag);
268 }
269 
SkipMessage(io::CodedInputStream * input)270 bool FieldSkipper::SkipMessage(io::CodedInputStream* input) {
271   return WireFormatLite::SkipMessage(input);
272 }
273 
SkipUnknownEnum(int,int)274 void FieldSkipper::SkipUnknownEnum(int /* field_number */, int /* value */) {
275   // Nothing.
276 }
277 
SkipField(io::CodedInputStream * input,uint32 tag)278 bool CodedOutputStreamFieldSkipper::SkipField(io::CodedInputStream* input,
279                                               uint32 tag) {
280   return WireFormatLite::SkipField(input, tag, unknown_fields_);
281 }
282 
SkipMessage(io::CodedInputStream * input)283 bool CodedOutputStreamFieldSkipper::SkipMessage(io::CodedInputStream* input) {
284   return WireFormatLite::SkipMessage(input, unknown_fields_);
285 }
286 
SkipUnknownEnum(int field_number,int value)287 void CodedOutputStreamFieldSkipper::SkipUnknownEnum(int field_number,
288                                                     int value) {
289   unknown_fields_->WriteVarint32(field_number);
290   unknown_fields_->WriteVarint64(value);
291 }
292 
ReadPackedEnumPreserveUnknowns(io::CodedInputStream * input,int field_number,bool (* is_valid)(int),io::CodedOutputStream * unknown_fields_stream,RepeatedField<int> * values)293 bool WireFormatLite::ReadPackedEnumPreserveUnknowns(
294     io::CodedInputStream* input, int field_number, bool (*is_valid)(int),
295     io::CodedOutputStream* unknown_fields_stream, RepeatedField<int>* values) {
296   uint32 length;
297   if (!input->ReadVarint32(&length)) return false;
298   io::CodedInputStream::Limit limit = input->PushLimit(length);
299   while (input->BytesUntilLimit() > 0) {
300     int value;
301     if (!ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(input, &value)) {
302       return false;
303     }
304     if (is_valid == NULL || is_valid(value)) {
305       values->Add(value);
306     } else {
307       uint32 tag = WireFormatLite::MakeTag(field_number,
308                                            WireFormatLite::WIRETYPE_VARINT);
309       unknown_fields_stream->WriteVarint32(tag);
310       unknown_fields_stream->WriteVarint32(value);
311     }
312   }
313   input->PopLimit(limit);
314   return true;
315 }
316 
317 #if !defined(PROTOBUF_LITTLE_ENDIAN)
318 
319 namespace {
EncodeFixedSizeValue(float v,uint8 * dest)320 void EncodeFixedSizeValue(float v, uint8* dest) {
321   WireFormatLite::WriteFloatNoTagToArray(v, dest);
322 }
323 
EncodeFixedSizeValue(double v,uint8 * dest)324 void EncodeFixedSizeValue(double v, uint8* dest) {
325   WireFormatLite::WriteDoubleNoTagToArray(v, dest);
326 }
327 
EncodeFixedSizeValue(uint32 v,uint8 * dest)328 void EncodeFixedSizeValue(uint32 v, uint8* dest) {
329   WireFormatLite::WriteFixed32NoTagToArray(v, dest);
330 }
331 
EncodeFixedSizeValue(uint64 v,uint8 * dest)332 void EncodeFixedSizeValue(uint64 v, uint8* dest) {
333   WireFormatLite::WriteFixed64NoTagToArray(v, dest);
334 }
335 
EncodeFixedSizeValue(int32 v,uint8 * dest)336 void EncodeFixedSizeValue(int32 v, uint8* dest) {
337   WireFormatLite::WriteSFixed32NoTagToArray(v, dest);
338 }
339 
EncodeFixedSizeValue(int64 v,uint8 * dest)340 void EncodeFixedSizeValue(int64 v, uint8* dest) {
341   WireFormatLite::WriteSFixed64NoTagToArray(v, dest);
342 }
343 
EncodeFixedSizeValue(bool v,uint8 * dest)344 void EncodeFixedSizeValue(bool v, uint8* dest) {
345   WireFormatLite::WriteBoolNoTagToArray(v, dest);
346 }
347 }  // anonymous namespace
348 
349 #endif  // !defined(PROTOBUF_LITTLE_ENDIAN)
350 
351 template <typename CType>
WriteArray(const CType * a,int n,io::CodedOutputStream * output)352 static void WriteArray(const CType* a, int n, io::CodedOutputStream* output) {
353 #if defined(PROTOBUF_LITTLE_ENDIAN)
354   output->WriteRaw(reinterpret_cast<const char*>(a), n * sizeof(a[0]));
355 #else
356   const int kAtATime = 128;
357   uint8 buf[sizeof(CType) * kAtATime];
358   for (int i = 0; i < n; i += kAtATime) {
359     int to_do = std::min(kAtATime, n - i);
360     uint8* ptr = buf;
361     for (int j = 0; j < to_do; j++) {
362       EncodeFixedSizeValue(a[i + j], ptr);
363       ptr += sizeof(a[0]);
364     }
365     output->WriteRaw(buf, to_do * sizeof(a[0]));
366   }
367 #endif
368 }
369 
WriteFloatArray(const float * a,int n,io::CodedOutputStream * output)370 void WireFormatLite::WriteFloatArray(const float* a, int n,
371                                      io::CodedOutputStream* output) {
372   WriteArray<float>(a, n, output);
373 }
374 
WriteDoubleArray(const double * a,int n,io::CodedOutputStream * output)375 void WireFormatLite::WriteDoubleArray(const double* a, int n,
376                                       io::CodedOutputStream* output) {
377   WriteArray<double>(a, n, output);
378 }
379 
WriteFixed32Array(const uint32 * a,int n,io::CodedOutputStream * output)380 void WireFormatLite::WriteFixed32Array(const uint32* a, int n,
381                                        io::CodedOutputStream* output) {
382   WriteArray<uint32>(a, n, output);
383 }
384 
WriteFixed64Array(const uint64 * a,int n,io::CodedOutputStream * output)385 void WireFormatLite::WriteFixed64Array(const uint64* a, int n,
386                                        io::CodedOutputStream* output) {
387   WriteArray<uint64>(a, n, output);
388 }
389 
WriteSFixed32Array(const int32 * a,int n,io::CodedOutputStream * output)390 void WireFormatLite::WriteSFixed32Array(const int32* a, int n,
391                                         io::CodedOutputStream* output) {
392   WriteArray<int32>(a, n, output);
393 }
394 
WriteSFixed64Array(const int64 * a,int n,io::CodedOutputStream * output)395 void WireFormatLite::WriteSFixed64Array(const int64* a, int n,
396                                         io::CodedOutputStream* output) {
397   WriteArray<int64>(a, n, output);
398 }
399 
WriteBoolArray(const bool * a,int n,io::CodedOutputStream * output)400 void WireFormatLite::WriteBoolArray(const bool* a, int n,
401                                     io::CodedOutputStream* output) {
402   WriteArray<bool>(a, n, output);
403 }
404 
WriteInt32(int field_number,int32 value,io::CodedOutputStream * output)405 void WireFormatLite::WriteInt32(int field_number, int32 value,
406                                 io::CodedOutputStream* output) {
407   WriteTag(field_number, WIRETYPE_VARINT, output);
408   WriteInt32NoTag(value, output);
409 }
WriteInt64(int field_number,int64 value,io::CodedOutputStream * output)410 void WireFormatLite::WriteInt64(int field_number, int64 value,
411                                 io::CodedOutputStream* output) {
412   WriteTag(field_number, WIRETYPE_VARINT, output);
413   WriteInt64NoTag(value, output);
414 }
WriteUInt32(int field_number,uint32 value,io::CodedOutputStream * output)415 void WireFormatLite::WriteUInt32(int field_number, uint32 value,
416                                  io::CodedOutputStream* output) {
417   WriteTag(field_number, WIRETYPE_VARINT, output);
418   WriteUInt32NoTag(value, output);
419 }
WriteUInt64(int field_number,uint64 value,io::CodedOutputStream * output)420 void WireFormatLite::WriteUInt64(int field_number, uint64 value,
421                                  io::CodedOutputStream* output) {
422   WriteTag(field_number, WIRETYPE_VARINT, output);
423   WriteUInt64NoTag(value, output);
424 }
WriteSInt32(int field_number,int32 value,io::CodedOutputStream * output)425 void WireFormatLite::WriteSInt32(int field_number, int32 value,
426                                  io::CodedOutputStream* output) {
427   WriteTag(field_number, WIRETYPE_VARINT, output);
428   WriteSInt32NoTag(value, output);
429 }
WriteSInt64(int field_number,int64 value,io::CodedOutputStream * output)430 void WireFormatLite::WriteSInt64(int field_number, int64 value,
431                                  io::CodedOutputStream* output) {
432   WriteTag(field_number, WIRETYPE_VARINT, output);
433   WriteSInt64NoTag(value, output);
434 }
WriteFixed32(int field_number,uint32 value,io::CodedOutputStream * output)435 void WireFormatLite::WriteFixed32(int field_number, uint32 value,
436                                   io::CodedOutputStream* output) {
437   WriteTag(field_number, WIRETYPE_FIXED32, output);
438   WriteFixed32NoTag(value, output);
439 }
WriteFixed64(int field_number,uint64 value,io::CodedOutputStream * output)440 void WireFormatLite::WriteFixed64(int field_number, uint64 value,
441                                   io::CodedOutputStream* output) {
442   WriteTag(field_number, WIRETYPE_FIXED64, output);
443   WriteFixed64NoTag(value, output);
444 }
WriteSFixed32(int field_number,int32 value,io::CodedOutputStream * output)445 void WireFormatLite::WriteSFixed32(int field_number, int32 value,
446                                    io::CodedOutputStream* output) {
447   WriteTag(field_number, WIRETYPE_FIXED32, output);
448   WriteSFixed32NoTag(value, output);
449 }
WriteSFixed64(int field_number,int64 value,io::CodedOutputStream * output)450 void WireFormatLite::WriteSFixed64(int field_number, int64 value,
451                                    io::CodedOutputStream* output) {
452   WriteTag(field_number, WIRETYPE_FIXED64, output);
453   WriteSFixed64NoTag(value, output);
454 }
WriteFloat(int field_number,float value,io::CodedOutputStream * output)455 void WireFormatLite::WriteFloat(int field_number, float value,
456                                 io::CodedOutputStream* output) {
457   WriteTag(field_number, WIRETYPE_FIXED32, output);
458   WriteFloatNoTag(value, output);
459 }
WriteDouble(int field_number,double value,io::CodedOutputStream * output)460 void WireFormatLite::WriteDouble(int field_number, double value,
461                                  io::CodedOutputStream* output) {
462   WriteTag(field_number, WIRETYPE_FIXED64, output);
463   WriteDoubleNoTag(value, output);
464 }
WriteBool(int field_number,bool value,io::CodedOutputStream * output)465 void WireFormatLite::WriteBool(int field_number, bool value,
466                                io::CodedOutputStream* output) {
467   WriteTag(field_number, WIRETYPE_VARINT, output);
468   WriteBoolNoTag(value, output);
469 }
WriteEnum(int field_number,int value,io::CodedOutputStream * output)470 void WireFormatLite::WriteEnum(int field_number, int value,
471                                io::CodedOutputStream* output) {
472   WriteTag(field_number, WIRETYPE_VARINT, output);
473   WriteEnumNoTag(value, output);
474 }
475 
WriteString(int field_number,const std::string & value,io::CodedOutputStream * output)476 void WireFormatLite::WriteString(int field_number, const std::string& value,
477                                  io::CodedOutputStream* output) {
478   // String is for UTF-8 text only
479   WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
480   GOOGLE_CHECK_LE(value.size(), kint32max);
481   output->WriteVarint32(value.size());
482   output->WriteString(value);
483 }
WriteStringMaybeAliased(int field_number,const std::string & value,io::CodedOutputStream * output)484 void WireFormatLite::WriteStringMaybeAliased(int field_number,
485                                              const std::string& value,
486                                              io::CodedOutputStream* output) {
487   // String is for UTF-8 text only
488   WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
489   GOOGLE_CHECK_LE(value.size(), kint32max);
490   output->WriteVarint32(value.size());
491   output->WriteRawMaybeAliased(value.data(), value.size());
492 }
WriteBytes(int field_number,const std::string & value,io::CodedOutputStream * output)493 void WireFormatLite::WriteBytes(int field_number, const std::string& value,
494                                 io::CodedOutputStream* output) {
495   WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
496   GOOGLE_CHECK_LE(value.size(), kint32max);
497   output->WriteVarint32(value.size());
498   output->WriteString(value);
499 }
WriteBytesMaybeAliased(int field_number,const std::string & value,io::CodedOutputStream * output)500 void WireFormatLite::WriteBytesMaybeAliased(int field_number,
501                                             const std::string& value,
502                                             io::CodedOutputStream* output) {
503   WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
504   GOOGLE_CHECK_LE(value.size(), kint32max);
505   output->WriteVarint32(value.size());
506   output->WriteRawMaybeAliased(value.data(), value.size());
507 }
508 
509 
WriteGroup(int field_number,const MessageLite & value,io::CodedOutputStream * output)510 void WireFormatLite::WriteGroup(int field_number, const MessageLite& value,
511                                 io::CodedOutputStream* output) {
512   WriteTag(field_number, WIRETYPE_START_GROUP, output);
513   value.SerializeWithCachedSizes(output);
514   WriteTag(field_number, WIRETYPE_END_GROUP, output);
515 }
516 
WriteMessage(int field_number,const MessageLite & value,io::CodedOutputStream * output)517 void WireFormatLite::WriteMessage(int field_number, const MessageLite& value,
518                                   io::CodedOutputStream* output) {
519   WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
520   const int size = value.GetCachedSize();
521   output->WriteVarint32(size);
522   value.SerializeWithCachedSizes(output);
523 }
524 
WriteSubMessageMaybeToArray(int size,const MessageLite & value,io::CodedOutputStream * output)525 void WireFormatLite::WriteSubMessageMaybeToArray(
526     int size, const MessageLite& value, io::CodedOutputStream* output) {
527   if (!output->IsSerializationDeterministic()) {
528     uint8* target = output->GetDirectBufferForNBytesAndAdvance(size);
529     if (target != nullptr) {
530       uint8* end = value.InternalSerializeWithCachedSizesToArray(target);
531       GOOGLE_DCHECK_EQ(end - target, size);
532       return;
533     }
534   }
535   value.SerializeWithCachedSizes(output);
536 }
537 
WriteGroupMaybeToArray(int field_number,const MessageLite & value,io::CodedOutputStream * output)538 void WireFormatLite::WriteGroupMaybeToArray(int field_number,
539                                             const MessageLite& value,
540                                             io::CodedOutputStream* output) {
541   WriteTag(field_number, WIRETYPE_START_GROUP, output);
542   const int size = value.GetCachedSize();
543   WriteSubMessageMaybeToArray(size, value, output);
544   WriteTag(field_number, WIRETYPE_END_GROUP, output);
545 }
546 
WriteMessageMaybeToArray(int field_number,const MessageLite & value,io::CodedOutputStream * output)547 void WireFormatLite::WriteMessageMaybeToArray(int field_number,
548                                               const MessageLite& value,
549                                               io::CodedOutputStream* output) {
550   WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
551   const int size = value.GetCachedSize();
552   output->WriteVarint32(size);
553   WriteSubMessageMaybeToArray(size, value, output);
554 }
555 
556 PROTOBUF_ALWAYS_INLINE static bool ReadBytesToString(
557     io::CodedInputStream* input, std::string* value);
ReadBytesToString(io::CodedInputStream * input,std::string * value)558 inline static bool ReadBytesToString(io::CodedInputStream* input,
559                                      std::string* value) {
560   uint32 length;
561   return input->ReadVarint32(&length) &&
562          input->InternalReadStringInline(value, length);
563 }
564 
ReadBytes(io::CodedInputStream * input,std::string * value)565 bool WireFormatLite::ReadBytes(io::CodedInputStream* input,
566                                std::string* value) {
567   return ReadBytesToString(input, value);
568 }
569 
ReadBytes(io::CodedInputStream * input,std::string ** p)570 bool WireFormatLite::ReadBytes(io::CodedInputStream* input, std::string** p) {
571   if (*p == &GetEmptyStringAlreadyInited()) {
572     *p = new std::string();
573   }
574   return ReadBytesToString(input, *p);
575 }
576 
PrintUTF8ErrorLog(const char * field_name,const char * operation_str,bool emit_stacktrace)577 void PrintUTF8ErrorLog(const char* field_name, const char* operation_str,
578                        bool emit_stacktrace) {
579   std::string stacktrace;
580   std::string quoted_field_name = "";
581   if (field_name != nullptr) {
582     quoted_field_name = StringPrintf(" '%s'", field_name);
583   }
584   GOOGLE_LOG(ERROR) << "String field" << quoted_field_name << " contains invalid "
585              << "UTF-8 data when " << operation_str << " a protocol "
586              << "buffer. Use the 'bytes' type if you intend to send raw "
587              << "bytes. " << stacktrace;
588 }
589 
VerifyUtf8String(const char * data,int size,Operation op,const char * field_name)590 bool WireFormatLite::VerifyUtf8String(const char* data, int size, Operation op,
591                                       const char* field_name) {
592   if (!IsStructurallyValidUTF8(data, size)) {
593     const char* operation_str = NULL;
594     switch (op) {
595       case PARSE:
596         operation_str = "parsing";
597         break;
598       case SERIALIZE:
599         operation_str = "serializing";
600         break;
601         // no default case: have the compiler warn if a case is not covered.
602     }
603     PrintUTF8ErrorLog(field_name, operation_str, false);
604     return false;
605   }
606   return true;
607 }
608 
609 // this code is deliberately written such that clang makes it into really
610 // efficient SSE code.
611 template <bool ZigZag, bool SignExtended, typename T>
VarintSize(const T * data,const int n)612 static size_t VarintSize(const T* data, const int n) {
613 #if __cplusplus >= 201103L
614   static_assert(sizeof(T) == 4, "This routine only works for 32 bit integers");
615   // is_unsigned<T> => !ZigZag
616   static_assert(
617       (std::is_unsigned<T>::value ^ ZigZag) || std::is_signed<T>::value,
618       "Cannot ZigZag encode unsigned types");
619   // is_unsigned<T> => !SignExtended
620   static_assert(
621       (std::is_unsigned<T>::value ^ SignExtended) || std::is_signed<T>::value,
622       "Cannot SignExtended unsigned types");
623   static_assert(!(SignExtended && ZigZag),
624                 "Cannot SignExtended and ZigZag on the same type");
625 #endif
626   uint32 sum = n;
627   uint32 msb_sum = 0;
628   for (int i = 0; i < n; i++) {
629     uint32 x = data[i];
630     if (ZigZag) {
631       x = WireFormatLite::ZigZagEncode32(x);
632     } else if (SignExtended) {
633       msb_sum += x >> 31;
634     }
635     // clang is so smart that it produces optimal SSE sequence unrolling
636     // the loop 8 ints at a time. With a sequence of 4
637     // cmpres = cmpgt x, sizeclass  ( -1 or 0)
638     // sum = sum - cmpres
639     if (x > 0x7F) sum++;
640     if (x > 0x3FFF) sum++;
641     if (x > 0x1FFFFF) sum++;
642     if (x > 0xFFFFFFF) sum++;
643   }
644   if (SignExtended) sum += msb_sum * 5;
645   return sum;
646 }
647 
648 template <bool ZigZag, typename T>
VarintSize64(const T * data,const int n)649 static size_t VarintSize64(const T* data, const int n) {
650 #if __cplusplus >= 201103L
651   static_assert(sizeof(T) == 8, "This routine only works for 64 bit integers");
652   // is_unsigned<T> => !ZigZag
653   static_assert(!ZigZag || !std::is_unsigned<T>::value,
654                 "Cannot ZigZag encode unsigned types");
655 #endif
656   uint64 sum = n;
657   for (int i = 0; i < n; i++) {
658     uint64 x = data[i];
659     if (ZigZag) {
660       x = WireFormatLite::ZigZagEncode64(x);
661     }
662     // First step is a binary search, we can't branch in sse so we use the
663     // result of the compare to adjust sum and appropriately. This code is
664     // written to make clang recognize the vectorization.
665     uint64 tmp = x >= (static_cast<uint64>(1) << 35) ? -1 : 0;
666     sum += 5 & tmp;
667     x >>= 35 & tmp;
668     if (x > 0x7F) sum++;
669     if (x > 0x3FFF) sum++;
670     if (x > 0x1FFFFF) sum++;
671     if (x > 0xFFFFFFF) sum++;
672   }
673   return sum;
674 }
675 
676 // GCC does not recognize the vectorization opportunity
677 // and other platforms are untested, in those cases using the optimized
678 // varint size routine for each element is faster.
679 // Hence we enable it only for clang
680 #if defined(__SSE__) && defined(__clang__)
Int32Size(const RepeatedField<int32> & value)681 size_t WireFormatLite::Int32Size(const RepeatedField<int32>& value) {
682   return VarintSize<false, true>(value.data(), value.size());
683 }
684 
UInt32Size(const RepeatedField<uint32> & value)685 size_t WireFormatLite::UInt32Size(const RepeatedField<uint32>& value) {
686   return VarintSize<false, false>(value.data(), value.size());
687 }
688 
SInt32Size(const RepeatedField<int32> & value)689 size_t WireFormatLite::SInt32Size(const RepeatedField<int32>& value) {
690   return VarintSize<true, false>(value.data(), value.size());
691 }
692 
EnumSize(const RepeatedField<int> & value)693 size_t WireFormatLite::EnumSize(const RepeatedField<int>& value) {
694   // On ILP64, sizeof(int) == 8, which would require a different template.
695   return VarintSize<false, true>(value.data(), value.size());
696 }
697 
698 #else  // !(defined(__SSE4_1__) && defined(__clang__))
699 
Int32Size(const RepeatedField<int32> & value)700 size_t WireFormatLite::Int32Size(const RepeatedField<int32>& value) {
701   size_t out = 0;
702   const int n = value.size();
703   for (int i = 0; i < n; i++) {
704     out += Int32Size(value.Get(i));
705   }
706   return out;
707 }
708 
UInt32Size(const RepeatedField<uint32> & value)709 size_t WireFormatLite::UInt32Size(const RepeatedField<uint32>& value) {
710   size_t out = 0;
711   const int n = value.size();
712   for (int i = 0; i < n; i++) {
713     out += UInt32Size(value.Get(i));
714   }
715   return out;
716 }
717 
SInt32Size(const RepeatedField<int32> & value)718 size_t WireFormatLite::SInt32Size(const RepeatedField<int32>& value) {
719   size_t out = 0;
720   const int n = value.size();
721   for (int i = 0; i < n; i++) {
722     out += SInt32Size(value.Get(i));
723   }
724   return out;
725 }
726 
EnumSize(const RepeatedField<int> & value)727 size_t WireFormatLite::EnumSize(const RepeatedField<int>& value) {
728   size_t out = 0;
729   const int n = value.size();
730   for (int i = 0; i < n; i++) {
731     out += EnumSize(value.Get(i));
732   }
733   return out;
734 }
735 
736 #endif
737 
738 // Micro benchmarks show that the SSE improved loop only starts beating
739 // the normal loop on Haswell platforms and then only for >32 ints. We
740 // disable this for now. Some specialized users might find it worthwhile to
741 // enable this.
742 #define USE_SSE_FOR_64_BIT_INTEGER_ARRAYS 0
743 #if USE_SSE_FOR_64_BIT_INTEGER_ARRAYS
Int64Size(const RepeatedField<int64> & value)744 size_t WireFormatLite::Int64Size(const RepeatedField<int64>& value) {
745   return VarintSize64<false>(value.data(), value.size());
746 }
747 
UInt64Size(const RepeatedField<uint64> & value)748 size_t WireFormatLite::UInt64Size(const RepeatedField<uint64>& value) {
749   return VarintSize64<false>(value.data(), value.size());
750 }
751 
SInt64Size(const RepeatedField<int64> & value)752 size_t WireFormatLite::SInt64Size(const RepeatedField<int64>& value) {
753   return VarintSize64<true>(value.data(), value.size());
754 }
755 
756 #else
757 
Int64Size(const RepeatedField<int64> & value)758 size_t WireFormatLite::Int64Size(const RepeatedField<int64>& value) {
759   size_t out = 0;
760   const int n = value.size();
761   for (int i = 0; i < n; i++) {
762     out += Int64Size(value.Get(i));
763   }
764   return out;
765 }
766 
UInt64Size(const RepeatedField<uint64> & value)767 size_t WireFormatLite::UInt64Size(const RepeatedField<uint64>& value) {
768   size_t out = 0;
769   const int n = value.size();
770   for (int i = 0; i < n; i++) {
771     out += UInt64Size(value.Get(i));
772   }
773   return out;
774 }
775 
SInt64Size(const RepeatedField<int64> & value)776 size_t WireFormatLite::SInt64Size(const RepeatedField<int64>& value) {
777   size_t out = 0;
778   const int n = value.size();
779   for (int i = 0; i < n; i++) {
780     out += SInt64Size(value.Get(i));
781   }
782   return out;
783 }
784 
785 #endif
786 
787 }  // namespace internal
788 }  // namespace protobuf
789 }  // namespace google
790