巧解银联8583数据域

2020-08-04 17:14:54 浏览数 (1)

仔细分析了下8583报文,发现理解之后,解析还是很方便的。

两个函数搞定。

代码语言:javascript复制
/*
**************************************************************************************************************
*Iso8583 报文初始化
Information8583SetRcv[0]  -----> 1域
Information8583SetRcv[1]  -----> 2域
		.				  	   .
		.				  	   .
		.				  	   .
Information8583SetRcv[63] ----->64域
**************************************************************************************************************
*/
void APP_8583_Init( __8583Information  *Information8583Set)
{
	Information8583Set[0].Seat = 0;
	Information8583Set[0].LenLen = 0;
	Information8583Set[0].DataLen = 0;
	Information8583Set[1].Seat = 0;
	Information8583Set[1].LenLen = 1;
	Information8583Set[1].DataLen = 0;
	Information8583Set[2].Seat = 0;
	Information8583Set[2].LenLen = 0;
	Information8583Set[2].DataLen = 3;
	Information8583Set[3].Seat = 0;
	Information8583Set[3].LenLen = 0;
	Information8583Set[3].DataLen = 6;
	Information8583Set[10].Seat = 0;
	Information8583Set[10].LenLen = 0;
	Information8583Set[10].DataLen = 3;
	Information8583Set[11].Seat = 0;
	Information8583Set[11].LenLen = 0;
	Information8583Set[11].DataLen = 3;
	Information8583Set[12].Seat = 0;
	Information8583Set[12].LenLen = 0;
	Information8583Set[12].DataLen = 2;
	Information8583Set[13].Seat = 0;
	Information8583Set[13].LenLen = 0;
	Information8583Set[13].DataLen = 2;
	Information8583Set[14].Seat = 0;
	Information8583Set[14].LenLen = 0;
	Information8583Set[14].DataLen = 2;
	Information8583Set[15].Seat = 0;
	Information8583Set[15].LenLen = 0;
	Information8583Set[15].DataLen = 2;
	Information8583Set[21].Seat = 0;
	Information8583Set[21].LenLen = 0;
	Information8583Set[21].DataLen = 2;
	Information8583Set[22].Seat = 0;
	Information8583Set[22].LenLen = 0;
	Information8583Set[22].DataLen = 2;
	Information8583Set[24].Seat = 0;
	Information8583Set[24].LenLen = 0;
	Information8583Set[24].DataLen = 1;
	Information8583Set[25].Seat = 0;
	Information8583Set[25].LenLen = 0;
	Information8583Set[25].DataLen = 1;
	Information8583Set[31].Seat = 0;
	Information8583Set[31].LenLen = 1;
	Information8583Set[31].DataLen = 0;
	Information8583Set[36].Seat = 0;
	Information8583Set[36].LenLen = 0;
	Information8583Set[36].DataLen = 12;
	Information8583Set[37].Seat = 0;
	Information8583Set[37].LenLen = 0;
	Information8583Set[37].DataLen = 6;
	Information8583Set[38].Seat = 0;
	Information8583Set[38].LenLen = 0;
	Information8583Set[38].DataLen = 2;
	Information8583Set[40].Seat = 0;
	Information8583Set[40].LenLen = 0;
	Information8583Set[40].DataLen = 8;
	Information8583Set[41].Seat = 0;
	Information8583Set[41].LenLen = 0;
	Information8583Set[41].DataLen = 15;
	Information8583Set[43].Seat = 0;
	Information8583Set[43].LenLen = 1;
	Information8583Set[43].DataLen = 0;
	Information8583Set[47].Seat = 0;
	Information8583Set[47].LenLen = 2;
	Information8583Set[47].DataLen = 0;
	Information8583Set[48].Seat = 0;
	Information8583Set[48].LenLen = 0;
	Information8583Set[48].DataLen = 3;
	Information8583Set[51].Seat = 0;
	Information8583Set[51].LenLen = 0;
	Information8583Set[51].DataLen = 8;
	Information8583Set[52].Seat = 0;
	Information8583Set[52].LenLen = 0;
	Information8583Set[52].DataLen = 8;

	Information8583Set[54].Seat = 0;
	Information8583Set[54].LenLen = 2;
	Information8583Set[54].DataLen = 0;
	Information8583Set[59].Seat = 0;
	Information8583Set[59].LenLen = 2;
	Information8583Set[59].DataLen = 0;
	Information8583Set[60].Seat = 0;
	Information8583Set[60].LenLen = 2;
	Information8583Set[60].DataLen = 0;
	Information8583Set[61].Seat = 0;
	Information8583Set[61].LenLen = 2;
	Information8583Set[61].DataLen = 0;
	Information8583Set[62].Seat = 0;
	Information8583Set[62].LenLen = 2;
	Information8583Set[62].DataLen = 0;
	Information8583Set[63].Seat = 0;
	Information8583Set[63].LenLen = 0;
	Information8583Set[63].DataLen = 8;
}
/*
**************************************************************************************************************
*Iso8583 报文解析
**************************************************************************************************************
*/
U32 APP_8583_Analysis( __8583Information  *Information8583SetRcv, U08 *SeatBuf, U08 *DataBuf, U32 BufferLen)
{
	U16 len = 0;
	U08 i,rbuf[5];
	U64 buf = 0,seat=1;
	APP_8583_Init(Information8583SetRcv);
	for(i=0;i<8;i  )
	{
		buf = ((buf<<8) | SeatBuf[i]);
	}
	len  = 23;
	for(i=0;i<64;i  )
	{
		if(buf&(seat<<(63-i)))
		{
			memset(rbuf, 0, 5);
			if(Information8583SetRcv[i].DataLen)
			{
				Information8583SetRcv[i].Seat = len;
				len  = Information8583SetRcv[i].DataLen;
			}
			else
			{	
				if(Information8583SetRcv[i].LenLen == 1)
				{
					sprintf((char*)rbuf,"X",DataBuf[len]);
					Information8583SetRcv[i].DataLen = (U16)atoi((const char*)rbuf);
					len  = 1;
					Information8583SetRcv[i].Seat = len;
					if((i==1)||(i==31)||(i==47)||(i==59)||(i==60))
					{
						Information8583SetRcv[i].DataLen =	Information8583SetRcv[i].DataLen/2   Information8583SetRcv[i].DataLen%2;
					}
				}
				else if(Information8583SetRcv[i].LenLen == 2)
				{
					sprintf((char*)rbuf,"XX", DataBuf[len], DataBuf[len 1]);
					Information8583SetRcv[i].DataLen = (U16)atoi((const char*)rbuf);
					len  = 2;
					Information8583SetRcv[i].Seat = len;
					if((i==1)||(i==31)||(i==59)||(i==60)||(i==61))
					{
						Information8583SetRcv[i].DataLen =	Information8583SetRcv[i].DataLen/2   Information8583SetRcv[i].DataLen%2;
					}
				}
				else
				{
					DEF_UPCash_Debug;
					return 1;
				}
				len  = Information8583SetRcv[i].DataLen;
			}	
		}
	}
	if(len>BufferLen)
	{
		DEF_UPCash_Debug;
		return 1;
	}
	return 0;
}

typedef struct { unsigned short DataLen; unsigned char LenLen; unsigned short Seat; }__8583Information;

0 人点赞