Linux内核中crc16_table与crc32_table的计算
来源:互联网 发布:淘宝上尾单衣服 编辑:程序博客网 时间:2024/06/05 01:04
CRC: Cyclic redundancy check 循环冗余校验
具体的计算原理与方法参考:http://en.wikipedia.org/wiki/Cyclic_redundancy_check
内核中使用的crc16计算方法位于代码树/lib/crc16.c文件中
crc32的计算方法位于代码树/lib/crc32.c文件中
均采用了查表法
其中crc32的表由代码树/lib/gen_crc32table.c中的主机小程序计算而来
生成的冗余表保存在文件/lib/crc32table.h中
具体的计算方法可以参考gen_crc32table.c中的代码
/lib/crc16.c中的crc16_table是直接定义的,没有提供计算代码
在学习了wiki上介绍的原理后,参考内核中crc32的计算方法,写了个crc16的代码
生成crc16_table表
/* *crc16gen.c - CRC-16 table gen routine * * Generate the standard CRC-16 table: * Width 16 * Poly 0x8005 (x^16 + x^15 + x^2 + 1) * Init 0 * * <kernel.digger@gmail.com> * * This source code is licensed under the GNU General Public License, * Version 2. See the file COPYING for more details. */#include <stdio.h>#include <string.h>#define CRC_BITS 8#define TABLE_SIZE (1 << CRC_BITS)/* 在小端序机器上,因为低位在前,所以将多项式对应的位串反转 */#define CRC16POLY_LE 0xa001#define CRC16POLY_BE 0x8005#define MASK_LE 0x0001#define MASK_BE 0x8000unsigned short crc16_table[TABLE_SIZE];/* * 在小端序的机器上,低位在前 * 作为位串计算时,这里的位操作向右移 */static voidcrc16init_le(void){unsigned short i, j, crc;for (i = 0; i < TABLE_SIZE; i++){/* i为0-255的字符值,放在crc的低字节处 */crc = i;for (j = 0; j < CRC_BITS; j++){/* 位串首位为1的话(最低bit为1) 则右移1位的同时异或多项式 否则异或0,即直接右移1位 */crc = (crc >> 1) ^ ((crc & MASK_LE) ? CRC16POLY_LE : 0);}crc16_table[i] = crc;}}static voidcrc16init_be(void){unsigned short i, j, crc;for (i = 0; i < TABLE_SIZE; i++){crc = i << 8;for (j = 0; j < CRC_BITS; j++){crc = (crc << 1) ^ ((crc & MASK_BE) ? CRC16POLY_BE : 0);}crc16_table[i] = crc;}}static voidcrc16_print(void){int i;for (i = 0; i < TABLE_SIZE; i++){printf("0x%04x ", crc16_table[i]);if ((i % 8) == 7) {printf("\n");}}}voidmain(void){crc16init_le();printf("--------------\nle table\n\n");crc16_print();crc16init_be();printf("--------------\nbe table\n\n");crc16_print();}
- Linux内核中crc16_table与crc32_table的计算
- LINUX内核中计算页面号
- Linux中与内核通信的Netlink机制
- linux中与Oracle有关的内核参数详解
- linux中与Oracle有关的内核参数详解
- Linux 中与 Oracle Database 相关的内核参数详解
- linux中与Oracle有关的内核参数详解
- Linux 内核中宏 offsetof 与 container_of 的含义
- linux中与Oracle有关的内核参数详解
- ubuntu中linux内核的编译、更换与使用
- linux中与Oracle有关的内核参数详解
- linux中与Oracle有关的内核参数详解
- linux内核中IP数据包的输入与输出
- Linux中与内核通信的Netlink机制(实例)
- linux中与Oracle有关的内核参数详解
- linux中与Oracle有关的内核参数详解
- Linux 内核中 likely 与 unlikely 的宏定义解析
- Linux中与内核通信的Netlink机制(实例)
- Lesson1 深入.Net框架
- 初来乍到
- PHP 生成16位唯一随机码
- 深入理解PHP内核
- Altium Designer 6(Protel最新版本)特性介绍
- Linux内核中crc16_table与crc32_table的计算
- Struts 2 文件上传
- 【C++ Primer】一、二章学习笔记
- 推荐 Altium Designer 6.8中3D功能的使用
- Altium Designer 6.8中3D
- linux学习技巧 态度决定成就
- 独立思考Justic:Hired Guns
- 学习-SQL查询连续号码段的巧妙解法
- log4j详细配置