From 0b73ce30604c4fc9a004338cac6a28bc3f3e2fab Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Sat, 23 Jan 2021 18:39:57 +0800 Subject: util: Add CRC16 (CCITT) calculation routines Import CRC16 calculation routines from Linux kernel v5.10: include/linux/crc-ccitt.h lib/crc-ccitt.c to QEMU: include/qemu/crc-ccitt.h util/crc-ccitt.c Signed-off-by: Bin Meng Acked-by: Alistair Francis Message-Id: <20210123104016.17485-7-bmeng.cn@gmail.com> Reviewed-by: Philippe Mathieu-Daudé [PMD: Restrict compilation to system emulation] Signed-off-by: Philippe Mathieu-Daudé --- include/qemu/crc-ccitt.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 include/qemu/crc-ccitt.h (limited to 'include/qemu') diff --git a/include/qemu/crc-ccitt.h b/include/qemu/crc-ccitt.h new file mode 100644 index 0000000000..06ee55b159 --- /dev/null +++ b/include/qemu/crc-ccitt.h @@ -0,0 +1,33 @@ +/* + * CRC16 (CCITT) Checksum Algorithm + * + * Copyright (c) 2021 Wind River Systems, Inc. + * + * Author: + * Bin Meng + * + * From Linux kernel v5.10 include/linux/crc-ccitt.h + * + * SPDX-License-Identifier: GPL-2.0 + */ + +#ifndef _CRC_CCITT_H +#define _CRC_CCITT_H + +extern uint16_t const crc_ccitt_table[256]; +extern uint16_t const crc_ccitt_false_table[256]; + +extern uint16_t crc_ccitt(uint16_t crc, const uint8_t *buffer, size_t len); +extern uint16_t crc_ccitt_false(uint16_t crc, const uint8_t *buffer, size_t len); + +static inline uint16_t crc_ccitt_byte(uint16_t crc, const uint8_t c) +{ + return (crc >> 8) ^ crc_ccitt_table[(crc ^ c) & 0xff]; +} + +static inline uint16_t crc_ccitt_false_byte(uint16_t crc, const uint8_t c) +{ + return (crc << 8) ^ crc_ccitt_false_table[(crc >> 8) ^ c]; +} + +#endif /* _CRC_CCITT_H */ -- cgit v1.2.3-55-g7522