CRC32 Implementation
Posted: Tue Jan 15, 2008 7:06 pm
Hi All,
I was wondering if someone could give me some ideas as to how to implement this CRC 32 algorithm in Swordfish. I am trying to write a Z-Modem transfer protocol and CRC 32 is a big part of doing it. I also have to get the file name and length from my SD-Card, but I think I can do that from the SD-Card library available. Any suggestions or help would be greatly appreciated.
Thanks
#include <stdio.h>
int main();
unsigned long getcrc();
void crcgen();
unsigned long crcTable[256];
/****************************************************************************/
int main( argc, argv )
int argc;
char *argv[];
{
int i;
FILE *fp;
unsigned long crc;
crcgen();
if (argc < 2) {
crc = getcrc( stdin );
printf("crc32 = %08lx for <stdin>\n", crc);
} else {
for (i=1; i<argc; i++) {
if ( (fp=fopen(argv,"rb")) == NULL ) {
printf("error opening file \"%s\"!\n",argv);
} else {
crc = getcrc( fp );
printf("crc32 = %08lx for \"%s\"\n",
crc, argv);
fclose( fp );
}
}
}
return( 0 );
}
/****************************************************************************/
unsigned long getcrc( fp )
FILE *fp;
{
register unsigned long crc;
int c;
crc = 0xFFFFFFFF;
while( (c=getc(fp)) != EOF ) {
crc = ((crc>>8) & 0x00FFFFFF) ^ crcTable[ (crc^c) & 0xFF ];
}
return( crc^0xFFFFFFFF );
}
/****************************************************************************/
void crcgen( )
{
unsigned long crc, poly;
int i, j;
poly = 0xEDB88320L;
for (i=0; i<256; i++) {
crc = i;
for (j=8; j>0; j--) {
if (crc&1) {
crc = (crc >> 1) ^ poly;
} else {
crc >>= 1;
}
}
crcTable = crc;
}
}
I was wondering if someone could give me some ideas as to how to implement this CRC 32 algorithm in Swordfish. I am trying to write a Z-Modem transfer protocol and CRC 32 is a big part of doing it. I also have to get the file name and length from my SD-Card, but I think I can do that from the SD-Card library available. Any suggestions or help would be greatly appreciated.
Thanks
#include <stdio.h>
int main();
unsigned long getcrc();
void crcgen();
unsigned long crcTable[256];
/****************************************************************************/
int main( argc, argv )
int argc;
char *argv[];
{
int i;
FILE *fp;
unsigned long crc;
crcgen();
if (argc < 2) {
crc = getcrc( stdin );
printf("crc32 = %08lx for <stdin>\n", crc);
} else {
for (i=1; i<argc; i++) {
if ( (fp=fopen(argv,"rb")) == NULL ) {
printf("error opening file \"%s\"!\n",argv);
} else {
crc = getcrc( fp );
printf("crc32 = %08lx for \"%s\"\n",
crc, argv);
fclose( fp );
}
}
}
return( 0 );
}
/****************************************************************************/
unsigned long getcrc( fp )
FILE *fp;
{
register unsigned long crc;
int c;
crc = 0xFFFFFFFF;
while( (c=getc(fp)) != EOF ) {
crc = ((crc>>8) & 0x00FFFFFF) ^ crcTable[ (crc^c) & 0xFF ];
}
return( crc^0xFFFFFFFF );
}
/****************************************************************************/
void crcgen( )
{
unsigned long crc, poly;
int i, j;
poly = 0xEDB88320L;
for (i=0; i<256; i++) {
crc = i;
for (j=8; j>0; j--) {
if (crc&1) {
crc = (crc >> 1) ^ poly;
} else {
crc >>= 1;
}
}
crcTable = crc;
}
}