Whats wrong with this crc32 code?

M

Matthew Wilson

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;

#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;
}
}
 
J

Jack Klein

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top