Harald Welte | 197a4ac | 2017-10-16 14:29:26 +0200 | [diff] [blame] | 1 | /*! \addtogroup crc |
| 2 | * @{ |
| 3 | * \file crc16.h |
| 4 | * This was copied from the linux kernel and adjusted for our types. |
Ingo Albrecht | 383134b | 2010-07-15 22:43:52 +0200 | [diff] [blame] | 5 | */ |
Harald Welte | 197a4ac | 2017-10-16 14:29:26 +0200 | [diff] [blame] | 6 | |
Ingo Albrecht | 383134b | 2010-07-15 22:43:52 +0200 | [diff] [blame] | 7 | /* |
| 8 | * crc16.h - CRC-16 routine |
| 9 | * |
| 10 | * Implements the standard CRC-16: |
Harald Welte | 197a4ac | 2017-10-16 14:29:26 +0200 | [diff] [blame] | 11 | * - Width 16 |
| 12 | * - Poly 0x8005 (x^16 + x^15 + x^2 + 1) |
| 13 | * - Init 0 |
Ingo Albrecht | 383134b | 2010-07-15 22:43:52 +0200 | [diff] [blame] | 14 | * |
| 15 | * Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com> |
| 16 | * |
| 17 | * This source code is licensed under the GNU General Public License, |
| 18 | * Version 2. See the file COPYING for more details. |
| 19 | */ |
| 20 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 21 | #pragma once |
Ingo Albrecht | 383134b | 2010-07-15 22:43:52 +0200 | [diff] [blame] | 22 | |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include <sys/types.h> |
| 26 | |
Pablo Neira Ayuso | ddcd2af | 2011-05-07 12:43:12 +0200 | [diff] [blame] | 27 | extern uint16_t const osmo_crc16_table[256]; |
Ingo Albrecht | 383134b | 2010-07-15 22:43:52 +0200 | [diff] [blame] | 28 | |
Pablo Neira Ayuso | ddcd2af | 2011-05-07 12:43:12 +0200 | [diff] [blame] | 29 | extern uint16_t osmo_crc16(uint16_t crc, const uint8_t *buffer, size_t len); |
Ingo Albrecht | 383134b | 2010-07-15 22:43:52 +0200 | [diff] [blame] | 30 | |
Harald Welte | 197a4ac | 2017-10-16 14:29:26 +0200 | [diff] [blame] | 31 | /*! CRC-16 polynome 0x8005 (x^16 + x^15 + x^2 + 1) */ |
Pablo Neira Ayuso | ddcd2af | 2011-05-07 12:43:12 +0200 | [diff] [blame] | 32 | static inline uint16_t osmo_crc16_byte(uint16_t crc, const uint8_t data) |
Ingo Albrecht | 383134b | 2010-07-15 22:43:52 +0200 | [diff] [blame] | 33 | { |
Pablo Neira Ayuso | ddcd2af | 2011-05-07 12:43:12 +0200 | [diff] [blame] | 34 | return (crc >> 8) ^ osmo_crc16_table[(crc ^ data) & 0xff]; |
Ingo Albrecht | 383134b | 2010-07-15 22:43:52 +0200 | [diff] [blame] | 35 | } |
Harald Welte | a0f74f2 | 2016-12-23 22:16:08 +0100 | [diff] [blame] | 36 | |
| 37 | |
Harald Welte | a0f74f2 | 2016-12-23 22:16:08 +0100 | [diff] [blame] | 38 | |
| 39 | extern uint16_t const osmo_crc16_ccitt_table[256]; |
| 40 | |
| 41 | extern uint16_t osmo_crc16_ccitt(uint16_t crc, const uint8_t *buffer, size_t len); |
| 42 | |
Harald Welte | 197a4ac | 2017-10-16 14:29:26 +0200 | [diff] [blame] | 43 | /*! CCITT polynome 0x8408 (x^0 + x^5 + x^12) */ |
Harald Welte | a0f74f2 | 2016-12-23 22:16:08 +0100 | [diff] [blame] | 44 | static inline uint16_t osmo_crc16_ccitt_byte(uint16_t crc, const uint8_t data) |
| 45 | { |
| 46 | return (crc >> 8) ^ osmo_crc16_ccitt_table[(crc ^ data) & 0xff]; |
| 47 | } |
Harald Welte | 197a4ac | 2017-10-16 14:29:26 +0200 | [diff] [blame] | 48 | |
| 49 | /*! @} */ |