Allowed for loading different file paths

This commit is contained in:
Maddox Werts 2024-09-09 08:16:20 -04:00
parent a522c85d27
commit fb74bb6fc3
6 changed files with 55 additions and 22 deletions

View file

@ -5,6 +5,8 @@
// Constants // Constants
#define TAG "FlippyPass" #define TAG "FlippyPass"
#define DIR EXT_PATH("apps_data/fpass") #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 #endif

View file

@ -37,7 +37,7 @@ char** split_string(const char* str, char delimiter, int* count) {
} }
void manager_loadnames(Manager* manager){ void manager_loadnames(Manager* manager){
// Loading the names of all passwords // 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? // Do we not have any passwords?
if (!data) { if (!data) {
@ -103,7 +103,7 @@ Manager* manager_init(char* key) {
// Functions // Functions
void manager_savepass(Manager* manager, Password* pass) { void manager_savepass(Manager* manager, Password* pass) {
// Our resulting string // 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? // Is the result empty?
if(!result) { if(!result) {
@ -116,7 +116,7 @@ void manager_savepass(Manager* manager, Password* pass) {
// Saving the data // Saving the data
char* data = malloc(sizeof(char) * furi_string_size(n_result)); char* data = malloc(sizeof(char) * furi_string_size(n_result));
strcpy(data, furi_string_get_cstr(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 // Cleanup
furi_string_free(n_result); furi_string_free(n_result);
@ -131,7 +131,7 @@ void manager_savepass(Manager* manager, Password* pass) {
// Saving the data // Saving the data
char* data = malloc(sizeof(char) * furi_string_size(new_result)); char* data = malloc(sizeof(char) * furi_string_size(new_result));
strcpy(data, furi_string_get_cstr(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 // Cleanup
furi_string_free(new_result); 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); FURI_LOG_D(TAG, "Loading password of name %s", name);
// Loading password string // 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 // Splitting string
int count = 0; int count = 0;

View file

@ -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. Currently, We will use XOR encryption & XOR decryption.
Not too secure but it's simple. Not too secure but it's simple.
*/ */
// Loading data from storage // Loading data from storage
char* data = store_load(tag); char* data = store_load(path, tag);
// Is the data empty? // Is the data empty?
if (!data) { if (!data) {
@ -52,12 +52,12 @@ char* secure_storage_load(SecureStorage* secure_storage, char* tag){
// Returning result // Returning result
return data; 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 // Encrypting data
xor_encrypt_decrypt(data, secure_storage->key); xor_encrypt_decrypt(data, secure_storage->key);
// Saving it // Saving it
store_save(tag, data); store_save(path, tag, data);
} }
void secure_storage_free(SecureStorage* secure_storage){ void secure_storage_free(SecureStorage* secure_storage){
free(secure_storage->key); free(secure_storage->key);

View file

@ -14,8 +14,8 @@ typedef struct {
SecureStorage* secure_storage_init(char* key); SecureStorage* secure_storage_init(char* key);
// Functions // Functions
char* secure_storage_load(SecureStorage* secure_storage, char* tag); char* secure_storage_load(SecureStorage* secure_storage, Store_File_Path path, char* tag);
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);
void secure_storage_free(SecureStorage* secure_storage); void secure_storage_free(SecureStorage* secure_storage);
#endif #endif

View file

@ -2,6 +2,9 @@
#include "store.h" #include "store.h"
#include <flipper_format.h> #include <flipper_format.h>
// Constants
#define FILE_PATH_SIZE 128
// Functions - Private // Functions - Private
void store_folder_init() { void store_folder_init() {
// Opening storage record // Opening storage record
@ -22,7 +25,18 @@ void store_folder_init() {
} }
// Functions // 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 // Init Folders
store_folder_init(); store_folder_init();
@ -42,10 +56,10 @@ char* store_load(char* type) {
} }
// DEBUG // DEBUG
FURI_LOG_D(TAG, "Opening file at %s", PATH); FURI_LOG_D(TAG, "Opening file at %s", file_path);
// Opening file // 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."); FURI_LOG_E(TAG, "Sorry, please register.");
// Cleaning up // Cleaning up
@ -85,7 +99,18 @@ char* store_load(char* type) {
// Returning result // Returning result
return 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 // Creating new file
Storage* _storage = furi_record_open(RECORD_STORAGE); Storage* _storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* _format = flipper_format_file_alloc(_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); furi_string_set_str(_data, data);
// Opening new file // Opening new file
if (!flipper_format_file_open_always(_format, PATH)) { if (!flipper_format_file_open_always(_format, file_path)) {
// DEBUG // 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 // Cleanup
furi_string_free(_data); furi_string_free(_data);
@ -114,10 +139,10 @@ void store_save(char* type, char* data) {
// Writing string // Writing string
if (!flipper_format_write_string(_format, type, _data)) { 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; return;
} else { } 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 // Closing file

View file

@ -6,8 +6,14 @@
#include <furi.h> #include <furi.h>
#include "base.h" #include "base.h"
// Enums
typedef enum {
Store_File_Path_Data,
Store_File_Path_Archive
} Store_File_Path;
// Functions // Functions
char* store_load(char* type); char* store_load(Store_File_Path path, char* type);
void store_save(char* type, char* data); void store_save(Store_File_Path path, char* type, char* data);
#endif #endif