コード例 #1
0
ファイル: BerConverter.cs プロジェクト: nickchal/pash
		private static byte[] DecodingByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
		{
			error = 0;
			IntPtr intPtr = (IntPtr)0;
			berval _berval = new berval();
			byte[] numArray = null;
			error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref intPtr);
			try
			{
				if (error == 0 && intPtr != (IntPtr)0)
				{
					Marshal.PtrToStructure(intPtr, _berval);
					numArray = new byte[_berval.bv_len];
					Marshal.Copy(_berval.bv_val, numArray, 0, _berval.bv_len);
				}
			}
			finally
			{
				if (intPtr != (IntPtr)0)
				{
					Wldap32.ber_bvfree(intPtr);
				}
			}
			return numArray;
		}
コード例 #2
0
        private static byte[] DecodingByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
        {
            error = 0;
            IntPtr zero      = IntPtr.Zero;
            berval structure = new berval();

            byte[] destination = null;
            error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref zero);
            try
            {
                if ((error == 0) && (zero != IntPtr.Zero))
                {
                    Marshal.PtrToStructure(zero, structure);
                    destination = new byte[structure.bv_len];
                    Marshal.Copy(structure.bv_val, destination, 0, structure.bv_len);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Wldap32.ber_bvfree(zero);
                }
            }
            return(destination);
        }
コード例 #3
0
 private static byte[] DecodingByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
 {
     error = 0;
     IntPtr zero = IntPtr.Zero;
     berval structure = new berval();
     byte[] destination = null;
     error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref zero);
     try
     {
         if ((error == 0) && (zero != IntPtr.Zero))
         {
             Marshal.PtrToStructure(zero, structure);
             destination = new byte[structure.bv_len];
             Marshal.Copy(structure.bv_val, destination, 0, structure.bv_len);
         }
     }
     finally
     {
         if (zero != IntPtr.Zero)
         {
             Wldap32.ber_bvfree(zero);
         }
     }
     return destination;
 }
コード例 #4
0
ファイル: BerConverter.cs プロジェクト: modulexcite/pash-1
        private static byte[] DecodingByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
        {
            error = 0;
            IntPtr intPtr  = (IntPtr)0;
            berval _berval = new berval();

            byte[] numArray = null;
            error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref intPtr);
            try
            {
                if (error == 0 && intPtr != (IntPtr)0)
                {
                    Marshal.PtrToStructure(intPtr, _berval);
                    numArray = new byte[_berval.bv_len];
                    Marshal.Copy(_berval.bv_val, numArray, 0, _berval.bv_len);
                }
            }
            finally
            {
                if (intPtr != (IntPtr)0)
                {
                    Wldap32.ber_bvfree(intPtr);
                }
            }
            return(numArray);
        }
コード例 #5
0
 internal SafeBerHandle(berval value) : base(true)
 {
     SetHandle(Interop.Ldap.ber_init(value));
     if (handle == IntPtr.Zero)
     {
         throw new BerConversionException();
     }
 }
コード例 #6
0
 internal BerSafeHandle(berval value) : base(true)
 {
     base.SetHandle(Wldap32.ber_init(value));
     if (base.handle == IntPtr.Zero)
     {
         throw new BerConversionException();
     }
 }
コード例 #7
0
 internal BerSafeHandle(berval value) : base(true)
 {
     SetHandle(Wldap32.ber_init(value));
     if (handle == IntPtr.Zero)
     {
         throw new BerConversionException();
     }
 }
コード例 #8
0
ファイル: BerConverter.cs プロジェクト: davzucky/corefx
        private static byte[][] DecodingMultiByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
        {
            error = 0;
            // several berval
            IntPtr    ptrResult  = (IntPtr)0;
            int       i          = 0;
            ArrayList binaryList = new ArrayList();
            IntPtr    tempPtr    = (IntPtr)0;

            byte[][] result = null;

            try
            {
                error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref ptrResult);

                if (error == 0)
                {
                    if (ptrResult != (IntPtr)0)
                    {
                        tempPtr = Marshal.ReadIntPtr(ptrResult);
                        while (tempPtr != (IntPtr)0)
                        {
                            berval ber = new berval();
                            Marshal.PtrToStructure(tempPtr, ber);

                            byte[] berArray = new byte[ber.bv_len];
                            Marshal.Copy(ber.bv_val, berArray, 0, ber.bv_len);

                            binaryList.Add(berArray);

                            i++;
                            tempPtr = Marshal.ReadIntPtr(ptrResult, i * Marshal.SizeOf(typeof(IntPtr)));
                        }

                        result = new byte[binaryList.Count][];
                        for (int j = 0; j < binaryList.Count; j++)
                        {
                            result[j] = (byte[])binaryList[j];
                        }
                    }
                }
                else
                {
                    Debug.WriteLine("ber_scanf for format character 'V' failed");
                }
            }
            finally
            {
                if (ptrResult != (IntPtr)0)
                {
                    Wldap32.ber_bvecfree(ptrResult);
                }
            }

            return(result);
        }
コード例 #9
0
        private static byte[][] DecodingMultiByteArrayHelper(SafeBerHandle berElement, char fmt, ref int error)
        {
            error = 0;
            // several berval
            IntPtr    ptrResult  = IntPtr.Zero;
            int       i          = 0;
            ArrayList binaryList = new ArrayList();
            IntPtr    tempPtr    = IntPtr.Zero;

            byte[][] result = null;

            try
            {
                error = BerPal.ScanNextPtr(berElement, new string(fmt, 1), ref ptrResult);

                if (!BerPal.IsBerDecodeError(error))
                {
                    if (ptrResult != IntPtr.Zero)
                    {
                        tempPtr = Marshal.ReadIntPtr(ptrResult);
                        while (tempPtr != IntPtr.Zero)
                        {
                            berval ber = new berval();
                            Marshal.PtrToStructure(tempPtr, ber);

                            byte[] berArray = new byte[ber.bv_len];
                            Marshal.Copy(ber.bv_val, berArray, 0, ber.bv_len);

                            binaryList.Add(berArray);

                            i++;
                            tempPtr = Marshal.ReadIntPtr(ptrResult, i * IntPtr.Size);
                        }

                        result = new byte[binaryList.Count][];
                        for (int j = 0; j < binaryList.Count; j++)
                        {
                            result[j] = (byte[])binaryList[j];
                        }
                    }
                }
                else
                {
                    Debug.WriteLine("ber_scanf for format character 'V' failed");
                }
            }
            finally
            {
                if (ptrResult != IntPtr.Zero)
                {
                    BerPal.FreeBervalArray(ptrResult);
                }
            }

            return(result);
        }
コード例 #10
0
ファイル: BerSafeHandle.cs プロジェクト: nickchal/pash
		internal BerSafeHandle(berval value) : base(true)
		{
			base.SetHandle(Wldap32.ber_init(value));
			if (this.handle != (IntPtr)0)
			{
				return;
			}
			else
			{
				throw new BerConversionException();
			}
		}
コード例 #11
0
ファイル: BerSafeHandle.cs プロジェクト: modulexcite/pash-1
 internal BerSafeHandle(berval value) : base(true)
 {
     base.SetHandle(Wldap32.ber_init(value));
     if (this.handle != (IntPtr)0)
     {
         return;
     }
     else
     {
         throw new BerConversionException();
     }
 }
コード例 #12
0
 internal SafeBerHandle(berval value) : base(true)
 {
     // In Linux if bv_val is null ber_init will segFault instead of returning IntPtr.Zero.
     // In Linux if bv_len is 0 ber_init returns a valid pointer which will then fail when trying to use it,
     // so we fail early by throwing exception if this is the case.
     if (value.bv_val == IntPtr.Zero || value.bv_len == 0)
     {
         throw new BerConversionException();
     }
     SetHandle(Interop.ber_init(value));
     if (handle == IntPtr.Zero)
     {
         throw new BerConversionException();
     }
 }
コード例 #13
0
ファイル: LdapPal.Linux.cs プロジェクト: wilfriedb/runtime
        internal static int BindToDirectory(ConnectionHandle ld, string who, string passwd)
        {
            IntPtr passwordPtr = IntPtr.Zero;
            try
            {
                passwordPtr = LdapPal.StringToPtr(passwd);
                berval passwordBerval = new berval
                {
                    bv_len = passwd.Length,
                    bv_val = passwordPtr,
                };

                return Interop.Ldap.ldap_sasl_bind(ld, who, Interop.LDAP_SASL_SIMPLE, passwordBerval, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
            }
            finally
            {
                Marshal.FreeHGlobal(passwordPtr);
            }
        }
コード例 #14
0
        private static byte[][] DecodingMultiByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
        {
            error = 0;
            IntPtr    zero = IntPtr.Zero;
            int       num  = 0;
            ArrayList list = new ArrayList();
            IntPtr    ptr  = IntPtr.Zero;

            byte[][] bufferArray = null;
            try
            {
                error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref zero);
                if ((error != 0) || !(zero != IntPtr.Zero))
                {
                    return(bufferArray);
                }
                for (ptr = Marshal.ReadIntPtr(zero); ptr != IntPtr.Zero; ptr = Marshal.ReadIntPtr(zero, num * Marshal.SizeOf(typeof(IntPtr))))
                {
                    berval structure = new berval();
                    Marshal.PtrToStructure(ptr, structure);
                    byte[] destination = new byte[structure.bv_len];
                    Marshal.Copy(structure.bv_val, destination, 0, structure.bv_len);
                    list.Add(destination);
                    num++;
                }
                bufferArray = new byte[list.Count][];
                for (int i = 0; i < list.Count; i++)
                {
                    bufferArray[i] = (byte[])list[i];
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Wldap32.ber_bvecfree(zero);
                }
            }
            return(bufferArray);
        }
コード例 #15
0
        private static byte[] DecodingByteArrayHelper(SafeBerHandle berElement, char fmt, ref int error)
        {
            error = 0;
            IntPtr result      = IntPtr.Zero;
            berval binaryValue = new berval();

            byte[] byteArray = null;

            // can't use SafeBerval here as CLR creates a SafeBerval which points to a different memory location, but when doing memory
            // deallocation, wldap has special check. So have to use IntPtr directly here.
            error = BerPal.ScanNextPtr(berElement, new string(fmt, 1), ref result);

            try
            {
                if (!BerPal.IsBerDecodeError(error))
                {
                    if (result != IntPtr.Zero)
                    {
                        Marshal.PtrToStructure(result, binaryValue);

                        byteArray = new byte[binaryValue.bv_len];
                        Marshal.Copy(binaryValue.bv_val, byteArray, 0, binaryValue.bv_len);
                    }
                }
                else
                {
                    Debug.WriteLine("ber_scanf for format character 'O' failed");
                }
            }
            finally
            {
                if (result != IntPtr.Zero)
                {
                    BerPal.FreeBerval(result);
                }
            }

            return(byteArray);
        }
コード例 #16
0
 private static byte[][] DecodingMultiByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
 {
     error = 0;
     IntPtr zero = IntPtr.Zero;
     int num = 0;
     ArrayList list = new ArrayList();
     IntPtr ptr = IntPtr.Zero;
     byte[][] bufferArray = null;
     try
     {
         error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref zero);
         if ((error != 0) || !(zero != IntPtr.Zero))
         {
             return bufferArray;
         }
         for (ptr = Marshal.ReadIntPtr(zero); ptr != IntPtr.Zero; ptr = Marshal.ReadIntPtr(zero, num * Marshal.SizeOf(typeof(IntPtr))))
         {
             berval structure = new berval();
             Marshal.PtrToStructure(ptr, structure);
             byte[] destination = new byte[structure.bv_len];
             Marshal.Copy(structure.bv_val, destination, 0, structure.bv_len);
             list.Add(destination);
             num++;
         }
         bufferArray = new byte[list.Count][];
         for (int i = 0; i < list.Count; i++)
         {
             bufferArray[i] = (byte[]) list[i];
         }
     }
     finally
     {
         if (zero != IntPtr.Zero)
         {
             Wldap32.ber_bvecfree(zero);
         }
     }
     return bufferArray;
 }
コード例 #17
0
ファイル: BerConverter.cs プロジェクト: modulexcite/pash-1
        private static byte[][] DecodingMultiByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
        {
            error = 0;
            IntPtr    intPtr     = (IntPtr)0;
            int       num        = 0;
            ArrayList arrayLists = new ArrayList();

            byte[][] item = null;
            try
            {
                error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref intPtr);
                if (error == 0 && intPtr != (IntPtr)0)
                {
                    for (IntPtr i = Marshal.ReadIntPtr(intPtr); i != (IntPtr)0; i = Marshal.ReadIntPtr(intPtr, num * Marshal.SizeOf(typeof(IntPtr))))
                    {
                        berval _berval = new berval();
                        Marshal.PtrToStructure(i, _berval);
                        byte[] numArray = new byte[_berval.bv_len];
                        Marshal.Copy(_berval.bv_val, numArray, 0, _berval.bv_len);
                        arrayLists.Add(numArray);
                        num++;
                    }
                    item = new byte[arrayLists.Count][];
                    for (int j = 0; j < arrayLists.Count; j++)
                    {
                        item[j] = (byte[])arrayLists[j];
                    }
                }
            }
            finally
            {
                if (intPtr != (IntPtr)0)
                {
                    Wldap32.ber_bvecfree(intPtr);
                }
            }
            return(item);
        }
コード例 #18
0
ファイル: BerConverter.cs プロジェクト: nickchal/pash
		private static byte[][] DecodingMultiByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
		{
			error = 0;
			IntPtr intPtr = (IntPtr)0;
			int num = 0;
			ArrayList arrayLists = new ArrayList();
			byte[][] item = null;
			try
			{
				error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref intPtr);
				if (error == 0 && intPtr != (IntPtr)0)
				{
					for (IntPtr i = Marshal.ReadIntPtr(intPtr); i != (IntPtr)0; i = Marshal.ReadIntPtr(intPtr, num * Marshal.SizeOf(typeof(IntPtr))))
					{
						berval _berval = new berval();
						Marshal.PtrToStructure(i, _berval);
						byte[] numArray = new byte[_berval.bv_len];
						Marshal.Copy(_berval.bv_val, numArray, 0, _berval.bv_len);
						arrayLists.Add(numArray);
						num++;
					}
					item = new byte[arrayLists.Count][];
					for (int j = 0; j < arrayLists.Count; j++)
					{
						item[j] = (byte[])arrayLists[j];
					}
				}
			}
			finally
			{
				if (intPtr != (IntPtr)0)
				{
					Wldap32.ber_bvecfree(intPtr);
				}
			}
			return item;
		}
コード例 #19
0
ファイル: Wldap32UnsafeMethods.cs プロジェクト: hughbe/corefx
 public static extern int ldap_extended_operation([In] ConnectionHandle ldapHandle, string oid, berval data, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber);
コード例 #20
0
ファイル: LdapConnection.cs プロジェクト: chcosta/corefx
        private int SendRequestHelper(DirectoryRequest request, ref int messageID)
        {
            IntPtr serverControlArray = (IntPtr)0;
            LdapControl[] managedServerControls = null;
            IntPtr clientControlArray = (IntPtr)0;
            LdapControl[] managedClientControls = null;

            string strValue = null;

            ArrayList ptrToFree = new ArrayList();
            LdapMod[] modifications = null;
            IntPtr modArray = (IntPtr)0;
            int addModCount = 0;

            berval berValuePtr = null;

            IntPtr searchAttributes = (IntPtr)0;
            DereferenceAlias searchAliases;
            int attributeCount = 0;

            int error = 0;

            // connect to the server first if have not done so
            if (!_connected)
            {
                Connect();
                _connected = true;
            }

            //do Bind if user has not turned off automatic bind, have not done so or there is a need to do rebind, also connectionless LDAP does not need to do bind
            if (AutoBind && (!_bounded || _needRebind) && ((LdapDirectoryIdentifier)Directory).Connectionless != true)
            {
                Debug.WriteLine("rebind occurs\n");
                Bind();
            }

            try
            {
                IntPtr controlPtr = (IntPtr)0;
                IntPtr tempPtr = (IntPtr)0;

                // build server control
                managedServerControls = BuildControlArray(request.Controls, true);
                int structSize = Marshal.SizeOf(typeof(LdapControl));

                if (managedServerControls != null)
                {
                    serverControlArray = Utility.AllocHGlobalIntPtrArray(managedServerControls.Length + 1);
                    for (int i = 0; i < managedServerControls.Length; i++)
                    {
                        controlPtr = Marshal.AllocHGlobal(structSize);
                        Marshal.StructureToPtr(managedServerControls[i], controlPtr, false);
                        tempPtr = (IntPtr)((long)serverControlArray + Marshal.SizeOf(typeof(IntPtr)) * i);
                        Marshal.WriteIntPtr(tempPtr, controlPtr);
                    }
                    tempPtr = (IntPtr)((long)serverControlArray + Marshal.SizeOf(typeof(IntPtr)) * managedServerControls.Length);
                    Marshal.WriteIntPtr(tempPtr, (IntPtr)0);
                }

                // build client control
                managedClientControls = BuildControlArray(request.Controls, false);
                if (managedClientControls != null)
                {
                    clientControlArray = Utility.AllocHGlobalIntPtrArray(managedClientControls.Length + 1);
                    for (int i = 0; i < managedClientControls.Length; i++)
                    {
                        controlPtr = Marshal.AllocHGlobal(structSize);
                        Marshal.StructureToPtr(managedClientControls[i], controlPtr, false);
                        tempPtr = (IntPtr)((long)clientControlArray + Marshal.SizeOf(typeof(IntPtr)) * i);
                        Marshal.WriteIntPtr(tempPtr, controlPtr);
                    }
                    tempPtr = (IntPtr)((long)clientControlArray + Marshal.SizeOf(typeof(IntPtr)) * managedClientControls.Length);
                    Marshal.WriteIntPtr(tempPtr, (IntPtr)0);
                }

                if (request is DeleteRequest)
                {
                    // it is an delete operation                      
                    error = Wldap32.ldap_delete_ext(ldapHandle, ((DeleteRequest)request).DistinguishedName, serverControlArray, clientControlArray, ref messageID);
                }
                else if (request is ModifyDNRequest)
                {
                    // it is a modify dn operation
                    error = Wldap32.ldap_rename(ldapHandle,
                                                 ((ModifyDNRequest)request).DistinguishedName,
                                                 ((ModifyDNRequest)request).NewName,
                                                 ((ModifyDNRequest)request).NewParentDistinguishedName,
                                                 ((ModifyDNRequest)request).DeleteOldRdn ? 1 : 0,
                                                 serverControlArray, clientControlArray, ref messageID);
                }
                else if (request is CompareRequest)
                {
                    // it is a compare request
                    DirectoryAttribute assertion = ((CompareRequest)request).Assertion;
                    if (assertion == null)
                        throw new ArgumentException(Res.GetString(Res.WrongAssertionCompare));

                    if (assertion.Count != 1)
                        throw new ArgumentException(Res.GetString(Res.WrongNumValuesCompare));

                    // process the attribute
                    byte[] byteArray = assertion[0] as byte[];
                    if (byteArray != null)
                    {
                        if (byteArray != null && byteArray.Length != 0)
                        {
                            berValuePtr = new berval();
                            berValuePtr.bv_len = byteArray.Length;
                            berValuePtr.bv_val = Marshal.AllocHGlobal(byteArray.Length);
                            Marshal.Copy(byteArray, 0, berValuePtr.bv_val, byteArray.Length);
                        }
                    }
                    else
                    {
                        strValue = assertion[0].ToString();
                    }

                    // it is a compare request
                    error = Wldap32.ldap_compare(ldapHandle,
                                                  ((CompareRequest)request).DistinguishedName,
                                                  assertion.Name,
                                                  strValue,
                                                  berValuePtr,
                                                  serverControlArray, clientControlArray, ref messageID);
                }
                else if (request is AddRequest || request is ModifyRequest)
                {
                    // build the attributes

                    if (request is AddRequest)
                        modifications = BuildAttributes(((AddRequest)request).Attributes, ptrToFree);
                    else
                        modifications = BuildAttributes(((ModifyRequest)request).Modifications, ptrToFree);

                    addModCount = (modifications == null ? 1 : modifications.Length + 1);
                    modArray = Utility.AllocHGlobalIntPtrArray(addModCount);
                    int modStructSize = Marshal.SizeOf(typeof(LdapMod));
                    int i = 0;
                    for (i = 0; i < addModCount - 1; i++)
                    {
                        controlPtr = Marshal.AllocHGlobal(modStructSize);
                        Marshal.StructureToPtr(modifications[i], controlPtr, false);
                        tempPtr = (IntPtr)((long)modArray + Marshal.SizeOf(typeof(IntPtr)) * i);
                        Marshal.WriteIntPtr(tempPtr, controlPtr);
                    }
                    tempPtr = (IntPtr)((long)modArray + Marshal.SizeOf(typeof(IntPtr)) * i);
                    Marshal.WriteIntPtr(tempPtr, (IntPtr)0);

                    if (request is AddRequest)
                    {
                        error = Wldap32.ldap_add(ldapHandle,
                                                  ((AddRequest)request).DistinguishedName,
                                                  modArray,
                                                  serverControlArray, clientControlArray, ref messageID);
                    }
                    else
                    {
                        error = Wldap32.ldap_modify(ldapHandle,
                                                     ((ModifyRequest)request).DistinguishedName,
                                                     modArray,
                                                     serverControlArray, clientControlArray, ref messageID);
                    }
                }
                else if (request is ExtendedRequest)
                {
                    string name = ((ExtendedRequest)request).RequestName;
                    byte[] val = ((ExtendedRequest)request).RequestValue;

                    // process the requestvalue
                    if (val != null && val.Length != 0)
                    {
                        berValuePtr = new berval();
                        berValuePtr.bv_len = val.Length;
                        berValuePtr.bv_val = Marshal.AllocHGlobal(val.Length);
                        Marshal.Copy(val, 0, berValuePtr.bv_val, val.Length);
                    }

                    error = Wldap32.ldap_extended_operation(ldapHandle,
                                                            name,
                                                            berValuePtr,
                                                            serverControlArray, clientControlArray, ref messageID);
                }
                else if (request is SearchRequest)
                {
                    // processing the filter
                    SearchRequest searchRequest = (SearchRequest)request;
                    object filter = searchRequest.Filter;
                    if (filter != null)
                    {
                        // LdapConnection only supports ldap filter
                        if (filter is XmlDocument)
                            throw new ArgumentException(Res.GetString(Res.InvalidLdapSearchRequestFilter));
                    }
                    string searchRequestFilter = (string)filter;

                    // processing the attributes
                    attributeCount = (searchRequest.Attributes == null ? 0 : searchRequest.Attributes.Count);
                    if (attributeCount != 0)
                    {
                        searchAttributes = Utility.AllocHGlobalIntPtrArray(attributeCount + 1);
                        int i = 0;
                        for (i = 0; i < attributeCount; i++)
                        {
                            controlPtr = Marshal.StringToHGlobalUni(searchRequest.Attributes[i]);
                            tempPtr = (IntPtr)((long)searchAttributes + Marshal.SizeOf(typeof(IntPtr)) * i);
                            Marshal.WriteIntPtr(tempPtr, controlPtr);
                        }
                        tempPtr = (IntPtr)((long)searchAttributes + Marshal.SizeOf(typeof(IntPtr)) * i);
                        Marshal.WriteIntPtr(tempPtr, (IntPtr)0);
                    }

                    // processing the scope
                    int searchScope = (int)searchRequest.Scope;

                    // processing the timelimit
                    int searchTimeLimit = (int)(searchRequest.TimeLimit.Ticks / TimeSpan.TicksPerSecond);

                    // processing the alias                    
                    searchAliases = _options.DerefAlias;
                    _options.DerefAlias = searchRequest.Aliases;

                    try
                    {
                        error = Wldap32.ldap_search(ldapHandle,
                                                     searchRequest.DistinguishedName,
                                                     searchScope,
                                                     searchRequestFilter,
                                                     searchAttributes,
                                                     searchRequest.TypesOnly,
                                                     serverControlArray,
                                                     clientControlArray,
                                                     searchTimeLimit,
                                                     searchRequest.SizeLimit,
                                                     ref messageID);
                    }
                    finally
                    {
                        // revert back
                        _options.DerefAlias = searchAliases;
                    }
                }
                else
                {
                    throw new NotSupportedException(Res.GetString(Res.InvliadRequestType));
                }

                // the asynchronous call itself timeout, this actually means that we time out the LDAP_OPT_SEND_TIMEOUT specified in the session option
                // wldap32 does not differentiate that, but the application caller actually needs this information to determin what to do with the error code
                if (error == (int)LdapError.TimeOut)
                    error = (int)LdapError.SendTimeOut;

                return error;
            }
            finally
            {
                GC.KeepAlive(modifications);

                if (serverControlArray != (IntPtr)0)
                {
                    //release the memory from the heap
                    for (int i = 0; i < managedServerControls.Length; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(serverControlArray, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (tempPtr != (IntPtr)0)
                            Marshal.FreeHGlobal(tempPtr);
                    }
                    Marshal.FreeHGlobal(serverControlArray);
                }

                if (managedServerControls != null)
                {
                    for (int i = 0; i < managedServerControls.Length; i++)
                    {
                        if (managedServerControls[i].ldctl_oid != (IntPtr)0)
                            Marshal.FreeHGlobal(managedServerControls[i].ldctl_oid);

                        if (managedServerControls[i].ldctl_value != null)
                        {
                            if (managedServerControls[i].ldctl_value.bv_val != (IntPtr)0)
                                Marshal.FreeHGlobal(managedServerControls[i].ldctl_value.bv_val);
                        }
                    }
                }

                if (clientControlArray != (IntPtr)0)
                {
                    // release the memory from the heap
                    for (int i = 0; i < managedClientControls.Length; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(clientControlArray, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (tempPtr != (IntPtr)0)
                            Marshal.FreeHGlobal(tempPtr);
                    }
                    Marshal.FreeHGlobal(clientControlArray);
                }

                if (managedClientControls != null)
                {
                    for (int i = 0; i < managedClientControls.Length; i++)
                    {
                        if (managedClientControls[i].ldctl_oid != (IntPtr)0)
                            Marshal.FreeHGlobal(managedClientControls[i].ldctl_oid);

                        if (managedClientControls[i].ldctl_value != null)
                        {
                            if (managedClientControls[i].ldctl_value.bv_val != (IntPtr)0)
                                Marshal.FreeHGlobal(managedClientControls[i].ldctl_value.bv_val);
                        }
                    }
                }

                if (modArray != (IntPtr)0)
                {
                    // release the memory from the heap
                    for (int i = 0; i < addModCount - 1; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(modArray, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (tempPtr != (IntPtr)0)
                            Marshal.FreeHGlobal(tempPtr);
                    }
                    Marshal.FreeHGlobal(modArray);
                }

                // free the pointers
                for (int x = 0; x < ptrToFree.Count; x++)
                {
                    IntPtr tempPtr = (IntPtr)ptrToFree[x];
                    Marshal.FreeHGlobal(tempPtr);
                }

                if (berValuePtr != null)
                {
                    if (berValuePtr.bv_val != (IntPtr)0)
                        Marshal.FreeHGlobal(berValuePtr.bv_val);
                }

                if (searchAttributes != (IntPtr)0)
                {
                    for (int i = 0; i < attributeCount; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(searchAttributes, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (tempPtr != (IntPtr)0)
                            Marshal.FreeHGlobal(tempPtr);
                    }
                    Marshal.FreeHGlobal(searchAttributes);
                }
            }
        }
コード例 #21
0
ファイル: LdapConnection.cs プロジェクト: chcosta/corefx
        internal DirectoryResponse ConstructResponse(int messageId, LdapOperation operation, ResultAll resultType, TimeSpan requestTimeOut, bool exceptionOnTimeOut)
        {
            int error;
            LDAP_TIMEVAL timeout = new LDAP_TIMEVAL();
            timeout.tv_sec = (int)(requestTimeOut.Ticks / TimeSpan.TicksPerSecond);
            IntPtr ldapResult = (IntPtr)0;
            DirectoryResponse response = null;

            IntPtr requestName = (IntPtr)0;
            IntPtr requestValue = (IntPtr)0;

            IntPtr entryMessage = (IntPtr)0;

            bool needAbandon = true;

            // processing for the partial results retrieval
            if (resultType != ResultAll.LDAP_MSG_ALL)
            {
                // we need to have 0 timeout as we are polling for the results and don't want to wait
                timeout.tv_sec = 0;
                timeout.tv_usec = 0;

                if (resultType == ResultAll.LDAP_MSG_POLLINGALL)
                    resultType = ResultAll.LDAP_MSG_ALL;

                // when doing partial results retrieving, if ldap_result failed, we don't do ldap_abandon here.
                needAbandon = false;
            }

            error = Wldap32.ldap_result(ldapHandle, messageId, (int)resultType, timeout, ref ldapResult);
            if (error != -1 && error != 0)
            {
                // parsing the result
                int serverError = 0;
                try
                {
                    int resulterror = 0;
                    string responseDn = null;
                    string responseMessage = null;
                    Uri[] responseReferral = null;
                    DirectoryControl[] responseControl = null;

                    // ldap_parse_result skips over messages of type LDAP_RES_SEARCH_ENTRY and LDAP_RES_SEARCH_REFERRAL
                    if (error != (int)LdapResult.LDAP_RES_SEARCH_ENTRY && error != (int)LdapResult.LDAP_RES_REFERRAL)
                        resulterror = ConstructParsedResult(ldapResult, ref serverError, ref responseDn, ref responseMessage, ref responseReferral, ref responseControl);

                    if (resulterror == 0)
                    {
                        resulterror = serverError;

                        if (error == (int)LdapResult.LDAP_RES_ADD)
                            response = new AddResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);
                        else if (error == (int)LdapResult.LDAP_RES_MODIFY)
                            response = new ModifyResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);
                        else if (error == (int)LdapResult.LDAP_RES_DELETE)
                            response = new DeleteResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);
                        else if (error == (int)LdapResult.LDAP_RES_MODRDN)
                            response = new ModifyDNResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);
                        else if (error == (int)LdapResult.LDAP_RES_COMPARE)
                            response = new CompareResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);
                        else if (error == (int)LdapResult.LDAP_RES_EXTENDED)
                        {
                            response = new ExtendedResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);
                            if (resulterror == (int)ResultCode.Success)
                            {
                                resulterror = Wldap32.ldap_parse_extended_result(ldapHandle, ldapResult, ref requestName, ref requestValue, 0 /*not free it*/);
                                if (resulterror == 0)
                                {
                                    string name = null;
                                    if (requestName != (IntPtr)0)
                                    {
                                        name = Marshal.PtrToStringUni(requestName);
                                    }

                                    berval val = null;
                                    byte[] requestValueArray = null;
                                    if (requestValue != (IntPtr)0)
                                    {
                                        val = new berval();
                                        Marshal.PtrToStructure(requestValue, val);
                                        if (val.bv_len != 0 && val.bv_val != (IntPtr)0)
                                        {
                                            requestValueArray = new byte[val.bv_len];
                                            Marshal.Copy(val.bv_val, requestValueArray, 0, val.bv_len);
                                        }
                                    }

                                    ((ExtendedResponse)response).name = name;
                                    ((ExtendedResponse)response).value = requestValueArray;
                                }
                            }
                        }
                        else if (error == (int)LdapResult.LDAP_RES_SEARCH_RESULT ||
                               error == (int)LdapResult.LDAP_RES_SEARCH_ENTRY ||
                               error == (int)LdapResult.LDAP_RES_REFERRAL)
                        {
                            response = new SearchResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);

                            //set the flag here so our partial result processor knows whether the search is done or not
                            if (error == (int)LdapResult.LDAP_RES_SEARCH_RESULT)
                            {
                                ((SearchResponse)response).searchDone = true;
                            }

                            SearchResultEntryCollection searchResultEntries = new SearchResultEntryCollection();
                            SearchResultReferenceCollection searchResultReferences = new SearchResultReferenceCollection();

                            // parsing the resultentry
                            entryMessage = Wldap32.ldap_first_entry(ldapHandle, ldapResult);

                            int entrycount = 0;
                            while (entryMessage != (IntPtr)0)
                            {
                                SearchResultEntry entry = ConstructEntry(entryMessage);
                                if (entry != null)
                                    searchResultEntries.Add(entry);

                                entrycount++;
                                entryMessage = Wldap32.ldap_next_entry(ldapHandle, entryMessage);
                            }

                            // parsing the reference
                            IntPtr referenceMessage = Wldap32.ldap_first_reference(ldapHandle, ldapResult);

                            while (referenceMessage != (IntPtr)0)
                            {
                                SearchResultReference reference = ConstructReference(referenceMessage);
                                if (reference != null)
                                    searchResultReferences.Add(reference);

                                referenceMessage = Wldap32.ldap_next_reference(ldapHandle, referenceMessage);
                            }

                            ((SearchResponse)response).SetEntries(searchResultEntries);
                            ((SearchResponse)response).SetReferences(searchResultReferences);
                        }

                        if (resulterror != (int)ResultCode.Success && resulterror != (int)ResultCode.CompareFalse && resulterror != (int)ResultCode.CompareTrue && resulterror != (int)ResultCode.Referral && resulterror != (int)ResultCode.ReferralV2)
                        {
                            // throw operation exception                   
                            if (Utility.IsResultCode((ResultCode)resulterror))
                            {
                                throw new DirectoryOperationException(response, OperationErrorMappings.MapResultCode(resulterror));
                            }
                            else
                                // should not occur
                                throw new DirectoryOperationException(response);
                        }

                        return response;
                    }
                    else
                    {
                        // fall over, throw the exception beow
                        error = resulterror;
                    }
                }
                finally
                {
                    if (requestName != (IntPtr)0)
                        Wldap32.ldap_memfree(requestName);

                    if (requestValue != (IntPtr)0)
                        Wldap32.ldap_memfree(requestValue);

                    if (ldapResult != (IntPtr)0)
                    {
                        Wldap32.ldap_msgfree(ldapResult);
                    }
                }
            }
            else
            {
                // ldap_result failed
                if (error == 0)
                {
                    if (exceptionOnTimeOut)
                    {
                        // client side timeout                        
                        error = (int)LdapError.TimeOut;
                    }
                    else
                    {
                        // if we don't throw exception on time out (notification search for example), we just return empty resposne
                        return null;
                    }
                }
                else
                {
                    error = Wldap32.LdapGetLastError();
                }

                // abandon the request
                if (needAbandon)
                    Wldap32.ldap_abandon(ldapHandle, messageId);
            }

            // throw proper exception here            
            throw ConstructException(error, operation);
        }
コード例 #22
0
ファイル: LdapConnection.cs プロジェクト: nickchal/pash
		internal DirectoryAttribute ConstructAttribute(IntPtr entryMessage, IntPtr attributeName)
		{
			DirectoryAttribute directoryAttribute = new DirectoryAttribute();
			directoryAttribute.isSearchResult = true;
			string stringUni = Marshal.PtrToStringUni(attributeName);
			directoryAttribute.Name = stringUni;
			IntPtr intPtr = Wldap32.ldap_get_values_len(this.ldapHandle, entryMessage, stringUni);
			try
			{
				int num = 0;
				if (intPtr != (IntPtr)0)
				{
					for (IntPtr i = Marshal.ReadIntPtr(intPtr, Marshal.SizeOf(typeof(IntPtr)) * num); i != (IntPtr)0; i = Marshal.ReadIntPtr(intPtr, Marshal.SizeOf(typeof(IntPtr)) * num))
					{
						berval _berval = new berval();
						Marshal.PtrToStructure(i, _berval);
						if (_berval.bv_len > 0 && _berval.bv_val != (IntPtr)0)
						{
							byte[] numArray = new byte[_berval.bv_len];
							Marshal.Copy(_berval.bv_val, numArray, 0, _berval.bv_len);
							directoryAttribute.Add(numArray);
						}
						num++;
					}
				}
			}
			finally
			{
				if (intPtr != (IntPtr)0)
				{
					Wldap32.ldap_value_free_len(intPtr);
				}
			}
			return directoryAttribute;
		}
コード例 #23
0
 public static extern IntPtr ber_init(berval value);
コード例 #24
0
 public static extern int ldap_compare([In] ConnectionHandle ldapHandle, string dn, string attributeName, string strValue, berval binaryValue, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber);
コード例 #25
0
        internal static object[] TryDecode(string format, byte[] value, out bool decodeSucceeded)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            Debug.WriteLine("Begin decoding");

            UTF8Encoding  utf8Encoder = new UTF8Encoding(false, true);
            berval        berValue    = new berval();
            ArrayList     resultList  = new ArrayList();
            SafeBerHandle berElement  = null;

            object[] decodeResult = null;
            decodeSucceeded = false;

            if (value == null)
            {
                berValue.bv_len = 0;
                berValue.bv_val = IntPtr.Zero;
            }
            else
            {
                berValue.bv_len = value.Length;
                berValue.bv_val = Marshal.AllocHGlobal(value.Length);
                Marshal.Copy(value, 0, berValue.bv_val, value.Length);
            }

            try
            {
                berElement = new SafeBerHandle(berValue);
            }
            finally
            {
                if (berValue.bv_val != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(berValue.bv_val);
                }
            }

            int error = 0;

            for (int formatCount = 0; formatCount < format.Length; formatCount++)
            {
                char fmt = format[formatCount];
                if (fmt == '{' || fmt == '}' || fmt == '[' || fmt == ']' || fmt == 'n' || fmt == 'x')
                {
                    error = BerPal.ScanNext(berElement, new string(fmt, 1));

                    if (BerPal.IsBerDecodeError(error))
                    {
                        Debug.WriteLine("ber_scanf for {, }, [, ], n or x failed");
                    }
                }
                else if (fmt == 'i' || fmt == 'e' || fmt == 'b')
                {
                    int result = 0;
                    error = BerPal.ScanNextInt(berElement, new string(fmt, 1), ref result);

                    if (!BerPal.IsBerDecodeError(error))
                    {
                        if (fmt == 'b')
                        {
                            // should return a bool
                            bool boolResult = false;
                            if (result == 0)
                            {
                                boolResult = false;
                            }
                            else
                            {
                                boolResult = true;
                            }
                            resultList.Add(boolResult);
                        }
                        else
                        {
                            resultList.Add(result);
                        }
                    }
                    else
                    {
                        Debug.WriteLine("ber_scanf for format character 'i', 'e' or 'b' failed");
                    }
                }
                else if (fmt == 'a')
                {
                    // return a string
                    byte[] byteArray = DecodingByteArrayHelper(berElement, 'O', ref error);
                    if (!BerPal.IsBerDecodeError(error))
                    {
                        string s = null;
                        if (byteArray != null)
                        {
                            s = utf8Encoder.GetString(byteArray);
                        }

                        resultList.Add(s);
                    }
                }
                else if (fmt == 'O')
                {
                    // return berval
                    byte[] byteArray = DecodingByteArrayHelper(berElement, fmt, ref error);
                    if (!BerPal.IsBerDecodeError(error))
                    {
                        // add result to the list
                        resultList.Add(byteArray);
                    }
                }
                else if (fmt == 'B')
                {
                    error = DecodeBitStringHelper(resultList, berElement);
                }
                else if (fmt == 'v')
                {
                    //null terminate strings
                    byte[][] byteArrayresult = null;
                    string[] stringArray     = null;

                    byteArrayresult = DecodingMultiByteArrayHelper(berElement, 'V', ref error);
                    if (!BerPal.IsBerDecodeError(error))
                    {
                        if (byteArrayresult != null)
                        {
                            stringArray = new string[byteArrayresult.Length];
                            for (int i = 0; i < byteArrayresult.Length; i++)
                            {
                                if (byteArrayresult[i] == null)
                                {
                                    stringArray[i] = null;
                                }
                                else
                                {
                                    stringArray[i] = utf8Encoder.GetString(byteArrayresult[i]);
                                }
                            }
                        }

                        resultList.Add(stringArray);
                    }
                }
                else if (fmt == 'V')
                {
                    byte[][] result = null;

                    result = DecodingMultiByteArrayHelper(berElement, fmt, ref error);
                    if (!BerPal.IsBerDecodeError(error))
                    {
                        resultList.Add(result);
                    }
                }
                else
                {
                    Debug.WriteLine("Format string contains undefined character\n");
                    throw new ArgumentException(SR.BerConverterUndefineChar);
                }

                if (BerPal.IsBerDecodeError(error))
                {
                    // decode failed, just return
                    return(decodeResult);
                }
            }

            decodeResult = new object[resultList.Count];
            for (int count = 0; count < resultList.Count; count++)
            {
                decodeResult[count] = resultList[count];
            }

            decodeSucceeded = true;
            return(decodeResult);
        }
コード例 #26
0
ファイル: BerConverter.cs プロジェクト: modulexcite/pash-1
        public static byte[] Encode(string format, object[] value)
        {
            int num;

            Utility.CheckOSVersion();
            if (format != null)
            {
                UTF8Encoding uTF8Encoding = new UTF8Encoding();
                byte[]       numArray     = null;
                if (value == null)
                {
                    value = new object[0];
                }
                BerSafeHandle berSafeHandle = new BerSafeHandle();
                int           num1          = 0;
                int           num2          = 0;
                int           num3          = 0;
                while (num3 < format.Length)
                {
                    char chr = format[num3];
                    if (chr == '{' || chr == '}' || chr == '[' || chr == ']' || chr == 'n')
                    {
                        num2 = Wldap32.ber_printf_emptyarg(berSafeHandle, new string(chr, 1));
                    }
                    else
                    {
                        if (chr == 't' || chr == 'i' || chr == 'e')
                        {
                            if (num1 < (int)value.Length)
                            {
                                if (value[num1] is int)
                                {
                                    num2 = Wldap32.ber_printf_int(berSafeHandle, new string(chr, 1), (int)value[num1]);
                                    num1++;
                                }
                                else
                                {
                                    throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                                }
                            }
                            else
                            {
                                throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                            }
                        }
                        else
                        {
                            if (chr != 'b')
                            {
                                if (chr != 's')
                                {
                                    if (chr == 'o' || chr == 'X')
                                    {
                                        if (num1 < (int)value.Length)
                                        {
                                            if (value[num1] == null || value[num1] as byte[] != null)
                                            {
                                                byte[] numArray1 = (byte[])value[num1];
                                                num2 = BerConverter.EncodingByteArrayHelper(berSafeHandle, numArray1, chr);
                                                num1++;
                                            }
                                            else
                                            {
                                                throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                                            }
                                        }
                                        else
                                        {
                                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                                        }
                                    }
                                    else
                                    {
                                        if (chr != 'v')
                                        {
                                            if (chr != 'V')
                                            {
                                                throw new ArgumentException(Res.GetString("BerConverterUndefineChar"));
                                            }
                                            else
                                            {
                                                if (num1 < (int)value.Length)
                                                {
                                                    if (value[num1] == null || value[num1] as byte[][] != null)
                                                    {
                                                        byte[][] numArray2 = (byte[][])value[num1];
                                                        num2 = BerConverter.EncodingMultiByteArrayHelper(berSafeHandle, numArray2, chr);
                                                        num1++;
                                                    }
                                                    else
                                                    {
                                                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                                                    }
                                                }
                                                else
                                                {
                                                    throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (num1 < (int)value.Length)
                                            {
                                                if (value[num1] == null || value[num1] as string[] != null)
                                                {
                                                    string[] strArrays = (string[])value[num1];
                                                    byte[][] bytes     = null;
                                                    if (strArrays != null)
                                                    {
                                                        bytes = new byte[(int)strArrays.Length][];
                                                        for (int i = 0; i < (int)strArrays.Length; i++)
                                                        {
                                                            string str = strArrays[i];
                                                            if (str != null)
                                                            {
                                                                bytes[i] = uTF8Encoding.GetBytes(str);
                                                            }
                                                            else
                                                            {
                                                                bytes[i] = null;
                                                            }
                                                        }
                                                    }
                                                    num2 = BerConverter.EncodingMultiByteArrayHelper(berSafeHandle, bytes, 'V');
                                                    num1++;
                                                }
                                                else
                                                {
                                                    throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                                                }
                                            }
                                            else
                                            {
                                                throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (num1 < (int)value.Length)
                                    {
                                        if (value[num1] == null || value[num1] as string != null)
                                        {
                                            byte[] bytes1 = null;
                                            if (value[num1] != null)
                                            {
                                                bytes1 = uTF8Encoding.GetBytes((string)value[num1]);
                                            }
                                            num2 = BerConverter.EncodingByteArrayHelper(berSafeHandle, bytes1, 'o');
                                            num1++;
                                        }
                                        else
                                        {
                                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                                        }
                                    }
                                    else
                                    {
                                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                                    }
                                }
                            }
                            else
                            {
                                if (num1 < (int)value.Length)
                                {
                                    if (value[num1] is bool)
                                    {
                                        BerSafeHandle berSafeHandle1 = berSafeHandle;
                                        string        str1           = new string(chr, 1);
                                        if ((bool)value[num1])
                                        {
                                            num = 1;
                                        }
                                        else
                                        {
                                            num = 0;
                                        }
                                        num2 = Wldap32.ber_printf_int(berSafeHandle1, str1, num);
                                        num1++;
                                    }
                                    else
                                    {
                                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                                    }
                                }
                                else
                                {
                                    throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                                }
                            }
                        }
                    }
                    if (num2 != -1)
                    {
                        num3++;
                    }
                    else
                    {
                        throw new BerConversionException();
                    }
                }
                berval _berval = new berval();
                IntPtr intPtr  = (IntPtr)0;
                try
                {
                    num2 = Wldap32.ber_flatten(berSafeHandle, ref intPtr);
                    if (num2 != -1)
                    {
                        if (intPtr != (IntPtr)0)
                        {
                            Marshal.PtrToStructure(intPtr, _berval);
                        }
                        if (_berval == null || _berval.bv_len == 0)
                        {
                            numArray = new byte[0];
                        }
                        else
                        {
                            numArray = new byte[_berval.bv_len];
                            Marshal.Copy(_berval.bv_val, numArray, 0, _berval.bv_len);
                        }
                    }
                    else
                    {
                        throw new BerConversionException();
                    }
                }
                finally
                {
                    if (intPtr != (IntPtr)0)
                    {
                        Wldap32.ber_bvfree(intPtr);
                    }
                }
                return(numArray);
            }
            else
            {
                throw new ArgumentNullException("format");
            }
        }
コード例 #27
0
        public override byte[] GetValue()
        {
            IntPtr zero   = IntPtr.Zero;
            int    cb     = Marshal.SizeOf(typeof(SortKey));
            int    length = this.keys.Length;
            IntPtr keys   = Marshal.AllocHGlobal((int)(Marshal.SizeOf(typeof(IntPtr)) * (length + 1)));

            try
            {
                IntPtr ptr   = IntPtr.Zero;
                IntPtr ptr4  = IntPtr.Zero;
                int    index = 0;
                index = 0;
                while (index < length)
                {
                    ptr4 = Marshal.AllocHGlobal(cb);
                    Marshal.StructureToPtr(this.keys[index], ptr4, false);
                    ptr = (IntPtr)(((long)keys) + (Marshal.SizeOf(typeof(IntPtr)) * index));
                    Marshal.WriteIntPtr(ptr, ptr4);
                    index++;
                }
                ptr = (IntPtr)(((long)keys) + (Marshal.SizeOf(typeof(IntPtr)) * index));
                Marshal.WriteIntPtr(ptr, IntPtr.Zero);
                bool isCritical = base.IsCritical;
                int  errorCode  = Wldap32.ldap_create_sort_control(UtilityHandle.GetHandle(), keys, isCritical ? ((byte)1) : ((byte)0), ref zero);
                if (errorCode != 0)
                {
                    if (Utility.IsLdapError((LdapError)errorCode))
                    {
                        string message = LdapErrorMappings.MapResultCode(errorCode);
                        throw new LdapException(errorCode, message);
                    }
                    throw new LdapException(errorCode);
                }
                LdapControl structure = new LdapControl();
                Marshal.PtrToStructure(zero, structure);
                berval berval = structure.ldctl_value;
                base.directoryControlValue = null;
                if (berval != null)
                {
                    base.directoryControlValue = new byte[berval.bv_len];
                    Marshal.Copy(berval.bv_val, base.directoryControlValue, 0, berval.bv_len);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Wldap32.ldap_control_free(zero);
                }
                if (keys != IntPtr.Zero)
                {
                    for (int i = 0; i < length; i++)
                    {
                        IntPtr ptr5 = Marshal.ReadIntPtr(keys, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (ptr5 != IntPtr.Zero)
                        {
                            IntPtr hglobal = Marshal.ReadIntPtr(ptr5);
                            if (hglobal != IntPtr.Zero)
                            {
                                Marshal.FreeHGlobal(hglobal);
                            }
                            hglobal = Marshal.ReadIntPtr(ptr5, Marshal.SizeOf(typeof(IntPtr)));
                            if (hglobal != IntPtr.Zero)
                            {
                                Marshal.FreeHGlobal(hglobal);
                            }
                            Marshal.FreeHGlobal(ptr5);
                        }
                    }
                    Marshal.FreeHGlobal(keys);
                }
            }
            return(base.GetValue());
        }
コード例 #28
0
ファイル: BerConverter.cs プロジェクト: chcosta/corefx
        private static byte[][] DecodingMultiByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
        {
            error = 0;
            // several berval
            IntPtr ptrResult = (IntPtr)0;
            int i = 0;
            ArrayList binaryList = new ArrayList();
            IntPtr tempPtr = (IntPtr)0;
            byte[][] result = null;

            try
            {
                error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref ptrResult);

                if (error == 0)
                {
                    if (ptrResult != (IntPtr)0)
                    {
                        tempPtr = Marshal.ReadIntPtr(ptrResult);
                        while (tempPtr != (IntPtr)0)
                        {
                            berval ber = new berval();
                            Marshal.PtrToStructure(tempPtr, ber);

                            byte[] berArray = new byte[ber.bv_len];
                            Marshal.Copy(ber.bv_val, berArray, 0, ber.bv_len);

                            binaryList.Add(berArray);

                            i++;
                            tempPtr = Marshal.ReadIntPtr(ptrResult, i * Marshal.SizeOf(typeof(IntPtr)));
                        }

                        result = new byte[binaryList.Count][];
                        for (int j = 0; j < binaryList.Count; j++)
                        {
                            result[j] = (byte[])binaryList[j];
                        }
                    }
                }
                else
                    Debug.WriteLine("ber_scanf for format character 'V' failed");
            }
            finally
            {
                if (ptrResult != (IntPtr)0)
                {
                    Wldap32.ber_bvecfree(ptrResult);
                }
            }

            return result;
        }
コード例 #29
0
ファイル: BerConverter.cs プロジェクト: chcosta/corefx
        private static byte[] DecodingByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
        {
            error = 0;
            IntPtr result = (IntPtr)0;
            berval binaryValue = new berval();
            byte[] byteArray = null;

            // can't use SafeBerval here as CLR creates a SafeBerval which points to a different memory location, but when doing memory
            // deallocation, wldap has special check. So have to use IntPtr directly here.
            error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref result);

            try
            {
                if (error == 0)
                {
                    if (result != (IntPtr)0)
                    {
                        Marshal.PtrToStructure(result, binaryValue);

                        byteArray = new byte[binaryValue.bv_len];
                        Marshal.Copy(binaryValue.bv_val, byteArray, 0, binaryValue.bv_len);
                    }
                }
                else
                    Debug.WriteLine("ber_scanf for format character 'O' failed");
            }
            finally
            {
                if (result != (IntPtr)0)
                    Wldap32.ber_bvfree(result);
            }

            return byteArray;
        }
コード例 #30
0
ファイル: BerConverter.cs プロジェクト: chcosta/corefx
        internal static object[] TryDecode(string format, byte[] value, out bool decodeSucceeded)
        {
            Utility.CheckOSVersion();

            if (format == null)
                throw new ArgumentNullException("format");

            Debug.WriteLine("Begin decoding\n");

            UTF8Encoding utf8Encoder = new UTF8Encoding(false, true);
            berval berValue = new berval();
            ArrayList resultList = new ArrayList();
            BerSafeHandle berElement = null;

            object[] decodeResult = null;
            decodeSucceeded = false;

            if (value == null)
            {
                berValue.bv_len = 0;
                berValue.bv_val = (IntPtr)0;
            }
            else
            {
                berValue.bv_len = value.Length;
                berValue.bv_val = Marshal.AllocHGlobal(value.Length);
                Marshal.Copy(value, 0, berValue.bv_val, value.Length);
            }

            try
            {
                berElement = new BerSafeHandle(berValue);
            }
            finally
            {
                if (berValue.bv_val != (IntPtr)0)
                    Marshal.FreeHGlobal(berValue.bv_val);
            }

            int error = 0;

            for (int formatCount = 0; formatCount < format.Length; formatCount++)
            {
                char fmt = format[formatCount];
                if (fmt == '{' || fmt == '}' || fmt == '[' || fmt == ']' || fmt == 'n' || fmt == 'x')
                {
                    error = Wldap32.ber_scanf(berElement, new string(fmt, 1));

                    if (error != 0)
                        Debug.WriteLine("ber_scanf for {, }, [, ], n or x failed");
                }
                else if (fmt == 'i' || fmt == 'e' || fmt == 'b')
                {
                    int result = 0;
                    error = Wldap32.ber_scanf_int(berElement, new string(fmt, 1), ref result);

                    if (error == 0)
                    {
                        if (fmt == 'b')
                        {
                            // should return a bool
                            bool boolResult = false;
                            if (result == 0)
                                boolResult = false;
                            else
                                boolResult = true;
                            resultList.Add(boolResult);
                        }
                        else
                        {
                            resultList.Add(result);
                        }
                    }
                    else
                        Debug.WriteLine("ber_scanf for format character 'i', 'e' or 'b' failed");
                }
                else if (fmt == 'a')
                {
                    // return a string
                    byte[] byteArray = DecodingByteArrayHelper(berElement, 'O', ref error);
                    if (error == 0)
                    {
                        string s = null;
                        if (byteArray != null)
                            s = utf8Encoder.GetString(byteArray);

                        resultList.Add(s);
                    }
                }
                else if (fmt == 'O')
                {
                    // return berval                   
                    byte[] byteArray = DecodingByteArrayHelper(berElement, fmt, ref error);
                    if (error == 0)
                    {
                        // add result to the list
                        resultList.Add(byteArray);
                    }
                }
                else if (fmt == 'B')
                {
                    // return a bitstring and its length
                    IntPtr ptrResult = (IntPtr)0;
                    int length = 0;
                    error = Wldap32.ber_scanf_bitstring(berElement, "B", ref ptrResult, ref length);

                    if (error == 0)
                    {
                        byte[] byteArray = null;
                        if (ptrResult != (IntPtr)0)
                        {
                            byteArray = new byte[length];
                            Marshal.Copy(ptrResult, byteArray, 0, length);
                        }
                        resultList.Add(byteArray);
                    }
                    else
                        Debug.WriteLine("ber_scanf for format character 'B' failed");

                    // no need to free memory as wldap32 returns the original pointer instead of a duplicating memory pointer that
                    // needs to be freed
                }
                else if (fmt == 'v')
                {
                    //null terminate strings
                    byte[][] byteArrayresult = null;
                    string[] stringArray = null;

                    byteArrayresult = DecodingMultiByteArrayHelper(berElement, 'V', ref error);
                    if (error == 0)
                    {
                        if (byteArrayresult != null)
                        {
                            stringArray = new string[byteArrayresult.Length];
                            for (int i = 0; i < byteArrayresult.Length; i++)
                            {
                                if (byteArrayresult[i] == null)
                                {
                                    stringArray[i] = null;
                                }
                                else
                                {
                                    stringArray[i] = utf8Encoder.GetString(byteArrayresult[i]);
                                }
                            }
                        }

                        resultList.Add(stringArray);
                    }
                }
                else if (fmt == 'V')
                {
                    byte[][] result = null;

                    result = DecodingMultiByteArrayHelper(berElement, fmt, ref error);
                    if (error == 0)
                    {
                        resultList.Add(result);
                    }
                }
                else
                {
                    Debug.WriteLine("Format string contains undefined character\n");
                    throw new ArgumentException(Res.GetString(Res.BerConverterUndefineChar));
                }

                if (error != 0)
                {
                    // decode failed, just return
                    return decodeResult;
                }
            }

            decodeResult = new object[resultList.Count];
            for (int count = 0; count < resultList.Count; count++)
            {
                decodeResult[count] = resultList[count];
            }

            decodeSucceeded = true;
            return decodeResult;
        }
コード例 #31
0
ファイル: BerConverter.cs プロジェクト: chcosta/corefx
        public static byte[] Encode(string format, params object[] value)
        {
            Utility.CheckOSVersion();

            if (format == null)
                throw new ArgumentNullException("format");

            // no need to turn on invalid encoding detection as we just do string->byte[] conversion.
            UTF8Encoding utf8Encoder = new UTF8Encoding();
            byte[] encodingResult = null;
            // value is allowed to be null in certain scenario, so if it is null, just set it to empty array.
            if (value == null)
                value = new object[0];

            Debug.WriteLine("Begin encoding\n");

            // allocate the berelement
            BerSafeHandle berElement = new BerSafeHandle();

            int valueCount = 0;
            int error = 0;
            for (int formatCount = 0; formatCount < format.Length; formatCount++)
            {
                char fmt = format[formatCount];
                if (fmt == '{' || fmt == '}' || fmt == '[' || fmt == ']' || fmt == 'n')
                {
                    // no argument needed
                    error = Wldap32.ber_printf_emptyarg(berElement, new string(fmt, 1));
                }
                else if (fmt == 't' || fmt == 'i' || fmt == 'e')
                {
                    if (valueCount >= value.Length)
                    {
                        // we don't have enough argument for the format string
                        Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
                        throw new ArgumentException(Res.GetString(Res.BerConverterNotMatch));
                    }

                    if (!(value[valueCount] is int))
                    {
                        // argument is wrong                                                                        
                        Debug.WriteLine("type should be int\n");
                        throw new ArgumentException(Res.GetString(Res.BerConverterNotMatch));
                    }

                    // one int argument
                    error = Wldap32.ber_printf_int(berElement, new string(fmt, 1), (int)value[valueCount]);

                    // increase the value count
                    valueCount++;
                }
                else if (fmt == 'b')
                {
                    if (valueCount >= value.Length)
                    {
                        // we don't have enough argument for the format string
                        Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
                        throw new ArgumentException(Res.GetString(Res.BerConverterNotMatch));
                    }

                    if (!(value[valueCount] is bool))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be boolean\n");
                        throw new ArgumentException(Res.GetString(Res.BerConverterNotMatch));
                    }

                    // one int argument                    
                    error = Wldap32.ber_printf_int(berElement, new string(fmt, 1), (bool)value[valueCount] ? 1 : 0);

                    // increase the value count
                    valueCount++;
                }
                else if (fmt == 's')
                {
                    if (valueCount >= value.Length)
                    {
                        // we don't have enough argument for the format string
                        Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
                        throw new ArgumentException(Res.GetString(Res.BerConverterNotMatch));
                    }

                    if (value[valueCount] != null && !(value[valueCount] is string))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be string, but receiving value has type of ");
                        Debug.WriteLine(value[valueCount].GetType());
                        throw new ArgumentException(Res.GetString(Res.BerConverterNotMatch));
                    }

                    // one string argument       
                    byte[] tempValue = null;
                    if (value[valueCount] != null)
                    {
                        tempValue = utf8Encoder.GetBytes((string)value[valueCount]);
                    }
                    error = EncodingByteArrayHelper(berElement, tempValue, 'o');

                    // increase the value count
                    valueCount++;
                }
                else if (fmt == 'o' || fmt == 'X')
                {
                    // we need to have one arguments
                    if (valueCount >= value.Length)
                    {
                        // we don't have enough argument for the format string
                        Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
                        throw new ArgumentException(Res.GetString(Res.BerConverterNotMatch));
                    }

                    if (value[valueCount] != null && !(value[valueCount] is byte[]))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be byte[], but receiving value has type of ");
                        Debug.WriteLine(value[valueCount].GetType());
                        throw new ArgumentException(Res.GetString(Res.BerConverterNotMatch));
                    }

                    byte[] tempValue = (byte[])value[valueCount];
                    error = EncodingByteArrayHelper(berElement, tempValue, fmt);

                    valueCount++;
                }
                else if (fmt == 'v')
                {
                    // we need to have one arguments
                    if (valueCount >= value.Length)
                    {
                        // we don't have enough argument for the format string
                        Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
                        throw new ArgumentException(Res.GetString(Res.BerConverterNotMatch));
                    }

                    if (value[valueCount] != null && !(value[valueCount] is string[]))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be string[], but receiving value has type of ");
                        Debug.WriteLine(value[valueCount].GetType());
                        throw new ArgumentException(Res.GetString(Res.BerConverterNotMatch));
                    }

                    string[] stringValues = (string[])value[valueCount];
                    byte[][] tempValues = null;
                    if (stringValues != null)
                    {
                        tempValues = new byte[stringValues.Length][];
                        for (int i = 0; i < stringValues.Length; i++)
                        {
                            string s = stringValues[i];
                            if (s == null)
                                tempValues[i] = null;
                            else
                            {
                                tempValues[i] = utf8Encoder.GetBytes(s);
                            }
                        }
                    }

                    error = EncodingMultiByteArrayHelper(berElement, tempValues, 'V');

                    valueCount++;
                }
                else if (fmt == 'V')
                {
                    // we need to have one arguments
                    if (valueCount >= value.Length)
                    {
                        // we don't have enough argument for the format string
                        Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
                        throw new ArgumentException(Res.GetString(Res.BerConverterNotMatch));
                    }

                    if (value[valueCount] != null && !(value[valueCount] is byte[][]))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be byte[][], but receiving value has type of ");
                        Debug.WriteLine(value[valueCount].GetType());
                        throw new ArgumentException(Res.GetString(Res.BerConverterNotMatch));
                    }

                    byte[][] tempValue = (byte[][])value[valueCount];

                    error = EncodingMultiByteArrayHelper(berElement, tempValue, fmt);

                    valueCount++;
                }
                else
                {
                    Debug.WriteLine("Format string contains undefined character: ");
                    Debug.WriteLine(new string(fmt, 1));
                    throw new ArgumentException(Res.GetString(Res.BerConverterUndefineChar));
                }

                // process the return value
                if (error == -1)
                {
                    Debug.WriteLine("ber_printf failed\n");
                    throw new BerConversionException();
                }
            }

            // get the binary value back
            berval binaryValue = new berval();
            IntPtr flattenptr = (IntPtr)0;

            try
            {
                // can't use SafeBerval here as CLR creates a SafeBerval which points to a different memory location, but when doing memory
                // deallocation, wldap has special check. So have to use IntPtr directly here.
                error = Wldap32.ber_flatten(berElement, ref flattenptr);

                if (error == -1)
                {
                    Debug.WriteLine("ber_flatten failed\n");
                    throw new BerConversionException();
                }

                if (flattenptr != (IntPtr)0)
                {
                    Marshal.PtrToStructure(flattenptr, binaryValue);
                }

                if (binaryValue == null || binaryValue.bv_len == 0)
                {
                    encodingResult = new byte[0];
                }
                else
                {
                    encodingResult = new byte[binaryValue.bv_len];

                    Marshal.Copy(binaryValue.bv_val, encodingResult, 0, binaryValue.bv_len);
                }
            }
            finally
            {
                if (flattenptr != (IntPtr)0)
                    Wldap32.ber_bvfree(flattenptr);
            }

            return encodingResult;
        }
