xref: /haiku/src/libs/compat/openbsd_network/compat/netinet/if_ethersubr.h (revision 97f11716bfaa0f385eb0e28a52bf56a5023b9e99)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
32  * $FreeBSD$
33  */
34 
35 static inline u_int32_t
ether_crc32_le_update(u_int32_t crc,const u_int8_t * buf,size_t len)36 ether_crc32_le_update(u_int32_t crc, const u_int8_t *buf, size_t len)
37 {
38 	static const u_int32_t crctab[] = {
39 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
40 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
41 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
42 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
43 	};
44 	size_t i;
45 
46 	for (i = 0; i < len; i++) {
47 		crc ^= buf[i];
48 		crc = (crc >> 4) ^ crctab[crc & 0xf];
49 		crc = (crc >> 4) ^ crctab[crc & 0xf];
50 	}
51 
52 	return (crc);
53 }
54 
55 
56 static inline u_int32_t
ether_crc32_be_update(u_int32_t crc,const u_int8_t * buf,size_t len)57 ether_crc32_be_update(u_int32_t crc, const u_int8_t *buf, size_t len)
58 {
59 	static const u_int8_t rev[] = {
60 		0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
61 		0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
62 	};
63 	static const u_int32_t crctab[] = {
64 		0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
65 		0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
66 		0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
67 		0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd
68 	};
69 	size_t i;
70 	u_int8_t data;
71 
72 	for (i = 0; i < len; i++) {
73 		data = buf[i];
74 		crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data & 0xf]];
75 		crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data >> 4]];
76 	}
77 
78 	return (crc);
79 }
80