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,724 @@
using System;
using System.Text;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Utilities;
public abstract class Arrays
{
public static readonly byte[] EmptyBytes = new byte[0];
public static readonly int[] EmptyInts = new int[0];
public static bool AreAllZeroes(byte[] buf, int off, int len)
{
uint num = 0u;
for (int i = 0; i < len; i++)
{
num |= buf[off + i];
}
return num == 0;
}
public static bool AreEqual(bool[] a, bool[] b)
{
if (a == b)
{
return true;
}
if (a == null || b == null)
{
return false;
}
return HaveSameContents(a, b);
}
public static bool AreEqual(char[] a, char[] b)
{
if (a == b)
{
return true;
}
if (a == null || b == null)
{
return false;
}
return HaveSameContents(a, b);
}
public static bool AreEqual(byte[] a, byte[] b)
{
if (a == b)
{
return true;
}
if (a == null || b == null)
{
return false;
}
return HaveSameContents(a, b);
}
[Obsolete("Use 'AreEqual' method instead")]
public static bool AreSame(byte[] a, byte[] b)
{
return AreEqual(a, b);
}
public static bool ConstantTimeAreEqual(byte[] a, byte[] b)
{
int num = a.Length;
if (num != b.Length)
{
return false;
}
int num2 = 0;
while (num != 0)
{
num--;
num2 |= a[num] ^ b[num];
}
return num2 == 0;
}
public static bool AreEqual(int[] a, int[] b)
{
if (a == b)
{
return true;
}
if (a == null || b == null)
{
return false;
}
return HaveSameContents(a, b);
}
[CLSCompliant(false)]
public static bool AreEqual(uint[] a, uint[] b)
{
if (a == b)
{
return true;
}
if (a == null || b == null)
{
return false;
}
return HaveSameContents(a, b);
}
private static bool HaveSameContents(bool[] a, bool[] b)
{
int num = a.Length;
if (num != b.Length)
{
return false;
}
while (num != 0)
{
num--;
if (a[num] != b[num])
{
return false;
}
}
return true;
}
private static bool HaveSameContents(char[] a, char[] b)
{
int num = a.Length;
if (num != b.Length)
{
return false;
}
while (num != 0)
{
num--;
if (a[num] != b[num])
{
return false;
}
}
return true;
}
private static bool HaveSameContents(byte[] a, byte[] b)
{
int num = a.Length;
if (num != b.Length)
{
return false;
}
while (num != 0)
{
num--;
if (a[num] != b[num])
{
return false;
}
}
return true;
}
private static bool HaveSameContents(int[] a, int[] b)
{
int num = a.Length;
if (num != b.Length)
{
return false;
}
while (num != 0)
{
num--;
if (a[num] != b[num])
{
return false;
}
}
return true;
}
private static bool HaveSameContents(uint[] a, uint[] b)
{
int num = a.Length;
if (num != b.Length)
{
return false;
}
while (num != 0)
{
num--;
if (a[num] != b[num])
{
return false;
}
}
return true;
}
public static string ToString(object[] a)
{
StringBuilder stringBuilder = new StringBuilder(91);
if (a.Length > 0)
{
stringBuilder.Append(a[0]);
for (int i = 1; i < a.Length; i++)
{
stringBuilder.Append(", ").Append(a[i]);
}
}
stringBuilder.Append(']');
return stringBuilder.ToString();
}
public static int GetHashCode(byte[] data)
{
if (data == null)
{
return 0;
}
int num = data.Length;
int num2 = num + 1;
while (--num >= 0)
{
num2 *= 257;
num2 ^= data[num];
}
return num2;
}
public static int GetHashCode(byte[] data, int off, int len)
{
if (data == null)
{
return 0;
}
int num = len;
int num2 = num + 1;
while (--num >= 0)
{
num2 *= 257;
num2 ^= data[off + num];
}
return num2;
}
public static int GetHashCode(int[] data)
{
if (data == null)
{
return 0;
}
int num = data.Length;
int num2 = num + 1;
while (--num >= 0)
{
num2 *= 257;
num2 ^= data[num];
}
return num2;
}
public static int GetHashCode(int[] data, int off, int len)
{
if (data == null)
{
return 0;
}
int num = len;
int num2 = num + 1;
while (--num >= 0)
{
num2 *= 257;
num2 ^= data[off + num];
}
return num2;
}
[CLSCompliant(false)]
public static int GetHashCode(uint[] data)
{
if (data == null)
{
return 0;
}
int num = data.Length;
int num2 = num + 1;
while (--num >= 0)
{
num2 *= 257;
num2 ^= (int)data[num];
}
return num2;
}
[CLSCompliant(false)]
public static int GetHashCode(uint[] data, int off, int len)
{
if (data == null)
{
return 0;
}
int num = len;
int num2 = num + 1;
while (--num >= 0)
{
num2 *= 257;
num2 ^= (int)data[off + num];
}
return num2;
}
[CLSCompliant(false)]
public static int GetHashCode(ulong[] data)
{
if (data == null)
{
return 0;
}
int num = data.Length;
int num2 = num + 1;
while (--num >= 0)
{
ulong num3 = data[num];
num2 *= 257;
num2 ^= (int)num3;
num2 *= 257;
num2 ^= (int)(num3 >> 32);
}
return num2;
}
[CLSCompliant(false)]
public static int GetHashCode(ulong[] data, int off, int len)
{
if (data == null)
{
return 0;
}
int num = len;
int num2 = num + 1;
while (--num >= 0)
{
ulong num3 = data[off + num];
num2 *= 257;
num2 ^= (int)num3;
num2 *= 257;
num2 ^= (int)(num3 >> 32);
}
return num2;
}
public static byte[] Clone(byte[] data)
{
if (data != null)
{
return (byte[])data.Clone();
}
return null;
}
public static byte[] Clone(byte[] data, byte[] existing)
{
if (data == null)
{
return null;
}
if (existing == null || existing.Length != data.Length)
{
return Clone(data);
}
Array.Copy(data, 0, existing, 0, existing.Length);
return existing;
}
public static int[] Clone(int[] data)
{
if (data != null)
{
return (int[])data.Clone();
}
return null;
}
internal static uint[] Clone(uint[] data)
{
if (data != null)
{
return (uint[])data.Clone();
}
return null;
}
public static long[] Clone(long[] data)
{
if (data != null)
{
return (long[])data.Clone();
}
return null;
}
[CLSCompliant(false)]
public static ulong[] Clone(ulong[] data)
{
if (data != null)
{
return (ulong[])data.Clone();
}
return null;
}
[CLSCompliant(false)]
public static ulong[] Clone(ulong[] data, ulong[] existing)
{
if (data == null)
{
return null;
}
if (existing == null || existing.Length != data.Length)
{
return Clone(data);
}
Array.Copy(data, 0, existing, 0, existing.Length);
return existing;
}
public static bool Contains(byte[] a, byte n)
{
for (int i = 0; i < a.Length; i++)
{
if (a[i] == n)
{
return true;
}
}
return false;
}
public static bool Contains(short[] a, short n)
{
for (int i = 0; i < a.Length; i++)
{
if (a[i] == n)
{
return true;
}
}
return false;
}
public static bool Contains(int[] a, int n)
{
for (int i = 0; i < a.Length; i++)
{
if (a[i] == n)
{
return true;
}
}
return false;
}
public static void Fill(byte[] buf, byte b)
{
int num = buf.Length;
while (num > 0)
{
buf[--num] = b;
}
}
public static void Fill(byte[] buf, int from, int to, byte b)
{
for (int i = from; i < to; i++)
{
buf[i] = b;
}
}
public static byte[] CopyOf(byte[] data, int newLength)
{
byte[] array = new byte[newLength];
Array.Copy(data, 0, array, 0, System.Math.Min(newLength, data.Length));
return array;
}
public static char[] CopyOf(char[] data, int newLength)
{
char[] array = new char[newLength];
Array.Copy(data, 0, array, 0, System.Math.Min(newLength, data.Length));
return array;
}
public static int[] CopyOf(int[] data, int newLength)
{
int[] array = new int[newLength];
Array.Copy(data, 0, array, 0, System.Math.Min(newLength, data.Length));
return array;
}
public static long[] CopyOf(long[] data, int newLength)
{
long[] array = new long[newLength];
Array.Copy(data, 0, array, 0, System.Math.Min(newLength, data.Length));
return array;
}
public static BigInteger[] CopyOf(BigInteger[] data, int newLength)
{
BigInteger[] array = new BigInteger[newLength];
Array.Copy(data, 0, array, 0, System.Math.Min(newLength, data.Length));
return array;
}
public static byte[] CopyOfRange(byte[] data, int from, int to)
{
int length = GetLength(from, to);
byte[] array = new byte[length];
Array.Copy(data, from, array, 0, System.Math.Min(length, data.Length - from));
return array;
}
public static int[] CopyOfRange(int[] data, int from, int to)
{
int length = GetLength(from, to);
int[] array = new int[length];
Array.Copy(data, from, array, 0, System.Math.Min(length, data.Length - from));
return array;
}
public static long[] CopyOfRange(long[] data, int from, int to)
{
int length = GetLength(from, to);
long[] array = new long[length];
Array.Copy(data, from, array, 0, System.Math.Min(length, data.Length - from));
return array;
}
public static BigInteger[] CopyOfRange(BigInteger[] data, int from, int to)
{
int length = GetLength(from, to);
BigInteger[] array = new BigInteger[length];
Array.Copy(data, from, array, 0, System.Math.Min(length, data.Length - from));
return array;
}
private static int GetLength(int from, int to)
{
int num = to - from;
if (num < 0)
{
throw new ArgumentException(from + " > " + to);
}
return num;
}
public static byte[] Append(byte[] a, byte b)
{
if (a == null)
{
return new byte[1] { b };
}
int num = a.Length;
byte[] array = new byte[num + 1];
Array.Copy(a, 0, array, 0, num);
array[num] = b;
return array;
}
public static short[] Append(short[] a, short b)
{
if (a == null)
{
return new short[1] { b };
}
int num = a.Length;
short[] array = new short[num + 1];
Array.Copy(a, 0, array, 0, num);
array[num] = b;
return array;
}
public static int[] Append(int[] a, int b)
{
if (a == null)
{
return new int[1] { b };
}
int num = a.Length;
int[] array = new int[num + 1];
Array.Copy(a, 0, array, 0, num);
array[num] = b;
return array;
}
public static byte[] Concatenate(byte[] a, byte[] b)
{
if (a == null)
{
return Clone(b);
}
if (b == null)
{
return Clone(a);
}
byte[] array = new byte[a.Length + b.Length];
Array.Copy(a, 0, array, 0, a.Length);
Array.Copy(b, 0, array, a.Length, b.Length);
return array;
}
public static byte[] ConcatenateAll(params byte[][] vs)
{
byte[][] array = new byte[vs.Length][];
int num = 0;
int num2 = 0;
foreach (byte[] array2 in vs)
{
if (array2 != null)
{
array[num++] = array2;
num2 += array2.Length;
}
}
byte[] array3 = new byte[num2];
int num3 = 0;
for (int j = 0; j < num; j++)
{
byte[] array4 = array[j];
Array.Copy(array4, 0, array3, num3, array4.Length);
num3 += array4.Length;
}
return array3;
}
public static int[] Concatenate(int[] a, int[] b)
{
if (a == null)
{
return Clone(b);
}
if (b == null)
{
return Clone(a);
}
int[] array = new int[a.Length + b.Length];
Array.Copy(a, 0, array, 0, a.Length);
Array.Copy(b, 0, array, a.Length, b.Length);
return array;
}
public static byte[] Prepend(byte[] a, byte b)
{
if (a == null)
{
return new byte[1] { b };
}
int num = a.Length;
byte[] array = new byte[num + 1];
Array.Copy(a, 0, array, 1, num);
array[0] = b;
return array;
}
public static short[] Prepend(short[] a, short b)
{
if (a == null)
{
return new short[1] { b };
}
int num = a.Length;
short[] array = new short[num + 1];
Array.Copy(a, 0, array, 1, num);
array[0] = b;
return array;
}
public static int[] Prepend(int[] a, int b)
{
if (a == null)
{
return new int[1] { b };
}
int num = a.Length;
int[] array = new int[num + 1];
Array.Copy(a, 0, array, 1, num);
array[0] = b;
return array;
}
public static byte[] Reverse(byte[] a)
{
if (a == null)
{
return null;
}
int num = 0;
int num2 = a.Length;
byte[] array = new byte[num2];
while (--num2 >= 0)
{
array[num2] = a[num++];
}
return array;
}
public static int[] Reverse(int[] a)
{
if (a == null)
{
return null;
}
int num = 0;
int num2 = a.Length;
int[] array = new int[num2];
while (--num2 >= 0)
{
array[num2] = a[num++];
}
return array;
}
}

