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 a way to pass binary data #31

Closed
wiktor-k opened this issue Apr 29, 2021 · 8 comments · Fixed by #32
Closed

Add a way to pass binary data #31

wiktor-k opened this issue Apr 29, 2021 · 8 comments · Fixed by #32
Assignees
Labels
enhancement New feature or request

Comments

@wiktor-k
Copy link

Hi!

I'd like to pass binary data between PHP and the Rust crate. As far as I understand both on PHP side this is just a string while on rust side they'd be slices of u8 (or vectors?).

Is something like that possible or planned?

Thanks again for this great library!

@davidcole1340 davidcole1340 self-assigned this Apr 29, 2021
@davidcole1340 davidcole1340 added the enhancement New feature or request label Apr 29, 2021
@davidcole1340
Copy link
Owner

Will take a look into it this weekend, seems simple enough.

@davidcole1340
Copy link
Owner

@wiktor-k Would you be able to provide an example? I'd probably like to try and provide a generic impl over a couple types so I'm thinking something similar to pack.

@wiktor-k
Copy link
Author

Sure! I'm working with @willbrowningme on one library that uses OpenPGP keys. Currently we need to armor all keys (basically base64 encode) and that makes keys stored in db 33% bigger than necessary.

You can check out this function: https://gitlab.com/willbrowning/anonaddy-sequoia/-/blob/master/src/lib.rs#L186

It should accept and return just bytes and the calling PHP code would save and that from the database.

@davidcole1340
Copy link
Owner

I see, would something like this work?

pub extern fn test(ex: &mut ExecutionData, ret: &mut Zval) {
    let x: Vec<u8> = vec![1, 2, 3, 4, 5];
    ret.set_string(String::from_utf8_lossy(x));
}

and then in PHP

$x = unpack('C*, test());

Not sure if this would work in your situation but seemed to work for me when testing. I will keep looking into a generic impl for other integer types as well, because I don't think the same would work for types like i16, i32 etc.

@wiktor-k
Copy link
Author

from_utf8_lossy will replace invalid chars with replacement character so it may break the underlying data if it's not UTF8 https://doc.rust-lang.org/std/string/struct.String.html#method.from_utf8_lossy

I need to check the actual example that you have. Thanks for help!

@davidcole1340 davidcole1340 linked a pull request May 2, 2021 that will close this issue
@davidcole1340
Copy link
Owner

Could you have a look at #32 and tell me what you think? Seems to work for most types that pack works with. Here's a quick example:

/// Unpacks a binary u8 array
pub extern fn skel_unpack(ex: &mut ExecutionData, ret: &mut Zval) {
    let mut packed = Arg::new("packed", DataType::String);
    parse_args!(ex, packed);
    let value = unsafe { packed.zval().unwrap().binary::<u8>(); };
    dbg!(value);
}

/// Packs a binary u8 array
pub extern fn skel_pack(ex: &mut ExecutionData, ret: &mut Zval) {
    ret.set_binary(vec![12u8, 13, 14, 15, 20, 25]);
}
$x = [15, 30, 45];
skel_unpack(pack('C*', ...$x));
// [15, 30, 45] from Rust

$x = unpack('C*', skel_pack());
var_dump($x); // array(6) [12, 13, 14, 15, 20, 25]

@wiktor-k
Copy link
Author

wiktor-k commented May 5, 2021

David, sorry for the super late reply but I did write a small proof-of-concept code using set_binary and it works very much fine: https://gitlab.com/willbrowning/anonaddy-sequoia/-/merge_requests/7/diffs I did file_put_contents the binary and it saved all bytes just fine. So 👍 from me!

Thanks for your time and hard work on this lib! I really appreciate it.

@davidcole1340
Copy link
Owner

No worries, thanks for confirming it works. I'll merge it into master now.

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

Successfully merging a pull request may close this issue.

2 participants