Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / plugins / epan / wimax / crc.c
blob0aacc4d6523e8be714dd79353346dd8bdfb5fd7c
1 /* crc.c
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
15 #include "crc.h"
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
23 #ifndef STATIC_DATA
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
37 ARGS:
39 RETURNS:
41 SIDE EFFECTS:
44 void wimax_mac_gen_crc32_table(void)
46 uint32_t i, bit;
47 uint32_t crc;
49 /* little-endian (reflected) algorithm */
50 for ( i = 0; i < G_N_ELEMENTS(crc32_table); i++ )
52 crc = ( i << 24 );
53 for ( bit = 0; bit < 8; bit++ )
55 if ( crc & 0x80000000U )
56 crc = ( crc << 1 ) ^ WMAX_MAC_CRC32_POLYNOMIAL;
57 else
58 crc = ( crc << 1 );
60 crc32_table[i] = crc;
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
72 ARGS:
74 RETURNS:
76 SIDE EFFECTS:
79 void wimax_mac_gen_crc8_table(void)
81 unsigned i, bit;
82 uint8_t crc;
84 for ( i = 0; i < G_N_ELEMENTS(crc8_table); i++ )
86 crc = i;
87 for ( bit = 0; bit < 8; bit++ )
89 if ( crc & 0x80 )
90 crc = ( crc << 1 ) ^ WMAX_MAC_CRC8_POLYNOMIAL;
91 else
92 crc = ( crc << 1 );
94 crc8_table[i] = crc;
97 #endif
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
112 SIDE EFFECTS:
115 uint32_t wimax_mac_calc_crc32(const uint8_t *data, unsigned data_len)
117 uint32_t crc=CRC32_INITIAL_VALUE;
118 unsigned i, j;
120 for ( j = 0; j < data_len; j++ )
122 i = ( (uint8_t)(crc>>24) ^ data[j] ) & 0xff;
123 crc = ( crc<<8 ) ^ crc32_table[i];
125 return ~crc;
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
141 SIDE EFFECTS:
144 uint16_t wimax_mac_calc_crc16(const uint8_t *data, unsigned data_len)
146 uint32_t crc=CRC16_INITIAL_VALUE;
147 unsigned j;
149 for ( j = 0; j < data_len; j++ )
151 crc ^= data[j] << 8;
152 crc = (crc << 8) ^ crc16_table[(crc & 0xff00) >> 8];
154 crc ^= 0xFFFF; /* Invert the output. */
155 crc &= 0xFFFF;
156 return crc;
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
172 SIDE EFFECTS:
175 uint8_t wimax_mac_calc_crc8(const uint8_t *data, unsigned data_len)
177 uint8_t crc=0;
178 unsigned i;
180 for(i = 0; i < data_len; i++)
182 crc = crc8_table[data[i]^crc];
184 return crc;
188 * Editor modelines - https://www.wireshark.org/tools/modelines.html
190 * Local Variables:
191 * c-basic-offset: 2
192 * tab-width: 8
193 * indent-tabs-mode: nil
194 * End:
196 * ex: set shiftwidth=2 tabstop=8 expandtab:
197 * :indentSize=2:tabSize=8:noTabs=true: