;------------------------------------------------------------
;	An assembler example of showing the use
;	of the 'removelink.library'.
;	Will assemble correctly with most assemblers,
;	supporting local labels.
;
;	This will examine a file, and check if the file is
;	executable, data or infected.
;	Will just check for virusnames. Not remove the
;	viruses.
;
;	This pice of code is copyright (C) Johan Ohman 1993
;
;------------------------------------------------------------

	incdir	'df0:include/'

	include	'exec/exec_lib.i'
	include	'exec/memory.i'
	include	'exec/tasks.i'
	include	'libraries/dos_lib.i'
	include	'libraries/dos.i'

	include	'RLL/removelink_lib.i'
	include	'RLL/removelink.i'

call:	macro
	jsr	_LVO\1(a6)
	endm

MaxFileSize=	$10000	;( <= 65535 bytes.)
HUNK_HEADER=	$3F3


S:	movem.l	d0-a6,-(sp)

	move.l	(4).W,a6	;This is if we were called from WB
	suba.l	a1,a1
	call	FindTask
	move.l	d0,a4
	tst.l	140(a4)
	bne.b	.FromCLI

	lea.l	92(a4),a0
	call	WaitPort

	lea.l	92(a4),a0
	call	GetMsg
.FromCLI:
	movem.l	(sp)+,d0-a6

	clr.b	-1(a0,d0.w)
	move.l	d0,Argc
	move.l	a0,Argv

	subq.w	#1,d0
	beq.w	NoArgs

	move.l	(4).W,a6
	move.l	#MEMF_PUBLIC!MEMF_CLEAR,d1
	move.l	#MaxFileSize,d0
	call	AllocMem
	tst.l	d0
	beq.w	NoMem
	move.l	d0,FileMemPtr

	move.l	(4).W,a6
	moveq.l	#32,d0
	lea.l	RLLib(pc),a1
	call	OpenLibrary
	tst.l	d0
	beq.w	NoRLLib
	move.l	d0,RLBase

	moveq.l	#33,d0		;Kick 1.2 +
	lea.l	DosLib(pc),a1
	call	OpenLibrary
	tst.l	d0
	beq.w	NoDosLib
	move.l	d0,DosBase

	move.l	RLBase(pc),a6
	call	AllocRLMem
	tst.l	d0
	beq.w	Quit
	move.l	d0,RLTab

	move.l	DosBase(pc),a6
	move.l	Argv(pc),d1
	move.l	#MODE_OLDFILE,d2
	call	Open
	tst.l	d0
	beq.w	Quit
	move.l	d0,FHandler

	move.l	FHandler(pc),d1
	move.l	FileMemPtr(pc),d2
	move.l	#MaxFileSize,d3
	call	read
	move.l	d0,FileSize	;Number of bytes read.

	move.l	FHandler(pc),d1
	call	close

	move.l	FileMemPtr(pc),a0
	cmp.l	#HUNK_HEADER,(a0)
	bne.w	NotExec

	move.l	FileSize(pc),d0
	cmp.l	#MaxFileSize,d0
	beq.w	TooBig

	move.l	RLTab(pc),a0
	move.l	FileMemPtr(pc),RL_BufferPtr(a0)
	move.l	FileSize,RL_BufferLen(a0)
	move.l	RLBase(pc),a6
	call	GetFileVName

	tst.l	RL_FileInfection(a0)
	beq.b	NoInfection

	move.l	RL_FileInfection(a0),a0
	lea.l	ThisFINode(pc),a1
	move.l	a0,(a1)
	bsr.b	Infection

	tst.l	FI_NextNode(a0)
	beq.b	NoNextFINode

NextFINode:
	move.l	FI_NextNode(a0),(a1)
	bsr.b	Infection
	tst.l	(a1)
	bne.b	NextFINode

NoNextFINode:
	bra.b	Quit

NoRLLib:
	pea.l	RLLib(pc)
	pea.l	_NoLib(pc)
	bsr.w	printF
	addq.w	#8,Sp
	bra.b	Quit

NoDosLib:
	pea.l	DosLib(pc)
	pea.l	_NoLib(pc)
	bsr.w	printF
	addq.w	#8,Sp
	bra.b	Quit

NoMem:
	move.l	#MaxFileSize,-(sp)
	move.l	#MaxFileSize,-(sp)
	pea.l	_NotEnoughMem
	bsr.w	printF
	lea.l	12(sp),sp
	bra.b	Quit

NoArgs:	pea.l	_NoArgs(pc)
	bsr.w	printF
	addq.w	#4,Sp
	rts

NotExec:
	pea.l	_NotExecutable(pc)
	bsr.b	printF
	addq.w	#4,Sp
	bra.b	Quit

NoInfection:
	move.l	Argv(pc),-(sp)
	pea.l	_NotInfected(pc)
	bsr.b	printF
	addq.w	#8,Sp
	bra.b	Quit

Infection:
	move.l	ThisFINode(pc),a6
	move.l	FI_VirusName(a6),-(sp)

	pea.l	_Infected(pc)
	bsr.b	printF
	addq.w	#8,Sp
	rts

TooBig:
	pea.l	_FileTooBig(pc)
	bsr.b	printF
	addq.w	#4,Sp
	bra.w	Quit

Quit:	move.l	FileMemPtr(pc),d0
	beq.b	.NoMemAllocated
	move.l	d0,a1
	move.l	#MaxFileSize,d0
	move.l	(4).W,a6
	call	FreeMem

.NoMemAllocated:
	move.l	RLBase(pc),d0
	tst.l	d0
	beq.b	.NoRLTab	;Don't use the library if there is no... 
	move.l	d0,a6		;ie. don't provoke a Guru ...

	move.l	RLTab(pc),d0
	beq.b	.NoRLTab
	move.l	d0,a0
	call	FreeRLMem

.NoRLTab:
	move.l	(4).W,a6

	move.l	DosBase(pc),d0
	beq.b	.NotDos
	move.l	d0,a1
	call	CloseLibrary

.NotDos:
	move.l	RLBase(pc),d0
	beq.b	.NotRL
	move.l	d0,a1
	call	CloseLibrary
.NotRL:	rts

printF:
	movem.l	d0-a6,-(Sp)
	move.l	(4).W,a6
	move.l	15*4+4*1(Sp),a0
	lea.l	15*4+4*2(Sp),a1
	lea.l	.SubPrintF(pc),a2
	link	a5,#-512
	move.l	a5,a3
	lea.l	-512(a3),a3
	call	RawDoFmt

	movem.l	d0-a6,-(sp)
	lea.l	-512(a5),a5
	move.l	(4).W,a6
	lea.l	DosLib(pc),a1
	call	OldOpenLibrary
	move.l	d0,a6
	jsr	-60(a6)
	move.l	d0,d1
	move.l	a5,d2
.1:	tst.b	(a5)+
	bne.b	.1
	move.l	a5,d3
	sub.l	d2,d3
	jsr	-48(a6)
	move.l	a6,a1
	move.l	(4).W,a6
	jsr	-414(a6)
	movem.l	(Sp)+,d0-a6
	unlk	a5
	movem.l	(Sp)+,d0-a6
	rts

.SubPrintF:
	move.b	d0,(a3)+
	rts

DosBase:	dc.l	0
RLBase:		dc.l	0
RLTab:		dc.l	0
Argc:		dc.l	0
Argv:		dc.l	0
ThisFINode:	dc.l	0
FileMemPtr:	dc.l	0
FileSize:	dc.l	0
FHandler:	dc.l	0

_NoArgs:	dc.b	'No args ...',10,0
_NoLib:		dc.b	'No %s ',10,0
_NotEnoughMem:	dc.b	'Sorry ! Not enough memory available '
		dc.b	'(bytes needed %ld/$%lx )',10,0
_NotInfected:	dc.b	'The file ''%s'' is not infected ... ',10,0
_Infected:	dc.b	'The file is infected by the ',$9b,'1;31;42m'
		dc.b	'%s',$9b,'0;31;40m - virus',10,0
_NotExecutable:	dc.b	'This file is not a programme [executable] file.',10,0
_FileTooBig:	dc.b	'The file is too big to fit into the allocated memory '
		dc.b	'block...',10,0

RLLib:		dc.b	'removelink.library',0
DosLib:		dc.b	'dos.library',0
		even
