getAtrInfo without regex engine

This commit is contained in:
Philippe Teuwen 2021-10-10 12:23:19 +02:00
commit b7ea848f7f
2 changed files with 27 additions and 44 deletions

View file

@ -8,54 +8,37 @@
// ATR information lookup
//-----------------------------------------------------------------------------
#include "atrs.h"
#include <ctype.h>
#include <string.h>
#include <regex.h> // regex?
#include <stdlib.h>
#include "commonutil.h" // ARRAYLEN
#include "ui.h" // get PrintAndLogEx
#include "util.h" // startswith
// get a ATR description based on the atr bytes
// returns description of the best match
const char *getAtrInfo(const char *atr_str) {
for (int i = 0; i < ARRAYLEN(AtrTable); ++i) {
// check for dots in atr table.
// dots indicate those bytes at those positions are optional.
// need a special case for them
if (strstr(AtrTable[i].bytes, ".") != NULL) {
regex_t r;
int ret = regcomp(&r, AtrTable[i].bytes, 0);
if (ret) {
// take next ATR value.
PrintAndLogEx(DEBUG, "can't compile regex");
continue;
size_t slen = strlen(atr_str);
int match = -1;
// skip last element of AtrTable
for (int i = 0; i < ARRAYLEN(AtrTable) - 1; ++i) {
if (strlen(AtrTable[i].bytes) != slen)
continue;
if (strstr(AtrTable[i].bytes, "..") != NULL) {
char *tmp_atr = malloc(slen);
for (int j = 0; j < slen; j++) {
tmp_atr[j] = AtrTable[i].bytes[j]=='.' ? '.' : atr_str[j];
}
/* Execute regular expression */
ret = regexec(&r, atr_str, 0, NULL, 0);
regfree(&r);
if (!ret) {
return AtrTable[i].desc;
if (strncmp(tmp_atr, AtrTable[i].bytes, slen) == 0) {
// record partial match but continue looking for full match
match = i;
}
else if (ret == REG_NOMATCH) {
continue;
}
else {
PrintAndLogEx(DEBUG, "regex failed");
continue;
}
free(tmp_atr);
} else {
if (str_startswith(atr_str, AtrTable[i].bytes)) {
return AtrTable[i].desc;
}
if (strncmp(atr_str, AtrTable[i].bytes, slen) == 0) return AtrTable[i].desc;
}
}
//No match, return default
return AtrTable[ARRAYLEN(AtrTable) - 1].desc;
if (match >= 0) {
return AtrTable[match].desc;
} else {
//No match, return default = last element of AtrTable
return AtrTable[ARRAYLEN(AtrTable) - 1].desc;
}
}

View file

@ -695,7 +695,7 @@ static int CmdSmartInfo(const char *Cmd) {
PrintAndLogEx(INFO, "http://smartcard-atr.apdu.fr/parse?ATR=%s", sprint_hex_inrow(card.atr, card.atr_len));
//PrintAndLogEx(INFO, "ATR loopup : %s", getAtrInfo("3FFF112503108041B00669FF4A50700000415A010011"));
//PrintAndLogEx(INFO, "ATR lookup : %s", getAtrInfo("3FFF112503108041B00669FF4A50700000415A010011"));
// print ATR
PrintAndLogEx(INFO, "ATR");
@ -761,15 +761,15 @@ static int CmdSmartReader(const char *Cmd) {
PrintAndLogEx(INFO, "ISO7816-3 ATR : %s", sprint_hex(card->atr, card->atr_len));
// test with normal lookup, with existing ATR in table
PrintAndLogEx(INFO, "ATR loopup : %s", getAtrInfo("3FFF112503108041B00669FF4A50700000415A010011")); // Astro (Pay TV)
PrintAndLogEx(INFO, "ATR lookup : %s", getAtrInfo("3FFF112503108041B00669FF4A50700000415A010011")); // Astro (Pay TV)
//PrintAndLogEx(INFO, "ATR loopup : %s", getAtrInfo("3FFD13250250800F..B0..69FF4A50D08000495403"));
//PrintAndLogEx(INFO, "ATR lookup : %s", getAtrInfo("3FFD13250250800F..B0..69FF4A50D08000495403"));
// test with random bytes in .. space
PrintAndLogEx(INFO, "ATR loopup : %s", getAtrInfo("3FFD13250250800FEEB00269FF4A50D08000495403")); // Sky (Italy) VideoGuard CAM card
PrintAndLogEx(INFO, "ATR lookup : %s", getAtrInfo("3FFD13250250800FEEB00269FF4A50D08000495403")); // Sky (Italy) VideoGuard CAM card
// test w shorted str, not in ATR table
PrintAndLogEx(INFO, "ATR loopup : %s", getAtrInfo("3FFD13250250800FEEB00269FF4A50D080004954")); // Sky (Italy) VideoGuard CAM card
PrintAndLogEx(INFO, "ATR lookup : %s", getAtrInfo("3FFD13250250800FEEB00269FF4A50D080004954")); // Sky (Italy) VideoGuard CAM card
return PM3_SUCCESS;
}