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);
        }
 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;
 }
Ejemplo n.º 3
0
		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;
		}
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        private static int EncodingMultiByteArrayHelper(BerSafeHandle berElement, byte[][] tempValue, char fmt)
        {
            IntPtr intPtr;
            IntPtr intPtr1 = (IntPtr)0;

            SafeBerval[] safeBerval = null;
            int          num        = 0;

            try
            {
                if (tempValue != null)
                {
                    int i = 0;
                    intPtr1 = Utility.AllocHGlobalIntPtrArray((int)tempValue.Length + 1);
                    int num1 = Marshal.SizeOf(typeof(SafeBerval));
                    safeBerval = new SafeBerval[(int)tempValue.Length];
                    for (i = 0; i < (int)tempValue.Length; i++)
                    {
                        byte[] numArray = tempValue[i];
                        safeBerval[i] = new SafeBerval();
                        if (numArray != null)
                        {
                            safeBerval[i].bv_len = (int)numArray.Length;
                            safeBerval[i].bv_val = Marshal.AllocHGlobal((int)numArray.Length);
                            Marshal.Copy(numArray, 0, safeBerval[i].bv_val, (int)numArray.Length);
                        }
                        else
                        {
                            safeBerval[i].bv_len = 0;
                            safeBerval[i].bv_val = (IntPtr)0;
                        }
                        IntPtr intPtr2 = Marshal.AllocHGlobal(num1);
                        Marshal.StructureToPtr(safeBerval[i], intPtr2, false);
                        intPtr = (IntPtr)((long)intPtr1 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
                        Marshal.WriteIntPtr(intPtr, intPtr2);
                    }
                    intPtr = (IntPtr)((long)intPtr1 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
                    Marshal.WriteIntPtr(intPtr, (IntPtr)0);
                }
                num = Wldap32.ber_printf_berarray(berElement, new string(fmt, 1), intPtr1);
                GC.KeepAlive(safeBerval);
            }
            finally
            {
                if (intPtr1 != (IntPtr)0)
                {
                    for (int j = 0; j < (int)tempValue.Length; j++)
                    {
                        IntPtr intPtr3 = Marshal.ReadIntPtr(intPtr1, Marshal.SizeOf(typeof(IntPtr)) * j);
                        if (intPtr3 != (IntPtr)0)
                        {
                            Marshal.FreeHGlobal(intPtr3);
                        }
                    }
                    Marshal.FreeHGlobal(intPtr1);
                }
            }
            return(num);
        }
Ejemplo n.º 6
0
        private static byte[][] DecodingMultiByteArrayHelper(BerSafeHandle 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 = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref ptrResult);

                if (error == 0)
                {
                    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)
                {
                    Wldap32.ber_bvecfree(ptrResult);
                }
            }

            return(result);
        }
 private static int EncodingByteArrayHelper(BerSafeHandle berElement, byte[] tempValue, char fmt)
 {
     if (tempValue != null)
     {
         IntPtr destination = Marshal.AllocHGlobal(tempValue.Length);
         Marshal.Copy(tempValue, 0, destination, tempValue.Length);
         HGlobalMemHandle handle = new HGlobalMemHandle(destination);
         return(Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), handle, tempValue.Length));
     }
     return(Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), new HGlobalMemHandle(IntPtr.Zero), 0));
 }
Ejemplo n.º 8
0
        private static int EncodingByteArrayHelper(BerSafeHandle berElement, byte[] tempValue, char fmt)
        {
            int num;

            if (tempValue == null)
            {
                num = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), new HGlobalMemHandle((IntPtr)0), 0);
            }
            else
            {
                IntPtr intPtr = Marshal.AllocHGlobal((int)tempValue.Length);
                Marshal.Copy(tempValue, 0, intPtr, (int)tempValue.Length);
                HGlobalMemHandle hGlobalMemHandle = new HGlobalMemHandle(intPtr);
                num = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), hGlobalMemHandle, (int)tempValue.Length);
            }
            return(num);
        }
