summary refs log tree commit diff
path: root/crypto/src/bcpg/sig/RegularExpression.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/bcpg/sig/RegularExpression.cs')
-rw-r--r--crypto/src/bcpg/sig/RegularExpression.cs40
1 files changed, 40 insertions, 0 deletions
diff --git a/crypto/src/bcpg/sig/RegularExpression.cs b/crypto/src/bcpg/sig/RegularExpression.cs
new file mode 100644

index 000000000..c4b48ea2a --- /dev/null +++ b/crypto/src/bcpg/sig/RegularExpression.cs
@@ -0,0 +1,40 @@ +using System; +using System.Text.RegularExpressions; + +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Bcpg.Sig +{ + /** + * Regexp Packet - RFC 4880 5.2.3.14. Note: the RFC says the byte encoding is to be null terminated. + */ + public class RegularExpression + : SignatureSubpacket + { + public RegularExpression(bool critical, bool isLongLength, byte[] data) + : base(SignatureSubpacketTag.RegExp, critical, isLongLength, data) + { + if (data[data.Length - 1] != 0) + throw new ArgumentException("data in regex missing null termination"); + } + + public RegularExpression(bool critical, string regex) + : base(SignatureSubpacketTag.RegExp, critical, false, ToNullTerminatedUtf8ByteArray(regex)) + { + } + + public string Regex + { + // last byte is null terminator + get { return Strings.FromUtf8ByteArray(data, 0, data.Length - 1); } + } + + public byte[] GetRawRegex() => Arrays.Clone(data); + + private static byte[] ToNullTerminatedUtf8ByteArray(string str) + { + byte[] utf8 = Strings.ToUtf8ByteArray(str); + return Arrays.Append(utf8, 0x00); + } + } +}