From 0961b019506b3800d551ce628453f94b7f07823e Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Mon, 14 Oct 2024 21:30:41 +0200 Subject: [PATCH] add option --json to pref show --- CHANGELOG.md | 1 + client/src/fileutils.c | 11 +++++++++++ client/src/fileutils.h | 1 + client/src/preferences.c | 17 +++++++++++++++++ client/src/preferences.h | 1 + 5 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05097e5fa..07e6d63a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Changed `pref show` - add option to dump as JSON (@doegox) - Changed `mf_backdoor_dump.py`- use faster ecfill/eview (@doegox) - Changed `hf mf ecfill` - wait for execution and return status (@doegox) - Changed `hf 14a reader` - added option to wait for a card (@doegox) diff --git a/client/src/fileutils.c b/client/src/fileutils.c index 91369af43..f93c15aa1 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -818,6 +818,17 @@ int saveFileJSONrootEx(const char *preferredName, const void *root, size_t flags return PM3_EFILE; } +char *sprintJSON(JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *)) { + + json_t *root = json_object(); + if (prepareJSON(root, ftype, data, datalen, verbose, callback) != PM3_SUCCESS) { + return NULL; + } + char *s = json_dumps(root, JSON_INDENT(2)); + json_decref(root); + return s; +} + // wave file of trace, int saveFileWAVE(const char *preferredName, const int *data, size_t datalen) { diff --git a/client/src/fileutils.h b/client/src/fileutils.h index f96eaa44d..023820f64 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -142,6 +142,7 @@ int saveFileJSONex(const char *preferredName, JSONFileType ftype, uint8_t *data, 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, savePaths_t e_save_path); int prepareJSON(json_t *root, JSONFileType ftype, uint8_t *data, size_t datalen, bool verbose, void (*callback)(json_t *)); +char *sprintJSON(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 * file already exists, it tries with another name until it finds something suitable. diff --git a/client/src/preferences.c b/client/src/preferences.c index a3d05b774..4c2eb30dc 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -168,6 +168,16 @@ int preferences_save(void) { return PM3_SUCCESS; } +// Dump all settings from memory (struct) to console in JSON +int preferences_dump(void) { + uint8_t dummyData = 0x00; + size_t dummyDL = 0x01; + char *s = sprintJSON(jsfCustom, &dummyData, dummyDL, true, &preferences_save_callback); + PrintAndLogEx(NORMAL, "%s", s); + free(s); + return PM3_SUCCESS; +} + void preferences_save_callback(json_t *root) { JsonSaveStr(root, "FileType", "settings"); @@ -1393,11 +1403,18 @@ static int CmdPrefShow(const char *Cmd) { ); void *argtable[] = { arg_param_begin, + arg_lit0("j", "json", "Dump prefs as JSON"), arg_param_end }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + bool json_dump = arg_get_lit(ctx, 1); CLIParserFree(ctx); + if (json_dump) { + preferences_dump(); + return PM3_SUCCESS; + } if (g_session.preferences_loaded) { char *fn = prefGetFilename(); PrintAndLogEx(NORMAL, ""); diff --git a/client/src/preferences.h b/client/src/preferences.h index e49b64d9c..f3169eb32 100644 --- a/client/src/preferences.h +++ b/client/src/preferences.h @@ -27,6 +27,7 @@ int CmdPreferences(const char *Cmd); int preferences_load(void); int preferences_save(void); +int preferences_dump(void); void preferences_save_callback(json_t *root); void preferences_load_callback(json_t *root);