			*************************
			*     Hunk Extract	*
			* without system AMIGA!	*
			*			*
			* Code: Lynx / Appendix	*
			*			*
			* Input:		*
			*			*
			* a0 - adr of file	*
			*      (of first hunk)	*
			*			*
			* Output:		*
			*			*
			* a0 - start of first	*
			*		hunk	*
			*************************


HunkExtract

	movem.l	d1-d7/a1-a5,-(sp)
	move.l	a0,a1

	lea	Tab_Relo1(pc),a1

	moveq	#31,d0
Cls_table
	clr.l	(a1)+
	dbf	d0,Cls_Table

	cmp.l	#$3f3,(a0)+
	bne.w	Hunk_1
	tst.l	(a0)+
	bne.w	Hunk_1
	movem.l	(a0)+,d0-d2
	lea	Tab_Relo2(pc),a2
	movem.l	d0-d1,(a2)
	move.l	d0,d3
	add.l	d1,d3
	subq.l	#1,d3
	cmp.l	d3,d2
	bne.w	Hunk_1
	move.l	d0,d3
	add.l	d3,d3
	add.l	d3,d3
	move.l	a0,a1
	add.l	d3,a0
	bsr.w	Hunk_2
	lea	Tab_Relo3(pc),a0
	lea	Tab_Relo1(pc),a1
	move.l	a0,a5
	move.l	a1,a4
	addq.l	#4,a4
	movem.l	Tab_Relo2(pc),d0-d1
	subq.l	#1,d0
Go_to_1:
	move.l	(a0)+,a3
	movem.l	(a3)+,d2-d3
	movem.l	d0-d1/a0-a1,-(a7)
	moveq	#0,d1
	move.l	d3,d0
	and.l	#$3fffffff,d0
	add.l	d0,d0
	add.l	d0,d0
	btst	#30,d3
	bne.s	Go_to_2
	btst	#30,d2
	beq.s	Go_to_3
Go_to_2
	moveq	#2,d1
Go_to_3
	lea	Tab_Relo_4(pc),a0
	tst	(a0)
	beq.s	Go_to_4
	move	(a0),d1
Go_to_4
	addq.l	#8,d0
	move.l	d0,d7
	addq.l	#6,d0

	;now you must alloc memory for this hunk
	;example I use sys alloc mem

	jsr -$c6(a6)			;a6 = exec lib

	move.l	d0,d4
	movem.l	(a7)+,d0-d1/a0-a1
	tst.l	d4
	beq.w	Memory_Error		;cant alloc memory
	move.l	d4,a2
	move.l	a2,(a1)+
	cmpa.l	a4,a1
	beq.s	Go_to_5
	move.l	a3,-(a7)
	move.l	-8(a1),a3
	move.l	a2,d4
	lsr.l	#2,d4
	addq.l	#1,d4
	move.l	d4,4(a3)
	move.l	(a7)+,a3
Go_to_5
	move.l	d7,(a2)+
	clr.l	(a2)+
	and.l	#$3fffffff,d2
	cmp.l	#$000003e9,d2
	beq.s	Go_to_6
	cmp.l	#$000003ea,d2
	bne.s	Go_to_7
Go_to_6
	subq.l	#1,d3

Go_to_8
	move.l	(a3)+,(a2)+
	dbf	d3,Go_to_8
	bra.s	Go_to_9
Go_to_7
	cmp.l	#$000003eb,d2
	bne.s	Go_to_27
	subq.l	#1,d3
	moveq	#0,d5
Go_to_29:
	move.l	d5,(a2)+
	dbf	d3,Go_to_29
	bra.s	Go_to_9
Go_to_27
	bsr.w	Go_to_28
	movem.l	(a7)+,d1-d7/a1-a5
	moveq	#40,d0
	rts

Go_to_9
	dbf	d0,Go_to_1
	lea	Tab_Relo3(pc),a0
	lea	Tab_Relo1(pc),a1
	move.l	a1,a5
	movem.l	Tab_Relo2(pc),d0-d1
	subq.l	#1,d0
Go_to_30:
	move.l	(a0)+,a3
	movem.l	(a3)+,d2-d3
	and.l	#$3fffffff,d2
	and.l	#$3fffffff,d3
	move.l	(a1)+,a4
	add.l	d3,d3
	add.l	d3,d3
	add.l	d3,a3
	move.l	(a3)+,d2
	cmp.l	#$000003f2,d2
	beq.s	Go_to_15
	cmp.l	#$000003ec,d2
	bne.s	Go_to_15
Go_to_16:
	move.l	(a3)+,d2
	beq.s	Go_to_15
	move.l	(a3)+,d3
	sub.l	d1,d3
	add.l	d3,d3
	add.l	d3,d3
	move.l	0(a5,d3.w),d6
	addq.l	#8,d6
	subq.l	#1,d2
Go_to_17:
	move.l	(a3)+,d4
	add.l	d6,8(a4,d4.l)
	dbf	d2,Go_to_17
	bra.s	Go_to_16
Go_to_15
	dbf	d0,Go_to_30
	move.l	Tab_Relo1(pc),a0
	addq.l	#8,a0
	movem.l	(a7)+,d1-d7/a1-a5
	moveq	#0,d0
	rts
Hunk_1
	movem.l	(a7)+,d1-d7/a1-a5
	moveq	#48,d0
	rts

Hunk_2
	lea	Tab_Relo3(pc),a2
	move.l	d0,d3
	subq.l	#1,d3
Go_to_18:
	move.l	a0,(a2)+
	move.l	(a0),d4
	move.l	(a1)+,d5
	btst	#30,d5
	beq.s	Go_to_19
	bset	#6,4(a0)
Go_to_19:
	and.l	#$3fffffff,d4
	cmp.l	#$000003eb,d4
	bne.s	Go_to_20
	tst.l	(a0)+
	tst.l	(a0)+
	bra.s	Go_to_22
Go_to_20:
	and.l	#$3fffffff,d5
	add.l	d5,d5
	add.l	d5,d5
	addq.l	#8,d5
	add.l	d5,a0
Go_to_22:
	cmp.l	#$000003ec,(a0)
	bne.s	Go_to_24
	tst.l	(a0)+
Go_to_23:
	move.l	(a0)+,d4
	beq.s	Go_to_24
	tst.l	(a0)+
	add.l	d4,d4
	add.l	d4,d4
	add.l	d4,a0
	bra.s	Go_to_23
Go_to_24:
	cmp.l	#$000003f2,(a0)
	bne.s	Go_to_25
	tst.l	(a0)+
Go_to_25:
	cmp.l	#$000003e9,(a0)
	bne.s	Go_to_26
	tst.l	4(a0)
	bne.s	Go_to_26
	move.l	#$000003eb,(a0)
	move.l	(a1),4(a0)
Go_to_26
	dbf	d3,Go_to_18
	rts

;cant alloc memory

Memory_Error
	bsr.b	Go_to_28
	movem.l	(a7)+,d1-d7/a1-a5
	moveq	#56,d0
	rts
Go_to_28
	lea	Tab_Relo1(pc),a2
Go_to_14:
	move.l	(a2)+,d0
	beq.s	Go_to_21
	move.l	d0,a1
	move.l	(a1),d0

	;now you must dealloc memory for this hunk
	;example I use sys free mem

	jsr -$d2(a6)			;a6 = exec lib

	bra.s	Go_to_14
Go_to_21
	rts


Tab_Relo1
	blk.l	6,0

Tab_Relo2
	dc.l	0
	dc.l	0

Tab_Relo3
	blk.w	37,0

Tab_Relo_4
	dc.l	0

;If you have any questions or any sugestions then you may call to me at:
;
;			amigos@safona.mech.tuniv.szczecin.pl
