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 :)

This commit is contained in:
iceman1001 2024-01-15 12:58:23 +01:00
parent 00dfd6e5f1
commit a79b2bb725
4 changed files with 50 additions and 11 deletions

View file

@ -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 (max_fn_space < fnlenB) {
truncate_filename(filenameB, max_fn_space);
fnlenB = strlen(filenameB);
}
if (fnlenA && fnlenB ) {
if (fnlenA && fnlenB && (max_fn_space > fnlenA) && (max_fn_space > 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;

View file

@ -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;
}

View file

@ -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) {

View file

@ -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