*****************************************************************************
*   $Id: ByteKiller.a,v 1.2 2005/06/23 14:39:16 stoecker Exp $
*   ByteKiller and related types decrunching slave
*
*   XFD library system for cruncher handling
*   Copyright (C) 1994-1998 by Georg Hörmann
*   Copyright (C) 1998 by Dirk Stöcker <soft@dstoecker.de>
*
*   This library is free software; you can redistribute it and/or
*   modify it under the terms of the GNU Lesser General Public
*   License as published by the Free Software Foundation; either
*   version 2.1 of the License, or (at your option) any later version.
*
*   This library is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*   Lesser General Public License for more details.
*
*   You should have received a copy of the GNU Lesser General Public
*   License along with this library; if not, write to the Free Software
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*****************************************************************************

S_BOND		dc.l	S_Trash-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_BOND-Xfd68KHeader
		dc.w	XFDPFF_DATA
		dc.w	0
		dc.l	RB_BOND-Xfd68KHeader
		dc.l	DB_BOND-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_BOND
		dc.w	0
		dc.l	16+25

N_BOND		dc.b	'BOND Cruncher',0
		even

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

RB_BOND		lea	-5(a0,d0.l),a0
		cmp.b	#'B',(a0)+
		bne.s	.Exit
		cmp.b	#'O',(a0)+
		bne.s	.Exit
		cmp.b	#'N',(a0)+
		bne.s	.Exit
		cmp.b	#'D',(a0)+
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_BOND		movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5

		move.l	xfdbi_SourceBuffer(a5),a2
		move.l	(a2),d1
		move.w	#XFDERR_BUFFERTRUNCATED,xfdbi_Error(a5)
		moveq	#0,d0
		cmp.l	xfdbi_SourceBufLen(a5),d1
		bgt.s	.Exit

		move.w	#XFDERR_CORRUPTEDDATA,xfdbi_Error(a5)
		and.w	#3,d1
		bne.s	.Exit

		add.l	(a2),a2
		move.l	-(a2),d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		move.l	d0,xfdbi_TargetBufLen(a5)

		move.l	xfdbi_TargetBufMemType(a5),d1
		move.l	xfdm_ExecBase(a6),a6
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		move.l	d0,xfdbi_TargetBuffer(a5)
		beq.s	.Exit

		move.l	d0,a1
		move.l	a2,a0
		move.l	-(a0),d4
		move.l	a1,a2
		add.l	xfdbi_TargetBufSaveLen(a5),a2
		moveq	#12,d7
		moveq	#9,d6
		moveq	#8,d5

		bsr	D_ByteKiller
		tst.l	d0
		bne.s	.Exit

		move.l	xfdbi_TargetBuffer(a5),a1
		move.l	xfdbi_TargetBufLen(a5),d0
		jsr	_LVOFreeMem(a6)
		move.w	#XFDERR_CORRUPTEDDATA,xfdbi_Error(a5)
		moveq	#0,d0

.Exit		movem.l	(a7)+,d2-d7/a2-a6
		rts

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

S_Trash		dc.l	S_BK13-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_Trash-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_Trash-Xfd68KHeader
		dc.l	DB_Trash-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_TrashEliminator10
		dc.w	0
		dc.l	$e8+16+24

N_Trash		dc.b	'TrashEliminator 1.0',0
		even

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

RB_Trash	cmp.l	#$41fa00c2,$24(a0)
		bne.s	.Exit
		cmp.l	#$2020b185,$c2(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$ce(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_Trash	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5

		move.l	xfdbi_SourceBuffer(a5),a0
		lea	$e8(a0),a2
		move.w	$9a(a0),d7
		move.w	$a2(a0),d6
		moveq	#0,d5
		move.b	$49(a0),d5
		move.l	$2a(a0),xfdbi_DecrAddress(a5)
		move.l	$be(a0),xfdbi_JmpAddress(a5)
		bra	DB_ByteKiller

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

S_BK13		dc.l	S_BK20-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_BK13-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_BK13-Xfd68KHeader
		dc.l	DB_BK13-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_ByteKiller13
		dc.w	0
		dc.l	$10c+16+24

N_BK13		dc.b	'ByteKiller 1.3',0
		even

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

RB_BK13		cmp.l	#$41fa00e6,$24(a0)
		bne.s	.Exit
		cmp.l	#$2020b185,$e6(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$f2(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_BK13		movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5

		move.l	xfdbi_SourceBuffer(a5),a0
		lea	$10c(a0),a2
		move.w	$a0(a0),d7
		move.w	$a8(a0),d6
		moveq	#0,d5
		move.b	$49(a0),d5
		move.l	$2a(a0),xfdbi_DecrAddress(a5)
		move.l	$d0(a0),xfdbi_JmpAddress(a5)
		bra	DB_ByteKiller

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

S_BK20		dc.l	S_BK30-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_BK20-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_BK20-Xfd68KHeader
		dc.l	DB_BK20-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_ByteKiller20
		dc.w	0
		dc.l	$ea+16+24

N_BK20		dc.b	'ByteKiller 2.0',0
		even

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

RB_BK20		cmp.l	#$41fa00ba,$2e(a0)
		bne.s	.Exit
		cmp.l	#$2020b185,$c4(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$d0(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_BK20		movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5

		move.l	xfdbi_SourceBuffer(a5),a0
		lea	$ea(a0),a2
		move.w	$9a(a0),d7
		move.w	$a0(a0),d6
		moveq	#0,d5
		move.b	$51(a0),d5
		move.l	$34(a0),xfdbi_DecrAddress(a5)
		move.l	$c0(a0),xfdbi_JmpAddress(a5)
		bra	DB_ByteKiller

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

S_BK30		dc.l	S_BKPro10-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_BK30-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_BK30-Xfd68KHeader
		dc.l	DB_BK30-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_ByteKiller30
		dc.w	0
		dc.l	$dc+16+4

N_BK30		dc.b	'ByteKiller 3.0',0
		even

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

RB_BK30		cmp.l	#$41fa00b6,$24(a0)
		bne.s	.Exit
		cmp.l	#$202044c7,$be(a0)
		bne.s	.Exit
		cmp.l	#$53417400,$c6(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_BK30		movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5

		move.l	xfdbi_SourceBuffer(a5),a0
		lea	$dc(a0),a2
		moveq	#12,d7
		moveq	#9,d6
		moveq	#8,d5
		move.l	$2a(a0),xfdbi_DecrAddress(a5)
		move.l	$b6(a0),xfdbi_JmpAddress(a5)
		bra	DB_ByteKiller

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

S_BKPro10	dc.l	S_BKPro10Pro-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_BKPro10-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_BKPro10-Xfd68KHeader
		dc.l	DB_BKPro10-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_ByteKillerPro10
		dc.w	0
		dc.l	$110+16+24

N_BKPro10	dc.b	'ByteKillerPro 1.0',0
		even

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

RB_BKPro10	cmp.l	#$41fa00dc,$32(a0)
		bne.s	.Exit
		cmp.l	#$45eaffff,$d2(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$f6(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_BKPro10	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5

		move.l	xfdbi_SourceBuffer(a5),a0
		lea	$110(a0),a2
		moveq	#0,d7
		move.b	$c5(a0),d7
		moveq	#0,d6
		move.b	$c9(a0),d6
		moveq	#0,d5
		move.b	$6d(a0),d5
		move.l	$40(a0),xfdbi_DecrAddress(a5)
		move.l	$f2(a0),xfdbi_JmpAddress(a5)
		bra	DB_ByteKiller
		
**************************************************

S_BKPro10Pro	dc.l	S_ByteKiller97-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_BKPro10Pro-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_BKPro10Pro-Xfd68KHeader
		dc.l	DB_BKPro10Pro-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_ByteKillerPro10Pro
		dc.w	0
		dc.l	$132+16+24

N_BKPro10Pro	dc.b	'ByteKillerPro 1.0 Pro',0
		even

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

