
; xpk inc
	include "libraries/xpk.i"
	include "libraries/xpksub.i"
; fast inc
	include "FAST.i"


	XDEF	PackChunk
	XDEF	PackReset
	XDEF	PackFree
	XREF	compress_fast
	XREF	compress_slow

	XDEF	UnpackChunk
	XDEF	UnpackFree
	XREF	decompress

	XDEF	PackerInfo

	XLIB	AllocMem
	XLIB	FreeMem

	CODE

PACKCHUNKREGS	reg	a0-a6/d1
PackChunk:
	movem.l PACKCHUNKREGS,-(a7)
	move.l	a6,a5			; lib base
	move.l	a0,a4			; xparams
	move.l	xid_SysLib(a5),a6

	move.l	xsp_Mode(a4),d1
	
	move.l	#PACKMEM_FAST,d0
	lea.l	compress_fast(pc),a3

	cmp.l	#80,d1
	bcs.s	IsFast
	move.l	#PACKMEM_SLOW,d0
	lea.l	compress_slow(pc),a3
IsFast:
	tst.l	xsp_Sub(a4)             ;Is there some memory allocated?
	beq.s	AllocPackMem
	cmp.l	xsp_Sub+4(a4),d0	; Is the size right?
	beq.s	packMemOK

	bsr.s	PackFree		; Free currently allocated memory.
AllocPackMem:
	move.l	d0,xsp_Sub+4(a4)
	move.l	#$10001,d1
	CALLSYS AllocMem
	move.l	d0,xsp_Sub(a4)
	bne.s	packMemOK
	move.l	#XPKERR_NOMEM,d0
	bra.s	endPackChunk

packMemOK:
	move.l	xsp_InBuf(a4),a0        ;a0:=InBuf
	move.l	xsp_OutBuf(a4),a1       ;a1:=OutBuf
	move.l	xsp_Sub(a4),a2          ;a2:=Hash
	move.l	xsp_InLen(a4),d0        ;d0:=InLen
	moveq.l #90,d1
	jsr	(a3)			; call the compressor.
	tst.l	d0
	bne.s	Nooverrun
	moveq.l #XPKERR_EXPANSION,d0
	bra.s	endPackChunk
Nooverrun:
	move.l	d0,xsp_OutLen(a4)       ;Fill in Final output length XpkSubParams
	moveq	#XPKERR_OK,d0
endPackChunk:
	movem.l (sp)+,PACKCHUNKREGS
	rts

PACKFREEREGS	reg	a0-a3/d0-d1
PackFree:	movem.l PACKFREEREGS,-(a7)
		move.l	xsp_Sub(a0),a1
		beq.s	packFreeEnd
		clr.l	xsp_Sub(a0)
		move.l	xsp_Sub+4(a0),d0	; currently allocated amount.
		clr.l	xsp_Sub+4(a0)
		move.l	a6,a3
		move.l	xid_SysLib(a3),a6
		CALLSYS FreeMem
		move.l	a3,a6
packFreeEnd:	movem.l (a7)+,PACKFREEREGS
UnpackFree:
PackReset:	moveq	#XPKERR_OK,d0
		rts

UnpackChunk:	move.l	a1,-(sp)
		move.l	xsp_OutBuf(a0),a1
		move.l	xsp_InLen(a0),d0
		move.l	xsp_InBuf(a0),a0

		bsr	decompress
		move.l	(sp)+,a1
		moveq.l #XPKERR_OK,d0
		rts


PackerInfo:	move.l	a0,d0
		lea	FastInfo(pc),a0
		exg	a0,d0
		rts



; --- Static Data --------
FastInfo:
 dc.w	1		;xi_XpkInfoVersion:Version number of this structure
 dc.w	Version 	;xi_LibVersion	 : The version of this sublibrary
 dc.w	2		;xi_MasterVersion: The required master lib version
 dc.w	0		;xi_ModesVersion : Longword align
 dc.l	FastName	;xi_Name	 : Brief name of the packer
 dc.l	LongName	;xi_LongName	 : Full name of the packer
 dc.l	Description	;xi_Description  : One line description of packer
 dc.b	'FAST'          ;xi_ID           : ID the packer goes by (XPK format)
			;xi_Flags	 : Defined below
 dc.l	XPKIF_PK_CHUNK|XPKIF_UP_CHUNK|XPKIF_MODES
 dc.l	$7FFFFFFF	;xi_MaxPkInChunk : Max input chunk size for packing
 dc.l	0		;xi_MinPkInChunk : Min input chunk size for packing
 dc.l	$10000		;xi_DefPkInChunk : Default packing chunk size
 dc.l	PackMsg 	;xi_PackMsg	 : Packing message, present tense
 dc.l	UnpackMsg	;xi_UnpackMsg	 : Unpacking message, present tense
 dc.l	PackedMsg	;xi_PackedMsg	 : Packing message, past tense
 dc.l	UnpackedMsg	;xi_UnpackedMsg  : Unpacking message, past tense
 dc.w	50		;xi_DefMode	 : Default mode number
 dc.w	0		;xi_Pad 	 : for future use
 dc.l	Modes		;xi_Modes	 : Array of compression modes
 dc.l	0,0,0,0,0,0	;xi_Reserved,6*4 : Future expansion - set to zero
		;LABEL	;xi_SIZEOF	 : Size of the *first* part only

FastName:	dc.b	'fasTcinator',0
LongName:	dc.b	'FAST V'
		VERSIONSTR
		dc.b	' alias WOOP or LZRW++',0
Description:	dc.b	'LZ77-family, windowsize: 4095, no quotecount, 2 streams',0
PackMsg:	dc.b	'tieing',0
PackedMsg:	dc.b	'compressed',0
UnpackMsg:	dc.b	'untieing',0
UnpackedMsg:	dc.b	'decompressed',0

	EVEN

Modes:
 dc.l	Crawling	;xm_Next	  : Chain to next descriptor for ModeDesc list
 dc.l	79		;xm_Upto	  : Maximum efficiency handled by this mode.
			;xm_Flags	  : Defined below
 dc.l	XPKMF_A3000SPEED
 dc.l	PACKMEM_FAST 	;xm_PackMemory	  : Extra memory required during packing
 dc.l	UNPACKMEM	;xm_UnpackMemory  : Extra memory during unpacking
 dc.l	428		;xm_PackSpeed	  : Approx packing speed in K per second
 dc.l	1055		;xm_UnpackSpeed   : Approx unpacking speed in K per second
 dc.w	327		;xm_Ratio	  : CF in 0.1% for AmigaVision executable
 dc.w	0		;xm_ChunkSize	  : Desired chunk size in K (!!) for this mode
 dc.b	'speedy',0,0,0,0 ;xm_Description,10:8 character mode description

Crawling:
 dc.l	0		;xm_Next	  : Chain to next descriptor for ModeDesc list
 dc.l	100		;xm_Upto	  : Maximum efficiency handled by this mode.
			;xm_Flags	  : Defined below
 dc.l	XPKMF_A3000SPEED
 dc.l	PACKMEM_SLOW 	;xm_PackMemory	  : Extra memory required during packing
 dc.l	UNPACKMEM	;xm_UnpackMemory  : Extra memory during unpacking
 dc.l	70		;xm_PackSpeed	  : Approx packing speed in K per second
 dc.l	1096		;xm_UnpackSpeed   : Approx unpacking speed in K per second
 dc.w	393		;xm_Ratio	  : CF in 0.1% for AmigaVision executable
 dc.w	0		;xm_ChunkSize	  : Desired chunk size in K (!!) for this mode
 dc.b	'crawling',0,0  ;xm_Description,10:8 character mode description
		;LABEL	;xm_SIZEOF
	END
