Hello Charles
Below is the program which i copied from your reply#1 on
https://www.oxygenbasic.org/forum/index.php?topic=1858.msg20086;topicseen#msg20086 (https://www.oxygenbasic.org/forum/index.php?topic=1858.msg20086;topicseen#msg20086)
Each time i run the program with different files, it always display negative CRC checksums
is this normal?
But when i use the PB crc program, it always gives positive CRC checksums
' https://www.oxygenbasic.org/forum/index.php?topic=1858.msg20086;topicseen#msg20086
$ filename "CRC_Checksum.exe"
uses rtl64
uses chaos
string fname="CRC_Checksum.o2bas"
string fstr
getfile fname, fstr
Long CrcVal = Crc32(strptr fstr, len fstr)
string fname2="Charles.txt"
string fstr2
getfile fname2, fstr2
Long CrcVal2 = Crc32(strptr fstr2, len fstr2)
print "CrcVal for file1 : " CrcVal + chr(13,10) +
"CrcVal for file2 : " CrcVal2
Here's the PB program which always return positive numbers
#COMPILE EXE
#DIM ALL
#INCLUDE "WIN32API.INC"
'https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/25048-asm-function-crc-32-checksum#post25048
'===========================
FUNCTION PBMAIN () AS LONG
LOCAL crcVal AS DWORD
LOCAL fstr AS STRING
LOCAL Buffer , GivenFile AS STRING
GivenFile = "CRC_Checksum.o2bas"
fstr = ""
fstr = CRepFileStr(GivenFile)
crcVal = crc32( fstr)
? " CRC Val : " + STR$(crcVal)
END FUNCTION
'==================================
FUNCTION CRC32(a AS STRING) AS DWORD
!mov eax, a
!mov eax, [eax]
!mov ecx, [eax-4]
!lea esi, crcData
!mov edx, &hFFFFFFFF
!mov edi, eax
procLoop:
!mov al, [edi]
!and eax, &h0000000FF
!xor eax, edx
!and eax, &h0000000FF
!shl eax, 2
!shr edx, 8
!and edx, &h000FFFFFF
!add eax, esi
!xor edx, [eax]
!inc edi
!dec ecx
!jnz procLoop
!not edx
!jmp retfunc
crcData:
!DD &h000000000, &h077073096, &h0EE0E612C, &h0990951BA
!DD &h0076DC419, &h0706AF48F, &h0E963A535, &h09E6495A3
!DD &h00EDB8832, &h079DCB8A4, &h0E0D5E91E, &h097D2D988
!DD &h009B64C2B, &h07EB17CBD, &h0E7B82D07, &h090BF1D91
!DD &h01DB71064, &h06AB020F2, &h0F3B97148, &h084BE41DE
!DD &h01ADAD47D, &h06DDDE4EB, &h0F4D4B551, &h083D385C7
!DD &h0136C9856, &h0646BA8C0, &h0FD62F97A, &h08A65C9EC
!DD &h014015C4F, &h063066CD9, &h0FA0F3D63, &h08D080DF5
!DD &h03B6E20C8, &h04C69105E, &h0D56041E4, &h0A2677172
!DD &h03C03E4D1, &h04B04D447, &h0D20D85FD, &h0A50AB56B
!DD &h035B5A8FA, &h042B2986C, &h0DBBBC9D6, &h0ACBCF940
!DD &h032D86CE3, &h045DF5C75, &h0DCD60DCF, &h0ABD13D59
!DD &h026D930AC, &h051DE003A, &h0C8D75180, &h0BFD06116
!DD &h021B4F4B5, &h056B3C423, &h0CFBA9599, &h0B8BDA50F
!DD &h02802B89E, &h05F058808, &h0C60CD9B2, &h0B10BE924
!DD &h02F6F7C87, &h058684C11, &h0C1611DAB, &h0B6662D3D
!DD &h076DC4190, &h001DB7106, &h098D220BC, &h0EFD5102A
!DD &h071B18589, &h006B6B51F, &h09FBFE4A5, &h0E8B8D433
!DD &h07807C9A2, &h00F00F934, &h09609A88E, &h0E10E9818
!DD &h07F6A0DBB, &h0086D3D2D, &h091646C97, &h0E6635C01
!DD &h06B6B51F4, &h01C6C6162, &h0856530D8, &h0F262004E
!DD &h06C0695ED, &h01B01A57B, &h08208F4C1, &h0F50FC457
!DD &h065B0D9C6, &h012B7E950, &h08BBEB8EA, &h0FCB9887C
!DD &h062DD1DDF, &h015DA2D49, &h08CD37CF3, &h0FBD44C65
!DD &h04DB26158, &h03AB551CE, &h0A3BC0074, &h0D4BB30E2
!DD &h04ADFA541, &h03DD895D7, &h0A4D1C46D, &h0D3D6F4FB
!DD &h04369E96A, &h0346ED9FC, &h0AD678846, &h0DA60B8D0
!DD &h044042D73, &h033031DE5, &h0AA0A4C5F, &h0DD0D7CC9
!DD &h05005713C, &h0270241AA, &h0BE0B1010, &h0C90C2086
!DD &h05768B525, &h0206F85B3, &h0B966D409, &h0CE61E49F
!DD &h05EDEF90E, &h029D9C998, &h0B0D09822, &h0C7D7A8B4
!DD &h059B33D17, &h02EB40D81, &h0B7BD5C3B, &h0C0BA6CAD
!DD &h0EDB88320, &h09ABFB3B6, &h003B6E20C, &h074B1D29A
!DD &h0EAD54739, &h09DD277AF, &h004DB2615, &h073DC1683
!DD &h0E3630B12, &h094643B84, &h00D6D6A3E, &h07A6A5AA8
!DD &h0E40ECF0B, &h09309FF9D, &h00A00AE27, &h07D079EB1
!DD &h0F00F9344, &h08708A3D2, &h01E01F268, &h06906C2FE
!DD &h0F762575D, &h0806567CB, &h0196C3671, &h06E6B06E7
!DD &h0FED41B76, &h089D32BE0, &h010DA7A5A, &h067DD4ACC
!DD &h0F9B9DF6F, &h08EBEEFF9, &h017B7BE43, &h060B08ED5
!DD &h0D6D6A3E8, &h0A1D1937E, &h038D8C2C4, &h04FDFF252
!DD &h0D1BB67F1, &h0A6BC5767, &h03FB506DD, &h048B2364B
!DD &h0D80D2BDA, &h0AF0A1B4C, &h036034AF6, &h041047A60
!DD &h0DF60EFC3, &h0A867DF55, &h0316E8EEF, &h04669BE79
!DD &h0CB61B38C, &h0BC66831A, &h0256FD2A0, &h05268E236
!DD &h0CC0C7795, &h0BB0B4703, &h0220216B9, &h05505262F
!DD &h0C5BA3BBE, &h0B2BD0B28, &h02BB45A92, &h05CB36A04
!DD &h0C2D7FFA7, &h0B5D0CF31, &h02CD99E8B, &h05BDEAE1D
!DD &h09B64C2B0, &h0EC63F226, &h0756AA39C, &h0026D930A
!DD &h09C0906A9, &h0EB0E363F, &h072076785, &h005005713
!DD &h095BF4A82, &h0E2B87A14, &h07BB12BAE, &h00CB61B38
!DD &h092D28E9B, &h0E5D5BE0D, &h07CDCEFB7, &h00BDBDF21
!DD &h086D3D2D4, &h0F1D4E242, &h068DDB3F8, &h01FDA836E
!DD &h081BE16CD, &h0F6B9265B, &h06FB077E1, &h018B74777
!DD &h088085AE6, &h0FF0F6A70, &h066063BCA, &h011010B5C
!DD &h08F659EFF, &h0F862AE69, &h0616BFFD3, &h0166CCF45
!DD &h0A00AE278, &h0D70DD2EE, &h04E048354, &h03903B3C2
!DD &h0A7672661, &h0D06016F7, &h04969474D, &h03E6E77DB
!DD &h0AED16A4A, &h0D9D65ADC, &h040DF0B66, &h037D83BF0
!DD &h0A9BCAE53, &h0DEBB9EC5, &h047B2CF7F, &h030B5FFE9
!DD &h0BDBDF21C, &h0CABAC28A, &h053B39330, &h024B4A3A6
!DD &h0BAD03605, &h0CDD70693, &h054DE5729, &h023D967BF
!DD &h0B3667A2E, &h0C4614AB8, &h05D681B02, &h02A6F2B94
!DD &h0B40BBE37, &h0C30C8EA1, &h05A05DF1B, &h02D02EF8D
retfunc:
!mov FUNCTION, edx
END FUNCTION
'===================================
FUNCTION CRepFileStr(gfnam AS STRING) AS STRING
' reads in the entire file as a string
' for FNV to hash
LOCAL ff AS LONG
LOCAL bst AS STRING
IF ISFALSE ISFILE(gfnam) THEN
' File not found error -- we return a null string
' this prevents hackers from guessing it
FUNCTION = ""
EXIT FUNCTION
END IF
ff = FREEFILE
OPEN gfnam FOR BINARY AS #ff
GET$ #ff, LOF(ff), bst
CLOSE #ff
FUNCTION = bst
END FUNCTION
Hi Chris,
Use a dword instead of an int. They hold the same bit values but dwords display positive.
dword d
d=-1
print d
Thanxx a lot, Charles
i will be using Dword from now onwards
i also noticed that your Crc32() function is quite good as i have tested very short strings
with only 3 characters as below
' Check very small strings
fstr = "123"
fstr2 = "124"
CrcVal = Crc32(strptr fstr, len fstr)
CrcVal2 = Crc32(strptr fstr2, len fstr2)
print "CrcVal for string1 : " CrcVal + chr(13,10) +
"CrcVal for string2 : " CrcVal2
it will display the results as
CrcVal for string1 : 2286445522
CrcVal for string2 : 372045425
And that this tally EXACTLY with crc values as computed by the PB program above (in reply#1)
i have use other checksum programs and that they don't work well with very short strings
due to collisions