linux shell spp2tc CRC计算

https://www.lammertbies.nl/comm/info/crc-calculation

https://github.com/lammertb/libcrc

 

#echo "$#"
#echo "$@"
#echo "$0"
#echo "$1"
#echo "$2"
curpath=$(cd $(dirname $0); pwd)

rm $curpath/out.txt
touch $curpath/out.txt

files=$(ls $curpath/spp)
for file in $files
do
    echo "~~~~~~~~~~~~~~~~~~~~~"
    $curpath/bin/a.out $file $curpath/spp/ $curpath/tc/ $curpath/
done
View Code

 

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

static bool             crc_tabccitt_init       = false;
static unsigned short         crc_tabccitt[256];

/*
 * static void init_crcccitt_tab( void );
 *
 * For optimal performance, the routine to calculate the CRC-CCITT uses a
 * lookup table with pre-compiled values that can be directly applied in the
 * XOR action. This table is created at the first call of the function by the
 * init_crcccitt_tab() routine.
 */

static void init_crcccitt_tab( void ) {

    unsigned short i;
    unsigned short j;
    unsigned short crc;
    unsigned short c;

    for (i=0; i<256; i++) {

        crc = 0;
        c   = i << 8;

        for (j=0; j<8; j++) {

            if ( (crc ^ c) & 0x8000 ) crc = ( crc << 1 ) ^ 0x1021;
            else                      crc =   crc << 1;

            c = c << 1;
        }

        crc_tabccitt[i] = crc;
    }

    crc_tabccitt_init = true;

}  /* init_crcccitt_tab */

/*
 * static unsigned short crc_ccitt_generic( const unsigned char *input_str, size_t num_bytes, unsigned short start_value );
 *
 * The function crc_ccitt_generic() is a generic implementation of the CCITT
 * algorithm for a one-pass calculation of the CRC for a byte string. The
 * function accepts an initial start value for the crc.
 */

static unsigned short crc_ccitt_generic( const unsigned char *input_str, size_t num_bytes, unsigned short start_value ) {

    unsigned short crc;
    const unsigned char *ptr;
    size_t a;

    if ( ! crc_tabccitt_init ) init_crcccitt_tab();

    crc = start_value;
    ptr = input_str;

    if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {

        crc = (crc << 8) ^ crc_tabccitt[ ((crc >> 8) ^ (unsigned short) *ptr++) & 0x00FF ];
    }

    return crc;

}  /* crc_ccitt_generic */

#define DEBUG 0
#define SPP_OFFSET 5

int main(int argc,char *argv[])
{
    unsigned char buf[1200]={1,2};
    unsigned int a;
    unsigned int fsize, spplen, tclen;
    unsigned short crc16;
    FILE *fp, *fptc, *fptxt;
    char inpath[200]={0};
    char outpath[200]={0};
    char txtpath[200]={0};

//    printf("argc= %d\n",argc);

//    printf("file=%s\n",argv[1]);
//    printf("inpath=%s\n",argv[2]);
//    printf("outpath=%s\n",argv[3]);
    printf("curpath=%s\n",argv[4]);

    strcat(inpath, argv[2]);
    strcat(inpath, argv[1]);
    printf("inpath=%s\n",inpath);

    fp = fopen(inpath, "rb");
    if(fp==NULL)
        printf("fp open error\n");

    fseek(fp, 0, SEEK_END);
    fsize = ftell(fp);
 //   printf("%d\n", fsize);

    fseek(fp, 0, SEEK_SET);
    fread(&buf[SPP_OFFSET], 1, fsize, fp);

    fclose(fp);

#if DEBUG==1
    for(a=0; a<fsize; a++)
    {
        printf("%02x ", buf[a]);
    }
    printf("\n");
#endif

    spplen = (buf[SPP_OFFSET+4]<<8) | buf[SPP_OFFSET+5];
//    printf("%d\n", spplen);
    if((spplen+1+6) == fsize)
        ;
    else
        printf("spp len is not ok\n");

    buf[0] = 0x21;
    buf[1] = 0x60;
    tclen = fsize+7-1;
    buf[2] = (0x14 << 2) | (tclen>>8);
    buf[3] = tclen & 0xFF;
    buf[4] = 0;

    crc16=crc_ccitt_generic(buf, tclen+1-2, 0xffff);
 //   printf("crc16=%x\n", crc16);

    buf[tclen-1] = crc16>>8;
    buf[tclen] = crc16 & 0xFF;

#if DEBUG==1
    for(a=0; a<tclen+1; a++)
    {
        printf("%02x ", buf[a]);
    }
    printf("\n");
#endif

    char fptcname[500];
    fptcname[0]='t';
    fptcname[1]='c';
    memcpy(&fptcname[2], &argv[1][3], strlen(argv[1])-3);
    strcat(outpath, argv[3]);
    strcat(outpath, fptcname);
    printf("outpath=%s\n", outpath);

    fptc = fopen(outpath, "wb");
    if(fptc==NULL)
        printf("fptc open error\n");

    fwrite(buf, 1, tclen+1, fptc);
    fclose(fptc);

    // 创建数据库文件
    strcat(txtpath, argv[4]);
    strcat(txtpath, "out.txt");
    fptxt = fopen(txtpath, "a+");
    printf("txtpath=%s\n", txtpath);
    if(fptxt==NULL)
        printf("fptxt open error\n");

    fprintf(fptxt, "%d %s\n", tclen+1, fptcname);
    for(a=0; a<tclen+1; a++)
    {
        fprintf(fptxt, "%02x ", buf[a]);
    }
    fprintf(fptxt, "\n\n");
    fclose(fptxt);


    return 0;
}
View Code

 

posted on 2022-12-22 07:38  yanhc  阅读(83)  评论(0编辑  收藏  举报

导航