Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 3 | // |
Joshua Haberman | 44bd65b | 2023-09-08 17:43:14 -0700 | [diff] [blame] | 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 |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 7 | |
| 8 | #import "GPBWireFormat.h" |
| 9 | |
| 10 | #import "GPBUtilities_PackagePrivate.h" |
| 11 | |
| 12 | enum { |
| 13 | GPBWireFormatTagTypeBits = 3, |
| 14 | GPBWireFormatTagTypeMask = 7 /* = (1 << GPBWireFormatTagTypeBits) - 1 */, |
| 15 | }; |
| 16 | |
| 17 | uint32_t GPBWireFormatMakeTag(uint32_t fieldNumber, GPBWireFormat wireType) { |
| 18 | return (fieldNumber << GPBWireFormatTagTypeBits) | wireType; |
| 19 | } |
| 20 | |
| 21 | GPBWireFormat GPBWireFormatGetTagWireType(uint32_t tag) { |
| 22 | return (GPBWireFormat)(tag & GPBWireFormatTagTypeMask); |
| 23 | } |
| 24 | |
| 25 | uint32_t GPBWireFormatGetTagFieldNumber(uint32_t tag) { |
| 26 | return GPBLogicalRightShift32(tag, GPBWireFormatTagTypeBits); |
| 27 | } |
| 28 | |
Thomas Van Lenten | c18aa77 | 2016-06-29 09:51:13 -0400 | [diff] [blame] | 29 | BOOL 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 Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 36 | GPBWireFormat GPBWireFormatForType(GPBDataType type, BOOL isPacked) { |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 37 | if (isPacked) { |
| 38 | return GPBWireFormatLengthDelimited; |
| 39 | } |
| 40 | |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 41 | 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 Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 60 | }; |
| 61 | return format[type]; |
| 62 | } |