

HunkHeader	dc.l	HUNK_HEADER,0,1,0,0
CodeLength	dc.l	0,HUNK_CODE,0
HeaderLen	EQU	*-HunkHeader


dmain	sf	-1(a0,d0.w)
	move.l	a0,-(sp)
	IFNE	ASMONE
	move.l	SysBase,a6
	ELSE
	move.l	SysBase.w,a6
	ENDC
	move.l	ThisTask(a6),a4
	tst.l	pr_CLI(a4)
	bne.s	dShellStart
	lea	pr_MsgPort(a4),a0
	CALL	WaitPort
	lea	pr_MsgPort(a4),a0
	CALL	GetMsg
	move.l	d0,a1
	addq.b	#1,IDNestCnt(a6)
	CALL	ReplyMsg
dError	addq.w	#4,sp
	moveq	#0,d0
	rts


dShellStart	moveq	#OS204,d0
	cmp.w	LIB_VERSION(a6),d0
	bgt.s	dError


LoopLength	move.l	#$99999999,d2
	lea	Data(pc),a1
	move.l	a1,a4
	move.l	d2,d4
	lsr.l	#2,d2
	move.l	d2,d5
DecryptPass1	moveq	#0,d6	;dest bits
	moveq	#32-1,d3	;num bits
	move.l	(a1),d0
	swap	d0
	bra.s	.2
.1	rol.l	#1,d0
.2	move.l	d0,d1
	and.l	#%00000000000000000000000000000001,d1
	or.l	d1,d6
	ror.l	#1,d6
	dbra	d3,.1
	move.l	d6,(a1)+
	subq.l	#1,d2
	bne.s	DecryptPass1

RandomKey	move.b	#$99,d3
DecryptLoop	move.b	(a4),d1
	sub.b	d3,d1
	eor.b	#$66,d1
	not.b	d1
	moveq	#"T",d6
	sub.b	d6,d1
	moveq	#"L",d6
	sub.b	d6,d1
	moveq	#"S",d6
	sub.b	d6,d1
	bchg	#1,d1
	ror.b	#2,d1
	not.b	d1
	move.b	d1,(a4)+
	subq.b	#1,d3
	subq.l	#1,d4
	bne.s	DecryptLoop

MakeChecksum	add.l	-(a4),d4
	subq.l	#1,d5
	bne.s	MakeChecksum
Checksum	cmp.l	#$99999999,d4
	bne.s	dError

	move.l	a4,a0
	cmp.l	#"CRU!",(a0)+
	bne.s	.3
	move.l	a0,a1
	add.l	4(a1),a0
	lea	3*4(a0),a0
	bsr	rUnpack
	move.l	a0,a4

.3	lea	ARexxPortName(pc),a1
	CALL	FindPort
	move.l	d0,d7
	beq	dError

	lea	RexxSysName(pc),a1
	moveq	#36,d0
	CALL	OpenLibrary
	move.l	d0,d6
	beq	dError

	CALL	CreateMsgPort
	move.l	d0,d5
	beq.s	MsgPortError

	move.l	d0,a0
	sub.l	a1,a1
	moveq	#0,d0
	move.l	d6,a6
	CALL	CreateRexxMsg
	move.l	d0,d4
	beq.s	NoRexxMsg

	move.l	a4,rm_Args(a0)
	move.l	(sp),rm_Args+4(a0)
	moveq	#2,d0
	moveq	#0,d1
	CALL	FillRexxMsg
	tst.l	d0
	beq.s	FillMsgError

	move.l	d4,a0
	move.l	#RXFUNC+RXFF_STRING+1,rm_Action(a0)
	IFNE	ASMONE
	move.l	SysBase,a6
	ELSE
	move.l	SysBase.w,a6
	ENDC
	move.l	d7,a0
	move.l	d4,a1
	CALL	PutMsg
	move.l	d5,a0
	CALL	WaitPort

FillMsgError	move.l	d6,a6
	move.l	d4,a0
	moveq	#2,d0
	CALL	ClearRexxMsg
	move.l	d4,a0
	CALL	DeleteRexxMsg
NoRexxMsg	move.l	d5,a0
	IFNE	ASMONE
	move.l	SysBase,a6
	ELSE
	move.l	SysBase.w,a6
	ENDC
	CALL	DeleteMsgPort
MsgPortError	move.l	d6,a1
	CALL	CloseLibrary

LoopLength2	move.l	#$99999999,d0
.4	sf	(a4)+
	subq.l	#1,d0
	bne.s	.4
	bra	dError

	INCLUDE	"RC:rcincludes/s404_random.asm"

	dc.w	0
RexxSysName	dc.b	"rexxsyslib.library",0
ARexxPortName	ds.b	20+1
Data
dhl	EQU	*-dmain
HUNKLENGTH	EQU	(dhl>>2+1)+3	;+null terminator
HunkEnd	dc.l	0	;null terminated for script
	dc.l	HUNK_END
