	include	exec/memory.i
;	include	intuition/intuition.i
	include	intuition/intuition_lib.i
	include	exec/execbase.i
	include	exec/exec_lib.i
;	include	graphics/graphics_lib.i
	include	libraries/dos_lib.i
	include	libraries/dos.i
	include	libraries/reqtools.i
	include	libraries/reqtools_lib.i
;	include	misc/easystart.i

CALLREQ	MACRO
	move.l	reqtoolsBase,a6
	jsr	_LVO\1(a6)
	ENDM

***************************************************************
* open the intuition.library , dos.library , reqtools.library *
*************************************************************** 
	lea	intname(pc),a1
	moveq	#0,d0
	CALLEXEC	OpenLibrary
	move.l	d0,intuitionBase
	bne.s	intopen
	moveq	#10,d0
	rts
intopen	lea	dosname(pc),a1
	moveq	#0,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,dosBase
	beq	_CloseIntuition
	lea	reqtoolsname(pc),a1
	moveq	#REQTOOLSVERSION,d0
	clr.l	reqtoolsBase
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,reqtoolsBase
	beq	_CloseDos

********************************
* allocate requester structure *
********************************
	moveq	#RT_FILEREQ,d0
	sub.l	a0,a0
	CALLREQ	rtAllocRequestA
	move.l	d0,reqpoint
	beq	_CloseReq

********************************
*      hello message            *
********************************
	lea	hello.msg(pc),a1
	lea	reqOK(pc),a2
	sub.l	a3,a3
	sub.l	a4,a4
	sub.l	a0,a0
	CALLREQ	rtEZRequestA

*****************************
* get current dir in buffer *
*****************************
	move.l	#reqdefdir,d1
	move.l	#2048,d2
	CALLDOS	GetCurrentDirName

******************************************
* set current dir in requester structure *
******************************************
	move.l	reqpoint(pc),a1
	lea	reqtagini(pc),a0
	CALLREQ	rtChangeReqAttrA

*********************
* Load file routine *
*********************
reqnofileselect
	move.l	reqpoint(pc),a1
	lea	reqfilename,a2
	lea	reqtitleLD(pc),a3
	sub.l	a0,a0		;lea	reqtaglist,a0
	CALLREQ	rtFileRequestA
	tst.l	d0
	beq.s	reqnofileselect
	move.l	reqpoint(pc),a0
	move.l	rtfi_Dir(a0),D1	selected path
	moveq.l	#SHARED_LOCK,d2
	CALLDOS	Lock
	move.l	d0,dirinputlock
	beq	_exit1
	move.l	d0,d1
	CALLDOS	CurrentDir
	move.l	d0,oldcurrentdir
	move.l	#reqfilename,d1
	move.l	#SHARED_LOCK,d2
	CALLDOS	Lock		lock input file
	move.l	d0,lockinputfile
	beq	_exit1
	move.l	d0,d1
	move.l	#exambuffer,d2
	move.l	d2,a5
	CALLDOS	Examine		examine file
	tst.l	d0
	beq	_exit
	move.l	fib_Size(a5),d0	file length
	move.l	d0,fileinputlen
	beq	_exit
	cmp.l	#64,d0
	blo	_exit
	move.l	d0,d1
	moveq	#0,d2
nextsub7	addq.l	#1,d2
	subq.l	#7,d0
	bgt.s	nextsub7
	neg.l	d0
	add.l	d1,d0
	move.l	d0,filelen7
	asl.l	#3,d2
	move.l	d2,filelen8
	moveq	#MEMF_PUBLIC,d1
	CALLEXEC	AllocMem		allocate memory
	move.l	d0,fileinputaddr	for input file
	beq	_exit
	move.l	#reqfilename,d1
	move.l	#MODE_OLDFILE,d2
	CALLDOS	Open		open file for input
	move.l	d0,handleinputfile
	beq	_exit
	move.l	d0,d1
	move.l	fileinputaddr(pc),d2
	move.l	fileinputlen(pc),d3
	CALLDOS	Read		load file to reserved
	move.l	handleinputfile,d1	memory
	CALLDOS	Close		close input file

****************************************
* make random values for calculate RND *
****************************************
	move.l	#nextrndfromname,d0
	swap	d0
	eor.l	#reqfilename,d0
	move.l	d0,rndvalues
	move.l	fileinputaddr(pc),d0
	move.l	fileinputlen(pc),d1
	swap	d0
	eor.l	d1,d0
	move.l	d0,rndvalues+4
	move.l	oldcurrentdir(pc),d0
	lea	reqfilename,a0
nextrndfromname
	move.b	(a0),d1	| создает копию имени файла
	move.b	d1,128(a0)	|  dont remove!
	beq.s	rndfromnameend
	eor.b	d1,d0
	rol.l	#8,d0
	addq	#1,a0
	bra.s	nextrndfromname
rndfromnameend	move.l	d0,rndvalues+8

*****************************
* ask user what his do want *
*****************************
	lea	reqaskwhatdo0(pc),a1
	lea	reqaskwhatdo(pc),a2
	sub.l	a3,a3
	sub.l	a4,a4
	sub.l	a0,a0
	CALLREQ	rtEZRequestA
	cmp.l	#1,d0
	beq	encode_main
	tst.l	d0
	beq.s	Code32enableY
	move.l	#44,Code32enable
Code32enableY
***********************************
*    allocate memory for code     *
***********************************
	move.l	filelen8(pc),d0
	moveq	#MEMF_PUBLIC,d1
	CALLEXEC	AllocMem
	move.l	d0,bufferadr8
	beq	_exit

*********************
* Save file routine *
*    coded files    *
*********************
reqnofileselectS
	move.l	reqpoint(pc),a1
	lea	reqfilename,a2
	lea	reqtitleSV(pc),a3
	lea	reqtaglist(pc),a0
	CALLREQ	rtFileRequestA
	tst.l	d0
	beq.s	reqnofileselectS

	move.l	reqpoint(pc),a0
	move.l	rtfi_Dir(a0),D1	selected path
	moveq.l	#SHARED_LOCK,d2
	CALLDOS	Lock
	move.l	d0,diroutputlock
	beq	_exit1
	move.l	d0,d1
	CALLDOS	CurrentDir

	move.l	#reqfilename,d1
	move.l	#MODE_NEWFILE,d2
	CALLDOS	Open
	move.l	d0,handleoutputfile
	beq	_exit

;-----------------------------------------------------------------------
;=======================================================================
;-----------------------------------------------------------------------
; MAIN 1  >> CODE FILE <<
;***************************

	lea	simplebuffer,a0
	moveq.l	#3,d7
	move.l	fileinputlen(pc),d2
CodeLen1	move.b	d2,d0
	asr.l	#8,d2
	bsr	numbcode
	move.b	d0,1(a0)	|	special for 68000
	asr.w	#8,d0	|
	move.b	d0,(a0)	|
	addq.l	#2,a0	|
	bsr	RNDcalc
	and.b	#3,d0
	bne.s	CL1nospace
	move.b	#32,(a0)+
CL1nospace	dbf	d7,CodeLen1
	moveq.l	#3,d7
	move.l	filelen8,d2
CodeLen2	move.b	d2,d0
	asr.l	#8,d2
	bsr	numbcode
	move.b	d0,1(a0)	|	special for 68000
	asr.w	#8,d0	|
	move.b	d0,(a0)	|
	addq.l	#2,a0	|
	bsr	RNDcalc
	and.b	#3,d0
	bne.s	CL2nospace
	move.b	#32,(a0)+
CL2nospace	dbf	d7,CodeLen2

	moveq	#15,d7	| d7 - сколько сомволов уже есть (фиктив)
	lea	reqfilename+128,a1
CodeName	move.b	(a1)+,d0
	beq.s	CodeNamend
	addq.w	#1,d7
	cmp.b	#60,d7
	blo.s	CNoendline
	moveq	#0,d7
	move.b	#10,(a0)+
CNoendline	bsr	numbcode
	move.b	d0,1(a0)	| 68000
	asr.w	#8,d0	|
	move.b	d0,(a0)	|
	addq.l	#2,a0	|
	bsr	RNDcalc
	and.b	#3,d0
	bne.s	CodeName
	move.b	#32,(a0)+
	bra.s	CodeName
