例如,我有这样的R-APDU
uchar rApdu[] = {0x6F, 0x35 ,0x84 ,0x08 ,0x45 ,0x4F ,0x50 , 0x43 ,0x43 ,0x41 ,0x52 ,0x44 ,0xA5 ,0x29 ,0x50 ,0x06 ,0x55 ,0x5A ,0x4B ,0x41 ,0x52 ,0x54 ,0x5F, 0x2D, 0x06 ,0x75 ,0x7A ,0x72 ,0x75 ,0x65 ,0x6E ,0x87 ,0x01 ,0x01 ,0x9F ,0x11 ,0x01 ,0x01 ,0x9F, 0x12 ,0x06 ,0x55 ,0x5A ,0x4B ,0x41 ,0x52 ,0x54 ,0xBF ,0x0C ,0x05 ,0x9F ,0x4D ,0x02 ,0x0B ,0x0A};
我想在C中解析这个TLV,就像在这个页面上一样: 解析TLV
它应该解析所有标签及其值并采用结构。 我试图解析,这就是我得到的:
#include #include #define uchar unsigned char #define uint unsigned int #define ushort unsigned short #define k6f 0x6F typedef enum{ false, true }bool; struct TAG{ uchar *data; uint len; }; struct TLVdata{ struct TAG s6F; struct TAG s84; struct TAG sA5; struct TAG s50; struct TAG s5F2D; struct TAG s87; struct TAG s9F11; struct TAG s9F12; struct TAG sBF0C; struct TAG s9F4D; }; static int readTLV(uchar rApdu[]); int main(void){ uchar rApdu[] = {0x6F, 0x35 ,0x84 ,0x08 ,0x45 ,0x4F ,0x50 , 0x43 ,0x43 ,0x41 ,0x52 ,0x44 ,0xA5 ,0x29 ,0x50 ,0x06 ,0x55 ,0x5A ,0x4B ,0x41 ,0x52 ,0x54 ,0x5F, 0x2D, 0x06 ,0x75 ,0x7A ,0x72 ,0x75 ,0x65 ,0x6E ,0x87 ,0x01 ,0x01 ,0x9F ,0x11 ,0x01 ,0x01 ,0x9F, 0x12 ,0x06 ,0x55 ,0x5A ,0x4B ,0x41 ,0x52 ,0x54 ,0xBF ,0x0C ,0x05 ,0x9F ,0x4D ,0x02 ,0x0B ,0x0A}; if (readTLV(rApdu) == 0) printf("ok!n"); system("PAUSE"); return false; } static int readTLV(uchar rApdu[]){ uint i, j, aLen, tLen, tlvIndex; struct TLVdata newtlv; aLen = sizeof(rApdu); tlvIndex = 0; switch(rApdu[tlvIndex]){ case k6f: tLen = (int)rApdu[++tlvIndex]; for(i=0; i <= tLen; i++, ++tlvIndex){ if(rApdu[tlvIndex] == 0x84){ newtlv.s84.len = (int)rApdu[++tlvIndex]; ++tlvIndex; newtlv.s84.data = (char*)malloc(10*sizeof(char)); for(j=0; j < newtlv.s84.len; j++, ++tlvIndex){ newtlv.s84.data[j] = rApdu[tlvIndex]; } newtlv.s84.data[j] = '