Example #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);
        }
Example #2
0
        public void CanDecompress()
        {
            var data         = GetData();
            var compressed   = Lzo.Compress(data);
            var decompressed = Lzo.Decompress(compressed);

            compressed.Should().NotBeNull().And.NotBeEquivalentTo(data);
            decompressed.Should().NotBeNull().And.BeEquivalentTo(data);
        }
Example #3
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));
        }
Example #4
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);
    }
Example #5
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));
        }