blob: 2bd0a5aa8a6d79f288decd21f148424e4e76cb22 [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
61
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>
90 public static WireType GetTagWireType(uint tag) {
91 return (WireType) (tag & TagTypeMask);
92 }
93
94 /// <summary>
95 /// Given a tag value, determines the field number (the upper 29 bits).
96 /// </summary>
97 public static int GetTagFieldNumber(uint tag) {
98 return (int) tag >> TagTypeBits;
99 }
100
101 /// <summary>
102 /// Makes a tag value given a field number and wire type.
103 /// TODO(jonskeet): Should we just have a Tag structure?
104 /// </summary>
105 public static uint MakeTag(int fieldNumber, WireType wireType) {
106 return (uint) (fieldNumber << TagTypeBits) | (uint) wireType;
107 }
108
Jon Skeet25a28582009-02-18 16:06:22 +0000109 public static uint MakeTag(FieldDescriptor field) {
110 return MakeTag(field.FieldNumber, GetWireType(field));
111 }
112
113 /// <summary>
114 /// Returns the wire type for the given field descriptor. This differs
115 /// from GetWireType(FieldType) for packed repeated fields.
116 /// </summary>
117 internal static WireType GetWireType(FieldDescriptor descriptor) {
118 return descriptor.IsPacked ? WireType.LengthDelimited : GetWireType(descriptor.FieldType);
119 }
120
Jon Skeet68036862008-10-22 13:30:34 +0100121 /// <summary>
122 /// Converts a field type to its wire type. Done with a switch for the sake
123 /// of speed - this is significantly faster than a dictionary lookup.
124 /// </summary>
125 public static WireType GetWireType(FieldType fieldType) {
126 switch (fieldType) {
127 case FieldType.Double:
128 return WireType.Fixed64;
129 case FieldType.Float:
130 return WireType.Fixed32;
131 case FieldType.Int64:
132 case FieldType.UInt64:
133 case FieldType.Int32:
134 return WireType.Varint;
135 case FieldType.Fixed64:
136 return WireType.Fixed64;
137 case FieldType.Fixed32:
138 return WireType.Fixed32;
139 case FieldType.Bool:
140 return WireType.Varint;
141 case FieldType.String:
142 return WireType.LengthDelimited;
143 case FieldType.Group:
144 return WireType.StartGroup;
145 case FieldType.Message:
146 case FieldType.Bytes:
147 return WireType.LengthDelimited;
148 case FieldType.UInt32:
149 return WireType.Varint;
150 case FieldType.SFixed32:
151 return WireType.Fixed32;
152 case FieldType.SFixed64:
153 return WireType.Fixed64;
154 case FieldType.SInt32:
155 case FieldType.SInt64:
156 case FieldType.Enum:
157 return WireType.Varint;
158 default:
159 throw new ArgumentOutOfRangeException("No such field type");
160 }
161 }
162 }
163}