Beispiel #1
0
		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;
		}
Beispiel #2
0
        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;
        }
 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;
 }