blob: f2e77e4af13e93c434f9f78cd0c1572e39a700a3 [file] [log] [blame]
Harald Welte197a4ac2017-10-16 14:29:26 +02001/*! \addtogroup crc
2 * @{
3 * \file crc16.h
4 * This was copied from the linux kernel and adjusted for our types.
Ingo Albrecht383134b2010-07-15 22:43:52 +02005 */
Harald Welte197a4ac2017-10-16 14:29:26 +02006
Ingo Albrecht383134b2010-07-15 22:43:52 +02007/*
8 * crc16.h - CRC-16 routine
9 *
10 * Implements the standard CRC-16:
Harald Welte197a4ac2017-10-16 14:29:26 +020011 * - Width 16
12 * - Poly 0x8005 (x^16 + x^15 + x^2 + 1)
13 * - Init 0
Ingo Albrecht383134b2010-07-15 22:43:52 +020014 *
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 Munaut12ba7782014-06-16 10:13:40 +020021#pragma once
Ingo Albrecht383134b2010-07-15 22:43:52 +020022
23#include <stdint.h>
24
25#include <sys/types.h>
26
Pablo Neira Ayusoddcd2af2011-05-07 12:43:12 +020027extern uint16_t const osmo_crc16_table[256];
Ingo Albrecht383134b2010-07-15 22:43:52 +020028
Pablo Neira Ayusoddcd2af2011-05-07 12:43:12 +020029extern uint16_t osmo_crc16(uint16_t crc, const uint8_t *buffer, size_t len);
Ingo Albrecht383134b2010-07-15 22:43:52 +020030
Harald Welte197a4ac2017-10-16 14:29:26 +020031/*! CRC-16 polynome 0x8005 (x^16 + x^15 + x^2 + 1) */
Pablo Neira Ayusoddcd2af2011-05-07 12:43:12 +020032static inline uint16_t osmo_crc16_byte(uint16_t crc, const uint8_t data)
Ingo Albrecht383134b2010-07-15 22:43:52 +020033{
Pablo Neira Ayusoddcd2af2011-05-07 12:43:12 +020034 return (crc >> 8) ^ osmo_crc16_table[(crc ^ data) & 0xff];
Ingo Albrecht383134b2010-07-15 22:43:52 +020035}
Harald Weltea0f74f22016-12-23 22:16:08 +010036
37
Harald Weltea0f74f22016-12-23 22:16:08 +010038
39extern uint16_t const osmo_crc16_ccitt_table[256];
40
41extern uint16_t osmo_crc16_ccitt(uint16_t crc, const uint8_t *buffer, size_t len);
42
Harald Welte197a4ac2017-10-16 14:29:26 +020043/*! CCITT polynome 0x8408 (x^0 + x^5 + x^12) */
Harald Weltea0f74f22016-12-23 22:16:08 +010044static 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 Welte197a4ac2017-10-16 14:29:26 +020048
49/*! @} */