X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/43912d6349ce08bb5d299b1602fba30e0354eaaa..7dadcc959fb7009b6e8bbde4a644aa2f7f1b7a98:/client/emv/tlv.c

diff --git a/client/emv/tlv.c b/client/emv/tlv.c
index 7feaa9aa..776fdeed 100644
--- a/client/emv/tlv.c
+++ b/client/emv/tlv.c
@@ -299,6 +299,58 @@ void tlvdb_free(struct tlvdb *tlvdb)
 	}
 }
 
+struct tlvdb *tlvdb_find_next(struct tlvdb *tlvdb, tlv_tag_t tag) {
+	if (!tlvdb)
+		return NULL;
+	
+	return tlvdb_find(tlvdb->next, tag);
+}
+
+struct tlvdb *tlvdb_find(struct tlvdb *tlvdb, tlv_tag_t tag) {
+	if (!tlvdb)
+		return NULL;
+	
+	for (; tlvdb; tlvdb = tlvdb->next) {
+		if (tlvdb->tag.tag == tag)
+			return tlvdb;
+	}
+
+	return NULL;
+}
+
+struct tlvdb *tlvdb_find_full(struct tlvdb *tlvdb, tlv_tag_t tag) {
+	if (!tlvdb)
+		return NULL;
+	
+	for (; tlvdb; tlvdb = tlvdb->next) {
+		if (tlvdb->tag.tag == tag)
+			return tlvdb;
+		
+		if (tlvdb->children) {
+			struct tlvdb * ch = tlvdb_find_full(tlvdb->children, tag);
+			if (ch)
+				return ch;
+		}			
+	}
+
+	return NULL;
+}
+
+struct tlvdb *tlvdb_find_path(struct tlvdb *tlvdb, tlv_tag_t tag[]) {
+	int i = 0;
+	struct tlvdb *tnext = tlvdb;
+	
+	while (tnext && tag[i]) {
+		tnext = tlvdb_find(tnext, tag[i]);
+		i++;
+		if (tag[i] && tnext) {
+			tnext = tnext->children;
+		}
+	}
+	
+	return tnext;
+}
+
 void tlvdb_add(struct tlvdb *tlvdb, struct tlvdb *other)
 {
 	while (tlvdb->next) {
@@ -308,6 +360,52 @@ 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_full(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;
+
+		// replace tlv element
+		struct tlvdb *tnewelm = tlvdb_fixed(tag, len, value);
+		tnewelm->next = telm->next;
+		tnewelm->parent = telm->parent;
+		
+		// if telm stayed first in children chain
+		if (telm->parent && telm->parent->children == telm) {
+			telm->parent->children = tnewelm;
+		}
+		
+		// if telm have previous element
+		if (telm != tlvdb) {
+			// elm in root
+			struct tlvdb *celm = tlvdb;
+			// elm in child list of node
+			if (telm->parent && telm->parent->children)
+				celm = telm->parent->children;		
+			
+			// find previous element
+			for (; celm; celm = celm->next) {
+				if (celm->next == telm) {
+					celm->next = tnewelm;
+					break;
+				}
+			}
+		}
+		
+		// free old element with childrens
+		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;
@@ -317,8 +415,8 @@ void tlvdb_visit(const struct tlvdb *tlvdb, tlv_cb cb, void *data, int level)
 
 	for (; tlvdb; tlvdb = next) {
 		next = tlvdb->next;
-		cb(data, &tlvdb->tag, level);
-		tlvdb_visit(tlvdb->children, cb, data, level+1);
+		cb(data, &tlvdb->tag, level, (tlvdb->children == NULL));
+		tlvdb_visit(tlvdb->children, cb, data, level + 1);
 	}
 }
 
@@ -355,6 +453,11 @@ const struct tlv *tlvdb_get(const struct tlvdb *tlvdb, tlv_tag_t tag, const stru
 	return NULL;
 }
 
+const struct tlv *tlvdb_get_inchild(const struct tlvdb *tlvdb, tlv_tag_t tag, const struct tlv *prev) {
+	tlvdb = tlvdb->children;
+	return tlvdb_get(tlvdb, tag, prev);
+}
+
 unsigned char *tlv_encode(const struct tlv *tlv, size_t *len)
 {
 	size_t size = tlv->len;