crc

#include <stdio.h>

int reverse_bits(int input, int bits) {
    int i, result = 0;           
   
    for (i = 0; i < bits; i++) {
        result = (result << 1) | (input & 1);
        input >>= 1;        
    }
    return result;
}

unsigned short crc5(unsigned char crc, unsigned char poly, unsigned char *ptr, int len)
{
	unsigned char i;
	
	while (len--) {
		for (i = 0x80; i != 0; i >>= 1) {
			if ((crc & 0x10) != 0) {
				crc <<= 1;
				crc ^= poly;
			} else {
				crc <<= 1;
			}
			if ((*ptr & i) != 0) {
				crc ^= poly;
			}
		}
		ptr++;
	}
	
	return crc;
}

unsigned short crc7(unsigned char crc, unsigned char poly, unsigned char *ptr, int len)
{
	unsigned char i;
	
	while (len--) {
		for (i = 0x80; i != 0; i >>= 1) {
			if ((crc & 0x40) != 0) {
				crc <<= 1;
				crc ^= poly;
			} else {
				crc <<= 1;
			}
			if ((*ptr & i) != 0) {
				crc ^= poly;
			}
		}
		ptr++;
	}
	
	return crc;
}

unsigned short crc8(unsigned char crc, unsigned char poly, unsigned char *ptr, int len)
{
	unsigned char i;
	
	while (len--) {
		for (i = 0x80; i != 0; i >>= 1) {
			if ((crc & 0x80) != 0) {
				crc <<= 1;
				crc ^= poly;
			} else {
				crc <<= 1;
			}
			if ((*ptr & i) != 0) {
				crc ^= poly;
			}
		}
		ptr++;
	}
	
	return crc ^ 0x55;
}

unsigned short crc16(unsigned short crc, unsigned short poly, unsigned char *ptr, int len)
{
	unsigned char i;
	
	while (len--) {
		for (i = 0x80; i != 0; i >>= 1) {
			if ((crc & 0x8000) != 0) {
				crc <<= 1;
				crc ^= poly;
			} else {
				crc <<= 1;
			}
			if ((*ptr & i) != 0) {
				crc ^= poly;
			}
		}
		ptr++;
	}
	
	return crc;
}

unsigned int crc32(unsigned int crc, unsigned int poly, unsigned char *ptr, int len)
{
	unsigned char i;
	
	while (len--) {
		for (i = 0x80; i != 0; i >>= 1) {
			if ((crc & 0x80000000) != 0) {
				crc <<= 1;
				crc ^= poly;
			} else {
				crc <<= 1;
			}
			if ((*ptr & i) != 0) {
				crc ^= poly;
			}
		}
		ptr++;
	}
	
	return crc;
}

int main(int argc, char** argv)
{
	unsigned char buff[] = {0x00, 0x00, 0x00, 0x00, 0x06, 0x0d, 0xd2, 0xe3};
	unsigned char mac_list[][6] = {
		{0x01, 0x00, 0x5e, 0x00, 0x00, 9},
		{0x01, 0x00, 0x5e, 0x00, 0x00, 13},
		{0x01, 0x00, 0x5e, 0x00, 0x00, 48},
		{0x01, 0x00, 0x5e, 0x00, 0x00, 52},
		{0x01, 0x00, 0x5e, 0x00, 0x00, 66}
	};
	
	printf("rev: 0x%02X -> 0x%02X\n", 0x11112222, reverse_bits(0x11112222, 32));
	
	printf("crc5: 0x%02X\n", crc5(0x09, 0x09, buff, sizeof(buff) / sizeof(buff[0])));
	printf("crc7: 0x%02X\n", crc7(0x00, 0x09, buff, sizeof(buff) / sizeof(buff[0])));
	printf("crc8: 0x%02X\n", crc8(0x00, 0x07, buff, sizeof(buff) / sizeof(buff[0])));
	printf("crc16: 0x%04X\n", crc16(0x0000, 0x1021, buff, sizeof(buff) / sizeof(buff[0])));
	printf("crc32: 0x%08X\n", crc32(0xFFFFFFFF, 0x04c11db7, buff, sizeof(buff) / sizeof(buff[0])));
	/*
	printf("crc32: 0x%08X\n", crc32(0, 0x04c11db7, mac_list[0], 6));
	printf("crc32: 0x%08X\n", crc32(0, 0x04c11db7, mac_list[1], 6));
	printf("crc32: 0x%08X\n", crc32(0, 0xC704DD7B, mac_list[2], 6));
	printf("crc32: 0x%08X\n", crc32(0, 0xC704DD7B, mac_list[3], 6));
	printf("crc32: 0x%08X\n", crc32(0, 0xC704DD7B, mac_list[4], 6));*/
	
	return 0;
}

/* 
	test pass:
	crc7		--- crc-7/MMC
	crc16		--- crc-16/IBM
	crc-ccitt	---	crc16-ccitt
*/
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值