*******************************************************
**             XFD external decruncher               **
**              for CRUNCH2A cruncher                **
**             adapted by Wanted Team                **
*******************************************************

		INCLUDE	"AINCLUDE:IncDirs.i"
		INCLUDE	"libraries/xfdmaster.i"

; xfdForeman structure MUST be first thing in all external decrunchers

ForeMan		moveq	#-1,d0		;security
		rts
		dc.l	XFDF_ID		;id
		dc.w	1		;version
		dc.w	0
		dc.l	0,0		;private
		dc.l	S_CRUNCH2A	;first slave

**************************************************

; xfdSlave structure: this one doesn't support data scanning

S_CRUNCH2A		dc.l	0		;no more slaves
		dc.w	2		;version
		dc.w	39		;master version
		dc.l	N_CRUNCH2A	;name
		dc.w	XFDPFF_DATA!XFDPFF_RECOGLEN!XFDPFF_USERTARGET
		dc.w	0
		dc.l	RB_CRUNCH2A	;recog buffer
		dc.l	DB_CRUNCH2A	;decrunch buffer
		dc.l	0		;recog segment
		dc.l	0		;decrunch segment
		dc.w	0,0
		dc.l	32+2

		dc.b	"$VER: CRUNCH2A 2.1 (09.06.2001) by Wanted Team",0
N_CRUNCH2A	dc.b	"CRUNCH2A data cruncher",0
		even

;-------------------------------------------------

; Recog buffer function: receives buffer + length in a0/d0

RB_CRUNCH2A	btst	#0,D0
		bne.b	Exit
		cmp.l	#"CRUN",(A0)+
		bne.b	Exit
		cmp.l	#"CH2A",(A0)+
		bne.b	Exit
		move.l	4(A0),D1
		beq.b	Exit
		move.l	D1,xfdrr_MinTargetLen(A1)
		moveq	#100,D0
		sub.l	D0,D1
		move.l	D1,xfdrr_FinalTargetLen(A1)
		move.l	20(A0),D1
		beq.b	Exit
		moveq	#10,D0
		add.l	D0,D1
		move.l	D1,xfdrr_MinSourceLen(A1)
		moveq	#1,D0
		rts	
Exit		moveq	#0,D0
		rts

;-------------------------------------------------

; Decrunch buffer function: receives bufferinfo in a0
; This style requires xfdmaster.library V39
DB_CRUNCH2A	movem.l	D2-D7/A2-A6,-(A7)
		move.l	A0,A5
		move.l	xfdbi_UserTargetBuf(A5),A1
		move.l	xfdbi_SourceBuffer(A5),A6
		bsr.b	D_CRUNCH2A
		moveq	#1,D0
		movem.l	(A7)+,D2-D7/A2-A6
		rts

;-------------------------------------------------

; Decruncher from "Street Gang" (c) 1987 by Time Warp/Rainbow Arts

D_CRUNCH2A	MOVE.B	8(A6),D2
		MOVE.B	9(A6),D3
		MOVE.B	10(A6),D4
		MOVEA.L	12(A6),A3			; depacked length
;		MOVEA.L	$14(A6),A5			; jump address
;		MOVEA.L	$18(A6),A4			; end of depack address
;		MOVEA.L	$1C(A6),A2			; packed length
		LEA	$20(A6),A0
		add.l	A1,A3
		MOVEA.L	A3,A4
		LEA	lbW001E64(PC),A2
		LEA	-$64(A4),A4
lbC001E22	CMPA.L	A1,A4
		BLE.B	lbC001E9E
		MOVEQ	#0,D0
		MOVE.B	(A0)+,D0
		TST.B	D0
		BMI.S	lbC001E50
		CMP.W	#$7F,D0
		BEQ.S	lbC001E40
		SUBQ.W	#1,D0
lbC001E38	MOVE.B	(A0)+,(A1)+
		DBRA	D0,lbC001E38
		BRA.S	lbC001E22
lbC001E40	MOVE.B	(A0)+,D1
		ROL.W	#8,D1
		MOVE.B	(A0)+,D1
		SUBQ.W	#1,D1
lbC001E48	MOVE.B	(A0)+,(A1)+
		DBRA	D1,lbC001E48
		BRA.S	lbC001E22
lbC001E50	MOVE.W	D0,D1
		ANDI.W	#$1F,D0
		ROR.W	#4,D1
		ANDI.W	#6,D1
		MOVEA.L	A2,A3
		ADDA.W	0(A2,D1.W),A3
		JMP	(A3)
lbW001E64	dc.w	8,$14,$20,$2E
		ADDQ.W	#1,D0
lbC001E6E	MOVE.B	D2,(A1)+
		DBRA	D0,lbC001E6E
		BRA.B	lbC001E22
		ADDQ.W	#1,D0
lbC001E7A	MOVE.B	D3,(A1)+
		DBRA	D0,lbC001E7A
		BRA.B	lbC001E22
lbC001E84	ADDQ.W	#1,D0
		MOVE.B	(A0)+,D1
lbC001E88	MOVE.B	D1,(A1)+
		DBRA	D0,lbC001E88
		BRA.B	lbC001E22
		LSL.W	#8,D0
		MOVE.B	(A0)+,D0
		ADDI.W	#$20,D0
		BRA.B	lbC001E84
lbC001E9E	TST.B	D4
		BNE.S	lbC001EAC
		MOVEQ	#0,D0
lbC001EAA	RTS
lbC001EAC	MOVEQ	#-1,D0
		RTS

		END
