| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Collections.Immutable; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Reflection.Internal; |
| | | 8 | | |
| | | 9 | | namespace System.Reflection.Metadata |
| | | 10 | | { |
| | | 11 | | public readonly struct BlobContentId : IEquatable<BlobContentId> |
| | | 12 | | { |
| | | 13 | | private const int Size = BlobUtilities.SizeOfGuid + sizeof(uint); |
| | | 14 | | |
| | | 15 | | private readonly Guid _guid; |
| | | 16 | | private readonly uint _stamp; |
| | | 17 | | |
| | 0 | 18 | | public Guid Guid => _guid; |
| | 0 | 19 | | public uint Stamp => _stamp; |
| | | 20 | | |
| | | 21 | | public BlobContentId(Guid guid, uint stamp) |
| | 0 | 22 | | { |
| | 0 | 23 | | _guid = guid; |
| | 0 | 24 | | _stamp = stamp; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public BlobContentId(ImmutableArray<byte> id) |
| | 0 | 28 | | { |
| | 0 | 29 | | if (id.IsDefault) |
| | 0 | 30 | | { |
| | 0 | 31 | | Throw.ArgumentNull(nameof(id)); |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | Initialize(id.AsSpan(), out _guid, out _stamp); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | public BlobContentId(byte[] id) |
| | 0 | 38 | | { |
| | 0 | 39 | | if (id is null) |
| | 0 | 40 | | { |
| | 0 | 41 | | Throw.ArgumentNull(nameof(id)); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | Initialize(id, out _guid, out _stamp); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | private static unsafe void Initialize(ReadOnlySpan<byte> id, out Guid guid, out uint stamp) |
| | 0 | 48 | | { |
| | 0 | 49 | | if (id.Length != Size) |
| | 0 | 50 | | { |
| | 0 | 51 | | throw new ArgumentException(SR.Format(SR.UnexpectedArrayLength, Size), nameof(id)); |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | fixed (byte* ptr = &id[0]) |
| | 0 | 55 | | { |
| | 0 | 56 | | var reader = new BlobReader(ptr, id.Length); |
| | 0 | 57 | | guid = reader.ReadGuid(); |
| | 0 | 58 | | stamp = reader.ReadUInt32(); |
| | 0 | 59 | | } |
| | 0 | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | public bool IsDefault => Guid == default(Guid) && Stamp == 0; |
| | | 63 | | |
| | | 64 | | public static BlobContentId FromHash(ImmutableArray<byte> hashCode) |
| | 0 | 65 | | { |
| | 0 | 66 | | if (hashCode.IsDefault) |
| | 0 | 67 | | { |
| | 0 | 68 | | Throw.ArgumentNull(nameof(hashCode)); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | return FromHash(hashCode.AsSpan()); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | public static BlobContentId FromHash(byte[] hashCode) |
| | 0 | 75 | | { |
| | 0 | 76 | | if (hashCode is null) |
| | 0 | 77 | | { |
| | 0 | 78 | | Throw.ArgumentNull(nameof(hashCode)); |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | return FromHash(hashCode.AsSpan()); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | private static BlobContentId FromHash(ReadOnlySpan<byte> hashCode) |
| | 0 | 85 | | { |
| | | 86 | | const int minHashSize = 20; |
| | | 87 | | |
| | 0 | 88 | | if (hashCode.Length < minHashSize) |
| | 0 | 89 | | { |
| | 0 | 90 | | throw new ArgumentException(SR.Format(SR.HashTooShort, minHashSize), nameof(hashCode)); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | // extract guid components from input data |
| | 0 | 94 | | uint a = ((uint)hashCode[3] << 24 | (uint)hashCode[2] << 16 | (uint)hashCode[1] << 8 | hashCode[0]); |
| | 0 | 95 | | ushort b = (ushort)(hashCode[5] << 8 | hashCode[4]); |
| | 0 | 96 | | ushort c = (ushort)(hashCode[7] << 8 | hashCode[6]); |
| | 0 | 97 | | byte d = hashCode[8]; |
| | 0 | 98 | | byte e = hashCode[9]; |
| | 0 | 99 | | byte f = hashCode[10]; |
| | 0 | 100 | | byte g = hashCode[11]; |
| | 0 | 101 | | byte h = hashCode[12]; |
| | 0 | 102 | | byte i = hashCode[13]; |
| | 0 | 103 | | byte j = hashCode[14]; |
| | 0 | 104 | | byte k = hashCode[15]; |
| | | 105 | | |
| | | 106 | | // modify the guid data so it decodes to the form of a "random" guid ala rfc4122 |
| | 0 | 107 | | c = (ushort)((c & 0x0fff) | (4 << 12)); |
| | 0 | 108 | | d = (byte)((d & 0x3f) | (2 << 6)); |
| | 0 | 109 | | Guid guid = new Guid((int)a, (short)b, (short)c, d, e, f, g, h, i, j, k); |
| | | 110 | | |
| | | 111 | | // compute a random-looking stamp from the remaining bits, but with the upper bit set |
| | 0 | 112 | | uint stamp = 0x80000000u | ((uint)hashCode[19] << 24 | (uint)hashCode[18] << 16 | (uint)hashCode[17] << 8 | |
| | | 113 | | |
| | 0 | 114 | | return new BlobContentId(guid, stamp); |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | public static Func<IEnumerable<Blob>, BlobContentId> GetTimeBasedProvider() |
| | 0 | 118 | | { |
| | | 119 | | // In the PE File Header this is a "Time/Date Stamp" whose description is "Time and date |
| | | 120 | | // the file was created in seconds since January 1st 1970 00:00:00 or 0" |
| | | 121 | | // However, when we want to make it deterministic we fill it in (later) with bits from the hash of the full |
| | 0 | 122 | | uint timestamp = (uint)DateTimeOffset.UtcNow.ToUnixTimeSeconds(); |
| | 0 | 123 | | return content => new BlobContentId(Guid.NewGuid(), timestamp); |
| | 0 | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | public bool Equals(BlobContentId other) => Guid == other.Guid && Stamp == other.Stamp; |
| | 0 | 127 | | public override bool Equals([NotNullWhen(true)] object? obj) => obj is BlobContentId bcid && Equals(bcid); |
| | 0 | 128 | | public override int GetHashCode() => Hash.Combine(Stamp, Guid.GetHashCode()); |
| | 0 | 129 | | public static bool operator ==(BlobContentId left, BlobContentId right) => left.Equals(right); |
| | 0 | 130 | | public static bool operator !=(BlobContentId left, BlobContentId right) => !left.Equals(right); |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | |