internal static string[] BuildCharExceptionArgs(char invChar, char nextChar)
        {
            string[] aStringList = new string[2];

            // for surrogate characters include both high and low char in the message so that a full character is displayed
            if (XmlCharType.IsHighSurrogate(invChar) && nextChar != 0)
            {
                int combinedChar = XmlCharType.CombineSurrogateChar(nextChar, invChar);
                aStringList[0] = new string(new char[] { invChar, nextChar });
                aStringList[1] = string.Format(CultureInfo.InvariantCulture, "0x{0:X2}", combinedChar);
            }
            else
            {
                // don't include 0 character in the string - in means eof-of-string in native code, where this may bubble up to
                if ((int)invChar == 0)
                {
                    aStringList[0] = ".";
                }
                else
                {
                    aStringList[0] = invChar.ToString(CultureInfo.InvariantCulture);
                }
                aStringList[1] = string.Format(CultureInfo.InvariantCulture, "0x{0:X2}", (int)invChar);
            }
            return(aStringList);
        }
Example #2
0
        internal void WriteRawWithSurrogateChecking(string text)
        {
            if (text == null)
            {
                return;
            }
            if (_cacheAttrValue)
            {
                _attrValue.Append(text);
            }

            int  len = text.Length;
            int  i   = 0;
            char ch  = (char)0;

            for (; ;)
            {
                unsafe
                {
                    while (i < len &&
                           ((_xmlCharType.charProperties[ch = text[i]] & XmlCharType.fCharData) != 0 || // ( xmlCharType.IsCharData( ( ch = text[i] ) )
                            ch < 0x20))
                    {
                        i++;
                    }
                }
                if (i == len)
                {
                    break;
                }
                if (XmlCharType.IsHighSurrogate(ch))
                {
                    if (i + 1 < len)
                    {
                        char lowChar = text[i + 1];
                        if (XmlCharType.IsLowSurrogate(lowChar))
                        {
                            i += 2;
                            continue;
                        }
                        else
                        {
                            throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, ch);
                        }
                    }
                    throw new ArgumentException(ResXml.Xml_InvalidSurrogateMissingLowChar);
                }
                else if (XmlCharType.IsLowSurrogate(ch))
                {
                    throw XmlConvert.CreateInvalidHighSurrogateCharException(ch);
                }
                else
                {
                    i++;
                }
            }

            _textWriter.Write(text);
            return;
        }
Example #3
0
        public void WriteRawWithSurrogateChecking(string text)
        {
            if (text == null)
            {
                return;
            }
            if (_cacheAttrValue)
            {
                _attrValue.Append(text);
            }

            int  len = text.Length;
            int  i   = 0;
            char ch  = (char)0;

            for (;;)
            {
                while (i < len && (_xmlCharType.IsCharData(ch = text[i]) || ch < 0x20))
                {
                    i++;
                }

                if (i == len)
                {
                    break;
                }
                if (XmlCharType.IsHighSurrogate(ch))
                {
                    if (i + 1 < len)
                    {
                        char lowChar = text[i + 1];
                        if (XmlCharType.IsLowSurrogate(lowChar))
                        {
                            i += 2;
                            continue;
                        }
                        else
                        {
                            throw new Exception("throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, ch);");
                            //throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, ch);
                        }
                    }
                    throw new Exception("throw new ArgumentException(SR.Xml_InvalidSurrogateMissingLowChar);");
                }
                else if (XmlCharType.IsLowSurrogate(ch))
                {
                    throw new Exception("throw XmlConvert.CreateInvalidHighSurrogateCharException(ch);");
                    //throw XmlConvert.CreateInvalidHighSurrogateCharException(ch);
                }
                else
                {
                    i++;
                }
            }

            _textWriter.Write(text);
            return;
        }
Example #4
0
        internal void WriteSurrogateChar(char lowChar, char highChar)
        {
            if (!XmlCharType.IsLowSurrogate(lowChar) ||
                !XmlCharType.IsHighSurrogate(highChar))
            {
                throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, highChar);
            }

            _textWriter.Write(highChar);
            _textWriter.Write(lowChar);
        }
Example #5
0
        public void WriteSurrogateCharEntity(char lowChar, char highChar)
        {
            if (!XmlCharType.IsLowSurrogate(lowChar) ||
                !XmlCharType.IsHighSurrogate(highChar))
            {
                throw new Exception("throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, highChar);");
            }
            int surrogateChar = XmlCharType.CombineSurrogateChar(lowChar, highChar);

            if (_cacheAttrValue)
            {
                _attrValue.Append(highChar);
                _attrValue.Append(lowChar);
            }

            _textWriter.Write("&#x");
            _textWriter.Write(surrogateChar.ToString("X", NumberFormatInfo.InvariantInfo));
            _textWriter.Write(';');
        }
Example #6
0
        public static unsafe void VerifyCharData(string data, ExceptionType invCharExceptionType, ExceptionType invSurrogateExceptionType)
        {
            if (string.IsNullOrEmpty(data))
            {
                return;
            }

            int i   = 0;
            int len = data.Length;

            for (;;)
            {
                while (i < len && (xmlCharType.charProperties[data[i]] & XmlCharType.fCharData) != 0)
                {
                    i++;
                }
                if (i == len)
                {
                    return;
                }

                char ch = data[i];
                if (XmlCharType.IsHighSurrogate(ch))
                {
                    if (i + 1 == len)
                    {
                        throw CreateException(SR.Xml_InvalidSurrogateMissingLowChar, invSurrogateExceptionType, 0, i + 1);
                    }
                    ch = data[i + 1];
                    if (XmlCharType.IsLowSurrogate(ch))
                    {
                        i += 2;
                        continue;
                    }
                    else
                    {
                        throw CreateInvalidSurrogatePairException(data[i + 1], data[i], invSurrogateExceptionType, 0, i + 1);
                    }
                }
                throw CreateInvalidCharException(data, i, invCharExceptionType);
            }
        }
        internal static void VerifyCharData(char[] data, int offset, int len, ExceptionType exceptionType)
        {
            if (data == null || len == 0)
            {
                return;
            }

            int i      = offset;
            int endPos = offset + len;

            for (; ;)
            {
                while (i < endPos && s_xmlCharType.IsCharData(data[i]))
                {
                    i++;
                }
                if (i == endPos)
                {
                    return;
                }

                char ch = data[i];
                if (XmlCharType.IsHighSurrogate(ch))
                {
                    if (i + 1 == endPos)
                    {
                        throw CreateException(SR.Xml_InvalidSurrogateMissingLowChar, exceptionType, 0, offset - i + 1);
                    }
                    ch = data[i + 1];
                    if (XmlCharType.IsLowSurrogate(ch))
                    {
                        i += 2;
                        continue;
                    }
                    else
                    {
                        throw CreateInvalidSurrogatePairException(data[i + 1], data[i], exceptionType, 0, offset - i + 1);
                    }
                }
                throw CreateInvalidCharException(data, len, i, exceptionType);
            }
        }
        internal static void VerifyCharData(string data, ExceptionType invCharExceptionType, ExceptionType invSurrogateExceptionType)
        {
            if (data == null || data.Length == 0)
            {
                return;
            }

            int i   = 0;
            int len = data.Length;

            for (; ;)
            {
                while (i < len && s_xmlCharType.IsCharData(data[i]))
                {
                    i++;
                }
                if (i == len)
                {
                    return;
                }

                char ch = data[i];
                if (XmlCharType.IsHighSurrogate(ch))
                {
                    if (i + 1 == len)
                    {
                        throw CreateException(SR.Xml_InvalidSurrogateMissingLowChar, invSurrogateExceptionType, 0, i + 1);
                    }
                    ch = data[i + 1];
                    if (XmlCharType.IsLowSurrogate(ch))
                    {
                        i += 2;
                        continue;
                    }
                    else
                    {
                        throw CreateInvalidSurrogatePairException(data[i + 1], data[i], invSurrogateExceptionType, 0, i + 1);
                    }
                }
                throw CreateInvalidCharException(data, i, invCharExceptionType);
            }
        }
 internal int IsOnlyCharData(string str)
 {
     if (str != null)
     {
         for (int i = 0; i < str.Length; i++)
         {
             if ((charProperties[str[i]] & fCharData) == 0)
             {
                 if (i + 1 >= str.Length || !(XmlCharType.IsHighSurrogate(str[i]) && XmlCharType.IsLowSurrogate(str[i + 1])))
                 {
                     return(i);
                 }
                 else
                 {
                     i++;
                 }
             }
         }
     }
     return(-1);
 }
Example #10
0
        public void Write(string text)
        {
            if (text == null)
            {
                return;
            }

            if (_cacheAttrValue)
            {
                _attrValue.Append(text);
            }

            // scan through the string to see if there are any characters to be escaped
            int  len      = text.Length;
            int  i        = 0;
            int  startPos = 0;
            char ch       = (char)0;

            for (;;)
            {
                while (i < len && _xmlCharType.IsAttributeValueChar(ch = text[i]))
                {
                    i++;
                }

                if (i == len)
                {
                    // reached the end of the string -> write it whole out
                    _textWriter.Write(text);
                    return;
                }
                if (_inAttribute)
                {
                    if (ch == 0x9)
                    {
                        i++;
                        continue;
                    }
                }
                else
                {
                    if (ch == 0x9 || ch == 0xA || ch == 0xD || ch == '"' || ch == '\'')
                    {
                        i++;
                        continue;
                    }
                }
                // some character that needs to be escaped is found:
                break;
            }

            char[] helperBuffer = new char[256];
            for (;;)
            {
                if (startPos < i)
                {
                    WriteStringFragment(text, startPos, i - startPos, helperBuffer);
                }
                if (i == len)
                {
                    break;
                }

                switch (ch)
                {
                case (char)0x9:
                    _textWriter.Write(ch);
                    break;

                case (char)0xA:
                case (char)0xD:
                    if (_inAttribute)
                    {
                        WriteCharEntityImpl(ch);
                    }
                    else
                    {
                        _textWriter.Write(ch);
                    }
                    break;

                case '<':
                    WriteEntityRefImpl("lt");
                    break;

                case '>':
                    WriteEntityRefImpl("gt");
                    break;

                case '&':
                    WriteEntityRefImpl("amp");
                    break;

                case '\'':
                    if (_inAttribute && _quoteChar == ch)
                    {
                        WriteEntityRefImpl("apos");
                    }
                    else
                    {
                        _textWriter.Write('\'');
                    }
                    break;

                case '"':
                    if (_inAttribute && _quoteChar == ch)
                    {
                        WriteEntityRefImpl("quot");
                    }
                    else
                    {
                        _textWriter.Write('"');
                    }
                    break;

                default:
                    if (XmlCharType.IsHighSurrogate(ch))
                    {
                        if (i + 1 < len)
                        {
                            WriteSurrogateChar(text[++i], ch);
                        }
                        else
                        {
                            throw new Exception("throw XmlConvert.CreateInvalidSurrogatePairException(text[i], ch);");
                        }
                    }
                    else if (XmlCharType.IsLowSurrogate(ch))
                    {
                        throw new Exception("throw XmlConvert.CreateInvalidHighSurrogateCharException(ch);");
                    }
                    else
                    {
                        Debug.Assert((ch < 0x20 && !_xmlCharType.IsWhiteSpace(ch)) || (ch > 0xFFFD));
                        WriteCharEntityImpl(ch);
                    }
                    break;
                }
                i++;
                startPos = i;
                while (i < len && _xmlCharType.IsAttributeValueChar(ch = text[i]))
                {
                    i++;
                }
            }
        }
Example #11
0
        public void Write(char[] array, int offset, int count)
        {
            if (null == array)
            {
                throw new ArgumentNullException(nameof(array));
            }

            if (0 > offset)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            if (0 > count)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            if (count > array.Length - offset)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            if (_cacheAttrValue)
            {
                _attrValue.Append(array, offset, count);
            }

            int  endPos = offset + count;
            int  i      = offset;
            char ch     = (char)0;

            for (;;)
            {
                int startPos = i;
                //unsafe
                //{
                while (i < endPos && _xmlCharType.IsAttributeValueChar(ch = array[i]))
                {
                    i++;
                }
                //}

                if (startPos < i)
                {
                    _textWriter.Write(array, startPos, i - startPos);
                }
                if (i == endPos)
                {
                    break;
                }

                switch (ch)
                {
                case (char)0x9:
                    _textWriter.Write(ch);
                    break;

                case (char)0xA:
                case (char)0xD:
                    if (_inAttribute)
                    {
                        WriteCharEntityImpl(ch);
                    }
                    else
                    {
                        _textWriter.Write(ch);
                    }
                    break;

                case '<':
                    WriteEntityRefImpl("lt");
                    break;

                case '>':
                    WriteEntityRefImpl("gt");
                    break;

                case '&':
                    WriteEntityRefImpl("amp");
                    break;

                case '\'':
                    if (_inAttribute && _quoteChar == ch)
                    {
                        WriteEntityRefImpl("apos");
                    }
                    else
                    {
                        _textWriter.Write('\'');
                    }
                    break;

                case '"':
                    if (_inAttribute && _quoteChar == ch)
                    {
                        WriteEntityRefImpl("quot");
                    }
                    else
                    {
                        _textWriter.Write('"');
                    }
                    break;

                default:
                    if (XmlCharType.IsHighSurrogate(ch))
                    {
                        if (i + 1 < endPos)
                        {
                            WriteSurrogateChar(array[++i], ch);
                        }
                        else
                        {
                            throw new Exception("throw new ArgumentException(SR.Xml_SurrogatePairSplit);");
                        }
                    }
                    else if (XmlCharType.IsLowSurrogate(ch))
                    {
                        throw new Exception("throw XmlConvert.CreateInvalidHighSurrogateCharException(ch)");
                    }
                    else
                    {
                        Debug.Assert((ch < 0x20 && !_xmlCharType.IsWhiteSpace(ch)) || (ch > 0xFFFD));
                        WriteCharEntityImpl(ch);
                    }
                    break;
                }
                i++;
            }
        }
