New topic: 

Changing strings post-compile

<http://forums.realsoftware.com/viewtopic.php?t=26251>

       Page 1 of 1
   [ 6 posts ]                 Previous topic | Next topic         Author  
Message       75mph           Post subject: Changing strings 
post-compilePosted: Thu Jan 29, 2009 6:27 pm                        
Joined: Thu Jan 29, 2009 6:16 pm
Posts: 3              In PHP I can:

Code:
$f = file_get_contents("My Application.exe");
$s = str_replace("WhateverStringThereIs", "WhateverStringThereWillBe", $f);
file_put_contents("Modded Application.exe", $s);



How would I do this in RB?

Whenever I try something like:

Code:
dim f as FolderItem
dim b as BinaryStream
dim s as String

f = GetFolderItem("My Application.exe")
if f <> Nil then
  if f.Exists then
  b = f.OpenAsBinaryFile
  s = b.Read(b.Length)
  else
  Quit
  end if
end if

if InStrB(s, "WhateverStringThereIs") > 0 then
  s = ReplaceAllB(s, "WhateverStringThereIs", "WhateverStringThereWillBe")
  b.Position = 0
  b.Write(s)
  b.Close
else
  print "string not found"
end if



It never makes it past the InstrB. Do I need to do some encoding or something? 
How would I do that?   
                            Top               npalardy           Post subject: 
Re: Changing strings post-compilePosted: Thu Jan 29, 2009 6:39 pm               
         
Joined: Sat Dec 24, 2005 8:18 pm
Posts: 4347
Location: Canada, Alberta, Near Red Deer              it crashes or generates 
and error or what happens exactly ?

I'll admit that I wonder why you want / need to do this at all     
_________________
My web site Great White Software
RBLibrary.com REALbasic learning  
                            Top               75mph           Post subject: Re: 
Changing strings post-compilePosted: Thu Jan 29, 2009 6:58 pm                   
     
Joined: Thu Jan 29, 2009 6:16 pm
Posts: 3              It doesn't crash.. It just never finds the strings I ask 
it to.

I've even converted the string into a byte array and it DID find those bytes, 
but when I went to replace one byte with another (by b.WriteByte, it never got 
written to the file.

My reasons aren't malicious if that's what you mean?

Thanks   
                            Top               npalardy           Post subject: 
Re: Changing strings post-compilePosted: Thu Jan 29, 2009 8:09 pm               
         
Joined: Sat Dec 24, 2005 8:18 pm
Posts: 4347
Location: Canada, Alberta, Near Red Deer              75mph wrote:but when I 
went to replace one byte with another (by b.WriteByte, it never got written to 
the file.

did you open the file in read write mode or read only ?

75mph wrote:My reasons aren't malicious if that's what you mean?

Just seems an unusual need that  might be done easier in a different way     
_________________
My web site Great White Software
RBLibrary.com REALbasic learning  
                            Top               harriew           Post subject: 
Re: Changing strings post-compilePosted: Thu Jan 29, 2009 8:33 pm               
         
Joined: Fri Dec 01, 2006 3:09 pm
Posts: 172
Location: Tennessee, USA              You need to look at the OpenAsBinaryFile 
function of the FolderItem class a little more closely. As you have used it the 
file is open as a read only file. There is an optional boolean parameter to the 
OpenAsBinaryFile function which, when set to true, opens the file in read/write 
mode. You never tested the error code after attempting to write to the file.

However, even if you do get it open and successfully write the file with a 
modified string, I would be very surprised if the exe file would still execute 
properly if you change the length of the string. It is a risky process so I 
would sure want to have a backup of the file.   
                            Top                75mph           Post subject: 
Re: Changing strings post-compilePosted: Thu Jan 29, 2009 8:47 pm               
         
Joined: Thu Jan 29, 2009 6:16 pm
Posts: 3              Thanks. That worked.

InStrB still doesn't seem to find my strings. The byte arrays work for 
searching/replacing but it's sloooow  I think I might stick with PHP.

Thanks

Code:
dim x, y, z, idx, pos, cnt as Integer
dim file, filename as String
dim f as FolderItem
dim b as BinaryStream
dim rstr(1) as String
dim bstr1(-1) as Byte
dim bstr2(-1) as Byte
dim byt as Byte

f = GetFolderItem("My Application.exe")
if f <> Nil then
  if f.Exists then
  System.DebugLog "File DOES exist..."
  filename = f.AbsolutePath
  System.DebugLog "Opening file"
  b = f.OpenAsBinaryFile(true)
  else
  System.DebugLog f.AbsolutePath+" does not exist"
  exit
  end if
end if

rstr(0) = edt(1).Text // Editfields..
rstr(1) = edt(2).Text

for y = 1 to len(rstr(0))
  bstr1.Append asc( mid(rstr(0), y, 1) )
  bstr2.Append asc( mid(rstr(1), y, 1) )
next

idx = 0
While NOT b.EOF
  byt = b.ReadByte
  if byt = bstr1(idx) then
  idx = idx + 1
  else
  idx = 0
  end if
  
  if idx = UBound(bstr1) then
  pos = b.Position-idx
  System.DebugLog "Found a replacement at "+str(pos)
  idx = 0
  
  b.Position = pos
  for z = 0 to UBound(bstr1)
  System.DebugLog "Writing "+chr(bstr2(z))+" "+str(b.Position)
  b.WriteByte(bstr2(z))
  next
  
  end if
wend
b.Close

System.DebugLog "Done."

   
                            Top           Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 6 posts ]     

-- 
Over 900 classes with 18000 functions in one REALbasic plug-in. 
The Monkeybread Software Realbasic Plugin v8.1. 

&lt;http://www.monkeybreadsoftware.de/realbasic/plugins.shtml&gt;

[email protected]

Reply via email to