1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2015-2017 Netronome Systems, Inc. */
3
4 #ifndef NFP_CRC32_H
5 #define NFP_CRC32_H
6
7 #include <linux/kernel.h>
8 #include <linux/crc32.h>
9
10 /**
11 * crc32_posix_end() - Finalize POSIX CRC32 working state
12 * @crc: Current CRC32 working state
13 * @total_len: Total length of data that was CRC32'd
14 *
15 * Return: Final POSIX CRC32 value
16 */
crc32_posix_end(u32 crc,size_t total_len)17 static inline u32 crc32_posix_end(u32 crc, size_t total_len)
18 {
19 /* Extend with the length of the string. */
20 while (total_len != 0) {
21 u8 c = total_len & 0xff;
22
23 crc = crc32_be(crc, &c, 1);
24 total_len >>= 8;
25 }
26
27 return ~crc;
28 }
29
crc32_posix(const void * buff,size_t len)30 static inline u32 crc32_posix(const void *buff, size_t len)
31 {
32 return crc32_posix_end(crc32_be(0, buff, len), len);
33 }
34
35 #endif /* NFP_CRC32_H */
36