From 0a0155fea7f88ebf16791414b59486bf0ca7a651 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Wed, 4 Nov 2020 19:19:38 +0100 Subject: [PATCH] refactore printDemodbuff fct. --- client/src/cmddata.c | 129 ++++++++++++++++++++------------------ client/src/cmddata.h | 3 +- client/src/cmdlfawid.c | 6 +- client/src/cmdlfem4x.c | 5 +- client/src/cmdlfhid.c | 6 +- client/src/cmdlfindala.c | 2 +- client/src/cmdlfio.c | 3 +- client/src/cmdlfparadox.c | 5 +- client/src/cmdlfpyramid.c | 5 +- 9 files changed, 90 insertions(+), 74 deletions(-) diff --git a/client/src/cmddata.c b/client/src/cmddata.c index 40a8ea5a3..9724f6dd9 100644 --- a/client/src/cmddata.c +++ b/client/src/cmddata.c @@ -398,19 +398,64 @@ static int CmdSetDebugMode(const char *Cmd) { //by marshmellow // max output to 512 bits if we have more // doesn't take inconsideration where the demod offset or bitlen found. -void printDemodBuff(void) { - int len = DemodBufferLen; - if (len < 1) { - PrintAndLogEx(INFO, "(printDemodBuff) no bits found in demod buffer"); - return; +int printDemodBuff(uint8_t offset, bool strip_leading, bool invert, bool print_hex) { + size_t len = DemodBufferLen; + if (len == 0) { + PrintAndLogEx(WARNING, "Demodbuffer is empty"); + return PM3_EINVARG; } - if (len > 512) len = 512; - PrintAndLogEx(NORMAL, "%s", sprint_bin_break(DemodBuffer, len, 32)); + uint8_t *buf = NULL; + + if (strip_leading) { + buf = (DemodBuffer + offset); + + if (len > (DemodBufferLen - offset)) + len = (DemodBufferLen - offset); + + size_t i; + for (i = 0; i < len; i++) { + if (buf[i] == 1) break; + } + offset += i; + } + + if (len > (DemodBufferLen - offset)) { + len = (DemodBufferLen - offset); + } + + if (len > 512) { + len = 512; + } + + if (invert) { + buf = (DemodBuffer + offset); + for (size_t i = 0; i < len; i++) { + if (buf[i] == 1) + buf[i] = 0; + else { + if (buf[i] == 0) + buf[i] = 1; + } + } + } + + if (print_hex) { + buf = (DemodBuffer + offset); + char hex[512] = {0x00}; + int num_bits = binarraytohex(hex, sizeof(hex), (char*)buf, len); + if (num_bits == 0) { + return PM3_ESOFT; + } + PrintAndLogEx(SUCCESS, "DemodBuffer: %s", hex); + } else { + PrintAndLogEx(SUCCESS, "DemodBuffer:\n%s", sprint_bin_break(DemodBuffer + offset, len, 32)); + } + return PM3_SUCCESS; } int CmdPrintDemodBuff(const char *Cmd) { - bool hexMode = false; + bool print_hex = false; bool errors = false; bool lstrip = false; bool invert = false; @@ -422,7 +467,7 @@ int CmdPrintDemodBuff(const char *Cmd) { case 'h': return usage_data_printdemodbuf(); case 'x': - hexMode = true; + print_hex = true; cmdp++; break; case 'o': @@ -452,45 +497,7 @@ int CmdPrintDemodBuff(const char *Cmd) { //Validations if (errors) return usage_data_printdemodbuf(); - if (DemodBufferLen == 0) { - PrintAndLogEx(WARNING, "Demodbuffer is empty"); - return PM3_ESOFT; - } - if (lstrip) { - char *buf = (char *)(DemodBuffer + offset); - length = (length > (DemodBufferLen - offset)) ? DemodBufferLen - offset : length; - uint32_t i; - for (i = 0; i < length; i++) { - if (buf[i] == 1) break; - } - offset += i; - } - length = (length > (DemodBufferLen - offset)) ? DemodBufferLen - offset : length; - - if (invert) { - char *buf = (char *)(DemodBuffer + offset); - for (uint32_t i = 0; i < length; i++) { - if (buf[i] == 1) - buf[i] = 0; - else { - if (buf[i] == 0) - buf[i] = 1; - } - } - } - - if (hexMode) { - char *buf = (char *)(DemodBuffer + offset); - char hex[512] = {0x00}; - int numBits = binarraytohex(hex, sizeof(hex), buf, length); - if (numBits == 0) { - return PM3_ESOFT; - } - PrintAndLogEx(SUCCESS, "DemodBuffer: %s", hex); - } else { - PrintAndLogEx(SUCCESS, "DemodBuffer:\n%s", sprint_bin_break(DemodBuffer + offset, length, 32)); - } - return PM3_SUCCESS; + return printDemodBuff(offset, lstrip, invert, print_hex); } //by marshmellow @@ -596,7 +603,7 @@ int ASKDemod_ext(int clk, int invert, int maxErr, size_t maxLen, bool amplify, b else PrintAndLogEx(DEBUG, "ASK/Raw - Clock: %d - Decoded bitstream:", clk); - printDemodBuff(); + printDemodBuff(0, false, false, false); } uint64_t lo = 0; uint32_t hi = 0; @@ -792,7 +799,7 @@ int ASKbiphaseDemod(int offset, int clk, int invert, int maxErr, bool verbose) { setClockGrid(clk, startIdx + clk * offset / 2); if (g_debugMode || verbose) { PrintAndLogEx(DEBUG, "Biphase Decoded using offset %d | clock %d | #errors %d | start index %d\ndata\n", offset, clk, errCnt, (startIdx + clk * offset / 2)); - printDemodBuff(); + printDemodBuff(offset, false, false, false); } return PM3_SUCCESS; } @@ -1204,7 +1211,7 @@ int FSKrawDemod(uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow, bo if (verbose || g_debugMode) { PrintAndLogEx(DEBUG, "DEBUG: (FSKrawDemod) Using Clock:%u, invert:%u, fchigh:%u, fclow:%u", rfLen, invert, fchigh, fclow); PrintAndLogEx(NORMAL, "%s decoded bitstream:", GetFSKType(fchigh, fclow, invert)); - printDemodBuff(); + printDemodBuff(0, false, invert, false); } goto out; } else { @@ -1327,7 +1334,7 @@ int NRZrawDemod(int clk, int invert, int maxErr, bool verbose) { if (verbose || g_debugMode) { PrintAndLogEx(NORMAL, "NRZ demoded bitstream:"); // Now output the bitstream to the scrollback by line of 16 bits - printDemodBuff(); + printDemodBuff(0, false, invert, false); } free(bits); @@ -1352,14 +1359,14 @@ static int CmdNRZrawDemod(const char *Cmd) { } // by marshmellow -// takes 3 arguments - clock, invert, maxErr as integers +// takes 3 arguments - clock, invert, max_err as integers // attempts to demodulate psk only // prints binary found and saves in demodbuffer for further commands int CmdPSK1rawDemod(const char *Cmd) { char cmdp = tolower(param_getchar(Cmd, 0)); if (strlen(Cmd) > 16 || cmdp == 'h') return usage_data_rawdemod_p1(); - int clk = 0, invert = 0, maxErr = 100; - sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr); + int clk = 0, invert = 0, max_err = 100; + sscanf(Cmd, "%i %i %i", &clk, &invert, &max_err); if (clk == 1) { invert = 1; clk = 0; @@ -1368,7 +1375,7 @@ int CmdPSK1rawDemod(const char *Cmd) { PrintAndLogEx(WARNING, "Invalid value for invert: %i", invert); return PM3_EINVARG; } - int ans = PSKDemod(clk, invert, maxErr, true); + int ans = PSKDemod(clk, invert, max_err, true); //output if (ans != PM3_SUCCESS) { if (g_debugMode) PrintAndLogEx(ERR, "Error demoding: %d", ans); @@ -1376,7 +1383,7 @@ int CmdPSK1rawDemod(const char *Cmd) { } PrintAndLogEx(NORMAL, "PSK1 demoded bitstream:"); // Now output the bitstream to the scrollback by line of 16 bits - printDemodBuff(); + printDemodBuff(0, false, invert, false); return PM3_SUCCESS; } @@ -1385,8 +1392,8 @@ int CmdPSK1rawDemod(const char *Cmd) { static int CmdPSK2rawDemod(const char *Cmd) { char cmdp = tolower(param_getchar(Cmd, 0)); if (strlen(Cmd) > 16 || cmdp == 'h') return usage_data_rawdemod_p2(); - int clk = 0, invert = 0, maxErr = 100; - sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr); + int clk = 0, invert = 0, max_err = 100; + sscanf(Cmd, "%i %i %i", &clk, &invert, &max_err); if (clk == 1) { invert = 1; clk = 0; @@ -1395,7 +1402,7 @@ static int CmdPSK2rawDemod(const char *Cmd) { PrintAndLogEx(WARNING, "Invalid value for invert: %i", invert); return PM3_EINVARG; } - int ans = PSKDemod(clk, invert, maxErr, true); + int ans = PSKDemod(clk, invert, max_err, true); if (ans != PM3_SUCCESS) { if (g_debugMode) PrintAndLogEx(ERR, "Error demoding: %d", ans); return PM3_ESOFT; @@ -1403,7 +1410,7 @@ static int CmdPSK2rawDemod(const char *Cmd) { psk1TOpsk2(DemodBuffer, DemodBufferLen); PrintAndLogEx(NORMAL, "PSK2 demoded bitstream:"); // Now output the bitstream to the scrollback by line of 16 bits - printDemodBuff(); + printDemodBuff(0, false, invert, false); return PM3_SUCCESS; } diff --git a/client/src/cmddata.h b/client/src/cmddata.h index 785e88b49..6ae362933 100644 --- a/client/src/cmddata.h +++ b/client/src/cmddata.h @@ -68,7 +68,8 @@ int PSKDemod(int clk, int invert, int maxErr, bool verbose); int NRZrawDemod(int clk, int invert, int maxErr, bool verbose); // used by cmd lf pac, lf t55xx -void printDemodBuff(void); +int printDemodBuff(uint8_t offset, bool strip_leading, bool invert, bool print_hex); + void setDemodBuff(uint8_t *buff, size_t size, size_t start_idx); bool getDemodBuff(uint8_t *buff, size_t *size); void save_restoreDB(uint8_t saveOpt);// option '1' to save DemodBuffer any other to restore diff --git a/client/src/cmdlfawid.c b/client/src/cmdlfawid.c index b22d0adb0..1616c63fe 100644 --- a/client/src/cmdlfawid.c +++ b/client/src/cmdlfawid.c @@ -330,8 +330,10 @@ int demodAWID(bool verbose) { free(bits); PrintAndLogEx(DEBUG, "DEBUG: AWID idx: %d, Len: %zu Printing Demod Buffer:", idx, size); - if (g_debugMode) - printDemodBuff(); + if (g_debugMode) { + printDemodBuff(0, false, false, true); + printDemodBuff(0, false, false, false); + } return PM3_SUCCESS; } diff --git a/client/src/cmdlfem4x.c b/client/src/cmdlfem4x.c index 1956bc4d1..9c1c7e0a6 100644 --- a/client/src/cmdlfem4x.c +++ b/client/src/cmdlfem4x.c @@ -318,8 +318,9 @@ int AskEm410xDecode(bool verbose, uint32_t *hi, uint64_t *lo) { setClockGrid(g_DemodClock, g_DemodStartIdx + ((idx + 1)*g_DemodClock)); PrintAndLogEx(DEBUG, "DEBUG: Em410x idx: %zu, Len: %zu, Printing Demod Buffer:", idx, size); - if (g_debugMode) - printDemodBuff(); + if (g_debugMode) { + printDemodBuff(0, false, false, true); + } if (verbose) printEM410x(*hi, *lo); diff --git a/client/src/cmdlfhid.c b/client/src/cmdlfhid.c index 05a8c7dff..c4443e688 100644 --- a/client/src/cmdlfhid.c +++ b/client/src/cmdlfhid.c @@ -140,8 +140,10 @@ int demodHID(bool verbose) { HIDTryUnpack(&packed, false); PrintAndLogEx(DEBUG, "DEBUG: HID idx: %d, Len: %zu, Printing Demod Buffer: ", idx, size); - if (g_debugMode) - printDemodBuff(); + if (g_debugMode) { + printDemodBuff(0, false, false, true); + printDemodBuff(0, false, false, false); + } return PM3_SUCCESS; } diff --git a/client/src/cmdlfindala.c b/client/src/cmdlfindala.c index a5c81114a..b9a811fd7 100644 --- a/client/src/cmdlfindala.c +++ b/client/src/cmdlfindala.c @@ -268,7 +268,7 @@ int demodIndalaEx(int clk, int invert, int maxErr, bool verbose) { if (g_debugMode) { PrintAndLogEx(DEBUG, "DEBUG: Indala - printing demodbuffer"); - printDemodBuff(); + printDemodBuff(0, false, false, false); } return PM3_SUCCESS; } diff --git a/client/src/cmdlfio.c b/client/src/cmdlfio.c index 729970246..f0f76e61d 100644 --- a/client/src/cmdlfio.c +++ b/client/src/cmdlfio.c @@ -185,7 +185,8 @@ int demodIOProx(bool verbose) { PrintAndLogEx(DEBUG, "DEBUG: Error - IO prox crc failed"); PrintAndLogEx(DEBUG, "DEBUG: IO prox idx: %d, Len: %zu, Printing demod buffer:", idx, size); - printDemodBuff(); + printDemodBuff(0, false, false, true); + printDemodBuff(0, false, false, false); } return retval; } diff --git a/client/src/cmdlfparadox.c b/client/src/cmdlfparadox.c index 8fc3751c8..1b0aae510 100644 --- a/client/src/cmdlfparadox.c +++ b/client/src/cmdlfparadox.c @@ -192,8 +192,9 @@ int demodParadox(bool verbose) { ); PrintAndLogEx(DEBUG, "DEBUG: Paradox idx: %d, len: %zu, Printing Demod Buffer:", idx, size); - if (g_debugMode) - printDemodBuff(); + if (g_debugMode) { + printDemodBuff(0, false, false, false); + } return PM3_SUCCESS; } diff --git a/client/src/cmdlfpyramid.c b/client/src/cmdlfpyramid.c index cd5cb84a2..aba305b5d 100644 --- a/client/src/cmdlfpyramid.c +++ b/client/src/cmdlfpyramid.c @@ -203,8 +203,9 @@ int demodPyramid(bool verbose) { ); PrintAndLogEx(DEBUG, "DEBUG: Pyramid: idx: %d, Len: %d, Printing Demod Buffer:", idx, 128); - if (g_debugMode) - printDemodBuff(); + if (g_debugMode) { + printDemodBuff(0, false, false, false); + } return PM3_SUCCESS; }