本文整理汇总了C#中zlib.ZStream类的典型用法代码示例。如果您正苦于以下问题:C# ZStream类的具体用法?C# ZStream怎么用?C# ZStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ZStream类属于zlib命名空间,在下文中一共展示了ZStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: inflateEnd
internal int inflateEnd(ZStream z)
{
if (blocks != null)
blocks.free(z);
blocks = null;
// ZFREE(z, z->state);
return Z_OK;
}
示例3: InfBlocks
internal long check; // check on output
internal InfBlocks(ZStream z, System.Object checkfn, int w)
{
hufts = new int[MANY * 3];
window = new byte[w];
end = w;
this.checkfn = checkfn;
mode = TYPE;
reset(z, null);
}
示例4: InfCodes
internal InfCodes(int bl, int bd, int[] tl, int[] td, ZStream z)
{
mode = START;
lbits = (byte) bl;
dbits = (byte) bd;
ltree = tl;
ltree_index = 0;
dtree = td;
dtree_index = 0;
}
示例5: InfCodes
internal InfCodes(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, ZStream z)
{
this.mode = 0;
this.lbits = (byte) bl;
this.dbits = (byte) bd;
this.ltree = tl;
this.ltree_index = tl_index;
this.dtree = td;
this.dtree_index = td_index;
}
示例6: inflateReset
internal InfBlocks blocks; // current inflate_blocks state
internal int inflateReset(ZStream z)
{
if (z == null || z.istate == null)
return Z_STREAM_ERROR;
z.total_in = z.total_out = 0;
z.msg = null;
z.istate.mode = z.istate.nowrap != 0?BLOCKS:METHOD;
z.istate.blocks.reset(z, null);
return Z_OK;
}
示例7: InfBlocks
internal InfBlocks(ZStream z, object checkfn, int w)
{
this.bb = new int[1];
this.tb = new int[1];
this.hufts = new int[0x10e0];
this.window = new byte[w];
this.end = w;
this.checkfn = checkfn;
this.mode = 0;
this.reset(z, null);
}
示例8: inflate_flush
internal int inflate_flush(ZStream z, int r)
{
int num2 = z.next_out_index;
int num3 = this.read;
int num1 = ((num3 <= this.write) ? this.write : this.end) - num3;
if (num1 > z.avail_out)
{
num1 = z.avail_out;
}
if ((num1 != 0) && (r == -5))
{
r = 0;
}
z.avail_out -= num1;
z.total_out += num1;
if (this.checkfn != null)
{
z.adler = this.check = z._adler.adler32(this.check, this.window, num3, num1);
}
Array.Copy(this.window, num3, z.next_out, num2, num1);
num2 += num1;
num3 += num1;
if (num3 == this.end)
{
num3 = 0;
if (this.write == this.end)
{
this.write = 0;
}
num1 = this.write - num3;
if (num1 > z.avail_out)
{
num1 = z.avail_out;
}
if ((num1 != 0) && (r == -5))
{
r = 0;
}
z.avail_out -= num1;
z.total_out += num1;
if (this.checkfn != null)
{
z.adler = this.check = z._adler.adler32(this.check, this.window, num3, num1);
}
Array.Copy(this.window, num3, z.next_out, num2, num1);
num2 += num1;
num3 += num1;
}
z.next_out_index = num2;
this.read = num3;
return r;
}
示例9: reset
internal void reset(ZStream z, long[] c)
{
if (c != null)
c[0] = check;
if (mode == BTREE || mode == DTREE)
{
blens = null;
}
if (mode == CODES)
{
codes.free(z);
}
mode = TYPE;
bitk = 0;
bitb = 0;
read = write = 0;
if (checkfn != null)
z.adler = check = z._adler.adler32(0L, null, 0, 0);
}
示例10: inflate_fast
// Called with number of bytes left to write in window at least 258
// (the maximum string length) and number of input bytes available
// at least ten. The ten bytes are six bytes for the longest length/
// distance pair plus four bytes for overloading the bit buffer.
internal int inflate_fast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InfBlocks s, ZStream z)
{
int t; // temporary pointer
int[] tp; // temporary pointer
int tp_index; // temporary pointer
int e; // extra bits or operation
int b; // bit buffer
int k; // bits in bit buffer
int p; // input data pointer
int n; // bytes available there
int q; // output window write pointer
int m; // bytes to end of window or read pointer
int ml; // mask for literal/length tree
int md; // mask for distance tree
int c; // bytes to copy
int d; // distance back to copy from
int r; // copy source pointer
// load input, output, bit values
p = z.next_in_index; n = z.avail_in; b = s.bitb; k = s.bitk;
q = s.write; m = q < s.read ? s.read - q - 1 : s.end - q;
// initialize masks
ml = inflate_mask[bl];
md = inflate_mask[bd];
// do until not enough input or output space for fast loop
do
{
// assume called with m >= 258 && n >= 10
// get literal/length code
while (k < (20))
{
// max bits for literal/length code
n--;
b |= (z.next_in[p++] & 0xff) << k; k += 8;
}
t = b & ml;
tp = tl;
tp_index = tl_index;
if ((e = tp[(tp_index + t) * 3]) == 0)
{
b >>= (tp[(tp_index + t) * 3 + 1]); k -= (tp[(tp_index + t) * 3 + 1]);
s.window[q++] = (byte)tp[(tp_index + t) * 3 + 2];
m--;
continue;
}
do
{
b >>= (tp[(tp_index + t) * 3 + 1]); k -= (tp[(tp_index + t) * 3 + 1]);
if ((e & 16) != 0)
{
e &= 15;
c = tp[(tp_index + t) * 3 + 2] + ((int)b & inflate_mask[e]);
b >>= e; k -= e;
// decode distance base of block to copy
while (k < (15))
{
// max bits for distance code
n--;
b |= (z.next_in[p++] & 0xff) << k; k += 8;
}
t = b & md;
tp = td;
tp_index = td_index;
e = tp[(tp_index + t) * 3];
do
{
b >>= (tp[(tp_index + t) * 3 + 1]); k -= (tp[(tp_index + t) * 3 + 1]);
if ((e & 16) != 0)
{
// get extra bits to add to distance base
e &= 15;
while (k < (e))
{
// get extra bits (up to 13)
n--;
b |= (z.next_in[p++] & 0xff) << k; k += 8;
}
d = tp[(tp_index + t) * 3 + 2] + (b & inflate_mask[e]);
b >>= (e); k -= (e);
// do the copy
m -= c;
if (q >= d)
{
//.........这里部分代码省略.........
示例11: 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;
}
示例12: InvalidOperationException
IEnumerable<WebSocketsFrame> IExtension.ApplyOutgoing(NetContext context, WebSocketConnection connection, WebSocketsFrame frame)
{
if (!frame.IsControlFrame && frame.PayloadLength > parent.compressMessagesLargerThanBytes)
{
if (frame.Reserved1)
{
throw new InvalidOperationException("Reserved1 flag is already set; extension conflict?");
}
int headerBytes = 0;
if (outbound == null)
{
outbound = new ZStream();
const int BITS = 12; // 4096 byte outbound buffer (instead of full 32k=15)
outbound.deflateInit(zlibConst.Z_BEST_COMPRESSION, BITS);
headerBytes = 2;
}
BufferStream tmp = null;
var payload = frame.Payload;
payload.Position = 0;
byte[] inBuffer = null, outBuffer = null;
try
{
inBuffer = context.GetBuffer();
outBuffer = context.GetBuffer();
tmp = new BufferStream(context, 0);
outbound.next_out = outBuffer;
outbound.next_in = inBuffer;
int remaining = frame.PayloadLength;
while (remaining > 0)
{
int readCount = payload.Read(inBuffer, 0, inBuffer.Length);
if (readCount <= 0) break;
remaining -= readCount;
outbound.next_in_index = 0;
outbound.avail_in = readCount;
do
{
outbound.next_out_index = 0;
outbound.avail_out = outBuffer.Length;
long priorOut = outbound.total_out;
int err = outbound.deflate(remaining == 0 ? zlibConst.Z_SYNC_FLUSH : zlibConst.Z_NO_FLUSH);
if (err != zlibConst.Z_OK && err != zlibConst.Z_STREAM_END)
throw new ZStreamException("deflating: " + outbound.msg);
int outCount = (int)(outbound.total_out - priorOut);
if (outCount > 0)
{
if (headerBytes == 0)
{
tmp.Write(outBuffer, 0, outCount);
}
else
{
if (outCount < headerBytes)
{
throw new InvalidOperationException("Failed to write entire header");
}
// check the generated header meets our expectations
// CMF is very specific - CM must be 8, and CINFO must be <=7 (for 32k window)
if ((outBuffer[0] & 15) != 8)
{
throw new InvalidOperationException("Zlib CM header was incorrect");
}
if ((outBuffer[0] & 128) != 0) // if msb set, is > 7 - invalid
{
throw new InvalidOperationException("Zlib CINFO header was incorrect");
}
// FLG is less important; FCHECK is irrelevent, FLEVEL doesn't matter; but
// FDICT must be zero, to ensure that we aren't expecting a an initialization dictionary
if ((outBuffer[1] & 32) != 0)
{
throw new InvalidOperationException("Zlib FLG.FDICT header was set (must not be)");
}
// skip the header, and write anything else
outCount -= headerBytes;
if (outCount > 0)
{
tmp.Write(outBuffer, headerBytes, outCount);
}
headerBytes = 0; // all written now
}
}
} while (outbound.avail_in > 0 || outbound.avail_out == 0);
}
if (remaining != 0) throw new EndOfStreamException();
if (headerBytes != 0) throw new InvalidOperationException("Zlib header was not written");
// verify the last 4 bytes, then drop them
tmp.Position = tmp.Length - 4;
NetContext.Fill(tmp, outBuffer, 4);
if (!(outBuffer[0] == 0x00 && outBuffer[1] == 0x00 && outBuffer[2] == 0xFF && outBuffer[3] == 0xFF))
{
throw new InvalidOperationException("expectation failed: 0000FFFF in the tail");
//.........这里部分代码省略.........
示例13: inflate_trees_dynamic
internal static int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZStream z)
{
int r;
int[] hn = new int[1]; // hufts used in space
int[] v = new int[288]; // work area for huft_build
// build literal/length tree
r = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
if (r != Z_OK || bl[0] == 0)
{
if (r == Z_DATA_ERROR)
{
z.msg = "oversubscribed literal/length tree";
}
else if (r != Z_MEM_ERROR)
{
z.msg = "incomplete literal/length tree";
r = Z_DATA_ERROR;
}
return r;
}
// build distance tree
r = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
if (r != Z_OK || (bd[0] == 0 && nl > 257))
{
if (r == Z_DATA_ERROR)
{
z.msg = "oversubscribed distance tree";
}
else if (r == Z_BUF_ERROR)
{
z.msg = "incomplete distance tree";
r = Z_DATA_ERROR;
}
else if (r != Z_MEM_ERROR)
{
z.msg = "empty distance tree with lengths";
r = Z_DATA_ERROR;
}
return r;
}
return Z_OK;
}
示例14: deflate
internal int deflate(ZStream strm, int flush)
{
int old_flush;
if (flush > Z_FINISH || flush < 0)
{
return Z_STREAM_ERROR;
}
if (strm.next_out == null || (strm.next_in == null && strm.avail_in != 0) || (status == FINISH_STATE && flush != Z_FINISH))
{
strm.msg = z_errmsg[Z_NEED_DICT - (Z_STREAM_ERROR)];
return Z_STREAM_ERROR;
}
if (strm.avail_out == 0)
{
strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
return Z_BUF_ERROR;
}
this.strm = strm; // just in case
old_flush = last_flush;
last_flush = flush;
// Write the zlib header
if (status == INIT_STATE)
{
int header = (Z_DEFLATED + ((w_bits - 8) << 4)) << 8;
int level_flags = ((level - 1) & 0xff) >> 1;
if (level_flags > 3)
level_flags = 3;
header |= (level_flags << 6);
if (strstart != 0)
header |= PRESET_DICT;
header += 31 - (header % 31);
status = BUSY_STATE;
putShortMSB(header);
// Save the adler32 of the preset dictionary:
if (strstart != 0)
{
putShortMSB((int) (SupportClass.URShift(strm.adler, 16)));
putShortMSB((int) (strm.adler & 0xffff));
}
strm.adler = strm._adler.adler32(0, null, 0, 0);
}
// Flush as much pending output as possible
if (pending != 0)
{
strm.flush_pending();
if (strm.avail_out == 0)
{
//System.out.println(" avail_out==0");
// Since avail_out is 0, deflate will be called again with
// more output space, but possibly with both pending and
// avail_in equal to zero. There won't be anything to do,
// but this is not an error situation so make sure we
// return OK instead of BUF_ERROR at next call of deflate:
last_flush = - 1;
return Z_OK;
}
// Make sure there is something to do and avoid duplicate consecutive
// flushes. For repeated and useless calls with Z_FINISH, we keep
// returning Z_STREAM_END instead of Z_BUFF_ERROR.
}
else if (strm.avail_in == 0 && flush <= old_flush && flush != Z_FINISH)
{
strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
return Z_BUF_ERROR;
}
// User must not provide more input after the first FINISH:
if (status == FINISH_STATE && strm.avail_in != 0)
{
strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
return Z_BUF_ERROR;
}
// Start a new block or continue the current one.
if (strm.avail_in != 0 || lookahead != 0 || (flush != Z_NO_FLUSH && status != FINISH_STATE))
{
int bstate = - 1;
switch (config_table[level].func)
{
case STORED:
bstate = deflate_stored(flush);
break;
case FAST:
bstate = deflate_fast(flush);
break;
case SLOW:
bstate = deflate_slow(flush);
//.........这里部分代码省略.........
示例15: deflateParams
internal int deflateParams(ZStream strm, int _level, int _strategy)
{
int err = Z_OK;
if (_level == Z_DEFAULT_COMPRESSION)
{
_level = 6;
}
if (_level < 0 || _level > 9 || _strategy < 0 || _strategy > Z_HUFFMAN_ONLY)
{
return Z_STREAM_ERROR;
}
if (config_table[level].func != config_table[_level].func && strm.total_in != 0)
{
// Flush the last buffer:
err = strm.deflate(Z_PARTIAL_FLUSH);
}
if (level != _level)
{
level = _level;
max_lazy_match = config_table[level].max_lazy;
good_match = config_table[level].good_length;
nice_match = config_table[level].nice_length;
max_chain_length = config_table[level].max_chain;
}
strategy = _strategy;
return err;
}