mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-21 05:43:48 -07:00
Merge pull request #1857 from jmichelp/master
Initial rework on TLV to avoid mem leaks
This commit is contained in:
commit
e5a39e47b2
2 changed files with 78 additions and 14 deletions
|
@ -44,19 +44,6 @@
|
||||||
// const typeof( ((type *)0)->member ) *__mptr = (ptr);
|
// const typeof( ((type *)0)->member ) *__mptr = (ptr);
|
||||||
// (type *)( (char *)__mptr - offsetof(type,member) );})
|
// (type *)( (char *)__mptr - offsetof(type,member) );})
|
||||||
|
|
||||||
struct tlvdb {
|
|
||||||
struct tlv tag;
|
|
||||||
struct tlvdb *next;
|
|
||||||
struct tlvdb *parent;
|
|
||||||
struct tlvdb *children;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct tlvdb_root {
|
|
||||||
struct tlvdb db;
|
|
||||||
size_t len;
|
|
||||||
unsigned char buf[0];
|
|
||||||
};
|
|
||||||
|
|
||||||
static tlv_tag_t tlv_parse_tag(const unsigned char **buf, size_t *len) {
|
static tlv_tag_t tlv_parse_tag(const unsigned char **buf, size_t *len) {
|
||||||
tlv_tag_t tag;
|
tlv_tag_t tag;
|
||||||
|
|
||||||
|
@ -212,6 +199,7 @@ struct tlvdb *tlvdb_parse(const unsigned char *buf, size_t len) {
|
||||||
err:
|
err:
|
||||||
tlvdb_free(&root->db);
|
tlvdb_free(&root->db);
|
||||||
|
|
||||||
|
free(root);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,9 +236,53 @@ struct tlvdb *tlvdb_parse_multi(const unsigned char *buf, size_t len) {
|
||||||
err:
|
err:
|
||||||
tlvdb_free(&root->db);
|
tlvdb_free(&root->db);
|
||||||
|
|
||||||
|
free(root);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool tlvdb_parse_root(struct tlvdb_root *root) {
|
||||||
|
if (root == NULL || root->len == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t *tmp;
|
||||||
|
size_t left;
|
||||||
|
|
||||||
|
tmp = root->buf;
|
||||||
|
left = root->len;
|
||||||
|
if (tlvdb_parse_one(&root->db, NULL, &tmp, &left) == true) {
|
||||||
|
if (left == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool tlvdb_parse_root_multi(struct tlvdb_root *root) {
|
||||||
|
if (root == NULL || root->len == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t *tmp;
|
||||||
|
size_t left;
|
||||||
|
|
||||||
|
tmp = root->buf;
|
||||||
|
left = root->len;
|
||||||
|
if (tlvdb_parse_one(&root->db, NULL, &tmp, &left) == true) {
|
||||||
|
while (left > 0) {
|
||||||
|
struct tlvdb *db = calloc(1, sizeof(*db));
|
||||||
|
if (tlvdb_parse_one(db, NULL, &tmp, &left) == true) {
|
||||||
|
tlvdb_add(&root->db, db);
|
||||||
|
} else {
|
||||||
|
free(db);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
struct tlvdb *tlvdb_fixed(tlv_tag_t tag, size_t len, const unsigned char *value) {
|
struct tlvdb *tlvdb_fixed(tlv_tag_t tag, size_t len, const unsigned char *value) {
|
||||||
struct tlvdb_root *root = calloc(1, sizeof(*root) + len);
|
struct tlvdb_root *root = calloc(1, sizeof(*root) + len);
|
||||||
|
|
||||||
|
@ -291,6 +323,21 @@ void tlvdb_free(struct tlvdb *tlvdb) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tlvdb_root_free(struct tlvdb_root *root) {
|
||||||
|
if (root == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (root->db.children) {
|
||||||
|
tlvdb_free(root->db.children);
|
||||||
|
root->db.children = NULL;
|
||||||
|
}
|
||||||
|
if (root->db.next) {
|
||||||
|
tlvdb_free(root->db.next);
|
||||||
|
root->db.next = NULL;
|
||||||
|
}
|
||||||
|
free(root);
|
||||||
|
}
|
||||||
|
|
||||||
struct tlvdb *tlvdb_find_next(struct tlvdb *tlvdb, tlv_tag_t tag) {
|
struct tlvdb *tlvdb_find_next(struct tlvdb *tlvdb, tlv_tag_t tag) {
|
||||||
if (!tlvdb)
|
if (!tlvdb)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
@ -31,14 +31,31 @@ struct tlv {
|
||||||
const unsigned char *value;
|
const unsigned char *value;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct tlvdb;
|
struct tlvdb {
|
||||||
|
struct tlv tag;
|
||||||
|
struct tlvdb *next;
|
||||||
|
struct tlvdb *parent;
|
||||||
|
struct tlvdb *children;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct tlvdb_root {
|
||||||
|
struct tlvdb db;
|
||||||
|
size_t len;
|
||||||
|
unsigned char buf[0];
|
||||||
|
};
|
||||||
|
|
||||||
typedef void (*tlv_cb)(void *data, const struct tlv *tlv, int level, bool is_leaf);
|
typedef void (*tlv_cb)(void *data, const struct tlv *tlv, int level, bool is_leaf);
|
||||||
|
|
||||||
struct tlvdb *tlvdb_fixed(tlv_tag_t tag, size_t len, const unsigned char *value);
|
struct tlvdb *tlvdb_fixed(tlv_tag_t tag, size_t len, const unsigned char *value);
|
||||||
struct tlvdb *tlvdb_external(tlv_tag_t tag, size_t len, const unsigned char *value);
|
struct tlvdb *tlvdb_external(tlv_tag_t tag, size_t len, const unsigned char *value);
|
||||||
struct tlvdb *tlvdb_parse(const unsigned char *buf, size_t len);
|
struct tlvdb *tlvdb_parse(const unsigned char *buf, size_t len);
|
||||||
struct tlvdb *tlvdb_parse_multi(const unsigned char *buf, size_t len);
|
struct tlvdb *tlvdb_parse_multi(const unsigned char *buf, size_t len);
|
||||||
|
|
||||||
|
bool tlvdb_parse_root(struct tlvdb_root *root);
|
||||||
|
bool tlvdb_parse_root_multi(struct tlvdb_root *root);
|
||||||
|
|
||||||
void tlvdb_free(struct tlvdb *tlvdb);
|
void tlvdb_free(struct tlvdb *tlvdb);
|
||||||
|
void tlvdb_root_free(struct tlvdb_root *root);
|
||||||
|
|
||||||
struct tlvdb *tlvdb_elm_get_next(struct tlvdb *tlvdb);
|
struct tlvdb *tlvdb_elm_get_next(struct tlvdb *tlvdb);
|
||||||
struct tlvdb *tlvdb_elm_get_children(struct tlvdb *tlvdb);
|
struct tlvdb *tlvdb_elm_get_children(struct tlvdb *tlvdb);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue