diff --git a/LibreMetaverse.StructuredData/LLSD/XmlLLSD.cs b/LibreMetaverse.StructuredData/LLSD/XmlLLSD.cs index 95fab32f..ca53f055 100644 --- a/LibreMetaverse.StructuredData/LLSD/XmlLLSD.cs +++ b/LibreMetaverse.StructuredData/LLSD/XmlLLSD.cs @@ -1,5 +1,6 @@ /* * Copyright (c) 2006-2016, openmetaverse.co + * Copyright (c) 2021, Sjofn LLC. * All rights reserved. * * - Redistribution and use in source and binary forms, with or without @@ -28,7 +29,6 @@ using System; using System.Collections.Generic; using System.IO; using System.Xml; -using System.Xml.Schema; using System.Text; namespace OpenMetaverse.StructuredData @@ -38,10 +38,24 @@ namespace OpenMetaverse.StructuredData /// public static partial class OSDParser { - private static XmlSchema XmlSchema; - private static XmlTextReader XmlTextReader; - private static string LastXmlErrors = string.Empty; - private static object XmlValidationLock = new object(); + /// + /// + /// + /// + /// + public static OSD DeserializeLLSDXml(Stream xmlStream) + { + XmlReaderSettings settings = new XmlReaderSettings + { + ValidationType = ValidationType.None, + CheckCharacters = false, + IgnoreComments = true, + IgnoreProcessingInstructions = true, + DtdProcessing = DtdProcessing.Prohibit + }; + using (XmlReader xrd = XmlReader.Create(xmlStream)) + return DeserializeLLSDXml(xrd); + } /// /// @@ -50,14 +64,7 @@ namespace OpenMetaverse.StructuredData /// public static OSD DeserializeLLSDXml(byte[] xmlData) { - using(XmlTextReader xrd = new XmlTextReader(new MemoryStream(xmlData, false))) - return DeserializeLLSDXml(xrd); - } - - public static OSD DeserializeLLSDXml(Stream xmlStream) - { - using(XmlTextReader xrd = new XmlTextReader(xmlStream)) - return DeserializeLLSDXml(xrd); + return DeserializeLLSDXml(new MemoryStream(xmlData, false)); } /// @@ -68,8 +75,7 @@ namespace OpenMetaverse.StructuredData public static OSD DeserializeLLSDXml(string xmlData) { byte[] bytes = Utils.StringToBytes(xmlData); - using(XmlTextReader xrd = new XmlTextReader(new MemoryStream(bytes, false))) - return DeserializeLLSDXml(xrd); + return DeserializeLLSDXml(bytes); } /// @@ -77,7 +83,7 @@ namespace OpenMetaverse.StructuredData /// /// /// - public static OSD DeserializeLLSDXml(XmlTextReader xmlData) + public static OSD DeserializeLLSDXml(XmlReader xmlData) { try { @@ -143,7 +149,7 @@ namespace OpenMetaverse.StructuredData /// /// /// - public static void SerializeLLSDXmlElement(XmlTextWriter writer, OSD data) + public static void SerializeLLSDXmlElement(XmlWriter writer, OSD data) { switch (data.Type) { @@ -224,59 +230,12 @@ namespace OpenMetaverse.StructuredData } } - /// - /// - /// - /// - /// - /// - public static bool TryValidateLLSDXml(XmlTextReader xmlData, out string error) - { - lock (XmlValidationLock) - { - LastXmlErrors = string.Empty; - XmlTextReader = xmlData; - - CreateLLSDXmlSchema(); - - XmlReaderSettings readerSettings = new XmlReaderSettings(); - readerSettings.ValidationType = ValidationType.Schema; - readerSettings.Schemas.Add(XmlSchema); - readerSettings.ValidationEventHandler += new ValidationEventHandler(LLSDXmlSchemaValidationHandler); - - using(XmlReader reader = XmlReader.Create(xmlData, readerSettings)) - { - - try - { - while (reader.Read()) { } - } - catch (XmlException) - { - error = LastXmlErrors; - return false; - } - - if (LastXmlErrors == string.Empty) - { - error = null; - return true; - } - else - { - error = LastXmlErrors; - return false; - } - } - } - } - /// /// /// /// /// - private static OSD ParseLLSDXmlElement(XmlTextReader reader) + private static OSD ParseLLSDXmlElement(XmlReader reader) { SkipWhitespace(reader); @@ -464,7 +423,7 @@ namespace OpenMetaverse.StructuredData return ret; } - private static OSDMap ParseLLSDXmlMap(XmlTextReader reader) + private static OSDMap ParseLLSDXmlMap(XmlReader reader) { if (reader.NodeType != XmlNodeType.Element || reader.LocalName != "map") throw new NotImplementedException("Expected "); @@ -507,7 +466,7 @@ namespace OpenMetaverse.StructuredData return map; } - private static OSDArray ParseLLSDXmlArray(XmlTextReader reader) + private static OSDArray ParseLLSDXmlArray(XmlReader reader) { if (reader.NodeType != XmlNodeType.Element || reader.LocalName != "array") throw new OSDException("Expected "); @@ -539,7 +498,7 @@ namespace OpenMetaverse.StructuredData return array; } - private static void SkipWhitespace(XmlTextReader reader) + private static void SkipWhitespace(XmlReader reader) { while ( reader.NodeType == XmlNodeType.Comment || @@ -550,121 +509,5 @@ namespace OpenMetaverse.StructuredData reader.Read(); } } - - private static void CreateLLSDXmlSchema() - { - if (XmlSchema == null) - { - #region XSD - const string schemaText = @" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"; - #endregion XSD - - MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(schemaText)); - - XmlSchema = new XmlSchema(); - XmlSchema = XmlSchema.Read(stream, new ValidationEventHandler(LLSDXmlSchemaValidationHandler)); - } - } - - private static void LLSDXmlSchemaValidationHandler(object sender, ValidationEventArgs args) - { - string error = - $"Line: {XmlTextReader.LineNumber} - Position: {XmlTextReader.LinePosition} - {args.Message}"; - - if (LastXmlErrors == string.Empty) - LastXmlErrors = error; - else - LastXmlErrors += Environment.NewLine + error; - } } }