<commit_before>﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using VCSJones.FiddlerCert.Interop;

namespace VCSJones.FiddlerCert
{
    public static class DistinguishedNameParser
    {
        public static Dictionary<string, List<string>> Parse(string distingishedName)
        {
            var result = new Dictionary<string, List<string>>(StringComparer.CurrentCultureIgnoreCase);
            var distinguishedNamePtr = IntPtr.Zero;
            try
            {
                distinguishedNamePtr = Marshal.StringToCoTaskMemUni(distingishedName);
                //We need to copy the IntPtr.
                //The copy is necessary because DsGetRdnW modifies the pointer to advance it. We need to keep
                //The original so we can free it later, otherwise we'll leak memory.
                var distinguishedNamePtrCopy = distinguishedNamePtr;
                uint pcDN = (uint)distingishedName.Length, pcKey, pcVal;
                IntPtr ppKey, ppVal;
                while (Ntdsapi.DsGetRdnW(ref distinguishedNamePtrCopy, ref pcDN, out ppKey, out pcKey, out ppVal, out pcVal) == 0)
                {
                    if (pcDN == 0)
                    {
                        break;
                    }
                    if (pcKey == 0 || pcVal == 0)
                    {
                        continue;
                    }
                    var key = Marshal.PtrToStringUni(ppKey, (int)pcKey);
                    var value = Marshal.PtrToStringUni(ppVal, (int)pcVal);
                    if (result.ContainsKey(key))
                    {
                        result[key].Add(value);
                    }<commit_msg>Fixed issue where subject was missing a component. The last component of a DN was being ignored. The pcDN == 0 check was added to handle an odd case of an infinite loop in some DNs, but this was checked prematurely because it could still be parsing the last component where there were no components left. The loop now only breaks if it things it has reached the end more than once.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using VCSJones.FiddlerCert.Interop;

namespace VCSJones.FiddlerCert
{
    public static class DistinguishedNameParser
    {
        public static Dictionary<string, List<string>> Parse(string distingishedName)
        {
            var result = new Dictionary<string, List<string>>(StringComparer.CurrentCultureIgnoreCase);
            var distinguishedNamePtr = IntPtr.Zero;
            try
            {
                distinguishedNamePtr = Marshal.StringToCoTaskMemUni(distingishedName);
                //We need to copy the IntPtr.
                //The copy is necessary because DsGetRdnW modifies the pointer to advance it. We need to keep
                //The original so we can free it later, otherwise we'll leak memory.
                var distinguishedNamePtrCopy = distinguishedNamePtr;
                uint pcDN = (uint)distingishedName.Length, pcKey, pcVal;
                IntPtr ppKey, ppVal;
                var hasZeroed = false;
                while (Ntdsapi.DsGetRdnW(ref distinguishedNamePtrCopy, ref pcDN, out ppKey, out pcKey, out ppVal, out pcVal) == 0)
                {
                    if (pcDN == 0 && hasZeroed)
                    {
                        break;
                    }
                    else if (pcDN == 0)
                    {
                        hasZeroed = true;
                    }
                    if (pcKey == 0 || pcVal == 0)
                    {
                        continue;
                    }
                    var key = Marshal.PtrToStringUni(ppKey, (int)pcKey);
                    var value = Marshal.PtrToStringUni(ppVal, (int)pcVal);
                    if (result.ContainsKey(key))
                    {
                        result[key].Add(value);
                    }
{'fraction_non_alphanumeric': 0.06860759493670886, 'fraction_numerical': 0.002531645569620253, 'mean_word_length': 1.589121887287025, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4324604', 'n_tokens_mistral': 1016, 'n_tokens_neox': 942, 'n_words': 302}