/// <summary>
        /// Url解码
        /// </summary>
        /// <param name="bytes">字节组</param>
        /// <param name="offset">偏移量</param>
        /// <param name="count">数量</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string UrlDecode(byte[] bytes, int offset, int count, Encoding encoding)
        {
            UrlDecoder helper = new UrlDecoder(count, encoding);

            // go through the bytes collapsing %XX and %uXXXX and appending
            // each byte as byte, with exception of %uXXXX constructs that
            // are appended as chars

            for (int i = 0; i < count; i++)
            {
                int  pos = offset + i;
                byte b   = bytes[pos];

                // The code assumes that + and % cannot be in multibyte sequence

                if (b == '+')
                {
                    b = (byte)' ';
                }
                else if (b == '%' && i < count - 2)
                {
                    if (bytes[pos + 1] == 'u' && i < count - 5)
                    {
                        int h1 = HttpUtility.HexToInt((char)bytes[pos + 2]);
                        int h2 = HttpUtility.HexToInt((char)bytes[pos + 3]);
                        int h3 = HttpUtility.HexToInt((char)bytes[pos + 4]);
                        int h4 = HttpUtility.HexToInt((char)bytes[pos + 5]);

                        if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0)
                        {   // valid 4 hex chars
                            char ch = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4);
                            i += 5;

                            // don't add as byte
                            helper.AddChar(ch);
                            continue;
                        }
                    }
                    else
                    {
                        int h1 = HttpUtility.HexToInt((char)bytes[pos + 1]);
                        int h2 = HttpUtility.HexToInt((char)bytes[pos + 2]);

                        if (h1 >= 0 && h2 >= 0)
                        {     // valid 2 hex chars
                            b  = (byte)((h1 << 4) | h2);
                            i += 2;
                        }
                    }
                }

                helper.AddByte(b);
            }
            return(helper.ToString());
        }
        /// <summary>
        /// Url解码
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string UrlDecode(string str, Encoding encoding)
        {
            if (str == null)
            {
                return(null);
            }

            int        count  = str.Length;
            UrlDecoder helper = new UrlDecoder(count, encoding);

            // go through the string's chars collapsing %XX and %uXXXX and
            // appending each char as char, with exception of %XX constructs
            // that are appended as bytes

            for (int pos = 0; pos < count; pos++)
            {
                char ch = str[pos];

                if (ch == '+')
                {
                    ch = ' ';
                }
                else if (ch == '%' && pos < count - 2)
                {
                    if (str[pos + 1] == 'u' && pos < count - 5)
                    {
                        int h1 = HttpUtility.HexToInt(str[pos + 2]);
                        int h2 = HttpUtility.HexToInt(str[pos + 3]);
                        int h3 = HttpUtility.HexToInt(str[pos + 4]);
                        int h4 = HttpUtility.HexToInt(str[pos + 5]);

                        if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0)
                        {   // valid 4 hex chars
                            ch   = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4);
                            pos += 5;

                            // only add as char
                            helper.AddChar(ch);
                            continue;
                        }
                    }
                    else
                    {
                        int h1 = HttpUtility.HexToInt(str[pos + 1]);
                        int h2 = HttpUtility.HexToInt(str[pos + 2]);

                        if (h1 >= 0 && h2 >= 0)
                        {     // valid 2 hex chars
                            byte b = (byte)((h1 << 4) | h2);
                            pos += 2;

                            // don't add as char
                            helper.AddByte(b);
                            continue;
                        }
                    }
                }

                if ((ch & 0xFF80) == 0)
                {
                    helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode
                }
                else
                {
                    helper.AddChar(ch);
                }
            }

            return(helper.ToString());
        }
