;********************************************
;*                                          *
;* File Coder v1.0 (c)1995 by ScoTT/Inferno *
;*                                          *
;*   Program koduje pliki korzystajâc z     *
;*    funkcji logicznej exclusive or z      *
;*     parametrem podanym jako klucz        *
;*                                          *
;********************************************

;--------------------------- "rozprawienie sië" z parametrami
	move.l	d0,d5
	movea.l	a0,a5
	subq.l	#1,d5
	beq	usage
	lea	filename,a1
search:
	cmp.b	#$20,(a5)
	beq	space
	cmp.b	#$0a,(a5)
	beq	usage
	move.b	(a5)+,(a1)+
	bra	search
space:	clr.b	0(a1)
	lea	key,a1
	move.b	(a5)+,(a1)
	cmp.b	#$0a,(a5)
	beq	usage
	cmp.b	#$0a,1(a5)
	beq	usage
	cmp.b	#$0a,2(a5)
	beq	usage
	cmp.b	#$0a,3(a5)
	beq	usage
	move.l	(a5),key

;--------------------------- otwieramy dos.library
	move.l	4,a6
	lea	dosname(pc),a1
	clr.l	d0
	jsr	-552(a6)	;OpenLibrary
	tst.l	d0
	beq	byebye
	move.l	d0,dosbase

;--------------------------- otwieramy plik
open_file:
	move.l	d0,a6
	move.l	#filename,d1
	move.l	#$3ed,d2
	jsr	-30(a6)	
	tst.l	d0
	beq	byebye
	move.l	d0,filehandle

;--------------------------- jaki dîugi plik
	move.l	d0,d1
	clr.l	d2
	moveq.l	#1,d3		;offset_end
	jsr	-66(a6)		;Seek
	move.l	filehandle,d1
	clr.l	d2
	moveq.l	#-1,d3		;offset_beginning
	jsr	-66(a6)		;Seek - przesuniëcie wskaúnika zbioru
				;z powrotem z koïca na poczâtek zbioru
	move.l	d0,file_length	;w d0 dîugoôê zbioru w bajtach

;--------------------------- alokacja pamiëci
	move.l	4,a6
	move.l	#$10001,d1
	jsr	-198(a6)
	tst.l	d0
	beq	close_file
	move.l	d0,membase

;--------------------------- wczytanie pliku
	move.l	filehandle,d1
	move.l	membase,d2
	move.l	file_length,d3
	move.l	dosbase,a6
	jsr	-42(a6)		;read
	tst.l	d0
	beq	close_file

;--------------------------- no to kodujemy
	move.l	membase,a1
	move.l	file_length,d1
	lsr.l	#2,d1
	move.l	key,d2
petla:
	eor.l	d2,(a1)+
	dbf	d1,petla      

;--------------------------- zapisujemy plik
	move.l	filehandle,d1
	clr.l	d2
	moveq.l	#-1,d3
	jsr	-66(a6)		;Seek

	move.l	filehandle,d1
	move.l	membase,d2
	move.l	file_length,d3
	jsr	-48(a6)		;Write

;--------------------------- zamykamy go
close_file:
	move.l	dosbase,a6
	move.l	filehandle,d1
	jsr	-36(a6)

;--------------------------- zwalniamy pamiëê
free_mem:
	move.l	4,a6		;execbase
	move.l	membase,a1
	move.l	file_length,d0
	jsr	-210(a6)
	clr.l	d0
	bra	close_lib

;--------------------------- okienko z "instrukcjâ"
usage:
	move.l	4,a6
	lea	dosname(pc),a1
	clr.l	d0
	jsr	-552(a6)	;OpenLibrary
	tst.l	d0
	beq	byebye
	move.l	d0,dosbase
	move.l	dosbase,a6
	
	jsr	-60(a6)
	move.l	d0,windowbase

	move.l	#stext,d2	;druk tekstu
	move.l	#etext-stext,d3
	move.l	dosbase,a6
	move.l	windowbase,d1
	jsr	-48(a6)
;--------------------------- zamykamy dos.library
close_lib:
	move.l      4,a6
	move.l	dosbase,a1
	move.l	4,a6
	jsr	-414(a6)	;CloseLibrary

byebye: 
	clr.l	d0
	rts

filename:	blk.b	200
dosname:	dc.b	'dos.library',0
	even
dosbase:	dc.l	0
filehandle:	dc.l	0
file_length	dc.l	0
membase:	dc.l	0
key:		dc.l	0

windowbase:	dc.l 0
stext:		dc.b	$0A
		dc.b	$0a,$1b,'[33mFileCoder 1.0',$1b,'[0m (c) 1995 by ScoTT/Inferno'
		dc.b	$0A
		dc.b	$0A
		dc.b	'Usage: FileCoder filename 4chars-key',$0A,$0A
etext:
