diff --git a/.gitignore b/.gitignore index e808ba2..7ea33d5 100644 --- a/.gitignore +++ b/.gitignore @@ -55,6 +55,9 @@ dkms.conf # Executables **.fap +# Confidential Files +**/archive.txt + # IDEs *.vscode diff --git a/project/backend/archive.c b/project/backend/archive.c new file mode 100644 index 0000000..e59aff9 --- /dev/null +++ b/project/backend/archive.c @@ -0,0 +1,60 @@ +// Header +#include "archive.h" + +// Functions +bool archive_import(Manager* manager) { + // Loading the file + char* data = store_load(Store_File_Path_Archive, "Data"); + + // Did we fail to load the data? + if (!data) {return false;} + + // Splitting the string up + int count = 0; + char** split = split_string(data, F_DELIMITER, &count); + + // Free data after split + free(data); + + // Going through all passwords + for (int i = 0; i < count; i++) { + // Skipping to each password + if (i % 4 == 0) { + // DEBUG + FURI_LOG_D(TAG, "(%i) Adding password.", i); + + // Allocating memory for a password + Password* new_password = pass_init( + split[i], + split[i+1], + split[i+2], + 0 + ); + + // Saving the new password + manager_savepass(manager, new_password); + + // Freeing that password + pass_free(new_password); + } + } + + // Wiping the original file + store_save(Store_File_Path_Archive, "Data", "Done."); + + // Cleanup + free(split); + + // Return result + return true; +} +bool archive_export(Manager* manager) { + // Result + bool result = true; + + // UNUSED variable + UNUSED(manager); + + // Return result + return result; +} \ No newline at end of file diff --git a/project/backend/archive.h b/project/backend/archive.h new file mode 100644 index 0000000..73cd976 --- /dev/null +++ b/project/backend/archive.h @@ -0,0 +1,13 @@ +// Define once +#ifndef H_ARCHIVE +#define H_ARCHIVE + +// Libraries +#include "base.h" +#include "manager.h" + +// Functions +bool archive_import(Manager* manager); +bool archive_export(Manager* manager); + +#endif \ No newline at end of file diff --git a/project/backend/base.c b/project/backend/base.c new file mode 100644 index 0000000..330c1ea --- /dev/null +++ b/project/backend/base.c @@ -0,0 +1,40 @@ +// Header +#include "base.h" + +// Libraries +#include + +// Functions +char** split_string(const char* str, char delimiter, int* count) { + // Calculate how many substrings will be created + int i, numSubstrings = 1; + for (i = 0; str[i] != '\0'; i++) { + if (str[i] == delimiter) { + numSubstrings++; + } + } + + // Allocate memory for the array of strings + char** result = malloc(numSubstrings * sizeof(char*)); + *count = numSubstrings; + + // Split the string + int start = 0, substringIndex = 0; + for (i = 0; str[i] != '\0'; i++) { + if (str[i] == delimiter) { + int length = i - start; + result[substringIndex] = malloc((length + 1) * sizeof(char)); + strncpy(result[substringIndex], str + start, length); + result[substringIndex][length] = '\0'; + start = i + 1; + substringIndex++; + } + } + // Add the last substring + int length = i - start; + result[substringIndex] = malloc((length + 1) * sizeof(char)); + strncpy(result[substringIndex], str + start, length); + result[substringIndex][length] = '\0'; + + return result; +} \ No newline at end of file diff --git a/project/backend/base.h b/project/backend/base.h index 05369ea..220aaab 100644 --- a/project/backend/base.h +++ b/project/backend/base.h @@ -9,4 +9,9 @@ #define F_DATA EXT_PATH("apps_data/fpass/data.bin") #define F_ARCHIVE EXT_PATH("apps_data/fpass/archive.txt") +#define F_DELIMITER '\v' + +// Functions +char** split_string(const char* str, char delimiter, int* count); + #endif \ No newline at end of file diff --git a/project/backend/manager.c b/project/backend/manager.c index 427c296..28c5a47 100644 --- a/project/backend/manager.c +++ b/project/backend/manager.c @@ -2,39 +2,6 @@ #include "manager.h" // Functions - Private -char** split_string(const char* str, char delimiter, int* count) { - // Calculate how many substrings will be created - int i, numSubstrings = 1; - for (i = 0; str[i] != '\0'; i++) { - if (str[i] == delimiter) { - numSubstrings++; - } - } - - // Allocate memory for the array of strings - char** result = malloc(numSubstrings * sizeof(char*)); - *count = numSubstrings; - - // Split the string - int start = 0, substringIndex = 0; - for (i = 0; str[i] != '\0'; i++) { - if (str[i] == delimiter) { - int length = i - start; - result[substringIndex] = malloc((length + 1) * sizeof(char)); - strncpy(result[substringIndex], str + start, length); - result[substringIndex][length] = '\0'; - start = i + 1; - substringIndex++; - } - } - // Add the last substring - int length = i - start; - result[substringIndex] = malloc((length + 1) * sizeof(char)); - strncpy(result[substringIndex], str + start, length); - result[substringIndex][length] = '\0'; - - return result; -} void manager_loadnames(Manager* manager){ // Loading the names of all passwords char* data = secure_storage_load(manager->secure_storage, Store_File_Path_Data, "Data"); @@ -53,7 +20,7 @@ void manager_loadnames(Manager* manager){ // Splitting string int count = 0; - char** split = split_string(data, '|', &count); + char** split = split_string(data, F_DELIMITER, &count); // Freeing data free(data); @@ -111,7 +78,10 @@ void manager_savepass(Manager* manager, Password* pass) { FuriString* n_result = furi_string_alloc(); // Setting stuff - furi_string_printf(n_result, "%s|%s|%s|0", pass->name, pass->user, pass->phrase); + furi_string_printf(n_result, "%s%c%s%c%s%c0", + pass->name, F_DELIMITER, + pass->user, F_DELIMITER, + pass->phrase, F_DELIMITER); // Saving the data char* data = malloc(sizeof(char) * furi_string_size(n_result)); @@ -126,7 +96,11 @@ void manager_savepass(Manager* manager, Password* pass) { FuriString* new_result = furi_string_alloc(); // Appending addon to result - furi_string_printf(new_result, "%s|%s|%s|%s|0", result, pass->name, pass->user, pass->phrase); + furi_string_printf(new_result, "%s%c%s%c%s%c%s%c0", + result, F_DELIMITER, + pass->name, F_DELIMITER, + pass->user, F_DELIMITER, + pass->phrase, F_DELIMITER); // Saving the data char* data = malloc(sizeof(char) * furi_string_size(new_result)); @@ -156,7 +130,7 @@ void manager_loadpass(Manager* manager, char* name) { // Splitting string int count = 0; - char** split = split_string(data, '|', &count); + char** split = split_string(data, F_DELIMITER, &count); // Unloading data free(data); diff --git a/project/ui/scenes/import.c b/project/ui/scenes/import.c index cc4cdc3..e0c26dd 100644 --- a/project/ui/scenes/import.c +++ b/project/ui/scenes/import.c @@ -1,6 +1,9 @@ // Header #include "import.h" +// Others +#include "../../backend/archive.h" + // Functions void FP_Scene_Callback_Import(DialogExResult result, void* context){ // Setting context @@ -81,13 +84,25 @@ bool FP_Scene_Event_Import(void* context, SceneManagerEvent event){ // What should we do? switch (event.event) { case DialogExResultCenter: // Yes + // Importing the passwords + bool success = archive_import(app->manager); + // Telling the user it worked! - dialog_ex_set_text( - app->dialog, - "Imported Successfully.", - 5, 15, - AlignLeft, AlignTop - ); + if (success) { + dialog_ex_set_text( + app->dialog, + "Imported Successfully.", + 5, 15, + AlignLeft, AlignTop + ); + } else { + dialog_ex_set_text( + app->dialog, + "Failed to import passwords", + 5, 15, + AlignLeft, AlignTop + ); + } dialog_ex_set_center_button_text( app->dialog, "Okay" diff --git a/tools/archive.py b/tools/archive.py index 6fe1f4c..ecdb3fe 100644 --- a/tools/archive.py +++ b/tools/archive.py @@ -10,6 +10,7 @@ import sys import json # Variables +file_delimiter = '\v' all_passwords = "" # Functions @@ -39,7 +40,7 @@ def insert_passwords(json_data): password = item["login"]["password"] # Adding it into our password array - all_passwords += name + "|" + username + "|" + password + "|0|" + all_passwords += name + file_delimiter + username + file_delimiter + password + file_delimiter + "0" + file_delimiter def save_data(): # Global global all_passwords