GuidConverter.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright 2010-2014 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. /// Converts a Guid to a byte array.
  75. /// </summary>
  76. /// <param name="guid">The Guid.</param>
  77. /// <param name="representation">The representation of the Guid in the byte array.</param>
  78. /// <returns>A byte array.</returns>
  79. public static byte[] ToBytes(Guid guid, GuidRepresentation representation)
  80. {
  81. var bytes = (byte[])guid.ToByteArray().Clone();
  82. switch (representation)
  83. {
  84. case GuidRepresentation.CSharpLegacy:
  85. if (!BitConverter.IsLittleEndian)
  86. {
  87. Array.Reverse(bytes, 0, 4);
  88. Array.Reverse(bytes, 4, 2);
  89. Array.Reverse(bytes, 6, 2);
  90. }
  91. break;
  92. case GuidRepresentation.JavaLegacy:
  93. if (BitConverter.IsLittleEndian)
  94. {
  95. Array.Reverse(bytes, 0, 4);
  96. Array.Reverse(bytes, 4, 2);
  97. Array.Reverse(bytes, 6, 2);
  98. }
  99. Array.Reverse(bytes, 0, 8);
  100. Array.Reverse(bytes, 8, 8);
  101. break;
  102. case GuidRepresentation.PythonLegacy:
  103. case GuidRepresentation.Standard:
  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.Unspecified:
  112. throw new InvalidOperationException("Unable to convert Guid to byte array because GuidRepresentation is Unspecified.");
  113. default:
  114. throw new BsonInternalException("Unexpected GuidRepresentation.");
  115. }
  116. return bytes;
  117. }
  118. }
  119. }