libcyaml icon indicating copy to clipboard operation
libcyaml copied to clipboard

Support for binary data defined with the '!!binary' YAML tag

Open LonerDan opened this issue 3 years ago • 2 comments

Hello, would it be possible to implement a support for loading data defined using the YAML tag 'binary' defined here? Does the underlying libyaml library handle those tags in any way that can be leveraged in cyaml? It would be nice to simply define a uint8_t pointer and size_t value in an user structure and have it automatically populated by this library without having to base64 decode it manually.

LonerDan avatar Nov 16 '21 14:11 LonerDan

That's a good idea, I could make a CYAML_BINARY type that does that.

At the moment, it would load as a string, and you would need to base64 decode manually.

I'll have a bit more of a think about it and get back to you. I want to avoid breaking the ABI if I can.

tlsa avatar Nov 16 '21 14:11 tlsa

Once we have CYAML_BINARY, the YAML !!binary tag becomes redundant.

It would be simple to implement CYAML_BINARY in the same way as CYAML_SEQUENCE is implemented at the moment. However, CYAML_SEQUENCE has the limitation that you can't have a CYAML_SEQUENCE directly inside a CYAML_SEQUENCE. The same restriction would apply here, so a CYAML_BINARY would not be allowed directly inside a CYAML_SEQUENCE.

That means the following would not be possible:

- !!binary "DEADBEEF"
- !!binary "CAFEF00D"

It would to be a sequence of mappings containing the binary data, for example:

- data: !!binary "DEADBEEF"
- data: !!binary "CAFEF00D"

This is because the mapping allows a structure to be used, which allows the data length to be stored in a structure.

So for an array of binaries, the client data structure would need to be something like:

struct binary {
	uint8_t data;
	size_t len;
};

struct my_prog_data {
	struct binary *binary;
	size_t binary_count;
};

tlsa avatar Nov 16 '21 16:11 tlsa