- ///////////
-
- //#define __DEBUG_ISO8583_MAIN__
- //#define __//e_log_ISO8583_EASTCOM__
- /********************************************************************************
- 文件名:ISO8583.c
- 功能:根據PBOC要求完成8583信息的組包及解包。本程序可以靈活定製各類型的8583信息包。
- 最大可支持192數據域。使用數據域指針ISO[ ]前一定要先初始化,否則內存出錯。
- 作者:CJH
- 日期:2003/06/05
- *********************************************************************************/
- //#define __//e_log_ISO8583_EASTCOM__ //if don't want log file posxxxx.log delete this line
- //#define __DEBUG_ISO8583_MAIN__ //release version delete this line
- #include <string>
- #include <iostream>
- #include "StdAfx.h"
- #include "ISO8583.h"
- char *PosISO[L_BITMAP + 1]; //存放數據的指針數組
- char* BankISO[L_BITMAP + 1];
- int PosFieldLength[L_BITMAP + 1];
- int BankFieldLength[L_BITMAP+1];
- int InitOK = 0; //指示ISO是否初始化
- int InitBank = 0;
- FIELD8583 Pos8583[129];
- FIELD8583 Bank8583[129];
- extern char g_szCurrentPath[100];
- /*****************************************************************
- * 函數名:LoadField
- * 功能:從ISO8583.ini文件中讀取字段配置表到Pos8583和Bank8583變量中
- * 返回:0 -- 成功 -1 -- 失敗
- ******************************************************************/
- int LoadField()
- {
- FILE* fp;
- int i,SN,iLen;
- char c;
- char line[1024];
- size_t len;
- size_t read;
- char filename[100];
- sprintf(filename,"%s\\ISO8583.ini",g_szCurrentPath);
- if ((fp = fopen(filename,"r")) == NULL)
- return -1;
- i = 0;
- while(fgets(line,1024,fp))
- {
- if (sscanf(line,"%d,%c,%d ",&SN,&c,&iLen))
- {
- Pos8583[i].SN = SN;
- Pos8583[i].Type = c;
- Pos8583[i].Length = iLen;
- //printf("%d,%c,%d\n",Pos8583[i].SN,Pos8583[i].Type,Pos8583[i].Length);
- if (Pos8583[i].SN == -1)
- break;
- i++;
- }
- }
- i=0;
- while(fgets(line,1024,fp))
- {
- if (sscanf(line,"%d,%c,%d ",&SN,&c,&iLen))
- {
- Bank8583[i].SN = SN;
- Bank8583[i].Type = c;
- Bank8583[i].Length = iLen;
- //printf("%d,%c,%d\n",Bank8583[i].SN,Bank8583[i].Type,Bank8583[i].Length);
- if (Bank8583[i].SN == -1)
- break;
- i++;
- }
- }
- //if (line)
- // free(line);
- fclose(fp);
- //printf("%d\n",i);
- return 0;
- }
- /********************************************************************************
- 功能:根據8583的信息結構申請存放數據域的內存,並初始化。
- 參數:無
- 返回值:0:正常
- -1:內存不足,異常
- *******************************************************************************/
- int InitPosISO(void)
- {
- // 初始化數組: 正常:0
- // 不正常:-1
- int i, iIndex;
- for(i = 0; i < L_BITMAP + 1; i++)
- {
- if((iIndex = FindPos8583(i)) != -1)
- {
- if((PosISO[i] = ( char*)malloc(Pos8583[iIndex].Length + 1)) == NULL)
- return (-1);
- else
- {
- PosISO[i][0] = 0;
- }
- }
- else
- {
- if((PosISO[i] = (char *)malloc(1)) == NULL)
- return (-1);
- }
- }
- InitOK = 1;
- return 0;
- }
- int InitBankISO(void)
- {
- int i, iIndex;
- for(i = 0; i < L_BITMAP + 1; i++)
- {
- if((iIndex = FindBank8583(i)) != -1)
- {
- if((BankISO[i] = ( char*)malloc(Bank8583[iIndex].Length + 1)) == NULL)
- return (-1);
- else
- {
- BankISO[i][0] = 0;
- }
- }
- else
- {
- if((BankISO[i] = (char *)malloc(1)) == NULL)
- return (-1);
- }
- }
- InitBank = 1;
- return 0;
- }
- /********************************************************************************
- 功能:初始化數據域。
- 參數:無
- 返回值:無
- *******************************************************************************/
- void ClearPosISO(void)
- {
- int i, iIndex;
- memset(PosISO[1], 0, L_BITMAP/8);
- for (i = 0; i < L_BITMAP + 1; i++)
- {
- if((iIndex = FindPos8583(i)) != -1)
- {
- memset(PosISO[i], 0, Pos8583[iIndex].Length + 1);
- }
- }
- }
- void ClearBankISO(void)
- {
- int i, iIndex;
- memset(BankISO[1], 0, L_BITMAP/8);
- for (i = 0; i < L_BITMAP + 1; i++)
- {
- if((iIndex = FindBank8583(i)) != -1)
- {
- memset(BankISO[i], 0, Bank8583[iIndex].Length + 1);
- }
- }
- }
- /********************************************************************************
- 功能:釋放數據域內存。
- 參數:無
- 返回值:無
- *******************************************************************************/
- void DelPosISO(void)
- {
- int i;
- for (i = 0; i < L_BITMAP + 1; i++)
- {
- free(PosISO[i]);
- PosISO[i] = NULL; // added by zxp 05/04/28
- }
- InitOK=0;
- }
- void DelBankISO(void)
- {
- int i;
- for (i = 0; i < L_BITMAP + 1; i++)
- {
- free(BankISO[i]);
- BankISO[i] = NULL; // added by zxp 05/04/28
- }
- InitBank=0;
- }
- /********************************************************************************
- 功能:把數據域的內容存入日誌文件。
- 參數:無
- 返回值:無
- *******************************************************************************/
- void RecordISO(void)
- {
- /* int i;
- unsigned char BitTemp, Bit;
- char TempBuf[200];
- int Index;
- char Type;
- // return; //add for EASTLIGHT
- for (i = 0; i < L_BITMAP + 1; i++)
- {
- if(i == 65) continue;
- if(i > 0)
- {
- BitTemp = ISO[1][(i-1) / 8];
- Bit = (i-1) % 8 + 1;
- }
- else
- {
- BitTemp = 0;
- Bit = 1;
- }
- if(i == 0 ||i == 1 || GETBIT(BitTemp, Bit))
- {
- Index = FindPos8583(i);
- if(Index != -1)
- {
- Type = Pos8583[Index].Type;
- if(Type == 'b')
- {
- sprintf(TempBuf, "ID:%02d ", i);
- //e_log((unsigned char * )TempBuf, strlen(TempBuf), LOG_STRING);
- //e_log((unsigned char * )ISO[i], Pos8583[Index].Length, LOG_BITMAP);
- }
- else
- {
- sprintf(TempBuf, "ID:%02d %s", i, ISO[i]);
- //e_log((unsigned char * )TempBuf, strlen(TempBuf), LOG_FIELD);
- }
- }
- }
- }
- sprintf(TempBuf, "%s\n", " ");
- //e_log((unsigned char * )TempBuf, strlen(TempBuf), LOG_FIELD);
- */
- }
- /*
- void DispISO(void)
- {
- int i;
- unsigned char BitTemp, Bit;
- int Index;
- for (i = 0; i < L_BITMAP + 1; i++)
- {
- if(i == 65) continue;
- if(i > 0)
- {
- BitTemp = ISO[1][(i - 1) / 8];
- Bit = (i - 1) % 8 + 1;
- }
- else
- {
- BitTemp = 0;
- Bit = 1;
- }
- if(i == 0 || i == 1 || GETBIT(BitTemp, Bit))
- {
- Index = FindPos8583(i);
- if(Index != -1)
- {
- int iLen;
- for(iLen = 0; i < Pos8583[Index].Length; i++)
- printf("%02x ", ISO[i][iLen]);
- printf("\n");
- }
- }
- }
- }*/
- /********************************************************************************
- 功能:把BCD串轉換成ASCII字符串
- 參數:
- BCDBuf:BCD碼串
- Len:轉成ASCII碼後的字符串長度(byte)
- CharBuf:輸出的ASCII字符串
- 返回值:0:轉換成功
- *******************************************************************************/
- int BCDtoSTR(unsigned char *BCDBuf, int Len, char * CharBuf)
- {
- int i;
- i = 0;
- *CharBuf = 0;
- while(i < Len)
- {
- CharBuf[i++] = ((BCDBuf[i/2] & 0xf0) >> 4) + 0x30; //BCDBuf[i/2]/16+0x30;
- if(i == Len)
- break;
- else
- {
- CharBuf[i++] = (BCDBuf[i/2] & 0x0f) + 0x30;
- }
- }
- CharBuf[i] = 0;
- return 0;
- }
- void DSP_2_HEX(char*dsp, char*hex, int count)
- {
- int i;
- /*for(i = 0; i < count; i++)
- {
- hex[i]=((dsp[i*2]<=0x39)?dsp[i*2]-0x30:dsp[i*2]-0x41+10);
- hex[i]=hex[i]<<4;
- if(dsp[2*i+1])
- hex[i]+=((dsp[i*2+1]<=0x39)?dsp[i*2+1]-0x30:dsp[i*2+1]-0x41+10);
- }*/
- for(i = 0; i < count; i++)
- {
- if (dsp[i*2] <=0x39)
- hex[i] = dsp[i*2] -0x30;
- else if ((dsp[i*2]>=0x41) && (dsp[i*2] <=0x5a) )
- hex[i] = dsp[i*2]-0x41+10;
- else if ((dsp[i*2]>=0x61) && (dsp[i*2] <=0x66) )
- hex[i] = dsp[i*2]-0x61+10;
- else
- hex[i] = dsp[i*2];
- //hex[i]=((dsp[i*2]<=0x39)?dsp[i*2]-0x30:dsp[i*2]-0x41+10);
- hex[i]=hex[i]<<4;
- if(dsp[2*i+1])
- {
- if (dsp[i*2+1] <=0x39)
- hex[i] += dsp[i*2+1] -0x30;
- else if ((dsp[i*2+1]>=0x41) && (dsp[i*2+1] <=0x5a) )
- hex[i] += dsp[i*2+1]-0x41+10;
- else if ((dsp[i*2+1]>=0x61) && (dsp[i*2+1] <=0x66) )
- hex[i] += dsp[i*2+1]-0x61+10;
- else
- hex[i]+= dsp[i*2+1];
- //hex[i]+=((dsp[i*2+1]<=0x39)?dsp[i*2+1]-0x30:dsp[i*2+1]-0x41+10);
- }
- }
- } /* Defined already in iso.h */
- void HEX_2_DSP(char *hex, char *dsp,int count)
- {
- int i;
- char ch;
- for(i = 0; i < count; i++)
- {
- ch=(hex[i]&0xf0)>>4;
- dsp[i*2]=(ch>9)?ch+0x41-10:ch+0x30;
- ch=hex[i]&0xf;
- dsp[i*2+1]=(ch>9)?ch+0x41-10:ch+0x30;
- }
- } /* Defined already in iso.h */
- /********************************************************************************
- 功能:把ASCII字符串轉換成BCD碼串
- 參數:
- InBuf:輸入的ASCII字符串
- Len:ASCII碼字符串長度(byte)
- PadChar:如果字符串爲奇數時,添加的字符
- BCDBuf:輸出的BCD碼串
- 返回值:0 轉換成功
- *******************************************************************************/
- int STRtoBCD(char *InBuf, int Len, char PadChar, unsigned char *BCDBuf)
- {
- int i, Odd;
- int PadValue;
- PadValue = (PadChar == '0' ? 0 : 15);
- Odd=(Len % 2 == 0 ? 0 : 1);
- i = 0;
- while(i < Len)
- {
- if(i == Len - 1 && Odd)
- {
- BCDBuf[i / 2] = ((InBuf[i] & 0x0f) << 4) + PadValue;
- }
- else
- BCDBuf[i / 2] = ((InBuf[i] & 0x0f) << 4) | (InBuf[i + 1] & 0x0f);
- /*sprintf(Temp,"%02x ",BCDBuf[i/2]);
- printf("%s ", Temp); */
- i += 2;
- }
- return 0;
- }
- /********************************************************************************
- 功能:把指的內存字節串組成信息包,並把返回信息包長度
- 參數:
- SendBuf:信息包指針
- scr:數據指針
- len:數據信息的長度
- 返回值:信息包長度(byte)
- *******************************************************************************/
- int FillSendBuf(unsigned char *SendBuf, char * scr, int len)
- {
- static int pos = 0;
- if(len < 0) //Reset send_buf
- {
- pos = 0;
- memset(SendBuf, 0, COMM_LENTH);
- return 0;
- }
- else if(len > 0) //Cat string to send_buf
- {
- memcpy(SendBuf + pos, scr, len);
- pos += len;
- return pos;
- }
- else
- return pos;
- }
- /********************************************************************************
- 功能:字符轉成整數
- 參數:
- buf:輸入的字符 (字符範圍: '0'-'9' 'a'-'f' 'A'-'F'
- 返回值:-1:轉換出錯
- 其它值轉換後的整數值
- *******************************************************************************/
- int atoint(unsigned char buf)
- {
- if(buf >= '0' && buf <= '?')
- return buf - 48;
- else if(buf >= 'A' && buf <= 'F')
- return buf - 55;
- else if(buf >= 'a' && buf <= 'f')
- return buf - 87;
- else
- {
- // //e_log((unsigned char * )"E:char to int convert Error!", 28, LOG_FIELD);
- return -1;
- }
- }
- int FindBank8583(int Index)
- {
- int i = 0;
- while(Bank8583[i].SN != -1)
- {
- if(Bank8583[i].SN == Index) return i;
- i++;
- }
- return -1;
- }
- int FindPos8583(int Index)
- {
- int i = 0;
- while(Pos8583[i].SN != -1)
- {
- if(Pos8583[i].SN == Index) return i;
- i++;
- }
- return -1;
- }
- int BCDtoINT(unsigned char BCDCode)
- {
- return(BCDCode / 16 * 10 + BCDCode % 16);
- }
- unsigned char INTtoBCD(int iCode)
- {
- return((iCode/10) * 16 + (iCode % 10));
- }
- /*int Exchange_bin(int Index,unsigned char *Buf,int iLen)
- {
- int iFound, Len, MessLen = 0;
- char Type;
- if((iFound = FindPos8583(Index)) == -1)
- {
- return (-1);
- }
- else
- {
- Type = Pos8583[iFound].Type;
- Len = Pos8583[iFound].Length;
- if (iLen > Len)
- return (-1);
- memcpy(ISO[Index], Buf, iLen);
- ISO[Index][iLen] = 0;
- return 0;
- }
- }
- */
- /********************************************************************************
- 功能:存取ISO[]數據組中的內容。
- 參數:
- Index:數據域
- act_type: 0:存入 1:取出
- Buf:輸入或輸出的內存指針
- iLen Buf長度
- 返回值:-1:出錯
- 0:正常
- *******************************************************************************/
- int PosExchange(int Index, int act_type, unsigned char *Buf,int iLen)
- {
- int iFound, Len, MessLen = 0;
- char Type;
- // if(!InitOK) return(-1);
- // printf("bgein exange\n");
- if((iFound = FindPos8583(Index)) == -1)
- {
- return (-1);
- }
- else
- {
- Type = Pos8583[iFound].Type;
- Len = Pos8583[iFound].Length;
- if(act_type == 0)
- {
- ////ydy modify 2004-11-22
- /*if(Type == 's') //組包
- Type = 'l';
- else if(Type == 'S')
- Type = 'L';
- */
- if(Type != 'b') MessLen = strlen((char*)Buf);
- //printf("stroe ID %d Type %c Len %d melen %d \n",Index,Type,Len,MessLen);
- if(Type == 'a' || Type == 'n')
- {
- if(MessLen != Len)
- return (-1);
- }
- else if(Type == 'l' || Type == 'L' || Type == 'v' || Type == 'V')
- {
- if(MessLen > Len)
- return (-1);
- }
- else if (Type == 's' || Type == 'S')
- {
- if (iLen > Len)
- return (-1);
- }
- else
- ;
- if(Type == 'b')
- {
- memcpy(PosISO[Index], Buf, Len);
- }
- //ydy add 2004-11-22 begin
- else if (Type == 's')
- {
- PosISO[Index][0] = INTtoBCD(iLen);
- memcpy(PosISO[Index]+1, Buf, iLen);
- PosISO[Index][iLen+1] = 0;
- }
- else if (Type == 'S')
- {
- PosISO[Index][0] = INTtoBCD(iLen/100);
- PosISO[Index][1] = INTtoBCD(iLen %100);
- //printf("iLen:%d\n",iLen);
- memcpy(PosISO[Index]+2, Buf, iLen);
- PosISO[Index][iLen+2] = 0;
- }
- ///ydy add 2004-11-22 end
- else
- {
- memcpy(PosISO[Index], Buf, MessLen);
- PosISO[Index][MessLen] = 0;
- }
- }
- else if(act_type == 1)
- {
- if(Type != 'b') MessLen = strlen(PosISO[Index]);
- //printf("stroe ID %d Type %c Len %d melen %d \n",Index,Type,Len,MessLen);
- if(Type == 'a' || Type == 'n')
- {
- if(MessLen != Len)
- return (-1);
- }
- else if(Type == 'l' || Type=='L' || Type== 'v' || Type== 'V')
- {
- if(MessLen > Len)
- return (-1);
- }
- if(Type == 'b')
- {
- //printf(" item is b type\n");
- memcpy(Buf, PosISO[Index], Len);
- }
- else if(Type == 'S' || Type == 's')
- {
- memcpy(Buf, PosISO[Index], PosFieldLength[Index]);
- Buf[PosFieldLength[Index]] = 0;
- }
- else
- {
- //printf("ID %d strlen %d\n",Index, strlen(ISO[Index]));
- memcpy(Buf, PosISO[Index], strlen(PosISO[Index]));
- Buf[strlen(PosISO[Index])] = 0;
- }
- }
- else
- return (-1);
- return 0;
- }
- }
- int BankExchange(int Index, int act_type, unsigned char *Buf,int iLen)
- {
- int iFound, Len, MessLen = 0;
- char Type;
- // if(!InitOK) return(-1);
- // printf("bgein exange\n");
- if((iFound = FindBank8583(Index)) == -1)
- {
- return (-1);
- }
- else
- {
- Type = Bank8583[iFound].Type;
- Len = Bank8583[iFound].Length;
- if(act_type == 0)
- {
- ////ydy modify 2004-11-22
- /*if(Type == 's') //組包
- Type = 'l';
- else if(Type == 'S')
- Type = 'L';
- */
- if(Type != 'b') MessLen = strlen((char*)Buf);
- //printf("stroe ID %d Type %c Len %d melen %d \n",Index,Type,Len,MessLen);
- if(Type == 'a' || Type == 'n')
- {
- if(MessLen != Len)
- return (-1);
- }
- else if(Type == 'l' || Type == 'L' || Type == 'v' || Type == 'V')
- {
- if(MessLen > Len)
- return (-1);
- }
- else if (Type == 's' || Type == 'S')
- {
- if (iLen > Len)
- return (-1);
- }
- else
- ;
- if(Type == 'b')
- {
- memcpy(BankISO[Index], Buf, Len);
- }
- //ydy add 2004-11-22 begin
- else if (Type == 's')
- {
- BankISO[Index][0] = INTtoBCD(iLen);
- memcpy(BankISO[Index]+1, Buf, iLen);
- BankISO[Index][iLen+1] = 0;
- }
- else if (Type == 'S')
- {
- BankISO[Index][0] = INTtoBCD(iLen/100);
- BankISO[Index][1] = INTtoBCD(iLen %100);
- //printf("iLen:%d\n",iLen);
- memcpy(BankISO[Index]+2, Buf, iLen);
- BankISO[Index][iLen+2] = 0;
- }
- ///ydy add 2004-11-22 end
- else
- {
- memcpy(BankISO[Index], Buf, MessLen);
- BankISO[Index][MessLen] = 0;
- }
- }
- else if(act_type == 1)
- {
- if(Type != 'b') MessLen = strlen(BankISO[Index]);
- //printf("stroe ID %d Type %c Len %d melen %d \n",Index,Type,Len,MessLen);
- if(Type == 'a' || Type == 'n')
- {
- if(MessLen != Len)
- return (-1);
- }
- else if(Type == 'l' || Type=='L' || Type== 'v' || Type== 'V')
- {
- if(MessLen > Len)
- return (-1);
- }
- if(Type == 'b')
- {
- //printf(" item is b type\n");
- memcpy(Buf, BankISO[Index], Len);
- }
- else if(Type == 'S' || Type == 's')
- {
- memcpy(Buf, BankISO[Index], BankFieldLength[Index]);
- BankISO[BankFieldLength[Index]] = 0;
- }
- else
- {
- //printf("ID %d strlen %d\n",Index, strlen(ISO[Index]));
- memcpy(Buf, BankISO[Index], strlen(BankISO[Index]));
- BankISO[strlen(BankISO[Index])] = 0;
- }
- }
- else
- return (-1);
- return 0;
- }
- }
- /**********************************************************************
- 功能:生成異步通信數據包。
- 參數:信息包的指針
- 返回值:正數:信息包的長度(byte)
- ***********************************************************************/
- int AsyMess(unsigned char *inbuf,int Len,unsigned char *outbuf)
- {
- int i;
- int LRC;
- /*outbuf[0] = 0x02;
- outbuf[1] = INTtoBCD(Len/100);
- outbuf[2] = INTtoBCD(Len%100);
- memcpy(outbuf+3,inbuf,Len);
- outbuf[3+Len]=0x03;
- LRC=0;
- for (i=1;i<Len+4;i++)
- LRC^=outbuf[i];
- outbuf[4+Len]=LRC;
- return Len + 5;*/
- //outbuf[0] = 0x02;
- /* outbuf[0] = INTtoBCD(Len/100);
- outbuf[1] = INTtoBCD(Len%100);
- memcpy(outbuf+2,inbuf,Len);
- //outbuf[3+Len]=0x03;
- //LRC=0;
- //for (i=1;i<Len+4;i++)
- // LRC^=outbuf[i];
- //outbuf[4+Len]=LRC;
- return Len + 2;*/
- outbuf[0] = Len / (0x100);
- outbuf[1] = Len % (0x100);
- memcpy(outbuf + 2, inbuf, Len);
- return (Len + 2);
- }
- /********************************************************************************
- 功能:根據ISO[1]中BITMAP進行組包。
- 參數:信息包的指針
- 返回值:-1:出錯
- 正數:信息包的長度(byte)
- *******************************************************************************/
- int PosMessagePacket(unsigned char *SendBuf)
- {
- int BitPointer, Pos8583Pointer, SendLen;
- char ErrMsg[100];
- //unsigned char TBuf[COMM_LENTH];
- unsigned char BitTemp, Bit;
- unsigned char BCDVarLen[4] = {0x00};
- int FieldLength, MessLength, BcdLength, LenBitMap = 0,i;//VarLengthByte;
- char Type;
- unsigned char TempBuf[FIELD_MAX_LENGTH + 1];
- LenBitMap = 64;
- /* if(GETBIT(ISO[1][0],1)) //add by cjh for 192 bitmap
- {
- LenBitMap=128;
- if(GETBIT(ISO[1][8],1))
- LenBitMap=192;
- }
- */
- /* if(L_BITMAP==128)
- {
- BM_SET(1,ISO[1]);
- }
- else if(L_BITMAP==192)
- {
- BM_SET(1,ISO[1]);
- BM_SET(65,ISO[1]);
- }*/
- SendLen = FillSendBuf(SendBuf, "1", -1);
- BitPointer = 0;
- //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);
- while(BitPointer <= LenBitMap)
- {
- if(BitPointer > 1)
- {
- BitTemp = PosISO[1][(BitPointer - 1) / 8];
- Bit = (BitPointer - 1) % 8 + 1;
- }
- else
- {
- BitTemp = 0;
- Bit = 1;
- }
- MessLength = 0;
- if(GETBIT(BitTemp, Bit)|| BitPointer == 0 || BitPointer == 1)
- {
- //如果位圖存在。Bit 65 位爲1不處理
- if(BitPointer != 65)
- {
- Pos8583Pointer = FindPos8583(BitPointer);
- if(Pos8583Pointer != -1) //如果查找正常
- {
- Type = Pos8583[Pos8583Pointer].Type;
- FieldLength = Pos8583[Pos8583Pointer].Length ;
- ///ydy modify 2004-11-22
- /*if(Type == 's') //組報
- Type = 'l';
- else if(Type == 'S')
- Type = 'L';
- */
- if(BitPointer == 1)
- FieldLength = LenBitMap / 8;
- if(Type == 'a' || Type == 'b')
- {
- if(Type == 'a')
- {
- MessLength = strlen(PosISO[BitPointer]);
- if(MessLength != FieldLength)
- {
- sprintf(ErrMsg,"Send message length error! a ID:%d", BitPointer);
- // //e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);
- //printf("%s\n",ErrMsg); //for debug
- return (-1);
- }
- }
- MessLength = FieldLength;
- SendLen = FillSendBuf(SendBuf, PosISO[BitPointer], MessLength);
- // printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);
- }
- //ydy add 2004-11-11 begin
- else if (Type == 's')
- {
- //sprintf(szTemp,"%02x",ISO[BitPointer][0]);
- MessLength =((unsigned char)PosISO[BitPointer][0]/16) * 10 +(unsigned char)PosISO[BitPointer][0] % 16;
- SendLen = FillSendBuf(SendBuf, PosISO[BitPointer], MessLength+1);
- //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);
- }
- else if (Type == 'S')
- {
- //sprintf(szTemp,"%02x%02x",ISO[BitPointer][0],ISO[BitPointer][1]);
- //printf("%02x %02x\n",PosISO[BitPointer][0],PosISO[BitPointer][1]);
- MessLength = (((unsigned char)PosISO[BitPointer][0]/16) * 10 +(unsigned char)PosISO[BitPointer][0] % 16)*100 +((unsigned char)PosISO[BitPointer][1]/16) * 10 +(unsigned char)PosISO[BitPointer][1] % 16;
- //printf("MessLength:%d\n",MessLength);
- SendLen = FillSendBuf(SendBuf, PosISO[BitPointer], MessLength+2);
- //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);
- //printf("SendLen:%d\nSendBuf:\n",SendLen);
- //for(i=0;i<SendLen;i++)
- //printf("%02x ",SendBuf[i]);
- //printf("\n");
- }
- //ydy add 2004-11-11 end
- else if(Type == 'l'||Type == 'L')
- {
- MessLength = strlen(PosISO[BitPointer]);
- if(MessLength > FieldLength)
- {
- sprintf(ErrMsg, "Send Message too long! l L ID:%d", BitPointer);
- // //e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);
- //printf("%s\n",ErrMsg); //for debug
- return -1;
- }
- if(Type == 'l')
- {
- BCDVarLen[0] = INTtoBCD(MessLength);
- SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 1);
- //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);
- }
- else
- {
- BCDVarLen[0] = INTtoBCD(MessLength / 100);
- BCDVarLen[1] = INTtoBCD(MessLength % 100);
- SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 2);
- //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);
- }
- SendLen = FillSendBuf(SendBuf, PosISO[BitPointer], MessLength);
- //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);
- }
- else if(Type == 'n')
- {
- BcdLength = FieldLength / 2 + FieldLength % 2;
- STRtoBCD(PosISO[BitPointer], FieldLength, '0', TempBuf);
- SendLen = FillSendBuf(SendBuf, (char *)TempBuf, BcdLength);
- //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);
- }
- else if(Type == 'v')
- {
- MessLength = strlen(PosISO[BitPointer]);
- if(MessLength > FieldLength)
- {
- sprintf(ErrMsg, "Send Message too long! v ID:%d", BitPointer);
- // //e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);
- //printf("%s\n",ErrMsg); //for debug
- return -1;
- }
- else
- {
- BCDVarLen[0] = INTtoBCD(MessLength);
- SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 1);
- //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);
- }
- }
- else if(Type == 'V')
- {
- MessLength = strlen(PosISO[BitPointer]);
- if(MessLength > FieldLength)
- {
- sprintf(ErrMsg, "Send Message too long! V ID:%d", BitPointer);
- // //e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);
- //printf("%s\n",ErrMsg); //for debug
- return -1;
- }
- else
- {
- BCDVarLen[0] = INTtoBCD(MessLength / 100);
- BCDVarLen[1] = INTtoBCD(MessLength % 100);
- SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 2);
- //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);
- }
- }
- if(Type == 'v' || Type == 'V')
- {
- int BCDMessLength;
- BCDMessLength = MessLength / 2 + MessLength % 2;
- STRtoBCD(PosISO[BitPointer], MessLength, 'F', TempBuf);
- SendLen = FillSendBuf(SendBuf, (char *)TempBuf, BCDMessLength);
- //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);
- }
- }
- else
- {
- {
- char pMsg[35];
- sprintf (pMsg,"ID %d 8583 field not found!", BitPointer);
- // //e_log((unsigned char * )pMsg, strlen(pMsg), LOG_FIELD);
- //printf("%s\n",pMsg); //for debug
- }
- return -1;
- }
- }
- } //end of GETBIT
- BitPointer++;
- }//end of while
- // memcpy(TBuf,SendBuf,SendLen);
- /// SendLen=AsyMess(TBuf,SendLen,SendBuf);
- return SendLen;
- }
- int BankMessagePacket(unsigned char *SendBuf)
- {
- int BitPointer, Pos8583Pointer, SendLen;
- char ErrMsg[100];
- //unsigned char TBuf[COMM_LENTH];
- unsigned char BitTemp, Bit;
- unsigned char BCDVarLen[4] = {0x00};
- int FieldLength, MessLength, BcdLength, LenBitMap = 0,i;//VarLengthByte;
- char Type;
- unsigned char TempBuf[FIELD_MAX_LENGTH + 1];
- LenBitMap = 64;
- /* if(GETBIT(ISO[1][0],1)) //add by cjh for 192 bitmap
- {
- LenBitMap=128;
- if(GETBIT(ISO[1][8],1))
- LenBitMap=192;
- }
- */
- /* if(L_BITMAP==128)
- {
- BM_SET(1,ISO[1]);
- }
- else if(L_BITMAP==192)
- {
- BM_SET(1,ISO[1]);
- BM_SET(65,ISO[1]);
- }*/
- SendLen = FillSendBuf(SendBuf, "1", -1);
- BitPointer = 0;
- while(BitPointer <= LenBitMap)
- {
- if(BitPointer > 1)
- {
- BitTemp = BankISO[1][(BitPointer - 1) / 8];
- Bit = (BitPointer - 1) % 8 + 1;
- }
- else
- {
- BitTemp = 0;
- Bit = 1;
- }
- MessLength = 0;
- if(GETBIT(BitTemp, Bit)|| BitPointer == 0 || BitPointer == 1)
- {
- //如果位圖存在。Bit 65 位爲1不處理
- if(BitPointer != 65)
- {
- Pos8583Pointer = FindBank8583(BitPointer);
- if(Pos8583Pointer != -1) //如果查找正常
- {
- Type = Bank8583[Pos8583Pointer].Type;
- FieldLength = Bank8583[Pos8583Pointer].Length ;
- ///ydy modify 2004-11-22
- /*if(Type == 's') //組報
- Type = 'l';
- else if(Type == 'S')
- Type = 'L';
- */
- if(BitPointer == 1)
- FieldLength = LenBitMap / 8;
- if(Type == 'a' || Type == 'b')
- {
- if(Type == 'a')
- {
- MessLength = strlen(BankISO[BitPointer]);
- if(MessLength != FieldLength)
- {
- sprintf(ErrMsg,"Send message length error! a ID:%d", BitPointer);
- ////e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);
- //printf("%s\n",ErrMsg); //for debug
- return (-1);
- }
- }
- MessLength = FieldLength;
- SendLen = FillSendBuf(SendBuf, BankISO[BitPointer], MessLength);
- }
- //ydy add 2004-11-11 begin
- else if (Type == 's')
- {
- //sprintf(szTemp,"%02x",ISO[BitPointer][0]);
- MessLength =(BankISO[BitPointer][0]/16) * 10 +BankISO[BitPointer][0] % 16;
- SendLen = FillSendBuf(SendBuf, BankISO[BitPointer], MessLength+1);
- }
- else if (Type == 'S')
- {
- //sprintf(szTemp,"%02x%02x",ISO[BitPointer][0],ISO[BitPointer][1]);
- MessLength = ((BankISO[BitPointer][0]/16) * 10 +BankISO[BitPointer][0] % 16)*100 +(BankISO[BitPointer][1]/16) * 10 +BankISO[BitPointer][1] % 16;
- //printf("MessLength:%d\n",MessLength);
- SendLen = FillSendBuf(SendBuf, BankISO[BitPointer], MessLength+2);
- //printf("SendLen:%d\nSendBuf:\n",SendLen);
- //for(i=0;i<SendLen;i++)
- //printf("%02x ",SendBuf[i]);
- //printf("\n");
- }
- //ydy add 2004-11-11 end
- else if(Type == 'l'||Type == 'L')
- {
- MessLength = strlen(BankISO[BitPointer]);
- if(MessLength > FieldLength)
- {
- sprintf(ErrMsg, "Send Message too long! l L ID:%d", BitPointer);
- ////e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);
- //printf("%s\n",ErrMsg); //for debug
- return -1;
- }
- if(Type == 'l')
- {
- BCDVarLen[0] = INTtoBCD(MessLength);
- SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 1);
- }
- else
- {
- BCDVarLen[0] = INTtoBCD(MessLength / 100);
- BCDVarLen[1] = INTtoBCD(MessLength % 100);
- SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 2);
- }
- SendLen = FillSendBuf(SendBuf, BankISO[BitPointer], MessLength);
- }
- else if(Type == 'n')
- {
- BcdLength = FieldLength / 2 + FieldLength % 2;
- STRtoBCD(BankISO[BitPointer], FieldLength, '0', TempBuf);
- SendLen = FillSendBuf(SendBuf, (char *)TempBuf, BcdLength);
- }
- else if(Type == 'v')
- {
- MessLength = strlen(BankISO[BitPointer]);
- if(MessLength > FieldLength)
- {
- sprintf(ErrMsg, "Send Message too long! v ID:%d", BitPointer);
- ////e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);
- //printf("%s\n",ErrMsg); //for debug
- return -1;
- }
- else
- {
- BCDVarLen[0] = INTtoBCD(MessLength);
- SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 1);
- }
- }
- else if(Type == 'V')
- {
- MessLength = strlen(BankISO[BitPointer]);
- if(MessLength > FieldLength)
- {
- sprintf(ErrMsg, "Send Message too long! V ID:%d", BitPointer);
- ////e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);
- //printf("%s\n",ErrMsg); //for debug
- return -1;
- }
- else
- {
- BCDVarLen[0] = INTtoBCD(MessLength / 100);
- BCDVarLen[1] = INTtoBCD(MessLength % 100);
- SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 2);
- }
- }
- if(Type == 'v' || Type == 'V')
- {
- int BCDMessLength;
- BCDMessLength = MessLength / 2 + MessLength % 2;
- STRtoBCD(BankISO[BitPointer], MessLength, 'F', TempBuf);
- SendLen = FillSendBuf(SendBuf, (char *)TempBuf, BCDMessLength);
- }
- }
- else
- {
- {
- char pMsg[35];
- sprintf (pMsg,"ID %d 8583 field not found!", BitPointer);
- //e_log((unsigned char * )pMsg, strlen(pMsg), LOG_FIELD);
- //printf("%s\n",pMsg); //for debug
- }
- return -1;
- }
- }
- } //end of GETBIT
- BitPointer++;
- }//end of while
- // memcpy(TBuf,SendBuf,SendLen);
- /// SendLen=AsyMess(TBuf,SendLen,SendBuf);
- return SendLen;
- }
- /********************************************************************************
- 功能:對Buf信息包解包,把各數據域的內容放入ISO數組指針所指的內存中。
- (char *)ISO[n]表示數據域n的指針。
- 參數:Buf:信息包的指針
- 返回值:-1:出錯
- 0: 正常
- *******************************************************************************/
- int PosMessageExpand(unsigned char *Buf)
- {
- int MessPointer, BitPointer, Pos8583Pointer;
- int FieldLength, MessLength, VarLengthByte, LenBitMap=64;
- char Bitmaps[192];
- unsigned char BitTemp, Bit;
- unsigned char BCDVarLen[4] = {0x00};
- unsigned char TempBcd[200];
- char Type;
- memset(Bitmaps, 0, sizeof(Bitmaps));
- MessPointer = 0;
- BitPointer = 0;
- Pos8583Pointer = 0;
- while(BitPointer <= LenBitMap)
- {
- if(BitPointer > 1)
- {
- BitTemp = PosISO[1][(BitPointer - 1) / 8];
- Bit = (BitPointer - 1) % 8 + 1;
- }
- else
- {
- BitTemp = 0;
- Bit = 1;
- }
- MessLength = 0;
- VarLengthByte = 0;
- if(GETBIT(BitTemp, Bit) || BitPointer == 0 || BitPointer == 1)
- {
- //如果位圖存在。Bit 65 位爲1不處理
- if(BitPointer != 65)
- {
- Pos8583Pointer = FindPos8583(BitPointer);
- if(Pos8583Pointer != -1) //如果查找正常
- {
- Type = Pos8583[Pos8583Pointer].Type;
- FieldLength = Pos8583[Pos8583Pointer].Length ;
- if(Type == 'a' || Type == 'n' || Type == 'b')
- {
- MessLength = FieldLength;
- }
- else
- {
- if(Type == 'v' || Type == 'l' || Type == 's')
- VarLengthByte = 1;
- else if (Type == 'V'|| Type == 'L'|| Type== 'S')
- VarLengthByte = 2;
- memcpy(BCDVarLen, Buf + MessPointer, VarLengthByte);
- MessPointer += VarLengthByte;
- if(Type == 'v' || Type == 'l' || Type == 's')
- MessLength = BCDtoINT(BCDVarLen[0]);
- else if(Type == 'V' || Type == 'L' || Type == 'S')
- MessLength = BCDtoINT(BCDVarLen[0]) * 100 + BCDtoINT(BCDVarLen[1]);
- if(MessLength > FieldLength)
- {
- //CString ds;
- // char Temp[50];
- // sprintf (Temp,"Filed length error ID:%d,%d",BitPointer,MessLength);
- //AfxMessageBox(ds);
- // //e_log((unsigned char * )Temp,strlen(Temp),LOG_FIELD);
- return -1;
- }
- }
- if(Type == 'a' || Type == 'b' || Type == 'l' || Type == 'L' ||Type == 's' || Type == 'S')
- {
- memcpy(PosISO[BitPointer], Buf + MessPointer, MessLength);
- PosISO[BitPointer][MessLength] = 0;
- MessPointer += MessLength;
- }
- else
- {
- int BCDMessLength;
- BCDMessLength = MessLength / 2 + MessLength % 2;
- PosISO[BitPointer][0] = 0;
- memcpy(TempBcd, Buf + MessPointer, BCDMessLength);
- MessPointer += BCDMessLength;
- BCDtoSTR(TempBcd, MessLength, PosISO[BitPointer]);
- }
- PosFieldLength[BitPointer] = MessLength;
- }
- else
- {
- // {
- // char Temp[30];
- // sprintf (Temp, "ID %d 8583 field not found!",BitPointer);
- // //AfxMessageBox(Disp);
- //sprintf(Temp,"%s\n",Disp);
- // //e_log((unsigned char * )Temp,strlen(Temp),LOG_FIELD);
- // }
- return -1;
- }
- }
- //strcat(Bitmaps,"1");
- //AfxMessageBox("1");
- }
- /*if(BitPointer==1) //add by cjh for 192bitmaps
- {
- if(GETBIT(ISO[1][0],1))
- {
- LenBitMap=128;
- if(GETBIT(ISO[1][8],1))
- LenBitMap=192;
- }
- MessPointer=MessPointer-24+LenBitMap/8;
- }*/
- // printf("LenBitMap is %d\n",LenBitMap);
- BitPointer++;
- }
- return 0;
- }
- int BankMessageExpand(unsigned char *Buf)
- {
- int MessPointer, BitPointer, Pos8583Pointer;
- int FieldLength, MessLength, VarLengthByte, LenBitMap=64;
- char Bitmaps[192];
- unsigned char BitTemp, Bit;
- unsigned char BCDVarLen[4] = {0x00};
- unsigned char TempBcd[200];
- char Type;
- memset(Bitmaps, 0, sizeof(Bitmaps));
- MessPointer = 0;
- BitPointer = 0;
- Pos8583Pointer = 0;
- while(BitPointer <= LenBitMap)
- {
- if(BitPointer > 1)
- {
- BitTemp = BankISO[1][(BitPointer - 1) / 8];
- Bit = (BitPointer - 1) % 8 + 1;
- }
- else
- {
- BitTemp = 0;
- Bit = 1;
- }
- MessLength = 0;
- VarLengthByte = 0;
- if(GETBIT(BitTemp, Bit) || BitPointer == 0 || BitPointer == 1)
- {
- //如果位圖存在。Bit 65 位爲1不處理
- if(BitPointer != 65)
- {
- Pos8583Pointer = FindBank8583(BitPointer);
- if(Pos8583Pointer != -1) //如果查找正常
- {
- Type = Bank8583[Pos8583Pointer].Type;
- FieldLength = Bank8583[Pos8583Pointer].Length ;
- if(Type == 'a' || Type == 'n' || Type == 'b')
- {
- MessLength = FieldLength;
- }
- else
- {
- if(Type == 'v' || Type == 'l' || Type == 's')
- VarLengthByte = 1;
- else if (Type == 'V'|| Type == 'L'|| Type== 'S')
- VarLengthByte = 2;
- memcpy(BCDVarLen, Buf + MessPointer, VarLengthByte);
- MessPointer += VarLengthByte;
- if(Type == 'v' || Type == 'l' || Type == 's')
- MessLength = BCDtoINT(BCDVarLen[0]);
- else if(Type == 'V' || Type == 'L' || Type == 'S')
- MessLength = BCDtoINT(BCDVarLen[0]) * 100 + BCDtoINT(BCDVarLen[1]);
- if(MessLength > FieldLength)
- {
- //CString ds;
- // char Temp[50];
- // sprintf (Temp,"Filed length error ID:%d,%d",BitPointer,MessLength);
- //AfxMessageBox(ds);
- // //e_log((unsigned char * )Temp,strlen(Temp),LOG_FIELD);
- return -1;
- }
- }
- if(Type == 'a' || Type == 'b' || Type == 'l' || Type == 'L' ||Type == 's' || Type == 'S')
- {
- memcpy(BankISO[BitPointer], Buf + MessPointer, MessLength);
- BankISO[BitPointer][MessLength] = 0;
- MessPointer += MessLength;
- }
- else
- {
- int BCDMessLength;
- BCDMessLength = MessLength / 2 + MessLength % 2;
- BankISO[BitPointer][0] = 0;
- memcpy(TempBcd, Buf + MessPointer, BCDMessLength);
- MessPointer += BCDMessLength;
- BCDtoSTR(TempBcd, MessLength, BankISO[BitPointer]);
- }
- BankFieldLength[BitPointer] = MessLength;
- }
- else
- {
- // {
- // char Temp[30];
- // sprintf (Temp, "ID %d 8583 field not found!",BitPointer);
- // //AfxMessageBox(Disp);
- //sprintf(Temp,"%s\n",Disp);
- // //e_log((unsigned char * )Temp,strlen(Temp),LOG_FIELD);
- // }
- return -1;
- }
- }
- //strcat(Bitmaps,"1");
- //AfxMessageBox("1");
- }
- /*if(BitPointer==1) //add by cjh for 192bitmaps
- {
- if(GETBIT(ISO[1][0],1))
- {
- LenBitMap=128;
- if(GETBIT(ISO[1][8],1))
- LenBitMap=192;
- }
- MessPointer=MessPointer-24+LenBitMap/8;
- }*/
- // printf("LenBitMap is %d\n",LenBitMap);
- BitPointer++;
- }
- return 0;
- }
- /********************************************************************************
- 功能:取得本機系統的時間:格式:mmdd 月月天天
- 參數:sztime: 輸出時間的字符串指針
- 返回值:無
- *******************************************************************************/
- void gsys_mmdd(char *sztime)
- {
- time_t t;
- struct tm *ltm; /* for local time */
- time(&t);
- ltm = localtime(&t);
- sprintf(sztime, "%02d%02d",ltm->tm_mon+1,ltm->tm_mday);
- }
- /********************************************************************************
- 功能:取得本機系統的時間:格式:yymmddhhmmss 年年月月日日時時分分秒秒
- 參數:ymdhms: 輸出時間的字符串指針,字符串必須至少13bytes長
- 返回值:無
- *******************************************************************************/
- void gsys_ymdhms(char *yymdhms)
- {
- time_t t;
- struct tm *ltm; /* for local time */
- time(&t);
- ltm = localtime(&t);
- sprintf(yymdhms, "%02d%02d%02d%02d%02d",
- ltm->tm_mon+1,ltm->tm_mday,
- ltm->tm_hour,ltm->tm_min,ltm->tm_sec);
- }