⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iso8583.c

📁 银行卡相关 Iso8583的实现代码
💻 C
📖 第 1 页 / 共 2 页
字号:
static char get_bcd_to_asc(bcd)
unsigned char bcd;
{
    return( (bcd & 0xf) + '0' );
}

/***************************************************************************
 Convert ASCII to packet BCD but first place leading zeros.
***************************************************************************/
max_asc_to_bcd(c,max)
int c,max;
{
    while ( c < max ) 
    {
        if ( max-- & 1 ) 
        {
            *dst_8583++ = 0;
        }
    }
    asc_to_bcd( max );
}

/***************************************************************************
 ASCII to packed BCD. ASCII string truncated on right if too long.
***************************************************************************/
max_asc_to_bv(c)
int c;
{
    while ( c-- > 0 ) 
    {
        *dst_8583 = get_asc_to_bcd() << 4;  /* most significant nibble  */
        if ( c > 0 ) 
        {                      /* more to go               */
            *dst_8583 |= get_asc_to_bcd();  /* least significant nibble */
            c--;
        }
        else 
           if (pad_nibble_8583)	    /* non-zero pad             */
               *dst_8583 |= (pad_nibble_8583 & 0x0F);
        dst_8583++;
    }
}

/***************************************************************************
 Simply move c BCD nibbles from source to destination. Since there are two
 nibbles per byte and everything is byte aligned this is done as byte moves.
***************************************************************************/
void bcd_to_bcd(c)
int c;
{
    while ( c > 0 ) 
    {
        *dst_8583++ = *src_8583++;
        c -= 2;
    }
}

/***************************************************************************
 Transfer packed BCD to ASCII. If odd number then advance source ptr.
***************************************************************************/
static void max_bv_to_asc(c)
int c;
{
    int i = 0;

    while ( c-- > 0 ) 
    {
        *dst_8583++ = get_bcd_to_asc(i++ & 1 ? *src_8583++ : *src_8583 >> 4);
    }
    if ( i & 1 )
        src_8583++;
}

/***************************************************************************
 Packed BCD to null terminated ASCII, advance source if odd number.
***************************************************************************/
copy_bv_to_str(c,max)
int c,max;
{
    max_bv_to_asc(c,max);
    *dst_8583++ = 0;
}

/***************************************************************************
 Simply transfer c bytes from source to destination.
***************************************************************************/
void asc_to_asc(c)
int c;
{
/*
    while ( c-- > 0 )
        *dst_8583++ = *src_8583++;
*/
    memcpy( dst_8583, src_8583, c );
    dst_8583 += c;
    src_8583 += c;
}

/***************************************************************************
 Convert c ASCII digits to packed BCD.
***************************************************************************/
void asc_to_bcd(c)
int c;
{
    /* Use this logic if you don't have the SVC function */
    /* ....................................................
    unsigned char bcd = 0;
    while ( c > 0 ) 
    {
        if (c-- & 1)
            *dst_8583++ = bcd | get_asc_to_bcd();
        else
            bcd = get_asc_to_bcd() << 4;
    }
    .................................................... */

    /* Odd length requires leading zero in most sig nibble */
    if ( c & 1 ) 
    {
        *dst_8583++ = *src_8583++ - '0';
        c--;
    }
    SVC_DSP_2_HEX( src_8583, dst_8583, c >> 1 );
    src_8583 += c;
    dst_8583 += c >> 1;
}

/***************************************************************************
 Move c characters from source to destination then place zero into
 destination. Simply makes a null terminated string out of c bytes.
***************************************************************************/
void asc_to_str(c)
int c;
{
    asc_to_asc( c );
    *dst_8583++ = 0;
}

/***************************************************************************
 Transform a counted ASCII string to a null terminated one. The count is
 in the format explained in put_len_v2.
***************************************************************************/
void av2_to_str(c)
int c;
{
    asc_to_asc( get_len_v2() );
    *dst_8583++ = 0;
}

/***************************************************************************
 Simple copy a 2 byte counted string from source to destination.
 The count is in the format explained in put_len_v3.
***************************************************************************/
void av3_to_av3(c)
int c;
{
    asc_to_asc( put_len_v3(get_len_v3()) );
}

/***************************************************************************
 Transform a 2 byte counted string of ASCII to a null terminated one.
 The count is in the format explained in put_len_v3.
***************************************************************************/
void av3_to_str(c)
int c;
{
    asc_to_asc( get_len_v3() );
    *dst_8583++ = 0;
}

