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