📄 crc_routines.c
字号:
/* | | 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. | */#include <stdlib.h> #include <stdio.h>#include <math.h>#include "crc_routines.h"static unsigned short crctable[256]; /* ----------------------------------------------------------- *//* * 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). * * Note: int type must be 32-bit entity. */unsigned short wcdma_calc_crc(int *input_data, int input_len){ unsigned short crc_value = 0; unsigned char data_byte; int i; while (input_len-- > 0) { for (i=3; i > -1; i--) { data_byte = (*input_data >> (8*i)) & 0xff; crc_value = (crc_value<<8) ^ crctable[(crc_value>>8) ^ data_byte]; } *input_data++; } 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) { /* * Group input bits to a byte and find crc value for the byte. */ data_byte = 0; for (j=0; j < 8; j++) { data_byte = (data_byte << 1) & 0xff; data_byte = data_byte + ((unsigned char)input_data[i+j] & 0x1); } crc_value = (crc_value<<8) ^ crctable[(crc_value>>8) ^ data_byte]; } /* * Break down the result crc value to the output bit vector. */ for (i=0,j=15; i < 16; i++,j--) { crc_bits[i] = (crc_value >> j) & 0x1; } return crc_value;} /* wcdma_get_crc_value *//* ----------------------------------------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -