CRC算法
来源:互联网 发布:firefox linux 升级 编辑:程序博客网 时间:2024/05/21 06:18
#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);
}
#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
- CRC算法
- CRC算法
- CRC算法
- CRC算法
- crc 算法
- CRC算法
- CRC算法
- CRC算法
- CRC算法
- CRC算法
- CRC算法
- CRC算法
- CRC算法介绍
- CRC算法另类应用
- CRC算法与实现
- CRC算法的实现...
- CRC算法与实现
- CRC算法与实现
- CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别
- 1)指向常量的指针变量
- JNDI---Naming Package
- 端口技术 GetOverlappedResult
- 通过句子学单词
- CRC算法
- Some Analogy(Tales) In Programming Books
- asp.net页面间数据传递(总结篇)
- Notes与SAP数据的互换
- 在ASP.NET中使用存储过程
- make the vs 2005 fly
- Struts logic 标签
- 删除确认对话框
- 通过Event事件正确结束线程