This commit is contained in:
Nikola Petrov 2023-08-15 16:54:08 +02:00
parent 43fda0a392
commit 6d9404b9b5
6 changed files with 97 additions and 105 deletions

View File

@ -2,12 +2,12 @@
enum class Glob_Result {
OOM_ERROR,
ENCODING_ERROR,
SYNTAX_ERROR,
UNMATCHED,
MATCHED,
} ;
OOM_ERROR,
ENCODING_ERROR,
SYNTAX_ERROR,
UNMATCHED,
MATCHED,
};
// https://github.com/tsoding/glob.h
Glob_Result glob(const char* pattern, const char* text);

View File

@ -35,7 +35,7 @@ bool Buffer::resize(size_t new_size)
void Buffer::add(uint8_t* data, size_t data_size)
{
if (taken + data_size > size)
if(!resize(size + data_size)) return;
if (!resize(size + data_size)) return;
memcpy_s(buffer + taken, size - taken, data, data_size);
taken += data_size;

View File

@ -34,7 +34,7 @@ bool Cryptography::encrypt(Buffer* plain, Buffer* encrypted)
if (!ctx) return handleErrors();
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) return handleErrors();
if (1 != EVP_EncryptUpdate(ctx, encrypted->buffer, (int*)& encrypted->taken, plain->buffer, plain->taken)) return handleErrors();
if (1 != EVP_EncryptUpdate(ctx, encrypted->buffer, (int*)&encrypted->taken, plain->buffer, plain->taken)) return handleErrors();
int final_len;
if (1 != EVP_EncryptFinal_ex(ctx, encrypted->buffer + encrypted->taken, &final_len)) return handleErrors();
encrypted->taken += final_len;
@ -54,7 +54,7 @@ bool Cryptography::decrypt(Buffer* encrypted, Buffer* decrypted)
if (!ctx) return handleErrors();
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) return handleErrors();
if (1 != EVP_DecryptUpdate(ctx, decrypted->buffer, (int*) & decrypted->taken, encrypted->buffer, encrypted->taken)) return handleErrors();
if (1 != EVP_DecryptUpdate(ctx, decrypted->buffer, (int*)&decrypted->taken, encrypted->buffer, encrypted->taken)) return handleErrors();
int final_len;
if (1 != EVP_DecryptFinal_ex(ctx, decrypted->buffer + decrypted->taken, &final_len)) return handleErrors();
decrypted->taken += final_len;

View File

@ -41,7 +41,6 @@ bool save_buffer_to_file(Buffer* buffer)
bool load_buffer_from_file(Buffer* buffer)
{
std::ifstream file("passwords.bin", std::ios::binary);
if (!file.is_open()) return false;
@ -116,6 +115,5 @@ Args get_args(int argc, char** argv, char** label) {
return Args::Input;
}
return Args::Error;
}

View File

