/// <summary> /// Initializes a new instance of the <see cref="XmlReader"/> class. /// </summary> /// <param name="buffer">An array of bytes containing the contents of a binary XML.</param> public XmlReader(byte[] buffer) { //Read header section. var binaryBuffer = new BigEndianBinaryBuffer(buffer); var signature = binaryBuffer.ReadU8(); var compressionFlag = binaryBuffer.ReadU8(); var encodingFlag = binaryBuffer.ReadU8(); var encodingFlagNot = binaryBuffer.ReadU8(); //Verify magic. if (signature != 0xA0) { throw new KbinException($"Signature was invalid. {signature} != 0xA0"); } //Encoding flag should be an inverse of the fourth byte. if ((byte)~encodingFlag != encodingFlagNot) { throw new KbinException($"Third byte was not an inverse of the fourth. {~encodingFlag} != {encodingFlagNot}"); } var compressed = compressionFlag == 0x42; var encoding = EncodingDictionary.EncodingMap[encodingFlag]; //Get buffer lengths and load. var nodeLength = binaryBuffer.ReadS32(); _nodeBuffer = new NodeBuffer(buffer.Skip(8).Take(nodeLength).ToArray(), compressed, encoding); var dataLength = BitConverter.ToInt32(buffer.Skip(nodeLength + 8).Take(4).Reverse().ToArray(), 0); _dataBuffer = new DataBuffer(buffer.Skip(nodeLength + 12).Take(dataLength).ToArray(), encoding); _xmlDocument.InsertBefore(_xmlDocument.CreateXmlDeclaration("1.0", encoding.WebName, null), _xmlDocument.DocumentElement); }
/// <summary> /// Initializes a new instance of the <see cref="XmlWriter"/> class. /// </summary> /// <param name="xmlDocument">The XML document to be wirtten as a binary XML.</param> /// <param name="encoding">The encoding of the XML document.</param> public XmlWriter(XmlDocument xmlDocument, Encoding encoding) { _xmlDocument = xmlDocument; _encoding = encoding; _nodeBuffer = new NodeBuffer(true, encoding); _dataBuffer = new DataBuffer(encoding); }
/// <summary> /// Initializes a new instance of the <see cref="KbinWriter"/> class. /// </summary> /// <param name="rawXml">The XML document to be written as a binary XML.</param> /// <param name="encoding">The encoding of the XML document.</param> public KbinWriter(string rawXml, Encoding encoding) { _rawXml = rawXml; _encoding = encoding; _nodeBuffer = new NodeBuffer(true, encoding); _dataBuffer = new DataBuffer(encoding); }
/// <summary> /// Initializes a new instance of the <see cref="KbinWriter"/> class. /// </summary> /// <param name="xNode">The XML document to be written as a binary XML.</param> /// <param name="encoding">The encoding of the XML document.</param> public KbinWriter(XNode xNode, Encoding encoding) { _document = xNode is XDocument xDoc ? xDoc : new XDocument(xNode); _encoding = encoding; _nodeBuffer = new NodeBuffer(true, encoding); _dataBuffer = new DataBuffer(encoding); }
/// <summary> /// Initializes a new instance of the <see cref="XmlWriter"/> class. /// </summary> /// <param name="xmlDocument">The XML document to be wirtten as a binary XML.</param> /// <param name="encoding">The encoding of the XML document.</param> public KbinWriter(XmlDocument document, Encoding encoding) { _document = document; _encoding = encoding; _nodeBuffer = new NodeBuffer(true, encoding); _dataBuffer = new DataBuffer(encoding); }
/// <summary> /// Initializes a new instance of the <see cref="XmlWriter"/> class. /// </summary> /// <param name="xNode">The XML document to be wirtten as a binary XML.</param> /// <param name="encoding">The encoding of the XML document.</param> public XmlWriter(XNode xNode, Encoding encoding) { _xmlDocument = new XmlDocument(); _xmlDocument.LoadXml(xNode.ToString()); _encoding = encoding; _nodeBuffer = new NodeBuffer(true, encoding); _dataBuffer = new DataBuffer(encoding); }
/// <summary> /// Initializes a new instance of the <see cref="KbinReader"/> class. /// </summary> /// <param name="buffer">An array of bytes containing the contents of a binary XML.</param> public KbinReader(byte[] buffer) { //Read header section. var binaryBuffer = new BigEndianBinaryBuffer(buffer); var signature = binaryBuffer.ReadU8(); var compressionFlag = binaryBuffer.ReadU8(); var encodingFlag = binaryBuffer.ReadU8(); var encodingFlagNot = binaryBuffer.ReadU8(); //Verify magic. if (signature != 0xA0) { throw new KbinException($"Signature was invalid. 0x{signature.ToString("X2")} != 0xA0"); } //Encoding flag should be an inverse of the fourth byte. if ((byte)~encodingFlag != encodingFlagNot) { throw new KbinException($"Third byte was not an inverse of the fourth. {~encodingFlag} != {encodingFlagNot}"); } var compressed = compressionFlag == 0x42; Encoding = EncodingDictionary.EncodingMap[encodingFlag]; //Get buffer lengths and load. var span = new Span <byte>(buffer); var nodeLength = binaryBuffer.ReadS32(); _nodeBuffer = new NodeBuffer(span.Slice(8, nodeLength).ToArray(), compressed, Encoding); var dataLength = BitConverterHelper.GetBigEndianInt32(span.Slice(nodeLength + 8, 4)); _dataBuffer = new DataBuffer(span.Slice(nodeLength + 12, dataLength).ToArray(), Encoding); var settings = new XmlWriterSettings { Async = false, Encoding = Encoding, Indent = false }; _writerStream = new MemoryStream(); _xmlWriter = XmlWriter.Create(_writerStream, settings); _xmlWriter.WriteStartDocument(); }