Beispiel #1
0
 public void Compress(string algorithm)
 {
     if (algorithm == CompressionAlgorithm.Deflate)
     {
         byte[]        buffer        = _memoryStream.ToArray();
         MemoryStream  ms            = new MemoryStream();
         DeflateStream deflateStream = new DeflateStream(ms, CompressionMode.Compress, true);
         deflateStream.Write(buffer, 0, buffer.Length);
         deflateStream.Close();
         _memoryStream.Close();
         _memoryStream = ms;
         AMFReader amfReader = new AMFReader(_memoryStream);
         AMFWriter amfWriter = new AMFWriter(_memoryStream);
         _dataOutput = new DataOutput(amfWriter);
         _dataInput  = new DataInput(amfReader);
     }
     if (algorithm == CompressionAlgorithm.Zlib)
     {
         byte[]       buffer     = _memoryStream.ToArray();
         MemoryStream ms         = new MemoryStream();
         ZlibStream   zlibStream = new ZlibStream(ms, CompressionMode.Compress, true);
         zlibStream.Write(buffer, 0, buffer.Length);
         zlibStream.Flush();
         zlibStream.Close();
         zlibStream.Dispose();
         _memoryStream.Close();
         _memoryStream = ms;
         AMFReader amfReader = new AMFReader(_memoryStream);
         AMFWriter amfWriter = new AMFWriter(_memoryStream);
         _dataOutput = new DataOutput(amfWriter);
         _dataInput  = new DataInput(amfReader);
     }
 }
Beispiel #2
0
        private byte[] InternalExtract()
        {
            _stream.Position = _manifest.Offset;

            var sizeBytes = new byte[4];

            _stream.Read(sizeBytes, 0, 4);

            int uncompressedSize = BitConverter.ToInt32(sizeBytes, 0);

            if (uncompressedSize <= 0)
            {
                return(new byte[0]);
            }

            _stream.Position += 4;

            var buffer = new byte[_manifest.UncompressedSize];

            var zlibStream = new ZlibStream(_stream, CompressionMode.Decompress, true);

            zlibStream.Read(buffer, 0, buffer.Length);

            zlibStream.Close();
            zlibStream.Dispose();

            return(buffer);
        }
Beispiel #3
0
        public static void Decompress(byte[] compressed, int compressedOffset, int compressedSize, byte[] uncompressed, int uncompressedOffset, int uncompressedSize, CompressionMethod method, FArchive?reader = null)
        {
            using var srcStream = new MemoryStream(compressed, compressedOffset, compressedSize, false)
                  {
                      Position = 0
                  };
            switch (method)
            {
            case CompressionMethod.None:
                Buffer.BlockCopy(compressed, compressedOffset, uncompressed, uncompressedOffset, compressedSize);
                return;

            case CompressionMethod.Zlib:
                var zlib = new ZlibStream(srcStream, CompressionMode.Decompress);
                zlib.Read(uncompressed, uncompressedOffset, uncompressedSize);
                zlib.Dispose();
                return;

            case CompressionMethod.Gzip:
                var gzip = new GZipStream(srcStream, CompressionMode.Decompress);
                gzip.Read(uncompressed, uncompressedOffset, uncompressedSize);
                gzip.Dispose();
                return;

            case CompressionMethod.Oodle:
                Oodle.Decompress(compressed, compressedOffset, compressedSize, uncompressed, uncompressedOffset, uncompressedSize, reader);
                return;

            case CompressionMethod.LZ4:
                var uncompressedBuffer = new byte[uncompressedSize + uncompressedSize / 255 + 16];     // LZ4_compressBound(uncompressedSize)
                int result;
#if USE_LZ4_NATIVE_LIB
                unsafe
                {
                    fixed(byte *compressedPtr = compressed, uncompressedBufferPtr = uncompressedBuffer)
                    {
                        result = LZ4.LZ4_decompress_safe(compressedPtr + compressedOffset, uncompressedBufferPtr, compressedSize, uncompressedBuffer.Length);
                    }
                }
#else
                result = LZ4Codec.Decode(compressed, compressedOffset, compressedSize, uncompressedBuffer, 0, uncompressedBuffer.Length);
#endif
                Buffer.BlockCopy(uncompressedBuffer, 0, uncompressed, uncompressedOffset, uncompressedSize);
                if (result != uncompressedSize)
                {
                    throw new FileLoadException($"Failed to decompress LZ4 data (Expected: {uncompressedSize}, Result: {result})");
                }
                return;

            default:
                if (reader != null)
                {
                    throw new UnknownCompressionMethodException(reader, $"Compression method \"{method}\" is unknown");
                }
                else
                {
                    throw new UnknownCompressionMethodException($"Compression method \"{method}\" is unknown");
                }
            }
        }
