By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,564 Members | 810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,564 IT Pros & Developers. It's quick & easy.

Whats wrong with this crc32 code?

P: n/a
I need to write a function crc(msg, len) that gets a char array of
length len and then calculates the crc32 for the code. I don't
understand what's going wrong in the code I have. It goes bit-by-bit,
XORing the most significant bit of the FCS with the msb of the
character.

All comments are welcome. Yes, this is for a school assignment. If I
wanted to just grab some googled code I would; I want to actually
understand what I am doing wrong.

Thanks in advance.

Here's what I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXFILESIZE 512
#define CRCDIV 0x04C11DB7

/* prototypes. */
unsigned int crc(unsigned char msg[], int len);
/* this function creates a binary representation of any integer. */
void d2b(char buff[], int i);

int main(int argc, char *argv[])
{
unsigned char msg[MAXFILESIZE];
FILE *infile;
unsigned int r;
int len = 0;
unsigned int c;
char buff[33];

/* make sure we get a file. */
if (argc != 2) {
printf("USAGE: %s file-to-encode.txt\n", argv[0]);
exit(1);
}

/* now try to open the file. */
if ((infile = fopen(argv[1], "r")) == NULL) {
perror("fopen failed");
exit(1);
}

/* load infile into msg. */
while ( (c = fgetc(infile)) != EOF) {
msg[len++] = c;
}

/* print CRCDIV just to see what it looks like. */
d2b(buff, CRCDIV);

#ifdef DEBUG
printf("CRCDIV: %s\n\n", buff);
#endif

/* call the crc and print the results. */
r = crc(msg, len);
printf("crc32 for msg: %08x.\n", r);

return 0;
}

unsigned int crc(unsigned char msg[], int len)
{
unsigned char c;
unsigned int FCS;
unsigned int y;
int i, j;
char buff[33];

FCS = 0;

/* outer loop through each unsigned char in msg. */
for (i=0; i<len; i++) {
c = msg[i];

#ifdef DEBUG
printf("c is %c:%d.\n", c, c);
#endif

/* now loop bit-by-bit. */
for (j=0; j<8; j++) {
d2b(buff, FCS);

#ifdef DEBUG
printf("before calculating y:\n");
printf("FCS: %x\n", FCS);
printf("%s\n", buff);
#endif

/* Calculate y. */
y = (FCS >> 31) ^ (c >> 7);
FCS <<= 1;
if (y)
FCS = FCS ^ CRCDIV;
c <<= 1;
d2b(buff, FCS);
#ifdef DEBUG
printf("FCS >> 31: %d; c >> 7: %d; y: %d.\n\n",
(FCS >> 31), (c >> 7), y);

printf("after calculating y:\n");
printf("FCS: %x\n", FCS);
printf("%s\n", buff);
#endif

}

#ifdef DEBUG
printf("\n");
#endif

}
return FCS;
}

void d2b(char buff[], int i)
{
int q, r, index;

for (index=0; index<32; index++)
buff[index] = '0';
buff[32] = '\0';

index = 31;
while (i > 0) {
r = i % 2;
q = i / 2;
if (r)
buff[index] = '1';
index--;
i = q;
}
}
--
My public key:
gpg --recv-keys --keyserver www.mandrakesecure.net 0x8D10BFD5
Nov 14 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Mon, 16 Feb 2004 17:04:10 GMT, Matthew Wilson
<ma**@overlook.homelinux.net> wrote in comp.lang.c:
I need to write a function crc(msg, len) that gets a char array of
length len and then calculates the crc32 for the code. I don't
understand what's going wrong in the code I have. It goes bit-by-bit,
XORing the most significant bit of the FCS with the msb of the
character.

All comments are welcome. Yes, this is for a school assignment. If I
wanted to just grab some googled code I would; I want to actually
understand what I am doing wrong.

Thanks in advance.

Here's what I have so far:


[snip code]

No, sorry, I'm just not going to wade through all that code without
any idea of what you think is "going wrong". And I've written a few
CRC implementations in my time, even one published in a book on C
programming a few years back.

At the very least you need to supply more information, specifically
what you mean by "going wrong".

If you get compiler errors (or linker errors), copy the text and paste
it into the body of your message along with the source code. If the
program builds but crashes when run, describe the symptoms of the
crash. If it build and runs but outputs incorrect results, you need
to explain what results you expected and what you got instead.

If you provide that much additional information along with the source
code, you will most likely get some useful advice.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.