Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <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();
        }