2010-09-15 13:01:56 +08:00
|
|
|
/*
|
2013-08-20 07:40:02 +08:00
|
|
|
* Copyright 2013 Nick Foster
|
2010-09-15 13:01:56 +08:00
|
|
|
*
|
2013-08-20 07:40:02 +08:00
|
|
|
* This file is part of gr-air-modes
|
2010-09-15 13:01:56 +08:00
|
|
|
*
|
2013-08-20 07:40:02 +08:00
|
|
|
* gr-air-modes is free software; you can redistribute it and/or modify
|
2010-09-15 13:01:56 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
2013-08-20 07:40:02 +08:00
|
|
|
* gr-air-modes is distributed in the hope that it will be useful,
|
2010-09-15 13:01:56 +08:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2013-08-20 07:40:02 +08:00
|
|
|
* along with gr-air-modes; see the file COPYING. If not, write to
|
2010-09-15 13:01:56 +08:00
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-08-06 07:26:22 +08:00
|
|
|
#include <gr_air_modes/types.h>
|
|
|
|
#include <gr_air_modes/modes_crc.h>
|
2010-09-15 13:01:56 +08:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-08-20 07:40:02 +08:00
|
|
|
unsigned int crc_table[256];
|
|
|
|
const unsigned int POLY=0xFFF409;
|
|
|
|
|
|
|
|
//generate a bytewise lookup CRC table
|
|
|
|
|
|
|
|
void generate_crc_table(void)
|
2010-09-15 13:01:56 +08:00
|
|
|
{
|
2013-08-20 07:40:02 +08:00
|
|
|
unsigned int crc = 0;
|
|
|
|
for(int n=0; n<256; n++) {
|
|
|
|
crc = n<<16;
|
|
|
|
for(int k=0; k<8; k++) {
|
|
|
|
if(crc & 0x800000) {
|
|
|
|
crc = ((crc<<1) ^ POLY) & 0xFFFFFF;
|
|
|
|
} else {
|
|
|
|
crc = (crc<<1) & 0xFFFFFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
crc_table[n] = crc & 0xFFFFFF;
|
|
|
|
}
|
|
|
|
}
|
2010-09-15 13:01:56 +08:00
|
|
|
|
2013-08-20 07:40:02 +08:00
|
|
|
//Perform a bytewise CRC check
|
|
|
|
unsigned int modes_check_crc(unsigned char data[], int length)
|
2012-06-13 22:49:22 +08:00
|
|
|
{
|
2013-08-20 07:40:02 +08:00
|
|
|
if(crc_table[1] != POLY) generate_crc_table();
|
|
|
|
unsigned int crc=0;
|
|
|
|
for(int i=0; i<length; i++) {
|
|
|
|
crc = crc_table[((crc>>16) ^ data[i]) & 0xff] ^ (crc << 8);
|
|
|
|
}
|
|
|
|
return crc & 0xFFFFFF;
|
2010-09-15 13:01:56 +08:00
|
|
|
}
|