コード例 #32
0
 internal DirectoryAttribute ConstructAttribute(IntPtr entryMessage, IntPtr attributeName)
 {
     DirectoryAttribute attribute = new DirectoryAttribute {
         isSearchResult = true
     };
     string name = Marshal.PtrToStringUni(attributeName);
     attribute.Name = name;
     IntPtr ptr = Wldap32.ldap_get_values_len(this.ldapHandle, entryMessage, name);
     try
     {
         IntPtr zero = IntPtr.Zero;
         int num = 0;
         if (!(ptr != IntPtr.Zero))
         {
             return attribute;
         }
         for (zero = Marshal.ReadIntPtr(ptr, Marshal.SizeOf(typeof(IntPtr)) * num); zero != IntPtr.Zero; zero = Marshal.ReadIntPtr(ptr, Marshal.SizeOf(typeof(IntPtr)) * num))
         {
             berval structure = new berval();
             Marshal.PtrToStructure(zero, structure);
             byte[] destination = null;
             if ((structure.bv_len > 0) && (structure.bv_val != IntPtr.Zero))
             {
                 destination = new byte[structure.bv_len];
                 Marshal.Copy(structure.bv_val, destination, 0, structure.bv_len);
                 attribute.Add(destination);
             }
             num++;
         }
     }
     finally
     {
         if (ptr != IntPtr.Zero)
         {
             Wldap32.ldap_value_free_len(ptr);
         }
     }
     return attribute;
 }
コード例 #33
0
ファイル: LdapPal.Linux.cs プロジェクト: wilfriedb/runtime
 internal static int ExtendedDirectoryOperation(ConnectionHandle ldapHandle, string oid, berval data, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber) =>
                         Interop.Ldap.ldap_extended_operation(ldapHandle, oid, data, servercontrol, clientcontrol, ref messageNumber);
コード例 #34
0
ファイル: BerConverter.cs プロジェクト: nickchal/pash
		internal static object[] TryDecode(string format, byte[] value, out bool decodeSucceeded)
		{
			Utility.CheckOSVersion();
			if (format != null)
			{
				UTF8Encoding uTF8Encoding = new UTF8Encoding(false, true);
				berval _berval = new berval();
				ArrayList arrayLists = new ArrayList();
				BerSafeHandle berSafeHandle = null;
				object[] item = null;
				decodeSucceeded = false;
				if (value != null)
				{
					_berval.bv_len = (int)value.Length;
					_berval.bv_val = Marshal.AllocHGlobal((int)value.Length);
					Marshal.Copy(value, 0, _berval.bv_val, (int)value.Length);
				}
				else
				{
					_berval.bv_len = 0;
					_berval.bv_val = (IntPtr)0;
				}
				try
				{
					berSafeHandle = new BerSafeHandle(_berval);
				}
				finally
				{
					if (_berval.bv_val != (IntPtr)0)
					{
						Marshal.FreeHGlobal(_berval.bv_val);
					}
				}
				int num = 0;
				int num1 = 0;
				while (num1 < format.Length)
				{
					char chr = format[num1];
					if (chr == '{' || chr == '}' || chr == '[' || chr == ']' || chr == 'n' || chr == 'x')
					{
						num = Wldap32.ber_scanf(berSafeHandle, new string(chr, 1));
						if (num != 0)
						{
						}
					}
					else
					{
						if (chr == 'i' || chr == 'e' || chr == 'b')
						{
							int num2 = 0;
							num = Wldap32.ber_scanf_int(berSafeHandle, new string(chr, 1), ref num2);
							if (num == 0)
							{
								if (chr != 'b')
								{
									arrayLists.Add(num2);
								}
								else
								{
									bool flag = false;
									if (num2 != 0)
									{
										flag = true;
									}
									else
									{
										flag = false;
									}
									arrayLists.Add(flag);
								}
							}
						}
						else
						{
							if (chr != 'a')
							{
								if (chr != 'O')
								{
									if (chr != 'B')
									{
										if (chr != 'v')
										{
											if (chr != 'V')
											{
												throw new ArgumentException(Res.GetString("BerConverterUndefineChar"));
											}
											else
											{
												byte[][] numArray = BerConverter.DecodingMultiByteArrayHelper(berSafeHandle, chr, ref num);
												if (num == 0)
												{
													arrayLists.Add(numArray);
												}
											}
										}
										else
										{
											string[] str = null;
											byte[][] numArray1 = BerConverter.DecodingMultiByteArrayHelper(berSafeHandle, 'V', ref num);
											if (num == 0)
											{
												if (numArray1 != null)
												{
													str = new string[(int)numArray1.Length];
													for (int i = 0; i < (int)numArray1.Length; i++)
													{
														if (numArray1[i] != null)
														{
															str[i] = uTF8Encoding.GetString(numArray1[i]);
														}
														else
														{
															str[i] = null;
														}
													}
												}
												arrayLists.Add(str);
											}
										}
									}
									else
									{
										IntPtr intPtr = (IntPtr)0;
										int num3 = 0;
										num = Wldap32.ber_scanf_bitstring(berSafeHandle, "B", ref intPtr, ref num3);
										if (num == 0)
										{
											byte[] numArray2 = null;
											if (intPtr != (IntPtr)0)
											{
												numArray2 = new byte[num3];
												Marshal.Copy(intPtr, numArray2, 0, num3);
											}
											arrayLists.Add(numArray2);
										}
									}
								}
								else
								{
									byte[] numArray3 = BerConverter.DecodingByteArrayHelper(berSafeHandle, chr, ref num);
									if (num == 0)
									{
										arrayLists.Add(numArray3);
									}
								}
							}
							else
							{
								byte[] numArray4 = BerConverter.DecodingByteArrayHelper(berSafeHandle, 'O', ref num);
								if (num == 0)
								{
									string str1 = null;
									if (numArray4 != null)
									{
										str1 = uTF8Encoding.GetString(numArray4);
									}
									arrayLists.Add(str1);
								}
							}
						}
					}
					if (num == 0)
					{
						num1++;
					}
					else
					{
						return item;
					}
				}
				item = new object[arrayLists.Count];
				for (int j = 0; j < arrayLists.Count; j++)
				{
					item[j] = arrayLists[j];
				}
				decodeSucceeded = true;
				return item;
			}
			else
			{
				throw new ArgumentNullException("format");
			}
		}
コード例 #35
0
ファイル: LdapConnection.cs プロジェクト: nickchal/pash
		private int SendRequestHelper(DirectoryRequest request, ref int messageID)
		{
			IntPtr hGlobalUni;
			IntPtr intPtr;
			int num;
			int length;
			int count;
			int num1;
			IntPtr intPtr1 = (IntPtr)0;
			LdapControl[] ldapControlArray = null;
			IntPtr intPtr2 = (IntPtr)0;
			LdapControl[] ldapControlArray1 = null;
			string str = null;
			ArrayList arrayLists = new ArrayList();
			LdapMod[] ldapModArray = null;
			IntPtr intPtr3 = (IntPtr)0;
			int num2 = 0;
			berval _berval = null;
			IntPtr intPtr4 = (IntPtr)0;
			int num3 = 0;
			int num4 = 0;
			if (!this.connected)
			{
				this.Connect();
				this.connected = true;
			}
			if (this.AutoBind && (!this.bounded || this.needRebind) && !((LdapDirectoryIdentifier)this.Directory).Connectionless)
			{
				this.Bind();
			}
			try
			{
				ldapControlArray = this.BuildControlArray(request.Controls, true);
				int num5 = Marshal.SizeOf(typeof(LdapControl));
				if (ldapControlArray != null)
				{
					intPtr1 = Utility.AllocHGlobalIntPtrArray((int)ldapControlArray.Length + 1);
					for (int i = 0; i < (int)ldapControlArray.Length; i++)
					{
						hGlobalUni = Marshal.AllocHGlobal(num5);
						Marshal.StructureToPtr(ldapControlArray[i], hGlobalUni, false);
						intPtr = (IntPtr)((long)intPtr1 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
						Marshal.WriteIntPtr(intPtr, hGlobalUni);
					}
					intPtr = (IntPtr)((long)intPtr1 + (long)(Marshal.SizeOf(typeof(IntPtr)) * (int)ldapControlArray.Length));
					Marshal.WriteIntPtr(intPtr, (IntPtr)0);
				}
				ldapControlArray1 = this.BuildControlArray(request.Controls, false);
				if (ldapControlArray1 != null)
				{
					intPtr2 = Utility.AllocHGlobalIntPtrArray((int)ldapControlArray1.Length + 1);
					for (int j = 0; j < (int)ldapControlArray1.Length; j++)
					{
						hGlobalUni = Marshal.AllocHGlobal(num5);
						Marshal.StructureToPtr(ldapControlArray1[j], hGlobalUni, false);
						intPtr = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * j));
						Marshal.WriteIntPtr(intPtr, hGlobalUni);
					}
					intPtr = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * (int)ldapControlArray1.Length));
					Marshal.WriteIntPtr(intPtr, (IntPtr)0);
				}
				if (request as DeleteRequest == null)
				{
					if (request as ModifyDNRequest == null)
					{
						if (request as CompareRequest == null)
						{
							if (request as AddRequest != null || request as ModifyRequest != null)
							{
								if (request as AddRequest == null)
								{
									ldapModArray = this.BuildAttributes(((ModifyRequest)request).Modifications, arrayLists);
								}
								else
								{
									ldapModArray = this.BuildAttributes(((AddRequest)request).Attributes, arrayLists);
								}
								if (ldapModArray == null)
								{
									length = 1;
								}
								else
								{
									length = (int)ldapModArray.Length + 1;
								}
								num2 = length;
								intPtr3 = Utility.AllocHGlobalIntPtrArray(num2);
								int num6 = Marshal.SizeOf(typeof(LdapMod));
								int k = 0;
								for (k = 0; k < num2 - 1; k++)
								{
									hGlobalUni = Marshal.AllocHGlobal(num6);
									Marshal.StructureToPtr(ldapModArray[k], hGlobalUni, false);
									intPtr = (IntPtr)((long)intPtr3 + (long)(Marshal.SizeOf(typeof(IntPtr)) * k));
									Marshal.WriteIntPtr(intPtr, hGlobalUni);
								}
								intPtr = (IntPtr)((long)intPtr3 + (long)(Marshal.SizeOf(typeof(IntPtr)) * k));
								Marshal.WriteIntPtr(intPtr, (IntPtr)0);
								if (request as AddRequest == null)
								{
									num4 = Wldap32.ldap_modify(this.ldapHandle, ((ModifyRequest)request).DistinguishedName, intPtr3, intPtr1, intPtr2, ref messageID);
								}
								else
								{
									num4 = Wldap32.ldap_add(this.ldapHandle, ((AddRequest)request).DistinguishedName, intPtr3, intPtr1, intPtr2, ref messageID);
								}
							}
							else
							{
								if (request as ExtendedRequest == null)
								{
									if (request as SearchRequest == null)
									{
										throw new NotSupportedException(Res.GetString("InvliadRequestType"));
									}
									else
									{
										SearchRequest searchRequest = (SearchRequest)request;
										object filter = searchRequest.Filter;
										if (filter == null || filter as XmlDocument == null)
										{
											string str1 = (string)filter;
											if (searchRequest.Attributes == null)
											{
												count = 0;
											}
											else
											{
												count = searchRequest.Attributes.Count;
											}
											num3 = count;
											if (num3 != 0)
											{
												intPtr4 = Utility.AllocHGlobalIntPtrArray(num3 + 1);
												int l = 0;
												for (l = 0; l < num3; l++)
												{
													hGlobalUni = Marshal.StringToHGlobalUni(searchRequest.Attributes[l]);
													intPtr = (IntPtr)((long)intPtr4 + (long)(Marshal.SizeOf(typeof(IntPtr)) * l));
													Marshal.WriteIntPtr(intPtr, hGlobalUni);
												}
												intPtr = (IntPtr)((long)intPtr4 + (long)(Marshal.SizeOf(typeof(IntPtr)) * l));
												Marshal.WriteIntPtr(intPtr, (IntPtr)0);
											}
											int scope = (int)searchRequest.Scope;
											TimeSpan timeLimit = searchRequest.TimeLimit;
											int ticks = (int)(timeLimit.Ticks / (long)0x989680);
											DereferenceAlias derefAlias = this.options.DerefAlias;
											this.options.DerefAlias = searchRequest.Aliases;
											try
											{
												num4 = Wldap32.ldap_search(this.ldapHandle, searchRequest.DistinguishedName, scope, str1, intPtr4, searchRequest.TypesOnly, intPtr1, intPtr2, ticks, searchRequest.SizeLimit, ref messageID);
											}
											finally
											{
												this.options.DerefAlias = derefAlias;
											}
										}
										else
										{
											throw new ArgumentException(Res.GetString("InvalidLdapSearchRequestFilter"));
										}
									}
								}
								else
								{
									string requestName = ((ExtendedRequest)request).RequestName;
									byte[] requestValue = ((ExtendedRequest)request).RequestValue;
									if (requestValue != null && (int)requestValue.Length != 0)
									{
										_berval = new berval();
										_berval.bv_len = (int)requestValue.Length;
										_berval.bv_val = Marshal.AllocHGlobal((int)requestValue.Length);
										Marshal.Copy(requestValue, 0, _berval.bv_val, (int)requestValue.Length);
									}
									num4 = Wldap32.ldap_extended_operation(this.ldapHandle, requestName, _berval, intPtr1, intPtr2, ref messageID);
								}
							}
						}
						else
						{
							DirectoryAttribute assertion = ((CompareRequest)request).Assertion;
							if (assertion != null)
							{
								if (assertion.Count == 1)
								{
									byte[] item = assertion[0] as byte[];
									if (item == null)
									{
										str = assertion[0].ToString();
									}
									else
									{
										if (item != null && (int)item.Length != 0)
										{
											_berval = new berval();
											_berval.bv_len = (int)item.Length;
											_berval.bv_val = Marshal.AllocHGlobal((int)item.Length);
											Marshal.Copy(item, 0, _berval.bv_val, (int)item.Length);
										}
									}
									num4 = Wldap32.ldap_compare(this.ldapHandle, ((CompareRequest)request).DistinguishedName, assertion.Name, str, _berval, intPtr1, intPtr2, ref messageID);
								}
								else
								{
									throw new ArgumentException(Res.GetString("WrongNumValuesCompare"));
								}
							}
							else
							{
								throw new ArgumentException(Res.GetString("WrongAssertionCompare"));
							}
						}
					}
					else
					{
						ConnectionHandle connectionHandle = this.ldapHandle;
						string distinguishedName = ((ModifyDNRequest)request).DistinguishedName;
						string newName = ((ModifyDNRequest)request).NewName;
						string newParentDistinguishedName = ((ModifyDNRequest)request).NewParentDistinguishedName;
						if (((ModifyDNRequest)request).DeleteOldRdn)
						{
							num1 = 1;
						}
						else
						{
							num1 = 0;
						}
						num4 = Wldap32.ldap_rename(connectionHandle, distinguishedName, newName, newParentDistinguishedName, num1, intPtr1, intPtr2, ref messageID);
					}
				}
				else
				{
					num4 = Wldap32.ldap_delete_ext(this.ldapHandle, ((DeleteRequest)request).DistinguishedName, intPtr1, intPtr2, ref messageID);
				}
				if (num4 == 85)
				{
					num4 = 112;
				}
				num = num4;
			}
			finally
			{
				GC.KeepAlive(ldapModArray);
				if (intPtr1 != (IntPtr)0)
				{
					for (int m = 0; m < (int)ldapControlArray.Length; m++)
					{
						IntPtr intPtr5 = Marshal.ReadIntPtr(intPtr1, Marshal.SizeOf(typeof(IntPtr)) * m);
						if (intPtr5 != (IntPtr)0)
						{
							Marshal.FreeHGlobal(intPtr5);
						}
					}
					Marshal.FreeHGlobal(intPtr1);
				}
				if (ldapControlArray != null)
				{
					for (int n = 0; n < (int)ldapControlArray.Length; n++)
					{
						if (ldapControlArray[n].ldctl_oid != (IntPtr)0)
						{
							Marshal.FreeHGlobal(ldapControlArray[n].ldctl_oid);
						}
						if (ldapControlArray[n].ldctl_value != null && ldapControlArray[n].ldctl_value.bv_val != (IntPtr)0)
						{
							Marshal.FreeHGlobal(ldapControlArray[n].ldctl_value.bv_val);
						}
					}
				}
				if (intPtr2 != (IntPtr)0)
				{
					for (int o = 0; o < (int)ldapControlArray1.Length; o++)
					{
						IntPtr intPtr6 = Marshal.ReadIntPtr(intPtr2, Marshal.SizeOf(typeof(IntPtr)) * o);
						if (intPtr6 != (IntPtr)0)
						{
							Marshal.FreeHGlobal(intPtr6);
						}
					}
					Marshal.FreeHGlobal(intPtr2);
				}
				if (ldapControlArray1 != null)
				{
					for (int p = 0; p < (int)ldapControlArray1.Length; p++)
					{
						if (ldapControlArray1[p].ldctl_oid != (IntPtr)0)
						{
							Marshal.FreeHGlobal(ldapControlArray1[p].ldctl_oid);
						}
						if (ldapControlArray1[p].ldctl_value != null && ldapControlArray1[p].ldctl_value.bv_val != (IntPtr)0)
						{
							Marshal.FreeHGlobal(ldapControlArray1[p].ldctl_value.bv_val);
						}
					}
				}
				if (intPtr3 != (IntPtr)0)
				{
					for (int q = 0; q < num2 - 1; q++)
					{
						IntPtr intPtr7 = Marshal.ReadIntPtr(intPtr3, Marshal.SizeOf(typeof(IntPtr)) * q);
						if (intPtr7 != (IntPtr)0)
						{
							Marshal.FreeHGlobal(intPtr7);
						}
					}
					Marshal.FreeHGlobal(intPtr3);
				}
				int num7 = 0;
				while (num7 < arrayLists.Count)
				{
					IntPtr item1 = (IntPtr)arrayLists[num7];
					Marshal.FreeHGlobal(item1);
					num7++;
				}
				if (_berval != null && _berval.bv_val != (IntPtr)0)
				{
					Marshal.FreeHGlobal(_berval.bv_val);
				}
				if (intPtr4 != (IntPtr)0)
				{
					for (int r = 0; r < num3; r++)
					{
						IntPtr intPtr8 = Marshal.ReadIntPtr(intPtr4, Marshal.SizeOf(typeof(IntPtr)) * r);
						if (intPtr8 != (IntPtr)0)
						{
							Marshal.FreeHGlobal(intPtr8);
						}
					}
					Marshal.FreeHGlobal(intPtr4);
				}
			}
			return num;
		}
コード例 #36
0
        internal static object[] TryDecode(string format, byte[] value, out bool decodeSucceeded)
        {
            Utility.CheckOSVersion();
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }
            UTF8Encoding encoding = new UTF8Encoding(false, true);
            berval berval = new berval();
            ArrayList list = new ArrayList();
            BerSafeHandle berElement = null;
            object[] objArray = null;
            decodeSucceeded = false;
            if (value == null)
            {
                berval.bv_len = 0;
                berval.bv_val = IntPtr.Zero;
            }
            else
            {
                berval.bv_len = value.Length;
                berval.bv_val = Marshal.AllocHGlobal(value.Length);
                Marshal.Copy(value, 0, berval.bv_val, value.Length);
            }
            try
            {
                berElement = new BerSafeHandle(berval);
            }
            finally
            {
                if (berval.bv_val != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(berval.bv_val);
                }
            }
            int error = 0;
            for (int i = 0; i < format.Length; i++)
            {
                char c = format[i];
                switch (c)
                {
                    case '{':
                    case '}':
                    case '[':
                    case ']':
                    case 'n':
                    case 'x':
                        error = Wldap32.ber_scanf(berElement, new string(c, 1));
                        if (error == 0)
                        {
                        }
                        break;

                    case 'i':
                    case 'e':
                    case 'b':
                    {
                        int num3 = 0;
                        error = Wldap32.ber_scanf_int(berElement, new string(c, 1), ref num3);
                        if (error == 0)
                        {
                            if (c == 'b')
                            {
                                bool flag = false;
                                if (num3 == 0)
                                {
                                    flag = false;
                                }
                                else
                                {
                                    flag = true;
                                }
                                list.Add(flag);
                            }
                            else
                            {
                                list.Add(num3);
                            }
                        }
                        break;
                    }
                    case 'a':
                    {
                        byte[] bytes = DecodingByteArrayHelper(berElement, 'O', ref error);
                        if (error == 0)
                        {
                            string str = null;
                            if (bytes != null)
                            {
                                str = encoding.GetString(bytes);
                            }
                            list.Add(str);
                        }
                        break;
                    }
                    case 'O':
                    {
                        byte[] buffer2 = DecodingByteArrayHelper(berElement, c, ref error);
                        if (error == 0)
                        {
                            list.Add(buffer2);
                        }
                        break;
                    }
                    case 'B':
                    {
                        IntPtr zero = IntPtr.Zero;
                        int length = 0;
                        error = Wldap32.ber_scanf_bitstring(berElement, "B", ref zero, ref length);
                        if (error == 0)
                        {
                            byte[] destination = null;
                            if (zero != IntPtr.Zero)
                            {
                                destination = new byte[length];
                                Marshal.Copy(zero, destination, 0, length);
                            }
                            list.Add(destination);
                        }
                        break;
                    }
                    case 'v':
                    {
                        byte[][] bufferArray = null;
                        string[] strArray = null;
                        bufferArray = DecodingMultiByteArrayHelper(berElement, 'V', ref error);
                        if (error == 0)
                        {
                            if (bufferArray != null)
                            {
                                strArray = new string[bufferArray.Length];
                                for (int k = 0; k < bufferArray.Length; k++)
                                {
                                    if (bufferArray[k] == null)
                                    {
                                        strArray[k] = null;
                                    }
                                    else
                                    {
                                        strArray[k] = encoding.GetString(bufferArray[k]);
                                    }
                                }
                            }
                            list.Add(strArray);
                        }
                        break;
                    }
                    default:
                    {
                        if (c != 'V')
                        {
                            throw new ArgumentException(Res.GetString("BerConverterUndefineChar"));
                        }
                        byte[][] bufferArray2 = null;
                        bufferArray2 = DecodingMultiByteArrayHelper(berElement, c, ref error);
                        if (error == 0)
                        {
                            list.Add(bufferArray2);
                        }
                        break;
                    }
                }
                if (error != 0)
                {
                    return objArray;
                }
            }
            objArray = new object[list.Count];
            for (int j = 0; j < list.Count; j++)
            {
                objArray[j] = list[j];
            }
            decodeSucceeded = true;
            return objArray;
        }
コード例 #37
0
 public static extern int ldap_extended_operation([In] ConnectionHandle ldapHandle, string oid, berval data, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber);
コード例 #38
0
        public static byte[] Encode(string format, params object[] value)
        {
            Utility.CheckOSVersion();
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] destination = null;
            if (value == null)
            {
                value = new object[0];
            }
            BerSafeHandle berElement = new BerSafeHandle();
            int index = 0;
            int num2 = 0;
            for (int i = 0; i < format.Length; i++)
            {
                char c = format[i];
                switch (c)
                {
                    case '{':
                    case '}':
                    case '[':
                    case ']':
                    case 'n':
                        num2 = Wldap32.ber_printf_emptyarg(berElement, new string(c, 1));
                        break;

                    case 't':
                    case 'i':
                    case 'e':
                        if (index >= value.Length)
                        {
                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                        }
                        if (!(value[index] is int))
                        {
                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                        }
                        num2 = Wldap32.ber_printf_int(berElement, new string(c, 1), (int) value[index]);
                        index++;
                        break;

                    case 'b':
                        if (index >= value.Length)
                        {
                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                        }
                        if (!(value[index] is bool))
                        {
                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                        }
                        num2 = Wldap32.ber_printf_int(berElement, new string(c, 1), ((bool) value[index]) ? 1 : 0);
                        index++;
                        break;

                    case 's':
                    {
                        if (index >= value.Length)
                        {
                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                        }
                        if ((value[index] != null) && !(value[index] is string))
                        {
                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                        }
                        byte[] tempValue = null;
                        if (value[index] != null)
                        {
                            tempValue = encoding.GetBytes((string) value[index]);
                        }
                        num2 = EncodingByteArrayHelper(berElement, tempValue, 'o');
                        index++;
                        break;
                    }
                    case 'o':
                    case 'X':
                    {
                        if (index >= value.Length)
                        {
                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                        }
                        if ((value[index] != null) && !(value[index] is byte[]))
                        {
                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                        }
                        byte[] buffer3 = (byte[]) value[index];
                        num2 = EncodingByteArrayHelper(berElement, buffer3, c);
                        index++;
                        break;
                    }
                    case 'v':
                    {
                        if (index >= value.Length)
                        {
                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                        }
                        if ((value[index] != null) && !(value[index] is string[]))
                        {
                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                        }
                        string[] strArray = (string[]) value[index];
                        byte[][] bufferArray = null;
                        if (strArray != null)
                        {
                            bufferArray = new byte[strArray.Length][];
                            for (int j = 0; j < strArray.Length; j++)
                            {
                                string s = strArray[j];
                                if (s == null)
                                {
                                    bufferArray[j] = null;
                                }
                                else
                                {
                                    bufferArray[j] = encoding.GetBytes(s);
                                }
                            }
                        }
                        num2 = EncodingMultiByteArrayHelper(berElement, bufferArray, 'V');
                        index++;
                        break;
                    }
                    default:
                    {
                        if (c != 'V')
                        {
                            throw new ArgumentException(Res.GetString("BerConverterUndefineChar"));
                        }
                        if (index >= value.Length)
                        {
                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                        }
                        if ((value[index] != null) && !(value[index] is byte[][]))
                        {
                            throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                        }
                        byte[][] bufferArray2 = (byte[][]) value[index];
                        num2 = EncodingMultiByteArrayHelper(berElement, bufferArray2, c);
                        index++;
                        break;
                    }
                }
                if (num2 == -1)
                {
                    throw new BerConversionException();
                }
            }
            berval structure = new berval();
            IntPtr zero = IntPtr.Zero;
            try
            {
                if (Wldap32.ber_flatten(berElement, ref zero) == -1)
                {
                    throw new BerConversionException();
                }
                if (zero != IntPtr.Zero)
                {
                    Marshal.PtrToStructure(zero, structure);
                }
                if ((structure == null) || (structure.bv_len == 0))
                {
                    return new byte[0];
                }
                destination = new byte[structure.bv_len];
                Marshal.Copy(structure.bv_val, destination, 0, structure.bv_len);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Wldap32.ber_bvfree(zero);
                }
            }
            return destination;
        }
コード例 #39
0
ファイル: BerConverter.cs プロジェクト: nickchal/pash
		public static byte[] Encode(string format, object[] value)
		{
			int num;
			Utility.CheckOSVersion();
			if (format != null)
			{
				UTF8Encoding uTF8Encoding = new UTF8Encoding();
				byte[] numArray = null;
				if (value == null)
				{
					value = new object[0];
				}
				BerSafeHandle berSafeHandle = new BerSafeHandle();
				int num1 = 0;
				int num2 = 0;
				int num3 = 0;
				while (num3 < format.Length)
				{
					char chr = format[num3];
					if (chr == '{' || chr == '}' || chr == '[' || chr == ']' || chr == 'n')
					{
						num2 = Wldap32.ber_printf_emptyarg(berSafeHandle, new string(chr, 1));
					}
					else
					{
						if (chr == 't' || chr == 'i' || chr == 'e')
						{
							if (num1 < (int)value.Length)
							{
								if (value[num1] is int)
								{
									num2 = Wldap32.ber_printf_int(berSafeHandle, new string(chr, 1), (int)value[num1]);
									num1++;
								}
								else
								{
									throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
								}
							}
							else
							{
								throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
							}
						}
						else
						{
							if (chr != 'b')
							{
								if (chr != 's')
								{
									if (chr == 'o' || chr == 'X')
									{
										if (num1 < (int)value.Length)
										{
											if (value[num1] == null || value[num1] as byte[] != null)
											{
												byte[] numArray1 = (byte[])value[num1];
												num2 = BerConverter.EncodingByteArrayHelper(berSafeHandle, numArray1, chr);
												num1++;
											}
											else
											{
												throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
											}
										}
										else
										{
											throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
										}
									}
									else
									{
										if (chr != 'v')
										{
											if (chr != 'V')
											{
												throw new ArgumentException(Res.GetString("BerConverterUndefineChar"));
											}
											else
											{
												if (num1 < (int)value.Length)
												{
													if (value[num1] == null || value[num1] as byte[][] != null)
													{
														byte[][] numArray2 = (byte[][])value[num1];
														num2 = BerConverter.EncodingMultiByteArrayHelper(berSafeHandle, numArray2, chr);
														num1++;
													}
													else
													{
														throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
													}
												}
												else
												{
													throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
												}
											}
										}
										else
										{
											if (num1 < (int)value.Length)
											{
												if (value[num1] == null || value[num1] as string[] != null)
												{
													string[] strArrays = (string[])value[num1];
													byte[][] bytes = null;
													if (strArrays != null)
													{
														bytes = new byte[(int)strArrays.Length][];
														for (int i = 0; i < (int)strArrays.Length; i++)
														{
															string str = strArrays[i];
															if (str != null)
															{
																bytes[i] = uTF8Encoding.GetBytes(str);
															}
															else
															{
																bytes[i] = null;
															}
														}
													}
													num2 = BerConverter.EncodingMultiByteArrayHelper(berSafeHandle, bytes, 'V');
													num1++;
												}
												else
												{
													throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
												}
											}
											else
											{
												throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
											}
										}
									}
								}
								else
								{
									if (num1 < (int)value.Length)
									{
										if (value[num1] == null || value[num1] as string != null)
										{
											byte[] bytes1 = null;
											if (value[num1] != null)
											{
												bytes1 = uTF8Encoding.GetBytes((string)value[num1]);
											}
											num2 = BerConverter.EncodingByteArrayHelper(berSafeHandle, bytes1, 'o');
											num1++;
										}
										else
										{
											throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
										}
									}
									else
									{
										throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
									}
								}
							}
							else
							{
								if (num1 < (int)value.Length)
								{
									if (value[num1] is bool)
									{
										BerSafeHandle berSafeHandle1 = berSafeHandle;
										string str1 = new string(chr, 1);
										if ((bool)value[num1])
										{
											num = 1;
										}
										else
										{
											num = 0;
										}
										num2 = Wldap32.ber_printf_int(berSafeHandle1, str1, num);
										num1++;
									}
									else
									{
										throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
									}
								}
								else
								{
									throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
								}
							}
						}
					}
					if (num2 != -1)
					{
						num3++;
					}
					else
					{
						throw new BerConversionException();
					}
				}
				berval _berval = new berval();
				IntPtr intPtr = (IntPtr)0;
				try
				{
					num2 = Wldap32.ber_flatten(berSafeHandle, ref intPtr);
					if (num2 != -1)
					{
						if (intPtr != (IntPtr)0)
						{
							Marshal.PtrToStructure(intPtr, _berval);
						}
						if (_berval == null || _berval.bv_len == 0)
						{
							numArray = new byte[0];
						}
						else
						{
							numArray = new byte[_berval.bv_len];
							Marshal.Copy(_berval.bv_val, numArray, 0, _berval.bv_len);
						}
					}
					else
					{
						throw new BerConversionException();
					}
				}
				finally
				{
					if (intPtr != (IntPtr)0)
					{
						Wldap32.ber_bvfree(intPtr);
					}
				}
				return numArray;
			}
			else
			{
				throw new ArgumentNullException("format");
			}
		}
コード例 #40
0
ファイル: LdapConnection.cs プロジェクト: nickchal/pash
		internal LdapMod[] BuildAttributes(CollectionBase directoryAttributes, ArrayList ptrToFree)
		{
			DirectoryAttribute item;
			byte[] bytes;
			IntPtr intPtr;
			LdapMod[] ldapMod = null;
			UTF8Encoding uTF8Encoding = new UTF8Encoding();
			DirectoryAttributeCollection directoryAttributeCollection = null;
			DirectoryAttributeModificationCollection directoryAttributeModificationCollection = null;
			if (directoryAttributes != null && directoryAttributes.Count != 0)
			{
				if (directoryAttributes as DirectoryAttributeModificationCollection == null)
				{
					directoryAttributeCollection = (DirectoryAttributeCollection)directoryAttributes;
				}
				else
				{
					directoryAttributeModificationCollection = (DirectoryAttributeModificationCollection)directoryAttributes;
				}
				ldapMod = new LdapMod[directoryAttributes.Count];
				for (int i = 0; i < directoryAttributes.Count; i++)
				{
					if (directoryAttributeCollection == null)
					{
						item = directoryAttributeModificationCollection[i];
					}
					else
					{
						item = directoryAttributeCollection[i];
					}
					ldapMod[i] = new LdapMod();
					if (item as DirectoryAttributeModification == null)
					{
						ldapMod[i].type = 0;
					}
					else
					{
						ldapMod[i].type = (int)((DirectoryAttributeModification)item).Operation;
					}
					LdapMod ldapMod1 = ldapMod[i];
					ldapMod1.type = ldapMod1.type | 128;
					ldapMod[i].attribute = Marshal.StringToHGlobalUni(item.Name);
					int count = 0;
					berval[] _berval = null;
					if (item.Count > 0)
					{
						count = item.Count;
						_berval = new berval[count];
						for (int j = 0; j < count; j++)
						{
							if (item[j] as string == null)
							{
								if (item[j] as Uri == null)
								{
									bytes = (byte[])item[j];
								}
								else
								{
									bytes = uTF8Encoding.GetBytes(((Uri)item[j]).ToString());
								}
							}
							else
							{
								bytes = uTF8Encoding.GetBytes((string)item[j]);
							}
							_berval[j] = new berval();
							_berval[j].bv_len = (int)bytes.Length;
							_berval[j].bv_val = Marshal.AllocHGlobal(_berval[j].bv_len);
							ptrToFree.Add(_berval[j].bv_val);
							Marshal.Copy(bytes, 0, _berval[j].bv_val, _berval[j].bv_len);
						}
					}
					ldapMod[i].values = Marshal.AllocHGlobal((count + 1) * Marshal.SizeOf(typeof(IntPtr)));
					int num = Marshal.SizeOf(typeof(berval));
					int num1 = 0;
					num1 = 0;
					while (num1 < count)
					{
						IntPtr intPtr1 = Marshal.AllocHGlobal(num);
						ptrToFree.Add(intPtr1);
						Marshal.StructureToPtr(_berval[num1], intPtr1, false);
						intPtr = (IntPtr)((long)ldapMod[i].values + (long)(Marshal.SizeOf(typeof(IntPtr)) * num1));
						Marshal.WriteIntPtr(intPtr, intPtr1);
						num1++;
					}
					intPtr = (IntPtr)((long)ldapMod[i].values + (long)(Marshal.SizeOf(typeof(IntPtr)) * num1));
					Marshal.WriteIntPtr(intPtr, (IntPtr)0);
				}
			}
			return ldapMod;
		}
コード例 #41
0
        public override byte[] GetValue()
        {
            IntPtr intPtr;
            byte   num;
            IntPtr intPtr1 = (IntPtr)0;
            int    num1    = Marshal.SizeOf(typeof(SortKey));
            IntPtr intPtr2 = Utility.AllocHGlobalIntPtrArray((int)this.keys.Length + 1);

            try
            {
                int i = 0;
                for (i = 0; i < (int)this.keys.Length; i++)
                {
                    IntPtr intPtr3 = Marshal.AllocHGlobal(num1);
                    Marshal.StructureToPtr(this.keys[i], intPtr3, false);
                    intPtr = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
                    Marshal.WriteIntPtr(intPtr, intPtr3);
                }
                intPtr = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
                Marshal.WriteIntPtr(intPtr, (IntPtr)0);
                bool             isCritical = base.IsCritical;
                ConnectionHandle handle     = UtilityHandle.GetHandle();
                IntPtr           intPtr4    = intPtr2;
                if (isCritical)
                {
                    num = 1;
                }
                else
                {
                    num = 0;
                }
                int num2 = Wldap32.ldap_create_sort_control(handle, intPtr4, num, ref intPtr1);
                if (num2 == 0)
                {
                    LdapControl ldapControl = new LdapControl();
                    Marshal.PtrToStructure(intPtr1, ldapControl);
                    berval ldctlValue = ldapControl.ldctl_value;
                    this.directoryControlValue = null;
                    if (ldctlValue != null)
                    {
                        this.directoryControlValue = new byte[ldctlValue.bv_len];
                        Marshal.Copy(ldctlValue.bv_val, this.directoryControlValue, 0, ldctlValue.bv_len);
                    }
                }
                else
                {
                    if (!Utility.IsLdapError((LdapError)num2))
                    {
                        throw new LdapException(num2);
                    }
                    else
                    {
                        string str = LdapErrorMappings.MapResultCode(num2);
                        throw new LdapException(num2, str);
                    }
                }
            }
            finally
            {
                if (intPtr1 != (IntPtr)0)
                {
                    Wldap32.ldap_control_free(intPtr1);
                }
                if (intPtr2 != (IntPtr)0)
                {
                    for (int j = 0; j < (int)this.keys.Length; j++)
                    {
                        IntPtr intPtr5 = Marshal.ReadIntPtr(intPtr2, Marshal.SizeOf(typeof(IntPtr)) * j);
                        if (intPtr5 != (IntPtr)0)
                        {
                            IntPtr intPtr6 = Marshal.ReadIntPtr(intPtr5);
                            if (intPtr6 != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(intPtr6);
                            }
                            intPtr6 = Marshal.ReadIntPtr(intPtr5, Marshal.SizeOf(typeof(IntPtr)));
                            if (intPtr6 != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(intPtr6);
                            }
                            Marshal.FreeHGlobal(intPtr5);
                        }
                    }
                    Marshal.FreeHGlobal(intPtr2);
                }
            }
            return(base.GetValue());
        }
