summaryrefslogtreecommitdiffstats
path: root/plugins/epan/wimax/crc.c
blob: 4bc74a2a49e48c149145d180ce4aa7f5eb29f424 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* crc.c
 * crc checksum generation and calculation functions: crc.c
 *
 * Copyright (c) 2007 by Intel Corporation.
 *
 * Author: Mike Harvey <michael.harvey@intel.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1999 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "crc.h"

#define WMAX_MAC_CRC32_POLYNOMIAL 0x04c11db7U /* polynomial used in calculating the CRC-32 checksum */
#define CCITT_X25_CRC16_POLYNOMIAL 0x1021     /* polynomial used in calculating the CRC-16 checksum */
#define WMAX_MAC_CRC8_POLYNOMIAL  0x07        /* polynomial used in calculating the CRC-8 checksum */
#define CRC32_INITIAL_VALUE       0xFFFFFFFF
#define CRC16_INITIAL_VALUE       0xFFFF

#ifndef STATIC_DATA
static guint8  crc8_table[256];
static guint32 crc32_table[256];

extern guint16 crc16_table[256];

/*
  void wimax_mac_gen_crc32_table(void)

  REQUIRES: The functions must be called only once to initialze CRC table

  DESCRIPTION:  Generate the table of CRC remainders
                for all possible bytes

  ARGS:

  RETURNS:

  SIDE EFFECTS:

*/
void wimax_mac_gen_crc32_table(void)
{
  guint32 i, bit;
  guint32 crc;

  /* little-endian (reflected) algorithm */
  for ( i = 0;  i < G_N_ELEMENTS(crc32_table);  i++ )
  {
    crc = ( i << 24 );
    for ( bit = 0;  bit < 8;  bit++ )
    {
      if ( crc & 0x80000000U )
        crc = ( crc << 1 ) ^ WMAX_MAC_CRC32_POLYNOMIAL;
      else
        crc = ( crc << 1 );
    }
    crc32_table[i] = crc;
  }
}

/*
  void wimax_mac_gen_crc8_table(void)

  REQUIRES: The functions must be called only once to initialze CRC table

  DESCRIPTION:  Generate the table of CRC remainders
                for all possible bytes

  ARGS:

  RETURNS:

  SIDE EFFECTS:

*/
void wimax_mac_gen_crc8_table(void)
{
  guint  i, bit;
  guint8 crc;

  for ( i = 0;  i < G_N_ELEMENTS(crc8_table);  i++ )
  {
    crc = i;
    for ( bit = 0;  bit < 8;  bit++ )
    {
      if ( crc & 0x80 )
        crc = ( crc << 1 ) ^ WMAX_MAC_CRC8_POLYNOMIAL;
      else
        crc = ( crc << 1 );
    }
    crc8_table[i] = crc;
  }
}
#endif

/*

  guint32 wimax_mac_calc_crc32(guint8 *data, guint data_len)

  REQUIRES: wimax_mac_gen_crc32_table() must be called before

  DESCRIPTION: Calculate the 32-bit CRC from a given data block

  ARGS:  data - pointer to data
         data_len - length of data (in bytes)

  RETURNS:  calculated crc32

  SIDE EFFECTS:

*/
guint32 wimax_mac_calc_crc32(const guint8 *data, guint data_len)
{
  guint32 crc=CRC32_INITIAL_VALUE;
  guint i, j;

  for ( j = 0;  j < data_len;  j++ )
  {
    i = ( (guint8)(crc>>24) ^ data[j] ) & 0xff;
    crc = ( crc<<8 ) ^ crc32_table[i];
  }
  return ~crc;
}

/*

  guint16 wimax_mac_calc_crc16(guint8 *data, guint data_len)

  REQUIRES: crc16_table[] in crc_data.c

  DESCRIPTION: Calculate the 16-bit CRC from a given data block

  ARGS:  data - pointer to data
         data_len - length of data (in bytes)

  RETURNS:  calculated crc16

  SIDE EFFECTS:

*/
guint16 wimax_mac_calc_crc16(const guint8 *data, guint data_len)
{
  guint32 crc=CRC16_INITIAL_VALUE;
  guint j;

  for ( j = 0;  j < data_len;  j++ )
  {
    crc ^= data[j] << 8;
    crc = (crc << 8) ^ crc16_table[(crc & 0xff00) >> 8];
  }
  crc ^= 0xFFFF;        /* Invert the output. */
  crc &= 0xFFFF;
  return crc;
}

/*

  guint8 wimax_mac_calc_crc8(guint8 *data, guint data_len)

  REQUIRES: wimax_mac_gen_crc8_table() must be called before

  DESCRIPTION: Calculate the 8-bit CRC from a given data block

  ARGS:  data - pointer to data
         data_len - length of data (in bytes)

  RETURNS:  calculated crc8

  SIDE EFFECTS:

*/
guint8 wimax_mac_calc_crc8(const guint8 *data, guint data_len)
{
  guint8 crc=0;
  guint i;

  for(i = 0; i < data_len; i++)
  {
    crc = crc8_table[data[i]^crc];
  }
  return crc;
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local Variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=2:tabSize=8:noTabs=true:
 */