• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <google/protobuf/stubs/common.h>
44 #include <google/protobuf/descriptor.h>
45 #include <google/protobuf/message.h>
46 #include <google/protobuf/wire_format_lite.h>
47 #include <google/protobuf/stubs/casts.h>
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(
86       FieldDescriptor::Type type);
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,
119                                        io::CodedOutputStream* output);
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,
149                                              RepeatedField<int>* values);
150 
151   // Write the contents of an UnknownFieldSet to the output.
152   static void SerializeUnknownFields(const UnknownFieldSet& unknown_fields,
153                                      io::CodedOutputStream* output);
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.
164   static void SerializeUnknownMessageSetItems(
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
194       Message* message, io::CodedInputStream* input);
195 
196   // Serialize a single field.
197   static void SerializeFieldWithCachedSizes(
198       const FieldDescriptor* field,  // Cannot be NULL
199       const Message& message, io::CodedOutputStream* output);
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,
213       io::CodedOutputStream* output);
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,
249                                            io::CodedInputStream* input);
250 
251   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormat);
252 };
253 
254 // Subclass of FieldSkipper which saves skipped fields to an UnknownFieldSet.
255 class PROTOBUF_EXPORT UnknownFieldSetFieldSkipper : public FieldSkipper {
256  public:
UnknownFieldSetFieldSkipper(UnknownFieldSet * unknown_fields)257   UnknownFieldSetFieldSkipper(UnknownFieldSet* unknown_fields)
258       : unknown_fields_(unknown_fields) {}
~UnknownFieldSetFieldSkipper()259   ~UnknownFieldSetFieldSkipper() override {}
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:
267   UnknownFieldSet* unknown_fields_;
268 };
269 
270 // inline methods ====================================================
271 
WireTypeForField(const FieldDescriptor * field)272 inline WireFormatLite::WireType WireFormat::WireTypeForField(
273     const FieldDescriptor* field) {
274   if (field->is_packed()) {
275     return WireFormatLite::WIRETYPE_LENGTH_DELIMITED;
276   } else {
277     return WireTypeForFieldType(field->type());
278   }
279 }
280 
WireTypeForFieldType(FieldDescriptor::Type type)281 inline WireFormatLite::WireType WireFormat::WireTypeForFieldType(
282     FieldDescriptor::Type type) {
283   // Some compilers don't like enum -> enum casts, so we implicit_cast to
284   // int first.
285   return WireFormatLite::WireTypeForFieldType(
286       static_cast<WireFormatLite::FieldType>(implicit_cast<int>(type)));
287 }
288 
MakeTag(const FieldDescriptor * field)289 inline uint32 WireFormat::MakeTag(const FieldDescriptor* field) {
290   return WireFormatLite::MakeTag(field->number(), WireTypeForField(field));
291 }
292 
TagSize(int field_number,FieldDescriptor::Type type)293 inline size_t WireFormat::TagSize(int field_number,
294                                   FieldDescriptor::Type type) {
295   // Some compilers don't like enum -> enum casts, so we implicit_cast to
296   // int first.
297   return WireFormatLite::TagSize(
298       field_number,
299       static_cast<WireFormatLite::FieldType>(implicit_cast<int>(type)));
300 }
301 
VerifyUTF8String(const char * data,int size,WireFormat::Operation op)302 inline void WireFormat::VerifyUTF8String(const char* data, int size,
303                                          WireFormat::Operation op) {
304 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
305   WireFormatLite::VerifyUtf8String(
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 
VerifyUTF8StringNamedField(const char * data,int size,WireFormat::Operation op,const char * field_name)315 inline void WireFormat::VerifyUTF8StringNamedField(const char* data, int size,
316                                                    WireFormat::Operation op,
317                                                    const char* field_name) {
318 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
319   WireFormatLite::VerifyUtf8String(
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 
SerializeUnknownMessageSetItems(const UnknownFieldSet & unknown_fields,io::CodedOutputStream * output)331 inline void SerializeUnknownMessageSetItems(
332     const UnknownFieldSet& unknown_fields, io::CodedOutputStream* output) {
333   WireFormat::SerializeUnknownMessageSetItems(unknown_fields, output);
334 }
335 
ComputeUnknownMessageSetItemsSize(const UnknownFieldSet & unknown_fields)336 inline size_t ComputeUnknownMessageSetItemsSize(
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__
348