Beispiel #4
0
        public void SetChunkData(byte[] cData, Boolean isCompressed = true)
        {
            MemoryStream byteStream = new MemoryStream();
            ZlibStream   zStream    = new ZlibStream(byteStream, CompressionMode.Decompress, CompressionLevel.BestCompression, true);

            zStream.Write(cData, 0, cData.Length);
            zStream.Flush();
            zStream.Dispose();
            zStream = null;

            byte[] bytes = byteStream.ToArray();
            byteStream.Dispose();
            byteStream = null;

            Block         b;
            WorldLocation blockLoc;
            int           x, y, z;

            // Block Types
            for (int i = 0; i < 32768; i++)
            {
                x        = (Location.X * 16) + (i >> 11);
                y        = i & 0x7F;
                z        = (Location.Z * 16) + ((i & 0x780) >> 7);
                blockLoc = new WorldLocation(x, y, z);
                b        = new Block((BlockType)bytes[i], blockLoc);
                Blocks.Add(b);
            }

            // TODO: Read and handle block/sky light and metadata

            bytes = null;
        }
Beispiel #5
0
        public void Write(Texture2D texture2D, Stream outputStream)
        {
            width  = texture2D.Width;
            height = texture2D.Height;

            GetColorData(texture2D);

            // write PNG signature
            outputStream.Write(HeaderChunk.PngSignature, 0, HeaderChunk.PngSignature.Length);

            // write header chunk
            var headerChunk = new HeaderChunk();

            headerChunk.Width             = (uint)texture2D.Width;
            headerChunk.Height            = (uint)texture2D.Height;
            headerChunk.BitDepth          = 8;
            headerChunk.ColorType         = colorType;
            headerChunk.CompressionMethod = 0;
            headerChunk.FilterMethod      = 0;
            headerChunk.InterlaceMethod   = 0;

            var headerChunkBytes = headerChunk.Encode();

            outputStream.Write(headerChunkBytes, 0, headerChunkBytes.Length);

            // write data chunks
            var encodedPixelData    = EncodePixelData(texture2D);
            var compressedPixelData = new MemoryStream();

            ZlibStream deflateStream = null;

            try
            {
                deflateStream = new ZlibStream(compressedPixelData, CompressionMode.Compress);
                deflateStream.Write(encodedPixelData, 0, encodedPixelData.Length);
                deflateStream.Finish();
            }
            catch (Exception exception)
            {
                throw new Exception("An error occurred during DEFLATE compression.", exception);
            }

            var dataChunk = new DataChunk();

            dataChunk.Data = compressedPixelData.ToArray();
            var dataChunkBytes = dataChunk.Encode();

            outputStream.Write(dataChunkBytes, 0, dataChunkBytes.Length);

            deflateStream.Dispose();
            compressedPixelData.Dispose();

            // write end chunk
            var endChunk      = new EndChunk();
            var endChunkBytes = endChunk.Encode();

            outputStream.Write(endChunkBytes, 0, endChunkBytes.Length);
        }
Beispiel #6
0
        public byte[] GetChunkData()
        {
            MemoryStream byteStream = new MemoryStream();

            byte[] bytes = new byte[Blocks.Count + ((Blocks.Count / 2) * 3)];

            int index;

            foreach (Block b in Blocks)
            {
                index = b.Location.Y + ((b.Location.Z - (Location.Z * 16)) * (SizeY + 1)) + ((b.Location.X - (Location.X * 16)) * (SizeY + 1) * (SizeZ + 1));
                byteStream.Seek(index, SeekOrigin.Begin);
                byteStream.WriteByte((byte)b.Type);
            }

            byteStream.Seek(Blocks.Count, SeekOrigin.Begin);

            for (int a = 1; a <= 3; a++)
            {
                for (int b = 0; b < 16384; b++)
                {
                    switch (a)
                    {
                    case 1:
                        byteStream.WriteByte(0xFF);
                        break;

                    default:
                        byteStream.WriteByte(0xFF);
                        break;
                    }
                }
            }

            bytes = byteStream.ToArray();
            byteStream.Dispose();
            byteStream = null;

            // Compress array
            MemoryStream ms = new MemoryStream();
            ZlibStream   zs = new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.Default, true);

            zs.Write(bytes, 0, bytes.Length);
            zs.Flush();
            zs.Dispose();
            zs = null;
            byte[] finalBytes = ms.ToArray();
            ms.Dispose();
            ms    = null;
            bytes = null;
            //

            return(finalBytes);
        }
