UniqueId.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // UniqueId.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. // Jonathan Pryor <[email protected]>
  7. //
  8. // Copyright (C) 2005,2007,2009 Novell, Inc. http://www.novell.com
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Security;
  33. namespace System.Xml
  34. {
  35. public class UniqueId
  36. {
  37. Guid guid;
  38. string id;
  39. public UniqueId ()
  40. : this (Guid.NewGuid ())
  41. {
  42. }
  43. public UniqueId (byte [] id)
  44. : this (id, 0)
  45. {
  46. }
  47. [SecuritySafeCritical]
  48. [SecurityTreatAsSafe]
  49. public UniqueId (Guid id)
  50. {
  51. this.guid = id;
  52. }
  53. public UniqueId (string value)
  54. {
  55. if (value == null)
  56. throw new ArgumentNullException ("value cannot be null", "value");
  57. if (value.Length == 0)
  58. throw new FormatException ("UniqueId cannot be zero length");
  59. this.id = value;
  60. }
  61. [SecuritySafeCritical]
  62. [SecurityTreatAsSafe]
  63. public UniqueId (byte [] id, int offset)
  64. {
  65. if (id == null)
  66. throw new ArgumentNullException ();
  67. if (offset < 0)
  68. throw new ArgumentOutOfRangeException ("offset");
  69. if (offset >= id.Length)
  70. throw new ArgumentException ("id too small.", "offset");
  71. if (id.Length - offset != 16)
  72. throw new ArgumentException ("id and offset provide less than 16 bytes");
  73. if (offset == 0)
  74. guid = new Guid (id);
  75. else {
  76. List<byte> buf = new List<byte> (id);
  77. buf.RemoveRange (0, offset);
  78. guid = new Guid (buf.ToArray ());
  79. }
  80. }
  81. [SecuritySafeCritical]
  82. [SecurityTreatAsSafe]
  83. public UniqueId (char [] id, int offset, int count)
  84. {
  85. if (id == null)
  86. throw new ArgumentNullException ("id");
  87. if (offset < 0 || offset >= id.Length)
  88. throw new ArgumentOutOfRangeException ("offset");
  89. if (count < 0 || id.Length - offset < count)
  90. throw new ArgumentOutOfRangeException ("count");
  91. if (count == 0)
  92. throw new FormatException ();
  93. // Does it start with "urn:uuid:"? If so, it's a Guid.
  94. if (count > 8 && id [offset] == 'u' && id [offset+1] == 'r' &&
  95. id [offset+2] == 'n' && id [offset+3] == ':' &&
  96. id [offset+4] == 'u' && id [offset+5] == 'u' &&
  97. id [offset+6] == 'i' && id [offset+7] == 'd' &&
  98. id [offset+8] == ':') {
  99. if (count != 45)
  100. throw new ArgumentOutOfRangeException ("Invalid Guid");
  101. this.guid = new Guid (new string (id, offset+9, count-9));
  102. } else
  103. this.id = new string (id, offset, count);
  104. }
  105. [SecuritySafeCritical]
  106. [SecurityTreatAsSafe]
  107. public int CharArrayLength {
  108. get {return id != null ? id.Length : 45;}
  109. }
  110. public bool IsGuid {
  111. get { return guid != default (Guid); }
  112. }
  113. public override bool Equals (Object obj)
  114. {
  115. UniqueId other = obj as UniqueId;
  116. if (other == null)
  117. return false;
  118. if (IsGuid && other.IsGuid) {
  119. return guid.Equals (other.guid);
  120. }
  121. return id == other.id;
  122. }
  123. [SecuritySafeCritical]
  124. [SecurityTreatAsSafe]
  125. [MonoTODO ("Determine semantics when IsGuid==true")]
  126. public override int GetHashCode ()
  127. {
  128. return id != null ? id.GetHashCode () : guid.GetHashCode ();
  129. }
  130. public static bool operator == (UniqueId id1, UniqueId id2)
  131. {
  132. return object.Equals (id1, id2);
  133. }
  134. public static bool operator != (UniqueId id1, UniqueId id2)
  135. {
  136. return ! (id1 == id2);
  137. }
  138. [SecuritySafeCritical]
  139. [SecurityTreatAsSafe]
  140. public int ToCharArray (char [] array, int offset)
  141. {
  142. if (array == null)
  143. throw new ArgumentNullException ("array");
  144. if (offset < 0 || offset >= array.Length)
  145. throw new ArgumentOutOfRangeException ("offset");
  146. string s = ToString ();
  147. s.CopyTo (0, array, offset, s.Length);
  148. return s.Length;
  149. }
  150. public override string ToString ()
  151. {
  152. if (id == null)
  153. return "urn:uuid:" + guid;
  154. return id;
  155. }
  156. public bool TryGetGuid (out Guid guid)
  157. {
  158. if (IsGuid) {
  159. guid = this.guid;
  160. return true;
  161. } else {
  162. guid = default (Guid);
  163. return false;
  164. }
  165. }
  166. [SecuritySafeCritical]
  167. [SecurityTreatAsSafe]
  168. public bool TryGetGuid (byte [] buffer, int offset)
  169. {
  170. if (!IsGuid)
  171. return false;
  172. if (buffer == null)
  173. throw new ArgumentNullException ("buffer");
  174. if (offset < 0 || offset >= buffer.Length)
  175. throw new ArgumentOutOfRangeException ("offset");
  176. if (buffer.Length - offset < 16)
  177. throw new ArgumentOutOfRangeException ("offset");
  178. guid.ToByteArray ().CopyTo (buffer, offset);
  179. return true;
  180. }
  181. }
  182. }
  183. #endif