RB_BKPro10Pro	cmp.l	#$41fa00d4,$5c(a0)
		bne.s	.Exit
		cmp.l	#$45eaffff,$f4(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$118(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_BKPro10Pro	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5

		move.l	xfdbi_SourceBuffer(a5),a0
		lea	$132(a0),a2
		moveq	#0,d7
		move.b	$e7(a0),d7
		moveq	#0,d6
		move.b	$eb(a0),d6
		moveq	#0,d5
		move.b	$8f(a0),d5
		move.l	$62(a0),xfdbi_DecrAddress(a5)
		move.l	$114(a0),xfdbi_JmpAddress(a5)
		bra	DB_ByteKiller

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

S_ByteKiller97	dc.l	S_DragPack10-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_BK97-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_BK97-Xfd68KHeader
		dc.l	DB_BK97-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_ByteKiller97
		dc.w	0
		dc.l	32+$30

N_BK97		DC.B	'ByteKiller 97',0
		EVEN

RB_BK97		MOVEQ	#0,D0
		CMP.L	#$41FA00A0,(A0)
		BEQ.B	.Next
		LEA	32(A0),A0
.Next		CMP.L	#$4CD80023,$A(A0)
		BNE.B	.Exit
		CMP.L	#$E290654A,$2A(A0)
		BNE.B	.Exit
		CMP.L	#$66042020,$18(A0)
		BNE.B	.Exit
		MOVEQ	#1,D0
.Exit		RTS

DB_BK97		MOVEM.L	D2-D7/A2-A6,-(A7)
		MOVE.L	A0,A5

		MOVE.L	xfdbi_SourceBuffer(A5),A0
		CMP.L	#$41FA00A0,(A0)
		BEQ.B	.Next
		LEA	32(A0),A0
.Next		LEA	$A2(A0),A2
		MOVEQ	#0,D7
		MOVE.B	$6D(A0),D7
		MOVEQ	#0,D6
		MOVE.B	$71(A0),D6
		MOVEQ	#0,D5
		MOVE.B	$21(A0),D5
		MOVE.L	$6(A0),xfdbi_DecrAddress(A5)
		MOVE.L	$8A(A0),xfdbi_JmpAddress(A5)
		BRA.W	DB_ByteKiller
		
**************************************************

S_DragPack10	dc.l	S_Deluxe1-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_DragPack10-Xfd68KHeader
		dc.w	XFDPFF_RELOC!XFDPFF_RELMODE
		dc.w	0
		dc.l	RB_DragPack10-Xfd68KHeader
		dc.l	DB_DragPack10-Xfd68KHeader
		dc.l	RS_DragPack10-Xfd68KHeader
		dc.l	DS_DragPack10-Xfd68KHeader
		dc.w	XFDID_DragPack10
		dc.w	0
		dc.l	$314+16+16

N_DragPack10	dc.b	'DragPack 1.0',0
		even

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

RB_DragPack10	cmp.l	#$3ea,$30c(a0)
		bne.s	.Exit

		cmp.l	#$4eaefd90,$26e(a0)	;orig
		bne.s	.NoOrig
		cmp.l	#$2020b185,$108(a0)
		bne.s	.NoOrig
		cmp.l	#$53414242,$ec(a0)
		beq.s	.Ok

.NoOrig		cmp.l	#$4eaefd90,$270(a0)	;patched
		bne.s	.Exit
		cmp.l	#$2020b185,$10a(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$ee(a0)
		bne.s	.Exit
.Ok		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_DragPack10	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5

		move.l	xfdbi_SourceBuffer(a5),a0
		lea	$314(a0),a2

		cmp.w	#$5341,$ec(a0)
		bne.s	.NoOrig
		move.w	$98(a0),d6
		move.w	$e8(a0),d7
		moveq	#0,d5
		move.b	$57(a0),d5
		bra.s	DB_ByteKiller

.NoOrig		move.w	$9a(a0),d6
		move.w	$ea(a0),d7
		moveq	#0,d5
		move.b	$59(a0),d5

DB_ByteKiller	move.l	4(a2),d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		move.l	d0,xfdbi_TargetBufLen(a5)

		move.l	xfdbi_TargetBufMemType(a5),d1
		move.l	xfdm_ExecBase(a6),a6
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		move.l	d0,xfdbi_TargetBuffer(a5)
		beq.s	.Exit

		move.l	d0,a1
		move.l	a2,a0

		bsr	D_ByteKillerOrg
		tst.l	d0
		bne.s	.Exit

		move.l	xfdbi_TargetBuffer(a5),a1
		move.l	xfdbi_TargetBufLen(a5),d0
		jsr	_LVOFreeMem(a6)
		move.w	#XFDERR_CORRUPTEDDATA,xfdbi_Error(a5)
		moveq	#0,d0

.Exit		movem.l	(a7)+,d2-d7/a2-a6
		rts

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

RS_DragPack10	add.l	a0,a0
		add.l	a0,a0
		cmp.l	#($a2*4)+8,-4(a0)	;segment len
		bne.s	.Exit

		cmp.l	#$4eaefd90,$24c(a0)	;patched
		bne.s	.NoOrig
		cmp.l	#$2020b185,$e6(a0)
		bne.s	.NoOrig
		cmp.l	#$53414242,$ca(a0)
		beq.s	.Ok

.NoOrig		cmp.l	#$4eaefd90,$24a(a0)	;orig
		bne.s	.Exit
		cmp.l	#$2020b185,$e4(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$c8(a0)
		bne.s	.Exit
.Ok		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DS_DragPack10	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5
		move.l	xfdsi_SegList(a5),a4
		clr.l	xfdsi_SegList(a5)
		add.l	a4,a4
		add.l	a4,a4

		cmp.w	#$5341,$c8(a4)		;orig
		bne.s	.NoOrig
		move.w	$74(a4),d6
		move.w	$c4(a4),d7
		moveq	#0,d5
		move.b	$33(a4),d5
		bra.s	.Orig

.NoOrig		move.w	$76(a4),d6
		move.w	$c6(a4),d7
		moveq	#0,d5
		move.b	$35(a4),d5

.Orig		move.l	$a(a4),a0
		move.l	$10(a4),a1

		move.l	4(a0),d0
		movem.l	d0/a1/a6,-(a7)

		bsr	D_ByteKillerOrg
		tst.l	d0
		bne.s	.DecrOk

		add.w	#12,a7
		move.w	#XFDERR_CORRUPTEDDATA,xfdsi_Error(a5)
		moveq	#0,d1
		bra.s	.FreeSeg

.DecrOk		moveq	#2-1,d2
		move.l	xfdm_ExecBase(a6),a6
.FreeNext	move.l	a4,a1
		move.l	(a4),a4
		add.l	a4,a4
		add.l	a4,a4
		move.l	-(a1),d0
		jsr	_LVOFreeMem(a6)
		dbf	d2,.FreeNext

		movem.l	(a7)+,d0/a0/a6
		lea	xfdsi_SegList(a5),a1
		move.w	xfdsi_RelMode(a5),d1
		jsr	_LVOxfdRelocate(a6)
		moveq	#0,d1
		move.w	d0,xfdsi_Error(a5)
		bne.s	.FreeSeg
		moveq	#1,d1

.FreeSeg	move.l	d1,-(a7)
		move.l	a4,d1
		lsr.l	#2,d1
		move.l	xfdm_DosBase(a6),a6
		jsr	_LVOUnLoadSeg(a6)
		move.l	(a7)+,d0

		movem.l	(a7)+,d2-d7/a2-a6
		rts

;=================================================

N_Deluxe	DC.B	'Deluxe Cruncher 1.0/2.0',0
N_Deluxe3	DC.B	'Deluxe Cruncher 3.0',0
		EVEN

S_Deluxe1	DC.L	S_Deluxe3-Xfd68KHeader
		DC.W	2		;version
		DC.W	39		;master version
		DC.L	N_Deluxe-Xfd68KHeader	;name
		DC.W	XFDPFF_ADDR	;flags
		DC.W	0
		DC.L	RB_Deluxe-Xfd68KHeader	;recog buffer
		DC.L	DB_Deluxe-Xfd68KHeader	;decrunch buffer
		DC.L	0		;recog segment
		DC.L	0		;decrunch segment
		DC.W	XFDID_DeluxeCruncher1
		DC.W	0
		DC.L	$100		;MinBufSize

S_Deluxe3	DC.L	S_TNM11-Xfd68KHeader
		DC.W	2		;version
		DC.W	39		;master version
		DC.L	N_Deluxe3-Xfd68KHeader	;name
		DC.W	XFDPFF_ADDR	;flags
		DC.W	0
		DC.L	RB_Deluxe3-Xfd68KHeader	;recog buffer
		DC.L	DB_Deluxe3-Xfd68KHeader	;decrunch buffer
		DC.L	0		;recog segment
		DC.L	0		;decrunch segment
		DC.W	XFDID_DeluxeCruncher3
		DC.W	0
		DC.L	$100		;MinBufSize

RB_Deluxe	MOVEQ	#0,D0			; False

		CMP.L	#$41FA00D6,$28(A0)
		BNE.B	.No
		CMP.L	#$00000001,$50(A0)
		BNE.B	.No
		CMP.L	#$4EFAFFD2,$64(A0)
		BNE.B	.No
		CMP.L	#$428223C9,$B4(A0)
		BNE.B	.No
		CMP.L	#$000195FC,$C8(A0)
		BNE.B	.No
		MOVEQ	#1,D0			; True
.No		RTS

DB_Deluxe	MOVEM.L	D2-D3/A4-A6,-(A7)
		MOVE.L	A0,A5

		MOVEA.L	xfdbi_SourceBuffer(A5),A4
		MOVE.L	$2E(A4),D0
		MOVE.L	D0,xfdbi_TargetBufLen(A5)
		MOVE.L	D0,xfdbi_TargetBufSaveLen(A5)
		MOVE.L	xfdbi_TargetBufMemType(A5),D1
		MOVEA.L	xfdm_ExecBase(A6),A6
		JSR	_LVOAllocMem(A6)
		MOVE.W	#XFDERR_NOMEMORY,xfdbi_Error(A5)
		MOVE.L	D0,xfdbi_TargetBuffer(A5)
		BEQ.B	.NoMem

		MOVE.L	D0,A1
		MOVE.L	$2E(A4),D0
		MOVE.B	$3B(A4),D1
		MOVE.B	$79(A4),D3

		MOVE.L	$34(A4),xfdbi_DecrAddress(A5)
		MOVE.L	$6C(A4),xfdbi_JmpAddress(A5)
		LEA	$100(A4),A4

.Decrunch	CMP.B	(A4),D1
		BEQ.B	.decr3
.decr2		MOVE.B	(A4)+,(A1)+
		SUBQ.L	#1,D0
		BNE.B	.Decrunch
.end		MOVEQ	#1,D0
.NoMem		MOVEM.L	(A7)+,D2-D3/A4-A6
		RTS

.decr3		ADDQ.L	#1,A4
		CMP.B	(A4)+,D3
		BNE.B	.decr5
		MOVE.B	(A4)+,D2
.decr4		MOVE.B	(A4),(A1)+
		SUBQ.L	#1,D0
		BEQ.B	.end
		SUBQ.B	#1,D2
		BNE.B	.decr4
		ADDQ.L	#1,A4
		BRA.B	.Decrunch
.decr5		SUBQ.L	#2,A4
		BRA.B	.decr2

;=================================================

RB_Deluxe3	MOVEQ	#0,D0			; False

		CMP.L	#$D1C0D5C1,$3C(A0)
		BNE.B	.No
		CMP.L	#$7601E288,$50(A0)
		BNE.B	.No
		CMP.L	#$3602D644,$64(A0)
		BNE.B	.No
		CMP.L	#$36026100,$B4(A0)
		BNE.B	.No
		CMP.L	#$F17EB3CA,$C8(A0)
		BNE.B	.No
		MOVEQ	#1,D0			; True
.No		RTS

DB_Deluxe3	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5

		move.l	xfdbi_SourceBuffer(a5),a0

		MOVE.L	$2A(A0),xfdbi_DecrAddress(A5)
		MOVE.L	$30(A0),xfdbi_JmpAddress(A5)

		LEA	$10E(A0),A2
		MOVEQ	#0,D5
		MOVE.B	$4F(A0),D5
		MOVE.W	$AE(A0),D6
		MOVE.W	$A6(A0),D7
		BRA.W	DB_ByteKiller

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

S_TNM11		dc.l	S_RSI14-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_TNM11-Xfd68KHeader
		dc.w	XFDPFF_RELOC!XFDPFF_RELMODE
		dc.w	0
		dc.l	RB_TNM11-Xfd68KHeader
		dc.l	DB_TNM11-Xfd68KHeader
		dc.l	RS_TNM11-Xfd68KHeader
		dc.l	DS_TNM11-Xfd68KHeader
		dc.w	XFDID_TNMCruncher11
		dc.w	0
		dc.l	$29c+4+12

N_TNM11		dc.b	'TNM Cruncher 1.1',0
		even

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

RB_TNM11	cmp.l	#$3ea,$294(a0)
		bne.s	.Exit

		cmp.l	#$4eaefd90,$17e(a0)
		bne.s	.Exit
		cmp.l	#$202044fc,$26a(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$274(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_TNM11	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5

		move.l	xfdbi_SourceBuffer(a5),a0
		move.l	$1d4(a0),d2	;offset im 2. hunk zu data
		move.l	$1d8(a0),d3	;laenge entpackt

		lea	$29c(a0),a2
		move.w	$248(a0),d7
		move.w	$24e(a0),d6
		moveq	#8,d5

		move.l	d3,xfdbi_TargetBufSaveLen(a5)
		move.l	d3,xfdbi_TargetBufLen(a5)

		move.l	d3,d0
		move.l	xfdbi_TargetBufMemType(a5),d1
		move.l	xfdm_ExecBase(a6),a6
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		move.l	d0,xfdbi_TargetBuffer(a5)
		beq.s	.Exit

		move.l	d0,a1
		move.l	a2,a0
		add.l	d2,a0
		move.l	d3,a2
		add.l	a1,a2

		bsr	D_ByteKiller

		moveq	#1,d0
.Exit		movem.l	(a7)+,d2-d7/a2-a6
		rts

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

RS_TNM11	add.l	a0,a0
		add.l	a0,a0
		cmp.l	#($9b*4)+8,-4(a0)	;segment len
		bne.s	.Exit

		cmp.l	#$4eaefd90,$15a(a0)
		bne.s	.Exit
		cmp.l	#$202044fc,$246(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$250(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DS_TNM11	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5
		move.l	xfdsi_SegList(a5),a4
		clr.l	xfdsi_SegList(a5)
		add.l	a4,a4
		add.l	a4,a4

		move.l	$1b0(a4),d2	;offset im 2. hunk zu data
		move.l	$1b4(a4),d3	;laenge entpackt

		move.w	$224(a4),d7
		move.w	$22a(a4),d6
		moveq	#8,d5

		move.l	(a4),a0
		add.l	a0,a0
		add.l	a0,a0
		move.l	(a0),a1
		add.l	a1,a1
		add.l	a1,a1
		addq.w	#4,a0
		addq.w	#4,a1

		add.l	d2,a0
		move.l	d3,a2
		add.l	a1,a2

		movem.l	d3/a1/a6,-(a7)
		bsr	D_ByteKiller

		moveq	#2-1,d2
		move.l	xfdm_ExecBase(a6),a6
.FreeNext	move.l	a4,a1
		move.l	(a4),a4
		add.l	a4,a4
		add.l	a4,a4
		move.l	-(a1),d0
		jsr	_LVOFreeMem(a6)
		dbf	d2,.FreeNext

		movem.l	(a7)+,d0/a0/a6
		lea	xfdsi_SegList(a5),a1
		move.w	xfdsi_RelMode(a5),d1
		jsr	_LVOxfdRelocate(a6)
		moveq	#0,d1
		move.w	d0,xfdsi_Error(a5)
		bne.s	.FreeSeg
		moveq	#1,d1

