WireFormat.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. namespace Google.ProtocolBuffers {
  17. public class WireFormat {
  18. public enum WireType : uint {
  19. Varint = 0,
  20. Fixed64 = 1,
  21. LengthDelimited = 2,
  22. StartGroup = 3,
  23. EndGroup = 4,
  24. Fixed32 = 5
  25. }
  26. internal class MessageSetField {
  27. internal const int Item = 1;
  28. internal const int TypeID = 2;
  29. internal const int Message = 3;
  30. }
  31. private const int TagTypeBits = 3;
  32. private const uint TagTypeMask = (1 << TagTypeBits) - 1;
  33. /// <summary>
  34. /// Given a tag value, determines the wire type (lower 3 bits).
  35. /// </summary>
  36. public static WireType GetTagWireType(uint tag) {
  37. return (WireType) (tag & TagTypeMask);
  38. }
  39. /// <summary>
  40. /// Given a tag value, determines the field number (the upper 29 bits).
  41. /// </summary>
  42. public static uint GetTagFieldNumber(uint tag) {
  43. return tag >> TagTypeBits;
  44. }
  45. /// <summary>
  46. /// Makes a tag value given a field number and wire type.
  47. /// </summary>
  48. public static uint MakeTag(int fieldNumber, WireType wireType) {
  49. return (uint) (fieldNumber << TagTypeBits) | (uint) wireType;
  50. }
  51. }
  52. }