Exemplo n.º 1
0
        /// <summary>
        /// Unquote a string.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string Unquote(string s)
        {
            if (s == null)
            {
                return(null);
            }
            if (s.Length < 2)
            {
                return(s);
            }

            char first = s[0];
            char last  = s[s.Length - 1];

            if (first != last || (first != '"' && first != '\''))
            {
                return(s);
            }

            StringBuilder b = new StringBuilder(s.Length - 2);

            lock (b)
            {
                bool escape = false;
                for (int i = 1; i < s.Length - 1; i++)
                {
                    char c = s[i];

                    if (escape)
                    {
                        escape = false;
                        switch (c)
                        {
                        case 'n':
                            b.Append('\n');
                            break;

                        case 'r':
                            b.Append('\r');
                            break;

                        case 't':
                            b.Append('\t');
                            break;

                        case 'f':
                            b.Append('\f');
                            break;

                        case 'b':
                            b.Append('\b');
                            break;

                        case 'u':
                            b.Append((char)(
                                         (TypeUtil.ConvertHexDigit((byte)s[i++]) << 24) +
                                         (TypeUtil.ConvertHexDigit((byte)s[i++]) << 16) +
                                         (TypeUtil.ConvertHexDigit((byte)s[i++]) << 8) +
                                         (TypeUtil.ConvertHexDigit((byte)s[i++]))
                                         )
                                     );
                            break;

                        default:
                            b.Append(c);
                            break;
                        }
                    }
                    else if (c == '\\')
                    {
                        escape = true;
                        continue;
                    }
                    else
                    {
                        b.Append(c);
                    }
                }

                return(b.ToString());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Decoded parameters to Map.
        /// </summary>
        /// <param name="input">InputSteam to read</param>
        /// <param name="map">MultiMap to Add parameters to</param>
        /// <param name="maxLength">maximum length of conent to read 0r -1 for no limit</param>
        public static void DecodeUtf8To(Stream input, MultiMap <string> map, int maxLength)
        {
            lock (map)
            {
                Utf8StringBuilder buffer = new Utf8StringBuilder();
                string            key    = null;
                string            value  = null;

                int b;

                // TODO cache of parameter names ???
                int totalLength = 0;
                while ((b = input.ReadByte()) >= 0)
                {
                    switch ((char)b)
                    {
                    case '&':
                        value = buffer.Length == 0 ? "" : buffer.ToString();
                        buffer.Reset();
                        if (key != null)
                        {
                            map.Append(key, value);
                        }
                        else if (value != null && value.Length > 0)
                        {
                            map.Append(value, "");
                        }
                        key   = null;
                        value = null;
                        break;

                    case '=':
                        if (key != null)
                        {
                            buffer.Append((byte)b);
                            break;
                        }
                        key = buffer.ToString();
                        buffer.Reset();
                        break;

                    case '+':
                        buffer.Append((byte)' ');
                        break;

                    case '%':
                        int dh = input.ReadByte();
                        int dl = input.ReadByte();
                        if (dh < 0 || dl < 0)
                        {
                            break;
                        }
                        buffer.Append((byte)((TypeUtil.ConvertHexDigit((byte)dh) << 4) + TypeUtil.ConvertHexDigit((byte)dl)));
                        break;

                    default:
                        buffer.Append((byte)b);
                        break;
                    }
                    if (maxLength >= 0 && (++totalLength > maxLength))
                    {
                        throw new InvalidOperationException("Form too large");
                    }
                }

                if (key != null)
                {
                    value = buffer.Length == 0 ? "" : buffer.ToString();
                    buffer.Reset();
                    map.Append(key, value);
                }
                else if (buffer.Length > 0)
                {
                    map.Append(buffer.ToString(), "");
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Decoded parameters to Map.
        /// </summary>
        /// <param name="input">the stream containing the encoded parameters</param>
        /// <param name="map"></param>
        /// <param name="charset"></param>
        /// <param name="maxLength"></param>
        public static void DecodeTo(Stream input, MultiMap <string> map, string charset, int maxLength)
        {
            if (charset == null || StringUtil.__ISO_8859_1.Equals(charset))
            {
                Decode88591To(input, map, maxLength);
                return;
            }

            if (StringUtil.__UTF8.Equals(charset, StringComparison.OrdinalIgnoreCase))
            {
                DecodeUtf8To(input, map, maxLength);
                return;
            }

            if (StringUtil.__UTF16.Equals(charset, StringComparison.OrdinalIgnoreCase)) // Should be all 2 byte encodings
            {
                DecodeUtf16To(input, map, maxLength);
                return;
            }


            lock (map)
            {
                string key   = null;
                string value = null;

                int c;
                int digit  = 0;
                int digits = 0;

                int totalLength = 0;
                ByteArrayOutputStream2 output = new ByteArrayOutputStream2();
                input.Position = 0;
                long size = 0;

                while ((c = input.ReadByte()) > 0)
                {
                    switch ((char)c)
                    {
                    case '&':
                        size            = output.Length;
                        value           = size == 0 ? "" : Encoding.GetEncoding(charset).GetString(output.GetBuffer(), 0, (int)size);
                        output.Position = 0;
                        if (key != null)
                        {
                            map.Append(key, value);
                        }
                        else if (value != null && value.Length > 0)
                        {
                            map.Append(value, "");
                        }
                        key   = null;
                        value = null;
                        break;

                    case '=':
                        if (key != null)
                        {
                            output.WriteByte(c);
                            break;
                        }
                        size            = output.Length;
                        key             = size == 0 ? "" : Encoding.GetEncoding(charset).GetString(output.GetBuffer(), 0, (int)size);
                        output.Position = 0;
                        break;

                    case '+':
                        output.WriteByte(' ');
                        break;

                    case '%':
                        digits = 2;
                        break;

                    default:
                        if (digits == 2)
                        {
                            digit  = TypeUtil.ConvertHexDigit((byte)c);
                            digits = 1;
                        }
                        else if (digits == 1)
                        {
                            int v = (byte)(digit << 4) + TypeUtil.ConvertHexDigit((byte)c);
                            output.WriteByte(v);
                            digits = 0;
                        }
                        else
                        {
                            output.WriteByte((byte)c);
                        }
                        break;
                    }

                    totalLength++;
                    if (maxLength >= 0 && totalLength > maxLength)
                    {
                        throw new InvalidOperationException("Form too large");
                    }
                }

                size = output.Length;
                if (key != null)
                {
                    value           = size == 0 ? "" : Encoding.GetEncoding(charset).GetString(output.GetBuffer(), 0, (int)size);
                    output.Position = 0;
                    map.Append(key, value);
                }
                else if (size > 0)
                {
                    map.Append(Encoding.GetEncoding(charset).GetString(output.GetBuffer(), 0, (int)size), "");
                }
            }
        }
Exemplo n.º 4
0
        /* -------------------------------------------------------------- */

        /** Decoded parameters to Map.
         * @param data the byte[] containing the encoded parameters
         */
        public static void DecodeUtf8To(byte[] raw, int offset, int length, MultiMap <string> map, Utf8StringBuilder buffer)
        {
            lock (map)
            {
                string key   = null;
                string value = null;

                // TODO cache of parameter names ???
                int end = offset + length;
                for (int i = offset; i < end; i++)
                {
                    byte b = raw[i];
                    switch ((char)(0xff & b))
                    {
                    case '&':
                        value = buffer.Length == 0 ? "" : buffer.ToString();
                        buffer.Reset();
                        if (key != null)
                        {
                            map.Append(key, value);
                        }
                        else if (value != null && value.Length > 0)
                        {
                            map.Append(value, "");
                        }
                        key   = null;
                        value = null;
                        break;

                    case '=':
                        if (key != null)
                        {
                            buffer.Append(b);
                            break;
                        }
                        key = buffer.ToString();
                        buffer.Reset();
                        break;

                    case '+':
                        buffer.Append((byte)' ');
                        break;

                    case '%':
                        if (i + 2 < end)
                        {
                            buffer.Append((byte)((TypeUtil.ConvertHexDigit(raw[++i]) << 4) + TypeUtil.ConvertHexDigit(raw[++i])));
                        }
                        break;

                    default:
                        buffer.Append(b);
                        break;
                    }
                }

                if (key != null)
                {
                    value = buffer.Length == 0 ? "" : buffer.ToString();
                    buffer.Reset();
                    map.Append(key, value);
                }
                else if (buffer.Length > 0)
                {
                    map.Append(buffer.ToString(), "");
                }
            }
        }