summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorclemenso <clemenso@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4>2011-02-25 13:54:20 +0000
committerclemenso <clemenso@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4>2011-02-25 13:54:20 +0000
commit519fd0e4e9ffe721fc11fa4395cc9ec9f313931a (patch)
tree34a87b53a8bfdd5b91908d1e97f77cae0a5a819f
parent22c876e8f676938a2c5e5609a8526cad47baf7c4 (diff)
downloadmocca-519fd0e4e9ffe721fc11fa4395cc9ec9f313931a.tar.gz
mocca-519fd0e4e9ffe721fc11fa4395cc9ec9f313931a.tar.bz2
mocca-519fd0e4e9ffe721fc11fa4395cc9ec9f313931a.zip
TLV with arbitrary length fields
git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@912 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4
-rw-r--r--smcc/src/main/java/at/gv/egiz/smcc/util/TLV.java29
-rw-r--r--smcc/src/main/java/at/gv/egiz/smcc/util/TLVSequence.java2
2 files changed, 23 insertions, 8 deletions
diff --git a/smcc/src/main/java/at/gv/egiz/smcc/util/TLV.java b/smcc/src/main/java/at/gv/egiz/smcc/util/TLV.java
index b67fe3fa..1e158e75 100644
--- a/smcc/src/main/java/at/gv/egiz/smcc/util/TLV.java
+++ b/smcc/src/main/java/at/gv/egiz/smcc/util/TLV.java
@@ -20,10 +20,10 @@ package at.gv.egiz.smcc.util;
*/
public class TLV {
-
+
private byte[] bytes;
private int start;
-
+
public TLV(byte[] bytes, int start) {
if (bytes.length - start < 2) {
throw new IllegalArgumentException("TLV must at least consit of tag and length.");
@@ -39,11 +39,28 @@ public class TLV {
return 0xFF & bytes[start];
}
+ public int getLengthFieldLength() {
+ if ((bytes[start + 1] & 0x80) > 0) {
+ // ISO 7816 allows length fields of up to 5 bytes
+ return 1 + (bytes[start + 1] & 0x07);
+ }
+ return 1;
+ }
+
/**
* @return the length
*/
public int getLength() {
- return 0xFF & bytes[start + 1];
+
+ if ((bytes[start + 1] & 0x80) > 0) {
+ int length = 0;
+ for (int i = 0; i < (bytes[start + 1] & 0x07); i++) {
+ length <<= 8;
+ length += bytes[start + 2 + i] & 0xff;
+ }
+ return length;
+ }
+ return bytes[start + 1] & 0x7f;
}
/**
@@ -51,7 +68,7 @@ public class TLV {
*/
public byte[] getValue() {
byte[] value = new byte[getLength()];
- System.arraycopy(bytes, start + 2, value, 0, value.length);
+ System.arraycopy(bytes, start + 1 + getLengthFieldLength(), value, 0, value.length);
return value;
}
@@ -62,7 +79,7 @@ public class TLV {
public String toString() {
return "Tag = " + Integer.toHexString(getTag()) + ", Length = " + getLength() + ", Value = " + toString(getValue());
}
-
+
public static String toString(byte[] b) {
StringBuffer sb = new StringBuffer();
sb.append('[');
@@ -79,6 +96,4 @@ public class TLV {
return sb.toString();
}
-
-
}
diff --git a/smcc/src/main/java/at/gv/egiz/smcc/util/TLVSequence.java b/smcc/src/main/java/at/gv/egiz/smcc/util/TLVSequence.java
index 2409f212..00639545 100644
--- a/smcc/src/main/java/at/gv/egiz/smcc/util/TLVSequence.java
+++ b/smcc/src/main/java/at/gv/egiz/smcc/util/TLVSequence.java
@@ -63,7 +63,7 @@ public class TLVSequence implements Iterable<TLV> {
public TLV next() {
if (hasNext()) {
TLV tlv = new TLV(bytes, pos);
- pos += tlv.getLength() + 2;
+ pos += 1 + tlv.getLengthFieldLength() + tlv.getLength();
return tlv;
} else {
throw new NoSuchElementException();