1 19 20 package com.sslexplorer.security.pki; 21 22 import java.io.IOException ; 23 24 25 30 public class SimpleASNReader { 31 private byte[] data; 32 private int offset; 33 34 39 public SimpleASNReader(byte[] data) { 40 this.data = data; 41 this.offset = 0; 42 } 43 44 51 public void assertByte(int b) throws IOException { 52 int x = getByte(); 53 54 if (x != b) { 55 throw new IOException ("Assertion failed, next byte value is " + 56 Integer.toHexString(x) + " instead of asserted " + 57 Integer.toHexString(b)); 58 } 59 } 60 61 66 public int getByte() { 67 return data[offset++] & 0xff; 68 } 69 70 75 public byte[] getData() { 76 int length = getLength(); 77 78 return getData(length); 79 } 80 81 86 public int getLength() { 87 int b = data[offset++] & 0xff; 88 89 if ((b & 0x80) != 0) { 90 int length = 0; 91 92 for (int bytes = b & 0x7f; bytes > 0; bytes--) { 93 length <<= 8; 94 length |= (data[offset++] & 0xff); 95 } 96 97 return length; 98 } 99 100 return b; 101 } 102 103 private byte[] getData(int length) { 104 byte[] result = new byte[length]; 105 System.arraycopy(data, offset, result, 0, length); 106 offset += length; 107 108 return result; 109 } 110 111 116 public boolean hasMoreData() { 117 return offset < data.length; 118 } 119 } 120 | Popular Tags |