File indexing completed on 2025-01-31 10:12:28
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_H__
0017 #define GOOGLE_PROTOBUF_WIRE_FORMAT_H__
0018
0019 #include "google/protobuf/stubs/common.h"
0020 #include "absl/base/casts.h"
0021 #include "google/protobuf/descriptor.h"
0022 #include "google/protobuf/generated_message_util.h"
0023 #include "google/protobuf/io/coded_stream.h"
0024 #include "google/protobuf/message.h"
0025 #include "google/protobuf/metadata_lite.h"
0026 #include "google/protobuf/parse_context.h"
0027 #include "google/protobuf/port.h"
0028 #include "google/protobuf/wire_format_lite.h"
0029
0030 #ifdef SWIG
0031 #error "You cannot SWIG proto headers"
0032 #endif
0033
0034
0035 #include "google/protobuf/port_def.inc"
0036
0037 namespace google {
0038 namespace protobuf {
0039 class MapKey;
0040 class UnknownFieldSet;
0041 }
0042 }
0043
0044 namespace google {
0045 namespace protobuf {
0046 namespace internal {
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057 class PROTOBUF_EXPORT WireFormat {
0058 public:
0059 WireFormat() = delete;
0060
0061
0062 static inline WireFormatLite::WireType WireTypeForField(
0063 const FieldDescriptor* field);
0064
0065
0066 static inline WireFormatLite::WireType WireTypeForFieldType(
0067 FieldDescriptor::Type type);
0068
0069
0070
0071 static inline size_t TagSize(int field_number, FieldDescriptor::Type type);
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089 static bool ParseAndMergePartial(io::CodedInputStream* input,
0090 Message* message);
0091
0092
0093
0094 static const char* _InternalParse(Message* msg, const char* ptr,
0095 internal::ParseContext* ctx);
0096
0097
0098
0099
0100
0101
0102
0103
0104 static void SerializeWithCachedSizes(const Message& message, int size,
0105 io::CodedOutputStream* output) {
0106 int expected_endpoint = output->ByteCount() + size;
0107 output->SetCur(
0108 _InternalSerialize(message, output->Cur(), output->EpsCopy()));
0109 ABSL_CHECK_EQ(output->ByteCount(), expected_endpoint)
0110 << ": Protocol message serialized to a size different from what was "
0111 "originally expected. Perhaps it was modified by another thread "
0112 "during serialization?";
0113 }
0114 static uint8_t* _InternalSerialize(const Message& message, uint8_t* target,
0115 io::EpsCopyOutputStream* stream);
0116
0117
0118
0119
0120
0121
0122 static size_t ByteSize(const Message& message);
0123
0124
0125
0126
0127
0128
0129
0130 static bool SkipField(io::CodedInputStream* input, uint32_t tag,
0131 UnknownFieldSet* unknown_fields);
0132
0133
0134
0135 static bool SkipMessage(io::CodedInputStream* input,
0136 UnknownFieldSet* unknown_fields);
0137
0138
0139
0140
0141 static bool ReadPackedEnumPreserveUnknowns(io::CodedInputStream* input,
0142 uint32_t field_number,
0143 bool (*is_valid)(int),
0144 UnknownFieldSet* unknown_fields,
0145 RepeatedField<int>* values);
0146
0147
0148 static void SerializeUnknownFields(const UnknownFieldSet& unknown_fields,
0149 io::CodedOutputStream* output) {
0150 output->SetCur(InternalSerializeUnknownFieldsToArray(
0151 unknown_fields, output->Cur(), output->EpsCopy()));
0152 }
0153
0154
0155
0156
0157
0158 static uint8_t* SerializeUnknownFieldsToArray(
0159 const UnknownFieldSet& unknown_fields, uint8_t* target) {
0160 io::EpsCopyOutputStream stream(
0161 target, static_cast<int>(ComputeUnknownFieldsSize(unknown_fields)),
0162 io::CodedOutputStream::IsDefaultSerializationDeterministic());
0163 return InternalSerializeUnknownFieldsToArray(unknown_fields, target,
0164 &stream);
0165 }
0166 static uint8_t* InternalSerializeUnknownFieldsToArray(
0167 const UnknownFieldSet& unknown_fields, uint8_t* target,
0168 io::EpsCopyOutputStream* stream);
0169
0170
0171
0172 static void SerializeUnknownMessageSetItems(
0173 const UnknownFieldSet& unknown_fields, io::CodedOutputStream* output) {
0174 output->SetCur(InternalSerializeUnknownMessageSetItemsToArray(
0175 unknown_fields, output->Cur(), output->EpsCopy()));
0176 }
0177
0178
0179
0180
0181
0182 static uint8_t* SerializeUnknownMessageSetItemsToArray(
0183 const UnknownFieldSet& unknown_fields, uint8_t* target);
0184 static uint8_t* InternalSerializeUnknownMessageSetItemsToArray(
0185 const UnknownFieldSet& unknown_fields, uint8_t* target,
0186 io::EpsCopyOutputStream* stream);
0187
0188
0189 static size_t ComputeUnknownFieldsSize(const UnknownFieldSet& unknown_fields);
0190
0191
0192
0193 static size_t ComputeUnknownMessageSetItemsSize(
0194 const UnknownFieldSet& unknown_fields);
0195
0196
0197
0198
0199
0200
0201 static uint32_t MakeTag(const FieldDescriptor* field);
0202
0203
0204
0205 static bool ParseAndMergeField(
0206 uint32_t tag,
0207 const FieldDescriptor* field,
0208 Message* message, io::CodedInputStream* input);
0209
0210
0211 static void SerializeFieldWithCachedSizes(
0212 const FieldDescriptor* field,
0213 const Message& message, io::CodedOutputStream* output) {
0214 output->SetCur(InternalSerializeField(field, message, output->Cur(),
0215 output->EpsCopy()));
0216 }
0217 static uint8_t* InternalSerializeField(
0218 const FieldDescriptor* field,
0219 const Message& message, uint8_t* target, io::EpsCopyOutputStream* stream);
0220
0221
0222
0223
0224 static size_t FieldByteSize(const FieldDescriptor* field,
0225 const Message& message);
0226
0227
0228
0229 static bool ParseAndMergeMessageSetItem(io::CodedInputStream* input,
0230 Message* message);
0231 static void SerializeMessageSetItemWithCachedSizes(
0232 const FieldDescriptor* field, const Message& message,
0233 io::CodedOutputStream* output) {
0234 output->SetCur(InternalSerializeMessageSetItem(
0235 field, message, output->Cur(), output->EpsCopy()));
0236 }
0237 static uint8_t* InternalSerializeMessageSetItem(
0238 const FieldDescriptor* field, const Message& message, uint8_t* target,
0239 io::EpsCopyOutputStream* stream);
0240 static size_t MessageSetItemByteSize(const FieldDescriptor* field,
0241 const Message& message);
0242
0243
0244
0245
0246
0247 static size_t FieldDataOnlyByteSize(
0248 const FieldDescriptor* field,
0249 const Message& message);
0250
0251 enum Operation {
0252 PARSE = 0,
0253 SERIALIZE = 1,
0254 };
0255
0256
0257
0258
0259 static void VerifyUTF8String(const char* data, int size, Operation op);
0260
0261
0262 static void VerifyUTF8StringNamedField(const char* data, int size,
0263 Operation op, const char* field_name);
0264
0265 private:
0266 struct MessageSetParser;
0267 friend class TcParser;
0268
0269 static bool SkipMessageSetField(io::CodedInputStream* input,
0270 uint32_t field_number,
0271 UnknownFieldSet* unknown_fields);
0272
0273
0274 static bool ParseAndMergeMessageSetField(uint32_t field_number,
0275 const FieldDescriptor* field,
0276 Message* message,
0277 io::CodedInputStream* input);
0278
0279 static const char* _InternalParseAndMergeField(Message* msg, const char* ptr,
0280 internal::ParseContext* ctx,
0281 uint64_t tag,
0282 const Reflection* reflection,
0283 const FieldDescriptor* field);
0284 };
0285
0286
0287 class PROTOBUF_EXPORT UnknownFieldSetFieldSkipper : public FieldSkipper {
0288 public:
0289 UnknownFieldSetFieldSkipper(UnknownFieldSet* unknown_fields)
0290 : unknown_fields_(unknown_fields) {}
0291 ~UnknownFieldSetFieldSkipper() override {}
0292
0293
0294 bool SkipField(io::CodedInputStream* input, uint32_t tag) override;
0295 bool SkipMessage(io::CodedInputStream* input) override;
0296 void SkipUnknownEnum(int field_number, int value) override;
0297
0298 protected:
0299 UnknownFieldSet* unknown_fields_;
0300 };
0301
0302
0303
0304 inline WireFormatLite::WireType WireFormat::WireTypeForField(
0305 const FieldDescriptor* field) {
0306 if (field->is_packed()) {
0307 return WireFormatLite::WIRETYPE_LENGTH_DELIMITED;
0308 } else {
0309 return WireTypeForFieldType(field->type());
0310 }
0311 }
0312
0313 inline WireFormatLite::WireType WireFormat::WireTypeForFieldType(
0314 FieldDescriptor::Type type) {
0315
0316
0317 return WireFormatLite::WireTypeForFieldType(
0318 static_cast<WireFormatLite::FieldType>(absl::implicit_cast<int>(type)));
0319 }
0320
0321 inline uint32_t WireFormat::MakeTag(const FieldDescriptor* field) {
0322 return WireFormatLite::MakeTag(field->number(), WireTypeForField(field));
0323 }
0324
0325 inline size_t WireFormat::TagSize(int field_number,
0326 FieldDescriptor::Type type) {
0327
0328
0329 return WireFormatLite::TagSize(
0330 field_number,
0331 static_cast<WireFormatLite::FieldType>(absl::implicit_cast<int>(type)));
0332 }
0333
0334 inline void WireFormat::VerifyUTF8String(const char* data, int size,
0335 WireFormat::Operation op) {
0336 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
0337 WireFormatLite::VerifyUtf8String(data, size,
0338 static_cast<WireFormatLite::Operation>(op),
0339 "");
0340 #else
0341
0342 (void)data;
0343 (void)size;
0344 (void)op;
0345 #endif
0346 }
0347
0348 inline void WireFormat::VerifyUTF8StringNamedField(const char* data, int size,
0349 WireFormat::Operation op,
0350 const char* field_name) {
0351 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
0352 WireFormatLite::VerifyUtf8String(
0353 data, size, static_cast<WireFormatLite::Operation>(op), field_name);
0354 #else
0355
0356 (void)data;
0357 (void)size;
0358 (void)op;
0359 (void)field_name;
0360 #endif
0361 }
0362
0363
0364 inline uint8_t* InternalSerializeUnknownMessageSetItemsToArray(
0365 const UnknownFieldSet& unknown_fields, uint8_t* target,
0366 io::EpsCopyOutputStream* stream) {
0367 return WireFormat::InternalSerializeUnknownMessageSetItemsToArray(
0368 unknown_fields, target, stream);
0369 }
0370
0371 inline size_t ComputeUnknownMessageSetItemsSize(
0372 const UnknownFieldSet& unknown_fields) {
0373 return WireFormat::ComputeUnknownMessageSetItemsSize(unknown_fields);
0374 }
0375
0376
0377 PROTOBUF_EXPORT
0378 size_t ComputeUnknownFieldsSize(const InternalMetadata& metadata, size_t size,
0379 CachedSize* cached_size);
0380
0381 size_t MapKeyDataOnlyByteSize(const FieldDescriptor* field,
0382 const MapKey& value);
0383
0384 uint8_t* SerializeMapKeyWithCachedSizes(const FieldDescriptor* field,
0385 const MapKey& value, uint8_t* target,
0386 io::EpsCopyOutputStream* stream);
0387 }
0388 }
0389 }
0390
0391 #include "google/protobuf/port_undef.inc"
0392
0393 #endif