private string GetCharText()
 {
     int num = this.GetChar();
     if (num > 0xffff)
     {
         SurrogateChar ch = new SurrogateChar(num);
         return new string(new char[] { ch.HighChar, ch.LowChar }, 0, 2);
     }
     char ch2 = (char) num;
     return ch2.ToString();
 }
        void ReadEscapedCharacter(bool moveToText)
        {
            BufferReader.SkipByte();
            char ch = (char)BufferReader.GetByte();
            if (ch == 'u')
            {
                BufferReader.SkipByte();
                int offset;
                byte[] buffer = BufferReader.GetBuffer(5, out offset);
                string bufferAsString = Encoding.UTF8.GetString(buffer, offset, 4);
                BufferReader.Advance(4);
                int charValue = ParseChar(bufferAsString, NumberStyles.HexNumber);
                if (Char.IsHighSurrogate((char)charValue))
                {
                    byte nextByte = BufferReader.GetByte();
                    if (nextByte == (byte)'\\')
                    {
                        BufferReader.SkipByte();
                        SkipExpectedByteInBufferReader((byte)'u');
                        buffer = BufferReader.GetBuffer(5, out offset);
                        bufferAsString = Encoding.UTF8.GetString(buffer, offset, 4);
                        BufferReader.Advance(4);
                        char lowChar = ParseChar(bufferAsString, NumberStyles.HexNumber);
                        if (!Char.IsLowSurrogate(lowChar))
                        {
                            XmlExceptionHelper.ThrowXmlException(this,
                                new XmlException(System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.XmlInvalidLowSurrogate, bufferAsString)));
                        }
                        charValue = new SurrogateChar(lowChar, (char)charValue).Char;
                    }
                }

                if (buffer[offset + 4] == JsonGlobals.QuoteByte)
                {
                    BufferReader.SkipByte();
                    if (moveToText)
                    {
                        MoveToAtomicText().Value.SetCharValue(charValue);
                    }
                    complexTextMode = JsonComplexTextMode.None;
                }
                else
                {
                    if (moveToText)
                    {
                        MoveToComplexText().Value.SetCharValue(charValue);
                    }
                    complexTextMode = JsonComplexTextMode.QuotedText;
                }
            }
            else
            {
                switch (ch)
                {
                    case 'b':
                        ch = '\b';
                        break;
                    case 'f':
                        ch = '\f';
                        break;
                    case 'n':
                        ch = '\n';
                        break;
                    case 'r':
                        ch = '\r';
                        break;
                    case 't':
                        ch = '\t';
                        break;
                    case '\"':
                    case '\\':
                    case '/':
                        // Do nothing. These are the actual unescaped values.
                        break;
                    default:
                        XmlExceptionHelper.ThrowXmlException(this,
                            new XmlException(SR.GetString(SR.JsonEncounteredUnexpectedCharacter, (char)ch)));
                        break;
                }
                BufferReader.SkipByte();
                if (BufferReader.GetByte() == JsonGlobals.QuoteByte)
                {
                    BufferReader.SkipByte();
                    if (moveToText)
                    {
                        MoveToAtomicText().Value.SetCharValue(ch);
                    }
                    complexTextMode = JsonComplexTextMode.None;
                }
                else
                {
                    if (moveToText)
                    {
                        MoveToComplexText().Value.SetCharValue(ch);
                    }
                    complexTextMode = JsonComplexTextMode.QuotedText;
                }
            }
        }
 unsafe protected void WriteUTF8Char(int ch)
 {
     if (ch < 0x80)
     {
         WriteByte((byte)ch);
     }
     else if (ch <= char.MaxValue)
     {
         char* chars = stackalloc char[1];
         chars[0] = (char)ch;
         UnsafeWriteUTF8Chars(chars, 1);
     }
     else
     {
         SurrogateChar surrogateChar = new SurrogateChar(ch);
         char* chars = stackalloc char[2];
         chars[0] = surrogateChar.HighChar;
         chars[1] = surrogateChar.LowChar;
         UnsafeWriteUTF8Chars(chars, 2);
     }
 }
 string GetCharText()
 {
     int ch = GetChar();
     if (ch > char.MaxValue)
     {
         SurrogateChar surrogate = new SurrogateChar(ch);
         char[] chars = new char[2];
         chars[0] = surrogate.HighChar;
         chars[1] = surrogate.LowChar;
         return new string(chars, 0, 2);
     }
     else
     {
         return ((char)ch).ToString();
     }
 }
 protected unsafe void WriteUTF8Char(int ch)
 {
     if (ch < 0x80)
     {
         this.WriteByte((byte) ch);
     }
     else if (ch <= 0xffff)
     {
         char* chars = (char*) stackalloc byte[(((IntPtr) 1) * 2)];
         chars[0] = (char) ch;
         this.UnsafeWriteUTF8Chars(chars, 1);
     }
     else
     {
         SurrogateChar ch2 = new SurrogateChar(ch);
         char* chPtr2 = (char*) stackalloc byte[(((IntPtr) 2) * 2)];
         chPtr2[0] = ch2.HighChar;
         chPtr2[1] = ch2.LowChar;
         this.UnsafeWriteUTF8Chars(chPtr2, 2);
     }
 }
Exemple #6
0
        public override void WriteSurrogateCharEntity(char lowChar, char highChar)
        {
            if (IsClosed)
                ThrowClosed();

            SurrogateChar ch = new SurrogateChar(lowChar, highChar);

            if (_attributeValue != null)
            {
                char[] chars = new char[2] { highChar, lowChar };
                WriteAttributeText(new string(chars));
            }

            if (!_isXmlnsAttribute)
            {
                StartContent();
                FlushBase64();
                _writer.WriteCharEntity(ch.Char);
                EndContent();
            }
        }
        public int GetEscapedChars(int offset, int length, char[] chars)
        {
            byte[] buffer = this.buffer;
            int charCount = 0;
            int textOffset = offset;
            int offsetMax = offset + length;
            while (true)
            {
                while (offset < offsetMax && IsAttrChar(buffer[offset]))
                    offset++;
                charCount += GetChars(textOffset, offset - textOffset, chars, charCount);
                if (offset == offsetMax)
                    break;
                textOffset = offset;
                if (buffer[offset] == '&')
                {
                    while (offset < offsetMax && buffer[offset] != ';')
                        offset++;
                    offset++;
                    int ch = GetCharEntity(textOffset, offset - textOffset);
                    textOffset = offset;
                    if (ch > char.MaxValue)
                    {
                        SurrogateChar surrogate = new SurrogateChar(ch);
                        chars[charCount++] = surrogate.HighChar;
                        chars[charCount++] = surrogate.LowChar;
                    }
                    else
                    {
                        chars[charCount++] = (char)ch;
                    }
                }
                else if (buffer[offset] == '\n' || buffer[offset] == '\t')
                {
                    chars[charCount++] = ' ';
                    offset++;
                    textOffset = offset;
                }
                else // '\r'
                {
                    chars[charCount++] = ' ';
                    offset++;

                    if (offset < offsetMax && buffer[offset] == '\n')
                        offset++;

                    textOffset = offset;
                }

            }
            return charCount;
        }
 private void ReadEscapedCharacter(bool moveToText)
 {
     this.BufferReader.SkipByte();
     char ch1 = (char)this.BufferReader.GetByte();
     if ((int)ch1 == 117)
     {
         this.BufferReader.SkipByte();
         int offset;
         byte[] buffer = this.BufferReader.GetBuffer(5, out offset);
         string string1 = Encoding.UTF8.GetString(buffer, offset, 4);
         this.BufferReader.Advance(4);
         int ch2 = (int)XmlJsonReader.ParseChar(string1, NumberStyles.HexNumber);
         if (char.IsHighSurrogate((char)ch2) && (int)this.BufferReader.GetByte() == 92)
         {
             this.BufferReader.SkipByte();
             this.SkipExpectedByteInBufferReader((byte)117);
             buffer = this.BufferReader.GetBuffer(5, out offset);
             string string2 = Encoding.UTF8.GetString(buffer, offset, 4);
             this.BufferReader.Advance(4);
             char ch3 = XmlJsonReader.ParseChar(string2, NumberStyles.HexNumber);
             if (!char.IsLowSurrogate(ch3))
                 XmlExceptionHelper.ThrowXmlException((XmlDictionaryReader)this, new XmlException(System.Runtime.Serialization.SR.GetString("XmlInvalidLowSurrogate", new object[1]
     {
       (object) string2
     })));
             ch2 = new SurrogateChar(ch3, (char)ch2).Char;
         }
         if ((int)buffer[offset + 4] == 34)
         {
             this.BufferReader.SkipByte();
             if (moveToText)
                 this.MoveToAtomicText().Value.SetCharValue(ch2);
             this.complexTextMode = XmlJsonReader.JsonComplexTextMode.None;
         }
         else
         {
             if (moveToText)
                 this.MoveToComplexText().Value.SetCharValue(ch2);
             this.complexTextMode = XmlJsonReader.JsonComplexTextMode.QuotedText;
         }
     }
     else
     {
         switch (ch1)
         {
             case 'n':
                 ch1 = '\n';
                 goto case '"';
             case 'r':
                 ch1 = '\r';
                 goto case '"';
             case 't':
                 ch1 = '\t';
                 goto case '"';
             case 'b':
                 ch1 = '\b';
                 goto case '"';
             case 'f':
                 ch1 = '\f';
                 goto case '"';
             case '"':
             case '/':
             case '\\':
                 this.BufferReader.SkipByte();
                 if ((int)this.BufferReader.GetByte() == 34)
                 {
                     this.BufferReader.SkipByte();
                     if (moveToText)
                         this.MoveToAtomicText().Value.SetCharValue((int)ch1);
                     this.complexTextMode = XmlJsonReader.JsonComplexTextMode.None;
                     break;
                 }
                 else
                 {
                     if (moveToText)
                         this.MoveToComplexText().Value.SetCharValue((int)ch1);
                     this.complexTextMode = XmlJsonReader.JsonComplexTextMode.QuotedText;
                     break;
                 }
             default:
                 // ISSUE: reference to a compiler-generated method
                 XmlExceptionHelper.ThrowXmlException((XmlDictionaryReader)this, new XmlException(SR2.GetString(SR2.JsonEncounteredUnexpectedCharacter, new object[1]
     {
       (object) ch1
     })));
                 goto case '"';
         }
     }
 }
Exemple #9
0
 public override void WriteCharEntity(int ch)
 {
     if (ch > char.MaxValue)
     {
         SurrogateChar sch = new SurrogateChar(ch);
         char[] chars = new char[2] { sch.HighChar, sch.LowChar, };
         WriteText(chars, 0, 2);
     }
     else
     {
         char[] chars = new char[1] { (char)ch };
         WriteText(chars, 0, 1);
     }
 }
 public int GetEscapedChars(int offset, int length, char[] chars)
 {
     byte[] buffer = this.buffer;
     int charOffset = 0;
     int num2 = offset;
     int num3 = offset + length;
     while (true)
     {
         while ((offset < num3) && this.IsAttrChar(buffer[offset]))
         {
             offset++;
         }
         charOffset += this.GetChars(num2, offset - num2, chars, charOffset);
         if (offset == num3)
         {
             return charOffset;
         }
         num2 = offset;
         if (buffer[offset] == 0x26)
         {
             while ((offset < num3) && (buffer[offset] != 0x3b))
             {
                 offset++;
             }
             offset++;
             int charEntity = this.GetCharEntity(num2, offset - num2);
             num2 = offset;
             if (charEntity > 0xffff)
             {
                 SurrogateChar ch = new SurrogateChar(charEntity);
                 chars[charOffset++] = ch.HighChar;
                 chars[charOffset++] = ch.LowChar;
             }
             else
             {
                 chars[charOffset++] = (char) charEntity;
             }
         }
         else if ((buffer[offset] == 10) || (buffer[offset] == 9))
         {
             chars[charOffset++] = ' ';
             offset++;
             num2 = offset;
         }
         else
         {
             chars[charOffset++] = ' ';
             offset++;
             if ((offset < num3) && (buffer[offset] == 10))
             {
                 offset++;
             }
             num2 = offset;
         }
     }
 }
 public override void WriteSurrogateCharEntity(char lowChar, char highChar)
 {
     if (this.IsClosed)
     {
         this.ThrowClosed();
     }
     SurrogateChar ch = new SurrogateChar(lowChar, highChar);
     if (this.attributeValue != null)
     {
         char[] chArray = new char[] { highChar, lowChar };
         this.WriteAttributeText(new string(chArray));
     }
     if (!this.isXmlnsAttribute)
     {
         this.StartContent();
         this.FlushBase64();
         this.writer.WriteCharEntity(ch.Char);
         this.EndContent();
     }
 }
 public override void WriteCharEntity(int ch)
 {
     if (ch > 0xffff)
     {
         SurrogateChar ch2 = new SurrogateChar(ch);
         char[] chars = new char[] { ch2.HighChar, ch2.LowChar };
         this.WriteText(chars, 0, 2);
     }
     else
     {
         char[] chArray2 = new char[] { (char) ch };
         this.WriteText(chArray2, 0, 1);
     }
 }
        private void ReadEscapedCharacter(bool moveToText)
        {
            base.BufferReader.SkipByte();
            char @byte = (char) base.BufferReader.GetByte();
            switch (@byte)
            {
                case '"':
                case '/':
                case '\\':
                    break;

                case 'r':
                    @byte = '\r';
                    break;

                case 't':
                    @byte = '\t';
                    break;

                case 'n':
                    @byte = '\n';
                    break;

                case 'b':
                    @byte = '\b';
                    break;

                case 'f':
                    @byte = '\f';
                    break;

                case 'u':
                {
                    int num;
                    base.BufferReader.SkipByte();
                    byte[] bytes = base.BufferReader.GetBuffer(5, out num);
                    string str = Encoding.UTF8.GetString(bytes, num, 4);
                    base.BufferReader.Advance(4);
                    int ch = ParseChar(str, NumberStyles.HexNumber);
                    if (char.IsHighSurrogate((char) ch) && (base.BufferReader.GetByte() == 0x5c))
                    {
                        base.BufferReader.SkipByte();
                        this.SkipExpectedByteInBufferReader(0x75);
                        bytes = base.BufferReader.GetBuffer(5, out num);
                        str = Encoding.UTF8.GetString(bytes, num, 4);
                        base.BufferReader.Advance(4);
                        char c = ParseChar(str, NumberStyles.HexNumber);
                        if (!char.IsLowSurrogate(c))
                        {
                            XmlExceptionHelper.ThrowXmlException(this, new XmlException(System.Runtime.Serialization.SR.GetString("XmlInvalidLowSurrogate", new object[] { str })));
                        }
                        SurrogateChar ch3 = new SurrogateChar(c, (char) ch);
                        ch = ch3.Char;
                    }
                    if (bytes[num + 4] == 0x22)
                    {
                        base.BufferReader.SkipByte();
                        if (moveToText)
                        {
                            base.MoveToAtomicText().Value.SetCharValue(ch);
                        }
                        this.complexTextMode = JsonComplexTextMode.None;
                        return;
                    }
                    if (moveToText)
                    {
                        base.MoveToComplexText().Value.SetCharValue(ch);
                    }
                    this.complexTextMode = JsonComplexTextMode.QuotedText;
                    return;
                }
                default:
                    XmlExceptionHelper.ThrowXmlException(this, new XmlException(System.Runtime.Serialization.SR.GetString("JsonEncounteredUnexpectedCharacter", new object[] { @byte })));
                    break;
            }
            base.BufferReader.SkipByte();
            if (base.BufferReader.GetByte() == 0x22)
            {
                base.BufferReader.SkipByte();
                if (moveToText)
                {
                    base.MoveToAtomicText().Value.SetCharValue(@byte);
                }
                this.complexTextMode = JsonComplexTextMode.None;
            }
            else
            {
                if (moveToText)
                {
                    base.MoveToComplexText().Value.SetCharValue(@byte);
                }
                this.complexTextMode = JsonComplexTextMode.QuotedText;
            }
        }