View File

@@ -0,0 +1,67 @@
using System;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Utilities;
public abstract class BigIntegers
{
private const int MaxIterations = 1000;
public static byte[] AsUnsignedByteArray(BigInteger n)
{
return n.ToByteArrayUnsigned();
}
public static byte[] AsUnsignedByteArray(int length, BigInteger n)
{
byte[] array = n.ToByteArrayUnsigned();
if (array.Length > length)
{
throw new ArgumentException("standard length exceeded", "n");
}
if (array.Length == length)
{
return array;
}
byte[] array2 = new byte[length];
Array.Copy(array, 0, array2, array2.Length - array.Length, array.Length);
return array2;
}
public static BigInteger CreateRandomBigInteger(int bitLength, SecureRandom secureRandom)
{
return new BigInteger(bitLength, secureRandom);
}
public static BigInteger CreateRandomInRange(BigInteger min, BigInteger max, SecureRandom random)
{
int num = min.CompareTo(max);
if (num >= 0)
{
if (num > 0)
{
throw new ArgumentException("'min' may not be greater than 'max'");
}
return min;
}
if (min.BitLength > max.BitLength / 2)
{
return CreateRandomInRange(BigInteger.Zero, max.Subtract(min), random).Add(min);
}
for (int i = 0; i < 1000; i++)
{
BigInteger bigInteger = new BigInteger(max.BitLength, random);
if (bigInteger.CompareTo(min) >= 0 && bigInteger.CompareTo(max) <= 0)
{
return bigInteger;
}
}
return new BigInteger(max.Subtract(min).BitLength - 1, random).Add(min);
}
public static int GetUnsignedByteLength(BigInteger n)
{
return (n.BitLength + 7) / 8;
}
}

View File

@@ -0,0 +1,69 @@
using System;
using System.Collections;
using System.Text;
namespace Org.BouncyCastle.Utilities.Collections;
public abstract class CollectionUtilities
{
public static void AddRange(IList to, IEnumerable range)
{
foreach (object item in range)
{
to.Add(item);
}
}
public static bool CheckElementsAreOfType(IEnumerable e, Type t)
{
foreach (object item in e)
{
if (!t.IsInstanceOfType(item))
{
return false;
}
}
return true;
}
public static IDictionary ReadOnly(IDictionary d)
{
return new UnmodifiableDictionaryProxy(d);
}
public static IList ReadOnly(IList l)
{
return new UnmodifiableListProxy(l);
}
public static ISet ReadOnly(ISet s)
{
return new UnmodifiableSetProxy(s);
}
public static object RequireNext(IEnumerator e)
{
if (!e.MoveNext())
{
throw new InvalidOperationException();
}
return e.Current;
}
public static string ToString(IEnumerable c)
{
StringBuilder stringBuilder = new StringBuilder("[");
IEnumerator enumerator = c.GetEnumerator();
if (enumerator.MoveNext())
{
stringBuilder.Append(enumerator.Current.ToString());
while (enumerator.MoveNext())
{
stringBuilder.Append(", ");
stringBuilder.Append(enumerator.Current.ToString());
}
}
stringBuilder.Append(']');
return stringBuilder.ToString();
}
}

View File

@@ -0,0 +1,17 @@
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
public sealed class EmptyEnumerable : IEnumerable
{
public static readonly IEnumerable Instance = new EmptyEnumerable();
private EmptyEnumerable()
{
}
public IEnumerator GetEnumerator()
{
return EmptyEnumerator.Instance;
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
public sealed class EmptyEnumerator : IEnumerator
{
public static readonly IEnumerator Instance = new EmptyEnumerator();
public object Current
{
get
{
throw new InvalidOperationException("No elements");
}
}
private EmptyEnumerator()
{
}
public bool MoveNext()
{
return false;
}
public void Reset()
{
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
public sealed class EnumerableProxy : IEnumerable
{
private readonly IEnumerable inner;
public EnumerableProxy(IEnumerable inner)
{
if (inner == null)
{
throw new ArgumentNullException("inner");
}
this.inner = inner;
}
public IEnumerator GetEnumerator()
{
return inner.GetEnumerator();
}
}

View File

@@ -0,0 +1,79 @@
using System;
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
public class HashSet : ISet, ICollection, IEnumerable
{
private readonly IDictionary impl = Platform.CreateHashtable();
public virtual int Count => impl.Count;
public virtual bool IsEmpty => impl.Count == 0;
public virtual bool IsFixedSize => impl.IsFixedSize;
public virtual bool IsReadOnly => impl.IsReadOnly;
public virtual bool IsSynchronized => impl.IsSynchronized;
public virtual object SyncRoot => impl.SyncRoot;
public HashSet()
{
}
public HashSet(IEnumerable s)
{
foreach (object item in s)
{
Add(item);
}
}
public virtual void Add(object o)
{
impl[o] = null;
}
public virtual void AddAll(IEnumerable e)
{
foreach (object item in e)
{
Add(item);
}
}
public virtual void Clear()
{
impl.Clear();
}
public virtual bool Contains(object o)
{
return impl.Contains(o);
}
public virtual void CopyTo(Array array, int index)
{
impl.Keys.CopyTo(array, index);
}
public virtual IEnumerator GetEnumerator()
{
return impl.Keys.GetEnumerator();
}
public virtual void Remove(object o)
{
impl.Remove(o);
}
public virtual void RemoveAll(IEnumerable e)
{
foreach (object item in e)
{
Remove(item);
}
}
}

View File

@@ -0,0 +1,24 @@
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
public interface ISet : ICollection, IEnumerable
{
bool IsEmpty { get; }
bool IsFixedSize { get; }
bool IsReadOnly { get; }
void Add(object o);
void AddAll(IEnumerable e);
void Clear();
bool Contains(object o);
void Remove(object o);
void RemoveAll(IEnumerable e);
}

View File

@@ -0,0 +1,93 @@
using System;
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
public class LinkedDictionary : IDictionary, ICollection, IEnumerable
{
internal readonly IDictionary hash = Platform.CreateHashtable();
internal readonly IList keys = Platform.CreateArrayList();
public virtual int Count => hash.Count;
public virtual bool IsFixedSize => false;
public virtual bool IsReadOnly => false;
public virtual bool IsSynchronized => false;
public virtual object SyncRoot => false;
public virtual ICollection Keys => Platform.CreateArrayList(keys);
public virtual ICollection Values
{
get
{
IList list = Platform.CreateArrayList(keys.Count);
foreach (object key in keys)
{
list.Add(hash[key]);
}
return list;
}
}
public virtual object this[object k]
{
get
{
return hash[k];
}
set
{
if (!hash.Contains(k))
{
keys.Add(k);
}
hash[k] = value;
}
}
public virtual void Add(object k, object v)
{
hash.Add(k, v);
keys.Add(k);
}
public virtual void Clear()
{
hash.Clear();
keys.Clear();
}
public virtual bool Contains(object k)
{
return hash.Contains(k);
}
public virtual void CopyTo(Array array, int index)
{
foreach (object key in keys)
{
array.SetValue(hash[key], index++);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public virtual IDictionaryEnumerator GetEnumerator()
{
return new LinkedDictionaryEnumerator(this);
}
public virtual void Remove(object k)
{
hash.Remove(k);
keys.Remove(k);
}
}

View File

@@ -0,0 +1,57 @@
using System;
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
internal class LinkedDictionaryEnumerator : IDictionaryEnumerator, IEnumerator
{
private readonly LinkedDictionary parent;
private int pos = -1;
public virtual object Current => Entry;
public virtual DictionaryEntry Entry
{
get
{
object currentKey = CurrentKey;
return new DictionaryEntry(currentKey, parent.hash[currentKey]);
}
}
public virtual object Key => CurrentKey;
public virtual object Value => parent.hash[CurrentKey];
private object CurrentKey
{
get
{
if (pos < 0 || pos >= parent.keys.Count)
{
throw new InvalidOperationException();
}
return parent.keys[pos];
}
}
internal LinkedDictionaryEnumerator(LinkedDictionary parent)
{
this.parent = parent;
}
public virtual bool MoveNext()
{
if (pos >= parent.keys.Count)
{
return false;
}
return ++pos < parent.keys.Count;
}
public virtual void Reset()
{
pos = -1;
}
}

View File

@@ -0,0 +1,61 @@
using System;
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
public abstract class UnmodifiableDictionary : IDictionary, ICollection, IEnumerable
{
public abstract int Count { get; }
public abstract bool IsFixedSize { get; }
public virtual bool IsReadOnly => true;
public abstract bool IsSynchronized { get; }
public abstract object SyncRoot { get; }
public abstract ICollection Keys { get; }
public abstract ICollection Values { get; }
public virtual object this[object k]
{
get
{
return GetValue(k);
}
set
{
throw new NotSupportedException();
}
}
public virtual void Add(object k, object v)
{
throw new NotSupportedException();
}
public virtual void Clear()
{
throw new NotSupportedException();
}
public abstract bool Contains(object k);
public abstract void CopyTo(Array array, int index);
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public abstract IDictionaryEnumerator GetEnumerator();
public virtual void Remove(object k)
{
throw new NotSupportedException();
}
protected abstract object GetValue(object k);
}

View File

@@ -0,0 +1,46 @@
using System;
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
public class UnmodifiableDictionaryProxy : UnmodifiableDictionary
{
private readonly IDictionary d;
public override int Count => d.Count;
public override bool IsFixedSize => d.IsFixedSize;
public override bool IsSynchronized => d.IsSynchronized;
public override object SyncRoot => d.SyncRoot;
public override ICollection Keys => d.Keys;
public override ICollection Values => d.Values;
public UnmodifiableDictionaryProxy(IDictionary d)
{
this.d = d;
}
public override bool Contains(object k)
{
return d.Contains(k);
}
public override void CopyTo(Array array, int index)
{
d.CopyTo(array, index);
}
public override IDictionaryEnumerator GetEnumerator()
{
return d.GetEnumerator();
}
protected override object GetValue(object k)
{
return d[k];
}
}

View File

@@ -0,0 +1,64 @@
using System;
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
public abstract class UnmodifiableList : IList, ICollection, IEnumerable
{
public abstract int Count { get; }
public abstract bool IsFixedSize { get; }
public virtual bool IsReadOnly => true;
public abstract bool IsSynchronized { get; }
public abstract object SyncRoot { get; }
public virtual object this[int i]
{
get
{
return GetValue(i);
}
set
{
throw new NotSupportedException();
}
}
public virtual int Add(object o)
{
throw new NotSupportedException();
}
public virtual void Clear()
{
throw new NotSupportedException();
}
public abstract bool Contains(object o);
public abstract void CopyTo(Array array, int index);
public abstract IEnumerator GetEnumerator();
public abstract int IndexOf(object o);
public virtual void Insert(int i, object o)
{
throw new NotSupportedException();
}
public virtual void Remove(object o)
{
throw new NotSupportedException();
}
public virtual void RemoveAt(int i)
{
throw new NotSupportedException();
}
protected abstract object GetValue(int i);
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
public class UnmodifiableListProxy : UnmodifiableList
{
private readonly IList l;
public override int Count => l.Count;
public override bool IsFixedSize => l.IsFixedSize;
public override bool IsSynchronized => l.IsSynchronized;
public override object SyncRoot => l.SyncRoot;
public UnmodifiableListProxy(IList l)
{
this.l = l;
}
public override bool Contains(object o)
{
return l.Contains(o);
}
public override void CopyTo(Array array, int index)
{
l.CopyTo(array, index);
}
public override IEnumerator GetEnumerator()
{
return l.GetEnumerator();
}
public override int IndexOf(object o)
{
return l.IndexOf(o);
}
protected override object GetValue(int i)
{
return l[i];
}
}

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
public abstract class UnmodifiableSet : ISet, ICollection, IEnumerable
{
public abstract int Count { get; }
public abstract bool IsEmpty { get; }
public abstract bool IsFixedSize { get; }
public virtual bool IsReadOnly => true;
public abstract bool IsSynchronized { get; }
public abstract object SyncRoot { get; }
public virtual void Add(object o)
{
throw new NotSupportedException();
}
public virtual void AddAll(IEnumerable e)
{
throw new NotSupportedException();
}
public virtual void Clear()
{
throw new NotSupportedException();
}
public abstract bool Contains(object o);
public abstract void CopyTo(Array array, int index);
public abstract IEnumerator GetEnumerator();
public virtual void Remove(object o)
{
throw new NotSupportedException();
}
public virtual void RemoveAll(IEnumerable e)
{
throw new NotSupportedException();
}
}

View File

@@ -0,0 +1,39 @@
using System;
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections;
public class UnmodifiableSetProxy : UnmodifiableSet
{
private readonly ISet s;
public override int Count => s.Count;
public override bool IsEmpty => s.IsEmpty;
public override bool IsFixedSize => s.IsFixedSize;
public override bool IsSynchronized => s.IsSynchronized;
public override object SyncRoot => s.SyncRoot;
public UnmodifiableSetProxy(ISet s)
{
this.s = s;
}
public override bool Contains(object o)
{
return s.Contains(o);
}
public override void CopyTo(Array array, int index)
{
s.CopyTo(array, index);
}
public override IEnumerator GetEnumerator()
{
return s.GetEnumerator();
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Utilities.Date;
public sealed class DateTimeObject
{
private readonly DateTime dt;
public DateTime Value => dt;
public DateTimeObject(DateTime dt)
{
this.dt = dt;
}
public override string ToString()
{
DateTime dateTime = dt;
return dateTime.ToString();
}
}

View File

@@ -0,0 +1,35 @@
using System;
namespace Org.BouncyCastle.Utilities.Date;
public class DateTimeUtilities
{
public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1);
private DateTimeUtilities()
{
}
public static long DateTimeToUnixMs(DateTime dateTime)
{
if (dateTime.CompareTo((object?)UnixEpoch) < 0)
{
throw new ArgumentException("DateTime value may not be before the epoch", "dateTime");
}
long ticks = dateTime.Ticks;
DateTime unixEpoch = UnixEpoch;
return (ticks - unixEpoch.Ticks) / 10000;
}
public static DateTime UnixMsToDateTime(long unixMs)
{
long num = unixMs * 10000;
DateTime unixEpoch = UnixEpoch;
return new DateTime(num + unixEpoch.Ticks);
}
public static long CurrentUnixMs()
{
return DateTimeToUnixMs(DateTime.UtcNow);
}
}

View File

@@ -0,0 +1,64 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders;
public sealed class Base64
{
private Base64()
{
}
public static string ToBase64String(byte[] data)
{
return Convert.ToBase64String(data, 0, data.Length);
}
public static string ToBase64String(byte[] data, int off, int length)
{
return Convert.ToBase64String(data, off, length);
}
public static byte[] Encode(byte[] data)
{
return Encode(data, 0, data.Length);
}
public static byte[] Encode(byte[] data, int off, int length)
{
string s = Convert.ToBase64String(data, off, length);
return Strings.ToAsciiByteArray(s);
}
public static int Encode(byte[] data, Stream outStream)
{
byte[] array = Encode(data);
outStream.Write(array, 0, array.Length);
return array.Length;
}
public static int Encode(byte[] data, int off, int length, Stream outStream)
{
byte[] array = Encode(data, off, length);
outStream.Write(array, 0, array.Length);
return array.Length;
}
public static byte[] Decode(byte[] data)
{
string s = Strings.FromAsciiByteArray(data);
return Convert.FromBase64String(s);
}
public static byte[] Decode(string data)
{
return Convert.FromBase64String(data);
}
public static int Decode(string data, Stream outStream)
{
byte[] array = Decode(data);
outStream.Write(array, 0, array.Length);
return array.Length;
}
}

View File

@@ -0,0 +1,212 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders;
public class Base64Encoder : IEncoder
{
protected readonly byte[] encodingTable = new byte[64]
{
65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 97, 98, 99, 100,
101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 43, 47
};
protected byte padding = 61;
protected readonly byte[] decodingTable = new byte[128];
protected void InitialiseDecodingTable()
{
Arrays.Fill(decodingTable, byte.MaxValue);
for (int i = 0; i < encodingTable.Length; i++)
{
decodingTable[encodingTable[i]] = (byte)i;
}
}
public Base64Encoder()
{
InitialiseDecodingTable();
}
public int Encode(byte[] data, int off, int length, Stream outStream)
{
int num = length % 3;
int num2 = length - num;
for (int i = off; i < off + num2; i += 3)
{
int num3 = data[i] & 0xFF;
int num4 = data[i + 1] & 0xFF;
int num5 = data[i + 2] & 0xFF;
outStream.WriteByte(encodingTable[(num3 >>> 2) & 0x3F]);
outStream.WriteByte(encodingTable[((num3 << 4) | (num4 >>> 4)) & 0x3F]);
outStream.WriteByte(encodingTable[((num4 << 2) | (num5 >>> 6)) & 0x3F]);
outStream.WriteByte(encodingTable[num5 & 0x3F]);
}
switch (num)
{
case 1:
{
int num6 = data[off + num2] & 0xFF;
int num8 = (num6 >> 2) & 0x3F;
int num9 = (num6 << 4) & 0x3F;
outStream.WriteByte(encodingTable[num8]);
outStream.WriteByte(encodingTable[num9]);
outStream.WriteByte(padding);
outStream.WriteByte(padding);
break;
}
case 2:
{
int num6 = data[off + num2] & 0xFF;
int num7 = data[off + num2 + 1] & 0xFF;
int num8 = (num6 >> 2) & 0x3F;
int num9 = ((num6 << 4) | (num7 >> 4)) & 0x3F;
int num10 = (num7 << 2) & 0x3F;
outStream.WriteByte(encodingTable[num8]);
outStream.WriteByte(encodingTable[num9]);
outStream.WriteByte(encodingTable[num10]);
outStream.WriteByte(padding);
break;
}
}
return num2 / 3 * 4 + ((num != 0) ? 4 : 0);
}
private bool ignore(char c)
{
if (c != '\n' && c != '\r' && c != '\t')
{
return c == ' ';
}
return true;
}
public int Decode(byte[] data, int off, int length, Stream outStream)
{
int num = 0;
int num2 = off + length;
while (num2 > off && ignore((char)data[num2 - 1]))
{
num2--;
}
int i = off;
int num3 = num2 - 4;
for (i = nextI(data, i, num3); i < num3; i = nextI(data, i, num3))
{
byte b = decodingTable[data[i++]];
i = nextI(data, i, num3);
byte b2 = decodingTable[data[i++]];
i = nextI(data, i, num3);
byte b3 = decodingTable[data[i++]];
i = nextI(data, i, num3);
byte b4 = decodingTable[data[i++]];
if ((b | b2 | b3 | b4) >= 128)
{
throw new IOException("invalid characters encountered in base64 data");
}
outStream.WriteByte((byte)((b << 2) | (b2 >> 4)));
outStream.WriteByte((byte)((b2 << 4) | (b3 >> 2)));
outStream.WriteByte((byte)((b3 << 6) | b4));
num += 3;
}
return num + decodeLastBlock(outStream, (char)data[num2 - 4], (char)data[num2 - 3], (char)data[num2 - 2], (char)data[num2 - 1]);
}
private int nextI(byte[] data, int i, int finish)
{
while (i < finish && ignore((char)data[i]))
{
i++;
}
return i;
}
public int DecodeString(string data, Stream outStream)
{
int num = 0;
int num2 = data.Length;
while (num2 > 0 && ignore(data[num2 - 1]))
{
num2--;
}
int i = 0;
int num3 = num2 - 4;
for (i = nextI(data, i, num3); i < num3; i = nextI(data, i, num3))
{
byte b = decodingTable[(uint)data[i++]];
i = nextI(data, i, num3);
byte b2 = decodingTable[(uint)data[i++]];
i = nextI(data, i, num3);
byte b3 = decodingTable[(uint)data[i++]];
i = nextI(data, i, num3);
byte b4 = decodingTable[(uint)data[i++]];
if ((b | b2 | b3 | b4) >= 128)
{
throw new IOException("invalid characters encountered in base64 data");
}
outStream.WriteByte((byte)((b << 2) | (b2 >> 4)));
outStream.WriteByte((byte)((b2 << 4) | (b3 >> 2)));
outStream.WriteByte((byte)((b3 << 6) | b4));
num += 3;
}
return num + decodeLastBlock(outStream, data[num2 - 4], data[num2 - 3], data[num2 - 2], data[num2 - 1]);
}
private int decodeLastBlock(Stream outStream, char c1, char c2, char c3, char c4)
{
if (c3 == padding)
{
if (c4 != padding)
{
throw new IOException("invalid characters encountered at end of base64 data");
}
byte b = decodingTable[(uint)c1];
byte b2 = decodingTable[(uint)c2];
if ((b | b2) >= 128)
{
throw new IOException("invalid characters encountered at end of base64 data");
}
outStream.WriteByte((byte)((b << 2) | (b2 >> 4)));
return 1;
}
if (c4 == padding)
{
byte b3 = decodingTable[(uint)c1];
byte b4 = decodingTable[(uint)c2];
byte b5 = decodingTable[(uint)c3];
if ((b3 | b4 | b5) >= 128)
{
throw new IOException("invalid characters encountered at end of base64 data");
}
outStream.WriteByte((byte)((b3 << 2) | (b4 >> 4)));
outStream.WriteByte((byte)((b4 << 4) | (b5 >> 2)));
return 2;
}
byte b6 = decodingTable[(uint)c1];
byte b7 = decodingTable[(uint)c2];
byte b8 = decodingTable[(uint)c3];
byte b9 = decodingTable[(uint)c4];
if ((b6 | b7 | b8 | b9) >= 128)
{
throw new IOException("invalid characters encountered at end of base64 data");
}
outStream.WriteByte((byte)((b6 << 2) | (b7 >> 4)));
outStream.WriteByte((byte)((b7 << 4) | (b8 >> 2)));
outStream.WriteByte((byte)((b8 << 6) | b9));
return 3;
}
private int nextI(string data, int i, int finish)
{
while (i < finish && ignore(data[i]))
{
i++;
}
return i;
}
}

View File

@@ -0,0 +1,63 @@
using System;
namespace Org.BouncyCastle.Utilities.Encoders;
public class BufferedDecoder
{
internal byte[] buffer;
internal int bufOff;
internal ITranslator translator;
public BufferedDecoder(ITranslator translator, int bufferSize)
{
this.translator = translator;
if (bufferSize % translator.GetEncodedBlockSize() != 0)
{
throw new ArgumentException("buffer size not multiple of input block size");
}
buffer = new byte[bufferSize];
}
public int ProcessByte(byte input, byte[] output, int outOff)
{
int result = 0;
buffer[bufOff++] = input;
if (bufOff == buffer.Length)
{
result = translator.Decode(buffer, 0, buffer.Length, output, outOff);
bufOff = 0;
}
return result;
}
public int ProcessBytes(byte[] input, int inOff, int len, byte[] outBytes, int outOff)
{
if (len < 0)
{
throw new ArgumentException("Can't have a negative input length!");
}
int num = 0;
int num2 = buffer.Length - bufOff;
if (len > num2)
{
Array.Copy(input, inOff, buffer, bufOff, num2);
num += translator.Decode(buffer, 0, buffer.Length, outBytes, outOff);
bufOff = 0;
len -= num2;
inOff += num2;
outOff += num;
int num3 = len - len % buffer.Length;
num += translator.Decode(input, inOff, num3, outBytes, outOff);
len -= num3;
inOff += num3;
}
if (len != 0)
{
Array.Copy(input, inOff, buffer, bufOff, len);
bufOff += len;
}
return num;
}
}

View File

@@ -0,0 +1,63 @@
using System;
namespace Org.BouncyCastle.Utilities.Encoders;
public class BufferedEncoder
{
internal byte[] Buffer;
internal int bufOff;
internal ITranslator translator;
public BufferedEncoder(ITranslator translator, int bufferSize)
{
this.translator = translator;
if (bufferSize % translator.GetEncodedBlockSize() != 0)
{
throw new ArgumentException("buffer size not multiple of input block size");
}
Buffer = new byte[bufferSize];
}
public int ProcessByte(byte input, byte[] outBytes, int outOff)
{
int result = 0;
Buffer[bufOff++] = input;
if (bufOff == Buffer.Length)
{
result = translator.Encode(Buffer, 0, Buffer.Length, outBytes, outOff);
bufOff = 0;
}
return result;
}
public int ProcessBytes(byte[] input, int inOff, int len, byte[] outBytes, int outOff)
{
if (len < 0)
{
throw new ArgumentException("Can't have a negative input length!");
}
int num = 0;
int num2 = Buffer.Length - bufOff;
if (len > num2)
{
Array.Copy(input, inOff, Buffer, bufOff, num2);
num += translator.Encode(Buffer, 0, Buffer.Length, outBytes, outOff);
bufOff = 0;
len -= num2;
inOff += num2;
outOff += num;
int num3 = len - len % Buffer.Length;
num += translator.Encode(input, inOff, num3, outBytes, outOff);
len -= num3;
inOff += num3;
}
if (len != 0)
{
Array.Copy(input, inOff, Buffer, bufOff, len);
bufOff += len;
}
return num;
}
}

View File

@@ -0,0 +1,64 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders;
public sealed class Hex
{
private static readonly IEncoder encoder = new HexEncoder();
private Hex()
{
}
public static string ToHexString(byte[] data)
{
return ToHexString(data, 0, data.Length);
}
public static string ToHexString(byte[] data, int off, int length)
{
byte[] bytes = Encode(data, off, length);
return Strings.FromAsciiByteArray(bytes);
}
public static byte[] Encode(byte[] data)
{
return Encode(data, 0, data.Length);
}
public static byte[] Encode(byte[] data, int off, int length)
{
MemoryStream memoryStream = new MemoryStream(length * 2);
encoder.Encode(data, off, length, memoryStream);
return memoryStream.ToArray();
}
public static int Encode(byte[] data, Stream outStream)
{
return encoder.Encode(data, 0, data.Length, outStream);
}
public static int Encode(byte[] data, int off, int length, Stream outStream)
{
return encoder.Encode(data, off, length, outStream);
}
public static byte[] Decode(byte[] data)
{
MemoryStream memoryStream = new MemoryStream((data.Length + 1) / 2);
encoder.Decode(data, 0, data.Length, memoryStream);
return memoryStream.ToArray();
}
public static byte[] Decode(string data)
{
MemoryStream memoryStream = new MemoryStream((data.Length + 1) / 2);
encoder.DecodeString(data, memoryStream);
return memoryStream.ToArray();
}
public static int Decode(string data, Stream outStream)
{
return encoder.DecodeString(data, outStream);
}
}

View File

@@ -0,0 +1,112 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders;
public class HexEncoder : IEncoder
{
protected readonly byte[] encodingTable = new byte[16]
{
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
97, 98, 99, 100, 101, 102
};
protected readonly byte[] decodingTable = new byte[128];
protected void InitialiseDecodingTable()
{
Arrays.Fill(decodingTable, byte.MaxValue);
for (int i = 0; i < encodingTable.Length; i++)
{
decodingTable[encodingTable[i]] = (byte)i;
}
decodingTable[65] = decodingTable[97];
decodingTable[66] = decodingTable[98];
decodingTable[67] = decodingTable[99];
decodingTable[68] = decodingTable[100];
decodingTable[69] = decodingTable[101];
decodingTable[70] = decodingTable[102];
}
public HexEncoder()
{
InitialiseDecodingTable();
}
public int Encode(byte[] data, int off, int length, Stream outStream)
{
for (int i = off; i < off + length; i++)
{
int num = data[i];
outStream.WriteByte(encodingTable[num >> 4]);
outStream.WriteByte(encodingTable[num & 0xF]);
}
return length * 2;
}
private static bool Ignore(char c)
{
if (c != '\n' && c != '\r' && c != '\t')
{
return c == ' ';
}
return true;
}
public int Decode(byte[] data, int off, int length, Stream outStream)
{
int num = 0;
int num2 = off + length;
while (num2 > off && Ignore((char)data[num2 - 1]))
{
num2--;
}
int i = off;
while (i < num2)
{
for (; i < num2 && Ignore((char)data[i]); i++)
{
}
byte b = decodingTable[data[i++]];
for (; i < num2 && Ignore((char)data[i]); i++)
{
}
byte b2 = decodingTable[data[i++]];
if ((b | b2) >= 128)
{
throw new IOException("invalid characters encountered in Hex data");
}
outStream.WriteByte((byte)((b << 4) | b2));
num++;
}
return num;
}
public int DecodeString(string data, Stream outStream)
{
int num = 0;
int num2 = data.Length;
while (num2 > 0 && Ignore(data[num2 - 1]))
{
num2--;
}
int i = 0;
while (i < num2)
{
for (; i < num2 && Ignore(data[i]); i++)
{
}
byte b = decodingTable[(uint)data[i++]];
for (; i < num2 && Ignore(data[i]); i++)
{
}
byte b2 = decodingTable[(uint)data[i++]];
if ((b | b2) >= 128)
{
throw new IOException("invalid characters encountered in Hex data");
}
outStream.WriteByte((byte)((b << 4) | b2));
num++;
}
return num;
}
}

View File

@@ -0,0 +1,71 @@
namespace Org.BouncyCastle.Utilities.Encoders;
public class HexTranslator : ITranslator
{
private static readonly byte[] hexTable = new byte[16]
{
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
97, 98, 99, 100, 101, 102
};
public int GetEncodedBlockSize()
{
return 2;
}
public int Encode(byte[] input, int inOff, int length, byte[] outBytes, int outOff)
{
int num = 0;
int num2 = 0;
while (num < length)
{
outBytes[outOff + num2] = hexTable[(input[inOff] >> 4) & 0xF];
outBytes[outOff + num2 + 1] = hexTable[input[inOff] & 0xF];
inOff++;
num++;
num2 += 2;
}
return length * 2;
}
public int GetDecodedBlockSize()
{
return 1;
}
public int Decode(byte[] input, int inOff, int length, byte[] outBytes, int outOff)
{
int num = length / 2;
for (int i = 0; i < num; i++)
{
byte b = input[inOff + i * 2];
byte b2 = input[inOff + i * 2 + 1];
if (b < 97)
{
outBytes[outOff] = (byte)(b - 48 << 4);
}
else
{
outBytes[outOff] = (byte)(b - 97 + 10 << 4);
}
if (b2 < 97)
{
byte[] array2;
byte[] array = (array2 = outBytes);
int num2 = outOff;
nint num3 = num2;
array[num2] = (byte)(array2[num3] + (byte)(b2 - 48));
}
else
{
byte[] array2;
byte[] array3 = (array2 = outBytes);
int num4 = outOff;
nint num3 = num4;
array3[num4] = (byte)(array2[num3] + (byte)(b2 - 97 + 10));
}
outOff++;
}
return num;
}
}

View File

@@ -0,0 +1,12 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders;
public interface IEncoder
{
int Encode(byte[] data, int off, int length, Stream outStream);
int Decode(byte[] data, int off, int length, Stream outStream);
int DecodeString(string data, Stream outStream);
}

View File

@@ -0,0 +1,12 @@
namespace Org.BouncyCastle.Utilities.Encoders;
public interface ITranslator
{
int GetEncodedBlockSize();
int Encode(byte[] input, int inOff, int length, byte[] outBytes, int outOff);
int GetDecodedBlockSize();
int Decode(byte[] input, int inOff, int length, byte[] outBytes, int outOff);
}

View File

@@ -0,0 +1,66 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders;
public class UrlBase64
{
private static readonly IEncoder encoder = new UrlBase64Encoder();
public static byte[] Encode(byte[] data)
{
MemoryStream memoryStream = new MemoryStream();
try
{
encoder.Encode(data, 0, data.Length, memoryStream);
}
catch (IOException ex)
{
throw new Exception("exception encoding URL safe base64 string: " + ex.Message, ex);
}
return memoryStream.ToArray();
}
public static int Encode(byte[] data, Stream outStr)
{
return encoder.Encode(data, 0, data.Length, outStr);
}
public static byte[] Decode(byte[] data)
{
MemoryStream memoryStream = new MemoryStream();
try
{
encoder.Decode(data, 0, data.Length, memoryStream);
}
catch (IOException ex)
{
throw new Exception("exception decoding URL safe base64 string: " + ex.Message, ex);
}
return memoryStream.ToArray();
}
public static int Decode(byte[] data, Stream outStr)
{
return encoder.Decode(data, 0, data.Length, outStr);
}
public static byte[] Decode(string data)
{
MemoryStream memoryStream = new MemoryStream();
try
{
encoder.DecodeString(data, memoryStream);
}
catch (IOException ex)
{
throw new Exception("exception decoding URL safe base64 string: " + ex.Message, ex);
}
return memoryStream.ToArray();
}
public static int Decode(string data, Stream outStr)
{
return encoder.DecodeString(data, outStr);
}
}

View File

@@ -0,0 +1,12 @@
namespace Org.BouncyCastle.Utilities.Encoders;
public class UrlBase64Encoder : Base64Encoder
{
public UrlBase64Encoder()
{
encodingTable[encodingTable.Length - 2] = 45;
encodingTable[encodingTable.Length - 1] = 95;
padding = 46;
InitialiseDecodingTable();
}
}

View File

@@ -0,0 +1,43 @@
using System;
using Org.BouncyCastle.Utilities.Date;
namespace Org.BouncyCastle.Utilities;
internal abstract class Enums
{
internal static Enum GetEnumValue(Type enumType, string s)
{
if (!IsEnumType(enumType))
{
throw new ArgumentException("Not an enumeration type", "enumType");
}
if (s.Length > 0 && char.IsLetter(s[0]) && s.IndexOf(',') < 0)
{
s = s.Replace('-', '_');
s = s.Replace('/', '_');
return (Enum)Enum.Parse(enumType, s, ignoreCase: false);
}
throw new ArgumentException();
}
internal static Array GetEnumValues(Type enumType)
{
if (!IsEnumType(enumType))
{
throw new ArgumentException("Not an enumeration type", "enumType");
}
return Enum.GetValues(enumType);
}
internal static Enum GetArbitraryValue(Type enumType)
{
Array enumValues = GetEnumValues(enumType);
int index = (int)(DateTimeUtilities.CurrentUnixMs() & 0x7FFFFFFF) % enumValues.Length;
return (Enum)enumValues.GetValue(index);
}
internal static bool IsEnumType(Type t)
{
return t.IsEnum;
}
}

View File

@@ -0,0 +1,8 @@
namespace Org.BouncyCastle.Utilities;
public interface IMemoable
{
IMemoable Copy();
void Reset(IMemoable other);
}

View File

@@ -0,0 +1,87 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.IO;
public abstract class BaseInputStream : Stream
{
private bool closed;
public sealed override bool CanRead => !closed;
public sealed override bool CanSeek => false;
public sealed override bool CanWrite => false;
public sealed override long Length
{
get
{
throw new NotSupportedException();
}
}
public sealed override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
public override void Close()
{
closed = true;
base.Close();
}
public sealed override void Flush()
{
}
public override int Read(byte[] buffer, int offset, int count)
{
int num = offset;
try
{
int num2 = offset + count;
while (num < num2)
{
int num3 = ReadByte();
if (num3 != -1)
{
buffer[num++] = (byte)num3;
continue;
}
break;
}
}
catch (IOException)
{
if (num == offset)
{
throw;
}
}
return num - offset;
}
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,79 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.IO;
public abstract class BaseOutputStream : Stream
{
private bool closed;
public sealed override bool CanRead => false;
public sealed override bool CanSeek => false;
public sealed override bool CanWrite => !closed;
public sealed override long Length
{
get
{
throw new NotSupportedException();
}
}
public sealed override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
public override void Close()
{
closed = true;
base.Close();
}
public override void 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[] buffer, int offset, int count)
{
int num = offset + count;
for (int i = offset; i < num; i++)
{
WriteByte(buffer[i]);
}
}
public virtual void Write(params byte[] buffer)
{
Write(buffer, 0, buffer.Length);
}
public override void WriteByte(byte b)
{
Write(new byte[1] { b }, 0, 1);
}
}

View File

@@ -0,0 +1,74 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.IO;
public class FilterStream : Stream
{
protected readonly Stream s;
public override bool CanRead => s.CanRead;
public override bool CanSeek => s.CanSeek;
public override bool CanWrite => s.CanWrite;
public override long Length => s.Length;
public override long Position
{
get
{
return s.Position;
}
set
{
s.Position = value;
}
}
public FilterStream(Stream s)
{
this.s = s;
}
public override void Close()
{
Platform.Dispose(s);
base.Close();
}
public override void Flush()
{
s.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
return s.Seek(offset, origin);
}
public override void SetLength(long value)
{
s.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count)
{
return s.Read(buffer, offset, count);
}
public override int ReadByte()
{
return s.ReadByte();
}
public override void Write(byte[] buffer, int offset, int count)
{
s.Write(buffer, offset, count);
}
public override void WriteByte(byte value)
{
s.WriteByte(value);
}
}

View File

@@ -0,0 +1,13 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.IO;
public class MemoryInputStream : MemoryStream
{
public sealed override bool CanWrite => false;
public MemoryInputStream(byte[] buffer)
: base(buffer, writable: false)
{
}
}

View File

@@ -0,0 +1,8 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.IO;
public class MemoryOutputStream : MemoryStream
{
public sealed override bool CanRead => false;
}

View File

@@ -0,0 +1,12 @@
namespace Org.BouncyCastle.Utilities.IO;
internal class NullOutputStream : BaseOutputStream
{
public override void WriteByte(byte b)
{
}
public override void Write(byte[] buffer, int offset, int count)
{
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Utilities.IO.Pem;
[Serializable]
public class PemGenerationException : Exception
{
public PemGenerationException()
{
}
public PemGenerationException(string message)
: base(message)
{
}
public PemGenerationException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,46 @@
namespace Org.BouncyCastle.Utilities.IO.Pem;
public class PemHeader
{
private string name;
private string val;
public virtual string Name => name;
public virtual string Value => val;
public PemHeader(string name, string val)
{
this.name = name;
this.val = val;
}
public override int GetHashCode()
{
return GetHashCode(name) + 31 * GetHashCode(val);
}
public override bool Equals(object obj)
{
if (obj == this)
{
return true;
}
if (!(obj is PemHeader))
{
return false;
}
PemHeader pemHeader = (PemHeader)obj;
if (object.Equals(name, pemHeader.name))
{
return object.Equals(val, pemHeader.val);
}
return false;
}
private int GetHashCode(string s)
{
return s?.GetHashCode() ?? 1;
}
}

View File

@@ -0,0 +1,35 @@
using System.Collections;
namespace Org.BouncyCastle.Utilities.IO.Pem;
public class PemObject : PemObjectGenerator
{
private string type;
private IList headers;
private byte[] content;
public string Type => type;
public IList Headers => headers;
public byte[] Content => content;
public PemObject(string type, byte[] content)
: this(type, Platform.CreateArrayList(), content)
{
}
public PemObject(string type, IList headers, byte[] content)
{
this.type = type;
this.headers = Platform.CreateArrayList(headers);
this.content = content;
}
public PemObject Generate()
{
return this;
}
}

View File

@@ -0,0 +1,6 @@
namespace Org.BouncyCastle.Utilities.IO.Pem;
public interface PemObjectGenerator
{
PemObject Generate();
}

View File

@@ -0,0 +1,6 @@
namespace Org.BouncyCastle.Utilities.IO.Pem;
public interface PemObjectParser
{
object ParseObject(PemObject obj);
}

View File

@@ -0,0 +1,76 @@
using System;
using System.Collections;
using System.IO;
using System.Text;
using Org.BouncyCastle.Utilities.Encoders;
namespace Org.BouncyCastle.Utilities.IO.Pem;
public class PemReader
{
private const string BeginString = "-----BEGIN ";
private const string EndString = "-----END ";
private readonly TextReader reader;
public TextReader Reader => reader;
public PemReader(TextReader reader)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
this.reader = reader;
}
public PemObject ReadPemObject()
{
string text = reader.ReadLine();
if (text != null && Platform.StartsWith(text, "-----BEGIN "))
{
text = text.Substring("-----BEGIN ".Length);
int num = text.IndexOf('-');
string type = text.Substring(0, num);
if (num > 0)
{
return LoadObject(type);
}
}
return null;
}
private PemObject LoadObject(string type)
{
string text = "-----END " + type;
IList list = Platform.CreateArrayList();
StringBuilder stringBuilder = new StringBuilder();
string text2;
while ((text2 = reader.ReadLine()) != null && Platform.IndexOf(text2, text) == -1)
{
int num = text2.IndexOf(':');
if (num == -1)
{
stringBuilder.Append(text2.Trim());
continue;
}
string text3 = text2.Substring(0, num).Trim();
if (Platform.StartsWith(text3, "X-"))
{
text3 = text3.Substring(2);
}
string val = text2.Substring(num + 1).Trim();
list.Add(new PemHeader(text3, val));
}
if (text2 == null)
{
throw new IOException(text + " not found");
}
if (stringBuilder.Length % 4 != 0)
{
throw new IOException("base64 data appears to be truncated");
}
return new PemObject(type, list, Base64.Decode(stringBuilder.ToString()));
}
}

View File

@@ -0,0 +1,85 @@
using System;
using System.IO;
using Org.BouncyCastle.Utilities.Encoders;
namespace Org.BouncyCastle.Utilities.IO.Pem;
public class PemWriter
{
private const int LineLength = 64;
private readonly TextWriter writer;
private readonly int nlLength;
private char[] buf = new char[64];
public TextWriter Writer => writer;
public PemWriter(TextWriter writer)
{
if (writer == null)
{
throw new ArgumentNullException("writer");
}
this.writer = writer;
nlLength = Platform.NewLine.Length;
}
public int GetOutputSize(PemObject obj)
{
int num = 2 * (obj.Type.Length + 10 + nlLength) + 6 + 4;
if (obj.Headers.Count > 0)
{
foreach (PemHeader header in obj.Headers)
{
num += header.Name.Length + ": ".Length + header.Value.Length + nlLength;
}
num += nlLength;
}
int num2 = (obj.Content.Length + 2) / 3 * 4;
return num + (num2 + (num2 + 64 - 1) / 64 * nlLength);
}
public void WriteObject(PemObjectGenerator objGen)
{
PemObject pemObject = objGen.Generate();
WritePreEncapsulationBoundary(pemObject.Type);
if (pemObject.Headers.Count > 0)
{
foreach (PemHeader header in pemObject.Headers)
{
writer.Write(header.Name);
writer.Write(": ");
writer.WriteLine(header.Value);
}
writer.WriteLine();
}
WriteEncoded(pemObject.Content);
WritePostEncapsulationBoundary(pemObject.Type);
}
private void WriteEncoded(byte[] bytes)
{
bytes = Base64.Encode(bytes);
for (int i = 0; i < bytes.Length; i += buf.Length)
{
int j;
for (j = 0; j != buf.Length && i + j < bytes.Length; j++)
{
buf[j] = (char)bytes[i + j];
}
writer.WriteLine(buf, 0, j);
}
}
private void WritePreEncapsulationBoundary(string type)
{
writer.WriteLine("-----BEGIN " + type + "-----");
}
private void WritePostEncapsulationBoundary(string type)
{
writer.WriteLine("-----END " + type + "-----");
}
}

View File

@@ -0,0 +1,45 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.IO;
public class PushbackStream : FilterStream
{
private int buf = -1;
public PushbackStream(Stream s)
: base(s)
{
}
public override int ReadByte()
{
if (buf != -1)
{
int result = buf;
buf = -1;
return result;
}
return base.ReadByte();
}
public override int Read(byte[] buffer, int offset, int count)
{
if (buf != -1 && count > 0)
{
buffer[offset] = (byte)buf;
buf = -1;
return 1;
}
return base.Read(buffer, offset, count);
}
public virtual void Unread(int b)
{
if (buf != -1)
{
throw new InvalidOperationException("Can only push back one byte");
}
buf = b & 0xFF;
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.IO;
[Serializable]
public class StreamOverflowException : IOException
{
public StreamOverflowException()
{
}
public StreamOverflowException(string message)
: base(message)
{
}
public StreamOverflowException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,104 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.IO;
public sealed class Streams
{
private const int BufferSize = 512;
private Streams()
{
}
public static void Drain(Stream inStr)
{
byte[] array = new byte[512];
while (inStr.Read(array, 0, array.Length) > 0)
{
}
}
public static byte[] ReadAll(Stream inStr)
{
MemoryStream memoryStream = new MemoryStream();
PipeAll(inStr, memoryStream);
return memoryStream.ToArray();
}
public static byte[] ReadAllLimited(Stream inStr, int limit)
{
MemoryStream memoryStream = new MemoryStream();
PipeAllLimited(inStr, limit, memoryStream);
return memoryStream.ToArray();
}
public static int ReadFully(Stream inStr, byte[] buf)
{
return ReadFully(inStr, buf, 0, buf.Length);
}
public static int ReadFully(Stream inStr, byte[] buf, int off, int len)
{
int i;
int num;
for (i = 0; i < len; i += num)
{
num = inStr.Read(buf, off + i, len - i);
if (num < 1)
{
break;
}
}
return i;
}
public static void PipeAll(Stream inStr, Stream outStr)
{
byte[] array = new byte[512];
int count;
while ((count = inStr.Read(array, 0, array.Length)) > 0)
{
outStr.Write(array, 0, count);
}
}
public static long PipeAllLimited(Stream inStr, long limit, Stream outStr)
{
byte[] array = new byte[512];
long num = 0L;
int num2;
while ((num2 = inStr.Read(array, 0, array.Length)) > 0)
{
if (limit - num < num2)
{
throw new StreamOverflowException("Data Overflow");
}
num += num2;
outStr.Write(array, 0, num2);
}
return num;
}
public static void WriteBufTo(MemoryStream buf, Stream output)
{
buf.WriteTo(output);
}
public static int WriteBufTo(MemoryStream buf, byte[] output, int offset)
{
int num = (int)buf.Length;
buf.WriteTo(new MemoryStream(output, offset, num, writable: true));
return num;
}
public static void WriteZeroes(Stream outStr, long count)
{
byte[] buffer = new byte[512];
while (count > 512)
{
outStr.Write(buffer, 0, 512);
count -= 512;
}
outStr.Write(buffer, 0, (int)count);
}
}

View File

@@ -0,0 +1,43 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.IO;
public class TeeInputStream : BaseInputStream
{
private readonly Stream input;
private readonly Stream tee;
public TeeInputStream(Stream input, Stream tee)
{
this.input = input;
this.tee = tee;
}
public override void Close()
{
Platform.Dispose(input);
Platform.Dispose(tee);
base.Close();
}
public override int Read(byte[] buf, int off, int len)
{
int num = input.Read(buf, off, len);
if (num > 0)
{
tee.Write(buf, off, num);
}
return num;
}
public override int ReadByte()
{
int num = input.ReadByte();
if (num >= 0)
{
tee.WriteByte((byte)num);
}
return num;
}
}

View File

@@ -0,0 +1,35 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.IO;
public class TeeOutputStream : BaseOutputStream
{
private readonly Stream output;
private readonly Stream tee;
public TeeOutputStream(Stream output, Stream tee)
{
this.output = output;
this.tee = tee;
}
public override void Close()
{
Platform.Dispose(output);
Platform.Dispose(tee);
base.Close();
}
public override void Write(byte[] buffer, int offset, int count)
{
output.Write(buffer, offset, count);
tee.Write(buffer, offset, count);
}
public override void WriteByte(byte b)
{
output.WriteByte(b);
tee.WriteByte(b);
}
}

View File

@@ -0,0 +1,28 @@
using System;
namespace Org.BouncyCastle.Utilities;
public abstract class Integers
{
public static int RotateLeft(int i, int distance)
{
return (i << distance) ^ (i >>> -distance);
}
[CLSCompliant(false)]
public static uint RotateLeft(uint i, int distance)
{
return (i << distance) ^ (i >> -distance);
}
public static int RotateRight(int i, int distance)
{
return (i >>> distance) ^ (i << -distance);
}
[CLSCompliant(false)]
public static uint RotateRight(uint i, int distance)
{
return (i >> distance) ^ (i << -distance);
}
}

View File

@@ -0,0 +1,11 @@
using System;
namespace Org.BouncyCastle.Utilities;
public class MemoableResetException : InvalidCastException
{
public MemoableResetException(string msg)
: base(msg)
{
}
}

View File

@@ -0,0 +1,189 @@
using System;
using System.Globalization;
namespace Org.BouncyCastle.Utilities.Net;
public class IPAddress
{
public static bool IsValid(string address)
{
if (!IsValidIPv4(address))
{
return IsValidIPv6(address);
}
return true;
}
public static bool IsValidWithNetMask(string address)
{
if (!IsValidIPv4WithNetmask(address))
{
return IsValidIPv6WithNetmask(address);
}
return true;
}
public static bool IsValidIPv4(string address)
{
try
{
return unsafeIsValidIPv4(address);
}
catch (FormatException)
{
}
catch (OverflowException)
{
}
return false;
}
private static bool unsafeIsValidIPv4(string address)
{
if (address.Length == 0)
{
return false;
}
int num = 0;
string text = address + ".";
int num2 = 0;
int num3;
while (num2 < text.Length && (num3 = text.IndexOf('.', num2)) > num2)
{
if (num == 4)
{
return false;
}
string s = text.Substring(num2, num3 - num2);
int num4 = int.Parse(s);
if (num4 < 0 || num4 > 255)
{
return false;
}
num2 = num3 + 1;
num++;
}
return num == 4;
}
public static bool IsValidIPv4WithNetmask(string address)
{
int num = address.IndexOf('/');
string text = address.Substring(num + 1);
if (num > 0 && IsValidIPv4(address.Substring(0, num)))
{
if (!IsValidIPv4(text))
{
return IsMaskValue(text, 32);
}
return true;
}
return false;
}
public static bool IsValidIPv6WithNetmask(string address)
{
int num = address.IndexOf('/');
string text = address.Substring(num + 1);
if (num > 0)
{
if (IsValidIPv6(address.Substring(0, num)))
{
if (!IsValidIPv6(text))
{
return IsMaskValue(text, 128);
}
return true;
}
return false;
}
return false;
}
private static bool IsMaskValue(string component, int size)
{
int num = int.Parse(component);
try
{
return num >= 0 && num <= size;
}
catch (FormatException)
{
}
catch (OverflowException)
{
}
return false;
}
public static bool IsValidIPv6(string address)
{
try
{
return unsafeIsValidIPv6(address);
}
catch (FormatException)
{
}
catch (OverflowException)
{
}
return false;
}
private static bool unsafeIsValidIPv6(string address)
{
if (address.Length == 0)
{
return false;
}
int num = 0;
string text = address + ":";
bool flag = false;
int num2 = 0;
int num3;
while (num2 < text.Length && (num3 = text.IndexOf(':', num2)) >= num2)
{
if (num == 8)
{
return false;
}
if (num2 != num3)
{
string text2 = text.Substring(num2, num3 - num2);
if (num3 == text.Length - 1 && text2.IndexOf('.') > 0)
{
if (!IsValidIPv4(text2))
{
return false;
}
num++;
}
else
{
string s = text.Substring(num2, num3 - num2);
int num4 = int.Parse(s, NumberStyles.AllowHexSpecifier);
if (num4 < 0 || num4 > 65535)
{
return false;
}
}
}
else
{
if (num3 != 1 && num3 != text.Length - 1 && flag)
{
return false;
}
flag = true;
}
num2 = num3 + 1;
num++;
}
if (num != 8)
{
return flag;
}
return true;
}
}

View File

@@ -0,0 +1,126 @@
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Security;
namespace Org.BouncyCastle.Utilities;
internal abstract class Platform
{
private static readonly CompareInfo InvariantCompareInfo = CultureInfo.InvariantCulture.CompareInfo;
internal static readonly string NewLine = GetNewLine();
private static string GetNewLine()
{
return Environment.NewLine;
}
internal static bool EqualsIgnoreCase(string a, string b)
{
return ToUpperInvariant(a) == ToUpperInvariant(b);
}
internal static string GetEnvironmentVariable(string variable)
{
try
{
return Environment.GetEnvironmentVariable(variable);
}
catch (SecurityException)
{
return null;
}
}
internal static Exception CreateNotImplementedException(string message)
{
return new NotImplementedException(message);
}
internal static IList CreateArrayList()
{
return new ArrayList();
}
internal static IList CreateArrayList(int capacity)
{
return new ArrayList(capacity);
}
internal static IList CreateArrayList(ICollection collection)
{
return new ArrayList(collection);
}
internal static IList CreateArrayList(IEnumerable collection)
{
ArrayList arrayList = new ArrayList();
foreach (object item in collection)
{
arrayList.Add(item);
}
return arrayList;
}
internal static IDictionary CreateHashtable()
{
return new Hashtable();
}
internal static IDictionary CreateHashtable(int capacity)
{
return new Hashtable(capacity);
}
internal static IDictionary CreateHashtable(IDictionary dictionary)
{
return new Hashtable(dictionary);
}
internal static string ToLowerInvariant(string s)
{
return s.ToLower(CultureInfo.InvariantCulture);
}
internal static string ToUpperInvariant(string s)
{
return s.ToUpper(CultureInfo.InvariantCulture);
}
internal static void Dispose(Stream s)
{
s.Close();
}
internal static void Dispose(TextWriter t)
{
t.Close();
}
internal static int IndexOf(string source, string value)
{
return InvariantCompareInfo.IndexOf(source, value, CompareOptions.Ordinal);
}
internal static int LastIndexOf(string source, string value)
{
return InvariantCompareInfo.LastIndexOf(source, value, CompareOptions.Ordinal);
}
internal static bool StartsWith(string source, string prefix)
{
return InvariantCompareInfo.IsPrefix(source, prefix, CompareOptions.Ordinal);
}
internal static bool EndsWith(string source, string suffix)
{
return InvariantCompareInfo.IsSuffix(source, suffix, CompareOptions.Ordinal);
}
internal static string GetTypeName(object obj)
{
return obj.GetType().FullName;
}
}

View File

@@ -0,0 +1,99 @@
using System;
using System.Text;
namespace Org.BouncyCastle.Utilities;
public abstract class Strings
{
public static string ToUpperCase(string original)
{
bool flag = false;
char[] array = original.ToCharArray();
for (int i = 0; i != array.Length; i++)
{
char c = array[i];
if ('a' <= c && 'z' >= c)
{
flag = true;
array[i] = (char)(c - 97 + 65);
}
}
if (flag)
{
return new string(array);
}
return original;
}
internal static bool IsOneOf(string s, params string[] candidates)
{
foreach (string text in candidates)
{
if (s == text)
{
return true;
}
}
return false;
}
public static string FromByteArray(byte[] bs)
{
char[] array = new char[bs.Length];
for (int i = 0; i < array.Length; i++)
{
array[i] = Convert.ToChar(bs[i]);
}
return new string(array);
}
public static byte[] ToByteArray(char[] cs)
{
byte[] array = new byte[cs.Length];
for (int i = 0; i < array.Length; i++)
{
array[i] = Convert.ToByte(cs[i]);
}
return array;
}
public static byte[] ToByteArray(string s)
{
byte[] array = new byte[s.Length];
for (int i = 0; i < array.Length; i++)
{
array[i] = Convert.ToByte(s[i]);
}
return array;
}
public static string FromAsciiByteArray(byte[] bytes)
{
return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
}
public static byte[] ToAsciiByteArray(char[] cs)
{
return Encoding.ASCII.GetBytes(cs);
}
public static byte[] ToAsciiByteArray(string s)
{
return Encoding.ASCII.GetBytes(s);
}
public static string FromUtf8ByteArray(byte[] bytes)
{
return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
}
public static byte[] ToUtf8ByteArray(char[] cs)
{
return Encoding.UTF8.GetBytes(cs);
}
public static byte[] ToUtf8ByteArray(string s)
{
return Encoding.UTF8.GetBytes(s);
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Org.BouncyCastle.Utilities;
public sealed class Times
{
private static long NanosecondsPerTick = 100L;
public static long NanoTime()
{
return DateTime.UtcNow.Ticks * NanosecondsPerTick;
}
}

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;
}
}