
*** decrunch.library example program
***
*** this is Public Domain, you may do what you want with it, but please keep
*** the unmodified version of this code with the decrunch.library package
***
*** this demo code was done in a great hurry and is not very optimized or
*** useable in your own programs, but you can at least see how to use
*** the library ;^)
***
*** Have fun, Georg 8^]

** enter the include path at the incdir line and the filenames at the
** end of the source code to load a file, decrunch it and save the
** result back to disk

		incdir	"dh0:declib"	;change this to your include path
		include "libraries/decrunch.i"

		move.l	4.w,a6
		lea	doslib(pc),a1		;open doslib
		moveq	#0,d0
		jsr	-552(a6)
		move.l	d0,dosbase
		beq	no_dos

		move.l	d0,a6

		move.l	#sourcefile,d1		;lock file
		moveq	#-2,d2
		jsr	-84(a6)
		move.l	d0,d7
		beq	no_file

		move.l	#fib,d2			;examine file
		move.l	d7,d1
		jsr	-102(a6)
		
		move.l	d7,d1			;unlock file
		jsr	-90(a6)
		
		move.l	fib+124(pc),d0		;allocate buffer for file
		moveq	#0,d1
		move.l	4.w,a6
		jsr	-198(a6)		
		move.l	d0,allocbuffer
		beq	no_mem

		move.l	dosbase(pc),a6		;open sourcefile
		move.l	#sourcefile,d1
		move.l	#1005,d2
		jsr	-30(a6)
		move.l	d0,d7
		beq	no_open

		move.l	d0,d1     		;read file
		move.l	allocbuffer(pc),d2
		move.l	fib+124(pc),d3
		jsr	-42(a6)
		
		move.l	d7,d1			;close file
		jsr	-36(a6)

		move.l	4.w,a6			;open decrunchlib
		lea	decrlib(pc),a1
		moveq	#DECRUNCHVERSION,d0
		jsr	-552(a6)
		move.l	d0,decrbase
		beq	no_decr

		move.l	d0,a6		

** the following hunk test is not necessary, because the library does it
** before decrunching. this is just to show you the usage...

		move.l	allocbuffer(pc),a0	;test hunks
		jsr	_LVOTestHunkStructure(a6)
		tst.l	d0
		beq.s	no_ci

		jsr	_LVOAllocCrunchInfo(a6) ;alloc crunch info
		move.l	d0,crunchinfo
		beq.s	no_ci	

		move.l	d0,a0			;get cruncher type
		move.l	d0,a2
		move.l	allocbuffer(pc),ci_data(a0)
		jsr	_LVOGetCruncher(a6)
		tst.l	d0
		beq.s	no_cruncher

		move.l	a2,a0			;decrunch
		cmp.b	#pt_data,ci_packtype(a0)
		bne.s	not_data
		move.l	fib+124(pc),ci_datalen(a0)	;if data file, then
not_data	jsr	_LVODecrunch(a6)		;submit file length
		tst.l	d0
		bne.s	decrunch_ok

** you could add a routine here that prints the error string.
** sample code would look like that:

;		move.l	ci_decdata(a2),-(a7)	;error string
;		move.w	ci_error(a2),-(a7)	;error code
;		move.l	a7,a1
;		lea	errortext(pc),a0
;		bsr	PrintF			;formatted print routine
;		addq.w	#6,a7

		bra	no_cruncher
		
;errortext	dc.b	'Error %d: %s',10,0
;		even

decrunch_ok	move.l	#targetfile,d1		;open target
		move.l	#1006,d2
		move.l	dosbase(pc),a6
		jsr	-30(a6)
		move.l	d0,d7
		beq.s	no_target

		move.l	d0,d1     		;write file
		move.l	ci_decdata(a2),d2
		move.l	ci_declen(a2),d3
		jsr	-48(a6)
		
		move.l	d7,d1			;close file
		jsr	-36(a6)

no_target	move.l	4.w,a6			;free decrunch buffer
		move.l	ci_decdata(a2),a1
		move.l	ci_reallen(a2),d0
		jsr	-210(a6)

no_cruncher	move.l	decrbase(pc),a6		;free crunchinfo
		move.l	crunchinfo(pc),a0
		jsr	_LVOFreeCrunchInfo(a6)

no_ci		move.l	4.w,a6          	;close decrunchlib
		move.l	decrbase(pc),a1
		jsr	-414(a6)
no_decr
no_open		move.l	4.w,a6			;free source buffer
		move.l	allocbuffer(pc),a1
		move.l	fib+124(pc),d0
		jsr	-210(a6)
no_mem
no_file		move.l	4.w,a6			;close doslib
		move.l	dosbase(pc),a1
		jsr	-414(a6)

no_dos		moveq	#0,d0
		rts

dosbase		dc.l	0
decrbase	dc.l	0
allocbuffer	dc.l	0
crunchinfo	dc.l	0

		cnop	0,4

fib		blk.b	260,0

doslib		dc.b	'dos.library',0
decrlib		dc.b	'decrunch.library',0
sourcefile	dc.b	'dh0:testfile1',0	;enter name of source here
targetfile	dc.b	'dh0:testfile2',0	;enter name of target here

		end

