View previous topic :: View next topic |
Author |
Message |
Estas Contributor
Joined: 31 Jan 2004 Posts: 66 Location: Germany
|
Posted: Sun Feb 26, 2006 12:51 pm Post subject: CRC-32 Checksum in a few lines |
|
|
Hi all,
I was wondering if there existed a small routine in which a CRC -Checksum program can be generated.
I found something for VB which seems simple enough:
=============Visual Basic ========================
Public Function CRC32(Str As String) As Long
Dim i As Long
Dim j As Long
Dim nPowers(0 To 7) As Integer
Dim nCRC As Long
Dim nByte As Integer
Dim nBit As Boolean
For i = 0 To 7
nPowers(i) = 2 ^ i
Next 'i
For i = 1 To Len(Str)
nByte = Asc(Mid$(Str, i, 1))
For j = 7 To 0 Step -1
nBit = CBool((nCRC And 32768) = 32768) Xor _
((nByte And nPowers(j)) = nPowers(j))
nCRC = (nCRC And 32767&) * 2&
If nBit Then
nCRC = nCRC Xor &H8005&
End If
Next 'j
Next 'i
CRC32 = nCRC
End Function
======================================
Now i am trying to transfer these lines into VDS, without using any external dlls.
Greetings, Mike
_________________ Greetings to all the folks back home in the States. A friendly "hola -como estas" to all my friends from Spain and Greece. |
|
Back to top |
|
|
jrmarquart Valued Newbie
Joined: 12 Jun 2004 Posts: 28 Location: Boise, ID
|
Posted: Sun Feb 26, 2006 7:38 pm Post subject: |
|
|
I am interested in doing this natively within VDS also (rather than using an external DLL). Here is a link for implementing CRC32 checking on the autohotkey forum:
http://www.autohotkey.com/forum/viewtopic.php?t=4934&highlight=crc32
After having read some of these threads though it sounds as if you loose a lot of the speed if you don't compile it into an external DLL?
|
|
Back to top |
|
|
Estas Contributor
Joined: 31 Jan 2004 Posts: 66 Location: Germany
|
Posted: Mon Feb 27, 2006 6:32 am Post subject: |
|
|
Thanks for the link - very informative.
It's true, as far as speed is concerned, you'd be better off using an external dll, especially when checking large files.
For me it's more a learning aspect, curiosity you might say and a routine I would like to implement in my program. The VB example seems small enough to convert, but since I am not familiar with VisualBasic, I am yet not able to convert it into VDS.
Greetings, Mike
_________________ Greetings to all the folks back home in the States. A friendly "hola -como estas" to all my friends from Spain and Greece. |
|
Back to top |
|
|
Estas Contributor
Joined: 31 Jan 2004 Posts: 66 Location: Germany
|
Posted: Tue Feb 28, 2006 1:12 pm Post subject: |
|
|
Hi all,
For the more experienced users of VisualDialog-
maybe someone can help transfer this code into native VDS:
For j = 7 To 0 Step -1
nBit = CBool((nCRC And 32768) = 32768) Xor _
((nByte And nPowers(j)) = nPowers(j))
nCRC = (nCRC And 32767&) * 2&
If nBit Then
nCRC = nCRC Xor &H8005&
End If
Next 'j
Greetings Mike
_________________ Greetings to all the folks back home in the States. A friendly "hola -como estas" to all my friends from Spain and Greece. |
|
Back to top |
|
|
SnarlingSheep Professional Member
Joined: 13 Mar 2001 Posts: 759 Location: Michigan
|
Posted: Tue Feb 28, 2006 6:34 pm Post subject: |
|
|
I took a stab at it even though I've never messed with VB.
It doesn't appear to be even close to right, but I'll post it here for you guys to play with.
It seems to be pretty much the same code, must be missing something small.
What does the & sign mean after some of the numbers in the VB example?
I know &h in front is hex, but not sure why some numbers are followed with just &.
Code: |
OPTION DECIMALSEP,"."
#DEFINE FUNCTION,SSBitAND
#DEFINE FUNCTION,SSBitXOR
#DEFINE FUNCTION,SSHex2Dec
#DEFINE FUNCTION,SSDec2Bin
#DEFINE FUNCTION,SSBin2Dec
#DEFINE FUNCTION,SSCRC32
REM *** Main Code ***
%%file = <Your File Here>
info CRC of %%file: @SSCRC32(%%file)
exit
REM *** Functions ***
:SSCRC32
%%CRCFile = %1
REM *** Powers of 2 ***
list create,1
list loadtext,1,
"1
"2
"4
"8
"16
"32
"64
"128
%%CRCx = 1
%%CRCnCRC = 0
%%CRCnBit = 0
repeat
%%CRCnByte = @asc(@substr(%%CRCFile, %%CRCx, %%CRCx))
%%CRCy = 7
repeat
if @equal(@SSBitAND(%%CRCnCRC,32768),32768)
%%CRCnBit = 1
else
%%CRCnBit = 0
end
if @equal(@SSBitAND(%%CRCnByte,@item(1,%%CRCy)),@item(1,%%CRCy))
%%CRCnBit2 = 1
else
%%CRCnBit2 = 0
end
%%CRCnBit = @SSBitXOR(%%CRCnBit,%%CRCnBit2)
%%CRCnCRC = @fmul(@SSBitAND(%%CRCnCRC,32767),2)
if @greater(%%CRCnBit,0)
%%CRCnCRC = @SSbitXOR(%%CRCnCRC,32773)
end
%%CRCy = @pred(%%CRCy)
until @equal(%%CRCy,-1)
%%CRCx = @succ(%%CRCx)
until @greater(%%CRCx,@len(%%CRCFile))
list close,1
exit %%CRCnCRC
:SSBitAnd
%%SSBAresult = ""
%%SSBA1 = @SSDec2Bin(%1)
%%SSBA2 = @SSDec2Bin(%2)
IF @GREATER(@LEN(%%SSBA1),@LEN(%%SSBA2))
%%SSBAStop = @LEN(%%SSBA1)
%%SSBACheck = 1
ELSE
%%SSBAStop = @LEN(%%SSBA2)
%%SSBACheck = 2
END
WHILE @NOT(@EQUAL(@LEN(%%SSBA2),@LEN(%%SSBA1)))
IF @EQUAL(%%SSBACheck,1)
%%SSBA2 = "0"%%SSBA2
ELSE
%%SSBA1 = "0"%%SSBA1
END
WEND
%%SSBAX = 1
REPEAT
IF @BOTH(@EQUAL(@SUBSTR(%%SSBA1,%%SSBAX,%%SSBAX),1),@EQUAL(@SUBSTR(%%SSBA2,%%SSBAX,%%SSBAX),1))
%%SSBAResult = %%SSBAResult"1"
ELSE
%%SSBAResult = %%SSBAResult"0"
END
%%SSBAX = @SUCC(%%SSBAX)
UNTIL @GREATER(%%SSBAX,%%SSBAStop)
%%SSBAresult = @SSBin2Dec(%%SSBAresult)
exit %%SSBAresult
:SSBitXOR
%%SSBXOresult = ""
%%SSBXO1 = @SSDec2Bin(%1)
%%SSBXO2 = @SSDec2Bin(%2)
IF @GREATER(@LEN(%%SSBXO1),@LEN(%%SSBXO2))
%%SSBXOStop = @LEN(%%SSBXO1)
%%SSBXOCheck = 1
ELSE
%%SSBXOStop = @LEN(%%SSBXO2)
%%SSBXOCheck = 2
END
WHILE @NOT(@EQUAL(@LEN(%%SSBXO2),@LEN(%%SSBXO1)))
IF @EQUAL(%%SSBXOCheck,1)
%%SSBXO2 = "0"%%SSBXO2
ELSE
%%SSBXO1 = "0"%%SSBXO1
END
WEND
%%SSBXOX = 1
REPEAT
IF @BOTH(@EQUAL(@SUBSTR(%%SSBXO1,%%SSBXOX,%%SSBXOX),1),@EQUAL(@SUBSTR(%%SSBXO2,%%SSBXOX,%%SSBXOX),1))
%%SSBXOResult = %%SSBXOResult"0"
ELSIF @BOTH(@ZERO(@SUBSTR(%%SSBXO1,%%SSBXOX,%%SSBXOX)),@ZERO(@SUBSTR(%%SSBXO2,%%SSBXOX,%%SSBXOX)))
%%SSBXOResult = %%SSBXOResult"0"
ELSE
%%SSBXOResult = %%SSBXOResult"1"
END
%%SSBXOX = @SUCC(%%SSBXOX)
UNTIL @GREATER(%%SSBXOX,%%SSBXOStop)
%%SSBXOresult = @SSBin2Dec(%%SSBXOresult)
exit %%SSBXOresult
:SSDec2Bin
%%SSD2Bresult = ""
%%SSD2BintValue = %1
While @GREATER(%%SSD2BintValue,0)
%%SSD2Bi = @MOD(%%SSD2BIntValue,2)
%%SSD2Bi = @FADD(%%SSD2Bi,1)
%%SSD2BintValue = @DIV(%%SSD2BintValue,2)
%%SSD2Bresult = @SUBSTR("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",%%SSD2Bi,%%SSD2Bi)%%SSD2Bresult
WEND
exit %%SSD2Bresult
:SSBin2Dec
%%SSB2DResult = 0
%%SSB2DX = 1
%%SSB2DStrValue = %1
REPEAT
%%SSB2DCharValue = @POS(@UPPER(@SUBSTR(%%SSB2DStrValue,%%SSB2DX,%%SSB2DX)),"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#return -1 if character is not supported by base
If @GREATER(%%SSB2DCharValue,2)
%%SSB2DResult = -1
%%SSB2DX = @SUCC(@LEN(%%SSB2DStrValue))
END
%%SSB2DResult = @FADD(@FMUL(%%SSB2DResult,2),@FSUB(%%SSB2DcharValue,1))
%%SSB2DX = @SUCC(%%SSB2DX)
UNTIL @GREATER(%%SSB2DX,@LEN(%%SSB2DStrValue))
exit %%SSB2DResult
|
_________________ -Sheep
My pockets hurt... |
|
Back to top |
|
|
Estas Contributor
Joined: 31 Jan 2004 Posts: 66 Location: Germany
|
Posted: Wed Mar 01, 2006 3:03 pm Post subject: |
|
|
Thanks so much,
for going through the hazzle of translating that checksum-routine.
Great work, although there seems to be something missing...
I am still trying to figure out what it is.
Nevertheless, thanks for your great help...
Greetings Mike
_________________ Greetings to all the folks back home in the States. A friendly "hola -como estas" to all my friends from Spain and Greece. |
|
Back to top |
|
|
SnarlingSheep Professional Member
Joined: 13 Mar 2001 Posts: 759 Location: Michigan
|
Posted: Wed Mar 01, 2006 4:02 pm Post subject: |
|
|
Glad to help, I just wish I could figure out why it doesn't work.
One thing I didn't bother to pay attention to.. this code can't be finding the CRC value based on the filename only.. or else a file would have a different checksum each time you renamed it.
Do you have the rest of this code? To see what is being passed to the CRC32 function.
_________________ -Sheep
My pockets hurt... |
|
Back to top |
|
|
Estas Contributor
Joined: 31 Jan 2004 Posts: 66 Location: Germany
|
Posted: Wed Mar 01, 2006 4:47 pm Post subject: |
|
|
The way I understand this:
the contens of a file is put into a string variable. The entire string is being processed byte by byte and the checksum calculated through some mathematical algorythm - not the filename is important, just its content.
_________________ Greetings to all the folks back home in the States. A friendly "hola -como estas" to all my friends from Spain and Greece. |
|
Back to top |
|
|
SnarlingSheep Professional Member
Joined: 13 Mar 2001 Posts: 759 Location: Michigan
|
Posted: Wed Mar 01, 2006 5:13 pm Post subject: |
|
|
That's what I figured.. I was too busy just converting the code to pay attention to what it really did.
Trying it with BINFILE, with an 8k file has taken about 5 minutes so far, with no result.
But, it may be possible to use @binfile() seek and read, instead of @substr().
I will try that when I get the chance.. I still think it'll be too slow for any good use.
_________________ -Sheep
My pockets hurt... |
|
Back to top |
|
|
Estas Contributor
Joined: 31 Jan 2004 Posts: 66 Location: Germany
|
Posted: Wed Mar 01, 2006 6:18 pm Post subject: |
|
|
I can second that, it took appr. 1 1/2 min for 1020 bytes to process.
Well, maybe not useful to use in VDS that way, but certainly very challenging to comprehend its structure.
_________________ Greetings to all the folks back home in the States. A friendly "hola -como estas" to all my friends from Spain and Greece. |
|
Back to top |
|
|
WidgetCoder Contributor
Joined: 28 May 2002 Posts: 126 Location: CO, USA
|
Posted: Wed Mar 01, 2006 11:26 pm Post subject: CRC DLL (24kb) |
|
|
I think you would be better off using an extension. Here's a small 24kb dll I just compiled that should do the trick.
Description: |
|
Download |
Filename: |
CRC32.zip |
Filesize: |
22.34 KB |
Downloaded: |
2188 Time(s) |
|
|
Back to top |
|
|
Estas Contributor
Joined: 31 Jan 2004 Posts: 66 Location: Germany
|
Posted: Thu Mar 02, 2006 9:08 pm Post subject: |
|
|
Thanks,
what can I say but thanks so much!!!
I*ll give it a shot tomorrow...
_________________ Greetings to all the folks back home in the States. A friendly "hola -como estas" to all my friends from Spain and Greece. |
|
Back to top |
|
|
Estas Contributor
Joined: 31 Jan 2004 Posts: 66 Location: Germany
|
Posted: Fri Mar 03, 2006 10:00 am Post subject: |
|
|
Hi,
I ran your demo and all I can say is: terrific!!!!
This is a fast as a dll can be - just splendid.
Thanks again for this outstanding program and your help,
Greetings Mike
_________________ Greetings to all the folks back home in the States. A friendly "hola -como estas" to all my friends from Spain and Greece. |
|
Back to top |
|
|
WidgetCoder Contributor
Joined: 28 May 2002 Posts: 126 Location: CO, USA
|
Posted: Fri Mar 03, 2006 3:48 pm Post subject: |
|
|
You're quite welcome.. glad to be of assistance.
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|