blob: f1564bd23b3c13c3136ec1dfd5d3c1c35669b3cd [file] [log] [blame]
Ingo Albrecht383134b2010-07-15 22:43:52 +02001/*
2 * This was copied from the linux kernel and adjusted for our types.
3 */
4/*
5 * crc16.h - CRC-16 routine
6 *
7 * Implements the standard CRC-16:
8 * Width 16
9 * Poly 0x8005 (x^16 + x^15 + x^2 + 1)
10 * Init 0
11 *
12 * Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com>
13 *
14 * This source code is licensed under the GNU General Public License,
15 * Version 2. See the file COPYING for more details.
16 */
17
Sylvain Munaut12ba7782014-06-16 10:13:40 +020018#pragma once
Ingo Albrecht383134b2010-07-15 22:43:52 +020019
20#include <stdint.h>
21
22#include <sys/types.h>
23
Pablo Neira Ayusoddcd2af2011-05-07 12:43:12 +020024extern uint16_t const osmo_crc16_table[256];
Ingo Albrecht383134b2010-07-15 22:43:52 +020025
Pablo Neira Ayusoddcd2af2011-05-07 12:43:12 +020026extern uint16_t osmo_crc16(uint16_t crc, const uint8_t *buffer, size_t len);
Ingo Albrecht383134b2010-07-15 22:43:52 +020027
Pablo Neira Ayusoddcd2af2011-05-07 12:43:12 +020028static inline uint16_t osmo_crc16_byte(uint16_t crc, const uint8_t data)
Ingo Albrecht383134b2010-07-15 22:43:52 +020029{
Pablo Neira Ayusoddcd2af2011-05-07 12:43:12 +020030 return (crc >> 8) ^ osmo_crc16_table[(crc ^ data) & 0xff];
Ingo Albrecht383134b2010-07-15 22:43:52 +020031}
Harald Weltea0f74f22016-12-23 22:16:08 +010032
33
34/* CCITT polynome 0x8408. This corresponds to x^0 + x^5 + x^12 */
35
36extern uint16_t const osmo_crc16_ccitt_table[256];
37
38extern uint16_t osmo_crc16_ccitt(uint16_t crc, const uint8_t *buffer, size_t len);
39
40static inline uint16_t osmo_crc16_ccitt_byte(uint16_t crc, const uint8_t data)
41{
42 return (crc >> 8) ^ osmo_crc16_ccitt_table[(crc ^ data) & 0xff];
43}