WireFormat.cs 7.0 KB

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