I am using asn1c to de/encode DER encoded data. That’s working fine. However, for logging it would be nice to be able to write the sent/received data in XER format (XML-like). The asn1c generated files contain routines to write XER encoded data to a FILE*, such as stdout. However, I would like to encode into a buffer, so that I can process the data before I print it. Can anyone tell me how to do that?
I would like a similar interface as encode_to_buffer(), which encodes a sructure in DER data.
I just found out how it can be done: The xer_encode() function generated by asn1c takes a callback, which gets a XER snippet and it’s size in bytes as well as a userdata pointer. By writing an appropriate callback, we can put the XER encoded data in a buffer.
Below is how I solved my problem. I implemented a xer_buffer_t which holds a buffer, it’s size and tracks how full the buffer is so that the callback can realloc if necessary.