Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Add BlobContentId ctor that takes byte[]
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Jul 21, 2016
1 parent a4ecee9 commit 7d3c111
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/System.Reflection.Metadata/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@
<data name="HashTooShort" xml:space="preserve">
<value>Hash must be at least {0}B long.</value>
</data>
<data name="UnexpectedArrayLength" xml:space="preserve">
<value>Expected array of length {0}.</value>
</data>
<data name="ValueMustBeMultiple" xml:space="preserve">
<value>Value must be multiple of {0}.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace System.Reflection.Metadata
{
public struct BlobContentId
{
private const int Size = BlobUtilities.SizeOfGuid + sizeof(uint);

public Guid Guid { get; }
public uint Stamp { get; }

Expand All @@ -19,6 +21,31 @@ public BlobContentId(Guid guid, uint stamp)
Stamp = stamp;
}

public BlobContentId(ImmutableArray<byte> id)
: this(ImmutableByteArrayInterop.DangerousGetUnderlyingArray(id))
{
}

public unsafe BlobContentId(byte[] id)
{
if (id == null)
{
throw new ArgumentNullException(nameof(id));
}

if (id.Length != Size)
{
throw new ArgumentException(SR.Format(SR.UnexpectedArrayLength, Size), nameof(id));
}

fixed (byte* ptr = id)
{
var reader = new BlobReader(ptr, id.Length);
Guid = reader.ReadGuid();
Stamp = reader.ReadUInt32();
}
}

public bool IsDefault => Guid == default(Guid) && Stamp == 0;

public static BlobContentId FromHash(ImmutableArray<byte> hashCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,25 @@ public void Ctor()
Assert.Equal(0x12345678u, id2.Stamp);
Assert.False(id2.IsDefault);

var id3 = new BlobContentId(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14 });
Assert.Equal(new Guid("04030201-0605-0807-090a-0b0c0d0e0f10"), id3.Guid);
Assert.Equal(0x14131211u, id3.Stamp);
Assert.False(id3.IsDefault);

Assert.True(default(BlobContentId).IsDefault);
}

[Fact]
public void Ctor_Errors()
{
Assert.Throws<ArgumentNullException>("id", () => new BlobContentId(null));
Assert.Throws<ArgumentNullException>("id", () => new BlobContentId(default(ImmutableArray<byte>)));
Assert.Throws<ArgumentException>("id", () => new BlobContentId(ImmutableArray.Create<byte>()));
Assert.Throws<ArgumentException>("id", () => new BlobContentId(ImmutableArray.Create<byte>(0)));
Assert.Throws<ArgumentException>("id", () => new BlobContentId(ImmutableArray.Create<byte>(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13)));
Assert.Throws<ArgumentException>("id", () => new BlobContentId(ImmutableArray.Create<byte>(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15)));
}

[Fact]
public void FromHash()
{
Expand Down

0 comments on commit 7d3c111

Please sign in to comment.