Nikola Petrov 57af63a71c Add tests
2023-08-25 00:29:16 +02:00

111 lines
2.6 KiB
C++

#include "func.h"
#include "glob.h"
#include "Buffer.h"
Pass* find_password(Buffer* buff, const char* label)
{
Pass* passwords = (Pass*)buff->buffer;
for (int i = 0; i < buff->taken / sizeof(Pass); i++)
{
if (glob(label, passwords[i].label) == Glob_Result::MATCHED)
{
return &passwords[i];
}
}
return nullptr;
}
void generate_password(char* password)
{
srand(time(NULL));
char characters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_-+={[}]|:;<,>.?";
for (int i = 0; i < MAX_STRING_SIZE; i++)
{
password[i] = characters[rand() % (sizeof(characters) - 1)];
}
password[MAX_STRING_SIZE - 1] = '\0';
}
bool save_buffer_to_file(Buffer* buffer)
{
std::ofstream file("passwords.bin", std::ios::binary);
if (!file.is_open())
{
printf_s("Error saving file\n");
return false;
}
file.write((char*)buffer->buffer, buffer->taken);
file.close();
return true;
}
bool load_buffer_from_file(Buffer* buffer)
{
std::ifstream file("passwords.bin", std::ios::binary);
if (!file.is_open()) return false;
file.seekg(0, std::ios::end);
buffer->resize(file.tellg());
file.seekg(0, std::ios::beg);
file.read((char*)buffer->buffer, buffer->size);
if (file)
buffer->taken = buffer->size;
else
buffer->taken = file.gcount();
file.close();
return true;
}
void print_usage()
{
printf_s(" Usage:\n\n");
printf_s(" password_manager.exe [flags]\n\n");
printf_s(" Note: <label> max len %d char\n\n", MAX_STRING_SIZE);
printf_s(" Flags:\n\n");
printf_s(" -h \t print this message\n");
printf_s(" <label> \t get password of this label can use GLOB\n");
printf_s(" -g <label> \t generate or update password of this label\n");
printf_s(" -d <label> \t delete password of this label\n");
printf_s(" -i <label> \t input password for this label max %d char\n", MAX_STRING_SIZE);
printf_s(" -s <label> \t show password for this label\n");
printf_s(" -l \t list all labels\n");
printf_s(" -p \t print all passwords\n");
printf_s(" -c \t change master password\n");
}
Arg get_args(int argc, char** argv, char** label) {
if (argc < 2)
{
print_usage();
return Arg::Error;
}
if (!strcmp("-h", argv[1]))
{
print_usage();
return Arg::Error;
}
if (!strcmp("-l", argv[1])) return Arg::List;
if (!strcmp("-p", argv[1])) return Arg::Print_all_p;
if (!strcmp("-c", argv[1])) return Arg::Change;
if (argc < 3)
{
*label = argv[1];
return Arg::Get;
}
*label = argv[2];
if (!strcmp("-g", argv[1])) return Arg::Generate;
if (!strcmp("-d", argv[1])) return Arg::Delete;
if (!strcmp("-i", argv[1])) return Arg::Input;
if (!strcmp("-s", argv[1])) return Arg::Show;
return Arg::Error;
}