blob: 2fb29fb2fe1fc00bb629d98f814c2e50e2148a2a [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.libg.asn1;
2
3import java.util.*;
4
5public class PDU implements Types, Iterable<PDU> {
Stuart McCulloch2286f232012-06-15 13:27:53 +00006 final int identifier;
7 final Object payload;
8 byte data[] = new byte[100];
Stuart McCullochbb014372012-06-07 21:57:32 +00009
Stuart McCulloch2286f232012-06-15 13:27:53 +000010 public PDU(int id, Object payload) {
11 identifier = id;
12 this.payload = payload;
13 }
Stuart McCullochbb014372012-06-07 21:57:32 +000014
Stuart McCulloch2286f232012-06-15 13:27:53 +000015 public PDU(Date payload) {
16 identifier = UTCTIME;
17 this.payload = payload;
18 }
Stuart McCullochbb014372012-06-07 21:57:32 +000019
Stuart McCulloch2286f232012-06-15 13:27:53 +000020 public PDU(int n) {
21 this(UNIVERSAL + INTEGER, n);
22 }
Stuart McCullochbb014372012-06-07 21:57:32 +000023
Stuart McCulloch2286f232012-06-15 13:27:53 +000024 public PDU(boolean value) {
25 this(UNIVERSAL + BOOLEAN, value);
26 }
Stuart McCullochbb014372012-06-07 21:57:32 +000027
Stuart McCulloch2286f232012-06-15 13:27:53 +000028 public PDU(String s) throws Exception {
29 this(UNIVERSAL + IA5STRING, s);
30 }
Stuart McCullochbb014372012-06-07 21:57:32 +000031
Stuart McCulloch2286f232012-06-15 13:27:53 +000032 public PDU(byte[] data) {
33 this(UNIVERSAL + OCTET_STRING, data);
34 }
Stuart McCullochbb014372012-06-07 21:57:32 +000035
Stuart McCulloch2286f232012-06-15 13:27:53 +000036 public PDU(BitSet bits) {
37 this(UNIVERSAL + BIT_STRING, bits);
38 }
Stuart McCullochbb014372012-06-07 21:57:32 +000039
Stuart McCulloch2286f232012-06-15 13:27:53 +000040 public PDU(int top, int l1, int... remainder) {
41 identifier = UNIVERSAL + OBJECT_IDENTIFIER;
42 int[] ids = new int[remainder.length + 2];
43 ids[0] = top;
44 ids[1] = l1;
45 System.arraycopy(remainder, 0, ids, 2, remainder.length);
46 payload = ids;
47 }
Stuart McCullochbb014372012-06-07 21:57:32 +000048
Stuart McCulloch2286f232012-06-15 13:27:53 +000049 public PDU(int tag, PDU... set) {
50 this(tag, (Object) set);
51 }
Stuart McCullochbb014372012-06-07 21:57:32 +000052
Stuart McCulloch2286f232012-06-15 13:27:53 +000053 public PDU(PDU... set) {
54 this(SEQUENCE + CONSTRUCTED, set);
55 }
Stuart McCullochbb014372012-06-07 21:57:32 +000056
Stuart McCulloch2286f232012-06-15 13:27:53 +000057 public int getTag() {
58 return identifier & TAGMASK;
59 }
Stuart McCullochbb014372012-06-07 21:57:32 +000060
Stuart McCulloch2286f232012-06-15 13:27:53 +000061 int getClss() {
62 return identifier & CLASSMASK;
63 }
Stuart McCullochbb014372012-06-07 21:57:32 +000064
Stuart McCulloch2286f232012-06-15 13:27:53 +000065 public boolean isConstructed() {
66 return (identifier & CONSTRUCTED) != 0;
67 }
Stuart McCullochbb014372012-06-07 21:57:32 +000068
Stuart McCulloch2286f232012-06-15 13:27:53 +000069 public String getString() {
70 return (String) payload;
71 }
Stuart McCullochbb014372012-06-07 21:57:32 +000072
Stuart McCulloch2286f232012-06-15 13:27:53 +000073 public Iterator<PDU> iterator() {
74 return Arrays.asList((PDU[]) payload).iterator();
75 }
Stuart McCullochbb014372012-06-07 21:57:32 +000076
Stuart McCulloch2286f232012-06-15 13:27:53 +000077 public int[] getOID() {
78 assert getTag() == OBJECT_IDENTIFIER;
79 return (int[]) payload;
80 }
Stuart McCullochbb014372012-06-07 21:57:32 +000081
Stuart McCulloch2286f232012-06-15 13:27:53 +000082 public Boolean getBoolean() {
83 assert getTag() == BOOLEAN;
84 return (Boolean) payload;
85 }
Stuart McCullochbb014372012-06-07 21:57:32 +000086
Stuart McCulloch2286f232012-06-15 13:27:53 +000087 public BitSet getBits() {
88 assert getTag() == BIT_STRING;
89 return (BitSet) payload;
90 }
Stuart McCullochbb014372012-06-07 21:57:32 +000091
Stuart McCulloch2286f232012-06-15 13:27:53 +000092 public int getInt() {
93 assert getTag() == INTEGER || getTag() == ENUMERATED;
94 return (Integer) payload;
95 }
Stuart McCullochbb014372012-06-07 21:57:32 +000096
Stuart McCulloch2286f232012-06-15 13:27:53 +000097 public byte[] getBytes() {
98 return (byte[]) payload;
99 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000100
Stuart McCulloch2286f232012-06-15 13:27:53 +0000101 public PDU[] getChildren() {
102 assert isConstructed();
103 return (PDU[]) payload;
104 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000105
Stuart McCulloch2286f232012-06-15 13:27:53 +0000106 public Date getDate() {
107 assert getTag() == UTCTIME || getTag() == GENERALIZED_TIME;
108 return (Date) payload;
109 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000110
111}