2 * This file contains part of linux kernel implementation of crc32
3 * kernel version 2.6.32
5 #include <libcfs/libcfs.h>
6 #define CRCPOLY_LE 0xedb88320
8 #define LE_TABLE_SIZE (1 << CRC_LE_BITS)
10 static unsigned int crc32table_le[LE_TABLE_SIZE];
12 * crc32init_le() - allocate and initialize LE table data
14 * crc is the crc of the byte i; other entries are filled in based on the
15 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
18 void crc32init_le(void)
25 for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) {
26 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
27 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
28 crc32table_le[i + j] = crc ^ crc32table_le[j];
32 unsigned int crc32_le(unsigned int crc, unsigned char const *p, size_t len)
34 const unsigned int *b = (unsigned int *)p;
35 const unsigned int *tab = crc32table_le;
37 # ifdef __LITTLE_ENDIAN
38 # define DO_CRC(x) crc = tab[(crc ^ (x)) & 255] ^ (crc>>8)
40 # define DO_CRC(x) crc = tab[((crc >> 24) ^ (x)) & 255] ^ (crc<<8)
43 crc = cpu_to_le32(crc);
45 if (unlikely(((long)b) & 3 && len)) {
47 unsigned char *p = (unsigned char *)b;
50 } while ((--len) && ((long)b) & 3);
52 if (likely(len >= 4)) {
53 /* load data 32 bits wide, xor data 32 bits wide. */
54 size_t save_len = len & 3;
56 --b; /* use pre increment below(*++b) for speed */
64 b++; /* point to next byte(s) */
67 /* And the last few bytes */
70 unsigned char *p = (unsigned char *)b;
76 return le32_to_cpu(crc);