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.º 2
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.º 3
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");
            }
        }