本文整理汇总了C#中zlib.ZStream.inflateInit方法的典型用法代码示例。如果您正苦于以下问题:C# ZStream.inflateInit方法的具体用法?C# ZStream.inflateInit怎么用?C# ZStream.inflateInit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zlib.ZStream
的用法示例。
在下文中一共展示了ZStream.inflateInit方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ZlibStream
public ZlibStream(Stream innerStream)
{
_innerStream = innerStream;
if(_innerStream.CanRead)
{
_in = new ZStream();
int ret = _in.inflateInit();
if (ret != zlibConst.Z_OK)
throw new ZStreamException("Unable to initialize inflate");
_inBuff = new byte[_buffSize];
_in.avail_in = 0;
_in.next_in = _inBuff;
_in.next_in_index = 0;
}
if(_innerStream.CanWrite)
{
_out = new ZStream();
int ret = _out.deflateInit(zlibConst.Z_DEFAULT_COMPRESSION);
if (ret != zlibConst.Z_OK)
throw new ZStreamException("Unable to initialize deflate");
_outBuff = new byte[_buffSize];
_out.next_out = _outBuff;
}
}
示例2: BufferStream
IEnumerable<WebSocketsFrame> IExtension.ApplyIncoming(NetContext context, WebSocketConnection connection, WebSocketsFrame frame)
{
if (frame.Reserved1 && !frame.IsControlFrame)
{
BufferStream tmp = null;
var payload = frame.Payload;
payload.Position = 0;
byte[] inBuffer = null, outBuffer = null;
try
{
outBuffer = context.GetBuffer();
inBuffer = context.GetBuffer();
tmp = new BufferStream(context, 0);
if (inbound == null)
{
inbound = new ZStream();
inbound.inflateInit();
// fake a zlib header with:
// CMF:
// CM = 8 (deflate)
// CINFO = 7 (32k window)
// FLG:
// FCHECK: 26 (checksum of other bits)
// FDICT: 0 (no dictionary)
// FLEVEL: 3 (maximum)
inBuffer[0] = 120;
inBuffer[1] = 218;
inbound.next_in = inBuffer;
int chk = Inflate(tmp, outBuffer, 2);
if (chk != 0) throw new InvalidOperationException("Spoofed zlib header suggested data");
}
inbound.next_in = inBuffer;
int remaining = frame.PayloadLength;
//bool first = true;
while (remaining > 0)
{
int readCount = payload.Read(inBuffer, 0, inBuffer.Length);
if (readCount <= 0) break;
remaining -= readCount;
//if (first)
//{ // kill the BFINAL flag from the first block, if set; we don't want zlib
// // trying to verify the ADLER checksum; unfortunately, a frame can contain
// // multiple blocks, and a *later* block could have BFINAL set. That sucks.
// inBuffer[0] &= 254;
// first = false;
//}
Inflate(tmp, outBuffer, readCount);
}
if (remaining != 0) throw new EndOfStreamException();
// spoof the missing 4 bytes from the tail
inBuffer[0] = inBuffer[1] = 0x00;
inBuffer[2] = inBuffer[3] = 0xFF;
Inflate(tmp, outBuffer, 4);
// set our final output
tmp.Position = 0;
frame.Payload = tmp;
long bytesSaved = tmp.Length - frame.PayloadLength;
frame.PayloadLength = (int)tmp.Length;
frame.Reserved1 = false;
tmp = payload as BufferStream;
parent.RegisterInboundBytesSaved(bytesSaved);
}
#if DEBUG
catch (Exception ex)
{
Debug.WriteLine(ex);
throw;
}
#endif
finally
{
if (inbound != null)
{
inbound.next_out = null;
inbound.next_in = null;
}
if (tmp != null) tmp.Dispose();
if (inBuffer != null) context.Recycle(inBuffer);
if (outBuffer != null) context.Recycle(outBuffer);
if (parent.disableContextTakeover) ClearContext(true, false);
}
}
yield return frame;
}
示例3: InitZlibOperation
protected override int InitZlibOperation(ZStream zs)
{
// -MAX_WBITS stands for absense of Zlib header and trailer (needed for GZIP compression and decompression)
return zs.inflateInit(-Zlib.MAX_WBITS);
}
示例4: init
private void init(Stream innerStream)
{
m_stream = innerStream;
if (m_stream.CanRead)
{
m_in = new ZStream();
int ret = m_in.inflateInit();
if (ret != zlibConst.Z_OK)
throw new CompressionFailedException("Unable to initialize zlib for deflate: " + ret);
m_inbuf = new byte[bufsize];
m_in.avail_in = 0;
m_in.next_in = m_inbuf;
m_in.next_in_index = 0;
}
if (m_stream.CanWrite)
{
m_out = new ZStream();
int ret = m_out.deflateInit(zlibConst.Z_DEFAULT_COMPRESSION);
if (ret != zlibConst.Z_OK)
throw new CompressionFailedException("Unable to initialize zlib for inflate: " + ret);
m_outbuf = new byte[bufsize];
m_out.next_out = m_outbuf;
}
}
示例5: inflate_file
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();
}
示例6: GzInflate
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;
}
}
示例7: ZlibUncompress
/// <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();
}
示例8: Decompress
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();
}