;*---------------------------------------------------------------------------
;  :Program.    TPWM.asm
;  :Contents.   Decruncher for XFDMaster.Library
;  :Author.     WEPL
;  :Address.    unknown
;  :Shortcut.   unknown
;  :History.    V 0.9 29-Oct-94
;		V 1.0 19-Nov-94 angepaßt auf orginal includes
;				msdos-tpwm support added
;  :Copyright.  Public Domain
;  :Language.   68000 Assembler
;  :Translator. MaxonAsm V1.1
;---------------------------------------------------------------------------*
;##########################################################################

	INCDIR	Includes:
	INCLUDE	lvo/exec.i
	INCLUDE	libraries/xfdmaster.i

;##########################################################################
;----------------------------------------
; XFD-Library-Header

		moveq	#-1,d0
		rts	

		dc.l	XFDF_ID		;id
		dc.w	XFDF_VERSION	;version
		dc.w	0
		dc.l	0,0		;private
		dc.l	_xfdSlave1	;first slave

;----------------------------------------
; version string

		dc.b	'$VER: External TPWM decruncher V1.0 ('
	DATE 1
		dc.b	') by WEPL for xfdmaster.library',0
	CNOP 4

;----------------------------------------
; xfdSlave-structure

_xfdSlave1	dc.l	0		; next slave
		dc.w	XFDS_VERSION	; structure version
		dc.w	33		; req xfdmaster version
		dc.l	_name1		; cruncher name
		dc.w	XFDPFF_DATA	; cruncher flags
		dc.w	0		; max length of special info (passwd)
		dc.l	_check1		; check routine
		dc.l	_decrunch1	; decrunch routine
		dc.l	0		; segment recog routine
		dc.l	0		; segment decrunch routine

;----------------------------------------
; name of packer

_name1		dc.b	'(TPWM) Cruncher',0
	EVEN

;----------------------------------------
; code to check crunched
; Übergabe :	D0 = ULONG data size
;		A0 = APTR  data
; Rückgabe :	D0 = BOOL  success

_check1		cmp.l	#12,d0		;minimum 12 bytes
		blo	.no
		cmpi.l	#"TPWM",(a0)
		beq	.yes
.no		moveq	#0,d0
		rts

.yes		moveq	#-1,d0
		rts

;----------------------------------------
; code to decrunch the file
; Übergabe :	A0 = APTR xfdBufferInfo
; Rückgabe :	D0 = BOOL success

_decrunch1	movem.l	a4-a6,-(a7)
		move.l	a0,a5				;A5 = xfdbi
		move.l	(xfdbi_SourceBuffer,a5),a4	;A4 = source
		move.l	(4,a4),d0
		move.l	d0,d1
		ror.w	#8,d1
		swap	d1
		ror.w	#8,d1
		cmp.l	d0,d1
		bhs	.ok1
		exg.l	d1,d0
.ok1		cmp.l	(xfdbi_SourceBufLen,a5),d0
		bhs	.ok2
		exg.l	d1,d0	;DANGER ! this calculation of size is not 100% clear
.ok2				; but if it's failed the dest file will only have false length
		move.l	d0,(xfdbi_TargetBufLen,a5)
		move.l	d0,(xfdbi_TargetBufSaveLen,a5)
		move.l	(xfdbi_TargetBufMemType,a5),d1
		move.l	(4).w,a6
		jsr	(_LVOAllocMem,a6)
		move.l	d0,(xfdbi_TargetBuffer,a5)
		beq	.nomem
		
		move.l	(xfdbi_TargetBufLen,a5),d0
		move.l	a4,a0
		move.l	(xfdbi_TargetBuffer,a5),a1
		bsr	_TPWM_Dec
		tst.l	d0
		beq	.dataerr
		
		clr.w	(xfdbi_Error,a5)
		moveq	#-1,d0

.quit		movem.l	(a7)+,a4-a6
		rts	

.nomem		move.w	#XFDERR_NOMEMORY,(xfdbi_Error,a5)
		bra	.quit
.dataerr	move.w	#XFDERR_CORRUPTEDDATA,(xfdbi_Error,a5)
		bra	.quit

;----------------------------------------
; Format :	$0	dc.b	"TPWM"
;		$4	ULONG	size unpacked (MSDOS: LSByte,...,HSByte)
;		$8		Daten...
;
; Übergabe :	D0 = ULONG size of savebuf
;		A0 = APTR source
;		A1 = APTR destination
; Rückgabe :	D0 = BOOL success

_TPWM_Dec	movem.l	d1-a6,-(a7)
		move.l	d0,d1
		addq.l	#8,a0
		
.370		MOVE.B	(A0)+,D0
		MOVEQ	#7,D5
.376		ADD.B	D0,D0
		BCS.B	.394
		SUBQ.L	#1,D1
		BMI.B	.40A
.386		MOVE.B	(A0)+,(A1)+
.38A		DBRA	D5,.376
		BRA.B	.370

.394		MOVE.B	(A0)+,D7
		MOVEQ	#0,D2
		MOVE.B	D7,D2
		LSL.W	#4,D2
.3A2		MOVE.B	(A0)+,D2
		ANDI.W	#$000F,D7
		ADDQ.W	#2,D7
		MOVEA.L	A1,A2
		SUBA.W	D2,A2
.3B0		SUBQ.L	#1,D1
		BMI.B	.40A
		MOVE.B	(A2)+,(A1)+
		DBRA	D7,.3B0
		BRA.B	.38A

.40A		movem.l	(a7)+,d1-a6
		moveq	#-1,d0
		RTS	

;##########################################################################

	END

