UUID.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. namespace SharpCifs.Dcerpc
  19. {
  20. public class Uuid : Rpc.UuidT
  21. {
  22. public static int Hex_to_bin(char[] arr, int offset, int length)
  23. {
  24. int value = 0;
  25. int ai;
  26. int count;
  27. count = 0;
  28. for (ai = offset; ai < arr.Length && count < length; ai++)
  29. {
  30. value <<= 4;
  31. switch (arr[ai])
  32. {
  33. case '0':
  34. case '1':
  35. case '2':
  36. case '3':
  37. case '4':
  38. case '5':
  39. case '6':
  40. case '7':
  41. case '8':
  42. case '9':
  43. {
  44. value += arr[ai] - '0';
  45. break;
  46. }
  47. case 'A':
  48. case 'B':
  49. case 'C':
  50. case 'D':
  51. case 'E':
  52. case 'F':
  53. {
  54. value += 10 + (arr[ai] - 'A');
  55. break;
  56. }
  57. case 'a':
  58. case 'b':
  59. case 'c':
  60. case 'd':
  61. case 'e':
  62. case 'f':
  63. {
  64. value += 10 + (arr[ai] - 'a');
  65. break;
  66. }
  67. default:
  68. {
  69. throw new ArgumentException(new string(arr, offset, length));
  70. }
  71. }
  72. count++;
  73. }
  74. return value;
  75. }
  76. internal static readonly char[] Hexchars = { '0', '1', '2', '3', '4',
  77. '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  78. public static string Bin_to_hex(int value, int length)
  79. {
  80. char[] arr = new char[length];
  81. int ai = arr.Length;
  82. while (ai-- > 0)
  83. {
  84. arr[ai] = Hexchars[value & unchecked(0xF)];
  85. value = (int)(((uint)value) >> 4);
  86. }
  87. return new string(arr);
  88. }
  89. private static byte B(int i)
  90. {
  91. return unchecked((byte)(i & unchecked(0xFF)));
  92. }
  93. private static short S(int i)
  94. {
  95. return (short)(i & unchecked(0xFFFF));
  96. }
  97. public Uuid(Rpc.UuidT uuid)
  98. {
  99. TimeLow = uuid.TimeLow;
  100. TimeMid = uuid.TimeMid;
  101. TimeHiAndVersion = uuid.TimeHiAndVersion;
  102. ClockSeqHiAndReserved = uuid.ClockSeqHiAndReserved;
  103. ClockSeqLow = uuid.ClockSeqLow;
  104. Node = new byte[6];
  105. Node[0] = uuid.Node[0];
  106. Node[1] = uuid.Node[1];
  107. Node[2] = uuid.Node[2];
  108. Node[3] = uuid.Node[3];
  109. Node[4] = uuid.Node[4];
  110. Node[5] = uuid.Node[5];
  111. }
  112. public Uuid(string str)
  113. {
  114. char[] arr = str.ToCharArray();
  115. TimeLow = Hex_to_bin(arr, 0, 8);
  116. TimeMid = S(Hex_to_bin(arr, 9, 4));
  117. TimeHiAndVersion = S(Hex_to_bin(arr, 14, 4));
  118. ClockSeqHiAndReserved = B(Hex_to_bin(arr, 19, 2));
  119. ClockSeqLow = B(Hex_to_bin(arr, 21, 2));
  120. Node = new byte[6];
  121. Node[0] = B(Hex_to_bin(arr, 24, 2));
  122. Node[1] = B(Hex_to_bin(arr, 26, 2));
  123. Node[2] = B(Hex_to_bin(arr, 28, 2));
  124. Node[3] = B(Hex_to_bin(arr, 30, 2));
  125. Node[4] = B(Hex_to_bin(arr, 32, 2));
  126. Node[5] = B(Hex_to_bin(arr, 34, 2));
  127. }
  128. public override string ToString()
  129. {
  130. return Bin_to_hex(TimeLow, 8) + '-' + Bin_to_hex(TimeMid, 4) + '-' + Bin_to_hex
  131. (TimeHiAndVersion, 4) + '-' + Bin_to_hex(ClockSeqHiAndReserved, 2) + Bin_to_hex
  132. (ClockSeqLow, 2) + '-' + Bin_to_hex(Node[0], 2) + Bin_to_hex(Node[1], 2) + Bin_to_hex
  133. (Node[2], 2) + Bin_to_hex(Node[3], 2) + Bin_to_hex(Node[4], 2) + Bin_to_hex(Node
  134. [5], 2);
  135. }
  136. }
  137. }