根据PBOC要求完成8583信息的组包及解包

来源:互联网 发布:flame painter mac 编辑:程序博客网 时间:2024/06/06 03:47
  1. ///////////

  2. //#define __DEBUG_ISO8583_MAIN__  
  3. //#define  __//e_log_ISO8583_EASTCOM__  
  4. /******************************************************************************** 
  5. 文件名:ISO8583.c 
  6. 功能:根据PBOC要求完成8583信息的组包及解包。本程序可以灵活定制各类型的8583信息包。 
  7.       最大可支持192数据域。使用数据域指针ISO[ ]前一定要先初始化,否则内存出错。 
  8. 作者:CJH 
  9. 日期:2003/06/05 
  10. *********************************************************************************/  
  11.   
  12. //#define __//e_log_ISO8583_EASTCOM__     //if don't want log file posxxxx.log delete this line  
  13.   
  14.   
  15. //#define __DEBUG_ISO8583_MAIN__   //release version delete this line  
  16. #include <string>   
  17. #include <iostream>   
  18.   
  19. #include "StdAfx.h"  
  20. #include "ISO8583.h"  
  21.   
  22. char *PosISO[L_BITMAP + 1];     //存放数据的指针数组  
  23. char* BankISO[L_BITMAP + 1];  
  24. int  PosFieldLength[L_BITMAP + 1];  
  25. int BankFieldLength[L_BITMAP+1];  
  26.   
  27. int  InitOK = 0;        //指示ISO是否初始化  
  28.   
  29. int InitBank = 0;  
  30.   
  31. FIELD8583   Pos8583[129];  
  32. FIELD8583 Bank8583[129];  
  33. extern char g_szCurrentPath[100];  
  34. /***************************************************************** 
  35. * 函数名:LoadField 
  36. * 功能:从ISO8583.ini文件中读取字段配置表到Pos8583和Bank8583变量中 
  37. * 返回:0 -- 成功 -1 -- 失败 
  38. ******************************************************************/  
  39. int LoadField()  
  40. {  
  41.     FILE* fp;  
  42.     int i,SN,iLen;  
  43.     char c;  
  44.     char line[1024];  
  45.     size_t len;  
  46.     size_t read;  
  47.     char filename[100];  
  48.     sprintf(filename,"%s\\ISO8583.ini",g_szCurrentPath);  
  49.     if ((fp = fopen(filename,"r")) == NULL)  
  50.         return -1;  
  51.     i = 0;  
  52.     while(fgets(line,1024,fp))  
  53.     {  
  54.         if (sscanf(line,"%d,%c,%d   ",&SN,&c,&iLen))  
  55.         {  
  56.             Pos8583[i].SN = SN;  
  57.             Pos8583[i].Type = c;  
  58.             Pos8583[i].Length = iLen;  
  59.             //printf("%d,%c,%d\n",Pos8583[i].SN,Pos8583[i].Type,Pos8583[i].Length);  
  60.             if (Pos8583[i].SN == -1)  
  61.                 break;  
  62.             i++;  
  63.         }  
  64.     }  
  65.       
  66.     i=0;  
  67.     while(fgets(line,1024,fp))  
  68.     {  
  69.         if (sscanf(line,"%d,%c,%d   ",&SN,&c,&iLen))  
  70.         {  
  71.             Bank8583[i].SN = SN;  
  72.             Bank8583[i].Type = c;  
  73.             Bank8583[i].Length = iLen;  
  74.             //printf("%d,%c,%d\n",Bank8583[i].SN,Bank8583[i].Type,Bank8583[i].Length);  
  75.             if (Bank8583[i].SN == -1)  
  76.                 break;  
  77.             i++;  
  78.         }  
  79.     }  
  80.     //if (line)  
  81.     //  free(line);  
  82.     fclose(fp);  
  83.     //printf("%d\n",i);  
  84.     return 0;  
  85. }  
  86.   
  87.   
  88. /******************************************************************************** 
  89. 功能:根据8583的信息结构申请存放数据域的内存,并初始化。 
  90. 参数:无 
  91. 返回值:0:正常 
  92.     -1:内存不足,异常 
  93. *******************************************************************************/  
  94. int InitPosISO(void)  
  95. {  
  96.     //   初始化数组:   正常:0  
  97.     //                不正常:-1  
  98.     int i, iIndex;  
  99.     for(i = 0; i < L_BITMAP + 1; i++)  
  100.     {  
  101.         if((iIndex = FindPos8583(i)) != -1)  
  102.         {  
  103.             if((PosISO[i] = ( char*)malloc(Pos8583[iIndex].Length + 1)) == NULL)  
  104.                 return (-1);  
  105.             else  
  106.             {  
  107.                 PosISO[i][0] = 0;  
  108.             }  
  109.         }  
  110.         else  
  111.         {  
  112.             if((PosISO[i] = (char *)malloc(1)) == NULL)  
  113.                 return (-1);  
  114.         }  
  115.     }  
  116.     InitOK = 1;   
  117.     return 0;  
  118. }  
  119.   
  120. int InitBankISO(void)  
  121. {  
  122.     int i, iIndex;  
  123.     for(i = 0; i < L_BITMAP + 1; i++)  
  124.     {  
  125.         if((iIndex = FindBank8583(i)) != -1)  
  126.         {  
  127.             if((BankISO[i] = ( char*)malloc(Bank8583[iIndex].Length + 1)) == NULL)  
  128.                 return (-1);  
  129.             else  
  130.             {  
  131.                 BankISO[i][0] = 0;  
  132.             }  
  133.         }  
  134.         else  
  135.         {  
  136.             if((BankISO[i] = (char *)malloc(1)) == NULL)  
  137.                 return (-1);  
  138.         }  
  139.     }  
  140.     InitBank = 1;  
  141.     return 0;  
  142. }  
  143.   
  144. /******************************************************************************** 
  145. 功能:初始化数据域。 
  146. 参数:无 
  147. 返回值:无 
  148. *******************************************************************************/  
  149. void ClearPosISO(void)  
  150. {  
  151.     int i, iIndex;  
  152.     memset(PosISO[1], 0, L_BITMAP/8);  
  153.     for (i = 0; i < L_BITMAP + 1; i++)  
  154.     {  
  155.         if((iIndex = FindPos8583(i)) != -1)  
  156.         {  
  157.             memset(PosISO[i], 0, Pos8583[iIndex].Length + 1);         
  158.         }  
  159.     }     
  160. }  
  161.   
  162. void ClearBankISO(void)  
  163. {  
  164.     int i, iIndex;  
  165.     memset(BankISO[1], 0, L_BITMAP/8);  
  166.     for (i = 0; i < L_BITMAP + 1; i++)  
  167.     {  
  168.         if((iIndex = FindBank8583(i)) != -1)  
  169.         {  
  170.             memset(BankISO[i], 0, Bank8583[iIndex].Length + 1);       
  171.         }  
  172.     }  
  173. }  
  174.   
  175. /******************************************************************************** 
  176. 功能:释放数据域内存。 
  177. 参数:无 
  178. 返回值:无 
  179. *******************************************************************************/  
  180. void DelPosISO(void)  
  181. {  
  182.     int i;  
  183.     for (i = 0; i < L_BITMAP + 1; i++)   
  184.     {   
  185.         free(PosISO[i]);   
  186.         PosISO[i] = NULL; // added by zxp 05/04/28   
  187.     }  
  188.       
  189.     InitOK=0;  
  190. }  
  191.   
  192. void DelBankISO(void)  
  193. {  
  194.     int i;  
  195.     for (i = 0; i < L_BITMAP + 1; i++)   
  196.     {   
  197.         free(BankISO[i]);   
  198.         BankISO[i] = NULL;  // added by zxp 05/04/28       
  199.     }  
  200.     InitBank=0;  
  201. }  
  202.   
  203. /******************************************************************************** 
  204. 功能:把数据域的内容存入日志文件。 
  205. 参数:无 
  206. 返回值:无 
  207. *******************************************************************************/  
  208. void RecordISO(void)  
  209. {  
  210. /*  int i; 
  211.     unsigned char  BitTemp, Bit; 
  212.     char TempBuf[200]; 
  213.     int Index; 
  214.     char Type; 
  215.  
  216. //  return; //add for EASTLIGHT 
  217.     for (i = 0; i < L_BITMAP + 1; i++) 
  218.     { 
  219.         if(i == 65) continue; 
  220.         if(i > 0) 
  221.         { 
  222.             BitTemp = ISO[1][(i-1) / 8]; 
  223.             Bit = (i-1) % 8 + 1; 
  224.         } 
  225.          
  226.         else 
  227.         { 
  228.             BitTemp = 0; 
  229.             Bit = 1; 
  230.         } 
  231.  
  232.         if(i == 0 ||i == 1 || GETBIT(BitTemp, Bit)) 
  233.         { 
  234.             Index = FindPos8583(i); 
  235.             if(Index != -1) 
  236.             { 
  237.             Type = Pos8583[Index].Type;  
  238.              
  239.             if(Type == 'b') 
  240.             { 
  241.                 sprintf(TempBuf, "ID:%02d ", i); 
  242.                 //e_log((unsigned char * )TempBuf, strlen(TempBuf), LOG_STRING); 
  243.                 //e_log((unsigned char * )ISO[i], Pos8583[Index].Length, LOG_BITMAP); 
  244.             } 
  245.             else 
  246.             { 
  247.                 sprintf(TempBuf, "ID:%02d %s", i, ISO[i]); 
  248.                 //e_log((unsigned char * )TempBuf, strlen(TempBuf), LOG_FIELD); 
  249.             } 
  250.                  
  251.  
  252.             } 
  253.              
  254.          
  255.         } 
  256.  
  257.      
  258.     } 
  259.     sprintf(TempBuf, "%s\n", "  "); 
  260.     //e_log((unsigned char * )TempBuf, strlen(TempBuf), LOG_FIELD); 
  261.     */  
  262. }  
  263.   
  264. /* 
  265. void DispISO(void) 
  266. { 
  267.     int i; 
  268.     unsigned char BitTemp, Bit; 
  269.     int Index; 
  270.  
  271.  
  272.     for (i = 0; i < L_BITMAP + 1; i++) 
  273.     { 
  274.         if(i == 65) continue; 
  275.         if(i > 0) 
  276.         { 
  277.             BitTemp = ISO[1][(i - 1) / 8]; 
  278.             Bit = (i - 1) % 8 + 1; 
  279.         } 
  280.  
  281.         else 
  282.         { 
  283.             BitTemp = 0; 
  284.             Bit = 1; 
  285.         } 
  286.         if(i == 0 || i == 1 || GETBIT(BitTemp, Bit)) 
  287.         { 
  288.             Index = FindPos8583(i); 
  289.             if(Index != -1) 
  290.             { 
  291.          
  292.                 int iLen; 
  293.                  
  294.                 for(iLen = 0; i < Pos8583[Index].Length; i++) 
  295.                      printf("%02x ", ISO[i][iLen]); 
  296.                 printf("\n"); 
  297.  
  298.             } 
  299.              
  300.          
  301.         } 
  302.  
  303.  
  304.     } 
  305. }*/  
  306.   
  307. /******************************************************************************** 
  308. 功能:把BCD串转换成ASCII字符串 
  309. 参数: 
  310.     BCDBuf:BCD码串 
  311.     Len:转成ASCII码后的字符串长度(byte) 
  312.     CharBuf:输出的ASCII字符串 
  313. 返回值:0:转换成功 
  314. *******************************************************************************/  
  315. int BCDtoSTR(unsigned char *BCDBuf, int Len, char * CharBuf)  
  316. {  
  317.     int i;  
  318.   
  319.     i = 0;  
  320.     *CharBuf = 0;  
  321.     while(i < Len)  
  322.     {  
  323.           
  324.         CharBuf[i++] = ((BCDBuf[i/2] & 0xf0) >> 4) + 0x30;    //BCDBuf[i/2]/16+0x30;  
  325.         if(i == Len)   
  326.             break;  
  327.         else          
  328.         {  
  329.             CharBuf[i++] = (BCDBuf[i/2] & 0x0f) + 0x30;  
  330.         }  
  331.     }  
  332.     CharBuf[i] = 0;  
  333.     return 0;  
  334. }  
  335.    
  336. void DSP_2_HEX(char*dsp, char*hex, int count)   
  337. {   
  338.     int i;   
  339.     /*for(i = 0; i < count; i++)  
  340.     {  
  341.     hex[i]=((dsp[i*2]<=0x39)?dsp[i*2]-0x30:dsp[i*2]-0x41+10);  
  342.         hex[i]=hex[i]<<4;  
  343.         if(dsp[2*i+1])  
  344.             hex[i]+=((dsp[i*2+1]<=0x39)?dsp[i*2+1]-0x30:dsp[i*2+1]-0x41+10);  
  345.     }*/   
  346.     for(i = 0; i < count; i++)   
  347.     {   
  348.         if (dsp[i*2] <=0x39)   
  349.             hex[i] = dsp[i*2] -0x30;   
  350.         else if ((dsp[i*2]>=0x41) && (dsp[i*2] <=0x5a) )   
  351.             hex[i] = dsp[i*2]-0x41+10;   
  352.         else if ((dsp[i*2]>=0x61) && (dsp[i*2] <=0x66) )   
  353.             hex[i] = dsp[i*2]-0x61+10;   
  354.         else   
  355.             hex[i] = dsp[i*2];   
  356.         //hex[i]=((dsp[i*2]<=0x39)?dsp[i*2]-0x30:dsp[i*2]-0x41+10);   
  357.         hex[i]=hex[i]<<4;   
  358.         if(dsp[2*i+1])   
  359.         {   
  360.             if (dsp[i*2+1] <=0x39)   
  361.                 hex[i] += dsp[i*2+1] -0x30;   
  362.             else if ((dsp[i*2+1]>=0x41) && (dsp[i*2+1] <=0x5a) )   
  363.                 hex[i] += dsp[i*2+1]-0x41+10;   
  364.             else if ((dsp[i*2+1]>=0x61) && (dsp[i*2+1] <=0x66) )   
  365.                 hex[i] += dsp[i*2+1]-0x61+10;   
  366.             else   
  367.                 hex[i]+= dsp[i*2+1];   
  368.             //hex[i]+=((dsp[i*2+1]<=0x39)?dsp[i*2+1]-0x30:dsp[i*2+1]-0x41+10);   
  369.         }   
  370.     }   
  371. }     /* Defined already in iso.h */   
  372.    
  373. void HEX_2_DSP(char *hex, char *dsp,int count)   
  374. {   
  375.     int i;   
  376.     char ch;   
  377.     for(i = 0; i < count; i++)   
  378.     {   
  379.         ch=(hex[i]&0xf0)>>4;   
  380.         dsp[i*2]=(ch>9)?ch+0x41-10:ch+0x30;   
  381.         ch=hex[i]&0xf;   
  382.         dsp[i*2+1]=(ch>9)?ch+0x41-10:ch+0x30;   
  383.     }   
  384. }      /* Defined already in iso.h */   
  385.    
  386.   
  387. /******************************************************************************** 
  388. 功能:把ASCII字符串转换成BCD码串 
  389. 参数: 
  390.     InBuf:输入的ASCII字符串 
  391.     Len:ASCII码字符串长度(byte) 
  392.     PadChar:如果字符串为奇数时,添加的字符 
  393.     BCDBuf:输出的BCD码串 
  394. 返回值:0  转换成功 
  395. *******************************************************************************/  
  396. int STRtoBCD(char *InBuf, int Len, char PadChar, unsigned char *BCDBuf)  
  397. {  
  398.     int i, Odd;  
  399.     int PadValue;  
  400.   
  401.     PadValue = (PadChar == '0' ? 0 : 15);  
  402.   
  403.     Odd=(Len % 2 == 0 ? 0 : 1);   
  404.   
  405.     i = 0;  
  406.     while(i < Len)  
  407.     {  
  408.         if(i == Len - 1 && Odd)  
  409.         {  
  410.             BCDBuf[i / 2] = ((InBuf[i] & 0x0f) << 4) + PadValue;  
  411.         }  
  412.         else  
  413.             BCDBuf[i / 2] = ((InBuf[i] & 0x0f) << 4) | (InBuf[i + 1] & 0x0f);   
  414.           
  415.         /*sprintf(Temp,"%02x ",BCDBuf[i/2]); 
  416.         printf("%s ", Temp);    */  
  417.       
  418.         i += 2;  
  419.     }  
  420.     return 0;  
  421. }  
  422.   
  423.   
  424. /******************************************************************************** 
  425. 功能:把指的内存字节串组成信息包,并把返回信息包长度 
  426. 参数: 
  427.     SendBuf:信息包指针 
  428.     scr:数据指针 
  429.     len:数据信息的长度 
  430. 返回值:信息包长度(byte) 
  431. *******************************************************************************/  
  432. int FillSendBuf(unsigned char *SendBuf, char * scr, int len)  
  433. {  
  434.     static int pos = 0;  
  435.   
  436.     if(len < 0)             //Reset send_buf  
  437.     {  
  438.         pos = 0;  
  439.         memset(SendBuf, 0, COMM_LENTH);  
  440.         return 0;  
  441.     }  
  442.     else if(len > 0)                  //Cat string to send_buf  
  443.     {  
  444.         memcpy(SendBuf + pos, scr, len);  
  445.         pos += len;  
  446.           
  447.         return pos;  
  448.     }  
  449.     else  
  450.         return pos;  
  451. }  
  452.   
  453.   
  454. /******************************************************************************** 
  455. 功能:字符转成整数 
  456. 参数: 
  457.     buf:输入的字符 (字符范围: '0'-'9' 'a'-'f' 'A'-'F' 
  458. 返回值:-1:转换出错 
  459.     其它值转换后的整数值 
  460. *******************************************************************************/  
  461. int atoint(unsigned char buf)  
  462. {  
  463.     if(buf >= '0' && buf <= '?')  
  464.         return buf - 48;  
  465.     else if(buf >= 'A' && buf <= 'F')  
  466.         return buf - 55;  
  467.     else if(buf >= 'a' && buf <= 'f')  
  468.         return buf - 87;  
  469.     else  
  470.     {     
  471.        // //e_log((unsigned char * )"E:char to int convert Error!", 28, LOG_FIELD);  
  472.         return -1;  
  473.     }  
  474. }  
  475.   
  476. int FindBank8583(int Index)  
  477. {  
  478.     int i = 0;  
  479.       
  480.     while(Bank8583[i].SN != -1)  
  481.     {  
  482.         if(Bank8583[i].SN == Index) return i;  
  483.         i++;  
  484.     }  
  485.     return -1;  
  486. }  
  487.   
  488. int FindPos8583(int Index)  
  489. {  
  490.     int i = 0;  
  491.       
  492.     while(Pos8583[i].SN != -1)  
  493.     {  
  494.         if(Pos8583[i].SN == Index) return i;  
  495.         i++;  
  496.     }  
  497.     return -1;  
  498. }  
  499.   
  500.   
  501. int BCDtoINT(unsigned char BCDCode)  
  502. {  
  503.     return(BCDCode / 16 * 10 + BCDCode % 16);  
  504. }  
  505.   
  506.   
  507. unsigned char INTtoBCD(int iCode)  
  508. {  
  509.     return((iCode/10) * 16 + (iCode % 10));  
  510. }  
  511.   
  512.   
  513. /*int Exchange_bin(int Index,unsigned char *Buf,int iLen) 
  514. { 
  515.     int iFound, Len, MessLen = 0; 
  516.     char Type; 
  517.      
  518.     if((iFound = FindPos8583(Index)) == -1) 
  519.     {  
  520.         return (-1); 
  521.     } 
  522.     else 
  523.     { 
  524.         Type = Pos8583[iFound].Type; 
  525.         Len = Pos8583[iFound].Length; 
  526.         if (iLen > Len) 
  527.             return (-1); 
  528.         memcpy(ISO[Index], Buf, iLen); 
  529.         ISO[Index][iLen] = 0; 
  530.         return 0; 
  531.     } 
  532. } 
  533. */  
  534.   
  535. /******************************************************************************** 
  536. 功能:存取ISO[]数据组中的内容。 
  537. 参数: 
  538.      Index:数据域 
  539.      act_type: 0:存入   1:取出 
  540.      Buf:输入或输出的内存指针 
  541.      iLen Buf长度 
  542. 返回值:-1:出错 
  543.     0:正常 
  544. *******************************************************************************/  
  545. int PosExchange(int Index, int act_type, unsigned char *Buf,int iLen)  
  546. {  
  547.     int iFound, Len, MessLen = 0;  
  548.     char Type;  
  549.   
  550. //  if(!InitOK) return(-1);  
  551.       
  552. //  printf("bgein exange\n");  
  553.     if((iFound = FindPos8583(Index)) == -1)  
  554.     {   
  555.         return (-1);  
  556.     }  
  557.     else  
  558.     {  
  559.         Type = Pos8583[iFound].Type;  
  560.         Len = Pos8583[iFound].Length;  
  561.           
  562.           
  563.         if(act_type == 0)  
  564.         {  
  565.               
  566.             ////ydy modify 2004-11-22  
  567.             /*if(Type == 's')  //组包 
  568.                 Type = 'l'; 
  569.             else if(Type == 'S') 
  570.                 Type = 'L'; 
  571.             */        
  572.             if(Type != 'b') MessLen = strlen((char*)Buf);  
  573.             //printf("stroe ID %d Type %c Len %d melen %d \n",Index,Type,Len,MessLen);   
  574.               
  575.             if(Type == 'a' || Type == 'n')  
  576.             {  
  577.                 if(MessLen != Len)  
  578.                     return (-1);  
  579.             }  
  580.             else if(Type == 'l' || Type == 'L' || Type == 'v' || Type == 'V')  
  581.             {  
  582.                 if(MessLen > Len)  
  583.                     return (-1);  
  584.             }  
  585.             else if (Type == 's' || Type == 'S')  
  586.             {  
  587.                 if (iLen > Len)  
  588.                     return (-1);  
  589.             }  
  590.             else  
  591.                 ;  
  592.                   
  593.             if(Type == 'b')  
  594.             {  
  595.                 memcpy(PosISO[Index], Buf, Len);  
  596.             }  
  597.             //ydy add 2004-11-22 begin  
  598.             else if (Type == 's')  
  599.             {  
  600.                 PosISO[Index][0] = INTtoBCD(iLen);  
  601.                 memcpy(PosISO[Index]+1, Buf, iLen);  
  602.                 PosISO[Index][iLen+1] = 0;  
  603.             }  
  604.             else if (Type == 'S')  
  605.             {  
  606.                 PosISO[Index][0] = INTtoBCD(iLen/100);  
  607.                 PosISO[Index][1] = INTtoBCD(iLen %100);  
  608.                   
  609.                 //printf("iLen:%d\n",iLen);  
  610.                 memcpy(PosISO[Index]+2, Buf, iLen);  
  611.                 PosISO[Index][iLen+2] = 0;  
  612.             }  
  613.             ///ydy add 2004-11-22 end  
  614.             else  
  615.             {     
  616.                 memcpy(PosISO[Index], Buf, MessLen);  
  617.                 PosISO[Index][MessLen] = 0;  
  618.             }  
  619.               
  620.         }  
  621.         else if(act_type == 1)  
  622.         {  
  623.               
  624.             if(Type != 'b') MessLen = strlen(PosISO[Index]);  
  625.             //printf("stroe ID %d Type %c Len %d melen %d \n",Index,Type,Len,MessLen);   
  626.               
  627.             if(Type == 'a' || Type == 'n')  
  628.             {  
  629.                 if(MessLen != Len)  
  630.                     return (-1);  
  631.             }  
  632.             else if(Type == 'l' || Type=='L' || Type== 'v' || Type== 'V')  
  633.             {  
  634.                 if(MessLen > Len)  
  635.                     return (-1);  
  636.             }  
  637.               
  638.             if(Type == 'b')  
  639.             {  
  640.                 //printf(" item is b type\n");  
  641.                 memcpy(Buf, PosISO[Index], Len);  
  642.             }  
  643.             else if(Type == 'S' || Type == 's')  
  644.             {  
  645.                 memcpy(Buf, PosISO[Index], PosFieldLength[Index]);  
  646.                 Buf[PosFieldLength[Index]] = 0;  
  647.             }  
  648.             else  
  649.             {  
  650.                 //printf("ID %d strlen %d\n",Index, strlen(ISO[Index]));  
  651.                 memcpy(Buf, PosISO[Index], strlen(PosISO[Index]));  
  652.                 Buf[strlen(PosISO[Index])] = 0;  
  653.             }  
  654.         }  
  655.         else  
  656.             return (-1);  
  657.       
  658.         return 0;  
  659.     }  
  660. }  
  661.   
  662. int BankExchange(int Index, int act_type, unsigned char *Buf,int iLen)  
  663. {  
  664.     int iFound, Len, MessLen = 0;  
  665.     char Type;  
  666.   
  667. //  if(!InitOK) return(-1);  
  668.       
  669. //  printf("bgein exange\n");  
  670.     if((iFound = FindBank8583(Index)) == -1)  
  671.     {   
  672.         return (-1);  
  673.     }  
  674.     else  
  675.     {  
  676.         Type = Bank8583[iFound].Type;  
  677.         Len = Bank8583[iFound].Length;  
  678.           
  679.           
  680.         if(act_type == 0)  
  681.         {  
  682.               
  683.             ////ydy modify 2004-11-22  
  684.             /*if(Type == 's')  //组包 
  685.                 Type = 'l'; 
  686.             else if(Type == 'S') 
  687.                 Type = 'L'; 
  688.             */        
  689.             if(Type != 'b') MessLen = strlen((char*)Buf);  
  690.             //printf("stroe ID %d Type %c Len %d melen %d \n",Index,Type,Len,MessLen);   
  691.               
  692.             if(Type == 'a' || Type == 'n')  
  693.             {  
  694.                 if(MessLen != Len)  
  695.                     return (-1);  
  696.             }  
  697.             else if(Type == 'l' || Type == 'L' || Type == 'v' || Type == 'V')  
  698.             {  
  699.                 if(MessLen > Len)  
  700.                     return (-1);  
  701.             }  
  702.             else if (Type == 's' || Type == 'S')  
  703.             {  
  704.                 if (iLen > Len)  
  705.                     return (-1);  
  706.             }  
  707.             else  
  708.                 ;  
  709.                   
  710.             if(Type == 'b')  
  711.             {  
  712.                 memcpy(BankISO[Index], Buf, Len);  
  713.             }  
  714.             //ydy add 2004-11-22 begin  
  715.             else if (Type == 's')  
  716.             {  
  717.                 BankISO[Index][0] = INTtoBCD(iLen);  
  718.                 memcpy(BankISO[Index]+1, Buf, iLen);  
  719.                 BankISO[Index][iLen+1] = 0;  
  720.             }  
  721.             else if (Type == 'S')  
  722.             {  
  723.                 BankISO[Index][0] = INTtoBCD(iLen/100);  
  724.                 BankISO[Index][1] = INTtoBCD(iLen %100);  
  725.                   
  726.                 //printf("iLen:%d\n",iLen);  
  727.                 memcpy(BankISO[Index]+2, Buf, iLen);  
  728.                 BankISO[Index][iLen+2] = 0;  
  729.             }  
  730.             ///ydy add 2004-11-22 end  
  731.             else  
  732.             {     
  733.                 memcpy(BankISO[Index], Buf, MessLen);  
  734.                 BankISO[Index][MessLen] = 0;  
  735.             }  
  736.               
  737.         }  
  738.         else if(act_type == 1)  
  739.         {  
  740.               
  741.             if(Type != 'b') MessLen = strlen(BankISO[Index]);  
  742.             //printf("stroe ID %d Type %c Len %d melen %d \n",Index,Type,Len,MessLen);   
  743.               
  744.             if(Type == 'a' || Type == 'n')  
  745.             {  
  746.                 if(MessLen != Len)  
  747.                     return (-1);  
  748.             }  
  749.             else if(Type == 'l' || Type=='L' || Type== 'v' || Type== 'V')  
  750.             {  
  751.                 if(MessLen > Len)  
  752.                     return (-1);  
  753.             }  
  754.               
  755.             if(Type == 'b')  
  756.             {  
  757.                 //printf(" item is b type\n");  
  758.                 memcpy(Buf, BankISO[Index], Len);  
  759.             }  
  760.             else if(Type == 'S' || Type == 's')  
  761.             {  
  762.                 memcpy(Buf, BankISO[Index], BankFieldLength[Index]);  
  763.                 BankISO[BankFieldLength[Index]] = 0;  
  764.             }  
  765.             else  
  766.             {  
  767.                 //printf("ID %d strlen %d\n",Index, strlen(ISO[Index]));  
  768.                 memcpy(Buf, BankISO[Index], strlen(BankISO[Index]));  
  769.                 BankISO[strlen(BankISO[Index])] = 0;  
  770.             }  
  771.         }  
  772.         else  
  773.             return (-1);  
  774.       
  775.         return 0;  
  776.     }  
  777. }  
  778.   
  779. /********************************************************************** 
  780. 功能:生成异步通信数据包。 
  781. 参数:信息包的指针 
  782. 返回值:正数:信息包的长度(byte) 
  783. ***********************************************************************/  
  784. int AsyMess(unsigned char *inbuf,int Len,unsigned char *outbuf)  
  785. {  
  786.     int i;  
  787.     int LRC;  
  788.       
  789.     /*outbuf[0] = 0x02; 
  790.     outbuf[1] = INTtoBCD(Len/100); 
  791.     outbuf[2] = INTtoBCD(Len%100); 
  792.      
  793.     memcpy(outbuf+3,inbuf,Len); 
  794.     outbuf[3+Len]=0x03; 
  795.     LRC=0; 
  796.     for (i=1;i<Len+4;i++) 
  797.         LRC^=outbuf[i]; 
  798.      
  799.     outbuf[4+Len]=LRC; 
  800.      
  801.     return Len + 5;*/  
  802.       
  803.     //outbuf[0] = 0x02;  
  804. /*  outbuf[0] = INTtoBCD(Len/100); 
  805.     outbuf[1] = INTtoBCD(Len%100); 
  806.      
  807.     memcpy(outbuf+2,inbuf,Len); 
  808.     //outbuf[3+Len]=0x03; 
  809.     //LRC=0; 
  810.     //for (i=1;i<Len+4;i++) 
  811.     //  LRC^=outbuf[i]; 
  812.      
  813.     //outbuf[4+Len]=LRC; 
  814.      
  815.     return Len + 2;*/  
  816.     outbuf[0] = Len / (0x100);  
  817.     outbuf[1] = Len % (0x100);  
  818.       
  819.       
  820.     memcpy(outbuf + 2, inbuf, Len);  
  821.   
  822.     return (Len + 2);  
  823. }  
  824.   
  825.   
  826. /******************************************************************************** 
  827. 功能:根据ISO[1]中BITMAP进行组包。 
  828. 参数:信息包的指针 
  829. 返回值:-1:出错 
  830.     正数:信息包的长度(byte) 
  831. *******************************************************************************/  
  832.   
  833.   
  834. int PosMessagePacket(unsigned char *SendBuf)  
  835. {  
  836.   
  837.     int BitPointer, Pos8583Pointer, SendLen;  
  838.     char ErrMsg[100];  
  839.     //unsigned char TBuf[COMM_LENTH];  
  840.     unsigned char BitTemp, Bit;  
  841.     unsigned char BCDVarLen[4] = {0x00};  
  842.     int FieldLength, MessLength, BcdLength, LenBitMap = 0,i;//VarLengthByte;  
  843.     char Type;  
  844.     unsigned char  TempBuf[FIELD_MAX_LENGTH + 1];  
  845.       
  846.       
  847.     LenBitMap = 64;  
  848. /*  if(GETBIT(ISO[1][0],1))   //add by cjh for 192 bitmap 
  849.     { 
  850.         LenBitMap=128; 
  851.         if(GETBIT(ISO[1][8],1)) 
  852.             LenBitMap=192; 
  853.     } 
  854. */  
  855. /*  if(L_BITMAP==128) 
  856.         { 
  857.            BM_SET(1,ISO[1]); 
  858.      
  859.     } 
  860.      
  861.     else if(L_BITMAP==192) 
  862.     { 
  863.         BM_SET(1,ISO[1]); 
  864.         BM_SET(65,ISO[1]); 
  865.     }*/  
  866.       
  867.     SendLen = FillSendBuf(SendBuf, "1", -1);  
  868.     BitPointer = 0;  
  869.     //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);  
  870.     while(BitPointer <= LenBitMap)  
  871.     {  
  872.   
  873.         if(BitPointer > 1)  
  874.         {  
  875.             BitTemp = PosISO[1][(BitPointer - 1) / 8];  
  876.             Bit = (BitPointer - 1) % 8 + 1;  
  877.         }     
  878.           
  879.         else  
  880.         {  
  881.             BitTemp = 0;  
  882.             Bit = 1;  
  883.         }  
  884.           
  885.         MessLength = 0;  
  886.         if(GETBIT(BitTemp, Bit)|| BitPointer == 0 || BitPointer == 1)  
  887.         {     
  888.             //如果位图存在。Bit 65 位为1不处理  
  889.             if(BitPointer != 65)  
  890.             {  
  891.                 Pos8583Pointer = FindPos8583(BitPointer);  
  892.                   
  893.                 if(Pos8583Pointer != -1)  //如果查找正常  
  894.                 {  
  895.                     Type = Pos8583[Pos8583Pointer].Type;  
  896.                     FieldLength = Pos8583[Pos8583Pointer].Length ;  
  897.                       
  898.                     ///ydy modify 2004-11-22  
  899.                     /*if(Type == 's')  //组报 
  900.                         Type = 'l'; 
  901.                     else if(Type == 'S') 
  902.                         Type = 'L'; 
  903.                     */  
  904.                       
  905.                     if(BitPointer == 1)  
  906.                         FieldLength = LenBitMap / 8;  
  907.                                           
  908.                     if(Type == 'a' || Type == 'b')  
  909.                     {  
  910.                         if(Type == 'a')  
  911.                         {  
  912.                             MessLength = strlen(PosISO[BitPointer]);  
  913.                             if(MessLength != FieldLength)  
  914.                             {  
  915.                               
  916.                                 sprintf(ErrMsg,"Send message length error! a ID:%d", BitPointer);  
  917.                             //  //e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);  
  918. //printf("%s\n",ErrMsg); //for debug                                  
  919.                                 return (-1);  
  920.                             }  
  921.                         }  
  922.                           
  923.                         MessLength = FieldLength;  
  924.                           
  925.                         SendLen = FillSendBuf(SendBuf, PosISO[BitPointer], MessLength);  
  926.                     //  printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);  
  927.                           
  928.                     }   
  929.                     //ydy add 2004-11-11 begin  
  930.                     else if (Type == 's')  
  931.                     {  
  932.                         //sprintf(szTemp,"%02x",ISO[BitPointer][0]);  
  933.                         MessLength =((unsigned char)PosISO[BitPointer][0]/16) * 10 +(unsigned char)PosISO[BitPointer][0] % 16;  
  934.                         SendLen = FillSendBuf(SendBuf, PosISO[BitPointer], MessLength+1);  
  935.                         //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);  
  936.                     }  
  937.                     else if (Type == 'S')  
  938.                     {  
  939.                         //sprintf(szTemp,"%02x%02x",ISO[BitPointer][0],ISO[BitPointer][1]);  
  940.                         //printf("%02x %02x\n",PosISO[BitPointer][0],PosISO[BitPointer][1]);  
  941.                         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;  
  942.                         //printf("MessLength:%d\n",MessLength);  
  943.                         SendLen = FillSendBuf(SendBuf, PosISO[BitPointer], MessLength+2);  
  944.                         //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);  
  945.                         //printf("SendLen:%d\nSendBuf:\n",SendLen);  
  946.                           
  947.                         //for(i=0;i<SendLen;i++)  
  948.                             //printf("%02x ",SendBuf[i]);  
  949.                         //printf("\n");  
  950.                     }  
  951.                     //ydy add 2004-11-11 end  
  952.                     else if(Type == 'l'||Type == 'L')  
  953.                     {  
  954.                         MessLength = strlen(PosISO[BitPointer]);  
  955.                         if(MessLength > FieldLength)  
  956.                         {  
  957.                             sprintf(ErrMsg, "Send Message too long! l L ID:%d", BitPointer);  
  958. //                          //e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);  
  959.                               
  960. //printf("%s\n",ErrMsg); //for debug                                  
  961.                             return -1;  
  962.                         }  
  963.                         if(Type == 'l')  
  964.                         {  
  965.                             BCDVarLen[0] = INTtoBCD(MessLength);  
  966.                             SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 1);  
  967.                             //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);  
  968.                         }  
  969.                         else  
  970.                         {  
  971.                             BCDVarLen[0] = INTtoBCD(MessLength / 100);  
  972.                             BCDVarLen[1] = INTtoBCD(MessLength % 100);  
  973.                             SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 2);  
  974.                             //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);  
  975.                         }  
  976.                           
  977.                         SendLen = FillSendBuf(SendBuf, PosISO[BitPointer], MessLength);  
  978.                         //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);  
  979.                     }  
  980.                     else if(Type == 'n')  
  981.                     {  
  982.                         BcdLength = FieldLength / 2 + FieldLength % 2;  
  983.                         STRtoBCD(PosISO[BitPointer], FieldLength, '0', TempBuf);  
  984.                         SendLen = FillSendBuf(SendBuf, (char *)TempBuf, BcdLength);  
  985.                         //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);  
  986.                     }  
  987.                     else if(Type == 'v')  
  988.                     {  
  989.                         MessLength = strlen(PosISO[BitPointer]);  
  990.                         if(MessLength > FieldLength)  
  991.                         {  
  992.                             sprintf(ErrMsg, "Send Message too long! v ID:%d", BitPointer);  
  993. //                          //e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);  
  994. //printf("%s\n",ErrMsg); //for debug                                  
  995.                             return -1;  
  996.                         }  
  997.                         else  
  998.                         {  
  999.                             BCDVarLen[0] = INTtoBCD(MessLength);  
  1000.                             SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 1);  
  1001.                             //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);  
  1002.                         }  
  1003.                     }  
  1004.                     else if(Type == 'V')  
  1005.                     {  
  1006.   
  1007.                         MessLength = strlen(PosISO[BitPointer]);  
  1008.                         if(MessLength > FieldLength)  
  1009.                         {  
  1010.                             sprintf(ErrMsg, "Send Message too long! V ID:%d", BitPointer);  
  1011.                         //  //e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);  
  1012. //printf("%s\n",ErrMsg); //for debug  
  1013.                             return -1;  
  1014.                         }  
  1015.                         else  
  1016.                         {  
  1017.                             BCDVarLen[0] = INTtoBCD(MessLength / 100);  
  1018.                             BCDVarLen[1] = INTtoBCD(MessLength % 100);  
  1019.                             SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 2);  
  1020.                             //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);  
  1021.                         }  
  1022.                     }  
  1023.                     if(Type == 'v' || Type == 'V')  
  1024.                     {  
  1025.                           
  1026.                         int BCDMessLength;  
  1027.                           
  1028.                         BCDMessLength = MessLength / 2 + MessLength % 2;  
  1029.                           
  1030.                         STRtoBCD(PosISO[BitPointer], MessLength, 'F', TempBuf);  
  1031.                         SendLen = FillSendBuf(SendBuf, (char *)TempBuf, BCDMessLength);  
  1032.                         //printf("BitPointer:%d,SendLen:%d\n",BitPointer,SendLen);  
  1033.                           
  1034.   
  1035.                     }  
  1036.                       
  1037.                 }  
  1038.                 else  
  1039.                 {  
  1040.                     {  
  1041.                         char pMsg[35];  
  1042.                         sprintf (pMsg,"ID %d 8583 field not found!", BitPointer);  
  1043.                     //  //e_log((unsigned char * )pMsg, strlen(pMsg), LOG_FIELD);  
  1044. //printf("%s\n",pMsg); //for debug                                
  1045.                     }  
  1046.                     return -1;  
  1047.                 }  
  1048.             }  
  1049.         } //end of GETBIT  
  1050.         BitPointer++;  
  1051.     }//end of while  
  1052.       
  1053. //  memcpy(TBuf,SendBuf,SendLen);  
  1054. /// SendLen=AsyMess(TBuf,SendLen,SendBuf);  
  1055.       
  1056.     return SendLen;  
  1057. }  
  1058.   
  1059. int BankMessagePacket(unsigned char *SendBuf)  
  1060. {  
  1061.   
  1062.     int BitPointer, Pos8583Pointer, SendLen;  
  1063.     char ErrMsg[100];  
  1064.     //unsigned char TBuf[COMM_LENTH];  
  1065.     unsigned char BitTemp, Bit;  
  1066.     unsigned char BCDVarLen[4] = {0x00};  
  1067.     int FieldLength, MessLength, BcdLength, LenBitMap = 0,i;//VarLengthByte;  
  1068.     char Type;  
  1069.     unsigned char  TempBuf[FIELD_MAX_LENGTH + 1];  
  1070.       
  1071.       
  1072.     LenBitMap = 64;  
  1073. /*  if(GETBIT(ISO[1][0],1))   //add by cjh for 192 bitmap 
  1074.     { 
  1075.         LenBitMap=128; 
  1076.         if(GETBIT(ISO[1][8],1)) 
  1077.             LenBitMap=192; 
  1078.     } 
  1079. */  
  1080. /*  if(L_BITMAP==128) 
  1081.         { 
  1082.            BM_SET(1,ISO[1]); 
  1083.      
  1084.     } 
  1085.      
  1086.     else if(L_BITMAP==192) 
  1087.     { 
  1088.         BM_SET(1,ISO[1]); 
  1089.         BM_SET(65,ISO[1]); 
  1090.     }*/  
  1091.       
  1092.     SendLen = FillSendBuf(SendBuf, "1", -1);  
  1093.     BitPointer = 0;  
  1094.   
  1095.     while(BitPointer <= LenBitMap)  
  1096.     {  
  1097.   
  1098.         if(BitPointer > 1)  
  1099.         {  
  1100.             BitTemp = BankISO[1][(BitPointer - 1) / 8];  
  1101.             Bit = (BitPointer - 1) % 8 + 1;  
  1102.         }     
  1103.           
  1104.         else  
  1105.         {  
  1106.             BitTemp = 0;  
  1107.             Bit = 1;  
  1108.         }  
  1109.           
  1110.         MessLength = 0;  
  1111.         if(GETBIT(BitTemp, Bit)|| BitPointer == 0 || BitPointer == 1)  
  1112.         {     
  1113.             //如果位图存在。Bit 65 位为1不处理  
  1114.             if(BitPointer != 65)  
  1115.             {  
  1116.                 Pos8583Pointer = FindBank8583(BitPointer);  
  1117.                   
  1118.                 if(Pos8583Pointer != -1)  //如果查找正常  
  1119.                 {  
  1120.                     Type = Bank8583[Pos8583Pointer].Type;  
  1121.                     FieldLength = Bank8583[Pos8583Pointer].Length ;  
  1122.                       
  1123.                     ///ydy modify 2004-11-22  
  1124.                     /*if(Type == 's')  //组报 
  1125.                         Type = 'l'; 
  1126.                     else if(Type == 'S') 
  1127.                         Type = 'L'; 
  1128.                     */  
  1129.                       
  1130.                     if(BitPointer == 1)  
  1131.                         FieldLength = LenBitMap / 8;  
  1132.                                           
  1133.                     if(Type == 'a' || Type == 'b')  
  1134.                     {  
  1135.                         if(Type == 'a')  
  1136.                         {  
  1137.                             MessLength = strlen(BankISO[BitPointer]);  
  1138.                             if(MessLength != FieldLength)  
  1139.                             {  
  1140.                               
  1141.                                 sprintf(ErrMsg,"Send message length error! a ID:%d", BitPointer);  
  1142.                                 ////e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);  
  1143. //printf("%s\n",ErrMsg); //for debug                                  
  1144.                                 return (-1);  
  1145.                             }  
  1146.                         }  
  1147.                           
  1148.                         MessLength = FieldLength;  
  1149.                           
  1150.                         SendLen = FillSendBuf(SendBuf, BankISO[BitPointer], MessLength);  
  1151.                     }   
  1152.                     //ydy add 2004-11-11 begin  
  1153.                     else if (Type == 's')  
  1154.                     {  
  1155.                         //sprintf(szTemp,"%02x",ISO[BitPointer][0]);  
  1156.                         MessLength =(BankISO[BitPointer][0]/16) * 10 +BankISO[BitPointer][0] % 16;  
  1157.                         SendLen = FillSendBuf(SendBuf, BankISO[BitPointer], MessLength+1);  
  1158.                     }  
  1159.                     else if (Type == 'S')  
  1160.                     {  
  1161.                         //sprintf(szTemp,"%02x%02x",ISO[BitPointer][0],ISO[BitPointer][1]);  
  1162.                           
  1163.                         MessLength = ((BankISO[BitPointer][0]/16) * 10 +BankISO[BitPointer][0] % 16)*100 +(BankISO[BitPointer][1]/16) * 10 +BankISO[BitPointer][1] % 16;  
  1164.                         //printf("MessLength:%d\n",MessLength);  
  1165.                         SendLen = FillSendBuf(SendBuf, BankISO[BitPointer], MessLength+2);  
  1166.                         //printf("SendLen:%d\nSendBuf:\n",SendLen);  
  1167.                           
  1168.                         //for(i=0;i<SendLen;i++)  
  1169.                             //printf("%02x ",SendBuf[i]);  
  1170.                         //printf("\n");  
  1171.                     }  
  1172.                     //ydy add 2004-11-11 end  
  1173.                     else if(Type == 'l'||Type == 'L')  
  1174.                     {  
  1175.                         MessLength = strlen(BankISO[BitPointer]);  
  1176.                         if(MessLength > FieldLength)  
  1177.                         {  
  1178.                             sprintf(ErrMsg, "Send Message too long! l L ID:%d", BitPointer);  
  1179.                             ////e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);  
  1180.                               
  1181. //printf("%s\n",ErrMsg); //for debug                                  
  1182.                             return -1;  
  1183.                         }  
  1184.                         if(Type == 'l')  
  1185.                         {  
  1186.                             BCDVarLen[0] = INTtoBCD(MessLength);  
  1187.                             SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 1);  
  1188.                         }  
  1189.                         else  
  1190.                         {  
  1191.                             BCDVarLen[0] = INTtoBCD(MessLength / 100);  
  1192.                             BCDVarLen[1] = INTtoBCD(MessLength % 100);  
  1193.                             SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 2);  
  1194.                         }  
  1195.                           
  1196.                         SendLen = FillSendBuf(SendBuf, BankISO[BitPointer], MessLength);  
  1197.                     }  
  1198.                     else if(Type == 'n')  
  1199.                     {  
  1200.                         BcdLength = FieldLength / 2 + FieldLength % 2;  
  1201.                         STRtoBCD(BankISO[BitPointer], FieldLength, '0', TempBuf);  
  1202.                         SendLen = FillSendBuf(SendBuf, (char *)TempBuf, BcdLength);  
  1203.                     }  
  1204.                     else if(Type == 'v')  
  1205.                     {  
  1206.                         MessLength = strlen(BankISO[BitPointer]);  
  1207.                         if(MessLength > FieldLength)  
  1208.                         {  
  1209.                             sprintf(ErrMsg, "Send Message too long! v ID:%d", BitPointer);  
  1210.                             ////e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);  
  1211. //printf("%s\n",ErrMsg); //for debug                                  
  1212.                             return -1;  
  1213.                         }  
  1214.                         else  
  1215.                         {  
  1216.                             BCDVarLen[0] = INTtoBCD(MessLength);  
  1217.                             SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 1);  
  1218.                         }  
  1219.                     }  
  1220.                     else if(Type == 'V')  
  1221.                     {  
  1222.   
  1223.                         MessLength = strlen(BankISO[BitPointer]);  
  1224.                         if(MessLength > FieldLength)  
  1225.                         {  
  1226.                             sprintf(ErrMsg, "Send Message too long! V ID:%d", BitPointer);  
  1227.                             ////e_log((unsigned char * )ErrMsg, strlen(ErrMsg), LOG_FIELD);  
  1228. //printf("%s\n",ErrMsg); //for debug  
  1229.                             return -1;  
  1230.                         }  
  1231.                         else  
  1232.                         {  
  1233.                             BCDVarLen[0] = INTtoBCD(MessLength / 100);  
  1234.                             BCDVarLen[1] = INTtoBCD(MessLength % 100);  
  1235.                             SendLen = FillSendBuf(SendBuf, (char *)BCDVarLen, 2);  
  1236.                         }  
  1237.                     }  
  1238.                     if(Type == 'v' || Type == 'V')  
  1239.                     {  
  1240.                           
  1241.                         int BCDMessLength;  
  1242.                           
  1243.                         BCDMessLength = MessLength / 2 + MessLength % 2;  
  1244.                           
  1245.                         STRtoBCD(BankISO[BitPointer], MessLength, 'F', TempBuf);  
  1246.                         SendLen = FillSendBuf(SendBuf, (char *)TempBuf, BCDMessLength);  
  1247.                           
  1248.                           
  1249.   
  1250.                     }  
  1251.                       
  1252.                 }  
  1253.                 else  
  1254.                 {  
  1255.                     {  
  1256.                         char pMsg[35];  
  1257.                         sprintf (pMsg,"ID %d 8583 field not found!", BitPointer);  
  1258.                         //e_log((unsigned char * )pMsg, strlen(pMsg), LOG_FIELD);  
  1259. //printf("%s\n",pMsg); //for debug                                
  1260.                     }  
  1261.                     return -1;  
  1262.                 }  
  1263.             }  
  1264.         } //end of GETBIT  
  1265.         BitPointer++;  
  1266.     }//end of while  
  1267.       
  1268. //  memcpy(TBuf,SendBuf,SendLen);  
  1269. /// SendLen=AsyMess(TBuf,SendLen,SendBuf);  
  1270.       
  1271.     return SendLen;  
  1272. }  
  1273.   
  1274. /******************************************************************************** 
  1275. 功能:对Buf信息包解包,把各数据域的内容放入ISO数组指针所指的内存中。 
  1276.     (char *)ISO[n]表示数据域n的指针。 
  1277.      
  1278. 参数:Buf:信息包的指针 
  1279. 返回值:-1:出错 
  1280.     0: 正常 
  1281. *******************************************************************************/  
  1282. int PosMessageExpand(unsigned char *Buf)  
  1283. {  
  1284.     int MessPointer, BitPointer, Pos8583Pointer;  
  1285.     int FieldLength, MessLength, VarLengthByte, LenBitMap=64;  
  1286.     char Bitmaps[192];  
  1287.     unsigned char  BitTemp, Bit;  
  1288.     unsigned char  BCDVarLen[4] = {0x00};  
  1289.     unsigned char  TempBcd[200];  
  1290.     char Type;  
  1291.       
  1292.       
  1293.     memset(Bitmaps, 0, sizeof(Bitmaps));  
  1294.     MessPointer = 0;  
  1295.     BitPointer = 0;  
  1296.     Pos8583Pointer = 0;  
  1297.       
  1298.     while(BitPointer <= LenBitMap)  
  1299.     {  
  1300.         if(BitPointer > 1)  
  1301.         {  
  1302.             BitTemp = PosISO[1][(BitPointer - 1) / 8];  
  1303.             Bit = (BitPointer - 1) % 8 + 1;  
  1304.         }  
  1305.         else  
  1306.         {  
  1307.             BitTemp = 0;  
  1308.             Bit = 1;  
  1309.         }  
  1310.           
  1311.         MessLength = 0;  
  1312.         VarLengthByte = 0;  
  1313.         if(GETBIT(BitTemp, Bit) || BitPointer == 0 || BitPointer == 1)  
  1314.         {  
  1315.             //如果位图存在。Bit 65 位为1不处理  
  1316.             if(BitPointer != 65)  
  1317.             {  
  1318.                 Pos8583Pointer = FindPos8583(BitPointer);  
  1319.                 if(Pos8583Pointer != -1)  //如果查找正常  
  1320.                 {  
  1321.                     Type = Pos8583[Pos8583Pointer].Type;  
  1322.                     FieldLength = Pos8583[Pos8583Pointer].Length ;  
  1323.                       
  1324.                     if(Type == 'a' || Type == 'n' || Type == 'b')  
  1325.                     {  
  1326.                         MessLength = FieldLength;  
  1327.                     }  
  1328.                     else   
  1329.                     {  
  1330.                       
  1331.                         if(Type == 'v' || Type == 'l' || Type == 's')  
  1332.                             VarLengthByte = 1;  
  1333.                         else if (Type == 'V'|| Type == 'L'|| Type== 'S')  
  1334.                             VarLengthByte = 2;  
  1335.                           
  1336.                         memcpy(BCDVarLen, Buf + MessPointer, VarLengthByte);  
  1337.                         MessPointer += VarLengthByte;  
  1338.                           
  1339.                         if(Type == 'v' || Type == 'l' || Type == 's')  
  1340.                             MessLength = BCDtoINT(BCDVarLen[0]);  
  1341.                         else if(Type == 'V' || Type == 'L' || Type == 'S')  
  1342.                             MessLength =  BCDtoINT(BCDVarLen[0]) * 100 + BCDtoINT(BCDVarLen[1]);  
  1343.                           
  1344.                         if(MessLength > FieldLength)  
  1345.                         {  
  1346.                             //CString ds;  
  1347.                         //  char Temp[50];  
  1348.                         //  sprintf (Temp,"Filed length error ID:%d,%d",BitPointer,MessLength);  
  1349.                             //AfxMessageBox(ds);  
  1350.                         //  //e_log((unsigned char * )Temp,strlen(Temp),LOG_FIELD);  
  1351.                               
  1352.                             return -1;  
  1353.                         }  
  1354.                     }  
  1355.   
  1356.                     if(Type == 'a' || Type == 'b' || Type == 'l' || Type == 'L' ||Type == 's' || Type == 'S')  
  1357.                     {  
  1358.                         memcpy(PosISO[BitPointer], Buf + MessPointer, MessLength);  
  1359.                         PosISO[BitPointer][MessLength] = 0;  
  1360.                         MessPointer += MessLength;  
  1361.                     }  
  1362.                     else   
  1363.                     {  
  1364.                         int BCDMessLength;  
  1365.                         BCDMessLength = MessLength / 2 + MessLength % 2;  
  1366.                         PosISO[BitPointer][0] = 0;  
  1367.                         memcpy(TempBcd, Buf + MessPointer, BCDMessLength);  
  1368.                         MessPointer += BCDMessLength;  
  1369.                         BCDtoSTR(TempBcd, MessLength, PosISO[BitPointer]);  
  1370.                     }  
  1371.                     PosFieldLength[BitPointer] = MessLength;  
  1372.                 }  
  1373.                 else  
  1374.                 {     
  1375.                 //  {  
  1376.                 //      char Temp[30];  
  1377.                     //  sprintf (Temp, "ID %d 8583 field not found!",BitPointer);  
  1378.                     //  //AfxMessageBox(Disp);  
  1379.                         //sprintf(Temp,"%s\n",Disp);  
  1380.                     //  //e_log((unsigned char * )Temp,strlen(Temp),LOG_FIELD);  
  1381.                 //  }  
  1382.   
  1383.                     return -1;  
  1384.                 }  
  1385.   
  1386.   
  1387.   
  1388.   
  1389.             }  
  1390.   
  1391.             //strcat(Bitmaps,"1");  
  1392.             //AfxMessageBox("1");  
  1393.         }  
  1394.           
  1395.         /*if(BitPointer==1)   //add by cjh for 192bitmaps 
  1396.         { 
  1397.             if(GETBIT(ISO[1][0],1)) 
  1398.             { 
  1399.                 LenBitMap=128; 
  1400.                 if(GETBIT(ISO[1][8],1)) 
  1401.                     LenBitMap=192; 
  1402.             }    
  1403.  
  1404.             MessPointer=MessPointer-24+LenBitMap/8; 
  1405.         }*/  
  1406.     //  printf("LenBitMap is %d\n",LenBitMap);  
  1407.           
  1408.         BitPointer++;  
  1409.     }  
  1410.   
  1411.     return 0;  
  1412. }  
  1413.   
  1414. int BankMessageExpand(unsigned char *Buf)  
  1415. {  
  1416.     int MessPointer, BitPointer, Pos8583Pointer;  
  1417.     int FieldLength, MessLength, VarLengthByte, LenBitMap=64;  
  1418.     char Bitmaps[192];  
  1419.     unsigned char  BitTemp, Bit;  
  1420.     unsigned char  BCDVarLen[4] = {0x00};  
  1421.     unsigned char  TempBcd[200];  
  1422.     char Type;  
  1423.       
  1424.       
  1425.     memset(Bitmaps, 0, sizeof(Bitmaps));  
  1426.     MessPointer = 0;  
  1427.     BitPointer = 0;  
  1428.     Pos8583Pointer = 0;  
  1429.       
  1430.     while(BitPointer <= LenBitMap)  
  1431.     {  
  1432.         if(BitPointer > 1)  
  1433.         {  
  1434.             BitTemp = BankISO[1][(BitPointer - 1) / 8];  
  1435.             Bit = (BitPointer - 1) % 8 + 1;  
  1436.         }  
  1437.         else  
  1438.         {  
  1439.             BitTemp = 0;  
  1440.             Bit = 1;  
  1441.         }  
  1442.           
  1443.         MessLength = 0;  
  1444.         VarLengthByte = 0;  
  1445.         if(GETBIT(BitTemp, Bit) || BitPointer == 0 || BitPointer == 1)  
  1446.         {  
  1447.             //如果位图存在。Bit 65 位为1不处理  
  1448.             if(BitPointer != 65)  
  1449.             {  
  1450.                 Pos8583Pointer = FindBank8583(BitPointer);  
  1451.                 if(Pos8583Pointer != -1)  //如果查找正常  
  1452.                 {  
  1453.                     Type = Bank8583[Pos8583Pointer].Type;  
  1454.                     FieldLength = Bank8583[Pos8583Pointer].Length ;  
  1455.                       
  1456.                     if(Type == 'a' || Type == 'n' || Type == 'b')  
  1457.                     {  
  1458.                         MessLength = FieldLength;  
  1459.                     }  
  1460.                     else   
  1461.                     {  
  1462.                       
  1463.                         if(Type == 'v' || Type == 'l' || Type == 's')  
  1464.                             VarLengthByte = 1;  
  1465.                         else if (Type == 'V'|| Type == 'L'|| Type== 'S')  
  1466.                             VarLengthByte = 2;  
  1467.                           
  1468.                         memcpy(BCDVarLen, Buf + MessPointer, VarLengthByte);  
  1469.                         MessPointer += VarLengthByte;  
  1470.                           
  1471.                         if(Type == 'v' || Type == 'l' || Type == 's')  
  1472.                             MessLength = BCDtoINT(BCDVarLen[0]);  
  1473.                         else if(Type == 'V' || Type == 'L' || Type == 'S')  
  1474.                             MessLength =  BCDtoINT(BCDVarLen[0]) * 100 + BCDtoINT(BCDVarLen[1]);  
  1475.                           
  1476.                         if(MessLength > FieldLength)  
  1477.                         {  
  1478.                             //CString ds;  
  1479.                         //  char Temp[50];  
  1480.                         //  sprintf (Temp,"Filed length error ID:%d,%d",BitPointer,MessLength);  
  1481.                             //AfxMessageBox(ds);  
  1482.                         //  //e_log((unsigned char * )Temp,strlen(Temp),LOG_FIELD);  
  1483.                               
  1484.                             return -1;  
  1485.                         }  
  1486.                     }  
  1487.   
  1488.                     if(Type == 'a' || Type == 'b' || Type == 'l' || Type == 'L' ||Type == 's' || Type == 'S')  
  1489.                     {  
  1490.                         memcpy(BankISO[BitPointer], Buf + MessPointer, MessLength);  
  1491.                         BankISO[BitPointer][MessLength] = 0;  
  1492.                         MessPointer += MessLength;  
  1493.                     }  
  1494.                     else   
  1495.                     {  
  1496.                         int BCDMessLength;  
  1497.                         BCDMessLength = MessLength / 2 + MessLength % 2;  
  1498.                         BankISO[BitPointer][0] = 0;  
  1499.                         memcpy(TempBcd, Buf + MessPointer, BCDMessLength);  
  1500.                         MessPointer += BCDMessLength;  
  1501.                         BCDtoSTR(TempBcd, MessLength, BankISO[BitPointer]);  
  1502.                     }  
  1503.                     BankFieldLength[BitPointer] = MessLength;  
  1504.                 }  
  1505.                 else  
  1506.                 {     
  1507.                 //  {  
  1508.                 //      char Temp[30];  
  1509.                     //  sprintf (Temp, "ID %d 8583 field not found!",BitPointer);  
  1510.                     //  //AfxMessageBox(Disp);  
  1511.                         //sprintf(Temp,"%s\n",Disp);  
  1512.                     //  //e_log((unsigned char * )Temp,strlen(Temp),LOG_FIELD);  
  1513.                 //  }  
  1514.   
  1515.                     return -1;  
  1516.                 }  
  1517.   
  1518.   
  1519.   
  1520.   
  1521.             }  
  1522.   
  1523.             //strcat(Bitmaps,"1");  
  1524.             //AfxMessageBox("1");  
  1525.         }  
  1526.           
  1527.         /*if(BitPointer==1)   //add by cjh for 192bitmaps 
  1528.         { 
  1529.             if(GETBIT(ISO[1][0],1)) 
  1530.             { 
  1531.                 LenBitMap=128; 
  1532.                 if(GETBIT(ISO[1][8],1)) 
  1533.                     LenBitMap=192; 
  1534.             }    
  1535.  
  1536.             MessPointer=MessPointer-24+LenBitMap/8; 
  1537.         }*/  
  1538.     //  printf("LenBitMap is %d\n",LenBitMap);  
  1539.           
  1540.         BitPointer++;  
  1541.     }  
  1542.   
  1543.     return 0;  
  1544. }  
  1545.   
  1546. /******************************************************************************** 
  1547. 功能:取得本机系统的时间:格式:mmdd 月月天天 
  1548. 参数:sztime: 输出时间的字符串指针 
  1549. 返回值:无 
  1550. *******************************************************************************/  
  1551.   
  1552. void gsys_mmdd(char *sztime)  
  1553. {  
  1554.     time_t      t;  
  1555.     struct tm   *ltm;       /* for local time */  
  1556.     time(&t);  
  1557.     ltm = localtime(&t);  
  1558.          sprintf(sztime, "%02d%02d",ltm->tm_mon+1,ltm->tm_mday);  
  1559. }  
  1560.   
  1561. /******************************************************************************** 
  1562. 功能:取得本机系统的时间:格式:yymmddhhmmss 年年月月日日时时分分秒秒 
  1563. 参数:ymdhms: 输出时间的字符串指针,字符串必须至少13bytes长 
  1564. 返回值:无 
  1565. *******************************************************************************/  
  1566. void gsys_ymdhms(char *yymdhms)  
  1567. {  
  1568.     time_t      t;  
  1569.     struct tm   *ltm;       /* for local time */  
  1570.     time(&t);  
  1571.     ltm = localtime(&t);  
  1572.         sprintf(yymdhms, "%02d%02d%02d%02d%02d",  
  1573.                             ltm->tm_mon+1,ltm->tm_mday,  
  1574.                    ltm->tm_hour,ltm->tm_min,ltm->tm_sec);  


http://read.pudn.com/downloads145/sourcecode/crypt/ca/631240/ISO8583.cpp__.htm  转自
  1. }