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