Esempio n. 1
0
        internal static byte[] UrlDecodeToBytesInternal(byte[] bytes, int offset, int count)
        {
            MemoryStream memoryStream = new MemoryStream();
            int          num          = offset + count;

            for (int i = offset; i < num; i++)
            {
                char c = (char)bytes[i];
                if (c == '+')
                {
                    c = ' ';
                }
                else if (c == '%' && i < num - 2)
                {
                    int @char = HttpUtility.GetChar(bytes, i + 1, 2);
                    if (@char != -1)
                    {
                        c  = (char)@char;
                        i += 2;
                    }
                }
                memoryStream.WriteByte((byte)c);
            }
            return(memoryStream.ToArray());
        }
Esempio n. 2
0
        public static string UrlDecode(string s, Encoding encoding)
        {
            if (s == null || s.Length == 0 || !s.Contains(new char[]
            {
                '%',
                '+'
            }))
            {
                return(s);
            }
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            int         length = s.Length;
            List <byte> list   = new List <byte>();

            for (int i = 0; i < length; i++)
            {
                char c = s[i];
                if (c == '%' && i + 2 < length && s[i + 1] != '%')
                {
                    int @char;
                    if (s[i + 1] == 'u' && i + 5 < length)
                    {
                        @char = HttpUtility.GetChar(s, i + 2, 4);
                        if (@char != -1)
                        {
                            HttpUtility.WriteCharBytes(list, (char)@char, encoding);
                            i += 5;
                        }
                        else
                        {
                            HttpUtility.WriteCharBytes(list, '%', encoding);
                        }
                    }
                    else if ((@char = HttpUtility.GetChar(s, i + 1, 2)) != -1)
                    {
                        HttpUtility.WriteCharBytes(list, (char)@char, encoding);
                        i += 2;
                    }
                    else
                    {
                        HttpUtility.WriteCharBytes(list, '%', encoding);
                    }
                }
                else if (c == '+')
                {
                    HttpUtility.WriteCharBytes(list, ' ', encoding);
                }
                else
                {
                    HttpUtility.WriteCharBytes(list, c, encoding);
                }
            }
            byte[] bytes = list.ToArray();
            return(encoding.GetString(bytes));
        }
Esempio n. 3
0
        internal static string UrlDecodeInternal(byte[] bytes, int offset, int count, Encoding encoding)
        {
            StringBuilder stringBuilder = new StringBuilder();
            MemoryStream  memoryStream  = new MemoryStream();
            int           num           = count + offset;
            int           i             = offset;

            while (i < num)
            {
                if (bytes[i] != 37 || i + 2 >= count || bytes[i + 1] == 37)
                {
                    goto IL_C6;
                }
                if (bytes[i + 1] == 117 && i + 5 < num)
                {
                    if (memoryStream.Length > 0L)
                    {
                        stringBuilder.Append(HttpUtility.GetChars(memoryStream, encoding));
                        memoryStream.SetLength(0L);
                    }
                    int @char = HttpUtility.GetChar(bytes, i + 2, 4);
                    if (@char == -1)
                    {
                        goto IL_C6;
                    }
                    stringBuilder.Append((char)@char);
                    i += 5;
                }
                else
                {
                    int @char;
                    if ((@char = HttpUtility.GetChar(bytes, i + 1, 2)) == -1)
                    {
                        goto IL_C6;
                    }
                    memoryStream.WriteByte((byte)@char);
                    i += 2;
                }
IL_10E:
                i++;
                continue;
IL_C6:
                if (memoryStream.Length > 0L)
                {
                    stringBuilder.Append(HttpUtility.GetChars(memoryStream, encoding));
                    memoryStream.SetLength(0L);
                }
                if (bytes[i] == 43)
                {
                    stringBuilder.Append(' ');
                    goto IL_10E;
                }
                stringBuilder.Append((char)bytes[i]);
                goto IL_10E;
            }
            if (memoryStream.Length > 0L)
            {
                stringBuilder.Append(HttpUtility.GetChars(memoryStream, encoding));
            }
            return(stringBuilder.ToString());
        }