Adler32 crc generator

Share your advanced PureBasic knowledge/code with the community.
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Adler32 crc generator

Post by jacdelad »

Hi,
here's an Adler32-crc-generator. I hope I got it right.

Code: Select all

#Mod_Adler = 65521
Procedure.l Adler32(*Buffer,BufferSize)
  Protected a.l=1,b.l,c.i,d.i=*Buffer+BufferSize-1
  For c=*Buffer To d
    a=(a+PeekA(c)) % #Mod_Adler
    b=(b+a) % #Mod_Adler
  Next
  ProcedureReturn (b << 16) | a
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
  Define *Test=AllocateMemory(1024),t
  For t=0 To MemorySize(*Test)-1
    PokeA(*Test+t,Random(255,0))
  Next
  Debug Hex(Adler32(*Test,MemorySize(*Test)) & $FFFFFFFF)
CompilerEndIf
According to Wikipedia it should be faster than CRC32, but isn't as reliable as it. Also, short memory blocks (up to several hundred bytes) tend to be problematic.

However, that's the easiest implementation. There's obviously a better one, but I have to admit that I didn't understand the concept how it works (and this one should be the fast one). Maybe someone wants to add this:
https://en.wikipedia.org/wiki/Adler-32
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD