FlippyPass/project/backend/manager.c

219 lines
No EOL
5.3 KiB
C

// Header
#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 string_append(char* first, const char* second) {
// Get to the end of string 1
while (*first) {
++first;
}
// Copying Strings
while (*second) {
*first++ = *second++;
}
}
void manager_loadnames(Manager* manager){
// Loading the names of all passwords
char* data = store_load("Data");
// Do we not have any passwords?
if (!data) {
// DEBUG
FURI_LOG_D(TAG, "No passwords found.");
// Creating names
manager->names = malloc(sizeof(char*));
// Returning
return;
}
// Splitting string
int count = 0;
char** split = split_string(data, '|', &count);
// Freeing data
free(data);
// Passwords
/*
Doing some risky bitshifting in order to divide by 4
This little ARMv7 processor would hate division
*/
int num_of_passwords = count >> 2;
// Allocating memory for names array
manager->names = malloc(num_of_passwords * sizeof(char*));
// Going through split string
for (int i = 0; i < count; i++) {
// Checking if it's the 4th number
if (i % 4 == 0) {
// Adding it to the array!
manager->names[manager->count] = split[i];
// It's another password!
manager->count++;
}
}
// Cleanup
free(split);
}
// Constructors
Manager* manager_init() {
// Creating result in memory
Manager* result = malloc(sizeof(Manager));
// Setting variables
result->count = 0;
// Loading all names on start
manager_loadnames(result);
// Returning manager
return result;
}
// Functions
void manager_savepass(Manager* manager, Password* pass) {
// Not using Manager Right Now
UNUSED(manager);
// DEBUG
FURI_LOG_D(TAG, "Saving:\nN:%s\nU:%s\nP:%s",
pass->name,
pass->user,
pass->phrase);
// Our resulting string
char* result = store_load("Data");
// Is the result empty?
if(!result) {
// Allocating result
result = malloc(sizeof(char*));
// Setting stuff
string_append(result, pass->name);
string_append(result, "|");
string_append(result, pass->user);
string_append(result, "|");
string_append(result, pass->phrase);
string_append(result, "|0");
// Saving the data
store_save("Data", result);
// Cleanup
free(result);
free(pass);
} else {
// Our new stuff
char* new_addon = malloc(sizeof(char*));
// Adding the Name
string_append(new_addon, "|");
string_append(new_addon, pass->name);
string_append(new_addon, "|");
string_append(new_addon, pass->user);
string_append(new_addon, "|");
string_append(new_addon, pass->phrase);
string_append(new_addon, "|0");
// Appending addon to result
string_append(result, new_addon);
// Saving the data
store_save("Data", result);
// Cleanup
free(new_addon);
free(result);
free(pass);
}
}
void manager_loadpass(Manager* manager, char* name) {
// Freeing previous password
if (manager->current != NULL) {
free(manager->current);
}
// DEBUG
FURI_LOG_D(TAG, "Loading password of name %s", name);
// Loading password string
char* data = store_load("Data");
// Splitting string
int count = 0;
char** split = split_string(data, '|', &count);
// Unloading data
free(data);
// Going through string to grab data
for(int i = 0; i < count; i++) {
// Is it a multiple of four?
if (i % 4 == 0) {
// Does the password name match?
if (!strcmp(name, split[i])) {
// Setting the new current password
manager->current = pass_init(
name,
split[i+1],
split[i+2],
0);
// Break!
break;
}
}
}
// Cleanup
free(split);
}
void manager_free(Manager* manager) {
for(int i = 0; i < manager->count; i++) {
free(manager->names[i]);
}
free(manager->current);
free(manager);
}