*******************************************************
**      XFD external decruncher for -CJ- Cruncher    **
**        	written by Phil. in 2000             **
**		  fixed by SDI in 2000		     **
*******************************************************

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

		* head function for tests
*		INCLUDE	"xfdExeHead.a"


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

		dc.b	'$VER: -CJ- 1.1 (26.10.2000) by Psygore',0
N_CJ		dc.b	'(-CJ-) Data Cruncher',0
		CNOP 0,4

S_CJ		dc.l	0			;no more slaves
		dc.w	1			;version
		dc.w	39			;master version
		dc.l	N_CJ			;name
		dc.w	XFDPFF_DATA|XFDPFF_RECOGLEN|XFDPFF_USERTARGET
		dc.w	0			;max length of special info (passwd)
		dc.l	RB_CJ			;recog buffer
		dc.l	DB_CJ			;decrunch buffer
		dc.l	0
		dc.l	0
		dc.w	0,0
		dc.l	4+4+4

RB_CJ		MOVEQ	#0,D0
		CMP.L	#'-CJ-',(A0)
		BNE.B	.err
		MOVE.L	4(A0),xfdrr_MinSourceLen(A1)
		MOVE.L	8(A0),D0
		MOVE.L	D0,xfdrr_FinalTargetLen(A1)
		MOVE.L	D0,xfdrr_MinTargetLen(A1)
		MOVEQ	#1,D0
.err		RTS

DB_CJ		MOVEM.L	D2-D7/A2-A6,-(A7)
		MOVEA.L	xfdbi_TargetBuffer(A0),A4
		MOVEA.L	A4,A6
		MOVEA.L	xfdbi_SourceBuffer(A0),A5
		ADDA.L	8(A5),A6
		ADDA.L	4(A5),A5

		BSR.B	lbC000188
		BSR.B	lbC00015C

		MOVEQ	#1,D0
		MOVEM.L	(A7)+,D2-D7/A2-A6
		RTS

lbC00015C:
	bsr.b	lbC0001B4
	bcc.b	lbC000182
	moveq	#0,d1
	bsr.b	lbC0001B4
	bcc.b	lbC00017C
	lea	(lbL000262,pc),a1
	moveq	#4,d3
lbC00016C:
	move.l	-(a1),d0
	bsr.b	lbC0001DA
	swap	d0
	cmp.w	d0,d1
	dbne	d3,lbC00016C
	add.l	($14,a1),d1
lbC00017C:
	move.b	-(a5),-(a6)
	dbra	d1,lbC00017C
lbC000182:
	cmpa.l	a4,a6
	bgt.b	lbC0001E8
	rts

lbC000188:
	moveq	#3,d0
lbC00018A:
	move.b	-(a5),d7
	ror.l	#8,d7
	dbra	d0,lbC00018A
	rts

lbC000194:
	move.w	a5,d7
	btst	#0,d7
	bne.b	lbC0001A2
	move.l	-(a5),d7
	addx.l	d7,d7
	bra.b	lbC0001E0

lbC0001A2:
	move.l	(-5,a5),d7
	lsl.l	#8,d7
	move.b	-(a5),d7
	subq.l	#3,a5
	add.l	d7,d7
	bset	#0,d7
	bra.b	lbC0001E0

lbC0001B4:
	add.l	d7,d7
	beq.b	lbC0001BA
	rts

lbC0001BA:
	move.w	a5,d7
	btst	#0,d7
	bne.b	lbC0001C8
	move.l	-(a5),d7
	addx.l	d7,d7
	rts

lbC0001C8:
	move.l	(-5,a5),d7
	lsl.l	#8,d7
	move.b	-(a5),d7
	subq.l	#3,a5
	add.l	d7,d7
	bset	#0,d7
	rts

lbC0001DA:
	moveq	#0,d1
lbC0001DC:
	add.l	d7,d7
	beq.b	lbC000194
lbC0001E0:
	addx.w	d1,d1
	dbra	d0,lbC0001DC
	rts

lbC0001E8:
	lea	(lbL000276,pc),a1
	moveq	#3,d2
lbC0001EE:
	bsr.b	lbC0001B4
	dbcc	d2,lbC0001EE
	moveq	#0,d4
	moveq	#0,d1
	move.b	(1,a1,d2.w),d0
	ext.w	d0
	bmi.b	lbC000202
	bsr.b	lbC0001DA
lbC000202:
	move.b	(6,a1,d2.w),d4
	add.w	d1,d4
	beq.b	lbC000228
	lea	(lbL000280,pc),a1
	moveq	#1,d2
lbC000210:
	bsr.b	lbC0001B4
	dbcc	d2,lbC000210
	moveq	#0,d1
	move.b	(1,a1,d2.w),d0
	ext.w	d0
	bsr.b	lbC0001DA
	add.w	d2,d2
	add.w	(6,a1,d2.w),d1
	bra.b	lbC00023C

lbC000228:
	moveq	#0,d1
	moveq	#5,d0
	moveq	#0,d2
	bsr.b	lbC0001B4
	bcc.b	lbC000238
	moveq	#8,d0
	moveq	#$40,d2
lbC000238:
	bsr.b	lbC0001DA
	add.w	d2,d1
lbC00023C:
	lea	(2,a6,d4.w),a1
	adda.w	d1,a1
	move.b	-(a1),-(a6)
lbC000244:
	move.b	-(a1),-(a6)
	dbra	d4,lbC000244
	bra.w	lbC00015C

		dc.l	$7FFF000E,$FF0007,$70002,$30001,$30001
lbL000262	dc.l	269,14,7,4,1
lbL000276	dc.b	9,1,0,255,255,8,4,2,1,0
lbL000280	dc.b	11,4,7,0,1,32,0,0,0,32
		end

