saveFileJSONex: reuse saveFileJSONrootEx

This commit is contained in:
Philippe Teuwen 2024-10-14 20:59:33 +02:00
commit 4dea606468
3 changed files with 13 additions and 26 deletions

View file

@ -398,7 +398,7 @@ static int CmdHFFidoRegister(const char *cmd) {
JsonSaveBufAsHexCompact(root, "KeyHandle", &buf[67], keyHandleLen); JsonSaveBufAsHexCompact(root, "KeyHandle", &buf[67], keyHandleLen);
JsonSaveBufAsHexCompact(root, "DER", &buf[67 + keyHandleLen], derLen); 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; (void)res;
} }
json_decref(root); json_decref(root);
@ -662,7 +662,7 @@ static int CmdHFFidoAuthenticate(const char *cmd) {
JsonSaveBufAsHexCompact(root, "KeyHandle", &data[65], keyHandleLen); JsonSaveBufAsHexCompact(root, "KeyHandle", &data[65], keyHandleLen);
JsonSaveInt(root, "Counter", cntr); 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; (void)res;
} }
json_decref(root); json_decref(root);
@ -783,7 +783,7 @@ static int CmdHFFido2MakeCredential(const char *cmd) {
// parse returned cbor // parse returned cbor
FIDO2MakeCredentionalParseRes(root, &buf[1], len - 1, verbose, verbose2, showCBOR, showDERTLV); 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; (void)res;
json_decref(root); json_decref(root);
return res; return res;
@ -903,7 +903,7 @@ static int CmdHFFido2GetAssertion(const char *cmd) {
// parse returned cbor // parse returned cbor
FIDO2GetAssertionParseRes(root, &buf[1], len - 1, verbose, verbose2, showCBOR); 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; (void)res;
json_decref(root); json_decref(root);
return res; return res;

View file

@ -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 *)) { 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); 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 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; int retval = PM3_SUCCESS;
char *fn = newfilenamemcopyEx(preferredName, ".json", e_save_path);
if (fn == NULL) {
return PM3_EMALLOC;
}
json_t *root = json_object(); json_t *root = json_object();
retval = prepareJSON(root, ftype, data, datalen, verbose, callback); retval = prepareJSON(root, ftype, data, datalen, verbose, callback);
if (retval != PM3_SUCCESS) { if (retval != PM3_SUCCESS) {
return retval; return retval;
} }
retval = saveFileJSONrootEx(preferredName, root, JSON_INDENT(2), verbose, false, e_save_path);
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:
json_decref(root); json_decref(root);
return retval; return retval;
} }
int saveFileJSONroot(const char *preferredName, void *root, size_t flags, bool verbose) { 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) if (root == NULL)
return PM3_EINVARG; return PM3_EINVARG;
@ -811,7 +798,7 @@ int saveFileJSONrootEx(const char *preferredName, const void *root, size_t flags
if (overwrite) if (overwrite)
filename = filenamemcopy(preferredName, ".json"); filename = filenamemcopy(preferredName, ".json");
else else
filename = newfilenamemcopyEx(preferredName, ".json", spDump); filename = newfilenamemcopyEx(preferredName, ".json", e_save_path);
if (filename == NULL) if (filename == NULL)
return PM3_EMALLOC; return PM3_EMALLOC;
@ -820,7 +807,7 @@ int saveFileJSONrootEx(const char *preferredName, const void *root, size_t flags
if (res == 0) { if (res == 0) {
if (verbose) { if (verbose) {
PrintAndLogEx(SUCCESS, "Saved to json file " _YELLOW_("%s"), filename); PrintAndLogEx(SUCCESS, "Saved to json file `" _YELLOW_("%s") "`", filename);
} }
free(filename); free(filename);
return PM3_SUCCESS; return PM3_SUCCESS;

View file

@ -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 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 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 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 *)); int prepareJSON(json_t *root, JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *));
/** STUB /** STUB
* @brief Utility function to save WAVE data to a file. This method takes a preferred name, but if that * @brief Utility function to save WAVE data to a file. This method takes a preferred name, but if that