aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/asn1/PrintableString.java
blob: b17ecbe3280ee297f8d76218830cf6d1fa1d60c7 (plain)
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
41
42
43
44
45
46
47
48
49
package model.asn1;

import model.asn1.exceptions.ParseException;
import model.asn1.parsing.BytesReader;
import ui.Utils;

import java.nio.charset.StandardCharsets;

/**
 * An ASN.1 PrintableString that only allows ([a-z]|[A-Z]| |[0-9]|['()+,-./:=?])*.
 */
public class PrintableString extends ASN1String {
    /**
     * The X.680 universal class tag assignment.
     */
    public static final Tag TAG = new Tag(TagClass.UNIVERSAL, false, 0x13);

    /**
     * EFFECTS: Constructs with the given string.
     * Throws {@link ParseException} if the given string is illegal (contains chars out of the PrintableString set).
     * REQUIRES: For the requirements of tag and parentTag, consult {@link ASN1Object}.
     */
    public PrintableString(Tag tag, Tag parentTag, String rawString) throws ParseException {
        super(tag, parentTag, rawString);
    }

    /**
     * EFFECTS: Parse from user input. Tags are parsed as-per {@link ASN1Object}. The value will be parsed as UTF-8 big
     * endian.
     * Throws {@link ParseException} if the encoded data is invalid:
     * - Early EOF and other cases in {@link ASN1Object}
     * - Illegal string: Contains non-printable chars
     * MODIFIES: this, encoded
     */
    public PrintableString(BytesReader encoded, boolean hasParentTag) throws ParseException {
        super(encoded, hasParentTag);
        setString(new String(Utils.byteToByte(encoded.require(getLength(), true)),
                StandardCharsets.UTF_8));
    }

    /**
     * EFFECTS: Validate the given string against PrintableString spec.
     * REQUIRES: newString != null
     */
    @Override
    protected boolean validate(String newString) {
        return newString.matches("([a-z]|[A-Z]| |[0-9]|['()+,-./:=?])*");
    }
}