| 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/export.h> | 
|---|
| 29 | #include <linux/module.h> | 
|---|
| 30 | #include <linux/types.h> | 
|---|
| 31 |  | 
|---|
| 32 | #include "crc32table.h" | 
|---|
| 33 |  | 
|---|
| 34 | static inline u32 __maybe_unused | 
|---|
| 35 | crc32_le_base(u32 crc, const u8 *p, size_t len) | 
|---|
| 36 | { | 
|---|
| 37 | while (len--) | 
|---|
| 38 | crc = (crc >> 8) ^ crc32table_le[(crc & 255) ^ *p++]; | 
|---|
| 39 | return crc; | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | static inline u32 __maybe_unused | 
|---|
| 43 | crc32_be_base(u32 crc, const u8 *p, size_t len) | 
|---|
| 44 | { | 
|---|
| 45 | while (len--) | 
|---|
| 46 | crc = (crc << 8) ^ crc32table_be[(crc >> 24) ^ *p++]; | 
|---|
| 47 | return crc; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | static inline u32 __maybe_unused | 
|---|
| 51 | crc32c_base(u32 crc, const u8 *p, size_t len) | 
|---|
| 52 | { | 
|---|
| 53 | while (len--) | 
|---|
| 54 | crc = (crc >> 8) ^ crc32ctable_le[(crc & 255) ^ *p++]; | 
|---|
| 55 | return crc; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | #ifdef CONFIG_CRC32_ARCH | 
|---|
| 59 | #include "crc32.h" /* $(SRCARCH)/crc32.h */ | 
|---|
| 60 |  | 
|---|
| 61 | u32 crc32_optimizations(void) | 
|---|
| 62 | { | 
|---|
| 63 | return crc32_optimizations_arch(); | 
|---|
| 64 | } | 
|---|
| 65 | EXPORT_SYMBOL(crc32_optimizations); | 
|---|
| 66 | #else | 
|---|
| 67 | #define crc32_le_arch crc32_le_base | 
|---|
| 68 | #define crc32_be_arch crc32_be_base | 
|---|
| 69 | #define crc32c_arch crc32c_base | 
|---|
| 70 | #endif | 
|---|
| 71 |  | 
|---|
| 72 | u32 crc32_le(u32 crc, const void *p, size_t len) | 
|---|
| 73 | { | 
|---|
| 74 | return crc32_le_arch(crc, p, len); | 
|---|
| 75 | } | 
|---|
| 76 | EXPORT_SYMBOL(crc32_le); | 
|---|
| 77 |  | 
|---|
| 78 | u32 crc32_be(u32 crc, const void *p, size_t len) | 
|---|
| 79 | { | 
|---|
| 80 | return crc32_be_arch(crc, p, len); | 
|---|
| 81 | } | 
|---|
| 82 | EXPORT_SYMBOL(crc32_be); | 
|---|
| 83 |  | 
|---|
| 84 | u32 crc32c(u32 crc, const void *p, size_t len) | 
|---|
| 85 | { | 
|---|
| 86 | return crc32c_arch(crc, p, len); | 
|---|
| 87 | } | 
|---|
| 88 | EXPORT_SYMBOL(crc32c); | 
|---|
| 89 |  | 
|---|
| 90 | #ifdef crc32_mod_init_arch | 
|---|
| 91 | static int __init crc32_mod_init(void) | 
|---|
| 92 | { | 
|---|
| 93 | crc32_mod_init_arch(); | 
|---|
| 94 | return 0; | 
|---|
| 95 | } | 
|---|
| 96 | subsys_initcall(crc32_mod_init); | 
|---|
| 97 |  | 
|---|
| 98 | static void __exit crc32_mod_exit(void) | 
|---|
| 99 | { | 
|---|
| 100 | } | 
|---|
| 101 | module_exit(crc32_mod_exit); | 
|---|
| 102 | #endif | 
|---|
| 103 |  | 
|---|
| 104 | MODULE_DESCRIPTION( "CRC32 library functions"); | 
|---|
| 105 | MODULE_LICENSE( "GPL"); | 
|---|
| 106 |  | 
|---|