init commit

This commit is contained in:
2025-10-09 09:57:24 +09:00
commit 4d551bd74f
6636 changed files with 1218703 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
namespace Org.BouncyCastle.Utilities.Zlib;
internal sealed class Adler32
{
private const int BASE = 65521;
private const int NMAX = 5552;
internal long adler32(long adler, byte[] buf, int index, int len)
{
if (buf == null)
{
return 1L;
}
long num = adler & 0xFFFF;
long num2 = (adler >> 16) & 0xFFFF;
while (len > 0)
{
int num3 = ((len < 5552) ? len : 5552);
len -= num3;
while (num3 >= 16)
{
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num += buf[index++] & 0xFF;
num2 += num;
num3 -= 16;
}
if (num3 != 0)
{
do
{
num += buf[index++] & 0xFF;
num2 += num;
}
while (--num3 != 0);
}
num %= 65521;
num2 %= 65521;
}
return (num2 << 16) | num;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,672 @@
using System;
namespace Org.BouncyCastle.Utilities.Zlib;
internal sealed class InfBlocks
{
private const int MANY = 1440;
private const int Z_OK = 0;
private const int Z_STREAM_END = 1;
private const int Z_NEED_DICT = 2;
private const int Z_ERRNO = -1;
private const int Z_STREAM_ERROR = -2;
private const int Z_DATA_ERROR = -3;
private const int Z_MEM_ERROR = -4;
private const int Z_BUF_ERROR = -5;
private const int Z_VERSION_ERROR = -6;
private const int TYPE = 0;
private const int LENS = 1;
private const int STORED = 2;
private const int TABLE = 3;
private const int BTREE = 4;
private const int DTREE = 5;
private const int CODES = 6;
private const int DRY = 7;
private const int DONE = 8;
private const int BAD = 9;
private static readonly int[] inflate_mask = new int[17]
{
0, 1, 3, 7, 15, 31, 63, 127, 255, 511,
1023, 2047, 4095, 8191, 16383, 32767, 65535
};
private static readonly int[] border = new int[19]
{
16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
11, 4, 12, 3, 13, 2, 14, 1, 15
};
internal int mode;
internal int left;
internal int table;
internal int index;
internal int[] blens;
internal int[] bb = new int[1];
internal int[] tb = new int[1];
internal InfCodes codes = new InfCodes();
private int last;
internal int bitk;
internal int bitb;
internal int[] hufts;
internal byte[] window;
internal int end;
internal int read;
internal int write;
internal object checkfn;
internal long check;
internal InfTree inftree = new InfTree();
internal InfBlocks(ZStream z, object checkfn, int w)
{
hufts = new int[4320];
window = new byte[w];
end = w;
this.checkfn = checkfn;
mode = 0;
reset(z, null);
}
internal void reset(ZStream z, long[] c)
{
if (c != null)
{
c[0] = check;
}
if (mode != 4)
{
_ = mode;
_ = 5;
}
if (mode == 6)
{
codes.free(z);
}
mode = 0;
bitk = 0;
bitb = 0;
read = (write = 0);
if (checkfn != null)
{
z.adler = (check = z._adler.adler32(0L, null, 0, 0));
}
}
internal int proc(ZStream z, int r)
{
int num = z.next_in_index;
int num2 = z.avail_in;
int num3 = bitb;
int i = bitk;
int num4 = write;
int num5 = ((num4 < read) ? (read - num4 - 1) : (end - num4));
while (true)
{
switch (mode)
{
case 0:
{
for (; i < 3; i += 8)
{
if (num2 != 0)
{
r = 0;
num2--;
num3 |= (z.next_in[num++] & 0xFF) << i;
continue;
}
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
int num6 = num3 & 7;
last = num6 & 1;
switch (num6 >> 1)
{
case 0:
num3 >>= 3;
i -= 3;
num6 = i & 7;
num3 >>= num6;
i -= num6;
mode = 1;
break;
case 1:
{
int[] array = new int[1];
int[] array2 = new int[1];
int[][] array3 = new int[1][];
int[][] array4 = new int[1][];
InfTree.inflate_trees_fixed(array, array2, array3, array4, z);
codes.init(array[0], array2[0], array3[0], 0, array4[0], 0, z);
num3 >>= 3;
i -= 3;
mode = 6;
break;
}
case 2:
num3 >>= 3;
i -= 3;
mode = 3;
break;
case 3:
num3 >>= 3;
i -= 3;
mode = 9;
z.msg = "invalid block type";
r = -3;
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
break;
}
case 1:
for (; i < 32; i += 8)
{
if (num2 != 0)
{
r = 0;
num2--;
num3 |= (z.next_in[num++] & 0xFF) << i;
continue;
}
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
if (((~num3 >> 16) & 0xFFFF) != (num3 & 0xFFFF))
{
mode = 9;
z.msg = "invalid stored block lengths";
r = -3;
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
left = num3 & 0xFFFF;
num3 = (i = 0);
mode = ((left != 0) ? 2 : ((last != 0) ? 7 : 0));
break;
case 2:
{
if (num2 == 0)
{
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
if (num5 == 0)
{
if (num4 == end && read != 0)
{
num4 = 0;
num5 = ((num4 < read) ? (read - num4 - 1) : (end - num4));
}
if (num5 == 0)
{
write = num4;
r = inflate_flush(z, r);
num4 = write;
num5 = ((num4 < read) ? (read - num4 - 1) : (end - num4));
if (num4 == end && read != 0)
{
num4 = 0;
num5 = ((num4 < read) ? (read - num4 - 1) : (end - num4));
}
if (num5 == 0)
{
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
}
}
r = 0;
int num6 = left;
if (num6 > num2)
{
num6 = num2;
}
if (num6 > num5)
{
num6 = num5;
}
Array.Copy(z.next_in, num, window, num4, num6);
num += num6;
num2 -= num6;
num4 += num6;
num5 -= num6;
if ((left -= num6) == 0)
{
mode = ((last != 0) ? 7 : 0);
}
break;
}
case 3:
{
for (; i < 14; i += 8)
{
if (num2 != 0)
{
r = 0;
num2--;
num3 |= (z.next_in[num++] & 0xFF) << i;
continue;
}
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
int num6 = (table = num3 & 0x3FFF);
if ((num6 & 0x1F) > 29 || ((num6 >> 5) & 0x1F) > 29)
{
mode = 9;
z.msg = "too many length or distance symbols";
r = -3;
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
num6 = 258 + (num6 & 0x1F) + ((num6 >> 5) & 0x1F);
if (blens == null || blens.Length < num6)
{
blens = new int[num6];
}
else
{
for (int j = 0; j < num6; j++)
{
blens[j] = 0;
}
}
num3 >>= 14;
i -= 14;
index = 0;
mode = 4;
goto case 4;
}
case 4:
{
while (index < 4 + (table >> 10))
{
for (; i < 3; i += 8)
{
if (num2 != 0)
{
r = 0;
num2--;
num3 |= (z.next_in[num++] & 0xFF) << i;
continue;
}
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
blens[border[index++]] = num3 & 7;
num3 >>= 3;
i -= 3;
}
while (index < 19)
{
blens[border[index++]] = 0;
}
bb[0] = 7;
int num6 = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
if (num6 != 0)
{
r = num6;
if (r == -3)
{
blens = null;
mode = 9;
}
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
index = 0;
mode = 5;
goto case 5;
}
case 5:
{
int num6;
while (true)
{
num6 = table;
if (index >= 258 + (num6 & 0x1F) + ((num6 >> 5) & 0x1F))
{
break;
}
for (num6 = bb[0]; i < num6; i += 8)
{
if (num2 != 0)
{
r = 0;
num2--;
num3 |= (z.next_in[num++] & 0xFF) << i;
continue;
}
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
_ = tb[0];
_ = -1;
num6 = hufts[(tb[0] + (num3 & inflate_mask[num6])) * 3 + 1];
int num7 = hufts[(tb[0] + (num3 & inflate_mask[num6])) * 3 + 2];
if (num7 < 16)
{
num3 >>= num6;
i -= num6;
blens[index++] = num7;
continue;
}
int num8 = ((num7 == 18) ? 7 : (num7 - 14));
int num9 = ((num7 == 18) ? 11 : 3);
for (; i < num6 + num8; i += 8)
{
if (num2 != 0)
{
r = 0;
num2--;
num3 |= (z.next_in[num++] & 0xFF) << i;
continue;
}
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
num3 >>= num6;
i -= num6;
num9 += num3 & inflate_mask[num8];
num3 >>= num8;
i -= num8;
num8 = index;
num6 = table;
if (num8 + num9 > 258 + (num6 & 0x1F) + ((num6 >> 5) & 0x1F) || (num7 == 16 && num8 < 1))
{
blens = null;
mode = 9;
z.msg = "invalid bit length repeat";
r = -3;
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
num7 = ((num7 == 16) ? blens[num8 - 1] : 0);
do
{
blens[num8++] = num7;
}
while (--num9 != 0);
index = num8;
}
tb[0] = -1;
int[] array5 = new int[1];
int[] array6 = new int[1];
int[] array7 = new int[1];
int[] array8 = new int[1];
array5[0] = 9;
array6[0] = 6;
num6 = table;
num6 = inftree.inflate_trees_dynamic(257 + (num6 & 0x1F), 1 + ((num6 >> 5) & 0x1F), blens, array5, array6, array7, array8, hufts, z);
if (num6 != 0)
{
if (num6 == -3)
{
blens = null;
mode = 9;
}
r = num6;
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
codes.init(array5[0], array6[0], hufts, array7[0], hufts, array8[0], z);
mode = 6;
goto case 6;
}
case 6:
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
if ((r = codes.proc(this, z, r)) != 1)
{
return inflate_flush(z, r);
}
r = 0;
codes.free(z);
num = z.next_in_index;
num2 = z.avail_in;
num3 = bitb;
i = bitk;
num4 = write;
num5 = ((num4 < read) ? (read - num4 - 1) : (end - num4));
if (last == 0)
{
mode = 0;
break;
}
mode = 7;
goto case 7;
case 7:
write = num4;
r = inflate_flush(z, r);
num4 = write;
num5 = ((num4 < read) ? (read - num4 - 1) : (end - num4));
if (read != write)
{
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
mode = 8;
goto case 8;
case 8:
r = 1;
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
case 9:
r = -3;
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
default:
r = -2;
bitb = num3;
bitk = i;
z.avail_in = num2;
z.total_in += num - z.next_in_index;
z.next_in_index = num;
write = num4;
return inflate_flush(z, r);
}
}
}
internal void free(ZStream z)
{
reset(z, null);
window = null;
hufts = null;
}
internal void set_dictionary(byte[] d, int start, int n)
{
Array.Copy(d, start, window, 0, n);
read = (write = n);
}
internal int sync_point()
{
if (mode != 1)
{
return 0;
}
return 1;
}
internal int inflate_flush(ZStream z, int r)
{
int next_out_index = z.next_out_index;
int num = read;
int num2 = ((num <= write) ? write : end) - num;
if (num2 > z.avail_out)
{
num2 = z.avail_out;
}
if (num2 != 0 && r == -5)
{
r = 0;
}
z.avail_out -= num2;
z.total_out += num2;
if (checkfn != null)
{
z.adler = (check = z._adler.adler32(check, window, num, num2));
}
Array.Copy(window, num, z.next_out, next_out_index, num2);
next_out_index += num2;
num += num2;
if (num == end)
{
num = 0;
if (write == end)
{
write = 0;
}
num2 = write - num;
if (num2 > z.avail_out)
{
num2 = z.avail_out;
}
if (num2 != 0 && r == -5)
{
r = 0;
}
z.avail_out -= num2;
z.total_out += num2;
if (checkfn != null)
{
z.adler = (check = z._adler.adler32(check, window, num, num2));
}
Array.Copy(window, num, z.next_out, next_out_index, num2);
next_out_index += num2;
num += num2;
}
z.next_out_index = next_out_index;
read = num;
return r;
}
}

View File

@@ -0,0 +1,660 @@
using System;
namespace Org.BouncyCastle.Utilities.Zlib;
internal sealed class InfCodes
{
private const int Z_OK = 0;
private const int Z_STREAM_END = 1;
private const int Z_NEED_DICT = 2;
private const int Z_ERRNO = -1;
private const int Z_STREAM_ERROR = -2;
private const int Z_DATA_ERROR = -3;
private const int Z_MEM_ERROR = -4;
private const int Z_BUF_ERROR = -5;
private const int Z_VERSION_ERROR = -6;
private const int START = 0;
private const int LEN = 1;
private const int LENEXT = 2;
private const int DIST = 3;
private const int DISTEXT = 4;
private const int COPY = 5;
private const int LIT = 6;
private const int WASH = 7;
private const int END = 8;
private const int BADCODE = 9;
private static readonly int[] inflate_mask = new int[17]
{
0, 1, 3, 7, 15, 31, 63, 127, 255, 511,
1023, 2047, 4095, 8191, 16383, 32767, 65535
};
private int mode;
private int len;
private int[] tree;
private int tree_index = 0;
private int need;
private int lit;
private int get;
private int dist;
private byte lbits;
private byte dbits;
private int[] ltree;
private int ltree_index;
private int[] dtree;
private int dtree_index;
internal InfCodes()
{
}
internal void init(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, ZStream z)
{
mode = 0;
lbits = (byte)bl;
dbits = (byte)bd;
ltree = tl;
ltree_index = tl_index;
dtree = td;
dtree_index = td_index;
tree = null;
}
internal int proc(InfBlocks s, ZStream z, int r)
{
int num = 0;
int num2 = 0;
int num3 = 0;
num3 = z.next_in_index;
int num4 = z.avail_in;
num = s.bitb;
num2 = s.bitk;
int num5 = s.write;
int num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
while (true)
{
switch (mode)
{
case 0:
if (num6 >= 258 && num4 >= 10)
{
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
r = inflate_fast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, s, z);
num3 = z.next_in_index;
num4 = z.avail_in;
num = s.bitb;
num2 = s.bitk;
num5 = s.write;
num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
if (r != 0)
{
mode = ((r == 1) ? 7 : 9);
break;
}
}
need = lbits;
tree = ltree;
tree_index = ltree_index;
mode = 1;
goto case 1;
case 1:
{
int num7;
for (num7 = need; num2 < num7; num2 += 8)
{
if (num4 != 0)
{
r = 0;
num4--;
num |= (z.next_in[num3++] & 0xFF) << num2;
continue;
}
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
return s.inflate_flush(z, r);
}
int num8 = (tree_index + (num & inflate_mask[num7])) * 3;
num >>= tree[num8 + 1];
num2 -= tree[num8 + 1];
int num9 = tree[num8];
if (num9 == 0)
{
lit = tree[num8 + 2];
mode = 6;
break;
}
if ((num9 & 0x10) != 0)
{
get = num9 & 0xF;
len = tree[num8 + 2];
mode = 2;
break;
}
if ((num9 & 0x40) == 0)
{
need = num9;
tree_index = num8 / 3 + tree[num8 + 2];
break;
}
if ((num9 & 0x20) != 0)
{
mode = 7;
break;
}
mode = 9;
z.msg = "invalid literal/length code";
r = -3;
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
return s.inflate_flush(z, r);
}
case 2:
{
int num7;
for (num7 = get; num2 < num7; num2 += 8)
{
if (num4 != 0)
{
r = 0;
num4--;
num |= (z.next_in[num3++] & 0xFF) << num2;
continue;
}
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
return s.inflate_flush(z, r);
}
len += num & inflate_mask[num7];
num >>= num7;
num2 -= num7;
need = dbits;
tree = dtree;
tree_index = dtree_index;
mode = 3;
goto case 3;
}
case 3:
{
int num7;
for (num7 = need; num2 < num7; num2 += 8)
{
if (num4 != 0)
{
r = 0;
num4--;
num |= (z.next_in[num3++] & 0xFF) << num2;
continue;
}
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
return s.inflate_flush(z, r);
}
int num8 = (tree_index + (num & inflate_mask[num7])) * 3;
num >>= tree[num8 + 1];
num2 -= tree[num8 + 1];
int num9 = tree[num8];
if ((num9 & 0x10) != 0)
{
get = num9 & 0xF;
dist = tree[num8 + 2];
mode = 4;
break;
}
if ((num9 & 0x40) == 0)
{
need = num9;
tree_index = num8 / 3 + tree[num8 + 2];
break;
}
mode = 9;
z.msg = "invalid distance code";
r = -3;
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
return s.inflate_flush(z, r);
}
case 4:
{
int num7;
for (num7 = get; num2 < num7; num2 += 8)
{
if (num4 != 0)
{
r = 0;
num4--;
num |= (z.next_in[num3++] & 0xFF) << num2;
continue;
}
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
return s.inflate_flush(z, r);
}
dist += num & inflate_mask[num7];
num >>= num7;
num2 -= num7;
mode = 5;
goto case 5;
}
case 5:
{
int i;
for (i = num5 - dist; i < 0; i += s.end)
{
}
while (len != 0)
{
if (num6 == 0)
{
if (num5 == s.end && s.read != 0)
{
num5 = 0;
num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
}
if (num6 == 0)
{
s.write = num5;
r = s.inflate_flush(z, r);
num5 = s.write;
num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
if (num5 == s.end && s.read != 0)
{
num5 = 0;
num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
}
if (num6 == 0)
{
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
return s.inflate_flush(z, r);
}
}
}
s.window[num5++] = s.window[i++];
num6--;
if (i == s.end)
{
i = 0;
}
len--;
}
mode = 0;
break;
}
case 6:
if (num6 == 0)
{
if (num5 == s.end && s.read != 0)
{
num5 = 0;
num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
}
if (num6 == 0)
{
s.write = num5;
r = s.inflate_flush(z, r);
num5 = s.write;
num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
if (num5 == s.end && s.read != 0)
{
num5 = 0;
num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
}
if (num6 == 0)
{
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
return s.inflate_flush(z, r);
}
}
}
r = 0;
s.window[num5++] = (byte)lit;
num6--;
mode = 0;
break;
case 7:
if (num2 > 7)
{
num2 -= 8;
num4++;
num3--;
}
s.write = num5;
r = s.inflate_flush(z, r);
num5 = s.write;
num6 = ((num5 < s.read) ? (s.read - num5 - 1) : (s.end - num5));
if (s.read != s.write)
{
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
return s.inflate_flush(z, r);
}
mode = 8;
goto case 8;
case 8:
r = 1;
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
return s.inflate_flush(z, r);
case 9:
r = -3;
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
return s.inflate_flush(z, r);
default:
r = -2;
s.bitb = num;
s.bitk = num2;
z.avail_in = num4;
z.total_in += num3 - z.next_in_index;
z.next_in_index = num3;
s.write = num5;
return s.inflate_flush(z, r);
}
}
}
internal void free(ZStream z)
{
}
internal int inflate_fast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InfBlocks s, ZStream z)
{
int next_in_index = z.next_in_index;
int num = z.avail_in;
int num2 = s.bitb;
int num3 = s.bitk;
int num4 = s.write;
int num5 = ((num4 < s.read) ? (s.read - num4 - 1) : (s.end - num4));
int num6 = inflate_mask[bl];
int num7 = inflate_mask[bd];
int num12;
while (true)
{
if (num3 < 20)
{
num--;
num2 |= (z.next_in[next_in_index++] & 0xFF) << num3;
num3 += 8;
continue;
}
int num8 = num2 & num6;
int[] array = tl;
int num9 = tl_index;
int num10 = (num9 + num8) * 3;
int num11;
if ((num11 = array[num10]) == 0)
{
num2 >>= array[num10 + 1];
num3 -= array[num10 + 1];
s.window[num4++] = (byte)array[num10 + 2];
num5--;
}
else
{
while (true)
{
num2 >>= array[num10 + 1];
num3 -= array[num10 + 1];
if ((num11 & 0x10) != 0)
{
num11 &= 0xF;
num12 = array[num10 + 2] + (num2 & inflate_mask[num11]);
num2 >>= num11;
for (num3 -= num11; num3 < 15; num3 += 8)
{
num--;
num2 |= (z.next_in[next_in_index++] & 0xFF) << num3;
}
num8 = num2 & num7;
array = td;
num9 = td_index;
num10 = (num9 + num8) * 3;
num11 = array[num10];
while (true)
{
num2 >>= array[num10 + 1];
num3 -= array[num10 + 1];
if ((num11 & 0x10) != 0)
{
break;
}
if ((num11 & 0x40) == 0)
{
num8 += array[num10 + 2];
num8 += num2 & inflate_mask[num11];
num10 = (num9 + num8) * 3;
num11 = array[num10];
continue;
}
z.msg = "invalid distance code";
num12 = z.avail_in - num;
num12 = ((num3 >> 3 < num12) ? (num3 >> 3) : num12);
num += num12;
next_in_index -= num12;
num3 -= num12 << 3;
s.bitb = num2;
s.bitk = num3;
z.avail_in = num;
z.total_in += next_in_index - z.next_in_index;
z.next_in_index = next_in_index;
s.write = num4;
return -3;
}
for (num11 &= 0xF; num3 < num11; num3 += 8)
{
num--;
num2 |= (z.next_in[next_in_index++] & 0xFF) << num3;
}
int num13 = array[num10 + 2] + (num2 & inflate_mask[num11]);
num2 >>= num11;
num3 -= num11;
num5 -= num12;
int num14;
if (num4 >= num13)
{
num14 = num4 - num13;
if (num4 - num14 > 0 && 2 > num4 - num14)
{
s.window[num4++] = s.window[num14++];
s.window[num4++] = s.window[num14++];
num12 -= 2;
}
else
{
Array.Copy(s.window, num14, s.window, num4, 2);
num4 += 2;
num14 += 2;
num12 -= 2;
}
}
else
{
num14 = num4 - num13;
do
{
num14 += s.end;
}
while (num14 < 0);
num11 = s.end - num14;
if (num12 > num11)
{
num12 -= num11;
if (num4 - num14 > 0 && num11 > num4 - num14)
{
do
{
s.window[num4++] = s.window[num14++];
}
while (--num11 != 0);
}
else
{
Array.Copy(s.window, num14, s.window, num4, num11);
num4 += num11;
num14 += num11;
num11 = 0;
}
num14 = 0;
}
}
if (num4 - num14 > 0 && num12 > num4 - num14)
{
do
{
s.window[num4++] = s.window[num14++];
}
while (--num12 != 0);
break;
}
Array.Copy(s.window, num14, s.window, num4, num12);
num4 += num12;
num14 += num12;
num12 = 0;
break;
}
if ((num11 & 0x40) == 0)
{
num8 += array[num10 + 2];
num8 += num2 & inflate_mask[num11];
num10 = (num9 + num8) * 3;
if ((num11 = array[num10]) == 0)
{
num2 >>= array[num10 + 1];
num3 -= array[num10 + 1];
s.window[num4++] = (byte)array[num10 + 2];
num5--;
break;
}
continue;
}
if ((num11 & 0x20) != 0)
{
num12 = z.avail_in - num;
num12 = ((num3 >> 3 < num12) ? (num3 >> 3) : num12);
num += num12;
next_in_index -= num12;
num3 -= num12 << 3;
s.bitb = num2;
s.bitk = num3;
z.avail_in = num;
z.total_in += next_in_index - z.next_in_index;
z.next_in_index = next_in_index;
s.write = num4;
return 1;
}
z.msg = "invalid literal/length code";
num12 = z.avail_in - num;
num12 = ((num3 >> 3 < num12) ? (num3 >> 3) : num12);
num += num12;
next_in_index -= num12;
num3 -= num12 << 3;
s.bitb = num2;
s.bitk = num3;
z.avail_in = num;
z.total_in += next_in_index - z.next_in_index;
z.next_in_index = next_in_index;
s.write = num4;
return -3;
}
}
if (num5 < 258 || num < 10)
{
break;
}
}
num12 = z.avail_in - num;
num12 = ((num3 >> 3 < num12) ? (num3 >> 3) : num12);
num += num12;
next_in_index -= num12;
num3 -= num12 << 3;
s.bitb = num2;
s.bitk = num3;
z.avail_in = num;
z.total_in += next_in_index - z.next_in_index;
z.next_in_index = next_in_index;
s.write = num4;
return 0;
}
}

View File

@@ -0,0 +1,531 @@
using System;
namespace Org.BouncyCastle.Utilities.Zlib;
internal sealed class InfTree
{
private const int MANY = 1440;
private const int Z_OK = 0;
private const int Z_STREAM_END = 1;
private const int Z_NEED_DICT = 2;
private const int Z_ERRNO = -1;
private const int Z_STREAM_ERROR = -2;
private const int Z_DATA_ERROR = -3;
private const int Z_MEM_ERROR = -4;
private const int Z_BUF_ERROR = -5;
private const int Z_VERSION_ERROR = -6;
private const int fixed_bl = 9;
private const int fixed_bd = 5;
private const int BMAX = 15;
private static readonly int[] fixed_tl = new int[1536]
{
96, 7, 256, 0, 8, 80, 0, 8, 16, 84,
8, 115, 82, 7, 31, 0, 8, 112, 0, 8,
48, 0, 9, 192, 80, 7, 10, 0, 8, 96,
0, 8, 32, 0, 9, 160, 0, 8, 0, 0,
8, 128, 0, 8, 64, 0, 9, 224, 80, 7,
6, 0, 8, 88, 0, 8, 24, 0, 9, 144,
83, 7, 59, 0, 8, 120, 0, 8, 56, 0,
9, 208, 81, 7, 17, 0, 8, 104, 0, 8,
40, 0, 9, 176, 0, 8, 8, 0, 8, 136,
0, 8, 72, 0, 9, 240, 80, 7, 4, 0,
8, 84, 0, 8, 20, 85, 8, 227, 83, 7,
43, 0, 8, 116, 0, 8, 52, 0, 9, 200,
81, 7, 13, 0, 8, 100, 0, 8, 36, 0,
9, 168, 0, 8, 4, 0, 8, 132, 0, 8,
68, 0, 9, 232, 80, 7, 8, 0, 8, 92,
0, 8, 28, 0, 9, 152, 84, 7, 83, 0,
8, 124, 0, 8, 60, 0, 9, 216, 82, 7,
23, 0, 8, 108, 0, 8, 44, 0, 9, 184,
0, 8, 12, 0, 8, 140, 0, 8, 76, 0,
9, 248, 80, 7, 3, 0, 8, 82, 0, 8,
18, 85, 8, 163, 83, 7, 35, 0, 8, 114,
0, 8, 50, 0, 9, 196, 81, 7, 11, 0,
8, 98, 0, 8, 34, 0, 9, 164, 0, 8,
2, 0, 8, 130, 0, 8, 66, 0, 9, 228,
80, 7, 7, 0, 8, 90, 0, 8, 26, 0,
9, 148, 84, 7, 67, 0, 8, 122, 0, 8,
58, 0, 9, 212, 82, 7, 19, 0, 8, 106,
0, 8, 42, 0, 9, 180, 0, 8, 10, 0,
8, 138, 0, 8, 74, 0, 9, 244, 80, 7,
5, 0, 8, 86, 0, 8, 22, 192, 8, 0,
83, 7, 51, 0, 8, 118, 0, 8, 54, 0,
9, 204, 81, 7, 15, 0, 8, 102, 0, 8,
38, 0, 9, 172, 0, 8, 6, 0, 8, 134,
0, 8, 70, 0, 9, 236, 80, 7, 9, 0,
8, 94, 0, 8, 30, 0, 9, 156, 84, 7,
99, 0, 8, 126, 0, 8, 62, 0, 9, 220,
82, 7, 27, 0, 8, 110, 0, 8, 46, 0,
9, 188, 0, 8, 14, 0, 8, 142, 0, 8,
78, 0, 9, 252, 96, 7, 256, 0, 8, 81,
0, 8, 17, 85, 8, 131, 82, 7, 31, 0,
8, 113, 0, 8, 49, 0, 9, 194, 80, 7,
10, 0, 8, 97, 0, 8, 33, 0, 9, 162,
0, 8, 1, 0, 8, 129, 0, 8, 65, 0,
9, 226, 80, 7, 6, 0, 8, 89, 0, 8,
25, 0, 9, 146, 83, 7, 59, 0, 8, 121,
0, 8, 57, 0, 9, 210, 81, 7, 17, 0,
8, 105, 0, 8, 41, 0, 9, 178, 0, 8,
9, 0, 8, 137, 0, 8, 73, 0, 9, 242,
80, 7, 4, 0, 8, 85, 0, 8, 21, 80,
8, 258, 83, 7, 43, 0, 8, 117, 0, 8,
53, 0, 9, 202, 81, 7, 13, 0, 8, 101,
0, 8, 37, 0, 9, 170, 0, 8, 5, 0,
8, 133, 0, 8, 69, 0, 9, 234, 80, 7,
8, 0, 8, 93, 0, 8, 29, 0, 9, 154,
84, 7, 83, 0, 8, 125, 0, 8, 61, 0,
9, 218, 82, 7, 23, 0, 8, 109, 0, 8,
45, 0, 9, 186, 0, 8, 13, 0, 8, 141,
0, 8, 77, 0, 9, 250, 80, 7, 3, 0,
8, 83, 0, 8, 19, 85, 8, 195, 83, 7,
35, 0, 8, 115, 0, 8, 51, 0, 9, 198,
81, 7, 11, 0, 8, 99, 0, 8, 35, 0,
9, 166, 0, 8, 3, 0, 8, 131, 0, 8,
67, 0, 9, 230, 80, 7, 7, 0, 8, 91,
0, 8, 27, 0, 9, 150, 84, 7, 67, 0,
8, 123, 0, 8, 59, 0, 9, 214, 82, 7,
19, 0, 8, 107, 0, 8, 43, 0, 9, 182,
0, 8, 11, 0, 8, 139, 0, 8, 75, 0,
9, 246, 80, 7, 5, 0, 8, 87, 0, 8,
23, 192, 8, 0, 83, 7, 51, 0, 8, 119,
0, 8, 55, 0, 9, 206, 81, 7, 15, 0,
8, 103, 0, 8, 39, 0, 9, 174, 0, 8,
7, 0, 8, 135, 0, 8, 71, 0, 9, 238,
80, 7, 9, 0, 8, 95, 0, 8, 31, 0,
9, 158, 84, 7, 99, 0, 8, 127, 0, 8,
63, 0, 9, 222, 82, 7, 27, 0, 8, 111,
0, 8, 47, 0, 9, 190, 0, 8, 15, 0,
8, 143, 0, 8, 79, 0, 9, 254, 96, 7,
256, 0, 8, 80, 0, 8, 16, 84, 8, 115,
82, 7, 31, 0, 8, 112, 0, 8, 48, 0,
9, 193, 80, 7, 10, 0, 8, 96, 0, 8,
32, 0, 9, 161, 0, 8, 0, 0, 8, 128,
0, 8, 64, 0, 9, 225, 80, 7, 6, 0,
8, 88, 0, 8, 24, 0, 9, 145, 83, 7,
59, 0, 8, 120, 0, 8, 56, 0, 9, 209,
81, 7, 17, 0, 8, 104, 0, 8, 40, 0,
9, 177, 0, 8, 8, 0, 8, 136, 0, 8,
72, 0, 9, 241, 80, 7, 4, 0, 8, 84,
0, 8, 20, 85, 8, 227, 83, 7, 43, 0,
8, 116, 0, 8, 52, 0, 9, 201, 81, 7,
13, 0, 8, 100, 0, 8, 36, 0, 9, 169,
0, 8, 4, 0, 8, 132, 0, 8, 68, 0,
9, 233, 80, 7, 8, 0, 8, 92, 0, 8,
28, 0, 9, 153, 84, 7, 83, 0, 8, 124,
0, 8, 60, 0, 9, 217, 82, 7, 23, 0,
8, 108, 0, 8, 44, 0, 9, 185, 0, 8,
12, 0, 8, 140, 0, 8, 76, 0, 9, 249,
80, 7, 3, 0, 8, 82, 0, 8, 18, 85,
8, 163, 83, 7, 35, 0, 8, 114, 0, 8,
50, 0, 9, 197, 81, 7, 11, 0, 8, 98,
0, 8, 34, 0, 9, 165, 0, 8, 2, 0,
8, 130, 0, 8, 66, 0, 9, 229, 80, 7,
7, 0, 8, 90, 0, 8, 26, 0, 9, 149,
84, 7, 67, 0, 8, 122, 0, 8, 58, 0,
9, 213, 82, 7, 19, 0, 8, 106, 0, 8,
42, 0, 9, 181, 0, 8, 10, 0, 8, 138,
0, 8, 74, 0, 9, 245, 80, 7, 5, 0,
8, 86, 0, 8, 22, 192, 8, 0, 83, 7,
51, 0, 8, 118, 0, 8, 54, 0, 9, 205,
81, 7, 15, 0, 8, 102, 0, 8, 38, 0,
9, 173, 0, 8, 6, 0, 8, 134, 0, 8,
70, 0, 9, 237, 80, 7, 9, 0, 8, 94,
0, 8, 30, 0, 9, 157, 84, 7, 99, 0,
8, 126, 0, 8, 62, 0, 9, 221, 82, 7,
27, 0, 8, 110, 0, 8, 46, 0, 9, 189,
0, 8, 14, 0, 8, 142, 0, 8, 78, 0,
9, 253, 96, 7, 256, 0, 8, 81, 0, 8,
17, 85, 8, 131, 82, 7, 31, 0, 8, 113,
0, 8, 49, 0, 9, 195, 80, 7, 10, 0,
8, 97, 0, 8, 33, 0, 9, 163, 0, 8,
1, 0, 8, 129, 0, 8, 65, 0, 9, 227,
80, 7, 6, 0, 8, 89, 0, 8, 25, 0,
9, 147, 83, 7, 59, 0, 8, 121, 0, 8,
57, 0, 9, 211, 81, 7, 17, 0, 8, 105,
0, 8, 41, 0, 9, 179, 0, 8, 9, 0,
8, 137, 0, 8, 73, 0, 9, 243, 80, 7,
4, 0, 8, 85, 0, 8, 21, 80, 8, 258,
83, 7, 43, 0, 8, 117, 0, 8, 53, 0,
9, 203, 81, 7, 13, 0, 8, 101, 0, 8,
37, 0, 9, 171, 0, 8, 5, 0, 8, 133,
0, 8, 69, 0, 9, 235, 80, 7, 8, 0,
8, 93, 0, 8, 29, 0, 9, 155, 84, 7,
83, 0, 8, 125, 0, 8, 61, 0, 9, 219,
82, 7, 23, 0, 8, 109, 0, 8, 45, 0,
9, 187, 0, 8, 13, 0, 8, 141, 0, 8,
77, 0, 9, 251, 80, 7, 3, 0, 8, 83,
0, 8, 19, 85, 8, 195, 83, 7, 35, 0,
8, 115, 0, 8, 51, 0, 9, 199, 81, 7,
11, 0, 8, 99, 0, 8, 35, 0, 9, 167,
0, 8, 3, 0, 8, 131, 0, 8, 67, 0,
9, 231, 80, 7, 7, 0, 8, 91, 0, 8,
27, 0, 9, 151, 84, 7, 67, 0, 8, 123,
0, 8, 59, 0, 9, 215, 82, 7, 19, 0,
8, 107, 0, 8, 43, 0, 9, 183, 0, 8,
11, 0, 8, 139, 0, 8, 75, 0, 9, 247,
80, 7, 5, 0, 8, 87, 0, 8, 23, 192,
8, 0, 83, 7, 51, 0, 8, 119, 0, 8,
55, 0, 9, 207, 81, 7, 15, 0, 8, 103,
0, 8, 39, 0, 9, 175, 0, 8, 7, 0,
8, 135, 0, 8, 71, 0, 9, 239, 80, 7,
9, 0, 8, 95, 0, 8, 31, 0, 9, 159,
84, 7, 99, 0, 8, 127, 0, 8, 63, 0,
9, 223, 82, 7, 27, 0, 8, 111, 0, 8,
47, 0, 9, 191, 0, 8, 15, 0, 8, 143,
0, 8, 79, 0, 9, 255
};
private static readonly int[] fixed_td = new int[96]
{
80, 5, 1, 87, 5, 257, 83, 5, 17, 91,
5, 4097, 81, 5, 5, 89, 5, 1025, 85, 5,
65, 93, 5, 16385, 80, 5, 3, 88, 5, 513,
84, 5, 33, 92, 5, 8193, 82, 5, 9, 90,
5, 2049, 86, 5, 129, 192, 5, 24577, 80, 5,
2, 87, 5, 385, 83, 5, 25, 91, 5, 6145,
81, 5, 7, 89, 5, 1537, 85, 5, 97, 93,
5, 24577, 80, 5, 4, 88, 5, 769, 84, 5,
49, 92, 5, 12289, 82, 5, 13, 90, 5, 3073,
86, 5, 193, 192, 5, 24577
};
private static readonly int[] cplens = new int[31]
{
3, 4, 5, 6, 7, 8, 9, 10, 11, 13,
15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
67, 83, 99, 115, 131, 163, 195, 227, 258, 0,
0
};
private static readonly int[] cplext = new int[31]
{
0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4, 5, 5, 5, 5, 0, 112,
112
};
private static readonly int[] cpdist = new int[30]
{
1, 2, 3, 4, 5, 7, 9, 13, 17, 25,
33, 49, 65, 97, 129, 193, 257, 385, 513, 769,
1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
};
private static readonly int[] cpdext = new int[30]
{
0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
9, 9, 10, 10, 11, 11, 12, 12, 13, 13
};
private int[] hn = null;
private int[] v = null;
private int[] c = null;
private int[] r = null;
private int[] u = null;
private int[] x = null;
private int huft_build(int[] b, int bindex, int n, int s, int[] d, int[] e, int[] t, int[] m, int[] hp, int[] hn, int[] v)
{
int num = 0;
int num2 = n;
nint num4;
int[] array2;
do
{
int[] array = (array2 = c);
int num3 = b[bindex + num];
num4 = num3;
array[num3] = array2[num4] + 1;
num++;
num2--;
}
while (num2 != 0);
if (c[0] == n)
{
t[0] = -1;
m[0] = 0;
return 0;
}
int num5 = m[0];
int i;
for (i = 1; i <= 15 && c[i] == 0; i++)
{
}
int j = i;
if (num5 < i)
{
num5 = i;
}
num2 = 15;
while (num2 != 0 && c[num2] == 0)
{
num2--;
}
int num6 = num2;
if (num5 > num2)
{
num5 = num2;
}
m[0] = num5;
int num7 = 1 << i;
while (i < num2)
{
if ((num7 -= c[i]) < 0)
{
return -3;
}
i++;
num7 <<= 1;
}
if ((num7 -= c[num2]) < 0)
{
return -3;
}
int[] array3 = (array2 = c);
int num8 = num2;
num4 = num8;
array3[num8] = array2[num4] + num7;
i = (x[1] = 0);
num = 1;
int num9 = 2;
while (--num2 != 0)
{
i = (x[num9] = i + c[num]);
num9++;
num++;
}
num2 = 0;
num = 0;
do
{
if ((i = b[bindex + num]) != 0)
{
int[] array4 = (array2 = x);
int num10 = i;
num4 = num10;
int num11;
array4[num10] = (num11 = array2[num4]) + 1;
v[num11] = num2;
}
num++;
}
while (++num2 < n);
n = x[num6];
num2 = (x[0] = 0);
num = 0;
int num12 = -1;
int num13 = -num5;
u[0] = 0;
int num14 = 0;
int num15 = 0;
for (; j <= num6; j++)
{
int num16 = c[j];
while (num16-- != 0)
{
int num17;
while (j > num13 + num5)
{
num12++;
num13 += num5;
num15 = num6 - num13;
num15 = ((num15 > num5) ? num5 : num15);
if ((num17 = 1 << (i = j - num13)) > num16 + 1)
{
num17 -= num16 + 1;
num9 = j;
if (i < num15)
{
while (++i < num15 && (num17 <<= 1) > c[++num9])
{
num17 -= c[num9];
}
}
}
num15 = 1 << i;
if (hn[0] + num15 > 1440)
{
return -3;
}
num14 = (u[num12] = hn[0]);
(array2 = hn)[0] = array2[0] + num15;
if (num12 != 0)
{
x[num12] = num2;
r[0] = (byte)i;
r[1] = (byte)num5;
i = num2 >> num13 - num5;
r[2] = num14 - u[num12 - 1] - i;
Array.Copy(r, 0, hp, (u[num12 - 1] + i) * 3, 3);
}
else
{
t[0] = num14;
}
}
r[1] = (byte)(j - num13);
if (num >= n)
{
r[0] = 192;
}
else if (v[num] < s)
{
r[0] = (byte)((v[num] >= 256) ? 96 : 0);
r[2] = v[num++];
}
else
{
r[0] = (byte)(e[v[num] - s] + 16 + 64);
r[2] = d[v[num++] - s];
}
num17 = 1 << j - num13;
for (i = num2 >> num13; i < num15; i += num17)
{
Array.Copy(r, 0, hp, (num14 + i) * 3, 3);
}
i = 1 << j - 1;
while ((num2 & i) != 0)
{
num2 ^= i;
i >>= 1;
}
num2 ^= i;
int num18 = (1 << num13) - 1;
while ((num2 & num18) != x[num12])
{
num12--;
num13 -= num5;
num18 = (1 << num13) - 1;
}
}
}
if (num7 == 0 || num6 == 1)
{
return 0;
}
return -5;
}
internal int inflate_trees_bits(int[] c, int[] bb, int[] tb, int[] hp, ZStream z)
{
initWorkArea(19);
hn[0] = 0;
int num = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
if (num == -3)
{
z.msg = "oversubscribed dynamic bit lengths tree";
}
else if (num == -5 || bb[0] == 0)
{
z.msg = "incomplete dynamic bit lengths tree";
num = -3;
}
return num;
}
internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZStream z)
{
initWorkArea(288);
hn[0] = 0;
int num = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
if (num != 0 || bl[0] == 0)
{
switch (num)
{
case -3:
z.msg = "oversubscribed literal/length tree";
break;
default:
z.msg = "incomplete literal/length tree";
num = -3;
break;
case -4:
break;
}
return num;
}
initWorkArea(288);
num = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
if (num != 0 || (bd[0] == 0 && nl > 257))
{
switch (num)
{
case -3:
z.msg = "oversubscribed distance tree";
break;
case -5:
z.msg = "incomplete distance tree";
num = -3;
break;
default:
z.msg = "empty distance tree with lengths";
num = -3;
break;
case -4:
break;
}
return num;
}
return 0;
}
internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZStream z)
{
bl[0] = 9;
bd[0] = 5;
tl[0] = fixed_tl;
td[0] = fixed_td;
return 0;
}
private void initWorkArea(int vsize)
{
if (hn == null)
{
hn = new int[1];
v = new int[vsize];
c = new int[16];
r = new int[3];
u = new int[15];
x = new int[16];
}
if (v.Length < vsize)
{
v = new int[vsize];
}
for (int i = 0; i < vsize; i++)
{
v[i] = 0;
}
for (int j = 0; j < 16; j++)
{
c[j] = 0;
}
for (int k = 0; k < 3; k++)
{
r[k] = 0;
}
Array.Copy(c, 0, u, 0, 15);
Array.Copy(c, 0, x, 0, 16);
}
}

View File

@@ -0,0 +1,402 @@
namespace Org.BouncyCastle.Utilities.Zlib;
internal sealed class Inflate
{
private const int MAX_WBITS = 15;
private const int PRESET_DICT = 32;
internal const int Z_NO_FLUSH = 0;
internal const int Z_PARTIAL_FLUSH = 1;
internal const int Z_SYNC_FLUSH = 2;
internal const int Z_FULL_FLUSH = 3;
internal const int Z_FINISH = 4;
private const int Z_DEFLATED = 8;
private const int Z_OK = 0;
private const int Z_STREAM_END = 1;
private const int Z_NEED_DICT = 2;
private const int Z_ERRNO = -1;
private const int Z_STREAM_ERROR = -2;
private const int Z_DATA_ERROR = -3;
private const int Z_MEM_ERROR = -4;
private const int Z_BUF_ERROR = -5;
private const int Z_VERSION_ERROR = -6;
private const int METHOD = 0;
private const int FLAG = 1;
private const int DICT4 = 2;
private const int DICT3 = 3;
private const int DICT2 = 4;
private const int DICT1 = 5;
private const int DICT0 = 6;
private const int BLOCKS = 7;
private const int CHECK4 = 8;
private const int CHECK3 = 9;
private const int CHECK2 = 10;
private const int CHECK1 = 11;
private const int DONE = 12;
private const int BAD = 13;
internal int mode;
internal int method;
internal long[] was = new long[1];
internal long need;
internal int marker;
internal int nowrap;
internal int wbits;
internal InfBlocks blocks;
private static readonly byte[] mark = new byte[4] { 0, 0, 255, 255 };
internal int inflateReset(ZStream z)
{
if (z == null || z.istate == null)
{
return -2;
}
z.total_in = (z.total_out = 0L);
z.msg = null;
z.istate.mode = ((z.istate.nowrap != 0) ? 7 : 0);
z.istate.blocks.reset(z, null);
return 0;
}
internal int inflateEnd(ZStream z)
{
if (blocks != null)
{
blocks.free(z);
}
blocks = null;
return 0;
}
internal int inflateInit(ZStream z, int w)
{
z.msg = null;
blocks = null;
nowrap = 0;
if (w < 0)
{
w = -w;
nowrap = 1;
}
if (w < 8 || w > 15)
{
inflateEnd(z);
return -2;
}
wbits = w;
z.istate.blocks = new InfBlocks(z, (z.istate.nowrap != 0) ? null : this, 1 << w);
inflateReset(z);
return 0;
}
internal int inflate(ZStream z, int f)
{
if (z == null || z.istate == null || z.next_in == null)
{
return -2;
}
f = ((f == 4) ? (-5) : 0);
int num = -5;
while (true)
{
switch (z.istate.mode)
{
case 0:
if (z.avail_in == 0)
{
return num;
}
num = f;
z.avail_in--;
z.total_in++;
if (((z.istate.method = z.next_in[z.next_in_index++]) & 0xF) != 8)
{
z.istate.mode = 13;
z.msg = "unknown compression method";
z.istate.marker = 5;
break;
}
if ((z.istate.method >> 4) + 8 > z.istate.wbits)
{
z.istate.mode = 13;
z.msg = "invalid window size";
z.istate.marker = 5;
break;
}
z.istate.mode = 1;
goto case 1;
case 1:
{
if (z.avail_in == 0)
{
return num;
}
num = f;
z.avail_in--;
z.total_in++;
int num2 = z.next_in[z.next_in_index++] & 0xFF;
if (((z.istate.method << 8) + num2) % 31 != 0)
{
z.istate.mode = 13;
z.msg = "incorrect header check";
z.istate.marker = 5;
break;
}
if ((num2 & 0x20) == 0)
{
z.istate.mode = 7;
break;
}
z.istate.mode = 2;
goto case 2;
}
case 2:
if (z.avail_in == 0)
{
return num;
}
num = f;
z.avail_in--;
z.total_in++;
z.istate.need = ((z.next_in[z.next_in_index++] & 0xFF) << 24) & 0xFF000000u;
z.istate.mode = 3;
goto case 3;
case 3:
if (z.avail_in == 0)
{
return num;
}
num = f;
z.avail_in--;
z.total_in++;
z.istate.need += (long)((ulong)((z.next_in[z.next_in_index++] & 0xFF) << 16) & 0xFF0000uL);
z.istate.mode = 4;
goto case 4;
case 4:
if (z.avail_in == 0)
{
return num;
}
num = f;
z.avail_in--;
z.total_in++;
z.istate.need += (long)((ulong)((z.next_in[z.next_in_index++] & 0xFF) << 8) & 0xFF00uL);
z.istate.mode = 5;
goto case 5;
case 5:
if (z.avail_in == 0)
{
return num;
}
num = f;
z.avail_in--;
z.total_in++;
z.istate.need += (long)((ulong)z.next_in[z.next_in_index++] & 0xFFuL);
z.adler = z.istate.need;
z.istate.mode = 6;
return 2;
case 6:
z.istate.mode = 13;
z.msg = "need dictionary";
z.istate.marker = 0;
return -2;
case 7:
num = z.istate.blocks.proc(z, num);
switch (num)
{
case -3:
z.istate.mode = 13;
z.istate.marker = 0;
goto end_IL_0031;
case 0:
num = f;
break;
}
if (num != 1)
{
return num;
}
num = f;
z.istate.blocks.reset(z, z.istate.was);
if (z.istate.nowrap != 0)
{
z.istate.mode = 12;
break;
}
z.istate.mode = 8;
goto case 8;
case 8:
if (z.avail_in == 0)
{
return num;
}
num = f;
z.avail_in--;
z.total_in++;
z.istate.need = ((z.next_in[z.next_in_index++] & 0xFF) << 24) & 0xFF000000u;
z.istate.mode = 9;
goto case 9;
case 9:
if (z.avail_in == 0)
{
return num;
}
num = f;
z.avail_in--;
z.total_in++;
z.istate.need += (long)((ulong)((z.next_in[z.next_in_index++] & 0xFF) << 16) & 0xFF0000uL);
z.istate.mode = 10;
goto case 10;
case 10:
if (z.avail_in == 0)
{
return num;
}
num = f;
z.avail_in--;
z.total_in++;
z.istate.need += (long)((ulong)((z.next_in[z.next_in_index++] & 0xFF) << 8) & 0xFF00uL);
z.istate.mode = 11;
goto case 11;
case 11:
if (z.avail_in == 0)
{
return num;
}
num = f;
z.avail_in--;
z.total_in++;
z.istate.need += (long)((ulong)z.next_in[z.next_in_index++] & 0xFFuL);
if ((int)z.istate.was[0] != (int)z.istate.need)
{
z.istate.mode = 13;
z.msg = "incorrect data check";
z.istate.marker = 5;
break;
}
z.istate.mode = 12;
goto case 12;
case 12:
return 1;
case 13:
return -3;
default:
{
return -2;
}
end_IL_0031:
break;
}
}
}
internal int inflateSetDictionary(ZStream z, byte[] dictionary, int dictLength)
{
int start = 0;
int num = dictLength;
if (z == null || z.istate == null || z.istate.mode != 6)
{
return -2;
}
if (z._adler.adler32(1L, dictionary, 0, dictLength) != z.adler)
{
return -3;
}
z.adler = z._adler.adler32(0L, null, 0, 0);
if (num >= 1 << z.istate.wbits)
{
num = (1 << z.istate.wbits) - 1;
start = dictLength - num;
}
z.istate.blocks.set_dictionary(dictionary, start, num);
z.istate.mode = 7;
return 0;
}
internal int inflateSync(ZStream z)
{
if (z == null || z.istate == null)
{
return -2;
}
if (z.istate.mode != 13)
{
z.istate.mode = 13;
z.istate.marker = 0;
}
int num;
if ((num = z.avail_in) == 0)
{
return -5;
}
int num2 = z.next_in_index;
int num3 = z.istate.marker;
while (num != 0 && num3 < 4)
{
num3 = ((z.next_in[num2] != mark[num3]) ? ((z.next_in[num2] == 0) ? (4 - num3) : 0) : (num3 + 1));
num2++;
num--;
}
z.total_in += num2 - z.next_in_index;
z.next_in_index = num2;
z.avail_in = num;
z.istate.marker = num3;
if (num3 != 4)
{
return -3;
}
long total_in = z.total_in;
long total_out = z.total_out;
inflateReset(z);
z.total_in = total_in;
z.total_out = total_out;
z.istate.mode = 7;
return 0;
}
internal int inflateSyncPoint(ZStream z)
{
if (z == null || z.istate == null || z.istate.blocks == null)
{
return -2;
}
return z.istate.blocks.sync_point();
}
}

View File

@@ -0,0 +1,53 @@
namespace Org.BouncyCastle.Utilities.Zlib;
public sealed class JZlib
{
private const string _version = "1.0.7";
public const int Z_NO_COMPRESSION = 0;
public const int Z_BEST_SPEED = 1;
public const int Z_BEST_COMPRESSION = 9;
public const int Z_DEFAULT_COMPRESSION = -1;
public const int Z_FILTERED = 1;
public const int Z_HUFFMAN_ONLY = 2;
public const int Z_DEFAULT_STRATEGY = 0;
public const int Z_NO_FLUSH = 0;
public const int Z_PARTIAL_FLUSH = 1;
public const int Z_SYNC_FLUSH = 2;
public const int Z_FULL_FLUSH = 3;
public const int Z_FINISH = 4;
public const int Z_OK = 0;
public const int Z_STREAM_END = 1;
public const int Z_NEED_DICT = 2;
public const int Z_ERRNO = -1;
public const int Z_STREAM_ERROR = -2;
public const int Z_DATA_ERROR = -3;
public const int Z_MEM_ERROR = -4;
public const int Z_BUF_ERROR = -5;
public const int Z_VERSION_ERROR = -6;
public static string version()
{
return "1.0.7";
}
}

View File

@@ -0,0 +1,115 @@
namespace Org.BouncyCastle.Utilities.Zlib;
internal sealed class StaticTree
{
private const int MAX_BITS = 15;
private const int BL_CODES = 19;
private const int D_CODES = 30;
private const int LITERALS = 256;
private const int LENGTH_CODES = 29;
private const int L_CODES = 286;
internal const int MAX_BL_BITS = 7;
internal static readonly short[] static_ltree = new short[576]
{
12, 8, 140, 8, 76, 8, 204, 8, 44, 8,
172, 8, 108, 8, 236, 8, 28, 8, 156, 8,
92, 8, 220, 8, 60, 8, 188, 8, 124, 8,
252, 8, 2, 8, 130, 8, 66, 8, 194, 8,
34, 8, 162, 8, 98, 8, 226, 8, 18, 8,
146, 8, 82, 8, 210, 8, 50, 8, 178, 8,
114, 8, 242, 8, 10, 8, 138, 8, 74, 8,
202, 8, 42, 8, 170, 8, 106, 8, 234, 8,
26, 8, 154, 8, 90, 8, 218, 8, 58, 8,
186, 8, 122, 8, 250, 8, 6, 8, 134, 8,
70, 8, 198, 8, 38, 8, 166, 8, 102, 8,
230, 8, 22, 8, 150, 8, 86, 8, 214, 8,
54, 8, 182, 8, 118, 8, 246, 8, 14, 8,
142, 8, 78, 8, 206, 8, 46, 8, 174, 8,
110, 8, 238, 8, 30, 8, 158, 8, 94, 8,
222, 8, 62, 8, 190, 8, 126, 8, 254, 8,
1, 8, 129, 8, 65, 8, 193, 8, 33, 8,
161, 8, 97, 8, 225, 8, 17, 8, 145, 8,
81, 8, 209, 8, 49, 8, 177, 8, 113, 8,
241, 8, 9, 8, 137, 8, 73, 8, 201, 8,
41, 8, 169, 8, 105, 8, 233, 8, 25, 8,
153, 8, 89, 8, 217, 8, 57, 8, 185, 8,
121, 8, 249, 8, 5, 8, 133, 8, 69, 8,
197, 8, 37, 8, 165, 8, 101, 8, 229, 8,
21, 8, 149, 8, 85, 8, 213, 8, 53, 8,
181, 8, 117, 8, 245, 8, 13, 8, 141, 8,
77, 8, 205, 8, 45, 8, 173, 8, 109, 8,
237, 8, 29, 8, 157, 8, 93, 8, 221, 8,
61, 8, 189, 8, 125, 8, 253, 8, 19, 9,
275, 9, 147, 9, 403, 9, 83, 9, 339, 9,
211, 9, 467, 9, 51, 9, 307, 9, 179, 9,
435, 9, 115, 9, 371, 9, 243, 9, 499, 9,
11, 9, 267, 9, 139, 9, 395, 9, 75, 9,
331, 9, 203, 9, 459, 9, 43, 9, 299, 9,
171, 9, 427, 9, 107, 9, 363, 9, 235, 9,
491, 9, 27, 9, 283, 9, 155, 9, 411, 9,
91, 9, 347, 9, 219, 9, 475, 9, 59, 9,
315, 9, 187, 9, 443, 9, 123, 9, 379, 9,
251, 9, 507, 9, 7, 9, 263, 9, 135, 9,
391, 9, 71, 9, 327, 9, 199, 9, 455, 9,
39, 9, 295, 9, 167, 9, 423, 9, 103, 9,
359, 9, 231, 9, 487, 9, 23, 9, 279, 9,
151, 9, 407, 9, 87, 9, 343, 9, 215, 9,
471, 9, 55, 9, 311, 9, 183, 9, 439, 9,
119, 9, 375, 9, 247, 9, 503, 9, 15, 9,
271, 9, 143, 9, 399, 9, 79, 9, 335, 9,
207, 9, 463, 9, 47, 9, 303, 9, 175, 9,
431, 9, 111, 9, 367, 9, 239, 9, 495, 9,
31, 9, 287, 9, 159, 9, 415, 9, 95, 9,
351, 9, 223, 9, 479, 9, 63, 9, 319, 9,
191, 9, 447, 9, 127, 9, 383, 9, 255, 9,
511, 9, 0, 7, 64, 7, 32, 7, 96, 7,
16, 7, 80, 7, 48, 7, 112, 7, 8, 7,
72, 7, 40, 7, 104, 7, 24, 7, 88, 7,
56, 7, 120, 7, 4, 7, 68, 7, 36, 7,
100, 7, 20, 7, 84, 7, 52, 7, 116, 7,
3, 8, 131, 8, 67, 8, 195, 8, 35, 8,
163, 8, 99, 8, 227, 8
};
internal static readonly short[] static_dtree = new short[60]
{
0, 5, 16, 5, 8, 5, 24, 5, 4, 5,
20, 5, 12, 5, 28, 5, 2, 5, 18, 5,
10, 5, 26, 5, 6, 5, 22, 5, 14, 5,
30, 5, 1, 5, 17, 5, 9, 5, 25, 5,
5, 5, 21, 5, 13, 5, 29, 5, 3, 5,
19, 5, 11, 5, 27, 5, 7, 5, 23, 5
};
internal static readonly StaticTree static_l_desc = new StaticTree(static_ltree, Tree.extra_lbits, 257, 286, 15);
internal static readonly StaticTree static_d_desc = new StaticTree(static_dtree, Tree.extra_dbits, 0, 30, 15);
internal static readonly StaticTree static_bl_desc = new StaticTree(null, Tree.extra_blbits, 0, 19, 7);
internal short[] static_tree;
internal int[] extra_bits;
internal int extra_base;
internal int elems;
internal int max_length;
internal StaticTree(short[] static_tree, int[] extra_bits, int extra_base, int elems, int max_length)
{
this.static_tree = static_tree;
this.extra_bits = extra_bits;
this.extra_base = extra_base;
this.elems = elems;
this.max_length = max_length;
}
}

View File

@@ -0,0 +1,359 @@
using System;
namespace Org.BouncyCastle.Utilities.Zlib;
internal sealed class Tree
{
private const int MAX_BITS = 15;
private const int BL_CODES = 19;
private const int D_CODES = 30;
private const int LITERALS = 256;
private const int LENGTH_CODES = 29;
private const int L_CODES = 286;
private const int HEAP_SIZE = 573;
internal const int MAX_BL_BITS = 7;
internal const int END_BLOCK = 256;
internal const int REP_3_6 = 16;
internal const int REPZ_3_10 = 17;
internal const int REPZ_11_138 = 18;
internal const int Buf_size = 16;
internal const int DIST_CODE_LEN = 512;
internal static readonly int[] extra_lbits = new int[29]
{
0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4, 5, 5, 5, 5, 0
};
internal static readonly int[] extra_dbits = new int[30]
{
0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
9, 9, 10, 10, 11, 11, 12, 12, 13, 13
};
internal static readonly int[] extra_blbits = new int[19]
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 2, 3, 7
};
internal static readonly byte[] bl_order = new byte[19]
{
16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
11, 4, 12, 3, 13, 2, 14, 1, 15
};
internal static readonly byte[] _dist_code = new byte[512]
{
0, 1, 2, 3, 4, 4, 5, 5, 6, 6,
6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
18, 18, 19, 19, 20, 20, 20, 20, 21, 21,
21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29
};
internal static readonly byte[] _length_code = new byte[256]
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
13, 13, 13, 13, 14, 14, 14, 14, 15, 15,
15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
17, 17, 17, 17, 17, 17, 17, 17, 18, 18,
18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
19, 19, 19, 19, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 28
};
internal static readonly int[] base_length = new int[29]
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 10,
12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
64, 80, 96, 112, 128, 160, 192, 224, 0
};
internal static readonly int[] base_dist = new int[30]
{
0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
};
internal short[] dyn_tree;
internal int max_code;
internal StaticTree stat_desc;
internal static int d_code(int dist)
{
if (dist >= 256)
{
return _dist_code[256 + (dist >> 7)];
}
return _dist_code[dist];
}
internal void gen_bitlen(Deflate s)
{
short[] array = dyn_tree;
short[] static_tree = stat_desc.static_tree;
int[] extra_bits = stat_desc.extra_bits;
int extra_base = stat_desc.extra_base;
int max_length = stat_desc.max_length;
int num = 0;
for (int i = 0; i <= 15; i++)
{
s.bl_count[i] = 0;
}
array[s.heap[s.heap_max] * 2 + 1] = 0;
int j;
for (j = s.heap_max + 1; j < 573; j++)
{
int num2 = s.heap[j];
int i = array[array[num2 * 2 + 1] * 2 + 1] + 1;
if (i > max_length)
{
i = max_length;
num++;
}
array[num2 * 2 + 1] = (short)i;
if (num2 <= max_code)
{
short[] bl_count;
short[] array2 = (bl_count = s.bl_count);
int num3 = i;
nint num4 = num3;
array2[num3] = (short)(bl_count[num4] + 1);
int num5 = 0;
if (num2 >= extra_base)
{
num5 = extra_bits[num2 - extra_base];
}
short num6 = array[num2 * 2];
s.opt_len += num6 * (i + num5);
if (static_tree != null)
{
s.static_len += num6 * (static_tree[num2 * 2 + 1] + num5);
}
}
}
if (num == 0)
{
return;
}
do
{
int i = max_length - 1;
while (s.bl_count[i] == 0)
{
i--;
}
short[] bl_count;
short[] array3 = (bl_count = s.bl_count);
int num7 = i;
nint num4 = num7;
array3[num7] = (short)(bl_count[num4] - 1);
short[] array4 = (bl_count = s.bl_count);
int num8 = i + 1;
num4 = num8;
array4[num8] = (short)(bl_count[num4] + 2);
short[] array5 = (bl_count = s.bl_count);
num4 = max_length;
array5[max_length] = (short)(bl_count[num4] - 1);
num -= 2;
}
while (num > 0);
for (int i = max_length; i != 0; i--)
{
int num2 = s.bl_count[i];
while (num2 != 0)
{
int num9 = s.heap[--j];
if (num9 <= max_code)
{
if (array[num9 * 2 + 1] != i)
{
s.opt_len += (int)(((long)i - (long)array[num9 * 2 + 1]) * array[num9 * 2]);
array[num9 * 2 + 1] = (short)i;
}
num2--;
}
}
}
}
internal void build_tree(Deflate s)
{
short[] array = dyn_tree;
short[] static_tree = stat_desc.static_tree;
int elems = stat_desc.elems;
int num = -1;
s.heap_len = 0;
s.heap_max = 573;
for (int i = 0; i < elems; i++)
{
if (array[i * 2] != 0)
{
num = (s.heap[++s.heap_len] = i);
s.depth[i] = 0;
}
else
{
array[i * 2 + 1] = 0;
}
}
int num2;
while (s.heap_len < 2)
{
num2 = (s.heap[++s.heap_len] = ((num < 2) ? (++num) : 0));
array[num2 * 2] = 1;
s.depth[num2] = 0;
s.opt_len--;
if (static_tree != null)
{
s.static_len -= static_tree[num2 * 2 + 1];
}
}
max_code = num;
for (int i = s.heap_len / 2; i >= 1; i--)
{
s.pqdownheap(array, i);
}
num2 = elems;
do
{
int i = s.heap[1];
s.heap[1] = s.heap[s.heap_len--];
s.pqdownheap(array, 1);
int num3 = s.heap[1];
s.heap[--s.heap_max] = i;
s.heap[--s.heap_max] = num3;
array[num2 * 2] = (short)(array[i * 2] + array[num3 * 2]);
s.depth[num2] = (byte)(System.Math.Max(s.depth[i], s.depth[num3]) + 1);
array[i * 2 + 1] = (array[num3 * 2 + 1] = (short)num2);
s.heap[1] = num2++;
s.pqdownheap(array, 1);
}
while (s.heap_len >= 2);
s.heap[--s.heap_max] = s.heap[1];
gen_bitlen(s);
gen_codes(array, num, s.bl_count);
}
internal static void gen_codes(short[] tree, int max_code, short[] bl_count)
{
short[] array = new short[16];
short num = 0;
for (int i = 1; i <= 15; i++)
{
num = (array[i] = (short)(num + bl_count[i - 1] << 1));
}
for (int j = 0; j <= max_code; j++)
{
int num2 = tree[j * 2 + 1];
if (num2 != 0)
{
int num3 = j * 2;
short[] array3;
short[] array2 = (array3 = array);
nint num4 = num2;
short code;
array2[num2] = (short)((code = array3[num4]) + 1);
tree[num3] = (short)bi_reverse(code, num2);
}
}
}
internal static int bi_reverse(int code, int len)
{
int num = 0;
do
{
num |= code & 1;
code >>= 1;
num <<= 1;
}
while (--len > 0);
return num >> 1;
}
}

View File

@@ -0,0 +1,155 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.Zlib;
[Obsolete("Use 'ZOutputStream' instead")]
public class ZDeflaterOutputStream : Stream
{
private const int BUFSIZE = 4192;
protected ZStream z = new ZStream();
protected int flushLevel = 0;
protected byte[] buf = new byte[4192];
private byte[] buf1 = new byte[1];
protected Stream outp;
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length => 0L;
public override long Position
{
get
{
return 0L;
}
set
{
}
}
public ZDeflaterOutputStream(Stream outp)
: this(outp, 6, nowrap: false)
{
}
public ZDeflaterOutputStream(Stream outp, int level)
: this(outp, level, nowrap: false)
{
}
public ZDeflaterOutputStream(Stream outp, int level, bool nowrap)
{
this.outp = outp;
z.deflateInit(level, nowrap);
}
public override void Write(byte[] b, int off, int len)
{
if (len == 0)
{
return;
}
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 = 4192;
if (z.deflate(flushLevel) != 0)
{
throw new IOException("deflating: " + z.msg);
}
if (z.avail_out < 4192)
{
outp.Write(buf, 0, 4192 - z.avail_out);
}
}
while (z.avail_in > 0 || z.avail_out == 0);
}
public override long Seek(long offset, SeekOrigin origin)
{
return 0L;
}
public override void SetLength(long value)
{
}
public override int Read(byte[] buffer, int offset, int count)
{
return 0;
}
public override void Flush()
{
outp.Flush();
}
public override void WriteByte(byte b)
{
buf1[0] = b;
Write(buf1, 0, 1);
}
public void Finish()
{
do
{
z.next_out = buf;
z.next_out_index = 0;
z.avail_out = 4192;
int num = z.deflate(4);
if (num != 1 && num != 0)
{
throw new IOException("deflating: " + z.msg);
}
if (4192 - z.avail_out > 0)
{
outp.Write(buf, 0, 4192 - z.avail_out);
}
}
while (z.avail_in > 0 || z.avail_out == 0);
Flush();
}
public void End()
{
if (z != null)
{
z.deflateEnd();
z.free();
z = null;
}
}
public override void Close()
{
try
{
Finish();
}
catch (IOException)
{
}
finally
{
End();
Platform.Dispose(outp);
outp = null;
}
base.Close();
}
}

View File

@@ -0,0 +1,132 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.Zlib;
[Obsolete("Use 'ZInputStream' instead")]
public class ZInflaterInputStream : Stream
{
private const int BUFSIZE = 4192;
protected ZStream z = new ZStream();
protected int flushLevel = 0;
protected byte[] buf = new byte[4192];
private byte[] buf1 = new byte[1];
protected Stream inp = null;
private bool nomoreinput = false;
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => 0L;
public override long Position
{
get
{
return 0L;
}
set
{
}
}
public ZInflaterInputStream(Stream inp)
: this(inp, nowrap: false)
{
}
public ZInflaterInputStream(Stream inp, bool nowrap)
{
this.inp = inp;
z.inflateInit(nowrap);
z.next_in = buf;
z.next_in_index = 0;
z.avail_in = 0;
}
public override void Write(byte[] b, int off, int len)
{
}
public override long Seek(long offset, SeekOrigin origin)
{
return 0L;
}
public override void SetLength(long value)
{
}
public override int Read(byte[] b, int off, int len)
{
if (len == 0)
{
return 0;
}
z.next_out = b;
z.next_out_index = off;
z.avail_out = len;
int num;
do
{
if (z.avail_in == 0 && !nomoreinput)
{
z.next_in_index = 0;
z.avail_in = inp.Read(buf, 0, 4192);
if (z.avail_in <= 0)
{
z.avail_in = 0;
nomoreinput = true;
}
}
num = z.inflate(flushLevel);
if (nomoreinput && num == -5)
{
return 0;
}
if (num != 0 && num != 1)
{
throw new IOException("inflating: " + z.msg);
}
if ((nomoreinput || num == 1) && z.avail_out == len)
{
return 0;
}
}
while (z.avail_out == len && num == 0);
return len - z.avail_out;
}
public override void Flush()
{
inp.Flush();
}
public override void WriteByte(byte b)
{
}
public override void Close()
{
Platform.Dispose(inp);
base.Close();
}
public override int ReadByte()
{
if (Read(buf1, 0, 1) <= 0)
{
return -1;
}
return buf1[0] & 0xFF;
}
}

View File

@@ -0,0 +1,196 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.Zlib;
public class ZInputStream : Stream
{
private const int BufferSize = 512;
protected ZStream z;
protected int flushLevel = 0;
protected byte[] buf = new byte[512];
protected byte[] buf1 = new byte[1];
protected bool compress;
protected Stream input;
protected bool closed;
private bool nomoreinput = false;
public sealed override bool CanRead => !closed;
public sealed override bool CanSeek => false;
public sealed override bool CanWrite => false;
public virtual int FlushMode
{
get
{
return flushLevel;
}
set
{
flushLevel = value;
}
}
public sealed override long Length
{
get
{
throw new NotSupportedException();
}
}
public sealed override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
public virtual long TotalIn => z.total_in;
public virtual long TotalOut => z.total_out;
private static ZStream GetDefaultZStream(bool nowrap)
{
ZStream zStream = new ZStream();
zStream.inflateInit(nowrap);
return zStream;
}
public ZInputStream(Stream input)
: this(input, nowrap: false)
{
}
public ZInputStream(Stream input, bool nowrap)
: this(input, GetDefaultZStream(nowrap))
{
}
public ZInputStream(Stream input, ZStream z)
{
if (z == null)
{
z = new ZStream();
}
if (z.istate == null && z.dstate == null)
{
z.inflateInit();
}
this.input = input;
compress = z.istate == null;
this.z = z;
this.z.next_in = buf;
this.z.next_in_index = 0;
this.z.avail_in = 0;
}
public ZInputStream(Stream input, int level)
: this(input, level, nowrap: false)
{
}
public ZInputStream(Stream input, int level, bool nowrap)
{
this.input = input;
compress = true;
z = new ZStream();
z.deflateInit(level, nowrap);
z.next_in = buf;
z.next_in_index = 0;
z.avail_in = 0;
}
public override void Close()
{
if (!closed)
{
closed = true;
Platform.Dispose(input);
base.Close();
}
}
public sealed override void Flush()
{
}
public override int Read(byte[] b, int off, int len)
{
if (len == 0)
{
return 0;
}
z.next_out = b;
z.next_out_index = off;
z.avail_out = len;
int num;
do
{
if (z.avail_in == 0 && !nomoreinput)
{
z.next_in_index = 0;
z.avail_in = input.Read(buf, 0, buf.Length);
if (z.avail_in <= 0)
{
z.avail_in = 0;
nomoreinput = true;
}
}
num = (compress ? z.deflate(flushLevel) : z.inflate(flushLevel));
if (nomoreinput && num == -5)
{
return 0;
}
if (num != 0 && num != 1)
{
throw new IOException((compress ? "de" : "in") + "flating: " + z.msg);
}
if ((nomoreinput || num == 1) && z.avail_out == len)
{
return 0;
}
}
while (z.avail_out == len && num == 0);
return len - z.avail_out;
}
public override int ReadByte()
{
if (Read(buf1, 0, 1) <= 0)
{
return -1;
}
return buf1[0];
}
public sealed override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public sealed override void SetLength(long value)
{
throw new NotSupportedException();
}
public sealed override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
}

View File

@@ -0,0 +1,225 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.Zlib;
public class ZOutputStream : Stream
{
private const int BufferSize = 512;
protected ZStream z;
protected int flushLevel = 0;
protected byte[] buf = new byte[512];
protected byte[] buf1 = new byte[1];
protected bool compress;
protected Stream output;
protected bool closed;
public sealed override bool CanRead => false;
public sealed override bool CanSeek => false;
public sealed override bool CanWrite => !closed;
public virtual int FlushMode
{
get
{
return flushLevel;
}
set
{
flushLevel = value;
}
}
public sealed override long Length
{
get
{
throw new NotSupportedException();
}
}
public sealed override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
public virtual long TotalIn => z.total_in;
public virtual long TotalOut => z.total_out;
private static ZStream GetDefaultZStream(bool nowrap)
{
ZStream zStream = new ZStream();
zStream.inflateInit(nowrap);
return zStream;
}
public ZOutputStream(Stream output)
: this(output, nowrap: false)
{
}
public ZOutputStream(Stream output, bool nowrap)
: this(output, GetDefaultZStream(nowrap))
{
}
public ZOutputStream(Stream output, ZStream z)
{
if (z == null)
{
z = new ZStream();
}
if (z.istate == null && z.dstate == null)
{
z.inflateInit();
}
this.output = output;
compress = z.istate == null;
this.z = z;
}
public ZOutputStream(Stream output, int level)
: this(output, level, nowrap: false)
{
}
public ZOutputStream(Stream output, int level, bool nowrap)
{
this.output = output;
compress = true;
z = new ZStream();
z.deflateInit(level, nowrap);
}
public override void Close()
{
if (!closed)
{
DoClose();
base.Close();
}
}
private void DoClose()
{
try
{
Finish();
}
catch (IOException)
{
}
finally
{
closed = true;
End();
Platform.Dispose(output);
output = null;
}
}
public virtual void End()
{
if (z != null)
{
if (compress)
{
z.deflateEnd();
}
else
{
z.inflateEnd();
}
z.free();
z = null;
}
}
public virtual void Finish()
{
do
{
z.next_out = buf;
z.next_out_index = 0;
z.avail_out = buf.Length;
int num = (compress ? z.deflate(4) : z.inflate(4));
if (num != 1 && num != 0)
{
throw new IOException((compress ? "de" : "in") + "flating: " + z.msg);
}
int num2 = buf.Length - z.avail_out;
if (num2 > 0)
{
output.Write(buf, 0, num2);
}
}
while (z.avail_in > 0 || z.avail_out == 0);
Flush();
}
public override void Flush()
{
output.Flush();
}
public sealed override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public sealed override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public sealed override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] b, int off, int len)
{
if (len == 0)
{
return;
}
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 = buf.Length;
if ((compress ? z.deflate(flushLevel) : z.inflate(flushLevel)) != 0)
{
throw new IOException((compress ? "de" : "in") + "flating: " + z.msg);
}
output.Write(buf, 0, buf.Length - z.avail_out);
}
while (z.avail_in > 0 || z.avail_out == 0);
}
public override void WriteByte(byte b)
{
buf1[0] = b;
Write(buf1, 0, 1);
}
}

View File

@@ -0,0 +1,243 @@
using System;
namespace Org.BouncyCastle.Utilities.Zlib;
public sealed class ZStream
{
private const int MAX_WBITS = 15;
private const int DEF_WBITS = 15;
private const int Z_NO_FLUSH = 0;
private const int Z_PARTIAL_FLUSH = 1;
private const int Z_SYNC_FLUSH = 2;
private const int Z_FULL_FLUSH = 3;
private const int Z_FINISH = 4;
private const int MAX_MEM_LEVEL = 9;
private const int Z_OK = 0;
private const int Z_STREAM_END = 1;
private const int Z_NEED_DICT = 2;
private const int Z_ERRNO = -1;
private const int Z_STREAM_ERROR = -2;
private const int Z_DATA_ERROR = -3;
private const int Z_MEM_ERROR = -4;
private const int Z_BUF_ERROR = -5;
private const int Z_VERSION_ERROR = -6;
public byte[] next_in;
public int next_in_index;
public int avail_in;
public long total_in;
public byte[] next_out;
public int next_out_index;
public int avail_out;
public long total_out;
public string msg;
internal Deflate dstate;
internal Inflate istate;
internal int data_type;
public long adler;
internal Adler32 _adler = new Adler32();
public int inflateInit()
{
return inflateInit(15);
}
public int inflateInit(bool nowrap)
{
return inflateInit(15, nowrap);
}
public int inflateInit(int w)
{
return inflateInit(w, nowrap: false);
}
public int inflateInit(int w, bool nowrap)
{
istate = new Inflate();
return istate.inflateInit(this, nowrap ? (-w) : w);
}
public int inflate(int f)
{
if (istate == null)
{
return -2;
}
return istate.inflate(this, f);
}
public int inflateEnd()
{
if (istate == null)
{
return -2;
}
int result = istate.inflateEnd(this);
istate = null;
return result;
}
public int inflateSync()
{
if (istate == null)
{
return -2;
}
return istate.inflateSync(this);
}
public int inflateSetDictionary(byte[] dictionary, int dictLength)
{
if (istate == null)
{
return -2;
}
return istate.inflateSetDictionary(this, dictionary, dictLength);
}
public int deflateInit(int level)
{
return deflateInit(level, 15);
}
public int deflateInit(int level, bool nowrap)
{
return deflateInit(level, 15, nowrap);
}
public int deflateInit(int level, int bits)
{
return deflateInit(level, bits, nowrap: false);
}
public int deflateInit(int level, int bits, bool nowrap)
{
dstate = new Deflate();
return dstate.deflateInit(this, level, nowrap ? (-bits) : bits);
}
public int deflate(int flush)
{
if (dstate == null)
{
return -2;
}
return dstate.deflate(this, flush);
}
public int deflateEnd()
{
if (dstate == null)
{
return -2;
}
int result = dstate.deflateEnd();
dstate = null;
return result;
}
public int deflateParams(int level, int strategy)
{
if (dstate == null)
{
return -2;
}
return dstate.deflateParams(this, level, strategy);
}
public int deflateSetDictionary(byte[] dictionary, int dictLength)
{
if (dstate == null)
{
return -2;
}
return dstate.deflateSetDictionary(this, dictionary, dictLength);
}
internal void flush_pending()
{
int pending = dstate.pending;
if (pending > avail_out)
{
pending = avail_out;
}
if (pending != 0)
{
if (dstate.pending_buf.Length > dstate.pending_out && next_out.Length > next_out_index && dstate.pending_buf.Length >= dstate.pending_out + pending)
{
_ = next_out.Length;
_ = next_out_index + pending;
}
Array.Copy(dstate.pending_buf, dstate.pending_out, next_out, next_out_index, pending);
next_out_index += pending;
dstate.pending_out += pending;
total_out += pending;
avail_out -= pending;
dstate.pending -= pending;
if (dstate.pending == 0)
{
dstate.pending_out = 0;
}
}
}
internal int read_buf(byte[] buf, int start, int size)
{
int num = avail_in;
if (num > size)
{
num = size;
}
if (num == 0)
{
return 0;
}
avail_in -= num;
if (dstate.noheader == 0)
{
adler = _adler.adler32(adler, next_in, next_in_index, num);
}
Array.Copy(next_in, next_in_index, buf, start, num);
next_in_index += num;
total_in += num;
return num;
}
public void free()
{
next_in = null;
next_out = null;
msg = null;
_adler = null;
}
}