1
2
3#include <linux/kernel.h>
4#include <linux/module.h>
5#include <linux/types.h>
6#include <linux/slab.h>
7#include <linux/init.h>
8#include <asm/atomic.h>
9
10#include "crc32.h"
11
12#define CRCPOLY_BE 0x04c11db7
13#define CRC_BE_BITS 8
14
15static u32 *bnep_crc32_table;
16
17/*
18 * This code is in the public domain; copyright abandoned.
19 * Liability for non-performance of this code is limited to the amount
20 * you paid for it.  Since it is distributed for free, your refund will
21 * be very very small.  If it breaks, you get to keep both pieces.
22 */
23u32 bnep_crc32(u32 crc, unsigned char const *p, size_t len)
24{
25	while (len--)
26		crc = (crc << 8) ^ bnep_crc32_table[(crc >> 24) ^ *p++];
27
28	return crc;
29}
30
31int __init bnep_crc32_init(void)
32{
33	unsigned i, j;
34	u32 crc = 0x80000000;
35
36	bnep_crc32_table = kmalloc((1 << CRC_BE_BITS) * sizeof(u32), GFP_KERNEL);
37	if (!bnep_crc32_table)
38		return -ENOMEM;
39
40	bnep_crc32_table[0] = 0;
41
42	for (i = 1; i < 1 << CRC_BE_BITS; i <<= 1) {
43		crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
44		for (j = 0; j < i; j++)
45			bnep_crc32_table[i + j] = crc ^ bnep_crc32_table[j];
46	}
47	return 0;
48}
49
50void __exit bnep_crc32_cleanup(void)
51{
52	if (bnep_crc32_table)
53		kfree(bnep_crc32_table);
54	bnep_crc32_table = NULL;
55}
56