Skip to content

Commit 8c7c675

Browse files
author
Eric Biggers
committed
lib/crc: crc32: Document crc32_le(), crc32_be(), and crc32c()
Document these widely used functions. Update kernel-api.rst to point to the correct place, instead of to crc32-main.c which no longer contains kerneldoc comments. Simplify the documentation in crc32poly.h to just point to the corresponding functions, now that they are properly documented. Change the value of CRC32C_POLY_LE to lower case, for consistency. Acked-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20250619183414.100082-2-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 1a822ea commit 8c7c675

3 files changed

Lines changed: 73 additions & 13 deletions

File tree

Documentation/core-api/kernel-api.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ CRC Functions
148148
.. kernel-doc:: lib/crc/crc16.c
149149
:export:
150150

151-
.. kernel-doc:: lib/crc/crc32-main.c
152-
153151
.. kernel-doc:: lib/crc/crc-ccitt.c
154152
:export:
155153

156154
.. kernel-doc:: lib/crc/crc-itu-t.c
157155
:export:
158156

157+
.. kernel-doc:: include/linux/crc32.h
158+
159159
Base 2 log and power Functions
160160
------------------------------
161161

include/linux/crc32.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,74 @@
55
#include <linux/types.h>
66
#include <linux/bitrev.h>
77

8+
/**
9+
* crc32_le() - Compute least-significant-bit-first IEEE CRC-32
10+
* @crc: Initial CRC value. ~0 (recommended) or 0 for a new CRC computation, or
11+
* the previous CRC value if computing incrementally.
12+
* @p: Pointer to the data buffer
13+
* @len: Length of data in bytes
14+
*
15+
* This implements the CRC variant that is often known as the IEEE CRC-32, or
16+
* simply CRC-32, and is widely used in Ethernet and other applications:
17+
*
18+
* - Polynomial: x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 +
19+
* x^7 + x^5 + x^4 + x^2 + x^1 + x^0
20+
* - Bit order: Least-significant-bit-first
21+
* - Polynomial in integer form: 0xedb88320
22+
*
23+
* This does *not* invert the CRC at the beginning or end. The caller is
24+
* expected to do that if it needs to. Inverting at both ends is recommended.
25+
*
26+
* For new applications, prefer to use CRC-32C instead. See crc32c().
27+
*
28+
* Context: Any context
29+
* Return: The new CRC value
30+
*/
831
u32 crc32_le(u32 crc, const void *p, size_t len);
32+
33+
/**
34+
* crc32_be() - Compute most-significant-bit-first IEEE CRC-32
35+
* @crc: Initial CRC value. ~0 (recommended) or 0 for a new CRC computation, or
36+
* the previous CRC value if computing incrementally.
37+
* @p: Pointer to the data buffer
38+
* @len: Length of data in bytes
39+
*
40+
* crc32_be() is the same as crc32_le() except that crc32_be() computes the
41+
* *most-significant-bit-first* variant of the CRC. I.e., within each byte, the
42+
* most significant bit is processed first (treated as highest order polynomial
43+
* coefficient). The same bit order is also used for the CRC value itself:
44+
*
45+
* - Polynomial: x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 +
46+
* x^7 + x^5 + x^4 + x^2 + x^1 + x^0
47+
* - Bit order: Most-significant-bit-first
48+
* - Polynomial in integer form: 0x04c11db7
49+
*
50+
* Context: Any context
51+
* Return: The new CRC value
52+
*/
953
u32 crc32_be(u32 crc, const void *p, size_t len);
54+
55+
/**
56+
* crc32c() - Compute CRC-32C
57+
* @crc: Initial CRC value. ~0 (recommended) or 0 for a new CRC computation, or
58+
* the previous CRC value if computing incrementally.
59+
* @p: Pointer to the data buffer
60+
* @len: Length of data in bytes
61+
*
62+
* This implements CRC-32C, i.e. the Castagnoli CRC. This is the recommended
63+
* CRC variant to use in new applications that want a 32-bit CRC.
64+
*
65+
* - Polynomial: x^32 + x^28 + x^27 + x^26 + x^25 + x^23 + x^22 + x^20 + x^19 +
66+
* x^18 + x^14 + x^13 + x^11 + x^10 + x^9 + x^8 + x^6 + x^0
67+
* - Bit order: Least-significant-bit-first
68+
* - Polynomial in integer form: 0x82f63b78
69+
*
70+
* This does *not* invert the CRC at the beginning or end. The caller is
71+
* expected to do that if it needs to. Inverting at both ends is recommended.
72+
*
73+
* Context: Any context
74+
* Return: The new CRC value
75+
*/
1076
u32 crc32c(u32 crc, const void *p, size_t len);
1177

1278
/*

include/linux/crc32poly.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22
#ifndef _LINUX_CRC32_POLY_H
33
#define _LINUX_CRC32_POLY_H
44

5-
/*
6-
* There are multiple 16-bit CRC polynomials in common use, but this is
7-
* *the* standard CRC-32 polynomial, first popularized by Ethernet.
8-
* x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0
9-
*/
5+
/* The polynomial used by crc32_le(), in integer form. See crc32_le(). */
106
#define CRC32_POLY_LE 0xedb88320
7+
8+
/* The polynomial used by crc32_be(), in integer form. See crc32_be(). */
119
#define CRC32_POLY_BE 0x04c11db7
1210

13-
/*
14-
* This is the CRC32c polynomial, as outlined by Castagnoli.
15-
* x^32+x^28+x^27+x^26+x^25+x^23+x^22+x^20+x^19+x^18+x^14+x^13+x^11+x^10+x^9+
16-
* x^8+x^6+x^0
17-
*/
18-
#define CRC32C_POLY_LE 0x82F63B78
11+
/* The polynomial used by crc32c(), in integer form. See crc32c(). */
12+
#define CRC32C_POLY_LE 0x82f63b78
1913

2014
#endif /* _LINUX_CRC32_POLY_H */

0 commit comments

Comments
 (0)