CodeNamend	bsr	numbcode
	move.b	d0,1(a0)	| 68000
	asr.w	#8,d0	|
	move.b	d0,(a0)	|
	addq.l	#2,a0	|
	move.b	#10,(a0)+	|
	move.b	#10,(a0)+	|

	move.l	#simplebuffer,d2
	move.l	a0,d3
	sub.l	d2,d3
	bsr	write2file	| write len,len7,filename CODED as numb

	move.l	bufferadr8(pc),a1
	move.l	fileinputaddr(pc),a2
	move.l	filelen8(pc),d7
	asr.l	#3,d7
	lea	CharCode.tab(pc),a0
	move.w	#$7f,d5
nextCode8	moveq	#6,d6
nextCode81	move.b	(a2)+,d0
	move.b	d0,d1
	asl.w	d1
	and.w	d5,d0
	move.b	(a0,d0.w),(a1)+
	dbf.w	d6,nextCode81
	asr.w	#8,d1
	and.w	d5,d1
	move.b	(a0,d1.w),(a1)+
	subq.l	#1,d7
	bne.s	nextCode8
	
	************
* FOR	move.l	filelen8,d3
* TEST	move.l	bufferadr8,d2
*	bsr	write2file
*	************

	move.l	filelen8(pc),d7
	move.l	bufferadr8(pc),a1
	lea	numbcode.tab(pc),a2
	moveq	#0,d6        номер строки в d6
CodeNextLine
	lea	simplebuffer,a0
	and.b	#$f,d6
	bsr	RNDcalc
	and.w	#7,d0
	sub.b	#4,d0
	bpl.s	CoNeLiNo1
	add.b	#4,d0
CoNeLiNo1	asl.w	#4,d0
	or.b	d6,d0
	move.b	(a2,d0.w),(a0)+	нимер строки (0-15)
	addq.l	#1,d6

	bsr	RNDcalc
	and.w	#$f,d0
	add.w	#50,d0
	move.w	d0,d1
CoNeChar	move.b	(a1)+,(a0)+
	subq.l	#1,d7
	beq.s	CoEndChar
	tst.l	Code32enable
	beq.s	CoNeChar1
	bsr	RNDcalc
	and.w	#$121,d0
	bne.s	CoNeChar1
	cmp.b	#32,-2(a0)
	beq.s	CoNeChar1
	move.b	#32,-1(a0)
	subq.l	#1,a1
	addq.l	#1,d7
CoNeChar1	dbf.w	d1,CoNeChar
	move.b	#10,(a0)+
	move.l	a0,d3
	move.l	#simplebuffer,d2
	sub.l	d2,d3
	bsr	write2file
	bra.s	CodeNextLine
CoEndChar	move.b	#10,(a0)+
	move.l	a0,d3
	move.l	#simplebuffer,d2
	sub.l	d2,d3
	bsr	write2file
	
	bra	_exit

*---------------------------------------------------------------
*===============================================================
*---------------------------------------------------------------
* MAIN 2 >>> DECODE FILE <<<
****************************
encode_main

	move.l	fileinputaddr(pc),a0
	move.l	a0,a1
	move.l	fileinputlen(pc),d0
RemoveSpace	cmp.b	#32,(a0)+
	beq.s	RemoveSp2
	move.b	-1(a0),(a1)+
RemoveSp2	subq.l	#1,d0
	bne.s	RemoveSpace
	move.b	#10,(a1)+
	move.b	#0,(a1)

	move.l	fileinputaddr(pc),a0
	move.l	a0,d7
	lea	numbdecode.tab(pc),a1
Search1line	move.b	(a0)+,d0
	beq	_exit
	cmp.b	(a0),d0
	bne.s	Search1line
	cmp.b	#10,d0
	bne.s	Search1line
	moveq.l	#0,d0
	move.b	-3(a0),d0
	move.b	(a1,d0.w),d0
	bne.s	Search1line
	move.b	-2(a0),d0
	move.b	(a1,d0.w),d0
	bne.s	Search1line
	bsr	testline1	a2=decoded area
	tst.l	d0
	bne.s	Search1line
