CreateInvalidSurrogatePairException() static private method

static private CreateInvalidSurrogatePairException ( char low, char hi ) : Exception
low char
hi char
return System.Exception
Example #1
0
        private static unsafe byte *EncodeSurrogate(char *pSrc, char *pSrcEnd, byte *pDst)
        {
            int highChar = pSrc[0];

            if (highChar > 0xdbff)
            {
                throw XmlConvert.CreateInvalidHighSurrogateCharException((char)highChar);
            }
            if ((pSrc + 1) >= pSrcEnd)
            {
                throw new ArgumentException(Res.GetString("Xml_InvalidSurrogateMissingLowChar"));
            }
            int lowChar = pSrc[1];

            if (lowChar < 0xdc00)
            {
                throw XmlConvert.CreateInvalidSurrogatePairException((char)lowChar, (char)highChar);
            }
            highChar = XmlCharType.CombineSurrogateChar(lowChar, highChar);
            pDst[0]  = (byte)(240 | (highChar >> 0x12));
            pDst[1]  = (byte)(0x80 | ((highChar >> 12) & 0x3f));
            pDst[2]  = (byte)(0x80 | ((highChar >> 6) & 0x3f));
            pDst[3]  = (byte)(0x80 | (highChar & 0x3f));
            pDst    += 4;
            return(pDst);
        }
        public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index)
        {
            // check input surrogate pair
            if (!char.IsSurrogatePair(charUnknownHigh, charUnknownLow))
            {
                throw XmlConvert.CreateInvalidSurrogatePairException(charUnknownHigh, charUnknownLow);
            }

            // If we are already in fallback, throw, it's probably at the suspect character in charEntity
            if (_charEntityIndex >= 0)
            {
                (new EncoderExceptionFallback()).CreateFallbackBuffer().Fallback(charUnknownHigh, charUnknownLow, index);
            }

            if (_parent.CanReplaceAt(index))
            {
                // Create the replacement character entity
                _charEntity      = string.Format(CultureInfo.InvariantCulture, "&#x{0:X};", new object[] { SurrogateCharToUtf32(charUnknownHigh, charUnknownLow) });
                _charEntityIndex = 0;
                return(true);
            }
            else
            {
                EncoderFallbackBuffer errorFallbackBuffer = (new EncoderExceptionFallback()).CreateFallbackBuffer();
                errorFallbackBuffer.Fallback(charUnknownHigh, charUnknownLow, index);
                return(false);
            }
        }
Example #3
0
        public override async Task WriteSurrogateCharEntityAsync(char lowChar, char highChar)
        {
            try
            {
                if (!Char.IsSurrogatePair(highChar, lowChar))
                {
                    throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, highChar);
                }

                await AdvanceStateAsync(Token.Text).ConfigureAwait(false);

                if (SaveAttrValue)
                {
                    _attrValueCache.WriteSurrogateCharEntity(lowChar, highChar);
                }
                else
                {
                    await _writer.WriteSurrogateCharEntityAsync(lowChar, highChar).ConfigureAwait(false);
                }
            }
            catch
            {
                _currentState = State.Error;
                throw;
            }
        }
Example #4
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(Res.GetString(Res.Xml_InvalidSurrogateMissingLowChar));
                }
                else if (XmlCharType.IsLowSurrogate(ch))
                {
                    throw XmlConvert.CreateInvalidHighSurrogateCharException(ch);
                }
                else
                {
                    i++;
                }
            }

            textWriter.Write(text);
            return;
        }
Example #5
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;

            while (true)
            {
                unsafe
                {
                    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 XmlConvert.CreateInvalidSurrogatePairException(lowChar, ch);
                        }
                    }
                    throw new ArgumentException(SR.Xml_InvalidSurrogateMissingLowChar);
                }
                else if (XmlCharType.IsLowSurrogate(ch))
                {
                    throw XmlConvert.CreateInvalidHighSurrogateCharException(ch);
                }
                else
                {
                    i++;
                }
            }

            _textWriter.Write(text);
            return;
        }
Example #6
0
 internal void WriteSurrogateChar(char lowChar, char highChar)
 {
     if (!XmlCharType.IsLowSurrogate(lowChar) || !XmlCharType.IsHighSurrogate(highChar))
     {
         throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, highChar);
     }
     this.textWriter.Write(highChar);
     this.textWriter.Write(lowChar);
 }
        internal void WriteSurrogateChar(char lowChar, char highChar)
        {
            if (((int)lowChar < SurLowStart || (int)lowChar > SurLowEnd) ||
                ((int)highChar < SurHighStart || (int)highChar > SurHighEnd))
            {
                throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, highChar);
            }

            textWriter.Write(highChar);
            textWriter.Write(lowChar);
        }
Example #8
0
        internal void WriteSurrogateCharEntity(char lowChar, char highChar)
        {
            if (!XmlCharType.IsLowSurrogate(lowChar) || !XmlCharType.IsHighSurrogate(highChar))
            {
                throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, highChar);
            }
            int num = XmlCharType.CombineSurrogateChar(lowChar, highChar);

            if (this.cacheAttrValue)
            {
                this.attrValue.Append(highChar);
                this.attrValue.Append(lowChar);
            }
            this.textWriter.Write("&#x");
            this.textWriter.Write(num.ToString("X", NumberFormatInfo.InvariantInfo));
            this.textWriter.Write(';');
        }
Example #9
0
        internal unsafe void WriteRawWithSurrogateChecking(string text)
        {
            if (text == null)
            {
                return;
            }
            if (this.cacheAttrValue)
            {
                this.attrValue.Append(text);
            }
            int  length = text.Length;
            int  num2   = 0;
            char ch     = '\0';

Label_002A:
            while ((num2 < length) && (((this.xmlCharType.charProperties[ch = text[num2]] & 0x10) != 0) || (ch < ' ')))
            {
                num2++;
            }
            if (num2 != length)
            {
                if (XmlCharType.IsHighSurrogate(ch))
                {
                    if ((num2 + 1) >= length)
                    {
                        throw new ArgumentException(Res.GetString("Xml_InvalidSurrogateMissingLowChar"));
                    }
                    char ch2 = text[num2 + 1];
                    if (!XmlCharType.IsLowSurrogate(ch2))
                    {
                        throw XmlConvert.CreateInvalidSurrogatePairException(ch2, ch);
                    }
                    num2 += 2;
                }
                else
                {
                    if (XmlCharType.IsLowSurrogate(ch))
                    {
                        throw XmlConvert.CreateInvalidHighSurrogateCharException(ch);
                    }
                    num2++;
                }
                goto Label_002A;
            }
            this.textWriter.Write(text);
        }
 public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index)
 {
     if (!char.IsSurrogatePair(charUnknownHigh, charUnknownLow))
     {
         throw XmlConvert.CreateInvalidSurrogatePairException(charUnknownHigh, charUnknownLow);
     }
     if (this.charEntityIndex >= 0)
     {
         new EncoderExceptionFallback().CreateFallbackBuffer().Fallback(charUnknownHigh, charUnknownLow, index);
     }
     if (this.parent.CanReplaceAt(index))
     {
         this.charEntity      = string.Format(CultureInfo.InvariantCulture, "&#x{0:X};", new object[] { this.SurrogateCharToUtf32(charUnknownHigh, charUnknownLow) });
         this.charEntityIndex = 0;
         return(true);
     }
     new EncoderExceptionFallback().CreateFallbackBuffer().Fallback(charUnknownHigh, charUnknownLow, index);
     return(false);
 }
        internal void WriteSurrogateCharEntity(char lowChar, char highChar)
        {
            if (((int)lowChar < SurLowStart || (int)lowChar > SurLowEnd) ||
                ((int)highChar < SurHighStart || (int)highChar > SurHighEnd))
            {
                throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, highChar);
            }
            int surrogateChar = ((int)lowChar - SurLowStart) | (((int)highChar - SurHighStart) << 10) + 0x10000;

            if (cacheAttrValue)
            {
                attrValue.Append(highChar);
                attrValue.Append(lowChar);
            }

            textWriter.Write("&#x");
            textWriter.Write(surrogateChar.ToString("X", NumberFormatInfo.InvariantInfo));
            textWriter.Write(';');
        }
Example #12
0
        internal void WriteSurrogateCharEntity(char lowChar, char highChar)
        {
            if (!XmlCharType.IsLowSurrogate(lowChar) ||
                !XmlCharType.IsHighSurrogate(highChar))
            {
                throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, highChar);
            }
            int surrogateChar = XmlCharType.CombineSurrogateChar(lowChar, highChar);

            if (_cacheAttrValue)
            {
                Debug.Assert(_attrValue != null);
                _attrValue.Append(highChar);
                _attrValue.Append(lowChar);
            }

            _textWriter.Write("&#x");
            _textWriter.Write(surrogateChar.ToString("X", NumberFormatInfo.InvariantInfo));
            _textWriter.Write(';');
        }
Example #13
0
        internal 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;

            while (true)
            {
                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];
            while (true)
            {
                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 XmlConvert.CreateInvalidSurrogatePairException(text[i], ch);
                        }
                    }
                    else if (XmlCharType.IsLowSurrogate(ch))
                    {
                        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 #14
0
        internal unsafe void Write(string text)
        {
            char[] chArray;
            if (text == null)
            {
                return;
            }
            if (this.cacheAttrValue)
            {
                this.attrValue.Append(text);
            }
            int  length = text.Length;
            int  num2   = 0;
            int  offset = 0;
            char ch     = '\0';

Label_002C:
            while ((num2 < length) && ((this.xmlCharType.charProperties[ch = text[num2]] & 0x80) != 0))
            {
                num2++;
            }
            if (num2 == length)
            {
                this.textWriter.Write(text);
                return;
            }
            if (this.inAttribute)
            {
                if (ch != '\t')
                {
                    goto Label_0091;
                }
                num2++;
                goto Label_002C;
            }
            if (((ch == '\t') || (ch == '\n')) || (((ch == '\r') || (ch == '"')) || (ch == '\'')))
            {
                num2++;
                goto Label_002C;
            }
Label_0091:
            chArray = new char[0x100];
            while (true)
            {
                if (offset < num2)
                {
                    this.WriteStringFragment(text, offset, num2 - offset, chArray);
                }
                if (num2 == length)
                {
                    return;
                }
                switch (ch)
                {
                case '\t':
                    this.textWriter.Write(ch);
                    break;

                case '\n':
                case '\r':
                    if (!this.inAttribute)
                    {
                        this.textWriter.Write(ch);
                    }
                    else
                    {
                        this.WriteCharEntityImpl(ch);
                    }
                    break;

                case '"':
                    if (this.inAttribute && (this.quoteChar == ch))
                    {
                        this.WriteEntityRefImpl("quot");
                    }
                    else
                    {
                        this.textWriter.Write('"');
                    }
                    break;

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

                case '\'':
                    if (!this.inAttribute || (this.quoteChar != ch))
                    {
                        this.textWriter.Write('\'');
                    }
                    else
                    {
                        this.WriteEntityRefImpl("apos");
                    }
                    break;

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

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

                default:
                    if (XmlCharType.IsHighSurrogate(ch))
                    {
                        if ((num2 + 1) >= length)
                        {
                            throw XmlConvert.CreateInvalidSurrogatePairException(text[num2], ch);
                        }
                        this.WriteSurrogateChar(text[++num2], ch);
                    }
                    else
                    {
                        if (XmlCharType.IsLowSurrogate(ch))
                        {
                            throw XmlConvert.CreateInvalidHighSurrogateCharException(ch);
                        }
                        this.WriteCharEntityImpl(ch);
                    }
                    break;
                }
                num2++;
                offset = num2;
                while ((num2 < length) && ((this.xmlCharType.charProperties[ch = text[num2]] & 0x80) != 0))
                {
                    num2++;
                }
            }
        }