Skip to content

Modelling a TLV (type length value) #290

Answered by csnover
ckrenslehner asked this question in Q&A
Discussion options

You must be logged in to vote

Here is a snippet for further illustration:

Use pre_assert, not assert, for selecting an enum variant. For unknown types, specify a fallback variant as the last variant with no pre_assert with #[br(count = length)] Vec<u8> to collect data for an unknown type.

When reading a TLV it would be nice to validate, that the size encoded size of value indeed exactly matches the value of length. How would you tackle this aspect?

The simplest way is to write a function that returns the expected size of a given type and e.g. assert(length == TLV::size_of_type(r#type)). This particular thing of determining struct size has been discussed in some other issues/discussions, so you may do a search around.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ckrenslehner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants