From 78a94ff9022f84d186d01e1baa0eafdf78a7652d Mon Sep 17 00:00:00 2001
From: merlokk <olegmsn@gmail.com>
Date: Tue, 31 Oct 2017 19:19:49 +0200
Subject: [PATCH] improved GetAPDUCode()

---
 client/emv/apduinfo.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/client/emv/apduinfo.c b/client/emv/apduinfo.c
index d73c24fc..fbdd714a 100644
--- a/client/emv/apduinfo.c
+++ b/client/emv/apduinfo.c
@@ -279,16 +279,33 @@ int CodeCmp(const char *code1, const char *code2) {
 
 const APDUCode* const GetAPDUCode(uint8_t sw1, uint8_t sw2) {
 	char buf[4] = {0};
+	int res;
+	int mineq = 100;
+	int mineqindx = 0;
 	
 	sprintf(&buf[0], "%02X ", sw1);
 	sprintf(&buf[2], "%02X ", sw2);
 	
 	for (int i = 0; i < APDUCodeTableLen; i++) {
-		if (CodeCmp(APDUCodeTable[i].ID, buf) == 0) { // TODO make not so equal comparation... XXXX - not works...
+		res = CodeCmp(APDUCodeTable[i].ID, buf);
+		
+		// equal
+		if (res == 0) { 
 			return &APDUCodeTable[i];
 		}
+		
+		// with some  'X'
+		if (res > 0 && mineq > res) {
+			mineq = res;
+			mineqindx = i;
+		}
 	}
 
+	// if we have not equal, but with some 'X'
+	if (mineqindx < 100) {
+		return &APDUCodeTable[mineqindx];
+	}
+	
 	return NULL;
 }
 
-- 
2.39.5