public void CanWriteEmptyFiles()
        {
            byte[] expected =
            {
                0xF9, 0x7D, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            };
            var expectedStream = DataStreamFactory.FromArray(expected, 0, expected.Length);

            var writer     = new StandardBinWriter();
            var parameters = new WriterParameters {
                BlockSize = 0x20
            };

            writer.Initialize(parameters);

            var container = new NodeContainerFormat();
            var node      = NodeFactory.FromMemory("empty");

            node.Tags["Type"]  = FileType.Empty;
            node.Tags["Index"] = 1;
            container.Root.Add(node);

            var binaryFormat = writer.Convert(container);

            Assert.AreEqual(expected.Length, binaryFormat.Stream.Length);
            Assert.IsTrue(binaryFormat.Stream.Compare(expectedStream));
        }
        public void CanSetFileNames()
        {
            var reader     = new StandardBinReader();
            var parameters = new ReaderParameters
            {
                Endianness = EndiannessMode.LittleEndian,
                FileNames  = new[] { "test/file1" },
            };

            reader.Initialize(parameters);

            byte[] data =
            {
                0xF9, 0x7D, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D,
            };
            using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length);
            var format = new BinaryFormat(stream);

            var result = reader.Convert(format);

            Assert.AreEqual(1, result.Root.Children.Count);
            Assert.AreEqual("test", result.Root.Children[0].Name);
            Assert.AreEqual(1, result.Root.Children[0].Children.Count);
            Assert.AreEqual(FileType.Normal, result.Root.Children[0].Children[0].Tags["Type"]);
            Assert.AreEqual("file1", result.Root.Children[0].Children[0].Name);
        }
        public void CanReadDummyFiles()
        {
            var reader     = new StandardBinReader();
            var parameters = new ReaderParameters
            {
                Endianness = EndiannessMode.BigEndian,
                FileNames  = Array.Empty <string>(),
            };

            reader.Initialize(parameters);

            byte[] data =
            {
                0x00, 0x07, 0x7D, 0xF9, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x70, 0x83, 0x5F, 0x83, 0x7E,
                0x83, 0x5F, 0x83, 0x7E, 0x81, 0x5B, 0x82, 0xC5, 0x82, 0xB7, 0x2E, 0x0D, 0x0A, 0x90, 0xB3, 0x8E,
                0xAE, 0x82, 0xC8, 0x83, 0x66, 0x81, 0x5B, 0x83, 0x5E, 0x82, 0xAA, 0x93, 0xFC, 0x82, 0xE9, 0x82,
                0xDC, 0x82, 0xC5, 0x81, 0x41, 0x82, 0xD0, 0x82, 0xC6, 0x82, 0xDC, 0x82, 0xB8, 0x83, 0x8A, 0x83,
                0x93, 0x83, 0x4E, 0x83, 0x66, 0x81, 0x5B, 0x83, 0x5E, 0x82, 0xF0, 0x8D, 0xEC, 0x90, 0xAC, 0x82,
                0xB7, 0x82, 0xE9, 0x82, 0xBD, 0x82, 0xDF, 0x82, 0xCC, 0x83, 0x5F, 0x83, 0x7E, 0x81, 0x5B, 0x83,
                0x74, 0x83, 0x40, 0x83, 0x43, 0x83, 0x8B, 0x82, 0xC6, 0x82, 0xB5, 0x82, 0xC4, 0x8D, 0xEC, 0x90,
                0xAC, 0x82, 0xB3, 0x82, 0xEA, 0x82, 0xC4, 0x82, 0xA2, 0x82, 0xDC, 0x82, 0xB7, 0x2E, 0x0D, 0x0A,
            };
            using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length);
            var format = new BinaryFormat(stream);

            var result = reader.Convert(format);

            Assert.AreEqual(1, result.Root.Children.Count);
            Assert.AreEqual(FileType.Dummy, result.Root.Children[0].Tags["Type"]);
        }
Example #4
0
        public void CanReadFiles()
        {
            var reader = new DlcBinReader();

            byte[] data =
            {
                0x64, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D,
            };
            using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length);
            var format = new BinaryFormat(stream);

            var result = reader.Convert(format);

            Assert.AreEqual(1, result.Root.Children.Count);
            Assert.AreEqual(0x10, result.Root.Children[0].Stream.Length);
        }
Example #5
0
        public void BadCompressedFilesThrowsException()
        {
            byte[] data =
            {
                0x01, 0x02, 0x00, 0x00, 0x31, 0x01, 0x00, 0x00, 0x78, 0xDA, 0x4D, 0x91, 0xCB, 0x6D, 0xC4, 0x30,
                0x0C, 0x44, 0x5B, 0x99, 0x02, 0x8C, 0xED, 0x21, 0xF7, 0x24, 0xC8, 0x25, 0x05, 0xD0, 0x32, 0xE3,
                0x25, 0x40, 0x49, 0x0E, 0x3F, 0x8B, 0x94, 0x1F, 0x7A, 0xF3, 0xBD, 0x48, 0x10, 0x35, 0x22, 0xE7,
                0x8D, 0x1E, 0xA7, 0x71, 0x87, 0x1C, 0x9E, 0x1D, 0xDB, 0xD4, 0x69, 0x70, 0x09, 0x50, 0xE7, 0x58,
                0xD0, 0xE6, 0x70, 0x6E, 0xC1, 0x91, 0x06, 0xDA, 0xE4, 0x10, 0x6F, 0x32, 0x76, 0xB0, 0x4A, 0x5C,
                0xF0, 0x34, 0x6D, 0x15, 0x1C, 0x57, 0x32, 0x0E, 0x23, 0x64, 0xE0, 0x76, 0x5E, 0x80, 0x77, 0x0E,
                0x1C, 0xD3, 0x82, 0x2E, 0x78, 0x0D, 0xA4, 0x2A, 0xF5, 0x36, 0xED, 0x60, 0x5B, 0x30, 0x64, 0xBD,
                0x82, 0x20, 0xBD, 0x4E, 0x9B, 0x94, 0x4E, 0xA8, 0xA5, 0x8A, 0x2F, 0xE8, 0xB4, 0x0F, 0x82, 0x89,
                0xA7, 0xA3, 0xA5, 0x9D, 0xDB, 0xE0, 0xF7, 0xE4, 0x05, 0xA5, 0x72, 0xDA, 0x25, 0x42, 0xBC, 0x54,
                0xEE, 0x54, 0xC7, 0x43, 0x78, 0x80, 0x5A, 0xF5, 0x73, 0xF9, 0x71, 0xF2, 0x63, 0x1B, 0xEC, 0x7F,
                0x2F, 0x96, 0x2F, 0x27, 0x88, 0x5A, 0x4F, 0x34, 0xDE, 0x16, 0xF8, 0x54, 0x95, 0x26, 0x91, 0x9B,
                0x0C, 0xBC, 0x95, 0x67, 0xBF, 0xE0, 0x99, 0x7A, 0xD9, 0xF7, 0x90, 0x35, 0xB5, 0x82, 0x50, 0xD9,
                0x53, 0x09, 0x54, 0x8D, 0xF8, 0xF4, 0x8A, 0x9B, 0xDC, 0xD8, 0xAC, 0x88, 0x5E, 0xAE, 0xE4, 0xAC,
                0x7A, 0xB7, 0xD7, 0xA0, 0x54, 0xE9, 0x9D, 0xB8, 0x69, 0x59, 0x5D, 0x9E, 0x88, 0x1B, 0x0F, 0xF2,
                0x33, 0x89, 0xAF, 0x1A, 0x64, 0x44, 0xA1, 0x66, 0x5F, 0xB0, 0x2A, 0x8D, 0xAD, 0x3C, 0xAA, 0xAC,
                0x6C, 0xB3, 0x2E, 0x16, 0xEC, 0x46, 0x37, 0xD9, 0x6A, 0x4E, 0xEE, 0xC9, 0xD5, 0xDB, 0x66, 0x39,
                0x62, 0xE5, 0xCE, 0x23, 0xCE, 0xEF, 0x48, 0x81, 0x6C, 0xDF, 0xD1, 0x44, 0x65, 0x13, 0x52, 0x89,
                0xFC, 0xB2, 0x9D, 0xF3, 0xD2, 0xEE, 0xA9, 0x7C, 0x48, 0xAF, 0x59, 0xFF, 0x24, 0x5C, 0x03, 0x2B,
                0x9F, 0x43, 0xA9, 0xB1, 0x15, 0x86, 0xF2, 0x44, 0x14, 0x4A, 0xA9, 0x0A, 0xE2, 0x41, 0x4B, 0x55,
                0xC4, 0x55, 0xBD, 0x7C, 0x02, 0x79, 0xB7, 0xBB, 0xBF, 0x00, 0x00, 0x00, 0x00,
            };
            using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length);
            var format = new BinaryFormat(stream);

            var decompressor = new Decompressor();

            Assert.Throws <ExtractionException>(() => decompressor.Convert(format));
        }
Example #6
0
        public BinaryFormat Convert(BinaryFormat source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            // Read the header
            DataReader reader = new DataReader(source.Stream)
            {
                Endianness = EndiannessMode.BigEndian
            };

            uint format           = reader.ReadUInt32();
            uint decompressedSize = reader.ReadUInt32(); // decompressed size

            reader.ReadUInt32();                         // reserved
            reader.ReadUInt32();                         // reserved

            if (format != 2)
            {
                throw new FormatException("Unknown format " + format);
            }

            byte[] data = new byte[source.Stream.Length - HeaderSize];
            source.Stream.Read(data, 0, data.Length);

            // Round 0 only applies to the latest 0x800 bytes
            int round0Size = (data.Length > 0x800) ? 0x800 : data.Length;
            int round0Pos  = data.Length - round0Size;

            Round0(0x6d73, 0, 0x100, data, round0Pos);
            Compare(data, 0);

            Round1(data);
            Compare(data, 1);

            // Last Int32 in big endian is used as part of the multiplier
            // in the generation of random numbers.
            // The other are the expected checksums.
            int  multiplierInit = GetInt32BE(data, data.Length - 0x4);
            uint checksum1      = (uint)GetInt32BE(data, data.Length - 0x8);
            uint checksum2      = (uint)GetInt32BE(data, data.Length - 0xC);

            int newSize = Round2(data, multiplierInit);

            Array.Resize(ref data, newSize);
            Compare(data, 2);

            Validate(data, checksum1, checksum2);

            Round0(0x728F, multiplierInit, 0x80, data, 0);
            Compare(data, 3);

            data = Round4(data);
            Compare(data, 4);

            return(new BinaryFormat(DataStreamFactory.FromArray(data, 0, data.Length)));
        }
Example #7
0
        public void CreateFromArrayDoesNotAllowToExpand()
        {
            byte[] data   = new byte[] { 0x01, 0x2, 0x3 };
            var    stream = DataStreamFactory.FromArray(data, 1, 2);

            stream.Position = 2;
            Assert.That(() => stream.WriteByte(0xFE), Throws.InvalidOperationException);
            stream.Dispose();
        }
Example #8
0
        public void CreateFromArrayWritesToArray()
        {
            byte[] data   = new byte[] { 0x01, 0x2, 0x3 };
            var    stream = DataStreamFactory.FromArray(data, 1, 2);

            stream.WriteByte(0xFE);
            Assert.That(data[1], Is.EqualTo(0xFE));
            stream.Dispose();
        }
        public void IncompleteHeaderThrowsException()
        {
            var reader = new StandardBinReader();

            byte[] data = { 0xF9, 0x7D, 0x07, 0x00 };
            using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length);
            var format = new BinaryFormat(stream);

            Assert.Throws <FormatException>(() => reader.Convert(format));
        }
Example #10
0
        public void BadMagicThrowsException()
        {
            var reader = new DlcBinReader();

            byte[] data = { 0x64, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length);
            var format = new BinaryFormat(stream);

            Assert.Throws <FormatException>(() => reader.Convert(format));
        }
        public void CompressFilesBigEndian()
        {
            var compressor = new StandardBinCompressor();

            compressor.Initialize(EndiannessMode.BigEndian);

            byte[] data =
            {
                0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F,
                0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20, 0x63, 0x6F, 0x6E, 0x73,
                0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73, 0x63, 0x69,
                0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74, 0x2E, 0x20, 0x4D, 0x6F, 0x72, 0x62, 0x69, 0x20, 0x70,
                0x68, 0x61, 0x72, 0x65, 0x74, 0x72, 0x61, 0x20, 0x75, 0x74, 0x20, 0x76, 0x65, 0x6C, 0x69, 0x74,
                0x20, 0x65, 0x67, 0x65, 0x74, 0x20, 0x70, 0x6F, 0x72, 0x74, 0x61, 0x2E, 0x20, 0x55, 0x74, 0x20,
                0x75, 0x6C, 0x6C, 0x61, 0x6D, 0x63, 0x6F, 0x72, 0x70, 0x65, 0x72, 0x2C, 0x20, 0x6E, 0x69, 0x62,
                0x68, 0x20, 0x61, 0x20, 0x69, 0x6D, 0x70, 0x65, 0x72, 0x64, 0x69, 0x65, 0x74, 0x20, 0x69, 0x61,
                0x63, 0x75, 0x6C, 0x69, 0x73, 0x2C, 0x20, 0x6D, 0x61, 0x67, 0x6E, 0x61, 0x20, 0x72, 0x69, 0x73,
                0x75, 0x73, 0x20, 0x63, 0x75, 0x72, 0x73, 0x75, 0x73, 0x20, 0x6E, 0x65, 0x71, 0x75, 0x65, 0x2C,
                0x20, 0x65, 0x74, 0x20, 0x73, 0x61, 0x67, 0x69, 0x74, 0x74, 0x69, 0x73, 0x20, 0x6D, 0x61, 0x73,
                0x73, 0x61, 0x20, 0x73, 0x61, 0x70, 0x69, 0x65, 0x6E, 0x20, 0x61, 0x63, 0x20, 0x6E, 0x69, 0x73,
                0x69, 0x2E, 0x20, 0x4D, 0x6F, 0x72, 0x62, 0x69, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65,
                0x74, 0x20, 0x65, 0x73, 0x74, 0x20, 0x73, 0x61, 0x67, 0x69, 0x74, 0x74, 0x69, 0x73, 0x2C, 0x20,
                0x70, 0x6F, 0x72, 0x74, 0x61, 0x20, 0x74, 0x6F, 0x72, 0x74, 0x6F, 0x72, 0x20, 0x73, 0x65, 0x64,
                0x2C, 0x20, 0x73, 0x6F, 0x6C, 0x6C, 0x69, 0x63, 0x69, 0x74, 0x75, 0x64, 0x69, 0x6E, 0x20, 0x66,
                0x65, 0x6C, 0x69, 0x73, 0x2E, 0x20, 0x4E, 0x61, 0x6D, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62,
                0x75, 0x6C, 0x75, 0x6D, 0x20, 0x6C, 0x69, 0x67, 0x75, 0x6C, 0x61, 0x20, 0x61, 0x74, 0x20, 0x73,
                0x65, 0x6D, 0x70, 0x65, 0x72, 0x20, 0x76, 0x69, 0x76, 0x65, 0x72, 0x72, 0x61, 0x2E, 0x20, 0x50,
                0x68, 0x61, 0x73, 0x65, 0x6C, 0x6C, 0x75, 0x73, 0x20, 0x6E, 0x65, 0x63, 0x20, 0x6C, 0x61, 0x6F,
                0x72, 0x65, 0x65, 0x74, 0x20, 0x70, 0x75, 0x72, 0x75, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x65, 0x63,
                0x65, 0x6E, 0x61, 0x73, 0x20, 0x76, 0x65, 0x6C, 0x20, 0x70, 0x75, 0x72, 0x75, 0x73, 0x20, 0x69,
                0x6E, 0x74, 0x65, 0x72, 0x64, 0x75, 0x6D, 0x2C, 0x20, 0x62, 0x6C, 0x61, 0x6E, 0x64, 0x69, 0x74,
                0x20, 0x6C, 0x69, 0x62, 0x65, 0x72, 0x6F, 0x20, 0x69, 0x6E, 0x2C, 0x20, 0x67, 0x72, 0x61, 0x76,
                0x69, 0x64, 0x61, 0x20, 0x61, 0x75, 0x67, 0x75, 0x65, 0x2E, 0x20, 0x50, 0x72, 0x6F, 0x69, 0x6E,
                0x20, 0x65, 0x6C, 0x65, 0x6D, 0x65, 0x6E, 0x74, 0x75, 0x6D, 0x20, 0x64, 0x75, 0x69, 0x20, 0x69,
                0x64, 0x20, 0x6D, 0x61, 0x67, 0x6E, 0x61, 0x20, 0x74, 0x72, 0x69, 0x73, 0x74, 0x69, 0x71, 0x75,
                0x65, 0x20, 0x73, 0x61, 0x67, 0x69, 0x74, 0x74, 0x69, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x75, 0x72,
                0x69, 0x73, 0x20, 0x6D, 0x61, 0x78, 0x69, 0x6D, 0x75, 0x73, 0x20, 0x74, 0x72, 0x69, 0x73, 0x74,
                0x69, 0x71, 0x75, 0x65, 0x20, 0x73, 0x65, 0x6D, 0x2C, 0x20, 0x61, 0x63, 0x20, 0x70, 0x6C, 0x61,
                0x63, 0x65, 0x72, 0x61, 0x74, 0x20, 0x6C, 0x65, 0x6F, 0x20, 0x74, 0x65, 0x6D, 0x70, 0x75, 0x73,
                0x20, 0x61, 0x2E, 0x20, 0x41, 0x6C, 0x69, 0x71, 0x75, 0x61, 0x6D, 0x20, 0x6C, 0x65, 0x6F, 0x2E,
            };

            using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length);
            using Node n            = NodeFactory.FromSubstream("compress", stream, 0, stream.Length);
            n.Tags["Type"]          = FileType.Compressed;

            var container = NodeFactory.CreateContainer("container");

            container.Add(n);

            var result = compressor.Convert(container.GetFormatAs <NodeContainerFormat>());

            Assert.AreEqual(1, result.Root.Children.Count);
            Assert.AreEqual(0x13D, result.Root.Children[0].Stream.Length);
        }
Example #12
0
        public void CreateFromArrayReadsArray()
        {
            byte[] data = new byte[] { 0x01, 0x2, 0x3 };

            var stream = DataStreamFactory.FromArray(data, 1, 2);

            Assert.AreEqual(0, stream.Position);
            Assert.AreEqual(1, stream.Offset);
            Assert.AreEqual(2, stream.Length);
            Assert.That(stream.ReadByte(), Is.EqualTo(2));
            stream.Dispose();
        }
