added new tlv function

This commit is contained in:
merlokk 2019-01-03 19:42:40 +02:00
parent a5f8454168
commit 723298d00c
2 changed files with 41 additions and 0 deletions

View file

@ -25,6 +25,7 @@
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <math.h>
#define TLV_TAG_CLASS_MASK 0xc0
#define TLV_TAG_COMPLEX 0x20
@ -534,3 +535,40 @@ struct tlvdb *tlvdb_elm_get_parent(struct tlvdb *tlvdb)
{
return tlvdb->parent;
}
bool tlv_get_uint8(const struct tlv *etlv, uint8_t *value)
{
*value = 0;
if (etlv)
{
if (etlv->len == 0)
return true;
if (etlv->len == 1)
{
*value = etlv->value[0];
return true;
}
}
return false;
}
bool tlv_get_int(const struct tlv *etlv, int *value)
{
*value = 0;
if (etlv)
{
if (etlv->len == 0)
return true;
if (etlv->len <= 4)
{
for (int i = 0; i < etlv->len; i++)
{
*value += etlv->value[i] * pow(0x100, i);
}
return true;
}
}
return false;
}

View file

@ -61,4 +61,7 @@ unsigned char *tlv_encode(const struct tlv *tlv, size_t *len);
bool tlv_is_constructed(const struct tlv *tlv);
bool tlv_equal(const struct tlv *a, const struct tlv *b);
bool tlv_get_uint8(const struct tlv *etlv, uint8_t *value);
bool tlv_get_int(const struct tlv *etlv, int *value);
#endif