#include "JQ_PSDGenerate.h"

/********************************************************************************************************************
 *                                  新版密码支持BCD码                                                               *
********************************************************************************************************************/
static u32 GetSpecialCodeIndex(u8 ucSpecialCode)
{
	if (ucSpecialCode >= '0' && ucSpecialCode <= '9')
	{
			return ucSpecialCode - 0x30;
	}
	else if (ucSpecialCode >= 'A' && ucSpecialCode <= 'Z')
	{
			return ucSpecialCode - 'A' + 10;
	}
	else if (ucSpecialCode >= 'a' && ucSpecialCode <= 'z')
	{
			return ucSpecialCode - 'a' + 36;
	}
	else
			return 63;
}

static u32 GetiDateInfo(u8 *ucpDate)
{
	u8 ucMonth = ucpDate[0]*10 + ucpDate[1];
	u8 ucDay = ucpDate[2]*10 + ucpDate[3];
	
	return ucMonth << 5 | ucDay;
}

//public static string GetPasswordByShellCode(string shellcode)
//{
//		string ssp = shellcode.Substring(7, 1);
//		string sdate = shellcode.Substring(3, 4);
//		string sindex = shellcode.Substring(8, 5); 

//		UInt32 u32BCD = (UInt32)(((GetiDateInfo(sdate) & 0x1FF) << 23) |
//										 ((GetSpecialCodeIndex(ssp) & 0x3F) << 17) |
//										 (GetiIndex(sindex) & 0x1FFFF));

//		return U32tohexstr(u32BCD);

//}

u8 JQ_GetPasswordByUidCode(const u8 *uidcode,u8* psd)
{
	u8  ucSpecialCode = 0;
	u8 usaDate[4] = {0};
	u32 ulIndex = 0;
	u32 ulBCDPsd = 0;
	u8  i=0;

	
	//581900230A12345
	ucSpecialCode = uidcode[9];
	for(i=5;i<9;i++)
		usaDate[i-5] = uidcode[i]-0x30;
	
	for(i=10;i<15;i++)
	{
		ulIndex *= 10;
		ulIndex += (uidcode[i]-0x30);
	} 
	
	ulBCDPsd = (GetiDateInfo(usaDate) & 0x1FF) << 23 |
						 (GetSpecialCodeIndex(ucSpecialCode) & 0x3F) << 17 |
							ulIndex & 0x1FFFF;
	
//	psd[0] = (ulBCDPsd&0xFF000000)>>24;
//	psd[1] = (ulBCDPsd&0x00FF0000)>>16;
//	psd[2] = (ulBCDPsd&0x0000FF00)>>8;
//	psd[3] =  ulBCDPsd&0x000000FF;
	// 反序
	psd[3] = (ulBCDPsd&0xFF000000)>>24;
	psd[2] = (ulBCDPsd&0x00FF0000)>>16;
	psd[1] = (ulBCDPsd&0x0000FF00)>>8;
	psd[0] =  ulBCDPsd&0x000000FF;
	return 0;
}






