diff --git a/client/src/util.c b/client/src/util.c index 0e2e27151..4a8aced6e 100644 --- a/client/src/util.c +++ b/client/src/util.c @@ -558,6 +558,64 @@ char *sprint_ascii(const uint8_t *data, const size_t len) { return sprint_ascii_ex(data, len, 0); } +char *sprint_breakdown_bin(color_t color, const char* bs, int width, int padn, int bits, const char* msg) { + + if (bs == NULL || width > 32) { + return NULL; + } + + const char *prepad = " "; + const char *postmarker = "................................"; + static char buf[32 + 120] = {0}; + memset(buf, 0, sizeof(buf)); + + int8_t end = (width - padn - 1 - bits); + if (end < 0) { + end = 0; + } + + switch (color) { + case C_GREEN: { + snprintf(buf, sizeof(buf), "%.*s" _GREEN_("%.*s") " %.*s - " _GREEN_("%s") + , padn, prepad + , bits, bs + padn + , end, postmarker + , msg + ); + break; + } + case C_RED: { + snprintf(buf, sizeof(buf), "%.*s" _RED_("%.*s") " %.*s - " _RED_("%s") + , padn, prepad + , bits, bs + padn + , end, postmarker + , msg + ); + break; + } + case C_YELLOW: { + snprintf(buf, sizeof(buf), "%.*s" _YELLOW_("%.*s") " %.*s - " _YELLOW_("%s") + , padn, prepad + , bits, bs + padn + , end, postmarker + , msg + ); + break; + } + case C_NONE: + default: { + snprintf(buf, sizeof(buf), "%.*s%.*s %.*s - %s" + , padn, prepad + , bits, bs + padn + , end, postmarker + , msg + ); + break; + } + } + return buf; +} + int hex_to_bytes(const char *hexValue, uint8_t *bytesValue, size_t maxBytesValueLen) { char buf[4] = {0}; int indx = 0; diff --git a/client/src/util.h b/client/src/util.h index 1949c1d53..4dd95175b 100644 --- a/client/src/util.h +++ b/client/src/util.h @@ -54,6 +54,13 @@ extern int g_numCPUs; #define PM3_RET_IF_ERR_WITH_MSG(res, ...) if (res != PM3_SUCCESS) { PrintAndLogEx(ERR, __VA_ARGS__); return res; } #define PM3_RET_IF_ERR_MAYBE_MSG(res, verbose, ...) if (res != PM3_SUCCESS) { if (verbose) PrintAndLogEx(ERR, __VA_ARGS__); return res; } +typedef enum { + C_NONE, + C_GREEN, + C_RED, + C_YELLOW, +} color_t; + int kbd_enter_pressed(void); void FillFileNameByUID(char *filenamePrefix, const uint8_t *uid, const char *ext, const int uidlen); // fill buffer from structure [{uint8_t data, size_t length},...] @@ -81,6 +88,8 @@ char *sprint_hex_ascii(const uint8_t *data, const size_t len); char *sprint_ascii(const uint8_t *data, const size_t len); char *sprint_ascii_ex(const uint8_t *data, const size_t len, const size_t min_str_len); +char *sprint_breakdown_bin(color_t color, const char* bs, int width, int padn, int bits, const char* msg); + void print_buffer_with_offset(const uint8_t *data, const size_t len, int offset, bool print_header); void print_buffer(const uint8_t *data, const size_t len, int level); void print_blocks(uint32_t *data, size_t len);