GuidConverter.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. namespace MongoDB.Bson
  17. {
  18. /// <summary>
  19. /// A static class containing methods to convert to and from Guids and byte arrays in various byte orders.
  20. /// </summary>
  21. public static class GuidConverter
  22. {
  23. /// <summary>
  24. /// Converts a byte array to a Guid.
  25. /// </summary>
  26. /// <param name="bytes">The byte array.</param>
  27. /// <param name="representation">The representation of the Guid in the byte array.</param>
  28. /// <returns>A Guid.</returns>
  29. public static Guid FromBytes(byte[] bytes, GuidRepresentation representation)
  30. {
  31. if (bytes.Length != 16)
  32. {
  33. var message = string.Format("Length of byte array must be 16, not {0}.", bytes.Length);
  34. throw new ArgumentException(message);
  35. }
  36. bytes = (byte[])bytes.Clone();
  37. switch (representation)
  38. {
  39. case GuidRepresentation.CSharpLegacy:
  40. if (!BitConverter.IsLittleEndian)
  41. {
  42. Array.Reverse(bytes, 0, 4);
  43. Array.Reverse(bytes, 4, 2);
  44. Array.Reverse(bytes, 6, 2);
  45. }
  46. break;
  47. case GuidRepresentation.JavaLegacy:
  48. Array.Reverse(bytes, 0, 8);
  49. Array.Reverse(bytes, 8, 8);
  50. if (BitConverter.IsLittleEndian)
  51. {
  52. Array.Reverse(bytes, 0, 4);
  53. Array.Reverse(bytes, 4, 2);
  54. Array.Reverse(bytes, 6, 2);
  55. }
  56. break;
  57. case GuidRepresentation.PythonLegacy:
  58. case GuidRepresentation.Standard:
  59. if (BitConverter.IsLittleEndian)
  60. {
  61. Array.Reverse(bytes, 0, 4);
  62. Array.Reverse(bytes, 4, 2);
  63. Array.Reverse(bytes, 6, 2);
  64. }
  65. break;
  66. case GuidRepresentation.Unspecified:
  67. throw new InvalidOperationException("Unable to convert byte array to Guid because GuidRepresentation is Unspecified.");
  68. default:
  69. throw new BsonInternalException("Unexpected GuidRepresentation.");
  70. }
  71. return new Guid(bytes);
  72. }
  73. /// <summary>
  74. /// Gets the sub type that corresponds to the guidRepresentation.
  75. /// </summary>
  76. /// <param name="guidRepresentation">The Guid representation.</param>
  77. /// <returns>The sub type.</returns>
  78. public static BsonBinarySubType GetSubType(GuidRepresentation guidRepresentation)
  79. {
  80. switch (guidRepresentation)
  81. {
  82. case GuidRepresentation.Standard:
  83. return BsonBinarySubType.UuidStandard;
  84. case GuidRepresentation.CSharpLegacy:
  85. case GuidRepresentation.JavaLegacy:
  86. case GuidRepresentation.PythonLegacy:
  87. return BsonBinarySubType.UuidLegacy;
  88. default:
  89. throw new ArgumentException("Invalid GuidRepresentation.", nameof(guidRepresentation));
  90. }
  91. }
  92. /// <summary>
  93. /// Converts a Guid to a byte array.
  94. /// </summary>
  95. /// <param name="guid">The Guid.</param>
  96. /// <param name="guidRepresentation">The representation of the Guid in the byte array.</param>
  97. /// <returns>A byte array.</returns>
  98. public static byte[] ToBytes(Guid guid, GuidRepresentation guidRepresentation)
  99. {
  100. var bytes = (byte[])guid.ToByteArray().Clone();
  101. switch (guidRepresentation)
  102. {
  103. case GuidRepresentation.CSharpLegacy:
  104. if (!BitConverter.IsLittleEndian)
  105. {
  106. Array.Reverse(bytes, 0, 4);
  107. Array.Reverse(bytes, 4, 2);
  108. Array.Reverse(bytes, 6, 2);
  109. }
  110. break;
  111. case GuidRepresentation.JavaLegacy:
  112. if (BitConverter.IsLittleEndian)
  113. {
  114. Array.Reverse(bytes, 0, 4);
  115. Array.Reverse(bytes, 4, 2);
  116. Array.Reverse(bytes, 6, 2);
  117. }
  118. Array.Reverse(bytes, 0, 8);
  119. Array.Reverse(bytes, 8, 8);
  120. break;
  121. case GuidRepresentation.PythonLegacy:
  122. case GuidRepresentation.Standard:
  123. if (BitConverter.IsLittleEndian)
  124. {
  125. Array.Reverse(bytes, 0, 4);
  126. Array.Reverse(bytes, 4, 2);
  127. Array.Reverse(bytes, 6, 2);
  128. }
  129. break;
  130. case GuidRepresentation.Unspecified:
  131. throw new InvalidOperationException("Unable to convert Guid to byte array because GuidRepresentation is Unspecified.");
  132. default:
  133. throw new ArgumentException($"Invalid guidRepresentation: {guidRepresentation}.", nameof(guidRepresentation));
  134. }
  135. return bytes;
  136. }
  137. }
  138. }