📄 ctcomm.~cpp
字号:
/*
* Common defination and Virtual Reader Class
*
* writen by Alfred Meng July, 2000
*/
#include <string.h>
//#include "gdcmd.h"
#include "ctcomm.h"
#include "cardutil.h"
TAPDU::TAPDU() {
len = 0x0;
tag = NULL;
}
void TAPDU::CLA(unsigned char ucCLA) {
apdu[0] = ucCLA;
}
void TAPDU::INS(unsigned char ucINS) {
apdu[1] = ucINS;
}
void TAPDU::P1(unsigned char ucP1) {
apdu[2] = ucP1;
}
void TAPDU::P2(unsigned char ucP2) {
apdu[3] = ucP2;
len = 0x04;
}
void TAPDU::P3(unsigned char ucP3) {
apdu[len++] = ucP3;
}
void TAPDU::DATA(unsigned char * data, unsigned char ucLc) {
apdu[4] = ucLc;
len = 0x05 + ucLc;
memcpy(apdu + 5, data, ucLc);
}
TAPDU::TAPDU(unsigned char ucCLA, unsigned char ucINS, unsigned char ucP1, unsigned char ucP2, unsigned char ucP3) {
apdu[0] = ucCLA;
apdu[1] = ucINS;
apdu[2] = ucP1;
apdu[3] = ucP2;
apdu[4] = ucP3;
len = 0x05;
}
TVSCReader::TVSCReader() {
cset = NULL;
count = 0;
_case4_op_ = CASE4_Enable;
__iso_cla = 0x00;
__fHook = NULL;
}
int TVSCReader::assign(TCommand * set, int n) {
count = n;
return (cset = set) == NULL ? 0 : 1;
}
int TVSCReader::findbyID(int id) {
for (int i = 0; i < count; i++)
if (cset[i].id == id) return i;
return INVAILD_CMD;
}
int TVSCReader::findbyCI(unsigned char cla, unsigned char ins) {
for (int i = 0; i < count; i++)
if (cset[i].apdu.cla == cla && cset[i].apdu.ins == ins) return i;
return INVAILD_CMD;
}
char * TVSCReader::findName(int id) {
if (id != INVAILD_CMD) return cset[id].name;
return NULL;
}
int TVSCReader::execute(int id, unsigned char p1, unsigned char p2, unsigned char lc, unsigned char * data, unsigned char le) {
if (cset == NULL) return -1;
lastc = findbyID(id);
if (lastc == INVAILD_CMD) return -1;
memset(__apdu.apdu, 0x0, sizeof(__apdu));
__apdu.apdu[0] = cset[lastc].apdu.cla;
__apdu.apdu[1] = cset[lastc].apdu.ins;
__apdu.apdu[2] = p1;
__apdu.apdu[3] = p2;
switch (cset[lastc].apdu.type) {
case CASE1:
__apdu.len = 4;
break;
case CASE2:
__apdu.apdu[4] = le;
__apdu.len = 5;
break;
case CASE3:
case CASE4:
__apdu.apdu[4] = lc;
memcpy(__apdu.apdu + 5, data, lc);
__apdu.len = 5 + lc;
if (cset[lastc].apdu.type == CASE4)
if (_case4_op_ == CASE4_Enable) {
__apdu.len++;
__apdu.apdu[__apdu.len - 1] = le;
}
break;
default:
return -1;
}
return sendapdu(& __apdu);
}
int TVSCReader::verify(char * pin, int pl, char kid) {
unsigned char vdata[16];
memset(vdata, 0xff, 16);
memcpy(vdata, pin, pl);
return execute(VERIFY_PIN, 0x00, kid, 0x08, vdata);
}
int TVSCReader::checkresp(unsigned char aSW1, unsigned char aSW2) {
if (SW1 == aSW1 && SW2 == aSW2) return 1;
return 0;
}
void TVSCReader::set_apdu_hook(TApduHookFun fHook) {
__fHook = fHook;
}
int TVSCReader::batchprocess(char * fname, int & Lines /*, TApduHookFun pHook*/) {
FILE * fp;
char buff[1024];
char ascbuf[1024];
int i = 0;
TAPDU apdu;
fp =::fopen(fname, "r");
if (fp == NULL) return -1;
apdu.len = 0;
Lines = 1;
while (fgets(buff, 1024, fp) != NULL) {
if (buff[0] == '\n') continue;
if (buff[0] == ';') continue;
/* remark */
if (buff[0] != ' ') {
if (apdu.len) {
ascbuf[apdu.len] = '\0';
apdu.len /= 2;
asc2bcd((char *) ascbuf, (char *) apdu.apdu, apdu.len);
/*if (pHook!=NULL)
(*pHook)(Lines,&apdu); */
i = sendapdu(& apdu);
if (!checkresp()) {
::fclose(fp);
return Lines;
}
Lines++;
apdu.len = 0;
}
if (i != 0) {
::fclose(fp);
return i;
}
__append_apdu:
i = 0;
while (1) {
while (buff[i] == ' ') i++;
if (buff[i] == '\n' /*|| buff[i] == 0x0*/) break;
ascbuf[apdu.len++] = buff[i++];
}
} else goto __append_apdu;
}
if (apdu.len) {
ascbuf[apdu.len] = '\0';
apdu.len /= 2;
asc2bcd((char *) ascbuf, (char *) apdu.apdu, apdu.len);
/*if (pHook!=NULL)
(*pHook)(Lines,&apdu); */
i = sendapdu(& apdu);
if (!checkresp()) {
::fclose(fp);
return Lines;
}
}
::fclose(fp);
return i;
}
int TVSCReader::magcommand(char * asccmd) {
char tempBuf[1024];
strcpy(tempBuf, asccmd);
trimspace(asccmd);
__apdu.len = strlen(asccmd);
__apdu.len /= 2;
asc2bcd(asccmd, (char *) __apdu.apdu, __apdu.len);
return sendapdu(& __apdu);
}
int TVSCReader::select(char * fid) {
return execute(SELECT, 0x00, 0x0C, 0x02, (unsigned char *) fid);
}
TFILE * TVSCReader::fopen_rec(char sid) {
TFILE * fp = new TFILE;
fp->am = 0x04;
fp->sid = sid;
fp->type = FT_LINFIX;
fp->am |= sid << 3;
rc = execute(READ_REC, 0x01, fp->am, 0, NULL, 0);
if (rc == 0) {
fp->offset = RespLen;
return fp;
}
delete fp;
return NULL;
}
int TVSCReader::seek(TFILE * fp, short offset_from_head) {
if (fp->type != FT_BINARY) return 0;
return (fp->offset = offset_from_head);
}
TFILE * TVSCReader::fopen_bin(char sid) {
TFILE * fp = new TFILE;
fp->am = 0x04;
fp->sid = sid;
fp->fid[0] = 0x88;
fp->type = FT_BINARY;
fp->offset = 0;
return fp;
}
int TVSCReader::read_bin(TFILE * fp, char * buff, int sr) {
if (fp->type == FT_BINARY) rc = execute(READ_BIN, 0x80 | fp->sid, LOBYTE(fp->offset), 0, NULL, sr);
else return 0;
if (rc == 0) {
rc = RespLen;
memcpy(buff, Resp, rc);
return rc;
} else return rc;
}
int TVSCReader::read_rec(TFILE * fp, char * buff, int sr) {
int rc;
if (fp->type == FT_LINFIX) rc = execute(READ_REC, (char)sr, fp->am, 0, NULL, fp->offset);
else return 0;
if (rc == 0) {
rc = RespLen;
memcpy(buff, Resp, rc);
return rc;
} else return rc;
}
int TVSCReader::write(TFILE * fp, char * buff, int sr) {
int rc;
switch (fp->type) {
case FT_BINARY:
rc = execute(UPDATE_BIN, 0x80 | fp->sid, LOBYTE(fp->offset), sr, (unsigned char *) buff);
break;
case FT_LINFIX:
case FT_CYCLIC:
rc = execute(UPDATE_REC, (BYTE)sr, fp->am, fp->offset, (unsigned char *) buff);
break;
default:
return -1;
}
return rc;
}
void TVSCReader::fclose(TFILE * fp) {
if (fp != NULL) delete fp;
}
//
void TVSCReader::deleteMF() {
__apdu.len = 7;
memcpy(__apdu.apdu, "\x80\xe4\x00\x00\x02\x3f\x00", 7);
sendapdu(& __apdu);
}
/*
void TVSCReader::creatEnd(char *fid)
{
// execute(CREATE,C_OP_END,0x00,0x02,(unsigned char *)fid);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -