using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; using VersOne.Epub.Options; namespace VersOne.Epub.Internal { internal static class XmlUtils { private const string XML_DECLARATION_FIRST_CHARS = " LoadDocumentAsync(Stream stream, XmlReaderOptions? xmlReaderOptions = null) { using MemoryStream memoryStream = new(); await stream.CopyToAsync(memoryStream).ConfigureAwait(false); memoryStream.Position = 0; if (xmlReaderOptions != null && xmlReaderOptions.SkipXmlHeaders) { SkipXmlHeader(memoryStream); } XmlReaderSettings xmlReaderSettings = new() { DtdProcessing = DtdProcessing.Ignore, Async = true }; using XmlReader xmlReader = XmlReader.Create(memoryStream, xmlReaderSettings); return await Task.Run(() => XDocument.Load(xmlReader)).ConfigureAwait(false); } // This is a workaround for an issue that XML 1.1 files are not supported in .NET. // See https://github.com/vers-one/EpubReader/issues/34 for more details. // This method checks if the stream content starts with an XML declaration ( buffer, string pattern, int startPosition) { for (int i = 0; i < pattern.Length; i++) { if (buffer[startPosition + i] != pattern[i]) { return false; } } return true; } int IndexOf(IList buffer, string pattern, int startPosition) { for (int i = startPosition; i < buffer.Count - pattern.Length; i++) { if (IsMatch(buffer, pattern, i)) { return i; } } return -1; } memoryStream.TryGetBuffer(out ArraySegment memoryStreamBuffer); int position = IndexOf(memoryStreamBuffer, XML_DECLARATION_FIRST_CHARS, 0); if (position == -1) { return; } position = IndexOf(memoryStreamBuffer, "<", position + XML_DECLARATION_FIRST_CHARS.Length); if (position == -1) { return; } memoryStream.Position = position; } } }