;**************************************
;
;   ENCODE - file encryption utility
;
;**************************************
;1.0 16-May-84 DFP written by D. Pallmann.
;1.1 05-Sep-84 DFP encoding algorithm corrected;  added auto-help.

       VMAJOR=1
       VMINOR=1

       SEARCH  SYS
       SEARCH  SYSSYM

       .OFINI
       .OFDEF  IN,D.DDB                ;input file
       .OFDEF  OUT,D.DDB               ;output file
       .OFDEF  KEY,512.                ;encryption key
       .OFSIZ  MEMSIZ

START:  PHDR    -1,0,PH$REE!PH$REU      ;program header
       GETIMP  MEMSIZ,A5               ;allocate impure area

HELP:   BYP                             ;bypass leading white space
       LIN                             ;end of line?                   1.1
       JNE     GETIN                   ; no                            1.1
       TYPECR  <Function: encodes and decodes files.>
       TYPECR  <Usage:    .ENCODE file,key>
       TYPECR  <Example:  .ENCODE TEST.BAS,HOWARD>
       CRLF                            ;newline                        1.1
       EXIT                            ;exit                           1.1

GETIN:  MOV     A2,A3                   ;save line index
       FSPEC   IN(A5),TXT              ;process filespec
       INIT    IN(A5)                  ;load driver, allocate buffer

GETOUT: MOV     A3,A2                   ;restore line index
       FSPEC   OUT(A5),TXT             ;process filespec
       INIT    OUT(A5)                 ;load driver, allocate buffer
       MOVW    #[$$$],OUT+D.EXT(A5)    ;set output extension to .$$$

GETKEY: BYP                             ;bypass leading white space
       LEA     A0,KEY(A5)              ;index key storage area
10$:    LIN                             ;end of line?                   1.1
       BEQ     20$                     ; yes - end-of-key              1.1
       MOVB    (A2)+,D1                ;get next char of key
       BEQ     20$                     ;branch if end-of-key
       MOVB    D1,(A0)+                ;store byte
       BR      10$                     ;loop till entire line processed
20$:    CLRB    @A0                     ;terminate key buffer
       LEA     A0,KEY(A5)              ;reset key buffer index

OPNIN:  OPENI   IN(A5)                  ;open input file

OPNOUT: LOOKUP  OUT(A5)                 ;check for existence of output file
       BNE     10$                     ;not found
       DSKDEL  OUT(A5)                 ;delete output file
10$:    OPENO   OUT(A5)                 ;open output file

GETBYT: CTRLC   ENDFIL                  ;branch on ^C
       FILINB  IN(A5)                  ;read byte
       TST     IN+D.SIZ(A5)            ;test for end-of-file
       BEQ     ENDFIL                  ;branch on end-of-file

CRYPT:  MOVB    (A0)+,D0                ;D0 := next char of key
       BNE     10$                     ;branch if not end-of-key
       LEA     A0,KEY(A5)              ;else reset key index
       BR      CRYPT                   ;and loop back to start of key  1.1
10$:    XORB    D0,D1                   ;XOR char with key byte

PUTBYT: FILOTB  OUT(A5)                 ;output encrypted byte
       BR      GETBYT                  ;loop till end of file

ENDFIL: CLOSE   IN(A5)
       CLOSE   OUT(A5)
       CTRLC   ABORT

DELIN:  DSKDEL  IN(A5)

RENOUT: MOV     IN+D.FIL(A5),OUT+D.DDB(A5)
       MOVW    IN+D.EXT(A5),OUT+D.DDB+4(A5)
       DSKREN  OUT(A5)
       EXIT

ABORT:  DSKDEL  OUT(A5)
       EXIT

       END