From a79b2bb725f91d017924309d83654e07f8b3f5ad Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 15 Jan 2024 12:58:23 +0100 Subject: [PATCH] fix the too long filename bug when using a smaller width param. also added some extra text for output length of binary strings in decode. One tend to not see if its 25 or 26 bits sometimes :) --- client/src/cmddata.c | 38 +++++++++++++++++++++++++++++--------- client/src/cmdwiegand.c | 5 +++-- client/src/fileutils.c | 16 ++++++++++++++++ client/src/fileutils.h | 2 ++ 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/client/src/cmddata.c b/client/src/cmddata.c index 8654c8476..619f060a4 100644 --- a/client/src/cmddata.c +++ b/client/src/cmddata.c @@ -3192,7 +3192,7 @@ static int CmdDiff(const char *Cmd) { // dump magic card memory /* if (use_c) { - PrintAndLogEx(WARNING, "not implemented yet, feel free to contribute!"); + PrintAndLogEx(INFO, " To be implemented, feel free to contribute!"); return PM3_ENOTIMPL; } */ @@ -3200,21 +3200,41 @@ static int CmdDiff(const char *Cmd) { size_t biggest = (datalenA > datalenB) ? datalenA : datalenB; PrintAndLogEx(DEBUG, "data len: %zu A %zu B %zu", biggest, datalenA, datalenB); - if (inA == NULL) + if (inA == NULL) { PrintAndLogEx(INFO, "inA null"); + } - if (inB == NULL) + if (inB == NULL) { PrintAndLogEx(INFO, "inB null"); + } + + + char hdr0[400] = {0}; int hdr_sln = (width * 4) + 2; - char hdr0[300] = {0}; + int max_fn_space = (width * 4); - int max_fn_space = (width * 5); + if (max_fn_space < fnlenA) { + truncate_filename(filenameA, max_fn_space); + fnlenA = strlen(filenameA); + } - if (fnlenA && fnlenB && (max_fn_space > fnlenA) && (max_fn_space > fnlenB)) { + if (max_fn_space < fnlenB) { + truncate_filename(filenameB, max_fn_space); + fnlenB = strlen(filenameB); + } + + if (fnlenA && fnlenB ) { + snprintf(hdr0, sizeof(hdr0) - 1, " # | " _CYAN_("%.*s"), max_fn_space, filenameA); - memset(hdr0 + strlen(hdr0), ' ', hdr_sln - strlen(filenameA) - 1); + + // add space if needed + int padding_len = (hdr_sln - fnlenA - 1); + if ( padding_len > 0 ) { + memset(hdr0 + strlen(hdr0), ' ', padding_len); + } snprintf(hdr0 + strlen(hdr0), sizeof(hdr0) - 1 - strlen(hdr0), "| " _CYAN_("%.*s"), max_fn_space, filenameB); + } else { strcat(hdr0, " # | " _CYAN_("a")); memset(hdr0 + strlen(hdr0), ' ', hdr_sln - 2); @@ -3334,7 +3354,6 @@ static int CmdNumCon(const char *Cmd) { memset(dec, 0, sizeof(dec)); int res = CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)dec, sizeof(dec), &dlen); - int hlen = 256; char hex[256]; memset(hex, 0, sizeof(hex)); @@ -3363,13 +3382,13 @@ static int CmdNumCon(const char *Cmd) { mbedtls_mpi N; mbedtls_mpi_init(&N); - // hex if (hlen > 0) { if (data_verify_hex((uint8_t *)hex, hlen) == false) { return PM3_EINVARG; } MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16, hex)); + PrintAndLogEx(INFO, "Input hex len... %d", hlen); } // decimal @@ -3382,6 +3401,7 @@ static int CmdNumCon(const char *Cmd) { if (blen > 0) { // should have bianry string check here too MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 2, bin)); + PrintAndLogEx(INFO, "Input bin len... %d", blen); } mbedtls_mpi base; diff --git a/client/src/cmdwiegand.c b/client/src/cmdwiegand.c index 0973a9beb..9fc06b6f9 100644 --- a/client/src/cmdwiegand.c +++ b/client/src/cmdwiegand.c @@ -145,7 +145,7 @@ int CmdWiegandDecode(const char *Cmd) { if (hlen) { res = hexstring_to_u96(&top, &mid, &bot, hex); if (res != hlen) { - PrintAndLogEx(ERR, "hex string contains none hex chars"); + PrintAndLogEx(ERR, "Hex string contains none hex chars"); return PM3_EINVARG; } } else if (blen) { @@ -154,8 +154,9 @@ int CmdWiegandDecode(const char *Cmd) { PrintAndLogEx(ERR, "Binary string contains none <0|1> chars"); return PM3_EINVARG; } + PrintAndLogEx(INFO, "Input bin len... %d", blen); } else { - PrintAndLogEx(ERR, "empty input"); + PrintAndLogEx(ERR, "Empty input"); return PM3_EINVARG; } diff --git a/client/src/fileutils.c b/client/src/fileutils.c index c4e915509..8f8b77f63 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -239,6 +239,22 @@ char *newfilenamemcopyEx(const char *preferredName, const char *suffix, savePath return fileName; } +// trunacate down a filename to LEN size +void truncate_filename(char *fn, uint16_t maxlen) { + if (fn == NULL || maxlen < 5) { + return; + } + + // Check if the filename is already shorter than or equal to the desired length + if (strlen(fn) <= maxlen) { + return; + } + + // If there's no extension or it's too long, just truncate the filename + fn[maxlen - 3] = '\0'; + strcat(fn, "..."); +} + // --------- SAVE FILES int saveFile(const char *preferredName, const char *suffix, const void *data, size_t datalen) { diff --git a/client/src/fileutils.h b/client/src/fileutils.h index 1af72f28d..fa36fb89e 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -107,6 +107,8 @@ bool setDefaultPath(savePaths_t pathIndex, const char *path); char *newfilenamemcopy(const char *preferredName, const char *suffix); char *newfilenamemcopyEx(const char *preferredName, const char *suffix, savePaths_t save_path); +void truncate_filename(char *fn, uint16_t len); + /** * @brief Utility function to save data to a binary file. This method takes a preferred name, but if that