public byte[] ExtractBytes(MAS2File f) { BinaryReader reader = new BinaryReader(System.IO.File.OpenRead(this.mas2_file)); reader.BaseStream.Seek(f.FileOffset, SeekOrigin.Begin); byte[] RawData = reader.ReadBytes((int)f.CompressedSize); reader.Close(); if (f.IsCompressed) { byte[] OutputData = new byte[f.UncompressedSize]; // MAS2 compression consists of a simple inflate/deflate process. ZlibCodec codec = new ZlibCodec(CompressionMode.Decompress); codec.InitializeInflate(); codec.InputBuffer = RawData; codec.NextIn = 0; codec.AvailableBytesIn = RawData.Length; codec.OutputBuffer = OutputData; codec.NextOut = 0; codec.AvailableBytesOut = OutputData.Length; codec.Inflate(FlushType.None); codec.EndInflate(); return(OutputData); } else { return(RawData); } }
private static byte[] ZlibCodecDecompress(byte[] data) { int buffer_size = 0x800; byte[] buffer = new byte[buffer_size]; bool flag = false; using (MemoryStream stream = new MemoryStream()) { ZlibCodec codec = new ZlibCodec(); codec.InitializeInflate(flag); codec.InputBuffer = data; codec.AvailableBytesIn = data.Length; codec.NextIn = 0; codec.OutputBuffer = buffer; FlushType[] typeArray1 = new FlushType[2]; typeArray1[1] = FlushType.Finish; foreach (FlushType type in typeArray1) { int count = 0; do { codec.AvailableBytesOut = buffer_size; codec.NextOut = 0; codec.Inflate(type); count = buffer_size - codec.AvailableBytesOut; if (count > 0) { stream.Write(buffer, 0, count); } }while (((type == FlushType.None) && ((codec.AvailableBytesIn != 0) || (codec.AvailableBytesOut == 0))) || ((type == FlushType.Finish) && (count != 0))); } codec.EndInflate(); return(stream.ToArray()); } }
public static byte[] Inflate(byte[] data, int outputSize) { byte[] output = new Byte[outputSize]; using (MemoryStream ms = new MemoryStream()) { ZlibCodec compressor = new ZlibCodec(); compressor.InitializeInflate(false); compressor.InputBuffer = data; compressor.AvailableBytesIn = data.Length; compressor.NextIn = 0; compressor.OutputBuffer = output; foreach (var f in new FlushType[] { FlushType.None, FlushType.Finish }) { int bytesToWrite = 0; do { compressor.AvailableBytesOut = outputSize; compressor.NextOut = 0; compressor.Inflate(f); bytesToWrite = outputSize - compressor.AvailableBytesOut; if (bytesToWrite > 0) { ms.Write(output, 0, bytesToWrite); } }while ((f == FlushType.None && (compressor.AvailableBytesIn != 0 || compressor.AvailableBytesOut == 0)) || (f == FlushType.Finish && bytesToWrite != 0)); } compressor.EndInflate(); return(ms.ToArray()); } }
private static PacketCaptureTestEntry RebuildEntryAsUncompressed(PacketCaptureTestEntry entry) { if (entry.OpCode == NetworkOperationCode.SMSG_COMPRESSED_UPDATE_OBJECT) { //Skip the opcode int decompressedSize = entry.BinaryData.Reinterpret <int>(2); byte[] newBytes = new byte[decompressedSize + 2]; // +2 for opcode ZlibCodec stream = new ZlibCodec(CompressionMode.Decompress) { InputBuffer = entry.BinaryData, NextIn = 2 + 4, //opcode + size AvailableBytesIn = entry.BinaryData.Length, OutputBuffer = newBytes, NextOut = 2, AvailableBytesOut = decompressedSize }; stream.InitializeInflate(true); stream.Inflate(FlushType.None); stream.Inflate(FlushType.Finish); stream.EndInflate(); ((short)(NetworkOperationCode.SMSG_UPDATE_OBJECT)).Reinterpret(newBytes, 0); entry = new PacketCaptureTestEntry(NetworkOperationCode.SMSG_UPDATE_OBJECT, newBytes, entry.FileName); } return(entry); }
public Int32 Decompress(byte[] InData, UInt32 InLength, byte[] OutData, UInt32 OutLength, UInt32 InStartIndex = 0, UInt32 OutStartIndex = 0) { if (InData[0 + InStartIndex] != 120 /*'x'*/) { return(0); } _zlib.InitializeInflate(true); _zlib.AvailableBytesIn = Convert.ToInt32(InLength); _zlib.InputBuffer = InData; _zlib.OutputBuffer = OutData; _zlib.AvailableBytesOut = Convert.ToInt32(OutLength); _zlib.NextIn = Convert.ToInt32(InStartIndex); _zlib.NextOut = Convert.ToInt32(OutStartIndex); _zlib.Inflate(FlushType.Finish); int outBytes = Convert.ToInt32(_zlib.TotalBytesOut); if (outBytes != 0) { _zlib.EndInflate(); } return(outBytes); }
public static byte[] Inflate(byte[] compressed) { int bufferSize = 1024 * 64; byte[] buffer = new byte[bufferSize]; ZlibCodec decompressor = new ZlibCodec(); MemoryStream ms = new MemoryStream(); int rc = decompressor.InitializeInflate(); if (rc != ZlibConstants.Z_OK) { throw new Exception("init inflate: " + decompressor.Message); } decompressor.InputBuffer = compressed; decompressor.NextIn = 0; decompressor.AvailableBytesIn = compressed.Length; decompressor.OutputBuffer = buffer; // pass 1: inflate do { decompressor.NextOut = 0; decompressor.AvailableBytesOut = bufferSize; rc = decompressor.Inflate(FlushType.None); if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) { throw new Exception("inflating: " + decompressor.Message); } ms.Write(decompressor.OutputBuffer, 0, bufferSize - decompressor.AvailableBytesOut); }while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0); // pass 2: finish and flush do { decompressor.NextOut = 0; decompressor.AvailableBytesOut = bufferSize; rc = decompressor.Inflate(FlushType.Finish); if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK) { throw new Exception("inflating: " + decompressor.Message); } if (bufferSize - decompressor.AvailableBytesOut > 0) { ms.Write(decompressor.OutputBuffer, 0, bufferSize - decompressor.AvailableBytesOut); } } while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0); decompressor.EndInflate(); return(ms.ToArray()); }
private void end() { if (z == null) { return; } _z.EndInflate(); _z = null; }
public Packet Inflate(int inflatedSize, bool keepStream = true) { var arr = ReadToEnd(); var newarr = new byte[inflatedSize]; if (ClientVersion.RemovedInVersion(ClientVersionBuild.V4_3_0_15005)) { keepStream = false; } if (keepStream) { if (!SessionHandler.z_streams.ContainsKey(ConnectionIndex)) { SessionHandler.z_streams[ConnectionIndex] = new ZlibCodec(CompressionMode.Decompress); } SessionHandler.z_streams[ConnectionIndex].InputBuffer = arr; SessionHandler.z_streams[ConnectionIndex].NextIn = 0; SessionHandler.z_streams[ConnectionIndex].AvailableBytesIn = arr.Length; SessionHandler.z_streams[ConnectionIndex].OutputBuffer = newarr; SessionHandler.z_streams[ConnectionIndex].NextOut = 0; SessionHandler.z_streams[ConnectionIndex].AvailableBytesOut = inflatedSize; SessionHandler.z_streams[ConnectionIndex].Inflate(FlushType.Sync); } else { /*try * { * var inflater = new Inflater(true); * inflater.SetInput(arr, 0, arr.Length); * inflater.Inflate(newarr, 0, inflatedSize); * } * catch (ICSharpCode.SharpZipLib.SharpZipBaseException) * { * var inflater = new Inflater(true); * inflater.SetInput(arr, 0, arr.Length); * inflater.Inflate(newarr, 0, inflatedSize); * }*/ ZlibCodec stream = new ZlibCodec(CompressionMode.Decompress); stream.InputBuffer = arr; stream.NextIn = 0; stream.AvailableBytesIn = arr.Length; stream.OutputBuffer = newarr; stream.NextOut = 0; stream.AvailableBytesOut = inflatedSize; stream.Inflate(FlushType.None); stream.Inflate(FlushType.Finish); stream.EndInflate(); } // Cannot use "using" here var pkt = new Packet(newarr, Opcode, Time, Direction, Number, Writer, FileName); pkt.ConnectionIndex = ConnectionIndex; return(pkt); }
public static void Cleanup() { if (s_Decompressor == null) { return; } s_Decompressor.EndInflate(); s_Decompressor = null; }
public Packet Inflate(int inflatedSize, bool keepStream = true) { var arr = ReadToEnd(); var newarr = new byte[inflatedSize]; if (ClientVersion.RemovedInVersion(ClientVersionBuild.V4_3_0_15005)) { keepStream = false; } if (keepStream) { int idx = ConnectionIndex; while (!TryInflate(inflatedSize, idx, arr, ref newarr) && idx <= 4) { idx += 1; } } else { /*try * { * var inflater = new Inflater(true); * inflater.SetInput(arr, 0, arr.Length); * inflater.Inflate(newarr, 0, inflatedSize); * } * catch (ICSharpCode.SharpZipLib.SharpZipBaseException) * { * var inflater = new Inflater(true); * inflater.SetInput(arr, 0, arr.Length); * inflater.Inflate(newarr, 0, inflatedSize); * }*/ var stream = new ZlibCodec(CompressionMode.Decompress) { InputBuffer = arr, NextIn = 0, AvailableBytesIn = arr.Length, OutputBuffer = newarr, NextOut = 0, AvailableBytesOut = inflatedSize }; stream.Inflate(FlushType.None); stream.Inflate(FlushType.Finish); stream.EndInflate(); } // Cannot use "using" here var pkt = new Packet(newarr, Opcode, Time, Direction, Number, Writer, FileName) { ConnectionIndex = ConnectionIndex }; return(pkt); }
/// <summary> /// Decompressed (inflates) a compressed byte array using the Inflate algorithm. /// </summary> /// <param name="compressedData">The deflate-compressed data</param> /// <param name="dictionary">The dictionary originally used to compress the data, or null if no dictionary was used.</param> /// <returns>The uncompressed data</returns> internal static byte[] ZlibDecompressWithDictionary(byte[] compressedData, byte[] dictionary) { using (var ms = new MemoryStream()) { const int bufferSize = 256; var buffer = new byte[bufferSize]; var codec = new ZlibCodec { InputBuffer = compressedData, NextIn = 0, AvailableBytesIn = compressedData.Length }; codec.AssertOk("InitializeInflate", codec.InitializeInflate(false)); if (dictionary != null) { codec.AssertOk("SetDictionary", codec.SetDictionary(dictionary)); } codec.OutputBuffer = buffer; while (true) { codec.NextOut = 0; codec.AvailableBytesOut = bufferSize; var inflateReturnCode = codec.Inflate(FlushType.None); var bytesToWrite = bufferSize - codec.AvailableBytesOut; ms.Write(buffer, 0, bytesToWrite); if (inflateReturnCode == ZlibConstants.Z_STREAM_END) { break; } else if (inflateReturnCode == ZlibConstants.Z_NEED_DICT && dictionary != null) { //implies bytesToWrite was 0 var dictionaryAdler32 = ((int)Adler.Adler32(1u, dictionary, 0, dictionary.Length)); if (codec.Adler32 != dictionaryAdler32) { throw new InvalidOperationException("Compressed data is requesting a dictionary with adler32 " + codec.Adler32 + ", but the dictionary is actually " + dictionaryAdler32); } codec.AssertOk("SetDictionary", codec.SetDictionary(dictionary)); } else { codec.AssertOk("Inflate", inflateReturnCode); } } codec.AssertOk("EndInflate", codec.EndInflate()); return(ms.ToArray()); } }
public ChannelSubBlock(BufferedBinaryReader br, PSPCompression compression, ushort majorVersion) { this.chunkSize = majorVersion > PSPConstants.majorVersion5 ? br.ReadUInt32() : 0U; this.compressedChannelLength = br.ReadUInt32(); this.uncompressedChannelLength = br.ReadUInt32(); this.bitmapType = (PSPDIBType)br.ReadUInt16(); this.channelType = (PSPChannelType)br.ReadUInt16(); this.channelData = null; long dif = (long)this.chunkSize - Version6HeaderSize; if (dif > 0 && majorVersion > PSPConstants.majorVersion5) { br.Position += dif; } if (this.compressedChannelLength > 0U) { switch (compression) { case PSPCompression.None: this.channelData = br.ReadBytes((int)this.compressedChannelLength); break; case PSPCompression.RLE: this.channelData = RLE.Decompress(br.ReadBytes((int)this.compressedChannelLength), this.uncompressedChannelLength); break; case PSPCompression.LZ77: byte[] compressedData = br.ReadBytes((int)this.compressedChannelLength); this.channelData = new byte[this.uncompressedChannelLength]; ZlibCodec codec = new ZlibCodec { AvailableBytesIn = (int)this.compressedChannelLength, AvailableBytesOut = (int)this.uncompressedChannelLength, InputBuffer = compressedData, OutputBuffer = this.channelData }; codec.InitializeInflate(); int status = codec.Inflate(FlushType.Finish); codec.EndInflate(); if (status != ZlibConstants.Z_OK && status != ZlibConstants.Z_STREAM_END) { throw new ZlibException(codec.Message); } break; } } }
/// <summary> /// Inflates (decompressed) a single block of data /// </summary> /// <param name="deflatedBytes">The deflated (compressed) data bytes</param> /// <param name="inflatedSize">The size of the bytes once inflated</param> /// <param name="inflatedBytes">The inflated (decompressed) data bytes</param> /// <param name="logger">The logger for debug output</param> /// <returns>Whether or not the inflation was successful</returns> private static bool InflateBlock(byte[] deflatedBytes, int inflatedSize, out byte[] inflatedBytes, ILogger logger) { var output = new byte[inflatedSize]; using (var memoryStream = new MemoryStream()) { var zlibCodec = new ZlibCodec(); zlibCodec.InitializeInflate(true); zlibCodec.InputBuffer = deflatedBytes; zlibCodec.AvailableBytesIn = deflatedBytes.Length; zlibCodec.NextIn = 0; zlibCodec.OutputBuffer = output; foreach (FlushType f in new[] { FlushType.None, FlushType.Finish }) { int bytesToWrite; do { zlibCodec.AvailableBytesOut = inflatedSize; zlibCodec.NextOut = 0; try { zlibCodec.Inflate(f); } catch (Exception e) { inflatedBytes = null; logger.LogError("Exception caught while inflating bytes: " + e); return(false); } bytesToWrite = inflatedSize - zlibCodec.AvailableBytesOut; if (bytesToWrite > 0) { memoryStream.Write(output, 0, bytesToWrite); } } while (f == FlushType.None && (zlibCodec.AvailableBytesIn != 0 || zlibCodec.AvailableBytesOut == 0) || f == FlushType.Finish && bytesToWrite != 0); } zlibCodec.EndInflate(); inflatedBytes = output; return(true); } }
/// <summary> /// InflateBufferWithPureZlib /// </summary> /// <param name="compressedBytes"></param> /// <param name="length"></param> /// <param name="outs"></param> /// <exception cref="Exception"></exception> public static void InflateBufferWithPureZlib(byte[] compressedBytes, int length, Stream outs) { //int bufferSize = 1024; var buffer = new byte[BufferSize]; var decompressor = new ZlibCodec(); var rc = decompressor.InitializeInflate(); decompressor.InputBuffer = compressedBytes; decompressor.NextIn = 0; decompressor.AvailableBytesIn = length; decompressor.OutputBuffer = buffer; // pass 1: inflate do { decompressor.NextOut = 0; decompressor.AvailableBytesOut = buffer.Length; rc = decompressor.Inflate(FlushType.None); if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) { throw new Exception("inflating: " + decompressor.Message); } outs.Write(decompressor.OutputBuffer, 0, buffer.Length - decompressor.AvailableBytesOut); } while (decompressor.AvailableBytesIn > 0 && decompressor.AvailableBytesOut == 0); // pass 2: finish and flush do { decompressor.NextOut = 0; decompressor.AvailableBytesOut = buffer.Length; rc = decompressor.Inflate(FlushType.Finish); if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK) { throw new Exception("inflating: " + decompressor.Message); } if (buffer.Length - decompressor.AvailableBytesOut > 0) { outs.Write(buffer, 0, buffer.Length - decompressor.AvailableBytesOut); } } while (decompressor.AvailableBytesIn > 0 && decompressor.AvailableBytesOut == 0); decompressor.EndInflate(); }
private string ZlibCodecDecompress(byte[] compressed) { int outputSize = 2048; byte[] output = new Byte[outputSize]; // If you have a ZLIB stream, set this to true. If you have // a bare DEFLATE stream, set this to false. bool expectRfc1950Header = false; using (MemoryStream ms = new MemoryStream()) { ZlibCodec compressor = new ZlibCodec(); compressor.InitializeInflate(expectRfc1950Header); compressor.InputBuffer = compressed; compressor.AvailableBytesIn = compressed.Length; compressor.NextIn = 0; compressor.OutputBuffer = output; foreach (var f in new FlushType[] { FlushType.None, FlushType.Finish }) { int bytesToWrite = 0; do { compressor.AvailableBytesOut = outputSize; compressor.NextOut = 0; compressor.Inflate(f); bytesToWrite = outputSize - compressor.AvailableBytesOut; if (bytesToWrite > 0) { ms.Write(output, 0, bytesToWrite); } }while ((f == FlushType.None && (compressor.AvailableBytesIn != 0 || compressor.AvailableBytesOut == 0)) || (f == FlushType.Finish && bytesToWrite != 0)); } compressor.EndInflate(); return(UTF8Encoding.UTF8.GetString(ms.ToArray())); } }
private void Run() { int rc; int bufferSize = 40000; byte[] compressedBytes = new byte[bufferSize]; byte[] decompressedBytes = new byte[bufferSize]; ZlibCodec compressingStream = new ZlibCodec(); rc = compressingStream.InitializeDeflate(CompressionLevel.LEVEL9_BEST_COMPRESSION); CheckForError(compressingStream, rc, "InitializeDeflate"); string dictionaryWord = "hello "; byte[] dictionary = System.Text.ASCIIEncoding.ASCII.GetBytes(dictionaryWord); string TextToCompress = "hello, hello! How are you, Joe? "; byte[] BytesToCompress = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress); rc = compressingStream.SetDictionary(dictionary); CheckForError(compressingStream, rc, "SetDeflateDictionary"); long dictId = compressingStream.Adler32; compressingStream.OutputBuffer = compressedBytes; compressingStream.NextOut = 0; compressingStream.AvailableBytesOut = bufferSize; compressingStream.InputBuffer = BytesToCompress; compressingStream.NextIn = 0; compressingStream.AvailableBytesIn = BytesToCompress.Length; rc = compressingStream.Deflate(ZlibConstants.Z_FINISH); if (rc != ZlibConstants.Z_STREAM_END) { System.Console.Out.WriteLine("deflate should report Z_STREAM_END"); System.Environment.Exit(1); } rc = compressingStream.EndDeflate(); CheckForError(compressingStream, rc, "deflateEnd"); ZlibCodec decompressingStream = new ZlibCodec(); decompressingStream.InputBuffer = compressedBytes; decompressingStream.NextIn = 0; decompressingStream.AvailableBytesIn = bufferSize; rc = decompressingStream.InitializeInflate(); CheckForError(decompressingStream, rc, "inflateInit"); decompressingStream.OutputBuffer = decompressedBytes; decompressingStream.NextOut = 0; decompressingStream.AvailableBytesOut = decompressedBytes.Length; while (true) { rc = decompressingStream.Inflate(ZlibConstants.Z_NO_FLUSH); if (rc == ZlibConstants.Z_STREAM_END) { break; } if (rc == ZlibConstants.Z_NEED_DICT) { if ((int)decompressingStream.Adler32 != (int)dictId) { System.Console.Out.WriteLine("unexpected dictionary"); System.Environment.Exit(1); } rc = decompressingStream.SetDictionary(dictionary); } CheckForError(decompressingStream, rc, "inflate with dict"); } rc = decompressingStream.EndInflate(); CheckForError(decompressingStream, rc, "EndInflate"); int j = 0; for (; j < decompressedBytes.Length; j++) { if (decompressedBytes[j] == 0) { break; } } var result = System.Text.ASCIIEncoding.ASCII.GetString(decompressedBytes, 0, j); Console.WriteLine("orig length: {0}", TextToCompress.Length); Console.WriteLine("compressed length: {0}", compressingStream.TotalBytesOut); Console.WriteLine("decompressed length: {0}", decompressingStream.TotalBytesOut); Console.WriteLine("result length: {0}", result.Length); Console.WriteLine("result of inflate:\n{0}", result); }
private static SwStorageChunkInfo ReadChunk(SwStorage storage, byte[] blob, int startIndex) { // within a block before offset 0x12 the bytes are yet unknown int index = startIndex + 0x12; uint compressedSize = GetUInt(blob, index); index += 4; uint uncompressedSize = GetUInt(blob, index); index += 4; int nameSize = (int)GetUInt(blob, index); index += 4; int namestart = index; if (namestart + nameSize > blob.Length) { // happens if we try to read the content table return(null); } // the stream names are scrambled ;-) byte[] unrolName = new byte[nameSize]; for (; index < namestart + nameSize; index++) { byte unroledByte = Rol(blob[index], (int)storage.Key); unrolName[index - namestart] = unroledByte; } string chunkName = Encoding.UTF8.GetString(unrolName); if (string.IsNullOrEmpty(chunkName)) { chunkName = "un_" + Guid.NewGuid().ToString(); } int compressedDataStart = namestart + nameSize; SwStorageChunkInfo chunkInfo = new SwStorageChunkInfo(); chunkInfo.ChunkOffset = (uint)startIndex; chunkInfo.CompressedSize = compressedSize; chunkInfo.StartCompressedBlock = compressedDataStart; chunkInfo.ChunkName = chunkName; chunkInfo.HeaderLength = compressedDataStart - startIndex; if (uncompressedSize > 0) { byte[] uncompressedData = new byte[uncompressedSize]; ZlibCodec inflator = new ZlibCodec(); inflator.InitializeInflate(false); inflator.InputBuffer = blob; inflator.AvailableBytesIn = (int)compressedSize; inflator.AvailableBytesOut = (int)uncompressedSize; inflator.NextIn = compressedDataStart; inflator.OutputBuffer = uncompressedData; inflator.NextOut = 0; inflator.Inflate(FlushType.Full); inflator.EndInflate(); chunkInfo.Chunk = uncompressedData; } else { chunkInfo.Chunk = new byte[0]; } return(chunkInfo); }
private void Run() { int rc; int comprLen = 40000; int uncomprLen = comprLen; byte[] CompressedBytes = new byte[comprLen]; byte[] DecompressedBytes = new byte[uncomprLen]; string TextToCompress = "This is the text that will be compressed."; byte[] BytesToCompress = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress); ZlibCodec compressor = new ZlibCodec(CompressionMode.Compress); compressor.InputBuffer = BytesToCompress; compressor.NextIn = 0; compressor.OutputBuffer = CompressedBytes; compressor.NextOut = 0; compressor.AvailableBytesIn = 3; compressor.AvailableBytesOut = CompressedBytes.Length; rc = compressor.Deflate(ZlibConstants.Z_FULL_FLUSH); CheckForError(compressor, rc, "Deflate"); CompressedBytes[3]++; // force an error in first compressed block // dinoch compressor.AvailableBytesIn = TextToCompress.Length - 3; rc = compressor.Deflate(ZlibConstants.Z_FINISH); if (rc != ZlibConstants.Z_STREAM_END) { CheckForError(compressor, rc, "Deflate"); } rc = compressor.EndDeflate(); CheckForError(compressor, rc, "EndDeflate"); comprLen = (int)(compressor.TotalBytesOut); ZlibCodec decompressor = new ZlibCodec(CompressionMode.Decompress); decompressor.InputBuffer = CompressedBytes; decompressor.NextIn = 0; decompressor.AvailableBytesIn = 2; decompressor.OutputBuffer = DecompressedBytes; decompressor.NextOut = 0; decompressor.AvailableBytesOut = DecompressedBytes.Length; rc = decompressor.Inflate(ZlibConstants.Z_NO_FLUSH); CheckForError(decompressor, rc, "Inflate"); decompressor.AvailableBytesIn = CompressedBytes.Length - 2; rc = decompressor.SyncInflate(); CheckForError(decompressor, rc, "SyncInflate"); bool gotException = false; try { rc = decompressor.Inflate(ZlibConstants.Z_FINISH); } catch (ZlibException ex1) { Console.WriteLine("Got Expected Exception: " + ex1); gotException = true; } if (!gotException) { System.Console.Out.WriteLine("inflate should report DATA_ERROR"); /* Because of incorrect adler32 */ System.Environment.Exit(1); } rc = decompressor.EndInflate(); CheckForError(decompressor, rc, "EndInflate"); int j = 0; for (; j < DecompressedBytes.Length; j++) { if (DecompressedBytes[j] == 0) { break; } } var result = System.Text.ASCIIEncoding.ASCII.GetString(DecompressedBytes, 0, j); Console.WriteLine("orig length: {0}", TextToCompress.Length); Console.WriteLine("compressed length: {0}", compressor.TotalBytesOut); Console.WriteLine("uncompressed length: {0}", decompressor.TotalBytesOut); Console.WriteLine("result length: {0}", result.Length); Console.WriteLine("result of inflate:\n(Thi){0}", result); }
private void Run() { int rc; int j; int bufferSize = 40000; byte[] compressedBytes = new byte[bufferSize]; byte[] bufferToCompress = new byte[bufferSize]; byte[] decompressedBytes = new byte[bufferSize]; ZlibCodec compressingStream = new ZlibCodec(); rc = compressingStream.InitializeDeflate(CompressionLevel.BestSpeed); CheckForError(compressingStream, rc, "InitializeDeflate"); compressingStream.OutputBuffer = compressedBytes; compressingStream.NextOut = 0; compressingStream.AvailableBytesOut = compressedBytes.Length; // At this point, bufferToCompress is all zeroes, so it should compress // very well: compressingStream.InputBuffer = bufferToCompress; compressingStream.AvailableBytesIn = bufferToCompress.Length; rc = compressingStream.Deflate(FlushType.None); CheckForError(compressingStream, rc, "deflate"); if (compressingStream.AvailableBytesIn != 0) { System.Console.Out.WriteLine("deflate not greedy"); System.Environment.Exit(1); } Console.WriteLine("Stage 1: uncompressed bytes in so far: {0,6}", compressingStream.TotalBytesIn); Console.WriteLine(" compressed bytes out so far: {0,6}", compressingStream.TotalBytesOut); // Feed in already compressed data and switch to no compression: compressingStream.SetDeflateParams(CompressionLevel.None, CompressionStrategy.Default); compressingStream.InputBuffer = compressedBytes; compressingStream.NextIn = 0; compressingStream.AvailableBytesIn = bufferSize / 2; // why? - for fun rc = compressingStream.Deflate(FlushType.None); CheckForError(compressingStream, rc, "Deflate"); Console.WriteLine("Stage 2: uncompressed bytes in so far: {0,6}", compressingStream.TotalBytesIn); Console.WriteLine(" compressed bytes out so far: {0,6}", compressingStream.TotalBytesOut); // Insert data into bufferToCompress, and Switch back to compressing mode: System.Random rnd = new Random(); for (int i = 0; i < bufferToCompress.Length / 1000; i++) { byte b = (byte)rnd.Next(); int n = 500 + rnd.Next(500); for (j = 0; j < n; j++) { bufferToCompress[j + i] = b; } i += j - 1; } compressingStream.SetDeflateParams(CompressionLevel.BestCompression, CompressionStrategy.Filtered); compressingStream.InputBuffer = bufferToCompress; compressingStream.NextIn = 0; compressingStream.AvailableBytesIn = bufferToCompress.Length; rc = compressingStream.Deflate(FlushType.None); CheckForError(compressingStream, rc, "Deflate"); Console.WriteLine("Stage 3: uncompressed bytes in so far: {0,6}", compressingStream.TotalBytesIn); Console.WriteLine(" compressed bytes out so far: {0,6}", compressingStream.TotalBytesOut); rc = compressingStream.Deflate(FlushType.Finish); if (rc != ZlibConstants.Z_STREAM_END) { Console.WriteLine("deflate reported {0}, should report Z_STREAM_END", rc); Environment.Exit(1); } rc = compressingStream.EndDeflate(); CheckForError(compressingStream, rc, "EndDeflate"); Console.WriteLine("Stage 4: uncompressed bytes in (final): {0,6}", compressingStream.TotalBytesIn); Console.WriteLine(" compressed bytes out (final): {0,6}", compressingStream.TotalBytesOut); ZlibCodec decompressingStream = new ZlibCodec(CompressionMode.Decompress); decompressingStream.InputBuffer = compressedBytes; decompressingStream.NextIn = 0; decompressingStream.AvailableBytesIn = bufferSize; // upon inflating, we overwrite the decompressedBytes buffer repeatedly while (true) { decompressingStream.OutputBuffer = decompressedBytes; decompressingStream.NextOut = 0; decompressingStream.AvailableBytesOut = decompressedBytes.Length; rc = decompressingStream.Inflate(FlushType.None); if (rc == ZlibConstants.Z_STREAM_END) { break; } CheckForError(decompressingStream, rc, "inflate large"); } rc = decompressingStream.EndInflate(); CheckForError(decompressingStream, rc, "EndInflate"); if (decompressingStream.TotalBytesOut != 2 * decompressedBytes.Length + bufferSize / 2) { System.Console.WriteLine("bad large inflate: " + decompressingStream.TotalBytesOut); System.Environment.Exit(1); } for (j = 0; j < decompressedBytes.Length; j++) { if (decompressedBytes[j] == 0) { break; } } Console.WriteLine("compressed length: {0}", compressingStream.TotalBytesOut); Console.WriteLine("decompressed length (expected): {0}", 2 * decompressedBytes.Length + bufferSize / 2); Console.WriteLine("decompressed length (actual) : {0}", decompressingStream.TotalBytesOut); }
public static void Can_Deserialize_Captures_To_GamePacketPayloads(PacketCaptureTestEntry entry) { Console.WriteLine($"Entry Size: {entry.BinaryData.Length} OpCode: {entry.OpCode}"); //TODO: Test compression another time. if (entry.OpCode == NetworkOperationCode.SMSG_COMPRESSED_UPDATE_OBJECT) { //Skip the opcode int decompressedSize = entry.BinaryData.Reinterpret <int>(2); byte[] newBytes = new byte[decompressedSize + 2]; // +2 for opcode ZlibCodec stream = new ZlibCodec(CompressionMode.Decompress) { InputBuffer = entry.BinaryData, NextIn = 2 + 4, //opcode + size AvailableBytesIn = entry.BinaryData.Length, OutputBuffer = newBytes, NextOut = 2, AvailableBytesOut = decompressedSize }; stream.Inflate(FlushType.None); stream.Inflate(FlushType.Finish); stream.EndInflate(); ((short)(NetworkOperationCode.SMSG_UPDATE_OBJECT)).Reinterpret(newBytes, 0); entry = new PacketCaptureTestEntry(NetworkOperationCode.SMSG_UPDATE_OBJECT, newBytes, entry.FileName); } //arrange SerializerService serializer = Serializer; GamePacketPayload payload; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); //act try { payload = serializer.Deserialize <GamePacketPayload>(entry.BinaryData); } catch (Exception e) { Console.WriteLine($"Critical failure. Cannot deserialize File: {entry.FileName} FileSize: {entry.BinaryData.Length} \n\n Exception: {e.Message} Stack: {e.StackTrace}"); return; } finally { stopwatch.Stop(); } Console.WriteLine($"Serialization time in ms: {stopwatch.ElapsedMilliseconds}"); foreach (ObjectUpdateBlock block in ((IObjectUpdatePayload)payload).UpdateBlocks.Items) { Console.WriteLine($"Encountered: {block.GetType().Name} Block Type: {block.UpdateType}"); } //assert if (payload == null) { Console.WriteLine($"Resulting capture capture deserialization attempt null for File: {entry.FileName}"); } //We should have deserialized it. We want to make sure the opcode matches if (entry.OpCode != payload.OperationCode) { Console.WriteLine($"Mismatched {nameof(NetworkOperationCode)} on packet capture File: {entry.FileName}. Expected: {entry.OpCode} Was: {payload.OperationCode}"); } }