简体   繁体   中英

How to create TLV encode/decode msg data using C?

I'm trying to encode/decode tlv msg data using C. Actually im newbie about TLV formatting. I just found few codes from the Google and i do not understand them.

unsigned char *tlv_buffer = NULL;
int size = 1;
int len = 0;
int result;

tlv_buffer = BKS_MALLOC(size);
result = append_bertlv_data(&tlv_buffer, &size, &len, 0xDF04, 2, "\x34\x56");
result = append_bertlv_data(&tlv_buffer, &size, &len, 0xDF81, 3, "ref");

append_bertlv_data:

int append_bertlv_data ( unsigned char ** buf_data, unsigned int buf_size,unsigned int * buf_len, unsigned int tag, unsigned int len, constunsigned char * value )

buf_data

As input, a pointer to the existing buffer to which the BER-TLV data element shall be appended. As output, the pointer to the possibly re-allocated buffer.

buf_size

The size of the allocated memory.

buf_len

The length of data written in the buffer.

I just need to understand buf_data (buffer) and its obliagtion. Anyone help me pls?

If you could post the definition of the function append_bertlv_data, could explain more.

The logic is to pack the data in the format [TAG][Length][Value]. To say an example( context is emv ).You have A tag 9F36 - application transaction counter length - 2 bytes Value - 0001. This will be represented as 9F36020001 The whole data is represented in 5 bytes. (hex/packed bcd ). One thing to note is that length is also in hex. Suppose it was a 10 bytes data we would have given it as 9F36 0A 12345678901234567890.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM