PureBasic Forumhttps://www.purebasic.fr/english/ Configurable CRC procedure (1 - 32 bits)https://www.purebasic.fr/english/viewtopic.php?f=12&t=73468 Page 1 of 1

 Author: wilbert [ Sat Aug 24, 2019 3:34 pm ] Post subject: Configurable CRC procedure (1 - 32 bits) After workin on a CRC module ( viewtopic.php?f=12&t=73462 ),I realized that I could do most of it in a single procedure without the use of asm.It's not as fast and doesn't support CRC with a width above 32 bits but seems to work fine.In it's current configuration, the result is the same as the PB builtin CRC32 algorithm.Some other often used configurations ...CRC-16/CCITT: Width=16, Poly=\$1021, Init=\$ffff, RefIn=#False, RefOut=#False, XOrOut=0CRC-16/MODBUS: Width=16, Poly=\$8005, Init=\$ffff, RefIn=#True, RefOut=#True, XOrOut=0For more of them, see http://reveng.sourceforge.net/crc-catalogue/Code:Procedure.l CRC(*Buffer, Size)    Static.l Width  = 32  Static.l Poly   = \$04c11db7  Static.l Init   = -1  Static.l RefIn  = #True  Static.l RefOut = #True  Static.l XOrOut = -1    Static.l M  Static Dim T.l(1023)  Protected.l i,j,C,R,*B1.Ascii,*B4.Long    ; Calculate CRC lookup table  If Not M    M=~((-2)<<(Width-1))    If RefIn      R=0: For i=1 To Width: R|((Poly>>(i-1))&1)<<(Width-i): Next    Else      R=Poly<<(32-Width)    EndIf    For i=0 To 255      If RefIn        C=i: For j=0 To 7: C=((C>>1)&\$7fffffff)!((-(C&1))&R): Next      Else        C=i<<24: For j=0 To 7: C=(C<<1)!((C>>31)&R): Next        C=(C<<24)|(((C>>8)&255)<<16)|(((C>>16)&255)<<8)|((C>>24)&255)      EndIf      T(i)=C    Next    For i=0 To 255      C=T(i): For j=1 To 3: C=((C>>8)&\$ffffff)!T(C&255): T((j<<8)|i)=C: Next    Next      EndIf    ; 4 byte loop  C=Init&M: *B4=*Buffer  While Size >= 4    C!*B4\l: *B4+4: Size-4    C=T(C&255+768)!T(((C>>8)&255)+512)!T(((C>>16)&255)+256)!T(((C>>24)&255))  Wend    ; 1 byte loop  *B1=*B4  While Size    C=((C>>8)&\$ffffff)!T((C!*B1\a)&255)    *B1+1: Size-1  Wend    ; Finalize and output CRC  If Not RefIn    C=((C<<24)|(((C>>8)&255)<<16)|(((C>>16)&255)<<8)|((C>>24)&255))>>(32-Width)  EndIf    If RefOut<>RefIn    R=0: For i=1 To Width: R|((C>>(i-1))&1)<<(Width-i): Next: C=R  EndIf  ProcedureReturn (C!XOrOut)&M      EndProcedure

 Author: Kwai chang caine [ Sat Aug 24, 2019 7:16 pm ] Post subject: Re: Configurable CRC procedure (1 - 32 bits) That works to on the same machine I obtain "378246432" all the timeCode:*Mem = AllocateMemory(1234567, #PB_Memory_NoClear)RandomSeed(0)RandomData(*Mem, 1234567)Debug CRC(*Mem, 1234567)FreeMemory(*Mem)

 Page 1 of 1 All times are UTC + 1 hour Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/