5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Jonathan Pryor <jpryor@novell.com>
8 // Copyright (C) 2005,2007,2009 Novell, Inc. http://www.novell.com
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections.Generic;
32 using System.Security;
42 : this (Guid.NewGuid ())
46 public UniqueId (byte [] id)
51 [SecuritySafeCritical]
53 public UniqueId (Guid id)
58 public UniqueId (string value)
61 throw new ArgumentNullException ("value cannot be null", "value");
63 if (value.Length == 0)
64 throw new FormatException ("UniqueId cannot be zero length");
69 [SecuritySafeCritical]
71 public UniqueId (byte [] id, int offset)
74 throw new ArgumentNullException ();
76 throw new ArgumentOutOfRangeException ("offset");
77 if (offset >= id.Length)
78 throw new ArgumentException ("id too small.", "offset");
80 if (id.Length - offset != 16)
81 throw new ArgumentException ("id and offset provide less than 16 bytes");
86 List<byte> buf = new List<byte> (id);
87 buf.RemoveRange (0, offset);
88 guid = new Guid (buf.ToArray ());
92 [SecuritySafeCritical]
94 public UniqueId (char [] id, int offset, int count)
97 throw new ArgumentNullException ("id");
98 if (offset < 0 || offset >= id.Length)
99 throw new ArgumentOutOfRangeException ("offset");
100 if (count < 0 || id.Length - offset < count)
101 throw new ArgumentOutOfRangeException ("count");
103 throw new FormatException ();
105 // Does it start with "urn:uuid:"? If so, it's a Guid.
106 if (count > 8 && id [offset] == 'u' && id [offset+1] == 'r' &&
107 id [offset+2] == 'n' && id [offset+3] == ':' &&
108 id [offset+4] == 'u' && id [offset+5] == 'u' &&
109 id [offset+6] == 'i' && id [offset+7] == 'd' &&
110 id [offset+8] == ':') {
112 throw new ArgumentOutOfRangeException ("Invalid Guid");
113 this.guid = new Guid (new string (id, offset+9, count-9));
115 this.id = new string (id, offset, count);
118 [SecuritySafeCritical]
119 [SecurityTreatAsSafe]
120 public int CharArrayLength {
121 get {return id != null ? id.Length : 45;}
125 get { return guid != default (Guid); }
128 public override bool Equals (Object obj)
130 UniqueId other = obj as UniqueId;
135 if (IsGuid && other.IsGuid) {
136 return guid.Equals (other.guid);
139 return id == other.id;
142 [SecuritySafeCritical]
143 [SecurityTreatAsSafe]
144 [MonoTODO ("Determine semantics when IsGuid==true")]
145 public override int GetHashCode ()
147 return id != null ? id.GetHashCode () : guid.GetHashCode ();
150 public static bool operator == (UniqueId id1, UniqueId id2)
152 return object.Equals (id1, id2);
155 public static bool operator != (UniqueId id1, UniqueId id2)
157 return ! (id1 == id2);
160 [SecuritySafeCritical]
161 [SecurityTreatAsSafe]
162 public int ToCharArray (char [] array, int offset)
165 throw new ArgumentNullException ("array");
166 if (offset < 0 || offset >= array.Length)
167 throw new ArgumentOutOfRangeException ("offset");
169 string s = ToString ();
170 s.CopyTo (0, array, offset, s.Length);
174 public override string ToString ()
177 return "urn:uuid:" + guid;
182 public bool TryGetGuid (out Guid guid)
188 guid = default (Guid);
193 [SecuritySafeCritical]
194 [SecurityTreatAsSafe]
195 public bool TryGetGuid (byte [] buffer, int offset)
201 throw new ArgumentNullException ("buffer");
202 if (offset < 0 || offset >= buffer.Length)
203 throw new ArgumentOutOfRangeException ("offset");
204 if (buffer.Length - offset < 16)
205 throw new ArgumentOutOfRangeException ("offset");
207 guid.ToByteArray ().CopyTo (buffer, offset);