<commit_before>﻿using System;
using System.Security.Principal;
using System.Text;

namespace Disco.Services.Interop.ActiveDirectory
{
    internal static class ADHelpers
    {
        internal static byte[] ToBytes(this SecurityIdentifier Sid)
        {
            var sidBytes = new byte[Sid.BinaryLength];

            Sid.GetBinaryForm(sidBytes, 0);

            return sidBytes;
        }

        internal static SecurityIdentifier BuildPrimaryGroupSid(SecurityIdentifier UserSid, int PrimaryGroupId)
        {
            var groupSid = UserSid.ToBytes();

            int ridOffset = groupSid.Length - 4;
            int groupId = PrimaryGroupId;
            for (int i = 0; i < 4; i++)
            {
                groupSid[ridOffset + i] = (byte)(groupId & 0xFF);
                groupId >>= 8;
            }

            return new SecurityIdentifier(groupSid, 0);
        }

        internal static string ToBinaryString(this SecurityIdentifier Sid)
        {
            StringBuilder escapedSid = new StringBuilder();

            foreach (var sidByte in Sid.ToBytes())
            {
                escapedSid.Append('\\');
                escapedSid.Append(sidByte.ToString("x2"));
            }

            return escapedSid.ToString();
        }

        internal static string EscapeLdapQuery(string query)
        {
            return query.Replace("*", "\\2a").Replace("(", "\\28").Replace(")", "\\29").Replace("\\", "\\5c").Replace("NUL", "\\00").Replace("/", "\\2f");
        }
<commit_msg>Bug Fix: Ldap Query Escaping<commit_after>﻿using System;
using System.Security.Principal;
using System.Text;

namespace Disco.Services.Interop.ActiveDirectory
{
    internal static class ADHelpers
    {
        internal static byte[] ToBytes(this SecurityIdentifier Sid)
        {
            var sidBytes = new byte[Sid.BinaryLength];

            Sid.GetBinaryForm(sidBytes, 0);

            return sidBytes;
        }

        internal static SecurityIdentifier BuildPrimaryGroupSid(SecurityIdentifier UserSid, int PrimaryGroupId)
        {
            var groupSid = UserSid.ToBytes();

            int ridOffset = groupSid.Length - 4;
            int groupId = PrimaryGroupId;
            for (int i = 0; i < 4; i++)
            {
                groupSid[ridOffset + i] = (byte)(groupId & 0xFF);
                groupId >>= 8;
            }

            return new SecurityIdentifier(groupSid, 0);
        }

        internal static string ToBinaryString(this SecurityIdentifier Sid)
        {
            StringBuilder escapedSid = new StringBuilder();

            foreach (var sidByte in Sid.ToBytes())
            {
                escapedSid.Append('\\');
                escapedSid.Append(sidByte.ToString("x2"));
            }

            return escapedSid.ToString();
        }

        internal static string EscapeLdapQuery(string query)
        {
            return query
                .Replace(@"\", @"\5C")
                .Replace("*", @"\2A")
                .Replace("(", @"\28")
                .Replace(")", @"\29")
                .Replace("\0", @"\00");
        }

{'fraction_non_alphanumeric': 0.11486486486486487, 'fraction_numerical': 0.010939510939510939, 'mean_word_length': 1.7416225749559082, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5894754', 'n_tokens_mistral': 855, 'n_tokens_neox': 804, 'n_words': 192}