diff --git a/client/src/util.c b/client/src/util.c index 723f3e758..51408a524 100644 --- a/client/src/util.c +++ b/client/src/util.c @@ -911,27 +911,32 @@ https://github.com/ApertureLabsLtd/RFIDler/blob/master/firmware/Pic32/RFIDler.X/ // convert hex to sequence of 0/1 bit values // returns number of bits converted int hextobinarray(char *target, char *source) { - int length, i, count = 0; + return hextobinarray_n(target, source, strlen(source)); +} + +int hextobinarray_n(char *target, char *source, int sourcelen) { + int i, count = 0; char *start = source; - length = strlen(source); // process 4 bits (1 hex digit) at a time - while (length--) { + while (sourcelen--) { char x = *(source++); // capitalize - if (x >= 'a' && x <= 'f') + if (x >= 'a' && x <= 'f') { x -= 32; + } // convert to numeric value - if (x >= '0' && x <= '9') + if (x >= '0' && x <= '9') { x -= '0'; - else if (x >= 'A' && x <= 'F') + } else if (x >= 'A' && x <= 'F') { x -= 'A' - 10; - else { + } else { PrintAndLogEx(INFO, "(hextobinarray) discovered unknown character %c %d at idx %d of %s", x, x, (int16_t)(source - start), start); return 0; } // output - for (i = 0 ; i < 4 ; ++i, ++count) + for (i = 0 ; i < 4 ; ++i, ++count) { *(target++) = (x >> (3 - i)) & 1; + } } return count; @@ -939,9 +944,14 @@ int hextobinarray(char *target, char *source) { // convert hex to human readable binary string int hextobinstring(char *target, char *source) { - int length = hextobinarray(target, source); - if (length == 0) + return hextobinstring_n(target, source, strlen(source)); +} + +int hextobinstring_n(char *target, char *source, int sourcelen) { + int length = hextobinarray_n(target, source, sourcelen); + if (length == 0) { return 0; + } binarraytobinstring(target, target, length); return length; } diff --git a/client/src/util.h b/client/src/util.h index c29fbd50f..4dab9ae06 100644 --- a/client/src/util.h +++ b/client/src/util.h @@ -112,7 +112,11 @@ int param_getbin_to_eol(const char *line, int paramnum, uint8_t *data, int maxda int param_getstr(const char *line, int paramnum, char *str, size_t buffersize); int hextobinarray(char *target, char *source); +int hextobinarray_n(char *target, char *source, int sourcelen); + int hextobinstring(char *target, char *source); +int hextobinstring_n(char *target, char *source, int sourcelen); + int binarraytohex(char *target, const size_t targetlen, const char *source, size_t srclen); void binarraytobinstring(char *target, char *source, int length); int binstring2binarray(uint8_t *target, char *source, int length);