added some helper functions

This commit is contained in:
iceman1001 committed 2023-10-01 13:05:08 +02:00
1 parent 7320f0dd16
commit bb5d61dca7
11 files changed
+111 -28

No files matched your search

+5 -3
View File
@@ -328,7 +328,7 @@ static int smart_responseEx(uint8_t *out, int maxoutlen, bool verbose) {
needGetData = true;
}
if (needGetData == true) {
if (needGetData) {
// Don't discard data we already received except the SW code.
// If we only received 1 byte, this is the echo of INS, we discard it.
totallen -= 2;
@@ -1222,8 +1222,9 @@ int ExchangeAPDUSC(bool verbose, uint8_t *datain, int datainlen, bool activateCa
}
bool smart_select(bool verbose, smart_card_atr_t *atr) {
if (atr)
if (atr) {
memset(atr, 0, sizeof(smart_card_atr_t));
}
clearCommandBuffer();
SendCommandNG(CMD_SMART_ATR, NULL, 0);
@@ -1241,8 +1242,9 @@ bool smart_select(bool verbose, smart_card_atr_t *atr) {
smart_card_atr_t card;
memcpy(&card, (smart_card_atr_t *)resp.data.asBytes, sizeof(smart_card_atr_t));
if (atr)
if (atr) {
memcpy(atr, &card, sizeof(smart_card_atr_t));
}
if (verbose)
PrintAndLogEx(INFO, "ISO7816-3 ATR : %s", sprint_hex(card.atr, card.atr_len));
+21 -3
View File
@@ -944,11 +944,11 @@ int hextobinarray_n(char *target, char *source, int sourcelen) {
return count;
}
// convert hex to human readable binary string
// convert hexstring to human readable binary string
int hextobinstring(char *target, char *source) {
return hextobinstring_n(target, source, strlen(source));
}
// convert hexstring to human readable binary string
int hextobinstring_n(char *target, char *source, int sourcelen) {
int length = hextobinarray_n(target, source, sourcelen);
if (length == 0) {
@@ -958,6 +958,23 @@ int hextobinstring_n(char *target, char *source, int sourcelen) {
return length;
}
// convert bytes to binary string
int byte_2_binstr(char *target, const uint8_t *source, size_t sourcelen) {
//uint8_t *p = *source;
for (int i = 0 ; i < sourcelen; ++i) {
uint8_t b = *(source++);
*(target++) = ((b >> 7) & 0x1) + '0';
*(target++) = ((b >> 6) & 0x1) + '0';
*(target++) = ((b >> 5) & 0x1) + '0';
*(target++) = ((b >> 4) & 0x1) + '0';
*(target++) = ((b >> 3) & 0x1) + '0';
*(target++) = ((b >> 2) & 0x1) + '0';
*(target++) = ((b >> 1) & 0x1) + '0';
*(target++) = (b & 0x1) + '0';
}
*target = '\0';
}
// convert binary array of 0x00/0x01 values to hex
// return number of bits converted
int binarraytohex(char *target, const size_t targetlen, const char *source, size_t srclen) {
@@ -1007,8 +1024,9 @@ int binarraytohex(char *target, const size_t targetlen, const char *source, size
// convert binary array to human readable binary
void binarraytobinstring(char *target, char *source, int length) {
for (int i = 0 ; i < length; ++i)
for (int i = 0 ; i < length; ++i) {
*(target++) = *(source++) + '0';
}
*target = '\0';
}
+2
View File
@@ -122,6 +122,8 @@ int binarraytohex(char *target, const size_t targetlen, const char *source, size
void binarraytobinstring(char *target, char *source, int length);
int binstring2binarray(uint8_t *target, char *source, int length);
int byte_2_binstr(char *target, const uint8_t *source, size_t sourcelen);
uint8_t GetParity(const uint8_t *bits, uint8_t type, int length);
void wiegand_add_parity(uint8_t *target, uint8_t *source, uint8_t length);
void wiegand_add_parity_swapped(uint8_t *target, uint8_t *source, uint8_t length);