mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-14 02:27:26 -07:00
sanity checks and style
This commit is contained in:
parent
d6af860136
commit
53808f2c7f
2 changed files with 88 additions and 29 deletions
|
@ -214,18 +214,23 @@ bool create_path(const char *dirname) {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
bool setDefaultPath(savePaths_t pathIndex, const char *Path) {
|
bool setDefaultPath(savePaths_t pathIndex, const char *path) {
|
||||||
|
|
||||||
if (pathIndex < spItemCount) {
|
if (pathIndex < spItemCount) {
|
||||||
if ((Path == NULL) && (g_session.defaultPaths[pathIndex] != NULL)) {
|
|
||||||
|
if ((path == NULL) && (g_session.defaultPaths[pathIndex] != NULL)) {
|
||||||
free(g_session.defaultPaths[pathIndex]);
|
free(g_session.defaultPaths[pathIndex]);
|
||||||
g_session.defaultPaths[pathIndex] = NULL;
|
g_session.defaultPaths[pathIndex] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Path != NULL) {
|
if (path == NULL) {
|
||||||
g_session.defaultPaths[pathIndex] = (char *)realloc(g_session.defaultPaths[pathIndex], strlen(Path) + 1);
|
return false;
|
||||||
strcpy(g_session.defaultPaths[pathIndex], Path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t len = strlen(path);
|
||||||
|
|
||||||
|
g_session.defaultPaths[pathIndex] = (char *)realloc(g_session.defaultPaths[pathIndex], len + 1);
|
||||||
|
strcpy(g_session.defaultPaths[pathIndex], path);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -234,47 +239,70 @@ bool setDefaultPath(savePaths_t pathIndex, const char *Path) {
|
||||||
static char *filenamemcopy(const char *preferredName, const char *suffix) {
|
static char *filenamemcopy(const char *preferredName, const char *suffix) {
|
||||||
if (preferredName == NULL) return NULL;
|
if (preferredName == NULL) return NULL;
|
||||||
if (suffix == NULL) return NULL;
|
if (suffix == NULL) return NULL;
|
||||||
|
|
||||||
char *fileName = (char *) calloc(strlen(preferredName) + strlen(suffix) + 1, sizeof(uint8_t));
|
char *fileName = (char *) calloc(strlen(preferredName) + strlen(suffix) + 1, sizeof(uint8_t));
|
||||||
if (fileName == NULL)
|
if (fileName == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
strcpy(fileName, preferredName);
|
strcpy(fileName, preferredName);
|
||||||
if (str_endswith(fileName, suffix))
|
if (str_endswith(fileName, suffix)) {
|
||||||
return fileName;
|
return fileName;
|
||||||
|
}
|
||||||
|
|
||||||
strcat(fileName, suffix);
|
strcat(fileName, suffix);
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static size_t path_size(savePaths_t a) {
|
||||||
|
if (a == spItemCount) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return strlen( g_session.defaultPaths[a] );
|
||||||
|
}
|
||||||
|
|
||||||
char *newfilenamemcopy(const char *preferredName, const char *suffix) {
|
char *newfilenamemcopy(const char *preferredName, const char *suffix) {
|
||||||
if (preferredName == NULL) return NULL;
|
if (preferredName == NULL || suffix == NULL) {
|
||||||
if (suffix == NULL) return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t p_namelen = strlen(preferredName);
|
uint16_t p_namelen = strlen(preferredName);
|
||||||
if (str_endswith(preferredName, suffix))
|
if (str_endswith(preferredName, suffix))
|
||||||
p_namelen -= strlen(suffix);
|
p_namelen -= strlen(suffix);
|
||||||
|
|
||||||
const size_t fileNameLen = p_namelen + strlen(suffix) + 1 + 10;
|
// 10: room for filenum to ensure new filename
|
||||||
const size_t fileNameSize = fileNameLen * sizeof(uint8_t);
|
const size_t len = p_namelen + strlen(suffix) + 1 + 10;
|
||||||
|
|
||||||
char *fileName = (char *) calloc(fileNameLen, sizeof(uint8_t)); // 10: room for filenum to ensure new filename
|
int foobar = path_size(spDefault);
|
||||||
|
(void) foobar;
|
||||||
|
|
||||||
|
char *fileName = (char *) calloc(len, sizeof(uint8_t));
|
||||||
if (fileName == NULL) {
|
if (fileName == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int num = 1;
|
int num = 1;
|
||||||
snprintf(fileName, fileNameSize, "%.*s%s", p_namelen, preferredName, suffix);
|
|
||||||
|
snprintf(fileName, len, "%.*s%s", p_namelen, preferredName, suffix);
|
||||||
while (fileExists(fileName)) {
|
while (fileExists(fileName)) {
|
||||||
snprintf(fileName, fileNameSize, "%.*s-%d%s", p_namelen, preferredName, num, suffix);
|
snprintf(fileName, len, "%.*s-%d%s", p_namelen, preferredName, num, suffix);
|
||||||
num++;
|
num++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PrintAndLogEx(INFO, "FILE PATH: %s", fileName);
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
int saveFile(const char *preferredName, const char *suffix, const void *data, size_t datalen) {
|
int saveFile(const char *preferredName, const char *suffix, const void *data, size_t datalen) {
|
||||||
|
|
||||||
if (data == NULL) return PM3_EINVARG;
|
if (data == NULL || datalen == 0) {
|
||||||
|
return PM3_EINVARG;
|
||||||
|
}
|
||||||
|
|
||||||
char *fileName = newfilenamemcopy(preferredName, suffix);
|
char *fileName = newfilenamemcopy(preferredName, suffix);
|
||||||
if (fileName == NULL) return PM3_EMALLOC;
|
if (fileName == NULL) {
|
||||||
|
return PM3_EMALLOC;
|
||||||
|
}
|
||||||
|
|
||||||
/* We should have a valid filename now, e.g. dumpdata-3.bin */
|
/* We should have a valid filename now, e.g. dumpdata-3.bin */
|
||||||
|
|
||||||
|
@ -295,9 +323,14 @@ int saveFile(const char *preferredName, const char *suffix, const void *data, si
|
||||||
|
|
||||||
int saveFileEML(const char *preferredName, uint8_t *data, size_t datalen, size_t blocksize) {
|
int saveFileEML(const char *preferredName, uint8_t *data, size_t datalen, size_t blocksize) {
|
||||||
|
|
||||||
if (data == NULL) return PM3_EINVARG;
|
if (data == NULL || datalen == 0) {
|
||||||
|
return PM3_EINVARG;
|
||||||
|
}
|
||||||
|
|
||||||
char *fileName = newfilenamemcopy(preferredName, ".eml");
|
char *fileName = newfilenamemcopy(preferredName, ".eml");
|
||||||
if (fileName == NULL) return PM3_EMALLOC;
|
if (fileName == NULL) {
|
||||||
|
return PM3_EMALLOC;
|
||||||
|
}
|
||||||
|
|
||||||
int retval = PM3_SUCCESS;
|
int retval = PM3_SUCCESS;
|
||||||
int blocks = datalen / blocksize;
|
int blocks = datalen / blocksize;
|
||||||
|
@ -343,10 +376,14 @@ int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, s
|
||||||
}
|
}
|
||||||
int saveFileJSONex(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *)) {
|
int saveFileJSONex(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *)) {
|
||||||
|
|
||||||
if (data == NULL) return PM3_EINVARG;
|
if (data == NULL || datalen == 0) {
|
||||||
|
return PM3_EINVARG;
|
||||||
|
}
|
||||||
|
|
||||||
char *fileName = newfilenamemcopy(preferredName, ".json");
|
char *fileName = newfilenamemcopy(preferredName, ".json");
|
||||||
if (fileName == NULL) return PM3_EMALLOC;
|
if (fileName == NULL) {
|
||||||
|
return PM3_EMALLOC;
|
||||||
|
}
|
||||||
|
|
||||||
int retval = PM3_SUCCESS;
|
int retval = PM3_SUCCESS;
|
||||||
|
|
||||||
|
@ -704,9 +741,15 @@ int saveFileJSONrootEx(const char *preferredName, void *root, size_t flags, bool
|
||||||
|
|
||||||
int saveFileWAVE(const char *preferredName, const int *data, size_t datalen) {
|
int saveFileWAVE(const char *preferredName, const int *data, size_t datalen) {
|
||||||
|
|
||||||
if (data == NULL) return PM3_EINVARG;
|
if (data == NULL || datalen == 0) {
|
||||||
|
return PM3_EINVARG;
|
||||||
|
}
|
||||||
|
|
||||||
char *fileName = newfilenamemcopy(preferredName, ".wav");
|
char *fileName = newfilenamemcopy(preferredName, ".wav");
|
||||||
if (fileName == NULL) return PM3_EMALLOC;
|
if (fileName == NULL) {
|
||||||
|
return PM3_EMALLOC;
|
||||||
|
}
|
||||||
|
|
||||||
int retval = PM3_SUCCESS;
|
int retval = PM3_SUCCESS;
|
||||||
|
|
||||||
struct wave_info_t wave_info = {
|
struct wave_info_t wave_info = {
|
||||||
|
@ -731,11 +774,14 @@ int saveFileWAVE(const char *preferredName, const int *data, size_t datalen) {
|
||||||
retval = PM3_EFILE;
|
retval = PM3_EFILE;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
fwrite(&wave_info, sizeof(wave_info), 1, wave_file);
|
fwrite(&wave_info, sizeof(wave_info), 1, wave_file);
|
||||||
|
|
||||||
for (int i = 0; i < datalen; i++) {
|
for (int i = 0; i < datalen; i++) {
|
||||||
uint8_t sample = data[i] + 128;
|
uint8_t sample = data[i] + 128;
|
||||||
fwrite(&sample, 1, 1, wave_file);
|
fwrite(&sample, 1, 1, wave_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(wave_file);
|
fclose(wave_file);
|
||||||
|
|
||||||
PrintAndLogEx(SUCCESS, "saved " _YELLOW_("%zu") " bytes to wave file " _YELLOW_("'%s'"), 2 * datalen, fileName);
|
PrintAndLogEx(SUCCESS, "saved " _YELLOW_("%zu") " bytes to wave file " _YELLOW_("'%s'"), 2 * datalen, fileName);
|
||||||
|
@ -747,9 +793,14 @@ out:
|
||||||
|
|
||||||
int saveFilePM3(const char *preferredName, int *data, size_t datalen) {
|
int saveFilePM3(const char *preferredName, int *data, size_t datalen) {
|
||||||
|
|
||||||
if (data == NULL) return PM3_EINVARG;
|
if (data == NULL || datalen == 0) {
|
||||||
|
return PM3_EINVARG;
|
||||||
|
}
|
||||||
|
|
||||||
char *fileName = newfilenamemcopy(preferredName, ".pm3");
|
char *fileName = newfilenamemcopy(preferredName, ".pm3");
|
||||||
if (fileName == NULL) return PM3_EMALLOC;
|
if (fileName == NULL) {
|
||||||
|
return PM3_EMALLOC;
|
||||||
|
}
|
||||||
|
|
||||||
int retval = PM3_SUCCESS;
|
int retval = PM3_SUCCESS;
|
||||||
|
|
||||||
|
@ -760,8 +811,9 @@ int saveFilePM3(const char *preferredName, int *data, size_t datalen) {
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint32_t i = 0; i < datalen; i++)
|
for (uint32_t i = 0; i < datalen; i++) {
|
||||||
fprintf(f, "%d\n", data[i]);
|
fprintf(f, "%d\n", data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
fflush(f);
|
fflush(f);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
@ -1862,14 +1914,18 @@ static int searchFinalFile(char **foundpath, const char *pm3dir, const char *sea
|
||||||
(strcmp(PYTHON_SCRIPTS_SUBDIR, pm3dir) == 0) ||
|
(strcmp(PYTHON_SCRIPTS_SUBDIR, pm3dir) == 0) ||
|
||||||
(strcmp(RESOURCES_SUBDIR, pm3dir) == 0))) {
|
(strcmp(RESOURCES_SUBDIR, pm3dir) == 0))) {
|
||||||
char *path = calloc(strlen(exec_path) + strlen(pm3dir) + strlen(filename) + 1, sizeof(char));
|
char *path = calloc(strlen(exec_path) + strlen(pm3dir) + strlen(filename) + 1, sizeof(char));
|
||||||
if (path == NULL)
|
if (path == NULL) {
|
||||||
goto out;
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
strcpy(path, exec_path);
|
strcpy(path, exec_path);
|
||||||
strcat(path, pm3dir);
|
strcat(path, pm3dir);
|
||||||
strcat(path, filename);
|
strcat(path, filename);
|
||||||
|
|
||||||
if ((g_debugMode == 2) && (!silent)) {
|
if ((g_debugMode == 2) && (!silent)) {
|
||||||
PrintAndLogEx(INFO, "Searching %s", path);
|
PrintAndLogEx(INFO, "Searching %s", path);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fileExists(path)) {
|
if (fileExists(path)) {
|
||||||
free(filename);
|
free(filename);
|
||||||
*foundpath = path;
|
*foundpath = path;
|
||||||
|
@ -2007,7 +2063,8 @@ int pm3_load_dump(const char *fn, void **pdump, size_t *dumplen, size_t maxdumpl
|
||||||
}
|
}
|
||||||
|
|
||||||
int pm3_save_dump(const char *fn, uint8_t *d, size_t n, JSONFileType jsft, size_t blocksize) {
|
int pm3_save_dump(const char *fn, uint8_t *d, size_t n, JSONFileType jsft, size_t blocksize) {
|
||||||
if (n == 0) {
|
|
||||||
|
if (d == NULL || n == 0) {
|
||||||
PrintAndLogEx(INFO, "No data to save. Skipping...");
|
PrintAndLogEx(INFO, "No data to save. Skipping...");
|
||||||
return PM3_EINVARG;
|
return PM3_EINVARG;
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,9 @@ typedef enum {
|
||||||
|
|
||||||
int fileExists(const char *filename);
|
int fileExists(const char *filename);
|
||||||
//bool create_path(const char *dirname);
|
//bool create_path(const char *dirname);
|
||||||
bool setDefaultPath(savePaths_t pathIndex, const char *Path); // set a path in the path list g_session.defaultPaths
|
|
||||||
|
// set a path in the path list g_session.defaultPaths
|
||||||
|
bool setDefaultPath(savePaths_t pathIndex, const char *path);
|
||||||
|
|
||||||
char *newfilenamemcopy(const char *preferredName, const char *suffix);
|
char *newfilenamemcopy(const char *preferredName, const char *suffix);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue