summary refs log tree commit diff
path: root/crypto/src/bcpg/sig/RegularExpression.cs
blob: c4b48ea2aa49d15cc1348c6e78a2d8584d52c94f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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);
        }
    }
}