[Win32utils-devel] Global substitution with win32-mmap

Heesob Park phasis at gmail.com
Tue Apr 22 01:51:44 EDT 2008


Hi,

2008/4/22, Daniel Berger <djberg96 at gmail.com>:
>
> Hi all,
>
> I'm trying to figure out how to do an inline file substitute/replace
> using the address. In short, I'm trying to replace "assert_raises" with
> "assert_raise" throughout a file.
>
> I tried this:
>
> require 'win32/mmap'
> require 'windows/msvcrt/string'
> require 'windows/msvcrt/buffer'
> include Win32
> include Windows::MSVCRT::String
> include Windows::MSVCRT::Buffer
>
> StrlenL = API.new('strlen', 'L', 'L', 'msvcrt')
>
> MMap.new(:file => 'test.rb') do |addr|
>    old_str = 'assert_raises'
>    old_len = old_str.length
>    new_str = 'assert_raise'
>    new_len = new_str.length
>
>    ptr1 = ptr2 = ptr3 = [strstr(addr,old_str)].pack('p*').unpack('l')[0]
>
>    while ptr1 && ptr1 != 0
>       ptr2 += new_len
>       ptr3 += old_len
>       memmove(ptr2, ptr3, 1 + StrlenL.call(ptr3))
>       memcpy(ptr1, new_str, new_len)
>       ptr1 = ptr2 = ptr3 = [strstr(ptr2,
> old_str)].pack('p*').unpack('l')[0]
>    end
> end
>
> I based it off of this thread:
>
> http://tinyurl.com/5qvbnw
>
> But, that doesn't seem to be working. Any ideas?



You must define strstr like this:
         Strstr   = API.new('strstr', 'LP', 'L', 'msvcrt')

and try this:

StrlenL = API.new('strlen', 'L', 'L', 'msvcrt')

MMap.new(:file => 'test.rb') do |addr|
   old_str = 'assert_raises'
   old_len = old_str.length
   new_str = 'assert_raise'
   new_len = new_str.length

   ptr1 = ptr2 = ptr3 = strstr(addr,old_str)
   while ptr1 && ptr1 != 0
      ptr2 += new_len
      ptr3 += old_len
      memmove(ptr2, ptr3, 1 + StrlenL.call(ptr3))
      memcpy(ptr1, new_str, new_len)
      ptr1 = ptr2 = ptr3 = strstr(ptr2,old_str)
   end
end
Regads,

Park Heesob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/win32utils-devel/attachments/20080422/f9b876bb/attachment.html 


More information about the win32utils-devel mailing list