wire_format.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 // Author: kenton@google.com (Kenton Varda)
32 // atenasio@google.com (Chris Atenasio) (ZigZag transform)
33 // Based on original Protocol Buffers design by
34 // Sanjay Ghemawat, Jeff Dean, and others.
35 //
36 // This header is logically internal, but is made public because it is used
37 // from protocol-compiler-generated code, which may reside in other components.
38 
39 #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_H__
40 #define GOOGLE_PROTOBUF_WIRE_FORMAT_H__
41 
42 #include <string>
48 
49 #ifdef SWIG
50 #error "You cannot SWIG proto headers"
51 #endif
52 
53 #include <google/protobuf/port_def.inc>
54 
55 namespace google {
56 namespace protobuf {
57 namespace io {
58 class CodedInputStream; // coded_stream.h
59 class CodedOutputStream; // coded_stream.h
60 } // namespace io
61 class UnknownFieldSet; // unknown_field_set.h
62 } // namespace protobuf
63 } // namespace google
64 
65 namespace google {
66 namespace protobuf {
67 namespace internal {
68 
69 // This class is for internal use by the protocol buffer library and by
70 // protocol-complier-generated message classes. It must not be called
71 // directly by clients.
72 //
73 // This class contains code for implementing the binary protocol buffer
74 // wire format via reflection. The WireFormatLite class implements the
75 // non-reflection based routines.
76 //
77 // This class is really a namespace that contains only static methods
78 class PROTOBUF_EXPORT WireFormat {
79  public:
80  // Given a field return its WireType
81  static inline WireFormatLite::WireType WireTypeForField(
82  const FieldDescriptor* field);
83 
84  // Given a FieldDescriptor::Type return its WireType
85  static inline WireFormatLite::WireType WireTypeForFieldType(
87 
88  // Compute the byte size of a tag. For groups, this includes both the start
89  // and end tags.
90  static inline size_t TagSize(int field_number, FieldDescriptor::Type type);
91 
92  // These procedures can be used to implement the methods of Message which
93  // handle parsing and serialization of the protocol buffer wire format
94  // using only the Reflection interface. When you ask the protocol
95  // compiler to optimize for code size rather than speed, it will implement
96  // those methods in terms of these procedures. Of course, these are much
97  // slower than the specialized implementations which the protocol compiler
98  // generates when told to optimize for speed.
99 
100  // Read a message in protocol buffer wire format.
101  //
102  // This procedure reads either to the end of the input stream or through
103  // a WIRETYPE_END_GROUP tag ending the message, whichever comes first.
104  // It returns false if the input is invalid.
105  //
106  // Required fields are NOT checked by this method. You must call
107  // IsInitialized() on the resulting message yourself.
108  static bool ParseAndMergePartial(io::CodedInputStream* input,
109  Message* message);
110 
111  // Serialize a message in protocol buffer wire format.
112  //
113  // Any embedded messages within the message must have their correct sizes
114  // cached. However, the top-level message need not; its size is passed as
115  // a parameter to this procedure.
116  //
117  // These return false iff the underlying stream returns a write error.
118  static void SerializeWithCachedSizes(const Message& message, int size,
120 
121  // Implements Message::ByteSize() via reflection. WARNING: The result
122  // of this method is *not* cached anywhere. However, all embedded messages
123  // will have their ByteSize() methods called, so their sizes will be cached.
124  // Therefore, calling this method is sufficient to allow you to call
125  // WireFormat::SerializeWithCachedSizes() on the same object.
126  static size_t ByteSize(const Message& message);
127 
128  // -----------------------------------------------------------------
129  // Helpers for dealing with unknown fields
130 
131  // Skips a field value of the given WireType. The input should start
132  // positioned immediately after the tag. If unknown_fields is non-NULL,
133  // the contents of the field will be added to it.
134  static bool SkipField(io::CodedInputStream* input, uint32 tag,
135  UnknownFieldSet* unknown_fields);
136 
137  // Reads and ignores a message from the input. If unknown_fields is
138  // non-NULL, the contents will be added to it.
139  static bool SkipMessage(io::CodedInputStream* input,
140  UnknownFieldSet* unknown_fields);
141 
142  // Read a packed enum field. If the is_valid function is not NULL, values
143  // for which is_valid(value) returns false are appended to
144  // unknown_fields_stream.
145  static bool ReadPackedEnumPreserveUnknowns(io::CodedInputStream* input,
146  uint32 field_number,
147  bool (*is_valid)(int),
148  UnknownFieldSet* unknown_fields,
150 
151  // Write the contents of an UnknownFieldSet to the output.
152  static void SerializeUnknownFields(const UnknownFieldSet& unknown_fields,
154  // Same as above, except writing directly to the provided buffer.
155  // Requires that the buffer have sufficient capacity for
156  // ComputeUnknownFieldsSize(unknown_fields).
157  //
158  // Returns a pointer past the last written byte.
159  static uint8* SerializeUnknownFieldsToArray(
160  const UnknownFieldSet& unknown_fields, uint8* target);
161 
162  // Same thing except for messages that have the message_set_wire_format
163  // option.
165  const UnknownFieldSet& unknown_fields, io::CodedOutputStream* output);
166  // Same as above, except writing directly to the provided buffer.
167  // Requires that the buffer have sufficient capacity for
168  // ComputeUnknownMessageSetItemsSize(unknown_fields).
169  //
170  // Returns a pointer past the last written byte.
171  static uint8* SerializeUnknownMessageSetItemsToArray(
172  const UnknownFieldSet& unknown_fields, uint8* target);
173 
174  // Compute the size of the UnknownFieldSet on the wire.
175  static size_t ComputeUnknownFieldsSize(const UnknownFieldSet& unknown_fields);
176 
177  // Same thing except for messages that have the message_set_wire_format
178  // option.
179  static size_t ComputeUnknownMessageSetItemsSize(
180  const UnknownFieldSet& unknown_fields);
181 
182  // Helper functions for encoding and decoding tags. (Inlined below and in
183  // _inl.h)
184  //
185  // This is different from MakeTag(field->number(), field->type()) in the
186  // case of packed repeated fields.
187  static uint32 MakeTag(const FieldDescriptor* field);
188 
189  // Parse a single field. The input should start out positioned immediately
190  // after the tag.
191  static bool ParseAndMergeField(
192  uint32 tag,
193  const FieldDescriptor* field, // May be NULL for unknown
195 
196  // Serialize a single field.
197  static void SerializeFieldWithCachedSizes(
198  const FieldDescriptor* field, // Cannot be NULL
200 
201  // Compute size of a single field. If the field is a message type, this
202  // will call ByteSize() for the embedded message, insuring that it caches
203  // its size.
204  static size_t FieldByteSize(const FieldDescriptor* field, // Cannot be NULL
205  const Message& message);
206 
207  // Parse/serialize a MessageSet::Item group. Used with messages that use
208  // opion message_set_wire_format = true.
209  static bool ParseAndMergeMessageSetItem(io::CodedInputStream* input,
210  Message* message);
211  static void SerializeMessageSetItemWithCachedSizes(
212  const FieldDescriptor* field, const Message& message,
214  static size_t MessageSetItemByteSize(const FieldDescriptor* field,
215  const Message& message);
216 
217  // Computes the byte size of a field, excluding tags. For packed fields, it
218  // only includes the size of the raw data, and not the size of the total
219  // length, but for other length-delimited types, the size of the length is
220  // included.
221  static size_t FieldDataOnlyByteSize(
222  const FieldDescriptor* field, // Cannot be NULL
223  const Message& message);
224 
225  enum Operation {
226  PARSE = 0,
227  SERIALIZE = 1,
228  };
229 
230  // Verifies that a string field is valid UTF8, logging an error if not.
231  // This function will not be called by newly generated protobuf code
232  // but remains present to support existing code.
233  static void VerifyUTF8String(const char* data, int size, Operation op);
234  // The NamedField variant takes a field name in order to produce an
235  // informative error message if verification fails.
236  static void VerifyUTF8StringNamedField(const char* data, int size,
237  Operation op, const char* field_name);
238 
239  private:
240  // Skip a MessageSet field.
241  static bool SkipMessageSetField(io::CodedInputStream* input,
242  uint32 field_number,
243  UnknownFieldSet* unknown_fields);
244 
245  // Parse a MessageSet field.
246  static bool ParseAndMergeMessageSetField(uint32 field_number,
247  const FieldDescriptor* field,
248  Message* message,
250 
252 };
253 
254 // Subclass of FieldSkipper which saves skipped fields to an UnknownFieldSet.
255 class PROTOBUF_EXPORT UnknownFieldSetFieldSkipper : public FieldSkipper {
256  public:
258  : unknown_fields_(unknown_fields) {}
260 
261  // implements FieldSkipper -----------------------------------------
262  bool SkipField(io::CodedInputStream* input, uint32 tag) override;
263  bool SkipMessage(io::CodedInputStream* input) override;
264  void SkipUnknownEnum(int field_number, int value) override;
265 
266  protected:
268 };
269 
270 // inline methods ====================================================
271 
273  const FieldDescriptor* field) {
274  if (field->is_packed()) {
276  } else {
277  return WireTypeForFieldType(field->type());
278  }
279 }
280 
283  // Some compilers don't like enum -> enum casts, so we implicit_cast to
284  // int first.
286  static_cast<WireFormatLite::FieldType>(implicit_cast<int>(type)));
287 }
288 
291 }
292 
293 inline size_t WireFormat::TagSize(int field_number,
295  // Some compilers don't like enum -> enum casts, so we implicit_cast to
296  // int first.
298  field_number,
299  static_cast<WireFormatLite::FieldType>(implicit_cast<int>(type)));
300 }
301 
302 inline void WireFormat::VerifyUTF8String(const char* data, int size,
304 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
306  data, size, static_cast<WireFormatLite::Operation>(op), NULL);
307 #else
308  // Avoid the compiler warning about unused variables.
309  (void)data;
310  (void)size;
311  (void)op;
312 #endif
313 }
314 
315 inline void WireFormat::VerifyUTF8StringNamedField(const char* data, int size,
317  const char* field_name) {
318 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
320  data, size, static_cast<WireFormatLite::Operation>(op), field_name);
321 #else
322  // Avoid the compiler warning about unused variables.
323  (void)data;
324  (void)size;
325  (void)op;
326  (void)field_name;
327 #endif
328 }
329 
330 
332  const UnknownFieldSet& unknown_fields, io::CodedOutputStream* output) {
334 }
335 
337  const UnknownFieldSet& unknown_fields) {
338  return WireFormat::ComputeUnknownMessageSetItemsSize(unknown_fields);
339 }
340 
341 } // namespace internal
342 } // namespace protobuf
343 } // namespace google
344 
345 #include <google/protobuf/port_undef.inc>
346 
347 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_H__
google::protobuf::FieldDescriptor::Type
Type
Definition: src/google/protobuf/descriptor.h:521
google::protobuf.internal.wire_format.MessageSetItemByteSize
def MessageSetItemByteSize(field_number, msg)
Definition: wire_format.py:204
google::protobuf.internal::WireFormatLite::WireTypeForFieldType
static WireFormatLite::WireType WireTypeForFieldType(WireFormatLite::FieldType type)
Definition: wire_format_lite.h:152
google::protobuf.internal::ComputeUnknownMessageSetItemsSize
size_t ComputeUnknownMessageSetItemsSize(const UnknownFieldSet &unknown_fields)
Definition: wire_format.h:336
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
google::protobuf.internal::WireFormat::TagSize
static size_t TagSize(int field_number, FieldDescriptor::Type type)
Definition: wire_format.h:293
google::protobuf.internal.decoder.SkipField
def SkipField
Definition: decoder.py:1016
wire_format_lite.h
NULL
NULL
Definition: test_security_zap.cpp:405
input
std::string input
Definition: tokenizer_unittest.cc:197
google::protobuf::uint8
uint8_t uint8
Definition: protobuf/src/google/protobuf/stubs/port.h:153
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf.internal::WireFormat::MakeTag
static uint32 MakeTag(const FieldDescriptor *field)
Definition: wire_format.h:289
google::protobuf.internal::FieldSkipper
Definition: wire_format_lite.h:726
google::protobuf.internal::WireFormat
Definition: wire_format.h:78
google::protobuf::RepeatedField< int >
target
GLenum target
Definition: glcorearb.h:3739
google::protobuf::python::cmessage::UnknownFieldSet
static PyObject * UnknownFieldSet(CMessage *self)
Definition: python/google/protobuf/pyext/message.cc:2501
google::protobuf.internal::WireFormat::Operation
Operation
Definition: wire_format.h:225
google::protobuf.internal::UnknownFieldSetFieldSkipper::~UnknownFieldSetFieldSkipper
~UnknownFieldSetFieldSkipper() override
Definition: wire_format.h:259
google::protobuf.internal::UnknownFieldSetFieldSkipper::unknown_fields_
UnknownFieldSet * unknown_fields_
Definition: wire_format.h:267
values
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:3591
google::protobuf.internal::WireFormatLite::Operation
Operation
Definition: wire_format_lite.h:322
google::protobuf.internal::UnknownFieldSetFieldSkipper::UnknownFieldSetFieldSkipper
UnknownFieldSetFieldSkipper(UnknownFieldSet *unknown_fields)
Definition: wire_format.h:257
google::protobuf.internal::WireFormat::ComputeUnknownMessageSetItemsSize
static size_t ComputeUnknownMessageSetItemsSize(const UnknownFieldSet &unknown_fields)
Definition: wire_format.cc:361
google::protobuf.internal::WireFormat::VerifyUTF8String
static void VerifyUTF8String(const char *data, int size, Operation op)
Definition: wire_format.h:302
message.h
google::protobuf.internal::WireFormatLite::VerifyUtf8String
static bool VerifyUtf8String(const char *data, int size, Operation op, const char *field_name)
Definition: wire_format_lite.cc:590
google::protobuf.internal::WireFormatLite::FieldType
FieldType
Definition: wire_format_lite.h:111
google::protobuf.internal::WireFormatLite::MakeTag
constexpr static uint32 MakeTag(int field_number, WireType type)
Definition: wire_format_lite.h:768
google::protobuf.internal.python_message.ByteSize
ByteSize
Definition: python_message.py:1067
casts.h
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
google::protobuf.internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED
@ WIRETYPE_LENGTH_DELIMITED
Definition: wire_format_lite.h:104
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
google::protobuf::io::CodedOutputStream
Definition: coded_stream.h:693
google::protobuf.internal::WireFormat::WireTypeForField
static WireFormatLite::WireType WireTypeForField(const FieldDescriptor *field)
Definition: wire_format.h:272
google::protobuf.internal::UnknownFieldSetFieldSkipper
Definition: wire_format.h:255
type
GLenum type
Definition: glcorearb.h:2695
google::protobuf.internal::WireFormat::SerializeUnknownMessageSetItems
static void SerializeUnknownMessageSetItems(const UnknownFieldSet &unknown_fields, io::CodedOutputStream *output)
Definition: wire_format.cc:263
google::protobuf::Message
Definition: src/google/protobuf/message.h:205
google::protobuf.internal::WireFormatLite::TagSize
static size_t TagSize(int field_number, WireFormatLite::FieldType type)
Definition: wire_format_lite.h:781
google::protobuf.internal::WireFormat::VerifyUTF8StringNamedField
static void VerifyUTF8StringNamedField(const char *data, int size, Operation op, const char *field_name)
Definition: wire_format.h:315
common.h
size
GLsizeiptr size
Definition: glcorearb.h:2943
google::protobuf.internal::WireFormat::WireTypeForFieldType
static WireFormatLite::WireType WireTypeForFieldType(FieldDescriptor::Type type)
Definition: wire_format.h:281
google::protobuf::UnknownFieldSet
Definition: unknown_field_set.h:86
google::protobuf::io::CodedInputStream
Definition: coded_stream.h:173
descriptor.h
google::protobuf.internal::WireFormatLite::WireType
WireType
Definition: wire_format_lite.h:101
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
internal
Definition: any.pb.h:40
google::protobuf.internal::SerializeUnknownMessageSetItems
void SerializeUnknownMessageSetItems(const UnknownFieldSet &unknown_fields, io::CodedOutputStream *output)
Definition: wire_format.h:331
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:01