IC卡 CN\AN\BIN数据解析 代码参考

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/jeaming1012/article/details/86064220
#define DT_ANS	1        //TLV格式 ANS
#define DT_CN	2        //TLV格式 CN
#define DT_BIN	3        //TLV格式 Binary
#define DT_LR_ANS	4	//ANS解析 NO TAG&LENGTH
#define DT_LR_CN	5	//CN解析  NO TAG&LENGTH
#define DT_LR_BIN	6	//Binary NO tag&length


//TLV VALUE
//将数据补值到TLV格式
int Padding(int iType, unsigned char* pszMsgTag, int iMsgLen, unsigned char* pszData, int& iRetLen)
{
	unsigned char szData[512] = { 0 };
	int iDataLen = 0;

	if (iType == DT_ANS)
	{//左对齐,右补0

		memset(szData, 0, sizeof(szData));
		iDataLen = (iMsgLen*2) > iRetLen ? iRetLen : (iMsgLen*2);
		sprintf_s((char*)szData, (iMsgLen+2) * 2, "%.02s%02x%.*s", pszMsgTag, iMsgLen, iDataLen, pszData);
		 
		iRetLen = iMsgLen + 2;
		memset(pszData, 0, iRetLen);
		Bin2Hex(pszData, szData, iRetLen);
		iRetLen *= 2;

		return 0;
	}
	else if (iType == DT_CN)
	{//左对齐, 右补F
		if (iRetLen < 0)
			return 1;

		for (int i = 0; i != iRetLen; ++i)
		{//压缩数字数据校验,规范可支持A-F字符,此处应用只支持数字
			if (pszData[i] > 0x39 || pszData[i] < 0x30)
				return 1;
		}

		iDataLen = (iMsgLen*2)>iRetLen?iRetLen:iMsgLen;
		sprintf_s((char*)szData, (iMsgLen+2) * 2, "%.02s%02x%.*s", pszMsgTag, iMsgLen, iDataLen, pszData);

		if(((iMsgLen*2)-iRetLen)>0)
			memset(szData + iRetLen + 4, 'F', iMsgLen*2 - iRetLen);
		
		iRetLen = (iMsgLen + 2) * 2;
		pszData[iRetLen] = 0x0;

		return 0;
	}
	else if (iType == DT_BIN)
	{
		memset(szData, 0, sizeof(szData));
		iDataLen = (iMsgLen * 2) >= iRetLen ? (iMsgLen * 2) : iRetLen;
		sprintf_s((char*)szData, (iMsgLen + 2) * 2, "%02s%02x%.*s", pszMsgTag, iMsgLen, iDataLen, pszData);

		iDataLen = (iMsgLen + 2) * 2;
		memcpy(szData, pszData, iDataLen);
		pszData[iDataLen] = 0x0;
		iRetLen = iDataLen;

		return 0;
	}
	else if (iType == DT_LR_ANS)
	{
		memset(szData, 0, sizeof(szData));
		iDataLen = (iMsgLen * 2) > iRetLen ? iRetLen : (iMsgLen * 2);
		sprintf_s((char*)szData, iDataLen + 1, "%.*s", iDataLen, pszData);

		iRetLen = iMsgLen;
		memset(pszData, 0, iRetLen);
		Bin2Hex(pszData, szData, iRetLen);
		iRetLen *= 2;

		return 0;
	}
	else if (iType == DT_LR_CN)
	{//左对齐, 右补F
		if (iRetLen < 0)
			return 1;

		for (int i = 0; i != iRetLen; ++i)
		{//压缩数字,只支持数字
			if (pszData[i] > 0x39 || pszData[i] < 0x30)
				return 1;
		}

		iDataLen = (iMsgLen * 2) > iRetLen ? iRetLen : (iMsgLen * 2);
		sprintf_s((char*)szData, (iMsgLen + 2) * 2, "%.*s", iDataLen, pszData);

		if (((iMsgLen * 2) - iRetLen)>0)
			memset(szData + iRetLen, 'F', iMsgLen * 2 - iRetLen);

		iRetLen = iMsgLen * 2;
		memcpy(pszData, szData, iRetLen);
		pszData[iRetLen] = 0x0;

		return 0;
	}
	else if (iType == DT_LR_BIN)
	{
		memset(szData, 0, sizeof(szData));
		iDataLen = (iMsgLen * 2) >= iRetLen ? (iMsgLen * 2) : iRetLen;
		sprintf_s((char*)szData, (iMsgLen + 2) * 2, "%.*s", iDataLen, pszData);

		iDataLen = iMsgLen * 2;
		memcpy(szData, pszData, iDataLen);
		pszData[iDataLen] = 0x0;
		iRetLen = iDataLen;

		return 0;
	}
	else
		return 1;

	return 0;
}

//TLV
int TrimPadding(int iType, unsigned char* pszData, int& iLen)
{
	unsigned char szData[512] = { 0 };
	int iDataLen = 0;
	int iOffsetR = 0;

	if (iType == DT_ANS)
	{//左对齐,右补0
		Hex2Bin(szData, pszData, iLen);
		iLen /= 2;
		iDataLen = szData[1];
		if (iDataLen >= (iLen - 2))
		{
			for (iOffsetR = iLen; iOffsetR != 0; iOffsetR--)
			{
				if (szData[iOffsetR -1] == 0x0)
					continue;
				else
				{
					iDataLen = iOffsetR - 2;
					break;
				}
			}
		}
		else
		{
			for (iOffsetR = iDataLen + 2; iOffsetR != 0; iOffsetR--)
			{
				if (szData[iOffsetR -1] == 0x0)
					continue;
				else
				{
					iDataLen = iOffsetR - 2;
					break;
				}
			}
		}

		memcpy(pszData, szData + 2, iDataLen);
		pszData[iDataLen] = 0x0;
		iLen = iDataLen;
		return 0;
	}
	else if (iType == DT_CN)
	{//左对齐, 右补F
		Hex2Bin(szData, pszData, 4);
		iDataLen = (szData[1]*2) > (iLen - 4) ? iLen - 4 : (szData[1]*2);

		memset(szData, 0, sizeof(szData));
		memcpy(szData, pszData + 4, iDataLen);
		for (iOffsetR = iDataLen; iOffsetR != 0; iOffsetR--)
		{
			if (szData[iOffsetR-1] == 'F')
				continue;
			else
				break;
		}
	    iDataLen = iOffsetR;

		memcpy(pszData, szData, iDataLen);
		pszData[iDataLen] = 0x0;
		iLen = iDataLen;

		return 0;
	}
	else if (iType == DT_BIN)
	{
		Hex2Bin(szData, pszData, 4);
		iDataLen = (szData[1]*2) > (iLen - 4) ? iLen - 4 : (szData[1]*2);

		memcpy(szData, pszData + 4, iDataLen);
		memcpy(pszData, szData, iDataLen);
		pszData[iDataLen] = 0x0;
		iLen = iDataLen;

		return 0;
	}
	else if (iType == DT_LR_ANS)
	{
		Hex2Bin(szData, pszData, iLen);
		iDataLen  = iLen / 2;

	for (iOffsetR = iDataLen; iOffsetR != 0; iOffsetR--)
	{
		if (szData[iOffsetR - 1] == 0x0)
			continue;
		else
			break;
	}
	iDataLen = iOffsetR;

		memcpy(pszData, szData, iDataLen);
		pszData[iDataLen] = 0x0;
		iLen = iDataLen;
		return 0;
	}
	else if (iType == DT_LR_CN)
	{//左对齐, 右补F
		iDataLen = iLen;

		memcpy(szData, pszData, iDataLen);
		for (iOffsetR = iDataLen; iOffsetR != 0; iOffsetR--)
		{
			if (szData[iOffsetR -1] == 'F')
				continue;
			else
				break;
		}
	    iDataLen = iOffsetR;

		memcpy(pszData, szData, iDataLen);
		pszData[iDataLen] = 0x0;
		iLen = iDataLen;

		return 0;
	}
	else if (iType == DT_LR_BIN)
	{
		return 0;
	}
	else
		return 1;

	return 0;
}


//二进制与ASCII码转换函数
enum DataType
{
	BIN2HEX,
	HEX2BIN
};

int Hex2Bin(unsigned char* outBin, unsigned char* inHex, long lPair_length)
{
	char src1,src2,factor1,factor2;
	long len;
	factor1 = '7';
	factor2 = '7';
    _strupr( (char *)inHex );

	for (len=0; len < lPair_length; len++) 
	{
		src1 = *(inHex+len*2);
		src2 = *(inHex+len*2+1);
		if ((src1>='0') && (src1<='9'))
		    factor1 = '0';
	    else if ((src1>='A') && (src1<='F'))
		    factor1 = '7';
	    else 
			return 1;
		if ((src2>='0') && (src2<='9'))
		    factor2 = '0';
	    else if ((src2>='A') && (src2<='F'))
		    factor2 = '7';
	    else 
			return 1;
		src1 = src1 - factor1;
		src2 = src2 - factor2;
		*outBin++ = (src1 << 4) | src2; 
	}
	return 0;
}

int Bin2Hex(unsigned char* outHex, unsigned char* inBin, long length)
{
	UCHAR hLowbit,hHighbit;
    long i;
	for(i=0;i<length*2;i=i+2)
	{
		hLowbit=inBin[i/2]&0x0f;
		hHighbit=inBin[i/2]/16;
		if(hHighbit>=10)
			outHex[i]=hHighbit+'7';
		else
			outHex[i]=hHighbit+'0';
		if(hLowbit>=10)
			outHex[i+1]=hLowbit+'7';
		else
			outHex[i+1]=hLowbit+'0';
	}
	outHex[length*2]='\0';

	return 0;
}

//HEX2BIN 为宏 数据不大于300 Bytes
int SwitchDataMode(unsigned char* pszData, int* iDataLen, int iSwitchMode)
{
	unsigned char szDataHex[600], szDataBin[300];
	if(!pszData)
		return -1;

	if(iSwitchMode == HEX2BIN)
	{
		Hex2Bin(szDataBin, pszData, *iDataLen);
		memcpy(pszData, szDataBin, *iDataLen/2);
		pszData[*iDataLen/2] = '\0';
		*iDataLen = *iDataLen/2;
		return -1;
	}
	
	if(iSwitchMode == BIN2HEX)
	{
		Bin2Hex(szDataHex, pszData, *iDataLen);
		memcpy(pszData, szDataHex, *iDataLen*2);
		pszData[*iDataLen*2] = '\0';
		*iDataLen = *iDataLen *2;
		return -1;
	}
	return 0;
}

 

展开阅读全文

没有更多推荐了,返回首页