public Bytes decompress([NotNone] IBufferProtocol data, int max_length = 0) { if (max_length < 0) { throw new ArgumentException("max_length must be greater than zero"); } using var buffer = data.GetBuffer(); byte[] input = buffer.AsUnsafeArray() ?? buffer.ToArray(); byte[] output = new byte[max_length > 0 && ZlibModule.DEFAULTALLOC > max_length ? max_length : ZlibModule.DEFAULTALLOC]; long start_total_out = zst.total_out; zst.next_in = input; zst.next_in_index = 0; zst.avail_in = input.Length; zst.next_out = output; zst.next_out_index = 0; zst.avail_out = output.Length; int err = zst.inflate(FlushStrategy.Z_SYNC_FLUSH); while (err == Z_OK && zst.avail_out == 0) { if (max_length > 0 && output.Length >= max_length) { break; } int old_length = output.Length; Array.Resize(ref output, output.Length * 2); zst.next_out = output; zst.avail_out = old_length; err = zst.inflate(FlushStrategy.Z_SYNC_FLUSH); } if (max_length > 0) { unconsumed_tail = GetBytes(zst.next_in, zst.next_in_index, zst.avail_in); } if (err == Z_STREAM_END) { unused_data += GetBytes(zst.next_in, zst.next_in_index, zst.avail_in); eof = true; } else if (err != Z_OK && err != Z_BUF_ERROR) { throw ZlibModule.zlib_error(this.zst, err, "while decompressing"); } return(GetBytes(output, 0, (int)(zst.total_out - start_total_out))); }
public string decompress([BytesConversion] IList <byte> value, int max_length = 0) { if (max_length < 0) { throw new ArgumentException("max_length must be greater than zero"); } byte[] input = value.ToArray(); byte[] output = new byte[max_length > 0 && ZlibModule.DEFAULTALLOC > max_length ? max_length : ZlibModule.DEFAULTALLOC]; long start_total_out = zst.total_out; zst.next_in = input; zst.next_in_index = 0; zst.avail_in = input.Length; zst.next_out = output; zst.next_out_index = 0; zst.avail_out = output.Length; int err = zst.inflate(FlushStrategy.Z_SYNC_FLUSH); while (err == Z_OK && zst.avail_out == 0) { if (max_length > 0 && output.Length >= max_length) { break; } int old_length = output.Length; Array.Resize(ref output, output.Length * 2); zst.next_out = output; zst.avail_out = old_length; err = zst.inflate(FlushStrategy.Z_SYNC_FLUSH); } if (max_length > 0) { _unconsumed_tail = PythonAsciiEncoding.Instance.GetString(zst.next_in, zst.next_in_index, zst.avail_in); } if (err == Z_STREAM_END) { _unused_data += PythonAsciiEncoding.Instance.GetString(zst.next_in, zst.next_in_index, zst.avail_in); } else if (err != Z_OK && err != Z_BUF_ERROR) { throw ZlibModule.zlib_error(this.zst, err, "while decompressing"); } return(PythonAsciiEncoding.Instance.GetString(output, 0, (int)(zst.total_out - start_total_out))); }
internal Compress(int level, int method, int wbits, int memlevel, int strategy) { zst = new ZStream(); int err = zst.deflateInit(level, wbits); switch (err) { case ZlibModule.Z_OK: break; case ZlibModule.Z_STREAM_ERROR: throw PythonOps.ValueError("Invalid initialization option"); default: throw ZlibModule.zlib_error(this.zst, err, "while creating compression object"); } }
public string flush([DefaultParameterValue(Z_FINISH)] int mode) { byte[] output = new byte[ZlibModule.DEFAULTALLOC]; if (mode == Z_NO_FLUSH) { return(string.Empty); } long start_total_out = zst.total_out; zst.avail_in = 0; zst.next_out = output; zst.next_out_index = 0; zst.avail_out = output.Length; int err = zst.deflate((FlushStrategy)mode); while (err == Z_OK && zst.avail_out == 0) { int old_length = output.Length; Array.Resize(ref output, output.Length * 2); zst.next_out = output; zst.avail_out = old_length; err = zst.deflate((FlushStrategy)mode); } if (err == Z_STREAM_END && mode == Z_FINISH) { err = zst.deflateEnd(); if (err != Z_OK) { throw ZlibModule.zlib_error(this.zst, err, "from deflateEnd()"); } } else if (err != Z_OK && err != Z_BUF_ERROR) { throw ZlibModule.zlib_error(this.zst, err, "while flushing"); } return(PythonAsciiEncoding.Instance.GetString(output, 0, (int)(zst.total_out - start_total_out))); }
internal Decompress(int wbits) { zst = new ZStream(); int err = zst.inflateInit(wbits); switch (err) { case ZlibModule.Z_OK: break; case ZlibModule.Z_STREAM_ERROR: throw PythonOps.ValueError("Invalid initialization option"); default: throw ZlibModule.zlib_error(this.zst, err, "while creating decompression object"); } _unused_data = string.Empty; _unconsumed_tail = string.Empty; }
public string flush(int length = ZlibModule.DEFAULTALLOC) { if (length < 1) { throw PythonOps.ValueError("length must be greater than 0."); } byte[] output = new byte[length]; long start_total_out = zst.total_out; zst.next_out = output; zst.next_out_index = 0; zst.avail_out = output.Length; int err = zst.inflate(FlushStrategy.Z_FINISH); while ((err == Z_OK || err == Z_BUF_ERROR) && zst.avail_out == 0) { int old_length = output.Length; Array.Resize(ref output, output.Length * 2); zst.next_out = output; zst.avail_out = old_length; err = zst.inflate(FlushStrategy.Z_FINISH); } if (err == Z_STREAM_END) { err = zst.inflateEnd(); if (err != Z_OK) { throw ZlibModule.zlib_error(this.zst, err, "from inflateEnd()"); } } return(PythonAsciiEncoding.Instance.GetString(output, 0, (int)(zst.total_out - start_total_out))); }