Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:28

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // Author: kenton@google.com (Kenton Varda)
0009 //         atenasio@google.com (Chris Atenasio) (ZigZag transform)
0010 //  Based on original Protocol Buffers design by
0011 //  Sanjay Ghemawat, Jeff Dean, and others.
0012 //
0013 // This header is logically internal, but is made public because it is used
0014 // from protocol-compiler-generated code, which may reside in other components.
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 // Must be included last.
0035 #include "google/protobuf/port_def.inc"
0036 
0037 namespace google {
0038 namespace protobuf {
0039 class MapKey;           // map_field.h
0040 class UnknownFieldSet;  // unknown_field_set.h
0041 }  // namespace protobuf
0042 }  // namespace google
0043 
0044 namespace google {
0045 namespace protobuf {
0046 namespace internal {
0047 
0048 // This class is for internal use by the protocol buffer library and by
0049 // protocol-compiler-generated message classes.  It must not be called
0050 // directly by clients.
0051 //
0052 // This class contains code for implementing the binary protocol buffer
0053 // wire format via reflection.  The WireFormatLite class implements the
0054 // non-reflection based routines.
0055 //
0056 // This class is really a namespace that contains only static methods
0057 class PROTOBUF_EXPORT WireFormat {
0058  public:
0059   WireFormat() = delete;
0060 
0061   // Given a field return its WireType
0062   static inline WireFormatLite::WireType WireTypeForField(
0063       const FieldDescriptor* field);
0064 
0065   // Given a FieldDescriptor::Type return its WireType
0066   static inline WireFormatLite::WireType WireTypeForFieldType(
0067       FieldDescriptor::Type type);
0068 
0069   // Compute the byte size of a tag.  For groups, this includes both the start
0070   // and end tags.
0071   static inline size_t TagSize(int field_number, FieldDescriptor::Type type);
0072 
0073   // These procedures can be used to implement the methods of Message which
0074   // handle parsing and serialization of the protocol buffer wire format
0075   // using only the Reflection interface.  When you ask the protocol
0076   // compiler to optimize for code size rather than speed, it will implement
0077   // those methods in terms of these procedures.  Of course, these are much
0078   // slower than the specialized implementations which the protocol compiler
0079   // generates when told to optimize for speed.
0080 
0081   // Read a message in protocol buffer wire format.
0082   //
0083   // This procedure reads either to the end of the input stream or through
0084   // a WIRETYPE_END_GROUP tag ending the message, whichever comes first.
0085   // It returns false if the input is invalid.
0086   //
0087   // Required fields are NOT checked by this method.  You must call
0088   // IsInitialized() on the resulting message yourself.
0089   static bool ParseAndMergePartial(io::CodedInputStream* input,
0090                                    Message* message);
0091 
0092   // This is meant for internal protobuf use (WireFormat is an internal class).
0093   // This is the reflective implementation of the _InternalParse functionality.
0094   static const char* _InternalParse(Message* msg, const char* ptr,
0095                                     internal::ParseContext* ctx);
0096 
0097   // Serialize a message in protocol buffer wire format.
0098   //
0099   // Any embedded messages within the message must have their correct sizes
0100   // cached.  However, the top-level message need not; its size is passed as
0101   // a parameter to this procedure.
0102   //
0103   // These return false iff the underlying stream returns a write error.
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   // Implements Message::ByteSize() via reflection.  WARNING:  The result
0118   // of this method is *not* cached anywhere.  However, all embedded messages
0119   // will have their ByteSize() methods called, so their sizes will be cached.
0120   // Therefore, calling this method is sufficient to allow you to call
0121   // WireFormat::SerializeWithCachedSizes() on the same object.
0122   static size_t ByteSize(const Message& message);
0123 
0124   // -----------------------------------------------------------------
0125   // Helpers for dealing with unknown fields
0126 
0127   // Skips a field value of the given WireType.  The input should start
0128   // positioned immediately after the tag.  If unknown_fields is non-nullptr,
0129   // the contents of the field will be added to it.
0130   static bool SkipField(io::CodedInputStream* input, uint32_t tag,
0131                         UnknownFieldSet* unknown_fields);
0132 
0133   // Reads and ignores a message from the input.  If unknown_fields is
0134   // non-nullptr, the contents will be added to it.
0135   static bool SkipMessage(io::CodedInputStream* input,
0136                           UnknownFieldSet* unknown_fields);
0137 
0138   // Read a packed enum field. If the is_valid function is not nullptr, values
0139   // for which is_valid(value) returns false are appended to
0140   // unknown_fields_stream.
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   // Write the contents of an UnknownFieldSet to the output.
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   // Same as above, except writing directly to the provided buffer.
0154   // Requires that the buffer have sufficient capacity for
0155   // ComputeUnknownFieldsSize(unknown_fields).
0156   //
0157   // Returns a pointer past the last written byte.
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   // Same thing except for messages that have the message_set_wire_format
0171   // option.
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   // Same as above, except writing directly to the provided buffer.
0178   // Requires that the buffer have sufficient capacity for
0179   // ComputeUnknownMessageSetItemsSize(unknown_fields).
0180   //
0181   // Returns a pointer past the last written byte.
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   // Compute the size of the UnknownFieldSet on the wire.
0189   static size_t ComputeUnknownFieldsSize(const UnknownFieldSet& unknown_fields);
0190 
0191   // Same thing except for messages that have the message_set_wire_format
0192   // option.
0193   static size_t ComputeUnknownMessageSetItemsSize(
0194       const UnknownFieldSet& unknown_fields);
0195 
0196   // Helper functions for encoding and decoding tags.  (Inlined below and in
0197   // _inl.h)
0198   //
0199   // This is different from MakeTag(field->number(), field->type()) in the
0200   // case of packed repeated fields.
0201   static uint32_t MakeTag(const FieldDescriptor* field);
0202 
0203   // Parse a single field.  The input should start out positioned immediately
0204   // after the tag.
0205   static bool ParseAndMergeField(
0206       uint32_t tag,
0207       const FieldDescriptor* field,  // May be nullptr for unknown
0208       Message* message, io::CodedInputStream* input);
0209 
0210   // Serialize a single field.
0211   static void SerializeFieldWithCachedSizes(
0212       const FieldDescriptor* field,  // Cannot be nullptr
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,  // Cannot be nullptr
0219       const Message& message, uint8_t* target, io::EpsCopyOutputStream* stream);
0220 
0221   // Compute size of a single field.  If the field is a message type, this
0222   // will call ByteSize() for the embedded message, insuring that it caches
0223   // its size.
0224   static size_t FieldByteSize(const FieldDescriptor* field,  // Can't be nullptr
0225                               const Message& message);
0226 
0227   // Parse/serialize a MessageSet::Item group.  Used with messages that use
0228   // option message_set_wire_format = true.
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   // Computes the byte size of a field, excluding tags. For packed fields, it
0244   // only includes the size of the raw data, and not the size of the total
0245   // length, but for other length-prefixed types, the size of the length is
0246   // included.
0247   static size_t FieldDataOnlyByteSize(
0248       const FieldDescriptor* field,  // Cannot be nullptr
0249       const Message& message);
0250 
0251   enum Operation {
0252     PARSE = 0,
0253     SERIALIZE = 1,
0254   };
0255 
0256   // Verifies that a string field is valid UTF8, logging an error if not.
0257   // This function will not be called by newly generated protobuf code
0258   // but remains present to support existing code.
0259   static void VerifyUTF8String(const char* data, int size, Operation op);
0260   // The NamedField variant takes a field name in order to produce an
0261   // informative error message if verification fails.
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   // Skip a MessageSet field.
0269   static bool SkipMessageSetField(io::CodedInputStream* input,
0270                                   uint32_t field_number,
0271                                   UnknownFieldSet* unknown_fields);
0272 
0273   // Parse a MessageSet field.
0274   static bool ParseAndMergeMessageSetField(uint32_t field_number,
0275                                            const FieldDescriptor* field,
0276                                            Message* message,
0277                                            io::CodedInputStream* input);
0278   // Parses the value from the wire that belongs to tag.
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 // Subclass of FieldSkipper which saves skipped fields to an UnknownFieldSet.
0287 class PROTOBUF_EXPORT UnknownFieldSetFieldSkipper : public FieldSkipper {
0288  public:
0289   UnknownFieldSetFieldSkipper(UnknownFieldSet* unknown_fields)
0290       : unknown_fields_(unknown_fields) {}
0291   ~UnknownFieldSetFieldSkipper() override {}
0292 
0293   // implements FieldSkipper -----------------------------------------
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 // inline methods ====================================================
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   // Some compilers don't like enum -> enum casts, so we implicit_cast to
0316   // int first.
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   // Some compilers don't like enum -> enum casts, so we implicit_cast to
0328   // int first.
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                                    /* field_name = */ "");
0340 #else
0341   // Avoid the compiler warning about unused variables.
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   // Avoid the compiler warning about unused variables.
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 // Compute the size of the UnknownFieldSet on the wire.
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 }  // namespace internal
0388 }  // namespace protobuf
0389 }  // namespace google
0390 
0391 #include "google/protobuf/port_undef.inc"
0392 
0393 #endif  // GOOGLE_PROTOBUF_WIRE_FORMAT_H__