ruby / fiddle

A libffi wrapper for Ruby.
BSD 2-Clause "Simplified" License
154 stars 37 forks source link

Add a helper method for reading/writing memory #123

Closed tenderlove closed 1 year ago

tenderlove commented 1 year ago

This commit adds two new methods, Fiddle::Pointer.read and Fiddle::Pointer.write. Both methods take an address, and will read or write bytes at that address respectively.

For example we can read from an address without making a Pointer object:

Fiddle::Pointer.read(address, 5) # read 5 bytes

We can also write to an address without allocating a Pointer object:

Fiddle::Pointer.write(address, "bytes") # write 5 bytes

This allows us to read / write memory at arbitrary addresses without instantiating a new Fiddle::Pointer object.

Examples where this API would be useful 1 2 3

I also added a writer method for the same reasons as the reader.

tenderlove commented 1 year ago

Hmm. Fiddle::Pointer[value] and Fiddle::Pointer[address, length] may be confused. How about Fiddle::Pointer.read/Fiddle::Pointer.write (like IO.read/IO.write) instead?

That sounds good to me. I wish we could use [] and []=, but I agree it's confusing.