emv-bertlv
emv-bertlv copied to clipboard
Cannot parse an empty data source
In the scenario where the data source is empty the library crashes throwing Method java.lang.IndexOutOfBoundsException exception.
Code used to test this:
assertNull(BerTlv.parse("".toByteArray()))
I think this can be easily fixed by this change in the BerTlv.java class:
@JvmStatic
fun parse(data: ByteArray): BerTlv {
if (data.isNotEmpty()) {
return parseList(ByteBuffer.wrap(data), true)[0]
} else {
null
}
}
Meanwhile I've created an extension function that does the validation before using the static BerTlv.parse() method:
fun String.parseBerTlv(): BerTlv? {
return if (ISOUtil.isValidHexString(this)) {
BerTlv.parse(this.decodeHex())
} else {
null
}
}