NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/md5
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/MLI.E.I
*--------------------------------------
FILEBUF.SIZE	.EQ 4096
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
ZPDataBufPtr	.BS 2
ZPDataLen		.BS 2

ArgIndex		.BS 1
Arg				.BS 1
bText			.BS 1
hFile			.BS 1
hDataBuf		.BS 1
hMD5Ctx			.BS 1

ZS.END			.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA 0
				.DA CS.END-CS.START		CS
				.DA DS.END-DS.START		DS
				.DA #16					SS
				.DA #ZS.END-ZS.START	ZP
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT		
				.DA	CS.QUIT
L.MSG.USAGE		.DA MSG.USAGE
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			ldy #S.PS.ARGC
				lda (pPS),y
				beq .99

.1				inc ArgIndex
				lda ArgIndex
				>SYSCALL ArgV
				bcs .8
				
				>STYA ZPPtr1
				lda (ZPPtr1)
				cmp #'-'
				bne .4
				ldy #1 
				lda (ZPPtr1),y
				beq .99

				ldx #OptionVars-OptionList-1

.2				cmp OptionList,x
				beq .3
				dex
				bpl .2

.99				>PUSHW L.MSG.USAGE
				>PUSHBI 0
				>SYSCALL PrintF
				lda #E.SYN
				sec
				rts

.3				ldy OptionVars,x
				lda #$80
				sta 0,y
				bra .1

.4				lda Arg
				bne .99
				
				lda ArgIndex
				sta Arg
				bra .1
				
.8				lda Arg
				beq .99
				
				lda bText
				beq CS.RUN.FILE
*--------------------------------------
CS.RUN.TEXT		lda Arg
				>SYSCALL ArgV
				>PUSHYA
				>PUSHEA.G MD5Buf
				>SYSCALL MD5
				jmp CS.RUN.PRINT
*--------------------------------------
CS.RUN.FILE		>LDYAI FILEBUF.SIZE
				>SYSCALL GetMem
				bcs .9
				
				>STYA ZPDataBufPtr
				stx hDataBuf
				
				lda Arg
				>SYSCALL ArgV
				>PUSHYA
				>PUSHBI	O.RDONLY
				>PUSHBI 0				ftype
				>PUSHWZ					Aux type
				>SYSCALL FOpen
				bcs .9

				sta hFile
				
				>SYSCALL MD5Init
				bcs .9
				
				sta hMD5Ctx
				
.1				>PUSHWI FILEBUF.SIZE	Bytes To Read
				>PUSHW ZPDataBufPtr		Dst Ptr
				lda hFile
				>SYSCALL FRead
				bcc .2
				
				eor #MLI.E.EOF
				beq .8
				
.9				rts

.2				>STYA ZPDataLen

				>PUSHB hMD5Ctx
				>PUSHW ZPDataBufPtr
				>PUSHW ZPDataLen
				>SYSCALL MD5Update
				bra .1

.8				>PUSHB hMD5Ctx
				>PUSHEA.G MD5Buf
				>SYSCALL MD5Finalize
				
CS.RUN.PRINT	>LEA.G MD5Buf
				>SYSCALL PutS
				lda #0
*				sec
*				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			lda hDataBuf
				beq .2
				
				>SYSCALL FreeMem
.2				lda hFile
				beq .3
	
				>SYSCALL FClose

.3				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
OptionList		.AZ "Dd"
OptionVars		.DA #bText,#bText
*--------------------------------------
MSG.USAGE		.AZ "Usage : MD5 [ -d input text | input file ]\r\n"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
MD5Buf			.BS 33
DS.END			.ED
*--------------------------------------
MAN
SAVE USR/SRC/BIN/MD5.S
ASM
