/// <summary> /// Reimplements function from zlib (uncompress) that is not present in ZLIB.NET. /// </summary> /// <param name="dest">Destination array of bytes. May be trimmed if necessary.</param> /// <param name="source">Source array of bytes.</param> /// <returns>Zlib status code.</returns> private static int ZlibUncompress(ref byte[] dest, byte[] source) { ZStream stream = new ZStream(); int err; stream.next_in = source; stream.avail_in = source.Length; stream.next_out = dest; stream.avail_out = dest.Length; err = stream.inflateInit(); if (err != zlibConst.Z_OK) return err; err = stream.inflate(zlibConst.Z_FINISH); if (err != zlibConst.Z_STREAM_END) { stream.inflateEnd(); return err == zlibConst.Z_OK ? zlibConst.Z_BUF_ERROR : err; } if (stream.total_out != dest.Length) { byte[] output = new byte[stream.total_out]; Buffer.BlockCopy(stream.next_out, 0, output, 0, (int)stream.total_out); dest = output; } return stream.inflateEnd(); }
public override void Write(System.Byte[] b1, int off, int len) { if (len == 0) { return; } int err; byte[] b = new byte[b1.Length]; System.Array.Copy(b1, b, b1.Length); z.next_in = b; z.next_in_index = off; z.avail_in = len; do { z.next_out = buf; z.next_out_index = 0; z.avail_out = bufsize; if (compress) { err = z.deflate(flush_Renamed_Field); } else { err = z.inflate(flush_Renamed_Field); } if (err != zlibConst.Z_OK) { throw new ZStreamException((compress?"de":"in") + "flating: " + z.msg); } out_Renamed.Write(buf, 0, bufsize - z.avail_out); }while (z.avail_in > 0 || z.avail_out == 0); }
public int read(byte[] b, int off, int len) { if (len == 0) { return(0); } int err; z.next_out = b; z.next_out_index = off; z.avail_out = len; do { if ((z.avail_in == 0) && (!nomoreinput)) { // if buffer is empty and more input is avaiable, refill it z.next_in_index = 0; z.avail_in = SupportClass.ReadInput(in_Renamed, buf, 0, bufsize); //(bufsize<z.avail_out ? bufsize : z.avail_out)); if (z.avail_in == -1) { z.avail_in = 0; nomoreinput = true; } } if (compress) { err = z.deflate(flush); } else { err = z.inflate(flush); } if (nomoreinput && (err == zlibConst.Z_BUF_ERROR)) { return(-1); } if (err != zlibConst.Z_OK && err != zlibConst.Z_STREAM_END) { throw new ZStreamException((compress?"de":"in") + "flating: " + z.msg); } if (nomoreinput && (z.avail_out == len)) { return(-1); } }while (z.avail_out == len && err == zlibConst.Z_OK); //System.err.print("("+(len-z.avail_out)+")"); return(len - z.avail_out); }
protected override int PerformZlibOperation(ZStream zs, int flush) { return zs.inflate(flush); }
private byte[] inflate_file(BinaryReader reader, long length) { byte[] uncompressed = new byte[length*16]; ZStream strm = new ZStream(); strm.next_in = reader.ReadBytes((int)length); strm.avail_in = (int)length; strm.avail_out = 0; var code = strm.inflateInit(-15); var output = new System.Collections.Generic.List<byte>(); while (code == zlibConst.Z_OK && strm.avail_out == 0) { strm.next_out = uncompressed; strm.avail_out = uncompressed.Length; code = strm.inflate(zlibConst.Z_SYNC_FLUSH); switch (code) { case zlibConst.Z_OK: for (int i = 0; i < uncompressed.Length; i++) output.Add(uncompressed[i]); break; case zlibConst.Z_STREAM_END: for (int i = 0; i < uncompressed.Length - strm.avail_out; i++) output.Add(uncompressed[i]); break; } } return output.ToArray(); }
public static PhpBytes GzInflate(PhpBytes data, long length) { uint factor=1, maxfactor=16; long ilength; ZStream zs = new ZStream(); zs.avail_in = data.Length; zs.next_in = data.ReadonlyData; zs.total_out = 0; // -15 omits the header (undocumented feature of zlib) int status = zs.inflateInit(-15); if (status != zlibConst.Z_OK) { PhpException.Throw(PhpError.Warning, zError(status)); return null; } do { ilength = length != 0 ? length : data.Length * (1 << (int)(factor++)); try { byte[] newOutput = new byte[ilength]; if (zs.next_out != null) { Buffer.BlockCopy(zs.next_out, 0, newOutput, 0, zs.next_out.Length); } zs.next_out = newOutput; } catch (OutOfMemoryException) { zs.inflateEnd(); return null; } zs.next_out_index = (int)zs.total_out; zs.avail_out = unchecked((int)(ilength - zs.total_out)); status = zs.inflate(zlibConst.Z_NO_FLUSH); } while ((status == zlibConst.Z_BUF_ERROR || (status == zlibConst.Z_OK && (zs.avail_in != 0 || zs.avail_out == 0))) && length == 0 && factor < maxfactor); zs.inflateEnd(); if ((length != 0 && status == zlibConst.Z_OK) || factor >= maxfactor) { status = zlibConst.Z_MEM_ERROR; } if (status == zlibConst.Z_STREAM_END || status == zlibConst.Z_OK) { byte[] result = new byte[zs.total_out]; Buffer.BlockCopy(zs.next_out, 0, result, 0, (int)zs.total_out); return new PhpBytes(result); } else { PhpException.Throw(PhpError.Warning, zError(status)); return null; } }
private void Decompress(byte[] deflated, int deflatedLen, byte[] inflated, int inflatedLen) { ZStream stream = new ZStream(); stream.next_in = deflated; stream.avail_in = deflatedLen; stream.next_out = inflated; stream.avail_out = inflatedLen; stream.inflateInit(); stream.inflate(zlibConst.Z_NO_FLUSH); stream.inflateEnd(); }