DECLARE SUB SetSpecialData (Register$)
DECLARE FUNCTION GetSpecialData$ ()
DECLARE FUNCTION SearchEXE$ (FileName$, Key$, DatLen%)
'Program to demonstrate cloning keys into .EXE'S

DEFINT A-Z

DIM SHARED SpotForKey$, BytesToData&

  
CLS
  
PRINT "This program lets you enter your name as part of a registration process."
PRINT : PRINT
Nme$ = GetSpecialData$
IF Nme$ = "" THEN
   PRINT "This program is not registered."
   INPUT "Please enter your name: ", Nme$
   Nme$ = RTRIM$(LEFT$(Nme$, 29)) + CHR$(0)  'This makes name 30 or less
   SetSpecialData Nme$                       'and ends it with a null
   PRINT : PRINT "Thank you for registering."
   END                                       'Next time you run it, it will
                                             'display your name
ELSE
   PRINT "This program is registered to "; Nme$
   END
END IF

FUNCTION GetSpecialData$ STATIC

SpotForKey$ = "–—˜™123456789012345678901234567890"
TempKey$ = LEFT$(SpotForKey$, 4)
DataLen = 30

Dat$ = SearchEXE$("CLONE.EXE", TempKey$, DataLen)
Null = INSTR(Dat$, CHR$(0))
IF Null THEN
   GetSpecialData$ = LEFT$(Dat$, (Null - 1))
ELSE
   GetSpecialData$ = ""
END IF

END FUNCTION

FUNCTION SearchEXE$ (FileName$, Key$, DatLen) STATIC

'The Bytes% variable is the number of bytes to read. If your program is
'less than 16000 bytes then this routine adjusts accordinly. Also, if
'a 16000 byte "GET" cuts the marker in two, you don't have to worry about
'it because we adjust the file pointer accordingly.

Bytes = 16000
BytesToData& = 0
Portion& = 1
CountToKey = 0

OPEN FileName$ FOR BINARY AS #1
FiSize& = LOF(1)
DO WHILE NOT EOF(1)
   IF Bytes > FiSize& THEN Bytes = FiSize&
   IF Bytes + Portion& > FiSize& THEN Bytes = FiSize& - Portion&
   SEEK #1, Portion&
   A$ = INPUT$(Bytes, 1)

   CountToKey = INSTR(A$, Key$)
   IF CountToKey THEN
      BytesToData& = Portion& + CountToKey + LEN(Key$) - 1
      SEEK #1, BytesToData&
      SearchEXE$ = INPUT$(DatLen, 1)
      CLOSE #1
      EXIT FUNCTION
   END IF

   Portion& = Bytes + Portion& - LEN(Key$) 'We subtract the key length in case
                                          'it was split by our read
LOOP

CLOSE #1
PRINT "Error. Could not find key!"
END

END FUNCTION

SUB SetSpecialData (Register$)

Key$ = LEFT$(SpotForKey$, 4)

OPEN "CLONE.EXE" FOR BINARY AS #1
PUT #1, BytesToData&, Register$
CLOSE #1

END SUB

