From 86b44ab789b935c9d7e12bb7b39875cd92a32f57 Mon Sep 17 00:00:00 2001 From: merlokk Date: Mon, 27 Aug 2018 19:20:07 +0300 Subject: [PATCH] added tlvdb_change_or_add_node, change of element dont work... --- client/emv/cmdemv.c | 5 ++--- client/emv/tlv.c | 29 +++++++++++++++++++++++++++++ client/emv/tlv.h | 1 + 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/client/emv/cmdemv.c b/client/emv/cmdemv.c index 16947885..ec6aa2c3 100644 --- a/client/emv/cmdemv.c +++ b/client/emv/cmdemv.c @@ -297,7 +297,7 @@ int UsageCmdHFEMVExec(void) { return 0; } -#define TLV_ADD(tag, value)( tlvdb_add(tlvRoot, tlvdb_fixed(tag, sizeof(value) - 1, (const unsigned char *)value)) ) +#define TLV_ADD(tag, value)( tlvdb_change_or_add_node(tlvRoot, tag, sizeof(value) - 1, (const unsigned char *)value) ) #define dreturn(n) {free(pdol_data_tlv);tlvdb_free(tlvSelect);tlvdb_free(tlvRoot);DropField();return n;} bool HexToBuffer(const char *errormsg, const char *hexvalue, uint8_t * buffer, size_t maxbufferlen, size_t *bufferlen) { @@ -417,8 +417,7 @@ bool ParamLoadFromJson(struct tlvdb *tlv) { return false; } - // TODO: here needs to be change_or_add!!!! - tlvdb_add(tlv, tlvdb_fixed(tag, tlvLength, (const unsigned char *)buf)); + tlvdb_change_or_add_node(tlv, tag, tlvLength, (const unsigned char *)buf); } json_decref(root); diff --git a/client/emv/tlv.c b/client/emv/tlv.c index 1be91777..0ec12364 100644 --- a/client/emv/tlv.c +++ b/client/emv/tlv.c @@ -342,6 +342,35 @@ void tlvdb_add(struct tlvdb *tlvdb, struct tlvdb *other) tlvdb->next = other; } +void tlvdb_change_or_add_node(struct tlvdb *tlvdb, tlv_tag_t tag, size_t len, const unsigned char *value) +{ + struct tlvdb *telm = tlvdb_find(tlvdb, tag); + if (telm == NULL) { + // new tlv element + tlvdb_add(tlvdb, tlvdb_fixed(tag, len, value)); + } else { + // the same tlv structure + if (telm->tag.tag == tag && telm->tag.len == len && !memcmp(telm->tag.value, value, len)) + return; + +PrintAndLog("-replace %x", tag); + // replace tlv element + struct tlvdb *tnewelm = tlvdb_fixed(tag, len, value); + tnewelm->next = telm->next; + tnewelm->parent = telm->parent; + + if (telm->parent && telm->parent->children == telm) { + telm->parent->children = tnewelm; + } + + +// telm->next = NULL; +// tlvdb_free(telm); + } + + return; +} + void tlvdb_visit(const struct tlvdb *tlvdb, tlv_cb cb, void *data, int level) { struct tlvdb *next = NULL; diff --git a/client/emv/tlv.h b/client/emv/tlv.h index 5b8b6825..295a3874 100644 --- a/client/emv/tlv.h +++ b/client/emv/tlv.h @@ -44,6 +44,7 @@ struct tlvdb *tlvdb_find_next(struct tlvdb *tlvdb, tlv_tag_t tag); struct tlvdb *tlvdb_find_path(struct tlvdb *tlvdb, tlv_tag_t tag[]); void tlvdb_add(struct tlvdb *tlvdb, struct tlvdb *other); +void tlvdb_change_or_add_node(struct tlvdb *tlvdb, tlv_tag_t tag, size_t len, const unsigned char *value); void tlvdb_visit(const struct tlvdb *tlvdb, tlv_cb cb, void *data, int level); const struct tlv *tlvdb_get(const struct tlvdb *tlvdb, tlv_tag_t tag, const struct tlv *prev);