All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.binaryfoo.decoders.CVMResultsDecoder.kt Maven / Gradle / Ivy

There is a newer version: 0.1.8
Show newest version
package io.github.binaryfoo.decoders

import java.util.Arrays

import io.github.binaryfoo.DecodedData
import io.github.binaryfoo.Decoder
import io.github.binaryfoo.tlv.ISOUtil

public class CVMResultsDecoder : Decoder {

    override fun decode(input: String, startIndexInBytes: Int, session: DecodeSession): List {
        val rule = CVRule(input.substring(0, 4))
        val result = input.substring(4, 6)
        return listOf(
                DecodedData.primitive(input.substring(0, 2), rule.verificationMethodDescription, startIndexInBytes, startIndexInBytes + 1),
                DecodedData.primitive(input.substring(2, 4), rule.conditionCodeDescription, startIndexInBytes + 1, startIndexInBytes + 2),
                DecodedData.primitive(result, decodeResult(result), startIndexInBytes + 2, startIndexInBytes + 3))
    }

    private fun decodeResult(result: String): String {
        return if ("01" == result)
            "Failed"
        else if ("02" == result) "Sucessful" else "Unknown"
    }

    override fun getMaxLength(): Int = FIELD_LENGTH

    override fun validate(input: String?): String? {
        if (input == null || input.length() != FIELD_LENGTH) {
            return "Value must be exactly ${FIELD_LENGTH} characters"
        }
        if (!ISOUtil.isValidHexString(input)) {
            return "Value must contain only the characters 0-9 and A-F"
        }
        return null
    }

    class object {
        private val FIELD_LENGTH = 6
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy