
;
;I had no tool to rip ASCII from WordWorth document,
;so now I have :)
;
;note: allocs one buffer for file and ripped text
;note: WTXT appear on WORD aligned locations...

;v 0.1 - works, I mean done :)
;v 0.2 - seems I have some "encrypted" files...
;        ( with eor %01111111 )






	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:
	add.l	d6,d6				;size*2!!!
	
	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.w	.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.w	PrintFault
	bra.w	.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.w	RipTXT

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




	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:

	asr.l	d6				;memsize/2
	move.l	d5,d1
	pea.l	(a5,d6.L)
	move.l	(sp)+,d2			;buffer=mem+memsize/2

	move.l	d2,a0



	moveq	#-1,d3				;calc len of text
.len:	addq.l	#1,d3

	tst.b	(a0)+
	bne.b	.len

	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.
;

RipTXT:
	movem.l	d1-a6,-(sp)
	bsr.b	.ripTxt
	movem.l	(sp)+,d1-a6
	rts

.ripTxt:

;a0 - begin of file

;a1 - end of buf

;d6 - size*2

	asr.l	d6
	lea.l	(a0,d6.L),a2

;a2 - end of file=begin of buf


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

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

.seek
	addq.l	#2,a0
	cmp.l	a0,a2
	ble.b	.done
	cmp.l	#"WTXT",(a0)
	bne.b	.seek

	addq.l	#4,a0
	move.l	(a0)+,d0
	subq.l	#1,d0

	cmp.b	#$70,(a0,d0.W)
	bne.b	.nonCrypted
		subq.l	#1,CryptedFlag-__(a4)
.nonCrypted:


	move.l	a0,a3
.cpy:	move.b	(a3)+,d1


	tst.l	CryptedFlag-__(a4)
	beq.b	.A
	eor.b	#%01111111,d1
.A:
	cmp.b	#$f,d1
	bne.b	.B
	move.b	#$a,d1
.B:
	move.b	d1,(a2)+
	beq.b	.zero
	dbra	d0,.cpy

	move.b	#$a,(a2)+
	bra.b	.seek

.zero:
	subq.l	#1,a2
	move.b	#$a,(a2)+
	bra.b	.seek

.done:
	clr.b	(a2)+					;end of text stream
	moveq	#1,d0
	rts

.NotIFF:
	moveq	#0,d0
	rts




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

		dc.b	"$VER: WW2TXT 0.2 (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

CryptedFlag:	ds.l	1

__:
