} // End Sub Decompress public static byte[] DeflateDecompress(byte[] gzip) { byte[] baRetVal = null; using (System.IO.MemoryStream ByteStream = new System.IO.MemoryStream(gzip)) { // Create a GZIP stream with decompression mode. // ... Then create a buffer and write into while reading from the GZIP stream. using (System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ByteStream , System.IO.Compression.CompressionMode.Decompress)) { const int size = 4096; byte[] buffer = new byte[size]; using (System.IO.MemoryStream memstrm = new System.IO.MemoryStream()) { int count = 0; count = stream.Read(buffer, 0, size); while (count > 0) { memstrm.Write(buffer, 0, count); memstrm.Flush(); count = stream.Read(buffer, 0, size); } // Whend baRetVal = memstrm.ToArray(); memstrm.Close(); } // End Using memstrm stream.Close(); } // End Using System.IO.Compression.GZipStream stream ByteStream.Close(); } // End Using System.IO.MemoryStream ByteStream return(baRetVal); } // End Sub Decompress
/// <summary> /// Decompress using deflate algorithm. /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] Decompress(byte[] data) { byte[] array = null; try { using (MemoryStream inputStream = new MemoryStream(data)) using (MemoryStream outputStream = new MemoryStream()) using (System.IO.Compression.DeflateStream decompressionStream = new System.IO.Compression.DeflateStream(inputStream , System.IO.Compression.CompressionMode.Decompress)) { int count = 2048; byte[] buffer = new byte[count]; for (; ;) { int length = decompressionStream.Read(buffer, 0, count); if (length == 0) { break; } outputStream.Write(buffer, 0, length); } return(outputStream.ToArray()); } } catch (Exception exception) { Energy.Core.Bug.Catch(exception); } return(array); }
public byte[] decompressFile(CentralDirectoryFileHeader inHeader) { streamPosition = inHeader.fileHeaderOffset; var localHeader = new LocalHeader(); localHeader.load(reader); var compressedBytes = reader.ReadBytes((int)inHeader.compressedSize); if (inHeader.compressionMethod == 0) { return(compressedBytes); } if (inHeader.compressionMethod != 8) //deflate { throw new System.ArgumentException( string.Format("Unsupported compression method {0} in file \"{1}\" of archive \"{2}\"", inHeader.filename, inHeader.compressionMethod, archiveFileName)); } var tmp = new byte[65536]; using (var memStream = new System.IO.MemoryStream(compressedBytes)) using (var deflateStream = new System.IO.Compression.DeflateStream(memStream, System.IO.Compression.CompressionMode.Decompress)) using (var outStream = new System.IO.MemoryStream()){ int bytesRead; while ((bytesRead = deflateStream.Read(tmp, 0, tmp.Length)) > 0) { outStream.Write(tmp, 0, bytesRead); } return(outStream.ToArray()); } }
public static byte[] Decompress(byte[] buffer) { // TODO : rename to unity flag #if SCRIPT_ENABLED var decompressBuffer = new List <byte>(); using (var decompressStream = new System.IO.MemoryStream(buffer.Skip(2).Take(buffer.Length - 4).ToArray())) using (var decompressor = new System.IO.Compression.DeflateStream(decompressStream, System.IO.Compression.CompressionMode.Decompress)) { while (true) { byte[] temp = new byte[1024]; int readSize = decompressor.Read(temp, 0, temp.Length); if (readSize == 0) { break; } decompressBuffer.AddRange(temp.Take(readSize)); } } return(decompressBuffer.ToArray()); #else return(null); #endif }
/// <summary> /// Recupera o resultado da requisição. /// </summary> /// <param name="response"></param> /// <returns></returns> private RequestResult GetRequestResult(WebResponse response) { if (response != null) { var webResponse = response as HttpWebResponse; var args = new WebClientResponseHeadersReceivedEventArgs(webResponse); OnResponseHeadersReceived(args); var responseStream = new System.IO.MemoryStream(); if (!args.Cancel) { var buffer = new byte[1024]; var read = 0; var contentLength = webResponse.ContentLength; if (string.Compare(webResponse.ContentEncoding, "gzip", true) == 0) { using (var stream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress)) while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { responseStream.Write(buffer, 0, read); OnResponseProgressChanged(contentLength, read); } } else if (string.Compare(webResponse.ContentEncoding, "deflate", true) == 0) { using (var stream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress)) while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { responseStream.Write(buffer, 0, read); OnResponseProgressChanged(contentLength, read); } } else { using (var stream = response.GetResponseStream()) while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { responseStream.Write(buffer, 0, read); OnResponseProgressChanged(contentLength, read); } } responseStream.Seek(0, System.IO.SeekOrigin.Begin); } System.Text.Encoding encoding = null; try { if (webResponse.CharacterSet != null && webResponse.CharacterSet != "") { encoding = System.Text.Encoding.GetEncoding(webResponse.CharacterSet); } } catch { } return(new RequestResult(webResponse, webResponse != null ? webResponse.StatusCode : HttpStatusCode.BadRequest, webResponse != null ? webResponse.StatusDescription : "Bad Request", encoding ?? System.Text.Encoding.Default, responseStream)); } else { return(new RequestResult(null, HttpStatusCode.BadRequest, "Bad Request", System.Text.Encoding.Default, null)); } }
public void ExtractEntry(Entry entry, Stream outputStream, ZipProgressHandler progressHandler = null) { if (entry.Offset >= _Stream.Length) { throw new Exception("Invalid ZIP entry offset"); } _Stream.Seek(entry.Offset, SeekOrigin.Begin); byte[] buffer = new byte[1024 * 1024]; _Stream.Read(buffer, 0, 30); ushort fileNameLength = BitConverter.ToUInt16(buffer, 26); ushort extraFieldLength = BitConverter.ToUInt16(buffer, 28); _Stream.Seek(entry.Offset + 30 + fileNameLength + extraFieldLength, SeekOrigin.Begin); Stream dataStream; switch (entry.CompressionMethod) { case 0: dataStream = _Stream; break; case 8: dataStream = new System.IO.Compression.DeflateStream(_Stream, System.IO.Compression.CompressionMode.Decompress, true); break; default: throw new Exception("Unsupported ZIP compression method: " + entry.CompressionMethod + " for " + entry.FileName); } uint done = 0; while (done < entry.UncompressedSize) { int todo = (int)Math.Min(entry.UncompressedSize - done, (uint)buffer.Length); if (dataStream.Read(buffer, 0, todo) != todo) { throw new Exception("Cannot read data from ZIP stream for " + entry.FileName); } outputStream.Write(buffer, 0, todo); done += (uint)todo; if (progressHandler != null) { progressHandler(done, entry.UncompressedSize); } } if (dataStream != _Stream) { dataStream.Dispose(); } }
public static byte[] Decompress(byte[] bytes) { var srcStream = new System.IO.MemoryStream(bytes); var dstStream = new System.IO.MemoryStream(); srcStream.Position = 0; var stream = new System.IO.Compression.DeflateStream(srcStream, System.IO.Compression.CompressionMode.Decompress); byte[] buffer = new byte[4096]; int numRead; while ((numRead = stream.Read(buffer, 0, buffer.Length)) != 0) dstStream.Write(buffer, 0, numRead); dstStream.Close(); return dstStream.ToArray(); }
static byte[] ZLibBufferFromStream(System.IO.Compression.DeflateStream dec, int offset, int size) { byte[] result; // adjust for zlib header dec.BaseStream.Seek(offset + sizeof(ushort), System.IO.SeekOrigin.Begin); // decompress the data and fill in the result array result = new byte[size]; dec.Read(result, 0, result.Length); return(result); }
} // End Sub DeCompressFile public static void DeflateDeCompressFile(string CompressedFile, string DeCompressedFile) { byte[] buffer = new byte[1024 * 1024]; using (System.IO.FileStream fstrmCompressedFile = System.IO.File.OpenRead(CompressedFile)) // fi.OpenRead()) { using (System.IO.FileStream fstrmDecompressedFile = System.IO.File.Create(DeCompressedFile)) { using (System.IO.Compression.DeflateStream strmUncompress = new System.IO.Compression.DeflateStream(fstrmCompressedFile, System.IO.Compression.CompressionMode.Decompress)) { int numRead = strmUncompress.Read(buffer, 0, buffer.Length); while (numRead != 0) { fstrmDecompressedFile.Write(buffer, 0, numRead); fstrmDecompressedFile.Flush(); numRead = strmUncompress.Read(buffer, 0, buffer.Length); } // Whend //int numRead = 0; //while ((numRead = strmUncompress.Read(buffer, 0, buffer.Length)) != 0) //{ // fstrmDecompressedFile.Write(buffer, 0, numRead); // fstrmDecompressedFile.Flush(); //} // Whend strmUncompress.Close(); } // End Using System.IO.Compression.GZipStream strmUncompress fstrmDecompressedFile.Flush(); fstrmDecompressedFile.Close(); } // End Using System.IO.FileStream fstrmCompressedFile fstrmCompressedFile.Close(); } // End Using System.IO.FileStream fstrmCompressedFile } // End Sub DeCompressFile
public static void OutputResources(string dir, CacheFileGen3 cf, TI.Block <pages_block> block) { uint offset; int size; uint base_offset = (uint)(cf.Header as CacheHeaderGen3).Interop[CacheSectionType.Resource].CacheOffset; string path = string.Format(@"{0}\\{1}_resources\\", dir, cf.Header.Name); if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } int x = -1; foreach (pages_block def in block) { x++; if (def.SharedCache.Value != -1) { continue; } size = def.BlockSizeUncompressed.Value; if (size <= 0) { continue; } offset = base_offset + (uint)def.BlockOffset.Value; cf.InputStream.Seek(offset); byte[] data; if (def.CompressionCodec.Value != -1) { using (var deflate = new System.IO.Compression.DeflateStream(cf.InputStream.BaseStream, System.IO.Compression.CompressionMode.Decompress, true)) { data = new byte[size]; deflate.Read(data, 0, data.Length); } } else { data = cf.InputStream.ReadBytes(size); } using (var fs = new FileStream(string.Format("{0}\\{1:X8}.bin", path, x), System.IO.FileMode.Create, System.IO.FileAccess.Write)) { fs.Write(data, 0, data.Length); } } }
static void Decode(Stream s) { // skip first two bytes Console.WriteLine(s.Length); s.ReadByte(); s.ReadByte(); using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(s, System.IO.Compression.CompressionMode.Decompress)) { ds.Flush(); byte []b = new byte[100]; int a = ds.Read(b, 0, 100); Console.WriteLine(a); string r = new StreamReader(ds).ReadToEnd(); Console.WriteLine(r); } }
/// <summary> /// Decompresses the stored IDAT information into a byte array. /// </summary> private byte[] decompress_datastream(PNG_Chunk[] idat_chunks) { if (!is_valid_png || idat_chunks == null) { return(null); } if (idat_chunks == null) { return(null); } // extract just the data from the IDAT chunks System.Collections.Generic.List <byte> compressed_bytes = new System.Collections.Generic.List <byte>(); foreach (PNG_Chunk IDAT_chunk in idat_chunks) { // take the data from each idat chunk, remove the first two // bytes from each chunk (which are // zlib compression specific bytes, not // necessary for the compressed data at all) for (int i = 2; i < IDAT_chunk.chunk_data.Length; i++) { compressed_bytes.Add(IDAT_chunk.chunk_data[i]); } } // convert list of compressed bytes to deflated stream System.IO.MemoryStream memstream = new System.IO.MemoryStream(compressed_bytes.ToArray()); System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(memstream, System.IO.Compression.CompressionMode.Decompress); System.Collections.Generic.List <byte> decompressed_list = new System.Collections.Generic.List <byte>(); // convert decompressed byte stream to a decompressed byte array to find the # of decompressed bytes byte[] temp = new byte[1000 * deflate.BaseStream.Length]; int true_length = deflate.Read(temp, 0, temp.Length); for (int i = 0; i < true_length; i++) { decompressed_list.Add(temp[i]); } return(decompressed_list.ToArray()); }
public byte[] ReadEntry(FPakEntry entry) { if (entry != null) { if (entry.CompressionMethod == FPakEntry.ECompressionFlags.COMPRESS_None) { return(ReadStreamData(entry.Offset, entry.Size)); } else { var decompressed = new byte[entry.UncompressedSize]; int Index = 0; int offset = 0; foreach (var block in entry.CompressionBlocks) { var CompressedBlockSize = block.CompressionEnd - block.CompressionStart; var UncompressedBlockSize = Math.Min(entry.UncompressedSize - entry.CompressionBlockSize * Index, entry.CompressionBlockSize); var data = ReadStreamData(block.CompressionStart, CompressedBlockSize); if (entry.CompressionMethod == FPakEntry.ECompressionFlags.COMPRESS_ZLIB) { using (var compressed = new MemoryStream(data, 2, data.Length - 2)) // skip 2 bytes for zlib specification { using (var decstream = new System.IO.Compression.DeflateStream(compressed, System.IO.Compression.CompressionMode.Decompress)) { offset += decstream.Read(decompressed, offset, (int)UncompressedBlockSize); } } } // TODO: Test if GZip decompresses fine. else if (entry.CompressionMethod == FPakEntry.ECompressionFlags.COMPRESS_GZIP) { using (var compressed = new MemoryStream(data)) // skip 2 bytes for zlib specification { using (var decstream = new System.IO.Compression.GZipStream(compressed, System.IO.Compression.CompressionMode.Decompress)) { offset += decstream.Read(decompressed, offset, (int)UncompressedBlockSize); } } } Index++; } return(decompressed); } } return(null); }
public static string Decompress(string strSource, int length) { byte[] buffer = Convert.FromBase64String(strSource); System.IO.MemoryStream ms = new System.IO.MemoryStream(); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress); stream.Flush(); int nSize = length; byte[] decompressBuffer = new byte[nSize]; int nSizeIncept = stream.Read(decompressBuffer, 0, nSize); stream.Close(); return System.Text.Encoding.Unicode.GetString(decompressBuffer, 0, nSizeIncept);//转换为普通的字符串 }
private XmlElement descomprimirDatos(XmlElement propiedades, CompressionAlgorithm descompresor) { string textoComprimido = propiedades.InnerText; byte[] datosComprimidos = Convert.FromBase64String(textoComprimido); System.IO.MemoryStream stream = new MemoryStream(datosComprimidos); System.IO.Stream streamDescompresor = null; switch (descompresor) { case CompressionAlgorithm.Deflate: streamDescompresor = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Decompress); break; case CompressionAlgorithm.Gzip: streamDescompresor = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress); break; #if LZMAPRESENT case CompressionAlgorithm.LZMA: streamDescompresor = new System.IO.Compression.LzmaStream(stream, System.IO.Compression.CompressionMode.Decompress); break; #endif default: streamDescompresor = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Decompress); break; } int contador = 1; byte[] datosLeidos; int leido; do { datosLeidos = new byte[2097152 * contador]; leido = streamDescompresor.Read(datosLeidos, 0, datosLeidos.Length); contador++; } while (leido == datosLeidos.Length - 1); string textoDescomprimido = System.Text.Encoding.Default.GetString(datosLeidos, 0, leido); propiedades.InnerXml = textoDescomprimido; return(propiedades["properties"]); }
public static byte[] Unzip(byte[] data) { System.IO.Compression.CompressionMode mode = System.IO.Compression.CompressionMode.Decompress; System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream( new System.IO.MemoryStream(data), mode); System.IO.MemoryStream mem = new System.IO.MemoryStream(); byte[] buffer = new byte[4096]; while (true) { int count = stream.Read(buffer, 0, buffer.Length); if (count != 0) mem.Write(buffer, 0, count); if (count != buffer.Length) break; } stream.Close(); return mem.ToArray(); }
public static string Decompress(string strSource, int length) { byte[] buffer = Convert.FromBase64String(strSource); System.IO.MemoryStream ms = new System.IO.MemoryStream(); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress); stream.Flush(); int nSize = length; byte[] decompressBuffer = new byte[nSize]; int nSizeIncept = stream.Read(decompressBuffer, 0, nSize); stream.Close(); return(System.Text.Encoding.Unicode.GetString(decompressBuffer, 0, nSizeIncept));//转换为普通的字符串 }
/// <summary> /// Decompresses and returns the <paramref name="source"/> byte array using the <see cref="System.IO.Compression.DeflateStream"/> algorithm. /// </summary> /// <param name="source">The byte array to decompress.</param> /// <returns>The decompressed <paramref name="source"/> byte array.</returns> public byte[] Decompress(byte[] source) { if (source == null) { return(source); } byte[] result = null; using (var ms = new System.IO.MemoryStream(source)) { using (var ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress, true)) { var byteArrayPieces = new List <byte[]>(); var count = 0; var length = 0; var readBuffer = new byte[_DecompressionReadBufferByteSize_]; while ((count = ds.Read(readBuffer, 0, _DecompressionReadBufferByteSize_)) > 0) { if (count == _DecompressionReadBufferByteSize_) { byteArrayPieces.Add(readBuffer); readBuffer = new byte[_DecompressionReadBufferByteSize_]; } else { byte[] tempBuffer = new byte[count]; Array.Copy(readBuffer, 0, tempBuffer, 0, count); byteArrayPieces.Add(tempBuffer); } length += count; } result = new byte[length]; var resultIndex = 0; foreach (var byteArrayPiece in byteArrayPieces) { Array.Copy(byteArrayPiece, 0, result, resultIndex, byteArrayPiece.Length); resultIndex += byteArrayPiece.Length; } } } return(result); }
protected static byte[] DecompressZLib(string base64EncodedText) { var msCompressed = new MemoryStream(Convert.FromBase64String(base64EncodedText)); var msInflated = new MemoryStream(base64EncodedText.Length * 2); // We must skip the first two bytes // See http://george.chiramattel.com/blog/2007/09/deflatestream-block-length-does-not-match.html msCompressed.ReadByte(); msCompressed.ReadByte(); using (var inflater = new System.IO.Compression.DeflateStream(msCompressed, System.IO.Compression.CompressionMode.Decompress)) { var buffer = new byte[4096]; while (inflater.CanRead) { var bytesRead = inflater.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } msInflated.Write(buffer, 0, bytesRead); } msInflated.Seek(0L, SeekOrigin.Begin); } byte[] byteArray; var totalBytesDecompressed = (int)msInflated.Length; if (totalBytesDecompressed > 0) { byteArray = new byte[totalBytesDecompressed]; msInflated.Read(byteArray, 0, totalBytesDecompressed); } else { byteArray = Array.Empty <byte>(); } return(byteArray); }
/// <summary> /// Looks up the given file in the Mix database and returns its contents as a <see cref="Byte"/> array. /// </summary> /// <param name="fileName">The file to find.</param> /// <returns>Returns a <see cref="Byte"/> array with the file's contents if found, null otherwise.</returns> public static byte[] GetBytes(string fileName) { Program.WriteLine("Mix.GetBytes({0})", fileName); //if (File.Exists(Path.Combine("data", fileName))) // return File.ReadAllBytes(Path.Combine("data", fileName)); if (!fileList.ContainsKey(fileName)) { throw new FileNotFoundException("File " + fileName + " was not found in the MIX files."); } byte[] ret; var entry = fileList[fileName]; if (entry.MixFile == null) { return(File.ReadAllBytes(entry.Filename)); } using (var mStream = new BinaryReader(File.Open(entry.MixFile, FileMode.Open))) { mStream.BaseStream.Seek(entry.Offset, SeekOrigin.Begin); if (!entry.IsCompressed) { ret = mStream.ReadBytes(entry.Length); } else { var cStream = new MemoryStream(mStream.ReadBytes(entry.Length)); var decompressor = new System.IO.Compression.DeflateStream(cStream, System.IO.Compression.CompressionMode.Decompress); var outStream = new MemoryStream(); var buffer = new byte[1024]; var recieved = 1; while (recieved > 0) { recieved = decompressor.Read(buffer, 0, buffer.Length); outStream.Write(buffer, 0, recieved); } ret = new byte[outStream.Length]; outStream.Seek(0, SeekOrigin.Begin); outStream.Read(ret, 0, ret.Length); } } return(ret); }
/// <summary> /// Returns a MemoryStream with the 'inflated' contents of the file specified in the <paramref name="path"/> parameter. /// </summary> /// <param name="path">Full path to the deflated file</param> /// <returns>Decompressed (inflated) MemoryStream</returns> public static MemoryStream Decompress(string path) { byte[] buffer = new byte[bufferLength]; int size; MemoryStream output = new MemoryStream(); using (FileStream fs = new FileStream(path, System.IO.FileMode.Open, FileAccess.Read, FileShare.Read, 8192)) { // MS, dare to be different ;-) // Skip the first two bytes, see : http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=97064 fs.ReadByte(); fs.ReadByte(); using (System.IO.Compression.DeflateStream inflaterStream = new System.IO.Compression.DeflateStream(fs, System.IO.Compression.CompressionMode.Decompress, false)) { while ((size = inflaterStream.Read(buffer, 0, buffer.Length)) > 0) output.Write(buffer, 0, size); } } return output; }
public static object DecompressAndDecode(byte[] input) { MemoryStream compressedStream = new MemoryStream(input); compressedStream.Seek(2, SeekOrigin.Begin); //skip zlib header, we're only interested in the deflated bytes byte[] outputBytes = new byte[4096]; DeflateStream stream = new DeflateStream(compressedStream, CompressionMode.Decompress); try { stream.Read(outputBytes, 0, (int) input.Length); stream.Flush(); stream.Close(); return Rencode.Decode(outputBytes); } catch (Exception e) { //Console.WriteLine("Exception reading stream"); } return null; }
public byte[] GetBytes(string fileName) { if (!fileList.ContainsKey(fileName)) { throw new FileNotFoundException(string.Format("File {0} was not found.", fileName)); } var entry = fileList[fileName]; if (entry.IsInFolder) { return(File.ReadAllBytes(Path.Combine(entry.Source, entry.Filename))); } byte[] ret; using (var mStream = new BinaryReader(File.Open(entry.Source, FileMode.Open))) { mStream.BaseStream.Seek(entry.Offset, SeekOrigin.Begin); if (!entry.IsCompressed) { ret = mStream.ReadBytes(entry.Length); } else { var cStream = new MemoryStream(mStream.ReadBytes(entry.Length)); var decompressor = new System.IO.Compression.DeflateStream(cStream, System.IO.Compression.CompressionMode.Decompress); var outStream = new MemoryStream(); var buffer = new byte[1024]; var recieved = 1; while (recieved > 0) { recieved = decompressor.Read(buffer, 0, buffer.Length); outStream.Write(buffer, 0, recieved); } ret = new byte[outStream.Length]; outStream.Seek(0, SeekOrigin.Begin); outStream.Read(ret, 0, ret.Length); } } return(ret); }
public static void ReadDynamics(PacketReader stream) { Logger.Log("Read dynamic instances: {0} Bytes.", stream.Length); int decomp = stream.ReadInt(); byte[] data = new byte[decomp]; using (var ms = new System.IO.MemoryStream(stream.GetRemainingData())) using (var ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress)) { ds.Read(data, 0, decomp); } stream.Load(data, decomp); // Read models if (stream.ReadBit()) { int count = stream.ReadUShort(); for (int i = 0; i < count; i++) { ModelInstance model = ScriptManager.Interface.CreateModelInstance(); model.ReadStream(stream); model.ScriptObject.Create(); } } // Read vob instances if (stream.ReadBit()) { int count = stream.ReadUShort(); for (int i = 0; i < count; i++) { byte type = stream.ReadByte(); BaseVobInstance inst = ScriptManager.Interface.CreateInstance(type); inst.ReadStream(stream); inst.ScriptObject.Create(); } } }
/// <summary> /// Returns a byte-array with the 'inflated' contents of the input array. /// </summary> /// <param name="input">inflated byte-array</param> /// <returns>Decompressed (inflated) byte-array</returns> public static byte[] Decompress(byte[] input) { byte[] buffer = new byte[1024]; int size; using (MemoryStream outputStream = new MemoryStream()) { using (MemoryStream inputStream = new MemoryStream(input)) { // MS, dare to be different ;-) // Skip the first two bytes, see : http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=97064 inputStream.ReadByte(); inputStream.ReadByte(); using (System.IO.Compression.DeflateStream inflaterStream = new System.IO.Compression.DeflateStream(inputStream, System.IO.Compression.CompressionMode.Decompress, false)) { while ((size = inflaterStream.Read(buffer, 0, buffer.Length)) > 0) outputStream.Write(buffer, 0, size); } } return outputStream.ToArray(); } }
private byte[] DecDeflateData(byte[] deflateData) { byte[] bytes = new byte[4096]; byte[] decoded; using (System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(new MemoryStream(deflateData), System.IO.Compression.CompressionMode.Decompress)) { using (MemoryStream memory = new MemoryStream()) { int count = 0; do { count = stream.Read(bytes, 0, 4096); if (count > 0) { memory.Write(bytes, 0, count); } }while (count > 0); decoded = memory.ToArray(); } } return(decoded); }
/// <summary> /// 解压数据 /// </summary> /// <param name="data"></param> /// <returns></returns> private byte[] DecompressData(byte[] data) { using (MemoryStream outBuffer = new MemoryStream()) { using (System.IO.Compression.DeflateStream compressedzipStream = new System.IO.Compression.DeflateStream(new MemoryStream(data, 2, data.Length - 2), System.IO.Compression.CompressionMode.Decompress)) { byte[] block = new byte[1024]; while (true) { int bytesRead = compressedzipStream.Read(block, 0, block.Length); if (bytesRead <= 0) { break; } else { outBuffer.Write(block, 0, bytesRead); } } compressedzipStream.Close(); return(outBuffer.ToArray()); } } }
// Gaetano 'FiR3N3T' Padalino Optimization - [email protected] internal static byte[] DecompressDeflate(System.IO.Stream streamInput) { using (System.IO.Stream streamOutput = new System.IO.MemoryStream()) { int iOutputLength = 0; byte[] readBuffer = new byte[4096]; using (System.IO.Compression.DeflateStream streamDeflate = new System.IO.Compression.DeflateStream(streamInput, System.IO.Compression.CompressionMode.Decompress)) { int i; while ((i = streamDeflate.Read(readBuffer, 0, readBuffer.Length)) != 0) { streamOutput.Write(readBuffer, 0, i); iOutputLength = iOutputLength + i; } } byte[] buffer = new byte[iOutputLength]; streamOutput.Position = 0; streamOutput.Read(buffer, 0, buffer.Length); return(buffer); } }
private void ReadScanlines(MemoryStream dataStream, byte[] pixels, IColorReader colorReader, PngColorTypeInformation colorTypeInformation) { // Read the zlib header : http://tools.ietf.org/html/rfc1950 // CMF(Compression Method and flags) // This byte is divided into a 4 - bit compression method and a // 4-bit information field depending on the compression method. // bits 0 to 3 CM Compression method // bits 4 to 7 CINFO Compression info // // 0 1 // +---+---+ // |CMF|FLG| // +---+---+ int cmf = dataStream.ReadByte(); int flag = dataStream.ReadByte(); //please note that position=2 int scanlineLength = CalculateScanlineLength(colorTypeInformation); int scanlineStep = CalculateScanlineStep(colorTypeInformation); byte[] lastScanline = new byte[scanlineLength]; byte[] currScanline = new byte[scanlineLength]; byte a = 0; byte b = 0; byte c = 0; int row = 0, filter = 0, column = -1; //using (InflaterInputStream compressedStream = new InflaterInputStream(dataStream)) //{ // int readByte = 0; // while ((readByte = compressedStream.ReadByte()) >= 0) // { // if (column == -1) // { // filter = readByte; // column++; // } // else // { // currScanline[column] = (byte)readByte; // if (column >= scanlineStep) // { // a = currScanline[column - scanlineStep]; // c = lastScanline[column - scanlineStep]; // } // else // { // a = 0; // c = 0; // } // b = lastScanline[column]; // if (filter == 1) // { // currScanline[column] = (byte)(currScanline[column] + a); // } // else if (filter == 2) // { // currScanline[column] = (byte)(currScanline[column] + b); // } // else if (filter == 3) // { // currScanline[column] = (byte)(currScanline[column] + (byte)Math.Floor((double)(a + b) / 2)); // } // else if (filter == 4) // { // currScanline[column] = (byte)(currScanline[column] + PaethPredicator(a, b, c)); // } // column++; // if (column == scanlineLength) // { // colorReader.ReadScanline(currScanline, pixels, _header); // column = -1; // row++; // Extensions.Swap(ref currScanline, ref lastScanline); // } // } // } //} //using (var decompressedMs = new System.IO.MemoryStream()) //using (var decompressedStream = new System.IO.Compression.DeflateStream( // dataStream, System.IO.Compression.CompressionMode.Decompress, true)) //{ // decompressedStream.Read() //} using (System.IO.Compression.DeflateStream compressedStream = new System.IO.Compression.DeflateStream( dataStream, System.IO.Compression.CompressionMode.Decompress, true)) //using (Ionic.Zlib.DeflateStream compressedStream = new Ionic.Zlib.DeflateStream(dataStream, Ionic.Zlib.CompressionMode.Decompress)) { int readByte = 0; byte[] singleByte = new byte[1]; compressedStream.Read(singleByte, 0, 1); while ((readByte = compressedStream.ReadByte()) >= 0) { if (column == -1) { filter = readByte; column++; } else { currScanline[column] = (byte)readByte; if (column >= scanlineStep) { a = currScanline[column - scanlineStep]; c = lastScanline[column - scanlineStep]; } else { a = 0; c = 0; } b = lastScanline[column]; if (filter == 1) { currScanline[column] = (byte)(currScanline[column] + a); } else if (filter == 2) { currScanline[column] = (byte)(currScanline[column] + b); } else if (filter == 3) { currScanline[column] = (byte)(currScanline[column] + (byte)Math.Floor((double)(a + b) / 2)); } else if (filter == 4) { currScanline[column] = (byte)(currScanline[column] + PaethPredicator(a, b, c)); } column++; if (column == scanlineLength) { colorReader.ReadScanline(currScanline, pixels, _header); column = -1; row++; // //Extensions.Swap(ref currScanline, ref lastScanline); var tmpA = currScanline; var tmpB = lastScanline; lastScanline = tmpA; currScanline = tmpB; } } } } }
public static void OutputResources(string dir, CacheFileGen3 cf, TI.Block<pages_block> block) { uint offset; int size; uint base_offset = (uint)(cf.Header as CacheHeaderGen3).Interop[CacheSectionType.Resource].CacheOffset; string path = string.Format(@"{0}\\{1}_resources\\", dir, cf.Header.Name); if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path); int x = -1; foreach (pages_block def in block) { x++; if (def.SharedCache.Value != -1) continue; size = def.BlockSizeUncompressed.Value; if (size <= 0) continue; offset = base_offset + (uint)def.BlockOffset.Value; cf.InputStream.Seek(offset); byte[] data; if (def.CompressionCodec.Value != -1) { using (var deflate = new System.IO.Compression.DeflateStream(cf.InputStream.BaseStream, System.IO.Compression.CompressionMode.Decompress, true)) { data = new byte[size]; deflate.Read(data, 0, data.Length); } } else data = cf.InputStream.ReadBytes(size); using(var fs = new FileStream(string.Format("{0}\\{1:X8}.bin", path, x), System.IO.FileMode.Create, System.IO.FileAccess.Write)) { fs.Write(data, 0, data.Length); } } }
public static MemoryStream DecompressDeflate(Stream thestream) { MemoryStream ms = new MemoryStream(); using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(thestream, System.IO.Compression.CompressionMode.Decompress,true)) { byte[] buffer = new byte[8192]; int bytesread = ds.Read(buffer, 0, buffer.Length); while (bytesread != 0) { ms.Write(buffer, 0, bytesread); bytesread = ds.Read(buffer, 0, buffer.Length); } ms.Position = 0; } return ms; }
public unsafe void CreateTexture(BinaryReader reader, int[] palette) { if (Width == 0 || Height == 0) return; Image = new Bitmap(Width, Height); MaskImage = new Bitmap(1, 1); BitmapData data = Image.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); byte[] bytes = new byte[0]; byte[] maskbytes = new byte[0]; MemoryStream output; switch (nType) { case 0://wemade wil file uncompressed if (palette.Length > 256) { bo16bit = true; nSize = nSize * 2; } bytes = reader.ReadBytes(nSize); break; case 1://shanda wzl file compressed case 4://shanda miz file compressed output = new MemoryStream(); Ionic.Zlib.ZlibStream deflateStream = new Ionic.Zlib.ZlibStream(output,Ionic.Zlib.CompressionMode.Decompress); deflateStream.Write(reader.ReadBytes(nSize), 0, nSize); bytes = output.ToArray(); deflateStream.Close(); output.Close(); break; case 2: byte Compressed = reader.ReadByte(); reader.ReadBytes(5); if (Compressed != 8) { bytes = reader.ReadBytes(nSize - 6); break; } MemoryStream input = new MemoryStream(reader.ReadBytes(nSize-6)); output = new MemoryStream(); byte[] buffer = new byte[10]; System.IO.Compression.DeflateStream decompress = new System.IO.Compression.DeflateStream(input, System.IO.Compression.CompressionMode.Decompress); int len; while ((len = decompress.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, len); } bytes = output.ToArray(); decompress.Close(); output.Close(); input.Close(); break; case 3: MaskImage = new Bitmap(Width, Height); byte[][] DecodedPixels = DecompressWemadeMir3(reader,Width,Height,nSize); if (DecodedPixels != null) { bytes = DecodedPixels[0]; if (HasMask) maskbytes = DecodedPixels[1]; } else { HasMask = false; bytes = new byte[Width * Height * 2]; } break; } int index = 0; int* scan0 = (int*) data.Scan0; { for (int y = Height - 1; y >= 0; y--) { for (int x = 0; x < Width; x++) { if (bo16bit) scan0[y * Width + x] = convert16bitTo32bit(bytes[index++] + (bytes[index++] << 8)); else scan0[y*Width + x] = palette[bytes[index++]]; } if (((nType == 1) || (nType == 4)) & (Width % 4 > 0)) index += WidthBytes(bo16bit ? 16 : 8, Width) - (Width * (bo16bit ? 2 : 1)); } } Image.UnlockBits(data); index = 0; if (HasMask) { BitmapData Maskdata = MaskImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); int* maskscan0 = (int*)Maskdata.Scan0; { for (int y = Height - 1; y >= 0; y--) { for (int x = 0; x < Width; x++) maskscan0[y * Width + x] = convert16bitTo32bit(maskbytes[index++] + (maskbytes[index++] << 8)); } } MaskImage.UnlockBits(Maskdata); } }
public hdErr read_block_into_cache(int block, byte[] cache, ref byte[] crc) { crc = null; mapentry mapEntry = _hd.map[block]; switch (mapEntry.flags & mapFlags.MAP_ENTRY_FLAG_TYPE_MASK) { case mapFlags.MAP_ENTRY_TYPE_COMPRESSED: { if (mapEntry.BlockCache != null) { Buffer.BlockCopy(mapEntry.BlockCache, 0, cache, 0, (int)_hd.blocksize); //already checked CRC for this block when the cache was made break; } _hd.file.Seek((long)_hd.map[block].offset, SeekOrigin.Begin); switch (_hd.compression) { case HDCOMPRESSION_ZLIB: case HDCOMPRESSION_ZLIB_PLUS: { using (var st = new System.IO.Compression.DeflateStream(_hd.file, System.IO.Compression.CompressionMode.Decompress, true)) { int bytes = st.Read(cache, 0, (int)_hd.blocksize); if (bytes != (int)_hd.blocksize) { return(hdErr.HDERR_READ_ERROR); } if (mapEntry.UseCount > 0) { mapEntry.BlockCache = new byte[bytes]; Buffer.BlockCopy(cache, 0, mapEntry.BlockCache, 0, bytes); } } if ((mapEntry.flags & mapFlags.MAP_ENTRY_FLAG_NO_CRC) == 0) { _crc = BitConverter.GetBytes(mapEntry.crc); } break; } default: { Console.WriteLine("Unknown compression"); return(hdErr.HDERR_DECOMPRESSION_ERROR); } } break; } case mapFlags.MAP_ENTRY_TYPE_UNCOMPRESSED: { _hd.file.Seek((long)_hd.map[block].offset, SeekOrigin.Begin); int bytes = _hd.file.Read(cache, 0, (int)_hd.blocksize); if (bytes != (int)_hd.blocksize) { return(hdErr.HDERR_READ_ERROR); } if ((mapEntry.flags & mapFlags.MAP_ENTRY_FLAG_NO_CRC) == 0) { _crc = BitConverter.GetBytes(mapEntry.crc); } break; } case mapFlags.MAP_ENTRY_TYPE_MINI: { byte[] tmp = BitConverter.GetBytes(_hd.map[block].offset); for (int i = 0; i < 8; i++) { cache[i] = tmp[7 - i]; } for (int i = 8; i < _hd.blocksize; i++) { cache[i] = cache[i - 8]; } if ((mapEntry.flags & mapFlags.MAP_ENTRY_FLAG_NO_CRC) == 0) { _crc = BitConverter.GetBytes(mapEntry.crc); } break; } case mapFlags.MAP_ENTRY_TYPE_SELF_HUNK: { hdErr ret = read_block_into_cache((int)mapEntry.offset, cache, ref crc); if (ret != hdErr.HDERR_NONE) { return(ret); } break; } default: return(hdErr.HDERR_DECOMPRESSION_ERROR); } return(hdErr.HDERR_NONE); }
internal byte[] GetSegmentData(CacheFileGen3 cf, cache_file_resource_layout_table owner, int segment_offset) { if (PageData == null) { #region shared cache handling if (SharedCache.Value != -1) { return(null); } if (SharedCache.Value > -1) // above stmt disables this code for now { shared_cache_block scb = owner.SharedCaches[SharedCache.Value]; bool is_internal; var scf = Program.GetManager(cf.EngineVersion).GetCacheFileFromLocation(cf.EngineVersion, scb.CachePath.Value, out is_internal) as CacheFileGen3; if (!is_internal) { cf = scf; } // if it says its internal, the shared cache file wasn't loaded else { throw new Debug.Exceptions.UnreachableException(); } } #endregion // If the page is valid, figure out the decompression size int size = BlockSizeUncompressed.Value; if (size <= 0) { return(null); } #region Perform codec operations uint base_offset = (uint)(cf.Header as CacheHeaderGen3).Interop[Blam.CacheSectionType.Resource].CacheOffset; uint offset = base_offset + (uint)BlockOffset.Value; cf.InputStream.Seek(offset); if (CompressionCodec.Value != -1) { // TODO: we would need to test the GUID here if\when bungie adds // more compression codecs. Since deflation and no-codec are the // only things they do use, we can just assume for now. using (var deflate = new System.IO.Compression.DeflateStream(cf.InputStream.BaseStream, System.IO.Compression.CompressionMode.Decompress, true)) { PageData = new byte[size]; deflate.Read(PageData, 0, PageData.Length); } } // No codec used, plain data else { PageData = cf.InputStream.ReadBytes(size); } #endregion } int segment_size = GetSegmentSize(segment_offset); if (segment_size == -1) { return(null); // offset was either invalid or sizes haven't been post-processed } byte[] segment_data = new byte[segment_size]; // Extract the segment data from the page Array.Copy(PageData, segment_offset, segment_data, 0, segment_data.Length); return(segment_data); }
public void ExtractEntry(Entry entry, Stream outputStream, ZipProgressHandler progressHandler = null) { if (entry.Offset >= _Stream.Length) throw new Exception("Invalid ZIP entry offset"); _Stream.Seek(entry.Offset, SeekOrigin.Begin); byte[] buffer = new byte[1024 * 1024]; _Stream.Read(buffer, 0, 30); ushort fileNameLength = BitConverter.ToUInt16(buffer, 26); ushort extraFieldLength = BitConverter.ToUInt16(buffer, 28); _Stream.Seek(entry.Offset + 30 + fileNameLength + extraFieldLength, SeekOrigin.Begin); Stream dataStream; switch (entry.CompressionMethod) { case 0: dataStream = _Stream; break; case 8: dataStream = new System.IO.Compression.DeflateStream(_Stream, System.IO.Compression.CompressionMode.Decompress, true); break; default: throw new Exception("Unsupported ZIP compression method: " + entry.CompressionMethod + " for " + entry.FileName); } uint done = 0; while (done < entry.UncompressedSize) { int todo = (int)Math.Min(entry.UncompressedSize - done, (uint)buffer.Length); if (dataStream.Read(buffer, 0, todo) != todo) throw new Exception("Cannot read data from ZIP stream for " + entry.FileName); outputStream.Write(buffer, 0, todo); done += (uint)todo; if (progressHandler != null) progressHandler(done, entry.UncompressedSize); } if (dataStream != _Stream) dataStream.Dispose(); }
public string GetMatchClientPayloadString() { string thiscontentType = GetHeaderMatchClient("Content-Type:"); if (string.IsNullOrEmpty(thiscontentType)) return ""; string retval = ""; foreach (string contenttype in TextContentTypes) { if (thiscontentType.ToLower().Contains(contenttype)) { string contentencoding = GetHeaderMatchClient("Content-Encoding:"); _matchClientPayload.Position = 0; if (contentencoding.ToLower().Contains("deflate")) { System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(_matchClientPayload, System.IO.Compression.CompressionMode.Decompress); byte[] buffer = new byte[8192]; MemoryStream ms = new MemoryStream(); int bytesread = ds.Read(buffer, 0, buffer.Length); while (bytesread != 0) { ms.Write(buffer, 0, bytesread); bytesread = ds.Read(buffer, 0, buffer.Length); } retval = System.Text.Encoding.Default.GetString(ms.GetBuffer()); } else if (contentencoding.ToLower().Contains("gzip")) { System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(_matchClientPayload, System.IO.Compression.CompressionMode.Decompress); byte[] buffer = new byte[8192]; MemoryStream ms = new MemoryStream(); int bytesread = 0; try { bytesread = gz.Read(buffer, 0, buffer.Length); } catch (Exception ex) { _downStreamExceptions.Add(ex); } while (bytesread != 0 & gz.CanRead) { ms.Write(buffer, 0, bytesread); try { bytesread = gz.Read(buffer, 0, buffer.Length); } catch (Exception ex) { _downStreamExceptions.Add(ex); break; } } retval = System.Text.Encoding.Default.GetString(ms.GetBuffer(), 0, (int)ms.Length); } else { byte[] thebytes = new byte[_matchClientPayload.Length]; _matchClientPayload.Read(thebytes, 0,(int) _matchClientPayload.Length); retval = System.Text.Encoding.Default.GetString(thebytes, 0, (int)_matchClientPayload.Length); } break; } } return retval; }
// thank you: http://www.dotnetperls.com/decompress public static byte[] DecompressBytes(byte[] gzip) { try { byte[] result; using (MemoryStream ms = new MemoryStream()) { ms.Write(gzip, 0, gzip.Length); ms.Position = 0L; using (System.IO.Compression.GZipStream stream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress, true)) { result = ToByteArray(stream); } } if (result == null) { throw new Exception("Result was null in GZip decompress."); } return(result); } catch (InvalidDataException /* ex */) { //Utils.Logger(ex.GetType().ToString() + ": " + ex.Message); return(gzip); } catch // (Exception ex) { //Utils.Logger(ex.GetType().ToString() + ": " + ex.Message); try { using (System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(new MemoryStream(gzip), System.IO.Compression.CompressionMode.Decompress)) { const int size = 4096; byte[] buffer = new byte[size]; using (MemoryStream memory = new MemoryStream()) { int count = 0; do { count = stream.Read(buffer, 0, size); if (count > 0) { memory.Write(buffer, 0, count); } }while (count > 0); return(memory.ToArray()); } } } catch { return(gzip); } } }
public byte[] GetMatchClientPayload() { Stream thestream = null; string transferEncoding = GetHeaderMatchClient("Transfer-Encoding:"); if (transferEncoding.Contains("chunked")) { thestream = DoChunkedTransferEndcoding(_matchClientPayload); } else { thestream = _matchClientPayload; } byte[] retval = null; string contentencoding = GetHeaderMatchClient("Content-Encoding:"); thestream.Position = 0; if (contentencoding.ToLower().Contains("deflate")) { System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(thestream, System.IO.Compression.CompressionMode.Decompress); byte[] buffer = new byte[8192]; MemoryStream ms = new MemoryStream(); int bytesread = ds.Read(buffer, 0, buffer.Length); while (bytesread != 0) { ms.Write(buffer, 0, bytesread); bytesread = ds.Read(buffer, 0, buffer.Length); } retval = ms.GetBuffer(); } else if (contentencoding.ToLower().Contains("gzip")) { System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(thestream, System.IO.Compression.CompressionMode.Decompress); byte[] buffer = new byte[8192]; MemoryStream ms = new MemoryStream(); int bytesread = 0; try { bytesread = gz.Read(buffer, 0, buffer.Length); } catch (Exception ex) { _downStreamExceptions.Add(ex); retval = new byte[_matchClientPayload.Length]; thestream.Position = 0; thestream.Read(retval, 0, (int)thestream.Length); } while (bytesread != 0 & gz.CanRead) { ms.Write(buffer, 0, bytesread); try { bytesread = gz.Read(buffer, 0, buffer.Length); } catch(Exception ex) { _downStreamExceptions.Add(ex); break; } } retval = new byte[ms.Length]; ms.Position = 0; ms.Read(retval, 0, (int)ms.Length); } else { retval = new byte[thestream.Length]; thestream.Position = 0; thestream.Read(retval, 0, (int)thestream.Length); } return retval; }
// thank you: http://www.dotnetperls.com/decompress public static byte[] DecompressBytes(byte[] gzip) { try { byte[] result; using (MemoryStream ms = new MemoryStream()) { ms.Write(gzip, 0, gzip.Length); ms.Position = 0L; using (System.IO.Compression.GZipStream stream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress, true)) { result = ToByteArray(stream); } } if (result == null) throw new Exception("Result was null in GZip decompress."); return result; } catch (InvalidDataException /* ex */) { //Utils.Logger(ex.GetType().ToString() + ": " + ex.Message); return gzip; } catch // (Exception ex) { //Utils.Logger(ex.GetType().ToString() + ": " + ex.Message); try { using (System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(new MemoryStream(gzip), System.IO.Compression.CompressionMode.Decompress)) { const int size = 4096; byte[] buffer = new byte[size]; using (MemoryStream memory = new MemoryStream()) { int count = 0; do { count = stream.Read(buffer, 0, size); if (count > 0) memory.Write(buffer, 0, count); } while (count > 0); return memory.ToArray(); } } } catch { return gzip; } } }
private hdErr read_block_into_cache(hard_disk_info info, int block) { bool checkCrc = true; mapentry mapEntry = info.map[block]; switch (mapEntry.flags & mapFlags.MAP_ENTRY_FLAG_TYPE_MASK) { case mapFlags.MAP_ENTRY_TYPE_COMPRESSED: { if (mapEntry.BlockCache != null) { Buffer.BlockCopy(mapEntry.BlockCache, 0, cache, 0, (int)info.blocksize); //already checked CRC for this block when the cache was made checkCrc = false; break; } info.file.Seek((long)info.map[block].offset, SeekOrigin.Begin); switch (info.compression) { case HDCOMPRESSION_ZLIB: case HDCOMPRESSION_ZLIB_PLUS: { using (var st = new System.IO.Compression.DeflateStream(info.file, System.IO.Compression.CompressionMode.Decompress, true)) { int bytes = st.Read(cache, 0, (int)info.blocksize); if (bytes != (int)info.blocksize) return hdErr.HDERR_READ_ERROR; if (mapEntry.UseCount > 0) { mapEntry.BlockCache = new byte[bytes]; Buffer.BlockCopy(cache, 0, mapEntry.BlockCache, 0, bytes); } } break; } default: { Console.WriteLine("Unknown compression"); return hdErr.HDERR_DECOMPRESSION_ERROR; } } break; } case mapFlags.MAP_ENTRY_TYPE_UNCOMPRESSED: { info.file.Seek((long)info.map[block].offset, SeekOrigin.Begin); int bytes = info.file.Read(cache, 0, (int)info.blocksize); if (bytes != (int)info.blocksize) return hdErr.HDERR_READ_ERROR; break; } case mapFlags.MAP_ENTRY_TYPE_MINI: { byte[] tmp = BitConverter.GetBytes(info.map[block].offset); for (int i = 0; i < 8; i++) { cache[i] = tmp[7 - i]; } for (int i = 8; i < info.blocksize; i++) { cache[i] = cache[i - 8]; } break; } case mapFlags.MAP_ENTRY_TYPE_SELF_HUNK: { hdErr ret = read_block_into_cache(info, (int)mapEntry.offset); if (ret != hdErr.HDERR_NONE) return ret; // check CRC in the read_block_into_cache call checkCrc = false; break; } default: return hdErr.HDERR_DECOMPRESSION_ERROR; } if (checkCrc && (mapEntry.flags & mapFlags.MAP_ENTRY_FLAG_NO_CRC) == 0) { if (!CRC.VerifyDigest(mapEntry.crc, cache, 0, info.blocksize)) return hdErr.HDERR_DECOMPRESSION_ERROR; } return hdErr.HDERR_NONE; }
public byte[] GetClientPayload() { byte[] retval = null; string contentencoding = GetHeaderClient("Content-Encoding:"); _clientPayload.Position = 0; if (contentencoding.ToLower().Contains("deflate")) { System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(_clientPayload, System.IO.Compression.CompressionMode.Decompress); byte[] buffer = new byte[8192]; MemoryStream ms = new MemoryStream(); int bytesread = ds.Read(buffer, 0, buffer.Length); while (bytesread != 0) { ms.Write(buffer, 0, bytesread); bytesread = ds.Read(buffer, 0, buffer.Length); } retval = ms.GetBuffer(); } else if (contentencoding.ToLower().Contains("gzip")) { System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(_clientPayload, System.IO.Compression.CompressionMode.Decompress); byte[] buffer = new byte[8192]; MemoryStream ms = new MemoryStream(); int bytesread = 0; try { bytesread = gz.Read(buffer, 0, buffer.Length); } catch (Exception ex) { _upStreamExceptions.Add(ex); } while (bytesread != 0 & gz.CanRead) { ms.Write(buffer, 0, bytesread); try { bytesread = gz.Read(buffer, 0, buffer.Length); } catch (Exception ex) { _upStreamExceptions.Add(ex); break; } } retval = new byte[ms.Length]; ms.Position = 0; ms.Read(retval, 0, (int)ms.Length); } else { retval = new byte[_clientPayload.Length]; _clientPayload.Position = 0; _clientPayload.Read(retval, 0, (int)_clientPayload.Length); } return retval; }
public void Zlib_Streams_VariousSizes() { byte[] working = new byte[WORKING_BUFFER_SIZE]; int n = -1; Int32[] Sizes = { 8000, 88000, 188000, 388000, 580000, 1580000 }; for (int p = 0; p < Sizes.Length; p++) { // both binary and text files for (int m = 0; m < 2; m++) { int sz = this.rnd.Next(Sizes[p]) + Sizes[p]; string FileToCompress = System.IO.Path.Combine(TopLevelDir, String.Format("Zlib_Streams.{0}.{1}", sz, (m == 0) ? "txt" : "bin")); Assert.IsFalse(System.IO.File.Exists(FileToCompress), "The temporary file '{0}' already exists.", FileToCompress); TestContext.WriteLine("Creating file {0} {1} bytes", FileToCompress, sz); if (m == 0) CreateAndFillFileText(FileToCompress, sz); else _CreateAndFillBinary(FileToCompress, sz, false); int crc1 = DoCrc(FileToCompress); TestContext.WriteLine("Initial CRC: 0x{0:X8}", crc1); // try both GZipStream and DeflateStream for (int k = 0; k < 2; k++) { // compress with Alienlab.and System.IO.Compression for (int i = 0; i < 2; i++) { string CompressedFileRoot = String.Format("{0}.{1}.{2}.compressed", FileToCompress, (k == 0) ? "GZIP" : "DEFLATE", (i == 0) ? "Ionic" : "BCL"); int x = k + i * 2; int z = (x == 0) ? 4 : 1; // why 4 trials?? (only for GZIP and Alienlab. for (int h = 0; h < z; h++) { string CompressedFile = (x == 0) ? CompressedFileRoot + ".trial" + h : CompressedFileRoot; using (var input = System.IO.File.OpenRead(FileToCompress)) { using (var raw = System.IO.File.Create(CompressedFile)) { Stream compressor = null; try { switch (x) { case 0: // k == 0, i == 0 compressor = new Alienlab.Zlib.GZipStream(raw, CompressionMode.Compress, true); break; case 1: // k == 1, i == 0 compressor = new Alienlab.Zlib.DeflateStream(raw, CompressionMode.Compress, true); break; case 2: // k == 0, i == 1 compressor = new System.IO.Compression.GZipStream(raw, System.IO.Compression.CompressionMode.Compress, true); break; case 3: // k == 1, i == 1 compressor = new System.IO.Compression.DeflateStream(raw, System.IO.Compression.CompressionMode.Compress, true); break; } //TestContext.WriteLine("Compress with: {0} ..", compressor.GetType().FullName); TestContext.WriteLine("........{0} ...", System.IO.Path.GetFileName(CompressedFile)); if (x == 0) { if (h != 0) { Alienlab.Zlib.GZipStream gzip = compressor as Alienlab.Zlib.GZipStream; if (h % 2 == 1) gzip.FileName = FileToCompress; if (h > 2) gzip.Comment = "Compressing: " + FileToCompress; } } n = -1; while ((n = input.Read(working, 0, working.Length)) != 0) { compressor.Write(working, 0, n); } } finally { if (compressor != null) compressor.Dispose(); } } } // now, decompress with Alienlab.and System.IO.Compression // for (int j = 0; j < 2; j++) for (int j = 1; j >= 0; j--) { using (var input = System.IO.File.OpenRead(CompressedFile)) { Stream decompressor = null; try { int w = k + j * 2; switch (w) { case 0: // k == 0, j == 0 decompressor = new Alienlab.Zlib.GZipStream(input, CompressionMode.Decompress, true); break; case 1: // k == 1, j == 0 decompressor = new Alienlab.Zlib.DeflateStream(input, CompressionMode.Decompress, true); break; case 2: // k == 0, j == 1 decompressor = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress, true); break; case 3: // k == 1, j == 1 decompressor = new System.IO.Compression.DeflateStream(input, System.IO.Compression.CompressionMode.Decompress, true); break; } //TestContext.WriteLine("Decompress: {0} ...", decompressor.GetType().FullName); string DecompressedFile = String.Format("{0}.{1}.decompressed", CompressedFile, (j == 0) ? "Ionic" : "BCL"); TestContext.WriteLine("........{0} ...", System.IO.Path.GetFileName(DecompressedFile)); using (var s2 = System.IO.File.Create(DecompressedFile)) { n = -1; while (n != 0) { n = decompressor.Read(working, 0, working.Length); if (n > 0) s2.Write(working, 0, n); } } int crc2 = DoCrc(DecompressedFile); Assert.AreEqual<UInt32>((UInt32)crc1, (UInt32)crc2); } finally { if (decompressor != null) decompressor.Dispose(); } } } } } } } } TestContext.WriteLine("Done."); }
public unsafe void CreateTexture(BinaryReader reader, int[] palette) { try { if (Width == 0 || Height == 0) { return; } Image = new Bitmap(Width, Height); MaskImage = new Bitmap(1, 1); BitmapData data = Image.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); byte[] bytes = new byte[0]; byte[] maskbytes = new byte[0]; MemoryStream output; switch (nType) { case 0: //wemade wil file uncompressed if (palette.Length > 256) { bo16bit = true; nSize = nSize * 2; } bytes = reader.ReadBytes(nSize); break; case 1: //shanda wzl file compressed case 4: //shanda miz file compressed output = new MemoryStream(); Ionic.Zlib.ZlibStream deflateStream = new Ionic.Zlib.ZlibStream(output, Ionic.Zlib.CompressionMode.Decompress); deflateStream.Write(reader.ReadBytes(nSize), 0, nSize); bytes = output.ToArray(); deflateStream.Close(); output.Close(); break; case 2: byte Compressed = reader.ReadByte(); reader.ReadBytes(5); if (Compressed != 8) { bytes = reader.ReadBytes(nSize - 6); break; } MemoryStream input = new MemoryStream(reader.ReadBytes(nSize - 6)); output = new MemoryStream(); byte[] buffer = new byte[10]; System.IO.Compression.DeflateStream decompress = new System.IO.Compression.DeflateStream(input, System.IO.Compression.CompressionMode.Decompress); int len; while ((len = decompress.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, len); } bytes = output.ToArray(); decompress.Close(); output.Close(); input.Close(); break; case 3: MaskImage = new Bitmap(Width, Height); byte[][] DecodedPixels = DecompressWemadeMir3(reader, Width, Height, nSize); if (DecodedPixels != null) { bytes = DecodedPixels[0]; if (HasMask) { maskbytes = DecodedPixels[1]; } } else { HasMask = false; bytes = new byte[Width * Height * 2]; } break; } int index = 0; int *scan0 = (int *)data.Scan0; { for (int y = Height - 1; y >= 0; y--) { for (int x = 0; x < Width; x++) { if (bo16bit) { scan0[y * Width + x] = convert16bitTo32bit(bytes[index++] + (bytes[index++] << 8)); } else { scan0[y * Width + x] = palette[bytes[index++]]; } } if (((nType == 1) || (nType == 4)) & (Width % 4 > 0)) { index += WidthBytes(bo16bit ? 16 : 8, Width) - (Width * (bo16bit ? 2 : 1)); } } } Image.UnlockBits(data); index = 0; if (HasMask) { BitmapData Maskdata = MaskImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); int * maskscan0 = (int *)Maskdata.Scan0; { for (int y = Height - 1; y >= 0; y--) { for (int x = 0; x < Width; x++) { maskscan0[y * Width + x] = convert16bitTo32bit(maskbytes[index++] + (maskbytes[index++] << 8)); } } } MaskImage.UnlockBits(Maskdata); } } catch (Exception) { } }
internal byte[] GetSegmentData(CacheFileGen3 cf, cache_file_resource_layout_table owner, int segment_offset) { if (PageData == null) { #region shared cache handling if (SharedCache.Value != -1) return null; if (SharedCache.Value > -1) // above stmt disables this code for now { shared_cache_block scb = owner.SharedCaches[SharedCache.Value]; bool is_internal; var scf = Program.GetManager(cf.EngineVersion).GetCacheFileFromLocation(cf.EngineVersion, scb.CachePath.Value, out is_internal) as CacheFileGen3; if (!is_internal) cf = scf; // if it says its internal, the shared cache file wasn't loaded else throw new Debug.Exceptions.UnreachableException(); } #endregion // If the page is valid, figure out the decompression size int size = BlockSizeUncompressed.Value; if (size <= 0) return null; #region Perform codec operations uint base_offset = (uint)(cf.Header as CacheHeaderGen3).Interop[Blam.CacheSectionType.Resource].CacheOffset; uint offset = base_offset + (uint)BlockOffset.Value; cf.InputStream.Seek(offset); if (CompressionCodec.Value != -1) { // TODO: we would need to test the GUID here if\when bungie adds // more compression codecs. Since deflation and no-codec are the // only things they do use, we can just assume for now. using (var deflate = new System.IO.Compression.DeflateStream(cf.InputStream.BaseStream, System.IO.Compression.CompressionMode.Decompress, true)) { PageData = new byte[size]; deflate.Read(PageData, 0, PageData.Length); } } // No codec used, plain data else PageData = cf.InputStream.ReadBytes(size); #endregion } int segment_size = GetSegmentSize(segment_offset); if (segment_size == -1) return null; // offset was either invalid or sizes haven't been post-processed byte[] segment_data = new byte[segment_size]; // Extract the segment data from the page Array.Copy(PageData, segment_offset, segment_data, 0, segment_data.Length); return segment_data; }
private byte[] Decode(string path) { var result = new byte[0]; using (var stream = new System.IO.FileStream(path, System.IO.FileMode.Open)) { var header = new PngHeader(stream); var chunks = new List <PngChunk>(); while (stream.Position < stream.Length) { var chunk = new PngChunk(stream); chunks.Add(chunk); if (chunk.Type == "IEND") { break; } } var ihdr = chunks.Where(a => a.Type == "IHDR").FirstOrDefault(); var ihdrData = ihdr.IRDRInfo; Width = ihdrData.Width; Height = ihdrData.Height; var data = new List <byte>(); for (var i = 0; i < chunks.Count; i++) { if (chunks[i].Type == "IDAT") { data.AddRange(chunks[i].Data); } } var pngData = new List <byte>(); // The first byte 0x78 is called CMF, and the value means CM = 8, CINFO = 7. CM = 8 denotes the "deflate" // compression method with a window size up to 32K. This is the method used by gzip and PNG. CINFO = 7 indicates a // 32K window size. // The 0x9C is called FLG and the value means FLEVEL = 2, CHECK = 28. // The information in FLEVEL is not needed for decompression; it is there to indicate if recompression might be worthwhile. // CHECK is set to whatever value is necessary such that CMF*256 + FLG is a multiple of 31 // we'll ignore the 2 bytees using (var deflate = new System.IO.Compression.DeflateStream(new System.IO.MemoryStream(data.Skip(2).Take(data.Count - 2).ToArray()), System.IO.Compression.CompressionMode.Decompress)) { var buffer = new byte[32768]; while (deflate.CanRead) { int count = deflate.Read(buffer, 0, buffer.Length); pngData.AddRange(buffer.Take(count)); if (count < 32768) { break; } } } var bytesPerPixel = 3; _dataStructure = ImageDataStructure.Rgb; switch (ihdrData.ColorType) { case PngChunk.ColorTypeCode.ColorUsedAlphaChannelUsed: bytesPerPixel = 4; _dataStructure = ImageDataStructure.Rgba; break; } result = new byte[Width * Height * bytesPerPixel]; var previousScanLine = new byte[Width * bytesPerPixel]; var scanLine = new byte[Width * bytesPerPixel]; var pngArray = pngData.ToArray(); var dataLineWithFilter = Width * bytesPerPixel + 1; for (var i = 0; i < Height; i++) { // first byte of each line specifies the filter var filter = (PngChunk.FilterTypeCode)pngArray[dataLineWithFilter * i]; // copy the data of the scan line Array.Copy(pngArray, dataLineWithFilter * i + 1, scanLine, 0, scanLine.Length); previousScanLine = Defilter(filter, scanLine, previousScanLine, bytesPerPixel); Array.Copy(previousScanLine, 0, result, scanLine.Length * i, scanLine.Length); } } return(result); }
private async void Listen() { Stream _netStream = _clientSocket.InputStream.AsStreamForRead(1024); byte[] stableBuffer = new byte[1024]; while (true) { if (!_StartState) { return; } try { _netStream.ReadB(stableBuffer, 0, 4); var packetlength = BitConverter.ToInt32(stableBuffer, 0); packetlength = IPAddress.NetworkToHostOrder(packetlength); if (packetlength < 16) { throw new NotSupportedException("协议失败: (L:" + packetlength + ")"); } _netStream.ReadB(stableBuffer, 0, 2); //magic _netStream.ReadB(stableBuffer, 0, 2); //protocol_version _netStream.ReadB(stableBuffer, 0, 4); var typeId = BitConverter.ToInt32(stableBuffer, 0); typeId = IPAddress.NetworkToHostOrder(typeId); _netStream.ReadB(stableBuffer, 0, 4);//magic, params? var playloadlength = packetlength - 16; if (playloadlength == 0) { continue;//没有内容了 } typeId = typeId - 1; var buffer = new byte[playloadlength]; _netStream.ReadB(buffer, 0, playloadlength); if (typeId == 2) { var viewer = BitConverter.ToUInt32(buffer.Take(4).Reverse().ToArray(), 0); //观众人数 if (NewMessage != null) { NewMessage(null, new LiveDanmuModel() { type = LiveDanmuTypes.Viewer, viewer = Convert.ToInt32(viewer) }); } Debug.WriteLine(viewer); continue; } var json_str = ""; try { //临时解决方案,可以优化 //参考https://github.com/Bililive/BililiveRecorder using (MemoryStream outBuffer = new MemoryStream()) { using (System.IO.Compression.DeflateStream compressedzipStream = new System.IO.Compression.DeflateStream(new MemoryStream(buffer, 2, playloadlength - 2), System.IO.Compression.CompressionMode.Decompress)) { byte[] block = new byte[1024]; while (true) { int bytesRead = compressedzipStream.Read(block, 0, block.Length); if (bytesRead <= 0) { break; } else { outBuffer.Write(block, 0, bytesRead); } } compressedzipStream.Close(); buffer = outBuffer.ToArray(); } } json_str = Regex.Replace(Encoding.UTF8.GetString(buffer, 16, buffer.Length - 16), "}\\0\\0.*?\\0\\0{", "},{"); } catch (Exception) { json_str = Encoding.UTF8.GetString(buffer, 0, buffer.Length); } if (json_str.Trim().Length != 0) { json_str = "[" + json_str + "]"; Debug.WriteLine(json_str); JArray json_array = JArray.Parse(json_str); foreach (var obj in json_array) { if (obj["cmd"] == null) { continue; } if (obj["cmd"].ToString().Contains("DANMU_MSG")) { var v = new DanmuMsgModel(); if (obj["info"] != null && obj["info"].ToArray().Length != 0) { v.text = obj["info"][1].ToString(); if (obj["info"][2] != null && obj["info"][2].ToArray().Length != 0) { v.username = obj["info"][2][1].ToString() + ":"; //v.usernameColor = GetColor(obj["info"][2][0].ToString()); if (obj["info"][2][3] != null && Convert.ToInt32(obj["info"][2][3].ToString()) == 1) { v.vip = "老爷"; v.isVip = Visibility.Visible; } if (obj["info"][2][4] != null && Convert.ToInt32(obj["info"][2][4].ToString()) == 1) { v.vip = "年费老爷"; v.isVip = Visibility.Collapsed; v.isBigVip = Visibility.Visible; } if (obj["info"][2][2] != null && Convert.ToInt32(obj["info"][2][2].ToString()) == 1) { v.vip = "房管"; v.isAdmin = Visibility.Visible; } } if (obj["info"][3] != null && obj["info"][3].ToArray().Length != 0) { v.medal_name = obj["info"][3][1].ToString(); v.medal_lv = obj["info"][3][0].ToString(); v.medalColor = obj["info"][3][4].ToString(); v.hasMedal = Visibility.Visible; } if (obj["info"][4] != null && obj["info"][4].ToArray().Length != 0) { v.ul = "UL" + obj["info"][4][0].ToString(); v.ulColor = obj["info"][4][2].ToString(); } if (obj["info"][5] != null && obj["info"][5].ToArray().Length != 0) { v.user_title = obj["info"][5][0].ToString(); v.hasTitle = Visibility.Visible; } if (NewMessage != null) { NewMessage(null, new LiveDanmuModel() { type = LiveDanmuTypes.Danmu, value = v }); } } } //19/10/01,cmd DANMU_MSG变成了DANMU_MSG:4:0:2:2:2:0 switch (obj["cmd"].ToString()) { //case "DANMU_MSG": // break; case "SEND_GIFT": var g = new GiftMsgModel(); if (obj["data"] != null) { g.uname = obj["data"]["uname"].ToString(); g.action = obj["data"]["action"].ToString(); g.giftId = Convert.ToInt32(obj["data"]["giftId"].ToString()); g.giftName = obj["data"]["giftName"].ToString(); g.num = obj["data"]["num"].ToString(); g.uid = obj["data"]["uid"].ToString(); if (NewMessage != null) { NewMessage(null, new LiveDanmuModel() { type = LiveDanmuTypes.Gift, value = g }); } } break; case "WELCOME": var w = new WelcomeMsgModel(); if (obj["data"] != null) { w.uname = obj["data"]["uname"].ToString(); w.uid = obj["data"]["uid"].ToString(); w.svip = obj["data"]["vip"].ToInt32() != 1; if (NewMessage != null) { NewMessage(null, new LiveDanmuModel() { type = LiveDanmuTypes.Welcome, value = w }); } } break; case "SYS_MSG": if (obj["msg"] != null) { if (NewMessage != null) { NewMessage(null, new LiveDanmuModel() { type = LiveDanmuTypes.SystemMsg, value = obj["msg"].ToString() }); } } break; case "ANCHOR_LOT_START": if (obj["data"] != null) { if (NewMessage != null) { NewMessage(null, new LiveDanmuModel() { type = LiveDanmuTypes.ANCHOR_LOT_START, value = obj["data"].ToString() }); } } break; case "ANCHOR_LOT_AWARD": if (obj["data"] != null) { if (NewMessage != null) { NewMessage(null, new LiveDanmuModel() { type = LiveDanmuTypes.ANCHOR_LOT_AWARD, value = obj["data"].ToString() }); } } break; default: break; } await Task.Delay(delay); } } // } } catch (Exception ex) { LogHelper.Log("加载直播弹幕失败", LogType.ERROR, ex); } await Task.Delay(delay); } }