From 53808f2c7f134eb205819c432e3e42912c3a1f07 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 31 Dec 2022 23:07:54 +0100 Subject: [PATCH] sanity checks and style --- client/src/fileutils.c | 113 +++++++++++++++++++++++++++++++---------- client/src/fileutils.h | 4 +- 2 files changed, 88 insertions(+), 29 deletions(-) diff --git a/client/src/fileutils.c b/client/src/fileutils.c index a2f8e4abe..22cf3ad5e 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -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 ((Path == NULL) && (g_session.defaultPaths[pathIndex] != NULL)) { + + if ((path == NULL) && (g_session.defaultPaths[pathIndex] != NULL)) { free(g_session.defaultPaths[pathIndex]); g_session.defaultPaths[pathIndex] = NULL; } - if (Path != NULL) { - g_session.defaultPaths[pathIndex] = (char *)realloc(g_session.defaultPaths[pathIndex], strlen(Path) + 1); - strcpy(g_session.defaultPaths[pathIndex], Path); + if (path == NULL) { + return false; } + + 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 false; @@ -234,47 +239,70 @@ bool setDefaultPath(savePaths_t pathIndex, const char *Path) { static char *filenamemcopy(const char *preferredName, const char *suffix) { if (preferredName == NULL) return NULL; if (suffix == NULL) return NULL; + char *fileName = (char *) calloc(strlen(preferredName) + strlen(suffix) + 1, sizeof(uint8_t)); - if (fileName == NULL) + if (fileName == NULL) { return NULL; + } + strcpy(fileName, preferredName); - if (str_endswith(fileName, suffix)) + if (str_endswith(fileName, suffix)) { return fileName; + } + strcat(fileName, suffix); 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) { - if (preferredName == NULL) return NULL; - if (suffix == NULL) return NULL; + if (preferredName == NULL || suffix == NULL) { + return NULL; + } uint16_t p_namelen = strlen(preferredName); if (str_endswith(preferredName, suffix)) p_namelen -= strlen(suffix); - const size_t fileNameLen = p_namelen + strlen(suffix) + 1 + 10; - const size_t fileNameSize = fileNameLen * sizeof(uint8_t); + // 10: room for filenum to ensure new filename + 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) { return NULL; } - int num = 1; - snprintf(fileName, fileNameSize, "%.*s%s", p_namelen, preferredName, suffix); + + snprintf(fileName, len, "%.*s%s", p_namelen, preferredName, suffix); 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++; } + + PrintAndLogEx(INFO, "FILE PATH: %s", fileName); return fileName; } 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); - if (fileName == NULL) return PM3_EMALLOC; + if (fileName == NULL) { + return PM3_EMALLOC; + } /* 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) { - if (data == NULL) return PM3_EINVARG; + if (data == NULL || datalen == 0) { + return PM3_EINVARG; + } + char *fileName = newfilenamemcopy(preferredName, ".eml"); - if (fileName == NULL) return PM3_EMALLOC; + if (fileName == NULL) { + return PM3_EMALLOC; + } int retval = PM3_SUCCESS; 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 *)) { - if (data == NULL) return PM3_EINVARG; + if (data == NULL || datalen == 0) { + return PM3_EINVARG; + } char *fileName = newfilenamemcopy(preferredName, ".json"); - if (fileName == NULL) return PM3_EMALLOC; + if (fileName == NULL) { + return PM3_EMALLOC; + } 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) { - if (data == NULL) return PM3_EINVARG; + if (data == NULL || datalen == 0) { + return PM3_EINVARG; + } + char *fileName = newfilenamemcopy(preferredName, ".wav"); - if (fileName == NULL) return PM3_EMALLOC; + if (fileName == NULL) { + return PM3_EMALLOC; + } + int retval = PM3_SUCCESS; struct wave_info_t wave_info = { @@ -731,11 +774,14 @@ int saveFileWAVE(const char *preferredName, const int *data, size_t datalen) { retval = PM3_EFILE; goto out; } + fwrite(&wave_info, sizeof(wave_info), 1, wave_file); + for (int i = 0; i < datalen; i++) { uint8_t sample = data[i] + 128; fwrite(&sample, 1, 1, wave_file); } + fclose(wave_file); 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) { - if (data == NULL) return PM3_EINVARG; + if (data == NULL || datalen == 0) { + return PM3_EINVARG; + } + char *fileName = newfilenamemcopy(preferredName, ".pm3"); - if (fileName == NULL) return PM3_EMALLOC; + if (fileName == NULL) { + return PM3_EMALLOC; + } int retval = PM3_SUCCESS; @@ -760,8 +811,9 @@ int saveFilePM3(const char *preferredName, int *data, size_t datalen) { goto out; } - for (uint32_t i = 0; i < datalen; i++) + for (uint32_t i = 0; i < datalen; i++) { fprintf(f, "%d\n", data[i]); + } fflush(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(RESOURCES_SUBDIR, pm3dir) == 0))) { char *path = calloc(strlen(exec_path) + strlen(pm3dir) + strlen(filename) + 1, sizeof(char)); - if (path == NULL) + if (path == NULL) { goto out; + } + strcpy(path, exec_path); strcat(path, pm3dir); strcat(path, filename); + if ((g_debugMode == 2) && (!silent)) { PrintAndLogEx(INFO, "Searching %s", path); } + if (fileExists(path)) { free(filename); *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) { - if (n == 0) { + + if (d == NULL || n == 0) { PrintAndLogEx(INFO, "No data to save. Skipping..."); return PM3_EINVARG; } diff --git a/client/src/fileutils.h b/client/src/fileutils.h index d237b0fe7..fc5d274aa 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -60,7 +60,9 @@ typedef enum { int fileExists(const char *filename); //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);