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