internal static byte[] ConvertIntPtrSidToByteArraySid(IntPtr binaryForm)
{
byte[] ResultSid;
//
// Verify the revision (just sanity, should never fail to be 1)
//
byte Revision = Marshal.ReadByte(binaryForm, 0);
if (Revision != SecurityIdentifier.Revision)
{
throw new ArgumentException(SR.IdentityReference_InvalidSidRevision, nameof(binaryForm));
}
//
// Need the subauthority count in order to figure out how many bytes to read
//
byte SubAuthorityCount = Marshal.ReadByte(binaryForm, 1);
if (SubAuthorityCount < 0 ||
SubAuthorityCount > SecurityIdentifier.MaxSubAuthorities)
{
throw new ArgumentException(SR.Format(SR.IdentityReference_InvalidNumberOfSubauthorities, SecurityIdentifier.MaxSubAuthorities), nameof(binaryForm));
}
//
// Compute the size of the binary form of this SID and allocate the memory
//
int BinaryLength = 1 + 1 + 6 + SubAuthorityCount * 4;
ResultSid = new byte[BinaryLength];
//
// Extract the data from the returned pointer
//
Marshal.Copy(binaryForm, ResultSid, 0, BinaryLength);
return ResultSid;
}