Example #12
0
        private static string EncodeName(string name, /*Name_not_NmToken*/ bool first, bool local)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(name);
            }

            StringBuilder bufBld       = null;
            int           length       = name.Length;
            int           copyPosition = 0;
            int           position     = 0;

            int underscorePos = name.IndexOf('_');

            MatchCollection mc = null;
            IEnumerator     en = null;

            if (underscorePos >= 0)
            {
                if (s_encodeCharPattern == null)
                {
                    s_encodeCharPattern = new Regex("(?<=_)[Xx]([0-9a-fA-F]{4}|[0-9a-fA-F]{8})_");
                }
                mc = s_encodeCharPattern.Matches(name, underscorePos);
                en = mc.GetEnumerator();
            }

            int matchPos = -1;

            if (en != null && en.MoveNext())
            {
                global::System.Text.RegularExpressions.Match m = (global::System.Text.RegularExpressions.Match)en.Current;
                matchPos = m.Index - 1;
            }
            if (first)
            {
                if ((!s_xmlCharType.IsStartNCNameCharXml4e(name[0]) && (local || (!local && name[0] != ':'))) ||
                    matchPos == 0)
                {
                    if (bufBld == null)
                    {
                        bufBld = new StringBuilder(length + 20);
                    }
                    bufBld.Append("_x");
                    if (length > 1 && XmlCharType.IsHighSurrogate(name[0]) && XmlCharType.IsLowSurrogate(name[1]))
                    {
                        int   x = name[0];
                        int   y = name[1];
                        Int32 u = XmlCharType.CombineSurrogateChar(y, x);
                        bufBld.Append(u.ToString("X8", CultureInfo.InvariantCulture));
                        position++;
                        copyPosition = 2;
                    }
                    else
                    {
                        bufBld.Append(((Int32)name[0]).ToString("X4", CultureInfo.InvariantCulture));
                        copyPosition = 1;
                    }

                    bufBld.Append('_');
                    position++;

                    if (matchPos == 0)
                    {
                        if (en.MoveNext())
                        {
                            global::System.Text.RegularExpressions.Match m = (global::System.Text.RegularExpressions.Match)en.Current;
                            matchPos = m.Index - 1;
                        }
                    }
                }
            }
            for (; position < length; position++)
            {
                if ((local && !s_xmlCharType.IsNCNameCharXml4e(name[position])) ||
                    (!local && !s_xmlCharType.IsNameCharXml4e(name[position])) ||
                    (matchPos == position))
                {
                    if (bufBld == null)
                    {
                        bufBld = new StringBuilder(length + 20);
                    }
                    if (matchPos == position)
                    {
                        if (en.MoveNext())
                        {
                            global::System.Text.RegularExpressions.Match m = (global::System.Text.RegularExpressions.Match)en.Current;
                            matchPos = m.Index - 1;
                        }
                    }

                    bufBld.Append(name, copyPosition, position - copyPosition);
                    bufBld.Append("_x");
                    if ((length > position + 1) && XmlCharType.IsHighSurrogate(name[position]) && XmlCharType.IsLowSurrogate(name[position + 1]))
                    {
                        int   x = name[position];
                        int   y = name[position + 1];
                        Int32 u = XmlCharType.CombineSurrogateChar(y, x);
                        bufBld.Append(u.ToString("X8", CultureInfo.InvariantCulture));
                        copyPosition = position + 2;
                        position++;
                    }
                    else
                    {
                        bufBld.Append(((Int32)name[position]).ToString("X4", CultureInfo.InvariantCulture));
                        copyPosition = position + 1;
                    }
                    bufBld.Append('_');
                }
            }
            if (copyPosition == 0)
            {
                return(name);
            }
            else
            {
                if (copyPosition < length)
                {
                    bufBld.Append(name, copyPosition, length - copyPosition);
                }
                return(bufBld.ToString());
            }
        }