blob: faf1e715ef3185daa5202ba774e4cc7bf338b122 [file] [log] [blame]
csharptestd965c662011-05-20 13:57:07 -05001#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc. All rights reserved.
Jon Skeetee835a32015-06-30 17:22:26 +01004// https://developers.google.com/protocol-buffers/
csharptestd965c662011-05-20 13:57:07 -05005//
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
Jon Skeete38294a2015-06-09 19:30:44 +010033namespace Google.Protobuf
csharptest71f662c2011-05-20 15:15:34 -050034{
csharptestd965c662011-05-20 13:57:07 -050035 /// <summary>
csharptest71f662c2011-05-20 15:15:34 -050036 /// 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>
csharptestd965c662011-05-20 13:57:07 -050043 /// </summary>
csharptest71f662c2011-05-20 15:15:34 -050044 public static class WireFormat
45 {
Jon Skeet811fc892015-08-04 15:58:39 +010046 /// <summary>
47 /// Wire types within protobuf encoding.
48 /// </summary>
csharptest71f662c2011-05-20 15:15:34 -050049 public enum WireType : uint
50 {
Jon Skeet811fc892015-08-04 15:58:39 +010051 /// <summary>
52 /// Variable-length integer.
53 /// </summary>
csharptest71f662c2011-05-20 15:15:34 -050054 Varint = 0,
Jon Skeet811fc892015-08-04 15:58:39 +010055 /// <summary>
56 /// A fixed-length 64-bit value.
57 /// </summary>
csharptest71f662c2011-05-20 15:15:34 -050058 Fixed64 = 1,
Jon Skeet811fc892015-08-04 15:58:39 +010059 /// <summary>
60 /// A length-delimited value, i.e. a length followed by that many bytes of data.
61 /// </summary>
csharptest71f662c2011-05-20 15:15:34 -050062 LengthDelimited = 2,
Jon Skeet811fc892015-08-04 15:58:39 +010063 /// <summary>
64 /// A "start group" value - not supported by this implementation.
65 /// </summary>
csharptest71f662c2011-05-20 15:15:34 -050066 StartGroup = 3,
Jon Skeet811fc892015-08-04 15:58:39 +010067 /// <summary>
68 /// An "end group" value - not supported by this implementation.
69 /// </summary>
csharptest71f662c2011-05-20 15:15:34 -050070 EndGroup = 4,
Jon Skeet811fc892015-08-04 15:58:39 +010071 /// <summary>
72 /// A fixed-length 32-bit value.
73 /// </summary>
csharptest71f662c2011-05-20 15:15:34 -050074 Fixed32 = 5
75 }
Jon Skeetf34d37a2015-06-30 13:16:20 +010076
csharptest71f662c2011-05-20 15:15:34 -050077 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>
csharptest71f662c2011-05-20 15:15:34 -050083 public static WireType GetTagWireType(uint tag)
84 {
85 return (WireType) (tag & TagTypeMask);
86 }
87
Jon Skeet811fc892015-08-04 15:58:39 +010088 /// <summary>
csharptest71f662c2011-05-20 15:15:34 -050089 /// Given a tag value, determines the field number (the upper 29 bits).
90 /// </summary>
csharptest71f662c2011-05-20 15:15:34 -050091 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.
csharptest71f662c2011-05-20 15:15:34 -050098 /// </summary>
csharptest71f662c2011-05-20 15:15:34 -050099 public static uint MakeTag(int fieldNumber, WireType wireType)
100 {
101 return (uint) (fieldNumber << TagTypeBits) | (uint) wireType;
Jon Skeet811fc892015-08-04 15:58:39 +0100102 }
csharptest71f662c2011-05-20 15:15:34 -0500103 }
104}