

ShellHeader	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	sShellStart
	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
	bra.s	sError
sErrorInput	lea	100(sp),sp
sError	addq.w	#4,sp
	moveq	#0,d0
	rts

sShellStart	moveq	#OS204,d0
	cmp.w	LIB_VERSION(a6),d0
	bgt.s	sError

	lea	sDosName(pc),a1
	CALL	OpenLibrary
	tst.l	d0
	beq.s	sError

	move.l	d0,a6
	move.l	pr_COS(a4),d1
	lea	sPrompt(pc),a0
	move.l	a0,d2
	moveq	#10,d3
	CALL	Write

	lea	-100(sp),sp
	move.l	pr_CIS(a4),d1
	move.l	sp,d2
	moveq	#100,d3
	CALL	Read
	subq.l	#1,d0
	beq.s	sErrorInput
	move.l	d0,d4
	moveq	#100,d1
	cmp.l	d1,d0
	bge.s	sErrorInput
	sf	(sp,d0.w)

sDecrypt	lea	sData(pc),a1
	move.l	a1,a4
sLoopLength	move.l	#$99999999,d1
	move.l	sp,a2	;restore register
	move.l	a2,a3	;work register
	add.l	d0,a2	;eop
sDecryptLoop	move.b	(a1),d2
	move.b	(a3)+,d3
	move.b	-1(sp,d0.w),d4
	eor.b	d4,d2
	not.b	d2
	eor.b	#$23,d2
	rol.b	#1,d2
	eor.b	d3,d2
	move.b	d2,(a1)+
	cmp.l	a3,a2
	bne.s	sNoReset
	move.l	sp,a3
sNoReset	subq.l	#1,d1
	bne.s	sDecryptLoop


sCheckCrunch	lea	sCryptJSR(pc),a0
	move.b	(sp),d0
	move.b	1(sp),d1
	eor.b	d0,(a0)
	eor.b	d0,1(a0)
	eor.b	d1,d0
	eor.b	d0,(a0)
	lea	100(sp),sp
	move.l	a6,a1
	IFNE	ASMONE
	move.l	SysBase,a6
	ELSE
	move.l	SysBase.w,a6
	ENDC
	CALL	CloseLibrary
	CALL	CacheClearU
	move.l	a4,a1
	cmp.l	#"CRU!",(a1)+
	bne.s	sGetPort
	move.l	a1,a0
	add.l	4(a1),a0
	lea	3*4(a0),a0
	bsr	sDepack
	move.l	a0,a4

sGetPort	lea	sARexxPortName(pc),a1
sCryptJSR	CALL	FindPort
	move.l	d0,d7
	beq	sError

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

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

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

	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	sFillMsgError

	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

sFillMsgError	move.l	d6,a6
	move.l	d4,a0
	moveq	#2,d0
	CALL	ClearRexxMsg
	move.l	d4,a0
	CALL	DeleteRexxMsg
sNoRexxMsg	move.l	d5,a0
	IFNE	ASMONE
	move.l	SysBase,a6
	ELSE
	move.l	SysBase.w,a6
	ENDC
	CALL	DeleteMsgPort
sMsgPortError	move.l	d6,a1
	CALL	CloseLibrary
	bra	sError

	INCLUDE	"RC:rcincludes/s404_pwshel.asm"

sDosName	dc.b	"dos.library",0
sRexxSysName	dc.b	"rexxsyslib.library",0
sPrompt	dc.b	"Password: "
sARexxPortName	ds.b	20+1
sData
shl	EQU	*-ShellHeader
sHUNKLENGTH	EQU	shl>>2+3	;+ null terminator
