; XOR program encryption, decoder is only 34 bytes
; Written by Tylisha C. Andersen

; Syntax:  XORPROG file.com

.model tiny
.code
.186
org 100h

start:  mov cl,ds:[80h]             ; get cmdline length
        xor ch,ch
        inc cx
        mov di,81h                  ; find first non-space
        mov al,' '
        repe scasb
        lea dx,[di-1]               ; dx = ptr to filename
        repne scasb                 ; find next space
        mov byte ptr [di-1],0       ; set to zero

        mov ax,3D02h                ; open the file
        int 21h
        jc ferror
        xchg bx,ax                  ; bx = ax
        mov ah,3Fh                  ; read file
        mov cx,0F000h               ; max 60k
        mov dx,offset buffer
        int 21h
        xchg cx,ax                  ; cx, bp = size
        mov bp,cx

        mov si,dx                   ; si, di = buffer
        mov di,dx
e_loop: lodsb                       ; encode using XOR
        xor al,8Eh
        stosb
        loop e_loop

        mov ax,4200h                ; reset to beginning
        xor dx,dx
        int 21h
        jc ferror

        mov ah,40h                  ; write encoded file
        lea cx,[bp+34]
        mov dx,offset decoder
        int 21h
        jc ferror

        mov ah,3Eh                  ; close file and return
        int 21h
        ret

ferror: mov ah,9                    ; output error string and return
        mov dx,offset error$
        int 21h
        ret

error$  db   'file access error', 13, 10, '$'
decoder db   060h, 0FAh, 0BEh, 00Fh, 001h, 0BFh, 000h, 0FEh
        db   057h, 0B9h, 013h, 000h, 0F3h, 0A4h, 0C3h, 0BEh
        db   022h, 001h, 0BFh, 000h, 001h, 0B9h, 000h, 0FDh
        db   0ACh, 034h, 08Eh, 0AAh, 0E2h, 0FAh, 061h, 0FBh
        db   0FFh, 0E6h

buffer:
end start
