tkblackbelt
tkblackbelt

Reputation: 391

Passing a binary to erlang nif

I'm writing some Erlang code that basically accepts some binary data from a TCP connection and then uses a C nif to decrypt the data and return the decrypted data.

The problem is that I can't seem to figure out how to modify the passed in binary.

This is the function I'm using. If anyone can point me in the right direction, I would appreciate it.

static ERL_NIF_TERM decrypt(ErlNifEnv* env, ErlNifBinary *data);

Thanks!

Upvotes: 3

Views: 1673

Answers (1)

jj1bdx
jj1bdx

Reputation: 1197

You cannot modify a passed binary. You need to make a copy first and modify the copied binary, then return the copied (=modified) binary back to the caller.

Remember binary is allocated in a shared heap; if you overwrite the original you'll be in trouble because that breaks the principle of single assignment of Erlang.

See an example of handling binary arguments in sfmt-erlang NIF code for the details.

Upvotes: 4

Related Questions