int ArrayEqualQ(void *a,void *b) { return ArrayCompare(a,b) == 0; }
void CardOPeration(int choice1, unsigned char slot) { long retval; FILE * txtfd; char fbuf[1024]; char *pBuf; int i; int TempLen; int choice2; UsrParam.p_oBuf = RecBuf; UsrParam.p_iBuf = CmdBuf; switch(choice1) { case '1': { CardPowerOn(slot); break; } case '2': { ioctl(fd, IFD_CMD(PowerOff, slot), &UsrParam); break; } case '3': { do { printf("Exchange Apdu with slot%d:\n",slot); printf("==============================================\n\n"); printf("1: ACOS3 T0 Test\n"); printf("2: ACOS5 Test\n"); printf("3: ACOS6 Test\n"); // printf("4: ACOS7 Test\n"); // printf("5: ACOS9 Test\n"); printf("6: JCOP30 T1 Test\n"); printf("\n"); printf("0: Exit the test program\n\n"); printf("==============================================\n\n"); choice2 = getc(stdin); if(choice2 == 10) { choice2 = getc(stdin); } if(choice2 == '1') { txtfd = fopen("ACOS3T0Test.txt","r"); if(txtfd == NULL) { printf("fail to open the file: ACOS3T0Test.txt"); } } else if(choice2 == '2') { txtfd = fopen("ACOS5Test.txt","r"); if(txtfd == NULL) { printf("fail to open the file: ACOS5Test.txt"); } } else if(choice2 == '3') { txtfd = fopen("ACOS6Test.txt","r"); if(txtfd == NULL) { printf("fail to open the file: ACOS6Test.txt"); } } /* else if(choice2 == '4') { txtfd = fopen("ACOS7Test.txt","r"); if(txtfd == NULL) { printf("fail to open the file: ACOS7Test.txt"); } } else if(choice2 == '5') { txtfd = fopen("ACOS9Test.txt","r"); if(txtfd == NULL) { printf("fail to open the file: ACOS9Test.txt"); } } */ else if(choice2 == '6') { txtfd = fopen("JCOP30.txt","r"); if(txtfd == NULL) { printf("fail to open the file: JCOP30.txt"); } } if(txtfd != NULL) { while( fgets(fbuf, 1024, txtfd) != NULL) { if(*fbuf == ';') { printf("%s",fbuf); } else if(*fbuf == '\n'); else if(strstr(fbuf, ".RESET") != NULL) { CardPowerOn(slot); } else if(strstr(fbuf, "Command:") != NULL) { pBuf = fbuf + 9; i = 0; while(*pBuf != '\n' && *pBuf != ' ') { CmdBuf[i] = StrToHex(pBuf, 2); pBuf += 3; i++; } UsrParam.p_iBuf = CmdBuf; UsrParam.iDataLen = i; UsrParam.oDataLen = 271; PrtMsg("\n\nCMD Len: %d\n", UsrParam.iDataLen); PrtMsg("slot%d CMD:", slot); for(i = 0; i < UsrParam.iDataLen; i++ ) { PrtMsg("0x%X ", UsrParam.p_iBuf[i]); } PrtMsg("\n\n"); if((retval = ioctl(fd, IFD_CMD(XfrAPDU, slot), &UsrParam)) < 0) { PrtMsg("\nOperation fail with errorcode = %lX\n", retval); break; } else { PrtMsg("Response Length: %d\n", UsrParam.oDataLen); PrtMsg("Response Data:"); for(i = 0; i < UsrParam.oDataLen; i++ ) { PrtMsg("0x%X ", RecBuf[i]); } PrtMsg("\n\n"); fgets(fbuf, 1024, txtfd); if(strstr(fbuf, "Response:") != NULL) { pBuf = fbuf + 10; i = 0; while(*pBuf != '\n' && *pBuf != ' ') { if(*pBuf == 'x' || *pBuf == 'X'); else { if(RecBuf[i] != StrToHex(pBuf, 2)) { goto exchangefail; } } pBuf += 3; i++; } TempLen = i; if(TempLen == UsrParam.oDataLen) { PrtMsg("Success to Exchange with the slot%d!\n", slot); PrtMsg("\n\n"); } else { exchangefail: PrtMsg("Fail to Exchange with the slot%d!\n", slot); PrtMsg("The expected data:\n"); PrtMsg("%s",fbuf); PrtMsg("\n\n"); break; } } } } } } }while(choice2 != '0'); #if 0 txtfd = fopen("ACOS3T0Test.txt","r"); if(txtfd == NULL) { printf("fail to open the file: JCOP30.txt"); } else { while( fgets(fbuf, 1024, txtfd) != NULL) { while(*fbuf == 0x0D && *(fbuf + 1) == 0x0A) fgets(fbuf, 1024, txtfd); pBuf = fbuf; i = 0; while(*pBuf != '\n' && *pBuf != ' ') { CmdBuf[i] = StrToHex(pBuf, 2); pBuf += 3; i++; } UsrParam.p_iBuf = CmdBuf; UsrParam.iDataLen = i; UsrParam.oDataLen = 271; PrtMsg("\n\nCMD Len: %d\n", UsrParam.iDataLen); PrtMsg("slot%d CMD:", slot); for(i = 0; i < UsrParam.iDataLen; i++ ) { PrtMsg("0x%X ", UsrParam.p_iBuf[i]); } PrtMsg("\n\n"); if((retval = ioctl(fd, IFD_CMD(XfrAPDU, slot), &UsrParam)) < 0) { PrtMsg("\nOperation fail with errorcode = %lX\n", retval); break; } else { PrtMsg("Response Length: %d\n", UsrParam.oDataLen); PrtMsg("Response Data:"); for(i = 0; i < UsrParam.oDataLen; i++ ) { PrtMsg("0x%X ", RecBuf[i]); } PrtMsg("\n\n"); PrtMsg("Success to Exchange with the slot%d!\n", slot); PrtMsg("\n\n"); } } } //#else txtfd = fopen("JCOP30.txt","r"); if(txtfd == NULL) { printf("fail to open the file: JCOP30.txt"); } else { while( fgets(fbuf, 1024, txtfd) != NULL) { while(strstr(fbuf, "Command:") == NULL) fgets(fbuf, 1024, txtfd); pBuf = fbuf + 9; i = 0; while(*pBuf != '\n' && *pBuf != ' ') { CmdBuf[i] = StrToHex(pBuf, 2); pBuf += 3; i++; } UsrParam.p_iBuf = CmdBuf; UsrParam.iDataLen = i; UsrParam.oDataLen = 271; PrtMsg("\n\nCMD Len: %d\n", UsrParam.iDataLen); PrtMsg("slot%d CMD:", slot); for(i = 0; i < UsrParam.iDataLen; i++ ) { PrtMsg("0x%X ", UsrParam.p_iBuf[i]); } PrtMsg("\n\n"); if((retval = ioctl(fd, IFD_CMD(XfrAPDU, slot), &UsrParam)) < 0) { PrtMsg("\nOperation fail with errorcode = %lX\n", retval); break; } else { PrtMsg("Response Length: %d\n", UsrParam.oDataLen); PrtMsg("Response Data:"); for(i = 0; i < UsrParam.oDataLen; i++ ) { PrtMsg("0x%X ", RecBuf[i]); } PrtMsg("\n\n"); fgets(fbuf, 1024, txtfd); while(strstr(fbuf, "Response:") == NULL) fgets(fbuf, 1024, txtfd); pBuf = fbuf + 10; i = 0; while(*pBuf != '\n' && *pBuf != ' ') { CmdBuf[i] = StrToHex(pBuf, 2); pBuf += 3; i++; } TempLen = i; if( (TempLen == UsrParam.oDataLen) && ArrayCompare(UsrParam.p_iBuf, UsrParam.p_oBuf, TempLen)) { PrtMsg("Success to Exchange with the slot%d!\n", slot); PrtMsg("\n\n"); } else { PrtMsg("Fail to Exchange with the slot%d!\n", slot); PrtMsg("The expected data:\n"); for(i = 0; i < TempLen; i++ ) { PrtMsg("0x%X ", CmdBuf[i]); } PrtMsg("\n\n"); break; } } } } #endif break; } default: printf("Invalid selection: %d\n",choice1); } }