Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for C strings #57

Closed
PaulDance opened this issue Apr 25, 2024 · 5 comments
Closed

Add support for C strings #57

PaulDance opened this issue Apr 25, 2024 · 5 comments

Comments

@PaulDance
Copy link

With 1.72 adding the c"..." literal C string syntax and const support for much of CStr's methods, it would be nice if this crate had support for them. As far as I can tell, there is currently no way to have such a literal obfuscated without allocating on the heap. Having a specialized macro, for example obfcstr, should help in this regard. In terms of API, it should probably have everything obfstr already has, and only differ in the type of expressions allowed.

As an implementation detail, it should probably work well to mimic what obfstr already does: passing raw bytes to obfbytes with str::to_bytes and then reconstructing a &str from its returned value. Here, CStr::to_bytes_with_nul and CStr::from_bytes_with_nul_unchecked should fit in just fine.

@CasualX
Copy link
Owner

CasualX commented May 9, 2024

Hi, I've tried to implement it but I'm getting a weird errors:

constants cannot refer to statics
cannot call non-const fn xref::<[u8; 11], 3123884736, 703691987675168347> in constants
cannot call non-const fn obfstr::bytes::deobfuscate::<11> in constants
temporary value dropped while borrowed

I'm confused as this should just work the same as the str variant... You can check it out in the cstr branch.

@PaulDance
Copy link
Author

Hi, thanks for the time spent on this! I've taken a quick look at the implementation and it looks just fine as-is.

Indeed, the example I see you using in the macro's description is:

const HELLO_WORLD: &'static CStr = obfcstr!(c"Hello CStr");
assert_eq!(HELLO_WORLD.to_str().unwrap(), "Hello CStr");

(mind the missing obf I added in obfcstr!, it was cstr! previously)

and it does not compile, yes. However, note that the following:

const X: &'static str = obfstr!("abc");
assert_eq!(X, "abc");

already does not compile either on a very similar error: the macro is called as part of the constant definition, which is probably not what is wanted here. Instead, the following is usually done in order to achieve this case:

const X: &'static str = "abc";
assert_eq!(obfstr!(X), "abc");

which should also apply with obfcstr: impossible to use in const contexts. The following also works with the current implementation:

const Y: &'static CStr = c"xyz";
assert_eq!(obfcstr!(Y).to_str().unwrap(), "xyz");

In tests, assert_eq!(obfstr::obfcstr!(c"xyz"), c"xyz"); should probably be enough, though.

@CasualX
Copy link
Owner

CasualX commented May 13, 2024

Oooh, /facepalm. I didn't look close enough at it. PR #58

@CasualX
Copy link
Owner

CasualX commented Oct 5, 2024

Merged and published

@CasualX CasualX closed this as completed Oct 5, 2024
@PaulDance
Copy link
Author

Cool, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants