You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
440 lines
15 KiB
440 lines
15 KiB
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR) |
|
#pragma warning disable |
|
using System; |
|
using System.Runtime.CompilerServices; |
|
|
|
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities |
|
{ |
|
|
|
|
|
|
|
internal static class Pack |
|
{ |
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt16_To_BE(ushort n, byte[] bs) |
|
{ |
|
bs[0] = (byte)(n >> 8); |
|
bs[1] = (byte)(n); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt16_To_BE(ushort n, byte[] bs, int off) |
|
{ |
|
bs[off] = (byte)(n >> 8); |
|
bs[off + 1] = (byte)(n); |
|
} |
|
|
|
//[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static ushort BE_To_UInt16(byte[] bs) |
|
//{ |
|
// for (int i = 0; i < ns.Length; ++i) |
|
// { |
|
// UInt16_To_BE(ns[i], bs, off); |
|
// off += 2; |
|
// } |
|
//} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt16_To_BE(ushort[] ns, int nsOff, int nsLen, byte[] bs, int bsOff) |
|
{ |
|
for (int i = 0; i < nsLen; ++i) |
|
{ |
|
UInt16_To_BE(ns[nsOff + i], bs, bsOff); |
|
bsOff += 2; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static byte[] UInt16_To_BE(ushort n) |
|
{ |
|
byte[] bs = new byte[2]; |
|
UInt16_To_BE(n, bs, 0); |
|
return bs; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static byte[] UInt16_To_BE(ushort[] ns) |
|
{ |
|
return UInt16_To_BE(ns, 0, ns.Length); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static byte[] UInt16_To_BE(ushort[] ns, int nsOff, int nsLen) |
|
{ |
|
byte[] bs = new byte[2 * nsLen]; |
|
UInt16_To_BE(ns, nsOff, nsLen, bs, 0); |
|
return bs; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static ushort BE_To_UInt16(byte[] bs, int off) |
|
{ |
|
uint n = (uint)bs[off] << 8 |
|
| (uint)bs[off + 1]; |
|
return (ushort)n; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void BE_To_UInt16(byte[] bs, int bsOff, ushort[] ns, int nsOff) |
|
{ |
|
ns[nsOff] = BE_To_UInt16(bs, bsOff); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static ushort[] BE_To_UInt16(byte[] bs) |
|
{ |
|
return BE_To_UInt16(bs, 0, bs.Length); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static ushort[] BE_To_UInt16(byte[] bs, int off, int len) |
|
{ |
|
if ((len & 1) != 0) |
|
throw new ArgumentException("must be a multiple of 2", "len"); |
|
|
|
ushort[] ns = new ushort[len / 2]; |
|
for (int i = 0; i < len; i += 2) |
|
{ |
|
BE_To_UInt16(bs, off + i, ns, i >> 1); |
|
} |
|
return ns; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt32_To_BE(uint n, byte[] bs) |
|
{ |
|
bs[0] = (byte)(n >> 24); |
|
bs[1] = (byte)(n >> 16); |
|
bs[2] = (byte)(n >> 8); |
|
bs[3] = (byte)(n); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt32_To_BE(uint n, byte[] bs, int off) |
|
{ |
|
bs[off] = (byte)(n >> 24); |
|
bs[off + 1] = (byte)(n >> 16); |
|
bs[off + 2] = (byte)(n >> 8); |
|
bs[off + 3] = (byte)(n); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt32_To_BE(uint[] ns, byte[] bs, int off) |
|
{ |
|
for (int i = 0; i < ns.Length; ++i) |
|
{ |
|
UInt32_To_BE(ns[i], bs, off); |
|
off += 4; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt32_To_BE(uint[] ns, int nsOff, int nsLen, byte[] bs, int bsOff) |
|
{ |
|
for (int i = 0; i < nsLen; ++i) |
|
{ |
|
UInt32_To_BE(ns[nsOff + i], bs, bsOff); |
|
bsOff += 4; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static byte[] UInt32_To_BE(uint n) |
|
{ |
|
byte[] bs = new byte[4]; |
|
UInt32_To_BE(n, bs, 0); |
|
return bs; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static byte[] UInt32_To_BE(uint[] ns) |
|
{ |
|
byte[] bs = new byte[4 * ns.Length]; |
|
UInt32_To_BE(ns, bs, 0); |
|
return bs; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static uint BE_To_UInt32(byte[] bs) |
|
{ |
|
return (uint)bs[0] << 24 |
|
| (uint)bs[1] << 16 |
|
| (uint)bs[2] << 8 |
|
| (uint)bs[3]; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static uint BE_To_UInt32(byte[] bs, int off) |
|
{ |
|
return (uint)bs[off] << 24 |
|
| (uint)bs[off + 1] << 16 |
|
| (uint)bs[off + 2] << 8 |
|
| (uint)bs[off + 3]; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void BE_To_UInt32(byte[] bs, int off, uint[] ns) |
|
{ |
|
for (int i = 0; i < ns.Length; ++i) |
|
{ |
|
ns[i] = BE_To_UInt32(bs, off); |
|
off += 4; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void BE_To_UInt32(byte[] bs, int bsOff, uint[] ns, int nsOff, int nsLen) |
|
{ |
|
for (int i = 0; i < nsLen; ++i) |
|
{ |
|
ns[nsOff + i] = BE_To_UInt32(bs, bsOff); |
|
bsOff += 4; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static byte[] UInt64_To_BE(ulong n) |
|
{ |
|
byte[] bs = new byte[8]; |
|
UInt64_To_BE(n, bs, 0); |
|
return bs; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt64_To_BE(ulong n, byte[] bs) |
|
{ |
|
UInt32_To_BE((uint)(n >> 32), bs); |
|
UInt32_To_BE((uint)(n), bs, 4); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt64_To_BE(ulong n, byte[] bs, int off) |
|
{ |
|
UInt32_To_BE((uint)(n >> 32), bs, off); |
|
UInt32_To_BE((uint)(n), bs, off + 4); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static byte[] UInt64_To_BE(ulong[] ns) |
|
{ |
|
byte[] bs = new byte[8 * ns.Length]; |
|
UInt64_To_BE(ns, bs, 0); |
|
return bs; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt64_To_BE(ulong[] ns, byte[] bs, int off) |
|
{ |
|
for (int i = 0; i < ns.Length; ++i) |
|
{ |
|
UInt64_To_BE(ns[i], bs, off); |
|
off += 8; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt64_To_BE(ulong[] ns, int nsOff, int nsLen, byte[] bs, int bsOff) |
|
{ |
|
for (int i = 0; i < nsLen; ++i) |
|
{ |
|
UInt64_To_BE(ns[nsOff + i], bs, bsOff); |
|
bsOff += 8; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static ulong BE_To_UInt64(byte[] bs, int off) |
|
{ |
|
uint hi = BE_To_UInt32(bs, off); |
|
uint lo = BE_To_UInt32(bs, off + 4); |
|
return ((ulong)hi << 32) | (ulong)lo; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void BE_To_UInt64(byte[] bs, int off, ulong[] ns) |
|
{ |
|
for (int i = 0; i < ns.Length; ++i) |
|
{ |
|
ns[i] = BE_To_UInt64(bs, off); |
|
off += 8; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void BE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, int nsLen) |
|
{ |
|
for (int i = 0; i < nsLen; ++i) |
|
{ |
|
ns[nsOff + i] = BE_To_UInt64(bs, bsOff); |
|
bsOff += 8; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt16_To_LE(ushort n, byte[] bs) |
|
{ |
|
bs[0] = (byte)(n); |
|
bs[1] = (byte)(n >> 8); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt16_To_LE(ushort n, byte[] bs, int off) |
|
{ |
|
bs[off] = (byte)(n); |
|
bs[off + 1] = (byte)(n >> 8); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static ushort LE_To_UInt16(byte[] bs) |
|
{ |
|
uint n = (uint)bs[0] |
|
| (uint)bs[1] << 8; |
|
return (ushort)n; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static ushort LE_To_UInt16(byte[] bs, int off) |
|
{ |
|
uint n = (uint)bs[off] |
|
| (uint)bs[off + 1] << 8; |
|
return (ushort)n; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static byte[] UInt32_To_LE(uint n) |
|
{ |
|
byte[] bs = new byte[4]; |
|
UInt32_To_LE(n, bs, 0); |
|
return bs; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt32_To_LE(uint n, byte[] bs) |
|
{ |
|
bs[0] = (byte)(n); |
|
bs[1] = (byte)(n >> 8); |
|
bs[2] = (byte)(n >> 16); |
|
bs[3] = (byte)(n >> 24); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt32_To_LE(uint n, byte[] bs, int off) |
|
{ |
|
bs[off] = (byte)(n); |
|
bs[off + 1] = (byte)(n >> 8); |
|
bs[off + 2] = (byte)(n >> 16); |
|
bs[off + 3] = (byte)(n >> 24); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static byte[] UInt32_To_LE(uint[] ns) |
|
{ |
|
byte[] bs = new byte[4 * ns.Length]; |
|
UInt32_To_LE(ns, bs, 0); |
|
return bs; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt32_To_LE(uint[] ns, byte[] bs, int off) |
|
{ |
|
for (int i = 0; i < ns.Length; ++i) |
|
{ |
|
UInt32_To_LE(ns[i], bs, off); |
|
off += 4; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt32_To_LE(uint[] ns, int nsOff, int nsLen, byte[] bs, int bsOff) |
|
{ |
|
for (int i = 0; i < nsLen; ++i) |
|
{ |
|
UInt32_To_LE(ns[nsOff + i], bs, bsOff); |
|
bsOff += 4; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static uint LE_To_UInt32(byte[] bs) |
|
{ |
|
return (uint)bs[0] |
|
| (uint)bs[1] << 8 |
|
| (uint)bs[2] << 16 |
|
| (uint)bs[3] << 24; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static uint LE_To_UInt32(byte[] bs, int off) |
|
{ |
|
return (uint)bs[off] |
|
| (uint)bs[off + 1] << 8 |
|
| (uint)bs[off + 2] << 16 |
|
| (uint)bs[off + 3] << 24; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void LE_To_UInt32(byte[] bs, int off, uint[] ns) |
|
{ |
|
for (int i = 0; i < ns.Length; ++i) |
|
{ |
|
ns[i] = LE_To_UInt32(bs, off); |
|
off += 4; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void LE_To_UInt32(byte[] bs, int bOff, uint[] ns, int nOff, int count) |
|
{ |
|
for (int i = 0; i < count; ++i) |
|
{ |
|
ns[nOff + i] = LE_To_UInt32(bs, bOff); |
|
bOff += 4; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static uint[] LE_To_UInt32(byte[] bs, int off, int count) |
|
{ |
|
uint[] ns = new uint[count]; |
|
for (int i = 0; i < ns.Length; ++i) |
|
{ |
|
ns[i] = LE_To_UInt32(bs, off); |
|
off += 4; |
|
} |
|
return ns; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static byte[] UInt64_To_LE(ulong n) |
|
{ |
|
byte[] bs = new byte[8]; |
|
UInt64_To_LE(n, bs, 0); |
|
return bs; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt64_To_LE(ulong n, byte[] bs) |
|
{ |
|
UInt32_To_LE((uint)(n), bs); |
|
UInt32_To_LE((uint)(n >> 32), bs, 4); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt64_To_LE(ulong n, byte[] bs, int off) |
|
{ |
|
UInt32_To_LE((uint)(n), bs, off); |
|
UInt32_To_LE((uint)(n >> 32), bs, off + 4); |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static byte[] UInt64_To_LE(ulong[] ns) |
|
{ |
|
byte[] bs = new byte[8 * ns.Length]; |
|
UInt64_To_LE(ns, bs, 0); |
|
return bs; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt64_To_LE(ulong[] ns, byte[] bs, int off) |
|
{ |
|
for (int i = 0; i < ns.Length; ++i) |
|
{ |
|
UInt64_To_LE(ns[i], bs, off); |
|
off += 8; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt64_To_LE(ulong[] ns, int nsOff, int nsLen, byte[] bs, int bsOff) |
|
{ |
|
for (int i = 0; i < nsLen; ++i) |
|
{ |
|
UInt64_To_LE(ns[nsOff + i], bs, bsOff); |
|
bsOff += 8; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static ulong LE_To_UInt64(byte[] bs) |
|
{ |
|
uint lo = LE_To_UInt32(bs); |
|
uint hi = LE_To_UInt32(bs, 4); |
|
return ((ulong)hi << 32) | (ulong)lo; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static ulong LE_To_UInt64(byte[] bs, int off) |
|
{ |
|
uint lo = LE_To_UInt32(bs, off); |
|
uint hi = LE_To_UInt32(bs, off + 4); |
|
return ((ulong)hi << 32) | (ulong)lo; |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void LE_To_UInt64(byte[] bs, int off, ulong[] ns) |
|
{ |
|
for (int i = 0; i < ns.Length; ++i) |
|
{ |
|
ns[i] = LE_To_UInt64(bs, off); |
|
off += 8; |
|
} |
|
} |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void LE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, int nsLen) |
|
{ |
|
for (int i = 0; i < nsLen; ++i) |
|
{ |
|
ns[nsOff + i] = LE_To_UInt64(bs, bsOff); |
|
bsOff += 8; |
|
} |
|
} |
|
} |
|
} |
|
#pragma warning restore |
|
#endif
|
|
|