Skip to content

Commit e077bfb

Browse files
committed
Add new Pack methods
1 parent cbf0bca commit e077bfb

File tree

1 file changed

+88
-5
lines changed

1 file changed

+88
-5
lines changed

crypto/src/crypto/util/Pack.cs

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,31 @@ internal static byte[] UInt16_To_LE(ushort n)
391391
return bs;
392392
}
393393

394+
internal static byte[] UInt16_To_LE(ushort[] ns)
395+
{
396+
byte[] bs = new byte[2 * ns.Length];
397+
UInt16_To_LE(ns, bs, 0);
398+
return bs;
399+
}
400+
401+
internal static void UInt16_To_LE(ushort[] ns, byte[] bs, int off)
402+
{
403+
for (int i = 0; i < ns.Length; ++i)
404+
{
405+
UInt16_To_LE(ns[i], bs, off);
406+
off += 2;
407+
}
408+
}
409+
410+
internal static void UInt16_To_LE(ushort[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
411+
{
412+
for (int i = 0; i < nsLen; ++i)
413+
{
414+
UInt16_To_LE(ns[nsOff + i], bs, bsOff);
415+
bsOff += 2;
416+
}
417+
}
418+
394419
internal static ushort LE_To_UInt16(byte[] bs)
395420
{
396421
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
@@ -413,6 +438,31 @@ internal static ushort LE_To_UInt16(byte[] bs, int off)
413438
#endif
414439
}
415440

441+
internal static void LE_To_UInt16(byte[] bs, int off, ushort[] ns)
442+
{
443+
for (int i = 0; i < ns.Length; ++i)
444+
{
445+
ns[i] = LE_To_UInt16(bs, off);
446+
off += 2;
447+
}
448+
}
449+
450+
internal static void LE_To_UInt16(byte[] bs, int bOff, ushort[] ns, int nOff, int count)
451+
{
452+
for (int i = 0; i < count; ++i)
453+
{
454+
ns[nOff + i] = LE_To_UInt16(bs, bOff);
455+
bOff += 2;
456+
}
457+
}
458+
459+
internal static ushort[] LE_To_UInt16(byte[] bs, int off, int count)
460+
{
461+
ushort[] ns = new ushort[count];
462+
LE_To_UInt16(bs, off, ns);
463+
return ns;
464+
}
465+
416466
internal static byte[] UInt32_To_LE(uint n)
417467
{
418468
byte[] bs = new byte[4];
@@ -521,11 +571,7 @@ internal static void LE_To_UInt32(byte[] bs, int bOff, uint[] ns, int nOff, int
521571
internal static uint[] LE_To_UInt32(byte[] bs, int off, int count)
522572
{
523573
uint[] ns = new uint[count];
524-
for (int i = 0; i < ns.Length; ++i)
525-
{
526-
ns[i] = LE_To_UInt32(bs, off);
527-
off += 4;
528-
}
574+
LE_To_UInt32(bs, off, ns);
529575
return ns;
530576
}
531577

@@ -621,6 +667,13 @@ internal static void LE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, i
621667
}
622668
}
623669

670+
internal static ulong[] LE_To_UInt64(byte[] bs, int off, int count)
671+
{
672+
ulong[] ns = new ulong[count];
673+
LE_To_UInt64(bs, off, ns);
674+
return ns;
675+
}
676+
624677
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
625678
[MethodImpl(MethodImplOptions.AggressiveInlining)]
626679
internal static uint BE_To_UInt32(ReadOnlySpan<byte> bs)
@@ -700,6 +753,16 @@ internal static ushort LE_To_UInt16(ReadOnlySpan<byte> bs)
700753
return BinaryPrimitives.ReadUInt16LittleEndian(bs);
701754
}
702755

756+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
757+
internal static void LE_To_UInt16(ReadOnlySpan<byte> bs, Span<ushort> ns)
758+
{
759+
for (int i = 0; i < ns.Length; ++i)
760+
{
761+
ns[i] = LE_To_UInt16(bs);
762+
bs = bs[2..];
763+
}
764+
}
765+
703766
[MethodImpl(MethodImplOptions.AggressiveInlining)]
704767
internal static uint LE_To_UInt32(ReadOnlySpan<byte> bs)
705768
{
@@ -738,12 +801,32 @@ internal static void UInt16_To_BE(ushort n, Span<byte> bs)
738801
BinaryPrimitives.WriteUInt16BigEndian(bs, n);
739802
}
740803

804+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
805+
internal static void UInt16_To_BE(ReadOnlySpan<ushort> ns, Span<byte> bs)
806+
{
807+
for (int i = 0; i < ns.Length; ++i)
808+
{
809+
UInt16_To_BE(ns[i], bs);
810+
bs = bs[2..];
811+
}
812+
}
813+
741814
[MethodImpl(MethodImplOptions.AggressiveInlining)]
742815
internal static void UInt16_To_LE(ushort n, Span<byte> bs)
743816
{
744817
BinaryPrimitives.WriteUInt16LittleEndian(bs, n);
745818
}
746819

820+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
821+
internal static void UInt16_To_LE(ReadOnlySpan<ushort> ns, Span<byte> bs)
822+
{
823+
for (int i = 0; i < ns.Length; ++i)
824+
{
825+
UInt16_To_LE(ns[i], bs);
826+
bs = bs[2..];
827+
}
828+
}
829+
747830
[MethodImpl(MethodImplOptions.AggressiveInlining)]
748831
internal static void UInt32_To_BE(uint n, Span<byte> bs)
749832
{

0 commit comments

Comments
 (0)