2 * crc checksum generation and calculation functions: crc.c
4 * Copyright (c) 2007 by Intel Corporation.
6 * Author: Mike Harvey <michael.harvey@intel.com>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1999 Gerald Combs
12 * SPDX-License-Identifier: GPL-2.0-or-later
17 #define WMAX_MAC_CRC32_POLYNOMIAL 0x04c11db7U /* polynomial used in calculating the CRC-32 checksum */
18 #define CCITT_X25_CRC16_POLYNOMIAL 0x1021 /* polynomial used in calculating the CRC-16 checksum */
19 #define WMAX_MAC_CRC8_POLYNOMIAL 0x07 /* polynomial used in calculating the CRC-8 checksum */
20 #define CRC32_INITIAL_VALUE 0xFFFFFFFF
21 #define CRC16_INITIAL_VALUE 0xFFFF
24 static uint8_t crc8_table
[256];
25 static uint32_t crc32_table
[256];
27 extern uint16_t crc16_table
[256];
30 void wimax_mac_gen_crc32_table(void)
32 REQUIRES: The functions must be called only once to initialze CRC table
34 DESCRIPTION: Generate the table of CRC remainders
35 for all possible bytes
44 void wimax_mac_gen_crc32_table(void)
49 /* little-endian (reflected) algorithm */
50 for ( i
= 0; i
< G_N_ELEMENTS(crc32_table
); i
++ )
53 for ( bit
= 0; bit
< 8; bit
++ )
55 if ( crc
& 0x80000000U
)
56 crc
= ( crc
<< 1 ) ^ WMAX_MAC_CRC32_POLYNOMIAL
;
65 void wimax_mac_gen_crc8_table(void)
67 REQUIRES: The functions must be called only once to initialze CRC table
69 DESCRIPTION: Generate the table of CRC remainders
70 for all possible bytes
79 void wimax_mac_gen_crc8_table(void)
84 for ( i
= 0; i
< G_N_ELEMENTS(crc8_table
); i
++ )
87 for ( bit
= 0; bit
< 8; bit
++ )
90 crc
= ( crc
<< 1 ) ^ WMAX_MAC_CRC8_POLYNOMIAL
;
101 uint32_t wimax_mac_calc_crc32(uint8_t *data, unsigned data_len)
103 REQUIRES: wimax_mac_gen_crc32_table() must be called before
105 DESCRIPTION: Calculate the 32-bit CRC from a given data block
107 ARGS: data - pointer to data
108 data_len - length of data (in bytes)
110 RETURNS: calculated crc32
115 uint32_t wimax_mac_calc_crc32(const uint8_t *data
, unsigned data_len
)
117 uint32_t crc
=CRC32_INITIAL_VALUE
;
120 for ( j
= 0; j
< data_len
; j
++ )
122 i
= ( (uint8_t)(crc
>>24) ^ data
[j
] ) & 0xff;
123 crc
= ( crc
<<8 ) ^ crc32_table
[i
];
130 uint16_t wimax_mac_calc_crc16(uint8_t *data, unsigned data_len)
132 REQUIRES: crc16_table[] in crc_data.c
134 DESCRIPTION: Calculate the 16-bit CRC from a given data block
136 ARGS: data - pointer to data
137 data_len - length of data (in bytes)
139 RETURNS: calculated crc16
144 uint16_t wimax_mac_calc_crc16(const uint8_t *data
, unsigned data_len
)
146 uint32_t crc
=CRC16_INITIAL_VALUE
;
149 for ( j
= 0; j
< data_len
; j
++ )
152 crc
= (crc
<< 8) ^ crc16_table
[(crc
& 0xff00) >> 8];
154 crc
^= 0xFFFF; /* Invert the output. */
161 uint8_t wimax_mac_calc_crc8(uint8_t *data, unsigned data_len)
163 REQUIRES: wimax_mac_gen_crc8_table() must be called before
165 DESCRIPTION: Calculate the 8-bit CRC from a given data block
167 ARGS: data - pointer to data
168 data_len - length of data (in bytes)
170 RETURNS: calculated crc8
175 uint8_t wimax_mac_calc_crc8(const uint8_t *data
, unsigned data_len
)
180 for(i
= 0; i
< data_len
; i
++)
182 crc
= crc8_table
[data
[i
]^crc
];
188 * Editor modelines - https://www.wireshark.org/tools/modelines.html
193 * indent-tabs-mode: nil
196 * ex: set shiftwidth=2 tabstop=8 expandtab:
197 * :indentSize=2:tabSize=8:noTabs=true: