aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/asn1/UTF8String.java
blob: e6b101e51c30c73da12056caf877cf57ccec0368 (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
package model.asn1;

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

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * Represents an ASN.1 UTF8String type. It accepts any UTF-8 chars. Because UTF-8 character set is large and its chars
 * have variable width, this implementation does not validate against legal UTF-8 characters.
 */
public class UTF8String extends ASN1String {
    /**
     * The X.680 universal class tag assignment.
     */
    public static final Tag TAG = new Tag(TagClass.UNIVERSAL, false, 0x0C);

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

    /**
     * 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.
     * MODIFIES: this, encoded
     */
    public UTF8String(BytesReader encoded, boolean hasParentTag) throws ParseException, IllegalArgumentException {
        super(encoded, hasParentTag);
        setString(new String(Utils.byteToByte(encoded.require(getLength(), true)),
                StandardCharsets.UTF_8));
    }
}