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>
43
44 #include <google/protobuf/stubs/common.h>
45 #include <google/protobuf/parse_context.h>
46 #include <google/protobuf/io/coded_stream.h>
47 #include <google/protobuf/descriptor.h>
48 #include <google/protobuf/generated_message_util.h>
49 #include <google/protobuf/message.h>
50 #include <google/protobuf/metadata_lite.h>
51 #include <google/protobuf/wire_format_lite.h>
52 #include <google/protobuf/stubs/casts.h>
53
54 #ifdef SWIG
55 #error "You cannot SWIG proto headers"
56 #endif
57
58 #include <google/protobuf/port_def.inc>
59
60 namespace google {
61 namespace protobuf {
62 class UnknownFieldSet; // unknown_field_set.h
63 } // namespace protobuf
64 } // namespace google
65
66 namespace google {
67 namespace protobuf {
68 namespace internal {
69
70 // This class is for internal use by the protocol buffer library and by
71 // protocol-compiler-generated message classes. It must not be called
72 // directly by clients.
73 //
74 // This class contains code for implementing the binary protocol buffer
75 // wire format via reflection. The WireFormatLite class implements the
76 // non-reflection based routines.
77 //
78 // This class is really a namespace that contains only static methods
79 class PROTOBUF_EXPORT WireFormat {
80 public:
81 // Given a field return its WireType
82 static inline WireFormatLite::WireType WireTypeForField(
83 const FieldDescriptor* field);
84
85 // Given a FieldDescriptor::Type return its WireType
86 static inline WireFormatLite::WireType WireTypeForFieldType(
87 FieldDescriptor::Type type);
88
89 // Compute the byte size of a tag. For groups, this includes both the start
90 // and end tags.
91 static inline size_t TagSize(int field_number, FieldDescriptor::Type type);
92
93 // These procedures can be used to implement the methods of Message which
94 // handle parsing and serialization of the protocol buffer wire format
95 // using only the Reflection interface. When you ask the protocol
96 // compiler to optimize for code size rather than speed, it will implement
97 // those methods in terms of these procedures. Of course, these are much
98 // slower than the specialized implementations which the protocol compiler
99 // generates when told to optimize for speed.
100
101 // Read a message in protocol buffer wire format.
102 //
103 // This procedure reads either to the end of the input stream or through
104 // a WIRETYPE_END_GROUP tag ending the message, whichever comes first.
105 // It returns false if the input is invalid.
106 //
107 // Required fields are NOT checked by this method. You must call
108 // IsInitialized() on the resulting message yourself.
109 static bool ParseAndMergePartial(io::CodedInputStream* input,
110 Message* message);
111
112 // This is meant for internal protobuf use (WireFormat is an internal class).
113 // This is the reflective implementation of the _InternalParse functionality.
114 static const char* _InternalParse(Message* msg, const char* ptr,
115 internal::ParseContext* ctx);
116
117 // Serialize a message in protocol buffer wire format.
118 //
119 // Any embedded messages within the message must have their correct sizes
120 // cached. However, the top-level message need not; its size is passed as
121 // a parameter to this procedure.
122 //
123 // These return false iff the underlying stream returns a write error.
SerializeWithCachedSizes(const Message & message,int size,io::CodedOutputStream * output)124 static void SerializeWithCachedSizes(const Message& message, int size,
125 io::CodedOutputStream* output) {
126 int expected_endpoint = output->ByteCount() + size;
127 output->SetCur(
128 _InternalSerialize(message, output->Cur(), output->EpsCopy()));
129 GOOGLE_CHECK_EQ(output->ByteCount(), expected_endpoint)
130 << ": Protocol message serialized to a size different from what was "
131 "originally expected. Perhaps it was modified by another thread "
132 "during serialization?";
133 }
134 static uint8* _InternalSerialize(const Message& message, uint8* target,
135 io::EpsCopyOutputStream* stream);
136
137 // Implements Message::ByteSize() via reflection. WARNING: The result
138 // of this method is *not* cached anywhere. However, all embedded messages
139 // will have their ByteSize() methods called, so their sizes will be cached.
140 // Therefore, calling this method is sufficient to allow you to call
141 // WireFormat::SerializeWithCachedSizes() on the same object.
142 static size_t ByteSize(const Message& message);
143
144 // -----------------------------------------------------------------
145 // Helpers for dealing with unknown fields
146
147 // Skips a field value of the given WireType. The input should start
148 // positioned immediately after the tag. If unknown_fields is non-NULL,
149 // the contents of the field will be added to it.
150 static bool SkipField(io::CodedInputStream* input, uint32 tag,
151 UnknownFieldSet* unknown_fields);
152
153 // Reads and ignores a message from the input. If unknown_fields is
154 // non-NULL, the contents will be added to it.
155 static bool SkipMessage(io::CodedInputStream* input,
156 UnknownFieldSet* unknown_fields);
157
158 // Read a packed enum field. If the is_valid function is not NULL, values
159 // for which is_valid(value) returns false are appended to
160 // unknown_fields_stream.
161 static bool ReadPackedEnumPreserveUnknowns(io::CodedInputStream* input,
162 uint32 field_number,
163 bool (*is_valid)(int),
164 UnknownFieldSet* unknown_fields,
165 RepeatedField<int>* values);
166
167 // Write the contents of an UnknownFieldSet to the output.
SerializeUnknownFields(const UnknownFieldSet & unknown_fields,io::CodedOutputStream * output)168 static void SerializeUnknownFields(const UnknownFieldSet& unknown_fields,
169 io::CodedOutputStream* output) {
170 output->SetCur(InternalSerializeUnknownFieldsToArray(
171 unknown_fields, output->Cur(), output->EpsCopy()));
172 }
173 // Same as above, except writing directly to the provided buffer.
174 // Requires that the buffer have sufficient capacity for
175 // ComputeUnknownFieldsSize(unknown_fields).
176 //
177 // Returns a pointer past the last written byte.
SerializeUnknownFieldsToArray(const UnknownFieldSet & unknown_fields,uint8 * target)178 static uint8* SerializeUnknownFieldsToArray(
179 const UnknownFieldSet& unknown_fields, uint8* target) {
180 io::EpsCopyOutputStream stream(
181 target, static_cast<int>(ComputeUnknownFieldsSize(unknown_fields)),
182 io::CodedOutputStream::IsDefaultSerializationDeterministic());
183 return InternalSerializeUnknownFieldsToArray(unknown_fields, target,
184 &stream);
185 }
186 static uint8* InternalSerializeUnknownFieldsToArray(
187 const UnknownFieldSet& unknown_fields, uint8* target,
188 io::EpsCopyOutputStream* stream);
189
190 // Same thing except for messages that have the message_set_wire_format
191 // option.
SerializeUnknownMessageSetItems(const UnknownFieldSet & unknown_fields,io::CodedOutputStream * output)192 static void SerializeUnknownMessageSetItems(
193 const UnknownFieldSet& unknown_fields, io::CodedOutputStream* output) {
194 output->SetCur(InternalSerializeUnknownMessageSetItemsToArray(
195 unknown_fields, output->Cur(), output->EpsCopy()));
196 }
197 // Same as above, except writing directly to the provided buffer.
198 // Requires that the buffer have sufficient capacity for
199 // ComputeUnknownMessageSetItemsSize(unknown_fields).
200 //
201 // Returns a pointer past the last written byte.
202 static uint8* SerializeUnknownMessageSetItemsToArray(
203 const UnknownFieldSet& unknown_fields, uint8* target);
204 static uint8* InternalSerializeUnknownMessageSetItemsToArray(
205 const UnknownFieldSet& unknown_fields, uint8* target,
206 io::EpsCopyOutputStream* stream);
207
208 // Compute the size of the UnknownFieldSet on the wire.
209 static size_t ComputeUnknownFieldsSize(const UnknownFieldSet& unknown_fields);
210
211 // Same thing except for messages that have the message_set_wire_format
212 // option.
213 static size_t ComputeUnknownMessageSetItemsSize(
214 const UnknownFieldSet& unknown_fields);
215
216 // Helper functions for encoding and decoding tags. (Inlined below and in
217 // _inl.h)
218 //
219 // This is different from MakeTag(field->number(), field->type()) in the
220 // case of packed repeated fields.
221 static uint32 MakeTag(const FieldDescriptor* field);
222
223 // Parse a single field. The input should start out positioned immediately
224 // after the tag.
225 static bool ParseAndMergeField(
226 uint32 tag,
227 const FieldDescriptor* field, // May be NULL for unknown
228 Message* message, io::CodedInputStream* input);
229
230 // Serialize a single field.
SerializeFieldWithCachedSizes(const FieldDescriptor * field,const Message & message,io::CodedOutputStream * output)231 static void SerializeFieldWithCachedSizes(
232 const FieldDescriptor* field, // Cannot be NULL
233 const Message& message, io::CodedOutputStream* output) {
234 output->SetCur(InternalSerializeField(field, message, output->Cur(),
235 output->EpsCopy()));
236 }
237 static uint8* InternalSerializeField(
238 const FieldDescriptor* field, // Cannot be NULL
239 const Message& message, uint8* target, io::EpsCopyOutputStream* stream);
240
241 // Compute size of a single field. If the field is a message type, this
242 // will call ByteSize() for the embedded message, insuring that it caches
243 // its size.
244 static size_t FieldByteSize(const FieldDescriptor* field, // Cannot be NULL
245 const Message& message);
246
247 // Parse/serialize a MessageSet::Item group. Used with messages that use
248 // option message_set_wire_format = true.
249 static bool ParseAndMergeMessageSetItem(io::CodedInputStream* input,
250 Message* message);
SerializeMessageSetItemWithCachedSizes(const FieldDescriptor * field,const Message & message,io::CodedOutputStream * output)251 static void SerializeMessageSetItemWithCachedSizes(
252 const FieldDescriptor* field, const Message& message,
253 io::CodedOutputStream* output) {
254 output->SetCur(InternalSerializeMessageSetItem(
255 field, message, output->Cur(), output->EpsCopy()));
256 }
257 static uint8* InternalSerializeMessageSetItem(
258 const FieldDescriptor* field, const Message& message, uint8* target,
259 io::EpsCopyOutputStream* stream);
260 static size_t MessageSetItemByteSize(const FieldDescriptor* field,
261 const Message& message);
262
263 // Computes the byte size of a field, excluding tags. For packed fields, it
264 // only includes the size of the raw data, and not the size of the total
265 // length, but for other length-delimited types, the size of the length is
266 // included.
267 static size_t FieldDataOnlyByteSize(
268 const FieldDescriptor* field, // Cannot be NULL
269 const Message& message);
270
271 enum Operation {
272 PARSE = 0,
273 SERIALIZE = 1,
274 };
275
276 // Verifies that a string field is valid UTF8, logging an error if not.
277 // This function will not be called by newly generated protobuf code
278 // but remains present to support existing code.
279 static void VerifyUTF8String(const char* data, int size, Operation op);
280 // The NamedField variant takes a field name in order to produce an
281 // informative error message if verification fails.
282 static void VerifyUTF8StringNamedField(const char* data, int size,
283 Operation op, const char* field_name);
284
285 private:
286 struct MessageSetParser;
287 // Skip a MessageSet field.
288 static bool SkipMessageSetField(io::CodedInputStream* input,
289 uint32 field_number,
290 UnknownFieldSet* unknown_fields);
291
292 // Parse a MessageSet field.
293 static bool ParseAndMergeMessageSetField(uint32 field_number,
294 const FieldDescriptor* field,
295 Message* message,
296 io::CodedInputStream* input);
297 // Parses the value from the wire that belongs to tag.
298 static const char* _InternalParseAndMergeField(Message* msg, const char* ptr,
299 internal::ParseContext* ctx,
300 uint64 tag,
301 const Reflection* reflection,
302 const FieldDescriptor* field);
303
304 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormat);
305 };
306
307 // Subclass of FieldSkipper which saves skipped fields to an UnknownFieldSet.
308 class PROTOBUF_EXPORT UnknownFieldSetFieldSkipper : public FieldSkipper {
309 public:
UnknownFieldSetFieldSkipper(UnknownFieldSet * unknown_fields)310 UnknownFieldSetFieldSkipper(UnknownFieldSet* unknown_fields)
311 : unknown_fields_(unknown_fields) {}
~UnknownFieldSetFieldSkipper()312 ~UnknownFieldSetFieldSkipper() override {}
313
314 // implements FieldSkipper -----------------------------------------
315 bool SkipField(io::CodedInputStream* input, uint32 tag) override;
316 bool SkipMessage(io::CodedInputStream* input) override;
317 void SkipUnknownEnum(int field_number, int value) override;
318
319 protected:
320 UnknownFieldSet* unknown_fields_;
321 };
322
323 // inline methods ====================================================
324
WireTypeForField(const FieldDescriptor * field)325 inline WireFormatLite::WireType WireFormat::WireTypeForField(
326 const FieldDescriptor* field) {
327 if (field->is_packed()) {
328 return WireFormatLite::WIRETYPE_LENGTH_DELIMITED;
329 } else {
330 return WireTypeForFieldType(field->type());
331 }
332 }
333
WireTypeForFieldType(FieldDescriptor::Type type)334 inline WireFormatLite::WireType WireFormat::WireTypeForFieldType(
335 FieldDescriptor::Type type) {
336 // Some compilers don't like enum -> enum casts, so we implicit_cast to
337 // int first.
338 return WireFormatLite::WireTypeForFieldType(
339 static_cast<WireFormatLite::FieldType>(implicit_cast<int>(type)));
340 }
341
MakeTag(const FieldDescriptor * field)342 inline uint32 WireFormat::MakeTag(const FieldDescriptor* field) {
343 return WireFormatLite::MakeTag(field->number(), WireTypeForField(field));
344 }
345
TagSize(int field_number,FieldDescriptor::Type type)346 inline size_t WireFormat::TagSize(int field_number,
347 FieldDescriptor::Type type) {
348 // Some compilers don't like enum -> enum casts, so we implicit_cast to
349 // int first.
350 return WireFormatLite::TagSize(
351 field_number,
352 static_cast<WireFormatLite::FieldType>(implicit_cast<int>(type)));
353 }
354
VerifyUTF8String(const char * data,int size,WireFormat::Operation op)355 inline void WireFormat::VerifyUTF8String(const char* data, int size,
356 WireFormat::Operation op) {
357 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
358 WireFormatLite::VerifyUtf8String(
359 data, size, static_cast<WireFormatLite::Operation>(op), NULL);
360 #else
361 // Avoid the compiler warning about unused variables.
362 (void)data;
363 (void)size;
364 (void)op;
365 #endif
366 }
367
VerifyUTF8StringNamedField(const char * data,int size,WireFormat::Operation op,const char * field_name)368 inline void WireFormat::VerifyUTF8StringNamedField(const char* data, int size,
369 WireFormat::Operation op,
370 const char* field_name) {
371 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
372 WireFormatLite::VerifyUtf8String(
373 data, size, static_cast<WireFormatLite::Operation>(op), field_name);
374 #else
375 // Avoid the compiler warning about unused variables.
376 (void)data;
377 (void)size;
378 (void)op;
379 (void)field_name;
380 #endif
381 }
382
383
InternalSerializeUnknownMessageSetItemsToArray(const UnknownFieldSet & unknown_fields,uint8 * target,io::EpsCopyOutputStream * stream)384 inline uint8* InternalSerializeUnknownMessageSetItemsToArray(
385 const UnknownFieldSet& unknown_fields, uint8* target,
386 io::EpsCopyOutputStream* stream) {
387 return WireFormat::InternalSerializeUnknownMessageSetItemsToArray(
388 unknown_fields, target, stream);
389 }
390
ComputeUnknownMessageSetItemsSize(const UnknownFieldSet & unknown_fields)391 inline size_t ComputeUnknownMessageSetItemsSize(
392 const UnknownFieldSet& unknown_fields) {
393 return WireFormat::ComputeUnknownMessageSetItemsSize(unknown_fields);
394 }
395
396 // Compute the size of the UnknownFieldSet on the wire.
397 PROTOBUF_EXPORT
398 size_t ComputeUnknownFieldsSize(const InternalMetadata& metadata, size_t size,
399 CachedSize* cached_size);
400
401 } // namespace internal
402 } // namespace protobuf
403 } // namespace google
404
405 #include <google/protobuf/port_undef.inc>
406
407 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_H__
408