Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Aug 8, 2011 Bob Pearson with help from Joakim Tjernlund and George Spelvin
0003  * cleaned up code to current version of sparse and added the slicing-by-8
0004  * algorithm to the closely similar existing slicing-by-4 algorithm.
0005  *
0006  * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
0007  * Nicer crc32 functions/docs submitted by linux@horizon.com.  Thanks!
0008  * Code was from the public domain, copyright abandoned.  Code was
0009  * subsequently included in the kernel, thus was re-licensed under the
0010  * GNU GPL v2.
0011  *
0012  * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
0013  * Same crc32 function was used in 5 other places in the kernel.
0014  * I made one version, and deleted the others.
0015  * There are various incantations of crc32().  Some use a seed of 0 or ~0.
0016  * Some xor at the end with ~0.  The generic crc32() function takes
0017  * seed as an argument, and doesn't xor at the end.  Then individual
0018  * users can do whatever they need.
0019  *   drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
0020  *   fs/jffs2 uses seed 0, doesn't xor with ~0.
0021  *   fs/partitions/efi.c uses seed ~0, xor's with ~0.
0022  *
0023  * This source code is licensed under the GNU General Public License,
0024  * Version 2.  See the file COPYING for more details.
0025  */
0026 
0027 /* see: Documentation/staging/crc32.rst for a description of algorithms */
0028 
0029 #include <linux/crc32.h>
0030 #include <linux/crc32poly.h>
0031 #include <linux/module.h>
0032 #include <linux/types.h>
0033 #include <linux/sched.h>
0034 #include "crc32defs.h"
0035 
0036 #if CRC_LE_BITS > 8
0037 # define tole(x) ((__force u32) cpu_to_le32(x))
0038 #else
0039 # define tole(x) (x)
0040 #endif
0041 
0042 #if CRC_BE_BITS > 8
0043 # define tobe(x) ((__force u32) cpu_to_be32(x))
0044 #else
0045 # define tobe(x) (x)
0046 #endif
0047 
0048 #include "crc32table.h"
0049 
0050 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
0051 MODULE_DESCRIPTION("Various CRC32 calculations");
0052 MODULE_LICENSE("GPL");
0053 
0054 #if CRC_LE_BITS > 8 || CRC_BE_BITS > 8
0055 
0056 /* implements slicing-by-4 or slicing-by-8 algorithm */
0057 static inline u32 __pure
0058 crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
0059 {
0060 # ifdef __LITTLE_ENDIAN
0061 #  define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)
0062 #  define DO_CRC4 (t3[(q) & 255] ^ t2[(q >> 8) & 255] ^ \
0063            t1[(q >> 16) & 255] ^ t0[(q >> 24) & 255])
0064 #  define DO_CRC8 (t7[(q) & 255] ^ t6[(q >> 8) & 255] ^ \
0065            t5[(q >> 16) & 255] ^ t4[(q >> 24) & 255])
0066 # else
0067 #  define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
0068 #  define DO_CRC4 (t0[(q) & 255] ^ t1[(q >> 8) & 255] ^ \
0069            t2[(q >> 16) & 255] ^ t3[(q >> 24) & 255])
0070 #  define DO_CRC8 (t4[(q) & 255] ^ t5[(q >> 8) & 255] ^ \
0071            t6[(q >> 16) & 255] ^ t7[(q >> 24) & 255])
0072 # endif
0073     const u32 *b;
0074     size_t    rem_len;
0075 # ifdef CONFIG_X86
0076     size_t i;
0077 # endif
0078     const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3];
0079 # if CRC_LE_BITS != 32
0080     const u32 *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7];
0081 # endif
0082     u32 q;
0083 
0084     /* Align it */
0085     if (unlikely((long)buf & 3 && len)) {
0086         do {
0087             DO_CRC(*buf++);
0088         } while ((--len) && ((long)buf)&3);
0089     }
0090 
0091 # if CRC_LE_BITS == 32
0092     rem_len = len & 3;
0093     len = len >> 2;
0094 # else
0095     rem_len = len & 7;
0096     len = len >> 3;
0097 # endif
0098 
0099     b = (const u32 *)buf;
0100 # ifdef CONFIG_X86
0101     --b;
0102     for (i = 0; i < len; i++) {
0103 # else
0104     for (--b; len; --len) {
0105 # endif
0106         q = crc ^ *++b; /* use pre increment for speed */
0107 # if CRC_LE_BITS == 32
0108         crc = DO_CRC4;
0109 # else
0110         crc = DO_CRC8;
0111         q = *++b;
0112         crc ^= DO_CRC4;
0113 # endif
0114     }
0115     len = rem_len;
0116     /* And the last few bytes */
0117     if (len) {
0118         u8 *p = (u8 *)(b + 1) - 1;
0119 # ifdef CONFIG_X86
0120         for (i = 0; i < len; i++)
0121             DO_CRC(*++p); /* use pre increment for speed */
0122 # else
0123         do {
0124             DO_CRC(*++p); /* use pre increment for speed */
0125         } while (--len);
0126 # endif
0127     }
0128     return crc;
0129 #undef DO_CRC
0130 #undef DO_CRC4
0131 #undef DO_CRC8
0132 }
0133 #endif
0134 
0135 
0136 /**
0137  * crc32_le_generic() - Calculate bitwise little-endian Ethernet AUTODIN II
0138  *          CRC32/CRC32C
0139  * @crc: seed value for computation.  ~0 for Ethernet, sometimes 0 for other
0140  *   uses, or the previous crc32/crc32c value if computing incrementally.
0141  * @p: pointer to buffer over which CRC32/CRC32C is run
0142  * @len: length of buffer @p
0143  * @tab: little-endian Ethernet table
0144  * @polynomial: CRC32/CRC32c LE polynomial
0145  */
0146 static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p,
0147                       size_t len, const u32 (*tab)[256],
0148                       u32 polynomial)
0149 {
0150 #if CRC_LE_BITS == 1
0151     int i;
0152     while (len--) {
0153         crc ^= *p++;
0154         for (i = 0; i < 8; i++)
0155             crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
0156     }
0157 # elif CRC_LE_BITS == 2
0158     while (len--) {
0159         crc ^= *p++;
0160         crc = (crc >> 2) ^ tab[0][crc & 3];
0161         crc = (crc >> 2) ^ tab[0][crc & 3];
0162         crc = (crc >> 2) ^ tab[0][crc & 3];
0163         crc = (crc >> 2) ^ tab[0][crc & 3];
0164     }
0165 # elif CRC_LE_BITS == 4
0166     while (len--) {
0167         crc ^= *p++;
0168         crc = (crc >> 4) ^ tab[0][crc & 15];
0169         crc = (crc >> 4) ^ tab[0][crc & 15];
0170     }
0171 # elif CRC_LE_BITS == 8
0172     /* aka Sarwate algorithm */
0173     while (len--) {
0174         crc ^= *p++;
0175         crc = (crc >> 8) ^ tab[0][crc & 255];
0176     }
0177 # else
0178     crc = (__force u32) __cpu_to_le32(crc);
0179     crc = crc32_body(crc, p, len, tab);
0180     crc = __le32_to_cpu((__force __le32)crc);
0181 #endif
0182     return crc;
0183 }
0184 
0185 #if CRC_LE_BITS == 1
0186 u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
0187 {
0188     return crc32_le_generic(crc, p, len, NULL, CRC32_POLY_LE);
0189 }
0190 u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
0191 {
0192     return crc32_le_generic(crc, p, len, NULL, CRC32C_POLY_LE);
0193 }
0194 #else
0195 u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
0196 {
0197     return crc32_le_generic(crc, p, len, crc32table_le, CRC32_POLY_LE);
0198 }
0199 u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
0200 {
0201     return crc32_le_generic(crc, p, len, crc32ctable_le, CRC32C_POLY_LE);
0202 }
0203 #endif
0204 EXPORT_SYMBOL(crc32_le);
0205 EXPORT_SYMBOL(__crc32c_le);
0206 
0207 u32 __pure crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le);
0208 u32 __pure __crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
0209 u32 __pure crc32_be_base(u32, unsigned char const *, size_t) __alias(crc32_be);
0210 
0211 /*
0212  * This multiplies the polynomials x and y modulo the given modulus.
0213  * This follows the "little-endian" CRC convention that the lsbit
0214  * represents the highest power of x, and the msbit represents x^0.
0215  */
0216 static u32 __attribute_const__ gf2_multiply(u32 x, u32 y, u32 modulus)
0217 {
0218     u32 product = x & 1 ? y : 0;
0219     int i;
0220 
0221     for (i = 0; i < 31; i++) {
0222         product = (product >> 1) ^ (product & 1 ? modulus : 0);
0223         x >>= 1;
0224         product ^= x & 1 ? y : 0;
0225     }
0226 
0227     return product;
0228 }
0229 
0230 /**
0231  * crc32_generic_shift - Append @len 0 bytes to crc, in logarithmic time
0232  * @crc: The original little-endian CRC (i.e. lsbit is x^31 coefficient)
0233  * @len: The number of bytes. @crc is multiplied by x^(8*@len)
0234  * @polynomial: The modulus used to reduce the result to 32 bits.
0235  *
0236  * It's possible to parallelize CRC computations by computing a CRC
0237  * over separate ranges of a buffer, then summing them.
0238  * This shifts the given CRC by 8*len bits (i.e. produces the same effect
0239  * as appending len bytes of zero to the data), in time proportional
0240  * to log(len).
0241  */
0242 static u32 __attribute_const__ crc32_generic_shift(u32 crc, size_t len,
0243                            u32 polynomial)
0244 {
0245     u32 power = polynomial; /* CRC of x^32 */
0246     int i;
0247 
0248     /* Shift up to 32 bits in the simple linear way */
0249     for (i = 0; i < 8 * (int)(len & 3); i++)
0250         crc = (crc >> 1) ^ (crc & 1 ? polynomial : 0);
0251 
0252     len >>= 2;
0253     if (!len)
0254         return crc;
0255 
0256     for (;;) {
0257         /* "power" is x^(2^i), modulo the polynomial */
0258         if (len & 1)
0259             crc = gf2_multiply(crc, power, polynomial);
0260 
0261         len >>= 1;
0262         if (!len)
0263             break;
0264 
0265         /* Square power, advancing to x^(2^(i+1)) */
0266         power = gf2_multiply(power, power, polynomial);
0267     }
0268 
0269     return crc;
0270 }
0271 
0272 u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len)
0273 {
0274     return crc32_generic_shift(crc, len, CRC32_POLY_LE);
0275 }
0276 
0277 u32 __attribute_const__ __crc32c_le_shift(u32 crc, size_t len)
0278 {
0279     return crc32_generic_shift(crc, len, CRC32C_POLY_LE);
0280 }
0281 EXPORT_SYMBOL(crc32_le_shift);
0282 EXPORT_SYMBOL(__crc32c_le_shift);
0283 
0284 /**
0285  * crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
0286  * @crc: seed value for computation.  ~0 for Ethernet, sometimes 0 for
0287  *  other uses, or the previous crc32 value if computing incrementally.
0288  * @p: pointer to buffer over which CRC32 is run
0289  * @len: length of buffer @p
0290  * @tab: big-endian Ethernet table
0291  * @polynomial: CRC32 BE polynomial
0292  */
0293 static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p,
0294                       size_t len, const u32 (*tab)[256],
0295                       u32 polynomial)
0296 {
0297 #if CRC_BE_BITS == 1
0298     int i;
0299     while (len--) {
0300         crc ^= *p++ << 24;
0301         for (i = 0; i < 8; i++)
0302             crc =
0303                 (crc << 1) ^ ((crc & 0x80000000) ? polynomial :
0304                       0);
0305     }
0306 # elif CRC_BE_BITS == 2
0307     while (len--) {
0308         crc ^= *p++ << 24;
0309         crc = (crc << 2) ^ tab[0][crc >> 30];
0310         crc = (crc << 2) ^ tab[0][crc >> 30];
0311         crc = (crc << 2) ^ tab[0][crc >> 30];
0312         crc = (crc << 2) ^ tab[0][crc >> 30];
0313     }
0314 # elif CRC_BE_BITS == 4
0315     while (len--) {
0316         crc ^= *p++ << 24;
0317         crc = (crc << 4) ^ tab[0][crc >> 28];
0318         crc = (crc << 4) ^ tab[0][crc >> 28];
0319     }
0320 # elif CRC_BE_BITS == 8
0321     while (len--) {
0322         crc ^= *p++ << 24;
0323         crc = (crc << 8) ^ tab[0][crc >> 24];
0324     }
0325 # else
0326     crc = (__force u32) __cpu_to_be32(crc);
0327     crc = crc32_body(crc, p, len, tab);
0328     crc = __be32_to_cpu((__force __be32)crc);
0329 # endif
0330     return crc;
0331 }
0332 
0333 #if CRC_BE_BITS == 1
0334 u32 __pure __weak crc32_be(u32 crc, unsigned char const *p, size_t len)
0335 {
0336     return crc32_be_generic(crc, p, len, NULL, CRC32_POLY_BE);
0337 }
0338 #else
0339 u32 __pure __weak crc32_be(u32 crc, unsigned char const *p, size_t len)
0340 {
0341     return crc32_be_generic(crc, p, len, crc32table_be, CRC32_POLY_BE);
0342 }
0343 #endif
0344 EXPORT_SYMBOL(crc32_be);