|
|
@ -28,63 +28,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "lte/utils/pack.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define _WITHMALLOC
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int cword;
|
|
|
|
unsigned int cword;
|
|
|
|
|
|
|
|
char *cdata;
|
|
|
|
|
|
|
|
const unsigned long crcinit = 0x00000000; //initial CRC value
|
|
|
|
|
|
|
|
const unsigned long crcxor = 0x00000000; //final XOR value
|
|
|
|
|
|
|
|
unsigned long crcmask;
|
|
|
|
|
|
|
|
unsigned long crchighbit;
|
|
|
|
|
|
|
|
unsigned long crctab[256];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void gen_crc_table(int lorder, unsigned long poly) {
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int icrc1(unsigned int crc, unsigned short onech,int long_crc,
|
|
|
|
int i, j, ord=(lorder-8);
|
|
|
|
int left_shift,unsigned int poly)
|
|
|
|
unsigned long bit, crc;
|
|
|
|
{
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
unsigned int tmp=(unsigned int) (crc ^ (onech << (long_crc >> 1) ));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i=0;i<left_shift;i++) {
|
|
|
|
for (i=0; i<256; i++) {
|
|
|
|
if (tmp & (0x1<<(long_crc-1)))
|
|
|
|
crc = ((unsigned long)i)<<ord;
|
|
|
|
tmp=(tmp<<1)^poly;
|
|
|
|
for (j=0; j<8; j++) {
|
|
|
|
else
|
|
|
|
bit = crc & crchighbit;
|
|
|
|
tmp <<= 1;
|
|
|
|
crc<<= 1;
|
|
|
|
|
|
|
|
if (bit) crc^= poly;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
crctab[i] = crc & crcmask;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long crctable (unsigned char* data, unsigned long length, int lorder) {
|
|
|
|
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
// Polynom lorders of 8, 16, 24 or 32.
|
|
|
|
|
|
|
|
unsigned long crc = crcinit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (length--) crc = (crc << 8) ^ crctab[ ((crc >> (lorder-8)) & 0xff) ^ *data++];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return((crc^crcxor)&crcmask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int crc(unsigned int crc, char *bufptr, int len,
|
|
|
|
unsigned long reversecrcbit(unsigned int crc, unsigned int polynom, int lorder, int nbits) {
|
|
|
|
int long_crc,unsigned int poly, int paste_word) {
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long m, rmask=0x1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(m=0; m<nbits; m++){
|
|
|
|
|
|
|
|
if((rmask & crc) == 0x01 )crc = (crc ^ polynom)>>1;
|
|
|
|
|
|
|
|
else crc = crc >> 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return((crc^crcxor)&crcmask);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int i,k;
|
|
|
|
|
|
|
|
unsigned int data;
|
|
|
|
|
|
|
|
int stop;
|
|
|
|
|
|
|
|
unsigned int ret;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cword=crc;
|
|
|
|
int init_crc(int lorder, unsigned long polynom){
|
|
|
|
|
|
|
|
|
|
|
|
k=0;
|
|
|
|
unsigned long polyhighbit;
|
|
|
|
stop=0;
|
|
|
|
|
|
|
|
while(!stop) {
|
|
|
|
// Compute bit masks for whole CRC and CRC high bit
|
|
|
|
data=0;
|
|
|
|
crcmask = ((((unsigned long)1<<(lorder-1))-1)<<1)|1;
|
|
|
|
for (i=0;i<long_crc/2;i++) {
|
|
|
|
polyhighbit=0xFFFFFFFF ^ (crcmask+1);
|
|
|
|
if (bufptr[k] && k<len)
|
|
|
|
crchighbit = (unsigned long)1<<(lorder-1);
|
|
|
|
data|=(0x1<<(long_crc/2-1-i));
|
|
|
|
|
|
|
|
k++;
|
|
|
|
// Eliminate highest bit in polynom word
|
|
|
|
if (k==len) {
|
|
|
|
polynom=polynom & polyhighbit;
|
|
|
|
stop=1;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
// check parameters
|
|
|
|
break;
|
|
|
|
if (lorder < 1 || lorder > 32) {
|
|
|
|
|
|
|
|
printf("ERROR, invalid order, it must be between 1..32.\n");
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lorder%8 != 0) {
|
|
|
|
|
|
|
|
printf("ERROR, invalid order=%d, it must be 8, 16, 24 or 32.\n", lorder);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (polynom != (polynom & crcmask)) {
|
|
|
|
|
|
|
|
printf("ERROR, invalid polynom.\n");
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crcinit != (crcinit & crcmask)) {
|
|
|
|
|
|
|
|
printf("ERROR, invalid crcinit.\n");
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crcxor != (crcxor & crcmask)) {
|
|
|
|
|
|
|
|
printf("ERROR, invalid crcxor.\n");
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate lookup table
|
|
|
|
|
|
|
|
gen_crc_table(lorder, polynom);
|
|
|
|
|
|
|
|
|
|
|
|
cword=(unsigned int) (icrc1((unsigned int) (cword<<long_crc>>long_crc),
|
|
|
|
return(1);
|
|
|
|
data,long_crc,i,poly)<<long_crc)>>long_crc;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ret=cword;
|
|
|
|
#define MAX_LENGTH 8192
|
|
|
|
if (paste_word) {
|
|
|
|
|
|
|
|
cword<<=32-long_crc;
|
|
|
|
unsigned int crc(unsigned int crc, char *bufptr, int len,
|
|
|
|
for (i=0;i<long_crc;i++) {
|
|
|
|
int long_crc, unsigned int poly, int paste_word) {
|
|
|
|
bufptr[i+len]=((cword&(0x1<<31))>>31);
|
|
|
|
|
|
|
|
cword<<=1;
|
|
|
|
int i, len8, res8, a;
|
|
|
|
|
|
|
|
#ifdef _WITHMALLOC
|
|
|
|
|
|
|
|
char *data0, *pter;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data0 = (char *)malloc(sizeof(char) * (len+long_crc)*2);
|
|
|
|
|
|
|
|
if (!data0) {
|
|
|
|
|
|
|
|
perror("malloc ERROR: Allocating memory for data pointer in crc() function");
|
|
|
|
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WITHMALLOC
|
|
|
|
|
|
|
|
char data0[MAX_LENGTH], *pter;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if((((len+long_crc)>>3) + 1) > MAX_LENGTH){
|
|
|
|
|
|
|
|
printf("ERROR: Not enough memory allocated\n");
|
|
|
|
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (ret);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//# Pack bits into bytes
|
|
|
|
|
|
|
|
len8=(len>>3);
|
|
|
|
|
|
|
|
res8=8-(len - (len8<<3));
|
|
|
|
|
|
|
|
if(res8>0)a=1;
|
|
|
|
|
|
|
|
else a=0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Zeroed additional bits
|
|
|
|
|
|
|
|
memset((char *)(bufptr+len),0,(32)*sizeof(char));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(i=0; i<len8+a; i++){
|
|
|
|
|
|
|
|
pter=(char *)(bufptr+8*i);
|
|
|
|
|
|
|
|
data0[i]=(char)(unpack_bits(&pter, 8)&0xFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate CRC
|
|
|
|
|
|
|
|
pter=data0;
|
|
|
|
|
|
|
|
crc=crctable ((unsigned char *)pter, len8+a, long_crc);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Reverse CRC res8 positions
|
|
|
|
|
|
|
|
if(a==1)crc=reversecrcbit(crc, poly, long_crc, res8);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add CRC
|
|
|
|
|
|
|
|
pter=(char *)(bufptr+len);
|
|
|
|
|
|
|
|
pack_bits(crc, &pter, long_crc);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WITHMALLOC
|
|
|
|
|
|
|
|
free(data0);
|
|
|
|
|
|
|
|
data0=NULL;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//Return CRC value
|
|
|
|
|
|
|
|
return crc;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|