So guys, continuing from the previous lecture video, here is one more example of the Tlv.
Now here you can see that each Tlv can carry multiple values.
Right?
So while passing this tlv buffer.
Now note that the contiguous chunk of memory which contains all the tlvs packed together is called tlv
buffer.
So these portion of memory is nothing, but it is called tlv buffer.
Right.
And it is contiguous straight flat chunk of memory.
Now, how would you know that?
Whether there are two units of values present in the value part of this Tlv.
It's simple.
You know that the type of the Tlv is one, and by definition whenever the type of the Tlv is one, it
means that the data type of the value is a string as well as the length of one unit of data is 32 bytes.
So here the length is specified as 64 bytes.
When you divide this length value by one unit of data, you will get two, right?
So 64 divided by 32 is equal to two.
It simply means that the the value portion of this Tlv contains two strings, each of 32 bytes.
And the same applies to the second Tlv as well as the third tlv.
So this is how you read the Tlv by dividing the length of the value portion of this tlv by the size
of one unit of data of this tlv.
Now let us try to write a program regarding how would you parse this tlv?
Right.
So for example, let us suppose that I give you a pointer which points to the very start of this tlv
buffer and the total size of this buffer.
Let's say that it is 222 bytes.
So how were you going to read the type field of this Tlv?
You can read the type field of the Tlv by simply type casting the first byte of this tlv buffer into
the unsigned int eight byte of data type and the length of the first tlv.
You can obtained by incrementing the pointer of the tlv buffer by one.
That is, you are reading one byte of the data starting from this pointer.
Right?
So it's a simple pointer arithmetic and the number of units which is packed in the value field of this
Tlv can be obtained by dividing the total length of the Tlv.
That is this value by the size of the unit data of this Tlv.
So you can always write a function which takes an argument the type of the tlv and depending on the
type of the Tlv, you can always return the size of unit data of this tlv.
Right.
So here you can see that I'm simply dividing the total length of this tlv by the size of unit data of
this tlv.
Right.
So in this case I will get the number of units which is packed in this tlv value.
So in this case it will return to.
And following that, I can simply read the first name and then I can start a loop depending on the number
of units and I can read all the names which are packaged in this value part of this.
Tlv Right.
And once we have read all the values which are present in the value part of this Tlv, we need to start
reading the next Tlv from the beginning.
So the TLB buffer pointer which was pointing to the start of the Tlv needs to be adjusted to the to
point to the start of the second tlv.
It is for this reason that we will increment the tlv buffer by size equal to the length of this tlv.
And also you have to consider the tlv overhead also, which is two bytes.
And after that we can start reading on to the next.
Because our tlv buffer now points to the start of the second tlv right.
So always you need to take a caution that you don't overshoot the TLB buffer and track how much buffer
you have scanned so far, starting from the beginning to the end of the Tlv buffer.
Right.
Don't overshoot the tlv buffer, otherwise it will lead to memory, corruption or segmentation fault.
So there is an assignment following this lecture video.
Please do the assignment.
In that assignment we will going to develop an easy way to parse the Tlv without having to worry about
jumping the pointer from here to there within a buffer.