CRC算法

 

#include < stdio.h >    
#include
< windows.h >    
#define    IN_FILE   "./crc.in"   
#define    MAX_IN_FILE_SIZE   1024   
#define    MIN_IN_FILE_SIZE   1   
// 8位&&16位
#define       AL2_FCS_COEF    ((1<<7)+(1<<6)+(1<<5))        /*X(8)+X(2)+X(1)+1*/   
#define       AL3_FCS_COEF    ((1<<15)+(1<<10)+(1<<3))    /*X(16)+X(12)+X(5)+1*/   
    
unsigned 
char  GetOneCrc(unsigned  char   * data,  int  length)   
{   
    unsigned 
char cFcs = 0;
    
int    i,j;   
    
for(i = 0; i < length; i++)   
    
{   
        cFcs 
^= data[i];   
        
for(j = 0; j < 8; j++)   
        
{   
            
if(cFcs & 1)
                cFcs 
= (unsigned char)((cFcs >> 1^ AL2_FCS_COEF);   
            
else   
                cFcs 
>>= 1;   
        }
   
    }
   
    
return   cFcs;   
}
   
unsigned 
short  GetTwoCrc(unsigned  char   * data,  int  length)   
{   
    
int i,j;   
    unsigned 
short iFcs=0;   
    
for(i = 0; i < length; i++)   
    
{   
        iFcs 
^= (unsigned short)data[i];   
        
for(j = 0; j < 8; j++)   
        
{   
            
if(iFcs & 1)   
                iFcs 
= (unsigned short)((iFcs >> 1^ AL3_FCS_COEF);   
            
else   
                iFcs 
>>= 1;   
        }
   
    }
   
    
return   iFcs;   
}
   
 
WORD CRC(unsigned 
char   * info, DWORD len)   
{   
    WORD        acc;   
    unsigned    
char i;   
    acc
=0;   
    
//printf("The len is : %x ", len);
    ////char *a="abc";
    //printf("The *info is : %s ", info);
    while (len--{
        
//printf("now len is : %x ", len);
        
//printf("(unsigned    int)(*info) is : %x ",(unsigned int)(*info));
        
//printf("The acc is : %x ", acc);
        acc=acc^(((unsigned    int)(*info))<<8);
        
//printf("(unsigned    int)(*info) is : %x ",(((unsigned int)(*info))<<8));
        
//printf("The acc is : %x ", acc);
        
//printf("The *info is : %s ", info);
        info++;
        
//printf("The *info is : %s ", info);
        for (i=8;i>0;i--)
        
{
            printf(
"The i is : %x    acc is : %x        acc&0x8000 is : %x ", i,acc,acc&0x8000);
            
if (acc&0x8000)    acc=(acc<<1)^0x1021;   
            
else    acc<<=1;
        }

    }
   
    
return   acc;   
}
   
void    main()   
{   
    HANDLE    hFile   
=   INVALID_HANDLE_VALUE;   
    
char    ch_in[MAX_IN_FILE_SIZE];   
    DWORD    dwRW;   
    WORD    result;   
    hFile   
=   CreateFile(IN_FILE, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);   
    
if(hFile   ==   INVALID_HANDLE_VALUE){   
        printf(
"LastError   :   %d ",   GetLastError());   
        exit(
1);   
    }
   
    
if(GetFileSize(hFile, NULL)   >   MAX_IN_FILE_SIZE   ||   GetFileSize(hFile, NULL)   <   MIN_IN_FILE_SIZE){   
        printf(
"FileSize   >   %d   or   FileSize   <   %d", MAX_IN_FILE_SIZE, MIN_IN_FILE_SIZE);   
        CloseHandle(hFile);   
        exit(
1);   
    }
   
    ReadFile(hFile, ch_in, MAX_IN_FILE_SIZE, 
&dwRW, NULL);   
    CloseHandle(hFile);   
    result   
=   CRC(ch_in, dwRW);   
    printf(
"The   CRC   code   is   :   %x ",   result);   
}

 c基础知识:http://www.cppreference.com/
crc原理:http://hyatus.newffr.com/TAZ/Reverse_Engineering/CRC.html
http://www.cs.nchu.edu.tw/~s9556032/CRC32.HTM

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值