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