From e077bfbd6cb3b7a8480293a6b209960b62f64303 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 9 Feb 2023 15:59:34 +0700 Subject: Add new Pack methods --- crypto/src/crypto/util/Pack.cs | 93 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 5 deletions(-) diff --git a/crypto/src/crypto/util/Pack.cs b/crypto/src/crypto/util/Pack.cs index 2a551fcab..e00ba39fa 100644 --- a/crypto/src/crypto/util/Pack.cs +++ b/crypto/src/crypto/util/Pack.cs @@ -391,6 +391,31 @@ namespace Org.BouncyCastle.Crypto.Utilities return bs; } + internal static byte[] UInt16_To_LE(ushort[] ns) + { + byte[] bs = new byte[2 * ns.Length]; + UInt16_To_LE(ns, bs, 0); + return bs; + } + + 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 ushort LE_To_UInt16(byte[] bs) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER @@ -413,6 +438,31 @@ namespace Org.BouncyCastle.Crypto.Utilities #endif } + 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 bOff, ushort[] ns, int nOff, int count) + { + for (int i = 0; i < count; ++i) + { + ns[nOff + i] = LE_To_UInt16(bs, bOff); + bOff += 2; + } + } + + internal static ushort[] LE_To_UInt16(byte[] bs, int off, int count) + { + ushort[] ns = new ushort[count]; + LE_To_UInt16(bs, off, ns); + return ns; + } + internal static byte[] UInt32_To_LE(uint n) { byte[] bs = new byte[4]; @@ -521,11 +571,7 @@ namespace Org.BouncyCastle.Crypto.Utilities 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; - } + LE_To_UInt32(bs, off, ns); return ns; } @@ -621,6 +667,13 @@ namespace Org.BouncyCastle.Crypto.Utilities } } + internal static ulong[] LE_To_UInt64(byte[] bs, int off, int count) + { + ulong[] ns = new ulong[count]; + LE_To_UInt64(bs, off, ns); + return ns; + } + #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static uint BE_To_UInt32(ReadOnlySpan bs) @@ -700,6 +753,16 @@ namespace Org.BouncyCastle.Crypto.Utilities return BinaryPrimitives.ReadUInt16LittleEndian(bs); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void LE_To_UInt16(ReadOnlySpan bs, Span ns) + { + for (int i = 0; i < ns.Length; ++i) + { + ns[i] = LE_To_UInt16(bs); + bs = bs[2..]; + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static uint LE_To_UInt32(ReadOnlySpan bs) { @@ -738,12 +801,32 @@ namespace Org.BouncyCastle.Crypto.Utilities BinaryPrimitives.WriteUInt16BigEndian(bs, n); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void UInt16_To_BE(ReadOnlySpan ns, Span bs) + { + for (int i = 0; i < ns.Length; ++i) + { + UInt16_To_BE(ns[i], bs); + bs = bs[2..]; + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt16_To_LE(ushort n, Span bs) { BinaryPrimitives.WriteUInt16LittleEndian(bs, n); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void UInt16_To_LE(ReadOnlySpan ns, Span bs) + { + for (int i = 0; i < ns.Length; ++i) + { + UInt16_To_LE(ns[i], bs); + bs = bs[2..]; + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt32_To_BE(uint n, Span bs) { -- cgit 1.4.1