From 4dea6064681507688486fc4b0fb3cc6e8638ae2b Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Mon, 14 Oct 2024 20:59:33 +0200 Subject: [PATCH] saveFileJSONex: reuse saveFileJSONrootEx --- client/src/cmdhffido.c | 8 ++++---- client/src/fileutils.c | 29 ++++++++--------------------- client/src/fileutils.h | 2 +- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/client/src/cmdhffido.c b/client/src/cmdhffido.c index b54a5a712..a39b1157d 100644 --- a/client/src/cmdhffido.c +++ b/client/src/cmdhffido.c @@ -398,7 +398,7 @@ static int CmdHFFidoRegister(const char *cmd) { JsonSaveBufAsHexCompact(root, "KeyHandle", &buf[67], keyHandleLen); JsonSaveBufAsHexCompact(root, "DER", &buf[67 + keyHandleLen], derLen); - res = saveFileJSONrootEx(filename, root, JSON_INDENT(2), verbose, true); + res = saveFileJSONrootEx(filename, root, JSON_INDENT(2), verbose, true, spDump); (void)res; } json_decref(root); @@ -662,7 +662,7 @@ static int CmdHFFidoAuthenticate(const char *cmd) { JsonSaveBufAsHexCompact(root, "KeyHandle", &data[65], keyHandleLen); JsonSaveInt(root, "Counter", cntr); - res = saveFileJSONrootEx(filename, root, JSON_INDENT(2), verbose, true); + res = saveFileJSONrootEx(filename, root, JSON_INDENT(2), verbose, true, spDump); (void)res; } json_decref(root); @@ -783,7 +783,7 @@ static int CmdHFFido2MakeCredential(const char *cmd) { // parse returned cbor FIDO2MakeCredentionalParseRes(root, &buf[1], len - 1, verbose, verbose2, showCBOR, showDERTLV); - res = saveFileJSONrootEx(filename, root, JSON_INDENT(2), verbose, true); + res = saveFileJSONrootEx(filename, root, JSON_INDENT(2), verbose, true, spDump); (void)res; json_decref(root); return res; @@ -903,7 +903,7 @@ static int CmdHFFido2GetAssertion(const char *cmd) { // parse returned cbor FIDO2GetAssertionParseRes(root, &buf[1], len - 1, verbose, verbose2, showCBOR); - res = saveFileJSONrootEx(filename, root, JSON_INDENT(2), verbose, true); + res = saveFileJSONrootEx(filename, root, JSON_INDENT(2), verbose, true, spDump); (void)res; json_decref(root); return res; diff --git a/client/src/fileutils.c b/client/src/fileutils.c index d41f03e47..91369af43 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -771,39 +771,26 @@ int prepareJSON(json_t *root, JSONFileType ftype, uint8_t *data, size_t datalen, int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen, void (*callback)(json_t *)) { return saveFileJSONex(preferredName, ftype, data, datalen, true, callback, spDump); } + int saveFileJSONex(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *), savePaths_t e_save_path) { int retval = PM3_SUCCESS; - char *fn = newfilenamemcopyEx(preferredName, ".json", e_save_path); - if (fn == NULL) { - return PM3_EMALLOC; - } json_t *root = json_object(); retval = prepareJSON(root, ftype, data, datalen, verbose, callback); if (retval != PM3_SUCCESS) { return retval; } - - if (json_dump_file(root, fn, JSON_INDENT(2))) { - PrintAndLogEx(FAILED, "error, can't save the file `" _YELLOW_("%s") "`", fn); - retval = PM3_ESOFT; - free(fn); - goto out; - } - if (verbose) { - PrintAndLogEx(SUCCESS, "Saved to json file `" _YELLOW_("%s") "`", fn); - } - free(fn); - -out: + retval = saveFileJSONrootEx(preferredName, root, JSON_INDENT(2), verbose, false, e_save_path); json_decref(root); return retval; } + int saveFileJSONroot(const char *preferredName, void *root, size_t flags, bool verbose) { - return saveFileJSONrootEx(preferredName, root, flags, verbose, false); + return saveFileJSONrootEx(preferredName, root, flags, verbose, false, spDump); } -int saveFileJSONrootEx(const char *preferredName, const void *root, size_t flags, bool verbose, bool overwrite) { + +int saveFileJSONrootEx(const char *preferredName, const void *root, size_t flags, bool verbose, bool overwrite, savePaths_t e_save_path) { if (root == NULL) return PM3_EINVARG; @@ -811,7 +798,7 @@ int saveFileJSONrootEx(const char *preferredName, const void *root, size_t flags if (overwrite) filename = filenamemcopy(preferredName, ".json"); else - filename = newfilenamemcopyEx(preferredName, ".json", spDump); + filename = newfilenamemcopyEx(preferredName, ".json", e_save_path); if (filename == NULL) return PM3_EMALLOC; @@ -820,7 +807,7 @@ int saveFileJSONrootEx(const char *preferredName, const void *root, size_t flags if (res == 0) { if (verbose) { - PrintAndLogEx(SUCCESS, "Saved to json file " _YELLOW_("%s"), filename); + PrintAndLogEx(SUCCESS, "Saved to json file `" _YELLOW_("%s") "`", filename); } free(filename); return PM3_SUCCESS; diff --git a/client/src/fileutils.h b/client/src/fileutils.h index 3b0b4f044..f96eaa44d 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -140,7 +140,7 @@ int saveFile(const char *preferredName, const char *suffix, const void *data, si int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen, void (*callback)(json_t *)); int saveFileJSONex(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *), savePaths_t e_save_path); int saveFileJSONroot(const char *preferredName, void *root, size_t flags, bool verbose); -int saveFileJSONrootEx(const char *preferredName, const void *root, size_t flags, bool verbose, bool overwrite); +int saveFileJSONrootEx(const char *preferredName, const void *root, size_t flags, bool verbose, bool overwrite, savePaths_t e_save_path); int prepareJSON(json_t *root, JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *)); /** STUB * @brief Utility function to save WAVE data to a file. This method takes a preferred name, but if that