Beispiel #1
0
        private static unsafe int IndexOfHtmlEncodingChars(string s, int startPos)
        {
            Debug.Assert(0 <= startPos && startPos <= s.Length, "0 <= startPos && startPos <= s.Length");

            UnicodeEncodingConformance encodeConformance = HtmlEncodeConformance;
            int cch = s.Length - startPos;

            fixed(char *str = s)
            {
                for (char *pch = &str[startPos]; cch > 0; pch++, cch--)
                {
                    char ch = *pch;
                    if (ch <= '>')
                    {
                        switch (ch)
                        {
                        case '<':
                        case '>':
                        case '"':
                        case '\'':
                        case '&':
                            return(s.Length - cch);
                        }
                    }
#if ENTITY_ENCODE_HIGH_ASCII_CHARS
                    else if (ch >= 160 && ch < 256)
                    {
                        return(s.Length - cch);
                    }
#endif // ENTITY_ENCODE_HIGH_ASCII_CHARS
                    else if (encodeConformance == UnicodeEncodingConformance.Strict && Char.IsSurrogate(ch))
                    {
                        return(s.Length - cch);
                    }
                }
            }

            return(-1);
        }
Beispiel #2
0
 static WebUtility()
 {
     s_htmlDecodeConformance = UnicodeDecodingConformance.Strict;
     s_htmlEncodeConformance = UnicodeEncodingConformance.Strict;
 }
Beispiel #3
0
        public static unsafe void HtmlEncode(string value, TextWriter output)
        {
            if (value == null)
            {
                return;
            }
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }

            int index = IndexOfHtmlEncodingChars(value, 0);

            if (index == -1)
            {
                output.Write(value);
                return;
            }

            Debug.Assert(0 <= index && index <= value.Length, "0 <= index && index <= value.Length");

            UnicodeEncodingConformance encodeConformance = HtmlEncodeConformance;
            int cch = value.Length - index;

            fixed(char *str = value)
            {
                char *pch = str;

                while (index-- > 0)
                {
                    output.Write(*pch++);
                }

                for (; cch > 0; cch--, pch++)
                {
                    char ch = *pch;
                    if (ch <= '>')
                    {
                        switch (ch)
                        {
                        case '<':
                            output.Write("&lt;");
                            break;

                        case '>':
                            output.Write("&gt;");
                            break;

                        case '"':
                            output.Write("&quot;");
                            break;

                        case '\'':
                            output.Write("&#39;");
                            break;

                        case '&':
                            output.Write("&amp;");
                            break;

                        default:
                            output.Write(ch);
                            break;
                        }
                    }
                    else
                    {
                        int valueToEncode = -1; // set to >= 0 if needs to be encoded

#if ENTITY_ENCODE_HIGH_ASCII_CHARS
                        if (ch >= 160 && ch < 256)
                        {
                            // The seemingly arbitrary 160 comes from RFC
                            valueToEncode = ch;
                        }
                        else
#endif // ENTITY_ENCODE_HIGH_ASCII_CHARS
                        if (encodeConformance == UnicodeEncodingConformance.Strict && Char.IsSurrogate(ch))
                        {
                            int scalarValue = GetNextUnicodeScalarValueFromUtf16Surrogate(ref pch, ref cch);
                            if (scalarValue >= UNICODE_PLANE01_START)
                            {
                                valueToEncode = scalarValue;
                            }
                            else
                            {
                                // Don't encode BMP characters (like U+FFFD) since they wouldn't have
                                // been encoded if explicitly present in the string anyway.
                                ch = (char)scalarValue;
                            }
                        }

                        if (valueToEncode >= 0)
                        {
                            // value needs to be encoded
                            output.Write("&#");
                            output.Write(valueToEncode.ToString(NumberFormatInfo.InvariantInfo));
                            output.Write(';');
                        }
                        else
                        {
                            // write out the character directly
                            output.Write(ch);
                        }
                    }
                }
            }
        }
Beispiel #4
0
 static WebUtility()
 {
     s_htmlDecodeConformance = UnicodeDecodingConformance.Strict;
     s_htmlEncodeConformance = UnicodeEncodingConformance.Strict;
 }