WireFormat.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #if !LITE
  36. using Google.ProtocolBuffers.Descriptors;
  37. #endif
  38. namespace Google.ProtocolBuffers {
  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. #region Fixed sizes.
  50. // TODO(jonskeet): Move these somewhere else. They're messy. Consider making FieldType a smarter kind of enum
  51. internal const int Fixed32Size = 4;
  52. internal const int Fixed64Size = 8;
  53. internal const int SFixed32Size = 4;
  54. internal const int SFixed64Size = 8;
  55. internal const int FloatSize = 4;
  56. internal const int DoubleSize = 8;
  57. internal const int BoolSize = 1;
  58. #endregion
  59. [CLSCompliant(false)]
  60. public enum WireType : uint {
  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. internal const int Item = 1;
  70. internal const int TypeID = 2;
  71. internal const int Message = 3;
  72. }
  73. internal static class MessageSetTag {
  74. internal static readonly uint ItemStart = MakeTag(MessageSetField.Item, WireType.StartGroup);
  75. internal static readonly uint ItemEnd = MakeTag(MessageSetField.Item, WireType.EndGroup);
  76. internal static readonly uint TypeID = MakeTag(MessageSetField.TypeID, WireType.Varint);
  77. internal static readonly uint Message = MakeTag(MessageSetField.Message, WireType.LengthDelimited);
  78. }
  79. private const int TagTypeBits = 3;
  80. private const uint TagTypeMask = (1 << TagTypeBits) - 1;
  81. /// <summary>
  82. /// Given a tag value, determines the wire type (lower 3 bits).
  83. /// </summary>
  84. [CLSCompliant(false)]
  85. public static WireType GetTagWireType(uint tag) {
  86. return (WireType) (tag & TagTypeMask);
  87. }
  88. [CLSCompliant(false)]
  89. public static bool IsEndGroupTag(uint tag) {
  90. return (WireType)(tag & TagTypeMask) == WireType.EndGroup;
  91. }
  92. /// <summary>
  93. /// Given a tag value, determines the field number (the upper 29 bits).
  94. /// </summary>
  95. [CLSCompliant(false)]
  96. public static int GetTagFieldNumber(uint tag) {
  97. return (int) tag >> TagTypeBits;
  98. }
  99. /// <summary>
  100. /// Makes a tag value given a field number and wire type.
  101. /// TODO(jonskeet): Should we just have a Tag structure?
  102. /// </summary>
  103. [CLSCompliant(false)]
  104. public static uint MakeTag(int fieldNumber, WireType wireType) {
  105. return (uint) (fieldNumber << TagTypeBits) | (uint) wireType;
  106. }
  107. #if !LITE
  108. [CLSCompliant(false)]
  109. public static uint MakeTag(FieldDescriptor field) {
  110. return MakeTag(field.FieldNumber, GetWireType(field));
  111. }
  112. /// <summary>
  113. /// Returns the wire type for the given field descriptor. This differs
  114. /// from GetWireType(FieldType) for packed repeated fields.
  115. /// </summary>
  116. internal static WireType GetWireType(FieldDescriptor descriptor) {
  117. return descriptor.IsPacked ? WireType.LengthDelimited : GetWireType(descriptor.FieldType);
  118. }
  119. /// <summary>
  120. /// Converts a field type to its wire type. Done with a switch for the sake
  121. /// of speed - this is significantly faster than a dictionary lookup.
  122. /// </summary>
  123. [CLSCompliant(false)]
  124. public static WireType GetWireType(FieldType fieldType) {
  125. switch (fieldType) {
  126. case FieldType.Double:
  127. return WireType.Fixed64;
  128. case FieldType.Float:
  129. return WireType.Fixed32;
  130. case FieldType.Int64:
  131. case FieldType.UInt64:
  132. case FieldType.Int32:
  133. return WireType.Varint;
  134. case FieldType.Fixed64:
  135. return WireType.Fixed64;
  136. case FieldType.Fixed32:
  137. return WireType.Fixed32;
  138. case FieldType.Bool:
  139. return WireType.Varint;
  140. case FieldType.String:
  141. return WireType.LengthDelimited;
  142. case FieldType.Group:
  143. return WireType.StartGroup;
  144. case FieldType.Message:
  145. case FieldType.Bytes:
  146. return WireType.LengthDelimited;
  147. case FieldType.UInt32:
  148. return WireType.Varint;
  149. case FieldType.SFixed32:
  150. return WireType.Fixed32;
  151. case FieldType.SFixed64:
  152. return WireType.Fixed64;
  153. case FieldType.SInt32:
  154. case FieldType.SInt64:
  155. case FieldType.Enum:
  156. return WireType.Varint;
  157. default:
  158. throw new ArgumentOutOfRangeException("No such field type");
  159. }
  160. }
  161. #endif
  162. }
  163. }