Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.asn1; |
| 2 | |
| 3 | import java.util.*; |
| 4 | |
| 5 | public class PDU implements Types, Iterable<PDU> { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 6 | final int identifier; |
| 7 | final Object payload; |
| 8 | byte data[] = new byte[100]; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 9 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 10 | public PDU(int id, Object payload) { |
| 11 | identifier = id; |
| 12 | this.payload = payload; |
| 13 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 15 | public PDU(Date payload) { |
| 16 | identifier = UTCTIME; |
| 17 | this.payload = payload; |
| 18 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 19 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 20 | public PDU(int n) { |
| 21 | this(UNIVERSAL + INTEGER, n); |
| 22 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 23 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 24 | public PDU(boolean value) { |
| 25 | this(UNIVERSAL + BOOLEAN, value); |
| 26 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 27 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 28 | public PDU(String s) throws Exception { |
| 29 | this(UNIVERSAL + IA5STRING, s); |
| 30 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 31 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 32 | public PDU(byte[] data) { |
| 33 | this(UNIVERSAL + OCTET_STRING, data); |
| 34 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 36 | public PDU(BitSet bits) { |
| 37 | this(UNIVERSAL + BIT_STRING, bits); |
| 38 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 39 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 40 | 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 48 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 49 | public PDU(int tag, PDU... set) { |
| 50 | this(tag, (Object) set); |
| 51 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 52 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 53 | public PDU(PDU... set) { |
| 54 | this(SEQUENCE + CONSTRUCTED, set); |
| 55 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 56 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 57 | public int getTag() { |
| 58 | return identifier & TAGMASK; |
| 59 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 60 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 61 | int getClss() { |
| 62 | return identifier & CLASSMASK; |
| 63 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 64 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 65 | public boolean isConstructed() { |
| 66 | return (identifier & CONSTRUCTED) != 0; |
| 67 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 68 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 69 | public String getString() { |
| 70 | return (String) payload; |
| 71 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 72 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 73 | public Iterator<PDU> iterator() { |
| 74 | return Arrays.asList((PDU[]) payload).iterator(); |
| 75 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 76 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 77 | public int[] getOID() { |
| 78 | assert getTag() == OBJECT_IDENTIFIER; |
| 79 | return (int[]) payload; |
| 80 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 81 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 82 | public Boolean getBoolean() { |
| 83 | assert getTag() == BOOLEAN; |
| 84 | return (Boolean) payload; |
| 85 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 86 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 87 | public BitSet getBits() { |
| 88 | assert getTag() == BIT_STRING; |
| 89 | return (BitSet) payload; |
| 90 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 91 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 92 | public int getInt() { |
| 93 | assert getTag() == INTEGER || getTag() == ENUMERATED; |
| 94 | return (Integer) payload; |
| 95 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 96 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 97 | public byte[] getBytes() { |
| 98 | return (byte[]) payload; |
| 99 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 100 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 101 | public PDU[] getChildren() { |
| 102 | assert isConstructed(); |
| 103 | return (PDU[]) payload; |
| 104 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 105 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 106 | public Date getDate() { |
| 107 | assert getTag() == UTCTIME || getTag() == GENERALIZED_TIME; |
| 108 | return (Date) payload; |
| 109 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 110 | |
| 111 | } |