GPBWireFormat.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.h"
  9. #import "GPBUtilities_PackagePrivate.h"
  10. enum {
  11. GPBWireFormatTagTypeBits = 3,
  12. GPBWireFormatTagTypeMask = 7 /* = (1 << GPBWireFormatTagTypeBits) - 1 */,
  13. };
  14. uint32_t GPBWireFormatMakeTag(uint32_t fieldNumber, GPBWireFormat wireType) {
  15. return (fieldNumber << GPBWireFormatTagTypeBits) | wireType;
  16. }
  17. GPBWireFormat GPBWireFormatGetTagWireType(uint32_t tag) {
  18. return (GPBWireFormat)(tag & GPBWireFormatTagTypeMask);
  19. }
  20. uint32_t GPBWireFormatGetTagFieldNumber(uint32_t tag) {
  21. return GPBLogicalRightShift32(tag, GPBWireFormatTagTypeBits);
  22. }
  23. BOOL GPBWireFormatIsValidTag(uint32_t tag) {
  24. uint32_t formatBits = (tag & GPBWireFormatTagTypeMask);
  25. // The valid GPBWireFormat* values are 0-5, anything else is not a valid tag.
  26. BOOL result = (formatBits <= 5);
  27. return result;
  28. }
  29. GPBWireFormat GPBWireFormatForType(GPBDataType type, BOOL isPacked) {
  30. if (isPacked) {
  31. return GPBWireFormatLengthDelimited;
  32. }
  33. static const GPBWireFormat format[GPBDataType_Count] = {
  34. GPBWireFormatVarint, // GPBDataTypeBool
  35. GPBWireFormatFixed32, // GPBDataTypeFixed32
  36. GPBWireFormatFixed32, // GPBDataTypeSFixed32
  37. GPBWireFormatFixed32, // GPBDataTypeFloat
  38. GPBWireFormatFixed64, // GPBDataTypeFixed64
  39. GPBWireFormatFixed64, // GPBDataTypeSFixed64
  40. GPBWireFormatFixed64, // GPBDataTypeDouble
  41. GPBWireFormatVarint, // GPBDataTypeInt32
  42. GPBWireFormatVarint, // GPBDataTypeInt64
  43. GPBWireFormatVarint, // GPBDataTypeSInt32
  44. GPBWireFormatVarint, // GPBDataTypeSInt64
  45. GPBWireFormatVarint, // GPBDataTypeUInt32
  46. GPBWireFormatVarint, // GPBDataTypeUInt64
  47. GPBWireFormatLengthDelimited, // GPBDataTypeBytes
  48. GPBWireFormatLengthDelimited, // GPBDataTypeString
  49. GPBWireFormatLengthDelimited, // GPBDataTypeMessage
  50. GPBWireFormatStartGroup, // GPBDataTypeGroup
  51. GPBWireFormatVarint // GPBDataTypeEnum
  52. };
  53. return format[type];
  54. }