public void ZLibCompressStreamTest() { ZLibCompressor target = new ZLibCompressor(); string originalText = "Test string."; MemoryStream msOriginal = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(originalText)); byte[] compressbuffer = new byte[2000]; MemoryStream msCompressed = new MemoryStream(compressbuffer); int outLen = 0; ZLibError err = target.Compress(msOriginal, msCompressed, ref outLen, ZLibQuality.Default); Assert.AreEqual(ZLibError.Okay, err); MemoryStream msToDecompress = new MemoryStream(compressbuffer, 0, outLen); byte[] decompressbuffer = new byte[2000]; MemoryStream msFinalResult = new MemoryStream(decompressbuffer); err = target.Decompress(msToDecompress, msFinalResult, ref outLen); Assert.AreEqual(ZLibError.Okay, err); string result = ASCIIEncoding.ASCII.GetString(decompressbuffer, 0, outLen); Assert.AreEqual(originalText, result); }
byte[] file_decompress(byte[] bye) { MemoryStream ms = new MemoryStream(bye); BinaryReader t_br = new BinaryReader(ms); byte[] unbye = new byte[t_br.ReadInt32()]; int block_num = unbye.Length / 32768 + 1; int[] size = new int[block_num]; int[] offset = new int[block_num]; int i; for (i = 0; i < block_num; i++) { offset[i] = t_br.ReadInt32(); if (i > 0) { size[i - 1] = offset[i] - offset[i - 1]; } } size[i - 1] = (int)ms.Length - offset[i - 1]; int index = 0; for (i = 0; i < block_num; i++) { ms.Seek(offset[i], SeekOrigin.Begin); byte[] t_bye = ZLibCompressor.DeCompress(t_br.ReadBytes(size[i])); t_bye.CopyTo(unbye, index); index += t_bye.Length; } t_br.Close(); ms.Close(); return(unbye); }
public void ZLibStream_Decompressor_2() { byte[] input = new byte[] { 0x78, 0x9C, 0x73, 0x74, 0x72, 0x76, 0x71, 0x75, 0x03, 0x00, 0x05, 0x7E, 0x01, 0x96 }; byte[] plaintext = Encoding.UTF8.GetBytes("ABCDEF"); byte[] decompBytes = ZLibCompressor.Decompress(input); Assert.IsTrue(decompBytes.SequenceEqual(plaintext)); }
public static BeamLiteralsChunk Read(EndianBinaryReader reader, uint size) { var decSize = reader.ReadUInt32(); var encBytes = reader.ReadBytes((int)size - 4); //-decSize size var decBytes = ZLibCompressor.Decompress(encBytes); if (decBytes.Length != decSize) { throw new ReadBytesCountException(decBytes.Length, (int)decSize); } var decReader = new EndianBinaryReader(new MemoryStream(decBytes)); var litCount = decReader.ReadUInt32(); var literals = new IExtTerm[litCount]; for (uint i = 0; i < litCount; i++) { var litSize = decReader.ReadUInt32(); var oldPos = decReader.Position; literals[i] = BeamTerm.BinaryToTerm(decReader); var newPos = decReader.Position; if (oldPos + litSize != newPos) { throw new Exception(); } } return(new BeamLiteralsChunk() { Literals = literals }); }
public void Write(EndianBinaryWriter writer) { var decMemStream = new MemoryStream(); var decWriter = new EndianBinaryWriter(decMemStream); decWriter.Write(Literals.Length); foreach (var literal in Literals) { decWriter.SeekNext(4); //uint size var oldPos = decWriter.Position; BeamTerm.TermToBinary(decWriter, literal); var len = (uint)(decWriter.Position - oldPos); decWriter.SeekBack((int)(len + 4)); decWriter.Write(len); decWriter.SeekNext((int)len); } decMemStream.Position = 0; var decBytes = decMemStream.ToArray(); writer.Write(decBytes.Length); var encBytes = ZLibCompressor.Compress(decBytes); writer.Write(encBytes); }
internal virtual void ReadYuzNames(byte[] yuz, FilenameMap filename_map) { using (var ystream = new MemoryStream(yuz)) using (var zstream = ZLibCompressor.DeCompress(ystream)) using (var input = new BinaryReader(zstream, Encoding.Unicode)) { long dir_offset = 0; while (-1 != input.PeekChar()) { uint entry_signature = input.ReadUInt32(); long entry_size = input.ReadInt64(); if (entry_size < 0) { return; } dir_offset += 12 + entry_size; uint hash = input.ReadUInt32(); int name_size = input.ReadInt16(); if (name_size > 0) { entry_size -= 6; if (name_size * 2 <= entry_size) { var filename = new string (input.ReadChars(name_size)); filename_map.Add(hash, filename); } } input.BaseStream.Position = dir_offset; } filename_map.AddShortcut("$", "startup.tjs"); } }
/// <summary>Разжимает массив</summary> /// <param name="input">Входной массив.</param> /// <returns>Разжатый массив</returns> public static byte[] Decompress(byte[] input) { try { return(ZLibCompressor.DeCompress(input)); } catch (Exception) { throw new ArgumentException("Ошибка декомпрессии"); } }
/// <summary> /// Compresses the specified input in /// </summary> public static int CompressInZLibFormat(byte[] input, out byte[] output) { byte[] compressed = ZLibCompressor.Compress(input, CompressionLevel.Level1); int compressedLength = compressed.Length; int uncompressedLength = input.Length; output = new byte[compressedLength + 4]; output[0] = (byte)uncompressedLength; output[1] = (byte)(uncompressedLength >> 8); output[2] = (byte)(uncompressedLength >> 16); output[3] = (byte)(uncompressedLength >> 24); Array.Copy(compressed, 0, output, 4, compressedLength); return(output.Length); }
public void ZLibStream_Compressor_2() { byte[] input = Encoding.UTF8.GetBytes("ABCDEF"); // Compress first, // 78-9C-73-74-72-76-71-75-03-00-05-7E-01-96 byte[] compBytes = ZLibCompressor.Compress(input); // then Decompress. byte[] decompBytes = ZLibCompressor.Decompress(compBytes); // Comprare SHA256 Digest byte[] inputDigest = TestSetup.SHA256Digest(input); byte[] decompDigest = TestSetup.SHA256Digest(decompBytes); Assert.IsTrue(decompDigest.SequenceEqual(inputDigest)); }
static byte[] GetQntAlpha(byte[] inputBytes, QntHeader qntHeader) { byte[] raw = null; using (var ms = new MemoryStream(inputBytes)) { ms.Position = qntHeader.headerSize + qntHeader.pixelSize; using (var rawMs = ZLibCompressor.DeCompress(ms)) { raw = rawMs.ToArray(); } } int w = qntHeader.width; int h = qntHeader.height; byte[] pic = DecodeQntAlpha(raw, w, h); VerifyQntAlpha(raw, w, h, pic); return(pic); }
public override void Read(BinaryReader br, int len, int[] bs = null) { if (this.Type != RiffType.LIST) { base.Read(br, len); } int zdataLen, dataLen, zblkLen, blocksLen4; zdataLen = br.ReadInt32(); dataLen = br.ReadInt32(); zblkLen = br.ReadInt32(); blocksLen4 = br.ReadInt32(); br.ReadInt64(); byte[] zdata = new byte[zdataLen - 8]; byte[] zblk = new byte[zblkLen - 8]; br.Read(zdata, 0, zdataLen - 8); br.ReadInt64(); br.Read(zblk, 0, zblkLen - 8); byte[] data = ZLibCompressor.DeCompress(zdata); //Zlib.deCompressBytes(zdata); byte[] blocks = ZLibCompressor.DeCompress(zblk); // Zlib.deCompressBytes(zblk); //byte[] data = ZLibNet.ZLibCompressor.DeCompress(zdata); //byte[] blocks = ZLibNet.ZLibCompressor.DeCompress(zblk); FileStream fs = File.Open("D:\\blocks", FileMode.Create); fs.Write(blocks, 0, blocks.Length); fs.Close(); fs = File.Open("D:\\data", FileMode.Create); fs.Write(data, 0, data.Length); fs.Close(); int[] blockSizes = new int[blocks.Length / 4]; for (int i = 0, j = 0; i < blocks.Length; i += 4, j++) { blockSizes[j] = BitConverter.ToInt32(blocks, i); } MemoryStream ms = new MemoryStream(data); BinaryReader br1 = new BinaryReader(ms); subList = RiffConn.ReadRiff(this.Parent, br1, blockSizes); br1.Close(); ms.Close(); }
/// <summary> /// Decompresses the specified input in my sql format. /// </summary> public static int DecompressInMySQLFormat(byte[] input, int length, out byte[] output) { int decompressedLength = input[0] | (input[1] << 8) | (input[2] << 16) | (input[3] << 24); using (MemoryStream inputStream = new MemoryStream(input, 4, input.Length - 4)) { using (MemoryStream outputStream = ZLibCompressor.Decompress(inputStream)) { byte[] decompressedByteArray = outputStream.ToArray(); if (decompressedByteArray.Length != decompressedLength) { Debugger.Error("ZLibHelper::decompressInMySQLFormat decompressed byte array is corrupted"); } output = decompressedByteArray; } } return(decompressedLength); }
public void ZLibStream_Compressor_1() { void Template(string fileName, ZLibCompLevel level) { string filePath = Path.Combine(TestSetup.SampleDir, fileName); using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (MemoryStream compMs = ZLibCompressor.Compress(fs)) using (MemoryStream decompMs = ZLibCompressor.Decompress(compMs)) { // Compare SHA256 Digest fs.Position = 0; byte[] fileDigest = TestSetup.SHA256Digest(fs); byte[] decompDigest = TestSetup.SHA256Digest(decompMs); Assert.IsTrue(decompDigest.SequenceEqual(fileDigest)); } } Template("ex1.jpg", ZLibCompLevel.Default); Template("ex2.jpg", ZLibCompLevel.BestCompression); Template("ex3.jpg", ZLibCompLevel.BestSpeed); }
public void ZLibStream_Decompressor_1() { void Template(string fileName) { string compPath = Path.Combine(TestSetup.SampleDir, fileName + ".zz"); string decompPath = Path.Combine(TestSetup.SampleDir, fileName); using (FileStream decompFs = new FileStream(decompPath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (FileStream compFs = new FileStream(compPath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (MemoryStream decompMs = ZLibCompressor.Decompress(compFs)) { // Compare SHA256 Digest byte[] fileDigest = TestSetup.SHA256Digest(decompFs); byte[] decompDigest = TestSetup.SHA256Digest(decompMs); Assert.IsTrue(decompDigest.SequenceEqual(fileDigest)); } } Template("ex1.jpg"); Template("ex2.jpg"); Template("ex3.jpg"); }
public static byte[] GetQntPixels(byte[] inputBytes, QntHeader qntHeader) { byte[] pic; if (qntHeader.pixelSize > 0) { byte[] raw = null; using (var ms = new MemoryStream(inputBytes, qntHeader.headerSize, qntHeader.pixelSize)) { using (var rawMs = ZLibCompressor.DeCompress(ms)) { raw = rawMs.ToArray(); } int endPosition = (int)ms.Position; //int length = (int)ms.Position - qntHeader.headerSize; //if (length != qntHeader.pixelSize + qntHeader.alphaSize) //{ //} } int w = qntHeader.width; int h = qntHeader.height; if (raw != null && raw.Length < w * h * 3) { throw new InvalidDataException("Size of decompressed data is wrong"); } pic = DecodeQntPixels(raw, w, h); //VerifyQntPixels(raw, w, h, pic); } else { int w = qntHeader.width; int h = qntHeader.height; pic = new byte[w * h * 3]; } return(pic); }
public static byte[] DeCompress(byte[] src) => ZLibCompressor.DeCompress(src);
/// <summary>Сжимает массив</summary> /// <param name="data">Входной массив.</param> /// <returns>Сжатый массив</returns> public static byte[] Compress(byte[] data) { return(ZLibCompressor.Compress(data)); }
public static byte[] DeCompress(byte[] src) { return(ZLibCompressor.DeCompress(src)); }
public override ArcFile TryOpen(ArcView file) { long base_offset = 0; if (0x5a4d == file.View.ReadUInt16(0)) // 'MZ' { base_offset = SkipExeHeader(file, s_xp3_header); } if (!file.View.BytesEqual(base_offset, s_xp3_header)) { return(null); } long dir_offset = base_offset + file.View.ReadInt64(base_offset + 0x0b); if (dir_offset < 0x13 || dir_offset >= file.MaxOffset) { return(null); } if (0x80 == file.View.ReadUInt32(dir_offset)) { dir_offset = base_offset + file.View.ReadInt64(dir_offset + 9); if (dir_offset < 0x13 || dir_offset >= file.MaxOffset) { return(null); } } int header_type = file.View.ReadByte(dir_offset); if (0 != header_type && 1 != header_type) { return(null); } Stream header_stream; if (0 == header_type) // read unpacked header { long header_size = file.View.ReadInt64(dir_offset + 1); if (header_size > uint.MaxValue) { return(null); } header_stream = file.CreateStream(dir_offset + 9, (uint)header_size); } else // read packed header { long packed_size = file.View.ReadInt64(dir_offset + 1); if (packed_size > uint.MaxValue) { return(null); } long header_size = file.View.ReadInt64(dir_offset + 9); using (var input = file.CreateStream(dir_offset + 17, (uint)packed_size)) header_stream = ZLibCompressor.DeCompress(input); } var crypt_algorithm = new Lazy <ICrypt> (() => QueryCryptAlgorithm(file), false); var dir = new List <Entry>(); dir_offset = 0; using (var header = new BinaryReader(header_stream, Encoding.Unicode)) using (var filename_map = new FilenameMap()) { while (-1 != header.PeekChar()) { uint entry_signature = header.ReadUInt32(); long entry_size = header.ReadInt64(); if (entry_size < 0) { return(null); } dir_offset += 12 + entry_size; if (0x656C6946 == entry_signature) // "File" { var entry = new Xp3Entry(); while (entry_size > 0) { uint section = header.ReadUInt32(); long section_size = header.ReadInt64(); entry_size -= 12; if (section_size > entry_size) { break; } entry_size -= section_size; long next_section_pos = header.BaseStream.Position + section_size; switch (section) { case 0x6f666e69: // "info" if (entry.Size != 0 || !string.IsNullOrEmpty(entry.Name)) { goto NextEntry; // ambiguous entry, ignore } entry.IsEncrypted = 0 != header.ReadUInt32(); long file_size = header.ReadInt64(); long packed_size = header.ReadInt64(); if (file_size >= uint.MaxValue || packed_size > uint.MaxValue || packed_size > file.MaxOffset) { goto NextEntry; } entry.IsPacked = file_size != packed_size; entry.Size = (uint)packed_size; entry.UnpackedSize = (uint)file_size; if (entry.IsEncrypted || ForceEncryptionQuery) { entry.Cipher = crypt_algorithm.Value; } else { entry.Cipher = NoCryptAlgorithm; } var name = entry.Cipher.ReadName(header); if (null == name) { goto NextEntry; } if (entry.Cipher.ObfuscatedIndex && ObfuscatedPathRe.IsMatch(name)) { goto NextEntry; } if (filename_map.Count > 0) { name = filename_map.Get(entry.Hash, name); } if (name.Length > 0x100) { goto NextEntry; } entry.Name = name; entry.Type = FormatCatalog.Instance.GetTypeFromName(name); entry.IsEncrypted = !(entry.Cipher is NoCrypt) && !(entry.Cipher.StartupTjsNotEncrypted && "startup.tjs" == name); break; case 0x6d676573: // "segm" int segment_count = (int)(section_size / 0x1c); if (segment_count > 0) { for (int i = 0; i < segment_count; ++i) { bool compressed = 0 != header.ReadInt32(); long segment_offset = base_offset + header.ReadInt64(); long segment_size = header.ReadInt64(); long segment_packed_size = header.ReadInt64(); if (segment_offset > file.MaxOffset || segment_packed_size > file.MaxOffset) { goto NextEntry; } var segment = new Xp3Segment { IsCompressed = compressed, Offset = segment_offset, Size = (uint)segment_size, PackedSize = (uint)segment_packed_size }; entry.Segments.Add(segment); } entry.Offset = entry.Segments.First().Offset; } break; case 0x726c6461: // "adlr" if (4 == section_size) { entry.Hash = header.ReadUInt32(); } break; default: // unknown section break; } header.BaseStream.Position = next_section_pos; } if (!string.IsNullOrEmpty(entry.Name) && entry.Segments.Any()) { if (entry.Cipher.ObfuscatedIndex) { DeobfuscateEntry(entry); } dir.Add(entry); } } else if (entry_size > 7) { // 0x6E666E68 == entry_signature // "hnfn" // 0x6C696D73 == entry_signature // "smil" // 0x46696C65 == entry_signature // "eliF" // 0x757A7559 == entry_signature // "Yuzu" uint hash = header.ReadUInt32(); int name_size = header.ReadInt16(); if (name_size > 0) { entry_size -= 6; if (name_size * 2 <= entry_size) { var filename = new string (header.ReadChars(name_size)); filename_map.Add(hash, filename); } } } NextEntry: header.BaseStream.Position = dir_offset; } } if (0 == dir.Count) { return(null); } var arc = new ArcFile(file, this, dir); try { if (crypt_algorithm.IsValueCreated) { crypt_algorithm.Value.Init(arc); } return(arc); } catch { arc.Dispose(); throw; } }
private void getNusbankIdInfo(string fname) { FileData nus3 = new FileData(fname); nus3.Endian = Endianness.Little; if (nus3.Magic() != "NUS3") //NUS3 { nus3.seek(0); byte[] data = nus3.read(nus3.eof()); byte[] nus3_decom = ZLibCompressor.DeCompress(data); FileOutput newf = new FileOutput(); newf.writeBytes(nus3_decom); newf.save(fname); data = null; nus3_decom = null; nus3 = new FileData(fname); nus3.Endian = Endianness.Little; } else { nus3.seek(0); } nus3.seek(4); uint size = nus3.readUInt(); nus3.skip(8); uint tocSize = nus3.readUInt(); uint contentCount = nus3.readUInt(); uint offset = 0x14 + tocSize; uint propOffset = 0, propSize = 0, binfOffset = 0, binfSize = 0, grpOffset = 0, grpSize = 0, dtonOffset = 0, dtonSize = 0, toneOffset = 0, toneSize = 0, junkOffset = 0, junkSize = 0, markOffset = 0, markSize = 0, packOffset = 0, packSize = 0; for (int i = 0; i < contentCount; ++i) { string content = nus3.readString(nus3.pos(), 4); nus3.skip(4); uint contentSize = nus3.readUInt(); if (content.Equals("PROP")) { propOffset = offset; propSize = contentSize; } else if (content.Equals("BINF")) { binfOffset = offset; binfSize = contentSize; } else if (content.Equals("GRP ")) { grpOffset = offset; grpSize = contentSize; } else if (content.Equals("DTON")) { dtonOffset = offset; dtonSize = contentSize; } else if (content.Equals("TONE")) { toneOffset = offset; toneSize = contentSize; } else if (content.Equals("JUNK")) { junkOffset = offset; junkSize = contentSize; } else if (content.Equals("MARK")) { markOffset = offset; markSize = contentSize; } else if (content.Equals("PACK")) { packOffset = offset; packSize = contentSize; } offset += 8 + contentSize; } nus3.seek((int)binfOffset + 16); byte binfStringSize = nus3.readByte(); string binfString = nus3.readString(nus3.pos(), binfStringSize - 1); nus3.skip(binfStringSize); int padding = (binfStringSize + 1) % 4; if (padding != 0) { nus3.skip(Math.Abs(padding - 4)); } nusIdPos = (uint)nus3.pos(); nusId = nus3.readUInt(); nus3 = null; }
/// <summary> /// 将录制的数据写入wav文件 改写为UDP发送 /// </summary> private void RecordCapturedData(Socket Client, EndPoint epserver) { byte[] CaptureData = null; byte[] SendData = null; int ReadPos = 0; int CapturePos = 0; int LockSize = 0; mRecBuffer.GetCurrentPosition(out CapturePos, out ReadPos); LockSize = ReadPos - mNextCaptureOffset; if (LockSize < 0) { LockSize += mBufferSize; } // 对齐缓冲区边界,实际上由于开始设定完整,这个操作是多余的. LockSize -= (LockSize % mNotifySize); if (0 == LockSize) { return; } // 读取缓冲区内的数据 CaptureData = (byte[])mRecBuffer.Read(mNextCaptureOffset, typeof(byte), LockFlag.None, LockSize); // 更新已经录制的数据长度. mSampleCount += CaptureData.Length; //压缩数据,实测压缩率在50%左右 SendData = ZLibCompressor.Compress(CaptureData); // 写入Wav文件 ,在这里改成发送 // mWriter.Write(SendData, 0, SendData.Length); // mWriter.Write(CaptureData, 0, CaptureData.Length); //发送文件到网络 try { Client.SendTo(SendData, epServer);//传送语音 } catch (Exception e) { MessageBox.Show(e.Message, "错误"); } // 移动录制数据的起始点,通知消息只负责指示产生消息的位置,并不记录上次录制的位置 mNextCaptureOffset += CaptureData.Length; mNextCaptureOffset %= mBufferSize; // Circular buffer }