xref: /netbsd-src/sys/lib/libkern/crc16.h (revision 72d26a2dda23791b892cf8f1f49376e5de407c78)
1*72d26a2dSrin /*	$NetBSD: crc16.h,v 1.3 2020/04/16 23:29:53 rin Exp $	*/
23c404120Sjdolecek 
33c404120Sjdolecek /*-
43c404120Sjdolecek  * Copyright (c) 2006 The NetBSD Foundation, Inc.
53c404120Sjdolecek  * All rights reserved.
63c404120Sjdolecek  *
73c404120Sjdolecek  * This code is derived from software contributed to The NetBSD Foundation
83c404120Sjdolecek  * by Steve C. Woodford.
93c404120Sjdolecek  *
103c404120Sjdolecek  * Redistribution and use in source and binary forms, with or without
113c404120Sjdolecek  * modification, are permitted provided that the following conditions
123c404120Sjdolecek  * are met:
133c404120Sjdolecek  * 1. Redistributions of source code must retain the above copyright
143c404120Sjdolecek  *    notice, this list of conditions and the following disclaimer.
153c404120Sjdolecek  * 2. Redistributions in binary form must reproduce the above copyright
163c404120Sjdolecek  *    notice, this list of conditions and the following disclaimer in the
173c404120Sjdolecek  *    documentation and/or other materials provided with the distribution.
183c404120Sjdolecek  *
193c404120Sjdolecek  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
203c404120Sjdolecek  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
213c404120Sjdolecek  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223c404120Sjdolecek  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
233c404120Sjdolecek  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
243c404120Sjdolecek  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
253c404120Sjdolecek  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
263c404120Sjdolecek  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
273c404120Sjdolecek  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
283c404120Sjdolecek  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
293c404120Sjdolecek  * POSSIBILITY OF SUCH DAMAGE.
303c404120Sjdolecek  */
313c404120Sjdolecek 
32*72d26a2dSrin #include <sys/cdefs.h>
33*72d26a2dSrin __KERNEL_RCSID(0, "$NetBSD: crc16.h,v 1.3 2020/04/16 23:29:53 rin Exp $");
34*72d26a2dSrin 
353c404120Sjdolecek /* CRC table for standard ANSI CRC-16 with polynom 0x8005 */
363c404120Sjdolecek static const uint16_t crc16_lookup[] = {
373c404120Sjdolecek 	0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
383c404120Sjdolecek 	0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
393c404120Sjdolecek 	0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
403c404120Sjdolecek 	0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
413c404120Sjdolecek 	0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
423c404120Sjdolecek 	0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
433c404120Sjdolecek 	0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
443c404120Sjdolecek 	0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
453c404120Sjdolecek 	0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
463c404120Sjdolecek 	0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
473c404120Sjdolecek 	0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
483c404120Sjdolecek 	0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
493c404120Sjdolecek 	0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
503c404120Sjdolecek 	0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
513c404120Sjdolecek 	0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
523c404120Sjdolecek 	0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
533c404120Sjdolecek 	0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
543c404120Sjdolecek 	0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
553c404120Sjdolecek 	0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
563c404120Sjdolecek 	0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
573c404120Sjdolecek 	0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
583c404120Sjdolecek 	0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
593c404120Sjdolecek 	0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
603c404120Sjdolecek 	0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
613c404120Sjdolecek 	0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
623c404120Sjdolecek 	0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
633c404120Sjdolecek 	0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
643c404120Sjdolecek 	0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
653c404120Sjdolecek 	0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
663c404120Sjdolecek 	0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
673c404120Sjdolecek 	0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
683c404120Sjdolecek 	0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
693c404120Sjdolecek };
703c404120Sjdolecek 
713c404120Sjdolecek static __inline uint16_t
crc16_byte(uint16_t ccrc,const uint8_t b)723c404120Sjdolecek crc16_byte(uint16_t ccrc, const uint8_t b)
733c404120Sjdolecek {
743c404120Sjdolecek 	return crc16_lookup[((ccrc) ^ (b)) & 0xffu] ^ ((ccrc) >> 8);
753c404120Sjdolecek }
763c404120Sjdolecek 
773c404120Sjdolecek static __inline uint16_t __unused
crc16(uint16_t crc,const uint8_t * data,size_t len)783c404120Sjdolecek crc16(uint16_t crc, const uint8_t *data, size_t len)
793c404120Sjdolecek {
803c404120Sjdolecek 	while (len--)
813c404120Sjdolecek 		crc = crc16_byte(crc, *data++);
823c404120Sjdolecek 	return crc;
833c404120Sjdolecek }
84