Pack
using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
namespace Org.BouncyCastle.Crypto.Utilities
{
internal static class Pack
{
internal static void UInt16_To_BE(ushort n, byte[] bs)
{
BinaryPrimitives.WriteUInt16BigEndian(bs, n);
}
internal static void UInt16_To_BE(ushort n, byte[] bs, int off)
{
BinaryPrimitives.WriteUInt16BigEndian(bs.AsSpan(off), n);
}
internal static void UInt16_To_BE(ushort[] ns, byte[] bs, int off)
{
for (int i = 0; i < ns.Length; i++) {
UInt16_To_BE(ns[i], bs, off);
off += 2;
}
}
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;
}
}
internal static byte[] UInt16_To_BE(ushort n)
{
byte[] array = new byte[2];
UInt16_To_BE(n, array);
return array;
}
internal static byte[] UInt16_To_BE(ushort[] ns)
{
return UInt16_To_BE(ns, 0, ns.Length);
}
internal static byte[] UInt16_To_BE(ushort[] ns, int nsOff, int nsLen)
{
byte[] array = new byte[2 * nsLen];
UInt16_To_BE(ns, nsOff, nsLen, array, 0);
return array;
}
internal static ushort BE_To_UInt16(byte[] bs)
{
return BinaryPrimitives.ReadUInt16BigEndian(bs);
}
internal static ushort BE_To_UInt16(byte[] bs, int off)
{
return BinaryPrimitives.ReadUInt16BigEndian(bs.AsSpan(off));
}
internal static void BE_To_UInt16(byte[] bs, int bsOff, ushort[] ns, int nsOff)
{
ns[nsOff] = BE_To_UInt16(bs, bsOff);
}
internal static void BE_To_UInt16(byte[] bs, int off, ushort[] ns)
{
for (int i = 0; i < ns.Length; i++) {
ns[i] = BE_To_UInt16(bs, off);
off += 2;
}
}
internal static void BE_To_UInt16(byte[] bs, int bsOff, ushort[] ns, int nsOff, int nsLen)
{
for (int i = 0; i < nsLen; i++) {
ns[nsOff + i] = BE_To_UInt16(bs, bsOff);
bsOff += 2;
}
}
internal static ushort[] BE_To_UInt16(byte[] bs, int off, int count)
{
ushort[] array = new ushort[count];
BE_To_UInt16(bs, off, array);
return array;
}
internal static void UInt24_To_BE(uint n, byte[] bs)
{
bs[0] = (byte)(n >> 16);
bs[1] = (byte)(n >> 8);
bs[2] = (byte)n;
}
internal static void UInt24_To_BE(uint n, byte[] bs, int off)
{
bs[off] = (byte)(n >> 16);
bs[off + 1] = (byte)(n >> 8);
bs[off + 2] = (byte)n;
}
internal static uint BE_To_UInt24(byte[] bs)
{
return (uint)((bs[0] << 16) | (bs[1] << 8) | bs[2]);
}
internal static uint BE_To_UInt24(byte[] bs, int off)
{
return (uint)((bs[off] << 16) | (bs[off + 1] << 8) | bs[off + 2]);
}
internal static void UInt32_To_BE(uint n, byte[] bs)
{
BinaryPrimitives.WriteUInt32BigEndian(bs, n);
}
internal static void UInt32_To_BE(uint n, byte[] bs, int off)
{
BinaryPrimitives.WriteUInt32BigEndian(bs.AsSpan(off), n);
}
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;
}
}
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;
}
}
internal static byte[] UInt32_To_BE(uint n)
{
byte[] array = new byte[4];
UInt32_To_BE(n, array);
return array;
}
internal static byte[] UInt32_To_BE(uint[] ns)
{
return UInt32_To_BE(ns, 0, ns.Length);
}
internal static byte[] UInt32_To_BE(uint[] ns, int nsOff, int nsLen)
{
byte[] array = new byte[4 * nsLen];
UInt32_To_BE(ns, nsOff, nsLen, array, 0);
return array;
}
internal static void UInt32_To_BE_High(uint n, byte[] bs, int off, int len)
{
int num = 24;
bs[off] = (byte)(n >> num);
for (int i = 1; i < len; i++) {
num -= 8;
bs[off + i] = (byte)(n >> num);
}
}
internal static void UInt32_To_BE_Low(uint n, byte[] bs, int off, int len)
{
UInt32_To_BE_High(n << (4 - len << 3), bs, off, len);
}
internal static uint BE_To_UInt32(byte[] bs)
{
return BinaryPrimitives.ReadUInt32BigEndian(bs);
}
internal static uint BE_To_UInt32(byte[] bs, int off)
{
return BinaryPrimitives.ReadUInt32BigEndian(bs.AsSpan(off));
}
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;
}
}
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;
}
}
internal static uint[] BE_To_UInt32(byte[] bs, int off, int count)
{
uint[] array = new uint[count];
BE_To_UInt32(bs, off, array);
return array;
}
internal static uint BE_To_UInt32_High(byte[] bs, int off, int len)
{
return BE_To_UInt32_Low(bs, off, len) << (4 - len << 3);
}
internal static uint BE_To_UInt32_Low(byte[] bs, int off, int len)
{
uint num = bs[off];
for (int i = 1; i < len; i++) {
num <<= 8;
num |= bs[off + i];
}
return num;
}
internal static void UInt64_To_BE(ulong n, byte[] bs)
{
BinaryPrimitives.WriteUInt64BigEndian(bs, n);
}
internal static void UInt64_To_BE(ulong n, byte[] bs, int off)
{
BinaryPrimitives.WriteUInt64BigEndian(bs.AsSpan(off), n);
}
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;
}
}
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;
}
}
internal static byte[] UInt64_To_BE(ulong n)
{
byte[] array = new byte[8];
UInt64_To_BE(n, array);
return array;
}
internal static byte[] UInt64_To_BE(ulong[] ns)
{
return UInt64_To_BE(ns, 0, ns.Length);
}
internal static byte[] UInt64_To_BE(ulong[] ns, int nsOff, int nsLen)
{
byte[] array = new byte[8 * nsLen];
UInt64_To_BE(ns, nsOff, nsLen, array, 0);
return array;
}
internal static void UInt64_To_BE_High(ulong n, byte[] bs, int off, int len)
{
int num = 56;
bs[off] = (byte)(n >> num);
for (int i = 1; i < len; i++) {
num -= 8;
bs[off + i] = (byte)(n >> num);
}
}
internal static void UInt64_To_BE_Low(ulong n, byte[] bs, int off, int len)
{
UInt64_To_BE_High(n << (8 - len << 3), bs, off, len);
}
internal static ulong BE_To_UInt64(byte[] bs)
{
return BinaryPrimitives.ReadUInt64BigEndian(bs);
}
internal static ulong BE_To_UInt64(byte[] bs, int off)
{
return BinaryPrimitives.ReadUInt64BigEndian(bs.AsSpan(off));
}
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;
}
}
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;
}
}
internal static ulong[] BE_To_UInt64(byte[] bs, int off, int count)
{
ulong[] array = new ulong[count];
BE_To_UInt64(bs, off, array);
return array;
}
internal static ulong BE_To_UInt64_High(byte[] bs, int off, int len)
{
return BE_To_UInt64_Low(bs, off, len) << (8 - len << 3);
}
internal static ulong BE_To_UInt64_Low(byte[] bs, int off, int len)
{
ulong num = bs[off];
for (int i = 1; i < len; i++) {
num <<= 8;
num |= bs[off + i];
}
return num;
}
internal static void UInt16_To_LE(ushort n, byte[] bs)
{
BinaryPrimitives.WriteUInt16LittleEndian(bs, n);
}
internal static void UInt16_To_LE(ushort n, byte[] bs, int off)
{
BinaryPrimitives.WriteUInt16LittleEndian(bs.AsSpan(off), n);
}
internal static void UInt16_To_LE(ushort[] ns, byte[] bs, int off)
{
for (int i = 0; i < ns.Length; i++) {
UInt16_To_LE(ns[i], bs, off);
off += 2;
}
}
internal static void UInt16_To_LE(ushort[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
{
for (int i = 0; i < nsLen; i++) {
UInt16_To_LE(ns[nsOff + i], bs, bsOff);
bsOff += 2;
}
}
internal static byte[] UInt16_To_LE(ushort n)
{
byte[] array = new byte[2];
UInt16_To_LE(n, array);
return array;
}
internal static byte[] UInt16_To_LE(ushort[] ns)
{
return UInt16_To_LE(ns, 0, ns.Length);
}
internal static byte[] UInt16_To_LE(ushort[] ns, int nsOff, int nsLen)
{
byte[] array = new byte[2 * nsLen];
UInt16_To_LE(ns, nsOff, nsLen, array, 0);
return array;
}
internal static ushort LE_To_UInt16(byte[] bs)
{
return BinaryPrimitives.ReadUInt16LittleEndian(bs);
}
internal static ushort LE_To_UInt16(byte[] bs, int off)
{
return BinaryPrimitives.ReadUInt16LittleEndian(bs.AsSpan(off));
}
internal static void LE_To_UInt16(byte[] bs, int off, ushort[] ns)
{
for (int i = 0; i < ns.Length; i++) {
ns[i] = LE_To_UInt16(bs, off);
off += 2;
}
}
internal static void LE_To_UInt16(byte[] bs, int bsOff, ushort[] ns, int nsOff, int nsLen)
{
for (int i = 0; i < nsLen; i++) {
ns[nsOff + i] = LE_To_UInt16(bs, bsOff);
bsOff += 2;
}
}
internal static ushort[] LE_To_UInt16(byte[] bs, int off, int count)
{
ushort[] array = new ushort[count];
LE_To_UInt16(bs, off, array);
return array;
}
internal static void UInt32_To_LE(uint n, byte[] bs)
{
BinaryPrimitives.WriteUInt32LittleEndian(bs, n);
}
internal static void UInt32_To_LE(uint n, byte[] bs, int off)
{
BinaryPrimitives.WriteUInt32LittleEndian(bs.AsSpan(off), n);
}
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;
}
}
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;
}
}
internal static byte[] UInt32_To_LE(uint n)
{
byte[] array = new byte[4];
UInt32_To_LE(n, array);
return array;
}
internal static byte[] UInt32_To_LE(uint[] ns)
{
return UInt32_To_LE(ns, 0, ns.Length);
}
internal static byte[] UInt32_To_LE(uint[] ns, int nsOff, int nsLen)
{
byte[] array = new byte[4 * nsLen];
UInt32_To_LE(ns, nsOff, nsLen, array, 0);
return array;
}
internal static void UInt32_To_LE_High(uint n, byte[] bs, int off, int len)
{
UInt32_To_LE_Low(n >> (4 - len << 3), bs, off, len);
}
internal static void UInt32_To_LE_Low(uint n, byte[] bs, int off, int len)
{
bs[off] = (byte)n;
for (int i = 1; i < len; i++) {
n >>= 8;
bs[off + i] = (byte)n;
}
}
internal static uint LE_To_UInt24(byte[] bs, int off)
{
return (uint)(bs[off] | (bs[off + 1] << 8) | (bs[off + 2] << 16));
}
internal static uint LE_To_UInt32(byte[] bs)
{
return BinaryPrimitives.ReadUInt32LittleEndian(bs);
}
internal static uint LE_To_UInt32(byte[] bs, int off)
{
return BinaryPrimitives.ReadUInt32LittleEndian(bs.AsSpan(off));
}
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;
}
}
internal static void LE_To_UInt32(byte[] bs, int bsOff, uint[] ns, int nsOff, int nsLen)
{
for (int i = 0; i < nsLen; i++) {
ns[nsOff + i] = LE_To_UInt32(bs, bsOff);
bsOff += 4;
}
}
internal static uint[] LE_To_UInt32(byte[] bs, int off, int count)
{
uint[] array = new uint[count];
LE_To_UInt32(bs, off, array);
return array;
}
internal static uint LE_To_UInt32_High(byte[] bs, int off, int len)
{
return LE_To_UInt32_Low(bs, off, len) << (4 - len << 3);
}
internal static uint LE_To_UInt32_Low(byte[] bs, int off, int len)
{
uint num = bs[off];
int num2 = 0;
for (int i = 1; i < len; i++) {
num2 += 8;
num = (uint)((int)num | (bs[off + i] << num2));
}
return num;
}
internal static void UInt64_To_LE(ulong n, byte[] bs)
{
BinaryPrimitives.WriteUInt64LittleEndian(bs, n);
}
internal static void UInt64_To_LE(ulong n, byte[] bs, int off)
{
BinaryPrimitives.WriteUInt64LittleEndian(bs.AsSpan(off), n);
}
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;
}
}
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;
}
}
internal static byte[] UInt64_To_LE(ulong n)
{
byte[] array = new byte[8];
UInt64_To_LE(n, array);
return array;
}
internal static byte[] UInt64_To_LE(ulong[] ns)
{
return UInt64_To_LE(ns, 0, ns.Length);
}
internal static byte[] UInt64_To_LE(ulong[] ns, int nsOff, int nsLen)
{
byte[] array = new byte[8 * nsLen];
UInt64_To_LE(ns, nsOff, nsLen, array, 0);
return array;
}
internal static void UInt64_To_LE_High(ulong n, byte[] bs, int off, int len)
{
UInt64_To_LE_Low(n >> (8 - len << 3), bs, off, len);
}
internal static void UInt64_To_LE_Low(ulong n, byte[] bs, int off, int len)
{
bs[off] = (byte)n;
for (int i = 1; i < len; i++) {
n >>= 8;
bs[off + i] = (byte)n;
}
}
internal static ulong LE_To_UInt64(byte[] bs)
{
return BinaryPrimitives.ReadUInt64LittleEndian(bs);
}
internal static ulong LE_To_UInt64(byte[] bs, int off)
{
return BinaryPrimitives.ReadUInt64LittleEndian(bs.AsSpan(off));
}
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;
}
}
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;
}
}
internal static ulong[] LE_To_UInt64(byte[] bs, int off, int count)
{
ulong[] array = new ulong[count];
LE_To_UInt64(bs, off, array);
return array;
}
internal static ulong LE_To_UInt64_High(byte[] bs, int off, int len)
{
return LE_To_UInt64_Low(bs, off, len) << (8 - len << 3);
}
internal static ulong LE_To_UInt64_Low(byte[] bs, int off, int len)
{
ulong num = bs[off];
int num2 = 0;
for (int i = 1; i < len; i++) {
num2 += 8;
num |= (ulong)bs[off + i] << num2;
}
return num;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ushort BE_To_UInt16(ReadOnlySpan<byte> bs)
{
return BinaryPrimitives.ReadUInt16BigEndian(bs);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ushort BE_To_UInt16(ReadOnlySpan<byte> bs, int off)
{
return BinaryPrimitives.ReadUInt16BigEndian(bs.Slice(off, bs.Length - off));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void BE_To_UInt16(ReadOnlySpan<byte> bs, Span<ushort> ns)
{
for (int i = 0; i < ns.Length; i++) {
ns[i] = BE_To_UInt16(bs);
bs = bs.Slice(2, bs.Length - 2);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static uint BE_To_UInt32(ReadOnlySpan<byte> bs)
{
return BinaryPrimitives.ReadUInt32BigEndian(bs);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static uint BE_To_UInt32(ReadOnlySpan<byte> bs, int off)
{
return BinaryPrimitives.ReadUInt32BigEndian(bs.Slice(off, bs.Length - off));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void BE_To_UInt32(ReadOnlySpan<byte> bs, Span<uint> ns)
{
for (int i = 0; i < ns.Length; i++) {
ns[i] = BE_To_UInt32(bs);
bs = bs.Slice(4, bs.Length - 4);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static uint BE_To_UInt32_High(ReadOnlySpan<byte> bs)
{
return BE_To_UInt32_Low(bs) << (4 - bs.Length << 3);
}
internal static uint BE_To_UInt32_Low(ReadOnlySpan<byte> bs)
{
int length = bs.Length;
uint num = bs[0];
for (int i = 1; i < length; i++) {
num <<= 8;
num |= bs[i];
}
return num;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ulong BE_To_UInt64(ReadOnlySpan<byte> bs)
{
return BinaryPrimitives.ReadUInt64BigEndian(bs);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ulong BE_To_UInt64(ReadOnlySpan<byte> bs, int off)
{
return BinaryPrimitives.ReadUInt64BigEndian(bs.Slice(off, bs.Length - off));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void BE_To_UInt64(ReadOnlySpan<byte> bs, Span<ulong> ns)
{
for (int i = 0; i < ns.Length; i++) {
ns[i] = BE_To_UInt64(bs);
bs = bs.Slice(8, bs.Length - 8);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ulong BE_To_UInt64_High(ReadOnlySpan<byte> bs)
{
return BE_To_UInt64_Low(bs) << (8 - bs.Length << 3);
}
internal static ulong BE_To_UInt64_Low(ReadOnlySpan<byte> bs)
{
int length = bs.Length;
ulong num = bs[0];
for (int i = 1; i < length; i++) {
num <<= 8;
num |= bs[i];
}
return num;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ushort LE_To_UInt16(ReadOnlySpan<byte> bs)
{
return BinaryPrimitives.ReadUInt16LittleEndian(bs);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ushort LE_To_UInt16(ReadOnlySpan<byte> bs, int off)
{
return BinaryPrimitives.ReadUInt16LittleEndian(bs.Slice(off, bs.Length - off));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void LE_To_UInt16(ReadOnlySpan<byte> bs, Span<ushort> ns)
{
for (int i = 0; i < ns.Length; i++) {
ns[i] = LE_To_UInt16(bs);
bs = bs.Slice(2, bs.Length - 2);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static uint LE_To_UInt24(ReadOnlySpan<byte> bs)
{
return (uint)(bs[0] | (bs[1] << 8) | (bs[2] << 16));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static uint LE_To_UInt24(ReadOnlySpan<byte> bs, int off)
{
return (uint)(bs[off] | (bs[off + 1] << 8) | (bs[off + 2] << 16));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static uint LE_To_UInt32(ReadOnlySpan<byte> bs)
{
return BinaryPrimitives.ReadUInt32LittleEndian(bs);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static uint LE_To_UInt32(ReadOnlySpan<byte> bs, int off)
{
return BinaryPrimitives.ReadUInt32LittleEndian(bs.Slice(off, bs.Length - off));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void LE_To_UInt32(ReadOnlySpan<byte> bs, Span<uint> ns)
{
for (int i = 0; i < ns.Length; i++) {
ns[i] = LE_To_UInt32(bs);
bs = bs.Slice(4, bs.Length - 4);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static uint LE_To_UInt32_High(ReadOnlySpan<byte> bs)
{
return LE_To_UInt32_Low(bs) << (4 - bs.Length << 3);
}
internal static uint LE_To_UInt32_Low(ReadOnlySpan<byte> bs)
{
int length = bs.Length;
uint num = bs[0];
int num2 = 0;
for (int i = 1; i < length; i++) {
num2 += 8;
num = (uint)((int)num | (bs[i] << num2));
}
return num;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ulong LE_To_UInt64(ReadOnlySpan<byte> bs)
{
return BinaryPrimitives.ReadUInt64LittleEndian(bs);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ulong LE_To_UInt64(ReadOnlySpan<byte> bs, int off)
{
return BinaryPrimitives.ReadUInt64LittleEndian(bs.Slice(off, bs.Length - off));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void LE_To_UInt64(ReadOnlySpan<byte> bs, Span<ulong> ns)
{
for (int i = 0; i < ns.Length; i++) {
ns[i] = LE_To_UInt64(bs);
bs = bs.Slice(8, bs.Length - 8);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ulong LE_To_UInt64_High(ReadOnlySpan<byte> bs)
{
return LE_To_UInt64_Low(bs) << (8 - bs.Length << 3);
}
internal static ulong LE_To_UInt64_Low(ReadOnlySpan<byte> bs)
{
int length = bs.Length;
ulong num = bs[0];
int num2 = 0;
for (int i = 1; i < length; i++) {
num2 += 8;
num |= (ulong)bs[i] << num2;
}
return num;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt16_To_BE(ushort n, Span<byte> bs)
{
BinaryPrimitives.WriteUInt16BigEndian(bs, n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt16_To_BE(ushort n, Span<byte> bs, int off)
{
BinaryPrimitives.WriteUInt16BigEndian(bs.Slice(off, bs.Length - off), n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt16_To_BE(ReadOnlySpan<ushort> ns, Span<byte> bs)
{
for (int i = 0; i < ns.Length; i++) {
UInt16_To_BE(ns[i], bs);
bs = bs.Slice(2, bs.Length - 2);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt16_To_LE(ushort n, Span<byte> bs)
{
BinaryPrimitives.WriteUInt16LittleEndian(bs, n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt16_To_LE(ushort n, Span<byte> bs, int off)
{
BinaryPrimitives.WriteUInt16LittleEndian(bs.Slice(off, bs.Length - off), n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt16_To_LE(ReadOnlySpan<ushort> ns, Span<byte> bs)
{
for (int i = 0; i < ns.Length; i++) {
UInt16_To_LE(ns[i], bs);
bs = bs.Slice(2, bs.Length - 2);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt32_To_BE(uint n, Span<byte> bs)
{
BinaryPrimitives.WriteUInt32BigEndian(bs, n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt32_To_BE(uint n, Span<byte> bs, int off)
{
BinaryPrimitives.WriteUInt32BigEndian(bs.Slice(off, bs.Length - off), n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt32_To_BE(ReadOnlySpan<uint> ns, Span<byte> bs)
{
for (int i = 0; i < ns.Length; i++) {
UInt32_To_BE(ns[i], bs);
bs = bs.Slice(4, bs.Length - 4);
}
}
internal static void UInt32_To_BE_High(uint n, Span<byte> bs)
{
int length = bs.Length;
int num = 24;
bs[0] = (byte)(n >> num);
for (int i = 1; i < length; i++) {
num -= 8;
bs[i] = (byte)(n >> num);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt32_To_BE_Low(uint n, Span<byte> bs)
{
UInt32_To_BE_High(n << (4 - bs.Length << 3), bs);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt32_To_LE(uint n, Span<byte> bs)
{
BinaryPrimitives.WriteUInt32LittleEndian(bs, n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt32_To_LE(uint n, Span<byte> bs, int off)
{
BinaryPrimitives.WriteUInt32LittleEndian(bs.Slice(off, bs.Length - off), n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt32_To_LE(ReadOnlySpan<uint> ns, Span<byte> bs)
{
for (int i = 0; i < ns.Length; i++) {
UInt32_To_LE(ns[i], bs);
bs = bs.Slice(4, bs.Length - 4);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt32_To_LE_High(uint n, Span<byte> bs)
{
UInt32_To_LE_Low(n >> (4 - bs.Length << 3), bs);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt32_To_LE_Low(uint n, Span<byte> bs)
{
int length = bs.Length;
bs[0] = (byte)n;
for (int i = 1; i < length; i++) {
n >>= 8;
bs[i] = (byte)n;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt64_To_BE(ulong n, Span<byte> bs)
{
BinaryPrimitives.WriteUInt64BigEndian(bs, n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt64_To_BE(ulong n, Span<byte> bs, int off)
{
BinaryPrimitives.WriteUInt64BigEndian(bs.Slice(off, bs.Length - off), n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt64_To_BE(ReadOnlySpan<ulong> ns, Span<byte> bs)
{
for (int i = 0; i < ns.Length; i++) {
UInt64_To_BE(ns[i], bs);
bs = bs.Slice(8, bs.Length - 8);
}
}
internal static void UInt64_To_BE_High(ulong n, Span<byte> bs)
{
int length = bs.Length;
int num = 56;
bs[0] = (byte)(n >> num);
for (int i = 1; i < length; i++) {
num -= 8;
bs[i] = (byte)(n >> num);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt64_To_BE_Low(ulong n, Span<byte> bs)
{
UInt64_To_BE_High(n << (8 - bs.Length << 3), bs);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt64_To_LE(ulong n, Span<byte> bs)
{
BinaryPrimitives.WriteUInt64LittleEndian(bs, n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt64_To_LE(ulong n, Span<byte> bs, int off)
{
BinaryPrimitives.WriteUInt64LittleEndian(bs.Slice(off, bs.Length - off), n);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt64_To_LE(ReadOnlySpan<ulong> ns, Span<byte> bs)
{
for (int i = 0; i < ns.Length; i++) {
UInt64_To_LE(ns[i], bs);
bs = bs.Slice(8, bs.Length - 8);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt64_To_LE_High(ulong n, Span<byte> bs)
{
UInt64_To_LE_Low(n >> (8 - bs.Length << 3), bs);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void UInt64_To_LE_Low(ulong n, Span<byte> bs)
{
int length = bs.Length;
bs[0] = (byte)n;
for (int i = 1; i < length; i++) {
n >>= 8;
bs[i] = (byte)n;
}
}
}
}