Example #13
0
        public void SkipNoBinaryFormats()
        {
            var decompressor = new StandardBinDecompressor();

            byte[] data =
            {
                0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F,
                0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20, 0x63, 0x6F, 0x6E, 0x73,
                0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73, 0x63, 0x69,
                0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74, 0x2E, 0x20, 0x4D, 0x6F, 0x72, 0x62, 0x69, 0x20, 0x70,
                0x68, 0x61, 0x72, 0x65, 0x74, 0x72, 0x61, 0x20, 0x75, 0x74, 0x20, 0x76, 0x65, 0x6C, 0x69, 0x74,
                0x20, 0x65, 0x67, 0x65, 0x74, 0x20, 0x70, 0x6F, 0x72, 0x74, 0x61, 0x2E, 0x20, 0x55, 0x74, 0x20,
                0x75, 0x6C, 0x6C, 0x61, 0x6D, 0x63, 0x6F, 0x72, 0x70, 0x65, 0x72, 0x2C, 0x20, 0x6E, 0x69, 0x62,
                0x68, 0x20, 0x61, 0x20, 0x69, 0x6D, 0x70, 0x65, 0x72, 0x64, 0x69, 0x65, 0x74, 0x20, 0x69, 0x61,
                0x63, 0x75, 0x6C, 0x69, 0x73, 0x2C, 0x20, 0x6D, 0x61, 0x67, 0x6E, 0x61, 0x20, 0x72, 0x69, 0x73,
                0x75, 0x73, 0x20, 0x63, 0x75, 0x72, 0x73, 0x75, 0x73, 0x20, 0x6E, 0x65, 0x71, 0x75, 0x65, 0x2C,
                0x20, 0x65, 0x74, 0x20, 0x73, 0x61, 0x67, 0x69, 0x74, 0x74, 0x69, 0x73, 0x20, 0x6D, 0x61, 0x73,
                0x73, 0x61, 0x20, 0x73, 0x61, 0x70, 0x69, 0x65, 0x6E, 0x20, 0x61, 0x63, 0x20, 0x6E, 0x69, 0x73,
                0x69, 0x2E, 0x20, 0x4D, 0x6F, 0x72, 0x62, 0x69, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65,
                0x74, 0x20, 0x65, 0x73, 0x74, 0x20, 0x73, 0x61, 0x67, 0x69, 0x74, 0x74, 0x69, 0x73, 0x2C, 0x20,
                0x70, 0x6F, 0x72, 0x74, 0x61, 0x20, 0x74, 0x6F, 0x72, 0x74, 0x6F, 0x72, 0x20, 0x73, 0x65, 0x64,
                0x2C, 0x20, 0x73, 0x6F, 0x6C, 0x6C, 0x69, 0x63, 0x69, 0x74, 0x75, 0x64, 0x69, 0x6E, 0x20, 0x66,
                0x65, 0x6C, 0x69, 0x73, 0x2E, 0x20, 0x4E, 0x61, 0x6D, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62,
                0x75, 0x6C, 0x75, 0x6D, 0x20, 0x6C, 0x69, 0x67, 0x75, 0x6C, 0x61, 0x20, 0x61, 0x74, 0x20, 0x73,
                0x65, 0x6D, 0x70, 0x65, 0x72, 0x20, 0x76, 0x69, 0x76, 0x65, 0x72, 0x72, 0x61, 0x2E, 0x20, 0x50,
                0x68, 0x61, 0x73, 0x65, 0x6C, 0x6C, 0x75, 0x73, 0x20, 0x6E, 0x65, 0x63, 0x20, 0x6C, 0x61, 0x6F,
                0x72, 0x65, 0x65, 0x74, 0x20, 0x70, 0x75, 0x72, 0x75, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x65, 0x63,
                0x65, 0x6E, 0x61, 0x73, 0x20, 0x76, 0x65, 0x6C, 0x20, 0x70, 0x75, 0x72, 0x75, 0x73, 0x20, 0x69,
                0x6E, 0x74, 0x65, 0x72, 0x64, 0x75, 0x6D, 0x2C, 0x20, 0x62, 0x6C, 0x61, 0x6E, 0x64, 0x69, 0x74,
                0x20, 0x6C, 0x69, 0x62, 0x65, 0x72, 0x6F, 0x20, 0x69, 0x6E, 0x2C, 0x20, 0x67, 0x72, 0x61, 0x76,
                0x69, 0x64, 0x61, 0x20, 0x61, 0x75, 0x67, 0x75, 0x65, 0x2E, 0x20, 0x50, 0x72, 0x6F, 0x69, 0x6E,
                0x20, 0x65, 0x6C, 0x65, 0x6D, 0x65, 0x6E, 0x74, 0x75, 0x6D, 0x20, 0x64, 0x75, 0x69, 0x20, 0x69,
                0x64, 0x20, 0x6D, 0x61, 0x67, 0x6E, 0x61, 0x20, 0x74, 0x72, 0x69, 0x73, 0x74, 0x69, 0x71, 0x75,
                0x65, 0x20, 0x73, 0x61, 0x67, 0x69, 0x74, 0x74, 0x69, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x75, 0x72,
                0x69, 0x73, 0x20, 0x6D, 0x61, 0x78, 0x69, 0x6D, 0x75, 0x73, 0x20, 0x74, 0x72, 0x69, 0x73, 0x74,
                0x69, 0x71, 0x75, 0x65, 0x20, 0x73, 0x65, 0x6D, 0x2C, 0x20, 0x61, 0x63, 0x20, 0x70, 0x6C, 0x61,
                0x63, 0x65, 0x72, 0x61, 0x74, 0x20, 0x6C, 0x65, 0x6F, 0x20, 0x74, 0x65, 0x6D, 0x70, 0x75, 0x73,
                0x20, 0x61, 0x2E, 0x20, 0x41, 0x6C, 0x69, 0x71, 0x75, 0x61, 0x6D, 0x20, 0x6C, 0x65, 0x6F, 0x2E,
            };

            using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length);
            using Node n            = NodeFactory.FromSubstream("skip", stream, 0, stream.Length);
            n.Tags["Type"]          = FileType.Normal;

            var container  = NodeFactory.CreateContainer("container");
            var container2 = NodeFactory.CreateContainer("container2");

            container.Add(container2);
            container2.Add(n);

            Assert.DoesNotThrow(() => decompressor.Convert(container.GetFormatAs <NodeContainerFormat>()));
        }
        public void CanReadEmptyFiles()
        {
            var reader = new StandardBinReader();

            byte[] data =
            {
                0xF9, 0x7D, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            };
            using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length);
            var format = new BinaryFormat(stream);
            var result = reader.Convert(format);

            Assert.AreEqual(1, result.Root.Children.Count);
            Assert.AreEqual(FileType.Empty, result.Root.Children[0].Tags["Type"]);
        }
Example #15
0
        /// <summary>
        /// Decompress a SLLZ zlib compressed BinaryFormat.
        /// </summary>
        /// <param name="source">Compressed format.</param>
        /// <returns>The decompressed binary.</returns>
        public ParFile Convert(ParFile source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var reader = new DataReader(source.Stream)
            {
                DefaultEncoding = Encoding.ASCII,
            };

            _ = source.Stream.Seek(4, SeekOrigin.Begin);
            byte endianness = reader.ReadByte();

            reader.Endianness = endianness == 0 ? EndiannessMode.LittleEndian : EndiannessMode.BigEndian;

            source.Stream.Position = 0;

            // Read the file header
            var header = reader.Read <SllzHeader>() as SllzHeader;

            CheckHeader(header);

            _ = reader.Stream.Seek(header.HeaderSize, SeekOrigin.Begin);

            byte[] compressedData   = reader.ReadBytes((int)(header.CompressedSize - header.HeaderSize));
            byte[] decompressedData = Decompress(compressedData, header.OriginalSize);

            DataStream newStream = DataStreamFactory.FromArray(
                decompressedData,
                0,
                (int)header.OriginalSize);

            var fileInfo = new ParFileInfo
            {
                Flags          = 0x00000000,
                OriginalSize   = (uint)newStream.Length,
                CompressedSize = (uint)newStream.Length,
                DataOffset     = source.FileInfo.DataOffset,
                RawAttributes  = source.FileInfo.RawAttributes,
                ExtendedOffset = source.FileInfo.ExtendedOffset,
                Timestamp      = source.FileInfo.Timestamp,
            };

            return(new ParFile(fileInfo, newStream));
        }
Example #16
0
        public void CanWriteBigEndianFiles()
        {
            byte[] expected =
            {
                0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D,
            };
            var expectedStream = DataStreamFactory.FromArray(expected, 0, expected.Length);

            var writer = new DlcBinWriter();
            writer.Initialize(new WriterParameters
            {
                Endianness = EndiannessMode.BigEndian,
            });
            var container = new NodeContainerFormat();
            var node = NodeFactory.FromMemory("normal");
            node.Stream.Write(new byte[]
            {
                0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D, 0xBA, 0xAD, 0xF0, 0x0D,
            });

            node.Tags["Index"] = 1;
            container.Root.Add(node);

            var binaryFormat = writer.Convert(container);
            Assert.AreEqual(expected.Length, binaryFormat.Stream.Length);
            Assert.IsTrue(binaryFormat.Stream.Compare(expectedStream));
        }
