📄 iso8583.c
字号:
/***************************************************
FILE: ISO8583.C
此版本 87 和 93 自动识别.
***************************************************/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "iso8583.h"
#include "def8583_87.h"
#include "def8583_93.h"
struct ISO_8583 *iso8583;
/*---------------------------------------------------
ENTRY: dstr
RETURN; 0 SUCC, return iso
-1 FAIL
-----------------------------------------------------*/
int strtoiso(in_dstr, iso)
char *in_dstr;
ISO_data *iso;
{
unsigned char *dstr;
unsigned char *rpt, bitmask;
unsigned char i,j,n,bitnum;
int off, len, k;
dstr = (unsigned char *)in_dstr;
/* Initialize the iso->f[].bitf */
for (i = 0; i<128; i++) iso->f[i].bitf = 0;
off = 0;
/* Get Message ID */
iso->message_id[0]=(dstr[0]>>4)+'0'; iso->message_id[1]=(dstr[0]&0x0f)+'0';
iso->message_id[2]=(dstr[1]>>4)+'0'; iso->message_id[3]=(dstr[1]&0x0f)+'0';
iso->message_id[4]='\0';
/* Added by LIN JIN FA */
if(iso->message_id[0] == '1') iso8583 = iso8583_93;
else iso8583 = iso8583_87;
/* Get Bitmap bytes */
if (dstr[2] & 0x80) bitnum=16;
else bitnum=8;
/* initialize <rpt> point to data elements */
rpt = dstr+2+bitnum;
/* copy dstr elements to iso */
for(i = 0; i<bitnum; i++) {
bitmask = 0x80;
for(j = 0; j < 8; j++, bitmask>>=1) {
if (i==0 && bitmask==0x80) continue;
if ((dstr[i+2] & bitmask)==0) continue;
n = (i<<3) +j;
/* process variable length data elements */
if (iso8583[n].flag > 0) {
len = (*rpt)-((*rpt)>>4)*6;
rpt++;
if (iso8583[n].flag==2){
len = len*100 + (*rpt)-(*rpt>>4)*6;
rpt++;
}
if (len>iso8583[n].len) {
return -1;
}
} else len = iso8583[n].len;
iso->f[n].len = len;
iso->f[n].addr = &(iso->dbuf[off]);
/* Credit or Debit Char no include in the len */
if (iso8583[n].type&0x01) iso->dbuf[off++]=*rpt++;
/* BCD field Bytes=len/2 */
if (iso8583[n].type&0x07) { len++; len>>=1;}
/* Copy to iso buffer */
for (k=0; k<len; k++) iso->dbuf[off++]=*rpt++;
iso->f[n].bitf=1;
}
}
iso->off = off;
return 0;
}
int isotostr(in_dstr, iso)
char *in_dstr;
ISO_data *iso;
{
unsigned char *dstr;
unsigned char *wpt, bitmask, bitmap;
unsigned char i, j, n, bitnum;
int k, lc;
dstr = (unsigned char *)in_dstr;
/* added by LIN JIN FA */
if(iso->message_id[0] == '1') iso8583 = iso8583_93;
else iso8583 = iso8583_87;
dstr[0]=((iso->message_id[0]-'0')<<4)+(iso->message_id[1]-'0');
dstr[1]=((iso->message_id[2]-'0')<<4)+(iso->message_id[3]-'0');
/* initialize wpt point to data elements */
for(i=64, bitnum=8; i<127; i++)
if(iso->f[i].bitf==0) continue;
else { bitnum=16; break;}
wpt = dstr+2+bitnum;
/* copy iso elements to dstr */
for(i=0; i<bitnum; i++) {
bitmap=0; bitmask=0x80;
for(j=0; j<8; j++, bitmask>>=1) {
n = (i<<3) + j;
if (iso->f[n].bitf==0) continue;
bitmap |= bitmask;
lc = iso->f[n].len;
if (iso8583[n].flag==1) {
(*wpt++)=(unsigned char)((lc%10)+(lc/10)*16);
} else if (iso8583[n].flag==2) {
(*wpt++)=(unsigned char)(lc/100);
(*wpt++)=(unsigned char)( ((lc%100)/10)*16+(lc%100)%10);
}
k=0;
if (iso8583[n].type&0x01) { /* x */
(*wpt++) = iso->f[n].addr[k++]; /* C or D */
lc++;
}
if (iso8583[n].type&0x07) { /* n or z */
lc++; lc>>=1; /* lc = (lc+1)/2 */
}
for (; k<lc ; k++) {
(*wpt++) = iso->f[n].addr[k];
}
}
dstr[i+2]=bitmap;
}
if (bitnum==8 && iso->f[127].bitf) { /* MAC Field */
dstr[bitnum+1]|=0x01;
memcpy(wpt, iso->f[127].addr, 8);
wpt+=8;
}
if (bitnum==16) dstr[2] |= 0x80;
return wpt-dstr;
}
/*---------------------------------------------
RETURN: 0 SUCC
-----------------------------------------------*/
int setbit(iso, n, in_str, len)
ISO_data *iso;
int n;
char *in_str;
int len;
{
int i, l;
unsigned char *pt, tmp[1024];
unsigned char *str;
str = (unsigned char *)in_str;
if (len==0) return 0;
if (n==0) {
memcpy(iso->message_id, str, 4);
iso->message_id[4]='\0';
return 0;
}
/* added by LIN JIN FA */
if( iso->message_id[0] == '1') iso8583 = iso8583_93;
else iso8583 = iso8583_87;
if (n<=1 || n>128) return -1;
n--;
if (len>iso8583[n].len) len = iso8583[n].len;
iso->f[n].bitf=1;
l=len;
if (iso8583[n].flag==0) len=iso8583[n].len;
iso->f[n].len=len;
iso->f[n].addr = &(iso->dbuf[iso->off]);
pt = (unsigned char *)(iso->f[n].addr);
iso->off+=len;
if (iso8583[n].type & 0x01) { /* x */
*(pt++)=*(str++);
(iso->off)++;
}
i=0;
if (iso8583[n].type & 0x03) /* n */
for (; i<len-l; i++) tmp[i]='0';
memcpy(tmp+i, str, l);
i+=l;
if (iso8583[n].type & 0x08) /* b */
for (; i<len; i++) tmp[i]=0;
else for (; i<len; i++) tmp[i]=' '; /* z or others */
if (iso8583[n].type & 0x07)
asc_to_bcd(pt, tmp, len, (unsigned char)(iso8583[n].type&0x03));
else memcpy(pt, tmp, len);
return 0;
}
/*------------------------------------------------------------
ENTRY: iso
n bitmap_num 位元号
RETURN: 0 FAIL
>0 the Length of str
str The result of unpacket
--------------------------------------------------------------*/
int getbit(iso, n, in_str)
ISO_data *iso;
int n;
char *in_str;
{
int j;
unsigned char *pt;
unsigned char *str;
str = (unsigned char *)in_str;
if (n==0) {
memcpy(str, iso->message_id,4);
str[4]='\0';
return 4;
}
/* added by LIN JIN FA */
if(iso->message_id[0] == '1') iso8583 = iso8583_93;
else iso8583 = iso8583_87;
if (n<=1 || n>128) return 0;
n--;
if (iso->f[n].bitf==0) {
if (n==127) if (iso->f[63].bitf==0) return 0;
return 0;
/* n=63; */
}
pt = (unsigned char *)(iso->f[n].addr);
j=iso->f[n].len;
if (iso8583[n].type&0x01) *str++=*pt++;
if (iso8583[n].type&0x07)
bcd_to_asc(str, pt, j, (unsigned char)(iso8583[n].type&0x03));
else memcpy(str, pt, j);
str[j]='\0';
return j;
}
void clearbit(iso)
ISO_data *iso;
{
int i;
for (i=0; i<128; i++) iso->f[i].bitf=0;
iso->off=0;
}
void asc_to_bcd(unsigned char *bcd_buf, unsigned char *ascii_buf,
int conv_len, unsigned char type)
{
int cnt;
char ch, ch1;
if (conv_len&0x01 && type ) ch1=0;
else ch1=0x55;
for (cnt=0; cnt<conv_len; ascii_buf++, cnt++) {
if (*ascii_buf >= 'a' ) ch = *ascii_buf-'a' + 10;
else if ( *ascii_buf >= 'A' ) ch =*ascii_buf- 'A' + 10;
else if ( *ascii_buf >= '0' ) ch =*ascii_buf-'0';
else ch = 0;
if (ch1==0x55) ch1=ch;
else {
*bcd_buf++=ch1<<4 | ch;
ch1=0x55;
}
}
if (ch1!=0x55) *bcd_buf=ch1<<4;
}
void bcd_to_asc(unsigned char *ascii_buf, unsigned char *bcd_buf,
int conv_len, unsigned char type)
{
int cnt;
if (conv_len&0x01 && type) {cnt=1; conv_len++;}
else cnt=0;
for (; cnt<conv_len; cnt++, ascii_buf++){
*ascii_buf = ((cnt&0x01) ? (*bcd_buf++&0x0f) : (*bcd_buf>>4));
*ascii_buf += ((*ascii_buf>9) ? ('A'-10) : '0');
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -