]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/tlv.c
3 int decode_ber_tlv_item(uint8_t* data
, tlvtag
* returnedtag
)
5 uint8_t tag
[TAG_LENGTH
] = {0x00,0x00};
7 //uint8_t value[VALUE_LENGTH];
13 if((tag
[0] & TLV_TAG_NUMBER_MASK
) == TLV_TAG_NUMBER_MASK
) { //see subsequent bytes
16 //assume tag is only two bytes long for now
18 while((data[i] & TLV_TAG_MASK) == TLV_TAG_MASK){
26 if((data
[i
] & TLV_LENGTH_MASK
) == TLV_LENGTH_MASK
) {
27 lenlen
= data
[i
] ^ TLV_LENGTH_MASK
;
29 length
= (uint16_t)data
[i
];
35 length
+= (uint16_t)data
[i
];
40 length
= (uint16_t)data
[i
];
43 //copy results into the structure and return
44 memcpy(returnedtag
->tag
, tag
, TAG_LENGTH
);
45 (*returnedtag
).valuelength
= length
; //return length of tag value
46 (*returnedtag
).fieldlength
= length
+ i
+ 1; //return length of total field
47 memcpy(returnedtag
->value
, &(data
[i
]), length
);
51 //generate a TLV tag off input data
52 int encode_ber_tlv_item(uint8_t* tag
, uint8_t taglen
, uint8_t* data
, uint32_t datalen
, uint8_t* outputtag
, uint32_t* outputtaglen
)
54 if(!tag
|| !data
|| !outputtag
|| !outputtaglen
) //null pointer check
57 uint8_t datafieldlen
= (datalen
/ 128) + 1; //field length of the tag
58 uint8_t tlvtotallen
= taglen
+ datafieldlen
+ datalen
; //total length of the tag
59 uint8_t returnedtag
[tlvtotallen
]; //buffer for the returned tag
61 memcpy(returnedtag
, tag
, taglen
); //copy tag into buffer
63 if(datalen
< 128){ // 1 byte length value
64 returnedtag
[counter
++] = datalen
;
67 returnedtag
[counter
++] = datafieldlen
| 0x80; //high bit set and number of length bytes
68 for(uint8_t i
=datafieldlen
; i
!=0; i
--){
69 returnedtag
[counter
++] = (datalen
>> (i
* 8)) & 0xFF; //get current byte
72 memcpy(&returnedtag
[counter
], data
, datalen
);
73 *outputtaglen
= tlvtotallen
;
74 memcpy(outputtag
, returnedtag
,tlvtotallen
);