Пример #1
0
        /// <summary>
        /// UrlDecode
        /// </summary>
        /// <param name="value"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string UrlDecode(string value, Encoding encoding)
        {
            if (value == null)
            {
                return(null);
            }
            int        length  = value.Length;
            UrlDecoder decoder = new UrlDecoder(length, encoding);

            for (int i = 0; i < length; i++)
            {
                char ch = value[i];
                if (ch == '+')
                {
                    ch = ' ';
                }
                else if ((ch == '%') && (i < (length - 2)))
                {
                    if ((value[i + 1] == 'u') && (i < (length - 5)))
                    {
                        int num3 = HexToInt(value[i + 2]);
                        int num4 = HexToInt(value[i + 3]);
                        int num5 = HexToInt(value[i + 4]);
                        int num6 = HexToInt(value[i + 5]);
                        if (((num3 < 0) || (num4 < 0)) || ((num5 < 0) || (num6 < 0)))
                        {
                            goto Label_010B;
                        }
                        ch = (char)((((num3 << 12) | (num4 << 8)) | (num5 << 4)) | num6);
                        i += 5;
                        decoder.AddChar(ch);
                        continue;
                    }
                    int num7 = HexToInt(value[i + 1]);
                    int num8 = HexToInt(value[i + 2]);
                    if ((num7 >= 0) && (num8 >= 0))
                    {
                        byte b = (byte)((num7 << 4) | num8);
                        i += 2;
                        decoder.AddByte(b);
                        continue;
                    }
                }
Label_010B:
                if ((ch & 0xff80) == 0)
                {
                    decoder.AddByte((byte)ch);
                }
                else
                {
                    decoder.AddChar(ch);
                }
            }
            return(decoder.GetString());
        }
        // Ripped this off from HttpUtility
        static internal string UrlDecode(string s)
        {
            int        length  = s.Length;
            UrlDecoder decoder = new UrlDecoder(length, System.Text.Encoding.UTF8);

            for (int currIndex = 0; currIndex != length; ++currIndex)
            {
                char currChar = s[currIndex];
                if (currChar == '+')
                {
                    currChar = ' ';
                }
                else if (currChar == '%' && currIndex < length - 2)
                {
                    if (s[currIndex + 1] == 'u' && currIndex < length - 5)
                    {
                        int digit1 = HexToInt(s[currIndex + 2]);
                        int digit2 = HexToInt(s[currIndex + 3]);
                        int digit3 = HexToInt(s[currIndex + 4]);
                        int digit4 = HexToInt(s[currIndex + 5]);
                        if (digit1 >= 0 && digit2 >= 0 && digit3 >= 0 && digit4 >= 0)
                        {
                            currChar   = (char)((ushort)digit1 << 12 | digit2 << 8 | digit3 << 4 | digit4);
                            currIndex += 5;
                            decoder.AddChar(currChar);
                            continue;
                        }
                    }
                    else
                    {
                        int digit1 = HexToInt(s[currIndex + 1]);
                        int digit2 = HexToInt(s[currIndex + 2]);
                        if (digit1 >= 0 && digit2 >= 0)
                        {
                            currIndex += 2;
                            decoder.AddByte((byte)(digit1 << 4 | digit2));
                            continue;
                        }
                    }
                }

                if ((currChar & 0xFF80) == 0)
                {
                    decoder.AddByte((byte)currChar);
                }
                else
                {
                    decoder.AddChar(currChar);
                }
            }

            return(decoder.GetString());
        }
Пример #3
0
        private static string UrlDecodeStringFromStringInternal(string s, Encoding e)
        {
            int        length  = s.Length;
            UrlDecoder decoder = new UrlDecoder(length, e);

            for (int i = 0; i < length; i++)
            {
                char ch = s[i];
                if (ch == '+')
                {
                    ch = ' ';
                }
                else if ((ch == '%') && (i < (length - 2)))
                {
                    if ((s[i + 1] == 'u') && (i < (length - 5)))
                    {
                        int num3 = HexToInt(s[i + 2]);
                        int num4 = HexToInt(s[i + 3]);
                        int num5 = HexToInt(s[i + 4]);
                        int num6 = HexToInt(s[i + 5]);
                        if (((num3 < 0) || (num4 < 0)) || ((num5 < 0) || (num6 < 0)))
                        {
                            goto decoderCH;
                        }
                        ch = (char)((((num3 << 12) | (num4 << 8)) | (num5 << 4)) | num6);
                        i += 5;
                        decoder.AddChar(ch);
                        continue;
                    }
                    int num7 = HexToInt(s[i + 1]);
                    int num8 = HexToInt(s[i + 2]);
                    if ((num7 >= 0) && (num8 >= 0))
                    {
                        byte b = (byte)((num7 << 4) | num8);
                        i += 2;
                        decoder.AddByte(b);
                        continue;
                    }
                }

decoderCH:
                if ((ch & 0xff80) == 0)
                {
                    decoder.AddByte((byte)ch);
                }
                else
                {
                    decoder.AddChar(ch);
                }
            }
            return(decoder.GetString());
        }
Пример #4
0
        private static string UrlDecode(string s, Encoding e)
        {
            int        len     = s.Length;
            UrlDecoder decoder = new UrlDecoder(len, e);

            for (int i = 0; i < len; i++)
            {
                char c = s[i];
                if (c == '+')
                {
                    c = ' ';
                }
                else if (c == '%' && i < (len - 2))
                {
                    if (s[i + 1] == 'u' && i < (len - 5))
                    {
                        int num3 = HexToInt(s[i + 2]);
                        int num4 = HexToInt(s[i + 3]);
                        int num5 = HexToInt(s[i + 4]);
                        int num6 = HexToInt(s[i + 5]);
                        if ((num3 < 0 || num4 < 0) || (num5 < 0 || num6 < 0))
                        {
                            goto loc_1;
                        }
                        c  = (char)((ushort)((((num3 << 12) | (num4 << 8)) | (num5 << 4)) | num6));
                        i += 5;
                        decoder.AddChar(c);
                        continue;
                    }
                    int num7 = HexToInt(s[i + 1]);
                    int num8 = HexToInt(s[i + 2]);
                    if (num7 >= 0 && num8 >= 0)
                    {
                        byte num9 = (byte)((num7 << 4) | num8);
                        i += 2;
                        decoder.AddByte(num9);
                        continue;
                    }
                }
loc_1:
                if ((c & 0xff80) == '\0')
                {
                    decoder.AddByte((byte)c);
                }
                else
                {
                    decoder.AddChar(c);
                }
            }
            return(decoder.GetString().Trim());
        }
Пример #5
0
        /// <summary>
        /// Decodes a URL string/value into clear text
        /// </summary>
        /// <param name="encodedValue">The encoded value.</param>
        /// <returns>Clear text version of the string</returns>
        /// <remarks>This implementation is identical to the one provided by the .NET Framework in the WebUtility class, but it is provided here without dependencies on any server components.</remarks>
        public static string UrlDecode(string encodedValue)
        {
            if (encodedValue == null)
            {
                return(null);
            }
            var count  = encodedValue.Length;
            var helper = new UrlDecoder(count, Encoding.UTF8);

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

            for (var position = 0; position < count; position++)
            {
                var ch = encodedValue[position];

                if (ch == '+')
                {
                    ch = ' ';
                }
                else if (ch == '%' && position < count - 2)
                {
                    var h1 = HexToInt(encodedValue[position + 1]);
                    var h2 = HexToInt(encodedValue[position + 2]);

                    if (h1 < 0 || h2 < 0)
                    {
                        continue;
                    }
                    // valid 2 hex chars
                    var b = (byte)((h1 << 4) | h2);
                    position += 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.GetString());
        }
Пример #6
0
        private static string UrlDecodeInternal(string value, Encoding encoding)
        {
            if (value == null)
            {
                return(null);
            }

            int        count  = value.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 = value[pos];

                if (ch == '+')
                {
                    ch = ' ';
                }
                else if (ch == '%' && pos < count - 2)
                {
                    int h1 = HexToInt(value[pos + 1]);
                    int h2 = HexToInt(value[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.GetString());
        }
Пример #7
0
        // *** Source: alm/tfs_core/Framework/Common/UriUtility/HttpUtility.cs
        // This specific code was copied from above ASP.NET codebase.
        // Changes done - Removed the logic to handle %Uxxxx as it is not standards compliant.

        private static string UrlDecodeInternal(string value, Encoding encoding)
        {
            if (value == null)
            {
                return null;
            }

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

            // go through the string's chars collapsing %XX 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 = value[pos];

                if (ch == '+')
                {
                    ch = ' ';
                }
                else if (ch == '%' && pos < count - 2)
                {
                    int h1 = HexToInt(value[pos + 1]);
                    int h2 = HexToInt(value[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.GetString();
        }
Пример #8
0
        private static string UrlDecodeStringFromBytesInternal(byte[] buf, int offset, int count, Encoding e)
        {
            UrlDecoder helper = new UrlDecoder(count, e);

            // 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   = buf[pos];

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

                if (b == '+')
                {
                    b = (byte)' ';
                }
                else if (b == '%' && i < count - 2)
                {
                    if (buf[pos + 1] == 'u' && i < count - 5)
                    {
                        int h1 = HexToInt((char)buf[pos + 2]);
                        int h2 = HexToInt((char)buf[pos + 3]);
                        int h3 = HexToInt((char)buf[pos + 4]);
                        int h4 = HexToInt((char)buf[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 = HexToInt((char)buf[pos + 1]);
                        int h2 = HexToInt((char)buf[pos + 2]);

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

                helper.AddByte(b);
            }

            return(helper.GetString());
        }
Пример #9
0
        /// <summary>
        /// Decodes a URL string/value into clear text
        /// </summary>
        /// <param name="encodedValue">The encoded value.</param>
        /// <returns>Clear text version of the string</returns>
        /// <remarks>This implementation is identical to the one provided by the .NET Framework in the WebUtility class, but it is provided here without dependencies on any server components.</remarks>
        public static string UrlDecode(string encodedValue)
        {
            if (encodedValue == null) return null;
            var count = encodedValue.Length;
            var helper = new UrlDecoder(count, Encoding.UTF8);

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

            for (var position = 0; position < count; position++)
            {
                var ch = encodedValue[position];

                if (ch == '+') ch = ' ';
                else if (ch == '%' && position < count - 2)
                {
                    var h1 = HexToInt(encodedValue[position + 1]);
                    var h2 = HexToInt(encodedValue[position + 2]);

                    if (h1 < 0 || h2 < 0) continue;
                    // valid 2 hex chars
                    var b = (byte) ((h1 << 4) | h2);
                    position += 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.GetString();
        }
Пример #10
0
        internal static string CollapsePercentUFromStringInternal(string s, Encoding e)
        {
            int        count  = s.Length;
            UrlDecoder helper = new UrlDecoder(count, e);

            // go thorugh the string's chars collapsing just %uXXXX and
            // appending each char as char
            int loc = s.IndexOf("%u", StringComparison.Ordinal);

            if (loc == -1)
            {
                return(s);
            }

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

                if (ch == '%' && pos < count - 5)
                {
                    if (s[pos + 1] == 'u')
                    {
                        int h1 = HttpEncoderUtility.HexToInt(s[pos + 2]);
                        int h2 = HttpEncoderUtility.HexToInt(s[pos + 3]);
                        int h3 = HttpEncoderUtility.HexToInt(s[pos + 4]);
                        int h4 = HttpEncoderUtility.HexToInt(s[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;

                            // add as char
                            helper.AddChar(ch);
                            continue;
                        }
                    }
                }
                if ((ch & 0xFF80) == 0)
                {
                    helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode
                }
                else
                {
                    helper.AddChar(ch);
                }
            }
            return(Utf16StringValidator.ValidateString(helper.GetString()));
        }
        internal string UrlDecode(byte[] bytes, int offset, int count, Encoding encoding)
        {
            if (!ValidateUrlEncodingParameters(bytes, offset, count))
            {
                return(null);
            }
            UrlDecoder decoder = new UrlDecoder(count, encoding);

            for (int i = 0; i < count; i++)
            {
                int  index = offset + i;
                byte b     = bytes[index];
                if (b == 0x2b)
                {
                    b = 0x20;
                }
                else if ((b == 0x25) && (i < (count - 2)))
                {
                    if ((bytes[index + 1] == 0x75) && (i < (count - 5)))
                    {
                        int num4 = HttpEncoderUtility.HexToInt((char)bytes[index + 2]);
                        int num5 = HttpEncoderUtility.HexToInt((char)bytes[index + 3]);
                        int num6 = HttpEncoderUtility.HexToInt((char)bytes[index + 4]);
                        int num7 = HttpEncoderUtility.HexToInt((char)bytes[index + 5]);
                        if (((num4 < 0) || (num5 < 0)) || ((num6 < 0) || (num7 < 0)))
                        {
                            goto Label_00E7;
                        }
                        char ch = (char)((((num4 << 12) | (num5 << 8)) | (num6 << 4)) | num7);
                        i += 5;
                        decoder.AddChar(ch);
                        continue;
                    }
                    int num8 = HttpEncoderUtility.HexToInt((char)bytes[index + 1]);
                    int num9 = HttpEncoderUtility.HexToInt((char)bytes[index + 2]);
                    if ((num8 >= 0) && (num9 >= 0))
                    {
                        b  = (byte)((num8 << 4) | num9);
                        i += 2;
                    }
                }
Label_00E7:
                decoder.AddByte(b);
            }
            return(decoder.GetString());
        }
Пример #12
0
        private static string UrlDecodeStringFromBytesInternal(byte[] buf, int offset, int count, Encoding e)
        {
            UrlDecoder decoder = new UrlDecoder(count, e);

            for (int i = 0; i < count; i++)
            {
                int  index = offset + i;
                byte b     = buf[index];
                if (b == 0x2b)
                {
                    b = 0x20;
                }
                else if ((b == 0x25) && (i < (count - 2)))
                {
                    if ((buf[index + 1] == 0x75) && (i < (count - 5)))
                    {
                        int num4 = HexToInt((char)buf[index + 2]);
                        int num5 = HexToInt((char)buf[index + 3]);
                        int num6 = HexToInt((char)buf[index + 4]);
                        int num7 = HexToInt((char)buf[index + 5]);
                        if (((num4 < 0) || (num5 < 0)) || ((num6 < 0) || (num7 < 0)))
                        {
                            goto Label_00DA;
                        }
                        char ch = (char)((((num4 << 12) | (num5 << 8)) | (num6 << 4)) | num7);
                        i += 5;
                        decoder.AddChar(ch);
                        continue;
                    }
                    int num8 = HexToInt((char)buf[index + 1]);
                    int num9 = HexToInt((char)buf[index + 2]);
                    if ((num8 >= 0) && (num9 >= 0))
                    {
                        b  = (byte)((num8 << 4) | num9);
                        i += 2;
                    }
                }
Label_00DA:
                decoder.AddByte(b);
            }
            return(decoder.GetString());
        }
        internal static string CollapsePercentUFromStringInternal(string s, Encoding e)
        {
            int        length  = s.Length;
            UrlDecoder decoder = new UrlDecoder(length, e);

            if (s.IndexOf("%u", StringComparison.Ordinal) == -1)
            {
                return(s);
            }
            for (int i = 0; i < length; i++)
            {
                char ch = s[i];
                if (((ch == '%') && (i < (length - 5))) && (s[i + 1] == 'u'))
                {
                    int num4 = HttpEncoderUtility.HexToInt(s[i + 2]);
                    int num5 = HttpEncoderUtility.HexToInt(s[i + 3]);
                    int num6 = HttpEncoderUtility.HexToInt(s[i + 4]);
                    int num7 = HttpEncoderUtility.HexToInt(s[i + 5]);
                    if (((num4 >= 0) && (num5 >= 0)) && ((num6 >= 0) && (num7 >= 0)))
                    {
                        ch = (char)((((num4 << 12) | (num5 << 8)) | (num6 << 4)) | num7);
                        i += 5;
                        decoder.AddChar(ch);
                        continue;
                    }
                }
                if ((ch & 0xff80) == 0)
                {
                    decoder.AddByte((byte)ch);
                }
                else
                {
                    decoder.AddChar(ch);
                }
            }
            return(decoder.GetString());
        }
 internal static string CollapsePercentUFromStringInternal(string s, Encoding e)
 {
     int length = s.Length;
     UrlDecoder decoder = new UrlDecoder(length, e);
     if (s.IndexOf("%u", StringComparison.Ordinal) == -1)
     {
         return s;
     }
     for (int i = 0; i < length; i++)
     {
         char ch = s[i];
         if (((ch == '%') && (i < (length - 5))) && (s[i + 1] == 'u'))
         {
             int num4 = HttpEncoderUtility.HexToInt(s[i + 2]);
             int num5 = HttpEncoderUtility.HexToInt(s[i + 3]);
             int num6 = HttpEncoderUtility.HexToInt(s[i + 4]);
             int num7 = HttpEncoderUtility.HexToInt(s[i + 5]);
             if (((num4 >= 0) && (num5 >= 0)) && ((num6 >= 0) && (num7 >= 0)))
             {
                 ch = (char) ((((num4 << 12) | (num5 << 8)) | (num6 << 4)) | num7);
                 i += 5;
                 decoder.AddChar(ch);
                 continue;
             }
         }
         if ((ch & 0xff80) == 0)
         {
             decoder.AddByte((byte) ch);
         }
         else
         {
             decoder.AddChar(ch);
         }
     }
     return decoder.GetString();
 }
Пример #15
0
        /// <summary>
        /// Decode an URL transmission-encoded string into its original representation using specified encoding.
        /// </summary>
        /// <param name="s">The string to decode.</param>
        /// <param name="encoding">The encoding to use.</param>
        public static string UrlDecode(string s, Encoding encoding)
        {
            if (s == null)
            {
                return(null);
            }

            int        count  = s.Length;
            UrlDecoder buffer = new UrlDecoder(count, encoding);

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

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

                        if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0)
                        {
                            ch   = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4);
                            pos += 5;
                            buffer.AddChar(ch); continue;
                        }
                    }
                    else    // %XX
                    {
                        int h1 = HexToInt(s[pos + 1]);
                        int h2 = HexToInt(s[pos + 2]);

                        if (h1 >= 0 && h2 >= 0)
                        {
                            byte b = (byte)((h1 << 4) | h2);
                            pos += 2;
                            buffer.AddByte(b); continue;
                        }
                    }
                }

                if ((ch & 0xFF80) == 0)
                {
                    buffer.AddByte((byte)ch);
                }
                else
                {
                    buffer.AddChar(ch);
                }
            }

            return(buffer.GetString());
        }
Пример #16
0
        private static string UrlDecodeInternal(string value, Encoding encoding)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(value);
            }

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

            // go through the string's chars collapsing %XX and
            // appending each char as char, with exception of %XX constructs
            // that are appended as bytes
            bool needsDecodingUnsafe = false;
            bool needsDecodingSpaces = false;

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

                if (ch == '+')
                {
                    needsDecodingSpaces = true;
                    ch = ' ';
                }
                else if (ch == '%' && pos < count - 2)
                {
                    int h1 = HexToInt(value[pos + 1]);
                    int h2 = HexToInt(value[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);
                        needsDecodingUnsafe = true;
                        continue;
                    }
                }

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

            if (!needsDecodingUnsafe)
            {
                if (needsDecodingSpaces)
                {
                    // Only spaces to decode
                    return(value.Replace('+', ' '));
                }

                // Nothing to decode
                return(value);
            }

            return(helper.GetString());
        }
Пример #17
0
            private static string UrlDecodeStringFromStringInternal(string s, Encoding e)
            {
                int count = s.Length;
                UrlDecoder helper = new UrlDecoder(count, e);

                // 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 = s[pos];

                    if (ch == '+')
                    {
                        ch = ' ';
                    }
                    else if (ch == '%' && pos < count - 2)
                    {
                        if (s[pos + 1] == 'u' && pos < count - 5)
                        {
                            int h1 = HexToInt(s[pos + 2]);
                            int h2 = HexToInt(s[pos + 3]);
                            int h3 = HexToInt(s[pos + 4]);
                            int h4 = HexToInt(s[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 = HexToInt(s[pos + 1]);
                            int h2 = HexToInt(s[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.GetString();
            }
Пример #18
0
 private static string UrlDecodeStringFromStringInternal(string s, Encoding e)
 {
     int length = s.Length;
     UrlDecoder decoder = new UrlDecoder(length, e);
     for (int i = 0; i < length; i++)
     {
         char ch = s[i];
         if (ch == '+')
         {
             ch = ' ';
         }
         else if ((ch == '%') && (i < (length - 2)))
         {
             if ((s[i + 1] == 'u') && (i < (length - 5)))
             {
                 int num3 = HexToInt(s[i + 2]);
                 int num4 = HexToInt(s[i + 3]);
                 int num5 = HexToInt(s[i + 4]);
                 int num6 = HexToInt(s[i + 5]);
                 if (((num3 < 0) || (num4 < 0)) || ((num5 < 0) || (num6 < 0)))
                 {
                     goto Label_0106;
                 }
                 ch = (char)((((num3 << 12) | (num4 << 8)) | (num5 << 4)) | num6);
                 i += 5;
                 decoder.AddChar(ch);
                 continue;
             }
             int num7 = HexToInt(s[i + 1]);
             int num8 = HexToInt(s[i + 2]);
             if ((num7 >= 0) && (num8 >= 0))
             {
                 byte b = (byte)((num7 << 4) | num8);
                 i += 2;
                 decoder.AddByte(b);
                 continue;
             }
         }
         Label_0106:
         if ((ch & 0xff80) == 0)
         {
             decoder.AddByte((byte)ch);
         }
         else
         {
             decoder.AddChar(ch);
         }
     }
     return decoder.GetString();
 }
Пример #19
0
        internal static string UrlDecode(string value, Encoding encoding)
        {
            if (value == null)
            {
                return null;
            }

            var count = value.Length;
            var enncoder = 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 (var i = 0; i < count; i++)
            {
                var ch = value[i];

                if (ch == '+')
                {
                    ch = ' ';
                }
                else if (ch == '%' && i < count - 2)
                {
                    if (value[i + 1] == 'u' && i < count - 5)
                    {
                        var h1 = HttpEncoderUtility.HexToInt(value[i + 2]);
                        var h2 = HttpEncoderUtility.HexToInt(value[i + 3]);
                        var h3 = HttpEncoderUtility.HexToInt(value[i + 4]);
                        var h4 = HttpEncoderUtility.HexToInt(value[i + 5]);

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

                            // only add as char 
                            enncoder.AddChar(ch);
                            continue;
                        }
                    }
                    else
                    {
                        var h1 = HttpEncoderUtility.HexToInt(value[i + 1]);
                        var h2 = HttpEncoderUtility.HexToInt(value[i + 2]);

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

                            // don't add as char
                            enncoder.AddByte(b);
                            continue;
                        }
                    }
                }
                if ((ch & 0xFF80) == 0)//unicode
                    enncoder.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode 
                else
                    enncoder.AddChar(ch);
            }

            return enncoder.GetString();
        }
Пример #20
0
        /// <summary>
        /// UrlDecodes a string
        /// </summary>
        public static string DecodeURL(this string url)
        {
            if (url == null)
            {
                return(null);
            }
            int        length  = url.Length;
            UrlDecoder decoder = new UrlDecoder(length, Encoding.UTF8);

            // per char
            for (int i = 0; i < length; i++)
            {
                char ch = url[i];


                // PLUS char converts to SPACE
                if (ch == '+')
                {
                    ch = ' ';

                    // SPECIAL chars encoded in "%20" format
                }
                else if ((ch == '%') && (i < (length - 2)))
                {
                    // unicode char (4 digit hex)
                    if ((url[i + 1] == 'u') && (i < (length - 5)))
                    {
                        int num3 = HexToInt(url[i + 2]);
                        int num4 = HexToInt(url[i + 3]);
                        int num5 = HexToInt(url[i + 4]);
                        int num6 = HexToInt(url[i + 5]);
                        if (((num3 < 0) || (num4 < 0)) || ((num5 < 0) || (num6 < 0)))
                        {
                            goto Label_010B;
                        }
                        ch = (char)((((num3 << 12) | (num4 << 8)) | (num5 << 4)) | num6);
                        i += 5;
                        decoder.AddChar(ch);
                        continue;
                    }

                    // ascii char (2 digit hex)
                    int num7 = HexToInt(url[i + 1]);
                    int num8 = HexToInt(url[i + 2]);
                    if ((num7 >= 0) && (num8 >= 0))
                    {
                        byte b = (byte)((num7 << 4) | num8);
                        i += 2;
                        decoder.AddByte(b);
                        continue;
                    }
                }
Label_010B:
                if ((ch & 0xff80) == 0)
                {
                    decoder.AddByte((byte)ch);
                }
                else
                {
                    decoder.AddChar(ch);
                }
            }
            return(decoder.GetString());
        }
Пример #21
0
        private static string UrlDecodeInternal(string value, Encoding encoding)
        {
            if (value == null)
            {
                return null;
            }
            int count = value.Length;
            UrlDecoder decoder = new UrlDecoder(count, encoding);
            for (int pos = 0; pos < count; pos++)
            {
                char ch = value[pos];
                if (ch == '+')
                {
                    ch = ' ';
                }
                else if ((ch == '%') && (pos < (count - 2)))
                {
                    if (value[pos + 1] == 'u' && pos < (count - 5))
                    {
                        int h1 = HexToInt(value[pos + 2]);
                        int h2 = HexToInt(value[pos + 3]);
                        int h3 = HexToInt(value[pos + 4]);
                        int h4 = HexToInt(value[pos + 5]);

                        if (((h1 < 0) || (h2 < 0)) || ((h3 < 0) || (h4 < 0)))
                        {
                            goto Label_010B;
                        }
                        ch = (char)((((h1 << 12) | (h2 << 8)) | (h3 << 4)) | h4);
                        pos += 5;
                        decoder.AddChar(ch);
                        continue;
                    }
                    int num7 = HexToInt(value[pos + 1]);
                    int num8 = HexToInt(value[pos + 2]);
                    if ((num7 >= 0) && (num8 >= 0))
                    {
                        byte b = (byte)((num7 << 4) | num8);
                        pos += 2;
                        decoder.AddByte(b);
                        continue;
                    }
                }
            Label_010B:
                if ((ch & 0xff80) == 0)
                {
                    decoder.AddByte((byte)ch);
                }
                else
                {
                    decoder.AddChar(ch);
                }
            }
            return decoder.GetString();
        }
Пример #22
0
        internal static string UrlDecode(string value, Encoding encoding)
        {
            if (value == null)
            {
                return null;
            }

            int count = value.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 = value[pos];

                if (ch == '+')
                {
                    ch = ' ';
                }
                else if (ch == '%' && pos < count - 2)
                {
                    if (value[pos + 1] == 'u' && pos < count - 5)
                    {
                        int h1 = HttpEncoderUtility.HexToInt(value[pos + 2]);
                        int h2 = HttpEncoderUtility.HexToInt(value[pos + 3]);
                        int h3 = HttpEncoderUtility.HexToInt(value[pos + 4]);
                        int h4 = HttpEncoderUtility.HexToInt(value[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 = HttpEncoderUtility.HexToInt(value[pos + 1]);
                        int h2 = HttpEncoderUtility.HexToInt(value[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 Utf16StringValidator.ValidateString(helper.GetString());
        }
Пример #23
0
        internal static string UrlDecode(byte[] bytes, int offset, int count, Encoding encoding)
        {
            if (!ValidateUrlEncodingParameters(bytes, offset, count))
            {
                return null;
            }

            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 = HttpEncoderUtility.HexToInt((char)bytes[pos + 2]);
                        int h2 = HttpEncoderUtility.HexToInt((char)bytes[pos + 3]);
                        int h3 = HttpEncoderUtility.HexToInt((char)bytes[pos + 4]);
                        int h4 = HttpEncoderUtility.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 = HttpEncoderUtility.HexToInt((char)bytes[pos + 1]);
                        int h2 = HttpEncoderUtility.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 Utf16StringValidator.ValidateString(helper.GetString());
        }
        internal static string CollapsePercentUFromStringInternal(string s, Encoding e) {
            int count = s.Length;
            UrlDecoder helper = new UrlDecoder(count, e);

            // go thorugh the string's chars collapsing just %uXXXX and
            // appending each char as char
            int loc = s.IndexOf("%u", StringComparison.Ordinal);
            if (loc == -1) {
                return s;
            }

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

                if (ch == '%' && pos < count - 5) {
                    if (s[pos + 1] == 'u') {
                        int h1 = HttpEncoderUtility.HexToInt(s[pos + 2]);
                        int h2 = HttpEncoderUtility.HexToInt(s[pos + 3]);
                        int h3 = HttpEncoderUtility.HexToInt(s[pos + 4]);
                        int h4 = HttpEncoderUtility.HexToInt(s[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;

                            // add as char
                            helper.AddChar(ch);
                            continue;
                        }
                    }
                }
                if ((ch & 0xFF80) == 0)
                    helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode
                else
                    helper.AddChar(ch);
            }
            return Utf16StringValidator.ValidateString(helper.GetString());
        }
Пример #25
0
        /// <summary>
        /// Decode an URL transmission-encoded string into its original representation using specified encoding.
        /// </summary>
        /// <param name="s">The string to decode.</param>
        /// <param name="encoding">The encoding to use.</param>
        public static string UrlDecode(string s, Encoding encoding)
        {
            if (s == null) return null;

            int count = s.Length;
            UrlDecoder buffer = new UrlDecoder(count, encoding);

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

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

                            if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0) {
                                ch = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4);
                                pos += 5;
                                buffer.AddChar(ch); continue;
                        }
                    }
                    else {  // %XX
                        int h1 = HexToInt(s[pos + 1]);
                        int h2 = HexToInt(s[pos + 2]);

                        if (h1 >= 0 && h2 >= 0) {
                            byte b = (byte)((h1 << 4) | h2);
                            pos += 2;
                            buffer.AddByte(b); continue;
                        }
                    }
                }

                if ((ch & 0xFF80) == 0)
                    buffer.AddByte((byte)ch);
                else
                    buffer.AddChar(ch);
            }

            return buffer.GetString();
        }
Пример #26
0
        /// <summary>
        /// UrlDecodes a string except chars forbidden in Windows filepaths
        /// </summary>
        public static string DecodeURLForFilepath(this string url)
        {
            if (url == null)
            {
                return(null);
            }

            int        length  = url.Length;
            UrlDecoder decoder = new UrlDecoder(length * 10, Encoding.UTF8);

            decoder.forFilePaths = true;


            // per char
            for (int i = 0; i < length; i++)
            {
                char ch = url[i];


                // PLUS char converts to SPACE
                if (ch == '+')
                {
                    ch = ' ';

                    // SPECIAL chars encoded in "%20" format
                }
                else if ((ch == '%') && (i < (length - 2)))
                {
                    // unicode char (4 digit hex)
                    if ((url[i + 1] == 'u') && (i < (length - 5)))
                    {
                        int num3 = HexToInt(url[i + 2]);
                        int num4 = HexToInt(url[i + 3]);
                        int num5 = HexToInt(url[i + 4]);
                        int num6 = HexToInt(url[i + 5]);
                        if (((num3 < 0) || (num4 < 0)) || ((num5 < 0) || (num6 < 0)))
                        {
                            goto Label_010B;
                        }
                        ch = (char)((((num3 << 12) | (num4 << 8)) | (num5 << 4)) | num6);
                        i += 5;
                        decoder.FlushBytes(false);                         // dont check previous stuff
                        decoder.AddChar(ch, true);                         // CHECK IF CHAR OK WITH FILEPATH
                        continue;
                    }

                    // ascii char (2 digit hex)
                    int num7 = HexToInt(url[i + 1]);
                    int num8 = HexToInt(url[i + 2]);
                    if ((num7 >= 0) && (num8 >= 0))
                    {
                        byte b = (byte)((num7 << 4) | num8);
                        i += 2;
                        decoder.FlushBytes(false);                         // dont check previous stuff
                        decoder.AddByte(b);

                        // check if unicode char ("%11%11")
                        if (((i + 1) < (length - 2)) && (url[i + 1] == '%'))
                        {
                            // YES, unicode char
                            num7 = HexToInt(url[i + 1]);
                            num8 = HexToInt(url[i + 2]);
                            if ((num7 >= 0) && (num8 >= 0))
                            {
                                b  = (byte)((num7 << 4) | num8);
                                i += 2;
                                decoder.AddByte(b);
                                decoder.FlushBytes(true);                                 // CHECK IF CHARS OK WITH FILEPATHS
                            }
                        }
                        else
                        {
                            // NO, not unicode char
                            decoder.FlushBytes(true);                             // CHECK IF CHARS OK WITH FILEPATHS
                        }

                        continue;
                    }
                }

Label_010B:
                if ((ch & 0xff80) == 0)
                {
                    decoder.AddByte((byte)ch);
                }
                else
                {
                    decoder.AddChar(ch, false);
                }
            }

            return(decoder.GetString());
        }
Пример #27
0
        internal static string UrlDecode(byte[] bytes, int offset, int count, Encoding encoding)
        {
            if (!ValidateUrlEncodingParameters(bytes, offset, count))
            {
                return(null);
            }

            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 = HttpEncoderUtility.HexToInt((char)bytes[pos + 2]);
                        int h2 = HttpEncoderUtility.HexToInt((char)bytes[pos + 3]);
                        int h3 = HttpEncoderUtility.HexToInt((char)bytes[pos + 4]);
                        int h4 = HttpEncoderUtility.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 = HttpEncoderUtility.HexToInt((char)bytes[pos + 1]);
                        int h2 = HttpEncoderUtility.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(Utf16StringValidator.ValidateString(helper.GetString()));
        }
Пример #28
0
        private static string UrlDecodeInternal(string value, Encoding encoding)
        {
            if (string.IsNullOrEmpty(value))
            {
                return value;
            }

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

            // go through the string's chars collapsing %XX and
            // appending each char as char, with exception of %XX constructs
            // that are appended as bytes
            bool needsDecodingUnsafe = false;
            bool needsDecodingSpaces = false;
            for (int pos = 0; pos < count; pos++)
            {
                char ch = value[pos];

                if (ch == '+')
                {
                    needsDecodingSpaces = true;
                    ch = ' ';
                }
                else if (ch == '%' && pos < count - 2)
                {
                    int h1 = HexToInt(value[pos + 1]);
                    int h2 = HexToInt(value[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);
                        needsDecodingUnsafe = true;
                        continue;
                    }
                }

                if ((ch & 0xFF80) == 0)
                    helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode
                else
                    helper.AddChar(ch);
            }
            
            if (!needsDecodingUnsafe)
            {
                if (needsDecodingSpaces)
                {
                    // Only spaces to decode
                    return value.Replace('+', ' ');
                }

                // Nothing to decode
                return value;
            }

            return helper.GetString();
        }
Пример #29
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());
        }
 internal string UrlDecode(string value, Encoding encoding)
 {
     if (value == null)
     {
         return null;
     }
     int length = value.Length;
     UrlDecoder decoder = new UrlDecoder(length, encoding);
     for (int i = 0; i < length; i++)
     {
         char ch = value[i];
         if (ch == '+')
         {
             ch = ' ';
         }
         else if ((ch == '%') && (i < (length - 2)))
         {
             if ((value[i + 1] == 'u') && (i < (length - 5)))
             {
                 int num3 = HttpEncoderUtility.HexToInt(value[i + 2]);
                 int num4 = HttpEncoderUtility.HexToInt(value[i + 3]);
                 int num5 = HttpEncoderUtility.HexToInt(value[i + 4]);
                 int num6 = HttpEncoderUtility.HexToInt(value[i + 5]);
                 if (((num3 < 0) || (num4 < 0)) || ((num5 < 0) || (num6 < 0)))
                 {
                     goto Label_010B;
                 }
                 ch = (char) ((((num3 << 12) | (num4 << 8)) | (num5 << 4)) | num6);
                 i += 5;
                 decoder.AddChar(ch);
                 continue;
             }
             int num7 = HttpEncoderUtility.HexToInt(value[i + 1]);
             int num8 = HttpEncoderUtility.HexToInt(value[i + 2]);
             if ((num7 >= 0) && (num8 >= 0))
             {
                 byte b = (byte) ((num7 << 4) | num8);
                 i += 2;
                 decoder.AddByte(b);
                 continue;
             }
         }
     Label_010B:
         if ((ch & 0xff80) == 0)
         {
             decoder.AddByte((byte) ch);
         }
         else
         {
             decoder.AddChar(ch);
         }
     }
     return decoder.GetString();
 }
Пример #31
0
        /// <summary>
        /// Performs urlencoding on a string.
        /// </summary>
        /// <param name="source">The original string.</param>
        /// <returns>The urlencoded string.</returns>
        public static string UrlDecode(string source, Encoding encoding)
        {
            if (source == null)
                return null;

            Func<char, int> unhexify = h => {
                if ((h >= '0') && (h <= '9')) return (h - '0');
                if ((h >= 'a') && (h <= 'f')) return ((h - 'a') + 10);
                if ((h >= 'A') && (h <= 'F')) return ((h - 'A') + 10);
                return -1;
            };

            var length = source.Length;
            UrlDecoder decoder = new UrlDecoder(length, encoding);
            for (var i = 0; i < length; i++) {
                var ch = source[i];
                if (ch == '+') {
                    ch = ' ';
                }
                else if ((ch == '%') && (i < (length - 2))) {
                    if ((source[i + 1] == 'u') && (i < (length - 5))) {
                        var unicode1 = unhexify(source[i + 2]);
                        var unicode2 = unhexify(source[i + 3]);
                        var unicode3 = unhexify(source[i + 4]);
                        var unicode4 = unhexify(source[i + 5]);
                        if (((unicode1 < 0) || (unicode2 < 0)) || ((unicode3 < 0) || (unicode4 < 0))) {
                            if ((ch & 0xff80) == 0) {
                                decoder.AddByte((byte)ch);
                            }
                            else {
                                decoder.AddChar(ch);
                            }
                        }
                        ch = (char)((((unicode1 << 12) | (unicode2 << 8)) | (unicode3 << 4)) | unicode4);
                        i += 5;
                        decoder.AddChar(ch);
                        continue;
                    }

                    var plain1 = unhexify(source[i + 1]);
                    var plain2 = unhexify(source[i + 2]);
                    if ((plain1 >= 0) && (plain2 >= 0)) {
                        byte b = (byte)((plain1 << 4) | plain2);
                        i += 2;
                        decoder.AddByte(b);
                        continue;
                    }
                }

                if ((ch & 0xff80) == 0) {
                    decoder.AddByte((byte)ch);
                }
                else {
                    decoder.AddChar(ch);
                }
            }

            return decoder.GetString();
        }
Пример #32
0
            private static string UrlDecodeStringFromStringInternal(string s, Encoding e)
            {
                int        count  = s.Length;
                UrlDecoder helper = new UrlDecoder(count, e);

                // 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 = s[pos];

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

                            if ((h1 | h2 | h3 | h4) != 0xFF)
                            {   // 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 = HexConverter.FromChar(s[pos + 1]);
                            int h2 = HexConverter.FromChar(s[pos + 2]);

                            if ((h1 | h2) != 0xFF)
                            {     // 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.GetString());
            }
Пример #33
0
 private static string UrlDecodeStringFromBytesInternal(byte[] buf, int offset, int count, Encoding e)
 {
     UrlDecoder decoder = new UrlDecoder(count, e);
     for (int i = 0; i < count; i++)
     {
         int index = offset + i;
         byte b = buf[index];
         if (b == 0x2b)
         {
             b = 0x20;
         }
         else if ((b == 0x25) && (i < (count - 2)))
         {
             if ((buf[index + 1] == 0x75) && (i < (count - 5)))
             {
                 int num4 = HexToInt((char)buf[index + 2]);
                 int num5 = HexToInt((char)buf[index + 3]);
                 int num6 = HexToInt((char)buf[index + 4]);
                 int num7 = HexToInt((char)buf[index + 5]);
                 if (((num4 < 0) || (num5 < 0)) || ((num6 < 0) || (num7 < 0)))
                 {
                     break;
                 }
                 char ch = (char)((((num4 << 12) | (num5 << 8)) | (num6 << 4)) | num7);
                 i += 5;
                 decoder.AddChar(ch);
                 continue;
             }
             int num8 = HexToInt((char)buf[index + 1]);
             int num9 = HexToInt((char)buf[index + 2]);
             if ((num8 >= 0) && (num9 >= 0))
             {
                 b = (byte)((num8 << 4) | num9);
                 i += 2;
             }
         }
         decoder.AddByte(b);
     }
     return decoder.GetString();
 }
Пример #34
0
        internal static string UrlDecode(string value, Encoding encoding)
        {
            if (value == null)
            {
                return(null);
            }

            int        count  = value.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 = value[pos];

                if (ch == '+')
                {
                    ch = ' ';
                }
                else if (ch == '%' && pos < count - 2)
                {
                    if (value[pos + 1] == 'u' && pos < count - 5)
                    {
                        int h1 = HttpEncoderUtility.HexToInt(value[pos + 2]);
                        int h2 = HttpEncoderUtility.HexToInt(value[pos + 3]);
                        int h3 = HttpEncoderUtility.HexToInt(value[pos + 4]);
                        int h4 = HttpEncoderUtility.HexToInt(value[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 = HttpEncoderUtility.HexToInt(value[pos + 1]);
                        int h2 = HttpEncoderUtility.HexToInt(value[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(Utf16StringValidator.ValidateString(helper.GetString()));
        }
 internal string UrlDecode(byte[] bytes, int offset, int count, Encoding encoding)
 {
     if (!ValidateUrlEncodingParameters(bytes, offset, count))
     {
         return null;
     }
     UrlDecoder decoder = new UrlDecoder(count, encoding);
     for (int i = 0; i < count; i++)
     {
         int index = offset + i;
         byte b = bytes[index];
         if (b == 0x2b)
         {
             b = 0x20;
         }
         else if ((b == 0x25) && (i < (count - 2)))
         {
             if ((bytes[index + 1] == 0x75) && (i < (count - 5)))
             {
                 int num4 = HttpEncoderUtility.HexToInt((char) bytes[index + 2]);
                 int num5 = HttpEncoderUtility.HexToInt((char) bytes[index + 3]);
                 int num6 = HttpEncoderUtility.HexToInt((char) bytes[index + 4]);
                 int num7 = HttpEncoderUtility.HexToInt((char) bytes[index + 5]);
                 if (((num4 < 0) || (num5 < 0)) || ((num6 < 0) || (num7 < 0)))
                 {
                     goto Label_00E7;
                 }
                 char ch = (char) ((((num4 << 12) | (num5 << 8)) | (num6 << 4)) | num7);
                 i += 5;
                 decoder.AddChar(ch);
                 continue;
             }
             int num8 = HttpEncoderUtility.HexToInt((char) bytes[index + 1]);
             int num9 = HttpEncoderUtility.HexToInt((char) bytes[index + 2]);
             if ((num8 >= 0) && (num9 >= 0))
             {
                 b = (byte) ((num8 << 4) | num9);
                 i += 2;
             }
         }
     Label_00E7:
         decoder.AddByte(b);
     }
     return decoder.GetString();
 }