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
34static inline u32 __maybe_unused
35crc32_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
42static inline u32 __maybe_unused
43crc32_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
50static inline u32 __maybe_unused
51crc32c_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
61u32 crc32_optimizations(void)
62{
63 return crc32_optimizations_arch();
64}
65EXPORT_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
72u32 crc32_le(u32 crc, const void *p, size_t len)
73{
74 return crc32_le_arch(crc, p, len);
75}
76EXPORT_SYMBOL(crc32_le);
77
78u32 crc32_be(u32 crc, const void *p, size_t len)
79{
80 return crc32_be_arch(crc, p, len);
81}
82EXPORT_SYMBOL(crc32_be);
83
84u32 crc32c(u32 crc, const void *p, size_t len)
85{
86 return crc32c_arch(crc, p, len);
87}
88EXPORT_SYMBOL(crc32c);
89
90#ifdef crc32_mod_init_arch
91static int __init crc32_mod_init(void)
92{
93 crc32_mod_init_arch();
94 return 0;
95}
96subsys_initcall(crc32_mod_init);
97
98static void __exit crc32_mod_exit(void)
99{
100}
101module_exit(crc32_mod_exit);
102#endif
103
104MODULE_DESCRIPTION("CRC32 library functions");
105MODULE_LICENSE("GPL");
106