; FPEngine code for Slayer
; Slayer - a desktop anti-virus for RISC OS
; Copyright (C) 1996 - 2000 Kiwi Software (UK)
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
;
; info@kiwisoft.co.uk
; -------------------------------------------------------------------------

; Version 0.06 last updated 03/10/99 (Luke)
; generate checksum based on first and last 256 bytes of a file

#base 0
#type &FFD
#name ^.512-CRCv2
#smile

TEMP	r12

#include "FPEng:h.aliases"
#include "SlayEng:h.versions"

; 	=> 	r1 = filename

;	<=	r0 = crc

			stmfd		r13!,{r14}

			mov		length,#0
			mov		crc,#&FFFFFFFF
			adr		table,crc_table

			mov		r0,#17
			;		r1 already contains filehandle
			swi		XOS_File
			bvs		exit_from_error
			mov		file_length,r4

			mov		r0,#&40
			swi		XOS_Find
			bvs		exit_from_error
			teq		r0,#0
			beq		exit_from_error
			mov		r1,r0

			;cmp		file_length,#512
			;if less than 512 bytes so grab all
			;movle		r3,file_length
			;movgt		r3,#512

			mov		r0,#3
			;		r1 already contains filehandle
			adr		r2,check_buffer
			mov		r3,#255
			;		r3 already contains bytes to read
			mov		r4,#0
			swi		XOS_GBPB

			;r3 contains number of bytes not transfered - if !=0 read from bottom
			teq		r3,#0
			movne		r0,#255
			subne		filelength,r0,r3

			moveq		r0,#3
			; r1 already contains the filehandle
			; r2 is buffer advanced to end of read data
			moveq		r3,#255
			subeq		r4,filelength,#255
			swieq		XOS_GBPB
			moveq		r0,#510
			subeq		filelength,r0,r3

			;cmp		file_length,#512
			;ble		find_crc

;file was bigger than 512 bytes grab the bottom 256 bytes

			;mov		r3,#256
			;sub		r4,file_length,#256
			;swi		XOS_GBPB

			;mov		file_length,#512	;max loop

.find_crc		;first close the file
			teq		r1,#0
			movne		r0,#0
			swine		XOS_Find

; actually calculate the crc

			adr		r6,check_buffer
			mov		length,#0

.crc_loop		ldrb		r0,[r6,length]
			add		length,length,#1
			teq		length,file_length
			beq		exit

			and		spare,crc,#&FF
			eor		spare,r0,spare
			ldr		spare,[table,spare, lsl#2]
			eor		crc,spare,crc,lsr #8

			b		crc_loop

.exit			mov		r0,crc
			ldmfd		r13!,{pc}	;unstack and return to caller

.exit_from_error	mvn		r0,#0
			ldmfd		r13!,{pc}	;unstack and return to caller

.check_buffer		dbb		512
			align

.crc_table		DCD &0
			DCD &77073096
			DCD &EE0E612C
			DCD &990951BA
			DCD &76DC419
			DCD &706AF48F
			DCD &E963A535
			DCD &9E6495A3
			DCD &EDB8832
			DCD &79DCB8A4
			DCD &E0D5E91E
			DCD &97D2D988
			DCD &9B64C2B
			DCD &7EB17CBD
			DCD &E7B82D07
			DCD &90BF1D91
			DCD &1DB71064
			DCD &6AB020F2
			DCD &F3B97148
			DCD &84BE41DE
			DCD &1ADAD47D
			DCD &6DDDE4EB
			DCD &F4D4B551
			DCD &83D385C7
			DCD &136C9856
			DCD &646BA8C0
			DCD &FD62F97A
			DCD &8A65C9EC
			DCD &14015C4F
			DCD &63066CD9
			DCD &FA0F3D63
			DCD &8D080DF5
			DCD &3B6E20C8
			DCD &4C69105E
			DCD &D56041E4
			DCD &A2677172
			DCD &3C03E4D1
			DCD &4B04D447
			DCD &D20D85FD
			DCD &A50AB56B
			DCD &35B5A8FA
			DCD &42B2986C
			DCD &DBBBC9D6
			DCD &ACBCF940
			DCD &32D86CE3
			DCD &45DF5C75
			DCD &DCD60DCF
			DCD &ABD13D59
			DCD &26D930AC
			DCD &51DE003A
			DCD &C8D75180
			DCD &BFD06116
			DCD &21B4F4B5
			DCD &56B3C423
			DCD &CFBA9599
			DCD &B8BDA50F
			DCD &2802B89E
			DCD &5F058808
			DCD &C60CD9B2
			DCD &B10BE924
			DCD &2F6F7C87
			DCD &58684C11
			DCD &C1611DAB
			DCD &B6662D3D
			DCD &76DC4190
			DCD &1DB7106
			DCD &98D220BC
			DCD &EFD5102A
			DCD &71B18589
			DCD &6B6B51F
			DCD &9FBFE4A5
			DCD &E8B8D433
			DCD &7807C9A2
			DCD &F00F934
			DCD &9609A88E
			DCD &E10E9818
			DCD &7F6A0DBB
			DCD &86D3D2D
			DCD &91646C97
			DCD &E6635C01
			DCD &6B6B51F4
			DCD &1C6C6162
			DCD &856530D8
			DCD &F262004E
			DCD &6C0695ED
			DCD &1B01A57B
			DCD &8208F4C1
			DCD &F50FC457
			DCD &65B0D9C6
			DCD &12B7E950
			DCD &8BBEB8EA
			DCD &FCB9887C
			DCD &62DD1DDF
			DCD &15DA2D49
			DCD &8CD37CF3
			DCD &FBD44C65
			DCD &4DB26158
			DCD &3AB551CE
			DCD &A3BC0074
			DCD &D4BB30E2
			DCD &4ADFA541
			DCD &3DD895D7
			DCD &A4D1C46D
			DCD &D3D6F4FB
			DCD &4369E96A
			DCD &346ED9FC
			DCD &AD678846
			DCD &DA60B8D0
			DCD &44042D73
			DCD &33031DE5
			DCD &AA0A4C5F
			DCD &DD0D7CC9
			DCD &5005713C
			DCD &270241AA
			DCD &BE0B1010
			DCD &C90C2086
			DCD &5768B525
			DCD &206F85B3
			DCD &B966D409
			DCD &CE61E49F
			DCD &5EDEF90E
			DCD &29D9C998
			DCD &B0D09822
			DCD &C7D7A8B4
			DCD &59B33D17
			DCD &2EB40D81
			DCD &B7BD5C3B
			DCD &C0BA6CAD
			DCD &EDB88320
			DCD &9ABFB3B6
			DCD &3B6E20C
			DCD &74B1D29A
			DCD &EAD54739
			DCD &9DD277AF
			DCD &4DB2615
			DCD &73DC1683
			DCD &E3630B12
			DCD &94643B84
			DCD &D6D6A3E
			DCD &7A6A5AA8
			DCD &E40ECF0B
			DCD &9309FF9D
			DCD &A00AE27
			DCD &7D079EB1
			DCD &F00F9344
			DCD &8708A3D2
			DCD &1E01F268
			DCD &6906C2FE
			DCD &F762575D
			DCD &806567CB
			DCD &196C3671
			DCD &6E6B06E7
			DCD &FED41B76
			DCD &89D32BE0
			DCD &10DA7A5A
			DCD &67DD4ACC
			DCD &F9B9DF6F
			DCD &8EBEEFF9
			DCD &17B7BE43
			DCD &60B08ED5
			DCD &D6D6A3E8
			DCD &A1D1937E
			DCD &38D8C2C4
			DCD &4FDFF252
			DCD &D1BB67F1
			DCD &A6BC5767
			DCD &3FB506DD
			DCD &48B2364B
			DCD &D80D2BDA
			DCD &AF0A1B4C
			DCD &36034AF6
			DCD &41047A60
			DCD &DF60EFC3
			DCD &A867DF55
			DCD &316E8EEF
			DCD &4669BE79
			DCD &CB61B38C
			DCD &BC66831A
			DCD &256FD2A0
			DCD &5268E236
			DCD &CC0C7795
			DCD &BB0B4703
			DCD &220216B9
			DCD &5505262F
			DCD &C5BA3BBE
			DCD &B2BD0B28
			DCD &2BB45A92
			DCD &5CB36A04
			DCD &C2D7FFA7
			DCD &B5D0CF31
			DCD &2CD99E8B
			DCD &5BDEAE1D
			DCD &9B64C2B0
			DCD &EC63F226
			DCD &756AA39C
			DCD &26D930A
			DCD &9C0906A9
			DCD &EB0E363F
			DCD &72076785
			DCD &5005713
			DCD &95BF4A82
			DCD &E2B87A14
			DCD &7BB12BAE
			DCD &CB61B38
			DCD &92D28E9B
			DCD &E5D5BE0D
			DCD &7CDCEFB7
			DCD &BDBDF21
			DCD &86D3D2D4
			DCD &F1D4E242
			DCD &68DDB3F8
			DCD &1FDA836E
			DCD &81BE16CD
			DCD &F6B9265B
			DCD &6FB077E1
			DCD &18B74777
			DCD &88085AE6
			DCD &FF0F6A70
			DCD &66063BCA
			DCD &11010B5C
			DCD &8F659EFF
			DCD &F862AE69
			DCD &616BFFD3
			DCD &166CCF45
			DCD &A00AE278
			DCD &D70DD2EE
			DCD &4E048354
			DCD &3903B3C2
			DCD &A7672661
			DCD &D06016F7
			DCD &4969474D
			DCD &3E6E77DB
			DCD &AED16A4A
			DCD &D9D65ADC
			DCD &40DF0B66
			DCD &37D83BF0
			DCD &A9BCAE53
			DCD &DEBB9EC5
			DCD &47B2CF7F
			DCD &30B5FFE9
			DCD &BDBDF21C
			DCD &CABAC28A
			DCD &53B39330
			DCD &24B4A3A6
			DCD &BAD03605
			DCD &CDD70693
			DCD &54DE5729
			DCD &23D967BF
			DCD &B3667A2E
			DCD &C4614AB8
			DCD &5D681B02
			DCD &2A6F2B94
			DCD &B40BBE37
			DCD &C30C8EA1
			DCD &5A05DF1B
			DCD &2D02EF8D

.copyright_string	dcb		" Kiwi Software (UK), 1999",0
			align

.version_text		dcb		"512-CRC v2",0
			align

.copyright_str_pointer	dcd		copyright_string

.infoblocks_date	dcd		0	;will be filled in when cert
			dcd		0	;with UTC time

.plugin_reserved	dcd		0

.version_text_pointer	dcd		version_text

.infoblocks_version	dcd		FP_512CRCv2_Version

.plugin_crc		dcd		0

.plugin_header		dcb		"SLAY"
