free up some stack

This commit is contained in:
iceman1001 2019-09-25 22:01:51 +02:00
parent dc16509539
commit 048a08ec34

View file

@ -8,13 +8,14 @@
// Graph utilities // Graph utilities
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include "graph.h" #include "graph.h"
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "ui.h" #include "ui.h"
#include "util.h" //param_get32ex #include "util.h" //param_get32ex
#include "lfdemod.h" #include "lfdemod.h"
#include "cmddata.h" //for g_debugmode #include "cmddata.h" //for g_debugmode
int GraphBuffer[MAX_GRAPH_TRACE_LEN]; int GraphBuffer[MAX_GRAPH_TRACE_LEN];
size_t GraphTraceLen; size_t GraphTraceLen;
int s_Buff[MAX_GRAPH_TRACE_LEN]; int s_Buff[MAX_GRAPH_TRACE_LEN];
@ -101,6 +102,7 @@ bool HasGraphData(void) {
} }
return true; return true;
} }
bool isGraphBitstream(void) { bool isGraphBitstream(void) {
// convert to bitstream if necessary // convert to bitstream if necessary
for (int i = 0; i < GraphTraceLen; i++) { for (int i = 0; i < GraphTraceLen; i++) {
@ -110,9 +112,11 @@ bool isGraphBitstream(void) {
} }
return true; return true;
} }
void convertGraphFromBitstream() { void convertGraphFromBitstream() {
convertGraphFromBitstreamEx(1, 0); convertGraphFromBitstreamEx(1, 0);
} }
void convertGraphFromBitstreamEx(int hi, int low) { void convertGraphFromBitstreamEx(int hi, int low) {
for (int i = 0; i < GraphTraceLen; i++) { for (int i = 0; i < GraphTraceLen; i++) {
if (GraphBuffer[i] == hi) if (GraphBuffer[i] == hi)
@ -122,29 +126,42 @@ void convertGraphFromBitstreamEx(int hi, int low) {
else else
GraphBuffer[i] = 0; 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); size_t size = getFromGraphBuf(bits);
// set signal properties low/high/mean/amplitude and is_noise detection // set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size); computeSignalProperties(bits, size);
free(bits);
RepaintGraphWindow(); RepaintGraphWindow();
} }
// Get or auto-detect ask clock rate // Get or auto-detect ask clock rate
int GetAskClock(const char *str, bool printAns) { int GetAskClock(const char *str, bool printAns) {
if (getSignalProperties()->isnoise) if (getSignalProperties()->isnoise)
return false; return -1;
int clock1 = param_get32ex(str, 0, 0, 10); int clock1 = param_get32ex(str, 0, 0, 10);
if (clock1 > 0) if (clock1 > 0)
return clock1; return clock1;
// Auto-detect clock // 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); size_t size = getFromGraphBuf(bits);
if (size == 0) { if (size == 0) {
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer"); PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
free(bits);
return -1; return -1;
} }
@ -162,20 +179,29 @@ int GetAskClock(const char *str, bool printAns) {
if (printAns || g_debugMode) if (printAns || g_debugMode)
PrintAndLogEx(SUCCESS, "Auto-detected clock rate: %d, Best Starting Position: %d", clock1, idx); PrintAndLogEx(SUCCESS, "Auto-detected clock rate: %d, Best Starting Position: %d", clock1, idx);
free(bits);
return clock1; return clock1;
} }
uint8_t GetPskCarrier(const char *str, bool printAns) { uint8_t GetPskCarrier(const char *str, bool printAns) {
if (getSignalProperties()->isnoise) if (getSignalProperties()->isnoise)
return false; return -1;
uint8_t carrier = 0; 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); size_t size = getFromGraphBuf(bits);
if (size == 0) { if (size == 0) {
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer"); PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
return 0; free(bits);
return -1;
} }
uint16_t fc = countFC(bits, size, false); uint16_t fc = countFC(bits, size, false);
carrier = fc & 0xFF; carrier = fc & 0xFF;
if (carrier != 2 && carrier != 4 && carrier != 8) return 0; 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 // Only print this message if we're not looping something
if (printAns) if (printAns)
PrintAndLogEx(SUCCESS, "Auto-detected PSK carrier rate: %d", carrier); PrintAndLogEx(SUCCESS, "Auto-detected PSK carrier rate: %d", carrier);
free(bits);
return carrier; return carrier;
} }
@ -196,20 +224,28 @@ int GetPskClock(const char *str, bool printAns) {
return clock1; return clock1;
// Auto-detect clock // Auto-detect clock
uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0}; uint8_t *bits = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t));
size_t size = getFromGraphBuf(grph); if (bits == NULL) {
if (size == 0) { PrintAndLogEx(WARNING, "Failed to allocate memory");
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
return -1; 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; size_t firstPhaseShiftLoc = 0;
uint8_t curPhase = 0, fc = 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); setClockGrid(clock1, firstPhaseShiftLoc);
// Only print this message if we're not looping something // Only print this message if we're not looping something
if (printAns) if (printAns)
PrintAndLogEx(SUCCESS, "Auto-detected clock rate: %d", clock1); PrintAndLogEx(SUCCESS, "Auto-detected clock rate: %d", clock1);
free(bits);
return clock1; return clock1;
} }
@ -223,21 +259,30 @@ int GetNrzClock(const char *str, bool printAns) {
return clock1; return clock1;
// Auto-detect clock // Auto-detect clock
uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0}; uint8_t *bits = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t));
size_t size = getFromGraphBuf(grph); if (bits == NULL) {
if (size == 0) { PrintAndLogEx(WARNING, "Failed to allocate memory");
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
return -1; 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; size_t clkStartIdx = 0;
clock1 = DetectNRZClock(grph, size, 0, &clkStartIdx); clock1 = DetectNRZClock(bits, size, 0, &clkStartIdx);
setClockGrid(clock1, clkStartIdx); setClockGrid(clock1, clkStartIdx);
// Only print this message if we're not looping something // Only print this message if we're not looping something
if (printAns) if (printAns)
PrintAndLogEx(SUCCESS, "Auto-detected clock rate: %d", clock1); PrintAndLogEx(SUCCESS, "Auto-detected clock rate: %d", clock1);
free(bits);
return clock1; return clock1;
} }
//by marshmellow //by marshmellow
//attempt to detect the field clock and bit clock for FSK //attempt to detect the field clock and bit clock for FSK
int GetFskClock(const char *str, bool printAns) { 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; uint8_t fc1 = 0, fc2 = 0, rf1 = 0;
int firstClockEdge = 0; int firstClockEdge = 0;
if (!fskClocks(&fc1, &fc2, &rf1, &firstClockEdge)) if (fskClocks(&fc1, &fc2, &rf1, &firstClockEdge) == false)
return 0; return 0;
if ((fc1 == 10 && fc2 == 8) || (fc1 == 8 && fc2 == 5)) { if ((fc1 == 10 && fc2 == 8) || (fc1 == 8 && fc2 == 5)) {
if (printAns) if (printAns)
PrintAndLogEx(SUCCESS, "Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1); PrintAndLogEx(SUCCESS, "Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
setClockGrid(rf1, firstClockEdge); setClockGrid(rf1, firstClockEdge);
return rf1; 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); PrintAndLogEx(DEBUG, "Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
return 0; return 0;
} }
bool fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, int *firstClockEdge) { bool fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, int *firstClockEdge) {
if (getSignalProperties()->isnoise) if (getSignalProperties()->isnoise)
return false; return false;
uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0}; uint8_t *bits = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t));
size_t size = getFromGraphBuf(bits); if (bits == NULL) {
if (size == 0) PrintAndLogEx(WARNING, "Failed to allocate memory");
return false; return false;
}
size_t size = getFromGraphBuf(bits);
if (size == 0) {
free(bits);
return false;
}
uint16_t ans = countFC(bits, size, true); uint16_t ans = countFC(bits, size, true);
if (ans == 0) { if (ans == 0) {
PrintAndLogEx(DEBUG, "DEBUG: No data found"); PrintAndLogEx(DEBUG, "DEBUG: No data found");
free(bits);
return false; return false;
} }
*fc1 = (ans >> 8) & 0xFF; *fc1 = (ans >> 8) & 0xFF;
*fc2 = ans & 0xFF; *fc2 = ans & 0xFF;
*rf1 = detectFSKClk(bits, size, *fc1, *fc2, firstClockEdge); *rf1 = detectFSKClk(bits, size, *fc1, *fc2, firstClockEdge);
free(bits);
if (*rf1 == 0) { if (*rf1 == 0) {
PrintAndLogEx(DEBUG, "DEBUG: Clock detect error"); PrintAndLogEx(DEBUG, "DEBUG: Clock detect error");
return false; return false;
} }
return true; return true;