Beispiel #7
0
        public static void Decompress(byte[] compressed, int compressedOffset, int compressedSize, byte[] uncompressed, int uncompressedOffset, int uncompressedSize, CompressionMethod method, FArchive?reader = null)
        {
            using var srcStream = new MemoryStream(compressed, compressedOffset, compressedSize, false)
                  {
                      Position = 0
                  };
            switch (method)
            {
            case CompressionMethod.None:
                Buffer.BlockCopy(compressed, compressedOffset, uncompressed, uncompressedOffset, compressedSize);
                return;

            case CompressionMethod.Zlib:
                var zlib = new ZlibStream(srcStream, CompressionMode.Decompress);
                zlib.Read(uncompressed, uncompressedOffset, uncompressedSize);
                zlib.Dispose();
                return;

            case CompressionMethod.Gzip:
                var gzip = new GZipStream(srcStream, CompressionMode.Decompress);
                gzip.Read(uncompressed, uncompressedOffset, uncompressedSize);
                gzip.Dispose();
                return;

            case CompressionMethod.Oodle:
                Pro_Swapper.Swap.RawExport = compressed;
                Oodle.Decompress(compressed, compressedOffset, compressedSize, uncompressed, uncompressedOffset, uncompressedSize, reader);
                return;

            case CompressionMethod.LZ4:
                var uncompressedBuffer = new byte[uncompressedSize + 4];
                var result             = LZ4Codec.Decode(compressed, compressedOffset, compressedSize, uncompressedBuffer, 0, uncompressedBuffer.Length);
                Buffer.BlockCopy(uncompressedBuffer, 0, uncompressed, uncompressedOffset, uncompressedSize);
                if (result != uncompressedSize)
                {
                    throw new FileLoadException($"Failed to decompress LZ4 data (Expected: {uncompressedSize}, Result: {result})");
                }
                //var lz4 = LZ4Stream.Decode(srcStream);
                //lz4.Read(uncompressed, uncompressedOffset, uncompressedSize);
                //lz4.Dispose();
                return;

            default:
                if (reader != null)
                {
                    throw new UnknownCompressionMethodException(reader, $"Compression method \"{method}\" is unknown");
                }
                else
                {
                    throw new UnknownCompressionMethodException($"Compression method \"{method}\" is unknown");
                }
            }
        }
        private byte[] GetBytes()
        {
            const int binaryFormatVersion = 1;
            const int sizeVersion         = 2;
            const int sizeDictionaryBlob  = 4;
            const int sizeCountMetrics    = 4;

            const int bitsInByte = 8;
            const int byteMask   = 0xff;

            // close the compressor, we're going to dispose the compressed streams
            _closed = true;

            AppendStrings();

            // got to dispose the compressed streams to flush them all
            _stringsCompressStream.Dispose();
            _stringsCompressStream = null;
            _metricsCompressStream.Dispose();
            _metricsCompressStream = null;

            // version info + dictionary length + dictionary blob + number of metrics + metrics blob
            var completeSize = sizeVersion + sizeDictionaryBlob + _stringsBuffer.Position + sizeCountMetrics + _metricsBuffer.Position;

            // protect casts
            if (completeSize > int.MaxValue)
            {
                throw new InvalidOperationException($"Out of range: completeSize ({completeSize}).");
            }
            if (_stringsBuffer.Position > int.MaxValue)
            {
                throw new InvalidOperationException($"Out of range: _stringsBuffer.Length ({_stringsBuffer.Position}).");
            }
            if (_metricsBuffer.Position > int.MaxValue)
            {
                throw new InvalidOperationException($"Out of range: _metricsBuffer.Length ({_metricsBuffer.Position}).");
            }

            using var buffer = new MemoryStream((int)completeSize);
            var output = new DataOutputStream(buffer);

            // ReSharper disable once ShiftExpressionResultEqualsZero - well, yes
            output.WriteByte((binaryFormatVersion >> bitsInByte) & byteMask);
            output.WriteByte(binaryFormatVersion & byteMask);

            output.WriteInt((int)_stringsBuffer.Position);
            output.Write(_stringsBuffer.GetBuffer(), 0, (int)_stringsBuffer.Position);
            output.WriteInt(_count);
            output.Write(_metricsBuffer.GetBuffer(), 0, (int)_metricsBuffer.Position);

            return(buffer.ToArray());
        }
