static int testStatus(SCARDCONTEXT ctx, const char *readerName, const char *readerList) { SCARDHANDLE hCard; DWORD pcchReaderLen = 2048; //can this be calculated beforehand!? DWORD pdwState; DWORD pdwProtocol; BYTE pbAtr[32]; DWORD pcbAtrLen = 32; char readerName2[2048]; int returnValue = 0; long ret = SCardConnectA(ctx, readerName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &pdwProtocol); CHECK_PCSC_RET("SCardConnect", ret); if (SCARD_S_SUCCESS == ret) { delayMS(200); // Normal case: fail position ----1 ret = SCardStatusA(hCard, readerName2, &pcchReaderLen, &pdwState, &pdwProtocol, (LPBYTE) &pbAtr, &pcbAtrLen); if (ret != SCARD_S_SUCCESS) returnValue |= 1; // ReaderLen = 0: fail position ---1- pcchReaderLen = NULL; readerName2[0] = '\0'; //make sure refreshed with new values ret = SCardStatusA(hCard, (LPSTR) readerName2, &pcchReaderLen, &pdwState, &pdwProtocol, (LPBYTE) &pbAtr, &pcbAtrLen); if (ret != SCARD_S_SUCCESS) returnValue |= 2; // ReaderLen = 1: fail position ---1-- pcchReaderLen = 1; readerName2[0] = '\0'; //make sure refreshed with new values ret = SCardStatusA(hCard, (LPSTR) readerName2, &pcchReaderLen, &pdwState, &pdwProtocol, (LPBYTE) &pbAtr, &pcbAtrLen); CHECK_PCSC_RET_PASS(0x4) //AtrLen = 0: fail position --1--- pcchReaderLen = 2048; pcbAtrLen = 0; ret = SCardStatusA(hCard, (LPSTR) readerName2, &pcchReaderLen, &pdwState, &pdwProtocol, (LPBYTE) &pbAtr, &pcbAtrLen); if (ret != SCARD_S_SUCCESS) returnValue |= 8; //AtrLen = 1: fail position -1---- pcbAtrLen = 1; ret = SCardStatusA(hCard, (LPSTR) readerName2, &pcchReaderLen, &pdwState, &pdwProtocol, (LPBYTE) &pbAtr, &pcbAtrLen); if (ret != SCARD_S_SUCCESS) returnValue |= 16; //AtrLen = 33 (bigger than returned): fail position 1----- pcbAtrLen = 33; ret = SCardStatusA(hCard, (LPSTR) readerName2, &pcchReaderLen, &pdwState, &pdwProtocol, (LPBYTE) &pbAtr, &pcbAtrLen); if (ret != SCARD_S_SUCCESS) returnValue |= 32; } return returnValue; }
static DWORD smartcard_StatusA_Call(SMARTCARD_DEVICE* smartcard, SMARTCARD_OPERATION* operation, Status_Call* call) { LONG status; Status_Return ret = { 0 }; DWORD cchReaderLen = 0; LPSTR mszReaderNames = NULL; IRP* irp = operation->irp; if (call->cbAtrLen > 32) call->cbAtrLen = 32; ret.cbAtrLen = call->cbAtrLen; ZeroMemory(ret.pbAtr, 32); cchReaderLen = SCARD_AUTOALLOCATE; status = ret.ReturnCode = SCardStatusA(operation->hCard, (LPSTR) &mszReaderNames, &cchReaderLen, &ret.dwState, &ret.dwProtocol, (BYTE*) &ret.pbAtr, &ret.cbAtrLen); if (status == SCARD_S_SUCCESS) { ret.mszReaderNames = (BYTE*) mszReaderNames; ret.cBytes = cchReaderLen; } smartcard_trace_status_return(smartcard, &ret, FALSE); status = smartcard_pack_status_return(smartcard, irp->output, &ret); if (status != SCARD_S_SUCCESS) return status; if (mszReaderNames) SCardFreeMemory(operation->hContext, mszReaderNames); return ret.ReturnCode; }
static int testSCardStatusA(SCARDCONTEXT hCard) { int errCount = 0; DWORD dwState; DWORD dwProt; char staticReader[250]; DWORD staticReaderLen = (DWORD) sizeof(staticReader); BYTE staticAtr[32]; DWORD staticAtrLen = (DWORD) sizeof(staticAtr); long ret = SCardStatusA(hCard, (char *) &staticReader, &staticReaderLen, &dwState, &dwProt, staticAtr, &staticAtrLen); CHECK_PCSC_RET("SCardStatusA(1)", ret); char *dynReader = NULL; DWORD dynReaderLen = 0; BYTE *dynAtr = NULL; DWORD dynAtrLen = 0; ret = SCardStatusA(hCard, dynReader, &dynReaderLen, &dwState, &dwProt, dynAtr, &dynAtrLen); CHECK_PCSC_RET("SCardStatusA(2)", ret); dynReader = new char[staticReaderLen]; dynAtr = new BYTE[dynAtrLen]; ret = SCardStatusA(hCard, dynReader, &dynReaderLen, &dwState, &dwProt, dynAtr, &dynAtrLen); CHECK_PCSC_RET("SCardStatusA(3)", ret); #ifndef MAC_OS_X char *autoReader = NULL; DWORD autoReaderLen = SCARD_AUTOALLOCATE; BYTE *autoAtr = NULL; DWORD autoAtrLen = SCARD_AUTOALLOCATE; ret = SCardStatusA(hCard, (char *) &autoReader, &autoReaderLen, &dwState, &dwProt, (BYTE *) &autoAtr, &autoAtrLen); CHECK_PCSC_RET("SCardStatusA(4)", ret); if (staticReaderLen != dynReaderLen || dynReaderLen != autoReaderLen) { printf("ERR: SCardStatusA() returned buffers lengths of different reader size\n"); printf(" (staticLen = %d, dynLen = %d, autoLen = %d\n", (int) staticReaderLen, (int) dynReaderLen, (int) autoReaderLen); errCount++; } else if (memcmp(staticReader, dynReader, dynReaderLen) != 0 || memcmp(dynReader, autoReader, dynReaderLen) != 0) { printf("ERR: SCardStatusA() returned different reader names\n"); dump(" staticReader = '%s' (%d bytes)\n", staticReader, dynReaderLen); dump(" dynReader = '%s' (%d bytes)\n", dynReader, dynReaderLen); dump(" autoReader = '%s' (%d bytes)\n", autoReader, dynReaderLen); errCount++; } if (staticAtrLen != dynAtrLen || dynAtrLen != autoAtrLen) { printf("ERR: SCardStatusA() returned buffers lengths of different ATR size\n"); printf(" (staticLen = %d, dynLen = %d, autoLen = %d\n", (int) staticAtrLen, (int) dynAtrLen, (int) autoAtrLen); errCount++; } else if (memcmp(staticAtr, dynAtr, dynAtrLen) != 0 || memcmp(dynAtr, autoAtr, dynAtrLen) != 0) { printf("ERR: SCardStatusA() returned different ATRs\n"); errCount++; } #endif return errCount; }