Example #17
0
        private static DataStream DecompressV2(DataStream inputDataStream, int compressedSize, int decompressedSize)
        {
            var inputData  = new byte[compressedSize];
            var outputData = new byte[decompressedSize];

            inputDataStream.Read(inputData, 0, compressedSize - 0x10);

            var inputPosition  = 0;
            var outputPosition = 0;

            while (outputPosition < decompressedSize)
            {
                int compressedChunkSize   = (inputData[inputPosition] << 16) | (inputData[inputPosition + 1] << 8) | inputData[inputPosition + 2];
                int decompressedChunkSize = ((inputData[inputPosition + 3] << 8) | inputData[inputPosition + 4]) + 1;

                bool flag = (inputData[inputPosition] & 0x80) == 0x80;

                if (!flag)
                {
                    byte[] decompressedData = ZlibDecompress(inputData, inputPosition + 5, compressedChunkSize - 5);

                    if (decompressedChunkSize != decompressedData.Length)
                    {
                        throw new FormatException("SLLZ: Wrong decompressed data.");
                    }

                    Array.Copy(decompressedData, 0, outputData, outputPosition, decompressedData.Length);
                    inputPosition += compressedChunkSize;
                }
                else
                {
                    // I haven't found any file with this compression
                    // The code is in FUN_141e27350 (YakuzaKiwami2.exe v1.4)
                    throw new FormatException("SLLZ: Not ZLIB compression.");
                }

                outputPosition += decompressedChunkSize;
            }

            DataStream outputDataStream = DataStreamFactory.FromArray(outputData, 0, decompressedSize);

            return(outputDataStream);
        }
Example #18
0
        public void DecompressFilesBigEndian()
        {
            var decompressor = new StandardBinDecompressor();

            decompressor.Initialize(EndiannessMode.BigEndian);

            byte[] data =
            {
                0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x31, 0x78, 0xDA, 0x4D, 0x91, 0xCB, 0x6D, 0xC4, 0x30,
                0x0C, 0x44, 0x5B, 0x99, 0x02, 0x8C, 0xED, 0x21, 0xF7, 0x24, 0xC8, 0x25, 0x05, 0xD0, 0x32, 0xE3,
                0x25, 0x40, 0x49, 0x0E, 0x3F, 0x8B, 0x94, 0x1F, 0x7A, 0xF3, 0xBD, 0x48, 0x10, 0x35, 0x22, 0xE7,
                0x8D, 0x1E, 0xA7, 0x71, 0x87, 0x1C, 0x9E, 0x1D, 0xDB, 0xD4, 0x69, 0x70, 0x09, 0x50, 0xE7, 0x58,
                0xD0, 0xE6, 0x70, 0x6E, 0xC1, 0x91, 0x06, 0xDA, 0xE4, 0x10, 0x6F, 0x32, 0x76, 0xB0, 0x4A, 0x5C,
                0xF0, 0x34, 0x6D, 0x15, 0x1C, 0x57, 0x32, 0x0E, 0x23, 0x64, 0xE0, 0x76, 0x5E, 0x80, 0x77, 0x0E,
                0x1C, 0xD3, 0x82, 0x2E, 0x78, 0x0D, 0xA4, 0x2A, 0xF5, 0x36, 0xED, 0x60, 0x5B, 0x30, 0x64, 0xBD,
                0x82, 0x20, 0xBD, 0x4E, 0x9B, 0x94, 0x4E, 0xA8, 0xA5, 0x8A, 0x2F, 0xE8, 0xB4, 0x0F, 0x82, 0x89,
                0xA7, 0xA3, 0xA5, 0x9D, 0xDB, 0xE0, 0xF7, 0xE4, 0x05, 0xA5, 0x72, 0xDA, 0x25, 0x42, 0xBC, 0x54,
                0xEE, 0x54, 0xC7, 0x43, 0x78, 0x80, 0x5A, 0xF5, 0x73, 0xF9, 0x71, 0xF2, 0x63, 0x1B, 0xEC, 0x7F,
                0x2F, 0x96, 0x2F, 0x27, 0x88, 0x5A, 0x4F, 0x34, 0xDE, 0x16, 0xF8, 0x54, 0x95, 0x26, 0x91, 0x9B,
                0x0C, 0xBC, 0x95, 0x67, 0xBF, 0xE0, 0x99, 0x7A, 0xD9, 0xF7, 0x90, 0x35, 0xB5, 0x82, 0x50, 0xD9,
                0x53, 0x09, 0x54, 0x8D, 0xF8, 0xF4, 0x8A, 0x9B, 0xDC, 0xD8, 0xAC, 0x88, 0x5E, 0xAE, 0xE4, 0xAC,
                0x7A, 0xB7, 0xD7, 0xA0, 0x54, 0xE9, 0x9D, 0xB8, 0x69, 0x59, 0x5D, 0x9E, 0x88, 0x1B, 0x0F, 0xF2,
                0x33, 0x89, 0xAF, 0x1A, 0x64, 0x44, 0xA1, 0x66, 0x5F, 0xB0, 0x2A, 0x8D, 0xAD, 0x3C, 0xAA, 0xAC,
                0x6C, 0xB3, 0x2E, 0x16, 0xEC, 0x46, 0x37, 0xD9, 0x6A, 0x4E, 0xEE, 0xC9, 0xD5, 0xDB, 0x66, 0x39,
                0x62, 0xE5, 0xCE, 0x23, 0xCE, 0xEF, 0x48, 0x81, 0x6C, 0xDF, 0xD1, 0x44, 0x65, 0x13, 0x52, 0x89,
                0xFC, 0xB2, 0x9D, 0xF3, 0xD2, 0xEE, 0xA9, 0x7C, 0x48, 0xAF, 0x59, 0xFF, 0x24, 0x5C, 0x03, 0x2B,
                0x9F, 0x43, 0xA9, 0xB1, 0x15, 0x86, 0xF2, 0x44, 0x14, 0x4A, 0xA9, 0x0A, 0xE2, 0x41, 0x4B, 0x55,
                0xC4, 0x55, 0xBD, 0x7C, 0x02, 0x79, 0xB7, 0xBB, 0xBF, 0x00, 0x00, 0x00, 0x00,
            };

            using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length);
            using Node n            = NodeFactory.FromSubstream("compress", stream, 0, stream.Length);
            n.Tags["Type"]          = FileType.Compressed;

            var container = NodeFactory.CreateContainer("container");

            container.Add(n);

            var result = decompressor.Convert(container.GetFormatAs <NodeContainerFormat>());

            Assert.AreEqual(1, result.Root.Children.Count);
            Assert.AreEqual(0x200, result.Root.Children[0].Stream.Length);
        }
Example #19
0
        public void TestSllz(byte compressorVersion)
        {
            const string sampleText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed pulvinar leo nec pulvinar pellentesque. Sed id dui et nisl tincidunt dignissim. Suspendisse ullamcorper eget ipsum et vehicula. Maecenas scelerisque dapibus rutrum. Suspendisse tincidunt dictum maximus. Ut rhoncus, lorem scelerisque euismod rhoncus, nunc augue egestas magna, ac mattis elit sapien eu erat. Pellentesque auctor in erat id molestie. Nam vehicula odio eget ipsum porta euismod. Donec eget placerat turpis. Aliquam erat volutpat. Etiam faucibus ligula sit amet ante tincidunt, sit amet efficitur justo lobortis. Nam volutpat augue at purus viverra tincidunt. Nam sapien eros, fringilla sollicitudin semper sed, bibendum eu nisl.\nPellentesque mattis, sem a placerat dictum, risus nisl faucibus odio, quis blandit est erat sed tellus. Mauris iaculis odio sed leo suscipit ultrices. Nunc leo purus, tempor at lobortis vel, molestie pellentesque dui. Sed ac nunc et metus placerat euismod non vel nisl. Proin fringilla viverra aliquet. Pellentesque scelerisque fermentum eleifend. Quisque rutrum orci nulla, sed ullamcorper nulla porttitor nec. Fusce elementum a nulla et ultricies. Pellentesque bibendum blandit leo nec gravida. Donec egestas vitae tellus id auctor.\nMorbi ultrices maximus mattis. Aliquam lobortis at lectus ut scelerisque. Morbi sem tortor, blandit non risus vel, commodo interdum neque. Fusce eu hendrerit mauris, in venenatis est. Vivamus vulputate placerat justo at condimentum. Proin porttitor ac velit quis pretium. Proin vitae lacus sit amet felis aliquam tempor. Fusce dignissim mi id dui imperdiet, nec commodo neque malesuada. Nullam tincidunt augue pellentesque aliquam fringilla. Sed placerat, nibh id fermentum volutpat, ligula felis sagittis lectus, vel semper ipsum arcu at ipsum. Etiam posuere tincidunt augue non gravida. Vivamus posuere posuere dui, a semper urna imperdiet a. Proin quis odio condimentum, pulvinar ipsum vitae, eleifend lorem. Praesent vel iaculis nisi. Aliquam imperdiet eleifend nisi a imperdiet. Mauris vel aliquam quam, ac imperdiet elit.\nVivamus semper odio nec scelerisque porttitor. Quisque ut pulvinar tortor, et consectetur sapien. Fusce neque nulla, laoreet tincidunt risus sit amet, dignissim condimentum enim. Donec dui libero, pulvinar a nibh eget, varius convallis diam. Donec rhoncus elit vel nibh varius, at rhoncus justo consectetur. Pellentesque congue feugiat pulvinar. Donec aliquet sapien ut egestas feugiat. Praesent rhoncus libero libero, non auctor tellus fermentum at. Nunc mollis ornare lacus, et sodales enim rhoncus non. Curabitur vitae dui finibus, varius ex quis, pellentesque ipsum. Etiam metus diam, porttitor in odio vel, suscipit malesuada odio. Nullam eget leo tortor. Duis id posuere augue. Vivamus consequat libero ipsum, nec efficitur ante maximus ullamcorper. Praesent consequat tristique lectus, in aliquam libero hendrerit ac.\nDuis nec felis risus. Pellentesque sit amet massa id lorem ullamcorper rhoncus dignissim pellentesque felis. Suspendisse vitae magna ut ex sodales porta. Proin facilisis augue consectetur ante feugiat vestibulum. Maecenas imperdiet enim nunc, a tincidunt eros luctus et. Nunc gravida, odio eget cursus efficitur, nulla mauris rhoncus mi, rutrum ullamcorper ante ante vitae elit. Quisque vel lacinia justo, in vestibulum nisi. Nam quis maximus ligula, eget ornare mi. Etiam porttitor orci non nulla vestibulum, vitae eleifend nulla pellentesque. Vestibulum eu orci quam. Nullam id tellus viverra, finibus purus et, iaculis diam. Sed ut volutpat eros. In vitae ultrices nibh.";

            byte[] buffer = Encoding.ASCII.GetBytes(sampleText);

            DataStream dataStream = DataStreamFactory.FromArray(buffer, 0, buffer.Length);

            using var binaryFormat = new ParFile(dataStream);

            var parameters = new CompressorParameters
            {
                Endianness = 0x00,
                Version    = compressorVersion,
            };

            var compressedBinaryFormat   = (ParFile)ConvertFormat.With <Compressor, CompressorParameters>(parameters, binaryFormat);
            var decompressedBinaryFormat = (ParFile)ConvertFormat.With <Decompressor>(compressedBinaryFormat);

            Assert.IsTrue(compressedBinaryFormat.Stream.Length < decompressedBinaryFormat.Stream.Length);
            Assert.IsTrue(dataStream.Compare(decompressedBinaryFormat.Stream));
        }
