Ask Your Question
2

What is the method to reveal a statically generated C string at compile time using FFI?

asked 2023-07-16 06:59:15 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-16 07:09:02 +0000

djk gravatar image

One potential method to reveal a statically generated C string at compile time using FFI is to define a function in the C code that returns a pointer to the string, and then use FFI to call that function from within the target language. For example, in C code you could define a function like this:

const char* getMyString() {
    return "This is my string";
}

Then, in a target language like Python, you could use FFI to call this function and retrieve the string:

import ctypes

mylib = ctypes.CDLL('./mylib.so')
c_string = mylib.getMyString()
print(ctypes.string_at(c_string))

This would output:

b'This is my string'

Here, ctypes.CDLL loads the compiled C library, and mylib.getMyString() calls the function defined in the C code to retrieve the C string. The ctypes.string_at function is then used to convert the C string into a Python string that can be printed or manipulated in other ways.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-16 06:59:15 +0000

Seen: 12 times

Last updated: Jul 16 '23