Update to spice2x-25-04-25 (pre-apply)

> broken commit
This commit is contained in:
[ ]
2025-05-07 00:25:31 +09:00
parent 04dee88276
commit c94de456b5
543 changed files with 78491 additions and 91698 deletions
+59 -11
View File
@@ -43,7 +43,6 @@ BYTE PICC_OPERATING_PARAM_CMD[5] = {0xFFu, 0x00u, 0x51u, PICC_OPERATING_PARAMS,
// return bytes from device
#define PICC_SUCCESS 0x90u
#define PICC_ERROR 0x63u
static const BYTE UID_CMD[5] = { 0xFFu, 0xCAu, 0x00u, 0x00u, 0x00u };
@@ -110,6 +109,9 @@ void scard_update(SCARDCONTEXT hContext, LPCTSTR readerName, uint8_t unit_no) {
if (cByteAtr != 20) {
log_warning("scard", "ignoring card with len(atr) = {} ({})", cByteAtr, bin2hex(atr, cByteAtr));
return;
} else {
// https://pcsc-tools.apdu.fr/smartcard_list.txt
log_misc("scard", "atr string (format of card) = {}", bin2hex(atr, cByteAtr));
}
//log_info("scard", "atr Return: len({}), {}", cByteAtr, bin2hex(atr, cByteAtr));
@@ -117,6 +119,7 @@ void scard_update(SCARDCONTEXT hContext, LPCTSTR readerName, uint8_t unit_no) {
// Figure out if we should reverse the UID returned by the card based on the ATR protocol
BYTE cardProtocol = atr[12];
BOOL shouldReverseUid = false;
bool is_felica = false;
if (cardProtocol == SCARD_ATR_PROTOCOL_ISO15693_PART3) {
log_info("scard", "card protocol: ISO15693_PART3");
shouldReverseUid = true;
@@ -124,8 +127,10 @@ void scard_update(SCARDCONTEXT hContext, LPCTSTR readerName, uint8_t unit_no) {
log_info("scard", "card protocol: ISO14443_PART3");
} else if (cardProtocol == SCARD_ATR_PROTOCOL_FELICA_212K) {
log_info("scard", "card protocol: FELICA_212K");
is_felica = true;
} else if (cardProtocol == SCARD_ATR_PROTOCOL_FELICA_424K) {
log_info("scard", "card protocol: FELICA_424K");
is_felica = true;
} else {
log_warning("scard", "Unknown NFC Protocol: 0x{:02x}", static_cast<unsigned>(cardProtocol));
//return;
@@ -138,20 +143,38 @@ void scard_update(SCARDCONTEXT hContext, LPCTSTR readerName, uint8_t unit_no) {
return;
}
if (cbRecv > 1 && pbRecv[0] == PICC_ERROR) {
log_warning("scard", "UID query failed");
// check response
// 2 bytes minimum for response code
// 3+ bytes for response code plus one byte card number
if (cbRecv < 3) {
if (cbRecv < 2) {
log_warning("scard", "UID query failed - not enough bytes ({})", cbRecv);
} else {
log_warning("scard", "UID query failed - received {:x} {:x} ({} bytes)", pbRecv[0], pbRecv[1], cbRecv);
}
return;
}
// according to ACS's spec the response format is
// UID(LSB)... UID(MSB) 90 00
// 90 00 is the success code, but previously this code was using it as if it was part of the UID
// fix this by decrementing cbRecv, but preserve the old behavior for compatibility if no flags are set
// (this wouldn't have mattered for actual FeliCa cards since the 9000 would have been truncated)
if (WINSCARD_CONFIG.add_padding_to_old_cards || WINSCARD_CONFIG.add_padding_to_felica) {
cbRecv -= 2;
}
if ((lRet = SCardDisconnect(hCard, SCARD_LEAVE_CARD)) != SCARD_S_SUCCESS) {
log_info("scard", "failed SCardDisconnect: 0x{:08x}", static_cast<unsigned>(lRet));
}
log_misc("scard", "reader unit no: {}, raw card number from reader: {}", unit_no, bin2hex(pbRecv, cbRecv));
if (cbRecv < 8) {
log_info("scard", "padding card uid to 8 bytes (read uid: {})", bin2hex(pbRecv, cbRecv));
log_misc("scard", "padding card uid to 8 bytes by adding zeroes at the end");
memset(&pbRecv[cbRecv], 0, 8 - cbRecv);
} else if (cbRecv > 8) {
log_info("scard", "taking first 8 bytes of len(uid) = {}", cbRecv);
log_misc("scard", "taking first 8 bytes of len(uid) = {}", cbRecv);
}
// Copy UID to struct, reversing if necessary
@@ -163,14 +186,39 @@ void scard_update(SCARDCONTEXT hContext, LPCTSTR readerName, uint8_t unit_no) {
} else {
memcpy(card_info.uid, pbRecv, 8);
}
log_info("scard", "read card: {}", bin2hex(card_info.uid, 8));
// add prefix if the user wants to override card number for compatibility
// don't do it if the card already begins with e00401
bool add_prefix =
(is_felica && WINSCARD_CONFIG.add_padding_to_felica) ||
(!is_felica && WINSCARD_CONFIG.add_padding_to_old_cards);
if (add_prefix &&
!(card_info.uid[0] == 0xe0 && card_info.uid[1] == 0x04 && card_info.uid[2] == 0x01)) {
// abcd ef12 3456 0000 => e004 01ab cdef 1234
log_misc("scard", "adding E00401 prefix to card for compatibility (-scardfix)");
uint8_t uid[8];
uid[0] = 0xe0;
uid[1] = 0x04;
uid[2] = 0x01;
for (DWORD i = 3; i < 8; i++) {
uid[i] = card_info.uid[i-3];
}
memcpy(card_info.uid, uid, 8);
}
log_info("scard", "reader unit no: {}, card number to be inserted into game: {}", unit_no, bin2hex(card_info.uid, 8));
// set card type to 1
card_info.card_type = 1;
// update toggle
bool flip_order = WINSCARD_CONFIG.flip_order;
if (WINSCARD_CONFIG.toggle_order && (GetAsyncKeyState(VK_NUMLOCK) & 1) > 0) {
if (WINSCARD_CONFIG.flip_order) {
log_info("scard", "Flip order of readers since -scardflip option is set");
}
if (WINSCARD_CONFIG.toggle_order && (GetKeyState(VK_NUMLOCK) & 1) > 0) {
log_info("scard", "Flip order of readers since Num Lock is on");
flip_order = !flip_order;
}
@@ -232,13 +280,13 @@ void scard_loop(SCARDCONTEXT hContext, LPCTSTR slot0_reader_name, LPCTSTR slot1_
DWORD newState = reader_states[unit_no].dwEventState ^ SCARD_STATE_CHANGED;
bool wasCardPresent = (reader_states[unit_no].dwCurrentState & SCARD_STATE_PRESENT) > 0;
if (newState & SCARD_STATE_UNAVAILABLE) {
log_info("scard", "new card state: unavailable");
log_misc("scard", "new card state: unavailable");
Sleep(SCARD_POLL_INTERVAL_MS);
} else if (newState & SCARD_STATE_EMPTY) {
log_info("scard", "new card state: empty");
log_misc("scard", "new card state: empty");
scard_clear(unit_no);
} else if (newState & SCARD_STATE_PRESENT && !wasCardPresent) {
log_info("scard", "new card state: present");
log_misc("scard", "new card state: present");
scard_update(hContext, reader_states[unit_no].szReader, unit_no);
}
@@ -339,7 +387,7 @@ int scard_threadmain() {
log_info("scard", "using reader slot 0: {}", reader_name_slots[0]);
}
if (reader_name_slots[1]) {
log_info("scard", "using reader slot 1: {}", reader_name_slots[0]);
log_info("scard", "using reader slot 1: {}", reader_name_slots[1]);
}
scard_loop(hContext, reader_name_slots[0], reader_name_slots[1], (uint8_t) (reader_name_slots[1] ? 2 : 1));
+3
View File
@@ -69,6 +69,9 @@ typedef struct {
// For toggling the order using NUMLOCK
bool toggle_order;
bool add_padding_to_old_cards;
bool add_padding_to_felica;
} winscard_config_t;
extern winscard_config_t WINSCARD_CONFIG;