⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crc1.cpp

📁 16位CRC的c语言实现。并编写了测试程序
💻 CPP
字号:
/*
 |
 | Copyright disclaimer:
 |   This software was developed at the National Institute of Standards
 |   and Technology by employees of the Federal Government in the course
 |   of their official duties. Pursuant to title 17 Section 105 of the
 |   United States Code this software is not subject to copyright
 |   protection and is in the public domain.
 |
 |   We would appreciate acknowledgement if the software is used.
 |
*/

/*
 | Project:     WCDMA simulation environment
 | Module:      Cyclic rendundance Check routines for different
 |              generator polynomials.
 | Author:      Tommi Makelainen
 | Date:        January 28, 1999
 |
 | History:
 |              January 28, 1999 Tommi Makelainen
 |                      Initial version.
 |
 */
#ifndef crc1_cpp
#define crc1_cpp

#include <stdlib.h> 
#include <stdio.h>
#include <math.h>
#include "crc.h"

static unsigned short crctable[256];
                  
/* ----------------------------------------------------------- */

/* 计算一个字节所有可能值的CRC校验码
 * Init the fast CRC-16 calculation table.
 */
void wcdma_crctable_init (int crc_gen_poly)  /* gen. polynomial as bit mask */
{
    int val, i;
    unsigned int crc;

    /*
     * Calculate crc value for all possible input bytes.
     */
    for (val=0; val <= 255; val++) {
        crc = val << 8;

        for (i=0; i < 8; ++i) {
	    crc <<= 1;
	    if (crc & 0x10000) crc ^= crc_gen_poly;
	}
        crctable[val] = crc;
    }

    /*
     * Check that int data type is a bit long.
     */
    if (sizeof(int) != 4) printf("crc init: size of int is not 32 bits.\n");

} /* wcdma_crctable_init */

/* ----------------------------------------------------------- */

/*
 * Calculate CRC for 'input_data' having 'input_len' words (32-bit ones).
 * 计算数据流的CRC校验码
 * Note: int type must be 32-bit entity.
 */
unsigned short wcdma_calc_crc(int *input_data, int input_len)
{
    unsigned short m,n,k,l,crc_value = 0;
    unsigned char data_byte;
    int i;
//1个length处理4个字节32位,
    while (input_len ) {
        for (i=3; i > -1; i--) {					  //一个for循环计算4个字节的CRC
            data_byte = (*input_data >> (8*i)) & 0xff;//确定字节的数值
			m = crc_value<<8;
			n = (crc_value>>8) ^ data_byte;			
			k = crctable[(crc_value>>8) ^ data_byte];
            crc_value = (crc_value<<8) ^ crctable[(crc_value>>8) ^ data_byte];//按字节计算CRC
        }
        *input_data++;
		input_len--;
    }

  return crc_value;
} /* wcdma_calc_crc */


/* ----------------------------------------------------------- */
/*
 * Calculate CRC for 'input_data' having 'input_len' words (32-bit ones).
 * 
 * Note:
 *          int type must be 32-bit entity.
 *          Input data must be multiple of 8 bits (a byte).
 */
unsigned short wcdma_get_crc_value(
          int input_data[],  /* IN: input data bits */
          int input_len,    /* IN: input data length */
          int crc_bits[])   /* OUT: crc bits (MSB first) */
{
    unsigned short crc_value = 0;
    unsigned char data_byte;
    int i, j;

    /*
     * Loop over all input bits.
     */
    for (i=0; i < input_len; i += 8) {
        
        /* input_len表示数据流的bit总数,input_data[]存放的是数据bit
         * Group input bits to a byte and find crc value for the byte.
         */
        data_byte = 0;
		/*
		 *for循环的作用是将input_data[]的数据以8bit为单位逐位送入到data_byte中
		*/
        for (j=0; j < 8; j++) {
            data_byte = (data_byte << 1) & 0xff;	//data_byte左移一位,input_data[]就送入一位bit
            data_byte = data_byte + ((unsigned char)input_data[i+j] & 0x1);
        }
        crc_value = (crc_value<<8) ^ crctable[(crc_value>>8) ^ data_byte];//按字节计算CRC
    }

    /*
     * Break down the result crc value to the output bit vector.
	 * 将所得的CRC翻转一下,为什么翻转呢?
     */
    for (i=0,j=15; i < 16; i++,j--) {
        crc_bits[i] = (crc_value >> j) & 0x1;
    }

    return crc_value;
} /* wcdma_get_crc_value */

#endif


/* ----------------------------------------------------------- */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -