2 * This file contains part of linux kernel implementation of crc32
3 * kernel version 2.6.32
6 #include <libcfs/libcfs.h>
7 #define CRCPOLY_LE 0xedb88320
9 #define LE_TABLE_SIZE (1 << CRC_LE_BITS)
11 static unsigned int crc32table_le[LE_TABLE_SIZE];
13 * crc32init_le() - allocate and initialize LE table data
15 * crc is the crc of the byte i; other entries are filled in based on the
16 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
19 void crc32init_le(void)
26 for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) {
27 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
28 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
29 crc32table_le[i + j] = crc ^ crc32table_le[j];
33 unsigned int crc32_le(unsigned int crc, unsigned char const *p, size_t len)
35 const unsigned int *b = (unsigned int *)p;
36 const unsigned int *tab = crc32table_le;
38 # if __BYTE_ORDER == __LITTLE_ENDIAN
39 # define DO_CRC(x) crc = tab[(crc ^ (x)) & 255] ^ (crc>>8)
41 # define DO_CRC(x) crc = tab[((crc >> 24) ^ (x)) & 255] ^ (crc<<8)
44 crc = cpu_to_le32(crc);
46 if (unlikely(((long)b) & 3 && len)) {
48 unsigned char *p = (unsigned char *)b;
51 } while ((--len) && ((long)b) & 3);
53 if (likely(len >= 4)) {
54 /* load data 32 bits wide, xor data 32 bits wide. */
55 size_t save_len = len & 3;
57 --b; /* use pre increment below(*++b) for speed */
65 b++; /* point to next byte(s) */
68 /* And the last few bytes */
71 unsigned char *p = (unsigned char *)b;
77 return le32_to_cpu(crc);