429,191 Members | 2,190 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,191 IT Pros & Developers. It's quick & easy.

# CRC32

 P: n/a Hi NG. Does anyone know of a place where I could download/get a C implementation of a CRC32 check. I would like a simple function that, for example, had a pointer to where the data to be CRC32 calculated reside, an indication of the length of the data and perhaps the polynomium as input arguments and then would return the calculated crc32 value like e.g. an unsigned long. Don Nov 13 '05 #1
14 Replies

 P: n/a On Wed, 30 Jul 2003 14:29:42 +0200 "Don" wrote: Hi NG. Does anyone know of a place where I could download/get a C implementation of a CRC32 check. I would like a simple function that, for example, had a pointer to where the data to be CRC32 calculated reside, an indication of the length of the data and perhaps the polynomium as input arguments and then would return the calculated crc32 value like e.g. an unsigned long. Don Learn the value of search engines! A simple google search provided me with: http://remus.rutgers.edu/~rhoads/Code/crc-32b.c -- main(int c,char*k,char*s){c>0?main(0,"adceoX\$_k6][^hn","-7\ 0#05&'40\$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c ),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;} Nov 13 '05 #2

 P: n/a Pieter Droogendijk wrote in news:20030730144653.6c4cd71d.gi*@binky.homeunix.or g: Does anyone know of a place where I could download/get a C implementation of a CRC32 check. I would like a simple function that, for example, had a pointer to where the data to be CRC32 calculated reside, an indication of the length of the data and perhaps the polynomium as input arguments and then would return the calculated crc32 value like e.g. an unsigned long. Don Learn the value of search engines! A simple google search provided me with: http://remus.rutgers.edu/~rhoads/Code/crc-32b.c I grabbed this and was confused by this function's use of 'int char'.: unsigned long get_crc( FILE *fp) /* calculate the crc value */ { register unsigned long crc; int char; <------------------ NOTE crc = 0xFFFFFFFF; while ((char = getc(fp)) != EOF) crc = ((crc>>8) & 0x00FFFFFF) ^ crc_table[ (crc^char) & 0xFF ]; return( crc^0xFFFFFFFF ); } What's with the NOTE'd line. Could someone really have published a function with this sort of error? -- - Mark -> -- Nov 13 '05 #3

 P: n/a On Wed, 30 Jul 2003, Mark A. Odell wrote: Pieter Droogendijk wrote... http://remus.rutgers.edu/~rhoads/Code/crc-32b.c I grabbed this and was confused by this function's use of 'int char'. unsigned long get_crc( FILE *fp) /* calculate the crc value */ { register unsigned long crc; int char; <------------------ NOTE crc = 0xFFFFFFFF; while ((char = getc(fp)) != EOF) crc = ((crc>>8) & 0x00FFFFFF) ^ crc_table[ (crc^char) & 0xFF ]; return( crc^0xFFFFFFFF ); } What's with the NOTE'd line. Could someone really have published a function with this sort of error? Yes. :) That is absolutely ridiculous. Maybe the OP should look somewhere else for more reliable code, since this was obviously never tested in a real program. The lines #ifndef FILE /* if FILE type not defined */ #include /* then bring it in */ #endif should also have raised several red flags. I'm going to mail the guy right away and point it out. http://www.google.com/search?q=crc32+c+source&btnI=I'mFeelingLucky -Arthur Nov 13 '05 #4

 P: n/a "Arthur J. O'Dwyer" wrote in message news:Pi***********************************@unix44. andrew.cmu.edu... On Wed, 30 Jul 2003, Mark A. Odell wrote: Pieter Droogendijk wrote... http://remus.rutgers.edu/~rhoads/Code/crc-32b.c I grabbed this and was confused by this function's use of 'int char'. unsigned long get_crc( FILE *fp) /* calculate the crc value */ { register unsigned long crc; int char; <------------------ NOTE crc = 0xFFFFFFFF; while ((char = getc(fp)) != EOF) crc = ((crc>>8) & 0x00FFFFFF) ^ crc_table[ (crc^char) & 0xFF ]; return( crc^0xFFFFFFFF ); } What's with the NOTE'd line. Could someone really have published a function with this sort of error? Yes. :) That is absolutely ridiculous. Maybe the OP should look somewhere else for more reliable code, since this was obviously never tested in a real program. The lines #ifndef FILE /* if FILE type not defined */ #include /* then bring it in */ #endif should also have raised several red flags. I'm going to mail the guy right away and point it out. http://www.google.com/search?q=crc32+c+source&btnI=I'mFeelingLucky -Arthur Hi Arthur. Thank you for the reply (and the rest of you too). I am a little puzzled by the code presented by the link you gave me. uLong crc32(uLong crc, char const *buf, size_t len) { if (crc_table[255] == 0) make_crc_table(); crc ^= 0xffffffff; while (len--) crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; return crc ^ 0xffffffff; } Why does the function take a uLong crc as argument, change it and return the same......would this cause the function to update the very ulong as I am calling the function with? For example if I called the function like: void myfunc() { unsigned long test; crc32(test,........); } then my ulong test would hold the crc32 value? Also why are the buf pointer constant.....? The pointer points to chars, could I just easily change this to uint's instead (uLong crc32(uLong crc, unsigned int const *buf, size_t len)) if I had an array of uint that I needed to calculate the crc32 from. Final question :-), what is the "size_t" type????? Don Nov 13 '05 #5

 P: n/a "Arthur J. O'Dwyer" wrote in message news:... On Wed, 30 Jul 2003, Mark A. Odell wrote: Pieter Droogendijk wrote... http://remus.rutgers.edu/~rhoads/Code/crc-32b.c I grabbed this and was confused by this function's use of 'int char'. unsigned long get_crc( FILE *fp) /* calculate the crc value */ { register unsigned long crc; int char; <------------------ NOTE crc = 0xFFFFFFFF; while ((char = getc(fp)) != EOF) crc = ((crc>>8) & 0x00FFFFFF) ^ crc_table[ (crc^char) & 0xFF ]; return( crc^0xFFFFFFFF ); } What's with the NOTE'd line. Could someone really have published a function with this sort of error? Yes. :) That is absolutely ridiculous. Maybe the OP should look somewhere else for more reliable code, since this was obviously never tested in a real program. The lines #ifndef FILE /* if FILE type not defined */ #include /* then bring it in */ #endif should also have raised several red flags. I'm going to mail the guy right away and point it out. http://www.google.com/search?q=crc32+c+source&btnI=I'mFeelingLucky -Arthur From my email response: The line "int char" is *not* a syntax error! In fact there is a very good reason for using a variable of type "int" instead of "char." The function "getc" can return any value from the character set *plus* the special non-character value EOF. According to the C standard, the type "char" is guaranteed to contain enough bits to hold all of the values corresponding to the machine's character set; it is *not* guaranteed to be able to hold the additional EOF value (without possibly mixing up EOF with some actual character value). Thus, to ensure that the code works across all implementations, you need to declare the variable as an "int" instead of a "char." For portability reasons, it is good practice to always declare character data as type "int" except when you are declaring an array of characters. (Also, the lines #ifndef FILE #include #endif are suspicious; the #ifndef and #endif directives are completely useless and irrelevant there.) I disagree with this claim too. There are two distinct methods of making use of this crc code as part of a system library. First, you may want to compile the crc code separately to generate an object file only and then make the object file available as part of your library while hiding the actual source code. Alternatively, you may want to make the actual source code available and have the user compile their source code and the crc source code together at once. To use the first method, you need to include "stdio.h" so the compiler will not complain about the type "FILE." But if include "stdio.h" without the preprocessor directives #ifndef and #endif, then you will run into a problem using the second method if your own source code file also includes "stdio.h." Since I cannot know in advance which method someone might use, I used the above method to let you to use either method without running into a problem. Having the #ifndef and #endif directives will not hurt you if your preferred method does not need them. Nov 13 '05 #7

 P: n/a "Don" wrote in news:bg**********@news.net.uni-c.dk: Ok. Thank you all for your help. I need to implement this crc32 check in a microcontroller and I have a program now that states: #define POLYNOMIAL (unsigned long)0x04c11db7 What possible (practical) benefit is there to making this a #define instead of a const var (e.g. const unsigned long polynomial = 0x04c11db7)? Try finding the #define in your debugger by name. -- - Mark -> -- Nov 13 '05 #10

 P: n/a In "Mark A. Odell" writes: "Don" wrote in news:bg**********@news.net.uni-c.dk: Ok. Thank you all for your help. I need to implement this crc32 check in a microcontroller and I have a program now that states: #define POLYNOMIAL (unsigned long)0x04c11db7What possible (practical) benefit is there to making this a #defineinstead of a const var (e.g. const unsigned long polynomial = 0x04c11db7)? You have forgotten static. And it's still better to spell it in upper case, to point out that it isn't an ordinary variable. Try finding the #define in your debugger by name. Why would one want to do such a thing? You have the source code in front of your eyes while using the debugger, don't you? Dan -- Dan Pop DESY Zeuthen, RZ group Email: Da*****@ifh.de Nov 13 '05 #11

 P: n/a In article , no**@spam.dk says... I would like to cross-check this with some program or similar. Does anyone know of a program similar to: http://www.smbus.org/faq/crc8Applet.htm Making me capable of calculating the crc32 (instead of only crc8) of a hexadecimal string? Don Btw, if you want a very efficient implementation, do a google search for "Hacker's Delight". The website for this (very interesting) book has an implementation of crc32 that is very well explained and along with that is half a dozen or more implementations of it, each analyzed for performance. Nov 13 '05 #12