	XDEF	_T1eexec

	INCLUDE "exec/types.i"

	STRUCTURE	F_FILE,0
	APTR	fbuf
	ULONG	len
	ULONG	curpos
	LABEL	F_FILE_sizeof

	SECTION	text,CODE

_T1eexec:
	; calling parameters
	; a0 = struct F_FILE *

	movem.l	d2-d4,-(sp)

	; register usage
	; d2 = r
	; d3 = c1
	; d4 = c2
	; a1 = current ptr
	; a0 = last byte + 1

	move.w	#55665,d2	; initial r
	move.w	#52845,d3	; c1 (constant)
	move.w	#22719,d4	; c2 (constant)

	move.l	curpos(a0),d0	; get curpos

	ADDQ.L	#4,curpos(a0)	; TetiSoft: BUG FIX: Skip over key

	move.l	fbuf(a0),a1	; get pointer to buffer
	move.l	a1,d1
	add.l	d0,a1		; get ptr to curpos
	move.l	len(a0),d0	; get length
	move.l	d1,a0
	add.l	d0,a0		; get pointer to end of buffer

	; calculate key from first four bytes

	moveq.l	#3,d1

1$:
	moveq.l	#0,d0
	move.b	(a1)+,d0
	add.w	d0,d2		; add r
	mulu.w	d3,d2		; multiple by c1
	add.w	d4,d2		; add c1
	dbra	d1,1$

	; decrypt the data

decrypt:
	cmpa.l	a1,a0
	bge.s	1$

	; all done so exit
	movem.l	(sp)+,d2-d4
	moveq	#0,d0
	rts

1$:
	moveq.l	#0,d0
	move.b	(a1),d0
	move.w	d2,d1
	lsr.w	#8,d1
	eor.w	d0,d1		; d1 = ch
	move.b	d1,(a1)+

	add.w	d0,d2
	mulu.w	d3,d2
	add.w	d4,d2		; r recomputed

	bra.s	decrypt

	END
