From fb74bb6fc374b6ec02a6cfe98fa1453f2e07e1d6 Mon Sep 17 00:00:00 2001 From: objnull Date: Mon, 9 Sep 2024 08:16:20 -0400 Subject: [PATCH] Allowed for loading different file paths --- project/backend/base.h | 4 +++- project/backend/manager.c | 10 +++++----- project/backend/secure.c | 8 ++++---- project/backend/secure.h | 4 ++-- project/backend/store.c | 41 +++++++++++++++++++++++++++++++-------- project/backend/store.h | 10 ++++++++-- 6 files changed, 55 insertions(+), 22 deletions(-) diff --git a/project/backend/base.h b/project/backend/base.h index c9b4e40..75f2c44 100644 --- a/project/backend/base.h +++ b/project/backend/base.h @@ -5,6 +5,8 @@ // Constants #define TAG "FlippyPass" #define DIR EXT_PATH("apps_data/fpass") -#define PATH EXT_PATH("apps_data/fpass/data.bin") + +#define F_DATA EXT_PATH("apps_data/fpass/data.bin") +#define F_ARCHIVE EXT_PATH("apps_data/fpass/data.bin") #endif \ No newline at end of file diff --git a/project/backend/manager.c b/project/backend/manager.c index eb94376..427c296 100644 --- a/project/backend/manager.c +++ b/project/backend/manager.c @@ -37,7 +37,7 @@ char** split_string(const char* str, char delimiter, int* count) { } void manager_loadnames(Manager* manager){ // Loading the names of all passwords - char* data = secure_storage_load(manager->secure_storage, "Data"); + char* data = secure_storage_load(manager->secure_storage, Store_File_Path_Data, "Data"); // Do we not have any passwords? if (!data) { @@ -103,7 +103,7 @@ Manager* manager_init(char* key) { // Functions void manager_savepass(Manager* manager, Password* pass) { // Our resulting string - char* result = secure_storage_load(manager->secure_storage, "Data"); + char* result = secure_storage_load(manager->secure_storage, Store_File_Path_Data, "Data"); // Is the result empty? if(!result) { @@ -116,7 +116,7 @@ void manager_savepass(Manager* manager, Password* pass) { // Saving the data char* data = malloc(sizeof(char) * furi_string_size(n_result)); strcpy(data, furi_string_get_cstr(n_result)); - secure_storage_save(manager->secure_storage, "Data", data); + secure_storage_save(manager->secure_storage, Store_File_Path_Data, "Data", data); // Cleanup furi_string_free(n_result); @@ -131,7 +131,7 @@ void manager_savepass(Manager* manager, Password* pass) { // Saving the data char* data = malloc(sizeof(char) * furi_string_size(new_result)); strcpy(data, furi_string_get_cstr(new_result)); - secure_storage_save(manager->secure_storage, "Data", data); + secure_storage_save(manager->secure_storage, Store_File_Path_Data, "Data", data); // Cleanup furi_string_free(new_result); @@ -152,7 +152,7 @@ void manager_loadpass(Manager* manager, char* name) { FURI_LOG_D(TAG, "Loading password of name %s", name); // Loading password string - char* data = secure_storage_load(manager->secure_storage, "Data"); + char* data = secure_storage_load(manager->secure_storage, Store_File_Path_Data, "Data"); // Splitting string int count = 0; diff --git a/project/backend/secure.c b/project/backend/secure.c index be2f6a3..8df84a6 100644 --- a/project/backend/secure.c +++ b/project/backend/secure.c @@ -29,14 +29,14 @@ void xor_encrypt_decrypt(char* data, char* key){ } } -char* secure_storage_load(SecureStorage* secure_storage, char* tag){ +char* secure_storage_load(SecureStorage* secure_storage, Store_File_Path path, char* tag){ /* Currently, We will use XOR encryption & XOR decryption. Not too secure but it's simple. */ // Loading data from storage - char* data = store_load(tag); + char* data = store_load(path, tag); // Is the data empty? if (!data) { @@ -52,12 +52,12 @@ char* secure_storage_load(SecureStorage* secure_storage, char* tag){ // Returning result return data; } -void secure_storage_save(SecureStorage* secure_storage, char* tag, char* data){ +void secure_storage_save(SecureStorage* secure_storage, Store_File_Path path, char* tag, char* data){ // Encrypting data xor_encrypt_decrypt(data, secure_storage->key); // Saving it - store_save(tag, data); + store_save(path, tag, data); } void secure_storage_free(SecureStorage* secure_storage){ free(secure_storage->key); diff --git a/project/backend/secure.h b/project/backend/secure.h index 58e08f0..31bc906 100644 --- a/project/backend/secure.h +++ b/project/backend/secure.h @@ -14,8 +14,8 @@ typedef struct { SecureStorage* secure_storage_init(char* key); // Functions -char* secure_storage_load(SecureStorage* secure_storage, char* tag); -void secure_storage_save(SecureStorage* secure_storage, char* tag, char* data); +char* secure_storage_load(SecureStorage* secure_storage, Store_File_Path path, char* tag); +void secure_storage_save(SecureStorage* secure_storage, Store_File_Path path, char* tag, char* data); void secure_storage_free(SecureStorage* secure_storage); #endif \ No newline at end of file diff --git a/project/backend/store.c b/project/backend/store.c index 18781ce..ffd9e0d 100644 --- a/project/backend/store.c +++ b/project/backend/store.c @@ -2,6 +2,9 @@ #include "store.h" #include +// Constants +#define FILE_PATH_SIZE 128 + // Functions - Private void store_folder_init() { // Opening storage record @@ -22,7 +25,18 @@ void store_folder_init() { } // Functions -char* store_load(char* type) { +char* store_load(Store_File_Path path, char* type) { + // Setting file path + char* file_path = malloc(sizeof(char) * FILE_PATH_SIZE); + switch (path) { + case Store_File_Path_Data: + file_path = F_DATA; + break; + case Store_File_Path_Archive: + file_path = F_ARCHIVE; + break; + } + // Init Folders store_folder_init(); @@ -42,10 +56,10 @@ char* store_load(char* type) { } // DEBUG - FURI_LOG_D(TAG, "Opening file at %s", PATH); + FURI_LOG_D(TAG, "Opening file at %s", file_path); // Opening file - if (!flipper_format_file_open_existing(_format, PATH)) { + if (!flipper_format_file_open_existing(_format, file_path)) { FURI_LOG_E(TAG, "Sorry, please register."); // Cleaning up @@ -85,7 +99,18 @@ char* store_load(char* type) { // Returning result return result; } -void store_save(char* type, char* data) { +void store_save(Store_File_Path path, char* type, char* data) { + // Setting file path + char* file_path = malloc(sizeof(char) * FILE_PATH_SIZE); + switch (path) { + case Store_File_Path_Data: + file_path = F_DATA; + break; + case Store_File_Path_Archive: + file_path = F_ARCHIVE; + break; + } + // Creating new file Storage* _storage = furi_record_open(RECORD_STORAGE); FlipperFormat* _format = flipper_format_file_alloc(_storage); @@ -101,9 +126,9 @@ void store_save(char* type, char* data) { furi_string_set_str(_data, data); // Opening new file - if (!flipper_format_file_open_always(_format, PATH)) { + if (!flipper_format_file_open_always(_format, file_path)) { // DEBUG - FURI_LOG_E(TAG, "Failed to create/open file at %s", PATH); + FURI_LOG_E(TAG, "Failed to create/open file at %s", file_path); // Cleanup furi_string_free(_data); @@ -114,10 +139,10 @@ void store_save(char* type, char* data) { // Writing string if (!flipper_format_write_string(_format, type, _data)) { - FURI_LOG_E(TAG, "Failed to write to file at %s", PATH); + FURI_LOG_E(TAG, "Failed to write to file at %s", file_path); return; } else { - FURI_LOG_D(TAG, "Successfully wrote to file at %s", PATH); + FURI_LOG_D(TAG, "Successfully wrote to file at %s", file_path); } // Closing file diff --git a/project/backend/store.h b/project/backend/store.h index e0ccfeb..3c620aa 100644 --- a/project/backend/store.h +++ b/project/backend/store.h @@ -6,8 +6,14 @@ #include #include "base.h" +// Enums +typedef enum { + Store_File_Path_Data, + Store_File_Path_Archive +} Store_File_Path; + // Functions -char* store_load(char* type); -void store_save(char* type, char* data); +char* store_load(Store_File_Path path, char* type); +void store_save(Store_File_Path path, char* type, char* data); #endif \ No newline at end of file