Ejemplo n.º 9
0
        private static int EncodingByteArrayHelper(BerSafeHandle berElement, byte[] tempValue, char fmt)
        {
            int error = 0;

            // one byte array, one int arguments
            if (tempValue != null)
            {
                IntPtr tmp = Marshal.AllocHGlobal(tempValue.Length);
                Marshal.Copy(tempValue, 0, tmp, tempValue.Length);
                HGlobalMemHandle memHandle = new HGlobalMemHandle(tmp);

                error = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), memHandle, tempValue.Length);
            }
            else
            {
                error = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), new HGlobalMemHandle(IntPtr.Zero), 0);
            }

            return(error);
        }
        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);
        }
 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;
 }
Ejemplo n.º 12
0
        private static byte[] DecodingByteArrayHelper(BerSafeHandle 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 = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref result);

            try
            {
                if (error == 0)
                {
                    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)
                {
                    Wldap32.ber_bvfree(result);
                }
            }

            return(byteArray);
        }
Ejemplo n.º 13
0
        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);
        }
Ejemplo n.º 14
0
		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;
		}
Ejemplo n.º 15
0
        private static int EncodingByteArrayHelper(BerSafeHandle berElement, byte[] tempValue, char fmt)
        {
            int error = 0;

            // one byte array, one int arguments
            //if (tempValue != null)
            //{

            tempValue = tempValue ?? new byte[0]; //openLdap ber_printf doesn't cope well with null values

            IntPtr tmp = Marshal.AllocHGlobal(tempValue.Length);

            Marshal.Copy(tempValue, 0, tmp, tempValue.Length);
            HGlobalMemHandle memHandle = new HGlobalMemHandle(tmp);

            error = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), memHandle, tempValue.Length);
            //}
            //else
            //{
            //    error = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), new HGlobalMemHandle(IntPtr.Zero), 0);
            //}

            return(error);
        }
 private static int EncodingByteArrayHelper(BerSafeHandle berElement, byte[] tempValue, char fmt)
 {
     if (tempValue != null)
     {
         IntPtr destination = Marshal.AllocHGlobal(tempValue.Length);
         Marshal.Copy(tempValue, 0, destination, tempValue.Length);
         HGlobalMemHandle handle = new HGlobalMemHandle(destination);
         return Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), handle, tempValue.Length);
     }
     return Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), new HGlobalMemHandle(IntPtr.Zero), 0);
 }
Ejemplo n.º 17
0
 public static extern int ber_scanf_bitstring(BerSafeHandle berElement, string format, ref IntPtr value, ref int length);
Ejemplo n.º 18
0
 public static extern int ber_scanf_int(BerSafeHandle berElement, string format, ref int value);
Ejemplo n.º 19
0
 public static extern int ber_flatten(BerSafeHandle berElement, ref IntPtr value);
Ejemplo n.º 20
0
 public static extern int ber_printf_bytearray(BerSafeHandle berElement, string format, HGlobalMemHandle value, int length);
        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);
        }
Ejemplo n.º 22
0
 public static extern int ber_printf_int(BerSafeHandle berElement, string format, int value);
Ejemplo n.º 23
0
        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;
        }
Ejemplo n.º 24
0
        private static int EncodingMultiByteArrayHelper(BerSafeHandle berElement, byte[][] tempValue, char fmt)
        {
            IntPtr berValArray = (IntPtr)0;
            IntPtr tempPtr = (IntPtr)0;
            SafeBerval[] managedBerVal = null;
            int error = 0;

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

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

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

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

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

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

                    tempPtr = (IntPtr)((long)berValArray + Marshal.SizeOf(typeof(IntPtr)) * i);
                    Marshal.WriteIntPtr(tempPtr, (IntPtr)0);
                }

                error = Wldap32.ber_printf_berarray(berElement, new string(fmt, 1), berValArray);

                GC.KeepAlive(managedBerVal);
            }
            finally
            {
                if (berValArray != (IntPtr)0)
                {
                    for (int i = 0; i < tempValue.Length; i++)
                    {
                        IntPtr ptr = Marshal.ReadIntPtr(berValArray, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (ptr != (IntPtr)0)
                            Marshal.FreeHGlobal(ptr);
                    }
                    Marshal.FreeHGlobal(berValArray);
                }
            }

            return error;
        }
Ejemplo n.º 25
0
        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;
        }
