
;
;
;
;Strips ANNO stuff from IFF-8SVX samples
;
;
;

;v 0.1 - works, I mean done :)






	incbin	start-4000.bin

IffEr:
	lea.l	__(pc),a4
	move.l	4.w,a6
	move.l	a6,ExecBase-__(a4)

	moveq	#37,d0
	lea.l	DosName(pc),a1
	jsr	-552(a6)			;OpenLibrary
	move.l	d0,DosBase-__(a4)
	beq.w	.noDos

	pea.l	Template-__(a4)
	move.l	(sp)+,d1
	pea.l	Array-__(a4)
	move.l	(sp)+,d2
	moveq	#0,d3
	move.l	DosBase-__(a4),a6
	jsr	-798(a6)			;ReadArgs
	move.l	d0,Args-__(a4)
	bne.b	.B
	bsr.w	PrintFault
	bra.w	.noArgs
.B:


;------------------------------------------------------------------
;
;examine file
;

	move.l	0+Array-__(a4),d1
	moveq	#-2,d2				;READ
	jsr	-84(a6)				;Lock
	tst.l	d0
	bne.b	.locked
	bsr.w	PrintFault
	bra.w	.noPath				;! NO LOCK !
.locked:
	move.l	d0,-(sp)

	moveq	#2,d1				;DOS_FIB
	moveq	#0,d2				;tags
	jsr	-228(a6)			;AllocDosObject
	move.l	d0,d2
	bne.b	.oki

	move.l	(sp)+,d1
	jsr	-90(a6)				;UnLock
	bra.w	.noPath				;! NO MEMORY FOR FIB !

.oki:
	move.l	(sp),d1				;d1 = lock
	;					;d2 = FIB
	jsr	-102(a6)			;Examine

		move.l	d2,a0
		move.l	4(a0),d7			;file/dir
		move.l	124(a0),d6			;size

	moveq	#2,d1				;DOS_FIB
	;					;d2 = FIB
	jsr	-234(a6)			;FreeDosObject

	move.l	(sp)+,d1
	jsr	-90(a6)				;UnLock

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

	tst.l	d7				;was it file?!
	bmi.b	.file
	bra.w	.noPath				;! NOT A FILE !

.file:
	move.l	d6,d0				;! SIZE = 0 !
	beq.w	.noPath
	moveq	#0,d1				;ANY
	move.l	ExecBase-__(a4),a6
	jsr	-684(a6)			;AllocVec
	tst.l	d0
	bne.b	.memory
	bra.b	.noPath				;! NO MEM FOR FILE !

.memory:
	move.l	d0,a5
	move.l	DosBase-__(a4),a6
	move.l	0+Array-__(a4),d1
	move.l	#1005,d2			;_OLDFILE
	jsr	-30(a6)				;Open
	move.l	d0,d5
	bne.b	.opened
	bsr.b	PrintFault
	bra.b	.noPath				;! CANT OPEN FILE TO READ!

.opened:
	move.l	d6,d3
	move.l	a5,d2
	move.l	d5,d1
	jsr	-42(a6)				;Read

	move.l	d5,d1
	jsr	-36(a6)				;Close



		move.l	a5,a0			;BufferBegin
		lea.l	(a0,d6.L),a1		;BufferEnd

		bsr.b	Shorten

		tst.l	d0
		beq.b	.noPath			;! NOTHING GAINED !

		sub.l	d0,a1
		sub.l	a0,a1
		move.l	a1,d3			;d3 = new length


	move.l	0+Array-__(a4),d1
	move.l	#1006,d2			;_NEWFILE
	jsr	-30(a6)				;Open
	move.l	d0,d5
	bne.b	.opened2

	move.l	a5,a1
	move.l	ExecBase-__(a4),a6
	jsr	-690(a6)			;FreeVec
	bsr.b	PrintFault
	bra.b	.noPath				;! CANT OPEN FILE TO WRITE!

.opened2:
	move.l	d5,d1
	move.l	a5,d2
	;d3 was set before
	jsr	-48(a6)				;Write

	move.l	d5,d1
	jsr	-36(a6)				;Close


	move.l	a5,a1
	move.l	ExecBase-__(a4),a6
	jsr	-690(a6)			;FreeVec


.noPath:
	move.l	Args-__(a4),d1
	move.l	DosBase-__(a4),a6
	jsr	-858(a6)			;FreeArgs
.noArgs:
	move.l	DosBase-__(a4),a1
	move.l	ExecBase-__(a4),a6
	jsr	-414(a6)
.noDos:
	moveq	#0,d0
	rts

PrintFault:
	move.l	DosBase-__(a4),a6
	jsr	-132(a6)			;IoErr
	move.l	d0,d1
PrintFaultB:
	moveq	#0,d2
	jmp	-474(a6)			;PrintFault




;*************************************************************
;
; note that You can replace this routine with anything
; and don't need to care about memory, file accesses etc.
;

Shorten:
	movem.l	d1-a6,-(sp)
	bsr.b	.stripANNO
	movem.l	(sp)+,d1-a6
	rts

.stripANNO:

;a0 - buf
;a1 - end of buf

	cmp.l	#"FORM",(a0)
	bne.b	.notIFF

	cmp.l	#"8SVX",8(a0)
	bne.b	.notIFF

	lea.l	4(a0),a3			;iff-len

.nextLong:
	addq.l	#4,a0

	cmp.l	a0,a1
	ble.b	.NotIff
	move.l	(a0),d0

	cmp.l	#"ANNO",d0
	bne.b	.nextLong	

.shorten:
	move.l	a0,a2
	
	moveq	#8,d0
	add.l	4(a0),d0			;ANNO len

	sub.l	d0,(a3)				;new iff-len

	add.l	d0,a0

	cmp.l	a0,a1
	ble.b	.NotIff

.cpy:
	move.l	(a0)+,(a2)+
	cmp.l	a0,a1
	ble.b	.done
	bra.b	.cpy

.done:

;d0 = gain

	rts

.NotIFF:
	moveq	#0,d0
	rts




DosName:	dc.b	"dos.library",0
TEMPLATE:	dc.b	"FILE/A",0

		dc.b	"$VER: Optim8SVX 0.1 (01.09.02) by Zbigniew Trzcionkowski",10,0

	cnop	0,4

ExecBase:	ds.l	1
DosBase:	ds.l	1
Args:		ds.l	1
Array:		ds.l	1*1

__:
