public ZlibStream(Stream inner, Inflater inflater, int buffSize) { _innerStream = inner; _in = inflater; _inBuff = new byte[buffSize]; _outBuff = _inBuff; _out = new Deflater(); }
void Inflate(MemoryStream ms, byte[] original, int level, bool zlib) { ms.Seek(0, SeekOrigin.Begin); Inflater inflater = new Inflater(!zlib); InflaterInputStream inStream = new InflaterInputStream(ms, inflater); byte[] buf2 = new byte[original.Length]; int currentIndex = 0; int count = buf2.Length; try { while (true) { int numRead = inStream.Read(buf2, currentIndex, count); if (numRead <= 0) { break; } currentIndex += numRead; count -= numRead; } } catch(Exception ex) { Console.WriteLine("Unexpected exception - '{0}'", ex.Message); throw; } if ( currentIndex != original.Length ) { Console.WriteLine("Original {0}, new {1}", original.Length, currentIndex); Assert.Fail("Lengths different"); } for (int i = 0; i < original.Length; ++i) { if ( buf2[i] != original[i] ) { string description = string.Format("Difference at {0} level {1} zlib {2} ", i, level, zlib); if ( original.Length < 2048 ) { StringBuilder builder = new StringBuilder(description); for (int d = 0; d < original.Length; ++d) { builder.AppendFormat("{0} ", original[d]); } Assert.Fail(builder.ToString()); } else { Assert.Fail(description); } } } }
byte[] Decrypt(byte[] encryptedData) { var reader = new BinaryReader(new MemoryStream(encryptedData)); int headerMagic = reader.ReadInt32(); if (headerMagic == 0x04034B50) throw new NotImplementedException("Not implemented yet since I haven't seen anyone use it."); byte encryption = (byte)(headerMagic >> 24); if ((headerMagic & 0x00FFFFFF) != 0x007D7A7B) // Check if "{z}" throw new ApplicationException(string.Format("Invalid SA header magic 0x{0:X8}", headerMagic)); switch (encryption) { case 1: int totalInflatedLength = reader.ReadInt32(); if (totalInflatedLength < 0) throw new ApplicationException("Invalid length"); var inflatedBytes = new byte[totalInflatedLength]; int partInflatedLength; for (int inflateOffset = 0; inflateOffset < totalInflatedLength; inflateOffset += partInflatedLength) { int partLength = reader.ReadInt32(); partInflatedLength = reader.ReadInt32(); if (partLength < 0 || partInflatedLength < 0) throw new ApplicationException("Invalid length"); var inflater = new Inflater(true); inflater.SetInput(encryptedData, checked((int)reader.BaseStream.Position), partLength); reader.BaseStream.Seek(partLength, SeekOrigin.Current); int realInflatedLen = inflater.Inflate(inflatedBytes, inflateOffset, inflatedBytes.Length - inflateOffset); if (realInflatedLen != partInflatedLength) throw new ApplicationException("Could not inflate"); } return inflatedBytes; case 2: if (resourceDecrypterInfo.DES_Key == null || resourceDecrypterInfo.DES_IV == null) throw new ApplicationException("DES key / iv have not been set yet"); using (var provider = new DESCryptoServiceProvider()) { provider.Key = resourceDecrypterInfo.DES_Key; provider.IV = resourceDecrypterInfo.DES_IV; using (var transform = provider.CreateDecryptor()) { return Decrypt(transform.TransformFinalBlock(encryptedData, 4, encryptedData.Length - 4)); } } case 3: if (resourceDecrypterInfo.AES_Key == null || resourceDecrypterInfo.AES_IV == null) throw new ApplicationException("AES key / iv have not been set yet"); using (var provider = new RijndaelManaged()) { provider.Key = resourceDecrypterInfo.AES_Key; provider.IV = resourceDecrypterInfo.AES_IV; using (var transform = provider.CreateDecryptor()) { return Decrypt(transform.TransformFinalBlock(encryptedData, 4, encryptedData.Length - 4)); } } default: throw new ApplicationException(string.Format("Unknown encryption type 0x{0:X2}", encryption)); } }
/// <exception cref="Sharpen.DataFormatException"></exception> protected internal override int SetInput(int pos, Inflater inf) { ByteBuffer s = buffer.Slice(); s.Position(pos); byte[] tmp = new byte[Math.Min(s.Remaining(), 512)]; s.Get(tmp, 0, tmp.Length); inf.SetInput(tmp, 0, tmp.Length); return tmp.Length; }
public static byte[] DecompressDeflate(byte[] data, int decompSize) { var decompData = new byte[decompSize]; var inflater = new Inflater(true); inflater.SetInput(data); inflater.Inflate(decompData); return decompData; }
public static byte[] DecompressAlphaValues(byte[] alphaValues, int width, int height) { var data = new byte[width * height]; var inflater = new Inflater(); inflater.SetInput(alphaValues); if (inflater.Inflate(data) != data.Length) throw new ArgumentException("Alpha values are not in valid compressed format!"); return data; }
/// <summary> /// ��ѹ�ֽ������� /// </summary> /// <param name="val">��������ֽ�������</param> /// <returns>���ؽ�ѹ�������</returns> public byte[] Decompress(byte[] val) { if (val[0] == 1) { Inflater inflater = new Inflater(true); using (InflaterInputStream decompressStream = new InflaterInputStream(new MemoryStream(UnwrapData(val)), inflater)) { return ArrayHelper.ReadAllBytesFromStream(decompressStream); } } else return UnwrapData(val); }
public static void HandleReqUpdateAccountData(Packet packet) { packet.ReadEnum<AccountDataType>("Type"); packet.ReadTime("Time"); int inflatedSize = packet.ReadInt32("Size"); byte[] compressedData = packet.ReadBytes((int)packet.GetLength() - (int)packet.GetPosition()); byte[] data = new byte[inflatedSize]; var inflater = new Inflater(); inflater.SetInput(compressedData, 0, compressedData.Length); inflater.Inflate(data, 0, inflatedSize); Console.WriteLine("Data: {0}", encoder.GetString(data)); }
public static byte[] Decompress(byte[] compressed, uint unzippedSize) { byte[] result = new byte[unzippedSize]; Inflater inf = new Inflater(); inf.SetInput(compressed); int error = inf.Inflate(result, 0, (int)unzippedSize); if (error == 0) { throw new FileLoadException("The a section of the swf file could not be decompressed."); } return result; }
public ChunkedChanges(bool compressed, CancellationToken token, ManualResetEventSlim pauseWait) { _innerStream = new ChunkStream(); _innerStream.BookmarkReached += (sender, args) => OnCaughtUp?.Invoke(this, null); if (compressed) { _inflater = new Inflater(true); } token.Register(Dispose); _pauseWait = pauseWait; Task.Factory.StartNew(Process, TaskCreationOptions.LongRunning); }
public CodecInputStream(Stream baseStream) { BinaryReader br = new BinaryReader(baseStream); decompressedSize = br.ReadInt32(); compressedSize = br.ReadInt32(); byte[] inbuf = new byte[compressedSize]; baseStream.Read(inbuf, 0, compressedSize); Inflater inflater = new Inflater(false); inflater.SetInput(inbuf); byte[] buf = new byte[decompressedSize]; inflater.Inflate(buf); this.iis = new MemoryStream(buf); }
protected override void inflateVerify(int pos, Inflater inf) { while (!inf.IsFinished) { if (inf.IsNeedingInput) { inf.SetInput(_array, pos, _array.Length - pos); break; } inf.Inflate(VerifyGarbageBuffer, 0, VerifyGarbageBuffer.Length); } while (!inf.IsFinished && !inf.IsNeedingInput) inf.Inflate(VerifyGarbageBuffer, 0, VerifyGarbageBuffer.Length); }
protected override int Inflate(int pos, byte[] dstbuf, int dstoff, Inflater inf) { while (!inf.IsFinished) { if (inf.IsNeedingInput) { inf.SetInput(_array, pos, _array.Length - pos); break; } dstoff += inf.Inflate(dstbuf, dstoff, dstbuf.Length - dstoff); } while (!inf.IsFinished && !inf.IsNeedingInput) dstoff += inf.Inflate(dstbuf, dstoff, dstbuf.Length - dstoff); return dstoff; }
public static byte[] Decompress(byte[] input, int uncompressedLength, bool header = true) { Contract.Requires(input != null); Contract.Requires(uncompressedLength >= 0); Contract.Ensures(Contract.Result<byte[]>() != null); Contract.Ensures(Contract.Result<byte[]>().Length == uncompressedLength); var inflater = new Inflater(!header); var output = new byte[uncompressedLength]; inflater.SetInput(input, 0, input.Length); inflater.Inflate(output); return output; }
protected override int Inflate(int pos, byte[] b, int o, Inflater inf) { while (!inf.IsFinished) { if (inf.IsNeedingInput) { inf.SetInput(_array, pos, _array.Length - pos); break; } o += inf.Inflate(b, o, b.Length - o); } while (!inf.IsFinished && !inf.IsNeedingInput) o += inf.Inflate(b, o, b.Length - o); return o; }
private void Uncompress(BinaryReader SWFBinary) { SWFBinary.BaseStream.Position = 4; int size = Convert.ToInt32(SWFBinary.ReadUInt32()); byte[] UncompressedData = new byte[size]; SWFBinary.BaseStream.Position = 0; SWFBinary.Read(UncompressedData, 0, 8); byte[] CompressedData = SWFBinary.ReadBytes(size); Inflater zipInflator = new Inflater(); zipInflator.SetInput(CompressedData); zipInflator.Inflate(UncompressedData, 8, size - 8); MemoryStream m = new MemoryStream(UncompressedData); this.SWFBinary = new BinaryReader(m); }
public static byte[] Decompress(byte[] content, int offset, int count) { //return content; Inflater decompressor = new Inflater(); decompressor.SetInput(content, offset, count); using (MemoryStream bos = new MemoryStream(content.Length)) { var buf = new byte[1024]; while (!decompressor.IsFinished) { int n = decompressor.Inflate(buf); bos.Write(buf, 0, n); } return bos.ToArray(); } }
public Stream Open(Stream stream) { stream.Seek(DataOffset, SeekOrigin.Begin); byte[] data; if (DataCompression == 0) { data = stream.ReadBytes(DataSize); } else { var dataBuffer = stream.ReadBytes(DataCompressedSize); var inflater = new Inflater(false); inflater.SetInput(dataBuffer); data = new Byte[DataSize]; inflater.Inflate(data); } return new MemoryStream(data); }
public byte[] Uncompress(byte[] input) { Inflater decompressor = new Inflater(); decompressor.SetInput(input); // Create an expandable byte array to hold the decompressed data MemoryStream bos = new MemoryStream(input.Length); // Decompress the data byte[] buf = new byte[1024]; while (!decompressor.IsFinished) { int count = decompressor.Inflate(buf); bos.Write(buf, 0, count); } // Get the decompressed data return bos.ToArray(); }
internal override int inflate(int pos, byte[] b, int o, Inflater inf) { byte[] tmp = new byte[512]; var s = _stream; s.Position=pos; while ((s.Length-s.Position) > 0 && !inf.IsFinished) { if (inf.IsNeedingInput) { int n = (int)Math.Min((s.Length - s.Position), tmp.Length); s.Read(tmp, 0, n); inf.SetInput(tmp, 0, n); } o += inf.Inflate(b, o, b.Length - o); } while (!inf.IsFinished && !inf.IsNeedingInput) o += inf.Inflate(b, o, b.Length - o); return o; }
protected override int Inflate(int pos, byte[] dstbuf, int dstoff, Inflater inf) { var tmp = new byte[512]; var s = _stream; s.Position=pos; while ((s.Length-s.Position) > 0 && !inf.IsFinished) { if (inf.IsNeedingInput) { var n = (int)Math.Min((s.Length - s.Position), tmp.Length); s.Read(tmp, 0, n); inf.SetInput(tmp, 0, n); } dstoff += inf.Inflate(dstbuf, dstoff, dstbuf.Length - dstoff); } while (!inf.IsFinished && !inf.IsNeedingInput) dstoff += inf.Inflate(dstbuf, dstoff, dstbuf.Length - dstoff); return dstoff; }
public InflaterInputStream(Stream baseInputStream, Inflater inflater, int bufferSize) { this.isStreamOwner = true; if (baseInputStream == null) { throw new ArgumentNullException("baseInputStream"); } if (inflater == null) { throw new ArgumentNullException("inflater"); } if (bufferSize <= 0) { throw new ArgumentOutOfRangeException("bufferSize"); } this.baseInputStream = baseInputStream; this.inf = inflater; this.inputBuffer = new InflaterInputBuffer(baseInputStream, bufferSize); }
protected bool Load(SwfStream stream, uint length, bool hasAlpha) { CharacterID = stream.ReadUShort(); byte format = stream.ReadByte(); Width = stream.ReadUShort(); Height = stream.ReadUShort(); byte table = (format == 3) ? stream.ReadByte() : (byte)0; byte[] compressed = stream.ReadByteArray(length - stream.TagPosition); byte[] data; var inflater = new Inflater(); inflater.SetInput(compressed); if (format == 3) { int rem = Width % 4; data = new byte[((rem == 0) ? Width : (Width + 4 - rem)) * Height * 4]; if (inflater.Inflate(data) != data.Length) throw new SwfCorruptedException("Bitmap data are not valid ZLIB stream!"); Pixels = BitmapUtils.UnpackIndexed(data, Width, Height, table, hasAlpha); } else if (format == 4 && !hasAlpha) { data = new byte[(Width + Width & 0x01) * Height * 2]; if (inflater.Inflate(data) != data.Length) throw new SwfCorruptedException("Bitmap data are not valid ZLIB stream!"); Pixels = BitmapUtils.UnpackPIX15(data, Width, Height); } else if (format == 5) { data = new byte[Width * Height * 4]; if (inflater.Inflate(data) != data.Length) return true; Pixels = BitmapUtils.UnpackPIX24(data, Width, Height, hasAlpha); } else throw new SwfCorruptedException("Invalid lossless bitmap format found!"); return true; }
public byte[] Unpack() { if (peImage.PEImage.Win32Resources == null) return null; var dataEntry = peImage.PEImage.Win32Resources.Find(10, "__", 0); if (dataEntry == null) return null; var encryptedData = dataEntry.Data.ReadAllBytes(); var keyData = GetKeyData(); if (keyData == null) return null; var decrypter = new NativeFileDecrypter(keyData); decrypter.Decrypt(encryptedData, 0, encryptedData.Length); byte[] inflatedData; if (isNet1x) inflatedData = DeobUtils.Inflate(encryptedData, false); else { int inflatedSize = BitConverter.ToInt32(encryptedData, 0); inflatedData = new byte[inflatedSize]; var inflater = new Inflater(false); inflater.SetInput(encryptedData, 4, encryptedData.Length - 4); int count = inflater.Inflate(inflatedData); if (count != inflatedSize) return null; } // CLR 1.x or DNR v4.0 - v4.4 if (BitConverter.ToInt16(inflatedData, 0) == 0x5A4D) return inflatedData; // DNR v4.5 if (BitConverter.ToInt16(inflatedData, loaderHeaderSizeV45) == 0x5A4D) return UnpackLoader(inflatedData); return null; }
//........................................................... /// <summary> /// Get an uncompressed blob from PAK_STREAM using the meta-data in ENTRY. /// </summary> /// <returns>A MemoryStream if possible, otherwise a FileStream to a auto-delete temp file.</returns> protected Stream Decompress(Stream PAK_STREAM, cmk.NMS.PAK.Item.Info ENTRY) { if (PAK_STREAM == null) { return(null); } Stream entry; if (ENTRY.Length < Int32.MaxValue) { entry = new MemoryStream((int)ENTRY.Length); } else { var temp = System.IO.Path.GetTempFileName(); entry = new FileStream(temp, FileMode.Open, FileAccess.ReadWrite, FileShare.None, m_block_size, FileOptions.DeleteOnClose ); } // m_block_size is the max decompressed size of a block. // each compressed block will be this size or smaller. var compressed = new byte [m_block_size]; var decompressed = new byte [m_block_size]; var inflater = new zlib.Inflater(); // using System.IO.Compression.DeflateStream.Read fails to parse the data, // even though it supposidly uses zlib behind the scenes. // todo: assume used it incorrectly, would prefer using built-in API instead of nuget zlib. var index = ENTRY.Index; // index of first block for ENTRY var offset = ENTRY.Offset; // where the first block ( m_blocks[index]) starts // e.g. ENTRY: Index = 7, Offset = 123456, Length = 123456: // - m_blocks[7] = 12345, m_blocks[8] = 6789 (example compressed sizes) // these would (likely) decompress to: 65536 and 57920 bytes = 123456 bytes Length. // - offset = 123456 is where m_blocks[7] 12345 bytes of compressed data starts, // m_blocks[8] 6789 bytes of compressed data will immediately follow // m_blocks[7] 12345 bytes, and so on. for ( ; entry.Length < ENTRY.Length; ++index) { PAK_STREAM.Position = offset; // current block is uncompressed full block, just copy to output if (m_blocks[index] == 0) { PAK_STREAM.Read(compressed, 0, m_block_size); entry.Write(compressed, 0, m_block_size); offset += m_block_size; continue; } PAK_STREAM.Read(compressed, 0, (int)m_blocks[index]); // read compressed data for current block offset += m_blocks[index]; // update offset for next block // if it's not an uncompressed full block we MUST assume it's a 'compressed' block (has compression header). // we have no way to differentiate a compression header from valid uncompressed data. // // original psarc.exe code assumes that if the compressed block does not // start with 0x78da then it's an uncompressed partial block - ugghhh. // // https://stackoverflow.com/questions/9050260/what-does-a-zlib-header-look-like // Level | ZLIB | GZIP // 1 | 78 01 | 1F 8B - No Compression/low // 2 | 78 5E | 1F 8B - Fastest Compression // 3 | 78 5E | 1F 8B // 4 | 78 5E | 1F 8B // 5 | 78 5E | 1F 8B // 6 | 78 9C | 1F 8B - Default Compression // 7 | 78 DA | 1F 8B - Best Compression (Slowest) // 8 | 78 DA | 1F 8B // 9 | 78 DA | 1F 8B // if (BitConverter.IsLittleEndian) { Array.Reverse(compressed, 0x00, 2); } var is_compressed = BitConverter.ToUInt16(compressed, 0); if (BitConverter.IsLittleEndian) { Array.Reverse(compressed, 0x00, 2); } if (is_compressed != 0x7801 && is_compressed != 0x785e && is_compressed != 0x789c && is_compressed != 0x78da ) { // MessageBox.Show("Surprise, uncompressed partial ... maybe"); // ... turns out these exist entry.Write(compressed, 0, (int)m_blocks[index]); continue; } // ??? wtf ??? // for blocks that inflate to a full block size (65536) we end up with 4 bytes // in RemainingInput after first Inflate call, the second call results in 0 bytes inflated. // the resulting data in uncompressed_file seems good though, no missing data. // if there was padding added by Deflate i'd expect it to be consumed by first Inflate. // maybe it's the 4 byte adler checksum ??? inflater.SetInput(compressed, 0, (int)m_blocks[index]); var length = inflater.Inflate(decompressed); entry.Write(decompressed, 0, length); inflater.Reset(); } entry.Position = 0; return(entry); }
internal override void inflateVerify(int pos, Inflater inf) { byte[] tmp = new byte[512]; var s = _stream; s.Position=(pos); while ((s.Length - s.Position) > 0 && !inf.IsFinished) { if (inf.IsNeedingInput) { int n = (int)Math.Min((s.Length - s.Position), tmp.Length); s.Read(tmp, 0, n); inf.SetInput(tmp, 0, n); } inf.Inflate(verifyGarbageBuffer, 0, verifyGarbageBuffer.Length); } while (!inf.IsFinished && !inf.IsNeedingInput) inf.Inflate(verifyGarbageBuffer, 0, verifyGarbageBuffer.Length); }
/// <summary> /// Create an InflaterInputStream with the specified decompressor /// and a default buffer size of 4KB. /// </summary> /// <param name = "baseInputStream"> /// The source of input data /// </param> /// <param name = "inf"> /// The decompressor used to decompress data read from baseInputStream /// </param> public InflaterInputStream(Stream baseInputStream, Inflater inf) : this(baseInputStream, inf, 4096) { }
/// <summary> /// Call <see cref="Inflater.SetInput(byte[], int, int)"/> passing the current clear text buffer contents. /// </summary> /// <param name="inflater">The inflater to set input for.</param> public void SetInflaterInput(Inflater inflater) { if ( available > 0 ) { inflater.SetInput(clearText, clearTextLength - available, available); available = 0; } }
/// <summary> /// Create an InflaterInputStream with the specified decompresseor /// and a specified buffer size. /// </summary> /// <param name = "baseInputStream"> /// the InputStream to read bytes from /// </param> /// <param name = "inf"> /// the decompressor used to decompress data read from baseInputStream /// </param> /// <param name = "size"> /// size of the buffer to use /// </param> public InflaterInputStream(Stream baseInputStream, Inflater inf, int size) { this.baseInputStream = baseInputStream; this.inf = inf; try { this.len = (int)baseInputStream.Length; } catch (Exception) { // the stream may not support .Length this.len = 0; } if (size <= 0) { throw new ArgumentOutOfRangeException("size <= 0"); } buf = new byte[size]; //Create the buffer }
/// <summary> /// Decompresses the provided data, returning the inflated result. /// </summary> /// <param name="data">the deflated picture data.</param> /// <returns>the inflated picture data.</returns> private static byte[] InflatePictureData(byte[] data) { using (MemoryStream in1 = new MemoryStream(data)) { using (MemoryStream out1 = new MemoryStream()) { InflaterInputStream zIn = null; try { Inflater inflater = new Inflater(false); zIn = new InflaterInputStream(in1, inflater); byte[] buf = new byte[4096]; int ReadBytes; while ((ReadBytes = zIn.Read(buf, 0, buf.Length)) > 0) { out1.Write(buf, 0, ReadBytes); } return out1.ToArray(); } catch (IOException e) { log.Log(POILogger.WARN, "Possibly corrupt compression or non-compressed data", e); return data; } } } }
/// <summary> /// Performs inflate decompression on the given data. /// </summary> /// <param name="input">the data to decompress</param> /// <param name="output">the decompressed data</param> public static void DecompressZLib(byte[] input, byte[] output) { Inflater item = new Inflater(); item.SetInput(input, 0, input.Length); item.Inflate(output, 0, output.Length); }