FreeBASIC CRC32
FreeBASIC CRC32
[Content removed at author's request]
That crc doesnt seem to match others, ie "Hello" should give F7D18982
heres one that matches others
heres one that matches others
Code: Select all
' Translated to FreeBASIC from RFC 1952 by yetifoot
'
' crc should be initialized to 0
'
' Dim crc As uInteger
' Dim teststr As String
'
' teststr = "Hello"
' crc = 0
' crc = crc32_update(crc, strptr(teststr), Len(teststr))
' Print HEX$(crc)
Option Explicit
Dim Shared crc_table(256) As uInteger => { _
&H00000000, &H77073096, &HEE0E612C, &H990951BA, &H076DC419, &H706AF48F, _
&HE963A535, &H9E6495A3, &H0EDB8832, &H79DCB8A4, &HE0D5E91E, &H97D2D988, _
&H09B64C2B, &H7EB17CBD, &HE7B82D07, &H90BF1D91, &H1DB71064, &H6AB020F2, _
&HF3B97148, &H84BE41DE, &H1ADAD47D, &H6DDDE4EB, &HF4D4B551, &H83D385C7, _
&H136C9856, &H646BA8C0, &HFD62F97A, &H8A65C9EC, &H14015C4F, &H63066CD9, _
&HFA0F3D63, &H8D080DF5, &H3B6E20C8, &H4C69105E, &HD56041E4, &HA2677172, _
&H3C03E4D1, &H4B04D447, &HD20D85FD, &HA50AB56B, &H35B5A8FA, &H42B2986C, _
&HDBBBC9D6, &HACBCF940, &H32D86CE3, &H45DF5C75, &HDCD60DCF, &HABD13D59, _
&H26D930AC, &H51DE003A, &HC8D75180, &HBFD06116, &H21B4F4B5, &H56B3C423, _
&HCFBA9599, &HB8BDA50F, &H2802B89E, &H5F058808, &HC60CD9B2, &HB10BE924, _
&H2F6F7C87, &H58684C11, &HC1611DAB, &HB6662D3D, &H76DC4190, &H01DB7106, _
&H98D220BC, &HEFD5102A, &H71B18589, &H06B6B51F, &H9FBFE4A5, &HE8B8D433, _
&H7807C9A2, &H0F00F934, &H9609A88E, &HE10E9818, &H7F6A0DBB, &H086D3D2D, _
&H91646C97, &HE6635C01, &H6B6B51F4, &H1C6C6162, &H856530D8, &HF262004E, _
&H6C0695ED, &H1B01A57B, &H8208F4C1, &HF50FC457, &H65B0D9C6, &H12B7E950, _
&H8BBEB8EA, &HFCB9887C, &H62DD1DDF, &H15DA2D49, &H8CD37CF3, &HFBD44C65, _
&H4DB26158, &H3AB551CE, &HA3BC0074, &HD4BB30E2, &H4ADFA541, &H3DD895D7, _
&HA4D1C46D, &HD3D6F4FB, &H4369E96A, &H346ED9FC, &HAD678846, &HDA60B8D0, _
&H44042D73, &H33031DE5, &HAA0A4C5F, &HDD0D7CC9, &H5005713C, &H270241AA, _
&HBE0B1010, &HC90C2086, &H5768B525, &H206F85B3, &HB966D409, &HCE61E49F, _
&H5EDEF90E, &H29D9C998, &HB0D09822, &HC7D7A8B4, &H59B33D17, &H2EB40D81, _
&HB7BD5C3B, &HC0BA6CAD, &HEDB88320, &H9ABFB3B6, &H03B6E20C, &H74B1D29A, _
&HEAD54739, &H9DD277AF, &H04DB2615, &H73DC1683, &HE3630B12, &H94643B84, _
&H0D6D6A3E, &H7A6A5AA8, &HE40ECF0B, &H9309FF9D, &H0A00AE27, &H7D079EB1, _
&HF00F9344, &H8708A3D2, &H1E01F268, &H6906C2FE, &HF762575D, &H806567CB, _
&H196C3671, &H6E6B06E7, &HFED41B76, &H89D32BE0, &H10DA7A5A, &H67DD4ACC, _
&HF9B9DF6F, &H8EBEEFF9, &H17B7BE43, &H60B08ED5, &HD6D6A3E8, &HA1D1937E, _
&H38D8C2C4, &H4FDFF252, &HD1BB67F1, &HA6BC5767, &H3FB506DD, &H48B2364B, _
&HD80D2BDA, &HAF0A1B4C, &H36034AF6, &H41047A60, &HDF60EFC3, &HA867DF55, _
&H316E8EEF, &H4669BE79, &HCB61B38C, &HBC66831A, &H256FD2A0, &H5268E236, _
&HCC0C7795, &HBB0B4703, &H220216B9, &H5505262F, &HC5BA3BBE, &HB2BD0B28, _
&H2BB45A92, &H5CB36A04, &HC2D7FFA7, &HB5D0CF31, &H2CD99E8B, &H5BDEAE1D, _
&H9B64C2B0, &HEC63F226, &H756AA39C, &H026D930A, &H9C0906A9, &HEB0E363F, _
&H72076785, &H05005713, &H95BF4A82, &HE2B87A14, &H7BB12BAE, &H0CB61B38, _
&H92D28E9B, &HE5D5BE0D, &H7CDCEFB7, &H0BDBDF21, &H86D3D2D4, &HF1D4E242, _
&H68DDB3F8, &H1FDA836E, &H81BE16CD, &HF6B9265B, &H6FB077E1, &H18B74777, _
&H88085AE6, &HFF0F6A70, &H66063BCA, &H11010B5C, &H8F659EFF, &HF862AE69, _
&H616BFFD3, &H166CCF45, &HA00AE278, &HD70DD2EE, &H4E048354, &H3903B3C2, _
&HA7672661, &HD06016F7, &H4969474D, &H3E6E77DB, &HAED16A4A, &HD9D65ADC, _
&H40DF0B66, &H37D83BF0, &HA9BCAE53, &HDEBB9EC5, &H47B2CF7F, &H30B5FFE9, _
&HBDBDF21C, &HCABAC28A, &H53B39330, &H24B4A3A6, &HBAD03605, &HCDD70693, _
&H54DE5729, &H23D967BF, &HB3667A2E, &HC4614AB8, &H5D681B02, &H2A6F2B94, _
&HB40BBE37, &HC30C8EA1, &H5A05DF1B, &H2D02EF8D }
'::::::::::::::::
Public Function crc32_update(ByVal crc As uInteger, _
buf As ZString ptr, _
ByVal buf_len As Integer) As uInteger
Dim c As uInteger
Dim n As Integer
c = crc XOR 4294967295
For n = 0 To buf_len - 1
c = crc_table((c XOR buf[n]) AND 255) XOR (c shr 8)
Next n
crc32_update = c XOR 4294967295
End Function
Last edited by yetifoot on Dec 04, 2005 3:09, edited 1 time in total.
It exists dozens of implementations of CRC-32, each one gives a diffrent result. The trick is in choosing the right one....
All about CRC32:
http://c.snippets.org/snip_lister.php?fname=crc.txt
All about CRC32:
http://c.snippets.org/snip_lister.php?fname=crc.txt
I did an adler as well, apparently its faster but has a significantly higher chance of collisions
Code: Select all
' Translated to FreeBASIC from RFC 1950 by yetifoot
'
' adler should be initialized to 1
'
' Dim adler As uInteger
' Dim teststr As String
'
' teststr = "Hello"
' adler = 1
' adler = adler32_update(adler, strptr(teststr), Len(teststr))
' Print HEX$(adler)
Option Explicit
Public Function adler32_update(ByVal adler As uInteger, _
buf As ZString ptr, _
ByVal buf_len As Integer) As uInteger
Dim s1 As uInteger
Dim s2 As uInteger
Dim n As Integer
s1 = adler AND 65535
s2 = (adler shr 16) AND 65535
For n = 0 To buf_len - 1
s1 = (s1 + buf[n]) MOD 65521
s2 = (s2 + s1) MOD 65521
Next n
adler32_update = (s2 shl 16) + s1
End Function
CRC32 wrong results
"It exists dozens of implementations of CRC-32, each one gives a diffrent result. The trick is in choosing the right one...."
NO. The implementations can vary in speed, but they definitely MUST
GIVE all same results. I tested some different programms and they all
gave same results as PKZIP 2.50.
So, PKZIP 2.50 is the correct reference for testing, besides CRC32
prog from Skylark or XVI32 file editor or Total Commander.
NO. The implementations can vary in speed, but they definitely MUST
GIVE all same results. I tested some different programms and they all
gave same results as PKZIP 2.50.
So, PKZIP 2.50 is the correct reference for testing, besides CRC32
prog from Skylark or XVI32 file editor or Total Commander.
CRC32 correct results
Tested yetifoot's code and performed detail fixes.
Code: Select all
' CRC32 algorithm for FreeBASIC
' Fast table-based calculation method
' Ready to compile incl. test code
'
' Translated to FreeBASIC from RFC 1952 by yetifoot
'
' Detail fixes (HEX numbers, working test code, all integers unsigned)
' 2005/12/17 by StopTCPA
'
' "crc" variable should be initialized to 0 in this code
'
' Compatible with PKZIP and other progs calculationg CRC32
Option Explicit
type uint32 as uinteger
Dim Shared crc_table(256) As uint32 => { _
&H00000000, &H77073096, &HEE0E612C, &H990951BA, &H076DC419, &H706AF48F, _
&HE963A535, &H9E6495A3, &H0EDB8832, &H79DCB8A4, &HE0D5E91E, &H97D2D988, _
&H09B64C2B, &H7EB17CBD, &HE7B82D07, &H90BF1D91, &H1DB71064, &H6AB020F2, _
&HF3B97148, &H84BE41DE, &H1ADAD47D, &H6DDDE4EB, &HF4D4B551, &H83D385C7, _
&H136C9856, &H646BA8C0, &HFD62F97A, &H8A65C9EC, &H14015C4F, &H63066CD9, _
&HFA0F3D63, &H8D080DF5, &H3B6E20C8, &H4C69105E, &HD56041E4, &HA2677172, _
&H3C03E4D1, &H4B04D447, &HD20D85FD, &HA50AB56B, &H35B5A8FA, &H42B2986C, _
&HDBBBC9D6, &HACBCF940, &H32D86CE3, &H45DF5C75, &HDCD60DCF, &HABD13D59, _
&H26D930AC, &H51DE003A, &HC8D75180, &HBFD06116, &H21B4F4B5, &H56B3C423, _
&HCFBA9599, &HB8BDA50F, &H2802B89E, &H5F058808, &HC60CD9B2, &HB10BE924, _
&H2F6F7C87, &H58684C11, &HC1611DAB, &HB6662D3D, &H76DC4190, &H01DB7106, _
&H98D220BC, &HEFD5102A, &H71B18589, &H06B6B51F, &H9FBFE4A5, &HE8B8D433, _
&H7807C9A2, &H0F00F934, &H9609A88E, &HE10E9818, &H7F6A0DBB, &H086D3D2D, _
&H91646C97, &HE6635C01, &H6B6B51F4, &H1C6C6162, &H856530D8, &HF262004E, _
&H6C0695ED, &H1B01A57B, &H8208F4C1, &HF50FC457, &H65B0D9C6, &H12B7E950, _
&H8BBEB8EA, &HFCB9887C, &H62DD1DDF, &H15DA2D49, &H8CD37CF3, &HFBD44C65, _
&H4DB26158, &H3AB551CE, &HA3BC0074, &HD4BB30E2, &H4ADFA541, &H3DD895D7, _
&HA4D1C46D, &HD3D6F4FB, &H4369E96A, &H346ED9FC, &HAD678846, &HDA60B8D0, _
&H44042D73, &H33031DE5, &HAA0A4C5F, &HDD0D7CC9, &H5005713C, &H270241AA, _
&HBE0B1010, &HC90C2086, &H5768B525, &H206F85B3, &HB966D409, &HCE61E49F, _
&H5EDEF90E, &H29D9C998, &HB0D09822, &HC7D7A8B4, &H59B33D17, &H2EB40D81, _
&HB7BD5C3B, &HC0BA6CAD, &HEDB88320, &H9ABFB3B6, &H03B6E20C, &H74B1D29A, _
&HEAD54739, &H9DD277AF, &H04DB2615, &H73DC1683, &HE3630B12, &H94643B84, _
&H0D6D6A3E, &H7A6A5AA8, &HE40ECF0B, &H9309FF9D, &H0A00AE27, &H7D079EB1, _
&HF00F9344, &H8708A3D2, &H1E01F268, &H6906C2FE, &HF762575D, &H806567CB, _
&H196C3671, &H6E6B06E7, &HFED41B76, &H89D32BE0, &H10DA7A5A, &H67DD4ACC, _
&HF9B9DF6F, &H8EBEEFF9, &H17B7BE43, &H60B08ED5, &HD6D6A3E8, &HA1D1937E, _
&H38D8C2C4, &H4FDFF252, &HD1BB67F1, &HA6BC5767, &H3FB506DD, &H48B2364B, _
&HD80D2BDA, &HAF0A1B4C, &H36034AF6, &H41047A60, &HDF60EFC3, &HA867DF55, _
&H316E8EEF, &H4669BE79, &HCB61B38C, &HBC66831A, &H256FD2A0, &H5268E236, _
&HCC0C7795, &HBB0B4703, &H220216B9, &H5505262F, &HC5BA3BBE, &HB2BD0B28, _
&H2BB45A92, &H5CB36A04, &HC2D7FFA7, &HB5D0CF31, &H2CD99E8B, &H5BDEAE1D, _
&H9B64C2B0, &HEC63F226, &H756AA39C, &H026D930A, &H9C0906A9, &HEB0E363F, _
&H72076785, &H05005713, &H95BF4A82, &HE2B87A14, &H7BB12BAE, &H0CB61B38, _
&H92D28E9B, &HE5D5BE0D, &H7CDCEFB7, &H0BDBDF21, &H86D3D2D4, &HF1D4E242, _
&H68DDB3F8, &H1FDA836E, &H81BE16CD, &HF6B9265B, &H6FB077E1, &H18B74777, _
&H88085AE6, &HFF0F6A70, &H66063BCA, &H11010B5C, &H8F659EFF, &HF862AE69, _
&H616BFFD3, &H166CCF45, &HA00AE278, &HD70DD2EE, &H4E048354, &H3903B3C2, _
&HA7672661, &HD06016F7, &H4969474D, &H3E6E77DB, &HAED16A4A, &HD9D65ADC, _
&H40DF0B66, &H37D83BF0, &HA9BCAE53, &HDEBB9EC5, &H47B2CF7F, &H30B5FFE9, _
&HBDBDF21C, &HCABAC28A, &H53B39330, &H24B4A3A6, &HBAD03605, &HCDD70693, _
&H54DE5729, &H23D967BF, &HB3667A2E, &HC4614AB8, &H5D681B02, &H2A6F2B94, _
&HB40BBE37, &HC30C8EA1, &H5A05DF1B, &H2D02EF8D }
'******************************************************
Function crc32_update(ByVal crc As uint32, _
buf As ZString ptr, _
ByVal buf_len As uint32) As uint32
Dim c As uint32
Dim n As uint32
c = crc XOR &HFFFFFFFF
For n = 0 To buf_len - 1
c = crc_table((c XOR buf[n]) AND &HFF) XOR (c shr 8)
Next n
crc32_update = c XOR &HFFFFFFFF
End Function
'*******************************************************
'*** TEST CODE
'*******************************************************
'F7D18982 correct results
'D202EF8D
'7218872B
Dim crc As uint32
Dim teststr As String
teststr = "Hello"
crc = 0
crc = crc32_update(crc, strptr(teststr), Len(teststr))
Print teststr;" ";HEX$(crc)
teststr = chr$(0) 'not visible on the screen
crc = 0
crc = crc32_update(crc, strptr(teststr), Len(teststr))
Print teststr;" ";HEX$(crc)
teststr = "Freebasic"
crc = 0
crc = crc32_update(crc, strptr(teststr), Len(teststr))
Print teststr;" ";HEX$(crc)
end
Re: CRC32 wrong results
Says who ?StopTCPA wrote:"It exists dozens of implementations of CRC-32, each one gives a diffrent result. The trick is in choosing the right one...."
NO. The implementations can vary in speed, but they definitely MUST
GIVE all same results.
CRC-32 is an algorithm which ( I paraphrase ) specifies how to calculate a checksum of 32-bit size using a polynomial. The starting values, polynomial used and inversion or bit reversal of results can all be varied and are not defined by the algorithm itself, only for a particular implementation / application.
I agree with Antoni.
In the case of cryptographic hash functions such as MD5 the magic constants used in the algorithm is what defines MD5, RIPEMD-160, SHA-1/SHA-256, etc, etc.
So whereas you can use the MD5 implementation and use other constants instead of the original constants you should not call your implementation MD5...
Simple.
So whereas you can use the MD5 implementation and use other constants instead of the original constants you should not call your implementation MD5...
Simple.
im sorry to say i've found an error in z!res code that will cause collisions that should not be there
it appears that the following data in treated the same.
Hex
00 01 == 01
here is my test program, however this is not a one off, these collisions repeatedly occur with similar data
it appears that the following data in treated the same.
Hex
00 01 == 01
here is my test program, however this is not a one off, these collisions repeatedly occur with similar data
Code: Select all
DIm test(2) As Ubyte
test(0) = 0
test(1) = 1
Print crc32(@test(0), 2)
test(0) = 1
Print crc32(@test(0), 1)
Sleep