1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Aug 8, 2011 Bob Pearson with help from Joakim Tjernlund and George Spelvin 4 * cleaned up code to current version of sparse and added the slicing-by-8 5 * algorithm to the closely similar existing slicing-by-4 algorithm. 6 * 7 * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com> 8 * Nicer crc32 functions/docs submitted by linux@horizon.com. Thanks! 9 * Code was from the public domain, copyright abandoned. Code was 10 * subsequently included in the kernel, thus was re-licensed under the 11 * GNU GPL v2. 12 * 13 * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com> 14 * Same crc32 function was used in 5 other places in the kernel. 15 * I made one version, and deleted the others. 16 * There are various incantations of crc32(). Some use a seed of 0 or ~0. 17 * Some xor at the end with ~0. The generic crc32() function takes 18 * seed as an argument, and doesn't xor at the end. Then individual 19 * users can do whatever they need. 20 * drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0. 21 * fs/jffs2 uses seed 0, doesn't xor with ~0. 22 * fs/partitions/efi.c uses seed ~0, xor's with ~0. 23 */ 24 25 /* see: Documentation/staging/crc32.rst for a description of algorithms */ 26 27 #include <linux/crc32.h> 28 #include <linux/module.h> 29 #include <linux/types.h> 30 31 #include "crc32table.h" 32 33 static inline u32 __maybe_unused 34 crc32_le_base(u32 crc, const u8 *p, size_t len) 35 { 36 while (len--) 37 crc = (crc >> 8) ^ crc32table_le[(crc & 255) ^ *p++]; 38 return crc; 39 } 40 41 static inline u32 __maybe_unused 42 crc32_be_base(u32 crc, const u8 *p, size_t len) 43 { 44 while (len--) 45 crc = (crc << 8) ^ crc32table_be[(crc >> 24) ^ *p++]; 46 return crc; 47 } 48 49 static inline u32 __maybe_unused 50 crc32c_base(u32 crc, const u8 *p, size_t len) 51 { 52 while (len--) 53 crc = (crc >> 8) ^ crc32ctable_le[(crc & 255) ^ *p++]; 54 return crc; 55 } 56 57 #ifdef CONFIG_CRC32_ARCH 58 #include "crc32.h" /* $(SRCARCH)/crc32.h */ 59 60 u32 crc32_optimizations(void) 61 { 62 return crc32_optimizations_arch(); 63 } 64 EXPORT_SYMBOL(crc32_optimizations); 65 #else 66 #define crc32_le_arch crc32_le_base 67 #define crc32_be_arch crc32_be_base 68 #define crc32c_arch crc32c_base 69 #endif 70 71 u32 crc32_le(u32 crc, const void *p, size_t len) 72 { 73 return crc32_le_arch(crc, p, len); 74 } 75 EXPORT_SYMBOL(crc32_le); 76 77 u32 crc32_be(u32 crc, const void *p, size_t len) 78 { 79 return crc32_be_arch(crc, p, len); 80 } 81 EXPORT_SYMBOL(crc32_be); 82 83 u32 crc32c(u32 crc, const void *p, size_t len) 84 { 85 return crc32c_arch(crc, p, len); 86 } 87 EXPORT_SYMBOL(crc32c); 88 89 #ifdef crc32_mod_init_arch 90 static int __init crc32_mod_init(void) 91 { 92 crc32_mod_init_arch(); 93 return 0; 94 } 95 subsys_initcall(crc32_mod_init); 96 97 static void __exit crc32_mod_exit(void) 98 { 99 } 100 module_exit(crc32_mod_exit); 101 #endif 102 103 MODULE_DESCRIPTION("CRC32 library functions"); 104 MODULE_LICENSE("GPL"); 105