+ flags |= SC_SELECT | SC_CONNECT;
+ }
+
+ if (APDUlen == 4) { // Case 1
+ memcpy(TPDU, APDU, 4);
+ TPDU[4] = 0x00;
+ smart_transmit(TPDU, 5, flags, response, responselen, maxresponselen);
+ } else if (APDUlen == 5) { // Case 2 Short
+ smart_transmit(APDU, 5, flags, response, responselen, maxresponselen);
+ if (response[0] == 0x6C) { // wrong Le
+ uint16_t Le = APDU[4] ? APDU[4] : 256;
+ uint8_t La = response[1];
+ memcpy(TPDU, APDU, 5);
+ TPDU[4] = La;
+ smart_transmit(TPDU, 5, SC_RAW_T0, response, responselen, maxresponselen);
+ if (Le < La && *responselen >= 0) {
+ response[Le] = response[*responselen-2];
+ response[Le+1] = response[*responselen-1];
+ *responselen = Le + 2;
+ }
+ }
+ } else if (APDU[4] != 0 && APDUlen == 5 + APDU[4]) { // Case 3 Short
+ smart_transmit(APDU, APDUlen, flags, response, responselen, maxresponselen);
+ } else if (APDU[4] != 0 && APDUlen == 5 + APDU[4] + 1) { // Case 4 Short
+ smart_transmit(APDU, APDUlen-1, flags, response, responselen, maxresponselen);
+ if (response[0] == 0x90 && response[1] == 0x00) {
+ uint8_t Le = APDU[APDUlen-1];
+ uint8_t get_response[5] = {0x00, ISO7816_GET_RESPONSE, 0x00, 0x00, Le};
+ return ExchangeAPDUSC(get_response, 5, false, leaveSignalON, response, maxresponselen, responselen);
+ }
+ } else { // Long Cases not yet implemented
+ PrintAndLogEx(ERR, "Long APDUs not yet implemented");
+ *responselen = -3;