CRC32 Implementation
Moderators: David Barker, Jerry Messina
CRC32 Implementation
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;
}
}
Long hex value
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
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
Array Index Issue
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
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
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
You have to change square bracket to brackets and remove the ";" at the end of line.
for line
I do not really inderstand what you want to do ???? but there is an error because NOT is a unary operator and not binary operator.
and finally for line
Poly is a Long Int .... and
$EDB88320 = "3 988 292 384" in decimal base.
from SF help file
Regards
octal
1- Table are accessed with "(..)" brackets instead of Square Brackets "[..]"
so in line
Code: Select all
CRCTable[I] = CRC;
Code: Select all
CRCTable(I) = CRC
for line
Code: Select all
CRC = CRC NOT 1
and finally for line
Code: Select all
Poly = $EDB88320
$EDB88320 = "3 988 292 384" in decimal base.
from SF help file
$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 variableTYPE SIZE INTERVAL OF VALUES
LongInt | 32 | -2147483648 to 2147483647
Regards
octal
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
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
May be if you post original code in C here we can help you.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.
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
Code Is Here
Hi octal,
The C code is posted in the first message here. Take a look above
Thanks
The C code is posted in the first message here. Take a look above
Thanks