//////////////////////////////////////////////////
//
// File : FuncHandle.c
// Function: interfaces for CDDRV & CF.
// Description: common interfaces for CDDRV & CF, the user,
// here is the CF, will include grant(initial) CI, add account to CI,
// delete account from CI, change key of account, and clear passwd
// of CI etc function.
// Created on Sep 1, 1999, by ZhangChangBin.
// Last updated on Feb 12, 2001, by ZhangChangBin.
//
// Version: 1.0
//
// Notice: This software has application to multifarious terminal,
// has not to application to PC machine(workstation).
//
// History: This program wes created on sep 1,1999,
// Feb 19, 2000, the account modify, 20 bit account is changed 40
// bit account.
// Apr 18, 2000, the CI version number upgrade, the upgrade modify
// CI config address, account config address, public message address,
// and CI program address.
// Jun 8, 2000, modify 'load public message' function, digit block is
// make of four part, primary digit is divide into four digit block.
// Jul 8, 2000, add new function about remote accredit.
// Add "user sign in, Initial IC Card, Get CDKEY" function for HoBei
// business bank Oct 20,2000.
// Oct 30, 2000, modify 'WriteCDInfo' function.
/////////////////////////////////////////////////////////
#include "stdhead.h"
#include "CddrvPublic.h"
#include "CddrvPrivate.h"
#include "FuncHandle.h"
//////////////////////
// define protocl.
static BOOL LoadCIprog(BYTE *, int);
static BOOL SearchAcc(BYTE *, BYTE *, int);
static BOOL ClearAcc(BYTE *, int, int);
static BOOL ExcutAddAcc(BYTE *, BYTE *, BYTE *, int, int);
static BOOL LoadConfig(BYTE *, int);
static BOOL ReadCfg(char *, BYTE *);
//////////////////////
// Globle Variable.
// MPkg mPkg;
static BYTE cMsgBlock[50] =
{
0xF4,0x07,0x01,0x00,
0x00,0x01,0x02,0x03,0xff,0xff,0xff,0xff
};
static BYTE cMsgBlock1[50] =
{
0xF4,0x07,0x01,0x01,
0x04,0x05,0x06,0x07,0xff,0xff,0xff,0xff, // 单位代码
0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b
};
static BYTE cMsgBlock2[50] =
{
0xF4,0x07,0x01,0x01,
0x08,0x09,0x0a,0x0b,0x0c,0x0d,0xff,0xff, // 开户许可证号
0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b
};
static BYTE cMsgBlock3[50] =
{
0xF4,0x07,0x01,0x01,
0x0e,0x0f,0x10,0x11,0xff,0xff,0xff,0xff, // 开户日期
0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b
};
static BYTE cMsgBlock4[50] =
{
0xF4,0x07,0x00,0x01,
0x12,0x13,0x14,0x15,0x16,0xff,0xff,0xff, // 密码器编号
0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,
0x00,0x00
};
static BYTE bConfigPasswdBuf[64] =
{
0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01
};
///////////////////////////////////////////////////
// Init CI.
//Function: Load CI program, Public message, config table and delete all
// account.
//Input : pSendMsgTo trasmit 40 bytes unit name, 16 bytes unit code,
// 16 bytes open account parmit, 16 bytes open account date and
// 16 bytes CI number.
//Output : pResultMsg return message that function handled error.
//Return : Right return TRUE, error return FALSE.
BOOL InitialCI(BYTE *pSendMsgTo, BYTE * pResultMsg, int nLfd)
{
if((pSendMsgTo == NULL) || (pResultMsg == NULL))
return FALSE;
if(!LoadCIprog(pResultMsg, nLfd))
return FALSE;
if(!LoadPublicMsg(pSendMsgTo, pResultMsg, nLfd))
return FALSE;
if(!LoadConfig(pResultMsg, nLfd))
return FALSE;
if(!ClearAllAcc(pResultMsg, nLfd))
return FALSE;
if(!ClearCIPasswd(pResultMsg, nLfd))
return FALSE;
return TRUE;
}
///////////////////////////////////////////////////
// Add Account.
// Function : add a new account.
// Inpput : pSendMsgTo transmit 20Bytes account.
// Output : pResultMsg return 64 bytes CD_Key.
// Return : Right return TRUE, error return FALSE.
BOOL AddAcc(BYTE *pSendMsgTo, BYTE *pResultMsg, int nLfd)
{
int nRet;
BYTE cRetBuf[50], cKeyBuf[65];
if((pSendMsgTo == NULL) || (pResultMsg == NULL))
return FALSE;
memset(cRetBuf, 0, 50);
memset(cKeyBuf, 0, 65);
if(!SearchAcc(pSendMsgTo, cRetBuf, nLfd))
{
memcpy(pResultMsg, cRetBuf, 3);
return FALSE;
}
nRet = nctohi((char *)cRetBuf, 2);
// Add 'ShutDown'、'Active' two function for jump fail of CD.
if(ShutDown(pResultMsg, nLfd) == FALSE)
return FALSE;
sleep(1);
if(Active(pResultMsg, nLfd) == FALSE)
return FALSE;
if(nRet == 0x0b)
{
memcpy(pResultMsg, ERR_ALREADY_LOAD_TEN_ACCOUNT, 3);
return FALSE;
}
// No this account and no free address;
if(nRet == 0xff)
{
memcpy(pResultMsg, ERR_NO_ACCOUNT_NO_FREEADDRESS, 3);
return FALSE;
}
// This Account already exist and not added again.
if((nRet >= 0x81) && (nRet <= 0x94))
{
memcpy(pResultMsg, ERR_ACCOUNT_EXIST, 3);
return FALSE;
}
if((nRet >= 0x01) && (nRet <= 0x0A))
{
if(!ExcutAddAcc(pSendMsgTo, pResultMsg, cKeyBuf, nRet, nLfd))
return FALSE;
else
memcpy(pResultMsg, cKeyBuf, 64);
}
if((nRet < 0x01) || ((nRet > 0x0b) && (nRet < 0x81)) || ((nRet > 0x94) && (nRet < 0xff)))
{
memcpy(pResultMsg, ERR_SEARCH_ACC_OUTSIDE, ERR_INFO_LEN);
return FALSE;
}
return TRUE;
}
///////////////////////////////////////////////////
// Clear Account.
//Function : Delete a account.
//Input : pSendMsgTo denote account.
//Output : pReslultMsg dnote return handle message.
//Return : Right return TRUE, error return FALSE.
BOOL DelAcc(BYTE *pSendMsgTo, BYTE *pResultMsg, int nLfd)
{
int nRet;
BYTE cRetBuf[50];
if((pSendMsgTo == NULL) || (pResultMsg == NULL))
return FALSE;
memset(cRetBuf, 0, 50);
if(!SearchAcc(pSendMsgTo, cRetBuf, nLfd))
{
memcpy(pResultMsg, cRetBuf, 3);
return FALSE;
}
nRet = nctohi((char *)cRetBuf, 2);
// Add 'ShutDown'、'Active' two function for jump fail of CD.
if(ShutDown(pResultMsg, nLfd) == FALSE)
return FALSE;
sleep(1);
if(Active(pResultMsg, nLfd) == FALSE)
return FALSE;
// This account not exist.
if((nRet < 0x81) || (nRet > 0x94))
{
memcpy(pResultMsg, ERR_ACCOUNT_NOT_EXIST, 3);
return FALSE;
}
// This account already exist and delete it.
if((nRet >= 0x81) && (nRet <= 0x94))
{
if(!ClearAcc(pResultMsg, nRet-0x81, nLfd))
return FALSE;
}
return TRUE;
}
///////////////////////////////////////////////////
// Change Account' key.
// Function : Change Key of a new account.
// Inpput : pSendMsgTo transmit 14Bytes account.
// Output : pResultMsg return 64 bytes Key.
// Return : Right return TRUE, error return FALSE.
BOOL ChangeAccKey(BYTE *pSendMsgTo, BYTE *pResultMsg, int nLfd)
{
int nRet;
BYTE cRetBuf[50], cKeyBuf[65];
if((pSendMsgTo == NULL) || (pResultMsg == NULL))
return FALSE;
memset(cRetBuf, 0, 50);
memset(cKeyBuf, 0, 65);
if(!SearchAcc(pSendMsgTo, cRetBuf, nLfd))
{
memcpy(pResultMsg, cRetBuf, 3);
return FALSE;
}
nRet = nctohi((char *)cRetBuf, 2);
// Add 'ShutDown'、'Active' two function for jump fail of CD.
if(ShutDown(pResultMsg, nLfd) == FALSE)
return FALSE;
sleep(1);
if(Active(pResultMsg, nLfd) == FALSE)
return FALSE;
// Account not exist.
if((nRet < 0x81) || (nRet > 0x94))
{
memcpy(pResultMsg, ERR_ACCOUNT_NOT_EXIST, 3);
return FALSE;
}
// This account already exist and change it's CD_Key.
// parameter cKeyBuf return 64 Bytes CD_key.
if((nRet >= 0x81) && (nRet <= 0x94))
{
if(!ExcutAddAcc(pSendMsgTo, pResultMsg, cKeyBuf, nRet - 0x80, nLfd))
return FALSE;
else
m