コード例 #1
0
ファイル: TypeUtil.cs プロジェクト: chris2001jm/njetty
 /* ------------------------------------------------------------ */
 public static byte[] ParseBytes(string s, int radix)
 {
     byte[] bytes = new byte[s.Length / 2];
     for (int i = 0; i < s.Length; i += 2)
     {
         bytes[i / 2] = (byte)TypeUtil.ParseInt(s, i, 2, radix);
     }
     return(bytes);
 }
コード例 #2
0
ファイル: URIUtil.cs プロジェクト: chris2001jm/njetty
        /// <summary>
        /// Decode a URI path.
        /// </summary>
        /// <param name="buf">StringBuilder to encode path into</param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string DecodePath(byte[] buf, int offset, int length)
        {
            byte[] bytes = null;
            int    n     = 0;

            for (int i = 0; i < length; i++)
            {
                byte b = buf[i + offset];

                if (b == '%' && (i + 2) < length)
                {
                    b  = (byte)(0xff & TypeUtil.ParseInt(buf, i + offset + 1, 2, 16));
                    i += 2;
                }
                else if (bytes == null)
                {
                    n++;
                    continue;
                }

                if (bytes == null)
                {
                    bytes = new byte[length];
                    for (int j = 0; j < n; j++)
                    {
                        bytes[j] = buf[j + offset];
                    }
                }

                bytes[n++] = b;
            }

            if (bytes == null)
            {
                return(StringUtil.ToString(buf, offset, length, __CHARSET));
            }
            return(StringUtil.ToString(bytes, 0, n, __CHARSET));
        }
コード例 #3
0
ファイル: UrlEncoded.cs プロジェクト: chris2001jm/njetty
        /// <summary>
        /// Decode string with % encoding.
        /// This method makes the assumption that the majority of calls
        /// will need no decoding.
        /// </summary>
        /// <param name="encoded"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <param name="charset"></param>
        /// <returns></returns>
        public static string DecodeString(string encoded, int offset, int length, string charset)
        {
            if (charset == null || StringUtil.IsUTF8(charset))
            {
                Utf8StringBuffer buffer = null;

                for (int i = 0; i < length; i++)
                {
                    char c = encoded[offset + i];
                    if (c < 0 || c > 0xff)
                    {
                        if (buffer == null)
                        {
                            buffer = new Utf8StringBuffer(length);
                            buffer.Append(encoded, offset, i + 1);
                        }
                        else
                        {
                            buffer.Append((byte)c);
                        }
                    }
                    else if (c == '+')
                    {
                        if (buffer == null)
                        {
                            buffer = new Utf8StringBuffer(length);
                            buffer.Append(encoded, offset, i);
                        }

                        buffer.Append((byte)' ');
                    }
                    else if (c == '%' && (i + 2) < length)
                    {
                        if (buffer == null)
                        {
                            buffer = new Utf8StringBuffer(length);
                            buffer.Append(encoded, offset, i);
                        }

                        while (c == '%' && (i + 2) < length)
                        {
                            try
                            {
                                byte b = (byte)TypeUtil.ParseInt(encoded, offset + i + 1, 2, 16);
                                buffer.Append(b);
                                i += 3;
                            }
                            catch (FormatException)
                            {
                                buffer.Append('%');
                                for (char next; ((next = encoded[++i + offset]) != '%');)
                                {
                                    buffer.Append((next == '+' ? ' ' : next));
                                }
                            }

                            if (i < length)
                            {
                                c = encoded[offset + i];
                            }
                        }
                        i--;
                    }
                    else if (buffer != null)
                    {
                        buffer.Append((byte)c);
                    }
                }

                if (buffer == null)
                {
                    if (offset == 0 && encoded.Length == length)
                    {
                        return(encoded);
                    }
                    return(encoded.Substring(offset, length));
                }

                return(buffer.ToString());
            }
            else
            {
                StringBuilder buffer = null;

                try
                {
                    for (int i = 0; i < length; i++)
                    {
                        char c = encoded[offset + i];
                        if (c < 0 || c > 0xff)
                        {
                            if (buffer == null)
                            {
                                buffer = new StringBuilder(length);
                                buffer.Append(encoded, offset, i + 1);
                            }
                            else
                            {
                                buffer.Append(c);
                            }
                        }
                        else if (c == '+')
                        {
                            if (buffer == null)
                            {
                                buffer = new StringBuilder(length);
                                buffer.Append(encoded, offset, i);
                            }

                            buffer.Append(' ');
                        }
                        else if (c == '%' && (i + 2) < length)
                        {
                            if (buffer == null)
                            {
                                buffer = new StringBuilder(length);
                                buffer.Append(encoded, offset, i);
                            }

                            byte[] ba = new byte[length];
                            int    n  = 0;
                            while (c >= 0 && c <= 0xff)
                            {
                                if (c == '%')
                                {
                                    if (i + 2 < length)
                                    {
                                        try
                                        {
                                            ba[n++] = (byte)TypeUtil.ParseInt(encoded, offset + i + 1, 2, 16);
                                            i      += 3;
                                        }
                                        catch (FormatException)
                                        {
                                            ba[n - 1] = (byte)'%';
                                            for (char next; ((next = encoded[++i + offset]) != '%');)
                                            {
                                                ba[n++] = (byte)(next == '+' ? ' ' : next);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        ba[n++] = (byte)'%';
                                        i++;
                                    }
                                }
                                else if (c == '+')
                                {
                                    ba[n++] = (byte)' ';
                                    i++;
                                }
                                else
                                {
                                    ba[n++] = (byte)c;
                                    i++;
                                }

                                if (i >= length)
                                {
                                    break;
                                }
                                c = encoded[offset + i];
                            }

                            i--;
                            string str = Encoding.GetEncoding(charset).GetString(ba, 0, n);
                            buffer.Append(str);
                        }
                        else if (buffer != null)
                        {
                            buffer.Append(c);
                        }
                    }

                    if (buffer == null)
                    {
                        if (offset == 0 && encoded.Length == length)
                        {
                            return(encoded);
                        }
                        return(encoded.Substring(offset, length));
                    }

                    return(buffer.ToString());
                }
                catch (ArgumentException e)
                {
                    throw new SystemException(e.Message, e);
                }
            }
        }