4 using System.Runtime.InteropServices;
6 namespace SiliconStudio.Core.Storage
11 [StructLayout(LayoutKind.Sequential, Pack = 4)]
12 [DataContract(
"ObjectId")]
18 public const int HashSize = 16;
19 public const int HashStringLength = HashSize * 2;
20 private const int HashSizeInUInt = HashSize /
sizeof(uint);
21 private const string HexDigits =
"0123456789abcdef";
22 private uint hash1, hash2, hash3,
hash4;
32 if (hash == null)
throw new ArgumentNullException(
"hash");
34 if (hash.Length != HashSize)
35 throw new InvalidOperationException(
"ObjectId value doesn't match expected size.");
37 fixed (byte* hashSource = hash)
39 var hashSourceCurrent = (uint*)hashSource;
40 hash1 = *hashSourceCurrent++;
41 hash2 = *hashSourceCurrent++;
42 hash3 = *hashSourceCurrent++;
43 hash4 = *hashSourceCurrent;
47 public ObjectId(uint hash1, uint hash2, uint hash3, uint hash4)
60 public static explicit operator byte[](
ObjectId objectId)
62 var result =
new byte[HashSize];
63 var hashSource = &objectId.hash1;
64 fixed (byte* hashDest = result)
66 var hashSourceCurrent = (uint*)hashSource;
67 var hashDestCurrent = (uint*)hashDest;
68 for (
int i = 0; i < HashSizeInUInt; ++i)
69 *hashDestCurrent++ = *hashSourceCurrent++;
82 return left.Equals(right);
93 return !left.Equals(right);
104 if (input.Length != HashStringLength)
110 var hash =
new byte[HashSize];
111 for (
int i = 0; i < HashStringLength; i += 2)
114 char c2 = input[i + 1];
117 if (((digit1 = HexDigits.IndexOf(c1)) == -1)
118 || ((digit2 = HexDigits.IndexOf(c2)) == -1))
124 hash[i >> 1] = (byte)((digit1 << 4) | digit2);
135 fixed (uint* xPtr = &hash1)
138 var y1 = &other.hash1;
140 for (
int i = 0; i < HashSizeInUInt; ++i)
153 if (ReferenceEquals(null, obj))
return false;
160 fixed (uint* objPtr = &hash1)
162 var obj1 = (
int*)objPtr;
171 fixed (uint* xPtr = &hash1)
174 var y1 = &other.hash1;
176 for (
int i = 0; i < HashSizeInUInt; ++i)
178 var compareResult = (*x1++).CompareTo(*y1++);
179 if (compareResult != 0)
180 return compareResult;
189 var c =
new char[HashStringLength];
191 fixed (uint* hashStart = &hash1)
193 var hashBytes = (byte*)hashStart;
194 for (
int i = 0; i < HashStringLength; ++i)
197 var
b = ((byte)(hashBytes[index0] >> 4));
198 c[i++] = HexDigits[
b];
200 b = ((byte)(hashBytes[index0] & 0x0F));
205 return new string(c);
214 fixed (
void* hashStart = &hash1)
216 return *(Guid*)hashStart;
226 return FromBytes(Guid.NewGuid().ToByteArray());
237 if (buffer == null)
throw new ArgumentNullException(
"buffer");
239 return FromBytes(buffer, 0, buffer.Length);
252 if (buffer == null)
throw new ArgumentNullException(
"buffer");
255 builder.Write(buffer, offset,
count);
256 return builder.ComputeHash();
static ObjectId New()
News this instance.
bool Equals(ObjectId other)
override string ToString()
ObjectId(byte[] hash)
Initializes a new instance of the ObjectId struct.
override bool Equals(object obj)
Guid ToGuid()
Gets a Guid from this object identifier.
A hash to uniquely identify data.
int CompareTo(ObjectId other)
static bool TryParse(string input, out ObjectId result)
Tries to parse an ObjectId from a string.
static ObjectId FromBytes(byte[] buffer, int offset, int count)
Computes a hash from a byte buffer.
static ObjectId FromBytes(byte[] buffer)
Computes a hash from a byte buffer.
override int GetHashCode()
ObjectId(uint hash1, uint hash2, uint hash3, uint hash4)