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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2016 Edouard Griffiths, F4EXB. //
// //
// Thanks to Sven Reifegerste: http://www.zorc.breitbandkatze.de/crc.html //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include "crc.h"
namespace DSDcc
{
const unsigned long CRC::PolyCCITT16 = 0x1021;
const unsigned long CRC::PolyDStar16 = 0x8408;
CRC::CRC(unsigned long polynomial,
int order,
unsigned long crcinit,
unsigned long crcxor,
int direct,
int refin,
int refout) :
m_order(order),
m_poly(polynomial),
m_direct(direct),
m_crcinit(crcinit),
m_crcxor(crcxor),
m_refin(refin),
m_refout(refout)
{
m_crcmask = ((((unsigned long) 1 << (m_order - 1)) - 1) << 1) | 1;
m_crchighbit = (unsigned long) 1 << (m_order - 1);
generate_crc_table();
init();
}
CRC::~CRC()
{
}
unsigned long CRC::reflect(unsigned long crc, int bitnum)
{
// reflects the lower 'bitnum' bits of 'crc'
unsigned long i, j = 1, crcout = 0;
for (i = (unsigned long) 1 << (bitnum - 1); i; i >>= 1)
{
if (crc & i)
crcout |= j;
j <<= 1;
}
return (crcout);
}
void CRC::generate_crc_table()
{
// make CRC lookup table used by table algorithms
int i, j;
unsigned long bit, crc;
for (i = 0; i < 256; i++)
{
crc = (unsigned long) i;
if (m_refin)
crc = reflect(crc, 8);
crc <<= m_order - 8;
for (j = 0; j < 8; j++)
{
bit = crc & m_crchighbit;
crc <<= 1;
if (bit)
crc ^= m_poly;
}
if (m_refin)
crc = reflect(crc, m_order);
crc &= m_crcmask;
m_crctab[i] = crc;
}
}
void CRC::init()
{
unsigned int i;
unsigned long bit, crc;
if (!m_direct)
{
m_crcinit_nondirect = m_crcinit;
crc = m_crcinit;
for (i = 0; i < m_order; i++)
{
bit = crc & m_crchighbit;
crc <<= 1;
if (bit)
crc ^= m_poly;
}
crc &= m_crcmask;
m_crcinit_direct = crc;
}
else
{
m_crcinit_direct = m_crcinit;
crc = m_crcinit;
for (i = 0; i < m_order; i++)
{
bit = crc & 1;
if (bit)
crc ^= m_poly;
crc >>= 1;
if (bit)
crc |= m_crchighbit;
}
m_crcinit_nondirect = crc;
}
}
unsigned long CRC::crctablefast(unsigned char* p, unsigned long len)
{
// fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
// only usable with polynom orders of 8, 16, 24 or 32.
unsigned long crc = m_crcinit_direct;
if (m_refin)
crc = reflect(crc, m_order);
if (!m_refin)
while (len--)
crc = (crc << 8) ^ m_crctab[((crc >> (m_order - 8)) & 0xff) ^ *p++];
else
while (len--)
crc = (crc >> 8) ^ m_crctab[(crc & 0xff) ^ *p++];
if (m_refout ^ m_refin)
crc = reflect(crc, m_order);
crc ^= m_crcxor;
crc &= m_crcmask;
return (crc);
}
unsigned long CRC::crctable(unsigned char* p, unsigned long len)
{
// normal lookup table algorithm with augmented zero bytes.
// only usable with polynom orders of 8, 16, 24 or 32.
unsigned long crc = m_crcinit_nondirect;
if (m_refin)
crc = reflect(crc, m_order);
if (!m_refin)
while (len--)
crc = ((crc << 8) | *p++) ^ m_crctab[(crc >> (m_order - 8)) & 0xff];
else
while (len--)
crc = ((crc >> 8) | (*p++ << (m_order - 8))) ^ m_crctab[crc & 0xff];
if (!m_refin)
while (++len < m_order / 8)
crc = (crc << 8) ^ m_crctab[(crc >> (m_order - 8)) & 0xff];
else
while (++len < m_order / 8)
crc = (crc >> 8) ^ m_crctab[crc & 0xff];
if (m_refout ^ m_refin)
crc = reflect(crc, m_order);
crc ^= m_crcxor;
crc &= m_crcmask;
return (crc);
}
unsigned long CRC::crcbitbybit(unsigned char* p, unsigned long len)
{
// bit by bit algorithm with augmented zero bytes.
// does not use lookup table, suited for polynom orders between 1...32.
unsigned long i, j, c, bit;
unsigned long crc = m_crcinit_nondirect;
for (i = 0; i < len; i++)
{
c = (unsigned long) *p++;
if (m_refin)
c = reflect(c, 8);
for (j = 0x80; j; j >>= 1)
{
bit = crc & m_crchighbit;
crc <<= 1;
if (c & j)
crc |= 1;
if (bit)
crc ^= m_poly;
}
}
for (i = 0; i < m_order; i++)
{
bit = crc & m_crchighbit;
crc <<= 1;
if (bit)
crc ^= m_poly;
}
if (m_refout)
crc = reflect(crc, m_order);
crc ^= m_crcxor;
crc &= m_crcmask;
return (crc);
}
unsigned long CRC::crcbitbybitfast(unsigned char* p, unsigned long len)
{
// fast bit by bit algorithm without augmented zero bytes.
// does not use lookup table, suited for polynom orders between 1...32.
unsigned long i, j, c, bit;
unsigned long crc = m_crcinit_direct;
for (i = 0; i < len; i++)
{
c = (unsigned long) *p++;
if (m_refin)
c = reflect(c, 8);
for (j = 0x80; j; j >>= 1)
{
bit = crc & m_crchighbit;
crc <<= 1;
if (c & j)
bit ^= m_crchighbit;
if (bit)
crc ^= m_poly;
}
}
if (m_refout)
crc = reflect(crc, m_order);
crc ^= m_crcxor;
crc &= m_crcmask;
return (crc);
}
// ====================================================================
DStarCRC::DStarCRC() : crc(0)
{}
DStarCRC::~DStarCRC()
{}
void DStarCRC::fcsbit(unsigned char tbyte)
{
crc ^= tbyte;
if (crc & 1)
{
crc = (crc >> 1) ^ 0x8408; // X-modem CRC poly
}
else
{
crc = crc >> 1;
}
}
void DStarCRC::compute_crc(unsigned char *array, int size_buffer)
{
crc = 0xffff;
for (int n = 0; n < (size_buffer - 2); n++)
{
for (int m = 0; m < 8; m++)
{ //each bit must be calculated separately
fcsbit(bitRead(array[n], m));
}
}
crc ^= 0xffff;
}
bool DStarCRC::check_crc(unsigned char *array, int size_buffer)
{
compute_crc(array, size_buffer);
unsigned int crc_decoded = (array[size_buffer - 1] << 8) + array[size_buffer - 2]; //inversion msb lsb
if (crc_decoded == crc)
{
return true;
}
else
{
return false;
}
}
bool DStarCRC::check_crc(unsigned char *array, int size_buffer, unsigned int crcValue)
{
compute_crc(array, size_buffer+2); // size given is buffer size without CRC
if (crcValue == crc)
{
return true;
}
else
{
return false;
}
}
} // namespace DSDcc
|