From 048a08ec3449c2713140395c299eae5b4ce55cf7 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Wed, 25 Sep 2019 22:01:51 +0200 Subject: [PATCH] free up some stack --- client/graph.c | 103 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 81 insertions(+), 22 deletions(-) diff --git a/client/graph.c b/client/graph.c index fc0ceab2d..094da3505 100644 --- a/client/graph.c +++ b/client/graph.c @@ -8,13 +8,14 @@ // Graph utilities //----------------------------------------------------------------------------- #include "graph.h" - +#include #include #include "ui.h" #include "util.h" //param_get32ex #include "lfdemod.h" #include "cmddata.h" //for g_debugmode + int GraphBuffer[MAX_GRAPH_TRACE_LEN]; size_t GraphTraceLen; int s_Buff[MAX_GRAPH_TRACE_LEN]; @@ -101,6 +102,7 @@ bool HasGraphData(void) { } return true; } + bool isGraphBitstream(void) { // convert to bitstream if necessary for (int i = 0; i < GraphTraceLen; i++) { @@ -110,9 +112,11 @@ bool isGraphBitstream(void) { } return true; } + void convertGraphFromBitstream() { convertGraphFromBitstreamEx(1, 0); } + void convertGraphFromBitstreamEx(int hi, int low) { for (int i = 0; i < GraphTraceLen; i++) { if (GraphBuffer[i] == hi) @@ -122,29 +126,42 @@ void convertGraphFromBitstreamEx(int hi, int low) { else GraphBuffer[i] = 0; } - uint8_t bits[GraphTraceLen]; - memset(bits, 0, sizeof(bits)); + + uint8_t *bits = calloc(GraphTraceLen, sizeof(uint8_t)); + if (bits == NULL) { + PrintAndLogEx(DEBUG, "ERR: convertGraphFromBitstreamEx, failed to allocate memory"); + return; + } + size_t size = getFromGraphBuf(bits); // set signal properties low/high/mean/amplitude and is_noise detection computeSignalProperties(bits, size); + free(bits); RepaintGraphWindow(); } // Get or auto-detect ask clock rate int GetAskClock(const char *str, bool printAns) { if (getSignalProperties()->isnoise) - return false; + return -1; int clock1 = param_get32ex(str, 0, 0, 10); if (clock1 > 0) return clock1; // Auto-detect clock - uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0}; + + uint8_t *bits = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t)); + if (bits == NULL) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); + return -1; + } + size_t size = getFromGraphBuf(bits); if (size == 0) { PrintAndLogEx(WARNING, "Failed to copy from graphbuffer"); + free(bits); return -1; } @@ -162,20 +179,29 @@ int GetAskClock(const char *str, bool printAns) { if (printAns || g_debugMode) PrintAndLogEx(SUCCESS, "Auto-detected clock rate: %d, Best Starting Position: %d", clock1, idx); + free(bits); return clock1; } uint8_t GetPskCarrier(const char *str, bool printAns) { if (getSignalProperties()->isnoise) - return false; + return -1; uint8_t carrier = 0; - uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0}; + + uint8_t *bits = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t)); + if (bits == NULL) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); + return -1; + } + size_t size = getFromGraphBuf(bits); if (size == 0) { PrintAndLogEx(WARNING, "Failed to copy from graphbuffer"); - return 0; + free(bits); + return -1; } + uint16_t fc = countFC(bits, size, false); carrier = fc & 0xFF; if (carrier != 2 && carrier != 4 && carrier != 8) return 0; @@ -183,6 +209,8 @@ uint8_t GetPskCarrier(const char *str, bool printAns) { // Only print this message if we're not looping something if (printAns) PrintAndLogEx(SUCCESS, "Auto-detected PSK carrier rate: %d", carrier); + + free(bits); return carrier; } @@ -196,20 +224,28 @@ int GetPskClock(const char *str, bool printAns) { return clock1; // Auto-detect clock - uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0}; - size_t size = getFromGraphBuf(grph); - if (size == 0) { - PrintAndLogEx(WARNING, "Failed to copy from graphbuffer"); + uint8_t *bits = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t)); + if (bits == NULL) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); return -1; } + + size_t size = getFromGraphBuf(bits); + if (size == 0) { + PrintAndLogEx(WARNING, "Failed to copy from graphbuffer"); + free(bits); + return -1; + } + size_t firstPhaseShiftLoc = 0; uint8_t curPhase = 0, fc = 0; - clock1 = DetectPSKClock(grph, size, 0, &firstPhaseShiftLoc, &curPhase, &fc); + clock1 = DetectPSKClock(bits, size, 0, &firstPhaseShiftLoc, &curPhase, &fc); setClockGrid(clock1, firstPhaseShiftLoc); // Only print this message if we're not looping something if (printAns) PrintAndLogEx(SUCCESS, "Auto-detected clock rate: %d", clock1); + free(bits); return clock1; } @@ -223,21 +259,30 @@ int GetNrzClock(const char *str, bool printAns) { return clock1; // Auto-detect clock - uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0}; - size_t size = getFromGraphBuf(grph); - if (size == 0) { - PrintAndLogEx(WARNING, "Failed to copy from graphbuffer"); + uint8_t *bits = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t)); + if (bits == NULL) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); return -1; } + + size_t size = getFromGraphBuf(bits); + if (size == 0) { + PrintAndLogEx(WARNING, "Failed to copy from graphbuffer"); + free(bits); + return -1; + } + size_t clkStartIdx = 0; - clock1 = DetectNRZClock(grph, size, 0, &clkStartIdx); + clock1 = DetectNRZClock(bits, size, 0, &clkStartIdx); setClockGrid(clock1, clkStartIdx); // Only print this message if we're not looping something if (printAns) PrintAndLogEx(SUCCESS, "Auto-detected clock rate: %d", clock1); + free(bits); return clock1; } + //by marshmellow //attempt to detect the field clock and bit clock for FSK int GetFskClock(const char *str, bool printAns) { @@ -249,12 +294,13 @@ int GetFskClock(const char *str, bool printAns) { uint8_t fc1 = 0, fc2 = 0, rf1 = 0; int firstClockEdge = 0; - if (!fskClocks(&fc1, &fc2, &rf1, &firstClockEdge)) + if (fskClocks(&fc1, &fc2, &rf1, &firstClockEdge) == false) return 0; if ((fc1 == 10 && fc2 == 8) || (fc1 == 8 && fc2 == 5)) { if (printAns) PrintAndLogEx(SUCCESS, "Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1); + setClockGrid(rf1, firstClockEdge); return rf1; } @@ -263,27 +309,40 @@ int GetFskClock(const char *str, bool printAns) { PrintAndLogEx(DEBUG, "Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1); return 0; } + bool fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, int *firstClockEdge) { if (getSignalProperties()->isnoise) return false; - uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0}; - size_t size = getFromGraphBuf(bits); - if (size == 0) + uint8_t *bits = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t)); + if (bits == NULL) { + PrintAndLogEx(WARNING, "Failed to allocate memory"); return false; + } + + size_t size = getFromGraphBuf(bits); + if (size == 0) { + free(bits); + return false; + } uint16_t ans = countFC(bits, size, true); if (ans == 0) { PrintAndLogEx(DEBUG, "DEBUG: No data found"); + free(bits); return false; } *fc1 = (ans >> 8) & 0xFF; *fc2 = ans & 0xFF; *rf1 = detectFSKClk(bits, size, *fc1, *fc2, firstClockEdge); + + free(bits); + if (*rf1 == 0) { PrintAndLogEx(DEBUG, "DEBUG: Clock detect error"); + return false; } return true;