public int GetCharEntity(int offset, int length) { if (length < 3) { XmlExceptionHelper.ThrowInvalidCharRef(_reader); } byte[] buffer = _buffer; DiagnosticUtility.DebugAssert(buffer[offset] == '&', ""); DiagnosticUtility.DebugAssert(buffer[offset + length - 1] == ';', ""); switch (buffer[offset + 1]) { case (byte)'l': return(GetLessThanCharEntity(offset, length)); case (byte)'g': return(GetGreaterThanCharEntity(offset, length)); case (byte)'a': if (buffer[offset + 2] == (byte)'m') { return(GetAmpersandCharEntity(offset, length)); } else { return(GetApostropheCharEntity(offset, length)); } case (byte)'q': return(GetQuoteCharEntity(offset, length)); case (byte)'#': if (buffer[offset + 2] == (byte)'x') { return(GetHexCharEntity(offset, length)); } else { return(GetDecimalCharEntity(offset, length)); } default: XmlExceptionHelper.ThrowInvalidCharRef(_reader); return(0); } }
static public UInt64 ToUInt64(string value) { try { return(ulong.Parse(value, NumberStyles.Any, NumberFormatInfo.InvariantInfo)); } catch (ArgumentException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "UInt64", exception)); } catch (FormatException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "UInt64", exception)); } catch (OverflowException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "UInt64", exception)); } }
static public Guid ToGuid(string value) { try { return(new Guid(Trim(value))); } catch (FormatException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Guid", exception)); } catch (ArgumentException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Guid", exception)); } catch (OverflowException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Guid", exception)); } }
static public TimeSpan ToTimeSpan(string value) { try { return(XmlConvert.ToTimeSpan(value)); } catch (ArgumentException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "TimeSpan", exception)); } catch (FormatException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "TimeSpan", exception)); } catch (OverflowException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "TimeSpan", exception)); } }
public XmlDictionaryString GetDictionaryString(int key) { IXmlDictionary keyDictionary; if ((key & 1) != 0) { keyDictionary = _session; } else { keyDictionary = _dictionary; } XmlDictionaryString s; if (!keyDictionary.TryLookup(key >> 1, out s)) { XmlExceptionHelper.ThrowInvalidBinaryFormat(_reader); } return(s); }
public int ReadDictionaryKey() { int key = ReadMultiByteUInt31(); if ((key & 1) != 0) { if (_session == null) { XmlExceptionHelper.ThrowInvalidBinaryFormat(_reader); } int sessionKey = (key >> 1); XmlDictionaryString xmlString; if (!_session.TryLookup(sessionKey, out xmlString)) { if (sessionKey < XmlDictionaryString.MinKey || sessionKey > XmlDictionaryString.MaxKey) { XmlExceptionHelper.ThrowXmlDictionaryStringIDOutOfRange(_reader); } XmlExceptionHelper.ThrowXmlDictionaryStringIDUndefinedSession(_reader, sessionKey); } } else { if (_dictionary == null) { XmlExceptionHelper.ThrowInvalidBinaryFormat(_reader); } int staticKey = (key >> 1); XmlDictionaryString xmlString; if (!_dictionary.TryLookup(staticKey, out xmlString)) { if (staticKey < XmlDictionaryString.MinKey || staticKey > XmlDictionaryString.MaxKey) { XmlExceptionHelper.ThrowXmlDictionaryStringIDOutOfRange(_reader); } XmlExceptionHelper.ThrowXmlDictionaryStringIDUndefinedStatic(_reader, staticKey); } } return(key); }
public TimeSpan ReadTimeSpan() { long value = 0; try { value = ReadInt64(); return(TimeSpan.FromTicks(value)); } catch (ArgumentException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "TimeSpan", exception)); } catch (FormatException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "TimeSpan", exception)); } catch (OverflowException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "TimeSpan", exception)); } }
public DateTime ReadDateTime() { long value = 0; try { value = ReadInt64(); return(DateTime.FromBinary(value)); } catch (ArgumentException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "DateTime", exception)); } catch (FormatException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "DateTime", exception)); } catch (OverflowException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "DateTime", exception)); } }
private int GetHexCharEntity(int offset, int length) { byte[] buffer = _buffer; DiagnosticUtility.DebugAssert(buffer[offset + 0] == '&', ""); DiagnosticUtility.DebugAssert(buffer[offset + 1] == '#', ""); DiagnosticUtility.DebugAssert(buffer[offset + 2] == 'x', ""); DiagnosticUtility.DebugAssert(buffer[offset + length - 1] == ';', ""); int value = 0; for (int i = 3; i < length - 1; i++) { byte ch = buffer[offset + i]; int digit = 0; if (ch >= '0' && ch <= '9') { digit = (ch - '0'); } else if (ch >= 'a' && ch <= 'f') { digit = 10 + (ch - 'a'); } else if (ch >= 'A' && ch <= 'F') { digit = 10 + (ch - 'A'); } else { XmlExceptionHelper.ThrowInvalidCharRef(_reader); } DiagnosticUtility.DebugAssert(digit >= 0 && digit < 16, ""); value = value * 16 + digit; if (value > SurrogateChar.MaxValue) { XmlExceptionHelper.ThrowInvalidCharRef(_reader); } } return(value); }
private int GetDecimalCharEntity(int offset, int length) { byte[] buffer = _buffer; DiagnosticUtility.DebugAssert(buffer[offset + 0] == '&', ""); DiagnosticUtility.DebugAssert(buffer[offset + 1] == '#', ""); DiagnosticUtility.DebugAssert(buffer[offset + length - 1] == ';', ""); int value = 0; for (int i = 2; i < length - 1; i++) { byte ch = buffer[offset + i]; if (ch < (byte)'0' || ch > (byte)'9') { XmlExceptionHelper.ThrowInvalidCharRef(_reader); } value = value * 10 + (ch - '0'); if (value > SurrogateChar.MaxValue) { XmlExceptionHelper.ThrowInvalidCharRef(_reader); } } return(value); }
public void ReadValue(XmlBinaryNodeType nodeType, ValueHandle value) { switch (nodeType) { case XmlBinaryNodeType.EmptyText: value.SetValue(ValueHandleType.Empty); break; case XmlBinaryNodeType.ZeroText: value.SetValue(ValueHandleType.Zero); break; case XmlBinaryNodeType.OneText: value.SetValue(ValueHandleType.One); break; case XmlBinaryNodeType.TrueText: value.SetValue(ValueHandleType.True); break; case XmlBinaryNodeType.FalseText: value.SetValue(ValueHandleType.False); break; case XmlBinaryNodeType.BoolText: value.SetValue(ReadUInt8() != 0 ? ValueHandleType.True : ValueHandleType.False); break; case XmlBinaryNodeType.Chars8Text: ReadValue(value, ValueHandleType.UTF8, ReadUInt8()); break; case XmlBinaryNodeType.Chars16Text: ReadValue(value, ValueHandleType.UTF8, ReadUInt16()); break; case XmlBinaryNodeType.Chars32Text: ReadValue(value, ValueHandleType.UTF8, ReadUInt31()); break; case XmlBinaryNodeType.UnicodeChars8Text: ReadUnicodeValue(value, ReadUInt8()); break; case XmlBinaryNodeType.UnicodeChars16Text: ReadUnicodeValue(value, ReadUInt16()); break; case XmlBinaryNodeType.UnicodeChars32Text: ReadUnicodeValue(value, ReadUInt31()); break; case XmlBinaryNodeType.Bytes8Text: ReadValue(value, ValueHandleType.Base64, ReadUInt8()); break; case XmlBinaryNodeType.Bytes16Text: ReadValue(value, ValueHandleType.Base64, ReadUInt16()); break; case XmlBinaryNodeType.Bytes32Text: ReadValue(value, ValueHandleType.Base64, ReadUInt31()); break; case XmlBinaryNodeType.DictionaryText: value.SetDictionaryValue(ReadDictionaryKey()); break; case XmlBinaryNodeType.UniqueIdText: ReadValue(value, ValueHandleType.UniqueId, ValueHandleLength.UniqueId); break; case XmlBinaryNodeType.GuidText: ReadValue(value, ValueHandleType.Guid, ValueHandleLength.Guid); break; case XmlBinaryNodeType.DecimalText: ReadValue(value, ValueHandleType.Decimal, ValueHandleLength.Decimal); break; case XmlBinaryNodeType.Int8Text: ReadValue(value, ValueHandleType.Int8, ValueHandleLength.Int8); break; case XmlBinaryNodeType.Int16Text: ReadValue(value, ValueHandleType.Int16, ValueHandleLength.Int16); break; case XmlBinaryNodeType.Int32Text: ReadValue(value, ValueHandleType.Int32, ValueHandleLength.Int32); break; case XmlBinaryNodeType.Int64Text: ReadValue(value, ValueHandleType.Int64, ValueHandleLength.Int64); break; case XmlBinaryNodeType.UInt64Text: ReadValue(value, ValueHandleType.UInt64, ValueHandleLength.UInt64); break; case XmlBinaryNodeType.FloatText: ReadValue(value, ValueHandleType.Single, ValueHandleLength.Single); break; case XmlBinaryNodeType.DoubleText: ReadValue(value, ValueHandleType.Double, ValueHandleLength.Double); break; case XmlBinaryNodeType.TimeSpanText: ReadValue(value, ValueHandleType.TimeSpan, ValueHandleLength.TimeSpan); break; case XmlBinaryNodeType.DateTimeText: ReadValue(value, ValueHandleType.DateTime, ValueHandleLength.DateTime); break; case XmlBinaryNodeType.StartListText: ReadList(value); break; case XmlBinaryNodeType.QNameDictionaryText: ReadQName(value); break; default: XmlExceptionHelper.ThrowInvalidBinaryFormat(_reader); break; } }
public bool TryReadChars(char[] chars, int offset, int count, out int actual) { DiagnosticUtility.DebugAssert(offset + count <= chars.Length, string.Format("offset '{0}' + count '{1}' MUST BE <= chars.Length '{2}'", offset, count, chars.Length)); if (_type == ValueHandleType.Unicode) { return(TryReadUnicodeChars(chars, offset, count, out actual)); } if (_type != ValueHandleType.UTF8) { actual = 0; return(false); } int charOffset = offset; int charCount = count; byte[] bytes = _bufferReader.Buffer; int byteOffset = _offset; int byteCount = _length; bool insufficientSpaceInCharsArray = false; while (true) { while (charCount > 0 && byteCount > 0) { // fast path for codepoints U+0000 - U+007F byte b = bytes[byteOffset]; if (b >= 0x80) { break; } chars[charOffset] = (char)b; byteOffset++; byteCount--; charOffset++; charCount--; } if (charCount == 0 || byteCount == 0 || insufficientSpaceInCharsArray) { break; } int actualByteCount; int actualCharCount; UTF8Encoding encoding = new UTF8Encoding(false, true); try { // If we're asking for more than are possibly available, or more than are truly available then we can return the entire thing if (charCount >= encoding.GetMaxCharCount(byteCount) || charCount >= encoding.GetCharCount(bytes, byteOffset, byteCount)) { actualCharCount = encoding.GetChars(bytes, byteOffset, byteCount, chars, charOffset); actualByteCount = byteCount; } else { Decoder decoder = encoding.GetDecoder(); // Since x bytes can never generate more than x characters this is a safe estimate as to what will fit actualByteCount = Math.Min(charCount, byteCount); // We use a decoder so we don't error if we fall across a character boundary actualCharCount = decoder.GetChars(bytes, byteOffset, actualByteCount, chars, charOffset); // We might've gotten zero characters though if < 4 bytes were requested because // codepoints from U+0000 - U+FFFF can be up to 3 bytes in UTF-8, and represented as ONE char // codepoints from U+10000 - U+10FFFF (last Unicode codepoint representable in UTF-8) are represented by up to 4 bytes in UTF-8 // and represented as TWO chars (high+low surrogate) // (e.g. 1 char requested, 1 char in the buffer represented in 3 bytes) while (actualCharCount == 0) { // Note the by the time we arrive here, if actualByteCount == 3, the next decoder.GetChars() call will read the 4th byte // if we don't bail out since the while loop will advance actualByteCount only after reading the byte. if (actualByteCount >= 3 && charCount < 2) { // If we reach here, it means that we're: // - trying to decode more than 3 bytes and, // - there is only one char left of charCount where we're stuffing decoded characters. // In this case, we need to back off since decoding > 3 bytes in UTF-8 means that we will get 2 16-bit chars // (a high surrogate and a low surrogate) - the Decoder will attempt to provide both at once // and an ArgumentException will be thrown complaining that there's not enough space in the output char array. // actualByteCount = 0 when the while loop is broken out of; decoder goes out of scope so its state no longer matters insufficientSpaceInCharsArray = true; break; } else { DiagnosticUtility.DebugAssert(byteOffset + actualByteCount < bytes.Length, string.Format("byteOffset {0} + actualByteCount {1} MUST BE < bytes.Length {2}", byteOffset, actualByteCount, bytes.Length)); // Request a few more bytes to get at least one character actualCharCount = decoder.GetChars(bytes, byteOffset + actualByteCount, 1, chars, charOffset); actualByteCount++; } } // Now that we actually retrieved some characters, figure out how many bytes it actually was actualByteCount = encoding.GetByteCount(chars, charOffset, actualCharCount); } } catch (FormatException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateEncodingException(bytes, byteOffset, byteCount, exception)); } // Advance byteOffset += actualByteCount; byteCount -= actualByteCount; charOffset += actualCharCount; charCount -= actualCharCount; } _offset = byteOffset; _length = byteCount; actual = (count - charCount); return(true); }