Exemple #3
0
        /// <summary>
        /// Url解码
        /// </summary>
        /// <param name="bytes">字节组</param>
        /// <param name="offset">偏移量</param>
        /// <param name="count">数量</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string UrlDecode(byte[] bytes, int offset, int count, Encoding encoding)
        {
            UrlDecoder helper = new UrlDecoder(count, encoding);

            // go through the bytes collapsing %XX and %uXXXX and appending
            // each byte as byte, with exception of %uXXXX constructs that
            // are appended as chars

            for (int i = 0; i < count; i++)
            {
                int pos = offset + i;
                byte b = bytes[pos];

                // The code assumes that + and % cannot be in multibyte sequence

                if (b == '+')
                {
                    b = (byte)' ';
                }
                else if (b == '%' && i < count - 2)
                {
                    if (bytes[pos + 1] == 'u' && i < count - 5)
                    {
                        int h1 = HttpUtility.HexToInt((char)bytes[pos + 2]);
                        int h2 = HttpUtility.HexToInt((char)bytes[pos + 3]);
                        int h3 = HttpUtility.HexToInt((char)bytes[pos + 4]);
                        int h4 = HttpUtility.HexToInt((char)bytes[pos + 5]);

                        if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0)
                        {   // valid 4 hex chars
                            char ch = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4);
                            i += 5;

                            // don't add as byte
                            helper.AddChar(ch);
                            continue;
                        }
                    }
                    else
                    {
                        int h1 = HttpUtility.HexToInt((char)bytes[pos + 1]);
                        int h2 = HttpUtility.HexToInt((char)bytes[pos + 2]);

                        if (h1 >= 0 && h2 >= 0)
                        {     // valid 2 hex chars
                            b = (byte)((h1 << 4) | h2);
                            i += 2;
                        }
                    }
                }

                helper.AddByte(b);
            }
            return helper.ToString();
        }
Exemple #4
0
        /// <summary>
        /// Url解码
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string UrlDecode(string str, Encoding encoding)
        {
            if (str == null)
            {
                return null;
            }

            int count = str.Length;
            UrlDecoder helper = new UrlDecoder(count, encoding);

            // go through the string's chars collapsing %XX and %uXXXX and
            // appending each char as char, with exception of %XX constructs
            // that are appended as bytes

            for (int pos = 0; pos < count; pos++)
            {
                char ch = str[pos];

                if (ch == '+')
                {
                    ch = ' ';
                }
                else if (ch == '%' && pos < count - 2)
                {
                    if (str[pos + 1] == 'u' && pos < count - 5)
                    {
                        int h1 = HttpUtility.HexToInt(str[pos + 2]);
                        int h2 = HttpUtility.HexToInt(str[pos + 3]);
                        int h3 = HttpUtility.HexToInt(str[pos + 4]);
                        int h4 = HttpUtility.HexToInt(str[pos + 5]);

                        if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0)
                        {   // valid 4 hex chars
                            ch = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4);
                            pos += 5;

                            // only add as char
                            helper.AddChar(ch);
                            continue;
                        }
                    }
                    else
                    {
                        int h1 = HttpUtility.HexToInt(str[pos + 1]);
                        int h2 = HttpUtility.HexToInt(str[pos + 2]);

                        if (h1 >= 0 && h2 >= 0)
                        {     // valid 2 hex chars
                            byte b = (byte)((h1 << 4) | h2);
                            pos += 2;

                            // don't add as char
                            helper.AddByte(b);
                            continue;
                        }
                    }
                }

                if ((ch & 0xFF80) == 0)
                    helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode
                else
                    helper.AddChar(ch);
            }

            return helper.ToString();
        }
Exemple #5
0
        /// <summary>
        /// Url解码
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        private static string UrlDecode(string str, Encoding encoding)
        {
            if (str == null)
            {
                return(null);
            }

            var count  = str.Length;
            var helper = new UrlDecoder(count, encoding);

            for (var pos = 0; pos < count; pos++)
            {
                var ch = str[pos];
                if (ch == '+')
                {
                    ch = ' ';
                }
                else if (ch == '%' && pos < count - 2)
                {
                    if (str[pos + 1] == 'u' && pos < count - 5)
                    {
                        var h1 = HexToInt(str[pos + 2]);
                        var h2 = HexToInt(str[pos + 3]);
                        var h3 = HexToInt(str[pos + 4]);
                        var h4 = HexToInt(str[pos + 5]);

                        if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0)
                        {   // valid 4 hex chars
                            ch   = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4);
                            pos += 5;

                            // only add as char
                            helper.AddChar(ch);
                            continue;
                        }
                    }
                    else
                    {
                        var h1 = HexToInt(str[pos + 1]);
                        var h2 = HexToInt(str[pos + 2]);

                        if (h1 >= 0 && h2 >= 0)
                        {     // valid 2 hex chars
                            var b = (byte)((h1 << 4) | h2);
                            pos += 2;

                            // don't add as char
                            helper.AddByte(b);
                            continue;
                        }
                    }
                }

                if ((ch & 0xFF80) == 0)
                {
                    helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode
                }
                else
                {
                    helper.AddChar(ch);
                }
            }

            return(helper.ToString());
        }