.FreeSeg	move.l	d1,-(a7)
		move.l	a4,d1
		lsr.l	#2,d1
		move.l	xfdm_DosBase(a6),a6
		jsr	_LVOUnLoadSeg(a6)
		move.l	(a7)+,d0

		movem.l	(a7)+,d2-d7/a2-a6
		rts

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

S_RSI14		dc.l	S_HPC-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_RSI14-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_RSI14-Xfd68KHeader
		dc.l	DB_RSI14-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_RSICruncher14
		dc.w	0
		dc.l	$dc+16+24

N_RSI14		dc.b	'RSI Cruncher 1.4',0
		even

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

RB_RSI14	cmp.l	#$41fa00f6,$3c(a0)
		bne.s	.Exit
		cmp.l	#$2020b185,$10e(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$11a(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_RSI14	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5
		move.l	xfdbi_SourceBuffer(a5),a3
						
		moveq	#0,d6		;return

		move.l	$138(a3),d7	;laenge 1. entpacken
		move.l	d7,d0
		moveq	#1,d1
		move.l	xfdm_ExecBase(a6),a6
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		tst.l	d0
		beq	.Exit
		move.l	d0,a4		;buffer 1.decrunch

		movem.l	d6/d7,-(a7)
		moveq	#0,d7
		move.b	$cd(a3),d7
		moveq	#0,d6
		move.b	$d1(a3),d6
		moveq	#0,d5
		move.b	$79(a3),d5
		lea	$134(a3),a0
		move.l	a4,a1
		bsr	D_ByteKillerOrg
		movem.l	(a7)+,d6/d7
		move.w	#XFDERR_CORRUPTEDDATA,xfdbi_Error(a5)
		tst.l	d0
		beq.s	.ExitFree

		move.l	$42(a3),d1	;1.decrunch addr
		move.l	$f8(a3),d0	;offset 2.decruncher
		sub.l	d1,d0
		lea	(a4,d0.l),a3	;bytecruncher addr
		
		move.l	d1,xfdbi_DecrAddress(a5)
		move.l	$5e(a3),xfdbi_JmpAddress(a5)

		move.l	$22(a3),d0	;laenge decdata				
		sub.l	d1,d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		move.l	d0,xfdbi_TargetBufLen(a5)
		
		move.l	xfdbi_TargetBufMemType(a5),d1
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		move.l	d0,xfdbi_TargetBuffer(a5)
		beq.s	.ExitFree

		move.l	a4,a1
		move.l	d0,a0
		move.b	$2f(a3),d6
		move.l	a0,a2
		add.l	xfdbi_TargetBufSaveLen(a5),a2
		bsr.s	D_RSIByte
		moveq	#1,d6		;return

.ExitFree	move.l	a4,a1
		move.l	d7,d0
		jsr	_LVOFreeMem(a6)

.Exit		move.l	d6,d0
		movem.l	(a7)+,d2-d7/a2-a6
		rts

D_RSIByte	moveq	#-1,d2
.Decr1		cmp.l	a2,a0
		beq.s	.Exit
		move.b	(a1)+,d0
		cmp.b	d6,d0
		beq.s	.Decr3
.Decr2		move.b	d0,(a0)+
		bra.s	.Decr1

.Decr3		moveq	#0,d1
		move.b	(a1)+,d1
		beq.s	.Decr2
		subq.w	#1,d1
		move.b	(a1)+,d0
.Decr4		move.b	d0,(a0)+
		cmp.l	a2,a0
		dbeq	d1,.Decr4
		cmp.w	d2,d1
		beq.s	.Decr1
.Exit		rts

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

S_HPC		dc.l	S_HQC20-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_HPC-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_HPC-Xfd68KHeader
		dc.l	DB_HPC-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_HighPressureCruncher
		dc.w	0
		dc.l	$208+12+24

N_HPC		dc.b	'High Pressure Cruncher',0
		even

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

RB_HPC		cmp.l	#$41fa0138,$ce(a0)
		bne.s	.Exit
		cmp.l	#$2020b185,$1e0(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$1ec(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_HPC		movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5
		move.l	xfdbi_SourceBuffer(a5),a3
				
		moveq	#0,d6		;return

		move.l	$30(a3),xfdbi_DecrAddress(a5)
		move.l	$76(a3),xfdbi_JmpAddress(a5)

		lea	$208(a3),a2
		add.l	$d4(a3),a2	;pointer decrunch
		
		move.l	-(a2),d7	;laenge 1.entpacken
		move.l	d7,d0
		moveq	#1,d1
		move.l	xfdm_ExecBase(a6),a6
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		tst.l	d0
		beq	.Exit
		move.l	d0,a4		;buffer 1.decrunch

		movem.l	d6/d7,-(a7)
		move.l	a4,a1
		move.l	a2,a0
		move.l	d7,a2
		add.l	a1,a2
		move.w	$160(a3),d7
		move.w	$168(a3),d6
		moveq	#0,d5
		move.b	$ff(a3),d5
		move.l	-(a0),d4
		bsr	D_ByteKiller
		movem.l	(a7)+,d6/d7
		move.w	#XFDERR_CORRUPTEDDATA,xfdbi_Error(a5)
		tst.l	d0
		beq.s	.ExitFree

		move.l	a4,a0
		bsr.s	D_HPCLong
		move.l	d0,d2		;sourcelen
		bsr.s	D_HPCLong
		move.l	d0,d3		
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		move.l	d0,xfdbi_TargetBufLen(a5)
		
		move.l	xfdbi_TargetBufMemType(a5),d1
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		move.l	d0,xfdbi_TargetBuffer(a5)
		beq.s	.ExitFree

		move.l	d0,a1
		move.l	a1,a2
		add.l	d3,a1
		move.l	a4,a0
		add.l	d2,a0

		moveq	#-1,d2
.Byte		move.b	-(a0),d1
		move.b	-(a0),d0
		lsl.w	#8,d0
		or.b	d1,d0
		subq.w	#1,d0
		tst.b	-(a0)
		beq.s	.Decr2
		move.b	-(a0),d1
.Decr1		move.b	d1,-(a1)
		cmp.l	a2,a1
		dbeq	d0,.Decr1
		bra.s	.Decr3
.Decr2		move.b	-(a0),-(a1)
		cmp.l	a2,a1
		dbeq	d0,.Decr2
.Decr3		cmp.w	d2,d0
		beq.s	.Byte

		moveq	#1,d6		;return

.ExitFree	move.l	a4,a1
		move.l	d7,d0
		jsr	_LVOFreeMem(a6)

.Exit		move.l	d6,d0
		movem.l	(a7)+,d2-d7/a2-a6
		rts

D_HPCLong	move.b	(a0)+,d0
		lsl.l	#8,d0
		move.b	(a0)+,d0
		lsl.l	#8,d0
		move.b	(a0)+,d0
		lsl.l	#8,d0
		move.b	(a0)+,d0
		rts

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

S_HQC20		dc.l	S_ReloKit10-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_HQC20-Xfd68KHeader
		dc.w	XFDPFF_RELOC!XFDPFF_RELMODE
		dc.w	0
		dc.l	RB_HQC20-Xfd68KHeader
		dc.l	DB_HQC20-Xfd68KHeader
		dc.l	RS_HQC20-Xfd68KHeader
		dc.l	DS_HQC20-Xfd68KHeader
		dc.w	XFDID_HQCCruncher20
		dc.w	0
		dc.l	$714+52+4

N_HQC20		dc.b	'HQC Cruncher 2.0',0
		even

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

RB_HQC20	cmp.l	#$3f3,$714(a0)
		bne.s	.Exit

		cmp.l	#$41faffc6,$60(a0)
		bne.s	.Exit
		cmp.l	#$2020b185,$512(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$51e(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_HQC20	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5
		move.l	xfdbi_SourceBuffer(a5),a3

		moveq	#20,d2
		lea	$714(a3),a0

		move.l	16(a0),d0
		sub.l	12(a0),d0
		addq.l	#1,d0
		move.l	d0,d3	;amount hunks
		move.l	d3,d7
		lsl.l	#2,d0
		add.l	d0,d2
		lea	20(a0,d0.l),a0		

