1#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc.  All rights reserved.
4// https://developers.google.com/protocol-buffers/
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10//     * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12//     * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16//     * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31#endregion
32
33namespace Google.Protobuf
34{
35    /// <summary>
36    /// This class is used internally by the Protocol Buffer Library and generated
37    /// message implementations. It is public only for the sake of those generated
38    /// messages. Others should not use this class directly.
39    /// <para>
40    /// This class contains constants and helper functions useful for dealing with
41    /// the Protocol Buffer wire format.
42    /// </para>
43    /// </summary>
44    public static class WireFormat
45    {
46        /// <summary>
47        /// Wire types within protobuf encoding.
48        /// </summary>
49        public enum WireType : uint
50        {
51            /// <summary>
52            /// Variable-length integer.
53            /// </summary>
54            Varint = 0,
55            /// <summary>
56            /// A fixed-length 64-bit value.
57            /// </summary>
58            Fixed64 = 1,
59            /// <summary>
60            /// A length-delimited value, i.e. a length followed by that many bytes of data.
61            /// </summary>
62            LengthDelimited = 2,
63            /// <summary>
64            /// A "start group" value - not supported by this implementation.
65            /// </summary>
66            StartGroup = 3,
67            /// <summary>
68            /// An "end group" value - not supported by this implementation.
69            /// </summary>
70            EndGroup = 4,
71            /// <summary>
72            /// A fixed-length 32-bit value.
73            /// </summary>
74            Fixed32 = 5
75        }
76
77        private const int TagTypeBits = 3;
78        private const uint TagTypeMask = (1 << TagTypeBits) - 1;
79
80        /// <summary>
81        /// Given a tag value, determines the wire type (lower 3 bits).
82        /// </summary>
83        public static WireType GetTagWireType(uint tag)
84        {
85            return (WireType) (tag & TagTypeMask);
86        }
87
88        /// <summary>
89        /// Given a tag value, determines the field number (the upper 29 bits).
90        /// </summary>
91        public static int GetTagFieldNumber(uint tag)
92        {
93            return (int) tag >> TagTypeBits;
94        }
95
96        /// <summary>
97        /// Makes a tag value given a field number and wire type.
98        /// </summary>
99        public static uint MakeTag(int fieldNumber, WireType wireType)
100        {
101            return (uint) (fieldNumber << TagTypeBits) | (uint) wireType;
102        }
103    }
104}