Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Original file line number Diff line number Diff line change
Expand Up @@ -792,21 +792,44 @@ public override string ToString()
{
if (_sddlForm == null)
{
StringBuilder result = new StringBuilder();

//
// Typecasting of _IdentifierAuthority to a long below is important, since
// Typecasting of _IdentifierAuthority to a ulong below is important, since
// otherwise you would see this: "S-1-NTAuthority-32-544"
//

result.Append("S-1-").Append((long)_identifierAuthority);

#if netcoreapp20 || netstandard
StringBuilder result = new StringBuilder();
result.Append("S-1-").Append((ulong)_identifierAuthority);
for (int i = 0; i < SubAuthorityCount; i++)
{
result.Append('-').Append((uint)(_subAuthorities[i]));
}

_sddlForm = result.ToString();
#else
// length of buffer calculation
// prefix = "S-1-".Length: 4;
// authority: ulong.MaxValue.ToString("D") : 20;
// subauth = MaxSubAuthorities * ( uint.MaxValue.ToString("D").Length + '-'.Length ): 15 * (10+1): 165;
// max possible length = 4 + 20 + 165: 189
Span<char> result = stackalloc char[189];
result[0] = 'S';
result[1] = '-';
result[2] = '1';
result[3] = '-';
int written;
int length = 4;
((ulong)_identifierAuthority).TryFormat(result.Slice(length), out written);
length += written;
int[] values = _subAuthorities;
for (int index = 0; index < values.Length; index++)
{
result[length] = '-';
length += 1;
((uint)values[index]).TryFormat(result.Slice(length), out written);
length += written;
}
_sddlForm = result.Slice(0, length).ToString();
#endif
}

return _sddlForm;
Expand Down Expand Up @@ -910,9 +933,9 @@ public override IdentityReference Translate(Type targetType)
}
}

#endregion
#endregion

#region Operators
#region Operators

public static bool operator ==(SecurityIdentifier left, SecurityIdentifier right)
{
Expand All @@ -938,9 +961,9 @@ public override IdentityReference Translate(Type targetType)
return !(left == right);
}

#endregion
#endregion

#region IComparable implementation
#region IComparable implementation

public int CompareTo(SecurityIdentifier sid)
{
Expand Down Expand Up @@ -982,9 +1005,9 @@ public int CompareTo(SecurityIdentifier sid)
return 0;
}

#endregion
#endregion

#region Public Methods
#region Public Methods

internal int GetSubAuthority(int index)
{
Expand Down Expand Up @@ -1220,6 +1243,6 @@ internal static IdentityReferenceCollection Translate(IdentityReferenceCollectio

throw new ArgumentException(SR.IdentityReference_MustBeIdentityReference, nameof(targetType));
}
#endregion
#endregion
}
}