blob: 8ebf3e8442c4c233a0e6cca56119110020e10c0a [file] [log] [blame]
Thomas Van Lenten30650d82015-05-01 08:57:16 -04001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
Thomas Van Lenten30650d82015-05-01 08:57:16 -04003//
Joshua Haberman44bd65b2023-09-08 17:43:14 -07004// 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
Thomas Van Lenten30650d82015-05-01 08:57:16 -04007
8#import "GPBWireFormat.h"
9
10#import "GPBUtilities_PackagePrivate.h"
11
12enum {
13 GPBWireFormatTagTypeBits = 3,
14 GPBWireFormatTagTypeMask = 7 /* = (1 << GPBWireFormatTagTypeBits) - 1 */,
15};
16
17uint32_t GPBWireFormatMakeTag(uint32_t fieldNumber, GPBWireFormat wireType) {
18 return (fieldNumber << GPBWireFormatTagTypeBits) | wireType;
19}
20
21GPBWireFormat GPBWireFormatGetTagWireType(uint32_t tag) {
22 return (GPBWireFormat)(tag & GPBWireFormatTagTypeMask);
23}
24
25uint32_t GPBWireFormatGetTagFieldNumber(uint32_t tag) {
26 return GPBLogicalRightShift32(tag, GPBWireFormatTagTypeBits);
27}
28
Thomas Van Lentenc18aa772016-06-29 09:51:13 -040029BOOL GPBWireFormatIsValidTag(uint32_t tag) {
30 uint32_t formatBits = (tag & GPBWireFormatTagTypeMask);
31 // The valid GPBWireFormat* values are 0-5, anything else is not a valid tag.
32 BOOL result = (formatBits <= 5);
33 return result;
34}
35
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040036GPBWireFormat GPBWireFormatForType(GPBDataType type, BOOL isPacked) {
Thomas Van Lenten30650d82015-05-01 08:57:16 -040037 if (isPacked) {
38 return GPBWireFormatLengthDelimited;
39 }
40
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040041 static const GPBWireFormat format[GPBDataType_Count] = {
42 GPBWireFormatVarint, // GPBDataTypeBool
43 GPBWireFormatFixed32, // GPBDataTypeFixed32
44 GPBWireFormatFixed32, // GPBDataTypeSFixed32
45 GPBWireFormatFixed32, // GPBDataTypeFloat
46 GPBWireFormatFixed64, // GPBDataTypeFixed64
47 GPBWireFormatFixed64, // GPBDataTypeSFixed64
48 GPBWireFormatFixed64, // GPBDataTypeDouble
49 GPBWireFormatVarint, // GPBDataTypeInt32
50 GPBWireFormatVarint, // GPBDataTypeInt64
51 GPBWireFormatVarint, // GPBDataTypeSInt32
52 GPBWireFormatVarint, // GPBDataTypeSInt64
53 GPBWireFormatVarint, // GPBDataTypeUInt32
54 GPBWireFormatVarint, // GPBDataTypeUInt64
55 GPBWireFormatLengthDelimited, // GPBDataTypeBytes
56 GPBWireFormatLengthDelimited, // GPBDataTypeString
57 GPBWireFormatLengthDelimited, // GPBDataTypeMessage
58 GPBWireFormatStartGroup, // GPBDataTypeGroup
59 GPBWireFormatVarint // GPBDataTypeEnum
Thomas Van Lenten30650d82015-05-01 08:57:16 -040060 };
61 return format[type];
62}