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

				ldy #OptionVars-OptionList-1

.2				cmp OptionList,y
				beq .3
				dey
				bpl .2

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

.3				ldx OptionVars,y
				sec
				ror 0,x
				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				>SLEEP
				
				>PUSHB hFile
				>PUSHW ZPDataBufPtr		Dst Ptr
				>PUSHWI FILEBUF.SIZE	Bytes To Read
				>SYSCALL FRead
				bcc .2
				
				cmp #MLI.E.EOF
				beq .8
				
.9				rts

.2				>STYA ZPDataLen

				>SLEEP
				
				>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		.AS "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
