WireFormat.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System;
  17. using System.Reflection;
  18. using Google.ProtocolBuffers.Descriptors;
  19. using System.Collections.Generic;
  20. using Google.ProtocolBuffers.Collections;
  21. namespace Google.ProtocolBuffers {
  22. /// <summary>
  23. /// This class is used internally by the Protocol Buffer Library and generated
  24. /// message implementations. It is public only for the sake of those generated
  25. /// messages. Others should not use this class directly.
  26. /// <para>
  27. /// This class contains constants and helper functions useful for dealing with
  28. /// the Protocol Buffer wire format.
  29. /// </para>
  30. /// </summary>
  31. public static class WireFormat {
  32. public enum WireType : uint {
  33. Varint = 0,
  34. Fixed64 = 1,
  35. LengthDelimited = 2,
  36. StartGroup = 3,
  37. EndGroup = 4,
  38. Fixed32 = 5
  39. }
  40. internal static class MessageSetField {
  41. internal const int Item = 1;
  42. internal const int TypeID = 2;
  43. internal const int Message = 3;
  44. }
  45. internal static class MessageSetTag {
  46. internal static readonly uint ItemStart = MakeTag(MessageSetField.Item, WireType.StartGroup);
  47. internal static readonly uint ItemEnd = MakeTag(MessageSetField.Item, WireType.EndGroup);
  48. internal static readonly uint TypeID = MakeTag(MessageSetField.TypeID, WireType.Varint);
  49. internal static readonly uint Message = MakeTag(MessageSetField.Message, WireType.LengthDelimited);
  50. }
  51. private const int TagTypeBits = 3;
  52. private const uint TagTypeMask = (1 << TagTypeBits) - 1;
  53. /// <summary>
  54. /// Given a tag value, determines the wire type (lower 3 bits).
  55. /// </summary>
  56. public static WireType GetTagWireType(uint tag) {
  57. return (WireType) (tag & TagTypeMask);
  58. }
  59. /// <summary>
  60. /// Given a tag value, determines the field number (the upper 29 bits).
  61. /// </summary>
  62. public static int GetTagFieldNumber(uint tag) {
  63. return (int) tag >> TagTypeBits;
  64. }
  65. /// <summary>
  66. /// Makes a tag value given a field number and wire type.
  67. /// TODO(jonskeet): Should we just have a Tag structure?
  68. /// </summary>
  69. public static uint MakeTag(int fieldNumber, WireType wireType) {
  70. return (uint) (fieldNumber << TagTypeBits) | (uint) wireType;
  71. }
  72. /// <summary>
  73. /// Converts a field type to its wire type. Done with a switch for the sake
  74. /// of speed - this is significantly faster than a dictionary lookup.
  75. /// </summary>
  76. public static WireType GetWireType(FieldType fieldType) {
  77. switch (fieldType) {
  78. case FieldType.Double:
  79. return WireType.Fixed64;
  80. case FieldType.Float:
  81. return WireType.Fixed32;
  82. case FieldType.Int64:
  83. case FieldType.UInt64:
  84. case FieldType.Int32:
  85. return WireType.Varint;
  86. case FieldType.Fixed64:
  87. return WireType.Fixed64;
  88. case FieldType.Fixed32:
  89. return WireType.Fixed32;
  90. case FieldType.Bool:
  91. return WireType.Varint;
  92. case FieldType.String:
  93. return WireType.LengthDelimited;
  94. case FieldType.Group:
  95. return WireType.StartGroup;
  96. case FieldType.Message:
  97. case FieldType.Bytes:
  98. return WireType.LengthDelimited;
  99. case FieldType.UInt32:
  100. return WireType.Varint;
  101. case FieldType.SFixed32:
  102. return WireType.Fixed32;
  103. case FieldType.SFixed64:
  104. return WireType.Fixed64;
  105. case FieldType.SInt32:
  106. case FieldType.SInt64:
  107. case FieldType.Enum:
  108. return WireType.Varint;
  109. default:
  110. throw new ArgumentOutOfRangeException("No such field type");
  111. }
  112. }
  113. }
  114. }