Ejemplo n.º 26
0
        private static int EncodingByteArrayHelper(BerSafeHandle berElement, byte[] tempValue, char fmt)
        {
            int error = 0;

            // one byte array, one int arguments
            if (tempValue != null)
            {
                IntPtr tmp = Marshal.AllocHGlobal(tempValue.Length);
                Marshal.Copy(tempValue, 0, tmp, tempValue.Length);
                HGlobalMemHandle memHandle = new HGlobalMemHandle(tmp);

                error = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), memHandle, tempValue.Length);
            }
            else
            {
                error = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), new HGlobalMemHandle((IntPtr)0), 0);
            }

            return error;
        }
Ejemplo n.º 27
0
        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;
        }
        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;
        }
        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;
        }
Ejemplo n.º 30
0
		private static int EncodingByteArrayHelper(BerSafeHandle berElement, byte[] tempValue, char fmt)
		{
			int num;
			if (tempValue == null)
			{
				num = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), new HGlobalMemHandle((IntPtr)0), 0);
			}
			else
			{
				IntPtr intPtr = Marshal.AllocHGlobal((int)tempValue.Length);
				Marshal.Copy(tempValue, 0, intPtr, (int)tempValue.Length);
				HGlobalMemHandle hGlobalMemHandle = new HGlobalMemHandle(intPtr);
				num = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), hGlobalMemHandle, (int)tempValue.Length);
			}
			return num;
		}
Ejemplo n.º 31
0
        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;
        }
Ejemplo n.º 32
0
		private static int EncodingMultiByteArrayHelper(BerSafeHandle berElement, byte[][] tempValue, char fmt)
		{
			IntPtr intPtr;
			IntPtr intPtr1 = (IntPtr)0;
			SafeBerval[] safeBerval = null;
			int num = 0;
			try
			{
				if (tempValue != null)
				{
					int i = 0;
					intPtr1 = Utility.AllocHGlobalIntPtrArray((int)tempValue.Length + 1);
					int num1 = Marshal.SizeOf(typeof(SafeBerval));
					safeBerval = new SafeBerval[(int)tempValue.Length];
					for (i = 0; i < (int)tempValue.Length; i++)
					{
						byte[] numArray = tempValue[i];
						safeBerval[i] = new SafeBerval();
						if (numArray != null)
						{
							safeBerval[i].bv_len = (int)numArray.Length;
							safeBerval[i].bv_val = Marshal.AllocHGlobal((int)numArray.Length);
							Marshal.Copy(numArray, 0, safeBerval[i].bv_val, (int)numArray.Length);
						}
						else
						{
							safeBerval[i].bv_len = 0;
							safeBerval[i].bv_val = (IntPtr)0;
						}
						IntPtr intPtr2 = Marshal.AllocHGlobal(num1);
						Marshal.StructureToPtr(safeBerval[i], intPtr2, false);
						intPtr = (IntPtr)((long)intPtr1 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
						Marshal.WriteIntPtr(intPtr, intPtr2);
					}
					intPtr = (IntPtr)((long)intPtr1 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
					Marshal.WriteIntPtr(intPtr, (IntPtr)0);
				}
				num = Wldap32.ber_printf_berarray(berElement, new string(fmt, 1), intPtr1);
				GC.KeepAlive(safeBerval);
			}
			finally
			{
				if (intPtr1 != (IntPtr)0)
				{
					for (int j = 0; j < (int)tempValue.Length; j++)
					{
						IntPtr intPtr3 = Marshal.ReadIntPtr(intPtr1, Marshal.SizeOf(typeof(IntPtr)) * j);
						if (intPtr3 != (IntPtr)0)
						{
							Marshal.FreeHGlobal(intPtr3);
						}
					}
					Marshal.FreeHGlobal(intPtr1);
				}
			}
			return num;
		}
Ejemplo n.º 33
0
 public static extern int ber_printf_int(BerSafeHandle berElement, string format, int value);
Ejemplo n.º 34
0
		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");
			}
		}
Ejemplo n.º 35
0
 public static extern int ber_printf_berarray(BerSafeHandle berElement, string format, IntPtr value);
Ejemplo n.º 36
0
		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");
			}
		}
Ejemplo n.º 37
0
 public static extern int ber_scanf(BerSafeHandle berElement, string format);
