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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
/*
* Fast CRC32 calculation algorithm suggested by Ferenc Rakoczi
* (ferenc.rakoczi@sun.com). The basic idea is to look at it
* four bytes (one word) at a time, using four tables. The
* standard algorithm in RFC 3309 uses one table.
*/
/*
* SCTP uses reflected/reverse polynomial CRC32 with generating
* polynomial 0x1EDC6F41L
*/
#define SCTP_POLY 0x1EDC6F41L
/* The four CRC tables. */
static uint32_t crctab[4][256];
static uint32_t
reflect_32(uint32_t b)
{
int i;
uint32_t rw = 0;
for (i = 0; i < 32; i++) {
if (b & 1) {
rw |= 1 << (31 - i);
}
b >>= 1;
}
return (rw);
}
#ifdef _BIG_ENDIAN
/*
* This function is only used for big endian processor.
*/
static uint32_t
flip32(uint32_t w)
{
return (((w >> 24) | ((w >> 8) & 0xff00) | ((w << 8) & 0xff0000) |
(w << 24)));
}
#endif
void
sctp_crc32_init(void)
{
uint32_t i, j, k, crc;
for (i = 0; i < 256; i++) {
crc = reflect_32(i);
for (k = 0; k < 4; k++) {
for (j = 0; j < 8; j++) {
crc = (crc & 0x80000000) ?
(crc << 1) ^ SCTP_POLY : crc << 1;
}
#ifdef _BIG_ENDIAN
crctab[3 - k][i] = flip32(reflect_32(crc));
#else
crctab[k][i] = reflect_32(crc);
#endif
}
}
}
static void
sctp_crc_byte(uint32_t *crcptr, const uint8_t *buf, int len)
{
uint32_t crc;
int i;
crc = *crcptr;
for (i = 0; i < len; i++) {
#ifdef _BIG_ENDIAN
crc = (crc << 8) ^ crctab[3][buf[i] ^ (crc >> 24)];
#else
crc = (crc >> 8) ^ crctab[0][buf[i] ^ (crc & 0xff)];
#endif
}
*crcptr = crc;
}
static void
sctp_crc_word(uint32_t *crcptr, const uint32_t *buf, int len)
{
uint32_t w, crc;
int i;
crc = *crcptr;
for (i = 0; i < len; i++) {
w = crc ^ buf[i];
crc = crctab[0][w >> 24] ^ crctab[1][(w >> 16) & 0xff] ^
crctab[2][(w >> 8) & 0xff] ^ crctab[3][w & 0xff];
}
*crcptr = crc;
}
uint32_t
sctp_crc32(uint32_t crc32, const uint8_t *buf, int len)
{
int rem;
rem = 4 - ((uintptr_t)buf) & 3;
if (rem != 0) {
if (len < rem) {
rem = len;
}
sctp_crc_byte(&crc32, buf, rem);
buf = buf + rem;
len = len - rem;
}
if (len > 3) {
sctp_crc_word(&crc32, (const uint32_t *)buf, len / 4);
}
rem = len & 3;
if (rem != 0) {
sctp_crc_byte(&crc32, buf + len - rem, rem);
}
return (crc32);
}
|