| 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/generated_message_util.h" |
| 36 |
|
| 37 |
#include <atomic> |
| 38 |
#include <limits> |
| 39 |
#include <vector> |
| 40 |
|
| 41 |
#include "google/protobuf/arenastring.h" |
| 42 |
#include "google/protobuf/extension_set.h" |
| 43 |
#include "google/protobuf/io/coded_stream.h" |
| 44 |
#include "google/protobuf/io/zero_copy_stream_impl_lite.h" |
| 45 |
#include "google/protobuf/message_lite.h" |
| 46 |
#include "google/protobuf/metadata_lite.h" |
| 47 |
#include "google/protobuf/repeated_field.h" |
| 48 |
#include "google/protobuf/wire_format_lite.h" |
| 49 |
|
| 50 |
// Must be included last |
| 51 |
#include "google/protobuf/port_def.inc" |
| 52 |
|
| 53 |
PROTOBUF_PRAGMA_INIT_SEG |
| 54 |
|
| 55 |
|
| 56 |
namespace google { |
| 57 |
namespace protobuf { |
| 58 |
namespace internal { |
| 59 |
|
| 60 |
void DestroyMessage(const void* message) { |
| 61 |
static_cast<const MessageLite*>(message)->~MessageLite(); |
| 62 |
} |
| 63 |
void DestroyString(const void* s) { |
| 64 |
static_cast<const std::string*>(s)->~basic_string(); |
| 65 |
} |
| 66 |
|
| 67 |
PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT |
| 68 |
PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 ExplicitlyConstructedArenaString |
| 69 |
fixed_address_empty_string{}; // NOLINT |
| 70 |
|
| 71 |
|
| 72 |
PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT const EmptyCord empty_cord_; |
| 73 |
|
| 74 |
PROTOBUF_CONSTINIT std::atomic<bool> init_protobuf_defaults_state{false}; |
| 75 |
static bool InitProtobufDefaultsImpl() { |
| 76 |
fixed_address_empty_string.DefaultConstruct(); |
| 77 |
OnShutdownDestroyString(fixed_address_empty_string.get_mutable()); |
| 78 |
|
| 79 |
|
| 80 |
init_protobuf_defaults_state.store(true, std::memory_order_release); |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
void InitProtobufDefaultsSlow() { |
| 85 |
static bool is_inited = InitProtobufDefaultsImpl(); |
| 86 |
(void)is_inited; |
| 87 |
} |
| 88 |
// Force the initialization of the empty string. |
| 89 |
// Normally, registration would do it, but we don't have any guarantee that |
| 90 |
// there is any object with reflection. |
| 91 |
PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 static std::true_type init_empty_string = |
| 92 |
(InitProtobufDefaultsSlow(), std::true_type{}); |
| 93 |
|
| 94 |
size_t StringSpaceUsedExcludingSelfLong(const std::string& str) { |
| 95 |
const void* start = &str; |
| 96 |
const void* end = &str + 1; |
| 97 |
if (start <= str.data() && str.data() < end) { |
| 98 |
// The string's data is stored inside the string object itself. |
| 99 |
return 0; |
| 100 |
} else { |
| 101 |
return str.capacity(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
template <typename T> |
| 106 |
const T& Get(const void* ptr) { |
| 107 |
return *static_cast<const T*>(ptr); |
| 108 |
} |
| 109 |
|
| 110 |
// PrimitiveTypeHelper is a wrapper around the interface of WireFormatLite. |
| 111 |
// WireFormatLite has a very inconvenient interface with respect to template |
| 112 |
// meta-programming. This class wraps the different named functions into |
| 113 |
// a single Serialize / SerializeToArray interface. |
| 114 |
template <int type> |
| 115 |
struct PrimitiveTypeHelper; |
| 116 |
|
| 117 |
template <> |
| 118 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_BOOL> { |
| 119 |
typedef bool Type; |
| 120 |
static void Serialize(const void* ptr, io::CodedOutputStream* output) { |
| 121 |
WireFormatLite::WriteBoolNoTag(Get<bool>(ptr), output); |
| 122 |
} |
| 123 |
static uint8_t* SerializeToArray(const void* ptr, uint8_t* buffer) { |
| 124 |
return WireFormatLite::WriteBoolNoTagToArray(Get<Type>(ptr), buffer); |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
template <> |
| 129 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_INT32> { |
| 130 |
typedef int32_t Type; |
| 131 |
static void Serialize(const void* ptr, io::CodedOutputStream* output) { |
| 132 |
WireFormatLite::WriteInt32NoTag(Get<int32_t>(ptr), output); |
| 133 |
} |
| 134 |
static uint8_t* SerializeToArray(const void* ptr, uint8_t* buffer) { |
| 135 |
return WireFormatLite::WriteInt32NoTagToArray(Get<Type>(ptr), buffer); |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
template <> |
| 140 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_SINT32> { |
| 141 |
typedef int32_t Type; |
| 142 |
static void Serialize(const void* ptr, io::CodedOutputStream* output) { |
| 143 |
WireFormatLite::WriteSInt32NoTag(Get<int32_t>(ptr), output); |
| 144 |
} |
| 145 |
static uint8_t* SerializeToArray(const void* ptr, uint8_t* buffer) { |
| 146 |
return WireFormatLite::WriteSInt32NoTagToArray(Get<Type>(ptr), buffer); |
| 147 |
} |
| 148 |
}; |
| 149 |
|
| 150 |
template <> |
| 151 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_UINT32> { |
| 152 |
typedef uint32_t Type; |
| 153 |
static void Serialize(const void* ptr, io::CodedOutputStream* output) { |
| 154 |
WireFormatLite::WriteUInt32NoTag(Get<uint32_t>(ptr), output); |
| 155 |
} |
| 156 |
static uint8_t* SerializeToArray(const void* ptr, uint8_t* buffer) { |
| 157 |
return WireFormatLite::WriteUInt32NoTagToArray(Get<Type>(ptr), buffer); |
| 158 |
} |
| 159 |
}; |
| 160 |
template <> |
| 161 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_INT64> { |
| 162 |
typedef int64_t Type; |
| 163 |
static void Serialize(const void* ptr, io::CodedOutputStream* output) { |
| 164 |
WireFormatLite::WriteInt64NoTag(Get<int64_t>(ptr), output); |
| 165 |
} |
| 166 |
static uint8_t* SerializeToArray(const void* ptr, uint8_t* buffer) { |
| 167 |
return WireFormatLite::WriteInt64NoTagToArray(Get<Type>(ptr), buffer); |
| 168 |
} |
| 169 |
}; |
| 170 |
|
| 171 |
template <> |
| 172 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_SINT64> { |
| 173 |
typedef int64_t Type; |
| 174 |
static void Serialize(const void* ptr, io::CodedOutputStream* output) { |
| 175 |
WireFormatLite::WriteSInt64NoTag(Get<int64_t>(ptr), output); |
| 176 |
} |
| 177 |
static uint8_t* SerializeToArray(const void* ptr, uint8_t* buffer) { |
| 178 |
return WireFormatLite::WriteSInt64NoTagToArray(Get<Type>(ptr), buffer); |
| 179 |
} |
| 180 |
}; |
| 181 |
template <> |
| 182 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_UINT64> { |
| 183 |
typedef uint64_t Type; |
| 184 |
static void Serialize(const void* ptr, io::CodedOutputStream* output) { |
| 185 |
WireFormatLite::WriteUInt64NoTag(Get<uint64_t>(ptr), output); |
| 186 |
} |
| 187 |
static uint8_t* SerializeToArray(const void* ptr, uint8_t* buffer) { |
| 188 |
return WireFormatLite::WriteUInt64NoTagToArray(Get<Type>(ptr), buffer); |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
template <> |
| 193 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_FIXED32> { |
| 194 |
typedef uint32_t Type; |
| 195 |
static void Serialize(const void* ptr, io::CodedOutputStream* output) { |
| 196 |
WireFormatLite::WriteFixed32NoTag(Get<uint32_t>(ptr), output); |
| 197 |
} |
| 198 |
static uint8_t* SerializeToArray(const void* ptr, uint8_t* buffer) { |
| 199 |
return WireFormatLite::WriteFixed32NoTagToArray(Get<Type>(ptr), buffer); |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
template <> |
| 204 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_FIXED64> { |
| 205 |
typedef uint64_t Type; |
| 206 |
static void Serialize(const void* ptr, io::CodedOutputStream* output) { |
| 207 |
WireFormatLite::WriteFixed64NoTag(Get<uint64_t>(ptr), output); |
| 208 |
} |
| 209 |
static uint8_t* SerializeToArray(const void* ptr, uint8_t* buffer) { |
| 210 |
return WireFormatLite::WriteFixed64NoTagToArray(Get<Type>(ptr), buffer); |
| 211 |
} |
| 212 |
}; |
| 213 |
|
| 214 |
template <> |
| 215 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_ENUM> |
| 216 |
: PrimitiveTypeHelper<WireFormatLite::TYPE_INT32> {}; |
| 217 |
|
| 218 |
template <> |
| 219 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_SFIXED32> |
| 220 |
: PrimitiveTypeHelper<WireFormatLite::TYPE_FIXED32> { |
| 221 |
typedef int32_t Type; |
| 222 |
}; |
| 223 |
template <> |
| 224 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_SFIXED64> |
| 225 |
: PrimitiveTypeHelper<WireFormatLite::TYPE_FIXED64> { |
| 226 |
typedef int64_t Type; |
| 227 |
}; |
| 228 |
template <> |
| 229 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_FLOAT> |
| 230 |
: PrimitiveTypeHelper<WireFormatLite::TYPE_FIXED32> { |
| 231 |
typedef float Type; |
| 232 |
}; |
| 233 |
template <> |
| 234 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_DOUBLE> |
| 235 |
: PrimitiveTypeHelper<WireFormatLite::TYPE_FIXED64> { |
| 236 |
typedef double Type; |
| 237 |
}; |
| 238 |
|
| 239 |
template <> |
| 240 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_STRING> { |
| 241 |
typedef std::string Type; |
| 242 |
static void Serialize(const void* ptr, io::CodedOutputStream* output) { |
| 243 |
const Type& value = *static_cast<const Type*>(ptr); |
| 244 |
output->WriteVarint32(value.size()); |
| 245 |
output->WriteRawMaybeAliased(value.data(), value.size()); |
| 246 |
} |
| 247 |
static uint8_t* SerializeToArray(const void* ptr, uint8_t* buffer) { |
| 248 |
const Type& value = *static_cast<const Type*>(ptr); |
| 249 |
return io::CodedOutputStream::WriteStringWithSizeToArray(value, buffer); |
| 250 |
} |
| 251 |
}; |
| 252 |
|
| 253 |
template <> |
| 254 |
struct PrimitiveTypeHelper<WireFormatLite::TYPE_BYTES> |
| 255 |
: PrimitiveTypeHelper<WireFormatLite::TYPE_STRING> {}; |
| 256 |
|
| 257 |
// We want to serialize to both CodedOutputStream and directly into byte arrays |
| 258 |
// without duplicating the code. In fact we might want extra output channels in |
| 259 |
// the future. |
| 260 |
template <typename O, int type> |
| 261 |
struct OutputHelper; |
| 262 |
|
| 263 |
template <int type, typename O> |
| 264 |
void SerializeTo(const void* ptr, O* output) { |
| 265 |
OutputHelper<O, type>::Serialize(ptr, output); |
| 266 |
} |
| 267 |
|
| 268 |
template <typename O> |
| 269 |
void WriteTagTo(uint32_t tag, O* output) { |
| 270 |
SerializeTo<WireFormatLite::TYPE_UINT32>(&tag, output); |
| 271 |
} |
| 272 |
|
| 273 |
template <typename O> |
| 274 |
void WriteLengthTo(uint32_t length, O* output) { |
| 275 |
SerializeTo<WireFormatLite::TYPE_UINT32>(&length, output); |
| 276 |
} |
| 277 |
|
| 278 |
// Specialization for coded output stream |
| 279 |
template <int type> |
| 280 |
struct OutputHelper<io::CodedOutputStream, type> { |
| 281 |
static void Serialize(const void* ptr, io::CodedOutputStream* output) { |
| 282 |
PrimitiveTypeHelper<type>::Serialize(ptr, output); |
| 283 |
} |
| 284 |
}; |
| 285 |
|
| 286 |
// Specialization for writing into a plain array |
| 287 |
struct ArrayOutput { |
| 288 |
uint8_t* ptr; |
| 289 |
bool is_deterministic; |
| 290 |
}; |
| 291 |
|
| 292 |
template <int type> |
| 293 |
struct OutputHelper<ArrayOutput, type> { |
| 294 |
static void Serialize(const void* ptr, ArrayOutput* output) { |
| 295 |
output->ptr = PrimitiveTypeHelper<type>::SerializeToArray(ptr, output->ptr); |
| 296 |
} |
| 297 |
}; |
| 298 |
|
| 299 |
void SerializeMessageNoTable(const MessageLite* msg, |
| 300 |
io::CodedOutputStream* output) { |
| 301 |
msg->SerializeWithCachedSizes(output); |
| 302 |
} |
| 303 |
|
| 304 |
void SerializeMessageNoTable(const MessageLite* msg, ArrayOutput* output) { |
| 305 |
io::ArrayOutputStream array_stream(output->ptr, INT_MAX); |
| 306 |
io::CodedOutputStream o(&array_stream); |
| 307 |
o.SetSerializationDeterministic(output->is_deterministic); |
| 308 |
msg->SerializeWithCachedSizes(&o); |
| 309 |
output->ptr += o.ByteCount(); |
| 310 |
} |
| 311 |
|
| 312 |
// We need to use a helper class to get access to the private members |
| 313 |
class AccessorHelper { |
| 314 |
public: |
| 315 |
static int Size(const RepeatedPtrFieldBase& x) { return x.size(); } |
| 316 |
static void const* Get(const RepeatedPtrFieldBase& x, int idx) { |
| 317 |
return x.raw_data()[idx]; |
| 318 |
} |
| 319 |
}; |
| 320 |
|
| 321 |
void SerializeNotImplemented(int field) { |
| 322 |
ABSL_LOG(FATAL) << "Not implemented field number " << field; |
| 323 |
} |
| 324 |
|
| 325 |
// When switching to c++11 we should make these constexpr functions |
| 326 |
#define SERIALIZE_TABLE_OP(type, type_class) \ |
| 327 |
((type - 1) + static_cast<int>(type_class) * FieldMetadata::kNumTypes) |
| 328 |
|
| 329 |
template <int type> |
| 330 |
bool IsNull(const void* ptr) { |
| 331 |
return *static_cast<const typename PrimitiveTypeHelper<type>::Type*>(ptr) == |
| 332 |
0; |
| 333 |
} |
| 334 |
|
| 335 |
template <> |
| 336 |
bool IsNull<WireFormatLite::TYPE_STRING>(const void* ptr) { |
| 337 |
return static_cast<const ArenaStringPtr*>(ptr)->Get().size() == 0; |
| 338 |
} |
| 339 |
|
| 340 |
template <> |
| 341 |
bool IsNull<WireFormatLite::TYPE_BYTES>(const void* ptr) { |
| 342 |
return static_cast<const ArenaStringPtr*>(ptr)->Get().size() == 0; |
| 343 |
} |
| 344 |
|
| 345 |
template <> |
| 346 |
bool IsNull<WireFormatLite::TYPE_GROUP>(const void* ptr) { |
| 347 |
return Get<const MessageLite*>(ptr) == nullptr; |
| 348 |
} |
| 349 |
|
| 350 |
template <> |
| 351 |
bool IsNull<WireFormatLite::TYPE_MESSAGE>(const void* ptr) { |
| 352 |
return Get<const MessageLite*>(ptr) == nullptr; |
| 353 |
} |
| 354 |
|
| 355 |
void ExtensionSerializer(const MessageLite* extendee, const uint8_t* ptr, |
| 356 |
uint32_t offset, uint32_t tag, uint32_t has_offset, |
| 357 |
io::CodedOutputStream* output) { |
| 358 |
reinterpret_cast<const ExtensionSet*>(ptr + offset) |
| 359 |
->SerializeWithCachedSizes(extendee, tag, has_offset, output); |
| 360 |
} |
| 361 |
|
| 362 |
void UnknownFieldSerializerLite(const uint8_t* ptr, uint32_t offset, |
| 363 |
uint32_t /*tag*/, uint32_t /*has_offset*/, |
| 364 |
io::CodedOutputStream* output) { |
| 365 |
output->WriteString( |
| 366 |
reinterpret_cast<const InternalMetadata*>(ptr + offset) |
| 367 |
->unknown_fields<std::string>(&internal::GetEmptyString)); |
| 368 |
} |
| 369 |
|
| 370 |
MessageLite* DuplicateIfNonNullInternal(MessageLite* message) { |
| 371 |
if (message) { |
| 372 |
MessageLite* ret = message->New(); |
| 373 |
ret->CheckTypeAndMergeFrom(*message); |
| 374 |
return ret; |
| 375 |
} else { |
| 376 |
return nullptr; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
void GenericSwap(MessageLite* m1, MessageLite* m2) { |
| 381 |
std::unique_ptr<MessageLite> tmp(m1->New()); |
| 382 |
tmp->CheckTypeAndMergeFrom(*m1); |
| 383 |
m1->Clear(); |
| 384 |
m1->CheckTypeAndMergeFrom(*m2); |
| 385 |
m2->Clear(); |
| 386 |
m2->CheckTypeAndMergeFrom(*tmp); |
| 387 |
} |
| 388 |
|
| 389 |
// Returns a message owned by this Arena. This may require Own()ing or |
| 390 |
// duplicating the message. |
| 391 |
MessageLite* GetOwnedMessageInternal(Arena* message_arena, |
| 392 |
MessageLite* submessage, |
| 393 |
Arena* submessage_arena) { |
| 394 |
ABSL_DCHECK(Arena::InternalGetOwningArena(submessage) == submessage_arena); |
| 395 |
ABSL_DCHECK(message_arena != submessage_arena); |
| 396 |
ABSL_DCHECK_EQ(submessage_arena, nullptr); |
| 397 |
if (message_arena != nullptr && submessage_arena == nullptr) { |
| 398 |
message_arena->Own(submessage); |
| 399 |
return submessage; |
| 400 |
} else { |
| 401 |
MessageLite* ret = submessage->New(message_arena); |
| 402 |
ret->CheckTypeAndMergeFrom(*submessage); |
| 403 |
return ret; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
} // namespace internal |
| 408 |
} // namespace protobuf |
| 409 |
} // namespace google |
| 410 |
|
| 411 |
#include "google/protobuf/port_undef.inc" |