Like most ASN.1 DER implementations, we encode backwards. The
existing asn1buf implementation inserts bytes forward into a
resizeable buffer, then reallocates the number of bytes inserted and
reverses them.
Throw out this implementation and replace it with two simple inline
functions in asn1_encode.c, which conditionally insert bytes in
reverse order and unconditionally increment a count. Make two passes
over the input representation, once to count the encoding size and
once to actually encode it into a precisely allocated buffer. Remove
all of the explicit length counting from encoding functions; in the
places where we need intermediate lengths, compute them from the
difference in buffer byte count. Make a few encoding functions return
void as they no longer have error cases.