Beispiel #9
0
        /// <summary>
        /// Compresses an array of bytes.
        /// </summary>
        /// <param name="data">Data to compress.</param>
        public static byte[] Compress(byte[] data, CompressionLevel compressionLevel)
        {
            // Compress the data.
            using var resultStream = new MemoryStream();
            var zlibStream = new ZlibStream(resultStream, compressionLevel, true);

            // Write compressed data to result.
            zlibStream.Write(data, 0, data.Length);
            zlibStream.Dispose();

            // Return compressed data
            return(resultStream.ToArray());
        }
        private static void Reset(ref MemoryStream buffer, ref ZlibStream compress, ref DataOutputStream output, int size)
        {
            compress?.Dispose();

            // shrink if capacity is more than 50% larger than the estimated size
            if (buffer == null || buffer.Capacity > 3 * size / 2)
            {
                buffer?.Dispose();
                buffer = new MemoryStream(size);
            }

            buffer.Seek(0, SeekOrigin.Begin);
            compress = new ZlibStream(buffer, CompressionMode.Compress, CompressionLevel.BestSpeed, true);
            output   = new DataOutputStream(compress);
        }
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }
            _disposed = true;

            _tempBuffer?.Dispose();

            _stringsCompressStream?.Dispose();
            _stringsBuffer?.Dispose();

            _metricsCompressStream?.Dispose();
            _metricsBuffer?.Dispose();
        }
Beispiel #12
0
        /// <summary>
        /// This method is called each time when some data is received from server.
        /// </summary>
        /// <param name="sender">The source of event.</param>
        /// <param name="e">Arguments of event.</param>
        protected override void OnDataReceived(object sender, DataReceivedEventArgs e)
        {
            Assert.ArgumentNotNull(sender, "sender");
            Assert.ArgumentNotNull(e, "e");

            TotalBytesReceived += (uint)e.BytesReceived;
            byte[] data = e.GetData();

            if (!_compressionInProgress)
            {
                ProcessData(data, e.Offset, e.BytesReceived);
            }
            else
            {
                _compressedDataStream.Seek(0, SeekOrigin.Begin);
                _compressedDataStream.Write(data, e.Offset, e.BytesReceived);
                _compressedDataStream.SetLength(e.BytesReceived);
                _compressedDataStream.Seek(0, SeekOrigin.Begin);

                try
                {
                    int bytesDecompresed = 1;
                    while (bytesDecompresed > 0)
                    {
                        bytesDecompresed = _zlibDecompressionStream.Read(_unpackBuffer, 0, _unpackBuffer.Length);

                        if (bytesDecompresed < 0)
                        {
                            _compressionInProgress = false;
                            _zlibDecompressionStream.Dispose();
                            _zlibDecompressionStream = null;
                            return;
                        }

                        BytesDecompressed += (uint)bytesDecompresed;
                        ProcessData(_unpackBuffer, 0, bytesDecompresed);
                    }
                }
                catch (Exception ex)
                {
                    ErrorLogger.Instance.Write(string.Format("Error data decompressed: offset = {0}, bytesCount = {1}, data = {2}\r\n\r\n{3}\r\n{4}",
                                                             e.Offset, e.BytesReceived, Encoding.Default.GetString(data), ex.Message, ex.StackTrace));
                    Dispose(true);
                    HandleException(new NetworkErrorEventArgs(ex));
                }
            }
        }
Beispiel #13
0
 public BinaryReader GetReaderForPotentiallyCompressedBlock(Context context, BinaryReader currentReader, BlockHeader block)
 {
     if (block.IsCompressed)
     {
         ZlibStream zlibStream = null;
         try {
             zlibStream = new ZlibStream(currentReader.BaseStream, System.IO.Compression.CompressionMode.Decompress,
                                         leaveOpen: currentReader == context.Reader.BaseReader);
             return(new BinaryReader(zlibStream));
         } catch {
             if (zlibStream != null)
             {
                 zlibStream.Dispose();
             }
             throw;
         }
     }
     return(currentReader);
 }
Beispiel #14
0
        public static void Decompress(byte[] compressed, int compressedOffset, int compressedSize, byte[] uncompressed, int uncompressedOffset, int uncompressedSize, CompressionMethod method, FArchive?reader = null)
        {
            using var srcStream = new MemoryStream(compressed, compressedOffset, compressedSize, false)
                  {
                      Position = 0
                  };
            switch (method)
            {
            case CompressionMethod.None:
                Buffer.BlockCopy(compressed, compressedOffset, uncompressed, uncompressedOffset, compressedSize);
                return;

            case CompressionMethod.Zlib:
                var zlib = new ZlibStream(srcStream, CompressionMode.Decompress);
                zlib.Read(uncompressed, uncompressedOffset, uncompressedSize);
                zlib.Dispose();
                return;

            case CompressionMethod.Gzip:
                var gzip = new GZipStream(srcStream, CompressionMode.Decompress);
                gzip.Read(uncompressed, uncompressedOffset, uncompressedSize);
                gzip.Dispose();
                return;

            case CompressionMethod.Oodle:
                Oodle.Decompress(compressed, compressedOffset, compressedSize, uncompressed, uncompressedOffset, uncompressedSize, reader);
                return;

            default:
                if (reader != null)
                {
                    throw new OodleException(reader, $"Compression method \"{method}\" is unknown");
                }
                else
                {
                    throw new OodleException($"Compression method \"{method}\" is unknown");
                }
            }
        }
Beispiel #15
0
        /// <summary>
        /// Compresses the byte array using zlib compression. The entire byte array is compressed.
        /// </summary>
        /// <param name="algorithm">The compression algorithm to use when compressing. Valid values are defined as constants in the CompressionAlgorithm class. The default is to use zlib format.</param>
        /// <remarks>
        /// After the call, the Length property of the ByteArray is set to the new length. The position property is set to the end of the byte array.
        /// </remarks>
        public void Compress(string algorithm)
        {
            ValidationUtils.ArgumentConditionTrue(algorithm == CompressionAlgorithm.Deflate || algorithm == CompressionAlgorithm.Zlib, "algorithm", "Invalid parameter");
#if SILVERLIGHT
            throw new NotSupportedException();
#else
            if (algorithm == CompressionAlgorithm.Deflate)
            {
                byte[]        buffer        = _memoryStream.ToArray();
                MemoryStream  ms            = new MemoryStream();
                DeflateStream deflateStream = new DeflateStream(ms, CompressionMode.Compress, true);
                deflateStream.Write(buffer, 0, buffer.Length);
                deflateStream.Close();
                _memoryStream.Close();
                _memoryStream = ms;
                AMFReader amfReader = new AMFReader(_memoryStream);
                AMFWriter amfWriter = new AMFWriter(_memoryStream);
                _dataOutput = new DataOutput(amfWriter);
                _dataInput  = new DataInput(amfReader);
            }
            if (algorithm == CompressionAlgorithm.Zlib)
            {
                byte[]       buffer     = _memoryStream.ToArray();
                MemoryStream ms         = new MemoryStream();
                ZlibStream   zlibStream = new ZlibStream(ms, CompressionMode.Compress, true);
                zlibStream.Write(buffer, 0, buffer.Length);
                zlibStream.Flush();
                zlibStream.Close();
                zlibStream.Dispose();
                _memoryStream.Close();
                _memoryStream = ms;
                AMFReader amfReader = new AMFReader(_memoryStream);
                AMFWriter amfWriter = new AMFWriter(_memoryStream);
                _dataOutput = new DataOutput(amfWriter);
                _dataInput  = new DataInput(amfReader);
            }
#endif
        }