Make sure we don't write on invalid memory

Don't write on the stream in case it is not large enough.
Adapt serialization functions for major type 0, 1 and 7.
Add tests