aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/asn1/IA5String.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/model/asn1/IA5String.java')
-rw-r--r--src/main/model/asn1/IA5String.java53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/main/model/asn1/IA5String.java b/src/main/model/asn1/IA5String.java
new file mode 100644
index 0000000..ea5cf91
--- /dev/null
+++ b/src/main/model/asn1/IA5String.java
@@ -0,0 +1,53 @@
+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 IA5String type. It is a string that is restricted to ISO 646 / T.50 characters.
+ */
+public class IA5String extends ASN1String {
+ /**
+ * The X.680 universal class tag assignment.
+ */
+ public static final Tag TAG = new Tag(TagClass.UNIVERSAL, false, 0x16);
+
+ /**
+ * EFFECTS: Constructs an IA5String with the given tag and string.
+ * Throws {@link ParseException} if the string is invalid. It must only contain T.50 chars.
+ * REQUIRES: For the requirements of tag and parentTag, consult {@link ASN1Object}.
+ */
+ public IA5String(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:
+ * - Illegal string (containing non-T.50 chars)
+ * - Early EOF
+ * - Other cases in {@link ASN1Object}
+ * MODIFIES: this, encoded
+ */
+ public IA5String(BytesReader encoded, boolean hasParentTag) throws ParseException {
+ super(encoded, hasParentTag);
+ setString(new String(Utils.byteToByte(encoded.require(getLength(), true)),
+ StandardCharsets.UTF_8));
+ }
+
+ /**
+ * EFFECTS: Checks whether the given string only contains ISO 646 / T.50 chars.
+ */
+ @Override
+ protected boolean validate(String newString) {
+ // Java doesn't have unsigned bytes - that is, bytes greater than 0x7F will
+ // overflow and become < 0. Thus, just compare b >= 0 will suffice.
+ return Arrays.stream(Utils.byteToByte(newString.getBytes(StandardCharsets.UTF_8)))
+ .noneMatch(b -> b < 0);
+ }
+}