obfstr icon indicating copy to clipboard operation
obfstr copied to clipboard

Add support for C strings

Open PaulDance opened this issue 1 year ago • 3 comments

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.

PaulDance avatar Apr 25 '24 11:04 PaulDance

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.

CasualX avatar May 09 '24 14:05 CasualX

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.

PaulDance avatar May 13 '24 20:05 PaulDance

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

CasualX avatar May 13 '24 22:05 CasualX

Merged and published

CasualX avatar Oct 05 '24 15:10 CasualX

Cool, thanks!

PaulDance avatar Oct 06 '24 13:10 PaulDance