UUID.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 =
  77. {
  78. '0', '1', '2', '3', '4',
  79. '5', '6', '7', '8', '9',
  80. 'A', 'B', 'C', 'D', 'E', 'F'
  81. };
  82. public static string Bin_to_hex(int value, int length)
  83. {
  84. char[] arr = new char[length];
  85. int ai = arr.Length;
  86. while (ai-- > 0)
  87. {
  88. arr[ai] = Hexchars[value & unchecked(0xF)];
  89. value = (int)(((uint)value) >> 4);
  90. }
  91. return new string(arr);
  92. }
  93. private static byte B(int i)
  94. {
  95. return unchecked((byte)(i & unchecked(0xFF)));
  96. }
  97. private static short S(int i)
  98. {
  99. return (short)(i & unchecked(0xFFFF));
  100. }
  101. public Uuid(Rpc.UuidT uuid)
  102. {
  103. TimeLow = uuid.TimeLow;
  104. TimeMid = uuid.TimeMid;
  105. TimeHiAndVersion = uuid.TimeHiAndVersion;
  106. ClockSeqHiAndReserved = uuid.ClockSeqHiAndReserved;
  107. ClockSeqLow = uuid.ClockSeqLow;
  108. Node = new byte[6];
  109. Node[0] = uuid.Node[0];
  110. Node[1] = uuid.Node[1];
  111. Node[2] = uuid.Node[2];
  112. Node[3] = uuid.Node[3];
  113. Node[4] = uuid.Node[4];
  114. Node[5] = uuid.Node[5];
  115. }
  116. public Uuid(string str)
  117. {
  118. char[] arr = str.ToCharArray();
  119. TimeLow = Hex_to_bin(arr, 0, 8);
  120. TimeMid = S(Hex_to_bin(arr, 9, 4));
  121. TimeHiAndVersion = S(Hex_to_bin(arr, 14, 4));
  122. ClockSeqHiAndReserved = B(Hex_to_bin(arr, 19, 2));
  123. ClockSeqLow = B(Hex_to_bin(arr, 21, 2));
  124. Node = new byte[6];
  125. Node[0] = B(Hex_to_bin(arr, 24, 2));
  126. Node[1] = B(Hex_to_bin(arr, 26, 2));
  127. Node[2] = B(Hex_to_bin(arr, 28, 2));
  128. Node[3] = B(Hex_to_bin(arr, 30, 2));
  129. Node[4] = B(Hex_to_bin(arr, 32, 2));
  130. Node[5] = B(Hex_to_bin(arr, 34, 2));
  131. }
  132. public override string ToString()
  133. {
  134. return Bin_to_hex(TimeLow, 8)
  135. + '-' + Bin_to_hex(TimeMid, 4)
  136. + '-' + Bin_to_hex(TimeHiAndVersion, 4)
  137. + '-' + Bin_to_hex(ClockSeqHiAndReserved, 2)
  138. + Bin_to_hex(ClockSeqLow, 2)
  139. + '-' + Bin_to_hex(Node[0], 2)
  140. + Bin_to_hex(Node[1], 2)
  141. + Bin_to_hex(Node[2], 2)
  142. + Bin_to_hex(Node[3], 2)
  143. + Bin_to_hex(Node[4], 2)
  144. + Bin_to_hex(Node[5], 2);
  145. }
  146. }
  147. }