Example #20
0
        static void Compare(byte[] data, int round)
        {
            if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("SIA_DEBUG")))
            {
                return;
            }

            string basePath = Environment.GetEnvironmentVariable("SIA_WORKDIR");
            string filePath = Path.Combine(basePath, $"round{round}_f.bin");

            using (var original = DataStreamFactory.FromFile(filePath, FileOpenMode.Read)) {
                using (var source = DataStreamFactory.FromArray(data, 0, data.Length)) {
                    if (!original.Compare(source))
                    {
                        string tempFile = Path.Combine(basePath, "temp.bin");
                        source.WriteTo(tempFile);
                        Console.WriteLine($"!! Error on round {round} !!");
                        Environment.Exit(1);
                    }
                }
            }
        }
Example #21
0
        public void CreateFromArrayWithInvalidThrows()
        {
            byte[] data = new byte[] { 0x01 };

            Assert.That(
                () => DataStreamFactory.FromArray(null, 0, 0),
                Throws.ArgumentNullException);
            Assert.That(
                () => DataStreamFactory.FromArray(data, -1, 0),
                Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(
                () => DataStreamFactory.FromArray(data, 2, 0),
                Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(
                () => DataStreamFactory.FromArray(data, 0, -1),
                Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(
                () => DataStreamFactory.FromArray(data, 0, 2),
                Throws.InstanceOf <ArgumentOutOfRangeException>());
            Assert.That(
                () => DataStreamFactory.FromArray(data, 1, 1),
                Throws.InstanceOf <ArgumentOutOfRangeException>());
        }
Example #22
0
        /// <summary>
        /// Converts a Armp into a Excel binary.
        /// </summary>
        /// <param name="source">Input format.</param>
        /// <returns>The xlsx format.</returns>
        /// <exception cref="ArgumentNullException">Thrown if source is null.</exception>
        public BinaryFormat Convert(ArmpTable source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

            using var package = new ExcelPackage();

            OfficeOpenXml.Style.XmlAccess.ExcelNamedStyleXml namedStyle = package.Workbook.Styles.CreateNamedStyle("HyperLink");
            namedStyle.Style.Font.UnderLine = true;
            namedStyle.Style.Font.Color.SetColor(Color.Blue);

            TableToSheet(source, "Main", package);

            byte[] data = package.GetAsByteArray();

            DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length);

            return(new BinaryFormat(stream));
        }
        public void CanWriteDummyFiles()
        {
            byte[] expected =
            {
                0x00, 0x07, 0x7D, 0xF9, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x70, 0x83, 0x5F, 0x83, 0x7E,
                0x83, 0x5F, 0x83, 0x7E, 0x81, 0x5B, 0x82, 0xC5, 0x82, 0xB7, 0x2E, 0x0D, 0x0A, 0x90, 0xB3, 0x8E,
                0xAE, 0x82, 0xC8, 0x83, 0x66, 0x81, 0x5B, 0x83, 0x5E, 0x82, 0xAA, 0x93, 0xFC, 0x82, 0xE9, 0x82,
                0xDC, 0x82, 0xC5, 0x81, 0x41, 0x82, 0xD0, 0x82, 0xC6, 0x82, 0xDC, 0x82, 0xB8, 0x83, 0x8A, 0x83,
                0x93, 0x83, 0x4E, 0x83, 0x66, 0x81, 0x5B, 0x83, 0x5E, 0x82, 0xF0, 0x8D, 0xEC, 0x90, 0xAC, 0x82,
                0xB7, 0x82, 0xE9, 0x82, 0xBD, 0x82, 0xDF, 0x82, 0xCC, 0x83, 0x5F, 0x83, 0x7E, 0x81, 0x5B, 0x83,
                0x74, 0x83, 0x40, 0x83, 0x43, 0x83, 0x8B, 0x82, 0xC6, 0x82, 0xB5, 0x82, 0xC4, 0x8D, 0xEC, 0x90,
                0xAC, 0x82, 0xB3, 0x82, 0xEA, 0x82, 0xC4, 0x82, 0xA2, 0x82, 0xDC, 0x82, 0xB7, 0x2E, 0x0D, 0x0A,
            };
            var expectedStream = DataStreamFactory.FromArray(expected, 0, expected.Length);

            var writer     = new StandardBinWriter();
            var parameters = new WriterParameters
            {
                Endianness = EndiannessMode.BigEndian,
                BlockSize  = 0x20,
            };

            writer.Initialize(parameters);

            var container = new NodeContainerFormat();
            var node      = NodeFactory.FromMemory("dummy");

            node.Tags["Type"]  = FileType.Dummy;
            node.Tags["Index"] = 1;
            container.Root.Add(node);

            var binaryFormat = writer.Convert(container);

            Assert.AreEqual(expected.Length, binaryFormat.Stream.Length);
            Assert.IsTrue(binaryFormat.Stream.Compare(expectedStream));
        }
        public void CanReadCompressedFiles()
        {
            var reader = new StandardBinReader();

            byte[] data =
            {
                0xF9, 0x7D, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3D, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
                0x00, 0x02, 0x00, 0x00, 0x31, 0x01, 0x00, 0x00, 0x78, 0xDA, 0x4D, 0x91, 0xCB, 0x6D, 0xC4, 0x30,
                0x0C, 0x44, 0x5B, 0x99, 0x02, 0x8C, 0xED, 0x21, 0xF7, 0x24, 0xC8, 0x25, 0x05, 0xD0, 0x32, 0xE3,
                0x25, 0x40, 0x49, 0x0E, 0x3F, 0x8B, 0x94, 0x1F, 0x7A, 0xF3, 0xBD, 0x48, 0x10, 0x35, 0x22, 0xE7,
                0x8D, 0x1E, 0xA7, 0x71, 0x87, 0x1C, 0x9E, 0x1D, 0xDB, 0xD4, 0x69, 0x70, 0x09, 0x50, 0xE7, 0x58,
                0xD0, 0xE6, 0x70, 0x6E, 0xC1, 0x91, 0x06, 0xDA, 0xE4, 0x10, 0x6F, 0x32, 0x76, 0xB0, 0x4A, 0x5C,
                0xF0, 0x34, 0x6D, 0x15, 0x1C, 0x57, 0x32, 0x0E, 0x23, 0x64, 0xE0, 0x76, 0x5E, 0x80, 0x77, 0x0E,
                0x1C, 0xD3, 0x82, 0x2E, 0x78, 0x0D, 0xA4, 0x2A, 0xF5, 0x36, 0xED, 0x60, 0x5B, 0x30, 0x64, 0xBD,
                0x82, 0x20, 0xBD, 0x4E, 0x9B, 0x94, 0x4E, 0xA8, 0xA5, 0x8A, 0x2F, 0xE8, 0xB4, 0x0F, 0x82, 0x89,
                0xA7, 0xA3, 0xA5, 0x9D, 0xDB, 0xE0, 0xF7, 0xE4, 0x05, 0xA5, 0x72, 0xDA, 0x25, 0x42, 0xBC, 0x54,
                0xEE, 0x54, 0xC7, 0x43, 0x78, 0x80, 0x5A, 0xF5, 0x73, 0xF9, 0x71, 0xF2, 0x63, 0x1B, 0xEC, 0x7F,
                0x2F, 0x96, 0x2F, 0x27, 0x88, 0x5A, 0x4F, 0x34, 0xDE, 0x16, 0xF8, 0x54, 0x95, 0x26, 0x91, 0x9B,
                0x0C, 0xBC, 0x95, 0x67, 0xBF, 0xE0, 0x99, 0x7A, 0xD9, 0xF7, 0x90, 0x35, 0xB5, 0x82, 0x50, 0xD9,
                0x53, 0x09, 0x54, 0x8D, 0xF8, 0xF4, 0x8A, 0x9B, 0xDC, 0xD8, 0xAC, 0x88, 0x5E, 0xAE, 0xE4, 0xAC,
                0x7A, 0xB7, 0xD7, 0xA0, 0x54, 0xE9, 0x9D, 0xB8, 0x69, 0x59, 0x5D, 0x9E, 0x88, 0x1B, 0x0F, 0xF2,
                0x33, 0x89, 0xAF, 0x1A, 0x64, 0x44, 0xA1, 0x66, 0x5F, 0xB0, 0x2A, 0x8D, 0xAD, 0x3C, 0xAA, 0xAC,
                0x6C, 0xB3, 0x2E, 0x16, 0xEC, 0x46, 0x37, 0xD9, 0x6A, 0x4E, 0xEE, 0xC9, 0xD5, 0xDB, 0x66, 0x39,
                0x62, 0xE5, 0xCE, 0x23, 0xCE, 0xEF, 0x48, 0x81, 0x6C, 0xDF, 0xD1, 0x44, 0x65, 0x13, 0x52, 0x89,
                0xFC, 0xB2, 0x9D, 0xF3, 0xD2, 0xEE, 0xA9, 0x7C, 0x48, 0xAF, 0x59, 0xFF, 0x24, 0x5C, 0x03, 0x2B,
                0x9F, 0x43, 0xA9, 0xB1, 0x15, 0x86, 0xF2, 0x44, 0x14, 0x4A, 0xA9, 0x0A, 0xE2, 0x41, 0x4B, 0x55,
                0xC4, 0x55, 0xBD, 0x7C, 0x02, 0x79, 0xB7, 0xBB, 0xBF, 0x00, 0x00, 0x00, 0x00,
            };
            using DataStream stream = DataStreamFactory.FromArray(data, 0, data.Length);
            var format = new BinaryFormat(stream);

            var result = reader.Convert(format);

            Assert.AreEqual(1, result.Root.Children.Count);
            Assert.AreEqual(FileType.Compressed, result.Root.Children[0].Tags["Type"]);
        }
Example #25
0
        private static DataStream CompressV1(DataStream inputDataStream)
        {
            // It's easier to implement working with a byte array.
            var inputData  = new byte[inputDataStream.Length];
            var outputData = new byte[inputData.Length * 2];

            inputDataStream.Read(inputData, 0, inputData.Length);

            int  inputPosition  = 0;
            int  outputPosition = 0;
            byte currentFlag    = 0x00;
            int  bitCount       = 0;
            long flagPosition   = outputPosition;

            outputData[flagPosition] = 0x00;
            outputPosition++;

            while (inputPosition < inputData.Length)
            {
                Tuple <int, int> match = FindMatch(inputData, inputPosition);

                if (match == null)
                {
                    // currentFlag |= (byte)(0 << (7 - bitCount)); // It's zero
                    bitCount++;

                    if (bitCount == 0x08)
                    {
                        outputData[flagPosition] = currentFlag;

                        currentFlag              = 0x00;
                        bitCount                 = 0x00;
                        flagPosition             = outputPosition;
                        outputData[flagPosition] = 0x00;
                        outputPosition++;
                    }

                    outputData[outputPosition] = inputData[inputPosition];
                    inputPosition++;
                    outputPosition++;
                }
                else
                {
                    currentFlag |= (byte)(1 << (7 - bitCount));
                    bitCount++;

                    if (bitCount == 0x08)
                    {
                        outputData[flagPosition] = currentFlag;

                        currentFlag              = 0x00;
                        bitCount                 = 0x00;
                        flagPosition             = outputPosition;
                        outputData[flagPosition] = 0x00;
                        outputPosition++;
                    }

                    short offset = (short)((match.Item1 - 1) << 4);
                    short size   = (short)((match.Item2 - 3) & 0x0F);

                    short tuple = (short)(offset | size);

                    outputData[outputPosition] = (byte)tuple;
                    outputPosition++;
                    outputData[outputPosition] = (byte)(tuple >> 8);
                    outputPosition++;

                    inputPosition += match.Item2;
                }
            }

            outputData[flagPosition] = currentFlag;

            DataStream outputDataStream = DataStreamFactory.FromArray(outputData, 0, outputPosition);

            return(outputDataStream);
        }
Example #26
0
        public void DecompressFileLittleEndian()
        {
            byte[] expected =
            {
                0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F,
                0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20, 0x63, 0x6F, 0x6E, 0x73,
                0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73, 0x63, 0x69,
                0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74, 0x2E, 0x20, 0x4D, 0x6F, 0x72, 0x62, 0x69, 0x20, 0x70,
                0x68, 0x61, 0x72, 0x65, 0x74, 0x72, 0x61, 0x20, 0x75, 0x74, 0x20, 0x76, 0x65, 0x6C, 0x69, 0x74,
                0x20, 0x65, 0x67, 0x65, 0x74, 0x20, 0x70, 0x6F, 0x72, 0x74, 0x61, 0x2E, 0x20, 0x55, 0x74, 0x20,
                0x75, 0x6C, 0x6C, 0x61, 0x6D, 0x63, 0x6F, 0x72, 0x70, 0x65, 0x72, 0x2C, 0x20, 0x6E, 0x69, 0x62,
                0x68, 0x20, 0x61, 0x20, 0x69, 0x6D, 0x70, 0x65, 0x72, 0x64, 0x69, 0x65, 0x74, 0x20, 0x69, 0x61,
                0x63, 0x75, 0x6C, 0x69, 0x73, 0x2C, 0x20, 0x6D, 0x61, 0x67, 0x6E, 0x61, 0x20, 0x72, 0x69, 0x73,
                0x75, 0x73, 0x20, 0x63, 0x75, 0x72, 0x73, 0x75, 0x73, 0x20, 0x6E, 0x65, 0x71, 0x75, 0x65, 0x2C,
                0x20, 0x65, 0x74, 0x20, 0x73, 0x61, 0x67, 0x69, 0x74, 0x74, 0x69, 0x73, 0x20, 0x6D, 0x61, 0x73,
                0x73, 0x61, 0x20, 0x73, 0x61, 0x70, 0x69, 0x65, 0x6E, 0x20, 0x61, 0x63, 0x20, 0x6E, 0x69, 0x73,
                0x69, 0x2E, 0x20, 0x4D, 0x6F, 0x72, 0x62, 0x69, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65,
                0x74, 0x20, 0x65, 0x73, 0x74, 0x20, 0x73, 0x61, 0x67, 0x69, 0x74, 0x74, 0x69, 0x73, 0x2C, 0x20,
                0x70, 0x6F, 0x72, 0x74, 0x61, 0x20, 0x74, 0x6F, 0x72, 0x74, 0x6F, 0x72, 0x20, 0x73, 0x65, 0x64,
                0x2C, 0x20, 0x73, 0x6F, 0x6C, 0x6C, 0x69, 0x63, 0x69, 0x74, 0x75, 0x64, 0x69, 0x6E, 0x20, 0x66,
                0x65, 0x6C, 0x69, 0x73, 0x2E, 0x20, 0x4E, 0x61, 0x6D, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62,
                0x75, 0x6C, 0x75, 0x6D, 0x20, 0x6C, 0x69, 0x67, 0x75, 0x6C, 0x61, 0x20, 0x61, 0x74, 0x20, 0x73,
                0x65, 0x6D, 0x70, 0x65, 0x72, 0x20, 0x76, 0x69, 0x76, 0x65, 0x72, 0x72, 0x61, 0x2E, 0x20, 0x50,
                0x68, 0x61, 0x73, 0x65, 0x6C, 0x6C, 0x75, 0x73, 0x20, 0x6E, 0x65, 0x63, 0x20, 0x6C, 0x61, 0x6F,
                0x72, 0x65, 0x65, 0x74, 0x20, 0x70, 0x75, 0x72, 0x75, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x65, 0x63,
                0x65, 0x6E, 0x61, 0x73, 0x20, 0x76, 0x65, 0x6C, 0x20, 0x70, 0x75, 0x72, 0x75, 0x73, 0x20, 0x69,
                0x6E, 0x74, 0x65, 0x72, 0x64, 0x75, 0x6D, 0x2C, 0x20, 0x62, 0x6C, 0x61, 0x6E, 0x64, 0x69, 0x74,
                0x20, 0x6C, 0x69, 0x62, 0x65, 0x72, 0x6F, 0x20, 0x69, 0x6E, 0x2C, 0x20, 0x67, 0x72, 0x61, 0x76,
                0x69, 0x64, 0x61, 0x20, 0x61, 0x75, 0x67, 0x75, 0x65, 0x2E, 0x20, 0x50, 0x72, 0x6F, 0x69, 0x6E,
                0x20, 0x65, 0x6C, 0x65, 0x6D, 0x65, 0x6E, 0x74, 0x75, 0x6D, 0x20, 0x64, 0x75, 0x69, 0x20, 0x69,
                0x64, 0x20, 0x6D, 0x61, 0x67, 0x6E, 0x61, 0x20, 0x74, 0x72, 0x69, 0x73, 0x74, 0x69, 0x71, 0x75,
                0x65, 0x20, 0x73, 0x61, 0x67, 0x69, 0x74, 0x74, 0x69, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x75, 0x72,
                0x69, 0x73, 0x20, 0x6D, 0x61, 0x78, 0x69, 0x6D, 0x75, 0x73, 0x20, 0x74, 0x72, 0x69, 0x73, 0x74,
                0x69, 0x71, 0x75, 0x65, 0x20, 0x73, 0x65, 0x6D, 0x2C, 0x20, 0x61, 0x63, 0x20, 0x70, 0x6C, 0x61,
                0x63, 0x65, 0x72, 0x61, 0x74, 0x20, 0x6C, 0x65, 0x6F, 0x20, 0x74, 0x65, 0x6D, 0x70, 0x75, 0x73,
                0x20, 0x61, 0x2E, 0x20, 0x41, 0x6C, 0x69, 0x71, 0x75, 0x61, 0x6D, 0x20, 0x6C, 0x65, 0x6F, 0x2E,
            };
            var expectedStream = DataStreamFactory.FromArray(expected, 0, expected.Length);

            byte[] data =
            {
                0x00, 0x02, 0x00, 0x00, 0x31, 0x01, 0x00, 0x00, 0x78, 0xDA, 0x4D, 0x91, 0xCB, 0x6D, 0xC4, 0x30,
                0x0C, 0x44, 0x5B, 0x99, 0x02, 0x8C, 0xED, 0x21, 0xF7, 0x24, 0xC8, 0x25, 0x05, 0xD0, 0x32, 0xE3,
                0x25, 0x40, 0x49, 0x0E, 0x3F, 0x8B, 0x94, 0x1F, 0x7A, 0xF3, 0xBD, 0x48, 0x10, 0x35, 0x22, 0xE7,
                0x8D, 0x1E, 0xA7, 0x71, 0x87, 0x1C, 0x9E, 0x1D, 0xDB, 0xD4, 0x69, 0x70, 0x09, 0x50, 0xE7, 0x58,
                0xD0, 0xE6, 0x70, 0x6E, 0xC1, 0x91, 0x06, 0xDA, 0xE4, 0x10, 0x6F, 0x32, 0x76, 0xB0, 0x4A, 0x5C,
                0xF0, 0x34, 0x6D, 0x15, 0x1C, 0x57, 0x32, 0x0E, 0x23, 0x64, 0xE0, 0x76, 0x5E, 0x80, 0x77, 0x0E,
                0x1C, 0xD3, 0x82, 0x2E, 0x78, 0x0D, 0xA4, 0x2A, 0xF5, 0x36, 0xED, 0x60, 0x5B, 0x30, 0x64, 0xBD,
                0x82, 0x20, 0xBD, 0x4E, 0x9B, 0x94, 0x4E, 0xA8, 0xA5, 0x8A, 0x2F, 0xE8, 0xB4, 0x0F, 0x82, 0x89,
                0xA7, 0xA3, 0xA5, 0x9D, 0xDB, 0xE0, 0xF7, 0xE4, 0x05, 0xA5, 0x72, 0xDA, 0x25, 0x42, 0xBC, 0x54,
                0xEE, 0x54, 0xC7, 0x43, 0x78, 0x80, 0x5A, 0xF5, 0x73, 0xF9, 0x71, 0xF2, 0x63, 0x1B, 0xEC, 0x7F,
                0x2F, 0x96, 0x2F, 0x27, 0x88, 0x5A, 0x4F, 0x34, 0xDE, 0x16, 0xF8, 0x54, 0x95, 0x26, 0x91, 0x9B,
                0x0C, 0xBC, 0x95, 0x67, 0xBF, 0xE0, 0x99, 0x7A, 0xD9, 0xF7, 0x90, 0x35, 0xB5, 0x82, 0x50, 0xD9,
                0x53, 0x09, 0x54, 0x8D, 0xF8, 0xF4, 0x8A, 0x9B, 0xDC, 0xD8, 0xAC, 0x88, 0x5E, 0xAE, 0xE4, 0xAC,
                0x7A, 0xB7, 0xD7, 0xA0, 0x54, 0xE9, 0x9D, 0xB8, 0x69, 0x59, 0x5D, 0x9E, 0x88, 0x1B, 0x0F, 0xF2,
                0x33, 0x89, 0xAF, 0x1A, 0x64, 0x44, 0xA1, 0x66, 0x5F, 0xB0, 0x2A, 0x8D, 0xAD, 0x3C, 0xAA, 0xAC,
                0x6C, 0xB3, 0x2E, 0x16, 0xEC, 0x46, 0x37, 0xD9, 0x6A, 0x4E, 0xEE, 0xC9, 0xD5, 0xDB, 0x66, 0x39,
                0x62, 0xE5, 0xCE, 0x23, 0xCE, 0xEF, 0x48, 0x81, 0x6C, 0xDF, 0xD1, 0x44, 0x65, 0x13, 0x52, 0x89,
                0xFC, 0xB2, 0x9D, 0xF3, 0xD2, 0xEE, 0xA9, 0x7C, 0x48, 0xAF, 0x59, 0xFF, 0x24, 0x5C, 0x03, 0x2B,
                0x9F, 0x43, 0xA9, 0xB1, 0x15, 0x86, 0xF2, 0x44, 0x14, 0x4A, 0xA9, 0x0A, 0xE2, 0x41, 0x4B, 0x55,
                0xC4, 0x55, 0xBD, 0x7C, 0x02, 0x79, 0xB7, 0xBB, 0xBF, 0x00, 0x00, 0x00, 0x00,
            };
            var dataStream = DataStreamFactory.FromArray(data, 0, data.Length);

            var decompressor = new Decompressor();
            var binaryFormat = decompressor.Convert(new BinaryFormat(dataStream));

            Assert.AreEqual(expected.Length, binaryFormat.Stream.Length);
            Assert.IsTrue(binaryFormat.Stream.Compare(expectedStream));
        }
        public void CanWriteCompressedFilesChangingEndianness()
        {
            byte[] expected =
            {
                0xF9, 0x7D, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3D, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
                0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x31, 0x78, 0xDA, 0x4D, 0x91, 0xCB, 0x6D, 0xC4, 0x30,
                0x0C, 0x44, 0x5B, 0x99, 0x02, 0x8C, 0xED, 0x21, 0xF7, 0x24, 0xC8, 0x25, 0x05, 0xD0, 0x32, 0xE3,
                0x25, 0x40, 0x49, 0x0E, 0x3F, 0x8B, 0x94, 0x1F, 0x7A, 0xF3, 0xBD, 0x48, 0x10, 0x35, 0x22, 0xE7,
                0x8D, 0x1E, 0xA7, 0x71, 0x87, 0x1C, 0x9E, 0x1D, 0xDB, 0xD4, 0x69, 0x70, 0x09, 0x50, 0xE7, 0x58,
                0xD0, 0xE6, 0x70, 0x6E, 0xC1, 0x91, 0x06, 0xDA, 0xE4, 0x10, 0x6F, 0x32, 0x76, 0xB0, 0x4A, 0x5C,
                0xF0, 0x34, 0x6D, 0x15, 0x1C, 0x57, 0x32, 0x0E, 0x23, 0x64, 0xE0, 0x76, 0x5E, 0x80, 0x77, 0x0E,
                0x1C, 0xD3, 0x82, 0x2E, 0x78, 0x0D, 0xA4, 0x2A, 0xF5, 0x36, 0xED, 0x60, 0x5B, 0x30, 0x64, 0xBD,
                0x82, 0x20, 0xBD, 0x4E, 0x9B, 0x94, 0x4E, 0xA8, 0xA5, 0x8A, 0x2F, 0xE8, 0xB4, 0x0F, 0x82, 0x89,
                0xA7, 0xA3, 0xA5, 0x9D, 0xDB, 0xE0, 0xF7, 0xE4, 0x05, 0xA5, 0x72, 0xDA, 0x25, 0x42, 0xBC, 0x54,
                0xEE, 0x54, 0xC7, 0x43, 0x78, 0x80, 0x5A, 0xF5, 0x73, 0xF9, 0x71, 0xF2, 0x63, 0x1B, 0xEC, 0x7F,
                0x2F, 0x96, 0x2F, 0x27, 0x88, 0x5A, 0x4F, 0x34, 0xDE, 0x16, 0xF8, 0x54, 0x95, 0x26, 0x91, 0x9B,
                0x0C, 0xBC, 0x95, 0x67, 0xBF, 0xE0, 0x99, 0x7A, 0xD9, 0xF7, 0x90, 0x35, 0xB5, 0x82, 0x50, 0xD9,
                0x53, 0x09, 0x54, 0x8D, 0xF8, 0xF4, 0x8A, 0x9B, 0xDC, 0xD8, 0xAC, 0x88, 0x5E, 0xAE, 0xE4, 0xAC,
                0x7A, 0xB7, 0xD7, 0xA0, 0x54, 0xE9, 0x9D, 0xB8, 0x69, 0x59, 0x5D, 0x9E, 0x88, 0x1B, 0x0F, 0xF2,
                0x33, 0x89, 0xAF, 0x1A, 0x64, 0x44, 0xA1, 0x66, 0x5F, 0xB0, 0x2A, 0x8D, 0xAD, 0x3C, 0xAA, 0xAC,
                0x6C, 0xB3, 0x2E, 0x16, 0xEC, 0x46, 0x37, 0xD9, 0x6A, 0x4E, 0xEE, 0xC9, 0xD5, 0xDB, 0x66, 0x39,
                0x62, 0xE5, 0xCE, 0x23, 0xCE, 0xEF, 0x48, 0x81, 0x6C, 0xDF, 0xD1, 0x44, 0x65, 0x13, 0x52, 0x89,
                0xFC, 0xB2, 0x9D, 0xF3, 0xD2, 0xEE, 0xA9, 0x7C, 0x48, 0xAF, 0x59, 0xFF, 0x24, 0x5C, 0x03, 0x2B,
                0x9F, 0x43, 0xA9, 0xB1, 0x15, 0x86, 0xF2, 0x44, 0x14, 0x4A, 0xA9, 0x0A, 0xE2, 0x41, 0x4B, 0x55,
                0xC4, 0x55, 0xBD, 0x7C, 0x02, 0x79, 0xB7, 0xBB, 0xBF, 0x00, 0x00, 0x00, 0x00,
            };
            var expectedStream = DataStreamFactory.FromArray(expected, 0, expected.Length);

            var writer     = new StandardBinWriter();
            var parameters = new WriterParameters
            {
                BlockSize = 0x20,
            };

            writer.Initialize(parameters);

            var container = new NodeContainerFormat();
            var node      = NodeFactory.FromMemory("compressed");

            node.Stream.Write(new byte[]
            {
                0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x31, 0x78, 0xDA, 0x4D, 0x91, 0xCB, 0x6D, 0xC4, 0x30,
                0x0C, 0x44, 0x5B, 0x99, 0x02, 0x8C, 0xED, 0x21, 0xF7, 0x24, 0xC8, 0x25, 0x05, 0xD0, 0x32, 0xE3,
                0x25, 0x40, 0x49, 0x0E, 0x3F, 0x8B, 0x94, 0x1F, 0x7A, 0xF3, 0xBD, 0x48, 0x10, 0x35, 0x22, 0xE7,
                0x8D, 0x1E, 0xA7, 0x71, 0x87, 0x1C, 0x9E, 0x1D, 0xDB, 0xD4, 0x69, 0x70, 0x09, 0x50, 0xE7, 0x58,
                0xD0, 0xE6, 0x70, 0x6E, 0xC1, 0x91, 0x06, 0xDA, 0xE4, 0x10, 0x6F, 0x32, 0x76, 0xB0, 0x4A, 0x5C,
                0xF0, 0x34, 0x6D, 0x15, 0x1C, 0x57, 0x32, 0x0E, 0x23, 0x64, 0xE0, 0x76, 0x5E, 0x80, 0x77, 0x0E,
                0x1C, 0xD3, 0x82, 0x2E, 0x78, 0x0D, 0xA4, 0x2A, 0xF5, 0x36, 0xED, 0x60, 0x5B, 0x30, 0x64, 0xBD,
                0x82, 0x20, 0xBD, 0x4E, 0x9B, 0x94, 0x4E, 0xA8, 0xA5, 0x8A, 0x2F, 0xE8, 0xB4, 0x0F, 0x82, 0x89,
                0xA7, 0xA3, 0xA5, 0x9D, 0xDB, 0xE0, 0xF7, 0xE4, 0x05, 0xA5, 0x72, 0xDA, 0x25, 0x42, 0xBC, 0x54,
                0xEE, 0x54, 0xC7, 0x43, 0x78, 0x80, 0x5A, 0xF5, 0x73, 0xF9, 0x71, 0xF2, 0x63, 0x1B, 0xEC, 0x7F,
                0x2F, 0x96, 0x2F, 0x27, 0x88, 0x5A, 0x4F, 0x34, 0xDE, 0x16, 0xF8, 0x54, 0x95, 0x26, 0x91, 0x9B,
                0x0C, 0xBC, 0x95, 0x67, 0xBF, 0xE0, 0x99, 0x7A, 0xD9, 0xF7, 0x90, 0x35, 0xB5, 0x82, 0x50, 0xD9,
                0x53, 0x09, 0x54, 0x8D, 0xF8, 0xF4, 0x8A, 0x9B, 0xDC, 0xD8, 0xAC, 0x88, 0x5E, 0xAE, 0xE4, 0xAC,
                0x7A, 0xB7, 0xD7, 0xA0, 0x54, 0xE9, 0x9D, 0xB8, 0x69, 0x59, 0x5D, 0x9E, 0x88, 0x1B, 0x0F, 0xF2,
                0x33, 0x89, 0xAF, 0x1A, 0x64, 0x44, 0xA1, 0x66, 0x5F, 0xB0, 0x2A, 0x8D, 0xAD, 0x3C, 0xAA, 0xAC,
                0x6C, 0xB3, 0x2E, 0x16, 0xEC, 0x46, 0x37, 0xD9, 0x6A, 0x4E, 0xEE, 0xC9, 0xD5, 0xDB, 0x66, 0x39,
                0x62, 0xE5, 0xCE, 0x23, 0xCE, 0xEF, 0x48, 0x81, 0x6C, 0xDF, 0xD1, 0x44, 0x65, 0x13, 0x52, 0x89,
                0xFC, 0xB2, 0x9D, 0xF3, 0xD2, 0xEE, 0xA9, 0x7C, 0x48, 0xAF, 0x59, 0xFF, 0x24, 0x5C, 0x03, 0x2B,
                0x9F, 0x43, 0xA9, 0xB1, 0x15, 0x86, 0xF2, 0x44, 0x14, 0x4A, 0xA9, 0x0A, 0xE2, 0x41, 0x4B, 0x55,
                0xC4, 0x55, 0xBD, 0x7C, 0x02, 0x79, 0xB7, 0xBB, 0xBF, 0x00, 0x00, 0x00, 0x00,
            });
            node.Tags["Type"]         = FileType.CompressedAlternateEndian;
            node.Tags["Index"]        = 1;
            node.Tags["InflatedSize"] = 0x200;
            container.Root.Add(node);

            var binaryFormat = writer.Convert(container);

            Assert.AreEqual(expected.Length, binaryFormat.Stream.Length);
            Assert.IsTrue(binaryFormat.Stream.Compare(expectedStream));
        }
Example #28
0
        private static DataStream CompressV1(DataStream inputDataStream)
        {
            // It's easier to implement working with a byte array.
            var inputData = new byte[inputDataStream.Length];

            inputDataStream.Read(inputData, 0, inputData.Length);

            uint outputSize = (uint)inputData.Length + 2048;
            var  outputData = new byte[outputSize];

            uint inputPosition  = 0;
            uint outputPosition = 0;
            byte currentFlag    = 0x00;
            var  bitCount       = 0;
            long flagPosition   = outputPosition;

            outputData[flagPosition] = 0x00;
            outputPosition++;

            if (outputPosition >= outputSize)
            {
                throw new SllzCompressorException("Compressed size is bigger than original size.");
            }

            while (inputPosition < inputData.Length)
            {
                uint windowSize      = Math.Min(inputPosition, MAX_WINDOW_SIZE);
                uint maxOffsetLength = Math.Min((uint)(inputData.Length - inputPosition), MAX_ENCODED_LENGTH);

                Tuple <uint, uint> match = FindMatch(inputData, inputPosition, windowSize, maxOffsetLength);

                if (match == null)
                {
                    // currentFlag |= (byte)(0 << (7 - bitCount)); // It's zero
                    bitCount++;

                    if (bitCount == 0x08)
                    {
                        outputData[flagPosition] = currentFlag;

                        currentFlag              = 0x00;
                        bitCount                 = 0x00;
                        flagPosition             = outputPosition;
                        outputData[flagPosition] = 0x00;
                        outputPosition++;
                        if (outputPosition >= outputSize)
                        {
                            throw new SllzCompressorException("Compressed size is bigger than original size.");
                        }
                    }

                    outputData[outputPosition] = inputData[inputPosition];
                    inputPosition++;
                    outputPosition++;

                    if (outputPosition >= outputSize)
                    {
                        throw new SllzCompressorException("Compressed size is bigger than original size.");
                    }
                }
                else
                {
                    currentFlag |= (byte)(1 << (7 - bitCount));
                    bitCount++;

                    if (bitCount == 0x08)
                    {
                        outputData[flagPosition] = currentFlag;

                        currentFlag              = 0x00;
                        bitCount                 = 0x00;
                        flagPosition             = outputPosition;
                        outputData[flagPosition] = 0x00;
                        outputPosition++;

                        if (outputPosition >= outputSize)
                        {
                            throw new SllzCompressorException("Compressed size is bigger than original size.");
                        }
                    }

                    var offset = (short)((match.Item1 - 1) << 4);
                    var size   = (short)((match.Item2 - 3) & 0x0F);

                    var tuple = (short)(offset | size);

                    outputData[outputPosition] = (byte)tuple;
                    outputPosition++;

                    if (outputPosition >= outputSize)
                    {
                        throw new SllzCompressorException("Compressed size is bigger than original size.");
                    }

                    outputData[outputPosition] = (byte)(tuple >> 8);
                    outputPosition++;

                    if (outputPosition >= outputSize)
                    {
                        throw new SllzCompressorException("Compressed size is bigger than original size.");
                    }

                    inputPosition += match.Item2;
                }
            }

            outputData[flagPosition] = currentFlag;

            DataStream outputDataStream = DataStreamFactory.FromArray(outputData, 0, (int)outputPosition);

            return(outputDataStream);
        }
Example #29
0
        private static DataStream DecompressV1(DataStream inputDataStream, int compressedSize, int decompressedSize)
        {
            var inputData  = new byte[compressedSize];
            var outputData = new byte[decompressedSize];

            inputDataStream.Read(inputData, 0, compressedSize - 0x10);

            var inputPosition  = 0;
            var outputPosition = 0;

            byte flag = inputData[inputPosition];

            inputPosition++;
            var flagCount = 8;

            do
            {
                if ((flag & 0x80) == 0x80)
                {
                    flag = (byte)(flag << 1);
                    flagCount--;
                    if (flagCount == 0)
                    {
                        flag = inputData[inputPosition];
                        inputPosition++;
                        flagCount = 8;
                    }

                    var copyFlags = (ushort)(inputData[inputPosition] | inputData[inputPosition + 1] << 8);
                    inputPosition += 2;

                    int copyDistance = 1 + (copyFlags >> 4);
                    int copyCount    = 3 + (copyFlags & 0xF);

                    var i = 0;
                    do
                    {
                        outputData[outputPosition] = outputData[outputPosition - copyDistance];
                        outputPosition++;
                        i++;
                    }while (i < copyCount);
                }
                else
                {
                    flag = (byte)(flag << 1);
                    flagCount--;
                    if (flagCount == 0)
                    {
                        flag = inputData[inputPosition];
                        inputPosition++;
                        flagCount = 8;
                    }

                    outputData[outputPosition] = inputData[inputPosition];
                    inputPosition++;
                    outputPosition++;
                }
            }while (outputPosition < decompressedSize);

            DataStream outputDataStream = DataStreamFactory.FromArray(outputData, 0, decompressedSize);

            return(outputDataStream);
        }