diff --git a/client/src/fileutils.c b/client/src/fileutils.c index 507d896b2..47598c815 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -1224,6 +1224,46 @@ out: return retval; } +int loadFileJSONroot(const char *preferredName, void *out_root, bool verbose) { + + if (out_root == NULL) { + return PM3_EINVARG; + } + + char *path; + int res = searchFile(&path, RESOURCES_SUBDIR, preferredName, ".json", false); + if (res != PM3_SUCCESS) { + return PM3_EFILE; + } + + json_error_t error; + json_t *root = json_load_file(path, 0, &error); + if (verbose) + PrintAndLogEx(SUCCESS, "loaded from JSON file " _YELLOW_("%s"), path); + + free(path); + + int retval = PM3_SUCCESS; + if (root == NULL) { + PrintAndLogEx(ERR, "ERROR: json " _YELLOW_("%s") " error on line %d: %s", preferredName, error.line, error.text); + retval = PM3_ESOFT; + goto out; + } + + if (!json_is_object(root)) { + PrintAndLogEx(ERR, "ERROR: Invalid json " _YELLOW_("%s") " format. root must be an object.", preferredName); + retval = PM3_ESOFT; + goto out; + } + + out_root = root; + return PM3_SUCCESS; + +out: + json_decref(root); + return retval; +} + int loadFileDICTIONARY(const char *preferredName, void *data, size_t *datalen, uint8_t keylen, uint32_t *keycnt) { // t5577 == 4bytes // mifare == 6 bytes diff --git a/client/src/fileutils.h b/client/src/fileutils.h index bc06bd061..3abcd2ab6 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -206,7 +206,7 @@ int loadFileEML_safe(const char *preferredName, void **pdata, size_t *datalen); */ int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, void (*callback)(json_t *)); int loadFileJSONex(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, bool verbose, void (*callback)(json_t *)); - +int loadFileJSONroot(const char *preferredName, void *out_root, bool verbose); /** * @brief Utility function to load data from a DICTIONARY textfile. This method takes a preferred name.