| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 // Given a field return its WireType | 78 // Given a field return its WireType |
| 79 static inline WireFormatLite::WireType WireTypeForField( | 79 static inline WireFormatLite::WireType WireTypeForField( |
| 80 const FieldDescriptor* field); | 80 const FieldDescriptor* field); |
| 81 | 81 |
| 82 // Given a FieldDescriptor::Type return its WireType | 82 // Given a FieldDescriptor::Type return its WireType |
| 83 static inline WireFormatLite::WireType WireTypeForFieldType( | 83 static inline WireFormatLite::WireType WireTypeForFieldType( |
| 84 FieldDescriptor::Type type); | 84 FieldDescriptor::Type type); |
| 85 | 85 |
| 86 // Compute the byte size of a tag. For groups, this includes both the start | 86 // Compute the byte size of a tag. For groups, this includes both the start |
| 87 // and end tags. | 87 // and end tags. |
| 88 static inline int TagSize(int field_number, FieldDescriptor::Type type); | 88 static inline size_t TagSize(int field_number, FieldDescriptor::Type type); |
| 89 | 89 |
| 90 // These procedures can be used to implement the methods of Message which | 90 // These procedures can be used to implement the methods of Message which |
| 91 // handle parsing and serialization of the protocol buffer wire format | 91 // handle parsing and serialization of the protocol buffer wire format |
| 92 // using only the Reflection interface. When you ask the protocol | 92 // using only the Reflection interface. When you ask the protocol |
| 93 // compiler to optimize for code size rather than speed, it will implement | 93 // compiler to optimize for code size rather than speed, it will implement |
| 94 // those methods in terms of these procedures. Of course, these are much | 94 // those methods in terms of these procedures. Of course, these are much |
| 95 // slower than the specialized implementations which the protocol compiler | 95 // slower than the specialized implementations which the protocol compiler |
| 96 // generates when told to optimize for speed. | 96 // generates when told to optimize for speed. |
| 97 | 97 |
| 98 // Read a message in protocol buffer wire format. | 98 // Read a message in protocol buffer wire format. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 115 // These return false iff the underlying stream returns a write error. | 115 // These return false iff the underlying stream returns a write error. |
| 116 static void SerializeWithCachedSizes( | 116 static void SerializeWithCachedSizes( |
| 117 const Message& message, | 117 const Message& message, |
| 118 int size, io::CodedOutputStream* output); | 118 int size, io::CodedOutputStream* output); |
| 119 | 119 |
| 120 // Implements Message::ByteSize() via reflection. WARNING: The result | 120 // Implements Message::ByteSize() via reflection. WARNING: The result |
| 121 // of this method is *not* cached anywhere. However, all embedded messages | 121 // of this method is *not* cached anywhere. However, all embedded messages |
| 122 // will have their ByteSize() methods called, so their sizes will be cached. | 122 // will have their ByteSize() methods called, so their sizes will be cached. |
| 123 // Therefore, calling this method is sufficient to allow you to call | 123 // Therefore, calling this method is sufficient to allow you to call |
| 124 // WireFormat::SerializeWithCachedSizes() on the same object. | 124 // WireFormat::SerializeWithCachedSizes() on the same object. |
| 125 static int ByteSize(const Message& message); | 125 static size_t ByteSize(const Message& message); |
| 126 | 126 |
| 127 // ----------------------------------------------------------------- | 127 // ----------------------------------------------------------------- |
| 128 // Helpers for dealing with unknown fields | 128 // Helpers for dealing with unknown fields |
| 129 | 129 |
| 130 // Skips a field value of the given WireType. The input should start | 130 // Skips a field value of the given WireType. The input should start |
| 131 // positioned immediately after the tag. If unknown_fields is non-NULL, | 131 // positioned immediately after the tag. If unknown_fields is non-NULL, |
| 132 // the contents of the field will be added to it. | 132 // the contents of the field will be added to it. |
| 133 static bool SkipField(io::CodedInputStream* input, uint32 tag, | 133 static bool SkipField(io::CodedInputStream* input, uint32 tag, |
| 134 UnknownFieldSet* unknown_fields); | 134 UnknownFieldSet* unknown_fields); |
| 135 | 135 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 166 // Same as above, except writing directly to the provided buffer. | 166 // Same as above, except writing directly to the provided buffer. |
| 167 // Requires that the buffer have sufficient capacity for | 167 // Requires that the buffer have sufficient capacity for |
| 168 // ComputeUnknownMessageSetItemsSize(unknown_fields). | 168 // ComputeUnknownMessageSetItemsSize(unknown_fields). |
| 169 // | 169 // |
| 170 // Returns a pointer past the last written byte. | 170 // Returns a pointer past the last written byte. |
| 171 static uint8* SerializeUnknownMessageSetItemsToArray( | 171 static uint8* SerializeUnknownMessageSetItemsToArray( |
| 172 const UnknownFieldSet& unknown_fields, | 172 const UnknownFieldSet& unknown_fields, |
| 173 uint8* target); | 173 uint8* target); |
| 174 | 174 |
| 175 // Compute the size of the UnknownFieldSet on the wire. | 175 // Compute the size of the UnknownFieldSet on the wire. |
| 176 static int ComputeUnknownFieldsSize(const UnknownFieldSet& unknown_fields); | 176 static size_t ComputeUnknownFieldsSize(const UnknownFieldSet& unknown_fields); |
| 177 | 177 |
| 178 // Same thing except for messages that have the message_set_wire_format | 178 // Same thing except for messages that have the message_set_wire_format |
| 179 // option. | 179 // option. |
| 180 static int ComputeUnknownMessageSetItemsSize( | 180 static size_t ComputeUnknownMessageSetItemsSize( |
| 181 const UnknownFieldSet& unknown_fields); | 181 const UnknownFieldSet& unknown_fields); |
| 182 | 182 |
| 183 | 183 |
| 184 // Helper functions for encoding and decoding tags. (Inlined below and in | 184 // Helper functions for encoding and decoding tags. (Inlined below and in |
| 185 // _inl.h) | 185 // _inl.h) |
| 186 // | 186 // |
| 187 // This is different from MakeTag(field->number(), field->type()) in the case | 187 // This is different from MakeTag(field->number(), field->type()) in the case |
| 188 // of packed repeated fields. | 188 // of packed repeated fields. |
| 189 static uint32 MakeTag(const FieldDescriptor* field); | 189 static uint32 MakeTag(const FieldDescriptor* field); |
| 190 | 190 |
| 191 // Parse a single field. The input should start out positioned immediately | 191 // Parse a single field. The input should start out positioned immediately |
| 192 // after the tag. | 192 // after the tag. |
| 193 static bool ParseAndMergeField( | 193 static bool ParseAndMergeField( |
| 194 uint32 tag, | 194 uint32 tag, |
| 195 const FieldDescriptor* field, // May be NULL for unknown | 195 const FieldDescriptor* field, // May be NULL for unknown |
| 196 Message* message, | 196 Message* message, |
| 197 io::CodedInputStream* input); | 197 io::CodedInputStream* input); |
| 198 | 198 |
| 199 // Serialize a single field. | 199 // Serialize a single field. |
| 200 static void SerializeFieldWithCachedSizes( | 200 static void SerializeFieldWithCachedSizes( |
| 201 const FieldDescriptor* field, // Cannot be NULL | 201 const FieldDescriptor* field, // Cannot be NULL |
| 202 const Message& message, | 202 const Message& message, |
| 203 io::CodedOutputStream* output); | 203 io::CodedOutputStream* output); |
| 204 | 204 |
| 205 // Compute size of a single field. If the field is a message type, this | 205 // Compute size of a single field. If the field is a message type, this |
| 206 // will call ByteSize() for the embedded message, insuring that it caches | 206 // will call ByteSize() for the embedded message, insuring that it caches |
| 207 // its size. | 207 // its size. |
| 208 static int FieldByteSize( | 208 static size_t FieldByteSize( |
| 209 const FieldDescriptor* field, // Cannot be NULL | 209 const FieldDescriptor* field, // Cannot be NULL |
| 210 const Message& message); | 210 const Message& message); |
| 211 | 211 |
| 212 // Parse/serialize a MessageSet::Item group. Used with messages that use | 212 // Parse/serialize a MessageSet::Item group. Used with messages that use |
| 213 // opion message_set_wire_format = true. | 213 // opion message_set_wire_format = true. |
| 214 static bool ParseAndMergeMessageSetItem( | 214 static bool ParseAndMergeMessageSetItem( |
| 215 io::CodedInputStream* input, | 215 io::CodedInputStream* input, |
| 216 Message* message); | 216 Message* message); |
| 217 static void SerializeMessageSetItemWithCachedSizes( | 217 static void SerializeMessageSetItemWithCachedSizes( |
| 218 const FieldDescriptor* field, | 218 const FieldDescriptor* field, |
| 219 const Message& message, | 219 const Message& message, |
| 220 io::CodedOutputStream* output); | 220 io::CodedOutputStream* output); |
| 221 static int MessageSetItemByteSize( | 221 static size_t MessageSetItemByteSize( |
| 222 const FieldDescriptor* field, | 222 const FieldDescriptor* field, |
| 223 const Message& message); | 223 const Message& message); |
| 224 | 224 |
| 225 // Computes the byte size of a field, excluding tags. For packed fields, it | 225 // Computes the byte size of a field, excluding tags. For packed fields, it |
| 226 // only includes the size of the raw data, and not the size of the total | 226 // only includes the size of the raw data, and not the size of the total |
| 227 // length, but for other length-delimited types, the size of the length is | 227 // length, but for other length-delimited types, the size of the length is |
| 228 // included. | 228 // included. |
| 229 static int FieldDataOnlyByteSize( | 229 static size_t FieldDataOnlyByteSize( |
| 230 const FieldDescriptor* field, // Cannot be NULL | 230 const FieldDescriptor* field, // Cannot be NULL |
| 231 const Message& message); | 231 const Message& message); |
| 232 | 232 |
| 233 enum Operation { | 233 enum Operation { |
| 234 PARSE = 0, | 234 PARSE = 0, |
| 235 SERIALIZE = 1, | 235 SERIALIZE = 1, |
| 236 }; | 236 }; |
| 237 | 237 |
| 238 // Verifies that a string field is valid UTF8, logging an error if not. | 238 // Verifies that a string field is valid UTF8, logging an error if not. |
| 239 // This function will not be called by newly generated protobuf code | 239 // This function will not be called by newly generated protobuf code |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 // int first. | 294 // int first. |
| 295 return WireFormatLite::WireTypeForFieldType( | 295 return WireFormatLite::WireTypeForFieldType( |
| 296 static_cast<WireFormatLite::FieldType>( | 296 static_cast<WireFormatLite::FieldType>( |
| 297 implicit_cast<int>(type))); | 297 implicit_cast<int>(type))); |
| 298 } | 298 } |
| 299 | 299 |
| 300 inline uint32 WireFormat::MakeTag(const FieldDescriptor* field) { | 300 inline uint32 WireFormat::MakeTag(const FieldDescriptor* field) { |
| 301 return WireFormatLite::MakeTag(field->number(), WireTypeForField(field)); | 301 return WireFormatLite::MakeTag(field->number(), WireTypeForField(field)); |
| 302 } | 302 } |
| 303 | 303 |
| 304 inline int WireFormat::TagSize(int field_number, FieldDescriptor::Type type) { | 304 inline size_t WireFormat::TagSize(int field_number, |
| 305 FieldDescriptor::Type type) { |
| 305 // Some compilers don't like enum -> enum casts, so we implicit_cast to | 306 // Some compilers don't like enum -> enum casts, so we implicit_cast to |
| 306 // int first. | 307 // int first. |
| 307 return WireFormatLite::TagSize(field_number, | 308 return WireFormatLite::TagSize(field_number, |
| 308 static_cast<WireFormatLite::FieldType>( | 309 static_cast<WireFormatLite::FieldType>( |
| 309 implicit_cast<int>(type))); | 310 implicit_cast<int>(type))); |
| 310 } | 311 } |
| 311 | 312 |
| 312 inline void WireFormat::VerifyUTF8String(const char* data, int size, | 313 inline void WireFormat::VerifyUTF8String(const char* data, int size, |
| 313 WireFormat::Operation op) { | 314 WireFormat::Operation op) { |
| 314 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 315 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
| 315 WireFormatLite::VerifyUtf8String( | 316 WireFormatLite::VerifyUtf8String( |
| 316 data, size, static_cast<WireFormatLite::Operation>(op), NULL); | 317 data, size, static_cast<WireFormatLite::Operation>(op), NULL); |
| 317 #else | 318 #else |
| 318 // Avoid the compiler warning about unsued variables. | 319 // Avoid the compiler warning about unused variables. |
| 319 (void)data; (void)size; (void)op; | 320 (void)data; (void)size; (void)op; |
| 320 #endif | 321 #endif |
| 321 } | 322 } |
| 322 | 323 |
| 323 inline void WireFormat::VerifyUTF8StringNamedField( | 324 inline void WireFormat::VerifyUTF8StringNamedField( |
| 324 const char* data, int size, WireFormat::Operation op, | 325 const char* data, int size, WireFormat::Operation op, |
| 325 const char* field_name) { | 326 const char* field_name) { |
| 326 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 327 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
| 327 WireFormatLite::VerifyUtf8String( | 328 WireFormatLite::VerifyUtf8String( |
| 328 data, size, static_cast<WireFormatLite::Operation>(op), field_name); | 329 data, size, static_cast<WireFormatLite::Operation>(op), field_name); |
| 329 #endif | 330 #endif |
| 330 } | 331 } |
| 331 | 332 |
| 332 | 333 |
| 333 } // namespace internal | 334 } // namespace internal |
| 334 } // namespace protobuf | 335 } // namespace protobuf |
| 335 | 336 |
| 336 } // namespace google | 337 } // namespace google |
| 337 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_H__ | 338 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_H__ |
| OLD | NEW |