Hi All,
I have been trying to work with CRC. I have searched the boards for some of the CRC-related discussion and I have also gone over this link. http://www.ross.net/crc/download/crc_v3.txt. I found a piece of code that implements CRC at http://www.google.com/url?sa=t&sourc...kTe77z5o8R6z2A and I am trying to adapt it.
So, this is what I know so far :
1. Grab a buffer, generate its CRC
2. Append the CRC to the buffer to generate a new string
3. Generate the CRC on this new string and it should be zero.
I have made very minor changes to that piece of code so far and this is what I have now :
So, I am trying to print out the CRC value for the original string and the appended string. But, the CRC generated for the second string is not 0. I am planning on digging deeper into the CRC code to see what could be going wrong. But, I just thought of posting this code here to see if someone can offer any insights.Code://----- Include files --------------------------------------------------------- #include <stdio.h> // Needed for printf() #include <stdlib.h> // Needed for rand() #include <string.h> //----- Type defines ---------------------------------------------------------- typedef unsigned char byte; // Byte is a char typedef unsigned short int word16; // 16-bit word is a short int typedef unsigned int word32; // 32-bit word is an int //----- Defines --------------------------------------------------------------- #define POLYNOMIAL 0x04c11db7L // Standard CRC-32 ppolynomial #define BUFFER_LEN 4096 // Length of buffer //----- Gloabl variables ------------------------------------------------------ static word32 crc_table[256]; // Table of 8-bit remainders //----- Prototypes ------------------------------------------------------------ void gen_crc_table(void); word32 update_crc(word32 crc_accum, byte *data_blk_ptr, word32 data_blk_size); //===== Main program ========================================================== int main() { byte *buff; // Buffer of packet bytes word32 crc32; // 32-bit CRC value word16 i; // Loop counter (16 bit) word32 j; // Loop counter (32 bit) char *crc_hex_string, *crc_bin_string; crc_hex_string = malloc(sizeof(char)*1024); buff = malloc(sizeof(char)*BUFFER_LEN); // Initialize the CRC table gen_crc_table(); // Load buffer with BUFFER_LEN random bytes for (i=0; i<BUFFER_LEN; i++) buff[i] = (byte) rand(); // Compute and output CRC printf("size of orig buffer = %d \n",strlen(buff)); crc32 = update_crc(-1, buff, BUFFER_LEN); sprintf(crc_hex_string,"%08X",crc32); printf("CRC = %08X string = %s\n", crc32,crc_hex_string); strcat(buff, crc_hex_string); printf("size of new buffer = %d, size of crc buffer =%d \n",strlen(buff),sizeof(crc_hex_string)); crc32 = update_crc(-1, buff, BUFFER_LEN + strlen(crc_hex_string)); printf("CRC = %08X \n", crc32); return 0; } //============================================================================= //= CRC32 table initialization = //============================================================================= void gen_crc_table(void) { register word16 i, j; register word32 crc_accum; for (i=0; i<256; i++) { crc_accum = ( (word32) i << 24 ); for ( j = 0; j < 8; j++ ) { if ( crc_accum & 0x80000000L ) crc_accum = (crc_accum << 1) ^ POLYNOMIAL; else crc_accum = (crc_accum << 1); } crc_table[i] = crc_accum; } } //============================================================================= //= CRC32 generation = //============================================================================= word32 update_crc(word32 crc_accum, byte *data_blk_ptr, word32 data_blk_size) { register word32 i, j; for (j=0; j<data_blk_size; j++) { i = ((int) (crc_accum >> 24) ^ *data_blk_ptr++) & 0xFF; crc_accum = (crc_accum << 8) ^ crc_table[i]; } crc_accum = ~crc_accum; return crc_accum; }
Thanks!



LinkBack URL
About LinkBacks


