xref: /linux-6.15/include/linux/crc32.h (revision 6d514b4e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * crc32.h
31da177e4SLinus Torvalds  * See linux/lib/crc32.c for license and changes
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds #ifndef _LINUX_CRC32_H
61da177e4SLinus Torvalds #define _LINUX_CRC32_H
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/types.h>
9906d66dfSAkinobu Mita #include <linux/bitrev.h>
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds extern u32  crc32_le(u32 crc, unsigned char const *p, size_t len);
121da177e4SLinus Torvalds extern u32  crc32_be(u32 crc, unsigned char const *p, size_t len);
131da177e4SLinus Torvalds 
146e95fcaaSDaniel Borkmann /**
156e95fcaaSDaniel Borkmann  * crc32_le_combine - Combine two crc32 check values into one. For two
166e95fcaaSDaniel Borkmann  * 		      sequences of bytes, seq1 and seq2 with lengths len1
176e95fcaaSDaniel Borkmann  * 		      and len2, crc32_le() check values were calculated
186e95fcaaSDaniel Borkmann  * 		      for each, crc1 and crc2.
196e95fcaaSDaniel Borkmann  *
206e95fcaaSDaniel Borkmann  * @crc1: crc32 of the first block
216e95fcaaSDaniel Borkmann  * @crc2: crc32 of the second block
226e95fcaaSDaniel Borkmann  * @len2: length of the second block
236e95fcaaSDaniel Borkmann  *
246e95fcaaSDaniel Borkmann  * Return: The crc32_le() check value of seq1 and seq2 concatenated,
256e95fcaaSDaniel Borkmann  * 	   requiring only crc1, crc2, and len2. Note: If seq_full denotes
266e95fcaaSDaniel Borkmann  * 	   the concatenated memory area of seq1 with seq2, and crc_full
276e95fcaaSDaniel Borkmann  * 	   the crc32_le() value of seq_full, then crc_full ==
286e95fcaaSDaniel Borkmann  * 	   crc32_le_combine(crc1, crc2, len2) when crc_full was seeded
296e95fcaaSDaniel Borkmann  * 	   with the same initializer as crc1, and crc2 seed was 0. See
306e95fcaaSDaniel Borkmann  * 	   also crc32_combine_test().
316e95fcaaSDaniel Borkmann  */
32*6d514b4eSGeorge Spelvin u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len);
33*6d514b4eSGeorge Spelvin 
34*6d514b4eSGeorge Spelvin static inline u32 crc32_le_combine(u32 crc1, u32 crc2, size_t len2)
35*6d514b4eSGeorge Spelvin {
36*6d514b4eSGeorge Spelvin 	return crc32_le_shift(crc1, len2) ^ crc2;
37*6d514b4eSGeorge Spelvin }
386e95fcaaSDaniel Borkmann 
3946c5801eSDarrick J. Wong extern u32  __crc32c_le(u32 crc, unsigned char const *p, size_t len);
4046c5801eSDarrick J. Wong 
416e95fcaaSDaniel Borkmann /**
426e95fcaaSDaniel Borkmann  * __crc32c_le_combine - Combine two crc32c check values into one. For two
436e95fcaaSDaniel Borkmann  * 			 sequences of bytes, seq1 and seq2 with lengths len1
446e95fcaaSDaniel Borkmann  * 			 and len2, __crc32c_le() check values were calculated
456e95fcaaSDaniel Borkmann  * 			 for each, crc1 and crc2.
466e95fcaaSDaniel Borkmann  *
476e95fcaaSDaniel Borkmann  * @crc1: crc32c of the first block
486e95fcaaSDaniel Borkmann  * @crc2: crc32c of the second block
496e95fcaaSDaniel Borkmann  * @len2: length of the second block
506e95fcaaSDaniel Borkmann  *
516e95fcaaSDaniel Borkmann  * Return: The __crc32c_le() check value of seq1 and seq2 concatenated,
526e95fcaaSDaniel Borkmann  * 	   requiring only crc1, crc2, and len2. Note: If seq_full denotes
536e95fcaaSDaniel Borkmann  * 	   the concatenated memory area of seq1 with seq2, and crc_full
546e95fcaaSDaniel Borkmann  * 	   the __crc32c_le() value of seq_full, then crc_full ==
556e95fcaaSDaniel Borkmann  * 	   __crc32c_le_combine(crc1, crc2, len2) when crc_full was
566e95fcaaSDaniel Borkmann  * 	   seeded with the same initializer as crc1, and crc2 seed
576e95fcaaSDaniel Borkmann  * 	   was 0. See also crc32c_combine_test().
586e95fcaaSDaniel Borkmann  */
59*6d514b4eSGeorge Spelvin u32 __attribute_const__ __crc32c_le_shift(u32 crc, size_t len);
60*6d514b4eSGeorge Spelvin 
61*6d514b4eSGeorge Spelvin static inline u32 __crc32c_le_combine(u32 crc1, u32 crc2, size_t len2)
62*6d514b4eSGeorge Spelvin {
63*6d514b4eSGeorge Spelvin 	return __crc32c_le_shift(crc1, len2) ^ crc2;
64*6d514b4eSGeorge Spelvin }
656e95fcaaSDaniel Borkmann 
66d03e1617SKonstantin Khlebnikov #define crc32(seed, data, length)  crc32_le(seed, (unsigned char const *)(data), length)
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /*
691da177e4SLinus Torvalds  * Helpers for hash table generation of ethernet nics:
701da177e4SLinus Torvalds  *
711da177e4SLinus Torvalds  * Ethernet sends the least significant bit of a byte first, thus crc32_le
721da177e4SLinus Torvalds  * is used. The output of crc32_le is bit reversed [most significant bit
731da177e4SLinus Torvalds  * is in bit nr 0], thus it must be reversed before use. Except for
741da177e4SLinus Torvalds  * nics that bit swap the result internally...
751da177e4SLinus Torvalds  */
76906d66dfSAkinobu Mita #define ether_crc(length, data)    bitrev32(crc32_le(~0, data, length))
771da177e4SLinus Torvalds #define ether_crc_le(length, data) crc32_le(~0, data, length)
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds #endif /* _LINUX_CRC32_H */
80