/***************************************************************************
 Simply expand a packed BCD sequence into its ASCII equivalent. Assumes
 all the BCD digits are in the range 0..9.
***************************************************************************/
void bcd_to_asc(c)
int c;
{
    /* Use this logic if you don't have the SVC function */
    /* .....................................................................
    while ( c > 0 )
        *dst_8583++ = get_bcd_to_asc( c--&1 ? *src_8583++ : *src_8583>>4 );
    ..................................................................... */

    if ( c & 1 ) 
    {
        *dst_8583++ = *src_8583++ + '0';
        c--;
    }
    /* Count needs to be given in terms of hex pairs */
    SVC_HEX_2_DSP( src_8583, dst_8583, c >> 1 );
    src_8583 += c >> 1;
    dst_8583 += c;
}

/***************************************************************************
 Move fixed length BCD field to string, omitting leading zeros.
***************************************************************************/
void bcd_to_snz(c)
int c;
{
    char ch;
    while ( c > 0 ) 
    {
        ch = ( c--&1 ? *src_8583++ : *src_8583>>4 ) & 0x0f;
        if ( ch ) 
        {	/* first non zero digit */
            if ( ++c & 1 )
                src_8583--;
            break;
        }
    }
    bcd_to_str(c);
}

/***************************************************************************
 Copy fixed length bcd field to null terminated string, preserving
 leading zeros.
***************************************************************************/
void bcd_to_str(c)
int c;
{
    bcd_to_asc(c);
    *dst_8583++ = 0;
}

/***************************************************************************
 Simply move bit field. Will always move whole bytes.
***************************************************************************/
void bit_to_bit(c)
int c;
{
    for ( ; c > 0;  c -= 8 )	/* 8 bits per iteration */
	*dst_8583++ = *src_8583++;
}

/***************************************************************************
 1 byte counted BCD string to null terminated.
***************************************************************************/
void bv2_to_str(c)
int c;
{
    copy_bv_to_str( get_len_v2(), c );
}

/***************************************************************************
 Null terminated string of ASCII to fixed size ASCII with blank padding
 on the right if necessary.
***************************************************************************/
void str_to_asc(c)
int c;
{
    int i = strlen( src_8583 );
    if ( i > c )
        i = c;		/* truncate on right if too long */
    asc_to_asc(i);
    i = c - i;
    if ( i > 0 ) 
    {
        memset( dst_8583, ' ', i );
        dst_8583 += i;
    }
}

/***************************************************************************
 Null terminated ASCII string to 1 byte counted string.
***************************************************************************/
void str_to_av2(c)
int c;
{
    asc_to_asc( put_len_v2(strlen(src_8583)) );
}

/***************************************************************************
 Null terminated ASCII string to 2 byte counted string.
***************************************************************************/
void str_to_av3(c)
int c;
{
    asc_to_asc( put_len_v3(strlen(src_8583)) );
}

/***************************************************************************
 Null terminated to fixed size BCD with zero padding on the left if necessary.
 Move string into fixed length bcd field.
***************************************************************************/
void str_to_bcd(c)
int c;
{
    max_asc_to_bcd( strlen(src_8583), c );
}

/***************************************************************************
 Null terminated BCD string to 1 byte counted string.
***************************************************************************/
void str_to_bv2(c)
int c;
{
    max_asc_to_bv( put_len_v2(strlen(src_8583)), c );
}

/***************************************************************************
 Null terminated to signed BCD. 'C' or 'D' followed by BCD.
***************************************************************************/
void str_to_xbc(c)
int c;
{
    *dst_8583++ = *src_8583++;
    str_to_bcd(c);
}

/***************************************************************************
 Signed BCD to null terminated string.
***************************************************************************/
void xbc_to_str(c)
int c;
{
    *dst_8583++ = *src_8583++;
    bcd_to_str(c);
}

/***************************************************************************
  SUPPOSE dsp CONTAINS THE ASSCII ARRAY "12345F" AND WE EXECUTE THIS FUNCTION
  THEN THE ARRAY  AT hex WILL CONTAIN 12H,34H, 5FH
****************************************************************************/
SVC_DSP_2_HEX(dsp, hex, count)
char *dsp, *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;
        hex[i] += ((dsp[i * 2 + 1] <= 0x39) ? dsp[i * 2 + 1] - 0x30
                                       : dsp[i * 2 + 1] - 0x41 + 10);
    }
}

/***************************************************************************
 SUPPOSE HEX CONTAINS THREE BYTES:12H 34H, 5FH AND WE EXECUTE THIS FUNCTION
 THEN dsp WILL CONTAIN ASCII BYTES "12345F"
**************************************************************************/
SVC_HEX_2_DSP(hex, dsp, count)
char *dsp, *hex;
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;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -