formats
formats copied to clipboard
tls_codec: Add function to ensure all bytes have been consumed
While usually more than one object is serialized in a byte buffer, there might be situations where you just expect one object from a byte buffer. In other words, there is currently no counterpart to tls_serialized_detached()
.
The motivation behind this is that the TLS encoding implicitly guarantees that at least the required number of bytes indicated in the length prefix have been read, but you don't know if any trailing bytes are left after deserializing an object unless you try to deserialize another object or do a manual check.
I would propose something like tls_deserialize_detached()
(or whatever other name is more suitable) that takes ownership of the Read
and after internally calling tls_deserialize
it checks if it can read one more byte and if so, returns an error.
I'm not sure how useful this is. Read
operates on byte slices in most cases (or on another slice). So consuming isn't really an option. So the question is whether it is useful to have a function that checks the length for you. I'd say maybe, but it's something that can be easily done on the outside.