blob: abf47169f3c0651add93ef170770db69cbb81fed [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 {
49 public enum WireType : uint {
50 Varint = 0,
51 Fixed64 = 1,
52 LengthDelimited = 2,
53 StartGroup = 3,
54 EndGroup = 4,
55 Fixed32 = 5
56 }
57
58 internal static class MessageSetField {
59 internal const int Item = 1;
60 internal const int TypeID = 2;
61 internal const int Message = 3;
62 }
63
64 internal static class MessageSetTag {
65 internal static readonly uint ItemStart = MakeTag(MessageSetField.Item, WireType.StartGroup);
66 internal static readonly uint ItemEnd = MakeTag(MessageSetField.Item, WireType.EndGroup);
67 internal static readonly uint TypeID = MakeTag(MessageSetField.TypeID, WireType.Varint);
68 internal static readonly uint Message = MakeTag(MessageSetField.Message, WireType.LengthDelimited);
69 }
70
71 private const int TagTypeBits = 3;
72 private const uint TagTypeMask = (1 << TagTypeBits) - 1;
73
74 /// <summary>
75 /// Given a tag value, determines the wire type (lower 3 bits).
76 /// </summary>
77 public static WireType GetTagWireType(uint tag) {
78 return (WireType) (tag & TagTypeMask);
79 }
80
81 /// <summary>
82 /// Given a tag value, determines the field number (the upper 29 bits).
83 /// </summary>
84 public static int GetTagFieldNumber(uint tag) {
85 return (int) tag >> TagTypeBits;
86 }
87
88 /// <summary>
89 /// Makes a tag value given a field number and wire type.
90 /// TODO(jonskeet): Should we just have a Tag structure?
91 /// </summary>
92 public static uint MakeTag(int fieldNumber, WireType wireType) {
93 return (uint) (fieldNumber << TagTypeBits) | (uint) wireType;
94 }
95
96 /// <summary>
97 /// Converts a field type to its wire type. Done with a switch for the sake
98 /// of speed - this is significantly faster than a dictionary lookup.
99 /// </summary>
100 public static WireType GetWireType(FieldType fieldType) {
101 switch (fieldType) {
102 case FieldType.Double:
103 return WireType.Fixed64;
104 case FieldType.Float:
105 return WireType.Fixed32;
106 case FieldType.Int64:
107 case FieldType.UInt64:
108 case FieldType.Int32:
109 return WireType.Varint;
110 case FieldType.Fixed64:
111 return WireType.Fixed64;
112 case FieldType.Fixed32:
113 return WireType.Fixed32;
114 case FieldType.Bool:
115 return WireType.Varint;
116 case FieldType.String:
117 return WireType.LengthDelimited;
118 case FieldType.Group:
119 return WireType.StartGroup;
120 case FieldType.Message:
121 case FieldType.Bytes:
122 return WireType.LengthDelimited;
123 case FieldType.UInt32:
124 return WireType.Varint;
125 case FieldType.SFixed32:
126 return WireType.Fixed32;
127 case FieldType.SFixed64:
128 return WireType.Fixed64;
129 case FieldType.SInt32:
130 case FieldType.SInt64:
131 case FieldType.Enum:
132 return WireType.Varint;
133 default:
134 throw new ArgumentOutOfRangeException("No such field type");
135 }
136 }
137 }
138}