1/*-
2 * Copyright (c) 1982, 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
30 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.193.2.12 2006/08/28 02:54:14 thompsa Exp $
31 */
32#include <stdio.h>
33#include <sys/types.h>
34#include <net/ethernet.h>
35
36#if 0
37/*
38 * This is for reference.  We have a table-driven version
39 * of the little-endian crc32 generator, which is faster
40 * than the double-loop.
41 */
42uint32_t
43ether_crc32_le(const uint8_t *buf, size_t len)
44{
45	size_t i;
46	uint32_t crc;
47	int bit;
48	uint8_t data;
49
50	crc = 0xffffffff;	/* initial value */
51
52	for (i = 0; i < len; i++) {
53		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
54			carry = (crc ^ data) & 1;
55			crc >>= 1;
56			if (carry)
57				crc = (crc ^ ETHER_CRC_POLY_LE);
58	}
59
60	return (crc);
61}
62#else
63uint32_t
64ether_crc32_le(const uint8_t *buf, size_t len)
65{
66	static const uint32_t crctab[] = {
67		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
68		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
69		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
70		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
71	};
72	size_t i;
73	uint32_t crc;
74
75	crc = 0xffffffff;	/* initial value */
76
77	for (i = 0; i < len; i++) {
78		crc ^= buf[i];
79		crc = (crc >> 4) ^ crctab[crc & 0xf];
80		crc = (crc >> 4) ^ crctab[crc & 0xf];
81	}
82
83	return (crc);
84}
85#endif
86
87uint32_t
88ether_crc32_be(const uint8_t *buf, size_t len)
89{
90	size_t i;
91	uint32_t crc, carry;
92	int bit;
93	uint8_t data;
94
95	crc = 0xffffffff;	/* initial value */
96
97	for (i = 0; i < len; i++) {
98		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
99			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
100			crc <<= 1;
101			if (carry)
102				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
103		}
104	}
105
106	return (crc);
107}
108
109char *
110ether_sprintf(const u_char *ap)
111{
112	static char etherbuf[18];
113	snprintf(etherbuf, sizeof (etherbuf),
114		"%02x:%02x:%02x:%02x:%02x:%02x",
115		(unsigned)ap[0], (unsigned)ap[1], (unsigned)ap[2],
116		(unsigned)ap[3], (unsigned)ap[4], (unsigned)ap[5]);
117	return (etherbuf);
118}
119