Exemple #1
0
        public CompressedChunk ReadCompressedChunk()
        {
            var chunk = new CompressedChunk();

            chunk.Signature = U32();
            if (chunk.Signature != PackageSignature.Valid)
            {
                throw new InvalidDataException("Invalid compressed chunk signature");
            }
            chunk.BlockSize        = U32();
            chunk.CompressedSize   = U32();
            chunk.UncompressedSize = U32();
            var blockCount = (uint)Math.Ceiling(chunk.UncompressedSize / (double)chunk.BlockSize);

            chunk.Blocks = Array(blockCount, () =>
            {
                var block = new CompressedBlock
                {
                    CompressedSize   = U32(),
                    UncompressedSize = U32()
                };
                var data         = Bytes(block.CompressedSize);
                var decompressed = new byte[block.UncompressedSize];
                var output       = new byte[block.UncompressedSize];
                block.Data       = Lzo.Decompress(data, output);
                return(block);
            });

            return(chunk);
        }
Exemple #2
0
        public void CanCompress()
        {
            var data       = GetData();
            var compressed = Lzo.Compress(data);

            compressed.Should().NotBeNull().And.NotBeEquivalentTo(data);
        }
Exemple #3
0
        public void CanCompressWithMode(CompressionMode compressionMode)
        {
            var data       = GetData();
            var compressed = Lzo.Compress(compressionMode, data);

            compressed.Should().NotBeNull().And.NotBeEquivalentTo(data);
        }
Exemple #4
0
        public void CanTryCompressWithMode(CompressionMode compressionMode)
        {
            var data   = GetData();
            var result = Lzo.TryCompress(compressionMode, data, out var compressed);

            result.Should().Be(LzoResult.OK);
            compressed.Should().NotBeNull().And.NotBeEquivalentTo(data);
        }
Exemple #5
0
        public void CanTryCompressWithSpan(CompressionMode compressionMode)
        {
            var data       = GetData();
            var compressed = new byte[data.Length + data.Length / 16 + 64 + 3];
            var result     = Lzo.TryCompress(compressionMode, data, data.Length, compressed, out var compressedLength);

            result.Should().Be(LzoResult.OK);
            compressedLength.Should().BeGreaterThan(0);
            compressed.Should().NotBeNull().And.NotBeEquivalentTo(data);
        }
Exemple #6
0
        private static void Main()
        {
            var sample       = Encoding.UTF8.GetBytes("Hello World");
            var workMemory   = new byte[Lzo.WorkMemorySize];
            var compressed   = Lzo.Compress(CompressionMode.Lzo1x_999, sample);
            var decompressed = Lzo.Decompress(compressed);

            Console.WriteLine("sample: {0}", BitConverter.ToString(sample));
            Console.WriteLine("compressed: {0}", BitConverter.ToString(compressed));
            Console.WriteLine("decompressed: {0}", BitConverter.ToString(decompressed));
        }
Exemple #7
0
        public void CanTryDecompress()
        {
            var data             = GetData();
            var compressResult   = Lzo.TryCompress(data, out var compressed);
            var decompressResult = Lzo.TryDecompress(compressed, out var decompressed);

            compressResult.Should().Be(LzoResult.OK);
            compressed.Should().NotBeNull().And.NotBeEquivalentTo(data);

            decompressResult.Should().Be(LzoResult.OK);
            decompressed.Should().NotBeNull().And.BeEquivalentTo(data);
        }
Exemple #8
0
    /// <exception cref="MissingLzoException"></exception>
    public void Read(byte[] data, int uncompressedSize, IProgress <GameBoxReadProgress>?progress = null)
    {
        var buffer = new byte[uncompressedSize];

        Lzo.Decompress(data, buffer);

#if DEBUG
        Debugger.CompressedData   = data;
        Debugger.UncompressedData = buffer;
#endif

        Read(buffer, progress);
    }
Exemple #9
0
        public void CanTryDecompressWithSpanAndWorkMemory(CompressionMode compressionMode)
        {
            var data             = GetData();
            var workMemory       = new byte[Lzo.WorkMemorySize];
            var compressed       = new byte[data.Length + data.Length / 16 + 64 + 3];
            var decompressed     = new byte[data.Length];
            var compressResult   = Lzo.TryCompress(compressionMode, data, data.Length, compressed, out var compressedLength, workMemory);
            var decompressResult = Lzo.TryDecompress(compressed, compressedLength, decompressed, out var decompressedLength);

            compressResult.Should().Be(LzoResult.OK);
            compressedLength.Should().BeGreaterThan(0);
            compressed.Should().NotBeNull().And.NotBeEquivalentTo(data);

            decompressResult.Should().Be(LzoResult.OK);
            decompressedLength.Should().Be(data.Length);
            decompressed.Should().NotBeNull().And.BeEquivalentTo(data);
        }
Exemple #10
0
    /// <exception cref="IOException">An I/O error occurs.</exception>
    /// <exception cref="ObjectDisposedException">The stream is closed.</exception>
    /// <exception cref="MissingLzoException"></exception>
    public void Write(GameBoxWriter w, IDRemap remap = default)
    {
        GBX.Remap = remap;

        if (GBX.Header.CompressionOfBody == GameBoxCompression.Uncompressed)
        {
            GBX.Node.Write(w);
            return;
        }

        using var msBody   = new MemoryStream();
        using var gbxwBody = new GameBoxWriter(msBody, body: this);

        GBX.Node.Write(gbxwBody, remap);

        var buffer = msBody.ToArray();

        var output = Lzo.Compress(buffer);

        w.Write((int)msBody.Length); // Uncompressed
        w.Write(output.Length);      // Compressed
        w.WriteBytes(output);        // Compressed body data
    }
Exemple #11
0
        public static byte[] Load(Stream stream, uint[] keys)
        {
            byte[] int32t = new byte[4], fourcc = new byte[4];

            stream.Read(fourcc, 0, 4);

            if (fourcc[0] == 'M' && fourcc[1] == 'C' && fourcc[2] == 'O' && fourcc[3] == 'Z')
            {
            }
            // @todo: Snappy
            else
            {
                return(null);
            }

            stream.Read(int32t, 0, 4);
            uint cryptedSize = BitConverter.ToUInt32(int32t, 0);

            stream.Read(int32t, 0, 4);
            uint compressSize = BitConverter.ToUInt32(int32t, 0);

            stream.Read(int32t, 0, 4);
            uint realSize = BitConverter.ToUInt32(int32t, 0);

            byte[] cdata;

            if (cryptedSize == 0)
            {
                if (stream.Length < compressSize)
                {
                    return(null);
                }

                cdata = new byte[compressSize];
                stream.Read(cdata, 0, (int)compressSize);

                if (cdata[0] != fourcc[0] || cdata[1] != fourcc[1] || cdata[2] != fourcc[2] || cdata[3] != fourcc[3])
                {
                    return(null);
                }
            }
            else
            {
                if (stream.Length < cryptedSize)
                {
                    return(null);
                }

                byte[] data = new byte[cryptedSize];
                stream.Read(data, 0, (int)cryptedSize);

                cdata = Xtea.XteaDecrypt(data, cryptedSize, keys);

                if (cdata[0] != fourcc[0] || cdata[1] != fourcc[1] || cdata[2] != fourcc[2] || cdata[3] != fourcc[3])
                {
                    return(null);
                }
            }

            return(Lzo.Decompress(cdata, compressSize, realSize));
        }