{* The all new encryption programme>>>>
**   Author:  Neil S. <pp0u203d@liv.ac.uk>
**   Dates:   16/03/1995 - v0.95
**            22/03/1995 - v1.00
** ---------------------------------------------------
**   Notes:   This programme was written by me in
**            a friendly competition with a 
**            cryptographic friend of mine.
**            I make NO guarantees as to the security
**            of this utility.  I call it PLP: Pretty
**            Lousy Privacy!
** ----------------------------------------------------
**   Copyright:  Mine but thanks go to David Benn for
**               the include, submods and the Excellent
**   ReqEd progamme.  This programme is therefore 
**   declared Public Domain.
*}

#include <stddef.h>
#include <WbArg.h>
#include <fexists.h>

DECLARE FUNCTION LONGINT xRead&(fh,buf,length) LIBRARY dos
DECLARE FUNCTION LONGINT xWrite&(fh,buf,length) LIBRARY dos
DECLARE FUNCTION SHORTINT DeleteFile(DFH) LIBRARY dos

DECLARE SUB main
DECLARE SUB parsefile
DECLARE SUB handleiface
DECLARE SUB gadgetwait
DECLARE SUB fastencryptit
DECLARE SUB slowencrypt
DECLARE SUB closeiface
DECLARE SUB handlewbargs
DECLARE SUB handlecli
DECLARE SUB doiface
'DECLARE SUB handleiface
DECLARE SUB SHORTINT reporterror(SHORTINT n)
'DECLARE SUB closeiface

ADDRESS fh, buf, DFH

LONGINT length

QUIT=FALSE
title$=""
filename$="Filename"
dest$="Filename.enc"
key%=1
main
END

SUB handlewbargs
  SHARED QUIT,filename$,key%
  if wbargcount=0 then
    handleiface
  else
    if wbargcount>1 then 
      dum%=msgbox("Multiple File Encryption Not Yet Supported","Aborting")
      STOP
    else
      filename$=wbarg$(1)
      key%=INPUTBOX("Enter key (from -32000 to 32000):")
    end if
    parsefile
    QUIT=TRUE
  end if
end sub

sub handlecli
  SHARED QUIT,filename$,key%
  numcliargs=argcount
  if argcount=0 then
    handleiface
  else
    filename$=arg$(1)
    if filename$="?" then
      print "Usage: ";arg$(0);" filename key"
      stop
    else 
      if argcount>2 then
        keystr$=arg$(2)
        key%=VAL(keystr$)
      else
        key%=INPUTBOX("Enter Key (from -32000 to 32000):")
      end if
    end if
    parsefile
    QUIT=TRUE
  end if
end sub

SUB DoIface
  SHARED filename$
  WINDOW 9,"PLP Encryption",(192,116)-(541,216),22
  {* RENDER GADGETS, BEVEL-BOXES AND TEXT *}
  GADGET 255,ON,"F",(10,10)-(36,26),BUTTON
  GADGET 254,ON,9,(160,30)-(323,45),POTX
  GADGET 253,ON,filename$,(56,13)-(329,28),STRING
  GADGET 252,ON,"1",(16,33)-(153,48),LONGINT
  GADGET 251,ON,"GO!",(10,50)-(112,76),BUTTON
  GADGET 250,ON,"About",(125,50)-(211,76),BUTTON
  GADGET 249,ON,"Quit",(222,50)-(324,76),BUTTON
end sub

sub handleiface
  SHARED QUIT,filename$,key%
  shortint gadnum, n
  DoIface
  repeat
    gadget wait 0
    gadnum=gadget(1)
    if gadnum=255 then
      filename$=FileBox$("Select file to En/Decrypt")
      GADGET CLOSE 253
      GADGET 253,ON,filename$,(56,13)-(329,28),STRING
    else
      if gadnum=254 then
        num%=msgbox("Not yet implemented!","Ahhh. O.K.")
      else
        if gadnum=253 then
          filename$=CSTR(Gadget(2))
        else
          if gadnum=252 then
            key%=Gadget(2)
          else
            if gadnum=251 then
              parsefile
            else
              if gadnum=250 then
                num%=msgbox("Pretty Lousy Privacy by Neil S.","O.K.")
              else
                if gadnum=249 then
                  QUIT=TRUE
                end if
              end if
            end if
          end if
        end if
      end if
    end if
  until QUIT=TRUE
  closeiface
end sub

sub closeiface
  {* CLEAN UP *}
  FOR n=255 TO 249 STEP -1
    GADGET CLOSE n
  NEXT
  WINDOW CLOSE 9 
END SUB


sub parsefile
  SHARED title$,filename$,dest$,key%
  if right$(filename$,4)=".enc" then
    strlen=len(filename$)
    dest$=left$(filename$,(strlen-4))
    title$="Decrypting "+filename$+" to "+dest$
  else
    dest$=filename$+".enc"
    title$="Encrypting "+filename$+" to "+dest$
  end if
  if WBLaunched=TRUE then
    filename$=wbargpath$(1)+filename$
    dest$=wbargpath$(1)+dest$
  end if
  if fexists(filename$) then
    let avail=fre(2)
    open "I",#50,filename$
      let filelen=lof(50)
    close #50
    if filelen<avail then
      fastencryptit
    else
      slowencrypt
    end if
  else
    dum=msgbox("File does not exist","O.K.")
  end if
end sub

sub reporterror(n)
  case
    n=1 : dum=msgbox("Unable to open sourcefile","Abort.")
    n=2 : dum=msgbox("Source file empty","Abort.")
    n=3 : dum=msgbox("Unableto allocate memory","Abort.")
    n=4 : dum=msgbox("Error while reading from source file","Abort.")
    n=5 : dum=msgbox("Unable to open destinaion file","Abort.")
    n=6 : dum=msgbox("Error while writingto destination file","Abort.")
    n=7 : dum=msgbox("Error during Initialisation","Abort.")
  end case
  close #1
  STOP
end sub

SUB fastencryptit
  shared title$,filename$,dest$,key%,fh,length,buf
  RANDOMIZE key%
  window 5,title$,(192,116)-(541,216),22
  cls
  print "Using quick mode."
  if fexists(dest$) then
    print "Initialising...";
     res=DeleteFile(SADD(dest$))
     if res=FALSE then call reporterror(7)
     print "Done"
  end if
  print "Reading...";
  open "I",#1,filename$
    fh=handle(1) : if fh=0 then call reporterror(1)
    length=lof(1) : if length=0 then call reporterror(2)
    buf=ALLOC(length) : if buf=0 then call reporterror(3)
    if xRead(fh,buf,length) <> length then call Reporterror(4)
  close #1
  print "Done."
  print "Encrypting:"
  for c&=buf to (buf+length)
    bit%=peek(c&)
    dynamic%=(c&-buf)+INT(RND*256)
    newbit%=((bit% XOR dynamic%) mod 256)
    poke c&,newbit%
  next c&
  print "Done.
  print "Writing...";
  open "O",#1,dest$
    fh=handle(1) : if fh=0 then call reporterror(5)
    if xWrite(fh,buf,length) <> length then call reporterror(6)
  close #1
  print "Done."
  sleep for 1
  window close 5
END SUB

sub slowencrypt
  shared title$,filename$,dest$,key%
  RANDOMIZE key%
  window 5,title$,(192,116)-(541,216),22
  cls
  posi=1
  open "I",#3,filename$
  open "O",#4,dest$
  print "Working on ";lof(3);" bytes."
  while eof(3)=1
    com$=""
    strnpos=1
    LINE INPUT #1,strn$
    while 1=1
      a$=mid$(strn$,strnpos,1)
      if a$=chr$(0) then goto jumphere
      num=posi+INT(RND*256)
      b=((ASC(a$) XOR num) mod 256)
      c$=CHR$(b)
      com$=com$+c$
      posi=posi+1
      strnpos=strnpos+1
    wend
    jumphere:
      print del$
      repstrn$=str$((int(((posi/lof(3))*100))))
      repstrn$=repstrn$+" % done"
      print repstrn$;
      replen=len(repstrn$)
      del$=string$(replen,chr$(8))
      print #4,com$
  wend
  close #3
  close #4
  window close 5
end sub

SUB main
  SHARED QUIT
  repeat
    IF WBlaunched=TRUE then
      handleWBargs
    else
      handlecli
    end if
  until QUIT
  library close dos
END SUB
