188 lines
No EOL
4.6 KiB
C
188 lines
No EOL
4.6 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;
|
|
}
|
|
|
|
// Constructors
|
|
Manager* manager_init() {
|
|
// Creating result in memory
|
|
Manager* result = malloc(sizeof(Manager));
|
|
|
|
// Storing password
|
|
result->page = 0;
|
|
|
|
// Returning manager
|
|
return result;
|
|
}
|
|
|
|
// Functions
|
|
void manager_switchpage(Manager* manager, int dir) {
|
|
// Switching page
|
|
manager->page += dir;
|
|
|
|
// Avoiding empty pages
|
|
if (manager->page < 0) {manager->page = 0;}
|
|
|
|
// Loading names
|
|
manager_loadnames(manager, manager->page);
|
|
|
|
// Too far?
|
|
if (manager->names[0][0] == '\0') {
|
|
manager_switchpage(manager, -1);
|
|
}
|
|
}
|
|
void manager_loadnames(Manager* manager, int page){
|
|
// DEBUG
|
|
FURI_LOG_D(TAG, "Freeing previous names");
|
|
|
|
// Freeing all prior names
|
|
for (int i = 0; i < 4; i++) {
|
|
// Checking if this name exists:
|
|
if (manager->names[i] != NULL) {
|
|
// DEBUG
|
|
FURI_LOG_D(TAG, "Freed %s", manager->names[i]);
|
|
|
|
// Freeing name
|
|
free(manager->names[i]);
|
|
} else {
|
|
// DEBUG
|
|
FURI_LOG_E(TAG, "Name %i does not exist!", i);
|
|
}
|
|
}
|
|
|
|
// Loading the next four passwords
|
|
char* data = store_load("Data");
|
|
|
|
// Splitting string
|
|
int count = 0;
|
|
char** split = split_string(data, '|', &count);
|
|
|
|
// Freeing data
|
|
free(data);
|
|
|
|
// Temporary variables
|
|
int i_pass = 0;
|
|
int i_name = 0;
|
|
|
|
// Going through split string
|
|
for (int i = 0; i < count; i++) {
|
|
// Checking if it's the 4th number
|
|
if (i % 4 == 0) {
|
|
// It's another password!
|
|
i_pass++;
|
|
|
|
// DEBUG
|
|
FURI_LOG_I(TAG, "(%i) Scanning password %s...", i_pass, split[i]);
|
|
|
|
// Is it on our page?
|
|
if (i_pass > page * 4
|
|
&& i_pass <= (page * 4) + 4) {
|
|
// Adding it to the array!
|
|
manager->names[i_name] = split[i];
|
|
|
|
// Increasing name index
|
|
i_name++;
|
|
|
|
// DEBUG
|
|
FURI_LOG_D(TAG, "Added %s to names", split[i]);
|
|
|
|
// Too big?
|
|
if (i_name > 3) {break;}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (i_name < 4) {
|
|
for (int i = i_name; i < 4; i++) {
|
|
manager->names[i] = "\0";
|
|
}
|
|
}
|
|
|
|
// Cleanup
|
|
free(split);
|
|
}
|
|
void manager_loadpass(Manager* manager, char* name) {
|
|
// Freeing previous password
|
|
if (manager->current != NULL) {
|
|
free(manager->current);
|
|
}
|
|
|
|
// DEBUG
|
|
FURI_LOG_I(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);
|
|
|
|
// DEBUG
|
|
FURI_LOG_D(TAG, "Loaded Password: %s, %s, %s", manager->current->name, manager->current->user, manager->current->phrase);
|
|
}
|
|
void manager_free(Manager* manager) {
|
|
for(int i = 0; i < 4; i++) {
|
|
free(manager->names[i]);
|
|
}
|
|
|
|
free(manager->current);
|
|
free(manager);
|
|
} |