From 37a36810c786707b204108095c8f23214c73df70 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 20 Jun 2024 20:39:59 +0700 Subject: Add Try... methods to DerInteger for small value accessors --- crypto/src/asn1/DerInteger.cs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'crypto/src/asn1') diff --git a/crypto/src/asn1/DerInteger.cs b/crypto/src/asn1/DerInteger.cs index d68a71e9d..666723527 100644 --- a/crypto/src/asn1/DerInteger.cs +++ b/crypto/src/asn1/DerInteger.cs @@ -209,6 +209,45 @@ namespace Org.BouncyCastle.Asn1 } } + public bool TryGetIntPositiveValueExact(out int value) + { + int count = bytes.Length - start; + if (count > 4 || (count == 4 && 0 != (bytes[start] & 0x80))) + { + value = default; + return false; + } + + value = IntValue(bytes, start, SignExtUnsigned); + return true; + } + + public bool TryGetIntValueExact(out int value) + { + int count = bytes.Length - start; + if (count > 4) + { + value = default; + return false; + } + + value = IntValue(bytes, start, SignExtSigned); + return true; + } + + public bool TryGetLongValueExact(out long value) + { + int count = bytes.Length - start; + if (count > 8) + { + value = default; + return false; + } + + value = LongValue(bytes, start, SignExtSigned); + return true; + } + internal override IAsn1Encoding GetEncoding(int encoding) { return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.Integer, bytes); -- cgit 1.4.1