;==============================  COPYRIGHT  ===============================

;		Author:		Thorsten Hopf

;		Program:	XCrypt - crypt/decrypt using XPK
;		Version:	V1.0
;		Date:		06/01/94

;===============================  VERSION  ================================

PRGNAME		MACRO
		dc.b " XCrypt "
		ENDM
VERSION		MACRO
		dc.b "V1.0"
		ENDM
VersionDate:	MACRO
		dc.b	' (06.01.94)',0
		ENDM

;==========================  MACRO DEFINITIONS  ===========================

CALLSYS		MACRO
		jsr _LVO\1(a6)
		ENDM

;=========================  INCLUDE DEFINITIONS  ==========================
;	INCLUDE "dh0:machinecode/assembler/devpac/system.GS"
	INCDIR	"Include:"
	include	"dos/dos.i"
	include	"dos/datetime.i"
	include	"exec/exec.i"
	include	"LVO/exec_lib.i"
	include	"LVO/dos_lib.i"
	include	"MISC/XPK/xpk.i"
	include "MISC/XPK/xpksub.i"

;=========================  PROGRAM DEFINITIONS  ==========================

defaultcrypt	EQU	"IDEA"
optioncrypt	EQU	"FEAL"

;============================  STARTUP CODE  ==============================

start	lea		var(PC),a5

	movea.l		$4.w,a6
	suba.l		a1,a1
	CALLSYS	FindTask			;Eigenen Task suchen
	move.l		d0,a4
	move.l		d0,my_task-var(a5)

	lea		$5c(a4),a1
	move.l		a1,MSG_port-var(a5)	;Get tasks MessagePort

	movea.l		$4.w,a6
	lea		dosname-var(a5),a1
	moveq		#34,d0
	CALLSYS	OpenLibrary
	move.l		d0,dosbase-var(a5)

	move.l		dosbase-var(a5),a6
	CALLSYS	Output
	move.l		d0,output_handle-var(a5)
	beq.b	error_output

	move.l		$4.w,a0
	cmp.w		#36,LIB_VERSION(a0)
	blt.s	upgrade

	lea		xpkname-var(a5),a1
	moveq		#0,d0
	move.l		$4.w,a6
	CALLSYS	OpenLibrary
	move.l		d0,xpkbase-var(a5)
	beq.b	error_output

	bsr.b	cli_parser
	bne.b	not_opened

	lea		welcomemsg-var(a5),a0
	move.l		a0,d2
	moveq		#endwelcome-welcomemsg,d3
	bsr.b	print_error

	bsr.w	create_tmp_filename
	tst.l		d0
	bne.b	not_opened			;kein TMP-Filename gefunden
	bsr.w	XPK_support
	tst.l		d0
	bne.b	not_opened			;Fehler im XPK-Teil
	bsr.w	copy_file			;und file schreiben

	move.l		arguments-var(a5),d1
	move.l		dosbase-var(a5),a6
	CALLSYS	FreeArgs


not_opened
	move.l		$4.w,a6
	move.l		xpkbase-var(a5),a1
	CALLSYS	CloseLibrary

error_output
	move.l		$4.w,a6
	move.l		dosbase-var(a5),a1
	CALLSYS	CloseLibrary

error_opening
	moveq		#0,d0			;keine Errorcodes
	rts

upgrade	lea		upgrade_msg-var(a5),a0
	move.l		a0,d2
	moveq		#endupgrade-upgrade_msg,d3
	bsr.b	do_print
	bra.b	error_output

;=========================================================================
print_error
;
;	Uebergabeparameter :
;				d2 = Pointer auf auszugebenden Text
;				d3 = Laenge des auszugebenden Textes
;=========================================================================
	lea		arg_array-var(a5),a0
	tst.l		3*4(a0)			;quiet flag testen
	bne.b	quiet
do_print
	move.l		dosbase-var(a5),a6
	move.l		output_handle-var(a5),d1
	jmp	_LVOWrite(a6)
quiet	rts

;=========================================================================
cli_parser
;
;	Uebergabeparameter :	keine
;
;	Rueckgabe :		d0 = -1 IF ERROR, 0 sonst
;=========================================================================
	move.l		dosbase-var(a5),a6
	lea		template-var(a5),a0
	move.l		a0,d1
	lea		arg_array-var(a5),a0
	move.l		a0,d2
	moveq		#0,d3			;keine weitere Struktur
	CALLSYS	ReadArgs
	move.l		d0,arguments-var(a5)
	beq.b	arg_error

	lea		arg_array-var(a5),a0	;pointer auf Filename
	move.l		(a0),d0
	move.l		d0,input_1-var(a5)
	move.l		d0,input_2-var(a5)	;und in taglist
	move.l		d0,filename-var(a5)

	moveq		#15-1,d0		;15 Bytes max
	lea		password-var(a5),a1	;password umkopieren
	move.l		8(a0),a0
.loop	move.b		(a0)+,(a1)+
	dbf	d0,.loop
	moveq		#0,d0
	rts

arg_error
	lea		argerrormsg-var(a5),a0
	move.l		a0,d2
	moveq		#endmsg-argerrormsg,d3
	bsr.b	do_print
	moveq		#-1,d0
	rts

;=========================================================================
create_tmp_filename
;
;	Uebergabeparameter :	keine
;
;	Rueckgabe :		d0 = -1 IF ERROR, 0 sonst
;=========================================================================
	move.l		dosbase-var(a5),a6
	lea		datearray-var(a5),a0
	move.l		a0,d1
	CALLSYS	DateStamp			;Uhrzeit und Datum holen

	lea		datearray-var(a5),a0
	move.l		a0,d1
	CALLSYS	DateToStr			;und in lesbaren String wandeln
	tst.l		d0
	beq.b	string_error

	lea		outputname-var(a5),a1	;Uhrzeit zum Namen hinzufuegen
	lea		time_string-var(a5),a0
	moveq		#3-1,d0
.copy	move.b		(a0)+,(a1)+
	move.b		(a0)+,(a1)+
	addq.l		#1,a0
	dbf	d0,.copy

	moveq		#1,d0
string_error
	subq.l		#1,d0
	rts

;=========================================================================
XPK_support
;	Uebergabeparameter :	keine
;
;	Rueckgabe :		d0 = -1 IF ERROR, 0 sonst
;=========================================================================
	move.l		crypt1-var(a5),d0
	move.l		crypt2-var(a5),d1
	tst.l		arg_array+4-var(a5)
	beq.b	.skip
	move.l		d0,crypt2-var(a5)
	move.l		d1,crypt1-var(a5)

.skip	lea		error_buffer-var(a5),a0	;pointer auf XPK-Error-Buffer
	move.l		a0,errorbuf-var(a5)	;zum Ausgeben der Fehlermeldung

	move.l		xpkbase-var(a5),a6
	lea		infostruct-var(a5),a0
	lea		examine_tags-var(a5),a1
	CALLSYS	XpkExamine			;File anschauen, ob gecrypted
	tst.l		d0
	bne.b	error_xpkfunc

	lea		infostruct-var(a5),a0	;hier stehen jetzt die infos
	move.l		28(a0),d0		;packer name

	cmp.l		crypt1-var(a5),d0	;isset mit meinem Teil gecrypted?
	beq.b	decrypt_file			;ja : entcrypten

	cmp.l		crypt2-var(a5),d0	;sonst gecrypted ? dann default
	beq.b	decrypt_file			;entcrypten


	lea		pack_tags-var(a5),a0
	CALLSYS	XpkPack
	tst.l		d0
	bne.b	error_xpkfunc
no_error
	rts

decrypt_file
	lea		unpack_tags-var(a5),a0
	CALLSYS	XpkUnpack
	tst.l		d0
	beq.b	no_error

error_xpkfunc
	lea		error_buffer-var(a5),a0
	move.l		a0,d2
	move.b		#10,error_buffer+XPKERRMSGSIZE-var(a5)	;Return
	moveq		#XPKERRMSGSIZE+1,d3
	bsr.w	print_error
	moveq		#-1,d0
	rts

;=========================================================================
copy_file
;
;	Uebergabeparameter :	keine
;
;	Rueckgabe :		keine
;=========================================================================
	move.l		dosbase-var(a5),a6
	move.l		arg_array-var(a5),d1		;open inputfile
	move.l		#MODE_NEWFILE,d2
	CALLSYS	Open
	move.l		d0,inputhandle-var(a5)
	beq.b	error_opening_input

	lea		outputpath-var(a5),a0		;open temporary file
	move.l		a0,d1
	move.l		#MODE_OLDFILE,d2
	CALLSYS	Open
	move.l		d0,temphandle-var(a5)
	beq.b	error_opening_temp

	move.l		$4.w,a6
	move.l		length-var(a5),d0		;alloc mem for file
	moveq		#MEMF_PUBLIC,d1
	CALLSYS	AllocMem
	move.l		d0,file_memory-var(a5)
	beq.b	error_allocating

	move.l		dosbase-var(a5),a6
	move.l		temphandle-var(a5),d1		;Read temporary file
	move.l		file_memory-var(a5),d2
	move.l		length-var(a5),d3
	CALLSYS	Read
	beq.b	read_error

	move.l		inputhandle-var(a5),d1		;Write input file
	move.l		file_memory-var(a5),d2
	move.l		length-var(a5),d3
	CALLSYS	Write
	tst.l		d0
	beq.b	write_error
	
free_mem
	move.l		$4.w,a6				;Free memory
	move.l		file_memory-var(a5),a1
	move.l		length-var(a5),d0
	CALLSYS	FreeMem

error_allocating
	move.l		dosbase-var(a5),a6		;Close temporary file
	move.l		temphandle-var(a5),d1
	CALLSYS	Close
error_opening_temp
	move.l		inputhandle-var(a5),d1		;Close input file
	CALLSYS	Close
error_opening_input
	lea		outputpath-var(a5),a0		;Delete temporary file
	move.l		a0,d1
	CALLSYS	DeleteFile
	rts

read_error
	lea		readerrormsg-var(a5),a0
	moveq		#endreadmsg-readerrormsg,d3
	bra.b	skip	
write_error
	lea		writeerrormsg-var(a5),a0
skip	moveq		#endwritemsg-writeerrormsg,d3
	move.l		a0,d3
	bsr.w	print_error

	bra.b	free_mem

;==============================  VARIABLEN  ===============================
		cnop	0,4
;==========================================================================
		dc.b	0,"$VER:"
		PRGNAME
		VERSION
		VersionDate
		cnop	0,4
;==========================================================================
var:
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
dosbase		dc.l	0
xpkbase		dc.l	0
output_handle	dc.l	0
arguments	dc.l	0

MSG_port	dc.l	0
my_task		dc.l	0
my_user_port	dc.l	0

inputhandle	dc.l	0
temphandle	dc.l	0
file_memory	dc.l	0

dosname		DOSNAME
	cnop	0,2
xpkname		XPKNAME

	cnop	0,4

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

examine_tags	dc.l	XPK_InName
filename	dc.l	0			;spaeter durch CLI-Uebergabe
		dc.l	XPK_GetError		;pointer to the error message buffer
errorbuf	dc.l	0
		dc.l	TAG_DONE

pack_tags	dc.l	XPK_InName
input_1		dc.l	0			;hier pointer auf inputname
		dc.l	XPK_GetError
		dc.l	error_buffer
		dc.l	XPK_OutName
		dc.l	outputpath
		dc.l	XPK_PackMethod
		dc.l	crypt1
		dc.l	XPK_PackMode
		dc.l	100
		dc.l	XPK_Password
		dc.l	password
		dc.l	XPK_GetOutLen
		dc.l	length
		dc.l	TAG_DONE

unpack_tags	dc.l	XPK_InName
input_2		dc.l	0			;hier pointer auf inputname
		dc.l	XPK_GetError
		dc.l	error_buffer
		dc.l	XPK_OutName
		dc.l	outputpath
		dc.l	XPK_Password
		dc.l	password
		dc.l	XPK_GetOutLen
		dc.l	length
		dc.l	TAG_DONE

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

length		dc.l	0			;Filelaenge nach (de)crypten

crypt1		dc.l	defaultcrypt		;hier schreibt der parser den
		dc.w	0			;gewaehlten cryptmode rein
		dc.w	0			;nur fuer LW-Align !!
crypt2		dc.l	optioncrypt
		dc.w	0
		dc.w	0


datearray	dcb.l	3			;Struktur fuer DateStamp
		dc.b	0			;day of the week format
		dc.b	FORMAT_DOS		;date format dd-mmm-yy
		dc.l	day_of_the_week
		dc.l	date_string
		dc.l	time_string

day_of_the_week	dcb.b	16
date_string	dcb.b	16
time_string	dcb.b	16

	even
password	dcb.b	15			;maximal 15 Zeichen !
	even
outputpath	dc.b	"T:XCrypt"
outputname	dcb.b	6			;Datum und Uhrzeit ..
		dc.b	".TMP",0

	even
template	dc.b	"SOURCE/A,FEAL/S,PW/K/A,QUIET/S",0

	even
arg_array	dcb.l	4			;4 templates
error_buffer	dcb.b	XPKERRMSGSIZE+1		; +1 to make room for LF

	cnop 0,4
infostruct	dcb.b	94			;XpkExamine Info Struktur
	cnop 0,4

;================================  TEXTE  =================================

welcomemsg	dc.b	"Xcrypt Encrypter/Decrypter by Thorsten Hopf",10,0
endwelcome
	even
upgrade_msg	dc.b	"After 3 years you should think about upgrading ..",10,0
endupgrade
	even
argerrormsg	dc.b	"Error parsing arguments !",10,0
endmsg
	even
readerrormsg	dc.b	"Error reading file",10,0
endreadmsg
	even
writeerrormsg	dc.b	"Error writing file",10,0
endwritemsg
;==========================================================================
END
