; --------------------------------------------------------------------------------------
;
; This source file is part of PureBasic
; For the latest info, see http://www.purebasic.com/
; 
; Copyright (c) 1998-2006 Fantaisie Software
;
; This program is free software; you can redistribute it and/or
; modify it under the terms of the GNU General Public License
; as published by the Free Software Foundation; either version 2
; of the License, or (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
;
; --------------------------------------------------------------------------------------
;
; PureBasic encrypt tool...
;
;
; 09/09/2000
;   Fixed a little bug..
;
; 18/07/2000
;   First version. Based on the easy xor algorythm.
;   Fully ASM optimised for max speed (Byte only compare, could be long..)
;

#CRYPT = 1


Statement EncryptFile{*DestFile, *KeyFile, Length1.l, KeyLength.l}

  UNLK   a4

  MOVE.l  d0, a0
  MOVE.l  d1, a1
  MOVE.l  d3, d4
  MOVE.l  d1, d5

_TestLoop:
  MOVE.b  (a0) , d0  ; Read the destination byte
  MOVE.b  (a1)+, d1  ; Read the keyfile byte

  EOR.b   d1,(a0)+   ; Eor the keyfile with the destination byte

  SUBQ.l  #1, d3
  BNE    _Skip
  MOVE.l  d4,d3
  MOVE.l  d5,a1
_Skip:

  SUBQ.l  #1, d2
  BNE    _TestLoop
  RTS

End Statement


Function.l GetCliFileName{}
  SHARED   InputName$, OutputName$, KeyName$

  Dim ReadResult.l(4)

  a$ = "KEYFILE/A,INFILE/A,DELTA/A"

  *rdargs = ReadArgs_(&a$, &ReadResult(0), 0)

  If *rdargs           ; is some args ?

    If ReadResult(0)
      KeyName$ = Peek.s(ReadResult(0))
    EndIf


    If ReadResult(1)
      InputName$ = Peek.s(ReadResult(1))
    EndIf

    If ReadResult(2)
      OutputName$ = Peek.s(ReadResult(2))
    EndIf

  EndIf

  Function Return *rdargs
End Function

;
; Just pass the needed parameter and decrypt
;

Ok.l

CNIF #CRYPT = 1

  Ok = GetCliFileName{}

CELSE

  Ok = 1

  InputName$  = "Dh1:PureBasic_1.50/PureBasic/Compilers/PBCompiler"
  OutputName$ = "Ram:PBCompiler.delta"
  KeyName$    = "Dh3:PureBasic_old/PureBasic/PureLibraries/Font"

CEND

If Ok

  If ReadFile(0, KeyName$)
    *KeyFile = AllocMem(Lof(0), 0)
    ReadMem 0, *KeyFile, Lof(0)

    If ReadFile(1, InputName$)
      *OrigFile = AllocMem(Lof(1), 0)
      ReadMem 1, *OrigFile, Lof(1)

      EncryptFile{*OrigFile, *KeyFile, Lof(1), Lof(0)}

      If WriteFile(2, OutputName$)
        WriteMem 2, *OrigFile, Lof(1)
        CloseFile 2
      EndIf

      CloseFile 1
    EndIf

    CloseFile 0
  EndIf

EndIf



End

