Check length of buf before encoding

Convert the remaining functions to check the length of the destination
buffer when encoding the term format.