aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/nfp/nfpcore/nfp_crc.c
blob: 20431bf845f6946e8039096a00dd926fa65c4eaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018 Netronome Systems, Inc.
 * All rights reserved.
 */

#include <stdio.h>
#include <inttypes.h>

#include "nfp_crc.h"

static inline uint32_t
nfp_crc32_be_generic(uint32_t crc, unsigned char const *p, size_t len,
		 uint32_t polynomial)
{
	int i;
	while (len--) {
		crc ^= *p++ << 24;
		for (i = 0; i < 8; i++)
			crc = (crc << 1) ^ ((crc & 0x80000000) ? polynomial :
					  0);
	}
	return crc;
}

static inline uint32_t
nfp_crc32_be(uint32_t crc, unsigned char const *p, size_t len)
{
	return nfp_crc32_be_generic(crc, p, len, CRCPOLY_BE);
}

static uint32_t
nfp_crc32_posix_end(uint32_t crc, size_t total_len)
{
	/* Extend with the length of the string. */
	while (total_len != 0) {
		uint8_t c = total_len & 0xff;

		crc = nfp_crc32_be(crc, &c, 1);
		total_len >>= 8;
	}

	return ~crc;
}

uint32_t
nfp_crc32_posix(const void *buff, size_t len)
{
	return nfp_crc32_posix_end(nfp_crc32_be(0, buff, len), len);
}