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

io.github.binaryfoo.decoders.apdu.ReplyAPDUDecoder.kt Maven / Gradle / Ivy

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

import io.github.binaryfoo.DecodedData
import io.github.binaryfoo.EmvTags
import io.github.binaryfoo.decoders.DecodeSession
import io.github.binaryfoo.decoders.TLVDecoder
import io.github.binaryfoo.tlv.Tag

import java.util.Arrays
import java.util.Collections

public class ReplyAPDUDecoder(private val tlvDecoder: TLVDecoder) {

    public fun decode(input: String, startIndexInBytes: Int, session: DecodeSession): DecodedData {
        val statusBytesStart = input.length() - 4
        val endIndex: Int
        val children: List
        val decodedData: String
        if (input.length() == 4) {
            val responseCode = ResponseCode.lookup(input.substring(statusBytesStart))
            decodedData = responseCode.getHex() + " " + responseCode.description
            children = listOf()
            endIndex = startIndexInBytes + 2
        } else {
            decodedData = input.substring(statusBytesStart)
            children = tlvDecoder.decode(input.substring(0, statusBytesStart), startIndexInBytes, session)
            addToSession(session, children, Arrays.asList(EmvTags.PDOL, EmvTags.CDOL_1, EmvTags.CDOL_2, EmvTags.DDOL))
            val payload = children.get(0)
            endIndex = payload.endIndex + 2
        }
        return DecodedData.constructed("R-APDU", decodedData, startIndexInBytes, endIndex, children)
    }

    private fun addToSession(session: DecodeSession, children: List, tags: List) {
        for (child in children) {
            if (tags.contains(child.tag)) {
                session.put(child.tag, child.fullDecodedData)
            } else if (child.isComposite()) {
                addToSession(session, child.children, tags)
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy