Why does the data need to be escaped? Can't you just transfer the raw bytes with some length information?
--
You received this message because you are subscribed to the Google Groups "ocaml-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Right, but if you transmit length and the data you don't have to use read_line.
The only ways I know of for encoding strings are:
1. Explicitly annotate with the length.
2. Have a termination character, like null or newline, and escape the character wherever it appears in your string.
3. Have a termination character, and ensure somehow that that character never appears in your string. (Eg C null terminated strings.)
You could maybe do this by mapping your byte stream to an encoding that uses a smaller alphabet, eg by base64 encoding your byte stream. But this is just as computationally expensive as escaping, and furthermore there is still the problem of having an obvious pattern: rather than being preceeded with a length, your data will be followed by a termination character.
4. Have all strings be a fixed length. That might be an option for you: perhaps you could split your plaintext into chunks of a fixed length, pad the last chunk so it is also that length, then encrypt the chunks. Presumably then your ciphertext is also of a fixed length, which could be known by your decoder.
What do standard encryption tools tend to do in this case? This feels like it should be a solved problem.
You have to encode the length somehow. Currently you're implicitly doing it with new lines. It's semantically equivalent to giving the length at the beginning. Doing it at the beginning just means you don't have to deal with escapes.