CRC32 Implementation

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

CRC32 Implementation

Post by Widgetman » 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;
}
}

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Long hex value

Post by Widgetman » Wed Jan 16, 2008 2:57 pm

Hi,
I am working on converting this CRC32 Algorithm into the Swordfish structure. I ran into a snag where it will not take a Longint Value as a 4 bytes. How do I handle this ?
Thanks


Poly = $EDB88320

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Array Index Issue

Post by Widgetman » Wed Jan 16, 2008 3:07 pm

Hi All,
I am also getting a While expected and End expected error with this sub routine I created. If I comment out the CRCTable line the errors go away. Any ideas ?

Thanks


Dim CRCTable(256) As LongInt

Dim I As Integer
Dim CRC As LongInt
Dim Poly As LongInt
Dim J As Integer

Sub CRCGEN()

Poly = $EDB88320
For I=0 To 256 Step 1
CRC = I
For J=8 To 0 Step -1

If (CRC And 1) = 1 Then
CRC = (CRC >> 1) Xor Poly
Else
CRC = CRC NOT 1 // CRC >>= 1 Doing a 1's complement ?
EndIf
Next
CRCTable = CRC;
Next
End Sub

User avatar
octal
Registered User
Registered User
Posts: 584
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Wed Jan 16, 2008 3:42 pm

You got that because there are a lot of errors in your sources.

1- Table are accessed with "(..)" brackets instead of Square Brackets "[..]"
so in line

Code: Select all

 CRCTable[I] = CRC; 
You have to change square bracket to brackets and remove the ";" at the end of line.

Code: Select all

 CRCTable(I) = CRC

for line

Code: Select all

CRC = CRC NOT 1 
I do not really inderstand what you want to do ???? :roll: but there is an error because NOT is a unary operator and not binary operator.

and finally for line

Code: Select all

 Poly = $EDB88320 
Poly is a Long Int .... and
$EDB88320 = "3 988 292 384" in decimal base.

from SF help file
TYPE SIZE INTERVAL OF VALUES
LongInt | 32 | -2147483648 to 2147483647
$EDB88320 = "3 988 292 384" is bigger than "214 748 3647" (even in negative values rollup) ... so you have an out of limit (bounds) for the variable ;)

Regards
octal

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Post by Widgetman » Wed Jan 16, 2008 3:59 pm

Hi Octal
Thanks for the help. I am trying to convert this algorithm from the C Code I posted above and missed a few syntax errors as you pointed out. I got the code to work now except for the long Hex value Poly
Is there no way to represent a large number in Swordfish ?
If not I may not be able to get this CRC32 routine to work.
Thanks

Sub CRCGEN()


Poly = $EDB88320
For I=0 To 256 Step 1
CRC = I
For J=8 To 0 Step -1
If (CRC And 1) = 1 Then
CRC = (CRC >> 1) Xor Poly
Else
CRC = CRC >> 1
EndIf
Next
CRCTable(I) = CRC
Next
End Sub

User avatar
octal
Registered User
Registered User
Posts: 584
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Wed Jan 16, 2008 4:37 pm

Widgetman wrote: ...now except for the long Hex value Poly
Is there no way to represent a large number in Swordfish ?
If not I may not be able to get this CRC32 routine to work.
May be if you post original code in C here we can help you.
If you do not want to post it, at least can you tell us the "C" declaration (type) of the variable that holds the big variable ?

is it Int, Long ??? or Unsigned Long (ulong)?


Regards
ocal

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Code Is Here

Post by Widgetman » Wed Jan 16, 2008 5:45 pm

Hi octal,
The C code is posted in the first message here. Take a look above
Thanks

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Wed Jan 16, 2008 6:29 pm

Try using a LongWord rather than a LongInt - this should give you the 4-byte unsigned word you are after.

Steve

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Post by Widgetman » Wed Jan 16, 2008 6:55 pm

Thanks Steven I figured that out. Man does this pig eat my RAM though. I am gona have to bump up to the next size part at this rate. Thanks for all the help everyone.

Post Reply