コード例 #42
0
ファイル: BerConverter.cs プロジェクト: modulexcite/pash-1
 internal static object[] TryDecode(string format, byte[] value, out bool decodeSucceeded)
 {
     Utility.CheckOSVersion();
     if (format != null)
     {
         UTF8Encoding  uTF8Encoding  = new UTF8Encoding(false, true);
         berval        _berval       = new berval();
         ArrayList     arrayLists    = new ArrayList();
         BerSafeHandle berSafeHandle = null;
         object[]      item          = null;
         decodeSucceeded = false;
         if (value != null)
         {
             _berval.bv_len = (int)value.Length;
             _berval.bv_val = Marshal.AllocHGlobal((int)value.Length);
             Marshal.Copy(value, 0, _berval.bv_val, (int)value.Length);
         }
         else
         {
             _berval.bv_len = 0;
             _berval.bv_val = (IntPtr)0;
         }
         try
         {
             berSafeHandle = new BerSafeHandle(_berval);
         }
         finally
         {
             if (_berval.bv_val != (IntPtr)0)
             {
                 Marshal.FreeHGlobal(_berval.bv_val);
             }
         }
         int num  = 0;
         int num1 = 0;
         while (num1 < format.Length)
         {
             char chr = format[num1];
             if (chr == '{' || chr == '}' || chr == '[' || chr == ']' || chr == 'n' || chr == 'x')
             {
                 num = Wldap32.ber_scanf(berSafeHandle, new string(chr, 1));
                 if (num != 0)
                 {
                 }
             }
             else
             {
                 if (chr == 'i' || chr == 'e' || chr == 'b')
                 {
                     int num2 = 0;
                     num = Wldap32.ber_scanf_int(berSafeHandle, new string(chr, 1), ref num2);
                     if (num == 0)
                     {
                         if (chr != 'b')
                         {
                             arrayLists.Add(num2);
                         }
                         else
                         {
                             bool flag = false;
                             if (num2 != 0)
                             {
                                 flag = true;
                             }
                             else
                             {
                                 flag = false;
                             }
                             arrayLists.Add(flag);
                         }
                     }
                 }
                 else
                 {
                     if (chr != 'a')
                     {
                         if (chr != 'O')
                         {
                             if (chr != 'B')
                             {
                                 if (chr != 'v')
                                 {
                                     if (chr != 'V')
                                     {
                                         throw new ArgumentException(Res.GetString("BerConverterUndefineChar"));
                                     }
                                     else
                                     {
                                         byte[][] numArray = BerConverter.DecodingMultiByteArrayHelper(berSafeHandle, chr, ref num);
                                         if (num == 0)
                                         {
                                             arrayLists.Add(numArray);
                                         }
                                     }
                                 }
                                 else
                                 {
                                     string[] str       = null;
                                     byte[][] numArray1 = BerConverter.DecodingMultiByteArrayHelper(berSafeHandle, 'V', ref num);
                                     if (num == 0)
                                     {
                                         if (numArray1 != null)
                                         {
                                             str = new string[(int)numArray1.Length];
                                             for (int i = 0; i < (int)numArray1.Length; i++)
                                             {
                                                 if (numArray1[i] != null)
                                                 {
                                                     str[i] = uTF8Encoding.GetString(numArray1[i]);
                                                 }
                                                 else
                                                 {
                                                     str[i] = null;
                                                 }
                                             }
                                         }
                                         arrayLists.Add(str);
                                     }
                                 }
                             }
                             else
                             {
                                 IntPtr intPtr = (IntPtr)0;
                                 int    num3   = 0;
                                 num = Wldap32.ber_scanf_bitstring(berSafeHandle, "B", ref intPtr, ref num3);
                                 if (num == 0)
                                 {
                                     byte[] numArray2 = null;
                                     if (intPtr != (IntPtr)0)
                                     {
                                         numArray2 = new byte[num3];
                                         Marshal.Copy(intPtr, numArray2, 0, num3);
                                     }
                                     arrayLists.Add(numArray2);
                                 }
                             }
                         }
                         else
                         {
                             byte[] numArray3 = BerConverter.DecodingByteArrayHelper(berSafeHandle, chr, ref num);
                             if (num == 0)
                             {
                                 arrayLists.Add(numArray3);
                             }
                         }
                     }
                     else
                     {
                         byte[] numArray4 = BerConverter.DecodingByteArrayHelper(berSafeHandle, 'O', ref num);
                         if (num == 0)
                         {
                             string str1 = null;
                             if (numArray4 != null)
                             {
                                 str1 = uTF8Encoding.GetString(numArray4);
                             }
                             arrayLists.Add(str1);
                         }
                     }
                 }
             }
             if (num == 0)
             {
                 num1++;
             }
             else
             {
                 return(item);
             }
         }
         item = new object[arrayLists.Count];
         for (int j = 0; j < arrayLists.Count; j++)
         {
             item[j] = arrayLists[j];
         }
         decodeSucceeded = true;
         return(item);
     }
     else
     {
         throw new ArgumentNullException("format");
     }
 }
コード例 #43
0
ファイル: LdapConnection.cs プロジェクト: chcosta/corefx
        internal LdapMod[] BuildAttributes(CollectionBase directoryAttributes, ArrayList ptrToFree)
        {
            LdapMod[] attributes = null;
            UTF8Encoding encoder = new UTF8Encoding();
            DirectoryAttributeCollection attributeCollection = null;
            DirectoryAttributeModificationCollection modificationCollection = null;
            DirectoryAttribute modAttribute = null;

            if (directoryAttributes != null && directoryAttributes.Count != 0)
            {
                if (directoryAttributes is DirectoryAttributeModificationCollection)
                {
                    modificationCollection = (DirectoryAttributeModificationCollection)directoryAttributes;
                }
                else
                {
                    attributeCollection = (DirectoryAttributeCollection)directoryAttributes;
                }

                attributes = new LdapMod[directoryAttributes.Count];
                for (int i = 0; i < directoryAttributes.Count; i++)
                {
                    // get the managed attribute first
                    if (attributeCollection != null)
                        modAttribute = attributeCollection[i];
                    else
                        modAttribute = modificationCollection[i];

                    attributes[i] = new LdapMod();

                    // operation type
                    if (modAttribute is DirectoryAttributeModification)
                    {
                        attributes[i].type = (int)((DirectoryAttributeModification)modAttribute).Operation;
                    }
                    else
                    {
                        attributes[i].type = (int)DirectoryAttributeOperation.Add;
                    }
                    // we treat all the values as binary
                    attributes[i].type |= (int)LDAP_MOD_BVALUES;

                    //attribute name
                    attributes[i].attribute = Marshal.StringToHGlobalUni(modAttribute.Name);

                    // values
                    int valuesCount = 0;
                    berval[] berValues = null;
                    if (modAttribute.Count > 0)
                    {
                        valuesCount = modAttribute.Count;
                        berValues = new berval[valuesCount];
                        for (int j = 0; j < valuesCount; j++)
                        {
                            byte[] byteArray = null;
                            if (modAttribute[j] is string)
                                byteArray = encoder.GetBytes((string)modAttribute[j]);
                            else if (modAttribute[j] is Uri)
                                byteArray = encoder.GetBytes(((Uri)modAttribute[j]).ToString());
                            else
                                byteArray = (byte[])modAttribute[j];

                            berValues[j] = new berval();
                            berValues[j].bv_len = byteArray.Length;
                            berValues[j].bv_val = Marshal.AllocHGlobal(berValues[j].bv_len);
                            // need to free the memory allocated on the heap when we are done
                            ptrToFree.Add(berValues[j].bv_val);
                            Marshal.Copy(byteArray, 0, berValues[j].bv_val, berValues[j].bv_len);
                        }
                    }

                    attributes[i].values = Utility.AllocHGlobalIntPtrArray(valuesCount + 1);
                    int structSize = Marshal.SizeOf(typeof(berval));
                    IntPtr controlPtr = (IntPtr)0;
                    IntPtr tempPtr = (IntPtr)0;
                    int m = 0;
                    for (m = 0; m < valuesCount; m++)
                    {
                        controlPtr = Marshal.AllocHGlobal(structSize);
                        // need to free the memory allocated on the heap when we are done
                        ptrToFree.Add(controlPtr);
                        Marshal.StructureToPtr(berValues[m], controlPtr, false);
                        tempPtr = (IntPtr)((long)attributes[i].values + Marshal.SizeOf(typeof(IntPtr)) * m);
                        Marshal.WriteIntPtr(tempPtr, controlPtr);
                    }
                    tempPtr = (IntPtr)((long)attributes[i].values + Marshal.SizeOf(typeof(IntPtr)) * m);
                    Marshal.WriteIntPtr(tempPtr, (IntPtr)0);
                }
            }

            return attributes;
        }
コード例 #44
0
        private static int EncodingMultiByteArrayHelper(SafeBerHandle berElement, byte[][] tempValue, char fmt)
        {
            IntPtr berValArray = IntPtr.Zero;
            IntPtr tempPtr     = IntPtr.Zero;

            berval[] managedBervalArray = null;
            int      error = 0;

            try
            {
                if (tempValue != null)
                {
                    int i = 0;
                    berValArray = Utility.AllocHGlobalIntPtrArray(tempValue.Length + 1);
                    int structSize = Marshal.SizeOf(typeof(berval));
                    managedBervalArray = new berval[tempValue.Length];

                    for (i = 0; i < tempValue.Length; i++)
                    {
                        byte[] byteArray = tempValue[i];

                        // construct the managed berval
                        managedBervalArray[i] = new berval();

                        if (byteArray != null)
                        {
                            managedBervalArray[i].bv_len = byteArray.Length;
                            managedBervalArray[i].bv_val = Marshal.AllocHGlobal(byteArray.Length);
                            Marshal.Copy(byteArray, 0, managedBervalArray[i].bv_val, byteArray.Length);
                        }

                        // allocate memory for the unmanaged structure
                        IntPtr valPtr = Marshal.AllocHGlobal(structSize);
                        Marshal.StructureToPtr(managedBervalArray[i], valPtr, false);

                        tempPtr = (IntPtr)((long)berValArray + IntPtr.Size * i);
                        Marshal.WriteIntPtr(tempPtr, valPtr);
                    }

                    tempPtr = (IntPtr)((long)berValArray + IntPtr.Size * i);
                    Marshal.WriteIntPtr(tempPtr, IntPtr.Zero);
                }

                error = BerPal.PrintBerArray(berElement, new string(fmt, 1), berValArray);
            }
            finally
            {
                if (berValArray != IntPtr.Zero)
                {
                    for (int i = 0; i < tempValue.Length; i++)
                    {
                        IntPtr ptr = Marshal.ReadIntPtr(berValArray, IntPtr.Size * i);
                        if (ptr != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(ptr);
                        }
                    }
                    Marshal.FreeHGlobal(berValArray);
                }
                if (managedBervalArray != null)
                {
                    foreach (berval managedBerval in managedBervalArray)
                    {
                        if (managedBerval.bv_val != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(managedBerval.bv_val);
                        }
                    }
                }
            }

            return(error);
        }
コード例 #45
0
ファイル: LdapConnection.cs プロジェクト: chcosta/corefx
        internal DirectoryAttribute ConstructAttribute(IntPtr entryMessage, IntPtr attributeName)
        {
            DirectoryAttribute attribute = new DirectoryAttribute();
            attribute.isSearchResult = true;

            // get name
            string name = Marshal.PtrToStringUni(attributeName);
            attribute.Name = name;

            // get values
            IntPtr valuesArray = Wldap32.ldap_get_values_len(ldapHandle, entryMessage, name);
            try
            {
                IntPtr tempPtr = (IntPtr)0;
                int count = 0;
                if (valuesArray != (IntPtr)0)
                {
                    tempPtr = Marshal.ReadIntPtr(valuesArray, Marshal.SizeOf(typeof(IntPtr)) * count);
                    while (tempPtr != (IntPtr)0)
                    {
                        berval bervalue = new berval();
                        Marshal.PtrToStructure(tempPtr, bervalue);
                        byte[] byteArray = null;
                        if (bervalue.bv_len > 0 && bervalue.bv_val != (IntPtr)0)
                        {
                            byteArray = new byte[bervalue.bv_len];
                            Marshal.Copy(bervalue.bv_val, byteArray, 0, bervalue.bv_len);
                            attribute.Add(byteArray);
                        }

                        count++;
                        tempPtr = Marshal.ReadIntPtr(valuesArray, Marshal.SizeOf(typeof(IntPtr)) * count);
                    }
                }
            }
            finally
            {
                if (valuesArray != (IntPtr)0)
                    Wldap32.ldap_value_free_len(valuesArray);
            }

            return attribute;
        }
コード例 #46
0
 internal LdapMod[] BuildAttributes(CollectionBase directoryAttributes, ArrayList ptrToFree)
 {
     LdapMod[] modArray = null;
     UTF8Encoding encoding = new UTF8Encoding();
     DirectoryAttributeCollection attributes = null;
     DirectoryAttributeModificationCollection modifications = null;
     DirectoryAttribute attribute = null;
     if ((directoryAttributes != null) && (directoryAttributes.Count != 0))
     {
         if (directoryAttributes is DirectoryAttributeModificationCollection)
         {
             modifications = (DirectoryAttributeModificationCollection) directoryAttributes;
         }
         else
         {
             attributes = (DirectoryAttributeCollection) directoryAttributes;
         }
         modArray = new LdapMod[directoryAttributes.Count];
         for (int i = 0; i < directoryAttributes.Count; i++)
         {
             if (attributes != null)
             {
                 attribute = attributes[i];
             }
             else
             {
                 attribute = modifications[i];
             }
             modArray[i] = new LdapMod();
             if (attribute is DirectoryAttributeModification)
             {
                 modArray[i].type = (int) ((DirectoryAttributeModification) attribute).Operation;
             }
             else
             {
                 modArray[i].type = 0;
             }
             LdapMod mod1 = modArray[i];
             mod1.type |= 0x80;
             modArray[i].attribute = Marshal.StringToHGlobalUni(attribute.Name);
             int count = 0;
             berval[] bervalArray = null;
             if (attribute.Count > 0)
             {
                 count = attribute.Count;
                 bervalArray = new berval[count];
                 for (int j = 0; j < count; j++)
                 {
                     byte[] source = null;
                     if (attribute[j] is string)
                     {
                         source = encoding.GetBytes((string) attribute[j]);
                     }
                     else if (attribute[j] is Uri)
                     {
                         source = encoding.GetBytes(((Uri) attribute[j]).ToString());
                     }
                     else
                     {
                         source = (byte[]) attribute[j];
                     }
                     bervalArray[j] = new berval();
                     bervalArray[j].bv_len = source.Length;
                     bervalArray[j].bv_val = Marshal.AllocHGlobal(bervalArray[j].bv_len);
                     ptrToFree.Add(bervalArray[j].bv_val);
                     Marshal.Copy(source, 0, bervalArray[j].bv_val, bervalArray[j].bv_len);
                 }
             }
             modArray[i].values = Marshal.AllocHGlobal((int) ((count + 1) * Marshal.SizeOf(typeof(IntPtr))));
             int cb = Marshal.SizeOf(typeof(berval));
             IntPtr zero = IntPtr.Zero;
             IntPtr ptr = IntPtr.Zero;
             int index = 0;
             index = 0;
             while (index < count)
             {
                 zero = Marshal.AllocHGlobal(cb);
                 ptrToFree.Add(zero);
                 Marshal.StructureToPtr(bervalArray[index], zero, false);
                 ptr = (IntPtr) (((long) modArray[i].values) + (Marshal.SizeOf(typeof(IntPtr)) * index));
                 Marshal.WriteIntPtr(ptr, zero);
                 index++;
             }
             ptr = (IntPtr) (((long) modArray[i].values) + (Marshal.SizeOf(typeof(IntPtr)) * index));
             Marshal.WriteIntPtr(ptr, IntPtr.Zero);
         }
     }
     return modArray;
 }
コード例 #47
0
ファイル: Wldap32UnsafeMethods.cs プロジェクト: hughbe/corefx
 public static extern int ldap_compare([In] ConnectionHandle ldapHandle, string dn, string attributeName, string strValue, berval binaryValue, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber);
コード例 #48
0
ファイル: SafeHandles.cs プロジェクト: chcosta/corefx
 internal BerSafeHandle(berval value) : base(true)
 {
     SetHandle(Wldap32.ber_init(value));
     if (handle == (IntPtr)0)
         throw new BerConversionException();
 }
コード例 #49
0
ファイル: Wldap32UnsafeMethods.cs プロジェクト: hughbe/corefx
 public static extern IntPtr ber_init(berval value);
コード例 #50
0
        public static byte[] Encode(string format, params object[] value)
        {
            Utility.CheckOSVersion();
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }
            UTF8Encoding encoding = new UTF8Encoding();

            byte[] destination = null;
            if (value == null)
            {
                value = new object[0];
            }
            BerSafeHandle berElement = new BerSafeHandle();
            int           index      = 0;
            int           num2       = 0;

            for (int i = 0; i < format.Length; i++)
            {
                char c = format[i];
                switch (c)
                {
                case '{':
                case '}':
                case '[':
                case ']':
                case 'n':
                    num2 = Wldap32.ber_printf_emptyarg(berElement, new string(c, 1));
                    break;

                case 't':
                case 'i':
                case 'e':
                    if (index >= value.Length)
                    {
                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                    }
                    if (!(value[index] is int))
                    {
                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                    }
                    num2 = Wldap32.ber_printf_int(berElement, new string(c, 1), (int)value[index]);
                    index++;
                    break;

                case 'b':
                    if (index >= value.Length)
                    {
                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                    }
                    if (!(value[index] is bool))
                    {
                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                    }
                    num2 = Wldap32.ber_printf_int(berElement, new string(c, 1), ((bool)value[index]) ? 1 : 0);
                    index++;
                    break;

                case 's':
                {
                    if (index >= value.Length)
                    {
                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                    }
                    if ((value[index] != null) && !(value[index] is string))
                    {
                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                    }
                    byte[] tempValue = null;
                    if (value[index] != null)
                    {
                        tempValue = encoding.GetBytes((string)value[index]);
                    }
                    num2 = EncodingByteArrayHelper(berElement, tempValue, 'o');
                    index++;
                    break;
                }

                case 'o':
                case 'X':
                {
                    if (index >= value.Length)
                    {
                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                    }
                    if ((value[index] != null) && !(value[index] is byte[]))
                    {
                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                    }
                    byte[] buffer3 = (byte[])value[index];
                    num2 = EncodingByteArrayHelper(berElement, buffer3, c);
                    index++;
                    break;
                }

                case 'v':
                {
                    if (index >= value.Length)
                    {
                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                    }
                    if ((value[index] != null) && !(value[index] is string[]))
                    {
                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                    }
                    string[] strArray    = (string[])value[index];
                    byte[][] bufferArray = null;
                    if (strArray != null)
                    {
                        bufferArray = new byte[strArray.Length][];
                        for (int j = 0; j < strArray.Length; j++)
                        {
                            string s = strArray[j];
                            if (s == null)
                            {
                                bufferArray[j] = null;
                            }
                            else
                            {
                                bufferArray[j] = encoding.GetBytes(s);
                            }
                        }
                    }
                    num2 = EncodingMultiByteArrayHelper(berElement, bufferArray, 'V');
                    index++;
                    break;
                }

                default:
                {
                    if (c != 'V')
                    {
                        throw new ArgumentException(Res.GetString("BerConverterUndefineChar"));
                    }
                    if (index >= value.Length)
                    {
                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                    }
                    if ((value[index] != null) && !(value[index] is byte[][]))
                    {
                        throw new ArgumentException(Res.GetString("BerConverterNotMatch"));
                    }
                    byte[][] bufferArray2 = (byte[][])value[index];
                    num2 = EncodingMultiByteArrayHelper(berElement, bufferArray2, c);
                    index++;
                    break;
                }
                }
                if (num2 == -1)
                {
                    throw new BerConversionException();
                }
            }
            berval structure = new berval();
            IntPtr zero      = IntPtr.Zero;

            try
            {
                if (Wldap32.ber_flatten(berElement, ref zero) == -1)
                {
                    throw new BerConversionException();
                }
                if (zero != IntPtr.Zero)
                {
                    Marshal.PtrToStructure(zero, structure);
                }
                if ((structure == null) || (structure.bv_len == 0))
                {
                    return(new byte[0]);
                }
                destination = new byte[structure.bv_len];
                Marshal.Copy(structure.bv_val, destination, 0, structure.bv_len);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Wldap32.ber_bvfree(zero);
                }
            }
            return(destination);
        }
コード例 #51
0
ファイル: LdapPal.Linux.cs プロジェクト: wilfriedb/runtime
 internal static int CompareDirectoryEntries(ConnectionHandle ldapHandle, string dn, string attributeName, string strValue, berval binaryValue, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber) =>
                         Interop.Ldap.ldap_compare(ldapHandle, dn, attributeName, binaryValue, servercontrol, clientcontrol, ref messageNumber);
コード例 #52
0
        internal static object[] TryDecode(string format, byte[] value, out bool decodeSucceeded)
        {
            Utility.CheckOSVersion();
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }
            UTF8Encoding  encoding   = new UTF8Encoding(false, true);
            berval        berval     = new berval();
            ArrayList     list       = new ArrayList();
            BerSafeHandle berElement = null;

            object[] objArray = null;
            decodeSucceeded = false;
            if (value == null)
            {
                berval.bv_len = 0;
                berval.bv_val = IntPtr.Zero;
            }
            else
            {
                berval.bv_len = value.Length;
                berval.bv_val = Marshal.AllocHGlobal(value.Length);
                Marshal.Copy(value, 0, berval.bv_val, value.Length);
            }
            try
            {
                berElement = new BerSafeHandle(berval);
            }
            finally
            {
                if (berval.bv_val != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(berval.bv_val);
                }
            }
            int error = 0;

            for (int i = 0; i < format.Length; i++)
            {
                char c = format[i];
                switch (c)
                {
                case '{':
                case '}':
                case '[':
                case ']':
                case 'n':
                case 'x':
                    error = Wldap32.ber_scanf(berElement, new string(c, 1));
                    if (error == 0)
                    {
                    }
                    break;

                case 'i':
                case 'e':
                case 'b':
                {
                    int num3 = 0;
                    error = Wldap32.ber_scanf_int(berElement, new string(c, 1), ref num3);
                    if (error == 0)
                    {
                        if (c == 'b')
                        {
                            bool flag = false;
                            if (num3 == 0)
                            {
                                flag = false;
                            }
                            else
                            {
                                flag = true;
                            }
                            list.Add(flag);
                        }
                        else
                        {
                            list.Add(num3);
                        }
                    }
                    break;
                }

                case 'a':
                {
                    byte[] bytes = DecodingByteArrayHelper(berElement, 'O', ref error);
                    if (error == 0)
                    {
                        string str = null;
                        if (bytes != null)
                        {
                            str = encoding.GetString(bytes);
                        }
                        list.Add(str);
                    }
                    break;
                }

                case 'O':
                {
                    byte[] buffer2 = DecodingByteArrayHelper(berElement, c, ref error);
                    if (error == 0)
                    {
                        list.Add(buffer2);
                    }
                    break;
                }

                case 'B':
                {
                    IntPtr zero   = IntPtr.Zero;
                    int    length = 0;
                    error = Wldap32.ber_scanf_bitstring(berElement, "B", ref zero, ref length);
                    if (error == 0)
                    {
                        byte[] destination = null;
                        if (zero != IntPtr.Zero)
                        {
                            destination = new byte[length];
                            Marshal.Copy(zero, destination, 0, length);
                        }
                        list.Add(destination);
                    }
                    break;
                }

                case 'v':
                {
                    byte[][] bufferArray = null;
                    string[] strArray    = null;
                    bufferArray = DecodingMultiByteArrayHelper(berElement, 'V', ref error);
                    if (error == 0)
                    {
                        if (bufferArray != null)
                        {
                            strArray = new string[bufferArray.Length];
                            for (int k = 0; k < bufferArray.Length; k++)
                            {
                                if (bufferArray[k] == null)
                                {
                                    strArray[k] = null;
                                }
                                else
                                {
                                    strArray[k] = encoding.GetString(bufferArray[k]);
                                }
                            }
                        }
                        list.Add(strArray);
                    }
                    break;
                }

                default:
                {
                    if (c != 'V')
                    {
                        throw new ArgumentException(Res.GetString("BerConverterUndefineChar"));
                    }
                    byte[][] bufferArray2 = null;
                    bufferArray2 = DecodingMultiByteArrayHelper(berElement, c, ref error);
                    if (error == 0)
                    {
                        list.Add(bufferArray2);
                    }
                    break;
                }
                }
                if (error != 0)
                {
                    return(objArray);
                }
            }
            objArray = new object[list.Count];
            for (int j = 0; j < list.Count; j++)
            {
                objArray[j] = list[j];
            }
            decodeSucceeded = true;
            return(objArray);
        }
コード例 #53
0
        public static byte[] Encode(string format, params object[] value)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            // no need to turn on invalid encoding detection as we just do string->byte[] conversion.
            UTF8Encoding utf8Encoder = new UTF8Encoding();

            byte[] encodingResult = null;
            // value is allowed to be null in certain scenario, so if it is null, just set it to empty array.
            if (value == null)
            {
                value = Array.Empty <object>();
            }

            Debug.WriteLine("Begin encoding\n");

            // allocate the berelement
            SafeBerHandle berElement = new SafeBerHandle();

            int valueCount = 0;
            int error      = 0;

            for (int formatCount = 0; formatCount < format.Length; formatCount++)
            {
                char fmt = format[formatCount];
                if (fmt == '{' || fmt == '}' || fmt == '[' || fmt == ']' || fmt == 'n')
                {
                    // no argument needed
                    error = BerPal.PrintEmptyArgument(berElement, new string(fmt, 1));
                }
                else if (fmt == 't' || fmt == 'i' || fmt == 'e')
                {
                    if (valueCount >= value.Length)
                    {
                        // we don't have enough argument for the format string
                        Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
                        throw new ArgumentException(SR.BerConverterNotMatch);
                    }

                    if (!(value[valueCount] is int))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be int\n");
                        throw new ArgumentException(SR.BerConverterNotMatch);
                    }

                    // one int argument
                    error = BerPal.PrintInt(berElement, new string(fmt, 1), (int)value[valueCount]);

                    // increase the value count
                    valueCount++;
                }
                else if (fmt == 'b')
                {
                    if (valueCount >= value.Length)
                    {
                        // we don't have enough argument for the format string
                        Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
                        throw new ArgumentException(SR.BerConverterNotMatch);
                    }

                    if (!(value[valueCount] is bool))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be boolean\n");
                        throw new ArgumentException(SR.BerConverterNotMatch);
                    }

                    // one int argument
                    error = BerPal.PrintInt(berElement, new string(fmt, 1), (bool)value[valueCount] ? 1 : 0);

                    // increase the value count
                    valueCount++;
                }
                else if (fmt == 's')
                {
                    if (valueCount >= value.Length)
                    {
                        // we don't have enough argument for the format string
                        Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
                        throw new ArgumentException(SR.BerConverterNotMatch);
                    }

                    if (value[valueCount] != null && !(value[valueCount] is string))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be string, but receiving value has type of ");
                        Debug.WriteLine(value[valueCount].GetType());
                        throw new ArgumentException(SR.BerConverterNotMatch);
                    }

                    // one string argument
                    byte[] tempValue = null;
                    if (value[valueCount] != null)
                    {
                        tempValue = utf8Encoder.GetBytes((string)value[valueCount]);
                    }
                    error = EncodingByteArrayHelper(berElement, tempValue, 'o');

                    // increase the value count
                    valueCount++;
                }
                else if (fmt == 'o' || fmt == 'X')
                {
                    // we need to have one arguments
                    if (valueCount >= value.Length)
                    {
                        // we don't have enough argument for the format string
                        Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
                        throw new ArgumentException(SR.BerConverterNotMatch);
                    }

                    if (value[valueCount] != null && !(value[valueCount] is byte[]))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be byte[], but receiving value has type of ");
                        Debug.WriteLine(value[valueCount].GetType());
                        throw new ArgumentException(SR.BerConverterNotMatch);
                    }

                    byte[] tempValue = (byte[])value[valueCount];
                    error = EncodingByteArrayHelper(berElement, tempValue, fmt);

                    valueCount++;
                }
                else if (fmt == 'v')
                {
                    // we need to have one arguments
                    if (valueCount >= value.Length)
                    {
                        // we don't have enough argument for the format string
                        Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
                        throw new ArgumentException(SR.BerConverterNotMatch);
                    }

                    if (value[valueCount] != null && !(value[valueCount] is string[]))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be string[], but receiving value has type of ");
                        Debug.WriteLine(value[valueCount].GetType());
                        throw new ArgumentException(SR.BerConverterNotMatch);
                    }

                    string[] stringValues = (string[])value[valueCount];
                    byte[][] tempValues   = null;
                    if (stringValues != null)
                    {
                        tempValues = new byte[stringValues.Length][];
                        for (int i = 0; i < stringValues.Length; i++)
                        {
                            string s = stringValues[i];
                            if (s == null)
                            {
                                tempValues[i] = null;
                            }
                            else
                            {
                                tempValues[i] = utf8Encoder.GetBytes(s);
                            }
                        }
                    }

                    error = EncodingMultiByteArrayHelper(berElement, tempValues, 'V');

                    valueCount++;
                }
                else if (fmt == 'V')
                {
                    // we need to have one arguments
                    if (valueCount >= value.Length)
                    {
                        // we don't have enough argument for the format string
                        Debug.WriteLine("value argument is not valid, valueCount >= value.Length\n");
                        throw new ArgumentException(SR.BerConverterNotMatch);
                    }

                    if (value[valueCount] != null && !(value[valueCount] is byte[][]))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be byte[][], but receiving value has type of ");
                        Debug.WriteLine(value[valueCount].GetType());
                        throw new ArgumentException(SR.BerConverterNotMatch);
                    }

                    byte[][] tempValue = (byte[][])value[valueCount];

                    error = EncodingMultiByteArrayHelper(berElement, tempValue, fmt);

                    valueCount++;
                }
                else
                {
                    Debug.WriteLine("Format string contains undefined character: ");
                    Debug.WriteLine(new string(fmt, 1));
                    throw new ArgumentException(SR.BerConverterUndefineChar);
                }

                // process the return value
                if (error == -1)
                {
                    Debug.WriteLine("ber_printf failed\n");
                    throw new BerConversionException();
                }
            }

            // get the binary value back
            berval binaryValue = new berval();
            IntPtr flattenptr  = IntPtr.Zero;

            try
            {
                // can't use SafeBerval here as CLR creates a SafeBerval which points to a different memory location, but when doing memory
                // deallocation, wldap has special check. So have to use IntPtr directly here.
                error = BerPal.FlattenBerElement(berElement, ref flattenptr);

                if (error == -1)
                {
                    Debug.WriteLine("ber_flatten failed\n");
                    throw new BerConversionException();
                }

                if (flattenptr != IntPtr.Zero)
                {
                    Marshal.PtrToStructure(flattenptr, binaryValue);
                }

                if (binaryValue == null || binaryValue.bv_len == 0)
                {
                    encodingResult = Array.Empty <byte>();
                }
                else
                {
                    encodingResult = new byte[binaryValue.bv_len];

                    Marshal.Copy(binaryValue.bv_val, encodingResult, 0, binaryValue.bv_len);
                }
            }
            finally
            {
                if (flattenptr != IntPtr.Zero)
                {
                    BerPal.FreeBerval(flattenptr);
                }
            }

            return(encodingResult);
        }
コード例 #54
0
        internal DirectoryResponse ConstructResponse(int messageId, LdapOperation operation, ResultAll resultType, TimeSpan requestTimeOut, bool exceptionOnTimeOut)
        {
            LDAP_TIMEVAL timeout = new LDAP_TIMEVAL {
                tv_sec = (int) (requestTimeOut.Ticks / 0x989680L)
            };
            IntPtr zero = IntPtr.Zero;
            DirectoryResponse response = null;
            IntPtr oid = IntPtr.Zero;
            IntPtr data = IntPtr.Zero;
            IntPtr entryMessage = IntPtr.Zero;
            bool flag = true;
            if (resultType != ResultAll.LDAP_MSG_ALL)
            {
                timeout.tv_sec = 0;
                timeout.tv_usec = 0;
                if (resultType == ResultAll.LDAP_MSG_POLLINGALL)
                {
                    resultType = ResultAll.LDAP_MSG_ALL;
                }
                flag = false;
            }
            int error = Wldap32.ldap_result(this.ldapHandle, messageId, (int) resultType, timeout, ref zero);
            switch (error)
            {
                case -1:
                case 0:
                    break;

                default:
                {
                    int serverError = 0;
                    try
                    {
                        int errorCode = 0;
                        string responseDn = null;
                        string responseMessage = null;
                        Uri[] responseReferral = null;
                        DirectoryControl[] responseControl = null;
                        if ((error != 100) && (error != 0x73))
                        {
                            errorCode = this.ConstructParsedResult(zero, ref serverError, ref responseDn, ref responseMessage, ref responseReferral, ref responseControl);
                        }
                        if (errorCode == 0)
                        {
                            errorCode = serverError;
                            switch (error)
                            {
                                case 0x69:
                                    response = new AddResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    break;

                                case 0x67:
                                    response = new ModifyResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    break;

                                case 0x6b:
                                    response = new DeleteResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    break;

                                case 0x6d:
                                    response = new ModifyDNResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    break;

                                case 0x6f:
                                    response = new CompareResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    break;

                                case 120:
                                    response = new ExtendedResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    if (errorCode == 0)
                                    {
                                        errorCode = Wldap32.ldap_parse_extended_result(this.ldapHandle, zero, ref oid, ref data, 0);
                                        if (errorCode == 0)
                                        {
                                            string str3 = null;
                                            if (oid != IntPtr.Zero)
                                            {
                                                str3 = Marshal.PtrToStringUni(oid);
                                            }
                                            berval structure = null;
                                            byte[] destination = null;
                                            if (data != IntPtr.Zero)
                                            {
                                                structure = new berval();
                                                Marshal.PtrToStructure(data, structure);
                                                if ((structure.bv_len != 0) && (structure.bv_val != IntPtr.Zero))
                                                {
                                                    destination = new byte[structure.bv_len];
                                                    Marshal.Copy(structure.bv_val, destination, 0, structure.bv_len);
                                                }
                                            }
                                            ((ExtendedResponse) response).name = str3;
                                            ((ExtendedResponse) response).value = destination;
                                        }
                                    }
                                    break;

                                case 0x65:
                                case 100:
                                case 0x73:
                                {
                                    response = new SearchResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    if (error == 0x65)
                                    {
                                        ((SearchResponse) response).searchDone = true;
                                    }
                                    SearchResultEntryCollection col = new SearchResultEntryCollection();
                                    SearchResultReferenceCollection references = new SearchResultReferenceCollection();
                                    entryMessage = Wldap32.ldap_first_entry(this.ldapHandle, zero);
                                    int num4 = 0;
                                    while (entryMessage != IntPtr.Zero)
                                    {
                                        SearchResultEntry entry = this.ConstructEntry(entryMessage);
                                        if (entry != null)
                                        {
                                            col.Add(entry);
                                        }
                                        num4++;
                                        entryMessage = Wldap32.ldap_next_entry(this.ldapHandle, entryMessage);
                                    }
                                    for (IntPtr ptr5 = Wldap32.ldap_first_reference(this.ldapHandle, zero); ptr5 != IntPtr.Zero; ptr5 = Wldap32.ldap_next_reference(this.ldapHandle, ptr5))
                                    {
                                        SearchResultReference reference = this.ConstructReference(ptr5);
                                        if (reference != null)
                                        {
                                            references.Add(reference);
                                        }
                                    }
                                    ((SearchResponse) response).SetEntries(col);
                                    ((SearchResponse) response).SetReferences(references);
                                    break;
                                }
                            }
                            switch (errorCode)
                            {
                                case 0:
                                case 5:
                                case 6:
                                case 10:
                                case 9:
                                    return response;

                                default:
                                    if (Utility.IsResultCode((ResultCode) errorCode))
                                    {
                                        throw new DirectoryOperationException(response, OperationErrorMappings.MapResultCode(errorCode));
                                    }
                                    throw new DirectoryOperationException(response);
                            }
                        }
                        error = errorCode;
                        goto Label_03A7;
                    }
                    finally
                    {
                        if (oid != IntPtr.Zero)
                        {
                            Wldap32.ldap_memfree(oid);
                        }
                        if (data != IntPtr.Zero)
                        {
                            Wldap32.ldap_memfree(data);
                        }
                        if (zero != IntPtr.Zero)
                        {
                            Wldap32.ldap_msgfree(zero);
                        }
                    }
                    break;
                }
            }
            if (error == 0)
            {
                if (!exceptionOnTimeOut)
                {
                    return null;
                }
                error = 0x55;
            }
            else
            {
                error = Wldap32.LdapGetLastError();
            }
            if (flag)
            {
                Wldap32.ldap_abandon(this.ldapHandle, messageId);
            }
        Label_03A7:
            throw this.ConstructException(error, operation);
        }
コード例 #55
0
ファイル: DirectoryControl.cs プロジェクト: davzucky/corefx
        public override byte[] GetValue()
        {
            IntPtr control    = (IntPtr)0;
            int    structSize = Marshal.SizeOf(typeof(SortKey));
            int    keyCount   = _keys.Length;
            IntPtr memHandle  = Utility.AllocHGlobalIntPtrArray(keyCount + 1);

            try
            {
                IntPtr tempPtr = (IntPtr)0;
                IntPtr sortPtr = (IntPtr)0;
                int    i       = 0;
                for (i = 0; i < keyCount; i++)
                {
                    sortPtr = Marshal.AllocHGlobal(structSize);
                    Marshal.StructureToPtr(_keys[i], sortPtr, false);
                    tempPtr = (IntPtr)((long)memHandle + Marshal.SizeOf(typeof(IntPtr)) * i);
                    Marshal.WriteIntPtr(tempPtr, sortPtr);
                }
                tempPtr = (IntPtr)((long)memHandle + Marshal.SizeOf(typeof(IntPtr)) * i);
                Marshal.WriteIntPtr(tempPtr, (IntPtr)0);

                bool critical = IsCritical;
                int  error    = Wldap32.ldap_create_sort_control(UtilityHandle.GetHandle(), memHandle, critical ? (byte)1 : (byte)0, ref control);

                if (error != 0)
                {
                    if (Utility.IsLdapError((LdapError)error))
                    {
                        string errorMessage = LdapErrorMappings.MapResultCode(error);
                        throw new LdapException(error, errorMessage);
                    }
                    else
                    {
                        throw new LdapException(error);
                    }
                }

                LdapControl managedControl = new LdapControl();
                Marshal.PtrToStructure(control, managedControl);
                berval value = managedControl.ldctl_value;
                // reinitialize the value
                directoryControlValue = null;
                if (value != null)
                {
                    directoryControlValue = new byte[value.bv_len];
                    Marshal.Copy(value.bv_val, directoryControlValue, 0, value.bv_len);
                }
            }
            finally
            {
                if (control != (IntPtr)0)
                {
                    Wldap32.ldap_control_free(control);
                }

                if (memHandle != (IntPtr)0)
                {
                    //release the memory from the heap
                    for (int i = 0; i < keyCount; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(memHandle, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (tempPtr != (IntPtr)0)
                        {
                            // free the marshalled name
                            IntPtr ptr = Marshal.ReadIntPtr(tempPtr);
                            if (ptr != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(ptr);
                            }
                            // free the marshalled rule
                            ptr = Marshal.ReadIntPtr(tempPtr, Marshal.SizeOf(typeof(IntPtr)));
                            if (ptr != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(ptr);
                            }

                            Marshal.FreeHGlobal(tempPtr);
                        }
                    }
                    Marshal.FreeHGlobal(memHandle);
                }
            }

            return(base.GetValue());
        }
コード例 #56
0
ファイル: LdapConnection.cs プロジェクト: nickchal/pash
		internal DirectoryResponse ConstructResponse(int messageId, LdapOperation operation, ResultAll resultType, TimeSpan requestTimeOut, bool exceptionOnTimeOut)
		{
			DirectoryResponse directoryResponse;
			LDAP_TIMEVAL lDAPTIMEVAL = new LDAP_TIMEVAL();
			lDAPTIMEVAL.tv_sec = (int)(requestTimeOut.Ticks / (long)0x989680);
			IntPtr intPtr = (IntPtr)0;
			DirectoryResponse searchResponse = null;
			IntPtr intPtr1 = (IntPtr)0;
			IntPtr intPtr2 = (IntPtr)0;
			bool flag = true;
			if (resultType != ResultAll.LDAP_MSG_ALL)
			{
				lDAPTIMEVAL.tv_sec = 0;
				lDAPTIMEVAL.tv_usec = 0;
				if (resultType == ResultAll.LDAP_MSG_POLLINGALL)
				{
					resultType = ResultAll.LDAP_MSG_ALL;
				}
				flag = false;
			}
			int num = Wldap32.ldap_result(this.ldapHandle, messageId, (int)resultType, lDAPTIMEVAL, ref intPtr);
			if (num == -1 || num == 0)
			{
				if (num != 0)
				{
					num = Wldap32.LdapGetLastError();
				}
				else
				{
					if (!exceptionOnTimeOut)
					{
						return null;
					}
					else
					{
						num = 85;
					}
				}
				if (flag)
				{
					Wldap32.ldap_abandon(this.ldapHandle, messageId);
				}
			}
			else
			{
				int num1 = 0;
				try
				{
					int num2 = 0;
					string str = null;
					string str1 = null;
					Uri[] uriArray = null;
					DirectoryControl[] directoryControlArray = null;
					if (num != 100 && num != 115)
					{
						num2 = this.ConstructParsedResult(intPtr, ref num1, ref str, ref str1, ref uriArray, ref directoryControlArray);
					}
					if (num2 != 0)
					{
						num = num2;
						throw this.ConstructException(num, operation);
					}
					else
					{
						num2 = num1;
						if (num != 105)
						{
							if (num != 103)
							{
								if (num != 107)
								{
									if (num != 109)
									{
										if (num != 111)
										{
											if (num != 120)
											{
												if (num == 101 || num == 100 || num == 115)
												{
													searchResponse = new SearchResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
													if (num == 101)
													{
														((SearchResponse)searchResponse).searchDone = true;
													}
													SearchResultEntryCollection searchResultEntryCollection = new SearchResultEntryCollection();
													SearchResultReferenceCollection searchResultReferenceCollection = new SearchResultReferenceCollection();
													IntPtr intPtr3 = Wldap32.ldap_first_entry(this.ldapHandle, intPtr);
													int num3 = 0;
													while (intPtr3 != (IntPtr)0)
													{
														SearchResultEntry searchResultEntry = this.ConstructEntry(intPtr3);
														if (searchResultEntry != null)
														{
															searchResultEntryCollection.Add(searchResultEntry);
														}
														num3++;
														intPtr3 = Wldap32.ldap_next_entry(this.ldapHandle, intPtr3);
													}
													IntPtr intPtr4 = Wldap32.ldap_first_reference(this.ldapHandle, intPtr);
													while (intPtr4 != (IntPtr)0)
													{
														SearchResultReference searchResultReference = this.ConstructReference(intPtr4);
														if (searchResultReference != null)
														{
															searchResultReferenceCollection.Add(searchResultReference);
														}
														intPtr4 = Wldap32.ldap_next_reference(this.ldapHandle, intPtr4);
													}
													((SearchResponse)searchResponse).SetEntries(searchResultEntryCollection);
													((SearchResponse)searchResponse).SetReferences(searchResultReferenceCollection);
												}
											}
											else
											{
												searchResponse = new ExtendedResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
												if (num2 == 0)
												{
													num2 = Wldap32.ldap_parse_extended_result(this.ldapHandle, intPtr, ref intPtr1, ref intPtr2, 0);
													if (num2 == 0)
													{
														string stringUni = null;
														if (intPtr1 != (IntPtr)0)
														{
															stringUni = Marshal.PtrToStringUni(intPtr1);
														}
														byte[] numArray = null;
														if (intPtr2 != (IntPtr)0)
														{
															berval _berval = new berval();
															Marshal.PtrToStructure(intPtr2, _berval);
															if (_berval.bv_len != 0 && _berval.bv_val != (IntPtr)0)
															{
																numArray = new byte[_berval.bv_len];
																Marshal.Copy(_berval.bv_val, numArray, 0, _berval.bv_len);
															}
														}
														((ExtendedResponse)searchResponse).name = stringUni;
														((ExtendedResponse)searchResponse).@value = numArray;
													}
												}
											}
										}
										else
										{
											searchResponse = new CompareResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
										}
									}
									else
									{
										searchResponse = new ModifyDNResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
									}
								}
								else
								{
									searchResponse = new DeleteResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
								}
							}
							else
							{
								searchResponse = new ModifyResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
							}
						}
						else
						{
							searchResponse = new AddResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
						}
						if (num2 == 0 || num2 == 5 || num2 == 6 || num2 == 10 || num2 == 9)
						{
							directoryResponse = searchResponse;
						}
						else
						{
							if (!Utility.IsResultCode((ResultCode)num2))
							{
								throw new DirectoryOperationException(searchResponse);
							}
							else
							{
								throw new DirectoryOperationException(searchResponse, OperationErrorMappings.MapResultCode(num2));
							}
						}
					}
				}
				finally
				{
					if (intPtr1 != (IntPtr)0)
					{
						Wldap32.ldap_memfree(intPtr1);
					}
					if (intPtr2 != (IntPtr)0)
					{
						Wldap32.ldap_memfree(intPtr2);
					}
					if (intPtr != (IntPtr)0)
					{
						Wldap32.ldap_msgfree(intPtr);
					}
				}
				return directoryResponse;
			}
			throw this.ConstructException(num, operation);
		}
コード例 #57
0
        internal static object[] TryDecode(string format, byte[] value, out bool decodeSucceeded)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            Debug.WriteLine("Begin decoding\n");

            UTF8Encoding  utf8Encoder = new UTF8Encoding(false, true);
            berval        berValue    = new berval();
            ArrayList     resultList  = new ArrayList();
            BerSafeHandle berElement  = null;

            object[] decodeResult = null;
            decodeSucceeded = false;

            if (value == null)
            {
                berValue.bv_len = 0;
                berValue.bv_val = IntPtr.Zero;
            }
            else
            {
                berValue.bv_len = value.Length;
                berValue.bv_val = Marshal.AllocHGlobal(value.Length);
                Marshal.Copy(value, 0, berValue.bv_val, value.Length);
            }

            try
            {
                berElement = new BerSafeHandle(berValue);
            }
            finally
            {
                if (berValue.bv_val != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(berValue.bv_val);
                }
            }

            int error = 0;

            for (int formatCount = 0; formatCount < format.Length; formatCount++)
            {
                char fmt = format[formatCount];
                if (fmt == '{' || fmt == '}' || fmt == '[' || fmt == ']' || fmt == 'n' || fmt == 'x')
                {
                    error = Wldap32.ber_scanf(berElement, new string(fmt, 1));

                    if (error != 0)
                    {
                        Debug.WriteLine("ber_scanf for {, }, [, ], n or x failed");
                    }
                }
                else if (fmt == 'i' || fmt == 'e' || fmt == 'b')
                {
                    int result = 0;
                    error = Wldap32.ber_scanf_int(berElement, new string(fmt, 1), ref result);

                    if (error == 0)
                    {
                        if (fmt == 'b')
                        {
                            // should return a bool
                            bool boolResult = false;
                            if (result == 0)
                            {
                                boolResult = false;
                            }
                            else
                            {
                                boolResult = true;
                            }
                            resultList.Add(boolResult);
                        }
                        else
                        {
                            resultList.Add(result);
                        }
                    }
                    else
                    {
                        Debug.WriteLine("ber_scanf for format character 'i', 'e' or 'b' failed");
                    }
                }
                else if (fmt == 'a')
                {
                    // return a string
                    byte[] byteArray = DecodingByteArrayHelper(berElement, 'O', ref error);
                    if (error == 0)
                    {
                        string s = null;
                        if (byteArray != null)
                        {
                            s = utf8Encoder.GetString(byteArray);
                        }

                        resultList.Add(s);
                    }
                }
                else if (fmt == 'O')
                {
                    // return berval
                    byte[] byteArray = DecodingByteArrayHelper(berElement, fmt, ref error);
                    if (error == 0)
                    {
                        // add result to the list
                        resultList.Add(byteArray);
                    }
                }
                else if (fmt == 'B')
                {
                    // return a bitstring and its length
                    IntPtr ptrResult = IntPtr.Zero;
                    int    length    = 0;
                    error = Wldap32.ber_scanf_bitstring(berElement, "B", ref ptrResult, ref length);

                    if (error == 0)
                    {
                        byte[] byteArray = null;
                        if (ptrResult != IntPtr.Zero)
                        {
                            byteArray = new byte[length];
                            Marshal.Copy(ptrResult, byteArray, 0, length);
                        }
                        resultList.Add(byteArray);
                    }
                    else
                    {
                        Debug.WriteLine("ber_scanf for format character 'B' failed");
                    }

                    // no need to free memory as wldap32 returns the original pointer instead of a duplicating memory pointer that
                    // needs to be freed
                }
                else if (fmt == 'v')
                {
                    //null terminate strings
                    byte[][] byteArrayresult = null;
                    string[] stringArray     = null;

                    byteArrayresult = DecodingMultiByteArrayHelper(berElement, 'V', ref error);
                    if (error == 0)
                    {
                        if (byteArrayresult != null)
                        {
                            stringArray = new string[byteArrayresult.Length];
                            for (int i = 0; i < byteArrayresult.Length; i++)
                            {
                                if (byteArrayresult[i] == null)
                                {
                                    stringArray[i] = null;
                                }
                                else
                                {
                                    stringArray[i] = utf8Encoder.GetString(byteArrayresult[i]);
                                }
                            }
                        }

                        resultList.Add(stringArray);
                    }
                }
                else if (fmt == 'V')
                {
                    byte[][] result = null;

                    result = DecodingMultiByteArrayHelper(berElement, fmt, ref error);
                    if (error == 0)
                    {
                        resultList.Add(result);
                    }
                }
                else
                {
                    Debug.WriteLine("Format string contains undefined character\n");
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.BerConverterUndefineChar));
                }

                if (error != 0)
                {
                    // decode failed, just return
                    return(decodeResult);
                }
            }

            decodeResult = new object[resultList.Count];
            for (int count = 0; count < resultList.Count; count++)
            {
                decodeResult[count] = resultList[count];
            }

            decodeSucceeded = true;
            return(decodeResult);
        }
コード例 #58
0
 private int SendRequestHelper(DirectoryRequest request, ref int messageID)
 {
     int num19;
     IntPtr zero = IntPtr.Zero;
     LdapControl[] controlArray = null;
     IntPtr clientcontrol = IntPtr.Zero;
     LdapControl[] controlArray2 = null;
     string strValue = null;
     ArrayList ptrToFree = new ArrayList();
     LdapMod[] modArray = null;
     IntPtr attrs = IntPtr.Zero;
     int num = 0;
     berval binaryValue = null;
     IntPtr attributes = IntPtr.Zero;
     int num2 = 0;
     int num3 = 0;
     if (!this.connected)
     {
         this.Connect();
         this.connected = true;
     }
     if ((this.AutoBind && (!this.bounded || this.needRebind)) && !((LdapDirectoryIdentifier) this.Directory).Connectionless)
     {
         this.Bind();
     }
     try
     {
         IntPtr ptr = IntPtr.Zero;
         IntPtr ptr6 = IntPtr.Zero;
         controlArray = this.BuildControlArray(request.Controls, true);
         int cb = Marshal.SizeOf(typeof(LdapControl));
         if (controlArray != null)
         {
             zero = Marshal.AllocHGlobal((int) (Marshal.SizeOf(typeof(IntPtr)) * (controlArray.Length + 1)));
             for (int i = 0; i < controlArray.Length; i++)
             {
                 ptr = Marshal.AllocHGlobal(cb);
                 Marshal.StructureToPtr(controlArray[i], ptr, false);
                 ptr6 = (IntPtr) (((long) zero) + (Marshal.SizeOf(typeof(IntPtr)) * i));
                 Marshal.WriteIntPtr(ptr6, ptr);
             }
             ptr6 = (IntPtr) (((long) zero) + (Marshal.SizeOf(typeof(IntPtr)) * controlArray.Length));
             Marshal.WriteIntPtr(ptr6, IntPtr.Zero);
         }
         controlArray2 = this.BuildControlArray(request.Controls, false);
         if (controlArray2 != null)
         {
             clientcontrol = Marshal.AllocHGlobal((int) (Marshal.SizeOf(typeof(IntPtr)) * (controlArray2.Length + 1)));
             for (int j = 0; j < controlArray2.Length; j++)
             {
                 ptr = Marshal.AllocHGlobal(cb);
                 Marshal.StructureToPtr(controlArray2[j], ptr, false);
                 ptr6 = (IntPtr) (((long) clientcontrol) + (Marshal.SizeOf(typeof(IntPtr)) * j));
                 Marshal.WriteIntPtr(ptr6, ptr);
             }
             ptr6 = (IntPtr) (((long) clientcontrol) + (Marshal.SizeOf(typeof(IntPtr)) * controlArray2.Length));
             Marshal.WriteIntPtr(ptr6, IntPtr.Zero);
         }
         if (request is DeleteRequest)
         {
             num3 = Wldap32.ldap_delete_ext(this.ldapHandle, ((DeleteRequest) request).DistinguishedName, zero, clientcontrol, ref messageID);
         }
         else if (request is ModifyDNRequest)
         {
             num3 = Wldap32.ldap_rename(this.ldapHandle, ((ModifyDNRequest) request).DistinguishedName, ((ModifyDNRequest) request).NewName, ((ModifyDNRequest) request).NewParentDistinguishedName, ((ModifyDNRequest) request).DeleteOldRdn ? 1 : 0, zero, clientcontrol, ref messageID);
         }
         else if (request is CompareRequest)
         {
             DirectoryAttribute assertion = ((CompareRequest) request).Assertion;
             if (assertion == null)
             {
                 throw new ArgumentException(System.DirectoryServices.Protocols.Res.GetString("WrongAssertionCompare"));
             }
             if (assertion.Count != 1)
             {
                 throw new ArgumentException(System.DirectoryServices.Protocols.Res.GetString("WrongNumValuesCompare"));
             }
             byte[] source = assertion[0] as byte[];
             if (source != null)
             {
                 if ((source != null) && (source.Length != 0))
                 {
                     binaryValue = new berval {
                         bv_len = source.Length,
                         bv_val = Marshal.AllocHGlobal(source.Length)
                     };
                     Marshal.Copy(source, 0, binaryValue.bv_val, source.Length);
                 }
             }
             else
             {
                 strValue = assertion[0].ToString();
             }
             num3 = Wldap32.ldap_compare(this.ldapHandle, ((CompareRequest) request).DistinguishedName, assertion.Name, strValue, binaryValue, zero, clientcontrol, ref messageID);
         }
         else if ((request is AddRequest) || (request is ModifyRequest))
         {
             if (request is AddRequest)
             {
                 modArray = this.BuildAttributes(((AddRequest) request).Attributes, ptrToFree);
             }
             else
             {
                 modArray = this.BuildAttributes(((ModifyRequest) request).Modifications, ptrToFree);
             }
             num = (modArray == null) ? 1 : (modArray.Length + 1);
             attrs = Marshal.AllocHGlobal((int) (Marshal.SizeOf(typeof(IntPtr)) * num));
             int num7 = Marshal.SizeOf(typeof(LdapMod));
             int index = 0;
             index = 0;
             while (index < (num - 1))
             {
                 ptr = Marshal.AllocHGlobal(num7);
                 Marshal.StructureToPtr(modArray[index], ptr, false);
                 ptr6 = (IntPtr) (((long) attrs) + (Marshal.SizeOf(typeof(IntPtr)) * index));
                 Marshal.WriteIntPtr(ptr6, ptr);
                 index++;
             }
             ptr6 = (IntPtr) (((long) attrs) + (Marshal.SizeOf(typeof(IntPtr)) * index));
             Marshal.WriteIntPtr(ptr6, IntPtr.Zero);
             if (request is AddRequest)
             {
                 num3 = Wldap32.ldap_add(this.ldapHandle, ((AddRequest) request).DistinguishedName, attrs, zero, clientcontrol, ref messageID);
             }
             else
             {
                 num3 = Wldap32.ldap_modify(this.ldapHandle, ((ModifyRequest) request).DistinguishedName, attrs, zero, clientcontrol, ref messageID);
             }
         }
         else if (request is ExtendedRequest)
         {
             string requestName = ((ExtendedRequest) request).RequestName;
             byte[] requestValue = ((ExtendedRequest) request).RequestValue;
             if ((requestValue != null) && (requestValue.Length != 0))
             {
                 binaryValue = new berval {
                     bv_len = requestValue.Length,
                     bv_val = Marshal.AllocHGlobal(requestValue.Length)
                 };
                 Marshal.Copy(requestValue, 0, binaryValue.bv_val, requestValue.Length);
             }
             num3 = Wldap32.ldap_extended_operation(this.ldapHandle, requestName, binaryValue, zero, clientcontrol, ref messageID);
         }
         else
         {
             if (request is SearchRequest)
             {
                 SearchRequest request2 = (SearchRequest) request;
                 object filter = request2.Filter;
                 if ((filter != null) && (filter is XmlDocument))
                 {
                     throw new ArgumentException(System.DirectoryServices.Protocols.Res.GetString("InvalidLdapSearchRequestFilter"));
                 }
                 string str3 = (string) filter;
                 num2 = (request2.Attributes == null) ? 0 : request2.Attributes.Count;
                 if (num2 != 0)
                 {
                     attributes = Marshal.AllocHGlobal((int) (Marshal.SizeOf(typeof(IntPtr)) * (num2 + 1)));
                     int num9 = 0;
                     num9 = 0;
                     while (num9 < num2)
                     {
                         ptr = Marshal.StringToHGlobalUni(request2.Attributes[num9]);
                         ptr6 = (IntPtr) (((long) attributes) + (Marshal.SizeOf(typeof(IntPtr)) * num9));
                         Marshal.WriteIntPtr(ptr6, ptr);
                         num9++;
                     }
                     ptr6 = (IntPtr) (((long) attributes) + (Marshal.SizeOf(typeof(IntPtr)) * num9));
                     Marshal.WriteIntPtr(ptr6, IntPtr.Zero);
                 }
                 int scope = (int) request2.Scope;
                 int timelimit = (int) (request2.TimeLimit.Ticks / 0x989680L);
                 System.DirectoryServices.Protocols.DereferenceAlias derefAlias = this.options.DerefAlias;
                 this.options.DerefAlias = request2.Aliases;
                 try
                 {
                     num3 = Wldap32.ldap_search(this.ldapHandle, request2.DistinguishedName, scope, str3, attributes, request2.TypesOnly, zero, clientcontrol, timelimit, request2.SizeLimit, ref messageID);
                     goto Label_06A6;
                 }
                 finally
                 {
                     this.options.DerefAlias = derefAlias;
                 }
             }
             throw new NotSupportedException(System.DirectoryServices.Protocols.Res.GetString("InvliadRequestType"));
         }
     Label_06A6:
         if (num3 == 0x55)
         {
             num3 = 0x70;
         }
         num19 = num3;
     }
     finally
     {
         GC.KeepAlive(modArray);
         if (zero != IntPtr.Zero)
         {
             for (int m = 0; m < controlArray.Length; m++)
             {
                 IntPtr hglobal = Marshal.ReadIntPtr(zero, Marshal.SizeOf(typeof(IntPtr)) * m);
                 if (hglobal != IntPtr.Zero)
                 {
                     Marshal.FreeHGlobal(hglobal);
                 }
             }
             Marshal.FreeHGlobal(zero);
         }
         if (controlArray != null)
         {
             for (int n = 0; n < controlArray.Length; n++)
             {
                 if (controlArray[n].ldctl_oid != IntPtr.Zero)
                 {
                     Marshal.FreeHGlobal(controlArray[n].ldctl_oid);
                 }
                 if ((controlArray[n].ldctl_value != null) && (controlArray[n].ldctl_value.bv_val != IntPtr.Zero))
                 {
                     Marshal.FreeHGlobal(controlArray[n].ldctl_value.bv_val);
                 }
             }
         }
         if (clientcontrol != IntPtr.Zero)
         {
             for (int num14 = 0; num14 < controlArray2.Length; num14++)
             {
                 IntPtr ptr8 = Marshal.ReadIntPtr(clientcontrol, Marshal.SizeOf(typeof(IntPtr)) * num14);
                 if (ptr8 != IntPtr.Zero)
                 {
                     Marshal.FreeHGlobal(ptr8);
                 }
             }
             Marshal.FreeHGlobal(clientcontrol);
         }
         if (controlArray2 != null)
         {
             for (int num15 = 0; num15 < controlArray2.Length; num15++)
             {
                 if (controlArray2[num15].ldctl_oid != IntPtr.Zero)
                 {
                     Marshal.FreeHGlobal(controlArray2[num15].ldctl_oid);
                 }
                 if ((controlArray2[num15].ldctl_value != null) && (controlArray2[num15].ldctl_value.bv_val != IntPtr.Zero))
                 {
                     Marshal.FreeHGlobal(controlArray2[num15].ldctl_value.bv_val);
                 }
             }
         }
         if (attrs != IntPtr.Zero)
         {
             for (int num16 = 0; num16 < (num - 1); num16++)
             {
                 IntPtr ptr9 = Marshal.ReadIntPtr(attrs, Marshal.SizeOf(typeof(IntPtr)) * num16);
                 if (ptr9 != IntPtr.Zero)
                 {
                     Marshal.FreeHGlobal(ptr9);
                 }
             }
             Marshal.FreeHGlobal(attrs);
         }
         for (int k = 0; k < ptrToFree.Count; k++)
         {
             IntPtr ptr10 = (IntPtr) ptrToFree[k];
             Marshal.FreeHGlobal(ptr10);
         }
         if ((binaryValue != null) && (binaryValue.bv_val != IntPtr.Zero))
         {
             Marshal.FreeHGlobal(binaryValue.bv_val);
         }
         if (attributes != IntPtr.Zero)
         {
             for (int num18 = 0; num18 < num2; num18++)
             {
                 IntPtr ptr11 = Marshal.ReadIntPtr(attributes, Marshal.SizeOf(typeof(IntPtr)) * num18);
                 if (ptr11 != IntPtr.Zero)
                 {
                     Marshal.FreeHGlobal(ptr11);
                 }
             }
             Marshal.FreeHGlobal(attributes);
         }
     }
     return num19;
 }