FirstLineDe	
	addq.l	#1,a0
	move.l	a0,SecondLine
	subq.l	#8,a2
	move.b	1(a2),d0
	asl.b	#4,d0
	or.b	(a2),d0
	asl.w	#8,d0
	move.b	3(a2),d0
	asl.b	#4,d0
	or.b	2(a2),d0
	asl.l	#8,d0
	move.b	5(a2),d0
	asl.b	#4,d0
	or.b	4(a2),d0
	asl.l	#8,d0
	move.b	7(a2),d0
	asl.b	#4,d0
	or.b	6(a2),d0
	move.l	d0,FileLenDecode
	subq.l	#8,a2
	move.b	1(a2),d1
	asl.b	#4,d1
	or.b	(a2),d1
	asl.w	#8,d1
	move.b	3(a2),d1
	asl.b	#4,d1
	or.b	2(a2),d1
	asl.l	#8,d1
	move.b	5(a2),d1
	asl.b	#4,d1
	or.b	4(a2),d1
	asl.l	#8,d1
	move.b	7(a2),d1
	asl.b	#4,d1
	or.b	6(a2),d1
	move.l	d1,filelen8
	sub.l	d0,d1
	bmi	_Corrupt

	lea	reqfilename,a0
	lea	simplebuffer,a1
PutCodedName
	subq.l	#2,a2
	move.b	1(a2),d0
	asl.b	#4,d0
	or.b	(a2),d0
	move.b	d0,(a0)+
	cmp.l	a1,a2
	bne.s	PutCodedName
	move.b	#0,(a0)

	move.l	SecondLine,a0
	move.l	a0,a1
	moveq.l	#0,d7
	move.l	filelen8(pc),d6
	lea	numbdecode.tab(pc),a2
	lea	CharDecode.tab(pc),a3
DecodeLineN	moveq.l	#0,d0
	move.b	(a0)+,d0
	beq	_Corrupt
	cmp.b	#10,d0
	beq.s	DecodeLineN
	move.b	(a2,d0.w),d0
	bmi.s	DecLiNno1
	move.b	d0,d5
	bsr	testlineN
	tst	d0
	bmi.s	DecLiNno1
	cmp.b	d5,d7
	bne	DecLiNno1
	addq.l	#1,d7
	and.b	#$f,d7
DeLiNcopy	cmp.b	#10,(a0)+
	beq.s	DecodeLineN
	move.b	-1(a0),(a1)+
	subq.l	#1,d6
	bne.s	DeLiNcopy
	move.b	#0,(a1)
	bra.s	DecodeTestOk
DecLiNno1	cmp.b	#10,(a0)+
	beq.s	DecodeLineN
	tst.b	-1(a0)
	bne.s	DecLiNno1
	bra	_Corrupt

DecodeTestOk

*********************
* Save file routine *
*   Decoded files   *
*********************
reqnofileselectSD
	move.l	reqpoint(pc),a1
	lea	reqfilename,a2
	lea	reqtitleSV(pc),a3
	lea	reqtaglist(pc),a0
	CALLREQ	rtFileRequestA
	tst.l	d0
	beq.s	reqnofileselectSD

	move.l	reqpoint(pc),a0
	move.l	rtfi_Dir(a0),D1	selected path
	moveq.l	#SHARED_LOCK,d2
	CALLDOS	Lock
	move.l	d0,diroutputlock
	beq	_exit1
	move.l	d0,d1
	CALLDOS	CurrentDir

	move.l	#reqfilename,d1
	move.l	#MODE_NEWFILE,d2
	CALLDOS	Open
	move.l	d0,handleoutputfile
	beq	_exit
	
	
	move.l	filelen8(pc),d7
	asr.l	#3,d7
	move.l	SecondLine,a0
	move.l	a0,a1
	lea	CharDecode.tab(pc),a2
DecodeEndNx	moveq	#0,d0
	move.b	7(a0),d0
	move.b	(a2,d0.w),d1
	add.b	d1,d1
	moveq.l	#6,d6
DecEndNx2	move.b	(a0)+,d0
	move.b	(a2,d0.w),d0
	add.b	d0,d0
	asl.b	d1
	roxr.b	d0
	move.b	d0,(a1)+
	dbf.w	d6,DecEndNx2
	addq.l	#1,a0
	subq.l	#1,d7
	bne.s	DecodeEndNx

	move.l	SecondLine,d2
	move.l	FileLenDecode,d3
	bsr	write2file
	bra.s	_exit	
	
_Corrupt

	lea	reqCorrupt(pc),a1
	lea	reqOK(pc),a2
	sub.l	a3,a3
	sub.l	a4,a4
	sub.l	a0,a0
	CALLREQ	rtEZRequestA


****************************************
**  close all opened library and exit **
**  free requester sructure if opened **
****************************************
_exit
	move.l	lockinputfile,d1
	beq.s	_exit1
	CALLDOS	UnLock
_exit1
	move.l	dirinputlock,d1
	beq.s	_exit2
	CALLDOS	UnLock
_exit2
	move.l	diroutputlock,d1
	beq.s	_exit21
	CALLDOS	UnLock
_exit21
	move.l	handleoutputfile,d1
	beq.s	_exit22
	CALLDOS	Close
_exit22
	move.l	oldcurrentdir,d1
	bmi.s	_exit23
	CALLDOS	CurrentDir
_exit23
	move.l	reqpoint,a1
	CALLREQ	rtFreeRequest	
_CloseReq
	move.l	reqtoolsBase,a1
	CALLEXEC	CloseLibrary
_CloseDos
	move.l	dosBase,a1
	CALLEXEC	CloseLibrary
_CloseIntuition
	move.l	intuitionBase,a1
	CALLEXEC	CloseLibrary
	moveq	#0,d0
	tst.l	reqtoolsBase
	bne.s	noerr
	moveq	#25,d0
noerr
	move.l	d0,-(SP)

	moveq	#2-1,d7
	lea	allocmemlist,a5
nextdealloc	move.l	(a5),d0
	beq.s	notalloc
	move.l	d0,a1
	move.l	4(a5),d0
	CALLEXEC	FreeMem
notalloc	addq	#8,a5
	dbf.w	d7,nextdealloc

	move.l	(SP)+,d0
	rts
***************************************************************
***************************************************************


write2file	*	input:	d2 = point to write block
	*		d3 = length of write block
	movem.l	d0-d7/a0-a6,-(sp)
	move.l	handleoutputfile,d1
	;		move.l	fileoutputpoint,d2
	;		move.l	fileoutputlen,d3
	CALLDOS	Write
	movem.l	(sp)+,d0-d7/a0-a6
	rts

;********************************************
;* number code to 2 byte of text characters *
;*   input: d0.b= 8 bit number for code     *
;*   oputput: d0.w 2 byte coded number      *
;* format: hi15.b,low15.b	       *
;********************************************
numbcode	movem.l	d1/d2/a0,-(sp)
	move.b	d0,d1
	bsr	RNDcalc
	and.w	#7,d0
	sub.b	#4,d0
	bpl.s	numbcodeNo1
	add.b	#4,d0
numbcodeNo1	asl.w	#4,d0
	move.b	d1,d2
	and.w	#$0f,d2
	and.w	#$f0,d1
	asr.w	#4,d1
	or.b	d2,d0
	lea	numbcode.tab(pc),a0
	move.b	(a0,d0.w),d2

	bsr	RNDcalc
	and.w	#7,d0
	sub.b	#4,d0
	bpl.s	numbcodeNo2
	add.b	#4,d0
numbcodeNo2	asl.w	#4,d0
	or.b	d1,d0
	move.b	(a0,d0.w),d0
	asl.w	#8,d0
	move.b	d2,d0
	movem.l	(sp)+,d1/d2/a0
	rts

RNDcalc	movem.l	d1-d4/a0,-(sp)
	lea	rndvalues,a0
	move.l	(a0),d1
	move.l	4(a0),d2
	move.l	8(a0),d3
	move.l	d1,d4
	add.l	d2,d4
	add.l	d3,d4
	move.l	d2,d1
	move.l	d3,d2
	rol.l	d4
	move.l	d4,d3
	move.l	d1,(a0)+
	move.l	d2,(a0)+
	move.l	d3,(a0)
	move.l	d1,d0
	movem.l	(sp)+,d1-d4/a0
	rts

testline1	move.l	a0,-(sp)
	subq.l	#3,a0
	lea	simplebuffer,a2
	moveq.l	#0,d6