Ejemplo n.º 38
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
            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(string.Format(CultureInfo.CurrentCulture, SR.BerConverterNotMatch));
                    }

                    if (!(value[valueCount] is int))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be int\n");
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.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(string.Format(CultureInfo.CurrentCulture, SR.BerConverterNotMatch));
                    }

                    if (!(value[valueCount] is bool))
                    {
                        // argument is wrong
                        Debug.WriteLine("type should be boolean\n");
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.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(string.Format(CultureInfo.CurrentCulture, 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(string.Format(CultureInfo.CurrentCulture, 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(string.Format(CultureInfo.CurrentCulture, 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(string.Format(CultureInfo.CurrentCulture, 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(string.Format(CultureInfo.CurrentCulture, 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(string.Format(CultureInfo.CurrentCulture, 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(string.Format(CultureInfo.CurrentCulture, 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(string.Format(CultureInfo.CurrentCulture, 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(string.Format(CultureInfo.CurrentCulture, 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 = Wldap32.ber_flatten(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)
                {
                    Wldap32.ber_bvfree(flattenptr);
                }
            }

            return(encodingResult);
        }
Ejemplo n.º 39
0
 public static extern int ber_scanf_ptr(BerSafeHandle berElement, string format, ref IntPtr value);
Ejemplo n.º 40
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);
        }
Ejemplo n.º 41
0
 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");
     }
 }
Ejemplo n.º 42
0
        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");
            }
        }
 private static int EncodingMultiByteArrayHelper(BerSafeHandle berElement, byte[][] tempValue, char fmt)
 {
     IntPtr zero = IntPtr.Zero;
     IntPtr ptr = IntPtr.Zero;
     SafeBerval[] bervalArray = null;
     int num = 0;
     try
     {
         if (tempValue != null)
         {
             int index = 0;
             zero = Marshal.AllocHGlobal((int) (Marshal.SizeOf(typeof(IntPtr)) * (tempValue.Length + 1)));
             int cb = Marshal.SizeOf(typeof(SafeBerval));
             bervalArray = new SafeBerval[tempValue.Length];
             index = 0;
             while (index < tempValue.Length)
             {
                 byte[] source = tempValue[index];
                 bervalArray[index] = new SafeBerval();
                 if (source == null)
                 {
                     bervalArray[index].bv_len = 0;
                     bervalArray[index].bv_val = IntPtr.Zero;
                 }
                 else
                 {
                     bervalArray[index].bv_len = source.Length;
                     bervalArray[index].bv_val = Marshal.AllocHGlobal(source.Length);
                     Marshal.Copy(source, 0, bervalArray[index].bv_val, source.Length);
                 }
                 IntPtr ptr3 = Marshal.AllocHGlobal(cb);
                 Marshal.StructureToPtr(bervalArray[index], ptr3, false);
                 ptr = (IntPtr) (((long) zero) + (Marshal.SizeOf(typeof(IntPtr)) * index));
                 Marshal.WriteIntPtr(ptr, ptr3);
                 index++;
             }
             ptr = (IntPtr) (((long) zero) + (Marshal.SizeOf(typeof(IntPtr)) * index));
             Marshal.WriteIntPtr(ptr, IntPtr.Zero);
         }
         num = Wldap32.ber_printf_berarray(berElement, new string(fmt, 1), zero);
         GC.KeepAlive(bervalArray);
     }
     finally
     {
         if (zero != IntPtr.Zero)
         {
             for (int i = 0; i < tempValue.Length; i++)
             {
                 IntPtr hglobal = Marshal.ReadIntPtr(zero, Marshal.SizeOf(typeof(IntPtr)) * i);
                 if (hglobal != IntPtr.Zero)
                 {
                     Marshal.FreeHGlobal(hglobal);
                 }
             }
             Marshal.FreeHGlobal(zero);
         }
     }
     return num;
 }
Ejemplo n.º 44
0
 public static extern int ber_printf_berarray(BerSafeHandle berElement, string format, IntPtr value);
        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);
        }
Ejemplo n.º 46
0
 public static extern int ber_scanf(BerSafeHandle berElement, string format);
Ejemplo n.º 47
0
 public static extern int ber_printf_emptyarg(BerSafeHandle berElement, string format);
Ejemplo n.º 48
0
 public static extern int ber_scanf_ptr(BerSafeHandle berElement, string format, ref IntPtr value);
Ejemplo n.º 49
0
 public static extern int ber_printf_bytearray(BerSafeHandle berElement, string format, HGlobalMemHandle value, int length);
Ejemplo n.º 50
0
 public static extern int ber_skip_tag(BerSafeHandle berElement, ref int lenPtr);
Ejemplo n.º 51
0
 public static extern int ber_flatten(BerSafeHandle berElement, ref IntPtr value);
Ejemplo n.º 52
0
        private static int EncodingMultiByteArrayHelper(BerSafeHandle berElement, byte[][] tempValue, char fmt)
        {
            IntPtr berValArray = IntPtr.Zero;
            IntPtr tempPtr     = IntPtr.Zero;

            SafeBerval[] managedBerVal = null;
            int          error         = 0;

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

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

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

                        if (byteArray == null)
                        {
                            managedBerVal[i].bv_len = 0;
                            managedBerVal[i].bv_val = IntPtr.Zero;
                        }
                        else
                        {
                            managedBerVal[i].bv_len = byteArray.Length;
                            managedBerVal[i].bv_val = Marshal.AllocHGlobal(byteArray.Length);
                            Marshal.Copy(byteArray, 0, managedBerVal[i].bv_val, byteArray.Length);
                        }

                        // allocate memory for the unmanaged structure
                        IntPtr valPtr = Marshal.AllocHGlobal(structSize);
                        Marshal.StructureToPtr(managedBerVal[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 = Wldap32.ber_printf_berarray(berElement, new string(fmt, 1), berValArray);

                GC.KeepAlive(managedBerVal);
            }
            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);
                }
            }

            return(error);
        }
Ejemplo n.º 53
0
 public static extern int ber_scanf_int(BerSafeHandle berElement, string format, ref int value);
Ejemplo n.º 54
0
 public static extern int ber_printf_emptyarg(BerSafeHandle berElement, string format);
Ejemplo n.º 55
0
 public static extern int ber_scanf_bitstring(BerSafeHandle berElement, string format, ref IntPtr value, ref int length);
        private static int EncodingMultiByteArrayHelper(BerSafeHandle berElement, byte[][] tempValue, char fmt)
        {
            IntPtr zero = IntPtr.Zero;
            IntPtr ptr  = IntPtr.Zero;

            SafeBerval[] bervalArray = null;
            int          num         = 0;

            try
            {
                if (tempValue != null)
                {
                    int index = 0;
                    zero = Marshal.AllocHGlobal((int)(Marshal.SizeOf(typeof(IntPtr)) * (tempValue.Length + 1)));
                    int cb = Marshal.SizeOf(typeof(SafeBerval));
                    bervalArray = new SafeBerval[tempValue.Length];
                    index       = 0;
                    while (index < tempValue.Length)
                    {
                        byte[] source = tempValue[index];
                        bervalArray[index] = new SafeBerval();
                        if (source == null)
                        {
                            bervalArray[index].bv_len = 0;
                            bervalArray[index].bv_val = IntPtr.Zero;
                        }
                        else
                        {
                            bervalArray[index].bv_len = source.Length;
                            bervalArray[index].bv_val = Marshal.AllocHGlobal(source.Length);
                            Marshal.Copy(source, 0, bervalArray[index].bv_val, source.Length);
                        }
                        IntPtr ptr3 = Marshal.AllocHGlobal(cb);
                        Marshal.StructureToPtr(bervalArray[index], ptr3, false);
                        ptr = (IntPtr)(((long)zero) + (Marshal.SizeOf(typeof(IntPtr)) * index));
                        Marshal.WriteIntPtr(ptr, ptr3);
                        index++;
                    }
                    ptr = (IntPtr)(((long)zero) + (Marshal.SizeOf(typeof(IntPtr)) * index));
                    Marshal.WriteIntPtr(ptr, IntPtr.Zero);
                }
                num = Wldap32.ber_printf_berarray(berElement, new string(fmt, 1), zero);
                GC.KeepAlive(bervalArray);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    for (int i = 0; i < tempValue.Length; i++)
                    {
                        IntPtr hglobal = Marshal.ReadIntPtr(zero, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (hglobal != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(hglobal);
                        }
                    }
                    Marshal.FreeHGlobal(zero);
                }
            }
            return(num);
        }