; 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
; -------------------------------------------------------------------------


; -> R1 = filename

; During use:
; r9 = infection start

			funcName "jester_removal"

.kill_jester		mov		r0,#&C0
			;filename already in R1
			swi		XOS_Find
			bvs		exit_from_error	;failed to open file
			teq		r0,#0
			beq		exit_from_error	;failed to open file

			mov		r1,r0
			mov		r10,r0	;keep a copy of filehandle
			mov		r0,#3
			;filehandle already in r1
			adr		r2,scrap_block
			mov		r3,#4
			mov		r4,#0
			swi		XOS_GBPB
			bvs		exit_from_error	;failed to read bytes

			ldr		r0,scrap_block
			mov		r3,#&FF000000
			and		r2,r0,r3
			mov		r3,#&EB000000
			teq		r2,r3
			movne		r3,#&EA000000
			teqne		r2,r3
			bne		exit_not_jester

			ldr		r0,scrap_block

			mov		r3,#&FFFFF
			and		r2,r0,r3
			add		r0,r2,#2
			mov		r4,r0,lsl#2
			;add		r9,r0,#8

			add		r9,r4,#4	;infect start

			mov		r0,#3
			; r1 already contains filehandle
			adr		r2,scrap_block
			mov		r3,#300
			; r1 contains offset calculated above
			swi		XOS_GBPB
			bvs		exit_from_error

			adr		r2,scrap_block
			ldr		r0,[r2,#&D0]
			mov		r3,#&65722049
			teq		r0,r3
			beq		jester_null_first_word

			ldr		r0,[r2,#&A8]
			teq		r0,r3
			beq		jester_b_start

			b		jester_bl_start


.jester_null_first_word	;this routine does /not/ extract original header
			mov		r0,#&E1A00000	; mov r0,r0
			str		r0,scrap_block

			mov		r0,#1
			; r1 already contains filehandle
			; r2 already contains pointer to scrap block
			mov		r3,#4
			mov		r4,#0
			swi		XOS_GBPB
			bvs		exit_from_error

			mov		r0,#3
			; r1 already contains filehandle
			sub		r2,r9,#4
			swi		XOS_Args
			bvs		exit_from_error

			teq		r1,#0
			movne		r0,#0
			swi		XOS_Find
			ldmfd		r13!,{pc}	;finished

.jester_b_start		ldr		r0,[r2,#4]
			mov		r3,#&FF000000
			and		r4,r0,r3
			mov		r3,#&EB000000
			teq		r3,r4
			movne		r3,#&EA000000
			teqne		r3,r4
			bne		exit_not_jester

			mov		r6,#&FFFFF	;calculate offset
			and		r0,r0,r6
			add		r0,r0,#2
			mov		r0,r0,lsl#2
			add		r0,r0,r9	;add the offset
			and		r0,r0,r6	;remove spurious

			;now we need to assemble an instruction
			mov		r0,r0,lsr#2
			mov		r3,#&EA000000
			add		r0,r0,r3
			sub		r0,r0,#2
			str		r0,[r2]

			mov		r0,#1
			; r1 already contains filehandle
			; r2 already contains pointer to scrap block
			mov		r3,#4
			mov		r4,#0
			swi		XOS_GBPB
			bvs		exit_from_error

			mov		r0,#3
			; r1 already contains filehandle
			sub		r2,r9,#&34
			swi		XOS_Args
			bvs		exit_from_error

			teq		r1,#0
			movne		r0,#0
			swi		XOS_Find
			ldmfd		r13!,{pc}	;finished


.jester_bl_start	ldr		r0,[r2,#12]
			mov		r3,#&FF000000
			and		r4,r0,r3
			mov		r3,#&EB000000
			teq		r3,r4
			movne		r3,#&EA000000
			teqne		r3,r4
			bne		exit_not_jester

			mov		r6,#&FFFF
			and		r0,r0,r6
			add		r0,r0,#2
			mov		r0,r0,lsl#2
			add		r0,r0,r9	;add the offset
			and		r0,r0,r6	;remove spurious

			;now we need to assemble an instruction
			mov		r0,r0,lsr#2
			mov		r3,#&EB000000
			add		r0,r0,r3
			;sub		r0,r0,#2
			str		r0,scrap_block

			mov		r0,#1
			; r1 already contains filehandle
			; r2 already contains pointer to scrap block
			mov		r3,#4
			mov		r4,#0
			swi		XOS_GBPB
			bvs		exit_from_error

			mov		r0,#3
			; r1 already contains filehandle
			sub		r2,r9,#&35
			swi		XOS_Args
			bvs		exit_from_error

			teq		r1,#0
			movne		r0,#0
			swi		XOS_Find
			ldmfd		r13!,{pc}	;unstack and return to caller

.exit_not_jester	teq		r1,#0
			movne		r0,#0
			swine		XOS_Find

			mov		r0,#3
			ldmfd		r13!,{pc}	;unstack and return to caller



