1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // http://code.google.com/p/protobuf/ 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 (at) google.com (Kenton Varda) 32 // atenasio (at) google.com (Chris Atenasio) (ZigZag transform) 33 // wink (at) google.com (Wink Saville) (refactored from wire_format.h) 34 // Based on original Protocol Buffers design by 35 // Sanjay Ghemawat, Jeff Dean, and others. 36 // 37 // This header is logically internal, but is made public because it is used 38 // from protocol-compiler-generated code, which may reside in other components. 39 40 #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__ 41 #define GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__ 42 43 #include <string> 44 #include <google/protobuf/stubs/common.h> 45 #include <google/protobuf/message_lite.h> 46 #include <google/protobuf/io/coded_stream.h> // for CodedOutputStream::Varint32Size 47 48 namespace google { 49 50 namespace protobuf { 51 template <typename T> class RepeatedField; // repeated_field.h 52 class UnknownFieldSet; 53 } 54 55 namespace protobuf { 56 namespace internal { 57 58 class StringPieceField; 59 60 // This class is for internal use by the protocol buffer library and by 61 // protocol-complier-generated message classes. It must not be called 62 // directly by clients. 63 // 64 // This class contains helpers for implementing the binary protocol buffer 65 // wire format without the need for reflection. Use WireFormat when using 66 // reflection. 67 // 68 // This class is really a namespace that contains only static methods. 69 class LIBPROTOBUF_EXPORT WireFormatLite { 70 public: 71 72 // ----------------------------------------------------------------- 73 // Helper constants and functions related to the format. These are 74 // mostly meant for internal and generated code to use. 75 76 // The wire format is composed of a sequence of tag/value pairs, each 77 // of which contains the value of one field (or one element of a repeated 78 // field). Each tag is encoded as a varint. The lower bits of the tag 79 // identify its wire type, which specifies the format of the data to follow. 80 // The rest of the bits contain the field number. Each type of field (as 81 // declared by FieldDescriptor::Type, in descriptor.h) maps to one of 82 // these wire types. Immediately following each tag is the field's value, 83 // encoded in the format specified by the wire type. Because the tag 84 // identifies the encoding of this data, it is possible to skip 85 // unrecognized fields for forwards compatibility. 86 87 enum WireType { 88 WIRETYPE_VARINT = 0, 89 WIRETYPE_FIXED64 = 1, 90 WIRETYPE_LENGTH_DELIMITED = 2, 91 WIRETYPE_START_GROUP = 3, 92 WIRETYPE_END_GROUP = 4, 93 WIRETYPE_FIXED32 = 5, 94 }; 95 96 // Lite alternative to FieldDescriptor::Type. Must be kept in sync. 97 enum FieldType { 98 TYPE_DOUBLE = 1, 99 TYPE_FLOAT = 2, 100 TYPE_INT64 = 3, 101 TYPE_UINT64 = 4, 102 TYPE_INT32 = 5, 103 TYPE_FIXED64 = 6, 104 TYPE_FIXED32 = 7, 105 TYPE_BOOL = 8, 106 TYPE_STRING = 9, 107 TYPE_GROUP = 10, 108 TYPE_MESSAGE = 11, 109 TYPE_BYTES = 12, 110 TYPE_UINT32 = 13, 111 TYPE_ENUM = 14, 112 TYPE_SFIXED32 = 15, 113 TYPE_SFIXED64 = 16, 114 TYPE_SINT32 = 17, 115 TYPE_SINT64 = 18, 116 MAX_FIELD_TYPE = 18, 117 }; 118 119 // Lite alternative to FieldDescriptor::CppType. Must be kept in sync. 120 enum CppType { 121 CPPTYPE_INT32 = 1, 122 CPPTYPE_INT64 = 2, 123 CPPTYPE_UINT32 = 3, 124 CPPTYPE_UINT64 = 4, 125 CPPTYPE_DOUBLE = 5, 126 CPPTYPE_FLOAT = 6, 127 CPPTYPE_BOOL = 7, 128 CPPTYPE_ENUM = 8, 129 CPPTYPE_STRING = 9, 130 CPPTYPE_MESSAGE = 10, 131 MAX_CPPTYPE = 10, 132 }; 133 134 // Helper method to get the CppType for a particular Type. 135 static CppType FieldTypeToCppType(FieldType type); 136 137 // Given a FieldSescriptor::Type return its WireType 138 static inline WireFormatLite::WireType WireTypeForFieldType( 139 WireFormatLite::FieldType type) { 140 return kWireTypeForFieldType[type]; 141 } 142 143 // Number of bits in a tag which identify the wire type. 144 static const int kTagTypeBits = 3; 145 // Mask for those bits. 146 static const uint32 kTagTypeMask = (1 << kTagTypeBits) - 1; 147 148 // Helper functions for encoding and decoding tags. (Inlined below and in 149 // _inl.h) 150 // 151 // This is different from MakeTag(field->number(), field->type()) in the case 152 // of packed repeated fields. 153 static uint32 MakeTag(int field_number, WireType type); 154 static WireType GetTagWireType(uint32 tag); 155 static int GetTagFieldNumber(uint32 tag); 156 157 // Compute the byte size of a tag. For groups, this includes both the start 158 // and end tags. 159 static inline int TagSize(int field_number, WireFormatLite::FieldType type); 160 161 // ----------------------------------------------------------------- 162 // Helpers for dealing with unknown fields 163 164 // Skips a field value with the given tag. The input should start 165 // positioned immediately after the tag. Skipped values are simply discarded, 166 // not recorded anywhere. See WireFormat::SkipField() for a version that 167 // records to an UnknownFieldSet. 168 static bool SkipField(io::CodedInputStream* input, uint32 tag, 169 UnknownFieldSet *unknown_fields); 170 171 // Reads and ignores a message from the input. If unknown_fields is non-NULL, 172 // the contents will be added to it. 173 static bool SkipMessage(io::CodedInputStream* input, 174 UnknownFieldSet* unknown_fields); 175 176 177 // Reads and ignores a message from the input. Skipped values may be stored 178 // in the UnknownFieldSet if it exists. 179 static bool SkipMessage(io::CodedInputStream* input); 180 181 // This macro does the same thing as WireFormatLite::MakeTag(), but the 182 // result is usable as a compile-time constant, which makes it usable 183 // as a switch case or a template input. WireFormatLite::MakeTag() is more 184 // type-safe, though, so prefer it if possible. 185 #define GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(FIELD_NUMBER, TYPE) \ 186 static_cast<uint32>( \ 187 ((FIELD_NUMBER) << ::google::protobuf::internal::WireFormatLite::kTagTypeBits) \ 188 | (TYPE)) 189 190 // These are the tags for the old MessageSet format, which was defined as: 191 // message MessageSet { 192 // repeated group Item = 1 { 193 // required int32 type_id = 2; 194 // required string message = 3; 195 // } 196 // } 197 static const int kMessageSetItemNumber = 1; 198 static const int kMessageSetTypeIdNumber = 2; 199 static const int kMessageSetMessageNumber = 3; 200 static const int kMessageSetItemStartTag = 201 GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetItemNumber, 202 WireFormatLite::WIRETYPE_START_GROUP); 203 static const int kMessageSetItemEndTag = 204 GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetItemNumber, 205 WireFormatLite::WIRETYPE_END_GROUP); 206 static const int kMessageSetTypeIdTag = 207 GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetTypeIdNumber, 208 WireFormatLite::WIRETYPE_VARINT); 209 static const int kMessageSetMessageTag = 210 GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetMessageNumber, 211 WireFormatLite::WIRETYPE_LENGTH_DELIMITED); 212 213 // Byte size of all tags of a MessageSet::Item combined. 214 static const int kMessageSetItemTagsSize; 215 216 // Helper functions for converting between floats/doubles and IEEE-754 217 // uint32s/uint64s so that they can be written. (Assumes your platform 218 // uses IEEE-754 floats.) 219 static uint32 EncodeFloat(float value); 220 static float DecodeFloat(uint32 value); 221 static uint64 EncodeDouble(double value); 222 static double DecodeDouble(uint64 value); 223 224 // Helper functions for mapping signed integers to unsigned integers in 225 // such a way that numbers with small magnitudes will encode to smaller 226 // varints. If you simply static_cast a negative number to an unsigned 227 // number and varint-encode it, it will always take 10 bytes, defeating 228 // the purpose of varint. So, for the "sint32" and "sint64" field types, 229 // we ZigZag-encode the values. 230 static uint32 ZigZagEncode32(int32 n); 231 static int32 ZigZagDecode32(uint32 n); 232 static uint64 ZigZagEncode64(int64 n); 233 static int64 ZigZagDecode64(uint64 n); 234 235 // Write the contents of an UnknownFieldSet to the output. 236 static void SerializeUnknownFields(const UnknownFieldSet& unknown_fields, 237 io::CodedOutputStream* output); 238 // Same as above, except writing directly to the provided buffer. 239 // Requires that the buffer have sufficient capacity for 240 // ComputeUnknownFieldsSize(unknown_fields). 241 // 242 // Returns a pointer past the last written byte. 243 static uint8* SerializeUnknownFieldsToArray( 244 const UnknownFieldSet& unknown_fields, 245 uint8* target); 246 247 // Same thing except for messages that have the message_set_wire_format 248 // option. 249 static void SerializeUnknownMessageSetItems( 250 const UnknownFieldSet& unknown_fields, 251 io::CodedOutputStream* output); 252 // Same as above, except writing directly to the provided buffer. 253 // Requires that the buffer have sufficient capacity for 254 // ComputeUnknownMessageSetItemsSize(unknown_fields). 255 // 256 // Returns a pointer past the last written byte. 257 static uint8* SerializeUnknownMessageSetItemsToArray( 258 const UnknownFieldSet& unknown_fields, 259 uint8* target); 260 261 // Compute the size of the UnknownFieldSet on the wire. 262 static int ComputeUnknownFieldsSize(const UnknownFieldSet& unknown_fields); 263 264 // Same thing except for messages that have the message_set_wire_format 265 // option. 266 static int ComputeUnknownMessageSetItemsSize( 267 const UnknownFieldSet& unknown_fields); 268 269 // ================================================================= 270 // Methods for reading/writing individual field. The implementations 271 // of these methods are defined in wire_format_lite_inl.h; you must #include 272 // that file to use these. 273 274 // Avoid ugly line wrapping 275 #define input io::CodedInputStream* input 276 #define output io::CodedOutputStream* output 277 #define field_number int field_number 278 #define INL GOOGLE_ATTRIBUTE_ALWAYS_INLINE 279 280 // Read fields, not including tags. The assumption is that you already 281 // read the tag to determine what field to read. 282 283 // For primitive fields, we just use a templatized routine parameterized by 284 // the represented type and the FieldType. These are specialized with the 285 // appropriate definition for each declared type. 286 template <typename CType, enum FieldType DeclaredType> 287 static inline bool ReadPrimitive(input, CType* value) INL; 288 289 // Reads repeated primitive values, with optimizations for repeats. 290 // tag_size and tag should both be compile-time constants provided by the 291 // protocol compiler. 292 template <typename CType, enum FieldType DeclaredType> 293 static inline bool ReadRepeatedPrimitive(int tag_size, 294 uint32 tag, 295 input, 296 RepeatedField<CType>* value) INL; 297 298 // Identical to ReadRepeatedPrimitive, except will not inline the 299 // implementation. 300 template <typename CType, enum FieldType DeclaredType> 301 static bool ReadRepeatedPrimitiveNoInline(int tag_size, 302 uint32 tag, 303 input, 304 RepeatedField<CType>* value); 305 306 // Reads a primitive value directly from the provided buffer. It returns a 307 // pointer past the segment of data that was read. 308 // 309 // This is only implemented for the types with fixed wire size, e.g. 310 // float, double, and the (s)fixed* types. 311 template <typename CType, enum FieldType DeclaredType> 312 static inline const uint8* ReadPrimitiveFromArray(const uint8* buffer, 313 CType* value) INL; 314 315 // Reads a primitive packed field. 316 // 317 // This is only implemented for packable types. 318 template <typename CType, enum FieldType DeclaredType> 319 static inline bool ReadPackedPrimitive(input, 320 RepeatedField<CType>* value) INL; 321 322 // Identical to ReadPackedPrimitive, except will not inline the 323 // implementation. 324 template <typename CType, enum FieldType DeclaredType> 325 static bool ReadPackedPrimitiveNoInline(input, RepeatedField<CType>* value); 326 327 // Read a packed enum field. Values for which is_valid() returns false are 328 // dropped. 329 static bool ReadPackedEnumNoInline(input, 330 bool (*is_valid)(int), 331 RepeatedField<int>* value); 332 333 static bool ReadString(input, string* value); 334 static bool ReadBytes (input, string* value); 335 336 static inline bool ReadGroup (field_number, input, MessageLite* value); 337 static inline bool ReadMessage(input, MessageLite* value); 338 339 // Like above, but de-virtualize the call to MergePartialFromCodedStream(). 340 // The pointer must point at an instance of MessageType, *not* a subclass (or 341 // the subclass must not override MergePartialFromCodedStream()). 342 template<typename MessageType> 343 static inline bool ReadGroupNoVirtual(field_number, input, 344 MessageType* value); 345 template<typename MessageType> 346 static inline bool ReadMessageNoVirtual(input, MessageType* value); 347 348 // Write a tag. The Write*() functions typically include the tag, so 349 // normally there's no need to call this unless using the Write*NoTag() 350 // variants. 351 static inline void WriteTag(field_number, WireType type, output) INL; 352 353 // Write fields, without tags. 354 static inline void WriteInt32NoTag (int32 value, output) INL; 355 static inline void WriteInt64NoTag (int64 value, output) INL; 356 static inline void WriteUInt32NoTag (uint32 value, output) INL; 357 static inline void WriteUInt64NoTag (uint64 value, output) INL; 358 static inline void WriteSInt32NoTag (int32 value, output) INL; 359 static inline void WriteSInt64NoTag (int64 value, output) INL; 360 static inline void WriteFixed32NoTag (uint32 value, output) INL; 361 static inline void WriteFixed64NoTag (uint64 value, output) INL; 362 static inline void WriteSFixed32NoTag(int32 value, output) INL; 363 static inline void WriteSFixed64NoTag(int64 value, output) INL; 364 static inline void WriteFloatNoTag (float value, output) INL; 365 static inline void WriteDoubleNoTag (double value, output) INL; 366 static inline void WriteBoolNoTag (bool value, output) INL; 367 static inline void WriteEnumNoTag (int value, output) INL; 368 369 // Write fields, including tags. 370 static void WriteInt32 (field_number, int32 value, output); 371 static void WriteInt64 (field_number, int64 value, output); 372 static void WriteUInt32 (field_number, uint32 value, output); 373 static void WriteUInt64 (field_number, uint64 value, output); 374 static void WriteSInt32 (field_number, int32 value, output); 375 static void WriteSInt64 (field_number, int64 value, output); 376 static void WriteFixed32 (field_number, uint32 value, output); 377 static void WriteFixed64 (field_number, uint64 value, output); 378 static void WriteSFixed32(field_number, int32 value, output); 379 static void WriteSFixed64(field_number, int64 value, output); 380 static void WriteFloat (field_number, float value, output); 381 static void WriteDouble (field_number, double value, output); 382 static void WriteBool (field_number, bool value, output); 383 static void WriteEnum (field_number, int value, output); 384 385 static void WriteString(field_number, const string& value, output); 386 static void WriteBytes (field_number, const string& value, output); 387 388 static void WriteGroup( 389 field_number, const MessageLite& value, output); 390 static void WriteMessage( 391 field_number, const MessageLite& value, output); 392 // Like above, but these will check if the output stream has enough 393 // space to write directly to a flat array. 394 static void WriteGroupMaybeToArray( 395 field_number, const MessageLite& value, output); 396 static void WriteMessageMaybeToArray( 397 field_number, const MessageLite& value, output); 398 399 // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The 400 // pointer must point at an instance of MessageType, *not* a subclass (or 401 // the subclass must not override SerializeWithCachedSizes()). 402 template<typename MessageType> 403 static inline void WriteGroupNoVirtual( 404 field_number, const MessageType& value, output); 405 template<typename MessageType> 406 static inline void WriteMessageNoVirtual( 407 field_number, const MessageType& value, output); 408 409 #undef output 410 #define output uint8* target 411 412 // Like above, but use only *ToArray methods of CodedOutputStream. 413 static inline uint8* WriteTagToArray(field_number, WireType type, output) INL; 414 415 // Write fields, without tags. 416 static inline uint8* WriteInt32NoTagToArray (int32 value, output) INL; 417 static inline uint8* WriteInt64NoTagToArray (int64 value, output) INL; 418 static inline uint8* WriteUInt32NoTagToArray (uint32 value, output) INL; 419 static inline uint8* WriteUInt64NoTagToArray (uint64 value, output) INL; 420 static inline uint8* WriteSInt32NoTagToArray (int32 value, output) INL; 421 static inline uint8* WriteSInt64NoTagToArray (int64 value, output) INL; 422 static inline uint8* WriteFixed32NoTagToArray (uint32 value, output) INL; 423 static inline uint8* WriteFixed64NoTagToArray (uint64 value, output) INL; 424 static inline uint8* WriteSFixed32NoTagToArray(int32 value, output) INL; 425 static inline uint8* WriteSFixed64NoTagToArray(int64 value, output) INL; 426 static inline uint8* WriteFloatNoTagToArray (float value, output) INL; 427 static inline uint8* WriteDoubleNoTagToArray (double value, output) INL; 428 static inline uint8* WriteBoolNoTagToArray (bool value, output) INL; 429 static inline uint8* WriteEnumNoTagToArray (int value, output) INL; 430 431 // Write fields, including tags. 432 static inline uint8* WriteInt32ToArray( 433 field_number, int32 value, output) INL; 434 static inline uint8* WriteInt64ToArray( 435 field_number, int64 value, output) INL; 436 static inline uint8* WriteUInt32ToArray( 437 field_number, uint32 value, output) INL; 438 static inline uint8* WriteUInt64ToArray( 439 field_number, uint64 value, output) INL; 440 static inline uint8* WriteSInt32ToArray( 441 field_number, int32 value, output) INL; 442 static inline uint8* WriteSInt64ToArray( 443 field_number, int64 value, output) INL; 444 static inline uint8* WriteFixed32ToArray( 445 field_number, uint32 value, output) INL; 446 static inline uint8* WriteFixed64ToArray( 447 field_number, uint64 value, output) INL; 448 static inline uint8* WriteSFixed32ToArray( 449 field_number, int32 value, output) INL; 450 static inline uint8* WriteSFixed64ToArray( 451 field_number, int64 value, output) INL; 452 static inline uint8* WriteFloatToArray( 453 field_number, float value, output) INL; 454 static inline uint8* WriteDoubleToArray( 455 field_number, double value, output) INL; 456 static inline uint8* WriteBoolToArray( 457 field_number, bool value, output) INL; 458 static inline uint8* WriteEnumToArray( 459 field_number, int value, output) INL; 460 461 static inline uint8* WriteStringToArray( 462 field_number, const string& value, output) INL; 463 static inline uint8* WriteBytesToArray( 464 field_number, const string& value, output) INL; 465 466 static inline uint8* WriteGroupToArray( 467 field_number, const MessageLite& value, output) INL; 468 static inline uint8* WriteMessageToArray( 469 field_number, const MessageLite& value, output) INL; 470 471 // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The 472 // pointer must point at an instance of MessageType, *not* a subclass (or 473 // the subclass must not override SerializeWithCachedSizes()). 474 template<typename MessageType> 475 static inline uint8* WriteGroupNoVirtualToArray( 476 field_number, const MessageType& value, output) INL; 477 template<typename MessageType> 478 static inline uint8* WriteMessageNoVirtualToArray( 479 field_number, const MessageType& value, output) INL; 480 481 #undef output 482 #undef input 483 #undef INL 484 485 #undef field_number 486 487 // Compute the byte size of a field. The XxSize() functions do NOT include 488 // the tag, so you must also call TagSize(). (This is because, for repeated 489 // fields, you should only call TagSize() once and multiply it by the element 490 // count, but you may have to call XxSize() for each individual element.) 491 static inline int Int32Size ( int32 value); 492 static inline int Int64Size ( int64 value); 493 static inline int UInt32Size (uint32 value); 494 static inline int UInt64Size (uint64 value); 495 static inline int SInt32Size ( int32 value); 496 static inline int SInt64Size ( int64 value); 497 static inline int EnumSize ( int value); 498 499 // These types always have the same size. 500 static const int kFixed32Size = 4; 501 static const int kFixed64Size = 8; 502 static const int kSFixed32Size = 4; 503 static const int kSFixed64Size = 8; 504 static const int kFloatSize = 4; 505 static const int kDoubleSize = 8; 506 static const int kBoolSize = 1; 507 508 static inline int StringSize(const string& value); 509 static inline int BytesSize (const string& value); 510 511 static inline int GroupSize (const MessageLite& value); 512 static inline int MessageSize(const MessageLite& value); 513 514 // Like above, but de-virtualize the call to ByteSize(). The 515 // pointer must point at an instance of MessageType, *not* a subclass (or 516 // the subclass must not override ByteSize()). 517 template<typename MessageType> 518 static inline int GroupSizeNoVirtual (const MessageType& value); 519 template<typename MessageType> 520 static inline int MessageSizeNoVirtual(const MessageType& value); 521 522 // Given the length of data, calculate the byte size of the data on the 523 // wire if we encode the data as a length delimited field. 524 static inline int LengthDelimitedSize(int length); 525 526 private: 527 // A helper method for the repeated primitive reader. This method has 528 // optimizations for primitive types that have fixed size on the wire, and 529 // can be read using potentially faster paths. 530 template <typename CType, enum FieldType DeclaredType> 531 static inline bool ReadRepeatedFixedSizePrimitive( 532 int tag_size, 533 uint32 tag, 534 google::protobuf::io::CodedInputStream* input, 535 RepeatedField<CType>* value) GOOGLE_ATTRIBUTE_ALWAYS_INLINE; 536 537 static const CppType kFieldTypeToCppTypeMap[]; 538 static const WireFormatLite::WireType kWireTypeForFieldType[]; 539 540 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormatLite); 541 }; 542 543 // A class which deals with unknown values by saving them to an UnknownFieldSet. 544 class LIBPROTOBUF_EXPORT FieldSkipper { 545 public: 546 FieldSkipper(UnknownFieldSet* unknown_fields) 547 : unknown_fields_(unknown_fields) {} 548 549 // Skip a field whose tag has already been consumed. 550 bool SkipField(io::CodedInputStream* input, uint32 tag); 551 552 // Skip an entire message or group, up to an end-group tag (which is consumed) 553 // or end-of-stream. 554 bool SkipMessage(io::CodedInputStream* input); 555 556 // Deal with an already-parsed unrecognized enum value. The default 557 // implementation does nothing, but the UnknownFieldSet-based implementation 558 // saves it as an unknown varint. 559 void SkipUnknownEnum(int field_number, int value); 560 561 protected: 562 UnknownFieldSet* unknown_fields_; 563 }; 564 565 // inline methods ==================================================== 566 567 inline WireFormatLite::CppType 568 WireFormatLite::FieldTypeToCppType(FieldType type) { 569 return kFieldTypeToCppTypeMap[type]; 570 } 571 572 inline uint32 WireFormatLite::MakeTag(int field_number, WireType type) { 573 return GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(field_number, type); 574 } 575 576 inline WireFormatLite::WireType WireFormatLite::GetTagWireType(uint32 tag) { 577 return static_cast<WireType>(tag & kTagTypeMask); 578 } 579 580 inline int WireFormatLite::GetTagFieldNumber(uint32 tag) { 581 return static_cast<int>(tag >> kTagTypeBits); 582 } 583 584 inline int WireFormatLite::TagSize(int field_number, 585 WireFormatLite::FieldType type) { 586 int result = io::CodedOutputStream::VarintSize32( 587 field_number << kTagTypeBits); 588 if (type == TYPE_GROUP) { 589 // Groups have both a start and an end tag. 590 return result * 2; 591 } else { 592 return result; 593 } 594 } 595 596 inline uint32 WireFormatLite::EncodeFloat(float value) { 597 union {float f; uint32 i;}; 598 f = value; 599 return i; 600 } 601 602 inline float WireFormatLite::DecodeFloat(uint32 value) { 603 union {float f; uint32 i;}; 604 i = value; 605 return f; 606 } 607 608 inline uint64 WireFormatLite::EncodeDouble(double value) { 609 union {double f; uint64 i;}; 610 f = value; 611 return i; 612 } 613 614 inline double WireFormatLite::DecodeDouble(uint64 value) { 615 union {double f; uint64 i;}; 616 i = value; 617 return f; 618 } 619 620 // ZigZag Transform: Encodes signed integers so that they can be 621 // effectively used with varint encoding. 622 // 623 // varint operates on unsigned integers, encoding smaller numbers into 624 // fewer bytes. If you try to use it on a signed integer, it will treat 625 // this number as a very large unsigned integer, which means that even 626 // small signed numbers like -1 will take the maximum number of bytes 627 // (10) to encode. ZigZagEncode() maps signed integers to unsigned 628 // in such a way that those with a small absolute value will have smaller 629 // encoded values, making them appropriate for encoding using varint. 630 // 631 // int32 -> uint32 632 // ------------------------- 633 // 0 -> 0 634 // -1 -> 1 635 // 1 -> 2 636 // -2 -> 3 637 // ... -> ... 638 // 2147483647 -> 4294967294 639 // -2147483648 -> 4294967295 640 // 641 // >> encode >> 642 // << decode << 643 644 inline uint32 WireFormatLite::ZigZagEncode32(int32 n) { 645 // Note: the right-shift must be arithmetic 646 return (n << 1) ^ (n >> 31); 647 } 648 649 inline int32 WireFormatLite::ZigZagDecode32(uint32 n) { 650 return (n >> 1) ^ -static_cast<int32>(n & 1); 651 } 652 653 inline uint64 WireFormatLite::ZigZagEncode64(int64 n) { 654 // Note: the right-shift must be arithmetic 655 return (n << 1) ^ (n >> 63); 656 } 657 658 inline int64 WireFormatLite::ZigZagDecode64(uint64 n) { 659 return (n >> 1) ^ -static_cast<int64>(n & 1); 660 } 661 662 } // namespace internal 663 } // namespace protobuf 664 665 } // namespace google 666 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__ 667