@ -2,93 +2,93 @@
Glob_Result glob(const char* pattern, const char* text)
{
while (*pattern != '\0' && *text != '\0') {
switch (*pattern) {
case '?': {
pattern += 1;
text += 1;
}
break;
while (*pattern != '\0' && *text != '\0') {
switch (*pattern) {
case '?': {
pattern += 1;
text += 1;
}
break;
case '*': {
Glob_Result result = glob(pattern + 1, text);
if (result != Glob_Result::UNMATCHED) return result;
text += 1;
}
break;
case '*': {
Glob_Result result = glob(pattern + 1, text);
if (result != Glob_Result::UNMATCHED) return result;
text += 1;
}
break;
case '[': {
bool matched = false;
bool negate = false;
case '[': {
bool matched = false;
bool negate = false;
pattern += 1; // skipping [
if (*pattern == '\0') return Glob_Result::SYNTAX_ERROR; // unclosed [
pattern += 1; // skipping [
if (*pattern == '\0') return Glob_Result::SYNTAX_ERROR; // unclosed [
if (*pattern == '!') {
negate = true;
pattern += 1;
if (*pattern == '\0') return Glob_Result::SYNTAX_ERROR; // unclosed [
}
if (*pattern == '!') {
negate = true;
pattern += 1;
if (*pattern == '\0') return Glob_Result::SYNTAX_ERROR; // unclosed [
}
char prev = *pattern;
matched |= prev == *text;
pattern += 1;
char prev = *pattern;
matched |= prev == *text;
pattern += 1;
while (*pattern != ']' && *pattern != '\0') {
switch (*pattern) {
case '-': {
pattern += 1;
switch (*pattern) {
case ']':
matched |= '-' == *text;
break;
case '\0':
return Glob_Result::SYNTAX_ERROR; // unclosed [
default: {
matched |= prev <= *text && *text <= *pattern;
prev = *pattern;
pattern += 1;
}
}
}
break;
default: {
prev = *pattern;
matched |= prev == *text;
pattern += 1;
}
}
}
while (*pattern != ']' && *pattern != '\0') {
switch (*pattern) {
case '-': {
pattern += 1;
switch (*pattern) {
case ']':
matched |= '-' == *text;
break;
case '\0':
return Glob_Result::SYNTAX_ERROR; // unclosed [
default: {
matched |= prev <= *text && *text <= *pattern;
prev = *pattern;
pattern += 1;
}
}
}
break;
default: {
prev = *pattern;
matched |= prev == *text;
pattern += 1;
}
}
}
if (*pattern != ']') return Glob_Result::SYNTAX_ERROR; // unclosed [
if (negate) matched = !matched;
if (!matched) return Glob_Result::UNMATCHED;
if (*pattern != ']') return Glob_Result::SYNTAX_ERROR; // unclosed [
if (negate) matched = !matched;
if (!matched) return Glob_Result::UNMATCHED;
pattern += 1;
text += 1;
}
break;
pattern += 1;
text += 1;
}
break;
case '\\':
pattern += 1;
if (*pattern == '\0') return Glob_Result::SYNTAX_ERROR; // unfinished escape
// fallthrough
default: {
if (*pattern == *text) {
pattern += 1;
text += 1;
}
else {
return Glob_Result::UNMATCHED;
}
}
}
}
case '\\':
pattern += 1;
if (*pattern == '\0') return Glob_Result::SYNTAX_ERROR; // unfinished escape
// fallthrough
default: {
if (*pattern == *text) {
pattern += 1;
text += 1;
}
else {
return Glob_Result::UNMATCHED;
}
}
}
}
if (*text == '\0') {
while (*pattern == '*') pattern += 1;
if (*pattern == '\0') return Glob_Result::MATCHED;
}
if (*text == '\0') {
while (*pattern == '*') pattern += 1;
if (*pattern == '\0') return Glob_Result::MATCHED;
}
return Glob_Result::UNMATCHED;
return Glob_Result::UNMATCHED;
}

View File

@ -9,9 +9,7 @@ int main(int argc, char** argv)
{
char* label = nullptr;
Args args = get_args(argc, argv, &label);
if(args == Args::Error) return 1;
if (args == Args::Error) return 1;
Buffer encrypted_buffer;
if (!load_buffer_from_file(&encrypted_buffer))
@ -20,8 +18,6 @@ int main(int argc, char** argv)
return 1;
}
printf_s("Input main password:");
std::string user_pass = get_passwd();
if (user_pass.empty())
@ -36,13 +32,11 @@ int main(int argc, char** argv)
if (encrypted_buffer.size > 0)
if (!crypto.decrypt(&encrypted_buffer, &decrypted_buffer)) return 1;
Pass* pass = nullptr;
Pass new_pass = { 0 };
Pass* passwords = nullptr;
std::string new_string;
switch (args)
{
case Args::Get:
@ -63,7 +57,7 @@ int main(int argc, char** argv)
{
generate_password(new_pass.password);
strcpy_s(new_pass.label, MAX_STRING_SIZE, label);
decrypted_buffer.add((uint8_t*) & new_pass, sizeof(Pass));
decrypted_buffer.add((uint8_t*)&new_pass, sizeof(Pass));
pass = &new_pass;
}
@ -125,7 +119,7 @@ int main(int argc, char** argv)
}
return 0;
break;
break;
case Args::Input:
printf_s("Input password for %s:\n", label);