My question title isn’t clear, sorry. I tried 😉
- I have a binary string that contains nul bytes.
- I know the length of the string.
- I have a function (from a library) encodes strings into another format.
- I need to encode the binary string using the library function.
- The library function doesn’t accept a length & assumes the string is nul terminated.
- I know how to encode the nul bytes by hand (represent them as
"\\000")
Take the pseudo code:
/* I have a string, and the length in bytes of that string */
char * data = value->bytes;
long length = value->length;
/* I need to use a function, but it doesn't handle embedded nuls */
char * encoded = lib_func_encode(data);
lib_free(encoded); // library function requires a free() routine be called
That doesn’t work, because the library function only encodes the first chunk of data up to the first nul byte. I could write a fairly horrible loop that keeps calling strlen() on data and counting until length bytes have been encoded, appending a "\\0000" string every time I need to encode some more bytes, realloc’ing some buffer to accomodate each extra chunk of encoded data. It feels like this is going to be a lot of code to get around a fairly simple problem. Coming from a language with concepts like split() (on the nul bytes) map() (to encode each chunk) and join() (using "\\000") I’m instinctively thinking about the cleanest solution that way… but nothing really exists in standard C to do things like that.
In a fictional functional language it might look like:
join("\\000",
map(lambda(chunk){encode(chunk)}, split('\0', data)))
How would a seasoned C programmer write a routine like I need to write?
You can do it in multiple steps: