4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
29 * Fast CRC32 calculation algorithm suggested by Ferenc Rakoczi
30 * (ferenc.rakoczi@sun.com). The basic idea is to look at it
31 * four bytes (one word) at a time, using four tables. The
32 * standard algorithm in RFC 3309 uses one table.
36 * SCTP uses reflected/reverse polynomial CRC32 with generating
37 * polynomial 0x1EDC6F41L
39 #define SCTP_POLY 0x1EDC6F41L
41 /* The four CRC tables. */
42 static uint32_t crctab
[4][256];
45 reflect_32(uint32_t b
)
50 for (i
= 0; i
< 32; i
++) {
62 * This function is only used for big endian processor.
67 return (((w
>> 24) | ((w
>> 8) & 0xff00) | ((w
<< 8) & 0xff0000) |
76 uint32_t i
, j
, k
, crc
;
78 for (i
= 0; i
< 256; i
++) {
80 for (k
= 0; k
< 4; k
++) {
81 for (j
= 0; j
< 8; j
++) {
82 crc
= (crc
& 0x80000000) ?
83 (crc
<< 1) ^ SCTP_POLY
: crc
<< 1;
86 crctab
[3 - k
][i
] = flip32(reflect_32(crc
));
88 crctab
[k
][i
] = reflect_32(crc
);
95 sctp_crc_byte(uint32_t *crcptr
, const uint8_t *buf
, int len
)
101 for (i
= 0; i
< len
; i
++) {
103 crc
= (crc
<< 8) ^ crctab
[3][buf
[i
] ^ (crc
>> 24)];
105 crc
= (crc
>> 8) ^ crctab
[0][buf
[i
] ^ (crc
& 0xff)];
112 sctp_crc_word(uint32_t *crcptr
, const uint32_t *buf
, int len
)
118 for (i
= 0; i
< len
; i
++) {
120 crc
= crctab
[0][w
>> 24] ^ crctab
[1][(w
>> 16) & 0xff] ^
121 crctab
[2][(w
>> 8) & 0xff] ^ crctab
[3][w
& 0xff];
127 sctp_crc32(uint32_t crc32
, const uint8_t *buf
, int len
)
131 rem
= 4 - ((uintptr_t)buf
) & 3;
136 sctp_crc_byte(&crc32
, buf
, rem
);
142 sctp_crc_word(&crc32
, (const uint32_t *)buf
, len
/ 4);
147 sctp_crc_byte(&crc32
, buf
+ len
- rem
, rem
);