Git Product home page Git Product logo

Comments (4)

lsaa avatar lsaa commented on May 24, 2024 1

Yeah that worked

pub fn try_read_std_string_utf8(handle: process_memory::ProcessHandle, starting_offsets: Vec<usize>) -> Result<String, std::io::Error> {
    use process_memory::*;
    let mut offset = DataMember::<u8>::new_offset(handle, starting_offsets);
    let len_off = DataMember::<i32>::new_offset(handle, vec![offset.get_offset()? - 8]);
    let len = len_off.read()? as usize;
    let mut bytes = vec![0_u8; len];
    handle.copy_address(offset.get_offset()?, &mut bytes)?;
    String::from_utf8(bytes).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
}

This is what it ended up like, couldn't find handle.get_offset() so I just did the offset the only way I know.
You can close the issue now, you made it clear why it is the way it is and gave a serviceable alternative, thanks man.

from rs-process-memory.

Tommoa avatar Tommoa commented on May 24, 2024

The reason why this crate doesn't define methods for copying strings (although it would be very useful) is because strings can vary even on the same platform. e.g. Rust uses (len, ptr) for strings, C/C++ uses a null byte, and this isn't even getting into small string optimizations that C++ uses. As such, its somewhat hard for me to recommend a way to read a string without necessarily knowing more about the layout of the string.

My first question would be, what kind of string is it? Is it an ASCII string, or a UTF-8 string, or a UTF-16 string, or a UTF-32 string? Depending on which one it is, you may need to change the size of your buffer.

Second question would be, if you overrun the string, are you likely to run out of allocated memory? If you're on Windows, calls to ReadProcessMemory (which this crate does under the hood) will fail if the entire region isn't readable, which could mean that you cut off your string early or return with an error that you otherwise shouldn't.

With these two things in mind, my suggested implementation for a UTF-8 string would be as follows:

pub fn try_read_string(handle: process_memory::ProcessHandle, starting_offsets: Vec<usize>) -> Result<String, std::io::Error> {
        let mut offset = handle.get_offset(&starting_offsets);
        let mut parts = Vec::<u8>::new(); 
        let mut byte = [0u8; 1];
        loop {
            handle.copy_address(offset, &mut byte)?;
            if byte[0] == 0 {
                break;
            }
            offset += 1;
            parts.push(byte[0]);
        }
        String::from_utf8(parts).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
}

This of course has the problem of being relatively slow as you have to get a single byte at a time. If you are able to get the length of the entire string, then I'd suggest allocating parts to be the same size as the length and then just calling handle.copy_address() once.

from rs-process-memory.

lsaa avatar lsaa commented on May 24, 2024

Thanks for the quick response, the string I'm looking at are std::String (C++) so I can find the length and that makes it a lot easier. I'll give the allocation and copying a shot.

from rs-process-memory.

Tommoa avatar Tommoa commented on May 24, 2024

get_offset() is a part of the CopyAddress trait.

Good to know you were able to do it!

from rs-process-memory.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.