protobuf/src/google/protobuf/extension_set_inl.h
Go to the documentation of this file.
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 #ifndef GOOGLE_PROTOBUF_EXTENSION_SET_INL_H__
32 #define GOOGLE_PROTOBUF_EXTENSION_SET_INL_H__
33 
34 #include <google/protobuf/parse_context.h>
35 #include <google/protobuf/extension_set.h>
36 #include <google/protobuf/metadata_lite.h>
37 
38 namespace google {
39 namespace protobuf {
40 namespace internal {
41 
42 template <typename T>
44  int number, bool was_packed_on_wire, const ExtensionInfo& extension,
46  if (was_packed_on_wire) {
47  switch (extension.type) {
48 #define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE) \
49  case WireFormatLite::TYPE_##UPPERCASE: \
50  return internal::Packed##CPP_CAMELCASE##Parser( \
51  MutableRawRepeatedField(number, extension.type, extension.is_packed, \
52  extension.descriptor), \
53  ptr, ctx);
54  HANDLE_TYPE(INT32, Int32);
55  HANDLE_TYPE(INT64, Int64);
56  HANDLE_TYPE(UINT32, UInt32);
57  HANDLE_TYPE(UINT64, UInt64);
58  HANDLE_TYPE(SINT32, SInt32);
59  HANDLE_TYPE(SINT64, SInt64);
60  HANDLE_TYPE(FIXED32, Fixed32);
61  HANDLE_TYPE(FIXED64, Fixed64);
62  HANDLE_TYPE(SFIXED32, SFixed32);
63  HANDLE_TYPE(SFIXED64, SFixed64);
64  HANDLE_TYPE(FLOAT, Float);
65  HANDLE_TYPE(DOUBLE, Double);
67 #undef HANDLE_TYPE
68 
70  return internal::PackedEnumParserArg<T>(
72  extension.descriptor),
73  ptr, ctx, extension.enum_validity_check.func,
74  extension.enum_validity_check.arg, metadata, number);
79  GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
80  break;
81  }
82  } else {
83  switch (extension.type) {
84 #define HANDLE_VARINT_TYPE(UPPERCASE, CPP_CAMELCASE) \
85  case WireFormatLite::TYPE_##UPPERCASE: { \
86  uint64_t value; \
87  ptr = VarintParse(ptr, &value); \
88  GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); \
89  if (extension.is_repeated) { \
90  Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \
91  extension.is_packed, value, extension.descriptor); \
92  } else { \
93  Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \
94  extension.descriptor); \
95  } \
96  } break
97 
98  HANDLE_VARINT_TYPE(INT32, Int32);
99  HANDLE_VARINT_TYPE(INT64, Int64);
100  HANDLE_VARINT_TYPE(UINT32, UInt32);
101  HANDLE_VARINT_TYPE(UINT64, UInt64);
103 #undef HANDLE_VARINT_TYPE
104 #define HANDLE_SVARINT_TYPE(UPPERCASE, CPP_CAMELCASE, SIZE) \
105  case WireFormatLite::TYPE_##UPPERCASE: { \
106  uint64_t val; \
107  ptr = VarintParse(ptr, &val); \
108  GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); \
109  auto value = WireFormatLite::ZigZagDecode##SIZE(val); \
110  if (extension.is_repeated) { \
111  Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \
112  extension.is_packed, value, extension.descriptor); \
113  } else { \
114  Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \
115  extension.descriptor); \
116  } \
117  } break
118 
119  HANDLE_SVARINT_TYPE(SINT32, Int32, 32);
120  HANDLE_SVARINT_TYPE(SINT64, Int64, 64);
121 #undef HANDLE_SVARINT_TYPE
122 #define HANDLE_FIXED_TYPE(UPPERCASE, CPP_CAMELCASE, CPPTYPE) \
123  case WireFormatLite::TYPE_##UPPERCASE: { \
124  auto value = UnalignedLoad<CPPTYPE>(ptr); \
125  ptr += sizeof(CPPTYPE); \
126  if (extension.is_repeated) { \
127  Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \
128  extension.is_packed, value, extension.descriptor); \
129  } else { \
130  Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \
131  extension.descriptor); \
132  } \
133  } break
134 
135  HANDLE_FIXED_TYPE(FIXED32, UInt32, uint32_t);
136  HANDLE_FIXED_TYPE(FIXED64, UInt64, uint64_t);
137  HANDLE_FIXED_TYPE(SFIXED32, Int32, int32_t);
138  HANDLE_FIXED_TYPE(SFIXED64, Int64, int64_t);
139  HANDLE_FIXED_TYPE(FLOAT, Float, float);
140  HANDLE_FIXED_TYPE(DOUBLE, Double, double);
141 #undef HANDLE_FIXED_TYPE
142 
144  uint64_t val;
145  ptr = VarintParse(ptr, &val);
147  int value = val;
148 
149  if (!extension.enum_validity_check.func(
150  extension.enum_validity_check.arg, value)) {
151  WriteVarint(number, val, metadata->mutable_unknown_fields<T>());
152  } else if (extension.is_repeated) {
154  extension.descriptor);
155  } else {
157  extension.descriptor);
158  }
159  break;
160  }
161 
164  std::string* value =
165  extension.is_repeated
167  extension.descriptor)
169  extension.descriptor);
170  int size = ReadSize(&ptr);
172  return ctx->ReadString(ptr, size, value);
173  }
174 
176  MessageLite* value =
177  extension.is_repeated
179  *extension.message_info.prototype,
180  extension.descriptor)
182  *extension.message_info.prototype,
183  extension.descriptor);
185  return ctx->ParseGroup(value, ptr, tag);
186  }
187 
189  MessageLite* value =
190  extension.is_repeated
192  *extension.message_info.prototype,
193  extension.descriptor)
195  *extension.message_info.prototype,
196  extension.descriptor);
197  return ctx->ParseMessage(value, ptr);
198  }
199  }
200  }
201  return ptr;
202 }
203 
204 template <typename Msg, typename T>
206  const char* ptr, const Msg* extendee, internal::InternalMetadata* metadata,
209  uint32_t type_id;
210  enum class State { kNoTag, kHasType, kHasPayload, kDone };
211  State state = State::kNoTag;
212 
213  while (!ctx->Done(&ptr)) {
214  uint32_t tag = static_cast<uint8_t>(*ptr++);
216  uint64_t tmp;
217  ptr = ParseBigVarint(ptr, &tmp);
219  if (state == State::kNoTag) {
220  type_id = tmp;
221  state = State::kHasType;
222  } else if (state == State::kHasPayload) {
223  type_id = tmp;
225  bool was_packed_on_wire;
226  if (!FindExtension(2, type_id, extendee, ctx, &extension,
227  &was_packed_on_wire)) {
228  WriteLengthDelimited(type_id, payload,
229  metadata->mutable_unknown_fields<T>());
230  } else {
231  MessageLite* value =
232  extension.is_repeated
234  *extension.message_info.prototype,
235  extension.descriptor)
237  *extension.message_info.prototype,
238  extension.descriptor);
239 
240  const char* p;
241  // We can't use regular parse from string as we have to track
242  // proper recursion depth and descriptor pools.
243  ParseContext tmp_ctx(ctx->depth(), false, &p, payload);
244  tmp_ctx.data().pool = ctx->data().pool;
245  tmp_ctx.data().factory = ctx->data().factory;
246  GOOGLE_PROTOBUF_PARSER_ASSERT(value->_InternalParse(p, &tmp_ctx) &&
247  tmp_ctx.EndedAtLimit());
248  }
249  state = State::kDone;
250  }
252  if (state == State::kHasType) {
253  ptr = ParseFieldMaybeLazily(static_cast<uint64_t>(type_id) * 8 + 2, ptr,
254  extendee, metadata, ctx);
256  state = State::kDone;
257  } else {
259  int32_t size = ReadSize(&ptr);
261  ptr = ctx->ReadString(ptr, size, &tmp);
263  if (state == State::kNoTag) {
264  payload = std::move(tmp);
265  state = State::kHasPayload;
266  }
267  }
268  } else {
269  ptr = ReadTag(ptr - 1, &tag);
270  if (tag == 0 || (tag & 7) == 4) {
271  ctx->SetLastTag(tag);
272  return ptr;
273  }
274  ptr = ParseField(tag, ptr, extendee, metadata, ctx);
276  }
277  }
278  return ptr;
279 }
280 
281 } // namespace internal
282 } // namespace protobuf
283 } // namespace google
284 
285 #endif // GOOGLE_PROTOBUF_EXTENSION_SET_INL_H__
google::protobuf.internal::WireFormatLite::TYPE_MESSAGE
@ TYPE_MESSAGE
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:122
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
google::protobuf.internal::ReadTag
const char * ReadTag(const char *p, uint32 *out, uint32 max_tag=0)
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:494
google::protobuf::value
const Descriptor::ReservedRange value
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1954
google::protobuf::EnumValueDescriptor::type
const EnumDescriptor * type() const
google::protobuf.internal::WireFormatLite::TYPE_ENUM
@ TYPE_ENUM
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:125
google::protobuf.internal::ReadSize
uint32 ReadSize(const char **pp)
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:561
ctx
Definition: benchmark-async.c:30
testing::internal::Int32
TypeWithSize< 4 >::Int Int32
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:2159
metadata
Definition: cq_verifier.cc:48
google::protobuf::extension
const Descriptor::ReservedRange const EnumValueDescriptor const MethodDescriptor extension
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:2001
Bool
Definition: bloaty/third_party/googletest/googletest/test/gtest_pred_impl_unittest.cc:56
google::protobuf.internal::WireFormatLite::TYPE_BYTES
@ TYPE_BYTES
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:123
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf.internal::ParseContext
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:336
google::protobuf.internal::EpsCopyInputStream::EndedAtLimit
bool EndedAtLimit() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:188
testing::internal::UInt64
TypeWithSize< 8 >::UInt UInt64
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:2162
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf.internal::ParseContext::data
Data & data()
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:356
google::protobuf::MessageLite
Definition: bloaty/third_party/protobuf/src/google/protobuf/message_lite.h:184
testing::internal::UInt32
TypeWithSize< 4 >::UInt UInt32
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:2160
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
google::protobuf::HANDLE_TYPE
HANDLE_TYPE(int32, FieldDescriptor::CPPTYPE_INT32, -1)
google::protobuf.internal::WireFormatLite::TYPE_STRING
@ TYPE_STRING
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:120
T
#define T(upbtypeconst, upbtype, ctype, default_value)
BOOL
int BOOL
Definition: undname.c:46
google::protobuf.internal::ExtensionSet::MutableRawRepeatedField
void * MutableRawRepeatedField(int number, FieldType field_type, bool packed, const FieldDescriptor *desc)
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set.cc:377
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
google::protobuf.internal::ParseBigVarint
const char * ParseBigVarint(const char *p, uint64 *out)
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:536
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
google::protobuf.internal::WireFormatLite::TYPE_GROUP
@ TYPE_GROUP
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:121
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
tag
static void * tag(intptr_t t)
Definition: bad_client.cc:318
google::protobuf.internal::ExtensionSet::MutableMessage
MessageLite * MutableMessage(int number, FieldType type, const MessageLite &prototype, desc)
google::protobuf.internal::ExtensionSet::ParseMessageSetItemTmpl
const char * ParseMessageSetItemTmpl(const char *ptr, const Msg *containing_type, Metadata *metadata, internal::ParseContext *ctx)
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set_inl.h:204
GOOGLE_PROTOBUF_PARSER_ASSERT
#define GOOGLE_PROTOBUF_PARSER_ASSERT(predicate)
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:660
google::protobuf.internal::VarintParse
const PROTOBUF_MUST_USE_RESULT char * VarintParse(const char *p, T *out)
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:471
testing::internal::Float
FloatingPoint< float > Float
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-internal.h:396
google::protobuf.internal::WriteLengthDelimited
void WriteLengthDelimited(uint32 num, StringPiece val, std::string *s)
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.cc:334
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
google::protobuf.internal::ExtensionInfo
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set.h:101
HANDLE_VARINT_TYPE
#define HANDLE_VARINT_TYPE(UPPERCASE, CPP_CAMELCASE)
google::protobuf.internal::InternalMetadata
Definition: protobuf/src/google/protobuf/metadata_lite.h:62
number
int32_t number
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:850
HANDLE_SVARINT_TYPE
#define HANDLE_SVARINT_TYPE(UPPERCASE, CPP_CAMELCASE, SIZE)
google::protobuf.internal::ParseContext::Data::factory
MessageFactory * factory
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:340
google::protobuf.internal::ExtensionSet::FindExtension
bool FindExtension(int wire_type, uint32 field, const MessageLite *containing_type, const internal::ParseContext *ctx, ExtensionInfo *extension, bool *was_packed_on_wire)
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set.h:761
Fixed64
TypeAndValue Fixed64(uint64_t val)
Definition: upb/upb/util/compare_test.cc:83
FATAL
#define FATAL(msg)
Definition: task.h:88
google::protobuf.internal::WireFormatLite::WIRETYPE_START_GROUP
@ WIRETYPE_START_GROUP
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:105
google::protobuf.internal::ExtensionSet::ParseFieldWithExtensionInfo
bool ParseFieldWithExtensionInfo(int field_number, bool was_packed_on_wire, const ExtensionInfo &extension, io::CodedInputStream *input, FieldSkipper *field_skipper)
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set.cc:1222
testing::internal::Double
FloatingPoint< double > Double
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-internal.h:397
google::protobuf.internal::ExtensionSet::ParseFieldMaybeLazily
bool ParseFieldMaybeLazily(int wire_type, int field_number, io::CodedInputStream *input, ExtensionFinder *extension_finder, MessageSetFieldSkipper *field_skipper)
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set_heavy.cc:480
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
google::protobuf.internal::WireFormatLite::kMessageSetMessageTag
static const int kMessageSetMessageTag
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:221
google::protobuf.internal::ExtensionSet::MutableString
std::string * MutableString(int number, FieldType type, desc)
internal
Definition: benchmark/test/output_test_helper.cc:20
testing::internal::Int64
TypeWithSize< 8 >::Int Int64
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:2161
HANDLE_FIXED_TYPE
#define HANDLE_FIXED_TYPE(UPPERCASE, CPP_CAMELCASE, CPPTYPE)
google::protobuf.internal::WriteVarint
void WriteVarint(uint64 val, std::string *s)
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.cc:320
google::protobuf.internal::ExtensionSet::AddMessage
MessageLite * AddMessage(int number, FieldType type, const MessageLite &prototype, desc)
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
Fixed32
TypeAndValue Fixed32(uint32_t val)
Definition: upb/upb/util/compare_test.cc:89
google::protobuf.internal::ExtensionSet::AddEnum
void AddEnum(int number, FieldType type, bool packed, int value, desc)
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
google::protobuf.internal::ParseContext::Data::pool
const DescriptorPool * pool
Definition: bloaty/third_party/protobuf/src/google/protobuf/parse_context.h:339
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:146
gen_server_registered_method_bad_client_test_body.payload
list payload
Definition: gen_server_registered_method_bad_client_test_body.py:40
google::protobuf.internal::ExtensionSet::AddString
void AddString(int number, FieldType type, std::string value, desc)
google::protobuf.internal::ExtensionSet::SetEnum
void SetEnum(int number, FieldType type, int value, desc)
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf.internal::ExtensionSet::ParseField
bool ParseField(uint32 tag, io::CodedInputStream *input, ExtensionFinder *extension_finder, FieldSkipper *field_skipper)
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set.cc:1184
google::protobuf.internal::WireFormatLite::kMessageSetTypeIdTag
static const int kMessageSetTypeIdTag
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:219


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:21