MODULE	'dos/doshunks'
MODULE	'tools/file'


DEF	hunks=NIL,
	cdh=NIL,
	lst=NIL,
	code=NIL,
	firstcode=NIL,
	newfile[256]:STRING,
	buffer=NIL,
	size=NIL

PROC main()
	 DEF 	lock=NIL

	size:=FileLength(arg)
	StringF(newfile,'\s.dec',arg)
	lock:=Open(arg,OLDFILE)
	buffer:=New(size)
	Read(lock,buffer,size)
	Close(lock)

	MOVE.L	size,D0
	MOVEA.L	buffer,A0
	MOVEQ	#0,D2
	MOVEQ	#0,D3

	MOVEA.L	A0,A2
	ADDA.L	D0,A2

	CMPI.L	#$3F3,(A0)+
	BEQ	namelp

	WriteF('Not an executable\n')
	CleanUp()

exit:	MOVE.L	D1,lst
	MOVE.L	D2,hunks
	MOVE.L	D3,cdh
	MOVE.L	D0,code
	eind()
namelp:
	CMPA.L	A0,A2
	BCS	exit
	MOVE.L	(A0)+,D0
	BEQ	endnam
	LSL.L	#2,D0
	ADDA.L	D0,A0
	BRA	namelp
endnam:	MOVE.L	(A0)+,D0
	LSL.L	#2,D0
	ADDQ.L	#8,D0
	ADDA.L	D0,A0
	MOVEQ	#0,D0

dohunk:
	CMPA.L	A0,A2
	BCS.B	exit

	MOVE.L	(A0),D0

	AND.L	#$00FFFFFF,D0
	CMP.L	#$3E9,D0
	BNE	doother

	ADDQ.L	#8,A0
	MOVE.L	A0,firstcode

	decode()

	eind()

doother:
	MOVE.W	(A0)+,D0
	CMPA.L	A0,A2
	BCS	exit
	CMP.W	#$3F2,D0
	BNE	doother

doend:
	->ADDA.L	#4,A0
	ADD.L	#1,D2
	->MOVE.L	(A0)+,D0
	BRA	dohunk

ENDPROC

PROC eind()
	WriteF('hunks: \d codehunks: \d last size: \d last code: $\h\n',hunks,cdh,lst,code)
	->WriteF('\s\n',text)
	writefile(newfile,buffer,size)
	CleanUp()
ENDPROC

PROC decode()

	MOVEA.L	firstcode,A0
	MOVE.L	-4(A0),D0
	LSL.L	#2,D0
	ADDA.L	D0,A0

	LEA	-738(A0),A1	->13c
	LEA	-88(A0),A2	->156
	MOVE.W	(A2),D1

lp:	EOR.W	D1,(A1)+
	CMPA.L	A1,A2
	BNE.B	lp


ENDPROC
