GPBWireFormat.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file or at
  6. // https://developers.google.com/open-source/licenses/bsd
  7. #import "GPBWireFormat.h"
  8. #import "GPBUtilities_PackagePrivate.h"
  9. enum {
  10. GPBWireFormatTagTypeBits = 3,
  11. GPBWireFormatTagTypeMask = 7 /* = (1 << GPBWireFormatTagTypeBits) - 1 */,
  12. };
  13. uint32_t GPBWireFormatMakeTag(uint32_t fieldNumber, GPBWireFormat wireType) {
  14. return (fieldNumber << GPBWireFormatTagTypeBits) | wireType;
  15. }
  16. GPBWireFormat GPBWireFormatGetTagWireType(uint32_t tag) {
  17. return (GPBWireFormat)(tag & GPBWireFormatTagTypeMask);
  18. }
  19. uint32_t GPBWireFormatGetTagFieldNumber(uint32_t tag) {
  20. return GPBLogicalRightShift32(tag, GPBWireFormatTagTypeBits);
  21. }
  22. BOOL GPBWireFormatIsValidTag(uint32_t tag) {
  23. uint32_t formatBits = (tag & GPBWireFormatTagTypeMask);
  24. // The valid GPBWireFormat* values are 0-5, anything else is not a valid tag.
  25. BOOL result = (formatBits <= 5);
  26. return result;
  27. }
  28. GPBWireFormat GPBWireFormatForType(GPBDataType type, BOOL isPacked) {
  29. if (isPacked) {
  30. return GPBWireFormatLengthDelimited;
  31. }
  32. static const GPBWireFormat format[GPBDataType_Count] = {
  33. GPBWireFormatVarint, // GPBDataTypeBool
  34. GPBWireFormatFixed32, // GPBDataTypeFixed32
  35. GPBWireFormatFixed32, // GPBDataTypeSFixed32
  36. GPBWireFormatFixed32, // GPBDataTypeFloat
  37. GPBWireFormatFixed64, // GPBDataTypeFixed64
  38. GPBWireFormatFixed64, // GPBDataTypeSFixed64
  39. GPBWireFormatFixed64, // GPBDataTypeDouble
  40. GPBWireFormatVarint, // GPBDataTypeInt32
  41. GPBWireFormatVarint, // GPBDataTypeInt64
  42. GPBWireFormatVarint, // GPBDataTypeSInt32
  43. GPBWireFormatVarint, // GPBDataTypeSInt64
  44. GPBWireFormatVarint, // GPBDataTypeUInt32
  45. GPBWireFormatVarint, // GPBDataTypeUInt64
  46. GPBWireFormatLengthDelimited, // GPBDataTypeBytes
  47. GPBWireFormatLengthDelimited, // GPBDataTypeString
  48. GPBWireFormatLengthDelimited, // GPBDataTypeMessage
  49. GPBWireFormatStartGroup, // GPBDataTypeGroup
  50. GPBWireFormatVarint // GPBDataTypeEnum
  51. };
  52. return format[type];
  53. }