16-bit CRC (Cyclic Redundancy Checksum) of Memory

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

;Enable Inline-asm !

;CRC16 Adaption by Siegfried Rings February,5.the 2002
;
;Evalutate the 16-bit CRC (Cyclic Redundancy Checksum) of an MemoryBlock
;
;
; Can be more optimized with pure inline-asm.

Procedure XOR(a,b)
MOV eax,a
XOR eax,b
MOV a,eax
ProcedureReturn a
EndProcedure

Procedure.l CRC16(StartAddress, Size)
i.l
fcs.l
Dim fcstab.l(255)

pppinitfcs16 = $FFFF ;Initial FCS value

; Initialize array once And For all
fcstab(0) = $0
fcstab(1) = $1189
fcstab(2) = $2312
fcstab(3) = $329B
fcstab(4) = $4624
fcstab(5) = $57AD
fcstab(6) = $6536
fcstab(7) = $74BF
fcstab(8) = $8C48
fcstab(9) = $9DC1
fcstab(10) = $AF5A
fcstab(11) = $BED3
fcstab(12) = $CA6C
fcstab(13) = $DBE5
fcstab(14) = $E97E
fcstab(15) = $F8F7
fcstab(16) = $1081
fcstab(17) = $108
fcstab(18) = $3393
fcstab(19) = $221A
fcstab(20) = $56A5
fcstab(21) = $472C
fcstab(22) = $75B7
fcstab(23) = $643E
fcstab(24) = $9CC9
fcstab(25) = $8D40
fcstab(26) = $BFDB
fcstab(27) = $AE52
fcstab(28) = $DAED
fcstab(29) = $CB64
fcstab(30) = $F9FF
fcstab(31) = $E876
fcstab(32) = $2102
fcstab(33) = $308B
fcstab(34) = $210
fcstab(35) = $1399
fcstab(36) = $6726
fcstab(37) = $76AF
fcstab(38) = $4434
fcstab(39) = $55BD
fcstab(40) = $AD4A
fcstab(41) = $BCC3
fcstab(42) = $8E58
fcstab(43) = $9FD1
fcstab(44) = $EB6E
fcstab(45) = $FAE7
fcstab(46) = $C87C
fcstab(47) = $D9F5
fcstab(48) = $3183
fcstab(49) = $200A
fcstab(50) = $1291
fcstab(51) = $318
fcstab(52) = $77A7
fcstab(53) = $662E
fcstab(54) = $54B5
fcstab(55) = $453C
fcstab(56) = $BDCB
fcstab(57) = $AC42
fcstab(58) = $9ED9
fcstab(59) = $8F50
fcstab(60) = $FBEF
fcstab(61) = $EA66
fcstab(62) = $D8FD
fcstab(63) = $C974
fcstab(64) = $4204
fcstab(65) = $538D
fcstab(66) = $6116
fcstab(67) = $709F
fcstab(68) = $420
fcstab(69) = $15A9
fcstab(70) = $2732
fcstab(71) = $36BB
fcstab(72) = $CE4C
fcstab(73) = $DFC5
fcstab(74) = $ED5E
fcstab(75) = $FCD7
fcstab(76) = $8868
fcstab(77) = $99E1
fcstab(78) = $AB7A
fcstab(79) = $BAF3
fcstab(80) = $5285
fcstab(81) = $430C
fcstab(82) = $7197
fcstab(83) = $601E
fcstab(84) = $14A1
fcstab(85) = $528
fcstab(86) = $37B3
fcstab(87) = $263A
fcstab(88) = $DECD
fcstab(89) = $CF44
fcstab(90) = $FDDF
fcstab(91) = $EC56
fcstab(92) = $98E9
fcstab(93) = $8960
fcstab(94) = $BBFB
fcstab(95) = $AA72
fcstab(96) = $6306
fcstab(97) = $728F
fcstab(98) = $4014
fcstab(99) = $519D
fcstab(100) = $2522
fcstab(101) = $34AB
fcstab(102) = $630
fcstab(103) = $17B9
fcstab(104) = $EF4E
fcstab(105) = $FEC7
fcstab(106) = $CC5C
fcstab(107) = $DDD5
fcstab(108) = $A96A
fcstab(109) = $B8E3
fcstab(110) = $8A78
fcstab(111) = $9BF1
fcstab(112) = $7387
fcstab(113) = $620E
fcstab(114) = $5095
fcstab(115) = $411C
fcstab(116) = $35A3
fcstab(117) = $242A
fcstab(118) = $16B1
fcstab(119) = $738
fcstab(120) = $FFCF
fcstab(121) = $EE46
fcstab(122) = $DCDD
fcstab(123) = $CD54
fcstab(124) = $B9EB
fcstab(125) = $A862
fcstab(126) = $9AF9
fcstab(127) = $8B70
fcstab(128) = $8408
fcstab(129) = $9581
fcstab(130) = $A71A
fcstab(131) = $B693
fcstab(132) = $C22C
fcstab(133) = $D3A5
fcstab(134) = $E13E
fcstab(135) = $F0B7
fcstab(136) = $840
fcstab(137) = $19C9
fcstab(138) = $2B52
fcstab(139) = $3ADB
fcstab(140) = $4E64
fcstab(141) = $5FED
fcstab(142) = $6D76
fcstab(143) = $7CFF
fcstab(144) = $9489
fcstab(145) = $8500
fcstab(146) = $B79B
fcstab(147) = $A612
fcstab(148) = $D2AD
fcstab(149) = $C324
fcstab(150) = $F1BF
fcstab(151) = $E036
fcstab(152) = $18C1
fcstab(153) = $948
fcstab(154) = $3BD3
fcstab(155) = $2A5A
fcstab(156) = $5EE5
fcstab(157) = $4F6C
fcstab(158) = $7DF7
fcstab(159) = $6C7E
fcstab(160) = $A50A
fcstab(161) = $B483
fcstab(162) = $8618
fcstab(163) = $9791
fcstab(164) = $E32E
fcstab(165) = $F2A7
fcstab(166) = $C03C
fcstab(167) = $D1B5
fcstab(168) = $2942
fcstab(169) = $38CB
fcstab(170) = $A50
fcstab(171) = $1BD9
fcstab(172) = $6F66
fcstab(173) = $7EEF
fcstab(174) = $4C74
fcstab(175) = $5DFD
fcstab(176) = $B58B
fcstab(177) = $A402
fcstab(178) = $9699
fcstab(179) = $8710
fcstab(180) = $F3AF
fcstab(181) = $E226
fcstab(182) = $D0BD
fcstab(183) = $C134
fcstab(184) = $39C3
fcstab(185) = $284A
fcstab(186) = $1AD1
fcstab(187) = $B58
fcstab(188) = $7FE7
fcstab(189) = $6E6E
fcstab(190) = $5CF5
fcstab(191) = $4D7C
fcstab(192) = $C60C
fcstab(193) = $D785
fcstab(194) = $E51E
fcstab(195) = $F497
fcstab(196) = $8028
fcstab(197) = $91A1
fcstab(198) = $A33A
fcstab(199) = $B2B3
fcstab(200) = $4A44
fcstab(201) = $5BCD
fcstab(202) = $6956
fcstab(203) = $78DF
fcstab(204) = $C60
fcstab(205) = $1DE9
fcstab(206) = $2F72
fcstab(207) = $3EFB
fcstab(208) = $D68D
fcstab(209) = $C704
fcstab(210) = $F59F
fcstab(211) = $E416
fcstab(212) = $90A9
fcstab(213) = $8120
fcstab(214) = $B3BB
fcstab(215) = $A232
fcstab(216) = $5AC5
fcstab(217) = $4B4C
fcstab(218) = $79D7
fcstab(219) = $685E
fcstab(220) = $1CE1
fcstab(221) = $D68
fcstab(222) = $3FF3
fcstab(223) = $2E7A
fcstab(224) = $E70E
fcstab(225) = $F687
fcstab(226) = $C41C
fcstab(227) = $D595
fcstab(228) = $A12A
fcstab(229) = $B0A3
fcstab(230) = $8238
fcstab(231) = $93B1
fcstab(232) = $6B46
fcstab(233) = $7ACF
fcstab(234) = $4854
fcstab(235) = $59DD
fcstab(236) = $2D62
fcstab(237) = $3CEB
fcstab(238) = $E70
fcstab(239) = $1FF9
fcstab(240) = $F78F
fcstab(241) = $E606
fcstab(242) = $D49D
fcstab(243) = $C514
fcstab(244) = $B1AB
fcstab(245) = $A022
fcstab(246) = $92B9
fcstab(247) = $8330
fcstab(248) = $7BC7
fcstab(249) = $6A4E
fcstab(250) = $58D5
fcstab(251) = $495C
fcstab(252) = $3DE3
fcstab(253) = $2C6A
fcstab(254) = $1EF1
fcstab(255) = $F78

; The initial FCS value
fcs = pppinitfcs16

fcs1.l
fcs2.l
fcs3.l

; evaluate the FCS
Endadresse.l=StartAddress+Size-1
For i=StartAddress To EndAdresse
mb=PeekB(i)
fcs1=XOR(mb,fcs)
fcs2=fcs1 & $FF
fcs3=fcstab(fcs2)
fcs4=fcs/$100
fcs=XOR(fcs3,fcs4)
;fcs = (fcs \ &H100&) Xor fcstab((fcs Xor cp(i)) And &HFF&) <-VB-Routine

;MessageRequester("Info",Str(mb)+":"+Str(fcs1)+":"+Str(fcs2)+":"+Str(fcs3)+":"+Str(fcs4),0)
Next
; Return the result
ProcedureReturn fcs
EndProcedure


If ReadFile(0, "c:\test.bmp")
FileLength = Lof()
If FileLength
Startadress=AllocateMemory(0, FileLength , 0)
ReadData(UseMemory(0), FileLength) ; Read the whole file in the memory buffer
Result=CRC16(Startadress,FileLength)
MessageRequester("Info","CRC16 is" + Chr(13) + Str(Result),0)
FreeMemory(0)
EndIf
CloseFile(0)
EndIf

Getting better with a little help from my friends....thx Siggi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

That's a nice tip ! CRC error check can be very useful...

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.
That's a nice tip ! CRC error check can be very useful...

Fred - AlphaSND
specially for Errorchecking(forthcoming xTract-version).
oh thx fred. at home i will implement CRC32.It can be faster.


Getting better with a little help from my friends....thx Siggi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by gnozal.

Here is an example of CRC32 code (not from me...) . I have tried it with PowerBasic and it works fine.
It should not be too hard for you guys to translate it to PureBasic !!!

'=============================================================================
'
' CRC32 - Calculate a CCITT 32-bit for a buffer
' Copyright (c) 1998 by PowerBASIC, Inc.
'
' This source will compile unmodified in PB/CC. The Crc32 function itself
' is compatible with PB/DLL 5.0.
'
'=============================================================================

FUNCTION Crc32(BYVAL Address AS DWORD, _
BYVAL Length AS LONG, _
BYVAL Seed AS LONG) AS LONG

! push EBX ; save EBX for PowerBASIC
! mov EDI,Address ; address in EDI
! mov ECX,Length ; length in ECX
! jecxz CrcDone ; exit is zero length
! cld ; clear the direction flag

BuildCRC:
! movzx EBX, Byte Ptr [EDI] ; get a char
! mov AX, Seed[1] ; get 2nd and 3rd bytes of seed
! xor DX, DX ; clear DX
! mov DL, Seed[3] ; get 4th byte of seed
! xor BL, Seed[0] ; xor char against first byte of seed
! xor BH, BH ; clear BH
! shl BX, 2 ; shift the index
! xor AX, CrcTable[EBX] ; xor low-half against the table
! xor DX, CrcTable[EBX+2] ; xor high-half against the table
! mov Seed[0], AX ; save the result
! mov Seed[2], DX ; ...
! inc EDI ; move to next char
! loop BuildCRC ; do ECX times

CrcDone:
! pop EBX ; restore EBX

FUNCTION = Seed

EXIT FUNCTION

CrcTable:
! 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&

END FUNCTION

FUNCTION PbMain() AS LONG

LOCAL Buffer AS STRING
LOCAL Crc AS LONG
LOCAL Filename AS STRING

PRINT "CRC32.EXE - Calculate 32-bit CRC of a file"
PRINT "Copyright (c) 1998 by PowerBASIC, Inc."
PRINT

Filename = COMMAND$

IF LEN(DIR$(COMMAND$)) = 0 THEN
PRINT "Usage: CRC32 filename"
EXIT FUNCTION
END IF

OPEN Filename FOR BINARY AS #1
GET$ 1, LOF(1), Buffer
CLOSE 1

Crc = Crc32(STRPTR(Buffer), LEN(Buffer), -1)

