This is not very easy to read, and the way things are mixed is awkward. We already have an append_data() helper; could we use it here, maybe adding some append_u16() and append_u32() helpers to make things easier?
This would also avoid the need to keep a separate "size" calculation and keep that in sync with the code.