chg: 'trace save' better checks

chg: 'trace load' closing filehandles..
This commit is contained in:
iceman1001 2018-03-18 21:47:16 +01:00
parent c30e282f5e
commit 52a0acd7f1

View file

@ -12,7 +12,7 @@
static int CmdHelp(const char *Cmd);
// trace pointer
uint8_t *trace;
static uint8_t *trace;
long traceLen = 0;
bool preRDV40 = true;
@ -474,6 +474,7 @@ int CmdTraceList(const char *Cmd) {
uint16_t tracepos = 0;
// reserv some space.
if (!trace)
trace = malloc(USB_CMD_DATA_SIZE);
if ( isOnline ) {
@ -541,7 +542,7 @@ int CmdTraceLoad(const char *Cmd) {
param_getstr(Cmd, 0, filename, sizeof(filename));
if ((f = fopen(filename,"rb")) == NULL) {
PrintAndLog("Could not open file %s", filename);
PrintAndLogEx(FAILED, "Could not open file %s", filename);
return 0;
}
@ -551,7 +552,7 @@ int CmdTraceLoad(const char *Cmd) {
fseek(f, 0, SEEK_SET);
if (fsize < 0) {
PrintAndLogDevice(WARNING, "error, when getting filesize");
PrintAndLogEx(WARNING, "error, when getting filesize");
fclose(f);
return 3;
}
@ -561,7 +562,7 @@ int CmdTraceLoad(const char *Cmd) {
// or we just skip this limit at all
bytes_read = fread(buf, 1, 2, f);
if (bytes_read != 2) {
PrintAndLog("File reading error.");
PrintAndLogEx(FAILED, "File reading error.");
fclose(f);
return 1;
}
@ -576,20 +577,28 @@ int CmdTraceLoad(const char *Cmd) {
}
if (trace == NULL) {
PrintAndLog("Cannot allocate memory for trace");
PrintAndLogEx(FAILED, "Cannot allocate memory for trace");
fclose(f);
return 2;
}
bytes_read = fread(trace, 1, traceLen, f);
if (bytes_read != traceLen) {
PrintAndLog("File reading error.");
PrintAndLogEx(FAILED, "File reading error.");
fclose(f);
return 1;
}
fclose(f);
return 0;
}
int CmdTraceSave(const char *Cmd) {
if (traceLen == 0 ) {
PrintAndLogEx(WARNING, "trace is empty, exiting...");
return 0;
}
FILE *f = NULL;
uint8_t buf[2] = {0x01, 0xCE};
char filename[FILE_PATH_SIZE];
@ -599,17 +608,18 @@ int CmdTraceSave(const char *Cmd) {
param_getstr(Cmd, 0, filename, sizeof(filename));
if ((f = fopen(filename, "wb")) == NULL) {
PrintAndLog("Could not create file %s", filename);
PrintAndLogEx(FAILED, "Could not create file %s", filename);
return 1;
}
// 40kb bigbuffer limit
if ( 40000 <= traceLen ){
if ( traceLen <= 40000 ) {
num_to_bytes(traceLen, 2, buf);
}
fwrite(buf, 1, 2, f);
fwrite(trace, 1, traceLen, f);
PrintAndLog("Recorded Activity (TraceLen = %d bytes) written to file %s", traceLen, filename);
fclose(f);
PrintAndLogEx(SUCCESS, "Recorded Activity (TraceLen = %d bytes) written to file %s", traceLen, filename);
return 0;
}