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