Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch DirectoryControl to use AsnWriter, AsnDecoder #101512

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,6 @@ internal static partial int ldap_sasl_interactive_bind(
[LibraryImport(Libraries.OpenLdap, EntryPoint = "ldap_first_reference")]
public static partial IntPtr ldap_first_reference(ConnectionHandle ldapHandle, IntPtr result);

[LibraryImport(Libraries.OpenLdap, EntryPoint = "ldap_create_sort_control")]
public static partial int ldap_create_sort_control(ConnectionHandle handle, IntPtr keys, byte critical, ref IntPtr control);

[LibraryImport(Libraries.OpenLdap, EntryPoint = "ldap_control_free")]
public static partial int ldap_control_free(IntPtr control);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ internal static partial class Ldap
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int ldap_parse_reference(ConnectionHandle ldapHandle, IntPtr result, ref IntPtr referrals);

[LibraryImport(Libraries.Wldap32, EntryPoint = "ldap_create_sort_controlW")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int ldap_create_sort_control(ConnectionHandle handle, IntPtr keys, byte critical, ref IntPtr control);

[LibraryImport(Libraries.Wldap32, EntryPoint = "ldap_control_freeW")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int ldap_control_free(IntPtr control);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
</PropertyGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' != ''">
<Compile Include="System\DirectoryServices\Protocols\common\AsnWriterExtensions.cs" />
<Compile Include="System\DirectoryServices\Protocols\common\AuthTypes.cs" />
<Compile Include="System\DirectoryServices\Protocols\common\BerConverter.cs" />
<Compile Include="System\DirectoryServices\Protocols\common\DereferenceAlias.cs" />
Expand All @@ -51,7 +52,6 @@
<Compile Include="System\DirectoryServices\Protocols\ldap\LdapPartialResultsProcessor.cs" />
<Compile Include="System\DirectoryServices\Protocols\ldap\LdapSessionOptions.cs" />
<Compile Include="System\DirectoryServices\Protocols\Interop\SafeHandles.cs" />
<Compile Include="System\DirectoryServices\Protocols\Interop\SortKeyInterop.cs" />

<Compile Include="$(CommonPath)DisableRuntimeMarshalling.cs" Link="Common\DisableRuntimeMarshalling.cs" />
<Compile Include="$(CommonPath)Interop\Interop.Ldap.cs" Link="Common\Interop\Interop.Ldap.cs" />
Expand All @@ -64,7 +64,6 @@
<Compile Include="System\DirectoryServices\Protocols\common\QuotaControl.Windows.cs" />
<Compile Include="System\DirectoryServices\Protocols\Interop\LdapPal.Windows.cs" />
<Compile Include="System\DirectoryServices\Protocols\Interop\BerPal.Windows.cs" />
<Compile Include="System\DirectoryServices\Protocols\Interop\SortKeyInterop.Windows.cs" />
<Compile Include="System\DirectoryServices\Protocols\ldap\LdapConnection.Windows.cs" />
<Compile Include="System\DirectoryServices\Protocols\ldap\LdapSessionOptions.Windows.cs" />
<Compile Include="System\DirectoryServices\Protocols\Interop\SafeHandles.Windows.cs" />
Expand All @@ -79,7 +78,6 @@
<Compile Include="System\DirectoryServices\Protocols\common\QuotaControl.Linux.cs" />
<Compile Include="System\DirectoryServices\Protocols\Interop\LdapPal.Linux.cs" />
<Compile Include="System\DirectoryServices\Protocols\Interop\BerPal.Linux.cs" />
<Compile Include="System\DirectoryServices\Protocols\Interop\SortKeyInterop.Linux.cs" />
<Compile Include="System\DirectoryServices\Protocols\ldap\LdapConnection.Linux.cs" />
<Compile Include="System\DirectoryServices\Protocols\ldap\LdapSessionOptions.Linux.cs" />
<Compile Include="System\DirectoryServices\Protocols\ldap\LocalAppContextSwitches.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ internal static int CompareDirectoryEntries(ConnectionHandle ldapHandle, string

internal static void FreeDirectoryControls(IntPtr value) => Interop.Ldap.ldap_controls_free(value);

internal static int CreateDirectorySortControl(ConnectionHandle handle, IntPtr keys, byte critical, ref IntPtr control) => Interop.Ldap.ldap_create_sort_control(handle, keys, critical, ref control);

internal static int DeleteDirectoryEntry(ConnectionHandle ldapHandle, string dn, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber) => Interop.Ldap.ldap_delete_ext(ldapHandle, dn, servercontrol, clientcontrol, ref messageNumber);

internal static int ExtendedDirectoryOperation(ConnectionHandle ldapHandle, string oid, BerVal data, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ internal static int CompareDirectoryEntries(ConnectionHandle ldapHandle, string

internal static void FreeDirectoryControls(IntPtr value) => Interop.Ldap.ldap_controls_free(value);

internal static int CreateDirectorySortControl(ConnectionHandle handle, IntPtr keys, byte critical, ref IntPtr control) => Interop.Ldap.ldap_create_sort_control(handle, keys, critical, ref control);

internal static int DeleteDirectoryEntry(ConnectionHandle ldapHandle, string dn, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber) => Interop.Ldap.ldap_delete_ext(ldapHandle, dn, servercontrol, clientcontrol, ref messageNumber);

internal static int ExtendedDirectoryOperation(ConnectionHandle ldapHandle, string oid, BerVal data, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber) =>
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Formats.Asn1;
using System.Text;

namespace System.DirectoryServices.Protocols
{
internal static class AsnWriterExtensions
{
public static void WriteLdapString(this AsnWriter writer, string value, Encoding stringEncoding, bool mandatory = true, Asn1Tag? tag = null)
{
// A typical stack allocation threshold would be 256 bytes. A higher threshold has been chosen because an LdapString can be
// used to serialize server names. A server name is defined by RF1035, which specifies that a label in a domain name should
// be < 64 characters. If a server name is specified as an FQDN, this will be at least three labels in an AD environment -
// up to 192 characters. Doubling this to allow for Unicode encoding, then rounding to the nearest power of two yields 512.
const int StackAllocationThreshold = 512;

if (!string.IsNullOrEmpty(value))
{
int octetStringLength = stringEncoding.GetByteCount(value);
// Allocate space on the stack. There's a modest codegen advantage to a constant-value stackalloc.
Span<byte> tmpValue = octetStringLength <= StackAllocationThreshold
? stackalloc byte[StackAllocationThreshold].Slice(0, octetStringLength)
: new byte[octetStringLength];

stringEncoding.GetBytes(value, tmpValue);
writer.WriteOctetString(tmpValue, tag);
}
else if (mandatory)
{
writer.WriteOctetString([], tag);
}
}
}
}
Loading
Loading