Crc = Crc XOR -1& 'compute PKZIP/ZMODEM compatible CRC result

PRINT "CRC32 for " & Filename & " is "; HEX$(Crc)

END FUNCTION
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.
Here is an example of CRC32 code (not from me...) . I have tried it with PowerBasic and it works fine.
It should not be too hard for you guys to translate it to PureBasic !!!
well, i have both (Power and Pure) so there is no reason not to convert them, maybe for a lib.Or fred did it.
stay tuned.

Getting better with a little help from my friends....thx Siggi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

I think this is a correct translation. Could someone check if the CRC result is correct? (I don't know what the hell is CRC).

Code: Select all

;=============================================================================
;
; CRC32 - Calculate a CCITT 32-bit For a buffer
; Copyright (c) 1998 by PowerBASIC, Inc.
; Translated to PureBasic by El_Choni
;
; This source will compile unmodified in PureBasic 2.90
;
;=============================================================================

Global Address.l, Length.l, Seed.l, number.l, operand.l

Procedure.l Crc32(Address.l, Length.l, Seed.l)
! mov edi, [v_Address]     ; address in EDI
! mov ecx, [v_Length]      ; length in ecx
! jecxz CrcDone            ; exit is zero length
! cld                      ; clear the direction flag
!BuildCRC:
! movzx ebx, byte [edi]    ; get a char
! mov ax, [v_Seed+1]       ; get 2nd and 3rd bytes of seed
! xor dx, dx               ; clear DX
! mov dl, [v_Seed]         ; get 4th byte of seed
! xor bl, [v_Seed+3]       ; xor char against first byte of seed
! xor bh, bh               ; clear BH
! shl bx, 2                ; shift the index
! xor ax, [CrcTable+ebx]   ; xor low-half against the table
! xor dx, [CrcTable+ebx+2] ; xor high-half against the table
! mov [v_Seed+2], ax       ; save the result
! mov [v_Seed], dx         ; ...
! inc edi                  ; move to next char
! loop BuildCRC            ; do ECX times
!CrcDone:
ProcedureReturn Seed
EndProcedure

Procedure.l Xor(number.l, operand.l)
! mov eax, [v_number]
! mov edx, [v_operand]
! xor eax, edx
! mov [v_number], eax
  ProcedureReturn number
EndProcedure

!jmp EndTable
!CrcTable:
! dd $000000000, $077073096, $0EE0E612C, $0990951BA
! dd $0076DC419, $0706AF48F, $0E963A535, $09E6495A3
! dd $00EDB8832, $079DCB8A4, $0E0D5E91E, $097D2D988
! dd $009B64C2B, $07EB17CBD, $0E7B82D07, $090BF1D91
! dd $01DB71064, $06AB020F2, $0F3B97148, $084BE41DE
! dd $01ADAD47D, $06DDDE4EB, $0F4D4B551, $083D385C7
! dd $0136C9856, $0646BA8C0, $0FD62F97A, $08A65C9EC
! dd $014015C4F, $063066CD9, $0FA0F3D63, $08D080DF5
! dd $03B6E20C8, $04C69105E, $0D56041E4, $0A2677172
! dd $03C03E4D1, $04B04D447, $0D20D85FD, $0A50AB56B
! dd $035B5A8FA, $042B2986C, $0DBBBC9D6, $0ACBCF940
! dd $032D86CE3, $045DF5C75, $0DCD60DCF, $0ABD13D59
! dd $026D930AC, $051DE003A, $0C8D75180, $0BFD06116
! dd $021B4F4B5, $056B3C423, $0CFBA9599, $0B8BDA50F
! dd $02802B89E, $05F058808, $0C60CD9B2, $0B10BE924
! dd $02F6F7C87, $058684C11, $0C1611DAB, $0B6662D3D
! dd $076DC4190, $001DB7106, $098D220BC, $0EFD5102A
! dd $071B18589, $006B6B51F, $09FBFE4A5, $0E8B8D433
! dd $07807C9A2, $00F00F934, $09609A88E, $0E10E9818
! dd $07F6A0DBB, $0086D3D2D, $091646C97, $0E6635C01
! dd $06B6B51F4, $01C6C6162, $0856530D8, $0F262004E
! dd $06C0695ED, $01B01A57B, $08208F4C1, $0F50FC457
! dd $065B0D9C6, $012B7E950, $08BBEB8EA, $0FCB9887C
! dd $062DD1DDF, $015DA2D49, $08CD37CF3, $0FBD44C65
! dd $04DB26158, $03AB551CE, $0A3BC0074, $0D4BB30E2
! dd $04ADFA541, $03DD895D7, $0A4D1C46D, $0D3D6F4FB
! dd $04369E96A, $0346ED9FC, $0AD678846, $0DA60B8D0
! dd $044042D73, $033031DE5, $0AA0A4C5F, $0DD0D7CC9
! dd $05005713C, $0270241AA, $0BE0B1010, $0C90C2086
! dd $05768B525, $0206F85B3, $0B966D409, $0CE61E49F
! dd $05EDEF90E, $029D9C998, $0B0D09822, $0C7D7A8B4
! dd $059B33D17, $02EB40D81, $0B7BD5C3B, $0C0BA6CAD
! dd $0EDB88320, $09ABFB3B6, $003B6E20C, $074B1D29A
! dd $0EAD54739, $09DD277AF, $004DB2615, $073DC1683
! dd $0E3630B12, $094643B84, $00D6D6A3E, $07A6A5AA8
! dd $0E40ECF0B, $09309FF9D, $00A00AE27, $07D079EB1
! dd $0F00F9344, $08708A3D2, $01E01F268, $06906C2FE
! dd $0F762575D, $0806567CB, $0196C3671, $06E6B06E7
! dd $0FED41B76, $089D32BE0, $010DA7A5A, $067DD4ACC
! dd $0F9B9DF6F, $08EBEEFF9, $017B7BE43, $060B08ED5
! dd $0D6D6A3E8, $0A1D1937E, $038D8C2C4, $04FDFF252
! dd $0D1BB67F1, $0A6BC5767, $03FB506DD, $048B2364B
! dd $0D80D2BDA, $0AF0A1B4C, $036034AF6, $041047A60
! dd $0DF60EFC3, $0A867DF55, $0316E8EEF, $04669BE79
! dd $0CB61B38C, $0BC66831A, $0256FD2A0, $05268E236
! dd $0CC0C7795, $0BB0B4703, $0220216B9, $05505262F
! dd $0C5BA3BBE, $0B2BD0B28, $02BB45A92, $05CB36A04
! dd $0C2D7FFA7, $0B5D0CF31, $02CD99E8B, $05BDEAE1D
! dd $09B64C2B0, $0EC63F226, $0756AA39C, $0026D930A
! dd $09C0906A9, $0EB0E363F, $072076785, $005005713
! dd $095BF4A82, $0E2B87A14, $07BB12BAE, $00CB61B38
! dd $092D28E9B, $0E5D5BE0D, $07CDCEFB7, $00BDBDF21
! dd $086D3D2D4, $0F1D4E242, $068DDB3F8, $01FDA836E
! dd $081BE16CD, $0F6B9265B, $06FB077E1, $018B74777
! dd $088085AE6, $0FF0F6A70, $066063BCA, $011010B5C
! dd $08F659EFF, $0F862AE69, $0616BFFD3, $0166CCF45
! dd $0A00AE278, $0D70DD2EE, $04E048354, $03903B3C2
! dd $0A7672661, $0D06016F7, $04969474D, $03E6E77DB
! dd $0AED16A4A, $0D9D65ADC, $040DF0B66, $037D83BF0
! dd $0A9BCAE53, $0DEBB9EC5, $047B2CF7F, $030B5FFE9
! dd $0BDBDF21C, $0CABAC28A, $053B39330, $024B4A3A6
! dd $0BAD03605, $0CDD70693, $054DE5729, $023D967BF
! dd $0B3667A2E, $0C4614AB8, $05D681B02, $02A6F2B94
! dd $0B40BBE37, $0C30C8EA1, $05A05DF1B, $02D02EF8D
!EndTable:

PbMain:

Buffer.l
Crc.l
Filename.s
a.b

Seed = -1

If CreateMenu(0)
  MenuTitle("&File")
    MenuItem(0, "&Open")
    MenuBar()
    MenuItem(1, "&Quit")
EndIf

InitGadget(1)

If OpenWindow(0, #CW_USEDEFAULT, #CW_USEDEFAULT, 320, 160, 0, "CRC32")

  AttachMenu(0, WindowID())

  If CreateGadgetList(WindowID())
    TextGadget(0, 5, 5, WindowWidth()-10, WindowHeight()-10, "CRC32.EXE - Calculate 32-bit CRC of a file"+Chr(10)+"Copyright (c) 1998 by PowerBASIC, Inc."+Chr(10)+"Translated to PureBasic by El_Choni")
  EndIf

  Repeat

    EventID.l = WaitWindowEvent()

    Select EventID
      Case #PB_EventMenu
        Select EventMenuID()
          Case 0 ; Open
            Filename = OpenFileRequester("Open file","","All files (*.*)|*.*", 0)
            If Len(Filename) > 0
              If ReadFile(0, Filename)
                Length = FileSize(Filename)
                Address = AllocateMemory(0, Length, 0)
                FileSeek(0)
                While Eof(0) = 0
                  a = ReadByte()
                  PokeB(Address+Loc()-1, a)
                Wend
                CloseFile(0)
                number = Crc32(Address, Length, Seed)
                operand = -1
                Crc = Xor(number, operand) ;compute PKZIP/ZMODEM compatible CRC result
                MessageRequester("CRC32 for " + Filename + " is:", Hex(Crc), 0)
              EndIf
            EndIf
          Case 1 ; Quit
            Quit = 1
        EndSelect
      Case #PB_EventCloseWindow
        Quit = 1
    EndSelect
  Until Quit = 1
EndIf

End     
Bye,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.


I've just tried your code and it works well. Unfortunately it runs very slow. I wonder why and found out than your loop if very unoptimized. Just replace the above part to enjoy a very fast CRC32 generator.

Code: Select all

REPLACE:

FileSeek(0)
While Eof(0) = 0
  a = ReadByte()
  PokeB(Address+Loc()-1, a)
Wend

BY:

ReadData(Address, Length)
BTW, CRC is a checksum of all file bytes to verify if a file has been modified or not. Example: take a big text file (1 Mb), compute the CRC32 and then change only one char and computes the CRC again. You willn't get the same number so you can tell than the file has changed.



Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.
I think this is a correct translation. Could someone check if the CRC result is correct? (I don't know what the hell is CRC).
not equal, but i try to search for the difference., anyway you 're faster.


Getting better with a little help from my friends....thx Siggi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.
I think this is a correct translation. Could someone check if the CRC result is correct? (I don't know what the hell is CRC).
oh i found it .simply.Here is the correct answer:

;=============================================================================
;
; CRC32 - Calculate a CCITT 32-bit For a buffer
; Copyright (c) 1998 by PowerBASIC, Inc.
; Translated to PureBasic by El_Choni
;
; This source will compile unmodified in PureBasic 2.90
;
;=============================================================================

Global Address.l, Length.l, Seed.l, number.l, operand.l

Procedure.l Crc32(Address.l, Length.l, Seed.l)
! mov edi, [v_Address] ; address in EDI
! mov ecx, [v_Length] ; length in ecx
! jecxz CrcDone ; exit is zero length
! cld ; clear the direction flag
!BuildCRC:
! movzx ebx, byte [edi] ; get a char
! mov ax, [v_Seed+1] ; get 2nd and 3rd bytes of seed
! xor dx, dx ; clear DX
! mov dl, [v_Seed+3] ; get 4th byte of seed
! xor bl, [v_Seed] ; xor char against first byte of seed
! xor bh, bh ; clear BH
! shl bx, 2 ; shift the index
! xor ax, [CrcTable+ebx] ; xor low-half against the table
! xor dx, [CrcTable+ebx+2] ; xor high-half against the table
! mov [v_Seed], ax ; save the result
! mov [v_Seed+2], dx ; ...
! inc edi ; move to next char
! loop BuildCRC ; do ECX times
!CrcDone:
ProcedureReturn Seed
EndProcedure

Procedure.l Xor(number.l, operand.l)
! mov eax, [v_number]
! mov edx, [v_operand]
! xor eax, edx
! mov [v_number], eax
ProcedureReturn number
EndProcedure

!jmp EndTable
!CrcTable:
! dd $000000000, $077073096, $0EE0E612C, $0990951BA
! dd $0076DC419, $0706AF48F, $0E963A535, $09E6495A3
! dd $00EDB8832, $079DCB8A4, $0E0D5E91E, $097D2D988
! dd $009B64C2B, $07EB17CBD, $0E7B82D07, $090BF1D91
! dd $01DB71064, $06AB020F2, $0F3B97148, $084BE41DE
! dd $01ADAD47D, $06DDDE4EB, $0F4D4B551, $083D385C7
! dd $0136C9856, $0646BA8C0, $0FD62F97A, $08A65C9EC
! dd $014015C4F, $063066CD9, $0FA0F3D63, $08D080DF5
! dd $03B6E20C8, $04C69105E, $0D56041E4, $0A2677172
! dd $03C03E4D1, $04B04D447, $0D20D85FD, $0A50AB56B
! dd $035B5A8FA, $042B2986C, $0DBBBC9D6, $0ACBCF940
! dd $032D86CE3, $045DF5C75, $0DCD60DCF, $0ABD13D59
! dd $026D930AC, $051DE003A, $0C8D75180, $0BFD06116
! dd $021B4F4B5, $056B3C423, $0CFBA9599, $0B8BDA50F
! dd $02802B89E, $05F058808, $0C60CD9B2, $0B10BE924
! dd $02F6F7C87, $058684C11, $0C1611DAB, $0B6662D3D
! dd $076DC4190, $001DB7106, $098D220BC, $0EFD5102A
! dd $071B18589, $006B6B51F, $09FBFE4A5, $0E8B8D433
! dd $07807C9A2, $00F00F934, $09609A88E, $0E10E9818
! dd $07F6A0DBB, $0086D3D2D, $091646C97, $0E6635C01
! dd $06B6B51F4, $01C6C6162, $0856530D8, $0F262004E
! dd $06C0695ED, $01B01A57B, $08208F4C1, $0F50FC457
! dd $065B0D9C6, $012B7E950, $08BBEB8EA, $0FCB9887C
! dd $062DD1DDF, $015DA2D49, $08CD37CF3, $0FBD44C65
! dd $04DB26158, $03AB551CE, $0A3BC0074, $0D4BB30E2
! dd $04ADFA541, $03DD895D7, $0A4D1C46D, $0D3D6F4FB
! dd $04369E96A, $0346ED9FC, $0AD678846, $0DA60B8D0
! dd $044042D73, $033031DE5, $0AA0A4C5F, $0DD0D7CC9
! dd $05005713C, $0270241AA, $0BE0B1010, $0C90C2086
! dd $05768B525, $0206F85B3, $0B966D409, $0CE61E49F
! dd $05EDEF90E, $029D9C998, $0B0D09822, $0C7D7A8B4
! dd $059B33D17, $02EB40D81, $0B7BD5C3B, $0C0BA6CAD
! dd $0EDB88320, $09ABFB3B6, $003B6E20C, $074B1D29A
! dd $0EAD54739, $09DD277AF, $004DB2615, $073DC1683
! dd $0E3630B12, $094643B84, $00D6D6A3E, $07A6A5AA8
! dd $0E40ECF0B, $09309FF9D, $00A00AE27, $07D079EB1
! dd $0F00F9344, $08708A3D2, $01E01F268, $06906C2FE
! dd $0F762575D, $0806567CB, $0196C3671, $06E6B06E7
! dd $0FED41B76, $089D32BE0, $010DA7A5A, $067DD4ACC
! dd $0F9B9DF6F, $08EBEEFF9, $017B7BE43, $060B08ED5
! dd $0D6D6A3E8, $0A1D1937E, $038D8C2C4, $04FDFF252
! dd $0D1BB67F1, $0A6BC5767, $03FB506DD, $048B2364B
! dd $0D80D2BDA, $0AF0A1B4C, $036034AF6, $041047A60
! dd $0DF60EFC3, $0A867DF55, $0316E8EEF, $04669BE79
! dd $0CB61B38C, $0BC66831A, $0256FD2A0, $05268E236
! dd $0CC0C7795, $0BB0B4703, $0220216B9, $05505262F
! dd $0C5BA3BBE, $0B2BD0B28, $02BB45A92, $05CB36A04
! dd $0C2D7FFA7, $0B5D0CF31, $02CD99E8B, $05BDEAE1D
! dd $09B64C2B0, $0EC63F226, $0756AA39C, $0026D930A
! dd $09C0906A9, $0EB0E363F, $072076785, $005005713
! dd $095BF4A82, $0E2B87A14, $07BB12BAE, $00CB61B38
! dd $092D28E9B, $0E5D5BE0D, $07CDCEFB7, $00BDBDF21
! dd $086D3D2D4, $0F1D4E242, $068DDB3F8, $01FDA836E
! dd $081BE16CD, $0F6B9265B, $06FB077E1, $018B74777
! dd $088085AE6, $0FF0F6A70, $066063BCA, $011010B5C
! dd $08F659EFF, $0F862AE69, $0616BFFD3, $0166CCF45
! dd $0A00AE278, $0D70DD2EE, $04E048354, $03903B3C2
! dd $0A7672661, $0D06016F7, $04969474D, $03E6E77DB
! dd $0AED16A4A, $0D9D65ADC, $040DF0B66, $037D83BF0
! dd $0A9BCAE53, $0DEBB9EC5, $047B2CF7F, $030B5FFE9
! dd $0BDBDF21C, $0CABAC28A, $053B39330, $024B4A3A6
! dd $0BAD03605, $0CDD70693, $054DE5729, $023D967BF
! dd $0B3667A2E, $0C4614AB8, $05D681B02, $02A6F2B94
! dd $0B40BBE37, $0C30C8EA1, $05A05DF1B, $02D02EF8D
!EndTable:

PbMain:

Buffer.l
Crc.l
Filename.s
a.b

Seed = -1

If CreateMenu(0)
MenuTitle("&File")
MenuItem(0, "&Open")
MenuBar()
MenuItem(1, "&Quit")
EndIf

InitGadget(1)

If OpenWindow(0, #CW_USEDEFAULT, #CW_USEDEFAULT, 320, 160, 0, "CRC32")

AttachMenu(0, WindowID())

If CreateGadgetList(WindowID())
TextGadget(0, 5, 5, WindowWidth()-10, WindowHeight()-10, "CRC32.EXE - Calculate 32-bit CRC of a file"+Chr(10)+"Copyright (c) 1998 by PowerBASIC, Inc."+Chr(10)+"Translated to PureBasic by El_Choni")
EndIf

Repeat

EventID.l = WaitWindowEvent()

Select EventID
Case #PB_EventMenu
Select EventMenuID()
Case 0 ; Open
Filename = OpenFileRequester("Open file","","All files (*.*)|*.*", 0)
If Len(Filename) > 0
If ReadFile(0, Filename)
Length = FileSize(Filename)
Address = AllocateMemory(0, Length, 0)
FileSeek(0)
;While Eof(0) = 0
; a = ReadByte()
; PokeB(Address+Loc()-1, a)
;Wend
ReadData(Address, Length)
CloseFile(0)
Seed=-1
number = Crc32(Address, Length, Seed)
;MessageRequester("Info",Hex(Seed)+Chr(13)+Hex(number),0)
operand = -1
Crc = Xor(number, operand) ;compute PKZIP/ZMODEM compatible CRC result
MessageRequester("CRC32 for " + Filename + " is:", Hex(Crc), 0)
EndIf
EndIf
Case 1 ; Quit
Quit = 1
EndSelect
Case #PB_EventCloseWindow
Quit = 1
EndSelect
Until Quit = 1
EndIf

End


Getting better with a little help from my friends....thx Siggi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

booh, ultrafast.great piece of code.

Getting better with a little help from my friends....thx Siggi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

Looks like there are some registers used
that shouldnt be modified... !?
El_Choni ??

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Hi,

If you look at the PureBasic.asm file, you'll see that, before starting the procedure code, five registers are pushed, so using them is safe.

Bye,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

Hi Rings, I tried this on my WinXp system but it just crashes. Yes, I did remember to turn on Inline ASm :):)

Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

@Fangles,
>I tried this on my WinXp system but it just crashes.
Which crc16 or crc32 crashes ?.
very strange, coz on NT4 and Win2000 everything worked fine.
Could you figured out (With single steps) on wich point it did crash ?


Getting better with a little help from my friends....thx Siggi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.
@Fangles,
>I tried this on my WinXp system but it just crashes.
Which crc16 or crc32 crashes ?.
very strange, coz on NT4 and Win2000 everything worked fine.
Could you figured out (With single steps) on wich point it did crash ?


Getting better with a little help from my friends....thx Siggi
The CRC32 code above, that's the one that crashes. Turned on debugger too but it doesn't allow me to get to the stage to select a file witht he debugger running so don't know what to do there. PB is v2.9

Fangles
Post Reply