]>
git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/tlv.c
4 int decode_ber_tlv_item(uint8_t* data
, tlvtag
* returnedtag
)
6 uint8_t tag
[TAG_LENGTH
] = {0x00,0x00};
8 //uint8_t value[VALUE_LENGTH];
14 if((tag
[0] & TLV_TAG_NUMBER_MASK
) == TLV_TAG_NUMBER_MASK
) { //see subsequent bytes
17 //assume tag is only two bytes long for now
19 while((data[i] & TLV_TAG_MASK) == TLV_TAG_MASK){
27 if((data
[i
] & TLV_LENGTH_MASK
) == TLV_LENGTH_MASK
) {
28 lenlen
= data
[i
] ^ TLV_LENGTH_MASK
;
30 length
= (uint16_t)data
[i
];
36 length
+= (uint16_t)data
[i
];
41 length
= (uint16_t)data
[i
];
44 //copy results into the structure and return
45 memcpy(returnedtag
->tag
, tag
, TAG_LENGTH
);
46 (*returnedtag
).valuelength
= length
; //return length of tag value
47 (*returnedtag
).fieldlength
= length
+ i
+ 1; //return length of total field
48 memcpy(returnedtag
->value
, &(data
[i
]), length
);
52 //generate a TLV tag off input data
53 int encode_ber_tlv_item(uint8_t* tag
, uint8_t taglen
, uint8_t* data
, uint32_t datalen
, uint8_t* outputtag
, uint32_t* outputtaglen
)
55 if(!tag
|| !data
|| !outputtag
|| !outputtaglen
) //null pointer check
58 uint8_t datafieldlen
= (datalen
/ 128) + 1; //field length of the tag
59 uint8_t tlvtotallen
= taglen
+ datafieldlen
+ datalen
; //total length of the tag
60 uint8_t returnedtag
[tlvtotallen
]; //buffer for the returned tag
62 memcpy(returnedtag
, tag
, taglen
); //copy tag into buffer
64 if(datalen
< 128){ // 1 byte length value
65 returnedtag
[counter
++] = datalen
;
68 returnedtag
[counter
++] = datafieldlen
| 0x80; //high bit set and number of length bytes
69 for(uint8_t i
=datafieldlen
; i
!=0; i
--){
70 returnedtag
[counter
++] = (datalen
>> (i
* 8)) & 0xFF; //get current byte
73 memcpy(&returnedtag
[counter
], data
, datalen
);
74 *outputtaglen
= tlvtotallen
;
75 memcpy(outputtag
, returnedtag
,tlvtotallen
);