📄 crc32.c
字号:
/* * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com> * Nicer crc32 functions/docs submitted by linux@horizon.com. Thanks! * Code was from the public domain, copyright abandoned. Code was * subsequently included in the kernel, thus was re-licensed under the * GNU GPL v2. * * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com> * Same crc32 function was used in 5 other places in the kernel. * I made one version, and deleted the others. * There are various incantations of crc32(). Some use a seed of 0 or ~0. * Some xor at the end with ~0. The generic crc32() function takes * seed as an argument, and doesn't xor at the end. Then individual * users can do whatever they need. * drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0. * fs/jffs2 uses seed 0, doesn't xor with ~0. * fs/partitions/efi.c uses seed ~0, xor's with ~0. * * This source code is licensed under the GNU General Public License, * Version 2. See the file COPYING for more details. */#ifdef UBI_LINUX#include <linux/crc32.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/compiler.h>#endif#include <linux/types.h>#include <asm/byteorder.h>#ifdef UBI_LINUX#include <linux/slab.h>#include <linux/init.h>#include <asm/atomic.h>#endif#include "crc32defs.h"#define CRC_LE_BITS 8# define __force#ifndef __constant_cpu_to_le32#define __constant_cpu_to_le32(x) ((__force __le32)(__u32)(x))#endif#ifndef __constant_le32_to_cpu#define __constant_le32_to_cpu(x) ((__force __u32)(__le32)(x))#endif#if CRC_LE_BITS == 8#define tole(x) __constant_cpu_to_le32(x)#define tobe(x) __constant_cpu_to_be32(x)#else#define tole(x) (x)#define tobe(x) (x)#endif#include "crc32table.h"#ifdef UBI_LINUXMODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");MODULE_DESCRIPTION("Ethernet CRC32 calculations");MODULE_LICENSE("GPL");#endif/** * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for * other uses, or the previous crc32 value if computing incrementally. * @p: pointer to buffer over which CRC is run * @len: length of buffer @p */u32 crc32_le(u32 crc, unsigned char const *p, size_t len);#if CRC_LE_BITS == 1/* * In fact, the table-based code will work in this case, but it can be * simplified by inlining the table in ?: form. */u32 crc32_le(u32 crc, unsigned char const *p, size_t len){ int i; while (len--) { crc ^= *p++; for (i = 0; i < 8; i++) crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); } return crc;}#else /* Table-based approach */u32 crc32_le(u32 crc, unsigned char const *p, size_t len){# if CRC_LE_BITS == 8 const u32 *b =(u32 *)p; const u32 *tab = crc32table_le;# ifdef __LITTLE_ENDIAN# define DO_CRC(x) crc = tab[ (crc ^ (x)) & 255 ] ^ (crc>>8)# else# define DO_CRC(x) crc = tab[ ((crc >> 24) ^ (x)) & 255] ^ (crc<<8)# endif /* printf("Crc32_le crc=%x\n",crc); */ crc = __cpu_to_le32(crc); /* Align it */ if((((long)b)&3 && len)){ do { u8 *p = (u8 *)b; DO_CRC(*p++); b = (void *)p; } while ((--len) && ((long)b)&3 ); } if((len >= 4)){ /* load data 32 bits wide, xor data 32 bits wide. */ size_t save_len = len & 3; len = len >> 2; --b; /* use pre increment below(*++b) for speed */ do { crc ^= *++b; DO_CRC(0); DO_CRC(0); DO_CRC(0); DO_CRC(0); } while (--len); b++; /* point to next byte(s) */ len = save_len; } /* And the last few bytes */ if(len){ do { u8 *p = (u8 *)b; DO_CRC(*p++); b = (void *)p; } while (--len); } return __le32_to_cpu(crc);#undef ENDIAN_SHIFT#undef DO_CRC# elif CRC_LE_BITS == 4 while (len--) { crc ^= *p++; crc = (crc >> 4) ^ crc32table_le[crc & 15]; crc = (crc >> 4) ^ crc32table_le[crc & 15]; } return crc;# elif CRC_LE_BITS == 2 while (len--) { crc ^= *p++; crc = (crc >> 2) ^ crc32table_le[crc & 3]; crc = (crc >> 2) ^ crc32table_le[crc & 3]; crc = (crc >> 2) ^ crc32table_le[crc & 3]; crc = (crc >> 2) ^ crc32table_le[crc & 3]; } return crc;# endif}#endif#ifdef UBI_LINUX/** * crc32_be() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for * other uses, or the previous crc32 value if computing incrementally. * @p: pointer to buffer over which CRC is run * @len: length of buffer @p */u32 __attribute_pure__ crc32_be(u32 crc, unsigned char const *p, size_t len);#if CRC_BE_BITS == 1/* * In fact, the table-based code will work in this case, but it can be * simplified by inlining the table in ?: form. */u32 __attribute_pure__ crc32_be(u32 crc, unsigned char const *p, size_t len){ int i; while (len--) { crc ^= *p++ << 24; for (i = 0; i < 8; i++) crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0); } return crc;}#else /* Table-based approach */u32 __attribute_pure__ crc32_be(u32 crc, unsigned char const *p, size_t len){# if CRC_BE_BITS == 8 const u32 *b =(u32 *)p; const u32 *tab = crc32table_be;# ifdef __LITTLE_ENDIAN# define DO_CRC(x) crc = tab[ (crc ^ (x)) & 255 ] ^ (crc>>8)# else# define DO_CRC(x) crc = tab[ ((crc >> 24) ^ (x)) & 255] ^ (crc<<8)# endif crc = __cpu_to_be32(crc); /* Align it */ if(unlikely(((long)b)&3 && len)){ do { u8 *p = (u8 *)b; DO_CRC(*p++); b = (u32 *)p; } while ((--len) && ((long)b)&3 ); } if(likely(len >= 4)){ /* load data 32 bits wide, xor data 32 bits wide. */ size_t save_len = len & 3; len = len >> 2; --b; /* use pre increment below(*++b) for speed */ do { crc ^= *++b; DO_CRC(0); DO_CRC(0); DO_CRC(0); DO_CRC(0); } while (--len); b++; /* point to next byte(s) */ len = save_len; } /* And the last few bytes */ if(len){ do { u8 *p = (u8 *)b; DO_CRC(*p++); b = (void *)p; } while (--len); } return __be32_to_cpu(crc);#undef ENDIAN_SHIFT#undef DO_CRC# elif CRC_BE_BITS == 4 while (len--) { crc ^= *p++ << 24; crc = (crc << 4) ^ crc32table_be[crc >> 28]; crc = (crc << 4) ^ crc32table_be[crc >> 28]; } return crc;# elif CRC_BE_BITS == 2 while (len--) { crc ^= *p++ << 24; crc = (crc << 2) ^ crc32table_be[crc >> 30]; crc = (crc << 2) ^ crc32table_be[crc >> 30]; crc = (crc << 2) ^ crc32table_be[crc >> 30]; crc = (crc << 2) ^ crc32table_be[crc >> 30]; } return crc;# endif}#endifEXPORT_SYMBOL(crc32_le);EXPORT_SYMBOL(crc32_be);#endif/* * A brief CRC tutorial. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -