1 | #region Copyright notice and license
|
---|
2 |
|
---|
3 | // Protocol Buffers - Google's data interchange format
|
---|
4 | // Copyright 2008 Google Inc. All rights reserved.
|
---|
5 | // http://github.com/jskeet/dotnet-protobufs/
|
---|
6 | // Original C++/Java/Python code:
|
---|
7 | // http://code.google.com/p/protobuf/
|
---|
8 | //
|
---|
9 | // Redistribution and use in source and binary forms, with or without
|
---|
10 | // modification, are permitted provided that the following conditions are
|
---|
11 | // met:
|
---|
12 | //
|
---|
13 | // * Redistributions of source code must retain the above copyright
|
---|
14 | // notice, this list of conditions and the following disclaimer.
|
---|
15 | // * Redistributions in binary form must reproduce the above
|
---|
16 | // copyright notice, this list of conditions and the following disclaimer
|
---|
17 | // in the documentation and/or other materials provided with the
|
---|
18 | // distribution.
|
---|
19 | // * Neither the name of Google Inc. nor the names of its
|
---|
20 | // contributors may be used to endorse or promote products derived from
|
---|
21 | // this software without specific prior written permission.
|
---|
22 | //
|
---|
23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
26 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
27 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
28 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
29 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
30 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
31 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
32 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
33 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
34 |
|
---|
35 | #endregion
|
---|
36 |
|
---|
37 | using System;
|
---|
38 | using Google.ProtocolBuffers.Descriptors;
|
---|
39 |
|
---|
40 | namespace Google.ProtocolBuffers
|
---|
41 | {
|
---|
42 | /// <summary>
|
---|
43 | /// This class is used internally by the Protocol Buffer Library and generated
|
---|
44 | /// message implementations. It is public only for the sake of those generated
|
---|
45 | /// messages. Others should not use this class directly.
|
---|
46 | /// <para>
|
---|
47 | /// This class contains constants and helper functions useful for dealing with
|
---|
48 | /// the Protocol Buffer wire format.
|
---|
49 | /// </para>
|
---|
50 | /// </summary>
|
---|
51 | public static class WireFormat
|
---|
52 | {
|
---|
53 | #region Fixed sizes.
|
---|
54 |
|
---|
55 | // TODO(jonskeet): Move these somewhere else. They're messy. Consider making FieldType a smarter kind of enum
|
---|
56 | public const int Fixed32Size = 4;
|
---|
57 | public const int Fixed64Size = 8;
|
---|
58 | public const int SFixed32Size = 4;
|
---|
59 | public const int SFixed64Size = 8;
|
---|
60 | public const int FloatSize = 4;
|
---|
61 | public const int DoubleSize = 8;
|
---|
62 | public const int BoolSize = 1;
|
---|
63 |
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | [CLSCompliant(false)]
|
---|
67 | public enum WireType : uint
|
---|
68 | {
|
---|
69 | Varint = 0,
|
---|
70 | Fixed64 = 1,
|
---|
71 | LengthDelimited = 2,
|
---|
72 | StartGroup = 3,
|
---|
73 | EndGroup = 4,
|
---|
74 | Fixed32 = 5
|
---|
75 | }
|
---|
76 |
|
---|
77 | internal static class MessageSetField
|
---|
78 | {
|
---|
79 | internal const int Item = 1;
|
---|
80 | internal const int TypeID = 2;
|
---|
81 | internal const int Message = 3;
|
---|
82 | }
|
---|
83 |
|
---|
84 | internal static class MessageSetTag
|
---|
85 | {
|
---|
86 | internal static readonly uint ItemStart = MakeTag(MessageSetField.Item, WireType.StartGroup);
|
---|
87 | internal static readonly uint ItemEnd = MakeTag(MessageSetField.Item, WireType.EndGroup);
|
---|
88 | internal static readonly uint TypeID = MakeTag(MessageSetField.TypeID, WireType.Varint);
|
---|
89 | internal static readonly uint Message = MakeTag(MessageSetField.Message, WireType.LengthDelimited);
|
---|
90 | }
|
---|
91 |
|
---|
92 | private const int TagTypeBits = 3;
|
---|
93 | private const uint TagTypeMask = (1 << TagTypeBits) - 1;
|
---|
94 |
|
---|
95 | /// <summary>
|
---|
96 | /// Given a tag value, determines the wire type (lower 3 bits).
|
---|
97 | /// </summary>
|
---|
98 | [CLSCompliant(false)]
|
---|
99 | public static WireType GetTagWireType(uint tag)
|
---|
100 | {
|
---|
101 | return (WireType) (tag & TagTypeMask);
|
---|
102 | }
|
---|
103 |
|
---|
104 | [CLSCompliant(false)]
|
---|
105 | public static bool IsEndGroupTag(uint tag)
|
---|
106 | {
|
---|
107 | return (WireType) (tag & TagTypeMask) == WireType.EndGroup;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /// <summary>
|
---|
111 | /// Given a tag value, determines the field number (the upper 29 bits).
|
---|
112 | /// </summary>
|
---|
113 | [CLSCompliant(false)]
|
---|
114 | public static int GetTagFieldNumber(uint tag)
|
---|
115 | {
|
---|
116 | return (int) tag >> TagTypeBits;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /// <summary>
|
---|
120 | /// Makes a tag value given a field number and wire type.
|
---|
121 | /// TODO(jonskeet): Should we just have a Tag structure?
|
---|
122 | /// </summary>
|
---|
123 | [CLSCompliant(false)]
|
---|
124 | public static uint MakeTag(int fieldNumber, WireType wireType)
|
---|
125 | {
|
---|
126 | return (uint) (fieldNumber << TagTypeBits) | (uint) wireType;
|
---|
127 | }
|
---|
128 |
|
---|
129 | #if !LITE
|
---|
130 | [CLSCompliant(false)]
|
---|
131 | public static uint MakeTag(FieldDescriptor field)
|
---|
132 | {
|
---|
133 | return MakeTag(field.FieldNumber, GetWireType(field));
|
---|
134 | }
|
---|
135 |
|
---|
136 | /// <summary>
|
---|
137 | /// Returns the wire type for the given field descriptor. This differs
|
---|
138 | /// from GetWireType(FieldType) for packed repeated fields.
|
---|
139 | /// </summary>
|
---|
140 | internal static WireType GetWireType(FieldDescriptor descriptor)
|
---|
141 | {
|
---|
142 | return descriptor.IsPacked ? WireType.LengthDelimited : GetWireType(descriptor.FieldType);
|
---|
143 | }
|
---|
144 |
|
---|
145 | #endif
|
---|
146 |
|
---|
147 | /// <summary>
|
---|
148 | /// Converts a field type to its wire type. Done with a switch for the sake
|
---|
149 | /// of speed - this is significantly faster than a dictionary lookup.
|
---|
150 | /// </summary>
|
---|
151 | [CLSCompliant(false)]
|
---|
152 | public static WireType GetWireType(FieldType fieldType)
|
---|
153 | {
|
---|
154 | switch (fieldType)
|
---|
155 | {
|
---|
156 | case FieldType.Double:
|
---|
157 | return WireType.Fixed64;
|
---|
158 | case FieldType.Float:
|
---|
159 | return WireType.Fixed32;
|
---|
160 | case FieldType.Int64:
|
---|
161 | case FieldType.UInt64:
|
---|
162 | case FieldType.Int32:
|
---|
163 | return WireType.Varint;
|
---|
164 | case FieldType.Fixed64:
|
---|
165 | return WireType.Fixed64;
|
---|
166 | case FieldType.Fixed32:
|
---|
167 | return WireType.Fixed32;
|
---|
168 | case FieldType.Bool:
|
---|
169 | return WireType.Varint;
|
---|
170 | case FieldType.String:
|
---|
171 | return WireType.LengthDelimited;
|
---|
172 | case FieldType.Group:
|
---|
173 | return WireType.StartGroup;
|
---|
174 | case FieldType.Message:
|
---|
175 | case FieldType.Bytes:
|
---|
176 | return WireType.LengthDelimited;
|
---|
177 | case FieldType.UInt32:
|
---|
178 | return WireType.Varint;
|
---|
179 | case FieldType.SFixed32:
|
---|
180 | return WireType.Fixed32;
|
---|
181 | case FieldType.SFixed64:
|
---|
182 | return WireType.Fixed64;
|
---|
183 | case FieldType.SInt32:
|
---|
184 | case FieldType.SInt64:
|
---|
185 | case FieldType.Enum:
|
---|
186 | return WireType.Varint;
|
---|
187 | default:
|
---|
188 | throw new ArgumentOutOfRangeException("No such field type");
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | } |
---|