testl1next	move.b	-(a0),d0
	move.b	(a1,d0.w),d0
	bmi.s	Test1Error
	move.b	d0,(a2)+
	addq.l	#1,d6
	cmp.l	d7,a0
	beq.s	Test1Ok
	cmp.b	#10,-1(a0)
	bne.s	testl1next
Test1Ok	cmp.w	#17,d6
	blo.s	Test1Error
	move.l	(sp)+,a0
	moveq	#0,d0
	rts
Test1Error	moveq.l	#-1,d0
	move.l	(sp)+,a0
	rts
	
testlineN	move.l	a0,-(sp)
	moveq.l	#0,d1
textlNnex	cmp.b	#10,(a0)
	beq.s	TestlNend
	move.b	(a0)+,d1
	move.b	(a3,d1.w),d1
	bpl.s	textlNnex
	move.l	(sp)+,a0
	moveq.l	#-1,d0
	rts
TestlNend	move.l	(sp)+,a0
	moveq	#0,d0
	rts

	
********************************************************
reqtitleLD	dc.b	'Select File to load',0 ;заголовок риквестора
	even
reqtitleSV	dc.b	'Select file to SAVE',0 ;заголовок риквестора
	even
reqaskwhatdo	dc.b	'Decode|Code|Code Without Spaces',0
	even
reqaskwhatdo0	dc.b	'What do You want?',0
	even
reqCorrupt		dc.b	'Source file DAMAGE',0
	even
reqOK		dc.b	'Ok',0
	even

hello.msg	dc.b	"AKUUE v1.0b",10
	dc.b	"Written by Shin of LKR/USL",10,10
	dc.b	"PLEASE SEND BUG-REPORT TO",10,10
	dc.b	" 2:5020/1174.19@FidoNet",10
	dc.b	" 39:241/17.19@AmigaNet",10
	dc.b	" e-mail: shin-lkr@usa.net",10,0

	cnop	0,8

reqtaglist	dc.l	RTFI_Flags,FREQF_SAVE  ;REQ taglist
	dc.l	TAG_END
reqtagini	dc.l	RTFI_Dir,reqdefdir	;таглист для инициализации
	dc.l	TAG_END		;файлового риквестора

dosBase		dc.l	0
intuitionBase	dc.l	0
reqtoolsBase	dc.l	0
reqpoint		dc.l	0

*********************************************************
* allocated memory list 
* format : long - point of allocated memory (if zero then no allocated)
*          long - length of allocated memory

allocmemlist
fileinputaddr	dc.l	0
filelen7		dc.l	0

bufferadr8	dc.l	0
filelen8		dc.l	0

*********************************************************

fileinputlen	dc.l	0	; only read !
oldcurrentdir	dc.l	-1
Code32enable	dc.l	0

********************************************************

intname		INTNAME
	even
dosname		DOSNAME
	even
reqtoolsname	REQTOOLSNAME
	even


*********************************************************
*	TABLES FOR CODE / ENCODE                    *
*********************************************************
numbcode.tab	incbin	tables/numbCodeRndSort.tab
	; содержит 5 вариантов кодирования чисел от 0 до 15
numbdecode.tab	incbin	tables/numbDecodeRndSort.tab
	; для декодирования закодорованных чисел
	; если это не число , то возвращает -1.b
CharCode.tab	incbin	tables/code.tab
CharDecode.tab	incbin	tables/decode.tab


 

_DOSBase	equ	dosBase

********************************************************
	section	data,BSS

reqfilename		ds.b	256	;имя файла
reqdefdir		ds.b	4096	;полный путь и имя файла
exambuffer		ds.b	2048
simplebuffer	ds.b	256

lockinputfile	ds.l	1
handleinputfile	ds.l	1
handleoutputfile	ds.l	1
dirinputlock	ds.l	1
diroutputlock	ds.l	1
rndvalues		ds.l	3

FileLenDecode	ds.l	1
SecondLine		ds.l	1
********************************************************

* Format!

; 8 byte file length		0.w,1.w,2.w,3.w
; 8 byte file length ( even @ 8)	0.w,1.w,2.w,3.w
; N*2 byte of file name
; 2 byte end of filename (it`s zero decoded)
