0

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?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Dulguuntur
  • 43
  • 3
  • 9
  • maybe this can help you: http://codereview.stackexchange.com/questions/56203/type-length-value-tlv-encode-decode. But this is not BER TLV – Giorgi Moniava Nov 24 '16 at 06:13

1 Answers1

1

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 9F360A12345678901234567890.

Adarsh Nanu
  • 2,133
  • 1
  • 13
  • 18