Novell.Directory.Ldap.Rfc2251.RfcFilter.unescapeString C# (CSharp) Method

unescapeString() private method

Replace escaped hex digits with the equivalent binary representation. Assume either V2 or V3 escape mechanisms: V2: \*, \(, \), \\. V3: \2A, \28, \29, \5C, \00.
private unescapeString ( System string_Renamed ) : sbyte[]
string_Renamed System
return sbyte[]
        private sbyte[] unescapeString(System.String string_Renamed)
        {
            // give octets enough space to grow
            sbyte[] octets = new sbyte[string_Renamed.Length * 3];
            // index for string and octets
            int iString, iOctets;
            // escape==true means we are in an escape sequence.
            bool escape = false;
            // escStart==true means we are reading the first character of an escape.
            bool escStart = false;

            int ival, length = string_Renamed.Length;
            sbyte[] utf8Bytes;
            char ch; // Character we are adding to the octet string
            char[] ca = new char[1]; // used while converting multibyte UTF-8 char
            char temp = (char) (0); // holds the value of the escaped sequence

            // loop through each character of the string and copy them into octets
            // converting escaped sequences when needed
            for (iString = 0, iOctets = 0; iString < length; iString++)
            {
                ch = string_Renamed[iString];
                if (escape)
                {
                    if ((ival = hex2int(ch)) < 0)
                    {
                        // Invalid escape value(not a hex character)
                        throw new LdapLocalException(ExceptionMessages.INVALID_ESCAPE, new System.Object[]{ch}, LdapException.FILTER_ERROR);
                    }
                    else
                    {
                        // V3 escaped: \\**
                        if (escStart)
                        {
                            temp = (char) (ival << 4); // high bits of escaped char
                            escStart = false;
                        }
                        else
                        {
                            temp |= (char) (ival); // all bits of escaped char
                            octets[iOctets++] = (sbyte) temp;
                            escStart = escape = false;
                        }
                    }
                }
                else if (ch == '\\')
                {
                    escStart = escape = true;
                }
                else
                {
                    try
                    {
                        // place the character into octets.
                        if ((ch >= 0x01 && ch <= 0x27) || (ch >= 0x2B && ch <= 0x5B) || (ch >= 0x5D))
                        {
                            // found valid char
                            if (ch <= 0x7f)
                            {
                                // char = %x01-27 / %x2b-5b / %x5d-7f
                                octets[iOctets++] = (sbyte) ch;
                            }
                            else
                            {
                                // char > 0x7f, could be encoded in 2 or 3 bytes
                                ca[0] = ch;
                                System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("utf-8");
                                byte[] ibytes = encoder.GetBytes(new System.String(ca));
                                utf8Bytes=SupportClass.ToSByteArray(ibytes);

            //								utf8Bytes = new System.String(ca).getBytes("UTF-8");
                                // copy utf8 encoded character into octets
                                Array.Copy((System.Array) (utf8Bytes), 0, (System.Array) octets, iOctets, utf8Bytes.Length);
                                iOctets = iOctets + utf8Bytes.Length;
                            }
                            escape = false;
                        }
                        else
                        {
                            // found invalid character
                            System.String escString = "";
                            ca[0] = ch;
                            System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("utf-8");
                            byte[] ibytes = encoder.GetBytes(new System.String(ca));
                            utf8Bytes=SupportClass.ToSByteArray(ibytes);

            //							utf8Bytes = new System.String(ca).getBytes("UTF-8");
                            for (int i = 0; i < utf8Bytes.Length; i++)
                            {
                                sbyte u = utf8Bytes[i];
                                if ((u >= 0) && (u < 0x10))
                                {
                                    escString = escString + "\\0" + System.Convert.ToString(u & 0xff, 16);
                                }
                                else
                                {
                                    escString = escString + "\\" + System.Convert.ToString(u & 0xff, 16);
                                }
                            }
                            throw new LdapLocalException(ExceptionMessages.INVALID_CHAR_IN_FILTER, new System.Object[]{ch, escString}, LdapException.FILTER_ERROR);
                        }
                    }
                    catch (System.IO.IOException ue)
                    {
                        throw new System.SystemException("UTF-8 String encoding not supported by JVM");
                    }
                }
            }

            // Verify that any escape sequence completed
            if (escStart || escape)
            {
                throw new LdapLocalException(ExceptionMessages.SHORT_ESCAPE, LdapException.FILTER_ERROR);
            }

            sbyte[] toReturn = new sbyte[iOctets];
            //			Array.Copy((System.Array)SupportClass.ToByteArray(octets), 0, (System.Array)SupportClass.ToByteArray(toReturn), 0, iOctets);
            Array.Copy((System.Array)octets, 0, (System.Array)toReturn, 0, iOctets);

            octets = null;
            return toReturn;
        }