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