From 36c7a0d9a777f0fccffec90aa53a5ba7c648b7de Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 9 Nov 2022 18:37:51 +0700 Subject: Crc24 perf. opts. --- crypto/src/bcpg/ArmoredInputStream.cs | 18 +++++++++--------- crypto/src/bcpg/Crc24.cs | 25 ++++++++++--------------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/crypto/src/bcpg/ArmoredInputStream.cs b/crypto/src/bcpg/ArmoredInputStream.cs index ab9a303dd..7bd7b5c04 100644 --- a/crypto/src/bcpg/ArmoredInputStream.cs +++ b/crypto/src/bcpg/ArmoredInputStream.cs @@ -53,7 +53,7 @@ namespace Org.BouncyCastle.Bcpg * * @return the offset the data starts in out. */ - private static int Decode(int in0, int in1, int in2, int in3, int[] result) + private static int Decode(int in0, int in1, int in2, int in3, byte[] result) { if (in3 < 0) throw new EndOfStreamException("unexpected end of file in armored stream."); @@ -66,7 +66,7 @@ namespace Org.BouncyCastle.Bcpg if ((b1 | b2) >= 128) throw new IOException("invalid armor"); - result[2] = ((b1 << 2) | (b2 >> 4)) & 0xff; + result[2] = (byte)((b1 << 2) | (b2 >> 4)); return 2; } else if (in3 == '=') @@ -77,8 +77,8 @@ namespace Org.BouncyCastle.Bcpg if ((b1 | b2 | b3) >= 128) throw new IOException("invalid armor"); - result[1] = ((b1 << 2) | (b2 >> 4)) & 0xff; - result[2] = ((b2 << 4) | (b3 >> 2)) & 0xff; + result[1] = (byte)((b1 << 2) | (b2 >> 4)); + result[2] = (byte)((b2 << 4) | (b3 >> 2)); return 1; } else @@ -90,9 +90,9 @@ namespace Org.BouncyCastle.Bcpg if ((b1 | b2 | b3 | b4) >= 128) throw new IOException("invalid armor"); - result[0] = ((b1 << 2) | (b2 >> 4)) & 0xff; - result[1] = ((b2 << 4) | (b3 >> 2)) & 0xff; - result[2] = ((b3 << 6) | b4) & 0xff; + result[0] = (byte)((b1 << 2) | (b2 >> 4)); + result[1] = (byte)((b2 << 4) | (b3 >> 2)); + result[2] = (byte)((b3 << 6) | b4); return 0; } } @@ -105,7 +105,7 @@ namespace Org.BouncyCastle.Bcpg Stream input; bool start = true; - int[] outBuf = new int[3]; + byte[] outBuf = new byte[3]; int bufPtr = 3; Crc24 crc = new Crc24(); bool crcFound = false; @@ -498,7 +498,7 @@ namespace Org.BouncyCastle.Bcpg c = outBuf[bufPtr++]; - crc.Update(c); + crc.Update((byte)c); return c; } diff --git a/crypto/src/bcpg/Crc24.cs b/crypto/src/bcpg/Crc24.cs index 5521bbb64..54c9f2f5a 100644 --- a/crypto/src/bcpg/Crc24.cs +++ b/crypto/src/bcpg/Crc24.cs @@ -2,39 +2,34 @@ using System; namespace Org.BouncyCastle.Bcpg { - public class Crc24 + public sealed class Crc24 { private const int Crc24Init = 0x0b704ce; private const int Crc24Poly = 0x1864cfb; - private int crc = Crc24Init; + private int m_crc = Crc24Init; public Crc24() { } - public void Update( - int b) + public void Update(byte b) { - crc ^= b << 16; + m_crc ^= (int)b << 16; for (int i = 0; i < 8; i++) { - crc <<= 1; - if ((crc & 0x1000000) != 0) - { - crc ^= Crc24Poly; - } + int carry = -((m_crc >> 23) & 1) & Crc24Poly; + + m_crc <<= 1; + m_crc ^= carry; } } - public int Value - { - get { return crc; } - } + public int Value => m_crc; public void Reset() { - crc = Crc24Init; + m_crc = Crc24Init; } } } -- cgit 1.4.1