refactore printDemodbuff fct.

This commit is contained in:
iceman1001 2020-11-04 19:19:38 +01:00
commit 0a0155fea7
9 changed files with 90 additions and 74 deletions

View file

@ -398,19 +398,64 @@ static int CmdSetDebugMode(const char *Cmd) {
//by marshmellow //by marshmellow
// max output to 512 bits if we have more // max output to 512 bits if we have more
// doesn't take inconsideration where the demod offset or bitlen found. // doesn't take inconsideration where the demod offset or bitlen found.
void printDemodBuff(void) { int printDemodBuff(uint8_t offset, bool strip_leading, bool invert, bool print_hex) {
int len = DemodBufferLen; size_t len = DemodBufferLen;
if (len < 1) { if (len == 0) {
PrintAndLogEx(INFO, "(printDemodBuff) no bits found in demod buffer"); PrintAndLogEx(WARNING, "Demodbuffer is empty");
return; 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) { int CmdPrintDemodBuff(const char *Cmd) {
bool hexMode = false; bool print_hex = false;
bool errors = false; bool errors = false;
bool lstrip = false; bool lstrip = false;
bool invert = false; bool invert = false;
@ -422,7 +467,7 @@ int CmdPrintDemodBuff(const char *Cmd) {
case 'h': case 'h':
return usage_data_printdemodbuf(); return usage_data_printdemodbuf();
case 'x': case 'x':
hexMode = true; print_hex = true;
cmdp++; cmdp++;
break; break;
case 'o': case 'o':
@ -452,45 +497,7 @@ int CmdPrintDemodBuff(const char *Cmd) {
//Validations //Validations
if (errors) return usage_data_printdemodbuf(); if (errors) return usage_data_printdemodbuf();
if (DemodBufferLen == 0) { return printDemodBuff(offset, lstrip, invert, print_hex);
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;
} }
//by marshmellow //by marshmellow
@ -596,7 +603,7 @@ int ASKDemod_ext(int clk, int invert, int maxErr, size_t maxLen, bool amplify, b
else else
PrintAndLogEx(DEBUG, "ASK/Raw - Clock: %d - Decoded bitstream:", clk); PrintAndLogEx(DEBUG, "ASK/Raw - Clock: %d - Decoded bitstream:", clk);
printDemodBuff(); printDemodBuff(0, false, false, false);
} }
uint64_t lo = 0; uint64_t lo = 0;
uint32_t hi = 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); setClockGrid(clk, startIdx + clk * offset / 2);
if (g_debugMode || verbose) { 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)); 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; 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) { if (verbose || g_debugMode) {
PrintAndLogEx(DEBUG, "DEBUG: (FSKrawDemod) Using Clock:%u, invert:%u, fchigh:%u, fclow:%u", rfLen, invert, fchigh, fclow); 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)); PrintAndLogEx(NORMAL, "%s decoded bitstream:", GetFSKType(fchigh, fclow, invert));
printDemodBuff(); printDemodBuff(0, false, invert, false);
} }
goto out; goto out;
} else { } else {
@ -1327,7 +1334,7 @@ int NRZrawDemod(int clk, int invert, int maxErr, bool verbose) {
if (verbose || g_debugMode) { if (verbose || g_debugMode) {
PrintAndLogEx(NORMAL, "NRZ demoded bitstream:"); PrintAndLogEx(NORMAL, "NRZ demoded bitstream:");
// Now output the bitstream to the scrollback by line of 16 bits // Now output the bitstream to the scrollback by line of 16 bits
printDemodBuff(); printDemodBuff(0, false, invert, false);
} }
free(bits); free(bits);
@ -1352,14 +1359,14 @@ static int CmdNRZrawDemod(const char *Cmd) {
} }
// by marshmellow // by marshmellow
// takes 3 arguments - clock, invert, maxErr as integers // takes 3 arguments - clock, invert, max_err as integers
// attempts to demodulate psk only // attempts to demodulate psk only
// prints binary found and saves in demodbuffer for further commands // prints binary found and saves in demodbuffer for further commands
int CmdPSK1rawDemod(const char *Cmd) { int CmdPSK1rawDemod(const char *Cmd) {
char cmdp = tolower(param_getchar(Cmd, 0)); char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 16 || cmdp == 'h') return usage_data_rawdemod_p1(); if (strlen(Cmd) > 16 || cmdp == 'h') return usage_data_rawdemod_p1();
int clk = 0, invert = 0, maxErr = 100; int clk = 0, invert = 0, max_err = 100;
sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr); sscanf(Cmd, "%i %i %i", &clk, &invert, &max_err);
if (clk == 1) { if (clk == 1) {
invert = 1; invert = 1;
clk = 0; clk = 0;
@ -1368,7 +1375,7 @@ int CmdPSK1rawDemod(const char *Cmd) {
PrintAndLogEx(WARNING, "Invalid value for invert: %i", invert); PrintAndLogEx(WARNING, "Invalid value for invert: %i", invert);
return PM3_EINVARG; return PM3_EINVARG;
} }
int ans = PSKDemod(clk, invert, maxErr, true); int ans = PSKDemod(clk, invert, max_err, true);
//output //output
if (ans != PM3_SUCCESS) { if (ans != PM3_SUCCESS) {
if (g_debugMode) PrintAndLogEx(ERR, "Error demoding: %d", ans); if (g_debugMode) PrintAndLogEx(ERR, "Error demoding: %d", ans);
@ -1376,7 +1383,7 @@ int CmdPSK1rawDemod(const char *Cmd) {
} }
PrintAndLogEx(NORMAL, "PSK1 demoded bitstream:"); PrintAndLogEx(NORMAL, "PSK1 demoded bitstream:");
// Now output the bitstream to the scrollback by line of 16 bits // Now output the bitstream to the scrollback by line of 16 bits
printDemodBuff(); printDemodBuff(0, false, invert, false);
return PM3_SUCCESS; return PM3_SUCCESS;
} }
@ -1385,8 +1392,8 @@ int CmdPSK1rawDemod(const char *Cmd) {
static int CmdPSK2rawDemod(const char *Cmd) { static int CmdPSK2rawDemod(const char *Cmd) {
char cmdp = tolower(param_getchar(Cmd, 0)); char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 16 || cmdp == 'h') return usage_data_rawdemod_p2(); if (strlen(Cmd) > 16 || cmdp == 'h') return usage_data_rawdemod_p2();
int clk = 0, invert = 0, maxErr = 100; int clk = 0, invert = 0, max_err = 100;
sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr); sscanf(Cmd, "%i %i %i", &clk, &invert, &max_err);
if (clk == 1) { if (clk == 1) {
invert = 1; invert = 1;
clk = 0; clk = 0;
@ -1395,7 +1402,7 @@ static int CmdPSK2rawDemod(const char *Cmd) {
PrintAndLogEx(WARNING, "Invalid value for invert: %i", invert); PrintAndLogEx(WARNING, "Invalid value for invert: %i", invert);
return PM3_EINVARG; return PM3_EINVARG;
} }
int ans = PSKDemod(clk, invert, maxErr, true); int ans = PSKDemod(clk, invert, max_err, true);
if (ans != PM3_SUCCESS) { if (ans != PM3_SUCCESS) {
if (g_debugMode) PrintAndLogEx(ERR, "Error demoding: %d", ans); if (g_debugMode) PrintAndLogEx(ERR, "Error demoding: %d", ans);
return PM3_ESOFT; return PM3_ESOFT;
@ -1403,7 +1410,7 @@ static int CmdPSK2rawDemod(const char *Cmd) {
psk1TOpsk2(DemodBuffer, DemodBufferLen); psk1TOpsk2(DemodBuffer, DemodBufferLen);
PrintAndLogEx(NORMAL, "PSK2 demoded bitstream:"); PrintAndLogEx(NORMAL, "PSK2 demoded bitstream:");
// Now output the bitstream to the scrollback by line of 16 bits // Now output the bitstream to the scrollback by line of 16 bits
printDemodBuff(); printDemodBuff(0, false, invert, false);
return PM3_SUCCESS; return PM3_SUCCESS;
} }

View file

@ -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 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); void setDemodBuff(uint8_t *buff, size_t size, size_t start_idx);
bool getDemodBuff(uint8_t *buff, size_t *size); bool getDemodBuff(uint8_t *buff, size_t *size);
void save_restoreDB(uint8_t saveOpt);// option '1' to save DemodBuffer any other to restore void save_restoreDB(uint8_t saveOpt);// option '1' to save DemodBuffer any other to restore

View file

@ -330,8 +330,10 @@ int demodAWID(bool verbose) {
free(bits); free(bits);
PrintAndLogEx(DEBUG, "DEBUG: AWID idx: %d, Len: %zu Printing Demod Buffer:", idx, size); PrintAndLogEx(DEBUG, "DEBUG: AWID idx: %d, Len: %zu Printing Demod Buffer:", idx, size);
if (g_debugMode) if (g_debugMode) {
printDemodBuff(); printDemodBuff(0, false, false, true);
printDemodBuff(0, false, false, false);
}
return PM3_SUCCESS; return PM3_SUCCESS;
} }

View file

@ -318,8 +318,9 @@ int AskEm410xDecode(bool verbose, uint32_t *hi, uint64_t *lo) {
setClockGrid(g_DemodClock, g_DemodStartIdx + ((idx + 1)*g_DemodClock)); setClockGrid(g_DemodClock, g_DemodStartIdx + ((idx + 1)*g_DemodClock));
PrintAndLogEx(DEBUG, "DEBUG: Em410x idx: %zu, Len: %zu, Printing Demod Buffer:", idx, size); PrintAndLogEx(DEBUG, "DEBUG: Em410x idx: %zu, Len: %zu, Printing Demod Buffer:", idx, size);
if (g_debugMode) if (g_debugMode) {
printDemodBuff(); printDemodBuff(0, false, false, true);
}
if (verbose) if (verbose)
printEM410x(*hi, *lo); printEM410x(*hi, *lo);

View file

@ -140,8 +140,10 @@ int demodHID(bool verbose) {
HIDTryUnpack(&packed, false); HIDTryUnpack(&packed, false);
PrintAndLogEx(DEBUG, "DEBUG: HID idx: %d, Len: %zu, Printing Demod Buffer: ", idx, size); PrintAndLogEx(DEBUG, "DEBUG: HID idx: %d, Len: %zu, Printing Demod Buffer: ", idx, size);
if (g_debugMode) if (g_debugMode) {
printDemodBuff(); printDemodBuff(0, false, false, true);
printDemodBuff(0, false, false, false);
}
return PM3_SUCCESS; return PM3_SUCCESS;
} }

View file

@ -268,7 +268,7 @@ int demodIndalaEx(int clk, int invert, int maxErr, bool verbose) {
if (g_debugMode) { if (g_debugMode) {
PrintAndLogEx(DEBUG, "DEBUG: Indala - printing demodbuffer"); PrintAndLogEx(DEBUG, "DEBUG: Indala - printing demodbuffer");
printDemodBuff(); printDemodBuff(0, false, false, false);
} }
return PM3_SUCCESS; return PM3_SUCCESS;
} }

View file

@ -185,7 +185,8 @@ int demodIOProx(bool verbose) {
PrintAndLogEx(DEBUG, "DEBUG: Error - IO prox crc failed"); PrintAndLogEx(DEBUG, "DEBUG: Error - IO prox crc failed");
PrintAndLogEx(DEBUG, "DEBUG: IO prox idx: %d, Len: %zu, Printing demod buffer:", idx, size); 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; return retval;
} }

View file

@ -192,8 +192,9 @@ int demodParadox(bool verbose) {
); );
PrintAndLogEx(DEBUG, "DEBUG: Paradox idx: %d, len: %zu, Printing Demod Buffer:", idx, size); PrintAndLogEx(DEBUG, "DEBUG: Paradox idx: %d, len: %zu, Printing Demod Buffer:", idx, size);
if (g_debugMode) if (g_debugMode) {
printDemodBuff(); printDemodBuff(0, false, false, false);
}
return PM3_SUCCESS; return PM3_SUCCESS;
} }

View file

@ -203,8 +203,9 @@ int demodPyramid(bool verbose) {
); );
PrintAndLogEx(DEBUG, "DEBUG: Pyramid: idx: %d, Len: %d, Printing Demod Buffer:", idx, 128); PrintAndLogEx(DEBUG, "DEBUG: Pyramid: idx: %d, Len: %d, Printing Demod Buffer:", idx, 128);
if (g_debugMode) if (g_debugMode) {
printDemodBuff(); printDemodBuff(0, false, false, false);
}
return PM3_SUCCESS; return PM3_SUCCESS;
} }