.Count		move.l	(a0)+,d0
		cmp.w	#$3e9,d0
		beq.s	.CountCode
		cmp.w	#$3ea,d0
		beq.s	.CountCode
		cmp.w	#$3eb,d0
		beq.s	.CountBSS
		cmp.w	#$3ec,d0
		beq.s	.CountReloc
		cmp.w	#$3f2,d0
		bne	.Error
		addq.l	#4,d2
		tst.l	d3
		bne.s	.Count
		bra.s	.Alloc

.CountCode	move.l	(a0),d0
		add.l	d0,a0
		move.l	(a0)+,d0
		add.l	d0,d2
		addq.l	#8,d2	;3e9,x
		subq.l	#1,d3
		bra.s	.Count		

.CountBSS	addq.w	#4,a0
		addq.l	#8,d2	;3e9,x
		subq.l	#1,d3
		bra.s	.Count		

.CountReloc	addq.l	#8,d2
.CountReloc1	move.l	(a0)+,d0
		beq.s	.Count
		lsl.l	#2,d0
		lea	4(a0,d0.l),a0
		add.l	d0,d2
		addq.l	#8,d2
		bra.s	.CountReloc1

.Alloc		move.l	d2,d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		move.l	d0,xfdbi_TargetBufLen(a5)
		
		move.l	xfdbi_TargetBufMemType(a5),d1
		move.l	xfdm_ExecBase(a6),a6
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		move.l	d0,xfdbi_TargetBuffer(a5)
		beq.s	.Exit

		move.l	d0,a1
		lea	$714(a3),a0
		move.l	d7,d3		;amount hunks
		move.w	$4d4(a3),d7
		move.w	$4dc(a3),d6
		moveq	#0,d5
		move.b	$47d(a3),d5

		moveq	#5,d0
		add.l	d3,d0
.CopyHeader	move.l	(a0)+,(a1)+
		subq.l	#1,d0
		bne.s	.CopyHeader

.Decr		move.l	(a0)+,d0
		move.l	d0,(a1)+
		cmp.w	#$3e9,d0
		beq.s	.DecrCode
		cmp.w	#$3ea,d0
		beq.s	.DecrCode
		cmp.w	#$3eb,d0
		bne.s	.NoBSS
		move.l	(a0)+,(a1)+
		subq.l	#1,d3
		bra.s	.Decr
.NoBSS		cmp.w	#$3ec,d0
		beq.s	.DecrReloc
		cmp.w	#$3f2,d0
		bne.s	.DecrError
		tst.l	d3
		bne.s	.Decr

		moveq	#1,d0
.Exit		movem.l	(a7)+,d2-d7/a2-a6
		rts

.DecrError	move.l	xfdbi_TargetBuffer(a5),a1
		move.l	xfdbi_TargetBufLen(a5),d0
		jsr	_LVOFreeMem(a6)
.Error		moveq	#0,d0
		move.w	#XFDERR_CORRUPTEDDATA,xfdbi_Error(a5)
		bra.s	.Exit

.DecrCode	move.l	(a0),d0
		add.l	d0,a0
		move.l	(a0)+,d0
		lsr.l	#2,d0
		move.l	d0,(a1)+
		movem.l	d3/a0,-(a7)
		move.l	-(a0),a2
		add.l	a1,a2
		move.l	-(a0),d4
		bsr	D_ByteKiller
		movem.l	(a7)+,d3/a0
		tst.l	d0
		beq.s	.DecrError
		add.l	-4(a0),a1
		subq.l	#1,d3
		bra.s	.Decr

.DecrReloc	move.l	(a0)+,d0
		move.l	d0,(a1)+
		beq.s	.Decr
		addq.l	#1,d0
.DecrReloc1	move.l	(a0)+,(a1)+
		subq.l	#1,d0
		bne.s	.DecrReloc1
		bra.s	.DecrReloc

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

RS_HQC20	add.l	a0,a0
		add.l	a0,a0
		cmp.l	#($1b3*4)+8,-4(a0)
		bne.s	.Exit

		cmp.l	#$41faffc6,$60-$24(a0)
		bne.s	.Exit
		cmp.l	#$2020b185,$512-$24(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$51e-$24(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DS_HQC20	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5

		move.l	xfdm_DosBase(a6),-(a7)
		move.l	xfdm_ExecBase(a6),a6

		move.l	xfdsi_SegList(a5),a4
		add.l	a4,a4
		add.l	a4,a4

		move.l	(a4),a3
		add.l	a3,a3
		add.l	a3,a3
		move.l	(a3),a3
		add.l	a3,a3
		add.l	a3,a3
		addq.w	#4,a3		;source

		move.l	8(a3),d2
		move.l	d2,d3
		add.w	#20,a3
		lea	xfdsi_SegList(a5),a2
		clr.l	(a2)
		move.w	xfdsi_RelMode(a5),d6
.AllocNext	cmp.w	#XFDREL_DEFAULT,d6
		beq.s	.AllocDefault
		move.l	#$10003,d1
		cmp.w	#XFDREL_FORCECHIP,d6
		beq.s	.AllocChipFast
		move.l	#$10005,d1
		cmp.w	#XFDREL_FORCEFAST,d6
		beq.s	.AllocChipFast
		move.w	#XFDERR_BADRELMODE,xfdsi_Error(a5)
		bra	.FreeError
.AllocDefault	moveq	#1,d1
		swap	d1
		move.w	(a3),d1
		rol.w	#3,d1
		and.w	#$0006,d1
		or.w	#$0001,d1
.AllocChipFast	move.l	(a3)+,d0
		addq.l	#2,d0
		lsl.l	#2,d0
		move.l	d0,-(a7)
		jsr	_LVOAllocMem(a6)
		move.l	(a7)+,d1
		move.w	#XFDERR_NOMEMORY,xfdsi_Error(a5)
		tst.l	d0
		beq.b	.FreeError
		move.l	d0,a0
		move.l	d1,(a0)+
		move.l	a0,d0
		lsr.l	#2,d0
		move.l	d0,(a2)
		move.l	a0,a2
		subq.l	#1,d2
		bne.s	.AllocNext

		move.w	$4d4-$24(a4),d7
		move.w	$4dc-$24(a4),d6
		moveq	#0,d5
		move.b	$47d-$24(a4),d5
		lea	xfdsi_SegList(a5),a2
		move.w	#XFDERR_CORRUPTEDDATA,xfdsi_Error(a5)

.Decr		move.l	(a3)+,d0
		cmp.w	#$3e9,d0
		beq.s	.DecrCode
		cmp.w	#$3ea,d0
		beq.s	.DecrCode
		cmp.w	#$3eb,d0
		bne.s	.NoBSS
		move.l	(a2),a2
		add.l	a2,a2
		add.l	a2,a2
		addq.w	#4,a3
		subq.l	#1,d3
		bra.s	.Decr
.NoBSS		cmp.w	#$3ec,d0
		beq.s	.DecrReloc
		cmp.w	#$3f2,d0
		bne.s	.FreeError
		tst.l	d3
		bne.s	.Decr

		move.l	a4,d1
		lsr.l	#2,d1
		move.l	(a7),a6
		jsr	_LVOUnLoadSeg(a6)
		moveq	#1,d0
		bra.s	.Exit

.FreeError	lea	xfdsi_SegList(a5),a2
.FreeSeg	move.l	(a2),d0
		beq.s	.Error
		lsl.l	#2,d0
		move.l	d0,a2
		move.l	d0,a1
		move.l	-(a1),d0
		jsr	_LVOFreeMem(a6)
		bra.s	.FreeSeg

.Error		move.l	a4,d1
		lsr.l	#2,d1
		move.l	d1,xfdsi_SegList(a5)
.Exit		addq.w	#4,a7
		movem.l	(a7)+,d2-d7/a2-a6
		rts

.DecrCode	move.l	(a2),a2
		add.l	a2,a2
		add.l	a2,a2
		move.l	(a3)+,d0
		add.l	d0,a3
		movem.l	d3/a2,-(a7)
		move.l	a3,a0
		lea	4(a2),a1
		move.l	-(a0),a2
		add.l	a1,a2
		move.l	-(a0),d4
		bsr	D_ByteKiller
		movem.l	(a7)+,d3/a2
		tst.l	d0
		beq.s	.FreeError
		subq.l	#1,d3
		bra	.Decr

.DecrReloc	move.l	(a3)+,d0
		beq	.Decr
		move.l	(a3)+,d1
		lea	xfdsi_SegList(a5),a0
.GetHunk	move.l	(a0),a0
		add.l	a0,a0
		add.l	a0,a0
		dbf	d1,.GetHunk
		addq.w	#4,a0
		move.l	a0,d1
.DecrReloc1	move.l	(a3)+,d2
		add.l	d1,4(a2,d2.l)
		subq.l	#1,d0
		bne.s	.DecrReloc1
		bra.s	.DecrReloc

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

S_ReloKit10	dc.l	S_STSong-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_ReloKit10-Xfd68KHeader
		dc.w	XFDPFF_RELOC!XFDPFF_RELMODE
		dc.w	0
		dc.l	RB_ReloKit10-Xfd68KHeader
		dc.l	DB_ReloKit10-Xfd68KHeader
		dc.l	RS_ReloKit10-Xfd68KHeader
		dc.l	DS_ReloKit10-Xfd68KHeader
		dc.w	XFDID_ReloKit10
		dc.w	0
		dc.l	$30a+16+$a8

N_ReloKit10	dc.b	'ReloKit 1.0',0
		even

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

RB_ReloKit10	cmp.l	#$41fa02c6,$28(a0)
		bne.s	.Exit
		cmp.l	#$2020b185,$2c8(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$2d4(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_ReloKit10	movem.l	d2-d7/a2-a6,-(a7)

ReloSize	SET	0
ReloBuf		SET	4
ReloStack	SET	8

		subq.w	#ReloStack,a7
		move.l	a7,a4
		move.l	a0,a5
		move.l	xfdbi_SourceBuffer(a5),a3
		
		move.l	$2fe(a3),d0
		add.l	$2f6(a3),d0	;buffer size
		move.l	d0,ReloSize(a4)
		moveq	#1,d1
		move.l	xfdm_ExecBase(a6),a6
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		move.l	d0,ReloBuf(a4)
		beq	.Exit

		move.w	$2f4(a3),d0	;amount hunks
		lsl.w	#3,d0
		lea	$302(a3),a0
		add.w	d0,a0		;data begin
		move.l	ReloBuf(a4),a1
		move.w	$296(a3),d7
		move.w	$29c(a3),d6
		moveq	#0,d5
		move.b	$249(a3),d5
		bsr	D_ByteKillerOrg
		move.w	#XFDERR_CORRUPTEDDATA,xfdbi_Error(a5)
		tst.l	d0
		beq	.ExitFree
		
		moveq	#20,d2		;header
		add.l	$2fe(a3),d2	;size of code
		moveq	#0,d1
		move.w	$2f4(a3),d1	;amount hunks
		lsl.w	#3,d1
		add.l	d1,d2		;3f2,3e9,x,x (abzuegl. hunkkette)				

		move.l	ReloBuf(a4),a0
		add.l	$2fe(a3),a0	;reloc begin

.CountReloc	moveq	#0,d3
.CountReloc1	moveq	#0,d0
		move.b	(a0)+,d0
		lsl.w	#8,d0
		move.b	(a0)+,d0
		move.w	d0,d4
		beq.s	.CountReloc4	;no (more) relocs to this hunk
		cmp.w	#-1,d0
		beq.s	.CountReloc5	;no more relocs
		moveq	#1,d3
		addq.l	#8,d2		;x entries zu y hunk
		moveq	#0,d0
		move.w	d4,d0
		lsl.l	#2,d0
		add.l	d0,d2		;amount entries
		addq.w	#5,a0		;hunk num, first offset
		bra.s	.CountReloc3
.CountReloc2	move.b	(a0)+,d0
		bne.s	.CountReloc3
		addq.w	#2,a0
.CountReloc3	subq.w	#1,d4
		bne.s	.CountReloc2
		bra.s	.CountReloc1
.CountReloc4	tst.w	d3
		beq.s	.CountReloc
		addq.l	#8,d2		;3ec,0
		bra.s	.CountReloc
.CountReloc5	move.l	d2,xfdbi_TargetBufLen(a5)

		move.l	d2,d0
		move.l	xfdbi_TargetBufMemType(a5),d1
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		move.l	d0,xfdbi_TargetBuffer(a5)
		beq	.ExitFree

		move.l	d0,a1
		move.l	#$3f3,(a1)+
		clr.l	(a1)+
		clr.w	(a1)+
		move.w	$2f4(a3),(a1)+	;amount hunks	
		clr.l	(a1)+
		move.l	-8(a1),(a1)
		subq.l	#1,(a1)+

		move.l	a5,-(a7)
		move.l	a1,a5		;hunk lens
		lea	$302+4(a3),a2	;lengths of hunks		
		move.l	ReloBuf(a4),a6
		move.l	a6,a0
		add.l	$2fe(a3),a0	;reloc begin
		move.w	$2f4(a3),d7	;hunks
		move.w	d7,d0
		lsl.w	#2,d0
		add.w	d0,a1

.MakeHunk	move.l	#$3e9,(a1)+
		move.l	(a2)+,d0
		move.l	d0,d1
		and.l	#$c0000000,d1
		and.l	#$3fffffff,d0
		subq.l	#8,d0
		lsr.l	#2,d0
		move.l	d0,(a1)+
		move.l	a1,d5
		move.l	d0,d2
		or.l	d1,d0
		move.l	d0,(a5)+
		addq.w	#8,a6
		tst.l	d2
		beq.s	.MakeReloc
.CopyHunk	move.l	(a6)+,(a1)+
		subq.l	#1,d2
		bne.s	.CopyHunk
		
		move.l	a1,d6

.TestBSS	tst.l	-(a1)
		bne.s	.TestData
		cmp.l	d5,a1
		bne.s	.TestBSS
		move.l	d5,a1
		move.l	#$3eb,-8(a1)
		bra.s	.MakeReloc	

.TestData	addq.w	#4,a1
		cmp.l	d6,a1
		beq.s	.MakeReloc

		sub.l	a1,d6
		lsr.l	#2,d6
		move.l	a3,-(a7)
		move.l	d5,a3
		sub.l	d6,-(a3)
		move.l	#$3ea,-(a3)
		move.l	(a7)+,a3

.MakeReloc	bsr.s	D_ReloKitRelocs

		move.l	#$3f2,(a1)+
		addq.w	#4,a2
		subq.w	#1,d7
		bne.s	.MakeHunk

		move.l	(a7)+,a5

		sub.l	xfdbi_TargetBuffer(a5),a1
		move.l	a1,xfdbi_TargetBufSaveLen(a5)
		moveq	#1,d0

.ExitFree	move.l	d0,-(a7)
		move.l	ReloBuf(a4),a1
		move.l	ReloSize(a4),d0
		move.l	4.w,a6
		jsr	_LVOFreeMem(a6)
		move.l	(a7)+,d0

.Exit		addq.w	#ReloStack,a7
		movem.l	(a7)+,d2-d7/a2-a6
		rts

D_ReloKitRelocs moveq	#0,d2
.NextHunk	moveq	#0,d0
		move.b	(a0)+,d0
		lsl.l	#8,d0
		move.b	(a0)+,d0
		move.w	d0,d4
		beq.s	.Exit
		
		tst.w	d2
		bne.s	.NoInit
		move.l	#$3ec,(a1)+	;start
		moveq	#1,d2

.NoInit		move.l	d0,(a1)+	;amount
		moveq	#0,d0
		move.b	(a0)+,d0
		move.l	d0,(a1)+	;hunk nr

		move.b	(a0)+,d0
		lsl.l	#8,d0
		move.b	(a0)+,d0
		lsl.l	#8,d0
		move.b	(a0)+,d0
		lsl.l	#8,d0
		move.b	(a0)+,d0
		bra.s	.SaveOffset

.NextOffset	moveq	#0,d1
		move.b	(a0)+,d1
		ext.w	d1
		tst.b	d1
		bne.s	.Short
		move.b	1(a0),d1
		lsl.w	#8,d1
		move.b	(a0),d1
		addq.w	#2,a0
.Short		ext.l	d1
		sub.l	d1,d0
.SaveOffset	move.l	d0,d3
		lsl.l	#1,d3
		move.l	d3,(a1)+
		subq.w	#1,d4
		bne.s	.NextOffset
		bra.s	.NextHunk

.Exit		tst.w	d2
		beq.s	.NoNull
		clr.l	(a1)+
.NoNull		rts

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

RS_ReloKit10	add.l	a0,a0
		add.l	a0,a0
		cmp.l	#($b3*4)+8,-4(a0)
		blt.s	.Exit

		cmp.l	#$41fa02c6,$8(a0)
		bne.s	.Exit
		cmp.l	#$2020b185,$2a8(a0)
		bne.s	.Exit
		cmp.l	#$53414242,$2b4(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

Relo_DosBase	set	xfdSegmentInfo_SIZE
Relo_Buffer	set	xfdSegmentInfo_SIZE+4
Relo_BufSize	set	xfdSegmentInfo_SIZE+8
Relo_SIZE	set	xfdSegmentInfo_SIZE+12

DS_ReloKit10	movem.l	d2-d7/a2-a6,-(a7)
		sub.w	#Relo_SIZE,a7
		move.l	a7,a5
		move.l	a0,-(a7)	;bufferinfo
		move.l	a5,a1
		moveq	#xfdSegmentInfo_SIZE-1,d1
.CopySI1	move.b	(a0)+,(a1)+
		dbf	d1,.CopySI1
		move.l	xfdm_DosBase(a6),Relo_DosBase(a5)

		move.l	xfdsi_SegList(a5),a4
		add.l	a4,a4
		add.l	a4,a4
		lea	$2d0(a4),a3

		move.l	6(a3),d0
		add.l	14(a3),d0	;buf len
		move.l	d0,Relo_BufSize(a5)
		moveq	#0,d1
		move.l	xfdm_ExecBase(a6),a6
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdsi_Error(a5)
		move.l	d0,Relo_Buffer(a5)
		beq	.Exit

		move.l	d0,a1
		move.w	4(a3),d0
		lsl.w	#3,d0
		lea	18(a3,d0.w),a0
		move.w	$276(a4),d7
		move.w	$27c(a4),d6
		moveq	#0,d5
		move.b	$229(a4),d5
		bsr	D_ByteKillerOrg
		move.w	#XFDERR_CORRUPTEDDATA,xfdsi_Error(a5)
		tst.l	d0
		beq	.ExitFree

		move.l	Relo_Buffer(a5),a1
		lea	18(a3),a0
		move.w	4(a3),d2
.PrepNext	move.l	4(a0),d0
		move.l	d0,d1
		and.l	#$3fffffff,d0
		move.l	d0,(a1)
		swap	d1
		lsr.w	#8,d1
		lsr.w	#5,d1
		moveq	#2,d0
		and.l	d1,d0
		move.l	d0,4(a1)
		add.l	(a1),a1
		addq.w	#8,a0
		subq.w	#1,d2
		bne.s	.PrepNext

		move.w	4(a3),d7
		move.l	xfdsi_SegList(a5),d1
		move.l	a6,-(a7)
		move.l	Relo_DosBase(a5),a6
		jsr	_LVOUnLoadSeg(a6)
		move.l	(a7)+,a6

		move.w	xfdsi_RelMode(a5),d3
		move.w	d7,d2
		move.l	Relo_Buffer(a5),a2
		lea	xfdsi_SegList(a5),a3
		clr.l	(a3)
		move.w	#XFDERR_NOMEMORY,xfdsi_Error(a5)

.AllocNext	move.l	4(a2),d1
		cmp.w	#XFDREL_DEFAULT,d3
		beq.s	.Alloc
		moveq	#2,d1
		cmp.w	#XFDREL_FORCECHIP,d3
		beq.s	.Alloc
		moveq	#4,d1
		cmp.w	#XFDREL_FORCEFAST,d3
		beq.s	.Alloc
		move.w	#XFDERR_BADRELMODE,xfdsi_Error(a5)
		bra	.ErrorFree
.Alloc		move.l	(a2),d0
		jsr	_LVOFreeMem(a6)
		tst.l	d0
		beq	.ErrorFree
		move.l	d0,a0
		move.l	(a2),(a0)+
		move.l	a0,d0
		lsr.l	#2,d0
		move.l	d0,(a3)
		move.l	a0,a3
		lea	8(a2),a0
		lea	4(a3),a1
		move.l	(a2),d0
		subq.l	#8,d0
		ble.s	.SkipCopy
		jsr	_LVOCopyMemQuick(a6)
.SkipCopy	add.l	(a2),a2
		subq.l	#1,d2
		bne.s	.AllocNext
		clr.l	(a3)

		moveq	#-1,d7
		moveq	#8,d6
.RelocDoHunk	addq.w	#1,d7
.NextReloc	moveq	#0,d0
		move.b	(a2)+,d0
		lsl.w	d6,d0
		move.b	(a2)+,d0
		move.w	d0,d4
		beq.s	.RelocDoHunk
		cmp.w	#-1,d0
		beq.s	.NoMoreRelocs
		moveq	#0,d0
		move.b	(a2)+,d0
		bsr.s	.GetHunk
		move.l	a0,d3
		move.w	d7,d0
		bsr.s	.GetHunk
		move.l	a0,a3
		move.b	(a2)+,d0
		lsl.w	d6,d0
		move.b	(a2)+,d0
		lsl.l	d6,d0
		move.b	(a2)+,d0
		lsl.l	d6,d0
		move.b	(a2)+,d0
		bra.s	.StartReloc
.DoNextReloc	moveq	#0,d1
		move.b	(a2)+,d1
		ext.w	d1
		tst.b	d1
		bne.s	.Short
		move.b	1(a2),d1
		lsl.w	d6,d1
		move.b	(a2),d1
		addq.w	#2,a2
.Short		ext.l	d1
		sub.l	d1,d0
.StartReloc	move.l	d0,d2
		add.l	d2,d2
		add.l	d3,(a3,d2.l)
		subq.w	#1,d4
		bne.s	.DoNextReloc
		bra.s	.NextReloc

.NoMoreRelocs	moveq	#1,d0
		bra.s	.ExitFree

.GetHunk	lea	xfdsi_SegList(a5),a0
.GetHunk1	move.l	(a0),a0
		add.l	a0,a0
		add.l	a0,a0
		dbf	d0,.GetHunk1
		addq.w	#4,a0
		rts

.ErrorFree	move.l	xfdsi_SegList(a5),d0
		beq.s	.ExitFree
		move.l	d0,d2
.FreeNext	lsl.l	#2,d2
		move.l	d2,a1
		move.l	(a1),d2
		move.l	-(a1),d0
		jsr	_LVOFreeMem(a6)
		tst.l	d2
		bne.s	.FreeNext
		clr.l	xfdsi_SegList(a5)
		moveq	#0,d0

.ExitFree	move.l	d0,-(a7)
		move.l	Relo_Buffer(a5),a1
		move.l	Relo_BufSize(a5),d0
		jsr	_LVOFreeMem(a6)
		move.l	(a7)+,d0

.Exit		move.l	(a7)+,a1	;segmentinfo
		moveq	#xfdSegmentInfo_SIZE-1,d1
.CopySI2	move.b	(a5)+,(a1)+
		dbf	d1,.CopySI2
		add.w	#Relo_SIZE,a7
		movem.l	(a7)+,d2-d7/a2-a6
		rts

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

S_STSong	dc.l	S_ANC-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_STSong-Xfd68KHeader
		dc.w	XFDPFF_DATA|XFDPFF_RECOGLEN|XFDPFF_USERTARGET
		dc.w	0
		dc.l	RB_STSong-Xfd68KHeader
		dc.l	DB_STSong-Xfd68KHeader
		dc.l	SD_STSong-Xfd68KHeader
		dc.l	VD_STSong-Xfd68KHeader
		dc.w	XFDID_STPackedSong
		dc.w	0
		dc.l	16

N_STSong	dc.b	'SoundTracker Packed Song',0
		even

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

RB_STSong	MOVEQ	#0,D0
		cmp.l	#'PACK',(a0)
		bne.s	.Exit
		move.l	8(a0),d1
		sub.l	#$43c,d1
		and.l	#$fffe03ff,d1
		beq.s	.Ok
		move.l	8(a0),d1
		sub.l	#$258,d1
		and.l	#$fffe03ff,d1
		bne.s	.Exit	
.Ok		MOVE.L	8(A0),D0
		MOVE.L	D0,xfdrr_MinTargetLen(A1)
		MOVE.L	D0,xfdrr_FinalTargetLen(A1)
		MOVEQ	#$C,D0
		ADD.L	4(A0),D0
		MOVE.L	D0,xfdrr_MinSourceLen(A1)
		moveq	#1,d0
.Exit		rts

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

SD_STSong	moveq	#0,d0
		cmp.l	#'PACK',(a0)
		bne.s	.Exit
		moveq	#1,d0
.Exit		rts

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

VD_STSong	move.l	8(a0),d1
		sub.l	#$43c,d1
		and.l	#$fffe03ff,d1
		beq.s	.Ok
		move.l	8(a0),d1
		sub.l	#$258,d1
		and.l	#$fffe03ff,d1
		bne.s	.Exit
.Ok		moveq	#$c,d1
		add.l	4(a0),d1	;crlen
		cmp.l	d0,d1		;crlen > buflen ??
		bgt.s	.Exit
		move.l	8(a0),d0
		sub.l	4(a0),d0	;cr > uncr ??
		bmi.s	.Exit
		move.l	d1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_STSong	movem.l	d2-d7/a2-a6,-(a7)
		MOVE.L	xfdbi_SourceBuffer(A0),A2
		MOVE.L	xfdbi_UserTargetBuf(A0),A1
		addq.w	#4,a2
		move.l	(a2)+,d0	;cr len
		move.l	(a2)+,d1	;uncr len
		move.l	a2,a0		;source
		add.l	d0,a0
		lea	(a1,d1.l),a2
		moveq	#8,d7
		moveq	#9,d6
		moveq	#8,d5
		bsr	D_ByteKiller

		moveq	#1,d0
		movem.l	(a7)+,d2-d7/a2-a6
		rts

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

S_ANC		
		IFND	XFD_LASTSLAVE
		dc.l	S_ByteKiller_END-Xfd68KHeader
		ELSE
		dc.l	0
		ENDIF
		dc.w	2
		dc.w	39
		dc.l	N_ANC-Xfd68KHeader
		dc.w	XFDPFF_DATA
		dc.w	0
		dc.l	RB_ANC-Xfd68KHeader
		dc.l	DB_ANC-Xfd68KHeader
		dc.l	RB_ANC-Xfd68KHeader
		dc.l	VD_ANC-Xfd68KHeader
		dc.w	XFDID_ANCCruncher
		dc.w	0
		dc.l	4+16

N_ANC		dc.b	'ANC Cruncher',0
		even

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

RB_ANC		moveq	#0,d0
		cmp.l	#'FVL0',(a0)
		bne.s	.Exit
		moveq	#1,d0
.Exit		rts

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

VD_ANC		moveq	#8,d1
		add.l	4(a0),d1	;crlen
		cmp.l	d0,d1		;crlen > buflen ??
		bgt.s	.Exit
		move.l	d1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_ANC		movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5

		move.l	xfdbi_SourceBuffer(a5),a2
		moveq	#8,d1
		add.l	4(a2),d1
		move.w	#XFDERR_BUFFERTRUNCATED,xfdbi_Error(a5)
		moveq	#0,d0
		cmp.l	xfdbi_SourceBufLen(a5),d1
		bgt.s	.Exit

		add.l	4(a2),a2
		move.l	4(a2),d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		move.l	d0,xfdbi_TargetBufLen(a5)

		move.l	xfdbi_TargetBufMemType(a5),d1
		move.l	xfdm_ExecBase(a6),a6
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		move.l	d0,xfdbi_TargetBuffer(a5)
		beq.s	.Exit

		move.l	d0,a1
		move.l	a2,a0
		move.l	(a0),d4
		move.l	a1,a2
		add.l	xfdbi_TargetBufSaveLen(a5),a2
		moveq	#12,d7
		moveq	#9,d6
		moveq	#8,d5

		bsr.b	D_ByteKiller
		tst.l	d0
		bne.s	.Exit

		move.l	xfdbi_TargetBuffer(a5),a1
		move.l	xfdbi_TargetBufLen(a5),d0
		jsr	_LVOFreeMem(a6)
		move.w	#XFDERR_CORRUPTEDDATA,xfdbi_Error(a5)
		moveq	#0,d0

.Exit		movem.l	(a7)+,d2-d7/a2-a6
		rts

;=================================================

* a0: source
* a1: target
* d5-d7: efficiency

D_ByteKillerOrg	move.l	(a0)+,d0
		move.l	(a0)+,a2
		move.l	(a0)+,d4
		add.l	a1,a2
		add.l	d0,a0
D_ByteKiller	move.l	-(a0),d0
		eor.l	d0,d4
.Decr1		lsr.l	#1,d0
		bne.s	.Decr2
		bsr.s	.Decr14
.Decr2		bcs.s	.Decr12
		move.w	d5,d1		;<--
		moveq	#1,d3
		lsr.l	#1,d0
		bne.s	.Decr3
		bsr.s	.Decr14
.Decr3		bcs.s	.Decr9
		moveq	#3,d1
		bsr.s	.Decr15
		move.w	d2,d3
.Decr5		moveq	#7,d1
.Decr6		lsr.l	#1,d0
		bne.s	.Decr7
		bsr.s	.Decr14
.Decr7		roxl.l	#1,d2
		dbra	d1,.Decr6
		move.b	d2,-(a2)
		cmp.l	a1,a2
		dbeq	d3,.Decr5
		bra.s	.Decr11

.Decr8		move.w	d6,d1		;<--
		add.w	d2,d1
		addq.w	#2,d2
		move.w	d2,d3
.Decr9		bsr.s	.Decr15
.Decr10		move.b	-1(a2,d2.w),-(a2)
		cmp.l	a1,a2
		dbeq	d3,.Decr10
.Decr11		cmp.w	#-1,d3
		beq.s	.Decr1
		moveq	#0,d0
		tst.l	d4
		bne.s	.Error
		moveq	#1,d0
.Error		rts

.Decr12		moveq	#2,d1
		bsr.s	.Decr15
		cmp.b	#2,d2
		blt.s	.Decr8
		cmp.b	#3,d2
		bne.s	.Decr13
		moveq	#8,d1
		bsr.s	.Decr15
		move.w	d2,d3
		addq.w	#8,d3
		bra.s	.Decr5

.Decr13		moveq	#8,d1
		bsr.s	.Decr15
		move.w	d2,d3
		move.w	d7,d1		;<--
		bra.s	.Decr9

.Decr14		move.l	-(a0),d0
		eor.l	d0,d4
		move.b	#$10,ccr
		roxr.l	#1,d0
		rts

.Decr15		subq.w	#1,d1
		moveq	#0,d2
.Decr16		lsr.l	#1,d0
		bne.s	.Decr17
		bsr.s	.Decr14
.Decr17		roxl.l	#1,d2
		dbra	d1,.Decr16
		rts

S_ByteKiller_END

