UniqueId.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. public UniqueId (Guid id)
  48. {
  49. this.guid = id;
  50. }
  51. #if !NET_2_1
  52. [SecurityCritical]
  53. [SecurityTreatAsSafe]
  54. #endif
  55. public UniqueId (string value)
  56. {
  57. if (value == null)
  58. throw new ArgumentNullException ("value cannot be null", "value");
  59. if (value.Length == 0)
  60. throw new FormatException ("UniqueId cannot be zero length");
  61. this.id = value;
  62. }
  63. #if !NET_2_1
  64. [SecurityCritical]
  65. [SecurityTreatAsSafe]
  66. #endif
  67. public UniqueId (byte [] id, int offset)
  68. {
  69. if (id == null)
  70. throw new ArgumentNullException ();
  71. if (offset < 0)
  72. throw new ArgumentOutOfRangeException ("offset");
  73. if (offset >= id.Length)
  74. throw new ArgumentException ("id too small.", "offset");
  75. if (id.Length - offset != 16)
  76. throw new ArgumentException ("id and offset provide less than 16 bytes");
  77. if (offset == 0)
  78. guid = new Guid (id);
  79. else {
  80. List<byte> buf = new List<byte> (id);
  81. buf.RemoveRange (0, offset);
  82. guid = new Guid (buf.ToArray ());
  83. }
  84. }
  85. #if !NET_2_1
  86. [SecurityCritical]
  87. [SecurityTreatAsSafe]
  88. #endif
  89. public UniqueId (char [] id, int offset, int count)
  90. {
  91. if (id == null)
  92. throw new ArgumentNullException ("id");
  93. if (offset < 0 || offset >= id.Length)
  94. throw new ArgumentOutOfRangeException ("offset");
  95. if (count < 0 || id.Length - offset < count)
  96. throw new ArgumentOutOfRangeException ("count");
  97. if (count == 0)
  98. throw new FormatException ();
  99. // Does it start with "urn:uuid:"? If so, it's a Guid.
  100. if (count > 8 && id [offset] == 'u' && id [offset+1] == 'r' &&
  101. id [offset+2] == 'n' && id [offset+3] == ':' &&
  102. id [offset+4] == 'u' && id [offset+5] == 'u' &&
  103. id [offset+6] == 'i' && id [offset+7] == 'd' &&
  104. id [offset+8] == ':') {
  105. if (count != 45)
  106. throw new ArgumentOutOfRangeException ("Invalid Guid");
  107. this.guid = new Guid (new string (id, offset+9, count-9));
  108. } else
  109. this.id = new string (id, offset, count);
  110. }
  111. public int CharArrayLength {
  112. #if !NET_2_1
  113. [SecurityCritical]
  114. [SecurityTreatAsSafe]
  115. #endif
  116. get {return id != null ? id.Length : 45;}
  117. }
  118. public bool IsGuid {
  119. get { return guid != default (Guid); }
  120. }
  121. public override bool Equals (Object obj)
  122. {
  123. UniqueId other = obj as UniqueId;
  124. if (other == null)
  125. return false;
  126. if (IsGuid && other.IsGuid) {
  127. return guid.Equals (other.guid);
  128. }
  129. return id == other.id;
  130. }
  131. [MonoTODO ("Determine semantics when IsGuid==true")]
  132. public override int GetHashCode ()
  133. {
  134. return id != null ? id.GetHashCode () : guid.GetHashCode ();
  135. }
  136. public static bool operator == (UniqueId id1, UniqueId id2)
  137. {
  138. return object.Equals (id1, id2);
  139. }
  140. public static bool operator != (UniqueId id1, UniqueId id2)
  141. {
  142. return ! (id1 == id2);
  143. }
  144. #if !NET_2_1
  145. [SecurityCritical]
  146. [SecurityTreatAsSafe]
  147. #endif
  148. public int ToCharArray (char [] array, int offset)
  149. {
  150. if (array == null)
  151. throw new ArgumentNullException ("array");
  152. if (offset < 0 || offset >= array.Length)
  153. throw new ArgumentOutOfRangeException ("offset");
  154. string s = ToString ();
  155. s.CopyTo (0, array, offset, s.Length);
  156. return s.Length;
  157. }
  158. #if !NET_2_1
  159. [SecurityCritical]
  160. [SecurityTreatAsSafe]
  161. #endif
  162. public override string ToString ()
  163. {
  164. if (id == null)
  165. return "urn:uuid:" + guid;
  166. return id;
  167. }
  168. public bool TryGetGuid (out Guid guid)
  169. {
  170. if (IsGuid) {
  171. guid = this.guid;
  172. return true;
  173. } else {
  174. guid = default (Guid);
  175. return false;
  176. }
  177. }
  178. #if !NET_2_1
  179. [SecurityCritical]
  180. [SecurityTreatAsSafe]
  181. #endif
  182. public bool TryGetGuid (byte [] buffer, int offset)
  183. {
  184. if (!IsGuid)
  185. return false;
  186. if (buffer == null)
  187. throw new ArgumentNullException ("buffer");
  188. if (offset < 0 || offset >= buffer.Length)
  189. throw new ArgumentOutOfRangeException ("offset");
  190. if (buffer.Length - offset < 16)
  191. throw new ArgumentOutOfRangeException ("offset");
  192. guid.ToByteArray ().CopyTo (buffer, offset);
  193. return true;
  194. }
  195. }
  196. }
  197. #endif