• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8 #endregion
9 
10 namespace Google.Protobuf
11 {
12     /// <summary>
13     /// This class is used internally by the Protocol Buffer Library and generated
14     /// message implementations. It is public only for the sake of those generated
15     /// messages. Others should not use this class directly.
16     /// <para>
17     /// This class contains constants and helper functions useful for dealing with
18     /// the Protocol Buffer wire format.
19     /// </para>
20     /// </summary>
21     public static class WireFormat
22     {
23         /// <summary>
24         /// Wire types within protobuf encoding.
25         /// </summary>
26         public enum WireType : uint
27         {
28             /// <summary>
29             /// Variable-length integer.
30             /// </summary>
31             Varint = 0,
32             /// <summary>
33             /// A fixed-length 64-bit value.
34             /// </summary>
35             Fixed64 = 1,
36             /// <summary>
37             /// A length-delimited value, i.e. a length followed by that many bytes of data.
38             /// </summary>
39             LengthDelimited = 2,
40             /// <summary>
41             /// A "start group" value
42             /// </summary>
43             StartGroup = 3,
44             /// <summary>
45             /// An "end group" value
46             /// </summary>
47             EndGroup = 4,
48             /// <summary>
49             /// A fixed-length 32-bit value.
50             /// </summary>
51             Fixed32 = 5
52         }
53 
54         private const int TagTypeBits = 3;
55         private const uint TagTypeMask = (1 << TagTypeBits) - 1;
56 
57         /// <summary>
58         /// Given a tag value, determines the wire type (lower 3 bits).
59         /// </summary>
GetTagWireType(uint tag)60         public static WireType GetTagWireType(uint tag)
61         {
62             return (WireType) (tag & TagTypeMask);
63         }
64 
65         /// <summary>
66         /// Given a tag value, determines the field number (the upper 29 bits).
67         /// </summary>
GetTagFieldNumber(uint tag)68         public static int GetTagFieldNumber(uint tag)
69         {
70             return (int) (tag >> TagTypeBits);
71         }
72 
73         /// <summary>
74         /// Makes a tag value given a field number and wire type.
75         /// </summary>
MakeTag(int fieldNumber, WireType wireType)76         public static uint MakeTag(int fieldNumber, WireType wireType)
77         {
78             return (uint) (fieldNumber << TagTypeBits) | (uint) wireType;
79         }
80     }
81 }