WireFormat.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. namespace Google.Protobuf
  33. {
  34. /// <summary>
  35. /// This class is used internally by the Protocol Buffer Library and generated
  36. /// message implementations. It is public only for the sake of those generated
  37. /// messages. Others should not use this class directly.
  38. /// <para>
  39. /// This class contains constants and helper functions useful for dealing with
  40. /// the Protocol Buffer wire format.
  41. /// </para>
  42. /// </summary>
  43. public static class WireFormat
  44. {
  45. /// <summary>
  46. /// Wire types within protobuf encoding.
  47. /// </summary>
  48. public enum WireType : uint
  49. {
  50. /// <summary>
  51. /// Variable-length integer.
  52. /// </summary>
  53. Varint = 0,
  54. /// <summary>
  55. /// A fixed-length 64-bit value.
  56. /// </summary>
  57. Fixed64 = 1,
  58. /// <summary>
  59. /// A length-delimited value, i.e. a length followed by that many bytes of data.
  60. /// </summary>
  61. LengthDelimited = 2,
  62. /// <summary>
  63. /// A "start group" value - not supported by this implementation.
  64. /// </summary>
  65. StartGroup = 3,
  66. /// <summary>
  67. /// An "end group" value - not supported by this implementation.
  68. /// </summary>
  69. EndGroup = 4,
  70. /// <summary>
  71. /// A fixed-length 32-bit value.
  72. /// </summary>
  73. Fixed32 = 5
  74. }
  75. private const int TagTypeBits = 3;
  76. private const uint TagTypeMask = (1 << TagTypeBits) - 1;
  77. /// <summary>
  78. /// Given a tag value, determines the wire type (lower 3 bits).
  79. /// </summary>
  80. public static WireType GetTagWireType(uint tag)
  81. {
  82. return (WireType) (tag & TagTypeMask);
  83. }
  84. /// <summary>
  85. /// Given a tag value, determines the field number (the upper 29 bits).
  86. /// </summary>
  87. public static int GetTagFieldNumber(uint tag)
  88. {
  89. return (int) tag >> TagTypeBits;
  90. }
  91. /// <summary>
  92. /// Makes a tag value given a field number and wire type.
  93. /// </summary>
  94. public static uint MakeTag(int fieldNumber, WireType wireType)
  95. {
  96. return (uint) (fieldNumber << TagTypeBits) | (uint) wireType;
  97. }
  98. }
  99. }