Newbie - Can't find handler near Initialize, char 1 error

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
gwbasic
Posts: 34
Joined: Tue Mar 16, 2010 6:39 am

Newbie - Can't find handler near Initialize, char 1 error

Post by gwbasic » Thu Mar 25, 2010 11:50 pm

Newbie here. Just finished converting a code to Rev and placed in Stack script and called it from a button but got error. I can't understand the error. Please help.

EncryptDecrypt "the quick brown fox", "12345ABCDEF"

Code: Select all

global sbox
global key

Function EncryptDecrypt plaintxt, psw
   put 0 into temp
   put 0 into  a
   put 0 into  i
   put 0 into  j
   put 0 into  k
   put 0 into  cipherby
   put "" into  cipher
   
   put 0 into  z
      
   Initialize(  psw )
   
   repeat with a = 1 to Length(plaintxt) 
      put ( (i + 1)  mod 256 ) into i
      	
      if i = 0 then put 1 into i
      	
      put ( (j + sbox[i])  mod 256 ) into j
      	
      if j = 0 then put 1 into j
      	
      put sbox[i] into temp
      put sbox[j] into sbox[i]
      put temp into sbox[j]
      
      put ((sbox[i] + sbox[j])  mod 256) into z
      	
      if z = 0 then put 1 to z
      
      put sbox[ z ] into k
      	
      if k = 0 then put 1 into k
      
      put ( Asc(Mid(plaintxt, a, 1))  bitXOr k ) into cipherby
      put cipher + Chr(cipherby) into cipher 
   end repeat
   
   return cipher
end EncryptDecrypt

function Initialize  pwd 
   
   put 0 into tempSwap
   put 0 into a
   put 0 into b
   put 0 into intLength
   
   put length(strPwd) into intLength
   
   repeat with a = 1 to 256
      put Asc( SubStr(strpwd, (a mod intLength)+1, 1) ) into key[a] 
      put a into sbox[a]
   end repeat
   
   put 1 into b
   
   repeat with a = 1 to 256
      put (( b + sbox[a] + key[a] ) mod 256 ) into b
      	
      if b = 0 then put 1 into b
      	
      put sbox[a] into tempSwap
      put sbox[b] into sbox[a]
      put tempSwap into sbox[b]
   end repeat
   
end Initialize

function SubStr thestring, from_pos,  to_pos  
   put char from_pos to from_pos of thestring into PartString
   return PartString
end SubStr 

function Asc theChar
   return charToNum(theChar)
end Asc

function Chr theASCIIcode
   return numToChar( theASCIIcode )
end Chr


BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Newbie - Can't find handler near Initialize, char 1 error

Post by BvG » Fri Mar 26, 2010 12:47 am

functions can't be called by their own. use a command instead if you want to do that. or make a cheap workaround.

Code: Select all

on mouseUp
  put initialise("blah") into myGarbage
end mouseUp
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Newbie - Can't find handler near Initialize, char 1 error

Post by bn » Fri Mar 26, 2010 12:56 am

Hi gwbasic,
Initialize( psw )
you have to call the function with e.g.

Code: Select all

get   Initialize(  psw )
then it will hold what is returned from the function. In your case you make "initialize" a function but do not return anything from "initialize"
That is what commands are for.
If you would change it to

Code: Select all

on initialize
--- your code
end initialize
then you could call it with

Code: Select all

initialize psw
later on are some syntactical errors, I corrected them. Check for the differences. The code runs, I did not check the logic behind it.

Code: Select all

global sbox
global gKey

on mouseUp
   put EncryptDecrypt ("the quick brown fox", "12345ABCDEF")
end mouseUp


Function EncryptDecrypt plaintxt, psw
   put 0 into temp
   put 0 into  a
   put 0 into  i
   put 0 into  j
   put 0 into  k
   put 0 into  cipherby
   put "" into  cipher
   
   put 0 into  z
   
   -- you made initialize a function although it does not return anything
   -- you could make it a command
   -- if you want to keep it a functiont then you have to use the function syntax
   get Initialize(psw)
   
   repeat with a = 1 to Length(plaintxt) 
      put ( (i + 1)  mod 256 ) into i
      
      if i = 0 then put 1 into i
      
      put ( (j + sbox[i])  mod 256 ) into j
      
      if j = 0 then put 1 into j
      
      put sbox[i] into temp
      put sbox[j] into sbox[i]
      put temp into sbox[j]
      
      put ((sbox[i] + sbox[j])  mod 256) into z
      
      if z = 0 then put 1 into z
      
      put sbox[ z ] into k
      
      if k = 0 then put 1 into k
      put ( Asc(Mid(plaintxt, a, 1))  bitXOr k ) into cipherby
      put cipher & (Chr(cipherby)) into cipher 
   end repeat
   
   return cipher
end EncryptDecrypt

function Initialize  pwd 
   
   put 0 into tempSwap
   put 0 into a
   put 0 into b
   put 0 into intLength
   
   put length(strPwd) into intLength
   
   repeat with a = 1 to 256
      put Asc( SubStr(strpwd, (a mod intLength)+1, 1) ) into gKey[a] 
      put a into sbox[a]
   end repeat
   
   put 1 into b
   
   repeat with a = 1 to 256
      put (( b + sbox[a] + gKey[a] ) mod 256 ) into b
         
      if b = 0 then put 1 into b
         
      put sbox[a] into tempSwap
      put sbox[b] into sbox[a]
      put tempSwap into sbox[b]
   end repeat
   
end Initialize

function SubStr thestring, from_pos,  to_pos  
   put char from_pos to from_pos of thestring into PartString
   return PartString
end SubStr 

function Asc theChar
   return charToNum(theChar)
end Asc

function Chr theASCIIcode
   return numToChar( theASCIIcode )
end Chr
be careful what command names you choose. Dont use reserved words. It is good practice to prepend your variable name with a character to make the code easier to read and at the same time you avoid to use a reserved word.
many here use tVariableName for local variables, sVariableName for script local variables, gVariableName for global variables, uVariableName for custom properties
I changed your global variable Key to gKey, first to indicate it is a global varible and then to get it away from key which is a reserved word. You can see it from the coloring in the script editor which words are "known" to Rev and which are not = your variables or literals.

regards
Bernd

gwbasic
Posts: 34
Joined: Tue Mar 16, 2010 6:39 am

Re: Newbie - Can't find handler near Initialize, char 1 error

Post by gwbasic » Fri Mar 26, 2010 1:04 am

Thank you very much. I'm still adapting on how thing are done in Rev. I still get mixed up with a lot of stuffs.

gwbasic
Posts: 34
Joined: Tue Mar 16, 2010 6:39 am

Re: Newbie - Can't find handler near Initialize, char 1 error

Post by gwbasic » Fri Mar 26, 2010 1:14 am

bn wrote:Hi gwbasic,
Initialize( psw )
you have to call the function with e.g.

Code: Select all

get   Initialize(  psw )
then it will hold what is returned from the function. In your case you make "initialize" a function but do not return anything from "initialize"
That is what commands are for.
If you would change it to

Code: Select all

on initialize
--- your code
end initialize
then you could call it with

Code: Select all

initialize psw
later on are some syntactical errors, I corrected them. Check for the differences. The code runs, I did not check the logic behind it.

Code: Select all

global sbox
global gKey

on mouseUp
   put EncryptDecrypt ("the quick brown fox", "12345ABCDEF")
end mouseUp


Function EncryptDecrypt plaintxt, psw
   put 0 into temp
   put 0 into  a
   put 0 into  i
   put 0 into  j
   put 0 into  k
   put 0 into  cipherby
   put "" into  cipher
   
   put 0 into  z
   
   -- you made initialize a function although it does not return anything
   -- you could make it a command
   -- if you want to keep it a functiont then you have to use the function syntax
   get Initialize(psw)
   
   repeat with a = 1 to Length(plaintxt) 
      put ( (i + 1)  mod 256 ) into i
      
      if i = 0 then put 1 into i
      
      put ( (j + sbox[i])  mod 256 ) into j
      
      if j = 0 then put 1 into j
      
      put sbox[i] into temp
      put sbox[j] into sbox[i]
      put temp into sbox[j]
      
      put ((sbox[i] + sbox[j])  mod 256) into z
      
      if z = 0 then put 1 into z
      
      put sbox[ z ] into k
      
      if k = 0 then put 1 into k
      put ( Asc(Mid(plaintxt, a, 1))  bitXOr k ) into cipherby
      put cipher & (Chr(cipherby)) into cipher 
   end repeat
   
   return cipher
end EncryptDecrypt

function Initialize  pwd 
   
   put 0 into tempSwap
   put 0 into a
   put 0 into b
   put 0 into intLength
   
   put length(strPwd) into intLength
   
   repeat with a = 1 to 256
      put Asc( SubStr(strpwd, (a mod intLength)+1, 1) ) into gKey[a] 
      put a into sbox[a]
   end repeat
   
   put 1 into b
   
   repeat with a = 1 to 256
      put (( b + sbox[a] + gKey[a] ) mod 256 ) into b
         
      if b = 0 then put 1 into b
         
      put sbox[a] into tempSwap
      put sbox[b] into sbox[a]
      put tempSwap into sbox[b]
   end repeat
   
end Initialize

function SubStr thestring, from_pos,  to_pos  
   put char from_pos to from_pos of thestring into PartString
   return PartString
end SubStr 

function Asc theChar
   return charToNum(theChar)
end Asc

function Chr theASCIIcode
   return numToChar( theASCIIcode )
end Chr
be careful what command names you choose. Dont use reserved words. It is good practice to prepend your variable name with a character to make the code easier to read and at the same time you avoid to use a reserved word.
many here use tVariableName for local variables, sVariableName for script local variables, gVariableName for global variables, uVariableName for custom properties
I changed your global variable Key to gKey, first to indicate it is a global varible and then to get it away from key which is a reserved word. You can see it from the coloring in the script editor which words are "known" to Rev and which are not = your variables or literals.

regards
Bernd
I pasted the code above but when I tried to save it I got this error:

Code: Select all

stack "mytest": compilation error at line 10 (Chunk: can't create a variable with that name (explicitVariables?)) near "temp", char 7
Please help again.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Newbie - Can't find handler near Initialize, char 1 error

Post by bn » Fri Mar 26, 2010 1:22 am

do you have explicit variables turned on? it is in the preferences under script editor and is called strict compilation mode. If that is checked uncheck it and try again
bernd

edit: put the code as it is into a new stack, single button, set the script of the button the the revised script I posted and try it. Strict compilation mode turned off. It should work and put something into the message box. I just copied the script from a button and it compiled and ran.

have a look at this:
http://www.fourthworld.com/embassy/arti ... style.html

gwbasic
Posts: 34
Joined: Tue Mar 16, 2010 6:39 am

Re: Newbie - Can't find handler near Initialize, char 1 error

Post by gwbasic » Fri Mar 26, 2010 1:52 am

Ok. I unchecked the variable checking and it's working now. But I need to debug it since it's not returning the correct decrypted value.

Post Reply